dep-BWSbWtLw.js 2.0 MB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196251972519825199252002520125202252032520425205252062520725208252092521025211252122521325214252152521625217252182521925220252212522225223252242522525226252272522825229252302523125232252332523425235252362523725238252392524025241252422524325244252452524625247252482524925250252512525225253252542525525256252572525825259252602526125262252632526425265252662526725268252692527025271252722527325274252752527625277252782527925280252812528225283252842528525286252872528825289252902529125292252932529425295252962529725298252992530025301253022530325304253052530625307253082530925310253112531225313253142531525316253172531825319253202532125322253232532425325253262532725328253292533025331253322533325334253352533625337253382533925340253412534225343253442534525346253472534825349253502535125352253532535425355253562535725358253592536025361253622536325364253652536625367253682536925370253712537225373253742537525376253772537825379253802538125382253832538425385253862538725388253892539025391253922539325394253952539625397253982539925400254012540225403254042540525406254072540825409254102541125412254132541425415254162541725418254192542025421254222542325424254252542625427254282542925430254312543225433254342543525436254372543825439254402544125442254432544425445254462544725448254492545025451254522545325454254552545625457254582545925460254612546225463254642546525466254672546825469254702547125472254732547425475254762547725478254792548025481254822548325484254852548625487254882548925490254912549225493254942549525496254972549825499255002550125502255032550425505255062550725508255092551025511255122551325514255152551625517255182551925520255212552225523255242552525526255272552825529255302553125532255332553425535255362553725538255392554025541255422554325544255452554625547255482554925550255512555225553255542555525556255572555825559255602556125562255632556425565255662556725568255692557025571255722557325574255752557625577255782557925580255812558225583255842558525586255872558825589255902559125592255932559425595255962559725598255992560025601256022560325604256052560625607256082560925610256112561225613256142561525616256172561825619256202562125622256232562425625256262562725628256292563025631256322563325634256352563625637256382563925640256412564225643256442564525646256472564825649256502565125652256532565425655256562565725658256592566025661256622566325664256652566625667256682566925670256712567225673256742567525676256772567825679256802568125682256832568425685256862568725688256892569025691256922569325694256952569625697256982569925700257012570225703257042570525706257072570825709257102571125712257132571425715257162571725718257192572025721257222572325724257252572625727257282572925730257312573225733257342573525736257372573825739257402574125742257432574425745257462574725748257492575025751257522575325754257552575625757257582575925760257612576225763257642576525766257672576825769257702577125772257732577425775257762577725778257792578025781257822578325784257852578625787257882578925790257912579225793257942579525796257972579825799258002580125802258032580425805258062580725808258092581025811258122581325814258152581625817258182581925820258212582225823258242582525826258272582825829258302583125832258332583425835258362583725838258392584025841258422584325844258452584625847258482584925850258512585225853258542585525856258572585825859258602586125862258632586425865258662586725868258692587025871258722587325874258752587625877258782587925880258812588225883258842588525886258872588825889258902589125892258932589425895258962589725898258992590025901259022590325904259052590625907259082590925910259112591225913259142591525916259172591825919259202592125922259232592425925259262592725928259292593025931259322593325934259352593625937259382593925940259412594225943259442594525946259472594825949259502595125952259532595425955259562595725958259592596025961259622596325964259652596625967259682596925970259712597225973259742597525976259772597825979259802598125982259832598425985259862598725988259892599025991259922599325994259952599625997259982599926000260012600226003260042600526006260072600826009260102601126012260132601426015260162601726018260192602026021260222602326024260252602626027260282602926030260312603226033260342603526036260372603826039260402604126042260432604426045260462604726048260492605026051260522605326054260552605626057260582605926060260612606226063260642606526066260672606826069260702607126072260732607426075260762607726078260792608026081260822608326084260852608626087260882608926090260912609226093260942609526096260972609826099261002610126102261032610426105261062610726108261092611026111261122611326114261152611626117261182611926120261212612226123261242612526126261272612826129261302613126132261332613426135261362613726138261392614026141261422614326144261452614626147261482614926150261512615226153261542615526156261572615826159261602616126162261632616426165261662616726168261692617026171261722617326174261752617626177261782617926180261812618226183261842618526186261872618826189261902619126192261932619426195261962619726198261992620026201262022620326204262052620626207262082620926210262112621226213262142621526216262172621826219262202622126222262232622426225262262622726228262292623026231262322623326234262352623626237262382623926240262412624226243262442624526246262472624826249262502625126252262532625426255262562625726258262592626026261262622626326264262652626626267262682626926270262712627226273262742627526276262772627826279262802628126282262832628426285262862628726288262892629026291262922629326294262952629626297262982629926300263012630226303263042630526306263072630826309263102631126312263132631426315263162631726318263192632026321263222632326324263252632626327263282632926330263312633226333263342633526336263372633826339263402634126342263432634426345263462634726348263492635026351263522635326354263552635626357263582635926360263612636226363263642636526366263672636826369263702637126372263732637426375263762637726378263792638026381263822638326384263852638626387263882638926390263912639226393263942639526396263972639826399264002640126402264032640426405264062640726408264092641026411264122641326414264152641626417264182641926420264212642226423264242642526426264272642826429264302643126432264332643426435264362643726438264392644026441264422644326444264452644626447264482644926450264512645226453264542645526456264572645826459264602646126462264632646426465264662646726468264692647026471264722647326474264752647626477264782647926480264812648226483264842648526486264872648826489264902649126492264932649426495264962649726498264992650026501265022650326504265052650626507265082650926510265112651226513265142651526516265172651826519265202652126522265232652426525265262652726528265292653026531265322653326534265352653626537265382653926540265412654226543265442654526546265472654826549265502655126552265532655426555265562655726558265592656026561265622656326564265652656626567265682656926570265712657226573265742657526576265772657826579265802658126582265832658426585265862658726588265892659026591265922659326594265952659626597265982659926600266012660226603266042660526606266072660826609266102661126612266132661426615266162661726618266192662026621266222662326624266252662626627266282662926630266312663226633266342663526636266372663826639266402664126642266432664426645266462664726648266492665026651266522665326654266552665626657266582665926660266612666226663266642666526666266672666826669266702667126672266732667426675266762667726678266792668026681266822668326684266852668626687266882668926690266912669226693266942669526696266972669826699267002670126702267032670426705267062670726708267092671026711267122671326714267152671626717267182671926720267212672226723267242672526726267272672826729267302673126732267332673426735267362673726738267392674026741267422674326744267452674626747267482674926750267512675226753267542675526756267572675826759267602676126762267632676426765267662676726768267692677026771267722677326774267752677626777267782677926780267812678226783267842678526786267872678826789267902679126792267932679426795267962679726798267992680026801268022680326804268052680626807268082680926810268112681226813268142681526816268172681826819268202682126822268232682426825268262682726828268292683026831268322683326834268352683626837268382683926840268412684226843268442684526846268472684826849268502685126852268532685426855268562685726858268592686026861268622686326864268652686626867268682686926870268712687226873268742687526876268772687826879268802688126882268832688426885268862688726888268892689026891268922689326894268952689626897268982689926900269012690226903269042690526906269072690826909269102691126912269132691426915269162691726918269192692026921269222692326924269252692626927269282692926930269312693226933269342693526936269372693826939269402694126942269432694426945269462694726948269492695026951269522695326954269552695626957269582695926960269612696226963269642696526966269672696826969269702697126972269732697426975269762697726978269792698026981269822698326984269852698626987269882698926990269912699226993269942699526996269972699826999270002700127002270032700427005270062700727008270092701027011270122701327014270152701627017270182701927020270212702227023270242702527026270272702827029270302703127032270332703427035270362703727038270392704027041270422704327044270452704627047270482704927050270512705227053270542705527056270572705827059270602706127062270632706427065270662706727068270692707027071270722707327074270752707627077270782707927080270812708227083270842708527086270872708827089270902709127092270932709427095270962709727098270992710027101271022710327104271052710627107271082710927110271112711227113271142711527116271172711827119271202712127122271232712427125271262712727128271292713027131271322713327134271352713627137271382713927140271412714227143271442714527146271472714827149271502715127152271532715427155271562715727158271592716027161271622716327164271652716627167271682716927170271712717227173271742717527176271772717827179271802718127182271832718427185271862718727188271892719027191271922719327194271952719627197271982719927200272012720227203272042720527206272072720827209272102721127212272132721427215272162721727218272192722027221272222722327224272252722627227272282722927230272312723227233272342723527236272372723827239272402724127242272432724427245272462724727248272492725027251272522725327254272552725627257272582725927260272612726227263272642726527266272672726827269272702727127272272732727427275272762727727278272792728027281272822728327284272852728627287272882728927290272912729227293272942729527296272972729827299273002730127302273032730427305273062730727308273092731027311273122731327314273152731627317273182731927320273212732227323273242732527326273272732827329273302733127332273332733427335273362733727338273392734027341273422734327344273452734627347273482734927350273512735227353273542735527356273572735827359273602736127362273632736427365273662736727368273692737027371273722737327374273752737627377273782737927380273812738227383273842738527386273872738827389273902739127392273932739427395273962739727398273992740027401274022740327404274052740627407274082740927410274112741227413274142741527416274172741827419274202742127422274232742427425274262742727428274292743027431274322743327434274352743627437274382743927440274412744227443274442744527446274472744827449274502745127452274532745427455274562745727458274592746027461274622746327464274652746627467274682746927470274712747227473274742747527476274772747827479274802748127482274832748427485274862748727488274892749027491274922749327494274952749627497274982749927500275012750227503275042750527506275072750827509275102751127512275132751427515275162751727518275192752027521275222752327524275252752627527275282752927530275312753227533275342753527536275372753827539275402754127542275432754427545275462754727548275492755027551275522755327554275552755627557275582755927560275612756227563275642756527566275672756827569275702757127572275732757427575275762757727578275792758027581275822758327584275852758627587275882758927590275912759227593275942759527596275972759827599276002760127602276032760427605276062760727608276092761027611276122761327614276152761627617276182761927620276212762227623276242762527626276272762827629276302763127632276332763427635276362763727638276392764027641276422764327644276452764627647276482764927650276512765227653276542765527656276572765827659276602766127662276632766427665276662766727668276692767027671276722767327674276752767627677276782767927680276812768227683276842768527686276872768827689276902769127692276932769427695276962769727698276992770027701277022770327704277052770627707277082770927710277112771227713277142771527716277172771827719277202772127722277232772427725277262772727728277292773027731277322773327734277352773627737277382773927740277412774227743277442774527746277472774827749277502775127752277532775427755277562775727758277592776027761277622776327764277652776627767277682776927770277712777227773277742777527776277772777827779277802778127782277832778427785277862778727788277892779027791277922779327794277952779627797277982779927800278012780227803278042780527806278072780827809278102781127812278132781427815278162781727818278192782027821278222782327824278252782627827278282782927830278312783227833278342783527836278372783827839278402784127842278432784427845278462784727848278492785027851278522785327854278552785627857278582785927860278612786227863278642786527866278672786827869278702787127872278732787427875278762787727878278792788027881278822788327884278852788627887278882788927890278912789227893278942789527896278972789827899279002790127902279032790427905279062790727908279092791027911279122791327914279152791627917279182791927920279212792227923279242792527926279272792827929279302793127932279332793427935279362793727938279392794027941279422794327944279452794627947279482794927950279512795227953279542795527956279572795827959279602796127962279632796427965279662796727968279692797027971279722797327974279752797627977279782797927980279812798227983279842798527986279872798827989279902799127992279932799427995279962799727998279992800028001280022800328004280052800628007280082800928010280112801228013280142801528016280172801828019280202802128022280232802428025280262802728028280292803028031280322803328034280352803628037280382803928040280412804228043280442804528046280472804828049280502805128052280532805428055280562805728058280592806028061280622806328064280652806628067280682806928070280712807228073280742807528076280772807828079280802808128082280832808428085280862808728088280892809028091280922809328094280952809628097280982809928100281012810228103281042810528106281072810828109281102811128112281132811428115281162811728118281192812028121281222812328124281252812628127281282812928130281312813228133281342813528136281372813828139281402814128142281432814428145281462814728148281492815028151281522815328154281552815628157281582815928160281612816228163281642816528166281672816828169281702817128172281732817428175281762817728178281792818028181281822818328184281852818628187281882818928190281912819228193281942819528196281972819828199282002820128202282032820428205282062820728208282092821028211282122821328214282152821628217282182821928220282212822228223282242822528226282272822828229282302823128232282332823428235282362823728238282392824028241282422824328244282452824628247282482824928250282512825228253282542825528256282572825828259282602826128262282632826428265282662826728268282692827028271282722827328274282752827628277282782827928280282812828228283282842828528286282872828828289282902829128292282932829428295282962829728298282992830028301283022830328304283052830628307283082830928310283112831228313283142831528316283172831828319283202832128322283232832428325283262832728328283292833028331283322833328334283352833628337283382833928340283412834228343283442834528346283472834828349283502835128352283532835428355283562835728358283592836028361283622836328364283652836628367283682836928370283712837228373283742837528376283772837828379283802838128382283832838428385283862838728388283892839028391283922839328394283952839628397283982839928400284012840228403284042840528406284072840828409284102841128412284132841428415284162841728418284192842028421284222842328424284252842628427284282842928430284312843228433284342843528436284372843828439284402844128442284432844428445284462844728448284492845028451284522845328454284552845628457284582845928460284612846228463284642846528466284672846828469284702847128472284732847428475284762847728478284792848028481284822848328484284852848628487284882848928490284912849228493284942849528496284972849828499285002850128502285032850428505285062850728508285092851028511285122851328514285152851628517285182851928520285212852228523285242852528526285272852828529285302853128532285332853428535285362853728538285392854028541285422854328544285452854628547285482854928550285512855228553285542855528556285572855828559285602856128562285632856428565285662856728568285692857028571285722857328574285752857628577285782857928580285812858228583285842858528586285872858828589285902859128592285932859428595285962859728598285992860028601286022860328604286052860628607286082860928610286112861228613286142861528616286172861828619286202862128622286232862428625286262862728628286292863028631286322863328634286352863628637286382863928640286412864228643286442864528646286472864828649286502865128652286532865428655286562865728658286592866028661286622866328664286652866628667286682866928670286712867228673286742867528676286772867828679286802868128682286832868428685286862868728688286892869028691286922869328694286952869628697286982869928700287012870228703287042870528706287072870828709287102871128712287132871428715287162871728718287192872028721287222872328724287252872628727287282872928730287312873228733287342873528736287372873828739287402874128742287432874428745287462874728748287492875028751287522875328754287552875628757287582875928760287612876228763287642876528766287672876828769287702877128772287732877428775287762877728778287792878028781287822878328784287852878628787287882878928790287912879228793287942879528796287972879828799288002880128802288032880428805288062880728808288092881028811288122881328814288152881628817288182881928820288212882228823288242882528826288272882828829288302883128832288332883428835288362883728838288392884028841288422884328844288452884628847288482884928850288512885228853288542885528856288572885828859288602886128862288632886428865288662886728868288692887028871288722887328874288752887628877288782887928880288812888228883288842888528886288872888828889288902889128892288932889428895288962889728898288992890028901289022890328904289052890628907289082890928910289112891228913289142891528916289172891828919289202892128922289232892428925289262892728928289292893028931289322893328934289352893628937289382893928940289412894228943289442894528946289472894828949289502895128952289532895428955289562895728958289592896028961289622896328964289652896628967289682896928970289712897228973289742897528976289772897828979289802898128982289832898428985289862898728988289892899028991289922899328994289952899628997289982899929000290012900229003290042900529006290072900829009290102901129012290132901429015290162901729018290192902029021290222902329024290252902629027290282902929030290312903229033290342903529036290372903829039290402904129042290432904429045290462904729048290492905029051290522905329054290552905629057290582905929060290612906229063290642906529066290672906829069290702907129072290732907429075290762907729078290792908029081290822908329084290852908629087290882908929090290912909229093290942909529096290972909829099291002910129102291032910429105291062910729108291092911029111291122911329114291152911629117291182911929120291212912229123291242912529126291272912829129291302913129132291332913429135291362913729138291392914029141291422914329144291452914629147291482914929150291512915229153291542915529156291572915829159291602916129162291632916429165291662916729168291692917029171291722917329174291752917629177291782917929180291812918229183291842918529186291872918829189291902919129192291932919429195291962919729198291992920029201292022920329204292052920629207292082920929210292112921229213292142921529216292172921829219292202922129222292232922429225292262922729228292292923029231292322923329234292352923629237292382923929240292412924229243292442924529246292472924829249292502925129252292532925429255292562925729258292592926029261292622926329264292652926629267292682926929270292712927229273292742927529276292772927829279292802928129282292832928429285292862928729288292892929029291292922929329294292952929629297292982929929300293012930229303293042930529306293072930829309293102931129312293132931429315293162931729318293192932029321293222932329324293252932629327293282932929330293312933229333293342933529336293372933829339293402934129342293432934429345293462934729348293492935029351293522935329354293552935629357293582935929360293612936229363293642936529366293672936829369293702937129372293732937429375293762937729378293792938029381293822938329384293852938629387293882938929390293912939229393293942939529396293972939829399294002940129402294032940429405294062940729408294092941029411294122941329414294152941629417294182941929420294212942229423294242942529426294272942829429294302943129432294332943429435294362943729438294392944029441294422944329444294452944629447294482944929450294512945229453294542945529456294572945829459294602946129462294632946429465294662946729468294692947029471294722947329474294752947629477294782947929480294812948229483294842948529486294872948829489294902949129492294932949429495294962949729498294992950029501295022950329504295052950629507295082950929510295112951229513295142951529516295172951829519295202952129522295232952429525295262952729528295292953029531295322953329534295352953629537295382953929540295412954229543295442954529546295472954829549295502955129552295532955429555295562955729558295592956029561295622956329564295652956629567295682956929570295712957229573295742957529576295772957829579295802958129582295832958429585295862958729588295892959029591295922959329594295952959629597295982959929600296012960229603296042960529606296072960829609296102961129612296132961429615296162961729618296192962029621296222962329624296252962629627296282962929630296312963229633296342963529636296372963829639296402964129642296432964429645296462964729648296492965029651296522965329654296552965629657296582965929660296612966229663296642966529666296672966829669296702967129672296732967429675296762967729678296792968029681296822968329684296852968629687296882968929690296912969229693296942969529696296972969829699297002970129702297032970429705297062970729708297092971029711297122971329714297152971629717297182971929720297212972229723297242972529726297272972829729297302973129732297332973429735297362973729738297392974029741297422974329744297452974629747297482974929750297512975229753297542975529756297572975829759297602976129762297632976429765297662976729768297692977029771297722977329774297752977629777297782977929780297812978229783297842978529786297872978829789297902979129792297932979429795297962979729798297992980029801298022980329804298052980629807298082980929810298112981229813298142981529816298172981829819298202982129822298232982429825298262982729828298292983029831298322983329834298352983629837298382983929840298412984229843298442984529846298472984829849298502985129852298532985429855298562985729858298592986029861298622986329864298652986629867298682986929870298712987229873298742987529876298772987829879298802988129882298832988429885298862988729888298892989029891298922989329894298952989629897298982989929900299012990229903299042990529906299072990829909299102991129912299132991429915299162991729918299192992029921299222992329924299252992629927299282992929930299312993229933299342993529936299372993829939299402994129942299432994429945299462994729948299492995029951299522995329954299552995629957299582995929960299612996229963299642996529966299672996829969299702997129972299732997429975299762997729978299792998029981299822998329984299852998629987299882998929990299912999229993299942999529996299972999829999300003000130002300033000430005300063000730008300093001030011300123001330014300153001630017300183001930020300213002230023300243002530026300273002830029300303003130032300333003430035300363003730038300393004030041300423004330044300453004630047300483004930050300513005230053300543005530056300573005830059300603006130062300633006430065300663006730068300693007030071300723007330074300753007630077300783007930080300813008230083300843008530086300873008830089300903009130092300933009430095300963009730098300993010030101301023010330104301053010630107301083010930110301113011230113301143011530116301173011830119301203012130122301233012430125301263012730128301293013030131301323013330134301353013630137301383013930140301413014230143301443014530146301473014830149301503015130152301533015430155301563015730158301593016030161301623016330164301653016630167301683016930170301713017230173301743017530176301773017830179301803018130182301833018430185301863018730188301893019030191301923019330194301953019630197301983019930200302013020230203302043020530206302073020830209302103021130212302133021430215302163021730218302193022030221302223022330224302253022630227302283022930230302313023230233302343023530236302373023830239302403024130242302433024430245302463024730248302493025030251302523025330254302553025630257302583025930260302613026230263302643026530266302673026830269302703027130272302733027430275302763027730278302793028030281302823028330284302853028630287302883028930290302913029230293302943029530296302973029830299303003030130302303033030430305303063030730308303093031030311303123031330314303153031630317303183031930320303213032230323303243032530326303273032830329303303033130332303333033430335303363033730338303393034030341303423034330344303453034630347303483034930350303513035230353303543035530356303573035830359303603036130362303633036430365303663036730368303693037030371303723037330374303753037630377303783037930380303813038230383303843038530386303873038830389303903039130392303933039430395303963039730398303993040030401304023040330404304053040630407304083040930410304113041230413304143041530416304173041830419304203042130422304233042430425304263042730428304293043030431304323043330434304353043630437304383043930440304413044230443304443044530446304473044830449304503045130452304533045430455304563045730458304593046030461304623046330464304653046630467304683046930470304713047230473304743047530476304773047830479304803048130482304833048430485304863048730488304893049030491304923049330494304953049630497304983049930500305013050230503305043050530506305073050830509305103051130512305133051430515305163051730518305193052030521305223052330524305253052630527305283052930530305313053230533305343053530536305373053830539305403054130542305433054430545305463054730548305493055030551305523055330554305553055630557305583055930560305613056230563305643056530566305673056830569305703057130572305733057430575305763057730578305793058030581305823058330584305853058630587305883058930590305913059230593305943059530596305973059830599306003060130602306033060430605306063060730608306093061030611306123061330614306153061630617306183061930620306213062230623306243062530626306273062830629306303063130632306333063430635306363063730638306393064030641306423064330644306453064630647306483064930650306513065230653306543065530656306573065830659306603066130662306633066430665306663066730668306693067030671306723067330674306753067630677306783067930680306813068230683306843068530686306873068830689306903069130692306933069430695306963069730698306993070030701307023070330704307053070630707307083070930710307113071230713307143071530716307173071830719307203072130722307233072430725307263072730728307293073030731307323073330734307353073630737307383073930740307413074230743307443074530746307473074830749307503075130752307533075430755307563075730758307593076030761307623076330764307653076630767307683076930770307713077230773307743077530776307773077830779307803078130782307833078430785307863078730788307893079030791307923079330794307953079630797307983079930800308013080230803308043080530806308073080830809308103081130812308133081430815308163081730818308193082030821308223082330824308253082630827308283082930830308313083230833308343083530836308373083830839308403084130842308433084430845308463084730848308493085030851308523085330854308553085630857308583085930860308613086230863308643086530866308673086830869308703087130872308733087430875308763087730878308793088030881308823088330884308853088630887308883088930890308913089230893308943089530896308973089830899309003090130902309033090430905309063090730908309093091030911309123091330914309153091630917309183091930920309213092230923309243092530926309273092830929309303093130932309333093430935309363093730938309393094030941309423094330944309453094630947309483094930950309513095230953309543095530956309573095830959309603096130962309633096430965309663096730968309693097030971309723097330974309753097630977309783097930980309813098230983309843098530986309873098830989309903099130992309933099430995309963099730998309993100031001310023100331004310053100631007310083100931010310113101231013310143101531016310173101831019310203102131022310233102431025310263102731028310293103031031310323103331034310353103631037310383103931040310413104231043310443104531046310473104831049310503105131052310533105431055310563105731058310593106031061310623106331064310653106631067310683106931070310713107231073310743107531076310773107831079310803108131082310833108431085310863108731088310893109031091310923109331094310953109631097310983109931100311013110231103311043110531106311073110831109311103111131112311133111431115311163111731118311193112031121311223112331124311253112631127311283112931130311313113231133311343113531136311373113831139311403114131142311433114431145311463114731148311493115031151311523115331154311553115631157311583115931160311613116231163311643116531166311673116831169311703117131172311733117431175311763117731178311793118031181311823118331184311853118631187311883118931190311913119231193311943119531196311973119831199312003120131202312033120431205312063120731208312093121031211312123121331214312153121631217312183121931220312213122231223312243122531226312273122831229312303123131232312333123431235312363123731238312393124031241312423124331244312453124631247312483124931250312513125231253312543125531256312573125831259312603126131262312633126431265312663126731268312693127031271312723127331274312753127631277312783127931280312813128231283312843128531286312873128831289312903129131292312933129431295312963129731298312993130031301313023130331304313053130631307313083130931310313113131231313313143131531316313173131831319313203132131322313233132431325313263132731328313293133031331313323133331334313353133631337313383133931340313413134231343313443134531346313473134831349313503135131352313533135431355313563135731358313593136031361313623136331364313653136631367313683136931370313713137231373313743137531376313773137831379313803138131382313833138431385313863138731388313893139031391313923139331394313953139631397313983139931400314013140231403314043140531406314073140831409314103141131412314133141431415314163141731418314193142031421314223142331424314253142631427314283142931430314313143231433314343143531436314373143831439314403144131442314433144431445314463144731448314493145031451314523145331454314553145631457314583145931460314613146231463314643146531466314673146831469314703147131472314733147431475314763147731478314793148031481314823148331484314853148631487314883148931490314913149231493314943149531496314973149831499315003150131502315033150431505315063150731508315093151031511315123151331514315153151631517315183151931520315213152231523315243152531526315273152831529315303153131532315333153431535315363153731538315393154031541315423154331544315453154631547315483154931550315513155231553315543155531556315573155831559315603156131562315633156431565315663156731568315693157031571315723157331574315753157631577315783157931580315813158231583315843158531586315873158831589315903159131592315933159431595315963159731598315993160031601316023160331604316053160631607316083160931610316113161231613316143161531616316173161831619316203162131622316233162431625316263162731628316293163031631316323163331634316353163631637316383163931640316413164231643316443164531646316473164831649316503165131652316533165431655316563165731658316593166031661316623166331664316653166631667316683166931670316713167231673316743167531676316773167831679316803168131682316833168431685316863168731688316893169031691316923169331694316953169631697316983169931700317013170231703317043170531706317073170831709317103171131712317133171431715317163171731718317193172031721317223172331724317253172631727317283172931730317313173231733317343173531736317373173831739317403174131742317433174431745317463174731748317493175031751317523175331754317553175631757317583175931760317613176231763317643176531766317673176831769317703177131772317733177431775317763177731778317793178031781317823178331784317853178631787317883178931790317913179231793317943179531796317973179831799318003180131802318033180431805318063180731808318093181031811318123181331814318153181631817318183181931820318213182231823318243182531826318273182831829318303183131832318333183431835318363183731838318393184031841318423184331844318453184631847318483184931850318513185231853318543185531856318573185831859318603186131862318633186431865318663186731868318693187031871318723187331874318753187631877318783187931880318813188231883318843188531886318873188831889318903189131892318933189431895318963189731898318993190031901319023190331904319053190631907319083190931910319113191231913319143191531916319173191831919319203192131922319233192431925319263192731928319293193031931319323193331934319353193631937319383193931940319413194231943319443194531946319473194831949319503195131952319533195431955319563195731958319593196031961319623196331964319653196631967319683196931970319713197231973319743197531976319773197831979319803198131982319833198431985319863198731988319893199031991319923199331994319953199631997319983199932000320013200232003320043200532006320073200832009320103201132012320133201432015320163201732018320193202032021320223202332024320253202632027320283202932030320313203232033320343203532036320373203832039320403204132042320433204432045320463204732048320493205032051320523205332054320553205632057320583205932060320613206232063320643206532066320673206832069320703207132072320733207432075320763207732078320793208032081320823208332084320853208632087320883208932090320913209232093320943209532096320973209832099321003210132102321033210432105321063210732108321093211032111321123211332114321153211632117321183211932120321213212232123321243212532126321273212832129321303213132132321333213432135321363213732138321393214032141321423214332144321453214632147321483214932150321513215232153321543215532156321573215832159321603216132162321633216432165321663216732168321693217032171321723217332174321753217632177321783217932180321813218232183321843218532186321873218832189321903219132192321933219432195321963219732198321993220032201322023220332204322053220632207322083220932210322113221232213322143221532216322173221832219322203222132222322233222432225322263222732228322293223032231322323223332234322353223632237322383223932240322413224232243322443224532246322473224832249322503225132252322533225432255322563225732258322593226032261322623226332264322653226632267322683226932270322713227232273322743227532276322773227832279322803228132282322833228432285322863228732288322893229032291322923229332294322953229632297322983229932300323013230232303323043230532306323073230832309323103231132312323133231432315323163231732318323193232032321323223232332324323253232632327323283232932330323313233232333323343233532336323373233832339323403234132342323433234432345323463234732348323493235032351323523235332354323553235632357323583235932360323613236232363323643236532366323673236832369323703237132372323733237432375323763237732378323793238032381323823238332384323853238632387323883238932390323913239232393323943239532396323973239832399324003240132402324033240432405324063240732408324093241032411324123241332414324153241632417324183241932420324213242232423324243242532426324273242832429324303243132432324333243432435324363243732438324393244032441324423244332444324453244632447324483244932450324513245232453324543245532456324573245832459324603246132462324633246432465324663246732468324693247032471324723247332474324753247632477324783247932480324813248232483324843248532486324873248832489324903249132492324933249432495324963249732498324993250032501325023250332504325053250632507325083250932510325113251232513325143251532516325173251832519325203252132522325233252432525325263252732528325293253032531325323253332534325353253632537325383253932540325413254232543325443254532546325473254832549325503255132552325533255432555325563255732558325593256032561325623256332564325653256632567325683256932570325713257232573325743257532576325773257832579325803258132582325833258432585325863258732588325893259032591325923259332594325953259632597325983259932600326013260232603326043260532606326073260832609326103261132612326133261432615326163261732618326193262032621326223262332624326253262632627326283262932630326313263232633326343263532636326373263832639326403264132642326433264432645326463264732648326493265032651326523265332654326553265632657326583265932660326613266232663326643266532666326673266832669326703267132672326733267432675326763267732678326793268032681326823268332684326853268632687326883268932690326913269232693326943269532696326973269832699327003270132702327033270432705327063270732708327093271032711327123271332714327153271632717327183271932720327213272232723327243272532726327273272832729327303273132732327333273432735327363273732738327393274032741327423274332744327453274632747327483274932750327513275232753327543275532756327573275832759327603276132762327633276432765327663276732768327693277032771327723277332774327753277632777327783277932780327813278232783327843278532786327873278832789327903279132792327933279432795327963279732798327993280032801328023280332804328053280632807328083280932810328113281232813328143281532816328173281832819328203282132822328233282432825328263282732828328293283032831328323283332834328353283632837328383283932840328413284232843328443284532846328473284832849328503285132852328533285432855328563285732858328593286032861328623286332864328653286632867328683286932870328713287232873328743287532876328773287832879328803288132882328833288432885328863288732888328893289032891328923289332894328953289632897328983289932900329013290232903329043290532906329073290832909329103291132912329133291432915329163291732918329193292032921329223292332924329253292632927329283292932930329313293232933329343293532936329373293832939329403294132942329433294432945329463294732948329493295032951329523295332954329553295632957329583295932960329613296232963329643296532966329673296832969329703297132972329733297432975329763297732978329793298032981329823298332984329853298632987329883298932990329913299232993329943299532996329973299832999330003300133002330033300433005330063300733008330093301033011330123301333014330153301633017330183301933020330213302233023330243302533026330273302833029330303303133032330333303433035330363303733038330393304033041330423304333044330453304633047330483304933050330513305233053330543305533056330573305833059330603306133062330633306433065330663306733068330693307033071330723307333074330753307633077330783307933080330813308233083330843308533086330873308833089330903309133092330933309433095330963309733098330993310033101331023310333104331053310633107331083310933110331113311233113331143311533116331173311833119331203312133122331233312433125331263312733128331293313033131331323313333134331353313633137331383313933140331413314233143331443314533146331473314833149331503315133152331533315433155331563315733158331593316033161331623316333164331653316633167331683316933170331713317233173331743317533176331773317833179331803318133182331833318433185331863318733188331893319033191331923319333194331953319633197331983319933200332013320233203332043320533206332073320833209332103321133212332133321433215332163321733218332193322033221332223322333224332253322633227332283322933230332313323233233332343323533236332373323833239332403324133242332433324433245332463324733248332493325033251332523325333254332553325633257332583325933260332613326233263332643326533266332673326833269332703327133272332733327433275332763327733278332793328033281332823328333284332853328633287332883328933290332913329233293332943329533296332973329833299333003330133302333033330433305333063330733308333093331033311333123331333314333153331633317333183331933320333213332233323333243332533326333273332833329333303333133332333333333433335333363333733338333393334033341333423334333344333453334633347333483334933350333513335233353333543335533356333573335833359333603336133362333633336433365333663336733368333693337033371333723337333374333753337633377333783337933380333813338233383333843338533386333873338833389333903339133392333933339433395333963339733398333993340033401334023340333404334053340633407334083340933410334113341233413334143341533416334173341833419334203342133422334233342433425334263342733428334293343033431334323343333434334353343633437334383343933440334413344233443334443344533446334473344833449334503345133452334533345433455334563345733458334593346033461334623346333464334653346633467334683346933470334713347233473334743347533476334773347833479334803348133482334833348433485334863348733488334893349033491334923349333494334953349633497334983349933500335013350233503335043350533506335073350833509335103351133512335133351433515335163351733518335193352033521335223352333524335253352633527335283352933530335313353233533335343353533536335373353833539335403354133542335433354433545335463354733548335493355033551335523355333554335553355633557335583355933560335613356233563335643356533566335673356833569335703357133572335733357433575335763357733578335793358033581335823358333584335853358633587335883358933590335913359233593335943359533596335973359833599336003360133602336033360433605336063360733608336093361033611336123361333614336153361633617336183361933620336213362233623336243362533626336273362833629336303363133632336333363433635336363363733638336393364033641336423364333644336453364633647336483364933650336513365233653336543365533656336573365833659336603366133662336633366433665336663366733668336693367033671336723367333674336753367633677336783367933680336813368233683336843368533686336873368833689336903369133692336933369433695336963369733698336993370033701337023370333704337053370633707337083370933710337113371233713337143371533716337173371833719337203372133722337233372433725337263372733728337293373033731337323373333734337353373633737337383373933740337413374233743337443374533746337473374833749337503375133752337533375433755337563375733758337593376033761337623376333764337653376633767337683376933770337713377233773337743377533776337773377833779337803378133782337833378433785337863378733788337893379033791337923379333794337953379633797337983379933800338013380233803338043380533806338073380833809338103381133812338133381433815338163381733818338193382033821338223382333824338253382633827338283382933830338313383233833338343383533836338373383833839338403384133842338433384433845338463384733848338493385033851338523385333854338553385633857338583385933860338613386233863338643386533866338673386833869338703387133872338733387433875338763387733878338793388033881338823388333884338853388633887338883388933890338913389233893338943389533896338973389833899339003390133902339033390433905339063390733908339093391033911339123391333914339153391633917339183391933920339213392233923339243392533926339273392833929339303393133932339333393433935339363393733938339393394033941339423394333944339453394633947339483394933950339513395233953339543395533956339573395833959339603396133962339633396433965339663396733968339693397033971339723397333974339753397633977339783397933980339813398233983339843398533986339873398833989339903399133992339933399433995339963399733998339993400034001340023400334004340053400634007340083400934010340113401234013340143401534016340173401834019340203402134022340233402434025340263402734028340293403034031340323403334034340353403634037340383403934040340413404234043340443404534046340473404834049340503405134052340533405434055340563405734058340593406034061340623406334064340653406634067340683406934070340713407234073340743407534076340773407834079340803408134082340833408434085340863408734088340893409034091340923409334094340953409634097340983409934100341013410234103341043410534106341073410834109341103411134112341133411434115341163411734118341193412034121341223412334124341253412634127341283412934130341313413234133341343413534136341373413834139341403414134142341433414434145341463414734148341493415034151341523415334154341553415634157341583415934160341613416234163341643416534166341673416834169341703417134172341733417434175341763417734178341793418034181341823418334184341853418634187341883418934190341913419234193341943419534196341973419834199342003420134202342033420434205342063420734208342093421034211342123421334214342153421634217342183421934220342213422234223342243422534226342273422834229342303423134232342333423434235342363423734238342393424034241342423424334244342453424634247342483424934250342513425234253342543425534256342573425834259342603426134262342633426434265342663426734268342693427034271342723427334274342753427634277342783427934280342813428234283342843428534286342873428834289342903429134292342933429434295342963429734298342993430034301343023430334304343053430634307343083430934310343113431234313343143431534316343173431834319343203432134322343233432434325343263432734328343293433034331343323433334334343353433634337343383433934340343413434234343343443434534346343473434834349343503435134352343533435434355343563435734358343593436034361343623436334364343653436634367343683436934370343713437234373343743437534376343773437834379343803438134382343833438434385343863438734388343893439034391343923439334394343953439634397343983439934400344013440234403344043440534406344073440834409344103441134412344133441434415344163441734418344193442034421344223442334424344253442634427344283442934430344313443234433344343443534436344373443834439344403444134442344433444434445344463444734448344493445034451344523445334454344553445634457344583445934460344613446234463344643446534466344673446834469344703447134472344733447434475344763447734478344793448034481344823448334484344853448634487344883448934490344913449234493344943449534496344973449834499345003450134502345033450434505345063450734508345093451034511345123451334514345153451634517345183451934520345213452234523345243452534526345273452834529345303453134532345333453434535345363453734538345393454034541345423454334544345453454634547345483454934550345513455234553345543455534556345573455834559345603456134562345633456434565345663456734568345693457034571345723457334574345753457634577345783457934580345813458234583345843458534586345873458834589345903459134592345933459434595345963459734598345993460034601346023460334604346053460634607346083460934610346113461234613346143461534616346173461834619346203462134622346233462434625346263462734628346293463034631346323463334634346353463634637346383463934640346413464234643346443464534646346473464834649346503465134652346533465434655346563465734658346593466034661346623466334664346653466634667346683466934670346713467234673346743467534676346773467834679346803468134682346833468434685346863468734688346893469034691346923469334694346953469634697346983469934700347013470234703347043470534706347073470834709347103471134712347133471434715347163471734718347193472034721347223472334724347253472634727347283472934730347313473234733347343473534736347373473834739347403474134742347433474434745347463474734748347493475034751347523475334754347553475634757347583475934760347613476234763347643476534766347673476834769347703477134772347733477434775347763477734778347793478034781347823478334784347853478634787347883478934790347913479234793347943479534796347973479834799348003480134802348033480434805348063480734808348093481034811348123481334814348153481634817348183481934820348213482234823348243482534826348273482834829348303483134832348333483434835348363483734838348393484034841348423484334844348453484634847348483484934850348513485234853348543485534856348573485834859348603486134862348633486434865348663486734868348693487034871348723487334874348753487634877348783487934880348813488234883348843488534886348873488834889348903489134892348933489434895348963489734898348993490034901349023490334904349053490634907349083490934910349113491234913349143491534916349173491834919349203492134922349233492434925349263492734928349293493034931349323493334934349353493634937349383493934940349413494234943349443494534946349473494834949349503495134952349533495434955349563495734958349593496034961349623496334964349653496634967349683496934970349713497234973349743497534976349773497834979349803498134982349833498434985349863498734988349893499034991349923499334994349953499634997349983499935000350013500235003350043500535006350073500835009350103501135012350133501435015350163501735018350193502035021350223502335024350253502635027350283502935030350313503235033350343503535036350373503835039350403504135042350433504435045350463504735048350493505035051350523505335054350553505635057350583505935060350613506235063350643506535066350673506835069350703507135072350733507435075350763507735078350793508035081350823508335084350853508635087350883508935090350913509235093350943509535096350973509835099351003510135102351033510435105351063510735108351093511035111351123511335114351153511635117351183511935120351213512235123351243512535126351273512835129351303513135132351333513435135351363513735138351393514035141351423514335144351453514635147351483514935150351513515235153351543515535156351573515835159351603516135162351633516435165351663516735168351693517035171351723517335174351753517635177351783517935180351813518235183351843518535186351873518835189351903519135192351933519435195351963519735198351993520035201352023520335204352053520635207352083520935210352113521235213352143521535216352173521835219352203522135222352233522435225352263522735228352293523035231352323523335234352353523635237352383523935240352413524235243352443524535246352473524835249352503525135252352533525435255352563525735258352593526035261352623526335264352653526635267352683526935270352713527235273352743527535276352773527835279352803528135282352833528435285352863528735288352893529035291352923529335294352953529635297352983529935300353013530235303353043530535306353073530835309353103531135312353133531435315353163531735318353193532035321353223532335324353253532635327353283532935330353313533235333353343533535336353373533835339353403534135342353433534435345353463534735348353493535035351353523535335354353553535635357353583535935360353613536235363353643536535366353673536835369353703537135372353733537435375353763537735378353793538035381353823538335384353853538635387353883538935390353913539235393353943539535396353973539835399354003540135402354033540435405354063540735408354093541035411354123541335414354153541635417354183541935420354213542235423354243542535426354273542835429354303543135432354333543435435354363543735438354393544035441354423544335444354453544635447354483544935450354513545235453354543545535456354573545835459354603546135462354633546435465354663546735468354693547035471354723547335474354753547635477354783547935480354813548235483354843548535486354873548835489354903549135492354933549435495354963549735498354993550035501355023550335504355053550635507355083550935510355113551235513355143551535516355173551835519355203552135522355233552435525355263552735528355293553035531355323553335534355353553635537355383553935540355413554235543355443554535546355473554835549355503555135552355533555435555355563555735558355593556035561355623556335564355653556635567355683556935570355713557235573355743557535576355773557835579355803558135582355833558435585355863558735588355893559035591355923559335594355953559635597355983559935600356013560235603356043560535606356073560835609356103561135612356133561435615356163561735618356193562035621356223562335624356253562635627356283562935630356313563235633356343563535636356373563835639356403564135642356433564435645356463564735648356493565035651356523565335654356553565635657356583565935660356613566235663356643566535666356673566835669356703567135672356733567435675356763567735678356793568035681356823568335684356853568635687356883568935690356913569235693356943569535696356973569835699357003570135702357033570435705357063570735708357093571035711357123571335714357153571635717357183571935720357213572235723357243572535726357273572835729357303573135732357333573435735357363573735738357393574035741357423574335744357453574635747357483574935750357513575235753357543575535756357573575835759357603576135762357633576435765357663576735768357693577035771357723577335774357753577635777357783577935780357813578235783357843578535786357873578835789357903579135792357933579435795357963579735798357993580035801358023580335804358053580635807358083580935810358113581235813358143581535816358173581835819358203582135822358233582435825358263582735828358293583035831358323583335834358353583635837358383583935840358413584235843358443584535846358473584835849358503585135852358533585435855358563585735858358593586035861358623586335864358653586635867358683586935870358713587235873358743587535876358773587835879358803588135882358833588435885358863588735888358893589035891358923589335894358953589635897358983589935900359013590235903359043590535906359073590835909359103591135912359133591435915359163591735918359193592035921359223592335924359253592635927359283592935930359313593235933359343593535936359373593835939359403594135942359433594435945359463594735948359493595035951359523595335954359553595635957359583595935960359613596235963359643596535966359673596835969359703597135972359733597435975359763597735978359793598035981359823598335984359853598635987359883598935990359913599235993359943599535996359973599835999360003600136002360033600436005360063600736008360093601036011360123601336014360153601636017360183601936020360213602236023360243602536026360273602836029360303603136032360333603436035360363603736038360393604036041360423604336044360453604636047360483604936050360513605236053360543605536056360573605836059360603606136062360633606436065360663606736068360693607036071360723607336074360753607636077360783607936080360813608236083360843608536086360873608836089360903609136092360933609436095360963609736098360993610036101361023610336104361053610636107361083610936110361113611236113361143611536116361173611836119361203612136122361233612436125361263612736128361293613036131361323613336134361353613636137361383613936140361413614236143361443614536146361473614836149361503615136152361533615436155361563615736158361593616036161361623616336164361653616636167361683616936170361713617236173361743617536176361773617836179361803618136182361833618436185361863618736188361893619036191361923619336194361953619636197361983619936200362013620236203362043620536206362073620836209362103621136212362133621436215362163621736218362193622036221362223622336224362253622636227362283622936230362313623236233362343623536236362373623836239362403624136242362433624436245362463624736248362493625036251362523625336254362553625636257362583625936260362613626236263362643626536266362673626836269362703627136272362733627436275362763627736278362793628036281362823628336284362853628636287362883628936290362913629236293362943629536296362973629836299363003630136302363033630436305363063630736308363093631036311363123631336314363153631636317363183631936320363213632236323363243632536326363273632836329363303633136332363333633436335363363633736338363393634036341363423634336344363453634636347363483634936350363513635236353363543635536356363573635836359363603636136362363633636436365363663636736368363693637036371363723637336374363753637636377363783637936380363813638236383363843638536386363873638836389363903639136392363933639436395363963639736398363993640036401364023640336404364053640636407364083640936410364113641236413364143641536416364173641836419364203642136422364233642436425364263642736428364293643036431364323643336434364353643636437364383643936440364413644236443364443644536446364473644836449364503645136452364533645436455364563645736458364593646036461364623646336464364653646636467364683646936470364713647236473364743647536476364773647836479364803648136482364833648436485364863648736488364893649036491364923649336494364953649636497364983649936500365013650236503365043650536506365073650836509365103651136512365133651436515365163651736518365193652036521365223652336524365253652636527365283652936530365313653236533365343653536536365373653836539365403654136542365433654436545365463654736548365493655036551365523655336554365553655636557365583655936560365613656236563365643656536566365673656836569365703657136572365733657436575365763657736578365793658036581365823658336584365853658636587365883658936590365913659236593365943659536596365973659836599366003660136602366033660436605366063660736608366093661036611366123661336614366153661636617366183661936620366213662236623366243662536626366273662836629366303663136632366333663436635366363663736638366393664036641366423664336644366453664636647366483664936650366513665236653366543665536656366573665836659366603666136662366633666436665366663666736668366693667036671366723667336674366753667636677366783667936680366813668236683366843668536686366873668836689366903669136692366933669436695366963669736698366993670036701367023670336704367053670636707367083670936710367113671236713367143671536716367173671836719367203672136722367233672436725367263672736728367293673036731367323673336734367353673636737367383673936740367413674236743367443674536746367473674836749367503675136752367533675436755367563675736758367593676036761367623676336764367653676636767367683676936770367713677236773367743677536776367773677836779367803678136782367833678436785367863678736788367893679036791367923679336794367953679636797367983679936800368013680236803368043680536806368073680836809368103681136812368133681436815368163681736818368193682036821368223682336824368253682636827368283682936830368313683236833368343683536836368373683836839368403684136842368433684436845368463684736848368493685036851368523685336854368553685636857368583685936860368613686236863368643686536866368673686836869368703687136872368733687436875368763687736878368793688036881368823688336884368853688636887368883688936890368913689236893368943689536896368973689836899369003690136902369033690436905369063690736908369093691036911369123691336914369153691636917369183691936920369213692236923369243692536926369273692836929369303693136932369333693436935369363693736938369393694036941369423694336944369453694636947369483694936950369513695236953369543695536956369573695836959369603696136962369633696436965369663696736968369693697036971369723697336974369753697636977369783697936980369813698236983369843698536986369873698836989369903699136992369933699436995369963699736998369993700037001370023700337004370053700637007370083700937010370113701237013370143701537016370173701837019370203702137022370233702437025370263702737028370293703037031370323703337034370353703637037370383703937040370413704237043370443704537046370473704837049370503705137052370533705437055370563705737058370593706037061370623706337064370653706637067370683706937070370713707237073370743707537076370773707837079370803708137082370833708437085370863708737088370893709037091370923709337094370953709637097370983709937100371013710237103371043710537106371073710837109371103711137112371133711437115371163711737118371193712037121371223712337124371253712637127371283712937130371313713237133371343713537136371373713837139371403714137142371433714437145371463714737148371493715037151371523715337154371553715637157371583715937160371613716237163371643716537166371673716837169371703717137172371733717437175371763717737178371793718037181371823718337184371853718637187371883718937190371913719237193371943719537196371973719837199372003720137202372033720437205372063720737208372093721037211372123721337214372153721637217372183721937220372213722237223372243722537226372273722837229372303723137232372333723437235372363723737238372393724037241372423724337244372453724637247372483724937250372513725237253372543725537256372573725837259372603726137262372633726437265372663726737268372693727037271372723727337274372753727637277372783727937280372813728237283372843728537286372873728837289372903729137292372933729437295372963729737298372993730037301373023730337304373053730637307373083730937310373113731237313373143731537316373173731837319373203732137322373233732437325373263732737328373293733037331373323733337334373353733637337373383733937340373413734237343373443734537346373473734837349373503735137352373533735437355373563735737358373593736037361373623736337364373653736637367373683736937370373713737237373373743737537376373773737837379373803738137382373833738437385373863738737388373893739037391373923739337394373953739637397373983739937400374013740237403374043740537406374073740837409374103741137412374133741437415374163741737418374193742037421374223742337424374253742637427374283742937430374313743237433374343743537436374373743837439374403744137442374433744437445374463744737448374493745037451374523745337454374553745637457374583745937460374613746237463374643746537466374673746837469374703747137472374733747437475374763747737478374793748037481374823748337484374853748637487374883748937490374913749237493374943749537496374973749837499375003750137502375033750437505375063750737508375093751037511375123751337514375153751637517375183751937520375213752237523375243752537526375273752837529375303753137532375333753437535375363753737538375393754037541375423754337544375453754637547375483754937550375513755237553375543755537556375573755837559375603756137562375633756437565375663756737568375693757037571375723757337574375753757637577375783757937580375813758237583375843758537586375873758837589375903759137592375933759437595375963759737598375993760037601376023760337604376053760637607376083760937610376113761237613376143761537616376173761837619376203762137622376233762437625376263762737628376293763037631376323763337634376353763637637376383763937640376413764237643376443764537646376473764837649376503765137652376533765437655376563765737658376593766037661376623766337664376653766637667376683766937670376713767237673376743767537676376773767837679376803768137682376833768437685376863768737688376893769037691376923769337694376953769637697376983769937700377013770237703377043770537706377073770837709377103771137712377133771437715377163771737718377193772037721377223772337724377253772637727377283772937730377313773237733377343773537736377373773837739377403774137742377433774437745377463774737748377493775037751377523775337754377553775637757377583775937760377613776237763377643776537766377673776837769377703777137772377733777437775377763777737778377793778037781377823778337784377853778637787377883778937790377913779237793377943779537796377973779837799378003780137802378033780437805378063780737808378093781037811378123781337814378153781637817378183781937820378213782237823378243782537826378273782837829378303783137832378333783437835378363783737838378393784037841378423784337844378453784637847378483784937850378513785237853378543785537856378573785837859378603786137862378633786437865378663786737868378693787037871378723787337874378753787637877378783787937880378813788237883378843788537886378873788837889378903789137892378933789437895378963789737898378993790037901379023790337904379053790637907379083790937910379113791237913379143791537916379173791837919379203792137922379233792437925379263792737928379293793037931379323793337934379353793637937379383793937940379413794237943379443794537946379473794837949379503795137952379533795437955379563795737958379593796037961379623796337964379653796637967379683796937970379713797237973379743797537976379773797837979379803798137982379833798437985379863798737988379893799037991379923799337994379953799637997379983799938000380013800238003380043800538006380073800838009380103801138012380133801438015380163801738018380193802038021380223802338024380253802638027380283802938030380313803238033380343803538036380373803838039380403804138042380433804438045380463804738048380493805038051380523805338054380553805638057380583805938060380613806238063380643806538066380673806838069380703807138072380733807438075380763807738078380793808038081380823808338084380853808638087380883808938090380913809238093380943809538096380973809838099381003810138102381033810438105381063810738108381093811038111381123811338114381153811638117381183811938120381213812238123381243812538126381273812838129381303813138132381333813438135381363813738138381393814038141381423814338144381453814638147381483814938150381513815238153381543815538156381573815838159381603816138162381633816438165381663816738168381693817038171381723817338174381753817638177381783817938180381813818238183381843818538186381873818838189381903819138192381933819438195381963819738198381993820038201382023820338204382053820638207382083820938210382113821238213382143821538216382173821838219382203822138222382233822438225382263822738228382293823038231382323823338234382353823638237382383823938240382413824238243382443824538246382473824838249382503825138252382533825438255382563825738258382593826038261382623826338264382653826638267382683826938270382713827238273382743827538276382773827838279382803828138282382833828438285382863828738288382893829038291382923829338294382953829638297382983829938300383013830238303383043830538306383073830838309383103831138312383133831438315383163831738318383193832038321383223832338324383253832638327383283832938330383313833238333383343833538336383373833838339383403834138342383433834438345383463834738348383493835038351383523835338354383553835638357383583835938360383613836238363383643836538366383673836838369383703837138372383733837438375383763837738378383793838038381383823838338384383853838638387383883838938390383913839238393383943839538396383973839838399384003840138402384033840438405384063840738408384093841038411384123841338414384153841638417384183841938420384213842238423384243842538426384273842838429384303843138432384333843438435384363843738438384393844038441384423844338444384453844638447384483844938450384513845238453384543845538456384573845838459384603846138462384633846438465384663846738468384693847038471384723847338474384753847638477384783847938480384813848238483384843848538486384873848838489384903849138492384933849438495384963849738498384993850038501385023850338504385053850638507385083850938510385113851238513385143851538516385173851838519385203852138522385233852438525385263852738528385293853038531385323853338534385353853638537385383853938540385413854238543385443854538546385473854838549385503855138552385533855438555385563855738558385593856038561385623856338564385653856638567385683856938570385713857238573385743857538576385773857838579385803858138582385833858438585385863858738588385893859038591385923859338594385953859638597385983859938600386013860238603386043860538606386073860838609386103861138612386133861438615386163861738618386193862038621386223862338624386253862638627386283862938630386313863238633386343863538636386373863838639386403864138642386433864438645386463864738648386493865038651386523865338654386553865638657386583865938660386613866238663386643866538666386673866838669386703867138672386733867438675386763867738678386793868038681386823868338684386853868638687386883868938690386913869238693386943869538696386973869838699387003870138702387033870438705387063870738708387093871038711387123871338714387153871638717387183871938720387213872238723387243872538726387273872838729387303873138732387333873438735387363873738738387393874038741387423874338744387453874638747387483874938750387513875238753387543875538756387573875838759387603876138762387633876438765387663876738768387693877038771387723877338774387753877638777387783877938780387813878238783387843878538786387873878838789387903879138792387933879438795387963879738798387993880038801388023880338804388053880638807388083880938810388113881238813388143881538816388173881838819388203882138822388233882438825388263882738828388293883038831388323883338834388353883638837388383883938840388413884238843388443884538846388473884838849388503885138852388533885438855388563885738858388593886038861388623886338864388653886638867388683886938870388713887238873388743887538876388773887838879388803888138882388833888438885388863888738888388893889038891388923889338894388953889638897388983889938900389013890238903389043890538906389073890838909389103891138912389133891438915389163891738918389193892038921389223892338924389253892638927389283892938930389313893238933389343893538936389373893838939389403894138942389433894438945389463894738948389493895038951389523895338954389553895638957389583895938960389613896238963389643896538966389673896838969389703897138972389733897438975389763897738978389793898038981389823898338984389853898638987389883898938990389913899238993389943899538996389973899838999390003900139002390033900439005390063900739008390093901039011390123901339014390153901639017390183901939020390213902239023390243902539026390273902839029390303903139032390333903439035390363903739038390393904039041390423904339044390453904639047390483904939050390513905239053390543905539056390573905839059390603906139062390633906439065390663906739068390693907039071390723907339074390753907639077390783907939080390813908239083390843908539086390873908839089390903909139092390933909439095390963909739098390993910039101391023910339104391053910639107391083910939110391113911239113391143911539116391173911839119391203912139122391233912439125391263912739128391293913039131391323913339134391353913639137391383913939140391413914239143391443914539146391473914839149391503915139152391533915439155391563915739158391593916039161391623916339164391653916639167391683916939170391713917239173391743917539176391773917839179391803918139182391833918439185391863918739188391893919039191391923919339194391953919639197391983919939200392013920239203392043920539206392073920839209392103921139212392133921439215392163921739218392193922039221392223922339224392253922639227392283922939230392313923239233392343923539236392373923839239392403924139242392433924439245392463924739248392493925039251392523925339254392553925639257392583925939260392613926239263392643926539266392673926839269392703927139272392733927439275392763927739278392793928039281392823928339284392853928639287392883928939290392913929239293392943929539296392973929839299393003930139302393033930439305393063930739308393093931039311393123931339314393153931639317393183931939320393213932239323393243932539326393273932839329393303933139332393333933439335393363933739338393393934039341393423934339344393453934639347393483934939350393513935239353393543935539356393573935839359393603936139362393633936439365393663936739368393693937039371393723937339374393753937639377393783937939380393813938239383393843938539386393873938839389393903939139392393933939439395393963939739398393993940039401394023940339404394053940639407394083940939410394113941239413394143941539416394173941839419394203942139422394233942439425394263942739428394293943039431394323943339434394353943639437394383943939440394413944239443394443944539446394473944839449394503945139452394533945439455394563945739458394593946039461394623946339464394653946639467394683946939470394713947239473394743947539476394773947839479394803948139482394833948439485394863948739488394893949039491394923949339494394953949639497394983949939500395013950239503395043950539506395073950839509395103951139512395133951439515395163951739518395193952039521395223952339524395253952639527395283952939530395313953239533395343953539536395373953839539395403954139542395433954439545395463954739548395493955039551395523955339554395553955639557395583955939560395613956239563395643956539566395673956839569395703957139572395733957439575395763957739578395793958039581395823958339584395853958639587395883958939590395913959239593395943959539596395973959839599396003960139602396033960439605396063960739608396093961039611396123961339614396153961639617396183961939620396213962239623396243962539626396273962839629396303963139632396333963439635396363963739638396393964039641396423964339644396453964639647396483964939650396513965239653396543965539656396573965839659396603966139662396633966439665396663966739668396693967039671396723967339674396753967639677396783967939680396813968239683396843968539686396873968839689396903969139692396933969439695396963969739698396993970039701397023970339704397053970639707397083970939710397113971239713397143971539716397173971839719397203972139722397233972439725397263972739728397293973039731397323973339734397353973639737397383973939740397413974239743397443974539746397473974839749397503975139752397533975439755397563975739758397593976039761397623976339764397653976639767397683976939770397713977239773397743977539776397773977839779397803978139782397833978439785397863978739788397893979039791397923979339794397953979639797397983979939800398013980239803398043980539806398073980839809398103981139812398133981439815398163981739818398193982039821398223982339824398253982639827398283982939830398313983239833398343983539836398373983839839398403984139842398433984439845398463984739848398493985039851398523985339854398553985639857398583985939860398613986239863398643986539866398673986839869398703987139872398733987439875398763987739878398793988039881398823988339884398853988639887398883988939890398913989239893398943989539896398973989839899399003990139902399033990439905399063990739908399093991039911399123991339914399153991639917399183991939920399213992239923399243992539926399273992839929399303993139932399333993439935399363993739938399393994039941399423994339944399453994639947399483994939950399513995239953399543995539956399573995839959399603996139962399633996439965399663996739968399693997039971399723997339974399753997639977399783997939980399813998239983399843998539986399873998839989399903999139992399933999439995399963999739998399994000040001400024000340004400054000640007400084000940010400114001240013400144001540016400174001840019400204002140022400234002440025400264002740028400294003040031400324003340034400354003640037400384003940040400414004240043400444004540046400474004840049400504005140052400534005440055400564005740058400594006040061400624006340064400654006640067400684006940070400714007240073400744007540076400774007840079400804008140082400834008440085400864008740088400894009040091400924009340094400954009640097400984009940100401014010240103401044010540106401074010840109401104011140112401134011440115401164011740118401194012040121401224012340124401254012640127401284012940130401314013240133401344013540136401374013840139401404014140142401434014440145401464014740148401494015040151401524015340154401554015640157401584015940160401614016240163401644016540166401674016840169401704017140172401734017440175401764017740178401794018040181401824018340184401854018640187401884018940190401914019240193401944019540196401974019840199402004020140202402034020440205402064020740208402094021040211402124021340214402154021640217402184021940220402214022240223402244022540226402274022840229402304023140232402334023440235402364023740238402394024040241402424024340244402454024640247402484024940250402514025240253402544025540256402574025840259402604026140262402634026440265402664026740268402694027040271402724027340274402754027640277402784027940280402814028240283402844028540286402874028840289402904029140292402934029440295402964029740298402994030040301403024030340304403054030640307403084030940310403114031240313403144031540316403174031840319403204032140322403234032440325403264032740328403294033040331403324033340334403354033640337403384033940340403414034240343403444034540346403474034840349403504035140352403534035440355403564035740358403594036040361403624036340364403654036640367403684036940370403714037240373403744037540376403774037840379403804038140382403834038440385403864038740388403894039040391403924039340394403954039640397403984039940400404014040240403404044040540406404074040840409404104041140412404134041440415404164041740418404194042040421404224042340424404254042640427404284042940430404314043240433404344043540436404374043840439404404044140442404434044440445404464044740448404494045040451404524045340454404554045640457404584045940460404614046240463404644046540466404674046840469404704047140472404734047440475404764047740478404794048040481404824048340484404854048640487404884048940490404914049240493404944049540496404974049840499405004050140502405034050440505405064050740508405094051040511405124051340514405154051640517405184051940520405214052240523405244052540526405274052840529405304053140532405334053440535405364053740538405394054040541405424054340544405454054640547405484054940550405514055240553405544055540556405574055840559405604056140562405634056440565405664056740568405694057040571405724057340574405754057640577405784057940580405814058240583405844058540586405874058840589405904059140592405934059440595405964059740598405994060040601406024060340604406054060640607406084060940610406114061240613406144061540616406174061840619406204062140622406234062440625406264062740628406294063040631406324063340634406354063640637406384063940640406414064240643406444064540646406474064840649406504065140652406534065440655406564065740658406594066040661406624066340664406654066640667406684066940670406714067240673406744067540676406774067840679406804068140682406834068440685406864068740688406894069040691406924069340694406954069640697406984069940700407014070240703407044070540706407074070840709407104071140712407134071440715407164071740718407194072040721407224072340724407254072640727407284072940730407314073240733407344073540736407374073840739407404074140742407434074440745407464074740748407494075040751407524075340754407554075640757407584075940760407614076240763407644076540766407674076840769407704077140772407734077440775407764077740778407794078040781407824078340784407854078640787407884078940790407914079240793407944079540796407974079840799408004080140802408034080440805408064080740808408094081040811408124081340814408154081640817408184081940820408214082240823408244082540826408274082840829408304083140832408334083440835408364083740838408394084040841408424084340844408454084640847408484084940850408514085240853408544085540856408574085840859408604086140862408634086440865408664086740868408694087040871408724087340874408754087640877408784087940880408814088240883408844088540886408874088840889408904089140892408934089440895408964089740898408994090040901409024090340904409054090640907409084090940910409114091240913409144091540916409174091840919409204092140922409234092440925409264092740928409294093040931409324093340934409354093640937409384093940940409414094240943409444094540946409474094840949409504095140952409534095440955409564095740958409594096040961409624096340964409654096640967409684096940970409714097240973409744097540976409774097840979409804098140982409834098440985409864098740988409894099040991409924099340994409954099640997409984099941000410014100241003410044100541006410074100841009410104101141012410134101441015410164101741018410194102041021410224102341024410254102641027410284102941030410314103241033410344103541036410374103841039410404104141042410434104441045410464104741048410494105041051410524105341054410554105641057410584105941060410614106241063410644106541066410674106841069410704107141072410734107441075410764107741078410794108041081410824108341084410854108641087410884108941090410914109241093410944109541096410974109841099411004110141102411034110441105411064110741108411094111041111411124111341114411154111641117411184111941120411214112241123411244112541126411274112841129411304113141132411334113441135411364113741138411394114041141411424114341144411454114641147411484114941150411514115241153411544115541156411574115841159411604116141162411634116441165411664116741168411694117041171411724117341174411754117641177411784117941180411814118241183411844118541186411874118841189411904119141192411934119441195411964119741198411994120041201412024120341204412054120641207412084120941210412114121241213412144121541216412174121841219412204122141222412234122441225412264122741228412294123041231412324123341234412354123641237412384123941240412414124241243412444124541246412474124841249412504125141252412534125441255412564125741258412594126041261412624126341264412654126641267412684126941270412714127241273412744127541276412774127841279412804128141282412834128441285412864128741288412894129041291412924129341294412954129641297412984129941300413014130241303413044130541306413074130841309413104131141312413134131441315413164131741318413194132041321413224132341324413254132641327413284132941330413314133241333413344133541336413374133841339413404134141342413434134441345413464134741348413494135041351413524135341354413554135641357413584135941360413614136241363413644136541366413674136841369413704137141372413734137441375413764137741378413794138041381413824138341384413854138641387413884138941390413914139241393413944139541396413974139841399414004140141402414034140441405414064140741408414094141041411414124141341414414154141641417414184141941420414214142241423414244142541426414274142841429414304143141432414334143441435414364143741438414394144041441414424144341444414454144641447414484144941450414514145241453414544145541456414574145841459414604146141462414634146441465414664146741468414694147041471414724147341474414754147641477414784147941480414814148241483414844148541486414874148841489414904149141492414934149441495414964149741498414994150041501415024150341504415054150641507415084150941510415114151241513415144151541516415174151841519415204152141522415234152441525415264152741528415294153041531415324153341534415354153641537415384153941540415414154241543415444154541546415474154841549415504155141552415534155441555415564155741558415594156041561415624156341564415654156641567415684156941570415714157241573415744157541576415774157841579415804158141582415834158441585415864158741588415894159041591415924159341594415954159641597415984159941600416014160241603416044160541606416074160841609416104161141612416134161441615416164161741618416194162041621416224162341624416254162641627416284162941630416314163241633416344163541636416374163841639416404164141642416434164441645416464164741648416494165041651416524165341654416554165641657416584165941660416614166241663416644166541666416674166841669416704167141672416734167441675416764167741678416794168041681416824168341684416854168641687416884168941690416914169241693416944169541696416974169841699417004170141702417034170441705417064170741708417094171041711417124171341714417154171641717417184171941720417214172241723417244172541726417274172841729417304173141732417334173441735417364173741738417394174041741417424174341744417454174641747417484174941750417514175241753417544175541756417574175841759417604176141762417634176441765417664176741768417694177041771417724177341774417754177641777417784177941780417814178241783417844178541786417874178841789417904179141792417934179441795417964179741798417994180041801418024180341804418054180641807418084180941810418114181241813418144181541816418174181841819418204182141822418234182441825418264182741828418294183041831418324183341834418354183641837418384183941840418414184241843418444184541846418474184841849418504185141852418534185441855418564185741858418594186041861418624186341864418654186641867418684186941870418714187241873418744187541876418774187841879418804188141882418834188441885418864188741888418894189041891418924189341894418954189641897418984189941900419014190241903419044190541906419074190841909419104191141912419134191441915419164191741918419194192041921419224192341924419254192641927419284192941930419314193241933419344193541936419374193841939419404194141942419434194441945419464194741948419494195041951419524195341954419554195641957419584195941960419614196241963419644196541966419674196841969419704197141972419734197441975419764197741978419794198041981419824198341984419854198641987419884198941990419914199241993419944199541996419974199841999420004200142002420034200442005420064200742008420094201042011420124201342014420154201642017420184201942020420214202242023420244202542026420274202842029420304203142032420334203442035420364203742038420394204042041420424204342044420454204642047420484204942050420514205242053420544205542056420574205842059420604206142062420634206442065420664206742068420694207042071420724207342074420754207642077420784207942080420814208242083420844208542086420874208842089420904209142092420934209442095420964209742098420994210042101421024210342104421054210642107421084210942110421114211242113421144211542116421174211842119421204212142122421234212442125421264212742128421294213042131421324213342134421354213642137421384213942140421414214242143421444214542146421474214842149421504215142152421534215442155421564215742158421594216042161421624216342164421654216642167421684216942170421714217242173421744217542176421774217842179421804218142182421834218442185421864218742188421894219042191421924219342194421954219642197421984219942200422014220242203422044220542206422074220842209422104221142212422134221442215422164221742218422194222042221422224222342224422254222642227422284222942230422314223242233422344223542236422374223842239422404224142242422434224442245422464224742248422494225042251422524225342254422554225642257422584225942260422614226242263422644226542266422674226842269422704227142272422734227442275422764227742278422794228042281422824228342284422854228642287422884228942290422914229242293422944229542296422974229842299423004230142302423034230442305423064230742308423094231042311423124231342314423154231642317423184231942320423214232242323423244232542326423274232842329423304233142332423334233442335423364233742338423394234042341423424234342344423454234642347423484234942350423514235242353423544235542356423574235842359423604236142362423634236442365423664236742368423694237042371423724237342374423754237642377423784237942380423814238242383423844238542386423874238842389423904239142392423934239442395423964239742398423994240042401424024240342404424054240642407424084240942410424114241242413424144241542416424174241842419424204242142422424234242442425424264242742428424294243042431424324243342434424354243642437424384243942440424414244242443424444244542446424474244842449424504245142452424534245442455424564245742458424594246042461424624246342464424654246642467424684246942470424714247242473424744247542476424774247842479424804248142482424834248442485424864248742488424894249042491424924249342494424954249642497424984249942500425014250242503425044250542506425074250842509425104251142512425134251442515425164251742518425194252042521425224252342524425254252642527425284252942530425314253242533425344253542536425374253842539425404254142542425434254442545425464254742548425494255042551425524255342554425554255642557425584255942560425614256242563425644256542566425674256842569425704257142572425734257442575425764257742578425794258042581425824258342584425854258642587425884258942590425914259242593425944259542596425974259842599426004260142602426034260442605426064260742608426094261042611426124261342614426154261642617426184261942620426214262242623426244262542626426274262842629426304263142632426334263442635426364263742638426394264042641426424264342644426454264642647426484264942650426514265242653426544265542656426574265842659426604266142662426634266442665426664266742668426694267042671426724267342674426754267642677426784267942680426814268242683426844268542686426874268842689426904269142692426934269442695426964269742698426994270042701427024270342704427054270642707427084270942710427114271242713427144271542716427174271842719427204272142722427234272442725427264272742728427294273042731427324273342734427354273642737427384273942740427414274242743427444274542746427474274842749427504275142752427534275442755427564275742758427594276042761427624276342764427654276642767427684276942770427714277242773427744277542776427774277842779427804278142782427834278442785427864278742788427894279042791427924279342794427954279642797427984279942800428014280242803428044280542806428074280842809428104281142812428134281442815428164281742818428194282042821428224282342824428254282642827428284282942830428314283242833428344283542836428374283842839428404284142842428434284442845428464284742848428494285042851428524285342854428554285642857428584285942860428614286242863428644286542866428674286842869428704287142872428734287442875428764287742878428794288042881428824288342884428854288642887428884288942890428914289242893428944289542896428974289842899429004290142902429034290442905429064290742908429094291042911429124291342914429154291642917429184291942920429214292242923429244292542926429274292842929429304293142932429334293442935429364293742938429394294042941429424294342944429454294642947429484294942950429514295242953429544295542956429574295842959429604296142962429634296442965429664296742968429694297042971429724297342974429754297642977429784297942980429814298242983429844298542986429874298842989429904299142992429934299442995429964299742998429994300043001430024300343004430054300643007430084300943010430114301243013430144301543016430174301843019430204302143022430234302443025430264302743028430294303043031430324303343034430354303643037430384303943040430414304243043430444304543046430474304843049430504305143052430534305443055430564305743058430594306043061430624306343064430654306643067430684306943070430714307243073430744307543076430774307843079430804308143082430834308443085430864308743088430894309043091430924309343094430954309643097430984309943100431014310243103431044310543106431074310843109431104311143112431134311443115431164311743118431194312043121431224312343124431254312643127431284312943130431314313243133431344313543136431374313843139431404314143142431434314443145431464314743148431494315043151431524315343154431554315643157431584315943160431614316243163431644316543166431674316843169431704317143172431734317443175431764317743178431794318043181431824318343184431854318643187431884318943190431914319243193431944319543196431974319843199432004320143202432034320443205432064320743208432094321043211432124321343214432154321643217432184321943220432214322243223432244322543226432274322843229432304323143232432334323443235432364323743238432394324043241432424324343244432454324643247432484324943250432514325243253432544325543256432574325843259432604326143262432634326443265432664326743268432694327043271432724327343274432754327643277432784327943280432814328243283432844328543286432874328843289432904329143292432934329443295432964329743298432994330043301433024330343304433054330643307433084330943310433114331243313433144331543316433174331843319433204332143322433234332443325433264332743328433294333043331433324333343334433354333643337433384333943340433414334243343433444334543346433474334843349433504335143352433534335443355433564335743358433594336043361433624336343364433654336643367433684336943370433714337243373433744337543376433774337843379433804338143382433834338443385433864338743388433894339043391433924339343394433954339643397433984339943400434014340243403434044340543406434074340843409434104341143412434134341443415434164341743418434194342043421434224342343424434254342643427434284342943430434314343243433434344343543436434374343843439434404344143442434434344443445434464344743448434494345043451434524345343454434554345643457434584345943460434614346243463434644346543466434674346843469434704347143472434734347443475434764347743478434794348043481434824348343484434854348643487434884348943490434914349243493434944349543496434974349843499435004350143502435034350443505435064350743508435094351043511435124351343514435154351643517435184351943520435214352243523435244352543526435274352843529435304353143532435334353443535435364353743538435394354043541435424354343544435454354643547435484354943550435514355243553435544355543556435574355843559435604356143562435634356443565435664356743568435694357043571435724357343574435754357643577435784357943580435814358243583435844358543586435874358843589435904359143592435934359443595435964359743598435994360043601436024360343604436054360643607436084360943610436114361243613436144361543616436174361843619436204362143622436234362443625436264362743628436294363043631436324363343634436354363643637436384363943640436414364243643436444364543646436474364843649436504365143652436534365443655436564365743658436594366043661436624366343664436654366643667436684366943670436714367243673436744367543676436774367843679436804368143682436834368443685436864368743688436894369043691436924369343694436954369643697436984369943700437014370243703437044370543706437074370843709437104371143712437134371443715437164371743718437194372043721437224372343724437254372643727437284372943730437314373243733437344373543736437374373843739437404374143742437434374443745437464374743748437494375043751437524375343754437554375643757437584375943760437614376243763437644376543766437674376843769437704377143772437734377443775437764377743778437794378043781437824378343784437854378643787437884378943790437914379243793437944379543796437974379843799438004380143802438034380443805438064380743808438094381043811438124381343814438154381643817438184381943820438214382243823438244382543826438274382843829438304383143832438334383443835438364383743838438394384043841438424384343844438454384643847438484384943850438514385243853438544385543856438574385843859438604386143862438634386443865438664386743868438694387043871438724387343874438754387643877438784387943880438814388243883438844388543886438874388843889438904389143892438934389443895438964389743898438994390043901439024390343904439054390643907439084390943910439114391243913439144391543916439174391843919439204392143922439234392443925439264392743928439294393043931439324393343934439354393643937439384393943940439414394243943439444394543946439474394843949439504395143952439534395443955439564395743958439594396043961439624396343964439654396643967439684396943970439714397243973439744397543976439774397843979439804398143982439834398443985439864398743988439894399043991439924399343994439954399643997439984399944000440014400244003440044400544006440074400844009440104401144012440134401444015440164401744018440194402044021440224402344024440254402644027440284402944030440314403244033440344403544036440374403844039440404404144042440434404444045440464404744048440494405044051440524405344054440554405644057440584405944060440614406244063440644406544066440674406844069440704407144072440734407444075440764407744078440794408044081440824408344084440854408644087440884408944090440914409244093440944409544096440974409844099441004410144102441034410444105441064410744108441094411044111441124411344114441154411644117441184411944120441214412244123441244412544126441274412844129441304413144132441334413444135441364413744138441394414044141441424414344144441454414644147441484414944150441514415244153441544415544156441574415844159441604416144162441634416444165441664416744168441694417044171441724417344174441754417644177441784417944180441814418244183441844418544186441874418844189441904419144192441934419444195441964419744198441994420044201442024420344204442054420644207442084420944210442114421244213442144421544216442174421844219442204422144222442234422444225442264422744228442294423044231442324423344234442354423644237442384423944240442414424244243442444424544246442474424844249442504425144252442534425444255442564425744258442594426044261442624426344264442654426644267442684426944270442714427244273442744427544276442774427844279442804428144282442834428444285442864428744288442894429044291442924429344294442954429644297442984429944300443014430244303443044430544306443074430844309443104431144312443134431444315443164431744318443194432044321443224432344324443254432644327443284432944330443314433244333443344433544336443374433844339443404434144342443434434444345443464434744348443494435044351443524435344354443554435644357443584435944360443614436244363443644436544366443674436844369443704437144372443734437444375443764437744378443794438044381443824438344384443854438644387443884438944390443914439244393443944439544396443974439844399444004440144402444034440444405444064440744408444094441044411444124441344414444154441644417444184441944420444214442244423444244442544426444274442844429444304443144432444334443444435444364443744438444394444044441444424444344444444454444644447444484444944450444514445244453444544445544456444574445844459444604446144462444634446444465444664446744468444694447044471444724447344474444754447644477444784447944480444814448244483444844448544486444874448844489444904449144492444934449444495444964449744498444994450044501445024450344504445054450644507445084450944510445114451244513445144451544516445174451844519445204452144522445234452444525445264452744528445294453044531445324453344534445354453644537445384453944540445414454244543445444454544546445474454844549445504455144552445534455444555445564455744558445594456044561445624456344564445654456644567445684456944570445714457244573445744457544576445774457844579445804458144582445834458444585445864458744588445894459044591445924459344594445954459644597445984459944600446014460244603446044460544606446074460844609446104461144612446134461444615446164461744618446194462044621446224462344624446254462644627446284462944630446314463244633446344463544636446374463844639446404464144642446434464444645446464464744648446494465044651446524465344654446554465644657446584465944660446614466244663446644466544666446674466844669446704467144672446734467444675446764467744678446794468044681446824468344684446854468644687446884468944690446914469244693446944469544696446974469844699447004470144702447034470444705447064470744708447094471044711447124471344714447154471644717447184471944720447214472244723447244472544726447274472844729447304473144732447334473444735447364473744738447394474044741447424474344744447454474644747447484474944750447514475244753447544475544756447574475844759447604476144762447634476444765447664476744768447694477044771447724477344774447754477644777447784477944780447814478244783447844478544786447874478844789447904479144792447934479444795447964479744798447994480044801448024480344804448054480644807448084480944810448114481244813448144481544816448174481844819448204482144822448234482444825448264482744828448294483044831448324483344834448354483644837448384483944840448414484244843448444484544846448474484844849448504485144852448534485444855448564485744858448594486044861448624486344864448654486644867448684486944870448714487244873448744487544876448774487844879448804488144882448834488444885448864488744888448894489044891448924489344894448954489644897448984489944900449014490244903449044490544906449074490844909449104491144912449134491444915449164491744918449194492044921449224492344924449254492644927449284492944930449314493244933449344493544936449374493844939449404494144942449434494444945449464494744948449494495044951449524495344954449554495644957449584495944960449614496244963449644496544966449674496844969449704497144972449734497444975449764497744978449794498044981449824498344984449854498644987449884498944990449914499244993449944499544996449974499844999450004500145002450034500445005450064500745008450094501045011450124501345014450154501645017450184501945020450214502245023450244502545026450274502845029450304503145032450334503445035450364503745038450394504045041450424504345044450454504645047450484504945050450514505245053450544505545056450574505845059450604506145062450634506445065450664506745068450694507045071450724507345074450754507645077450784507945080450814508245083450844508545086450874508845089450904509145092450934509445095450964509745098450994510045101451024510345104451054510645107451084510945110451114511245113451144511545116451174511845119451204512145122451234512445125451264512745128451294513045131451324513345134451354513645137451384513945140451414514245143451444514545146451474514845149451504515145152451534515445155451564515745158451594516045161451624516345164451654516645167451684516945170451714517245173451744517545176451774517845179451804518145182451834518445185451864518745188451894519045191451924519345194451954519645197451984519945200452014520245203452044520545206452074520845209452104521145212452134521445215452164521745218452194522045221452224522345224452254522645227452284522945230452314523245233452344523545236452374523845239452404524145242452434524445245452464524745248452494525045251452524525345254452554525645257452584525945260452614526245263452644526545266452674526845269452704527145272452734527445275452764527745278452794528045281452824528345284452854528645287452884528945290452914529245293452944529545296452974529845299453004530145302453034530445305453064530745308453094531045311453124531345314453154531645317453184531945320453214532245323453244532545326453274532845329453304533145332453334533445335453364533745338453394534045341453424534345344453454534645347453484534945350453514535245353453544535545356453574535845359453604536145362453634536445365453664536745368453694537045371453724537345374453754537645377453784537945380453814538245383453844538545386453874538845389453904539145392453934539445395453964539745398453994540045401454024540345404454054540645407454084540945410454114541245413454144541545416454174541845419454204542145422454234542445425454264542745428454294543045431454324543345434454354543645437454384543945440454414544245443454444544545446454474544845449454504545145452454534545445455454564545745458454594546045461454624546345464454654546645467454684546945470454714547245473454744547545476454774547845479454804548145482454834548445485454864548745488454894549045491454924549345494454954549645497454984549945500455014550245503455044550545506455074550845509455104551145512455134551445515455164551745518455194552045521455224552345524455254552645527455284552945530455314553245533455344553545536455374553845539455404554145542455434554445545455464554745548455494555045551455524555345554455554555645557455584555945560455614556245563455644556545566455674556845569455704557145572455734557445575455764557745578455794558045581455824558345584455854558645587455884558945590455914559245593455944559545596455974559845599456004560145602456034560445605456064560745608456094561045611456124561345614456154561645617456184561945620456214562245623456244562545626456274562845629456304563145632456334563445635456364563745638456394564045641456424564345644456454564645647456484564945650456514565245653456544565545656456574565845659456604566145662456634566445665456664566745668456694567045671456724567345674456754567645677456784567945680456814568245683456844568545686456874568845689456904569145692456934569445695456964569745698456994570045701457024570345704457054570645707457084570945710457114571245713457144571545716457174571845719457204572145722457234572445725457264572745728457294573045731457324573345734457354573645737457384573945740457414574245743457444574545746457474574845749457504575145752457534575445755457564575745758457594576045761457624576345764457654576645767457684576945770457714577245773457744577545776457774577845779457804578145782457834578445785457864578745788457894579045791457924579345794457954579645797457984579945800458014580245803458044580545806458074580845809458104581145812458134581445815458164581745818458194582045821458224582345824458254582645827458284582945830458314583245833458344583545836458374583845839458404584145842458434584445845458464584745848458494585045851458524585345854458554585645857458584585945860458614586245863458644586545866458674586845869458704587145872458734587445875458764587745878458794588045881458824588345884458854588645887458884588945890458914589245893458944589545896458974589845899459004590145902459034590445905459064590745908459094591045911459124591345914459154591645917459184591945920459214592245923459244592545926459274592845929459304593145932459334593445935459364593745938459394594045941459424594345944459454594645947459484594945950459514595245953459544595545956459574595845959459604596145962459634596445965459664596745968459694597045971459724597345974459754597645977459784597945980459814598245983459844598545986459874598845989459904599145992459934599445995459964599745998459994600046001460024600346004460054600646007460084600946010460114601246013460144601546016460174601846019460204602146022460234602446025460264602746028460294603046031460324603346034460354603646037460384603946040460414604246043460444604546046460474604846049460504605146052460534605446055460564605746058460594606046061460624606346064460654606646067460684606946070460714607246073460744607546076460774607846079460804608146082460834608446085460864608746088460894609046091460924609346094460954609646097460984609946100461014610246103461044610546106461074610846109461104611146112461134611446115461164611746118461194612046121461224612346124461254612646127461284612946130461314613246133461344613546136461374613846139461404614146142461434614446145461464614746148461494615046151461524615346154461554615646157461584615946160461614616246163461644616546166461674616846169461704617146172461734617446175461764617746178461794618046181461824618346184461854618646187461884618946190461914619246193461944619546196461974619846199462004620146202462034620446205462064620746208462094621046211462124621346214462154621646217462184621946220462214622246223462244622546226462274622846229462304623146232462334623446235462364623746238462394624046241462424624346244462454624646247462484624946250462514625246253462544625546256462574625846259462604626146262462634626446265462664626746268462694627046271462724627346274462754627646277462784627946280462814628246283462844628546286462874628846289462904629146292462934629446295462964629746298462994630046301463024630346304463054630646307463084630946310463114631246313463144631546316463174631846319463204632146322463234632446325463264632746328463294633046331463324633346334463354633646337463384633946340463414634246343463444634546346463474634846349463504635146352463534635446355463564635746358463594636046361463624636346364463654636646367463684636946370463714637246373463744637546376463774637846379463804638146382463834638446385463864638746388463894639046391463924639346394463954639646397463984639946400464014640246403464044640546406464074640846409464104641146412464134641446415464164641746418464194642046421464224642346424464254642646427464284642946430464314643246433464344643546436464374643846439464404644146442464434644446445464464644746448464494645046451464524645346454464554645646457464584645946460464614646246463464644646546466464674646846469464704647146472464734647446475464764647746478464794648046481464824648346484464854648646487464884648946490464914649246493464944649546496464974649846499465004650146502465034650446505465064650746508465094651046511465124651346514465154651646517465184651946520465214652246523465244652546526465274652846529465304653146532465334653446535465364653746538465394654046541465424654346544465454654646547465484654946550465514655246553465544655546556465574655846559465604656146562465634656446565465664656746568465694657046571465724657346574465754657646577465784657946580465814658246583465844658546586465874658846589465904659146592465934659446595465964659746598465994660046601466024660346604466054660646607466084660946610466114661246613466144661546616466174661846619466204662146622466234662446625466264662746628466294663046631466324663346634466354663646637466384663946640466414664246643466444664546646466474664846649466504665146652466534665446655466564665746658466594666046661466624666346664466654666646667466684666946670466714667246673466744667546676466774667846679466804668146682466834668446685466864668746688466894669046691466924669346694466954669646697466984669946700467014670246703467044670546706467074670846709467104671146712467134671446715467164671746718467194672046721467224672346724467254672646727467284672946730467314673246733467344673546736467374673846739467404674146742467434674446745467464674746748467494675046751467524675346754467554675646757467584675946760467614676246763467644676546766467674676846769467704677146772467734677446775467764677746778467794678046781467824678346784467854678646787467884678946790467914679246793467944679546796467974679846799468004680146802468034680446805468064680746808468094681046811468124681346814468154681646817468184681946820468214682246823468244682546826468274682846829468304683146832468334683446835468364683746838468394684046841468424684346844468454684646847468484684946850468514685246853468544685546856468574685846859468604686146862468634686446865468664686746868468694687046871468724687346874468754687646877468784687946880468814688246883468844688546886468874688846889468904689146892468934689446895468964689746898468994690046901469024690346904469054690646907469084690946910469114691246913469144691546916469174691846919469204692146922469234692446925469264692746928469294693046931469324693346934469354693646937469384693946940469414694246943469444694546946469474694846949469504695146952469534695446955469564695746958469594696046961469624696346964469654696646967469684696946970469714697246973469744697546976469774697846979469804698146982469834698446985469864698746988469894699046991469924699346994469954699646997469984699947000470014700247003470044700547006470074700847009470104701147012470134701447015470164701747018470194702047021470224702347024470254702647027470284702947030470314703247033470344703547036470374703847039470404704147042470434704447045470464704747048470494705047051470524705347054470554705647057470584705947060470614706247063470644706547066470674706847069470704707147072470734707447075470764707747078470794708047081470824708347084470854708647087470884708947090470914709247093470944709547096470974709847099471004710147102471034710447105471064710747108471094711047111471124711347114471154711647117471184711947120471214712247123471244712547126471274712847129471304713147132471334713447135471364713747138471394714047141471424714347144471454714647147471484714947150471514715247153471544715547156471574715847159471604716147162471634716447165471664716747168471694717047171471724717347174471754717647177471784717947180471814718247183471844718547186471874718847189471904719147192471934719447195471964719747198471994720047201472024720347204472054720647207472084720947210472114721247213472144721547216472174721847219472204722147222472234722447225472264722747228472294723047231472324723347234472354723647237472384723947240472414724247243472444724547246472474724847249472504725147252472534725447255472564725747258472594726047261472624726347264472654726647267472684726947270472714727247273472744727547276472774727847279472804728147282472834728447285472864728747288472894729047291472924729347294472954729647297472984729947300473014730247303473044730547306473074730847309473104731147312473134731447315473164731747318473194732047321473224732347324473254732647327473284732947330473314733247333473344733547336473374733847339473404734147342473434734447345473464734747348473494735047351473524735347354473554735647357473584735947360473614736247363473644736547366473674736847369473704737147372473734737447375473764737747378473794738047381473824738347384473854738647387473884738947390473914739247393473944739547396473974739847399474004740147402474034740447405474064740747408474094741047411474124741347414474154741647417474184741947420474214742247423474244742547426474274742847429474304743147432474334743447435474364743747438474394744047441474424744347444474454744647447474484744947450474514745247453474544745547456474574745847459474604746147462474634746447465474664746747468474694747047471474724747347474474754747647477474784747947480474814748247483474844748547486474874748847489474904749147492474934749447495474964749747498474994750047501475024750347504475054750647507475084750947510475114751247513475144751547516475174751847519475204752147522475234752447525475264752747528475294753047531475324753347534475354753647537475384753947540475414754247543475444754547546475474754847549475504755147552475534755447555475564755747558475594756047561475624756347564475654756647567475684756947570475714757247573475744757547576475774757847579475804758147582475834758447585475864758747588475894759047591475924759347594475954759647597475984759947600476014760247603476044760547606476074760847609476104761147612476134761447615476164761747618476194762047621476224762347624476254762647627476284762947630476314763247633476344763547636476374763847639476404764147642476434764447645476464764747648476494765047651476524765347654476554765647657476584765947660476614766247663476644766547666476674766847669476704767147672476734767447675476764767747678476794768047681476824768347684476854768647687476884768947690476914769247693476944769547696476974769847699477004770147702477034770447705477064770747708477094771047711477124771347714477154771647717477184771947720477214772247723477244772547726477274772847729477304773147732477334773447735477364773747738477394774047741477424774347744477454774647747477484774947750477514775247753477544775547756477574775847759477604776147762477634776447765477664776747768477694777047771477724777347774477754777647777477784777947780477814778247783477844778547786477874778847789477904779147792477934779447795477964779747798477994780047801478024780347804478054780647807478084780947810478114781247813478144781547816478174781847819478204782147822478234782447825478264782747828478294783047831478324783347834478354783647837478384783947840478414784247843478444784547846478474784847849478504785147852478534785447855478564785747858478594786047861478624786347864478654786647867478684786947870478714787247873478744787547876478774787847879478804788147882478834788447885478864788747888478894789047891478924789347894478954789647897478984789947900479014790247903479044790547906479074790847909479104791147912479134791447915479164791747918479194792047921479224792347924479254792647927479284792947930479314793247933479344793547936479374793847939479404794147942479434794447945479464794747948479494795047951479524795347954479554795647957479584795947960479614796247963479644796547966479674796847969479704797147972479734797447975479764797747978479794798047981479824798347984479854798647987479884798947990479914799247993479944799547996479974799847999480004800148002480034800448005480064800748008480094801048011480124801348014480154801648017480184801948020480214802248023480244802548026480274802848029480304803148032480334803448035480364803748038480394804048041480424804348044480454804648047480484804948050480514805248053480544805548056480574805848059480604806148062480634806448065480664806748068480694807048071480724807348074480754807648077480784807948080480814808248083480844808548086480874808848089480904809148092480934809448095480964809748098480994810048101481024810348104481054810648107481084810948110481114811248113481144811548116481174811848119481204812148122481234812448125481264812748128481294813048131481324813348134481354813648137481384813948140481414814248143481444814548146481474814848149481504815148152481534815448155481564815748158481594816048161481624816348164481654816648167481684816948170481714817248173481744817548176481774817848179481804818148182481834818448185481864818748188481894819048191481924819348194481954819648197481984819948200482014820248203482044820548206482074820848209482104821148212482134821448215482164821748218482194822048221482224822348224482254822648227482284822948230482314823248233482344823548236482374823848239482404824148242482434824448245482464824748248482494825048251482524825348254482554825648257482584825948260482614826248263482644826548266482674826848269482704827148272482734827448275482764827748278482794828048281482824828348284482854828648287482884828948290482914829248293482944829548296482974829848299483004830148302483034830448305483064830748308483094831048311483124831348314483154831648317483184831948320483214832248323483244832548326483274832848329483304833148332483334833448335483364833748338483394834048341483424834348344483454834648347483484834948350483514835248353483544835548356483574835848359483604836148362483634836448365483664836748368483694837048371483724837348374483754837648377483784837948380483814838248383483844838548386483874838848389483904839148392483934839448395483964839748398483994840048401484024840348404484054840648407484084840948410484114841248413484144841548416484174841848419484204842148422484234842448425484264842748428484294843048431484324843348434484354843648437484384843948440484414844248443484444844548446484474844848449484504845148452484534845448455484564845748458484594846048461484624846348464484654846648467484684846948470484714847248473484744847548476484774847848479484804848148482484834848448485484864848748488484894849048491484924849348494484954849648497484984849948500485014850248503485044850548506485074850848509485104851148512485134851448515485164851748518485194852048521485224852348524485254852648527485284852948530485314853248533485344853548536485374853848539485404854148542485434854448545485464854748548485494855048551485524855348554485554855648557485584855948560485614856248563485644856548566485674856848569485704857148572485734857448575485764857748578485794858048581485824858348584485854858648587485884858948590485914859248593485944859548596485974859848599486004860148602486034860448605486064860748608486094861048611486124861348614486154861648617486184861948620486214862248623486244862548626486274862848629486304863148632486334863448635486364863748638486394864048641486424864348644486454864648647486484864948650486514865248653486544865548656486574865848659486604866148662486634866448665486664866748668486694867048671486724867348674486754867648677486784867948680486814868248683486844868548686486874868848689486904869148692486934869448695486964869748698486994870048701487024870348704487054870648707487084870948710487114871248713487144871548716487174871848719487204872148722487234872448725487264872748728487294873048731487324873348734487354873648737487384873948740487414874248743487444874548746487474874848749487504875148752487534875448755487564875748758487594876048761487624876348764487654876648767487684876948770487714877248773487744877548776487774877848779487804878148782487834878448785487864878748788487894879048791487924879348794487954879648797487984879948800488014880248803488044880548806488074880848809488104881148812488134881448815488164881748818488194882048821488224882348824488254882648827488284882948830488314883248833488344883548836488374883848839488404884148842488434884448845488464884748848488494885048851488524885348854488554885648857488584885948860488614886248863488644886548866488674886848869488704887148872488734887448875488764887748878488794888048881488824888348884488854888648887488884888948890488914889248893488944889548896488974889848899489004890148902489034890448905489064890748908489094891048911489124891348914489154891648917489184891948920489214892248923489244892548926489274892848929489304893148932489334893448935489364893748938489394894048941489424894348944489454894648947489484894948950489514895248953489544895548956489574895848959489604896148962489634896448965489664896748968489694897048971489724897348974489754897648977489784897948980489814898248983489844898548986489874898848989489904899148992489934899448995489964899748998489994900049001490024900349004490054900649007490084900949010490114901249013490144901549016490174901849019490204902149022490234902449025490264902749028490294903049031490324903349034490354903649037490384903949040490414904249043490444904549046490474904849049490504905149052490534905449055490564905749058490594906049061490624906349064490654906649067490684906949070490714907249073490744907549076490774907849079490804908149082490834908449085490864908749088490894909049091490924909349094490954909649097490984909949100491014910249103491044910549106491074910849109491104911149112491134911449115491164911749118491194912049121491224912349124491254912649127491284912949130491314913249133491344913549136491374913849139491404914149142491434914449145491464914749148491494915049151491524915349154491554915649157491584915949160491614916249163491644916549166491674916849169491704917149172491734917449175491764917749178491794918049181491824918349184491854918649187491884918949190491914919249193491944919549196491974919849199492004920149202492034920449205492064920749208492094921049211492124921349214492154921649217492184921949220492214922249223492244922549226492274922849229492304923149232492334923449235492364923749238492394924049241492424924349244492454924649247492484924949250492514925249253492544925549256492574925849259492604926149262492634926449265492664926749268492694927049271492724927349274492754927649277492784927949280492814928249283492844928549286492874928849289492904929149292492934929449295492964929749298492994930049301493024930349304493054930649307493084930949310493114931249313493144931549316493174931849319493204932149322493234932449325493264932749328493294933049331493324933349334493354933649337493384933949340493414934249343493444934549346493474934849349493504935149352493534935449355493564935749358493594936049361493624936349364493654936649367493684936949370493714937249373493744937549376493774937849379493804938149382493834938449385493864938749388493894939049391493924939349394493954939649397493984939949400494014940249403494044940549406494074940849409494104941149412494134941449415494164941749418494194942049421494224942349424494254942649427494284942949430494314943249433494344943549436494374943849439494404944149442494434944449445494464944749448494494945049451494524945349454494554945649457494584945949460494614946249463494644946549466494674946849469494704947149472494734947449475494764947749478494794948049481494824948349484494854948649487494884948949490494914949249493494944949549496494974949849499495004950149502495034950449505495064950749508495094951049511495124951349514495154951649517495184951949520495214952249523495244952549526495274952849529495304953149532495334953449535495364953749538495394954049541495424954349544495454954649547495484954949550495514955249553495544955549556495574955849559495604956149562495634956449565495664956749568495694957049571495724957349574495754957649577495784957949580495814958249583495844958549586495874958849589495904959149592495934959449595495964959749598495994960049601496024960349604496054960649607496084960949610496114961249613496144961549616496174961849619496204962149622496234962449625496264962749628496294963049631496324963349634496354963649637496384963949640496414964249643496444964549646496474964849649496504965149652496534965449655496564965749658496594966049661496624966349664496654966649667496684966949670496714967249673496744967549676496774967849679496804968149682496834968449685496864968749688496894969049691496924969349694496954969649697496984969949700497014970249703497044970549706497074970849709497104971149712497134971449715497164971749718497194972049721497224972349724497254972649727497284972949730497314973249733497344973549736497374973849739497404974149742497434974449745497464974749748497494975049751497524975349754497554975649757497584975949760497614976249763497644976549766497674976849769497704977149772497734977449775497764977749778497794978049781497824978349784497854978649787497884978949790497914979249793497944979549796497974979849799498004980149802498034980449805498064980749808498094981049811498124981349814498154981649817498184981949820498214982249823498244982549826498274982849829498304983149832498334983449835498364983749838498394984049841498424984349844498454984649847498484984949850498514985249853498544985549856498574985849859498604986149862498634986449865498664986749868498694987049871498724987349874498754987649877498784987949880498814988249883498844988549886498874988849889498904989149892498934989449895498964989749898498994990049901499024990349904499054990649907499084990949910499114991249913499144991549916499174991849919499204992149922499234992449925499264992749928499294993049931499324993349934499354993649937499384993949940499414994249943499444994549946499474994849949499504995149952499534995449955499564995749958499594996049961499624996349964499654996649967499684996949970499714997249973499744997549976499774997849979499804998149982499834998449985499864998749988499894999049991499924999349994499954999649997499984999950000500015000250003500045000550006500075000850009500105001150012500135001450015500165001750018500195002050021500225002350024500255002650027500285002950030500315003250033500345003550036500375003850039500405004150042500435004450045500465004750048500495005050051500525005350054500555005650057500585005950060500615006250063500645006550066500675006850069500705007150072500735007450075500765007750078500795008050081500825008350084500855008650087500885008950090500915009250093500945009550096500975009850099501005010150102501035010450105501065010750108501095011050111501125011350114501155011650117501185011950120501215012250123501245012550126501275012850129501305013150132501335013450135501365013750138501395014050141501425014350144501455014650147501485014950150501515015250153501545015550156501575015850159501605016150162501635016450165501665016750168501695017050171501725017350174501755017650177501785017950180501815018250183501845018550186501875018850189501905019150192501935019450195501965019750198501995020050201502025020350204502055020650207502085020950210502115021250213502145021550216502175021850219502205022150222502235022450225502265022750228502295023050231502325023350234502355023650237502385023950240502415024250243502445024550246502475024850249502505025150252502535025450255502565025750258502595026050261502625026350264502655026650267502685026950270502715027250273502745027550276502775027850279502805028150282502835028450285502865028750288502895029050291502925029350294502955029650297502985029950300503015030250303503045030550306503075030850309503105031150312503135031450315503165031750318503195032050321503225032350324503255032650327503285032950330503315033250333503345033550336503375033850339503405034150342503435034450345503465034750348503495035050351503525035350354503555035650357503585035950360503615036250363503645036550366503675036850369503705037150372503735037450375503765037750378503795038050381503825038350384503855038650387503885038950390503915039250393503945039550396503975039850399504005040150402504035040450405504065040750408504095041050411504125041350414504155041650417504185041950420504215042250423504245042550426504275042850429504305043150432504335043450435504365043750438504395044050441504425044350444504455044650447504485044950450504515045250453504545045550456504575045850459504605046150462504635046450465504665046750468504695047050471504725047350474504755047650477504785047950480504815048250483504845048550486504875048850489504905049150492504935049450495504965049750498504995050050501505025050350504505055050650507505085050950510505115051250513505145051550516505175051850519505205052150522505235052450525505265052750528505295053050531505325053350534505355053650537505385053950540505415054250543505445054550546505475054850549505505055150552505535055450555505565055750558505595056050561505625056350564505655056650567505685056950570505715057250573505745057550576505775057850579505805058150582505835058450585505865058750588505895059050591505925059350594505955059650597505985059950600506015060250603506045060550606506075060850609506105061150612506135061450615506165061750618506195062050621506225062350624506255062650627506285062950630506315063250633506345063550636506375063850639506405064150642506435064450645506465064750648506495065050651506525065350654506555065650657506585065950660506615066250663506645066550666506675066850669506705067150672506735067450675506765067750678506795068050681506825068350684506855068650687506885068950690506915069250693506945069550696506975069850699507005070150702507035070450705507065070750708507095071050711507125071350714507155071650717507185071950720507215072250723507245072550726507275072850729507305073150732507335073450735507365073750738507395074050741507425074350744507455074650747507485074950750507515075250753507545075550756507575075850759507605076150762507635076450765507665076750768507695077050771507725077350774507755077650777507785077950780507815078250783507845078550786507875078850789507905079150792507935079450795507965079750798507995080050801508025080350804508055080650807508085080950810508115081250813508145081550816508175081850819508205082150822508235082450825508265082750828508295083050831508325083350834508355083650837508385083950840508415084250843508445084550846508475084850849508505085150852508535085450855508565085750858508595086050861508625086350864508655086650867508685086950870508715087250873508745087550876508775087850879508805088150882508835088450885508865088750888508895089050891508925089350894508955089650897508985089950900509015090250903509045090550906509075090850909509105091150912509135091450915509165091750918509195092050921509225092350924509255092650927509285092950930509315093250933509345093550936509375093850939509405094150942509435094450945509465094750948509495095050951509525095350954509555095650957509585095950960509615096250963509645096550966509675096850969509705097150972509735097450975509765097750978509795098050981509825098350984509855098650987509885098950990509915099250993509945099550996509975099850999510005100151002510035100451005510065100751008510095101051011510125101351014510155101651017510185101951020510215102251023510245102551026510275102851029510305103151032510335103451035510365103751038510395104051041510425104351044510455104651047510485104951050510515105251053510545105551056510575105851059510605106151062510635106451065510665106751068510695107051071510725107351074510755107651077510785107951080510815108251083510845108551086510875108851089510905109151092510935109451095510965109751098510995110051101511025110351104511055110651107511085110951110511115111251113511145111551116511175111851119511205112151122511235112451125511265112751128511295113051131511325113351134511355113651137511385113951140511415114251143511445114551146511475114851149511505115151152511535115451155511565115751158511595116051161511625116351164511655116651167511685116951170511715117251173511745117551176511775117851179511805118151182511835118451185511865118751188511895119051191511925119351194511955119651197511985119951200512015120251203512045120551206512075120851209512105121151212512135121451215512165121751218512195122051221512225122351224512255122651227512285122951230512315123251233512345123551236512375123851239512405124151242512435124451245512465124751248512495125051251512525125351254512555125651257512585125951260512615126251263512645126551266512675126851269512705127151272512735127451275512765127751278512795128051281512825128351284512855128651287512885128951290512915129251293512945129551296512975129851299513005130151302513035130451305513065130751308513095131051311513125131351314513155131651317513185131951320513215132251323513245132551326513275132851329513305133151332513335133451335513365133751338513395134051341513425134351344513455134651347513485134951350513515135251353513545135551356513575135851359513605136151362513635136451365513665136751368513695137051371513725137351374513755137651377513785137951380513815138251383513845138551386513875138851389513905139151392513935139451395513965139751398513995140051401514025140351404514055140651407514085140951410514115141251413514145141551416514175141851419514205142151422514235142451425514265142751428514295143051431514325143351434514355143651437514385143951440514415144251443514445144551446514475144851449514505145151452514535145451455514565145751458514595146051461514625146351464514655146651467514685146951470514715147251473514745147551476514775147851479514805148151482514835148451485514865148751488514895149051491514925149351494514955149651497514985149951500515015150251503515045150551506515075150851509515105151151512515135151451515515165151751518515195152051521515225152351524515255152651527515285152951530515315153251533515345153551536515375153851539515405154151542515435154451545515465154751548515495155051551515525155351554515555155651557515585155951560515615156251563515645156551566515675156851569515705157151572515735157451575515765157751578515795158051581515825158351584515855158651587515885158951590515915159251593515945159551596515975159851599516005160151602516035160451605516065160751608516095161051611516125161351614516155161651617516185161951620516215162251623516245162551626516275162851629516305163151632516335163451635516365163751638516395164051641516425164351644516455164651647516485164951650516515165251653516545165551656516575165851659516605166151662516635166451665516665166751668516695167051671516725167351674516755167651677516785167951680516815168251683516845168551686516875168851689516905169151692516935169451695516965169751698516995170051701517025170351704517055170651707517085170951710517115171251713517145171551716517175171851719517205172151722517235172451725517265172751728517295173051731517325173351734517355173651737517385173951740517415174251743517445174551746517475174851749517505175151752517535175451755517565175751758517595176051761517625176351764517655176651767517685176951770517715177251773517745177551776517775177851779517805178151782517835178451785517865178751788517895179051791517925179351794517955179651797517985179951800518015180251803518045180551806518075180851809518105181151812518135181451815518165181751818518195182051821518225182351824518255182651827518285182951830518315183251833518345183551836518375183851839518405184151842518435184451845518465184751848518495185051851518525185351854518555185651857518585185951860518615186251863518645186551866518675186851869518705187151872518735187451875518765187751878518795188051881518825188351884518855188651887518885188951890518915189251893518945189551896518975189851899519005190151902519035190451905519065190751908519095191051911519125191351914519155191651917519185191951920519215192251923519245192551926519275192851929519305193151932519335193451935519365193751938519395194051941519425194351944519455194651947519485194951950519515195251953519545195551956519575195851959519605196151962519635196451965519665196751968519695197051971519725197351974519755197651977519785197951980519815198251983519845198551986519875198851989519905199151992519935199451995519965199751998519995200052001520025200352004520055200652007520085200952010520115201252013520145201552016520175201852019520205202152022520235202452025520265202752028520295203052031520325203352034520355203652037520385203952040520415204252043520445204552046520475204852049520505205152052520535205452055520565205752058520595206052061520625206352064520655206652067520685206952070520715207252073520745207552076520775207852079520805208152082520835208452085520865208752088520895209052091520925209352094520955209652097520985209952100521015210252103521045210552106521075210852109521105211152112521135211452115521165211752118521195212052121521225212352124521255212652127521285212952130521315213252133521345213552136521375213852139521405214152142521435214452145521465214752148521495215052151521525215352154521555215652157521585215952160521615216252163521645216552166521675216852169521705217152172521735217452175521765217752178521795218052181521825218352184521855218652187521885218952190521915219252193521945219552196521975219852199522005220152202522035220452205522065220752208522095221052211522125221352214522155221652217522185221952220522215222252223522245222552226522275222852229522305223152232522335223452235522365223752238522395224052241522425224352244522455224652247522485224952250522515225252253522545225552256522575225852259522605226152262522635226452265522665226752268522695227052271522725227352274522755227652277522785227952280522815228252283522845228552286522875228852289522905229152292522935229452295522965229752298522995230052301523025230352304523055230652307523085230952310523115231252313523145231552316523175231852319523205232152322523235232452325523265232752328523295233052331523325233352334523355233652337523385233952340523415234252343523445234552346523475234852349523505235152352523535235452355523565235752358523595236052361523625236352364523655236652367523685236952370523715237252373523745237552376523775237852379523805238152382523835238452385523865238752388523895239052391523925239352394523955239652397523985239952400524015240252403524045240552406524075240852409524105241152412524135241452415524165241752418524195242052421524225242352424524255242652427524285242952430524315243252433524345243552436524375243852439524405244152442524435244452445524465244752448524495245052451524525245352454524555245652457524585245952460524615246252463524645246552466524675246852469524705247152472524735247452475524765247752478524795248052481524825248352484524855248652487524885248952490524915249252493524945249552496524975249852499525005250152502525035250452505525065250752508525095251052511525125251352514525155251652517525185251952520525215252252523525245252552526525275252852529525305253152532525335253452535525365253752538525395254052541525425254352544525455254652547525485254952550525515255252553525545255552556525575255852559525605256152562525635256452565525665256752568525695257052571525725257352574525755257652577525785257952580525815258252583525845258552586525875258852589525905259152592525935259452595525965259752598525995260052601526025260352604526055260652607526085260952610526115261252613526145261552616526175261852619526205262152622526235262452625526265262752628526295263052631526325263352634526355263652637526385263952640526415264252643526445264552646526475264852649526505265152652526535265452655526565265752658526595266052661526625266352664526655266652667526685266952670526715267252673526745267552676526775267852679526805268152682526835268452685526865268752688526895269052691526925269352694526955269652697526985269952700527015270252703527045270552706527075270852709527105271152712527135271452715527165271752718527195272052721527225272352724527255272652727527285272952730527315273252733527345273552736527375273852739527405274152742527435274452745527465274752748527495275052751527525275352754527555275652757527585275952760527615276252763527645276552766527675276852769527705277152772527735277452775527765277752778527795278052781527825278352784527855278652787527885278952790527915279252793527945279552796527975279852799528005280152802528035280452805528065280752808528095281052811528125281352814528155281652817528185281952820528215282252823528245282552826528275282852829528305283152832528335283452835528365283752838528395284052841528425284352844528455284652847528485284952850528515285252853528545285552856528575285852859528605286152862528635286452865528665286752868528695287052871528725287352874528755287652877528785287952880528815288252883528845288552886528875288852889528905289152892528935289452895528965289752898528995290052901529025290352904529055290652907529085290952910529115291252913529145291552916529175291852919529205292152922529235292452925529265292752928529295293052931529325293352934529355293652937529385293952940529415294252943529445294552946529475294852949529505295152952529535295452955529565295752958529595296052961529625296352964529655296652967529685296952970529715297252973529745297552976529775297852979529805298152982529835298452985529865298752988529895299052991529925299352994529955299652997529985299953000530015300253003530045300553006530075300853009530105301153012530135301453015530165301753018530195302053021530225302353024530255302653027530285302953030530315303253033530345303553036530375303853039530405304153042530435304453045530465304753048530495305053051530525305353054530555305653057530585305953060530615306253063530645306553066530675306853069530705307153072530735307453075530765307753078530795308053081530825308353084530855308653087530885308953090530915309253093530945309553096530975309853099531005310153102531035310453105531065310753108531095311053111531125311353114531155311653117531185311953120531215312253123531245312553126531275312853129531305313153132531335313453135531365313753138531395314053141531425314353144531455314653147531485314953150531515315253153531545315553156531575315853159531605316153162531635316453165531665316753168531695317053171531725317353174531755317653177531785317953180531815318253183531845318553186531875318853189531905319153192531935319453195531965319753198531995320053201532025320353204532055320653207532085320953210532115321253213532145321553216532175321853219532205322153222532235322453225532265322753228532295323053231532325323353234532355323653237532385323953240532415324253243532445324553246532475324853249532505325153252532535325453255532565325753258532595326053261532625326353264532655326653267532685326953270532715327253273532745327553276532775327853279532805328153282532835328453285532865328753288532895329053291532925329353294532955329653297532985329953300533015330253303533045330553306533075330853309533105331153312533135331453315533165331753318533195332053321533225332353324533255332653327533285332953330533315333253333533345333553336533375333853339533405334153342533435334453345533465334753348533495335053351533525335353354533555335653357533585335953360533615336253363533645336553366533675336853369533705337153372533735337453375533765337753378533795338053381533825338353384533855338653387533885338953390533915339253393533945339553396533975339853399534005340153402534035340453405534065340753408534095341053411534125341353414534155341653417534185341953420534215342253423534245342553426534275342853429534305343153432534335343453435534365343753438534395344053441534425344353444534455344653447534485344953450534515345253453534545345553456534575345853459534605346153462534635346453465534665346753468534695347053471534725347353474534755347653477534785347953480534815348253483534845348553486534875348853489534905349153492534935349453495534965349753498534995350053501535025350353504535055350653507535085350953510535115351253513535145351553516535175351853519535205352153522535235352453525535265352753528535295353053531535325353353534535355353653537535385353953540535415354253543535445354553546535475354853549535505355153552535535355453555535565355753558535595356053561535625356353564535655356653567535685356953570535715357253573535745357553576535775357853579535805358153582535835358453585535865358753588535895359053591535925359353594535955359653597535985359953600536015360253603536045360553606536075360853609536105361153612536135361453615536165361753618536195362053621536225362353624536255362653627536285362953630536315363253633536345363553636536375363853639536405364153642536435364453645536465364753648536495365053651536525365353654536555365653657536585365953660536615366253663536645366553666536675366853669536705367153672536735367453675536765367753678536795368053681536825368353684536855368653687536885368953690536915369253693536945369553696536975369853699537005370153702537035370453705537065370753708537095371053711537125371353714537155371653717537185371953720537215372253723537245372553726537275372853729537305373153732537335373453735537365373753738537395374053741537425374353744537455374653747537485374953750537515375253753537545375553756537575375853759537605376153762537635376453765537665376753768537695377053771537725377353774537755377653777537785377953780537815378253783537845378553786537875378853789537905379153792537935379453795537965379753798537995380053801538025380353804538055380653807538085380953810538115381253813538145381553816538175381853819538205382153822538235382453825538265382753828538295383053831538325383353834538355383653837538385383953840538415384253843538445384553846538475384853849538505385153852538535385453855538565385753858538595386053861538625386353864538655386653867538685386953870538715387253873538745387553876538775387853879538805388153882538835388453885538865388753888538895389053891538925389353894538955389653897538985389953900539015390253903539045390553906539075390853909539105391153912539135391453915539165391753918539195392053921539225392353924539255392653927539285392953930539315393253933539345393553936539375393853939539405394153942539435394453945539465394753948539495395053951539525395353954539555395653957539585395953960539615396253963539645396553966539675396853969539705397153972539735397453975539765397753978539795398053981539825398353984539855398653987539885398953990539915399253993539945399553996539975399853999540005400154002540035400454005540065400754008540095401054011540125401354014540155401654017540185401954020540215402254023540245402554026540275402854029540305403154032540335403454035540365403754038540395404054041540425404354044540455404654047540485404954050540515405254053540545405554056540575405854059540605406154062540635406454065540665406754068540695407054071540725407354074540755407654077540785407954080540815408254083540845408554086540875408854089540905409154092540935409454095540965409754098540995410054101541025410354104541055410654107541085410954110541115411254113541145411554116541175411854119541205412154122541235412454125541265412754128541295413054131541325413354134541355413654137541385413954140541415414254143541445414554146541475414854149541505415154152541535415454155541565415754158541595416054161541625416354164541655416654167541685416954170541715417254173541745417554176541775417854179541805418154182541835418454185541865418754188541895419054191541925419354194541955419654197541985419954200542015420254203542045420554206542075420854209542105421154212542135421454215542165421754218542195422054221542225422354224542255422654227542285422954230542315423254233542345423554236542375423854239542405424154242542435424454245542465424754248542495425054251542525425354254542555425654257542585425954260542615426254263542645426554266542675426854269542705427154272542735427454275542765427754278542795428054281542825428354284542855428654287542885428954290542915429254293542945429554296542975429854299543005430154302543035430454305543065430754308543095431054311543125431354314543155431654317543185431954320543215432254323543245432554326543275432854329543305433154332543335433454335543365433754338543395434054341543425434354344543455434654347543485434954350543515435254353543545435554356543575435854359543605436154362543635436454365543665436754368543695437054371543725437354374543755437654377543785437954380543815438254383543845438554386543875438854389543905439154392543935439454395543965439754398543995440054401544025440354404544055440654407544085440954410544115441254413544145441554416544175441854419544205442154422544235442454425544265442754428544295443054431544325443354434544355443654437544385443954440544415444254443544445444554446544475444854449544505445154452544535445454455544565445754458544595446054461544625446354464544655446654467544685446954470544715447254473544745447554476544775447854479544805448154482544835448454485544865448754488544895449054491544925449354494544955449654497544985449954500545015450254503545045450554506545075450854509545105451154512545135451454515545165451754518545195452054521545225452354524545255452654527545285452954530545315453254533545345453554536545375453854539545405454154542545435454454545545465454754548545495455054551545525455354554545555455654557545585455954560545615456254563545645456554566545675456854569545705457154572545735457454575545765457754578545795458054581545825458354584545855458654587545885458954590545915459254593545945459554596545975459854599546005460154602546035460454605546065460754608546095461054611546125461354614546155461654617546185461954620546215462254623546245462554626546275462854629546305463154632546335463454635546365463754638546395464054641546425464354644546455464654647546485464954650546515465254653546545465554656546575465854659546605466154662546635466454665546665466754668546695467054671546725467354674546755467654677546785467954680546815468254683546845468554686546875468854689546905469154692546935469454695546965469754698546995470054701547025470354704547055470654707547085470954710547115471254713547145471554716547175471854719547205472154722547235472454725547265472754728547295473054731547325473354734547355473654737547385473954740547415474254743547445474554746547475474854749547505475154752547535475454755547565475754758547595476054761547625476354764547655476654767547685476954770547715477254773547745477554776547775477854779547805478154782547835478454785547865478754788547895479054791547925479354794547955479654797547985479954800548015480254803548045480554806548075480854809548105481154812548135481454815548165481754818548195482054821548225482354824548255482654827548285482954830548315483254833548345483554836548375483854839548405484154842548435484454845548465484754848548495485054851548525485354854548555485654857548585485954860548615486254863548645486554866548675486854869548705487154872548735487454875548765487754878548795488054881548825488354884548855488654887548885488954890548915489254893548945489554896548975489854899549005490154902549035490454905549065490754908549095491054911549125491354914549155491654917549185491954920549215492254923549245492554926549275492854929549305493154932549335493454935549365493754938549395494054941549425494354944549455494654947549485494954950549515495254953549545495554956549575495854959549605496154962549635496454965549665496754968549695497054971549725497354974549755497654977549785497954980549815498254983549845498554986549875498854989549905499154992549935499454995549965499754998549995500055001550025500355004550055500655007550085500955010550115501255013550145501555016550175501855019550205502155022550235502455025550265502755028550295503055031550325503355034550355503655037550385503955040550415504255043550445504555046550475504855049550505505155052550535505455055550565505755058550595506055061550625506355064550655506655067550685506955070550715507255073550745507555076550775507855079550805508155082550835508455085550865508755088550895509055091550925509355094550955509655097550985509955100551015510255103551045510555106551075510855109551105511155112551135511455115551165511755118551195512055121551225512355124551255512655127551285512955130551315513255133551345513555136551375513855139551405514155142551435514455145551465514755148551495515055151551525515355154551555515655157551585515955160551615516255163551645516555166551675516855169551705517155172551735517455175551765517755178551795518055181551825518355184551855518655187551885518955190551915519255193551945519555196551975519855199552005520155202552035520455205552065520755208552095521055211552125521355214552155521655217552185521955220552215522255223552245522555226552275522855229552305523155232552335523455235552365523755238552395524055241552425524355244552455524655247552485524955250552515525255253552545525555256552575525855259552605526155262552635526455265552665526755268552695527055271552725527355274552755527655277552785527955280552815528255283552845528555286552875528855289552905529155292552935529455295552965529755298552995530055301553025530355304553055530655307553085530955310553115531255313553145531555316553175531855319553205532155322553235532455325553265532755328553295533055331553325533355334553355533655337553385533955340553415534255343553445534555346553475534855349553505535155352553535535455355553565535755358553595536055361553625536355364553655536655367553685536955370553715537255373553745537555376553775537855379553805538155382553835538455385553865538755388553895539055391553925539355394553955539655397553985539955400554015540255403554045540555406554075540855409554105541155412554135541455415554165541755418554195542055421554225542355424554255542655427554285542955430554315543255433554345543555436554375543855439554405544155442554435544455445554465544755448554495545055451554525545355454554555545655457554585545955460554615546255463554645546555466554675546855469554705547155472554735547455475554765547755478554795548055481554825548355484554855548655487554885548955490554915549255493554945549555496554975549855499555005550155502555035550455505555065550755508555095551055511555125551355514555155551655517555185551955520555215552255523555245552555526555275552855529555305553155532555335553455535555365553755538555395554055541555425554355544555455554655547555485554955550555515555255553555545555555556555575555855559555605556155562555635556455565555665556755568555695557055571555725557355574555755557655577555785557955580555815558255583555845558555586555875558855589555905559155592555935559455595555965559755598555995560055601556025560355604556055560655607556085560955610556115561255613556145561555616556175561855619556205562155622556235562455625556265562755628556295563055631556325563355634556355563655637556385563955640556415564255643556445564555646556475564855649556505565155652556535565455655556565565755658556595566055661556625566355664556655566655667556685566955670556715567255673556745567555676556775567855679556805568155682556835568455685556865568755688556895569055691556925569355694556955569655697556985569955700557015570255703557045570555706557075570855709557105571155712557135571455715557165571755718557195572055721557225572355724557255572655727557285572955730557315573255733557345573555736557375573855739557405574155742557435574455745557465574755748557495575055751557525575355754557555575655757557585575955760557615576255763557645576555766557675576855769557705577155772557735577455775557765577755778557795578055781557825578355784557855578655787557885578955790557915579255793557945579555796557975579855799558005580155802558035580455805558065580755808558095581055811558125581355814558155581655817558185581955820558215582255823558245582555826558275582855829558305583155832558335583455835558365583755838558395584055841558425584355844558455584655847558485584955850558515585255853558545585555856558575585855859558605586155862558635586455865558665586755868558695587055871558725587355874558755587655877558785587955880558815588255883558845588555886558875588855889558905589155892558935589455895558965589755898558995590055901559025590355904559055590655907559085590955910559115591255913559145591555916559175591855919559205592155922559235592455925559265592755928559295593055931559325593355934559355593655937559385593955940559415594255943559445594555946559475594855949559505595155952559535595455955559565595755958559595596055961559625596355964559655596655967559685596955970559715597255973559745597555976559775597855979559805598155982559835598455985559865598755988559895599055991559925599355994559955599655997559985599956000560015600256003560045600556006560075600856009560105601156012560135601456015560165601756018560195602056021560225602356024560255602656027560285602956030560315603256033560345603556036560375603856039560405604156042560435604456045560465604756048560495605056051560525605356054560555605656057560585605956060560615606256063560645606556066560675606856069560705607156072560735607456075560765607756078560795608056081560825608356084560855608656087560885608956090560915609256093560945609556096560975609856099561005610156102561035610456105561065610756108561095611056111561125611356114561155611656117561185611956120561215612256123561245612556126561275612856129561305613156132561335613456135561365613756138561395614056141561425614356144561455614656147561485614956150561515615256153561545615556156561575615856159561605616156162561635616456165561665616756168561695617056171561725617356174561755617656177561785617956180561815618256183561845618556186561875618856189561905619156192561935619456195561965619756198561995620056201562025620356204562055620656207562085620956210562115621256213562145621556216562175621856219562205622156222562235622456225562265622756228562295623056231562325623356234562355623656237562385623956240562415624256243562445624556246562475624856249562505625156252562535625456255562565625756258562595626056261562625626356264562655626656267562685626956270562715627256273562745627556276562775627856279562805628156282562835628456285562865628756288562895629056291562925629356294562955629656297562985629956300563015630256303563045630556306563075630856309563105631156312563135631456315563165631756318563195632056321563225632356324563255632656327563285632956330563315633256333563345633556336563375633856339563405634156342563435634456345563465634756348563495635056351563525635356354563555635656357563585635956360563615636256363563645636556366563675636856369563705637156372563735637456375563765637756378563795638056381563825638356384563855638656387563885638956390563915639256393563945639556396563975639856399564005640156402564035640456405564065640756408564095641056411564125641356414564155641656417564185641956420564215642256423564245642556426564275642856429564305643156432564335643456435564365643756438564395644056441564425644356444564455644656447564485644956450564515645256453564545645556456564575645856459564605646156462564635646456465564665646756468564695647056471564725647356474564755647656477564785647956480564815648256483564845648556486564875648856489564905649156492564935649456495564965649756498564995650056501565025650356504565055650656507565085650956510565115651256513565145651556516565175651856519565205652156522565235652456525565265652756528565295653056531565325653356534565355653656537565385653956540565415654256543565445654556546565475654856549565505655156552565535655456555565565655756558565595656056561565625656356564565655656656567565685656956570565715657256573565745657556576565775657856579565805658156582565835658456585565865658756588565895659056591565925659356594565955659656597565985659956600566015660256603566045660556606566075660856609566105661156612566135661456615566165661756618566195662056621566225662356624566255662656627566285662956630566315663256633566345663556636566375663856639566405664156642566435664456645566465664756648566495665056651566525665356654566555665656657566585665956660566615666256663566645666556666566675666856669566705667156672566735667456675566765667756678566795668056681566825668356684566855668656687566885668956690566915669256693566945669556696566975669856699567005670156702567035670456705567065670756708567095671056711567125671356714567155671656717567185671956720567215672256723567245672556726567275672856729567305673156732567335673456735567365673756738567395674056741567425674356744567455674656747567485674956750567515675256753567545675556756567575675856759567605676156762567635676456765567665676756768567695677056771567725677356774567755677656777567785677956780567815678256783567845678556786567875678856789567905679156792567935679456795567965679756798567995680056801568025680356804568055680656807568085680956810568115681256813568145681556816568175681856819568205682156822568235682456825568265682756828568295683056831568325683356834568355683656837568385683956840568415684256843568445684556846568475684856849568505685156852568535685456855568565685756858568595686056861568625686356864568655686656867568685686956870568715687256873568745687556876568775687856879568805688156882568835688456885568865688756888568895689056891568925689356894568955689656897568985689956900569015690256903569045690556906569075690856909569105691156912569135691456915569165691756918569195692056921569225692356924569255692656927569285692956930569315693256933569345693556936569375693856939569405694156942569435694456945569465694756948569495695056951569525695356954569555695656957569585695956960569615696256963569645696556966569675696856969569705697156972569735697456975569765697756978569795698056981569825698356984569855698656987569885698956990569915699256993569945699556996569975699856999570005700157002570035700457005570065700757008570095701057011570125701357014570155701657017570185701957020570215702257023570245702557026570275702857029570305703157032570335703457035570365703757038570395704057041570425704357044570455704657047570485704957050570515705257053570545705557056570575705857059570605706157062570635706457065570665706757068570695707057071570725707357074570755707657077570785707957080570815708257083570845708557086570875708857089570905709157092570935709457095570965709757098570995710057101571025710357104571055710657107571085710957110571115711257113571145711557116571175711857119571205712157122571235712457125571265712757128571295713057131571325713357134571355713657137571385713957140571415714257143571445714557146571475714857149571505715157152571535715457155571565715757158571595716057161571625716357164571655716657167571685716957170571715717257173571745717557176571775717857179571805718157182571835718457185571865718757188571895719057191571925719357194571955719657197571985719957200572015720257203572045720557206572075720857209572105721157212572135721457215572165721757218572195722057221572225722357224572255722657227572285722957230572315723257233572345723557236572375723857239572405724157242572435724457245572465724757248572495725057251572525725357254572555725657257572585725957260572615726257263572645726557266572675726857269572705727157272572735727457275572765727757278572795728057281572825728357284572855728657287572885728957290572915729257293572945729557296572975729857299573005730157302573035730457305573065730757308573095731057311573125731357314573155731657317573185731957320573215732257323573245732557326573275732857329573305733157332573335733457335573365733757338573395734057341573425734357344573455734657347573485734957350573515735257353573545735557356573575735857359573605736157362573635736457365573665736757368573695737057371573725737357374573755737657377573785737957380573815738257383573845738557386573875738857389573905739157392573935739457395573965739757398573995740057401574025740357404574055740657407574085740957410574115741257413574145741557416574175741857419574205742157422574235742457425574265742757428574295743057431574325743357434574355743657437574385743957440574415744257443574445744557446574475744857449574505745157452574535745457455574565745757458574595746057461574625746357464574655746657467574685746957470574715747257473574745747557476574775747857479574805748157482574835748457485574865748757488574895749057491574925749357494574955749657497574985749957500575015750257503575045750557506575075750857509575105751157512575135751457515575165751757518575195752057521575225752357524575255752657527575285752957530575315753257533575345753557536575375753857539575405754157542575435754457545575465754757548575495755057551575525755357554575555755657557575585755957560575615756257563575645756557566575675756857569575705757157572575735757457575575765757757578575795758057581575825758357584575855758657587575885758957590575915759257593575945759557596575975759857599576005760157602576035760457605576065760757608576095761057611576125761357614576155761657617576185761957620576215762257623576245762557626576275762857629576305763157632576335763457635576365763757638576395764057641576425764357644576455764657647576485764957650576515765257653576545765557656576575765857659576605766157662576635766457665576665766757668576695767057671576725767357674576755767657677576785767957680576815768257683576845768557686576875768857689576905769157692576935769457695576965769757698576995770057701577025770357704577055770657707577085770957710577115771257713577145771557716577175771857719577205772157722577235772457725577265772757728577295773057731577325773357734577355773657737577385773957740577415774257743577445774557746577475774857749577505775157752577535775457755577565775757758577595776057761577625776357764577655776657767577685776957770577715777257773577745777557776577775777857779577805778157782577835778457785577865778757788577895779057791577925779357794577955779657797577985779957800578015780257803578045780557806578075780857809578105781157812578135781457815578165781757818578195782057821578225782357824578255782657827578285782957830578315783257833578345783557836578375783857839578405784157842578435784457845578465784757848578495785057851578525785357854578555785657857578585785957860578615786257863578645786557866578675786857869578705787157872578735787457875578765787757878578795788057881578825788357884578855788657887578885788957890578915789257893578945789557896578975789857899579005790157902579035790457905579065790757908579095791057911579125791357914579155791657917579185791957920579215792257923579245792557926579275792857929579305793157932579335793457935579365793757938579395794057941579425794357944579455794657947579485794957950579515795257953579545795557956579575795857959579605796157962579635796457965579665796757968579695797057971579725797357974579755797657977579785797957980579815798257983579845798557986579875798857989579905799157992579935799457995579965799757998579995800058001580025800358004580055800658007580085800958010580115801258013580145801558016580175801858019580205802158022580235802458025580265802758028580295803058031580325803358034580355803658037580385803958040580415804258043580445804558046580475804858049580505805158052580535805458055580565805758058580595806058061580625806358064580655806658067580685806958070580715807258073580745807558076580775807858079580805808158082580835808458085580865808758088580895809058091580925809358094580955809658097580985809958100581015810258103581045810558106581075810858109581105811158112581135811458115581165811758118581195812058121581225812358124581255812658127581285812958130581315813258133581345813558136581375813858139581405814158142581435814458145581465814758148581495815058151581525815358154581555815658157581585815958160581615816258163581645816558166581675816858169581705817158172581735817458175581765817758178581795818058181581825818358184581855818658187581885818958190581915819258193581945819558196581975819858199582005820158202582035820458205582065820758208582095821058211582125821358214582155821658217582185821958220582215822258223582245822558226582275822858229582305823158232582335823458235582365823758238582395824058241582425824358244582455824658247582485824958250582515825258253582545825558256582575825858259582605826158262582635826458265582665826758268582695827058271582725827358274582755827658277582785827958280582815828258283582845828558286582875828858289582905829158292582935829458295582965829758298582995830058301583025830358304583055830658307583085830958310583115831258313583145831558316583175831858319583205832158322583235832458325583265832758328583295833058331583325833358334583355833658337583385833958340583415834258343583445834558346583475834858349583505835158352583535835458355583565835758358583595836058361583625836358364583655836658367583685836958370583715837258373583745837558376583775837858379583805838158382583835838458385583865838758388583895839058391583925839358394583955839658397583985839958400584015840258403584045840558406584075840858409584105841158412584135841458415584165841758418584195842058421584225842358424584255842658427584285842958430584315843258433584345843558436584375843858439584405844158442584435844458445584465844758448584495845058451584525845358454584555845658457584585845958460584615846258463584645846558466584675846858469584705847158472584735847458475584765847758478584795848058481584825848358484584855848658487584885848958490584915849258493584945849558496584975849858499585005850158502585035850458505585065850758508585095851058511585125851358514585155851658517585185851958520585215852258523585245852558526585275852858529585305853158532585335853458535585365853758538585395854058541585425854358544585455854658547585485854958550585515855258553585545855558556585575855858559585605856158562585635856458565585665856758568585695857058571585725857358574585755857658577585785857958580585815858258583585845858558586585875858858589585905859158592585935859458595585965859758598585995860058601586025860358604586055860658607586085860958610586115861258613586145861558616586175861858619586205862158622586235862458625586265862758628586295863058631586325863358634586355863658637586385863958640586415864258643586445864558646586475864858649586505865158652586535865458655586565865758658586595866058661586625866358664586655866658667586685866958670586715867258673586745867558676586775867858679586805868158682586835868458685586865868758688586895869058691586925869358694586955869658697586985869958700587015870258703587045870558706587075870858709587105871158712587135871458715587165871758718587195872058721587225872358724587255872658727587285872958730587315873258733587345873558736587375873858739587405874158742587435874458745587465874758748587495875058751587525875358754587555875658757587585875958760587615876258763587645876558766587675876858769587705877158772587735877458775587765877758778587795878058781587825878358784587855878658787587885878958790587915879258793587945879558796587975879858799588005880158802588035880458805588065880758808588095881058811588125881358814588155881658817588185881958820588215882258823588245882558826588275882858829588305883158832588335883458835588365883758838588395884058841588425884358844588455884658847588485884958850588515885258853588545885558856588575885858859588605886158862588635886458865588665886758868588695887058871588725887358874588755887658877588785887958880588815888258883588845888558886588875888858889588905889158892588935889458895588965889758898588995890058901589025890358904589055890658907589085890958910589115891258913589145891558916589175891858919589205892158922589235892458925589265892758928589295893058931589325893358934589355893658937589385893958940589415894258943589445894558946589475894858949589505895158952589535895458955589565895758958589595896058961589625896358964589655896658967589685896958970589715897258973589745897558976589775897858979589805898158982589835898458985589865898758988589895899058991589925899358994589955899658997589985899959000590015900259003590045900559006590075900859009590105901159012590135901459015590165901759018590195902059021590225902359024590255902659027590285902959030590315903259033590345903559036590375903859039590405904159042590435904459045590465904759048590495905059051590525905359054590555905659057590585905959060590615906259063590645906559066590675906859069590705907159072590735907459075590765907759078590795908059081590825908359084590855908659087590885908959090590915909259093590945909559096590975909859099591005910159102591035910459105591065910759108591095911059111591125911359114591155911659117591185911959120591215912259123591245912559126591275912859129591305913159132591335913459135591365913759138591395914059141591425914359144591455914659147591485914959150591515915259153591545915559156591575915859159591605916159162591635916459165591665916759168591695917059171591725917359174591755917659177591785917959180591815918259183591845918559186591875918859189591905919159192591935919459195591965919759198591995920059201592025920359204592055920659207592085920959210592115921259213592145921559216592175921859219592205922159222592235922459225592265922759228592295923059231592325923359234592355923659237592385923959240592415924259243592445924559246592475924859249592505925159252592535925459255592565925759258592595926059261592625926359264592655926659267592685926959270592715927259273592745927559276592775927859279592805928159282592835928459285592865928759288592895929059291592925929359294592955929659297592985929959300593015930259303593045930559306593075930859309593105931159312593135931459315593165931759318593195932059321593225932359324593255932659327593285932959330593315933259333593345933559336593375933859339593405934159342593435934459345593465934759348593495935059351593525935359354593555935659357593585935959360593615936259363593645936559366593675936859369593705937159372593735937459375593765937759378593795938059381593825938359384593855938659387593885938959390593915939259393593945939559396593975939859399594005940159402594035940459405594065940759408594095941059411594125941359414594155941659417594185941959420594215942259423594245942559426594275942859429594305943159432594335943459435594365943759438594395944059441594425944359444594455944659447594485944959450594515945259453594545945559456594575945859459594605946159462594635946459465594665946759468594695947059471594725947359474594755947659477594785947959480594815948259483594845948559486594875948859489594905949159492594935949459495594965949759498594995950059501595025950359504595055950659507595085950959510595115951259513595145951559516595175951859519595205952159522595235952459525595265952759528595295953059531595325953359534595355953659537595385953959540595415954259543595445954559546595475954859549595505955159552595535955459555595565955759558595595956059561595625956359564595655956659567595685956959570595715957259573595745957559576595775957859579595805958159582595835958459585595865958759588595895959059591595925959359594595955959659597595985959959600596015960259603596045960559606596075960859609596105961159612596135961459615596165961759618596195962059621596225962359624596255962659627596285962959630596315963259633596345963559636596375963859639596405964159642596435964459645596465964759648596495965059651596525965359654596555965659657596585965959660596615966259663596645966559666596675966859669596705967159672596735967459675596765967759678596795968059681596825968359684596855968659687596885968959690596915969259693596945969559696596975969859699597005970159702597035970459705597065970759708597095971059711597125971359714597155971659717597185971959720597215972259723597245972559726597275972859729597305973159732597335973459735597365973759738597395974059741597425974359744597455974659747597485974959750597515975259753597545975559756597575975859759597605976159762597635976459765597665976759768597695977059771597725977359774597755977659777597785977959780597815978259783597845978559786597875978859789597905979159792597935979459795597965979759798597995980059801598025980359804598055980659807598085980959810598115981259813598145981559816598175981859819598205982159822598235982459825598265982759828598295983059831598325983359834598355983659837598385983959840598415984259843598445984559846598475984859849598505985159852598535985459855598565985759858598595986059861598625986359864598655986659867598685986959870598715987259873598745987559876598775987859879598805988159882598835988459885598865988759888598895989059891598925989359894598955989659897598985989959900599015990259903599045990559906599075990859909599105991159912599135991459915599165991759918599195992059921599225992359924599255992659927599285992959930599315993259933599345993559936599375993859939599405994159942599435994459945599465994759948599495995059951599525995359954599555995659957599585995959960599615996259963599645996559966599675996859969599705997159972599735997459975599765997759978599795998059981599825998359984599855998659987599885998959990599915999259993599945999559996599975999859999600006000160002600036000460005600066000760008600096001060011600126001360014600156001660017600186001960020600216002260023600246002560026600276002860029600306003160032600336003460035600366003760038600396004060041600426004360044600456004660047600486004960050600516005260053600546005560056600576005860059600606006160062600636006460065600666006760068600696007060071600726007360074600756007660077600786007960080600816008260083600846008560086600876008860089600906009160092600936009460095600966009760098600996010060101601026010360104601056010660107601086010960110601116011260113601146011560116601176011860119601206012160122601236012460125601266012760128601296013060131601326013360134601356013660137601386013960140601416014260143601446014560146601476014860149601506015160152601536015460155601566015760158601596016060161601626016360164601656016660167601686016960170601716017260173601746017560176601776017860179601806018160182601836018460185601866018760188601896019060191601926019360194601956019660197601986019960200602016020260203602046020560206602076020860209602106021160212602136021460215602166021760218602196022060221602226022360224602256022660227602286022960230602316023260233602346023560236602376023860239602406024160242602436024460245602466024760248602496025060251602526025360254602556025660257602586025960260602616026260263602646026560266602676026860269602706027160272602736027460275602766027760278602796028060281602826028360284602856028660287602886028960290602916029260293602946029560296602976029860299603006030160302603036030460305603066030760308603096031060311603126031360314603156031660317603186031960320603216032260323603246032560326603276032860329603306033160332603336033460335603366033760338603396034060341603426034360344603456034660347603486034960350603516035260353603546035560356603576035860359603606036160362603636036460365603666036760368603696037060371603726037360374603756037660377603786037960380603816038260383603846038560386603876038860389603906039160392603936039460395603966039760398603996040060401604026040360404604056040660407604086040960410604116041260413604146041560416604176041860419604206042160422604236042460425604266042760428604296043060431604326043360434604356043660437604386043960440604416044260443604446044560446604476044860449604506045160452604536045460455604566045760458604596046060461604626046360464604656046660467604686046960470604716047260473604746047560476604776047860479604806048160482604836048460485604866048760488604896049060491604926049360494604956049660497604986049960500605016050260503605046050560506605076050860509605106051160512605136051460515605166051760518605196052060521605226052360524605256052660527605286052960530605316053260533605346053560536605376053860539605406054160542605436054460545605466054760548605496055060551605526055360554605556055660557605586055960560605616056260563605646056560566605676056860569605706057160572605736057460575605766057760578605796058060581605826058360584605856058660587605886058960590605916059260593605946059560596605976059860599606006060160602606036060460605606066060760608606096061060611606126061360614606156061660617606186061960620606216062260623606246062560626606276062860629606306063160632606336063460635606366063760638606396064060641606426064360644606456064660647606486064960650606516065260653606546065560656606576065860659606606066160662606636066460665606666066760668606696067060671606726067360674606756067660677606786067960680606816068260683606846068560686606876068860689606906069160692606936069460695606966069760698606996070060701607026070360704607056070660707607086070960710607116071260713607146071560716607176071860719607206072160722607236072460725607266072760728607296073060731607326073360734607356073660737607386073960740607416074260743607446074560746607476074860749607506075160752607536075460755607566075760758607596076060761607626076360764607656076660767607686076960770607716077260773607746077560776607776077860779607806078160782607836078460785607866078760788607896079060791607926079360794607956079660797607986079960800608016080260803608046080560806608076080860809608106081160812608136081460815608166081760818608196082060821608226082360824608256082660827608286082960830608316083260833608346083560836608376083860839608406084160842608436084460845608466084760848608496085060851608526085360854608556085660857608586085960860608616086260863608646086560866608676086860869608706087160872608736087460875608766087760878608796088060881608826088360884608856088660887608886088960890608916089260893608946089560896608976089860899609006090160902609036090460905609066090760908609096091060911609126091360914609156091660917609186091960920609216092260923609246092560926609276092860929609306093160932609336093460935609366093760938609396094060941609426094360944609456094660947609486094960950609516095260953609546095560956609576095860959609606096160962609636096460965609666096760968609696097060971609726097360974609756097660977609786097960980609816098260983609846098560986609876098860989609906099160992609936099460995609966099760998609996100061001610026100361004610056100661007610086100961010610116101261013610146101561016610176101861019610206102161022610236102461025610266102761028610296103061031610326103361034610356103661037610386103961040610416104261043610446104561046610476104861049610506105161052610536105461055610566105761058610596106061061610626106361064610656106661067610686106961070610716107261073610746107561076610776107861079610806108161082610836108461085610866108761088610896109061091610926109361094610956109661097610986109961100611016110261103611046110561106611076110861109611106111161112611136111461115611166111761118611196112061121611226112361124611256112661127611286112961130611316113261133611346113561136611376113861139611406114161142611436114461145611466114761148611496115061151611526115361154611556115661157611586115961160611616116261163611646116561166611676116861169611706117161172611736117461175611766117761178611796118061181611826118361184611856118661187611886118961190611916119261193611946119561196611976119861199612006120161202612036120461205612066120761208612096121061211612126121361214612156121661217612186121961220612216122261223612246122561226612276122861229612306123161232612336123461235612366123761238612396124061241612426124361244612456124661247612486124961250612516125261253612546125561256612576125861259612606126161262612636126461265612666126761268612696127061271612726127361274612756127661277612786127961280612816128261283612846128561286612876128861289612906129161292612936129461295612966129761298612996130061301613026130361304613056130661307613086130961310613116131261313613146131561316613176131861319613206132161322613236132461325613266132761328613296133061331613326133361334613356133661337613386133961340613416134261343613446134561346613476134861349613506135161352613536135461355613566135761358613596136061361613626136361364613656136661367613686136961370613716137261373613746137561376613776137861379613806138161382613836138461385613866138761388613896139061391613926139361394613956139661397613986139961400614016140261403614046140561406614076140861409614106141161412614136141461415614166141761418614196142061421614226142361424614256142661427614286142961430614316143261433614346143561436614376143861439614406144161442614436144461445614466144761448614496145061451614526145361454614556145661457614586145961460614616146261463614646146561466614676146861469614706147161472614736147461475614766147761478614796148061481614826148361484614856148661487614886148961490614916149261493614946149561496614976149861499615006150161502615036150461505615066150761508615096151061511615126151361514615156151661517615186151961520615216152261523615246152561526615276152861529615306153161532615336153461535615366153761538615396154061541615426154361544615456154661547615486154961550615516155261553615546155561556615576155861559615606156161562615636156461565615666156761568615696157061571615726157361574615756157661577615786157961580615816158261583615846158561586615876158861589615906159161592615936159461595615966159761598615996160061601616026160361604616056160661607616086160961610616116161261613616146161561616616176161861619616206162161622616236162461625616266162761628616296163061631616326163361634616356163661637616386163961640616416164261643616446164561646616476164861649616506165161652616536165461655616566165761658616596166061661616626166361664616656166661667616686166961670616716167261673616746167561676616776167861679616806168161682616836168461685616866168761688616896169061691616926169361694616956169661697616986169961700617016170261703617046170561706617076170861709617106171161712617136171461715617166171761718617196172061721617226172361724617256172661727617286172961730617316173261733617346173561736617376173861739617406174161742617436174461745617466174761748617496175061751617526175361754617556175661757617586175961760617616176261763617646176561766617676176861769617706177161772617736177461775617766177761778617796178061781617826178361784617856178661787617886178961790617916179261793617946179561796617976179861799618006180161802618036180461805618066180761808618096181061811618126181361814618156181661817618186181961820618216182261823618246182561826618276182861829618306183161832618336183461835618366183761838618396184061841618426184361844618456184661847618486184961850618516185261853618546185561856618576185861859618606186161862618636186461865618666186761868618696187061871618726187361874618756187661877618786187961880618816188261883618846188561886618876188861889618906189161892618936189461895618966189761898618996190061901619026190361904619056190661907619086190961910619116191261913619146191561916619176191861919619206192161922619236192461925619266192761928619296193061931619326193361934619356193661937619386193961940619416194261943619446194561946619476194861949619506195161952619536195461955619566195761958619596196061961619626196361964619656196661967619686196961970619716197261973619746197561976619776197861979619806198161982619836198461985619866198761988619896199061991619926199361994619956199661997619986199962000620016200262003620046200562006620076200862009620106201162012620136201462015620166201762018620196202062021620226202362024620256202662027620286202962030620316203262033620346203562036620376203862039620406204162042620436204462045620466204762048620496205062051620526205362054620556205662057620586205962060620616206262063620646206562066620676206862069620706207162072620736207462075620766207762078620796208062081620826208362084620856208662087620886208962090620916209262093620946209562096620976209862099621006210162102621036210462105621066210762108621096211062111621126211362114621156211662117621186211962120621216212262123621246212562126621276212862129621306213162132621336213462135621366213762138621396214062141621426214362144621456214662147621486214962150621516215262153621546215562156621576215862159621606216162162621636216462165621666216762168621696217062171621726217362174621756217662177621786217962180621816218262183621846218562186621876218862189621906219162192621936219462195621966219762198621996220062201622026220362204622056220662207622086220962210622116221262213622146221562216622176221862219622206222162222622236222462225622266222762228622296223062231622326223362234622356223662237622386223962240622416224262243622446224562246622476224862249622506225162252622536225462255622566225762258622596226062261622626226362264622656226662267622686226962270622716227262273622746227562276622776227862279622806228162282622836228462285622866228762288622896229062291622926229362294622956229662297622986229962300623016230262303623046230562306623076230862309623106231162312623136231462315623166231762318623196232062321623226232362324623256232662327623286232962330623316233262333623346233562336623376233862339623406234162342623436234462345623466234762348623496235062351623526235362354623556235662357623586235962360623616236262363623646236562366623676236862369623706237162372623736237462375623766237762378623796238062381623826238362384623856238662387623886238962390623916239262393623946239562396623976239862399624006240162402624036240462405624066240762408624096241062411624126241362414624156241662417624186241962420624216242262423624246242562426624276242862429624306243162432624336243462435624366243762438624396244062441624426244362444624456244662447624486244962450624516245262453624546245562456624576245862459624606246162462624636246462465624666246762468624696247062471624726247362474624756247662477624786247962480624816248262483624846248562486624876248862489624906249162492624936249462495624966249762498624996250062501625026250362504625056250662507625086250962510625116251262513625146251562516625176251862519625206252162522625236252462525625266252762528625296253062531625326253362534625356253662537625386253962540625416254262543625446254562546625476254862549625506255162552625536255462555625566255762558625596256062561625626256362564625656256662567625686256962570625716257262573625746257562576625776257862579625806258162582625836258462585625866258762588625896259062591625926259362594625956259662597625986259962600626016260262603626046260562606626076260862609626106261162612626136261462615626166261762618626196262062621626226262362624626256262662627626286262962630626316263262633626346263562636626376263862639626406264162642626436264462645626466264762648626496265062651626526265362654626556265662657626586265962660626616266262663626646266562666626676266862669626706267162672626736267462675626766267762678626796268062681626826268362684626856268662687626886268962690626916269262693626946269562696626976269862699627006270162702627036270462705627066270762708627096271062711627126271362714627156271662717627186271962720627216272262723627246272562726627276272862729627306273162732627336273462735627366273762738627396274062741627426274362744627456274662747627486274962750627516275262753627546275562756627576275862759627606276162762627636276462765627666276762768627696277062771627726277362774627756277662777627786277962780627816278262783627846278562786627876278862789627906279162792627936279462795627966279762798627996280062801628026280362804628056280662807628086280962810628116281262813628146281562816628176281862819628206282162822628236282462825628266282762828628296283062831628326283362834628356283662837628386283962840628416284262843628446284562846628476284862849628506285162852628536285462855628566285762858628596286062861628626286362864628656286662867628686286962870628716287262873628746287562876628776287862879628806288162882628836288462885628866288762888628896289062891628926289362894628956289662897628986289962900629016290262903629046290562906629076290862909629106291162912629136291462915629166291762918629196292062921629226292362924629256292662927629286292962930629316293262933629346293562936629376293862939629406294162942629436294462945629466294762948629496295062951629526295362954629556295662957629586295962960629616296262963629646296562966629676296862969629706297162972629736297462975629766297762978629796298062981629826298362984629856298662987629886298962990629916299262993629946299562996629976299862999630006300163002630036300463005630066300763008630096301063011630126301363014630156301663017630186301963020630216302263023630246302563026630276302863029630306303163032630336303463035630366303763038630396304063041630426304363044630456304663047630486304963050630516305263053630546305563056630576305863059630606306163062630636306463065630666306763068630696307063071630726307363074630756307663077630786307963080630816308263083630846308563086630876308863089630906309163092630936309463095630966309763098630996310063101631026310363104631056310663107631086310963110631116311263113631146311563116631176311863119631206312163122631236312463125631266312763128631296313063131631326313363134631356313663137631386313963140631416314263143631446314563146631476314863149631506315163152631536315463155631566315763158631596316063161631626316363164631656316663167631686316963170631716317263173631746317563176631776317863179631806318163182631836318463185631866318763188631896319063191631926319363194631956319663197631986319963200632016320263203632046320563206632076320863209632106321163212632136321463215632166321763218632196322063221632226322363224632256322663227632286322963230632316323263233632346323563236632376323863239632406324163242632436324463245632466324763248632496325063251632526325363254632556325663257632586325963260632616326263263632646326563266632676326863269632706327163272632736327463275632766327763278632796328063281632826328363284632856328663287632886328963290632916329263293632946329563296632976329863299633006330163302633036330463305633066330763308633096331063311633126331363314633156331663317633186331963320633216332263323633246332563326633276332863329633306333163332633336333463335633366333763338633396334063341633426334363344633456334663347633486334963350633516335263353633546335563356633576335863359633606336163362633636336463365633666336763368633696337063371633726337363374633756337663377633786337963380633816338263383633846338563386633876338863389633906339163392633936339463395633966339763398633996340063401634026340363404634056340663407634086340963410634116341263413634146341563416634176341863419634206342163422634236342463425634266342763428634296343063431634326343363434634356343663437634386343963440634416344263443634446344563446634476344863449634506345163452634536345463455634566345763458634596346063461634626346363464634656346663467634686346963470634716347263473634746347563476634776347863479634806348163482634836348463485634866348763488634896349063491634926349363494634956349663497634986349963500635016350263503635046350563506635076350863509635106351163512635136351463515635166351763518635196352063521635226352363524635256352663527635286352963530635316353263533635346353563536635376353863539635406354163542635436354463545635466354763548635496355063551635526355363554635556355663557635586355963560635616356263563635646356563566635676356863569635706357163572635736357463575635766357763578635796358063581635826358363584635856358663587635886358963590635916359263593635946359563596635976359863599636006360163602636036360463605636066360763608636096361063611636126361363614636156361663617636186361963620636216362263623636246362563626636276362863629636306363163632636336363463635636366363763638636396364063641636426364363644636456364663647636486364963650636516365263653636546365563656636576365863659636606366163662636636366463665636666366763668636696367063671636726367363674636756367663677636786367963680636816368263683636846368563686636876368863689636906369163692636936369463695636966369763698636996370063701637026370363704637056370663707637086370963710637116371263713637146371563716637176371863719637206372163722637236372463725637266372763728637296373063731637326373363734637356373663737637386373963740637416374263743637446374563746637476374863749637506375163752637536375463755637566375763758637596376063761637626376363764637656376663767637686376963770637716377263773637746377563776637776377863779637806378163782637836378463785637866378763788637896379063791637926379363794637956379663797637986379963800638016380263803638046380563806638076380863809638106381163812638136381463815638166381763818638196382063821638226382363824638256382663827638286382963830638316383263833638346383563836638376383863839638406384163842638436384463845638466384763848638496385063851638526385363854638556385663857638586385963860638616386263863638646386563866638676386863869638706387163872638736387463875638766387763878638796388063881638826388363884638856388663887638886388963890638916389263893638946389563896638976389863899639006390163902639036390463905639066390763908639096391063911639126391363914639156391663917639186391963920639216392263923639246392563926639276392863929639306393163932639336393463935639366393763938639396394063941639426394363944639456394663947639486394963950639516395263953639546395563956639576395863959639606396163962639636396463965639666396763968639696397063971639726397363974639756397663977639786397963980639816398263983639846398563986639876398863989639906399163992639936399463995639966399763998639996400064001640026400364004640056400664007640086400964010640116401264013640146401564016640176401864019640206402164022640236402464025640266402764028640296403064031640326403364034640356403664037640386403964040640416404264043640446404564046640476404864049640506405164052640536405464055640566405764058640596406064061640626406364064640656406664067640686406964070640716407264073640746407564076640776407864079640806408164082640836408464085640866408764088640896409064091640926409364094640956409664097640986409964100641016410264103641046410564106641076410864109641106411164112641136411464115641166411764118641196412064121641226412364124641256412664127641286412964130641316413264133641346413564136641376413864139641406414164142641436414464145641466414764148641496415064151641526415364154641556415664157641586415964160641616416264163641646416564166641676416864169641706417164172641736417464175641766417764178641796418064181641826418364184641856418664187641886418964190641916419264193641946419564196641976419864199642006420164202642036420464205642066420764208642096421064211642126421364214642156421664217642186421964220642216422264223642246422564226642276422864229642306423164232642336423464235642366423764238642396424064241642426424364244642456424664247642486424964250642516425264253642546425564256642576425864259642606426164262642636426464265642666426764268642696427064271642726427364274642756427664277642786427964280642816428264283642846428564286642876428864289642906429164292642936429464295642966429764298642996430064301643026430364304643056430664307643086430964310643116431264313643146431564316643176431864319643206432164322643236432464325643266432764328643296433064331643326433364334643356433664337643386433964340643416434264343643446434564346643476434864349643506435164352643536435464355643566435764358643596436064361643626436364364643656436664367643686436964370643716437264373643746437564376643776437864379643806438164382643836438464385643866438764388643896439064391643926439364394643956439664397643986439964400644016440264403644046440564406644076440864409644106441164412644136441464415644166441764418644196442064421644226442364424644256442664427644286442964430644316443264433644346443564436644376443864439644406444164442644436444464445644466444764448644496445064451644526445364454644556445664457644586445964460644616446264463644646446564466644676446864469644706447164472644736447464475644766447764478644796448064481644826448364484644856448664487644886448964490644916449264493644946449564496644976449864499645006450164502645036450464505645066450764508645096451064511645126451364514645156451664517645186451964520645216452264523645246452564526645276452864529645306453164532645336453464535645366453764538645396454064541645426454364544645456454664547645486454964550645516455264553645546455564556645576455864559645606456164562645636456464565645666456764568645696457064571645726457364574645756457664577645786457964580645816458264583645846458564586645876458864589645906459164592645936459464595645966459764598645996460064601646026460364604646056460664607646086460964610646116461264613646146461564616646176461864619646206462164622646236462464625646266462764628646296463064631646326463364634646356463664637646386463964640646416464264643646446464564646646476464864649646506465164652646536465464655646566465764658646596466064661646626466364664646656466664667646686466964670646716467264673646746467564676646776467864679646806468164682646836468464685646866468764688646896469064691646926469364694646956469664697646986469964700647016470264703647046470564706647076470864709647106471164712647136471464715647166471764718647196472064721647226472364724647256472664727647286472964730647316473264733647346473564736647376473864739647406474164742647436474464745647466474764748647496475064751647526475364754647556475664757647586475964760647616476264763647646476564766647676476864769647706477164772647736477464775647766477764778647796478064781647826478364784647856478664787647886478964790647916479264793647946479564796647976479864799648006480164802648036480464805648066480764808648096481064811648126481364814648156481664817648186481964820648216482264823648246482564826648276482864829648306483164832648336483464835648366483764838648396484064841648426484364844648456484664847648486484964850648516485264853648546485564856648576485864859648606486164862648636486464865648666486764868648696487064871648726487364874648756487664877648786487964880648816488264883648846488564886648876488864889648906489164892648936489464895648966489764898648996490064901649026490364904649056490664907649086490964910649116491264913649146491564916649176491864919649206492164922649236492464925649266492764928649296493064931649326493364934649356493664937649386493964940649416494264943649446494564946649476494864949649506495164952649536495464955649566495764958649596496064961649626496364964649656496664967649686496964970649716497264973649746497564976649776497864979649806498164982649836498464985649866498764988649896499064991649926499364994649956499664997649986499965000650016500265003650046500565006650076500865009650106501165012650136501465015650166501765018650196502065021650226502365024650256502665027650286502965030650316503265033650346503565036650376503865039650406504165042650436504465045650466504765048650496505065051650526505365054650556505665057650586505965060650616506265063650646506565066650676506865069650706507165072650736507465075650766507765078650796508065081650826508365084650856508665087650886508965090650916509265093650946509565096650976509865099651006510165102651036510465105651066510765108651096511065111651126511365114651156511665117651186511965120651216512265123651246512565126651276512865129651306513165132651336513465135651366513765138651396514065141651426514365144651456514665147651486514965150651516515265153651546515565156651576515865159651606516165162651636516465165651666516765168651696517065171651726517365174651756517665177651786517965180651816518265183651846518565186651876518865189651906519165192651936519465195651966519765198651996520065201652026520365204652056520665207652086520965210652116521265213652146521565216652176521865219652206522165222652236522465225652266522765228652296523065231652326523365234652356523665237652386523965240652416524265243652446524565246652476524865249652506525165252652536525465255652566525765258652596526065261652626526365264652656526665267652686526965270652716527265273652746527565276652776527865279652806528165282652836528465285652866528765288652896529065291652926529365294652956529665297652986529965300653016530265303653046530565306653076530865309653106531165312653136531465315653166531765318653196532065321653226532365324653256532665327653286532965330653316533265333653346533565336653376533865339653406534165342653436534465345653466534765348653496535065351653526535365354653556535665357653586535965360653616536265363653646536565366653676536865369653706537165372653736537465375653766537765378653796538065381653826538365384653856538665387653886538965390653916539265393653946539565396653976539865399654006540165402654036540465405654066540765408654096541065411654126541365414654156541665417654186541965420654216542265423654246542565426654276542865429654306543165432654336543465435654366543765438654396544065441654426544365444654456544665447654486544965450654516545265453654546545565456654576545865459654606546165462654636546465465654666546765468654696547065471654726547365474654756547665477654786547965480654816548265483654846548565486654876548865489654906549165492654936549465495654966549765498654996550065501655026550365504655056550665507655086550965510655116551265513655146551565516655176551865519655206552165522655236552465525655266552765528655296553065531655326553365534655356553665537655386553965540655416554265543655446554565546655476554865549655506555165552655536555465555655566555765558655596556065561655626556365564655656556665567655686556965570655716557265573655746557565576655776557865579655806558165582655836558465585655866558765588655896559065591655926559365594655956559665597655986559965600656016560265603656046560565606656076560865609656106561165612656136561465615656166561765618656196562065621656226562365624656256562665627656286562965630656316563265633656346563565636656376563865639656406564165642656436564465645656466564765648656496565065651656526565365654656556565665657656586565965660656616566265663656646566565666656676566865669656706567165672656736567465675656766567765678656796568065681656826568365684656856568665687656886568965690656916569265693656946569565696656976569865699657006570165702657036570465705657066570765708657096571065711657126571365714657156571665717657186571965720657216572265723657246572565726657276572865729657306573165732657336573465735657366573765738657396574065741657426574365744657456574665747657486574965750657516575265753657546575565756657576575865759657606576165762657636576465765657666576765768657696577065771657726577365774657756577665777657786577965780657816578265783657846578565786657876578865789657906579165792657936579465795657966579765798657996580065801658026580365804658056580665807658086580965810658116581265813658146581565816658176581865819658206582165822658236582465825658266582765828658296583065831658326583365834658356583665837658386583965840658416584265843658446584565846658476584865849658506585165852658536585465855658566585765858658596586065861658626586365864658656586665867658686586965870658716587265873658746587565876658776587865879658806588165882658836588465885658866588765888658896589065891658926589365894658956589665897658986589965900659016590265903659046590565906659076590865909659106591165912659136591465915659166591765918659196592065921659226592365924659256592665927659286592965930659316593265933659346593565936659376593865939659406594165942659436594465945659466594765948659496595065951659526595365954659556595665957659586595965960659616596265963659646596565966659676596865969659706597165972659736597465975659766597765978659796598065981659826598365984659856598665987659886598965990659916599265993659946599565996659976599865999660006600166002660036600466005660066600766008660096601066011660126601366014660156601666017660186601966020660216602266023660246602566026660276602866029660306603166032660336603466035660366603766038660396604066041660426604366044660456604666047660486604966050660516605266053660546605566056660576605866059660606606166062660636606466065660666606766068660696607066071660726607366074660756607666077660786607966080660816608266083660846608566086660876608866089660906609166092660936609466095660966609766098660996610066101661026610366104661056610666107661086610966110661116611266113661146611566116661176611866119661206612166122661236612466125661266612766128661296613066131661326613366134661356613666137661386613966140661416614266143661446614566146661476614866149661506615166152661536615466155661566615766158661596616066161661626616366164661656616666167661686616966170661716617266173661746617566176661776617866179661806618166182661836618466185661866618766188661896619066191661926619366194661956619666197661986619966200662016620266203662046620566206662076620866209662106621166212662136621466215662166621766218662196622066221662226622366224662256622666227662286622966230662316623266233662346623566236662376623866239662406624166242662436624466245662466624766248662496625066251662526625366254662556625666257662586625966260662616626266263662646626566266662676626866269662706627166272662736627466275662766627766278662796628066281662826628366284662856628666287662886628966290662916629266293662946629566296662976629866299663006630166302663036630466305663066630766308663096631066311663126631366314663156631666317663186631966320663216632266323663246632566326663276632866329663306633166332663336633466335663366633766338663396634066341663426634366344663456634666347663486634966350663516635266353663546635566356663576635866359663606636166362663636636466365663666636766368663696637066371663726637366374663756637666377663786637966380663816638266383663846638566386663876638866389663906639166392663936639466395663966639766398663996640066401664026640366404664056640666407664086640966410664116641266413664146641566416664176641866419664206642166422664236642466425664266642766428664296643066431664326643366434664356643666437664386643966440664416644266443664446644566446664476644866449664506645166452664536645466455664566645766458664596646066461664626646366464664656646666467664686646966470664716647266473664746647566476664776647866479664806648166482664836648466485664866648766488664896649066491664926649366494664956649666497664986649966500665016650266503665046650566506665076650866509665106651166512665136651466515665166651766518665196652066521665226652366524665256652666527665286652966530665316653266533665346653566536665376653866539665406654166542665436654466545665466654766548665496655066551665526655366554665556655666557665586655966560665616656266563665646656566566665676656866569665706657166572665736657466575665766657766578665796658066581665826658366584665856658666587665886658966590665916659266593665946659566596665976659866599666006660166602666036660466605666066660766608666096661066611666126661366614666156661666617666186661966620666216662266623666246662566626666276662866629666306663166632666336663466635666366663766638666396664066641666426664366644666456664666647666486664966650666516665266653666546665566656666576665866659666606666166662666636666466665666666666766668666696667066671666726667366674666756667666677666786667966680666816668266683666846668566686666876668866689666906669166692666936669466695666966669766698666996670066701667026670366704667056670666707667086670966710667116671266713667146671566716667176671866719667206672166722667236672466725667266672766728667296673066731667326673366734667356673666737667386673966740667416674266743667446674566746667476674866749667506675166752667536675466755667566675766758667596676066761667626676366764
  1. import * as fs$j from 'node:fs';
  2. import fs__default, { promises as promises$1 } from 'node:fs';
  3. import fsp, { lstat as lstat$3, readdir as readdir$5, readlink, realpath as realpath$2 } from 'node:fs/promises';
  4. import path$n, { win32 as win32$1, posix as posix$1, isAbsolute as isAbsolute$1, join as join$2, extname as extname$1, dirname as dirname$2, relative as relative$2, basename as basename$2 } from 'node:path';
  5. import { fileURLToPath, URL as URL$3, parse as parse$h, pathToFileURL } from 'node:url';
  6. import { promisify as promisify$4, format as format$2, inspect } from 'node:util';
  7. import { performance as performance$1 } from 'node:perf_hooks';
  8. import { createRequire as createRequire$1, builtinModules } from 'node:module';
  9. import require$$0$3 from 'tty';
  10. import require$$0$4, { win32, posix, isAbsolute, resolve as resolve$3, relative as relative$1, basename as basename$1, extname, dirname as dirname$1, join as join$1, sep as sep$1, normalize as normalize$1 } from 'path';
  11. import esbuild, { transform as transform$1, formatMessages, build as build$3 } from 'esbuild';
  12. import { CLIENT_ENTRY, OPTIMIZABLE_ENTRY_RE, wildcardHosts, loopbackHosts, FS_PREFIX, CLIENT_PUBLIC_PATH, ENV_PUBLIC_PATH, DEFAULT_ASSETS_INLINE_LIMIT, CSS_LANGS_RE, ESBUILD_MODULES_TARGET, SPECIAL_QUERY_RE, ENV_ENTRY, DEP_VERSION_RE, DEFAULT_MAIN_FIELDS, DEFAULT_EXTENSIONS, KNOWN_ASSET_TYPES, JS_TYPES_RE, METADATA_FILENAME, VITE_PACKAGE_DIR, DEFAULT_DEV_PORT, CLIENT_DIR, VERSION, DEFAULT_PREVIEW_PORT, DEFAULT_ASSETS_RE, DEFAULT_CONFIG_FILES } from '../constants.js';
  13. import * as require$$0$2 from 'fs';
  14. import require$$0__default, { lstatSync, readdir as readdir$4, readdirSync, readlinkSync, realpathSync as realpathSync$1, existsSync, readFileSync, statSync as statSync$1 } from 'fs';
  15. import { EventEmitter as EventEmitter$4 } from 'node:events';
  16. import Stream$1 from 'node:stream';
  17. import { StringDecoder } from 'node:string_decoder';
  18. import { exec, execSync } from 'node:child_process';
  19. import { createServer as createServer$3, STATUS_CODES, get as get$2 } from 'node:http';
  20. import { createServer as createServer$2, get as get$1 } from 'node:https';
  21. import require$$0$5 from 'util';
  22. import require$$4$1 from 'net';
  23. import require$$0$7 from 'events';
  24. import require$$0$9 from 'url';
  25. import require$$1 from 'http';
  26. import require$$0$6 from 'stream';
  27. import require$$2 from 'os';
  28. import require$$2$1 from 'child_process';
  29. import os$5 from 'node:os';
  30. import { createHash as createHash$2 } from 'node:crypto';
  31. import { promises } from 'node:dns';
  32. import require$$3$1 from 'crypto';
  33. import require$$0$8, { createRequire as createRequire$2 } from 'module';
  34. import assert$1 from 'node:assert';
  35. import v8 from 'node:v8';
  36. import { Worker as Worker$1 } from 'node:worker_threads';
  37. import { Buffer as Buffer$1 } from 'node:buffer';
  38. import { parseAstAsync, parseAst } from 'rollup/parseAst';
  39. import * as qs from 'querystring';
  40. import readline from 'node:readline';
  41. import zlib$1 from 'zlib';
  42. import require$$0$a from 'buffer';
  43. import require$$1$1 from 'https';
  44. import require$$4$2 from 'tls';
  45. import require$$4$3 from 'assert';
  46. import { gzip } from 'node:zlib';
  47. import { fileURLToPath as __cjs_fileURLToPath } from 'node:url';
  48. import { dirname as __cjs_dirname } from 'node:path';
  49. import { createRequire as __cjs_createRequire } from 'node:module';
  50. const __filename = __cjs_fileURLToPath(import.meta.url);
  51. const __dirname = __cjs_dirname(__filename);
  52. const require = __cjs_createRequire(import.meta.url);
  53. const __require = require;
  54. var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
  55. function getDefaultExportFromCjs (x) {
  56. return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
  57. }
  58. function getAugmentedNamespace(n) {
  59. if (n.__esModule) return n;
  60. var f = n.default;
  61. if (typeof f == "function") {
  62. var a = function a () {
  63. if (this instanceof a) {
  64. return Reflect.construct(f, arguments, this.constructor);
  65. }
  66. return f.apply(this, arguments);
  67. };
  68. a.prototype = f.prototype;
  69. } else a = {};
  70. Object.defineProperty(a, '__esModule', {value: true});
  71. Object.keys(n).forEach(function (k) {
  72. var d = Object.getOwnPropertyDescriptor(n, k);
  73. Object.defineProperty(a, k, d.get ? d : {
  74. enumerable: true,
  75. get: function () {
  76. return n[k];
  77. }
  78. });
  79. });
  80. return a;
  81. }
  82. function commonjsRequire(path) {
  83. throw new Error('Could not dynamically require "' + path + '". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.');
  84. }
  85. var picocolors = {exports: {}};
  86. let argv = process.argv || [],
  87. env$1 = process.env;
  88. let isColorSupported =
  89. !("NO_COLOR" in env$1 || argv.includes("--no-color")) &&
  90. ("FORCE_COLOR" in env$1 ||
  91. argv.includes("--color") ||
  92. process.platform === "win32" ||
  93. (commonjsRequire != null && require$$0$3.isatty(1) && env$1.TERM !== "dumb") ||
  94. "CI" in env$1);
  95. let formatter =
  96. (open, close, replace = open) =>
  97. input => {
  98. let string = "" + input;
  99. let index = string.indexOf(close, open.length);
  100. return ~index
  101. ? open + replaceClose(string, close, replace, index) + close
  102. : open + string + close
  103. };
  104. let replaceClose = (string, close, replace, index) => {
  105. let result = "";
  106. let cursor = 0;
  107. do {
  108. result += string.substring(cursor, index) + replace;
  109. cursor = index + close.length;
  110. index = string.indexOf(close, cursor);
  111. } while (~index)
  112. return result + string.substring(cursor)
  113. };
  114. let createColors = (enabled = isColorSupported) => {
  115. let init = enabled ? formatter : () => String;
  116. return {
  117. isColorSupported: enabled,
  118. reset: init("\x1b[0m", "\x1b[0m"),
  119. bold: init("\x1b[1m", "\x1b[22m", "\x1b[22m\x1b[1m"),
  120. dim: init("\x1b[2m", "\x1b[22m", "\x1b[22m\x1b[2m"),
  121. italic: init("\x1b[3m", "\x1b[23m"),
  122. underline: init("\x1b[4m", "\x1b[24m"),
  123. inverse: init("\x1b[7m", "\x1b[27m"),
  124. hidden: init("\x1b[8m", "\x1b[28m"),
  125. strikethrough: init("\x1b[9m", "\x1b[29m"),
  126. black: init("\x1b[30m", "\x1b[39m"),
  127. red: init("\x1b[31m", "\x1b[39m"),
  128. green: init("\x1b[32m", "\x1b[39m"),
  129. yellow: init("\x1b[33m", "\x1b[39m"),
  130. blue: init("\x1b[34m", "\x1b[39m"),
  131. magenta: init("\x1b[35m", "\x1b[39m"),
  132. cyan: init("\x1b[36m", "\x1b[39m"),
  133. white: init("\x1b[37m", "\x1b[39m"),
  134. gray: init("\x1b[90m", "\x1b[39m"),
  135. bgBlack: init("\x1b[40m", "\x1b[49m"),
  136. bgRed: init("\x1b[41m", "\x1b[49m"),
  137. bgGreen: init("\x1b[42m", "\x1b[49m"),
  138. bgYellow: init("\x1b[43m", "\x1b[49m"),
  139. bgBlue: init("\x1b[44m", "\x1b[49m"),
  140. bgMagenta: init("\x1b[45m", "\x1b[49m"),
  141. bgCyan: init("\x1b[46m", "\x1b[49m"),
  142. bgWhite: init("\x1b[47m", "\x1b[49m"),
  143. }
  144. };
  145. picocolors.exports = createColors();
  146. picocolors.exports.createColors = createColors;
  147. var picocolorsExports = picocolors.exports;
  148. var colors$1 = /*@__PURE__*/getDefaultExportFromCjs(picocolorsExports);
  149. function matches$1(pattern, importee) {
  150. if (pattern instanceof RegExp) {
  151. return pattern.test(importee);
  152. }
  153. if (importee.length < pattern.length) {
  154. return false;
  155. }
  156. if (importee === pattern) {
  157. return true;
  158. }
  159. // eslint-disable-next-line prefer-template
  160. return importee.startsWith(pattern + '/');
  161. }
  162. function getEntries({ entries, customResolver }) {
  163. if (!entries) {
  164. return [];
  165. }
  166. const resolverFunctionFromOptions = resolveCustomResolver(customResolver);
  167. if (Array.isArray(entries)) {
  168. return entries.map((entry) => {
  169. return {
  170. find: entry.find,
  171. replacement: entry.replacement,
  172. resolverFunction: resolveCustomResolver(entry.customResolver) || resolverFunctionFromOptions
  173. };
  174. });
  175. }
  176. return Object.entries(entries).map(([key, value]) => {
  177. return { find: key, replacement: value, resolverFunction: resolverFunctionFromOptions };
  178. });
  179. }
  180. function getHookFunction(hook) {
  181. if (typeof hook === 'function') {
  182. return hook;
  183. }
  184. if (hook && 'handler' in hook && typeof hook.handler === 'function') {
  185. return hook.handler;
  186. }
  187. return null;
  188. }
  189. function resolveCustomResolver(customResolver) {
  190. if (typeof customResolver === 'function') {
  191. return customResolver;
  192. }
  193. if (customResolver) {
  194. return getHookFunction(customResolver.resolveId);
  195. }
  196. return null;
  197. }
  198. function alias$1(options = {}) {
  199. const entries = getEntries(options);
  200. if (entries.length === 0) {
  201. return {
  202. name: 'alias',
  203. resolveId: () => null
  204. };
  205. }
  206. return {
  207. name: 'alias',
  208. async buildStart(inputOptions) {
  209. await Promise.all([...(Array.isArray(options.entries) ? options.entries : []), options].map(({ customResolver }) => { var _a; return customResolver && ((_a = getHookFunction(customResolver.buildStart)) === null || _a === void 0 ? void 0 : _a.call(this, inputOptions)); }));
  210. },
  211. resolveId(importee, importer, resolveOptions) {
  212. // First match is supposed to be the correct one
  213. const matchedEntry = entries.find((entry) => matches$1(entry.find, importee));
  214. if (!matchedEntry) {
  215. return null;
  216. }
  217. const updatedId = importee.replace(matchedEntry.find, matchedEntry.replacement);
  218. if (matchedEntry.resolverFunction) {
  219. return matchedEntry.resolverFunction.call(this, updatedId, importer, resolveOptions);
  220. }
  221. return this.resolve(updatedId, importer, Object.assign({ skipSelf: true }, resolveOptions)).then((resolved) => {
  222. if (resolved)
  223. return resolved;
  224. if (!require$$0$4.isAbsolute(updatedId)) {
  225. this.warn(`rewrote ${importee} to ${updatedId} but was not an abolute path and was not handled by other plugins. ` +
  226. `This will lead to duplicated modules for the same path. ` +
  227. `To avoid duplicating modules, you should resolve to an absolute path.`);
  228. }
  229. return { id: updatedId };
  230. });
  231. }
  232. };
  233. }
  234. const VALID_ID_PREFIX = `/@id/`;
  235. const NULL_BYTE_PLACEHOLDER = `__x00__`;
  236. let SOURCEMAPPING_URL = "sourceMa";
  237. SOURCEMAPPING_URL += "ppingURL";
  238. const VITE_RUNTIME_SOURCEMAPPING_SOURCE = "//# sourceMappingSource=vite-runtime";
  239. const isWindows$3 = typeof process !== "undefined" && process.platform === "win32";
  240. function wrapId$1(id) {
  241. return id.startsWith(VALID_ID_PREFIX) ? id : VALID_ID_PREFIX + id.replace("\0", NULL_BYTE_PLACEHOLDER);
  242. }
  243. function unwrapId$1(id) {
  244. return id.startsWith(VALID_ID_PREFIX) ? id.slice(VALID_ID_PREFIX.length).replace(NULL_BYTE_PLACEHOLDER, "\0") : id;
  245. }
  246. const windowsSlashRE = /\\/g;
  247. function slash$1(p) {
  248. return p.replace(windowsSlashRE, "/");
  249. }
  250. const postfixRE = /[?#].*$/;
  251. function cleanUrl(url) {
  252. return url.replace(postfixRE, "");
  253. }
  254. function withTrailingSlash(path) {
  255. if (path[path.length - 1] !== "/") {
  256. return `${path}/`;
  257. }
  258. return path;
  259. }
  260. const AsyncFunction = async function() {
  261. }.constructor;
  262. const asyncFunctionDeclarationPaddingLineCount = /* @__PURE__ */ (() => {
  263. const body = "/*code*/";
  264. const source = new AsyncFunction("a", "b", body).toString();
  265. return source.slice(0, source.indexOf(body)).split("\n").length - 1;
  266. })();
  267. // @ts-check
  268. /** @typedef { import('estree').BaseNode} BaseNode */
  269. /** @typedef {{
  270. skip: () => void;
  271. remove: () => void;
  272. replace: (node: BaseNode) => void;
  273. }} WalkerContext */
  274. let WalkerBase$1 = class WalkerBase {
  275. constructor() {
  276. /** @type {boolean} */
  277. this.should_skip = false;
  278. /** @type {boolean} */
  279. this.should_remove = false;
  280. /** @type {BaseNode | null} */
  281. this.replacement = null;
  282. /** @type {WalkerContext} */
  283. this.context = {
  284. skip: () => (this.should_skip = true),
  285. remove: () => (this.should_remove = true),
  286. replace: (node) => (this.replacement = node)
  287. };
  288. }
  289. /**
  290. *
  291. * @param {any} parent
  292. * @param {string} prop
  293. * @param {number} index
  294. * @param {BaseNode} node
  295. */
  296. replace(parent, prop, index, node) {
  297. if (parent) {
  298. if (index !== null) {
  299. parent[prop][index] = node;
  300. } else {
  301. parent[prop] = node;
  302. }
  303. }
  304. }
  305. /**
  306. *
  307. * @param {any} parent
  308. * @param {string} prop
  309. * @param {number} index
  310. */
  311. remove(parent, prop, index) {
  312. if (parent) {
  313. if (index !== null) {
  314. parent[prop].splice(index, 1);
  315. } else {
  316. delete parent[prop];
  317. }
  318. }
  319. }
  320. };
  321. // @ts-check
  322. /** @typedef { import('estree').BaseNode} BaseNode */
  323. /** @typedef { import('./walker.js').WalkerContext} WalkerContext */
  324. /** @typedef {(
  325. * this: WalkerContext,
  326. * node: BaseNode,
  327. * parent: BaseNode,
  328. * key: string,
  329. * index: number
  330. * ) => void} SyncHandler */
  331. let SyncWalker$1 = class SyncWalker extends WalkerBase$1 {
  332. /**
  333. *
  334. * @param {SyncHandler} enter
  335. * @param {SyncHandler} leave
  336. */
  337. constructor(enter, leave) {
  338. super();
  339. /** @type {SyncHandler} */
  340. this.enter = enter;
  341. /** @type {SyncHandler} */
  342. this.leave = leave;
  343. }
  344. /**
  345. *
  346. * @param {BaseNode} node
  347. * @param {BaseNode} parent
  348. * @param {string} [prop]
  349. * @param {number} [index]
  350. * @returns {BaseNode}
  351. */
  352. visit(node, parent, prop, index) {
  353. if (node) {
  354. if (this.enter) {
  355. const _should_skip = this.should_skip;
  356. const _should_remove = this.should_remove;
  357. const _replacement = this.replacement;
  358. this.should_skip = false;
  359. this.should_remove = false;
  360. this.replacement = null;
  361. this.enter.call(this.context, node, parent, prop, index);
  362. if (this.replacement) {
  363. node = this.replacement;
  364. this.replace(parent, prop, index, node);
  365. }
  366. if (this.should_remove) {
  367. this.remove(parent, prop, index);
  368. }
  369. const skipped = this.should_skip;
  370. const removed = this.should_remove;
  371. this.should_skip = _should_skip;
  372. this.should_remove = _should_remove;
  373. this.replacement = _replacement;
  374. if (skipped) return node;
  375. if (removed) return null;
  376. }
  377. for (const key in node) {
  378. const value = node[key];
  379. if (typeof value !== "object") {
  380. continue;
  381. } else if (Array.isArray(value)) {
  382. for (let i = 0; i < value.length; i += 1) {
  383. if (value[i] !== null && typeof value[i].type === 'string') {
  384. if (!this.visit(value[i], node, key, i)) {
  385. // removed
  386. i--;
  387. }
  388. }
  389. }
  390. } else if (value !== null && typeof value.type === "string") {
  391. this.visit(value, node, key, null);
  392. }
  393. }
  394. if (this.leave) {
  395. const _replacement = this.replacement;
  396. const _should_remove = this.should_remove;
  397. this.replacement = null;
  398. this.should_remove = false;
  399. this.leave.call(this.context, node, parent, prop, index);
  400. if (this.replacement) {
  401. node = this.replacement;
  402. this.replace(parent, prop, index, node);
  403. }
  404. if (this.should_remove) {
  405. this.remove(parent, prop, index);
  406. }
  407. const removed = this.should_remove;
  408. this.replacement = _replacement;
  409. this.should_remove = _should_remove;
  410. if (removed) return null;
  411. }
  412. }
  413. return node;
  414. }
  415. };
  416. // @ts-check
  417. /** @typedef { import('estree').BaseNode} BaseNode */
  418. /** @typedef { import('./sync.js').SyncHandler} SyncHandler */
  419. /** @typedef { import('./async.js').AsyncHandler} AsyncHandler */
  420. /**
  421. *
  422. * @param {BaseNode} ast
  423. * @param {{
  424. * enter?: SyncHandler
  425. * leave?: SyncHandler
  426. * }} walker
  427. * @returns {BaseNode}
  428. */
  429. function walk$3(ast, { enter, leave }) {
  430. const instance = new SyncWalker$1(enter, leave);
  431. return instance.visit(ast, null);
  432. }
  433. var utils$k = {};
  434. const path$m = require$$0$4;
  435. const WIN_SLASH = '\\\\/';
  436. const WIN_NO_SLASH = `[^${WIN_SLASH}]`;
  437. /**
  438. * Posix glob regex
  439. */
  440. const DOT_LITERAL = '\\.';
  441. const PLUS_LITERAL = '\\+';
  442. const QMARK_LITERAL = '\\?';
  443. const SLASH_LITERAL = '\\/';
  444. const ONE_CHAR = '(?=.)';
  445. const QMARK = '[^/]';
  446. const END_ANCHOR = `(?:${SLASH_LITERAL}|$)`;
  447. const START_ANCHOR = `(?:^|${SLASH_LITERAL})`;
  448. const DOTS_SLASH = `${DOT_LITERAL}{1,2}${END_ANCHOR}`;
  449. const NO_DOT = `(?!${DOT_LITERAL})`;
  450. const NO_DOTS = `(?!${START_ANCHOR}${DOTS_SLASH})`;
  451. const NO_DOT_SLASH = `(?!${DOT_LITERAL}{0,1}${END_ANCHOR})`;
  452. const NO_DOTS_SLASH = `(?!${DOTS_SLASH})`;
  453. const QMARK_NO_DOT = `[^.${SLASH_LITERAL}]`;
  454. const STAR$1 = `${QMARK}*?`;
  455. const POSIX_CHARS = {
  456. DOT_LITERAL,
  457. PLUS_LITERAL,
  458. QMARK_LITERAL,
  459. SLASH_LITERAL,
  460. ONE_CHAR,
  461. QMARK,
  462. END_ANCHOR,
  463. DOTS_SLASH,
  464. NO_DOT,
  465. NO_DOTS,
  466. NO_DOT_SLASH,
  467. NO_DOTS_SLASH,
  468. QMARK_NO_DOT,
  469. STAR: STAR$1,
  470. START_ANCHOR
  471. };
  472. /**
  473. * Windows glob regex
  474. */
  475. const WINDOWS_CHARS = {
  476. ...POSIX_CHARS,
  477. SLASH_LITERAL: `[${WIN_SLASH}]`,
  478. QMARK: WIN_NO_SLASH,
  479. STAR: `${WIN_NO_SLASH}*?`,
  480. DOTS_SLASH: `${DOT_LITERAL}{1,2}(?:[${WIN_SLASH}]|$)`,
  481. NO_DOT: `(?!${DOT_LITERAL})`,
  482. NO_DOTS: `(?!(?:^|[${WIN_SLASH}])${DOT_LITERAL}{1,2}(?:[${WIN_SLASH}]|$))`,
  483. NO_DOT_SLASH: `(?!${DOT_LITERAL}{0,1}(?:[${WIN_SLASH}]|$))`,
  484. NO_DOTS_SLASH: `(?!${DOT_LITERAL}{1,2}(?:[${WIN_SLASH}]|$))`,
  485. QMARK_NO_DOT: `[^.${WIN_SLASH}]`,
  486. START_ANCHOR: `(?:^|[${WIN_SLASH}])`,
  487. END_ANCHOR: `(?:[${WIN_SLASH}]|$)`
  488. };
  489. /**
  490. * POSIX Bracket Regex
  491. */
  492. const POSIX_REGEX_SOURCE$1 = {
  493. alnum: 'a-zA-Z0-9',
  494. alpha: 'a-zA-Z',
  495. ascii: '\\x00-\\x7F',
  496. blank: ' \\t',
  497. cntrl: '\\x00-\\x1F\\x7F',
  498. digit: '0-9',
  499. graph: '\\x21-\\x7E',
  500. lower: 'a-z',
  501. print: '\\x20-\\x7E ',
  502. punct: '\\-!"#$%&\'()\\*+,./:;<=>?@[\\]^_`{|}~',
  503. space: ' \\t\\r\\n\\v\\f',
  504. upper: 'A-Z',
  505. word: 'A-Za-z0-9_',
  506. xdigit: 'A-Fa-f0-9'
  507. };
  508. var constants$6 = {
  509. MAX_LENGTH: 1024 * 64,
  510. POSIX_REGEX_SOURCE: POSIX_REGEX_SOURCE$1,
  511. // regular expressions
  512. REGEX_BACKSLASH: /\\(?![*+?^${}(|)[\]])/g,
  513. REGEX_NON_SPECIAL_CHARS: /^[^@![\].,$*+?^{}()|\\/]+/,
  514. REGEX_SPECIAL_CHARS: /[-*+?.^${}(|)[\]]/,
  515. REGEX_SPECIAL_CHARS_BACKREF: /(\\?)((\W)(\3*))/g,
  516. REGEX_SPECIAL_CHARS_GLOBAL: /([-*+?.^${}(|)[\]])/g,
  517. REGEX_REMOVE_BACKSLASH: /(?:\[.*?[^\\]\]|\\(?=.))/g,
  518. // Replace globs with equivalent patterns to reduce parsing time.
  519. REPLACEMENTS: {
  520. '***': '*',
  521. '**/**': '**',
  522. '**/**/**': '**'
  523. },
  524. // Digits
  525. CHAR_0: 48, /* 0 */
  526. CHAR_9: 57, /* 9 */
  527. // Alphabet chars.
  528. CHAR_UPPERCASE_A: 65, /* A */
  529. CHAR_LOWERCASE_A: 97, /* a */
  530. CHAR_UPPERCASE_Z: 90, /* Z */
  531. CHAR_LOWERCASE_Z: 122, /* z */
  532. CHAR_LEFT_PARENTHESES: 40, /* ( */
  533. CHAR_RIGHT_PARENTHESES: 41, /* ) */
  534. CHAR_ASTERISK: 42, /* * */
  535. // Non-alphabetic chars.
  536. CHAR_AMPERSAND: 38, /* & */
  537. CHAR_AT: 64, /* @ */
  538. CHAR_BACKWARD_SLASH: 92, /* \ */
  539. CHAR_CARRIAGE_RETURN: 13, /* \r */
  540. CHAR_CIRCUMFLEX_ACCENT: 94, /* ^ */
  541. CHAR_COLON: 58, /* : */
  542. CHAR_COMMA: 44, /* , */
  543. CHAR_DOT: 46, /* . */
  544. CHAR_DOUBLE_QUOTE: 34, /* " */
  545. CHAR_EQUAL: 61, /* = */
  546. CHAR_EXCLAMATION_MARK: 33, /* ! */
  547. CHAR_FORM_FEED: 12, /* \f */
  548. CHAR_FORWARD_SLASH: 47, /* / */
  549. CHAR_GRAVE_ACCENT: 96, /* ` */
  550. CHAR_HASH: 35, /* # */
  551. CHAR_HYPHEN_MINUS: 45, /* - */
  552. CHAR_LEFT_ANGLE_BRACKET: 60, /* < */
  553. CHAR_LEFT_CURLY_BRACE: 123, /* { */
  554. CHAR_LEFT_SQUARE_BRACKET: 91, /* [ */
  555. CHAR_LINE_FEED: 10, /* \n */
  556. CHAR_NO_BREAK_SPACE: 160, /* \u00A0 */
  557. CHAR_PERCENT: 37, /* % */
  558. CHAR_PLUS: 43, /* + */
  559. CHAR_QUESTION_MARK: 63, /* ? */
  560. CHAR_RIGHT_ANGLE_BRACKET: 62, /* > */
  561. CHAR_RIGHT_CURLY_BRACE: 125, /* } */
  562. CHAR_RIGHT_SQUARE_BRACKET: 93, /* ] */
  563. CHAR_SEMICOLON: 59, /* ; */
  564. CHAR_SINGLE_QUOTE: 39, /* ' */
  565. CHAR_SPACE: 32, /* */
  566. CHAR_TAB: 9, /* \t */
  567. CHAR_UNDERSCORE: 95, /* _ */
  568. CHAR_VERTICAL_LINE: 124, /* | */
  569. CHAR_ZERO_WIDTH_NOBREAK_SPACE: 65279, /* \uFEFF */
  570. SEP: path$m.sep,
  571. /**
  572. * Create EXTGLOB_CHARS
  573. */
  574. extglobChars(chars) {
  575. return {
  576. '!': { type: 'negate', open: '(?:(?!(?:', close: `))${chars.STAR})` },
  577. '?': { type: 'qmark', open: '(?:', close: ')?' },
  578. '+': { type: 'plus', open: '(?:', close: ')+' },
  579. '*': { type: 'star', open: '(?:', close: ')*' },
  580. '@': { type: 'at', open: '(?:', close: ')' }
  581. };
  582. },
  583. /**
  584. * Create GLOB_CHARS
  585. */
  586. globChars(win32) {
  587. return win32 === true ? WINDOWS_CHARS : POSIX_CHARS;
  588. }
  589. };
  590. (function (exports) {
  591. const path = require$$0$4;
  592. const win32 = process.platform === 'win32';
  593. const {
  594. REGEX_BACKSLASH,
  595. REGEX_REMOVE_BACKSLASH,
  596. REGEX_SPECIAL_CHARS,
  597. REGEX_SPECIAL_CHARS_GLOBAL
  598. } = constants$6;
  599. exports.isObject = val => val !== null && typeof val === 'object' && !Array.isArray(val);
  600. exports.hasRegexChars = str => REGEX_SPECIAL_CHARS.test(str);
  601. exports.isRegexChar = str => str.length === 1 && exports.hasRegexChars(str);
  602. exports.escapeRegex = str => str.replace(REGEX_SPECIAL_CHARS_GLOBAL, '\\$1');
  603. exports.toPosixSlashes = str => str.replace(REGEX_BACKSLASH, '/');
  604. exports.removeBackslashes = str => {
  605. return str.replace(REGEX_REMOVE_BACKSLASH, match => {
  606. return match === '\\' ? '' : match;
  607. });
  608. };
  609. exports.supportsLookbehinds = () => {
  610. const segs = process.version.slice(1).split('.').map(Number);
  611. if (segs.length === 3 && segs[0] >= 9 || (segs[0] === 8 && segs[1] >= 10)) {
  612. return true;
  613. }
  614. return false;
  615. };
  616. exports.isWindows = options => {
  617. if (options && typeof options.windows === 'boolean') {
  618. return options.windows;
  619. }
  620. return win32 === true || path.sep === '\\';
  621. };
  622. exports.escapeLast = (input, char, lastIdx) => {
  623. const idx = input.lastIndexOf(char, lastIdx);
  624. if (idx === -1) return input;
  625. if (input[idx - 1] === '\\') return exports.escapeLast(input, char, idx - 1);
  626. return `${input.slice(0, idx)}\\${input.slice(idx)}`;
  627. };
  628. exports.removePrefix = (input, state = {}) => {
  629. let output = input;
  630. if (output.startsWith('./')) {
  631. output = output.slice(2);
  632. state.prefix = './';
  633. }
  634. return output;
  635. };
  636. exports.wrapOutput = (input, state = {}, options = {}) => {
  637. const prepend = options.contains ? '' : '^';
  638. const append = options.contains ? '' : '$';
  639. let output = `${prepend}(?:${input})${append}`;
  640. if (state.negated === true) {
  641. output = `(?:^(?!${output}).*$)`;
  642. }
  643. return output;
  644. };
  645. } (utils$k));
  646. const utils$j = utils$k;
  647. const {
  648. CHAR_ASTERISK, /* * */
  649. CHAR_AT, /* @ */
  650. CHAR_BACKWARD_SLASH, /* \ */
  651. CHAR_COMMA: CHAR_COMMA$1, /* , */
  652. CHAR_DOT: CHAR_DOT$1, /* . */
  653. CHAR_EXCLAMATION_MARK, /* ! */
  654. CHAR_FORWARD_SLASH, /* / */
  655. CHAR_LEFT_CURLY_BRACE: CHAR_LEFT_CURLY_BRACE$1, /* { */
  656. CHAR_LEFT_PARENTHESES: CHAR_LEFT_PARENTHESES$1, /* ( */
  657. CHAR_LEFT_SQUARE_BRACKET: CHAR_LEFT_SQUARE_BRACKET$1, /* [ */
  658. CHAR_PLUS, /* + */
  659. CHAR_QUESTION_MARK, /* ? */
  660. CHAR_RIGHT_CURLY_BRACE: CHAR_RIGHT_CURLY_BRACE$1, /* } */
  661. CHAR_RIGHT_PARENTHESES: CHAR_RIGHT_PARENTHESES$1, /* ) */
  662. CHAR_RIGHT_SQUARE_BRACKET: CHAR_RIGHT_SQUARE_BRACKET$1 /* ] */
  663. } = constants$6;
  664. const isPathSeparator = code => {
  665. return code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH;
  666. };
  667. const depth = token => {
  668. if (token.isPrefix !== true) {
  669. token.depth = token.isGlobstar ? Infinity : 1;
  670. }
  671. };
  672. /**
  673. * Quickly scans a glob pattern and returns an object with a handful of
  674. * useful properties, like `isGlob`, `path` (the leading non-glob, if it exists),
  675. * `glob` (the actual pattern), `negated` (true if the path starts with `!` but not
  676. * with `!(`) and `negatedExtglob` (true if the path starts with `!(`).
  677. *
  678. * ```js
  679. * const pm = require('picomatch');
  680. * console.log(pm.scan('foo/bar/*.js'));
  681. * { isGlob: true, input: 'foo/bar/*.js', base: 'foo/bar', glob: '*.js' }
  682. * ```
  683. * @param {String} `str`
  684. * @param {Object} `options`
  685. * @return {Object} Returns an object with tokens and regex source string.
  686. * @api public
  687. */
  688. const scan$2 = (input, options) => {
  689. const opts = options || {};
  690. const length = input.length - 1;
  691. const scanToEnd = opts.parts === true || opts.scanToEnd === true;
  692. const slashes = [];
  693. const tokens = [];
  694. const parts = [];
  695. let str = input;
  696. let index = -1;
  697. let start = 0;
  698. let lastIndex = 0;
  699. let isBrace = false;
  700. let isBracket = false;
  701. let isGlob = false;
  702. let isExtglob = false;
  703. let isGlobstar = false;
  704. let braceEscaped = false;
  705. let backslashes = false;
  706. let negated = false;
  707. let negatedExtglob = false;
  708. let finished = false;
  709. let braces = 0;
  710. let prev;
  711. let code;
  712. let token = { value: '', depth: 0, isGlob: false };
  713. const eos = () => index >= length;
  714. const peek = () => str.charCodeAt(index + 1);
  715. const advance = () => {
  716. prev = code;
  717. return str.charCodeAt(++index);
  718. };
  719. while (index < length) {
  720. code = advance();
  721. let next;
  722. if (code === CHAR_BACKWARD_SLASH) {
  723. backslashes = token.backslashes = true;
  724. code = advance();
  725. if (code === CHAR_LEFT_CURLY_BRACE$1) {
  726. braceEscaped = true;
  727. }
  728. continue;
  729. }
  730. if (braceEscaped === true || code === CHAR_LEFT_CURLY_BRACE$1) {
  731. braces++;
  732. while (eos() !== true && (code = advance())) {
  733. if (code === CHAR_BACKWARD_SLASH) {
  734. backslashes = token.backslashes = true;
  735. advance();
  736. continue;
  737. }
  738. if (code === CHAR_LEFT_CURLY_BRACE$1) {
  739. braces++;
  740. continue;
  741. }
  742. if (braceEscaped !== true && code === CHAR_DOT$1 && (code = advance()) === CHAR_DOT$1) {
  743. isBrace = token.isBrace = true;
  744. isGlob = token.isGlob = true;
  745. finished = true;
  746. if (scanToEnd === true) {
  747. continue;
  748. }
  749. break;
  750. }
  751. if (braceEscaped !== true && code === CHAR_COMMA$1) {
  752. isBrace = token.isBrace = true;
  753. isGlob = token.isGlob = true;
  754. finished = true;
  755. if (scanToEnd === true) {
  756. continue;
  757. }
  758. break;
  759. }
  760. if (code === CHAR_RIGHT_CURLY_BRACE$1) {
  761. braces--;
  762. if (braces === 0) {
  763. braceEscaped = false;
  764. isBrace = token.isBrace = true;
  765. finished = true;
  766. break;
  767. }
  768. }
  769. }
  770. if (scanToEnd === true) {
  771. continue;
  772. }
  773. break;
  774. }
  775. if (code === CHAR_FORWARD_SLASH) {
  776. slashes.push(index);
  777. tokens.push(token);
  778. token = { value: '', depth: 0, isGlob: false };
  779. if (finished === true) continue;
  780. if (prev === CHAR_DOT$1 && index === (start + 1)) {
  781. start += 2;
  782. continue;
  783. }
  784. lastIndex = index + 1;
  785. continue;
  786. }
  787. if (opts.noext !== true) {
  788. const isExtglobChar = code === CHAR_PLUS
  789. || code === CHAR_AT
  790. || code === CHAR_ASTERISK
  791. || code === CHAR_QUESTION_MARK
  792. || code === CHAR_EXCLAMATION_MARK;
  793. if (isExtglobChar === true && peek() === CHAR_LEFT_PARENTHESES$1) {
  794. isGlob = token.isGlob = true;
  795. isExtglob = token.isExtglob = true;
  796. finished = true;
  797. if (code === CHAR_EXCLAMATION_MARK && index === start) {
  798. negatedExtglob = true;
  799. }
  800. if (scanToEnd === true) {
  801. while (eos() !== true && (code = advance())) {
  802. if (code === CHAR_BACKWARD_SLASH) {
  803. backslashes = token.backslashes = true;
  804. code = advance();
  805. continue;
  806. }
  807. if (code === CHAR_RIGHT_PARENTHESES$1) {
  808. isGlob = token.isGlob = true;
  809. finished = true;
  810. break;
  811. }
  812. }
  813. continue;
  814. }
  815. break;
  816. }
  817. }
  818. if (code === CHAR_ASTERISK) {
  819. if (prev === CHAR_ASTERISK) isGlobstar = token.isGlobstar = true;
  820. isGlob = token.isGlob = true;
  821. finished = true;
  822. if (scanToEnd === true) {
  823. continue;
  824. }
  825. break;
  826. }
  827. if (code === CHAR_QUESTION_MARK) {
  828. isGlob = token.isGlob = true;
  829. finished = true;
  830. if (scanToEnd === true) {
  831. continue;
  832. }
  833. break;
  834. }
  835. if (code === CHAR_LEFT_SQUARE_BRACKET$1) {
  836. while (eos() !== true && (next = advance())) {
  837. if (next === CHAR_BACKWARD_SLASH) {
  838. backslashes = token.backslashes = true;
  839. advance();
  840. continue;
  841. }
  842. if (next === CHAR_RIGHT_SQUARE_BRACKET$1) {
  843. isBracket = token.isBracket = true;
  844. isGlob = token.isGlob = true;
  845. finished = true;
  846. break;
  847. }
  848. }
  849. if (scanToEnd === true) {
  850. continue;
  851. }
  852. break;
  853. }
  854. if (opts.nonegate !== true && code === CHAR_EXCLAMATION_MARK && index === start) {
  855. negated = token.negated = true;
  856. start++;
  857. continue;
  858. }
  859. if (opts.noparen !== true && code === CHAR_LEFT_PARENTHESES$1) {
  860. isGlob = token.isGlob = true;
  861. if (scanToEnd === true) {
  862. while (eos() !== true && (code = advance())) {
  863. if (code === CHAR_LEFT_PARENTHESES$1) {
  864. backslashes = token.backslashes = true;
  865. code = advance();
  866. continue;
  867. }
  868. if (code === CHAR_RIGHT_PARENTHESES$1) {
  869. finished = true;
  870. break;
  871. }
  872. }
  873. continue;
  874. }
  875. break;
  876. }
  877. if (isGlob === true) {
  878. finished = true;
  879. if (scanToEnd === true) {
  880. continue;
  881. }
  882. break;
  883. }
  884. }
  885. if (opts.noext === true) {
  886. isExtglob = false;
  887. isGlob = false;
  888. }
  889. let base = str;
  890. let prefix = '';
  891. let glob = '';
  892. if (start > 0) {
  893. prefix = str.slice(0, start);
  894. str = str.slice(start);
  895. lastIndex -= start;
  896. }
  897. if (base && isGlob === true && lastIndex > 0) {
  898. base = str.slice(0, lastIndex);
  899. glob = str.slice(lastIndex);
  900. } else if (isGlob === true) {
  901. base = '';
  902. glob = str;
  903. } else {
  904. base = str;
  905. }
  906. if (base && base !== '' && base !== '/' && base !== str) {
  907. if (isPathSeparator(base.charCodeAt(base.length - 1))) {
  908. base = base.slice(0, -1);
  909. }
  910. }
  911. if (opts.unescape === true) {
  912. if (glob) glob = utils$j.removeBackslashes(glob);
  913. if (base && backslashes === true) {
  914. base = utils$j.removeBackslashes(base);
  915. }
  916. }
  917. const state = {
  918. prefix,
  919. input,
  920. start,
  921. base,
  922. glob,
  923. isBrace,
  924. isBracket,
  925. isGlob,
  926. isExtglob,
  927. isGlobstar,
  928. negated,
  929. negatedExtglob
  930. };
  931. if (opts.tokens === true) {
  932. state.maxDepth = 0;
  933. if (!isPathSeparator(code)) {
  934. tokens.push(token);
  935. }
  936. state.tokens = tokens;
  937. }
  938. if (opts.parts === true || opts.tokens === true) {
  939. let prevIndex;
  940. for (let idx = 0; idx < slashes.length; idx++) {
  941. const n = prevIndex ? prevIndex + 1 : start;
  942. const i = slashes[idx];
  943. const value = input.slice(n, i);
  944. if (opts.tokens) {
  945. if (idx === 0 && start !== 0) {
  946. tokens[idx].isPrefix = true;
  947. tokens[idx].value = prefix;
  948. } else {
  949. tokens[idx].value = value;
  950. }
  951. depth(tokens[idx]);
  952. state.maxDepth += tokens[idx].depth;
  953. }
  954. if (idx !== 0 || value !== '') {
  955. parts.push(value);
  956. }
  957. prevIndex = i;
  958. }
  959. if (prevIndex && prevIndex + 1 < input.length) {
  960. const value = input.slice(prevIndex + 1);
  961. parts.push(value);
  962. if (opts.tokens) {
  963. tokens[tokens.length - 1].value = value;
  964. depth(tokens[tokens.length - 1]);
  965. state.maxDepth += tokens[tokens.length - 1].depth;
  966. }
  967. }
  968. state.slashes = slashes;
  969. state.parts = parts;
  970. }
  971. return state;
  972. };
  973. var scan_1 = scan$2;
  974. const constants$5 = constants$6;
  975. const utils$i = utils$k;
  976. /**
  977. * Constants
  978. */
  979. const {
  980. MAX_LENGTH: MAX_LENGTH$1,
  981. POSIX_REGEX_SOURCE,
  982. REGEX_NON_SPECIAL_CHARS,
  983. REGEX_SPECIAL_CHARS_BACKREF,
  984. REPLACEMENTS
  985. } = constants$5;
  986. /**
  987. * Helpers
  988. */
  989. const expandRange = (args, options) => {
  990. if (typeof options.expandRange === 'function') {
  991. return options.expandRange(...args, options);
  992. }
  993. args.sort();
  994. const value = `[${args.join('-')}]`;
  995. return value;
  996. };
  997. /**
  998. * Create the message for a syntax error
  999. */
  1000. const syntaxError = (type, char) => {
  1001. return `Missing ${type}: "${char}" - use "\\\\${char}" to match literal characters`;
  1002. };
  1003. /**
  1004. * Parse the given input string.
  1005. * @param {String} input
  1006. * @param {Object} options
  1007. * @return {Object}
  1008. */
  1009. const parse$g = (input, options) => {
  1010. if (typeof input !== 'string') {
  1011. throw new TypeError('Expected a string');
  1012. }
  1013. input = REPLACEMENTS[input] || input;
  1014. const opts = { ...options };
  1015. const max = typeof opts.maxLength === 'number' ? Math.min(MAX_LENGTH$1, opts.maxLength) : MAX_LENGTH$1;
  1016. let len = input.length;
  1017. if (len > max) {
  1018. throw new SyntaxError(`Input length: ${len}, exceeds maximum allowed length: ${max}`);
  1019. }
  1020. const bos = { type: 'bos', value: '', output: opts.prepend || '' };
  1021. const tokens = [bos];
  1022. const capture = opts.capture ? '' : '?:';
  1023. const win32 = utils$i.isWindows(options);
  1024. // create constants based on platform, for windows or posix
  1025. const PLATFORM_CHARS = constants$5.globChars(win32);
  1026. const EXTGLOB_CHARS = constants$5.extglobChars(PLATFORM_CHARS);
  1027. const {
  1028. DOT_LITERAL,
  1029. PLUS_LITERAL,
  1030. SLASH_LITERAL,
  1031. ONE_CHAR,
  1032. DOTS_SLASH,
  1033. NO_DOT,
  1034. NO_DOT_SLASH,
  1035. NO_DOTS_SLASH,
  1036. QMARK,
  1037. QMARK_NO_DOT,
  1038. STAR,
  1039. START_ANCHOR
  1040. } = PLATFORM_CHARS;
  1041. const globstar = opts => {
  1042. return `(${capture}(?:(?!${START_ANCHOR}${opts.dot ? DOTS_SLASH : DOT_LITERAL}).)*?)`;
  1043. };
  1044. const nodot = opts.dot ? '' : NO_DOT;
  1045. const qmarkNoDot = opts.dot ? QMARK : QMARK_NO_DOT;
  1046. let star = opts.bash === true ? globstar(opts) : STAR;
  1047. if (opts.capture) {
  1048. star = `(${star})`;
  1049. }
  1050. // minimatch options support
  1051. if (typeof opts.noext === 'boolean') {
  1052. opts.noextglob = opts.noext;
  1053. }
  1054. const state = {
  1055. input,
  1056. index: -1,
  1057. start: 0,
  1058. dot: opts.dot === true,
  1059. consumed: '',
  1060. output: '',
  1061. prefix: '',
  1062. backtrack: false,
  1063. negated: false,
  1064. brackets: 0,
  1065. braces: 0,
  1066. parens: 0,
  1067. quotes: 0,
  1068. globstar: false,
  1069. tokens
  1070. };
  1071. input = utils$i.removePrefix(input, state);
  1072. len = input.length;
  1073. const extglobs = [];
  1074. const braces = [];
  1075. const stack = [];
  1076. let prev = bos;
  1077. let value;
  1078. /**
  1079. * Tokenizing helpers
  1080. */
  1081. const eos = () => state.index === len - 1;
  1082. const peek = state.peek = (n = 1) => input[state.index + n];
  1083. const advance = state.advance = () => input[++state.index] || '';
  1084. const remaining = () => input.slice(state.index + 1);
  1085. const consume = (value = '', num = 0) => {
  1086. state.consumed += value;
  1087. state.index += num;
  1088. };
  1089. const append = token => {
  1090. state.output += token.output != null ? token.output : token.value;
  1091. consume(token.value);
  1092. };
  1093. const negate = () => {
  1094. let count = 1;
  1095. while (peek() === '!' && (peek(2) !== '(' || peek(3) === '?')) {
  1096. advance();
  1097. state.start++;
  1098. count++;
  1099. }
  1100. if (count % 2 === 0) {
  1101. return false;
  1102. }
  1103. state.negated = true;
  1104. state.start++;
  1105. return true;
  1106. };
  1107. const increment = type => {
  1108. state[type]++;
  1109. stack.push(type);
  1110. };
  1111. const decrement = type => {
  1112. state[type]--;
  1113. stack.pop();
  1114. };
  1115. /**
  1116. * Push tokens onto the tokens array. This helper speeds up
  1117. * tokenizing by 1) helping us avoid backtracking as much as possible,
  1118. * and 2) helping us avoid creating extra tokens when consecutive
  1119. * characters are plain text. This improves performance and simplifies
  1120. * lookbehinds.
  1121. */
  1122. const push = tok => {
  1123. if (prev.type === 'globstar') {
  1124. const isBrace = state.braces > 0 && (tok.type === 'comma' || tok.type === 'brace');
  1125. const isExtglob = tok.extglob === true || (extglobs.length && (tok.type === 'pipe' || tok.type === 'paren'));
  1126. if (tok.type !== 'slash' && tok.type !== 'paren' && !isBrace && !isExtglob) {
  1127. state.output = state.output.slice(0, -prev.output.length);
  1128. prev.type = 'star';
  1129. prev.value = '*';
  1130. prev.output = star;
  1131. state.output += prev.output;
  1132. }
  1133. }
  1134. if (extglobs.length && tok.type !== 'paren') {
  1135. extglobs[extglobs.length - 1].inner += tok.value;
  1136. }
  1137. if (tok.value || tok.output) append(tok);
  1138. if (prev && prev.type === 'text' && tok.type === 'text') {
  1139. prev.value += tok.value;
  1140. prev.output = (prev.output || '') + tok.value;
  1141. return;
  1142. }
  1143. tok.prev = prev;
  1144. tokens.push(tok);
  1145. prev = tok;
  1146. };
  1147. const extglobOpen = (type, value) => {
  1148. const token = { ...EXTGLOB_CHARS[value], conditions: 1, inner: '' };
  1149. token.prev = prev;
  1150. token.parens = state.parens;
  1151. token.output = state.output;
  1152. const output = (opts.capture ? '(' : '') + token.open;
  1153. increment('parens');
  1154. push({ type, value, output: state.output ? '' : ONE_CHAR });
  1155. push({ type: 'paren', extglob: true, value: advance(), output });
  1156. extglobs.push(token);
  1157. };
  1158. const extglobClose = token => {
  1159. let output = token.close + (opts.capture ? ')' : '');
  1160. let rest;
  1161. if (token.type === 'negate') {
  1162. let extglobStar = star;
  1163. if (token.inner && token.inner.length > 1 && token.inner.includes('/')) {
  1164. extglobStar = globstar(opts);
  1165. }
  1166. if (extglobStar !== star || eos() || /^\)+$/.test(remaining())) {
  1167. output = token.close = `)$))${extglobStar}`;
  1168. }
  1169. if (token.inner.includes('*') && (rest = remaining()) && /^\.[^\\/.]+$/.test(rest)) {
  1170. // Any non-magical string (`.ts`) or even nested expression (`.{ts,tsx}`) can follow after the closing parenthesis.
  1171. // In this case, we need to parse the string and use it in the output of the original pattern.
  1172. // Suitable patterns: `/!(*.d).ts`, `/!(*.d).{ts,tsx}`, `**/!(*-dbg).@(js)`.
  1173. //
  1174. // Disabling the `fastpaths` option due to a problem with parsing strings as `.ts` in the pattern like `**/!(*.d).ts`.
  1175. const expression = parse$g(rest, { ...options, fastpaths: false }).output;
  1176. output = token.close = `)${expression})${extglobStar})`;
  1177. }
  1178. if (token.prev.type === 'bos') {
  1179. state.negatedExtglob = true;
  1180. }
  1181. }
  1182. push({ type: 'paren', extglob: true, value, output });
  1183. decrement('parens');
  1184. };
  1185. /**
  1186. * Fast paths
  1187. */
  1188. if (opts.fastpaths !== false && !/(^[*!]|[/()[\]{}"])/.test(input)) {
  1189. let backslashes = false;
  1190. let output = input.replace(REGEX_SPECIAL_CHARS_BACKREF, (m, esc, chars, first, rest, index) => {
  1191. if (first === '\\') {
  1192. backslashes = true;
  1193. return m;
  1194. }
  1195. if (first === '?') {
  1196. if (esc) {
  1197. return esc + first + (rest ? QMARK.repeat(rest.length) : '');
  1198. }
  1199. if (index === 0) {
  1200. return qmarkNoDot + (rest ? QMARK.repeat(rest.length) : '');
  1201. }
  1202. return QMARK.repeat(chars.length);
  1203. }
  1204. if (first === '.') {
  1205. return DOT_LITERAL.repeat(chars.length);
  1206. }
  1207. if (first === '*') {
  1208. if (esc) {
  1209. return esc + first + (rest ? star : '');
  1210. }
  1211. return star;
  1212. }
  1213. return esc ? m : `\\${m}`;
  1214. });
  1215. if (backslashes === true) {
  1216. if (opts.unescape === true) {
  1217. output = output.replace(/\\/g, '');
  1218. } else {
  1219. output = output.replace(/\\+/g, m => {
  1220. return m.length % 2 === 0 ? '\\\\' : (m ? '\\' : '');
  1221. });
  1222. }
  1223. }
  1224. if (output === input && opts.contains === true) {
  1225. state.output = input;
  1226. return state;
  1227. }
  1228. state.output = utils$i.wrapOutput(output, state, options);
  1229. return state;
  1230. }
  1231. /**
  1232. * Tokenize input until we reach end-of-string
  1233. */
  1234. while (!eos()) {
  1235. value = advance();
  1236. if (value === '\u0000') {
  1237. continue;
  1238. }
  1239. /**
  1240. * Escaped characters
  1241. */
  1242. if (value === '\\') {
  1243. const next = peek();
  1244. if (next === '/' && opts.bash !== true) {
  1245. continue;
  1246. }
  1247. if (next === '.' || next === ';') {
  1248. continue;
  1249. }
  1250. if (!next) {
  1251. value += '\\';
  1252. push({ type: 'text', value });
  1253. continue;
  1254. }
  1255. // collapse slashes to reduce potential for exploits
  1256. const match = /^\\+/.exec(remaining());
  1257. let slashes = 0;
  1258. if (match && match[0].length > 2) {
  1259. slashes = match[0].length;
  1260. state.index += slashes;
  1261. if (slashes % 2 !== 0) {
  1262. value += '\\';
  1263. }
  1264. }
  1265. if (opts.unescape === true) {
  1266. value = advance();
  1267. } else {
  1268. value += advance();
  1269. }
  1270. if (state.brackets === 0) {
  1271. push({ type: 'text', value });
  1272. continue;
  1273. }
  1274. }
  1275. /**
  1276. * If we're inside a regex character class, continue
  1277. * until we reach the closing bracket.
  1278. */
  1279. if (state.brackets > 0 && (value !== ']' || prev.value === '[' || prev.value === '[^')) {
  1280. if (opts.posix !== false && value === ':') {
  1281. const inner = prev.value.slice(1);
  1282. if (inner.includes('[')) {
  1283. prev.posix = true;
  1284. if (inner.includes(':')) {
  1285. const idx = prev.value.lastIndexOf('[');
  1286. const pre = prev.value.slice(0, idx);
  1287. const rest = prev.value.slice(idx + 2);
  1288. const posix = POSIX_REGEX_SOURCE[rest];
  1289. if (posix) {
  1290. prev.value = pre + posix;
  1291. state.backtrack = true;
  1292. advance();
  1293. if (!bos.output && tokens.indexOf(prev) === 1) {
  1294. bos.output = ONE_CHAR;
  1295. }
  1296. continue;
  1297. }
  1298. }
  1299. }
  1300. }
  1301. if ((value === '[' && peek() !== ':') || (value === '-' && peek() === ']')) {
  1302. value = `\\${value}`;
  1303. }
  1304. if (value === ']' && (prev.value === '[' || prev.value === '[^')) {
  1305. value = `\\${value}`;
  1306. }
  1307. if (opts.posix === true && value === '!' && prev.value === '[') {
  1308. value = '^';
  1309. }
  1310. prev.value += value;
  1311. append({ value });
  1312. continue;
  1313. }
  1314. /**
  1315. * If we're inside a quoted string, continue
  1316. * until we reach the closing double quote.
  1317. */
  1318. if (state.quotes === 1 && value !== '"') {
  1319. value = utils$i.escapeRegex(value);
  1320. prev.value += value;
  1321. append({ value });
  1322. continue;
  1323. }
  1324. /**
  1325. * Double quotes
  1326. */
  1327. if (value === '"') {
  1328. state.quotes = state.quotes === 1 ? 0 : 1;
  1329. if (opts.keepQuotes === true) {
  1330. push({ type: 'text', value });
  1331. }
  1332. continue;
  1333. }
  1334. /**
  1335. * Parentheses
  1336. */
  1337. if (value === '(') {
  1338. increment('parens');
  1339. push({ type: 'paren', value });
  1340. continue;
  1341. }
  1342. if (value === ')') {
  1343. if (state.parens === 0 && opts.strictBrackets === true) {
  1344. throw new SyntaxError(syntaxError('opening', '('));
  1345. }
  1346. const extglob = extglobs[extglobs.length - 1];
  1347. if (extglob && state.parens === extglob.parens + 1) {
  1348. extglobClose(extglobs.pop());
  1349. continue;
  1350. }
  1351. push({ type: 'paren', value, output: state.parens ? ')' : '\\)' });
  1352. decrement('parens');
  1353. continue;
  1354. }
  1355. /**
  1356. * Square brackets
  1357. */
  1358. if (value === '[') {
  1359. if (opts.nobracket === true || !remaining().includes(']')) {
  1360. if (opts.nobracket !== true && opts.strictBrackets === true) {
  1361. throw new SyntaxError(syntaxError('closing', ']'));
  1362. }
  1363. value = `\\${value}`;
  1364. } else {
  1365. increment('brackets');
  1366. }
  1367. push({ type: 'bracket', value });
  1368. continue;
  1369. }
  1370. if (value === ']') {
  1371. if (opts.nobracket === true || (prev && prev.type === 'bracket' && prev.value.length === 1)) {
  1372. push({ type: 'text', value, output: `\\${value}` });
  1373. continue;
  1374. }
  1375. if (state.brackets === 0) {
  1376. if (opts.strictBrackets === true) {
  1377. throw new SyntaxError(syntaxError('opening', '['));
  1378. }
  1379. push({ type: 'text', value, output: `\\${value}` });
  1380. continue;
  1381. }
  1382. decrement('brackets');
  1383. const prevValue = prev.value.slice(1);
  1384. if (prev.posix !== true && prevValue[0] === '^' && !prevValue.includes('/')) {
  1385. value = `/${value}`;
  1386. }
  1387. prev.value += value;
  1388. append({ value });
  1389. // when literal brackets are explicitly disabled
  1390. // assume we should match with a regex character class
  1391. if (opts.literalBrackets === false || utils$i.hasRegexChars(prevValue)) {
  1392. continue;
  1393. }
  1394. const escaped = utils$i.escapeRegex(prev.value);
  1395. state.output = state.output.slice(0, -prev.value.length);
  1396. // when literal brackets are explicitly enabled
  1397. // assume we should escape the brackets to match literal characters
  1398. if (opts.literalBrackets === true) {
  1399. state.output += escaped;
  1400. prev.value = escaped;
  1401. continue;
  1402. }
  1403. // when the user specifies nothing, try to match both
  1404. prev.value = `(${capture}${escaped}|${prev.value})`;
  1405. state.output += prev.value;
  1406. continue;
  1407. }
  1408. /**
  1409. * Braces
  1410. */
  1411. if (value === '{' && opts.nobrace !== true) {
  1412. increment('braces');
  1413. const open = {
  1414. type: 'brace',
  1415. value,
  1416. output: '(',
  1417. outputIndex: state.output.length,
  1418. tokensIndex: state.tokens.length
  1419. };
  1420. braces.push(open);
  1421. push(open);
  1422. continue;
  1423. }
  1424. if (value === '}') {
  1425. const brace = braces[braces.length - 1];
  1426. if (opts.nobrace === true || !brace) {
  1427. push({ type: 'text', value, output: value });
  1428. continue;
  1429. }
  1430. let output = ')';
  1431. if (brace.dots === true) {
  1432. const arr = tokens.slice();
  1433. const range = [];
  1434. for (let i = arr.length - 1; i >= 0; i--) {
  1435. tokens.pop();
  1436. if (arr[i].type === 'brace') {
  1437. break;
  1438. }
  1439. if (arr[i].type !== 'dots') {
  1440. range.unshift(arr[i].value);
  1441. }
  1442. }
  1443. output = expandRange(range, opts);
  1444. state.backtrack = true;
  1445. }
  1446. if (brace.comma !== true && brace.dots !== true) {
  1447. const out = state.output.slice(0, brace.outputIndex);
  1448. const toks = state.tokens.slice(brace.tokensIndex);
  1449. brace.value = brace.output = '\\{';
  1450. value = output = '\\}';
  1451. state.output = out;
  1452. for (const t of toks) {
  1453. state.output += (t.output || t.value);
  1454. }
  1455. }
  1456. push({ type: 'brace', value, output });
  1457. decrement('braces');
  1458. braces.pop();
  1459. continue;
  1460. }
  1461. /**
  1462. * Pipes
  1463. */
  1464. if (value === '|') {
  1465. if (extglobs.length > 0) {
  1466. extglobs[extglobs.length - 1].conditions++;
  1467. }
  1468. push({ type: 'text', value });
  1469. continue;
  1470. }
  1471. /**
  1472. * Commas
  1473. */
  1474. if (value === ',') {
  1475. let output = value;
  1476. const brace = braces[braces.length - 1];
  1477. if (brace && stack[stack.length - 1] === 'braces') {
  1478. brace.comma = true;
  1479. output = '|';
  1480. }
  1481. push({ type: 'comma', value, output });
  1482. continue;
  1483. }
  1484. /**
  1485. * Slashes
  1486. */
  1487. if (value === '/') {
  1488. // if the beginning of the glob is "./", advance the start
  1489. // to the current index, and don't add the "./" characters
  1490. // to the state. This greatly simplifies lookbehinds when
  1491. // checking for BOS characters like "!" and "." (not "./")
  1492. if (prev.type === 'dot' && state.index === state.start + 1) {
  1493. state.start = state.index + 1;
  1494. state.consumed = '';
  1495. state.output = '';
  1496. tokens.pop();
  1497. prev = bos; // reset "prev" to the first token
  1498. continue;
  1499. }
  1500. push({ type: 'slash', value, output: SLASH_LITERAL });
  1501. continue;
  1502. }
  1503. /**
  1504. * Dots
  1505. */
  1506. if (value === '.') {
  1507. if (state.braces > 0 && prev.type === 'dot') {
  1508. if (prev.value === '.') prev.output = DOT_LITERAL;
  1509. const brace = braces[braces.length - 1];
  1510. prev.type = 'dots';
  1511. prev.output += value;
  1512. prev.value += value;
  1513. brace.dots = true;
  1514. continue;
  1515. }
  1516. if ((state.braces + state.parens) === 0 && prev.type !== 'bos' && prev.type !== 'slash') {
  1517. push({ type: 'text', value, output: DOT_LITERAL });
  1518. continue;
  1519. }
  1520. push({ type: 'dot', value, output: DOT_LITERAL });
  1521. continue;
  1522. }
  1523. /**
  1524. * Question marks
  1525. */
  1526. if (value === '?') {
  1527. const isGroup = prev && prev.value === '(';
  1528. if (!isGroup && opts.noextglob !== true && peek() === '(' && peek(2) !== '?') {
  1529. extglobOpen('qmark', value);
  1530. continue;
  1531. }
  1532. if (prev && prev.type === 'paren') {
  1533. const next = peek();
  1534. let output = value;
  1535. if (next === '<' && !utils$i.supportsLookbehinds()) {
  1536. throw new Error('Node.js v10 or higher is required for regex lookbehinds');
  1537. }
  1538. if ((prev.value === '(' && !/[!=<:]/.test(next)) || (next === '<' && !/<([!=]|\w+>)/.test(remaining()))) {
  1539. output = `\\${value}`;
  1540. }
  1541. push({ type: 'text', value, output });
  1542. continue;
  1543. }
  1544. if (opts.dot !== true && (prev.type === 'slash' || prev.type === 'bos')) {
  1545. push({ type: 'qmark', value, output: QMARK_NO_DOT });
  1546. continue;
  1547. }
  1548. push({ type: 'qmark', value, output: QMARK });
  1549. continue;
  1550. }
  1551. /**
  1552. * Exclamation
  1553. */
  1554. if (value === '!') {
  1555. if (opts.noextglob !== true && peek() === '(') {
  1556. if (peek(2) !== '?' || !/[!=<:]/.test(peek(3))) {
  1557. extglobOpen('negate', value);
  1558. continue;
  1559. }
  1560. }
  1561. if (opts.nonegate !== true && state.index === 0) {
  1562. negate();
  1563. continue;
  1564. }
  1565. }
  1566. /**
  1567. * Plus
  1568. */
  1569. if (value === '+') {
  1570. if (opts.noextglob !== true && peek() === '(' && peek(2) !== '?') {
  1571. extglobOpen('plus', value);
  1572. continue;
  1573. }
  1574. if ((prev && prev.value === '(') || opts.regex === false) {
  1575. push({ type: 'plus', value, output: PLUS_LITERAL });
  1576. continue;
  1577. }
  1578. if ((prev && (prev.type === 'bracket' || prev.type === 'paren' || prev.type === 'brace')) || state.parens > 0) {
  1579. push({ type: 'plus', value });
  1580. continue;
  1581. }
  1582. push({ type: 'plus', value: PLUS_LITERAL });
  1583. continue;
  1584. }
  1585. /**
  1586. * Plain text
  1587. */
  1588. if (value === '@') {
  1589. if (opts.noextglob !== true && peek() === '(' && peek(2) !== '?') {
  1590. push({ type: 'at', extglob: true, value, output: '' });
  1591. continue;
  1592. }
  1593. push({ type: 'text', value });
  1594. continue;
  1595. }
  1596. /**
  1597. * Plain text
  1598. */
  1599. if (value !== '*') {
  1600. if (value === '$' || value === '^') {
  1601. value = `\\${value}`;
  1602. }
  1603. const match = REGEX_NON_SPECIAL_CHARS.exec(remaining());
  1604. if (match) {
  1605. value += match[0];
  1606. state.index += match[0].length;
  1607. }
  1608. push({ type: 'text', value });
  1609. continue;
  1610. }
  1611. /**
  1612. * Stars
  1613. */
  1614. if (prev && (prev.type === 'globstar' || prev.star === true)) {
  1615. prev.type = 'star';
  1616. prev.star = true;
  1617. prev.value += value;
  1618. prev.output = star;
  1619. state.backtrack = true;
  1620. state.globstar = true;
  1621. consume(value);
  1622. continue;
  1623. }
  1624. let rest = remaining();
  1625. if (opts.noextglob !== true && /^\([^?]/.test(rest)) {
  1626. extglobOpen('star', value);
  1627. continue;
  1628. }
  1629. if (prev.type === 'star') {
  1630. if (opts.noglobstar === true) {
  1631. consume(value);
  1632. continue;
  1633. }
  1634. const prior = prev.prev;
  1635. const before = prior.prev;
  1636. const isStart = prior.type === 'slash' || prior.type === 'bos';
  1637. const afterStar = before && (before.type === 'star' || before.type === 'globstar');
  1638. if (opts.bash === true && (!isStart || (rest[0] && rest[0] !== '/'))) {
  1639. push({ type: 'star', value, output: '' });
  1640. continue;
  1641. }
  1642. const isBrace = state.braces > 0 && (prior.type === 'comma' || prior.type === 'brace');
  1643. const isExtglob = extglobs.length && (prior.type === 'pipe' || prior.type === 'paren');
  1644. if (!isStart && prior.type !== 'paren' && !isBrace && !isExtglob) {
  1645. push({ type: 'star', value, output: '' });
  1646. continue;
  1647. }
  1648. // strip consecutive `/**/`
  1649. while (rest.slice(0, 3) === '/**') {
  1650. const after = input[state.index + 4];
  1651. if (after && after !== '/') {
  1652. break;
  1653. }
  1654. rest = rest.slice(3);
  1655. consume('/**', 3);
  1656. }
  1657. if (prior.type === 'bos' && eos()) {
  1658. prev.type = 'globstar';
  1659. prev.value += value;
  1660. prev.output = globstar(opts);
  1661. state.output = prev.output;
  1662. state.globstar = true;
  1663. consume(value);
  1664. continue;
  1665. }
  1666. if (prior.type === 'slash' && prior.prev.type !== 'bos' && !afterStar && eos()) {
  1667. state.output = state.output.slice(0, -(prior.output + prev.output).length);
  1668. prior.output = `(?:${prior.output}`;
  1669. prev.type = 'globstar';
  1670. prev.output = globstar(opts) + (opts.strictSlashes ? ')' : '|$)');
  1671. prev.value += value;
  1672. state.globstar = true;
  1673. state.output += prior.output + prev.output;
  1674. consume(value);
  1675. continue;
  1676. }
  1677. if (prior.type === 'slash' && prior.prev.type !== 'bos' && rest[0] === '/') {
  1678. const end = rest[1] !== void 0 ? '|$' : '';
  1679. state.output = state.output.slice(0, -(prior.output + prev.output).length);
  1680. prior.output = `(?:${prior.output}`;
  1681. prev.type = 'globstar';
  1682. prev.output = `${globstar(opts)}${SLASH_LITERAL}|${SLASH_LITERAL}${end})`;
  1683. prev.value += value;
  1684. state.output += prior.output + prev.output;
  1685. state.globstar = true;
  1686. consume(value + advance());
  1687. push({ type: 'slash', value: '/', output: '' });
  1688. continue;
  1689. }
  1690. if (prior.type === 'bos' && rest[0] === '/') {
  1691. prev.type = 'globstar';
  1692. prev.value += value;
  1693. prev.output = `(?:^|${SLASH_LITERAL}|${globstar(opts)}${SLASH_LITERAL})`;
  1694. state.output = prev.output;
  1695. state.globstar = true;
  1696. consume(value + advance());
  1697. push({ type: 'slash', value: '/', output: '' });
  1698. continue;
  1699. }
  1700. // remove single star from output
  1701. state.output = state.output.slice(0, -prev.output.length);
  1702. // reset previous token to globstar
  1703. prev.type = 'globstar';
  1704. prev.output = globstar(opts);
  1705. prev.value += value;
  1706. // reset output with globstar
  1707. state.output += prev.output;
  1708. state.globstar = true;
  1709. consume(value);
  1710. continue;
  1711. }
  1712. const token = { type: 'star', value, output: star };
  1713. if (opts.bash === true) {
  1714. token.output = '.*?';
  1715. if (prev.type === 'bos' || prev.type === 'slash') {
  1716. token.output = nodot + token.output;
  1717. }
  1718. push(token);
  1719. continue;
  1720. }
  1721. if (prev && (prev.type === 'bracket' || prev.type === 'paren') && opts.regex === true) {
  1722. token.output = value;
  1723. push(token);
  1724. continue;
  1725. }
  1726. if (state.index === state.start || prev.type === 'slash' || prev.type === 'dot') {
  1727. if (prev.type === 'dot') {
  1728. state.output += NO_DOT_SLASH;
  1729. prev.output += NO_DOT_SLASH;
  1730. } else if (opts.dot === true) {
  1731. state.output += NO_DOTS_SLASH;
  1732. prev.output += NO_DOTS_SLASH;
  1733. } else {
  1734. state.output += nodot;
  1735. prev.output += nodot;
  1736. }
  1737. if (peek() !== '*') {
  1738. state.output += ONE_CHAR;
  1739. prev.output += ONE_CHAR;
  1740. }
  1741. }
  1742. push(token);
  1743. }
  1744. while (state.brackets > 0) {
  1745. if (opts.strictBrackets === true) throw new SyntaxError(syntaxError('closing', ']'));
  1746. state.output = utils$i.escapeLast(state.output, '[');
  1747. decrement('brackets');
  1748. }
  1749. while (state.parens > 0) {
  1750. if (opts.strictBrackets === true) throw new SyntaxError(syntaxError('closing', ')'));
  1751. state.output = utils$i.escapeLast(state.output, '(');
  1752. decrement('parens');
  1753. }
  1754. while (state.braces > 0) {
  1755. if (opts.strictBrackets === true) throw new SyntaxError(syntaxError('closing', '}'));
  1756. state.output = utils$i.escapeLast(state.output, '{');
  1757. decrement('braces');
  1758. }
  1759. if (opts.strictSlashes !== true && (prev.type === 'star' || prev.type === 'bracket')) {
  1760. push({ type: 'maybe_slash', value: '', output: `${SLASH_LITERAL}?` });
  1761. }
  1762. // rebuild the output if we had to backtrack at any point
  1763. if (state.backtrack === true) {
  1764. state.output = '';
  1765. for (const token of state.tokens) {
  1766. state.output += token.output != null ? token.output : token.value;
  1767. if (token.suffix) {
  1768. state.output += token.suffix;
  1769. }
  1770. }
  1771. }
  1772. return state;
  1773. };
  1774. /**
  1775. * Fast paths for creating regular expressions for common glob patterns.
  1776. * This can significantly speed up processing and has very little downside
  1777. * impact when none of the fast paths match.
  1778. */
  1779. parse$g.fastpaths = (input, options) => {
  1780. const opts = { ...options };
  1781. const max = typeof opts.maxLength === 'number' ? Math.min(MAX_LENGTH$1, opts.maxLength) : MAX_LENGTH$1;
  1782. const len = input.length;
  1783. if (len > max) {
  1784. throw new SyntaxError(`Input length: ${len}, exceeds maximum allowed length: ${max}`);
  1785. }
  1786. input = REPLACEMENTS[input] || input;
  1787. const win32 = utils$i.isWindows(options);
  1788. // create constants based on platform, for windows or posix
  1789. const {
  1790. DOT_LITERAL,
  1791. SLASH_LITERAL,
  1792. ONE_CHAR,
  1793. DOTS_SLASH,
  1794. NO_DOT,
  1795. NO_DOTS,
  1796. NO_DOTS_SLASH,
  1797. STAR,
  1798. START_ANCHOR
  1799. } = constants$5.globChars(win32);
  1800. const nodot = opts.dot ? NO_DOTS : NO_DOT;
  1801. const slashDot = opts.dot ? NO_DOTS_SLASH : NO_DOT;
  1802. const capture = opts.capture ? '' : '?:';
  1803. const state = { negated: false, prefix: '' };
  1804. let star = opts.bash === true ? '.*?' : STAR;
  1805. if (opts.capture) {
  1806. star = `(${star})`;
  1807. }
  1808. const globstar = opts => {
  1809. if (opts.noglobstar === true) return star;
  1810. return `(${capture}(?:(?!${START_ANCHOR}${opts.dot ? DOTS_SLASH : DOT_LITERAL}).)*?)`;
  1811. };
  1812. const create = str => {
  1813. switch (str) {
  1814. case '*':
  1815. return `${nodot}${ONE_CHAR}${star}`;
  1816. case '.*':
  1817. return `${DOT_LITERAL}${ONE_CHAR}${star}`;
  1818. case '*.*':
  1819. return `${nodot}${star}${DOT_LITERAL}${ONE_CHAR}${star}`;
  1820. case '*/*':
  1821. return `${nodot}${star}${SLASH_LITERAL}${ONE_CHAR}${slashDot}${star}`;
  1822. case '**':
  1823. return nodot + globstar(opts);
  1824. case '**/*':
  1825. return `(?:${nodot}${globstar(opts)}${SLASH_LITERAL})?${slashDot}${ONE_CHAR}${star}`;
  1826. case '**/*.*':
  1827. return `(?:${nodot}${globstar(opts)}${SLASH_LITERAL})?${slashDot}${star}${DOT_LITERAL}${ONE_CHAR}${star}`;
  1828. case '**/.*':
  1829. return `(?:${nodot}${globstar(opts)}${SLASH_LITERAL})?${DOT_LITERAL}${ONE_CHAR}${star}`;
  1830. default: {
  1831. const match = /^(.*?)\.(\w+)$/.exec(str);
  1832. if (!match) return;
  1833. const source = create(match[1]);
  1834. if (!source) return;
  1835. return source + DOT_LITERAL + match[2];
  1836. }
  1837. }
  1838. };
  1839. const output = utils$i.removePrefix(input, state);
  1840. let source = create(output);
  1841. if (source && opts.strictSlashes !== true) {
  1842. source += `${SLASH_LITERAL}?`;
  1843. }
  1844. return source;
  1845. };
  1846. var parse_1$3 = parse$g;
  1847. const path$l = require$$0$4;
  1848. const scan$1 = scan_1;
  1849. const parse$f = parse_1$3;
  1850. const utils$h = utils$k;
  1851. const constants$4 = constants$6;
  1852. const isObject$3 = val => val && typeof val === 'object' && !Array.isArray(val);
  1853. /**
  1854. * Creates a matcher function from one or more glob patterns. The
  1855. * returned function takes a string to match as its first argument,
  1856. * and returns true if the string is a match. The returned matcher
  1857. * function also takes a boolean as the second argument that, when true,
  1858. * returns an object with additional information.
  1859. *
  1860. * ```js
  1861. * const picomatch = require('picomatch');
  1862. * // picomatch(glob[, options]);
  1863. *
  1864. * const isMatch = picomatch('*.!(*a)');
  1865. * console.log(isMatch('a.a')); //=> false
  1866. * console.log(isMatch('a.b')); //=> true
  1867. * ```
  1868. * @name picomatch
  1869. * @param {String|Array} `globs` One or more glob patterns.
  1870. * @param {Object=} `options`
  1871. * @return {Function=} Returns a matcher function.
  1872. * @api public
  1873. */
  1874. const picomatch$5 = (glob, options, returnState = false) => {
  1875. if (Array.isArray(glob)) {
  1876. const fns = glob.map(input => picomatch$5(input, options, returnState));
  1877. const arrayMatcher = str => {
  1878. for (const isMatch of fns) {
  1879. const state = isMatch(str);
  1880. if (state) return state;
  1881. }
  1882. return false;
  1883. };
  1884. return arrayMatcher;
  1885. }
  1886. const isState = isObject$3(glob) && glob.tokens && glob.input;
  1887. if (glob === '' || (typeof glob !== 'string' && !isState)) {
  1888. throw new TypeError('Expected pattern to be a non-empty string');
  1889. }
  1890. const opts = options || {};
  1891. const posix = utils$h.isWindows(options);
  1892. const regex = isState
  1893. ? picomatch$5.compileRe(glob, options)
  1894. : picomatch$5.makeRe(glob, options, false, true);
  1895. const state = regex.state;
  1896. delete regex.state;
  1897. let isIgnored = () => false;
  1898. if (opts.ignore) {
  1899. const ignoreOpts = { ...options, ignore: null, onMatch: null, onResult: null };
  1900. isIgnored = picomatch$5(opts.ignore, ignoreOpts, returnState);
  1901. }
  1902. const matcher = (input, returnObject = false) => {
  1903. const { isMatch, match, output } = picomatch$5.test(input, regex, options, { glob, posix });
  1904. const result = { glob, state, regex, posix, input, output, match, isMatch };
  1905. if (typeof opts.onResult === 'function') {
  1906. opts.onResult(result);
  1907. }
  1908. if (isMatch === false) {
  1909. result.isMatch = false;
  1910. return returnObject ? result : false;
  1911. }
  1912. if (isIgnored(input)) {
  1913. if (typeof opts.onIgnore === 'function') {
  1914. opts.onIgnore(result);
  1915. }
  1916. result.isMatch = false;
  1917. return returnObject ? result : false;
  1918. }
  1919. if (typeof opts.onMatch === 'function') {
  1920. opts.onMatch(result);
  1921. }
  1922. return returnObject ? result : true;
  1923. };
  1924. if (returnState) {
  1925. matcher.state = state;
  1926. }
  1927. return matcher;
  1928. };
  1929. /**
  1930. * Test `input` with the given `regex`. This is used by the main
  1931. * `picomatch()` function to test the input string.
  1932. *
  1933. * ```js
  1934. * const picomatch = require('picomatch');
  1935. * // picomatch.test(input, regex[, options]);
  1936. *
  1937. * console.log(picomatch.test('foo/bar', /^(?:([^/]*?)\/([^/]*?))$/));
  1938. * // { isMatch: true, match: [ 'foo/', 'foo', 'bar' ], output: 'foo/bar' }
  1939. * ```
  1940. * @param {String} `input` String to test.
  1941. * @param {RegExp} `regex`
  1942. * @return {Object} Returns an object with matching info.
  1943. * @api public
  1944. */
  1945. picomatch$5.test = (input, regex, options, { glob, posix } = {}) => {
  1946. if (typeof input !== 'string') {
  1947. throw new TypeError('Expected input to be a string');
  1948. }
  1949. if (input === '') {
  1950. return { isMatch: false, output: '' };
  1951. }
  1952. const opts = options || {};
  1953. const format = opts.format || (posix ? utils$h.toPosixSlashes : null);
  1954. let match = input === glob;
  1955. let output = (match && format) ? format(input) : input;
  1956. if (match === false) {
  1957. output = format ? format(input) : input;
  1958. match = output === glob;
  1959. }
  1960. if (match === false || opts.capture === true) {
  1961. if (opts.matchBase === true || opts.basename === true) {
  1962. match = picomatch$5.matchBase(input, regex, options, posix);
  1963. } else {
  1964. match = regex.exec(output);
  1965. }
  1966. }
  1967. return { isMatch: Boolean(match), match, output };
  1968. };
  1969. /**
  1970. * Match the basename of a filepath.
  1971. *
  1972. * ```js
  1973. * const picomatch = require('picomatch');
  1974. * // picomatch.matchBase(input, glob[, options]);
  1975. * console.log(picomatch.matchBase('foo/bar.js', '*.js'); // true
  1976. * ```
  1977. * @param {String} `input` String to test.
  1978. * @param {RegExp|String} `glob` Glob pattern or regex created by [.makeRe](#makeRe).
  1979. * @return {Boolean}
  1980. * @api public
  1981. */
  1982. picomatch$5.matchBase = (input, glob, options, posix = utils$h.isWindows(options)) => {
  1983. const regex = glob instanceof RegExp ? glob : picomatch$5.makeRe(glob, options);
  1984. return regex.test(path$l.basename(input));
  1985. };
  1986. /**
  1987. * Returns true if **any** of the given glob `patterns` match the specified `string`.
  1988. *
  1989. * ```js
  1990. * const picomatch = require('picomatch');
  1991. * // picomatch.isMatch(string, patterns[, options]);
  1992. *
  1993. * console.log(picomatch.isMatch('a.a', ['b.*', '*.a'])); //=> true
  1994. * console.log(picomatch.isMatch('a.a', 'b.*')); //=> false
  1995. * ```
  1996. * @param {String|Array} str The string to test.
  1997. * @param {String|Array} patterns One or more glob patterns to use for matching.
  1998. * @param {Object} [options] See available [options](#options).
  1999. * @return {Boolean} Returns true if any patterns match `str`
  2000. * @api public
  2001. */
  2002. picomatch$5.isMatch = (str, patterns, options) => picomatch$5(patterns, options)(str);
  2003. /**
  2004. * Parse a glob pattern to create the source string for a regular
  2005. * expression.
  2006. *
  2007. * ```js
  2008. * const picomatch = require('picomatch');
  2009. * const result = picomatch.parse(pattern[, options]);
  2010. * ```
  2011. * @param {String} `pattern`
  2012. * @param {Object} `options`
  2013. * @return {Object} Returns an object with useful properties and output to be used as a regex source string.
  2014. * @api public
  2015. */
  2016. picomatch$5.parse = (pattern, options) => {
  2017. if (Array.isArray(pattern)) return pattern.map(p => picomatch$5.parse(p, options));
  2018. return parse$f(pattern, { ...options, fastpaths: false });
  2019. };
  2020. /**
  2021. * Scan a glob pattern to separate the pattern into segments.
  2022. *
  2023. * ```js
  2024. * const picomatch = require('picomatch');
  2025. * // picomatch.scan(input[, options]);
  2026. *
  2027. * const result = picomatch.scan('!./foo/*.js');
  2028. * console.log(result);
  2029. * { prefix: '!./',
  2030. * input: '!./foo/*.js',
  2031. * start: 3,
  2032. * base: 'foo',
  2033. * glob: '*.js',
  2034. * isBrace: false,
  2035. * isBracket: false,
  2036. * isGlob: true,
  2037. * isExtglob: false,
  2038. * isGlobstar: false,
  2039. * negated: true }
  2040. * ```
  2041. * @param {String} `input` Glob pattern to scan.
  2042. * @param {Object} `options`
  2043. * @return {Object} Returns an object with
  2044. * @api public
  2045. */
  2046. picomatch$5.scan = (input, options) => scan$1(input, options);
  2047. /**
  2048. * Compile a regular expression from the `state` object returned by the
  2049. * [parse()](#parse) method.
  2050. *
  2051. * @param {Object} `state`
  2052. * @param {Object} `options`
  2053. * @param {Boolean} `returnOutput` Intended for implementors, this argument allows you to return the raw output from the parser.
  2054. * @param {Boolean} `returnState` Adds the state to a `state` property on the returned regex. Useful for implementors and debugging.
  2055. * @return {RegExp}
  2056. * @api public
  2057. */
  2058. picomatch$5.compileRe = (state, options, returnOutput = false, returnState = false) => {
  2059. if (returnOutput === true) {
  2060. return state.output;
  2061. }
  2062. const opts = options || {};
  2063. const prepend = opts.contains ? '' : '^';
  2064. const append = opts.contains ? '' : '$';
  2065. let source = `${prepend}(?:${state.output})${append}`;
  2066. if (state && state.negated === true) {
  2067. source = `^(?!${source}).*$`;
  2068. }
  2069. const regex = picomatch$5.toRegex(source, options);
  2070. if (returnState === true) {
  2071. regex.state = state;
  2072. }
  2073. return regex;
  2074. };
  2075. /**
  2076. * Create a regular expression from a parsed glob pattern.
  2077. *
  2078. * ```js
  2079. * const picomatch = require('picomatch');
  2080. * const state = picomatch.parse('*.js');
  2081. * // picomatch.compileRe(state[, options]);
  2082. *
  2083. * console.log(picomatch.compileRe(state));
  2084. * //=> /^(?:(?!\.)(?=.)[^/]*?\.js)$/
  2085. * ```
  2086. * @param {String} `state` The object returned from the `.parse` method.
  2087. * @param {Object} `options`
  2088. * @param {Boolean} `returnOutput` Implementors may use this argument to return the compiled output, instead of a regular expression. This is not exposed on the options to prevent end-users from mutating the result.
  2089. * @param {Boolean} `returnState` Implementors may use this argument to return the state from the parsed glob with the returned regular expression.
  2090. * @return {RegExp} Returns a regex created from the given pattern.
  2091. * @api public
  2092. */
  2093. picomatch$5.makeRe = (input, options = {}, returnOutput = false, returnState = false) => {
  2094. if (!input || typeof input !== 'string') {
  2095. throw new TypeError('Expected a non-empty string');
  2096. }
  2097. let parsed = { negated: false, fastpaths: true };
  2098. if (options.fastpaths !== false && (input[0] === '.' || input[0] === '*')) {
  2099. parsed.output = parse$f.fastpaths(input, options);
  2100. }
  2101. if (!parsed.output) {
  2102. parsed = parse$f(input, options);
  2103. }
  2104. return picomatch$5.compileRe(parsed, options, returnOutput, returnState);
  2105. };
  2106. /**
  2107. * Create a regular expression from the given regex source string.
  2108. *
  2109. * ```js
  2110. * const picomatch = require('picomatch');
  2111. * // picomatch.toRegex(source[, options]);
  2112. *
  2113. * const { output } = picomatch.parse('*.js');
  2114. * console.log(picomatch.toRegex(output));
  2115. * //=> /^(?:(?!\.)(?=.)[^/]*?\.js)$/
  2116. * ```
  2117. * @param {String} `source` Regular expression source string.
  2118. * @param {Object} `options`
  2119. * @return {RegExp}
  2120. * @api public
  2121. */
  2122. picomatch$5.toRegex = (source, options) => {
  2123. try {
  2124. const opts = options || {};
  2125. return new RegExp(source, opts.flags || (opts.nocase ? 'i' : ''));
  2126. } catch (err) {
  2127. if (options && options.debug === true) throw err;
  2128. return /$^/;
  2129. }
  2130. };
  2131. /**
  2132. * Picomatch constants.
  2133. * @return {Object}
  2134. */
  2135. picomatch$5.constants = constants$4;
  2136. /**
  2137. * Expose "picomatch"
  2138. */
  2139. var picomatch_1 = picomatch$5;
  2140. var picomatch$3 = picomatch_1;
  2141. var picomatch$4 = /*@__PURE__*/getDefaultExportFromCjs(picomatch$3);
  2142. const extractors = {
  2143. ArrayPattern(names, param) {
  2144. for (const element of param.elements) {
  2145. if (element)
  2146. extractors[element.type](names, element);
  2147. }
  2148. },
  2149. AssignmentPattern(names, param) {
  2150. extractors[param.left.type](names, param.left);
  2151. },
  2152. Identifier(names, param) {
  2153. names.push(param.name);
  2154. },
  2155. MemberExpression() { },
  2156. ObjectPattern(names, param) {
  2157. for (const prop of param.properties) {
  2158. // @ts-ignore Typescript reports that this is not a valid type
  2159. if (prop.type === 'RestElement') {
  2160. extractors.RestElement(names, prop);
  2161. }
  2162. else {
  2163. extractors[prop.value.type](names, prop.value);
  2164. }
  2165. }
  2166. },
  2167. RestElement(names, param) {
  2168. extractors[param.argument.type](names, param.argument);
  2169. }
  2170. };
  2171. const extractAssignedNames = function extractAssignedNames(param) {
  2172. const names = [];
  2173. extractors[param.type](names, param);
  2174. return names;
  2175. };
  2176. const blockDeclarations = {
  2177. const: true,
  2178. let: true
  2179. };
  2180. class Scope {
  2181. constructor(options = {}) {
  2182. this.parent = options.parent;
  2183. this.isBlockScope = !!options.block;
  2184. this.declarations = Object.create(null);
  2185. if (options.params) {
  2186. options.params.forEach((param) => {
  2187. extractAssignedNames(param).forEach((name) => {
  2188. this.declarations[name] = true;
  2189. });
  2190. });
  2191. }
  2192. }
  2193. addDeclaration(node, isBlockDeclaration, isVar) {
  2194. if (!isBlockDeclaration && this.isBlockScope) {
  2195. // it's a `var` or function node, and this
  2196. // is a block scope, so we need to go up
  2197. this.parent.addDeclaration(node, isBlockDeclaration, isVar);
  2198. }
  2199. else if (node.id) {
  2200. extractAssignedNames(node.id).forEach((name) => {
  2201. this.declarations[name] = true;
  2202. });
  2203. }
  2204. }
  2205. contains(name) {
  2206. return this.declarations[name] || (this.parent ? this.parent.contains(name) : false);
  2207. }
  2208. }
  2209. const attachScopes = function attachScopes(ast, propertyName = 'scope') {
  2210. let scope = new Scope();
  2211. walk$3(ast, {
  2212. enter(n, parent) {
  2213. const node = n;
  2214. // function foo () {...}
  2215. // class Foo {...}
  2216. if (/(Function|Class)Declaration/.test(node.type)) {
  2217. scope.addDeclaration(node, false, false);
  2218. }
  2219. // var foo = 1
  2220. if (node.type === 'VariableDeclaration') {
  2221. const { kind } = node;
  2222. const isBlockDeclaration = blockDeclarations[kind];
  2223. node.declarations.forEach((declaration) => {
  2224. scope.addDeclaration(declaration, isBlockDeclaration, true);
  2225. });
  2226. }
  2227. let newScope;
  2228. // create new function scope
  2229. if (/Function/.test(node.type)) {
  2230. const func = node;
  2231. newScope = new Scope({
  2232. parent: scope,
  2233. block: false,
  2234. params: func.params
  2235. });
  2236. // named function expressions - the name is considered
  2237. // part of the function's scope
  2238. if (func.type === 'FunctionExpression' && func.id) {
  2239. newScope.addDeclaration(func, false, false);
  2240. }
  2241. }
  2242. // create new for scope
  2243. if (/For(In|Of)?Statement/.test(node.type)) {
  2244. newScope = new Scope({
  2245. parent: scope,
  2246. block: true
  2247. });
  2248. }
  2249. // create new block scope
  2250. if (node.type === 'BlockStatement' && !/Function/.test(parent.type)) {
  2251. newScope = new Scope({
  2252. parent: scope,
  2253. block: true
  2254. });
  2255. }
  2256. // catch clause has its own block scope
  2257. if (node.type === 'CatchClause') {
  2258. newScope = new Scope({
  2259. parent: scope,
  2260. params: node.param ? [node.param] : [],
  2261. block: true
  2262. });
  2263. }
  2264. if (newScope) {
  2265. Object.defineProperty(node, propertyName, {
  2266. value: newScope,
  2267. configurable: true
  2268. });
  2269. scope = newScope;
  2270. }
  2271. },
  2272. leave(n) {
  2273. const node = n;
  2274. if (node[propertyName])
  2275. scope = scope.parent;
  2276. }
  2277. });
  2278. return scope;
  2279. };
  2280. // Helper since Typescript can't detect readonly arrays with Array.isArray
  2281. function isArray(arg) {
  2282. return Array.isArray(arg);
  2283. }
  2284. function ensureArray(thing) {
  2285. if (isArray(thing))
  2286. return thing;
  2287. if (thing == null)
  2288. return [];
  2289. return [thing];
  2290. }
  2291. const normalizePath$5 = function normalizePath(filename) {
  2292. return filename.split(win32.sep).join(posix.sep);
  2293. };
  2294. function getMatcherString(id, resolutionBase) {
  2295. if (resolutionBase === false || isAbsolute(id) || id.startsWith('**')) {
  2296. return normalizePath$5(id);
  2297. }
  2298. // resolve('') is valid and will default to process.cwd()
  2299. const basePath = normalizePath$5(resolve$3(resolutionBase || ''))
  2300. // escape all possible (posix + win) path characters that might interfere with regex
  2301. .replace(/[-^$*+?.()|[\]{}]/g, '\\$&');
  2302. // Note that we use posix.join because:
  2303. // 1. the basePath has been normalized to use /
  2304. // 2. the incoming glob (id) matcher, also uses /
  2305. // otherwise Node will force backslash (\) on windows
  2306. return posix.join(basePath, normalizePath$5(id));
  2307. }
  2308. const createFilter$1 = function createFilter(include, exclude, options) {
  2309. const resolutionBase = options && options.resolve;
  2310. const getMatcher = (id) => id instanceof RegExp
  2311. ? id
  2312. : {
  2313. test: (what) => {
  2314. // this refactor is a tad overly verbose but makes for easy debugging
  2315. const pattern = getMatcherString(id, resolutionBase);
  2316. const fn = picomatch$4(pattern, { dot: true });
  2317. const result = fn(what);
  2318. return result;
  2319. }
  2320. };
  2321. const includeMatchers = ensureArray(include).map(getMatcher);
  2322. const excludeMatchers = ensureArray(exclude).map(getMatcher);
  2323. return function result(id) {
  2324. if (typeof id !== 'string')
  2325. return false;
  2326. if (/\0/.test(id))
  2327. return false;
  2328. const pathId = normalizePath$5(id);
  2329. for (let i = 0; i < excludeMatchers.length; ++i) {
  2330. const matcher = excludeMatchers[i];
  2331. if (matcher.test(pathId))
  2332. return false;
  2333. }
  2334. for (let i = 0; i < includeMatchers.length; ++i) {
  2335. const matcher = includeMatchers[i];
  2336. if (matcher.test(pathId))
  2337. return true;
  2338. }
  2339. return !includeMatchers.length;
  2340. };
  2341. };
  2342. const reservedWords = 'break case class catch const continue debugger default delete do else export extends finally for function if import in instanceof let new return super switch this throw try typeof var void while with yield enum await implements package protected static interface private public';
  2343. const builtins = 'arguments Infinity NaN undefined null true false eval uneval isFinite isNaN parseFloat parseInt decodeURI decodeURIComponent encodeURI encodeURIComponent escape unescape Object Function Boolean Symbol Error EvalError InternalError RangeError ReferenceError SyntaxError TypeError URIError Number Math Date String RegExp Array Int8Array Uint8Array Uint8ClampedArray Int16Array Uint16Array Int32Array Uint32Array Float32Array Float64Array Map Set WeakMap WeakSet SIMD ArrayBuffer DataView JSON Promise Generator GeneratorFunction Reflect Proxy Intl';
  2344. const forbiddenIdentifiers = new Set(`${reservedWords} ${builtins}`.split(' '));
  2345. forbiddenIdentifiers.add('');
  2346. const makeLegalIdentifier = function makeLegalIdentifier(str) {
  2347. let identifier = str
  2348. .replace(/-(\w)/g, (_, letter) => letter.toUpperCase())
  2349. .replace(/[^$_a-zA-Z0-9]/g, '_');
  2350. if (/\d/.test(identifier[0]) || forbiddenIdentifiers.has(identifier)) {
  2351. identifier = `_${identifier}`;
  2352. }
  2353. return identifier || '_';
  2354. };
  2355. function stringify$8(obj) {
  2356. return (JSON.stringify(obj) || 'undefined').replace(/[\u2028\u2029]/g, (char) => `\\u${`000${char.charCodeAt(0).toString(16)}`.slice(-4)}`);
  2357. }
  2358. function serializeArray(arr, indent, baseIndent) {
  2359. let output = '[';
  2360. const separator = indent ? `\n${baseIndent}${indent}` : '';
  2361. for (let i = 0; i < arr.length; i++) {
  2362. const key = arr[i];
  2363. output += `${i > 0 ? ',' : ''}${separator}${serialize(key, indent, baseIndent + indent)}`;
  2364. }
  2365. return `${output}${indent ? `\n${baseIndent}` : ''}]`;
  2366. }
  2367. function serializeObject(obj, indent, baseIndent) {
  2368. let output = '{';
  2369. const separator = indent ? `\n${baseIndent}${indent}` : '';
  2370. const entries = Object.entries(obj);
  2371. for (let i = 0; i < entries.length; i++) {
  2372. const [key, value] = entries[i];
  2373. const stringKey = makeLegalIdentifier(key) === key ? key : stringify$8(key);
  2374. output += `${i > 0 ? ',' : ''}${separator}${stringKey}:${indent ? ' ' : ''}${serialize(value, indent, baseIndent + indent)}`;
  2375. }
  2376. return `${output}${indent ? `\n${baseIndent}` : ''}}`;
  2377. }
  2378. function serialize(obj, indent, baseIndent) {
  2379. if (typeof obj === 'object' && obj !== null) {
  2380. if (Array.isArray(obj))
  2381. return serializeArray(obj, indent, baseIndent);
  2382. if (obj instanceof Date)
  2383. return `new Date(${obj.getTime()})`;
  2384. if (obj instanceof RegExp)
  2385. return obj.toString();
  2386. return serializeObject(obj, indent, baseIndent);
  2387. }
  2388. if (typeof obj === 'number') {
  2389. if (obj === Infinity)
  2390. return 'Infinity';
  2391. if (obj === -Infinity)
  2392. return '-Infinity';
  2393. if (obj === 0)
  2394. return 1 / obj === Infinity ? '0' : '-0';
  2395. if (obj !== obj)
  2396. return 'NaN'; // eslint-disable-line no-self-compare
  2397. }
  2398. if (typeof obj === 'symbol') {
  2399. const key = Symbol.keyFor(obj);
  2400. // eslint-disable-next-line no-undefined
  2401. if (key !== undefined)
  2402. return `Symbol.for(${stringify$8(key)})`;
  2403. }
  2404. if (typeof obj === 'bigint')
  2405. return `${obj}n`;
  2406. return stringify$8(obj);
  2407. }
  2408. // isWellFormed exists from Node.js 20
  2409. const hasStringIsWellFormed = 'isWellFormed' in String.prototype;
  2410. function isWellFormedString(input) {
  2411. // @ts-expect-error String::isWellFormed exists from ES2024. tsconfig lib is set to ES6
  2412. if (hasStringIsWellFormed)
  2413. return input.isWellFormed();
  2414. // https://github.com/tc39/proposal-is-usv-string/blob/main/README.md#algorithm
  2415. return !/\p{Surrogate}/u.test(input);
  2416. }
  2417. const dataToEsm = function dataToEsm(data, options = {}) {
  2418. var _a, _b;
  2419. const t = options.compact ? '' : 'indent' in options ? options.indent : '\t';
  2420. const _ = options.compact ? '' : ' ';
  2421. const n = options.compact ? '' : '\n';
  2422. const declarationType = options.preferConst ? 'const' : 'var';
  2423. if (options.namedExports === false ||
  2424. typeof data !== 'object' ||
  2425. Array.isArray(data) ||
  2426. data instanceof Date ||
  2427. data instanceof RegExp ||
  2428. data === null) {
  2429. const code = serialize(data, options.compact ? null : t, '');
  2430. const magic = _ || (/^[{[\-\/]/.test(code) ? '' : ' '); // eslint-disable-line no-useless-escape
  2431. return `export default${magic}${code};`;
  2432. }
  2433. let maxUnderbarPrefixLength = 0;
  2434. for (const key of Object.keys(data)) {
  2435. const underbarPrefixLength = (_b = (_a = key.match(/^(_+)/)) === null || _a === void 0 ? void 0 : _a[0].length) !== null && _b !== void 0 ? _b : 0;
  2436. if (underbarPrefixLength > maxUnderbarPrefixLength) {
  2437. maxUnderbarPrefixLength = underbarPrefixLength;
  2438. }
  2439. }
  2440. const arbitraryNamePrefix = `${'_'.repeat(maxUnderbarPrefixLength + 1)}arbitrary`;
  2441. let namedExportCode = '';
  2442. const defaultExportRows = [];
  2443. const arbitraryNameExportRows = [];
  2444. for (const [key, value] of Object.entries(data)) {
  2445. if (key === makeLegalIdentifier(key)) {
  2446. if (options.objectShorthand)
  2447. defaultExportRows.push(key);
  2448. else
  2449. defaultExportRows.push(`${key}:${_}${key}`);
  2450. namedExportCode += `export ${declarationType} ${key}${_}=${_}${serialize(value, options.compact ? null : t, '')};${n}`;
  2451. }
  2452. else {
  2453. defaultExportRows.push(`${stringify$8(key)}:${_}${serialize(value, options.compact ? null : t, '')}`);
  2454. if (options.includeArbitraryNames && isWellFormedString(key)) {
  2455. const variableName = `${arbitraryNamePrefix}${arbitraryNameExportRows.length}`;
  2456. namedExportCode += `${declarationType} ${variableName}${_}=${_}${serialize(value, options.compact ? null : t, '')};${n}`;
  2457. arbitraryNameExportRows.push(`${variableName} as ${JSON.stringify(key)}`);
  2458. }
  2459. }
  2460. }
  2461. const arbitraryExportCode = arbitraryNameExportRows.length > 0
  2462. ? `export${_}{${n}${t}${arbitraryNameExportRows.join(`,${n}${t}`)}${n}};${n}`
  2463. : '';
  2464. const defaultExportCode = `export default${_}{${n}${t}${defaultExportRows.join(`,${n}${t}`)}${n}};${n}`;
  2465. return `${namedExportCode}${arbitraryExportCode}${defaultExportCode}`;
  2466. };
  2467. var path$k = require$$0$4;
  2468. var commondir = function (basedir, relfiles) {
  2469. if (relfiles) {
  2470. var files = relfiles.map(function (r) {
  2471. return path$k.resolve(basedir, r);
  2472. });
  2473. }
  2474. else {
  2475. var files = basedir;
  2476. }
  2477. var res = files.slice(1).reduce(function (ps, file) {
  2478. if (!file.match(/^([A-Za-z]:)?\/|\\/)) {
  2479. throw new Error('relative path without a basedir');
  2480. }
  2481. var xs = file.split(/\/+|\\+/);
  2482. for (
  2483. var i = 0;
  2484. ps[i] === xs[i] && i < Math.min(ps.length, xs.length);
  2485. i++
  2486. );
  2487. return ps.slice(0, i);
  2488. }, files[0].split(/\/+|\\+/));
  2489. // Windows correctly handles paths with forward-slashes
  2490. return res.length > 1 ? res.join('/') : '/'
  2491. };
  2492. var getCommonDir = /*@__PURE__*/getDefaultExportFromCjs(commondir);
  2493. var balancedMatch = balanced$1;
  2494. function balanced$1(a, b, str) {
  2495. if (a instanceof RegExp) a = maybeMatch(a, str);
  2496. if (b instanceof RegExp) b = maybeMatch(b, str);
  2497. var r = range$1(a, b, str);
  2498. return r && {
  2499. start: r[0],
  2500. end: r[1],
  2501. pre: str.slice(0, r[0]),
  2502. body: str.slice(r[0] + a.length, r[1]),
  2503. post: str.slice(r[1] + b.length)
  2504. };
  2505. }
  2506. function maybeMatch(reg, str) {
  2507. var m = str.match(reg);
  2508. return m ? m[0] : null;
  2509. }
  2510. balanced$1.range = range$1;
  2511. function range$1(a, b, str) {
  2512. var begs, beg, left, right, result;
  2513. var ai = str.indexOf(a);
  2514. var bi = str.indexOf(b, ai + 1);
  2515. var i = ai;
  2516. if (ai >= 0 && bi > 0) {
  2517. if(a===b) {
  2518. return [ai, bi];
  2519. }
  2520. begs = [];
  2521. left = str.length;
  2522. while (i >= 0 && !result) {
  2523. if (i == ai) {
  2524. begs.push(i);
  2525. ai = str.indexOf(a, i + 1);
  2526. } else if (begs.length == 1) {
  2527. result = [ begs.pop(), bi ];
  2528. } else {
  2529. beg = begs.pop();
  2530. if (beg < left) {
  2531. left = beg;
  2532. right = bi;
  2533. }
  2534. bi = str.indexOf(b, i + 1);
  2535. }
  2536. i = ai < bi && ai >= 0 ? ai : bi;
  2537. }
  2538. if (begs.length) {
  2539. result = [ left, right ];
  2540. }
  2541. }
  2542. return result;
  2543. }
  2544. var balanced = balancedMatch;
  2545. var braceExpansion = expandTop;
  2546. var escSlash = '\0SLASH'+Math.random()+'\0';
  2547. var escOpen = '\0OPEN'+Math.random()+'\0';
  2548. var escClose = '\0CLOSE'+Math.random()+'\0';
  2549. var escComma = '\0COMMA'+Math.random()+'\0';
  2550. var escPeriod = '\0PERIOD'+Math.random()+'\0';
  2551. function numeric(str) {
  2552. return parseInt(str, 10) == str
  2553. ? parseInt(str, 10)
  2554. : str.charCodeAt(0);
  2555. }
  2556. function escapeBraces(str) {
  2557. return str.split('\\\\').join(escSlash)
  2558. .split('\\{').join(escOpen)
  2559. .split('\\}').join(escClose)
  2560. .split('\\,').join(escComma)
  2561. .split('\\.').join(escPeriod);
  2562. }
  2563. function unescapeBraces(str) {
  2564. return str.split(escSlash).join('\\')
  2565. .split(escOpen).join('{')
  2566. .split(escClose).join('}')
  2567. .split(escComma).join(',')
  2568. .split(escPeriod).join('.');
  2569. }
  2570. // Basically just str.split(","), but handling cases
  2571. // where we have nested braced sections, which should be
  2572. // treated as individual members, like {a,{b,c},d}
  2573. function parseCommaParts(str) {
  2574. if (!str)
  2575. return [''];
  2576. var parts = [];
  2577. var m = balanced('{', '}', str);
  2578. if (!m)
  2579. return str.split(',');
  2580. var pre = m.pre;
  2581. var body = m.body;
  2582. var post = m.post;
  2583. var p = pre.split(',');
  2584. p[p.length-1] += '{' + body + '}';
  2585. var postParts = parseCommaParts(post);
  2586. if (post.length) {
  2587. p[p.length-1] += postParts.shift();
  2588. p.push.apply(p, postParts);
  2589. }
  2590. parts.push.apply(parts, p);
  2591. return parts;
  2592. }
  2593. function expandTop(str) {
  2594. if (!str)
  2595. return [];
  2596. // I don't know why Bash 4.3 does this, but it does.
  2597. // Anything starting with {} will have the first two bytes preserved
  2598. // but *only* at the top level, so {},a}b will not expand to anything,
  2599. // but a{},b}c will be expanded to [a}c,abc].
  2600. // One could argue that this is a bug in Bash, but since the goal of
  2601. // this module is to match Bash's rules, we escape a leading {}
  2602. if (str.substr(0, 2) === '{}') {
  2603. str = '\\{\\}' + str.substr(2);
  2604. }
  2605. return expand$3(escapeBraces(str), true).map(unescapeBraces);
  2606. }
  2607. function embrace(str) {
  2608. return '{' + str + '}';
  2609. }
  2610. function isPadded(el) {
  2611. return /^-?0\d/.test(el);
  2612. }
  2613. function lte(i, y) {
  2614. return i <= y;
  2615. }
  2616. function gte(i, y) {
  2617. return i >= y;
  2618. }
  2619. function expand$3(str, isTop) {
  2620. var expansions = [];
  2621. var m = balanced('{', '}', str);
  2622. if (!m) return [str];
  2623. // no need to expand pre, since it is guaranteed to be free of brace-sets
  2624. var pre = m.pre;
  2625. var post = m.post.length
  2626. ? expand$3(m.post, false)
  2627. : [''];
  2628. if (/\$$/.test(m.pre)) {
  2629. for (var k = 0; k < post.length; k++) {
  2630. var expansion = pre+ '{' + m.body + '}' + post[k];
  2631. expansions.push(expansion);
  2632. }
  2633. } else {
  2634. var isNumericSequence = /^-?\d+\.\.-?\d+(?:\.\.-?\d+)?$/.test(m.body);
  2635. var isAlphaSequence = /^[a-zA-Z]\.\.[a-zA-Z](?:\.\.-?\d+)?$/.test(m.body);
  2636. var isSequence = isNumericSequence || isAlphaSequence;
  2637. var isOptions = m.body.indexOf(',') >= 0;
  2638. if (!isSequence && !isOptions) {
  2639. // {a},b}
  2640. if (m.post.match(/,.*\}/)) {
  2641. str = m.pre + '{' + m.body + escClose + m.post;
  2642. return expand$3(str);
  2643. }
  2644. return [str];
  2645. }
  2646. var n;
  2647. if (isSequence) {
  2648. n = m.body.split(/\.\./);
  2649. } else {
  2650. n = parseCommaParts(m.body);
  2651. if (n.length === 1) {
  2652. // x{{a,b}}y ==> x{a}y x{b}y
  2653. n = expand$3(n[0], false).map(embrace);
  2654. if (n.length === 1) {
  2655. return post.map(function(p) {
  2656. return m.pre + n[0] + p;
  2657. });
  2658. }
  2659. }
  2660. }
  2661. // at this point, n is the parts, and we know it's not a comma set
  2662. // with a single entry.
  2663. var N;
  2664. if (isSequence) {
  2665. var x = numeric(n[0]);
  2666. var y = numeric(n[1]);
  2667. var width = Math.max(n[0].length, n[1].length);
  2668. var incr = n.length == 3
  2669. ? Math.abs(numeric(n[2]))
  2670. : 1;
  2671. var test = lte;
  2672. var reverse = y < x;
  2673. if (reverse) {
  2674. incr *= -1;
  2675. test = gte;
  2676. }
  2677. var pad = n.some(isPadded);
  2678. N = [];
  2679. for (var i = x; test(i, y); i += incr) {
  2680. var c;
  2681. if (isAlphaSequence) {
  2682. c = String.fromCharCode(i);
  2683. if (c === '\\')
  2684. c = '';
  2685. } else {
  2686. c = String(i);
  2687. if (pad) {
  2688. var need = width - c.length;
  2689. if (need > 0) {
  2690. var z = new Array(need + 1).join('0');
  2691. if (i < 0)
  2692. c = '-' + z + c.slice(1);
  2693. else
  2694. c = z + c;
  2695. }
  2696. }
  2697. }
  2698. N.push(c);
  2699. }
  2700. } else {
  2701. N = [];
  2702. for (var j = 0; j < n.length; j++) {
  2703. N.push.apply(N, expand$3(n[j], false));
  2704. }
  2705. }
  2706. for (var j = 0; j < N.length; j++) {
  2707. for (var k = 0; k < post.length; k++) {
  2708. var expansion = pre + N[j] + post[k];
  2709. if (!isTop || isSequence || expansion)
  2710. expansions.push(expansion);
  2711. }
  2712. }
  2713. }
  2714. return expansions;
  2715. }
  2716. var expand$4 = /*@__PURE__*/getDefaultExportFromCjs(braceExpansion);
  2717. const MAX_PATTERN_LENGTH = 1024 * 64;
  2718. const assertValidPattern = (pattern) => {
  2719. if (typeof pattern !== 'string') {
  2720. throw new TypeError('invalid pattern');
  2721. }
  2722. if (pattern.length > MAX_PATTERN_LENGTH) {
  2723. throw new TypeError('pattern is too long');
  2724. }
  2725. };
  2726. // translate the various posix character classes into unicode properties
  2727. // this works across all unicode locales
  2728. // { <posix class>: [<translation>, /u flag required, negated]
  2729. const posixClasses = {
  2730. '[:alnum:]': ['\\p{L}\\p{Nl}\\p{Nd}', true],
  2731. '[:alpha:]': ['\\p{L}\\p{Nl}', true],
  2732. '[:ascii:]': ['\\x' + '00-\\x' + '7f', false],
  2733. '[:blank:]': ['\\p{Zs}\\t', true],
  2734. '[:cntrl:]': ['\\p{Cc}', true],
  2735. '[:digit:]': ['\\p{Nd}', true],
  2736. '[:graph:]': ['\\p{Z}\\p{C}', true, true],
  2737. '[:lower:]': ['\\p{Ll}', true],
  2738. '[:print:]': ['\\p{C}', true],
  2739. '[:punct:]': ['\\p{P}', true],
  2740. '[:space:]': ['\\p{Z}\\t\\r\\n\\v\\f', true],
  2741. '[:upper:]': ['\\p{Lu}', true],
  2742. '[:word:]': ['\\p{L}\\p{Nl}\\p{Nd}\\p{Pc}', true],
  2743. '[:xdigit:]': ['A-Fa-f0-9', false],
  2744. };
  2745. // only need to escape a few things inside of brace expressions
  2746. // escapes: [ \ ] -
  2747. const braceEscape = (s) => s.replace(/[[\]\\-]/g, '\\$&');
  2748. // escape all regexp magic characters
  2749. const regexpEscape = (s) => s.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
  2750. // everything has already been escaped, we just have to join
  2751. const rangesToString = (ranges) => ranges.join('');
  2752. // takes a glob string at a posix brace expression, and returns
  2753. // an equivalent regular expression source, and boolean indicating
  2754. // whether the /u flag needs to be applied, and the number of chars
  2755. // consumed to parse the character class.
  2756. // This also removes out of order ranges, and returns ($.) if the
  2757. // entire class just no good.
  2758. const parseClass = (glob, position) => {
  2759. const pos = position;
  2760. /* c8 ignore start */
  2761. if (glob.charAt(pos) !== '[') {
  2762. throw new Error('not in a brace expression');
  2763. }
  2764. /* c8 ignore stop */
  2765. const ranges = [];
  2766. const negs = [];
  2767. let i = pos + 1;
  2768. let sawStart = false;
  2769. let uflag = false;
  2770. let escaping = false;
  2771. let negate = false;
  2772. let endPos = pos;
  2773. let rangeStart = '';
  2774. WHILE: while (i < glob.length) {
  2775. const c = glob.charAt(i);
  2776. if ((c === '!' || c === '^') && i === pos + 1) {
  2777. negate = true;
  2778. i++;
  2779. continue;
  2780. }
  2781. if (c === ']' && sawStart && !escaping) {
  2782. endPos = i + 1;
  2783. break;
  2784. }
  2785. sawStart = true;
  2786. if (c === '\\') {
  2787. if (!escaping) {
  2788. escaping = true;
  2789. i++;
  2790. continue;
  2791. }
  2792. // escaped \ char, fall through and treat like normal char
  2793. }
  2794. if (c === '[' && !escaping) {
  2795. // either a posix class, a collation equivalent, or just a [
  2796. for (const [cls, [unip, u, neg]] of Object.entries(posixClasses)) {
  2797. if (glob.startsWith(cls, i)) {
  2798. // invalid, [a-[] is fine, but not [a-[:alpha]]
  2799. if (rangeStart) {
  2800. return ['$.', false, glob.length - pos, true];
  2801. }
  2802. i += cls.length;
  2803. if (neg)
  2804. negs.push(unip);
  2805. else
  2806. ranges.push(unip);
  2807. uflag = uflag || u;
  2808. continue WHILE;
  2809. }
  2810. }
  2811. }
  2812. // now it's just a normal character, effectively
  2813. escaping = false;
  2814. if (rangeStart) {
  2815. // throw this range away if it's not valid, but others
  2816. // can still match.
  2817. if (c > rangeStart) {
  2818. ranges.push(braceEscape(rangeStart) + '-' + braceEscape(c));
  2819. }
  2820. else if (c === rangeStart) {
  2821. ranges.push(braceEscape(c));
  2822. }
  2823. rangeStart = '';
  2824. i++;
  2825. continue;
  2826. }
  2827. // now might be the start of a range.
  2828. // can be either c-d or c-] or c<more...>] or c] at this point
  2829. if (glob.startsWith('-]', i + 1)) {
  2830. ranges.push(braceEscape(c + '-'));
  2831. i += 2;
  2832. continue;
  2833. }
  2834. if (glob.startsWith('-', i + 1)) {
  2835. rangeStart = c;
  2836. i += 2;
  2837. continue;
  2838. }
  2839. // not the start of a range, just a single character
  2840. ranges.push(braceEscape(c));
  2841. i++;
  2842. }
  2843. if (endPos < i) {
  2844. // didn't see the end of the class, not a valid class,
  2845. // but might still be valid as a literal match.
  2846. return ['', false, 0, false];
  2847. }
  2848. // if we got no ranges and no negates, then we have a range that
  2849. // cannot possibly match anything, and that poisons the whole glob
  2850. if (!ranges.length && !negs.length) {
  2851. return ['$.', false, glob.length - pos, true];
  2852. }
  2853. // if we got one positive range, and it's a single character, then that's
  2854. // not actually a magic pattern, it's just that one literal character.
  2855. // we should not treat that as "magic", we should just return the literal
  2856. // character. [_] is a perfectly valid way to escape glob magic chars.
  2857. if (negs.length === 0 &&
  2858. ranges.length === 1 &&
  2859. /^\\?.$/.test(ranges[0]) &&
  2860. !negate) {
  2861. const r = ranges[0].length === 2 ? ranges[0].slice(-1) : ranges[0];
  2862. return [regexpEscape(r), false, endPos - pos, false];
  2863. }
  2864. const sranges = '[' + (negate ? '^' : '') + rangesToString(ranges) + ']';
  2865. const snegs = '[' + (negate ? '' : '^') + rangesToString(negs) + ']';
  2866. const comb = ranges.length && negs.length
  2867. ? '(' + sranges + '|' + snegs + ')'
  2868. : ranges.length
  2869. ? sranges
  2870. : snegs;
  2871. return [comb, uflag, endPos - pos, true];
  2872. };
  2873. /**
  2874. * Un-escape a string that has been escaped with {@link escape}.
  2875. *
  2876. * If the {@link windowsPathsNoEscape} option is used, then square-brace
  2877. * escapes are removed, but not backslash escapes. For example, it will turn
  2878. * the string `'[*]'` into `*`, but it will not turn `'\\*'` into `'*'`,
  2879. * becuase `\` is a path separator in `windowsPathsNoEscape` mode.
  2880. *
  2881. * When `windowsPathsNoEscape` is not set, then both brace escapes and
  2882. * backslash escapes are removed.
  2883. *
  2884. * Slashes (and backslashes in `windowsPathsNoEscape` mode) cannot be escaped
  2885. * or unescaped.
  2886. */
  2887. const unescape$1 = (s, { windowsPathsNoEscape = false, } = {}) => {
  2888. return windowsPathsNoEscape
  2889. ? s.replace(/\[([^\/\\])\]/g, '$1')
  2890. : s.replace(/((?!\\).|^)\[([^\/\\])\]/g, '$1$2').replace(/\\([^\/])/g, '$1');
  2891. };
  2892. // parse a single path portion
  2893. const types$1 = new Set(['!', '?', '+', '*', '@']);
  2894. const isExtglobType = (c) => types$1.has(c);
  2895. // Patterns that get prepended to bind to the start of either the
  2896. // entire string, or just a single path portion, to prevent dots
  2897. // and/or traversal patterns, when needed.
  2898. // Exts don't need the ^ or / bit, because the root binds that already.
  2899. const startNoTraversal = '(?!(?:^|/)\\.\\.?(?:$|/))';
  2900. const startNoDot = '(?!\\.)';
  2901. // characters that indicate a start of pattern needs the "no dots" bit,
  2902. // because a dot *might* be matched. ( is not in the list, because in
  2903. // the case of a child extglob, it will handle the prevention itself.
  2904. const addPatternStart = new Set(['[', '.']);
  2905. // cases where traversal is A-OK, no dot prevention needed
  2906. const justDots = new Set(['..', '.']);
  2907. const reSpecials = new Set('().*{}+?[]^$\\!');
  2908. const regExpEscape$1 = (s) => s.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
  2909. // any single thing other than /
  2910. const qmark$1 = '[^/]';
  2911. // * => any number of characters
  2912. const star$1 = qmark$1 + '*?';
  2913. // use + when we need to ensure that *something* matches, because the * is
  2914. // the only thing in the path portion.
  2915. const starNoEmpty = qmark$1 + '+?';
  2916. // remove the \ chars that we added if we end up doing a nonmagic compare
  2917. // const deslash = (s: string) => s.replace(/\\(.)/g, '$1')
  2918. class AST {
  2919. type;
  2920. #root;
  2921. #hasMagic;
  2922. #uflag = false;
  2923. #parts = [];
  2924. #parent;
  2925. #parentIndex;
  2926. #negs;
  2927. #filledNegs = false;
  2928. #options;
  2929. #toString;
  2930. // set to true if it's an extglob with no children
  2931. // (which really means one child of '')
  2932. #emptyExt = false;
  2933. constructor(type, parent, options = {}) {
  2934. this.type = type;
  2935. // extglobs are inherently magical
  2936. if (type)
  2937. this.#hasMagic = true;
  2938. this.#parent = parent;
  2939. this.#root = this.#parent ? this.#parent.#root : this;
  2940. this.#options = this.#root === this ? options : this.#root.#options;
  2941. this.#negs = this.#root === this ? [] : this.#root.#negs;
  2942. if (type === '!' && !this.#root.#filledNegs)
  2943. this.#negs.push(this);
  2944. this.#parentIndex = this.#parent ? this.#parent.#parts.length : 0;
  2945. }
  2946. get hasMagic() {
  2947. /* c8 ignore start */
  2948. if (this.#hasMagic !== undefined)
  2949. return this.#hasMagic;
  2950. /* c8 ignore stop */
  2951. for (const p of this.#parts) {
  2952. if (typeof p === 'string')
  2953. continue;
  2954. if (p.type || p.hasMagic)
  2955. return (this.#hasMagic = true);
  2956. }
  2957. // note: will be undefined until we generate the regexp src and find out
  2958. return this.#hasMagic;
  2959. }
  2960. // reconstructs the pattern
  2961. toString() {
  2962. if (this.#toString !== undefined)
  2963. return this.#toString;
  2964. if (!this.type) {
  2965. return (this.#toString = this.#parts.map(p => String(p)).join(''));
  2966. }
  2967. else {
  2968. return (this.#toString =
  2969. this.type + '(' + this.#parts.map(p => String(p)).join('|') + ')');
  2970. }
  2971. }
  2972. #fillNegs() {
  2973. /* c8 ignore start */
  2974. if (this !== this.#root)
  2975. throw new Error('should only call on root');
  2976. if (this.#filledNegs)
  2977. return this;
  2978. /* c8 ignore stop */
  2979. // call toString() once to fill this out
  2980. this.toString();
  2981. this.#filledNegs = true;
  2982. let n;
  2983. while ((n = this.#negs.pop())) {
  2984. if (n.type !== '!')
  2985. continue;
  2986. // walk up the tree, appending everthing that comes AFTER parentIndex
  2987. let p = n;
  2988. let pp = p.#parent;
  2989. while (pp) {
  2990. for (let i = p.#parentIndex + 1; !pp.type && i < pp.#parts.length; i++) {
  2991. for (const part of n.#parts) {
  2992. /* c8 ignore start */
  2993. if (typeof part === 'string') {
  2994. throw new Error('string part in extglob AST??');
  2995. }
  2996. /* c8 ignore stop */
  2997. part.copyIn(pp.#parts[i]);
  2998. }
  2999. }
  3000. p = pp;
  3001. pp = p.#parent;
  3002. }
  3003. }
  3004. return this;
  3005. }
  3006. push(...parts) {
  3007. for (const p of parts) {
  3008. if (p === '')
  3009. continue;
  3010. /* c8 ignore start */
  3011. if (typeof p !== 'string' && !(p instanceof AST && p.#parent === this)) {
  3012. throw new Error('invalid part: ' + p);
  3013. }
  3014. /* c8 ignore stop */
  3015. this.#parts.push(p);
  3016. }
  3017. }
  3018. toJSON() {
  3019. const ret = this.type === null
  3020. ? this.#parts.slice().map(p => (typeof p === 'string' ? p : p.toJSON()))
  3021. : [this.type, ...this.#parts.map(p => p.toJSON())];
  3022. if (this.isStart() && !this.type)
  3023. ret.unshift([]);
  3024. if (this.isEnd() &&
  3025. (this === this.#root ||
  3026. (this.#root.#filledNegs && this.#parent?.type === '!'))) {
  3027. ret.push({});
  3028. }
  3029. return ret;
  3030. }
  3031. isStart() {
  3032. if (this.#root === this)
  3033. return true;
  3034. // if (this.type) return !!this.#parent?.isStart()
  3035. if (!this.#parent?.isStart())
  3036. return false;
  3037. if (this.#parentIndex === 0)
  3038. return true;
  3039. // if everything AHEAD of this is a negation, then it's still the "start"
  3040. const p = this.#parent;
  3041. for (let i = 0; i < this.#parentIndex; i++) {
  3042. const pp = p.#parts[i];
  3043. if (!(pp instanceof AST && pp.type === '!')) {
  3044. return false;
  3045. }
  3046. }
  3047. return true;
  3048. }
  3049. isEnd() {
  3050. if (this.#root === this)
  3051. return true;
  3052. if (this.#parent?.type === '!')
  3053. return true;
  3054. if (!this.#parent?.isEnd())
  3055. return false;
  3056. if (!this.type)
  3057. return this.#parent?.isEnd();
  3058. // if not root, it'll always have a parent
  3059. /* c8 ignore start */
  3060. const pl = this.#parent ? this.#parent.#parts.length : 0;
  3061. /* c8 ignore stop */
  3062. return this.#parentIndex === pl - 1;
  3063. }
  3064. copyIn(part) {
  3065. if (typeof part === 'string')
  3066. this.push(part);
  3067. else
  3068. this.push(part.clone(this));
  3069. }
  3070. clone(parent) {
  3071. const c = new AST(this.type, parent);
  3072. for (const p of this.#parts) {
  3073. c.copyIn(p);
  3074. }
  3075. return c;
  3076. }
  3077. static #parseAST(str, ast, pos, opt) {
  3078. let escaping = false;
  3079. let inBrace = false;
  3080. let braceStart = -1;
  3081. let braceNeg = false;
  3082. if (ast.type === null) {
  3083. // outside of a extglob, append until we find a start
  3084. let i = pos;
  3085. let acc = '';
  3086. while (i < str.length) {
  3087. const c = str.charAt(i++);
  3088. // still accumulate escapes at this point, but we do ignore
  3089. // starts that are escaped
  3090. if (escaping || c === '\\') {
  3091. escaping = !escaping;
  3092. acc += c;
  3093. continue;
  3094. }
  3095. if (inBrace) {
  3096. if (i === braceStart + 1) {
  3097. if (c === '^' || c === '!') {
  3098. braceNeg = true;
  3099. }
  3100. }
  3101. else if (c === ']' && !(i === braceStart + 2 && braceNeg)) {
  3102. inBrace = false;
  3103. }
  3104. acc += c;
  3105. continue;
  3106. }
  3107. else if (c === '[') {
  3108. inBrace = true;
  3109. braceStart = i;
  3110. braceNeg = false;
  3111. acc += c;
  3112. continue;
  3113. }
  3114. if (!opt.noext && isExtglobType(c) && str.charAt(i) === '(') {
  3115. ast.push(acc);
  3116. acc = '';
  3117. const ext = new AST(c, ast);
  3118. i = AST.#parseAST(str, ext, i, opt);
  3119. ast.push(ext);
  3120. continue;
  3121. }
  3122. acc += c;
  3123. }
  3124. ast.push(acc);
  3125. return i;
  3126. }
  3127. // some kind of extglob, pos is at the (
  3128. // find the next | or )
  3129. let i = pos + 1;
  3130. let part = new AST(null, ast);
  3131. const parts = [];
  3132. let acc = '';
  3133. while (i < str.length) {
  3134. const c = str.charAt(i++);
  3135. // still accumulate escapes at this point, but we do ignore
  3136. // starts that are escaped
  3137. if (escaping || c === '\\') {
  3138. escaping = !escaping;
  3139. acc += c;
  3140. continue;
  3141. }
  3142. if (inBrace) {
  3143. if (i === braceStart + 1) {
  3144. if (c === '^' || c === '!') {
  3145. braceNeg = true;
  3146. }
  3147. }
  3148. else if (c === ']' && !(i === braceStart + 2 && braceNeg)) {
  3149. inBrace = false;
  3150. }
  3151. acc += c;
  3152. continue;
  3153. }
  3154. else if (c === '[') {
  3155. inBrace = true;
  3156. braceStart = i;
  3157. braceNeg = false;
  3158. acc += c;
  3159. continue;
  3160. }
  3161. if (isExtglobType(c) && str.charAt(i) === '(') {
  3162. part.push(acc);
  3163. acc = '';
  3164. const ext = new AST(c, part);
  3165. part.push(ext);
  3166. i = AST.#parseAST(str, ext, i, opt);
  3167. continue;
  3168. }
  3169. if (c === '|') {
  3170. part.push(acc);
  3171. acc = '';
  3172. parts.push(part);
  3173. part = new AST(null, ast);
  3174. continue;
  3175. }
  3176. if (c === ')') {
  3177. if (acc === '' && ast.#parts.length === 0) {
  3178. ast.#emptyExt = true;
  3179. }
  3180. part.push(acc);
  3181. acc = '';
  3182. ast.push(...parts, part);
  3183. return i;
  3184. }
  3185. acc += c;
  3186. }
  3187. // unfinished extglob
  3188. // if we got here, it was a malformed extglob! not an extglob, but
  3189. // maybe something else in there.
  3190. ast.type = null;
  3191. ast.#hasMagic = undefined;
  3192. ast.#parts = [str.substring(pos - 1)];
  3193. return i;
  3194. }
  3195. static fromGlob(pattern, options = {}) {
  3196. const ast = new AST(null, undefined, options);
  3197. AST.#parseAST(pattern, ast, 0, options);
  3198. return ast;
  3199. }
  3200. // returns the regular expression if there's magic, or the unescaped
  3201. // string if not.
  3202. toMMPattern() {
  3203. // should only be called on root
  3204. /* c8 ignore start */
  3205. if (this !== this.#root)
  3206. return this.#root.toMMPattern();
  3207. /* c8 ignore stop */
  3208. const glob = this.toString();
  3209. const [re, body, hasMagic, uflag] = this.toRegExpSource();
  3210. // if we're in nocase mode, and not nocaseMagicOnly, then we do
  3211. // still need a regular expression if we have to case-insensitively
  3212. // match capital/lowercase characters.
  3213. const anyMagic = hasMagic ||
  3214. this.#hasMagic ||
  3215. (this.#options.nocase &&
  3216. !this.#options.nocaseMagicOnly &&
  3217. glob.toUpperCase() !== glob.toLowerCase());
  3218. if (!anyMagic) {
  3219. return body;
  3220. }
  3221. const flags = (this.#options.nocase ? 'i' : '') + (uflag ? 'u' : '');
  3222. return Object.assign(new RegExp(`^${re}$`, flags), {
  3223. _src: re,
  3224. _glob: glob,
  3225. });
  3226. }
  3227. get options() {
  3228. return this.#options;
  3229. }
  3230. // returns the string match, the regexp source, whether there's magic
  3231. // in the regexp (so a regular expression is required) and whether or
  3232. // not the uflag is needed for the regular expression (for posix classes)
  3233. // TODO: instead of injecting the start/end at this point, just return
  3234. // the BODY of the regexp, along with the start/end portions suitable
  3235. // for binding the start/end in either a joined full-path makeRe context
  3236. // (where we bind to (^|/), or a standalone matchPart context (where
  3237. // we bind to ^, and not /). Otherwise slashes get duped!
  3238. //
  3239. // In part-matching mode, the start is:
  3240. // - if not isStart: nothing
  3241. // - if traversal possible, but not allowed: ^(?!\.\.?$)
  3242. // - if dots allowed or not possible: ^
  3243. // - if dots possible and not allowed: ^(?!\.)
  3244. // end is:
  3245. // - if not isEnd(): nothing
  3246. // - else: $
  3247. //
  3248. // In full-path matching mode, we put the slash at the START of the
  3249. // pattern, so start is:
  3250. // - if first pattern: same as part-matching mode
  3251. // - if not isStart(): nothing
  3252. // - if traversal possible, but not allowed: /(?!\.\.?(?:$|/))
  3253. // - if dots allowed or not possible: /
  3254. // - if dots possible and not allowed: /(?!\.)
  3255. // end is:
  3256. // - if last pattern, same as part-matching mode
  3257. // - else nothing
  3258. //
  3259. // Always put the (?:$|/) on negated tails, though, because that has to be
  3260. // there to bind the end of the negated pattern portion, and it's easier to
  3261. // just stick it in now rather than try to inject it later in the middle of
  3262. // the pattern.
  3263. //
  3264. // We can just always return the same end, and leave it up to the caller
  3265. // to know whether it's going to be used joined or in parts.
  3266. // And, if the start is adjusted slightly, can do the same there:
  3267. // - if not isStart: nothing
  3268. // - if traversal possible, but not allowed: (?:/|^)(?!\.\.?$)
  3269. // - if dots allowed or not possible: (?:/|^)
  3270. // - if dots possible and not allowed: (?:/|^)(?!\.)
  3271. //
  3272. // But it's better to have a simpler binding without a conditional, for
  3273. // performance, so probably better to return both start options.
  3274. //
  3275. // Then the caller just ignores the end if it's not the first pattern,
  3276. // and the start always gets applied.
  3277. //
  3278. // But that's always going to be $ if it's the ending pattern, or nothing,
  3279. // so the caller can just attach $ at the end of the pattern when building.
  3280. //
  3281. // So the todo is:
  3282. // - better detect what kind of start is needed
  3283. // - return both flavors of starting pattern
  3284. // - attach $ at the end of the pattern when creating the actual RegExp
  3285. //
  3286. // Ah, but wait, no, that all only applies to the root when the first pattern
  3287. // is not an extglob. If the first pattern IS an extglob, then we need all
  3288. // that dot prevention biz to live in the extglob portions, because eg
  3289. // +(*|.x*) can match .xy but not .yx.
  3290. //
  3291. // So, return the two flavors if it's #root and the first child is not an
  3292. // AST, otherwise leave it to the child AST to handle it, and there,
  3293. // use the (?:^|/) style of start binding.
  3294. //
  3295. // Even simplified further:
  3296. // - Since the start for a join is eg /(?!\.) and the start for a part
  3297. // is ^(?!\.), we can just prepend (?!\.) to the pattern (either root
  3298. // or start or whatever) and prepend ^ or / at the Regexp construction.
  3299. toRegExpSource(allowDot) {
  3300. const dot = allowDot ?? !!this.#options.dot;
  3301. if (this.#root === this)
  3302. this.#fillNegs();
  3303. if (!this.type) {
  3304. const noEmpty = this.isStart() && this.isEnd();
  3305. const src = this.#parts
  3306. .map(p => {
  3307. const [re, _, hasMagic, uflag] = typeof p === 'string'
  3308. ? AST.#parseGlob(p, this.#hasMagic, noEmpty)
  3309. : p.toRegExpSource(allowDot);
  3310. this.#hasMagic = this.#hasMagic || hasMagic;
  3311. this.#uflag = this.#uflag || uflag;
  3312. return re;
  3313. })
  3314. .join('');
  3315. let start = '';
  3316. if (this.isStart()) {
  3317. if (typeof this.#parts[0] === 'string') {
  3318. // this is the string that will match the start of the pattern,
  3319. // so we need to protect against dots and such.
  3320. // '.' and '..' cannot match unless the pattern is that exactly,
  3321. // even if it starts with . or dot:true is set.
  3322. const dotTravAllowed = this.#parts.length === 1 && justDots.has(this.#parts[0]);
  3323. if (!dotTravAllowed) {
  3324. const aps = addPatternStart;
  3325. // check if we have a possibility of matching . or ..,
  3326. // and prevent that.
  3327. const needNoTrav =
  3328. // dots are allowed, and the pattern starts with [ or .
  3329. (dot && aps.has(src.charAt(0))) ||
  3330. // the pattern starts with \., and then [ or .
  3331. (src.startsWith('\\.') && aps.has(src.charAt(2))) ||
  3332. // the pattern starts with \.\., and then [ or .
  3333. (src.startsWith('\\.\\.') && aps.has(src.charAt(4)));
  3334. // no need to prevent dots if it can't match a dot, or if a
  3335. // sub-pattern will be preventing it anyway.
  3336. const needNoDot = !dot && !allowDot && aps.has(src.charAt(0));
  3337. start = needNoTrav ? startNoTraversal : needNoDot ? startNoDot : '';
  3338. }
  3339. }
  3340. }
  3341. // append the "end of path portion" pattern to negation tails
  3342. let end = '';
  3343. if (this.isEnd() &&
  3344. this.#root.#filledNegs &&
  3345. this.#parent?.type === '!') {
  3346. end = '(?:$|\\/)';
  3347. }
  3348. const final = start + src + end;
  3349. return [
  3350. final,
  3351. unescape$1(src),
  3352. (this.#hasMagic = !!this.#hasMagic),
  3353. this.#uflag,
  3354. ];
  3355. }
  3356. // We need to calculate the body *twice* if it's a repeat pattern
  3357. // at the start, once in nodot mode, then again in dot mode, so a
  3358. // pattern like *(?) can match 'x.y'
  3359. const repeated = this.type === '*' || this.type === '+';
  3360. // some kind of extglob
  3361. const start = this.type === '!' ? '(?:(?!(?:' : '(?:';
  3362. let body = this.#partsToRegExp(dot);
  3363. if (this.isStart() && this.isEnd() && !body && this.type !== '!') {
  3364. // invalid extglob, has to at least be *something* present, if it's
  3365. // the entire path portion.
  3366. const s = this.toString();
  3367. this.#parts = [s];
  3368. this.type = null;
  3369. this.#hasMagic = undefined;
  3370. return [s, unescape$1(this.toString()), false, false];
  3371. }
  3372. // XXX abstract out this map method
  3373. let bodyDotAllowed = !repeated || allowDot || dot || !startNoDot
  3374. ? ''
  3375. : this.#partsToRegExp(true);
  3376. if (bodyDotAllowed === body) {
  3377. bodyDotAllowed = '';
  3378. }
  3379. if (bodyDotAllowed) {
  3380. body = `(?:${body})(?:${bodyDotAllowed})*?`;
  3381. }
  3382. // an empty !() is exactly equivalent to a starNoEmpty
  3383. let final = '';
  3384. if (this.type === '!' && this.#emptyExt) {
  3385. final = (this.isStart() && !dot ? startNoDot : '') + starNoEmpty;
  3386. }
  3387. else {
  3388. const close = this.type === '!'
  3389. ? // !() must match something,but !(x) can match ''
  3390. '))' +
  3391. (this.isStart() && !dot && !allowDot ? startNoDot : '') +
  3392. star$1 +
  3393. ')'
  3394. : this.type === '@'
  3395. ? ')'
  3396. : this.type === '?'
  3397. ? ')?'
  3398. : this.type === '+' && bodyDotAllowed
  3399. ? ')'
  3400. : this.type === '*' && bodyDotAllowed
  3401. ? `)?`
  3402. : `)${this.type}`;
  3403. final = start + body + close;
  3404. }
  3405. return [
  3406. final,
  3407. unescape$1(body),
  3408. (this.#hasMagic = !!this.#hasMagic),
  3409. this.#uflag,
  3410. ];
  3411. }
  3412. #partsToRegExp(dot) {
  3413. return this.#parts
  3414. .map(p => {
  3415. // extglob ASTs should only contain parent ASTs
  3416. /* c8 ignore start */
  3417. if (typeof p === 'string') {
  3418. throw new Error('string type in extglob ast??');
  3419. }
  3420. /* c8 ignore stop */
  3421. // can ignore hasMagic, because extglobs are already always magic
  3422. const [re, _, _hasMagic, uflag] = p.toRegExpSource(dot);
  3423. this.#uflag = this.#uflag || uflag;
  3424. return re;
  3425. })
  3426. .filter(p => !(this.isStart() && this.isEnd()) || !!p)
  3427. .join('|');
  3428. }
  3429. static #parseGlob(glob, hasMagic, noEmpty = false) {
  3430. let escaping = false;
  3431. let re = '';
  3432. let uflag = false;
  3433. for (let i = 0; i < glob.length; i++) {
  3434. const c = glob.charAt(i);
  3435. if (escaping) {
  3436. escaping = false;
  3437. re += (reSpecials.has(c) ? '\\' : '') + c;
  3438. continue;
  3439. }
  3440. if (c === '\\') {
  3441. if (i === glob.length - 1) {
  3442. re += '\\\\';
  3443. }
  3444. else {
  3445. escaping = true;
  3446. }
  3447. continue;
  3448. }
  3449. if (c === '[') {
  3450. const [src, needUflag, consumed, magic] = parseClass(glob, i);
  3451. if (consumed) {
  3452. re += src;
  3453. uflag = uflag || needUflag;
  3454. i += consumed - 1;
  3455. hasMagic = hasMagic || magic;
  3456. continue;
  3457. }
  3458. }
  3459. if (c === '*') {
  3460. if (noEmpty && glob === '*')
  3461. re += starNoEmpty;
  3462. else
  3463. re += star$1;
  3464. hasMagic = true;
  3465. continue;
  3466. }
  3467. if (c === '?') {
  3468. re += qmark$1;
  3469. hasMagic = true;
  3470. continue;
  3471. }
  3472. re += regExpEscape$1(c);
  3473. }
  3474. return [re, unescape$1(glob), !!hasMagic, uflag];
  3475. }
  3476. }
  3477. /**
  3478. * Escape all magic characters in a glob pattern.
  3479. *
  3480. * If the {@link windowsPathsNoEscape | GlobOptions.windowsPathsNoEscape}
  3481. * option is used, then characters are escaped by wrapping in `[]`, because
  3482. * a magic character wrapped in a character class can only be satisfied by
  3483. * that exact character. In this mode, `\` is _not_ escaped, because it is
  3484. * not interpreted as a magic character, but instead as a path separator.
  3485. */
  3486. const escape$2 = (s, { windowsPathsNoEscape = false, } = {}) => {
  3487. // don't need to escape +@! because we escape the parens
  3488. // that make those magic, and escaping ! as [!] isn't valid,
  3489. // because [!]] is a valid glob class meaning not ']'.
  3490. return windowsPathsNoEscape
  3491. ? s.replace(/[?*()[\]]/g, '[$&]')
  3492. : s.replace(/[?*()[\]\\]/g, '\\$&');
  3493. };
  3494. const minimatch = (p, pattern, options = {}) => {
  3495. assertValidPattern(pattern);
  3496. // shortcut: comments match nothing.
  3497. if (!options.nocomment && pattern.charAt(0) === '#') {
  3498. return false;
  3499. }
  3500. return new Minimatch(pattern, options).match(p);
  3501. };
  3502. // Optimized checking for the most common glob patterns.
  3503. const starDotExtRE = /^\*+([^+@!?\*\[\(]*)$/;
  3504. const starDotExtTest = (ext) => (f) => !f.startsWith('.') && f.endsWith(ext);
  3505. const starDotExtTestDot = (ext) => (f) => f.endsWith(ext);
  3506. const starDotExtTestNocase = (ext) => {
  3507. ext = ext.toLowerCase();
  3508. return (f) => !f.startsWith('.') && f.toLowerCase().endsWith(ext);
  3509. };
  3510. const starDotExtTestNocaseDot = (ext) => {
  3511. ext = ext.toLowerCase();
  3512. return (f) => f.toLowerCase().endsWith(ext);
  3513. };
  3514. const starDotStarRE = /^\*+\.\*+$/;
  3515. const starDotStarTest = (f) => !f.startsWith('.') && f.includes('.');
  3516. const starDotStarTestDot = (f) => f !== '.' && f !== '..' && f.includes('.');
  3517. const dotStarRE = /^\.\*+$/;
  3518. const dotStarTest = (f) => f !== '.' && f !== '..' && f.startsWith('.');
  3519. const starRE = /^\*+$/;
  3520. const starTest = (f) => f.length !== 0 && !f.startsWith('.');
  3521. const starTestDot = (f) => f.length !== 0 && f !== '.' && f !== '..';
  3522. const qmarksRE = /^\?+([^+@!?\*\[\(]*)?$/;
  3523. const qmarksTestNocase = ([$0, ext = '']) => {
  3524. const noext = qmarksTestNoExt([$0]);
  3525. if (!ext)
  3526. return noext;
  3527. ext = ext.toLowerCase();
  3528. return (f) => noext(f) && f.toLowerCase().endsWith(ext);
  3529. };
  3530. const qmarksTestNocaseDot = ([$0, ext = '']) => {
  3531. const noext = qmarksTestNoExtDot([$0]);
  3532. if (!ext)
  3533. return noext;
  3534. ext = ext.toLowerCase();
  3535. return (f) => noext(f) && f.toLowerCase().endsWith(ext);
  3536. };
  3537. const qmarksTestDot = ([$0, ext = '']) => {
  3538. const noext = qmarksTestNoExtDot([$0]);
  3539. return !ext ? noext : (f) => noext(f) && f.endsWith(ext);
  3540. };
  3541. const qmarksTest = ([$0, ext = '']) => {
  3542. const noext = qmarksTestNoExt([$0]);
  3543. return !ext ? noext : (f) => noext(f) && f.endsWith(ext);
  3544. };
  3545. const qmarksTestNoExt = ([$0]) => {
  3546. const len = $0.length;
  3547. return (f) => f.length === len && !f.startsWith('.');
  3548. };
  3549. const qmarksTestNoExtDot = ([$0]) => {
  3550. const len = $0.length;
  3551. return (f) => f.length === len && f !== '.' && f !== '..';
  3552. };
  3553. /* c8 ignore start */
  3554. const defaultPlatform$2 = (typeof process === 'object' && process
  3555. ? (typeof process.env === 'object' &&
  3556. process.env &&
  3557. process.env.__MINIMATCH_TESTING_PLATFORM__) ||
  3558. process.platform
  3559. : 'posix');
  3560. const path$j = {
  3561. win32: { sep: '\\' },
  3562. posix: { sep: '/' },
  3563. };
  3564. /* c8 ignore stop */
  3565. const sep = defaultPlatform$2 === 'win32' ? path$j.win32.sep : path$j.posix.sep;
  3566. minimatch.sep = sep;
  3567. const GLOBSTAR$2 = Symbol('globstar **');
  3568. minimatch.GLOBSTAR = GLOBSTAR$2;
  3569. // any single thing other than /
  3570. // don't need to escape / when using new RegExp()
  3571. const qmark = '[^/]';
  3572. // * => any number of characters
  3573. const star = qmark + '*?';
  3574. // ** when dots are allowed. Anything goes, except .. and .
  3575. // not (^ or / followed by one or two dots followed by $ or /),
  3576. // followed by anything, any number of times.
  3577. const twoStarDot = '(?:(?!(?:\\/|^)(?:\\.{1,2})($|\\/)).)*?';
  3578. // not a ^ or / followed by a dot,
  3579. // followed by anything, any number of times.
  3580. const twoStarNoDot = '(?:(?!(?:\\/|^)\\.).)*?';
  3581. const filter$1 = (pattern, options = {}) => (p) => minimatch(p, pattern, options);
  3582. minimatch.filter = filter$1;
  3583. const ext = (a, b = {}) => Object.assign({}, a, b);
  3584. const defaults = (def) => {
  3585. if (!def || typeof def !== 'object' || !Object.keys(def).length) {
  3586. return minimatch;
  3587. }
  3588. const orig = minimatch;
  3589. const m = (p, pattern, options = {}) => orig(p, pattern, ext(def, options));
  3590. return Object.assign(m, {
  3591. Minimatch: class Minimatch extends orig.Minimatch {
  3592. constructor(pattern, options = {}) {
  3593. super(pattern, ext(def, options));
  3594. }
  3595. static defaults(options) {
  3596. return orig.defaults(ext(def, options)).Minimatch;
  3597. }
  3598. },
  3599. AST: class AST extends orig.AST {
  3600. /* c8 ignore start */
  3601. constructor(type, parent, options = {}) {
  3602. super(type, parent, ext(def, options));
  3603. }
  3604. /* c8 ignore stop */
  3605. static fromGlob(pattern, options = {}) {
  3606. return orig.AST.fromGlob(pattern, ext(def, options));
  3607. }
  3608. },
  3609. unescape: (s, options = {}) => orig.unescape(s, ext(def, options)),
  3610. escape: (s, options = {}) => orig.escape(s, ext(def, options)),
  3611. filter: (pattern, options = {}) => orig.filter(pattern, ext(def, options)),
  3612. defaults: (options) => orig.defaults(ext(def, options)),
  3613. makeRe: (pattern, options = {}) => orig.makeRe(pattern, ext(def, options)),
  3614. braceExpand: (pattern, options = {}) => orig.braceExpand(pattern, ext(def, options)),
  3615. match: (list, pattern, options = {}) => orig.match(list, pattern, ext(def, options)),
  3616. sep: orig.sep,
  3617. GLOBSTAR: GLOBSTAR$2,
  3618. });
  3619. };
  3620. minimatch.defaults = defaults;
  3621. // Brace expansion:
  3622. // a{b,c}d -> abd acd
  3623. // a{b,}c -> abc ac
  3624. // a{0..3}d -> a0d a1d a2d a3d
  3625. // a{b,c{d,e}f}g -> abg acdfg acefg
  3626. // a{b,c}d{e,f}g -> abdeg acdeg abdeg abdfg
  3627. //
  3628. // Invalid sets are not expanded.
  3629. // a{2..}b -> a{2..}b
  3630. // a{b}c -> a{b}c
  3631. const braceExpand = (pattern, options = {}) => {
  3632. assertValidPattern(pattern);
  3633. // Thanks to Yeting Li <https://github.com/yetingli> for
  3634. // improving this regexp to avoid a ReDOS vulnerability.
  3635. if (options.nobrace || !/\{(?:(?!\{).)*\}/.test(pattern)) {
  3636. // shortcut. no need to expand.
  3637. return [pattern];
  3638. }
  3639. return expand$4(pattern);
  3640. };
  3641. minimatch.braceExpand = braceExpand;
  3642. // parse a component of the expanded set.
  3643. // At this point, no pattern may contain "/" in it
  3644. // so we're going to return a 2d array, where each entry is the full
  3645. // pattern, split on '/', and then turned into a regular expression.
  3646. // A regexp is made at the end which joins each array with an
  3647. // escaped /, and another full one which joins each regexp with |.
  3648. //
  3649. // Following the lead of Bash 4.1, note that "**" only has special meaning
  3650. // when it is the *only* thing in a path portion. Otherwise, any series
  3651. // of * is equivalent to a single *. Globstar behavior is enabled by
  3652. // default, and can be disabled by setting options.noglobstar.
  3653. const makeRe$1 = (pattern, options = {}) => new Minimatch(pattern, options).makeRe();
  3654. minimatch.makeRe = makeRe$1;
  3655. const match = (list, pattern, options = {}) => {
  3656. const mm = new Minimatch(pattern, options);
  3657. list = list.filter(f => mm.match(f));
  3658. if (mm.options.nonull && !list.length) {
  3659. list.push(pattern);
  3660. }
  3661. return list;
  3662. };
  3663. minimatch.match = match;
  3664. // replace stuff like \* with *
  3665. const globMagic = /[?*]|[+@!]\(.*?\)|\[|\]/;
  3666. const regExpEscape = (s) => s.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
  3667. class Minimatch {
  3668. options;
  3669. set;
  3670. pattern;
  3671. windowsPathsNoEscape;
  3672. nonegate;
  3673. negate;
  3674. comment;
  3675. empty;
  3676. preserveMultipleSlashes;
  3677. partial;
  3678. globSet;
  3679. globParts;
  3680. nocase;
  3681. isWindows;
  3682. platform;
  3683. windowsNoMagicRoot;
  3684. regexp;
  3685. constructor(pattern, options = {}) {
  3686. assertValidPattern(pattern);
  3687. options = options || {};
  3688. this.options = options;
  3689. this.pattern = pattern;
  3690. this.platform = options.platform || defaultPlatform$2;
  3691. this.isWindows = this.platform === 'win32';
  3692. this.windowsPathsNoEscape =
  3693. !!options.windowsPathsNoEscape || options.allowWindowsEscape === false;
  3694. if (this.windowsPathsNoEscape) {
  3695. this.pattern = this.pattern.replace(/\\/g, '/');
  3696. }
  3697. this.preserveMultipleSlashes = !!options.preserveMultipleSlashes;
  3698. this.regexp = null;
  3699. this.negate = false;
  3700. this.nonegate = !!options.nonegate;
  3701. this.comment = false;
  3702. this.empty = false;
  3703. this.partial = !!options.partial;
  3704. this.nocase = !!this.options.nocase;
  3705. this.windowsNoMagicRoot =
  3706. options.windowsNoMagicRoot !== undefined
  3707. ? options.windowsNoMagicRoot
  3708. : !!(this.isWindows && this.nocase);
  3709. this.globSet = [];
  3710. this.globParts = [];
  3711. this.set = [];
  3712. // make the set of regexps etc.
  3713. this.make();
  3714. }
  3715. hasMagic() {
  3716. if (this.options.magicalBraces && this.set.length > 1) {
  3717. return true;
  3718. }
  3719. for (const pattern of this.set) {
  3720. for (const part of pattern) {
  3721. if (typeof part !== 'string')
  3722. return true;
  3723. }
  3724. }
  3725. return false;
  3726. }
  3727. debug(..._) { }
  3728. make() {
  3729. const pattern = this.pattern;
  3730. const options = this.options;
  3731. // empty patterns and comments match nothing.
  3732. if (!options.nocomment && pattern.charAt(0) === '#') {
  3733. this.comment = true;
  3734. return;
  3735. }
  3736. if (!pattern) {
  3737. this.empty = true;
  3738. return;
  3739. }
  3740. // step 1: figure out negation, etc.
  3741. this.parseNegate();
  3742. // step 2: expand braces
  3743. this.globSet = [...new Set(this.braceExpand())];
  3744. if (options.debug) {
  3745. this.debug = (...args) => console.error(...args);
  3746. }
  3747. this.debug(this.pattern, this.globSet);
  3748. // step 3: now we have a set, so turn each one into a series of
  3749. // path-portion matching patterns.
  3750. // These will be regexps, except in the case of "**", which is
  3751. // set to the GLOBSTAR object for globstar behavior,
  3752. // and will not contain any / characters
  3753. //
  3754. // First, we preprocess to make the glob pattern sets a bit simpler
  3755. // and deduped. There are some perf-killing patterns that can cause
  3756. // problems with a glob walk, but we can simplify them down a bit.
  3757. const rawGlobParts = this.globSet.map(s => this.slashSplit(s));
  3758. this.globParts = this.preprocess(rawGlobParts);
  3759. this.debug(this.pattern, this.globParts);
  3760. // glob --> regexps
  3761. let set = this.globParts.map((s, _, __) => {
  3762. if (this.isWindows && this.windowsNoMagicRoot) {
  3763. // check if it's a drive or unc path.
  3764. const isUNC = s[0] === '' &&
  3765. s[1] === '' &&
  3766. (s[2] === '?' || !globMagic.test(s[2])) &&
  3767. !globMagic.test(s[3]);
  3768. const isDrive = /^[a-z]:/i.test(s[0]);
  3769. if (isUNC) {
  3770. return [...s.slice(0, 4), ...s.slice(4).map(ss => this.parse(ss))];
  3771. }
  3772. else if (isDrive) {
  3773. return [s[0], ...s.slice(1).map(ss => this.parse(ss))];
  3774. }
  3775. }
  3776. return s.map(ss => this.parse(ss));
  3777. });
  3778. this.debug(this.pattern, set);
  3779. // filter out everything that didn't compile properly.
  3780. this.set = set.filter(s => s.indexOf(false) === -1);
  3781. // do not treat the ? in UNC paths as magic
  3782. if (this.isWindows) {
  3783. for (let i = 0; i < this.set.length; i++) {
  3784. const p = this.set[i];
  3785. if (p[0] === '' &&
  3786. p[1] === '' &&
  3787. this.globParts[i][2] === '?' &&
  3788. typeof p[3] === 'string' &&
  3789. /^[a-z]:$/i.test(p[3])) {
  3790. p[2] = '?';
  3791. }
  3792. }
  3793. }
  3794. this.debug(this.pattern, this.set);
  3795. }
  3796. // various transforms to equivalent pattern sets that are
  3797. // faster to process in a filesystem walk. The goal is to
  3798. // eliminate what we can, and push all ** patterns as far
  3799. // to the right as possible, even if it increases the number
  3800. // of patterns that we have to process.
  3801. preprocess(globParts) {
  3802. // if we're not in globstar mode, then turn all ** into *
  3803. if (this.options.noglobstar) {
  3804. for (let i = 0; i < globParts.length; i++) {
  3805. for (let j = 0; j < globParts[i].length; j++) {
  3806. if (globParts[i][j] === '**') {
  3807. globParts[i][j] = '*';
  3808. }
  3809. }
  3810. }
  3811. }
  3812. const { optimizationLevel = 1 } = this.options;
  3813. if (optimizationLevel >= 2) {
  3814. // aggressive optimization for the purpose of fs walking
  3815. globParts = this.firstPhasePreProcess(globParts);
  3816. globParts = this.secondPhasePreProcess(globParts);
  3817. }
  3818. else if (optimizationLevel >= 1) {
  3819. // just basic optimizations to remove some .. parts
  3820. globParts = this.levelOneOptimize(globParts);
  3821. }
  3822. else {
  3823. // just collapse multiple ** portions into one
  3824. globParts = this.adjascentGlobstarOptimize(globParts);
  3825. }
  3826. return globParts;
  3827. }
  3828. // just get rid of adjascent ** portions
  3829. adjascentGlobstarOptimize(globParts) {
  3830. return globParts.map(parts => {
  3831. let gs = -1;
  3832. while (-1 !== (gs = parts.indexOf('**', gs + 1))) {
  3833. let i = gs;
  3834. while (parts[i + 1] === '**') {
  3835. i++;
  3836. }
  3837. if (i !== gs) {
  3838. parts.splice(gs, i - gs);
  3839. }
  3840. }
  3841. return parts;
  3842. });
  3843. }
  3844. // get rid of adjascent ** and resolve .. portions
  3845. levelOneOptimize(globParts) {
  3846. return globParts.map(parts => {
  3847. parts = parts.reduce((set, part) => {
  3848. const prev = set[set.length - 1];
  3849. if (part === '**' && prev === '**') {
  3850. return set;
  3851. }
  3852. if (part === '..') {
  3853. if (prev && prev !== '..' && prev !== '.' && prev !== '**') {
  3854. set.pop();
  3855. return set;
  3856. }
  3857. }
  3858. set.push(part);
  3859. return set;
  3860. }, []);
  3861. return parts.length === 0 ? [''] : parts;
  3862. });
  3863. }
  3864. levelTwoFileOptimize(parts) {
  3865. if (!Array.isArray(parts)) {
  3866. parts = this.slashSplit(parts);
  3867. }
  3868. let didSomething = false;
  3869. do {
  3870. didSomething = false;
  3871. // <pre>/<e>/<rest> -> <pre>/<rest>
  3872. if (!this.preserveMultipleSlashes) {
  3873. for (let i = 1; i < parts.length - 1; i++) {
  3874. const p = parts[i];
  3875. // don't squeeze out UNC patterns
  3876. if (i === 1 && p === '' && parts[0] === '')
  3877. continue;
  3878. if (p === '.' || p === '') {
  3879. didSomething = true;
  3880. parts.splice(i, 1);
  3881. i--;
  3882. }
  3883. }
  3884. if (parts[0] === '.' &&
  3885. parts.length === 2 &&
  3886. (parts[1] === '.' || parts[1] === '')) {
  3887. didSomething = true;
  3888. parts.pop();
  3889. }
  3890. }
  3891. // <pre>/<p>/../<rest> -> <pre>/<rest>
  3892. let dd = 0;
  3893. while (-1 !== (dd = parts.indexOf('..', dd + 1))) {
  3894. const p = parts[dd - 1];
  3895. if (p && p !== '.' && p !== '..' && p !== '**') {
  3896. didSomething = true;
  3897. parts.splice(dd - 1, 2);
  3898. dd -= 2;
  3899. }
  3900. }
  3901. } while (didSomething);
  3902. return parts.length === 0 ? [''] : parts;
  3903. }
  3904. // First phase: single-pattern processing
  3905. // <pre> is 1 or more portions
  3906. // <rest> is 1 or more portions
  3907. // <p> is any portion other than ., .., '', or **
  3908. // <e> is . or ''
  3909. //
  3910. // **/.. is *brutal* for filesystem walking performance, because
  3911. // it effectively resets the recursive walk each time it occurs,
  3912. // and ** cannot be reduced out by a .. pattern part like a regexp
  3913. // or most strings (other than .., ., and '') can be.
  3914. //
  3915. // <pre>/**/../<p>/<p>/<rest> -> {<pre>/../<p>/<p>/<rest>,<pre>/**/<p>/<p>/<rest>}
  3916. // <pre>/<e>/<rest> -> <pre>/<rest>
  3917. // <pre>/<p>/../<rest> -> <pre>/<rest>
  3918. // **/**/<rest> -> **/<rest>
  3919. //
  3920. // **/*/<rest> -> */**/<rest> <== not valid because ** doesn't follow
  3921. // this WOULD be allowed if ** did follow symlinks, or * didn't
  3922. firstPhasePreProcess(globParts) {
  3923. let didSomething = false;
  3924. do {
  3925. didSomething = false;
  3926. // <pre>/**/../<p>/<p>/<rest> -> {<pre>/../<p>/<p>/<rest>,<pre>/**/<p>/<p>/<rest>}
  3927. for (let parts of globParts) {
  3928. let gs = -1;
  3929. while (-1 !== (gs = parts.indexOf('**', gs + 1))) {
  3930. let gss = gs;
  3931. while (parts[gss + 1] === '**') {
  3932. // <pre>/**/**/<rest> -> <pre>/**/<rest>
  3933. gss++;
  3934. }
  3935. // eg, if gs is 2 and gss is 4, that means we have 3 **
  3936. // parts, and can remove 2 of them.
  3937. if (gss > gs) {
  3938. parts.splice(gs + 1, gss - gs);
  3939. }
  3940. let next = parts[gs + 1];
  3941. const p = parts[gs + 2];
  3942. const p2 = parts[gs + 3];
  3943. if (next !== '..')
  3944. continue;
  3945. if (!p ||
  3946. p === '.' ||
  3947. p === '..' ||
  3948. !p2 ||
  3949. p2 === '.' ||
  3950. p2 === '..') {
  3951. continue;
  3952. }
  3953. didSomething = true;
  3954. // edit parts in place, and push the new one
  3955. parts.splice(gs, 1);
  3956. const other = parts.slice(0);
  3957. other[gs] = '**';
  3958. globParts.push(other);
  3959. gs--;
  3960. }
  3961. // <pre>/<e>/<rest> -> <pre>/<rest>
  3962. if (!this.preserveMultipleSlashes) {
  3963. for (let i = 1; i < parts.length - 1; i++) {
  3964. const p = parts[i];
  3965. // don't squeeze out UNC patterns
  3966. if (i === 1 && p === '' && parts[0] === '')
  3967. continue;
  3968. if (p === '.' || p === '') {
  3969. didSomething = true;
  3970. parts.splice(i, 1);
  3971. i--;
  3972. }
  3973. }
  3974. if (parts[0] === '.' &&
  3975. parts.length === 2 &&
  3976. (parts[1] === '.' || parts[1] === '')) {
  3977. didSomething = true;
  3978. parts.pop();
  3979. }
  3980. }
  3981. // <pre>/<p>/../<rest> -> <pre>/<rest>
  3982. let dd = 0;
  3983. while (-1 !== (dd = parts.indexOf('..', dd + 1))) {
  3984. const p = parts[dd - 1];
  3985. if (p && p !== '.' && p !== '..' && p !== '**') {
  3986. didSomething = true;
  3987. const needDot = dd === 1 && parts[dd + 1] === '**';
  3988. const splin = needDot ? ['.'] : [];
  3989. parts.splice(dd - 1, 2, ...splin);
  3990. if (parts.length === 0)
  3991. parts.push('');
  3992. dd -= 2;
  3993. }
  3994. }
  3995. }
  3996. } while (didSomething);
  3997. return globParts;
  3998. }
  3999. // second phase: multi-pattern dedupes
  4000. // {<pre>/*/<rest>,<pre>/<p>/<rest>} -> <pre>/*/<rest>
  4001. // {<pre>/<rest>,<pre>/<rest>} -> <pre>/<rest>
  4002. // {<pre>/**/<rest>,<pre>/<rest>} -> <pre>/**/<rest>
  4003. //
  4004. // {<pre>/**/<rest>,<pre>/**/<p>/<rest>} -> <pre>/**/<rest>
  4005. // ^-- not valid because ** doens't follow symlinks
  4006. secondPhasePreProcess(globParts) {
  4007. for (let i = 0; i < globParts.length - 1; i++) {
  4008. for (let j = i + 1; j < globParts.length; j++) {
  4009. const matched = this.partsMatch(globParts[i], globParts[j], !this.preserveMultipleSlashes);
  4010. if (matched) {
  4011. globParts[i] = [];
  4012. globParts[j] = matched;
  4013. break;
  4014. }
  4015. }
  4016. }
  4017. return globParts.filter(gs => gs.length);
  4018. }
  4019. partsMatch(a, b, emptyGSMatch = false) {
  4020. let ai = 0;
  4021. let bi = 0;
  4022. let result = [];
  4023. let which = '';
  4024. while (ai < a.length && bi < b.length) {
  4025. if (a[ai] === b[bi]) {
  4026. result.push(which === 'b' ? b[bi] : a[ai]);
  4027. ai++;
  4028. bi++;
  4029. }
  4030. else if (emptyGSMatch && a[ai] === '**' && b[bi] === a[ai + 1]) {
  4031. result.push(a[ai]);
  4032. ai++;
  4033. }
  4034. else if (emptyGSMatch && b[bi] === '**' && a[ai] === b[bi + 1]) {
  4035. result.push(b[bi]);
  4036. bi++;
  4037. }
  4038. else if (a[ai] === '*' &&
  4039. b[bi] &&
  4040. (this.options.dot || !b[bi].startsWith('.')) &&
  4041. b[bi] !== '**') {
  4042. if (which === 'b')
  4043. return false;
  4044. which = 'a';
  4045. result.push(a[ai]);
  4046. ai++;
  4047. bi++;
  4048. }
  4049. else if (b[bi] === '*' &&
  4050. a[ai] &&
  4051. (this.options.dot || !a[ai].startsWith('.')) &&
  4052. a[ai] !== '**') {
  4053. if (which === 'a')
  4054. return false;
  4055. which = 'b';
  4056. result.push(b[bi]);
  4057. ai++;
  4058. bi++;
  4059. }
  4060. else {
  4061. return false;
  4062. }
  4063. }
  4064. // if we fall out of the loop, it means they two are identical
  4065. // as long as their lengths match
  4066. return a.length === b.length && result;
  4067. }
  4068. parseNegate() {
  4069. if (this.nonegate)
  4070. return;
  4071. const pattern = this.pattern;
  4072. let negate = false;
  4073. let negateOffset = 0;
  4074. for (let i = 0; i < pattern.length && pattern.charAt(i) === '!'; i++) {
  4075. negate = !negate;
  4076. negateOffset++;
  4077. }
  4078. if (negateOffset)
  4079. this.pattern = pattern.slice(negateOffset);
  4080. this.negate = negate;
  4081. }
  4082. // set partial to true to test if, for example,
  4083. // "/a/b" matches the start of "/*/b/*/d"
  4084. // Partial means, if you run out of file before you run
  4085. // out of pattern, then that's fine, as long as all
  4086. // the parts match.
  4087. matchOne(file, pattern, partial = false) {
  4088. const options = this.options;
  4089. // UNC paths like //?/X:/... can match X:/... and vice versa
  4090. // Drive letters in absolute drive or unc paths are always compared
  4091. // case-insensitively.
  4092. if (this.isWindows) {
  4093. const fileDrive = typeof file[0] === 'string' && /^[a-z]:$/i.test(file[0]);
  4094. const fileUNC = !fileDrive &&
  4095. file[0] === '' &&
  4096. file[1] === '' &&
  4097. file[2] === '?' &&
  4098. /^[a-z]:$/i.test(file[3]);
  4099. const patternDrive = typeof pattern[0] === 'string' && /^[a-z]:$/i.test(pattern[0]);
  4100. const patternUNC = !patternDrive &&
  4101. pattern[0] === '' &&
  4102. pattern[1] === '' &&
  4103. pattern[2] === '?' &&
  4104. typeof pattern[3] === 'string' &&
  4105. /^[a-z]:$/i.test(pattern[3]);
  4106. const fdi = fileUNC ? 3 : fileDrive ? 0 : undefined;
  4107. const pdi = patternUNC ? 3 : patternDrive ? 0 : undefined;
  4108. if (typeof fdi === 'number' && typeof pdi === 'number') {
  4109. const [fd, pd] = [file[fdi], pattern[pdi]];
  4110. if (fd.toLowerCase() === pd.toLowerCase()) {
  4111. pattern[pdi] = fd;
  4112. if (pdi > fdi) {
  4113. pattern = pattern.slice(pdi);
  4114. }
  4115. else if (fdi > pdi) {
  4116. file = file.slice(fdi);
  4117. }
  4118. }
  4119. }
  4120. }
  4121. // resolve and reduce . and .. portions in the file as well.
  4122. // dont' need to do the second phase, because it's only one string[]
  4123. const { optimizationLevel = 1 } = this.options;
  4124. if (optimizationLevel >= 2) {
  4125. file = this.levelTwoFileOptimize(file);
  4126. }
  4127. this.debug('matchOne', this, { file, pattern });
  4128. this.debug('matchOne', file.length, pattern.length);
  4129. for (var fi = 0, pi = 0, fl = file.length, pl = pattern.length; fi < fl && pi < pl; fi++, pi++) {
  4130. this.debug('matchOne loop');
  4131. var p = pattern[pi];
  4132. var f = file[fi];
  4133. this.debug(pattern, p, f);
  4134. // should be impossible.
  4135. // some invalid regexp stuff in the set.
  4136. /* c8 ignore start */
  4137. if (p === false) {
  4138. return false;
  4139. }
  4140. /* c8 ignore stop */
  4141. if (p === GLOBSTAR$2) {
  4142. this.debug('GLOBSTAR', [pattern, p, f]);
  4143. // "**"
  4144. // a/**/b/**/c would match the following:
  4145. // a/b/x/y/z/c
  4146. // a/x/y/z/b/c
  4147. // a/b/x/b/x/c
  4148. // a/b/c
  4149. // To do this, take the rest of the pattern after
  4150. // the **, and see if it would match the file remainder.
  4151. // If so, return success.
  4152. // If not, the ** "swallows" a segment, and try again.
  4153. // This is recursively awful.
  4154. //
  4155. // a/**/b/**/c matching a/b/x/y/z/c
  4156. // - a matches a
  4157. // - doublestar
  4158. // - matchOne(b/x/y/z/c, b/**/c)
  4159. // - b matches b
  4160. // - doublestar
  4161. // - matchOne(x/y/z/c, c) -> no
  4162. // - matchOne(y/z/c, c) -> no
  4163. // - matchOne(z/c, c) -> no
  4164. // - matchOne(c, c) yes, hit
  4165. var fr = fi;
  4166. var pr = pi + 1;
  4167. if (pr === pl) {
  4168. this.debug('** at the end');
  4169. // a ** at the end will just swallow the rest.
  4170. // We have found a match.
  4171. // however, it will not swallow /.x, unless
  4172. // options.dot is set.
  4173. // . and .. are *never* matched by **, for explosively
  4174. // exponential reasons.
  4175. for (; fi < fl; fi++) {
  4176. if (file[fi] === '.' ||
  4177. file[fi] === '..' ||
  4178. (!options.dot && file[fi].charAt(0) === '.'))
  4179. return false;
  4180. }
  4181. return true;
  4182. }
  4183. // ok, let's see if we can swallow whatever we can.
  4184. while (fr < fl) {
  4185. var swallowee = file[fr];
  4186. this.debug('\nglobstar while', file, fr, pattern, pr, swallowee);
  4187. // XXX remove this slice. Just pass the start index.
  4188. if (this.matchOne(file.slice(fr), pattern.slice(pr), partial)) {
  4189. this.debug('globstar found match!', fr, fl, swallowee);
  4190. // found a match.
  4191. return true;
  4192. }
  4193. else {
  4194. // can't swallow "." or ".." ever.
  4195. // can only swallow ".foo" when explicitly asked.
  4196. if (swallowee === '.' ||
  4197. swallowee === '..' ||
  4198. (!options.dot && swallowee.charAt(0) === '.')) {
  4199. this.debug('dot detected!', file, fr, pattern, pr);
  4200. break;
  4201. }
  4202. // ** swallows a segment, and continue.
  4203. this.debug('globstar swallow a segment, and continue');
  4204. fr++;
  4205. }
  4206. }
  4207. // no match was found.
  4208. // However, in partial mode, we can't say this is necessarily over.
  4209. /* c8 ignore start */
  4210. if (partial) {
  4211. // ran out of file
  4212. this.debug('\n>>> no match, partial?', file, fr, pattern, pr);
  4213. if (fr === fl) {
  4214. return true;
  4215. }
  4216. }
  4217. /* c8 ignore stop */
  4218. return false;
  4219. }
  4220. // something other than **
  4221. // non-magic patterns just have to match exactly
  4222. // patterns with magic have been turned into regexps.
  4223. let hit;
  4224. if (typeof p === 'string') {
  4225. hit = f === p;
  4226. this.debug('string match', p, f, hit);
  4227. }
  4228. else {
  4229. hit = p.test(f);
  4230. this.debug('pattern match', p, f, hit);
  4231. }
  4232. if (!hit)
  4233. return false;
  4234. }
  4235. // Note: ending in / means that we'll get a final ""
  4236. // at the end of the pattern. This can only match a
  4237. // corresponding "" at the end of the file.
  4238. // If the file ends in /, then it can only match a
  4239. // a pattern that ends in /, unless the pattern just
  4240. // doesn't have any more for it. But, a/b/ should *not*
  4241. // match "a/b/*", even though "" matches against the
  4242. // [^/]*? pattern, except in partial mode, where it might
  4243. // simply not be reached yet.
  4244. // However, a/b/ should still satisfy a/*
  4245. // now either we fell off the end of the pattern, or we're done.
  4246. if (fi === fl && pi === pl) {
  4247. // ran out of pattern and filename at the same time.
  4248. // an exact hit!
  4249. return true;
  4250. }
  4251. else if (fi === fl) {
  4252. // ran out of file, but still had pattern left.
  4253. // this is ok if we're doing the match as part of
  4254. // a glob fs traversal.
  4255. return partial;
  4256. }
  4257. else if (pi === pl) {
  4258. // ran out of pattern, still have file left.
  4259. // this is only acceptable if we're on the very last
  4260. // empty segment of a file with a trailing slash.
  4261. // a/* should match a/b/
  4262. return fi === fl - 1 && file[fi] === '';
  4263. /* c8 ignore start */
  4264. }
  4265. else {
  4266. // should be unreachable.
  4267. throw new Error('wtf?');
  4268. }
  4269. /* c8 ignore stop */
  4270. }
  4271. braceExpand() {
  4272. return braceExpand(this.pattern, this.options);
  4273. }
  4274. parse(pattern) {
  4275. assertValidPattern(pattern);
  4276. const options = this.options;
  4277. // shortcuts
  4278. if (pattern === '**')
  4279. return GLOBSTAR$2;
  4280. if (pattern === '')
  4281. return '';
  4282. // far and away, the most common glob pattern parts are
  4283. // *, *.*, and *.<ext> Add a fast check method for those.
  4284. let m;
  4285. let fastTest = null;
  4286. if ((m = pattern.match(starRE))) {
  4287. fastTest = options.dot ? starTestDot : starTest;
  4288. }
  4289. else if ((m = pattern.match(starDotExtRE))) {
  4290. fastTest = (options.nocase
  4291. ? options.dot
  4292. ? starDotExtTestNocaseDot
  4293. : starDotExtTestNocase
  4294. : options.dot
  4295. ? starDotExtTestDot
  4296. : starDotExtTest)(m[1]);
  4297. }
  4298. else if ((m = pattern.match(qmarksRE))) {
  4299. fastTest = (options.nocase
  4300. ? options.dot
  4301. ? qmarksTestNocaseDot
  4302. : qmarksTestNocase
  4303. : options.dot
  4304. ? qmarksTestDot
  4305. : qmarksTest)(m);
  4306. }
  4307. else if ((m = pattern.match(starDotStarRE))) {
  4308. fastTest = options.dot ? starDotStarTestDot : starDotStarTest;
  4309. }
  4310. else if ((m = pattern.match(dotStarRE))) {
  4311. fastTest = dotStarTest;
  4312. }
  4313. const re = AST.fromGlob(pattern, this.options).toMMPattern();
  4314. if (fastTest && typeof re === 'object') {
  4315. // Avoids overriding in frozen environments
  4316. Reflect.defineProperty(re, 'test', { value: fastTest });
  4317. }
  4318. return re;
  4319. }
  4320. makeRe() {
  4321. if (this.regexp || this.regexp === false)
  4322. return this.regexp;
  4323. // at this point, this.set is a 2d array of partial
  4324. // pattern strings, or "**".
  4325. //
  4326. // It's better to use .match(). This function shouldn't
  4327. // be used, really, but it's pretty convenient sometimes,
  4328. // when you just want to work with a regex.
  4329. const set = this.set;
  4330. if (!set.length) {
  4331. this.regexp = false;
  4332. return this.regexp;
  4333. }
  4334. const options = this.options;
  4335. const twoStar = options.noglobstar
  4336. ? star
  4337. : options.dot
  4338. ? twoStarDot
  4339. : twoStarNoDot;
  4340. const flags = new Set(options.nocase ? ['i'] : []);
  4341. // regexpify non-globstar patterns
  4342. // if ** is only item, then we just do one twoStar
  4343. // if ** is first, and there are more, prepend (\/|twoStar\/)? to next
  4344. // if ** is last, append (\/twoStar|) to previous
  4345. // if ** is in the middle, append (\/|\/twoStar\/) to previous
  4346. // then filter out GLOBSTAR symbols
  4347. let re = set
  4348. .map(pattern => {
  4349. const pp = pattern.map(p => {
  4350. if (p instanceof RegExp) {
  4351. for (const f of p.flags.split(''))
  4352. flags.add(f);
  4353. }
  4354. return typeof p === 'string'
  4355. ? regExpEscape(p)
  4356. : p === GLOBSTAR$2
  4357. ? GLOBSTAR$2
  4358. : p._src;
  4359. });
  4360. pp.forEach((p, i) => {
  4361. const next = pp[i + 1];
  4362. const prev = pp[i - 1];
  4363. if (p !== GLOBSTAR$2 || prev === GLOBSTAR$2) {
  4364. return;
  4365. }
  4366. if (prev === undefined) {
  4367. if (next !== undefined && next !== GLOBSTAR$2) {
  4368. pp[i + 1] = '(?:\\/|' + twoStar + '\\/)?' + next;
  4369. }
  4370. else {
  4371. pp[i] = twoStar;
  4372. }
  4373. }
  4374. else if (next === undefined) {
  4375. pp[i - 1] = prev + '(?:\\/|' + twoStar + ')?';
  4376. }
  4377. else if (next !== GLOBSTAR$2) {
  4378. pp[i - 1] = prev + '(?:\\/|\\/' + twoStar + '\\/)' + next;
  4379. pp[i + 1] = GLOBSTAR$2;
  4380. }
  4381. });
  4382. return pp.filter(p => p !== GLOBSTAR$2).join('/');
  4383. })
  4384. .join('|');
  4385. // need to wrap in parens if we had more than one thing with |,
  4386. // otherwise only the first will be anchored to ^ and the last to $
  4387. const [open, close] = set.length > 1 ? ['(?:', ')'] : ['', ''];
  4388. // must match entire pattern
  4389. // ending in a * or ** will make it less strict.
  4390. re = '^' + open + re + close + '$';
  4391. // can match anything, as long as it's not this.
  4392. if (this.negate)
  4393. re = '^(?!' + re + ').+$';
  4394. try {
  4395. this.regexp = new RegExp(re, [...flags].join(''));
  4396. /* c8 ignore start */
  4397. }
  4398. catch (ex) {
  4399. // should be impossible
  4400. this.regexp = false;
  4401. }
  4402. /* c8 ignore stop */
  4403. return this.regexp;
  4404. }
  4405. slashSplit(p) {
  4406. // if p starts with // on windows, we preserve that
  4407. // so that UNC paths aren't broken. Otherwise, any number of
  4408. // / characters are coalesced into one, unless
  4409. // preserveMultipleSlashes is set to true.
  4410. if (this.preserveMultipleSlashes) {
  4411. return p.split('/');
  4412. }
  4413. else if (this.isWindows && /^\/\/[^\/]+/.test(p)) {
  4414. // add an extra '' for the one we lose
  4415. return ['', ...p.split(/\/+/)];
  4416. }
  4417. else {
  4418. return p.split(/\/+/);
  4419. }
  4420. }
  4421. match(f, partial = this.partial) {
  4422. this.debug('match', f, this.pattern);
  4423. // short-circuit in the case of busted things.
  4424. // comments, etc.
  4425. if (this.comment) {
  4426. return false;
  4427. }
  4428. if (this.empty) {
  4429. return f === '';
  4430. }
  4431. if (f === '/' && partial) {
  4432. return true;
  4433. }
  4434. const options = this.options;
  4435. // windows: need to use /, not \
  4436. if (this.isWindows) {
  4437. f = f.split('\\').join('/');
  4438. }
  4439. // treat the test path as a set of pathparts.
  4440. const ff = this.slashSplit(f);
  4441. this.debug(this.pattern, 'split', ff);
  4442. // just ONE of the pattern sets in this.set needs to match
  4443. // in order for it to be valid. If negating, then just one
  4444. // match means that we have failed.
  4445. // Either way, return on the first hit.
  4446. const set = this.set;
  4447. this.debug(this.pattern, 'set', set);
  4448. // Find the basename of the path by looking for the last non-empty segment
  4449. let filename = ff[ff.length - 1];
  4450. if (!filename) {
  4451. for (let i = ff.length - 2; !filename && i >= 0; i--) {
  4452. filename = ff[i];
  4453. }
  4454. }
  4455. for (let i = 0; i < set.length; i++) {
  4456. const pattern = set[i];
  4457. let file = ff;
  4458. if (options.matchBase && pattern.length === 1) {
  4459. file = [filename];
  4460. }
  4461. const hit = this.matchOne(file, pattern, partial);
  4462. if (hit) {
  4463. if (options.flipNegate) {
  4464. return true;
  4465. }
  4466. return !this.negate;
  4467. }
  4468. }
  4469. // didn't get any hits. this is success if it's a negative
  4470. // pattern, failure otherwise.
  4471. if (options.flipNegate) {
  4472. return false;
  4473. }
  4474. return this.negate;
  4475. }
  4476. static defaults(def) {
  4477. return minimatch.defaults(def).Minimatch;
  4478. }
  4479. }
  4480. /* c8 ignore stop */
  4481. minimatch.AST = AST;
  4482. minimatch.Minimatch = Minimatch;
  4483. minimatch.escape = escape$2;
  4484. minimatch.unescape = unescape$1;
  4485. /**
  4486. * @module LRUCache
  4487. */
  4488. const perf = typeof performance === 'object' &&
  4489. performance &&
  4490. typeof performance.now === 'function'
  4491. ? performance
  4492. : Date;
  4493. const warned$1 = new Set();
  4494. /* c8 ignore start */
  4495. const PROCESS = (typeof process === 'object' && !!process ? process : {});
  4496. /* c8 ignore start */
  4497. const emitWarning = (msg, type, code, fn) => {
  4498. typeof PROCESS.emitWarning === 'function'
  4499. ? PROCESS.emitWarning(msg, type, code, fn)
  4500. : console.error(`[${code}] ${type}: ${msg}`);
  4501. };
  4502. let AC = globalThis.AbortController;
  4503. let AS = globalThis.AbortSignal;
  4504. /* c8 ignore start */
  4505. if (typeof AC === 'undefined') {
  4506. //@ts-ignore
  4507. AS = class AbortSignal {
  4508. onabort;
  4509. _onabort = [];
  4510. reason;
  4511. aborted = false;
  4512. addEventListener(_, fn) {
  4513. this._onabort.push(fn);
  4514. }
  4515. };
  4516. //@ts-ignore
  4517. AC = class AbortController {
  4518. constructor() {
  4519. warnACPolyfill();
  4520. }
  4521. signal = new AS();
  4522. abort(reason) {
  4523. if (this.signal.aborted)
  4524. return;
  4525. //@ts-ignore
  4526. this.signal.reason = reason;
  4527. //@ts-ignore
  4528. this.signal.aborted = true;
  4529. //@ts-ignore
  4530. for (const fn of this.signal._onabort) {
  4531. fn(reason);
  4532. }
  4533. this.signal.onabort?.(reason);
  4534. }
  4535. };
  4536. let printACPolyfillWarning = PROCESS.env?.LRU_CACHE_IGNORE_AC_WARNING !== '1';
  4537. const warnACPolyfill = () => {
  4538. if (!printACPolyfillWarning)
  4539. return;
  4540. printACPolyfillWarning = false;
  4541. emitWarning('AbortController is not defined. If using lru-cache in ' +
  4542. 'node 14, load an AbortController polyfill from the ' +
  4543. '`node-abort-controller` package. A minimal polyfill is ' +
  4544. 'provided for use by LRUCache.fetch(), but it should not be ' +
  4545. 'relied upon in other contexts (eg, passing it to other APIs that ' +
  4546. 'use AbortController/AbortSignal might have undesirable effects). ' +
  4547. 'You may disable this with LRU_CACHE_IGNORE_AC_WARNING=1 in the env.', 'NO_ABORT_CONTROLLER', 'ENOTSUP', warnACPolyfill);
  4548. };
  4549. }
  4550. /* c8 ignore stop */
  4551. const shouldWarn = (code) => !warned$1.has(code);
  4552. const isPosInt = (n) => n && n === Math.floor(n) && n > 0 && isFinite(n);
  4553. /* c8 ignore start */
  4554. // This is a little bit ridiculous, tbh.
  4555. // The maximum array length is 2^32-1 or thereabouts on most JS impls.
  4556. // And well before that point, you're caching the entire world, I mean,
  4557. // that's ~32GB of just integers for the next/prev links, plus whatever
  4558. // else to hold that many keys and values. Just filling the memory with
  4559. // zeroes at init time is brutal when you get that big.
  4560. // But why not be complete?
  4561. // Maybe in the future, these limits will have expanded.
  4562. const getUintArray = (max) => !isPosInt(max)
  4563. ? null
  4564. : max <= Math.pow(2, 8)
  4565. ? Uint8Array
  4566. : max <= Math.pow(2, 16)
  4567. ? Uint16Array
  4568. : max <= Math.pow(2, 32)
  4569. ? Uint32Array
  4570. : max <= Number.MAX_SAFE_INTEGER
  4571. ? ZeroArray
  4572. : null;
  4573. /* c8 ignore stop */
  4574. class ZeroArray extends Array {
  4575. constructor(size) {
  4576. super(size);
  4577. this.fill(0);
  4578. }
  4579. }
  4580. class Stack {
  4581. heap;
  4582. length;
  4583. // private constructor
  4584. static #constructing = false;
  4585. static create(max) {
  4586. const HeapCls = getUintArray(max);
  4587. if (!HeapCls)
  4588. return [];
  4589. Stack.#constructing = true;
  4590. const s = new Stack(max, HeapCls);
  4591. Stack.#constructing = false;
  4592. return s;
  4593. }
  4594. constructor(max, HeapCls) {
  4595. /* c8 ignore start */
  4596. if (!Stack.#constructing) {
  4597. throw new TypeError('instantiate Stack using Stack.create(n)');
  4598. }
  4599. /* c8 ignore stop */
  4600. this.heap = new HeapCls(max);
  4601. this.length = 0;
  4602. }
  4603. push(n) {
  4604. this.heap[this.length++] = n;
  4605. }
  4606. pop() {
  4607. return this.heap[--this.length];
  4608. }
  4609. }
  4610. /**
  4611. * Default export, the thing you're using this module to get.
  4612. *
  4613. * The `K` and `V` types define the key and value types, respectively. The
  4614. * optional `FC` type defines the type of the `context` object passed to
  4615. * `cache.fetch()` and `cache.memo()`.
  4616. *
  4617. * Keys and values **must not** be `null` or `undefined`.
  4618. *
  4619. * All properties from the options object (with the exception of `max`,
  4620. * `maxSize`, `fetchMethod`, `memoMethod`, `dispose` and `disposeAfter`) are
  4621. * added as normal public members. (The listed options are read-only getters.)
  4622. *
  4623. * Changing any of these will alter the defaults for subsequent method calls.
  4624. */
  4625. class LRUCache {
  4626. // options that cannot be changed without disaster
  4627. #max;
  4628. #maxSize;
  4629. #dispose;
  4630. #disposeAfter;
  4631. #fetchMethod;
  4632. #memoMethod;
  4633. /**
  4634. * {@link LRUCache.OptionsBase.ttl}
  4635. */
  4636. ttl;
  4637. /**
  4638. * {@link LRUCache.OptionsBase.ttlResolution}
  4639. */
  4640. ttlResolution;
  4641. /**
  4642. * {@link LRUCache.OptionsBase.ttlAutopurge}
  4643. */
  4644. ttlAutopurge;
  4645. /**
  4646. * {@link LRUCache.OptionsBase.updateAgeOnGet}
  4647. */
  4648. updateAgeOnGet;
  4649. /**
  4650. * {@link LRUCache.OptionsBase.updateAgeOnHas}
  4651. */
  4652. updateAgeOnHas;
  4653. /**
  4654. * {@link LRUCache.OptionsBase.allowStale}
  4655. */
  4656. allowStale;
  4657. /**
  4658. * {@link LRUCache.OptionsBase.noDisposeOnSet}
  4659. */
  4660. noDisposeOnSet;
  4661. /**
  4662. * {@link LRUCache.OptionsBase.noUpdateTTL}
  4663. */
  4664. noUpdateTTL;
  4665. /**
  4666. * {@link LRUCache.OptionsBase.maxEntrySize}
  4667. */
  4668. maxEntrySize;
  4669. /**
  4670. * {@link LRUCache.OptionsBase.sizeCalculation}
  4671. */
  4672. sizeCalculation;
  4673. /**
  4674. * {@link LRUCache.OptionsBase.noDeleteOnFetchRejection}
  4675. */
  4676. noDeleteOnFetchRejection;
  4677. /**
  4678. * {@link LRUCache.OptionsBase.noDeleteOnStaleGet}
  4679. */
  4680. noDeleteOnStaleGet;
  4681. /**
  4682. * {@link LRUCache.OptionsBase.allowStaleOnFetchAbort}
  4683. */
  4684. allowStaleOnFetchAbort;
  4685. /**
  4686. * {@link LRUCache.OptionsBase.allowStaleOnFetchRejection}
  4687. */
  4688. allowStaleOnFetchRejection;
  4689. /**
  4690. * {@link LRUCache.OptionsBase.ignoreFetchAbort}
  4691. */
  4692. ignoreFetchAbort;
  4693. // computed properties
  4694. #size;
  4695. #calculatedSize;
  4696. #keyMap;
  4697. #keyList;
  4698. #valList;
  4699. #next;
  4700. #prev;
  4701. #head;
  4702. #tail;
  4703. #free;
  4704. #disposed;
  4705. #sizes;
  4706. #starts;
  4707. #ttls;
  4708. #hasDispose;
  4709. #hasFetchMethod;
  4710. #hasDisposeAfter;
  4711. /**
  4712. * Do not call this method unless you need to inspect the
  4713. * inner workings of the cache. If anything returned by this
  4714. * object is modified in any way, strange breakage may occur.
  4715. *
  4716. * These fields are private for a reason!
  4717. *
  4718. * @internal
  4719. */
  4720. static unsafeExposeInternals(c) {
  4721. return {
  4722. // properties
  4723. starts: c.#starts,
  4724. ttls: c.#ttls,
  4725. sizes: c.#sizes,
  4726. keyMap: c.#keyMap,
  4727. keyList: c.#keyList,
  4728. valList: c.#valList,
  4729. next: c.#next,
  4730. prev: c.#prev,
  4731. get head() {
  4732. return c.#head;
  4733. },
  4734. get tail() {
  4735. return c.#tail;
  4736. },
  4737. free: c.#free,
  4738. // methods
  4739. isBackgroundFetch: (p) => c.#isBackgroundFetch(p),
  4740. backgroundFetch: (k, index, options, context) => c.#backgroundFetch(k, index, options, context),
  4741. moveToTail: (index) => c.#moveToTail(index),
  4742. indexes: (options) => c.#indexes(options),
  4743. rindexes: (options) => c.#rindexes(options),
  4744. isStale: (index) => c.#isStale(index),
  4745. };
  4746. }
  4747. // Protected read-only members
  4748. /**
  4749. * {@link LRUCache.OptionsBase.max} (read-only)
  4750. */
  4751. get max() {
  4752. return this.#max;
  4753. }
  4754. /**
  4755. * {@link LRUCache.OptionsBase.maxSize} (read-only)
  4756. */
  4757. get maxSize() {
  4758. return this.#maxSize;
  4759. }
  4760. /**
  4761. * The total computed size of items in the cache (read-only)
  4762. */
  4763. get calculatedSize() {
  4764. return this.#calculatedSize;
  4765. }
  4766. /**
  4767. * The number of items stored in the cache (read-only)
  4768. */
  4769. get size() {
  4770. return this.#size;
  4771. }
  4772. /**
  4773. * {@link LRUCache.OptionsBase.fetchMethod} (read-only)
  4774. */
  4775. get fetchMethod() {
  4776. return this.#fetchMethod;
  4777. }
  4778. get memoMethod() {
  4779. return this.#memoMethod;
  4780. }
  4781. /**
  4782. * {@link LRUCache.OptionsBase.dispose} (read-only)
  4783. */
  4784. get dispose() {
  4785. return this.#dispose;
  4786. }
  4787. /**
  4788. * {@link LRUCache.OptionsBase.disposeAfter} (read-only)
  4789. */
  4790. get disposeAfter() {
  4791. return this.#disposeAfter;
  4792. }
  4793. constructor(options) {
  4794. const { max = 0, ttl, ttlResolution = 1, ttlAutopurge, updateAgeOnGet, updateAgeOnHas, allowStale, dispose, disposeAfter, noDisposeOnSet, noUpdateTTL, maxSize = 0, maxEntrySize = 0, sizeCalculation, fetchMethod, memoMethod, noDeleteOnFetchRejection, noDeleteOnStaleGet, allowStaleOnFetchRejection, allowStaleOnFetchAbort, ignoreFetchAbort, } = options;
  4795. if (max !== 0 && !isPosInt(max)) {
  4796. throw new TypeError('max option must be a nonnegative integer');
  4797. }
  4798. const UintArray = max ? getUintArray(max) : Array;
  4799. if (!UintArray) {
  4800. throw new Error('invalid max value: ' + max);
  4801. }
  4802. this.#max = max;
  4803. this.#maxSize = maxSize;
  4804. this.maxEntrySize = maxEntrySize || this.#maxSize;
  4805. this.sizeCalculation = sizeCalculation;
  4806. if (this.sizeCalculation) {
  4807. if (!this.#maxSize && !this.maxEntrySize) {
  4808. throw new TypeError('cannot set sizeCalculation without setting maxSize or maxEntrySize');
  4809. }
  4810. if (typeof this.sizeCalculation !== 'function') {
  4811. throw new TypeError('sizeCalculation set to non-function');
  4812. }
  4813. }
  4814. if (memoMethod !== undefined &&
  4815. typeof memoMethod !== 'function') {
  4816. throw new TypeError('memoMethod must be a function if defined');
  4817. }
  4818. this.#memoMethod = memoMethod;
  4819. if (fetchMethod !== undefined &&
  4820. typeof fetchMethod !== 'function') {
  4821. throw new TypeError('fetchMethod must be a function if specified');
  4822. }
  4823. this.#fetchMethod = fetchMethod;
  4824. this.#hasFetchMethod = !!fetchMethod;
  4825. this.#keyMap = new Map();
  4826. this.#keyList = new Array(max).fill(undefined);
  4827. this.#valList = new Array(max).fill(undefined);
  4828. this.#next = new UintArray(max);
  4829. this.#prev = new UintArray(max);
  4830. this.#head = 0;
  4831. this.#tail = 0;
  4832. this.#free = Stack.create(max);
  4833. this.#size = 0;
  4834. this.#calculatedSize = 0;
  4835. if (typeof dispose === 'function') {
  4836. this.#dispose = dispose;
  4837. }
  4838. if (typeof disposeAfter === 'function') {
  4839. this.#disposeAfter = disposeAfter;
  4840. this.#disposed = [];
  4841. }
  4842. else {
  4843. this.#disposeAfter = undefined;
  4844. this.#disposed = undefined;
  4845. }
  4846. this.#hasDispose = !!this.#dispose;
  4847. this.#hasDisposeAfter = !!this.#disposeAfter;
  4848. this.noDisposeOnSet = !!noDisposeOnSet;
  4849. this.noUpdateTTL = !!noUpdateTTL;
  4850. this.noDeleteOnFetchRejection = !!noDeleteOnFetchRejection;
  4851. this.allowStaleOnFetchRejection = !!allowStaleOnFetchRejection;
  4852. this.allowStaleOnFetchAbort = !!allowStaleOnFetchAbort;
  4853. this.ignoreFetchAbort = !!ignoreFetchAbort;
  4854. // NB: maxEntrySize is set to maxSize if it's set
  4855. if (this.maxEntrySize !== 0) {
  4856. if (this.#maxSize !== 0) {
  4857. if (!isPosInt(this.#maxSize)) {
  4858. throw new TypeError('maxSize must be a positive integer if specified');
  4859. }
  4860. }
  4861. if (!isPosInt(this.maxEntrySize)) {
  4862. throw new TypeError('maxEntrySize must be a positive integer if specified');
  4863. }
  4864. this.#initializeSizeTracking();
  4865. }
  4866. this.allowStale = !!allowStale;
  4867. this.noDeleteOnStaleGet = !!noDeleteOnStaleGet;
  4868. this.updateAgeOnGet = !!updateAgeOnGet;
  4869. this.updateAgeOnHas = !!updateAgeOnHas;
  4870. this.ttlResolution =
  4871. isPosInt(ttlResolution) || ttlResolution === 0
  4872. ? ttlResolution
  4873. : 1;
  4874. this.ttlAutopurge = !!ttlAutopurge;
  4875. this.ttl = ttl || 0;
  4876. if (this.ttl) {
  4877. if (!isPosInt(this.ttl)) {
  4878. throw new TypeError('ttl must be a positive integer if specified');
  4879. }
  4880. this.#initializeTTLTracking();
  4881. }
  4882. // do not allow completely unbounded caches
  4883. if (this.#max === 0 && this.ttl === 0 && this.#maxSize === 0) {
  4884. throw new TypeError('At least one of max, maxSize, or ttl is required');
  4885. }
  4886. if (!this.ttlAutopurge && !this.#max && !this.#maxSize) {
  4887. const code = 'LRU_CACHE_UNBOUNDED';
  4888. if (shouldWarn(code)) {
  4889. warned$1.add(code);
  4890. const msg = 'TTL caching without ttlAutopurge, max, or maxSize can ' +
  4891. 'result in unbounded memory consumption.';
  4892. emitWarning(msg, 'UnboundedCacheWarning', code, LRUCache);
  4893. }
  4894. }
  4895. }
  4896. /**
  4897. * Return the number of ms left in the item's TTL. If item is not in cache,
  4898. * returns `0`. Returns `Infinity` if item is in cache without a defined TTL.
  4899. */
  4900. getRemainingTTL(key) {
  4901. return this.#keyMap.has(key) ? Infinity : 0;
  4902. }
  4903. #initializeTTLTracking() {
  4904. const ttls = new ZeroArray(this.#max);
  4905. const starts = new ZeroArray(this.#max);
  4906. this.#ttls = ttls;
  4907. this.#starts = starts;
  4908. this.#setItemTTL = (index, ttl, start = perf.now()) => {
  4909. starts[index] = ttl !== 0 ? start : 0;
  4910. ttls[index] = ttl;
  4911. if (ttl !== 0 && this.ttlAutopurge) {
  4912. const t = setTimeout(() => {
  4913. if (this.#isStale(index)) {
  4914. this.#delete(this.#keyList[index], 'expire');
  4915. }
  4916. }, ttl + 1);
  4917. // unref() not supported on all platforms
  4918. /* c8 ignore start */
  4919. if (t.unref) {
  4920. t.unref();
  4921. }
  4922. /* c8 ignore stop */
  4923. }
  4924. };
  4925. this.#updateItemAge = index => {
  4926. starts[index] = ttls[index] !== 0 ? perf.now() : 0;
  4927. };
  4928. this.#statusTTL = (status, index) => {
  4929. if (ttls[index]) {
  4930. const ttl = ttls[index];
  4931. const start = starts[index];
  4932. /* c8 ignore next */
  4933. if (!ttl || !start)
  4934. return;
  4935. status.ttl = ttl;
  4936. status.start = start;
  4937. status.now = cachedNow || getNow();
  4938. const age = status.now - start;
  4939. status.remainingTTL = ttl - age;
  4940. }
  4941. };
  4942. // debounce calls to perf.now() to 1s so we're not hitting
  4943. // that costly call repeatedly.
  4944. let cachedNow = 0;
  4945. const getNow = () => {
  4946. const n = perf.now();
  4947. if (this.ttlResolution > 0) {
  4948. cachedNow = n;
  4949. const t = setTimeout(() => (cachedNow = 0), this.ttlResolution);
  4950. // not available on all platforms
  4951. /* c8 ignore start */
  4952. if (t.unref) {
  4953. t.unref();
  4954. }
  4955. /* c8 ignore stop */
  4956. }
  4957. return n;
  4958. };
  4959. this.getRemainingTTL = key => {
  4960. const index = this.#keyMap.get(key);
  4961. if (index === undefined) {
  4962. return 0;
  4963. }
  4964. const ttl = ttls[index];
  4965. const start = starts[index];
  4966. if (!ttl || !start) {
  4967. return Infinity;
  4968. }
  4969. const age = (cachedNow || getNow()) - start;
  4970. return ttl - age;
  4971. };
  4972. this.#isStale = index => {
  4973. const s = starts[index];
  4974. const t = ttls[index];
  4975. return !!t && !!s && (cachedNow || getNow()) - s > t;
  4976. };
  4977. }
  4978. // conditionally set private methods related to TTL
  4979. #updateItemAge = () => { };
  4980. #statusTTL = () => { };
  4981. #setItemTTL = () => { };
  4982. /* c8 ignore stop */
  4983. #isStale = () => false;
  4984. #initializeSizeTracking() {
  4985. const sizes = new ZeroArray(this.#max);
  4986. this.#calculatedSize = 0;
  4987. this.#sizes = sizes;
  4988. this.#removeItemSize = index => {
  4989. this.#calculatedSize -= sizes[index];
  4990. sizes[index] = 0;
  4991. };
  4992. this.#requireSize = (k, v, size, sizeCalculation) => {
  4993. // provisionally accept background fetches.
  4994. // actual value size will be checked when they return.
  4995. if (this.#isBackgroundFetch(v)) {
  4996. return 0;
  4997. }
  4998. if (!isPosInt(size)) {
  4999. if (sizeCalculation) {
  5000. if (typeof sizeCalculation !== 'function') {
  5001. throw new TypeError('sizeCalculation must be a function');
  5002. }
  5003. size = sizeCalculation(v, k);
  5004. if (!isPosInt(size)) {
  5005. throw new TypeError('sizeCalculation return invalid (expect positive integer)');
  5006. }
  5007. }
  5008. else {
  5009. throw new TypeError('invalid size value (must be positive integer). ' +
  5010. 'When maxSize or maxEntrySize is used, sizeCalculation ' +
  5011. 'or size must be set.');
  5012. }
  5013. }
  5014. return size;
  5015. };
  5016. this.#addItemSize = (index, size, status) => {
  5017. sizes[index] = size;
  5018. if (this.#maxSize) {
  5019. const maxSize = this.#maxSize - sizes[index];
  5020. while (this.#calculatedSize > maxSize) {
  5021. this.#evict(true);
  5022. }
  5023. }
  5024. this.#calculatedSize += sizes[index];
  5025. if (status) {
  5026. status.entrySize = size;
  5027. status.totalCalculatedSize = this.#calculatedSize;
  5028. }
  5029. };
  5030. }
  5031. #removeItemSize = _i => { };
  5032. #addItemSize = (_i, _s, _st) => { };
  5033. #requireSize = (_k, _v, size, sizeCalculation) => {
  5034. if (size || sizeCalculation) {
  5035. throw new TypeError('cannot set size without setting maxSize or maxEntrySize on cache');
  5036. }
  5037. return 0;
  5038. };
  5039. *#indexes({ allowStale = this.allowStale } = {}) {
  5040. if (this.#size) {
  5041. for (let i = this.#tail; true;) {
  5042. if (!this.#isValidIndex(i)) {
  5043. break;
  5044. }
  5045. if (allowStale || !this.#isStale(i)) {
  5046. yield i;
  5047. }
  5048. if (i === this.#head) {
  5049. break;
  5050. }
  5051. else {
  5052. i = this.#prev[i];
  5053. }
  5054. }
  5055. }
  5056. }
  5057. *#rindexes({ allowStale = this.allowStale } = {}) {
  5058. if (this.#size) {
  5059. for (let i = this.#head; true;) {
  5060. if (!this.#isValidIndex(i)) {
  5061. break;
  5062. }
  5063. if (allowStale || !this.#isStale(i)) {
  5064. yield i;
  5065. }
  5066. if (i === this.#tail) {
  5067. break;
  5068. }
  5069. else {
  5070. i = this.#next[i];
  5071. }
  5072. }
  5073. }
  5074. }
  5075. #isValidIndex(index) {
  5076. return (index !== undefined &&
  5077. this.#keyMap.get(this.#keyList[index]) === index);
  5078. }
  5079. /**
  5080. * Return a generator yielding `[key, value]` pairs,
  5081. * in order from most recently used to least recently used.
  5082. */
  5083. *entries() {
  5084. for (const i of this.#indexes()) {
  5085. if (this.#valList[i] !== undefined &&
  5086. this.#keyList[i] !== undefined &&
  5087. !this.#isBackgroundFetch(this.#valList[i])) {
  5088. yield [this.#keyList[i], this.#valList[i]];
  5089. }
  5090. }
  5091. }
  5092. /**
  5093. * Inverse order version of {@link LRUCache.entries}
  5094. *
  5095. * Return a generator yielding `[key, value]` pairs,
  5096. * in order from least recently used to most recently used.
  5097. */
  5098. *rentries() {
  5099. for (const i of this.#rindexes()) {
  5100. if (this.#valList[i] !== undefined &&
  5101. this.#keyList[i] !== undefined &&
  5102. !this.#isBackgroundFetch(this.#valList[i])) {
  5103. yield [this.#keyList[i], this.#valList[i]];
  5104. }
  5105. }
  5106. }
  5107. /**
  5108. * Return a generator yielding the keys in the cache,
  5109. * in order from most recently used to least recently used.
  5110. */
  5111. *keys() {
  5112. for (const i of this.#indexes()) {
  5113. const k = this.#keyList[i];
  5114. if (k !== undefined &&
  5115. !this.#isBackgroundFetch(this.#valList[i])) {
  5116. yield k;
  5117. }
  5118. }
  5119. }
  5120. /**
  5121. * Inverse order version of {@link LRUCache.keys}
  5122. *
  5123. * Return a generator yielding the keys in the cache,
  5124. * in order from least recently used to most recently used.
  5125. */
  5126. *rkeys() {
  5127. for (const i of this.#rindexes()) {
  5128. const k = this.#keyList[i];
  5129. if (k !== undefined &&
  5130. !this.#isBackgroundFetch(this.#valList[i])) {
  5131. yield k;
  5132. }
  5133. }
  5134. }
  5135. /**
  5136. * Return a generator yielding the values in the cache,
  5137. * in order from most recently used to least recently used.
  5138. */
  5139. *values() {
  5140. for (const i of this.#indexes()) {
  5141. const v = this.#valList[i];
  5142. if (v !== undefined &&
  5143. !this.#isBackgroundFetch(this.#valList[i])) {
  5144. yield this.#valList[i];
  5145. }
  5146. }
  5147. }
  5148. /**
  5149. * Inverse order version of {@link LRUCache.values}
  5150. *
  5151. * Return a generator yielding the values in the cache,
  5152. * in order from least recently used to most recently used.
  5153. */
  5154. *rvalues() {
  5155. for (const i of this.#rindexes()) {
  5156. const v = this.#valList[i];
  5157. if (v !== undefined &&
  5158. !this.#isBackgroundFetch(this.#valList[i])) {
  5159. yield this.#valList[i];
  5160. }
  5161. }
  5162. }
  5163. /**
  5164. * Iterating over the cache itself yields the same results as
  5165. * {@link LRUCache.entries}
  5166. */
  5167. [Symbol.iterator]() {
  5168. return this.entries();
  5169. }
  5170. /**
  5171. * A String value that is used in the creation of the default string
  5172. * description of an object. Called by the built-in method
  5173. * `Object.prototype.toString`.
  5174. */
  5175. [Symbol.toStringTag] = 'LRUCache';
  5176. /**
  5177. * Find a value for which the supplied fn method returns a truthy value,
  5178. * similar to `Array.find()`. fn is called as `fn(value, key, cache)`.
  5179. */
  5180. find(fn, getOptions = {}) {
  5181. for (const i of this.#indexes()) {
  5182. const v = this.#valList[i];
  5183. const value = this.#isBackgroundFetch(v)
  5184. ? v.__staleWhileFetching
  5185. : v;
  5186. if (value === undefined)
  5187. continue;
  5188. if (fn(value, this.#keyList[i], this)) {
  5189. return this.get(this.#keyList[i], getOptions);
  5190. }
  5191. }
  5192. }
  5193. /**
  5194. * Call the supplied function on each item in the cache, in order from most
  5195. * recently used to least recently used.
  5196. *
  5197. * `fn` is called as `fn(value, key, cache)`.
  5198. *
  5199. * If `thisp` is provided, function will be called in the `this`-context of
  5200. * the provided object, or the cache if no `thisp` object is provided.
  5201. *
  5202. * Does not update age or recenty of use, or iterate over stale values.
  5203. */
  5204. forEach(fn, thisp = this) {
  5205. for (const i of this.#indexes()) {
  5206. const v = this.#valList[i];
  5207. const value = this.#isBackgroundFetch(v)
  5208. ? v.__staleWhileFetching
  5209. : v;
  5210. if (value === undefined)
  5211. continue;
  5212. fn.call(thisp, value, this.#keyList[i], this);
  5213. }
  5214. }
  5215. /**
  5216. * The same as {@link LRUCache.forEach} but items are iterated over in
  5217. * reverse order. (ie, less recently used items are iterated over first.)
  5218. */
  5219. rforEach(fn, thisp = this) {
  5220. for (const i of this.#rindexes()) {
  5221. const v = this.#valList[i];
  5222. const value = this.#isBackgroundFetch(v)
  5223. ? v.__staleWhileFetching
  5224. : v;
  5225. if (value === undefined)
  5226. continue;
  5227. fn.call(thisp, value, this.#keyList[i], this);
  5228. }
  5229. }
  5230. /**
  5231. * Delete any stale entries. Returns true if anything was removed,
  5232. * false otherwise.
  5233. */
  5234. purgeStale() {
  5235. let deleted = false;
  5236. for (const i of this.#rindexes({ allowStale: true })) {
  5237. if (this.#isStale(i)) {
  5238. this.#delete(this.#keyList[i], 'expire');
  5239. deleted = true;
  5240. }
  5241. }
  5242. return deleted;
  5243. }
  5244. /**
  5245. * Get the extended info about a given entry, to get its value, size, and
  5246. * TTL info simultaneously. Returns `undefined` if the key is not present.
  5247. *
  5248. * Unlike {@link LRUCache#dump}, which is designed to be portable and survive
  5249. * serialization, the `start` value is always the current timestamp, and the
  5250. * `ttl` is a calculated remaining time to live (negative if expired).
  5251. *
  5252. * Always returns stale values, if their info is found in the cache, so be
  5253. * sure to check for expirations (ie, a negative {@link LRUCache.Entry#ttl})
  5254. * if relevant.
  5255. */
  5256. info(key) {
  5257. const i = this.#keyMap.get(key);
  5258. if (i === undefined)
  5259. return undefined;
  5260. const v = this.#valList[i];
  5261. const value = this.#isBackgroundFetch(v)
  5262. ? v.__staleWhileFetching
  5263. : v;
  5264. if (value === undefined)
  5265. return undefined;
  5266. const entry = { value };
  5267. if (this.#ttls && this.#starts) {
  5268. const ttl = this.#ttls[i];
  5269. const start = this.#starts[i];
  5270. if (ttl && start) {
  5271. const remain = ttl - (perf.now() - start);
  5272. entry.ttl = remain;
  5273. entry.start = Date.now();
  5274. }
  5275. }
  5276. if (this.#sizes) {
  5277. entry.size = this.#sizes[i];
  5278. }
  5279. return entry;
  5280. }
  5281. /**
  5282. * Return an array of [key, {@link LRUCache.Entry}] tuples which can be
  5283. * passed to {@link LRLUCache#load}.
  5284. *
  5285. * The `start` fields are calculated relative to a portable `Date.now()`
  5286. * timestamp, even if `performance.now()` is available.
  5287. *
  5288. * Stale entries are always included in the `dump`, even if
  5289. * {@link LRUCache.OptionsBase.allowStale} is false.
  5290. *
  5291. * Note: this returns an actual array, not a generator, so it can be more
  5292. * easily passed around.
  5293. */
  5294. dump() {
  5295. const arr = [];
  5296. for (const i of this.#indexes({ allowStale: true })) {
  5297. const key = this.#keyList[i];
  5298. const v = this.#valList[i];
  5299. const value = this.#isBackgroundFetch(v)
  5300. ? v.__staleWhileFetching
  5301. : v;
  5302. if (value === undefined || key === undefined)
  5303. continue;
  5304. const entry = { value };
  5305. if (this.#ttls && this.#starts) {
  5306. entry.ttl = this.#ttls[i];
  5307. // always dump the start relative to a portable timestamp
  5308. // it's ok for this to be a bit slow, it's a rare operation.
  5309. const age = perf.now() - this.#starts[i];
  5310. entry.start = Math.floor(Date.now() - age);
  5311. }
  5312. if (this.#sizes) {
  5313. entry.size = this.#sizes[i];
  5314. }
  5315. arr.unshift([key, entry]);
  5316. }
  5317. return arr;
  5318. }
  5319. /**
  5320. * Reset the cache and load in the items in entries in the order listed.
  5321. *
  5322. * The shape of the resulting cache may be different if the same options are
  5323. * not used in both caches.
  5324. *
  5325. * The `start` fields are assumed to be calculated relative to a portable
  5326. * `Date.now()` timestamp, even if `performance.now()` is available.
  5327. */
  5328. load(arr) {
  5329. this.clear();
  5330. for (const [key, entry] of arr) {
  5331. if (entry.start) {
  5332. // entry.start is a portable timestamp, but we may be using
  5333. // node's performance.now(), so calculate the offset, so that
  5334. // we get the intended remaining TTL, no matter how long it's
  5335. // been on ice.
  5336. //
  5337. // it's ok for this to be a bit slow, it's a rare operation.
  5338. const age = Date.now() - entry.start;
  5339. entry.start = perf.now() - age;
  5340. }
  5341. this.set(key, entry.value, entry);
  5342. }
  5343. }
  5344. /**
  5345. * Add a value to the cache.
  5346. *
  5347. * Note: if `undefined` is specified as a value, this is an alias for
  5348. * {@link LRUCache#delete}
  5349. *
  5350. * Fields on the {@link LRUCache.SetOptions} options param will override
  5351. * their corresponding values in the constructor options for the scope
  5352. * of this single `set()` operation.
  5353. *
  5354. * If `start` is provided, then that will set the effective start
  5355. * time for the TTL calculation. Note that this must be a previous
  5356. * value of `performance.now()` if supported, or a previous value of
  5357. * `Date.now()` if not.
  5358. *
  5359. * Options object may also include `size`, which will prevent
  5360. * calling the `sizeCalculation` function and just use the specified
  5361. * number if it is a positive integer, and `noDisposeOnSet` which
  5362. * will prevent calling a `dispose` function in the case of
  5363. * overwrites.
  5364. *
  5365. * If the `size` (or return value of `sizeCalculation`) for a given
  5366. * entry is greater than `maxEntrySize`, then the item will not be
  5367. * added to the cache.
  5368. *
  5369. * Will update the recency of the entry.
  5370. *
  5371. * If the value is `undefined`, then this is an alias for
  5372. * `cache.delete(key)`. `undefined` is never stored in the cache.
  5373. */
  5374. set(k, v, setOptions = {}) {
  5375. if (v === undefined) {
  5376. this.delete(k);
  5377. return this;
  5378. }
  5379. const { ttl = this.ttl, start, noDisposeOnSet = this.noDisposeOnSet, sizeCalculation = this.sizeCalculation, status, } = setOptions;
  5380. let { noUpdateTTL = this.noUpdateTTL } = setOptions;
  5381. const size = this.#requireSize(k, v, setOptions.size || 0, sizeCalculation);
  5382. // if the item doesn't fit, don't do anything
  5383. // NB: maxEntrySize set to maxSize by default
  5384. if (this.maxEntrySize && size > this.maxEntrySize) {
  5385. if (status) {
  5386. status.set = 'miss';
  5387. status.maxEntrySizeExceeded = true;
  5388. }
  5389. // have to delete, in case something is there already.
  5390. this.#delete(k, 'set');
  5391. return this;
  5392. }
  5393. let index = this.#size === 0 ? undefined : this.#keyMap.get(k);
  5394. if (index === undefined) {
  5395. // addition
  5396. index = (this.#size === 0
  5397. ? this.#tail
  5398. : this.#free.length !== 0
  5399. ? this.#free.pop()
  5400. : this.#size === this.#max
  5401. ? this.#evict(false)
  5402. : this.#size);
  5403. this.#keyList[index] = k;
  5404. this.#valList[index] = v;
  5405. this.#keyMap.set(k, index);
  5406. this.#next[this.#tail] = index;
  5407. this.#prev[index] = this.#tail;
  5408. this.#tail = index;
  5409. this.#size++;
  5410. this.#addItemSize(index, size, status);
  5411. if (status)
  5412. status.set = 'add';
  5413. noUpdateTTL = false;
  5414. }
  5415. else {
  5416. // update
  5417. this.#moveToTail(index);
  5418. const oldVal = this.#valList[index];
  5419. if (v !== oldVal) {
  5420. if (this.#hasFetchMethod && this.#isBackgroundFetch(oldVal)) {
  5421. oldVal.__abortController.abort(new Error('replaced'));
  5422. const { __staleWhileFetching: s } = oldVal;
  5423. if (s !== undefined && !noDisposeOnSet) {
  5424. if (this.#hasDispose) {
  5425. this.#dispose?.(s, k, 'set');
  5426. }
  5427. if (this.#hasDisposeAfter) {
  5428. this.#disposed?.push([s, k, 'set']);
  5429. }
  5430. }
  5431. }
  5432. else if (!noDisposeOnSet) {
  5433. if (this.#hasDispose) {
  5434. this.#dispose?.(oldVal, k, 'set');
  5435. }
  5436. if (this.#hasDisposeAfter) {
  5437. this.#disposed?.push([oldVal, k, 'set']);
  5438. }
  5439. }
  5440. this.#removeItemSize(index);
  5441. this.#addItemSize(index, size, status);
  5442. this.#valList[index] = v;
  5443. if (status) {
  5444. status.set = 'replace';
  5445. const oldValue = oldVal && this.#isBackgroundFetch(oldVal)
  5446. ? oldVal.__staleWhileFetching
  5447. : oldVal;
  5448. if (oldValue !== undefined)
  5449. status.oldValue = oldValue;
  5450. }
  5451. }
  5452. else if (status) {
  5453. status.set = 'update';
  5454. }
  5455. }
  5456. if (ttl !== 0 && !this.#ttls) {
  5457. this.#initializeTTLTracking();
  5458. }
  5459. if (this.#ttls) {
  5460. if (!noUpdateTTL) {
  5461. this.#setItemTTL(index, ttl, start);
  5462. }
  5463. if (status)
  5464. this.#statusTTL(status, index);
  5465. }
  5466. if (!noDisposeOnSet && this.#hasDisposeAfter && this.#disposed) {
  5467. const dt = this.#disposed;
  5468. let task;
  5469. while ((task = dt?.shift())) {
  5470. this.#disposeAfter?.(...task);
  5471. }
  5472. }
  5473. return this;
  5474. }
  5475. /**
  5476. * Evict the least recently used item, returning its value or
  5477. * `undefined` if cache is empty.
  5478. */
  5479. pop() {
  5480. try {
  5481. while (this.#size) {
  5482. const val = this.#valList[this.#head];
  5483. this.#evict(true);
  5484. if (this.#isBackgroundFetch(val)) {
  5485. if (val.__staleWhileFetching) {
  5486. return val.__staleWhileFetching;
  5487. }
  5488. }
  5489. else if (val !== undefined) {
  5490. return val;
  5491. }
  5492. }
  5493. }
  5494. finally {
  5495. if (this.#hasDisposeAfter && this.#disposed) {
  5496. const dt = this.#disposed;
  5497. let task;
  5498. while ((task = dt?.shift())) {
  5499. this.#disposeAfter?.(...task);
  5500. }
  5501. }
  5502. }
  5503. }
  5504. #evict(free) {
  5505. const head = this.#head;
  5506. const k = this.#keyList[head];
  5507. const v = this.#valList[head];
  5508. if (this.#hasFetchMethod && this.#isBackgroundFetch(v)) {
  5509. v.__abortController.abort(new Error('evicted'));
  5510. }
  5511. else if (this.#hasDispose || this.#hasDisposeAfter) {
  5512. if (this.#hasDispose) {
  5513. this.#dispose?.(v, k, 'evict');
  5514. }
  5515. if (this.#hasDisposeAfter) {
  5516. this.#disposed?.push([v, k, 'evict']);
  5517. }
  5518. }
  5519. this.#removeItemSize(head);
  5520. // if we aren't about to use the index, then null these out
  5521. if (free) {
  5522. this.#keyList[head] = undefined;
  5523. this.#valList[head] = undefined;
  5524. this.#free.push(head);
  5525. }
  5526. if (this.#size === 1) {
  5527. this.#head = this.#tail = 0;
  5528. this.#free.length = 0;
  5529. }
  5530. else {
  5531. this.#head = this.#next[head];
  5532. }
  5533. this.#keyMap.delete(k);
  5534. this.#size--;
  5535. return head;
  5536. }
  5537. /**
  5538. * Check if a key is in the cache, without updating the recency of use.
  5539. * Will return false if the item is stale, even though it is technically
  5540. * in the cache.
  5541. *
  5542. * Check if a key is in the cache, without updating the recency of
  5543. * use. Age is updated if {@link LRUCache.OptionsBase.updateAgeOnHas} is set
  5544. * to `true` in either the options or the constructor.
  5545. *
  5546. * Will return `false` if the item is stale, even though it is technically in
  5547. * the cache. The difference can be determined (if it matters) by using a
  5548. * `status` argument, and inspecting the `has` field.
  5549. *
  5550. * Will not update item age unless
  5551. * {@link LRUCache.OptionsBase.updateAgeOnHas} is set.
  5552. */
  5553. has(k, hasOptions = {}) {
  5554. const { updateAgeOnHas = this.updateAgeOnHas, status } = hasOptions;
  5555. const index = this.#keyMap.get(k);
  5556. if (index !== undefined) {
  5557. const v = this.#valList[index];
  5558. if (this.#isBackgroundFetch(v) &&
  5559. v.__staleWhileFetching === undefined) {
  5560. return false;
  5561. }
  5562. if (!this.#isStale(index)) {
  5563. if (updateAgeOnHas) {
  5564. this.#updateItemAge(index);
  5565. }
  5566. if (status) {
  5567. status.has = 'hit';
  5568. this.#statusTTL(status, index);
  5569. }
  5570. return true;
  5571. }
  5572. else if (status) {
  5573. status.has = 'stale';
  5574. this.#statusTTL(status, index);
  5575. }
  5576. }
  5577. else if (status) {
  5578. status.has = 'miss';
  5579. }
  5580. return false;
  5581. }
  5582. /**
  5583. * Like {@link LRUCache#get} but doesn't update recency or delete stale
  5584. * items.
  5585. *
  5586. * Returns `undefined` if the item is stale, unless
  5587. * {@link LRUCache.OptionsBase.allowStale} is set.
  5588. */
  5589. peek(k, peekOptions = {}) {
  5590. const { allowStale = this.allowStale } = peekOptions;
  5591. const index = this.#keyMap.get(k);
  5592. if (index === undefined ||
  5593. (!allowStale && this.#isStale(index))) {
  5594. return;
  5595. }
  5596. const v = this.#valList[index];
  5597. // either stale and allowed, or forcing a refresh of non-stale value
  5598. return this.#isBackgroundFetch(v) ? v.__staleWhileFetching : v;
  5599. }
  5600. #backgroundFetch(k, index, options, context) {
  5601. const v = index === undefined ? undefined : this.#valList[index];
  5602. if (this.#isBackgroundFetch(v)) {
  5603. return v;
  5604. }
  5605. const ac = new AC();
  5606. const { signal } = options;
  5607. // when/if our AC signals, then stop listening to theirs.
  5608. signal?.addEventListener('abort', () => ac.abort(signal.reason), {
  5609. signal: ac.signal,
  5610. });
  5611. const fetchOpts = {
  5612. signal: ac.signal,
  5613. options,
  5614. context,
  5615. };
  5616. const cb = (v, updateCache = false) => {
  5617. const { aborted } = ac.signal;
  5618. const ignoreAbort = options.ignoreFetchAbort && v !== undefined;
  5619. if (options.status) {
  5620. if (aborted && !updateCache) {
  5621. options.status.fetchAborted = true;
  5622. options.status.fetchError = ac.signal.reason;
  5623. if (ignoreAbort)
  5624. options.status.fetchAbortIgnored = true;
  5625. }
  5626. else {
  5627. options.status.fetchResolved = true;
  5628. }
  5629. }
  5630. if (aborted && !ignoreAbort && !updateCache) {
  5631. return fetchFail(ac.signal.reason);
  5632. }
  5633. // either we didn't abort, and are still here, or we did, and ignored
  5634. const bf = p;
  5635. if (this.#valList[index] === p) {
  5636. if (v === undefined) {
  5637. if (bf.__staleWhileFetching) {
  5638. this.#valList[index] = bf.__staleWhileFetching;
  5639. }
  5640. else {
  5641. this.#delete(k, 'fetch');
  5642. }
  5643. }
  5644. else {
  5645. if (options.status)
  5646. options.status.fetchUpdated = true;
  5647. this.set(k, v, fetchOpts.options);
  5648. }
  5649. }
  5650. return v;
  5651. };
  5652. const eb = (er) => {
  5653. if (options.status) {
  5654. options.status.fetchRejected = true;
  5655. options.status.fetchError = er;
  5656. }
  5657. return fetchFail(er);
  5658. };
  5659. const fetchFail = (er) => {
  5660. const { aborted } = ac.signal;
  5661. const allowStaleAborted = aborted && options.allowStaleOnFetchAbort;
  5662. const allowStale = allowStaleAborted || options.allowStaleOnFetchRejection;
  5663. const noDelete = allowStale || options.noDeleteOnFetchRejection;
  5664. const bf = p;
  5665. if (this.#valList[index] === p) {
  5666. // if we allow stale on fetch rejections, then we need to ensure that
  5667. // the stale value is not removed from the cache when the fetch fails.
  5668. const del = !noDelete || bf.__staleWhileFetching === undefined;
  5669. if (del) {
  5670. this.#delete(k, 'fetch');
  5671. }
  5672. else if (!allowStaleAborted) {
  5673. // still replace the *promise* with the stale value,
  5674. // since we are done with the promise at this point.
  5675. // leave it untouched if we're still waiting for an
  5676. // aborted background fetch that hasn't yet returned.
  5677. this.#valList[index] = bf.__staleWhileFetching;
  5678. }
  5679. }
  5680. if (allowStale) {
  5681. if (options.status && bf.__staleWhileFetching !== undefined) {
  5682. options.status.returnedStale = true;
  5683. }
  5684. return bf.__staleWhileFetching;
  5685. }
  5686. else if (bf.__returned === bf) {
  5687. throw er;
  5688. }
  5689. };
  5690. const pcall = (res, rej) => {
  5691. const fmp = this.#fetchMethod?.(k, v, fetchOpts);
  5692. if (fmp && fmp instanceof Promise) {
  5693. fmp.then(v => res(v === undefined ? undefined : v), rej);
  5694. }
  5695. // ignored, we go until we finish, regardless.
  5696. // defer check until we are actually aborting,
  5697. // so fetchMethod can override.
  5698. ac.signal.addEventListener('abort', () => {
  5699. if (!options.ignoreFetchAbort ||
  5700. options.allowStaleOnFetchAbort) {
  5701. res(undefined);
  5702. // when it eventually resolves, update the cache.
  5703. if (options.allowStaleOnFetchAbort) {
  5704. res = v => cb(v, true);
  5705. }
  5706. }
  5707. });
  5708. };
  5709. if (options.status)
  5710. options.status.fetchDispatched = true;
  5711. const p = new Promise(pcall).then(cb, eb);
  5712. const bf = Object.assign(p, {
  5713. __abortController: ac,
  5714. __staleWhileFetching: v,
  5715. __returned: undefined,
  5716. });
  5717. if (index === undefined) {
  5718. // internal, don't expose status.
  5719. this.set(k, bf, { ...fetchOpts.options, status: undefined });
  5720. index = this.#keyMap.get(k);
  5721. }
  5722. else {
  5723. this.#valList[index] = bf;
  5724. }
  5725. return bf;
  5726. }
  5727. #isBackgroundFetch(p) {
  5728. if (!this.#hasFetchMethod)
  5729. return false;
  5730. const b = p;
  5731. return (!!b &&
  5732. b instanceof Promise &&
  5733. b.hasOwnProperty('__staleWhileFetching') &&
  5734. b.__abortController instanceof AC);
  5735. }
  5736. async fetch(k, fetchOptions = {}) {
  5737. const {
  5738. // get options
  5739. allowStale = this.allowStale, updateAgeOnGet = this.updateAgeOnGet, noDeleteOnStaleGet = this.noDeleteOnStaleGet,
  5740. // set options
  5741. ttl = this.ttl, noDisposeOnSet = this.noDisposeOnSet, size = 0, sizeCalculation = this.sizeCalculation, noUpdateTTL = this.noUpdateTTL,
  5742. // fetch exclusive options
  5743. noDeleteOnFetchRejection = this.noDeleteOnFetchRejection, allowStaleOnFetchRejection = this.allowStaleOnFetchRejection, ignoreFetchAbort = this.ignoreFetchAbort, allowStaleOnFetchAbort = this.allowStaleOnFetchAbort, context, forceRefresh = false, status, signal, } = fetchOptions;
  5744. if (!this.#hasFetchMethod) {
  5745. if (status)
  5746. status.fetch = 'get';
  5747. return this.get(k, {
  5748. allowStale,
  5749. updateAgeOnGet,
  5750. noDeleteOnStaleGet,
  5751. status,
  5752. });
  5753. }
  5754. const options = {
  5755. allowStale,
  5756. updateAgeOnGet,
  5757. noDeleteOnStaleGet,
  5758. ttl,
  5759. noDisposeOnSet,
  5760. size,
  5761. sizeCalculation,
  5762. noUpdateTTL,
  5763. noDeleteOnFetchRejection,
  5764. allowStaleOnFetchRejection,
  5765. allowStaleOnFetchAbort,
  5766. ignoreFetchAbort,
  5767. status,
  5768. signal,
  5769. };
  5770. let index = this.#keyMap.get(k);
  5771. if (index === undefined) {
  5772. if (status)
  5773. status.fetch = 'miss';
  5774. const p = this.#backgroundFetch(k, index, options, context);
  5775. return (p.__returned = p);
  5776. }
  5777. else {
  5778. // in cache, maybe already fetching
  5779. const v = this.#valList[index];
  5780. if (this.#isBackgroundFetch(v)) {
  5781. const stale = allowStale && v.__staleWhileFetching !== undefined;
  5782. if (status) {
  5783. status.fetch = 'inflight';
  5784. if (stale)
  5785. status.returnedStale = true;
  5786. }
  5787. return stale ? v.__staleWhileFetching : (v.__returned = v);
  5788. }
  5789. // if we force a refresh, that means do NOT serve the cached value,
  5790. // unless we are already in the process of refreshing the cache.
  5791. const isStale = this.#isStale(index);
  5792. if (!forceRefresh && !isStale) {
  5793. if (status)
  5794. status.fetch = 'hit';
  5795. this.#moveToTail(index);
  5796. if (updateAgeOnGet) {
  5797. this.#updateItemAge(index);
  5798. }
  5799. if (status)
  5800. this.#statusTTL(status, index);
  5801. return v;
  5802. }
  5803. // ok, it is stale or a forced refresh, and not already fetching.
  5804. // refresh the cache.
  5805. const p = this.#backgroundFetch(k, index, options, context);
  5806. const hasStale = p.__staleWhileFetching !== undefined;
  5807. const staleVal = hasStale && allowStale;
  5808. if (status) {
  5809. status.fetch = isStale ? 'stale' : 'refresh';
  5810. if (staleVal && isStale)
  5811. status.returnedStale = true;
  5812. }
  5813. return staleVal ? p.__staleWhileFetching : (p.__returned = p);
  5814. }
  5815. }
  5816. async forceFetch(k, fetchOptions = {}) {
  5817. const v = await this.fetch(k, fetchOptions);
  5818. if (v === undefined)
  5819. throw new Error('fetch() returned undefined');
  5820. return v;
  5821. }
  5822. memo(k, memoOptions = {}) {
  5823. const memoMethod = this.#memoMethod;
  5824. if (!memoMethod) {
  5825. throw new Error('no memoMethod provided to constructor');
  5826. }
  5827. const { context, forceRefresh, ...options } = memoOptions;
  5828. const v = this.get(k, options);
  5829. if (!forceRefresh && v !== undefined)
  5830. return v;
  5831. const vv = memoMethod(k, v, {
  5832. options,
  5833. context,
  5834. });
  5835. this.set(k, vv, options);
  5836. return vv;
  5837. }
  5838. /**
  5839. * Return a value from the cache. Will update the recency of the cache
  5840. * entry found.
  5841. *
  5842. * If the key is not found, get() will return `undefined`.
  5843. */
  5844. get(k, getOptions = {}) {
  5845. const { allowStale = this.allowStale, updateAgeOnGet = this.updateAgeOnGet, noDeleteOnStaleGet = this.noDeleteOnStaleGet, status, } = getOptions;
  5846. const index = this.#keyMap.get(k);
  5847. if (index !== undefined) {
  5848. const value = this.#valList[index];
  5849. const fetching = this.#isBackgroundFetch(value);
  5850. if (status)
  5851. this.#statusTTL(status, index);
  5852. if (this.#isStale(index)) {
  5853. if (status)
  5854. status.get = 'stale';
  5855. // delete only if not an in-flight background fetch
  5856. if (!fetching) {
  5857. if (!noDeleteOnStaleGet) {
  5858. this.#delete(k, 'expire');
  5859. }
  5860. if (status && allowStale)
  5861. status.returnedStale = true;
  5862. return allowStale ? value : undefined;
  5863. }
  5864. else {
  5865. if (status &&
  5866. allowStale &&
  5867. value.__staleWhileFetching !== undefined) {
  5868. status.returnedStale = true;
  5869. }
  5870. return allowStale ? value.__staleWhileFetching : undefined;
  5871. }
  5872. }
  5873. else {
  5874. if (status)
  5875. status.get = 'hit';
  5876. // if we're currently fetching it, we don't actually have it yet
  5877. // it's not stale, which means this isn't a staleWhileRefetching.
  5878. // If it's not stale, and fetching, AND has a __staleWhileFetching
  5879. // value, then that means the user fetched with {forceRefresh:true},
  5880. // so it's safe to return that value.
  5881. if (fetching) {
  5882. return value.__staleWhileFetching;
  5883. }
  5884. this.#moveToTail(index);
  5885. if (updateAgeOnGet) {
  5886. this.#updateItemAge(index);
  5887. }
  5888. return value;
  5889. }
  5890. }
  5891. else if (status) {
  5892. status.get = 'miss';
  5893. }
  5894. }
  5895. #connect(p, n) {
  5896. this.#prev[n] = p;
  5897. this.#next[p] = n;
  5898. }
  5899. #moveToTail(index) {
  5900. // if tail already, nothing to do
  5901. // if head, move head to next[index]
  5902. // else
  5903. // move next[prev[index]] to next[index] (head has no prev)
  5904. // move prev[next[index]] to prev[index]
  5905. // prev[index] = tail
  5906. // next[tail] = index
  5907. // tail = index
  5908. if (index !== this.#tail) {
  5909. if (index === this.#head) {
  5910. this.#head = this.#next[index];
  5911. }
  5912. else {
  5913. this.#connect(this.#prev[index], this.#next[index]);
  5914. }
  5915. this.#connect(this.#tail, index);
  5916. this.#tail = index;
  5917. }
  5918. }
  5919. /**
  5920. * Deletes a key out of the cache.
  5921. *
  5922. * Returns true if the key was deleted, false otherwise.
  5923. */
  5924. delete(k) {
  5925. return this.#delete(k, 'delete');
  5926. }
  5927. #delete(k, reason) {
  5928. let deleted = false;
  5929. if (this.#size !== 0) {
  5930. const index = this.#keyMap.get(k);
  5931. if (index !== undefined) {
  5932. deleted = true;
  5933. if (this.#size === 1) {
  5934. this.#clear(reason);
  5935. }
  5936. else {
  5937. this.#removeItemSize(index);
  5938. const v = this.#valList[index];
  5939. if (this.#isBackgroundFetch(v)) {
  5940. v.__abortController.abort(new Error('deleted'));
  5941. }
  5942. else if (this.#hasDispose || this.#hasDisposeAfter) {
  5943. if (this.#hasDispose) {
  5944. this.#dispose?.(v, k, reason);
  5945. }
  5946. if (this.#hasDisposeAfter) {
  5947. this.#disposed?.push([v, k, reason]);
  5948. }
  5949. }
  5950. this.#keyMap.delete(k);
  5951. this.#keyList[index] = undefined;
  5952. this.#valList[index] = undefined;
  5953. if (index === this.#tail) {
  5954. this.#tail = this.#prev[index];
  5955. }
  5956. else if (index === this.#head) {
  5957. this.#head = this.#next[index];
  5958. }
  5959. else {
  5960. const pi = this.#prev[index];
  5961. this.#next[pi] = this.#next[index];
  5962. const ni = this.#next[index];
  5963. this.#prev[ni] = this.#prev[index];
  5964. }
  5965. this.#size--;
  5966. this.#free.push(index);
  5967. }
  5968. }
  5969. }
  5970. if (this.#hasDisposeAfter && this.#disposed?.length) {
  5971. const dt = this.#disposed;
  5972. let task;
  5973. while ((task = dt?.shift())) {
  5974. this.#disposeAfter?.(...task);
  5975. }
  5976. }
  5977. return deleted;
  5978. }
  5979. /**
  5980. * Clear the cache entirely, throwing away all values.
  5981. */
  5982. clear() {
  5983. return this.#clear('delete');
  5984. }
  5985. #clear(reason) {
  5986. for (const index of this.#rindexes({ allowStale: true })) {
  5987. const v = this.#valList[index];
  5988. if (this.#isBackgroundFetch(v)) {
  5989. v.__abortController.abort(new Error('deleted'));
  5990. }
  5991. else {
  5992. const k = this.#keyList[index];
  5993. if (this.#hasDispose) {
  5994. this.#dispose?.(v, k, reason);
  5995. }
  5996. if (this.#hasDisposeAfter) {
  5997. this.#disposed?.push([v, k, reason]);
  5998. }
  5999. }
  6000. }
  6001. this.#keyMap.clear();
  6002. this.#valList.fill(undefined);
  6003. this.#keyList.fill(undefined);
  6004. if (this.#ttls && this.#starts) {
  6005. this.#ttls.fill(0);
  6006. this.#starts.fill(0);
  6007. }
  6008. if (this.#sizes) {
  6009. this.#sizes.fill(0);
  6010. }
  6011. this.#head = 0;
  6012. this.#tail = 0;
  6013. this.#free.length = 0;
  6014. this.#calculatedSize = 0;
  6015. this.#size = 0;
  6016. if (this.#hasDisposeAfter && this.#disposed) {
  6017. const dt = this.#disposed;
  6018. let task;
  6019. while ((task = dt?.shift())) {
  6020. this.#disposeAfter?.(...task);
  6021. }
  6022. }
  6023. }
  6024. }
  6025. const proc = typeof process === 'object' && process
  6026. ? process
  6027. : {
  6028. stdout: null,
  6029. stderr: null,
  6030. };
  6031. /**
  6032. * Return true if the argument is a Minipass stream, Node stream, or something
  6033. * else that Minipass can interact with.
  6034. */
  6035. const isStream = (s) => !!s &&
  6036. typeof s === 'object' &&
  6037. (s instanceof Minipass ||
  6038. s instanceof Stream$1 ||
  6039. isReadable(s) ||
  6040. isWritable(s));
  6041. /**
  6042. * Return true if the argument is a valid {@link Minipass.Readable}
  6043. */
  6044. const isReadable = (s) => !!s &&
  6045. typeof s === 'object' &&
  6046. s instanceof EventEmitter$4 &&
  6047. typeof s.pipe === 'function' &&
  6048. // node core Writable streams have a pipe() method, but it throws
  6049. s.pipe !== Stream$1.Writable.prototype.pipe;
  6050. /**
  6051. * Return true if the argument is a valid {@link Minipass.Writable}
  6052. */
  6053. const isWritable = (s) => !!s &&
  6054. typeof s === 'object' &&
  6055. s instanceof EventEmitter$4 &&
  6056. typeof s.write === 'function' &&
  6057. typeof s.end === 'function';
  6058. const EOF = Symbol('EOF');
  6059. const MAYBE_EMIT_END = Symbol('maybeEmitEnd');
  6060. const EMITTED_END = Symbol('emittedEnd');
  6061. const EMITTING_END = Symbol('emittingEnd');
  6062. const EMITTED_ERROR = Symbol('emittedError');
  6063. const CLOSED$1 = Symbol('closed');
  6064. const READ = Symbol('read');
  6065. const FLUSH = Symbol('flush');
  6066. const FLUSHCHUNK = Symbol('flushChunk');
  6067. const ENCODING$1 = Symbol('encoding');
  6068. const DECODER = Symbol('decoder');
  6069. const FLOWING = Symbol('flowing');
  6070. const PAUSED = Symbol('paused');
  6071. const RESUME = Symbol('resume');
  6072. const BUFFER = Symbol('buffer');
  6073. const PIPES = Symbol('pipes');
  6074. const BUFFERLENGTH = Symbol('bufferLength');
  6075. const BUFFERPUSH = Symbol('bufferPush');
  6076. const BUFFERSHIFT = Symbol('bufferShift');
  6077. const OBJECTMODE = Symbol('objectMode');
  6078. // internal event when stream is destroyed
  6079. const DESTROYED = Symbol('destroyed');
  6080. // internal event when stream has an error
  6081. const ERROR = Symbol('error');
  6082. const EMITDATA = Symbol('emitData');
  6083. const EMITEND = Symbol('emitEnd');
  6084. const EMITEND2 = Symbol('emitEnd2');
  6085. const ASYNC = Symbol('async');
  6086. const ABORT = Symbol('abort');
  6087. const ABORTED = Symbol('aborted');
  6088. const SIGNAL = Symbol('signal');
  6089. const DATALISTENERS = Symbol('dataListeners');
  6090. const DISCARDED = Symbol('discarded');
  6091. const defer$3 = (fn) => Promise.resolve().then(fn);
  6092. const nodefer = (fn) => fn();
  6093. const isEndish = (ev) => ev === 'end' || ev === 'finish' || ev === 'prefinish';
  6094. const isArrayBufferLike = (b) => b instanceof ArrayBuffer ||
  6095. (!!b &&
  6096. typeof b === 'object' &&
  6097. b.constructor &&
  6098. b.constructor.name === 'ArrayBuffer' &&
  6099. b.byteLength >= 0);
  6100. const isArrayBufferView = (b) => !Buffer.isBuffer(b) && ArrayBuffer.isView(b);
  6101. /**
  6102. * Internal class representing a pipe to a destination stream.
  6103. *
  6104. * @internal
  6105. */
  6106. class Pipe {
  6107. src;
  6108. dest;
  6109. opts;
  6110. ondrain;
  6111. constructor(src, dest, opts) {
  6112. this.src = src;
  6113. this.dest = dest;
  6114. this.opts = opts;
  6115. this.ondrain = () => src[RESUME]();
  6116. this.dest.on('drain', this.ondrain);
  6117. }
  6118. unpipe() {
  6119. this.dest.removeListener('drain', this.ondrain);
  6120. }
  6121. // only here for the prototype
  6122. /* c8 ignore start */
  6123. proxyErrors(_er) { }
  6124. /* c8 ignore stop */
  6125. end() {
  6126. this.unpipe();
  6127. if (this.opts.end)
  6128. this.dest.end();
  6129. }
  6130. }
  6131. /**
  6132. * Internal class representing a pipe to a destination stream where
  6133. * errors are proxied.
  6134. *
  6135. * @internal
  6136. */
  6137. class PipeProxyErrors extends Pipe {
  6138. unpipe() {
  6139. this.src.removeListener('error', this.proxyErrors);
  6140. super.unpipe();
  6141. }
  6142. constructor(src, dest, opts) {
  6143. super(src, dest, opts);
  6144. this.proxyErrors = er => dest.emit('error', er);
  6145. src.on('error', this.proxyErrors);
  6146. }
  6147. }
  6148. const isObjectModeOptions = (o) => !!o.objectMode;
  6149. const isEncodingOptions = (o) => !o.objectMode && !!o.encoding && o.encoding !== 'buffer';
  6150. /**
  6151. * Main export, the Minipass class
  6152. *
  6153. * `RType` is the type of data emitted, defaults to Buffer
  6154. *
  6155. * `WType` is the type of data to be written, if RType is buffer or string,
  6156. * then any {@link Minipass.ContiguousData} is allowed.
  6157. *
  6158. * `Events` is the set of event handler signatures that this object
  6159. * will emit, see {@link Minipass.Events}
  6160. */
  6161. class Minipass extends EventEmitter$4 {
  6162. [FLOWING] = false;
  6163. [PAUSED] = false;
  6164. [PIPES] = [];
  6165. [BUFFER] = [];
  6166. [OBJECTMODE];
  6167. [ENCODING$1];
  6168. [ASYNC];
  6169. [DECODER];
  6170. [EOF] = false;
  6171. [EMITTED_END] = false;
  6172. [EMITTING_END] = false;
  6173. [CLOSED$1] = false;
  6174. [EMITTED_ERROR] = null;
  6175. [BUFFERLENGTH] = 0;
  6176. [DESTROYED] = false;
  6177. [SIGNAL];
  6178. [ABORTED] = false;
  6179. [DATALISTENERS] = 0;
  6180. [DISCARDED] = false;
  6181. /**
  6182. * true if the stream can be written
  6183. */
  6184. writable = true;
  6185. /**
  6186. * true if the stream can be read
  6187. */
  6188. readable = true;
  6189. /**
  6190. * If `RType` is Buffer, then options do not need to be provided.
  6191. * Otherwise, an options object must be provided to specify either
  6192. * {@link Minipass.SharedOptions.objectMode} or
  6193. * {@link Minipass.SharedOptions.encoding}, as appropriate.
  6194. */
  6195. constructor(...args) {
  6196. const options = (args[0] ||
  6197. {});
  6198. super();
  6199. if (options.objectMode && typeof options.encoding === 'string') {
  6200. throw new TypeError('Encoding and objectMode may not be used together');
  6201. }
  6202. if (isObjectModeOptions(options)) {
  6203. this[OBJECTMODE] = true;
  6204. this[ENCODING$1] = null;
  6205. }
  6206. else if (isEncodingOptions(options)) {
  6207. this[ENCODING$1] = options.encoding;
  6208. this[OBJECTMODE] = false;
  6209. }
  6210. else {
  6211. this[OBJECTMODE] = false;
  6212. this[ENCODING$1] = null;
  6213. }
  6214. this[ASYNC] = !!options.async;
  6215. this[DECODER] = this[ENCODING$1]
  6216. ? new StringDecoder(this[ENCODING$1])
  6217. : null;
  6218. //@ts-ignore - private option for debugging and testing
  6219. if (options && options.debugExposeBuffer === true) {
  6220. Object.defineProperty(this, 'buffer', { get: () => this[BUFFER] });
  6221. }
  6222. //@ts-ignore - private option for debugging and testing
  6223. if (options && options.debugExposePipes === true) {
  6224. Object.defineProperty(this, 'pipes', { get: () => this[PIPES] });
  6225. }
  6226. const { signal } = options;
  6227. if (signal) {
  6228. this[SIGNAL] = signal;
  6229. if (signal.aborted) {
  6230. this[ABORT]();
  6231. }
  6232. else {
  6233. signal.addEventListener('abort', () => this[ABORT]());
  6234. }
  6235. }
  6236. }
  6237. /**
  6238. * The amount of data stored in the buffer waiting to be read.
  6239. *
  6240. * For Buffer strings, this will be the total byte length.
  6241. * For string encoding streams, this will be the string character length,
  6242. * according to JavaScript's `string.length` logic.
  6243. * For objectMode streams, this is a count of the items waiting to be
  6244. * emitted.
  6245. */
  6246. get bufferLength() {
  6247. return this[BUFFERLENGTH];
  6248. }
  6249. /**
  6250. * The `BufferEncoding` currently in use, or `null`
  6251. */
  6252. get encoding() {
  6253. return this[ENCODING$1];
  6254. }
  6255. /**
  6256. * @deprecated - This is a read only property
  6257. */
  6258. set encoding(_enc) {
  6259. throw new Error('Encoding must be set at instantiation time');
  6260. }
  6261. /**
  6262. * @deprecated - Encoding may only be set at instantiation time
  6263. */
  6264. setEncoding(_enc) {
  6265. throw new Error('Encoding must be set at instantiation time');
  6266. }
  6267. /**
  6268. * True if this is an objectMode stream
  6269. */
  6270. get objectMode() {
  6271. return this[OBJECTMODE];
  6272. }
  6273. /**
  6274. * @deprecated - This is a read-only property
  6275. */
  6276. set objectMode(_om) {
  6277. throw new Error('objectMode must be set at instantiation time');
  6278. }
  6279. /**
  6280. * true if this is an async stream
  6281. */
  6282. get ['async']() {
  6283. return this[ASYNC];
  6284. }
  6285. /**
  6286. * Set to true to make this stream async.
  6287. *
  6288. * Once set, it cannot be unset, as this would potentially cause incorrect
  6289. * behavior. Ie, a sync stream can be made async, but an async stream
  6290. * cannot be safely made sync.
  6291. */
  6292. set ['async'](a) {
  6293. this[ASYNC] = this[ASYNC] || !!a;
  6294. }
  6295. // drop everything and get out of the flow completely
  6296. [ABORT]() {
  6297. this[ABORTED] = true;
  6298. this.emit('abort', this[SIGNAL]?.reason);
  6299. this.destroy(this[SIGNAL]?.reason);
  6300. }
  6301. /**
  6302. * True if the stream has been aborted.
  6303. */
  6304. get aborted() {
  6305. return this[ABORTED];
  6306. }
  6307. /**
  6308. * No-op setter. Stream aborted status is set via the AbortSignal provided
  6309. * in the constructor options.
  6310. */
  6311. set aborted(_) { }
  6312. write(chunk, encoding, cb) {
  6313. if (this[ABORTED])
  6314. return false;
  6315. if (this[EOF])
  6316. throw new Error('write after end');
  6317. if (this[DESTROYED]) {
  6318. this.emit('error', Object.assign(new Error('Cannot call write after a stream was destroyed'), { code: 'ERR_STREAM_DESTROYED' }));
  6319. return true;
  6320. }
  6321. if (typeof encoding === 'function') {
  6322. cb = encoding;
  6323. encoding = 'utf8';
  6324. }
  6325. if (!encoding)
  6326. encoding = 'utf8';
  6327. const fn = this[ASYNC] ? defer$3 : nodefer;
  6328. // convert array buffers and typed array views into buffers
  6329. // at some point in the future, we may want to do the opposite!
  6330. // leave strings and buffers as-is
  6331. // anything is only allowed if in object mode, so throw
  6332. if (!this[OBJECTMODE] && !Buffer.isBuffer(chunk)) {
  6333. if (isArrayBufferView(chunk)) {
  6334. //@ts-ignore - sinful unsafe type changing
  6335. chunk = Buffer.from(chunk.buffer, chunk.byteOffset, chunk.byteLength);
  6336. }
  6337. else if (isArrayBufferLike(chunk)) {
  6338. //@ts-ignore - sinful unsafe type changing
  6339. chunk = Buffer.from(chunk);
  6340. }
  6341. else if (typeof chunk !== 'string') {
  6342. throw new Error('Non-contiguous data written to non-objectMode stream');
  6343. }
  6344. }
  6345. // handle object mode up front, since it's simpler
  6346. // this yields better performance, fewer checks later.
  6347. if (this[OBJECTMODE]) {
  6348. // maybe impossible?
  6349. /* c8 ignore start */
  6350. if (this[FLOWING] && this[BUFFERLENGTH] !== 0)
  6351. this[FLUSH](true);
  6352. /* c8 ignore stop */
  6353. if (this[FLOWING])
  6354. this.emit('data', chunk);
  6355. else
  6356. this[BUFFERPUSH](chunk);
  6357. if (this[BUFFERLENGTH] !== 0)
  6358. this.emit('readable');
  6359. if (cb)
  6360. fn(cb);
  6361. return this[FLOWING];
  6362. }
  6363. // at this point the chunk is a buffer or string
  6364. // don't buffer it up or send it to the decoder
  6365. if (!chunk.length) {
  6366. if (this[BUFFERLENGTH] !== 0)
  6367. this.emit('readable');
  6368. if (cb)
  6369. fn(cb);
  6370. return this[FLOWING];
  6371. }
  6372. // fast-path writing strings of same encoding to a stream with
  6373. // an empty buffer, skipping the buffer/decoder dance
  6374. if (typeof chunk === 'string' &&
  6375. // unless it is a string already ready for us to use
  6376. !(encoding === this[ENCODING$1] && !this[DECODER]?.lastNeed)) {
  6377. //@ts-ignore - sinful unsafe type change
  6378. chunk = Buffer.from(chunk, encoding);
  6379. }
  6380. if (Buffer.isBuffer(chunk) && this[ENCODING$1]) {
  6381. //@ts-ignore - sinful unsafe type change
  6382. chunk = this[DECODER].write(chunk);
  6383. }
  6384. // Note: flushing CAN potentially switch us into not-flowing mode
  6385. if (this[FLOWING] && this[BUFFERLENGTH] !== 0)
  6386. this[FLUSH](true);
  6387. if (this[FLOWING])
  6388. this.emit('data', chunk);
  6389. else
  6390. this[BUFFERPUSH](chunk);
  6391. if (this[BUFFERLENGTH] !== 0)
  6392. this.emit('readable');
  6393. if (cb)
  6394. fn(cb);
  6395. return this[FLOWING];
  6396. }
  6397. /**
  6398. * Low-level explicit read method.
  6399. *
  6400. * In objectMode, the argument is ignored, and one item is returned if
  6401. * available.
  6402. *
  6403. * `n` is the number of bytes (or in the case of encoding streams,
  6404. * characters) to consume. If `n` is not provided, then the entire buffer
  6405. * is returned, or `null` is returned if no data is available.
  6406. *
  6407. * If `n` is greater that the amount of data in the internal buffer,
  6408. * then `null` is returned.
  6409. */
  6410. read(n) {
  6411. if (this[DESTROYED])
  6412. return null;
  6413. this[DISCARDED] = false;
  6414. if (this[BUFFERLENGTH] === 0 ||
  6415. n === 0 ||
  6416. (n && n > this[BUFFERLENGTH])) {
  6417. this[MAYBE_EMIT_END]();
  6418. return null;
  6419. }
  6420. if (this[OBJECTMODE])
  6421. n = null;
  6422. if (this[BUFFER].length > 1 && !this[OBJECTMODE]) {
  6423. // not object mode, so if we have an encoding, then RType is string
  6424. // otherwise, must be Buffer
  6425. this[BUFFER] = [
  6426. (this[ENCODING$1]
  6427. ? this[BUFFER].join('')
  6428. : Buffer.concat(this[BUFFER], this[BUFFERLENGTH])),
  6429. ];
  6430. }
  6431. const ret = this[READ](n || null, this[BUFFER][0]);
  6432. this[MAYBE_EMIT_END]();
  6433. return ret;
  6434. }
  6435. [READ](n, chunk) {
  6436. if (this[OBJECTMODE])
  6437. this[BUFFERSHIFT]();
  6438. else {
  6439. const c = chunk;
  6440. if (n === c.length || n === null)
  6441. this[BUFFERSHIFT]();
  6442. else if (typeof c === 'string') {
  6443. this[BUFFER][0] = c.slice(n);
  6444. chunk = c.slice(0, n);
  6445. this[BUFFERLENGTH] -= n;
  6446. }
  6447. else {
  6448. this[BUFFER][0] = c.subarray(n);
  6449. chunk = c.subarray(0, n);
  6450. this[BUFFERLENGTH] -= n;
  6451. }
  6452. }
  6453. this.emit('data', chunk);
  6454. if (!this[BUFFER].length && !this[EOF])
  6455. this.emit('drain');
  6456. return chunk;
  6457. }
  6458. end(chunk, encoding, cb) {
  6459. if (typeof chunk === 'function') {
  6460. cb = chunk;
  6461. chunk = undefined;
  6462. }
  6463. if (typeof encoding === 'function') {
  6464. cb = encoding;
  6465. encoding = 'utf8';
  6466. }
  6467. if (chunk !== undefined)
  6468. this.write(chunk, encoding);
  6469. if (cb)
  6470. this.once('end', cb);
  6471. this[EOF] = true;
  6472. this.writable = false;
  6473. // if we haven't written anything, then go ahead and emit,
  6474. // even if we're not reading.
  6475. // we'll re-emit if a new 'end' listener is added anyway.
  6476. // This makes MP more suitable to write-only use cases.
  6477. if (this[FLOWING] || !this[PAUSED])
  6478. this[MAYBE_EMIT_END]();
  6479. return this;
  6480. }
  6481. // don't let the internal resume be overwritten
  6482. [RESUME]() {
  6483. if (this[DESTROYED])
  6484. return;
  6485. if (!this[DATALISTENERS] && !this[PIPES].length) {
  6486. this[DISCARDED] = true;
  6487. }
  6488. this[PAUSED] = false;
  6489. this[FLOWING] = true;
  6490. this.emit('resume');
  6491. if (this[BUFFER].length)
  6492. this[FLUSH]();
  6493. else if (this[EOF])
  6494. this[MAYBE_EMIT_END]();
  6495. else
  6496. this.emit('drain');
  6497. }
  6498. /**
  6499. * Resume the stream if it is currently in a paused state
  6500. *
  6501. * If called when there are no pipe destinations or `data` event listeners,
  6502. * this will place the stream in a "discarded" state, where all data will
  6503. * be thrown away. The discarded state is removed if a pipe destination or
  6504. * data handler is added, if pause() is called, or if any synchronous or
  6505. * asynchronous iteration is started.
  6506. */
  6507. resume() {
  6508. return this[RESUME]();
  6509. }
  6510. /**
  6511. * Pause the stream
  6512. */
  6513. pause() {
  6514. this[FLOWING] = false;
  6515. this[PAUSED] = true;
  6516. this[DISCARDED] = false;
  6517. }
  6518. /**
  6519. * true if the stream has been forcibly destroyed
  6520. */
  6521. get destroyed() {
  6522. return this[DESTROYED];
  6523. }
  6524. /**
  6525. * true if the stream is currently in a flowing state, meaning that
  6526. * any writes will be immediately emitted.
  6527. */
  6528. get flowing() {
  6529. return this[FLOWING];
  6530. }
  6531. /**
  6532. * true if the stream is currently in a paused state
  6533. */
  6534. get paused() {
  6535. return this[PAUSED];
  6536. }
  6537. [BUFFERPUSH](chunk) {
  6538. if (this[OBJECTMODE])
  6539. this[BUFFERLENGTH] += 1;
  6540. else
  6541. this[BUFFERLENGTH] += chunk.length;
  6542. this[BUFFER].push(chunk);
  6543. }
  6544. [BUFFERSHIFT]() {
  6545. if (this[OBJECTMODE])
  6546. this[BUFFERLENGTH] -= 1;
  6547. else
  6548. this[BUFFERLENGTH] -= this[BUFFER][0].length;
  6549. return this[BUFFER].shift();
  6550. }
  6551. [FLUSH](noDrain = false) {
  6552. do { } while (this[FLUSHCHUNK](this[BUFFERSHIFT]()) &&
  6553. this[BUFFER].length);
  6554. if (!noDrain && !this[BUFFER].length && !this[EOF])
  6555. this.emit('drain');
  6556. }
  6557. [FLUSHCHUNK](chunk) {
  6558. this.emit('data', chunk);
  6559. return this[FLOWING];
  6560. }
  6561. /**
  6562. * Pipe all data emitted by this stream into the destination provided.
  6563. *
  6564. * Triggers the flow of data.
  6565. */
  6566. pipe(dest, opts) {
  6567. if (this[DESTROYED])
  6568. return dest;
  6569. this[DISCARDED] = false;
  6570. const ended = this[EMITTED_END];
  6571. opts = opts || {};
  6572. if (dest === proc.stdout || dest === proc.stderr)
  6573. opts.end = false;
  6574. else
  6575. opts.end = opts.end !== false;
  6576. opts.proxyErrors = !!opts.proxyErrors;
  6577. // piping an ended stream ends immediately
  6578. if (ended) {
  6579. if (opts.end)
  6580. dest.end();
  6581. }
  6582. else {
  6583. // "as" here just ignores the WType, which pipes don't care about,
  6584. // since they're only consuming from us, and writing to the dest
  6585. this[PIPES].push(!opts.proxyErrors
  6586. ? new Pipe(this, dest, opts)
  6587. : new PipeProxyErrors(this, dest, opts));
  6588. if (this[ASYNC])
  6589. defer$3(() => this[RESUME]());
  6590. else
  6591. this[RESUME]();
  6592. }
  6593. return dest;
  6594. }
  6595. /**
  6596. * Fully unhook a piped destination stream.
  6597. *
  6598. * If the destination stream was the only consumer of this stream (ie,
  6599. * there are no other piped destinations or `'data'` event listeners)
  6600. * then the flow of data will stop until there is another consumer or
  6601. * {@link Minipass#resume} is explicitly called.
  6602. */
  6603. unpipe(dest) {
  6604. const p = this[PIPES].find(p => p.dest === dest);
  6605. if (p) {
  6606. if (this[PIPES].length === 1) {
  6607. if (this[FLOWING] && this[DATALISTENERS] === 0) {
  6608. this[FLOWING] = false;
  6609. }
  6610. this[PIPES] = [];
  6611. }
  6612. else
  6613. this[PIPES].splice(this[PIPES].indexOf(p), 1);
  6614. p.unpipe();
  6615. }
  6616. }
  6617. /**
  6618. * Alias for {@link Minipass#on}
  6619. */
  6620. addListener(ev, handler) {
  6621. return this.on(ev, handler);
  6622. }
  6623. /**
  6624. * Mostly identical to `EventEmitter.on`, with the following
  6625. * behavior differences to prevent data loss and unnecessary hangs:
  6626. *
  6627. * - Adding a 'data' event handler will trigger the flow of data
  6628. *
  6629. * - Adding a 'readable' event handler when there is data waiting to be read
  6630. * will cause 'readable' to be emitted immediately.
  6631. *
  6632. * - Adding an 'endish' event handler ('end', 'finish', etc.) which has
  6633. * already passed will cause the event to be emitted immediately and all
  6634. * handlers removed.
  6635. *
  6636. * - Adding an 'error' event handler after an error has been emitted will
  6637. * cause the event to be re-emitted immediately with the error previously
  6638. * raised.
  6639. */
  6640. on(ev, handler) {
  6641. const ret = super.on(ev, handler);
  6642. if (ev === 'data') {
  6643. this[DISCARDED] = false;
  6644. this[DATALISTENERS]++;
  6645. if (!this[PIPES].length && !this[FLOWING]) {
  6646. this[RESUME]();
  6647. }
  6648. }
  6649. else if (ev === 'readable' && this[BUFFERLENGTH] !== 0) {
  6650. super.emit('readable');
  6651. }
  6652. else if (isEndish(ev) && this[EMITTED_END]) {
  6653. super.emit(ev);
  6654. this.removeAllListeners(ev);
  6655. }
  6656. else if (ev === 'error' && this[EMITTED_ERROR]) {
  6657. const h = handler;
  6658. if (this[ASYNC])
  6659. defer$3(() => h.call(this, this[EMITTED_ERROR]));
  6660. else
  6661. h.call(this, this[EMITTED_ERROR]);
  6662. }
  6663. return ret;
  6664. }
  6665. /**
  6666. * Alias for {@link Minipass#off}
  6667. */
  6668. removeListener(ev, handler) {
  6669. return this.off(ev, handler);
  6670. }
  6671. /**
  6672. * Mostly identical to `EventEmitter.off`
  6673. *
  6674. * If a 'data' event handler is removed, and it was the last consumer
  6675. * (ie, there are no pipe destinations or other 'data' event listeners),
  6676. * then the flow of data will stop until there is another consumer or
  6677. * {@link Minipass#resume} is explicitly called.
  6678. */
  6679. off(ev, handler) {
  6680. const ret = super.off(ev, handler);
  6681. // if we previously had listeners, and now we don't, and we don't
  6682. // have any pipes, then stop the flow, unless it's been explicitly
  6683. // put in a discarded flowing state via stream.resume().
  6684. if (ev === 'data') {
  6685. this[DATALISTENERS] = this.listeners('data').length;
  6686. if (this[DATALISTENERS] === 0 &&
  6687. !this[DISCARDED] &&
  6688. !this[PIPES].length) {
  6689. this[FLOWING] = false;
  6690. }
  6691. }
  6692. return ret;
  6693. }
  6694. /**
  6695. * Mostly identical to `EventEmitter.removeAllListeners`
  6696. *
  6697. * If all 'data' event handlers are removed, and they were the last consumer
  6698. * (ie, there are no pipe destinations), then the flow of data will stop
  6699. * until there is another consumer or {@link Minipass#resume} is explicitly
  6700. * called.
  6701. */
  6702. removeAllListeners(ev) {
  6703. const ret = super.removeAllListeners(ev);
  6704. if (ev === 'data' || ev === undefined) {
  6705. this[DATALISTENERS] = 0;
  6706. if (!this[DISCARDED] && !this[PIPES].length) {
  6707. this[FLOWING] = false;
  6708. }
  6709. }
  6710. return ret;
  6711. }
  6712. /**
  6713. * true if the 'end' event has been emitted
  6714. */
  6715. get emittedEnd() {
  6716. return this[EMITTED_END];
  6717. }
  6718. [MAYBE_EMIT_END]() {
  6719. if (!this[EMITTING_END] &&
  6720. !this[EMITTED_END] &&
  6721. !this[DESTROYED] &&
  6722. this[BUFFER].length === 0 &&
  6723. this[EOF]) {
  6724. this[EMITTING_END] = true;
  6725. this.emit('end');
  6726. this.emit('prefinish');
  6727. this.emit('finish');
  6728. if (this[CLOSED$1])
  6729. this.emit('close');
  6730. this[EMITTING_END] = false;
  6731. }
  6732. }
  6733. /**
  6734. * Mostly identical to `EventEmitter.emit`, with the following
  6735. * behavior differences to prevent data loss and unnecessary hangs:
  6736. *
  6737. * If the stream has been destroyed, and the event is something other
  6738. * than 'close' or 'error', then `false` is returned and no handlers
  6739. * are called.
  6740. *
  6741. * If the event is 'end', and has already been emitted, then the event
  6742. * is ignored. If the stream is in a paused or non-flowing state, then
  6743. * the event will be deferred until data flow resumes. If the stream is
  6744. * async, then handlers will be called on the next tick rather than
  6745. * immediately.
  6746. *
  6747. * If the event is 'close', and 'end' has not yet been emitted, then
  6748. * the event will be deferred until after 'end' is emitted.
  6749. *
  6750. * If the event is 'error', and an AbortSignal was provided for the stream,
  6751. * and there are no listeners, then the event is ignored, matching the
  6752. * behavior of node core streams in the presense of an AbortSignal.
  6753. *
  6754. * If the event is 'finish' or 'prefinish', then all listeners will be
  6755. * removed after emitting the event, to prevent double-firing.
  6756. */
  6757. emit(ev, ...args) {
  6758. const data = args[0];
  6759. // error and close are only events allowed after calling destroy()
  6760. if (ev !== 'error' &&
  6761. ev !== 'close' &&
  6762. ev !== DESTROYED &&
  6763. this[DESTROYED]) {
  6764. return false;
  6765. }
  6766. else if (ev === 'data') {
  6767. return !this[OBJECTMODE] && !data
  6768. ? false
  6769. : this[ASYNC]
  6770. ? (defer$3(() => this[EMITDATA](data)), true)
  6771. : this[EMITDATA](data);
  6772. }
  6773. else if (ev === 'end') {
  6774. return this[EMITEND]();
  6775. }
  6776. else if (ev === 'close') {
  6777. this[CLOSED$1] = true;
  6778. // don't emit close before 'end' and 'finish'
  6779. if (!this[EMITTED_END] && !this[DESTROYED])
  6780. return false;
  6781. const ret = super.emit('close');
  6782. this.removeAllListeners('close');
  6783. return ret;
  6784. }
  6785. else if (ev === 'error') {
  6786. this[EMITTED_ERROR] = data;
  6787. super.emit(ERROR, data);
  6788. const ret = !this[SIGNAL] || this.listeners('error').length
  6789. ? super.emit('error', data)
  6790. : false;
  6791. this[MAYBE_EMIT_END]();
  6792. return ret;
  6793. }
  6794. else if (ev === 'resume') {
  6795. const ret = super.emit('resume');
  6796. this[MAYBE_EMIT_END]();
  6797. return ret;
  6798. }
  6799. else if (ev === 'finish' || ev === 'prefinish') {
  6800. const ret = super.emit(ev);
  6801. this.removeAllListeners(ev);
  6802. return ret;
  6803. }
  6804. // Some other unknown event
  6805. const ret = super.emit(ev, ...args);
  6806. this[MAYBE_EMIT_END]();
  6807. return ret;
  6808. }
  6809. [EMITDATA](data) {
  6810. for (const p of this[PIPES]) {
  6811. if (p.dest.write(data) === false)
  6812. this.pause();
  6813. }
  6814. const ret = this[DISCARDED] ? false : super.emit('data', data);
  6815. this[MAYBE_EMIT_END]();
  6816. return ret;
  6817. }
  6818. [EMITEND]() {
  6819. if (this[EMITTED_END])
  6820. return false;
  6821. this[EMITTED_END] = true;
  6822. this.readable = false;
  6823. return this[ASYNC]
  6824. ? (defer$3(() => this[EMITEND2]()), true)
  6825. : this[EMITEND2]();
  6826. }
  6827. [EMITEND2]() {
  6828. if (this[DECODER]) {
  6829. const data = this[DECODER].end();
  6830. if (data) {
  6831. for (const p of this[PIPES]) {
  6832. p.dest.write(data);
  6833. }
  6834. if (!this[DISCARDED])
  6835. super.emit('data', data);
  6836. }
  6837. }
  6838. for (const p of this[PIPES]) {
  6839. p.end();
  6840. }
  6841. const ret = super.emit('end');
  6842. this.removeAllListeners('end');
  6843. return ret;
  6844. }
  6845. /**
  6846. * Return a Promise that resolves to an array of all emitted data once
  6847. * the stream ends.
  6848. */
  6849. async collect() {
  6850. const buf = Object.assign([], {
  6851. dataLength: 0,
  6852. });
  6853. if (!this[OBJECTMODE])
  6854. buf.dataLength = 0;
  6855. // set the promise first, in case an error is raised
  6856. // by triggering the flow here.
  6857. const p = this.promise();
  6858. this.on('data', c => {
  6859. buf.push(c);
  6860. if (!this[OBJECTMODE])
  6861. buf.dataLength += c.length;
  6862. });
  6863. await p;
  6864. return buf;
  6865. }
  6866. /**
  6867. * Return a Promise that resolves to the concatenation of all emitted data
  6868. * once the stream ends.
  6869. *
  6870. * Not allowed on objectMode streams.
  6871. */
  6872. async concat() {
  6873. if (this[OBJECTMODE]) {
  6874. throw new Error('cannot concat in objectMode');
  6875. }
  6876. const buf = await this.collect();
  6877. return (this[ENCODING$1]
  6878. ? buf.join('')
  6879. : Buffer.concat(buf, buf.dataLength));
  6880. }
  6881. /**
  6882. * Return a void Promise that resolves once the stream ends.
  6883. */
  6884. async promise() {
  6885. return new Promise((resolve, reject) => {
  6886. this.on(DESTROYED, () => reject(new Error('stream destroyed')));
  6887. this.on('error', er => reject(er));
  6888. this.on('end', () => resolve());
  6889. });
  6890. }
  6891. /**
  6892. * Asynchronous `for await of` iteration.
  6893. *
  6894. * This will continue emitting all chunks until the stream terminates.
  6895. */
  6896. [Symbol.asyncIterator]() {
  6897. // set this up front, in case the consumer doesn't call next()
  6898. // right away.
  6899. this[DISCARDED] = false;
  6900. let stopped = false;
  6901. const stop = async () => {
  6902. this.pause();
  6903. stopped = true;
  6904. return { value: undefined, done: true };
  6905. };
  6906. const next = () => {
  6907. if (stopped)
  6908. return stop();
  6909. const res = this.read();
  6910. if (res !== null)
  6911. return Promise.resolve({ done: false, value: res });
  6912. if (this[EOF])
  6913. return stop();
  6914. let resolve;
  6915. let reject;
  6916. const onerr = (er) => {
  6917. this.off('data', ondata);
  6918. this.off('end', onend);
  6919. this.off(DESTROYED, ondestroy);
  6920. stop();
  6921. reject(er);
  6922. };
  6923. const ondata = (value) => {
  6924. this.off('error', onerr);
  6925. this.off('end', onend);
  6926. this.off(DESTROYED, ondestroy);
  6927. this.pause();
  6928. resolve({ value, done: !!this[EOF] });
  6929. };
  6930. const onend = () => {
  6931. this.off('error', onerr);
  6932. this.off('data', ondata);
  6933. this.off(DESTROYED, ondestroy);
  6934. stop();
  6935. resolve({ done: true, value: undefined });
  6936. };
  6937. const ondestroy = () => onerr(new Error('stream destroyed'));
  6938. return new Promise((res, rej) => {
  6939. reject = rej;
  6940. resolve = res;
  6941. this.once(DESTROYED, ondestroy);
  6942. this.once('error', onerr);
  6943. this.once('end', onend);
  6944. this.once('data', ondata);
  6945. });
  6946. };
  6947. return {
  6948. next,
  6949. throw: stop,
  6950. return: stop,
  6951. [Symbol.asyncIterator]() {
  6952. return this;
  6953. },
  6954. };
  6955. }
  6956. /**
  6957. * Synchronous `for of` iteration.
  6958. *
  6959. * The iteration will terminate when the internal buffer runs out, even
  6960. * if the stream has not yet terminated.
  6961. */
  6962. [Symbol.iterator]() {
  6963. // set this up front, in case the consumer doesn't call next()
  6964. // right away.
  6965. this[DISCARDED] = false;
  6966. let stopped = false;
  6967. const stop = () => {
  6968. this.pause();
  6969. this.off(ERROR, stop);
  6970. this.off(DESTROYED, stop);
  6971. this.off('end', stop);
  6972. stopped = true;
  6973. return { done: true, value: undefined };
  6974. };
  6975. const next = () => {
  6976. if (stopped)
  6977. return stop();
  6978. const value = this.read();
  6979. return value === null ? stop() : { done: false, value };
  6980. };
  6981. this.once('end', stop);
  6982. this.once(ERROR, stop);
  6983. this.once(DESTROYED, stop);
  6984. return {
  6985. next,
  6986. throw: stop,
  6987. return: stop,
  6988. [Symbol.iterator]() {
  6989. return this;
  6990. },
  6991. };
  6992. }
  6993. /**
  6994. * Destroy a stream, preventing it from being used for any further purpose.
  6995. *
  6996. * If the stream has a `close()` method, then it will be called on
  6997. * destruction.
  6998. *
  6999. * After destruction, any attempt to write data, read data, or emit most
  7000. * events will be ignored.
  7001. *
  7002. * If an error argument is provided, then it will be emitted in an
  7003. * 'error' event.
  7004. */
  7005. destroy(er) {
  7006. if (this[DESTROYED]) {
  7007. if (er)
  7008. this.emit('error', er);
  7009. else
  7010. this.emit(DESTROYED);
  7011. return this;
  7012. }
  7013. this[DESTROYED] = true;
  7014. this[DISCARDED] = true;
  7015. // throw away all buffered data, it's never coming out
  7016. this[BUFFER].length = 0;
  7017. this[BUFFERLENGTH] = 0;
  7018. const wc = this;
  7019. if (typeof wc.close === 'function' && !this[CLOSED$1])
  7020. wc.close();
  7021. if (er)
  7022. this.emit('error', er);
  7023. // if no error to emit, still reject pending promises
  7024. else
  7025. this.emit(DESTROYED);
  7026. return this;
  7027. }
  7028. /**
  7029. * Alias for {@link isStream}
  7030. *
  7031. * Former export location, maintained for backwards compatibility.
  7032. *
  7033. * @deprecated
  7034. */
  7035. static get isStream() {
  7036. return isStream;
  7037. }
  7038. }
  7039. const realpathSync = realpathSync$1.native;
  7040. const defaultFS = {
  7041. lstatSync,
  7042. readdir: readdir$4,
  7043. readdirSync,
  7044. readlinkSync,
  7045. realpathSync,
  7046. promises: {
  7047. lstat: lstat$3,
  7048. readdir: readdir$5,
  7049. readlink,
  7050. realpath: realpath$2,
  7051. },
  7052. };
  7053. // if they just gave us require('fs') then use our default
  7054. const fsFromOption = (fsOption) => !fsOption || fsOption === defaultFS || fsOption === fs$j ?
  7055. defaultFS
  7056. : {
  7057. ...defaultFS,
  7058. ...fsOption,
  7059. promises: {
  7060. ...defaultFS.promises,
  7061. ...(fsOption.promises || {}),
  7062. },
  7063. };
  7064. // turn something like //?/c:/ into c:\
  7065. const uncDriveRegexp = /^\\\\\?\\([a-z]:)\\?$/i;
  7066. const uncToDrive = (rootPath) => rootPath.replace(/\//g, '\\').replace(uncDriveRegexp, '$1\\');
  7067. // windows paths are separated by either / or \
  7068. const eitherSep = /[\\\/]/;
  7069. const UNKNOWN = 0; // may not even exist, for all we know
  7070. const IFIFO = 0b0001;
  7071. const IFCHR = 0b0010;
  7072. const IFDIR = 0b0100;
  7073. const IFBLK = 0b0110;
  7074. const IFREG = 0b1000;
  7075. const IFLNK = 0b1010;
  7076. const IFSOCK = 0b1100;
  7077. const IFMT = 0b1111;
  7078. // mask to unset low 4 bits
  7079. const IFMT_UNKNOWN = ~IFMT;
  7080. // set after successfully calling readdir() and getting entries.
  7081. const READDIR_CALLED = 0b0000_0001_0000;
  7082. // set after a successful lstat()
  7083. const LSTAT_CALLED = 0b0000_0010_0000;
  7084. // set if an entry (or one of its parents) is definitely not a dir
  7085. const ENOTDIR = 0b0000_0100_0000;
  7086. // set if an entry (or one of its parents) does not exist
  7087. // (can also be set on lstat errors like EACCES or ENAMETOOLONG)
  7088. const ENOENT = 0b0000_1000_0000;
  7089. // cannot have child entries -- also verify &IFMT is either IFDIR or IFLNK
  7090. // set if we fail to readlink
  7091. const ENOREADLINK = 0b0001_0000_0000;
  7092. // set if we know realpath() will fail
  7093. const ENOREALPATH = 0b0010_0000_0000;
  7094. const ENOCHILD = ENOTDIR | ENOENT | ENOREALPATH;
  7095. const TYPEMASK = 0b0011_1111_1111;
  7096. const entToType = (s) => s.isFile() ? IFREG
  7097. : s.isDirectory() ? IFDIR
  7098. : s.isSymbolicLink() ? IFLNK
  7099. : s.isCharacterDevice() ? IFCHR
  7100. : s.isBlockDevice() ? IFBLK
  7101. : s.isSocket() ? IFSOCK
  7102. : s.isFIFO() ? IFIFO
  7103. : UNKNOWN;
  7104. // normalize unicode path names
  7105. const normalizeCache = new Map();
  7106. const normalize = (s) => {
  7107. const c = normalizeCache.get(s);
  7108. if (c)
  7109. return c;
  7110. const n = s.normalize('NFKD');
  7111. normalizeCache.set(s, n);
  7112. return n;
  7113. };
  7114. const normalizeNocaseCache = new Map();
  7115. const normalizeNocase = (s) => {
  7116. const c = normalizeNocaseCache.get(s);
  7117. if (c)
  7118. return c;
  7119. const n = normalize(s.toLowerCase());
  7120. normalizeNocaseCache.set(s, n);
  7121. return n;
  7122. };
  7123. /**
  7124. * An LRUCache for storing resolved path strings or Path objects.
  7125. * @internal
  7126. */
  7127. class ResolveCache extends LRUCache {
  7128. constructor() {
  7129. super({ max: 256 });
  7130. }
  7131. }
  7132. // In order to prevent blowing out the js heap by allocating hundreds of
  7133. // thousands of Path entries when walking extremely large trees, the "children"
  7134. // in this tree are represented by storing an array of Path entries in an
  7135. // LRUCache, indexed by the parent. At any time, Path.children() may return an
  7136. // empty array, indicating that it doesn't know about any of its children, and
  7137. // thus has to rebuild that cache. This is fine, it just means that we don't
  7138. // benefit as much from having the cached entries, but huge directory walks
  7139. // don't blow out the stack, and smaller ones are still as fast as possible.
  7140. //
  7141. //It does impose some complexity when building up the readdir data, because we
  7142. //need to pass a reference to the children array that we started with.
  7143. /**
  7144. * an LRUCache for storing child entries.
  7145. * @internal
  7146. */
  7147. class ChildrenCache extends LRUCache {
  7148. constructor(maxSize = 16 * 1024) {
  7149. super({
  7150. maxSize,
  7151. // parent + children
  7152. sizeCalculation: a => a.length + 1,
  7153. });
  7154. }
  7155. }
  7156. const setAsCwd = Symbol('PathScurry setAsCwd');
  7157. /**
  7158. * Path objects are sort of like a super-powered
  7159. * {@link https://nodejs.org/docs/latest/api/fs.html#class-fsdirent fs.Dirent}
  7160. *
  7161. * Each one represents a single filesystem entry on disk, which may or may not
  7162. * exist. It includes methods for reading various types of information via
  7163. * lstat, readlink, and readdir, and caches all information to the greatest
  7164. * degree possible.
  7165. *
  7166. * Note that fs operations that would normally throw will instead return an
  7167. * "empty" value. This is in order to prevent excessive overhead from error
  7168. * stack traces.
  7169. */
  7170. class PathBase {
  7171. /**
  7172. * the basename of this path
  7173. *
  7174. * **Important**: *always* test the path name against any test string
  7175. * usingthe {@link isNamed} method, and not by directly comparing this
  7176. * string. Otherwise, unicode path strings that the system sees as identical
  7177. * will not be properly treated as the same path, leading to incorrect
  7178. * behavior and possible security issues.
  7179. */
  7180. name;
  7181. /**
  7182. * the Path entry corresponding to the path root.
  7183. *
  7184. * @internal
  7185. */
  7186. root;
  7187. /**
  7188. * All roots found within the current PathScurry family
  7189. *
  7190. * @internal
  7191. */
  7192. roots;
  7193. /**
  7194. * a reference to the parent path, or undefined in the case of root entries
  7195. *
  7196. * @internal
  7197. */
  7198. parent;
  7199. /**
  7200. * boolean indicating whether paths are compared case-insensitively
  7201. * @internal
  7202. */
  7203. nocase;
  7204. /**
  7205. * boolean indicating that this path is the current working directory
  7206. * of the PathScurry collection that contains it.
  7207. */
  7208. isCWD = false;
  7209. // potential default fs override
  7210. #fs;
  7211. // Stats fields
  7212. #dev;
  7213. get dev() {
  7214. return this.#dev;
  7215. }
  7216. #mode;
  7217. get mode() {
  7218. return this.#mode;
  7219. }
  7220. #nlink;
  7221. get nlink() {
  7222. return this.#nlink;
  7223. }
  7224. #uid;
  7225. get uid() {
  7226. return this.#uid;
  7227. }
  7228. #gid;
  7229. get gid() {
  7230. return this.#gid;
  7231. }
  7232. #rdev;
  7233. get rdev() {
  7234. return this.#rdev;
  7235. }
  7236. #blksize;
  7237. get blksize() {
  7238. return this.#blksize;
  7239. }
  7240. #ino;
  7241. get ino() {
  7242. return this.#ino;
  7243. }
  7244. #size;
  7245. get size() {
  7246. return this.#size;
  7247. }
  7248. #blocks;
  7249. get blocks() {
  7250. return this.#blocks;
  7251. }
  7252. #atimeMs;
  7253. get atimeMs() {
  7254. return this.#atimeMs;
  7255. }
  7256. #mtimeMs;
  7257. get mtimeMs() {
  7258. return this.#mtimeMs;
  7259. }
  7260. #ctimeMs;
  7261. get ctimeMs() {
  7262. return this.#ctimeMs;
  7263. }
  7264. #birthtimeMs;
  7265. get birthtimeMs() {
  7266. return this.#birthtimeMs;
  7267. }
  7268. #atime;
  7269. get atime() {
  7270. return this.#atime;
  7271. }
  7272. #mtime;
  7273. get mtime() {
  7274. return this.#mtime;
  7275. }
  7276. #ctime;
  7277. get ctime() {
  7278. return this.#ctime;
  7279. }
  7280. #birthtime;
  7281. get birthtime() {
  7282. return this.#birthtime;
  7283. }
  7284. #matchName;
  7285. #depth;
  7286. #fullpath;
  7287. #fullpathPosix;
  7288. #relative;
  7289. #relativePosix;
  7290. #type;
  7291. #children;
  7292. #linkTarget;
  7293. #realpath;
  7294. /**
  7295. * This property is for compatibility with the Dirent class as of
  7296. * Node v20, where Dirent['parentPath'] refers to the path of the
  7297. * directory that was passed to readdir. For root entries, it's the path
  7298. * to the entry itself.
  7299. */
  7300. get parentPath() {
  7301. return (this.parent || this).fullpath();
  7302. }
  7303. /**
  7304. * Deprecated alias for Dirent['parentPath'] Somewhat counterintuitively,
  7305. * this property refers to the *parent* path, not the path object itself.
  7306. */
  7307. get path() {
  7308. return this.parentPath;
  7309. }
  7310. /**
  7311. * Do not create new Path objects directly. They should always be accessed
  7312. * via the PathScurry class or other methods on the Path class.
  7313. *
  7314. * @internal
  7315. */
  7316. constructor(name, type = UNKNOWN, root, roots, nocase, children, opts) {
  7317. this.name = name;
  7318. this.#matchName = nocase ? normalizeNocase(name) : normalize(name);
  7319. this.#type = type & TYPEMASK;
  7320. this.nocase = nocase;
  7321. this.roots = roots;
  7322. this.root = root || this;
  7323. this.#children = children;
  7324. this.#fullpath = opts.fullpath;
  7325. this.#relative = opts.relative;
  7326. this.#relativePosix = opts.relativePosix;
  7327. this.parent = opts.parent;
  7328. if (this.parent) {
  7329. this.#fs = this.parent.#fs;
  7330. }
  7331. else {
  7332. this.#fs = fsFromOption(opts.fs);
  7333. }
  7334. }
  7335. /**
  7336. * Returns the depth of the Path object from its root.
  7337. *
  7338. * For example, a path at `/foo/bar` would have a depth of 2.
  7339. */
  7340. depth() {
  7341. if (this.#depth !== undefined)
  7342. return this.#depth;
  7343. if (!this.parent)
  7344. return (this.#depth = 0);
  7345. return (this.#depth = this.parent.depth() + 1);
  7346. }
  7347. /**
  7348. * @internal
  7349. */
  7350. childrenCache() {
  7351. return this.#children;
  7352. }
  7353. /**
  7354. * Get the Path object referenced by the string path, resolved from this Path
  7355. */
  7356. resolve(path) {
  7357. if (!path) {
  7358. return this;
  7359. }
  7360. const rootPath = this.getRootString(path);
  7361. const dir = path.substring(rootPath.length);
  7362. const dirParts = dir.split(this.splitSep);
  7363. const result = rootPath ?
  7364. this.getRoot(rootPath).#resolveParts(dirParts)
  7365. : this.#resolveParts(dirParts);
  7366. return result;
  7367. }
  7368. #resolveParts(dirParts) {
  7369. let p = this;
  7370. for (const part of dirParts) {
  7371. p = p.child(part);
  7372. }
  7373. return p;
  7374. }
  7375. /**
  7376. * Returns the cached children Path objects, if still available. If they
  7377. * have fallen out of the cache, then returns an empty array, and resets the
  7378. * READDIR_CALLED bit, so that future calls to readdir() will require an fs
  7379. * lookup.
  7380. *
  7381. * @internal
  7382. */
  7383. children() {
  7384. const cached = this.#children.get(this);
  7385. if (cached) {
  7386. return cached;
  7387. }
  7388. const children = Object.assign([], { provisional: 0 });
  7389. this.#children.set(this, children);
  7390. this.#type &= ~READDIR_CALLED;
  7391. return children;
  7392. }
  7393. /**
  7394. * Resolves a path portion and returns or creates the child Path.
  7395. *
  7396. * Returns `this` if pathPart is `''` or `'.'`, or `parent` if pathPart is
  7397. * `'..'`.
  7398. *
  7399. * This should not be called directly. If `pathPart` contains any path
  7400. * separators, it will lead to unsafe undefined behavior.
  7401. *
  7402. * Use `Path.resolve()` instead.
  7403. *
  7404. * @internal
  7405. */
  7406. child(pathPart, opts) {
  7407. if (pathPart === '' || pathPart === '.') {
  7408. return this;
  7409. }
  7410. if (pathPart === '..') {
  7411. return this.parent || this;
  7412. }
  7413. // find the child
  7414. const children = this.children();
  7415. const name = this.nocase ? normalizeNocase(pathPart) : normalize(pathPart);
  7416. for (const p of children) {
  7417. if (p.#matchName === name) {
  7418. return p;
  7419. }
  7420. }
  7421. // didn't find it, create provisional child, since it might not
  7422. // actually exist. If we know the parent isn't a dir, then
  7423. // in fact it CAN'T exist.
  7424. const s = this.parent ? this.sep : '';
  7425. const fullpath = this.#fullpath ? this.#fullpath + s + pathPart : undefined;
  7426. const pchild = this.newChild(pathPart, UNKNOWN, {
  7427. ...opts,
  7428. parent: this,
  7429. fullpath,
  7430. });
  7431. if (!this.canReaddir()) {
  7432. pchild.#type |= ENOENT;
  7433. }
  7434. // don't have to update provisional, because if we have real children,
  7435. // then provisional is set to children.length, otherwise a lower number
  7436. children.push(pchild);
  7437. return pchild;
  7438. }
  7439. /**
  7440. * The relative path from the cwd. If it does not share an ancestor with
  7441. * the cwd, then this ends up being equivalent to the fullpath()
  7442. */
  7443. relative() {
  7444. if (this.isCWD)
  7445. return '';
  7446. if (this.#relative !== undefined) {
  7447. return this.#relative;
  7448. }
  7449. const name = this.name;
  7450. const p = this.parent;
  7451. if (!p) {
  7452. return (this.#relative = this.name);
  7453. }
  7454. const pv = p.relative();
  7455. return pv + (!pv || !p.parent ? '' : this.sep) + name;
  7456. }
  7457. /**
  7458. * The relative path from the cwd, using / as the path separator.
  7459. * If it does not share an ancestor with
  7460. * the cwd, then this ends up being equivalent to the fullpathPosix()
  7461. * On posix systems, this is identical to relative().
  7462. */
  7463. relativePosix() {
  7464. if (this.sep === '/')
  7465. return this.relative();
  7466. if (this.isCWD)
  7467. return '';
  7468. if (this.#relativePosix !== undefined)
  7469. return this.#relativePosix;
  7470. const name = this.name;
  7471. const p = this.parent;
  7472. if (!p) {
  7473. return (this.#relativePosix = this.fullpathPosix());
  7474. }
  7475. const pv = p.relativePosix();
  7476. return pv + (!pv || !p.parent ? '' : '/') + name;
  7477. }
  7478. /**
  7479. * The fully resolved path string for this Path entry
  7480. */
  7481. fullpath() {
  7482. if (this.#fullpath !== undefined) {
  7483. return this.#fullpath;
  7484. }
  7485. const name = this.name;
  7486. const p = this.parent;
  7487. if (!p) {
  7488. return (this.#fullpath = this.name);
  7489. }
  7490. const pv = p.fullpath();
  7491. const fp = pv + (!p.parent ? '' : this.sep) + name;
  7492. return (this.#fullpath = fp);
  7493. }
  7494. /**
  7495. * On platforms other than windows, this is identical to fullpath.
  7496. *
  7497. * On windows, this is overridden to return the forward-slash form of the
  7498. * full UNC path.
  7499. */
  7500. fullpathPosix() {
  7501. if (this.#fullpathPosix !== undefined)
  7502. return this.#fullpathPosix;
  7503. if (this.sep === '/')
  7504. return (this.#fullpathPosix = this.fullpath());
  7505. if (!this.parent) {
  7506. const p = this.fullpath().replace(/\\/g, '/');
  7507. if (/^[a-z]:\//i.test(p)) {
  7508. return (this.#fullpathPosix = `//?/${p}`);
  7509. }
  7510. else {
  7511. return (this.#fullpathPosix = p);
  7512. }
  7513. }
  7514. const p = this.parent;
  7515. const pfpp = p.fullpathPosix();
  7516. const fpp = pfpp + (!pfpp || !p.parent ? '' : '/') + this.name;
  7517. return (this.#fullpathPosix = fpp);
  7518. }
  7519. /**
  7520. * Is the Path of an unknown type?
  7521. *
  7522. * Note that we might know *something* about it if there has been a previous
  7523. * filesystem operation, for example that it does not exist, or is not a
  7524. * link, or whether it has child entries.
  7525. */
  7526. isUnknown() {
  7527. return (this.#type & IFMT) === UNKNOWN;
  7528. }
  7529. isType(type) {
  7530. return this[`is${type}`]();
  7531. }
  7532. getType() {
  7533. return (this.isUnknown() ? 'Unknown'
  7534. : this.isDirectory() ? 'Directory'
  7535. : this.isFile() ? 'File'
  7536. : this.isSymbolicLink() ? 'SymbolicLink'
  7537. : this.isFIFO() ? 'FIFO'
  7538. : this.isCharacterDevice() ? 'CharacterDevice'
  7539. : this.isBlockDevice() ? 'BlockDevice'
  7540. : /* c8 ignore start */ this.isSocket() ? 'Socket'
  7541. : 'Unknown');
  7542. /* c8 ignore stop */
  7543. }
  7544. /**
  7545. * Is the Path a regular file?
  7546. */
  7547. isFile() {
  7548. return (this.#type & IFMT) === IFREG;
  7549. }
  7550. /**
  7551. * Is the Path a directory?
  7552. */
  7553. isDirectory() {
  7554. return (this.#type & IFMT) === IFDIR;
  7555. }
  7556. /**
  7557. * Is the path a character device?
  7558. */
  7559. isCharacterDevice() {
  7560. return (this.#type & IFMT) === IFCHR;
  7561. }
  7562. /**
  7563. * Is the path a block device?
  7564. */
  7565. isBlockDevice() {
  7566. return (this.#type & IFMT) === IFBLK;
  7567. }
  7568. /**
  7569. * Is the path a FIFO pipe?
  7570. */
  7571. isFIFO() {
  7572. return (this.#type & IFMT) === IFIFO;
  7573. }
  7574. /**
  7575. * Is the path a socket?
  7576. */
  7577. isSocket() {
  7578. return (this.#type & IFMT) === IFSOCK;
  7579. }
  7580. /**
  7581. * Is the path a symbolic link?
  7582. */
  7583. isSymbolicLink() {
  7584. return (this.#type & IFLNK) === IFLNK;
  7585. }
  7586. /**
  7587. * Return the entry if it has been subject of a successful lstat, or
  7588. * undefined otherwise.
  7589. *
  7590. * Does not read the filesystem, so an undefined result *could* simply
  7591. * mean that we haven't called lstat on it.
  7592. */
  7593. lstatCached() {
  7594. return this.#type & LSTAT_CALLED ? this : undefined;
  7595. }
  7596. /**
  7597. * Return the cached link target if the entry has been the subject of a
  7598. * successful readlink, or undefined otherwise.
  7599. *
  7600. * Does not read the filesystem, so an undefined result *could* just mean we
  7601. * don't have any cached data. Only use it if you are very sure that a
  7602. * readlink() has been called at some point.
  7603. */
  7604. readlinkCached() {
  7605. return this.#linkTarget;
  7606. }
  7607. /**
  7608. * Returns the cached realpath target if the entry has been the subject
  7609. * of a successful realpath, or undefined otherwise.
  7610. *
  7611. * Does not read the filesystem, so an undefined result *could* just mean we
  7612. * don't have any cached data. Only use it if you are very sure that a
  7613. * realpath() has been called at some point.
  7614. */
  7615. realpathCached() {
  7616. return this.#realpath;
  7617. }
  7618. /**
  7619. * Returns the cached child Path entries array if the entry has been the
  7620. * subject of a successful readdir(), or [] otherwise.
  7621. *
  7622. * Does not read the filesystem, so an empty array *could* just mean we
  7623. * don't have any cached data. Only use it if you are very sure that a
  7624. * readdir() has been called recently enough to still be valid.
  7625. */
  7626. readdirCached() {
  7627. const children = this.children();
  7628. return children.slice(0, children.provisional);
  7629. }
  7630. /**
  7631. * Return true if it's worth trying to readlink. Ie, we don't (yet) have
  7632. * any indication that readlink will definitely fail.
  7633. *
  7634. * Returns false if the path is known to not be a symlink, if a previous
  7635. * readlink failed, or if the entry does not exist.
  7636. */
  7637. canReadlink() {
  7638. if (this.#linkTarget)
  7639. return true;
  7640. if (!this.parent)
  7641. return false;
  7642. // cases where it cannot possibly succeed
  7643. const ifmt = this.#type & IFMT;
  7644. return !((ifmt !== UNKNOWN && ifmt !== IFLNK) ||
  7645. this.#type & ENOREADLINK ||
  7646. this.#type & ENOENT);
  7647. }
  7648. /**
  7649. * Return true if readdir has previously been successfully called on this
  7650. * path, indicating that cachedReaddir() is likely valid.
  7651. */
  7652. calledReaddir() {
  7653. return !!(this.#type & READDIR_CALLED);
  7654. }
  7655. /**
  7656. * Returns true if the path is known to not exist. That is, a previous lstat
  7657. * or readdir failed to verify its existence when that would have been
  7658. * expected, or a parent entry was marked either enoent or enotdir.
  7659. */
  7660. isENOENT() {
  7661. return !!(this.#type & ENOENT);
  7662. }
  7663. /**
  7664. * Return true if the path is a match for the given path name. This handles
  7665. * case sensitivity and unicode normalization.
  7666. *
  7667. * Note: even on case-sensitive systems, it is **not** safe to test the
  7668. * equality of the `.name` property to determine whether a given pathname
  7669. * matches, due to unicode normalization mismatches.
  7670. *
  7671. * Always use this method instead of testing the `path.name` property
  7672. * directly.
  7673. */
  7674. isNamed(n) {
  7675. return !this.nocase ?
  7676. this.#matchName === normalize(n)
  7677. : this.#matchName === normalizeNocase(n);
  7678. }
  7679. /**
  7680. * Return the Path object corresponding to the target of a symbolic link.
  7681. *
  7682. * If the Path is not a symbolic link, or if the readlink call fails for any
  7683. * reason, `undefined` is returned.
  7684. *
  7685. * Result is cached, and thus may be outdated if the filesystem is mutated.
  7686. */
  7687. async readlink() {
  7688. const target = this.#linkTarget;
  7689. if (target) {
  7690. return target;
  7691. }
  7692. if (!this.canReadlink()) {
  7693. return undefined;
  7694. }
  7695. /* c8 ignore start */
  7696. // already covered by the canReadlink test, here for ts grumples
  7697. if (!this.parent) {
  7698. return undefined;
  7699. }
  7700. /* c8 ignore stop */
  7701. try {
  7702. const read = await this.#fs.promises.readlink(this.fullpath());
  7703. const linkTarget = (await this.parent.realpath())?.resolve(read);
  7704. if (linkTarget) {
  7705. return (this.#linkTarget = linkTarget);
  7706. }
  7707. }
  7708. catch (er) {
  7709. this.#readlinkFail(er.code);
  7710. return undefined;
  7711. }
  7712. }
  7713. /**
  7714. * Synchronous {@link PathBase.readlink}
  7715. */
  7716. readlinkSync() {
  7717. const target = this.#linkTarget;
  7718. if (target) {
  7719. return target;
  7720. }
  7721. if (!this.canReadlink()) {
  7722. return undefined;
  7723. }
  7724. /* c8 ignore start */
  7725. // already covered by the canReadlink test, here for ts grumples
  7726. if (!this.parent) {
  7727. return undefined;
  7728. }
  7729. /* c8 ignore stop */
  7730. try {
  7731. const read = this.#fs.readlinkSync(this.fullpath());
  7732. const linkTarget = this.parent.realpathSync()?.resolve(read);
  7733. if (linkTarget) {
  7734. return (this.#linkTarget = linkTarget);
  7735. }
  7736. }
  7737. catch (er) {
  7738. this.#readlinkFail(er.code);
  7739. return undefined;
  7740. }
  7741. }
  7742. #readdirSuccess(children) {
  7743. // succeeded, mark readdir called bit
  7744. this.#type |= READDIR_CALLED;
  7745. // mark all remaining provisional children as ENOENT
  7746. for (let p = children.provisional; p < children.length; p++) {
  7747. const c = children[p];
  7748. if (c)
  7749. c.#markENOENT();
  7750. }
  7751. }
  7752. #markENOENT() {
  7753. // mark as UNKNOWN and ENOENT
  7754. if (this.#type & ENOENT)
  7755. return;
  7756. this.#type = (this.#type | ENOENT) & IFMT_UNKNOWN;
  7757. this.#markChildrenENOENT();
  7758. }
  7759. #markChildrenENOENT() {
  7760. // all children are provisional and do not exist
  7761. const children = this.children();
  7762. children.provisional = 0;
  7763. for (const p of children) {
  7764. p.#markENOENT();
  7765. }
  7766. }
  7767. #markENOREALPATH() {
  7768. this.#type |= ENOREALPATH;
  7769. this.#markENOTDIR();
  7770. }
  7771. // save the information when we know the entry is not a dir
  7772. #markENOTDIR() {
  7773. // entry is not a directory, so any children can't exist.
  7774. // this *should* be impossible, since any children created
  7775. // after it's been marked ENOTDIR should be marked ENOENT,
  7776. // so it won't even get to this point.
  7777. /* c8 ignore start */
  7778. if (this.#type & ENOTDIR)
  7779. return;
  7780. /* c8 ignore stop */
  7781. let t = this.#type;
  7782. // this could happen if we stat a dir, then delete it,
  7783. // then try to read it or one of its children.
  7784. if ((t & IFMT) === IFDIR)
  7785. t &= IFMT_UNKNOWN;
  7786. this.#type = t | ENOTDIR;
  7787. this.#markChildrenENOENT();
  7788. }
  7789. #readdirFail(code = '') {
  7790. // markENOTDIR and markENOENT also set provisional=0
  7791. if (code === 'ENOTDIR' || code === 'EPERM') {
  7792. this.#markENOTDIR();
  7793. }
  7794. else if (code === 'ENOENT') {
  7795. this.#markENOENT();
  7796. }
  7797. else {
  7798. this.children().provisional = 0;
  7799. }
  7800. }
  7801. #lstatFail(code = '') {
  7802. // Windows just raises ENOENT in this case, disable for win CI
  7803. /* c8 ignore start */
  7804. if (code === 'ENOTDIR') {
  7805. // already know it has a parent by this point
  7806. const p = this.parent;
  7807. p.#markENOTDIR();
  7808. }
  7809. else if (code === 'ENOENT') {
  7810. /* c8 ignore stop */
  7811. this.#markENOENT();
  7812. }
  7813. }
  7814. #readlinkFail(code = '') {
  7815. let ter = this.#type;
  7816. ter |= ENOREADLINK;
  7817. if (code === 'ENOENT')
  7818. ter |= ENOENT;
  7819. // windows gets a weird error when you try to readlink a file
  7820. if (code === 'EINVAL' || code === 'UNKNOWN') {
  7821. // exists, but not a symlink, we don't know WHAT it is, so remove
  7822. // all IFMT bits.
  7823. ter &= IFMT_UNKNOWN;
  7824. }
  7825. this.#type = ter;
  7826. // windows just gets ENOENT in this case. We do cover the case,
  7827. // just disabled because it's impossible on Windows CI
  7828. /* c8 ignore start */
  7829. if (code === 'ENOTDIR' && this.parent) {
  7830. this.parent.#markENOTDIR();
  7831. }
  7832. /* c8 ignore stop */
  7833. }
  7834. #readdirAddChild(e, c) {
  7835. return (this.#readdirMaybePromoteChild(e, c) ||
  7836. this.#readdirAddNewChild(e, c));
  7837. }
  7838. #readdirAddNewChild(e, c) {
  7839. // alloc new entry at head, so it's never provisional
  7840. const type = entToType(e);
  7841. const child = this.newChild(e.name, type, { parent: this });
  7842. const ifmt = child.#type & IFMT;
  7843. if (ifmt !== IFDIR && ifmt !== IFLNK && ifmt !== UNKNOWN) {
  7844. child.#type |= ENOTDIR;
  7845. }
  7846. c.unshift(child);
  7847. c.provisional++;
  7848. return child;
  7849. }
  7850. #readdirMaybePromoteChild(e, c) {
  7851. for (let p = c.provisional; p < c.length; p++) {
  7852. const pchild = c[p];
  7853. const name = this.nocase ? normalizeNocase(e.name) : normalize(e.name);
  7854. if (name !== pchild.#matchName) {
  7855. continue;
  7856. }
  7857. return this.#readdirPromoteChild(e, pchild, p, c);
  7858. }
  7859. }
  7860. #readdirPromoteChild(e, p, index, c) {
  7861. const v = p.name;
  7862. // retain any other flags, but set ifmt from dirent
  7863. p.#type = (p.#type & IFMT_UNKNOWN) | entToType(e);
  7864. // case sensitivity fixing when we learn the true name.
  7865. if (v !== e.name)
  7866. p.name = e.name;
  7867. // just advance provisional index (potentially off the list),
  7868. // otherwise we have to splice/pop it out and re-insert at head
  7869. if (index !== c.provisional) {
  7870. if (index === c.length - 1)
  7871. c.pop();
  7872. else
  7873. c.splice(index, 1);
  7874. c.unshift(p);
  7875. }
  7876. c.provisional++;
  7877. return p;
  7878. }
  7879. /**
  7880. * Call lstat() on this Path, and update all known information that can be
  7881. * determined.
  7882. *
  7883. * Note that unlike `fs.lstat()`, the returned value does not contain some
  7884. * information, such as `mode`, `dev`, `nlink`, and `ino`. If that
  7885. * information is required, you will need to call `fs.lstat` yourself.
  7886. *
  7887. * If the Path refers to a nonexistent file, or if the lstat call fails for
  7888. * any reason, `undefined` is returned. Otherwise the updated Path object is
  7889. * returned.
  7890. *
  7891. * Results are cached, and thus may be out of date if the filesystem is
  7892. * mutated.
  7893. */
  7894. async lstat() {
  7895. if ((this.#type & ENOENT) === 0) {
  7896. try {
  7897. this.#applyStat(await this.#fs.promises.lstat(this.fullpath()));
  7898. return this;
  7899. }
  7900. catch (er) {
  7901. this.#lstatFail(er.code);
  7902. }
  7903. }
  7904. }
  7905. /**
  7906. * synchronous {@link PathBase.lstat}
  7907. */
  7908. lstatSync() {
  7909. if ((this.#type & ENOENT) === 0) {
  7910. try {
  7911. this.#applyStat(this.#fs.lstatSync(this.fullpath()));
  7912. return this;
  7913. }
  7914. catch (er) {
  7915. this.#lstatFail(er.code);
  7916. }
  7917. }
  7918. }
  7919. #applyStat(st) {
  7920. const { atime, atimeMs, birthtime, birthtimeMs, blksize, blocks, ctime, ctimeMs, dev, gid, ino, mode, mtime, mtimeMs, nlink, rdev, size, uid, } = st;
  7921. this.#atime = atime;
  7922. this.#atimeMs = atimeMs;
  7923. this.#birthtime = birthtime;
  7924. this.#birthtimeMs = birthtimeMs;
  7925. this.#blksize = blksize;
  7926. this.#blocks = blocks;
  7927. this.#ctime = ctime;
  7928. this.#ctimeMs = ctimeMs;
  7929. this.#dev = dev;
  7930. this.#gid = gid;
  7931. this.#ino = ino;
  7932. this.#mode = mode;
  7933. this.#mtime = mtime;
  7934. this.#mtimeMs = mtimeMs;
  7935. this.#nlink = nlink;
  7936. this.#rdev = rdev;
  7937. this.#size = size;
  7938. this.#uid = uid;
  7939. const ifmt = entToType(st);
  7940. // retain any other flags, but set the ifmt
  7941. this.#type = (this.#type & IFMT_UNKNOWN) | ifmt | LSTAT_CALLED;
  7942. if (ifmt !== UNKNOWN && ifmt !== IFDIR && ifmt !== IFLNK) {
  7943. this.#type |= ENOTDIR;
  7944. }
  7945. }
  7946. #onReaddirCB = [];
  7947. #readdirCBInFlight = false;
  7948. #callOnReaddirCB(children) {
  7949. this.#readdirCBInFlight = false;
  7950. const cbs = this.#onReaddirCB.slice();
  7951. this.#onReaddirCB.length = 0;
  7952. cbs.forEach(cb => cb(null, children));
  7953. }
  7954. /**
  7955. * Standard node-style callback interface to get list of directory entries.
  7956. *
  7957. * If the Path cannot or does not contain any children, then an empty array
  7958. * is returned.
  7959. *
  7960. * Results are cached, and thus may be out of date if the filesystem is
  7961. * mutated.
  7962. *
  7963. * @param cb The callback called with (er, entries). Note that the `er`
  7964. * param is somewhat extraneous, as all readdir() errors are handled and
  7965. * simply result in an empty set of entries being returned.
  7966. * @param allowZalgo Boolean indicating that immediately known results should
  7967. * *not* be deferred with `queueMicrotask`. Defaults to `false`. Release
  7968. * zalgo at your peril, the dark pony lord is devious and unforgiving.
  7969. */
  7970. readdirCB(cb, allowZalgo = false) {
  7971. if (!this.canReaddir()) {
  7972. if (allowZalgo)
  7973. cb(null, []);
  7974. else
  7975. queueMicrotask(() => cb(null, []));
  7976. return;
  7977. }
  7978. const children = this.children();
  7979. if (this.calledReaddir()) {
  7980. const c = children.slice(0, children.provisional);
  7981. if (allowZalgo)
  7982. cb(null, c);
  7983. else
  7984. queueMicrotask(() => cb(null, c));
  7985. return;
  7986. }
  7987. // don't have to worry about zalgo at this point.
  7988. this.#onReaddirCB.push(cb);
  7989. if (this.#readdirCBInFlight) {
  7990. return;
  7991. }
  7992. this.#readdirCBInFlight = true;
  7993. // else read the directory, fill up children
  7994. // de-provisionalize any provisional children.
  7995. const fullpath = this.fullpath();
  7996. this.#fs.readdir(fullpath, { withFileTypes: true }, (er, entries) => {
  7997. if (er) {
  7998. this.#readdirFail(er.code);
  7999. children.provisional = 0;
  8000. }
  8001. else {
  8002. // if we didn't get an error, we always get entries.
  8003. //@ts-ignore
  8004. for (const e of entries) {
  8005. this.#readdirAddChild(e, children);
  8006. }
  8007. this.#readdirSuccess(children);
  8008. }
  8009. this.#callOnReaddirCB(children.slice(0, children.provisional));
  8010. return;
  8011. });
  8012. }
  8013. #asyncReaddirInFlight;
  8014. /**
  8015. * Return an array of known child entries.
  8016. *
  8017. * If the Path cannot or does not contain any children, then an empty array
  8018. * is returned.
  8019. *
  8020. * Results are cached, and thus may be out of date if the filesystem is
  8021. * mutated.
  8022. */
  8023. async readdir() {
  8024. if (!this.canReaddir()) {
  8025. return [];
  8026. }
  8027. const children = this.children();
  8028. if (this.calledReaddir()) {
  8029. return children.slice(0, children.provisional);
  8030. }
  8031. // else read the directory, fill up children
  8032. // de-provisionalize any provisional children.
  8033. const fullpath = this.fullpath();
  8034. if (this.#asyncReaddirInFlight) {
  8035. await this.#asyncReaddirInFlight;
  8036. }
  8037. else {
  8038. /* c8 ignore start */
  8039. let resolve = () => { };
  8040. /* c8 ignore stop */
  8041. this.#asyncReaddirInFlight = new Promise(res => (resolve = res));
  8042. try {
  8043. for (const e of await this.#fs.promises.readdir(fullpath, {
  8044. withFileTypes: true,
  8045. })) {
  8046. this.#readdirAddChild(e, children);
  8047. }
  8048. this.#readdirSuccess(children);
  8049. }
  8050. catch (er) {
  8051. this.#readdirFail(er.code);
  8052. children.provisional = 0;
  8053. }
  8054. this.#asyncReaddirInFlight = undefined;
  8055. resolve();
  8056. }
  8057. return children.slice(0, children.provisional);
  8058. }
  8059. /**
  8060. * synchronous {@link PathBase.readdir}
  8061. */
  8062. readdirSync() {
  8063. if (!this.canReaddir()) {
  8064. return [];
  8065. }
  8066. const children = this.children();
  8067. if (this.calledReaddir()) {
  8068. return children.slice(0, children.provisional);
  8069. }
  8070. // else read the directory, fill up children
  8071. // de-provisionalize any provisional children.
  8072. const fullpath = this.fullpath();
  8073. try {
  8074. for (const e of this.#fs.readdirSync(fullpath, {
  8075. withFileTypes: true,
  8076. })) {
  8077. this.#readdirAddChild(e, children);
  8078. }
  8079. this.#readdirSuccess(children);
  8080. }
  8081. catch (er) {
  8082. this.#readdirFail(er.code);
  8083. children.provisional = 0;
  8084. }
  8085. return children.slice(0, children.provisional);
  8086. }
  8087. canReaddir() {
  8088. if (this.#type & ENOCHILD)
  8089. return false;
  8090. const ifmt = IFMT & this.#type;
  8091. // we always set ENOTDIR when setting IFMT, so should be impossible
  8092. /* c8 ignore start */
  8093. if (!(ifmt === UNKNOWN || ifmt === IFDIR || ifmt === IFLNK)) {
  8094. return false;
  8095. }
  8096. /* c8 ignore stop */
  8097. return true;
  8098. }
  8099. shouldWalk(dirs, walkFilter) {
  8100. return ((this.#type & IFDIR) === IFDIR &&
  8101. !(this.#type & ENOCHILD) &&
  8102. !dirs.has(this) &&
  8103. (!walkFilter || walkFilter(this)));
  8104. }
  8105. /**
  8106. * Return the Path object corresponding to path as resolved
  8107. * by realpath(3).
  8108. *
  8109. * If the realpath call fails for any reason, `undefined` is returned.
  8110. *
  8111. * Result is cached, and thus may be outdated if the filesystem is mutated.
  8112. * On success, returns a Path object.
  8113. */
  8114. async realpath() {
  8115. if (this.#realpath)
  8116. return this.#realpath;
  8117. if ((ENOREALPATH | ENOREADLINK | ENOENT) & this.#type)
  8118. return undefined;
  8119. try {
  8120. const rp = await this.#fs.promises.realpath(this.fullpath());
  8121. return (this.#realpath = this.resolve(rp));
  8122. }
  8123. catch (_) {
  8124. this.#markENOREALPATH();
  8125. }
  8126. }
  8127. /**
  8128. * Synchronous {@link realpath}
  8129. */
  8130. realpathSync() {
  8131. if (this.#realpath)
  8132. return this.#realpath;
  8133. if ((ENOREALPATH | ENOREADLINK | ENOENT) & this.#type)
  8134. return undefined;
  8135. try {
  8136. const rp = this.#fs.realpathSync(this.fullpath());
  8137. return (this.#realpath = this.resolve(rp));
  8138. }
  8139. catch (_) {
  8140. this.#markENOREALPATH();
  8141. }
  8142. }
  8143. /**
  8144. * Internal method to mark this Path object as the scurry cwd,
  8145. * called by {@link PathScurry#chdir}
  8146. *
  8147. * @internal
  8148. */
  8149. [setAsCwd](oldCwd) {
  8150. if (oldCwd === this)
  8151. return;
  8152. oldCwd.isCWD = false;
  8153. this.isCWD = true;
  8154. const changed = new Set([]);
  8155. let rp = [];
  8156. let p = this;
  8157. while (p && p.parent) {
  8158. changed.add(p);
  8159. p.#relative = rp.join(this.sep);
  8160. p.#relativePosix = rp.join('/');
  8161. p = p.parent;
  8162. rp.push('..');
  8163. }
  8164. // now un-memoize parents of old cwd
  8165. p = oldCwd;
  8166. while (p && p.parent && !changed.has(p)) {
  8167. p.#relative = undefined;
  8168. p.#relativePosix = undefined;
  8169. p = p.parent;
  8170. }
  8171. }
  8172. }
  8173. /**
  8174. * Path class used on win32 systems
  8175. *
  8176. * Uses `'\\'` as the path separator for returned paths, either `'\\'` or `'/'`
  8177. * as the path separator for parsing paths.
  8178. */
  8179. class PathWin32 extends PathBase {
  8180. /**
  8181. * Separator for generating path strings.
  8182. */
  8183. sep = '\\';
  8184. /**
  8185. * Separator for parsing path strings.
  8186. */
  8187. splitSep = eitherSep;
  8188. /**
  8189. * Do not create new Path objects directly. They should always be accessed
  8190. * via the PathScurry class or other methods on the Path class.
  8191. *
  8192. * @internal
  8193. */
  8194. constructor(name, type = UNKNOWN, root, roots, nocase, children, opts) {
  8195. super(name, type, root, roots, nocase, children, opts);
  8196. }
  8197. /**
  8198. * @internal
  8199. */
  8200. newChild(name, type = UNKNOWN, opts = {}) {
  8201. return new PathWin32(name, type, this.root, this.roots, this.nocase, this.childrenCache(), opts);
  8202. }
  8203. /**
  8204. * @internal
  8205. */
  8206. getRootString(path) {
  8207. return win32$1.parse(path).root;
  8208. }
  8209. /**
  8210. * @internal
  8211. */
  8212. getRoot(rootPath) {
  8213. rootPath = uncToDrive(rootPath.toUpperCase());
  8214. if (rootPath === this.root.name) {
  8215. return this.root;
  8216. }
  8217. // ok, not that one, check if it matches another we know about
  8218. for (const [compare, root] of Object.entries(this.roots)) {
  8219. if (this.sameRoot(rootPath, compare)) {
  8220. return (this.roots[rootPath] = root);
  8221. }
  8222. }
  8223. // otherwise, have to create a new one.
  8224. return (this.roots[rootPath] = new PathScurryWin32(rootPath, this).root);
  8225. }
  8226. /**
  8227. * @internal
  8228. */
  8229. sameRoot(rootPath, compare = this.root.name) {
  8230. // windows can (rarely) have case-sensitive filesystem, but
  8231. // UNC and drive letters are always case-insensitive, and canonically
  8232. // represented uppercase.
  8233. rootPath = rootPath
  8234. .toUpperCase()
  8235. .replace(/\//g, '\\')
  8236. .replace(uncDriveRegexp, '$1\\');
  8237. return rootPath === compare;
  8238. }
  8239. }
  8240. /**
  8241. * Path class used on all posix systems.
  8242. *
  8243. * Uses `'/'` as the path separator.
  8244. */
  8245. class PathPosix extends PathBase {
  8246. /**
  8247. * separator for parsing path strings
  8248. */
  8249. splitSep = '/';
  8250. /**
  8251. * separator for generating path strings
  8252. */
  8253. sep = '/';
  8254. /**
  8255. * Do not create new Path objects directly. They should always be accessed
  8256. * via the PathScurry class or other methods on the Path class.
  8257. *
  8258. * @internal
  8259. */
  8260. constructor(name, type = UNKNOWN, root, roots, nocase, children, opts) {
  8261. super(name, type, root, roots, nocase, children, opts);
  8262. }
  8263. /**
  8264. * @internal
  8265. */
  8266. getRootString(path) {
  8267. return path.startsWith('/') ? '/' : '';
  8268. }
  8269. /**
  8270. * @internal
  8271. */
  8272. getRoot(_rootPath) {
  8273. return this.root;
  8274. }
  8275. /**
  8276. * @internal
  8277. */
  8278. newChild(name, type = UNKNOWN, opts = {}) {
  8279. return new PathPosix(name, type, this.root, this.roots, this.nocase, this.childrenCache(), opts);
  8280. }
  8281. }
  8282. /**
  8283. * The base class for all PathScurry classes, providing the interface for path
  8284. * resolution and filesystem operations.
  8285. *
  8286. * Typically, you should *not* instantiate this class directly, but rather one
  8287. * of the platform-specific classes, or the exported {@link PathScurry} which
  8288. * defaults to the current platform.
  8289. */
  8290. class PathScurryBase {
  8291. /**
  8292. * The root Path entry for the current working directory of this Scurry
  8293. */
  8294. root;
  8295. /**
  8296. * The string path for the root of this Scurry's current working directory
  8297. */
  8298. rootPath;
  8299. /**
  8300. * A collection of all roots encountered, referenced by rootPath
  8301. */
  8302. roots;
  8303. /**
  8304. * The Path entry corresponding to this PathScurry's current working directory.
  8305. */
  8306. cwd;
  8307. #resolveCache;
  8308. #resolvePosixCache;
  8309. #children;
  8310. /**
  8311. * Perform path comparisons case-insensitively.
  8312. *
  8313. * Defaults true on Darwin and Windows systems, false elsewhere.
  8314. */
  8315. nocase;
  8316. #fs;
  8317. /**
  8318. * This class should not be instantiated directly.
  8319. *
  8320. * Use PathScurryWin32, PathScurryDarwin, PathScurryPosix, or PathScurry
  8321. *
  8322. * @internal
  8323. */
  8324. constructor(cwd = process.cwd(), pathImpl, sep, { nocase, childrenCacheSize = 16 * 1024, fs = defaultFS, } = {}) {
  8325. this.#fs = fsFromOption(fs);
  8326. if (cwd instanceof URL || cwd.startsWith('file://')) {
  8327. cwd = fileURLToPath(cwd);
  8328. }
  8329. // resolve and split root, and then add to the store.
  8330. // this is the only time we call path.resolve()
  8331. const cwdPath = pathImpl.resolve(cwd);
  8332. this.roots = Object.create(null);
  8333. this.rootPath = this.parseRootPath(cwdPath);
  8334. this.#resolveCache = new ResolveCache();
  8335. this.#resolvePosixCache = new ResolveCache();
  8336. this.#children = new ChildrenCache(childrenCacheSize);
  8337. const split = cwdPath.substring(this.rootPath.length).split(sep);
  8338. // resolve('/') leaves '', splits to [''], we don't want that.
  8339. if (split.length === 1 && !split[0]) {
  8340. split.pop();
  8341. }
  8342. /* c8 ignore start */
  8343. if (nocase === undefined) {
  8344. throw new TypeError('must provide nocase setting to PathScurryBase ctor');
  8345. }
  8346. /* c8 ignore stop */
  8347. this.nocase = nocase;
  8348. this.root = this.newRoot(this.#fs);
  8349. this.roots[this.rootPath] = this.root;
  8350. let prev = this.root;
  8351. let len = split.length - 1;
  8352. const joinSep = pathImpl.sep;
  8353. let abs = this.rootPath;
  8354. let sawFirst = false;
  8355. for (const part of split) {
  8356. const l = len--;
  8357. prev = prev.child(part, {
  8358. relative: new Array(l).fill('..').join(joinSep),
  8359. relativePosix: new Array(l).fill('..').join('/'),
  8360. fullpath: (abs += (sawFirst ? '' : joinSep) + part),
  8361. });
  8362. sawFirst = true;
  8363. }
  8364. this.cwd = prev;
  8365. }
  8366. /**
  8367. * Get the depth of a provided path, string, or the cwd
  8368. */
  8369. depth(path = this.cwd) {
  8370. if (typeof path === 'string') {
  8371. path = this.cwd.resolve(path);
  8372. }
  8373. return path.depth();
  8374. }
  8375. /**
  8376. * Return the cache of child entries. Exposed so subclasses can create
  8377. * child Path objects in a platform-specific way.
  8378. *
  8379. * @internal
  8380. */
  8381. childrenCache() {
  8382. return this.#children;
  8383. }
  8384. /**
  8385. * Resolve one or more path strings to a resolved string
  8386. *
  8387. * Same interface as require('path').resolve.
  8388. *
  8389. * Much faster than path.resolve() when called multiple times for the same
  8390. * path, because the resolved Path objects are cached. Much slower
  8391. * otherwise.
  8392. */
  8393. resolve(...paths) {
  8394. // first figure out the minimum number of paths we have to test
  8395. // we always start at cwd, but any absolutes will bump the start
  8396. let r = '';
  8397. for (let i = paths.length - 1; i >= 0; i--) {
  8398. const p = paths[i];
  8399. if (!p || p === '.')
  8400. continue;
  8401. r = r ? `${p}/${r}` : p;
  8402. if (this.isAbsolute(p)) {
  8403. break;
  8404. }
  8405. }
  8406. const cached = this.#resolveCache.get(r);
  8407. if (cached !== undefined) {
  8408. return cached;
  8409. }
  8410. const result = this.cwd.resolve(r).fullpath();
  8411. this.#resolveCache.set(r, result);
  8412. return result;
  8413. }
  8414. /**
  8415. * Resolve one or more path strings to a resolved string, returning
  8416. * the posix path. Identical to .resolve() on posix systems, but on
  8417. * windows will return a forward-slash separated UNC path.
  8418. *
  8419. * Same interface as require('path').resolve.
  8420. *
  8421. * Much faster than path.resolve() when called multiple times for the same
  8422. * path, because the resolved Path objects are cached. Much slower
  8423. * otherwise.
  8424. */
  8425. resolvePosix(...paths) {
  8426. // first figure out the minimum number of paths we have to test
  8427. // we always start at cwd, but any absolutes will bump the start
  8428. let r = '';
  8429. for (let i = paths.length - 1; i >= 0; i--) {
  8430. const p = paths[i];
  8431. if (!p || p === '.')
  8432. continue;
  8433. r = r ? `${p}/${r}` : p;
  8434. if (this.isAbsolute(p)) {
  8435. break;
  8436. }
  8437. }
  8438. const cached = this.#resolvePosixCache.get(r);
  8439. if (cached !== undefined) {
  8440. return cached;
  8441. }
  8442. const result = this.cwd.resolve(r).fullpathPosix();
  8443. this.#resolvePosixCache.set(r, result);
  8444. return result;
  8445. }
  8446. /**
  8447. * find the relative path from the cwd to the supplied path string or entry
  8448. */
  8449. relative(entry = this.cwd) {
  8450. if (typeof entry === 'string') {
  8451. entry = this.cwd.resolve(entry);
  8452. }
  8453. return entry.relative();
  8454. }
  8455. /**
  8456. * find the relative path from the cwd to the supplied path string or
  8457. * entry, using / as the path delimiter, even on Windows.
  8458. */
  8459. relativePosix(entry = this.cwd) {
  8460. if (typeof entry === 'string') {
  8461. entry = this.cwd.resolve(entry);
  8462. }
  8463. return entry.relativePosix();
  8464. }
  8465. /**
  8466. * Return the basename for the provided string or Path object
  8467. */
  8468. basename(entry = this.cwd) {
  8469. if (typeof entry === 'string') {
  8470. entry = this.cwd.resolve(entry);
  8471. }
  8472. return entry.name;
  8473. }
  8474. /**
  8475. * Return the dirname for the provided string or Path object
  8476. */
  8477. dirname(entry = this.cwd) {
  8478. if (typeof entry === 'string') {
  8479. entry = this.cwd.resolve(entry);
  8480. }
  8481. return (entry.parent || entry).fullpath();
  8482. }
  8483. async readdir(entry = this.cwd, opts = {
  8484. withFileTypes: true,
  8485. }) {
  8486. if (typeof entry === 'string') {
  8487. entry = this.cwd.resolve(entry);
  8488. }
  8489. else if (!(entry instanceof PathBase)) {
  8490. opts = entry;
  8491. entry = this.cwd;
  8492. }
  8493. const { withFileTypes } = opts;
  8494. if (!entry.canReaddir()) {
  8495. return [];
  8496. }
  8497. else {
  8498. const p = await entry.readdir();
  8499. return withFileTypes ? p : p.map(e => e.name);
  8500. }
  8501. }
  8502. readdirSync(entry = this.cwd, opts = {
  8503. withFileTypes: true,
  8504. }) {
  8505. if (typeof entry === 'string') {
  8506. entry = this.cwd.resolve(entry);
  8507. }
  8508. else if (!(entry instanceof PathBase)) {
  8509. opts = entry;
  8510. entry = this.cwd;
  8511. }
  8512. const { withFileTypes = true } = opts;
  8513. if (!entry.canReaddir()) {
  8514. return [];
  8515. }
  8516. else if (withFileTypes) {
  8517. return entry.readdirSync();
  8518. }
  8519. else {
  8520. return entry.readdirSync().map(e => e.name);
  8521. }
  8522. }
  8523. /**
  8524. * Call lstat() on the string or Path object, and update all known
  8525. * information that can be determined.
  8526. *
  8527. * Note that unlike `fs.lstat()`, the returned value does not contain some
  8528. * information, such as `mode`, `dev`, `nlink`, and `ino`. If that
  8529. * information is required, you will need to call `fs.lstat` yourself.
  8530. *
  8531. * If the Path refers to a nonexistent file, or if the lstat call fails for
  8532. * any reason, `undefined` is returned. Otherwise the updated Path object is
  8533. * returned.
  8534. *
  8535. * Results are cached, and thus may be out of date if the filesystem is
  8536. * mutated.
  8537. */
  8538. async lstat(entry = this.cwd) {
  8539. if (typeof entry === 'string') {
  8540. entry = this.cwd.resolve(entry);
  8541. }
  8542. return entry.lstat();
  8543. }
  8544. /**
  8545. * synchronous {@link PathScurryBase.lstat}
  8546. */
  8547. lstatSync(entry = this.cwd) {
  8548. if (typeof entry === 'string') {
  8549. entry = this.cwd.resolve(entry);
  8550. }
  8551. return entry.lstatSync();
  8552. }
  8553. async readlink(entry = this.cwd, { withFileTypes } = {
  8554. withFileTypes: false,
  8555. }) {
  8556. if (typeof entry === 'string') {
  8557. entry = this.cwd.resolve(entry);
  8558. }
  8559. else if (!(entry instanceof PathBase)) {
  8560. withFileTypes = entry.withFileTypes;
  8561. entry = this.cwd;
  8562. }
  8563. const e = await entry.readlink();
  8564. return withFileTypes ? e : e?.fullpath();
  8565. }
  8566. readlinkSync(entry = this.cwd, { withFileTypes } = {
  8567. withFileTypes: false,
  8568. }) {
  8569. if (typeof entry === 'string') {
  8570. entry = this.cwd.resolve(entry);
  8571. }
  8572. else if (!(entry instanceof PathBase)) {
  8573. withFileTypes = entry.withFileTypes;
  8574. entry = this.cwd;
  8575. }
  8576. const e = entry.readlinkSync();
  8577. return withFileTypes ? e : e?.fullpath();
  8578. }
  8579. async realpath(entry = this.cwd, { withFileTypes } = {
  8580. withFileTypes: false,
  8581. }) {
  8582. if (typeof entry === 'string') {
  8583. entry = this.cwd.resolve(entry);
  8584. }
  8585. else if (!(entry instanceof PathBase)) {
  8586. withFileTypes = entry.withFileTypes;
  8587. entry = this.cwd;
  8588. }
  8589. const e = await entry.realpath();
  8590. return withFileTypes ? e : e?.fullpath();
  8591. }
  8592. realpathSync(entry = this.cwd, { withFileTypes } = {
  8593. withFileTypes: false,
  8594. }) {
  8595. if (typeof entry === 'string') {
  8596. entry = this.cwd.resolve(entry);
  8597. }
  8598. else if (!(entry instanceof PathBase)) {
  8599. withFileTypes = entry.withFileTypes;
  8600. entry = this.cwd;
  8601. }
  8602. const e = entry.realpathSync();
  8603. return withFileTypes ? e : e?.fullpath();
  8604. }
  8605. async walk(entry = this.cwd, opts = {}) {
  8606. if (typeof entry === 'string') {
  8607. entry = this.cwd.resolve(entry);
  8608. }
  8609. else if (!(entry instanceof PathBase)) {
  8610. opts = entry;
  8611. entry = this.cwd;
  8612. }
  8613. const { withFileTypes = true, follow = false, filter, walkFilter, } = opts;
  8614. const results = [];
  8615. if (!filter || filter(entry)) {
  8616. results.push(withFileTypes ? entry : entry.fullpath());
  8617. }
  8618. const dirs = new Set();
  8619. const walk = (dir, cb) => {
  8620. dirs.add(dir);
  8621. dir.readdirCB((er, entries) => {
  8622. /* c8 ignore start */
  8623. if (er) {
  8624. return cb(er);
  8625. }
  8626. /* c8 ignore stop */
  8627. let len = entries.length;
  8628. if (!len)
  8629. return cb();
  8630. const next = () => {
  8631. if (--len === 0) {
  8632. cb();
  8633. }
  8634. };
  8635. for (const e of entries) {
  8636. if (!filter || filter(e)) {
  8637. results.push(withFileTypes ? e : e.fullpath());
  8638. }
  8639. if (follow && e.isSymbolicLink()) {
  8640. e.realpath()
  8641. .then(r => (r?.isUnknown() ? r.lstat() : r))
  8642. .then(r => r?.shouldWalk(dirs, walkFilter) ? walk(r, next) : next());
  8643. }
  8644. else {
  8645. if (e.shouldWalk(dirs, walkFilter)) {
  8646. walk(e, next);
  8647. }
  8648. else {
  8649. next();
  8650. }
  8651. }
  8652. }
  8653. }, true); // zalgooooooo
  8654. };
  8655. const start = entry;
  8656. return new Promise((res, rej) => {
  8657. walk(start, er => {
  8658. /* c8 ignore start */
  8659. if (er)
  8660. return rej(er);
  8661. /* c8 ignore stop */
  8662. res(results);
  8663. });
  8664. });
  8665. }
  8666. walkSync(entry = this.cwd, opts = {}) {
  8667. if (typeof entry === 'string') {
  8668. entry = this.cwd.resolve(entry);
  8669. }
  8670. else if (!(entry instanceof PathBase)) {
  8671. opts = entry;
  8672. entry = this.cwd;
  8673. }
  8674. const { withFileTypes = true, follow = false, filter, walkFilter, } = opts;
  8675. const results = [];
  8676. if (!filter || filter(entry)) {
  8677. results.push(withFileTypes ? entry : entry.fullpath());
  8678. }
  8679. const dirs = new Set([entry]);
  8680. for (const dir of dirs) {
  8681. const entries = dir.readdirSync();
  8682. for (const e of entries) {
  8683. if (!filter || filter(e)) {
  8684. results.push(withFileTypes ? e : e.fullpath());
  8685. }
  8686. let r = e;
  8687. if (e.isSymbolicLink()) {
  8688. if (!(follow && (r = e.realpathSync())))
  8689. continue;
  8690. if (r.isUnknown())
  8691. r.lstatSync();
  8692. }
  8693. if (r.shouldWalk(dirs, walkFilter)) {
  8694. dirs.add(r);
  8695. }
  8696. }
  8697. }
  8698. return results;
  8699. }
  8700. /**
  8701. * Support for `for await`
  8702. *
  8703. * Alias for {@link PathScurryBase.iterate}
  8704. *
  8705. * Note: As of Node 19, this is very slow, compared to other methods of
  8706. * walking. Consider using {@link PathScurryBase.stream} if memory overhead
  8707. * and backpressure are concerns, or {@link PathScurryBase.walk} if not.
  8708. */
  8709. [Symbol.asyncIterator]() {
  8710. return this.iterate();
  8711. }
  8712. iterate(entry = this.cwd, options = {}) {
  8713. // iterating async over the stream is significantly more performant,
  8714. // especially in the warm-cache scenario, because it buffers up directory
  8715. // entries in the background instead of waiting for a yield for each one.
  8716. if (typeof entry === 'string') {
  8717. entry = this.cwd.resolve(entry);
  8718. }
  8719. else if (!(entry instanceof PathBase)) {
  8720. options = entry;
  8721. entry = this.cwd;
  8722. }
  8723. return this.stream(entry, options)[Symbol.asyncIterator]();
  8724. }
  8725. /**
  8726. * Iterating over a PathScurry performs a synchronous walk.
  8727. *
  8728. * Alias for {@link PathScurryBase.iterateSync}
  8729. */
  8730. [Symbol.iterator]() {
  8731. return this.iterateSync();
  8732. }
  8733. *iterateSync(entry = this.cwd, opts = {}) {
  8734. if (typeof entry === 'string') {
  8735. entry = this.cwd.resolve(entry);
  8736. }
  8737. else if (!(entry instanceof PathBase)) {
  8738. opts = entry;
  8739. entry = this.cwd;
  8740. }
  8741. const { withFileTypes = true, follow = false, filter, walkFilter, } = opts;
  8742. if (!filter || filter(entry)) {
  8743. yield withFileTypes ? entry : entry.fullpath();
  8744. }
  8745. const dirs = new Set([entry]);
  8746. for (const dir of dirs) {
  8747. const entries = dir.readdirSync();
  8748. for (const e of entries) {
  8749. if (!filter || filter(e)) {
  8750. yield withFileTypes ? e : e.fullpath();
  8751. }
  8752. let r = e;
  8753. if (e.isSymbolicLink()) {
  8754. if (!(follow && (r = e.realpathSync())))
  8755. continue;
  8756. if (r.isUnknown())
  8757. r.lstatSync();
  8758. }
  8759. if (r.shouldWalk(dirs, walkFilter)) {
  8760. dirs.add(r);
  8761. }
  8762. }
  8763. }
  8764. }
  8765. stream(entry = this.cwd, opts = {}) {
  8766. if (typeof entry === 'string') {
  8767. entry = this.cwd.resolve(entry);
  8768. }
  8769. else if (!(entry instanceof PathBase)) {
  8770. opts = entry;
  8771. entry = this.cwd;
  8772. }
  8773. const { withFileTypes = true, follow = false, filter, walkFilter, } = opts;
  8774. const results = new Minipass({ objectMode: true });
  8775. if (!filter || filter(entry)) {
  8776. results.write(withFileTypes ? entry : entry.fullpath());
  8777. }
  8778. const dirs = new Set();
  8779. const queue = [entry];
  8780. let processing = 0;
  8781. const process = () => {
  8782. let paused = false;
  8783. while (!paused) {
  8784. const dir = queue.shift();
  8785. if (!dir) {
  8786. if (processing === 0)
  8787. results.end();
  8788. return;
  8789. }
  8790. processing++;
  8791. dirs.add(dir);
  8792. const onReaddir = (er, entries, didRealpaths = false) => {
  8793. /* c8 ignore start */
  8794. if (er)
  8795. return results.emit('error', er);
  8796. /* c8 ignore stop */
  8797. if (follow && !didRealpaths) {
  8798. const promises = [];
  8799. for (const e of entries) {
  8800. if (e.isSymbolicLink()) {
  8801. promises.push(e
  8802. .realpath()
  8803. .then((r) => r?.isUnknown() ? r.lstat() : r));
  8804. }
  8805. }
  8806. if (promises.length) {
  8807. Promise.all(promises).then(() => onReaddir(null, entries, true));
  8808. return;
  8809. }
  8810. }
  8811. for (const e of entries) {
  8812. if (e && (!filter || filter(e))) {
  8813. if (!results.write(withFileTypes ? e : e.fullpath())) {
  8814. paused = true;
  8815. }
  8816. }
  8817. }
  8818. processing--;
  8819. for (const e of entries) {
  8820. const r = e.realpathCached() || e;
  8821. if (r.shouldWalk(dirs, walkFilter)) {
  8822. queue.push(r);
  8823. }
  8824. }
  8825. if (paused && !results.flowing) {
  8826. results.once('drain', process);
  8827. }
  8828. else if (!sync) {
  8829. process();
  8830. }
  8831. };
  8832. // zalgo containment
  8833. let sync = true;
  8834. dir.readdirCB(onReaddir, true);
  8835. sync = false;
  8836. }
  8837. };
  8838. process();
  8839. return results;
  8840. }
  8841. streamSync(entry = this.cwd, opts = {}) {
  8842. if (typeof entry === 'string') {
  8843. entry = this.cwd.resolve(entry);
  8844. }
  8845. else if (!(entry instanceof PathBase)) {
  8846. opts = entry;
  8847. entry = this.cwd;
  8848. }
  8849. const { withFileTypes = true, follow = false, filter, walkFilter, } = opts;
  8850. const results = new Minipass({ objectMode: true });
  8851. const dirs = new Set();
  8852. if (!filter || filter(entry)) {
  8853. results.write(withFileTypes ? entry : entry.fullpath());
  8854. }
  8855. const queue = [entry];
  8856. let processing = 0;
  8857. const process = () => {
  8858. let paused = false;
  8859. while (!paused) {
  8860. const dir = queue.shift();
  8861. if (!dir) {
  8862. if (processing === 0)
  8863. results.end();
  8864. return;
  8865. }
  8866. processing++;
  8867. dirs.add(dir);
  8868. const entries = dir.readdirSync();
  8869. for (const e of entries) {
  8870. if (!filter || filter(e)) {
  8871. if (!results.write(withFileTypes ? e : e.fullpath())) {
  8872. paused = true;
  8873. }
  8874. }
  8875. }
  8876. processing--;
  8877. for (const e of entries) {
  8878. let r = e;
  8879. if (e.isSymbolicLink()) {
  8880. if (!(follow && (r = e.realpathSync())))
  8881. continue;
  8882. if (r.isUnknown())
  8883. r.lstatSync();
  8884. }
  8885. if (r.shouldWalk(dirs, walkFilter)) {
  8886. queue.push(r);
  8887. }
  8888. }
  8889. }
  8890. if (paused && !results.flowing)
  8891. results.once('drain', process);
  8892. };
  8893. process();
  8894. return results;
  8895. }
  8896. chdir(path = this.cwd) {
  8897. const oldCwd = this.cwd;
  8898. this.cwd = typeof path === 'string' ? this.cwd.resolve(path) : path;
  8899. this.cwd[setAsCwd](oldCwd);
  8900. }
  8901. }
  8902. /**
  8903. * Windows implementation of {@link PathScurryBase}
  8904. *
  8905. * Defaults to case insensitve, uses `'\\'` to generate path strings. Uses
  8906. * {@link PathWin32} for Path objects.
  8907. */
  8908. class PathScurryWin32 extends PathScurryBase {
  8909. /**
  8910. * separator for generating path strings
  8911. */
  8912. sep = '\\';
  8913. constructor(cwd = process.cwd(), opts = {}) {
  8914. const { nocase = true } = opts;
  8915. super(cwd, win32$1, '\\', { ...opts, nocase });
  8916. this.nocase = nocase;
  8917. for (let p = this.cwd; p; p = p.parent) {
  8918. p.nocase = this.nocase;
  8919. }
  8920. }
  8921. /**
  8922. * @internal
  8923. */
  8924. parseRootPath(dir) {
  8925. // if the path starts with a single separator, it's not a UNC, and we'll
  8926. // just get separator as the root, and driveFromUNC will return \
  8927. // In that case, mount \ on the root from the cwd.
  8928. return win32$1.parse(dir).root.toUpperCase();
  8929. }
  8930. /**
  8931. * @internal
  8932. */
  8933. newRoot(fs) {
  8934. return new PathWin32(this.rootPath, IFDIR, undefined, this.roots, this.nocase, this.childrenCache(), { fs });
  8935. }
  8936. /**
  8937. * Return true if the provided path string is an absolute path
  8938. */
  8939. isAbsolute(p) {
  8940. return (p.startsWith('/') || p.startsWith('\\') || /^[a-z]:(\/|\\)/i.test(p));
  8941. }
  8942. }
  8943. /**
  8944. * {@link PathScurryBase} implementation for all posix systems other than Darwin.
  8945. *
  8946. * Defaults to case-sensitive matching, uses `'/'` to generate path strings.
  8947. *
  8948. * Uses {@link PathPosix} for Path objects.
  8949. */
  8950. class PathScurryPosix extends PathScurryBase {
  8951. /**
  8952. * separator for generating path strings
  8953. */
  8954. sep = '/';
  8955. constructor(cwd = process.cwd(), opts = {}) {
  8956. const { nocase = false } = opts;
  8957. super(cwd, posix$1, '/', { ...opts, nocase });
  8958. this.nocase = nocase;
  8959. }
  8960. /**
  8961. * @internal
  8962. */
  8963. parseRootPath(_dir) {
  8964. return '/';
  8965. }
  8966. /**
  8967. * @internal
  8968. */
  8969. newRoot(fs) {
  8970. return new PathPosix(this.rootPath, IFDIR, undefined, this.roots, this.nocase, this.childrenCache(), { fs });
  8971. }
  8972. /**
  8973. * Return true if the provided path string is an absolute path
  8974. */
  8975. isAbsolute(p) {
  8976. return p.startsWith('/');
  8977. }
  8978. }
  8979. /**
  8980. * {@link PathScurryBase} implementation for Darwin (macOS) systems.
  8981. *
  8982. * Defaults to case-insensitive matching, uses `'/'` for generating path
  8983. * strings.
  8984. *
  8985. * Uses {@link PathPosix} for Path objects.
  8986. */
  8987. class PathScurryDarwin extends PathScurryPosix {
  8988. constructor(cwd = process.cwd(), opts = {}) {
  8989. const { nocase = true } = opts;
  8990. super(cwd, { ...opts, nocase });
  8991. }
  8992. }
  8993. /**
  8994. * Default {@link PathBase} implementation for the current platform.
  8995. *
  8996. * {@link PathWin32} on Windows systems, {@link PathPosix} on all others.
  8997. */
  8998. process.platform === 'win32' ? PathWin32 : PathPosix;
  8999. /**
  9000. * Default {@link PathScurryBase} implementation for the current platform.
  9001. *
  9002. * {@link PathScurryWin32} on Windows systems, {@link PathScurryDarwin} on
  9003. * Darwin (macOS) systems, {@link PathScurryPosix} on all others.
  9004. */
  9005. const PathScurry = process.platform === 'win32' ? PathScurryWin32
  9006. : process.platform === 'darwin' ? PathScurryDarwin
  9007. : PathScurryPosix;
  9008. // this is just a very light wrapper around 2 arrays with an offset index
  9009. const isPatternList = (pl) => pl.length >= 1;
  9010. const isGlobList = (gl) => gl.length >= 1;
  9011. /**
  9012. * An immutable-ish view on an array of glob parts and their parsed
  9013. * results
  9014. */
  9015. class Pattern {
  9016. #patternList;
  9017. #globList;
  9018. #index;
  9019. length;
  9020. #platform;
  9021. #rest;
  9022. #globString;
  9023. #isDrive;
  9024. #isUNC;
  9025. #isAbsolute;
  9026. #followGlobstar = true;
  9027. constructor(patternList, globList, index, platform) {
  9028. if (!isPatternList(patternList)) {
  9029. throw new TypeError('empty pattern list');
  9030. }
  9031. if (!isGlobList(globList)) {
  9032. throw new TypeError('empty glob list');
  9033. }
  9034. if (globList.length !== patternList.length) {
  9035. throw new TypeError('mismatched pattern list and glob list lengths');
  9036. }
  9037. this.length = patternList.length;
  9038. if (index < 0 || index >= this.length) {
  9039. throw new TypeError('index out of range');
  9040. }
  9041. this.#patternList = patternList;
  9042. this.#globList = globList;
  9043. this.#index = index;
  9044. this.#platform = platform;
  9045. // normalize root entries of absolute patterns on initial creation.
  9046. if (this.#index === 0) {
  9047. // c: => ['c:/']
  9048. // C:/ => ['C:/']
  9049. // C:/x => ['C:/', 'x']
  9050. // //host/share => ['//host/share/']
  9051. // //host/share/ => ['//host/share/']
  9052. // //host/share/x => ['//host/share/', 'x']
  9053. // /etc => ['/', 'etc']
  9054. // / => ['/']
  9055. if (this.isUNC()) {
  9056. // '' / '' / 'host' / 'share'
  9057. const [p0, p1, p2, p3, ...prest] = this.#patternList;
  9058. const [g0, g1, g2, g3, ...grest] = this.#globList;
  9059. if (prest[0] === '') {
  9060. // ends in /
  9061. prest.shift();
  9062. grest.shift();
  9063. }
  9064. const p = [p0, p1, p2, p3, ''].join('/');
  9065. const g = [g0, g1, g2, g3, ''].join('/');
  9066. this.#patternList = [p, ...prest];
  9067. this.#globList = [g, ...grest];
  9068. this.length = this.#patternList.length;
  9069. }
  9070. else if (this.isDrive() || this.isAbsolute()) {
  9071. const [p1, ...prest] = this.#patternList;
  9072. const [g1, ...grest] = this.#globList;
  9073. if (prest[0] === '') {
  9074. // ends in /
  9075. prest.shift();
  9076. grest.shift();
  9077. }
  9078. const p = p1 + '/';
  9079. const g = g1 + '/';
  9080. this.#patternList = [p, ...prest];
  9081. this.#globList = [g, ...grest];
  9082. this.length = this.#patternList.length;
  9083. }
  9084. }
  9085. }
  9086. /**
  9087. * The first entry in the parsed list of patterns
  9088. */
  9089. pattern() {
  9090. return this.#patternList[this.#index];
  9091. }
  9092. /**
  9093. * true of if pattern() returns a string
  9094. */
  9095. isString() {
  9096. return typeof this.#patternList[this.#index] === 'string';
  9097. }
  9098. /**
  9099. * true of if pattern() returns GLOBSTAR
  9100. */
  9101. isGlobstar() {
  9102. return this.#patternList[this.#index] === GLOBSTAR$2;
  9103. }
  9104. /**
  9105. * true if pattern() returns a regexp
  9106. */
  9107. isRegExp() {
  9108. return this.#patternList[this.#index] instanceof RegExp;
  9109. }
  9110. /**
  9111. * The /-joined set of glob parts that make up this pattern
  9112. */
  9113. globString() {
  9114. return (this.#globString =
  9115. this.#globString ||
  9116. (this.#index === 0 ?
  9117. this.isAbsolute() ?
  9118. this.#globList[0] + this.#globList.slice(1).join('/')
  9119. : this.#globList.join('/')
  9120. : this.#globList.slice(this.#index).join('/')));
  9121. }
  9122. /**
  9123. * true if there are more pattern parts after this one
  9124. */
  9125. hasMore() {
  9126. return this.length > this.#index + 1;
  9127. }
  9128. /**
  9129. * The rest of the pattern after this part, or null if this is the end
  9130. */
  9131. rest() {
  9132. if (this.#rest !== undefined)
  9133. return this.#rest;
  9134. if (!this.hasMore())
  9135. return (this.#rest = null);
  9136. this.#rest = new Pattern(this.#patternList, this.#globList, this.#index + 1, this.#platform);
  9137. this.#rest.#isAbsolute = this.#isAbsolute;
  9138. this.#rest.#isUNC = this.#isUNC;
  9139. this.#rest.#isDrive = this.#isDrive;
  9140. return this.#rest;
  9141. }
  9142. /**
  9143. * true if the pattern represents a //unc/path/ on windows
  9144. */
  9145. isUNC() {
  9146. const pl = this.#patternList;
  9147. return this.#isUNC !== undefined ?
  9148. this.#isUNC
  9149. : (this.#isUNC =
  9150. this.#platform === 'win32' &&
  9151. this.#index === 0 &&
  9152. pl[0] === '' &&
  9153. pl[1] === '' &&
  9154. typeof pl[2] === 'string' &&
  9155. !!pl[2] &&
  9156. typeof pl[3] === 'string' &&
  9157. !!pl[3]);
  9158. }
  9159. // pattern like C:/...
  9160. // split = ['C:', ...]
  9161. // XXX: would be nice to handle patterns like `c:*` to test the cwd
  9162. // in c: for *, but I don't know of a way to even figure out what that
  9163. // cwd is without actually chdir'ing into it?
  9164. /**
  9165. * True if the pattern starts with a drive letter on Windows
  9166. */
  9167. isDrive() {
  9168. const pl = this.#patternList;
  9169. return this.#isDrive !== undefined ?
  9170. this.#isDrive
  9171. : (this.#isDrive =
  9172. this.#platform === 'win32' &&
  9173. this.#index === 0 &&
  9174. this.length > 1 &&
  9175. typeof pl[0] === 'string' &&
  9176. /^[a-z]:$/i.test(pl[0]));
  9177. }
  9178. // pattern = '/' or '/...' or '/x/...'
  9179. // split = ['', ''] or ['', ...] or ['', 'x', ...]
  9180. // Drive and UNC both considered absolute on windows
  9181. /**
  9182. * True if the pattern is rooted on an absolute path
  9183. */
  9184. isAbsolute() {
  9185. const pl = this.#patternList;
  9186. return this.#isAbsolute !== undefined ?
  9187. this.#isAbsolute
  9188. : (this.#isAbsolute =
  9189. (pl[0] === '' && pl.length > 1) ||
  9190. this.isDrive() ||
  9191. this.isUNC());
  9192. }
  9193. /**
  9194. * consume the root of the pattern, and return it
  9195. */
  9196. root() {
  9197. const p = this.#patternList[0];
  9198. return (typeof p === 'string' && this.isAbsolute() && this.#index === 0) ?
  9199. p
  9200. : '';
  9201. }
  9202. /**
  9203. * Check to see if the current globstar pattern is allowed to follow
  9204. * a symbolic link.
  9205. */
  9206. checkFollowGlobstar() {
  9207. return !(this.#index === 0 ||
  9208. !this.isGlobstar() ||
  9209. !this.#followGlobstar);
  9210. }
  9211. /**
  9212. * Mark that the current globstar pattern is following a symbolic link
  9213. */
  9214. markFollowGlobstar() {
  9215. if (this.#index === 0 || !this.isGlobstar() || !this.#followGlobstar)
  9216. return false;
  9217. this.#followGlobstar = false;
  9218. return true;
  9219. }
  9220. }
  9221. // give it a pattern, and it'll be able to tell you if
  9222. // a given path should be ignored.
  9223. // Ignoring a path ignores its children if the pattern ends in /**
  9224. // Ignores are always parsed in dot:true mode
  9225. const defaultPlatform$1 = (typeof process === 'object' &&
  9226. process &&
  9227. typeof process.platform === 'string') ?
  9228. process.platform
  9229. : 'linux';
  9230. /**
  9231. * Class used to process ignored patterns
  9232. */
  9233. class Ignore {
  9234. relative;
  9235. relativeChildren;
  9236. absolute;
  9237. absoluteChildren;
  9238. platform;
  9239. mmopts;
  9240. constructor(ignored, { nobrace, nocase, noext, noglobstar, platform = defaultPlatform$1, }) {
  9241. this.relative = [];
  9242. this.absolute = [];
  9243. this.relativeChildren = [];
  9244. this.absoluteChildren = [];
  9245. this.platform = platform;
  9246. this.mmopts = {
  9247. dot: true,
  9248. nobrace,
  9249. nocase,
  9250. noext,
  9251. noglobstar,
  9252. optimizationLevel: 2,
  9253. platform,
  9254. nocomment: true,
  9255. nonegate: true,
  9256. };
  9257. for (const ign of ignored)
  9258. this.add(ign);
  9259. }
  9260. add(ign) {
  9261. // this is a little weird, but it gives us a clean set of optimized
  9262. // minimatch matchers, without getting tripped up if one of them
  9263. // ends in /** inside a brace section, and it's only inefficient at
  9264. // the start of the walk, not along it.
  9265. // It'd be nice if the Pattern class just had a .test() method, but
  9266. // handling globstars is a bit of a pita, and that code already lives
  9267. // in minimatch anyway.
  9268. // Another way would be if maybe Minimatch could take its set/globParts
  9269. // as an option, and then we could at least just use Pattern to test
  9270. // for absolute-ness.
  9271. // Yet another way, Minimatch could take an array of glob strings, and
  9272. // a cwd option, and do the right thing.
  9273. const mm = new Minimatch(ign, this.mmopts);
  9274. for (let i = 0; i < mm.set.length; i++) {
  9275. const parsed = mm.set[i];
  9276. const globParts = mm.globParts[i];
  9277. /* c8 ignore start */
  9278. if (!parsed || !globParts) {
  9279. throw new Error('invalid pattern object');
  9280. }
  9281. // strip off leading ./ portions
  9282. // https://github.com/isaacs/node-glob/issues/570
  9283. while (parsed[0] === '.' && globParts[0] === '.') {
  9284. parsed.shift();
  9285. globParts.shift();
  9286. }
  9287. /* c8 ignore stop */
  9288. const p = new Pattern(parsed, globParts, 0, this.platform);
  9289. const m = new Minimatch(p.globString(), this.mmopts);
  9290. const children = globParts[globParts.length - 1] === '**';
  9291. const absolute = p.isAbsolute();
  9292. if (absolute)
  9293. this.absolute.push(m);
  9294. else
  9295. this.relative.push(m);
  9296. if (children) {
  9297. if (absolute)
  9298. this.absoluteChildren.push(m);
  9299. else
  9300. this.relativeChildren.push(m);
  9301. }
  9302. }
  9303. }
  9304. ignored(p) {
  9305. const fullpath = p.fullpath();
  9306. const fullpaths = `${fullpath}/`;
  9307. const relative = p.relative() || '.';
  9308. const relatives = `${relative}/`;
  9309. for (const m of this.relative) {
  9310. if (m.match(relative) || m.match(relatives))
  9311. return true;
  9312. }
  9313. for (const m of this.absolute) {
  9314. if (m.match(fullpath) || m.match(fullpaths))
  9315. return true;
  9316. }
  9317. return false;
  9318. }
  9319. childrenIgnored(p) {
  9320. const fullpath = p.fullpath() + '/';
  9321. const relative = (p.relative() || '.') + '/';
  9322. for (const m of this.relativeChildren) {
  9323. if (m.match(relative))
  9324. return true;
  9325. }
  9326. for (const m of this.absoluteChildren) {
  9327. if (m.match(fullpath))
  9328. return true;
  9329. }
  9330. return false;
  9331. }
  9332. }
  9333. // synchronous utility for filtering entries and calculating subwalks
  9334. /**
  9335. * A cache of which patterns have been processed for a given Path
  9336. */
  9337. class HasWalkedCache {
  9338. store;
  9339. constructor(store = new Map()) {
  9340. this.store = store;
  9341. }
  9342. copy() {
  9343. return new HasWalkedCache(new Map(this.store));
  9344. }
  9345. hasWalked(target, pattern) {
  9346. return this.store.get(target.fullpath())?.has(pattern.globString());
  9347. }
  9348. storeWalked(target, pattern) {
  9349. const fullpath = target.fullpath();
  9350. const cached = this.store.get(fullpath);
  9351. if (cached)
  9352. cached.add(pattern.globString());
  9353. else
  9354. this.store.set(fullpath, new Set([pattern.globString()]));
  9355. }
  9356. }
  9357. /**
  9358. * A record of which paths have been matched in a given walk step,
  9359. * and whether they only are considered a match if they are a directory,
  9360. * and whether their absolute or relative path should be returned.
  9361. */
  9362. class MatchRecord {
  9363. store = new Map();
  9364. add(target, absolute, ifDir) {
  9365. const n = (absolute ? 2 : 0) | (ifDir ? 1 : 0);
  9366. const current = this.store.get(target);
  9367. this.store.set(target, current === undefined ? n : n & current);
  9368. }
  9369. // match, absolute, ifdir
  9370. entries() {
  9371. return [...this.store.entries()].map(([path, n]) => [
  9372. path,
  9373. !!(n & 2),
  9374. !!(n & 1),
  9375. ]);
  9376. }
  9377. }
  9378. /**
  9379. * A collection of patterns that must be processed in a subsequent step
  9380. * for a given path.
  9381. */
  9382. class SubWalks {
  9383. store = new Map();
  9384. add(target, pattern) {
  9385. if (!target.canReaddir()) {
  9386. return;
  9387. }
  9388. const subs = this.store.get(target);
  9389. if (subs) {
  9390. if (!subs.find(p => p.globString() === pattern.globString())) {
  9391. subs.push(pattern);
  9392. }
  9393. }
  9394. else
  9395. this.store.set(target, [pattern]);
  9396. }
  9397. get(target) {
  9398. const subs = this.store.get(target);
  9399. /* c8 ignore start */
  9400. if (!subs) {
  9401. throw new Error('attempting to walk unknown path');
  9402. }
  9403. /* c8 ignore stop */
  9404. return subs;
  9405. }
  9406. entries() {
  9407. return this.keys().map(k => [k, this.store.get(k)]);
  9408. }
  9409. keys() {
  9410. return [...this.store.keys()].filter(t => t.canReaddir());
  9411. }
  9412. }
  9413. /**
  9414. * The class that processes patterns for a given path.
  9415. *
  9416. * Handles child entry filtering, and determining whether a path's
  9417. * directory contents must be read.
  9418. */
  9419. class Processor {
  9420. hasWalkedCache;
  9421. matches = new MatchRecord();
  9422. subwalks = new SubWalks();
  9423. patterns;
  9424. follow;
  9425. dot;
  9426. opts;
  9427. constructor(opts, hasWalkedCache) {
  9428. this.opts = opts;
  9429. this.follow = !!opts.follow;
  9430. this.dot = !!opts.dot;
  9431. this.hasWalkedCache =
  9432. hasWalkedCache ? hasWalkedCache.copy() : new HasWalkedCache();
  9433. }
  9434. processPatterns(target, patterns) {
  9435. this.patterns = patterns;
  9436. const processingSet = patterns.map(p => [target, p]);
  9437. // map of paths to the magic-starting subwalks they need to walk
  9438. // first item in patterns is the filter
  9439. for (let [t, pattern] of processingSet) {
  9440. this.hasWalkedCache.storeWalked(t, pattern);
  9441. const root = pattern.root();
  9442. const absolute = pattern.isAbsolute() && this.opts.absolute !== false;
  9443. // start absolute patterns at root
  9444. if (root) {
  9445. t = t.resolve(root === '/' && this.opts.root !== undefined ?
  9446. this.opts.root
  9447. : root);
  9448. const rest = pattern.rest();
  9449. if (!rest) {
  9450. this.matches.add(t, true, false);
  9451. continue;
  9452. }
  9453. else {
  9454. pattern = rest;
  9455. }
  9456. }
  9457. if (t.isENOENT())
  9458. continue;
  9459. let p;
  9460. let rest;
  9461. let changed = false;
  9462. while (typeof (p = pattern.pattern()) === 'string' &&
  9463. (rest = pattern.rest())) {
  9464. const c = t.resolve(p);
  9465. t = c;
  9466. pattern = rest;
  9467. changed = true;
  9468. }
  9469. p = pattern.pattern();
  9470. rest = pattern.rest();
  9471. if (changed) {
  9472. if (this.hasWalkedCache.hasWalked(t, pattern))
  9473. continue;
  9474. this.hasWalkedCache.storeWalked(t, pattern);
  9475. }
  9476. // now we have either a final string for a known entry,
  9477. // more strings for an unknown entry,
  9478. // or a pattern starting with magic, mounted on t.
  9479. if (typeof p === 'string') {
  9480. // must not be final entry, otherwise we would have
  9481. // concatenated it earlier.
  9482. const ifDir = p === '..' || p === '' || p === '.';
  9483. this.matches.add(t.resolve(p), absolute, ifDir);
  9484. continue;
  9485. }
  9486. else if (p === GLOBSTAR$2) {
  9487. // if no rest, match and subwalk pattern
  9488. // if rest, process rest and subwalk pattern
  9489. // if it's a symlink, but we didn't get here by way of a
  9490. // globstar match (meaning it's the first time THIS globstar
  9491. // has traversed a symlink), then we follow it. Otherwise, stop.
  9492. if (!t.isSymbolicLink() ||
  9493. this.follow ||
  9494. pattern.checkFollowGlobstar()) {
  9495. this.subwalks.add(t, pattern);
  9496. }
  9497. const rp = rest?.pattern();
  9498. const rrest = rest?.rest();
  9499. if (!rest || ((rp === '' || rp === '.') && !rrest)) {
  9500. // only HAS to be a dir if it ends in **/ or **/.
  9501. // but ending in ** will match files as well.
  9502. this.matches.add(t, absolute, rp === '' || rp === '.');
  9503. }
  9504. else {
  9505. if (rp === '..') {
  9506. // this would mean you're matching **/.. at the fs root,
  9507. // and no thanks, I'm not gonna test that specific case.
  9508. /* c8 ignore start */
  9509. const tp = t.parent || t;
  9510. /* c8 ignore stop */
  9511. if (!rrest)
  9512. this.matches.add(tp, absolute, true);
  9513. else if (!this.hasWalkedCache.hasWalked(tp, rrest)) {
  9514. this.subwalks.add(tp, rrest);
  9515. }
  9516. }
  9517. }
  9518. }
  9519. else if (p instanceof RegExp) {
  9520. this.subwalks.add(t, pattern);
  9521. }
  9522. }
  9523. return this;
  9524. }
  9525. subwalkTargets() {
  9526. return this.subwalks.keys();
  9527. }
  9528. child() {
  9529. return new Processor(this.opts, this.hasWalkedCache);
  9530. }
  9531. // return a new Processor containing the subwalks for each
  9532. // child entry, and a set of matches, and
  9533. // a hasWalkedCache that's a copy of this one
  9534. // then we're going to call
  9535. filterEntries(parent, entries) {
  9536. const patterns = this.subwalks.get(parent);
  9537. // put matches and entry walks into the results processor
  9538. const results = this.child();
  9539. for (const e of entries) {
  9540. for (const pattern of patterns) {
  9541. const absolute = pattern.isAbsolute();
  9542. const p = pattern.pattern();
  9543. const rest = pattern.rest();
  9544. if (p === GLOBSTAR$2) {
  9545. results.testGlobstar(e, pattern, rest, absolute);
  9546. }
  9547. else if (p instanceof RegExp) {
  9548. results.testRegExp(e, p, rest, absolute);
  9549. }
  9550. else {
  9551. results.testString(e, p, rest, absolute);
  9552. }
  9553. }
  9554. }
  9555. return results;
  9556. }
  9557. testGlobstar(e, pattern, rest, absolute) {
  9558. if (this.dot || !e.name.startsWith('.')) {
  9559. if (!pattern.hasMore()) {
  9560. this.matches.add(e, absolute, false);
  9561. }
  9562. if (e.canReaddir()) {
  9563. // if we're in follow mode or it's not a symlink, just keep
  9564. // testing the same pattern. If there's more after the globstar,
  9565. // then this symlink consumes the globstar. If not, then we can
  9566. // follow at most ONE symlink along the way, so we mark it, which
  9567. // also checks to ensure that it wasn't already marked.
  9568. if (this.follow || !e.isSymbolicLink()) {
  9569. this.subwalks.add(e, pattern);
  9570. }
  9571. else if (e.isSymbolicLink()) {
  9572. if (rest && pattern.checkFollowGlobstar()) {
  9573. this.subwalks.add(e, rest);
  9574. }
  9575. else if (pattern.markFollowGlobstar()) {
  9576. this.subwalks.add(e, pattern);
  9577. }
  9578. }
  9579. }
  9580. }
  9581. // if the NEXT thing matches this entry, then also add
  9582. // the rest.
  9583. if (rest) {
  9584. const rp = rest.pattern();
  9585. if (typeof rp === 'string' &&
  9586. // dots and empty were handled already
  9587. rp !== '..' &&
  9588. rp !== '' &&
  9589. rp !== '.') {
  9590. this.testString(e, rp, rest.rest(), absolute);
  9591. }
  9592. else if (rp === '..') {
  9593. /* c8 ignore start */
  9594. const ep = e.parent || e;
  9595. /* c8 ignore stop */
  9596. this.subwalks.add(ep, rest);
  9597. }
  9598. else if (rp instanceof RegExp) {
  9599. this.testRegExp(e, rp, rest.rest(), absolute);
  9600. }
  9601. }
  9602. }
  9603. testRegExp(e, p, rest, absolute) {
  9604. if (!p.test(e.name))
  9605. return;
  9606. if (!rest) {
  9607. this.matches.add(e, absolute, false);
  9608. }
  9609. else {
  9610. this.subwalks.add(e, rest);
  9611. }
  9612. }
  9613. testString(e, p, rest, absolute) {
  9614. // should never happen?
  9615. if (!e.isNamed(p))
  9616. return;
  9617. if (!rest) {
  9618. this.matches.add(e, absolute, false);
  9619. }
  9620. else {
  9621. this.subwalks.add(e, rest);
  9622. }
  9623. }
  9624. }
  9625. /**
  9626. * Single-use utility classes to provide functionality to the {@link Glob}
  9627. * methods.
  9628. *
  9629. * @module
  9630. */
  9631. const makeIgnore = (ignore, opts) => typeof ignore === 'string' ? new Ignore([ignore], opts)
  9632. : Array.isArray(ignore) ? new Ignore(ignore, opts)
  9633. : ignore;
  9634. /**
  9635. * basic walking utilities that all the glob walker types use
  9636. */
  9637. class GlobUtil {
  9638. path;
  9639. patterns;
  9640. opts;
  9641. seen = new Set();
  9642. paused = false;
  9643. aborted = false;
  9644. #onResume = [];
  9645. #ignore;
  9646. #sep;
  9647. signal;
  9648. maxDepth;
  9649. includeChildMatches;
  9650. constructor(patterns, path, opts) {
  9651. this.patterns = patterns;
  9652. this.path = path;
  9653. this.opts = opts;
  9654. this.#sep = !opts.posix && opts.platform === 'win32' ? '\\' : '/';
  9655. this.includeChildMatches = opts.includeChildMatches !== false;
  9656. if (opts.ignore || !this.includeChildMatches) {
  9657. this.#ignore = makeIgnore(opts.ignore ?? [], opts);
  9658. if (!this.includeChildMatches &&
  9659. typeof this.#ignore.add !== 'function') {
  9660. const m = 'cannot ignore child matches, ignore lacks add() method.';
  9661. throw new Error(m);
  9662. }
  9663. }
  9664. // ignore, always set with maxDepth, but it's optional on the
  9665. // GlobOptions type
  9666. /* c8 ignore start */
  9667. this.maxDepth = opts.maxDepth || Infinity;
  9668. /* c8 ignore stop */
  9669. if (opts.signal) {
  9670. this.signal = opts.signal;
  9671. this.signal.addEventListener('abort', () => {
  9672. this.#onResume.length = 0;
  9673. });
  9674. }
  9675. }
  9676. #ignored(path) {
  9677. return this.seen.has(path) || !!this.#ignore?.ignored?.(path);
  9678. }
  9679. #childrenIgnored(path) {
  9680. return !!this.#ignore?.childrenIgnored?.(path);
  9681. }
  9682. // backpressure mechanism
  9683. pause() {
  9684. this.paused = true;
  9685. }
  9686. resume() {
  9687. /* c8 ignore start */
  9688. if (this.signal?.aborted)
  9689. return;
  9690. /* c8 ignore stop */
  9691. this.paused = false;
  9692. let fn = undefined;
  9693. while (!this.paused && (fn = this.#onResume.shift())) {
  9694. fn();
  9695. }
  9696. }
  9697. onResume(fn) {
  9698. if (this.signal?.aborted)
  9699. return;
  9700. /* c8 ignore start */
  9701. if (!this.paused) {
  9702. fn();
  9703. }
  9704. else {
  9705. /* c8 ignore stop */
  9706. this.#onResume.push(fn);
  9707. }
  9708. }
  9709. // do the requisite realpath/stat checking, and return the path
  9710. // to add or undefined to filter it out.
  9711. async matchCheck(e, ifDir) {
  9712. if (ifDir && this.opts.nodir)
  9713. return undefined;
  9714. let rpc;
  9715. if (this.opts.realpath) {
  9716. rpc = e.realpathCached() || (await e.realpath());
  9717. if (!rpc)
  9718. return undefined;
  9719. e = rpc;
  9720. }
  9721. const needStat = e.isUnknown() || this.opts.stat;
  9722. const s = needStat ? await e.lstat() : e;
  9723. if (this.opts.follow && this.opts.nodir && s?.isSymbolicLink()) {
  9724. const target = await s.realpath();
  9725. /* c8 ignore start */
  9726. if (target && (target.isUnknown() || this.opts.stat)) {
  9727. await target.lstat();
  9728. }
  9729. /* c8 ignore stop */
  9730. }
  9731. return this.matchCheckTest(s, ifDir);
  9732. }
  9733. matchCheckTest(e, ifDir) {
  9734. return (e &&
  9735. (this.maxDepth === Infinity || e.depth() <= this.maxDepth) &&
  9736. (!ifDir || e.canReaddir()) &&
  9737. (!this.opts.nodir || !e.isDirectory()) &&
  9738. (!this.opts.nodir ||
  9739. !this.opts.follow ||
  9740. !e.isSymbolicLink() ||
  9741. !e.realpathCached()?.isDirectory()) &&
  9742. !this.#ignored(e)) ?
  9743. e
  9744. : undefined;
  9745. }
  9746. matchCheckSync(e, ifDir) {
  9747. if (ifDir && this.opts.nodir)
  9748. return undefined;
  9749. let rpc;
  9750. if (this.opts.realpath) {
  9751. rpc = e.realpathCached() || e.realpathSync();
  9752. if (!rpc)
  9753. return undefined;
  9754. e = rpc;
  9755. }
  9756. const needStat = e.isUnknown() || this.opts.stat;
  9757. const s = needStat ? e.lstatSync() : e;
  9758. if (this.opts.follow && this.opts.nodir && s?.isSymbolicLink()) {
  9759. const target = s.realpathSync();
  9760. if (target && (target?.isUnknown() || this.opts.stat)) {
  9761. target.lstatSync();
  9762. }
  9763. }
  9764. return this.matchCheckTest(s, ifDir);
  9765. }
  9766. matchFinish(e, absolute) {
  9767. if (this.#ignored(e))
  9768. return;
  9769. // we know we have an ignore if this is false, but TS doesn't
  9770. if (!this.includeChildMatches && this.#ignore?.add) {
  9771. const ign = `${e.relativePosix()}/**`;
  9772. this.#ignore.add(ign);
  9773. }
  9774. const abs = this.opts.absolute === undefined ? absolute : this.opts.absolute;
  9775. this.seen.add(e);
  9776. const mark = this.opts.mark && e.isDirectory() ? this.#sep : '';
  9777. // ok, we have what we need!
  9778. if (this.opts.withFileTypes) {
  9779. this.matchEmit(e);
  9780. }
  9781. else if (abs) {
  9782. const abs = this.opts.posix ? e.fullpathPosix() : e.fullpath();
  9783. this.matchEmit(abs + mark);
  9784. }
  9785. else {
  9786. const rel = this.opts.posix ? e.relativePosix() : e.relative();
  9787. const pre = this.opts.dotRelative && !rel.startsWith('..' + this.#sep) ?
  9788. '.' + this.#sep
  9789. : '';
  9790. this.matchEmit(!rel ? '.' + mark : pre + rel + mark);
  9791. }
  9792. }
  9793. async match(e, absolute, ifDir) {
  9794. const p = await this.matchCheck(e, ifDir);
  9795. if (p)
  9796. this.matchFinish(p, absolute);
  9797. }
  9798. matchSync(e, absolute, ifDir) {
  9799. const p = this.matchCheckSync(e, ifDir);
  9800. if (p)
  9801. this.matchFinish(p, absolute);
  9802. }
  9803. walkCB(target, patterns, cb) {
  9804. /* c8 ignore start */
  9805. if (this.signal?.aborted)
  9806. cb();
  9807. /* c8 ignore stop */
  9808. this.walkCB2(target, patterns, new Processor(this.opts), cb);
  9809. }
  9810. walkCB2(target, patterns, processor, cb) {
  9811. if (this.#childrenIgnored(target))
  9812. return cb();
  9813. if (this.signal?.aborted)
  9814. cb();
  9815. if (this.paused) {
  9816. this.onResume(() => this.walkCB2(target, patterns, processor, cb));
  9817. return;
  9818. }
  9819. processor.processPatterns(target, patterns);
  9820. // done processing. all of the above is sync, can be abstracted out.
  9821. // subwalks is a map of paths to the entry filters they need
  9822. // matches is a map of paths to [absolute, ifDir] tuples.
  9823. let tasks = 1;
  9824. const next = () => {
  9825. if (--tasks === 0)
  9826. cb();
  9827. };
  9828. for (const [m, absolute, ifDir] of processor.matches.entries()) {
  9829. if (this.#ignored(m))
  9830. continue;
  9831. tasks++;
  9832. this.match(m, absolute, ifDir).then(() => next());
  9833. }
  9834. for (const t of processor.subwalkTargets()) {
  9835. if (this.maxDepth !== Infinity && t.depth() >= this.maxDepth) {
  9836. continue;
  9837. }
  9838. tasks++;
  9839. const childrenCached = t.readdirCached();
  9840. if (t.calledReaddir())
  9841. this.walkCB3(t, childrenCached, processor, next);
  9842. else {
  9843. t.readdirCB((_, entries) => this.walkCB3(t, entries, processor, next), true);
  9844. }
  9845. }
  9846. next();
  9847. }
  9848. walkCB3(target, entries, processor, cb) {
  9849. processor = processor.filterEntries(target, entries);
  9850. let tasks = 1;
  9851. const next = () => {
  9852. if (--tasks === 0)
  9853. cb();
  9854. };
  9855. for (const [m, absolute, ifDir] of processor.matches.entries()) {
  9856. if (this.#ignored(m))
  9857. continue;
  9858. tasks++;
  9859. this.match(m, absolute, ifDir).then(() => next());
  9860. }
  9861. for (const [target, patterns] of processor.subwalks.entries()) {
  9862. tasks++;
  9863. this.walkCB2(target, patterns, processor.child(), next);
  9864. }
  9865. next();
  9866. }
  9867. walkCBSync(target, patterns, cb) {
  9868. /* c8 ignore start */
  9869. if (this.signal?.aborted)
  9870. cb();
  9871. /* c8 ignore stop */
  9872. this.walkCB2Sync(target, patterns, new Processor(this.opts), cb);
  9873. }
  9874. walkCB2Sync(target, patterns, processor, cb) {
  9875. if (this.#childrenIgnored(target))
  9876. return cb();
  9877. if (this.signal?.aborted)
  9878. cb();
  9879. if (this.paused) {
  9880. this.onResume(() => this.walkCB2Sync(target, patterns, processor, cb));
  9881. return;
  9882. }
  9883. processor.processPatterns(target, patterns);
  9884. // done processing. all of the above is sync, can be abstracted out.
  9885. // subwalks is a map of paths to the entry filters they need
  9886. // matches is a map of paths to [absolute, ifDir] tuples.
  9887. let tasks = 1;
  9888. const next = () => {
  9889. if (--tasks === 0)
  9890. cb();
  9891. };
  9892. for (const [m, absolute, ifDir] of processor.matches.entries()) {
  9893. if (this.#ignored(m))
  9894. continue;
  9895. this.matchSync(m, absolute, ifDir);
  9896. }
  9897. for (const t of processor.subwalkTargets()) {
  9898. if (this.maxDepth !== Infinity && t.depth() >= this.maxDepth) {
  9899. continue;
  9900. }
  9901. tasks++;
  9902. const children = t.readdirSync();
  9903. this.walkCB3Sync(t, children, processor, next);
  9904. }
  9905. next();
  9906. }
  9907. walkCB3Sync(target, entries, processor, cb) {
  9908. processor = processor.filterEntries(target, entries);
  9909. let tasks = 1;
  9910. const next = () => {
  9911. if (--tasks === 0)
  9912. cb();
  9913. };
  9914. for (const [m, absolute, ifDir] of processor.matches.entries()) {
  9915. if (this.#ignored(m))
  9916. continue;
  9917. this.matchSync(m, absolute, ifDir);
  9918. }
  9919. for (const [target, patterns] of processor.subwalks.entries()) {
  9920. tasks++;
  9921. this.walkCB2Sync(target, patterns, processor.child(), next);
  9922. }
  9923. next();
  9924. }
  9925. }
  9926. class GlobWalker extends GlobUtil {
  9927. matches = new Set();
  9928. constructor(patterns, path, opts) {
  9929. super(patterns, path, opts);
  9930. }
  9931. matchEmit(e) {
  9932. this.matches.add(e);
  9933. }
  9934. async walk() {
  9935. if (this.signal?.aborted)
  9936. throw this.signal.reason;
  9937. if (this.path.isUnknown()) {
  9938. await this.path.lstat();
  9939. }
  9940. await new Promise((res, rej) => {
  9941. this.walkCB(this.path, this.patterns, () => {
  9942. if (this.signal?.aborted) {
  9943. rej(this.signal.reason);
  9944. }
  9945. else {
  9946. res(this.matches);
  9947. }
  9948. });
  9949. });
  9950. return this.matches;
  9951. }
  9952. walkSync() {
  9953. if (this.signal?.aborted)
  9954. throw this.signal.reason;
  9955. if (this.path.isUnknown()) {
  9956. this.path.lstatSync();
  9957. }
  9958. // nothing for the callback to do, because this never pauses
  9959. this.walkCBSync(this.path, this.patterns, () => {
  9960. if (this.signal?.aborted)
  9961. throw this.signal.reason;
  9962. });
  9963. return this.matches;
  9964. }
  9965. }
  9966. class GlobStream extends GlobUtil {
  9967. results;
  9968. constructor(patterns, path, opts) {
  9969. super(patterns, path, opts);
  9970. this.results = new Minipass({
  9971. signal: this.signal,
  9972. objectMode: true,
  9973. });
  9974. this.results.on('drain', () => this.resume());
  9975. this.results.on('resume', () => this.resume());
  9976. }
  9977. matchEmit(e) {
  9978. this.results.write(e);
  9979. if (!this.results.flowing)
  9980. this.pause();
  9981. }
  9982. stream() {
  9983. const target = this.path;
  9984. if (target.isUnknown()) {
  9985. target.lstat().then(() => {
  9986. this.walkCB(target, this.patterns, () => this.results.end());
  9987. });
  9988. }
  9989. else {
  9990. this.walkCB(target, this.patterns, () => this.results.end());
  9991. }
  9992. return this.results;
  9993. }
  9994. streamSync() {
  9995. if (this.path.isUnknown()) {
  9996. this.path.lstatSync();
  9997. }
  9998. this.walkCBSync(this.path, this.patterns, () => this.results.end());
  9999. return this.results;
  10000. }
  10001. }
  10002. // if no process global, just call it linux.
  10003. // so we default to case-sensitive, / separators
  10004. const defaultPlatform = (typeof process === 'object' &&
  10005. process &&
  10006. typeof process.platform === 'string') ?
  10007. process.platform
  10008. : 'linux';
  10009. /**
  10010. * An object that can perform glob pattern traversals.
  10011. */
  10012. class Glob {
  10013. absolute;
  10014. cwd;
  10015. root;
  10016. dot;
  10017. dotRelative;
  10018. follow;
  10019. ignore;
  10020. magicalBraces;
  10021. mark;
  10022. matchBase;
  10023. maxDepth;
  10024. nobrace;
  10025. nocase;
  10026. nodir;
  10027. noext;
  10028. noglobstar;
  10029. pattern;
  10030. platform;
  10031. realpath;
  10032. scurry;
  10033. stat;
  10034. signal;
  10035. windowsPathsNoEscape;
  10036. withFileTypes;
  10037. includeChildMatches;
  10038. /**
  10039. * The options provided to the constructor.
  10040. */
  10041. opts;
  10042. /**
  10043. * An array of parsed immutable {@link Pattern} objects.
  10044. */
  10045. patterns;
  10046. /**
  10047. * All options are stored as properties on the `Glob` object.
  10048. *
  10049. * See {@link GlobOptions} for full options descriptions.
  10050. *
  10051. * Note that a previous `Glob` object can be passed as the
  10052. * `GlobOptions` to another `Glob` instantiation to re-use settings
  10053. * and caches with a new pattern.
  10054. *
  10055. * Traversal functions can be called multiple times to run the walk
  10056. * again.
  10057. */
  10058. constructor(pattern, opts) {
  10059. /* c8 ignore start */
  10060. if (!opts)
  10061. throw new TypeError('glob options required');
  10062. /* c8 ignore stop */
  10063. this.withFileTypes = !!opts.withFileTypes;
  10064. this.signal = opts.signal;
  10065. this.follow = !!opts.follow;
  10066. this.dot = !!opts.dot;
  10067. this.dotRelative = !!opts.dotRelative;
  10068. this.nodir = !!opts.nodir;
  10069. this.mark = !!opts.mark;
  10070. if (!opts.cwd) {
  10071. this.cwd = '';
  10072. }
  10073. else if (opts.cwd instanceof URL || opts.cwd.startsWith('file://')) {
  10074. opts.cwd = fileURLToPath(opts.cwd);
  10075. }
  10076. this.cwd = opts.cwd || '';
  10077. this.root = opts.root;
  10078. this.magicalBraces = !!opts.magicalBraces;
  10079. this.nobrace = !!opts.nobrace;
  10080. this.noext = !!opts.noext;
  10081. this.realpath = !!opts.realpath;
  10082. this.absolute = opts.absolute;
  10083. this.includeChildMatches = opts.includeChildMatches !== false;
  10084. this.noglobstar = !!opts.noglobstar;
  10085. this.matchBase = !!opts.matchBase;
  10086. this.maxDepth =
  10087. typeof opts.maxDepth === 'number' ? opts.maxDepth : Infinity;
  10088. this.stat = !!opts.stat;
  10089. this.ignore = opts.ignore;
  10090. if (this.withFileTypes && this.absolute !== undefined) {
  10091. throw new Error('cannot set absolute and withFileTypes:true');
  10092. }
  10093. if (typeof pattern === 'string') {
  10094. pattern = [pattern];
  10095. }
  10096. this.windowsPathsNoEscape =
  10097. !!opts.windowsPathsNoEscape ||
  10098. opts.allowWindowsEscape ===
  10099. false;
  10100. if (this.windowsPathsNoEscape) {
  10101. pattern = pattern.map(p => p.replace(/\\/g, '/'));
  10102. }
  10103. if (this.matchBase) {
  10104. if (opts.noglobstar) {
  10105. throw new TypeError('base matching requires globstar');
  10106. }
  10107. pattern = pattern.map(p => (p.includes('/') ? p : `./**/${p}`));
  10108. }
  10109. this.pattern = pattern;
  10110. this.platform = opts.platform || defaultPlatform;
  10111. this.opts = { ...opts, platform: this.platform };
  10112. if (opts.scurry) {
  10113. this.scurry = opts.scurry;
  10114. if (opts.nocase !== undefined &&
  10115. opts.nocase !== opts.scurry.nocase) {
  10116. throw new Error('nocase option contradicts provided scurry option');
  10117. }
  10118. }
  10119. else {
  10120. const Scurry = opts.platform === 'win32' ? PathScurryWin32
  10121. : opts.platform === 'darwin' ? PathScurryDarwin
  10122. : opts.platform ? PathScurryPosix
  10123. : PathScurry;
  10124. this.scurry = new Scurry(this.cwd, {
  10125. nocase: opts.nocase,
  10126. fs: opts.fs,
  10127. });
  10128. }
  10129. this.nocase = this.scurry.nocase;
  10130. // If you do nocase:true on a case-sensitive file system, then
  10131. // we need to use regexps instead of strings for non-magic
  10132. // path portions, because statting `aBc` won't return results
  10133. // for the file `AbC` for example.
  10134. const nocaseMagicOnly = this.platform === 'darwin' || this.platform === 'win32';
  10135. const mmo = {
  10136. // default nocase based on platform
  10137. ...opts,
  10138. dot: this.dot,
  10139. matchBase: this.matchBase,
  10140. nobrace: this.nobrace,
  10141. nocase: this.nocase,
  10142. nocaseMagicOnly,
  10143. nocomment: true,
  10144. noext: this.noext,
  10145. nonegate: true,
  10146. optimizationLevel: 2,
  10147. platform: this.platform,
  10148. windowsPathsNoEscape: this.windowsPathsNoEscape,
  10149. debug: !!this.opts.debug,
  10150. };
  10151. const mms = this.pattern.map(p => new Minimatch(p, mmo));
  10152. const [matchSet, globParts] = mms.reduce((set, m) => {
  10153. set[0].push(...m.set);
  10154. set[1].push(...m.globParts);
  10155. return set;
  10156. }, [[], []]);
  10157. this.patterns = matchSet.map((set, i) => {
  10158. const g = globParts[i];
  10159. /* c8 ignore start */
  10160. if (!g)
  10161. throw new Error('invalid pattern object');
  10162. /* c8 ignore stop */
  10163. return new Pattern(set, g, 0, this.platform);
  10164. });
  10165. }
  10166. async walk() {
  10167. // Walkers always return array of Path objects, so we just have to
  10168. // coerce them into the right shape. It will have already called
  10169. // realpath() if the option was set to do so, so we know that's cached.
  10170. // start out knowing the cwd, at least
  10171. return [
  10172. ...(await new GlobWalker(this.patterns, this.scurry.cwd, {
  10173. ...this.opts,
  10174. maxDepth: this.maxDepth !== Infinity ?
  10175. this.maxDepth + this.scurry.cwd.depth()
  10176. : Infinity,
  10177. platform: this.platform,
  10178. nocase: this.nocase,
  10179. includeChildMatches: this.includeChildMatches,
  10180. }).walk()),
  10181. ];
  10182. }
  10183. walkSync() {
  10184. return [
  10185. ...new GlobWalker(this.patterns, this.scurry.cwd, {
  10186. ...this.opts,
  10187. maxDepth: this.maxDepth !== Infinity ?
  10188. this.maxDepth + this.scurry.cwd.depth()
  10189. : Infinity,
  10190. platform: this.platform,
  10191. nocase: this.nocase,
  10192. includeChildMatches: this.includeChildMatches,
  10193. }).walkSync(),
  10194. ];
  10195. }
  10196. stream() {
  10197. return new GlobStream(this.patterns, this.scurry.cwd, {
  10198. ...this.opts,
  10199. maxDepth: this.maxDepth !== Infinity ?
  10200. this.maxDepth + this.scurry.cwd.depth()
  10201. : Infinity,
  10202. platform: this.platform,
  10203. nocase: this.nocase,
  10204. includeChildMatches: this.includeChildMatches,
  10205. }).stream();
  10206. }
  10207. streamSync() {
  10208. return new GlobStream(this.patterns, this.scurry.cwd, {
  10209. ...this.opts,
  10210. maxDepth: this.maxDepth !== Infinity ?
  10211. this.maxDepth + this.scurry.cwd.depth()
  10212. : Infinity,
  10213. platform: this.platform,
  10214. nocase: this.nocase,
  10215. includeChildMatches: this.includeChildMatches,
  10216. }).streamSync();
  10217. }
  10218. /**
  10219. * Default sync iteration function. Returns a Generator that
  10220. * iterates over the results.
  10221. */
  10222. iterateSync() {
  10223. return this.streamSync()[Symbol.iterator]();
  10224. }
  10225. [Symbol.iterator]() {
  10226. return this.iterateSync();
  10227. }
  10228. /**
  10229. * Default async iteration function. Returns an AsyncGenerator that
  10230. * iterates over the results.
  10231. */
  10232. iterate() {
  10233. return this.stream()[Symbol.asyncIterator]();
  10234. }
  10235. [Symbol.asyncIterator]() {
  10236. return this.iterate();
  10237. }
  10238. }
  10239. /**
  10240. * Return true if the patterns provided contain any magic glob characters,
  10241. * given the options provided.
  10242. *
  10243. * Brace expansion is not considered "magic" unless the `magicalBraces` option
  10244. * is set, as brace expansion just turns one string into an array of strings.
  10245. * So a pattern like `'x{a,b}y'` would return `false`, because `'xay'` and
  10246. * `'xby'` both do not contain any magic glob characters, and it's treated the
  10247. * same as if you had called it on `['xay', 'xby']`. When `magicalBraces:true`
  10248. * is in the options, brace expansion _is_ treated as a pattern having magic.
  10249. */
  10250. const hasMagic = (pattern, options = {}) => {
  10251. if (!Array.isArray(pattern)) {
  10252. pattern = [pattern];
  10253. }
  10254. for (const p of pattern) {
  10255. if (new Minimatch(p, options).hasMagic())
  10256. return true;
  10257. }
  10258. return false;
  10259. };
  10260. function globStreamSync(pattern, options = {}) {
  10261. return new Glob(pattern, options).streamSync();
  10262. }
  10263. function globStream(pattern, options = {}) {
  10264. return new Glob(pattern, options).stream();
  10265. }
  10266. function globSync(pattern, options = {}) {
  10267. return new Glob(pattern, options).walkSync();
  10268. }
  10269. async function glob_(pattern, options = {}) {
  10270. return new Glob(pattern, options).walk();
  10271. }
  10272. function globIterateSync(pattern, options = {}) {
  10273. return new Glob(pattern, options).iterateSync();
  10274. }
  10275. function globIterate(pattern, options = {}) {
  10276. return new Glob(pattern, options).iterate();
  10277. }
  10278. // aliases: glob.sync.stream() glob.stream.sync() glob.sync() etc
  10279. const streamSync = globStreamSync;
  10280. const stream$5 = Object.assign(globStream, { sync: globStreamSync });
  10281. const iterateSync = globIterateSync;
  10282. const iterate = Object.assign(globIterate, {
  10283. sync: globIterateSync,
  10284. });
  10285. const sync$9 = Object.assign(globSync, {
  10286. stream: globStreamSync,
  10287. iterate: globIterateSync,
  10288. });
  10289. const glob$1 = Object.assign(glob_, {
  10290. glob: glob_,
  10291. globSync,
  10292. sync: sync$9,
  10293. globStream,
  10294. stream: stream$5,
  10295. globStreamSync,
  10296. streamSync,
  10297. globIterate,
  10298. iterate,
  10299. globIterateSync,
  10300. iterateSync,
  10301. Glob,
  10302. hasMagic,
  10303. escape: escape$2,
  10304. unescape: unescape$1,
  10305. });
  10306. glob$1.glob = glob$1;
  10307. const comma = ','.charCodeAt(0);
  10308. const semicolon = ';'.charCodeAt(0);
  10309. const chars$1 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
  10310. const intToChar = new Uint8Array(64); // 64 possible chars.
  10311. const charToInt = new Uint8Array(128); // z is 122 in ASCII
  10312. for (let i = 0; i < chars$1.length; i++) {
  10313. const c = chars$1.charCodeAt(i);
  10314. intToChar[i] = c;
  10315. charToInt[c] = i;
  10316. }
  10317. function decodeInteger(reader, relative) {
  10318. let value = 0;
  10319. let shift = 0;
  10320. let integer = 0;
  10321. do {
  10322. const c = reader.next();
  10323. integer = charToInt[c];
  10324. value |= (integer & 31) << shift;
  10325. shift += 5;
  10326. } while (integer & 32);
  10327. const shouldNegate = value & 1;
  10328. value >>>= 1;
  10329. if (shouldNegate) {
  10330. value = -0x80000000 | -value;
  10331. }
  10332. return relative + value;
  10333. }
  10334. function encodeInteger(builder, num, relative) {
  10335. let delta = num - relative;
  10336. delta = delta < 0 ? (-delta << 1) | 1 : delta << 1;
  10337. do {
  10338. let clamped = delta & 0b011111;
  10339. delta >>>= 5;
  10340. if (delta > 0)
  10341. clamped |= 0b100000;
  10342. builder.write(intToChar[clamped]);
  10343. } while (delta > 0);
  10344. return num;
  10345. }
  10346. function hasMoreVlq(reader, max) {
  10347. if (reader.pos >= max)
  10348. return false;
  10349. return reader.peek() !== comma;
  10350. }
  10351. const bufLength = 1024 * 16;
  10352. // Provide a fallback for older environments.
  10353. const td = typeof TextDecoder !== 'undefined'
  10354. ? /* #__PURE__ */ new TextDecoder()
  10355. : typeof Buffer !== 'undefined'
  10356. ? {
  10357. decode(buf) {
  10358. const out = Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength);
  10359. return out.toString();
  10360. },
  10361. }
  10362. : {
  10363. decode(buf) {
  10364. let out = '';
  10365. for (let i = 0; i < buf.length; i++) {
  10366. out += String.fromCharCode(buf[i]);
  10367. }
  10368. return out;
  10369. },
  10370. };
  10371. class StringWriter {
  10372. constructor() {
  10373. this.pos = 0;
  10374. this.out = '';
  10375. this.buffer = new Uint8Array(bufLength);
  10376. }
  10377. write(v) {
  10378. const { buffer } = this;
  10379. buffer[this.pos++] = v;
  10380. if (this.pos === bufLength) {
  10381. this.out += td.decode(buffer);
  10382. this.pos = 0;
  10383. }
  10384. }
  10385. flush() {
  10386. const { buffer, out, pos } = this;
  10387. return pos > 0 ? out + td.decode(buffer.subarray(0, pos)) : out;
  10388. }
  10389. }
  10390. class StringReader {
  10391. constructor(buffer) {
  10392. this.pos = 0;
  10393. this.buffer = buffer;
  10394. }
  10395. next() {
  10396. return this.buffer.charCodeAt(this.pos++);
  10397. }
  10398. peek() {
  10399. return this.buffer.charCodeAt(this.pos);
  10400. }
  10401. indexOf(char) {
  10402. const { buffer, pos } = this;
  10403. const idx = buffer.indexOf(char, pos);
  10404. return idx === -1 ? buffer.length : idx;
  10405. }
  10406. }
  10407. function decode(mappings) {
  10408. const { length } = mappings;
  10409. const reader = new StringReader(mappings);
  10410. const decoded = [];
  10411. let genColumn = 0;
  10412. let sourcesIndex = 0;
  10413. let sourceLine = 0;
  10414. let sourceColumn = 0;
  10415. let namesIndex = 0;
  10416. do {
  10417. const semi = reader.indexOf(';');
  10418. const line = [];
  10419. let sorted = true;
  10420. let lastCol = 0;
  10421. genColumn = 0;
  10422. while (reader.pos < semi) {
  10423. let seg;
  10424. genColumn = decodeInteger(reader, genColumn);
  10425. if (genColumn < lastCol)
  10426. sorted = false;
  10427. lastCol = genColumn;
  10428. if (hasMoreVlq(reader, semi)) {
  10429. sourcesIndex = decodeInteger(reader, sourcesIndex);
  10430. sourceLine = decodeInteger(reader, sourceLine);
  10431. sourceColumn = decodeInteger(reader, sourceColumn);
  10432. if (hasMoreVlq(reader, semi)) {
  10433. namesIndex = decodeInteger(reader, namesIndex);
  10434. seg = [genColumn, sourcesIndex, sourceLine, sourceColumn, namesIndex];
  10435. }
  10436. else {
  10437. seg = [genColumn, sourcesIndex, sourceLine, sourceColumn];
  10438. }
  10439. }
  10440. else {
  10441. seg = [genColumn];
  10442. }
  10443. line.push(seg);
  10444. reader.pos++;
  10445. }
  10446. if (!sorted)
  10447. sort(line);
  10448. decoded.push(line);
  10449. reader.pos = semi + 1;
  10450. } while (reader.pos <= length);
  10451. return decoded;
  10452. }
  10453. function sort(line) {
  10454. line.sort(sortComparator$1);
  10455. }
  10456. function sortComparator$1(a, b) {
  10457. return a[0] - b[0];
  10458. }
  10459. function encode$1(decoded) {
  10460. const writer = new StringWriter();
  10461. let sourcesIndex = 0;
  10462. let sourceLine = 0;
  10463. let sourceColumn = 0;
  10464. let namesIndex = 0;
  10465. for (let i = 0; i < decoded.length; i++) {
  10466. const line = decoded[i];
  10467. if (i > 0)
  10468. writer.write(semicolon);
  10469. if (line.length === 0)
  10470. continue;
  10471. let genColumn = 0;
  10472. for (let j = 0; j < line.length; j++) {
  10473. const segment = line[j];
  10474. if (j > 0)
  10475. writer.write(comma);
  10476. genColumn = encodeInteger(writer, segment[0], genColumn);
  10477. if (segment.length === 1)
  10478. continue;
  10479. sourcesIndex = encodeInteger(writer, segment[1], sourcesIndex);
  10480. sourceLine = encodeInteger(writer, segment[2], sourceLine);
  10481. sourceColumn = encodeInteger(writer, segment[3], sourceColumn);
  10482. if (segment.length === 4)
  10483. continue;
  10484. namesIndex = encodeInteger(writer, segment[4], namesIndex);
  10485. }
  10486. }
  10487. return writer.flush();
  10488. }
  10489. class BitSet {
  10490. constructor(arg) {
  10491. this.bits = arg instanceof BitSet ? arg.bits.slice() : [];
  10492. }
  10493. add(n) {
  10494. this.bits[n >> 5] |= 1 << (n & 31);
  10495. }
  10496. has(n) {
  10497. return !!(this.bits[n >> 5] & (1 << (n & 31)));
  10498. }
  10499. }
  10500. class Chunk {
  10501. constructor(start, end, content) {
  10502. this.start = start;
  10503. this.end = end;
  10504. this.original = content;
  10505. this.intro = '';
  10506. this.outro = '';
  10507. this.content = content;
  10508. this.storeName = false;
  10509. this.edited = false;
  10510. {
  10511. this.previous = null;
  10512. this.next = null;
  10513. }
  10514. }
  10515. appendLeft(content) {
  10516. this.outro += content;
  10517. }
  10518. appendRight(content) {
  10519. this.intro = this.intro + content;
  10520. }
  10521. clone() {
  10522. const chunk = new Chunk(this.start, this.end, this.original);
  10523. chunk.intro = this.intro;
  10524. chunk.outro = this.outro;
  10525. chunk.content = this.content;
  10526. chunk.storeName = this.storeName;
  10527. chunk.edited = this.edited;
  10528. return chunk;
  10529. }
  10530. contains(index) {
  10531. return this.start < index && index < this.end;
  10532. }
  10533. eachNext(fn) {
  10534. let chunk = this;
  10535. while (chunk) {
  10536. fn(chunk);
  10537. chunk = chunk.next;
  10538. }
  10539. }
  10540. eachPrevious(fn) {
  10541. let chunk = this;
  10542. while (chunk) {
  10543. fn(chunk);
  10544. chunk = chunk.previous;
  10545. }
  10546. }
  10547. edit(content, storeName, contentOnly) {
  10548. this.content = content;
  10549. if (!contentOnly) {
  10550. this.intro = '';
  10551. this.outro = '';
  10552. }
  10553. this.storeName = storeName;
  10554. this.edited = true;
  10555. return this;
  10556. }
  10557. prependLeft(content) {
  10558. this.outro = content + this.outro;
  10559. }
  10560. prependRight(content) {
  10561. this.intro = content + this.intro;
  10562. }
  10563. reset() {
  10564. this.intro = '';
  10565. this.outro = '';
  10566. if (this.edited) {
  10567. this.content = this.original;
  10568. this.storeName = false;
  10569. this.edited = false;
  10570. }
  10571. }
  10572. split(index) {
  10573. const sliceIndex = index - this.start;
  10574. const originalBefore = this.original.slice(0, sliceIndex);
  10575. const originalAfter = this.original.slice(sliceIndex);
  10576. this.original = originalBefore;
  10577. const newChunk = new Chunk(index, this.end, originalAfter);
  10578. newChunk.outro = this.outro;
  10579. this.outro = '';
  10580. this.end = index;
  10581. if (this.edited) {
  10582. // after split we should save the edit content record into the correct chunk
  10583. // to make sure sourcemap correct
  10584. // For example:
  10585. // ' test'.trim()
  10586. // split -> ' ' + 'test'
  10587. // ✔️ edit -> '' + 'test'
  10588. // ✖️ edit -> 'test' + ''
  10589. // TODO is this block necessary?...
  10590. newChunk.edit('', false);
  10591. this.content = '';
  10592. } else {
  10593. this.content = originalBefore;
  10594. }
  10595. newChunk.next = this.next;
  10596. if (newChunk.next) newChunk.next.previous = newChunk;
  10597. newChunk.previous = this;
  10598. this.next = newChunk;
  10599. return newChunk;
  10600. }
  10601. toString() {
  10602. return this.intro + this.content + this.outro;
  10603. }
  10604. trimEnd(rx) {
  10605. this.outro = this.outro.replace(rx, '');
  10606. if (this.outro.length) return true;
  10607. const trimmed = this.content.replace(rx, '');
  10608. if (trimmed.length) {
  10609. if (trimmed !== this.content) {
  10610. this.split(this.start + trimmed.length).edit('', undefined, true);
  10611. if (this.edited) {
  10612. // save the change, if it has been edited
  10613. this.edit(trimmed, this.storeName, true);
  10614. }
  10615. }
  10616. return true;
  10617. } else {
  10618. this.edit('', undefined, true);
  10619. this.intro = this.intro.replace(rx, '');
  10620. if (this.intro.length) return true;
  10621. }
  10622. }
  10623. trimStart(rx) {
  10624. this.intro = this.intro.replace(rx, '');
  10625. if (this.intro.length) return true;
  10626. const trimmed = this.content.replace(rx, '');
  10627. if (trimmed.length) {
  10628. if (trimmed !== this.content) {
  10629. const newChunk = this.split(this.end - trimmed.length);
  10630. if (this.edited) {
  10631. // save the change, if it has been edited
  10632. newChunk.edit(trimmed, this.storeName, true);
  10633. }
  10634. this.edit('', undefined, true);
  10635. }
  10636. return true;
  10637. } else {
  10638. this.edit('', undefined, true);
  10639. this.outro = this.outro.replace(rx, '');
  10640. if (this.outro.length) return true;
  10641. }
  10642. }
  10643. }
  10644. function getBtoa() {
  10645. if (typeof globalThis !== 'undefined' && typeof globalThis.btoa === 'function') {
  10646. return (str) => globalThis.btoa(unescape(encodeURIComponent(str)));
  10647. } else if (typeof Buffer === 'function') {
  10648. return (str) => Buffer.from(str, 'utf-8').toString('base64');
  10649. } else {
  10650. return () => {
  10651. throw new Error('Unsupported environment: `window.btoa` or `Buffer` should be supported.');
  10652. };
  10653. }
  10654. }
  10655. const btoa$1 = /*#__PURE__*/ getBtoa();
  10656. let SourceMap$1 = class SourceMap {
  10657. constructor(properties) {
  10658. this.version = 3;
  10659. this.file = properties.file;
  10660. this.sources = properties.sources;
  10661. this.sourcesContent = properties.sourcesContent;
  10662. this.names = properties.names;
  10663. this.mappings = encode$1(properties.mappings);
  10664. if (typeof properties.x_google_ignoreList !== 'undefined') {
  10665. this.x_google_ignoreList = properties.x_google_ignoreList;
  10666. }
  10667. }
  10668. toString() {
  10669. return JSON.stringify(this);
  10670. }
  10671. toUrl() {
  10672. return 'data:application/json;charset=utf-8;base64,' + btoa$1(this.toString());
  10673. }
  10674. };
  10675. function guessIndent(code) {
  10676. const lines = code.split('\n');
  10677. const tabbed = lines.filter((line) => /^\t+/.test(line));
  10678. const spaced = lines.filter((line) => /^ {2,}/.test(line));
  10679. if (tabbed.length === 0 && spaced.length === 0) {
  10680. return null;
  10681. }
  10682. // More lines tabbed than spaced? Assume tabs, and
  10683. // default to tabs in the case of a tie (or nothing
  10684. // to go on)
  10685. if (tabbed.length >= spaced.length) {
  10686. return '\t';
  10687. }
  10688. // Otherwise, we need to guess the multiple
  10689. const min = spaced.reduce((previous, current) => {
  10690. const numSpaces = /^ +/.exec(current)[0].length;
  10691. return Math.min(numSpaces, previous);
  10692. }, Infinity);
  10693. return new Array(min + 1).join(' ');
  10694. }
  10695. function getRelativePath(from, to) {
  10696. const fromParts = from.split(/[/\\]/);
  10697. const toParts = to.split(/[/\\]/);
  10698. fromParts.pop(); // get dirname
  10699. while (fromParts[0] === toParts[0]) {
  10700. fromParts.shift();
  10701. toParts.shift();
  10702. }
  10703. if (fromParts.length) {
  10704. let i = fromParts.length;
  10705. while (i--) fromParts[i] = '..';
  10706. }
  10707. return fromParts.concat(toParts).join('/');
  10708. }
  10709. const toString$1 = Object.prototype.toString;
  10710. function isObject$2(thing) {
  10711. return toString$1.call(thing) === '[object Object]';
  10712. }
  10713. function getLocator(source) {
  10714. const originalLines = source.split('\n');
  10715. const lineOffsets = [];
  10716. for (let i = 0, pos = 0; i < originalLines.length; i++) {
  10717. lineOffsets.push(pos);
  10718. pos += originalLines[i].length + 1;
  10719. }
  10720. return function locate(index) {
  10721. let i = 0;
  10722. let j = lineOffsets.length;
  10723. while (i < j) {
  10724. const m = (i + j) >> 1;
  10725. if (index < lineOffsets[m]) {
  10726. j = m;
  10727. } else {
  10728. i = m + 1;
  10729. }
  10730. }
  10731. const line = i - 1;
  10732. const column = index - lineOffsets[line];
  10733. return { line, column };
  10734. };
  10735. }
  10736. const wordRegex = /\w/;
  10737. class Mappings {
  10738. constructor(hires) {
  10739. this.hires = hires;
  10740. this.generatedCodeLine = 0;
  10741. this.generatedCodeColumn = 0;
  10742. this.raw = [];
  10743. this.rawSegments = this.raw[this.generatedCodeLine] = [];
  10744. this.pending = null;
  10745. }
  10746. addEdit(sourceIndex, content, loc, nameIndex) {
  10747. if (content.length) {
  10748. const contentLengthMinusOne = content.length - 1;
  10749. let contentLineEnd = content.indexOf('\n', 0);
  10750. let previousContentLineEnd = -1;
  10751. // Loop through each line in the content and add a segment, but stop if the last line is empty,
  10752. // else code afterwards would fill one line too many
  10753. while (contentLineEnd >= 0 && contentLengthMinusOne > contentLineEnd) {
  10754. const segment = [this.generatedCodeColumn, sourceIndex, loc.line, loc.column];
  10755. if (nameIndex >= 0) {
  10756. segment.push(nameIndex);
  10757. }
  10758. this.rawSegments.push(segment);
  10759. this.generatedCodeLine += 1;
  10760. this.raw[this.generatedCodeLine] = this.rawSegments = [];
  10761. this.generatedCodeColumn = 0;
  10762. previousContentLineEnd = contentLineEnd;
  10763. contentLineEnd = content.indexOf('\n', contentLineEnd + 1);
  10764. }
  10765. const segment = [this.generatedCodeColumn, sourceIndex, loc.line, loc.column];
  10766. if (nameIndex >= 0) {
  10767. segment.push(nameIndex);
  10768. }
  10769. this.rawSegments.push(segment);
  10770. this.advance(content.slice(previousContentLineEnd + 1));
  10771. } else if (this.pending) {
  10772. this.rawSegments.push(this.pending);
  10773. this.advance(content);
  10774. }
  10775. this.pending = null;
  10776. }
  10777. addUneditedChunk(sourceIndex, chunk, original, loc, sourcemapLocations) {
  10778. let originalCharIndex = chunk.start;
  10779. let first = true;
  10780. // when iterating each char, check if it's in a word boundary
  10781. let charInHiresBoundary = false;
  10782. while (originalCharIndex < chunk.end) {
  10783. if (this.hires || first || sourcemapLocations.has(originalCharIndex)) {
  10784. const segment = [this.generatedCodeColumn, sourceIndex, loc.line, loc.column];
  10785. if (this.hires === 'boundary') {
  10786. // in hires "boundary", group segments per word boundary than per char
  10787. if (wordRegex.test(original[originalCharIndex])) {
  10788. // for first char in the boundary found, start the boundary by pushing a segment
  10789. if (!charInHiresBoundary) {
  10790. this.rawSegments.push(segment);
  10791. charInHiresBoundary = true;
  10792. }
  10793. } else {
  10794. // for non-word char, end the boundary by pushing a segment
  10795. this.rawSegments.push(segment);
  10796. charInHiresBoundary = false;
  10797. }
  10798. } else {
  10799. this.rawSegments.push(segment);
  10800. }
  10801. }
  10802. if (original[originalCharIndex] === '\n') {
  10803. loc.line += 1;
  10804. loc.column = 0;
  10805. this.generatedCodeLine += 1;
  10806. this.raw[this.generatedCodeLine] = this.rawSegments = [];
  10807. this.generatedCodeColumn = 0;
  10808. first = true;
  10809. } else {
  10810. loc.column += 1;
  10811. this.generatedCodeColumn += 1;
  10812. first = false;
  10813. }
  10814. originalCharIndex += 1;
  10815. }
  10816. this.pending = null;
  10817. }
  10818. advance(str) {
  10819. if (!str) return;
  10820. const lines = str.split('\n');
  10821. if (lines.length > 1) {
  10822. for (let i = 0; i < lines.length - 1; i++) {
  10823. this.generatedCodeLine++;
  10824. this.raw[this.generatedCodeLine] = this.rawSegments = [];
  10825. }
  10826. this.generatedCodeColumn = 0;
  10827. }
  10828. this.generatedCodeColumn += lines[lines.length - 1].length;
  10829. }
  10830. }
  10831. const n$1 = '\n';
  10832. const warned = {
  10833. insertLeft: false,
  10834. insertRight: false,
  10835. storeName: false,
  10836. };
  10837. class MagicString {
  10838. constructor(string, options = {}) {
  10839. const chunk = new Chunk(0, string.length, string);
  10840. Object.defineProperties(this, {
  10841. original: { writable: true, value: string },
  10842. outro: { writable: true, value: '' },
  10843. intro: { writable: true, value: '' },
  10844. firstChunk: { writable: true, value: chunk },
  10845. lastChunk: { writable: true, value: chunk },
  10846. lastSearchedChunk: { writable: true, value: chunk },
  10847. byStart: { writable: true, value: {} },
  10848. byEnd: { writable: true, value: {} },
  10849. filename: { writable: true, value: options.filename },
  10850. indentExclusionRanges: { writable: true, value: options.indentExclusionRanges },
  10851. sourcemapLocations: { writable: true, value: new BitSet() },
  10852. storedNames: { writable: true, value: {} },
  10853. indentStr: { writable: true, value: undefined },
  10854. ignoreList: { writable: true, value: options.ignoreList },
  10855. });
  10856. this.byStart[0] = chunk;
  10857. this.byEnd[string.length] = chunk;
  10858. }
  10859. addSourcemapLocation(char) {
  10860. this.sourcemapLocations.add(char);
  10861. }
  10862. append(content) {
  10863. if (typeof content !== 'string') throw new TypeError('outro content must be a string');
  10864. this.outro += content;
  10865. return this;
  10866. }
  10867. appendLeft(index, content) {
  10868. if (typeof content !== 'string') throw new TypeError('inserted content must be a string');
  10869. this._split(index);
  10870. const chunk = this.byEnd[index];
  10871. if (chunk) {
  10872. chunk.appendLeft(content);
  10873. } else {
  10874. this.intro += content;
  10875. }
  10876. return this;
  10877. }
  10878. appendRight(index, content) {
  10879. if (typeof content !== 'string') throw new TypeError('inserted content must be a string');
  10880. this._split(index);
  10881. const chunk = this.byStart[index];
  10882. if (chunk) {
  10883. chunk.appendRight(content);
  10884. } else {
  10885. this.outro += content;
  10886. }
  10887. return this;
  10888. }
  10889. clone() {
  10890. const cloned = new MagicString(this.original, { filename: this.filename });
  10891. let originalChunk = this.firstChunk;
  10892. let clonedChunk = (cloned.firstChunk = cloned.lastSearchedChunk = originalChunk.clone());
  10893. while (originalChunk) {
  10894. cloned.byStart[clonedChunk.start] = clonedChunk;
  10895. cloned.byEnd[clonedChunk.end] = clonedChunk;
  10896. const nextOriginalChunk = originalChunk.next;
  10897. const nextClonedChunk = nextOriginalChunk && nextOriginalChunk.clone();
  10898. if (nextClonedChunk) {
  10899. clonedChunk.next = nextClonedChunk;
  10900. nextClonedChunk.previous = clonedChunk;
  10901. clonedChunk = nextClonedChunk;
  10902. }
  10903. originalChunk = nextOriginalChunk;
  10904. }
  10905. cloned.lastChunk = clonedChunk;
  10906. if (this.indentExclusionRanges) {
  10907. cloned.indentExclusionRanges = this.indentExclusionRanges.slice();
  10908. }
  10909. cloned.sourcemapLocations = new BitSet(this.sourcemapLocations);
  10910. cloned.intro = this.intro;
  10911. cloned.outro = this.outro;
  10912. return cloned;
  10913. }
  10914. generateDecodedMap(options) {
  10915. options = options || {};
  10916. const sourceIndex = 0;
  10917. const names = Object.keys(this.storedNames);
  10918. const mappings = new Mappings(options.hires);
  10919. const locate = getLocator(this.original);
  10920. if (this.intro) {
  10921. mappings.advance(this.intro);
  10922. }
  10923. this.firstChunk.eachNext((chunk) => {
  10924. const loc = locate(chunk.start);
  10925. if (chunk.intro.length) mappings.advance(chunk.intro);
  10926. if (chunk.edited) {
  10927. mappings.addEdit(
  10928. sourceIndex,
  10929. chunk.content,
  10930. loc,
  10931. chunk.storeName ? names.indexOf(chunk.original) : -1,
  10932. );
  10933. } else {
  10934. mappings.addUneditedChunk(sourceIndex, chunk, this.original, loc, this.sourcemapLocations);
  10935. }
  10936. if (chunk.outro.length) mappings.advance(chunk.outro);
  10937. });
  10938. return {
  10939. file: options.file ? options.file.split(/[/\\]/).pop() : undefined,
  10940. sources: [
  10941. options.source ? getRelativePath(options.file || '', options.source) : options.file || '',
  10942. ],
  10943. sourcesContent: options.includeContent ? [this.original] : undefined,
  10944. names,
  10945. mappings: mappings.raw,
  10946. x_google_ignoreList: this.ignoreList ? [sourceIndex] : undefined,
  10947. };
  10948. }
  10949. generateMap(options) {
  10950. return new SourceMap$1(this.generateDecodedMap(options));
  10951. }
  10952. _ensureindentStr() {
  10953. if (this.indentStr === undefined) {
  10954. this.indentStr = guessIndent(this.original);
  10955. }
  10956. }
  10957. _getRawIndentString() {
  10958. this._ensureindentStr();
  10959. return this.indentStr;
  10960. }
  10961. getIndentString() {
  10962. this._ensureindentStr();
  10963. return this.indentStr === null ? '\t' : this.indentStr;
  10964. }
  10965. indent(indentStr, options) {
  10966. const pattern = /^[^\r\n]/gm;
  10967. if (isObject$2(indentStr)) {
  10968. options = indentStr;
  10969. indentStr = undefined;
  10970. }
  10971. if (indentStr === undefined) {
  10972. this._ensureindentStr();
  10973. indentStr = this.indentStr || '\t';
  10974. }
  10975. if (indentStr === '') return this; // noop
  10976. options = options || {};
  10977. // Process exclusion ranges
  10978. const isExcluded = {};
  10979. if (options.exclude) {
  10980. const exclusions =
  10981. typeof options.exclude[0] === 'number' ? [options.exclude] : options.exclude;
  10982. exclusions.forEach((exclusion) => {
  10983. for (let i = exclusion[0]; i < exclusion[1]; i += 1) {
  10984. isExcluded[i] = true;
  10985. }
  10986. });
  10987. }
  10988. let shouldIndentNextCharacter = options.indentStart !== false;
  10989. const replacer = (match) => {
  10990. if (shouldIndentNextCharacter) return `${indentStr}${match}`;
  10991. shouldIndentNextCharacter = true;
  10992. return match;
  10993. };
  10994. this.intro = this.intro.replace(pattern, replacer);
  10995. let charIndex = 0;
  10996. let chunk = this.firstChunk;
  10997. while (chunk) {
  10998. const end = chunk.end;
  10999. if (chunk.edited) {
  11000. if (!isExcluded[charIndex]) {
  11001. chunk.content = chunk.content.replace(pattern, replacer);
  11002. if (chunk.content.length) {
  11003. shouldIndentNextCharacter = chunk.content[chunk.content.length - 1] === '\n';
  11004. }
  11005. }
  11006. } else {
  11007. charIndex = chunk.start;
  11008. while (charIndex < end) {
  11009. if (!isExcluded[charIndex]) {
  11010. const char = this.original[charIndex];
  11011. if (char === '\n') {
  11012. shouldIndentNextCharacter = true;
  11013. } else if (char !== '\r' && shouldIndentNextCharacter) {
  11014. shouldIndentNextCharacter = false;
  11015. if (charIndex === chunk.start) {
  11016. chunk.prependRight(indentStr);
  11017. } else {
  11018. this._splitChunk(chunk, charIndex);
  11019. chunk = chunk.next;
  11020. chunk.prependRight(indentStr);
  11021. }
  11022. }
  11023. }
  11024. charIndex += 1;
  11025. }
  11026. }
  11027. charIndex = chunk.end;
  11028. chunk = chunk.next;
  11029. }
  11030. this.outro = this.outro.replace(pattern, replacer);
  11031. return this;
  11032. }
  11033. insert() {
  11034. throw new Error(
  11035. 'magicString.insert(...) is deprecated. Use prependRight(...) or appendLeft(...)',
  11036. );
  11037. }
  11038. insertLeft(index, content) {
  11039. if (!warned.insertLeft) {
  11040. console.warn(
  11041. 'magicString.insertLeft(...) is deprecated. Use magicString.appendLeft(...) instead',
  11042. ); // eslint-disable-line no-console
  11043. warned.insertLeft = true;
  11044. }
  11045. return this.appendLeft(index, content);
  11046. }
  11047. insertRight(index, content) {
  11048. if (!warned.insertRight) {
  11049. console.warn(
  11050. 'magicString.insertRight(...) is deprecated. Use magicString.prependRight(...) instead',
  11051. ); // eslint-disable-line no-console
  11052. warned.insertRight = true;
  11053. }
  11054. return this.prependRight(index, content);
  11055. }
  11056. move(start, end, index) {
  11057. if (index >= start && index <= end) throw new Error('Cannot move a selection inside itself');
  11058. this._split(start);
  11059. this._split(end);
  11060. this._split(index);
  11061. const first = this.byStart[start];
  11062. const last = this.byEnd[end];
  11063. const oldLeft = first.previous;
  11064. const oldRight = last.next;
  11065. const newRight = this.byStart[index];
  11066. if (!newRight && last === this.lastChunk) return this;
  11067. const newLeft = newRight ? newRight.previous : this.lastChunk;
  11068. if (oldLeft) oldLeft.next = oldRight;
  11069. if (oldRight) oldRight.previous = oldLeft;
  11070. if (newLeft) newLeft.next = first;
  11071. if (newRight) newRight.previous = last;
  11072. if (!first.previous) this.firstChunk = last.next;
  11073. if (!last.next) {
  11074. this.lastChunk = first.previous;
  11075. this.lastChunk.next = null;
  11076. }
  11077. first.previous = newLeft;
  11078. last.next = newRight || null;
  11079. if (!newLeft) this.firstChunk = first;
  11080. if (!newRight) this.lastChunk = last;
  11081. return this;
  11082. }
  11083. overwrite(start, end, content, options) {
  11084. options = options || {};
  11085. return this.update(start, end, content, { ...options, overwrite: !options.contentOnly });
  11086. }
  11087. update(start, end, content, options) {
  11088. if (typeof content !== 'string') throw new TypeError('replacement content must be a string');
  11089. if (this.original.length !== 0) {
  11090. while (start < 0) start += this.original.length;
  11091. while (end < 0) end += this.original.length;
  11092. }
  11093. if (end > this.original.length) throw new Error('end is out of bounds');
  11094. if (start === end)
  11095. throw new Error(
  11096. 'Cannot overwrite a zero-length range – use appendLeft or prependRight instead',
  11097. );
  11098. this._split(start);
  11099. this._split(end);
  11100. if (options === true) {
  11101. if (!warned.storeName) {
  11102. console.warn(
  11103. 'The final argument to magicString.overwrite(...) should be an options object. See https://github.com/rich-harris/magic-string',
  11104. ); // eslint-disable-line no-console
  11105. warned.storeName = true;
  11106. }
  11107. options = { storeName: true };
  11108. }
  11109. const storeName = options !== undefined ? options.storeName : false;
  11110. const overwrite = options !== undefined ? options.overwrite : false;
  11111. if (storeName) {
  11112. const original = this.original.slice(start, end);
  11113. Object.defineProperty(this.storedNames, original, {
  11114. writable: true,
  11115. value: true,
  11116. enumerable: true,
  11117. });
  11118. }
  11119. const first = this.byStart[start];
  11120. const last = this.byEnd[end];
  11121. if (first) {
  11122. let chunk = first;
  11123. while (chunk !== last) {
  11124. if (chunk.next !== this.byStart[chunk.end]) {
  11125. throw new Error('Cannot overwrite across a split point');
  11126. }
  11127. chunk = chunk.next;
  11128. chunk.edit('', false);
  11129. }
  11130. first.edit(content, storeName, !overwrite);
  11131. } else {
  11132. // must be inserting at the end
  11133. const newChunk = new Chunk(start, end, '').edit(content, storeName);
  11134. // TODO last chunk in the array may not be the last chunk, if it's moved...
  11135. last.next = newChunk;
  11136. newChunk.previous = last;
  11137. }
  11138. return this;
  11139. }
  11140. prepend(content) {
  11141. if (typeof content !== 'string') throw new TypeError('outro content must be a string');
  11142. this.intro = content + this.intro;
  11143. return this;
  11144. }
  11145. prependLeft(index, content) {
  11146. if (typeof content !== 'string') throw new TypeError('inserted content must be a string');
  11147. this._split(index);
  11148. const chunk = this.byEnd[index];
  11149. if (chunk) {
  11150. chunk.prependLeft(content);
  11151. } else {
  11152. this.intro = content + this.intro;
  11153. }
  11154. return this;
  11155. }
  11156. prependRight(index, content) {
  11157. if (typeof content !== 'string') throw new TypeError('inserted content must be a string');
  11158. this._split(index);
  11159. const chunk = this.byStart[index];
  11160. if (chunk) {
  11161. chunk.prependRight(content);
  11162. } else {
  11163. this.outro = content + this.outro;
  11164. }
  11165. return this;
  11166. }
  11167. remove(start, end) {
  11168. if (this.original.length !== 0) {
  11169. while (start < 0) start += this.original.length;
  11170. while (end < 0) end += this.original.length;
  11171. }
  11172. if (start === end) return this;
  11173. if (start < 0 || end > this.original.length) throw new Error('Character is out of bounds');
  11174. if (start > end) throw new Error('end must be greater than start');
  11175. this._split(start);
  11176. this._split(end);
  11177. let chunk = this.byStart[start];
  11178. while (chunk) {
  11179. chunk.intro = '';
  11180. chunk.outro = '';
  11181. chunk.edit('');
  11182. chunk = end > chunk.end ? this.byStart[chunk.end] : null;
  11183. }
  11184. return this;
  11185. }
  11186. reset(start, end) {
  11187. if (this.original.length !== 0) {
  11188. while (start < 0) start += this.original.length;
  11189. while (end < 0) end += this.original.length;
  11190. }
  11191. if (start === end) return this;
  11192. if (start < 0 || end > this.original.length) throw new Error('Character is out of bounds');
  11193. if (start > end) throw new Error('end must be greater than start');
  11194. this._split(start);
  11195. this._split(end);
  11196. let chunk = this.byStart[start];
  11197. while (chunk) {
  11198. chunk.reset();
  11199. chunk = end > chunk.end ? this.byStart[chunk.end] : null;
  11200. }
  11201. return this;
  11202. }
  11203. lastChar() {
  11204. if (this.outro.length) return this.outro[this.outro.length - 1];
  11205. let chunk = this.lastChunk;
  11206. do {
  11207. if (chunk.outro.length) return chunk.outro[chunk.outro.length - 1];
  11208. if (chunk.content.length) return chunk.content[chunk.content.length - 1];
  11209. if (chunk.intro.length) return chunk.intro[chunk.intro.length - 1];
  11210. } while ((chunk = chunk.previous));
  11211. if (this.intro.length) return this.intro[this.intro.length - 1];
  11212. return '';
  11213. }
  11214. lastLine() {
  11215. let lineIndex = this.outro.lastIndexOf(n$1);
  11216. if (lineIndex !== -1) return this.outro.substr(lineIndex + 1);
  11217. let lineStr = this.outro;
  11218. let chunk = this.lastChunk;
  11219. do {
  11220. if (chunk.outro.length > 0) {
  11221. lineIndex = chunk.outro.lastIndexOf(n$1);
  11222. if (lineIndex !== -1) return chunk.outro.substr(lineIndex + 1) + lineStr;
  11223. lineStr = chunk.outro + lineStr;
  11224. }
  11225. if (chunk.content.length > 0) {
  11226. lineIndex = chunk.content.lastIndexOf(n$1);
  11227. if (lineIndex !== -1) return chunk.content.substr(lineIndex + 1) + lineStr;
  11228. lineStr = chunk.content + lineStr;
  11229. }
  11230. if (chunk.intro.length > 0) {
  11231. lineIndex = chunk.intro.lastIndexOf(n$1);
  11232. if (lineIndex !== -1) return chunk.intro.substr(lineIndex + 1) + lineStr;
  11233. lineStr = chunk.intro + lineStr;
  11234. }
  11235. } while ((chunk = chunk.previous));
  11236. lineIndex = this.intro.lastIndexOf(n$1);
  11237. if (lineIndex !== -1) return this.intro.substr(lineIndex + 1) + lineStr;
  11238. return this.intro + lineStr;
  11239. }
  11240. slice(start = 0, end = this.original.length) {
  11241. if (this.original.length !== 0) {
  11242. while (start < 0) start += this.original.length;
  11243. while (end < 0) end += this.original.length;
  11244. }
  11245. let result = '';
  11246. // find start chunk
  11247. let chunk = this.firstChunk;
  11248. while (chunk && (chunk.start > start || chunk.end <= start)) {
  11249. // found end chunk before start
  11250. if (chunk.start < end && chunk.end >= end) {
  11251. return result;
  11252. }
  11253. chunk = chunk.next;
  11254. }
  11255. if (chunk && chunk.edited && chunk.start !== start)
  11256. throw new Error(`Cannot use replaced character ${start} as slice start anchor.`);
  11257. const startChunk = chunk;
  11258. while (chunk) {
  11259. if (chunk.intro && (startChunk !== chunk || chunk.start === start)) {
  11260. result += chunk.intro;
  11261. }
  11262. const containsEnd = chunk.start < end && chunk.end >= end;
  11263. if (containsEnd && chunk.edited && chunk.end !== end)
  11264. throw new Error(`Cannot use replaced character ${end} as slice end anchor.`);
  11265. const sliceStart = startChunk === chunk ? start - chunk.start : 0;
  11266. const sliceEnd = containsEnd ? chunk.content.length + end - chunk.end : chunk.content.length;
  11267. result += chunk.content.slice(sliceStart, sliceEnd);
  11268. if (chunk.outro && (!containsEnd || chunk.end === end)) {
  11269. result += chunk.outro;
  11270. }
  11271. if (containsEnd) {
  11272. break;
  11273. }
  11274. chunk = chunk.next;
  11275. }
  11276. return result;
  11277. }
  11278. // TODO deprecate this? not really very useful
  11279. snip(start, end) {
  11280. const clone = this.clone();
  11281. clone.remove(0, start);
  11282. clone.remove(end, clone.original.length);
  11283. return clone;
  11284. }
  11285. _split(index) {
  11286. if (this.byStart[index] || this.byEnd[index]) return;
  11287. let chunk = this.lastSearchedChunk;
  11288. const searchForward = index > chunk.end;
  11289. while (chunk) {
  11290. if (chunk.contains(index)) return this._splitChunk(chunk, index);
  11291. chunk = searchForward ? this.byStart[chunk.end] : this.byEnd[chunk.start];
  11292. }
  11293. }
  11294. _splitChunk(chunk, index) {
  11295. if (chunk.edited && chunk.content.length) {
  11296. // zero-length edited chunks are a special case (overlapping replacements)
  11297. const loc = getLocator(this.original)(index);
  11298. throw new Error(
  11299. `Cannot split a chunk that has already been edited (${loc.line}:${loc.column} – "${chunk.original}")`,
  11300. );
  11301. }
  11302. const newChunk = chunk.split(index);
  11303. this.byEnd[index] = chunk;
  11304. this.byStart[index] = newChunk;
  11305. this.byEnd[newChunk.end] = newChunk;
  11306. if (chunk === this.lastChunk) this.lastChunk = newChunk;
  11307. this.lastSearchedChunk = chunk;
  11308. return true;
  11309. }
  11310. toString() {
  11311. let str = this.intro;
  11312. let chunk = this.firstChunk;
  11313. while (chunk) {
  11314. str += chunk.toString();
  11315. chunk = chunk.next;
  11316. }
  11317. return str + this.outro;
  11318. }
  11319. isEmpty() {
  11320. let chunk = this.firstChunk;
  11321. do {
  11322. if (
  11323. (chunk.intro.length && chunk.intro.trim()) ||
  11324. (chunk.content.length && chunk.content.trim()) ||
  11325. (chunk.outro.length && chunk.outro.trim())
  11326. )
  11327. return false;
  11328. } while ((chunk = chunk.next));
  11329. return true;
  11330. }
  11331. length() {
  11332. let chunk = this.firstChunk;
  11333. let length = 0;
  11334. do {
  11335. length += chunk.intro.length + chunk.content.length + chunk.outro.length;
  11336. } while ((chunk = chunk.next));
  11337. return length;
  11338. }
  11339. trimLines() {
  11340. return this.trim('[\\r\\n]');
  11341. }
  11342. trim(charType) {
  11343. return this.trimStart(charType).trimEnd(charType);
  11344. }
  11345. trimEndAborted(charType) {
  11346. const rx = new RegExp((charType || '\\s') + '+$');
  11347. this.outro = this.outro.replace(rx, '');
  11348. if (this.outro.length) return true;
  11349. let chunk = this.lastChunk;
  11350. do {
  11351. const end = chunk.end;
  11352. const aborted = chunk.trimEnd(rx);
  11353. // if chunk was trimmed, we have a new lastChunk
  11354. if (chunk.end !== end) {
  11355. if (this.lastChunk === chunk) {
  11356. this.lastChunk = chunk.next;
  11357. }
  11358. this.byEnd[chunk.end] = chunk;
  11359. this.byStart[chunk.next.start] = chunk.next;
  11360. this.byEnd[chunk.next.end] = chunk.next;
  11361. }
  11362. if (aborted) return true;
  11363. chunk = chunk.previous;
  11364. } while (chunk);
  11365. return false;
  11366. }
  11367. trimEnd(charType) {
  11368. this.trimEndAborted(charType);
  11369. return this;
  11370. }
  11371. trimStartAborted(charType) {
  11372. const rx = new RegExp('^' + (charType || '\\s') + '+');
  11373. this.intro = this.intro.replace(rx, '');
  11374. if (this.intro.length) return true;
  11375. let chunk = this.firstChunk;
  11376. do {
  11377. const end = chunk.end;
  11378. const aborted = chunk.trimStart(rx);
  11379. if (chunk.end !== end) {
  11380. // special case...
  11381. if (chunk === this.lastChunk) this.lastChunk = chunk.next;
  11382. this.byEnd[chunk.end] = chunk;
  11383. this.byStart[chunk.next.start] = chunk.next;
  11384. this.byEnd[chunk.next.end] = chunk.next;
  11385. }
  11386. if (aborted) return true;
  11387. chunk = chunk.next;
  11388. } while (chunk);
  11389. return false;
  11390. }
  11391. trimStart(charType) {
  11392. this.trimStartAborted(charType);
  11393. return this;
  11394. }
  11395. hasChanged() {
  11396. return this.original !== this.toString();
  11397. }
  11398. _replaceRegexp(searchValue, replacement) {
  11399. function getReplacement(match, str) {
  11400. if (typeof replacement === 'string') {
  11401. return replacement.replace(/\$(\$|&|\d+)/g, (_, i) => {
  11402. // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#specifying_a_string_as_a_parameter
  11403. if (i === '$') return '$';
  11404. if (i === '&') return match[0];
  11405. const num = +i;
  11406. if (num < match.length) return match[+i];
  11407. return `$${i}`;
  11408. });
  11409. } else {
  11410. return replacement(...match, match.index, str, match.groups);
  11411. }
  11412. }
  11413. function matchAll(re, str) {
  11414. let match;
  11415. const matches = [];
  11416. while ((match = re.exec(str))) {
  11417. matches.push(match);
  11418. }
  11419. return matches;
  11420. }
  11421. if (searchValue.global) {
  11422. const matches = matchAll(searchValue, this.original);
  11423. matches.forEach((match) => {
  11424. if (match.index != null) {
  11425. const replacement = getReplacement(match, this.original);
  11426. if (replacement !== match[0]) {
  11427. this.overwrite(
  11428. match.index,
  11429. match.index + match[0].length,
  11430. replacement
  11431. );
  11432. }
  11433. }
  11434. });
  11435. } else {
  11436. const match = this.original.match(searchValue);
  11437. if (match && match.index != null) {
  11438. const replacement = getReplacement(match, this.original);
  11439. if (replacement !== match[0]) {
  11440. this.overwrite(
  11441. match.index,
  11442. match.index + match[0].length,
  11443. replacement
  11444. );
  11445. }
  11446. }
  11447. }
  11448. return this;
  11449. }
  11450. _replaceString(string, replacement) {
  11451. const { original } = this;
  11452. const index = original.indexOf(string);
  11453. if (index !== -1) {
  11454. this.overwrite(index, index + string.length, replacement);
  11455. }
  11456. return this;
  11457. }
  11458. replace(searchValue, replacement) {
  11459. if (typeof searchValue === 'string') {
  11460. return this._replaceString(searchValue, replacement);
  11461. }
  11462. return this._replaceRegexp(searchValue, replacement);
  11463. }
  11464. _replaceAllString(string, replacement) {
  11465. const { original } = this;
  11466. const stringLength = string.length;
  11467. for (
  11468. let index = original.indexOf(string);
  11469. index !== -1;
  11470. index = original.indexOf(string, index + stringLength)
  11471. ) {
  11472. const previous = original.slice(index, index + stringLength);
  11473. if (previous !== replacement)
  11474. this.overwrite(index, index + stringLength, replacement);
  11475. }
  11476. return this;
  11477. }
  11478. replaceAll(searchValue, replacement) {
  11479. if (typeof searchValue === 'string') {
  11480. return this._replaceAllString(searchValue, replacement);
  11481. }
  11482. if (!searchValue.global) {
  11483. throw new TypeError(
  11484. 'MagicString.prototype.replaceAll called with a non-global RegExp argument',
  11485. );
  11486. }
  11487. return this._replaceRegexp(searchValue, replacement);
  11488. }
  11489. }
  11490. function isReference(node, parent) {
  11491. if (node.type === 'MemberExpression') {
  11492. return !node.computed && isReference(node.object, node);
  11493. }
  11494. if (node.type === 'Identifier') {
  11495. if (!parent)
  11496. return true;
  11497. switch (parent.type) {
  11498. // disregard `bar` in `foo.bar`
  11499. case 'MemberExpression': return parent.computed || node === parent.object;
  11500. // disregard the `foo` in `class {foo(){}}` but keep it in `class {[foo](){}}`
  11501. case 'MethodDefinition': return parent.computed;
  11502. // disregard the `foo` in `class {foo=bar}` but keep it in `class {[foo]=bar}` and `class {bar=foo}`
  11503. case 'FieldDefinition': return parent.computed || node === parent.value;
  11504. // disregard the `bar` in `{ bar: foo }`, but keep it in `{ [bar]: foo }`
  11505. case 'Property': return parent.computed || node === parent.value;
  11506. // disregard the `bar` in `export { foo as bar }` or
  11507. // the foo in `import { foo as bar }`
  11508. case 'ExportSpecifier':
  11509. case 'ImportSpecifier': return node === parent.local;
  11510. // disregard the `foo` in `foo: while (...) { ... break foo; ... continue foo;}`
  11511. case 'LabeledStatement':
  11512. case 'BreakStatement':
  11513. case 'ContinueStatement': return false;
  11514. default: return true;
  11515. }
  11516. }
  11517. return false;
  11518. }
  11519. var version$2 = "26.0.1";
  11520. var peerDependencies = {
  11521. rollup: "^2.68.0||^3.0.0||^4.0.0"
  11522. };
  11523. function tryParse(parse, code, id) {
  11524. try {
  11525. return parse(code, { allowReturnOutsideFunction: true });
  11526. } catch (err) {
  11527. err.message += ` in ${id}`;
  11528. throw err;
  11529. }
  11530. }
  11531. const firstpassGlobal = /\b(?:require|module|exports|global)\b/;
  11532. const firstpassNoGlobal = /\b(?:require|module|exports)\b/;
  11533. function hasCjsKeywords(code, ignoreGlobal) {
  11534. const firstpass = ignoreGlobal ? firstpassNoGlobal : firstpassGlobal;
  11535. return firstpass.test(code);
  11536. }
  11537. /* eslint-disable no-underscore-dangle */
  11538. function analyzeTopLevelStatements(parse, code, id) {
  11539. const ast = tryParse(parse, code, id);
  11540. let isEsModule = false;
  11541. let hasDefaultExport = false;
  11542. let hasNamedExports = false;
  11543. for (const node of ast.body) {
  11544. switch (node.type) {
  11545. case 'ExportDefaultDeclaration':
  11546. isEsModule = true;
  11547. hasDefaultExport = true;
  11548. break;
  11549. case 'ExportNamedDeclaration':
  11550. isEsModule = true;
  11551. if (node.declaration) {
  11552. hasNamedExports = true;
  11553. } else {
  11554. for (const specifier of node.specifiers) {
  11555. if (specifier.exported.name === 'default') {
  11556. hasDefaultExport = true;
  11557. } else {
  11558. hasNamedExports = true;
  11559. }
  11560. }
  11561. }
  11562. break;
  11563. case 'ExportAllDeclaration':
  11564. isEsModule = true;
  11565. if (node.exported && node.exported.name === 'default') {
  11566. hasDefaultExport = true;
  11567. } else {
  11568. hasNamedExports = true;
  11569. }
  11570. break;
  11571. case 'ImportDeclaration':
  11572. isEsModule = true;
  11573. break;
  11574. }
  11575. }
  11576. return { isEsModule, hasDefaultExport, hasNamedExports, ast };
  11577. }
  11578. /* eslint-disable import/prefer-default-export */
  11579. function deconflict(scopes, globals, identifier) {
  11580. let i = 1;
  11581. let deconflicted = makeLegalIdentifier(identifier);
  11582. const hasConflicts = () =>
  11583. scopes.some((scope) => scope.contains(deconflicted)) || globals.has(deconflicted);
  11584. while (hasConflicts()) {
  11585. deconflicted = makeLegalIdentifier(`${identifier}_${i}`);
  11586. i += 1;
  11587. }
  11588. for (const scope of scopes) {
  11589. scope.declarations[deconflicted] = true;
  11590. }
  11591. return deconflicted;
  11592. }
  11593. function getName(id) {
  11594. const name = makeLegalIdentifier(basename$1(id, extname(id)));
  11595. if (name !== 'index') {
  11596. return name;
  11597. }
  11598. return makeLegalIdentifier(basename$1(dirname$1(id)));
  11599. }
  11600. function normalizePathSlashes(path) {
  11601. return path.replace(/\\/g, '/');
  11602. }
  11603. const getVirtualPathForDynamicRequirePath = (path, commonDir) =>
  11604. `/${normalizePathSlashes(relative$1(commonDir, path))}`;
  11605. function capitalize(name) {
  11606. return name[0].toUpperCase() + name.slice(1);
  11607. }
  11608. function getStrictRequiresFilter({ strictRequires }) {
  11609. switch (strictRequires) {
  11610. case true:
  11611. return { strictRequiresFilter: () => true, detectCyclesAndConditional: false };
  11612. // eslint-disable-next-line no-undefined
  11613. case undefined:
  11614. case 'auto':
  11615. case 'debug':
  11616. case null:
  11617. return { strictRequiresFilter: () => false, detectCyclesAndConditional: true };
  11618. case false:
  11619. return { strictRequiresFilter: () => false, detectCyclesAndConditional: false };
  11620. default:
  11621. if (typeof strictRequires === 'string' || Array.isArray(strictRequires)) {
  11622. return {
  11623. strictRequiresFilter: createFilter$1(strictRequires),
  11624. detectCyclesAndConditional: false
  11625. };
  11626. }
  11627. throw new Error('Unexpected value for "strictRequires" option.');
  11628. }
  11629. }
  11630. function getPackageEntryPoint(dirPath) {
  11631. let entryPoint = 'index.js';
  11632. try {
  11633. if (existsSync(join$1(dirPath, 'package.json'))) {
  11634. entryPoint =
  11635. JSON.parse(readFileSync(join$1(dirPath, 'package.json'), { encoding: 'utf8' })).main ||
  11636. entryPoint;
  11637. }
  11638. } catch (ignored) {
  11639. // ignored
  11640. }
  11641. return entryPoint;
  11642. }
  11643. function isDirectory$1(path) {
  11644. try {
  11645. if (statSync$1(path).isDirectory()) return true;
  11646. } catch (ignored) {
  11647. // Nothing to do here
  11648. }
  11649. return false;
  11650. }
  11651. function getDynamicRequireModules(patterns, dynamicRequireRoot) {
  11652. const dynamicRequireModules = new Map();
  11653. const dirNames = new Set();
  11654. for (const pattern of !patterns || Array.isArray(patterns) ? patterns || [] : [patterns]) {
  11655. const isNegated = pattern.startsWith('!');
  11656. const modifyMap = (targetPath, resolvedPath) =>
  11657. isNegated
  11658. ? dynamicRequireModules.delete(targetPath)
  11659. : dynamicRequireModules.set(targetPath, resolvedPath);
  11660. for (const path of glob$1
  11661. .sync(isNegated ? pattern.substr(1) : pattern)
  11662. .sort((a, b) => a.localeCompare(b, 'en'))) {
  11663. const resolvedPath = resolve$3(path);
  11664. const requirePath = normalizePathSlashes(resolvedPath);
  11665. if (isDirectory$1(resolvedPath)) {
  11666. dirNames.add(resolvedPath);
  11667. const modulePath = resolve$3(join$1(resolvedPath, getPackageEntryPoint(path)));
  11668. modifyMap(requirePath, modulePath);
  11669. modifyMap(normalizePathSlashes(modulePath), modulePath);
  11670. } else {
  11671. dirNames.add(dirname$1(resolvedPath));
  11672. modifyMap(requirePath, resolvedPath);
  11673. }
  11674. }
  11675. }
  11676. return {
  11677. commonDir: dirNames.size ? getCommonDir([...dirNames, dynamicRequireRoot]) : null,
  11678. dynamicRequireModules
  11679. };
  11680. }
  11681. const FAILED_REQUIRE_ERROR = `throw new Error('Could not dynamically require "' + path + '". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.');`;
  11682. const COMMONJS_REQUIRE_EXPORT = 'commonjsRequire';
  11683. const CREATE_COMMONJS_REQUIRE_EXPORT = 'createCommonjsRequire';
  11684. function getDynamicModuleRegistry(
  11685. isDynamicRequireModulesEnabled,
  11686. dynamicRequireModules,
  11687. commonDir,
  11688. ignoreDynamicRequires
  11689. ) {
  11690. if (!isDynamicRequireModulesEnabled) {
  11691. return `export function ${COMMONJS_REQUIRE_EXPORT}(path) {
  11692. ${FAILED_REQUIRE_ERROR}
  11693. }`;
  11694. }
  11695. const dynamicModuleImports = [...dynamicRequireModules.values()]
  11696. .map(
  11697. (id, index) =>
  11698. `import ${
  11699. id.endsWith('.json') ? `json${index}` : `{ __require as require${index} }`
  11700. } from ${JSON.stringify(id)};`
  11701. )
  11702. .join('\n');
  11703. const dynamicModuleProps = [...dynamicRequireModules.keys()]
  11704. .map(
  11705. (id, index) =>
  11706. `\t\t${JSON.stringify(getVirtualPathForDynamicRequirePath(id, commonDir))}: ${
  11707. id.endsWith('.json') ? `function () { return json${index}; }` : `require${index}`
  11708. }`
  11709. )
  11710. .join(',\n');
  11711. return `${dynamicModuleImports}
  11712. var dynamicModules;
  11713. function getDynamicModules() {
  11714. return dynamicModules || (dynamicModules = {
  11715. ${dynamicModuleProps}
  11716. });
  11717. }
  11718. export function ${CREATE_COMMONJS_REQUIRE_EXPORT}(originalModuleDir) {
  11719. function handleRequire(path) {
  11720. var resolvedPath = commonjsResolve(path, originalModuleDir);
  11721. if (resolvedPath !== null) {
  11722. return getDynamicModules()[resolvedPath]();
  11723. }
  11724. ${ignoreDynamicRequires ? 'return require(path);' : FAILED_REQUIRE_ERROR}
  11725. }
  11726. handleRequire.resolve = function (path) {
  11727. var resolvedPath = commonjsResolve(path, originalModuleDir);
  11728. if (resolvedPath !== null) {
  11729. return resolvedPath;
  11730. }
  11731. return require.resolve(path);
  11732. }
  11733. return handleRequire;
  11734. }
  11735. function commonjsResolve (path, originalModuleDir) {
  11736. var shouldTryNodeModules = isPossibleNodeModulesPath(path);
  11737. path = normalize(path);
  11738. var relPath;
  11739. if (path[0] === '/') {
  11740. originalModuleDir = '';
  11741. }
  11742. var modules = getDynamicModules();
  11743. var checkedExtensions = ['', '.js', '.json'];
  11744. while (true) {
  11745. if (!shouldTryNodeModules) {
  11746. relPath = normalize(originalModuleDir + '/' + path);
  11747. } else {
  11748. relPath = normalize(originalModuleDir + '/node_modules/' + path);
  11749. }
  11750. if (relPath.endsWith('/..')) {
  11751. break; // Travelled too far up, avoid infinite loop
  11752. }
  11753. for (var extensionIndex = 0; extensionIndex < checkedExtensions.length; extensionIndex++) {
  11754. var resolvedPath = relPath + checkedExtensions[extensionIndex];
  11755. if (modules[resolvedPath]) {
  11756. return resolvedPath;
  11757. }
  11758. }
  11759. if (!shouldTryNodeModules) break;
  11760. var nextDir = normalize(originalModuleDir + '/..');
  11761. if (nextDir === originalModuleDir) break;
  11762. originalModuleDir = nextDir;
  11763. }
  11764. return null;
  11765. }
  11766. function isPossibleNodeModulesPath (modulePath) {
  11767. var c0 = modulePath[0];
  11768. if (c0 === '/' || c0 === '\\\\') return false;
  11769. var c1 = modulePath[1], c2 = modulePath[2];
  11770. if ((c0 === '.' && (!c1 || c1 === '/' || c1 === '\\\\')) ||
  11771. (c0 === '.' && c1 === '.' && (!c2 || c2 === '/' || c2 === '\\\\'))) return false;
  11772. if (c1 === ':' && (c2 === '/' || c2 === '\\\\')) return false;
  11773. return true;
  11774. }
  11775. function normalize (path) {
  11776. path = path.replace(/\\\\/g, '/');
  11777. var parts = path.split('/');
  11778. var slashed = parts[0] === '';
  11779. for (var i = 1; i < parts.length; i++) {
  11780. if (parts[i] === '.' || parts[i] === '') {
  11781. parts.splice(i--, 1);
  11782. }
  11783. }
  11784. for (var i = 1; i < parts.length; i++) {
  11785. if (parts[i] !== '..') continue;
  11786. if (i > 0 && parts[i - 1] !== '..' && parts[i - 1] !== '.') {
  11787. parts.splice(--i, 2);
  11788. i--;
  11789. }
  11790. }
  11791. path = parts.join('/');
  11792. if (slashed && path[0] !== '/') path = '/' + path;
  11793. else if (path.length === 0) path = '.';
  11794. return path;
  11795. }`;
  11796. }
  11797. const isWrappedId = (id, suffix) => id.endsWith(suffix);
  11798. const wrapId = (id, suffix) => `\0${id}${suffix}`;
  11799. const unwrapId = (wrappedId, suffix) => wrappedId.slice(1, -suffix.length);
  11800. const PROXY_SUFFIX = '?commonjs-proxy';
  11801. const WRAPPED_SUFFIX = '?commonjs-wrapped';
  11802. const EXTERNAL_SUFFIX = '?commonjs-external';
  11803. const EXPORTS_SUFFIX = '?commonjs-exports';
  11804. const MODULE_SUFFIX = '?commonjs-module';
  11805. const ENTRY_SUFFIX = '?commonjs-entry';
  11806. const ES_IMPORT_SUFFIX = '?commonjs-es-import';
  11807. const DYNAMIC_MODULES_ID = '\0commonjs-dynamic-modules';
  11808. const HELPERS_ID = '\0commonjsHelpers.js';
  11809. const IS_WRAPPED_COMMONJS = 'withRequireFunction';
  11810. // `x['default']` is used instead of `x.default` for backward compatibility with ES3 browsers.
  11811. // Minifiers like uglify will usually transpile it back if compatibility with ES3 is not enabled.
  11812. // This could be improved by inspecting Rollup's "generatedCode" option
  11813. const HELPERS = `
  11814. export var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
  11815. export function getDefaultExportFromCjs (x) {
  11816. return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
  11817. }
  11818. export function getDefaultExportFromNamespaceIfPresent (n) {
  11819. return n && Object.prototype.hasOwnProperty.call(n, 'default') ? n['default'] : n;
  11820. }
  11821. export function getDefaultExportFromNamespaceIfNotNamed (n) {
  11822. return n && Object.prototype.hasOwnProperty.call(n, 'default') && Object.keys(n).length === 1 ? n['default'] : n;
  11823. }
  11824. export function getAugmentedNamespace(n) {
  11825. if (n.__esModule) return n;
  11826. var f = n.default;
  11827. if (typeof f == "function") {
  11828. var a = function a () {
  11829. if (this instanceof a) {
  11830. return Reflect.construct(f, arguments, this.constructor);
  11831. }
  11832. return f.apply(this, arguments);
  11833. };
  11834. a.prototype = f.prototype;
  11835. } else a = {};
  11836. Object.defineProperty(a, '__esModule', {value: true});
  11837. Object.keys(n).forEach(function (k) {
  11838. var d = Object.getOwnPropertyDescriptor(n, k);
  11839. Object.defineProperty(a, k, d.get ? d : {
  11840. enumerable: true,
  11841. get: function () {
  11842. return n[k];
  11843. }
  11844. });
  11845. });
  11846. return a;
  11847. }
  11848. `;
  11849. function getHelpersModule() {
  11850. return HELPERS;
  11851. }
  11852. function getUnknownRequireProxy(id, requireReturnsDefault) {
  11853. if (requireReturnsDefault === true || id.endsWith('.json')) {
  11854. return `export { default } from ${JSON.stringify(id)};`;
  11855. }
  11856. const name = getName(id);
  11857. const exported =
  11858. requireReturnsDefault === 'auto'
  11859. ? `import { getDefaultExportFromNamespaceIfNotNamed } from "${HELPERS_ID}"; export default /*@__PURE__*/getDefaultExportFromNamespaceIfNotNamed(${name});`
  11860. : requireReturnsDefault === 'preferred'
  11861. ? `import { getDefaultExportFromNamespaceIfPresent } from "${HELPERS_ID}"; export default /*@__PURE__*/getDefaultExportFromNamespaceIfPresent(${name});`
  11862. : !requireReturnsDefault
  11863. ? `import { getAugmentedNamespace } from "${HELPERS_ID}"; export default /*@__PURE__*/getAugmentedNamespace(${name});`
  11864. : `export default ${name};`;
  11865. return `import * as ${name} from ${JSON.stringify(id)}; ${exported}`;
  11866. }
  11867. async function getStaticRequireProxy(id, requireReturnsDefault, loadModule) {
  11868. const name = getName(id);
  11869. const {
  11870. meta: { commonjs: commonjsMeta }
  11871. } = await loadModule({ id });
  11872. if (!commonjsMeta) {
  11873. return getUnknownRequireProxy(id, requireReturnsDefault);
  11874. }
  11875. if (commonjsMeta.isCommonJS) {
  11876. return `export { __moduleExports as default } from ${JSON.stringify(id)};`;
  11877. }
  11878. if (!requireReturnsDefault) {
  11879. return `import { getAugmentedNamespace } from "${HELPERS_ID}"; import * as ${name} from ${JSON.stringify(
  11880. id
  11881. )}; export default /*@__PURE__*/getAugmentedNamespace(${name});`;
  11882. }
  11883. if (
  11884. requireReturnsDefault !== true &&
  11885. (requireReturnsDefault === 'namespace' ||
  11886. !commonjsMeta.hasDefaultExport ||
  11887. (requireReturnsDefault === 'auto' && commonjsMeta.hasNamedExports))
  11888. ) {
  11889. return `import * as ${name} from ${JSON.stringify(id)}; export default ${name};`;
  11890. }
  11891. return `export { default } from ${JSON.stringify(id)};`;
  11892. }
  11893. function getEntryProxy(id, defaultIsModuleExports, getModuleInfo, shebang) {
  11894. const {
  11895. meta: { commonjs: commonjsMeta },
  11896. hasDefaultExport
  11897. } = getModuleInfo(id);
  11898. if (!commonjsMeta || commonjsMeta.isCommonJS !== IS_WRAPPED_COMMONJS) {
  11899. const stringifiedId = JSON.stringify(id);
  11900. let code = `export * from ${stringifiedId};`;
  11901. if (hasDefaultExport) {
  11902. code += `export { default } from ${stringifiedId};`;
  11903. }
  11904. return shebang + code;
  11905. }
  11906. const result = getEsImportProxy(id, defaultIsModuleExports);
  11907. return {
  11908. ...result,
  11909. code: shebang + result.code
  11910. };
  11911. }
  11912. function getEsImportProxy(id, defaultIsModuleExports) {
  11913. const name = getName(id);
  11914. const exportsName = `${name}Exports`;
  11915. const requireModule = `require${capitalize(name)}`;
  11916. let code =
  11917. `import { getDefaultExportFromCjs } from "${HELPERS_ID}";\n` +
  11918. `import { __require as ${requireModule} } from ${JSON.stringify(id)};\n` +
  11919. `var ${exportsName} = ${requireModule}();\n` +
  11920. `export { ${exportsName} as __moduleExports };`;
  11921. if (defaultIsModuleExports === true) {
  11922. code += `\nexport { ${exportsName} as default };`;
  11923. } else {
  11924. code += `export default /*@__PURE__*/getDefaultExportFromCjs(${exportsName});`;
  11925. }
  11926. return {
  11927. code,
  11928. syntheticNamedExports: '__moduleExports'
  11929. };
  11930. }
  11931. /* eslint-disable no-param-reassign, no-undefined */
  11932. function getCandidatesForExtension(resolved, extension) {
  11933. return [resolved + extension, `${resolved}${sep$1}index${extension}`];
  11934. }
  11935. function getCandidates(resolved, extensions) {
  11936. return extensions.reduce(
  11937. (paths, extension) => paths.concat(getCandidatesForExtension(resolved, extension)),
  11938. [resolved]
  11939. );
  11940. }
  11941. function resolveExtensions(importee, importer, extensions) {
  11942. // not our problem
  11943. if (importee[0] !== '.' || !importer) return undefined;
  11944. const resolved = resolve$3(dirname$1(importer), importee);
  11945. const candidates = getCandidates(resolved, extensions);
  11946. for (let i = 0; i < candidates.length; i += 1) {
  11947. try {
  11948. const stats = statSync$1(candidates[i]);
  11949. if (stats.isFile()) return { id: candidates[i] };
  11950. } catch (err) {
  11951. /* noop */
  11952. }
  11953. }
  11954. return undefined;
  11955. }
  11956. function getResolveId(extensions, isPossibleCjsId) {
  11957. const currentlyResolving = new Map();
  11958. return {
  11959. /**
  11960. * This is a Maps of importers to Sets of require sources being resolved at
  11961. * the moment by resolveRequireSourcesAndUpdateMeta
  11962. */
  11963. currentlyResolving,
  11964. async resolveId(importee, importer, resolveOptions) {
  11965. const customOptions = resolveOptions.custom;
  11966. // All logic below is specific to ES imports.
  11967. // Also, if we do not skip this logic for requires that are resolved while
  11968. // transforming a commonjs file, it can easily lead to deadlocks.
  11969. if (
  11970. customOptions &&
  11971. customOptions['node-resolve'] &&
  11972. customOptions['node-resolve'].isRequire
  11973. ) {
  11974. return null;
  11975. }
  11976. const currentlyResolvingForParent = currentlyResolving.get(importer);
  11977. if (currentlyResolvingForParent && currentlyResolvingForParent.has(importee)) {
  11978. this.warn({
  11979. code: 'THIS_RESOLVE_WITHOUT_OPTIONS',
  11980. message:
  11981. 'It appears a plugin has implemented a "resolveId" hook that uses "this.resolve" without forwarding the third "options" parameter of "resolveId". This is problematic as it can lead to wrong module resolutions especially for the node-resolve plugin and in certain cases cause early exit errors for the commonjs plugin.\nIn rare cases, this warning can appear if the same file is both imported and required from the same mixed ES/CommonJS module, in which case it can be ignored.',
  11982. url: 'https://rollupjs.org/guide/en/#resolveid'
  11983. });
  11984. return null;
  11985. }
  11986. if (isWrappedId(importee, WRAPPED_SUFFIX)) {
  11987. return unwrapId(importee, WRAPPED_SUFFIX);
  11988. }
  11989. if (
  11990. importee.endsWith(ENTRY_SUFFIX) ||
  11991. isWrappedId(importee, MODULE_SUFFIX) ||
  11992. isWrappedId(importee, EXPORTS_SUFFIX) ||
  11993. isWrappedId(importee, PROXY_SUFFIX) ||
  11994. isWrappedId(importee, ES_IMPORT_SUFFIX) ||
  11995. isWrappedId(importee, EXTERNAL_SUFFIX) ||
  11996. importee.startsWith(HELPERS_ID) ||
  11997. importee === DYNAMIC_MODULES_ID
  11998. ) {
  11999. return importee;
  12000. }
  12001. if (importer) {
  12002. if (
  12003. importer === DYNAMIC_MODULES_ID ||
  12004. // Proxies are only importing resolved ids, no need to resolve again
  12005. isWrappedId(importer, PROXY_SUFFIX) ||
  12006. isWrappedId(importer, ES_IMPORT_SUFFIX) ||
  12007. importer.endsWith(ENTRY_SUFFIX)
  12008. ) {
  12009. return importee;
  12010. }
  12011. if (isWrappedId(importer, EXTERNAL_SUFFIX)) {
  12012. // We need to return null for unresolved imports so that the proper warning is shown
  12013. if (
  12014. !(await this.resolve(
  12015. importee,
  12016. importer,
  12017. Object.assign({ skipSelf: true }, resolveOptions)
  12018. ))
  12019. ) {
  12020. return null;
  12021. }
  12022. // For other external imports, we need to make sure they are handled as external
  12023. return { id: importee, external: true };
  12024. }
  12025. }
  12026. if (importee.startsWith('\0')) {
  12027. return null;
  12028. }
  12029. // If this is an entry point or ESM import, we need to figure out if the importee is wrapped and
  12030. // if that is the case, we need to add a proxy.
  12031. const resolved =
  12032. (await this.resolve(
  12033. importee,
  12034. importer,
  12035. Object.assign({ skipSelf: true }, resolveOptions)
  12036. )) || resolveExtensions(importee, importer, extensions);
  12037. // Make sure that even if other plugins resolve again, we ignore our own proxies
  12038. if (
  12039. !resolved ||
  12040. resolved.external ||
  12041. resolved.id.endsWith(ENTRY_SUFFIX) ||
  12042. isWrappedId(resolved.id, ES_IMPORT_SUFFIX) ||
  12043. !isPossibleCjsId(resolved.id)
  12044. ) {
  12045. return resolved;
  12046. }
  12047. const moduleInfo = await this.load(resolved);
  12048. const {
  12049. meta: { commonjs: commonjsMeta }
  12050. } = moduleInfo;
  12051. if (commonjsMeta) {
  12052. const { isCommonJS } = commonjsMeta;
  12053. if (isCommonJS) {
  12054. if (resolveOptions.isEntry) {
  12055. moduleInfo.moduleSideEffects = true;
  12056. // We must not precede entry proxies with a `\0` as that will mess up relative external resolution
  12057. return resolved.id + ENTRY_SUFFIX;
  12058. }
  12059. if (isCommonJS === IS_WRAPPED_COMMONJS) {
  12060. return { id: wrapId(resolved.id, ES_IMPORT_SUFFIX), meta: { commonjs: { resolved } } };
  12061. }
  12062. }
  12063. }
  12064. return resolved;
  12065. }
  12066. };
  12067. }
  12068. function getRequireResolver(extensions, detectCyclesAndConditional, currentlyResolving) {
  12069. const knownCjsModuleTypes = Object.create(null);
  12070. const requiredIds = Object.create(null);
  12071. const unconditionallyRequiredIds = Object.create(null);
  12072. const dependencies = Object.create(null);
  12073. const getDependencies = (id) => dependencies[id] || (dependencies[id] = new Set());
  12074. const isCyclic = (id) => {
  12075. const dependenciesToCheck = new Set(getDependencies(id));
  12076. for (const dependency of dependenciesToCheck) {
  12077. if (dependency === id) {
  12078. return true;
  12079. }
  12080. for (const childDependency of getDependencies(dependency)) {
  12081. dependenciesToCheck.add(childDependency);
  12082. }
  12083. }
  12084. return false;
  12085. };
  12086. // Once a module is listed here, its type (wrapped or not) is fixed and may
  12087. // not change for the rest of the current build, to not break already
  12088. // transformed modules.
  12089. const fullyAnalyzedModules = Object.create(null);
  12090. const getTypeForFullyAnalyzedModule = (id) => {
  12091. const knownType = knownCjsModuleTypes[id];
  12092. if (knownType !== true || !detectCyclesAndConditional || fullyAnalyzedModules[id]) {
  12093. return knownType;
  12094. }
  12095. if (isCyclic(id)) {
  12096. return (knownCjsModuleTypes[id] = IS_WRAPPED_COMMONJS);
  12097. }
  12098. return knownType;
  12099. };
  12100. const setInitialParentType = (id, initialCommonJSType) => {
  12101. // Fully analyzed modules may never change type
  12102. if (fullyAnalyzedModules[id]) {
  12103. return;
  12104. }
  12105. knownCjsModuleTypes[id] = initialCommonJSType;
  12106. if (
  12107. detectCyclesAndConditional &&
  12108. knownCjsModuleTypes[id] === true &&
  12109. requiredIds[id] &&
  12110. !unconditionallyRequiredIds[id]
  12111. ) {
  12112. knownCjsModuleTypes[id] = IS_WRAPPED_COMMONJS;
  12113. }
  12114. };
  12115. const analyzeRequiredModule = async (parentId, resolved, isConditional, loadModule) => {
  12116. const childId = resolved.id;
  12117. requiredIds[childId] = true;
  12118. if (!(isConditional || knownCjsModuleTypes[parentId] === IS_WRAPPED_COMMONJS)) {
  12119. unconditionallyRequiredIds[childId] = true;
  12120. }
  12121. getDependencies(parentId).add(childId);
  12122. if (!isCyclic(childId)) {
  12123. // This makes sure the current transform handler waits for all direct
  12124. // dependencies to be loaded and transformed and therefore for all
  12125. // transitive CommonJS dependencies to be loaded as well so that all
  12126. // cycles have been found and knownCjsModuleTypes is reliable.
  12127. await loadModule(resolved);
  12128. }
  12129. };
  12130. const getTypeForImportedModule = async (resolved, loadModule) => {
  12131. if (resolved.id in knownCjsModuleTypes) {
  12132. // This handles cyclic ES dependencies
  12133. return knownCjsModuleTypes[resolved.id];
  12134. }
  12135. const {
  12136. meta: { commonjs }
  12137. } = await loadModule(resolved);
  12138. return (commonjs && commonjs.isCommonJS) || false;
  12139. };
  12140. return {
  12141. getWrappedIds: () =>
  12142. Object.keys(knownCjsModuleTypes).filter(
  12143. (id) => knownCjsModuleTypes[id] === IS_WRAPPED_COMMONJS
  12144. ),
  12145. isRequiredId: (id) => requiredIds[id],
  12146. async shouldTransformCachedModule({
  12147. id: parentId,
  12148. resolvedSources,
  12149. meta: { commonjs: parentMeta }
  12150. }) {
  12151. // We explicitly track ES modules to handle circular imports
  12152. if (!(parentMeta && parentMeta.isCommonJS)) knownCjsModuleTypes[parentId] = false;
  12153. if (isWrappedId(parentId, ES_IMPORT_SUFFIX)) return false;
  12154. const parentRequires = parentMeta && parentMeta.requires;
  12155. if (parentRequires) {
  12156. setInitialParentType(parentId, parentMeta.initialCommonJSType);
  12157. await Promise.all(
  12158. parentRequires.map(({ resolved, isConditional }) =>
  12159. analyzeRequiredModule(parentId, resolved, isConditional, this.load)
  12160. )
  12161. );
  12162. if (getTypeForFullyAnalyzedModule(parentId) !== parentMeta.isCommonJS) {
  12163. return true;
  12164. }
  12165. for (const {
  12166. resolved: { id }
  12167. } of parentRequires) {
  12168. if (getTypeForFullyAnalyzedModule(id) !== parentMeta.isRequiredCommonJS[id]) {
  12169. return true;
  12170. }
  12171. }
  12172. // Now that we decided to go with the cached copy, neither the parent
  12173. // module nor any of its children may change types anymore
  12174. fullyAnalyzedModules[parentId] = true;
  12175. for (const {
  12176. resolved: { id }
  12177. } of parentRequires) {
  12178. fullyAnalyzedModules[id] = true;
  12179. }
  12180. }
  12181. const parentRequireSet = new Set((parentRequires || []).map(({ resolved: { id } }) => id));
  12182. return (
  12183. await Promise.all(
  12184. Object.keys(resolvedSources)
  12185. .map((source) => resolvedSources[source])
  12186. .filter(({ id, external }) => !(external || parentRequireSet.has(id)))
  12187. .map(async (resolved) => {
  12188. if (isWrappedId(resolved.id, ES_IMPORT_SUFFIX)) {
  12189. return (
  12190. (await getTypeForImportedModule(
  12191. (
  12192. await this.load({ id: resolved.id })
  12193. ).meta.commonjs.resolved,
  12194. this.load
  12195. )) !== IS_WRAPPED_COMMONJS
  12196. );
  12197. }
  12198. return (await getTypeForImportedModule(resolved, this.load)) === IS_WRAPPED_COMMONJS;
  12199. })
  12200. )
  12201. ).some((shouldTransform) => shouldTransform);
  12202. },
  12203. /* eslint-disable no-param-reassign */
  12204. resolveRequireSourcesAndUpdateMeta:
  12205. (rollupContext) => async (parentId, isParentCommonJS, parentMeta, sources) => {
  12206. parentMeta.initialCommonJSType = isParentCommonJS;
  12207. parentMeta.requires = [];
  12208. parentMeta.isRequiredCommonJS = Object.create(null);
  12209. setInitialParentType(parentId, isParentCommonJS);
  12210. const currentlyResolvingForParent = currentlyResolving.get(parentId) || new Set();
  12211. currentlyResolving.set(parentId, currentlyResolvingForParent);
  12212. const requireTargets = await Promise.all(
  12213. sources.map(async ({ source, isConditional }) => {
  12214. // Never analyze or proxy internal modules
  12215. if (source.startsWith('\0')) {
  12216. return { id: source, allowProxy: false };
  12217. }
  12218. currentlyResolvingForParent.add(source);
  12219. const resolved =
  12220. (await rollupContext.resolve(source, parentId, {
  12221. skipSelf: false,
  12222. custom: { 'node-resolve': { isRequire: true } }
  12223. })) || resolveExtensions(source, parentId, extensions);
  12224. currentlyResolvingForParent.delete(source);
  12225. if (!resolved) {
  12226. return { id: wrapId(source, EXTERNAL_SUFFIX), allowProxy: false };
  12227. }
  12228. const childId = resolved.id;
  12229. if (resolved.external) {
  12230. return { id: wrapId(childId, EXTERNAL_SUFFIX), allowProxy: false };
  12231. }
  12232. parentMeta.requires.push({ resolved, isConditional });
  12233. await analyzeRequiredModule(parentId, resolved, isConditional, rollupContext.load);
  12234. return { id: childId, allowProxy: true };
  12235. })
  12236. );
  12237. parentMeta.isCommonJS = getTypeForFullyAnalyzedModule(parentId);
  12238. fullyAnalyzedModules[parentId] = true;
  12239. return requireTargets.map(({ id: dependencyId, allowProxy }, index) => {
  12240. // eslint-disable-next-line no-multi-assign
  12241. const isCommonJS = (parentMeta.isRequiredCommonJS[dependencyId] =
  12242. getTypeForFullyAnalyzedModule(dependencyId));
  12243. fullyAnalyzedModules[dependencyId] = true;
  12244. return {
  12245. source: sources[index].source,
  12246. id: allowProxy
  12247. ? isCommonJS === IS_WRAPPED_COMMONJS
  12248. ? wrapId(dependencyId, WRAPPED_SUFFIX)
  12249. : wrapId(dependencyId, PROXY_SUFFIX)
  12250. : dependencyId,
  12251. isCommonJS
  12252. };
  12253. });
  12254. },
  12255. isCurrentlyResolving(source, parentId) {
  12256. const currentlyResolvingForParent = currentlyResolving.get(parentId);
  12257. return currentlyResolvingForParent && currentlyResolvingForParent.has(source);
  12258. }
  12259. };
  12260. }
  12261. function validateVersion(actualVersion, peerDependencyVersion, name) {
  12262. const versionRegexp = /\^(\d+\.\d+\.\d+)/g;
  12263. let minMajor = Infinity;
  12264. let minMinor = Infinity;
  12265. let minPatch = Infinity;
  12266. let foundVersion;
  12267. // eslint-disable-next-line no-cond-assign
  12268. while ((foundVersion = versionRegexp.exec(peerDependencyVersion))) {
  12269. const [foundMajor, foundMinor, foundPatch] = foundVersion[1].split('.').map(Number);
  12270. if (foundMajor < minMajor) {
  12271. minMajor = foundMajor;
  12272. minMinor = foundMinor;
  12273. minPatch = foundPatch;
  12274. }
  12275. }
  12276. if (!actualVersion) {
  12277. throw new Error(
  12278. `Insufficient ${name} version: "@rollup/plugin-commonjs" requires at least ${name}@${minMajor}.${minMinor}.${minPatch}.`
  12279. );
  12280. }
  12281. const [major, minor, patch] = actualVersion.split('.').map(Number);
  12282. if (
  12283. major < minMajor ||
  12284. (major === minMajor && (minor < minMinor || (minor === minMinor && patch < minPatch)))
  12285. ) {
  12286. throw new Error(
  12287. `Insufficient ${name} version: "@rollup/plugin-commonjs" requires at least ${name}@${minMajor}.${minMinor}.${minPatch} but found ${name}@${actualVersion}.`
  12288. );
  12289. }
  12290. }
  12291. const operators = {
  12292. '==': (x) => equals(x.left, x.right, false),
  12293. '!=': (x) => not(operators['=='](x)),
  12294. '===': (x) => equals(x.left, x.right, true),
  12295. '!==': (x) => not(operators['==='](x)),
  12296. '!': (x) => isFalsy(x.argument),
  12297. '&&': (x) => isTruthy(x.left) && isTruthy(x.right),
  12298. '||': (x) => isTruthy(x.left) || isTruthy(x.right)
  12299. };
  12300. function not(value) {
  12301. return value === null ? value : !value;
  12302. }
  12303. function equals(a, b, strict) {
  12304. if (a.type !== b.type) return null;
  12305. // eslint-disable-next-line eqeqeq
  12306. if (a.type === 'Literal') return strict ? a.value === b.value : a.value == b.value;
  12307. return null;
  12308. }
  12309. function isTruthy(node) {
  12310. if (!node) return false;
  12311. if (node.type === 'Literal') return !!node.value;
  12312. if (node.type === 'ParenthesizedExpression') return isTruthy(node.expression);
  12313. if (node.operator in operators) return operators[node.operator](node);
  12314. return null;
  12315. }
  12316. function isFalsy(node) {
  12317. return not(isTruthy(node));
  12318. }
  12319. function getKeypath(node) {
  12320. const parts = [];
  12321. while (node.type === 'MemberExpression') {
  12322. if (node.computed) return null;
  12323. parts.unshift(node.property.name);
  12324. // eslint-disable-next-line no-param-reassign
  12325. node = node.object;
  12326. }
  12327. if (node.type !== 'Identifier') return null;
  12328. const { name } = node;
  12329. parts.unshift(name);
  12330. return { name, keypath: parts.join('.') };
  12331. }
  12332. const KEY_COMPILED_ESM = '__esModule';
  12333. function getDefineCompiledEsmType(node) {
  12334. const definedPropertyWithExports = getDefinePropertyCallName(node, 'exports');
  12335. const definedProperty =
  12336. definedPropertyWithExports || getDefinePropertyCallName(node, 'module.exports');
  12337. if (definedProperty && definedProperty.key === KEY_COMPILED_ESM) {
  12338. return isTruthy(definedProperty.value)
  12339. ? definedPropertyWithExports
  12340. ? 'exports'
  12341. : 'module'
  12342. : false;
  12343. }
  12344. return false;
  12345. }
  12346. function getDefinePropertyCallName(node, targetName) {
  12347. const {
  12348. callee: { object, property }
  12349. } = node;
  12350. if (!object || object.type !== 'Identifier' || object.name !== 'Object') return;
  12351. if (!property || property.type !== 'Identifier' || property.name !== 'defineProperty') return;
  12352. if (node.arguments.length !== 3) return;
  12353. const targetNames = targetName.split('.');
  12354. const [target, key, value] = node.arguments;
  12355. if (targetNames.length === 1) {
  12356. if (target.type !== 'Identifier' || target.name !== targetNames[0]) {
  12357. return;
  12358. }
  12359. }
  12360. if (targetNames.length === 2) {
  12361. if (
  12362. target.type !== 'MemberExpression' ||
  12363. target.object.name !== targetNames[0] ||
  12364. target.property.name !== targetNames[1]
  12365. ) {
  12366. return;
  12367. }
  12368. }
  12369. if (value.type !== 'ObjectExpression' || !value.properties) return;
  12370. const valueProperty = value.properties.find((p) => p.key && p.key.name === 'value');
  12371. if (!valueProperty || !valueProperty.value) return;
  12372. // eslint-disable-next-line consistent-return
  12373. return { key: key.value, value: valueProperty.value };
  12374. }
  12375. function isShorthandProperty(parent) {
  12376. return parent && parent.type === 'Property' && parent.shorthand;
  12377. }
  12378. function wrapCode(magicString, uses, moduleName, exportsName, indentExclusionRanges) {
  12379. const args = [];
  12380. const passedArgs = [];
  12381. if (uses.module) {
  12382. args.push('module');
  12383. passedArgs.push(moduleName);
  12384. }
  12385. if (uses.exports) {
  12386. args.push('exports');
  12387. passedArgs.push(uses.module ? `${moduleName}.exports` : exportsName);
  12388. }
  12389. magicString
  12390. .trim()
  12391. .indent('\t', { exclude: indentExclusionRanges })
  12392. .prepend(`(function (${args.join(', ')}) {\n`)
  12393. // For some reason, this line is only indented correctly when using a
  12394. // require-wrapper if we have this leading space
  12395. .append(` \n} (${passedArgs.join(', ')}));`);
  12396. }
  12397. function rewriteExportsAndGetExportsBlock(
  12398. magicString,
  12399. moduleName,
  12400. exportsName,
  12401. exportedExportsName,
  12402. wrapped,
  12403. moduleExportsAssignments,
  12404. firstTopLevelModuleExportsAssignment,
  12405. exportsAssignmentsByName,
  12406. topLevelAssignments,
  12407. defineCompiledEsmExpressions,
  12408. deconflictedExportNames,
  12409. code,
  12410. HELPERS_NAME,
  12411. exportMode,
  12412. defaultIsModuleExports,
  12413. usesRequireWrapper,
  12414. requireName
  12415. ) {
  12416. const exports = [];
  12417. const exportDeclarations = [];
  12418. if (usesRequireWrapper) {
  12419. getExportsWhenUsingRequireWrapper(
  12420. magicString,
  12421. wrapped,
  12422. exportMode,
  12423. exports,
  12424. moduleExportsAssignments,
  12425. exportsAssignmentsByName,
  12426. moduleName,
  12427. exportsName,
  12428. requireName,
  12429. defineCompiledEsmExpressions
  12430. );
  12431. } else if (exportMode === 'replace') {
  12432. getExportsForReplacedModuleExports(
  12433. magicString,
  12434. exports,
  12435. exportDeclarations,
  12436. moduleExportsAssignments,
  12437. firstTopLevelModuleExportsAssignment,
  12438. exportsName,
  12439. defaultIsModuleExports,
  12440. HELPERS_NAME
  12441. );
  12442. } else {
  12443. if (exportMode === 'module') {
  12444. exportDeclarations.push(`var ${exportedExportsName} = ${moduleName}.exports`);
  12445. exports.push(`${exportedExportsName} as __moduleExports`);
  12446. } else {
  12447. exports.push(`${exportsName} as __moduleExports`);
  12448. }
  12449. if (wrapped) {
  12450. exportDeclarations.push(
  12451. getDefaultExportDeclaration(exportedExportsName, defaultIsModuleExports, HELPERS_NAME)
  12452. );
  12453. } else {
  12454. getExports(
  12455. magicString,
  12456. exports,
  12457. exportDeclarations,
  12458. moduleExportsAssignments,
  12459. exportsAssignmentsByName,
  12460. deconflictedExportNames,
  12461. topLevelAssignments,
  12462. moduleName,
  12463. exportsName,
  12464. exportedExportsName,
  12465. defineCompiledEsmExpressions,
  12466. HELPERS_NAME,
  12467. defaultIsModuleExports,
  12468. exportMode
  12469. );
  12470. }
  12471. }
  12472. if (exports.length) {
  12473. exportDeclarations.push(`export { ${exports.join(', ')} }`);
  12474. }
  12475. return `\n\n${exportDeclarations.join(';\n')};`;
  12476. }
  12477. function getExportsWhenUsingRequireWrapper(
  12478. magicString,
  12479. wrapped,
  12480. exportMode,
  12481. exports,
  12482. moduleExportsAssignments,
  12483. exportsAssignmentsByName,
  12484. moduleName,
  12485. exportsName,
  12486. requireName,
  12487. defineCompiledEsmExpressions
  12488. ) {
  12489. exports.push(`${requireName} as __require`);
  12490. if (wrapped) return;
  12491. if (exportMode === 'replace') {
  12492. rewriteModuleExportsAssignments(magicString, moduleExportsAssignments, exportsName);
  12493. } else {
  12494. rewriteModuleExportsAssignments(magicString, moduleExportsAssignments, `${moduleName}.exports`);
  12495. // Collect and rewrite named exports
  12496. for (const [exportName, { nodes }] of exportsAssignmentsByName) {
  12497. for (const { node, type } of nodes) {
  12498. magicString.overwrite(
  12499. node.start,
  12500. node.left.end,
  12501. `${
  12502. exportMode === 'module' && type === 'module' ? `${moduleName}.exports` : exportsName
  12503. }.${exportName}`
  12504. );
  12505. }
  12506. }
  12507. replaceDefineCompiledEsmExpressionsAndGetIfRestorable(
  12508. defineCompiledEsmExpressions,
  12509. magicString,
  12510. exportMode,
  12511. moduleName,
  12512. exportsName
  12513. );
  12514. }
  12515. }
  12516. function getExportsForReplacedModuleExports(
  12517. magicString,
  12518. exports,
  12519. exportDeclarations,
  12520. moduleExportsAssignments,
  12521. firstTopLevelModuleExportsAssignment,
  12522. exportsName,
  12523. defaultIsModuleExports,
  12524. HELPERS_NAME
  12525. ) {
  12526. for (const { left } of moduleExportsAssignments) {
  12527. magicString.overwrite(left.start, left.end, exportsName);
  12528. }
  12529. magicString.prependRight(firstTopLevelModuleExportsAssignment.left.start, 'var ');
  12530. exports.push(`${exportsName} as __moduleExports`);
  12531. exportDeclarations.push(
  12532. getDefaultExportDeclaration(exportsName, defaultIsModuleExports, HELPERS_NAME)
  12533. );
  12534. }
  12535. function getDefaultExportDeclaration(exportedExportsName, defaultIsModuleExports, HELPERS_NAME) {
  12536. return `export default ${
  12537. defaultIsModuleExports === true
  12538. ? exportedExportsName
  12539. : defaultIsModuleExports === false
  12540. ? `${exportedExportsName}.default`
  12541. : `/*@__PURE__*/${HELPERS_NAME}.getDefaultExportFromCjs(${exportedExportsName})`
  12542. }`;
  12543. }
  12544. function getExports(
  12545. magicString,
  12546. exports,
  12547. exportDeclarations,
  12548. moduleExportsAssignments,
  12549. exportsAssignmentsByName,
  12550. deconflictedExportNames,
  12551. topLevelAssignments,
  12552. moduleName,
  12553. exportsName,
  12554. exportedExportsName,
  12555. defineCompiledEsmExpressions,
  12556. HELPERS_NAME,
  12557. defaultIsModuleExports,
  12558. exportMode
  12559. ) {
  12560. let deconflictedDefaultExportName;
  12561. // Collect and rewrite module.exports assignments
  12562. for (const { left } of moduleExportsAssignments) {
  12563. magicString.overwrite(left.start, left.end, `${moduleName}.exports`);
  12564. }
  12565. // Collect and rewrite named exports
  12566. for (const [exportName, { nodes }] of exportsAssignmentsByName) {
  12567. const deconflicted = deconflictedExportNames[exportName];
  12568. let needsDeclaration = true;
  12569. for (const { node, type } of nodes) {
  12570. let replacement = `${deconflicted} = ${
  12571. exportMode === 'module' && type === 'module' ? `${moduleName}.exports` : exportsName
  12572. }.${exportName}`;
  12573. if (needsDeclaration && topLevelAssignments.has(node)) {
  12574. replacement = `var ${replacement}`;
  12575. needsDeclaration = false;
  12576. }
  12577. magicString.overwrite(node.start, node.left.end, replacement);
  12578. }
  12579. if (needsDeclaration) {
  12580. magicString.prepend(`var ${deconflicted};\n`);
  12581. }
  12582. if (exportName === 'default') {
  12583. deconflictedDefaultExportName = deconflicted;
  12584. } else {
  12585. exports.push(exportName === deconflicted ? exportName : `${deconflicted} as ${exportName}`);
  12586. }
  12587. }
  12588. const isRestorableCompiledEsm = replaceDefineCompiledEsmExpressionsAndGetIfRestorable(
  12589. defineCompiledEsmExpressions,
  12590. magicString,
  12591. exportMode,
  12592. moduleName,
  12593. exportsName
  12594. );
  12595. if (
  12596. defaultIsModuleExports === false ||
  12597. (defaultIsModuleExports === 'auto' &&
  12598. isRestorableCompiledEsm &&
  12599. moduleExportsAssignments.length === 0)
  12600. ) {
  12601. // If there is no deconflictedDefaultExportName, then we use the namespace as
  12602. // fallback because there can be no "default" property on the namespace
  12603. exports.push(`${deconflictedDefaultExportName || exportedExportsName} as default`);
  12604. } else if (
  12605. defaultIsModuleExports === true ||
  12606. (!isRestorableCompiledEsm && moduleExportsAssignments.length === 0)
  12607. ) {
  12608. exports.push(`${exportedExportsName} as default`);
  12609. } else {
  12610. exportDeclarations.push(
  12611. getDefaultExportDeclaration(exportedExportsName, defaultIsModuleExports, HELPERS_NAME)
  12612. );
  12613. }
  12614. }
  12615. function rewriteModuleExportsAssignments(magicString, moduleExportsAssignments, exportsName) {
  12616. for (const { left } of moduleExportsAssignments) {
  12617. magicString.overwrite(left.start, left.end, exportsName);
  12618. }
  12619. }
  12620. function replaceDefineCompiledEsmExpressionsAndGetIfRestorable(
  12621. defineCompiledEsmExpressions,
  12622. magicString,
  12623. exportMode,
  12624. moduleName,
  12625. exportsName
  12626. ) {
  12627. let isRestorableCompiledEsm = false;
  12628. for (const { node, type } of defineCompiledEsmExpressions) {
  12629. isRestorableCompiledEsm = true;
  12630. const moduleExportsExpression =
  12631. node.type === 'CallExpression' ? node.arguments[0] : node.left.object;
  12632. magicString.overwrite(
  12633. moduleExportsExpression.start,
  12634. moduleExportsExpression.end,
  12635. exportMode === 'module' && type === 'module' ? `${moduleName}.exports` : exportsName
  12636. );
  12637. }
  12638. return isRestorableCompiledEsm;
  12639. }
  12640. function isRequireExpression(node, scope) {
  12641. if (!node) return false;
  12642. if (node.type !== 'CallExpression') return false;
  12643. // Weird case of `require()` or `module.require()` without arguments
  12644. if (node.arguments.length === 0) return false;
  12645. return isRequire(node.callee, scope);
  12646. }
  12647. function isRequire(node, scope) {
  12648. return (
  12649. (node.type === 'Identifier' && node.name === 'require' && !scope.contains('require')) ||
  12650. (node.type === 'MemberExpression' && isModuleRequire(node, scope))
  12651. );
  12652. }
  12653. function isModuleRequire({ object, property }, scope) {
  12654. return (
  12655. object.type === 'Identifier' &&
  12656. object.name === 'module' &&
  12657. property.type === 'Identifier' &&
  12658. property.name === 'require' &&
  12659. !scope.contains('module')
  12660. );
  12661. }
  12662. function hasDynamicArguments(node) {
  12663. return (
  12664. node.arguments.length > 1 ||
  12665. (node.arguments[0].type !== 'Literal' &&
  12666. (node.arguments[0].type !== 'TemplateLiteral' || node.arguments[0].expressions.length > 0))
  12667. );
  12668. }
  12669. const reservedMethod = { resolve: true, cache: true, main: true };
  12670. function isNodeRequirePropertyAccess(parent) {
  12671. return parent && parent.property && reservedMethod[parent.property.name];
  12672. }
  12673. function getRequireStringArg(node) {
  12674. return node.arguments[0].type === 'Literal'
  12675. ? node.arguments[0].value
  12676. : node.arguments[0].quasis[0].value.cooked;
  12677. }
  12678. function getRequireHandlers() {
  12679. const requireExpressions = [];
  12680. function addRequireExpression(
  12681. sourceId,
  12682. node,
  12683. scope,
  12684. usesReturnValue,
  12685. isInsideTryBlock,
  12686. isInsideConditional,
  12687. toBeRemoved
  12688. ) {
  12689. requireExpressions.push({
  12690. sourceId,
  12691. node,
  12692. scope,
  12693. usesReturnValue,
  12694. isInsideTryBlock,
  12695. isInsideConditional,
  12696. toBeRemoved
  12697. });
  12698. }
  12699. async function rewriteRequireExpressionsAndGetImportBlock(
  12700. magicString,
  12701. topLevelDeclarations,
  12702. reassignedNames,
  12703. helpersName,
  12704. dynamicRequireName,
  12705. moduleName,
  12706. exportsName,
  12707. id,
  12708. exportMode,
  12709. resolveRequireSourcesAndUpdateMeta,
  12710. needsRequireWrapper,
  12711. isEsModule,
  12712. isDynamicRequireModulesEnabled,
  12713. getIgnoreTryCatchRequireStatementMode,
  12714. commonjsMeta
  12715. ) {
  12716. const imports = [];
  12717. imports.push(`import * as ${helpersName} from "${HELPERS_ID}"`);
  12718. if (dynamicRequireName) {
  12719. imports.push(
  12720. `import { ${
  12721. isDynamicRequireModulesEnabled ? CREATE_COMMONJS_REQUIRE_EXPORT : COMMONJS_REQUIRE_EXPORT
  12722. } as ${dynamicRequireName} } from "${DYNAMIC_MODULES_ID}"`
  12723. );
  12724. }
  12725. if (exportMode === 'module') {
  12726. imports.push(
  12727. `import { __module as ${moduleName} } from ${JSON.stringify(wrapId(id, MODULE_SUFFIX))}`,
  12728. `var ${exportsName} = ${moduleName}.exports`
  12729. );
  12730. } else if (exportMode === 'exports') {
  12731. imports.push(
  12732. `import { __exports as ${exportsName} } from ${JSON.stringify(wrapId(id, EXPORTS_SUFFIX))}`
  12733. );
  12734. }
  12735. const requiresBySource = collectSources(requireExpressions);
  12736. const requireTargets = await resolveRequireSourcesAndUpdateMeta(
  12737. id,
  12738. needsRequireWrapper ? IS_WRAPPED_COMMONJS : !isEsModule,
  12739. commonjsMeta,
  12740. Object.keys(requiresBySource).map((source) => {
  12741. return {
  12742. source,
  12743. isConditional: requiresBySource[source].every((require) => require.isInsideConditional)
  12744. };
  12745. })
  12746. );
  12747. processRequireExpressions(
  12748. imports,
  12749. requireTargets,
  12750. requiresBySource,
  12751. getIgnoreTryCatchRequireStatementMode,
  12752. magicString
  12753. );
  12754. return imports.length ? `${imports.join(';\n')};\n\n` : '';
  12755. }
  12756. return {
  12757. addRequireExpression,
  12758. rewriteRequireExpressionsAndGetImportBlock
  12759. };
  12760. }
  12761. function collectSources(requireExpressions) {
  12762. const requiresBySource = Object.create(null);
  12763. for (const requireExpression of requireExpressions) {
  12764. const { sourceId } = requireExpression;
  12765. if (!requiresBySource[sourceId]) {
  12766. requiresBySource[sourceId] = [];
  12767. }
  12768. const requires = requiresBySource[sourceId];
  12769. requires.push(requireExpression);
  12770. }
  12771. return requiresBySource;
  12772. }
  12773. function processRequireExpressions(
  12774. imports,
  12775. requireTargets,
  12776. requiresBySource,
  12777. getIgnoreTryCatchRequireStatementMode,
  12778. magicString
  12779. ) {
  12780. const generateRequireName = getGenerateRequireName();
  12781. for (const { source, id: resolvedId, isCommonJS } of requireTargets) {
  12782. const requires = requiresBySource[source];
  12783. const name = generateRequireName(requires);
  12784. let usesRequired = false;
  12785. let needsImport = false;
  12786. for (const { node, usesReturnValue, toBeRemoved, isInsideTryBlock } of requires) {
  12787. const { canConvertRequire, shouldRemoveRequire } =
  12788. isInsideTryBlock && isWrappedId(resolvedId, EXTERNAL_SUFFIX)
  12789. ? getIgnoreTryCatchRequireStatementMode(source)
  12790. : { canConvertRequire: true, shouldRemoveRequire: false };
  12791. if (shouldRemoveRequire) {
  12792. if (usesReturnValue) {
  12793. magicString.overwrite(node.start, node.end, 'undefined');
  12794. } else {
  12795. magicString.remove(toBeRemoved.start, toBeRemoved.end);
  12796. }
  12797. } else if (canConvertRequire) {
  12798. needsImport = true;
  12799. if (isCommonJS === IS_WRAPPED_COMMONJS) {
  12800. magicString.overwrite(node.start, node.end, `${name}()`);
  12801. } else if (usesReturnValue) {
  12802. usesRequired = true;
  12803. magicString.overwrite(node.start, node.end, name);
  12804. } else {
  12805. magicString.remove(toBeRemoved.start, toBeRemoved.end);
  12806. }
  12807. }
  12808. }
  12809. if (needsImport) {
  12810. if (isCommonJS === IS_WRAPPED_COMMONJS) {
  12811. imports.push(`import { __require as ${name} } from ${JSON.stringify(resolvedId)}`);
  12812. } else {
  12813. imports.push(`import ${usesRequired ? `${name} from ` : ''}${JSON.stringify(resolvedId)}`);
  12814. }
  12815. }
  12816. }
  12817. }
  12818. function getGenerateRequireName() {
  12819. let uid = 0;
  12820. return (requires) => {
  12821. let name;
  12822. const hasNameConflict = ({ scope }) => scope.contains(name);
  12823. do {
  12824. name = `require$$${uid}`;
  12825. uid += 1;
  12826. } while (requires.some(hasNameConflict));
  12827. return name;
  12828. };
  12829. }
  12830. /* eslint-disable no-param-reassign, no-shadow, no-underscore-dangle, no-continue */
  12831. const exportsPattern = /^(?:module\.)?exports(?:\.([a-zA-Z_$][a-zA-Z_$0-9]*))?$/;
  12832. const functionType = /^(?:FunctionDeclaration|FunctionExpression|ArrowFunctionExpression)$/;
  12833. // There are three different types of CommonJS modules, described by their
  12834. // "exportMode":
  12835. // - exports: Only assignments to (module.)exports properties
  12836. // - replace: A single assignment to module.exports itself
  12837. // - module: Anything else
  12838. // Special cases:
  12839. // - usesRequireWrapper
  12840. // - isWrapped
  12841. async function transformCommonjs(
  12842. parse,
  12843. code,
  12844. id,
  12845. isEsModule,
  12846. ignoreGlobal,
  12847. ignoreRequire,
  12848. ignoreDynamicRequires,
  12849. getIgnoreTryCatchRequireStatementMode,
  12850. sourceMap,
  12851. isDynamicRequireModulesEnabled,
  12852. dynamicRequireModules,
  12853. commonDir,
  12854. astCache,
  12855. defaultIsModuleExports,
  12856. needsRequireWrapper,
  12857. resolveRequireSourcesAndUpdateMeta,
  12858. isRequired,
  12859. checkDynamicRequire,
  12860. commonjsMeta
  12861. ) {
  12862. const ast = astCache || tryParse(parse, code, id);
  12863. const magicString = new MagicString(code);
  12864. const uses = {
  12865. module: false,
  12866. exports: false,
  12867. global: false,
  12868. require: false
  12869. };
  12870. const virtualDynamicRequirePath =
  12871. isDynamicRequireModulesEnabled && getVirtualPathForDynamicRequirePath(dirname$1(id), commonDir);
  12872. let scope = attachScopes(ast, 'scope');
  12873. let lexicalDepth = 0;
  12874. let programDepth = 0;
  12875. let classBodyDepth = 0;
  12876. let currentTryBlockEnd = null;
  12877. let shouldWrap = false;
  12878. const globals = new Set();
  12879. // A conditionalNode is a node for which execution is not guaranteed. If such a node is a require
  12880. // or contains nested requires, those should be handled as function calls unless there is an
  12881. // unconditional require elsewhere.
  12882. let currentConditionalNodeEnd = null;
  12883. const conditionalNodes = new Set();
  12884. const { addRequireExpression, rewriteRequireExpressionsAndGetImportBlock } = getRequireHandlers();
  12885. // See which names are assigned to. This is necessary to prevent
  12886. // illegally replacing `var foo = require('foo')` with `import foo from 'foo'`,
  12887. // where `foo` is later reassigned. (This happens in the wild. CommonJS, sigh)
  12888. const reassignedNames = new Set();
  12889. const topLevelDeclarations = [];
  12890. const skippedNodes = new Set();
  12891. const moduleAccessScopes = new Set([scope]);
  12892. const exportsAccessScopes = new Set([scope]);
  12893. const moduleExportsAssignments = [];
  12894. let firstTopLevelModuleExportsAssignment = null;
  12895. const exportsAssignmentsByName = new Map();
  12896. const topLevelAssignments = new Set();
  12897. const topLevelDefineCompiledEsmExpressions = [];
  12898. const replacedGlobal = [];
  12899. const replacedDynamicRequires = [];
  12900. const importedVariables = new Set();
  12901. const indentExclusionRanges = [];
  12902. walk$3(ast, {
  12903. enter(node, parent) {
  12904. if (skippedNodes.has(node)) {
  12905. this.skip();
  12906. return;
  12907. }
  12908. if (currentTryBlockEnd !== null && node.start > currentTryBlockEnd) {
  12909. currentTryBlockEnd = null;
  12910. }
  12911. if (currentConditionalNodeEnd !== null && node.start > currentConditionalNodeEnd) {
  12912. currentConditionalNodeEnd = null;
  12913. }
  12914. if (currentConditionalNodeEnd === null && conditionalNodes.has(node)) {
  12915. currentConditionalNodeEnd = node.end;
  12916. }
  12917. programDepth += 1;
  12918. if (node.scope) ({ scope } = node);
  12919. if (functionType.test(node.type)) lexicalDepth += 1;
  12920. if (sourceMap) {
  12921. magicString.addSourcemapLocation(node.start);
  12922. magicString.addSourcemapLocation(node.end);
  12923. }
  12924. // eslint-disable-next-line default-case
  12925. switch (node.type) {
  12926. case 'AssignmentExpression':
  12927. if (node.left.type === 'MemberExpression') {
  12928. const flattened = getKeypath(node.left);
  12929. if (!flattened || scope.contains(flattened.name)) return;
  12930. const exportsPatternMatch = exportsPattern.exec(flattened.keypath);
  12931. if (!exportsPatternMatch || flattened.keypath === 'exports') return;
  12932. const [, exportName] = exportsPatternMatch;
  12933. uses[flattened.name] = true;
  12934. // we're dealing with `module.exports = ...` or `[module.]exports.foo = ...` –
  12935. if (flattened.keypath === 'module.exports') {
  12936. moduleExportsAssignments.push(node);
  12937. if (programDepth > 3) {
  12938. moduleAccessScopes.add(scope);
  12939. } else if (!firstTopLevelModuleExportsAssignment) {
  12940. firstTopLevelModuleExportsAssignment = node;
  12941. }
  12942. } else if (exportName === KEY_COMPILED_ESM) {
  12943. if (programDepth > 3) {
  12944. shouldWrap = true;
  12945. } else {
  12946. // The "type" is either "module" or "exports" to discern
  12947. // assignments to module.exports vs exports if needed
  12948. topLevelDefineCompiledEsmExpressions.push({ node, type: flattened.name });
  12949. }
  12950. } else {
  12951. const exportsAssignments = exportsAssignmentsByName.get(exportName) || {
  12952. nodes: [],
  12953. scopes: new Set()
  12954. };
  12955. exportsAssignments.nodes.push({ node, type: flattened.name });
  12956. exportsAssignments.scopes.add(scope);
  12957. exportsAccessScopes.add(scope);
  12958. exportsAssignmentsByName.set(exportName, exportsAssignments);
  12959. if (programDepth <= 3) {
  12960. topLevelAssignments.add(node);
  12961. }
  12962. }
  12963. skippedNodes.add(node.left);
  12964. } else {
  12965. for (const name of extractAssignedNames(node.left)) {
  12966. reassignedNames.add(name);
  12967. }
  12968. }
  12969. return;
  12970. case 'CallExpression': {
  12971. const defineCompiledEsmType = getDefineCompiledEsmType(node);
  12972. if (defineCompiledEsmType) {
  12973. if (programDepth === 3 && parent.type === 'ExpressionStatement') {
  12974. // skip special handling for [module.]exports until we know we render this
  12975. skippedNodes.add(node.arguments[0]);
  12976. topLevelDefineCompiledEsmExpressions.push({ node, type: defineCompiledEsmType });
  12977. } else {
  12978. shouldWrap = true;
  12979. }
  12980. return;
  12981. }
  12982. // Transform require.resolve
  12983. if (
  12984. isDynamicRequireModulesEnabled &&
  12985. node.callee.object &&
  12986. isRequire(node.callee.object, scope) &&
  12987. node.callee.property.name === 'resolve'
  12988. ) {
  12989. checkDynamicRequire(node.start);
  12990. uses.require = true;
  12991. const requireNode = node.callee.object;
  12992. replacedDynamicRequires.push(requireNode);
  12993. skippedNodes.add(node.callee);
  12994. return;
  12995. }
  12996. if (!isRequireExpression(node, scope)) {
  12997. const keypath = getKeypath(node.callee);
  12998. if (keypath && importedVariables.has(keypath.name)) {
  12999. // Heuristic to deoptimize requires after a required function has been called
  13000. currentConditionalNodeEnd = Infinity;
  13001. }
  13002. return;
  13003. }
  13004. skippedNodes.add(node.callee);
  13005. uses.require = true;
  13006. if (hasDynamicArguments(node)) {
  13007. if (isDynamicRequireModulesEnabled) {
  13008. checkDynamicRequire(node.start);
  13009. }
  13010. if (!ignoreDynamicRequires) {
  13011. replacedDynamicRequires.push(node.callee);
  13012. }
  13013. return;
  13014. }
  13015. const requireStringArg = getRequireStringArg(node);
  13016. if (!ignoreRequire(requireStringArg)) {
  13017. const usesReturnValue = parent.type !== 'ExpressionStatement';
  13018. const toBeRemoved =
  13019. parent.type === 'ExpressionStatement' &&
  13020. (!currentConditionalNodeEnd ||
  13021. // We should completely remove requires directly in a try-catch
  13022. // so that Rollup can remove up the try-catch
  13023. (currentTryBlockEnd !== null && currentTryBlockEnd < currentConditionalNodeEnd))
  13024. ? parent
  13025. : node;
  13026. addRequireExpression(
  13027. requireStringArg,
  13028. node,
  13029. scope,
  13030. usesReturnValue,
  13031. currentTryBlockEnd !== null,
  13032. currentConditionalNodeEnd !== null,
  13033. toBeRemoved
  13034. );
  13035. if (parent.type === 'VariableDeclarator' && parent.id.type === 'Identifier') {
  13036. for (const name of extractAssignedNames(parent.id)) {
  13037. importedVariables.add(name);
  13038. }
  13039. }
  13040. }
  13041. return;
  13042. }
  13043. case 'ClassBody':
  13044. classBodyDepth += 1;
  13045. return;
  13046. case 'ConditionalExpression':
  13047. case 'IfStatement':
  13048. // skip dead branches
  13049. if (isFalsy(node.test)) {
  13050. skippedNodes.add(node.consequent);
  13051. } else if (isTruthy(node.test)) {
  13052. if (node.alternate) {
  13053. skippedNodes.add(node.alternate);
  13054. }
  13055. } else {
  13056. conditionalNodes.add(node.consequent);
  13057. if (node.alternate) {
  13058. conditionalNodes.add(node.alternate);
  13059. }
  13060. }
  13061. return;
  13062. case 'ArrowFunctionExpression':
  13063. case 'FunctionDeclaration':
  13064. case 'FunctionExpression':
  13065. // requires in functions should be conditional unless it is an IIFE
  13066. if (
  13067. currentConditionalNodeEnd === null &&
  13068. !(parent.type === 'CallExpression' && parent.callee === node)
  13069. ) {
  13070. currentConditionalNodeEnd = node.end;
  13071. }
  13072. return;
  13073. case 'Identifier': {
  13074. const { name } = node;
  13075. if (
  13076. !isReference(node, parent) ||
  13077. scope.contains(name) ||
  13078. (parent.type === 'PropertyDefinition' && parent.key === node)
  13079. )
  13080. return;
  13081. switch (name) {
  13082. case 'require':
  13083. uses.require = true;
  13084. if (isNodeRequirePropertyAccess(parent)) {
  13085. return;
  13086. }
  13087. if (!ignoreDynamicRequires) {
  13088. if (isShorthandProperty(parent)) {
  13089. // as key and value are the same object, isReference regards
  13090. // both as references, so we need to skip now
  13091. skippedNodes.add(parent.value);
  13092. magicString.prependRight(node.start, 'require: ');
  13093. }
  13094. replacedDynamicRequires.push(node);
  13095. }
  13096. return;
  13097. case 'module':
  13098. case 'exports':
  13099. shouldWrap = true;
  13100. uses[name] = true;
  13101. return;
  13102. case 'global':
  13103. uses.global = true;
  13104. if (!ignoreGlobal) {
  13105. replacedGlobal.push(node);
  13106. }
  13107. return;
  13108. case 'define':
  13109. magicString.overwrite(node.start, node.end, 'undefined', {
  13110. storeName: true
  13111. });
  13112. return;
  13113. default:
  13114. globals.add(name);
  13115. return;
  13116. }
  13117. }
  13118. case 'LogicalExpression':
  13119. // skip dead branches
  13120. if (node.operator === '&&') {
  13121. if (isFalsy(node.left)) {
  13122. skippedNodes.add(node.right);
  13123. } else if (!isTruthy(node.left)) {
  13124. conditionalNodes.add(node.right);
  13125. }
  13126. } else if (node.operator === '||') {
  13127. if (isTruthy(node.left)) {
  13128. skippedNodes.add(node.right);
  13129. } else if (!isFalsy(node.left)) {
  13130. conditionalNodes.add(node.right);
  13131. }
  13132. }
  13133. return;
  13134. case 'MemberExpression':
  13135. if (!isDynamicRequireModulesEnabled && isModuleRequire(node, scope)) {
  13136. uses.require = true;
  13137. replacedDynamicRequires.push(node);
  13138. skippedNodes.add(node.object);
  13139. skippedNodes.add(node.property);
  13140. }
  13141. return;
  13142. case 'ReturnStatement':
  13143. // if top-level return, we need to wrap it
  13144. if (lexicalDepth === 0) {
  13145. shouldWrap = true;
  13146. }
  13147. return;
  13148. case 'ThisExpression':
  13149. // rewrite top-level `this` as `commonjsHelpers.commonjsGlobal`
  13150. if (lexicalDepth === 0 && !classBodyDepth) {
  13151. uses.global = true;
  13152. if (!ignoreGlobal) {
  13153. replacedGlobal.push(node);
  13154. }
  13155. }
  13156. return;
  13157. case 'TryStatement':
  13158. if (currentTryBlockEnd === null) {
  13159. currentTryBlockEnd = node.block.end;
  13160. }
  13161. if (currentConditionalNodeEnd === null) {
  13162. currentConditionalNodeEnd = node.end;
  13163. }
  13164. return;
  13165. case 'UnaryExpression':
  13166. // rewrite `typeof module`, `typeof module.exports` and `typeof exports` (https://github.com/rollup/rollup-plugin-commonjs/issues/151)
  13167. if (node.operator === 'typeof') {
  13168. const flattened = getKeypath(node.argument);
  13169. if (!flattened) return;
  13170. if (scope.contains(flattened.name)) return;
  13171. if (
  13172. !isEsModule &&
  13173. (flattened.keypath === 'module.exports' ||
  13174. flattened.keypath === 'module' ||
  13175. flattened.keypath === 'exports')
  13176. ) {
  13177. magicString.overwrite(node.start, node.end, `'object'`, {
  13178. storeName: false
  13179. });
  13180. }
  13181. }
  13182. return;
  13183. case 'VariableDeclaration':
  13184. if (!scope.parent) {
  13185. topLevelDeclarations.push(node);
  13186. }
  13187. return;
  13188. case 'TemplateElement':
  13189. if (node.value.raw.includes('\n')) {
  13190. indentExclusionRanges.push([node.start, node.end]);
  13191. }
  13192. }
  13193. },
  13194. leave(node) {
  13195. programDepth -= 1;
  13196. if (node.scope) scope = scope.parent;
  13197. if (functionType.test(node.type)) lexicalDepth -= 1;
  13198. if (node.type === 'ClassBody') classBodyDepth -= 1;
  13199. }
  13200. });
  13201. const nameBase = getName(id);
  13202. const exportsName = deconflict([...exportsAccessScopes], globals, nameBase);
  13203. const moduleName = deconflict([...moduleAccessScopes], globals, `${nameBase}Module`);
  13204. const requireName = deconflict([scope], globals, `require${capitalize(nameBase)}`);
  13205. const isRequiredName = deconflict([scope], globals, `hasRequired${capitalize(nameBase)}`);
  13206. const helpersName = deconflict([scope], globals, 'commonjsHelpers');
  13207. const dynamicRequireName =
  13208. replacedDynamicRequires.length > 0 &&
  13209. deconflict(
  13210. [scope],
  13211. globals,
  13212. isDynamicRequireModulesEnabled ? CREATE_COMMONJS_REQUIRE_EXPORT : COMMONJS_REQUIRE_EXPORT
  13213. );
  13214. const deconflictedExportNames = Object.create(null);
  13215. for (const [exportName, { scopes }] of exportsAssignmentsByName) {
  13216. deconflictedExportNames[exportName] = deconflict([...scopes], globals, exportName);
  13217. }
  13218. for (const node of replacedGlobal) {
  13219. magicString.overwrite(node.start, node.end, `${helpersName}.commonjsGlobal`, {
  13220. storeName: true
  13221. });
  13222. }
  13223. for (const node of replacedDynamicRequires) {
  13224. magicString.overwrite(
  13225. node.start,
  13226. node.end,
  13227. isDynamicRequireModulesEnabled
  13228. ? `${dynamicRequireName}(${JSON.stringify(virtualDynamicRequirePath)})`
  13229. : dynamicRequireName,
  13230. {
  13231. contentOnly: true,
  13232. storeName: true
  13233. }
  13234. );
  13235. }
  13236. // We cannot wrap ES/mixed modules
  13237. shouldWrap = !isEsModule && (shouldWrap || (uses.exports && moduleExportsAssignments.length > 0));
  13238. if (
  13239. !(
  13240. shouldWrap ||
  13241. isRequired ||
  13242. needsRequireWrapper ||
  13243. uses.module ||
  13244. uses.exports ||
  13245. uses.require ||
  13246. topLevelDefineCompiledEsmExpressions.length > 0
  13247. ) &&
  13248. (ignoreGlobal || !uses.global)
  13249. ) {
  13250. return { meta: { commonjs: { isCommonJS: false } } };
  13251. }
  13252. let leadingComment = '';
  13253. if (code.startsWith('/*')) {
  13254. const commentEnd = code.indexOf('*/', 2) + 2;
  13255. leadingComment = `${code.slice(0, commentEnd)}\n`;
  13256. magicString.remove(0, commentEnd).trim();
  13257. }
  13258. let shebang = '';
  13259. if (code.startsWith('#!')) {
  13260. const shebangEndPosition = code.indexOf('\n') + 1;
  13261. shebang = code.slice(0, shebangEndPosition);
  13262. magicString.remove(0, shebangEndPosition).trim();
  13263. }
  13264. const exportMode = isEsModule
  13265. ? 'none'
  13266. : shouldWrap
  13267. ? uses.module
  13268. ? 'module'
  13269. : 'exports'
  13270. : firstTopLevelModuleExportsAssignment
  13271. ? exportsAssignmentsByName.size === 0 && topLevelDefineCompiledEsmExpressions.length === 0
  13272. ? 'replace'
  13273. : 'module'
  13274. : moduleExportsAssignments.length === 0
  13275. ? 'exports'
  13276. : 'module';
  13277. const exportedExportsName =
  13278. exportMode === 'module' ? deconflict([], globals, `${nameBase}Exports`) : exportsName;
  13279. const importBlock = await rewriteRequireExpressionsAndGetImportBlock(
  13280. magicString,
  13281. topLevelDeclarations,
  13282. reassignedNames,
  13283. helpersName,
  13284. dynamicRequireName,
  13285. moduleName,
  13286. exportsName,
  13287. id,
  13288. exportMode,
  13289. resolveRequireSourcesAndUpdateMeta,
  13290. needsRequireWrapper,
  13291. isEsModule,
  13292. isDynamicRequireModulesEnabled,
  13293. getIgnoreTryCatchRequireStatementMode,
  13294. commonjsMeta
  13295. );
  13296. const usesRequireWrapper = commonjsMeta.isCommonJS === IS_WRAPPED_COMMONJS;
  13297. const exportBlock = isEsModule
  13298. ? ''
  13299. : rewriteExportsAndGetExportsBlock(
  13300. magicString,
  13301. moduleName,
  13302. exportsName,
  13303. exportedExportsName,
  13304. shouldWrap,
  13305. moduleExportsAssignments,
  13306. firstTopLevelModuleExportsAssignment,
  13307. exportsAssignmentsByName,
  13308. topLevelAssignments,
  13309. topLevelDefineCompiledEsmExpressions,
  13310. deconflictedExportNames,
  13311. code,
  13312. helpersName,
  13313. exportMode,
  13314. defaultIsModuleExports,
  13315. usesRequireWrapper,
  13316. requireName
  13317. );
  13318. if (shouldWrap) {
  13319. wrapCode(magicString, uses, moduleName, exportsName, indentExclusionRanges);
  13320. }
  13321. if (usesRequireWrapper) {
  13322. magicString.trim().indent('\t', {
  13323. exclude: indentExclusionRanges
  13324. });
  13325. const exported = exportMode === 'module' ? `${moduleName}.exports` : exportsName;
  13326. magicString.prepend(
  13327. `var ${isRequiredName};
  13328. function ${requireName} () {
  13329. \tif (${isRequiredName}) return ${exported};
  13330. \t${isRequiredName} = 1;
  13331. `
  13332. ).append(`
  13333. \treturn ${exported};
  13334. }`);
  13335. if (exportMode === 'replace') {
  13336. magicString.prepend(`var ${exportsName};\n`);
  13337. }
  13338. }
  13339. magicString
  13340. .trim()
  13341. .prepend(shebang + leadingComment + importBlock)
  13342. .append(exportBlock);
  13343. return {
  13344. code: magicString.toString(),
  13345. map: sourceMap ? magicString.generateMap() : null,
  13346. syntheticNamedExports: isEsModule || usesRequireWrapper ? false : '__moduleExports',
  13347. meta: { commonjs: { ...commonjsMeta, shebang } }
  13348. };
  13349. }
  13350. const PLUGIN_NAME = 'commonjs';
  13351. function commonjs(options = {}) {
  13352. const {
  13353. ignoreGlobal,
  13354. ignoreDynamicRequires,
  13355. requireReturnsDefault: requireReturnsDefaultOption,
  13356. defaultIsModuleExports: defaultIsModuleExportsOption,
  13357. esmExternals
  13358. } = options;
  13359. const extensions = options.extensions || ['.js'];
  13360. const filter = createFilter$1(options.include, options.exclude);
  13361. const isPossibleCjsId = (id) => {
  13362. const extName = extname(id);
  13363. return extName === '.cjs' || (extensions.includes(extName) && filter(id));
  13364. };
  13365. const { strictRequiresFilter, detectCyclesAndConditional } = getStrictRequiresFilter(options);
  13366. const getRequireReturnsDefault =
  13367. typeof requireReturnsDefaultOption === 'function'
  13368. ? requireReturnsDefaultOption
  13369. : () => requireReturnsDefaultOption;
  13370. let esmExternalIds;
  13371. const isEsmExternal =
  13372. typeof esmExternals === 'function'
  13373. ? esmExternals
  13374. : Array.isArray(esmExternals)
  13375. ? ((esmExternalIds = new Set(esmExternals)), (id) => esmExternalIds.has(id))
  13376. : () => esmExternals;
  13377. const getDefaultIsModuleExports =
  13378. typeof defaultIsModuleExportsOption === 'function'
  13379. ? defaultIsModuleExportsOption
  13380. : () =>
  13381. typeof defaultIsModuleExportsOption === 'boolean' ? defaultIsModuleExportsOption : 'auto';
  13382. const dynamicRequireRoot =
  13383. typeof options.dynamicRequireRoot === 'string'
  13384. ? resolve$3(options.dynamicRequireRoot)
  13385. : process.cwd();
  13386. const { commonDir, dynamicRequireModules } = getDynamicRequireModules(
  13387. options.dynamicRequireTargets,
  13388. dynamicRequireRoot
  13389. );
  13390. const isDynamicRequireModulesEnabled = dynamicRequireModules.size > 0;
  13391. const ignoreRequire =
  13392. typeof options.ignore === 'function'
  13393. ? options.ignore
  13394. : Array.isArray(options.ignore)
  13395. ? (id) => options.ignore.includes(id)
  13396. : () => false;
  13397. const getIgnoreTryCatchRequireStatementMode = (id) => {
  13398. const mode =
  13399. typeof options.ignoreTryCatch === 'function'
  13400. ? options.ignoreTryCatch(id)
  13401. : Array.isArray(options.ignoreTryCatch)
  13402. ? options.ignoreTryCatch.includes(id)
  13403. : typeof options.ignoreTryCatch !== 'undefined'
  13404. ? options.ignoreTryCatch
  13405. : true;
  13406. return {
  13407. canConvertRequire: mode !== 'remove' && mode !== true,
  13408. shouldRemoveRequire: mode === 'remove'
  13409. };
  13410. };
  13411. const { currentlyResolving, resolveId } = getResolveId(extensions, isPossibleCjsId);
  13412. const sourceMap = options.sourceMap !== false;
  13413. // Initialized in buildStart
  13414. let requireResolver;
  13415. function transformAndCheckExports(code, id) {
  13416. const normalizedId = normalizePathSlashes(id);
  13417. const { isEsModule, hasDefaultExport, hasNamedExports, ast } = analyzeTopLevelStatements(
  13418. this.parse,
  13419. code,
  13420. id
  13421. );
  13422. const commonjsMeta = this.getModuleInfo(id).meta.commonjs || {};
  13423. if (hasDefaultExport) {
  13424. commonjsMeta.hasDefaultExport = true;
  13425. }
  13426. if (hasNamedExports) {
  13427. commonjsMeta.hasNamedExports = true;
  13428. }
  13429. if (
  13430. !dynamicRequireModules.has(normalizedId) &&
  13431. (!(hasCjsKeywords(code, ignoreGlobal) || requireResolver.isRequiredId(id)) ||
  13432. (isEsModule && !options.transformMixedEsModules))
  13433. ) {
  13434. commonjsMeta.isCommonJS = false;
  13435. return { meta: { commonjs: commonjsMeta } };
  13436. }
  13437. const needsRequireWrapper =
  13438. !isEsModule && (dynamicRequireModules.has(normalizedId) || strictRequiresFilter(id));
  13439. const checkDynamicRequire = (position) => {
  13440. const normalizedDynamicRequireRoot = normalizePathSlashes(dynamicRequireRoot);
  13441. if (normalizedId.indexOf(normalizedDynamicRequireRoot) !== 0) {
  13442. this.error(
  13443. {
  13444. code: 'DYNAMIC_REQUIRE_OUTSIDE_ROOT',
  13445. normalizedId,
  13446. normalizedDynamicRequireRoot,
  13447. message: `"${normalizedId}" contains dynamic require statements but it is not within the current dynamicRequireRoot "${normalizedDynamicRequireRoot}". You should set dynamicRequireRoot to "${dirname$1(
  13448. normalizedId
  13449. )}" or one of its parent directories.`
  13450. },
  13451. position
  13452. );
  13453. }
  13454. };
  13455. return transformCommonjs(
  13456. this.parse,
  13457. code,
  13458. id,
  13459. isEsModule,
  13460. ignoreGlobal || isEsModule,
  13461. ignoreRequire,
  13462. ignoreDynamicRequires && !isDynamicRequireModulesEnabled,
  13463. getIgnoreTryCatchRequireStatementMode,
  13464. sourceMap,
  13465. isDynamicRequireModulesEnabled,
  13466. dynamicRequireModules,
  13467. commonDir,
  13468. ast,
  13469. getDefaultIsModuleExports(id),
  13470. needsRequireWrapper,
  13471. requireResolver.resolveRequireSourcesAndUpdateMeta(this),
  13472. requireResolver.isRequiredId(id),
  13473. checkDynamicRequire,
  13474. commonjsMeta
  13475. );
  13476. }
  13477. return {
  13478. name: PLUGIN_NAME,
  13479. version: version$2,
  13480. options(rawOptions) {
  13481. // We inject the resolver in the beginning so that "catch-all-resolver" like node-resolver
  13482. // do not prevent our plugin from resolving entry points ot proxies.
  13483. const plugins = Array.isArray(rawOptions.plugins)
  13484. ? [...rawOptions.plugins]
  13485. : rawOptions.plugins
  13486. ? [rawOptions.plugins]
  13487. : [];
  13488. plugins.unshift({
  13489. name: 'commonjs--resolver',
  13490. resolveId
  13491. });
  13492. return { ...rawOptions, plugins };
  13493. },
  13494. buildStart({ plugins }) {
  13495. validateVersion(this.meta.rollupVersion, peerDependencies.rollup, 'rollup');
  13496. const nodeResolve = plugins.find(({ name }) => name === 'node-resolve');
  13497. if (nodeResolve) {
  13498. validateVersion(nodeResolve.version, '^13.0.6', '@rollup/plugin-node-resolve');
  13499. }
  13500. if (options.namedExports != null) {
  13501. this.warn(
  13502. 'The namedExports option from "@rollup/plugin-commonjs" is deprecated. Named exports are now handled automatically.'
  13503. );
  13504. }
  13505. requireResolver = getRequireResolver(
  13506. extensions,
  13507. detectCyclesAndConditional,
  13508. currentlyResolving
  13509. );
  13510. },
  13511. buildEnd() {
  13512. if (options.strictRequires === 'debug') {
  13513. const wrappedIds = requireResolver.getWrappedIds();
  13514. if (wrappedIds.length) {
  13515. this.warn({
  13516. code: 'WRAPPED_IDS',
  13517. ids: wrappedIds,
  13518. message: `The commonjs plugin automatically wrapped the following files:\n[\n${wrappedIds
  13519. .map((id) => `\t${JSON.stringify(relative$1(process.cwd(), id))}`)
  13520. .join(',\n')}\n]`
  13521. });
  13522. } else {
  13523. this.warn({
  13524. code: 'WRAPPED_IDS',
  13525. ids: wrappedIds,
  13526. message: 'The commonjs plugin did not wrap any files.'
  13527. });
  13528. }
  13529. }
  13530. },
  13531. load(id) {
  13532. if (id === HELPERS_ID) {
  13533. return getHelpersModule();
  13534. }
  13535. if (isWrappedId(id, MODULE_SUFFIX)) {
  13536. const name = getName(unwrapId(id, MODULE_SUFFIX));
  13537. return {
  13538. code: `var ${name} = {exports: {}}; export {${name} as __module}`,
  13539. meta: { commonjs: { isCommonJS: false } }
  13540. };
  13541. }
  13542. if (isWrappedId(id, EXPORTS_SUFFIX)) {
  13543. const name = getName(unwrapId(id, EXPORTS_SUFFIX));
  13544. return {
  13545. code: `var ${name} = {}; export {${name} as __exports}`,
  13546. meta: { commonjs: { isCommonJS: false } }
  13547. };
  13548. }
  13549. if (isWrappedId(id, EXTERNAL_SUFFIX)) {
  13550. const actualId = unwrapId(id, EXTERNAL_SUFFIX);
  13551. return getUnknownRequireProxy(
  13552. actualId,
  13553. isEsmExternal(actualId) ? getRequireReturnsDefault(actualId) : true
  13554. );
  13555. }
  13556. // entry suffix is just appended to not mess up relative external resolution
  13557. if (id.endsWith(ENTRY_SUFFIX)) {
  13558. const acutalId = id.slice(0, -ENTRY_SUFFIX.length);
  13559. const {
  13560. meta: { commonjs: commonjsMeta }
  13561. } = this.getModuleInfo(acutalId);
  13562. const shebang = commonjsMeta?.shebang ?? '';
  13563. return getEntryProxy(
  13564. acutalId,
  13565. getDefaultIsModuleExports(acutalId),
  13566. this.getModuleInfo,
  13567. shebang
  13568. );
  13569. }
  13570. if (isWrappedId(id, ES_IMPORT_SUFFIX)) {
  13571. const actualId = unwrapId(id, ES_IMPORT_SUFFIX);
  13572. return getEsImportProxy(actualId, getDefaultIsModuleExports(actualId));
  13573. }
  13574. if (id === DYNAMIC_MODULES_ID) {
  13575. return getDynamicModuleRegistry(
  13576. isDynamicRequireModulesEnabled,
  13577. dynamicRequireModules,
  13578. commonDir,
  13579. ignoreDynamicRequires
  13580. );
  13581. }
  13582. if (isWrappedId(id, PROXY_SUFFIX)) {
  13583. const actualId = unwrapId(id, PROXY_SUFFIX);
  13584. return getStaticRequireProxy(actualId, getRequireReturnsDefault(actualId), this.load);
  13585. }
  13586. return null;
  13587. },
  13588. shouldTransformCachedModule(...args) {
  13589. return requireResolver.shouldTransformCachedModule.call(this, ...args);
  13590. },
  13591. transform(code, id) {
  13592. if (!isPossibleCjsId(id)) return null;
  13593. try {
  13594. return transformAndCheckExports.call(this, code, id);
  13595. } catch (err) {
  13596. return this.error(err, err.pos);
  13597. }
  13598. }
  13599. };
  13600. }
  13601. // Matches the scheme of a URL, eg "http://"
  13602. const schemeRegex = /^[\w+.-]+:\/\//;
  13603. /**
  13604. * Matches the parts of a URL:
  13605. * 1. Scheme, including ":", guaranteed.
  13606. * 2. User/password, including "@", optional.
  13607. * 3. Host, guaranteed.
  13608. * 4. Port, including ":", optional.
  13609. * 5. Path, including "/", optional.
  13610. * 6. Query, including "?", optional.
  13611. * 7. Hash, including "#", optional.
  13612. */
  13613. const urlRegex = /^([\w+.-]+:)\/\/([^@/#?]*@)?([^:/#?]*)(:\d+)?(\/[^#?]*)?(\?[^#]*)?(#.*)?/;
  13614. /**
  13615. * File URLs are weird. They dont' need the regular `//` in the scheme, they may or may not start
  13616. * with a leading `/`, they can have a domain (but only if they don't start with a Windows drive).
  13617. *
  13618. * 1. Host, optional.
  13619. * 2. Path, which may include "/", guaranteed.
  13620. * 3. Query, including "?", optional.
  13621. * 4. Hash, including "#", optional.
  13622. */
  13623. const fileRegex = /^file:(?:\/\/((?![a-z]:)[^/#?]*)?)?(\/?[^#?]*)(\?[^#]*)?(#.*)?/i;
  13624. function isAbsoluteUrl(input) {
  13625. return schemeRegex.test(input);
  13626. }
  13627. function isSchemeRelativeUrl(input) {
  13628. return input.startsWith('//');
  13629. }
  13630. function isAbsolutePath(input) {
  13631. return input.startsWith('/');
  13632. }
  13633. function isFileUrl(input) {
  13634. return input.startsWith('file:');
  13635. }
  13636. function isRelative(input) {
  13637. return /^[.?#]/.test(input);
  13638. }
  13639. function parseAbsoluteUrl(input) {
  13640. const match = urlRegex.exec(input);
  13641. return makeUrl(match[1], match[2] || '', match[3], match[4] || '', match[5] || '/', match[6] || '', match[7] || '');
  13642. }
  13643. function parseFileUrl(input) {
  13644. const match = fileRegex.exec(input);
  13645. const path = match[2];
  13646. return makeUrl('file:', '', match[1] || '', '', isAbsolutePath(path) ? path : '/' + path, match[3] || '', match[4] || '');
  13647. }
  13648. function makeUrl(scheme, user, host, port, path, query, hash) {
  13649. return {
  13650. scheme,
  13651. user,
  13652. host,
  13653. port,
  13654. path,
  13655. query,
  13656. hash,
  13657. type: 7 /* Absolute */,
  13658. };
  13659. }
  13660. function parseUrl$3(input) {
  13661. if (isSchemeRelativeUrl(input)) {
  13662. const url = parseAbsoluteUrl('http:' + input);
  13663. url.scheme = '';
  13664. url.type = 6 /* SchemeRelative */;
  13665. return url;
  13666. }
  13667. if (isAbsolutePath(input)) {
  13668. const url = parseAbsoluteUrl('http://foo.com' + input);
  13669. url.scheme = '';
  13670. url.host = '';
  13671. url.type = 5 /* AbsolutePath */;
  13672. return url;
  13673. }
  13674. if (isFileUrl(input))
  13675. return parseFileUrl(input);
  13676. if (isAbsoluteUrl(input))
  13677. return parseAbsoluteUrl(input);
  13678. const url = parseAbsoluteUrl('http://foo.com/' + input);
  13679. url.scheme = '';
  13680. url.host = '';
  13681. url.type = input
  13682. ? input.startsWith('?')
  13683. ? 3 /* Query */
  13684. : input.startsWith('#')
  13685. ? 2 /* Hash */
  13686. : 4 /* RelativePath */
  13687. : 1 /* Empty */;
  13688. return url;
  13689. }
  13690. function stripPathFilename(path) {
  13691. // If a path ends with a parent directory "..", then it's a relative path with excess parent
  13692. // paths. It's not a file, so we can't strip it.
  13693. if (path.endsWith('/..'))
  13694. return path;
  13695. const index = path.lastIndexOf('/');
  13696. return path.slice(0, index + 1);
  13697. }
  13698. function mergePaths(url, base) {
  13699. normalizePath$4(base, base.type);
  13700. // If the path is just a "/", then it was an empty path to begin with (remember, we're a relative
  13701. // path).
  13702. if (url.path === '/') {
  13703. url.path = base.path;
  13704. }
  13705. else {
  13706. // Resolution happens relative to the base path's directory, not the file.
  13707. url.path = stripPathFilename(base.path) + url.path;
  13708. }
  13709. }
  13710. /**
  13711. * The path can have empty directories "//", unneeded parents "foo/..", or current directory
  13712. * "foo/.". We need to normalize to a standard representation.
  13713. */
  13714. function normalizePath$4(url, type) {
  13715. const rel = type <= 4 /* RelativePath */;
  13716. const pieces = url.path.split('/');
  13717. // We need to preserve the first piece always, so that we output a leading slash. The item at
  13718. // pieces[0] is an empty string.
  13719. let pointer = 1;
  13720. // Positive is the number of real directories we've output, used for popping a parent directory.
  13721. // Eg, "foo/bar/.." will have a positive 2, and we can decrement to be left with just "foo".
  13722. let positive = 0;
  13723. // We need to keep a trailing slash if we encounter an empty directory (eg, splitting "foo/" will
  13724. // generate `["foo", ""]` pieces). And, if we pop a parent directory. But once we encounter a
  13725. // real directory, we won't need to append, unless the other conditions happen again.
  13726. let addTrailingSlash = false;
  13727. for (let i = 1; i < pieces.length; i++) {
  13728. const piece = pieces[i];
  13729. // An empty directory, could be a trailing slash, or just a double "//" in the path.
  13730. if (!piece) {
  13731. addTrailingSlash = true;
  13732. continue;
  13733. }
  13734. // If we encounter a real directory, then we don't need to append anymore.
  13735. addTrailingSlash = false;
  13736. // A current directory, which we can always drop.
  13737. if (piece === '.')
  13738. continue;
  13739. // A parent directory, we need to see if there are any real directories we can pop. Else, we
  13740. // have an excess of parents, and we'll need to keep the "..".
  13741. if (piece === '..') {
  13742. if (positive) {
  13743. addTrailingSlash = true;
  13744. positive--;
  13745. pointer--;
  13746. }
  13747. else if (rel) {
  13748. // If we're in a relativePath, then we need to keep the excess parents. Else, in an absolute
  13749. // URL, protocol relative URL, or an absolute path, we don't need to keep excess.
  13750. pieces[pointer++] = piece;
  13751. }
  13752. continue;
  13753. }
  13754. // We've encountered a real directory. Move it to the next insertion pointer, which accounts for
  13755. // any popped or dropped directories.
  13756. pieces[pointer++] = piece;
  13757. positive++;
  13758. }
  13759. let path = '';
  13760. for (let i = 1; i < pointer; i++) {
  13761. path += '/' + pieces[i];
  13762. }
  13763. if (!path || (addTrailingSlash && !path.endsWith('/..'))) {
  13764. path += '/';
  13765. }
  13766. url.path = path;
  13767. }
  13768. /**
  13769. * Attempts to resolve `input` URL/path relative to `base`.
  13770. */
  13771. function resolve$2(input, base) {
  13772. if (!input && !base)
  13773. return '';
  13774. const url = parseUrl$3(input);
  13775. let inputType = url.type;
  13776. if (base && inputType !== 7 /* Absolute */) {
  13777. const baseUrl = parseUrl$3(base);
  13778. const baseType = baseUrl.type;
  13779. switch (inputType) {
  13780. case 1 /* Empty */:
  13781. url.hash = baseUrl.hash;
  13782. // fall through
  13783. case 2 /* Hash */:
  13784. url.query = baseUrl.query;
  13785. // fall through
  13786. case 3 /* Query */:
  13787. case 4 /* RelativePath */:
  13788. mergePaths(url, baseUrl);
  13789. // fall through
  13790. case 5 /* AbsolutePath */:
  13791. // The host, user, and port are joined, you can't copy one without the others.
  13792. url.user = baseUrl.user;
  13793. url.host = baseUrl.host;
  13794. url.port = baseUrl.port;
  13795. // fall through
  13796. case 6 /* SchemeRelative */:
  13797. // The input doesn't have a schema at least, so we need to copy at least that over.
  13798. url.scheme = baseUrl.scheme;
  13799. }
  13800. if (baseType > inputType)
  13801. inputType = baseType;
  13802. }
  13803. normalizePath$4(url, inputType);
  13804. const queryHash = url.query + url.hash;
  13805. switch (inputType) {
  13806. // This is impossible, because of the empty checks at the start of the function.
  13807. // case UrlType.Empty:
  13808. case 2 /* Hash */:
  13809. case 3 /* Query */:
  13810. return queryHash;
  13811. case 4 /* RelativePath */: {
  13812. // The first char is always a "/", and we need it to be relative.
  13813. const path = url.path.slice(1);
  13814. if (!path)
  13815. return queryHash || '.';
  13816. if (isRelative(base || input) && !isRelative(path)) {
  13817. // If base started with a leading ".", or there is no base and input started with a ".",
  13818. // then we need to ensure that the relative path starts with a ".". We don't know if
  13819. // relative starts with a "..", though, so check before prepending.
  13820. return './' + path + queryHash;
  13821. }
  13822. return path + queryHash;
  13823. }
  13824. case 5 /* AbsolutePath */:
  13825. return url.path + queryHash;
  13826. default:
  13827. return url.scheme + '//' + url.user + url.host + url.port + url.path + queryHash;
  13828. }
  13829. }
  13830. function resolve$1(input, base) {
  13831. // The base is always treated as a directory, if it's not empty.
  13832. // https://github.com/mozilla/source-map/blob/8cb3ee57/lib/util.js#L327
  13833. // https://github.com/chromium/chromium/blob/da4adbb3/third_party/blink/renderer/devtools/front_end/sdk/SourceMap.js#L400-L401
  13834. if (base && !base.endsWith('/'))
  13835. base += '/';
  13836. return resolve$2(input, base);
  13837. }
  13838. /**
  13839. * Removes everything after the last "/", but leaves the slash.
  13840. */
  13841. function stripFilename(path) {
  13842. if (!path)
  13843. return '';
  13844. const index = path.lastIndexOf('/');
  13845. return path.slice(0, index + 1);
  13846. }
  13847. const COLUMN$1 = 0;
  13848. const SOURCES_INDEX$1 = 1;
  13849. const SOURCE_LINE$1 = 2;
  13850. const SOURCE_COLUMN$1 = 3;
  13851. const NAMES_INDEX$1 = 4;
  13852. function maybeSort(mappings, owned) {
  13853. const unsortedIndex = nextUnsortedSegmentLine(mappings, 0);
  13854. if (unsortedIndex === mappings.length)
  13855. return mappings;
  13856. // If we own the array (meaning we parsed it from JSON), then we're free to directly mutate it. If
  13857. // not, we do not want to modify the consumer's input array.
  13858. if (!owned)
  13859. mappings = mappings.slice();
  13860. for (let i = unsortedIndex; i < mappings.length; i = nextUnsortedSegmentLine(mappings, i + 1)) {
  13861. mappings[i] = sortSegments(mappings[i], owned);
  13862. }
  13863. return mappings;
  13864. }
  13865. function nextUnsortedSegmentLine(mappings, start) {
  13866. for (let i = start; i < mappings.length; i++) {
  13867. if (!isSorted(mappings[i]))
  13868. return i;
  13869. }
  13870. return mappings.length;
  13871. }
  13872. function isSorted(line) {
  13873. for (let j = 1; j < line.length; j++) {
  13874. if (line[j][COLUMN$1] < line[j - 1][COLUMN$1]) {
  13875. return false;
  13876. }
  13877. }
  13878. return true;
  13879. }
  13880. function sortSegments(line, owned) {
  13881. if (!owned)
  13882. line = line.slice();
  13883. return line.sort(sortComparator);
  13884. }
  13885. function sortComparator(a, b) {
  13886. return a[COLUMN$1] - b[COLUMN$1];
  13887. }
  13888. let found = false;
  13889. /**
  13890. * A binary search implementation that returns the index if a match is found.
  13891. * If no match is found, then the left-index (the index associated with the item that comes just
  13892. * before the desired index) is returned. To maintain proper sort order, a splice would happen at
  13893. * the next index:
  13894. *
  13895. * ```js
  13896. * const array = [1, 3];
  13897. * const needle = 2;
  13898. * const index = binarySearch(array, needle, (item, needle) => item - needle);
  13899. *
  13900. * assert.equal(index, 0);
  13901. * array.splice(index + 1, 0, needle);
  13902. * assert.deepEqual(array, [1, 2, 3]);
  13903. * ```
  13904. */
  13905. function binarySearch(haystack, needle, low, high) {
  13906. while (low <= high) {
  13907. const mid = low + ((high - low) >> 1);
  13908. const cmp = haystack[mid][COLUMN$1] - needle;
  13909. if (cmp === 0) {
  13910. found = true;
  13911. return mid;
  13912. }
  13913. if (cmp < 0) {
  13914. low = mid + 1;
  13915. }
  13916. else {
  13917. high = mid - 1;
  13918. }
  13919. }
  13920. found = false;
  13921. return low - 1;
  13922. }
  13923. function upperBound(haystack, needle, index) {
  13924. for (let i = index + 1; i < haystack.length; index = i++) {
  13925. if (haystack[i][COLUMN$1] !== needle)
  13926. break;
  13927. }
  13928. return index;
  13929. }
  13930. function lowerBound(haystack, needle, index) {
  13931. for (let i = index - 1; i >= 0; index = i--) {
  13932. if (haystack[i][COLUMN$1] !== needle)
  13933. break;
  13934. }
  13935. return index;
  13936. }
  13937. function memoizedState() {
  13938. return {
  13939. lastKey: -1,
  13940. lastNeedle: -1,
  13941. lastIndex: -1,
  13942. };
  13943. }
  13944. /**
  13945. * This overly complicated beast is just to record the last tested line/column and the resulting
  13946. * index, allowing us to skip a few tests if mappings are monotonically increasing.
  13947. */
  13948. function memoizedBinarySearch(haystack, needle, state, key) {
  13949. const { lastKey, lastNeedle, lastIndex } = state;
  13950. let low = 0;
  13951. let high = haystack.length - 1;
  13952. if (key === lastKey) {
  13953. if (needle === lastNeedle) {
  13954. found = lastIndex !== -1 && haystack[lastIndex][COLUMN$1] === needle;
  13955. return lastIndex;
  13956. }
  13957. if (needle >= lastNeedle) {
  13958. // lastIndex may be -1 if the previous needle was not found.
  13959. low = lastIndex === -1 ? 0 : lastIndex;
  13960. }
  13961. else {
  13962. high = lastIndex;
  13963. }
  13964. }
  13965. state.lastKey = key;
  13966. state.lastNeedle = needle;
  13967. return (state.lastIndex = binarySearch(haystack, needle, low, high));
  13968. }
  13969. const LINE_GTR_ZERO = '`line` must be greater than 0 (lines start at line 1)';
  13970. const COL_GTR_EQ_ZERO = '`column` must be greater than or equal to 0 (columns start at column 0)';
  13971. const LEAST_UPPER_BOUND = -1;
  13972. const GREATEST_LOWER_BOUND = 1;
  13973. class TraceMap {
  13974. constructor(map, mapUrl) {
  13975. const isString = typeof map === 'string';
  13976. if (!isString && map._decodedMemo)
  13977. return map;
  13978. const parsed = (isString ? JSON.parse(map) : map);
  13979. const { version, file, names, sourceRoot, sources, sourcesContent } = parsed;
  13980. this.version = version;
  13981. this.file = file;
  13982. this.names = names || [];
  13983. this.sourceRoot = sourceRoot;
  13984. this.sources = sources;
  13985. this.sourcesContent = sourcesContent;
  13986. this.ignoreList = parsed.ignoreList || parsed.x_google_ignoreList || undefined;
  13987. const from = resolve$1(sourceRoot || '', stripFilename(mapUrl));
  13988. this.resolvedSources = sources.map((s) => resolve$1(s || '', from));
  13989. const { mappings } = parsed;
  13990. if (typeof mappings === 'string') {
  13991. this._encoded = mappings;
  13992. this._decoded = undefined;
  13993. }
  13994. else {
  13995. this._encoded = undefined;
  13996. this._decoded = maybeSort(mappings, isString);
  13997. }
  13998. this._decodedMemo = memoizedState();
  13999. this._bySources = undefined;
  14000. this._bySourceMemos = undefined;
  14001. }
  14002. }
  14003. /**
  14004. * Typescript doesn't allow friend access to private fields, so this just casts the map into a type
  14005. * with public access modifiers.
  14006. */
  14007. function cast$2(map) {
  14008. return map;
  14009. }
  14010. /**
  14011. * Returns the encoded (VLQ string) form of the SourceMap's mappings field.
  14012. */
  14013. function encodedMappings(map) {
  14014. var _a;
  14015. var _b;
  14016. return ((_a = (_b = cast$2(map))._encoded) !== null && _a !== void 0 ? _a : (_b._encoded = encode$1(cast$2(map)._decoded)));
  14017. }
  14018. /**
  14019. * Returns the decoded (array of lines of segments) form of the SourceMap's mappings field.
  14020. */
  14021. function decodedMappings(map) {
  14022. var _a;
  14023. return ((_a = cast$2(map))._decoded || (_a._decoded = decode(cast$2(map)._encoded)));
  14024. }
  14025. /**
  14026. * A low-level API to find the segment associated with a generated line/column (think, from a
  14027. * stack trace). Line and column here are 0-based, unlike `originalPositionFor`.
  14028. */
  14029. function traceSegment(map, line, column) {
  14030. const decoded = decodedMappings(map);
  14031. // It's common for parent source maps to have pointers to lines that have no
  14032. // mapping (like a "//# sourceMappingURL=") at the end of the child file.
  14033. if (line >= decoded.length)
  14034. return null;
  14035. const segments = decoded[line];
  14036. const index = traceSegmentInternal(segments, cast$2(map)._decodedMemo, line, column, GREATEST_LOWER_BOUND);
  14037. return index === -1 ? null : segments[index];
  14038. }
  14039. /**
  14040. * A higher-level API to find the source/line/column associated with a generated line/column
  14041. * (think, from a stack trace). Line is 1-based, but column is 0-based, due to legacy behavior in
  14042. * `source-map` library.
  14043. */
  14044. function originalPositionFor$1(map, needle) {
  14045. let { line, column, bias } = needle;
  14046. line--;
  14047. if (line < 0)
  14048. throw new Error(LINE_GTR_ZERO);
  14049. if (column < 0)
  14050. throw new Error(COL_GTR_EQ_ZERO);
  14051. const decoded = decodedMappings(map);
  14052. // It's common for parent source maps to have pointers to lines that have no
  14053. // mapping (like a "//# sourceMappingURL=") at the end of the child file.
  14054. if (line >= decoded.length)
  14055. return OMapping(null, null, null, null);
  14056. const segments = decoded[line];
  14057. const index = traceSegmentInternal(segments, cast$2(map)._decodedMemo, line, column, bias || GREATEST_LOWER_BOUND);
  14058. if (index === -1)
  14059. return OMapping(null, null, null, null);
  14060. const segment = segments[index];
  14061. if (segment.length === 1)
  14062. return OMapping(null, null, null, null);
  14063. const { names, resolvedSources } = map;
  14064. return OMapping(resolvedSources[segment[SOURCES_INDEX$1]], segment[SOURCE_LINE$1] + 1, segment[SOURCE_COLUMN$1], segment.length === 5 ? names[segment[NAMES_INDEX$1]] : null);
  14065. }
  14066. /**
  14067. * Returns a sourcemap object (with decoded mappings) suitable for passing to a library that expects
  14068. * a sourcemap, or to JSON.stringify.
  14069. */
  14070. function decodedMap(map) {
  14071. return clone(map, decodedMappings(map));
  14072. }
  14073. /**
  14074. * Returns a sourcemap object (with encoded mappings) suitable for passing to a library that expects
  14075. * a sourcemap, or to JSON.stringify.
  14076. */
  14077. function encodedMap(map) {
  14078. return clone(map, encodedMappings(map));
  14079. }
  14080. function clone(map, mappings) {
  14081. return {
  14082. version: map.version,
  14083. file: map.file,
  14084. names: map.names,
  14085. sourceRoot: map.sourceRoot,
  14086. sources: map.sources,
  14087. sourcesContent: map.sourcesContent,
  14088. mappings,
  14089. ignoreList: map.ignoreList || map.x_google_ignoreList,
  14090. };
  14091. }
  14092. function OMapping(source, line, column, name) {
  14093. return { source, line, column, name };
  14094. }
  14095. function traceSegmentInternal(segments, memo, line, column, bias) {
  14096. let index = memoizedBinarySearch(segments, column, memo, line);
  14097. if (found) {
  14098. index = (bias === LEAST_UPPER_BOUND ? upperBound : lowerBound)(segments, column, index);
  14099. }
  14100. else if (bias === LEAST_UPPER_BOUND)
  14101. index++;
  14102. if (index === -1 || index === segments.length)
  14103. return -1;
  14104. return index;
  14105. }
  14106. /**
  14107. * SetArray acts like a `Set` (allowing only one occurrence of a string `key`), but provides the
  14108. * index of the `key` in the backing array.
  14109. *
  14110. * This is designed to allow synchronizing a second array with the contents of the backing array,
  14111. * like how in a sourcemap `sourcesContent[i]` is the source content associated with `source[i]`,
  14112. * and there are never duplicates.
  14113. */
  14114. class SetArray {
  14115. constructor() {
  14116. this._indexes = { __proto__: null };
  14117. this.array = [];
  14118. }
  14119. }
  14120. /**
  14121. * Typescript doesn't allow friend access to private fields, so this just casts the set into a type
  14122. * with public access modifiers.
  14123. */
  14124. function cast$1(set) {
  14125. return set;
  14126. }
  14127. /**
  14128. * Gets the index associated with `key` in the backing array, if it is already present.
  14129. */
  14130. function get(setarr, key) {
  14131. return cast$1(setarr)._indexes[key];
  14132. }
  14133. /**
  14134. * Puts `key` into the backing array, if it is not already present. Returns
  14135. * the index of the `key` in the backing array.
  14136. */
  14137. function put(setarr, key) {
  14138. // The key may or may not be present. If it is present, it's a number.
  14139. const index = get(setarr, key);
  14140. if (index !== undefined)
  14141. return index;
  14142. const { array, _indexes: indexes } = cast$1(setarr);
  14143. const length = array.push(key);
  14144. return (indexes[key] = length - 1);
  14145. }
  14146. /**
  14147. * Removes the key, if it exists in the set.
  14148. */
  14149. function remove(setarr, key) {
  14150. const index = get(setarr, key);
  14151. if (index === undefined)
  14152. return;
  14153. const { array, _indexes: indexes } = cast$1(setarr);
  14154. for (let i = index + 1; i < array.length; i++) {
  14155. const k = array[i];
  14156. array[i - 1] = k;
  14157. indexes[k]--;
  14158. }
  14159. indexes[key] = undefined;
  14160. array.pop();
  14161. }
  14162. const COLUMN = 0;
  14163. const SOURCES_INDEX = 1;
  14164. const SOURCE_LINE = 2;
  14165. const SOURCE_COLUMN = 3;
  14166. const NAMES_INDEX = 4;
  14167. const NO_NAME = -1;
  14168. /**
  14169. * Provides the state to generate a sourcemap.
  14170. */
  14171. class GenMapping {
  14172. constructor({ file, sourceRoot } = {}) {
  14173. this._names = new SetArray();
  14174. this._sources = new SetArray();
  14175. this._sourcesContent = [];
  14176. this._mappings = [];
  14177. this.file = file;
  14178. this.sourceRoot = sourceRoot;
  14179. this._ignoreList = new SetArray();
  14180. }
  14181. }
  14182. /**
  14183. * Typescript doesn't allow friend access to private fields, so this just casts the map into a type
  14184. * with public access modifiers.
  14185. */
  14186. function cast(map) {
  14187. return map;
  14188. }
  14189. /**
  14190. * Same as `addSegment`, but will only add the segment if it generates useful information in the
  14191. * resulting map. This only works correctly if segments are added **in order**, meaning you should
  14192. * not add a segment with a lower generated line/column than one that came before.
  14193. */
  14194. const maybeAddSegment = (map, genLine, genColumn, source, sourceLine, sourceColumn, name, content) => {
  14195. return addSegmentInternal(true, map, genLine, genColumn, source, sourceLine, sourceColumn, name);
  14196. };
  14197. /**
  14198. * Adds/removes the content of the source file to the source map.
  14199. */
  14200. function setSourceContent(map, source, content) {
  14201. const { _sources: sources, _sourcesContent: sourcesContent } = cast(map);
  14202. const index = put(sources, source);
  14203. sourcesContent[index] = content;
  14204. }
  14205. function setIgnore(map, source, ignore = true) {
  14206. const { _sources: sources, _sourcesContent: sourcesContent, _ignoreList: ignoreList } = cast(map);
  14207. const index = put(sources, source);
  14208. if (index === sourcesContent.length)
  14209. sourcesContent[index] = null;
  14210. if (ignore)
  14211. put(ignoreList, index);
  14212. else
  14213. remove(ignoreList, index);
  14214. }
  14215. /**
  14216. * Returns a sourcemap object (with decoded mappings) suitable for passing to a library that expects
  14217. * a sourcemap, or to JSON.stringify.
  14218. */
  14219. function toDecodedMap(map) {
  14220. const { _mappings: mappings, _sources: sources, _sourcesContent: sourcesContent, _names: names, _ignoreList: ignoreList, } = cast(map);
  14221. removeEmptyFinalLines(mappings);
  14222. return {
  14223. version: 3,
  14224. file: map.file || undefined,
  14225. names: names.array,
  14226. sourceRoot: map.sourceRoot || undefined,
  14227. sources: sources.array,
  14228. sourcesContent,
  14229. mappings,
  14230. ignoreList: ignoreList.array,
  14231. };
  14232. }
  14233. /**
  14234. * Returns a sourcemap object (with encoded mappings) suitable for passing to a library that expects
  14235. * a sourcemap, or to JSON.stringify.
  14236. */
  14237. function toEncodedMap(map) {
  14238. const decoded = toDecodedMap(map);
  14239. return Object.assign(Object.assign({}, decoded), { mappings: encode$1(decoded.mappings) });
  14240. }
  14241. // This split declaration is only so that terser can elminiate the static initialization block.
  14242. function addSegmentInternal(skipable, map, genLine, genColumn, source, sourceLine, sourceColumn, name, content) {
  14243. const { _mappings: mappings, _sources: sources, _sourcesContent: sourcesContent, _names: names, } = cast(map);
  14244. const line = getLine(mappings, genLine);
  14245. const index = getColumnIndex(line, genColumn);
  14246. if (!source) {
  14247. if (skipSourceless(line, index))
  14248. return;
  14249. return insert(line, index, [genColumn]);
  14250. }
  14251. const sourcesIndex = put(sources, source);
  14252. const namesIndex = name ? put(names, name) : NO_NAME;
  14253. if (sourcesIndex === sourcesContent.length)
  14254. sourcesContent[sourcesIndex] = null;
  14255. if (skipSource(line, index, sourcesIndex, sourceLine, sourceColumn, namesIndex)) {
  14256. return;
  14257. }
  14258. return insert(line, index, name
  14259. ? [genColumn, sourcesIndex, sourceLine, sourceColumn, namesIndex]
  14260. : [genColumn, sourcesIndex, sourceLine, sourceColumn]);
  14261. }
  14262. function getLine(mappings, index) {
  14263. for (let i = mappings.length; i <= index; i++) {
  14264. mappings[i] = [];
  14265. }
  14266. return mappings[index];
  14267. }
  14268. function getColumnIndex(line, genColumn) {
  14269. let index = line.length;
  14270. for (let i = index - 1; i >= 0; index = i--) {
  14271. const current = line[i];
  14272. if (genColumn >= current[COLUMN])
  14273. break;
  14274. }
  14275. return index;
  14276. }
  14277. function insert(array, index, value) {
  14278. for (let i = array.length; i > index; i--) {
  14279. array[i] = array[i - 1];
  14280. }
  14281. array[index] = value;
  14282. }
  14283. function removeEmptyFinalLines(mappings) {
  14284. const { length } = mappings;
  14285. let len = length;
  14286. for (let i = len - 1; i >= 0; len = i, i--) {
  14287. if (mappings[i].length > 0)
  14288. break;
  14289. }
  14290. if (len < length)
  14291. mappings.length = len;
  14292. }
  14293. function skipSourceless(line, index) {
  14294. // The start of a line is already sourceless, so adding a sourceless segment to the beginning
  14295. // doesn't generate any useful information.
  14296. if (index === 0)
  14297. return true;
  14298. const prev = line[index - 1];
  14299. // If the previous segment is also sourceless, then adding another sourceless segment doesn't
  14300. // genrate any new information. Else, this segment will end the source/named segment and point to
  14301. // a sourceless position, which is useful.
  14302. return prev.length === 1;
  14303. }
  14304. function skipSource(line, index, sourcesIndex, sourceLine, sourceColumn, namesIndex) {
  14305. // A source/named segment at the start of a line gives position at that genColumn
  14306. if (index === 0)
  14307. return false;
  14308. const prev = line[index - 1];
  14309. // If the previous segment is sourceless, then we're transitioning to a source.
  14310. if (prev.length === 1)
  14311. return false;
  14312. // If the previous segment maps to the exact same source position, then this segment doesn't
  14313. // provide any new position information.
  14314. return (sourcesIndex === prev[SOURCES_INDEX] &&
  14315. sourceLine === prev[SOURCE_LINE] &&
  14316. sourceColumn === prev[SOURCE_COLUMN] &&
  14317. namesIndex === (prev.length === 5 ? prev[NAMES_INDEX] : NO_NAME));
  14318. }
  14319. const SOURCELESS_MAPPING = /* #__PURE__ */ SegmentObject('', -1, -1, '', null, false);
  14320. const EMPTY_SOURCES = [];
  14321. function SegmentObject(source, line, column, name, content, ignore) {
  14322. return { source, line, column, name, content, ignore };
  14323. }
  14324. function Source(map, sources, source, content, ignore) {
  14325. return {
  14326. map,
  14327. sources,
  14328. source,
  14329. content,
  14330. ignore,
  14331. };
  14332. }
  14333. /**
  14334. * MapSource represents a single sourcemap, with the ability to trace mappings into its child nodes
  14335. * (which may themselves be SourceMapTrees).
  14336. */
  14337. function MapSource(map, sources) {
  14338. return Source(map, sources, '', null, false);
  14339. }
  14340. /**
  14341. * A "leaf" node in the sourcemap tree, representing an original, unmodified source file. Recursive
  14342. * segment tracing ends at the `OriginalSource`.
  14343. */
  14344. function OriginalSource(source, content, ignore) {
  14345. return Source(null, EMPTY_SOURCES, source, content, ignore);
  14346. }
  14347. /**
  14348. * traceMappings is only called on the root level SourceMapTree, and begins the process of
  14349. * resolving each mapping in terms of the original source files.
  14350. */
  14351. function traceMappings(tree) {
  14352. // TODO: Eventually support sourceRoot, which has to be removed because the sources are already
  14353. // fully resolved. We'll need to make sources relative to the sourceRoot before adding them.
  14354. const gen = new GenMapping({ file: tree.map.file });
  14355. const { sources: rootSources, map } = tree;
  14356. const rootNames = map.names;
  14357. const rootMappings = decodedMappings(map);
  14358. for (let i = 0; i < rootMappings.length; i++) {
  14359. const segments = rootMappings[i];
  14360. for (let j = 0; j < segments.length; j++) {
  14361. const segment = segments[j];
  14362. const genCol = segment[0];
  14363. let traced = SOURCELESS_MAPPING;
  14364. // 1-length segments only move the current generated column, there's no source information
  14365. // to gather from it.
  14366. if (segment.length !== 1) {
  14367. const source = rootSources[segment[1]];
  14368. traced = originalPositionFor(source, segment[2], segment[3], segment.length === 5 ? rootNames[segment[4]] : '');
  14369. // If the trace is invalid, then the trace ran into a sourcemap that doesn't contain a
  14370. // respective segment into an original source.
  14371. if (traced == null)
  14372. continue;
  14373. }
  14374. const { column, line, name, content, source, ignore } = traced;
  14375. maybeAddSegment(gen, i, genCol, source, line, column, name);
  14376. if (source && content != null)
  14377. setSourceContent(gen, source, content);
  14378. if (ignore)
  14379. setIgnore(gen, source, true);
  14380. }
  14381. }
  14382. return gen;
  14383. }
  14384. /**
  14385. * originalPositionFor is only called on children SourceMapTrees. It recurses down into its own
  14386. * child SourceMapTrees, until we find the original source map.
  14387. */
  14388. function originalPositionFor(source, line, column, name) {
  14389. if (!source.map) {
  14390. return SegmentObject(source.source, line, column, name, source.content, source.ignore);
  14391. }
  14392. const segment = traceSegment(source.map, line, column);
  14393. // If we couldn't find a segment, then this doesn't exist in the sourcemap.
  14394. if (segment == null)
  14395. return null;
  14396. // 1-length segments only move the current generated column, there's no source information
  14397. // to gather from it.
  14398. if (segment.length === 1)
  14399. return SOURCELESS_MAPPING;
  14400. return originalPositionFor(source.sources[segment[1]], segment[2], segment[3], segment.length === 5 ? source.map.names[segment[4]] : name);
  14401. }
  14402. function asArray(value) {
  14403. if (Array.isArray(value))
  14404. return value;
  14405. return [value];
  14406. }
  14407. /**
  14408. * Recursively builds a tree structure out of sourcemap files, with each node
  14409. * being either an `OriginalSource` "leaf" or a `SourceMapTree` composed of
  14410. * `OriginalSource`s and `SourceMapTree`s.
  14411. *
  14412. * Every sourcemap is composed of a collection of source files and mappings
  14413. * into locations of those source files. When we generate a `SourceMapTree` for
  14414. * the sourcemap, we attempt to load each source file's own sourcemap. If it
  14415. * does not have an associated sourcemap, it is considered an original,
  14416. * unmodified source file.
  14417. */
  14418. function buildSourceMapTree(input, loader) {
  14419. const maps = asArray(input).map((m) => new TraceMap(m, ''));
  14420. const map = maps.pop();
  14421. for (let i = 0; i < maps.length; i++) {
  14422. if (maps[i].sources.length > 1) {
  14423. throw new Error(`Transformation map ${i} must have exactly one source file.\n` +
  14424. 'Did you specify these with the most recent transformation maps first?');
  14425. }
  14426. }
  14427. let tree = build$2(map, loader, '', 0);
  14428. for (let i = maps.length - 1; i >= 0; i--) {
  14429. tree = MapSource(maps[i], [tree]);
  14430. }
  14431. return tree;
  14432. }
  14433. function build$2(map, loader, importer, importerDepth) {
  14434. const { resolvedSources, sourcesContent, ignoreList } = map;
  14435. const depth = importerDepth + 1;
  14436. const children = resolvedSources.map((sourceFile, i) => {
  14437. // The loading context gives the loader more information about why this file is being loaded
  14438. // (eg, from which importer). It also allows the loader to override the location of the loaded
  14439. // sourcemap/original source, or to override the content in the sourcesContent field if it's
  14440. // an unmodified source file.
  14441. const ctx = {
  14442. importer,
  14443. depth,
  14444. source: sourceFile || '',
  14445. content: undefined,
  14446. ignore: undefined,
  14447. };
  14448. // Use the provided loader callback to retrieve the file's sourcemap.
  14449. // TODO: We should eventually support async loading of sourcemap files.
  14450. const sourceMap = loader(ctx.source, ctx);
  14451. const { source, content, ignore } = ctx;
  14452. // If there is a sourcemap, then we need to recurse into it to load its source files.
  14453. if (sourceMap)
  14454. return build$2(new TraceMap(sourceMap, source), loader, source, depth);
  14455. // Else, it's an unmodified source file.
  14456. // The contents of this unmodified source file can be overridden via the loader context,
  14457. // allowing it to be explicitly null or a string. If it remains undefined, we fall back to
  14458. // the importing sourcemap's `sourcesContent` field.
  14459. const sourceContent = content !== undefined ? content : sourcesContent ? sourcesContent[i] : null;
  14460. const ignored = ignore !== undefined ? ignore : ignoreList ? ignoreList.includes(i) : false;
  14461. return OriginalSource(source, sourceContent, ignored);
  14462. });
  14463. return MapSource(map, children);
  14464. }
  14465. /**
  14466. * A SourceMap v3 compatible sourcemap, which only includes fields that were
  14467. * provided to it.
  14468. */
  14469. class SourceMap {
  14470. constructor(map, options) {
  14471. const out = options.decodedMappings ? toDecodedMap(map) : toEncodedMap(map);
  14472. this.version = out.version; // SourceMap spec says this should be first.
  14473. this.file = out.file;
  14474. this.mappings = out.mappings;
  14475. this.names = out.names;
  14476. this.ignoreList = out.ignoreList;
  14477. this.sourceRoot = out.sourceRoot;
  14478. this.sources = out.sources;
  14479. if (!options.excludeContent) {
  14480. this.sourcesContent = out.sourcesContent;
  14481. }
  14482. }
  14483. toString() {
  14484. return JSON.stringify(this);
  14485. }
  14486. }
  14487. /**
  14488. * Traces through all the mappings in the root sourcemap, through the sources
  14489. * (and their sourcemaps), all the way back to the original source location.
  14490. *
  14491. * `loader` will be called every time we encounter a source file. If it returns
  14492. * a sourcemap, we will recurse into that sourcemap to continue the trace. If
  14493. * it returns a falsey value, that source file is treated as an original,
  14494. * unmodified source file.
  14495. *
  14496. * Pass `excludeContent` to exclude any self-containing source file content
  14497. * from the output sourcemap.
  14498. *
  14499. * Pass `decodedMappings` to receive a SourceMap with decoded (instead of
  14500. * VLQ encoded) mappings.
  14501. */
  14502. function remapping(input, loader, options) {
  14503. const opts = { excludeContent: !!options, decodedMappings: false };
  14504. const tree = buildSourceMapTree(input, loader);
  14505. return new SourceMap(traceMappings(tree), opts);
  14506. }
  14507. var src$3 = {exports: {}};
  14508. var browser$3 = {exports: {}};
  14509. /**
  14510. * Helpers.
  14511. */
  14512. var ms$1;
  14513. var hasRequiredMs$1;
  14514. function requireMs$1 () {
  14515. if (hasRequiredMs$1) return ms$1;
  14516. hasRequiredMs$1 = 1;
  14517. var s = 1000;
  14518. var m = s * 60;
  14519. var h = m * 60;
  14520. var d = h * 24;
  14521. var w = d * 7;
  14522. var y = d * 365.25;
  14523. /**
  14524. * Parse or format the given `val`.
  14525. *
  14526. * Options:
  14527. *
  14528. * - `long` verbose formatting [false]
  14529. *
  14530. * @param {String|Number} val
  14531. * @param {Object} [options]
  14532. * @throws {Error} throw an error if val is not a non-empty string or a number
  14533. * @return {String|Number}
  14534. * @api public
  14535. */
  14536. ms$1 = function(val, options) {
  14537. options = options || {};
  14538. var type = typeof val;
  14539. if (type === 'string' && val.length > 0) {
  14540. return parse(val);
  14541. } else if (type === 'number' && isFinite(val)) {
  14542. return options.long ? fmtLong(val) : fmtShort(val);
  14543. }
  14544. throw new Error(
  14545. 'val is not a non-empty string or a valid number. val=' +
  14546. JSON.stringify(val)
  14547. );
  14548. };
  14549. /**
  14550. * Parse the given `str` and return milliseconds.
  14551. *
  14552. * @param {String} str
  14553. * @return {Number}
  14554. * @api private
  14555. */
  14556. function parse(str) {
  14557. str = String(str);
  14558. if (str.length > 100) {
  14559. return;
  14560. }
  14561. var match = /^(-?(?:\d+)?\.?\d+) *(milliseconds?|msecs?|ms|seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|weeks?|w|years?|yrs?|y)?$/i.exec(
  14562. str
  14563. );
  14564. if (!match) {
  14565. return;
  14566. }
  14567. var n = parseFloat(match[1]);
  14568. var type = (match[2] || 'ms').toLowerCase();
  14569. switch (type) {
  14570. case 'years':
  14571. case 'year':
  14572. case 'yrs':
  14573. case 'yr':
  14574. case 'y':
  14575. return n * y;
  14576. case 'weeks':
  14577. case 'week':
  14578. case 'w':
  14579. return n * w;
  14580. case 'days':
  14581. case 'day':
  14582. case 'd':
  14583. return n * d;
  14584. case 'hours':
  14585. case 'hour':
  14586. case 'hrs':
  14587. case 'hr':
  14588. case 'h':
  14589. return n * h;
  14590. case 'minutes':
  14591. case 'minute':
  14592. case 'mins':
  14593. case 'min':
  14594. case 'm':
  14595. return n * m;
  14596. case 'seconds':
  14597. case 'second':
  14598. case 'secs':
  14599. case 'sec':
  14600. case 's':
  14601. return n * s;
  14602. case 'milliseconds':
  14603. case 'millisecond':
  14604. case 'msecs':
  14605. case 'msec':
  14606. case 'ms':
  14607. return n;
  14608. default:
  14609. return undefined;
  14610. }
  14611. }
  14612. /**
  14613. * Short format for `ms`.
  14614. *
  14615. * @param {Number} ms
  14616. * @return {String}
  14617. * @api private
  14618. */
  14619. function fmtShort(ms) {
  14620. var msAbs = Math.abs(ms);
  14621. if (msAbs >= d) {
  14622. return Math.round(ms / d) + 'd';
  14623. }
  14624. if (msAbs >= h) {
  14625. return Math.round(ms / h) + 'h';
  14626. }
  14627. if (msAbs >= m) {
  14628. return Math.round(ms / m) + 'm';
  14629. }
  14630. if (msAbs >= s) {
  14631. return Math.round(ms / s) + 's';
  14632. }
  14633. return ms + 'ms';
  14634. }
  14635. /**
  14636. * Long format for `ms`.
  14637. *
  14638. * @param {Number} ms
  14639. * @return {String}
  14640. * @api private
  14641. */
  14642. function fmtLong(ms) {
  14643. var msAbs = Math.abs(ms);
  14644. if (msAbs >= d) {
  14645. return plural(ms, msAbs, d, 'day');
  14646. }
  14647. if (msAbs >= h) {
  14648. return plural(ms, msAbs, h, 'hour');
  14649. }
  14650. if (msAbs >= m) {
  14651. return plural(ms, msAbs, m, 'minute');
  14652. }
  14653. if (msAbs >= s) {
  14654. return plural(ms, msAbs, s, 'second');
  14655. }
  14656. return ms + ' ms';
  14657. }
  14658. /**
  14659. * Pluralization helper.
  14660. */
  14661. function plural(ms, msAbs, n, name) {
  14662. var isPlural = msAbs >= n * 1.5;
  14663. return Math.round(ms / n) + ' ' + name + (isPlural ? 's' : '');
  14664. }
  14665. return ms$1;
  14666. }
  14667. var common$b;
  14668. var hasRequiredCommon;
  14669. function requireCommon () {
  14670. if (hasRequiredCommon) return common$b;
  14671. hasRequiredCommon = 1;
  14672. /**
  14673. * This is the common logic for both the Node.js and web browser
  14674. * implementations of `debug()`.
  14675. */
  14676. function setup(env) {
  14677. createDebug.debug = createDebug;
  14678. createDebug.default = createDebug;
  14679. createDebug.coerce = coerce;
  14680. createDebug.disable = disable;
  14681. createDebug.enable = enable;
  14682. createDebug.enabled = enabled;
  14683. createDebug.humanize = requireMs$1();
  14684. createDebug.destroy = destroy;
  14685. Object.keys(env).forEach(key => {
  14686. createDebug[key] = env[key];
  14687. });
  14688. /**
  14689. * The currently active debug mode names, and names to skip.
  14690. */
  14691. createDebug.names = [];
  14692. createDebug.skips = [];
  14693. /**
  14694. * Map of special "%n" handling functions, for the debug "format" argument.
  14695. *
  14696. * Valid key names are a single, lower or upper-case letter, i.e. "n" and "N".
  14697. */
  14698. createDebug.formatters = {};
  14699. /**
  14700. * Selects a color for a debug namespace
  14701. * @param {String} namespace The namespace string for the debug instance to be colored
  14702. * @return {Number|String} An ANSI color code for the given namespace
  14703. * @api private
  14704. */
  14705. function selectColor(namespace) {
  14706. let hash = 0;
  14707. for (let i = 0; i < namespace.length; i++) {
  14708. hash = ((hash << 5) - hash) + namespace.charCodeAt(i);
  14709. hash |= 0; // Convert to 32bit integer
  14710. }
  14711. return createDebug.colors[Math.abs(hash) % createDebug.colors.length];
  14712. }
  14713. createDebug.selectColor = selectColor;
  14714. /**
  14715. * Create a debugger with the given `namespace`.
  14716. *
  14717. * @param {String} namespace
  14718. * @return {Function}
  14719. * @api public
  14720. */
  14721. function createDebug(namespace) {
  14722. let prevTime;
  14723. let enableOverride = null;
  14724. let namespacesCache;
  14725. let enabledCache;
  14726. function debug(...args) {
  14727. // Disabled?
  14728. if (!debug.enabled) {
  14729. return;
  14730. }
  14731. const self = debug;
  14732. // Set `diff` timestamp
  14733. const curr = Number(new Date());
  14734. const ms = curr - (prevTime || curr);
  14735. self.diff = ms;
  14736. self.prev = prevTime;
  14737. self.curr = curr;
  14738. prevTime = curr;
  14739. args[0] = createDebug.coerce(args[0]);
  14740. if (typeof args[0] !== 'string') {
  14741. // Anything else let's inspect with %O
  14742. args.unshift('%O');
  14743. }
  14744. // Apply any `formatters` transformations
  14745. let index = 0;
  14746. args[0] = args[0].replace(/%([a-zA-Z%])/g, (match, format) => {
  14747. // If we encounter an escaped % then don't increase the array index
  14748. if (match === '%%') {
  14749. return '%';
  14750. }
  14751. index++;
  14752. const formatter = createDebug.formatters[format];
  14753. if (typeof formatter === 'function') {
  14754. const val = args[index];
  14755. match = formatter.call(self, val);
  14756. // Now we need to remove `args[index]` since it's inlined in the `format`
  14757. args.splice(index, 1);
  14758. index--;
  14759. }
  14760. return match;
  14761. });
  14762. // Apply env-specific formatting (colors, etc.)
  14763. createDebug.formatArgs.call(self, args);
  14764. const logFn = self.log || createDebug.log;
  14765. logFn.apply(self, args);
  14766. }
  14767. debug.namespace = namespace;
  14768. debug.useColors = createDebug.useColors();
  14769. debug.color = createDebug.selectColor(namespace);
  14770. debug.extend = extend;
  14771. debug.destroy = createDebug.destroy; // XXX Temporary. Will be removed in the next major release.
  14772. Object.defineProperty(debug, 'enabled', {
  14773. enumerable: true,
  14774. configurable: false,
  14775. get: () => {
  14776. if (enableOverride !== null) {
  14777. return enableOverride;
  14778. }
  14779. if (namespacesCache !== createDebug.namespaces) {
  14780. namespacesCache = createDebug.namespaces;
  14781. enabledCache = createDebug.enabled(namespace);
  14782. }
  14783. return enabledCache;
  14784. },
  14785. set: v => {
  14786. enableOverride = v;
  14787. }
  14788. });
  14789. // Env-specific initialization logic for debug instances
  14790. if (typeof createDebug.init === 'function') {
  14791. createDebug.init(debug);
  14792. }
  14793. return debug;
  14794. }
  14795. function extend(namespace, delimiter) {
  14796. const newDebug = createDebug(this.namespace + (typeof delimiter === 'undefined' ? ':' : delimiter) + namespace);
  14797. newDebug.log = this.log;
  14798. return newDebug;
  14799. }
  14800. /**
  14801. * Enables a debug mode by namespaces. This can include modes
  14802. * separated by a colon and wildcards.
  14803. *
  14804. * @param {String} namespaces
  14805. * @api public
  14806. */
  14807. function enable(namespaces) {
  14808. createDebug.save(namespaces);
  14809. createDebug.namespaces = namespaces;
  14810. createDebug.names = [];
  14811. createDebug.skips = [];
  14812. let i;
  14813. const split = (typeof namespaces === 'string' ? namespaces : '').split(/[\s,]+/);
  14814. const len = split.length;
  14815. for (i = 0; i < len; i++) {
  14816. if (!split[i]) {
  14817. // ignore empty strings
  14818. continue;
  14819. }
  14820. namespaces = split[i].replace(/\*/g, '.*?');
  14821. if (namespaces[0] === '-') {
  14822. createDebug.skips.push(new RegExp('^' + namespaces.slice(1) + '$'));
  14823. } else {
  14824. createDebug.names.push(new RegExp('^' + namespaces + '$'));
  14825. }
  14826. }
  14827. }
  14828. /**
  14829. * Disable debug output.
  14830. *
  14831. * @return {String} namespaces
  14832. * @api public
  14833. */
  14834. function disable() {
  14835. const namespaces = [
  14836. ...createDebug.names.map(toNamespace),
  14837. ...createDebug.skips.map(toNamespace).map(namespace => '-' + namespace)
  14838. ].join(',');
  14839. createDebug.enable('');
  14840. return namespaces;
  14841. }
  14842. /**
  14843. * Returns true if the given mode name is enabled, false otherwise.
  14844. *
  14845. * @param {String} name
  14846. * @return {Boolean}
  14847. * @api public
  14848. */
  14849. function enabled(name) {
  14850. if (name[name.length - 1] === '*') {
  14851. return true;
  14852. }
  14853. let i;
  14854. let len;
  14855. for (i = 0, len = createDebug.skips.length; i < len; i++) {
  14856. if (createDebug.skips[i].test(name)) {
  14857. return false;
  14858. }
  14859. }
  14860. for (i = 0, len = createDebug.names.length; i < len; i++) {
  14861. if (createDebug.names[i].test(name)) {
  14862. return true;
  14863. }
  14864. }
  14865. return false;
  14866. }
  14867. /**
  14868. * Convert regexp to namespace
  14869. *
  14870. * @param {RegExp} regxep
  14871. * @return {String} namespace
  14872. * @api private
  14873. */
  14874. function toNamespace(regexp) {
  14875. return regexp.toString()
  14876. .substring(2, regexp.toString().length - 2)
  14877. .replace(/\.\*\?$/, '*');
  14878. }
  14879. /**
  14880. * Coerce `val`.
  14881. *
  14882. * @param {Mixed} val
  14883. * @return {Mixed}
  14884. * @api private
  14885. */
  14886. function coerce(val) {
  14887. if (val instanceof Error) {
  14888. return val.stack || val.message;
  14889. }
  14890. return val;
  14891. }
  14892. /**
  14893. * XXX DO NOT USE. This is a temporary stub function.
  14894. * XXX It WILL be removed in the next major release.
  14895. */
  14896. function destroy() {
  14897. console.warn('Instance method `debug.destroy()` is deprecated and no longer does anything. It will be removed in the next major version of `debug`.');
  14898. }
  14899. createDebug.enable(createDebug.load());
  14900. return createDebug;
  14901. }
  14902. common$b = setup;
  14903. return common$b;
  14904. }
  14905. /* eslint-env browser */
  14906. var hasRequiredBrowser$1;
  14907. function requireBrowser$1 () {
  14908. if (hasRequiredBrowser$1) return browser$3.exports;
  14909. hasRequiredBrowser$1 = 1;
  14910. (function (module, exports) {
  14911. /**
  14912. * This is the web browser implementation of `debug()`.
  14913. */
  14914. exports.formatArgs = formatArgs;
  14915. exports.save = save;
  14916. exports.load = load;
  14917. exports.useColors = useColors;
  14918. exports.storage = localstorage();
  14919. exports.destroy = (() => {
  14920. let warned = false;
  14921. return () => {
  14922. if (!warned) {
  14923. warned = true;
  14924. console.warn('Instance method `debug.destroy()` is deprecated and no longer does anything. It will be removed in the next major version of `debug`.');
  14925. }
  14926. };
  14927. })();
  14928. /**
  14929. * Colors.
  14930. */
  14931. exports.colors = [
  14932. '#0000CC',
  14933. '#0000FF',
  14934. '#0033CC',
  14935. '#0033FF',
  14936. '#0066CC',
  14937. '#0066FF',
  14938. '#0099CC',
  14939. '#0099FF',
  14940. '#00CC00',
  14941. '#00CC33',
  14942. '#00CC66',
  14943. '#00CC99',
  14944. '#00CCCC',
  14945. '#00CCFF',
  14946. '#3300CC',
  14947. '#3300FF',
  14948. '#3333CC',
  14949. '#3333FF',
  14950. '#3366CC',
  14951. '#3366FF',
  14952. '#3399CC',
  14953. '#3399FF',
  14954. '#33CC00',
  14955. '#33CC33',
  14956. '#33CC66',
  14957. '#33CC99',
  14958. '#33CCCC',
  14959. '#33CCFF',
  14960. '#6600CC',
  14961. '#6600FF',
  14962. '#6633CC',
  14963. '#6633FF',
  14964. '#66CC00',
  14965. '#66CC33',
  14966. '#9900CC',
  14967. '#9900FF',
  14968. '#9933CC',
  14969. '#9933FF',
  14970. '#99CC00',
  14971. '#99CC33',
  14972. '#CC0000',
  14973. '#CC0033',
  14974. '#CC0066',
  14975. '#CC0099',
  14976. '#CC00CC',
  14977. '#CC00FF',
  14978. '#CC3300',
  14979. '#CC3333',
  14980. '#CC3366',
  14981. '#CC3399',
  14982. '#CC33CC',
  14983. '#CC33FF',
  14984. '#CC6600',
  14985. '#CC6633',
  14986. '#CC9900',
  14987. '#CC9933',
  14988. '#CCCC00',
  14989. '#CCCC33',
  14990. '#FF0000',
  14991. '#FF0033',
  14992. '#FF0066',
  14993. '#FF0099',
  14994. '#FF00CC',
  14995. '#FF00FF',
  14996. '#FF3300',
  14997. '#FF3333',
  14998. '#FF3366',
  14999. '#FF3399',
  15000. '#FF33CC',
  15001. '#FF33FF',
  15002. '#FF6600',
  15003. '#FF6633',
  15004. '#FF9900',
  15005. '#FF9933',
  15006. '#FFCC00',
  15007. '#FFCC33'
  15008. ];
  15009. /**
  15010. * Currently only WebKit-based Web Inspectors, Firefox >= v31,
  15011. * and the Firebug extension (any Firefox version) are known
  15012. * to support "%c" CSS customizations.
  15013. *
  15014. * TODO: add a `localStorage` variable to explicitly enable/disable colors
  15015. */
  15016. // eslint-disable-next-line complexity
  15017. function useColors() {
  15018. // NB: In an Electron preload script, document will be defined but not fully
  15019. // initialized. Since we know we're in Chrome, we'll just detect this case
  15020. // explicitly
  15021. if (typeof window !== 'undefined' && window.process && (window.process.type === 'renderer' || window.process.__nwjs)) {
  15022. return true;
  15023. }
  15024. // Internet Explorer and Edge do not support colors.
  15025. if (typeof navigator !== 'undefined' && navigator.userAgent && navigator.userAgent.toLowerCase().match(/(edge|trident)\/(\d+)/)) {
  15026. return false;
  15027. }
  15028. let m;
  15029. // Is webkit? http://stackoverflow.com/a/16459606/376773
  15030. // document is undefined in react-native: https://github.com/facebook/react-native/pull/1632
  15031. return (typeof document !== 'undefined' && document.documentElement && document.documentElement.style && document.documentElement.style.WebkitAppearance) ||
  15032. // Is firebug? http://stackoverflow.com/a/398120/376773
  15033. (typeof window !== 'undefined' && window.console && (window.console.firebug || (window.console.exception && window.console.table))) ||
  15034. // Is firefox >= v31?
  15035. // https://developer.mozilla.org/en-US/docs/Tools/Web_Console#Styling_messages
  15036. (typeof navigator !== 'undefined' && navigator.userAgent && (m = navigator.userAgent.toLowerCase().match(/firefox\/(\d+)/)) && parseInt(m[1], 10) >= 31) ||
  15037. // Double check webkit in userAgent just in case we are in a worker
  15038. (typeof navigator !== 'undefined' && navigator.userAgent && navigator.userAgent.toLowerCase().match(/applewebkit\/(\d+)/));
  15039. }
  15040. /**
  15041. * Colorize log arguments if enabled.
  15042. *
  15043. * @api public
  15044. */
  15045. function formatArgs(args) {
  15046. args[0] = (this.useColors ? '%c' : '') +
  15047. this.namespace +
  15048. (this.useColors ? ' %c' : ' ') +
  15049. args[0] +
  15050. (this.useColors ? '%c ' : ' ') +
  15051. '+' + module.exports.humanize(this.diff);
  15052. if (!this.useColors) {
  15053. return;
  15054. }
  15055. const c = 'color: ' + this.color;
  15056. args.splice(1, 0, c, 'color: inherit');
  15057. // The final "%c" is somewhat tricky, because there could be other
  15058. // arguments passed either before or after the %c, so we need to
  15059. // figure out the correct index to insert the CSS into
  15060. let index = 0;
  15061. let lastC = 0;
  15062. args[0].replace(/%[a-zA-Z%]/g, match => {
  15063. if (match === '%%') {
  15064. return;
  15065. }
  15066. index++;
  15067. if (match === '%c') {
  15068. // We only are interested in the *last* %c
  15069. // (the user may have provided their own)
  15070. lastC = index;
  15071. }
  15072. });
  15073. args.splice(lastC, 0, c);
  15074. }
  15075. /**
  15076. * Invokes `console.debug()` when available.
  15077. * No-op when `console.debug` is not a "function".
  15078. * If `console.debug` is not available, falls back
  15079. * to `console.log`.
  15080. *
  15081. * @api public
  15082. */
  15083. exports.log = console.debug || console.log || (() => {});
  15084. /**
  15085. * Save `namespaces`.
  15086. *
  15087. * @param {String} namespaces
  15088. * @api private
  15089. */
  15090. function save(namespaces) {
  15091. try {
  15092. if (namespaces) {
  15093. exports.storage.setItem('debug', namespaces);
  15094. } else {
  15095. exports.storage.removeItem('debug');
  15096. }
  15097. } catch (error) {
  15098. // Swallow
  15099. // XXX (@Qix-) should we be logging these?
  15100. }
  15101. }
  15102. /**
  15103. * Load `namespaces`.
  15104. *
  15105. * @return {String} returns the previously persisted debug modes
  15106. * @api private
  15107. */
  15108. function load() {
  15109. let r;
  15110. try {
  15111. r = exports.storage.getItem('debug');
  15112. } catch (error) {
  15113. // Swallow
  15114. // XXX (@Qix-) should we be logging these?
  15115. }
  15116. // If debug isn't set in LS, and we're in Electron, try to load $DEBUG
  15117. if (!r && typeof process !== 'undefined' && 'env' in process) {
  15118. r = process.env.DEBUG;
  15119. }
  15120. return r;
  15121. }
  15122. /**
  15123. * Localstorage attempts to return the localstorage.
  15124. *
  15125. * This is necessary because safari throws
  15126. * when a user disables cookies/localstorage
  15127. * and you attempt to access it.
  15128. *
  15129. * @return {LocalStorage}
  15130. * @api private
  15131. */
  15132. function localstorage() {
  15133. try {
  15134. // TVMLKit (Apple TV JS Runtime) does not have a window object, just localStorage in the global context
  15135. // The Browser also has localStorage in the global context.
  15136. return localStorage;
  15137. } catch (error) {
  15138. // Swallow
  15139. // XXX (@Qix-) should we be logging these?
  15140. }
  15141. }
  15142. module.exports = requireCommon()(exports);
  15143. const {formatters} = module.exports;
  15144. /**
  15145. * Map %j to `JSON.stringify()`, since no Web Inspectors do that by default.
  15146. */
  15147. formatters.j = function (v) {
  15148. try {
  15149. return JSON.stringify(v);
  15150. } catch (error) {
  15151. return '[UnexpectedJSONParseError]: ' + error.message;
  15152. }
  15153. };
  15154. } (browser$3, browser$3.exports));
  15155. return browser$3.exports;
  15156. }
  15157. var node$1 = {exports: {}};
  15158. /**
  15159. * Module dependencies.
  15160. */
  15161. var hasRequiredNode$1;
  15162. function requireNode$1 () {
  15163. if (hasRequiredNode$1) return node$1.exports;
  15164. hasRequiredNode$1 = 1;
  15165. (function (module, exports) {
  15166. const tty = require$$0$3;
  15167. const util = require$$0$5;
  15168. /**
  15169. * This is the Node.js implementation of `debug()`.
  15170. */
  15171. exports.init = init;
  15172. exports.log = log;
  15173. exports.formatArgs = formatArgs;
  15174. exports.save = save;
  15175. exports.load = load;
  15176. exports.useColors = useColors;
  15177. exports.destroy = util.deprecate(
  15178. () => {},
  15179. 'Instance method `debug.destroy()` is deprecated and no longer does anything. It will be removed in the next major version of `debug`.'
  15180. );
  15181. /**
  15182. * Colors.
  15183. */
  15184. exports.colors = [6, 2, 3, 4, 5, 1];
  15185. try {
  15186. // Optional dependency (as in, doesn't need to be installed, NOT like optionalDependencies in package.json)
  15187. // eslint-disable-next-line import/no-extraneous-dependencies
  15188. const supportsColor = require('supports-color');
  15189. if (supportsColor && (supportsColor.stderr || supportsColor).level >= 2) {
  15190. exports.colors = [
  15191. 20,
  15192. 21,
  15193. 26,
  15194. 27,
  15195. 32,
  15196. 33,
  15197. 38,
  15198. 39,
  15199. 40,
  15200. 41,
  15201. 42,
  15202. 43,
  15203. 44,
  15204. 45,
  15205. 56,
  15206. 57,
  15207. 62,
  15208. 63,
  15209. 68,
  15210. 69,
  15211. 74,
  15212. 75,
  15213. 76,
  15214. 77,
  15215. 78,
  15216. 79,
  15217. 80,
  15218. 81,
  15219. 92,
  15220. 93,
  15221. 98,
  15222. 99,
  15223. 112,
  15224. 113,
  15225. 128,
  15226. 129,
  15227. 134,
  15228. 135,
  15229. 148,
  15230. 149,
  15231. 160,
  15232. 161,
  15233. 162,
  15234. 163,
  15235. 164,
  15236. 165,
  15237. 166,
  15238. 167,
  15239. 168,
  15240. 169,
  15241. 170,
  15242. 171,
  15243. 172,
  15244. 173,
  15245. 178,
  15246. 179,
  15247. 184,
  15248. 185,
  15249. 196,
  15250. 197,
  15251. 198,
  15252. 199,
  15253. 200,
  15254. 201,
  15255. 202,
  15256. 203,
  15257. 204,
  15258. 205,
  15259. 206,
  15260. 207,
  15261. 208,
  15262. 209,
  15263. 214,
  15264. 215,
  15265. 220,
  15266. 221
  15267. ];
  15268. }
  15269. } catch (error) {
  15270. // Swallow - we only care if `supports-color` is available; it doesn't have to be.
  15271. }
  15272. /**
  15273. * Build up the default `inspectOpts` object from the environment variables.
  15274. *
  15275. * $ DEBUG_COLORS=no DEBUG_DEPTH=10 DEBUG_SHOW_HIDDEN=enabled node script.js
  15276. */
  15277. exports.inspectOpts = Object.keys(process.env).filter(key => {
  15278. return /^debug_/i.test(key);
  15279. }).reduce((obj, key) => {
  15280. // Camel-case
  15281. const prop = key
  15282. .substring(6)
  15283. .toLowerCase()
  15284. .replace(/_([a-z])/g, (_, k) => {
  15285. return k.toUpperCase();
  15286. });
  15287. // Coerce string value into JS value
  15288. let val = process.env[key];
  15289. if (/^(yes|on|true|enabled)$/i.test(val)) {
  15290. val = true;
  15291. } else if (/^(no|off|false|disabled)$/i.test(val)) {
  15292. val = false;
  15293. } else if (val === 'null') {
  15294. val = null;
  15295. } else {
  15296. val = Number(val);
  15297. }
  15298. obj[prop] = val;
  15299. return obj;
  15300. }, {});
  15301. /**
  15302. * Is stdout a TTY? Colored output is enabled when `true`.
  15303. */
  15304. function useColors() {
  15305. return 'colors' in exports.inspectOpts ?
  15306. Boolean(exports.inspectOpts.colors) :
  15307. tty.isatty(process.stderr.fd);
  15308. }
  15309. /**
  15310. * Adds ANSI color escape codes if enabled.
  15311. *
  15312. * @api public
  15313. */
  15314. function formatArgs(args) {
  15315. const {namespace: name, useColors} = this;
  15316. if (useColors) {
  15317. const c = this.color;
  15318. const colorCode = '\u001B[3' + (c < 8 ? c : '8;5;' + c);
  15319. const prefix = ` ${colorCode};1m${name} \u001B[0m`;
  15320. args[0] = prefix + args[0].split('\n').join('\n' + prefix);
  15321. args.push(colorCode + 'm+' + module.exports.humanize(this.diff) + '\u001B[0m');
  15322. } else {
  15323. args[0] = getDate() + name + ' ' + args[0];
  15324. }
  15325. }
  15326. function getDate() {
  15327. if (exports.inspectOpts.hideDate) {
  15328. return '';
  15329. }
  15330. return new Date().toISOString() + ' ';
  15331. }
  15332. /**
  15333. * Invokes `util.formatWithOptions()` with the specified arguments and writes to stderr.
  15334. */
  15335. function log(...args) {
  15336. return process.stderr.write(util.formatWithOptions(exports.inspectOpts, ...args) + '\n');
  15337. }
  15338. /**
  15339. * Save `namespaces`.
  15340. *
  15341. * @param {String} namespaces
  15342. * @api private
  15343. */
  15344. function save(namespaces) {
  15345. if (namespaces) {
  15346. process.env.DEBUG = namespaces;
  15347. } else {
  15348. // If you set a process.env field to null or undefined, it gets cast to the
  15349. // string 'null' or 'undefined'. Just delete instead.
  15350. delete process.env.DEBUG;
  15351. }
  15352. }
  15353. /**
  15354. * Load `namespaces`.
  15355. *
  15356. * @return {String} returns the previously persisted debug modes
  15357. * @api private
  15358. */
  15359. function load() {
  15360. return process.env.DEBUG;
  15361. }
  15362. /**
  15363. * Init logic for `debug` instances.
  15364. *
  15365. * Create a new `inspectOpts` object in case `useColors` is set
  15366. * differently for a particular `debug` instance.
  15367. */
  15368. function init(debug) {
  15369. debug.inspectOpts = {};
  15370. const keys = Object.keys(exports.inspectOpts);
  15371. for (let i = 0; i < keys.length; i++) {
  15372. debug.inspectOpts[keys[i]] = exports.inspectOpts[keys[i]];
  15373. }
  15374. }
  15375. module.exports = requireCommon()(exports);
  15376. const {formatters} = module.exports;
  15377. /**
  15378. * Map %o to `util.inspect()`, all on a single line.
  15379. */
  15380. formatters.o = function (v) {
  15381. this.inspectOpts.colors = this.useColors;
  15382. return util.inspect(v, this.inspectOpts)
  15383. .split('\n')
  15384. .map(str => str.trim())
  15385. .join(' ');
  15386. };
  15387. /**
  15388. * Map %O to `util.inspect()`, allowing multiple lines if needed.
  15389. */
  15390. formatters.O = function (v) {
  15391. this.inspectOpts.colors = this.useColors;
  15392. return util.inspect(v, this.inspectOpts);
  15393. };
  15394. } (node$1, node$1.exports));
  15395. return node$1.exports;
  15396. }
  15397. /**
  15398. * Detect Electron renderer / nwjs process, which is node, but we should
  15399. * treat as a browser.
  15400. */
  15401. if (typeof process === 'undefined' || process.type === 'renderer' || process.browser === true || process.__nwjs) {
  15402. src$3.exports = requireBrowser$1();
  15403. } else {
  15404. src$3.exports = requireNode$1();
  15405. }
  15406. var srcExports$1 = src$3.exports;
  15407. var debug$i = /*@__PURE__*/getDefaultExportFromCjs(srcExports$1);
  15408. let pnp;
  15409. if (process.versions.pnp) {
  15410. try {
  15411. pnp = createRequire$1(import.meta.url)("pnpapi");
  15412. } catch {
  15413. }
  15414. }
  15415. function invalidatePackageData(packageCache, pkgPath) {
  15416. const pkgDir = normalizePath$3(path$n.dirname(pkgPath));
  15417. packageCache.forEach((pkg, cacheKey) => {
  15418. if (pkg.dir === pkgDir) {
  15419. packageCache.delete(cacheKey);
  15420. }
  15421. });
  15422. }
  15423. function resolvePackageData(pkgName, basedir, preserveSymlinks = false, packageCache) {
  15424. if (pnp) {
  15425. const cacheKey = getRpdCacheKey(pkgName, basedir, preserveSymlinks);
  15426. if (packageCache?.has(cacheKey)) return packageCache.get(cacheKey);
  15427. try {
  15428. const pkg = pnp.resolveToUnqualified(pkgName, basedir, {
  15429. considerBuiltins: false
  15430. });
  15431. if (!pkg) return null;
  15432. const pkgData = loadPackageData(path$n.join(pkg, "package.json"));
  15433. packageCache?.set(cacheKey, pkgData);
  15434. return pkgData;
  15435. } catch {
  15436. return null;
  15437. }
  15438. }
  15439. const originalBasedir = basedir;
  15440. while (basedir) {
  15441. if (packageCache) {
  15442. const cached = getRpdCache(
  15443. packageCache,
  15444. pkgName,
  15445. basedir,
  15446. originalBasedir,
  15447. preserveSymlinks
  15448. );
  15449. if (cached) return cached;
  15450. }
  15451. const pkg = path$n.join(basedir, "node_modules", pkgName, "package.json");
  15452. try {
  15453. if (fs__default.existsSync(pkg)) {
  15454. const pkgPath = preserveSymlinks ? pkg : safeRealpathSync(pkg);
  15455. const pkgData = loadPackageData(pkgPath);
  15456. if (packageCache) {
  15457. setRpdCache(
  15458. packageCache,
  15459. pkgData,
  15460. pkgName,
  15461. basedir,
  15462. originalBasedir,
  15463. preserveSymlinks
  15464. );
  15465. }
  15466. return pkgData;
  15467. }
  15468. } catch {
  15469. }
  15470. const nextBasedir = path$n.dirname(basedir);
  15471. if (nextBasedir === basedir) break;
  15472. basedir = nextBasedir;
  15473. }
  15474. return null;
  15475. }
  15476. function findNearestPackageData(basedir, packageCache) {
  15477. const originalBasedir = basedir;
  15478. while (basedir) {
  15479. if (packageCache) {
  15480. const cached = getFnpdCache(packageCache, basedir, originalBasedir);
  15481. if (cached) return cached;
  15482. }
  15483. const pkgPath = path$n.join(basedir, "package.json");
  15484. if (tryStatSync(pkgPath)?.isFile()) {
  15485. try {
  15486. const pkgData = loadPackageData(pkgPath);
  15487. if (packageCache) {
  15488. setFnpdCache(packageCache, pkgData, basedir, originalBasedir);
  15489. }
  15490. return pkgData;
  15491. } catch {
  15492. }
  15493. }
  15494. const nextBasedir = path$n.dirname(basedir);
  15495. if (nextBasedir === basedir) break;
  15496. basedir = nextBasedir;
  15497. }
  15498. return null;
  15499. }
  15500. function findNearestMainPackageData(basedir, packageCache) {
  15501. const nearestPackage = findNearestPackageData(basedir, packageCache);
  15502. return nearestPackage && (nearestPackage.data.name ? nearestPackage : findNearestMainPackageData(
  15503. path$n.dirname(nearestPackage.dir),
  15504. packageCache
  15505. ));
  15506. }
  15507. function loadPackageData(pkgPath) {
  15508. const data = JSON.parse(fs__default.readFileSync(pkgPath, "utf-8"));
  15509. const pkgDir = normalizePath$3(path$n.dirname(pkgPath));
  15510. const { sideEffects } = data;
  15511. let hasSideEffects;
  15512. if (typeof sideEffects === "boolean") {
  15513. hasSideEffects = () => sideEffects;
  15514. } else if (Array.isArray(sideEffects)) {
  15515. if (sideEffects.length <= 0) {
  15516. hasSideEffects = () => false;
  15517. } else {
  15518. const finalPackageSideEffects = sideEffects.map((sideEffect) => {
  15519. if (sideEffect.includes("/")) {
  15520. return sideEffect;
  15521. }
  15522. return `**/${sideEffect}`;
  15523. });
  15524. hasSideEffects = createFilter(finalPackageSideEffects, null, {
  15525. resolve: pkgDir
  15526. });
  15527. }
  15528. } else {
  15529. hasSideEffects = () => null;
  15530. }
  15531. const pkg = {
  15532. dir: pkgDir,
  15533. data,
  15534. hasSideEffects,
  15535. webResolvedImports: {},
  15536. nodeResolvedImports: {},
  15537. setResolvedCache(key, entry, targetWeb) {
  15538. if (targetWeb) {
  15539. pkg.webResolvedImports[key] = entry;
  15540. } else {
  15541. pkg.nodeResolvedImports[key] = entry;
  15542. }
  15543. },
  15544. getResolvedCache(key, targetWeb) {
  15545. if (targetWeb) {
  15546. return pkg.webResolvedImports[key];
  15547. } else {
  15548. return pkg.nodeResolvedImports[key];
  15549. }
  15550. }
  15551. };
  15552. return pkg;
  15553. }
  15554. function watchPackageDataPlugin(packageCache) {
  15555. const watchQueue = /* @__PURE__ */ new Set();
  15556. const watchedDirs = /* @__PURE__ */ new Set();
  15557. const watchFileStub = (id) => {
  15558. watchQueue.add(id);
  15559. };
  15560. let watchFile = watchFileStub;
  15561. const setPackageData = packageCache.set.bind(packageCache);
  15562. packageCache.set = (id, pkg) => {
  15563. if (!isInNodeModules$1(pkg.dir) && !watchedDirs.has(pkg.dir)) {
  15564. watchedDirs.add(pkg.dir);
  15565. watchFile(path$n.join(pkg.dir, "package.json"));
  15566. }
  15567. return setPackageData(id, pkg);
  15568. };
  15569. return {
  15570. name: "vite:watch-package-data",
  15571. buildStart() {
  15572. watchFile = this.addWatchFile.bind(this);
  15573. watchQueue.forEach(watchFile);
  15574. watchQueue.clear();
  15575. },
  15576. buildEnd() {
  15577. watchFile = watchFileStub;
  15578. },
  15579. watchChange(id) {
  15580. if (id.endsWith("/package.json")) {
  15581. invalidatePackageData(packageCache, path$n.normalize(id));
  15582. }
  15583. }
  15584. };
  15585. }
  15586. function getRpdCache(packageCache, pkgName, basedir, originalBasedir, preserveSymlinks) {
  15587. const cacheKey = getRpdCacheKey(pkgName, basedir, preserveSymlinks);
  15588. const pkgData = packageCache.get(cacheKey);
  15589. if (pkgData) {
  15590. traverseBetweenDirs(originalBasedir, basedir, (dir) => {
  15591. packageCache.set(getRpdCacheKey(pkgName, dir, preserveSymlinks), pkgData);
  15592. });
  15593. return pkgData;
  15594. }
  15595. }
  15596. function setRpdCache(packageCache, pkgData, pkgName, basedir, originalBasedir, preserveSymlinks) {
  15597. packageCache.set(getRpdCacheKey(pkgName, basedir, preserveSymlinks), pkgData);
  15598. traverseBetweenDirs(originalBasedir, basedir, (dir) => {
  15599. packageCache.set(getRpdCacheKey(pkgName, dir, preserveSymlinks), pkgData);
  15600. });
  15601. }
  15602. function getRpdCacheKey(pkgName, basedir, preserveSymlinks) {
  15603. return `rpd_${pkgName}_${basedir}_${preserveSymlinks}`;
  15604. }
  15605. function getFnpdCache(packageCache, basedir, originalBasedir) {
  15606. const cacheKey = getFnpdCacheKey(basedir);
  15607. const pkgData = packageCache.get(cacheKey);
  15608. if (pkgData) {
  15609. traverseBetweenDirs(originalBasedir, basedir, (dir) => {
  15610. packageCache.set(getFnpdCacheKey(dir), pkgData);
  15611. });
  15612. return pkgData;
  15613. }
  15614. }
  15615. function setFnpdCache(packageCache, pkgData, basedir, originalBasedir) {
  15616. packageCache.set(getFnpdCacheKey(basedir), pkgData);
  15617. traverseBetweenDirs(originalBasedir, basedir, (dir) => {
  15618. packageCache.set(getFnpdCacheKey(dir), pkgData);
  15619. });
  15620. }
  15621. function getFnpdCacheKey(basedir) {
  15622. return `fnpd_${basedir}`;
  15623. }
  15624. function traverseBetweenDirs(longerDir, shorterDir, cb) {
  15625. while (longerDir !== shorterDir) {
  15626. cb(longerDir);
  15627. longerDir = path$n.dirname(longerDir);
  15628. }
  15629. }
  15630. const createFilter = createFilter$1;
  15631. const replaceSlashOrColonRE = /[/:]/g;
  15632. const replaceDotRE = /\./g;
  15633. const replaceNestedIdRE = /\s*>\s*/g;
  15634. const replaceHashRE = /#/g;
  15635. const flattenId = (id) => {
  15636. const flatId = limitFlattenIdLength(
  15637. id.replace(replaceSlashOrColonRE, "_").replace(replaceDotRE, "__").replace(replaceNestedIdRE, "___").replace(replaceHashRE, "____")
  15638. );
  15639. return flatId;
  15640. };
  15641. const FLATTEN_ID_HASH_LENGTH = 8;
  15642. const FLATTEN_ID_MAX_FILE_LENGTH = 170;
  15643. const limitFlattenIdLength = (id, limit = FLATTEN_ID_MAX_FILE_LENGTH) => {
  15644. if (id.length <= limit) {
  15645. return id;
  15646. }
  15647. return id.slice(0, limit - (FLATTEN_ID_HASH_LENGTH + 1)) + "_" + getHash(id);
  15648. };
  15649. const normalizeId = (id) => id.replace(replaceNestedIdRE, " > ");
  15650. const NODE_BUILTIN_NAMESPACE = "node:";
  15651. const NPM_BUILTIN_NAMESPACE = "npm:";
  15652. const BUN_BUILTIN_NAMESPACE = "bun:";
  15653. const nodeBuiltins = builtinModules.filter((id) => !id.includes(":"));
  15654. function isBuiltin(id) {
  15655. if (process.versions.deno && id.startsWith(NPM_BUILTIN_NAMESPACE)) return true;
  15656. if (process.versions.bun && id.startsWith(BUN_BUILTIN_NAMESPACE)) return true;
  15657. return isNodeBuiltin(id);
  15658. }
  15659. function isNodeBuiltin(id) {
  15660. if (id.startsWith(NODE_BUILTIN_NAMESPACE)) return true;
  15661. return nodeBuiltins.includes(id);
  15662. }
  15663. function isInNodeModules$1(id) {
  15664. return id.includes("node_modules");
  15665. }
  15666. function moduleListContains(moduleList, id) {
  15667. return moduleList?.some(
  15668. (m) => m === id || id.startsWith(withTrailingSlash(m))
  15669. );
  15670. }
  15671. function isOptimizable(id, optimizeDeps) {
  15672. const { extensions } = optimizeDeps;
  15673. return OPTIMIZABLE_ENTRY_RE.test(id) || (extensions?.some((ext) => id.endsWith(ext)) ?? false);
  15674. }
  15675. const bareImportRE = /^(?![a-zA-Z]:)[\w@](?!.*:\/\/)/;
  15676. const deepImportRE = /^([^@][^/]*)\/|^(@[^/]+\/[^/]+)\//;
  15677. const _require$1 = createRequire$1(import.meta.url);
  15678. function resolveDependencyVersion(dep, pkgRelativePath = "../../package.json") {
  15679. const pkgPath = path$n.resolve(_require$1.resolve(dep), pkgRelativePath);
  15680. return JSON.parse(fs__default.readFileSync(pkgPath, "utf-8")).version;
  15681. }
  15682. const rollupVersion = resolveDependencyVersion("rollup");
  15683. const filter = process.env.VITE_DEBUG_FILTER;
  15684. const DEBUG = process.env.DEBUG;
  15685. function createDebugger(namespace, options = {}) {
  15686. const log = debug$i(namespace);
  15687. const { onlyWhenFocused } = options;
  15688. let enabled = log.enabled;
  15689. if (enabled && onlyWhenFocused) {
  15690. const ns = typeof onlyWhenFocused === "string" ? onlyWhenFocused : namespace;
  15691. enabled = !!DEBUG?.includes(ns);
  15692. }
  15693. if (enabled) {
  15694. return (...args) => {
  15695. if (!filter || args.some((a) => a?.includes?.(filter))) {
  15696. log(...args);
  15697. }
  15698. };
  15699. }
  15700. }
  15701. function testCaseInsensitiveFS() {
  15702. if (!CLIENT_ENTRY.endsWith("client.mjs")) {
  15703. throw new Error(
  15704. `cannot test case insensitive FS, CLIENT_ENTRY const doesn't contain client.mjs`
  15705. );
  15706. }
  15707. if (!fs__default.existsSync(CLIENT_ENTRY)) {
  15708. throw new Error(
  15709. "cannot test case insensitive FS, CLIENT_ENTRY does not point to an existing file: " + CLIENT_ENTRY
  15710. );
  15711. }
  15712. return fs__default.existsSync(CLIENT_ENTRY.replace("client.mjs", "cLiEnT.mjs"));
  15713. }
  15714. const urlCanParse = (
  15715. // eslint-disable-next-line n/no-unsupported-features/node-builtins
  15716. URL$3.canParse ?? // URL.canParse is supported from Node.js 18.17.0+, 20.0.0+
  15717. ((path2, base) => {
  15718. try {
  15719. new URL$3(path2, base);
  15720. return true;
  15721. } catch {
  15722. return false;
  15723. }
  15724. })
  15725. );
  15726. const isCaseInsensitiveFS = testCaseInsensitiveFS();
  15727. const VOLUME_RE = /^[A-Z]:/i;
  15728. function normalizePath$3(id) {
  15729. return path$n.posix.normalize(isWindows$3 ? slash$1(id) : id);
  15730. }
  15731. function fsPathFromId(id) {
  15732. const fsPath = normalizePath$3(
  15733. id.startsWith(FS_PREFIX) ? id.slice(FS_PREFIX.length) : id
  15734. );
  15735. return fsPath[0] === "/" || VOLUME_RE.test(fsPath) ? fsPath : `/${fsPath}`;
  15736. }
  15737. function fsPathFromUrl(url) {
  15738. return fsPathFromId(cleanUrl(url));
  15739. }
  15740. function isParentDirectory(dir, file) {
  15741. dir = withTrailingSlash(dir);
  15742. return file.startsWith(dir) || isCaseInsensitiveFS && file.toLowerCase().startsWith(dir.toLowerCase());
  15743. }
  15744. function isSameFileUri(file1, file2) {
  15745. return file1 === file2 || isCaseInsensitiveFS && file1.toLowerCase() === file2.toLowerCase();
  15746. }
  15747. const externalRE = /^(https?:)?\/\//;
  15748. const isExternalUrl = (url) => externalRE.test(url);
  15749. const dataUrlRE = /^\s*data:/i;
  15750. const isDataUrl = (url) => dataUrlRE.test(url);
  15751. const virtualModuleRE = /^virtual-module:.*/;
  15752. const virtualModulePrefix = "virtual-module:";
  15753. const knownJsSrcRE = /\.(?:[jt]sx?|m[jt]s|vue|marko|svelte|astro|imba|mdx)(?:$|\?)/;
  15754. const isJSRequest = (url) => {
  15755. url = cleanUrl(url);
  15756. if (knownJsSrcRE.test(url)) {
  15757. return true;
  15758. }
  15759. if (!path$n.extname(url) && url[url.length - 1] !== "/") {
  15760. return true;
  15761. }
  15762. return false;
  15763. };
  15764. const knownTsRE = /\.(?:ts|mts|cts|tsx)(?:$|\?)/;
  15765. const isTsRequest = (url) => knownTsRE.test(url);
  15766. const importQueryRE = /(\?|&)import=?(?:&|$)/;
  15767. const directRequestRE$1 = /(\?|&)direct=?(?:&|$)/;
  15768. const internalPrefixes = [
  15769. FS_PREFIX,
  15770. VALID_ID_PREFIX,
  15771. CLIENT_PUBLIC_PATH,
  15772. ENV_PUBLIC_PATH
  15773. ];
  15774. const InternalPrefixRE = new RegExp(`^(?:${internalPrefixes.join("|")})`);
  15775. const trailingSeparatorRE = /[?&]$/;
  15776. const isImportRequest = (url) => importQueryRE.test(url);
  15777. const isInternalRequest = (url) => InternalPrefixRE.test(url);
  15778. function removeImportQuery(url) {
  15779. return url.replace(importQueryRE, "$1").replace(trailingSeparatorRE, "");
  15780. }
  15781. function removeDirectQuery(url) {
  15782. return url.replace(directRequestRE$1, "$1").replace(trailingSeparatorRE, "");
  15783. }
  15784. const urlRE = /(\?|&)url(?:&|$)/;
  15785. const rawRE = /(\?|&)raw(?:&|$)/;
  15786. function removeUrlQuery(url) {
  15787. return url.replace(urlRE, "$1").replace(trailingSeparatorRE, "");
  15788. }
  15789. const replacePercentageRE = /%/g;
  15790. function injectQuery(url, queryToInject) {
  15791. const resolvedUrl = new URL$3(
  15792. url.replace(replacePercentageRE, "%25"),
  15793. "relative:///"
  15794. );
  15795. const { search, hash } = resolvedUrl;
  15796. let pathname = cleanUrl(url);
  15797. pathname = isWindows$3 ? slash$1(pathname) : pathname;
  15798. return `${pathname}?${queryToInject}${search ? `&` + search.slice(1) : ""}${hash ?? ""}`;
  15799. }
  15800. const timestampRE = /\bt=\d{13}&?\b/;
  15801. function removeTimestampQuery(url) {
  15802. return url.replace(timestampRE, "").replace(trailingSeparatorRE, "");
  15803. }
  15804. async function asyncReplace(input, re, replacer) {
  15805. let match;
  15806. let remaining = input;
  15807. let rewritten = "";
  15808. while (match = re.exec(remaining)) {
  15809. rewritten += remaining.slice(0, match.index);
  15810. rewritten += await replacer(match);
  15811. remaining = remaining.slice(match.index + match[0].length);
  15812. }
  15813. rewritten += remaining;
  15814. return rewritten;
  15815. }
  15816. function timeFrom(start, subtract = 0) {
  15817. const time = performance$1.now() - start - subtract;
  15818. const timeString = (time.toFixed(2) + `ms`).padEnd(5, " ");
  15819. if (time < 10) {
  15820. return colors$1.green(timeString);
  15821. } else if (time < 50) {
  15822. return colors$1.yellow(timeString);
  15823. } else {
  15824. return colors$1.red(timeString);
  15825. }
  15826. }
  15827. function prettifyUrl(url, root) {
  15828. url = removeTimestampQuery(url);
  15829. const isAbsoluteFile = url.startsWith(root);
  15830. if (isAbsoluteFile || url.startsWith(FS_PREFIX)) {
  15831. const file = path$n.posix.relative(
  15832. root,
  15833. isAbsoluteFile ? url : fsPathFromId(url)
  15834. );
  15835. return colors$1.dim(file);
  15836. } else {
  15837. return colors$1.dim(url);
  15838. }
  15839. }
  15840. function isObject$1(value) {
  15841. return Object.prototype.toString.call(value) === "[object Object]";
  15842. }
  15843. function isDefined(value) {
  15844. return value != null;
  15845. }
  15846. function tryStatSync(file) {
  15847. try {
  15848. return fs__default.statSync(file, { throwIfNoEntry: false });
  15849. } catch {
  15850. }
  15851. }
  15852. function lookupFile(dir, fileNames) {
  15853. while (dir) {
  15854. for (const fileName of fileNames) {
  15855. const fullPath = path$n.join(dir, fileName);
  15856. if (tryStatSync(fullPath)?.isFile()) return fullPath;
  15857. }
  15858. const parentDir = path$n.dirname(dir);
  15859. if (parentDir === dir) return;
  15860. dir = parentDir;
  15861. }
  15862. }
  15863. function isFilePathESM(filePath, packageCache) {
  15864. if (/\.m[jt]s$/.test(filePath)) {
  15865. return true;
  15866. } else if (/\.c[jt]s$/.test(filePath)) {
  15867. return false;
  15868. } else {
  15869. try {
  15870. const pkg = findNearestPackageData(path$n.dirname(filePath), packageCache);
  15871. return pkg?.data.type === "module";
  15872. } catch {
  15873. return false;
  15874. }
  15875. }
  15876. }
  15877. const splitRE = /\r?\n/g;
  15878. const range = 2;
  15879. function pad$1(source, n = 2) {
  15880. const lines = source.split(splitRE);
  15881. return lines.map((l) => ` `.repeat(n) + l).join(`
  15882. `);
  15883. }
  15884. function posToNumber(source, pos) {
  15885. if (typeof pos === "number") return pos;
  15886. const lines = source.split(splitRE);
  15887. const { line, column } = pos;
  15888. let start = 0;
  15889. for (let i = 0; i < line - 1 && i < lines.length; i++) {
  15890. start += lines[i].length + 1;
  15891. }
  15892. return start + column;
  15893. }
  15894. function numberToPos(source, offset) {
  15895. if (typeof offset !== "number") return offset;
  15896. if (offset > source.length) {
  15897. throw new Error(
  15898. `offset is longer than source length! offset ${offset} > length ${source.length}`
  15899. );
  15900. }
  15901. const lines = source.split(splitRE);
  15902. let counted = 0;
  15903. let line = 0;
  15904. let column = 0;
  15905. for (; line < lines.length; line++) {
  15906. const lineLength = lines[line].length + 1;
  15907. if (counted + lineLength >= offset) {
  15908. column = offset - counted + 1;
  15909. break;
  15910. }
  15911. counted += lineLength;
  15912. }
  15913. return { line: line + 1, column };
  15914. }
  15915. function generateCodeFrame(source, start = 0, end) {
  15916. start = Math.max(posToNumber(source, start), 0);
  15917. end = Math.min(
  15918. end !== void 0 ? posToNumber(source, end) : start,
  15919. source.length
  15920. );
  15921. const lines = source.split(splitRE);
  15922. let count = 0;
  15923. const res = [];
  15924. for (let i = 0; i < lines.length; i++) {
  15925. count += lines[i].length;
  15926. if (count >= start) {
  15927. for (let j = i - range; j <= i + range || end > count; j++) {
  15928. if (j < 0 || j >= lines.length) continue;
  15929. const line = j + 1;
  15930. res.push(
  15931. `${line}${" ".repeat(Math.max(3 - String(line).length, 0))}| ${lines[j]}`
  15932. );
  15933. const lineLength = lines[j].length;
  15934. if (j === i) {
  15935. const pad2 = Math.max(start - (count - lineLength), 0);
  15936. const length = Math.max(
  15937. 1,
  15938. end > count ? lineLength - pad2 : end - start
  15939. );
  15940. res.push(` | ` + " ".repeat(pad2) + "^".repeat(length));
  15941. } else if (j > i) {
  15942. if (end > count) {
  15943. const length = Math.max(Math.min(end - count, lineLength), 1);
  15944. res.push(` | ` + "^".repeat(length));
  15945. }
  15946. count += lineLength + 1;
  15947. }
  15948. }
  15949. break;
  15950. }
  15951. count++;
  15952. }
  15953. return res.join("\n");
  15954. }
  15955. function isFileReadable(filename) {
  15956. if (!tryStatSync(filename)) {
  15957. return false;
  15958. }
  15959. try {
  15960. fs__default.accessSync(filename, fs__default.constants.R_OK);
  15961. return true;
  15962. } catch {
  15963. return false;
  15964. }
  15965. }
  15966. const splitFirstDirRE = /(.+?)[\\/](.+)/;
  15967. function emptyDir(dir, skip) {
  15968. const skipInDir = [];
  15969. let nested = null;
  15970. if (skip?.length) {
  15971. for (const file of skip) {
  15972. if (path$n.dirname(file) !== ".") {
  15973. const matched = splitFirstDirRE.exec(file);
  15974. if (matched) {
  15975. nested ??= /* @__PURE__ */ new Map();
  15976. const [, nestedDir, skipPath] = matched;
  15977. let nestedSkip = nested.get(nestedDir);
  15978. if (!nestedSkip) {
  15979. nestedSkip = [];
  15980. nested.set(nestedDir, nestedSkip);
  15981. }
  15982. if (!nestedSkip.includes(skipPath)) {
  15983. nestedSkip.push(skipPath);
  15984. }
  15985. }
  15986. } else {
  15987. skipInDir.push(file);
  15988. }
  15989. }
  15990. }
  15991. for (const file of fs__default.readdirSync(dir)) {
  15992. if (skipInDir.includes(file)) {
  15993. continue;
  15994. }
  15995. if (nested?.has(file)) {
  15996. emptyDir(path$n.resolve(dir, file), nested.get(file));
  15997. } else {
  15998. fs__default.rmSync(path$n.resolve(dir, file), { recursive: true, force: true });
  15999. }
  16000. }
  16001. }
  16002. function copyDir(srcDir, destDir) {
  16003. fs__default.mkdirSync(destDir, { recursive: true });
  16004. for (const file of fs__default.readdirSync(srcDir)) {
  16005. const srcFile = path$n.resolve(srcDir, file);
  16006. if (srcFile === destDir) {
  16007. continue;
  16008. }
  16009. const destFile = path$n.resolve(destDir, file);
  16010. const stat = fs__default.statSync(srcFile);
  16011. if (stat.isDirectory()) {
  16012. copyDir(srcFile, destFile);
  16013. } else {
  16014. fs__default.copyFileSync(srcFile, destFile);
  16015. }
  16016. }
  16017. }
  16018. const ERR_SYMLINK_IN_RECURSIVE_READDIR = "ERR_SYMLINK_IN_RECURSIVE_READDIR";
  16019. async function recursiveReaddir(dir) {
  16020. if (!fs__default.existsSync(dir)) {
  16021. return [];
  16022. }
  16023. let dirents;
  16024. try {
  16025. dirents = await fsp.readdir(dir, { withFileTypes: true });
  16026. } catch (e) {
  16027. if (e.code === "EACCES") {
  16028. return [];
  16029. }
  16030. throw e;
  16031. }
  16032. if (dirents.some((dirent) => dirent.isSymbolicLink())) {
  16033. const err = new Error(
  16034. "Symbolic links are not supported in recursiveReaddir"
  16035. );
  16036. err.code = ERR_SYMLINK_IN_RECURSIVE_READDIR;
  16037. throw err;
  16038. }
  16039. const files = await Promise.all(
  16040. dirents.map((dirent) => {
  16041. const res = path$n.resolve(dir, dirent.name);
  16042. return dirent.isDirectory() ? recursiveReaddir(res) : normalizePath$3(res);
  16043. })
  16044. );
  16045. return files.flat(1);
  16046. }
  16047. let safeRealpathSync = isWindows$3 ? windowsSafeRealPathSync : fs__default.realpathSync.native;
  16048. const windowsNetworkMap = /* @__PURE__ */ new Map();
  16049. function windowsMappedRealpathSync(path2) {
  16050. const realPath = fs__default.realpathSync.native(path2);
  16051. if (realPath.startsWith("\\\\")) {
  16052. for (const [network, volume] of windowsNetworkMap) {
  16053. if (realPath.startsWith(network)) return realPath.replace(network, volume);
  16054. }
  16055. }
  16056. return realPath;
  16057. }
  16058. const parseNetUseRE = /^\w* +(\w:) +([^ ]+)\s/;
  16059. let firstSafeRealPathSyncRun = false;
  16060. function windowsSafeRealPathSync(path2) {
  16061. if (!firstSafeRealPathSyncRun) {
  16062. optimizeSafeRealPathSync();
  16063. firstSafeRealPathSyncRun = true;
  16064. }
  16065. return fs__default.realpathSync(path2);
  16066. }
  16067. function optimizeSafeRealPathSync() {
  16068. const nodeVersion = process.versions.node.split(".").map(Number);
  16069. if (nodeVersion[0] < 18 || nodeVersion[0] === 18 && nodeVersion[1] < 10) {
  16070. safeRealpathSync = fs__default.realpathSync;
  16071. return;
  16072. }
  16073. try {
  16074. fs__default.realpathSync.native(path$n.resolve("./"));
  16075. } catch (error) {
  16076. if (error.message.includes("EISDIR: illegal operation on a directory")) {
  16077. safeRealpathSync = fs__default.realpathSync;
  16078. return;
  16079. }
  16080. }
  16081. exec("net use", (error, stdout) => {
  16082. if (error) return;
  16083. const lines = stdout.split("\n");
  16084. for (const line of lines) {
  16085. const m = parseNetUseRE.exec(line);
  16086. if (m) windowsNetworkMap.set(m[2], m[1]);
  16087. }
  16088. if (windowsNetworkMap.size === 0) {
  16089. safeRealpathSync = fs__default.realpathSync.native;
  16090. } else {
  16091. safeRealpathSync = windowsMappedRealpathSync;
  16092. }
  16093. });
  16094. }
  16095. function ensureWatchedFile(watcher, file, root) {
  16096. if (file && // only need to watch if out of root
  16097. !file.startsWith(withTrailingSlash(root)) && // some rollup plugins use null bytes for private resolved Ids
  16098. !file.includes("\0") && fs__default.existsSync(file)) {
  16099. watcher.add(path$n.resolve(file));
  16100. }
  16101. }
  16102. const escapedSpaceCharacters = /(?: |\\t|\\n|\\f|\\r)+/g;
  16103. const imageSetUrlRE = /^(?:[\w\-]+\(.*?\)|'.*?'|".*?"|\S*)/;
  16104. function joinSrcset(ret) {
  16105. return ret.map(({ url, descriptor }) => url + (descriptor ? ` ${descriptor}` : "")).join(", ");
  16106. }
  16107. function splitSrcSetDescriptor(srcs) {
  16108. return splitSrcSet(srcs).map((s) => {
  16109. const src = s.replace(escapedSpaceCharacters, " ").trim();
  16110. const url = imageSetUrlRE.exec(src)?.[0] ?? "";
  16111. return {
  16112. url,
  16113. descriptor: src.slice(url.length).trim()
  16114. };
  16115. }).filter(({ url }) => !!url);
  16116. }
  16117. function processSrcSet(srcs, replacer) {
  16118. return Promise.all(
  16119. splitSrcSetDescriptor(srcs).map(async ({ url, descriptor }) => ({
  16120. url: await replacer({ url, descriptor }),
  16121. descriptor
  16122. }))
  16123. ).then(joinSrcset);
  16124. }
  16125. function processSrcSetSync(srcs, replacer) {
  16126. return joinSrcset(
  16127. splitSrcSetDescriptor(srcs).map(({ url, descriptor }) => ({
  16128. url: replacer({ url, descriptor }),
  16129. descriptor
  16130. }))
  16131. );
  16132. }
  16133. const cleanSrcSetRE = /(?:url|image|gradient|cross-fade)\([^)]*\)|"([^"]|(?<=\\)")*"|'([^']|(?<=\\)')*'|data:\w+\/[\w.+\-]+;base64,[\w+/=]+|\?\S+,/g;
  16134. function splitSrcSet(srcs) {
  16135. const parts = [];
  16136. const cleanedSrcs = srcs.replace(cleanSrcSetRE, blankReplacer);
  16137. let startIndex = 0;
  16138. let splitIndex;
  16139. do {
  16140. splitIndex = cleanedSrcs.indexOf(",", startIndex);
  16141. parts.push(
  16142. srcs.slice(startIndex, splitIndex !== -1 ? splitIndex : void 0)
  16143. );
  16144. startIndex = splitIndex + 1;
  16145. } while (splitIndex !== -1);
  16146. return parts;
  16147. }
  16148. const windowsDriveRE = /^[A-Z]:/;
  16149. const replaceWindowsDriveRE = /^([A-Z]):\//;
  16150. const linuxAbsolutePathRE = /^\/[^/]/;
  16151. function escapeToLinuxLikePath(path2) {
  16152. if (windowsDriveRE.test(path2)) {
  16153. return path2.replace(replaceWindowsDriveRE, "/windows/$1/");
  16154. }
  16155. if (linuxAbsolutePathRE.test(path2)) {
  16156. return `/linux${path2}`;
  16157. }
  16158. return path2;
  16159. }
  16160. const revertWindowsDriveRE = /^\/windows\/([A-Z])\//;
  16161. function unescapeToLinuxLikePath(path2) {
  16162. if (path2.startsWith("/linux/")) {
  16163. return path2.slice("/linux".length);
  16164. }
  16165. if (path2.startsWith("/windows/")) {
  16166. return path2.replace(revertWindowsDriveRE, "$1:/");
  16167. }
  16168. return path2;
  16169. }
  16170. const nullSourceMap = {
  16171. names: [],
  16172. sources: [],
  16173. mappings: "",
  16174. version: 3
  16175. };
  16176. function combineSourcemaps(filename, sourcemapList) {
  16177. if (sourcemapList.length === 0 || sourcemapList.every((m) => m.sources.length === 0)) {
  16178. return { ...nullSourceMap };
  16179. }
  16180. sourcemapList = sourcemapList.map((sourcemap) => {
  16181. const newSourcemaps = { ...sourcemap };
  16182. newSourcemaps.sources = sourcemap.sources.map(
  16183. (source) => source ? escapeToLinuxLikePath(source) : null
  16184. );
  16185. if (sourcemap.sourceRoot) {
  16186. newSourcemaps.sourceRoot = escapeToLinuxLikePath(sourcemap.sourceRoot);
  16187. }
  16188. return newSourcemaps;
  16189. });
  16190. let map;
  16191. let mapIndex = 1;
  16192. const useArrayInterface = sourcemapList.slice(0, -1).find((m) => m.sources.length !== 1) === void 0;
  16193. if (useArrayInterface) {
  16194. map = remapping(sourcemapList, () => null);
  16195. } else {
  16196. map = remapping(sourcemapList[0], function loader(sourcefile) {
  16197. const mapForSources = sourcemapList.slice(mapIndex).find((s) => s.sources.includes(sourcefile));
  16198. if (mapForSources) {
  16199. mapIndex++;
  16200. return mapForSources;
  16201. }
  16202. return null;
  16203. });
  16204. }
  16205. if (!map.file) {
  16206. delete map.file;
  16207. }
  16208. map.sources = map.sources.map(
  16209. (source) => source ? unescapeToLinuxLikePath(source) : source
  16210. );
  16211. map.file = filename;
  16212. return map;
  16213. }
  16214. function unique(arr) {
  16215. return Array.from(new Set(arr));
  16216. }
  16217. async function getLocalhostAddressIfDiffersFromDNS() {
  16218. const [nodeResult, dnsResult] = await Promise.all([
  16219. promises.lookup("localhost"),
  16220. promises.lookup("localhost", { verbatim: true })
  16221. ]);
  16222. const isSame = nodeResult.family === dnsResult.family && nodeResult.address === dnsResult.address;
  16223. return isSame ? void 0 : nodeResult.address;
  16224. }
  16225. function diffDnsOrderChange(oldUrls, newUrls) {
  16226. return !(oldUrls === newUrls || oldUrls && newUrls && arrayEqual(oldUrls.local, newUrls.local) && arrayEqual(oldUrls.network, newUrls.network));
  16227. }
  16228. async function resolveHostname(optionsHost) {
  16229. let host;
  16230. if (optionsHost === void 0 || optionsHost === false) {
  16231. host = "localhost";
  16232. } else if (optionsHost === true) {
  16233. host = void 0;
  16234. } else {
  16235. host = optionsHost;
  16236. }
  16237. let name = host === void 0 || wildcardHosts.has(host) ? "localhost" : host;
  16238. if (host === "localhost") {
  16239. const localhostAddr = await getLocalhostAddressIfDiffersFromDNS();
  16240. if (localhostAddr) {
  16241. name = localhostAddr;
  16242. }
  16243. }
  16244. return { host, name };
  16245. }
  16246. async function resolveServerUrls(server, options, config) {
  16247. const address = server.address();
  16248. const isAddressInfo = (x) => x?.address;
  16249. if (!isAddressInfo(address)) {
  16250. return { local: [], network: [] };
  16251. }
  16252. const local = [];
  16253. const network = [];
  16254. const hostname = await resolveHostname(options.host);
  16255. const protocol = options.https ? "https" : "http";
  16256. const port = address.port;
  16257. const base = config.rawBase === "./" || config.rawBase === "" ? "/" : config.rawBase;
  16258. if (hostname.host !== void 0 && !wildcardHosts.has(hostname.host)) {
  16259. let hostnameName = hostname.name;
  16260. if (hostnameName.includes(":")) {
  16261. hostnameName = `[${hostnameName}]`;
  16262. }
  16263. const address2 = `${protocol}://${hostnameName}:${port}${base}`;
  16264. if (loopbackHosts.has(hostname.host)) {
  16265. local.push(address2);
  16266. } else {
  16267. network.push(address2);
  16268. }
  16269. } else {
  16270. Object.values(os$5.networkInterfaces()).flatMap((nInterface) => nInterface ?? []).filter(
  16271. (detail) => detail && detail.address && (detail.family === "IPv4" || // @ts-expect-error Node 18.0 - 18.3 returns number
  16272. detail.family === 4)
  16273. ).forEach((detail) => {
  16274. let host = detail.address.replace("127.0.0.1", hostname.name);
  16275. if (host.includes(":")) {
  16276. host = `[${host}]`;
  16277. }
  16278. const url = `${protocol}://${host}:${port}${base}`;
  16279. if (detail.address.includes("127.0.0.1")) {
  16280. local.push(url);
  16281. } else {
  16282. network.push(url);
  16283. }
  16284. });
  16285. }
  16286. return { local, network };
  16287. }
  16288. function arraify(target) {
  16289. return Array.isArray(target) ? target : [target];
  16290. }
  16291. const multilineCommentsRE = /\/\*[^*]*\*+(?:[^/*][^*]*\*+)*\//g;
  16292. const singlelineCommentsRE = /\/\/.*/g;
  16293. const requestQuerySplitRE = /\?(?!.*[/|}])/;
  16294. const requestQueryMaybeEscapedSplitRE = /\\?\?(?!.*[/|}])/;
  16295. const blankReplacer = (match) => " ".repeat(match.length);
  16296. function getHash(text, length = 8) {
  16297. const h = createHash$2("sha256").update(text).digest("hex").substring(0, length);
  16298. if (length <= 64) return h;
  16299. return h.padEnd(length, "_");
  16300. }
  16301. const _dirname = path$n.dirname(fileURLToPath(import.meta.url));
  16302. const requireResolveFromRootWithFallback = (root, id) => {
  16303. const found = resolvePackageData(id, root) || resolvePackageData(id, _dirname);
  16304. if (!found) {
  16305. const error = new Error(`${JSON.stringify(id)} not found.`);
  16306. error.code = "MODULE_NOT_FOUND";
  16307. throw error;
  16308. }
  16309. return _require$1.resolve(id, { paths: [root, _dirname] });
  16310. };
  16311. function emptyCssComments(raw) {
  16312. return raw.replace(multilineCommentsRE, blankReplacer);
  16313. }
  16314. function backwardCompatibleWorkerPlugins(plugins) {
  16315. if (Array.isArray(plugins)) {
  16316. return plugins;
  16317. }
  16318. if (typeof plugins === "function") {
  16319. return plugins();
  16320. }
  16321. return [];
  16322. }
  16323. function mergeConfigRecursively(defaults, overrides, rootPath) {
  16324. const merged = { ...defaults };
  16325. for (const key in overrides) {
  16326. const value = overrides[key];
  16327. if (value == null) {
  16328. continue;
  16329. }
  16330. const existing = merged[key];
  16331. if (existing == null) {
  16332. merged[key] = value;
  16333. continue;
  16334. }
  16335. if (key === "alias" && (rootPath === "resolve" || rootPath === "")) {
  16336. merged[key] = mergeAlias(existing, value);
  16337. continue;
  16338. } else if (key === "assetsInclude" && rootPath === "") {
  16339. merged[key] = [].concat(existing, value);
  16340. continue;
  16341. } else if (key === "noExternal" && rootPath === "ssr" && (existing === true || value === true)) {
  16342. merged[key] = true;
  16343. continue;
  16344. } else if (key === "plugins" && rootPath === "worker") {
  16345. merged[key] = () => [
  16346. ...backwardCompatibleWorkerPlugins(existing),
  16347. ...backwardCompatibleWorkerPlugins(value)
  16348. ];
  16349. continue;
  16350. } else if (key === "server" && rootPath === "server.hmr") {
  16351. merged[key] = value;
  16352. continue;
  16353. }
  16354. if (Array.isArray(existing) || Array.isArray(value)) {
  16355. merged[key] = [...arraify(existing), ...arraify(value)];
  16356. continue;
  16357. }
  16358. if (isObject$1(existing) && isObject$1(value)) {
  16359. merged[key] = mergeConfigRecursively(
  16360. existing,
  16361. value,
  16362. rootPath ? `${rootPath}.${key}` : key
  16363. );
  16364. continue;
  16365. }
  16366. merged[key] = value;
  16367. }
  16368. return merged;
  16369. }
  16370. function mergeConfig(defaults, overrides, isRoot = true) {
  16371. if (typeof defaults === "function" || typeof overrides === "function") {
  16372. throw new Error(`Cannot merge config in form of callback`);
  16373. }
  16374. return mergeConfigRecursively(defaults, overrides, isRoot ? "" : ".");
  16375. }
  16376. function mergeAlias(a, b) {
  16377. if (!a) return b;
  16378. if (!b) return a;
  16379. if (isObject$1(a) && isObject$1(b)) {
  16380. return { ...a, ...b };
  16381. }
  16382. return [...normalizeAlias(b), ...normalizeAlias(a)];
  16383. }
  16384. function normalizeAlias(o = []) {
  16385. return Array.isArray(o) ? o.map(normalizeSingleAlias) : Object.keys(o).map(
  16386. (find) => normalizeSingleAlias({
  16387. find,
  16388. replacement: o[find]
  16389. })
  16390. );
  16391. }
  16392. function normalizeSingleAlias({
  16393. find,
  16394. replacement,
  16395. customResolver
  16396. }) {
  16397. if (typeof find === "string" && find[find.length - 1] === "/" && replacement[replacement.length - 1] === "/") {
  16398. find = find.slice(0, find.length - 1);
  16399. replacement = replacement.slice(0, replacement.length - 1);
  16400. }
  16401. const alias = {
  16402. find,
  16403. replacement
  16404. };
  16405. if (customResolver) {
  16406. alias.customResolver = customResolver;
  16407. }
  16408. return alias;
  16409. }
  16410. function transformStableResult(s, id, config) {
  16411. return {
  16412. code: s.toString(),
  16413. map: config.command === "build" && config.build.sourcemap ? s.generateMap({ hires: "boundary", source: id }) : null
  16414. };
  16415. }
  16416. async function asyncFlatten(arr) {
  16417. do {
  16418. arr = (await Promise.all(arr)).flat(Infinity);
  16419. } while (arr.some((v) => v?.then));
  16420. return arr;
  16421. }
  16422. function stripBomTag(content) {
  16423. if (content.charCodeAt(0) === 65279) {
  16424. return content.slice(1);
  16425. }
  16426. return content;
  16427. }
  16428. const windowsDrivePathPrefixRE = /^[A-Za-z]:[/\\]/;
  16429. const isNonDriveRelativeAbsolutePath = (p) => {
  16430. if (!isWindows$3) return p[0] === "/";
  16431. return windowsDrivePathPrefixRE.test(p);
  16432. };
  16433. function shouldServeFile(filePath, root) {
  16434. if (!isCaseInsensitiveFS) return true;
  16435. return hasCorrectCase(filePath, root);
  16436. }
  16437. function hasCorrectCase(file, assets) {
  16438. if (file === assets) return true;
  16439. const parent = path$n.dirname(file);
  16440. if (fs__default.readdirSync(parent).includes(path$n.basename(file))) {
  16441. return hasCorrectCase(parent, assets);
  16442. }
  16443. return false;
  16444. }
  16445. function joinUrlSegments(a, b) {
  16446. if (!a || !b) {
  16447. return a || b || "";
  16448. }
  16449. if (a[a.length - 1] === "/") {
  16450. a = a.substring(0, a.length - 1);
  16451. }
  16452. if (b[0] !== "/") {
  16453. b = "/" + b;
  16454. }
  16455. return a + b;
  16456. }
  16457. function removeLeadingSlash(str) {
  16458. return str[0] === "/" ? str.slice(1) : str;
  16459. }
  16460. function stripBase(path2, base) {
  16461. if (path2 === base) {
  16462. return "/";
  16463. }
  16464. const devBase = withTrailingSlash(base);
  16465. return path2.startsWith(devBase) ? path2.slice(devBase.length - 1) : path2;
  16466. }
  16467. function arrayEqual(a, b) {
  16468. if (a === b) return true;
  16469. if (a.length !== b.length) return false;
  16470. for (let i = 0; i < a.length; i++) {
  16471. if (a[i] !== b[i]) return false;
  16472. }
  16473. return true;
  16474. }
  16475. function evalValue(rawValue) {
  16476. const fn = new Function(`
  16477. var console, exports, global, module, process, require
  16478. return (
  16479. ${rawValue}
  16480. )
  16481. `);
  16482. return fn();
  16483. }
  16484. function getNpmPackageName(importPath) {
  16485. const parts = importPath.split("/");
  16486. if (parts[0][0] === "@") {
  16487. if (!parts[1]) return null;
  16488. return `${parts[0]}/${parts[1]}`;
  16489. } else {
  16490. return parts[0];
  16491. }
  16492. }
  16493. const escapeRegexRE = /[-/\\^$*+?.()|[\]{}]/g;
  16494. function escapeRegex(str) {
  16495. return str.replace(escapeRegexRE, "\\$&");
  16496. }
  16497. function getPackageManagerCommand(type = "install") {
  16498. const packageManager = process.env.npm_config_user_agent?.split(" ")[0].split("/")[0] || "npm";
  16499. switch (type) {
  16500. case "install":
  16501. return packageManager === "npm" ? "npm install" : `${packageManager} add`;
  16502. case "uninstall":
  16503. return packageManager === "npm" ? "npm uninstall" : `${packageManager} remove`;
  16504. case "update":
  16505. return packageManager === "yarn" ? "yarn upgrade" : `${packageManager} update`;
  16506. default:
  16507. throw new TypeError(`Unknown command type: ${type}`);
  16508. }
  16509. }
  16510. function isDevServer(server) {
  16511. return "pluginContainer" in server;
  16512. }
  16513. function promiseWithResolvers() {
  16514. let resolve;
  16515. let reject;
  16516. const promise = new Promise((_resolve, _reject) => {
  16517. resolve = _resolve;
  16518. reject = _reject;
  16519. });
  16520. return { promise, resolve, reject };
  16521. }
  16522. function createSerialPromiseQueue() {
  16523. let previousTask;
  16524. return {
  16525. async run(f) {
  16526. const thisTask = f();
  16527. const depTasks = Promise.all([previousTask, thisTask]);
  16528. previousTask = depTasks;
  16529. const [, result] = await depTasks;
  16530. if (previousTask === depTasks) {
  16531. previousTask = void 0;
  16532. }
  16533. return result;
  16534. }
  16535. };
  16536. }
  16537. function sortObjectKeys(obj) {
  16538. const sorted = {};
  16539. for (const key of Object.keys(obj).sort()) {
  16540. sorted[key] = obj[key];
  16541. }
  16542. return sorted;
  16543. }
  16544. function displayTime(time) {
  16545. if (time < 1e3) {
  16546. return `${time}ms`;
  16547. }
  16548. time = time / 1e3;
  16549. if (time < 60) {
  16550. return `${time.toFixed(2)}s`;
  16551. }
  16552. const mins = parseInt((time / 60).toString());
  16553. const seconds = time % 60;
  16554. return `${mins}m${seconds < 1 ? "" : ` ${seconds.toFixed(0)}s`}`;
  16555. }
  16556. function encodeURIPath(uri) {
  16557. if (uri.startsWith("data:")) return uri;
  16558. const filePath = cleanUrl(uri);
  16559. const postfix = filePath !== uri ? uri.slice(filePath.length) : "";
  16560. return encodeURI(filePath) + postfix;
  16561. }
  16562. function partialEncodeURIPath(uri) {
  16563. if (uri.startsWith("data:")) return uri;
  16564. const filePath = cleanUrl(uri);
  16565. const postfix = filePath !== uri ? uri.slice(filePath.length) : "";
  16566. return filePath.replaceAll("%", "%25") + postfix;
  16567. }
  16568. const setupSIGTERMListener = (callback) => {
  16569. process.once("SIGTERM", callback);
  16570. if (process.env.CI !== "true") {
  16571. process.stdin.on("end", callback);
  16572. }
  16573. };
  16574. const teardownSIGTERMListener = (callback) => {
  16575. process.off("SIGTERM", callback);
  16576. if (process.env.CI !== "true") {
  16577. process.stdin.off("end", callback);
  16578. }
  16579. };
  16580. const LogLevels = {
  16581. silent: 0,
  16582. error: 1,
  16583. warn: 2,
  16584. info: 3
  16585. };
  16586. let lastType;
  16587. let lastMsg;
  16588. let sameCount = 0;
  16589. function clearScreen() {
  16590. const repeatCount = process.stdout.rows - 2;
  16591. const blank = repeatCount > 0 ? "\n".repeat(repeatCount) : "";
  16592. console.log(blank);
  16593. readline.cursorTo(process.stdout, 0, 0);
  16594. readline.clearScreenDown(process.stdout);
  16595. }
  16596. let timeFormatter;
  16597. function getTimeFormatter() {
  16598. timeFormatter ??= new Intl.DateTimeFormat(void 0, {
  16599. hour: "numeric",
  16600. minute: "numeric",
  16601. second: "numeric"
  16602. });
  16603. return timeFormatter;
  16604. }
  16605. function createLogger(level = "info", options = {}) {
  16606. if (options.customLogger) {
  16607. return options.customLogger;
  16608. }
  16609. const loggedErrors = /* @__PURE__ */ new WeakSet();
  16610. const { prefix = "[vite]", allowClearScreen = true } = options;
  16611. const thresh = LogLevels[level];
  16612. const canClearScreen = allowClearScreen && process.stdout.isTTY && !process.env.CI;
  16613. const clear = canClearScreen ? clearScreen : () => {
  16614. };
  16615. function format(type, msg, options2 = {}) {
  16616. if (options2.timestamp) {
  16617. let tag = "";
  16618. if (type === "info") {
  16619. tag = colors$1.cyan(colors$1.bold(prefix));
  16620. } else if (type === "warn") {
  16621. tag = colors$1.yellow(colors$1.bold(prefix));
  16622. } else {
  16623. tag = colors$1.red(colors$1.bold(prefix));
  16624. }
  16625. return `${colors$1.dim(getTimeFormatter().format(/* @__PURE__ */ new Date()))} ${tag} ${msg}`;
  16626. } else {
  16627. return msg;
  16628. }
  16629. }
  16630. function output(type, msg, options2 = {}) {
  16631. if (thresh >= LogLevels[type]) {
  16632. const method = type === "info" ? "log" : type;
  16633. if (options2.error) {
  16634. loggedErrors.add(options2.error);
  16635. }
  16636. if (canClearScreen) {
  16637. if (type === lastType && msg === lastMsg) {
  16638. sameCount++;
  16639. clear();
  16640. console[method](
  16641. format(type, msg, options2),
  16642. colors$1.yellow(`(x${sameCount + 1})`)
  16643. );
  16644. } else {
  16645. sameCount = 0;
  16646. lastMsg = msg;
  16647. lastType = type;
  16648. if (options2.clear) {
  16649. clear();
  16650. }
  16651. console[method](format(type, msg, options2));
  16652. }
  16653. } else {
  16654. console[method](format(type, msg, options2));
  16655. }
  16656. }
  16657. }
  16658. const warnedMessages = /* @__PURE__ */ new Set();
  16659. const logger = {
  16660. hasWarned: false,
  16661. info(msg, opts) {
  16662. output("info", msg, opts);
  16663. },
  16664. warn(msg, opts) {
  16665. logger.hasWarned = true;
  16666. output("warn", msg, opts);
  16667. },
  16668. warnOnce(msg, opts) {
  16669. if (warnedMessages.has(msg)) return;
  16670. logger.hasWarned = true;
  16671. output("warn", msg, opts);
  16672. warnedMessages.add(msg);
  16673. },
  16674. error(msg, opts) {
  16675. logger.hasWarned = true;
  16676. output("error", msg, opts);
  16677. },
  16678. clearScreen(type) {
  16679. if (thresh >= LogLevels[type]) {
  16680. clear();
  16681. }
  16682. },
  16683. hasErrorLogged(error) {
  16684. return loggedErrors.has(error);
  16685. }
  16686. };
  16687. return logger;
  16688. }
  16689. function printServerUrls(urls, optionsHost, info) {
  16690. const colorUrl = (url) => colors$1.cyan(url.replace(/:(\d+)\//, (_, port) => `:${colors$1.bold(port)}/`));
  16691. for (const url of urls.local) {
  16692. info(` ${colors$1.green("\u279C")} ${colors$1.bold("Local")}: ${colorUrl(url)}`);
  16693. }
  16694. for (const url of urls.network) {
  16695. info(` ${colors$1.green("\u279C")} ${colors$1.bold("Network")}: ${colorUrl(url)}`);
  16696. }
  16697. if (urls.network.length === 0 && optionsHost === void 0) {
  16698. info(
  16699. colors$1.dim(` ${colors$1.green("\u279C")} ${colors$1.bold("Network")}: use `) + colors$1.bold("--host") + colors$1.dim(" to expose")
  16700. );
  16701. }
  16702. }
  16703. const groups = [
  16704. { name: "Assets", color: colors$1.green },
  16705. { name: "CSS", color: colors$1.magenta },
  16706. { name: "JS", color: colors$1.cyan }
  16707. ];
  16708. const COMPRESSIBLE_ASSETS_RE = /\.(?:html|json|svg|txt|xml|xhtml)$/;
  16709. function buildReporterPlugin(config) {
  16710. const compress = promisify$4(gzip);
  16711. const chunkLimit = config.build.chunkSizeWarningLimit;
  16712. const numberFormatter = new Intl.NumberFormat("en", {
  16713. maximumFractionDigits: 2,
  16714. minimumFractionDigits: 2
  16715. });
  16716. const displaySize = (bytes) => {
  16717. return `${numberFormatter.format(bytes / 1e3)} kB`;
  16718. };
  16719. const tty = process.stdout.isTTY && !process.env.CI;
  16720. const shouldLogInfo = LogLevels[config.logLevel || "info"] >= LogLevels.info;
  16721. let hasTransformed = false;
  16722. let hasRenderedChunk = false;
  16723. let hasCompressChunk = false;
  16724. let transformedCount = 0;
  16725. let chunkCount = 0;
  16726. let compressedCount = 0;
  16727. async function getCompressedSize(code) {
  16728. if (config.build.ssr || !config.build.reportCompressedSize) {
  16729. return null;
  16730. }
  16731. if (shouldLogInfo && !hasCompressChunk) {
  16732. if (!tty) {
  16733. config.logger.info("computing gzip size...");
  16734. } else {
  16735. writeLine("computing gzip size (0)...");
  16736. }
  16737. hasCompressChunk = true;
  16738. }
  16739. const compressed = await compress(
  16740. typeof code === "string" ? code : Buffer.from(code)
  16741. );
  16742. compressedCount++;
  16743. if (shouldLogInfo && tty) {
  16744. writeLine(`computing gzip size (${compressedCount})...`);
  16745. }
  16746. return compressed.length;
  16747. }
  16748. const logTransform = throttle((id) => {
  16749. writeLine(
  16750. `transforming (${transformedCount}) ${colors$1.dim(
  16751. path$n.relative(config.root, id)
  16752. )}`
  16753. );
  16754. });
  16755. return {
  16756. name: "vite:reporter",
  16757. transform(_, id) {
  16758. transformedCount++;
  16759. if (shouldLogInfo) {
  16760. if (!tty) {
  16761. if (!hasTransformed) {
  16762. config.logger.info(`transforming...`);
  16763. }
  16764. } else {
  16765. if (id.includes(`?`)) return;
  16766. logTransform(id);
  16767. }
  16768. hasTransformed = true;
  16769. }
  16770. return null;
  16771. },
  16772. buildStart() {
  16773. transformedCount = 0;
  16774. },
  16775. buildEnd() {
  16776. if (shouldLogInfo) {
  16777. if (tty) {
  16778. clearLine$1();
  16779. }
  16780. config.logger.info(
  16781. `${colors$1.green(`\u2713`)} ${transformedCount} modules transformed.`
  16782. );
  16783. }
  16784. },
  16785. renderStart() {
  16786. chunkCount = 0;
  16787. compressedCount = 0;
  16788. },
  16789. renderChunk(code, chunk, options) {
  16790. if (!options.inlineDynamicImports) {
  16791. for (const id of chunk.moduleIds) {
  16792. const module = this.getModuleInfo(id);
  16793. if (!module) continue;
  16794. if (module.importers.length && module.dynamicImporters.length) {
  16795. const detectedIneffectiveDynamicImport = module.dynamicImporters.some(
  16796. (id2) => !isInNodeModules$1(id2) && chunk.moduleIds.includes(id2)
  16797. );
  16798. if (detectedIneffectiveDynamicImport) {
  16799. this.warn(
  16800. `
  16801. (!) ${module.id} is dynamically imported by ${module.dynamicImporters.join(
  16802. ", "
  16803. )} but also statically imported by ${module.importers.join(
  16804. ", "
  16805. )}, dynamic import will not move module into another chunk.
  16806. `
  16807. );
  16808. }
  16809. }
  16810. }
  16811. }
  16812. chunkCount++;
  16813. if (shouldLogInfo) {
  16814. if (!tty) {
  16815. if (!hasRenderedChunk) {
  16816. config.logger.info("rendering chunks...");
  16817. }
  16818. } else {
  16819. writeLine(`rendering chunks (${chunkCount})...`);
  16820. }
  16821. hasRenderedChunk = true;
  16822. }
  16823. return null;
  16824. },
  16825. generateBundle() {
  16826. if (shouldLogInfo && tty) clearLine$1();
  16827. },
  16828. async writeBundle({ dir: outDir }, output) {
  16829. let hasLargeChunks = false;
  16830. if (shouldLogInfo) {
  16831. const entries = (await Promise.all(
  16832. Object.values(output).map(
  16833. async (chunk) => {
  16834. if (chunk.type === "chunk") {
  16835. return {
  16836. name: chunk.fileName,
  16837. group: "JS",
  16838. size: chunk.code.length,
  16839. compressedSize: await getCompressedSize(chunk.code),
  16840. mapSize: chunk.map ? chunk.map.toString().length : null
  16841. };
  16842. } else {
  16843. if (chunk.fileName.endsWith(".map")) return null;
  16844. const isCSS = chunk.fileName.endsWith(".css");
  16845. const isCompressible = isCSS || COMPRESSIBLE_ASSETS_RE.test(chunk.fileName);
  16846. return {
  16847. name: chunk.fileName,
  16848. group: isCSS ? "CSS" : "Assets",
  16849. size: chunk.source.length,
  16850. mapSize: null,
  16851. // Rollup doesn't support CSS maps?
  16852. compressedSize: isCompressible ? await getCompressedSize(chunk.source) : null
  16853. };
  16854. }
  16855. }
  16856. )
  16857. )).filter(isDefined);
  16858. if (tty) clearLine$1();
  16859. let longest = 0;
  16860. let biggestSize = 0;
  16861. let biggestMap = 0;
  16862. let biggestCompressSize = 0;
  16863. for (const entry of entries) {
  16864. if (entry.name.length > longest) longest = entry.name.length;
  16865. if (entry.size > biggestSize) biggestSize = entry.size;
  16866. if (entry.mapSize && entry.mapSize > biggestMap) {
  16867. biggestMap = entry.mapSize;
  16868. }
  16869. if (entry.compressedSize && entry.compressedSize > biggestCompressSize) {
  16870. biggestCompressSize = entry.compressedSize;
  16871. }
  16872. }
  16873. const sizePad = displaySize(biggestSize).length;
  16874. const mapPad = displaySize(biggestMap).length;
  16875. const compressPad = displaySize(biggestCompressSize).length;
  16876. const relativeOutDir = normalizePath$3(
  16877. path$n.relative(
  16878. config.root,
  16879. path$n.resolve(config.root, outDir ?? config.build.outDir)
  16880. )
  16881. );
  16882. const assetsDir = path$n.join(config.build.assetsDir, "/");
  16883. for (const group of groups) {
  16884. const filtered = entries.filter((e) => e.group === group.name);
  16885. if (!filtered.length) continue;
  16886. for (const entry of filtered.sort((a, z) => a.size - z.size)) {
  16887. const isLarge = group.name === "JS" && entry.size / 1e3 > chunkLimit;
  16888. if (isLarge) hasLargeChunks = true;
  16889. const sizeColor = isLarge ? colors$1.yellow : colors$1.dim;
  16890. let log = colors$1.dim(withTrailingSlash(relativeOutDir));
  16891. log += !config.build.lib && entry.name.startsWith(withTrailingSlash(assetsDir)) ? colors$1.dim(assetsDir) + group.color(
  16892. entry.name.slice(assetsDir.length).padEnd(longest + 2 - assetsDir.length)
  16893. ) : group.color(entry.name.padEnd(longest + 2));
  16894. log += colors$1.bold(
  16895. sizeColor(displaySize(entry.size).padStart(sizePad))
  16896. );
  16897. if (entry.compressedSize) {
  16898. log += colors$1.dim(
  16899. ` \u2502 gzip: ${displaySize(entry.compressedSize).padStart(
  16900. compressPad
  16901. )}`
  16902. );
  16903. }
  16904. if (entry.mapSize) {
  16905. log += colors$1.dim(
  16906. ` \u2502 map: ${displaySize(entry.mapSize).padStart(mapPad)}`
  16907. );
  16908. }
  16909. config.logger.info(log);
  16910. }
  16911. }
  16912. } else {
  16913. hasLargeChunks = Object.values(output).some((chunk) => {
  16914. return chunk.type === "chunk" && chunk.code.length / 1e3 > chunkLimit;
  16915. });
  16916. }
  16917. if (hasLargeChunks && config.build.minify && !config.build.lib && !config.build.ssr) {
  16918. config.logger.warn(
  16919. colors$1.yellow(
  16920. `
  16921. (!) Some chunks are larger than ${chunkLimit} kB after minification. Consider:
  16922. - Using dynamic import() to code-split the application
  16923. - Use build.rollupOptions.output.manualChunks to improve chunking: https://rollupjs.org/configuration-options/#output-manualchunks
  16924. - Adjust chunk size limit for this warning via build.chunkSizeWarningLimit.`
  16925. )
  16926. );
  16927. }
  16928. }
  16929. };
  16930. }
  16931. function writeLine(output) {
  16932. clearLine$1();
  16933. if (output.length < process.stdout.columns) {
  16934. process.stdout.write(output);
  16935. } else {
  16936. process.stdout.write(output.substring(0, process.stdout.columns - 1));
  16937. }
  16938. }
  16939. function clearLine$1() {
  16940. process.stdout.clearLine(0);
  16941. process.stdout.cursorTo(0);
  16942. }
  16943. function throttle(fn) {
  16944. let timerHandle = null;
  16945. return (...args) => {
  16946. if (timerHandle) return;
  16947. fn(...args);
  16948. timerHandle = setTimeout(() => {
  16949. timerHandle = null;
  16950. }, 100);
  16951. };
  16952. }
  16953. const POSIX_SEP_RE = new RegExp('\\' + path$n.posix.sep, 'g');
  16954. const NATIVE_SEP_RE = new RegExp('\\' + path$n.sep, 'g');
  16955. /** @type {Map<string,RegExp>}*/
  16956. const PATTERN_REGEX_CACHE = new Map();
  16957. const GLOB_ALL_PATTERN = `**/*`;
  16958. const TS_EXTENSIONS = ['.ts', '.tsx', '.mts', '.cts'];
  16959. const JS_EXTENSIONS = ['.js', '.jsx', '.mjs', '.cjs'];
  16960. const TSJS_EXTENSIONS = TS_EXTENSIONS.concat(JS_EXTENSIONS);
  16961. const TS_EXTENSIONS_RE_GROUP = `\\.(?:${TS_EXTENSIONS.map((ext) => ext.substring(1)).join('|')})`;
  16962. const TSJS_EXTENSIONS_RE_GROUP = `\\.(?:${TSJS_EXTENSIONS.map((ext) => ext.substring(1)).join(
  16963. '|'
  16964. )})`;
  16965. const IS_POSIX = path$n.posix.sep === path$n.sep;
  16966. /**
  16967. * @template T
  16968. * @returns {{resolve:(result:T)=>void, reject:(error:any)=>void, promise: Promise<T>}}
  16969. */
  16970. function makePromise() {
  16971. let resolve, reject;
  16972. const promise = new Promise((res, rej) => {
  16973. resolve = res;
  16974. reject = rej;
  16975. });
  16976. return { promise, resolve, reject };
  16977. }
  16978. /**
  16979. * @param {string} filename
  16980. * @param {import('./cache.js').TSConfckCache} [cache]
  16981. * @returns {Promise<string|void>}
  16982. */
  16983. async function resolveTSConfigJson(filename, cache) {
  16984. if (path$n.extname(filename) !== '.json') {
  16985. return; // ignore files that are not json
  16986. }
  16987. const tsconfig = path$n.resolve(filename);
  16988. if (cache && (cache.hasParseResult(tsconfig) || cache.hasParseResult(filename))) {
  16989. return tsconfig;
  16990. }
  16991. return promises$1.stat(tsconfig).then((stat) => {
  16992. if (stat.isFile() || stat.isFIFO()) {
  16993. return tsconfig;
  16994. } else {
  16995. throw new Error(`${filename} exists but is not a regular file.`);
  16996. }
  16997. });
  16998. }
  16999. /**
  17000. *
  17001. * @param {string} dir an absolute directory path
  17002. * @returns {boolean} if dir path includes a node_modules segment
  17003. */
  17004. const isInNodeModules = IS_POSIX
  17005. ? (dir) => dir.includes('/node_modules/')
  17006. : (dir) => dir.match(/[/\\]node_modules[/\\]/);
  17007. /**
  17008. * convert posix separator to native separator
  17009. *
  17010. * eg.
  17011. * windows: C:/foo/bar -> c:\foo\bar
  17012. * linux: /foo/bar -> /foo/bar
  17013. *
  17014. * @param {string} filename with posix separators
  17015. * @returns {string} filename with native separators
  17016. */
  17017. const posix2native = IS_POSIX
  17018. ? (filename) => filename
  17019. : (filename) => filename.replace(POSIX_SEP_RE, path$n.sep);
  17020. /**
  17021. * convert native separator to posix separator
  17022. *
  17023. * eg.
  17024. * windows: C:\foo\bar -> c:/foo/bar
  17025. * linux: /foo/bar -> /foo/bar
  17026. *
  17027. * @param {string} filename - filename with native separators
  17028. * @returns {string} filename with posix separators
  17029. */
  17030. const native2posix = IS_POSIX
  17031. ? (filename) => filename
  17032. : (filename) => filename.replace(NATIVE_SEP_RE, path$n.posix.sep);
  17033. /**
  17034. * converts params to native separator, resolves path and converts native back to posix
  17035. *
  17036. * needed on windows to handle posix paths in tsconfig
  17037. *
  17038. * @param dir {string|null} directory to resolve from
  17039. * @param filename {string} filename or pattern to resolve
  17040. * @returns string
  17041. */
  17042. const resolve2posix = IS_POSIX
  17043. ? (dir, filename) => (dir ? path$n.resolve(dir, filename) : path$n.resolve(filename))
  17044. : (dir, filename) =>
  17045. native2posix(
  17046. dir
  17047. ? path$n.resolve(posix2native(dir), posix2native(filename))
  17048. : path$n.resolve(posix2native(filename))
  17049. );
  17050. /**
  17051. *
  17052. * @param {import('./public.d.ts').TSConfckParseResult} result
  17053. * @param {import('./public.d.ts').TSConfckParseOptions} [options]
  17054. * @returns {string[]}
  17055. */
  17056. function resolveReferencedTSConfigFiles(result, options) {
  17057. const dir = path$n.dirname(result.tsconfigFile);
  17058. return result.tsconfig.references.map((ref) => {
  17059. const refPath = ref.path.endsWith('.json')
  17060. ? ref.path
  17061. : path$n.join(ref.path, options?.configName ?? 'tsconfig.json');
  17062. return resolve2posix(dir, refPath);
  17063. });
  17064. }
  17065. /**
  17066. * @param {string} filename
  17067. * @param {import('./public.d.ts').TSConfckParseResult} result
  17068. * @returns {import('./public.d.ts').TSConfckParseResult}
  17069. */
  17070. function resolveSolutionTSConfig(filename, result) {
  17071. const allowJs = result.tsconfig.compilerOptions?.allowJs;
  17072. const extensions = allowJs ? TSJS_EXTENSIONS : TS_EXTENSIONS;
  17073. if (
  17074. result.referenced &&
  17075. extensions.some((ext) => filename.endsWith(ext)) &&
  17076. !isIncluded(filename, result)
  17077. ) {
  17078. const solutionTSConfig = result.referenced.find((referenced) =>
  17079. isIncluded(filename, referenced)
  17080. );
  17081. if (solutionTSConfig) {
  17082. return solutionTSConfig;
  17083. }
  17084. }
  17085. return result;
  17086. }
  17087. /**
  17088. *
  17089. * @param {string} filename
  17090. * @param {import('./public.d.ts').TSConfckParseResult} result
  17091. * @returns {boolean}
  17092. */
  17093. function isIncluded(filename, result) {
  17094. const dir = native2posix(path$n.dirname(result.tsconfigFile));
  17095. const files = (result.tsconfig.files || []).map((file) => resolve2posix(dir, file));
  17096. const absoluteFilename = resolve2posix(null, filename);
  17097. if (files.includes(filename)) {
  17098. return true;
  17099. }
  17100. const allowJs = result.tsconfig.compilerOptions?.allowJs;
  17101. const isIncluded = isGlobMatch(
  17102. absoluteFilename,
  17103. dir,
  17104. result.tsconfig.include || (result.tsconfig.files ? [] : [GLOB_ALL_PATTERN]),
  17105. allowJs
  17106. );
  17107. if (isIncluded) {
  17108. const isExcluded = isGlobMatch(absoluteFilename, dir, result.tsconfig.exclude || [], allowJs);
  17109. return !isExcluded;
  17110. }
  17111. return false;
  17112. }
  17113. /**
  17114. * test filenames agains glob patterns in tsconfig
  17115. *
  17116. * @param filename {string} posix style abolute path to filename to test
  17117. * @param dir {string} posix style absolute path to directory of tsconfig containing patterns
  17118. * @param patterns {string[]} glob patterns to match against
  17119. * @param allowJs {boolean} allowJs setting in tsconfig to include js extensions in checks
  17120. * @returns {boolean} true when at least one pattern matches filename
  17121. */
  17122. function isGlobMatch(filename, dir, patterns, allowJs) {
  17123. const extensions = allowJs ? TSJS_EXTENSIONS : TS_EXTENSIONS;
  17124. return patterns.some((pattern) => {
  17125. // filename must end with part of pattern that comes after last wildcard
  17126. let lastWildcardIndex = pattern.length;
  17127. let hasWildcard = false;
  17128. let hasExtension = false;
  17129. let hasSlash = false;
  17130. let lastSlashIndex = -1;
  17131. for (let i = pattern.length - 1; i > -1; i--) {
  17132. const c = pattern[i];
  17133. if (!hasWildcard) {
  17134. if (c === '*' || c === '?') {
  17135. lastWildcardIndex = i;
  17136. hasWildcard = true;
  17137. }
  17138. }
  17139. if (!hasSlash) {
  17140. if (c === '.') {
  17141. hasExtension = true;
  17142. } else if (c === '/') {
  17143. lastSlashIndex = i;
  17144. hasSlash = true;
  17145. }
  17146. }
  17147. if (hasWildcard && hasSlash) {
  17148. break;
  17149. }
  17150. }
  17151. if (!hasExtension && (!hasWildcard || lastWildcardIndex < lastSlashIndex)) {
  17152. // add implicit glob
  17153. pattern += `${pattern.endsWith('/') ? '' : '/'}${GLOB_ALL_PATTERN}`;
  17154. lastWildcardIndex = pattern.length - 1;
  17155. hasWildcard = true;
  17156. }
  17157. // if pattern does not end with wildcard, filename must end with pattern after last wildcard
  17158. if (
  17159. lastWildcardIndex < pattern.length - 1 &&
  17160. !filename.endsWith(pattern.slice(lastWildcardIndex + 1))
  17161. ) {
  17162. return false;
  17163. }
  17164. // if pattern ends with *, filename must end with a default extension
  17165. if (pattern.endsWith('*') && !extensions.some((ext) => filename.endsWith(ext))) {
  17166. return false;
  17167. }
  17168. // for **/* , filename must start with the dir
  17169. if (pattern === GLOB_ALL_PATTERN) {
  17170. return filename.startsWith(`${dir}/`);
  17171. }
  17172. const resolvedPattern = resolve2posix(dir, pattern);
  17173. // filename must start with part of pattern that comes before first wildcard
  17174. let firstWildcardIndex = -1;
  17175. for (let i = 0; i < resolvedPattern.length; i++) {
  17176. if (resolvedPattern[i] === '*' || resolvedPattern[i] === '?') {
  17177. firstWildcardIndex = i;
  17178. hasWildcard = true;
  17179. break;
  17180. }
  17181. }
  17182. if (
  17183. firstWildcardIndex > 1 &&
  17184. !filename.startsWith(resolvedPattern.slice(0, firstWildcardIndex - 1))
  17185. ) {
  17186. return false;
  17187. }
  17188. if (!hasWildcard) {
  17189. // no wildcard in pattern, filename must be equal to resolved pattern
  17190. return filename === resolvedPattern;
  17191. } else if (
  17192. firstWildcardIndex + GLOB_ALL_PATTERN.length ===
  17193. resolvedPattern.length - (pattern.length - 1 - lastWildcardIndex) &&
  17194. resolvedPattern.slice(firstWildcardIndex, firstWildcardIndex + GLOB_ALL_PATTERN.length) ===
  17195. GLOB_ALL_PATTERN
  17196. ) {
  17197. // singular glob-all pattern and we already validated prefix and suffix matches
  17198. return true;
  17199. }
  17200. // complex pattern, use regex to check it
  17201. if (PATTERN_REGEX_CACHE.has(resolvedPattern)) {
  17202. return PATTERN_REGEX_CACHE.get(resolvedPattern).test(filename);
  17203. }
  17204. const regex = pattern2regex(resolvedPattern, allowJs);
  17205. PATTERN_REGEX_CACHE.set(resolvedPattern, regex);
  17206. return regex.test(filename);
  17207. });
  17208. }
  17209. /**
  17210. * @param {string} resolvedPattern
  17211. * @param {boolean} allowJs
  17212. * @returns {RegExp}
  17213. */
  17214. function pattern2regex(resolvedPattern, allowJs) {
  17215. let regexStr = '^';
  17216. for (let i = 0; i < resolvedPattern.length; i++) {
  17217. const char = resolvedPattern[i];
  17218. if (char === '?') {
  17219. regexStr += '[^\\/]';
  17220. continue;
  17221. }
  17222. if (char === '*') {
  17223. if (resolvedPattern[i + 1] === '*' && resolvedPattern[i + 2] === '/') {
  17224. i += 2;
  17225. regexStr += '(?:[^\\/]*\\/)*'; // zero or more path segments
  17226. continue;
  17227. }
  17228. regexStr += '[^\\/]*';
  17229. continue;
  17230. }
  17231. if ('/.+^${}()|[]\\'.includes(char)) {
  17232. regexStr += `\\`;
  17233. }
  17234. regexStr += char;
  17235. }
  17236. // add known file endings if pattern ends on *
  17237. if (resolvedPattern.endsWith('*')) {
  17238. regexStr += allowJs ? TSJS_EXTENSIONS_RE_GROUP : TS_EXTENSIONS_RE_GROUP;
  17239. }
  17240. regexStr += '$';
  17241. return new RegExp(regexStr);
  17242. }
  17243. /**
  17244. * replace tokens like ${configDir}
  17245. * @param {any} tsconfig
  17246. * @param {string} configDir
  17247. * @returns {any}
  17248. */
  17249. function replaceTokens(tsconfig, configDir) {
  17250. return JSON.parse(
  17251. JSON.stringify(tsconfig)
  17252. // replace ${configDir}
  17253. .replaceAll(/"\${configDir}/g, `"${native2posix(configDir)}`)
  17254. );
  17255. }
  17256. /**
  17257. * find the closest tsconfig.json file
  17258. *
  17259. * @param {string} filename - path to file to find tsconfig for (absolute or relative to cwd)
  17260. * @param {import('./public.d.ts').TSConfckFindOptions} [options] - options
  17261. * @returns {Promise<string|null>} absolute path to closest tsconfig.json or null if not found
  17262. */
  17263. async function find(filename, options) {
  17264. let dir = path$n.dirname(path$n.resolve(filename));
  17265. if (options?.ignoreNodeModules && isInNodeModules(dir)) {
  17266. return null;
  17267. }
  17268. const cache = options?.cache;
  17269. const configName = options?.configName ?? 'tsconfig.json';
  17270. if (cache?.hasConfigPath(dir, configName)) {
  17271. return cache.getConfigPath(dir, configName);
  17272. }
  17273. const { /** @type {Promise<string|null>} */ promise, resolve, reject } = makePromise();
  17274. if (options?.root && !path$n.isAbsolute(options.root)) {
  17275. options.root = path$n.resolve(options.root);
  17276. }
  17277. findUp(dir, { promise, resolve, reject }, options);
  17278. return promise;
  17279. }
  17280. /**
  17281. *
  17282. * @param {string} dir
  17283. * @param {{promise:Promise<string|null>,resolve:(result:string|null)=>void,reject:(err:any)=>void}} madePromise
  17284. * @param {import('./public.d.ts').TSConfckFindOptions} [options] - options
  17285. */
  17286. function findUp(dir, { resolve, reject, promise }, options) {
  17287. const { cache, root, configName } = options ?? {};
  17288. if (cache) {
  17289. if (cache.hasConfigPath(dir, configName)) {
  17290. let cached;
  17291. try {
  17292. cached = cache.getConfigPath(dir, configName);
  17293. } catch (e) {
  17294. reject(e);
  17295. return;
  17296. }
  17297. if (cached?.then) {
  17298. cached.then(resolve).catch(reject);
  17299. } else {
  17300. resolve(cached);
  17301. }
  17302. } else {
  17303. cache.setConfigPath(dir, promise, configName);
  17304. }
  17305. }
  17306. const tsconfig = path$n.join(dir, options?.configName ?? 'tsconfig.json');
  17307. fs__default.stat(tsconfig, (err, stats) => {
  17308. if (stats && (stats.isFile() || stats.isFIFO())) {
  17309. resolve(tsconfig);
  17310. } else if (err?.code !== 'ENOENT') {
  17311. reject(err);
  17312. } else {
  17313. let parent;
  17314. if (root === dir || (parent = path$n.dirname(dir)) === dir) {
  17315. resolve(null);
  17316. } else {
  17317. findUp(parent, { promise, resolve, reject }, options);
  17318. }
  17319. }
  17320. });
  17321. }
  17322. /*
  17323. this file contains code from strip-bom and strip-json-comments by Sindre Sorhus
  17324. https://github.com/sindresorhus/strip-json-comments/blob/v4.0.0/index.js
  17325. https://github.com/sindresorhus/strip-bom/blob/v5.0.0/index.js
  17326. licensed under MIT, see ../LICENSE
  17327. */
  17328. /**
  17329. * convert content of tsconfig.json to regular json
  17330. *
  17331. * @param {string} tsconfigJson - content of tsconfig.json
  17332. * @returns {string} content as regular json, comments and dangling commas have been replaced with whitespace
  17333. */
  17334. function toJson(tsconfigJson) {
  17335. const stripped = stripDanglingComma(stripJsonComments(stripBom(tsconfigJson)));
  17336. if (stripped.trim() === '') {
  17337. // only whitespace left after stripping, return empty object so that JSON.parse still works
  17338. return '{}';
  17339. } else {
  17340. return stripped;
  17341. }
  17342. }
  17343. /**
  17344. * replace dangling commas from pseudo-json string with single space
  17345. * implementation heavily inspired by strip-json-comments
  17346. *
  17347. * @param {string} pseudoJson
  17348. * @returns {string}
  17349. */
  17350. function stripDanglingComma(pseudoJson) {
  17351. let insideString = false;
  17352. let offset = 0;
  17353. let result = '';
  17354. let danglingCommaPos = null;
  17355. for (let i = 0; i < pseudoJson.length; i++) {
  17356. const currentCharacter = pseudoJson[i];
  17357. if (currentCharacter === '"') {
  17358. const escaped = isEscaped(pseudoJson, i);
  17359. if (!escaped) {
  17360. insideString = !insideString;
  17361. }
  17362. }
  17363. if (insideString) {
  17364. danglingCommaPos = null;
  17365. continue;
  17366. }
  17367. if (currentCharacter === ',') {
  17368. danglingCommaPos = i;
  17369. continue;
  17370. }
  17371. if (danglingCommaPos) {
  17372. if (currentCharacter === '}' || currentCharacter === ']') {
  17373. result += pseudoJson.slice(offset, danglingCommaPos) + ' ';
  17374. offset = danglingCommaPos + 1;
  17375. danglingCommaPos = null;
  17376. } else if (!currentCharacter.match(/\s/)) {
  17377. danglingCommaPos = null;
  17378. }
  17379. }
  17380. }
  17381. return result + pseudoJson.substring(offset);
  17382. }
  17383. // start strip-json-comments
  17384. /**
  17385. *
  17386. * @param {string} jsonString
  17387. * @param {number} quotePosition
  17388. * @returns {boolean}
  17389. */
  17390. function isEscaped(jsonString, quotePosition) {
  17391. let index = quotePosition - 1;
  17392. let backslashCount = 0;
  17393. while (jsonString[index] === '\\') {
  17394. index -= 1;
  17395. backslashCount += 1;
  17396. }
  17397. return Boolean(backslashCount % 2);
  17398. }
  17399. /**
  17400. *
  17401. * @param {string} string
  17402. * @param {number?} start
  17403. * @param {number?} end
  17404. */
  17405. function strip(string, start, end) {
  17406. return string.slice(start, end).replace(/\S/g, ' ');
  17407. }
  17408. const singleComment = Symbol('singleComment');
  17409. const multiComment = Symbol('multiComment');
  17410. /**
  17411. * @param {string} jsonString
  17412. * @returns {string}
  17413. */
  17414. function stripJsonComments(jsonString) {
  17415. let isInsideString = false;
  17416. /** @type {false | symbol} */
  17417. let isInsideComment = false;
  17418. let offset = 0;
  17419. let result = '';
  17420. for (let index = 0; index < jsonString.length; index++) {
  17421. const currentCharacter = jsonString[index];
  17422. const nextCharacter = jsonString[index + 1];
  17423. if (!isInsideComment && currentCharacter === '"') {
  17424. const escaped = isEscaped(jsonString, index);
  17425. if (!escaped) {
  17426. isInsideString = !isInsideString;
  17427. }
  17428. }
  17429. if (isInsideString) {
  17430. continue;
  17431. }
  17432. if (!isInsideComment && currentCharacter + nextCharacter === '//') {
  17433. result += jsonString.slice(offset, index);
  17434. offset = index;
  17435. isInsideComment = singleComment;
  17436. index++;
  17437. } else if (isInsideComment === singleComment && currentCharacter + nextCharacter === '\r\n') {
  17438. index++;
  17439. isInsideComment = false;
  17440. result += strip(jsonString, offset, index);
  17441. offset = index;
  17442. } else if (isInsideComment === singleComment && currentCharacter === '\n') {
  17443. isInsideComment = false;
  17444. result += strip(jsonString, offset, index);
  17445. offset = index;
  17446. } else if (!isInsideComment && currentCharacter + nextCharacter === '/*') {
  17447. result += jsonString.slice(offset, index);
  17448. offset = index;
  17449. isInsideComment = multiComment;
  17450. index++;
  17451. } else if (isInsideComment === multiComment && currentCharacter + nextCharacter === '*/') {
  17452. index++;
  17453. isInsideComment = false;
  17454. result += strip(jsonString, offset, index + 1);
  17455. offset = index + 1;
  17456. }
  17457. }
  17458. return result + (isInsideComment ? strip(jsonString.slice(offset)) : jsonString.slice(offset));
  17459. }
  17460. // end strip-json-comments
  17461. // start strip-bom
  17462. /**
  17463. * @param {string} string
  17464. * @returns {string}
  17465. */
  17466. function stripBom(string) {
  17467. // Catches EFBBBF (UTF-8 BOM) because the buffer-to-string
  17468. // conversion translates it to FEFF (UTF-16 BOM).
  17469. if (string.charCodeAt(0) === 0xfeff) {
  17470. return string.slice(1);
  17471. }
  17472. return string;
  17473. }
  17474. // end strip-bom
  17475. const not_found_result = {
  17476. tsconfigFile: null,
  17477. tsconfig: {}
  17478. };
  17479. /**
  17480. * parse the closest tsconfig.json file
  17481. *
  17482. * @param {string} filename - path to a tsconfig .json or a source file or directory (absolute or relative to cwd)
  17483. * @param {import('./public.d.ts').TSConfckParseOptions} [options] - options
  17484. * @returns {Promise<import('./public.d.ts').TSConfckParseResult>}
  17485. * @throws {TSConfckParseError}
  17486. */
  17487. async function parse$e(filename, options) {
  17488. /** @type {import('./cache.js').TSConfckCache} */
  17489. const cache = options?.cache;
  17490. if (cache?.hasParseResult(filename)) {
  17491. return getParsedDeep(filename, cache, options);
  17492. }
  17493. const {
  17494. resolve,
  17495. reject,
  17496. /** @type {Promise<import('./public.d.ts').TSConfckParseResult>}*/
  17497. promise
  17498. } = makePromise();
  17499. cache?.setParseResult(filename, promise, true);
  17500. try {
  17501. let tsconfigFile =
  17502. (await resolveTSConfigJson(filename, cache)) || (await find(filename, options));
  17503. if (!tsconfigFile) {
  17504. resolve(not_found_result);
  17505. return promise;
  17506. }
  17507. let result;
  17508. if (filename !== tsconfigFile && cache?.hasParseResult(tsconfigFile)) {
  17509. result = await getParsedDeep(tsconfigFile, cache, options);
  17510. } else {
  17511. result = await parseFile$1(tsconfigFile, cache, filename === tsconfigFile);
  17512. await Promise.all([parseExtends(result, cache), parseReferences(result, options)]);
  17513. }
  17514. result.tsconfig = replaceTokens(result.tsconfig, path$n.dirname(tsconfigFile));
  17515. resolve(resolveSolutionTSConfig(filename, result));
  17516. } catch (e) {
  17517. reject(e);
  17518. }
  17519. return promise;
  17520. }
  17521. /**
  17522. * ensure extends and references are parsed
  17523. *
  17524. * @param {string} filename - cached file
  17525. * @param {import('./cache.js').TSConfckCache} cache - cache
  17526. * @param {import('./public.d.ts').TSConfckParseOptions} options - options
  17527. */
  17528. async function getParsedDeep(filename, cache, options) {
  17529. const result = await cache.getParseResult(filename);
  17530. if (
  17531. (result.tsconfig.extends && !result.extended) ||
  17532. (result.tsconfig.references && !result.referenced)
  17533. ) {
  17534. const promise = Promise.all([
  17535. parseExtends(result, cache),
  17536. parseReferences(result, options)
  17537. ]).then(() => result);
  17538. cache.setParseResult(filename, promise, true);
  17539. return promise;
  17540. }
  17541. return result;
  17542. }
  17543. /**
  17544. *
  17545. * @param {string} tsconfigFile - path to tsconfig file
  17546. * @param {import('./cache.js').TSConfckCache} [cache] - cache
  17547. * @param {boolean} [skipCache] - skip cache
  17548. * @returns {Promise<import('./public.d.ts').TSConfckParseResult>}
  17549. */
  17550. async function parseFile$1(tsconfigFile, cache, skipCache) {
  17551. if (
  17552. !skipCache &&
  17553. cache?.hasParseResult(tsconfigFile) &&
  17554. !cache.getParseResult(tsconfigFile)._isRootFile_
  17555. ) {
  17556. return cache.getParseResult(tsconfigFile);
  17557. }
  17558. const promise = promises$1
  17559. .readFile(tsconfigFile, 'utf-8')
  17560. .then(toJson)
  17561. .then((json) => {
  17562. const parsed = JSON.parse(json);
  17563. applyDefaults(parsed, tsconfigFile);
  17564. return {
  17565. tsconfigFile,
  17566. tsconfig: normalizeTSConfig(parsed, path$n.dirname(tsconfigFile))
  17567. };
  17568. })
  17569. .catch((e) => {
  17570. throw new TSConfckParseError(
  17571. `parsing ${tsconfigFile} failed: ${e}`,
  17572. 'PARSE_FILE',
  17573. tsconfigFile,
  17574. e
  17575. );
  17576. });
  17577. if (
  17578. !skipCache &&
  17579. (!cache?.hasParseResult(tsconfigFile) || !cache.getParseResult(tsconfigFile)._isRootFile_)
  17580. ) {
  17581. cache?.setParseResult(tsconfigFile, promise);
  17582. }
  17583. return promise;
  17584. }
  17585. /**
  17586. * normalize to match the output of ts.parseJsonConfigFileContent
  17587. *
  17588. * @param {any} tsconfig - typescript tsconfig output
  17589. * @param {string} dir - directory
  17590. */
  17591. function normalizeTSConfig(tsconfig, dir) {
  17592. // set baseUrl to absolute path
  17593. const baseUrl = tsconfig.compilerOptions?.baseUrl;
  17594. if (baseUrl && !baseUrl.startsWith('${') && !path$n.isAbsolute(baseUrl)) {
  17595. tsconfig.compilerOptions.baseUrl = resolve2posix(dir, baseUrl);
  17596. }
  17597. return tsconfig;
  17598. }
  17599. /**
  17600. *
  17601. * @param {import('./public.d.ts').TSConfckParseResult} result
  17602. * @param {import('./public.d.ts').TSConfckParseOptions} [options]
  17603. * @returns {Promise<void>}
  17604. */
  17605. async function parseReferences(result, options) {
  17606. if (!result.tsconfig.references) {
  17607. return;
  17608. }
  17609. const referencedFiles = resolveReferencedTSConfigFiles(result, options);
  17610. const referenced = await Promise.all(
  17611. referencedFiles.map((file) => parseFile$1(file, options?.cache))
  17612. );
  17613. await Promise.all(referenced.map((ref) => parseExtends(ref, options?.cache)));
  17614. referenced.forEach((ref) => {
  17615. ref.solution = result;
  17616. });
  17617. result.referenced = referenced;
  17618. }
  17619. /**
  17620. * @param {import('./public.d.ts').TSConfckParseResult} result
  17621. * @param {import('./cache.js').TSConfckCache}[cache]
  17622. * @returns {Promise<void>}
  17623. */
  17624. async function parseExtends(result, cache) {
  17625. if (!result.tsconfig.extends) {
  17626. return;
  17627. }
  17628. // use result as first element in extended
  17629. // but dereference tsconfig so that mergeExtended can modify the original without affecting extended[0]
  17630. /** @type {import('./public.d.ts').TSConfckParseResult[]} */
  17631. const extended = [
  17632. { tsconfigFile: result.tsconfigFile, tsconfig: JSON.parse(JSON.stringify(result.tsconfig)) }
  17633. ];
  17634. // flatten extends graph into extended
  17635. let pos = 0;
  17636. /** @type {string[]} */
  17637. const extendsPath = [];
  17638. let currentBranchDepth = 0;
  17639. while (pos < extended.length) {
  17640. const extending = extended[pos];
  17641. extendsPath.push(extending.tsconfigFile);
  17642. if (extending.tsconfig.extends) {
  17643. // keep following this branch
  17644. currentBranchDepth += 1;
  17645. /** @type {string[]} */
  17646. let resolvedExtends;
  17647. if (!Array.isArray(extending.tsconfig.extends)) {
  17648. resolvedExtends = [resolveExtends(extending.tsconfig.extends, extending.tsconfigFile)];
  17649. } else {
  17650. // reverse because typescript 5.0 treats ['a','b','c'] as c extends b extends a
  17651. resolvedExtends = extending.tsconfig.extends
  17652. .reverse()
  17653. .map((ex) => resolveExtends(ex, extending.tsconfigFile));
  17654. }
  17655. const circularExtends = resolvedExtends.find((tsconfigFile) =>
  17656. extendsPath.includes(tsconfigFile)
  17657. );
  17658. if (circularExtends) {
  17659. const circle = extendsPath.concat([circularExtends]).join(' -> ');
  17660. throw new TSConfckParseError(
  17661. `Circular dependency in "extends": ${circle}`,
  17662. 'EXTENDS_CIRCULAR',
  17663. result.tsconfigFile
  17664. );
  17665. }
  17666. // add new extends to the list directly after current
  17667. extended.splice(
  17668. pos + 1,
  17669. 0,
  17670. ...(await Promise.all(resolvedExtends.map((file) => parseFile$1(file, cache))))
  17671. );
  17672. } else {
  17673. // reached a leaf, backtrack to the last branching point and continue
  17674. extendsPath.splice(-currentBranchDepth);
  17675. currentBranchDepth = 0;
  17676. }
  17677. pos = pos + 1;
  17678. }
  17679. result.extended = extended;
  17680. // skip first as it is the original config
  17681. for (const ext of result.extended.slice(1)) {
  17682. extendTSConfig(result, ext);
  17683. }
  17684. }
  17685. /**
  17686. *
  17687. * @param {string} extended
  17688. * @param {string} from
  17689. * @returns {string}
  17690. */
  17691. function resolveExtends(extended, from) {
  17692. if (extended === '..') {
  17693. // see #149
  17694. extended = '../tsconfig.json';
  17695. }
  17696. const req = createRequire$2(from);
  17697. let error;
  17698. try {
  17699. return req.resolve(extended);
  17700. } catch (e) {
  17701. error = e;
  17702. }
  17703. if (extended[0] !== '.' && !path$n.isAbsolute(extended)) {
  17704. try {
  17705. return req.resolve(`${extended}/tsconfig.json`);
  17706. } catch (e) {
  17707. error = e;
  17708. }
  17709. }
  17710. throw new TSConfckParseError(
  17711. `failed to resolve "extends":"${extended}" in ${from}`,
  17712. 'EXTENDS_RESOLVE',
  17713. from,
  17714. error
  17715. );
  17716. }
  17717. // references, extends and custom keys are not carried over
  17718. const EXTENDABLE_KEYS = [
  17719. 'compilerOptions',
  17720. 'files',
  17721. 'include',
  17722. 'exclude',
  17723. 'watchOptions',
  17724. 'compileOnSave',
  17725. 'typeAcquisition',
  17726. 'buildOptions'
  17727. ];
  17728. /**
  17729. *
  17730. * @param {import('./public.d.ts').TSConfckParseResult} extending
  17731. * @param {import('./public.d.ts').TSConfckParseResult} extended
  17732. * @returns void
  17733. */
  17734. function extendTSConfig(extending, extended) {
  17735. const extendingConfig = extending.tsconfig;
  17736. const extendedConfig = extended.tsconfig;
  17737. const relativePath = native2posix(
  17738. path$n.relative(path$n.dirname(extending.tsconfigFile), path$n.dirname(extended.tsconfigFile))
  17739. );
  17740. for (const key of Object.keys(extendedConfig).filter((key) => EXTENDABLE_KEYS.includes(key))) {
  17741. if (key === 'compilerOptions') {
  17742. if (!extendingConfig.compilerOptions) {
  17743. extendingConfig.compilerOptions = {};
  17744. }
  17745. for (const option of Object.keys(extendedConfig.compilerOptions)) {
  17746. if (Object.prototype.hasOwnProperty.call(extendingConfig.compilerOptions, option)) {
  17747. continue; // already set
  17748. }
  17749. extendingConfig.compilerOptions[option] = rebaseRelative(
  17750. option,
  17751. extendedConfig.compilerOptions[option],
  17752. relativePath
  17753. );
  17754. }
  17755. } else if (extendingConfig[key] === undefined) {
  17756. if (key === 'watchOptions') {
  17757. extendingConfig.watchOptions = {};
  17758. for (const option of Object.keys(extendedConfig.watchOptions)) {
  17759. extendingConfig.watchOptions[option] = rebaseRelative(
  17760. option,
  17761. extendedConfig.watchOptions[option],
  17762. relativePath
  17763. );
  17764. }
  17765. } else {
  17766. extendingConfig[key] = rebaseRelative(key, extendedConfig[key], relativePath);
  17767. }
  17768. }
  17769. }
  17770. }
  17771. const REBASE_KEYS = [
  17772. // root
  17773. 'files',
  17774. 'include',
  17775. 'exclude',
  17776. // compilerOptions
  17777. 'baseUrl',
  17778. 'rootDir',
  17779. 'rootDirs',
  17780. 'typeRoots',
  17781. 'outDir',
  17782. 'outFile',
  17783. 'declarationDir',
  17784. // watchOptions
  17785. 'excludeDirectories',
  17786. 'excludeFiles'
  17787. ];
  17788. /** @typedef {string | string[]} PathValue */
  17789. /**
  17790. *
  17791. * @param {string} key
  17792. * @param {PathValue} value
  17793. * @param {string} prependPath
  17794. * @returns {PathValue}
  17795. */
  17796. function rebaseRelative(key, value, prependPath) {
  17797. if (!REBASE_KEYS.includes(key)) {
  17798. return value;
  17799. }
  17800. if (Array.isArray(value)) {
  17801. return value.map((x) => rebasePath(x, prependPath));
  17802. } else {
  17803. return rebasePath(value, prependPath);
  17804. }
  17805. }
  17806. /**
  17807. *
  17808. * @param {string} value
  17809. * @param {string} prependPath
  17810. * @returns {string}
  17811. */
  17812. function rebasePath(value, prependPath) {
  17813. if (path$n.isAbsolute(value) || value.startsWith('${configDir}')) {
  17814. return value;
  17815. } else {
  17816. // relative paths use posix syntax in tsconfig
  17817. return path$n.posix.normalize(path$n.posix.join(prependPath, value));
  17818. }
  17819. }
  17820. class TSConfckParseError extends Error {
  17821. /**
  17822. * error code
  17823. * @type {string}
  17824. */
  17825. code;
  17826. /**
  17827. * error cause
  17828. * @type { Error | undefined}
  17829. */
  17830. cause;
  17831. /**
  17832. * absolute path of tsconfig file where the error happened
  17833. * @type {string}
  17834. */
  17835. tsconfigFile;
  17836. /**
  17837. *
  17838. * @param {string} message - error message
  17839. * @param {string} code - error code
  17840. * @param {string} tsconfigFile - path to tsconfig file
  17841. * @param {Error?} cause - cause of this error
  17842. */
  17843. constructor(message, code, tsconfigFile, cause) {
  17844. super(message);
  17845. // Set the prototype explicitly.
  17846. Object.setPrototypeOf(this, TSConfckParseError.prototype);
  17847. this.name = TSConfckParseError.name;
  17848. this.code = code;
  17849. this.cause = cause;
  17850. this.tsconfigFile = tsconfigFile;
  17851. }
  17852. }
  17853. /**
  17854. *
  17855. * @param {any} tsconfig
  17856. * @param {string} tsconfigFile
  17857. */
  17858. function applyDefaults(tsconfig, tsconfigFile) {
  17859. if (isJSConfig(tsconfigFile)) {
  17860. tsconfig.compilerOptions = {
  17861. ...DEFAULT_JSCONFIG_COMPILER_OPTIONS,
  17862. ...tsconfig.compilerOptions
  17863. };
  17864. }
  17865. }
  17866. const DEFAULT_JSCONFIG_COMPILER_OPTIONS = {
  17867. allowJs: true,
  17868. maxNodeModuleJsDepth: 2,
  17869. allowSyntheticDefaultImports: true,
  17870. skipLibCheck: true,
  17871. noEmit: true
  17872. };
  17873. /**
  17874. * @param {string} configFileName
  17875. */
  17876. function isJSConfig(configFileName) {
  17877. return path$n.basename(configFileName) === 'jsconfig.json';
  17878. }
  17879. /** @template T */
  17880. class TSConfckCache {
  17881. /**
  17882. * clear cache, use this if you have a long running process and tsconfig files have been added,changed or deleted
  17883. */
  17884. clear() {
  17885. this.#configPaths.clear();
  17886. this.#parsed.clear();
  17887. }
  17888. /**
  17889. * has cached closest config for files in dir
  17890. * @param {string} dir
  17891. * @param {string} [configName=tsconfig.json]
  17892. * @returns {boolean}
  17893. */
  17894. hasConfigPath(dir, configName = 'tsconfig.json') {
  17895. return this.#configPaths.has(`${dir}/${configName}`);
  17896. }
  17897. /**
  17898. * get cached closest tsconfig for files in dir
  17899. * @param {string} dir
  17900. * @param {string} [configName=tsconfig.json]
  17901. * @returns {Promise<string|null>|string|null}
  17902. * @throws {unknown} if cached value is an error
  17903. */
  17904. getConfigPath(dir, configName = 'tsconfig.json') {
  17905. const key = `${dir}/${configName}`;
  17906. const value = this.#configPaths.get(key);
  17907. if (value == null || value.length || value.then) {
  17908. return value;
  17909. } else {
  17910. throw value;
  17911. }
  17912. }
  17913. /**
  17914. * has parsed tsconfig for file
  17915. * @param {string} file
  17916. * @returns {boolean}
  17917. */
  17918. hasParseResult(file) {
  17919. return this.#parsed.has(file);
  17920. }
  17921. /**
  17922. * get parsed tsconfig for file
  17923. * @param {string} file
  17924. * @returns {Promise<T>|T}
  17925. * @throws {unknown} if cached value is an error
  17926. */
  17927. getParseResult(file) {
  17928. const value = this.#parsed.get(file);
  17929. if (value.then || value.tsconfig) {
  17930. return value;
  17931. } else {
  17932. throw value; // cached error, rethrow
  17933. }
  17934. }
  17935. /**
  17936. * @internal
  17937. * @private
  17938. * @param file
  17939. * @param {boolean} isRootFile a flag to check if current file which involking the parse() api, used to distinguish the normal cache which only parsed by parseFile()
  17940. * @param {Promise<T>} result
  17941. */
  17942. setParseResult(file, result, isRootFile = false) {
  17943. // _isRootFile_ is a temporary property for Promise result, used to prevent deadlock with cache
  17944. Object.defineProperty(result, '_isRootFile_', {
  17945. value: isRootFile,
  17946. writable: false,
  17947. enumerable: false,
  17948. configurable: false
  17949. });
  17950. this.#parsed.set(file, result);
  17951. result
  17952. .then((parsed) => {
  17953. if (this.#parsed.get(file) === result) {
  17954. this.#parsed.set(file, parsed);
  17955. }
  17956. })
  17957. .catch((e) => {
  17958. if (this.#parsed.get(file) === result) {
  17959. this.#parsed.set(file, e);
  17960. }
  17961. });
  17962. }
  17963. /**
  17964. * @internal
  17965. * @private
  17966. * @param {string} dir
  17967. * @param {Promise<string|null>} configPath
  17968. * @param {string} [configName=tsconfig.json]
  17969. */
  17970. setConfigPath(dir, configPath, configName = 'tsconfig.json') {
  17971. const key = `${dir}/${configName}`;
  17972. this.#configPaths.set(key, configPath);
  17973. configPath
  17974. .then((path) => {
  17975. if (this.#configPaths.get(key) === configPath) {
  17976. this.#configPaths.set(key, path);
  17977. }
  17978. })
  17979. .catch((e) => {
  17980. if (this.#configPaths.get(key) === configPath) {
  17981. this.#configPaths.set(key, e);
  17982. }
  17983. });
  17984. }
  17985. /**
  17986. * map directories to their closest tsconfig.json
  17987. * @internal
  17988. * @private
  17989. * @type{Map<string,(Promise<string|null>|string|null)>}
  17990. */
  17991. #configPaths = new Map();
  17992. /**
  17993. * map files to their parsed tsconfig result
  17994. * @internal
  17995. * @private
  17996. * @type {Map<string,(Promise<T>|T)> }
  17997. */
  17998. #parsed = new Map();
  17999. }
  18000. const debug$h = createDebugger("vite:esbuild");
  18001. const IIFE_BEGIN_RE = /(?:const|var)\s+\S+\s*=\s*function\([^()]*\)\s*\{\s*"use strict";/;
  18002. const validExtensionRE = /\.\w+$/;
  18003. const jsxExtensionsRE = /\.(?:j|t)sx\b/;
  18004. const defaultEsbuildSupported = {
  18005. "dynamic-import": true,
  18006. "import-meta": true
  18007. };
  18008. let server;
  18009. async function transformWithEsbuild(code, filename, options, inMap) {
  18010. let loader = options?.loader;
  18011. if (!loader) {
  18012. const ext = path$n.extname(validExtensionRE.test(filename) ? filename : cleanUrl(filename)).slice(1);
  18013. if (ext === "cjs" || ext === "mjs") {
  18014. loader = "js";
  18015. } else if (ext === "cts" || ext === "mts") {
  18016. loader = "ts";
  18017. } else {
  18018. loader = ext;
  18019. }
  18020. }
  18021. let tsconfigRaw = options?.tsconfigRaw;
  18022. if (typeof tsconfigRaw !== "string") {
  18023. const meaningfulFields = [
  18024. "alwaysStrict",
  18025. "experimentalDecorators",
  18026. "importsNotUsedAsValues",
  18027. "jsx",
  18028. "jsxFactory",
  18029. "jsxFragmentFactory",
  18030. "jsxImportSource",
  18031. "preserveValueImports",
  18032. "target",
  18033. "useDefineForClassFields",
  18034. "verbatimModuleSyntax"
  18035. ];
  18036. const compilerOptionsForFile = {};
  18037. if (loader === "ts" || loader === "tsx") {
  18038. const loadedTsconfig = await loadTsconfigJsonForFile(filename);
  18039. const loadedCompilerOptions = loadedTsconfig.compilerOptions ?? {};
  18040. for (const field of meaningfulFields) {
  18041. if (field in loadedCompilerOptions) {
  18042. compilerOptionsForFile[field] = loadedCompilerOptions[field];
  18043. }
  18044. }
  18045. }
  18046. const compilerOptions = {
  18047. ...compilerOptionsForFile,
  18048. ...tsconfigRaw?.compilerOptions
  18049. };
  18050. if (compilerOptions.useDefineForClassFields === void 0 && compilerOptions.target === void 0) {
  18051. compilerOptions.useDefineForClassFields = false;
  18052. }
  18053. if (options) {
  18054. options.jsx && (compilerOptions.jsx = void 0);
  18055. options.jsxFactory && (compilerOptions.jsxFactory = void 0);
  18056. options.jsxFragment && (compilerOptions.jsxFragmentFactory = void 0);
  18057. options.jsxImportSource && (compilerOptions.jsxImportSource = void 0);
  18058. }
  18059. tsconfigRaw = {
  18060. ...tsconfigRaw,
  18061. compilerOptions
  18062. };
  18063. }
  18064. const resolvedOptions = {
  18065. sourcemap: true,
  18066. // ensure source file name contains full query
  18067. sourcefile: filename,
  18068. ...options,
  18069. loader,
  18070. tsconfigRaw
  18071. };
  18072. delete resolvedOptions.include;
  18073. delete resolvedOptions.exclude;
  18074. delete resolvedOptions.jsxInject;
  18075. try {
  18076. const result = await transform$1(code, resolvedOptions);
  18077. let map;
  18078. if (inMap && resolvedOptions.sourcemap) {
  18079. const nextMap = JSON.parse(result.map);
  18080. nextMap.sourcesContent = [];
  18081. map = combineSourcemaps(filename, [
  18082. nextMap,
  18083. inMap
  18084. ]);
  18085. } else {
  18086. map = resolvedOptions.sourcemap && resolvedOptions.sourcemap !== "inline" ? JSON.parse(result.map) : { mappings: "" };
  18087. }
  18088. return {
  18089. ...result,
  18090. map
  18091. };
  18092. } catch (e) {
  18093. debug$h?.(`esbuild error with options used: `, resolvedOptions);
  18094. if (e.errors) {
  18095. e.frame = "";
  18096. e.errors.forEach((m) => {
  18097. if (m.text === "Experimental decorators are not currently enabled" || m.text === "Parameter decorators only work when experimental decorators are enabled") {
  18098. m.text += '. Vite 5 now uses esbuild 0.18 and you need to enable them by adding "experimentalDecorators": true in your "tsconfig.json" file.';
  18099. }
  18100. e.frame += `
  18101. ` + prettifyMessage(m, code);
  18102. });
  18103. e.loc = e.errors[0].location;
  18104. }
  18105. throw e;
  18106. }
  18107. }
  18108. function esbuildPlugin(config) {
  18109. const options = config.esbuild;
  18110. const { jsxInject, include, exclude, ...esbuildTransformOptions } = options;
  18111. const filter = createFilter(include || /\.(m?ts|[jt]sx)$/, exclude || /\.js$/);
  18112. const transformOptions = {
  18113. target: "esnext",
  18114. charset: "utf8",
  18115. ...esbuildTransformOptions,
  18116. minify: false,
  18117. minifyIdentifiers: false,
  18118. minifySyntax: false,
  18119. minifyWhitespace: false,
  18120. treeShaking: false,
  18121. // keepNames is not needed when minify is disabled.
  18122. // Also transforming multiple times with keepNames enabled breaks
  18123. // tree-shaking. (#9164)
  18124. keepNames: false,
  18125. supported: {
  18126. ...defaultEsbuildSupported,
  18127. ...esbuildTransformOptions.supported
  18128. }
  18129. };
  18130. return {
  18131. name: "vite:esbuild",
  18132. configureServer(_server) {
  18133. server = _server;
  18134. server.watcher.on("add", reloadOnTsconfigChange).on("change", reloadOnTsconfigChange).on("unlink", reloadOnTsconfigChange);
  18135. },
  18136. buildEnd() {
  18137. server = null;
  18138. },
  18139. async transform(code, id) {
  18140. if (filter(id) || filter(cleanUrl(id))) {
  18141. const result = await transformWithEsbuild(code, id, transformOptions);
  18142. if (result.warnings.length) {
  18143. result.warnings.forEach((m) => {
  18144. this.warn(prettifyMessage(m, code));
  18145. });
  18146. }
  18147. if (jsxInject && jsxExtensionsRE.test(id)) {
  18148. result.code = jsxInject + ";" + result.code;
  18149. }
  18150. return {
  18151. code: result.code,
  18152. map: result.map
  18153. };
  18154. }
  18155. }
  18156. };
  18157. }
  18158. const rollupToEsbuildFormatMap = {
  18159. es: "esm",
  18160. cjs: "cjs",
  18161. // passing `var Lib = (() => {})()` to esbuild with format = "iife"
  18162. // will turn it to `(() => { var Lib = (() => {})() })()`,
  18163. // so we remove the format config to tell esbuild not doing this
  18164. //
  18165. // although esbuild doesn't change format, there is still possibility
  18166. // that `{ treeShaking: true }` removes a top-level no-side-effect variable
  18167. // like: `var Lib = 1`, which becomes `` after esbuild transforming,
  18168. // but thankfully rollup does not do this optimization now
  18169. iife: void 0
  18170. };
  18171. const buildEsbuildPlugin = (config) => {
  18172. return {
  18173. name: "vite:esbuild-transpile",
  18174. async renderChunk(code, chunk, opts) {
  18175. if (opts.__vite_skip_esbuild__) {
  18176. return null;
  18177. }
  18178. const options = resolveEsbuildTranspileOptions(config, opts.format);
  18179. if (!options) {
  18180. return null;
  18181. }
  18182. const res = await transformWithEsbuild(code, chunk.fileName, options);
  18183. if (config.build.lib) {
  18184. const esbuildCode = res.code;
  18185. const contentIndex = opts.format === "iife" ? Math.max(esbuildCode.search(IIFE_BEGIN_RE), 0) : opts.format === "umd" ? esbuildCode.indexOf(`(function(`) : 0;
  18186. if (contentIndex > 0) {
  18187. const esbuildHelpers = esbuildCode.slice(0, contentIndex);
  18188. res.code = esbuildCode.slice(contentIndex).replace(`"use strict";`, `"use strict";` + esbuildHelpers);
  18189. }
  18190. }
  18191. return res;
  18192. }
  18193. };
  18194. };
  18195. function resolveEsbuildTranspileOptions(config, format) {
  18196. const target = config.build.target;
  18197. const minify = config.build.minify === "esbuild";
  18198. if ((!target || target === "esnext") && !minify) {
  18199. return null;
  18200. }
  18201. const isEsLibBuild = config.build.lib && format === "es";
  18202. const esbuildOptions = config.esbuild || {};
  18203. const options = {
  18204. charset: "utf8",
  18205. ...esbuildOptions,
  18206. loader: "js",
  18207. target: target || void 0,
  18208. format: rollupToEsbuildFormatMap[format],
  18209. supported: {
  18210. ...defaultEsbuildSupported,
  18211. ...esbuildOptions.supported
  18212. }
  18213. };
  18214. if (!minify) {
  18215. return {
  18216. ...options,
  18217. minify: false,
  18218. minifyIdentifiers: false,
  18219. minifySyntax: false,
  18220. minifyWhitespace: false,
  18221. treeShaking: false
  18222. };
  18223. }
  18224. if (options.minifyIdentifiers != null || options.minifySyntax != null || options.minifyWhitespace != null) {
  18225. if (isEsLibBuild) {
  18226. return {
  18227. ...options,
  18228. minify: false,
  18229. minifyIdentifiers: options.minifyIdentifiers ?? true,
  18230. minifySyntax: options.minifySyntax ?? true,
  18231. minifyWhitespace: false,
  18232. treeShaking: true
  18233. };
  18234. } else {
  18235. return {
  18236. ...options,
  18237. minify: false,
  18238. minifyIdentifiers: options.minifyIdentifiers ?? true,
  18239. minifySyntax: options.minifySyntax ?? true,
  18240. minifyWhitespace: options.minifyWhitespace ?? true,
  18241. treeShaking: true
  18242. };
  18243. }
  18244. }
  18245. if (isEsLibBuild) {
  18246. return {
  18247. ...options,
  18248. minify: false,
  18249. minifyIdentifiers: true,
  18250. minifySyntax: true,
  18251. minifyWhitespace: false,
  18252. treeShaking: true
  18253. };
  18254. } else {
  18255. return {
  18256. ...options,
  18257. minify: true,
  18258. treeShaking: true
  18259. };
  18260. }
  18261. }
  18262. function prettifyMessage(m, code) {
  18263. let res = colors$1.yellow(m.text);
  18264. if (m.location) {
  18265. res += `
  18266. ` + generateCodeFrame(code, m.location);
  18267. }
  18268. return res + `
  18269. `;
  18270. }
  18271. let tsconfckCache;
  18272. async function loadTsconfigJsonForFile(filename) {
  18273. try {
  18274. if (!tsconfckCache) {
  18275. tsconfckCache = new TSConfckCache();
  18276. }
  18277. const result = await parse$e(filename, {
  18278. cache: tsconfckCache,
  18279. ignoreNodeModules: true
  18280. });
  18281. if (server && result.tsconfigFile) {
  18282. ensureWatchedFile(server.watcher, result.tsconfigFile, server.config.root);
  18283. }
  18284. return result.tsconfig;
  18285. } catch (e) {
  18286. if (e instanceof TSConfckParseError) {
  18287. if (server && e.tsconfigFile) {
  18288. ensureWatchedFile(server.watcher, e.tsconfigFile, server.config.root);
  18289. }
  18290. }
  18291. throw e;
  18292. }
  18293. }
  18294. async function reloadOnTsconfigChange(changedFile) {
  18295. if (!server) return;
  18296. if (path$n.basename(changedFile) === "tsconfig.json" || changedFile.endsWith(".json") && tsconfckCache?.hasParseResult(changedFile)) {
  18297. server.config.logger.info(
  18298. `changed tsconfig file detected: ${changedFile} - Clearing cache and forcing full-reload to ensure TypeScript is compiled with updated config values.`,
  18299. { clear: server.config.clearScreen, timestamp: true }
  18300. );
  18301. server.moduleGraph.invalidateAll();
  18302. tsconfckCache?.clear();
  18303. if (server) {
  18304. server.hot.send({
  18305. type: "full-reload",
  18306. path: "*"
  18307. });
  18308. }
  18309. }
  18310. }
  18311. // src/realWorker.ts
  18312. var Worker = class {
  18313. /** @internal */
  18314. _code;
  18315. /** @internal */
  18316. _parentFunctions;
  18317. /** @internal */
  18318. _max;
  18319. /** @internal */
  18320. _pool;
  18321. /** @internal */
  18322. _idlePool;
  18323. /** @internal */
  18324. _queue;
  18325. constructor(fn, options = {}) {
  18326. this._code = genWorkerCode(fn, options.parentFunctions ?? {});
  18327. this._parentFunctions = options.parentFunctions ?? {};
  18328. const defaultMax = Math.max(
  18329. 1,
  18330. // os.availableParallelism is available from Node.js 18.14.0
  18331. (os$5.availableParallelism?.() ?? os$5.cpus().length) - 1
  18332. );
  18333. this._max = options.max || defaultMax;
  18334. this._pool = [];
  18335. this._idlePool = [];
  18336. this._queue = [];
  18337. }
  18338. async run(...args) {
  18339. const worker = await this._getAvailableWorker();
  18340. return new Promise((resolve, reject) => {
  18341. worker.currentResolve = resolve;
  18342. worker.currentReject = reject;
  18343. worker.postMessage({ type: "run", args });
  18344. });
  18345. }
  18346. stop() {
  18347. this._pool.forEach((w) => w.unref());
  18348. this._queue.forEach(
  18349. ([, reject]) => reject(
  18350. new Error("Main worker pool stopped before a worker was available.")
  18351. )
  18352. );
  18353. this._pool = [];
  18354. this._idlePool = [];
  18355. this._queue = [];
  18356. }
  18357. /** @internal */
  18358. async _getAvailableWorker() {
  18359. if (this._idlePool.length) {
  18360. return this._idlePool.shift();
  18361. }
  18362. if (this._pool.length < this._max) {
  18363. const worker = new Worker$1(this._code, { eval: true });
  18364. worker.on("message", async (args) => {
  18365. if (args.type === "run") {
  18366. if ("result" in args) {
  18367. worker.currentResolve && worker.currentResolve(args.result);
  18368. worker.currentResolve = null;
  18369. } else {
  18370. if (args.error instanceof ReferenceError) {
  18371. args.error.message += ". Maybe you forgot to pass the function to parentFunction?";
  18372. }
  18373. worker.currentReject && worker.currentReject(args.error);
  18374. worker.currentReject = null;
  18375. }
  18376. this._assignDoneWorker(worker);
  18377. } else if (args.type === "parentFunction") {
  18378. try {
  18379. const result = await this._parentFunctions[args.name](...args.args);
  18380. worker.postMessage({ type: "parentFunction", id: args.id, result });
  18381. } catch (e) {
  18382. worker.postMessage({
  18383. type: "parentFunction",
  18384. id: args.id,
  18385. error: e
  18386. });
  18387. }
  18388. }
  18389. });
  18390. worker.on("error", (err) => {
  18391. worker.currentReject && worker.currentReject(err);
  18392. worker.currentReject = null;
  18393. });
  18394. worker.on("exit", (code) => {
  18395. const i = this._pool.indexOf(worker);
  18396. if (i > -1)
  18397. this._pool.splice(i, 1);
  18398. if (code !== 0 && worker.currentReject) {
  18399. worker.currentReject(
  18400. new Error(`Worker stopped with non-0 exit code ${code}`)
  18401. );
  18402. worker.currentReject = null;
  18403. }
  18404. });
  18405. this._pool.push(worker);
  18406. return worker;
  18407. }
  18408. let resolve;
  18409. let reject;
  18410. const onWorkerAvailablePromise = new Promise((r, rj) => {
  18411. resolve = r;
  18412. reject = rj;
  18413. });
  18414. this._queue.push([resolve, reject]);
  18415. return onWorkerAvailablePromise;
  18416. }
  18417. /** @internal */
  18418. _assignDoneWorker(worker) {
  18419. if (this._queue.length) {
  18420. const [resolve] = this._queue.shift();
  18421. resolve(worker);
  18422. return;
  18423. }
  18424. this._idlePool.push(worker);
  18425. }
  18426. };
  18427. function genWorkerCode(fn, parentFunctions) {
  18428. const createParentFunctionCaller = (parentPort) => {
  18429. let id = 0;
  18430. const resolvers = /* @__PURE__ */ new Map();
  18431. const call = (key) => async (...args) => {
  18432. id++;
  18433. let resolve, reject;
  18434. const promise = new Promise((res, rej) => {
  18435. resolve = res;
  18436. reject = rej;
  18437. });
  18438. resolvers.set(id, { resolve, reject });
  18439. parentPort.postMessage({ type: "parentFunction", id, name: key, args });
  18440. return await promise;
  18441. };
  18442. const receive = (id2, args) => {
  18443. if (resolvers.has(id2)) {
  18444. const { resolve, reject } = resolvers.get(id2);
  18445. resolvers.delete(id2);
  18446. if ("result" in args) {
  18447. resolve(args.result);
  18448. } else {
  18449. reject(args.error);
  18450. }
  18451. }
  18452. };
  18453. return { call, receive };
  18454. };
  18455. return `
  18456. const { parentPort } = require('worker_threads')
  18457. const parentFunctionCaller = (${createParentFunctionCaller.toString()})(parentPort)
  18458. const doWork = (() => {
  18459. ${Object.keys(parentFunctions).map(
  18460. (key) => `const ${key} = parentFunctionCaller.call(${JSON.stringify(key)});`
  18461. ).join("\n")}
  18462. return (${fn.toString()})()
  18463. })()
  18464. parentPort.on('message', async (args) => {
  18465. if (args.type === 'run') {
  18466. try {
  18467. const res = await doWork(...args.args)
  18468. parentPort.postMessage({ type: 'run', result: res })
  18469. } catch (e) {
  18470. parentPort.postMessage({ type: 'run', error: e })
  18471. }
  18472. } else if (args.type === 'parentFunction') {
  18473. parentFunctionCaller.receive(args.id, args)
  18474. }
  18475. })
  18476. `;
  18477. }
  18478. var FakeWorker = class {
  18479. /** @internal */
  18480. _fn;
  18481. constructor(fn, options = {}) {
  18482. const argsAndCode = genFakeWorkerArgsAndCode(
  18483. fn,
  18484. options.parentFunctions ?? {}
  18485. );
  18486. const require2 = createRequire$1(import.meta.url);
  18487. this._fn = new Function(...argsAndCode)(require2, options.parentFunctions);
  18488. }
  18489. async run(...args) {
  18490. try {
  18491. return await this._fn(...args);
  18492. } catch (err) {
  18493. if (err instanceof ReferenceError) {
  18494. err.message += ". Maybe you forgot to pass the function to parentFunction?";
  18495. }
  18496. throw err;
  18497. }
  18498. }
  18499. stop() {
  18500. }
  18501. };
  18502. function genFakeWorkerArgsAndCode(fn, parentFunctions) {
  18503. return [
  18504. "require",
  18505. "parentFunctions",
  18506. `
  18507. ${Object.keys(parentFunctions).map((key) => `const ${key} = parentFunctions[${JSON.stringify(key)}];`).join("\n")}
  18508. return (${fn.toString()})()
  18509. `
  18510. ];
  18511. }
  18512. // src/workerWithFallback.ts
  18513. var WorkerWithFallback = class {
  18514. /** @internal */
  18515. _disableReal;
  18516. /** @internal */
  18517. _realWorker;
  18518. /** @internal */
  18519. _fakeWorker;
  18520. /** @internal */
  18521. _shouldUseFake;
  18522. constructor(fn, options) {
  18523. this._disableReal = options.max !== void 0 && options.max <= 0;
  18524. this._realWorker = new Worker(fn, options);
  18525. this._fakeWorker = new FakeWorker(fn, options);
  18526. this._shouldUseFake = options.shouldUseFake;
  18527. }
  18528. async run(...args) {
  18529. const useFake = this._disableReal || this._shouldUseFake(...args);
  18530. return this[useFake ? "_fakeWorker" : "_realWorker"].run(...args);
  18531. }
  18532. stop() {
  18533. this._realWorker.stop();
  18534. this._fakeWorker.stop();
  18535. }
  18536. };
  18537. let terserPath;
  18538. const loadTerserPath = (root) => {
  18539. if (terserPath) return terserPath;
  18540. try {
  18541. terserPath = requireResolveFromRootWithFallback(root, "terser");
  18542. } catch (e) {
  18543. if (e.code === "MODULE_NOT_FOUND") {
  18544. throw new Error(
  18545. "terser not found. Since Vite v3, terser has become an optional dependency. You need to install it."
  18546. );
  18547. } else {
  18548. const message = new Error(`terser failed to load:
  18549. ${e.message}`);
  18550. message.stack = e.stack + "\n" + message.stack;
  18551. throw message;
  18552. }
  18553. }
  18554. return terserPath;
  18555. };
  18556. function terserPlugin(config) {
  18557. const { maxWorkers, ...terserOptions } = config.build.terserOptions;
  18558. const makeWorker = () => new Worker(
  18559. () => async (terserPath2, code, options) => {
  18560. const terser = require(terserPath2);
  18561. return terser.minify(code, options);
  18562. },
  18563. {
  18564. max: maxWorkers
  18565. }
  18566. );
  18567. let worker;
  18568. return {
  18569. name: "vite:terser",
  18570. async renderChunk(code, _chunk, outputOptions) {
  18571. if (config.build.minify !== "terser" && // @ts-expect-error injected by @vitejs/plugin-legacy
  18572. !outputOptions.__vite_force_terser__) {
  18573. return null;
  18574. }
  18575. if (config.build.lib && outputOptions.format === "es") {
  18576. return null;
  18577. }
  18578. worker ||= makeWorker();
  18579. const terserPath2 = loadTerserPath(config.root);
  18580. const res = await worker.run(terserPath2, code, {
  18581. safari10: true,
  18582. ...terserOptions,
  18583. sourceMap: !!outputOptions.sourcemap,
  18584. module: outputOptions.format.startsWith("es"),
  18585. toplevel: outputOptions.format === "cjs"
  18586. });
  18587. return {
  18588. code: res.code,
  18589. map: res.map
  18590. };
  18591. },
  18592. closeBundle() {
  18593. worker?.stop();
  18594. }
  18595. };
  18596. }
  18597. const mimes = {
  18598. "3g2": "video/3gpp2",
  18599. "3gp": "video/3gpp",
  18600. "3gpp": "video/3gpp",
  18601. "3mf": "model/3mf",
  18602. "aac": "audio/aac",
  18603. "ac": "application/pkix-attr-cert",
  18604. "adp": "audio/adpcm",
  18605. "adts": "audio/aac",
  18606. "ai": "application/postscript",
  18607. "aml": "application/automationml-aml+xml",
  18608. "amlx": "application/automationml-amlx+zip",
  18609. "amr": "audio/amr",
  18610. "apng": "image/apng",
  18611. "appcache": "text/cache-manifest",
  18612. "appinstaller": "application/appinstaller",
  18613. "appx": "application/appx",
  18614. "appxbundle": "application/appxbundle",
  18615. "asc": "application/pgp-keys",
  18616. "atom": "application/atom+xml",
  18617. "atomcat": "application/atomcat+xml",
  18618. "atomdeleted": "application/atomdeleted+xml",
  18619. "atomsvc": "application/atomsvc+xml",
  18620. "au": "audio/basic",
  18621. "avci": "image/avci",
  18622. "avcs": "image/avcs",
  18623. "avif": "image/avif",
  18624. "aw": "application/applixware",
  18625. "bdoc": "application/bdoc",
  18626. "bin": "application/octet-stream",
  18627. "bmp": "image/bmp",
  18628. "bpk": "application/octet-stream",
  18629. "btf": "image/prs.btif",
  18630. "btif": "image/prs.btif",
  18631. "buffer": "application/octet-stream",
  18632. "ccxml": "application/ccxml+xml",
  18633. "cdfx": "application/cdfx+xml",
  18634. "cdmia": "application/cdmi-capability",
  18635. "cdmic": "application/cdmi-container",
  18636. "cdmid": "application/cdmi-domain",
  18637. "cdmio": "application/cdmi-object",
  18638. "cdmiq": "application/cdmi-queue",
  18639. "cer": "application/pkix-cert",
  18640. "cgm": "image/cgm",
  18641. "cjs": "application/node",
  18642. "class": "application/java-vm",
  18643. "coffee": "text/coffeescript",
  18644. "conf": "text/plain",
  18645. "cpl": "application/cpl+xml",
  18646. "cpt": "application/mac-compactpro",
  18647. "crl": "application/pkix-crl",
  18648. "css": "text/css",
  18649. "csv": "text/csv",
  18650. "cu": "application/cu-seeme",
  18651. "cwl": "application/cwl",
  18652. "cww": "application/prs.cww",
  18653. "davmount": "application/davmount+xml",
  18654. "dbk": "application/docbook+xml",
  18655. "deb": "application/octet-stream",
  18656. "def": "text/plain",
  18657. "deploy": "application/octet-stream",
  18658. "dib": "image/bmp",
  18659. "disposition-notification": "message/disposition-notification",
  18660. "dist": "application/octet-stream",
  18661. "distz": "application/octet-stream",
  18662. "dll": "application/octet-stream",
  18663. "dmg": "application/octet-stream",
  18664. "dms": "application/octet-stream",
  18665. "doc": "application/msword",
  18666. "dot": "application/msword",
  18667. "dpx": "image/dpx",
  18668. "drle": "image/dicom-rle",
  18669. "dsc": "text/prs.lines.tag",
  18670. "dssc": "application/dssc+der",
  18671. "dtd": "application/xml-dtd",
  18672. "dump": "application/octet-stream",
  18673. "dwd": "application/atsc-dwd+xml",
  18674. "ear": "application/java-archive",
  18675. "ecma": "application/ecmascript",
  18676. "elc": "application/octet-stream",
  18677. "emf": "image/emf",
  18678. "eml": "message/rfc822",
  18679. "emma": "application/emma+xml",
  18680. "emotionml": "application/emotionml+xml",
  18681. "eps": "application/postscript",
  18682. "epub": "application/epub+zip",
  18683. "exe": "application/octet-stream",
  18684. "exi": "application/exi",
  18685. "exp": "application/express",
  18686. "exr": "image/aces",
  18687. "ez": "application/andrew-inset",
  18688. "fdf": "application/fdf",
  18689. "fdt": "application/fdt+xml",
  18690. "fits": "image/fits",
  18691. "g3": "image/g3fax",
  18692. "gbr": "application/rpki-ghostbusters",
  18693. "geojson": "application/geo+json",
  18694. "gif": "image/gif",
  18695. "glb": "model/gltf-binary",
  18696. "gltf": "model/gltf+json",
  18697. "gml": "application/gml+xml",
  18698. "gpx": "application/gpx+xml",
  18699. "gram": "application/srgs",
  18700. "grxml": "application/srgs+xml",
  18701. "gxf": "application/gxf",
  18702. "gz": "application/gzip",
  18703. "h261": "video/h261",
  18704. "h263": "video/h263",
  18705. "h264": "video/h264",
  18706. "heic": "image/heic",
  18707. "heics": "image/heic-sequence",
  18708. "heif": "image/heif",
  18709. "heifs": "image/heif-sequence",
  18710. "hej2": "image/hej2k",
  18711. "held": "application/atsc-held+xml",
  18712. "hjson": "application/hjson",
  18713. "hlp": "application/winhlp",
  18714. "hqx": "application/mac-binhex40",
  18715. "hsj2": "image/hsj2",
  18716. "htm": "text/html",
  18717. "html": "text/html",
  18718. "ics": "text/calendar",
  18719. "ief": "image/ief",
  18720. "ifb": "text/calendar",
  18721. "iges": "model/iges",
  18722. "igs": "model/iges",
  18723. "img": "application/octet-stream",
  18724. "in": "text/plain",
  18725. "ini": "text/plain",
  18726. "ink": "application/inkml+xml",
  18727. "inkml": "application/inkml+xml",
  18728. "ipfix": "application/ipfix",
  18729. "iso": "application/octet-stream",
  18730. "its": "application/its+xml",
  18731. "jade": "text/jade",
  18732. "jar": "application/java-archive",
  18733. "jhc": "image/jphc",
  18734. "jls": "image/jls",
  18735. "jp2": "image/jp2",
  18736. "jpe": "image/jpeg",
  18737. "jpeg": "image/jpeg",
  18738. "jpf": "image/jpx",
  18739. "jpg": "image/jpeg",
  18740. "jpg2": "image/jp2",
  18741. "jpgm": "image/jpm",
  18742. "jpgv": "video/jpeg",
  18743. "jph": "image/jph",
  18744. "jpm": "image/jpm",
  18745. "jpx": "image/jpx",
  18746. "js": "text/javascript",
  18747. "json": "application/json",
  18748. "json5": "application/json5",
  18749. "jsonld": "application/ld+json",
  18750. "jsonml": "application/jsonml+json",
  18751. "jsx": "text/jsx",
  18752. "jt": "model/jt",
  18753. "jxr": "image/jxr",
  18754. "jxra": "image/jxra",
  18755. "jxrs": "image/jxrs",
  18756. "jxs": "image/jxs",
  18757. "jxsc": "image/jxsc",
  18758. "jxsi": "image/jxsi",
  18759. "jxss": "image/jxss",
  18760. "kar": "audio/midi",
  18761. "ktx": "image/ktx",
  18762. "ktx2": "image/ktx2",
  18763. "less": "text/less",
  18764. "lgr": "application/lgr+xml",
  18765. "list": "text/plain",
  18766. "litcoffee": "text/coffeescript",
  18767. "log": "text/plain",
  18768. "lostxml": "application/lost+xml",
  18769. "lrf": "application/octet-stream",
  18770. "m1v": "video/mpeg",
  18771. "m21": "application/mp21",
  18772. "m2a": "audio/mpeg",
  18773. "m2v": "video/mpeg",
  18774. "m3a": "audio/mpeg",
  18775. "m4a": "audio/mp4",
  18776. "m4p": "application/mp4",
  18777. "m4s": "video/iso.segment",
  18778. "ma": "application/mathematica",
  18779. "mads": "application/mads+xml",
  18780. "maei": "application/mmt-aei+xml",
  18781. "man": "text/troff",
  18782. "manifest": "text/cache-manifest",
  18783. "map": "application/json",
  18784. "mar": "application/octet-stream",
  18785. "markdown": "text/markdown",
  18786. "mathml": "application/mathml+xml",
  18787. "mb": "application/mathematica",
  18788. "mbox": "application/mbox",
  18789. "md": "text/markdown",
  18790. "mdx": "text/mdx",
  18791. "me": "text/troff",
  18792. "mesh": "model/mesh",
  18793. "meta4": "application/metalink4+xml",
  18794. "metalink": "application/metalink+xml",
  18795. "mets": "application/mets+xml",
  18796. "mft": "application/rpki-manifest",
  18797. "mid": "audio/midi",
  18798. "midi": "audio/midi",
  18799. "mime": "message/rfc822",
  18800. "mj2": "video/mj2",
  18801. "mjp2": "video/mj2",
  18802. "mjs": "text/javascript",
  18803. "mml": "text/mathml",
  18804. "mods": "application/mods+xml",
  18805. "mov": "video/quicktime",
  18806. "mp2": "audio/mpeg",
  18807. "mp21": "application/mp21",
  18808. "mp2a": "audio/mpeg",
  18809. "mp3": "audio/mpeg",
  18810. "mp4": "video/mp4",
  18811. "mp4a": "audio/mp4",
  18812. "mp4s": "application/mp4",
  18813. "mp4v": "video/mp4",
  18814. "mpd": "application/dash+xml",
  18815. "mpe": "video/mpeg",
  18816. "mpeg": "video/mpeg",
  18817. "mpf": "application/media-policy-dataset+xml",
  18818. "mpg": "video/mpeg",
  18819. "mpg4": "video/mp4",
  18820. "mpga": "audio/mpeg",
  18821. "mpp": "application/dash-patch+xml",
  18822. "mrc": "application/marc",
  18823. "mrcx": "application/marcxml+xml",
  18824. "ms": "text/troff",
  18825. "mscml": "application/mediaservercontrol+xml",
  18826. "msh": "model/mesh",
  18827. "msi": "application/octet-stream",
  18828. "msix": "application/msix",
  18829. "msixbundle": "application/msixbundle",
  18830. "msm": "application/octet-stream",
  18831. "msp": "application/octet-stream",
  18832. "mtl": "model/mtl",
  18833. "musd": "application/mmt-usd+xml",
  18834. "mxf": "application/mxf",
  18835. "mxmf": "audio/mobile-xmf",
  18836. "mxml": "application/xv+xml",
  18837. "n3": "text/n3",
  18838. "nb": "application/mathematica",
  18839. "nq": "application/n-quads",
  18840. "nt": "application/n-triples",
  18841. "obj": "model/obj",
  18842. "oda": "application/oda",
  18843. "oga": "audio/ogg",
  18844. "ogg": "audio/ogg",
  18845. "ogv": "video/ogg",
  18846. "ogx": "application/ogg",
  18847. "omdoc": "application/omdoc+xml",
  18848. "onepkg": "application/onenote",
  18849. "onetmp": "application/onenote",
  18850. "onetoc": "application/onenote",
  18851. "onetoc2": "application/onenote",
  18852. "opf": "application/oebps-package+xml",
  18853. "opus": "audio/ogg",
  18854. "otf": "font/otf",
  18855. "owl": "application/rdf+xml",
  18856. "oxps": "application/oxps",
  18857. "p10": "application/pkcs10",
  18858. "p7c": "application/pkcs7-mime",
  18859. "p7m": "application/pkcs7-mime",
  18860. "p7s": "application/pkcs7-signature",
  18861. "p8": "application/pkcs8",
  18862. "pdf": "application/pdf",
  18863. "pfr": "application/font-tdpfr",
  18864. "pgp": "application/pgp-encrypted",
  18865. "pkg": "application/octet-stream",
  18866. "pki": "application/pkixcmp",
  18867. "pkipath": "application/pkix-pkipath",
  18868. "pls": "application/pls+xml",
  18869. "png": "image/png",
  18870. "prc": "model/prc",
  18871. "prf": "application/pics-rules",
  18872. "provx": "application/provenance+xml",
  18873. "ps": "application/postscript",
  18874. "pskcxml": "application/pskc+xml",
  18875. "pti": "image/prs.pti",
  18876. "qt": "video/quicktime",
  18877. "raml": "application/raml+yaml",
  18878. "rapd": "application/route-apd+xml",
  18879. "rdf": "application/rdf+xml",
  18880. "relo": "application/p2p-overlay+xml",
  18881. "rif": "application/reginfo+xml",
  18882. "rl": "application/resource-lists+xml",
  18883. "rld": "application/resource-lists-diff+xml",
  18884. "rmi": "audio/midi",
  18885. "rnc": "application/relax-ng-compact-syntax",
  18886. "rng": "application/xml",
  18887. "roa": "application/rpki-roa",
  18888. "roff": "text/troff",
  18889. "rq": "application/sparql-query",
  18890. "rs": "application/rls-services+xml",
  18891. "rsat": "application/atsc-rsat+xml",
  18892. "rsd": "application/rsd+xml",
  18893. "rsheet": "application/urc-ressheet+xml",
  18894. "rss": "application/rss+xml",
  18895. "rtf": "text/rtf",
  18896. "rtx": "text/richtext",
  18897. "rusd": "application/route-usd+xml",
  18898. "s3m": "audio/s3m",
  18899. "sbml": "application/sbml+xml",
  18900. "scq": "application/scvp-cv-request",
  18901. "scs": "application/scvp-cv-response",
  18902. "sdp": "application/sdp",
  18903. "senmlx": "application/senml+xml",
  18904. "sensmlx": "application/sensml+xml",
  18905. "ser": "application/java-serialized-object",
  18906. "setpay": "application/set-payment-initiation",
  18907. "setreg": "application/set-registration-initiation",
  18908. "sgi": "image/sgi",
  18909. "sgm": "text/sgml",
  18910. "sgml": "text/sgml",
  18911. "shex": "text/shex",
  18912. "shf": "application/shf+xml",
  18913. "shtml": "text/html",
  18914. "sieve": "application/sieve",
  18915. "sig": "application/pgp-signature",
  18916. "sil": "audio/silk",
  18917. "silo": "model/mesh",
  18918. "siv": "application/sieve",
  18919. "slim": "text/slim",
  18920. "slm": "text/slim",
  18921. "sls": "application/route-s-tsid+xml",
  18922. "smi": "application/smil+xml",
  18923. "smil": "application/smil+xml",
  18924. "snd": "audio/basic",
  18925. "so": "application/octet-stream",
  18926. "spdx": "text/spdx",
  18927. "spp": "application/scvp-vp-response",
  18928. "spq": "application/scvp-vp-request",
  18929. "spx": "audio/ogg",
  18930. "sql": "application/sql",
  18931. "sru": "application/sru+xml",
  18932. "srx": "application/sparql-results+xml",
  18933. "ssdl": "application/ssdl+xml",
  18934. "ssml": "application/ssml+xml",
  18935. "stk": "application/hyperstudio",
  18936. "stl": "model/stl",
  18937. "stpx": "model/step+xml",
  18938. "stpxz": "model/step-xml+zip",
  18939. "stpz": "model/step+zip",
  18940. "styl": "text/stylus",
  18941. "stylus": "text/stylus",
  18942. "svg": "image/svg+xml",
  18943. "svgz": "image/svg+xml",
  18944. "swidtag": "application/swid+xml",
  18945. "t": "text/troff",
  18946. "t38": "image/t38",
  18947. "td": "application/urc-targetdesc+xml",
  18948. "tei": "application/tei+xml",
  18949. "teicorpus": "application/tei+xml",
  18950. "text": "text/plain",
  18951. "tfi": "application/thraud+xml",
  18952. "tfx": "image/tiff-fx",
  18953. "tif": "image/tiff",
  18954. "tiff": "image/tiff",
  18955. "toml": "application/toml",
  18956. "tr": "text/troff",
  18957. "trig": "application/trig",
  18958. "ts": "video/mp2t",
  18959. "tsd": "application/timestamped-data",
  18960. "tsv": "text/tab-separated-values",
  18961. "ttc": "font/collection",
  18962. "ttf": "font/ttf",
  18963. "ttl": "text/turtle",
  18964. "ttml": "application/ttml+xml",
  18965. "txt": "text/plain",
  18966. "u3d": "model/u3d",
  18967. "u8dsn": "message/global-delivery-status",
  18968. "u8hdr": "message/global-headers",
  18969. "u8mdn": "message/global-disposition-notification",
  18970. "u8msg": "message/global",
  18971. "ubj": "application/ubjson",
  18972. "uri": "text/uri-list",
  18973. "uris": "text/uri-list",
  18974. "urls": "text/uri-list",
  18975. "vcard": "text/vcard",
  18976. "vrml": "model/vrml",
  18977. "vtt": "text/vtt",
  18978. "vxml": "application/voicexml+xml",
  18979. "war": "application/java-archive",
  18980. "wasm": "application/wasm",
  18981. "wav": "audio/wav",
  18982. "weba": "audio/webm",
  18983. "webm": "video/webm",
  18984. "webmanifest": "application/manifest+json",
  18985. "webp": "image/webp",
  18986. "wgsl": "text/wgsl",
  18987. "wgt": "application/widget",
  18988. "wif": "application/watcherinfo+xml",
  18989. "wmf": "image/wmf",
  18990. "woff": "font/woff",
  18991. "woff2": "font/woff2",
  18992. "wrl": "model/vrml",
  18993. "wsdl": "application/wsdl+xml",
  18994. "wspolicy": "application/wspolicy+xml",
  18995. "x3d": "model/x3d+xml",
  18996. "x3db": "model/x3d+fastinfoset",
  18997. "x3dbz": "model/x3d+binary",
  18998. "x3dv": "model/x3d-vrml",
  18999. "x3dvz": "model/x3d+vrml",
  19000. "x3dz": "model/x3d+xml",
  19001. "xaml": "application/xaml+xml",
  19002. "xav": "application/xcap-att+xml",
  19003. "xca": "application/xcap-caps+xml",
  19004. "xcs": "application/calendar+xml",
  19005. "xdf": "application/xcap-diff+xml",
  19006. "xdssc": "application/dssc+xml",
  19007. "xel": "application/xcap-el+xml",
  19008. "xenc": "application/xenc+xml",
  19009. "xer": "application/patch-ops-error+xml",
  19010. "xfdf": "application/xfdf",
  19011. "xht": "application/xhtml+xml",
  19012. "xhtml": "application/xhtml+xml",
  19013. "xhvml": "application/xv+xml",
  19014. "xlf": "application/xliff+xml",
  19015. "xm": "audio/xm",
  19016. "xml": "text/xml",
  19017. "xns": "application/xcap-ns+xml",
  19018. "xop": "application/xop+xml",
  19019. "xpl": "application/xproc+xml",
  19020. "xsd": "application/xml",
  19021. "xsf": "application/prs.xsf+xml",
  19022. "xsl": "application/xml",
  19023. "xslt": "application/xml",
  19024. "xspf": "application/xspf+xml",
  19025. "xvm": "application/xv+xml",
  19026. "xvml": "application/xv+xml",
  19027. "yaml": "text/yaml",
  19028. "yang": "application/yang",
  19029. "yin": "application/yin+xml",
  19030. "yml": "text/yaml",
  19031. "zip": "application/zip"
  19032. };
  19033. function lookup(extn) {
  19034. let tmp = ('' + extn).trim().toLowerCase();
  19035. let idx = tmp.lastIndexOf('.');
  19036. return mimes[!~idx ? tmp : tmp.substring(++idx)];
  19037. }
  19038. const publicFilesMap = /* @__PURE__ */ new WeakMap();
  19039. async function initPublicFiles(config) {
  19040. let fileNames;
  19041. try {
  19042. fileNames = await recursiveReaddir(config.publicDir);
  19043. } catch (e) {
  19044. if (e.code === ERR_SYMLINK_IN_RECURSIVE_READDIR) {
  19045. return;
  19046. }
  19047. throw e;
  19048. }
  19049. const publicFiles = new Set(
  19050. fileNames.map((fileName) => fileName.slice(config.publicDir.length))
  19051. );
  19052. publicFilesMap.set(config, publicFiles);
  19053. return publicFiles;
  19054. }
  19055. function getPublicFiles(config) {
  19056. return publicFilesMap.get(config);
  19057. }
  19058. function checkPublicFile(url, config) {
  19059. const { publicDir } = config;
  19060. if (!publicDir || url[0] !== "/") {
  19061. return;
  19062. }
  19063. const fileName = cleanUrl(url);
  19064. const publicFiles = getPublicFiles(config);
  19065. if (publicFiles) {
  19066. return publicFiles.has(fileName) ? normalizePath$3(path$n.join(publicDir, fileName)) : void 0;
  19067. }
  19068. const publicFile = normalizePath$3(path$n.join(publicDir, fileName));
  19069. if (!publicFile.startsWith(withTrailingSlash(publicDir))) {
  19070. return;
  19071. }
  19072. return fs__default.existsSync(publicFile) ? publicFile : void 0;
  19073. }
  19074. const assetUrlRE = /__VITE_ASSET__([\w$]+)__(?:\$_(.*?)__)?/g;
  19075. const jsSourceMapRE = /\.[cm]?js\.map$/;
  19076. const assetCache = /* @__PURE__ */ new WeakMap();
  19077. const generatedAssets = /* @__PURE__ */ new WeakMap();
  19078. function registerCustomMime() {
  19079. mimes["ico"] = "image/x-icon";
  19080. mimes["flac"] = "audio/flac";
  19081. mimes["eot"] = "application/vnd.ms-fontobject";
  19082. }
  19083. function renderAssetUrlInJS(ctx, config, chunk, opts, code) {
  19084. const toRelativeRuntime = createToImportMetaURLBasedRelativeRuntime(
  19085. opts.format,
  19086. config.isWorker
  19087. );
  19088. let match;
  19089. let s;
  19090. assetUrlRE.lastIndex = 0;
  19091. while (match = assetUrlRE.exec(code)) {
  19092. s ||= new MagicString(code);
  19093. const [full, referenceId, postfix = ""] = match;
  19094. const file = ctx.getFileName(referenceId);
  19095. chunk.viteMetadata.importedAssets.add(cleanUrl(file));
  19096. const filename = file + postfix;
  19097. const replacement = toOutputFilePathInJS(
  19098. filename,
  19099. "asset",
  19100. chunk.fileName,
  19101. "js",
  19102. config,
  19103. toRelativeRuntime
  19104. );
  19105. const replacementString = typeof replacement === "string" ? JSON.stringify(encodeURIPath(replacement)).slice(1, -1) : `"+${replacement.runtime}+"`;
  19106. s.update(match.index, match.index + full.length, replacementString);
  19107. }
  19108. const publicAssetUrlMap = publicAssetUrlCache.get(config);
  19109. publicAssetUrlRE.lastIndex = 0;
  19110. while (match = publicAssetUrlRE.exec(code)) {
  19111. s ||= new MagicString(code);
  19112. const [full, hash] = match;
  19113. const publicUrl = publicAssetUrlMap.get(hash).slice(1);
  19114. const replacement = toOutputFilePathInJS(
  19115. publicUrl,
  19116. "public",
  19117. chunk.fileName,
  19118. "js",
  19119. config,
  19120. toRelativeRuntime
  19121. );
  19122. const replacementString = typeof replacement === "string" ? JSON.stringify(encodeURIPath(replacement)).slice(1, -1) : `"+${replacement.runtime}+"`;
  19123. s.update(match.index, match.index + full.length, replacementString);
  19124. }
  19125. return s;
  19126. }
  19127. function assetPlugin(config) {
  19128. registerCustomMime();
  19129. let moduleGraph;
  19130. return {
  19131. name: "vite:asset",
  19132. buildStart() {
  19133. assetCache.set(config, /* @__PURE__ */ new Map());
  19134. generatedAssets.set(config, /* @__PURE__ */ new Map());
  19135. },
  19136. configureServer(server) {
  19137. moduleGraph = server.moduleGraph;
  19138. },
  19139. resolveId(id) {
  19140. if (!config.assetsInclude(cleanUrl(id)) && !urlRE.test(id)) {
  19141. return;
  19142. }
  19143. const publicFile = checkPublicFile(id, config);
  19144. if (publicFile) {
  19145. return id;
  19146. }
  19147. },
  19148. async load(id) {
  19149. if (id[0] === "\0") {
  19150. return;
  19151. }
  19152. if (rawRE.test(id)) {
  19153. const file = checkPublicFile(id, config) || cleanUrl(id);
  19154. this.addWatchFile(file);
  19155. return `export default ${JSON.stringify(
  19156. await fsp.readFile(file, "utf-8")
  19157. )}`;
  19158. }
  19159. if (!urlRE.test(id) && !config.assetsInclude(cleanUrl(id))) {
  19160. return;
  19161. }
  19162. id = removeUrlQuery(id);
  19163. let url = await fileToUrl$1(id, config, this);
  19164. if (moduleGraph) {
  19165. const mod = moduleGraph.getModuleById(id);
  19166. if (mod && mod.lastHMRTimestamp > 0) {
  19167. url = injectQuery(url, `t=${mod.lastHMRTimestamp}`);
  19168. }
  19169. }
  19170. return {
  19171. code: `export default ${JSON.stringify(encodeURIPath(url))}`,
  19172. // Force rollup to keep this module from being shared between other entry points if it's an entrypoint.
  19173. // If the resulting chunk is empty, it will be removed in generateBundle.
  19174. moduleSideEffects: config.command === "build" && this.getModuleInfo(id)?.isEntry ? "no-treeshake" : false,
  19175. meta: config.command === "build" ? { "vite:asset": true } : void 0
  19176. };
  19177. },
  19178. renderChunk(code, chunk, opts) {
  19179. const s = renderAssetUrlInJS(this, config, chunk, opts, code);
  19180. if (s) {
  19181. return {
  19182. code: s.toString(),
  19183. map: config.build.sourcemap ? s.generateMap({ hires: "boundary" }) : null
  19184. };
  19185. } else {
  19186. return null;
  19187. }
  19188. },
  19189. generateBundle(_, bundle) {
  19190. for (const file in bundle) {
  19191. const chunk = bundle[file];
  19192. if (chunk.type === "chunk" && chunk.isEntry && chunk.moduleIds.length === 1 && config.assetsInclude(chunk.moduleIds[0]) && this.getModuleInfo(chunk.moduleIds[0])?.meta["vite:asset"]) {
  19193. delete bundle[file];
  19194. }
  19195. }
  19196. if (config.command === "build" && config.build.ssr && !config.build.ssrEmitAssets) {
  19197. for (const file in bundle) {
  19198. if (bundle[file].type === "asset" && !file.endsWith("ssr-manifest.json") && !jsSourceMapRE.test(file)) {
  19199. delete bundle[file];
  19200. }
  19201. }
  19202. }
  19203. }
  19204. };
  19205. }
  19206. async function fileToUrl$1(id, config, ctx) {
  19207. if (config.command === "serve") {
  19208. return fileToDevUrl(id, config);
  19209. } else {
  19210. return fileToBuiltUrl(id, config, ctx);
  19211. }
  19212. }
  19213. function fileToDevUrl(id, config, skipBase = false) {
  19214. let rtn;
  19215. if (checkPublicFile(id, config)) {
  19216. rtn = id;
  19217. } else if (id.startsWith(withTrailingSlash(config.root))) {
  19218. rtn = "/" + path$n.posix.relative(config.root, id);
  19219. } else {
  19220. rtn = path$n.posix.join(FS_PREFIX, id);
  19221. }
  19222. if (skipBase) {
  19223. return rtn;
  19224. }
  19225. const base = joinUrlSegments(config.server?.origin ?? "", config.decodedBase);
  19226. return joinUrlSegments(base, removeLeadingSlash(rtn));
  19227. }
  19228. function getPublicAssetFilename(hash, config) {
  19229. return publicAssetUrlCache.get(config)?.get(hash);
  19230. }
  19231. const publicAssetUrlCache = /* @__PURE__ */ new WeakMap();
  19232. const publicAssetUrlRE = /__VITE_PUBLIC_ASSET__([a-z\d]{8})__/g;
  19233. function publicFileToBuiltUrl(url, config) {
  19234. if (config.command !== "build") {
  19235. return joinUrlSegments(config.decodedBase, url);
  19236. }
  19237. const hash = getHash(url);
  19238. let cache = publicAssetUrlCache.get(config);
  19239. if (!cache) {
  19240. cache = /* @__PURE__ */ new Map();
  19241. publicAssetUrlCache.set(config, cache);
  19242. }
  19243. if (!cache.get(hash)) {
  19244. cache.set(hash, url);
  19245. }
  19246. return `__VITE_PUBLIC_ASSET__${hash}__`;
  19247. }
  19248. const GIT_LFS_PREFIX = Buffer$1.from("version https://git-lfs.github.com");
  19249. function isGitLfsPlaceholder(content) {
  19250. if (content.length < GIT_LFS_PREFIX.length) return false;
  19251. return GIT_LFS_PREFIX.compare(content, 0, GIT_LFS_PREFIX.length) === 0;
  19252. }
  19253. async function fileToBuiltUrl(id, config, pluginContext, skipPublicCheck = false, forceInline) {
  19254. if (!skipPublicCheck && checkPublicFile(id, config)) {
  19255. return publicFileToBuiltUrl(id, config);
  19256. }
  19257. const cache = assetCache.get(config);
  19258. const cached = cache.get(id);
  19259. if (cached) {
  19260. return cached;
  19261. }
  19262. const file = cleanUrl(id);
  19263. const content = await fsp.readFile(file);
  19264. let url;
  19265. if (shouldInline(config, file, id, content, pluginContext, forceInline)) {
  19266. if (config.build.lib && isGitLfsPlaceholder(content)) {
  19267. config.logger.warn(
  19268. colors$1.yellow(`Inlined file ${id} was not downloaded via Git LFS`)
  19269. );
  19270. }
  19271. if (file.endsWith(".svg")) {
  19272. url = svgToDataURL(content);
  19273. } else {
  19274. const mimeType = lookup(file) ?? "application/octet-stream";
  19275. url = `data:${mimeType};base64,${content.toString("base64")}`;
  19276. }
  19277. } else {
  19278. const { search, hash } = parse$h(id);
  19279. const postfix = (search || "") + (hash || "");
  19280. const originalFileName = normalizePath$3(path$n.relative(config.root, file));
  19281. const referenceId = pluginContext.emitFile({
  19282. type: "asset",
  19283. // Ignore directory structure for asset file names
  19284. name: path$n.basename(file),
  19285. originalFileName,
  19286. source: content
  19287. });
  19288. generatedAssets.get(config).set(referenceId, { originalFileName });
  19289. url = `__VITE_ASSET__${referenceId}__${postfix ? `$_${postfix}__` : ``}`;
  19290. }
  19291. cache.set(id, url);
  19292. return url;
  19293. }
  19294. async function urlToBuiltUrl(url, importer, config, pluginContext, forceInline) {
  19295. if (checkPublicFile(url, config)) {
  19296. return publicFileToBuiltUrl(url, config);
  19297. }
  19298. const file = url[0] === "/" ? path$n.join(config.root, url) : path$n.join(path$n.dirname(importer), url);
  19299. return fileToBuiltUrl(
  19300. file,
  19301. config,
  19302. pluginContext,
  19303. // skip public check since we just did it above
  19304. true,
  19305. forceInline
  19306. );
  19307. }
  19308. const shouldInline = (config, file, id, content, pluginContext, forceInline) => {
  19309. if (config.build.lib) return true;
  19310. if (pluginContext.getModuleInfo(id)?.isEntry) return false;
  19311. if (forceInline !== void 0) return forceInline;
  19312. let limit;
  19313. if (typeof config.build.assetsInlineLimit === "function") {
  19314. const userShouldInline = config.build.assetsInlineLimit(file, content);
  19315. if (userShouldInline != null) return userShouldInline;
  19316. limit = DEFAULT_ASSETS_INLINE_LIMIT;
  19317. } else {
  19318. limit = Number(config.build.assetsInlineLimit);
  19319. }
  19320. if (file.endsWith(".html")) return false;
  19321. if (file.endsWith(".svg") && id.includes("#")) return false;
  19322. return content.length < limit && !isGitLfsPlaceholder(content);
  19323. };
  19324. const nestedQuotesRE = /"[^"']*'[^"]*"|'[^'"]*"[^']*'/;
  19325. function svgToDataURL(content) {
  19326. const stringContent = content.toString();
  19327. if (stringContent.includes("<text") || stringContent.includes("<foreignObject") || nestedQuotesRE.test(stringContent)) {
  19328. return `data:image/svg+xml;base64,${content.toString("base64")}`;
  19329. } else {
  19330. return "data:image/svg+xml," + stringContent.trim().replaceAll(/>\s+</g, "><").replaceAll('"', "'").replaceAll("%", "%25").replaceAll("#", "%23").replaceAll("<", "%3c").replaceAll(">", "%3e").replaceAll(/\s+/g, "%20");
  19331. }
  19332. }
  19333. const endsWithJSRE = /\.[cm]?js$/;
  19334. function manifestPlugin(config) {
  19335. const manifest = {};
  19336. let outputCount;
  19337. return {
  19338. name: "vite:manifest",
  19339. buildStart() {
  19340. outputCount = 0;
  19341. },
  19342. generateBundle({ format }, bundle) {
  19343. function getChunkName(chunk) {
  19344. return getChunkOriginalFileName(chunk, config.root, format);
  19345. }
  19346. function getInternalImports(imports) {
  19347. const filteredImports = [];
  19348. for (const file of imports) {
  19349. if (bundle[file] === void 0) {
  19350. continue;
  19351. }
  19352. filteredImports.push(getChunkName(bundle[file]));
  19353. }
  19354. return filteredImports;
  19355. }
  19356. function createChunk(chunk) {
  19357. const manifestChunk = {
  19358. file: chunk.fileName,
  19359. name: chunk.name
  19360. };
  19361. if (chunk.facadeModuleId) {
  19362. manifestChunk.src = getChunkName(chunk);
  19363. }
  19364. if (chunk.isEntry) {
  19365. manifestChunk.isEntry = true;
  19366. }
  19367. if (chunk.isDynamicEntry) {
  19368. manifestChunk.isDynamicEntry = true;
  19369. }
  19370. if (chunk.imports.length) {
  19371. const internalImports = getInternalImports(chunk.imports);
  19372. if (internalImports.length > 0) {
  19373. manifestChunk.imports = internalImports;
  19374. }
  19375. }
  19376. if (chunk.dynamicImports.length) {
  19377. const internalImports = getInternalImports(chunk.dynamicImports);
  19378. if (internalImports.length > 0) {
  19379. manifestChunk.dynamicImports = internalImports;
  19380. }
  19381. }
  19382. if (chunk.viteMetadata?.importedCss.size) {
  19383. manifestChunk.css = [...chunk.viteMetadata.importedCss];
  19384. }
  19385. if (chunk.viteMetadata?.importedAssets.size) {
  19386. manifestChunk.assets = [...chunk.viteMetadata.importedAssets];
  19387. }
  19388. return manifestChunk;
  19389. }
  19390. function createAsset(asset, src, isEntry) {
  19391. const manifestChunk = {
  19392. file: asset.fileName,
  19393. src
  19394. };
  19395. if (isEntry) manifestChunk.isEntry = true;
  19396. return manifestChunk;
  19397. }
  19398. const assets = generatedAssets.get(config);
  19399. const entryCssAssetFileNames = /* @__PURE__ */ new Set();
  19400. for (const [id, asset] of assets.entries()) {
  19401. if (asset.isEntry) {
  19402. try {
  19403. const fileName = this.getFileName(id);
  19404. entryCssAssetFileNames.add(fileName);
  19405. } catch (error) {
  19406. assets.delete(id);
  19407. }
  19408. }
  19409. }
  19410. const fileNameToAsset = /* @__PURE__ */ new Map();
  19411. for (const file in bundle) {
  19412. const chunk = bundle[file];
  19413. if (chunk.type === "chunk") {
  19414. manifest[getChunkName(chunk)] = createChunk(chunk);
  19415. } else if (chunk.type === "asset" && typeof chunk.name === "string") {
  19416. const src = chunk.originalFileName ?? chunk.name;
  19417. const isEntry = entryCssAssetFileNames.has(chunk.fileName);
  19418. const asset = createAsset(chunk, src, isEntry);
  19419. const file2 = manifest[src]?.file;
  19420. if (file2 && endsWithJSRE.test(file2)) continue;
  19421. manifest[src] = asset;
  19422. fileNameToAsset.set(chunk.fileName, asset);
  19423. }
  19424. }
  19425. for (const [referenceId, { originalFileName }] of assets.entries()) {
  19426. if (!manifest[originalFileName]) {
  19427. const fileName = this.getFileName(referenceId);
  19428. const asset = fileNameToAsset.get(fileName);
  19429. if (asset) {
  19430. manifest[originalFileName] = asset;
  19431. }
  19432. }
  19433. }
  19434. outputCount++;
  19435. const output = config.build.rollupOptions?.output;
  19436. const outputLength = Array.isArray(output) ? output.length : 1;
  19437. if (outputCount >= outputLength) {
  19438. this.emitFile({
  19439. fileName: typeof config.build.manifest === "string" ? config.build.manifest : ".vite/manifest.json",
  19440. type: "asset",
  19441. source: JSON.stringify(sortObjectKeys(manifest), void 0, 2)
  19442. });
  19443. }
  19444. }
  19445. };
  19446. }
  19447. function getChunkOriginalFileName(chunk, root, format) {
  19448. if (chunk.facadeModuleId) {
  19449. let name = normalizePath$3(path$n.relative(root, chunk.facadeModuleId));
  19450. if (format === "system" && !chunk.name.includes("-legacy")) {
  19451. const ext = path$n.extname(name);
  19452. const endPos = ext.length !== 0 ? -ext.length : void 0;
  19453. name = name.slice(0, endPos) + `-legacy` + ext;
  19454. }
  19455. return name.replace(/\0/g, "");
  19456. } else {
  19457. return `_` + path$n.basename(chunk.fileName);
  19458. }
  19459. }
  19460. const dataUriRE = /^([^/]+\/[^;,]+)(;base64)?,([\s\S]*)$/;
  19461. const base64RE = /base64/i;
  19462. const dataUriPrefix = `\0/@data-uri/`;
  19463. function dataURIPlugin() {
  19464. let resolved;
  19465. return {
  19466. name: "vite:data-uri",
  19467. buildStart() {
  19468. resolved = /* @__PURE__ */ new Map();
  19469. },
  19470. resolveId(id) {
  19471. if (!id.trimStart().startsWith("data:")) {
  19472. return;
  19473. }
  19474. const uri = new URL$3(id);
  19475. if (uri.protocol !== "data:") {
  19476. return;
  19477. }
  19478. const match = dataUriRE.exec(uri.pathname);
  19479. if (!match) {
  19480. return;
  19481. }
  19482. const [, mime, format, data] = match;
  19483. if (mime !== "text/javascript") {
  19484. throw new Error(
  19485. `data URI with non-JavaScript mime type is not supported. If you're using legacy JavaScript MIME types (such as 'application/javascript'), please use 'text/javascript' instead.`
  19486. );
  19487. }
  19488. const base64 = format && base64RE.test(format.substring(1));
  19489. const content = base64 ? Buffer.from(data, "base64").toString("utf-8") : data;
  19490. resolved.set(id, content);
  19491. return dataUriPrefix + id;
  19492. },
  19493. load(id) {
  19494. if (id.startsWith(dataUriPrefix)) {
  19495. return resolved.get(id.slice(dataUriPrefix.length));
  19496. }
  19497. }
  19498. };
  19499. }
  19500. /* es-module-lexer 1.5.4 */
  19501. var ImportType;!function(A){A[A.Static=1]="Static",A[A.Dynamic=2]="Dynamic",A[A.ImportMeta=3]="ImportMeta",A[A.StaticSourcePhase=4]="StaticSourcePhase",A[A.DynamicSourcePhase=5]="DynamicSourcePhase";}(ImportType||(ImportType={}));const A=1===new Uint8Array(new Uint16Array([1]).buffer)[0];function parse$d(E,g="@"){if(!C)return init.then((()=>parse$d(E)));const I=E.length+1,w=(C.__heap_base.value||C.__heap_base)+4*I-C.memory.buffer.byteLength;w>0&&C.memory.grow(Math.ceil(w/65536));const K=C.sa(I-1);if((A?B:Q)(E,new Uint16Array(C.memory.buffer,K,I)),!C.parse())throw Object.assign(new Error(`Parse error ${g}:${E.slice(0,C.e()).split("\n").length}:${C.e()-E.lastIndexOf("\n",C.e()-1)}`),{idx:C.e()});const D=[],o=[];for(;C.ri();){const A=C.is(),Q=C.ie(),B=C.it(),g=C.ai(),I=C.id(),w=C.ss(),K=C.se();let o;C.ip()&&(o=k(E.slice(-1===I?A-1:A,-1===I?Q+1:Q))),D.push({n:o,t:B,s:A,e:Q,ss:w,se:K,d:I,a:g});}for(;C.re();){const A=C.es(),Q=C.ee(),B=C.els(),g=C.ele(),I=E.slice(A,Q),w=I[0],K=B<0?void 0:E.slice(B,g),D=K?K[0]:"";o.push({s:A,e:Q,ls:B,le:g,n:'"'===w||"'"===w?k(I):I,ln:'"'===D||"'"===D?k(K):K});}function k(A){try{return (0, eval)(A)}catch(A){}}return [D,o,!!C.f(),!!C.ms()]}function Q(A,Q){const B=A.length;let C=0;for(;C<B;){const B=A.charCodeAt(C);Q[C++]=(255&B)<<8|B>>>8;}}function B(A,Q){const B=A.length;let C=0;for(;C<B;)Q[C]=A.charCodeAt(C++);}let C;const init=WebAssembly.compile((E="AGFzbQEAAAABKwhgAX8Bf2AEf39/fwBgAAF/YAAAYAF/AGADf39/AX9gAn9/AX9gA39/fwADMTAAAQECAgICAgICAgICAgICAgICAgIAAwMDBAQAAAUAAAAAAAMDAwAGAAAABwAGAgUEBQFwAQEBBQMBAAEGDwJ/AUHA8gALfwBBwPIACwd6FQZtZW1vcnkCAAJzYQAAAWUAAwJpcwAEAmllAAUCc3MABgJzZQAHAml0AAgCYWkACQJpZAAKAmlwAAsCZXMADAJlZQANA2VscwAOA2VsZQAPAnJpABACcmUAEQFmABICbXMAEwVwYXJzZQAUC19faGVhcF9iYXNlAwEKm0EwaAEBf0EAIAA2AoAKQQAoAtwJIgEgAEEBdGoiAEEAOwEAQQAgAEECaiIANgKECkEAIAA2AogKQQBBADYC4AlBAEEANgLwCUEAQQA2AugJQQBBADYC5AlBAEEANgL4CUEAQQA2AuwJIAEL0wEBA39BACgC8AkhBEEAQQAoAogKIgU2AvAJQQAgBDYC9AlBACAFQSRqNgKICiAEQSBqQeAJIAQbIAU2AgBBACgC1AkhBEEAKALQCSEGIAUgATYCACAFIAA2AgggBSACIAJBAmpBACAGIANGIgAbIAQgA0YiBBs2AgwgBSADNgIUIAVBADYCECAFIAI2AgQgBUEANgIgIAVBA0EBQQIgABsgBBs2AhwgBUEAKALQCSADRiICOgAYAkACQCACDQBBACgC1AkgA0cNAQtBAEEBOgCMCgsLXgEBf0EAKAL4CSIEQRBqQeQJIAQbQQAoAogKIgQ2AgBBACAENgL4CUEAIARBFGo2AogKQQBBAToAjAogBEEANgIQIAQgAzYCDCAEIAI2AgggBCABNgIEIAQgADYCAAsIAEEAKAKQCgsVAEEAKALoCSgCAEEAKALcCWtBAXULHgEBf0EAKALoCSgCBCIAQQAoAtwJa0EBdUF/IAAbCxUAQQAoAugJKAIIQQAoAtwJa0EBdQseAQF/QQAoAugJKAIMIgBBACgC3AlrQQF1QX8gABsLCwBBACgC6AkoAhwLHgEBf0EAKALoCSgCECIAQQAoAtwJa0EBdUF/IAAbCzsBAX8CQEEAKALoCSgCFCIAQQAoAtAJRw0AQX8PCwJAIABBACgC1AlHDQBBfg8LIABBACgC3AlrQQF1CwsAQQAoAugJLQAYCxUAQQAoAuwJKAIAQQAoAtwJa0EBdQsVAEEAKALsCSgCBEEAKALcCWtBAXULHgEBf0EAKALsCSgCCCIAQQAoAtwJa0EBdUF/IAAbCx4BAX9BACgC7AkoAgwiAEEAKALcCWtBAXVBfyAAGwslAQF/QQBBACgC6AkiAEEgakHgCSAAGygCACIANgLoCSAAQQBHCyUBAX9BAEEAKALsCSIAQRBqQeQJIAAbKAIAIgA2AuwJIABBAEcLCABBAC0AlAoLCABBAC0AjAoL3Q0BBX8jAEGA0ABrIgAkAEEAQQE6AJQKQQBBACgC2Ak2ApwKQQBBACgC3AlBfmoiATYCsApBACABQQAoAoAKQQF0aiICNgK0CkEAQQA6AIwKQQBBADsBlgpBAEEAOwGYCkEAQQA6AKAKQQBBADYCkApBAEEAOgD8CUEAIABBgBBqNgKkCkEAIAA2AqgKQQBBADoArAoCQAJAAkACQANAQQAgAUECaiIDNgKwCiABIAJPDQECQCADLwEAIgJBd2pBBUkNAAJAAkACQAJAAkAgAkGbf2oOBQEICAgCAAsgAkEgRg0EIAJBL0YNAyACQTtGDQIMBwtBAC8BmAoNASADEBVFDQEgAUEEakGCCEEKEC8NARAWQQAtAJQKDQFBAEEAKAKwCiIBNgKcCgwHCyADEBVFDQAgAUEEakGMCEEKEC8NABAXC0EAQQAoArAKNgKcCgwBCwJAIAEvAQQiA0EqRg0AIANBL0cNBBAYDAELQQEQGQtBACgCtAohAkEAKAKwCiEBDAALC0EAIQIgAyEBQQAtAPwJDQIMAQtBACABNgKwCkEAQQA6AJQKCwNAQQAgAUECaiIDNgKwCgJAAkACQAJAAkACQAJAIAFBACgCtApPDQAgAy8BACICQXdqQQVJDQYCQAJAAkACQAJAAkACQAJAAkACQCACQWBqDgoQDwYPDw8PBQECAAsCQAJAAkACQCACQaB/ag4KCxISAxIBEhISAgALIAJBhX9qDgMFEQYJC0EALwGYCg0QIAMQFUUNECABQQRqQYIIQQoQLw0QEBYMEAsgAxAVRQ0PIAFBBGpBjAhBChAvDQ8QFwwPCyADEBVFDQ4gASkABELsgISDsI7AOVINDiABLwEMIgNBd2oiAUEXSw0MQQEgAXRBn4CABHFFDQwMDQtBAEEALwGYCiIBQQFqOwGYCkEAKAKkCiABQQN0aiIBQQE2AgAgAUEAKAKcCjYCBAwNC0EALwGYCiIDRQ0JQQAgA0F/aiIDOwGYCkEALwGWCiICRQ0MQQAoAqQKIANB//8DcUEDdGooAgBBBUcNDAJAIAJBAnRBACgCqApqQXxqKAIAIgMoAgQNACADQQAoApwKQQJqNgIEC0EAIAJBf2o7AZYKIAMgAUEEajYCDAwMCwJAQQAoApwKIgEvAQBBKUcNAEEAKALwCSIDRQ0AIAMoAgQgAUcNAEEAQQAoAvQJIgM2AvAJAkAgA0UNACADQQA2AiAMAQtBAEEANgLgCQtBAEEALwGYCiIDQQFqOwGYCkEAKAKkCiADQQN0aiIDQQZBAkEALQCsChs2AgAgAyABNgIEQQBBADoArAoMCwtBAC8BmAoiAUUNB0EAIAFBf2oiATsBmApBACgCpAogAUH//wNxQQN0aigCAEEERg0EDAoLQScQGgwJC0EiEBoMCAsgAkEvRw0HAkACQCABLwEEIgFBKkYNACABQS9HDQEQGAwKC0EBEBkMCQsCQAJAAkACQEEAKAKcCiIBLwEAIgMQG0UNAAJAAkAgA0FVag4EAAkBAwkLIAFBfmovAQBBK0YNAwwICyABQX5qLwEAQS1GDQIMBwsgA0EpRw0BQQAoAqQKQQAvAZgKIgJBA3RqKAIEEBxFDQIMBgsgAUF+ai8BAEFQakH//wNxQQpPDQULQQAvAZgKIQILAkACQCACQf//A3EiAkUNACADQeYARw0AQQAoAqQKIAJBf2pBA3RqIgQoAgBBAUcNACABQX5qLwEAQe8ARw0BIAQoAgRBlghBAxAdRQ0BDAULIANB/QBHDQBBACgCpAogAkEDdGoiAigCBBAeDQQgAigCAEEGRg0ECyABEB8NAyADRQ0DIANBL0ZBAC0AoApBAEdxDQMCQEEAKAL4CSICRQ0AIAEgAigCAEkNACABIAIoAgRNDQQLIAFBfmohAUEAKALcCSECAkADQCABQQJqIgQgAk0NAUEAIAE2ApwKIAEvAQAhAyABQX5qIgQhASADECBFDQALIARBAmohBAsCQCADQf//A3EQIUUNACAEQX5qIQECQANAIAFBAmoiAyACTQ0BQQAgATYCnAogAS8BACEDIAFBfmoiBCEBIAMQIQ0ACyAEQQJqIQMLIAMQIg0EC0EAQQE6AKAKDAcLQQAoAqQKQQAvAZgKIgFBA3QiA2pBACgCnAo2AgRBACABQQFqOwGYCkEAKAKkCiADakEDNgIACxAjDAULQQAtAPwJQQAvAZYKQQAvAZgKcnJFIQIMBwsQJEEAQQA6AKAKDAMLECVBACECDAULIANBoAFHDQELQQBBAToArAoLQQBBACgCsAo2ApwKC0EAKAKwCiEBDAALCyAAQYDQAGokACACCxoAAkBBACgC3AkgAEcNAEEBDwsgAEF+ahAmC/4KAQZ/QQBBACgCsAoiAEEMaiIBNgKwCkEAKAL4CSECQQEQKSEDAkACQAJAAkACQAJAAkACQAJAQQAoArAKIgQgAUcNACADEChFDQELAkACQAJAAkACQAJAAkAgA0EqRg0AIANB+wBHDQFBACAEQQJqNgKwCkEBECkhA0EAKAKwCiEEA0ACQAJAIANB//8DcSIDQSJGDQAgA0EnRg0AIAMQLBpBACgCsAohAwwBCyADEBpBAEEAKAKwCkECaiIDNgKwCgtBARApGgJAIAQgAxAtIgNBLEcNAEEAQQAoArAKQQJqNgKwCkEBECkhAwsgA0H9AEYNA0EAKAKwCiIFIARGDQ8gBSEEIAVBACgCtApNDQAMDwsLQQAgBEECajYCsApBARApGkEAKAKwCiIDIAMQLRoMAgtBAEEAOgCUCgJAAkACQAJAAkACQCADQZ9/ag4MAgsEAQsDCwsLCwsFAAsgA0H2AEYNBAwKC0EAIARBDmoiAzYCsAoCQAJAAkBBARApQZ9/ag4GABICEhIBEgtBACgCsAoiBSkAAkLzgOSD4I3AMVINESAFLwEKECFFDRFBACAFQQpqNgKwCkEAECkaC0EAKAKwCiIFQQJqQbIIQQ4QLw0QIAUvARAiAkF3aiIBQRdLDQ1BASABdEGfgIAEcUUNDQwOC0EAKAKwCiIFKQACQuyAhIOwjsA5Ug0PIAUvAQoiAkF3aiIBQRdNDQYMCgtBACAEQQpqNgKwCkEAECkaQQAoArAKIQQLQQAgBEEQajYCsAoCQEEBECkiBEEqRw0AQQBBACgCsApBAmo2ArAKQQEQKSEEC0EAKAKwCiEDIAQQLBogA0EAKAKwCiIEIAMgBBACQQBBACgCsApBfmo2ArAKDwsCQCAEKQACQuyAhIOwjsA5Ug0AIAQvAQoQIEUNAEEAIARBCmo2ArAKQQEQKSEEQQAoArAKIQMgBBAsGiADQQAoArAKIgQgAyAEEAJBAEEAKAKwCkF+ajYCsAoPC0EAIARBBGoiBDYCsAoLQQAgBEEGajYCsApBAEEAOgCUCkEBECkhBEEAKAKwCiEDIAQQLCEEQQAoArAKIQIgBEHf/wNxIgFB2wBHDQNBACACQQJqNgKwCkEBECkhBUEAKAKwCiEDQQAhBAwEC0EAQQE6AIwKQQBBACgCsApBAmo2ArAKC0EBECkhBEEAKAKwCiEDAkAgBEHmAEcNACADQQJqQawIQQYQLw0AQQAgA0EIajYCsAogAEEBEClBABArIAJBEGpB5AkgAhshAwNAIAMoAgAiA0UNBSADQgA3AgggA0EQaiEDDAALC0EAIANBfmo2ArAKDAMLQQEgAXRBn4CABHFFDQMMBAtBASEECwNAAkACQCAEDgIAAQELIAVB//8DcRAsGkEBIQQMAQsCQAJAQQAoArAKIgQgA0YNACADIAQgAyAEEAJBARApIQQCQCABQdsARw0AIARBIHJB/QBGDQQLQQAoArAKIQMCQCAEQSxHDQBBACADQQJqNgKwCkEBECkhBUEAKAKwCiEDIAVBIHJB+wBHDQILQQAgA0F+ajYCsAoLIAFB2wBHDQJBACACQX5qNgKwCg8LQQAhBAwACwsPCyACQaABRg0AIAJB+wBHDQQLQQAgBUEKajYCsApBARApIgVB+wBGDQMMAgsCQCACQVhqDgMBAwEACyACQaABRw0CC0EAIAVBEGo2ArAKAkBBARApIgVBKkcNAEEAQQAoArAKQQJqNgKwCkEBECkhBQsgBUEoRg0BC0EAKAKwCiEBIAUQLBpBACgCsAoiBSABTQ0AIAQgAyABIAUQAkEAQQAoArAKQX5qNgKwCg8LIAQgA0EAQQAQAkEAIARBDGo2ArAKDwsQJQvcCAEGf0EAIQBBAEEAKAKwCiIBQQxqIgI2ArAKQQEQKSEDQQAoArAKIQQCQAJAAkACQAJAAkACQAJAIANBLkcNAEEAIARBAmo2ArAKAkBBARApIgNB8wBGDQAgA0HtAEcNB0EAKAKwCiIDQQJqQZwIQQYQLw0HAkBBACgCnAoiBBAqDQAgBC8BAEEuRg0ICyABIAEgA0EIakEAKALUCRABDwtBACgCsAoiA0ECakGiCEEKEC8NBgJAQQAoApwKIgQQKg0AIAQvAQBBLkYNBwsgA0EMaiEDDAELIANB8wBHDQEgBCACTQ0BQQYhAEEAIQIgBEECakGiCEEKEC8NAiAEQQxqIQMCQCAELwEMIgVBd2oiBEEXSw0AQQEgBHRBn4CABHENAQsgBUGgAUcNAgtBACADNgKwCkEBIQBBARApIQMLAkACQAJAAkAgA0H7AEYNACADQShHDQFBACgCpApBAC8BmAoiA0EDdGoiBEEAKAKwCjYCBEEAIANBAWo7AZgKIARBBTYCAEEAKAKcCi8BAEEuRg0HQQBBACgCsAoiBEECajYCsApBARApIQMgAUEAKAKwCkEAIAQQAQJAAkAgAA0AQQAoAvAJIQQMAQtBACgC8AkiBEEFNgIcC0EAQQAvAZYKIgBBAWo7AZYKQQAoAqgKIABBAnRqIAQ2AgACQCADQSJGDQAgA0EnRg0AQQBBACgCsApBfmo2ArAKDwsgAxAaQQBBACgCsApBAmoiAzYCsAoCQAJAAkBBARApQVdqDgQBAgIAAgtBAEEAKAKwCkECajYCsApBARApGkEAKALwCSIEIAM2AgQgBEEBOgAYIARBACgCsAoiAzYCEEEAIANBfmo2ArAKDwtBACgC8AkiBCADNgIEIARBAToAGEEAQQAvAZgKQX9qOwGYCiAEQQAoArAKQQJqNgIMQQBBAC8BlgpBf2o7AZYKDwtBAEEAKAKwCkF+ajYCsAoPCyAADQJBACgCsAohA0EALwGYCg0BA0ACQAJAAkAgA0EAKAK0Ck8NAEEBECkiA0EiRg0BIANBJ0YNASADQf0ARw0CQQBBACgCsApBAmo2ArAKC0EBECkhBEEAKAKwCiEDAkAgBEHmAEcNACADQQJqQawIQQYQLw0JC0EAIANBCGo2ArAKAkBBARApIgNBIkYNACADQSdHDQkLIAEgA0EAECsPCyADEBoLQQBBACgCsApBAmoiAzYCsAoMAAsLIAANAUEGIQBBACECAkAgA0FZag4EBAMDBAALIANBIkYNAwwCC0EAIANBfmo2ArAKDwtBDCEAQQEhAgtBACgCsAoiAyABIABBAXRqRw0AQQAgA0F+ajYCsAoPC0EALwGYCg0CQQAoArAKIQNBACgCtAohAANAIAMgAE8NAQJAAkAgAy8BACIEQSdGDQAgBEEiRw0BCyABIAQgAhArDwtBACADQQJqIgM2ArAKDAALCxAlCw8LQQBBACgCsApBfmo2ArAKC0cBA39BACgCsApBAmohAEEAKAK0CiEBAkADQCAAIgJBfmogAU8NASACQQJqIQAgAi8BAEF2ag4EAQAAAQALC0EAIAI2ArAKC5gBAQN/QQBBACgCsAoiAUECajYCsAogAUEGaiEBQQAoArQKIQIDQAJAAkACQCABQXxqIAJPDQAgAUF+ai8BACEDAkACQCAADQAgA0EqRg0BIANBdmoOBAIEBAIECyADQSpHDQMLIAEvAQBBL0cNAkEAIAFBfmo2ArAKDAELIAFBfmohAQtBACABNgKwCg8LIAFBAmohAQwACwuIAQEEf0EAKAKwCiEBQQAoArQKIQICQAJAA0AgASIDQQJqIQEgAyACTw0BIAEvAQAiBCAARg0CAkAgBEHcAEYNACAEQXZqDgQCAQECAQsgA0EEaiEBIAMvAQRBDUcNACADQQZqIAEgAy8BBkEKRhshAQwACwtBACABNgKwChAlDwtBACABNgKwCgtsAQF/AkACQCAAQV9qIgFBBUsNAEEBIAF0QTFxDQELIABBRmpB//8DcUEGSQ0AIABBKUcgAEFYakH//wNxQQdJcQ0AAkAgAEGlf2oOBAEAAAEACyAAQf0ARyAAQYV/akH//wNxQQRJcQ8LQQELLgEBf0EBIQECQCAAQaYJQQUQHQ0AIABBlghBAxAdDQAgAEGwCUECEB0hAQsgAQtGAQN/QQAhAwJAIAAgAkEBdCICayIEQQJqIgBBACgC3AkiBUkNACAAIAEgAhAvDQACQCAAIAVHDQBBAQ8LIAQQJiEDCyADC4MBAQJ/QQEhAQJAAkACQAJAAkACQCAALwEAIgJBRWoOBAUEBAEACwJAIAJBm39qDgQDBAQCAAsgAkEpRg0EIAJB+QBHDQMgAEF+akG8CUEGEB0PCyAAQX5qLwEAQT1GDwsgAEF+akG0CUEEEB0PCyAAQX5qQcgJQQMQHQ8LQQAhAQsgAQu0AwECf0EAIQECQAJAAkACQAJAAkACQAJAAkACQCAALwEAQZx/ag4UAAECCQkJCQMJCQQFCQkGCQcJCQgJCwJAAkAgAEF+ai8BAEGXf2oOBAAKCgEKCyAAQXxqQcoIQQIQHQ8LIABBfGpBzghBAxAdDwsCQAJAAkAgAEF+ai8BAEGNf2oOAwABAgoLAkAgAEF8ai8BACICQeEARg0AIAJB7ABHDQogAEF6akHlABAnDwsgAEF6akHjABAnDwsgAEF8akHUCEEEEB0PCyAAQXxqQdwIQQYQHQ8LIABBfmovAQBB7wBHDQYgAEF8ai8BAEHlAEcNBgJAIABBemovAQAiAkHwAEYNACACQeMARw0HIABBeGpB6AhBBhAdDwsgAEF4akH0CEECEB0PCyAAQX5qQfgIQQQQHQ8LQQEhASAAQX5qIgBB6QAQJw0EIABBgAlBBRAdDwsgAEF+akHkABAnDwsgAEF+akGKCUEHEB0PCyAAQX5qQZgJQQQQHQ8LAkAgAEF+ai8BACICQe8ARg0AIAJB5QBHDQEgAEF8akHuABAnDwsgAEF8akGgCUEDEB0hAQsgAQs0AQF/QQEhAQJAIABBd2pB//8DcUEFSQ0AIABBgAFyQaABRg0AIABBLkcgABAocSEBCyABCzABAX8CQAJAIABBd2oiAUEXSw0AQQEgAXRBjYCABHENAQsgAEGgAUYNAEEADwtBAQtOAQJ/QQAhAQJAAkAgAC8BACICQeUARg0AIAJB6wBHDQEgAEF+akH4CEEEEB0PCyAAQX5qLwEAQfUARw0AIABBfGpB3AhBBhAdIQELIAEL3gEBBH9BACgCsAohAEEAKAK0CiEBAkACQAJAA0AgACICQQJqIQAgAiABTw0BAkACQAJAIAAvAQAiA0Gkf2oOBQIDAwMBAAsgA0EkRw0CIAIvAQRB+wBHDQJBACACQQRqIgA2ArAKQQBBAC8BmAoiAkEBajsBmApBACgCpAogAkEDdGoiAkEENgIAIAIgADYCBA8LQQAgADYCsApBAEEALwGYCkF/aiIAOwGYCkEAKAKkCiAAQf//A3FBA3RqKAIAQQNHDQMMBAsgAkEEaiEADAALC0EAIAA2ArAKCxAlCwtwAQJ/AkACQANAQQBBACgCsAoiAEECaiIBNgKwCiAAQQAoArQKTw0BAkACQAJAIAEvAQAiAUGlf2oOAgECAAsCQCABQXZqDgQEAwMEAAsgAUEvRw0CDAQLEC4aDAELQQAgAEEEajYCsAoMAAsLECULCzUBAX9BAEEBOgD8CUEAKAKwCiEAQQBBACgCtApBAmo2ArAKQQAgAEEAKALcCWtBAXU2ApAKC0MBAn9BASEBAkAgAC8BACICQXdqQf//A3FBBUkNACACQYABckGgAUYNAEEAIQEgAhAoRQ0AIAJBLkcgABAqcg8LIAELPQECf0EAIQICQEEAKALcCSIDIABLDQAgAC8BACABRw0AAkAgAyAARw0AQQEPCyAAQX5qLwEAECAhAgsgAgtoAQJ/QQEhAQJAAkAgAEFfaiICQQVLDQBBASACdEExcQ0BCyAAQfj/A3FBKEYNACAAQUZqQf//A3FBBkkNAAJAIABBpX9qIgJBA0sNACACQQFHDQELIABBhX9qQf//A3FBBEkhAQsgAQucAQEDf0EAKAKwCiEBAkADQAJAAkAgAS8BACICQS9HDQACQCABLwECIgFBKkYNACABQS9HDQQQGAwCCyAAEBkMAQsCQAJAIABFDQAgAkF3aiIBQRdLDQFBASABdEGfgIAEcUUNAQwCCyACECFFDQMMAQsgAkGgAUcNAgtBAEEAKAKwCiIDQQJqIgE2ArAKIANBACgCtApJDQALCyACCzEBAX9BACEBAkAgAC8BAEEuRw0AIABBfmovAQBBLkcNACAAQXxqLwEAQS5GIQELIAELnAQBAX8CQCABQSJGDQAgAUEnRg0AECUPC0EAKAKwCiEDIAEQGiAAIANBAmpBACgCsApBACgC0AkQAQJAIAJFDQBBACgC8AlBBDYCHAtBAEEAKAKwCkECajYCsAoCQAJAAkACQEEAECkiAUHhAEYNACABQfcARg0BQQAoArAKIQEMAgtBACgCsAoiAUECakHACEEKEC8NAUEGIQAMAgtBACgCsAoiAS8BAkHpAEcNACABLwEEQfQARw0AQQQhACABLwEGQegARg0BC0EAIAFBfmo2ArAKDwtBACABIABBAXRqNgKwCgJAQQEQKUH7AEYNAEEAIAE2ArAKDwtBACgCsAoiAiEAA0BBACAAQQJqNgKwCgJAAkACQEEBECkiAEEiRg0AIABBJ0cNAUEnEBpBAEEAKAKwCkECajYCsApBARApIQAMAgtBIhAaQQBBACgCsApBAmo2ArAKQQEQKSEADAELIAAQLCEACwJAIABBOkYNAEEAIAE2ArAKDwtBAEEAKAKwCkECajYCsAoCQEEBECkiAEEiRg0AIABBJ0YNAEEAIAE2ArAKDwsgABAaQQBBACgCsApBAmo2ArAKAkACQEEBECkiAEEsRg0AIABB/QBGDQFBACABNgKwCg8LQQBBACgCsApBAmo2ArAKQQEQKUH9AEYNAEEAKAKwCiEADAELC0EAKALwCSIBIAI2AhAgAUEAKAKwCkECajYCDAttAQJ/AkACQANAAkAgAEH//wNxIgFBd2oiAkEXSw0AQQEgAnRBn4CABHENAgsgAUGgAUYNASAAIQIgARAoDQJBACECQQBBACgCsAoiAEECajYCsAogAC8BAiIADQAMAgsLIAAhAgsgAkH//wNxC6sBAQR/AkACQEEAKAKwCiICLwEAIgNB4QBGDQAgASEEIAAhBQwBC0EAIAJBBGo2ArAKQQEQKSECQQAoArAKIQUCQAJAIAJBIkYNACACQSdGDQAgAhAsGkEAKAKwCiEEDAELIAIQGkEAQQAoArAKQQJqIgQ2ArAKC0EBECkhA0EAKAKwCiECCwJAIAIgBUYNACAFIARBACAAIAAgAUYiAhtBACABIAIbEAILIAMLcgEEf0EAKAKwCiEAQQAoArQKIQECQAJAA0AgAEECaiECIAAgAU8NAQJAAkAgAi8BACIDQaR/ag4CAQQACyACIQAgA0F2ag4EAgEBAgELIABBBGohAAwACwtBACACNgKwChAlQQAPC0EAIAI2ArAKQd0AC0kBA39BACEDAkAgAkUNAAJAA0AgAC0AACIEIAEtAAAiBUcNASABQQFqIQEgAEEBaiEAIAJBf2oiAg0ADAILCyAEIAVrIQMLIAMLC+wBAgBBgAgLzgEAAHgAcABvAHIAdABtAHAAbwByAHQAZgBvAHIAZQB0AGEAbwB1AHIAYwBlAHIAbwBtAHUAbgBjAHQAaQBvAG4AcwBzAGUAcgB0AHYAbwB5AGkAZQBkAGUAbABlAGMAbwBuAHQAaQBuAGkAbgBzAHQAYQBuAHQAeQBiAHIAZQBhAHIAZQB0AHUAcgBkAGUAYgB1AGcAZwBlAGEAdwBhAGkAdABoAHIAdwBoAGkAbABlAGkAZgBjAGEAdABjAGYAaQBuAGEAbABsAGUAbABzAABB0AkLEAEAAAACAAAAAAQAAEA5AAA=","undefined"!=typeof Buffer?Buffer.from(E,"base64"):Uint8Array.from(atob(E),(A=>A.charCodeAt(0))))).then(WebAssembly.instantiate).then((({exports:A})=>{C=A;}));var E;
  19502. var convertSourceMap$1 = {};
  19503. (function (exports) {
  19504. Object.defineProperty(exports, 'commentRegex', {
  19505. get: function getCommentRegex () {
  19506. // Groups: 1: media type, 2: MIME type, 3: charset, 4: encoding, 5: data.
  19507. return /^\s*?\/[\/\*][@#]\s+?sourceMappingURL=data:(((?:application|text)\/json)(?:;charset=([^;,]+?)?)?)?(?:;(base64))?,(.*?)$/mg;
  19508. }
  19509. });
  19510. Object.defineProperty(exports, 'mapFileCommentRegex', {
  19511. get: function getMapFileCommentRegex () {
  19512. // Matches sourceMappingURL in either // or /* comment styles.
  19513. return /(?:\/\/[@#][ \t]+?sourceMappingURL=([^\s'"`]+?)[ \t]*?$)|(?:\/\*[@#][ \t]+sourceMappingURL=([^*]+?)[ \t]*?(?:\*\/){1}[ \t]*?$)/mg;
  19514. }
  19515. });
  19516. var decodeBase64;
  19517. if (typeof Buffer !== 'undefined') {
  19518. if (typeof Buffer.from === 'function') {
  19519. decodeBase64 = decodeBase64WithBufferFrom;
  19520. } else {
  19521. decodeBase64 = decodeBase64WithNewBuffer;
  19522. }
  19523. } else {
  19524. decodeBase64 = decodeBase64WithAtob;
  19525. }
  19526. function decodeBase64WithBufferFrom(base64) {
  19527. return Buffer.from(base64, 'base64').toString();
  19528. }
  19529. function decodeBase64WithNewBuffer(base64) {
  19530. if (typeof value === 'number') {
  19531. throw new TypeError('The value to decode must not be of type number.');
  19532. }
  19533. return new Buffer(base64, 'base64').toString();
  19534. }
  19535. function decodeBase64WithAtob(base64) {
  19536. return decodeURIComponent(escape(atob(base64)));
  19537. }
  19538. function stripComment(sm) {
  19539. return sm.split(',').pop();
  19540. }
  19541. function readFromFileMap(sm, read) {
  19542. var r = exports.mapFileCommentRegex.exec(sm);
  19543. // for some odd reason //# .. captures in 1 and /* .. */ in 2
  19544. var filename = r[1] || r[2];
  19545. try {
  19546. var sm = read(filename);
  19547. if (sm != null && typeof sm.catch === 'function') {
  19548. return sm.catch(throwError);
  19549. } else {
  19550. return sm;
  19551. }
  19552. } catch (e) {
  19553. throwError(e);
  19554. }
  19555. function throwError(e) {
  19556. throw new Error('An error occurred while trying to read the map file at ' + filename + '\n' + e.stack);
  19557. }
  19558. }
  19559. function Converter (sm, opts) {
  19560. opts = opts || {};
  19561. if (opts.hasComment) {
  19562. sm = stripComment(sm);
  19563. }
  19564. if (opts.encoding === 'base64') {
  19565. sm = decodeBase64(sm);
  19566. } else if (opts.encoding === 'uri') {
  19567. sm = decodeURIComponent(sm);
  19568. }
  19569. if (opts.isJSON || opts.encoding) {
  19570. sm = JSON.parse(sm);
  19571. }
  19572. this.sourcemap = sm;
  19573. }
  19574. Converter.prototype.toJSON = function (space) {
  19575. return JSON.stringify(this.sourcemap, null, space);
  19576. };
  19577. if (typeof Buffer !== 'undefined') {
  19578. if (typeof Buffer.from === 'function') {
  19579. Converter.prototype.toBase64 = encodeBase64WithBufferFrom;
  19580. } else {
  19581. Converter.prototype.toBase64 = encodeBase64WithNewBuffer;
  19582. }
  19583. } else {
  19584. Converter.prototype.toBase64 = encodeBase64WithBtoa;
  19585. }
  19586. function encodeBase64WithBufferFrom() {
  19587. var json = this.toJSON();
  19588. return Buffer.from(json, 'utf8').toString('base64');
  19589. }
  19590. function encodeBase64WithNewBuffer() {
  19591. var json = this.toJSON();
  19592. if (typeof json === 'number') {
  19593. throw new TypeError('The json to encode must not be of type number.');
  19594. }
  19595. return new Buffer(json, 'utf8').toString('base64');
  19596. }
  19597. function encodeBase64WithBtoa() {
  19598. var json = this.toJSON();
  19599. return btoa(unescape(encodeURIComponent(json)));
  19600. }
  19601. Converter.prototype.toURI = function () {
  19602. var json = this.toJSON();
  19603. return encodeURIComponent(json);
  19604. };
  19605. Converter.prototype.toComment = function (options) {
  19606. var encoding, content, data;
  19607. if (options != null && options.encoding === 'uri') {
  19608. encoding = '';
  19609. content = this.toURI();
  19610. } else {
  19611. encoding = ';base64';
  19612. content = this.toBase64();
  19613. }
  19614. data = 'sourceMappingURL=data:application/json;charset=utf-8' + encoding + ',' + content;
  19615. return options != null && options.multiline ? '/*# ' + data + ' */' : '//# ' + data;
  19616. };
  19617. // returns copy instead of original
  19618. Converter.prototype.toObject = function () {
  19619. return JSON.parse(this.toJSON());
  19620. };
  19621. Converter.prototype.addProperty = function (key, value) {
  19622. if (this.sourcemap.hasOwnProperty(key)) throw new Error('property "' + key + '" already exists on the sourcemap, use set property instead');
  19623. return this.setProperty(key, value);
  19624. };
  19625. Converter.prototype.setProperty = function (key, value) {
  19626. this.sourcemap[key] = value;
  19627. return this;
  19628. };
  19629. Converter.prototype.getProperty = function (key) {
  19630. return this.sourcemap[key];
  19631. };
  19632. exports.fromObject = function (obj) {
  19633. return new Converter(obj);
  19634. };
  19635. exports.fromJSON = function (json) {
  19636. return new Converter(json, { isJSON: true });
  19637. };
  19638. exports.fromURI = function (uri) {
  19639. return new Converter(uri, { encoding: 'uri' });
  19640. };
  19641. exports.fromBase64 = function (base64) {
  19642. return new Converter(base64, { encoding: 'base64' });
  19643. };
  19644. exports.fromComment = function (comment) {
  19645. var m, encoding;
  19646. comment = comment
  19647. .replace(/^\/\*/g, '//')
  19648. .replace(/\*\/$/g, '');
  19649. m = exports.commentRegex.exec(comment);
  19650. encoding = m && m[4] || 'uri';
  19651. return new Converter(comment, { encoding: encoding, hasComment: true });
  19652. };
  19653. function makeConverter(sm) {
  19654. return new Converter(sm, { isJSON: true });
  19655. }
  19656. exports.fromMapFileComment = function (comment, read) {
  19657. if (typeof read === 'string') {
  19658. throw new Error(
  19659. 'String directory paths are no longer supported with `fromMapFileComment`\n' +
  19660. 'Please review the Upgrading documentation at https://github.com/thlorenz/convert-source-map#upgrading'
  19661. )
  19662. }
  19663. var sm = readFromFileMap(comment, read);
  19664. if (sm != null && typeof sm.then === 'function') {
  19665. return sm.then(makeConverter);
  19666. } else {
  19667. return makeConverter(sm);
  19668. }
  19669. };
  19670. // Finds last sourcemap comment in file or returns null if none was found
  19671. exports.fromSource = function (content) {
  19672. var m = content.match(exports.commentRegex);
  19673. return m ? exports.fromComment(m.pop()) : null;
  19674. };
  19675. // Finds last sourcemap comment in file or returns null if none was found
  19676. exports.fromMapFileSource = function (content, read) {
  19677. if (typeof read === 'string') {
  19678. throw new Error(
  19679. 'String directory paths are no longer supported with `fromMapFileSource`\n' +
  19680. 'Please review the Upgrading documentation at https://github.com/thlorenz/convert-source-map#upgrading'
  19681. )
  19682. }
  19683. var m = content.match(exports.mapFileCommentRegex);
  19684. return m ? exports.fromMapFileComment(m.pop(), read) : null;
  19685. };
  19686. exports.removeComments = function (src) {
  19687. return src.replace(exports.commentRegex, '');
  19688. };
  19689. exports.removeMapFileComments = function (src) {
  19690. return src.replace(exports.mapFileCommentRegex, '');
  19691. };
  19692. exports.generateMapFileComment = function (file, options) {
  19693. var data = 'sourceMappingURL=' + file;
  19694. return options && options.multiline ? '/*# ' + data + ' */' : '//# ' + data;
  19695. };
  19696. } (convertSourceMap$1));
  19697. var convertSourceMap = /*@__PURE__*/getDefaultExportFromCjs(convertSourceMap$1);
  19698. const debug$g = createDebugger("vite:sourcemap", {
  19699. onlyWhenFocused: true
  19700. });
  19701. const virtualSourceRE = /^(?:dep:|browser-external:|virtual:)|\0/;
  19702. async function computeSourceRoute(map, file) {
  19703. let sourceRoot;
  19704. try {
  19705. sourceRoot = await fsp.realpath(
  19706. path$n.resolve(path$n.dirname(file), map.sourceRoot || "")
  19707. );
  19708. } catch {
  19709. }
  19710. return sourceRoot;
  19711. }
  19712. async function injectSourcesContent(map, file, logger) {
  19713. let sourceRootPromise;
  19714. const missingSources = [];
  19715. const sourcesContent = map.sourcesContent || [];
  19716. const sourcesContentPromises = [];
  19717. for (let index = 0; index < map.sources.length; index++) {
  19718. const sourcePath = map.sources[index];
  19719. if (sourcesContent[index] == null && sourcePath && !virtualSourceRE.test(sourcePath)) {
  19720. sourcesContentPromises.push(
  19721. (async () => {
  19722. sourceRootPromise ??= computeSourceRoute(map, file);
  19723. const sourceRoot = await sourceRootPromise;
  19724. let resolvedSourcePath = cleanUrl(decodeURI(sourcePath));
  19725. if (sourceRoot) {
  19726. resolvedSourcePath = path$n.resolve(sourceRoot, resolvedSourcePath);
  19727. }
  19728. sourcesContent[index] = await fsp.readFile(resolvedSourcePath, "utf-8").catch(() => {
  19729. missingSources.push(resolvedSourcePath);
  19730. return null;
  19731. });
  19732. })()
  19733. );
  19734. }
  19735. }
  19736. await Promise.all(sourcesContentPromises);
  19737. map.sourcesContent = sourcesContent;
  19738. if (missingSources.length) {
  19739. logger.warnOnce(`Sourcemap for "${file}" points to missing source files`);
  19740. debug$g?.(`Missing sources:
  19741. ` + missingSources.join(`
  19742. `));
  19743. }
  19744. }
  19745. function genSourceMapUrl(map) {
  19746. if (typeof map !== "string") {
  19747. map = JSON.stringify(map);
  19748. }
  19749. return `data:application/json;base64,${Buffer.from(map).toString("base64")}`;
  19750. }
  19751. function getCodeWithSourcemap(type, code, map) {
  19752. if (debug$g) {
  19753. code += `
  19754. /*${JSON.stringify(map, null, 2).replace(/\*\//g, "*\\/")}*/
  19755. `;
  19756. }
  19757. if (type === "js") {
  19758. code += `
  19759. //# sourceMappingURL=${genSourceMapUrl(map)}`;
  19760. } else if (type === "css") {
  19761. code += `
  19762. /*# sourceMappingURL=${genSourceMapUrl(map)} */`;
  19763. }
  19764. return code;
  19765. }
  19766. function applySourcemapIgnoreList(map, sourcemapPath, sourcemapIgnoreList, logger) {
  19767. let { x_google_ignoreList } = map;
  19768. if (x_google_ignoreList === void 0) {
  19769. x_google_ignoreList = [];
  19770. }
  19771. for (let sourcesIndex = 0; sourcesIndex < map.sources.length; ++sourcesIndex) {
  19772. const sourcePath = map.sources[sourcesIndex];
  19773. if (!sourcePath) continue;
  19774. const ignoreList = sourcemapIgnoreList(
  19775. path$n.isAbsolute(sourcePath) ? sourcePath : path$n.resolve(path$n.dirname(sourcemapPath), sourcePath),
  19776. sourcemapPath
  19777. );
  19778. if (logger && typeof ignoreList !== "boolean") {
  19779. logger.warn("sourcemapIgnoreList function must return a boolean.");
  19780. }
  19781. if (ignoreList && !x_google_ignoreList.includes(sourcesIndex)) {
  19782. x_google_ignoreList.push(sourcesIndex);
  19783. }
  19784. }
  19785. if (x_google_ignoreList.length > 0) {
  19786. if (!map.x_google_ignoreList) map.x_google_ignoreList = x_google_ignoreList;
  19787. }
  19788. }
  19789. async function extractSourcemapFromFile(code, filePath) {
  19790. const map = (convertSourceMap.fromSource(code) || await convertSourceMap.fromMapFileSource(
  19791. code,
  19792. createConvertSourceMapReadMap(filePath)
  19793. ))?.toObject();
  19794. if (map) {
  19795. return {
  19796. code: code.replace(convertSourceMap.mapFileCommentRegex, blankReplacer),
  19797. map
  19798. };
  19799. }
  19800. }
  19801. function createConvertSourceMapReadMap(originalFileName) {
  19802. return (filename) => {
  19803. return fsp.readFile(
  19804. path$n.resolve(path$n.dirname(originalFileName), filename),
  19805. "utf-8"
  19806. );
  19807. };
  19808. }
  19809. var tasks = {};
  19810. var utils$g = {};
  19811. var array$1 = {};
  19812. Object.defineProperty(array$1, "__esModule", { value: true });
  19813. array$1.splitWhen = array$1.flatten = void 0;
  19814. function flatten$1(items) {
  19815. return items.reduce((collection, item) => [].concat(collection, item), []);
  19816. }
  19817. array$1.flatten = flatten$1;
  19818. function splitWhen(items, predicate) {
  19819. const result = [[]];
  19820. let groupIndex = 0;
  19821. for (const item of items) {
  19822. if (predicate(item)) {
  19823. groupIndex++;
  19824. result[groupIndex] = [];
  19825. }
  19826. else {
  19827. result[groupIndex].push(item);
  19828. }
  19829. }
  19830. return result;
  19831. }
  19832. array$1.splitWhen = splitWhen;
  19833. var errno$1 = {};
  19834. Object.defineProperty(errno$1, "__esModule", { value: true });
  19835. errno$1.isEnoentCodeError = void 0;
  19836. function isEnoentCodeError(error) {
  19837. return error.code === 'ENOENT';
  19838. }
  19839. errno$1.isEnoentCodeError = isEnoentCodeError;
  19840. var fs$i = {};
  19841. Object.defineProperty(fs$i, "__esModule", { value: true });
  19842. fs$i.createDirentFromStats = void 0;
  19843. let DirentFromStats$1 = class DirentFromStats {
  19844. constructor(name, stats) {
  19845. this.name = name;
  19846. this.isBlockDevice = stats.isBlockDevice.bind(stats);
  19847. this.isCharacterDevice = stats.isCharacterDevice.bind(stats);
  19848. this.isDirectory = stats.isDirectory.bind(stats);
  19849. this.isFIFO = stats.isFIFO.bind(stats);
  19850. this.isFile = stats.isFile.bind(stats);
  19851. this.isSocket = stats.isSocket.bind(stats);
  19852. this.isSymbolicLink = stats.isSymbolicLink.bind(stats);
  19853. }
  19854. };
  19855. function createDirentFromStats$1(name, stats) {
  19856. return new DirentFromStats$1(name, stats);
  19857. }
  19858. fs$i.createDirentFromStats = createDirentFromStats$1;
  19859. var path$i = {};
  19860. Object.defineProperty(path$i, "__esModule", { value: true });
  19861. path$i.convertPosixPathToPattern = path$i.convertWindowsPathToPattern = path$i.convertPathToPattern = path$i.escapePosixPath = path$i.escapeWindowsPath = path$i.escape = path$i.removeLeadingDotSegment = path$i.makeAbsolute = path$i.unixify = void 0;
  19862. const os$4 = require$$2;
  19863. const path$h = require$$0$4;
  19864. const IS_WINDOWS_PLATFORM = os$4.platform() === 'win32';
  19865. const LEADING_DOT_SEGMENT_CHARACTERS_COUNT = 2; // ./ or .\\
  19866. /**
  19867. * All non-escaped special characters.
  19868. * Posix: ()*?[]{|}, !+@ before (, ! at the beginning, \\ before non-special characters.
  19869. * Windows: (){}[], !+@ before (, ! at the beginning.
  19870. */
  19871. const POSIX_UNESCAPED_GLOB_SYMBOLS_RE = /(\\?)([()*?[\]{|}]|^!|[!+@](?=\()|\\(?![!()*+?@[\]{|}]))/g;
  19872. const WINDOWS_UNESCAPED_GLOB_SYMBOLS_RE = /(\\?)([()[\]{}]|^!|[!+@](?=\())/g;
  19873. /**
  19874. * The device path (\\.\ or \\?\).
  19875. * https://learn.microsoft.com/en-us/dotnet/standard/io/file-path-formats#dos-device-paths
  19876. */
  19877. const DOS_DEVICE_PATH_RE = /^\\\\([.?])/;
  19878. /**
  19879. * All backslashes except those escaping special characters.
  19880. * Windows: !()+@{}
  19881. * https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file#naming-conventions
  19882. */
  19883. const WINDOWS_BACKSLASHES_RE = /\\(?![!()+@[\]{}])/g;
  19884. /**
  19885. * Designed to work only with simple paths: `dir\\file`.
  19886. */
  19887. function unixify(filepath) {
  19888. return filepath.replace(/\\/g, '/');
  19889. }
  19890. path$i.unixify = unixify;
  19891. function makeAbsolute(cwd, filepath) {
  19892. return path$h.resolve(cwd, filepath);
  19893. }
  19894. path$i.makeAbsolute = makeAbsolute;
  19895. function removeLeadingDotSegment(entry) {
  19896. // We do not use `startsWith` because this is 10x slower than current implementation for some cases.
  19897. // eslint-disable-next-line @typescript-eslint/prefer-string-starts-ends-with
  19898. if (entry.charAt(0) === '.') {
  19899. const secondCharactery = entry.charAt(1);
  19900. if (secondCharactery === '/' || secondCharactery === '\\') {
  19901. return entry.slice(LEADING_DOT_SEGMENT_CHARACTERS_COUNT);
  19902. }
  19903. }
  19904. return entry;
  19905. }
  19906. path$i.removeLeadingDotSegment = removeLeadingDotSegment;
  19907. path$i.escape = IS_WINDOWS_PLATFORM ? escapeWindowsPath : escapePosixPath;
  19908. function escapeWindowsPath(pattern) {
  19909. return pattern.replace(WINDOWS_UNESCAPED_GLOB_SYMBOLS_RE, '\\$2');
  19910. }
  19911. path$i.escapeWindowsPath = escapeWindowsPath;
  19912. function escapePosixPath(pattern) {
  19913. return pattern.replace(POSIX_UNESCAPED_GLOB_SYMBOLS_RE, '\\$2');
  19914. }
  19915. path$i.escapePosixPath = escapePosixPath;
  19916. path$i.convertPathToPattern = IS_WINDOWS_PLATFORM ? convertWindowsPathToPattern : convertPosixPathToPattern;
  19917. function convertWindowsPathToPattern(filepath) {
  19918. return escapeWindowsPath(filepath)
  19919. .replace(DOS_DEVICE_PATH_RE, '//$1')
  19920. .replace(WINDOWS_BACKSLASHES_RE, '/');
  19921. }
  19922. path$i.convertWindowsPathToPattern = convertWindowsPathToPattern;
  19923. function convertPosixPathToPattern(filepath) {
  19924. return escapePosixPath(filepath);
  19925. }
  19926. path$i.convertPosixPathToPattern = convertPosixPathToPattern;
  19927. var pattern$1 = {};
  19928. /*!
  19929. * is-extglob <https://github.com/jonschlinkert/is-extglob>
  19930. *
  19931. * Copyright (c) 2014-2016, Jon Schlinkert.
  19932. * Licensed under the MIT License.
  19933. */
  19934. var isExtglob$1 = function isExtglob(str) {
  19935. if (typeof str !== 'string' || str === '') {
  19936. return false;
  19937. }
  19938. var match;
  19939. while ((match = /(\\).|([@?!+*]\(.*\))/g.exec(str))) {
  19940. if (match[2]) return true;
  19941. str = str.slice(match.index + match[0].length);
  19942. }
  19943. return false;
  19944. };
  19945. /*!
  19946. * is-glob <https://github.com/jonschlinkert/is-glob>
  19947. *
  19948. * Copyright (c) 2014-2017, Jon Schlinkert.
  19949. * Released under the MIT License.
  19950. */
  19951. var isExtglob = isExtglob$1;
  19952. var chars = { '{': '}', '(': ')', '[': ']'};
  19953. var strictCheck = function(str) {
  19954. if (str[0] === '!') {
  19955. return true;
  19956. }
  19957. var index = 0;
  19958. var pipeIndex = -2;
  19959. var closeSquareIndex = -2;
  19960. var closeCurlyIndex = -2;
  19961. var closeParenIndex = -2;
  19962. var backSlashIndex = -2;
  19963. while (index < str.length) {
  19964. if (str[index] === '*') {
  19965. return true;
  19966. }
  19967. if (str[index + 1] === '?' && /[\].+)]/.test(str[index])) {
  19968. return true;
  19969. }
  19970. if (closeSquareIndex !== -1 && str[index] === '[' && str[index + 1] !== ']') {
  19971. if (closeSquareIndex < index) {
  19972. closeSquareIndex = str.indexOf(']', index);
  19973. }
  19974. if (closeSquareIndex > index) {
  19975. if (backSlashIndex === -1 || backSlashIndex > closeSquareIndex) {
  19976. return true;
  19977. }
  19978. backSlashIndex = str.indexOf('\\', index);
  19979. if (backSlashIndex === -1 || backSlashIndex > closeSquareIndex) {
  19980. return true;
  19981. }
  19982. }
  19983. }
  19984. if (closeCurlyIndex !== -1 && str[index] === '{' && str[index + 1] !== '}') {
  19985. closeCurlyIndex = str.indexOf('}', index);
  19986. if (closeCurlyIndex > index) {
  19987. backSlashIndex = str.indexOf('\\', index);
  19988. if (backSlashIndex === -1 || backSlashIndex > closeCurlyIndex) {
  19989. return true;
  19990. }
  19991. }
  19992. }
  19993. if (closeParenIndex !== -1 && str[index] === '(' && str[index + 1] === '?' && /[:!=]/.test(str[index + 2]) && str[index + 3] !== ')') {
  19994. closeParenIndex = str.indexOf(')', index);
  19995. if (closeParenIndex > index) {
  19996. backSlashIndex = str.indexOf('\\', index);
  19997. if (backSlashIndex === -1 || backSlashIndex > closeParenIndex) {
  19998. return true;
  19999. }
  20000. }
  20001. }
  20002. if (pipeIndex !== -1 && str[index] === '(' && str[index + 1] !== '|') {
  20003. if (pipeIndex < index) {
  20004. pipeIndex = str.indexOf('|', index);
  20005. }
  20006. if (pipeIndex !== -1 && str[pipeIndex + 1] !== ')') {
  20007. closeParenIndex = str.indexOf(')', pipeIndex);
  20008. if (closeParenIndex > pipeIndex) {
  20009. backSlashIndex = str.indexOf('\\', pipeIndex);
  20010. if (backSlashIndex === -1 || backSlashIndex > closeParenIndex) {
  20011. return true;
  20012. }
  20013. }
  20014. }
  20015. }
  20016. if (str[index] === '\\') {
  20017. var open = str[index + 1];
  20018. index += 2;
  20019. var close = chars[open];
  20020. if (close) {
  20021. var n = str.indexOf(close, index);
  20022. if (n !== -1) {
  20023. index = n + 1;
  20024. }
  20025. }
  20026. if (str[index] === '!') {
  20027. return true;
  20028. }
  20029. } else {
  20030. index++;
  20031. }
  20032. }
  20033. return false;
  20034. };
  20035. var relaxedCheck = function(str) {
  20036. if (str[0] === '!') {
  20037. return true;
  20038. }
  20039. var index = 0;
  20040. while (index < str.length) {
  20041. if (/[*?{}()[\]]/.test(str[index])) {
  20042. return true;
  20043. }
  20044. if (str[index] === '\\') {
  20045. var open = str[index + 1];
  20046. index += 2;
  20047. var close = chars[open];
  20048. if (close) {
  20049. var n = str.indexOf(close, index);
  20050. if (n !== -1) {
  20051. index = n + 1;
  20052. }
  20053. }
  20054. if (str[index] === '!') {
  20055. return true;
  20056. }
  20057. } else {
  20058. index++;
  20059. }
  20060. }
  20061. return false;
  20062. };
  20063. var isGlob$2 = function isGlob(str, options) {
  20064. if (typeof str !== 'string' || str === '') {
  20065. return false;
  20066. }
  20067. if (isExtglob(str)) {
  20068. return true;
  20069. }
  20070. var check = strictCheck;
  20071. // optionally relax check
  20072. if (options && options.strict === false) {
  20073. check = relaxedCheck;
  20074. }
  20075. return check(str);
  20076. };
  20077. var isGlob$1 = isGlob$2;
  20078. var pathPosixDirname = require$$0$4.posix.dirname;
  20079. var isWin32 = require$$2.platform() === 'win32';
  20080. var slash = '/';
  20081. var backslash = /\\/g;
  20082. var enclosure = /[\{\[].*[\}\]]$/;
  20083. var globby = /(^|[^\\])([\{\[]|\([^\)]+$)/;
  20084. var escaped = /\\([\!\*\?\|\[\]\(\)\{\}])/g;
  20085. /**
  20086. * @param {string} str
  20087. * @param {Object} opts
  20088. * @param {boolean} [opts.flipBackslashes=true]
  20089. * @returns {string}
  20090. */
  20091. var globParent$2 = function globParent(str, opts) {
  20092. var options = Object.assign({ flipBackslashes: true }, opts);
  20093. // flip windows path separators
  20094. if (options.flipBackslashes && isWin32 && str.indexOf(slash) < 0) {
  20095. str = str.replace(backslash, slash);
  20096. }
  20097. // special case for strings ending in enclosure containing path separator
  20098. if (enclosure.test(str)) {
  20099. str += slash;
  20100. }
  20101. // preserves full path in case of trailing path separator
  20102. str += 'a';
  20103. // remove path parts that are globby
  20104. do {
  20105. str = pathPosixDirname(str);
  20106. } while (isGlob$1(str) || globby.test(str));
  20107. // remove escape chars and return result
  20108. return str.replace(escaped, '$1');
  20109. };
  20110. var utils$f = {};
  20111. (function (exports) {
  20112. exports.isInteger = num => {
  20113. if (typeof num === 'number') {
  20114. return Number.isInteger(num);
  20115. }
  20116. if (typeof num === 'string' && num.trim() !== '') {
  20117. return Number.isInteger(Number(num));
  20118. }
  20119. return false;
  20120. };
  20121. /**
  20122. * Find a node of the given type
  20123. */
  20124. exports.find = (node, type) => node.nodes.find(node => node.type === type);
  20125. /**
  20126. * Find a node of the given type
  20127. */
  20128. exports.exceedsLimit = (min, max, step = 1, limit) => {
  20129. if (limit === false) return false;
  20130. if (!exports.isInteger(min) || !exports.isInteger(max)) return false;
  20131. return ((Number(max) - Number(min)) / Number(step)) >= limit;
  20132. };
  20133. /**
  20134. * Escape the given node with '\\' before node.value
  20135. */
  20136. exports.escapeNode = (block, n = 0, type) => {
  20137. const node = block.nodes[n];
  20138. if (!node) return;
  20139. if ((type && node.type === type) || node.type === 'open' || node.type === 'close') {
  20140. if (node.escaped !== true) {
  20141. node.value = '\\' + node.value;
  20142. node.escaped = true;
  20143. }
  20144. }
  20145. };
  20146. /**
  20147. * Returns true if the given brace node should be enclosed in literal braces
  20148. */
  20149. exports.encloseBrace = node => {
  20150. if (node.type !== 'brace') return false;
  20151. if ((node.commas >> 0 + node.ranges >> 0) === 0) {
  20152. node.invalid = true;
  20153. return true;
  20154. }
  20155. return false;
  20156. };
  20157. /**
  20158. * Returns true if a brace node is invalid.
  20159. */
  20160. exports.isInvalidBrace = block => {
  20161. if (block.type !== 'brace') return false;
  20162. if (block.invalid === true || block.dollar) return true;
  20163. if ((block.commas >> 0 + block.ranges >> 0) === 0) {
  20164. block.invalid = true;
  20165. return true;
  20166. }
  20167. if (block.open !== true || block.close !== true) {
  20168. block.invalid = true;
  20169. return true;
  20170. }
  20171. return false;
  20172. };
  20173. /**
  20174. * Returns true if a node is an open or close node
  20175. */
  20176. exports.isOpenOrClose = node => {
  20177. if (node.type === 'open' || node.type === 'close') {
  20178. return true;
  20179. }
  20180. return node.open === true || node.close === true;
  20181. };
  20182. /**
  20183. * Reduce an array of text nodes.
  20184. */
  20185. exports.reduce = nodes => nodes.reduce((acc, node) => {
  20186. if (node.type === 'text') acc.push(node.value);
  20187. if (node.type === 'range') node.type = 'text';
  20188. return acc;
  20189. }, []);
  20190. /**
  20191. * Flatten an array
  20192. */
  20193. exports.flatten = (...args) => {
  20194. const result = [];
  20195. const flat = arr => {
  20196. for (let i = 0; i < arr.length; i++) {
  20197. const ele = arr[i];
  20198. if (Array.isArray(ele)) {
  20199. flat(ele);
  20200. continue;
  20201. }
  20202. if (ele !== undefined) {
  20203. result.push(ele);
  20204. }
  20205. }
  20206. return result;
  20207. };
  20208. flat(args);
  20209. return result;
  20210. };
  20211. } (utils$f));
  20212. const utils$e = utils$f;
  20213. var stringify$7 = (ast, options = {}) => {
  20214. const stringify = (node, parent = {}) => {
  20215. const invalidBlock = options.escapeInvalid && utils$e.isInvalidBrace(parent);
  20216. const invalidNode = node.invalid === true && options.escapeInvalid === true;
  20217. let output = '';
  20218. if (node.value) {
  20219. if ((invalidBlock || invalidNode) && utils$e.isOpenOrClose(node)) {
  20220. return '\\' + node.value;
  20221. }
  20222. return node.value;
  20223. }
  20224. if (node.value) {
  20225. return node.value;
  20226. }
  20227. if (node.nodes) {
  20228. for (const child of node.nodes) {
  20229. output += stringify(child);
  20230. }
  20231. }
  20232. return output;
  20233. };
  20234. return stringify(ast);
  20235. };
  20236. /*!
  20237. * is-number <https://github.com/jonschlinkert/is-number>
  20238. *
  20239. * Copyright (c) 2014-present, Jon Schlinkert.
  20240. * Released under the MIT License.
  20241. */
  20242. var isNumber$2 = function(num) {
  20243. if (typeof num === 'number') {
  20244. return num - num === 0;
  20245. }
  20246. if (typeof num === 'string' && num.trim() !== '') {
  20247. return Number.isFinite ? Number.isFinite(+num) : isFinite(+num);
  20248. }
  20249. return false;
  20250. };
  20251. /*!
  20252. * to-regex-range <https://github.com/micromatch/to-regex-range>
  20253. *
  20254. * Copyright (c) 2015-present, Jon Schlinkert.
  20255. * Released under the MIT License.
  20256. */
  20257. const isNumber$1 = isNumber$2;
  20258. const toRegexRange$1 = (min, max, options) => {
  20259. if (isNumber$1(min) === false) {
  20260. throw new TypeError('toRegexRange: expected the first argument to be a number');
  20261. }
  20262. if (max === void 0 || min === max) {
  20263. return String(min);
  20264. }
  20265. if (isNumber$1(max) === false) {
  20266. throw new TypeError('toRegexRange: expected the second argument to be a number.');
  20267. }
  20268. let opts = { relaxZeros: true, ...options };
  20269. if (typeof opts.strictZeros === 'boolean') {
  20270. opts.relaxZeros = opts.strictZeros === false;
  20271. }
  20272. let relax = String(opts.relaxZeros);
  20273. let shorthand = String(opts.shorthand);
  20274. let capture = String(opts.capture);
  20275. let wrap = String(opts.wrap);
  20276. let cacheKey = min + ':' + max + '=' + relax + shorthand + capture + wrap;
  20277. if (toRegexRange$1.cache.hasOwnProperty(cacheKey)) {
  20278. return toRegexRange$1.cache[cacheKey].result;
  20279. }
  20280. let a = Math.min(min, max);
  20281. let b = Math.max(min, max);
  20282. if (Math.abs(a - b) === 1) {
  20283. let result = min + '|' + max;
  20284. if (opts.capture) {
  20285. return `(${result})`;
  20286. }
  20287. if (opts.wrap === false) {
  20288. return result;
  20289. }
  20290. return `(?:${result})`;
  20291. }
  20292. let isPadded = hasPadding(min) || hasPadding(max);
  20293. let state = { min, max, a, b };
  20294. let positives = [];
  20295. let negatives = [];
  20296. if (isPadded) {
  20297. state.isPadded = isPadded;
  20298. state.maxLen = String(state.max).length;
  20299. }
  20300. if (a < 0) {
  20301. let newMin = b < 0 ? Math.abs(b) : 1;
  20302. negatives = splitToPatterns(newMin, Math.abs(a), state, opts);
  20303. a = state.a = 0;
  20304. }
  20305. if (b >= 0) {
  20306. positives = splitToPatterns(a, b, state, opts);
  20307. }
  20308. state.negatives = negatives;
  20309. state.positives = positives;
  20310. state.result = collatePatterns(negatives, positives);
  20311. if (opts.capture === true) {
  20312. state.result = `(${state.result})`;
  20313. } else if (opts.wrap !== false && (positives.length + negatives.length) > 1) {
  20314. state.result = `(?:${state.result})`;
  20315. }
  20316. toRegexRange$1.cache[cacheKey] = state;
  20317. return state.result;
  20318. };
  20319. function collatePatterns(neg, pos, options) {
  20320. let onlyNegative = filterPatterns(neg, pos, '-', false) || [];
  20321. let onlyPositive = filterPatterns(pos, neg, '', false) || [];
  20322. let intersected = filterPatterns(neg, pos, '-?', true) || [];
  20323. let subpatterns = onlyNegative.concat(intersected).concat(onlyPositive);
  20324. return subpatterns.join('|');
  20325. }
  20326. function splitToRanges(min, max) {
  20327. let nines = 1;
  20328. let zeros = 1;
  20329. let stop = countNines(min, nines);
  20330. let stops = new Set([max]);
  20331. while (min <= stop && stop <= max) {
  20332. stops.add(stop);
  20333. nines += 1;
  20334. stop = countNines(min, nines);
  20335. }
  20336. stop = countZeros(max + 1, zeros) - 1;
  20337. while (min < stop && stop <= max) {
  20338. stops.add(stop);
  20339. zeros += 1;
  20340. stop = countZeros(max + 1, zeros) - 1;
  20341. }
  20342. stops = [...stops];
  20343. stops.sort(compare);
  20344. return stops;
  20345. }
  20346. /**
  20347. * Convert a range to a regex pattern
  20348. * @param {Number} `start`
  20349. * @param {Number} `stop`
  20350. * @return {String}
  20351. */
  20352. function rangeToPattern(start, stop, options) {
  20353. if (start === stop) {
  20354. return { pattern: start, count: [], digits: 0 };
  20355. }
  20356. let zipped = zip(start, stop);
  20357. let digits = zipped.length;
  20358. let pattern = '';
  20359. let count = 0;
  20360. for (let i = 0; i < digits; i++) {
  20361. let [startDigit, stopDigit] = zipped[i];
  20362. if (startDigit === stopDigit) {
  20363. pattern += startDigit;
  20364. } else if (startDigit !== '0' || stopDigit !== '9') {
  20365. pattern += toCharacterClass(startDigit, stopDigit);
  20366. } else {
  20367. count++;
  20368. }
  20369. }
  20370. if (count) {
  20371. pattern += options.shorthand === true ? '\\d' : '[0-9]';
  20372. }
  20373. return { pattern, count: [count], digits };
  20374. }
  20375. function splitToPatterns(min, max, tok, options) {
  20376. let ranges = splitToRanges(min, max);
  20377. let tokens = [];
  20378. let start = min;
  20379. let prev;
  20380. for (let i = 0; i < ranges.length; i++) {
  20381. let max = ranges[i];
  20382. let obj = rangeToPattern(String(start), String(max), options);
  20383. let zeros = '';
  20384. if (!tok.isPadded && prev && prev.pattern === obj.pattern) {
  20385. if (prev.count.length > 1) {
  20386. prev.count.pop();
  20387. }
  20388. prev.count.push(obj.count[0]);
  20389. prev.string = prev.pattern + toQuantifier(prev.count);
  20390. start = max + 1;
  20391. continue;
  20392. }
  20393. if (tok.isPadded) {
  20394. zeros = padZeros(max, tok, options);
  20395. }
  20396. obj.string = zeros + obj.pattern + toQuantifier(obj.count);
  20397. tokens.push(obj);
  20398. start = max + 1;
  20399. prev = obj;
  20400. }
  20401. return tokens;
  20402. }
  20403. function filterPatterns(arr, comparison, prefix, intersection, options) {
  20404. let result = [];
  20405. for (let ele of arr) {
  20406. let { string } = ele;
  20407. // only push if _both_ are negative...
  20408. if (!intersection && !contains(comparison, 'string', string)) {
  20409. result.push(prefix + string);
  20410. }
  20411. // or _both_ are positive
  20412. if (intersection && contains(comparison, 'string', string)) {
  20413. result.push(prefix + string);
  20414. }
  20415. }
  20416. return result;
  20417. }
  20418. /**
  20419. * Zip strings
  20420. */
  20421. function zip(a, b) {
  20422. let arr = [];
  20423. for (let i = 0; i < a.length; i++) arr.push([a[i], b[i]]);
  20424. return arr;
  20425. }
  20426. function compare(a, b) {
  20427. return a > b ? 1 : b > a ? -1 : 0;
  20428. }
  20429. function contains(arr, key, val) {
  20430. return arr.some(ele => ele[key] === val);
  20431. }
  20432. function countNines(min, len) {
  20433. return Number(String(min).slice(0, -len) + '9'.repeat(len));
  20434. }
  20435. function countZeros(integer, zeros) {
  20436. return integer - (integer % Math.pow(10, zeros));
  20437. }
  20438. function toQuantifier(digits) {
  20439. let [start = 0, stop = ''] = digits;
  20440. if (stop || start > 1) {
  20441. return `{${start + (stop ? ',' + stop : '')}}`;
  20442. }
  20443. return '';
  20444. }
  20445. function toCharacterClass(a, b, options) {
  20446. return `[${a}${(b - a === 1) ? '' : '-'}${b}]`;
  20447. }
  20448. function hasPadding(str) {
  20449. return /^-?(0+)\d/.test(str);
  20450. }
  20451. function padZeros(value, tok, options) {
  20452. if (!tok.isPadded) {
  20453. return value;
  20454. }
  20455. let diff = Math.abs(tok.maxLen - String(value).length);
  20456. let relax = options.relaxZeros !== false;
  20457. switch (diff) {
  20458. case 0:
  20459. return '';
  20460. case 1:
  20461. return relax ? '0?' : '0';
  20462. case 2:
  20463. return relax ? '0{0,2}' : '00';
  20464. default: {
  20465. return relax ? `0{0,${diff}}` : `0{${diff}}`;
  20466. }
  20467. }
  20468. }
  20469. /**
  20470. * Cache
  20471. */
  20472. toRegexRange$1.cache = {};
  20473. toRegexRange$1.clearCache = () => (toRegexRange$1.cache = {});
  20474. /**
  20475. * Expose `toRegexRange`
  20476. */
  20477. var toRegexRange_1 = toRegexRange$1;
  20478. /*!
  20479. * fill-range <https://github.com/jonschlinkert/fill-range>
  20480. *
  20481. * Copyright (c) 2014-present, Jon Schlinkert.
  20482. * Licensed under the MIT License.
  20483. */
  20484. const util$1 = require$$0$5;
  20485. const toRegexRange = toRegexRange_1;
  20486. const isObject = val => val !== null && typeof val === 'object' && !Array.isArray(val);
  20487. const transform = toNumber => {
  20488. return value => toNumber === true ? Number(value) : String(value);
  20489. };
  20490. const isValidValue = value => {
  20491. return typeof value === 'number' || (typeof value === 'string' && value !== '');
  20492. };
  20493. const isNumber = num => Number.isInteger(+num);
  20494. const zeros = input => {
  20495. let value = `${input}`;
  20496. let index = -1;
  20497. if (value[0] === '-') value = value.slice(1);
  20498. if (value === '0') return false;
  20499. while (value[++index] === '0');
  20500. return index > 0;
  20501. };
  20502. const stringify$6 = (start, end, options) => {
  20503. if (typeof start === 'string' || typeof end === 'string') {
  20504. return true;
  20505. }
  20506. return options.stringify === true;
  20507. };
  20508. const pad = (input, maxLength, toNumber) => {
  20509. if (maxLength > 0) {
  20510. let dash = input[0] === '-' ? '-' : '';
  20511. if (dash) input = input.slice(1);
  20512. input = (dash + input.padStart(dash ? maxLength - 1 : maxLength, '0'));
  20513. }
  20514. if (toNumber === false) {
  20515. return String(input);
  20516. }
  20517. return input;
  20518. };
  20519. const toMaxLen = (input, maxLength) => {
  20520. let negative = input[0] === '-' ? '-' : '';
  20521. if (negative) {
  20522. input = input.slice(1);
  20523. maxLength--;
  20524. }
  20525. while (input.length < maxLength) input = '0' + input;
  20526. return negative ? ('-' + input) : input;
  20527. };
  20528. const toSequence = (parts, options, maxLen) => {
  20529. parts.negatives.sort((a, b) => a < b ? -1 : a > b ? 1 : 0);
  20530. parts.positives.sort((a, b) => a < b ? -1 : a > b ? 1 : 0);
  20531. let prefix = options.capture ? '' : '?:';
  20532. let positives = '';
  20533. let negatives = '';
  20534. let result;
  20535. if (parts.positives.length) {
  20536. positives = parts.positives.map(v => toMaxLen(String(v), maxLen)).join('|');
  20537. }
  20538. if (parts.negatives.length) {
  20539. negatives = `-(${prefix}${parts.negatives.map(v => toMaxLen(String(v), maxLen)).join('|')})`;
  20540. }
  20541. if (positives && negatives) {
  20542. result = `${positives}|${negatives}`;
  20543. } else {
  20544. result = positives || negatives;
  20545. }
  20546. if (options.wrap) {
  20547. return `(${prefix}${result})`;
  20548. }
  20549. return result;
  20550. };
  20551. const toRange = (a, b, isNumbers, options) => {
  20552. if (isNumbers) {
  20553. return toRegexRange(a, b, { wrap: false, ...options });
  20554. }
  20555. let start = String.fromCharCode(a);
  20556. if (a === b) return start;
  20557. let stop = String.fromCharCode(b);
  20558. return `[${start}-${stop}]`;
  20559. };
  20560. const toRegex = (start, end, options) => {
  20561. if (Array.isArray(start)) {
  20562. let wrap = options.wrap === true;
  20563. let prefix = options.capture ? '' : '?:';
  20564. return wrap ? `(${prefix}${start.join('|')})` : start.join('|');
  20565. }
  20566. return toRegexRange(start, end, options);
  20567. };
  20568. const rangeError = (...args) => {
  20569. return new RangeError('Invalid range arguments: ' + util$1.inspect(...args));
  20570. };
  20571. const invalidRange = (start, end, options) => {
  20572. if (options.strictRanges === true) throw rangeError([start, end]);
  20573. return [];
  20574. };
  20575. const invalidStep = (step, options) => {
  20576. if (options.strictRanges === true) {
  20577. throw new TypeError(`Expected step "${step}" to be a number`);
  20578. }
  20579. return [];
  20580. };
  20581. const fillNumbers = (start, end, step = 1, options = {}) => {
  20582. let a = Number(start);
  20583. let b = Number(end);
  20584. if (!Number.isInteger(a) || !Number.isInteger(b)) {
  20585. if (options.strictRanges === true) throw rangeError([start, end]);
  20586. return [];
  20587. }
  20588. // fix negative zero
  20589. if (a === 0) a = 0;
  20590. if (b === 0) b = 0;
  20591. let descending = a > b;
  20592. let startString = String(start);
  20593. let endString = String(end);
  20594. let stepString = String(step);
  20595. step = Math.max(Math.abs(step), 1);
  20596. let padded = zeros(startString) || zeros(endString) || zeros(stepString);
  20597. let maxLen = padded ? Math.max(startString.length, endString.length, stepString.length) : 0;
  20598. let toNumber = padded === false && stringify$6(start, end, options) === false;
  20599. let format = options.transform || transform(toNumber);
  20600. if (options.toRegex && step === 1) {
  20601. return toRange(toMaxLen(start, maxLen), toMaxLen(end, maxLen), true, options);
  20602. }
  20603. let parts = { negatives: [], positives: [] };
  20604. let push = num => parts[num < 0 ? 'negatives' : 'positives'].push(Math.abs(num));
  20605. let range = [];
  20606. let index = 0;
  20607. while (descending ? a >= b : a <= b) {
  20608. if (options.toRegex === true && step > 1) {
  20609. push(a);
  20610. } else {
  20611. range.push(pad(format(a, index), maxLen, toNumber));
  20612. }
  20613. a = descending ? a - step : a + step;
  20614. index++;
  20615. }
  20616. if (options.toRegex === true) {
  20617. return step > 1
  20618. ? toSequence(parts, options, maxLen)
  20619. : toRegex(range, null, { wrap: false, ...options });
  20620. }
  20621. return range;
  20622. };
  20623. const fillLetters = (start, end, step = 1, options = {}) => {
  20624. if ((!isNumber(start) && start.length > 1) || (!isNumber(end) && end.length > 1)) {
  20625. return invalidRange(start, end, options);
  20626. }
  20627. let format = options.transform || (val => String.fromCharCode(val));
  20628. let a = `${start}`.charCodeAt(0);
  20629. let b = `${end}`.charCodeAt(0);
  20630. let descending = a > b;
  20631. let min = Math.min(a, b);
  20632. let max = Math.max(a, b);
  20633. if (options.toRegex && step === 1) {
  20634. return toRange(min, max, false, options);
  20635. }
  20636. let range = [];
  20637. let index = 0;
  20638. while (descending ? a >= b : a <= b) {
  20639. range.push(format(a, index));
  20640. a = descending ? a - step : a + step;
  20641. index++;
  20642. }
  20643. if (options.toRegex === true) {
  20644. return toRegex(range, null, { wrap: false, options });
  20645. }
  20646. return range;
  20647. };
  20648. const fill$2 = (start, end, step, options = {}) => {
  20649. if (end == null && isValidValue(start)) {
  20650. return [start];
  20651. }
  20652. if (!isValidValue(start) || !isValidValue(end)) {
  20653. return invalidRange(start, end, options);
  20654. }
  20655. if (typeof step === 'function') {
  20656. return fill$2(start, end, 1, { transform: step });
  20657. }
  20658. if (isObject(step)) {
  20659. return fill$2(start, end, 0, step);
  20660. }
  20661. let opts = { ...options };
  20662. if (opts.capture === true) opts.wrap = true;
  20663. step = step || opts.step || 1;
  20664. if (!isNumber(step)) {
  20665. if (step != null && !isObject(step)) return invalidStep(step, opts);
  20666. return fill$2(start, end, 1, step);
  20667. }
  20668. if (isNumber(start) && isNumber(end)) {
  20669. return fillNumbers(start, end, step, opts);
  20670. }
  20671. return fillLetters(start, end, Math.max(Math.abs(step), 1), opts);
  20672. };
  20673. var fillRange = fill$2;
  20674. const fill$1 = fillRange;
  20675. const utils$d = utils$f;
  20676. const compile$1 = (ast, options = {}) => {
  20677. const walk = (node, parent = {}) => {
  20678. const invalidBlock = utils$d.isInvalidBrace(parent);
  20679. const invalidNode = node.invalid === true && options.escapeInvalid === true;
  20680. const invalid = invalidBlock === true || invalidNode === true;
  20681. const prefix = options.escapeInvalid === true ? '\\' : '';
  20682. let output = '';
  20683. if (node.isOpen === true) {
  20684. return prefix + node.value;
  20685. }
  20686. if (node.isClose === true) {
  20687. console.log('node.isClose', prefix, node.value);
  20688. return prefix + node.value;
  20689. }
  20690. if (node.type === 'open') {
  20691. return invalid ? prefix + node.value : '(';
  20692. }
  20693. if (node.type === 'close') {
  20694. return invalid ? prefix + node.value : ')';
  20695. }
  20696. if (node.type === 'comma') {
  20697. return node.prev.type === 'comma' ? '' : invalid ? node.value : '|';
  20698. }
  20699. if (node.value) {
  20700. return node.value;
  20701. }
  20702. if (node.nodes && node.ranges > 0) {
  20703. const args = utils$d.reduce(node.nodes);
  20704. const range = fill$1(...args, { ...options, wrap: false, toRegex: true, strictZeros: true });
  20705. if (range.length !== 0) {
  20706. return args.length > 1 && range.length > 1 ? `(${range})` : range;
  20707. }
  20708. }
  20709. if (node.nodes) {
  20710. for (const child of node.nodes) {
  20711. output += walk(child, node);
  20712. }
  20713. }
  20714. return output;
  20715. };
  20716. return walk(ast);
  20717. };
  20718. var compile_1 = compile$1;
  20719. const fill = fillRange;
  20720. const stringify$5 = stringify$7;
  20721. const utils$c = utils$f;
  20722. const append$1 = (queue = '', stash = '', enclose = false) => {
  20723. const result = [];
  20724. queue = [].concat(queue);
  20725. stash = [].concat(stash);
  20726. if (!stash.length) return queue;
  20727. if (!queue.length) {
  20728. return enclose ? utils$c.flatten(stash).map(ele => `{${ele}}`) : stash;
  20729. }
  20730. for (const item of queue) {
  20731. if (Array.isArray(item)) {
  20732. for (const value of item) {
  20733. result.push(append$1(value, stash, enclose));
  20734. }
  20735. } else {
  20736. for (let ele of stash) {
  20737. if (enclose === true && typeof ele === 'string') ele = `{${ele}}`;
  20738. result.push(Array.isArray(ele) ? append$1(item, ele, enclose) : item + ele);
  20739. }
  20740. }
  20741. }
  20742. return utils$c.flatten(result);
  20743. };
  20744. const expand$2 = (ast, options = {}) => {
  20745. const rangeLimit = options.rangeLimit === undefined ? 1000 : options.rangeLimit;
  20746. const walk = (node, parent = {}) => {
  20747. node.queue = [];
  20748. let p = parent;
  20749. let q = parent.queue;
  20750. while (p.type !== 'brace' && p.type !== 'root' && p.parent) {
  20751. p = p.parent;
  20752. q = p.queue;
  20753. }
  20754. if (node.invalid || node.dollar) {
  20755. q.push(append$1(q.pop(), stringify$5(node, options)));
  20756. return;
  20757. }
  20758. if (node.type === 'brace' && node.invalid !== true && node.nodes.length === 2) {
  20759. q.push(append$1(q.pop(), ['{}']));
  20760. return;
  20761. }
  20762. if (node.nodes && node.ranges > 0) {
  20763. const args = utils$c.reduce(node.nodes);
  20764. if (utils$c.exceedsLimit(...args, options.step, rangeLimit)) {
  20765. throw new RangeError('expanded array length exceeds range limit. Use options.rangeLimit to increase or disable the limit.');
  20766. }
  20767. let range = fill(...args, options);
  20768. if (range.length === 0) {
  20769. range = stringify$5(node, options);
  20770. }
  20771. q.push(append$1(q.pop(), range));
  20772. node.nodes = [];
  20773. return;
  20774. }
  20775. const enclose = utils$c.encloseBrace(node);
  20776. let queue = node.queue;
  20777. let block = node;
  20778. while (block.type !== 'brace' && block.type !== 'root' && block.parent) {
  20779. block = block.parent;
  20780. queue = block.queue;
  20781. }
  20782. for (let i = 0; i < node.nodes.length; i++) {
  20783. const child = node.nodes[i];
  20784. if (child.type === 'comma' && node.type === 'brace') {
  20785. if (i === 1) queue.push('');
  20786. queue.push('');
  20787. continue;
  20788. }
  20789. if (child.type === 'close') {
  20790. q.push(append$1(q.pop(), queue, enclose));
  20791. continue;
  20792. }
  20793. if (child.value && child.type !== 'open') {
  20794. queue.push(append$1(queue.pop(), child.value));
  20795. continue;
  20796. }
  20797. if (child.nodes) {
  20798. walk(child, node);
  20799. }
  20800. }
  20801. return queue;
  20802. };
  20803. return utils$c.flatten(walk(ast));
  20804. };
  20805. var expand_1$1 = expand$2;
  20806. var constants$3 = {
  20807. MAX_LENGTH: 10000,
  20808. // Digits
  20809. CHAR_0: '0', /* 0 */
  20810. CHAR_9: '9', /* 9 */
  20811. // Alphabet chars.
  20812. CHAR_UPPERCASE_A: 'A', /* A */
  20813. CHAR_LOWERCASE_A: 'a', /* a */
  20814. CHAR_UPPERCASE_Z: 'Z', /* Z */
  20815. CHAR_LOWERCASE_Z: 'z', /* z */
  20816. CHAR_LEFT_PARENTHESES: '(', /* ( */
  20817. CHAR_RIGHT_PARENTHESES: ')', /* ) */
  20818. CHAR_ASTERISK: '*', /* * */
  20819. // Non-alphabetic chars.
  20820. CHAR_AMPERSAND: '&', /* & */
  20821. CHAR_AT: '@', /* @ */
  20822. CHAR_BACKSLASH: '\\', /* \ */
  20823. CHAR_BACKTICK: '`', /* ` */
  20824. CHAR_CARRIAGE_RETURN: '\r', /* \r */
  20825. CHAR_CIRCUMFLEX_ACCENT: '^', /* ^ */
  20826. CHAR_COLON: ':', /* : */
  20827. CHAR_COMMA: ',', /* , */
  20828. CHAR_DOLLAR: '$', /* . */
  20829. CHAR_DOT: '.', /* . */
  20830. CHAR_DOUBLE_QUOTE: '"', /* " */
  20831. CHAR_EQUAL: '=', /* = */
  20832. CHAR_EXCLAMATION_MARK: '!', /* ! */
  20833. CHAR_FORM_FEED: '\f', /* \f */
  20834. CHAR_FORWARD_SLASH: '/', /* / */
  20835. CHAR_HASH: '#', /* # */
  20836. CHAR_HYPHEN_MINUS: '-', /* - */
  20837. CHAR_LEFT_ANGLE_BRACKET: '<', /* < */
  20838. CHAR_LEFT_CURLY_BRACE: '{', /* { */
  20839. CHAR_LEFT_SQUARE_BRACKET: '[', /* [ */
  20840. CHAR_LINE_FEED: '\n', /* \n */
  20841. CHAR_NO_BREAK_SPACE: '\u00A0', /* \u00A0 */
  20842. CHAR_PERCENT: '%', /* % */
  20843. CHAR_PLUS: '+', /* + */
  20844. CHAR_QUESTION_MARK: '?', /* ? */
  20845. CHAR_RIGHT_ANGLE_BRACKET: '>', /* > */
  20846. CHAR_RIGHT_CURLY_BRACE: '}', /* } */
  20847. CHAR_RIGHT_SQUARE_BRACKET: ']', /* ] */
  20848. CHAR_SEMICOLON: ';', /* ; */
  20849. CHAR_SINGLE_QUOTE: '\'', /* ' */
  20850. CHAR_SPACE: ' ', /* */
  20851. CHAR_TAB: '\t', /* \t */
  20852. CHAR_UNDERSCORE: '_', /* _ */
  20853. CHAR_VERTICAL_LINE: '|', /* | */
  20854. CHAR_ZERO_WIDTH_NOBREAK_SPACE: '\uFEFF' /* \uFEFF */
  20855. };
  20856. const stringify$4 = stringify$7;
  20857. /**
  20858. * Constants
  20859. */
  20860. const {
  20861. MAX_LENGTH,
  20862. CHAR_BACKSLASH, /* \ */
  20863. CHAR_BACKTICK, /* ` */
  20864. CHAR_COMMA, /* , */
  20865. CHAR_DOT, /* . */
  20866. CHAR_LEFT_PARENTHESES, /* ( */
  20867. CHAR_RIGHT_PARENTHESES, /* ) */
  20868. CHAR_LEFT_CURLY_BRACE, /* { */
  20869. CHAR_RIGHT_CURLY_BRACE, /* } */
  20870. CHAR_LEFT_SQUARE_BRACKET, /* [ */
  20871. CHAR_RIGHT_SQUARE_BRACKET, /* ] */
  20872. CHAR_DOUBLE_QUOTE, /* " */
  20873. CHAR_SINGLE_QUOTE, /* ' */
  20874. CHAR_NO_BREAK_SPACE,
  20875. CHAR_ZERO_WIDTH_NOBREAK_SPACE
  20876. } = constants$3;
  20877. /**
  20878. * parse
  20879. */
  20880. const parse$c = (input, options = {}) => {
  20881. if (typeof input !== 'string') {
  20882. throw new TypeError('Expected a string');
  20883. }
  20884. const opts = options || {};
  20885. const max = typeof opts.maxLength === 'number' ? Math.min(MAX_LENGTH, opts.maxLength) : MAX_LENGTH;
  20886. if (input.length > max) {
  20887. throw new SyntaxError(`Input length (${input.length}), exceeds max characters (${max})`);
  20888. }
  20889. const ast = { type: 'root', input, nodes: [] };
  20890. const stack = [ast];
  20891. let block = ast;
  20892. let prev = ast;
  20893. let brackets = 0;
  20894. const length = input.length;
  20895. let index = 0;
  20896. let depth = 0;
  20897. let value;
  20898. /**
  20899. * Helpers
  20900. */
  20901. const advance = () => input[index++];
  20902. const push = node => {
  20903. if (node.type === 'text' && prev.type === 'dot') {
  20904. prev.type = 'text';
  20905. }
  20906. if (prev && prev.type === 'text' && node.type === 'text') {
  20907. prev.value += node.value;
  20908. return;
  20909. }
  20910. block.nodes.push(node);
  20911. node.parent = block;
  20912. node.prev = prev;
  20913. prev = node;
  20914. return node;
  20915. };
  20916. push({ type: 'bos' });
  20917. while (index < length) {
  20918. block = stack[stack.length - 1];
  20919. value = advance();
  20920. /**
  20921. * Invalid chars
  20922. */
  20923. if (value === CHAR_ZERO_WIDTH_NOBREAK_SPACE || value === CHAR_NO_BREAK_SPACE) {
  20924. continue;
  20925. }
  20926. /**
  20927. * Escaped chars
  20928. */
  20929. if (value === CHAR_BACKSLASH) {
  20930. push({ type: 'text', value: (options.keepEscaping ? value : '') + advance() });
  20931. continue;
  20932. }
  20933. /**
  20934. * Right square bracket (literal): ']'
  20935. */
  20936. if (value === CHAR_RIGHT_SQUARE_BRACKET) {
  20937. push({ type: 'text', value: '\\' + value });
  20938. continue;
  20939. }
  20940. /**
  20941. * Left square bracket: '['
  20942. */
  20943. if (value === CHAR_LEFT_SQUARE_BRACKET) {
  20944. brackets++;
  20945. let next;
  20946. while (index < length && (next = advance())) {
  20947. value += next;
  20948. if (next === CHAR_LEFT_SQUARE_BRACKET) {
  20949. brackets++;
  20950. continue;
  20951. }
  20952. if (next === CHAR_BACKSLASH) {
  20953. value += advance();
  20954. continue;
  20955. }
  20956. if (next === CHAR_RIGHT_SQUARE_BRACKET) {
  20957. brackets--;
  20958. if (brackets === 0) {
  20959. break;
  20960. }
  20961. }
  20962. }
  20963. push({ type: 'text', value });
  20964. continue;
  20965. }
  20966. /**
  20967. * Parentheses
  20968. */
  20969. if (value === CHAR_LEFT_PARENTHESES) {
  20970. block = push({ type: 'paren', nodes: [] });
  20971. stack.push(block);
  20972. push({ type: 'text', value });
  20973. continue;
  20974. }
  20975. if (value === CHAR_RIGHT_PARENTHESES) {
  20976. if (block.type !== 'paren') {
  20977. push({ type: 'text', value });
  20978. continue;
  20979. }
  20980. block = stack.pop();
  20981. push({ type: 'text', value });
  20982. block = stack[stack.length - 1];
  20983. continue;
  20984. }
  20985. /**
  20986. * Quotes: '|"|`
  20987. */
  20988. if (value === CHAR_DOUBLE_QUOTE || value === CHAR_SINGLE_QUOTE || value === CHAR_BACKTICK) {
  20989. const open = value;
  20990. let next;
  20991. if (options.keepQuotes !== true) {
  20992. value = '';
  20993. }
  20994. while (index < length && (next = advance())) {
  20995. if (next === CHAR_BACKSLASH) {
  20996. value += next + advance();
  20997. continue;
  20998. }
  20999. if (next === open) {
  21000. if (options.keepQuotes === true) value += next;
  21001. break;
  21002. }
  21003. value += next;
  21004. }
  21005. push({ type: 'text', value });
  21006. continue;
  21007. }
  21008. /**
  21009. * Left curly brace: '{'
  21010. */
  21011. if (value === CHAR_LEFT_CURLY_BRACE) {
  21012. depth++;
  21013. const dollar = prev.value && prev.value.slice(-1) === '$' || block.dollar === true;
  21014. const brace = {
  21015. type: 'brace',
  21016. open: true,
  21017. close: false,
  21018. dollar,
  21019. depth,
  21020. commas: 0,
  21021. ranges: 0,
  21022. nodes: []
  21023. };
  21024. block = push(brace);
  21025. stack.push(block);
  21026. push({ type: 'open', value });
  21027. continue;
  21028. }
  21029. /**
  21030. * Right curly brace: '}'
  21031. */
  21032. if (value === CHAR_RIGHT_CURLY_BRACE) {
  21033. if (block.type !== 'brace') {
  21034. push({ type: 'text', value });
  21035. continue;
  21036. }
  21037. const type = 'close';
  21038. block = stack.pop();
  21039. block.close = true;
  21040. push({ type, value });
  21041. depth--;
  21042. block = stack[stack.length - 1];
  21043. continue;
  21044. }
  21045. /**
  21046. * Comma: ','
  21047. */
  21048. if (value === CHAR_COMMA && depth > 0) {
  21049. if (block.ranges > 0) {
  21050. block.ranges = 0;
  21051. const open = block.nodes.shift();
  21052. block.nodes = [open, { type: 'text', value: stringify$4(block) }];
  21053. }
  21054. push({ type: 'comma', value });
  21055. block.commas++;
  21056. continue;
  21057. }
  21058. /**
  21059. * Dot: '.'
  21060. */
  21061. if (value === CHAR_DOT && depth > 0 && block.commas === 0) {
  21062. const siblings = block.nodes;
  21063. if (depth === 0 || siblings.length === 0) {
  21064. push({ type: 'text', value });
  21065. continue;
  21066. }
  21067. if (prev.type === 'dot') {
  21068. block.range = [];
  21069. prev.value += value;
  21070. prev.type = 'range';
  21071. if (block.nodes.length !== 3 && block.nodes.length !== 5) {
  21072. block.invalid = true;
  21073. block.ranges = 0;
  21074. prev.type = 'text';
  21075. continue;
  21076. }
  21077. block.ranges++;
  21078. block.args = [];
  21079. continue;
  21080. }
  21081. if (prev.type === 'range') {
  21082. siblings.pop();
  21083. const before = siblings[siblings.length - 1];
  21084. before.value += prev.value + value;
  21085. prev = before;
  21086. block.ranges--;
  21087. continue;
  21088. }
  21089. push({ type: 'dot', value });
  21090. continue;
  21091. }
  21092. /**
  21093. * Text
  21094. */
  21095. push({ type: 'text', value });
  21096. }
  21097. // Mark imbalanced braces and brackets as invalid
  21098. do {
  21099. block = stack.pop();
  21100. if (block.type !== 'root') {
  21101. block.nodes.forEach(node => {
  21102. if (!node.nodes) {
  21103. if (node.type === 'open') node.isOpen = true;
  21104. if (node.type === 'close') node.isClose = true;
  21105. if (!node.nodes) node.type = 'text';
  21106. node.invalid = true;
  21107. }
  21108. });
  21109. // get the location of the block on parent.nodes (block's siblings)
  21110. const parent = stack[stack.length - 1];
  21111. const index = parent.nodes.indexOf(block);
  21112. // replace the (invalid) block with it's nodes
  21113. parent.nodes.splice(index, 1, ...block.nodes);
  21114. }
  21115. } while (stack.length > 0);
  21116. push({ type: 'eos' });
  21117. return ast;
  21118. };
  21119. var parse_1$2 = parse$c;
  21120. const stringify$3 = stringify$7;
  21121. const compile = compile_1;
  21122. const expand$1 = expand_1$1;
  21123. const parse$b = parse_1$2;
  21124. /**
  21125. * Expand the given pattern or create a regex-compatible string.
  21126. *
  21127. * ```js
  21128. * const braces = require('braces');
  21129. * console.log(braces('{a,b,c}', { compile: true })); //=> ['(a|b|c)']
  21130. * console.log(braces('{a,b,c}')); //=> ['a', 'b', 'c']
  21131. * ```
  21132. * @param {String} `str`
  21133. * @param {Object} `options`
  21134. * @return {String}
  21135. * @api public
  21136. */
  21137. const braces$2 = (input, options = {}) => {
  21138. let output = [];
  21139. if (Array.isArray(input)) {
  21140. for (const pattern of input) {
  21141. const result = braces$2.create(pattern, options);
  21142. if (Array.isArray(result)) {
  21143. output.push(...result);
  21144. } else {
  21145. output.push(result);
  21146. }
  21147. }
  21148. } else {
  21149. output = [].concat(braces$2.create(input, options));
  21150. }
  21151. if (options && options.expand === true && options.nodupes === true) {
  21152. output = [...new Set(output)];
  21153. }
  21154. return output;
  21155. };
  21156. /**
  21157. * Parse the given `str` with the given `options`.
  21158. *
  21159. * ```js
  21160. * // braces.parse(pattern, [, options]);
  21161. * const ast = braces.parse('a/{b,c}/d');
  21162. * console.log(ast);
  21163. * ```
  21164. * @param {String} pattern Brace pattern to parse
  21165. * @param {Object} options
  21166. * @return {Object} Returns an AST
  21167. * @api public
  21168. */
  21169. braces$2.parse = (input, options = {}) => parse$b(input, options);
  21170. /**
  21171. * Creates a braces string from an AST, or an AST node.
  21172. *
  21173. * ```js
  21174. * const braces = require('braces');
  21175. * let ast = braces.parse('foo/{a,b}/bar');
  21176. * console.log(stringify(ast.nodes[2])); //=> '{a,b}'
  21177. * ```
  21178. * @param {String} `input` Brace pattern or AST.
  21179. * @param {Object} `options`
  21180. * @return {Array} Returns an array of expanded values.
  21181. * @api public
  21182. */
  21183. braces$2.stringify = (input, options = {}) => {
  21184. if (typeof input === 'string') {
  21185. return stringify$3(braces$2.parse(input, options), options);
  21186. }
  21187. return stringify$3(input, options);
  21188. };
  21189. /**
  21190. * Compiles a brace pattern into a regex-compatible, optimized string.
  21191. * This method is called by the main [braces](#braces) function by default.
  21192. *
  21193. * ```js
  21194. * const braces = require('braces');
  21195. * console.log(braces.compile('a/{b,c}/d'));
  21196. * //=> ['a/(b|c)/d']
  21197. * ```
  21198. * @param {String} `input` Brace pattern or AST.
  21199. * @param {Object} `options`
  21200. * @return {Array} Returns an array of expanded values.
  21201. * @api public
  21202. */
  21203. braces$2.compile = (input, options = {}) => {
  21204. if (typeof input === 'string') {
  21205. input = braces$2.parse(input, options);
  21206. }
  21207. return compile(input, options);
  21208. };
  21209. /**
  21210. * Expands a brace pattern into an array. This method is called by the
  21211. * main [braces](#braces) function when `options.expand` is true. Before
  21212. * using this method it's recommended that you read the [performance notes](#performance))
  21213. * and advantages of using [.compile](#compile) instead.
  21214. *
  21215. * ```js
  21216. * const braces = require('braces');
  21217. * console.log(braces.expand('a/{b,c}/d'));
  21218. * //=> ['a/b/d', 'a/c/d'];
  21219. * ```
  21220. * @param {String} `pattern` Brace pattern
  21221. * @param {Object} `options`
  21222. * @return {Array} Returns an array of expanded values.
  21223. * @api public
  21224. */
  21225. braces$2.expand = (input, options = {}) => {
  21226. if (typeof input === 'string') {
  21227. input = braces$2.parse(input, options);
  21228. }
  21229. let result = expand$1(input, options);
  21230. // filter out empty strings if specified
  21231. if (options.noempty === true) {
  21232. result = result.filter(Boolean);
  21233. }
  21234. // filter out duplicates if specified
  21235. if (options.nodupes === true) {
  21236. result = [...new Set(result)];
  21237. }
  21238. return result;
  21239. };
  21240. /**
  21241. * Processes a brace pattern and returns either an expanded array
  21242. * (if `options.expand` is true), a highly optimized regex-compatible string.
  21243. * This method is called by the main [braces](#braces) function.
  21244. *
  21245. * ```js
  21246. * const braces = require('braces');
  21247. * console.log(braces.create('user-{200..300}/project-{a,b,c}-{1..10}'))
  21248. * //=> 'user-(20[0-9]|2[1-9][0-9]|300)/project-(a|b|c)-([1-9]|10)'
  21249. * ```
  21250. * @param {String} `pattern` Brace pattern
  21251. * @param {Object} `options`
  21252. * @return {Array} Returns an array of expanded values.
  21253. * @api public
  21254. */
  21255. braces$2.create = (input, options = {}) => {
  21256. if (input === '' || input.length < 3) {
  21257. return [input];
  21258. }
  21259. return options.expand !== true
  21260. ? braces$2.compile(input, options)
  21261. : braces$2.expand(input, options);
  21262. };
  21263. /**
  21264. * Expose "braces"
  21265. */
  21266. var braces_1 = braces$2;
  21267. const util = require$$0$5;
  21268. const braces$1 = braces_1;
  21269. const picomatch$2 = picomatch$3;
  21270. const utils$b = utils$k;
  21271. const isEmptyString = v => v === '' || v === './';
  21272. const hasBraces = v => {
  21273. const index = v.indexOf('{');
  21274. return index > -1 && v.indexOf('}', index) > -1;
  21275. };
  21276. /**
  21277. * Returns an array of strings that match one or more glob patterns.
  21278. *
  21279. * ```js
  21280. * const mm = require('micromatch');
  21281. * // mm(list, patterns[, options]);
  21282. *
  21283. * console.log(mm(['a.js', 'a.txt'], ['*.js']));
  21284. * //=> [ 'a.js' ]
  21285. * ```
  21286. * @param {String|Array<string>} `list` List of strings to match.
  21287. * @param {String|Array<string>} `patterns` One or more glob patterns to use for matching.
  21288. * @param {Object} `options` See available [options](#options)
  21289. * @return {Array} Returns an array of matches
  21290. * @summary false
  21291. * @api public
  21292. */
  21293. const micromatch$1 = (list, patterns, options) => {
  21294. patterns = [].concat(patterns);
  21295. list = [].concat(list);
  21296. let omit = new Set();
  21297. let keep = new Set();
  21298. let items = new Set();
  21299. let negatives = 0;
  21300. let onResult = state => {
  21301. items.add(state.output);
  21302. if (options && options.onResult) {
  21303. options.onResult(state);
  21304. }
  21305. };
  21306. for (let i = 0; i < patterns.length; i++) {
  21307. let isMatch = picomatch$2(String(patterns[i]), { ...options, onResult }, true);
  21308. let negated = isMatch.state.negated || isMatch.state.negatedExtglob;
  21309. if (negated) negatives++;
  21310. for (let item of list) {
  21311. let matched = isMatch(item, true);
  21312. let match = negated ? !matched.isMatch : matched.isMatch;
  21313. if (!match) continue;
  21314. if (negated) {
  21315. omit.add(matched.output);
  21316. } else {
  21317. omit.delete(matched.output);
  21318. keep.add(matched.output);
  21319. }
  21320. }
  21321. }
  21322. let result = negatives === patterns.length ? [...items] : [...keep];
  21323. let matches = result.filter(item => !omit.has(item));
  21324. if (options && matches.length === 0) {
  21325. if (options.failglob === true) {
  21326. throw new Error(`No matches found for "${patterns.join(', ')}"`);
  21327. }
  21328. if (options.nonull === true || options.nullglob === true) {
  21329. return options.unescape ? patterns.map(p => p.replace(/\\/g, '')) : patterns;
  21330. }
  21331. }
  21332. return matches;
  21333. };
  21334. /**
  21335. * Backwards compatibility
  21336. */
  21337. micromatch$1.match = micromatch$1;
  21338. /**
  21339. * Returns a matcher function from the given glob `pattern` and `options`.
  21340. * The returned function takes a string to match as its only argument and returns
  21341. * true if the string is a match.
  21342. *
  21343. * ```js
  21344. * const mm = require('micromatch');
  21345. * // mm.matcher(pattern[, options]);
  21346. *
  21347. * const isMatch = mm.matcher('*.!(*a)');
  21348. * console.log(isMatch('a.a')); //=> false
  21349. * console.log(isMatch('a.b')); //=> true
  21350. * ```
  21351. * @param {String} `pattern` Glob pattern
  21352. * @param {Object} `options`
  21353. * @return {Function} Returns a matcher function.
  21354. * @api public
  21355. */
  21356. micromatch$1.matcher = (pattern, options) => picomatch$2(pattern, options);
  21357. /**
  21358. * Returns true if **any** of the given glob `patterns` match the specified `string`.
  21359. *
  21360. * ```js
  21361. * const mm = require('micromatch');
  21362. * // mm.isMatch(string, patterns[, options]);
  21363. *
  21364. * console.log(mm.isMatch('a.a', ['b.*', '*.a'])); //=> true
  21365. * console.log(mm.isMatch('a.a', 'b.*')); //=> false
  21366. * ```
  21367. * @param {String} `str` The string to test.
  21368. * @param {String|Array} `patterns` One or more glob patterns to use for matching.
  21369. * @param {Object} `[options]` See available [options](#options).
  21370. * @return {Boolean} Returns true if any patterns match `str`
  21371. * @api public
  21372. */
  21373. micromatch$1.isMatch = (str, patterns, options) => picomatch$2(patterns, options)(str);
  21374. /**
  21375. * Backwards compatibility
  21376. */
  21377. micromatch$1.any = micromatch$1.isMatch;
  21378. /**
  21379. * Returns a list of strings that _**do not match any**_ of the given `patterns`.
  21380. *
  21381. * ```js
  21382. * const mm = require('micromatch');
  21383. * // mm.not(list, patterns[, options]);
  21384. *
  21385. * console.log(mm.not(['a.a', 'b.b', 'c.c'], '*.a'));
  21386. * //=> ['b.b', 'c.c']
  21387. * ```
  21388. * @param {Array} `list` Array of strings to match.
  21389. * @param {String|Array} `patterns` One or more glob pattern to use for matching.
  21390. * @param {Object} `options` See available [options](#options) for changing how matches are performed
  21391. * @return {Array} Returns an array of strings that **do not match** the given patterns.
  21392. * @api public
  21393. */
  21394. micromatch$1.not = (list, patterns, options = {}) => {
  21395. patterns = [].concat(patterns).map(String);
  21396. let result = new Set();
  21397. let items = [];
  21398. let onResult = state => {
  21399. if (options.onResult) options.onResult(state);
  21400. items.push(state.output);
  21401. };
  21402. let matches = new Set(micromatch$1(list, patterns, { ...options, onResult }));
  21403. for (let item of items) {
  21404. if (!matches.has(item)) {
  21405. result.add(item);
  21406. }
  21407. }
  21408. return [...result];
  21409. };
  21410. /**
  21411. * Returns true if the given `string` contains the given pattern. Similar
  21412. * to [.isMatch](#isMatch) but the pattern can match any part of the string.
  21413. *
  21414. * ```js
  21415. * var mm = require('micromatch');
  21416. * // mm.contains(string, pattern[, options]);
  21417. *
  21418. * console.log(mm.contains('aa/bb/cc', '*b'));
  21419. * //=> true
  21420. * console.log(mm.contains('aa/bb/cc', '*d'));
  21421. * //=> false
  21422. * ```
  21423. * @param {String} `str` The string to match.
  21424. * @param {String|Array} `patterns` Glob pattern to use for matching.
  21425. * @param {Object} `options` See available [options](#options) for changing how matches are performed
  21426. * @return {Boolean} Returns true if any of the patterns matches any part of `str`.
  21427. * @api public
  21428. */
  21429. micromatch$1.contains = (str, pattern, options) => {
  21430. if (typeof str !== 'string') {
  21431. throw new TypeError(`Expected a string: "${util.inspect(str)}"`);
  21432. }
  21433. if (Array.isArray(pattern)) {
  21434. return pattern.some(p => micromatch$1.contains(str, p, options));
  21435. }
  21436. if (typeof pattern === 'string') {
  21437. if (isEmptyString(str) || isEmptyString(pattern)) {
  21438. return false;
  21439. }
  21440. if (str.includes(pattern) || (str.startsWith('./') && str.slice(2).includes(pattern))) {
  21441. return true;
  21442. }
  21443. }
  21444. return micromatch$1.isMatch(str, pattern, { ...options, contains: true });
  21445. };
  21446. /**
  21447. * Filter the keys of the given object with the given `glob` pattern
  21448. * and `options`. Does not attempt to match nested keys. If you need this feature,
  21449. * use [glob-object][] instead.
  21450. *
  21451. * ```js
  21452. * const mm = require('micromatch');
  21453. * // mm.matchKeys(object, patterns[, options]);
  21454. *
  21455. * const obj = { aa: 'a', ab: 'b', ac: 'c' };
  21456. * console.log(mm.matchKeys(obj, '*b'));
  21457. * //=> { ab: 'b' }
  21458. * ```
  21459. * @param {Object} `object` The object with keys to filter.
  21460. * @param {String|Array} `patterns` One or more glob patterns to use for matching.
  21461. * @param {Object} `options` See available [options](#options) for changing how matches are performed
  21462. * @return {Object} Returns an object with only keys that match the given patterns.
  21463. * @api public
  21464. */
  21465. micromatch$1.matchKeys = (obj, patterns, options) => {
  21466. if (!utils$b.isObject(obj)) {
  21467. throw new TypeError('Expected the first argument to be an object');
  21468. }
  21469. let keys = micromatch$1(Object.keys(obj), patterns, options);
  21470. let res = {};
  21471. for (let key of keys) res[key] = obj[key];
  21472. return res;
  21473. };
  21474. /**
  21475. * Returns true if some of the strings in the given `list` match any of the given glob `patterns`.
  21476. *
  21477. * ```js
  21478. * const mm = require('micromatch');
  21479. * // mm.some(list, patterns[, options]);
  21480. *
  21481. * console.log(mm.some(['foo.js', 'bar.js'], ['*.js', '!foo.js']));
  21482. * // true
  21483. * console.log(mm.some(['foo.js'], ['*.js', '!foo.js']));
  21484. * // false
  21485. * ```
  21486. * @param {String|Array} `list` The string or array of strings to test. Returns as soon as the first match is found.
  21487. * @param {String|Array} `patterns` One or more glob patterns to use for matching.
  21488. * @param {Object} `options` See available [options](#options) for changing how matches are performed
  21489. * @return {Boolean} Returns true if any `patterns` matches any of the strings in `list`
  21490. * @api public
  21491. */
  21492. micromatch$1.some = (list, patterns, options) => {
  21493. let items = [].concat(list);
  21494. for (let pattern of [].concat(patterns)) {
  21495. let isMatch = picomatch$2(String(pattern), options);
  21496. if (items.some(item => isMatch(item))) {
  21497. return true;
  21498. }
  21499. }
  21500. return false;
  21501. };
  21502. /**
  21503. * Returns true if every string in the given `list` matches
  21504. * any of the given glob `patterns`.
  21505. *
  21506. * ```js
  21507. * const mm = require('micromatch');
  21508. * // mm.every(list, patterns[, options]);
  21509. *
  21510. * console.log(mm.every('foo.js', ['foo.js']));
  21511. * // true
  21512. * console.log(mm.every(['foo.js', 'bar.js'], ['*.js']));
  21513. * // true
  21514. * console.log(mm.every(['foo.js', 'bar.js'], ['*.js', '!foo.js']));
  21515. * // false
  21516. * console.log(mm.every(['foo.js'], ['*.js', '!foo.js']));
  21517. * // false
  21518. * ```
  21519. * @param {String|Array} `list` The string or array of strings to test.
  21520. * @param {String|Array} `patterns` One or more glob patterns to use for matching.
  21521. * @param {Object} `options` See available [options](#options) for changing how matches are performed
  21522. * @return {Boolean} Returns true if all `patterns` matches all of the strings in `list`
  21523. * @api public
  21524. */
  21525. micromatch$1.every = (list, patterns, options) => {
  21526. let items = [].concat(list);
  21527. for (let pattern of [].concat(patterns)) {
  21528. let isMatch = picomatch$2(String(pattern), options);
  21529. if (!items.every(item => isMatch(item))) {
  21530. return false;
  21531. }
  21532. }
  21533. return true;
  21534. };
  21535. /**
  21536. * Returns true if **all** of the given `patterns` match
  21537. * the specified string.
  21538. *
  21539. * ```js
  21540. * const mm = require('micromatch');
  21541. * // mm.all(string, patterns[, options]);
  21542. *
  21543. * console.log(mm.all('foo.js', ['foo.js']));
  21544. * // true
  21545. *
  21546. * console.log(mm.all('foo.js', ['*.js', '!foo.js']));
  21547. * // false
  21548. *
  21549. * console.log(mm.all('foo.js', ['*.js', 'foo.js']));
  21550. * // true
  21551. *
  21552. * console.log(mm.all('foo.js', ['*.js', 'f*', '*o*', '*o.js']));
  21553. * // true
  21554. * ```
  21555. * @param {String|Array} `str` The string to test.
  21556. * @param {String|Array} `patterns` One or more glob patterns to use for matching.
  21557. * @param {Object} `options` See available [options](#options) for changing how matches are performed
  21558. * @return {Boolean} Returns true if any patterns match `str`
  21559. * @api public
  21560. */
  21561. micromatch$1.all = (str, patterns, options) => {
  21562. if (typeof str !== 'string') {
  21563. throw new TypeError(`Expected a string: "${util.inspect(str)}"`);
  21564. }
  21565. return [].concat(patterns).every(p => picomatch$2(p, options)(str));
  21566. };
  21567. /**
  21568. * Returns an array of matches captured by `pattern` in `string, or `null` if the pattern did not match.
  21569. *
  21570. * ```js
  21571. * const mm = require('micromatch');
  21572. * // mm.capture(pattern, string[, options]);
  21573. *
  21574. * console.log(mm.capture('test/*.js', 'test/foo.js'));
  21575. * //=> ['foo']
  21576. * console.log(mm.capture('test/*.js', 'foo/bar.css'));
  21577. * //=> null
  21578. * ```
  21579. * @param {String} `glob` Glob pattern to use for matching.
  21580. * @param {String} `input` String to match
  21581. * @param {Object} `options` See available [options](#options) for changing how matches are performed
  21582. * @return {Array|null} Returns an array of captures if the input matches the glob pattern, otherwise `null`.
  21583. * @api public
  21584. */
  21585. micromatch$1.capture = (glob, input, options) => {
  21586. let posix = utils$b.isWindows(options);
  21587. let regex = picomatch$2.makeRe(String(glob), { ...options, capture: true });
  21588. let match = regex.exec(posix ? utils$b.toPosixSlashes(input) : input);
  21589. if (match) {
  21590. return match.slice(1).map(v => v === void 0 ? '' : v);
  21591. }
  21592. };
  21593. /**
  21594. * Create a regular expression from the given glob `pattern`.
  21595. *
  21596. * ```js
  21597. * const mm = require('micromatch');
  21598. * // mm.makeRe(pattern[, options]);
  21599. *
  21600. * console.log(mm.makeRe('*.js'));
  21601. * //=> /^(?:(\.[\\\/])?(?!\.)(?=.)[^\/]*?\.js)$/
  21602. * ```
  21603. * @param {String} `pattern` A glob pattern to convert to regex.
  21604. * @param {Object} `options`
  21605. * @return {RegExp} Returns a regex created from the given pattern.
  21606. * @api public
  21607. */
  21608. micromatch$1.makeRe = (...args) => picomatch$2.makeRe(...args);
  21609. /**
  21610. * Scan a glob pattern to separate the pattern into segments. Used
  21611. * by the [split](#split) method.
  21612. *
  21613. * ```js
  21614. * const mm = require('micromatch');
  21615. * const state = mm.scan(pattern[, options]);
  21616. * ```
  21617. * @param {String} `pattern`
  21618. * @param {Object} `options`
  21619. * @return {Object} Returns an object with
  21620. * @api public
  21621. */
  21622. micromatch$1.scan = (...args) => picomatch$2.scan(...args);
  21623. /**
  21624. * Parse a glob pattern to create the source string for a regular
  21625. * expression.
  21626. *
  21627. * ```js
  21628. * const mm = require('micromatch');
  21629. * const state = mm.parse(pattern[, options]);
  21630. * ```
  21631. * @param {String} `glob`
  21632. * @param {Object} `options`
  21633. * @return {Object} Returns an object with useful properties and output to be used as regex source string.
  21634. * @api public
  21635. */
  21636. micromatch$1.parse = (patterns, options) => {
  21637. let res = [];
  21638. for (let pattern of [].concat(patterns || [])) {
  21639. for (let str of braces$1(String(pattern), options)) {
  21640. res.push(picomatch$2.parse(str, options));
  21641. }
  21642. }
  21643. return res;
  21644. };
  21645. /**
  21646. * Process the given brace `pattern`.
  21647. *
  21648. * ```js
  21649. * const { braces } = require('micromatch');
  21650. * console.log(braces('foo/{a,b,c}/bar'));
  21651. * //=> [ 'foo/(a|b|c)/bar' ]
  21652. *
  21653. * console.log(braces('foo/{a,b,c}/bar', { expand: true }));
  21654. * //=> [ 'foo/a/bar', 'foo/b/bar', 'foo/c/bar' ]
  21655. * ```
  21656. * @param {String} `pattern` String with brace pattern to process.
  21657. * @param {Object} `options` Any [options](#options) to change how expansion is performed. See the [braces][] library for all available options.
  21658. * @return {Array}
  21659. * @api public
  21660. */
  21661. micromatch$1.braces = (pattern, options) => {
  21662. if (typeof pattern !== 'string') throw new TypeError('Expected a string');
  21663. if ((options && options.nobrace === true) || !hasBraces(pattern)) {
  21664. return [pattern];
  21665. }
  21666. return braces$1(pattern, options);
  21667. };
  21668. /**
  21669. * Expand braces
  21670. */
  21671. micromatch$1.braceExpand = (pattern, options) => {
  21672. if (typeof pattern !== 'string') throw new TypeError('Expected a string');
  21673. return micromatch$1.braces(pattern, { ...options, expand: true });
  21674. };
  21675. /**
  21676. * Expose micromatch
  21677. */
  21678. // exposed for tests
  21679. micromatch$1.hasBraces = hasBraces;
  21680. var micromatch_1 = micromatch$1;
  21681. var micromatch$2 = /*@__PURE__*/getDefaultExportFromCjs(micromatch_1);
  21682. Object.defineProperty(pattern$1, "__esModule", { value: true });
  21683. pattern$1.removeDuplicateSlashes = pattern$1.matchAny = pattern$1.convertPatternsToRe = pattern$1.makeRe = pattern$1.getPatternParts = pattern$1.expandBraceExpansion = pattern$1.expandPatternsWithBraceExpansion = pattern$1.isAffectDepthOfReadingPattern = pattern$1.endsWithSlashGlobStar = pattern$1.hasGlobStar = pattern$1.getBaseDirectory = pattern$1.isPatternRelatedToParentDirectory = pattern$1.getPatternsOutsideCurrentDirectory = pattern$1.getPatternsInsideCurrentDirectory = pattern$1.getPositivePatterns = pattern$1.getNegativePatterns = pattern$1.isPositivePattern = pattern$1.isNegativePattern = pattern$1.convertToNegativePattern = pattern$1.convertToPositivePattern = pattern$1.isDynamicPattern = pattern$1.isStaticPattern = void 0;
  21684. const path$g = require$$0$4;
  21685. const globParent$1 = globParent$2;
  21686. const micromatch = micromatch_1;
  21687. const GLOBSTAR$1 = '**';
  21688. const ESCAPE_SYMBOL = '\\';
  21689. const COMMON_GLOB_SYMBOLS_RE = /[*?]|^!/;
  21690. const REGEX_CHARACTER_CLASS_SYMBOLS_RE = /\[[^[]*]/;
  21691. const REGEX_GROUP_SYMBOLS_RE = /(?:^|[^!*+?@])\([^(]*\|[^|]*\)/;
  21692. const GLOB_EXTENSION_SYMBOLS_RE = /[!*+?@]\([^(]*\)/;
  21693. const BRACE_EXPANSION_SEPARATORS_RE = /,|\.\./;
  21694. /**
  21695. * Matches a sequence of two or more consecutive slashes, excluding the first two slashes at the beginning of the string.
  21696. * The latter is due to the presence of the device path at the beginning of the UNC path.
  21697. */
  21698. const DOUBLE_SLASH_RE$1 = /(?!^)\/{2,}/g;
  21699. function isStaticPattern(pattern, options = {}) {
  21700. return !isDynamicPattern(pattern, options);
  21701. }
  21702. pattern$1.isStaticPattern = isStaticPattern;
  21703. function isDynamicPattern(pattern, options = {}) {
  21704. /**
  21705. * A special case with an empty string is necessary for matching patterns that start with a forward slash.
  21706. * An empty string cannot be a dynamic pattern.
  21707. * For example, the pattern `/lib/*` will be spread into parts: '', 'lib', '*'.
  21708. */
  21709. if (pattern === '') {
  21710. return false;
  21711. }
  21712. /**
  21713. * When the `caseSensitiveMatch` option is disabled, all patterns must be marked as dynamic, because we cannot check
  21714. * filepath directly (without read directory).
  21715. */
  21716. if (options.caseSensitiveMatch === false || pattern.includes(ESCAPE_SYMBOL)) {
  21717. return true;
  21718. }
  21719. if (COMMON_GLOB_SYMBOLS_RE.test(pattern) || REGEX_CHARACTER_CLASS_SYMBOLS_RE.test(pattern) || REGEX_GROUP_SYMBOLS_RE.test(pattern)) {
  21720. return true;
  21721. }
  21722. if (options.extglob !== false && GLOB_EXTENSION_SYMBOLS_RE.test(pattern)) {
  21723. return true;
  21724. }
  21725. if (options.braceExpansion !== false && hasBraceExpansion(pattern)) {
  21726. return true;
  21727. }
  21728. return false;
  21729. }
  21730. pattern$1.isDynamicPattern = isDynamicPattern;
  21731. function hasBraceExpansion(pattern) {
  21732. const openingBraceIndex = pattern.indexOf('{');
  21733. if (openingBraceIndex === -1) {
  21734. return false;
  21735. }
  21736. const closingBraceIndex = pattern.indexOf('}', openingBraceIndex + 1);
  21737. if (closingBraceIndex === -1) {
  21738. return false;
  21739. }
  21740. const braceContent = pattern.slice(openingBraceIndex, closingBraceIndex);
  21741. return BRACE_EXPANSION_SEPARATORS_RE.test(braceContent);
  21742. }
  21743. function convertToPositivePattern(pattern) {
  21744. return isNegativePattern(pattern) ? pattern.slice(1) : pattern;
  21745. }
  21746. pattern$1.convertToPositivePattern = convertToPositivePattern;
  21747. function convertToNegativePattern(pattern) {
  21748. return '!' + pattern;
  21749. }
  21750. pattern$1.convertToNegativePattern = convertToNegativePattern;
  21751. function isNegativePattern(pattern) {
  21752. return pattern.startsWith('!') && pattern[1] !== '(';
  21753. }
  21754. pattern$1.isNegativePattern = isNegativePattern;
  21755. function isPositivePattern(pattern) {
  21756. return !isNegativePattern(pattern);
  21757. }
  21758. pattern$1.isPositivePattern = isPositivePattern;
  21759. function getNegativePatterns(patterns) {
  21760. return patterns.filter(isNegativePattern);
  21761. }
  21762. pattern$1.getNegativePatterns = getNegativePatterns;
  21763. function getPositivePatterns$1(patterns) {
  21764. return patterns.filter(isPositivePattern);
  21765. }
  21766. pattern$1.getPositivePatterns = getPositivePatterns$1;
  21767. /**
  21768. * Returns patterns that can be applied inside the current directory.
  21769. *
  21770. * @example
  21771. * // ['./*', '*', 'a/*']
  21772. * getPatternsInsideCurrentDirectory(['./*', '*', 'a/*', '../*', './../*'])
  21773. */
  21774. function getPatternsInsideCurrentDirectory(patterns) {
  21775. return patterns.filter((pattern) => !isPatternRelatedToParentDirectory(pattern));
  21776. }
  21777. pattern$1.getPatternsInsideCurrentDirectory = getPatternsInsideCurrentDirectory;
  21778. /**
  21779. * Returns patterns to be expanded relative to (outside) the current directory.
  21780. *
  21781. * @example
  21782. * // ['../*', './../*']
  21783. * getPatternsInsideCurrentDirectory(['./*', '*', 'a/*', '../*', './../*'])
  21784. */
  21785. function getPatternsOutsideCurrentDirectory(patterns) {
  21786. return patterns.filter(isPatternRelatedToParentDirectory);
  21787. }
  21788. pattern$1.getPatternsOutsideCurrentDirectory = getPatternsOutsideCurrentDirectory;
  21789. function isPatternRelatedToParentDirectory(pattern) {
  21790. return pattern.startsWith('..') || pattern.startsWith('./..');
  21791. }
  21792. pattern$1.isPatternRelatedToParentDirectory = isPatternRelatedToParentDirectory;
  21793. function getBaseDirectory(pattern) {
  21794. return globParent$1(pattern, { flipBackslashes: false });
  21795. }
  21796. pattern$1.getBaseDirectory = getBaseDirectory;
  21797. function hasGlobStar(pattern) {
  21798. return pattern.includes(GLOBSTAR$1);
  21799. }
  21800. pattern$1.hasGlobStar = hasGlobStar;
  21801. function endsWithSlashGlobStar(pattern) {
  21802. return pattern.endsWith('/' + GLOBSTAR$1);
  21803. }
  21804. pattern$1.endsWithSlashGlobStar = endsWithSlashGlobStar;
  21805. function isAffectDepthOfReadingPattern(pattern) {
  21806. const basename = path$g.basename(pattern);
  21807. return endsWithSlashGlobStar(pattern) || isStaticPattern(basename);
  21808. }
  21809. pattern$1.isAffectDepthOfReadingPattern = isAffectDepthOfReadingPattern;
  21810. function expandPatternsWithBraceExpansion(patterns) {
  21811. return patterns.reduce((collection, pattern) => {
  21812. return collection.concat(expandBraceExpansion(pattern));
  21813. }, []);
  21814. }
  21815. pattern$1.expandPatternsWithBraceExpansion = expandPatternsWithBraceExpansion;
  21816. function expandBraceExpansion(pattern) {
  21817. const patterns = micromatch.braces(pattern, { expand: true, nodupes: true, keepEscaping: true });
  21818. /**
  21819. * Sort the patterns by length so that the same depth patterns are processed side by side.
  21820. * `a/{b,}/{c,}/*` – `['a///*', 'a/b//*', 'a//c/*', 'a/b/c/*']`
  21821. */
  21822. patterns.sort((a, b) => a.length - b.length);
  21823. /**
  21824. * Micromatch can return an empty string in the case of patterns like `{a,}`.
  21825. */
  21826. return patterns.filter((pattern) => pattern !== '');
  21827. }
  21828. pattern$1.expandBraceExpansion = expandBraceExpansion;
  21829. function getPatternParts(pattern, options) {
  21830. let { parts } = micromatch.scan(pattern, Object.assign(Object.assign({}, options), { parts: true }));
  21831. /**
  21832. * The scan method returns an empty array in some cases.
  21833. * See micromatch/picomatch#58 for more details.
  21834. */
  21835. if (parts.length === 0) {
  21836. parts = [pattern];
  21837. }
  21838. /**
  21839. * The scan method does not return an empty part for the pattern with a forward slash.
  21840. * This is another part of micromatch/picomatch#58.
  21841. */
  21842. if (parts[0].startsWith('/')) {
  21843. parts[0] = parts[0].slice(1);
  21844. parts.unshift('');
  21845. }
  21846. return parts;
  21847. }
  21848. pattern$1.getPatternParts = getPatternParts;
  21849. function makeRe(pattern, options) {
  21850. return micromatch.makeRe(pattern, options);
  21851. }
  21852. pattern$1.makeRe = makeRe;
  21853. function convertPatternsToRe(patterns, options) {
  21854. return patterns.map((pattern) => makeRe(pattern, options));
  21855. }
  21856. pattern$1.convertPatternsToRe = convertPatternsToRe;
  21857. function matchAny(entry, patternsRe) {
  21858. return patternsRe.some((patternRe) => patternRe.test(entry));
  21859. }
  21860. pattern$1.matchAny = matchAny;
  21861. /**
  21862. * This package only works with forward slashes as a path separator.
  21863. * Because of this, we cannot use the standard `path.normalize` method, because on Windows platform it will use of backslashes.
  21864. */
  21865. function removeDuplicateSlashes(pattern) {
  21866. return pattern.replace(DOUBLE_SLASH_RE$1, '/');
  21867. }
  21868. pattern$1.removeDuplicateSlashes = removeDuplicateSlashes;
  21869. var stream$4 = {};
  21870. /*
  21871. * merge2
  21872. * https://github.com/teambition/merge2
  21873. *
  21874. * Copyright (c) 2014-2020 Teambition
  21875. * Licensed under the MIT license.
  21876. */
  21877. const Stream = require$$0$6;
  21878. const PassThrough = Stream.PassThrough;
  21879. const slice = Array.prototype.slice;
  21880. var merge2_1 = merge2$1;
  21881. function merge2$1 () {
  21882. const streamsQueue = [];
  21883. const args = slice.call(arguments);
  21884. let merging = false;
  21885. let options = args[args.length - 1];
  21886. if (options && !Array.isArray(options) && options.pipe == null) {
  21887. args.pop();
  21888. } else {
  21889. options = {};
  21890. }
  21891. const doEnd = options.end !== false;
  21892. const doPipeError = options.pipeError === true;
  21893. if (options.objectMode == null) {
  21894. options.objectMode = true;
  21895. }
  21896. if (options.highWaterMark == null) {
  21897. options.highWaterMark = 64 * 1024;
  21898. }
  21899. const mergedStream = PassThrough(options);
  21900. function addStream () {
  21901. for (let i = 0, len = arguments.length; i < len; i++) {
  21902. streamsQueue.push(pauseStreams(arguments[i], options));
  21903. }
  21904. mergeStream();
  21905. return this
  21906. }
  21907. function mergeStream () {
  21908. if (merging) {
  21909. return
  21910. }
  21911. merging = true;
  21912. let streams = streamsQueue.shift();
  21913. if (!streams) {
  21914. process.nextTick(endStream);
  21915. return
  21916. }
  21917. if (!Array.isArray(streams)) {
  21918. streams = [streams];
  21919. }
  21920. let pipesCount = streams.length + 1;
  21921. function next () {
  21922. if (--pipesCount > 0) {
  21923. return
  21924. }
  21925. merging = false;
  21926. mergeStream();
  21927. }
  21928. function pipe (stream) {
  21929. function onend () {
  21930. stream.removeListener('merge2UnpipeEnd', onend);
  21931. stream.removeListener('end', onend);
  21932. if (doPipeError) {
  21933. stream.removeListener('error', onerror);
  21934. }
  21935. next();
  21936. }
  21937. function onerror (err) {
  21938. mergedStream.emit('error', err);
  21939. }
  21940. // skip ended stream
  21941. if (stream._readableState.endEmitted) {
  21942. return next()
  21943. }
  21944. stream.on('merge2UnpipeEnd', onend);
  21945. stream.on('end', onend);
  21946. if (doPipeError) {
  21947. stream.on('error', onerror);
  21948. }
  21949. stream.pipe(mergedStream, { end: false });
  21950. // compatible for old stream
  21951. stream.resume();
  21952. }
  21953. for (let i = 0; i < streams.length; i++) {
  21954. pipe(streams[i]);
  21955. }
  21956. next();
  21957. }
  21958. function endStream () {
  21959. merging = false;
  21960. // emit 'queueDrain' when all streams merged.
  21961. mergedStream.emit('queueDrain');
  21962. if (doEnd) {
  21963. mergedStream.end();
  21964. }
  21965. }
  21966. mergedStream.setMaxListeners(0);
  21967. mergedStream.add = addStream;
  21968. mergedStream.on('unpipe', function (stream) {
  21969. stream.emit('merge2UnpipeEnd');
  21970. });
  21971. if (args.length) {
  21972. addStream.apply(null, args);
  21973. }
  21974. return mergedStream
  21975. }
  21976. // check and pause streams for pipe.
  21977. function pauseStreams (streams, options) {
  21978. if (!Array.isArray(streams)) {
  21979. // Backwards-compat with old-style streams
  21980. if (!streams._readableState && streams.pipe) {
  21981. streams = streams.pipe(PassThrough(options));
  21982. }
  21983. if (!streams._readableState || !streams.pause || !streams.pipe) {
  21984. throw new Error('Only readable stream can be merged.')
  21985. }
  21986. streams.pause();
  21987. } else {
  21988. for (let i = 0, len = streams.length; i < len; i++) {
  21989. streams[i] = pauseStreams(streams[i], options);
  21990. }
  21991. }
  21992. return streams
  21993. }
  21994. Object.defineProperty(stream$4, "__esModule", { value: true });
  21995. stream$4.merge = void 0;
  21996. const merge2 = merge2_1;
  21997. function merge$1(streams) {
  21998. const mergedStream = merge2(streams);
  21999. streams.forEach((stream) => {
  22000. stream.once('error', (error) => mergedStream.emit('error', error));
  22001. });
  22002. mergedStream.once('close', () => propagateCloseEventToSources(streams));
  22003. mergedStream.once('end', () => propagateCloseEventToSources(streams));
  22004. return mergedStream;
  22005. }
  22006. stream$4.merge = merge$1;
  22007. function propagateCloseEventToSources(streams) {
  22008. streams.forEach((stream) => stream.emit('close'));
  22009. }
  22010. var string$2 = {};
  22011. Object.defineProperty(string$2, "__esModule", { value: true });
  22012. string$2.isEmpty = string$2.isString = void 0;
  22013. function isString$1(input) {
  22014. return typeof input === 'string';
  22015. }
  22016. string$2.isString = isString$1;
  22017. function isEmpty$1(input) {
  22018. return input === '';
  22019. }
  22020. string$2.isEmpty = isEmpty$1;
  22021. Object.defineProperty(utils$g, "__esModule", { value: true });
  22022. utils$g.string = utils$g.stream = utils$g.pattern = utils$g.path = utils$g.fs = utils$g.errno = utils$g.array = void 0;
  22023. const array = array$1;
  22024. utils$g.array = array;
  22025. const errno = errno$1;
  22026. utils$g.errno = errno;
  22027. const fs$h = fs$i;
  22028. utils$g.fs = fs$h;
  22029. const path$f = path$i;
  22030. utils$g.path = path$f;
  22031. const pattern = pattern$1;
  22032. utils$g.pattern = pattern;
  22033. const stream$3 = stream$4;
  22034. utils$g.stream = stream$3;
  22035. const string$1 = string$2;
  22036. utils$g.string = string$1;
  22037. Object.defineProperty(tasks, "__esModule", { value: true });
  22038. tasks.convertPatternGroupToTask = tasks.convertPatternGroupsToTasks = tasks.groupPatternsByBaseDirectory = tasks.getNegativePatternsAsPositive = tasks.getPositivePatterns = tasks.convertPatternsToTasks = tasks.generate = void 0;
  22039. const utils$a = utils$g;
  22040. function generate(input, settings) {
  22041. const patterns = processPatterns(input, settings);
  22042. const ignore = processPatterns(settings.ignore, settings);
  22043. const positivePatterns = getPositivePatterns(patterns);
  22044. const negativePatterns = getNegativePatternsAsPositive(patterns, ignore);
  22045. const staticPatterns = positivePatterns.filter((pattern) => utils$a.pattern.isStaticPattern(pattern, settings));
  22046. const dynamicPatterns = positivePatterns.filter((pattern) => utils$a.pattern.isDynamicPattern(pattern, settings));
  22047. const staticTasks = convertPatternsToTasks(staticPatterns, negativePatterns, /* dynamic */ false);
  22048. const dynamicTasks = convertPatternsToTasks(dynamicPatterns, negativePatterns, /* dynamic */ true);
  22049. return staticTasks.concat(dynamicTasks);
  22050. }
  22051. tasks.generate = generate;
  22052. function processPatterns(input, settings) {
  22053. let patterns = input;
  22054. /**
  22055. * The original pattern like `{,*,**,a/*}` can lead to problems checking the depth when matching entry
  22056. * and some problems with the micromatch package (see fast-glob issues: #365, #394).
  22057. *
  22058. * To solve this problem, we expand all patterns containing brace expansion. This can lead to a slight slowdown
  22059. * in matching in the case of a large set of patterns after expansion.
  22060. */
  22061. if (settings.braceExpansion) {
  22062. patterns = utils$a.pattern.expandPatternsWithBraceExpansion(patterns);
  22063. }
  22064. /**
  22065. * If the `baseNameMatch` option is enabled, we must add globstar to patterns, so that they can be used
  22066. * at any nesting level.
  22067. *
  22068. * We do this here, because otherwise we have to complicate the filtering logic. For example, we need to change
  22069. * the pattern in the filter before creating a regular expression. There is no need to change the patterns
  22070. * in the application. Only on the input.
  22071. */
  22072. if (settings.baseNameMatch) {
  22073. patterns = patterns.map((pattern) => pattern.includes('/') ? pattern : `**/${pattern}`);
  22074. }
  22075. /**
  22076. * This method also removes duplicate slashes that may have been in the pattern or formed as a result of expansion.
  22077. */
  22078. return patterns.map((pattern) => utils$a.pattern.removeDuplicateSlashes(pattern));
  22079. }
  22080. /**
  22081. * Returns tasks grouped by basic pattern directories.
  22082. *
  22083. * Patterns that can be found inside (`./`) and outside (`../`) the current directory are handled separately.
  22084. * This is necessary because directory traversal starts at the base directory and goes deeper.
  22085. */
  22086. function convertPatternsToTasks(positive, negative, dynamic) {
  22087. const tasks = [];
  22088. const patternsOutsideCurrentDirectory = utils$a.pattern.getPatternsOutsideCurrentDirectory(positive);
  22089. const patternsInsideCurrentDirectory = utils$a.pattern.getPatternsInsideCurrentDirectory(positive);
  22090. const outsideCurrentDirectoryGroup = groupPatternsByBaseDirectory(patternsOutsideCurrentDirectory);
  22091. const insideCurrentDirectoryGroup = groupPatternsByBaseDirectory(patternsInsideCurrentDirectory);
  22092. tasks.push(...convertPatternGroupsToTasks(outsideCurrentDirectoryGroup, negative, dynamic));
  22093. /*
  22094. * For the sake of reducing future accesses to the file system, we merge all tasks within the current directory
  22095. * into a global task, if at least one pattern refers to the root (`.`). In this case, the global task covers the rest.
  22096. */
  22097. if ('.' in insideCurrentDirectoryGroup) {
  22098. tasks.push(convertPatternGroupToTask('.', patternsInsideCurrentDirectory, negative, dynamic));
  22099. }
  22100. else {
  22101. tasks.push(...convertPatternGroupsToTasks(insideCurrentDirectoryGroup, negative, dynamic));
  22102. }
  22103. return tasks;
  22104. }
  22105. tasks.convertPatternsToTasks = convertPatternsToTasks;
  22106. function getPositivePatterns(patterns) {
  22107. return utils$a.pattern.getPositivePatterns(patterns);
  22108. }
  22109. tasks.getPositivePatterns = getPositivePatterns;
  22110. function getNegativePatternsAsPositive(patterns, ignore) {
  22111. const negative = utils$a.pattern.getNegativePatterns(patterns).concat(ignore);
  22112. const positive = negative.map(utils$a.pattern.convertToPositivePattern);
  22113. return positive;
  22114. }
  22115. tasks.getNegativePatternsAsPositive = getNegativePatternsAsPositive;
  22116. function groupPatternsByBaseDirectory(patterns) {
  22117. const group = {};
  22118. return patterns.reduce((collection, pattern) => {
  22119. const base = utils$a.pattern.getBaseDirectory(pattern);
  22120. if (base in collection) {
  22121. collection[base].push(pattern);
  22122. }
  22123. else {
  22124. collection[base] = [pattern];
  22125. }
  22126. return collection;
  22127. }, group);
  22128. }
  22129. tasks.groupPatternsByBaseDirectory = groupPatternsByBaseDirectory;
  22130. function convertPatternGroupsToTasks(positive, negative, dynamic) {
  22131. return Object.keys(positive).map((base) => {
  22132. return convertPatternGroupToTask(base, positive[base], negative, dynamic);
  22133. });
  22134. }
  22135. tasks.convertPatternGroupsToTasks = convertPatternGroupsToTasks;
  22136. function convertPatternGroupToTask(base, positive, negative, dynamic) {
  22137. return {
  22138. dynamic,
  22139. positive,
  22140. negative,
  22141. base,
  22142. patterns: [].concat(positive, negative.map(utils$a.pattern.convertToNegativePattern))
  22143. };
  22144. }
  22145. tasks.convertPatternGroupToTask = convertPatternGroupToTask;
  22146. var async$7 = {};
  22147. var async$6 = {};
  22148. var out$3 = {};
  22149. var async$5 = {};
  22150. var async$4 = {};
  22151. var out$2 = {};
  22152. var async$3 = {};
  22153. var out$1 = {};
  22154. var async$2 = {};
  22155. Object.defineProperty(async$2, "__esModule", { value: true });
  22156. async$2.read = void 0;
  22157. function read$3(path, settings, callback) {
  22158. settings.fs.lstat(path, (lstatError, lstat) => {
  22159. if (lstatError !== null) {
  22160. callFailureCallback$2(callback, lstatError);
  22161. return;
  22162. }
  22163. if (!lstat.isSymbolicLink() || !settings.followSymbolicLink) {
  22164. callSuccessCallback$2(callback, lstat);
  22165. return;
  22166. }
  22167. settings.fs.stat(path, (statError, stat) => {
  22168. if (statError !== null) {
  22169. if (settings.throwErrorOnBrokenSymbolicLink) {
  22170. callFailureCallback$2(callback, statError);
  22171. return;
  22172. }
  22173. callSuccessCallback$2(callback, lstat);
  22174. return;
  22175. }
  22176. if (settings.markSymbolicLink) {
  22177. stat.isSymbolicLink = () => true;
  22178. }
  22179. callSuccessCallback$2(callback, stat);
  22180. });
  22181. });
  22182. }
  22183. async$2.read = read$3;
  22184. function callFailureCallback$2(callback, error) {
  22185. callback(error);
  22186. }
  22187. function callSuccessCallback$2(callback, result) {
  22188. callback(null, result);
  22189. }
  22190. var sync$8 = {};
  22191. Object.defineProperty(sync$8, "__esModule", { value: true });
  22192. sync$8.read = void 0;
  22193. function read$2(path, settings) {
  22194. const lstat = settings.fs.lstatSync(path);
  22195. if (!lstat.isSymbolicLink() || !settings.followSymbolicLink) {
  22196. return lstat;
  22197. }
  22198. try {
  22199. const stat = settings.fs.statSync(path);
  22200. if (settings.markSymbolicLink) {
  22201. stat.isSymbolicLink = () => true;
  22202. }
  22203. return stat;
  22204. }
  22205. catch (error) {
  22206. if (!settings.throwErrorOnBrokenSymbolicLink) {
  22207. return lstat;
  22208. }
  22209. throw error;
  22210. }
  22211. }
  22212. sync$8.read = read$2;
  22213. var settings$3 = {};
  22214. var fs$g = {};
  22215. (function (exports) {
  22216. Object.defineProperty(exports, "__esModule", { value: true });
  22217. exports.createFileSystemAdapter = exports.FILE_SYSTEM_ADAPTER = void 0;
  22218. const fs = require$$0__default;
  22219. exports.FILE_SYSTEM_ADAPTER = {
  22220. lstat: fs.lstat,
  22221. stat: fs.stat,
  22222. lstatSync: fs.lstatSync,
  22223. statSync: fs.statSync
  22224. };
  22225. function createFileSystemAdapter(fsMethods) {
  22226. if (fsMethods === undefined) {
  22227. return exports.FILE_SYSTEM_ADAPTER;
  22228. }
  22229. return Object.assign(Object.assign({}, exports.FILE_SYSTEM_ADAPTER), fsMethods);
  22230. }
  22231. exports.createFileSystemAdapter = createFileSystemAdapter;
  22232. } (fs$g));
  22233. Object.defineProperty(settings$3, "__esModule", { value: true });
  22234. const fs$f = fs$g;
  22235. let Settings$2 = class Settings {
  22236. constructor(_options = {}) {
  22237. this._options = _options;
  22238. this.followSymbolicLink = this._getValue(this._options.followSymbolicLink, true);
  22239. this.fs = fs$f.createFileSystemAdapter(this._options.fs);
  22240. this.markSymbolicLink = this._getValue(this._options.markSymbolicLink, false);
  22241. this.throwErrorOnBrokenSymbolicLink = this._getValue(this._options.throwErrorOnBrokenSymbolicLink, true);
  22242. }
  22243. _getValue(option, value) {
  22244. return option !== null && option !== void 0 ? option : value;
  22245. }
  22246. };
  22247. settings$3.default = Settings$2;
  22248. Object.defineProperty(out$1, "__esModule", { value: true });
  22249. out$1.statSync = out$1.stat = out$1.Settings = void 0;
  22250. const async$1 = async$2;
  22251. const sync$7 = sync$8;
  22252. const settings_1$3 = settings$3;
  22253. out$1.Settings = settings_1$3.default;
  22254. function stat$4(path, optionsOrSettingsOrCallback, callback) {
  22255. if (typeof optionsOrSettingsOrCallback === 'function') {
  22256. async$1.read(path, getSettings$2(), optionsOrSettingsOrCallback);
  22257. return;
  22258. }
  22259. async$1.read(path, getSettings$2(optionsOrSettingsOrCallback), callback);
  22260. }
  22261. out$1.stat = stat$4;
  22262. function statSync(path, optionsOrSettings) {
  22263. const settings = getSettings$2(optionsOrSettings);
  22264. return sync$7.read(path, settings);
  22265. }
  22266. out$1.statSync = statSync;
  22267. function getSettings$2(settingsOrOptions = {}) {
  22268. if (settingsOrOptions instanceof settings_1$3.default) {
  22269. return settingsOrOptions;
  22270. }
  22271. return new settings_1$3.default(settingsOrOptions);
  22272. }
  22273. /*! queue-microtask. MIT License. Feross Aboukhadijeh <https://feross.org/opensource> */
  22274. let promise;
  22275. var queueMicrotask_1 = typeof queueMicrotask === 'function'
  22276. ? queueMicrotask.bind(typeof window !== 'undefined' ? window : commonjsGlobal)
  22277. // reuse resolved promise, and allocate it lazily
  22278. : cb => (promise || (promise = Promise.resolve()))
  22279. .then(cb)
  22280. .catch(err => setTimeout(() => { throw err }, 0));
  22281. /*! run-parallel. MIT License. Feross Aboukhadijeh <https://feross.org/opensource> */
  22282. var runParallel_1 = runParallel;
  22283. const queueMicrotask$1 = queueMicrotask_1;
  22284. function runParallel (tasks, cb) {
  22285. let results, pending, keys;
  22286. let isSync = true;
  22287. if (Array.isArray(tasks)) {
  22288. results = [];
  22289. pending = tasks.length;
  22290. } else {
  22291. keys = Object.keys(tasks);
  22292. results = {};
  22293. pending = keys.length;
  22294. }
  22295. function done (err) {
  22296. function end () {
  22297. if (cb) cb(err, results);
  22298. cb = null;
  22299. }
  22300. if (isSync) queueMicrotask$1(end);
  22301. else end();
  22302. }
  22303. function each (i, err, result) {
  22304. results[i] = result;
  22305. if (--pending === 0 || err) {
  22306. done(err);
  22307. }
  22308. }
  22309. if (!pending) {
  22310. // empty
  22311. done(null);
  22312. } else if (keys) {
  22313. // object
  22314. keys.forEach(function (key) {
  22315. tasks[key](function (err, result) { each(key, err, result); });
  22316. });
  22317. } else {
  22318. // array
  22319. tasks.forEach(function (task, i) {
  22320. task(function (err, result) { each(i, err, result); });
  22321. });
  22322. }
  22323. isSync = false;
  22324. }
  22325. var constants$2 = {};
  22326. Object.defineProperty(constants$2, "__esModule", { value: true });
  22327. constants$2.IS_SUPPORT_READDIR_WITH_FILE_TYPES = void 0;
  22328. const NODE_PROCESS_VERSION_PARTS = process.versions.node.split('.');
  22329. if (NODE_PROCESS_VERSION_PARTS[0] === undefined || NODE_PROCESS_VERSION_PARTS[1] === undefined) {
  22330. throw new Error(`Unexpected behavior. The 'process.versions.node' variable has invalid value: ${process.versions.node}`);
  22331. }
  22332. const MAJOR_VERSION = Number.parseInt(NODE_PROCESS_VERSION_PARTS[0], 10);
  22333. const MINOR_VERSION = Number.parseInt(NODE_PROCESS_VERSION_PARTS[1], 10);
  22334. const SUPPORTED_MAJOR_VERSION = 10;
  22335. const SUPPORTED_MINOR_VERSION = 10;
  22336. const IS_MATCHED_BY_MAJOR = MAJOR_VERSION > SUPPORTED_MAJOR_VERSION;
  22337. const IS_MATCHED_BY_MAJOR_AND_MINOR = MAJOR_VERSION === SUPPORTED_MAJOR_VERSION && MINOR_VERSION >= SUPPORTED_MINOR_VERSION;
  22338. /**
  22339. * IS `true` for Node.js 10.10 and greater.
  22340. */
  22341. constants$2.IS_SUPPORT_READDIR_WITH_FILE_TYPES = IS_MATCHED_BY_MAJOR || IS_MATCHED_BY_MAJOR_AND_MINOR;
  22342. var utils$9 = {};
  22343. var fs$e = {};
  22344. Object.defineProperty(fs$e, "__esModule", { value: true });
  22345. fs$e.createDirentFromStats = void 0;
  22346. class DirentFromStats {
  22347. constructor(name, stats) {
  22348. this.name = name;
  22349. this.isBlockDevice = stats.isBlockDevice.bind(stats);
  22350. this.isCharacterDevice = stats.isCharacterDevice.bind(stats);
  22351. this.isDirectory = stats.isDirectory.bind(stats);
  22352. this.isFIFO = stats.isFIFO.bind(stats);
  22353. this.isFile = stats.isFile.bind(stats);
  22354. this.isSocket = stats.isSocket.bind(stats);
  22355. this.isSymbolicLink = stats.isSymbolicLink.bind(stats);
  22356. }
  22357. }
  22358. function createDirentFromStats(name, stats) {
  22359. return new DirentFromStats(name, stats);
  22360. }
  22361. fs$e.createDirentFromStats = createDirentFromStats;
  22362. Object.defineProperty(utils$9, "__esModule", { value: true });
  22363. utils$9.fs = void 0;
  22364. const fs$d = fs$e;
  22365. utils$9.fs = fs$d;
  22366. var common$a = {};
  22367. Object.defineProperty(common$a, "__esModule", { value: true });
  22368. common$a.joinPathSegments = void 0;
  22369. function joinPathSegments$1(a, b, separator) {
  22370. /**
  22371. * The correct handling of cases when the first segment is a root (`/`, `C:/`) or UNC path (`//?/C:/`).
  22372. */
  22373. if (a.endsWith(separator)) {
  22374. return a + b;
  22375. }
  22376. return a + separator + b;
  22377. }
  22378. common$a.joinPathSegments = joinPathSegments$1;
  22379. Object.defineProperty(async$3, "__esModule", { value: true });
  22380. async$3.readdir = async$3.readdirWithFileTypes = async$3.read = void 0;
  22381. const fsStat$5 = out$1;
  22382. const rpl = runParallel_1;
  22383. const constants_1$1 = constants$2;
  22384. const utils$8 = utils$9;
  22385. const common$9 = common$a;
  22386. function read$1(directory, settings, callback) {
  22387. if (!settings.stats && constants_1$1.IS_SUPPORT_READDIR_WITH_FILE_TYPES) {
  22388. readdirWithFileTypes$1(directory, settings, callback);
  22389. return;
  22390. }
  22391. readdir$3(directory, settings, callback);
  22392. }
  22393. async$3.read = read$1;
  22394. function readdirWithFileTypes$1(directory, settings, callback) {
  22395. settings.fs.readdir(directory, { withFileTypes: true }, (readdirError, dirents) => {
  22396. if (readdirError !== null) {
  22397. callFailureCallback$1(callback, readdirError);
  22398. return;
  22399. }
  22400. const entries = dirents.map((dirent) => ({
  22401. dirent,
  22402. name: dirent.name,
  22403. path: common$9.joinPathSegments(directory, dirent.name, settings.pathSegmentSeparator)
  22404. }));
  22405. if (!settings.followSymbolicLinks) {
  22406. callSuccessCallback$1(callback, entries);
  22407. return;
  22408. }
  22409. const tasks = entries.map((entry) => makeRplTaskEntry(entry, settings));
  22410. rpl(tasks, (rplError, rplEntries) => {
  22411. if (rplError !== null) {
  22412. callFailureCallback$1(callback, rplError);
  22413. return;
  22414. }
  22415. callSuccessCallback$1(callback, rplEntries);
  22416. });
  22417. });
  22418. }
  22419. async$3.readdirWithFileTypes = readdirWithFileTypes$1;
  22420. function makeRplTaskEntry(entry, settings) {
  22421. return (done) => {
  22422. if (!entry.dirent.isSymbolicLink()) {
  22423. done(null, entry);
  22424. return;
  22425. }
  22426. settings.fs.stat(entry.path, (statError, stats) => {
  22427. if (statError !== null) {
  22428. if (settings.throwErrorOnBrokenSymbolicLink) {
  22429. done(statError);
  22430. return;
  22431. }
  22432. done(null, entry);
  22433. return;
  22434. }
  22435. entry.dirent = utils$8.fs.createDirentFromStats(entry.name, stats);
  22436. done(null, entry);
  22437. });
  22438. };
  22439. }
  22440. function readdir$3(directory, settings, callback) {
  22441. settings.fs.readdir(directory, (readdirError, names) => {
  22442. if (readdirError !== null) {
  22443. callFailureCallback$1(callback, readdirError);
  22444. return;
  22445. }
  22446. const tasks = names.map((name) => {
  22447. const path = common$9.joinPathSegments(directory, name, settings.pathSegmentSeparator);
  22448. return (done) => {
  22449. fsStat$5.stat(path, settings.fsStatSettings, (error, stats) => {
  22450. if (error !== null) {
  22451. done(error);
  22452. return;
  22453. }
  22454. const entry = {
  22455. name,
  22456. path,
  22457. dirent: utils$8.fs.createDirentFromStats(name, stats)
  22458. };
  22459. if (settings.stats) {
  22460. entry.stats = stats;
  22461. }
  22462. done(null, entry);
  22463. });
  22464. };
  22465. });
  22466. rpl(tasks, (rplError, entries) => {
  22467. if (rplError !== null) {
  22468. callFailureCallback$1(callback, rplError);
  22469. return;
  22470. }
  22471. callSuccessCallback$1(callback, entries);
  22472. });
  22473. });
  22474. }
  22475. async$3.readdir = readdir$3;
  22476. function callFailureCallback$1(callback, error) {
  22477. callback(error);
  22478. }
  22479. function callSuccessCallback$1(callback, result) {
  22480. callback(null, result);
  22481. }
  22482. var sync$6 = {};
  22483. Object.defineProperty(sync$6, "__esModule", { value: true });
  22484. sync$6.readdir = sync$6.readdirWithFileTypes = sync$6.read = void 0;
  22485. const fsStat$4 = out$1;
  22486. const constants_1 = constants$2;
  22487. const utils$7 = utils$9;
  22488. const common$8 = common$a;
  22489. function read(directory, settings) {
  22490. if (!settings.stats && constants_1.IS_SUPPORT_READDIR_WITH_FILE_TYPES) {
  22491. return readdirWithFileTypes(directory, settings);
  22492. }
  22493. return readdir$2(directory, settings);
  22494. }
  22495. sync$6.read = read;
  22496. function readdirWithFileTypes(directory, settings) {
  22497. const dirents = settings.fs.readdirSync(directory, { withFileTypes: true });
  22498. return dirents.map((dirent) => {
  22499. const entry = {
  22500. dirent,
  22501. name: dirent.name,
  22502. path: common$8.joinPathSegments(directory, dirent.name, settings.pathSegmentSeparator)
  22503. };
  22504. if (entry.dirent.isSymbolicLink() && settings.followSymbolicLinks) {
  22505. try {
  22506. const stats = settings.fs.statSync(entry.path);
  22507. entry.dirent = utils$7.fs.createDirentFromStats(entry.name, stats);
  22508. }
  22509. catch (error) {
  22510. if (settings.throwErrorOnBrokenSymbolicLink) {
  22511. throw error;
  22512. }
  22513. }
  22514. }
  22515. return entry;
  22516. });
  22517. }
  22518. sync$6.readdirWithFileTypes = readdirWithFileTypes;
  22519. function readdir$2(directory, settings) {
  22520. const names = settings.fs.readdirSync(directory);
  22521. return names.map((name) => {
  22522. const entryPath = common$8.joinPathSegments(directory, name, settings.pathSegmentSeparator);
  22523. const stats = fsStat$4.statSync(entryPath, settings.fsStatSettings);
  22524. const entry = {
  22525. name,
  22526. path: entryPath,
  22527. dirent: utils$7.fs.createDirentFromStats(name, stats)
  22528. };
  22529. if (settings.stats) {
  22530. entry.stats = stats;
  22531. }
  22532. return entry;
  22533. });
  22534. }
  22535. sync$6.readdir = readdir$2;
  22536. var settings$2 = {};
  22537. var fs$c = {};
  22538. (function (exports) {
  22539. Object.defineProperty(exports, "__esModule", { value: true });
  22540. exports.createFileSystemAdapter = exports.FILE_SYSTEM_ADAPTER = void 0;
  22541. const fs = require$$0__default;
  22542. exports.FILE_SYSTEM_ADAPTER = {
  22543. lstat: fs.lstat,
  22544. stat: fs.stat,
  22545. lstatSync: fs.lstatSync,
  22546. statSync: fs.statSync,
  22547. readdir: fs.readdir,
  22548. readdirSync: fs.readdirSync
  22549. };
  22550. function createFileSystemAdapter(fsMethods) {
  22551. if (fsMethods === undefined) {
  22552. return exports.FILE_SYSTEM_ADAPTER;
  22553. }
  22554. return Object.assign(Object.assign({}, exports.FILE_SYSTEM_ADAPTER), fsMethods);
  22555. }
  22556. exports.createFileSystemAdapter = createFileSystemAdapter;
  22557. } (fs$c));
  22558. Object.defineProperty(settings$2, "__esModule", { value: true });
  22559. const path$e = require$$0$4;
  22560. const fsStat$3 = out$1;
  22561. const fs$b = fs$c;
  22562. let Settings$1 = class Settings {
  22563. constructor(_options = {}) {
  22564. this._options = _options;
  22565. this.followSymbolicLinks = this._getValue(this._options.followSymbolicLinks, false);
  22566. this.fs = fs$b.createFileSystemAdapter(this._options.fs);
  22567. this.pathSegmentSeparator = this._getValue(this._options.pathSegmentSeparator, path$e.sep);
  22568. this.stats = this._getValue(this._options.stats, false);
  22569. this.throwErrorOnBrokenSymbolicLink = this._getValue(this._options.throwErrorOnBrokenSymbolicLink, true);
  22570. this.fsStatSettings = new fsStat$3.Settings({
  22571. followSymbolicLink: this.followSymbolicLinks,
  22572. fs: this.fs,
  22573. throwErrorOnBrokenSymbolicLink: this.throwErrorOnBrokenSymbolicLink
  22574. });
  22575. }
  22576. _getValue(option, value) {
  22577. return option !== null && option !== void 0 ? option : value;
  22578. }
  22579. };
  22580. settings$2.default = Settings$1;
  22581. Object.defineProperty(out$2, "__esModule", { value: true });
  22582. out$2.Settings = out$2.scandirSync = out$2.scandir = void 0;
  22583. const async = async$3;
  22584. const sync$5 = sync$6;
  22585. const settings_1$2 = settings$2;
  22586. out$2.Settings = settings_1$2.default;
  22587. function scandir(path, optionsOrSettingsOrCallback, callback) {
  22588. if (typeof optionsOrSettingsOrCallback === 'function') {
  22589. async.read(path, getSettings$1(), optionsOrSettingsOrCallback);
  22590. return;
  22591. }
  22592. async.read(path, getSettings$1(optionsOrSettingsOrCallback), callback);
  22593. }
  22594. out$2.scandir = scandir;
  22595. function scandirSync(path, optionsOrSettings) {
  22596. const settings = getSettings$1(optionsOrSettings);
  22597. return sync$5.read(path, settings);
  22598. }
  22599. out$2.scandirSync = scandirSync;
  22600. function getSettings$1(settingsOrOptions = {}) {
  22601. if (settingsOrOptions instanceof settings_1$2.default) {
  22602. return settingsOrOptions;
  22603. }
  22604. return new settings_1$2.default(settingsOrOptions);
  22605. }
  22606. var queue = {exports: {}};
  22607. function reusify$1 (Constructor) {
  22608. var head = new Constructor();
  22609. var tail = head;
  22610. function get () {
  22611. var current = head;
  22612. if (current.next) {
  22613. head = current.next;
  22614. } else {
  22615. head = new Constructor();
  22616. tail = head;
  22617. }
  22618. current.next = null;
  22619. return current
  22620. }
  22621. function release (obj) {
  22622. tail.next = obj;
  22623. tail = obj;
  22624. }
  22625. return {
  22626. get: get,
  22627. release: release
  22628. }
  22629. }
  22630. var reusify_1 = reusify$1;
  22631. /* eslint-disable no-var */
  22632. var reusify = reusify_1;
  22633. function fastqueue (context, worker, _concurrency) {
  22634. if (typeof context === 'function') {
  22635. _concurrency = worker;
  22636. worker = context;
  22637. context = null;
  22638. }
  22639. if (!(_concurrency >= 1)) {
  22640. throw new Error('fastqueue concurrency must be equal to or greater than 1')
  22641. }
  22642. var cache = reusify(Task);
  22643. var queueHead = null;
  22644. var queueTail = null;
  22645. var _running = 0;
  22646. var errorHandler = null;
  22647. var self = {
  22648. push: push,
  22649. drain: noop$4,
  22650. saturated: noop$4,
  22651. pause: pause,
  22652. paused: false,
  22653. get concurrency () {
  22654. return _concurrency
  22655. },
  22656. set concurrency (value) {
  22657. if (!(value >= 1)) {
  22658. throw new Error('fastqueue concurrency must be equal to or greater than 1')
  22659. }
  22660. _concurrency = value;
  22661. if (self.paused) return
  22662. for (; queueHead && _running < _concurrency;) {
  22663. _running++;
  22664. release();
  22665. }
  22666. },
  22667. running: running,
  22668. resume: resume,
  22669. idle: idle,
  22670. length: length,
  22671. getQueue: getQueue,
  22672. unshift: unshift,
  22673. empty: noop$4,
  22674. kill: kill,
  22675. killAndDrain: killAndDrain,
  22676. error: error
  22677. };
  22678. return self
  22679. function running () {
  22680. return _running
  22681. }
  22682. function pause () {
  22683. self.paused = true;
  22684. }
  22685. function length () {
  22686. var current = queueHead;
  22687. var counter = 0;
  22688. while (current) {
  22689. current = current.next;
  22690. counter++;
  22691. }
  22692. return counter
  22693. }
  22694. function getQueue () {
  22695. var current = queueHead;
  22696. var tasks = [];
  22697. while (current) {
  22698. tasks.push(current.value);
  22699. current = current.next;
  22700. }
  22701. return tasks
  22702. }
  22703. function resume () {
  22704. if (!self.paused) return
  22705. self.paused = false;
  22706. if (queueHead === null) {
  22707. _running++;
  22708. release();
  22709. return
  22710. }
  22711. for (; queueHead && _running < _concurrency;) {
  22712. _running++;
  22713. release();
  22714. }
  22715. }
  22716. function idle () {
  22717. return _running === 0 && self.length() === 0
  22718. }
  22719. function push (value, done) {
  22720. var current = cache.get();
  22721. current.context = context;
  22722. current.release = release;
  22723. current.value = value;
  22724. current.callback = done || noop$4;
  22725. current.errorHandler = errorHandler;
  22726. if (_running >= _concurrency || self.paused) {
  22727. if (queueTail) {
  22728. queueTail.next = current;
  22729. queueTail = current;
  22730. } else {
  22731. queueHead = current;
  22732. queueTail = current;
  22733. self.saturated();
  22734. }
  22735. } else {
  22736. _running++;
  22737. worker.call(context, current.value, current.worked);
  22738. }
  22739. }
  22740. function unshift (value, done) {
  22741. var current = cache.get();
  22742. current.context = context;
  22743. current.release = release;
  22744. current.value = value;
  22745. current.callback = done || noop$4;
  22746. current.errorHandler = errorHandler;
  22747. if (_running >= _concurrency || self.paused) {
  22748. if (queueHead) {
  22749. current.next = queueHead;
  22750. queueHead = current;
  22751. } else {
  22752. queueHead = current;
  22753. queueTail = current;
  22754. self.saturated();
  22755. }
  22756. } else {
  22757. _running++;
  22758. worker.call(context, current.value, current.worked);
  22759. }
  22760. }
  22761. function release (holder) {
  22762. if (holder) {
  22763. cache.release(holder);
  22764. }
  22765. var next = queueHead;
  22766. if (next && _running <= _concurrency) {
  22767. if (!self.paused) {
  22768. if (queueTail === queueHead) {
  22769. queueTail = null;
  22770. }
  22771. queueHead = next.next;
  22772. next.next = null;
  22773. worker.call(context, next.value, next.worked);
  22774. if (queueTail === null) {
  22775. self.empty();
  22776. }
  22777. } else {
  22778. _running--;
  22779. }
  22780. } else if (--_running === 0) {
  22781. self.drain();
  22782. }
  22783. }
  22784. function kill () {
  22785. queueHead = null;
  22786. queueTail = null;
  22787. self.drain = noop$4;
  22788. }
  22789. function killAndDrain () {
  22790. queueHead = null;
  22791. queueTail = null;
  22792. self.drain();
  22793. self.drain = noop$4;
  22794. }
  22795. function error (handler) {
  22796. errorHandler = handler;
  22797. }
  22798. }
  22799. function noop$4 () {}
  22800. function Task () {
  22801. this.value = null;
  22802. this.callback = noop$4;
  22803. this.next = null;
  22804. this.release = noop$4;
  22805. this.context = null;
  22806. this.errorHandler = null;
  22807. var self = this;
  22808. this.worked = function worked (err, result) {
  22809. var callback = self.callback;
  22810. var errorHandler = self.errorHandler;
  22811. var val = self.value;
  22812. self.value = null;
  22813. self.callback = noop$4;
  22814. if (self.errorHandler) {
  22815. errorHandler(err, val);
  22816. }
  22817. callback.call(self.context, err, result);
  22818. self.release(self);
  22819. };
  22820. }
  22821. function queueAsPromised (context, worker, _concurrency) {
  22822. if (typeof context === 'function') {
  22823. _concurrency = worker;
  22824. worker = context;
  22825. context = null;
  22826. }
  22827. function asyncWrapper (arg, cb) {
  22828. worker.call(this, arg)
  22829. .then(function (res) {
  22830. cb(null, res);
  22831. }, cb);
  22832. }
  22833. var queue = fastqueue(context, asyncWrapper, _concurrency);
  22834. var pushCb = queue.push;
  22835. var unshiftCb = queue.unshift;
  22836. queue.push = push;
  22837. queue.unshift = unshift;
  22838. queue.drained = drained;
  22839. return queue
  22840. function push (value) {
  22841. var p = new Promise(function (resolve, reject) {
  22842. pushCb(value, function (err, result) {
  22843. if (err) {
  22844. reject(err);
  22845. return
  22846. }
  22847. resolve(result);
  22848. });
  22849. });
  22850. // Let's fork the promise chain to
  22851. // make the error bubble up to the user but
  22852. // not lead to a unhandledRejection
  22853. p.catch(noop$4);
  22854. return p
  22855. }
  22856. function unshift (value) {
  22857. var p = new Promise(function (resolve, reject) {
  22858. unshiftCb(value, function (err, result) {
  22859. if (err) {
  22860. reject(err);
  22861. return
  22862. }
  22863. resolve(result);
  22864. });
  22865. });
  22866. // Let's fork the promise chain to
  22867. // make the error bubble up to the user but
  22868. // not lead to a unhandledRejection
  22869. p.catch(noop$4);
  22870. return p
  22871. }
  22872. function drained () {
  22873. if (queue.idle()) {
  22874. return new Promise(function (resolve) {
  22875. resolve();
  22876. })
  22877. }
  22878. var previousDrain = queue.drain;
  22879. var p = new Promise(function (resolve) {
  22880. queue.drain = function () {
  22881. previousDrain();
  22882. resolve();
  22883. };
  22884. });
  22885. return p
  22886. }
  22887. }
  22888. queue.exports = fastqueue;
  22889. queue.exports.promise = queueAsPromised;
  22890. var queueExports = queue.exports;
  22891. var common$7 = {};
  22892. Object.defineProperty(common$7, "__esModule", { value: true });
  22893. common$7.joinPathSegments = common$7.replacePathSegmentSeparator = common$7.isAppliedFilter = common$7.isFatalError = void 0;
  22894. function isFatalError(settings, error) {
  22895. if (settings.errorFilter === null) {
  22896. return true;
  22897. }
  22898. return !settings.errorFilter(error);
  22899. }
  22900. common$7.isFatalError = isFatalError;
  22901. function isAppliedFilter(filter, value) {
  22902. return filter === null || filter(value);
  22903. }
  22904. common$7.isAppliedFilter = isAppliedFilter;
  22905. function replacePathSegmentSeparator(filepath, separator) {
  22906. return filepath.split(/[/\\]/).join(separator);
  22907. }
  22908. common$7.replacePathSegmentSeparator = replacePathSegmentSeparator;
  22909. function joinPathSegments(a, b, separator) {
  22910. if (a === '') {
  22911. return b;
  22912. }
  22913. /**
  22914. * The correct handling of cases when the first segment is a root (`/`, `C:/`) or UNC path (`//?/C:/`).
  22915. */
  22916. if (a.endsWith(separator)) {
  22917. return a + b;
  22918. }
  22919. return a + separator + b;
  22920. }
  22921. common$7.joinPathSegments = joinPathSegments;
  22922. var reader$1 = {};
  22923. Object.defineProperty(reader$1, "__esModule", { value: true });
  22924. const common$6 = common$7;
  22925. let Reader$1 = class Reader {
  22926. constructor(_root, _settings) {
  22927. this._root = _root;
  22928. this._settings = _settings;
  22929. this._root = common$6.replacePathSegmentSeparator(_root, _settings.pathSegmentSeparator);
  22930. }
  22931. };
  22932. reader$1.default = Reader$1;
  22933. Object.defineProperty(async$4, "__esModule", { value: true });
  22934. const events_1 = require$$0$7;
  22935. const fsScandir$2 = out$2;
  22936. const fastq = queueExports;
  22937. const common$5 = common$7;
  22938. const reader_1$4 = reader$1;
  22939. class AsyncReader extends reader_1$4.default {
  22940. constructor(_root, _settings) {
  22941. super(_root, _settings);
  22942. this._settings = _settings;
  22943. this._scandir = fsScandir$2.scandir;
  22944. this._emitter = new events_1.EventEmitter();
  22945. this._queue = fastq(this._worker.bind(this), this._settings.concurrency);
  22946. this._isFatalError = false;
  22947. this._isDestroyed = false;
  22948. this._queue.drain = () => {
  22949. if (!this._isFatalError) {
  22950. this._emitter.emit('end');
  22951. }
  22952. };
  22953. }
  22954. read() {
  22955. this._isFatalError = false;
  22956. this._isDestroyed = false;
  22957. setImmediate(() => {
  22958. this._pushToQueue(this._root, this._settings.basePath);
  22959. });
  22960. return this._emitter;
  22961. }
  22962. get isDestroyed() {
  22963. return this._isDestroyed;
  22964. }
  22965. destroy() {
  22966. if (this._isDestroyed) {
  22967. throw new Error('The reader is already destroyed');
  22968. }
  22969. this._isDestroyed = true;
  22970. this._queue.killAndDrain();
  22971. }
  22972. onEntry(callback) {
  22973. this._emitter.on('entry', callback);
  22974. }
  22975. onError(callback) {
  22976. this._emitter.once('error', callback);
  22977. }
  22978. onEnd(callback) {
  22979. this._emitter.once('end', callback);
  22980. }
  22981. _pushToQueue(directory, base) {
  22982. const queueItem = { directory, base };
  22983. this._queue.push(queueItem, (error) => {
  22984. if (error !== null) {
  22985. this._handleError(error);
  22986. }
  22987. });
  22988. }
  22989. _worker(item, done) {
  22990. this._scandir(item.directory, this._settings.fsScandirSettings, (error, entries) => {
  22991. if (error !== null) {
  22992. done(error, undefined);
  22993. return;
  22994. }
  22995. for (const entry of entries) {
  22996. this._handleEntry(entry, item.base);
  22997. }
  22998. done(null, undefined);
  22999. });
  23000. }
  23001. _handleError(error) {
  23002. if (this._isDestroyed || !common$5.isFatalError(this._settings, error)) {
  23003. return;
  23004. }
  23005. this._isFatalError = true;
  23006. this._isDestroyed = true;
  23007. this._emitter.emit('error', error);
  23008. }
  23009. _handleEntry(entry, base) {
  23010. if (this._isDestroyed || this._isFatalError) {
  23011. return;
  23012. }
  23013. const fullpath = entry.path;
  23014. if (base !== undefined) {
  23015. entry.path = common$5.joinPathSegments(base, entry.name, this._settings.pathSegmentSeparator);
  23016. }
  23017. if (common$5.isAppliedFilter(this._settings.entryFilter, entry)) {
  23018. this._emitEntry(entry);
  23019. }
  23020. if (entry.dirent.isDirectory() && common$5.isAppliedFilter(this._settings.deepFilter, entry)) {
  23021. this._pushToQueue(fullpath, base === undefined ? undefined : entry.path);
  23022. }
  23023. }
  23024. _emitEntry(entry) {
  23025. this._emitter.emit('entry', entry);
  23026. }
  23027. }
  23028. async$4.default = AsyncReader;
  23029. Object.defineProperty(async$5, "__esModule", { value: true });
  23030. const async_1$4 = async$4;
  23031. class AsyncProvider {
  23032. constructor(_root, _settings) {
  23033. this._root = _root;
  23034. this._settings = _settings;
  23035. this._reader = new async_1$4.default(this._root, this._settings);
  23036. this._storage = [];
  23037. }
  23038. read(callback) {
  23039. this._reader.onError((error) => {
  23040. callFailureCallback(callback, error);
  23041. });
  23042. this._reader.onEntry((entry) => {
  23043. this._storage.push(entry);
  23044. });
  23045. this._reader.onEnd(() => {
  23046. callSuccessCallback(callback, this._storage);
  23047. });
  23048. this._reader.read();
  23049. }
  23050. }
  23051. async$5.default = AsyncProvider;
  23052. function callFailureCallback(callback, error) {
  23053. callback(error);
  23054. }
  23055. function callSuccessCallback(callback, entries) {
  23056. callback(null, entries);
  23057. }
  23058. var stream$2 = {};
  23059. Object.defineProperty(stream$2, "__esModule", { value: true });
  23060. const stream_1$5 = require$$0$6;
  23061. const async_1$3 = async$4;
  23062. class StreamProvider {
  23063. constructor(_root, _settings) {
  23064. this._root = _root;
  23065. this._settings = _settings;
  23066. this._reader = new async_1$3.default(this._root, this._settings);
  23067. this._stream = new stream_1$5.Readable({
  23068. objectMode: true,
  23069. read: () => { },
  23070. destroy: () => {
  23071. if (!this._reader.isDestroyed) {
  23072. this._reader.destroy();
  23073. }
  23074. }
  23075. });
  23076. }
  23077. read() {
  23078. this._reader.onError((error) => {
  23079. this._stream.emit('error', error);
  23080. });
  23081. this._reader.onEntry((entry) => {
  23082. this._stream.push(entry);
  23083. });
  23084. this._reader.onEnd(() => {
  23085. this._stream.push(null);
  23086. });
  23087. this._reader.read();
  23088. return this._stream;
  23089. }
  23090. }
  23091. stream$2.default = StreamProvider;
  23092. var sync$4 = {};
  23093. var sync$3 = {};
  23094. Object.defineProperty(sync$3, "__esModule", { value: true });
  23095. const fsScandir$1 = out$2;
  23096. const common$4 = common$7;
  23097. const reader_1$3 = reader$1;
  23098. class SyncReader extends reader_1$3.default {
  23099. constructor() {
  23100. super(...arguments);
  23101. this._scandir = fsScandir$1.scandirSync;
  23102. this._storage = [];
  23103. this._queue = new Set();
  23104. }
  23105. read() {
  23106. this._pushToQueue(this._root, this._settings.basePath);
  23107. this._handleQueue();
  23108. return this._storage;
  23109. }
  23110. _pushToQueue(directory, base) {
  23111. this._queue.add({ directory, base });
  23112. }
  23113. _handleQueue() {
  23114. for (const item of this._queue.values()) {
  23115. this._handleDirectory(item.directory, item.base);
  23116. }
  23117. }
  23118. _handleDirectory(directory, base) {
  23119. try {
  23120. const entries = this._scandir(directory, this._settings.fsScandirSettings);
  23121. for (const entry of entries) {
  23122. this._handleEntry(entry, base);
  23123. }
  23124. }
  23125. catch (error) {
  23126. this._handleError(error);
  23127. }
  23128. }
  23129. _handleError(error) {
  23130. if (!common$4.isFatalError(this._settings, error)) {
  23131. return;
  23132. }
  23133. throw error;
  23134. }
  23135. _handleEntry(entry, base) {
  23136. const fullpath = entry.path;
  23137. if (base !== undefined) {
  23138. entry.path = common$4.joinPathSegments(base, entry.name, this._settings.pathSegmentSeparator);
  23139. }
  23140. if (common$4.isAppliedFilter(this._settings.entryFilter, entry)) {
  23141. this._pushToStorage(entry);
  23142. }
  23143. if (entry.dirent.isDirectory() && common$4.isAppliedFilter(this._settings.deepFilter, entry)) {
  23144. this._pushToQueue(fullpath, base === undefined ? undefined : entry.path);
  23145. }
  23146. }
  23147. _pushToStorage(entry) {
  23148. this._storage.push(entry);
  23149. }
  23150. }
  23151. sync$3.default = SyncReader;
  23152. Object.defineProperty(sync$4, "__esModule", { value: true });
  23153. const sync_1$3 = sync$3;
  23154. class SyncProvider {
  23155. constructor(_root, _settings) {
  23156. this._root = _root;
  23157. this._settings = _settings;
  23158. this._reader = new sync_1$3.default(this._root, this._settings);
  23159. }
  23160. read() {
  23161. return this._reader.read();
  23162. }
  23163. }
  23164. sync$4.default = SyncProvider;
  23165. var settings$1 = {};
  23166. Object.defineProperty(settings$1, "__esModule", { value: true });
  23167. const path$d = require$$0$4;
  23168. const fsScandir = out$2;
  23169. class Settings {
  23170. constructor(_options = {}) {
  23171. this._options = _options;
  23172. this.basePath = this._getValue(this._options.basePath, undefined);
  23173. this.concurrency = this._getValue(this._options.concurrency, Number.POSITIVE_INFINITY);
  23174. this.deepFilter = this._getValue(this._options.deepFilter, null);
  23175. this.entryFilter = this._getValue(this._options.entryFilter, null);
  23176. this.errorFilter = this._getValue(this._options.errorFilter, null);
  23177. this.pathSegmentSeparator = this._getValue(this._options.pathSegmentSeparator, path$d.sep);
  23178. this.fsScandirSettings = new fsScandir.Settings({
  23179. followSymbolicLinks: this._options.followSymbolicLinks,
  23180. fs: this._options.fs,
  23181. pathSegmentSeparator: this._options.pathSegmentSeparator,
  23182. stats: this._options.stats,
  23183. throwErrorOnBrokenSymbolicLink: this._options.throwErrorOnBrokenSymbolicLink
  23184. });
  23185. }
  23186. _getValue(option, value) {
  23187. return option !== null && option !== void 0 ? option : value;
  23188. }
  23189. }
  23190. settings$1.default = Settings;
  23191. Object.defineProperty(out$3, "__esModule", { value: true });
  23192. out$3.Settings = out$3.walkStream = out$3.walkSync = out$3.walk = void 0;
  23193. const async_1$2 = async$5;
  23194. const stream_1$4 = stream$2;
  23195. const sync_1$2 = sync$4;
  23196. const settings_1$1 = settings$1;
  23197. out$3.Settings = settings_1$1.default;
  23198. function walk$2(directory, optionsOrSettingsOrCallback, callback) {
  23199. if (typeof optionsOrSettingsOrCallback === 'function') {
  23200. new async_1$2.default(directory, getSettings()).read(optionsOrSettingsOrCallback);
  23201. return;
  23202. }
  23203. new async_1$2.default(directory, getSettings(optionsOrSettingsOrCallback)).read(callback);
  23204. }
  23205. out$3.walk = walk$2;
  23206. function walkSync(directory, optionsOrSettings) {
  23207. const settings = getSettings(optionsOrSettings);
  23208. const provider = new sync_1$2.default(directory, settings);
  23209. return provider.read();
  23210. }
  23211. out$3.walkSync = walkSync;
  23212. function walkStream(directory, optionsOrSettings) {
  23213. const settings = getSettings(optionsOrSettings);
  23214. const provider = new stream_1$4.default(directory, settings);
  23215. return provider.read();
  23216. }
  23217. out$3.walkStream = walkStream;
  23218. function getSettings(settingsOrOptions = {}) {
  23219. if (settingsOrOptions instanceof settings_1$1.default) {
  23220. return settingsOrOptions;
  23221. }
  23222. return new settings_1$1.default(settingsOrOptions);
  23223. }
  23224. var reader = {};
  23225. Object.defineProperty(reader, "__esModule", { value: true });
  23226. const path$c = require$$0$4;
  23227. const fsStat$2 = out$1;
  23228. const utils$6 = utils$g;
  23229. class Reader {
  23230. constructor(_settings) {
  23231. this._settings = _settings;
  23232. this._fsStatSettings = new fsStat$2.Settings({
  23233. followSymbolicLink: this._settings.followSymbolicLinks,
  23234. fs: this._settings.fs,
  23235. throwErrorOnBrokenSymbolicLink: this._settings.followSymbolicLinks
  23236. });
  23237. }
  23238. _getFullEntryPath(filepath) {
  23239. return path$c.resolve(this._settings.cwd, filepath);
  23240. }
  23241. _makeEntry(stats, pattern) {
  23242. const entry = {
  23243. name: pattern,
  23244. path: pattern,
  23245. dirent: utils$6.fs.createDirentFromStats(pattern, stats)
  23246. };
  23247. if (this._settings.stats) {
  23248. entry.stats = stats;
  23249. }
  23250. return entry;
  23251. }
  23252. _isFatalError(error) {
  23253. return !utils$6.errno.isEnoentCodeError(error) && !this._settings.suppressErrors;
  23254. }
  23255. }
  23256. reader.default = Reader;
  23257. var stream$1 = {};
  23258. Object.defineProperty(stream$1, "__esModule", { value: true });
  23259. const stream_1$3 = require$$0$6;
  23260. const fsStat$1 = out$1;
  23261. const fsWalk$2 = out$3;
  23262. const reader_1$2 = reader;
  23263. class ReaderStream extends reader_1$2.default {
  23264. constructor() {
  23265. super(...arguments);
  23266. this._walkStream = fsWalk$2.walkStream;
  23267. this._stat = fsStat$1.stat;
  23268. }
  23269. dynamic(root, options) {
  23270. return this._walkStream(root, options);
  23271. }
  23272. static(patterns, options) {
  23273. const filepaths = patterns.map(this._getFullEntryPath, this);
  23274. const stream = new stream_1$3.PassThrough({ objectMode: true });
  23275. stream._write = (index, _enc, done) => {
  23276. return this._getEntry(filepaths[index], patterns[index], options)
  23277. .then((entry) => {
  23278. if (entry !== null && options.entryFilter(entry)) {
  23279. stream.push(entry);
  23280. }
  23281. if (index === filepaths.length - 1) {
  23282. stream.end();
  23283. }
  23284. done();
  23285. })
  23286. .catch(done);
  23287. };
  23288. for (let i = 0; i < filepaths.length; i++) {
  23289. stream.write(i);
  23290. }
  23291. return stream;
  23292. }
  23293. _getEntry(filepath, pattern, options) {
  23294. return this._getStat(filepath)
  23295. .then((stats) => this._makeEntry(stats, pattern))
  23296. .catch((error) => {
  23297. if (options.errorFilter(error)) {
  23298. return null;
  23299. }
  23300. throw error;
  23301. });
  23302. }
  23303. _getStat(filepath) {
  23304. return new Promise((resolve, reject) => {
  23305. this._stat(filepath, this._fsStatSettings, (error, stats) => {
  23306. return error === null ? resolve(stats) : reject(error);
  23307. });
  23308. });
  23309. }
  23310. }
  23311. stream$1.default = ReaderStream;
  23312. Object.defineProperty(async$6, "__esModule", { value: true });
  23313. const fsWalk$1 = out$3;
  23314. const reader_1$1 = reader;
  23315. const stream_1$2 = stream$1;
  23316. class ReaderAsync extends reader_1$1.default {
  23317. constructor() {
  23318. super(...arguments);
  23319. this._walkAsync = fsWalk$1.walk;
  23320. this._readerStream = new stream_1$2.default(this._settings);
  23321. }
  23322. dynamic(root, options) {
  23323. return new Promise((resolve, reject) => {
  23324. this._walkAsync(root, options, (error, entries) => {
  23325. if (error === null) {
  23326. resolve(entries);
  23327. }
  23328. else {
  23329. reject(error);
  23330. }
  23331. });
  23332. });
  23333. }
  23334. async static(patterns, options) {
  23335. const entries = [];
  23336. const stream = this._readerStream.static(patterns, options);
  23337. // After #235, replace it with an asynchronous iterator.
  23338. return new Promise((resolve, reject) => {
  23339. stream.once('error', reject);
  23340. stream.on('data', (entry) => entries.push(entry));
  23341. stream.once('end', () => resolve(entries));
  23342. });
  23343. }
  23344. }
  23345. async$6.default = ReaderAsync;
  23346. var provider = {};
  23347. var deep = {};
  23348. var partial = {};
  23349. var matcher = {};
  23350. Object.defineProperty(matcher, "__esModule", { value: true });
  23351. const utils$5 = utils$g;
  23352. class Matcher {
  23353. constructor(_patterns, _settings, _micromatchOptions) {
  23354. this._patterns = _patterns;
  23355. this._settings = _settings;
  23356. this._micromatchOptions = _micromatchOptions;
  23357. this._storage = [];
  23358. this._fillStorage();
  23359. }
  23360. _fillStorage() {
  23361. for (const pattern of this._patterns) {
  23362. const segments = this._getPatternSegments(pattern);
  23363. const sections = this._splitSegmentsIntoSections(segments);
  23364. this._storage.push({
  23365. complete: sections.length <= 1,
  23366. pattern,
  23367. segments,
  23368. sections
  23369. });
  23370. }
  23371. }
  23372. _getPatternSegments(pattern) {
  23373. const parts = utils$5.pattern.getPatternParts(pattern, this._micromatchOptions);
  23374. return parts.map((part) => {
  23375. const dynamic = utils$5.pattern.isDynamicPattern(part, this._settings);
  23376. if (!dynamic) {
  23377. return {
  23378. dynamic: false,
  23379. pattern: part
  23380. };
  23381. }
  23382. return {
  23383. dynamic: true,
  23384. pattern: part,
  23385. patternRe: utils$5.pattern.makeRe(part, this._micromatchOptions)
  23386. };
  23387. });
  23388. }
  23389. _splitSegmentsIntoSections(segments) {
  23390. return utils$5.array.splitWhen(segments, (segment) => segment.dynamic && utils$5.pattern.hasGlobStar(segment.pattern));
  23391. }
  23392. }
  23393. matcher.default = Matcher;
  23394. Object.defineProperty(partial, "__esModule", { value: true });
  23395. const matcher_1 = matcher;
  23396. class PartialMatcher extends matcher_1.default {
  23397. match(filepath) {
  23398. const parts = filepath.split('/');
  23399. const levels = parts.length;
  23400. const patterns = this._storage.filter((info) => !info.complete || info.segments.length > levels);
  23401. for (const pattern of patterns) {
  23402. const section = pattern.sections[0];
  23403. /**
  23404. * In this case, the pattern has a globstar and we must read all directories unconditionally,
  23405. * but only if the level has reached the end of the first group.
  23406. *
  23407. * fixtures/{a,b}/**
  23408. * ^ true/false ^ always true
  23409. */
  23410. if (!pattern.complete && levels > section.length) {
  23411. return true;
  23412. }
  23413. const match = parts.every((part, index) => {
  23414. const segment = pattern.segments[index];
  23415. if (segment.dynamic && segment.patternRe.test(part)) {
  23416. return true;
  23417. }
  23418. if (!segment.dynamic && segment.pattern === part) {
  23419. return true;
  23420. }
  23421. return false;
  23422. });
  23423. if (match) {
  23424. return true;
  23425. }
  23426. }
  23427. return false;
  23428. }
  23429. }
  23430. partial.default = PartialMatcher;
  23431. Object.defineProperty(deep, "__esModule", { value: true });
  23432. const utils$4 = utils$g;
  23433. const partial_1 = partial;
  23434. class DeepFilter {
  23435. constructor(_settings, _micromatchOptions) {
  23436. this._settings = _settings;
  23437. this._micromatchOptions = _micromatchOptions;
  23438. }
  23439. getFilter(basePath, positive, negative) {
  23440. const matcher = this._getMatcher(positive);
  23441. const negativeRe = this._getNegativePatternsRe(negative);
  23442. return (entry) => this._filter(basePath, entry, matcher, negativeRe);
  23443. }
  23444. _getMatcher(patterns) {
  23445. return new partial_1.default(patterns, this._settings, this._micromatchOptions);
  23446. }
  23447. _getNegativePatternsRe(patterns) {
  23448. const affectDepthOfReadingPatterns = patterns.filter(utils$4.pattern.isAffectDepthOfReadingPattern);
  23449. return utils$4.pattern.convertPatternsToRe(affectDepthOfReadingPatterns, this._micromatchOptions);
  23450. }
  23451. _filter(basePath, entry, matcher, negativeRe) {
  23452. if (this._isSkippedByDeep(basePath, entry.path)) {
  23453. return false;
  23454. }
  23455. if (this._isSkippedSymbolicLink(entry)) {
  23456. return false;
  23457. }
  23458. const filepath = utils$4.path.removeLeadingDotSegment(entry.path);
  23459. if (this._isSkippedByPositivePatterns(filepath, matcher)) {
  23460. return false;
  23461. }
  23462. return this._isSkippedByNegativePatterns(filepath, negativeRe);
  23463. }
  23464. _isSkippedByDeep(basePath, entryPath) {
  23465. /**
  23466. * Avoid unnecessary depth calculations when it doesn't matter.
  23467. */
  23468. if (this._settings.deep === Infinity) {
  23469. return false;
  23470. }
  23471. return this._getEntryLevel(basePath, entryPath) >= this._settings.deep;
  23472. }
  23473. _getEntryLevel(basePath, entryPath) {
  23474. const entryPathDepth = entryPath.split('/').length;
  23475. if (basePath === '') {
  23476. return entryPathDepth;
  23477. }
  23478. const basePathDepth = basePath.split('/').length;
  23479. return entryPathDepth - basePathDepth;
  23480. }
  23481. _isSkippedSymbolicLink(entry) {
  23482. return !this._settings.followSymbolicLinks && entry.dirent.isSymbolicLink();
  23483. }
  23484. _isSkippedByPositivePatterns(entryPath, matcher) {
  23485. return !this._settings.baseNameMatch && !matcher.match(entryPath);
  23486. }
  23487. _isSkippedByNegativePatterns(entryPath, patternsRe) {
  23488. return !utils$4.pattern.matchAny(entryPath, patternsRe);
  23489. }
  23490. }
  23491. deep.default = DeepFilter;
  23492. var entry$1 = {};
  23493. Object.defineProperty(entry$1, "__esModule", { value: true });
  23494. const utils$3 = utils$g;
  23495. class EntryFilter {
  23496. constructor(_settings, _micromatchOptions) {
  23497. this._settings = _settings;
  23498. this._micromatchOptions = _micromatchOptions;
  23499. this.index = new Map();
  23500. }
  23501. getFilter(positive, negative) {
  23502. const positiveRe = utils$3.pattern.convertPatternsToRe(positive, this._micromatchOptions);
  23503. const negativeRe = utils$3.pattern.convertPatternsToRe(negative, Object.assign(Object.assign({}, this._micromatchOptions), { dot: true }));
  23504. return (entry) => this._filter(entry, positiveRe, negativeRe);
  23505. }
  23506. _filter(entry, positiveRe, negativeRe) {
  23507. const filepath = utils$3.path.removeLeadingDotSegment(entry.path);
  23508. if (this._settings.unique && this._isDuplicateEntry(filepath)) {
  23509. return false;
  23510. }
  23511. if (this._onlyFileFilter(entry) || this._onlyDirectoryFilter(entry)) {
  23512. return false;
  23513. }
  23514. if (this._isSkippedByAbsoluteNegativePatterns(filepath, negativeRe)) {
  23515. return false;
  23516. }
  23517. const isDirectory = entry.dirent.isDirectory();
  23518. const isMatched = this._isMatchToPatterns(filepath, positiveRe, isDirectory) && !this._isMatchToPatterns(filepath, negativeRe, isDirectory);
  23519. if (this._settings.unique && isMatched) {
  23520. this._createIndexRecord(filepath);
  23521. }
  23522. return isMatched;
  23523. }
  23524. _isDuplicateEntry(filepath) {
  23525. return this.index.has(filepath);
  23526. }
  23527. _createIndexRecord(filepath) {
  23528. this.index.set(filepath, undefined);
  23529. }
  23530. _onlyFileFilter(entry) {
  23531. return this._settings.onlyFiles && !entry.dirent.isFile();
  23532. }
  23533. _onlyDirectoryFilter(entry) {
  23534. return this._settings.onlyDirectories && !entry.dirent.isDirectory();
  23535. }
  23536. _isSkippedByAbsoluteNegativePatterns(entryPath, patternsRe) {
  23537. if (!this._settings.absolute) {
  23538. return false;
  23539. }
  23540. const fullpath = utils$3.path.makeAbsolute(this._settings.cwd, entryPath);
  23541. return utils$3.pattern.matchAny(fullpath, patternsRe);
  23542. }
  23543. _isMatchToPatterns(filepath, patternsRe, isDirectory) {
  23544. // Trying to match files and directories by patterns.
  23545. const isMatched = utils$3.pattern.matchAny(filepath, patternsRe);
  23546. // A pattern with a trailling slash can be used for directory matching.
  23547. // To apply such pattern, we need to add a tralling slash to the path.
  23548. if (!isMatched && isDirectory) {
  23549. return utils$3.pattern.matchAny(filepath + '/', patternsRe);
  23550. }
  23551. return isMatched;
  23552. }
  23553. }
  23554. entry$1.default = EntryFilter;
  23555. var error$1 = {};
  23556. Object.defineProperty(error$1, "__esModule", { value: true });
  23557. const utils$2 = utils$g;
  23558. class ErrorFilter {
  23559. constructor(_settings) {
  23560. this._settings = _settings;
  23561. }
  23562. getFilter() {
  23563. return (error) => this._isNonFatalError(error);
  23564. }
  23565. _isNonFatalError(error) {
  23566. return utils$2.errno.isEnoentCodeError(error) || this._settings.suppressErrors;
  23567. }
  23568. }
  23569. error$1.default = ErrorFilter;
  23570. var entry = {};
  23571. Object.defineProperty(entry, "__esModule", { value: true });
  23572. const utils$1 = utils$g;
  23573. class EntryTransformer {
  23574. constructor(_settings) {
  23575. this._settings = _settings;
  23576. }
  23577. getTransformer() {
  23578. return (entry) => this._transform(entry);
  23579. }
  23580. _transform(entry) {
  23581. let filepath = entry.path;
  23582. if (this._settings.absolute) {
  23583. filepath = utils$1.path.makeAbsolute(this._settings.cwd, filepath);
  23584. filepath = utils$1.path.unixify(filepath);
  23585. }
  23586. if (this._settings.markDirectories && entry.dirent.isDirectory()) {
  23587. filepath += '/';
  23588. }
  23589. if (!this._settings.objectMode) {
  23590. return filepath;
  23591. }
  23592. return Object.assign(Object.assign({}, entry), { path: filepath });
  23593. }
  23594. }
  23595. entry.default = EntryTransformer;
  23596. Object.defineProperty(provider, "__esModule", { value: true });
  23597. const path$b = require$$0$4;
  23598. const deep_1 = deep;
  23599. const entry_1 = entry$1;
  23600. const error_1 = error$1;
  23601. const entry_2 = entry;
  23602. class Provider {
  23603. constructor(_settings) {
  23604. this._settings = _settings;
  23605. this.errorFilter = new error_1.default(this._settings);
  23606. this.entryFilter = new entry_1.default(this._settings, this._getMicromatchOptions());
  23607. this.deepFilter = new deep_1.default(this._settings, this._getMicromatchOptions());
  23608. this.entryTransformer = new entry_2.default(this._settings);
  23609. }
  23610. _getRootDirectory(task) {
  23611. return path$b.resolve(this._settings.cwd, task.base);
  23612. }
  23613. _getReaderOptions(task) {
  23614. const basePath = task.base === '.' ? '' : task.base;
  23615. return {
  23616. basePath,
  23617. pathSegmentSeparator: '/',
  23618. concurrency: this._settings.concurrency,
  23619. deepFilter: this.deepFilter.getFilter(basePath, task.positive, task.negative),
  23620. entryFilter: this.entryFilter.getFilter(task.positive, task.negative),
  23621. errorFilter: this.errorFilter.getFilter(),
  23622. followSymbolicLinks: this._settings.followSymbolicLinks,
  23623. fs: this._settings.fs,
  23624. stats: this._settings.stats,
  23625. throwErrorOnBrokenSymbolicLink: this._settings.throwErrorOnBrokenSymbolicLink,
  23626. transform: this.entryTransformer.getTransformer()
  23627. };
  23628. }
  23629. _getMicromatchOptions() {
  23630. return {
  23631. dot: this._settings.dot,
  23632. matchBase: this._settings.baseNameMatch,
  23633. nobrace: !this._settings.braceExpansion,
  23634. nocase: !this._settings.caseSensitiveMatch,
  23635. noext: !this._settings.extglob,
  23636. noglobstar: !this._settings.globstar,
  23637. posix: true,
  23638. strictSlashes: false
  23639. };
  23640. }
  23641. }
  23642. provider.default = Provider;
  23643. Object.defineProperty(async$7, "__esModule", { value: true });
  23644. const async_1$1 = async$6;
  23645. const provider_1$2 = provider;
  23646. class ProviderAsync extends provider_1$2.default {
  23647. constructor() {
  23648. super(...arguments);
  23649. this._reader = new async_1$1.default(this._settings);
  23650. }
  23651. async read(task) {
  23652. const root = this._getRootDirectory(task);
  23653. const options = this._getReaderOptions(task);
  23654. const entries = await this.api(root, task, options);
  23655. return entries.map((entry) => options.transform(entry));
  23656. }
  23657. api(root, task, options) {
  23658. if (task.dynamic) {
  23659. return this._reader.dynamic(root, options);
  23660. }
  23661. return this._reader.static(task.patterns, options);
  23662. }
  23663. }
  23664. async$7.default = ProviderAsync;
  23665. var stream = {};
  23666. Object.defineProperty(stream, "__esModule", { value: true });
  23667. const stream_1$1 = require$$0$6;
  23668. const stream_2 = stream$1;
  23669. const provider_1$1 = provider;
  23670. class ProviderStream extends provider_1$1.default {
  23671. constructor() {
  23672. super(...arguments);
  23673. this._reader = new stream_2.default(this._settings);
  23674. }
  23675. read(task) {
  23676. const root = this._getRootDirectory(task);
  23677. const options = this._getReaderOptions(task);
  23678. const source = this.api(root, task, options);
  23679. const destination = new stream_1$1.Readable({ objectMode: true, read: () => { } });
  23680. source
  23681. .once('error', (error) => destination.emit('error', error))
  23682. .on('data', (entry) => destination.emit('data', options.transform(entry)))
  23683. .once('end', () => destination.emit('end'));
  23684. destination
  23685. .once('close', () => source.destroy());
  23686. return destination;
  23687. }
  23688. api(root, task, options) {
  23689. if (task.dynamic) {
  23690. return this._reader.dynamic(root, options);
  23691. }
  23692. return this._reader.static(task.patterns, options);
  23693. }
  23694. }
  23695. stream.default = ProviderStream;
  23696. var sync$2 = {};
  23697. var sync$1 = {};
  23698. Object.defineProperty(sync$1, "__esModule", { value: true });
  23699. const fsStat = out$1;
  23700. const fsWalk = out$3;
  23701. const reader_1 = reader;
  23702. class ReaderSync extends reader_1.default {
  23703. constructor() {
  23704. super(...arguments);
  23705. this._walkSync = fsWalk.walkSync;
  23706. this._statSync = fsStat.statSync;
  23707. }
  23708. dynamic(root, options) {
  23709. return this._walkSync(root, options);
  23710. }
  23711. static(patterns, options) {
  23712. const entries = [];
  23713. for (const pattern of patterns) {
  23714. const filepath = this._getFullEntryPath(pattern);
  23715. const entry = this._getEntry(filepath, pattern, options);
  23716. if (entry === null || !options.entryFilter(entry)) {
  23717. continue;
  23718. }
  23719. entries.push(entry);
  23720. }
  23721. return entries;
  23722. }
  23723. _getEntry(filepath, pattern, options) {
  23724. try {
  23725. const stats = this._getStat(filepath);
  23726. return this._makeEntry(stats, pattern);
  23727. }
  23728. catch (error) {
  23729. if (options.errorFilter(error)) {
  23730. return null;
  23731. }
  23732. throw error;
  23733. }
  23734. }
  23735. _getStat(filepath) {
  23736. return this._statSync(filepath, this._fsStatSettings);
  23737. }
  23738. }
  23739. sync$1.default = ReaderSync;
  23740. Object.defineProperty(sync$2, "__esModule", { value: true });
  23741. const sync_1$1 = sync$1;
  23742. const provider_1 = provider;
  23743. class ProviderSync extends provider_1.default {
  23744. constructor() {
  23745. super(...arguments);
  23746. this._reader = new sync_1$1.default(this._settings);
  23747. }
  23748. read(task) {
  23749. const root = this._getRootDirectory(task);
  23750. const options = this._getReaderOptions(task);
  23751. const entries = this.api(root, task, options);
  23752. return entries.map(options.transform);
  23753. }
  23754. api(root, task, options) {
  23755. if (task.dynamic) {
  23756. return this._reader.dynamic(root, options);
  23757. }
  23758. return this._reader.static(task.patterns, options);
  23759. }
  23760. }
  23761. sync$2.default = ProviderSync;
  23762. var settings = {};
  23763. (function (exports) {
  23764. Object.defineProperty(exports, "__esModule", { value: true });
  23765. exports.DEFAULT_FILE_SYSTEM_ADAPTER = void 0;
  23766. const fs = require$$0__default;
  23767. const os = require$$2;
  23768. /**
  23769. * The `os.cpus` method can return zero. We expect the number of cores to be greater than zero.
  23770. * https://github.com/nodejs/node/blob/7faeddf23a98c53896f8b574a6e66589e8fb1eb8/lib/os.js#L106-L107
  23771. */
  23772. const CPU_COUNT = Math.max(os.cpus().length, 1);
  23773. exports.DEFAULT_FILE_SYSTEM_ADAPTER = {
  23774. lstat: fs.lstat,
  23775. lstatSync: fs.lstatSync,
  23776. stat: fs.stat,
  23777. statSync: fs.statSync,
  23778. readdir: fs.readdir,
  23779. readdirSync: fs.readdirSync
  23780. };
  23781. class Settings {
  23782. constructor(_options = {}) {
  23783. this._options = _options;
  23784. this.absolute = this._getValue(this._options.absolute, false);
  23785. this.baseNameMatch = this._getValue(this._options.baseNameMatch, false);
  23786. this.braceExpansion = this._getValue(this._options.braceExpansion, true);
  23787. this.caseSensitiveMatch = this._getValue(this._options.caseSensitiveMatch, true);
  23788. this.concurrency = this._getValue(this._options.concurrency, CPU_COUNT);
  23789. this.cwd = this._getValue(this._options.cwd, process.cwd());
  23790. this.deep = this._getValue(this._options.deep, Infinity);
  23791. this.dot = this._getValue(this._options.dot, false);
  23792. this.extglob = this._getValue(this._options.extglob, true);
  23793. this.followSymbolicLinks = this._getValue(this._options.followSymbolicLinks, true);
  23794. this.fs = this._getFileSystemMethods(this._options.fs);
  23795. this.globstar = this._getValue(this._options.globstar, true);
  23796. this.ignore = this._getValue(this._options.ignore, []);
  23797. this.markDirectories = this._getValue(this._options.markDirectories, false);
  23798. this.objectMode = this._getValue(this._options.objectMode, false);
  23799. this.onlyDirectories = this._getValue(this._options.onlyDirectories, false);
  23800. this.onlyFiles = this._getValue(this._options.onlyFiles, true);
  23801. this.stats = this._getValue(this._options.stats, false);
  23802. this.suppressErrors = this._getValue(this._options.suppressErrors, false);
  23803. this.throwErrorOnBrokenSymbolicLink = this._getValue(this._options.throwErrorOnBrokenSymbolicLink, false);
  23804. this.unique = this._getValue(this._options.unique, true);
  23805. if (this.onlyDirectories) {
  23806. this.onlyFiles = false;
  23807. }
  23808. if (this.stats) {
  23809. this.objectMode = true;
  23810. }
  23811. // Remove the cast to the array in the next major (#404).
  23812. this.ignore = [].concat(this.ignore);
  23813. }
  23814. _getValue(option, value) {
  23815. return option === undefined ? value : option;
  23816. }
  23817. _getFileSystemMethods(methods = {}) {
  23818. return Object.assign(Object.assign({}, exports.DEFAULT_FILE_SYSTEM_ADAPTER), methods);
  23819. }
  23820. }
  23821. exports.default = Settings;
  23822. } (settings));
  23823. const taskManager = tasks;
  23824. const async_1 = async$7;
  23825. const stream_1 = stream;
  23826. const sync_1 = sync$2;
  23827. const settings_1 = settings;
  23828. const utils = utils$g;
  23829. async function FastGlob(source, options) {
  23830. assertPatternsInput(source);
  23831. const works = getWorks(source, async_1.default, options);
  23832. const result = await Promise.all(works);
  23833. return utils.array.flatten(result);
  23834. }
  23835. // https://github.com/typescript-eslint/typescript-eslint/issues/60
  23836. // eslint-disable-next-line no-redeclare
  23837. (function (FastGlob) {
  23838. FastGlob.glob = FastGlob;
  23839. FastGlob.globSync = sync;
  23840. FastGlob.globStream = stream;
  23841. FastGlob.async = FastGlob;
  23842. function sync(source, options) {
  23843. assertPatternsInput(source);
  23844. const works = getWorks(source, sync_1.default, options);
  23845. return utils.array.flatten(works);
  23846. }
  23847. FastGlob.sync = sync;
  23848. function stream(source, options) {
  23849. assertPatternsInput(source);
  23850. const works = getWorks(source, stream_1.default, options);
  23851. /**
  23852. * The stream returned by the provider cannot work with an asynchronous iterator.
  23853. * To support asynchronous iterators, regardless of the number of tasks, we always multiplex streams.
  23854. * This affects performance (+25%). I don't see best solution right now.
  23855. */
  23856. return utils.stream.merge(works);
  23857. }
  23858. FastGlob.stream = stream;
  23859. function generateTasks(source, options) {
  23860. assertPatternsInput(source);
  23861. const patterns = [].concat(source);
  23862. const settings = new settings_1.default(options);
  23863. return taskManager.generate(patterns, settings);
  23864. }
  23865. FastGlob.generateTasks = generateTasks;
  23866. function isDynamicPattern(source, options) {
  23867. assertPatternsInput(source);
  23868. const settings = new settings_1.default(options);
  23869. return utils.pattern.isDynamicPattern(source, settings);
  23870. }
  23871. FastGlob.isDynamicPattern = isDynamicPattern;
  23872. function escapePath(source) {
  23873. assertPatternsInput(source);
  23874. return utils.path.escape(source);
  23875. }
  23876. FastGlob.escapePath = escapePath;
  23877. function convertPathToPattern(source) {
  23878. assertPatternsInput(source);
  23879. return utils.path.convertPathToPattern(source);
  23880. }
  23881. FastGlob.convertPathToPattern = convertPathToPattern;
  23882. (function (posix) {
  23883. function escapePath(source) {
  23884. assertPatternsInput(source);
  23885. return utils.path.escapePosixPath(source);
  23886. }
  23887. posix.escapePath = escapePath;
  23888. function convertPathToPattern(source) {
  23889. assertPatternsInput(source);
  23890. return utils.path.convertPosixPathToPattern(source);
  23891. }
  23892. posix.convertPathToPattern = convertPathToPattern;
  23893. })(FastGlob.posix || (FastGlob.posix = {}));
  23894. (function (win32) {
  23895. function escapePath(source) {
  23896. assertPatternsInput(source);
  23897. return utils.path.escapeWindowsPath(source);
  23898. }
  23899. win32.escapePath = escapePath;
  23900. function convertPathToPattern(source) {
  23901. assertPatternsInput(source);
  23902. return utils.path.convertWindowsPathToPattern(source);
  23903. }
  23904. win32.convertPathToPattern = convertPathToPattern;
  23905. })(FastGlob.win32 || (FastGlob.win32 = {}));
  23906. })(FastGlob || (FastGlob = {}));
  23907. function getWorks(source, _Provider, options) {
  23908. const patterns = [].concat(source);
  23909. const settings = new settings_1.default(options);
  23910. const tasks = taskManager.generate(patterns, settings);
  23911. const provider = new _Provider(settings);
  23912. return tasks.map(provider.read, provider);
  23913. }
  23914. function assertPatternsInput(input) {
  23915. const source = [].concat(input);
  23916. const isValidSource = source.every((item) => utils.string.isString(item) && !utils.string.isEmpty(item));
  23917. if (!isValidSource) {
  23918. throw new TypeError('Patterns must be a string (non empty) or an array of strings');
  23919. }
  23920. }
  23921. var out = FastGlob;
  23922. var glob = /*@__PURE__*/getDefaultExportFromCjs(out);
  23923. var src$2 = {};
  23924. // @ts-check
  23925. const path$a = require$$0$4;
  23926. const fs$a = require$$0__default;
  23927. const os$3 = require$$2;
  23928. const fsReadFileAsync = fs$a.promises.readFile;
  23929. /** @type {(name: string, sync: boolean) => string[]} */
  23930. function getDefaultSearchPlaces(name, sync) {
  23931. return [
  23932. 'package.json',
  23933. `.${name}rc.json`,
  23934. `.${name}rc.js`,
  23935. `.${name}rc.cjs`,
  23936. ...(sync ? [] : [`.${name}rc.mjs`]),
  23937. `.config/${name}rc`,
  23938. `.config/${name}rc.json`,
  23939. `.config/${name}rc.js`,
  23940. `.config/${name}rc.cjs`,
  23941. ...(sync ? [] : [`.config/${name}rc.mjs`]),
  23942. `${name}.config.js`,
  23943. `${name}.config.cjs`,
  23944. ...(sync ? [] : [`${name}.config.mjs`]),
  23945. ];
  23946. }
  23947. /**
  23948. * @type {(p: string) => string}
  23949. *
  23950. * see #17
  23951. * On *nix, if cwd is not under homedir,
  23952. * the last path will be '', ('/build' -> '')
  23953. * but it should be '/' actually.
  23954. * And on Windows, this will never happen. ('C:\build' -> 'C:')
  23955. */
  23956. function parentDir(p) {
  23957. return path$a.dirname(p) || path$a.sep;
  23958. }
  23959. /** @type {import('./index').LoaderSync} */
  23960. const jsonLoader = (_, content) => JSON.parse(content);
  23961. // Use plain require in webpack context for dynamic import
  23962. const requireFunc = typeof __webpack_require__ === "function" ? __non_webpack_require__ : __require;
  23963. /** @type {import('./index').LoadersSync} */
  23964. const defaultLoadersSync = Object.freeze({
  23965. '.js': requireFunc,
  23966. '.json': requireFunc,
  23967. '.cjs': requireFunc,
  23968. noExt: jsonLoader,
  23969. });
  23970. src$2.defaultLoadersSync = defaultLoadersSync;
  23971. /** @type {import('./index').Loader} */
  23972. const dynamicImport = async id => {
  23973. try {
  23974. const mod = await import(/* webpackIgnore: true */ id);
  23975. return mod.default;
  23976. } catch (e) {
  23977. try {
  23978. return requireFunc(id);
  23979. } catch (/** @type {any} */ requireE) {
  23980. if (
  23981. requireE.code === 'ERR_REQUIRE_ESM' ||
  23982. (requireE instanceof SyntaxError &&
  23983. requireE
  23984. .toString()
  23985. .includes('Cannot use import statement outside a module'))
  23986. ) {
  23987. throw e;
  23988. }
  23989. throw requireE;
  23990. }
  23991. }
  23992. };
  23993. /** @type {import('./index').Loaders} */
  23994. const defaultLoaders = Object.freeze({
  23995. '.js': dynamicImport,
  23996. '.mjs': dynamicImport,
  23997. '.cjs': dynamicImport,
  23998. '.json': jsonLoader,
  23999. noExt: jsonLoader,
  24000. });
  24001. src$2.defaultLoaders = defaultLoaders;
  24002. /**
  24003. * @param {string} name
  24004. * @param {import('./index').Options | import('./index').OptionsSync} options
  24005. * @param {boolean} sync
  24006. * @returns {Required<import('./index').Options | import('./index').OptionsSync>}
  24007. */
  24008. function getOptions(name, options, sync) {
  24009. /** @type {Required<import('./index').Options>} */
  24010. const conf = {
  24011. stopDir: os$3.homedir(),
  24012. searchPlaces: getDefaultSearchPlaces(name, sync),
  24013. ignoreEmptySearchPlaces: true,
  24014. cache: true,
  24015. transform: x => x,
  24016. packageProp: [name],
  24017. ...options,
  24018. loaders: {
  24019. ...(sync ? defaultLoadersSync : defaultLoaders),
  24020. ...options.loaders,
  24021. },
  24022. };
  24023. conf.searchPlaces.forEach(place => {
  24024. const key = path$a.extname(place) || 'noExt';
  24025. const loader = conf.loaders[key];
  24026. if (!loader) {
  24027. throw new Error(`Missing loader for extension "${place}"`);
  24028. }
  24029. if (typeof loader !== 'function') {
  24030. throw new Error(
  24031. `Loader for extension "${place}" is not a function: Received ${typeof loader}.`,
  24032. );
  24033. }
  24034. });
  24035. return conf;
  24036. }
  24037. /** @type {(props: string | string[], obj: Record<string, any>) => unknown} */
  24038. function getPackageProp(props, obj) {
  24039. if (typeof props === 'string' && props in obj) return obj[props];
  24040. return (
  24041. (Array.isArray(props) ? props : props.split('.')).reduce(
  24042. (acc, prop) => (acc === undefined ? acc : acc[prop]),
  24043. obj,
  24044. ) || null
  24045. );
  24046. }
  24047. /** @param {string} filepath */
  24048. function validateFilePath(filepath) {
  24049. if (!filepath) throw new Error('load must pass a non-empty string');
  24050. }
  24051. /** @type {(loader: import('./index').Loader, ext: string) => void} */
  24052. function validateLoader(loader, ext) {
  24053. if (!loader) throw new Error(`No loader specified for extension "${ext}"`);
  24054. if (typeof loader !== 'function') throw new Error('loader is not a function');
  24055. }
  24056. /** @type {(enableCache: boolean) => <T>(c: Map<string, T>, filepath: string, res: T) => T} */
  24057. const makeEmplace = enableCache => (c, filepath, res) => {
  24058. if (enableCache) c.set(filepath, res);
  24059. return res;
  24060. };
  24061. /** @type {import('./index').lilconfig} */
  24062. src$2.lilconfig = function lilconfig(name, options) {
  24063. const {
  24064. ignoreEmptySearchPlaces,
  24065. loaders,
  24066. packageProp,
  24067. searchPlaces,
  24068. stopDir,
  24069. transform,
  24070. cache,
  24071. } = getOptions(name, options ?? {}, false);
  24072. const searchCache = new Map();
  24073. const loadCache = new Map();
  24074. const emplace = makeEmplace(cache);
  24075. return {
  24076. async search(searchFrom = process.cwd()) {
  24077. /** @type {import('./index').LilconfigResult} */
  24078. const result = {
  24079. config: null,
  24080. filepath: '',
  24081. };
  24082. /** @type {Set<string>} */
  24083. const visited = new Set();
  24084. let dir = searchFrom;
  24085. dirLoop: while (true) {
  24086. if (cache) {
  24087. const r = searchCache.get(dir);
  24088. if (r !== undefined) {
  24089. for (const p of visited) searchCache.set(p, r);
  24090. return r;
  24091. }
  24092. visited.add(dir);
  24093. }
  24094. for (const searchPlace of searchPlaces) {
  24095. const filepath = path$a.join(dir, searchPlace);
  24096. try {
  24097. await fs$a.promises.access(filepath);
  24098. } catch {
  24099. continue;
  24100. }
  24101. const content = String(await fsReadFileAsync(filepath));
  24102. const loaderKey = path$a.extname(searchPlace) || 'noExt';
  24103. const loader = loaders[loaderKey];
  24104. // handle package.json
  24105. if (searchPlace === 'package.json') {
  24106. const pkg = await loader(filepath, content);
  24107. const maybeConfig = getPackageProp(packageProp, pkg);
  24108. if (maybeConfig != null) {
  24109. result.config = maybeConfig;
  24110. result.filepath = filepath;
  24111. break dirLoop;
  24112. }
  24113. continue;
  24114. }
  24115. // handle other type of configs
  24116. const isEmpty = content.trim() === '';
  24117. if (isEmpty && ignoreEmptySearchPlaces) continue;
  24118. if (isEmpty) {
  24119. result.isEmpty = true;
  24120. result.config = undefined;
  24121. } else {
  24122. validateLoader(loader, loaderKey);
  24123. result.config = await loader(filepath, content);
  24124. }
  24125. result.filepath = filepath;
  24126. break dirLoop;
  24127. }
  24128. if (dir === stopDir || dir === parentDir(dir)) break dirLoop;
  24129. dir = parentDir(dir);
  24130. }
  24131. const transformed =
  24132. // not found
  24133. result.filepath === '' && result.config === null
  24134. ? transform(null)
  24135. : transform(result);
  24136. if (cache) {
  24137. for (const p of visited) searchCache.set(p, transformed);
  24138. }
  24139. return transformed;
  24140. },
  24141. async load(filepath) {
  24142. validateFilePath(filepath);
  24143. const absPath = path$a.resolve(process.cwd(), filepath);
  24144. if (cache && loadCache.has(absPath)) {
  24145. return loadCache.get(absPath);
  24146. }
  24147. const {base, ext} = path$a.parse(absPath);
  24148. const loaderKey = ext || 'noExt';
  24149. const loader = loaders[loaderKey];
  24150. validateLoader(loader, loaderKey);
  24151. const content = String(await fsReadFileAsync(absPath));
  24152. if (base === 'package.json') {
  24153. const pkg = await loader(absPath, content);
  24154. return emplace(
  24155. loadCache,
  24156. absPath,
  24157. transform({
  24158. config: getPackageProp(packageProp, pkg),
  24159. filepath: absPath,
  24160. }),
  24161. );
  24162. }
  24163. /** @type {import('./index').LilconfigResult} */
  24164. const result = {
  24165. config: null,
  24166. filepath: absPath,
  24167. };
  24168. // handle other type of configs
  24169. const isEmpty = content.trim() === '';
  24170. if (isEmpty && ignoreEmptySearchPlaces)
  24171. return emplace(
  24172. loadCache,
  24173. absPath,
  24174. transform({
  24175. config: undefined,
  24176. filepath: absPath,
  24177. isEmpty: true,
  24178. }),
  24179. );
  24180. // cosmiconfig returns undefined for empty files
  24181. result.config = isEmpty ? undefined : await loader(absPath, content);
  24182. return emplace(
  24183. loadCache,
  24184. absPath,
  24185. transform(isEmpty ? {...result, isEmpty, config: undefined} : result),
  24186. );
  24187. },
  24188. clearLoadCache() {
  24189. if (cache) loadCache.clear();
  24190. },
  24191. clearSearchCache() {
  24192. if (cache) searchCache.clear();
  24193. },
  24194. clearCaches() {
  24195. if (cache) {
  24196. loadCache.clear();
  24197. searchCache.clear();
  24198. }
  24199. },
  24200. };
  24201. };
  24202. /** @type {import('./index').lilconfigSync} */
  24203. src$2.lilconfigSync = function lilconfigSync(name, options) {
  24204. const {
  24205. ignoreEmptySearchPlaces,
  24206. loaders,
  24207. packageProp,
  24208. searchPlaces,
  24209. stopDir,
  24210. transform,
  24211. cache,
  24212. } = getOptions(name, options ?? {}, true);
  24213. const searchCache = new Map();
  24214. const loadCache = new Map();
  24215. const emplace = makeEmplace(cache);
  24216. return {
  24217. search(searchFrom = process.cwd()) {
  24218. /** @type {import('./index').LilconfigResult} */
  24219. const result = {
  24220. config: null,
  24221. filepath: '',
  24222. };
  24223. /** @type {Set<string>} */
  24224. const visited = new Set();
  24225. let dir = searchFrom;
  24226. dirLoop: while (true) {
  24227. if (cache) {
  24228. const r = searchCache.get(dir);
  24229. if (r !== undefined) {
  24230. for (const p of visited) searchCache.set(p, r);
  24231. return r;
  24232. }
  24233. visited.add(dir);
  24234. }
  24235. for (const searchPlace of searchPlaces) {
  24236. const filepath = path$a.join(dir, searchPlace);
  24237. try {
  24238. fs$a.accessSync(filepath);
  24239. } catch {
  24240. continue;
  24241. }
  24242. const loaderKey = path$a.extname(searchPlace) || 'noExt';
  24243. const loader = loaders[loaderKey];
  24244. const content = String(fs$a.readFileSync(filepath));
  24245. // handle package.json
  24246. if (searchPlace === 'package.json') {
  24247. const pkg = loader(filepath, content);
  24248. const maybeConfig = getPackageProp(packageProp, pkg);
  24249. if (maybeConfig != null) {
  24250. result.config = maybeConfig;
  24251. result.filepath = filepath;
  24252. break dirLoop;
  24253. }
  24254. continue;
  24255. }
  24256. // handle other type of configs
  24257. const isEmpty = content.trim() === '';
  24258. if (isEmpty && ignoreEmptySearchPlaces) continue;
  24259. if (isEmpty) {
  24260. result.isEmpty = true;
  24261. result.config = undefined;
  24262. } else {
  24263. validateLoader(loader, loaderKey);
  24264. result.config = loader(filepath, content);
  24265. }
  24266. result.filepath = filepath;
  24267. break dirLoop;
  24268. }
  24269. if (dir === stopDir || dir === parentDir(dir)) break dirLoop;
  24270. dir = parentDir(dir);
  24271. }
  24272. const transformed =
  24273. // not found
  24274. result.filepath === '' && result.config === null
  24275. ? transform(null)
  24276. : transform(result);
  24277. if (cache) {
  24278. for (const p of visited) searchCache.set(p, transformed);
  24279. }
  24280. return transformed;
  24281. },
  24282. load(filepath) {
  24283. validateFilePath(filepath);
  24284. const absPath = path$a.resolve(process.cwd(), filepath);
  24285. if (cache && loadCache.has(absPath)) {
  24286. return loadCache.get(absPath);
  24287. }
  24288. const {base, ext} = path$a.parse(absPath);
  24289. const loaderKey = ext || 'noExt';
  24290. const loader = loaders[loaderKey];
  24291. validateLoader(loader, loaderKey);
  24292. const content = String(fs$a.readFileSync(absPath));
  24293. if (base === 'package.json') {
  24294. const pkg = loader(absPath, content);
  24295. return transform({
  24296. config: getPackageProp(packageProp, pkg),
  24297. filepath: absPath,
  24298. });
  24299. }
  24300. const result = {
  24301. config: null,
  24302. filepath: absPath,
  24303. };
  24304. // handle other type of configs
  24305. const isEmpty = content.trim() === '';
  24306. if (isEmpty && ignoreEmptySearchPlaces)
  24307. return emplace(
  24308. loadCache,
  24309. absPath,
  24310. transform({
  24311. filepath: absPath,
  24312. config: undefined,
  24313. isEmpty: true,
  24314. }),
  24315. );
  24316. // cosmiconfig returns undefined for empty files
  24317. result.config = isEmpty ? undefined : loader(absPath, content);
  24318. return emplace(
  24319. loadCache,
  24320. absPath,
  24321. transform(isEmpty ? {...result, isEmpty, config: undefined} : result),
  24322. );
  24323. },
  24324. clearLoadCache() {
  24325. if (cache) loadCache.clear();
  24326. },
  24327. clearSearchCache() {
  24328. if (cache) searchCache.clear();
  24329. },
  24330. clearCaches() {
  24331. if (cache) {
  24332. loadCache.clear();
  24333. searchCache.clear();
  24334. }
  24335. },
  24336. };
  24337. };
  24338. const ALIAS = Symbol.for('yaml.alias');
  24339. const DOC = Symbol.for('yaml.document');
  24340. const MAP = Symbol.for('yaml.map');
  24341. const PAIR = Symbol.for('yaml.pair');
  24342. const SCALAR$1 = Symbol.for('yaml.scalar');
  24343. const SEQ = Symbol.for('yaml.seq');
  24344. const NODE_TYPE = Symbol.for('yaml.node.type');
  24345. const isAlias = (node) => !!node && typeof node === 'object' && node[NODE_TYPE] === ALIAS;
  24346. const isDocument = (node) => !!node && typeof node === 'object' && node[NODE_TYPE] === DOC;
  24347. const isMap = (node) => !!node && typeof node === 'object' && node[NODE_TYPE] === MAP;
  24348. const isPair = (node) => !!node && typeof node === 'object' && node[NODE_TYPE] === PAIR;
  24349. const isScalar$1 = (node) => !!node && typeof node === 'object' && node[NODE_TYPE] === SCALAR$1;
  24350. const isSeq = (node) => !!node && typeof node === 'object' && node[NODE_TYPE] === SEQ;
  24351. function isCollection$1(node) {
  24352. if (node && typeof node === 'object')
  24353. switch (node[NODE_TYPE]) {
  24354. case MAP:
  24355. case SEQ:
  24356. return true;
  24357. }
  24358. return false;
  24359. }
  24360. function isNode$1(node) {
  24361. if (node && typeof node === 'object')
  24362. switch (node[NODE_TYPE]) {
  24363. case ALIAS:
  24364. case MAP:
  24365. case SCALAR$1:
  24366. case SEQ:
  24367. return true;
  24368. }
  24369. return false;
  24370. }
  24371. const hasAnchor = (node) => (isScalar$1(node) || isCollection$1(node)) && !!node.anchor;
  24372. const BREAK$1 = Symbol('break visit');
  24373. const SKIP$1 = Symbol('skip children');
  24374. const REMOVE$1 = Symbol('remove node');
  24375. /**
  24376. * Apply a visitor to an AST node or document.
  24377. *
  24378. * Walks through the tree (depth-first) starting from `node`, calling a
  24379. * `visitor` function with three arguments:
  24380. * - `key`: For sequence values and map `Pair`, the node's index in the
  24381. * collection. Within a `Pair`, `'key'` or `'value'`, correspondingly.
  24382. * `null` for the root node.
  24383. * - `node`: The current node.
  24384. * - `path`: The ancestry of the current node.
  24385. *
  24386. * The return value of the visitor may be used to control the traversal:
  24387. * - `undefined` (default): Do nothing and continue
  24388. * - `visit.SKIP`: Do not visit the children of this node, continue with next
  24389. * sibling
  24390. * - `visit.BREAK`: Terminate traversal completely
  24391. * - `visit.REMOVE`: Remove the current node, then continue with the next one
  24392. * - `Node`: Replace the current node, then continue by visiting it
  24393. * - `number`: While iterating the items of a sequence or map, set the index
  24394. * of the next step. This is useful especially if the index of the current
  24395. * node has changed.
  24396. *
  24397. * If `visitor` is a single function, it will be called with all values
  24398. * encountered in the tree, including e.g. `null` values. Alternatively,
  24399. * separate visitor functions may be defined for each `Map`, `Pair`, `Seq`,
  24400. * `Alias` and `Scalar` node. To define the same visitor function for more than
  24401. * one node type, use the `Collection` (map and seq), `Value` (map, seq & scalar)
  24402. * and `Node` (alias, map, seq & scalar) targets. Of all these, only the most
  24403. * specific defined one will be used for each node.
  24404. */
  24405. function visit$1(node, visitor) {
  24406. const visitor_ = initVisitor(visitor);
  24407. if (isDocument(node)) {
  24408. const cd = visit_(null, node.contents, visitor_, Object.freeze([node]));
  24409. if (cd === REMOVE$1)
  24410. node.contents = null;
  24411. }
  24412. else
  24413. visit_(null, node, visitor_, Object.freeze([]));
  24414. }
  24415. // Without the `as symbol` casts, TS declares these in the `visit`
  24416. // namespace using `var`, but then complains about that because
  24417. // `unique symbol` must be `const`.
  24418. /** Terminate visit traversal completely */
  24419. visit$1.BREAK = BREAK$1;
  24420. /** Do not visit the children of the current node */
  24421. visit$1.SKIP = SKIP$1;
  24422. /** Remove the current node */
  24423. visit$1.REMOVE = REMOVE$1;
  24424. function visit_(key, node, visitor, path) {
  24425. const ctrl = callVisitor(key, node, visitor, path);
  24426. if (isNode$1(ctrl) || isPair(ctrl)) {
  24427. replaceNode(key, path, ctrl);
  24428. return visit_(key, ctrl, visitor, path);
  24429. }
  24430. if (typeof ctrl !== 'symbol') {
  24431. if (isCollection$1(node)) {
  24432. path = Object.freeze(path.concat(node));
  24433. for (let i = 0; i < node.items.length; ++i) {
  24434. const ci = visit_(i, node.items[i], visitor, path);
  24435. if (typeof ci === 'number')
  24436. i = ci - 1;
  24437. else if (ci === BREAK$1)
  24438. return BREAK$1;
  24439. else if (ci === REMOVE$1) {
  24440. node.items.splice(i, 1);
  24441. i -= 1;
  24442. }
  24443. }
  24444. }
  24445. else if (isPair(node)) {
  24446. path = Object.freeze(path.concat(node));
  24447. const ck = visit_('key', node.key, visitor, path);
  24448. if (ck === BREAK$1)
  24449. return BREAK$1;
  24450. else if (ck === REMOVE$1)
  24451. node.key = null;
  24452. const cv = visit_('value', node.value, visitor, path);
  24453. if (cv === BREAK$1)
  24454. return BREAK$1;
  24455. else if (cv === REMOVE$1)
  24456. node.value = null;
  24457. }
  24458. }
  24459. return ctrl;
  24460. }
  24461. /**
  24462. * Apply an async visitor to an AST node or document.
  24463. *
  24464. * Walks through the tree (depth-first) starting from `node`, calling a
  24465. * `visitor` function with three arguments:
  24466. * - `key`: For sequence values and map `Pair`, the node's index in the
  24467. * collection. Within a `Pair`, `'key'` or `'value'`, correspondingly.
  24468. * `null` for the root node.
  24469. * - `node`: The current node.
  24470. * - `path`: The ancestry of the current node.
  24471. *
  24472. * The return value of the visitor may be used to control the traversal:
  24473. * - `Promise`: Must resolve to one of the following values
  24474. * - `undefined` (default): Do nothing and continue
  24475. * - `visit.SKIP`: Do not visit the children of this node, continue with next
  24476. * sibling
  24477. * - `visit.BREAK`: Terminate traversal completely
  24478. * - `visit.REMOVE`: Remove the current node, then continue with the next one
  24479. * - `Node`: Replace the current node, then continue by visiting it
  24480. * - `number`: While iterating the items of a sequence or map, set the index
  24481. * of the next step. This is useful especially if the index of the current
  24482. * node has changed.
  24483. *
  24484. * If `visitor` is a single function, it will be called with all values
  24485. * encountered in the tree, including e.g. `null` values. Alternatively,
  24486. * separate visitor functions may be defined for each `Map`, `Pair`, `Seq`,
  24487. * `Alias` and `Scalar` node. To define the same visitor function for more than
  24488. * one node type, use the `Collection` (map and seq), `Value` (map, seq & scalar)
  24489. * and `Node` (alias, map, seq & scalar) targets. Of all these, only the most
  24490. * specific defined one will be used for each node.
  24491. */
  24492. async function visitAsync(node, visitor) {
  24493. const visitor_ = initVisitor(visitor);
  24494. if (isDocument(node)) {
  24495. const cd = await visitAsync_(null, node.contents, visitor_, Object.freeze([node]));
  24496. if (cd === REMOVE$1)
  24497. node.contents = null;
  24498. }
  24499. else
  24500. await visitAsync_(null, node, visitor_, Object.freeze([]));
  24501. }
  24502. // Without the `as symbol` casts, TS declares these in the `visit`
  24503. // namespace using `var`, but then complains about that because
  24504. // `unique symbol` must be `const`.
  24505. /** Terminate visit traversal completely */
  24506. visitAsync.BREAK = BREAK$1;
  24507. /** Do not visit the children of the current node */
  24508. visitAsync.SKIP = SKIP$1;
  24509. /** Remove the current node */
  24510. visitAsync.REMOVE = REMOVE$1;
  24511. async function visitAsync_(key, node, visitor, path) {
  24512. const ctrl = await callVisitor(key, node, visitor, path);
  24513. if (isNode$1(ctrl) || isPair(ctrl)) {
  24514. replaceNode(key, path, ctrl);
  24515. return visitAsync_(key, ctrl, visitor, path);
  24516. }
  24517. if (typeof ctrl !== 'symbol') {
  24518. if (isCollection$1(node)) {
  24519. path = Object.freeze(path.concat(node));
  24520. for (let i = 0; i < node.items.length; ++i) {
  24521. const ci = await visitAsync_(i, node.items[i], visitor, path);
  24522. if (typeof ci === 'number')
  24523. i = ci - 1;
  24524. else if (ci === BREAK$1)
  24525. return BREAK$1;
  24526. else if (ci === REMOVE$1) {
  24527. node.items.splice(i, 1);
  24528. i -= 1;
  24529. }
  24530. }
  24531. }
  24532. else if (isPair(node)) {
  24533. path = Object.freeze(path.concat(node));
  24534. const ck = await visitAsync_('key', node.key, visitor, path);
  24535. if (ck === BREAK$1)
  24536. return BREAK$1;
  24537. else if (ck === REMOVE$1)
  24538. node.key = null;
  24539. const cv = await visitAsync_('value', node.value, visitor, path);
  24540. if (cv === BREAK$1)
  24541. return BREAK$1;
  24542. else if (cv === REMOVE$1)
  24543. node.value = null;
  24544. }
  24545. }
  24546. return ctrl;
  24547. }
  24548. function initVisitor(visitor) {
  24549. if (typeof visitor === 'object' &&
  24550. (visitor.Collection || visitor.Node || visitor.Value)) {
  24551. return Object.assign({
  24552. Alias: visitor.Node,
  24553. Map: visitor.Node,
  24554. Scalar: visitor.Node,
  24555. Seq: visitor.Node
  24556. }, visitor.Value && {
  24557. Map: visitor.Value,
  24558. Scalar: visitor.Value,
  24559. Seq: visitor.Value
  24560. }, visitor.Collection && {
  24561. Map: visitor.Collection,
  24562. Seq: visitor.Collection
  24563. }, visitor);
  24564. }
  24565. return visitor;
  24566. }
  24567. function callVisitor(key, node, visitor, path) {
  24568. if (typeof visitor === 'function')
  24569. return visitor(key, node, path);
  24570. if (isMap(node))
  24571. return visitor.Map?.(key, node, path);
  24572. if (isSeq(node))
  24573. return visitor.Seq?.(key, node, path);
  24574. if (isPair(node))
  24575. return visitor.Pair?.(key, node, path);
  24576. if (isScalar$1(node))
  24577. return visitor.Scalar?.(key, node, path);
  24578. if (isAlias(node))
  24579. return visitor.Alias?.(key, node, path);
  24580. return undefined;
  24581. }
  24582. function replaceNode(key, path, node) {
  24583. const parent = path[path.length - 1];
  24584. if (isCollection$1(parent)) {
  24585. parent.items[key] = node;
  24586. }
  24587. else if (isPair(parent)) {
  24588. if (key === 'key')
  24589. parent.key = node;
  24590. else
  24591. parent.value = node;
  24592. }
  24593. else if (isDocument(parent)) {
  24594. parent.contents = node;
  24595. }
  24596. else {
  24597. const pt = isAlias(parent) ? 'alias' : 'scalar';
  24598. throw new Error(`Cannot replace node with ${pt} parent`);
  24599. }
  24600. }
  24601. const escapeChars = {
  24602. '!': '%21',
  24603. ',': '%2C',
  24604. '[': '%5B',
  24605. ']': '%5D',
  24606. '{': '%7B',
  24607. '}': '%7D'
  24608. };
  24609. const escapeTagName = (tn) => tn.replace(/[!,[\]{}]/g, ch => escapeChars[ch]);
  24610. class Directives {
  24611. constructor(yaml, tags) {
  24612. /**
  24613. * The directives-end/doc-start marker `---`. If `null`, a marker may still be
  24614. * included in the document's stringified representation.
  24615. */
  24616. this.docStart = null;
  24617. /** The doc-end marker `...`. */
  24618. this.docEnd = false;
  24619. this.yaml = Object.assign({}, Directives.defaultYaml, yaml);
  24620. this.tags = Object.assign({}, Directives.defaultTags, tags);
  24621. }
  24622. clone() {
  24623. const copy = new Directives(this.yaml, this.tags);
  24624. copy.docStart = this.docStart;
  24625. return copy;
  24626. }
  24627. /**
  24628. * During parsing, get a Directives instance for the current document and
  24629. * update the stream state according to the current version's spec.
  24630. */
  24631. atDocument() {
  24632. const res = new Directives(this.yaml, this.tags);
  24633. switch (this.yaml.version) {
  24634. case '1.1':
  24635. this.atNextDocument = true;
  24636. break;
  24637. case '1.2':
  24638. this.atNextDocument = false;
  24639. this.yaml = {
  24640. explicit: Directives.defaultYaml.explicit,
  24641. version: '1.2'
  24642. };
  24643. this.tags = Object.assign({}, Directives.defaultTags);
  24644. break;
  24645. }
  24646. return res;
  24647. }
  24648. /**
  24649. * @param onError - May be called even if the action was successful
  24650. * @returns `true` on success
  24651. */
  24652. add(line, onError) {
  24653. if (this.atNextDocument) {
  24654. this.yaml = { explicit: Directives.defaultYaml.explicit, version: '1.1' };
  24655. this.tags = Object.assign({}, Directives.defaultTags);
  24656. this.atNextDocument = false;
  24657. }
  24658. const parts = line.trim().split(/[ \t]+/);
  24659. const name = parts.shift();
  24660. switch (name) {
  24661. case '%TAG': {
  24662. if (parts.length !== 2) {
  24663. onError(0, '%TAG directive should contain exactly two parts');
  24664. if (parts.length < 2)
  24665. return false;
  24666. }
  24667. const [handle, prefix] = parts;
  24668. this.tags[handle] = prefix;
  24669. return true;
  24670. }
  24671. case '%YAML': {
  24672. this.yaml.explicit = true;
  24673. if (parts.length !== 1) {
  24674. onError(0, '%YAML directive should contain exactly one part');
  24675. return false;
  24676. }
  24677. const [version] = parts;
  24678. if (version === '1.1' || version === '1.2') {
  24679. this.yaml.version = version;
  24680. return true;
  24681. }
  24682. else {
  24683. const isValid = /^\d+\.\d+$/.test(version);
  24684. onError(6, `Unsupported YAML version ${version}`, isValid);
  24685. return false;
  24686. }
  24687. }
  24688. default:
  24689. onError(0, `Unknown directive ${name}`, true);
  24690. return false;
  24691. }
  24692. }
  24693. /**
  24694. * Resolves a tag, matching handles to those defined in %TAG directives.
  24695. *
  24696. * @returns Resolved tag, which may also be the non-specific tag `'!'` or a
  24697. * `'!local'` tag, or `null` if unresolvable.
  24698. */
  24699. tagName(source, onError) {
  24700. if (source === '!')
  24701. return '!'; // non-specific tag
  24702. if (source[0] !== '!') {
  24703. onError(`Not a valid tag: ${source}`);
  24704. return null;
  24705. }
  24706. if (source[1] === '<') {
  24707. const verbatim = source.slice(2, -1);
  24708. if (verbatim === '!' || verbatim === '!!') {
  24709. onError(`Verbatim tags aren't resolved, so ${source} is invalid.`);
  24710. return null;
  24711. }
  24712. if (source[source.length - 1] !== '>')
  24713. onError('Verbatim tags must end with a >');
  24714. return verbatim;
  24715. }
  24716. const [, handle, suffix] = source.match(/^(.*!)([^!]*)$/s);
  24717. if (!suffix)
  24718. onError(`The ${source} tag has no suffix`);
  24719. const prefix = this.tags[handle];
  24720. if (prefix) {
  24721. try {
  24722. return prefix + decodeURIComponent(suffix);
  24723. }
  24724. catch (error) {
  24725. onError(String(error));
  24726. return null;
  24727. }
  24728. }
  24729. if (handle === '!')
  24730. return source; // local tag
  24731. onError(`Could not resolve tag: ${source}`);
  24732. return null;
  24733. }
  24734. /**
  24735. * Given a fully resolved tag, returns its printable string form,
  24736. * taking into account current tag prefixes and defaults.
  24737. */
  24738. tagString(tag) {
  24739. for (const [handle, prefix] of Object.entries(this.tags)) {
  24740. if (tag.startsWith(prefix))
  24741. return handle + escapeTagName(tag.substring(prefix.length));
  24742. }
  24743. return tag[0] === '!' ? tag : `!<${tag}>`;
  24744. }
  24745. toString(doc) {
  24746. const lines = this.yaml.explicit
  24747. ? [`%YAML ${this.yaml.version || '1.2'}`]
  24748. : [];
  24749. const tagEntries = Object.entries(this.tags);
  24750. let tagNames;
  24751. if (doc && tagEntries.length > 0 && isNode$1(doc.contents)) {
  24752. const tags = {};
  24753. visit$1(doc.contents, (_key, node) => {
  24754. if (isNode$1(node) && node.tag)
  24755. tags[node.tag] = true;
  24756. });
  24757. tagNames = Object.keys(tags);
  24758. }
  24759. else
  24760. tagNames = [];
  24761. for (const [handle, prefix] of tagEntries) {
  24762. if (handle === '!!' && prefix === 'tag:yaml.org,2002:')
  24763. continue;
  24764. if (!doc || tagNames.some(tn => tn.startsWith(prefix)))
  24765. lines.push(`%TAG ${handle} ${prefix}`);
  24766. }
  24767. return lines.join('\n');
  24768. }
  24769. }
  24770. Directives.defaultYaml = { explicit: false, version: '1.2' };
  24771. Directives.defaultTags = { '!!': 'tag:yaml.org,2002:' };
  24772. /**
  24773. * Verify that the input string is a valid anchor.
  24774. *
  24775. * Will throw on errors.
  24776. */
  24777. function anchorIsValid(anchor) {
  24778. if (/[\x00-\x19\s,[\]{}]/.test(anchor)) {
  24779. const sa = JSON.stringify(anchor);
  24780. const msg = `Anchor must not contain whitespace or control characters: ${sa}`;
  24781. throw new Error(msg);
  24782. }
  24783. return true;
  24784. }
  24785. function anchorNames(root) {
  24786. const anchors = new Set();
  24787. visit$1(root, {
  24788. Value(_key, node) {
  24789. if (node.anchor)
  24790. anchors.add(node.anchor);
  24791. }
  24792. });
  24793. return anchors;
  24794. }
  24795. /** Find a new anchor name with the given `prefix` and a one-indexed suffix. */
  24796. function findNewAnchor(prefix, exclude) {
  24797. for (let i = 1; true; ++i) {
  24798. const name = `${prefix}${i}`;
  24799. if (!exclude.has(name))
  24800. return name;
  24801. }
  24802. }
  24803. function createNodeAnchors(doc, prefix) {
  24804. const aliasObjects = [];
  24805. const sourceObjects = new Map();
  24806. let prevAnchors = null;
  24807. return {
  24808. onAnchor: (source) => {
  24809. aliasObjects.push(source);
  24810. if (!prevAnchors)
  24811. prevAnchors = anchorNames(doc);
  24812. const anchor = findNewAnchor(prefix, prevAnchors);
  24813. prevAnchors.add(anchor);
  24814. return anchor;
  24815. },
  24816. /**
  24817. * With circular references, the source node is only resolved after all
  24818. * of its child nodes are. This is why anchors are set only after all of
  24819. * the nodes have been created.
  24820. */
  24821. setAnchors: () => {
  24822. for (const source of aliasObjects) {
  24823. const ref = sourceObjects.get(source);
  24824. if (typeof ref === 'object' &&
  24825. ref.anchor &&
  24826. (isScalar$1(ref.node) || isCollection$1(ref.node))) {
  24827. ref.node.anchor = ref.anchor;
  24828. }
  24829. else {
  24830. const error = new Error('Failed to resolve repeated object (this should not happen)');
  24831. error.source = source;
  24832. throw error;
  24833. }
  24834. }
  24835. },
  24836. sourceObjects
  24837. };
  24838. }
  24839. /**
  24840. * Applies the JSON.parse reviver algorithm as defined in the ECMA-262 spec,
  24841. * in section 24.5.1.1 "Runtime Semantics: InternalizeJSONProperty" of the
  24842. * 2021 edition: https://tc39.es/ecma262/#sec-json.parse
  24843. *
  24844. * Includes extensions for handling Map and Set objects.
  24845. */
  24846. function applyReviver(reviver, obj, key, val) {
  24847. if (val && typeof val === 'object') {
  24848. if (Array.isArray(val)) {
  24849. for (let i = 0, len = val.length; i < len; ++i) {
  24850. const v0 = val[i];
  24851. const v1 = applyReviver(reviver, val, String(i), v0);
  24852. if (v1 === undefined)
  24853. delete val[i];
  24854. else if (v1 !== v0)
  24855. val[i] = v1;
  24856. }
  24857. }
  24858. else if (val instanceof Map) {
  24859. for (const k of Array.from(val.keys())) {
  24860. const v0 = val.get(k);
  24861. const v1 = applyReviver(reviver, val, k, v0);
  24862. if (v1 === undefined)
  24863. val.delete(k);
  24864. else if (v1 !== v0)
  24865. val.set(k, v1);
  24866. }
  24867. }
  24868. else if (val instanceof Set) {
  24869. for (const v0 of Array.from(val)) {
  24870. const v1 = applyReviver(reviver, val, v0, v0);
  24871. if (v1 === undefined)
  24872. val.delete(v0);
  24873. else if (v1 !== v0) {
  24874. val.delete(v0);
  24875. val.add(v1);
  24876. }
  24877. }
  24878. }
  24879. else {
  24880. for (const [k, v0] of Object.entries(val)) {
  24881. const v1 = applyReviver(reviver, val, k, v0);
  24882. if (v1 === undefined)
  24883. delete val[k];
  24884. else if (v1 !== v0)
  24885. val[k] = v1;
  24886. }
  24887. }
  24888. }
  24889. return reviver.call(obj, key, val);
  24890. }
  24891. /**
  24892. * Recursively convert any node or its contents to native JavaScript
  24893. *
  24894. * @param value - The input value
  24895. * @param arg - If `value` defines a `toJSON()` method, use this
  24896. * as its first argument
  24897. * @param ctx - Conversion context, originally set in Document#toJS(). If
  24898. * `{ keep: true }` is not set, output should be suitable for JSON
  24899. * stringification.
  24900. */
  24901. function toJS(value, arg, ctx) {
  24902. // eslint-disable-next-line @typescript-eslint/no-unsafe-return
  24903. if (Array.isArray(value))
  24904. return value.map((v, i) => toJS(v, String(i), ctx));
  24905. if (value && typeof value.toJSON === 'function') {
  24906. // eslint-disable-next-line @typescript-eslint/no-unsafe-call
  24907. if (!ctx || !hasAnchor(value))
  24908. return value.toJSON(arg, ctx);
  24909. const data = { aliasCount: 0, count: 1, res: undefined };
  24910. ctx.anchors.set(value, data);
  24911. ctx.onCreate = res => {
  24912. data.res = res;
  24913. delete ctx.onCreate;
  24914. };
  24915. const res = value.toJSON(arg, ctx);
  24916. if (ctx.onCreate)
  24917. ctx.onCreate(res);
  24918. return res;
  24919. }
  24920. if (typeof value === 'bigint' && !ctx?.keep)
  24921. return Number(value);
  24922. return value;
  24923. }
  24924. class NodeBase {
  24925. constructor(type) {
  24926. Object.defineProperty(this, NODE_TYPE, { value: type });
  24927. }
  24928. /** Create a copy of this node. */
  24929. clone() {
  24930. const copy = Object.create(Object.getPrototypeOf(this), Object.getOwnPropertyDescriptors(this));
  24931. if (this.range)
  24932. copy.range = this.range.slice();
  24933. return copy;
  24934. }
  24935. /** A plain JavaScript representation of this node. */
  24936. toJS(doc, { mapAsMap, maxAliasCount, onAnchor, reviver } = {}) {
  24937. if (!isDocument(doc))
  24938. throw new TypeError('A document argument is required');
  24939. const ctx = {
  24940. anchors: new Map(),
  24941. doc,
  24942. keep: true,
  24943. mapAsMap: mapAsMap === true,
  24944. mapKeyWarned: false,
  24945. maxAliasCount: typeof maxAliasCount === 'number' ? maxAliasCount : 100
  24946. };
  24947. const res = toJS(this, '', ctx);
  24948. if (typeof onAnchor === 'function')
  24949. for (const { count, res } of ctx.anchors.values())
  24950. onAnchor(res, count);
  24951. return typeof reviver === 'function'
  24952. ? applyReviver(reviver, { '': res }, '', res)
  24953. : res;
  24954. }
  24955. }
  24956. class Alias extends NodeBase {
  24957. constructor(source) {
  24958. super(ALIAS);
  24959. this.source = source;
  24960. Object.defineProperty(this, 'tag', {
  24961. set() {
  24962. throw new Error('Alias nodes cannot have tags');
  24963. }
  24964. });
  24965. }
  24966. /**
  24967. * Resolve the value of this alias within `doc`, finding the last
  24968. * instance of the `source` anchor before this node.
  24969. */
  24970. resolve(doc) {
  24971. let found = undefined;
  24972. visit$1(doc, {
  24973. Node: (_key, node) => {
  24974. if (node === this)
  24975. return visit$1.BREAK;
  24976. if (node.anchor === this.source)
  24977. found = node;
  24978. }
  24979. });
  24980. return found;
  24981. }
  24982. toJSON(_arg, ctx) {
  24983. if (!ctx)
  24984. return { source: this.source };
  24985. const { anchors, doc, maxAliasCount } = ctx;
  24986. const source = this.resolve(doc);
  24987. if (!source) {
  24988. const msg = `Unresolved alias (the anchor must be set before the alias): ${this.source}`;
  24989. throw new ReferenceError(msg);
  24990. }
  24991. let data = anchors.get(source);
  24992. if (!data) {
  24993. // Resolve anchors for Node.prototype.toJS()
  24994. toJS(source, null, ctx);
  24995. data = anchors.get(source);
  24996. }
  24997. /* istanbul ignore if */
  24998. if (!data || data.res === undefined) {
  24999. const msg = 'This should not happen: Alias anchor was not resolved?';
  25000. throw new ReferenceError(msg);
  25001. }
  25002. if (maxAliasCount >= 0) {
  25003. data.count += 1;
  25004. if (data.aliasCount === 0)
  25005. data.aliasCount = getAliasCount(doc, source, anchors);
  25006. if (data.count * data.aliasCount > maxAliasCount) {
  25007. const msg = 'Excessive alias count indicates a resource exhaustion attack';
  25008. throw new ReferenceError(msg);
  25009. }
  25010. }
  25011. return data.res;
  25012. }
  25013. toString(ctx, _onComment, _onChompKeep) {
  25014. const src = `*${this.source}`;
  25015. if (ctx) {
  25016. anchorIsValid(this.source);
  25017. if (ctx.options.verifyAliasOrder && !ctx.anchors.has(this.source)) {
  25018. const msg = `Unresolved alias (the anchor must be set before the alias): ${this.source}`;
  25019. throw new Error(msg);
  25020. }
  25021. if (ctx.implicitKey)
  25022. return `${src} `;
  25023. }
  25024. return src;
  25025. }
  25026. }
  25027. function getAliasCount(doc, node, anchors) {
  25028. if (isAlias(node)) {
  25029. const source = node.resolve(doc);
  25030. const anchor = anchors && source && anchors.get(source);
  25031. return anchor ? anchor.count * anchor.aliasCount : 0;
  25032. }
  25033. else if (isCollection$1(node)) {
  25034. let count = 0;
  25035. for (const item of node.items) {
  25036. const c = getAliasCount(doc, item, anchors);
  25037. if (c > count)
  25038. count = c;
  25039. }
  25040. return count;
  25041. }
  25042. else if (isPair(node)) {
  25043. const kc = getAliasCount(doc, node.key, anchors);
  25044. const vc = getAliasCount(doc, node.value, anchors);
  25045. return Math.max(kc, vc);
  25046. }
  25047. return 1;
  25048. }
  25049. const isScalarValue = (value) => !value || (typeof value !== 'function' && typeof value !== 'object');
  25050. class Scalar extends NodeBase {
  25051. constructor(value) {
  25052. super(SCALAR$1);
  25053. this.value = value;
  25054. }
  25055. toJSON(arg, ctx) {
  25056. return ctx?.keep ? this.value : toJS(this.value, arg, ctx);
  25057. }
  25058. toString() {
  25059. return String(this.value);
  25060. }
  25061. }
  25062. Scalar.BLOCK_FOLDED = 'BLOCK_FOLDED';
  25063. Scalar.BLOCK_LITERAL = 'BLOCK_LITERAL';
  25064. Scalar.PLAIN = 'PLAIN';
  25065. Scalar.QUOTE_DOUBLE = 'QUOTE_DOUBLE';
  25066. Scalar.QUOTE_SINGLE = 'QUOTE_SINGLE';
  25067. const defaultTagPrefix = 'tag:yaml.org,2002:';
  25068. function findTagObject(value, tagName, tags) {
  25069. if (tagName) {
  25070. const match = tags.filter(t => t.tag === tagName);
  25071. const tagObj = match.find(t => !t.format) ?? match[0];
  25072. if (!tagObj)
  25073. throw new Error(`Tag ${tagName} not found`);
  25074. return tagObj;
  25075. }
  25076. return tags.find(t => t.identify?.(value) && !t.format);
  25077. }
  25078. function createNode(value, tagName, ctx) {
  25079. if (isDocument(value))
  25080. value = value.contents;
  25081. if (isNode$1(value))
  25082. return value;
  25083. if (isPair(value)) {
  25084. const map = ctx.schema[MAP].createNode?.(ctx.schema, null, ctx);
  25085. map.items.push(value);
  25086. return map;
  25087. }
  25088. if (value instanceof String ||
  25089. value instanceof Number ||
  25090. value instanceof Boolean ||
  25091. (typeof BigInt !== 'undefined' && value instanceof BigInt) // not supported everywhere
  25092. ) {
  25093. // https://tc39.es/ecma262/#sec-serializejsonproperty
  25094. value = value.valueOf();
  25095. }
  25096. const { aliasDuplicateObjects, onAnchor, onTagObj, schema, sourceObjects } = ctx;
  25097. // Detect duplicate references to the same object & use Alias nodes for all
  25098. // after first. The `ref` wrapper allows for circular references to resolve.
  25099. let ref = undefined;
  25100. if (aliasDuplicateObjects && value && typeof value === 'object') {
  25101. ref = sourceObjects.get(value);
  25102. if (ref) {
  25103. if (!ref.anchor)
  25104. ref.anchor = onAnchor(value);
  25105. return new Alias(ref.anchor);
  25106. }
  25107. else {
  25108. ref = { anchor: null, node: null };
  25109. sourceObjects.set(value, ref);
  25110. }
  25111. }
  25112. if (tagName?.startsWith('!!'))
  25113. tagName = defaultTagPrefix + tagName.slice(2);
  25114. let tagObj = findTagObject(value, tagName, schema.tags);
  25115. if (!tagObj) {
  25116. if (value && typeof value.toJSON === 'function') {
  25117. // eslint-disable-next-line @typescript-eslint/no-unsafe-call
  25118. value = value.toJSON();
  25119. }
  25120. if (!value || typeof value !== 'object') {
  25121. const node = new Scalar(value);
  25122. if (ref)
  25123. ref.node = node;
  25124. return node;
  25125. }
  25126. tagObj =
  25127. value instanceof Map
  25128. ? schema[MAP]
  25129. : Symbol.iterator in Object(value)
  25130. ? schema[SEQ]
  25131. : schema[MAP];
  25132. }
  25133. if (onTagObj) {
  25134. onTagObj(tagObj);
  25135. delete ctx.onTagObj;
  25136. }
  25137. const node = tagObj?.createNode
  25138. ? tagObj.createNode(ctx.schema, value, ctx)
  25139. : typeof tagObj?.nodeClass?.from === 'function'
  25140. ? tagObj.nodeClass.from(ctx.schema, value, ctx)
  25141. : new Scalar(value);
  25142. if (tagName)
  25143. node.tag = tagName;
  25144. else if (!tagObj.default)
  25145. node.tag = tagObj.tag;
  25146. if (ref)
  25147. ref.node = node;
  25148. return node;
  25149. }
  25150. function collectionFromPath(schema, path, value) {
  25151. let v = value;
  25152. for (let i = path.length - 1; i >= 0; --i) {
  25153. const k = path[i];
  25154. if (typeof k === 'number' && Number.isInteger(k) && k >= 0) {
  25155. const a = [];
  25156. a[k] = v;
  25157. v = a;
  25158. }
  25159. else {
  25160. v = new Map([[k, v]]);
  25161. }
  25162. }
  25163. return createNode(v, undefined, {
  25164. aliasDuplicateObjects: false,
  25165. keepUndefined: false,
  25166. onAnchor: () => {
  25167. throw new Error('This should not happen, please report a bug.');
  25168. },
  25169. schema,
  25170. sourceObjects: new Map()
  25171. });
  25172. }
  25173. // Type guard is intentionally a little wrong so as to be more useful,
  25174. // as it does not cover untypable empty non-string iterables (e.g. []).
  25175. const isEmptyPath = (path) => path == null ||
  25176. (typeof path === 'object' && !!path[Symbol.iterator]().next().done);
  25177. class Collection extends NodeBase {
  25178. constructor(type, schema) {
  25179. super(type);
  25180. Object.defineProperty(this, 'schema', {
  25181. value: schema,
  25182. configurable: true,
  25183. enumerable: false,
  25184. writable: true
  25185. });
  25186. }
  25187. /**
  25188. * Create a copy of this collection.
  25189. *
  25190. * @param schema - If defined, overwrites the original's schema
  25191. */
  25192. clone(schema) {
  25193. const copy = Object.create(Object.getPrototypeOf(this), Object.getOwnPropertyDescriptors(this));
  25194. if (schema)
  25195. copy.schema = schema;
  25196. copy.items = copy.items.map(it => isNode$1(it) || isPair(it) ? it.clone(schema) : it);
  25197. if (this.range)
  25198. copy.range = this.range.slice();
  25199. return copy;
  25200. }
  25201. /**
  25202. * Adds a value to the collection. For `!!map` and `!!omap` the value must
  25203. * be a Pair instance or a `{ key, value }` object, which may not have a key
  25204. * that already exists in the map.
  25205. */
  25206. addIn(path, value) {
  25207. if (isEmptyPath(path))
  25208. this.add(value);
  25209. else {
  25210. const [key, ...rest] = path;
  25211. const node = this.get(key, true);
  25212. if (isCollection$1(node))
  25213. node.addIn(rest, value);
  25214. else if (node === undefined && this.schema)
  25215. this.set(key, collectionFromPath(this.schema, rest, value));
  25216. else
  25217. throw new Error(`Expected YAML collection at ${key}. Remaining path: ${rest}`);
  25218. }
  25219. }
  25220. /**
  25221. * Removes a value from the collection.
  25222. * @returns `true` if the item was found and removed.
  25223. */
  25224. deleteIn(path) {
  25225. const [key, ...rest] = path;
  25226. if (rest.length === 0)
  25227. return this.delete(key);
  25228. const node = this.get(key, true);
  25229. if (isCollection$1(node))
  25230. return node.deleteIn(rest);
  25231. else
  25232. throw new Error(`Expected YAML collection at ${key}. Remaining path: ${rest}`);
  25233. }
  25234. /**
  25235. * Returns item at `key`, or `undefined` if not found. By default unwraps
  25236. * scalar values from their surrounding node; to disable set `keepScalar` to
  25237. * `true` (collections are always returned intact).
  25238. */
  25239. getIn(path, keepScalar) {
  25240. const [key, ...rest] = path;
  25241. const node = this.get(key, true);
  25242. if (rest.length === 0)
  25243. return !keepScalar && isScalar$1(node) ? node.value : node;
  25244. else
  25245. return isCollection$1(node) ? node.getIn(rest, keepScalar) : undefined;
  25246. }
  25247. hasAllNullValues(allowScalar) {
  25248. return this.items.every(node => {
  25249. if (!isPair(node))
  25250. return false;
  25251. const n = node.value;
  25252. return (n == null ||
  25253. (allowScalar &&
  25254. isScalar$1(n) &&
  25255. n.value == null &&
  25256. !n.commentBefore &&
  25257. !n.comment &&
  25258. !n.tag));
  25259. });
  25260. }
  25261. /**
  25262. * Checks if the collection includes a value with the key `key`.
  25263. */
  25264. hasIn(path) {
  25265. const [key, ...rest] = path;
  25266. if (rest.length === 0)
  25267. return this.has(key);
  25268. const node = this.get(key, true);
  25269. return isCollection$1(node) ? node.hasIn(rest) : false;
  25270. }
  25271. /**
  25272. * Sets a value in this collection. For `!!set`, `value` needs to be a
  25273. * boolean to add/remove the item from the set.
  25274. */
  25275. setIn(path, value) {
  25276. const [key, ...rest] = path;
  25277. if (rest.length === 0) {
  25278. this.set(key, value);
  25279. }
  25280. else {
  25281. const node = this.get(key, true);
  25282. if (isCollection$1(node))
  25283. node.setIn(rest, value);
  25284. else if (node === undefined && this.schema)
  25285. this.set(key, collectionFromPath(this.schema, rest, value));
  25286. else
  25287. throw new Error(`Expected YAML collection at ${key}. Remaining path: ${rest}`);
  25288. }
  25289. }
  25290. }
  25291. /**
  25292. * Stringifies a comment.
  25293. *
  25294. * Empty comment lines are left empty,
  25295. * lines consisting of a single space are replaced by `#`,
  25296. * and all other lines are prefixed with a `#`.
  25297. */
  25298. const stringifyComment = (str) => str.replace(/^(?!$)(?: $)?/gm, '#');
  25299. function indentComment(comment, indent) {
  25300. if (/^\n+$/.test(comment))
  25301. return comment.substring(1);
  25302. return indent ? comment.replace(/^(?! *$)/gm, indent) : comment;
  25303. }
  25304. const lineComment = (str, indent, comment) => str.endsWith('\n')
  25305. ? indentComment(comment, indent)
  25306. : comment.includes('\n')
  25307. ? '\n' + indentComment(comment, indent)
  25308. : (str.endsWith(' ') ? '' : ' ') + comment;
  25309. const FOLD_FLOW = 'flow';
  25310. const FOLD_BLOCK = 'block';
  25311. const FOLD_QUOTED = 'quoted';
  25312. /**
  25313. * Tries to keep input at up to `lineWidth` characters, splitting only on spaces
  25314. * not followed by newlines or spaces unless `mode` is `'quoted'`. Lines are
  25315. * terminated with `\n` and started with `indent`.
  25316. */
  25317. function foldFlowLines(text, indent, mode = 'flow', { indentAtStart, lineWidth = 80, minContentWidth = 20, onFold, onOverflow } = {}) {
  25318. if (!lineWidth || lineWidth < 0)
  25319. return text;
  25320. if (lineWidth < minContentWidth)
  25321. minContentWidth = 0;
  25322. const endStep = Math.max(1 + minContentWidth, 1 + lineWidth - indent.length);
  25323. if (text.length <= endStep)
  25324. return text;
  25325. const folds = [];
  25326. const escapedFolds = {};
  25327. let end = lineWidth - indent.length;
  25328. if (typeof indentAtStart === 'number') {
  25329. if (indentAtStart > lineWidth - Math.max(2, minContentWidth))
  25330. folds.push(0);
  25331. else
  25332. end = lineWidth - indentAtStart;
  25333. }
  25334. let split = undefined;
  25335. let prev = undefined;
  25336. let overflow = false;
  25337. let i = -1;
  25338. let escStart = -1;
  25339. let escEnd = -1;
  25340. if (mode === FOLD_BLOCK) {
  25341. i = consumeMoreIndentedLines(text, i, indent.length);
  25342. if (i !== -1)
  25343. end = i + endStep;
  25344. }
  25345. for (let ch; (ch = text[(i += 1)]);) {
  25346. if (mode === FOLD_QUOTED && ch === '\\') {
  25347. escStart = i;
  25348. switch (text[i + 1]) {
  25349. case 'x':
  25350. i += 3;
  25351. break;
  25352. case 'u':
  25353. i += 5;
  25354. break;
  25355. case 'U':
  25356. i += 9;
  25357. break;
  25358. default:
  25359. i += 1;
  25360. }
  25361. escEnd = i;
  25362. }
  25363. if (ch === '\n') {
  25364. if (mode === FOLD_BLOCK)
  25365. i = consumeMoreIndentedLines(text, i, indent.length);
  25366. end = i + indent.length + endStep;
  25367. split = undefined;
  25368. }
  25369. else {
  25370. if (ch === ' ' &&
  25371. prev &&
  25372. prev !== ' ' &&
  25373. prev !== '\n' &&
  25374. prev !== '\t') {
  25375. // space surrounded by non-space can be replaced with newline + indent
  25376. const next = text[i + 1];
  25377. if (next && next !== ' ' && next !== '\n' && next !== '\t')
  25378. split = i;
  25379. }
  25380. if (i >= end) {
  25381. if (split) {
  25382. folds.push(split);
  25383. end = split + endStep;
  25384. split = undefined;
  25385. }
  25386. else if (mode === FOLD_QUOTED) {
  25387. // white-space collected at end may stretch past lineWidth
  25388. while (prev === ' ' || prev === '\t') {
  25389. prev = ch;
  25390. ch = text[(i += 1)];
  25391. overflow = true;
  25392. }
  25393. // Account for newline escape, but don't break preceding escape
  25394. const j = i > escEnd + 1 ? i - 2 : escStart - 1;
  25395. // Bail out if lineWidth & minContentWidth are shorter than an escape string
  25396. if (escapedFolds[j])
  25397. return text;
  25398. folds.push(j);
  25399. escapedFolds[j] = true;
  25400. end = j + endStep;
  25401. split = undefined;
  25402. }
  25403. else {
  25404. overflow = true;
  25405. }
  25406. }
  25407. }
  25408. prev = ch;
  25409. }
  25410. if (overflow && onOverflow)
  25411. onOverflow();
  25412. if (folds.length === 0)
  25413. return text;
  25414. if (onFold)
  25415. onFold();
  25416. let res = text.slice(0, folds[0]);
  25417. for (let i = 0; i < folds.length; ++i) {
  25418. const fold = folds[i];
  25419. const end = folds[i + 1] || text.length;
  25420. if (fold === 0)
  25421. res = `\n${indent}${text.slice(0, end)}`;
  25422. else {
  25423. if (mode === FOLD_QUOTED && escapedFolds[fold])
  25424. res += `${text[fold]}\\`;
  25425. res += `\n${indent}${text.slice(fold + 1, end)}`;
  25426. }
  25427. }
  25428. return res;
  25429. }
  25430. /**
  25431. * Presumes `i + 1` is at the start of a line
  25432. * @returns index of last newline in more-indented block
  25433. */
  25434. function consumeMoreIndentedLines(text, i, indent) {
  25435. let end = i;
  25436. let start = i + 1;
  25437. let ch = text[start];
  25438. while (ch === ' ' || ch === '\t') {
  25439. if (i < start + indent) {
  25440. ch = text[++i];
  25441. }
  25442. else {
  25443. do {
  25444. ch = text[++i];
  25445. } while (ch && ch !== '\n');
  25446. end = i;
  25447. start = i + 1;
  25448. ch = text[start];
  25449. }
  25450. }
  25451. return end;
  25452. }
  25453. const getFoldOptions = (ctx, isBlock) => ({
  25454. indentAtStart: isBlock ? ctx.indent.length : ctx.indentAtStart,
  25455. lineWidth: ctx.options.lineWidth,
  25456. minContentWidth: ctx.options.minContentWidth
  25457. });
  25458. // Also checks for lines starting with %, as parsing the output as YAML 1.1 will
  25459. // presume that's starting a new document.
  25460. const containsDocumentMarker = (str) => /^(%|---|\.\.\.)/m.test(str);
  25461. function lineLengthOverLimit(str, lineWidth, indentLength) {
  25462. if (!lineWidth || lineWidth < 0)
  25463. return false;
  25464. const limit = lineWidth - indentLength;
  25465. const strLen = str.length;
  25466. if (strLen <= limit)
  25467. return false;
  25468. for (let i = 0, start = 0; i < strLen; ++i) {
  25469. if (str[i] === '\n') {
  25470. if (i - start > limit)
  25471. return true;
  25472. start = i + 1;
  25473. if (strLen - start <= limit)
  25474. return false;
  25475. }
  25476. }
  25477. return true;
  25478. }
  25479. function doubleQuotedString(value, ctx) {
  25480. const json = JSON.stringify(value);
  25481. if (ctx.options.doubleQuotedAsJSON)
  25482. return json;
  25483. const { implicitKey } = ctx;
  25484. const minMultiLineLength = ctx.options.doubleQuotedMinMultiLineLength;
  25485. const indent = ctx.indent || (containsDocumentMarker(value) ? ' ' : '');
  25486. let str = '';
  25487. let start = 0;
  25488. for (let i = 0, ch = json[i]; ch; ch = json[++i]) {
  25489. if (ch === ' ' && json[i + 1] === '\\' && json[i + 2] === 'n') {
  25490. // space before newline needs to be escaped to not be folded
  25491. str += json.slice(start, i) + '\\ ';
  25492. i += 1;
  25493. start = i;
  25494. ch = '\\';
  25495. }
  25496. if (ch === '\\')
  25497. switch (json[i + 1]) {
  25498. case 'u':
  25499. {
  25500. str += json.slice(start, i);
  25501. const code = json.substr(i + 2, 4);
  25502. switch (code) {
  25503. case '0000':
  25504. str += '\\0';
  25505. break;
  25506. case '0007':
  25507. str += '\\a';
  25508. break;
  25509. case '000b':
  25510. str += '\\v';
  25511. break;
  25512. case '001b':
  25513. str += '\\e';
  25514. break;
  25515. case '0085':
  25516. str += '\\N';
  25517. break;
  25518. case '00a0':
  25519. str += '\\_';
  25520. break;
  25521. case '2028':
  25522. str += '\\L';
  25523. break;
  25524. case '2029':
  25525. str += '\\P';
  25526. break;
  25527. default:
  25528. if (code.substr(0, 2) === '00')
  25529. str += '\\x' + code.substr(2);
  25530. else
  25531. str += json.substr(i, 6);
  25532. }
  25533. i += 5;
  25534. start = i + 1;
  25535. }
  25536. break;
  25537. case 'n':
  25538. if (implicitKey ||
  25539. json[i + 2] === '"' ||
  25540. json.length < minMultiLineLength) {
  25541. i += 1;
  25542. }
  25543. else {
  25544. // folding will eat first newline
  25545. str += json.slice(start, i) + '\n\n';
  25546. while (json[i + 2] === '\\' &&
  25547. json[i + 3] === 'n' &&
  25548. json[i + 4] !== '"') {
  25549. str += '\n';
  25550. i += 2;
  25551. }
  25552. str += indent;
  25553. // space after newline needs to be escaped to not be folded
  25554. if (json[i + 2] === ' ')
  25555. str += '\\';
  25556. i += 1;
  25557. start = i + 1;
  25558. }
  25559. break;
  25560. default:
  25561. i += 1;
  25562. }
  25563. }
  25564. str = start ? str + json.slice(start) : json;
  25565. return implicitKey
  25566. ? str
  25567. : foldFlowLines(str, indent, FOLD_QUOTED, getFoldOptions(ctx, false));
  25568. }
  25569. function singleQuotedString(value, ctx) {
  25570. if (ctx.options.singleQuote === false ||
  25571. (ctx.implicitKey && value.includes('\n')) ||
  25572. /[ \t]\n|\n[ \t]/.test(value) // single quoted string can't have leading or trailing whitespace around newline
  25573. )
  25574. return doubleQuotedString(value, ctx);
  25575. const indent = ctx.indent || (containsDocumentMarker(value) ? ' ' : '');
  25576. const res = "'" + value.replace(/'/g, "''").replace(/\n+/g, `$&\n${indent}`) + "'";
  25577. return ctx.implicitKey
  25578. ? res
  25579. : foldFlowLines(res, indent, FOLD_FLOW, getFoldOptions(ctx, false));
  25580. }
  25581. function quotedString(value, ctx) {
  25582. const { singleQuote } = ctx.options;
  25583. let qs;
  25584. if (singleQuote === false)
  25585. qs = doubleQuotedString;
  25586. else {
  25587. const hasDouble = value.includes('"');
  25588. const hasSingle = value.includes("'");
  25589. if (hasDouble && !hasSingle)
  25590. qs = singleQuotedString;
  25591. else if (hasSingle && !hasDouble)
  25592. qs = doubleQuotedString;
  25593. else
  25594. qs = singleQuote ? singleQuotedString : doubleQuotedString;
  25595. }
  25596. return qs(value, ctx);
  25597. }
  25598. // The negative lookbehind avoids a polynomial search,
  25599. // but isn't supported yet on Safari: https://caniuse.com/js-regexp-lookbehind
  25600. let blockEndNewlines;
  25601. try {
  25602. blockEndNewlines = new RegExp('(^|(?<!\n))\n+(?!\n|$)', 'g');
  25603. }
  25604. catch {
  25605. blockEndNewlines = /\n+(?!\n|$)/g;
  25606. }
  25607. function blockString({ comment, type, value }, ctx, onComment, onChompKeep) {
  25608. const { blockQuote, commentString, lineWidth } = ctx.options;
  25609. // 1. Block can't end in whitespace unless the last line is non-empty.
  25610. // 2. Strings consisting of only whitespace are best rendered explicitly.
  25611. if (!blockQuote || /\n[\t ]+$/.test(value) || /^\s*$/.test(value)) {
  25612. return quotedString(value, ctx);
  25613. }
  25614. const indent = ctx.indent ||
  25615. (ctx.forceBlockIndent || containsDocumentMarker(value) ? ' ' : '');
  25616. const literal = blockQuote === 'literal'
  25617. ? true
  25618. : blockQuote === 'folded' || type === Scalar.BLOCK_FOLDED
  25619. ? false
  25620. : type === Scalar.BLOCK_LITERAL
  25621. ? true
  25622. : !lineLengthOverLimit(value, lineWidth, indent.length);
  25623. if (!value)
  25624. return literal ? '|\n' : '>\n';
  25625. // determine chomping from whitespace at value end
  25626. let chomp;
  25627. let endStart;
  25628. for (endStart = value.length; endStart > 0; --endStart) {
  25629. const ch = value[endStart - 1];
  25630. if (ch !== '\n' && ch !== '\t' && ch !== ' ')
  25631. break;
  25632. }
  25633. let end = value.substring(endStart);
  25634. const endNlPos = end.indexOf('\n');
  25635. if (endNlPos === -1) {
  25636. chomp = '-'; // strip
  25637. }
  25638. else if (value === end || endNlPos !== end.length - 1) {
  25639. chomp = '+'; // keep
  25640. if (onChompKeep)
  25641. onChompKeep();
  25642. }
  25643. else {
  25644. chomp = ''; // clip
  25645. }
  25646. if (end) {
  25647. value = value.slice(0, -end.length);
  25648. if (end[end.length - 1] === '\n')
  25649. end = end.slice(0, -1);
  25650. end = end.replace(blockEndNewlines, `$&${indent}`);
  25651. }
  25652. // determine indent indicator from whitespace at value start
  25653. let startWithSpace = false;
  25654. let startEnd;
  25655. let startNlPos = -1;
  25656. for (startEnd = 0; startEnd < value.length; ++startEnd) {
  25657. const ch = value[startEnd];
  25658. if (ch === ' ')
  25659. startWithSpace = true;
  25660. else if (ch === '\n')
  25661. startNlPos = startEnd;
  25662. else
  25663. break;
  25664. }
  25665. let start = value.substring(0, startNlPos < startEnd ? startNlPos + 1 : startEnd);
  25666. if (start) {
  25667. value = value.substring(start.length);
  25668. start = start.replace(/\n+/g, `$&${indent}`);
  25669. }
  25670. const indentSize = indent ? '2' : '1'; // root is at -1
  25671. let header = (literal ? '|' : '>') + (startWithSpace ? indentSize : '') + chomp;
  25672. if (comment) {
  25673. header += ' ' + commentString(comment.replace(/ ?[\r\n]+/g, ' '));
  25674. if (onComment)
  25675. onComment();
  25676. }
  25677. if (literal) {
  25678. value = value.replace(/\n+/g, `$&${indent}`);
  25679. return `${header}\n${indent}${start}${value}${end}`;
  25680. }
  25681. value = value
  25682. .replace(/\n+/g, '\n$&')
  25683. .replace(/(?:^|\n)([\t ].*)(?:([\n\t ]*)\n(?![\n\t ]))?/g, '$1$2') // more-indented lines aren't folded
  25684. // ^ more-ind. ^ empty ^ capture next empty lines only at end of indent
  25685. .replace(/\n+/g, `$&${indent}`);
  25686. const body = foldFlowLines(`${start}${value}${end}`, indent, FOLD_BLOCK, getFoldOptions(ctx, true));
  25687. return `${header}\n${indent}${body}`;
  25688. }
  25689. function plainString(item, ctx, onComment, onChompKeep) {
  25690. const { type, value } = item;
  25691. const { actualString, implicitKey, indent, indentStep, inFlow } = ctx;
  25692. if ((implicitKey && value.includes('\n')) ||
  25693. (inFlow && /[[\]{},]/.test(value))) {
  25694. return quotedString(value, ctx);
  25695. }
  25696. if (!value ||
  25697. /^[\n\t ,[\]{}#&*!|>'"%@`]|^[?-]$|^[?-][ \t]|[\n:][ \t]|[ \t]\n|[\n\t ]#|[\n\t :]$/.test(value)) {
  25698. // not allowed:
  25699. // - empty string, '-' or '?'
  25700. // - start with an indicator character (except [?:-]) or /[?-] /
  25701. // - '\n ', ': ' or ' \n' anywhere
  25702. // - '#' not preceded by a non-space char
  25703. // - end with ' ' or ':'
  25704. return implicitKey || inFlow || !value.includes('\n')
  25705. ? quotedString(value, ctx)
  25706. : blockString(item, ctx, onComment, onChompKeep);
  25707. }
  25708. if (!implicitKey &&
  25709. !inFlow &&
  25710. type !== Scalar.PLAIN &&
  25711. value.includes('\n')) {
  25712. // Where allowed & type not set explicitly, prefer block style for multiline strings
  25713. return blockString(item, ctx, onComment, onChompKeep);
  25714. }
  25715. if (containsDocumentMarker(value)) {
  25716. if (indent === '') {
  25717. ctx.forceBlockIndent = true;
  25718. return blockString(item, ctx, onComment, onChompKeep);
  25719. }
  25720. else if (implicitKey && indent === indentStep) {
  25721. return quotedString(value, ctx);
  25722. }
  25723. }
  25724. const str = value.replace(/\n+/g, `$&\n${indent}`);
  25725. // Verify that output will be parsed as a string, as e.g. plain numbers and
  25726. // booleans get parsed with those types in v1.2 (e.g. '42', 'true' & '0.9e-3'),
  25727. // and others in v1.1.
  25728. if (actualString) {
  25729. const test = (tag) => tag.default && tag.tag !== 'tag:yaml.org,2002:str' && tag.test?.test(str);
  25730. const { compat, tags } = ctx.doc.schema;
  25731. if (tags.some(test) || compat?.some(test))
  25732. return quotedString(value, ctx);
  25733. }
  25734. return implicitKey
  25735. ? str
  25736. : foldFlowLines(str, indent, FOLD_FLOW, getFoldOptions(ctx, false));
  25737. }
  25738. function stringifyString(item, ctx, onComment, onChompKeep) {
  25739. const { implicitKey, inFlow } = ctx;
  25740. const ss = typeof item.value === 'string'
  25741. ? item
  25742. : Object.assign({}, item, { value: String(item.value) });
  25743. let { type } = item;
  25744. if (type !== Scalar.QUOTE_DOUBLE) {
  25745. // force double quotes on control characters & unpaired surrogates
  25746. if (/[\x00-\x08\x0b-\x1f\x7f-\x9f\u{D800}-\u{DFFF}]/u.test(ss.value))
  25747. type = Scalar.QUOTE_DOUBLE;
  25748. }
  25749. const _stringify = (_type) => {
  25750. switch (_type) {
  25751. case Scalar.BLOCK_FOLDED:
  25752. case Scalar.BLOCK_LITERAL:
  25753. return implicitKey || inFlow
  25754. ? quotedString(ss.value, ctx) // blocks are not valid inside flow containers
  25755. : blockString(ss, ctx, onComment, onChompKeep);
  25756. case Scalar.QUOTE_DOUBLE:
  25757. return doubleQuotedString(ss.value, ctx);
  25758. case Scalar.QUOTE_SINGLE:
  25759. return singleQuotedString(ss.value, ctx);
  25760. case Scalar.PLAIN:
  25761. return plainString(ss, ctx, onComment, onChompKeep);
  25762. default:
  25763. return null;
  25764. }
  25765. };
  25766. let res = _stringify(type);
  25767. if (res === null) {
  25768. const { defaultKeyType, defaultStringType } = ctx.options;
  25769. const t = (implicitKey && defaultKeyType) || defaultStringType;
  25770. res = _stringify(t);
  25771. if (res === null)
  25772. throw new Error(`Unsupported default string type ${t}`);
  25773. }
  25774. return res;
  25775. }
  25776. function createStringifyContext(doc, options) {
  25777. const opt = Object.assign({
  25778. blockQuote: true,
  25779. commentString: stringifyComment,
  25780. defaultKeyType: null,
  25781. defaultStringType: 'PLAIN',
  25782. directives: null,
  25783. doubleQuotedAsJSON: false,
  25784. doubleQuotedMinMultiLineLength: 40,
  25785. falseStr: 'false',
  25786. flowCollectionPadding: true,
  25787. indentSeq: true,
  25788. lineWidth: 80,
  25789. minContentWidth: 20,
  25790. nullStr: 'null',
  25791. simpleKeys: false,
  25792. singleQuote: null,
  25793. trueStr: 'true',
  25794. verifyAliasOrder: true
  25795. }, doc.schema.toStringOptions, options);
  25796. let inFlow;
  25797. switch (opt.collectionStyle) {
  25798. case 'block':
  25799. inFlow = false;
  25800. break;
  25801. case 'flow':
  25802. inFlow = true;
  25803. break;
  25804. default:
  25805. inFlow = null;
  25806. }
  25807. return {
  25808. anchors: new Set(),
  25809. doc,
  25810. flowCollectionPadding: opt.flowCollectionPadding ? ' ' : '',
  25811. indent: '',
  25812. indentStep: typeof opt.indent === 'number' ? ' '.repeat(opt.indent) : ' ',
  25813. inFlow,
  25814. options: opt
  25815. };
  25816. }
  25817. function getTagObject(tags, item) {
  25818. if (item.tag) {
  25819. const match = tags.filter(t => t.tag === item.tag);
  25820. if (match.length > 0)
  25821. return match.find(t => t.format === item.format) ?? match[0];
  25822. }
  25823. let tagObj = undefined;
  25824. let obj;
  25825. if (isScalar$1(item)) {
  25826. obj = item.value;
  25827. const match = tags.filter(t => t.identify?.(obj));
  25828. tagObj =
  25829. match.find(t => t.format === item.format) ?? match.find(t => !t.format);
  25830. }
  25831. else {
  25832. obj = item;
  25833. tagObj = tags.find(t => t.nodeClass && obj instanceof t.nodeClass);
  25834. }
  25835. if (!tagObj) {
  25836. const name = obj?.constructor?.name ?? typeof obj;
  25837. throw new Error(`Tag not resolved for ${name} value`);
  25838. }
  25839. return tagObj;
  25840. }
  25841. // needs to be called before value stringifier to allow for circular anchor refs
  25842. function stringifyProps(node, tagObj, { anchors, doc }) {
  25843. if (!doc.directives)
  25844. return '';
  25845. const props = [];
  25846. const anchor = (isScalar$1(node) || isCollection$1(node)) && node.anchor;
  25847. if (anchor && anchorIsValid(anchor)) {
  25848. anchors.add(anchor);
  25849. props.push(`&${anchor}`);
  25850. }
  25851. const tag = node.tag ? node.tag : tagObj.default ? null : tagObj.tag;
  25852. if (tag)
  25853. props.push(doc.directives.tagString(tag));
  25854. return props.join(' ');
  25855. }
  25856. function stringify$2(item, ctx, onComment, onChompKeep) {
  25857. if (isPair(item))
  25858. return item.toString(ctx, onComment, onChompKeep);
  25859. if (isAlias(item)) {
  25860. if (ctx.doc.directives)
  25861. return item.toString(ctx);
  25862. if (ctx.resolvedAliases?.has(item)) {
  25863. throw new TypeError(`Cannot stringify circular structure without alias nodes`);
  25864. }
  25865. else {
  25866. if (ctx.resolvedAliases)
  25867. ctx.resolvedAliases.add(item);
  25868. else
  25869. ctx.resolvedAliases = new Set([item]);
  25870. item = item.resolve(ctx.doc);
  25871. }
  25872. }
  25873. let tagObj = undefined;
  25874. const node = isNode$1(item)
  25875. ? item
  25876. : ctx.doc.createNode(item, { onTagObj: o => (tagObj = o) });
  25877. if (!tagObj)
  25878. tagObj = getTagObject(ctx.doc.schema.tags, node);
  25879. const props = stringifyProps(node, tagObj, ctx);
  25880. if (props.length > 0)
  25881. ctx.indentAtStart = (ctx.indentAtStart ?? 0) + props.length + 1;
  25882. const str = typeof tagObj.stringify === 'function'
  25883. ? tagObj.stringify(node, ctx, onComment, onChompKeep)
  25884. : isScalar$1(node)
  25885. ? stringifyString(node, ctx, onComment, onChompKeep)
  25886. : node.toString(ctx, onComment, onChompKeep);
  25887. if (!props)
  25888. return str;
  25889. return isScalar$1(node) || str[0] === '{' || str[0] === '['
  25890. ? `${props} ${str}`
  25891. : `${props}\n${ctx.indent}${str}`;
  25892. }
  25893. function stringifyPair({ key, value }, ctx, onComment, onChompKeep) {
  25894. const { allNullValues, doc, indent, indentStep, options: { commentString, indentSeq, simpleKeys } } = ctx;
  25895. let keyComment = (isNode$1(key) && key.comment) || null;
  25896. if (simpleKeys) {
  25897. if (keyComment) {
  25898. throw new Error('With simple keys, key nodes cannot have comments');
  25899. }
  25900. if (isCollection$1(key) || (!isNode$1(key) && typeof key === 'object')) {
  25901. const msg = 'With simple keys, collection cannot be used as a key value';
  25902. throw new Error(msg);
  25903. }
  25904. }
  25905. let explicitKey = !simpleKeys &&
  25906. (!key ||
  25907. (keyComment && value == null && !ctx.inFlow) ||
  25908. isCollection$1(key) ||
  25909. (isScalar$1(key)
  25910. ? key.type === Scalar.BLOCK_FOLDED || key.type === Scalar.BLOCK_LITERAL
  25911. : typeof key === 'object'));
  25912. ctx = Object.assign({}, ctx, {
  25913. allNullValues: false,
  25914. implicitKey: !explicitKey && (simpleKeys || !allNullValues),
  25915. indent: indent + indentStep
  25916. });
  25917. let keyCommentDone = false;
  25918. let chompKeep = false;
  25919. let str = stringify$2(key, ctx, () => (keyCommentDone = true), () => (chompKeep = true));
  25920. if (!explicitKey && !ctx.inFlow && str.length > 1024) {
  25921. if (simpleKeys)
  25922. throw new Error('With simple keys, single line scalar must not span more than 1024 characters');
  25923. explicitKey = true;
  25924. }
  25925. if (ctx.inFlow) {
  25926. if (allNullValues || value == null) {
  25927. if (keyCommentDone && onComment)
  25928. onComment();
  25929. return str === '' ? '?' : explicitKey ? `? ${str}` : str;
  25930. }
  25931. }
  25932. else if ((allNullValues && !simpleKeys) || (value == null && explicitKey)) {
  25933. str = `? ${str}`;
  25934. if (keyComment && !keyCommentDone) {
  25935. str += lineComment(str, ctx.indent, commentString(keyComment));
  25936. }
  25937. else if (chompKeep && onChompKeep)
  25938. onChompKeep();
  25939. return str;
  25940. }
  25941. if (keyCommentDone)
  25942. keyComment = null;
  25943. if (explicitKey) {
  25944. if (keyComment)
  25945. str += lineComment(str, ctx.indent, commentString(keyComment));
  25946. str = `? ${str}\n${indent}:`;
  25947. }
  25948. else {
  25949. str = `${str}:`;
  25950. if (keyComment)
  25951. str += lineComment(str, ctx.indent, commentString(keyComment));
  25952. }
  25953. let vsb, vcb, valueComment;
  25954. if (isNode$1(value)) {
  25955. vsb = !!value.spaceBefore;
  25956. vcb = value.commentBefore;
  25957. valueComment = value.comment;
  25958. }
  25959. else {
  25960. vsb = false;
  25961. vcb = null;
  25962. valueComment = null;
  25963. if (value && typeof value === 'object')
  25964. value = doc.createNode(value);
  25965. }
  25966. ctx.implicitKey = false;
  25967. if (!explicitKey && !keyComment && isScalar$1(value))
  25968. ctx.indentAtStart = str.length + 1;
  25969. chompKeep = false;
  25970. if (!indentSeq &&
  25971. indentStep.length >= 2 &&
  25972. !ctx.inFlow &&
  25973. !explicitKey &&
  25974. isSeq(value) &&
  25975. !value.flow &&
  25976. !value.tag &&
  25977. !value.anchor) {
  25978. // If indentSeq === false, consider '- ' as part of indentation where possible
  25979. ctx.indent = ctx.indent.substring(2);
  25980. }
  25981. let valueCommentDone = false;
  25982. const valueStr = stringify$2(value, ctx, () => (valueCommentDone = true), () => (chompKeep = true));
  25983. let ws = ' ';
  25984. if (keyComment || vsb || vcb) {
  25985. ws = vsb ? '\n' : '';
  25986. if (vcb) {
  25987. const cs = commentString(vcb);
  25988. ws += `\n${indentComment(cs, ctx.indent)}`;
  25989. }
  25990. if (valueStr === '' && !ctx.inFlow) {
  25991. if (ws === '\n')
  25992. ws = '\n\n';
  25993. }
  25994. else {
  25995. ws += `\n${ctx.indent}`;
  25996. }
  25997. }
  25998. else if (!explicitKey && isCollection$1(value)) {
  25999. const vs0 = valueStr[0];
  26000. const nl0 = valueStr.indexOf('\n');
  26001. const hasNewline = nl0 !== -1;
  26002. const flow = ctx.inFlow ?? value.flow ?? value.items.length === 0;
  26003. if (hasNewline || !flow) {
  26004. let hasPropsLine = false;
  26005. if (hasNewline && (vs0 === '&' || vs0 === '!')) {
  26006. let sp0 = valueStr.indexOf(' ');
  26007. if (vs0 === '&' &&
  26008. sp0 !== -1 &&
  26009. sp0 < nl0 &&
  26010. valueStr[sp0 + 1] === '!') {
  26011. sp0 = valueStr.indexOf(' ', sp0 + 1);
  26012. }
  26013. if (sp0 === -1 || nl0 < sp0)
  26014. hasPropsLine = true;
  26015. }
  26016. if (!hasPropsLine)
  26017. ws = `\n${ctx.indent}`;
  26018. }
  26019. }
  26020. else if (valueStr === '' || valueStr[0] === '\n') {
  26021. ws = '';
  26022. }
  26023. str += ws + valueStr;
  26024. if (ctx.inFlow) {
  26025. if (valueCommentDone && onComment)
  26026. onComment();
  26027. }
  26028. else if (valueComment && !valueCommentDone) {
  26029. str += lineComment(str, ctx.indent, commentString(valueComment));
  26030. }
  26031. else if (chompKeep && onChompKeep) {
  26032. onChompKeep();
  26033. }
  26034. return str;
  26035. }
  26036. function warn(logLevel, warning) {
  26037. if (logLevel === 'debug' || logLevel === 'warn') {
  26038. // https://github.com/typescript-eslint/typescript-eslint/issues/7478
  26039. // eslint-disable-next-line @typescript-eslint/prefer-optional-chain
  26040. if (typeof process !== 'undefined' && process.emitWarning)
  26041. process.emitWarning(warning);
  26042. else
  26043. console.warn(warning);
  26044. }
  26045. }
  26046. const MERGE_KEY = '<<';
  26047. function addPairToJSMap(ctx, map, { key, value }) {
  26048. if (ctx?.doc.schema.merge && isMergeKey(key)) {
  26049. value = isAlias(value) ? value.resolve(ctx.doc) : value;
  26050. if (isSeq(value))
  26051. for (const it of value.items)
  26052. mergeToJSMap(ctx, map, it);
  26053. else if (Array.isArray(value))
  26054. for (const it of value)
  26055. mergeToJSMap(ctx, map, it);
  26056. else
  26057. mergeToJSMap(ctx, map, value);
  26058. }
  26059. else {
  26060. const jsKey = toJS(key, '', ctx);
  26061. if (map instanceof Map) {
  26062. map.set(jsKey, toJS(value, jsKey, ctx));
  26063. }
  26064. else if (map instanceof Set) {
  26065. map.add(jsKey);
  26066. }
  26067. else {
  26068. const stringKey = stringifyKey(key, jsKey, ctx);
  26069. const jsValue = toJS(value, stringKey, ctx);
  26070. if (stringKey in map)
  26071. Object.defineProperty(map, stringKey, {
  26072. value: jsValue,
  26073. writable: true,
  26074. enumerable: true,
  26075. configurable: true
  26076. });
  26077. else
  26078. map[stringKey] = jsValue;
  26079. }
  26080. }
  26081. return map;
  26082. }
  26083. const isMergeKey = (key) => key === MERGE_KEY ||
  26084. (isScalar$1(key) &&
  26085. key.value === MERGE_KEY &&
  26086. (!key.type || key.type === Scalar.PLAIN));
  26087. // If the value associated with a merge key is a single mapping node, each of
  26088. // its key/value pairs is inserted into the current mapping, unless the key
  26089. // already exists in it. If the value associated with the merge key is a
  26090. // sequence, then this sequence is expected to contain mapping nodes and each
  26091. // of these nodes is merged in turn according to its order in the sequence.
  26092. // Keys in mapping nodes earlier in the sequence override keys specified in
  26093. // later mapping nodes. -- http://yaml.org/type/merge.html
  26094. function mergeToJSMap(ctx, map, value) {
  26095. const source = ctx && isAlias(value) ? value.resolve(ctx.doc) : value;
  26096. if (!isMap(source))
  26097. throw new Error('Merge sources must be maps or map aliases');
  26098. const srcMap = source.toJSON(null, ctx, Map);
  26099. for (const [key, value] of srcMap) {
  26100. if (map instanceof Map) {
  26101. if (!map.has(key))
  26102. map.set(key, value);
  26103. }
  26104. else if (map instanceof Set) {
  26105. map.add(key);
  26106. }
  26107. else if (!Object.prototype.hasOwnProperty.call(map, key)) {
  26108. Object.defineProperty(map, key, {
  26109. value,
  26110. writable: true,
  26111. enumerable: true,
  26112. configurable: true
  26113. });
  26114. }
  26115. }
  26116. return map;
  26117. }
  26118. function stringifyKey(key, jsKey, ctx) {
  26119. if (jsKey === null)
  26120. return '';
  26121. if (typeof jsKey !== 'object')
  26122. return String(jsKey);
  26123. if (isNode$1(key) && ctx?.doc) {
  26124. const strCtx = createStringifyContext(ctx.doc, {});
  26125. strCtx.anchors = new Set();
  26126. for (const node of ctx.anchors.keys())
  26127. strCtx.anchors.add(node.anchor);
  26128. strCtx.inFlow = true;
  26129. strCtx.inStringifyKey = true;
  26130. const strKey = key.toString(strCtx);
  26131. if (!ctx.mapKeyWarned) {
  26132. let jsonStr = JSON.stringify(strKey);
  26133. if (jsonStr.length > 40)
  26134. jsonStr = jsonStr.substring(0, 36) + '..."';
  26135. warn(ctx.doc.options.logLevel, `Keys with collection values will be stringified due to JS Object restrictions: ${jsonStr}. Set mapAsMap: true to use object keys.`);
  26136. ctx.mapKeyWarned = true;
  26137. }
  26138. return strKey;
  26139. }
  26140. return JSON.stringify(jsKey);
  26141. }
  26142. function createPair(key, value, ctx) {
  26143. const k = createNode(key, undefined, ctx);
  26144. const v = createNode(value, undefined, ctx);
  26145. return new Pair(k, v);
  26146. }
  26147. class Pair {
  26148. constructor(key, value = null) {
  26149. Object.defineProperty(this, NODE_TYPE, { value: PAIR });
  26150. this.key = key;
  26151. this.value = value;
  26152. }
  26153. clone(schema) {
  26154. let { key, value } = this;
  26155. if (isNode$1(key))
  26156. key = key.clone(schema);
  26157. if (isNode$1(value))
  26158. value = value.clone(schema);
  26159. return new Pair(key, value);
  26160. }
  26161. toJSON(_, ctx) {
  26162. const pair = ctx?.mapAsMap ? new Map() : {};
  26163. return addPairToJSMap(ctx, pair, this);
  26164. }
  26165. toString(ctx, onComment, onChompKeep) {
  26166. return ctx?.doc
  26167. ? stringifyPair(this, ctx, onComment, onChompKeep)
  26168. : JSON.stringify(this);
  26169. }
  26170. }
  26171. function stringifyCollection(collection, ctx, options) {
  26172. const flow = ctx.inFlow ?? collection.flow;
  26173. const stringify = flow ? stringifyFlowCollection : stringifyBlockCollection;
  26174. return stringify(collection, ctx, options);
  26175. }
  26176. function stringifyBlockCollection({ comment, items }, ctx, { blockItemPrefix, flowChars, itemIndent, onChompKeep, onComment }) {
  26177. const { indent, options: { commentString } } = ctx;
  26178. const itemCtx = Object.assign({}, ctx, { indent: itemIndent, type: null });
  26179. let chompKeep = false; // flag for the preceding node's status
  26180. const lines = [];
  26181. for (let i = 0; i < items.length; ++i) {
  26182. const item = items[i];
  26183. let comment = null;
  26184. if (isNode$1(item)) {
  26185. if (!chompKeep && item.spaceBefore)
  26186. lines.push('');
  26187. addCommentBefore(ctx, lines, item.commentBefore, chompKeep);
  26188. if (item.comment)
  26189. comment = item.comment;
  26190. }
  26191. else if (isPair(item)) {
  26192. const ik = isNode$1(item.key) ? item.key : null;
  26193. if (ik) {
  26194. if (!chompKeep && ik.spaceBefore)
  26195. lines.push('');
  26196. addCommentBefore(ctx, lines, ik.commentBefore, chompKeep);
  26197. }
  26198. }
  26199. chompKeep = false;
  26200. let str = stringify$2(item, itemCtx, () => (comment = null), () => (chompKeep = true));
  26201. if (comment)
  26202. str += lineComment(str, itemIndent, commentString(comment));
  26203. if (chompKeep && comment)
  26204. chompKeep = false;
  26205. lines.push(blockItemPrefix + str);
  26206. }
  26207. let str;
  26208. if (lines.length === 0) {
  26209. str = flowChars.start + flowChars.end;
  26210. }
  26211. else {
  26212. str = lines[0];
  26213. for (let i = 1; i < lines.length; ++i) {
  26214. const line = lines[i];
  26215. str += line ? `\n${indent}${line}` : '\n';
  26216. }
  26217. }
  26218. if (comment) {
  26219. str += '\n' + indentComment(commentString(comment), indent);
  26220. if (onComment)
  26221. onComment();
  26222. }
  26223. else if (chompKeep && onChompKeep)
  26224. onChompKeep();
  26225. return str;
  26226. }
  26227. function stringifyFlowCollection({ items }, ctx, { flowChars, itemIndent }) {
  26228. const { indent, indentStep, flowCollectionPadding: fcPadding, options: { commentString } } = ctx;
  26229. itemIndent += indentStep;
  26230. const itemCtx = Object.assign({}, ctx, {
  26231. indent: itemIndent,
  26232. inFlow: true,
  26233. type: null
  26234. });
  26235. let reqNewline = false;
  26236. let linesAtValue = 0;
  26237. const lines = [];
  26238. for (let i = 0; i < items.length; ++i) {
  26239. const item = items[i];
  26240. let comment = null;
  26241. if (isNode$1(item)) {
  26242. if (item.spaceBefore)
  26243. lines.push('');
  26244. addCommentBefore(ctx, lines, item.commentBefore, false);
  26245. if (item.comment)
  26246. comment = item.comment;
  26247. }
  26248. else if (isPair(item)) {
  26249. const ik = isNode$1(item.key) ? item.key : null;
  26250. if (ik) {
  26251. if (ik.spaceBefore)
  26252. lines.push('');
  26253. addCommentBefore(ctx, lines, ik.commentBefore, false);
  26254. if (ik.comment)
  26255. reqNewline = true;
  26256. }
  26257. const iv = isNode$1(item.value) ? item.value : null;
  26258. if (iv) {
  26259. if (iv.comment)
  26260. comment = iv.comment;
  26261. if (iv.commentBefore)
  26262. reqNewline = true;
  26263. }
  26264. else if (item.value == null && ik?.comment) {
  26265. comment = ik.comment;
  26266. }
  26267. }
  26268. if (comment)
  26269. reqNewline = true;
  26270. let str = stringify$2(item, itemCtx, () => (comment = null));
  26271. if (i < items.length - 1)
  26272. str += ',';
  26273. if (comment)
  26274. str += lineComment(str, itemIndent, commentString(comment));
  26275. if (!reqNewline && (lines.length > linesAtValue || str.includes('\n')))
  26276. reqNewline = true;
  26277. lines.push(str);
  26278. linesAtValue = lines.length;
  26279. }
  26280. const { start, end } = flowChars;
  26281. if (lines.length === 0) {
  26282. return start + end;
  26283. }
  26284. else {
  26285. if (!reqNewline) {
  26286. const len = lines.reduce((sum, line) => sum + line.length + 2, 2);
  26287. reqNewline = ctx.options.lineWidth > 0 && len > ctx.options.lineWidth;
  26288. }
  26289. if (reqNewline) {
  26290. let str = start;
  26291. for (const line of lines)
  26292. str += line ? `\n${indentStep}${indent}${line}` : '\n';
  26293. return `${str}\n${indent}${end}`;
  26294. }
  26295. else {
  26296. return `${start}${fcPadding}${lines.join(' ')}${fcPadding}${end}`;
  26297. }
  26298. }
  26299. }
  26300. function addCommentBefore({ indent, options: { commentString } }, lines, comment, chompKeep) {
  26301. if (comment && chompKeep)
  26302. comment = comment.replace(/^\n+/, '');
  26303. if (comment) {
  26304. const ic = indentComment(commentString(comment), indent);
  26305. lines.push(ic.trimStart()); // Avoid double indent on first line
  26306. }
  26307. }
  26308. function findPair(items, key) {
  26309. const k = isScalar$1(key) ? key.value : key;
  26310. for (const it of items) {
  26311. if (isPair(it)) {
  26312. if (it.key === key || it.key === k)
  26313. return it;
  26314. if (isScalar$1(it.key) && it.key.value === k)
  26315. return it;
  26316. }
  26317. }
  26318. return undefined;
  26319. }
  26320. class YAMLMap extends Collection {
  26321. static get tagName() {
  26322. return 'tag:yaml.org,2002:map';
  26323. }
  26324. constructor(schema) {
  26325. super(MAP, schema);
  26326. this.items = [];
  26327. }
  26328. /**
  26329. * A generic collection parsing method that can be extended
  26330. * to other node classes that inherit from YAMLMap
  26331. */
  26332. static from(schema, obj, ctx) {
  26333. const { keepUndefined, replacer } = ctx;
  26334. const map = new this(schema);
  26335. const add = (key, value) => {
  26336. if (typeof replacer === 'function')
  26337. value = replacer.call(obj, key, value);
  26338. else if (Array.isArray(replacer) && !replacer.includes(key))
  26339. return;
  26340. if (value !== undefined || keepUndefined)
  26341. map.items.push(createPair(key, value, ctx));
  26342. };
  26343. if (obj instanceof Map) {
  26344. for (const [key, value] of obj)
  26345. add(key, value);
  26346. }
  26347. else if (obj && typeof obj === 'object') {
  26348. for (const key of Object.keys(obj))
  26349. add(key, obj[key]);
  26350. }
  26351. if (typeof schema.sortMapEntries === 'function') {
  26352. map.items.sort(schema.sortMapEntries);
  26353. }
  26354. return map;
  26355. }
  26356. /**
  26357. * Adds a value to the collection.
  26358. *
  26359. * @param overwrite - If not set `true`, using a key that is already in the
  26360. * collection will throw. Otherwise, overwrites the previous value.
  26361. */
  26362. add(pair, overwrite) {
  26363. let _pair;
  26364. if (isPair(pair))
  26365. _pair = pair;
  26366. else if (!pair || typeof pair !== 'object' || !('key' in pair)) {
  26367. // In TypeScript, this never happens.
  26368. _pair = new Pair(pair, pair?.value);
  26369. }
  26370. else
  26371. _pair = new Pair(pair.key, pair.value);
  26372. const prev = findPair(this.items, _pair.key);
  26373. const sortEntries = this.schema?.sortMapEntries;
  26374. if (prev) {
  26375. if (!overwrite)
  26376. throw new Error(`Key ${_pair.key} already set`);
  26377. // For scalars, keep the old node & its comments and anchors
  26378. if (isScalar$1(prev.value) && isScalarValue(_pair.value))
  26379. prev.value.value = _pair.value;
  26380. else
  26381. prev.value = _pair.value;
  26382. }
  26383. else if (sortEntries) {
  26384. const i = this.items.findIndex(item => sortEntries(_pair, item) < 0);
  26385. if (i === -1)
  26386. this.items.push(_pair);
  26387. else
  26388. this.items.splice(i, 0, _pair);
  26389. }
  26390. else {
  26391. this.items.push(_pair);
  26392. }
  26393. }
  26394. delete(key) {
  26395. const it = findPair(this.items, key);
  26396. if (!it)
  26397. return false;
  26398. const del = this.items.splice(this.items.indexOf(it), 1);
  26399. return del.length > 0;
  26400. }
  26401. get(key, keepScalar) {
  26402. const it = findPair(this.items, key);
  26403. const node = it?.value;
  26404. return (!keepScalar && isScalar$1(node) ? node.value : node) ?? undefined;
  26405. }
  26406. has(key) {
  26407. return !!findPair(this.items, key);
  26408. }
  26409. set(key, value) {
  26410. this.add(new Pair(key, value), true);
  26411. }
  26412. /**
  26413. * @param ctx - Conversion context, originally set in Document#toJS()
  26414. * @param {Class} Type - If set, forces the returned collection type
  26415. * @returns Instance of Type, Map, or Object
  26416. */
  26417. toJSON(_, ctx, Type) {
  26418. const map = Type ? new Type() : ctx?.mapAsMap ? new Map() : {};
  26419. if (ctx?.onCreate)
  26420. ctx.onCreate(map);
  26421. for (const item of this.items)
  26422. addPairToJSMap(ctx, map, item);
  26423. return map;
  26424. }
  26425. toString(ctx, onComment, onChompKeep) {
  26426. if (!ctx)
  26427. return JSON.stringify(this);
  26428. for (const item of this.items) {
  26429. if (!isPair(item))
  26430. throw new Error(`Map items must all be pairs; found ${JSON.stringify(item)} instead`);
  26431. }
  26432. if (!ctx.allNullValues && this.hasAllNullValues(false))
  26433. ctx = Object.assign({}, ctx, { allNullValues: true });
  26434. return stringifyCollection(this, ctx, {
  26435. blockItemPrefix: '',
  26436. flowChars: { start: '{', end: '}' },
  26437. itemIndent: ctx.indent || '',
  26438. onChompKeep,
  26439. onComment
  26440. });
  26441. }
  26442. }
  26443. const map$1 = {
  26444. collection: 'map',
  26445. default: true,
  26446. nodeClass: YAMLMap,
  26447. tag: 'tag:yaml.org,2002:map',
  26448. resolve(map, onError) {
  26449. if (!isMap(map))
  26450. onError('Expected a mapping for this tag');
  26451. return map;
  26452. },
  26453. createNode: (schema, obj, ctx) => YAMLMap.from(schema, obj, ctx)
  26454. };
  26455. class YAMLSeq extends Collection {
  26456. static get tagName() {
  26457. return 'tag:yaml.org,2002:seq';
  26458. }
  26459. constructor(schema) {
  26460. super(SEQ, schema);
  26461. this.items = [];
  26462. }
  26463. add(value) {
  26464. this.items.push(value);
  26465. }
  26466. /**
  26467. * Removes a value from the collection.
  26468. *
  26469. * `key` must contain a representation of an integer for this to succeed.
  26470. * It may be wrapped in a `Scalar`.
  26471. *
  26472. * @returns `true` if the item was found and removed.
  26473. */
  26474. delete(key) {
  26475. const idx = asItemIndex(key);
  26476. if (typeof idx !== 'number')
  26477. return false;
  26478. const del = this.items.splice(idx, 1);
  26479. return del.length > 0;
  26480. }
  26481. get(key, keepScalar) {
  26482. const idx = asItemIndex(key);
  26483. if (typeof idx !== 'number')
  26484. return undefined;
  26485. const it = this.items[idx];
  26486. return !keepScalar && isScalar$1(it) ? it.value : it;
  26487. }
  26488. /**
  26489. * Checks if the collection includes a value with the key `key`.
  26490. *
  26491. * `key` must contain a representation of an integer for this to succeed.
  26492. * It may be wrapped in a `Scalar`.
  26493. */
  26494. has(key) {
  26495. const idx = asItemIndex(key);
  26496. return typeof idx === 'number' && idx < this.items.length;
  26497. }
  26498. /**
  26499. * Sets a value in this collection. For `!!set`, `value` needs to be a
  26500. * boolean to add/remove the item from the set.
  26501. *
  26502. * If `key` does not contain a representation of an integer, this will throw.
  26503. * It may be wrapped in a `Scalar`.
  26504. */
  26505. set(key, value) {
  26506. const idx = asItemIndex(key);
  26507. if (typeof idx !== 'number')
  26508. throw new Error(`Expected a valid index, not ${key}.`);
  26509. const prev = this.items[idx];
  26510. if (isScalar$1(prev) && isScalarValue(value))
  26511. prev.value = value;
  26512. else
  26513. this.items[idx] = value;
  26514. }
  26515. toJSON(_, ctx) {
  26516. const seq = [];
  26517. if (ctx?.onCreate)
  26518. ctx.onCreate(seq);
  26519. let i = 0;
  26520. for (const item of this.items)
  26521. seq.push(toJS(item, String(i++), ctx));
  26522. return seq;
  26523. }
  26524. toString(ctx, onComment, onChompKeep) {
  26525. if (!ctx)
  26526. return JSON.stringify(this);
  26527. return stringifyCollection(this, ctx, {
  26528. blockItemPrefix: '- ',
  26529. flowChars: { start: '[', end: ']' },
  26530. itemIndent: (ctx.indent || '') + ' ',
  26531. onChompKeep,
  26532. onComment
  26533. });
  26534. }
  26535. static from(schema, obj, ctx) {
  26536. const { replacer } = ctx;
  26537. const seq = new this(schema);
  26538. if (obj && Symbol.iterator in Object(obj)) {
  26539. let i = 0;
  26540. for (let it of obj) {
  26541. if (typeof replacer === 'function') {
  26542. const key = obj instanceof Set ? it : String(i++);
  26543. it = replacer.call(obj, key, it);
  26544. }
  26545. seq.items.push(createNode(it, undefined, ctx));
  26546. }
  26547. }
  26548. return seq;
  26549. }
  26550. }
  26551. function asItemIndex(key) {
  26552. let idx = isScalar$1(key) ? key.value : key;
  26553. if (idx && typeof idx === 'string')
  26554. idx = Number(idx);
  26555. return typeof idx === 'number' && Number.isInteger(idx) && idx >= 0
  26556. ? idx
  26557. : null;
  26558. }
  26559. const seq = {
  26560. collection: 'seq',
  26561. default: true,
  26562. nodeClass: YAMLSeq,
  26563. tag: 'tag:yaml.org,2002:seq',
  26564. resolve(seq, onError) {
  26565. if (!isSeq(seq))
  26566. onError('Expected a sequence for this tag');
  26567. return seq;
  26568. },
  26569. createNode: (schema, obj, ctx) => YAMLSeq.from(schema, obj, ctx)
  26570. };
  26571. const string = {
  26572. identify: value => typeof value === 'string',
  26573. default: true,
  26574. tag: 'tag:yaml.org,2002:str',
  26575. resolve: str => str,
  26576. stringify(item, ctx, onComment, onChompKeep) {
  26577. ctx = Object.assign({ actualString: true }, ctx);
  26578. return stringifyString(item, ctx, onComment, onChompKeep);
  26579. }
  26580. };
  26581. const nullTag = {
  26582. identify: value => value == null,
  26583. createNode: () => new Scalar(null),
  26584. default: true,
  26585. tag: 'tag:yaml.org,2002:null',
  26586. test: /^(?:~|[Nn]ull|NULL)?$/,
  26587. resolve: () => new Scalar(null),
  26588. stringify: ({ source }, ctx) => typeof source === 'string' && nullTag.test.test(source)
  26589. ? source
  26590. : ctx.options.nullStr
  26591. };
  26592. const boolTag = {
  26593. identify: value => typeof value === 'boolean',
  26594. default: true,
  26595. tag: 'tag:yaml.org,2002:bool',
  26596. test: /^(?:[Tt]rue|TRUE|[Ff]alse|FALSE)$/,
  26597. resolve: str => new Scalar(str[0] === 't' || str[0] === 'T'),
  26598. stringify({ source, value }, ctx) {
  26599. if (source && boolTag.test.test(source)) {
  26600. const sv = source[0] === 't' || source[0] === 'T';
  26601. if (value === sv)
  26602. return source;
  26603. }
  26604. return value ? ctx.options.trueStr : ctx.options.falseStr;
  26605. }
  26606. };
  26607. function stringifyNumber({ format, minFractionDigits, tag, value }) {
  26608. if (typeof value === 'bigint')
  26609. return String(value);
  26610. const num = typeof value === 'number' ? value : Number(value);
  26611. if (!isFinite(num))
  26612. return isNaN(num) ? '.nan' : num < 0 ? '-.inf' : '.inf';
  26613. let n = JSON.stringify(value);
  26614. if (!format &&
  26615. minFractionDigits &&
  26616. (!tag || tag === 'tag:yaml.org,2002:float') &&
  26617. /^\d/.test(n)) {
  26618. let i = n.indexOf('.');
  26619. if (i < 0) {
  26620. i = n.length;
  26621. n += '.';
  26622. }
  26623. let d = minFractionDigits - (n.length - i - 1);
  26624. while (d-- > 0)
  26625. n += '0';
  26626. }
  26627. return n;
  26628. }
  26629. const floatNaN$1 = {
  26630. identify: value => typeof value === 'number',
  26631. default: true,
  26632. tag: 'tag:yaml.org,2002:float',
  26633. test: /^(?:[-+]?\.(?:inf|Inf|INF)|\.nan|\.NaN|\.NAN)$/,
  26634. resolve: str => str.slice(-3).toLowerCase() === 'nan'
  26635. ? NaN
  26636. : str[0] === '-'
  26637. ? Number.NEGATIVE_INFINITY
  26638. : Number.POSITIVE_INFINITY,
  26639. stringify: stringifyNumber
  26640. };
  26641. const floatExp$1 = {
  26642. identify: value => typeof value === 'number',
  26643. default: true,
  26644. tag: 'tag:yaml.org,2002:float',
  26645. format: 'EXP',
  26646. test: /^[-+]?(?:\.[0-9]+|[0-9]+(?:\.[0-9]*)?)[eE][-+]?[0-9]+$/,
  26647. resolve: str => parseFloat(str),
  26648. stringify(node) {
  26649. const num = Number(node.value);
  26650. return isFinite(num) ? num.toExponential() : stringifyNumber(node);
  26651. }
  26652. };
  26653. const float$1 = {
  26654. identify: value => typeof value === 'number',
  26655. default: true,
  26656. tag: 'tag:yaml.org,2002:float',
  26657. test: /^[-+]?(?:\.[0-9]+|[0-9]+\.[0-9]*)$/,
  26658. resolve(str) {
  26659. const node = new Scalar(parseFloat(str));
  26660. const dot = str.indexOf('.');
  26661. if (dot !== -1 && str[str.length - 1] === '0')
  26662. node.minFractionDigits = str.length - dot - 1;
  26663. return node;
  26664. },
  26665. stringify: stringifyNumber
  26666. };
  26667. const intIdentify$2 = (value) => typeof value === 'bigint' || Number.isInteger(value);
  26668. const intResolve$1 = (str, offset, radix, { intAsBigInt }) => (intAsBigInt ? BigInt(str) : parseInt(str.substring(offset), radix));
  26669. function intStringify$1(node, radix, prefix) {
  26670. const { value } = node;
  26671. if (intIdentify$2(value) && value >= 0)
  26672. return prefix + value.toString(radix);
  26673. return stringifyNumber(node);
  26674. }
  26675. const intOct$1 = {
  26676. identify: value => intIdentify$2(value) && value >= 0,
  26677. default: true,
  26678. tag: 'tag:yaml.org,2002:int',
  26679. format: 'OCT',
  26680. test: /^0o[0-7]+$/,
  26681. resolve: (str, _onError, opt) => intResolve$1(str, 2, 8, opt),
  26682. stringify: node => intStringify$1(node, 8, '0o')
  26683. };
  26684. const int$1 = {
  26685. identify: intIdentify$2,
  26686. default: true,
  26687. tag: 'tag:yaml.org,2002:int',
  26688. test: /^[-+]?[0-9]+$/,
  26689. resolve: (str, _onError, opt) => intResolve$1(str, 0, 10, opt),
  26690. stringify: stringifyNumber
  26691. };
  26692. const intHex$1 = {
  26693. identify: value => intIdentify$2(value) && value >= 0,
  26694. default: true,
  26695. tag: 'tag:yaml.org,2002:int',
  26696. format: 'HEX',
  26697. test: /^0x[0-9a-fA-F]+$/,
  26698. resolve: (str, _onError, opt) => intResolve$1(str, 2, 16, opt),
  26699. stringify: node => intStringify$1(node, 16, '0x')
  26700. };
  26701. const schema$2 = [
  26702. map$1,
  26703. seq,
  26704. string,
  26705. nullTag,
  26706. boolTag,
  26707. intOct$1,
  26708. int$1,
  26709. intHex$1,
  26710. floatNaN$1,
  26711. floatExp$1,
  26712. float$1
  26713. ];
  26714. function intIdentify$1(value) {
  26715. return typeof value === 'bigint' || Number.isInteger(value);
  26716. }
  26717. const stringifyJSON = ({ value }) => JSON.stringify(value);
  26718. const jsonScalars = [
  26719. {
  26720. identify: value => typeof value === 'string',
  26721. default: true,
  26722. tag: 'tag:yaml.org,2002:str',
  26723. resolve: str => str,
  26724. stringify: stringifyJSON
  26725. },
  26726. {
  26727. identify: value => value == null,
  26728. createNode: () => new Scalar(null),
  26729. default: true,
  26730. tag: 'tag:yaml.org,2002:null',
  26731. test: /^null$/,
  26732. resolve: () => null,
  26733. stringify: stringifyJSON
  26734. },
  26735. {
  26736. identify: value => typeof value === 'boolean',
  26737. default: true,
  26738. tag: 'tag:yaml.org,2002:bool',
  26739. test: /^true|false$/,
  26740. resolve: str => str === 'true',
  26741. stringify: stringifyJSON
  26742. },
  26743. {
  26744. identify: intIdentify$1,
  26745. default: true,
  26746. tag: 'tag:yaml.org,2002:int',
  26747. test: /^-?(?:0|[1-9][0-9]*)$/,
  26748. resolve: (str, _onError, { intAsBigInt }) => intAsBigInt ? BigInt(str) : parseInt(str, 10),
  26749. stringify: ({ value }) => intIdentify$1(value) ? value.toString() : JSON.stringify(value)
  26750. },
  26751. {
  26752. identify: value => typeof value === 'number',
  26753. default: true,
  26754. tag: 'tag:yaml.org,2002:float',
  26755. test: /^-?(?:0|[1-9][0-9]*)(?:\.[0-9]*)?(?:[eE][-+]?[0-9]+)?$/,
  26756. resolve: str => parseFloat(str),
  26757. stringify: stringifyJSON
  26758. }
  26759. ];
  26760. const jsonError = {
  26761. default: true,
  26762. tag: '',
  26763. test: /^/,
  26764. resolve(str, onError) {
  26765. onError(`Unresolved plain scalar ${JSON.stringify(str)}`);
  26766. return str;
  26767. }
  26768. };
  26769. const schema$1 = [map$1, seq].concat(jsonScalars, jsonError);
  26770. const binary = {
  26771. identify: value => value instanceof Uint8Array, // Buffer inherits from Uint8Array
  26772. default: false,
  26773. tag: 'tag:yaml.org,2002:binary',
  26774. /**
  26775. * Returns a Buffer in node and an Uint8Array in browsers
  26776. *
  26777. * To use the resulting buffer as an image, you'll want to do something like:
  26778. *
  26779. * const blob = new Blob([buffer], { type: 'image/jpeg' })
  26780. * document.querySelector('#photo').src = URL.createObjectURL(blob)
  26781. */
  26782. resolve(src, onError) {
  26783. if (typeof Buffer === 'function') {
  26784. return Buffer.from(src, 'base64');
  26785. }
  26786. else if (typeof atob === 'function') {
  26787. // On IE 11, atob() can't handle newlines
  26788. const str = atob(src.replace(/[\n\r]/g, ''));
  26789. const buffer = new Uint8Array(str.length);
  26790. for (let i = 0; i < str.length; ++i)
  26791. buffer[i] = str.charCodeAt(i);
  26792. return buffer;
  26793. }
  26794. else {
  26795. onError('This environment does not support reading binary tags; either Buffer or atob is required');
  26796. return src;
  26797. }
  26798. },
  26799. stringify({ comment, type, value }, ctx, onComment, onChompKeep) {
  26800. const buf = value; // checked earlier by binary.identify()
  26801. let str;
  26802. if (typeof Buffer === 'function') {
  26803. str =
  26804. buf instanceof Buffer
  26805. ? buf.toString('base64')
  26806. : Buffer.from(buf.buffer).toString('base64');
  26807. }
  26808. else if (typeof btoa === 'function') {
  26809. let s = '';
  26810. for (let i = 0; i < buf.length; ++i)
  26811. s += String.fromCharCode(buf[i]);
  26812. str = btoa(s);
  26813. }
  26814. else {
  26815. throw new Error('This environment does not support writing binary tags; either Buffer or btoa is required');
  26816. }
  26817. if (!type)
  26818. type = Scalar.BLOCK_LITERAL;
  26819. if (type !== Scalar.QUOTE_DOUBLE) {
  26820. const lineWidth = Math.max(ctx.options.lineWidth - ctx.indent.length, ctx.options.minContentWidth);
  26821. const n = Math.ceil(str.length / lineWidth);
  26822. const lines = new Array(n);
  26823. for (let i = 0, o = 0; i < n; ++i, o += lineWidth) {
  26824. lines[i] = str.substr(o, lineWidth);
  26825. }
  26826. str = lines.join(type === Scalar.BLOCK_LITERAL ? '\n' : ' ');
  26827. }
  26828. return stringifyString({ comment, type, value: str }, ctx, onComment, onChompKeep);
  26829. }
  26830. };
  26831. function resolvePairs(seq, onError) {
  26832. if (isSeq(seq)) {
  26833. for (let i = 0; i < seq.items.length; ++i) {
  26834. let item = seq.items[i];
  26835. if (isPair(item))
  26836. continue;
  26837. else if (isMap(item)) {
  26838. if (item.items.length > 1)
  26839. onError('Each pair must have its own sequence indicator');
  26840. const pair = item.items[0] || new Pair(new Scalar(null));
  26841. if (item.commentBefore)
  26842. pair.key.commentBefore = pair.key.commentBefore
  26843. ? `${item.commentBefore}\n${pair.key.commentBefore}`
  26844. : item.commentBefore;
  26845. if (item.comment) {
  26846. const cn = pair.value ?? pair.key;
  26847. cn.comment = cn.comment
  26848. ? `${item.comment}\n${cn.comment}`
  26849. : item.comment;
  26850. }
  26851. item = pair;
  26852. }
  26853. seq.items[i] = isPair(item) ? item : new Pair(item);
  26854. }
  26855. }
  26856. else
  26857. onError('Expected a sequence for this tag');
  26858. return seq;
  26859. }
  26860. function createPairs(schema, iterable, ctx) {
  26861. const { replacer } = ctx;
  26862. const pairs = new YAMLSeq(schema);
  26863. pairs.tag = 'tag:yaml.org,2002:pairs';
  26864. let i = 0;
  26865. if (iterable && Symbol.iterator in Object(iterable))
  26866. for (let it of iterable) {
  26867. if (typeof replacer === 'function')
  26868. it = replacer.call(iterable, String(i++), it);
  26869. let key, value;
  26870. if (Array.isArray(it)) {
  26871. if (it.length === 2) {
  26872. key = it[0];
  26873. value = it[1];
  26874. }
  26875. else
  26876. throw new TypeError(`Expected [key, value] tuple: ${it}`);
  26877. }
  26878. else if (it && it instanceof Object) {
  26879. const keys = Object.keys(it);
  26880. if (keys.length === 1) {
  26881. key = keys[0];
  26882. value = it[key];
  26883. }
  26884. else {
  26885. throw new TypeError(`Expected tuple with one key, not ${keys.length} keys`);
  26886. }
  26887. }
  26888. else {
  26889. key = it;
  26890. }
  26891. pairs.items.push(createPair(key, value, ctx));
  26892. }
  26893. return pairs;
  26894. }
  26895. const pairs = {
  26896. collection: 'seq',
  26897. default: false,
  26898. tag: 'tag:yaml.org,2002:pairs',
  26899. resolve: resolvePairs,
  26900. createNode: createPairs
  26901. };
  26902. class YAMLOMap extends YAMLSeq {
  26903. constructor() {
  26904. super();
  26905. this.add = YAMLMap.prototype.add.bind(this);
  26906. this.delete = YAMLMap.prototype.delete.bind(this);
  26907. this.get = YAMLMap.prototype.get.bind(this);
  26908. this.has = YAMLMap.prototype.has.bind(this);
  26909. this.set = YAMLMap.prototype.set.bind(this);
  26910. this.tag = YAMLOMap.tag;
  26911. }
  26912. /**
  26913. * If `ctx` is given, the return type is actually `Map<unknown, unknown>`,
  26914. * but TypeScript won't allow widening the signature of a child method.
  26915. */
  26916. toJSON(_, ctx) {
  26917. if (!ctx)
  26918. return super.toJSON(_);
  26919. const map = new Map();
  26920. if (ctx?.onCreate)
  26921. ctx.onCreate(map);
  26922. for (const pair of this.items) {
  26923. let key, value;
  26924. if (isPair(pair)) {
  26925. key = toJS(pair.key, '', ctx);
  26926. value = toJS(pair.value, key, ctx);
  26927. }
  26928. else {
  26929. key = toJS(pair, '', ctx);
  26930. }
  26931. if (map.has(key))
  26932. throw new Error('Ordered maps must not include duplicate keys');
  26933. map.set(key, value);
  26934. }
  26935. return map;
  26936. }
  26937. static from(schema, iterable, ctx) {
  26938. const pairs = createPairs(schema, iterable, ctx);
  26939. const omap = new this();
  26940. omap.items = pairs.items;
  26941. return omap;
  26942. }
  26943. }
  26944. YAMLOMap.tag = 'tag:yaml.org,2002:omap';
  26945. const omap = {
  26946. collection: 'seq',
  26947. identify: value => value instanceof Map,
  26948. nodeClass: YAMLOMap,
  26949. default: false,
  26950. tag: 'tag:yaml.org,2002:omap',
  26951. resolve(seq, onError) {
  26952. const pairs = resolvePairs(seq, onError);
  26953. const seenKeys = [];
  26954. for (const { key } of pairs.items) {
  26955. if (isScalar$1(key)) {
  26956. if (seenKeys.includes(key.value)) {
  26957. onError(`Ordered maps must not include duplicate keys: ${key.value}`);
  26958. }
  26959. else {
  26960. seenKeys.push(key.value);
  26961. }
  26962. }
  26963. }
  26964. return Object.assign(new YAMLOMap(), pairs);
  26965. },
  26966. createNode: (schema, iterable, ctx) => YAMLOMap.from(schema, iterable, ctx)
  26967. };
  26968. function boolStringify({ value, source }, ctx) {
  26969. const boolObj = value ? trueTag : falseTag;
  26970. if (source && boolObj.test.test(source))
  26971. return source;
  26972. return value ? ctx.options.trueStr : ctx.options.falseStr;
  26973. }
  26974. const trueTag = {
  26975. identify: value => value === true,
  26976. default: true,
  26977. tag: 'tag:yaml.org,2002:bool',
  26978. test: /^(?:Y|y|[Yy]es|YES|[Tt]rue|TRUE|[Oo]n|ON)$/,
  26979. resolve: () => new Scalar(true),
  26980. stringify: boolStringify
  26981. };
  26982. const falseTag = {
  26983. identify: value => value === false,
  26984. default: true,
  26985. tag: 'tag:yaml.org,2002:bool',
  26986. test: /^(?:N|n|[Nn]o|NO|[Ff]alse|FALSE|[Oo]ff|OFF)$/,
  26987. resolve: () => new Scalar(false),
  26988. stringify: boolStringify
  26989. };
  26990. const floatNaN = {
  26991. identify: value => typeof value === 'number',
  26992. default: true,
  26993. tag: 'tag:yaml.org,2002:float',
  26994. test: /^(?:[-+]?\.(?:inf|Inf|INF)|\.nan|\.NaN|\.NAN)$/,
  26995. resolve: (str) => str.slice(-3).toLowerCase() === 'nan'
  26996. ? NaN
  26997. : str[0] === '-'
  26998. ? Number.NEGATIVE_INFINITY
  26999. : Number.POSITIVE_INFINITY,
  27000. stringify: stringifyNumber
  27001. };
  27002. const floatExp = {
  27003. identify: value => typeof value === 'number',
  27004. default: true,
  27005. tag: 'tag:yaml.org,2002:float',
  27006. format: 'EXP',
  27007. test: /^[-+]?(?:[0-9][0-9_]*)?(?:\.[0-9_]*)?[eE][-+]?[0-9]+$/,
  27008. resolve: (str) => parseFloat(str.replace(/_/g, '')),
  27009. stringify(node) {
  27010. const num = Number(node.value);
  27011. return isFinite(num) ? num.toExponential() : stringifyNumber(node);
  27012. }
  27013. };
  27014. const float = {
  27015. identify: value => typeof value === 'number',
  27016. default: true,
  27017. tag: 'tag:yaml.org,2002:float',
  27018. test: /^[-+]?(?:[0-9][0-9_]*)?\.[0-9_]*$/,
  27019. resolve(str) {
  27020. const node = new Scalar(parseFloat(str.replace(/_/g, '')));
  27021. const dot = str.indexOf('.');
  27022. if (dot !== -1) {
  27023. const f = str.substring(dot + 1).replace(/_/g, '');
  27024. if (f[f.length - 1] === '0')
  27025. node.minFractionDigits = f.length;
  27026. }
  27027. return node;
  27028. },
  27029. stringify: stringifyNumber
  27030. };
  27031. const intIdentify = (value) => typeof value === 'bigint' || Number.isInteger(value);
  27032. function intResolve(str, offset, radix, { intAsBigInt }) {
  27033. const sign = str[0];
  27034. if (sign === '-' || sign === '+')
  27035. offset += 1;
  27036. str = str.substring(offset).replace(/_/g, '');
  27037. if (intAsBigInt) {
  27038. switch (radix) {
  27039. case 2:
  27040. str = `0b${str}`;
  27041. break;
  27042. case 8:
  27043. str = `0o${str}`;
  27044. break;
  27045. case 16:
  27046. str = `0x${str}`;
  27047. break;
  27048. }
  27049. const n = BigInt(str);
  27050. return sign === '-' ? BigInt(-1) * n : n;
  27051. }
  27052. const n = parseInt(str, radix);
  27053. return sign === '-' ? -1 * n : n;
  27054. }
  27055. function intStringify(node, radix, prefix) {
  27056. const { value } = node;
  27057. if (intIdentify(value)) {
  27058. const str = value.toString(radix);
  27059. return value < 0 ? '-' + prefix + str.substr(1) : prefix + str;
  27060. }
  27061. return stringifyNumber(node);
  27062. }
  27063. const intBin = {
  27064. identify: intIdentify,
  27065. default: true,
  27066. tag: 'tag:yaml.org,2002:int',
  27067. format: 'BIN',
  27068. test: /^[-+]?0b[0-1_]+$/,
  27069. resolve: (str, _onError, opt) => intResolve(str, 2, 2, opt),
  27070. stringify: node => intStringify(node, 2, '0b')
  27071. };
  27072. const intOct = {
  27073. identify: intIdentify,
  27074. default: true,
  27075. tag: 'tag:yaml.org,2002:int',
  27076. format: 'OCT',
  27077. test: /^[-+]?0[0-7_]+$/,
  27078. resolve: (str, _onError, opt) => intResolve(str, 1, 8, opt),
  27079. stringify: node => intStringify(node, 8, '0')
  27080. };
  27081. const int = {
  27082. identify: intIdentify,
  27083. default: true,
  27084. tag: 'tag:yaml.org,2002:int',
  27085. test: /^[-+]?[0-9][0-9_]*$/,
  27086. resolve: (str, _onError, opt) => intResolve(str, 0, 10, opt),
  27087. stringify: stringifyNumber
  27088. };
  27089. const intHex = {
  27090. identify: intIdentify,
  27091. default: true,
  27092. tag: 'tag:yaml.org,2002:int',
  27093. format: 'HEX',
  27094. test: /^[-+]?0x[0-9a-fA-F_]+$/,
  27095. resolve: (str, _onError, opt) => intResolve(str, 2, 16, opt),
  27096. stringify: node => intStringify(node, 16, '0x')
  27097. };
  27098. class YAMLSet extends YAMLMap {
  27099. constructor(schema) {
  27100. super(schema);
  27101. this.tag = YAMLSet.tag;
  27102. }
  27103. add(key) {
  27104. let pair;
  27105. if (isPair(key))
  27106. pair = key;
  27107. else if (key &&
  27108. typeof key === 'object' &&
  27109. 'key' in key &&
  27110. 'value' in key &&
  27111. key.value === null)
  27112. pair = new Pair(key.key, null);
  27113. else
  27114. pair = new Pair(key, null);
  27115. const prev = findPair(this.items, pair.key);
  27116. if (!prev)
  27117. this.items.push(pair);
  27118. }
  27119. /**
  27120. * If `keepPair` is `true`, returns the Pair matching `key`.
  27121. * Otherwise, returns the value of that Pair's key.
  27122. */
  27123. get(key, keepPair) {
  27124. const pair = findPair(this.items, key);
  27125. return !keepPair && isPair(pair)
  27126. ? isScalar$1(pair.key)
  27127. ? pair.key.value
  27128. : pair.key
  27129. : pair;
  27130. }
  27131. set(key, value) {
  27132. if (typeof value !== 'boolean')
  27133. throw new Error(`Expected boolean value for set(key, value) in a YAML set, not ${typeof value}`);
  27134. const prev = findPair(this.items, key);
  27135. if (prev && !value) {
  27136. this.items.splice(this.items.indexOf(prev), 1);
  27137. }
  27138. else if (!prev && value) {
  27139. this.items.push(new Pair(key));
  27140. }
  27141. }
  27142. toJSON(_, ctx) {
  27143. return super.toJSON(_, ctx, Set);
  27144. }
  27145. toString(ctx, onComment, onChompKeep) {
  27146. if (!ctx)
  27147. return JSON.stringify(this);
  27148. if (this.hasAllNullValues(true))
  27149. return super.toString(Object.assign({}, ctx, { allNullValues: true }), onComment, onChompKeep);
  27150. else
  27151. throw new Error('Set items must all have null values');
  27152. }
  27153. static from(schema, iterable, ctx) {
  27154. const { replacer } = ctx;
  27155. const set = new this(schema);
  27156. if (iterable && Symbol.iterator in Object(iterable))
  27157. for (let value of iterable) {
  27158. if (typeof replacer === 'function')
  27159. value = replacer.call(iterable, value, value);
  27160. set.items.push(createPair(value, null, ctx));
  27161. }
  27162. return set;
  27163. }
  27164. }
  27165. YAMLSet.tag = 'tag:yaml.org,2002:set';
  27166. const set = {
  27167. collection: 'map',
  27168. identify: value => value instanceof Set,
  27169. nodeClass: YAMLSet,
  27170. default: false,
  27171. tag: 'tag:yaml.org,2002:set',
  27172. createNode: (schema, iterable, ctx) => YAMLSet.from(schema, iterable, ctx),
  27173. resolve(map, onError) {
  27174. if (isMap(map)) {
  27175. if (map.hasAllNullValues(true))
  27176. return Object.assign(new YAMLSet(), map);
  27177. else
  27178. onError('Set items must all have null values');
  27179. }
  27180. else
  27181. onError('Expected a mapping for this tag');
  27182. return map;
  27183. }
  27184. };
  27185. /** Internal types handle bigint as number, because TS can't figure it out. */
  27186. function parseSexagesimal(str, asBigInt) {
  27187. const sign = str[0];
  27188. const parts = sign === '-' || sign === '+' ? str.substring(1) : str;
  27189. const num = (n) => asBigInt ? BigInt(n) : Number(n);
  27190. const res = parts
  27191. .replace(/_/g, '')
  27192. .split(':')
  27193. .reduce((res, p) => res * num(60) + num(p), num(0));
  27194. return (sign === '-' ? num(-1) * res : res);
  27195. }
  27196. /**
  27197. * hhhh:mm:ss.sss
  27198. *
  27199. * Internal types handle bigint as number, because TS can't figure it out.
  27200. */
  27201. function stringifySexagesimal(node) {
  27202. let { value } = node;
  27203. let num = (n) => n;
  27204. if (typeof value === 'bigint')
  27205. num = n => BigInt(n);
  27206. else if (isNaN(value) || !isFinite(value))
  27207. return stringifyNumber(node);
  27208. let sign = '';
  27209. if (value < 0) {
  27210. sign = '-';
  27211. value *= num(-1);
  27212. }
  27213. const _60 = num(60);
  27214. const parts = [value % _60]; // seconds, including ms
  27215. if (value < 60) {
  27216. parts.unshift(0); // at least one : is required
  27217. }
  27218. else {
  27219. value = (value - parts[0]) / _60;
  27220. parts.unshift(value % _60); // minutes
  27221. if (value >= 60) {
  27222. value = (value - parts[0]) / _60;
  27223. parts.unshift(value); // hours
  27224. }
  27225. }
  27226. return (sign +
  27227. parts
  27228. .map(n => String(n).padStart(2, '0'))
  27229. .join(':')
  27230. .replace(/000000\d*$/, '') // % 60 may introduce error
  27231. );
  27232. }
  27233. const intTime = {
  27234. identify: value => typeof value === 'bigint' || Number.isInteger(value),
  27235. default: true,
  27236. tag: 'tag:yaml.org,2002:int',
  27237. format: 'TIME',
  27238. test: /^[-+]?[0-9][0-9_]*(?::[0-5]?[0-9])+$/,
  27239. resolve: (str, _onError, { intAsBigInt }) => parseSexagesimal(str, intAsBigInt),
  27240. stringify: stringifySexagesimal
  27241. };
  27242. const floatTime = {
  27243. identify: value => typeof value === 'number',
  27244. default: true,
  27245. tag: 'tag:yaml.org,2002:float',
  27246. format: 'TIME',
  27247. test: /^[-+]?[0-9][0-9_]*(?::[0-5]?[0-9])+\.[0-9_]*$/,
  27248. resolve: str => parseSexagesimal(str, false),
  27249. stringify: stringifySexagesimal
  27250. };
  27251. const timestamp = {
  27252. identify: value => value instanceof Date,
  27253. default: true,
  27254. tag: 'tag:yaml.org,2002:timestamp',
  27255. // If the time zone is omitted, the timestamp is assumed to be specified in UTC. The time part
  27256. // may be omitted altogether, resulting in a date format. In such a case, the time part is
  27257. // assumed to be 00:00:00Z (start of day, UTC).
  27258. test: RegExp('^([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})' + // YYYY-Mm-Dd
  27259. '(?:' + // time is optional
  27260. '(?:t|T|[ \\t]+)' + // t | T | whitespace
  27261. '([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2}(\\.[0-9]+)?)' + // Hh:Mm:Ss(.ss)?
  27262. '(?:[ \\t]*(Z|[-+][012]?[0-9](?::[0-9]{2})?))?' + // Z | +5 | -03:30
  27263. ')?$'),
  27264. resolve(str) {
  27265. const match = str.match(timestamp.test);
  27266. if (!match)
  27267. throw new Error('!!timestamp expects a date, starting with yyyy-mm-dd');
  27268. const [, year, month, day, hour, minute, second] = match.map(Number);
  27269. const millisec = match[7] ? Number((match[7] + '00').substr(1, 3)) : 0;
  27270. let date = Date.UTC(year, month - 1, day, hour || 0, minute || 0, second || 0, millisec);
  27271. const tz = match[8];
  27272. if (tz && tz !== 'Z') {
  27273. let d = parseSexagesimal(tz, false);
  27274. if (Math.abs(d) < 30)
  27275. d *= 60;
  27276. date -= 60000 * d;
  27277. }
  27278. return new Date(date);
  27279. },
  27280. stringify: ({ value }) => value.toISOString().replace(/((T00:00)?:00)?\.000Z$/, '')
  27281. };
  27282. const schema = [
  27283. map$1,
  27284. seq,
  27285. string,
  27286. nullTag,
  27287. trueTag,
  27288. falseTag,
  27289. intBin,
  27290. intOct,
  27291. int,
  27292. intHex,
  27293. floatNaN,
  27294. floatExp,
  27295. float,
  27296. binary,
  27297. omap,
  27298. pairs,
  27299. set,
  27300. intTime,
  27301. floatTime,
  27302. timestamp
  27303. ];
  27304. const schemas = new Map([
  27305. ['core', schema$2],
  27306. ['failsafe', [map$1, seq, string]],
  27307. ['json', schema$1],
  27308. ['yaml11', schema],
  27309. ['yaml-1.1', schema]
  27310. ]);
  27311. const tagsByName = {
  27312. binary,
  27313. bool: boolTag,
  27314. float: float$1,
  27315. floatExp: floatExp$1,
  27316. floatNaN: floatNaN$1,
  27317. floatTime,
  27318. int: int$1,
  27319. intHex: intHex$1,
  27320. intOct: intOct$1,
  27321. intTime,
  27322. map: map$1,
  27323. null: nullTag,
  27324. omap,
  27325. pairs,
  27326. seq,
  27327. set,
  27328. timestamp
  27329. };
  27330. const coreKnownTags = {
  27331. 'tag:yaml.org,2002:binary': binary,
  27332. 'tag:yaml.org,2002:omap': omap,
  27333. 'tag:yaml.org,2002:pairs': pairs,
  27334. 'tag:yaml.org,2002:set': set,
  27335. 'tag:yaml.org,2002:timestamp': timestamp
  27336. };
  27337. function getTags(customTags, schemaName) {
  27338. let tags = schemas.get(schemaName);
  27339. if (!tags) {
  27340. if (Array.isArray(customTags))
  27341. tags = [];
  27342. else {
  27343. const keys = Array.from(schemas.keys())
  27344. .filter(key => key !== 'yaml11')
  27345. .map(key => JSON.stringify(key))
  27346. .join(', ');
  27347. throw new Error(`Unknown schema "${schemaName}"; use one of ${keys} or define customTags array`);
  27348. }
  27349. }
  27350. if (Array.isArray(customTags)) {
  27351. for (const tag of customTags)
  27352. tags = tags.concat(tag);
  27353. }
  27354. else if (typeof customTags === 'function') {
  27355. tags = customTags(tags.slice());
  27356. }
  27357. return tags.map(tag => {
  27358. if (typeof tag !== 'string')
  27359. return tag;
  27360. const tagObj = tagsByName[tag];
  27361. if (tagObj)
  27362. return tagObj;
  27363. const keys = Object.keys(tagsByName)
  27364. .map(key => JSON.stringify(key))
  27365. .join(', ');
  27366. throw new Error(`Unknown custom tag "${tag}"; use one of ${keys}`);
  27367. });
  27368. }
  27369. const sortMapEntriesByKey = (a, b) => a.key < b.key ? -1 : a.key > b.key ? 1 : 0;
  27370. class Schema {
  27371. constructor({ compat, customTags, merge, resolveKnownTags, schema, sortMapEntries, toStringDefaults }) {
  27372. this.compat = Array.isArray(compat)
  27373. ? getTags(compat, 'compat')
  27374. : compat
  27375. ? getTags(null, compat)
  27376. : null;
  27377. this.merge = !!merge;
  27378. this.name = (typeof schema === 'string' && schema) || 'core';
  27379. this.knownTags = resolveKnownTags ? coreKnownTags : {};
  27380. this.tags = getTags(customTags, this.name);
  27381. this.toStringOptions = toStringDefaults ?? null;
  27382. Object.defineProperty(this, MAP, { value: map$1 });
  27383. Object.defineProperty(this, SCALAR$1, { value: string });
  27384. Object.defineProperty(this, SEQ, { value: seq });
  27385. // Used by createMap()
  27386. this.sortMapEntries =
  27387. typeof sortMapEntries === 'function'
  27388. ? sortMapEntries
  27389. : sortMapEntries === true
  27390. ? sortMapEntriesByKey
  27391. : null;
  27392. }
  27393. clone() {
  27394. const copy = Object.create(Schema.prototype, Object.getOwnPropertyDescriptors(this));
  27395. copy.tags = this.tags.slice();
  27396. return copy;
  27397. }
  27398. }
  27399. function stringifyDocument(doc, options) {
  27400. const lines = [];
  27401. let hasDirectives = options.directives === true;
  27402. if (options.directives !== false && doc.directives) {
  27403. const dir = doc.directives.toString(doc);
  27404. if (dir) {
  27405. lines.push(dir);
  27406. hasDirectives = true;
  27407. }
  27408. else if (doc.directives.docStart)
  27409. hasDirectives = true;
  27410. }
  27411. if (hasDirectives)
  27412. lines.push('---');
  27413. const ctx = createStringifyContext(doc, options);
  27414. const { commentString } = ctx.options;
  27415. if (doc.commentBefore) {
  27416. if (lines.length !== 1)
  27417. lines.unshift('');
  27418. const cs = commentString(doc.commentBefore);
  27419. lines.unshift(indentComment(cs, ''));
  27420. }
  27421. let chompKeep = false;
  27422. let contentComment = null;
  27423. if (doc.contents) {
  27424. if (isNode$1(doc.contents)) {
  27425. if (doc.contents.spaceBefore && hasDirectives)
  27426. lines.push('');
  27427. if (doc.contents.commentBefore) {
  27428. const cs = commentString(doc.contents.commentBefore);
  27429. lines.push(indentComment(cs, ''));
  27430. }
  27431. // top-level block scalars need to be indented if followed by a comment
  27432. ctx.forceBlockIndent = !!doc.comment;
  27433. contentComment = doc.contents.comment;
  27434. }
  27435. const onChompKeep = contentComment ? undefined : () => (chompKeep = true);
  27436. let body = stringify$2(doc.contents, ctx, () => (contentComment = null), onChompKeep);
  27437. if (contentComment)
  27438. body += lineComment(body, '', commentString(contentComment));
  27439. if ((body[0] === '|' || body[0] === '>') &&
  27440. lines[lines.length - 1] === '---') {
  27441. // Top-level block scalars with a preceding doc marker ought to use the
  27442. // same line for their header.
  27443. lines[lines.length - 1] = `--- ${body}`;
  27444. }
  27445. else
  27446. lines.push(body);
  27447. }
  27448. else {
  27449. lines.push(stringify$2(doc.contents, ctx));
  27450. }
  27451. if (doc.directives?.docEnd) {
  27452. if (doc.comment) {
  27453. const cs = commentString(doc.comment);
  27454. if (cs.includes('\n')) {
  27455. lines.push('...');
  27456. lines.push(indentComment(cs, ''));
  27457. }
  27458. else {
  27459. lines.push(`... ${cs}`);
  27460. }
  27461. }
  27462. else {
  27463. lines.push('...');
  27464. }
  27465. }
  27466. else {
  27467. let dc = doc.comment;
  27468. if (dc && chompKeep)
  27469. dc = dc.replace(/^\n+/, '');
  27470. if (dc) {
  27471. if ((!chompKeep || contentComment) && lines[lines.length - 1] !== '')
  27472. lines.push('');
  27473. lines.push(indentComment(commentString(dc), ''));
  27474. }
  27475. }
  27476. return lines.join('\n') + '\n';
  27477. }
  27478. class Document {
  27479. constructor(value, replacer, options) {
  27480. /** A comment before this Document */
  27481. this.commentBefore = null;
  27482. /** A comment immediately after this Document */
  27483. this.comment = null;
  27484. /** Errors encountered during parsing. */
  27485. this.errors = [];
  27486. /** Warnings encountered during parsing. */
  27487. this.warnings = [];
  27488. Object.defineProperty(this, NODE_TYPE, { value: DOC });
  27489. let _replacer = null;
  27490. if (typeof replacer === 'function' || Array.isArray(replacer)) {
  27491. _replacer = replacer;
  27492. }
  27493. else if (options === undefined && replacer) {
  27494. options = replacer;
  27495. replacer = undefined;
  27496. }
  27497. const opt = Object.assign({
  27498. intAsBigInt: false,
  27499. keepSourceTokens: false,
  27500. logLevel: 'warn',
  27501. prettyErrors: true,
  27502. strict: true,
  27503. uniqueKeys: true,
  27504. version: '1.2'
  27505. }, options);
  27506. this.options = opt;
  27507. let { version } = opt;
  27508. if (options?._directives) {
  27509. this.directives = options._directives.atDocument();
  27510. if (this.directives.yaml.explicit)
  27511. version = this.directives.yaml.version;
  27512. }
  27513. else
  27514. this.directives = new Directives({ version });
  27515. this.setSchema(version, options);
  27516. // @ts-expect-error We can't really know that this matches Contents.
  27517. this.contents =
  27518. value === undefined ? null : this.createNode(value, _replacer, options);
  27519. }
  27520. /**
  27521. * Create a deep copy of this Document and its contents.
  27522. *
  27523. * Custom Node values that inherit from `Object` still refer to their original instances.
  27524. */
  27525. clone() {
  27526. const copy = Object.create(Document.prototype, {
  27527. [NODE_TYPE]: { value: DOC }
  27528. });
  27529. copy.commentBefore = this.commentBefore;
  27530. copy.comment = this.comment;
  27531. copy.errors = this.errors.slice();
  27532. copy.warnings = this.warnings.slice();
  27533. copy.options = Object.assign({}, this.options);
  27534. if (this.directives)
  27535. copy.directives = this.directives.clone();
  27536. copy.schema = this.schema.clone();
  27537. // @ts-expect-error We can't really know that this matches Contents.
  27538. copy.contents = isNode$1(this.contents)
  27539. ? this.contents.clone(copy.schema)
  27540. : this.contents;
  27541. if (this.range)
  27542. copy.range = this.range.slice();
  27543. return copy;
  27544. }
  27545. /** Adds a value to the document. */
  27546. add(value) {
  27547. if (assertCollection(this.contents))
  27548. this.contents.add(value);
  27549. }
  27550. /** Adds a value to the document. */
  27551. addIn(path, value) {
  27552. if (assertCollection(this.contents))
  27553. this.contents.addIn(path, value);
  27554. }
  27555. /**
  27556. * Create a new `Alias` node, ensuring that the target `node` has the required anchor.
  27557. *
  27558. * If `node` already has an anchor, `name` is ignored.
  27559. * Otherwise, the `node.anchor` value will be set to `name`,
  27560. * or if an anchor with that name is already present in the document,
  27561. * `name` will be used as a prefix for a new unique anchor.
  27562. * If `name` is undefined, the generated anchor will use 'a' as a prefix.
  27563. */
  27564. createAlias(node, name) {
  27565. if (!node.anchor) {
  27566. const prev = anchorNames(this);
  27567. node.anchor =
  27568. // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
  27569. !name || prev.has(name) ? findNewAnchor(name || 'a', prev) : name;
  27570. }
  27571. return new Alias(node.anchor);
  27572. }
  27573. createNode(value, replacer, options) {
  27574. let _replacer = undefined;
  27575. if (typeof replacer === 'function') {
  27576. value = replacer.call({ '': value }, '', value);
  27577. _replacer = replacer;
  27578. }
  27579. else if (Array.isArray(replacer)) {
  27580. const keyToStr = (v) => typeof v === 'number' || v instanceof String || v instanceof Number;
  27581. const asStr = replacer.filter(keyToStr).map(String);
  27582. if (asStr.length > 0)
  27583. replacer = replacer.concat(asStr);
  27584. _replacer = replacer;
  27585. }
  27586. else if (options === undefined && replacer) {
  27587. options = replacer;
  27588. replacer = undefined;
  27589. }
  27590. const { aliasDuplicateObjects, anchorPrefix, flow, keepUndefined, onTagObj, tag } = options ?? {};
  27591. const { onAnchor, setAnchors, sourceObjects } = createNodeAnchors(this,
  27592. // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
  27593. anchorPrefix || 'a');
  27594. const ctx = {
  27595. aliasDuplicateObjects: aliasDuplicateObjects ?? true,
  27596. keepUndefined: keepUndefined ?? false,
  27597. onAnchor,
  27598. onTagObj,
  27599. replacer: _replacer,
  27600. schema: this.schema,
  27601. sourceObjects
  27602. };
  27603. const node = createNode(value, tag, ctx);
  27604. if (flow && isCollection$1(node))
  27605. node.flow = true;
  27606. setAnchors();
  27607. return node;
  27608. }
  27609. /**
  27610. * Convert a key and a value into a `Pair` using the current schema,
  27611. * recursively wrapping all values as `Scalar` or `Collection` nodes.
  27612. */
  27613. createPair(key, value, options = {}) {
  27614. const k = this.createNode(key, null, options);
  27615. const v = this.createNode(value, null, options);
  27616. return new Pair(k, v);
  27617. }
  27618. /**
  27619. * Removes a value from the document.
  27620. * @returns `true` if the item was found and removed.
  27621. */
  27622. delete(key) {
  27623. return assertCollection(this.contents) ? this.contents.delete(key) : false;
  27624. }
  27625. /**
  27626. * Removes a value from the document.
  27627. * @returns `true` if the item was found and removed.
  27628. */
  27629. deleteIn(path) {
  27630. if (isEmptyPath(path)) {
  27631. if (this.contents == null)
  27632. return false;
  27633. // @ts-expect-error Presumed impossible if Strict extends false
  27634. this.contents = null;
  27635. return true;
  27636. }
  27637. return assertCollection(this.contents)
  27638. ? this.contents.deleteIn(path)
  27639. : false;
  27640. }
  27641. /**
  27642. * Returns item at `key`, or `undefined` if not found. By default unwraps
  27643. * scalar values from their surrounding node; to disable set `keepScalar` to
  27644. * `true` (collections are always returned intact).
  27645. */
  27646. get(key, keepScalar) {
  27647. return isCollection$1(this.contents)
  27648. ? this.contents.get(key, keepScalar)
  27649. : undefined;
  27650. }
  27651. /**
  27652. * Returns item at `path`, or `undefined` if not found. By default unwraps
  27653. * scalar values from their surrounding node; to disable set `keepScalar` to
  27654. * `true` (collections are always returned intact).
  27655. */
  27656. getIn(path, keepScalar) {
  27657. if (isEmptyPath(path))
  27658. return !keepScalar && isScalar$1(this.contents)
  27659. ? this.contents.value
  27660. : this.contents;
  27661. return isCollection$1(this.contents)
  27662. ? this.contents.getIn(path, keepScalar)
  27663. : undefined;
  27664. }
  27665. /**
  27666. * Checks if the document includes a value with the key `key`.
  27667. */
  27668. has(key) {
  27669. return isCollection$1(this.contents) ? this.contents.has(key) : false;
  27670. }
  27671. /**
  27672. * Checks if the document includes a value at `path`.
  27673. */
  27674. hasIn(path) {
  27675. if (isEmptyPath(path))
  27676. return this.contents !== undefined;
  27677. return isCollection$1(this.contents) ? this.contents.hasIn(path) : false;
  27678. }
  27679. /**
  27680. * Sets a value in this document. For `!!set`, `value` needs to be a
  27681. * boolean to add/remove the item from the set.
  27682. */
  27683. set(key, value) {
  27684. if (this.contents == null) {
  27685. // @ts-expect-error We can't really know that this matches Contents.
  27686. this.contents = collectionFromPath(this.schema, [key], value);
  27687. }
  27688. else if (assertCollection(this.contents)) {
  27689. this.contents.set(key, value);
  27690. }
  27691. }
  27692. /**
  27693. * Sets a value in this document. For `!!set`, `value` needs to be a
  27694. * boolean to add/remove the item from the set.
  27695. */
  27696. setIn(path, value) {
  27697. if (isEmptyPath(path)) {
  27698. // @ts-expect-error We can't really know that this matches Contents.
  27699. this.contents = value;
  27700. }
  27701. else if (this.contents == null) {
  27702. // @ts-expect-error We can't really know that this matches Contents.
  27703. this.contents = collectionFromPath(this.schema, Array.from(path), value);
  27704. }
  27705. else if (assertCollection(this.contents)) {
  27706. this.contents.setIn(path, value);
  27707. }
  27708. }
  27709. /**
  27710. * Change the YAML version and schema used by the document.
  27711. * A `null` version disables support for directives, explicit tags, anchors, and aliases.
  27712. * It also requires the `schema` option to be given as a `Schema` instance value.
  27713. *
  27714. * Overrides all previously set schema options.
  27715. */
  27716. setSchema(version, options = {}) {
  27717. if (typeof version === 'number')
  27718. version = String(version);
  27719. let opt;
  27720. switch (version) {
  27721. case '1.1':
  27722. if (this.directives)
  27723. this.directives.yaml.version = '1.1';
  27724. else
  27725. this.directives = new Directives({ version: '1.1' });
  27726. opt = { merge: true, resolveKnownTags: false, schema: 'yaml-1.1' };
  27727. break;
  27728. case '1.2':
  27729. case 'next':
  27730. if (this.directives)
  27731. this.directives.yaml.version = version;
  27732. else
  27733. this.directives = new Directives({ version });
  27734. opt = { merge: false, resolveKnownTags: true, schema: 'core' };
  27735. break;
  27736. case null:
  27737. if (this.directives)
  27738. delete this.directives;
  27739. opt = null;
  27740. break;
  27741. default: {
  27742. const sv = JSON.stringify(version);
  27743. throw new Error(`Expected '1.1', '1.2' or null as first argument, but found: ${sv}`);
  27744. }
  27745. }
  27746. // Not using `instanceof Schema` to allow for duck typing
  27747. if (options.schema instanceof Object)
  27748. this.schema = options.schema;
  27749. else if (opt)
  27750. this.schema = new Schema(Object.assign(opt, options));
  27751. else
  27752. throw new Error(`With a null YAML version, the { schema: Schema } option is required`);
  27753. }
  27754. // json & jsonArg are only used from toJSON()
  27755. toJS({ json, jsonArg, mapAsMap, maxAliasCount, onAnchor, reviver } = {}) {
  27756. const ctx = {
  27757. anchors: new Map(),
  27758. doc: this,
  27759. keep: !json,
  27760. mapAsMap: mapAsMap === true,
  27761. mapKeyWarned: false,
  27762. maxAliasCount: typeof maxAliasCount === 'number' ? maxAliasCount : 100
  27763. };
  27764. const res = toJS(this.contents, jsonArg ?? '', ctx);
  27765. if (typeof onAnchor === 'function')
  27766. for (const { count, res } of ctx.anchors.values())
  27767. onAnchor(res, count);
  27768. return typeof reviver === 'function'
  27769. ? applyReviver(reviver, { '': res }, '', res)
  27770. : res;
  27771. }
  27772. /**
  27773. * A JSON representation of the document `contents`.
  27774. *
  27775. * @param jsonArg Used by `JSON.stringify` to indicate the array index or
  27776. * property name.
  27777. */
  27778. toJSON(jsonArg, onAnchor) {
  27779. return this.toJS({ json: true, jsonArg, mapAsMap: false, onAnchor });
  27780. }
  27781. /** A YAML representation of the document. */
  27782. toString(options = {}) {
  27783. if (this.errors.length > 0)
  27784. throw new Error('Document with errors cannot be stringified');
  27785. if ('indent' in options &&
  27786. (!Number.isInteger(options.indent) || Number(options.indent) <= 0)) {
  27787. const s = JSON.stringify(options.indent);
  27788. throw new Error(`"indent" option must be a positive integer, not ${s}`);
  27789. }
  27790. return stringifyDocument(this, options);
  27791. }
  27792. }
  27793. function assertCollection(contents) {
  27794. if (isCollection$1(contents))
  27795. return true;
  27796. throw new Error('Expected a YAML collection as document contents');
  27797. }
  27798. class YAMLError extends Error {
  27799. constructor(name, pos, code, message) {
  27800. super();
  27801. this.name = name;
  27802. this.code = code;
  27803. this.message = message;
  27804. this.pos = pos;
  27805. }
  27806. }
  27807. class YAMLParseError extends YAMLError {
  27808. constructor(pos, code, message) {
  27809. super('YAMLParseError', pos, code, message);
  27810. }
  27811. }
  27812. class YAMLWarning extends YAMLError {
  27813. constructor(pos, code, message) {
  27814. super('YAMLWarning', pos, code, message);
  27815. }
  27816. }
  27817. const prettifyError = (src, lc) => (error) => {
  27818. if (error.pos[0] === -1)
  27819. return;
  27820. error.linePos = error.pos.map(pos => lc.linePos(pos));
  27821. const { line, col } = error.linePos[0];
  27822. error.message += ` at line ${line}, column ${col}`;
  27823. let ci = col - 1;
  27824. let lineStr = src
  27825. .substring(lc.lineStarts[line - 1], lc.lineStarts[line])
  27826. .replace(/[\n\r]+$/, '');
  27827. // Trim to max 80 chars, keeping col position near the middle
  27828. if (ci >= 60 && lineStr.length > 80) {
  27829. const trimStart = Math.min(ci - 39, lineStr.length - 79);
  27830. lineStr = '…' + lineStr.substring(trimStart);
  27831. ci -= trimStart - 1;
  27832. }
  27833. if (lineStr.length > 80)
  27834. lineStr = lineStr.substring(0, 79) + '…';
  27835. // Include previous line in context if pointing at line start
  27836. if (line > 1 && /^ *$/.test(lineStr.substring(0, ci))) {
  27837. // Regexp won't match if start is trimmed
  27838. let prev = src.substring(lc.lineStarts[line - 2], lc.lineStarts[line - 1]);
  27839. if (prev.length > 80)
  27840. prev = prev.substring(0, 79) + '…\n';
  27841. lineStr = prev + lineStr;
  27842. }
  27843. if (/[^ ]/.test(lineStr)) {
  27844. let count = 1;
  27845. const end = error.linePos[1];
  27846. if (end && end.line === line && end.col > col) {
  27847. count = Math.max(1, Math.min(end.col - col, 80 - ci));
  27848. }
  27849. const pointer = ' '.repeat(ci) + '^'.repeat(count);
  27850. error.message += `:\n\n${lineStr}\n${pointer}\n`;
  27851. }
  27852. };
  27853. function resolveProps(tokens, { flow, indicator, next, offset, onError, parentIndent, startOnNewline }) {
  27854. let spaceBefore = false;
  27855. let atNewline = startOnNewline;
  27856. let hasSpace = startOnNewline;
  27857. let comment = '';
  27858. let commentSep = '';
  27859. let hasNewline = false;
  27860. let reqSpace = false;
  27861. let tab = null;
  27862. let anchor = null;
  27863. let tag = null;
  27864. let newlineAfterProp = null;
  27865. let comma = null;
  27866. let found = null;
  27867. let start = null;
  27868. for (const token of tokens) {
  27869. if (reqSpace) {
  27870. if (token.type !== 'space' &&
  27871. token.type !== 'newline' &&
  27872. token.type !== 'comma')
  27873. onError(token.offset, 'MISSING_CHAR', 'Tags and anchors must be separated from the next token by white space');
  27874. reqSpace = false;
  27875. }
  27876. if (tab) {
  27877. if (atNewline && token.type !== 'comment' && token.type !== 'newline') {
  27878. onError(tab, 'TAB_AS_INDENT', 'Tabs are not allowed as indentation');
  27879. }
  27880. tab = null;
  27881. }
  27882. switch (token.type) {
  27883. case 'space':
  27884. // At the doc level, tabs at line start may be parsed
  27885. // as leading white space rather than indentation.
  27886. // In a flow collection, only the parser handles indent.
  27887. if (!flow &&
  27888. (indicator !== 'doc-start' || next?.type !== 'flow-collection') &&
  27889. token.source.includes('\t')) {
  27890. tab = token;
  27891. }
  27892. hasSpace = true;
  27893. break;
  27894. case 'comment': {
  27895. if (!hasSpace)
  27896. onError(token, 'MISSING_CHAR', 'Comments must be separated from other tokens by white space characters');
  27897. const cb = token.source.substring(1) || ' ';
  27898. if (!comment)
  27899. comment = cb;
  27900. else
  27901. comment += commentSep + cb;
  27902. commentSep = '';
  27903. atNewline = false;
  27904. break;
  27905. }
  27906. case 'newline':
  27907. if (atNewline) {
  27908. if (comment)
  27909. comment += token.source;
  27910. else
  27911. spaceBefore = true;
  27912. }
  27913. else
  27914. commentSep += token.source;
  27915. atNewline = true;
  27916. hasNewline = true;
  27917. if (anchor || tag)
  27918. newlineAfterProp = token;
  27919. hasSpace = true;
  27920. break;
  27921. case 'anchor':
  27922. if (anchor)
  27923. onError(token, 'MULTIPLE_ANCHORS', 'A node can have at most one anchor');
  27924. if (token.source.endsWith(':'))
  27925. onError(token.offset + token.source.length - 1, 'BAD_ALIAS', 'Anchor ending in : is ambiguous', true);
  27926. anchor = token;
  27927. if (start === null)
  27928. start = token.offset;
  27929. atNewline = false;
  27930. hasSpace = false;
  27931. reqSpace = true;
  27932. break;
  27933. case 'tag': {
  27934. if (tag)
  27935. onError(token, 'MULTIPLE_TAGS', 'A node can have at most one tag');
  27936. tag = token;
  27937. if (start === null)
  27938. start = token.offset;
  27939. atNewline = false;
  27940. hasSpace = false;
  27941. reqSpace = true;
  27942. break;
  27943. }
  27944. case indicator:
  27945. // Could here handle preceding comments differently
  27946. if (anchor || tag)
  27947. onError(token, 'BAD_PROP_ORDER', `Anchors and tags must be after the ${token.source} indicator`);
  27948. if (found)
  27949. onError(token, 'UNEXPECTED_TOKEN', `Unexpected ${token.source} in ${flow ?? 'collection'}`);
  27950. found = token;
  27951. atNewline =
  27952. indicator === 'seq-item-ind' || indicator === 'explicit-key-ind';
  27953. hasSpace = false;
  27954. break;
  27955. case 'comma':
  27956. if (flow) {
  27957. if (comma)
  27958. onError(token, 'UNEXPECTED_TOKEN', `Unexpected , in ${flow}`);
  27959. comma = token;
  27960. atNewline = false;
  27961. hasSpace = false;
  27962. break;
  27963. }
  27964. // else fallthrough
  27965. default:
  27966. onError(token, 'UNEXPECTED_TOKEN', `Unexpected ${token.type} token`);
  27967. atNewline = false;
  27968. hasSpace = false;
  27969. }
  27970. }
  27971. const last = tokens[tokens.length - 1];
  27972. const end = last ? last.offset + last.source.length : offset;
  27973. if (reqSpace &&
  27974. next &&
  27975. next.type !== 'space' &&
  27976. next.type !== 'newline' &&
  27977. next.type !== 'comma' &&
  27978. (next.type !== 'scalar' || next.source !== '')) {
  27979. onError(next.offset, 'MISSING_CHAR', 'Tags and anchors must be separated from the next token by white space');
  27980. }
  27981. if (tab &&
  27982. ((atNewline && tab.indent <= parentIndent) ||
  27983. next?.type === 'block-map' ||
  27984. next?.type === 'block-seq'))
  27985. onError(tab, 'TAB_AS_INDENT', 'Tabs are not allowed as indentation');
  27986. return {
  27987. comma,
  27988. found,
  27989. spaceBefore,
  27990. comment,
  27991. hasNewline,
  27992. anchor,
  27993. tag,
  27994. newlineAfterProp,
  27995. end,
  27996. start: start ?? end
  27997. };
  27998. }
  27999. function containsNewline(key) {
  28000. if (!key)
  28001. return null;
  28002. switch (key.type) {
  28003. case 'alias':
  28004. case 'scalar':
  28005. case 'double-quoted-scalar':
  28006. case 'single-quoted-scalar':
  28007. if (key.source.includes('\n'))
  28008. return true;
  28009. if (key.end)
  28010. for (const st of key.end)
  28011. if (st.type === 'newline')
  28012. return true;
  28013. return false;
  28014. case 'flow-collection':
  28015. for (const it of key.items) {
  28016. for (const st of it.start)
  28017. if (st.type === 'newline')
  28018. return true;
  28019. if (it.sep)
  28020. for (const st of it.sep)
  28021. if (st.type === 'newline')
  28022. return true;
  28023. if (containsNewline(it.key) || containsNewline(it.value))
  28024. return true;
  28025. }
  28026. return false;
  28027. default:
  28028. return true;
  28029. }
  28030. }
  28031. function flowIndentCheck(indent, fc, onError) {
  28032. if (fc?.type === 'flow-collection') {
  28033. const end = fc.end[0];
  28034. if (end.indent === indent &&
  28035. (end.source === ']' || end.source === '}') &&
  28036. containsNewline(fc)) {
  28037. const msg = 'Flow end indicator should be more indented than parent';
  28038. onError(end, 'BAD_INDENT', msg, true);
  28039. }
  28040. }
  28041. }
  28042. function mapIncludes(ctx, items, search) {
  28043. const { uniqueKeys } = ctx.options;
  28044. if (uniqueKeys === false)
  28045. return false;
  28046. const isEqual = typeof uniqueKeys === 'function'
  28047. ? uniqueKeys
  28048. : (a, b) => a === b ||
  28049. (isScalar$1(a) &&
  28050. isScalar$1(b) &&
  28051. a.value === b.value &&
  28052. !(a.value === '<<' && ctx.schema.merge));
  28053. return items.some(pair => isEqual(pair.key, search));
  28054. }
  28055. const startColMsg = 'All mapping items must start at the same column';
  28056. function resolveBlockMap({ composeNode, composeEmptyNode }, ctx, bm, onError, tag) {
  28057. const NodeClass = tag?.nodeClass ?? YAMLMap;
  28058. const map = new NodeClass(ctx.schema);
  28059. if (ctx.atRoot)
  28060. ctx.atRoot = false;
  28061. let offset = bm.offset;
  28062. let commentEnd = null;
  28063. for (const collItem of bm.items) {
  28064. const { start, key, sep, value } = collItem;
  28065. // key properties
  28066. const keyProps = resolveProps(start, {
  28067. indicator: 'explicit-key-ind',
  28068. next: key ?? sep?.[0],
  28069. offset,
  28070. onError,
  28071. parentIndent: bm.indent,
  28072. startOnNewline: true
  28073. });
  28074. const implicitKey = !keyProps.found;
  28075. if (implicitKey) {
  28076. if (key) {
  28077. if (key.type === 'block-seq')
  28078. onError(offset, 'BLOCK_AS_IMPLICIT_KEY', 'A block sequence may not be used as an implicit map key');
  28079. else if ('indent' in key && key.indent !== bm.indent)
  28080. onError(offset, 'BAD_INDENT', startColMsg);
  28081. }
  28082. if (!keyProps.anchor && !keyProps.tag && !sep) {
  28083. commentEnd = keyProps.end;
  28084. if (keyProps.comment) {
  28085. if (map.comment)
  28086. map.comment += '\n' + keyProps.comment;
  28087. else
  28088. map.comment = keyProps.comment;
  28089. }
  28090. continue;
  28091. }
  28092. if (keyProps.newlineAfterProp || containsNewline(key)) {
  28093. onError(key ?? start[start.length - 1], 'MULTILINE_IMPLICIT_KEY', 'Implicit keys need to be on a single line');
  28094. }
  28095. }
  28096. else if (keyProps.found?.indent !== bm.indent) {
  28097. onError(offset, 'BAD_INDENT', startColMsg);
  28098. }
  28099. // key value
  28100. const keyStart = keyProps.end;
  28101. const keyNode = key
  28102. ? composeNode(ctx, key, keyProps, onError)
  28103. : composeEmptyNode(ctx, keyStart, start, null, keyProps, onError);
  28104. if (ctx.schema.compat)
  28105. flowIndentCheck(bm.indent, key, onError);
  28106. if (mapIncludes(ctx, map.items, keyNode))
  28107. onError(keyStart, 'DUPLICATE_KEY', 'Map keys must be unique');
  28108. // value properties
  28109. const valueProps = resolveProps(sep ?? [], {
  28110. indicator: 'map-value-ind',
  28111. next: value,
  28112. offset: keyNode.range[2],
  28113. onError,
  28114. parentIndent: bm.indent,
  28115. startOnNewline: !key || key.type === 'block-scalar'
  28116. });
  28117. offset = valueProps.end;
  28118. if (valueProps.found) {
  28119. if (implicitKey) {
  28120. if (value?.type === 'block-map' && !valueProps.hasNewline)
  28121. onError(offset, 'BLOCK_AS_IMPLICIT_KEY', 'Nested mappings are not allowed in compact mappings');
  28122. if (ctx.options.strict &&
  28123. keyProps.start < valueProps.found.offset - 1024)
  28124. onError(keyNode.range, 'KEY_OVER_1024_CHARS', 'The : indicator must be at most 1024 chars after the start of an implicit block mapping key');
  28125. }
  28126. // value value
  28127. const valueNode = value
  28128. ? composeNode(ctx, value, valueProps, onError)
  28129. : composeEmptyNode(ctx, offset, sep, null, valueProps, onError);
  28130. if (ctx.schema.compat)
  28131. flowIndentCheck(bm.indent, value, onError);
  28132. offset = valueNode.range[2];
  28133. const pair = new Pair(keyNode, valueNode);
  28134. if (ctx.options.keepSourceTokens)
  28135. pair.srcToken = collItem;
  28136. map.items.push(pair);
  28137. }
  28138. else {
  28139. // key with no value
  28140. if (implicitKey)
  28141. onError(keyNode.range, 'MISSING_CHAR', 'Implicit map keys need to be followed by map values');
  28142. if (valueProps.comment) {
  28143. if (keyNode.comment)
  28144. keyNode.comment += '\n' + valueProps.comment;
  28145. else
  28146. keyNode.comment = valueProps.comment;
  28147. }
  28148. const pair = new Pair(keyNode);
  28149. if (ctx.options.keepSourceTokens)
  28150. pair.srcToken = collItem;
  28151. map.items.push(pair);
  28152. }
  28153. }
  28154. if (commentEnd && commentEnd < offset)
  28155. onError(commentEnd, 'IMPOSSIBLE', 'Map comment with trailing content');
  28156. map.range = [bm.offset, offset, commentEnd ?? offset];
  28157. return map;
  28158. }
  28159. function resolveBlockSeq({ composeNode, composeEmptyNode }, ctx, bs, onError, tag) {
  28160. const NodeClass = tag?.nodeClass ?? YAMLSeq;
  28161. const seq = new NodeClass(ctx.schema);
  28162. if (ctx.atRoot)
  28163. ctx.atRoot = false;
  28164. let offset = bs.offset;
  28165. let commentEnd = null;
  28166. for (const { start, value } of bs.items) {
  28167. const props = resolveProps(start, {
  28168. indicator: 'seq-item-ind',
  28169. next: value,
  28170. offset,
  28171. onError,
  28172. parentIndent: bs.indent,
  28173. startOnNewline: true
  28174. });
  28175. if (!props.found) {
  28176. if (props.anchor || props.tag || value) {
  28177. if (value && value.type === 'block-seq')
  28178. onError(props.end, 'BAD_INDENT', 'All sequence items must start at the same column');
  28179. else
  28180. onError(offset, 'MISSING_CHAR', 'Sequence item without - indicator');
  28181. }
  28182. else {
  28183. commentEnd = props.end;
  28184. if (props.comment)
  28185. seq.comment = props.comment;
  28186. continue;
  28187. }
  28188. }
  28189. const node = value
  28190. ? composeNode(ctx, value, props, onError)
  28191. : composeEmptyNode(ctx, props.end, start, null, props, onError);
  28192. if (ctx.schema.compat)
  28193. flowIndentCheck(bs.indent, value, onError);
  28194. offset = node.range[2];
  28195. seq.items.push(node);
  28196. }
  28197. seq.range = [bs.offset, offset, commentEnd ?? offset];
  28198. return seq;
  28199. }
  28200. function resolveEnd(end, offset, reqSpace, onError) {
  28201. let comment = '';
  28202. if (end) {
  28203. let hasSpace = false;
  28204. let sep = '';
  28205. for (const token of end) {
  28206. const { source, type } = token;
  28207. switch (type) {
  28208. case 'space':
  28209. hasSpace = true;
  28210. break;
  28211. case 'comment': {
  28212. if (reqSpace && !hasSpace)
  28213. onError(token, 'MISSING_CHAR', 'Comments must be separated from other tokens by white space characters');
  28214. const cb = source.substring(1) || ' ';
  28215. if (!comment)
  28216. comment = cb;
  28217. else
  28218. comment += sep + cb;
  28219. sep = '';
  28220. break;
  28221. }
  28222. case 'newline':
  28223. if (comment)
  28224. sep += source;
  28225. hasSpace = true;
  28226. break;
  28227. default:
  28228. onError(token, 'UNEXPECTED_TOKEN', `Unexpected ${type} at node end`);
  28229. }
  28230. offset += source.length;
  28231. }
  28232. }
  28233. return { comment, offset };
  28234. }
  28235. const blockMsg = 'Block collections are not allowed within flow collections';
  28236. const isBlock$1 = (token) => token && (token.type === 'block-map' || token.type === 'block-seq');
  28237. function resolveFlowCollection({ composeNode, composeEmptyNode }, ctx, fc, onError, tag) {
  28238. const isMap = fc.start.source === '{';
  28239. const fcName = isMap ? 'flow map' : 'flow sequence';
  28240. const NodeClass = (tag?.nodeClass ?? (isMap ? YAMLMap : YAMLSeq));
  28241. const coll = new NodeClass(ctx.schema);
  28242. coll.flow = true;
  28243. const atRoot = ctx.atRoot;
  28244. if (atRoot)
  28245. ctx.atRoot = false;
  28246. let offset = fc.offset + fc.start.source.length;
  28247. for (let i = 0; i < fc.items.length; ++i) {
  28248. const collItem = fc.items[i];
  28249. const { start, key, sep, value } = collItem;
  28250. const props = resolveProps(start, {
  28251. flow: fcName,
  28252. indicator: 'explicit-key-ind',
  28253. next: key ?? sep?.[0],
  28254. offset,
  28255. onError,
  28256. parentIndent: fc.indent,
  28257. startOnNewline: false
  28258. });
  28259. if (!props.found) {
  28260. if (!props.anchor && !props.tag && !sep && !value) {
  28261. if (i === 0 && props.comma)
  28262. onError(props.comma, 'UNEXPECTED_TOKEN', `Unexpected , in ${fcName}`);
  28263. else if (i < fc.items.length - 1)
  28264. onError(props.start, 'UNEXPECTED_TOKEN', `Unexpected empty item in ${fcName}`);
  28265. if (props.comment) {
  28266. if (coll.comment)
  28267. coll.comment += '\n' + props.comment;
  28268. else
  28269. coll.comment = props.comment;
  28270. }
  28271. offset = props.end;
  28272. continue;
  28273. }
  28274. if (!isMap && ctx.options.strict && containsNewline(key))
  28275. onError(key, // checked by containsNewline()
  28276. 'MULTILINE_IMPLICIT_KEY', 'Implicit keys of flow sequence pairs need to be on a single line');
  28277. }
  28278. if (i === 0) {
  28279. if (props.comma)
  28280. onError(props.comma, 'UNEXPECTED_TOKEN', `Unexpected , in ${fcName}`);
  28281. }
  28282. else {
  28283. if (!props.comma)
  28284. onError(props.start, 'MISSING_CHAR', `Missing , between ${fcName} items`);
  28285. if (props.comment) {
  28286. let prevItemComment = '';
  28287. loop: for (const st of start) {
  28288. switch (st.type) {
  28289. case 'comma':
  28290. case 'space':
  28291. break;
  28292. case 'comment':
  28293. prevItemComment = st.source.substring(1);
  28294. break loop;
  28295. default:
  28296. break loop;
  28297. }
  28298. }
  28299. if (prevItemComment) {
  28300. let prev = coll.items[coll.items.length - 1];
  28301. if (isPair(prev))
  28302. prev = prev.value ?? prev.key;
  28303. if (prev.comment)
  28304. prev.comment += '\n' + prevItemComment;
  28305. else
  28306. prev.comment = prevItemComment;
  28307. props.comment = props.comment.substring(prevItemComment.length + 1);
  28308. }
  28309. }
  28310. }
  28311. if (!isMap && !sep && !props.found) {
  28312. // item is a value in a seq
  28313. // → key & sep are empty, start does not include ? or :
  28314. const valueNode = value
  28315. ? composeNode(ctx, value, props, onError)
  28316. : composeEmptyNode(ctx, props.end, sep, null, props, onError);
  28317. coll.items.push(valueNode);
  28318. offset = valueNode.range[2];
  28319. if (isBlock$1(value))
  28320. onError(valueNode.range, 'BLOCK_IN_FLOW', blockMsg);
  28321. }
  28322. else {
  28323. // item is a key+value pair
  28324. // key value
  28325. const keyStart = props.end;
  28326. const keyNode = key
  28327. ? composeNode(ctx, key, props, onError)
  28328. : composeEmptyNode(ctx, keyStart, start, null, props, onError);
  28329. if (isBlock$1(key))
  28330. onError(keyNode.range, 'BLOCK_IN_FLOW', blockMsg);
  28331. // value properties
  28332. const valueProps = resolveProps(sep ?? [], {
  28333. flow: fcName,
  28334. indicator: 'map-value-ind',
  28335. next: value,
  28336. offset: keyNode.range[2],
  28337. onError,
  28338. parentIndent: fc.indent,
  28339. startOnNewline: false
  28340. });
  28341. if (valueProps.found) {
  28342. if (!isMap && !props.found && ctx.options.strict) {
  28343. if (sep)
  28344. for (const st of sep) {
  28345. if (st === valueProps.found)
  28346. break;
  28347. if (st.type === 'newline') {
  28348. onError(st, 'MULTILINE_IMPLICIT_KEY', 'Implicit keys of flow sequence pairs need to be on a single line');
  28349. break;
  28350. }
  28351. }
  28352. if (props.start < valueProps.found.offset - 1024)
  28353. onError(valueProps.found, 'KEY_OVER_1024_CHARS', 'The : indicator must be at most 1024 chars after the start of an implicit flow sequence key');
  28354. }
  28355. }
  28356. else if (value) {
  28357. if ('source' in value && value.source && value.source[0] === ':')
  28358. onError(value, 'MISSING_CHAR', `Missing space after : in ${fcName}`);
  28359. else
  28360. onError(valueProps.start, 'MISSING_CHAR', `Missing , or : between ${fcName} items`);
  28361. }
  28362. // value value
  28363. const valueNode = value
  28364. ? composeNode(ctx, value, valueProps, onError)
  28365. : valueProps.found
  28366. ? composeEmptyNode(ctx, valueProps.end, sep, null, valueProps, onError)
  28367. : null;
  28368. if (valueNode) {
  28369. if (isBlock$1(value))
  28370. onError(valueNode.range, 'BLOCK_IN_FLOW', blockMsg);
  28371. }
  28372. else if (valueProps.comment) {
  28373. if (keyNode.comment)
  28374. keyNode.comment += '\n' + valueProps.comment;
  28375. else
  28376. keyNode.comment = valueProps.comment;
  28377. }
  28378. const pair = new Pair(keyNode, valueNode);
  28379. if (ctx.options.keepSourceTokens)
  28380. pair.srcToken = collItem;
  28381. if (isMap) {
  28382. const map = coll;
  28383. if (mapIncludes(ctx, map.items, keyNode))
  28384. onError(keyStart, 'DUPLICATE_KEY', 'Map keys must be unique');
  28385. map.items.push(pair);
  28386. }
  28387. else {
  28388. const map = new YAMLMap(ctx.schema);
  28389. map.flow = true;
  28390. map.items.push(pair);
  28391. coll.items.push(map);
  28392. }
  28393. offset = valueNode ? valueNode.range[2] : valueProps.end;
  28394. }
  28395. }
  28396. const expectedEnd = isMap ? '}' : ']';
  28397. const [ce, ...ee] = fc.end;
  28398. let cePos = offset;
  28399. if (ce && ce.source === expectedEnd)
  28400. cePos = ce.offset + ce.source.length;
  28401. else {
  28402. const name = fcName[0].toUpperCase() + fcName.substring(1);
  28403. const msg = atRoot
  28404. ? `${name} must end with a ${expectedEnd}`
  28405. : `${name} in block collection must be sufficiently indented and end with a ${expectedEnd}`;
  28406. onError(offset, atRoot ? 'MISSING_CHAR' : 'BAD_INDENT', msg);
  28407. if (ce && ce.source.length !== 1)
  28408. ee.unshift(ce);
  28409. }
  28410. if (ee.length > 0) {
  28411. const end = resolveEnd(ee, cePos, ctx.options.strict, onError);
  28412. if (end.comment) {
  28413. if (coll.comment)
  28414. coll.comment += '\n' + end.comment;
  28415. else
  28416. coll.comment = end.comment;
  28417. }
  28418. coll.range = [fc.offset, cePos, end.offset];
  28419. }
  28420. else {
  28421. coll.range = [fc.offset, cePos, cePos];
  28422. }
  28423. return coll;
  28424. }
  28425. function resolveCollection(CN, ctx, token, onError, tagName, tag) {
  28426. const coll = token.type === 'block-map'
  28427. ? resolveBlockMap(CN, ctx, token, onError, tag)
  28428. : token.type === 'block-seq'
  28429. ? resolveBlockSeq(CN, ctx, token, onError, tag)
  28430. : resolveFlowCollection(CN, ctx, token, onError, tag);
  28431. const Coll = coll.constructor;
  28432. // If we got a tagName matching the class, or the tag name is '!',
  28433. // then use the tagName from the node class used to create it.
  28434. if (tagName === '!' || tagName === Coll.tagName) {
  28435. coll.tag = Coll.tagName;
  28436. return coll;
  28437. }
  28438. if (tagName)
  28439. coll.tag = tagName;
  28440. return coll;
  28441. }
  28442. function composeCollection(CN, ctx, token, props, onError) {
  28443. const tagToken = props.tag;
  28444. const tagName = !tagToken
  28445. ? null
  28446. : ctx.directives.tagName(tagToken.source, msg => onError(tagToken, 'TAG_RESOLVE_FAILED', msg));
  28447. if (token.type === 'block-seq') {
  28448. const { anchor, newlineAfterProp: nl } = props;
  28449. const lastProp = anchor && tagToken
  28450. ? anchor.offset > tagToken.offset
  28451. ? anchor
  28452. : tagToken
  28453. : (anchor ?? tagToken);
  28454. if (lastProp && (!nl || nl.offset < lastProp.offset)) {
  28455. const message = 'Missing newline after block sequence props';
  28456. onError(lastProp, 'MISSING_CHAR', message);
  28457. }
  28458. }
  28459. const expType = token.type === 'block-map'
  28460. ? 'map'
  28461. : token.type === 'block-seq'
  28462. ? 'seq'
  28463. : token.start.source === '{'
  28464. ? 'map'
  28465. : 'seq';
  28466. // shortcut: check if it's a generic YAMLMap or YAMLSeq
  28467. // before jumping into the custom tag logic.
  28468. if (!tagToken ||
  28469. !tagName ||
  28470. tagName === '!' ||
  28471. (tagName === YAMLMap.tagName && expType === 'map') ||
  28472. (tagName === YAMLSeq.tagName && expType === 'seq')) {
  28473. return resolveCollection(CN, ctx, token, onError, tagName);
  28474. }
  28475. let tag = ctx.schema.tags.find(t => t.tag === tagName && t.collection === expType);
  28476. if (!tag) {
  28477. const kt = ctx.schema.knownTags[tagName];
  28478. if (kt && kt.collection === expType) {
  28479. ctx.schema.tags.push(Object.assign({}, kt, { default: false }));
  28480. tag = kt;
  28481. }
  28482. else {
  28483. if (kt?.collection) {
  28484. onError(tagToken, 'BAD_COLLECTION_TYPE', `${kt.tag} used for ${expType} collection, but expects ${kt.collection}`, true);
  28485. }
  28486. else {
  28487. onError(tagToken, 'TAG_RESOLVE_FAILED', `Unresolved tag: ${tagName}`, true);
  28488. }
  28489. return resolveCollection(CN, ctx, token, onError, tagName);
  28490. }
  28491. }
  28492. const coll = resolveCollection(CN, ctx, token, onError, tagName, tag);
  28493. const res = tag.resolve?.(coll, msg => onError(tagToken, 'TAG_RESOLVE_FAILED', msg), ctx.options) ?? coll;
  28494. const node = isNode$1(res)
  28495. ? res
  28496. : new Scalar(res);
  28497. node.range = coll.range;
  28498. node.tag = tagName;
  28499. if (tag?.format)
  28500. node.format = tag.format;
  28501. return node;
  28502. }
  28503. function resolveBlockScalar(ctx, scalar, onError) {
  28504. const start = scalar.offset;
  28505. const header = parseBlockScalarHeader(scalar, ctx.options.strict, onError);
  28506. if (!header)
  28507. return { value: '', type: null, comment: '', range: [start, start, start] };
  28508. const type = header.mode === '>' ? Scalar.BLOCK_FOLDED : Scalar.BLOCK_LITERAL;
  28509. const lines = scalar.source ? splitLines(scalar.source) : [];
  28510. // determine the end of content & start of chomping
  28511. let chompStart = lines.length;
  28512. for (let i = lines.length - 1; i >= 0; --i) {
  28513. const content = lines[i][1];
  28514. if (content === '' || content === '\r')
  28515. chompStart = i;
  28516. else
  28517. break;
  28518. }
  28519. // shortcut for empty contents
  28520. if (chompStart === 0) {
  28521. const value = header.chomp === '+' && lines.length > 0
  28522. ? '\n'.repeat(Math.max(1, lines.length - 1))
  28523. : '';
  28524. let end = start + header.length;
  28525. if (scalar.source)
  28526. end += scalar.source.length;
  28527. return { value, type, comment: header.comment, range: [start, end, end] };
  28528. }
  28529. // find the indentation level to trim from start
  28530. let trimIndent = scalar.indent + header.indent;
  28531. let offset = scalar.offset + header.length;
  28532. let contentStart = 0;
  28533. for (let i = 0; i < chompStart; ++i) {
  28534. const [indent, content] = lines[i];
  28535. if (content === '' || content === '\r') {
  28536. if (header.indent === 0 && indent.length > trimIndent)
  28537. trimIndent = indent.length;
  28538. }
  28539. else {
  28540. if (indent.length < trimIndent) {
  28541. const message = 'Block scalars with more-indented leading empty lines must use an explicit indentation indicator';
  28542. onError(offset + indent.length, 'MISSING_CHAR', message);
  28543. }
  28544. if (header.indent === 0)
  28545. trimIndent = indent.length;
  28546. contentStart = i;
  28547. if (trimIndent === 0 && !ctx.atRoot) {
  28548. const message = 'Block scalar values in collections must be indented';
  28549. onError(offset, 'BAD_INDENT', message);
  28550. }
  28551. break;
  28552. }
  28553. offset += indent.length + content.length + 1;
  28554. }
  28555. // include trailing more-indented empty lines in content
  28556. for (let i = lines.length - 1; i >= chompStart; --i) {
  28557. if (lines[i][0].length > trimIndent)
  28558. chompStart = i + 1;
  28559. }
  28560. let value = '';
  28561. let sep = '';
  28562. let prevMoreIndented = false;
  28563. // leading whitespace is kept intact
  28564. for (let i = 0; i < contentStart; ++i)
  28565. value += lines[i][0].slice(trimIndent) + '\n';
  28566. for (let i = contentStart; i < chompStart; ++i) {
  28567. let [indent, content] = lines[i];
  28568. offset += indent.length + content.length + 1;
  28569. const crlf = content[content.length - 1] === '\r';
  28570. if (crlf)
  28571. content = content.slice(0, -1);
  28572. /* istanbul ignore if already caught in lexer */
  28573. if (content && indent.length < trimIndent) {
  28574. const src = header.indent
  28575. ? 'explicit indentation indicator'
  28576. : 'first line';
  28577. const message = `Block scalar lines must not be less indented than their ${src}`;
  28578. onError(offset - content.length - (crlf ? 2 : 1), 'BAD_INDENT', message);
  28579. indent = '';
  28580. }
  28581. if (type === Scalar.BLOCK_LITERAL) {
  28582. value += sep + indent.slice(trimIndent) + content;
  28583. sep = '\n';
  28584. }
  28585. else if (indent.length > trimIndent || content[0] === '\t') {
  28586. // more-indented content within a folded block
  28587. if (sep === ' ')
  28588. sep = '\n';
  28589. else if (!prevMoreIndented && sep === '\n')
  28590. sep = '\n\n';
  28591. value += sep + indent.slice(trimIndent) + content;
  28592. sep = '\n';
  28593. prevMoreIndented = true;
  28594. }
  28595. else if (content === '') {
  28596. // empty line
  28597. if (sep === '\n')
  28598. value += '\n';
  28599. else
  28600. sep = '\n';
  28601. }
  28602. else {
  28603. value += sep + content;
  28604. sep = ' ';
  28605. prevMoreIndented = false;
  28606. }
  28607. }
  28608. switch (header.chomp) {
  28609. case '-':
  28610. break;
  28611. case '+':
  28612. for (let i = chompStart; i < lines.length; ++i)
  28613. value += '\n' + lines[i][0].slice(trimIndent);
  28614. if (value[value.length - 1] !== '\n')
  28615. value += '\n';
  28616. break;
  28617. default:
  28618. value += '\n';
  28619. }
  28620. const end = start + header.length + scalar.source.length;
  28621. return { value, type, comment: header.comment, range: [start, end, end] };
  28622. }
  28623. function parseBlockScalarHeader({ offset, props }, strict, onError) {
  28624. /* istanbul ignore if should not happen */
  28625. if (props[0].type !== 'block-scalar-header') {
  28626. onError(props[0], 'IMPOSSIBLE', 'Block scalar header not found');
  28627. return null;
  28628. }
  28629. const { source } = props[0];
  28630. const mode = source[0];
  28631. let indent = 0;
  28632. let chomp = '';
  28633. let error = -1;
  28634. for (let i = 1; i < source.length; ++i) {
  28635. const ch = source[i];
  28636. if (!chomp && (ch === '-' || ch === '+'))
  28637. chomp = ch;
  28638. else {
  28639. const n = Number(ch);
  28640. if (!indent && n)
  28641. indent = n;
  28642. else if (error === -1)
  28643. error = offset + i;
  28644. }
  28645. }
  28646. if (error !== -1)
  28647. onError(error, 'UNEXPECTED_TOKEN', `Block scalar header includes extra characters: ${source}`);
  28648. let hasSpace = false;
  28649. let comment = '';
  28650. let length = source.length;
  28651. for (let i = 1; i < props.length; ++i) {
  28652. const token = props[i];
  28653. switch (token.type) {
  28654. case 'space':
  28655. hasSpace = true;
  28656. // fallthrough
  28657. case 'newline':
  28658. length += token.source.length;
  28659. break;
  28660. case 'comment':
  28661. if (strict && !hasSpace) {
  28662. const message = 'Comments must be separated from other tokens by white space characters';
  28663. onError(token, 'MISSING_CHAR', message);
  28664. }
  28665. length += token.source.length;
  28666. comment = token.source.substring(1);
  28667. break;
  28668. case 'error':
  28669. onError(token, 'UNEXPECTED_TOKEN', token.message);
  28670. length += token.source.length;
  28671. break;
  28672. /* istanbul ignore next should not happen */
  28673. default: {
  28674. const message = `Unexpected token in block scalar header: ${token.type}`;
  28675. onError(token, 'UNEXPECTED_TOKEN', message);
  28676. const ts = token.source;
  28677. if (ts && typeof ts === 'string')
  28678. length += ts.length;
  28679. }
  28680. }
  28681. }
  28682. return { mode, indent, chomp, comment, length };
  28683. }
  28684. /** @returns Array of lines split up as `[indent, content]` */
  28685. function splitLines(source) {
  28686. const split = source.split(/\n( *)/);
  28687. const first = split[0];
  28688. const m = first.match(/^( *)/);
  28689. const line0 = m?.[1]
  28690. ? [m[1], first.slice(m[1].length)]
  28691. : ['', first];
  28692. const lines = [line0];
  28693. for (let i = 1; i < split.length; i += 2)
  28694. lines.push([split[i], split[i + 1]]);
  28695. return lines;
  28696. }
  28697. function resolveFlowScalar(scalar, strict, onError) {
  28698. const { offset, type, source, end } = scalar;
  28699. let _type;
  28700. let value;
  28701. const _onError = (rel, code, msg) => onError(offset + rel, code, msg);
  28702. switch (type) {
  28703. case 'scalar':
  28704. _type = Scalar.PLAIN;
  28705. value = plainValue(source, _onError);
  28706. break;
  28707. case 'single-quoted-scalar':
  28708. _type = Scalar.QUOTE_SINGLE;
  28709. value = singleQuotedValue(source, _onError);
  28710. break;
  28711. case 'double-quoted-scalar':
  28712. _type = Scalar.QUOTE_DOUBLE;
  28713. value = doubleQuotedValue(source, _onError);
  28714. break;
  28715. /* istanbul ignore next should not happen */
  28716. default:
  28717. onError(scalar, 'UNEXPECTED_TOKEN', `Expected a flow scalar value, but found: ${type}`);
  28718. return {
  28719. value: '',
  28720. type: null,
  28721. comment: '',
  28722. range: [offset, offset + source.length, offset + source.length]
  28723. };
  28724. }
  28725. const valueEnd = offset + source.length;
  28726. const re = resolveEnd(end, valueEnd, strict, onError);
  28727. return {
  28728. value,
  28729. type: _type,
  28730. comment: re.comment,
  28731. range: [offset, valueEnd, re.offset]
  28732. };
  28733. }
  28734. function plainValue(source, onError) {
  28735. let badChar = '';
  28736. switch (source[0]) {
  28737. /* istanbul ignore next should not happen */
  28738. case '\t':
  28739. badChar = 'a tab character';
  28740. break;
  28741. case ',':
  28742. badChar = 'flow indicator character ,';
  28743. break;
  28744. case '%':
  28745. badChar = 'directive indicator character %';
  28746. break;
  28747. case '|':
  28748. case '>': {
  28749. badChar = `block scalar indicator ${source[0]}`;
  28750. break;
  28751. }
  28752. case '@':
  28753. case '`': {
  28754. badChar = `reserved character ${source[0]}`;
  28755. break;
  28756. }
  28757. }
  28758. if (badChar)
  28759. onError(0, 'BAD_SCALAR_START', `Plain value cannot start with ${badChar}`);
  28760. return foldLines(source);
  28761. }
  28762. function singleQuotedValue(source, onError) {
  28763. if (source[source.length - 1] !== "'" || source.length === 1)
  28764. onError(source.length, 'MISSING_CHAR', "Missing closing 'quote");
  28765. return foldLines(source.slice(1, -1)).replace(/''/g, "'");
  28766. }
  28767. function foldLines(source) {
  28768. /**
  28769. * The negative lookbehind here and in the `re` RegExp is to
  28770. * prevent causing a polynomial search time in certain cases.
  28771. *
  28772. * The try-catch is for Safari, which doesn't support this yet:
  28773. * https://caniuse.com/js-regexp-lookbehind
  28774. */
  28775. let first, line;
  28776. try {
  28777. first = new RegExp('(.*?)(?<![ \t])[ \t]*\r?\n', 'sy');
  28778. line = new RegExp('[ \t]*(.*?)(?:(?<![ \t])[ \t]*)?\r?\n', 'sy');
  28779. }
  28780. catch (_) {
  28781. first = /(.*?)[ \t]*\r?\n/sy;
  28782. line = /[ \t]*(.*?)[ \t]*\r?\n/sy;
  28783. }
  28784. let match = first.exec(source);
  28785. if (!match)
  28786. return source;
  28787. let res = match[1];
  28788. let sep = ' ';
  28789. let pos = first.lastIndex;
  28790. line.lastIndex = pos;
  28791. while ((match = line.exec(source))) {
  28792. if (match[1] === '') {
  28793. if (sep === '\n')
  28794. res += sep;
  28795. else
  28796. sep = '\n';
  28797. }
  28798. else {
  28799. res += sep + match[1];
  28800. sep = ' ';
  28801. }
  28802. pos = line.lastIndex;
  28803. }
  28804. const last = /[ \t]*(.*)/sy;
  28805. last.lastIndex = pos;
  28806. match = last.exec(source);
  28807. return res + sep + (match?.[1] ?? '');
  28808. }
  28809. function doubleQuotedValue(source, onError) {
  28810. let res = '';
  28811. for (let i = 1; i < source.length - 1; ++i) {
  28812. const ch = source[i];
  28813. if (ch === '\r' && source[i + 1] === '\n')
  28814. continue;
  28815. if (ch === '\n') {
  28816. const { fold, offset } = foldNewline(source, i);
  28817. res += fold;
  28818. i = offset;
  28819. }
  28820. else if (ch === '\\') {
  28821. let next = source[++i];
  28822. const cc = escapeCodes[next];
  28823. if (cc)
  28824. res += cc;
  28825. else if (next === '\n') {
  28826. // skip escaped newlines, but still trim the following line
  28827. next = source[i + 1];
  28828. while (next === ' ' || next === '\t')
  28829. next = source[++i + 1];
  28830. }
  28831. else if (next === '\r' && source[i + 1] === '\n') {
  28832. // skip escaped CRLF newlines, but still trim the following line
  28833. next = source[++i + 1];
  28834. while (next === ' ' || next === '\t')
  28835. next = source[++i + 1];
  28836. }
  28837. else if (next === 'x' || next === 'u' || next === 'U') {
  28838. const length = { x: 2, u: 4, U: 8 }[next];
  28839. res += parseCharCode(source, i + 1, length, onError);
  28840. i += length;
  28841. }
  28842. else {
  28843. const raw = source.substr(i - 1, 2);
  28844. onError(i - 1, 'BAD_DQ_ESCAPE', `Invalid escape sequence ${raw}`);
  28845. res += raw;
  28846. }
  28847. }
  28848. else if (ch === ' ' || ch === '\t') {
  28849. // trim trailing whitespace
  28850. const wsStart = i;
  28851. let next = source[i + 1];
  28852. while (next === ' ' || next === '\t')
  28853. next = source[++i + 1];
  28854. if (next !== '\n' && !(next === '\r' && source[i + 2] === '\n'))
  28855. res += i > wsStart ? source.slice(wsStart, i + 1) : ch;
  28856. }
  28857. else {
  28858. res += ch;
  28859. }
  28860. }
  28861. if (source[source.length - 1] !== '"' || source.length === 1)
  28862. onError(source.length, 'MISSING_CHAR', 'Missing closing "quote');
  28863. return res;
  28864. }
  28865. /**
  28866. * Fold a single newline into a space, multiple newlines to N - 1 newlines.
  28867. * Presumes `source[offset] === '\n'`
  28868. */
  28869. function foldNewline(source, offset) {
  28870. let fold = '';
  28871. let ch = source[offset + 1];
  28872. while (ch === ' ' || ch === '\t' || ch === '\n' || ch === '\r') {
  28873. if (ch === '\r' && source[offset + 2] !== '\n')
  28874. break;
  28875. if (ch === '\n')
  28876. fold += '\n';
  28877. offset += 1;
  28878. ch = source[offset + 1];
  28879. }
  28880. if (!fold)
  28881. fold = ' ';
  28882. return { fold, offset };
  28883. }
  28884. const escapeCodes = {
  28885. '0': '\0', // null character
  28886. a: '\x07', // bell character
  28887. b: '\b', // backspace
  28888. e: '\x1b', // escape character
  28889. f: '\f', // form feed
  28890. n: '\n', // line feed
  28891. r: '\r', // carriage return
  28892. t: '\t', // horizontal tab
  28893. v: '\v', // vertical tab
  28894. N: '\u0085', // Unicode next line
  28895. _: '\u00a0', // Unicode non-breaking space
  28896. L: '\u2028', // Unicode line separator
  28897. P: '\u2029', // Unicode paragraph separator
  28898. ' ': ' ',
  28899. '"': '"',
  28900. '/': '/',
  28901. '\\': '\\',
  28902. '\t': '\t'
  28903. };
  28904. function parseCharCode(source, offset, length, onError) {
  28905. const cc = source.substr(offset, length);
  28906. const ok = cc.length === length && /^[0-9a-fA-F]+$/.test(cc);
  28907. const code = ok ? parseInt(cc, 16) : NaN;
  28908. if (isNaN(code)) {
  28909. const raw = source.substr(offset - 2, length + 2);
  28910. onError(offset - 2, 'BAD_DQ_ESCAPE', `Invalid escape sequence ${raw}`);
  28911. return raw;
  28912. }
  28913. return String.fromCodePoint(code);
  28914. }
  28915. function composeScalar(ctx, token, tagToken, onError) {
  28916. const { value, type, comment, range } = token.type === 'block-scalar'
  28917. ? resolveBlockScalar(ctx, token, onError)
  28918. : resolveFlowScalar(token, ctx.options.strict, onError);
  28919. const tagName = tagToken
  28920. ? ctx.directives.tagName(tagToken.source, msg => onError(tagToken, 'TAG_RESOLVE_FAILED', msg))
  28921. : null;
  28922. const tag = tagToken && tagName
  28923. ? findScalarTagByName(ctx.schema, value, tagName, tagToken, onError)
  28924. : token.type === 'scalar'
  28925. ? findScalarTagByTest(ctx, value, token, onError)
  28926. : ctx.schema[SCALAR$1];
  28927. let scalar;
  28928. try {
  28929. const res = tag.resolve(value, msg => onError(tagToken ?? token, 'TAG_RESOLVE_FAILED', msg), ctx.options);
  28930. scalar = isScalar$1(res) ? res : new Scalar(res);
  28931. }
  28932. catch (error) {
  28933. const msg = error instanceof Error ? error.message : String(error);
  28934. onError(tagToken ?? token, 'TAG_RESOLVE_FAILED', msg);
  28935. scalar = new Scalar(value);
  28936. }
  28937. scalar.range = range;
  28938. scalar.source = value;
  28939. if (type)
  28940. scalar.type = type;
  28941. if (tagName)
  28942. scalar.tag = tagName;
  28943. if (tag.format)
  28944. scalar.format = tag.format;
  28945. if (comment)
  28946. scalar.comment = comment;
  28947. return scalar;
  28948. }
  28949. function findScalarTagByName(schema, value, tagName, tagToken, onError) {
  28950. if (tagName === '!')
  28951. return schema[SCALAR$1]; // non-specific tag
  28952. const matchWithTest = [];
  28953. for (const tag of schema.tags) {
  28954. if (!tag.collection && tag.tag === tagName) {
  28955. if (tag.default && tag.test)
  28956. matchWithTest.push(tag);
  28957. else
  28958. return tag;
  28959. }
  28960. }
  28961. for (const tag of matchWithTest)
  28962. if (tag.test?.test(value))
  28963. return tag;
  28964. const kt = schema.knownTags[tagName];
  28965. if (kt && !kt.collection) {
  28966. // Ensure that the known tag is available for stringifying,
  28967. // but does not get used by default.
  28968. schema.tags.push(Object.assign({}, kt, { default: false, test: undefined }));
  28969. return kt;
  28970. }
  28971. onError(tagToken, 'TAG_RESOLVE_FAILED', `Unresolved tag: ${tagName}`, tagName !== 'tag:yaml.org,2002:str');
  28972. return schema[SCALAR$1];
  28973. }
  28974. function findScalarTagByTest({ directives, schema }, value, token, onError) {
  28975. const tag = schema.tags.find(tag => tag.default && tag.test?.test(value)) || schema[SCALAR$1];
  28976. if (schema.compat) {
  28977. const compat = schema.compat.find(tag => tag.default && tag.test?.test(value)) ??
  28978. schema[SCALAR$1];
  28979. if (tag.tag !== compat.tag) {
  28980. const ts = directives.tagString(tag.tag);
  28981. const cs = directives.tagString(compat.tag);
  28982. const msg = `Value may be parsed as either ${ts} or ${cs}`;
  28983. onError(token, 'TAG_RESOLVE_FAILED', msg, true);
  28984. }
  28985. }
  28986. return tag;
  28987. }
  28988. function emptyScalarPosition(offset, before, pos) {
  28989. if (before) {
  28990. if (pos === null)
  28991. pos = before.length;
  28992. for (let i = pos - 1; i >= 0; --i) {
  28993. let st = before[i];
  28994. switch (st.type) {
  28995. case 'space':
  28996. case 'comment':
  28997. case 'newline':
  28998. offset -= st.source.length;
  28999. continue;
  29000. }
  29001. // Technically, an empty scalar is immediately after the last non-empty
  29002. // node, but it's more useful to place it after any whitespace.
  29003. st = before[++i];
  29004. while (st?.type === 'space') {
  29005. offset += st.source.length;
  29006. st = before[++i];
  29007. }
  29008. break;
  29009. }
  29010. }
  29011. return offset;
  29012. }
  29013. const CN = { composeNode, composeEmptyNode };
  29014. function composeNode(ctx, token, props, onError) {
  29015. const { spaceBefore, comment, anchor, tag } = props;
  29016. let node;
  29017. let isSrcToken = true;
  29018. switch (token.type) {
  29019. case 'alias':
  29020. node = composeAlias(ctx, token, onError);
  29021. if (anchor || tag)
  29022. onError(token, 'ALIAS_PROPS', 'An alias node must not specify any properties');
  29023. break;
  29024. case 'scalar':
  29025. case 'single-quoted-scalar':
  29026. case 'double-quoted-scalar':
  29027. case 'block-scalar':
  29028. node = composeScalar(ctx, token, tag, onError);
  29029. if (anchor)
  29030. node.anchor = anchor.source.substring(1);
  29031. break;
  29032. case 'block-map':
  29033. case 'block-seq':
  29034. case 'flow-collection':
  29035. node = composeCollection(CN, ctx, token, props, onError);
  29036. if (anchor)
  29037. node.anchor = anchor.source.substring(1);
  29038. break;
  29039. default: {
  29040. const message = token.type === 'error'
  29041. ? token.message
  29042. : `Unsupported token (type: ${token.type})`;
  29043. onError(token, 'UNEXPECTED_TOKEN', message);
  29044. node = composeEmptyNode(ctx, token.offset, undefined, null, props, onError);
  29045. isSrcToken = false;
  29046. }
  29047. }
  29048. if (anchor && node.anchor === '')
  29049. onError(anchor, 'BAD_ALIAS', 'Anchor cannot be an empty string');
  29050. if (spaceBefore)
  29051. node.spaceBefore = true;
  29052. if (comment) {
  29053. if (token.type === 'scalar' && token.source === '')
  29054. node.comment = comment;
  29055. else
  29056. node.commentBefore = comment;
  29057. }
  29058. // @ts-expect-error Type checking misses meaning of isSrcToken
  29059. if (ctx.options.keepSourceTokens && isSrcToken)
  29060. node.srcToken = token;
  29061. return node;
  29062. }
  29063. function composeEmptyNode(ctx, offset, before, pos, { spaceBefore, comment, anchor, tag, end }, onError) {
  29064. const token = {
  29065. type: 'scalar',
  29066. offset: emptyScalarPosition(offset, before, pos),
  29067. indent: -1,
  29068. source: ''
  29069. };
  29070. const node = composeScalar(ctx, token, tag, onError);
  29071. if (anchor) {
  29072. node.anchor = anchor.source.substring(1);
  29073. if (node.anchor === '')
  29074. onError(anchor, 'BAD_ALIAS', 'Anchor cannot be an empty string');
  29075. }
  29076. if (spaceBefore)
  29077. node.spaceBefore = true;
  29078. if (comment) {
  29079. node.comment = comment;
  29080. node.range[2] = end;
  29081. }
  29082. return node;
  29083. }
  29084. function composeAlias({ options }, { offset, source, end }, onError) {
  29085. const alias = new Alias(source.substring(1));
  29086. if (alias.source === '')
  29087. onError(offset, 'BAD_ALIAS', 'Alias cannot be an empty string');
  29088. if (alias.source.endsWith(':'))
  29089. onError(offset + source.length - 1, 'BAD_ALIAS', 'Alias ending in : is ambiguous', true);
  29090. const valueEnd = offset + source.length;
  29091. const re = resolveEnd(end, valueEnd, options.strict, onError);
  29092. alias.range = [offset, valueEnd, re.offset];
  29093. if (re.comment)
  29094. alias.comment = re.comment;
  29095. return alias;
  29096. }
  29097. function composeDoc(options, directives, { offset, start, value, end }, onError) {
  29098. const opts = Object.assign({ _directives: directives }, options);
  29099. const doc = new Document(undefined, opts);
  29100. const ctx = {
  29101. atRoot: true,
  29102. directives: doc.directives,
  29103. options: doc.options,
  29104. schema: doc.schema
  29105. };
  29106. const props = resolveProps(start, {
  29107. indicator: 'doc-start',
  29108. next: value ?? end?.[0],
  29109. offset,
  29110. onError,
  29111. parentIndent: 0,
  29112. startOnNewline: true
  29113. });
  29114. if (props.found) {
  29115. doc.directives.docStart = true;
  29116. if (value &&
  29117. (value.type === 'block-map' || value.type === 'block-seq') &&
  29118. !props.hasNewline)
  29119. onError(props.end, 'MISSING_CHAR', 'Block collection cannot start on same line with directives-end marker');
  29120. }
  29121. // @ts-expect-error If Contents is set, let's trust the user
  29122. doc.contents = value
  29123. ? composeNode(ctx, value, props, onError)
  29124. : composeEmptyNode(ctx, props.end, start, null, props, onError);
  29125. const contentEnd = doc.contents.range[2];
  29126. const re = resolveEnd(end, contentEnd, false, onError);
  29127. if (re.comment)
  29128. doc.comment = re.comment;
  29129. doc.range = [offset, contentEnd, re.offset];
  29130. return doc;
  29131. }
  29132. function getErrorPos(src) {
  29133. if (typeof src === 'number')
  29134. return [src, src + 1];
  29135. if (Array.isArray(src))
  29136. return src.length === 2 ? src : [src[0], src[1]];
  29137. const { offset, source } = src;
  29138. return [offset, offset + (typeof source === 'string' ? source.length : 1)];
  29139. }
  29140. function parsePrelude(prelude) {
  29141. let comment = '';
  29142. let atComment = false;
  29143. let afterEmptyLine = false;
  29144. for (let i = 0; i < prelude.length; ++i) {
  29145. const source = prelude[i];
  29146. switch (source[0]) {
  29147. case '#':
  29148. comment +=
  29149. (comment === '' ? '' : afterEmptyLine ? '\n\n' : '\n') +
  29150. (source.substring(1) || ' ');
  29151. atComment = true;
  29152. afterEmptyLine = false;
  29153. break;
  29154. case '%':
  29155. if (prelude[i + 1]?.[0] !== '#')
  29156. i += 1;
  29157. atComment = false;
  29158. break;
  29159. default:
  29160. // This may be wrong after doc-end, but in that case it doesn't matter
  29161. if (!atComment)
  29162. afterEmptyLine = true;
  29163. atComment = false;
  29164. }
  29165. }
  29166. return { comment, afterEmptyLine };
  29167. }
  29168. /**
  29169. * Compose a stream of CST nodes into a stream of YAML Documents.
  29170. *
  29171. * ```ts
  29172. * import { Composer, Parser } from 'yaml'
  29173. *
  29174. * const src: string = ...
  29175. * const tokens = new Parser().parse(src)
  29176. * const docs = new Composer().compose(tokens)
  29177. * ```
  29178. */
  29179. class Composer {
  29180. constructor(options = {}) {
  29181. this.doc = null;
  29182. this.atDirectives = false;
  29183. this.prelude = [];
  29184. this.errors = [];
  29185. this.warnings = [];
  29186. this.onError = (source, code, message, warning) => {
  29187. const pos = getErrorPos(source);
  29188. if (warning)
  29189. this.warnings.push(new YAMLWarning(pos, code, message));
  29190. else
  29191. this.errors.push(new YAMLParseError(pos, code, message));
  29192. };
  29193. // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
  29194. this.directives = new Directives({ version: options.version || '1.2' });
  29195. this.options = options;
  29196. }
  29197. decorate(doc, afterDoc) {
  29198. const { comment, afterEmptyLine } = parsePrelude(this.prelude);
  29199. //console.log({ dc: doc.comment, prelude, comment })
  29200. if (comment) {
  29201. const dc = doc.contents;
  29202. if (afterDoc) {
  29203. doc.comment = doc.comment ? `${doc.comment}\n${comment}` : comment;
  29204. }
  29205. else if (afterEmptyLine || doc.directives.docStart || !dc) {
  29206. doc.commentBefore = comment;
  29207. }
  29208. else if (isCollection$1(dc) && !dc.flow && dc.items.length > 0) {
  29209. let it = dc.items[0];
  29210. if (isPair(it))
  29211. it = it.key;
  29212. const cb = it.commentBefore;
  29213. it.commentBefore = cb ? `${comment}\n${cb}` : comment;
  29214. }
  29215. else {
  29216. const cb = dc.commentBefore;
  29217. dc.commentBefore = cb ? `${comment}\n${cb}` : comment;
  29218. }
  29219. }
  29220. if (afterDoc) {
  29221. Array.prototype.push.apply(doc.errors, this.errors);
  29222. Array.prototype.push.apply(doc.warnings, this.warnings);
  29223. }
  29224. else {
  29225. doc.errors = this.errors;
  29226. doc.warnings = this.warnings;
  29227. }
  29228. this.prelude = [];
  29229. this.errors = [];
  29230. this.warnings = [];
  29231. }
  29232. /**
  29233. * Current stream status information.
  29234. *
  29235. * Mostly useful at the end of input for an empty stream.
  29236. */
  29237. streamInfo() {
  29238. return {
  29239. comment: parsePrelude(this.prelude).comment,
  29240. directives: this.directives,
  29241. errors: this.errors,
  29242. warnings: this.warnings
  29243. };
  29244. }
  29245. /**
  29246. * Compose tokens into documents.
  29247. *
  29248. * @param forceDoc - If the stream contains no document, still emit a final document including any comments and directives that would be applied to a subsequent document.
  29249. * @param endOffset - Should be set if `forceDoc` is also set, to set the document range end and to indicate errors correctly.
  29250. */
  29251. *compose(tokens, forceDoc = false, endOffset = -1) {
  29252. for (const token of tokens)
  29253. yield* this.next(token);
  29254. yield* this.end(forceDoc, endOffset);
  29255. }
  29256. /** Advance the composer by one CST token. */
  29257. *next(token) {
  29258. switch (token.type) {
  29259. case 'directive':
  29260. this.directives.add(token.source, (offset, message, warning) => {
  29261. const pos = getErrorPos(token);
  29262. pos[0] += offset;
  29263. this.onError(pos, 'BAD_DIRECTIVE', message, warning);
  29264. });
  29265. this.prelude.push(token.source);
  29266. this.atDirectives = true;
  29267. break;
  29268. case 'document': {
  29269. const doc = composeDoc(this.options, this.directives, token, this.onError);
  29270. if (this.atDirectives && !doc.directives.docStart)
  29271. this.onError(token, 'MISSING_CHAR', 'Missing directives-end/doc-start indicator line');
  29272. this.decorate(doc, false);
  29273. if (this.doc)
  29274. yield this.doc;
  29275. this.doc = doc;
  29276. this.atDirectives = false;
  29277. break;
  29278. }
  29279. case 'byte-order-mark':
  29280. case 'space':
  29281. break;
  29282. case 'comment':
  29283. case 'newline':
  29284. this.prelude.push(token.source);
  29285. break;
  29286. case 'error': {
  29287. const msg = token.source
  29288. ? `${token.message}: ${JSON.stringify(token.source)}`
  29289. : token.message;
  29290. const error = new YAMLParseError(getErrorPos(token), 'UNEXPECTED_TOKEN', msg);
  29291. if (this.atDirectives || !this.doc)
  29292. this.errors.push(error);
  29293. else
  29294. this.doc.errors.push(error);
  29295. break;
  29296. }
  29297. case 'doc-end': {
  29298. if (!this.doc) {
  29299. const msg = 'Unexpected doc-end without preceding document';
  29300. this.errors.push(new YAMLParseError(getErrorPos(token), 'UNEXPECTED_TOKEN', msg));
  29301. break;
  29302. }
  29303. this.doc.directives.docEnd = true;
  29304. const end = resolveEnd(token.end, token.offset + token.source.length, this.doc.options.strict, this.onError);
  29305. this.decorate(this.doc, true);
  29306. if (end.comment) {
  29307. const dc = this.doc.comment;
  29308. this.doc.comment = dc ? `${dc}\n${end.comment}` : end.comment;
  29309. }
  29310. this.doc.range[2] = end.offset;
  29311. break;
  29312. }
  29313. default:
  29314. this.errors.push(new YAMLParseError(getErrorPos(token), 'UNEXPECTED_TOKEN', `Unsupported token ${token.type}`));
  29315. }
  29316. }
  29317. /**
  29318. * Call at end of input to yield any remaining document.
  29319. *
  29320. * @param forceDoc - If the stream contains no document, still emit a final document including any comments and directives that would be applied to a subsequent document.
  29321. * @param endOffset - Should be set if `forceDoc` is also set, to set the document range end and to indicate errors correctly.
  29322. */
  29323. *end(forceDoc = false, endOffset = -1) {
  29324. if (this.doc) {
  29325. this.decorate(this.doc, true);
  29326. yield this.doc;
  29327. this.doc = null;
  29328. }
  29329. else if (forceDoc) {
  29330. const opts = Object.assign({ _directives: this.directives }, this.options);
  29331. const doc = new Document(undefined, opts);
  29332. if (this.atDirectives)
  29333. this.onError(endOffset, 'MISSING_CHAR', 'Missing directives-end indicator line');
  29334. doc.range = [0, endOffset, endOffset];
  29335. this.decorate(doc, false);
  29336. yield doc;
  29337. }
  29338. }
  29339. }
  29340. function resolveAsScalar(token, strict = true, onError) {
  29341. if (token) {
  29342. const _onError = (pos, code, message) => {
  29343. const offset = typeof pos === 'number' ? pos : Array.isArray(pos) ? pos[0] : pos.offset;
  29344. if (onError)
  29345. onError(offset, code, message);
  29346. else
  29347. throw new YAMLParseError([offset, offset + 1], code, message);
  29348. };
  29349. switch (token.type) {
  29350. case 'scalar':
  29351. case 'single-quoted-scalar':
  29352. case 'double-quoted-scalar':
  29353. return resolveFlowScalar(token, strict, _onError);
  29354. case 'block-scalar':
  29355. return resolveBlockScalar({ options: { strict } }, token, _onError);
  29356. }
  29357. }
  29358. return null;
  29359. }
  29360. /**
  29361. * Create a new scalar token with `value`
  29362. *
  29363. * Values that represent an actual string but may be parsed as a different type should use a `type` other than `'PLAIN'`,
  29364. * as this function does not support any schema operations and won't check for such conflicts.
  29365. *
  29366. * @param value The string representation of the value, which will have its content properly indented.
  29367. * @param context.end Comments and whitespace after the end of the value, or after the block scalar header. If undefined, a newline will be added.
  29368. * @param context.implicitKey Being within an implicit key may affect the resolved type of the token's value.
  29369. * @param context.indent The indent level of the token.
  29370. * @param context.inFlow Is this scalar within a flow collection? This may affect the resolved type of the token's value.
  29371. * @param context.offset The offset position of the token.
  29372. * @param context.type The preferred type of the scalar token. If undefined, the previous type of the `token` will be used, defaulting to `'PLAIN'`.
  29373. */
  29374. function createScalarToken(value, context) {
  29375. const { implicitKey = false, indent, inFlow = false, offset = -1, type = 'PLAIN' } = context;
  29376. const source = stringifyString({ type, value }, {
  29377. implicitKey,
  29378. indent: indent > 0 ? ' '.repeat(indent) : '',
  29379. inFlow,
  29380. options: { blockQuote: true, lineWidth: -1 }
  29381. });
  29382. const end = context.end ?? [
  29383. { type: 'newline', offset: -1, indent, source: '\n' }
  29384. ];
  29385. switch (source[0]) {
  29386. case '|':
  29387. case '>': {
  29388. const he = source.indexOf('\n');
  29389. const head = source.substring(0, he);
  29390. const body = source.substring(he + 1) + '\n';
  29391. const props = [
  29392. { type: 'block-scalar-header', offset, indent, source: head }
  29393. ];
  29394. if (!addEndtoBlockProps(props, end))
  29395. props.push({ type: 'newline', offset: -1, indent, source: '\n' });
  29396. return { type: 'block-scalar', offset, indent, props, source: body };
  29397. }
  29398. case '"':
  29399. return { type: 'double-quoted-scalar', offset, indent, source, end };
  29400. case "'":
  29401. return { type: 'single-quoted-scalar', offset, indent, source, end };
  29402. default:
  29403. return { type: 'scalar', offset, indent, source, end };
  29404. }
  29405. }
  29406. /**
  29407. * Set the value of `token` to the given string `value`, overwriting any previous contents and type that it may have.
  29408. *
  29409. * Best efforts are made to retain any comments previously associated with the `token`,
  29410. * though all contents within a collection's `items` will be overwritten.
  29411. *
  29412. * Values that represent an actual string but may be parsed as a different type should use a `type` other than `'PLAIN'`,
  29413. * as this function does not support any schema operations and won't check for such conflicts.
  29414. *
  29415. * @param token Any token. If it does not include an `indent` value, the value will be stringified as if it were an implicit key.
  29416. * @param value The string representation of the value, which will have its content properly indented.
  29417. * @param context.afterKey In most cases, values after a key should have an additional level of indentation.
  29418. * @param context.implicitKey Being within an implicit key may affect the resolved type of the token's value.
  29419. * @param context.inFlow Being within a flow collection may affect the resolved type of the token's value.
  29420. * @param context.type The preferred type of the scalar token. If undefined, the previous type of the `token` will be used, defaulting to `'PLAIN'`.
  29421. */
  29422. function setScalarValue(token, value, context = {}) {
  29423. let { afterKey = false, implicitKey = false, inFlow = false, type } = context;
  29424. let indent = 'indent' in token ? token.indent : null;
  29425. if (afterKey && typeof indent === 'number')
  29426. indent += 2;
  29427. if (!type)
  29428. switch (token.type) {
  29429. case 'single-quoted-scalar':
  29430. type = 'QUOTE_SINGLE';
  29431. break;
  29432. case 'double-quoted-scalar':
  29433. type = 'QUOTE_DOUBLE';
  29434. break;
  29435. case 'block-scalar': {
  29436. const header = token.props[0];
  29437. if (header.type !== 'block-scalar-header')
  29438. throw new Error('Invalid block scalar header');
  29439. type = header.source[0] === '>' ? 'BLOCK_FOLDED' : 'BLOCK_LITERAL';
  29440. break;
  29441. }
  29442. default:
  29443. type = 'PLAIN';
  29444. }
  29445. const source = stringifyString({ type, value }, {
  29446. implicitKey: implicitKey || indent === null,
  29447. indent: indent !== null && indent > 0 ? ' '.repeat(indent) : '',
  29448. inFlow,
  29449. options: { blockQuote: true, lineWidth: -1 }
  29450. });
  29451. switch (source[0]) {
  29452. case '|':
  29453. case '>':
  29454. setBlockScalarValue(token, source);
  29455. break;
  29456. case '"':
  29457. setFlowScalarValue(token, source, 'double-quoted-scalar');
  29458. break;
  29459. case "'":
  29460. setFlowScalarValue(token, source, 'single-quoted-scalar');
  29461. break;
  29462. default:
  29463. setFlowScalarValue(token, source, 'scalar');
  29464. }
  29465. }
  29466. function setBlockScalarValue(token, source) {
  29467. const he = source.indexOf('\n');
  29468. const head = source.substring(0, he);
  29469. const body = source.substring(he + 1) + '\n';
  29470. if (token.type === 'block-scalar') {
  29471. const header = token.props[0];
  29472. if (header.type !== 'block-scalar-header')
  29473. throw new Error('Invalid block scalar header');
  29474. header.source = head;
  29475. token.source = body;
  29476. }
  29477. else {
  29478. const { offset } = token;
  29479. const indent = 'indent' in token ? token.indent : -1;
  29480. const props = [
  29481. { type: 'block-scalar-header', offset, indent, source: head }
  29482. ];
  29483. if (!addEndtoBlockProps(props, 'end' in token ? token.end : undefined))
  29484. props.push({ type: 'newline', offset: -1, indent, source: '\n' });
  29485. for (const key of Object.keys(token))
  29486. if (key !== 'type' && key !== 'offset')
  29487. delete token[key];
  29488. Object.assign(token, { type: 'block-scalar', indent, props, source: body });
  29489. }
  29490. }
  29491. /** @returns `true` if last token is a newline */
  29492. function addEndtoBlockProps(props, end) {
  29493. if (end)
  29494. for (const st of end)
  29495. switch (st.type) {
  29496. case 'space':
  29497. case 'comment':
  29498. props.push(st);
  29499. break;
  29500. case 'newline':
  29501. props.push(st);
  29502. return true;
  29503. }
  29504. return false;
  29505. }
  29506. function setFlowScalarValue(token, source, type) {
  29507. switch (token.type) {
  29508. case 'scalar':
  29509. case 'double-quoted-scalar':
  29510. case 'single-quoted-scalar':
  29511. token.type = type;
  29512. token.source = source;
  29513. break;
  29514. case 'block-scalar': {
  29515. const end = token.props.slice(1);
  29516. let oa = source.length;
  29517. if (token.props[0].type === 'block-scalar-header')
  29518. oa -= token.props[0].source.length;
  29519. for (const tok of end)
  29520. tok.offset += oa;
  29521. delete token.props;
  29522. Object.assign(token, { type, source, end });
  29523. break;
  29524. }
  29525. case 'block-map':
  29526. case 'block-seq': {
  29527. const offset = token.offset + source.length;
  29528. const nl = { type: 'newline', offset, indent: token.indent, source: '\n' };
  29529. delete token.items;
  29530. Object.assign(token, { type, source, end: [nl] });
  29531. break;
  29532. }
  29533. default: {
  29534. const indent = 'indent' in token ? token.indent : -1;
  29535. const end = 'end' in token && Array.isArray(token.end)
  29536. ? token.end.filter(st => st.type === 'space' ||
  29537. st.type === 'comment' ||
  29538. st.type === 'newline')
  29539. : [];
  29540. for (const key of Object.keys(token))
  29541. if (key !== 'type' && key !== 'offset')
  29542. delete token[key];
  29543. Object.assign(token, { type, indent, source, end });
  29544. }
  29545. }
  29546. }
  29547. /**
  29548. * Stringify a CST document, token, or collection item
  29549. *
  29550. * Fair warning: This applies no validation whatsoever, and
  29551. * simply concatenates the sources in their logical order.
  29552. */
  29553. const stringify$1 = (cst) => 'type' in cst ? stringifyToken(cst) : stringifyItem(cst);
  29554. function stringifyToken(token) {
  29555. switch (token.type) {
  29556. case 'block-scalar': {
  29557. let res = '';
  29558. for (const tok of token.props)
  29559. res += stringifyToken(tok);
  29560. return res + token.source;
  29561. }
  29562. case 'block-map':
  29563. case 'block-seq': {
  29564. let res = '';
  29565. for (const item of token.items)
  29566. res += stringifyItem(item);
  29567. return res;
  29568. }
  29569. case 'flow-collection': {
  29570. let res = token.start.source;
  29571. for (const item of token.items)
  29572. res += stringifyItem(item);
  29573. for (const st of token.end)
  29574. res += st.source;
  29575. return res;
  29576. }
  29577. case 'document': {
  29578. let res = stringifyItem(token);
  29579. if (token.end)
  29580. for (const st of token.end)
  29581. res += st.source;
  29582. return res;
  29583. }
  29584. default: {
  29585. let res = token.source;
  29586. if ('end' in token && token.end)
  29587. for (const st of token.end)
  29588. res += st.source;
  29589. return res;
  29590. }
  29591. }
  29592. }
  29593. function stringifyItem({ start, key, sep, value }) {
  29594. let res = '';
  29595. for (const st of start)
  29596. res += st.source;
  29597. if (key)
  29598. res += stringifyToken(key);
  29599. if (sep)
  29600. for (const st of sep)
  29601. res += st.source;
  29602. if (value)
  29603. res += stringifyToken(value);
  29604. return res;
  29605. }
  29606. const BREAK = Symbol('break visit');
  29607. const SKIP = Symbol('skip children');
  29608. const REMOVE = Symbol('remove item');
  29609. /**
  29610. * Apply a visitor to a CST document or item.
  29611. *
  29612. * Walks through the tree (depth-first) starting from the root, calling a
  29613. * `visitor` function with two arguments when entering each item:
  29614. * - `item`: The current item, which included the following members:
  29615. * - `start: SourceToken[]` – Source tokens before the key or value,
  29616. * possibly including its anchor or tag.
  29617. * - `key?: Token | null` – Set for pair values. May then be `null`, if
  29618. * the key before the `:` separator is empty.
  29619. * - `sep?: SourceToken[]` – Source tokens between the key and the value,
  29620. * which should include the `:` map value indicator if `value` is set.
  29621. * - `value?: Token` – The value of a sequence item, or of a map pair.
  29622. * - `path`: The steps from the root to the current node, as an array of
  29623. * `['key' | 'value', number]` tuples.
  29624. *
  29625. * The return value of the visitor may be used to control the traversal:
  29626. * - `undefined` (default): Do nothing and continue
  29627. * - `visit.SKIP`: Do not visit the children of this token, continue with
  29628. * next sibling
  29629. * - `visit.BREAK`: Terminate traversal completely
  29630. * - `visit.REMOVE`: Remove the current item, then continue with the next one
  29631. * - `number`: Set the index of the next step. This is useful especially if
  29632. * the index of the current token has changed.
  29633. * - `function`: Define the next visitor for this item. After the original
  29634. * visitor is called on item entry, next visitors are called after handling
  29635. * a non-empty `key` and when exiting the item.
  29636. */
  29637. function visit(cst, visitor) {
  29638. if ('type' in cst && cst.type === 'document')
  29639. cst = { start: cst.start, value: cst.value };
  29640. _visit(Object.freeze([]), cst, visitor);
  29641. }
  29642. // Without the `as symbol` casts, TS declares these in the `visit`
  29643. // namespace using `var`, but then complains about that because
  29644. // `unique symbol` must be `const`.
  29645. /** Terminate visit traversal completely */
  29646. visit.BREAK = BREAK;
  29647. /** Do not visit the children of the current item */
  29648. visit.SKIP = SKIP;
  29649. /** Remove the current item */
  29650. visit.REMOVE = REMOVE;
  29651. /** Find the item at `path` from `cst` as the root */
  29652. visit.itemAtPath = (cst, path) => {
  29653. let item = cst;
  29654. for (const [field, index] of path) {
  29655. const tok = item?.[field];
  29656. if (tok && 'items' in tok) {
  29657. item = tok.items[index];
  29658. }
  29659. else
  29660. return undefined;
  29661. }
  29662. return item;
  29663. };
  29664. /**
  29665. * Get the immediate parent collection of the item at `path` from `cst` as the root.
  29666. *
  29667. * Throws an error if the collection is not found, which should never happen if the item itself exists.
  29668. */
  29669. visit.parentCollection = (cst, path) => {
  29670. const parent = visit.itemAtPath(cst, path.slice(0, -1));
  29671. const field = path[path.length - 1][0];
  29672. const coll = parent?.[field];
  29673. if (coll && 'items' in coll)
  29674. return coll;
  29675. throw new Error('Parent collection not found');
  29676. };
  29677. function _visit(path, item, visitor) {
  29678. let ctrl = visitor(item, path);
  29679. if (typeof ctrl === 'symbol')
  29680. return ctrl;
  29681. for (const field of ['key', 'value']) {
  29682. const token = item[field];
  29683. if (token && 'items' in token) {
  29684. for (let i = 0; i < token.items.length; ++i) {
  29685. const ci = _visit(Object.freeze(path.concat([[field, i]])), token.items[i], visitor);
  29686. if (typeof ci === 'number')
  29687. i = ci - 1;
  29688. else if (ci === BREAK)
  29689. return BREAK;
  29690. else if (ci === REMOVE) {
  29691. token.items.splice(i, 1);
  29692. i -= 1;
  29693. }
  29694. }
  29695. if (typeof ctrl === 'function' && field === 'key')
  29696. ctrl = ctrl(item, path);
  29697. }
  29698. }
  29699. return typeof ctrl === 'function' ? ctrl(item, path) : ctrl;
  29700. }
  29701. /** The byte order mark */
  29702. const BOM = '\u{FEFF}';
  29703. /** Start of doc-mode */
  29704. const DOCUMENT = '\x02'; // C0: Start of Text
  29705. /** Unexpected end of flow-mode */
  29706. const FLOW_END = '\x18'; // C0: Cancel
  29707. /** Next token is a scalar value */
  29708. const SCALAR = '\x1f'; // C0: Unit Separator
  29709. /** @returns `true` if `token` is a flow or block collection */
  29710. const isCollection = (token) => !!token && 'items' in token;
  29711. /** @returns `true` if `token` is a flow or block scalar; not an alias */
  29712. const isScalar = (token) => !!token &&
  29713. (token.type === 'scalar' ||
  29714. token.type === 'single-quoted-scalar' ||
  29715. token.type === 'double-quoted-scalar' ||
  29716. token.type === 'block-scalar');
  29717. /* istanbul ignore next */
  29718. /** Get a printable representation of a lexer token */
  29719. function prettyToken(token) {
  29720. switch (token) {
  29721. case BOM:
  29722. return '<BOM>';
  29723. case DOCUMENT:
  29724. return '<DOC>';
  29725. case FLOW_END:
  29726. return '<FLOW_END>';
  29727. case SCALAR:
  29728. return '<SCALAR>';
  29729. default:
  29730. return JSON.stringify(token);
  29731. }
  29732. }
  29733. /** Identify the type of a lexer token. May return `null` for unknown tokens. */
  29734. function tokenType(source) {
  29735. switch (source) {
  29736. case BOM:
  29737. return 'byte-order-mark';
  29738. case DOCUMENT:
  29739. return 'doc-mode';
  29740. case FLOW_END:
  29741. return 'flow-error-end';
  29742. case SCALAR:
  29743. return 'scalar';
  29744. case '---':
  29745. return 'doc-start';
  29746. case '...':
  29747. return 'doc-end';
  29748. case '':
  29749. case '\n':
  29750. case '\r\n':
  29751. return 'newline';
  29752. case '-':
  29753. return 'seq-item-ind';
  29754. case '?':
  29755. return 'explicit-key-ind';
  29756. case ':':
  29757. return 'map-value-ind';
  29758. case '{':
  29759. return 'flow-map-start';
  29760. case '}':
  29761. return 'flow-map-end';
  29762. case '[':
  29763. return 'flow-seq-start';
  29764. case ']':
  29765. return 'flow-seq-end';
  29766. case ',':
  29767. return 'comma';
  29768. }
  29769. switch (source[0]) {
  29770. case ' ':
  29771. case '\t':
  29772. return 'space';
  29773. case '#':
  29774. return 'comment';
  29775. case '%':
  29776. return 'directive-line';
  29777. case '*':
  29778. return 'alias';
  29779. case '&':
  29780. return 'anchor';
  29781. case '!':
  29782. return 'tag';
  29783. case "'":
  29784. return 'single-quoted-scalar';
  29785. case '"':
  29786. return 'double-quoted-scalar';
  29787. case '|':
  29788. case '>':
  29789. return 'block-scalar-header';
  29790. }
  29791. return null;
  29792. }
  29793. var cst = {
  29794. __proto__: null,
  29795. BOM: BOM,
  29796. DOCUMENT: DOCUMENT,
  29797. FLOW_END: FLOW_END,
  29798. SCALAR: SCALAR,
  29799. createScalarToken: createScalarToken,
  29800. isCollection: isCollection,
  29801. isScalar: isScalar,
  29802. prettyToken: prettyToken,
  29803. resolveAsScalar: resolveAsScalar,
  29804. setScalarValue: setScalarValue,
  29805. stringify: stringify$1,
  29806. tokenType: tokenType,
  29807. visit: visit
  29808. };
  29809. /*
  29810. START -> stream
  29811. stream
  29812. directive -> line-end -> stream
  29813. indent + line-end -> stream
  29814. [else] -> line-start
  29815. line-end
  29816. comment -> line-end
  29817. newline -> .
  29818. input-end -> END
  29819. line-start
  29820. doc-start -> doc
  29821. doc-end -> stream
  29822. [else] -> indent -> block-start
  29823. block-start
  29824. seq-item-start -> block-start
  29825. explicit-key-start -> block-start
  29826. map-value-start -> block-start
  29827. [else] -> doc
  29828. doc
  29829. line-end -> line-start
  29830. spaces -> doc
  29831. anchor -> doc
  29832. tag -> doc
  29833. flow-start -> flow -> doc
  29834. flow-end -> error -> doc
  29835. seq-item-start -> error -> doc
  29836. explicit-key-start -> error -> doc
  29837. map-value-start -> doc
  29838. alias -> doc
  29839. quote-start -> quoted-scalar -> doc
  29840. block-scalar-header -> line-end -> block-scalar(min) -> line-start
  29841. [else] -> plain-scalar(false, min) -> doc
  29842. flow
  29843. line-end -> flow
  29844. spaces -> flow
  29845. anchor -> flow
  29846. tag -> flow
  29847. flow-start -> flow -> flow
  29848. flow-end -> .
  29849. seq-item-start -> error -> flow
  29850. explicit-key-start -> flow
  29851. map-value-start -> flow
  29852. alias -> flow
  29853. quote-start -> quoted-scalar -> flow
  29854. comma -> flow
  29855. [else] -> plain-scalar(true, 0) -> flow
  29856. quoted-scalar
  29857. quote-end -> .
  29858. [else] -> quoted-scalar
  29859. block-scalar(min)
  29860. newline + peek(indent < min) -> .
  29861. [else] -> block-scalar(min)
  29862. plain-scalar(is-flow, min)
  29863. scalar-end(is-flow) -> .
  29864. peek(newline + (indent < min)) -> .
  29865. [else] -> plain-scalar(min)
  29866. */
  29867. function isEmpty(ch) {
  29868. switch (ch) {
  29869. case undefined:
  29870. case ' ':
  29871. case '\n':
  29872. case '\r':
  29873. case '\t':
  29874. return true;
  29875. default:
  29876. return false;
  29877. }
  29878. }
  29879. const hexDigits = new Set('0123456789ABCDEFabcdef');
  29880. const tagChars = new Set("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-#;/?:@&=+$_.!~*'()");
  29881. const flowIndicatorChars = new Set(',[]{}');
  29882. const invalidAnchorChars = new Set(' ,[]{}\n\r\t');
  29883. const isNotAnchorChar = (ch) => !ch || invalidAnchorChars.has(ch);
  29884. /**
  29885. * Splits an input string into lexical tokens, i.e. smaller strings that are
  29886. * easily identifiable by `tokens.tokenType()`.
  29887. *
  29888. * Lexing starts always in a "stream" context. Incomplete input may be buffered
  29889. * until a complete token can be emitted.
  29890. *
  29891. * In addition to slices of the original input, the following control characters
  29892. * may also be emitted:
  29893. *
  29894. * - `\x02` (Start of Text): A document starts with the next token
  29895. * - `\x18` (Cancel): Unexpected end of flow-mode (indicates an error)
  29896. * - `\x1f` (Unit Separator): Next token is a scalar value
  29897. * - `\u{FEFF}` (Byte order mark): Emitted separately outside documents
  29898. */
  29899. class Lexer {
  29900. constructor() {
  29901. /**
  29902. * Flag indicating whether the end of the current buffer marks the end of
  29903. * all input
  29904. */
  29905. this.atEnd = false;
  29906. /**
  29907. * Explicit indent set in block scalar header, as an offset from the current
  29908. * minimum indent, so e.g. set to 1 from a header `|2+`. Set to -1 if not
  29909. * explicitly set.
  29910. */
  29911. this.blockScalarIndent = -1;
  29912. /**
  29913. * Block scalars that include a + (keep) chomping indicator in their header
  29914. * include trailing empty lines, which are otherwise excluded from the
  29915. * scalar's contents.
  29916. */
  29917. this.blockScalarKeep = false;
  29918. /** Current input */
  29919. this.buffer = '';
  29920. /**
  29921. * Flag noting whether the map value indicator : can immediately follow this
  29922. * node within a flow context.
  29923. */
  29924. this.flowKey = false;
  29925. /** Count of surrounding flow collection levels. */
  29926. this.flowLevel = 0;
  29927. /**
  29928. * Minimum level of indentation required for next lines to be parsed as a
  29929. * part of the current scalar value.
  29930. */
  29931. this.indentNext = 0;
  29932. /** Indentation level of the current line. */
  29933. this.indentValue = 0;
  29934. /** Position of the next \n character. */
  29935. this.lineEndPos = null;
  29936. /** Stores the state of the lexer if reaching the end of incpomplete input */
  29937. this.next = null;
  29938. /** A pointer to `buffer`; the current position of the lexer. */
  29939. this.pos = 0;
  29940. }
  29941. /**
  29942. * Generate YAML tokens from the `source` string. If `incomplete`,
  29943. * a part of the last line may be left as a buffer for the next call.
  29944. *
  29945. * @returns A generator of lexical tokens
  29946. */
  29947. *lex(source, incomplete = false) {
  29948. if (source) {
  29949. if (typeof source !== 'string')
  29950. throw TypeError('source is not a string');
  29951. this.buffer = this.buffer ? this.buffer + source : source;
  29952. this.lineEndPos = null;
  29953. }
  29954. this.atEnd = !incomplete;
  29955. let next = this.next ?? 'stream';
  29956. while (next && (incomplete || this.hasChars(1)))
  29957. next = yield* this.parseNext(next);
  29958. }
  29959. atLineEnd() {
  29960. let i = this.pos;
  29961. let ch = this.buffer[i];
  29962. while (ch === ' ' || ch === '\t')
  29963. ch = this.buffer[++i];
  29964. if (!ch || ch === '#' || ch === '\n')
  29965. return true;
  29966. if (ch === '\r')
  29967. return this.buffer[i + 1] === '\n';
  29968. return false;
  29969. }
  29970. charAt(n) {
  29971. return this.buffer[this.pos + n];
  29972. }
  29973. continueScalar(offset) {
  29974. let ch = this.buffer[offset];
  29975. if (this.indentNext > 0) {
  29976. let indent = 0;
  29977. while (ch === ' ')
  29978. ch = this.buffer[++indent + offset];
  29979. if (ch === '\r') {
  29980. const next = this.buffer[indent + offset + 1];
  29981. if (next === '\n' || (!next && !this.atEnd))
  29982. return offset + indent + 1;
  29983. }
  29984. return ch === '\n' || indent >= this.indentNext || (!ch && !this.atEnd)
  29985. ? offset + indent
  29986. : -1;
  29987. }
  29988. if (ch === '-' || ch === '.') {
  29989. const dt = this.buffer.substr(offset, 3);
  29990. if ((dt === '---' || dt === '...') && isEmpty(this.buffer[offset + 3]))
  29991. return -1;
  29992. }
  29993. return offset;
  29994. }
  29995. getLine() {
  29996. let end = this.lineEndPos;
  29997. if (typeof end !== 'number' || (end !== -1 && end < this.pos)) {
  29998. end = this.buffer.indexOf('\n', this.pos);
  29999. this.lineEndPos = end;
  30000. }
  30001. if (end === -1)
  30002. return this.atEnd ? this.buffer.substring(this.pos) : null;
  30003. if (this.buffer[end - 1] === '\r')
  30004. end -= 1;
  30005. return this.buffer.substring(this.pos, end);
  30006. }
  30007. hasChars(n) {
  30008. return this.pos + n <= this.buffer.length;
  30009. }
  30010. setNext(state) {
  30011. this.buffer = this.buffer.substring(this.pos);
  30012. this.pos = 0;
  30013. this.lineEndPos = null;
  30014. this.next = state;
  30015. return null;
  30016. }
  30017. peek(n) {
  30018. return this.buffer.substr(this.pos, n);
  30019. }
  30020. *parseNext(next) {
  30021. switch (next) {
  30022. case 'stream':
  30023. return yield* this.parseStream();
  30024. case 'line-start':
  30025. return yield* this.parseLineStart();
  30026. case 'block-start':
  30027. return yield* this.parseBlockStart();
  30028. case 'doc':
  30029. return yield* this.parseDocument();
  30030. case 'flow':
  30031. return yield* this.parseFlowCollection();
  30032. case 'quoted-scalar':
  30033. return yield* this.parseQuotedScalar();
  30034. case 'block-scalar':
  30035. return yield* this.parseBlockScalar();
  30036. case 'plain-scalar':
  30037. return yield* this.parsePlainScalar();
  30038. }
  30039. }
  30040. *parseStream() {
  30041. let line = this.getLine();
  30042. if (line === null)
  30043. return this.setNext('stream');
  30044. if (line[0] === BOM) {
  30045. yield* this.pushCount(1);
  30046. line = line.substring(1);
  30047. }
  30048. if (line[0] === '%') {
  30049. let dirEnd = line.length;
  30050. let cs = line.indexOf('#');
  30051. while (cs !== -1) {
  30052. const ch = line[cs - 1];
  30053. if (ch === ' ' || ch === '\t') {
  30054. dirEnd = cs - 1;
  30055. break;
  30056. }
  30057. else {
  30058. cs = line.indexOf('#', cs + 1);
  30059. }
  30060. }
  30061. while (true) {
  30062. const ch = line[dirEnd - 1];
  30063. if (ch === ' ' || ch === '\t')
  30064. dirEnd -= 1;
  30065. else
  30066. break;
  30067. }
  30068. const n = (yield* this.pushCount(dirEnd)) + (yield* this.pushSpaces(true));
  30069. yield* this.pushCount(line.length - n); // possible comment
  30070. this.pushNewline();
  30071. return 'stream';
  30072. }
  30073. if (this.atLineEnd()) {
  30074. const sp = yield* this.pushSpaces(true);
  30075. yield* this.pushCount(line.length - sp);
  30076. yield* this.pushNewline();
  30077. return 'stream';
  30078. }
  30079. yield DOCUMENT;
  30080. return yield* this.parseLineStart();
  30081. }
  30082. *parseLineStart() {
  30083. const ch = this.charAt(0);
  30084. if (!ch && !this.atEnd)
  30085. return this.setNext('line-start');
  30086. if (ch === '-' || ch === '.') {
  30087. if (!this.atEnd && !this.hasChars(4))
  30088. return this.setNext('line-start');
  30089. const s = this.peek(3);
  30090. if ((s === '---' || s === '...') && isEmpty(this.charAt(3))) {
  30091. yield* this.pushCount(3);
  30092. this.indentValue = 0;
  30093. this.indentNext = 0;
  30094. return s === '---' ? 'doc' : 'stream';
  30095. }
  30096. }
  30097. this.indentValue = yield* this.pushSpaces(false);
  30098. if (this.indentNext > this.indentValue && !isEmpty(this.charAt(1)))
  30099. this.indentNext = this.indentValue;
  30100. return yield* this.parseBlockStart();
  30101. }
  30102. *parseBlockStart() {
  30103. const [ch0, ch1] = this.peek(2);
  30104. if (!ch1 && !this.atEnd)
  30105. return this.setNext('block-start');
  30106. if ((ch0 === '-' || ch0 === '?' || ch0 === ':') && isEmpty(ch1)) {
  30107. const n = (yield* this.pushCount(1)) + (yield* this.pushSpaces(true));
  30108. this.indentNext = this.indentValue + 1;
  30109. this.indentValue += n;
  30110. return yield* this.parseBlockStart();
  30111. }
  30112. return 'doc';
  30113. }
  30114. *parseDocument() {
  30115. yield* this.pushSpaces(true);
  30116. const line = this.getLine();
  30117. if (line === null)
  30118. return this.setNext('doc');
  30119. let n = yield* this.pushIndicators();
  30120. switch (line[n]) {
  30121. case '#':
  30122. yield* this.pushCount(line.length - n);
  30123. // fallthrough
  30124. case undefined:
  30125. yield* this.pushNewline();
  30126. return yield* this.parseLineStart();
  30127. case '{':
  30128. case '[':
  30129. yield* this.pushCount(1);
  30130. this.flowKey = false;
  30131. this.flowLevel = 1;
  30132. return 'flow';
  30133. case '}':
  30134. case ']':
  30135. // this is an error
  30136. yield* this.pushCount(1);
  30137. return 'doc';
  30138. case '*':
  30139. yield* this.pushUntil(isNotAnchorChar);
  30140. return 'doc';
  30141. case '"':
  30142. case "'":
  30143. return yield* this.parseQuotedScalar();
  30144. case '|':
  30145. case '>':
  30146. n += yield* this.parseBlockScalarHeader();
  30147. n += yield* this.pushSpaces(true);
  30148. yield* this.pushCount(line.length - n);
  30149. yield* this.pushNewline();
  30150. return yield* this.parseBlockScalar();
  30151. default:
  30152. return yield* this.parsePlainScalar();
  30153. }
  30154. }
  30155. *parseFlowCollection() {
  30156. let nl, sp;
  30157. let indent = -1;
  30158. do {
  30159. nl = yield* this.pushNewline();
  30160. if (nl > 0) {
  30161. sp = yield* this.pushSpaces(false);
  30162. this.indentValue = indent = sp;
  30163. }
  30164. else {
  30165. sp = 0;
  30166. }
  30167. sp += yield* this.pushSpaces(true);
  30168. } while (nl + sp > 0);
  30169. const line = this.getLine();
  30170. if (line === null)
  30171. return this.setNext('flow');
  30172. if ((indent !== -1 && indent < this.indentNext && line[0] !== '#') ||
  30173. (indent === 0 &&
  30174. (line.startsWith('---') || line.startsWith('...')) &&
  30175. isEmpty(line[3]))) {
  30176. // Allowing for the terminal ] or } at the same (rather than greater)
  30177. // indent level as the initial [ or { is technically invalid, but
  30178. // failing here would be surprising to users.
  30179. const atFlowEndMarker = indent === this.indentNext - 1 &&
  30180. this.flowLevel === 1 &&
  30181. (line[0] === ']' || line[0] === '}');
  30182. if (!atFlowEndMarker) {
  30183. // this is an error
  30184. this.flowLevel = 0;
  30185. yield FLOW_END;
  30186. return yield* this.parseLineStart();
  30187. }
  30188. }
  30189. let n = 0;
  30190. while (line[n] === ',') {
  30191. n += yield* this.pushCount(1);
  30192. n += yield* this.pushSpaces(true);
  30193. this.flowKey = false;
  30194. }
  30195. n += yield* this.pushIndicators();
  30196. switch (line[n]) {
  30197. case undefined:
  30198. return 'flow';
  30199. case '#':
  30200. yield* this.pushCount(line.length - n);
  30201. return 'flow';
  30202. case '{':
  30203. case '[':
  30204. yield* this.pushCount(1);
  30205. this.flowKey = false;
  30206. this.flowLevel += 1;
  30207. return 'flow';
  30208. case '}':
  30209. case ']':
  30210. yield* this.pushCount(1);
  30211. this.flowKey = true;
  30212. this.flowLevel -= 1;
  30213. return this.flowLevel ? 'flow' : 'doc';
  30214. case '*':
  30215. yield* this.pushUntil(isNotAnchorChar);
  30216. return 'flow';
  30217. case '"':
  30218. case "'":
  30219. this.flowKey = true;
  30220. return yield* this.parseQuotedScalar();
  30221. case ':': {
  30222. const next = this.charAt(1);
  30223. if (this.flowKey || isEmpty(next) || next === ',') {
  30224. this.flowKey = false;
  30225. yield* this.pushCount(1);
  30226. yield* this.pushSpaces(true);
  30227. return 'flow';
  30228. }
  30229. }
  30230. // fallthrough
  30231. default:
  30232. this.flowKey = false;
  30233. return yield* this.parsePlainScalar();
  30234. }
  30235. }
  30236. *parseQuotedScalar() {
  30237. const quote = this.charAt(0);
  30238. let end = this.buffer.indexOf(quote, this.pos + 1);
  30239. if (quote === "'") {
  30240. while (end !== -1 && this.buffer[end + 1] === "'")
  30241. end = this.buffer.indexOf("'", end + 2);
  30242. }
  30243. else {
  30244. // double-quote
  30245. while (end !== -1) {
  30246. let n = 0;
  30247. while (this.buffer[end - 1 - n] === '\\')
  30248. n += 1;
  30249. if (n % 2 === 0)
  30250. break;
  30251. end = this.buffer.indexOf('"', end + 1);
  30252. }
  30253. }
  30254. // Only looking for newlines within the quotes
  30255. const qb = this.buffer.substring(0, end);
  30256. let nl = qb.indexOf('\n', this.pos);
  30257. if (nl !== -1) {
  30258. while (nl !== -1) {
  30259. const cs = this.continueScalar(nl + 1);
  30260. if (cs === -1)
  30261. break;
  30262. nl = qb.indexOf('\n', cs);
  30263. }
  30264. if (nl !== -1) {
  30265. // this is an error caused by an unexpected unindent
  30266. end = nl - (qb[nl - 1] === '\r' ? 2 : 1);
  30267. }
  30268. }
  30269. if (end === -1) {
  30270. if (!this.atEnd)
  30271. return this.setNext('quoted-scalar');
  30272. end = this.buffer.length;
  30273. }
  30274. yield* this.pushToIndex(end + 1, false);
  30275. return this.flowLevel ? 'flow' : 'doc';
  30276. }
  30277. *parseBlockScalarHeader() {
  30278. this.blockScalarIndent = -1;
  30279. this.blockScalarKeep = false;
  30280. let i = this.pos;
  30281. while (true) {
  30282. const ch = this.buffer[++i];
  30283. if (ch === '+')
  30284. this.blockScalarKeep = true;
  30285. else if (ch > '0' && ch <= '9')
  30286. this.blockScalarIndent = Number(ch) - 1;
  30287. else if (ch !== '-')
  30288. break;
  30289. }
  30290. return yield* this.pushUntil(ch => isEmpty(ch) || ch === '#');
  30291. }
  30292. *parseBlockScalar() {
  30293. let nl = this.pos - 1; // may be -1 if this.pos === 0
  30294. let indent = 0;
  30295. let ch;
  30296. loop: for (let i = this.pos; (ch = this.buffer[i]); ++i) {
  30297. switch (ch) {
  30298. case ' ':
  30299. indent += 1;
  30300. break;
  30301. case '\n':
  30302. nl = i;
  30303. indent = 0;
  30304. break;
  30305. case '\r': {
  30306. const next = this.buffer[i + 1];
  30307. if (!next && !this.atEnd)
  30308. return this.setNext('block-scalar');
  30309. if (next === '\n')
  30310. break;
  30311. } // fallthrough
  30312. default:
  30313. break loop;
  30314. }
  30315. }
  30316. if (!ch && !this.atEnd)
  30317. return this.setNext('block-scalar');
  30318. if (indent >= this.indentNext) {
  30319. if (this.blockScalarIndent === -1)
  30320. this.indentNext = indent;
  30321. else {
  30322. this.indentNext =
  30323. this.blockScalarIndent + (this.indentNext === 0 ? 1 : this.indentNext);
  30324. }
  30325. do {
  30326. const cs = this.continueScalar(nl + 1);
  30327. if (cs === -1)
  30328. break;
  30329. nl = this.buffer.indexOf('\n', cs);
  30330. } while (nl !== -1);
  30331. if (nl === -1) {
  30332. if (!this.atEnd)
  30333. return this.setNext('block-scalar');
  30334. nl = this.buffer.length;
  30335. }
  30336. }
  30337. // Trailing insufficiently indented tabs are invalid.
  30338. // To catch that during parsing, we include them in the block scalar value.
  30339. let i = nl + 1;
  30340. ch = this.buffer[i];
  30341. while (ch === ' ')
  30342. ch = this.buffer[++i];
  30343. if (ch === '\t') {
  30344. while (ch === '\t' || ch === ' ' || ch === '\r' || ch === '\n')
  30345. ch = this.buffer[++i];
  30346. nl = i - 1;
  30347. }
  30348. else if (!this.blockScalarKeep) {
  30349. do {
  30350. let i = nl - 1;
  30351. let ch = this.buffer[i];
  30352. if (ch === '\r')
  30353. ch = this.buffer[--i];
  30354. const lastChar = i; // Drop the line if last char not more indented
  30355. while (ch === ' ')
  30356. ch = this.buffer[--i];
  30357. if (ch === '\n' && i >= this.pos && i + 1 + indent > lastChar)
  30358. nl = i;
  30359. else
  30360. break;
  30361. } while (true);
  30362. }
  30363. yield SCALAR;
  30364. yield* this.pushToIndex(nl + 1, true);
  30365. return yield* this.parseLineStart();
  30366. }
  30367. *parsePlainScalar() {
  30368. const inFlow = this.flowLevel > 0;
  30369. let end = this.pos - 1;
  30370. let i = this.pos - 1;
  30371. let ch;
  30372. while ((ch = this.buffer[++i])) {
  30373. if (ch === ':') {
  30374. const next = this.buffer[i + 1];
  30375. if (isEmpty(next) || (inFlow && flowIndicatorChars.has(next)))
  30376. break;
  30377. end = i;
  30378. }
  30379. else if (isEmpty(ch)) {
  30380. let next = this.buffer[i + 1];
  30381. if (ch === '\r') {
  30382. if (next === '\n') {
  30383. i += 1;
  30384. ch = '\n';
  30385. next = this.buffer[i + 1];
  30386. }
  30387. else
  30388. end = i;
  30389. }
  30390. if (next === '#' || (inFlow && flowIndicatorChars.has(next)))
  30391. break;
  30392. if (ch === '\n') {
  30393. const cs = this.continueScalar(i + 1);
  30394. if (cs === -1)
  30395. break;
  30396. i = Math.max(i, cs - 2); // to advance, but still account for ' #'
  30397. }
  30398. }
  30399. else {
  30400. if (inFlow && flowIndicatorChars.has(ch))
  30401. break;
  30402. end = i;
  30403. }
  30404. }
  30405. if (!ch && !this.atEnd)
  30406. return this.setNext('plain-scalar');
  30407. yield SCALAR;
  30408. yield* this.pushToIndex(end + 1, true);
  30409. return inFlow ? 'flow' : 'doc';
  30410. }
  30411. *pushCount(n) {
  30412. if (n > 0) {
  30413. yield this.buffer.substr(this.pos, n);
  30414. this.pos += n;
  30415. return n;
  30416. }
  30417. return 0;
  30418. }
  30419. *pushToIndex(i, allowEmpty) {
  30420. const s = this.buffer.slice(this.pos, i);
  30421. if (s) {
  30422. yield s;
  30423. this.pos += s.length;
  30424. return s.length;
  30425. }
  30426. else if (allowEmpty)
  30427. yield '';
  30428. return 0;
  30429. }
  30430. *pushIndicators() {
  30431. switch (this.charAt(0)) {
  30432. case '!':
  30433. return ((yield* this.pushTag()) +
  30434. (yield* this.pushSpaces(true)) +
  30435. (yield* this.pushIndicators()));
  30436. case '&':
  30437. return ((yield* this.pushUntil(isNotAnchorChar)) +
  30438. (yield* this.pushSpaces(true)) +
  30439. (yield* this.pushIndicators()));
  30440. case '-': // this is an error
  30441. case '?': // this is an error outside flow collections
  30442. case ':': {
  30443. const inFlow = this.flowLevel > 0;
  30444. const ch1 = this.charAt(1);
  30445. if (isEmpty(ch1) || (inFlow && flowIndicatorChars.has(ch1))) {
  30446. if (!inFlow)
  30447. this.indentNext = this.indentValue + 1;
  30448. else if (this.flowKey)
  30449. this.flowKey = false;
  30450. return ((yield* this.pushCount(1)) +
  30451. (yield* this.pushSpaces(true)) +
  30452. (yield* this.pushIndicators()));
  30453. }
  30454. }
  30455. }
  30456. return 0;
  30457. }
  30458. *pushTag() {
  30459. if (this.charAt(1) === '<') {
  30460. let i = this.pos + 2;
  30461. let ch = this.buffer[i];
  30462. while (!isEmpty(ch) && ch !== '>')
  30463. ch = this.buffer[++i];
  30464. return yield* this.pushToIndex(ch === '>' ? i + 1 : i, false);
  30465. }
  30466. else {
  30467. let i = this.pos + 1;
  30468. let ch = this.buffer[i];
  30469. while (ch) {
  30470. if (tagChars.has(ch))
  30471. ch = this.buffer[++i];
  30472. else if (ch === '%' &&
  30473. hexDigits.has(this.buffer[i + 1]) &&
  30474. hexDigits.has(this.buffer[i + 2])) {
  30475. ch = this.buffer[(i += 3)];
  30476. }
  30477. else
  30478. break;
  30479. }
  30480. return yield* this.pushToIndex(i, false);
  30481. }
  30482. }
  30483. *pushNewline() {
  30484. const ch = this.buffer[this.pos];
  30485. if (ch === '\n')
  30486. return yield* this.pushCount(1);
  30487. else if (ch === '\r' && this.charAt(1) === '\n')
  30488. return yield* this.pushCount(2);
  30489. else
  30490. return 0;
  30491. }
  30492. *pushSpaces(allowTabs) {
  30493. let i = this.pos - 1;
  30494. let ch;
  30495. do {
  30496. ch = this.buffer[++i];
  30497. } while (ch === ' ' || (allowTabs && ch === '\t'));
  30498. const n = i - this.pos;
  30499. if (n > 0) {
  30500. yield this.buffer.substr(this.pos, n);
  30501. this.pos = i;
  30502. }
  30503. return n;
  30504. }
  30505. *pushUntil(test) {
  30506. let i = this.pos;
  30507. let ch = this.buffer[i];
  30508. while (!test(ch))
  30509. ch = this.buffer[++i];
  30510. return yield* this.pushToIndex(i, false);
  30511. }
  30512. }
  30513. /**
  30514. * Tracks newlines during parsing in order to provide an efficient API for
  30515. * determining the one-indexed `{ line, col }` position for any offset
  30516. * within the input.
  30517. */
  30518. class LineCounter {
  30519. constructor() {
  30520. this.lineStarts = [];
  30521. /**
  30522. * Should be called in ascending order. Otherwise, call
  30523. * `lineCounter.lineStarts.sort()` before calling `linePos()`.
  30524. */
  30525. this.addNewLine = (offset) => this.lineStarts.push(offset);
  30526. /**
  30527. * Performs a binary search and returns the 1-indexed { line, col }
  30528. * position of `offset`. If `line === 0`, `addNewLine` has never been
  30529. * called or `offset` is before the first known newline.
  30530. */
  30531. this.linePos = (offset) => {
  30532. let low = 0;
  30533. let high = this.lineStarts.length;
  30534. while (low < high) {
  30535. const mid = (low + high) >> 1; // Math.floor((low + high) / 2)
  30536. if (this.lineStarts[mid] < offset)
  30537. low = mid + 1;
  30538. else
  30539. high = mid;
  30540. }
  30541. if (this.lineStarts[low] === offset)
  30542. return { line: low + 1, col: 1 };
  30543. if (low === 0)
  30544. return { line: 0, col: offset };
  30545. const start = this.lineStarts[low - 1];
  30546. return { line: low, col: offset - start + 1 };
  30547. };
  30548. }
  30549. }
  30550. function includesToken(list, type) {
  30551. for (let i = 0; i < list.length; ++i)
  30552. if (list[i].type === type)
  30553. return true;
  30554. return false;
  30555. }
  30556. function findNonEmptyIndex(list) {
  30557. for (let i = 0; i < list.length; ++i) {
  30558. switch (list[i].type) {
  30559. case 'space':
  30560. case 'comment':
  30561. case 'newline':
  30562. break;
  30563. default:
  30564. return i;
  30565. }
  30566. }
  30567. return -1;
  30568. }
  30569. function isFlowToken(token) {
  30570. switch (token?.type) {
  30571. case 'alias':
  30572. case 'scalar':
  30573. case 'single-quoted-scalar':
  30574. case 'double-quoted-scalar':
  30575. case 'flow-collection':
  30576. return true;
  30577. default:
  30578. return false;
  30579. }
  30580. }
  30581. function getPrevProps(parent) {
  30582. switch (parent.type) {
  30583. case 'document':
  30584. return parent.start;
  30585. case 'block-map': {
  30586. const it = parent.items[parent.items.length - 1];
  30587. return it.sep ?? it.start;
  30588. }
  30589. case 'block-seq':
  30590. return parent.items[parent.items.length - 1].start;
  30591. /* istanbul ignore next should not happen */
  30592. default:
  30593. return [];
  30594. }
  30595. }
  30596. /** Note: May modify input array */
  30597. function getFirstKeyStartProps(prev) {
  30598. if (prev.length === 0)
  30599. return [];
  30600. let i = prev.length;
  30601. loop: while (--i >= 0) {
  30602. switch (prev[i].type) {
  30603. case 'doc-start':
  30604. case 'explicit-key-ind':
  30605. case 'map-value-ind':
  30606. case 'seq-item-ind':
  30607. case 'newline':
  30608. break loop;
  30609. }
  30610. }
  30611. return prev.splice(i, prev.length);
  30612. }
  30613. function fixFlowSeqItems(fc) {
  30614. if (fc.start.type === 'flow-seq-start') {
  30615. for (const it of fc.items) {
  30616. if (it.sep &&
  30617. !it.value &&
  30618. !includesToken(it.start, 'explicit-key-ind') &&
  30619. !includesToken(it.sep, 'map-value-ind')) {
  30620. if (it.key)
  30621. it.value = it.key;
  30622. delete it.key;
  30623. if (isFlowToken(it.value)) {
  30624. if (it.value.end)
  30625. Array.prototype.push.apply(it.value.end, it.sep);
  30626. else
  30627. it.value.end = it.sep;
  30628. }
  30629. else
  30630. Array.prototype.push.apply(it.start, it.sep);
  30631. delete it.sep;
  30632. }
  30633. }
  30634. }
  30635. }
  30636. /**
  30637. * A YAML concrete syntax tree (CST) parser
  30638. *
  30639. * ```ts
  30640. * const src: string = ...
  30641. * for (const token of new Parser().parse(src)) {
  30642. * // token: Token
  30643. * }
  30644. * ```
  30645. *
  30646. * To use the parser with a user-provided lexer:
  30647. *
  30648. * ```ts
  30649. * function* parse(source: string, lexer: Lexer) {
  30650. * const parser = new Parser()
  30651. * for (const lexeme of lexer.lex(source))
  30652. * yield* parser.next(lexeme)
  30653. * yield* parser.end()
  30654. * }
  30655. *
  30656. * const src: string = ...
  30657. * const lexer = new Lexer()
  30658. * for (const token of parse(src, lexer)) {
  30659. * // token: Token
  30660. * }
  30661. * ```
  30662. */
  30663. class Parser {
  30664. /**
  30665. * @param onNewLine - If defined, called separately with the start position of
  30666. * each new line (in `parse()`, including the start of input).
  30667. */
  30668. constructor(onNewLine) {
  30669. /** If true, space and sequence indicators count as indentation */
  30670. this.atNewLine = true;
  30671. /** If true, next token is a scalar value */
  30672. this.atScalar = false;
  30673. /** Current indentation level */
  30674. this.indent = 0;
  30675. /** Current offset since the start of parsing */
  30676. this.offset = 0;
  30677. /** On the same line with a block map key */
  30678. this.onKeyLine = false;
  30679. /** Top indicates the node that's currently being built */
  30680. this.stack = [];
  30681. /** The source of the current token, set in parse() */
  30682. this.source = '';
  30683. /** The type of the current token, set in parse() */
  30684. this.type = '';
  30685. // Must be defined after `next()`
  30686. this.lexer = new Lexer();
  30687. this.onNewLine = onNewLine;
  30688. }
  30689. /**
  30690. * Parse `source` as a YAML stream.
  30691. * If `incomplete`, a part of the last line may be left as a buffer for the next call.
  30692. *
  30693. * Errors are not thrown, but yielded as `{ type: 'error', message }` tokens.
  30694. *
  30695. * @returns A generator of tokens representing each directive, document, and other structure.
  30696. */
  30697. *parse(source, incomplete = false) {
  30698. if (this.onNewLine && this.offset === 0)
  30699. this.onNewLine(0);
  30700. for (const lexeme of this.lexer.lex(source, incomplete))
  30701. yield* this.next(lexeme);
  30702. if (!incomplete)
  30703. yield* this.end();
  30704. }
  30705. /**
  30706. * Advance the parser by the `source` of one lexical token.
  30707. */
  30708. *next(source) {
  30709. this.source = source;
  30710. if (this.atScalar) {
  30711. this.atScalar = false;
  30712. yield* this.step();
  30713. this.offset += source.length;
  30714. return;
  30715. }
  30716. const type = tokenType(source);
  30717. if (!type) {
  30718. const message = `Not a YAML token: ${source}`;
  30719. yield* this.pop({ type: 'error', offset: this.offset, message, source });
  30720. this.offset += source.length;
  30721. }
  30722. else if (type === 'scalar') {
  30723. this.atNewLine = false;
  30724. this.atScalar = true;
  30725. this.type = 'scalar';
  30726. }
  30727. else {
  30728. this.type = type;
  30729. yield* this.step();
  30730. switch (type) {
  30731. case 'newline':
  30732. this.atNewLine = true;
  30733. this.indent = 0;
  30734. if (this.onNewLine)
  30735. this.onNewLine(this.offset + source.length);
  30736. break;
  30737. case 'space':
  30738. if (this.atNewLine && source[0] === ' ')
  30739. this.indent += source.length;
  30740. break;
  30741. case 'explicit-key-ind':
  30742. case 'map-value-ind':
  30743. case 'seq-item-ind':
  30744. if (this.atNewLine)
  30745. this.indent += source.length;
  30746. break;
  30747. case 'doc-mode':
  30748. case 'flow-error-end':
  30749. return;
  30750. default:
  30751. this.atNewLine = false;
  30752. }
  30753. this.offset += source.length;
  30754. }
  30755. }
  30756. /** Call at end of input to push out any remaining constructions */
  30757. *end() {
  30758. while (this.stack.length > 0)
  30759. yield* this.pop();
  30760. }
  30761. get sourceToken() {
  30762. const st = {
  30763. type: this.type,
  30764. offset: this.offset,
  30765. indent: this.indent,
  30766. source: this.source
  30767. };
  30768. return st;
  30769. }
  30770. *step() {
  30771. const top = this.peek(1);
  30772. if (this.type === 'doc-end' && (!top || top.type !== 'doc-end')) {
  30773. while (this.stack.length > 0)
  30774. yield* this.pop();
  30775. this.stack.push({
  30776. type: 'doc-end',
  30777. offset: this.offset,
  30778. source: this.source
  30779. });
  30780. return;
  30781. }
  30782. if (!top)
  30783. return yield* this.stream();
  30784. switch (top.type) {
  30785. case 'document':
  30786. return yield* this.document(top);
  30787. case 'alias':
  30788. case 'scalar':
  30789. case 'single-quoted-scalar':
  30790. case 'double-quoted-scalar':
  30791. return yield* this.scalar(top);
  30792. case 'block-scalar':
  30793. return yield* this.blockScalar(top);
  30794. case 'block-map':
  30795. return yield* this.blockMap(top);
  30796. case 'block-seq':
  30797. return yield* this.blockSequence(top);
  30798. case 'flow-collection':
  30799. return yield* this.flowCollection(top);
  30800. case 'doc-end':
  30801. return yield* this.documentEnd(top);
  30802. }
  30803. /* istanbul ignore next should not happen */
  30804. yield* this.pop();
  30805. }
  30806. peek(n) {
  30807. return this.stack[this.stack.length - n];
  30808. }
  30809. *pop(error) {
  30810. const token = error ?? this.stack.pop();
  30811. /* istanbul ignore if should not happen */
  30812. if (!token) {
  30813. const message = 'Tried to pop an empty stack';
  30814. yield { type: 'error', offset: this.offset, source: '', message };
  30815. }
  30816. else if (this.stack.length === 0) {
  30817. yield token;
  30818. }
  30819. else {
  30820. const top = this.peek(1);
  30821. if (token.type === 'block-scalar') {
  30822. // Block scalars use their parent rather than header indent
  30823. token.indent = 'indent' in top ? top.indent : 0;
  30824. }
  30825. else if (token.type === 'flow-collection' && top.type === 'document') {
  30826. // Ignore all indent for top-level flow collections
  30827. token.indent = 0;
  30828. }
  30829. if (token.type === 'flow-collection')
  30830. fixFlowSeqItems(token);
  30831. switch (top.type) {
  30832. case 'document':
  30833. top.value = token;
  30834. break;
  30835. case 'block-scalar':
  30836. top.props.push(token); // error
  30837. break;
  30838. case 'block-map': {
  30839. const it = top.items[top.items.length - 1];
  30840. if (it.value) {
  30841. top.items.push({ start: [], key: token, sep: [] });
  30842. this.onKeyLine = true;
  30843. return;
  30844. }
  30845. else if (it.sep) {
  30846. it.value = token;
  30847. }
  30848. else {
  30849. Object.assign(it, { key: token, sep: [] });
  30850. this.onKeyLine = !it.explicitKey;
  30851. return;
  30852. }
  30853. break;
  30854. }
  30855. case 'block-seq': {
  30856. const it = top.items[top.items.length - 1];
  30857. if (it.value)
  30858. top.items.push({ start: [], value: token });
  30859. else
  30860. it.value = token;
  30861. break;
  30862. }
  30863. case 'flow-collection': {
  30864. const it = top.items[top.items.length - 1];
  30865. if (!it || it.value)
  30866. top.items.push({ start: [], key: token, sep: [] });
  30867. else if (it.sep)
  30868. it.value = token;
  30869. else
  30870. Object.assign(it, { key: token, sep: [] });
  30871. return;
  30872. }
  30873. /* istanbul ignore next should not happen */
  30874. default:
  30875. yield* this.pop();
  30876. yield* this.pop(token);
  30877. }
  30878. if ((top.type === 'document' ||
  30879. top.type === 'block-map' ||
  30880. top.type === 'block-seq') &&
  30881. (token.type === 'block-map' || token.type === 'block-seq')) {
  30882. const last = token.items[token.items.length - 1];
  30883. if (last &&
  30884. !last.sep &&
  30885. !last.value &&
  30886. last.start.length > 0 &&
  30887. findNonEmptyIndex(last.start) === -1 &&
  30888. (token.indent === 0 ||
  30889. last.start.every(st => st.type !== 'comment' || st.indent < token.indent))) {
  30890. if (top.type === 'document')
  30891. top.end = last.start;
  30892. else
  30893. top.items.push({ start: last.start });
  30894. token.items.splice(-1, 1);
  30895. }
  30896. }
  30897. }
  30898. }
  30899. *stream() {
  30900. switch (this.type) {
  30901. case 'directive-line':
  30902. yield { type: 'directive', offset: this.offset, source: this.source };
  30903. return;
  30904. case 'byte-order-mark':
  30905. case 'space':
  30906. case 'comment':
  30907. case 'newline':
  30908. yield this.sourceToken;
  30909. return;
  30910. case 'doc-mode':
  30911. case 'doc-start': {
  30912. const doc = {
  30913. type: 'document',
  30914. offset: this.offset,
  30915. start: []
  30916. };
  30917. if (this.type === 'doc-start')
  30918. doc.start.push(this.sourceToken);
  30919. this.stack.push(doc);
  30920. return;
  30921. }
  30922. }
  30923. yield {
  30924. type: 'error',
  30925. offset: this.offset,
  30926. message: `Unexpected ${this.type} token in YAML stream`,
  30927. source: this.source
  30928. };
  30929. }
  30930. *document(doc) {
  30931. if (doc.value)
  30932. return yield* this.lineEnd(doc);
  30933. switch (this.type) {
  30934. case 'doc-start': {
  30935. if (findNonEmptyIndex(doc.start) !== -1) {
  30936. yield* this.pop();
  30937. yield* this.step();
  30938. }
  30939. else
  30940. doc.start.push(this.sourceToken);
  30941. return;
  30942. }
  30943. case 'anchor':
  30944. case 'tag':
  30945. case 'space':
  30946. case 'comment':
  30947. case 'newline':
  30948. doc.start.push(this.sourceToken);
  30949. return;
  30950. }
  30951. const bv = this.startBlockValue(doc);
  30952. if (bv)
  30953. this.stack.push(bv);
  30954. else {
  30955. yield {
  30956. type: 'error',
  30957. offset: this.offset,
  30958. message: `Unexpected ${this.type} token in YAML document`,
  30959. source: this.source
  30960. };
  30961. }
  30962. }
  30963. *scalar(scalar) {
  30964. if (this.type === 'map-value-ind') {
  30965. const prev = getPrevProps(this.peek(2));
  30966. const start = getFirstKeyStartProps(prev);
  30967. let sep;
  30968. if (scalar.end) {
  30969. sep = scalar.end;
  30970. sep.push(this.sourceToken);
  30971. delete scalar.end;
  30972. }
  30973. else
  30974. sep = [this.sourceToken];
  30975. const map = {
  30976. type: 'block-map',
  30977. offset: scalar.offset,
  30978. indent: scalar.indent,
  30979. items: [{ start, key: scalar, sep }]
  30980. };
  30981. this.onKeyLine = true;
  30982. this.stack[this.stack.length - 1] = map;
  30983. }
  30984. else
  30985. yield* this.lineEnd(scalar);
  30986. }
  30987. *blockScalar(scalar) {
  30988. switch (this.type) {
  30989. case 'space':
  30990. case 'comment':
  30991. case 'newline':
  30992. scalar.props.push(this.sourceToken);
  30993. return;
  30994. case 'scalar':
  30995. scalar.source = this.source;
  30996. // block-scalar source includes trailing newline
  30997. this.atNewLine = true;
  30998. this.indent = 0;
  30999. if (this.onNewLine) {
  31000. let nl = this.source.indexOf('\n') + 1;
  31001. while (nl !== 0) {
  31002. this.onNewLine(this.offset + nl);
  31003. nl = this.source.indexOf('\n', nl) + 1;
  31004. }
  31005. }
  31006. yield* this.pop();
  31007. break;
  31008. /* istanbul ignore next should not happen */
  31009. default:
  31010. yield* this.pop();
  31011. yield* this.step();
  31012. }
  31013. }
  31014. *blockMap(map) {
  31015. const it = map.items[map.items.length - 1];
  31016. // it.sep is true-ish if pair already has key or : separator
  31017. switch (this.type) {
  31018. case 'newline':
  31019. this.onKeyLine = false;
  31020. if (it.value) {
  31021. const end = 'end' in it.value ? it.value.end : undefined;
  31022. const last = Array.isArray(end) ? end[end.length - 1] : undefined;
  31023. if (last?.type === 'comment')
  31024. end?.push(this.sourceToken);
  31025. else
  31026. map.items.push({ start: [this.sourceToken] });
  31027. }
  31028. else if (it.sep) {
  31029. it.sep.push(this.sourceToken);
  31030. }
  31031. else {
  31032. it.start.push(this.sourceToken);
  31033. }
  31034. return;
  31035. case 'space':
  31036. case 'comment':
  31037. if (it.value) {
  31038. map.items.push({ start: [this.sourceToken] });
  31039. }
  31040. else if (it.sep) {
  31041. it.sep.push(this.sourceToken);
  31042. }
  31043. else {
  31044. if (this.atIndentedComment(it.start, map.indent)) {
  31045. const prev = map.items[map.items.length - 2];
  31046. const end = prev?.value?.end;
  31047. if (Array.isArray(end)) {
  31048. Array.prototype.push.apply(end, it.start);
  31049. end.push(this.sourceToken);
  31050. map.items.pop();
  31051. return;
  31052. }
  31053. }
  31054. it.start.push(this.sourceToken);
  31055. }
  31056. return;
  31057. }
  31058. if (this.indent >= map.indent) {
  31059. const atMapIndent = !this.onKeyLine && this.indent === map.indent;
  31060. const atNextItem = atMapIndent &&
  31061. (it.sep || it.explicitKey) &&
  31062. this.type !== 'seq-item-ind';
  31063. // For empty nodes, assign newline-separated not indented empty tokens to following node
  31064. let start = [];
  31065. if (atNextItem && it.sep && !it.value) {
  31066. const nl = [];
  31067. for (let i = 0; i < it.sep.length; ++i) {
  31068. const st = it.sep[i];
  31069. switch (st.type) {
  31070. case 'newline':
  31071. nl.push(i);
  31072. break;
  31073. case 'space':
  31074. break;
  31075. case 'comment':
  31076. if (st.indent > map.indent)
  31077. nl.length = 0;
  31078. break;
  31079. default:
  31080. nl.length = 0;
  31081. }
  31082. }
  31083. if (nl.length >= 2)
  31084. start = it.sep.splice(nl[1]);
  31085. }
  31086. switch (this.type) {
  31087. case 'anchor':
  31088. case 'tag':
  31089. if (atNextItem || it.value) {
  31090. start.push(this.sourceToken);
  31091. map.items.push({ start });
  31092. this.onKeyLine = true;
  31093. }
  31094. else if (it.sep) {
  31095. it.sep.push(this.sourceToken);
  31096. }
  31097. else {
  31098. it.start.push(this.sourceToken);
  31099. }
  31100. return;
  31101. case 'explicit-key-ind':
  31102. if (!it.sep && !it.explicitKey) {
  31103. it.start.push(this.sourceToken);
  31104. it.explicitKey = true;
  31105. }
  31106. else if (atNextItem || it.value) {
  31107. start.push(this.sourceToken);
  31108. map.items.push({ start, explicitKey: true });
  31109. }
  31110. else {
  31111. this.stack.push({
  31112. type: 'block-map',
  31113. offset: this.offset,
  31114. indent: this.indent,
  31115. items: [{ start: [this.sourceToken], explicitKey: true }]
  31116. });
  31117. }
  31118. this.onKeyLine = true;
  31119. return;
  31120. case 'map-value-ind':
  31121. if (it.explicitKey) {
  31122. if (!it.sep) {
  31123. if (includesToken(it.start, 'newline')) {
  31124. Object.assign(it, { key: null, sep: [this.sourceToken] });
  31125. }
  31126. else {
  31127. const start = getFirstKeyStartProps(it.start);
  31128. this.stack.push({
  31129. type: 'block-map',
  31130. offset: this.offset,
  31131. indent: this.indent,
  31132. items: [{ start, key: null, sep: [this.sourceToken] }]
  31133. });
  31134. }
  31135. }
  31136. else if (it.value) {
  31137. map.items.push({ start: [], key: null, sep: [this.sourceToken] });
  31138. }
  31139. else if (includesToken(it.sep, 'map-value-ind')) {
  31140. this.stack.push({
  31141. type: 'block-map',
  31142. offset: this.offset,
  31143. indent: this.indent,
  31144. items: [{ start, key: null, sep: [this.sourceToken] }]
  31145. });
  31146. }
  31147. else if (isFlowToken(it.key) &&
  31148. !includesToken(it.sep, 'newline')) {
  31149. const start = getFirstKeyStartProps(it.start);
  31150. const key = it.key;
  31151. const sep = it.sep;
  31152. sep.push(this.sourceToken);
  31153. // @ts-expect-error type guard is wrong here
  31154. delete it.key, delete it.sep;
  31155. this.stack.push({
  31156. type: 'block-map',
  31157. offset: this.offset,
  31158. indent: this.indent,
  31159. items: [{ start, key, sep }]
  31160. });
  31161. }
  31162. else if (start.length > 0) {
  31163. // Not actually at next item
  31164. it.sep = it.sep.concat(start, this.sourceToken);
  31165. }
  31166. else {
  31167. it.sep.push(this.sourceToken);
  31168. }
  31169. }
  31170. else {
  31171. if (!it.sep) {
  31172. Object.assign(it, { key: null, sep: [this.sourceToken] });
  31173. }
  31174. else if (it.value || atNextItem) {
  31175. map.items.push({ start, key: null, sep: [this.sourceToken] });
  31176. }
  31177. else if (includesToken(it.sep, 'map-value-ind')) {
  31178. this.stack.push({
  31179. type: 'block-map',
  31180. offset: this.offset,
  31181. indent: this.indent,
  31182. items: [{ start: [], key: null, sep: [this.sourceToken] }]
  31183. });
  31184. }
  31185. else {
  31186. it.sep.push(this.sourceToken);
  31187. }
  31188. }
  31189. this.onKeyLine = true;
  31190. return;
  31191. case 'alias':
  31192. case 'scalar':
  31193. case 'single-quoted-scalar':
  31194. case 'double-quoted-scalar': {
  31195. const fs = this.flowScalar(this.type);
  31196. if (atNextItem || it.value) {
  31197. map.items.push({ start, key: fs, sep: [] });
  31198. this.onKeyLine = true;
  31199. }
  31200. else if (it.sep) {
  31201. this.stack.push(fs);
  31202. }
  31203. else {
  31204. Object.assign(it, { key: fs, sep: [] });
  31205. this.onKeyLine = true;
  31206. }
  31207. return;
  31208. }
  31209. default: {
  31210. const bv = this.startBlockValue(map);
  31211. if (bv) {
  31212. if (atMapIndent && bv.type !== 'block-seq') {
  31213. map.items.push({ start });
  31214. }
  31215. this.stack.push(bv);
  31216. return;
  31217. }
  31218. }
  31219. }
  31220. }
  31221. yield* this.pop();
  31222. yield* this.step();
  31223. }
  31224. *blockSequence(seq) {
  31225. const it = seq.items[seq.items.length - 1];
  31226. switch (this.type) {
  31227. case 'newline':
  31228. if (it.value) {
  31229. const end = 'end' in it.value ? it.value.end : undefined;
  31230. const last = Array.isArray(end) ? end[end.length - 1] : undefined;
  31231. if (last?.type === 'comment')
  31232. end?.push(this.sourceToken);
  31233. else
  31234. seq.items.push({ start: [this.sourceToken] });
  31235. }
  31236. else
  31237. it.start.push(this.sourceToken);
  31238. return;
  31239. case 'space':
  31240. case 'comment':
  31241. if (it.value)
  31242. seq.items.push({ start: [this.sourceToken] });
  31243. else {
  31244. if (this.atIndentedComment(it.start, seq.indent)) {
  31245. const prev = seq.items[seq.items.length - 2];
  31246. const end = prev?.value?.end;
  31247. if (Array.isArray(end)) {
  31248. Array.prototype.push.apply(end, it.start);
  31249. end.push(this.sourceToken);
  31250. seq.items.pop();
  31251. return;
  31252. }
  31253. }
  31254. it.start.push(this.sourceToken);
  31255. }
  31256. return;
  31257. case 'anchor':
  31258. case 'tag':
  31259. if (it.value || this.indent <= seq.indent)
  31260. break;
  31261. it.start.push(this.sourceToken);
  31262. return;
  31263. case 'seq-item-ind':
  31264. if (this.indent !== seq.indent)
  31265. break;
  31266. if (it.value || includesToken(it.start, 'seq-item-ind'))
  31267. seq.items.push({ start: [this.sourceToken] });
  31268. else
  31269. it.start.push(this.sourceToken);
  31270. return;
  31271. }
  31272. if (this.indent > seq.indent) {
  31273. const bv = this.startBlockValue(seq);
  31274. if (bv) {
  31275. this.stack.push(bv);
  31276. return;
  31277. }
  31278. }
  31279. yield* this.pop();
  31280. yield* this.step();
  31281. }
  31282. *flowCollection(fc) {
  31283. const it = fc.items[fc.items.length - 1];
  31284. if (this.type === 'flow-error-end') {
  31285. let top;
  31286. do {
  31287. yield* this.pop();
  31288. top = this.peek(1);
  31289. } while (top && top.type === 'flow-collection');
  31290. }
  31291. else if (fc.end.length === 0) {
  31292. switch (this.type) {
  31293. case 'comma':
  31294. case 'explicit-key-ind':
  31295. if (!it || it.sep)
  31296. fc.items.push({ start: [this.sourceToken] });
  31297. else
  31298. it.start.push(this.sourceToken);
  31299. return;
  31300. case 'map-value-ind':
  31301. if (!it || it.value)
  31302. fc.items.push({ start: [], key: null, sep: [this.sourceToken] });
  31303. else if (it.sep)
  31304. it.sep.push(this.sourceToken);
  31305. else
  31306. Object.assign(it, { key: null, sep: [this.sourceToken] });
  31307. return;
  31308. case 'space':
  31309. case 'comment':
  31310. case 'newline':
  31311. case 'anchor':
  31312. case 'tag':
  31313. if (!it || it.value)
  31314. fc.items.push({ start: [this.sourceToken] });
  31315. else if (it.sep)
  31316. it.sep.push(this.sourceToken);
  31317. else
  31318. it.start.push(this.sourceToken);
  31319. return;
  31320. case 'alias':
  31321. case 'scalar':
  31322. case 'single-quoted-scalar':
  31323. case 'double-quoted-scalar': {
  31324. const fs = this.flowScalar(this.type);
  31325. if (!it || it.value)
  31326. fc.items.push({ start: [], key: fs, sep: [] });
  31327. else if (it.sep)
  31328. this.stack.push(fs);
  31329. else
  31330. Object.assign(it, { key: fs, sep: [] });
  31331. return;
  31332. }
  31333. case 'flow-map-end':
  31334. case 'flow-seq-end':
  31335. fc.end.push(this.sourceToken);
  31336. return;
  31337. }
  31338. const bv = this.startBlockValue(fc);
  31339. /* istanbul ignore else should not happen */
  31340. if (bv)
  31341. this.stack.push(bv);
  31342. else {
  31343. yield* this.pop();
  31344. yield* this.step();
  31345. }
  31346. }
  31347. else {
  31348. const parent = this.peek(2);
  31349. if (parent.type === 'block-map' &&
  31350. ((this.type === 'map-value-ind' && parent.indent === fc.indent) ||
  31351. (this.type === 'newline' &&
  31352. !parent.items[parent.items.length - 1].sep))) {
  31353. yield* this.pop();
  31354. yield* this.step();
  31355. }
  31356. else if (this.type === 'map-value-ind' &&
  31357. parent.type !== 'flow-collection') {
  31358. const prev = getPrevProps(parent);
  31359. const start = getFirstKeyStartProps(prev);
  31360. fixFlowSeqItems(fc);
  31361. const sep = fc.end.splice(1, fc.end.length);
  31362. sep.push(this.sourceToken);
  31363. const map = {
  31364. type: 'block-map',
  31365. offset: fc.offset,
  31366. indent: fc.indent,
  31367. items: [{ start, key: fc, sep }]
  31368. };
  31369. this.onKeyLine = true;
  31370. this.stack[this.stack.length - 1] = map;
  31371. }
  31372. else {
  31373. yield* this.lineEnd(fc);
  31374. }
  31375. }
  31376. }
  31377. flowScalar(type) {
  31378. if (this.onNewLine) {
  31379. let nl = this.source.indexOf('\n') + 1;
  31380. while (nl !== 0) {
  31381. this.onNewLine(this.offset + nl);
  31382. nl = this.source.indexOf('\n', nl) + 1;
  31383. }
  31384. }
  31385. return {
  31386. type,
  31387. offset: this.offset,
  31388. indent: this.indent,
  31389. source: this.source
  31390. };
  31391. }
  31392. startBlockValue(parent) {
  31393. switch (this.type) {
  31394. case 'alias':
  31395. case 'scalar':
  31396. case 'single-quoted-scalar':
  31397. case 'double-quoted-scalar':
  31398. return this.flowScalar(this.type);
  31399. case 'block-scalar-header':
  31400. return {
  31401. type: 'block-scalar',
  31402. offset: this.offset,
  31403. indent: this.indent,
  31404. props: [this.sourceToken],
  31405. source: ''
  31406. };
  31407. case 'flow-map-start':
  31408. case 'flow-seq-start':
  31409. return {
  31410. type: 'flow-collection',
  31411. offset: this.offset,
  31412. indent: this.indent,
  31413. start: this.sourceToken,
  31414. items: [],
  31415. end: []
  31416. };
  31417. case 'seq-item-ind':
  31418. return {
  31419. type: 'block-seq',
  31420. offset: this.offset,
  31421. indent: this.indent,
  31422. items: [{ start: [this.sourceToken] }]
  31423. };
  31424. case 'explicit-key-ind': {
  31425. this.onKeyLine = true;
  31426. const prev = getPrevProps(parent);
  31427. const start = getFirstKeyStartProps(prev);
  31428. start.push(this.sourceToken);
  31429. return {
  31430. type: 'block-map',
  31431. offset: this.offset,
  31432. indent: this.indent,
  31433. items: [{ start, explicitKey: true }]
  31434. };
  31435. }
  31436. case 'map-value-ind': {
  31437. this.onKeyLine = true;
  31438. const prev = getPrevProps(parent);
  31439. const start = getFirstKeyStartProps(prev);
  31440. return {
  31441. type: 'block-map',
  31442. offset: this.offset,
  31443. indent: this.indent,
  31444. items: [{ start, key: null, sep: [this.sourceToken] }]
  31445. };
  31446. }
  31447. }
  31448. return null;
  31449. }
  31450. atIndentedComment(start, indent) {
  31451. if (this.type !== 'comment')
  31452. return false;
  31453. if (this.indent <= indent)
  31454. return false;
  31455. return start.every(st => st.type === 'newline' || st.type === 'space');
  31456. }
  31457. *documentEnd(docEnd) {
  31458. if (this.type !== 'doc-mode') {
  31459. if (docEnd.end)
  31460. docEnd.end.push(this.sourceToken);
  31461. else
  31462. docEnd.end = [this.sourceToken];
  31463. if (this.type === 'newline')
  31464. yield* this.pop();
  31465. }
  31466. }
  31467. *lineEnd(token) {
  31468. switch (this.type) {
  31469. case 'comma':
  31470. case 'doc-start':
  31471. case 'doc-end':
  31472. case 'flow-seq-end':
  31473. case 'flow-map-end':
  31474. case 'map-value-ind':
  31475. yield* this.pop();
  31476. yield* this.step();
  31477. break;
  31478. case 'newline':
  31479. this.onKeyLine = false;
  31480. // fallthrough
  31481. case 'space':
  31482. case 'comment':
  31483. default:
  31484. // all other values are errors
  31485. if (token.end)
  31486. token.end.push(this.sourceToken);
  31487. else
  31488. token.end = [this.sourceToken];
  31489. if (this.type === 'newline')
  31490. yield* this.pop();
  31491. }
  31492. }
  31493. }
  31494. function parseOptions(options) {
  31495. const prettyErrors = options.prettyErrors !== false;
  31496. const lineCounter = options.lineCounter || (prettyErrors && new LineCounter()) || null;
  31497. return { lineCounter, prettyErrors };
  31498. }
  31499. /**
  31500. * Parse the input as a stream of YAML documents.
  31501. *
  31502. * Documents should be separated from each other by `...` or `---` marker lines.
  31503. *
  31504. * @returns If an empty `docs` array is returned, it will be of type
  31505. * EmptyStream and contain additional stream information. In
  31506. * TypeScript, you should use `'empty' in docs` as a type guard for it.
  31507. */
  31508. function parseAllDocuments(source, options = {}) {
  31509. const { lineCounter, prettyErrors } = parseOptions(options);
  31510. const parser = new Parser(lineCounter?.addNewLine);
  31511. const composer = new Composer(options);
  31512. const docs = Array.from(composer.compose(parser.parse(source)));
  31513. if (prettyErrors && lineCounter)
  31514. for (const doc of docs) {
  31515. doc.errors.forEach(prettifyError(source, lineCounter));
  31516. doc.warnings.forEach(prettifyError(source, lineCounter));
  31517. }
  31518. if (docs.length > 0)
  31519. return docs;
  31520. return Object.assign([], { empty: true }, composer.streamInfo());
  31521. }
  31522. /** Parse an input string into a single YAML.Document */
  31523. function parseDocument(source, options = {}) {
  31524. const { lineCounter, prettyErrors } = parseOptions(options);
  31525. const parser = new Parser(lineCounter?.addNewLine);
  31526. const composer = new Composer(options);
  31527. // `doc` is always set by compose.end(true) at the very latest
  31528. let doc = null;
  31529. for (const _doc of composer.compose(parser.parse(source), true, source.length)) {
  31530. if (!doc)
  31531. doc = _doc;
  31532. else if (doc.options.logLevel !== 'silent') {
  31533. doc.errors.push(new YAMLParseError(_doc.range.slice(0, 2), 'MULTIPLE_DOCS', 'Source contains multiple documents; please use YAML.parseAllDocuments()'));
  31534. break;
  31535. }
  31536. }
  31537. if (prettyErrors && lineCounter) {
  31538. doc.errors.forEach(prettifyError(source, lineCounter));
  31539. doc.warnings.forEach(prettifyError(source, lineCounter));
  31540. }
  31541. return doc;
  31542. }
  31543. function parse$a(src, reviver, options) {
  31544. let _reviver = undefined;
  31545. if (typeof reviver === 'function') {
  31546. _reviver = reviver;
  31547. }
  31548. else if (options === undefined && reviver && typeof reviver === 'object') {
  31549. options = reviver;
  31550. }
  31551. const doc = parseDocument(src, options);
  31552. if (!doc)
  31553. return null;
  31554. doc.warnings.forEach(warning => warn(doc.options.logLevel, warning));
  31555. if (doc.errors.length > 0) {
  31556. if (doc.options.logLevel !== 'silent')
  31557. throw doc.errors[0];
  31558. else
  31559. doc.errors = [];
  31560. }
  31561. return doc.toJS(Object.assign({ reviver: _reviver }, options));
  31562. }
  31563. function stringify(value, replacer, options) {
  31564. let _replacer = null;
  31565. if (typeof replacer === 'function' || Array.isArray(replacer)) {
  31566. _replacer = replacer;
  31567. }
  31568. else if (options === undefined && replacer) {
  31569. options = replacer;
  31570. }
  31571. if (typeof options === 'string')
  31572. options = options.length;
  31573. if (typeof options === 'number') {
  31574. const indent = Math.round(options);
  31575. options = indent < 1 ? undefined : indent > 8 ? { indent: 8 } : { indent };
  31576. }
  31577. if (value === undefined) {
  31578. const { keepUndefined } = options ?? replacer ?? {};
  31579. if (!keepUndefined)
  31580. return undefined;
  31581. }
  31582. return new Document(value, _replacer, options).toString(options);
  31583. }
  31584. var YAML = {
  31585. __proto__: null,
  31586. Alias: Alias,
  31587. CST: cst,
  31588. Composer: Composer,
  31589. Document: Document,
  31590. Lexer: Lexer,
  31591. LineCounter: LineCounter,
  31592. Pair: Pair,
  31593. Parser: Parser,
  31594. Scalar: Scalar,
  31595. Schema: Schema,
  31596. YAMLError: YAMLError,
  31597. YAMLMap: YAMLMap,
  31598. YAMLParseError: YAMLParseError,
  31599. YAMLSeq: YAMLSeq,
  31600. YAMLWarning: YAMLWarning,
  31601. isAlias: isAlias,
  31602. isCollection: isCollection$1,
  31603. isDocument: isDocument,
  31604. isMap: isMap,
  31605. isNode: isNode$1,
  31606. isPair: isPair,
  31607. isScalar: isScalar$1,
  31608. isSeq: isSeq,
  31609. parse: parse$a,
  31610. parseAllDocuments: parseAllDocuments,
  31611. parseDocument: parseDocument,
  31612. stringify: stringify,
  31613. visit: visit$1,
  31614. visitAsync: visitAsync
  31615. };
  31616. // `export * as default from ...` fails on Webpack v4
  31617. // https://github.com/eemeli/yaml/issues/228
  31618. var browser$2 = {
  31619. __proto__: null,
  31620. Alias: Alias,
  31621. CST: cst,
  31622. Composer: Composer,
  31623. Document: Document,
  31624. Lexer: Lexer,
  31625. LineCounter: LineCounter,
  31626. Pair: Pair,
  31627. Parser: Parser,
  31628. Scalar: Scalar,
  31629. Schema: Schema,
  31630. YAMLError: YAMLError,
  31631. YAMLMap: YAMLMap,
  31632. YAMLParseError: YAMLParseError,
  31633. YAMLSeq: YAMLSeq,
  31634. YAMLWarning: YAMLWarning,
  31635. default: YAML,
  31636. isAlias: isAlias,
  31637. isCollection: isCollection$1,
  31638. isDocument: isDocument,
  31639. isMap: isMap,
  31640. isNode: isNode$1,
  31641. isPair: isPair,
  31642. isScalar: isScalar$1,
  31643. isSeq: isSeq,
  31644. parse: parse$a,
  31645. parseAllDocuments: parseAllDocuments,
  31646. parseDocument: parseDocument,
  31647. stringify: stringify,
  31648. visit: visit$1,
  31649. visitAsync: visitAsync
  31650. };
  31651. var require$$3 = /*@__PURE__*/getAugmentedNamespace(browser$2);
  31652. // eslint-disable-next-line n/no-deprecated-api
  31653. const { createRequire, createRequireFromPath } = require$$0$8;
  31654. function req$2 (name, rootFile) {
  31655. const create = createRequire || createRequireFromPath;
  31656. const require = create(rootFile);
  31657. return require(name)
  31658. }
  31659. var req_1 = req$2;
  31660. const req$1 = req_1;
  31661. /**
  31662. * Load Options
  31663. *
  31664. * @private
  31665. * @method options
  31666. *
  31667. * @param {Object} config PostCSS Config
  31668. *
  31669. * @return {Object} options PostCSS Options
  31670. */
  31671. const options = (config, file) => {
  31672. if (config.parser && typeof config.parser === 'string') {
  31673. try {
  31674. config.parser = req$1(config.parser, file);
  31675. } catch (err) {
  31676. throw new Error(`Loading PostCSS Parser failed: ${err.message}\n\n(@${file})`)
  31677. }
  31678. }
  31679. if (config.syntax && typeof config.syntax === 'string') {
  31680. try {
  31681. config.syntax = req$1(config.syntax, file);
  31682. } catch (err) {
  31683. throw new Error(`Loading PostCSS Syntax failed: ${err.message}\n\n(@${file})`)
  31684. }
  31685. }
  31686. if (config.stringifier && typeof config.stringifier === 'string') {
  31687. try {
  31688. config.stringifier = req$1(config.stringifier, file);
  31689. } catch (err) {
  31690. throw new Error(`Loading PostCSS Stringifier failed: ${err.message}\n\n(@${file})`)
  31691. }
  31692. }
  31693. if (config.plugins) {
  31694. delete config.plugins;
  31695. }
  31696. return config
  31697. };
  31698. var options_1 = options;
  31699. const req = req_1;
  31700. /**
  31701. * Plugin Loader
  31702. *
  31703. * @private
  31704. * @method load
  31705. *
  31706. * @param {String} plugin PostCSS Plugin Name
  31707. * @param {Object} options PostCSS Plugin Options
  31708. *
  31709. * @return {Function} PostCSS Plugin
  31710. */
  31711. const load = (plugin, options, file) => {
  31712. try {
  31713. if (
  31714. options === null ||
  31715. options === undefined ||
  31716. Object.keys(options).length === 0
  31717. ) {
  31718. return req(plugin, file)
  31719. } else {
  31720. return req(plugin, file)(options)
  31721. }
  31722. } catch (err) {
  31723. throw new Error(`Loading PostCSS Plugin failed: ${err.message}\n\n(@${file})`)
  31724. }
  31725. };
  31726. /**
  31727. * Load Plugins
  31728. *
  31729. * @private
  31730. * @method plugins
  31731. *
  31732. * @param {Object} config PostCSS Config Plugins
  31733. *
  31734. * @return {Array} plugins PostCSS Plugins
  31735. */
  31736. const plugins = (config, file) => {
  31737. let plugins = [];
  31738. if (Array.isArray(config.plugins)) {
  31739. plugins = config.plugins.filter(Boolean);
  31740. } else {
  31741. plugins = Object.keys(config.plugins)
  31742. .filter((plugin) => {
  31743. return config.plugins[plugin] !== false ? plugin : ''
  31744. })
  31745. .map((plugin) => {
  31746. return load(plugin, config.plugins[plugin], file)
  31747. });
  31748. }
  31749. if (plugins.length && plugins.length > 0) {
  31750. plugins.forEach((plugin, i) => {
  31751. if (plugin.default) {
  31752. plugin = plugin.default;
  31753. }
  31754. if (plugin.postcss === true) {
  31755. plugin = plugin();
  31756. } else if (plugin.postcss) {
  31757. plugin = plugin.postcss;
  31758. }
  31759. if (
  31760. // eslint-disable-next-line
  31761. !(
  31762. (typeof plugin === 'object' && Array.isArray(plugin.plugins)) ||
  31763. (typeof plugin === 'object' && plugin.postcssPlugin) ||
  31764. (typeof plugin === 'function')
  31765. )
  31766. ) {
  31767. throw new TypeError(`Invalid PostCSS Plugin found at: plugins[${i}]\n\n(@${file})`)
  31768. }
  31769. });
  31770. }
  31771. return plugins
  31772. };
  31773. var plugins_1 = plugins;
  31774. const resolve = require$$0$4.resolve;
  31775. const url$4 = require$$0$9;
  31776. const config$1 = src$2;
  31777. const yaml = require$$3;
  31778. const loadOptions = options_1;
  31779. const loadPlugins = plugins_1;
  31780. /* istanbul ignore next */
  31781. const interopRequireDefault = (obj) => obj && obj.__esModule ? obj : { default: obj };
  31782. /**
  31783. * Process the result from cosmiconfig
  31784. *
  31785. * @param {Object} ctx Config Context
  31786. * @param {Object} result Cosmiconfig result
  31787. *
  31788. * @return {Object} PostCSS Config
  31789. */
  31790. const processResult = (ctx, result) => {
  31791. const file = result.filepath || '';
  31792. let config = interopRequireDefault(result.config).default || {};
  31793. if (typeof config === 'function') {
  31794. config = config(ctx);
  31795. } else {
  31796. config = Object.assign({}, config, ctx);
  31797. }
  31798. if (!config.plugins) {
  31799. config.plugins = [];
  31800. }
  31801. return {
  31802. plugins: loadPlugins(config, file),
  31803. options: loadOptions(config, file),
  31804. file
  31805. }
  31806. };
  31807. /**
  31808. * Builds the Config Context
  31809. *
  31810. * @param {Object} ctx Config Context
  31811. *
  31812. * @return {Object} Config Context
  31813. */
  31814. const createContext = (ctx) => {
  31815. /**
  31816. * @type {Object}
  31817. *
  31818. * @prop {String} cwd=process.cwd() Config search start location
  31819. * @prop {String} env=process.env.NODE_ENV Config Enviroment, will be set to `development` by `postcss-load-config` if `process.env.NODE_ENV` is `undefined`
  31820. */
  31821. ctx = Object.assign({
  31822. cwd: process.cwd(),
  31823. env: process.env.NODE_ENV
  31824. }, ctx);
  31825. if (!ctx.env) {
  31826. process.env.NODE_ENV = 'development';
  31827. }
  31828. return ctx
  31829. };
  31830. const importDefault = async filepath => {
  31831. const module = await import(url$4.pathToFileURL(filepath).href);
  31832. return module.default
  31833. };
  31834. const addTypeScriptLoader = (options = {}, loader) => {
  31835. const moduleName = 'postcss';
  31836. return {
  31837. ...options,
  31838. searchPlaces: [
  31839. ...(options.searchPlaces || []),
  31840. 'package.json',
  31841. `.${moduleName}rc`,
  31842. `.${moduleName}rc.json`,
  31843. `.${moduleName}rc.yaml`,
  31844. `.${moduleName}rc.yml`,
  31845. `.${moduleName}rc.ts`,
  31846. `.${moduleName}rc.cts`,
  31847. `.${moduleName}rc.js`,
  31848. `.${moduleName}rc.cjs`,
  31849. `.${moduleName}rc.mjs`,
  31850. `${moduleName}.config.ts`,
  31851. `${moduleName}.config.cts`,
  31852. `${moduleName}.config.js`,
  31853. `${moduleName}.config.cjs`,
  31854. `${moduleName}.config.mjs`
  31855. ],
  31856. loaders: {
  31857. ...options.loaders,
  31858. '.yaml': (filepath, content) => yaml.parse(content),
  31859. '.yml': (filepath, content) => yaml.parse(content),
  31860. '.js': importDefault,
  31861. '.cjs': importDefault,
  31862. '.mjs': importDefault,
  31863. '.ts': loader,
  31864. '.cts': loader
  31865. }
  31866. }
  31867. };
  31868. const withTypeScriptLoader = (rcFunc) => {
  31869. return (ctx, path, options) => {
  31870. return rcFunc(ctx, path, addTypeScriptLoader(options, (configFile) => {
  31871. let registerer = { enabled () {} };
  31872. try {
  31873. // Register TypeScript compiler instance
  31874. registerer = __require('ts-node').register({
  31875. // transpile to cjs even if compilerOptions.module in tsconfig is not Node16/NodeNext.
  31876. moduleTypes: { '**/*.cts': 'cjs' }
  31877. });
  31878. return __require(configFile)
  31879. } catch (err) {
  31880. if (err.code === 'MODULE_NOT_FOUND') {
  31881. throw new Error(
  31882. `'ts-node' is required for the TypeScript configuration files. Make sure it is installed\nError: ${err.message}`
  31883. )
  31884. }
  31885. throw err
  31886. } finally {
  31887. registerer.enabled(false);
  31888. }
  31889. }))
  31890. }
  31891. };
  31892. /**
  31893. * Load Config
  31894. *
  31895. * @method rc
  31896. *
  31897. * @param {Object} ctx Config Context
  31898. * @param {String} path Config Path
  31899. * @param {Object} options Config Options
  31900. *
  31901. * @return {Promise} config PostCSS Config
  31902. */
  31903. const rc = withTypeScriptLoader((ctx, path, options) => {
  31904. /**
  31905. * @type {Object} The full Config Context
  31906. */
  31907. ctx = createContext(ctx);
  31908. /**
  31909. * @type {String} `process.cwd()`
  31910. */
  31911. path = path ? resolve(path) : process.cwd();
  31912. return config$1.lilconfig('postcss', options)
  31913. .search(path)
  31914. .then((result) => {
  31915. if (!result) {
  31916. throw new Error(`No PostCSS Config found in: ${path}`)
  31917. }
  31918. return processResult(ctx, result)
  31919. })
  31920. });
  31921. /**
  31922. * Autoload Config for PostCSS
  31923. *
  31924. * @author Michael Ciniawsky @michael-ciniawsky <michael.ciniawsky@gmail.com>
  31925. * @license MIT
  31926. *
  31927. * @module postcss-load-config
  31928. * @version 2.1.0
  31929. *
  31930. * @requires comsiconfig
  31931. * @requires ./options
  31932. * @requires ./plugins
  31933. */
  31934. var src$1 = rc;
  31935. var postcssrc = /*@__PURE__*/getDefaultExportFromCjs(src$1);
  31936. // Copyright 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023 Simon Lydell
  31937. // License: MIT.
  31938. var HashbangComment, Identifier, JSXIdentifier, JSXPunctuator, JSXString, JSXText, KeywordsWithExpressionAfter, KeywordsWithNoLineTerminatorAfter, LineTerminatorSequence, MultiLineComment, Newline, NumericLiteral, Punctuator, RegularExpressionLiteral, SingleLineComment, StringLiteral, Template, TokensNotPrecedingObjectLiteral, TokensPrecedingExpression, WhiteSpace;
  31939. RegularExpressionLiteral = /\/(?![*\/])(?:\[(?:[^\]\\\n\r\u2028\u2029]+|\\.)*\]|[^\/\\\n\r\u2028\u2029]+|\\.)*(\/[$_\u200C\u200D\p{ID_Continue}]*|\\)?/yu;
  31940. Punctuator = /--|\+\+|=>|\.{3}|\??\.(?!\d)|(?:&&|\|\||\?\?|[+\-%&|^]|\*{1,2}|<{1,2}|>{1,3}|!=?|={1,2}|\/(?![\/*]))=?|[?~,:;[\](){}]/y;
  31941. Identifier = /(\x23?)(?=[$_\p{ID_Start}\\])(?:[$_\u200C\u200D\p{ID_Continue}]+|\\u[\da-fA-F]{4}|\\u\{[\da-fA-F]+\})+/yu;
  31942. StringLiteral = /(['"])(?:[^'"\\\n\r]+|(?!\1)['"]|\\(?:\r\n|[^]))*(\1)?/y;
  31943. NumericLiteral = /(?:0[xX][\da-fA-F](?:_?[\da-fA-F])*|0[oO][0-7](?:_?[0-7])*|0[bB][01](?:_?[01])*)n?|0n|[1-9](?:_?\d)*n|(?:(?:0(?!\d)|0\d*[89]\d*|[1-9](?:_?\d)*)(?:\.(?:\d(?:_?\d)*)?)?|\.\d(?:_?\d)*)(?:[eE][+-]?\d(?:_?\d)*)?|0[0-7]+/y;
  31944. Template = /[`}](?:[^`\\$]+|\\[^]|\$(?!\{))*(`|\$\{)?/y;
  31945. WhiteSpace = /[\t\v\f\ufeff\p{Zs}]+/yu;
  31946. LineTerminatorSequence = /\r?\n|[\r\u2028\u2029]/y;
  31947. MultiLineComment = /\/\*(?:[^*]+|\*(?!\/))*(\*\/)?/y;
  31948. SingleLineComment = /\/\/.*/y;
  31949. HashbangComment = /^#!.*/;
  31950. JSXPunctuator = /[<>.:={}]|\/(?![\/*])/y;
  31951. JSXIdentifier = /[$_\p{ID_Start}][$_\u200C\u200D\p{ID_Continue}-]*/yu;
  31952. JSXString = /(['"])(?:[^'"]+|(?!\1)['"])*(\1)?/y;
  31953. JSXText = /[^<>{}]+/y;
  31954. TokensPrecedingExpression = /^(?:[\/+-]|\.{3}|\?(?:InterpolationIn(?:JSX|Template)|NoLineTerminatorHere|NonExpressionParenEnd|UnaryIncDec))?$|[{}([,;<>=*%&|^!~?:]$/;
  31955. TokensNotPrecedingObjectLiteral = /^(?:=>|[;\]){}]|else|\?(?:NoLineTerminatorHere|NonExpressionParenEnd))?$/;
  31956. KeywordsWithExpressionAfter = /^(?:await|case|default|delete|do|else|instanceof|new|return|throw|typeof|void|yield)$/;
  31957. KeywordsWithNoLineTerminatorAfter = /^(?:return|throw|yield)$/;
  31958. Newline = RegExp(LineTerminatorSequence.source);
  31959. var jsTokens_1 = function*(input, {jsx = false} = {}) {
  31960. var braces, firstCodePoint, isExpression, lastIndex, lastSignificantToken, length, match, mode, nextLastIndex, nextLastSignificantToken, parenNesting, postfixIncDec, punctuator, stack;
  31961. ({length} = input);
  31962. lastIndex = 0;
  31963. lastSignificantToken = "";
  31964. stack = [
  31965. {tag: "JS"}
  31966. ];
  31967. braces = [];
  31968. parenNesting = 0;
  31969. postfixIncDec = false;
  31970. if (match = HashbangComment.exec(input)) {
  31971. yield ({
  31972. type: "HashbangComment",
  31973. value: match[0]
  31974. });
  31975. lastIndex = match[0].length;
  31976. }
  31977. while (lastIndex < length) {
  31978. mode = stack[stack.length - 1];
  31979. switch (mode.tag) {
  31980. case "JS":
  31981. case "JSNonExpressionParen":
  31982. case "InterpolationInTemplate":
  31983. case "InterpolationInJSX":
  31984. if (input[lastIndex] === "/" && (TokensPrecedingExpression.test(lastSignificantToken) || KeywordsWithExpressionAfter.test(lastSignificantToken))) {
  31985. RegularExpressionLiteral.lastIndex = lastIndex;
  31986. if (match = RegularExpressionLiteral.exec(input)) {
  31987. lastIndex = RegularExpressionLiteral.lastIndex;
  31988. lastSignificantToken = match[0];
  31989. postfixIncDec = true;
  31990. yield ({
  31991. type: "RegularExpressionLiteral",
  31992. value: match[0],
  31993. closed: match[1] !== void 0 && match[1] !== "\\"
  31994. });
  31995. continue;
  31996. }
  31997. }
  31998. Punctuator.lastIndex = lastIndex;
  31999. if (match = Punctuator.exec(input)) {
  32000. punctuator = match[0];
  32001. nextLastIndex = Punctuator.lastIndex;
  32002. nextLastSignificantToken = punctuator;
  32003. switch (punctuator) {
  32004. case "(":
  32005. if (lastSignificantToken === "?NonExpressionParenKeyword") {
  32006. stack.push({
  32007. tag: "JSNonExpressionParen",
  32008. nesting: parenNesting
  32009. });
  32010. }
  32011. parenNesting++;
  32012. postfixIncDec = false;
  32013. break;
  32014. case ")":
  32015. parenNesting--;
  32016. postfixIncDec = true;
  32017. if (mode.tag === "JSNonExpressionParen" && parenNesting === mode.nesting) {
  32018. stack.pop();
  32019. nextLastSignificantToken = "?NonExpressionParenEnd";
  32020. postfixIncDec = false;
  32021. }
  32022. break;
  32023. case "{":
  32024. Punctuator.lastIndex = 0;
  32025. isExpression = !TokensNotPrecedingObjectLiteral.test(lastSignificantToken) && (TokensPrecedingExpression.test(lastSignificantToken) || KeywordsWithExpressionAfter.test(lastSignificantToken));
  32026. braces.push(isExpression);
  32027. postfixIncDec = false;
  32028. break;
  32029. case "}":
  32030. switch (mode.tag) {
  32031. case "InterpolationInTemplate":
  32032. if (braces.length === mode.nesting) {
  32033. Template.lastIndex = lastIndex;
  32034. match = Template.exec(input);
  32035. lastIndex = Template.lastIndex;
  32036. lastSignificantToken = match[0];
  32037. if (match[1] === "${") {
  32038. lastSignificantToken = "?InterpolationInTemplate";
  32039. postfixIncDec = false;
  32040. yield ({
  32041. type: "TemplateMiddle",
  32042. value: match[0]
  32043. });
  32044. } else {
  32045. stack.pop();
  32046. postfixIncDec = true;
  32047. yield ({
  32048. type: "TemplateTail",
  32049. value: match[0],
  32050. closed: match[1] === "`"
  32051. });
  32052. }
  32053. continue;
  32054. }
  32055. break;
  32056. case "InterpolationInJSX":
  32057. if (braces.length === mode.nesting) {
  32058. stack.pop();
  32059. lastIndex += 1;
  32060. lastSignificantToken = "}";
  32061. yield ({
  32062. type: "JSXPunctuator",
  32063. value: "}"
  32064. });
  32065. continue;
  32066. }
  32067. }
  32068. postfixIncDec = braces.pop();
  32069. nextLastSignificantToken = postfixIncDec ? "?ExpressionBraceEnd" : "}";
  32070. break;
  32071. case "]":
  32072. postfixIncDec = true;
  32073. break;
  32074. case "++":
  32075. case "--":
  32076. nextLastSignificantToken = postfixIncDec ? "?PostfixIncDec" : "?UnaryIncDec";
  32077. break;
  32078. case "<":
  32079. if (jsx && (TokensPrecedingExpression.test(lastSignificantToken) || KeywordsWithExpressionAfter.test(lastSignificantToken))) {
  32080. stack.push({tag: "JSXTag"});
  32081. lastIndex += 1;
  32082. lastSignificantToken = "<";
  32083. yield ({
  32084. type: "JSXPunctuator",
  32085. value: punctuator
  32086. });
  32087. continue;
  32088. }
  32089. postfixIncDec = false;
  32090. break;
  32091. default:
  32092. postfixIncDec = false;
  32093. }
  32094. lastIndex = nextLastIndex;
  32095. lastSignificantToken = nextLastSignificantToken;
  32096. yield ({
  32097. type: "Punctuator",
  32098. value: punctuator
  32099. });
  32100. continue;
  32101. }
  32102. Identifier.lastIndex = lastIndex;
  32103. if (match = Identifier.exec(input)) {
  32104. lastIndex = Identifier.lastIndex;
  32105. nextLastSignificantToken = match[0];
  32106. switch (match[0]) {
  32107. case "for":
  32108. case "if":
  32109. case "while":
  32110. case "with":
  32111. if (lastSignificantToken !== "." && lastSignificantToken !== "?.") {
  32112. nextLastSignificantToken = "?NonExpressionParenKeyword";
  32113. }
  32114. }
  32115. lastSignificantToken = nextLastSignificantToken;
  32116. postfixIncDec = !KeywordsWithExpressionAfter.test(match[0]);
  32117. yield ({
  32118. type: match[1] === "#" ? "PrivateIdentifier" : "IdentifierName",
  32119. value: match[0]
  32120. });
  32121. continue;
  32122. }
  32123. StringLiteral.lastIndex = lastIndex;
  32124. if (match = StringLiteral.exec(input)) {
  32125. lastIndex = StringLiteral.lastIndex;
  32126. lastSignificantToken = match[0];
  32127. postfixIncDec = true;
  32128. yield ({
  32129. type: "StringLiteral",
  32130. value: match[0],
  32131. closed: match[2] !== void 0
  32132. });
  32133. continue;
  32134. }
  32135. NumericLiteral.lastIndex = lastIndex;
  32136. if (match = NumericLiteral.exec(input)) {
  32137. lastIndex = NumericLiteral.lastIndex;
  32138. lastSignificantToken = match[0];
  32139. postfixIncDec = true;
  32140. yield ({
  32141. type: "NumericLiteral",
  32142. value: match[0]
  32143. });
  32144. continue;
  32145. }
  32146. Template.lastIndex = lastIndex;
  32147. if (match = Template.exec(input)) {
  32148. lastIndex = Template.lastIndex;
  32149. lastSignificantToken = match[0];
  32150. if (match[1] === "${") {
  32151. lastSignificantToken = "?InterpolationInTemplate";
  32152. stack.push({
  32153. tag: "InterpolationInTemplate",
  32154. nesting: braces.length
  32155. });
  32156. postfixIncDec = false;
  32157. yield ({
  32158. type: "TemplateHead",
  32159. value: match[0]
  32160. });
  32161. } else {
  32162. postfixIncDec = true;
  32163. yield ({
  32164. type: "NoSubstitutionTemplate",
  32165. value: match[0],
  32166. closed: match[1] === "`"
  32167. });
  32168. }
  32169. continue;
  32170. }
  32171. break;
  32172. case "JSXTag":
  32173. case "JSXTagEnd":
  32174. JSXPunctuator.lastIndex = lastIndex;
  32175. if (match = JSXPunctuator.exec(input)) {
  32176. lastIndex = JSXPunctuator.lastIndex;
  32177. nextLastSignificantToken = match[0];
  32178. switch (match[0]) {
  32179. case "<":
  32180. stack.push({tag: "JSXTag"});
  32181. break;
  32182. case ">":
  32183. stack.pop();
  32184. if (lastSignificantToken === "/" || mode.tag === "JSXTagEnd") {
  32185. nextLastSignificantToken = "?JSX";
  32186. postfixIncDec = true;
  32187. } else {
  32188. stack.push({tag: "JSXChildren"});
  32189. }
  32190. break;
  32191. case "{":
  32192. stack.push({
  32193. tag: "InterpolationInJSX",
  32194. nesting: braces.length
  32195. });
  32196. nextLastSignificantToken = "?InterpolationInJSX";
  32197. postfixIncDec = false;
  32198. break;
  32199. case "/":
  32200. if (lastSignificantToken === "<") {
  32201. stack.pop();
  32202. if (stack[stack.length - 1].tag === "JSXChildren") {
  32203. stack.pop();
  32204. }
  32205. stack.push({tag: "JSXTagEnd"});
  32206. }
  32207. }
  32208. lastSignificantToken = nextLastSignificantToken;
  32209. yield ({
  32210. type: "JSXPunctuator",
  32211. value: match[0]
  32212. });
  32213. continue;
  32214. }
  32215. JSXIdentifier.lastIndex = lastIndex;
  32216. if (match = JSXIdentifier.exec(input)) {
  32217. lastIndex = JSXIdentifier.lastIndex;
  32218. lastSignificantToken = match[0];
  32219. yield ({
  32220. type: "JSXIdentifier",
  32221. value: match[0]
  32222. });
  32223. continue;
  32224. }
  32225. JSXString.lastIndex = lastIndex;
  32226. if (match = JSXString.exec(input)) {
  32227. lastIndex = JSXString.lastIndex;
  32228. lastSignificantToken = match[0];
  32229. yield ({
  32230. type: "JSXString",
  32231. value: match[0],
  32232. closed: match[2] !== void 0
  32233. });
  32234. continue;
  32235. }
  32236. break;
  32237. case "JSXChildren":
  32238. JSXText.lastIndex = lastIndex;
  32239. if (match = JSXText.exec(input)) {
  32240. lastIndex = JSXText.lastIndex;
  32241. lastSignificantToken = match[0];
  32242. yield ({
  32243. type: "JSXText",
  32244. value: match[0]
  32245. });
  32246. continue;
  32247. }
  32248. switch (input[lastIndex]) {
  32249. case "<":
  32250. stack.push({tag: "JSXTag"});
  32251. lastIndex++;
  32252. lastSignificantToken = "<";
  32253. yield ({
  32254. type: "JSXPunctuator",
  32255. value: "<"
  32256. });
  32257. continue;
  32258. case "{":
  32259. stack.push({
  32260. tag: "InterpolationInJSX",
  32261. nesting: braces.length
  32262. });
  32263. lastIndex++;
  32264. lastSignificantToken = "?InterpolationInJSX";
  32265. postfixIncDec = false;
  32266. yield ({
  32267. type: "JSXPunctuator",
  32268. value: "{"
  32269. });
  32270. continue;
  32271. }
  32272. }
  32273. WhiteSpace.lastIndex = lastIndex;
  32274. if (match = WhiteSpace.exec(input)) {
  32275. lastIndex = WhiteSpace.lastIndex;
  32276. yield ({
  32277. type: "WhiteSpace",
  32278. value: match[0]
  32279. });
  32280. continue;
  32281. }
  32282. LineTerminatorSequence.lastIndex = lastIndex;
  32283. if (match = LineTerminatorSequence.exec(input)) {
  32284. lastIndex = LineTerminatorSequence.lastIndex;
  32285. postfixIncDec = false;
  32286. if (KeywordsWithNoLineTerminatorAfter.test(lastSignificantToken)) {
  32287. lastSignificantToken = "?NoLineTerminatorHere";
  32288. }
  32289. yield ({
  32290. type: "LineTerminatorSequence",
  32291. value: match[0]
  32292. });
  32293. continue;
  32294. }
  32295. MultiLineComment.lastIndex = lastIndex;
  32296. if (match = MultiLineComment.exec(input)) {
  32297. lastIndex = MultiLineComment.lastIndex;
  32298. if (Newline.test(match[0])) {
  32299. postfixIncDec = false;
  32300. if (KeywordsWithNoLineTerminatorAfter.test(lastSignificantToken)) {
  32301. lastSignificantToken = "?NoLineTerminatorHere";
  32302. }
  32303. }
  32304. yield ({
  32305. type: "MultiLineComment",
  32306. value: match[0],
  32307. closed: match[1] !== void 0
  32308. });
  32309. continue;
  32310. }
  32311. SingleLineComment.lastIndex = lastIndex;
  32312. if (match = SingleLineComment.exec(input)) {
  32313. lastIndex = SingleLineComment.lastIndex;
  32314. postfixIncDec = false;
  32315. yield ({
  32316. type: "SingleLineComment",
  32317. value: match[0]
  32318. });
  32319. continue;
  32320. }
  32321. firstCodePoint = String.fromCodePoint(input.codePointAt(lastIndex));
  32322. lastIndex += firstCodePoint.length;
  32323. lastSignificantToken = firstCodePoint;
  32324. postfixIncDec = false;
  32325. yield ({
  32326. type: mode.tag.startsWith("JSX") ? "JSXInvalid" : "Invalid",
  32327. value: firstCodePoint
  32328. });
  32329. }
  32330. return void 0;
  32331. };
  32332. var jsTokens = /*@__PURE__*/getDefaultExportFromCjs(jsTokens_1);
  32333. function stripLiteralJsTokens(code, options) {
  32334. const FILL = " ";
  32335. const FILL_COMMENT = " ";
  32336. let result = "";
  32337. const tokens = [];
  32338. for (const token of jsTokens(code, { jsx: false })) {
  32339. tokens.push(token);
  32340. if (token.type === "SingleLineComment") {
  32341. result += FILL_COMMENT.repeat(token.value.length);
  32342. continue;
  32343. }
  32344. if (token.type === "MultiLineComment") {
  32345. result += token.value.replace(/[^\n]/g, FILL_COMMENT);
  32346. continue;
  32347. }
  32348. if (token.type === "StringLiteral") {
  32349. if (!token.closed) {
  32350. result += token.value;
  32351. continue;
  32352. }
  32353. const body = token.value.slice(1, -1);
  32354. {
  32355. result += token.value[0] + FILL.repeat(body.length) + token.value[token.value.length - 1];
  32356. continue;
  32357. }
  32358. }
  32359. if (token.type === "NoSubstitutionTemplate") {
  32360. const body = token.value.slice(1, -1);
  32361. {
  32362. result += `\`${body.replace(/[^\n]/g, FILL)}\``;
  32363. continue;
  32364. }
  32365. }
  32366. if (token.type === "RegularExpressionLiteral") {
  32367. const body = token.value;
  32368. {
  32369. result += body.replace(/\/(.*)\/(\w?)$/g, (_, $1, $2) => `/${FILL.repeat($1.length)}/${$2}`);
  32370. continue;
  32371. }
  32372. }
  32373. if (token.type === "TemplateHead") {
  32374. const body = token.value.slice(1, -2);
  32375. {
  32376. result += `\`${body.replace(/[^\n]/g, FILL)}\${`;
  32377. continue;
  32378. }
  32379. }
  32380. if (token.type === "TemplateTail") {
  32381. const body = token.value.slice(0, -2);
  32382. {
  32383. result += `}${body.replace(/[^\n]/g, FILL)}\``;
  32384. continue;
  32385. }
  32386. }
  32387. if (token.type === "TemplateMiddle") {
  32388. const body = token.value.slice(1, -2);
  32389. {
  32390. result += `}${body.replace(/[^\n]/g, FILL)}\${`;
  32391. continue;
  32392. }
  32393. }
  32394. result += token.value;
  32395. }
  32396. return {
  32397. result,
  32398. tokens
  32399. };
  32400. }
  32401. function stripLiteral(code, options) {
  32402. return stripLiteralDetailed(code).result;
  32403. }
  32404. function stripLiteralDetailed(code, options) {
  32405. return stripLiteralJsTokens(code);
  32406. }
  32407. var main$1 = {exports: {}};
  32408. var name = "dotenv";
  32409. var version$1 = "16.4.5";
  32410. var description = "Loads environment variables from .env file";
  32411. var main = "lib/main.js";
  32412. var types = "lib/main.d.ts";
  32413. var exports = {
  32414. ".": {
  32415. types: "./lib/main.d.ts",
  32416. require: "./lib/main.js",
  32417. "default": "./lib/main.js"
  32418. },
  32419. "./config": "./config.js",
  32420. "./config.js": "./config.js",
  32421. "./lib/env-options": "./lib/env-options.js",
  32422. "./lib/env-options.js": "./lib/env-options.js",
  32423. "./lib/cli-options": "./lib/cli-options.js",
  32424. "./lib/cli-options.js": "./lib/cli-options.js",
  32425. "./package.json": "./package.json"
  32426. };
  32427. var scripts = {
  32428. "dts-check": "tsc --project tests/types/tsconfig.json",
  32429. lint: "standard",
  32430. "lint-readme": "standard-markdown",
  32431. pretest: "npm run lint && npm run dts-check",
  32432. test: "tap tests/*.js --100 -Rspec",
  32433. "test:coverage": "tap --coverage-report=lcov",
  32434. prerelease: "npm test",
  32435. release: "standard-version"
  32436. };
  32437. var repository = {
  32438. type: "git",
  32439. url: "git://github.com/motdotla/dotenv.git"
  32440. };
  32441. var funding = "https://dotenvx.com";
  32442. var keywords = [
  32443. "dotenv",
  32444. "env",
  32445. ".env",
  32446. "environment",
  32447. "variables",
  32448. "config",
  32449. "settings"
  32450. ];
  32451. var readmeFilename = "README.md";
  32452. var license = "BSD-2-Clause";
  32453. var devDependencies = {
  32454. "@definitelytyped/dtslint": "^0.0.133",
  32455. "@types/node": "^18.11.3",
  32456. decache: "^4.6.1",
  32457. sinon: "^14.0.1",
  32458. standard: "^17.0.0",
  32459. "standard-markdown": "^7.1.0",
  32460. "standard-version": "^9.5.0",
  32461. tap: "^16.3.0",
  32462. tar: "^6.1.11",
  32463. typescript: "^4.8.4"
  32464. };
  32465. var engines = {
  32466. node: ">=12"
  32467. };
  32468. var browser$1 = {
  32469. fs: false
  32470. };
  32471. var require$$4 = {
  32472. name: name,
  32473. version: version$1,
  32474. description: description,
  32475. main: main,
  32476. types: types,
  32477. exports: exports,
  32478. scripts: scripts,
  32479. repository: repository,
  32480. funding: funding,
  32481. keywords: keywords,
  32482. readmeFilename: readmeFilename,
  32483. license: license,
  32484. devDependencies: devDependencies,
  32485. engines: engines,
  32486. browser: browser$1
  32487. };
  32488. const fs$9 = require$$0__default;
  32489. const path$9 = require$$0$4;
  32490. const os$2 = require$$2;
  32491. const crypto$1 = require$$3$1;
  32492. const packageJson = require$$4;
  32493. const version = packageJson.version;
  32494. const LINE = /(?:^|^)\s*(?:export\s+)?([\w.-]+)(?:\s*=\s*?|:\s+?)(\s*'(?:\\'|[^'])*'|\s*"(?:\\"|[^"])*"|\s*`(?:\\`|[^`])*`|[^#\r\n]+)?\s*(?:#.*)?(?:$|$)/mg;
  32495. // Parse src into an Object
  32496. function parse$9 (src) {
  32497. const obj = {};
  32498. // Convert buffer to string
  32499. let lines = src.toString();
  32500. // Convert line breaks to same format
  32501. lines = lines.replace(/\r\n?/mg, '\n');
  32502. let match;
  32503. while ((match = LINE.exec(lines)) != null) {
  32504. const key = match[1];
  32505. // Default undefined or null to empty string
  32506. let value = (match[2] || '');
  32507. // Remove whitespace
  32508. value = value.trim();
  32509. // Check if double quoted
  32510. const maybeQuote = value[0];
  32511. // Remove surrounding quotes
  32512. value = value.replace(/^(['"`])([\s\S]*)\1$/mg, '$2');
  32513. // Expand newlines if double quoted
  32514. if (maybeQuote === '"') {
  32515. value = value.replace(/\\n/g, '\n');
  32516. value = value.replace(/\\r/g, '\r');
  32517. }
  32518. // Add to object
  32519. obj[key] = value;
  32520. }
  32521. return obj
  32522. }
  32523. function _parseVault (options) {
  32524. const vaultPath = _vaultPath(options);
  32525. // Parse .env.vault
  32526. const result = DotenvModule.configDotenv({ path: vaultPath });
  32527. if (!result.parsed) {
  32528. const err = new Error(`MISSING_DATA: Cannot parse ${vaultPath} for an unknown reason`);
  32529. err.code = 'MISSING_DATA';
  32530. throw err
  32531. }
  32532. // handle scenario for comma separated keys - for use with key rotation
  32533. // example: DOTENV_KEY="dotenv://:key_1234@dotenvx.com/vault/.env.vault?environment=prod,dotenv://:key_7890@dotenvx.com/vault/.env.vault?environment=prod"
  32534. const keys = _dotenvKey(options).split(',');
  32535. const length = keys.length;
  32536. let decrypted;
  32537. for (let i = 0; i < length; i++) {
  32538. try {
  32539. // Get full key
  32540. const key = keys[i].trim();
  32541. // Get instructions for decrypt
  32542. const attrs = _instructions(result, key);
  32543. // Decrypt
  32544. decrypted = DotenvModule.decrypt(attrs.ciphertext, attrs.key);
  32545. break
  32546. } catch (error) {
  32547. // last key
  32548. if (i + 1 >= length) {
  32549. throw error
  32550. }
  32551. // try next key
  32552. }
  32553. }
  32554. // Parse decrypted .env string
  32555. return DotenvModule.parse(decrypted)
  32556. }
  32557. function _log (message) {
  32558. console.log(`[dotenv@${version}][INFO] ${message}`);
  32559. }
  32560. function _warn (message) {
  32561. console.log(`[dotenv@${version}][WARN] ${message}`);
  32562. }
  32563. function _debug (message) {
  32564. console.log(`[dotenv@${version}][DEBUG] ${message}`);
  32565. }
  32566. function _dotenvKey (options) {
  32567. // prioritize developer directly setting options.DOTENV_KEY
  32568. if (options && options.DOTENV_KEY && options.DOTENV_KEY.length > 0) {
  32569. return options.DOTENV_KEY
  32570. }
  32571. // secondary infra already contains a DOTENV_KEY environment variable
  32572. if (process.env.DOTENV_KEY && process.env.DOTENV_KEY.length > 0) {
  32573. return process.env.DOTENV_KEY
  32574. }
  32575. // fallback to empty string
  32576. return ''
  32577. }
  32578. function _instructions (result, dotenvKey) {
  32579. // Parse DOTENV_KEY. Format is a URI
  32580. let uri;
  32581. try {
  32582. uri = new URL(dotenvKey);
  32583. } catch (error) {
  32584. if (error.code === 'ERR_INVALID_URL') {
  32585. const err = new Error('INVALID_DOTENV_KEY: Wrong format. Must be in valid uri format like dotenv://:key_1234@dotenvx.com/vault/.env.vault?environment=development');
  32586. err.code = 'INVALID_DOTENV_KEY';
  32587. throw err
  32588. }
  32589. throw error
  32590. }
  32591. // Get decrypt key
  32592. const key = uri.password;
  32593. if (!key) {
  32594. const err = new Error('INVALID_DOTENV_KEY: Missing key part');
  32595. err.code = 'INVALID_DOTENV_KEY';
  32596. throw err
  32597. }
  32598. // Get environment
  32599. const environment = uri.searchParams.get('environment');
  32600. if (!environment) {
  32601. const err = new Error('INVALID_DOTENV_KEY: Missing environment part');
  32602. err.code = 'INVALID_DOTENV_KEY';
  32603. throw err
  32604. }
  32605. // Get ciphertext payload
  32606. const environmentKey = `DOTENV_VAULT_${environment.toUpperCase()}`;
  32607. const ciphertext = result.parsed[environmentKey]; // DOTENV_VAULT_PRODUCTION
  32608. if (!ciphertext) {
  32609. const err = new Error(`NOT_FOUND_DOTENV_ENVIRONMENT: Cannot locate environment ${environmentKey} in your .env.vault file.`);
  32610. err.code = 'NOT_FOUND_DOTENV_ENVIRONMENT';
  32611. throw err
  32612. }
  32613. return { ciphertext, key }
  32614. }
  32615. function _vaultPath (options) {
  32616. let possibleVaultPath = null;
  32617. if (options && options.path && options.path.length > 0) {
  32618. if (Array.isArray(options.path)) {
  32619. for (const filepath of options.path) {
  32620. if (fs$9.existsSync(filepath)) {
  32621. possibleVaultPath = filepath.endsWith('.vault') ? filepath : `${filepath}.vault`;
  32622. }
  32623. }
  32624. } else {
  32625. possibleVaultPath = options.path.endsWith('.vault') ? options.path : `${options.path}.vault`;
  32626. }
  32627. } else {
  32628. possibleVaultPath = path$9.resolve(process.cwd(), '.env.vault');
  32629. }
  32630. if (fs$9.existsSync(possibleVaultPath)) {
  32631. return possibleVaultPath
  32632. }
  32633. return null
  32634. }
  32635. function _resolveHome (envPath) {
  32636. return envPath[0] === '~' ? path$9.join(os$2.homedir(), envPath.slice(1)) : envPath
  32637. }
  32638. function _configVault (options) {
  32639. _log('Loading env from encrypted .env.vault');
  32640. const parsed = DotenvModule._parseVault(options);
  32641. let processEnv = process.env;
  32642. if (options && options.processEnv != null) {
  32643. processEnv = options.processEnv;
  32644. }
  32645. DotenvModule.populate(processEnv, parsed, options);
  32646. return { parsed }
  32647. }
  32648. function configDotenv (options) {
  32649. const dotenvPath = path$9.resolve(process.cwd(), '.env');
  32650. let encoding = 'utf8';
  32651. const debug = Boolean(options && options.debug);
  32652. if (options && options.encoding) {
  32653. encoding = options.encoding;
  32654. } else {
  32655. if (debug) {
  32656. _debug('No encoding is specified. UTF-8 is used by default');
  32657. }
  32658. }
  32659. let optionPaths = [dotenvPath]; // default, look for .env
  32660. if (options && options.path) {
  32661. if (!Array.isArray(options.path)) {
  32662. optionPaths = [_resolveHome(options.path)];
  32663. } else {
  32664. optionPaths = []; // reset default
  32665. for (const filepath of options.path) {
  32666. optionPaths.push(_resolveHome(filepath));
  32667. }
  32668. }
  32669. }
  32670. // Build the parsed data in a temporary object (because we need to return it). Once we have the final
  32671. // parsed data, we will combine it with process.env (or options.processEnv if provided).
  32672. let lastError;
  32673. const parsedAll = {};
  32674. for (const path of optionPaths) {
  32675. try {
  32676. // Specifying an encoding returns a string instead of a buffer
  32677. const parsed = DotenvModule.parse(fs$9.readFileSync(path, { encoding }));
  32678. DotenvModule.populate(parsedAll, parsed, options);
  32679. } catch (e) {
  32680. if (debug) {
  32681. _debug(`Failed to load ${path} ${e.message}`);
  32682. }
  32683. lastError = e;
  32684. }
  32685. }
  32686. let processEnv = process.env;
  32687. if (options && options.processEnv != null) {
  32688. processEnv = options.processEnv;
  32689. }
  32690. DotenvModule.populate(processEnv, parsedAll, options);
  32691. if (lastError) {
  32692. return { parsed: parsedAll, error: lastError }
  32693. } else {
  32694. return { parsed: parsedAll }
  32695. }
  32696. }
  32697. // Populates process.env from .env file
  32698. function config (options) {
  32699. // fallback to original dotenv if DOTENV_KEY is not set
  32700. if (_dotenvKey(options).length === 0) {
  32701. return DotenvModule.configDotenv(options)
  32702. }
  32703. const vaultPath = _vaultPath(options);
  32704. // dotenvKey exists but .env.vault file does not exist
  32705. if (!vaultPath) {
  32706. _warn(`You set DOTENV_KEY but you are missing a .env.vault file at ${vaultPath}. Did you forget to build it?`);
  32707. return DotenvModule.configDotenv(options)
  32708. }
  32709. return DotenvModule._configVault(options)
  32710. }
  32711. function decrypt (encrypted, keyStr) {
  32712. const key = Buffer.from(keyStr.slice(-64), 'hex');
  32713. let ciphertext = Buffer.from(encrypted, 'base64');
  32714. const nonce = ciphertext.subarray(0, 12);
  32715. const authTag = ciphertext.subarray(-16);
  32716. ciphertext = ciphertext.subarray(12, -16);
  32717. try {
  32718. const aesgcm = crypto$1.createDecipheriv('aes-256-gcm', key, nonce);
  32719. aesgcm.setAuthTag(authTag);
  32720. return `${aesgcm.update(ciphertext)}${aesgcm.final()}`
  32721. } catch (error) {
  32722. const isRange = error instanceof RangeError;
  32723. const invalidKeyLength = error.message === 'Invalid key length';
  32724. const decryptionFailed = error.message === 'Unsupported state or unable to authenticate data';
  32725. if (isRange || invalidKeyLength) {
  32726. const err = new Error('INVALID_DOTENV_KEY: It must be 64 characters long (or more)');
  32727. err.code = 'INVALID_DOTENV_KEY';
  32728. throw err
  32729. } else if (decryptionFailed) {
  32730. const err = new Error('DECRYPTION_FAILED: Please check your DOTENV_KEY');
  32731. err.code = 'DECRYPTION_FAILED';
  32732. throw err
  32733. } else {
  32734. throw error
  32735. }
  32736. }
  32737. }
  32738. // Populate process.env with parsed values
  32739. function populate (processEnv, parsed, options = {}) {
  32740. const debug = Boolean(options && options.debug);
  32741. const override = Boolean(options && options.override);
  32742. if (typeof parsed !== 'object') {
  32743. const err = new Error('OBJECT_REQUIRED: Please check the processEnv argument being passed to populate');
  32744. err.code = 'OBJECT_REQUIRED';
  32745. throw err
  32746. }
  32747. // Set process.env
  32748. for (const key of Object.keys(parsed)) {
  32749. if (Object.prototype.hasOwnProperty.call(processEnv, key)) {
  32750. if (override === true) {
  32751. processEnv[key] = parsed[key];
  32752. }
  32753. if (debug) {
  32754. if (override === true) {
  32755. _debug(`"${key}" is already defined and WAS overwritten`);
  32756. } else {
  32757. _debug(`"${key}" is already defined and was NOT overwritten`);
  32758. }
  32759. }
  32760. } else {
  32761. processEnv[key] = parsed[key];
  32762. }
  32763. }
  32764. }
  32765. const DotenvModule = {
  32766. configDotenv,
  32767. _configVault,
  32768. _parseVault,
  32769. config,
  32770. decrypt,
  32771. parse: parse$9,
  32772. populate
  32773. };
  32774. main$1.exports.configDotenv = DotenvModule.configDotenv;
  32775. main$1.exports._configVault = DotenvModule._configVault;
  32776. main$1.exports._parseVault = DotenvModule._parseVault;
  32777. main$1.exports.config = DotenvModule.config;
  32778. main$1.exports.decrypt = DotenvModule.decrypt;
  32779. var parse_1$1 = main$1.exports.parse = DotenvModule.parse;
  32780. main$1.exports.populate = DotenvModule.populate;
  32781. main$1.exports = DotenvModule;
  32782. // * /
  32783. // * (\\)? # is it escaped with a backslash?
  32784. // * (\$) # literal $
  32785. // * (?!\() # shouldnt be followed by parenthesis
  32786. // * (\{?) # first brace wrap opening
  32787. // * ([\w.]+) # key
  32788. // * (?::-((?:\$\{(?:\$\{(?:\$\{[^}]*\}|[^}])*}|[^}])*}|[^}])+))? # optional default nested 3 times
  32789. // * (\}?) # last brace warp closing
  32790. // * /xi
  32791. const DOTENV_SUBSTITUTION_REGEX = /(\\)?(\$)(?!\()(\{?)([\w.]+)(?::?-((?:\$\{(?:\$\{(?:\$\{[^}]*\}|[^}])*}|[^}])*}|[^}])+))?(\}?)/gi;
  32792. function _resolveEscapeSequences (value) {
  32793. return value.replace(/\\\$/g, '$')
  32794. }
  32795. function interpolate (value, processEnv, parsed) {
  32796. return value.replace(DOTENV_SUBSTITUTION_REGEX, (match, escaped, dollarSign, openBrace, key, defaultValue, closeBrace) => {
  32797. if (escaped === '\\') {
  32798. return match.slice(1)
  32799. } else {
  32800. if (processEnv[key]) {
  32801. if (processEnv[key] === parsed[key]) {
  32802. return processEnv[key]
  32803. } else {
  32804. // scenario: PASSWORD_EXPAND_NESTED=${PASSWORD_EXPAND}
  32805. return interpolate(processEnv[key], processEnv, parsed)
  32806. }
  32807. }
  32808. if (parsed[key]) {
  32809. // avoid recursion from EXPAND_SELF=$EXPAND_SELF
  32810. if (parsed[key] === value) {
  32811. return parsed[key]
  32812. } else {
  32813. return interpolate(parsed[key], processEnv, parsed)
  32814. }
  32815. }
  32816. if (defaultValue) {
  32817. if (defaultValue.startsWith('$')) {
  32818. return interpolate(defaultValue, processEnv, parsed)
  32819. } else {
  32820. return defaultValue
  32821. }
  32822. }
  32823. return ''
  32824. }
  32825. })
  32826. }
  32827. function expand (options) {
  32828. let processEnv = process.env;
  32829. if (options && options.processEnv != null) {
  32830. processEnv = options.processEnv;
  32831. }
  32832. for (const key in options.parsed) {
  32833. let value = options.parsed[key];
  32834. const inProcessEnv = Object.prototype.hasOwnProperty.call(processEnv, key);
  32835. if (inProcessEnv) {
  32836. if (processEnv[key] === options.parsed[key]) {
  32837. // assume was set to processEnv from the .env file if the values match and therefore interpolate
  32838. value = interpolate(value, processEnv, options.parsed);
  32839. } else {
  32840. // do not interpolate - assume processEnv had the intended value even if containing a $.
  32841. value = processEnv[key];
  32842. }
  32843. } else {
  32844. // not inProcessEnv so assume interpolation for this .env key
  32845. value = interpolate(value, processEnv, options.parsed);
  32846. }
  32847. options.parsed[key] = _resolveEscapeSequences(value);
  32848. }
  32849. for (const processKey in options.parsed) {
  32850. processEnv[processKey] = options.parsed[processKey];
  32851. }
  32852. return options
  32853. }
  32854. var expand_1 = expand;
  32855. function getEnvFilesForMode(mode, envDir) {
  32856. return [
  32857. /** default file */
  32858. `.env`,
  32859. /** local file */
  32860. `.env.local`,
  32861. /** mode file */
  32862. `.env.${mode}`,
  32863. /** mode local file */
  32864. `.env.${mode}.local`
  32865. ].map((file) => normalizePath$3(path$n.join(envDir, file)));
  32866. }
  32867. function loadEnv(mode, envDir, prefixes = "VITE_") {
  32868. if (mode === "local") {
  32869. throw new Error(
  32870. `"local" cannot be used as a mode name because it conflicts with the .local postfix for .env files.`
  32871. );
  32872. }
  32873. prefixes = arraify(prefixes);
  32874. const env = {};
  32875. const envFiles = getEnvFilesForMode(mode, envDir);
  32876. const parsed = Object.fromEntries(
  32877. envFiles.flatMap((filePath) => {
  32878. if (!tryStatSync(filePath)?.isFile()) return [];
  32879. return Object.entries(parse_1$1(fs__default.readFileSync(filePath)));
  32880. })
  32881. );
  32882. if (parsed.NODE_ENV && process.env.VITE_USER_NODE_ENV === void 0) {
  32883. process.env.VITE_USER_NODE_ENV = parsed.NODE_ENV;
  32884. }
  32885. if (parsed.BROWSER && process.env.BROWSER === void 0) {
  32886. process.env.BROWSER = parsed.BROWSER;
  32887. }
  32888. if (parsed.BROWSER_ARGS && process.env.BROWSER_ARGS === void 0) {
  32889. process.env.BROWSER_ARGS = parsed.BROWSER_ARGS;
  32890. }
  32891. const processEnv = { ...process.env };
  32892. expand_1({ parsed, processEnv });
  32893. for (const [key, value] of Object.entries(parsed)) {
  32894. if (prefixes.some((prefix) => key.startsWith(prefix))) {
  32895. env[key] = value;
  32896. }
  32897. }
  32898. for (const key in process.env) {
  32899. if (prefixes.some((prefix) => key.startsWith(prefix))) {
  32900. env[key] = process.env[key];
  32901. }
  32902. }
  32903. return env;
  32904. }
  32905. function resolveEnvPrefix({
  32906. envPrefix = "VITE_"
  32907. }) {
  32908. envPrefix = arraify(envPrefix);
  32909. if (envPrefix.includes("")) {
  32910. throw new Error(
  32911. `envPrefix option contains value '', which could lead unexpected exposure of sensitive information.`
  32912. );
  32913. }
  32914. return envPrefix;
  32915. }
  32916. const modulePreloadPolyfillId = "vite/modulepreload-polyfill";
  32917. const resolvedModulePreloadPolyfillId = "\0" + modulePreloadPolyfillId + ".js";
  32918. function modulePreloadPolyfillPlugin(config) {
  32919. const skip = config.command !== "build" || config.build.ssr;
  32920. let polyfillString;
  32921. return {
  32922. name: "vite:modulepreload-polyfill",
  32923. resolveId(id) {
  32924. if (id === modulePreloadPolyfillId) {
  32925. return resolvedModulePreloadPolyfillId;
  32926. }
  32927. },
  32928. load(id) {
  32929. if (id === resolvedModulePreloadPolyfillId) {
  32930. if (skip) {
  32931. return "";
  32932. }
  32933. if (!polyfillString) {
  32934. polyfillString = `${isModernFlag}&&(${polyfill.toString()}());`;
  32935. }
  32936. return { code: polyfillString, moduleSideEffects: true };
  32937. }
  32938. }
  32939. };
  32940. }
  32941. function polyfill() {
  32942. const relList = document.createElement("link").relList;
  32943. if (relList && relList.supports && relList.supports("modulepreload")) {
  32944. return;
  32945. }
  32946. for (const link of document.querySelectorAll('link[rel="modulepreload"]')) {
  32947. processPreload(link);
  32948. }
  32949. new MutationObserver((mutations) => {
  32950. for (const mutation of mutations) {
  32951. if (mutation.type !== "childList") {
  32952. continue;
  32953. }
  32954. for (const node of mutation.addedNodes) {
  32955. if (node.tagName === "LINK" && node.rel === "modulepreload")
  32956. processPreload(node);
  32957. }
  32958. }
  32959. }).observe(document, { childList: true, subtree: true });
  32960. function getFetchOpts(link) {
  32961. const fetchOpts = {};
  32962. if (link.integrity) fetchOpts.integrity = link.integrity;
  32963. if (link.referrerPolicy) fetchOpts.referrerPolicy = link.referrerPolicy;
  32964. if (link.crossOrigin === "use-credentials")
  32965. fetchOpts.credentials = "include";
  32966. else if (link.crossOrigin === "anonymous") fetchOpts.credentials = "omit";
  32967. else fetchOpts.credentials = "same-origin";
  32968. return fetchOpts;
  32969. }
  32970. function processPreload(link) {
  32971. if (link.ep)
  32972. return;
  32973. link.ep = true;
  32974. const fetchOpts = getFetchOpts(link);
  32975. fetch(link.href, fetchOpts);
  32976. }
  32977. }
  32978. const htmlProxyRE$1 = /\?html-proxy=?(?:&inline-css)?(?:&style-attr)?&index=(\d+)\.(?:js|css)$/;
  32979. const isHtmlProxyRE = /\?html-proxy\b/;
  32980. const inlineCSSRE$1 = /__VITE_INLINE_CSS__([a-z\d]{8}_\d+)__/g;
  32981. const inlineImportRE = /(?<!(?<!\.\.)\.)\bimport\s*\(("(?:[^"]|(?<=\\)")*"|'(?:[^']|(?<=\\)')*')\)/dg;
  32982. const htmlLangRE = /\.(?:html|htm)$/;
  32983. const spaceRe = /[\t\n\f\r ]/;
  32984. const importMapRE = /[ \t]*<script[^>]*type\s*=\s*(?:"importmap"|'importmap'|importmap)[^>]*>.*?<\/script>/is;
  32985. const moduleScriptRE = /[ \t]*<script[^>]*type\s*=\s*(?:"module"|'module'|module)[^>]*>/i;
  32986. const modulePreloadLinkRE = /[ \t]*<link[^>]*rel\s*=\s*(?:"modulepreload"|'modulepreload'|modulepreload)[\s\S]*?\/>/i;
  32987. const importMapAppendRE = new RegExp(
  32988. [moduleScriptRE, modulePreloadLinkRE].map((r) => r.source).join("|"),
  32989. "i"
  32990. );
  32991. const isHTMLProxy = (id) => isHtmlProxyRE.test(id);
  32992. const isHTMLRequest = (request) => htmlLangRE.test(request);
  32993. const htmlProxyMap = /* @__PURE__ */ new WeakMap();
  32994. const htmlProxyResult = /* @__PURE__ */ new Map();
  32995. function htmlInlineProxyPlugin(config) {
  32996. htmlProxyMap.set(config, /* @__PURE__ */ new Map());
  32997. return {
  32998. name: "vite:html-inline-proxy",
  32999. resolveId(id) {
  33000. if (isHTMLProxy(id)) {
  33001. return id;
  33002. }
  33003. },
  33004. load(id) {
  33005. const proxyMatch = htmlProxyRE$1.exec(id);
  33006. if (proxyMatch) {
  33007. const index = Number(proxyMatch[1]);
  33008. const file = cleanUrl(id);
  33009. const url = file.replace(normalizePath$3(config.root), "");
  33010. const result = htmlProxyMap.get(config).get(url)?.[index];
  33011. if (result) {
  33012. return result;
  33013. } else {
  33014. throw new Error(`No matching HTML proxy module found from ${id}`);
  33015. }
  33016. }
  33017. }
  33018. };
  33019. }
  33020. function addToHTMLProxyCache(config, filePath, index, result) {
  33021. if (!htmlProxyMap.get(config)) {
  33022. htmlProxyMap.set(config, /* @__PURE__ */ new Map());
  33023. }
  33024. if (!htmlProxyMap.get(config).get(filePath)) {
  33025. htmlProxyMap.get(config).set(filePath, []);
  33026. }
  33027. htmlProxyMap.get(config).get(filePath)[index] = result;
  33028. }
  33029. function addToHTMLProxyTransformResult(hash, code) {
  33030. htmlProxyResult.set(hash, code);
  33031. }
  33032. const assetAttrsConfig = {
  33033. link: ["href"],
  33034. video: ["src", "poster"],
  33035. source: ["src", "srcset"],
  33036. img: ["src", "srcset"],
  33037. image: ["xlink:href", "href"],
  33038. use: ["xlink:href", "href"]
  33039. };
  33040. const noInlineLinkRels = /* @__PURE__ */ new Set([
  33041. "icon",
  33042. "apple-touch-icon",
  33043. "apple-touch-startup-image",
  33044. "manifest"
  33045. ]);
  33046. const isAsyncScriptMap = /* @__PURE__ */ new WeakMap();
  33047. function nodeIsElement(node) {
  33048. return node.nodeName[0] !== "#";
  33049. }
  33050. function traverseNodes(node, visitor) {
  33051. if (node.nodeName === "template") {
  33052. node = node.content;
  33053. }
  33054. visitor(node);
  33055. if (nodeIsElement(node) || node.nodeName === "#document" || node.nodeName === "#document-fragment") {
  33056. node.childNodes.forEach((childNode) => traverseNodes(childNode, visitor));
  33057. }
  33058. }
  33059. async function traverseHtml(html, filePath, visitor) {
  33060. const { parse } = await import('./dep-D-7KCb9p.js');
  33061. const ast = parse(html, {
  33062. scriptingEnabled: false,
  33063. // parse inside <noscript>
  33064. sourceCodeLocationInfo: true,
  33065. onParseError: (e) => {
  33066. handleParseError(e, html, filePath);
  33067. }
  33068. });
  33069. traverseNodes(ast, visitor);
  33070. }
  33071. function getScriptInfo(node) {
  33072. let src;
  33073. let sourceCodeLocation;
  33074. let isModule = false;
  33075. let isAsync = false;
  33076. for (const p of node.attrs) {
  33077. if (p.prefix !== void 0) continue;
  33078. if (p.name === "src") {
  33079. if (!src) {
  33080. src = p;
  33081. sourceCodeLocation = node.sourceCodeLocation?.attrs["src"];
  33082. }
  33083. } else if (p.name === "type" && p.value && p.value === "module") {
  33084. isModule = true;
  33085. } else if (p.name === "async") {
  33086. isAsync = true;
  33087. }
  33088. }
  33089. return { src, sourceCodeLocation, isModule, isAsync };
  33090. }
  33091. const attrValueStartRE = /=\s*(.)/;
  33092. function overwriteAttrValue(s, sourceCodeLocation, newValue) {
  33093. const srcString = s.slice(
  33094. sourceCodeLocation.startOffset,
  33095. sourceCodeLocation.endOffset
  33096. );
  33097. const valueStart = attrValueStartRE.exec(srcString);
  33098. if (!valueStart) {
  33099. throw new Error(
  33100. `[vite:html] internal error, failed to overwrite attribute value`
  33101. );
  33102. }
  33103. const wrapOffset = valueStart[1] === '"' || valueStart[1] === "'" ? 1 : 0;
  33104. const valueOffset = valueStart.index + valueStart[0].length - 1;
  33105. s.update(
  33106. sourceCodeLocation.startOffset + valueOffset + wrapOffset,
  33107. sourceCodeLocation.endOffset - wrapOffset,
  33108. newValue
  33109. );
  33110. return s;
  33111. }
  33112. function formatParseError(parserError, id, html) {
  33113. const formattedError = {
  33114. code: parserError.code,
  33115. message: `parse5 error code ${parserError.code}`,
  33116. frame: generateCodeFrame(
  33117. html,
  33118. parserError.startOffset,
  33119. parserError.endOffset
  33120. ),
  33121. loc: {
  33122. file: id,
  33123. line: parserError.startLine,
  33124. column: parserError.startCol
  33125. }
  33126. };
  33127. return formattedError;
  33128. }
  33129. function handleParseError(parserError, html, filePath) {
  33130. switch (parserError.code) {
  33131. case "missing-doctype":
  33132. return;
  33133. case "abandoned-head-element-child":
  33134. return;
  33135. case "duplicate-attribute":
  33136. return;
  33137. case "non-void-html-element-start-tag-with-trailing-solidus":
  33138. return;
  33139. }
  33140. const parseError = formatParseError(parserError, filePath, html);
  33141. throw new Error(
  33142. `Unable to parse HTML; ${parseError.message}
  33143. at ${parseError.loc.file}:${parseError.loc.line}:${parseError.loc.column}
  33144. ${parseError.frame}`
  33145. );
  33146. }
  33147. function buildHtmlPlugin(config) {
  33148. const [preHooks, normalHooks, postHooks] = resolveHtmlTransforms(
  33149. config.plugins,
  33150. config.logger
  33151. );
  33152. preHooks.unshift(injectCspNonceMetaTagHook(config));
  33153. preHooks.unshift(preImportMapHook(config));
  33154. preHooks.push(htmlEnvHook(config));
  33155. postHooks.push(injectNonceAttributeTagHook(config));
  33156. postHooks.push(postImportMapHook());
  33157. const processedHtml = /* @__PURE__ */ new Map();
  33158. const isExcludedUrl = (url) => url[0] === "#" || isExternalUrl(url) || isDataUrl(url);
  33159. isAsyncScriptMap.set(config, /* @__PURE__ */ new Map());
  33160. return {
  33161. name: "vite:build-html",
  33162. async transform(html, id) {
  33163. if (id.endsWith(".html")) {
  33164. id = normalizePath$3(id);
  33165. const relativeUrlPath = normalizePath$3(path$n.relative(config.root, id));
  33166. const publicPath = `/${relativeUrlPath}`;
  33167. const publicBase = getBaseInHTML(relativeUrlPath, config);
  33168. const publicToRelative = (filename, importer) => publicBase + filename;
  33169. const toOutputPublicFilePath = (url) => toOutputFilePathInHtml(
  33170. url.slice(1),
  33171. "public",
  33172. relativeUrlPath,
  33173. "html",
  33174. config,
  33175. publicToRelative
  33176. );
  33177. const nodeStartWithLeadingWhitespace = (node) => {
  33178. const startOffset = node.sourceCodeLocation.startOffset;
  33179. if (startOffset === 0) return 0;
  33180. const lineStartOffset = startOffset - node.sourceCodeLocation.startCol;
  33181. let isLineEmpty = false;
  33182. try {
  33183. const line = s.slice(Math.max(0, lineStartOffset), startOffset);
  33184. isLineEmpty = !line.trim();
  33185. } catch {
  33186. }
  33187. return isLineEmpty ? lineStartOffset : startOffset;
  33188. };
  33189. html = await applyHtmlTransforms(html, preHooks, {
  33190. path: publicPath,
  33191. filename: id
  33192. });
  33193. let js = "";
  33194. const s = new MagicString(html);
  33195. const scriptUrls = [];
  33196. const styleUrls = [];
  33197. let inlineModuleIndex = -1;
  33198. let everyScriptIsAsync = true;
  33199. let someScriptsAreAsync = false;
  33200. let someScriptsAreDefer = false;
  33201. const assetUrlsPromises = [];
  33202. const namedOutput = Object.keys(
  33203. config?.build?.rollupOptions?.input || {}
  33204. );
  33205. const processAssetUrl = async (url, shouldInline) => {
  33206. if (url !== "" && // Empty attribute
  33207. !namedOutput.includes(url) && // Direct reference to named output
  33208. !namedOutput.includes(removeLeadingSlash(url))) {
  33209. try {
  33210. return await urlToBuiltUrl(url, id, config, this, shouldInline);
  33211. } catch (e) {
  33212. if (e.code !== "ENOENT") {
  33213. throw e;
  33214. }
  33215. }
  33216. }
  33217. return url;
  33218. };
  33219. await traverseHtml(html, id, (node) => {
  33220. if (!nodeIsElement(node)) {
  33221. return;
  33222. }
  33223. let shouldRemove = false;
  33224. if (node.nodeName === "script") {
  33225. const { src, sourceCodeLocation, isModule, isAsync } = getScriptInfo(node);
  33226. const url = src && src.value;
  33227. const isPublicFile = !!(url && checkPublicFile(url, config));
  33228. if (isPublicFile) {
  33229. overwriteAttrValue(
  33230. s,
  33231. sourceCodeLocation,
  33232. partialEncodeURIPath(toOutputPublicFilePath(url))
  33233. );
  33234. }
  33235. if (isModule) {
  33236. inlineModuleIndex++;
  33237. if (url && !isExcludedUrl(url) && !isPublicFile) {
  33238. js += `
  33239. import ${JSON.stringify(url)}`;
  33240. shouldRemove = true;
  33241. } else if (node.childNodes.length) {
  33242. const scriptNode = node.childNodes.pop();
  33243. const contents = scriptNode.value;
  33244. const filePath = id.replace(normalizePath$3(config.root), "");
  33245. addToHTMLProxyCache(config, filePath, inlineModuleIndex, {
  33246. code: contents
  33247. });
  33248. js += `
  33249. import "${id}?html-proxy&index=${inlineModuleIndex}.js"`;
  33250. shouldRemove = true;
  33251. }
  33252. everyScriptIsAsync &&= isAsync;
  33253. someScriptsAreAsync ||= isAsync;
  33254. someScriptsAreDefer ||= !isAsync;
  33255. } else if (url && !isPublicFile) {
  33256. if (!isExcludedUrl(url)) {
  33257. config.logger.warn(
  33258. `<script src="${url}"> in "${publicPath}" can't be bundled without type="module" attribute`
  33259. );
  33260. }
  33261. } else if (node.childNodes.length) {
  33262. const scriptNode = node.childNodes.pop();
  33263. scriptUrls.push(
  33264. ...extractImportExpressionFromClassicScript(scriptNode)
  33265. );
  33266. }
  33267. }
  33268. const assetAttrs = assetAttrsConfig[node.nodeName];
  33269. if (assetAttrs) {
  33270. for (const p of node.attrs) {
  33271. const attrKey = getAttrKey(p);
  33272. if (p.value && assetAttrs.includes(attrKey)) {
  33273. if (attrKey === "srcset") {
  33274. assetUrlsPromises.push(
  33275. (async () => {
  33276. const processedEncodedUrl = await processSrcSet(
  33277. p.value,
  33278. async ({ url }) => {
  33279. const decodedUrl = decodeURI(url);
  33280. if (!isExcludedUrl(decodedUrl)) {
  33281. const result = await processAssetUrl(url);
  33282. return result !== decodedUrl ? encodeURIPath(result) : url;
  33283. }
  33284. return url;
  33285. }
  33286. );
  33287. if (processedEncodedUrl !== p.value) {
  33288. overwriteAttrValue(
  33289. s,
  33290. getAttrSourceCodeLocation(node, attrKey),
  33291. processedEncodedUrl
  33292. );
  33293. }
  33294. })()
  33295. );
  33296. } else {
  33297. const url = decodeURI(p.value);
  33298. if (checkPublicFile(url, config)) {
  33299. overwriteAttrValue(
  33300. s,
  33301. getAttrSourceCodeLocation(node, attrKey),
  33302. partialEncodeURIPath(toOutputPublicFilePath(url))
  33303. );
  33304. } else if (!isExcludedUrl(url)) {
  33305. if (node.nodeName === "link" && isCSSRequest(url) && // should not be converted if following attributes are present (#6748)
  33306. !node.attrs.some(
  33307. (p2) => p2.prefix === void 0 && (p2.name === "media" || p2.name === "disabled")
  33308. )) {
  33309. const importExpression = `
  33310. import ${JSON.stringify(url)}`;
  33311. styleUrls.push({
  33312. url,
  33313. start: nodeStartWithLeadingWhitespace(node),
  33314. end: node.sourceCodeLocation.endOffset
  33315. });
  33316. js += importExpression;
  33317. } else {
  33318. const isNoInlineLink = node.nodeName === "link" && node.attrs.some(
  33319. (p2) => p2.name === "rel" && parseRelAttr(p2.value).some(
  33320. (v) => noInlineLinkRels.has(v)
  33321. )
  33322. );
  33323. const shouldInline = isNoInlineLink ? false : void 0;
  33324. assetUrlsPromises.push(
  33325. (async () => {
  33326. const processedUrl = await processAssetUrl(
  33327. url,
  33328. shouldInline
  33329. );
  33330. if (processedUrl !== url) {
  33331. overwriteAttrValue(
  33332. s,
  33333. getAttrSourceCodeLocation(node, attrKey),
  33334. partialEncodeURIPath(processedUrl)
  33335. );
  33336. }
  33337. })()
  33338. );
  33339. }
  33340. }
  33341. }
  33342. }
  33343. }
  33344. }
  33345. const inlineStyle = findNeedTransformStyleAttribute(node);
  33346. if (inlineStyle) {
  33347. inlineModuleIndex++;
  33348. const code = inlineStyle.attr.value;
  33349. const filePath = id.replace(normalizePath$3(config.root), "");
  33350. addToHTMLProxyCache(config, filePath, inlineModuleIndex, { code });
  33351. js += `
  33352. import "${id}?html-proxy&inline-css&style-attr&index=${inlineModuleIndex}.css"`;
  33353. const hash = getHash(cleanUrl(id));
  33354. overwriteAttrValue(
  33355. s,
  33356. inlineStyle.location,
  33357. `__VITE_INLINE_CSS__${hash}_${inlineModuleIndex}__`
  33358. );
  33359. }
  33360. if (node.nodeName === "style" && node.childNodes.length) {
  33361. const styleNode = node.childNodes.pop();
  33362. const filePath = id.replace(normalizePath$3(config.root), "");
  33363. inlineModuleIndex++;
  33364. addToHTMLProxyCache(config, filePath, inlineModuleIndex, {
  33365. code: styleNode.value
  33366. });
  33367. js += `
  33368. import "${id}?html-proxy&inline-css&index=${inlineModuleIndex}.css"`;
  33369. const hash = getHash(cleanUrl(id));
  33370. s.update(
  33371. styleNode.sourceCodeLocation.startOffset,
  33372. styleNode.sourceCodeLocation.endOffset,
  33373. `__VITE_INLINE_CSS__${hash}_${inlineModuleIndex}__`
  33374. );
  33375. }
  33376. if (shouldRemove) {
  33377. s.remove(
  33378. nodeStartWithLeadingWhitespace(node),
  33379. node.sourceCodeLocation.endOffset
  33380. );
  33381. }
  33382. });
  33383. isAsyncScriptMap.get(config).set(id, everyScriptIsAsync);
  33384. if (someScriptsAreAsync && someScriptsAreDefer) {
  33385. config.logger.warn(
  33386. `
  33387. Mixed async and defer script modules in ${id}, output script will fallback to defer. Every script, including inline ones, need to be marked as async for your output script to be async.`
  33388. );
  33389. }
  33390. await Promise.all(assetUrlsPromises);
  33391. for (const { start, end, url } of scriptUrls) {
  33392. if (checkPublicFile(url, config)) {
  33393. s.update(
  33394. start,
  33395. end,
  33396. partialEncodeURIPath(toOutputPublicFilePath(url))
  33397. );
  33398. } else if (!isExcludedUrl(url)) {
  33399. s.update(
  33400. start,
  33401. end,
  33402. partialEncodeURIPath(await urlToBuiltUrl(url, id, config, this))
  33403. );
  33404. }
  33405. }
  33406. const resolvedStyleUrls = await Promise.all(
  33407. styleUrls.map(async (styleUrl) => ({
  33408. ...styleUrl,
  33409. resolved: await this.resolve(styleUrl.url, id)
  33410. }))
  33411. );
  33412. for (const { start, end, url, resolved } of resolvedStyleUrls) {
  33413. if (resolved == null) {
  33414. config.logger.warnOnce(
  33415. `
  33416. ${url} doesn't exist at build time, it will remain unchanged to be resolved at runtime`
  33417. );
  33418. const importExpression = `
  33419. import ${JSON.stringify(url)}`;
  33420. js = js.replace(importExpression, "");
  33421. } else {
  33422. s.remove(start, end);
  33423. }
  33424. }
  33425. processedHtml.set(id, s.toString());
  33426. const { modulePreload } = config.build;
  33427. if (modulePreload !== false && modulePreload.polyfill && (someScriptsAreAsync || someScriptsAreDefer)) {
  33428. js = `import "${modulePreloadPolyfillId}";
  33429. ${js}`;
  33430. }
  33431. return { code: js, moduleSideEffects: "no-treeshake" };
  33432. }
  33433. },
  33434. async generateBundle(options, bundle) {
  33435. const analyzedChunk = /* @__PURE__ */ new Map();
  33436. const inlineEntryChunk = /* @__PURE__ */ new Set();
  33437. const getImportedChunks = (chunk, seen = /* @__PURE__ */ new Set()) => {
  33438. const chunks = [];
  33439. chunk.imports.forEach((file) => {
  33440. const importee = bundle[file];
  33441. if (importee?.type === "chunk" && !seen.has(file)) {
  33442. seen.add(file);
  33443. chunks.push(...getImportedChunks(importee, seen));
  33444. chunks.push(importee);
  33445. }
  33446. });
  33447. return chunks;
  33448. };
  33449. const toScriptTag = (chunk, toOutputPath, isAsync) => ({
  33450. tag: "script",
  33451. attrs: {
  33452. ...isAsync ? { async: true } : {},
  33453. type: "module",
  33454. // crossorigin must be set not only for serving assets in a different origin
  33455. // but also to make it possible to preload the script using `<link rel="preload">`.
  33456. // `<script type="module">` used to fetch the script with credential mode `omit`,
  33457. // however `crossorigin` attribute cannot specify that value.
  33458. // https://developer.chrome.com/blog/modulepreload/#ok-so-why-doesnt-link-relpreload-work-for-modules:~:text=For%20%3Cscript%3E,of%20other%20modules.
  33459. // Now `<script type="module">` uses `same origin`: https://github.com/whatwg/html/pull/3656#:~:text=Module%20scripts%20are%20always%20fetched%20with%20credentials%20mode%20%22same%2Dorigin%22%20by%20default%20and%20can%20no%20longer%0Ause%20%22omit%22
  33460. crossorigin: true,
  33461. src: toOutputPath(chunk.fileName)
  33462. }
  33463. });
  33464. const toPreloadTag = (filename, toOutputPath) => ({
  33465. tag: "link",
  33466. attrs: {
  33467. rel: "modulepreload",
  33468. crossorigin: true,
  33469. href: toOutputPath(filename)
  33470. }
  33471. });
  33472. const getCssTagsForChunk = (chunk, toOutputPath, seen = /* @__PURE__ */ new Set()) => {
  33473. const tags = [];
  33474. if (!analyzedChunk.has(chunk)) {
  33475. analyzedChunk.set(chunk, 1);
  33476. chunk.imports.forEach((file) => {
  33477. const importee = bundle[file];
  33478. if (importee?.type === "chunk") {
  33479. tags.push(...getCssTagsForChunk(importee, toOutputPath, seen));
  33480. }
  33481. });
  33482. }
  33483. chunk.viteMetadata.importedCss.forEach((file) => {
  33484. if (!seen.has(file)) {
  33485. seen.add(file);
  33486. tags.push({
  33487. tag: "link",
  33488. attrs: {
  33489. rel: "stylesheet",
  33490. crossorigin: true,
  33491. href: toOutputPath(file)
  33492. }
  33493. });
  33494. }
  33495. });
  33496. return tags;
  33497. };
  33498. for (const [normalizedId, html] of processedHtml) {
  33499. const relativeUrlPath = normalizePath$3(
  33500. path$n.relative(config.root, normalizedId)
  33501. );
  33502. const assetsBase = getBaseInHTML(relativeUrlPath, config);
  33503. const toOutputFilePath = (filename, type) => {
  33504. if (isExternalUrl(filename)) {
  33505. return filename;
  33506. } else {
  33507. return toOutputFilePathInHtml(
  33508. filename,
  33509. type,
  33510. relativeUrlPath,
  33511. "html",
  33512. config,
  33513. (filename2, importer) => assetsBase + filename2
  33514. );
  33515. }
  33516. };
  33517. const toOutputAssetFilePath = (filename) => toOutputFilePath(filename, "asset");
  33518. const toOutputPublicAssetFilePath = (filename) => toOutputFilePath(filename, "public");
  33519. const isAsync = isAsyncScriptMap.get(config).get(normalizedId);
  33520. let result = html;
  33521. const chunk = Object.values(bundle).find(
  33522. (chunk2) => chunk2.type === "chunk" && chunk2.isEntry && chunk2.facadeModuleId && normalizePath$3(chunk2.facadeModuleId) === normalizedId
  33523. );
  33524. let canInlineEntry = false;
  33525. if (chunk) {
  33526. if (options.format === "es" && isEntirelyImport(chunk.code)) {
  33527. canInlineEntry = true;
  33528. }
  33529. const imports = getImportedChunks(chunk);
  33530. let assetTags;
  33531. if (canInlineEntry) {
  33532. assetTags = imports.map(
  33533. (chunk2) => toScriptTag(chunk2, toOutputAssetFilePath, isAsync)
  33534. );
  33535. } else {
  33536. assetTags = [toScriptTag(chunk, toOutputAssetFilePath, isAsync)];
  33537. const { modulePreload } = config.build;
  33538. if (modulePreload !== false) {
  33539. const resolveDependencies = typeof modulePreload === "object" && modulePreload.resolveDependencies;
  33540. const importsFileNames = imports.map((chunk2) => chunk2.fileName);
  33541. const resolvedDeps = resolveDependencies ? resolveDependencies(chunk.fileName, importsFileNames, {
  33542. hostId: relativeUrlPath,
  33543. hostType: "html"
  33544. }) : importsFileNames;
  33545. assetTags.push(
  33546. ...resolvedDeps.map(
  33547. (i) => toPreloadTag(i, toOutputAssetFilePath)
  33548. )
  33549. );
  33550. }
  33551. }
  33552. assetTags.push(...getCssTagsForChunk(chunk, toOutputAssetFilePath));
  33553. result = injectToHead(result, assetTags);
  33554. }
  33555. if (!config.build.cssCodeSplit) {
  33556. const cssChunk = Object.values(bundle).find(
  33557. (chunk2) => chunk2.type === "asset" && chunk2.name === "style.css"
  33558. );
  33559. if (cssChunk) {
  33560. result = injectToHead(result, [
  33561. {
  33562. tag: "link",
  33563. attrs: {
  33564. rel: "stylesheet",
  33565. crossorigin: true,
  33566. href: toOutputAssetFilePath(cssChunk.fileName)
  33567. }
  33568. }
  33569. ]);
  33570. }
  33571. }
  33572. let match;
  33573. let s;
  33574. inlineCSSRE$1.lastIndex = 0;
  33575. while (match = inlineCSSRE$1.exec(result)) {
  33576. s ||= new MagicString(result);
  33577. const { 0: full, 1: scopedName } = match;
  33578. const cssTransformedCode = htmlProxyResult.get(scopedName);
  33579. s.update(match.index, match.index + full.length, cssTransformedCode);
  33580. }
  33581. if (s) {
  33582. result = s.toString();
  33583. }
  33584. result = await applyHtmlTransforms(
  33585. result,
  33586. [...normalHooks, ...postHooks],
  33587. {
  33588. path: "/" + relativeUrlPath,
  33589. filename: normalizedId,
  33590. bundle,
  33591. chunk
  33592. }
  33593. );
  33594. result = result.replace(assetUrlRE, (_, fileHash, postfix = "") => {
  33595. const file = this.getFileName(fileHash);
  33596. if (chunk) {
  33597. chunk.viteMetadata.importedAssets.add(cleanUrl(file));
  33598. }
  33599. return encodeURIPath(toOutputAssetFilePath(file)) + postfix;
  33600. });
  33601. result = result.replace(publicAssetUrlRE, (_, fileHash) => {
  33602. const publicAssetPath = toOutputPublicAssetFilePath(
  33603. getPublicAssetFilename(fileHash, config)
  33604. );
  33605. return encodeURIPath(
  33606. urlCanParse(publicAssetPath) ? publicAssetPath : normalizePath$3(publicAssetPath)
  33607. );
  33608. });
  33609. if (chunk && canInlineEntry) {
  33610. inlineEntryChunk.add(chunk.fileName);
  33611. }
  33612. const shortEmitName = normalizePath$3(
  33613. path$n.relative(config.root, normalizedId)
  33614. );
  33615. this.emitFile({
  33616. type: "asset",
  33617. originalFileName: normalizedId,
  33618. fileName: shortEmitName,
  33619. source: result
  33620. });
  33621. }
  33622. for (const fileName of inlineEntryChunk) {
  33623. delete bundle[fileName];
  33624. }
  33625. }
  33626. };
  33627. }
  33628. function parseRelAttr(attr) {
  33629. return attr.split(spaceRe).map((v) => v.toLowerCase());
  33630. }
  33631. function findNeedTransformStyleAttribute(node) {
  33632. const attr = node.attrs.find(
  33633. (prop) => prop.prefix === void 0 && prop.name === "style" && // only url(...) or image-set(...) in css need to emit file
  33634. (prop.value.includes("url(") || prop.value.includes("image-set("))
  33635. );
  33636. if (!attr) return void 0;
  33637. const location = node.sourceCodeLocation?.attrs?.["style"];
  33638. return { attr, location };
  33639. }
  33640. function extractImportExpressionFromClassicScript(scriptTextNode) {
  33641. const startOffset = scriptTextNode.sourceCodeLocation.startOffset;
  33642. const cleanCode = stripLiteral(scriptTextNode.value);
  33643. const scriptUrls = [];
  33644. let match;
  33645. inlineImportRE.lastIndex = 0;
  33646. while (match = inlineImportRE.exec(cleanCode)) {
  33647. const [, [urlStart, urlEnd]] = match.indices;
  33648. const start = urlStart + 1;
  33649. const end = urlEnd - 1;
  33650. scriptUrls.push({
  33651. start: start + startOffset,
  33652. end: end + startOffset,
  33653. url: scriptTextNode.value.slice(start, end)
  33654. });
  33655. }
  33656. return scriptUrls;
  33657. }
  33658. function preImportMapHook(config) {
  33659. return (html, ctx) => {
  33660. const importMapIndex = html.search(importMapRE);
  33661. if (importMapIndex < 0) return;
  33662. const importMapAppendIndex = html.search(importMapAppendRE);
  33663. if (importMapAppendIndex < 0) return;
  33664. if (importMapAppendIndex < importMapIndex) {
  33665. const relativeHtml = normalizePath$3(
  33666. path$n.relative(config.root, ctx.filename)
  33667. );
  33668. config.logger.warnOnce(
  33669. colors$1.yellow(
  33670. colors$1.bold(
  33671. `(!) <script type="importmap"> should come before <script type="module"> and <link rel="modulepreload"> in /${relativeHtml}`
  33672. )
  33673. )
  33674. );
  33675. }
  33676. };
  33677. }
  33678. function postImportMapHook() {
  33679. return (html) => {
  33680. if (!importMapAppendRE.test(html)) return;
  33681. let importMap;
  33682. html = html.replace(importMapRE, (match) => {
  33683. importMap = match;
  33684. return "";
  33685. });
  33686. if (importMap) {
  33687. html = html.replace(
  33688. importMapAppendRE,
  33689. (match) => `${importMap}
  33690. ${match}`
  33691. );
  33692. }
  33693. return html;
  33694. };
  33695. }
  33696. function injectCspNonceMetaTagHook(config) {
  33697. return () => {
  33698. if (!config.html?.cspNonce) return;
  33699. return [
  33700. {
  33701. tag: "meta",
  33702. injectTo: "head",
  33703. // use nonce attribute so that it's hidden
  33704. // https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/nonce#accessing_nonces_and_nonce_hiding
  33705. attrs: { property: "csp-nonce", nonce: config.html.cspNonce }
  33706. }
  33707. ];
  33708. };
  33709. }
  33710. function htmlEnvHook(config) {
  33711. const pattern = /%(\S+?)%/g;
  33712. const envPrefix = resolveEnvPrefix({ envPrefix: config.envPrefix });
  33713. const env = { ...config.env };
  33714. for (const key in config.define) {
  33715. if (key.startsWith(`import.meta.env.`)) {
  33716. const val = config.define[key];
  33717. if (typeof val === "string") {
  33718. try {
  33719. const parsed = JSON.parse(val);
  33720. env[key.slice(16)] = typeof parsed === "string" ? parsed : val;
  33721. } catch {
  33722. env[key.slice(16)] = val;
  33723. }
  33724. } else {
  33725. env[key.slice(16)] = JSON.stringify(val);
  33726. }
  33727. }
  33728. }
  33729. return (html, ctx) => {
  33730. return html.replace(pattern, (text, key) => {
  33731. if (key in env) {
  33732. return env[key];
  33733. } else {
  33734. if (envPrefix.some((prefix) => key.startsWith(prefix))) {
  33735. const relativeHtml = normalizePath$3(
  33736. path$n.relative(config.root, ctx.filename)
  33737. );
  33738. config.logger.warn(
  33739. colors$1.yellow(
  33740. colors$1.bold(
  33741. `(!) ${text} is not defined in env variables found in /${relativeHtml}. Is the variable mistyped?`
  33742. )
  33743. )
  33744. );
  33745. }
  33746. return text;
  33747. }
  33748. });
  33749. };
  33750. }
  33751. function injectNonceAttributeTagHook(config) {
  33752. const processRelType = /* @__PURE__ */ new Set(["stylesheet", "modulepreload", "preload"]);
  33753. return async (html, { filename }) => {
  33754. const nonce = config.html?.cspNonce;
  33755. if (!nonce) return;
  33756. const s = new MagicString(html);
  33757. await traverseHtml(html, filename, (node) => {
  33758. if (!nodeIsElement(node)) {
  33759. return;
  33760. }
  33761. const { nodeName, attrs, sourceCodeLocation } = node;
  33762. if (nodeName === "script" || nodeName === "style" || nodeName === "link" && attrs.some(
  33763. (attr) => attr.name === "rel" && parseRelAttr(attr.value).some((a) => processRelType.has(a))
  33764. )) {
  33765. if (attrs.some(({ name }) => name === "nonce")) {
  33766. return;
  33767. }
  33768. const startTagEndOffset = sourceCodeLocation.startTag.endOffset;
  33769. const appendOffset = html[startTagEndOffset - 2] === "/" ? 2 : 1;
  33770. s.appendRight(startTagEndOffset - appendOffset, ` nonce="${nonce}"`);
  33771. }
  33772. });
  33773. return s.toString();
  33774. };
  33775. }
  33776. function resolveHtmlTransforms(plugins, logger) {
  33777. const preHooks = [];
  33778. const normalHooks = [];
  33779. const postHooks = [];
  33780. for (const plugin of plugins) {
  33781. const hook = plugin.transformIndexHtml;
  33782. if (!hook) continue;
  33783. if (typeof hook === "function") {
  33784. normalHooks.push(hook);
  33785. } else {
  33786. if (!("order" in hook) && "enforce" in hook) {
  33787. logger.warnOnce(
  33788. colors$1.yellow(
  33789. `plugin '${plugin.name}' uses deprecated 'enforce' option. Use 'order' option instead.`
  33790. )
  33791. );
  33792. }
  33793. if (!("handler" in hook) && "transform" in hook) {
  33794. logger.warnOnce(
  33795. colors$1.yellow(
  33796. `plugin '${plugin.name}' uses deprecated 'transform' option. Use 'handler' option instead.`
  33797. )
  33798. );
  33799. }
  33800. const order = hook.order ?? (hook.enforce === "pre" ? "pre" : void 0);
  33801. const handler = hook.handler ?? hook.transform;
  33802. if (order === "pre") {
  33803. preHooks.push(handler);
  33804. } else if (order === "post") {
  33805. postHooks.push(handler);
  33806. } else {
  33807. normalHooks.push(handler);
  33808. }
  33809. }
  33810. }
  33811. return [preHooks, normalHooks, postHooks];
  33812. }
  33813. const elementsAllowedInHead = /* @__PURE__ */ new Set([
  33814. "title",
  33815. "base",
  33816. "link",
  33817. "style",
  33818. "meta",
  33819. "script",
  33820. "noscript",
  33821. "template"
  33822. ]);
  33823. function headTagInsertCheck(tags, ctx) {
  33824. if (!tags.length) return;
  33825. const { logger } = ctx.server?.config || {};
  33826. const disallowedTags = tags.filter(
  33827. (tagDescriptor) => !elementsAllowedInHead.has(tagDescriptor.tag)
  33828. );
  33829. if (disallowedTags.length) {
  33830. const dedupedTags = unique(
  33831. disallowedTags.map((tagDescriptor) => `<${tagDescriptor.tag}>`)
  33832. );
  33833. logger?.warn(
  33834. colors$1.yellow(
  33835. colors$1.bold(
  33836. `[${dedupedTags.join(",")}] can not be used inside the <head> Element, please check the 'injectTo' value`
  33837. )
  33838. )
  33839. );
  33840. }
  33841. }
  33842. async function applyHtmlTransforms(html, hooks, ctx) {
  33843. for (const hook of hooks) {
  33844. const res = await hook(html, ctx);
  33845. if (!res) {
  33846. continue;
  33847. }
  33848. if (typeof res === "string") {
  33849. html = res;
  33850. } else {
  33851. let tags;
  33852. if (Array.isArray(res)) {
  33853. tags = res;
  33854. } else {
  33855. html = res.html || html;
  33856. tags = res.tags;
  33857. }
  33858. let headTags;
  33859. let headPrependTags;
  33860. let bodyTags;
  33861. let bodyPrependTags;
  33862. for (const tag of tags) {
  33863. switch (tag.injectTo) {
  33864. case "body":
  33865. (bodyTags ??= []).push(tag);
  33866. break;
  33867. case "body-prepend":
  33868. (bodyPrependTags ??= []).push(tag);
  33869. break;
  33870. case "head":
  33871. (headTags ??= []).push(tag);
  33872. break;
  33873. default:
  33874. (headPrependTags ??= []).push(tag);
  33875. }
  33876. }
  33877. headTagInsertCheck([...headTags || [], ...headPrependTags || []], ctx);
  33878. if (headPrependTags) html = injectToHead(html, headPrependTags, true);
  33879. if (headTags) html = injectToHead(html, headTags);
  33880. if (bodyPrependTags) html = injectToBody(html, bodyPrependTags, true);
  33881. if (bodyTags) html = injectToBody(html, bodyTags);
  33882. }
  33883. }
  33884. return html;
  33885. }
  33886. const importRE = /\bimport\s*(?:"[^"]*[^\\]"|'[^']*[^\\]');*/g;
  33887. const commentRE$1 = /\/\*[\s\S]*?\*\/|\/\/.*$/gm;
  33888. function isEntirelyImport(code) {
  33889. return !code.replace(importRE, "").replace(commentRE$1, "").trim().length;
  33890. }
  33891. function getBaseInHTML(urlRelativePath, config) {
  33892. return config.base === "./" || config.base === "" ? path$n.posix.join(
  33893. path$n.posix.relative(urlRelativePath, "").slice(0, -2),
  33894. "./"
  33895. ) : config.base;
  33896. }
  33897. const headInjectRE = /([ \t]*)<\/head>/i;
  33898. const headPrependInjectRE = /([ \t]*)<head[^>]*>/i;
  33899. const htmlInjectRE = /<\/html>/i;
  33900. const htmlPrependInjectRE = /([ \t]*)<html[^>]*>/i;
  33901. const bodyInjectRE = /([ \t]*)<\/body>/i;
  33902. const bodyPrependInjectRE = /([ \t]*)<body[^>]*>/i;
  33903. const doctypePrependInjectRE = /<!doctype html>/i;
  33904. function injectToHead(html, tags, prepend = false) {
  33905. if (tags.length === 0) return html;
  33906. if (prepend) {
  33907. if (headPrependInjectRE.test(html)) {
  33908. return html.replace(
  33909. headPrependInjectRE,
  33910. (match, p1) => `${match}
  33911. ${serializeTags(tags, incrementIndent(p1))}`
  33912. );
  33913. }
  33914. } else {
  33915. if (headInjectRE.test(html)) {
  33916. return html.replace(
  33917. headInjectRE,
  33918. (match, p1) => `${serializeTags(tags, incrementIndent(p1))}${match}`
  33919. );
  33920. }
  33921. if (bodyPrependInjectRE.test(html)) {
  33922. return html.replace(
  33923. bodyPrependInjectRE,
  33924. (match, p1) => `${serializeTags(tags, p1)}
  33925. ${match}`
  33926. );
  33927. }
  33928. }
  33929. return prependInjectFallback(html, tags);
  33930. }
  33931. function injectToBody(html, tags, prepend = false) {
  33932. if (tags.length === 0) return html;
  33933. if (prepend) {
  33934. if (bodyPrependInjectRE.test(html)) {
  33935. return html.replace(
  33936. bodyPrependInjectRE,
  33937. (match, p1) => `${match}
  33938. ${serializeTags(tags, incrementIndent(p1))}`
  33939. );
  33940. }
  33941. if (headInjectRE.test(html)) {
  33942. return html.replace(
  33943. headInjectRE,
  33944. (match, p1) => `${match}
  33945. ${serializeTags(tags, p1)}`
  33946. );
  33947. }
  33948. return prependInjectFallback(html, tags);
  33949. } else {
  33950. if (bodyInjectRE.test(html)) {
  33951. return html.replace(
  33952. bodyInjectRE,
  33953. (match, p1) => `${serializeTags(tags, incrementIndent(p1))}${match}`
  33954. );
  33955. }
  33956. if (htmlInjectRE.test(html)) {
  33957. return html.replace(htmlInjectRE, `${serializeTags(tags)}
  33958. $&`);
  33959. }
  33960. return html + `
  33961. ` + serializeTags(tags);
  33962. }
  33963. }
  33964. function prependInjectFallback(html, tags) {
  33965. if (htmlPrependInjectRE.test(html)) {
  33966. return html.replace(htmlPrependInjectRE, `$&
  33967. ${serializeTags(tags)}`);
  33968. }
  33969. if (doctypePrependInjectRE.test(html)) {
  33970. return html.replace(doctypePrependInjectRE, `$&
  33971. ${serializeTags(tags)}`);
  33972. }
  33973. return serializeTags(tags) + html;
  33974. }
  33975. const unaryTags = /* @__PURE__ */ new Set(["link", "meta", "base"]);
  33976. function serializeTag({ tag, attrs, children }, indent = "") {
  33977. if (unaryTags.has(tag)) {
  33978. return `<${tag}${serializeAttrs(attrs)}>`;
  33979. } else {
  33980. return `<${tag}${serializeAttrs(attrs)}>${serializeTags(
  33981. children,
  33982. incrementIndent(indent)
  33983. )}</${tag}>`;
  33984. }
  33985. }
  33986. function serializeTags(tags, indent = "") {
  33987. if (typeof tags === "string") {
  33988. return tags;
  33989. } else if (tags && tags.length) {
  33990. return tags.map((tag) => `${indent}${serializeTag(tag, indent)}
  33991. `).join("");
  33992. }
  33993. return "";
  33994. }
  33995. function serializeAttrs(attrs) {
  33996. let res = "";
  33997. for (const key in attrs) {
  33998. if (typeof attrs[key] === "boolean") {
  33999. res += attrs[key] ? ` ${key}` : ``;
  34000. } else {
  34001. res += ` ${key}=${JSON.stringify(attrs[key])}`;
  34002. }
  34003. }
  34004. return res;
  34005. }
  34006. function incrementIndent(indent = "") {
  34007. return `${indent}${indent[0] === " " ? " " : " "}`;
  34008. }
  34009. function getAttrKey(attr) {
  34010. return attr.prefix === void 0 ? attr.name : `${attr.prefix}:${attr.name}`;
  34011. }
  34012. function getAttrSourceCodeLocation(node, attrKey) {
  34013. return node.sourceCodeLocation.attrs[attrKey];
  34014. }
  34015. const decoder = new TextDecoder();
  34016. function resolveCSSOptions(options) {
  34017. if (options?.transformer === "lightningcss") {
  34018. return {
  34019. ...options,
  34020. lightningcss: {
  34021. ...options.lightningcss,
  34022. targets: options.lightningcss?.targets ?? convertTargets(ESBUILD_MODULES_TARGET)
  34023. }
  34024. };
  34025. }
  34026. return { ...options, lightningcss: void 0 };
  34027. }
  34028. const cssModuleRE = new RegExp(`\\.module${CSS_LANGS_RE.source}`);
  34029. const directRequestRE = /[?&]direct\b/;
  34030. const htmlProxyRE = /[?&]html-proxy\b/;
  34031. const htmlProxyIndexRE = /&index=(\d+)/;
  34032. const commonjsProxyRE = /\?commonjs-proxy/;
  34033. const inlineRE$1 = /[?&]inline\b/;
  34034. const inlineCSSRE = /[?&]inline-css\b/;
  34035. const styleAttrRE = /[?&]style-attr\b/;
  34036. const functionCallRE = /^[A-Z_][\w-]*\(/i;
  34037. const transformOnlyRE = /[?&]transform-only\b/;
  34038. const nonEscapedDoubleQuoteRe = /(?<!\\)"/g;
  34039. const cssBundleName = "style.css";
  34040. const isCSSRequest = (request) => CSS_LANGS_RE.test(request);
  34041. const isModuleCSSRequest = (request) => cssModuleRE.test(request);
  34042. const isDirectCSSRequest = (request) => CSS_LANGS_RE.test(request) && directRequestRE.test(request);
  34043. const isDirectRequest = (request) => directRequestRE.test(request);
  34044. const cssModulesCache = /* @__PURE__ */ new WeakMap();
  34045. const removedPureCssFilesCache = /* @__PURE__ */ new WeakMap();
  34046. const postcssConfigCache = /* @__PURE__ */ new WeakMap();
  34047. function encodePublicUrlsInCSS(config) {
  34048. return config.command === "build";
  34049. }
  34050. const cssUrlAssetRE = /__VITE_CSS_URL__([\da-f]+)__/g;
  34051. function cssPlugin(config) {
  34052. const isBuild = config.command === "build";
  34053. let moduleCache;
  34054. const resolveUrl = config.createResolver({
  34055. preferRelative: true,
  34056. tryIndex: false,
  34057. extensions: []
  34058. });
  34059. let preprocessorWorkerController;
  34060. if (config.css?.transformer !== "lightningcss") {
  34061. resolvePostcssConfig(config);
  34062. }
  34063. return {
  34064. name: "vite:css",
  34065. buildStart() {
  34066. moduleCache = /* @__PURE__ */ new Map();
  34067. cssModulesCache.set(config, moduleCache);
  34068. removedPureCssFilesCache.set(config, /* @__PURE__ */ new Map());
  34069. preprocessorWorkerController = createPreprocessorWorkerController(
  34070. normalizeMaxWorkers(config.css.preprocessorMaxWorkers)
  34071. );
  34072. preprocessorWorkerControllerCache.set(
  34073. config,
  34074. preprocessorWorkerController
  34075. );
  34076. },
  34077. buildEnd() {
  34078. preprocessorWorkerController?.close();
  34079. },
  34080. async load(id) {
  34081. if (!isCSSRequest(id)) return;
  34082. if (urlRE.test(id)) {
  34083. if (isModuleCSSRequest(id)) {
  34084. throw new Error(
  34085. `?url is not supported with CSS modules. (tried to import ${JSON.stringify(
  34086. id
  34087. )})`
  34088. );
  34089. }
  34090. if (isBuild) {
  34091. id = injectQuery(removeUrlQuery(id), "transform-only");
  34092. return `import ${JSON.stringify(id)};export default "__VITE_CSS_URL__${Buffer.from(id).toString(
  34093. "hex"
  34094. )}__"`;
  34095. }
  34096. }
  34097. },
  34098. async transform(raw, id) {
  34099. if (!isCSSRequest(id) || commonjsProxyRE.test(id) || SPECIAL_QUERY_RE.test(id)) {
  34100. return;
  34101. }
  34102. const urlReplacer = async (url, importer) => {
  34103. const decodedUrl = decodeURI(url);
  34104. if (checkPublicFile(decodedUrl, config)) {
  34105. if (encodePublicUrlsInCSS(config)) {
  34106. return publicFileToBuiltUrl(decodedUrl, config);
  34107. } else {
  34108. return joinUrlSegments(config.base, decodedUrl);
  34109. }
  34110. }
  34111. const [id2, fragment] = decodedUrl.split("#");
  34112. let resolved = await resolveUrl(id2, importer);
  34113. if (resolved) {
  34114. if (fragment) resolved += "#" + fragment;
  34115. return fileToUrl$1(resolved, config, this);
  34116. }
  34117. if (config.command === "build") {
  34118. const isExternal = config.build.rollupOptions.external ? resolveUserExternal(
  34119. config.build.rollupOptions.external,
  34120. decodedUrl,
  34121. // use URL as id since id could not be resolved
  34122. id2,
  34123. false
  34124. ) : false;
  34125. if (!isExternal) {
  34126. config.logger.warnOnce(
  34127. `
  34128. ${decodedUrl} referenced in ${id2} didn't resolve at build time, it will remain unchanged to be resolved at runtime`
  34129. );
  34130. }
  34131. }
  34132. return url;
  34133. };
  34134. const {
  34135. code: css,
  34136. modules,
  34137. deps,
  34138. map: map2
  34139. } = await compileCSS(
  34140. id,
  34141. raw,
  34142. config,
  34143. preprocessorWorkerController,
  34144. urlReplacer
  34145. );
  34146. if (modules) {
  34147. moduleCache.set(id, modules);
  34148. }
  34149. if (deps) {
  34150. for (const file of deps) {
  34151. this.addWatchFile(file);
  34152. }
  34153. }
  34154. return {
  34155. code: css,
  34156. map: map2
  34157. };
  34158. }
  34159. };
  34160. }
  34161. function cssPostPlugin(config) {
  34162. const styles = /* @__PURE__ */ new Map();
  34163. let codeSplitEmitQueue = createSerialPromiseQueue();
  34164. const urlEmitQueue = createSerialPromiseQueue();
  34165. let pureCssChunks;
  34166. let hasEmitted = false;
  34167. let chunkCSSMap;
  34168. const rollupOptionsOutput = config.build.rollupOptions.output;
  34169. const assetFileNames = (Array.isArray(rollupOptionsOutput) ? rollupOptionsOutput[0] : rollupOptionsOutput)?.assetFileNames;
  34170. const getCssAssetDirname = (cssAssetName) => {
  34171. const cssAssetNameDir = path$n.dirname(cssAssetName);
  34172. if (!assetFileNames) {
  34173. return path$n.join(config.build.assetsDir, cssAssetNameDir);
  34174. } else if (typeof assetFileNames === "string") {
  34175. return path$n.join(path$n.dirname(assetFileNames), cssAssetNameDir);
  34176. } else {
  34177. return path$n.dirname(
  34178. assetFileNames({
  34179. type: "asset",
  34180. name: cssAssetName,
  34181. originalFileName: null,
  34182. source: "/* vite internal call, ignore */"
  34183. })
  34184. );
  34185. }
  34186. };
  34187. return {
  34188. name: "vite:css-post",
  34189. renderStart() {
  34190. pureCssChunks = /* @__PURE__ */ new Set();
  34191. hasEmitted = false;
  34192. chunkCSSMap = /* @__PURE__ */ new Map();
  34193. codeSplitEmitQueue = createSerialPromiseQueue();
  34194. },
  34195. async transform(css, id, options) {
  34196. if (!isCSSRequest(id) || commonjsProxyRE.test(id) || SPECIAL_QUERY_RE.test(id)) {
  34197. return;
  34198. }
  34199. css = stripBomTag(css);
  34200. const inlineCSS = inlineCSSRE.test(id);
  34201. const isHTMLProxy = htmlProxyRE.test(id);
  34202. if (inlineCSS && isHTMLProxy) {
  34203. if (styleAttrRE.test(id)) {
  34204. css = css.replace(/"/g, "&quot;");
  34205. }
  34206. const index = htmlProxyIndexRE.exec(id)?.[1];
  34207. if (index == null) {
  34208. throw new Error(`HTML proxy index in "${id}" not found`);
  34209. }
  34210. addToHTMLProxyTransformResult(
  34211. `${getHash(cleanUrl(id))}_${Number.parseInt(index)}`,
  34212. css
  34213. );
  34214. return `export default ''`;
  34215. }
  34216. const inlined = inlineRE$1.test(id);
  34217. const modules = cssModulesCache.get(config).get(id);
  34218. const modulesCode = modules && !inlined && dataToEsm(modules, { namedExports: true, preferConst: true });
  34219. if (config.command === "serve") {
  34220. const getContentWithSourcemap = async (content) => {
  34221. if (config.css?.devSourcemap) {
  34222. const sourcemap = this.getCombinedSourcemap();
  34223. if (sourcemap.mappings) {
  34224. await injectSourcesContent(sourcemap, cleanUrl(id), config.logger);
  34225. }
  34226. return getCodeWithSourcemap("css", content, sourcemap);
  34227. }
  34228. return content;
  34229. };
  34230. if (isDirectCSSRequest(id)) {
  34231. return null;
  34232. }
  34233. if (options?.ssr) {
  34234. return modulesCode || `export default ${JSON.stringify(css)}`;
  34235. }
  34236. if (inlined) {
  34237. return `export default ${JSON.stringify(css)}`;
  34238. }
  34239. const cssContent = await getContentWithSourcemap(css);
  34240. const code2 = [
  34241. `import { updateStyle as __vite__updateStyle, removeStyle as __vite__removeStyle } from ${JSON.stringify(
  34242. path$n.posix.join(config.base, CLIENT_PUBLIC_PATH)
  34243. )}`,
  34244. `const __vite__id = ${JSON.stringify(id)}`,
  34245. `const __vite__css = ${JSON.stringify(cssContent)}`,
  34246. `__vite__updateStyle(__vite__id, __vite__css)`,
  34247. // css modules exports change on edit so it can't self accept
  34248. `${modulesCode || "import.meta.hot.accept()"}`,
  34249. `import.meta.hot.prune(() => __vite__removeStyle(__vite__id))`
  34250. ].join("\n");
  34251. return { code: code2, map: { mappings: "" } };
  34252. }
  34253. if (!inlined) {
  34254. styles.set(id, css);
  34255. }
  34256. let code;
  34257. if (modulesCode) {
  34258. code = modulesCode;
  34259. } else if (inlined) {
  34260. let content = css;
  34261. if (config.build.cssMinify) {
  34262. content = await minifyCSS(content, config, true);
  34263. }
  34264. code = `export default ${JSON.stringify(content)}`;
  34265. } else {
  34266. code = "";
  34267. }
  34268. return {
  34269. code,
  34270. map: { mappings: "" },
  34271. // avoid the css module from being tree-shaken so that we can retrieve
  34272. // it in renderChunk()
  34273. moduleSideEffects: modulesCode || inlined ? false : "no-treeshake"
  34274. };
  34275. },
  34276. async renderChunk(code, chunk, opts) {
  34277. let chunkCSS = "";
  34278. const isJsChunkEmpty = code === "" && !chunk.isEntry;
  34279. let isPureCssChunk = true;
  34280. const ids = Object.keys(chunk.modules);
  34281. for (const id of ids) {
  34282. if (styles.has(id)) {
  34283. if (!transformOnlyRE.test(id)) {
  34284. chunkCSS += styles.get(id);
  34285. if (cssModuleRE.test(id)) {
  34286. isPureCssChunk = false;
  34287. }
  34288. }
  34289. } else if (!isJsChunkEmpty) {
  34290. isPureCssChunk = false;
  34291. }
  34292. }
  34293. const publicAssetUrlMap = publicAssetUrlCache.get(config);
  34294. const resolveAssetUrlsInCss = (chunkCSS2, cssAssetName) => {
  34295. const encodedPublicUrls = encodePublicUrlsInCSS(config);
  34296. const relative = config.base === "./" || config.base === "";
  34297. const cssAssetDirname = encodedPublicUrls || relative ? slash$1(getCssAssetDirname(cssAssetName)) : void 0;
  34298. const toRelative = (filename) => {
  34299. const relativePath = normalizePath$3(
  34300. path$n.relative(cssAssetDirname, filename)
  34301. );
  34302. return relativePath[0] === "." ? relativePath : "./" + relativePath;
  34303. };
  34304. chunkCSS2 = chunkCSS2.replace(assetUrlRE, (_, fileHash, postfix = "") => {
  34305. const filename = this.getFileName(fileHash) + postfix;
  34306. chunk.viteMetadata.importedAssets.add(cleanUrl(filename));
  34307. return encodeURIPath(
  34308. toOutputFilePathInCss(
  34309. filename,
  34310. "asset",
  34311. cssAssetName,
  34312. "css",
  34313. config,
  34314. toRelative
  34315. )
  34316. );
  34317. });
  34318. if (encodedPublicUrls) {
  34319. const relativePathToPublicFromCSS = normalizePath$3(
  34320. path$n.relative(cssAssetDirname, "")
  34321. );
  34322. chunkCSS2 = chunkCSS2.replace(publicAssetUrlRE, (_, hash) => {
  34323. const publicUrl = publicAssetUrlMap.get(hash).slice(1);
  34324. return encodeURIPath(
  34325. toOutputFilePathInCss(
  34326. publicUrl,
  34327. "public",
  34328. cssAssetName,
  34329. "css",
  34330. config,
  34331. () => `${relativePathToPublicFromCSS}/${publicUrl}`
  34332. )
  34333. );
  34334. });
  34335. }
  34336. return chunkCSS2;
  34337. };
  34338. function ensureFileExt(name, ext) {
  34339. return normalizePath$3(
  34340. path$n.format({ ...path$n.parse(name), base: void 0, ext })
  34341. );
  34342. }
  34343. let s;
  34344. const urlEmitTasks = [];
  34345. if (code.includes("__VITE_CSS_URL__")) {
  34346. let match;
  34347. cssUrlAssetRE.lastIndex = 0;
  34348. while (match = cssUrlAssetRE.exec(code)) {
  34349. const [full, idHex] = match;
  34350. const id = Buffer.from(idHex, "hex").toString();
  34351. const originalFileName = cleanUrl(id);
  34352. const cssAssetName = ensureFileExt(
  34353. path$n.basename(originalFileName),
  34354. ".css"
  34355. );
  34356. if (!styles.has(id)) {
  34357. throw new Error(
  34358. `css content for ${JSON.stringify(id)} was not found`
  34359. );
  34360. }
  34361. let cssContent = styles.get(id);
  34362. cssContent = resolveAssetUrlsInCss(cssContent, cssAssetName);
  34363. urlEmitTasks.push({
  34364. cssAssetName,
  34365. originalFileName,
  34366. content: cssContent,
  34367. start: match.index,
  34368. end: match.index + full.length
  34369. });
  34370. }
  34371. }
  34372. await urlEmitQueue.run(
  34373. async () => Promise.all(
  34374. urlEmitTasks.map(async (info) => {
  34375. info.content = await finalizeCss(info.content, true, config);
  34376. })
  34377. )
  34378. );
  34379. if (urlEmitTasks.length > 0) {
  34380. const toRelativeRuntime = createToImportMetaURLBasedRelativeRuntime(
  34381. opts.format,
  34382. config.isWorker
  34383. );
  34384. s ||= new MagicString(code);
  34385. for (const {
  34386. cssAssetName,
  34387. originalFileName,
  34388. content,
  34389. start,
  34390. end
  34391. } of urlEmitTasks) {
  34392. const referenceId = this.emitFile({
  34393. type: "asset",
  34394. name: cssAssetName,
  34395. originalFileName,
  34396. source: content
  34397. });
  34398. generatedAssets.get(config).set(referenceId, { originalFileName });
  34399. const filename = this.getFileName(referenceId);
  34400. chunk.viteMetadata.importedAssets.add(cleanUrl(filename));
  34401. const replacement = toOutputFilePathInJS(
  34402. filename,
  34403. "asset",
  34404. chunk.fileName,
  34405. "js",
  34406. config,
  34407. toRelativeRuntime
  34408. );
  34409. const replacementString = typeof replacement === "string" ? JSON.stringify(encodeURIPath(replacement)).slice(1, -1) : `"+${replacement.runtime}+"`;
  34410. s.update(start, end, replacementString);
  34411. }
  34412. }
  34413. if (chunkCSS) {
  34414. if (isPureCssChunk && (opts.format === "es" || opts.format === "cjs")) {
  34415. pureCssChunks.add(chunk);
  34416. }
  34417. if (config.build.cssCodeSplit) {
  34418. if (opts.format === "es" || opts.format === "cjs") {
  34419. const isEntry = chunk.isEntry && isPureCssChunk;
  34420. const cssFullAssetName = ensureFileExt(chunk.name, ".css");
  34421. const cssAssetName = chunk.isEntry && (!chunk.facadeModuleId || !isCSSRequest(chunk.facadeModuleId)) ? path$n.basename(cssFullAssetName) : cssFullAssetName;
  34422. const originalFileName = getChunkOriginalFileName(
  34423. chunk,
  34424. config.root,
  34425. opts.format
  34426. );
  34427. chunkCSS = resolveAssetUrlsInCss(chunkCSS, cssAssetName);
  34428. chunkCSS = await codeSplitEmitQueue.run(async () => {
  34429. return finalizeCss(chunkCSS, true, config);
  34430. });
  34431. const referenceId = this.emitFile({
  34432. type: "asset",
  34433. name: cssAssetName,
  34434. originalFileName,
  34435. source: chunkCSS
  34436. });
  34437. generatedAssets.get(config).set(referenceId, { originalFileName, isEntry });
  34438. chunk.viteMetadata.importedCss.add(this.getFileName(referenceId));
  34439. } else if (!config.build.ssr) {
  34440. chunkCSS = await finalizeCss(chunkCSS, true, config);
  34441. let cssString = JSON.stringify(chunkCSS);
  34442. cssString = renderAssetUrlInJS(
  34443. this,
  34444. config,
  34445. chunk,
  34446. opts,
  34447. cssString
  34448. )?.toString() || cssString;
  34449. const style = `__vite_style__`;
  34450. const injectCode = `var ${style} = document.createElement('style');${style}.textContent = ${cssString};document.head.appendChild(${style});`;
  34451. let injectionPoint;
  34452. const wrapIdx = code.indexOf("System.register");
  34453. if (wrapIdx >= 0) {
  34454. const executeFnStart = code.indexOf("execute:", wrapIdx);
  34455. injectionPoint = code.indexOf("{", executeFnStart) + 1;
  34456. } else {
  34457. const insertMark = "'use strict';";
  34458. injectionPoint = code.indexOf(insertMark) + insertMark.length;
  34459. }
  34460. s ||= new MagicString(code);
  34461. s.appendRight(injectionPoint, injectCode);
  34462. }
  34463. } else {
  34464. chunkCSS = resolveAssetUrlsInCss(chunkCSS, cssBundleName);
  34465. chunkCSSMap.set(chunk.fileName, chunkCSS);
  34466. }
  34467. }
  34468. if (s) {
  34469. if (config.build.sourcemap) {
  34470. return {
  34471. code: s.toString(),
  34472. map: s.generateMap({ hires: "boundary" })
  34473. };
  34474. } else {
  34475. return { code: s.toString() };
  34476. }
  34477. }
  34478. return null;
  34479. },
  34480. augmentChunkHash(chunk) {
  34481. if (chunk.viteMetadata?.importedCss.size) {
  34482. let hash = "";
  34483. for (const id of chunk.viteMetadata.importedCss) {
  34484. hash += id;
  34485. }
  34486. return hash;
  34487. }
  34488. },
  34489. async generateBundle(opts, bundle) {
  34490. if (opts.__vite_skip_asset_emit__) {
  34491. return;
  34492. }
  34493. function extractCss() {
  34494. let css = "";
  34495. const collected = /* @__PURE__ */ new Set();
  34496. const dynamicImports = /* @__PURE__ */ new Set();
  34497. function collect(chunk) {
  34498. if (!chunk || chunk.type !== "chunk" || collected.has(chunk)) return;
  34499. collected.add(chunk);
  34500. chunk.imports.forEach((importName) => collect(bundle[importName]));
  34501. chunk.dynamicImports.forEach(
  34502. (importName) => dynamicImports.add(importName)
  34503. );
  34504. css += chunkCSSMap.get(chunk.preliminaryFileName) ?? "";
  34505. }
  34506. for (const chunk of Object.values(bundle)) {
  34507. if (chunk.type === "chunk" && chunk.isEntry) {
  34508. collect(chunk);
  34509. }
  34510. }
  34511. for (const chunkName of dynamicImports) {
  34512. collect(bundle[chunkName]);
  34513. }
  34514. return css;
  34515. }
  34516. let extractedCss = !hasEmitted && extractCss();
  34517. if (extractedCss) {
  34518. hasEmitted = true;
  34519. extractedCss = await finalizeCss(extractedCss, true, config);
  34520. this.emitFile({
  34521. name: cssBundleName,
  34522. type: "asset",
  34523. source: extractedCss
  34524. });
  34525. }
  34526. if (pureCssChunks.size) {
  34527. const prelimaryNameToChunkMap = Object.fromEntries(
  34528. Object.values(bundle).filter((chunk) => chunk.type === "chunk").map((chunk) => [chunk.preliminaryFileName, chunk.fileName])
  34529. );
  34530. const pureCssChunkNames = [...pureCssChunks].map((pureCssChunk) => prelimaryNameToChunkMap[pureCssChunk.fileName]).filter(Boolean);
  34531. const replaceEmptyChunk = getEmptyChunkReplacer(
  34532. pureCssChunkNames,
  34533. opts.format
  34534. );
  34535. for (const file in bundle) {
  34536. const chunk = bundle[file];
  34537. if (chunk.type === "chunk") {
  34538. let chunkImportsPureCssChunk = false;
  34539. chunk.imports = chunk.imports.filter((file2) => {
  34540. if (pureCssChunkNames.includes(file2)) {
  34541. const { importedCss, importedAssets } = bundle[file2].viteMetadata;
  34542. importedCss.forEach(
  34543. (file3) => chunk.viteMetadata.importedCss.add(file3)
  34544. );
  34545. importedAssets.forEach(
  34546. (file3) => chunk.viteMetadata.importedAssets.add(file3)
  34547. );
  34548. chunkImportsPureCssChunk = true;
  34549. return false;
  34550. }
  34551. return true;
  34552. });
  34553. if (chunkImportsPureCssChunk) {
  34554. chunk.code = replaceEmptyChunk(chunk.code);
  34555. }
  34556. }
  34557. }
  34558. const removedPureCssFiles = removedPureCssFilesCache.get(config);
  34559. pureCssChunkNames.forEach((fileName) => {
  34560. removedPureCssFiles.set(fileName, bundle[fileName]);
  34561. delete bundle[fileName];
  34562. delete bundle[`${fileName}.map`];
  34563. });
  34564. }
  34565. const cssAssets = Object.values(bundle).filter(
  34566. (asset) => asset.type === "asset" && asset.fileName.endsWith(".css")
  34567. );
  34568. for (const cssAsset of cssAssets) {
  34569. if (typeof cssAsset.source === "string") {
  34570. cssAsset.source = cssAsset.source.replace(viteHashUpdateMarkerRE, "");
  34571. }
  34572. }
  34573. }
  34574. };
  34575. }
  34576. function cssAnalysisPlugin(config) {
  34577. let server;
  34578. return {
  34579. name: "vite:css-analysis",
  34580. configureServer(_server) {
  34581. server = _server;
  34582. },
  34583. async transform(_, id, options) {
  34584. if (!isCSSRequest(id) || commonjsProxyRE.test(id) || SPECIAL_QUERY_RE.test(id)) {
  34585. return;
  34586. }
  34587. const ssr = options?.ssr === true;
  34588. const { moduleGraph } = server;
  34589. const thisModule = moduleGraph.getModuleById(id);
  34590. if (thisModule) {
  34591. const isSelfAccepting = !cssModulesCache.get(config)?.get(id) && !inlineRE$1.test(id) && !htmlProxyRE.test(id);
  34592. const pluginImports = this._addedImports;
  34593. if (pluginImports) {
  34594. const depModules = /* @__PURE__ */ new Set();
  34595. for (const file of pluginImports) {
  34596. depModules.add(
  34597. isCSSRequest(file) ? moduleGraph.createFileOnlyEntry(file) : await moduleGraph.ensureEntryFromUrl(
  34598. fileToDevUrl(
  34599. file,
  34600. config,
  34601. /* skipBase */
  34602. true
  34603. ),
  34604. ssr
  34605. )
  34606. );
  34607. }
  34608. moduleGraph.updateModuleInfo(
  34609. thisModule,
  34610. depModules,
  34611. null,
  34612. // The root CSS proxy module is self-accepting and should not
  34613. // have an explicit accept list
  34614. /* @__PURE__ */ new Set(),
  34615. null,
  34616. isSelfAccepting,
  34617. ssr
  34618. );
  34619. } else {
  34620. thisModule.isSelfAccepting = isSelfAccepting;
  34621. }
  34622. }
  34623. }
  34624. };
  34625. }
  34626. function getEmptyChunkReplacer(pureCssChunkNames, outputFormat) {
  34627. const emptyChunkFiles = pureCssChunkNames.map((file) => path$n.basename(file)).join("|").replace(/\./g, "\\.");
  34628. const emptyChunkRE = new RegExp(
  34629. outputFormat === "es" ? `\\bimport\\s*["'][^"']*(?:${emptyChunkFiles})["'];` : `(\\b|,\\s*)require\\(\\s*["'][^"']*(?:${emptyChunkFiles})["']\\)(;|,)`,
  34630. "g"
  34631. );
  34632. return (code) => code.replace(
  34633. emptyChunkRE,
  34634. // remove css import while preserving source map location
  34635. (m) => outputFormat === "es" ? `/* empty css ${"".padEnd(m.length - 15)}*/` : `${m.at(-1)}/* empty css ${"".padEnd(m.length - 16)}*/`
  34636. );
  34637. }
  34638. function createCSSResolvers(config) {
  34639. let cssResolve;
  34640. let sassResolve;
  34641. let lessResolve;
  34642. return {
  34643. get css() {
  34644. return cssResolve || (cssResolve = config.createResolver({
  34645. extensions: [".css"],
  34646. mainFields: ["style"],
  34647. conditions: ["style"],
  34648. tryIndex: false,
  34649. preferRelative: true
  34650. }));
  34651. },
  34652. get sass() {
  34653. if (!sassResolve) {
  34654. const resolver = config.createResolver({
  34655. extensions: [".scss", ".sass", ".css"],
  34656. mainFields: ["sass", "style"],
  34657. conditions: ["sass", "style"],
  34658. tryIndex: true,
  34659. tryPrefix: "_",
  34660. preferRelative: true
  34661. });
  34662. sassResolve = async (...args) => {
  34663. if (args[0].startsWith("file://")) {
  34664. args[0] = fileURLToPath(args[0]);
  34665. }
  34666. return resolver(...args);
  34667. };
  34668. }
  34669. return sassResolve;
  34670. },
  34671. get less() {
  34672. return lessResolve || (lessResolve = config.createResolver({
  34673. extensions: [".less", ".css"],
  34674. mainFields: ["less", "style"],
  34675. conditions: ["less", "style"],
  34676. tryIndex: false,
  34677. preferRelative: true
  34678. }));
  34679. }
  34680. };
  34681. }
  34682. function getCssResolversKeys(resolvers) {
  34683. return Object.keys(resolvers);
  34684. }
  34685. async function compileCSSPreprocessors(id, lang, code, config, workerController) {
  34686. const { preprocessorOptions, devSourcemap } = config.css ?? {};
  34687. const atImportResolvers = getAtImportResolvers(config);
  34688. const preProcessor = workerController[lang];
  34689. let opts = preprocessorOptions && preprocessorOptions[lang] || {};
  34690. switch (lang) {
  34691. case "scss" /* scss */:
  34692. case "sass" /* sass */:
  34693. opts = {
  34694. includePaths: ["node_modules"],
  34695. alias: config.resolve.alias,
  34696. ...opts
  34697. };
  34698. break;
  34699. case "less" /* less */:
  34700. case "styl" /* styl */:
  34701. case "stylus" /* stylus */:
  34702. opts = {
  34703. paths: ["node_modules"],
  34704. alias: config.resolve.alias,
  34705. ...opts
  34706. };
  34707. }
  34708. opts.filename = cleanUrl(id);
  34709. opts.enableSourcemap = devSourcemap ?? false;
  34710. const preprocessResult = await preProcessor(
  34711. code,
  34712. config.root,
  34713. opts,
  34714. atImportResolvers
  34715. );
  34716. if (preprocessResult.error) {
  34717. throw preprocessResult.error;
  34718. }
  34719. let deps;
  34720. if (preprocessResult.deps) {
  34721. const normalizedFilename = normalizePath$3(opts.filename);
  34722. deps = new Set(
  34723. [...preprocessResult.deps].filter(
  34724. (dep) => normalizePath$3(dep) !== normalizedFilename
  34725. )
  34726. );
  34727. }
  34728. return {
  34729. code: preprocessResult.code,
  34730. map: combineSourcemapsIfExists(
  34731. opts.filename,
  34732. preprocessResult.map,
  34733. preprocessResult.additionalMap
  34734. ),
  34735. deps
  34736. };
  34737. }
  34738. const configToAtImportResolvers = /* @__PURE__ */ new WeakMap();
  34739. function getAtImportResolvers(config) {
  34740. let atImportResolvers = configToAtImportResolvers.get(config);
  34741. if (!atImportResolvers) {
  34742. atImportResolvers = createCSSResolvers(config);
  34743. configToAtImportResolvers.set(config, atImportResolvers);
  34744. }
  34745. return atImportResolvers;
  34746. }
  34747. async function compileCSS(id, code, config, workerController, urlReplacer) {
  34748. if (config.css?.transformer === "lightningcss") {
  34749. return compileLightningCSS(id, code, config, urlReplacer);
  34750. }
  34751. const { modules: modulesOptions, devSourcemap } = config.css || {};
  34752. const isModule = modulesOptions !== false && cssModuleRE.test(id);
  34753. const needInlineImport = code.includes("@import");
  34754. const hasUrl = cssUrlRE.test(code) || cssImageSetRE.test(code);
  34755. const lang = CSS_LANGS_RE.exec(id)?.[1];
  34756. const postcssConfig = await resolvePostcssConfig(config);
  34757. if (lang === "css" && !postcssConfig && !isModule && !needInlineImport && !hasUrl) {
  34758. return { code, map: null };
  34759. }
  34760. let modules;
  34761. const deps = /* @__PURE__ */ new Set();
  34762. let preprocessorMap;
  34763. if (isPreProcessor(lang)) {
  34764. const preprocessorResult = await compileCSSPreprocessors(
  34765. id,
  34766. lang,
  34767. code,
  34768. config,
  34769. workerController
  34770. );
  34771. code = preprocessorResult.code;
  34772. preprocessorMap = preprocessorResult.map;
  34773. preprocessorResult.deps?.forEach((dep) => deps.add(dep));
  34774. }
  34775. const atImportResolvers = getAtImportResolvers(config);
  34776. const postcssOptions = postcssConfig && postcssConfig.options || {};
  34777. const postcssPlugins = postcssConfig && postcssConfig.plugins ? postcssConfig.plugins.slice() : [];
  34778. if (needInlineImport) {
  34779. postcssPlugins.unshift(
  34780. (await importPostcssImport()).default({
  34781. async resolve(id2, basedir) {
  34782. const publicFile = checkPublicFile(id2, config);
  34783. if (publicFile) {
  34784. return publicFile;
  34785. }
  34786. const resolved = await atImportResolvers.css(
  34787. id2,
  34788. path$n.join(basedir, "*")
  34789. );
  34790. if (resolved) {
  34791. return path$n.resolve(resolved);
  34792. }
  34793. if (!path$n.isAbsolute(id2)) {
  34794. config.logger.error(
  34795. colors$1.red(
  34796. `Unable to resolve \`@import "${id2}"\` from ${basedir}`
  34797. )
  34798. );
  34799. }
  34800. return id2;
  34801. },
  34802. async load(id2) {
  34803. const code2 = await fs__default.promises.readFile(id2, "utf-8");
  34804. const lang2 = CSS_LANGS_RE.exec(id2)?.[1];
  34805. if (isPreProcessor(lang2)) {
  34806. const result = await compileCSSPreprocessors(
  34807. id2,
  34808. lang2,
  34809. code2,
  34810. config,
  34811. workerController
  34812. );
  34813. result.deps?.forEach((dep) => deps.add(dep));
  34814. return result.code;
  34815. }
  34816. return code2;
  34817. },
  34818. nameLayer(index) {
  34819. return `vite--anon-layer-${getHash(id)}-${index}`;
  34820. }
  34821. })
  34822. );
  34823. }
  34824. if (urlReplacer) {
  34825. postcssPlugins.push(
  34826. UrlRewritePostcssPlugin({
  34827. replacer: urlReplacer,
  34828. logger: config.logger
  34829. })
  34830. );
  34831. }
  34832. if (isModule) {
  34833. postcssPlugins.unshift(
  34834. (await importPostcssModules()).default({
  34835. ...modulesOptions,
  34836. localsConvention: modulesOptions?.localsConvention,
  34837. getJSON(cssFileName, _modules, outputFileName) {
  34838. modules = _modules;
  34839. if (modulesOptions && typeof modulesOptions.getJSON === "function") {
  34840. modulesOptions.getJSON(cssFileName, _modules, outputFileName);
  34841. }
  34842. },
  34843. async resolve(id2, importer) {
  34844. for (const key of getCssResolversKeys(atImportResolvers)) {
  34845. const resolved = await atImportResolvers[key](id2, importer);
  34846. if (resolved) {
  34847. return path$n.resolve(resolved);
  34848. }
  34849. }
  34850. return id2;
  34851. }
  34852. })
  34853. );
  34854. }
  34855. if (!postcssPlugins.length) {
  34856. return {
  34857. code,
  34858. map: preprocessorMap,
  34859. deps
  34860. };
  34861. }
  34862. let postcssResult;
  34863. try {
  34864. const source = removeDirectQuery(id);
  34865. const postcss = await importPostcss();
  34866. postcssResult = await postcss.default(postcssPlugins).process(code, {
  34867. ...postcssOptions,
  34868. parser: lang === "sss" ? loadSss(config.root) : postcssOptions.parser,
  34869. to: source,
  34870. from: source,
  34871. ...devSourcemap ? {
  34872. map: {
  34873. inline: false,
  34874. annotation: false,
  34875. // postcss may return virtual files
  34876. // we cannot obtain content of them, so this needs to be enabled
  34877. sourcesContent: true
  34878. // when "prev: preprocessorMap", the result map may include duplicate filename in `postcssResult.map.sources`
  34879. // prev: preprocessorMap,
  34880. }
  34881. } : {}
  34882. });
  34883. for (const message of postcssResult.messages) {
  34884. if (message.type === "dependency") {
  34885. deps.add(normalizePath$3(message.file));
  34886. } else if (message.type === "dir-dependency") {
  34887. const { dir, glob: globPattern = "**" } = message;
  34888. const pattern = glob.escapePath(normalizePath$3(path$n.resolve(path$n.dirname(id), dir))) + `/` + globPattern;
  34889. const files = glob.sync(pattern, {
  34890. ignore: ["**/node_modules/**"]
  34891. });
  34892. for (let i = 0; i < files.length; i++) {
  34893. deps.add(files[i]);
  34894. }
  34895. } else if (message.type === "warning") {
  34896. const warning = message;
  34897. let msg = `[vite:css] ${warning.text}`;
  34898. msg += `
  34899. ${generateCodeFrame(
  34900. code,
  34901. {
  34902. line: warning.line,
  34903. column: warning.column - 1
  34904. // 1-based
  34905. },
  34906. warning.endLine !== void 0 && warning.endColumn !== void 0 ? {
  34907. line: warning.endLine,
  34908. column: warning.endColumn - 1
  34909. // 1-based
  34910. } : void 0
  34911. )}`;
  34912. config.logger.warn(colors$1.yellow(msg));
  34913. }
  34914. }
  34915. } catch (e) {
  34916. e.message = `[postcss] ${e.message}`;
  34917. e.code = code;
  34918. e.loc = {
  34919. file: e.file,
  34920. line: e.line,
  34921. column: e.column - 1
  34922. // 1-based
  34923. };
  34924. throw e;
  34925. }
  34926. if (!devSourcemap) {
  34927. return {
  34928. ast: postcssResult,
  34929. code: postcssResult.css,
  34930. map: { mappings: "" },
  34931. modules,
  34932. deps
  34933. };
  34934. }
  34935. const rawPostcssMap = postcssResult.map.toJSON();
  34936. const postcssMap = await formatPostcssSourceMap(
  34937. // version property of rawPostcssMap is declared as string
  34938. // but actually it is a number
  34939. rawPostcssMap,
  34940. cleanUrl(id)
  34941. );
  34942. return {
  34943. ast: postcssResult,
  34944. code: postcssResult.css,
  34945. map: combineSourcemapsIfExists(cleanUrl(id), postcssMap, preprocessorMap),
  34946. modules,
  34947. deps
  34948. };
  34949. }
  34950. function createCachedImport(imp) {
  34951. let cached;
  34952. return () => {
  34953. if (!cached) {
  34954. cached = imp().then((module) => {
  34955. cached = module;
  34956. return module;
  34957. });
  34958. }
  34959. return cached;
  34960. };
  34961. }
  34962. const importPostcssImport = createCachedImport(() => import('./dep-GkhNNjoY.js').then(function (n) { return n.i; }));
  34963. const importPostcssModules = createCachedImport(() => import('./dep-BASfdaBA.js').then(function (n) { return n.i; }));
  34964. const importPostcss = createCachedImport(() => import('postcss'));
  34965. const preprocessorWorkerControllerCache = /* @__PURE__ */ new WeakMap();
  34966. let alwaysFakeWorkerWorkerControllerCache;
  34967. async function preprocessCSS(code, filename, config) {
  34968. let workerController = preprocessorWorkerControllerCache.get(config);
  34969. if (!workerController) {
  34970. alwaysFakeWorkerWorkerControllerCache ||= createPreprocessorWorkerController(0);
  34971. workerController = alwaysFakeWorkerWorkerControllerCache;
  34972. }
  34973. return await compileCSS(filename, code, config, workerController);
  34974. }
  34975. async function formatPostcssSourceMap(rawMap, file) {
  34976. const inputFileDir = path$n.dirname(file);
  34977. const sources = rawMap.sources.map((source) => {
  34978. const cleanSource = cleanUrl(decodeURIComponent(source));
  34979. if (cleanSource[0] === "<" && cleanSource[cleanSource.length - 1] === ">") {
  34980. return `\0${cleanSource}`;
  34981. }
  34982. return normalizePath$3(path$n.resolve(inputFileDir, cleanSource));
  34983. });
  34984. return {
  34985. file,
  34986. mappings: rawMap.mappings,
  34987. names: rawMap.names,
  34988. sources,
  34989. sourcesContent: rawMap.sourcesContent,
  34990. version: rawMap.version
  34991. };
  34992. }
  34993. function combineSourcemapsIfExists(filename, map1, map2) {
  34994. return map1 && map2 ? combineSourcemaps(filename, [
  34995. // type of version property of ExistingRawSourceMap is number
  34996. // but it is always 3
  34997. map1,
  34998. map2
  34999. ]) : map1;
  35000. }
  35001. const viteHashUpdateMarker = "/*$vite$:1*/";
  35002. const viteHashUpdateMarkerRE = /\/\*\$vite\$:\d+\*\//;
  35003. async function finalizeCss(css, minify, config) {
  35004. if (css.includes("@import") || css.includes("@charset")) {
  35005. css = await hoistAtRules(css);
  35006. }
  35007. if (config.build.cssMinify) {
  35008. css = await minifyCSS(css, config, false);
  35009. }
  35010. css += viteHashUpdateMarker;
  35011. return css;
  35012. }
  35013. async function resolvePostcssConfig(config) {
  35014. let result = postcssConfigCache.get(config);
  35015. if (result !== void 0) {
  35016. return await result;
  35017. }
  35018. const inlineOptions = config.css?.postcss;
  35019. if (isObject$1(inlineOptions)) {
  35020. const options = { ...inlineOptions };
  35021. delete options.plugins;
  35022. result = {
  35023. options,
  35024. plugins: inlineOptions.plugins || []
  35025. };
  35026. } else {
  35027. const searchPath = typeof inlineOptions === "string" ? inlineOptions : config.root;
  35028. result = postcssrc({}, searchPath).catch((e) => {
  35029. if (!e.message.includes("No PostCSS Config found")) {
  35030. if (e instanceof Error) {
  35031. const { name, message, stack } = e;
  35032. e.name = "Failed to load PostCSS config";
  35033. e.message = `Failed to load PostCSS config (searchPath: ${searchPath}): [${name}] ${message}
  35034. ${stack}`;
  35035. e.stack = "";
  35036. throw e;
  35037. } else {
  35038. throw new Error(`Failed to load PostCSS config: ${e}`);
  35039. }
  35040. }
  35041. return null;
  35042. });
  35043. result.then((resolved) => {
  35044. postcssConfigCache.set(config, resolved);
  35045. });
  35046. }
  35047. postcssConfigCache.set(config, result);
  35048. return result;
  35049. }
  35050. const cssUrlRE = /(?<=^|[^\w\-\u0080-\uffff])url\((\s*('[^']+'|"[^"]+")\s*|[^'")]+)\)/;
  35051. const cssDataUriRE = /(?<=^|[^\w\-\u0080-\uffff])data-uri\((\s*('[^']+'|"[^"]+")\s*|[^'")]+)\)/;
  35052. const importCssRE = /@import ('[^']+\.css'|"[^"]+\.css"|[^'")]+\.css)/;
  35053. const cssImageSetRE = /(?<=image-set\()((?:[\w\-]{1,256}\([^)]*\)|[^)])*)(?=\))/;
  35054. const UrlRewritePostcssPlugin = (opts) => {
  35055. if (!opts) {
  35056. throw new Error("base or replace is required");
  35057. }
  35058. return {
  35059. postcssPlugin: "vite-url-rewrite",
  35060. Once(root) {
  35061. const promises = [];
  35062. root.walkDecls((declaration) => {
  35063. const importer = declaration.source?.input.file;
  35064. if (!importer) {
  35065. opts.logger.warnOnce(
  35066. "\nA PostCSS plugin did not pass the `from` option to `postcss.parse`. This may cause imported assets to be incorrectly transformed. If you've recently added a PostCSS plugin that raised this warning, please contact the package author to fix the issue."
  35067. );
  35068. }
  35069. const isCssUrl = cssUrlRE.test(declaration.value);
  35070. const isCssImageSet = cssImageSetRE.test(declaration.value);
  35071. if (isCssUrl || isCssImageSet) {
  35072. const replacerForDeclaration = (rawUrl) => {
  35073. return opts.replacer(rawUrl, importer);
  35074. };
  35075. const rewriterToUse = isCssImageSet ? rewriteCssImageSet : rewriteCssUrls;
  35076. promises.push(
  35077. rewriterToUse(declaration.value, replacerForDeclaration).then(
  35078. (url) => {
  35079. declaration.value = url;
  35080. }
  35081. )
  35082. );
  35083. }
  35084. });
  35085. if (promises.length) {
  35086. return Promise.all(promises);
  35087. }
  35088. }
  35089. };
  35090. };
  35091. UrlRewritePostcssPlugin.postcss = true;
  35092. function rewriteCssUrls(css, replacer) {
  35093. return asyncReplace(css, cssUrlRE, async (match) => {
  35094. const [matched, rawUrl] = match;
  35095. return await doUrlReplace(rawUrl.trim(), matched, replacer);
  35096. });
  35097. }
  35098. function rewriteCssDataUris(css, replacer) {
  35099. return asyncReplace(css, cssDataUriRE, async (match) => {
  35100. const [matched, rawUrl] = match;
  35101. return await doUrlReplace(rawUrl.trim(), matched, replacer, "data-uri");
  35102. });
  35103. }
  35104. function rewriteImportCss(css, replacer) {
  35105. return asyncReplace(css, importCssRE, async (match) => {
  35106. const [matched, rawUrl] = match;
  35107. return await doImportCSSReplace(rawUrl, matched, replacer);
  35108. });
  35109. }
  35110. const cssNotProcessedRE = /(?:gradient|element|cross-fade|image)\(/;
  35111. async function rewriteCssImageSet(css, replacer) {
  35112. return await asyncReplace(css, cssImageSetRE, async (match) => {
  35113. const [, rawUrl] = match;
  35114. const url = await processSrcSet(rawUrl, async ({ url: url2 }) => {
  35115. if (cssUrlRE.test(url2)) {
  35116. return await rewriteCssUrls(url2, replacer);
  35117. }
  35118. if (!cssNotProcessedRE.test(url2)) {
  35119. return await doUrlReplace(url2, url2, replacer);
  35120. }
  35121. return url2;
  35122. });
  35123. return url;
  35124. });
  35125. }
  35126. function skipUrlReplacer(rawUrl) {
  35127. return isExternalUrl(rawUrl) || isDataUrl(rawUrl) || rawUrl[0] === "#" || functionCallRE.test(rawUrl);
  35128. }
  35129. async function doUrlReplace(rawUrl, matched, replacer, funcName = "url") {
  35130. let wrap = "";
  35131. const first = rawUrl[0];
  35132. if (first === `"` || first === `'`) {
  35133. wrap = first;
  35134. rawUrl = rawUrl.slice(1, -1);
  35135. }
  35136. if (skipUrlReplacer(rawUrl)) {
  35137. return matched;
  35138. }
  35139. let newUrl = await replacer(rawUrl);
  35140. if (wrap === "" && newUrl !== encodeURI(newUrl)) {
  35141. wrap = '"';
  35142. }
  35143. if (wrap === "'" && newUrl.includes("'")) {
  35144. wrap = '"';
  35145. }
  35146. if (wrap === '"' && newUrl.includes('"')) {
  35147. newUrl = newUrl.replace(nonEscapedDoubleQuoteRe, '\\"');
  35148. }
  35149. return `${funcName}(${wrap}${newUrl}${wrap})`;
  35150. }
  35151. async function doImportCSSReplace(rawUrl, matched, replacer) {
  35152. let wrap = "";
  35153. const first = rawUrl[0];
  35154. if (first === `"` || first === `'`) {
  35155. wrap = first;
  35156. rawUrl = rawUrl.slice(1, -1);
  35157. }
  35158. if (isExternalUrl(rawUrl) || isDataUrl(rawUrl) || rawUrl[0] === "#") {
  35159. return matched;
  35160. }
  35161. return `@import ${wrap}${await replacer(rawUrl)}${wrap}`;
  35162. }
  35163. async function minifyCSS(css, config, inlined) {
  35164. if (config.build.cssMinify === "lightningcss") {
  35165. const { code, warnings } = (await importLightningCSS()).transform({
  35166. ...config.css?.lightningcss,
  35167. targets: convertTargets(config.build.cssTarget),
  35168. cssModules: void 0,
  35169. filename: cssBundleName,
  35170. code: Buffer.from(css),
  35171. minify: true
  35172. });
  35173. if (warnings.length) {
  35174. config.logger.warn(
  35175. colors$1.yellow(
  35176. `warnings when minifying css:
  35177. ${warnings.map((w) => w.message).join("\n")}`
  35178. )
  35179. );
  35180. }
  35181. return decoder.decode(code) + (inlined ? "" : "\n");
  35182. }
  35183. try {
  35184. const { code, warnings } = await transform$1(css, {
  35185. loader: "css",
  35186. target: config.build.cssTarget || void 0,
  35187. ...resolveMinifyCssEsbuildOptions(config.esbuild || {})
  35188. });
  35189. if (warnings.length) {
  35190. const msgs = await formatMessages(warnings, { kind: "warning" });
  35191. config.logger.warn(
  35192. colors$1.yellow(`warnings when minifying css:
  35193. ${msgs.join("\n")}`)
  35194. );
  35195. }
  35196. return inlined ? code.trimEnd() : code;
  35197. } catch (e) {
  35198. if (e.errors) {
  35199. e.message = "[esbuild css minify] " + e.message;
  35200. const msgs = await formatMessages(e.errors, { kind: "error" });
  35201. e.frame = "\n" + msgs.join("\n");
  35202. e.loc = e.errors[0].location;
  35203. }
  35204. throw e;
  35205. }
  35206. }
  35207. function resolveMinifyCssEsbuildOptions(options) {
  35208. const base = {
  35209. charset: options.charset ?? "utf8",
  35210. logLevel: options.logLevel,
  35211. logLimit: options.logLimit,
  35212. logOverride: options.logOverride,
  35213. legalComments: options.legalComments
  35214. };
  35215. if (options.minifyIdentifiers != null || options.minifySyntax != null || options.minifyWhitespace != null) {
  35216. return {
  35217. ...base,
  35218. minifyIdentifiers: options.minifyIdentifiers ?? true,
  35219. minifySyntax: options.minifySyntax ?? true,
  35220. minifyWhitespace: options.minifyWhitespace ?? true
  35221. };
  35222. } else {
  35223. return { ...base, minify: true };
  35224. }
  35225. }
  35226. const atImportRE = /@import(?:\s*(?:url\([^)]*\)|"(?:[^"]|(?<=\\)")*"|'(?:[^']|(?<=\\)')*').*?|[^;]*);/g;
  35227. const atCharsetRE = /@charset(?:\s*(?:"(?:[^"]|(?<=\\)")*"|'(?:[^']|(?<=\\)')*').*?|[^;]*);/g;
  35228. async function hoistAtRules(css) {
  35229. const s = new MagicString(css);
  35230. const cleanCss = emptyCssComments(css);
  35231. let match;
  35232. atImportRE.lastIndex = 0;
  35233. while (match = atImportRE.exec(cleanCss)) {
  35234. s.remove(match.index, match.index + match[0].length);
  35235. s.appendLeft(0, match[0]);
  35236. }
  35237. atCharsetRE.lastIndex = 0;
  35238. let foundCharset = false;
  35239. while (match = atCharsetRE.exec(cleanCss)) {
  35240. s.remove(match.index, match.index + match[0].length);
  35241. if (!foundCharset) {
  35242. s.prepend(match[0]);
  35243. foundCharset = true;
  35244. }
  35245. }
  35246. return s.toString();
  35247. }
  35248. const loadedPreprocessorPath = {};
  35249. function loadPreprocessorPath(lang, root) {
  35250. const cached = loadedPreprocessorPath[lang];
  35251. if (cached) {
  35252. return cached;
  35253. }
  35254. try {
  35255. const resolved = requireResolveFromRootWithFallback(root, lang);
  35256. return loadedPreprocessorPath[lang] = resolved;
  35257. } catch (e) {
  35258. if (e.code === "MODULE_NOT_FOUND") {
  35259. const installCommand = getPackageManagerCommand("install");
  35260. throw new Error(
  35261. `Preprocessor dependency "${lang}" not found. Did you install it? Try \`${installCommand} -D ${lang}\`.`
  35262. );
  35263. } else {
  35264. const message = new Error(
  35265. `Preprocessor dependency "${lang}" failed to load:
  35266. ${e.message}`
  35267. );
  35268. message.stack = e.stack + "\n" + message.stack;
  35269. throw message;
  35270. }
  35271. }
  35272. }
  35273. function loadSassPackage(root) {
  35274. try {
  35275. const path2 = loadPreprocessorPath("sass-embedded", root);
  35276. return { name: "sass-embedded", path: path2 };
  35277. } catch (e1) {
  35278. try {
  35279. const path2 = loadPreprocessorPath("sass" /* sass */, root);
  35280. return { name: "sass", path: path2 };
  35281. } catch (e2) {
  35282. throw e1;
  35283. }
  35284. }
  35285. }
  35286. let cachedSss;
  35287. function loadSss(root) {
  35288. if (cachedSss) return cachedSss;
  35289. const sssPath = loadPreprocessorPath("sugarss" /* sss */, root);
  35290. cachedSss = createRequire$1(import.meta.url)(sssPath);
  35291. return cachedSss;
  35292. }
  35293. function cleanScssBugUrl(url) {
  35294. if (
  35295. // check bug via `window` and `location` global
  35296. typeof window !== "undefined" && typeof location !== "undefined" && typeof location?.href === "string"
  35297. ) {
  35298. const prefix = location.href.replace(/\/$/, "");
  35299. return url.replace(prefix, "");
  35300. } else {
  35301. return url;
  35302. }
  35303. }
  35304. function fixScssBugImportValue(data) {
  35305. if (
  35306. // check bug via `window` and `location` global
  35307. typeof window !== "undefined" && typeof location !== "undefined" && data && "file" in data && (!("contents" in data) || data.contents == null)
  35308. ) {
  35309. data.contents = fs__default.readFileSync(data.file, "utf-8");
  35310. }
  35311. return data;
  35312. }
  35313. const makeScssWorker = (resolvers, alias, maxWorkers, packageName) => {
  35314. const internalImporter = async (url, importer, filename) => {
  35315. importer = cleanScssBugUrl(importer);
  35316. const resolved = await resolvers.sass(url, importer);
  35317. if (resolved) {
  35318. try {
  35319. const data = await rebaseUrls(
  35320. resolved,
  35321. filename,
  35322. alias,
  35323. "$",
  35324. resolvers.sass
  35325. );
  35326. if (packageName === "sass-embedded") {
  35327. return data;
  35328. }
  35329. return fixScssBugImportValue(data);
  35330. } catch (data) {
  35331. return data;
  35332. }
  35333. } else {
  35334. return null;
  35335. }
  35336. };
  35337. const worker = new WorkerWithFallback(
  35338. () => async (sassPath, data, options) => {
  35339. const sass = require(sassPath);
  35340. const path2 = require("node:path");
  35341. const _internalImporter = (url, importer2, done) => {
  35342. internalImporter(url, importer2, options.filename).then(
  35343. (data2) => done?.(data2)
  35344. );
  35345. };
  35346. const importer = [_internalImporter];
  35347. if (options.importer) {
  35348. Array.isArray(options.importer) ? importer.unshift(...options.importer) : importer.unshift(options.importer);
  35349. }
  35350. const finalOptions = {
  35351. ...options,
  35352. data,
  35353. file: options.filename,
  35354. outFile: options.filename,
  35355. importer,
  35356. ...options.enableSourcemap ? {
  35357. sourceMap: true,
  35358. omitSourceMapUrl: true,
  35359. sourceMapRoot: path2.dirname(options.filename)
  35360. } : {}
  35361. };
  35362. return new Promise((resolve, reject) => {
  35363. sass.render(finalOptions, (err, res) => {
  35364. if (err) {
  35365. reject(err);
  35366. } else {
  35367. resolve({
  35368. css: res.css.toString(),
  35369. map: res.map?.toString(),
  35370. stats: res.stats
  35371. });
  35372. }
  35373. });
  35374. });
  35375. },
  35376. {
  35377. parentFunctions: { internalImporter },
  35378. shouldUseFake(_sassPath, _data, options) {
  35379. return !!(options.functions && Object.keys(options.functions).length > 0 || options.importer && (!Array.isArray(options.importer) || options.importer.length > 0));
  35380. },
  35381. max: maxWorkers
  35382. }
  35383. );
  35384. return worker;
  35385. };
  35386. const makeModernScssWorker = (resolvers, alias, maxWorkers) => {
  35387. const internalCanonicalize = async (url, importer) => {
  35388. importer = cleanScssBugUrl(importer);
  35389. const resolved = await resolvers.sass(url, importer);
  35390. return resolved ?? null;
  35391. };
  35392. const internalLoad = async (file, rootFile) => {
  35393. const result = await rebaseUrls(file, rootFile, alias, "$", resolvers.sass);
  35394. if (result.contents) {
  35395. return result.contents;
  35396. }
  35397. return await fsp.readFile(result.file, "utf-8");
  35398. };
  35399. const worker = new WorkerWithFallback(
  35400. () => async (sassPath, data, options) => {
  35401. const sass = require(sassPath);
  35402. const path2 = require("node:path");
  35403. const { fileURLToPath: fileURLToPath2, pathToFileURL: pathToFileURL2 } = (
  35404. // eslint-disable-next-line no-restricted-globals
  35405. require("node:url")
  35406. );
  35407. const sassOptions = { ...options };
  35408. sassOptions.url = pathToFileURL2(options.filename);
  35409. sassOptions.sourceMap = options.enableSourcemap;
  35410. const internalImporter = {
  35411. async canonicalize(url, context) {
  35412. const importer = context.containingUrl ? fileURLToPath2(context.containingUrl) : options.filename;
  35413. const resolved = await internalCanonicalize(url, importer);
  35414. return resolved ? pathToFileURL2(resolved) : null;
  35415. },
  35416. async load(canonicalUrl) {
  35417. const ext = path2.extname(canonicalUrl.pathname);
  35418. let syntax = "scss";
  35419. if (ext === ".sass") {
  35420. syntax = "indented";
  35421. } else if (ext === ".css") {
  35422. syntax = "css";
  35423. }
  35424. const contents = await internalLoad(
  35425. fileURLToPath2(canonicalUrl),
  35426. options.filename
  35427. );
  35428. return { contents, syntax, sourceMapUrl: canonicalUrl };
  35429. }
  35430. };
  35431. sassOptions.importers = [
  35432. ...sassOptions.importers ?? [],
  35433. internalImporter
  35434. ];
  35435. const result = await sass.compileStringAsync(data, sassOptions);
  35436. return {
  35437. css: result.css,
  35438. map: result.sourceMap ? JSON.stringify(result.sourceMap) : void 0,
  35439. stats: {
  35440. includedFiles: result.loadedUrls.filter((url) => url.protocol === "file:").map((url) => fileURLToPath2(url))
  35441. }
  35442. };
  35443. },
  35444. {
  35445. parentFunctions: {
  35446. internalCanonicalize,
  35447. internalLoad
  35448. },
  35449. shouldUseFake(_sassPath, _data, options) {
  35450. return !!(options.functions && Object.keys(options.functions).length > 0 || options.importers && (!Array.isArray(options.importers) || options.importers.length > 0));
  35451. },
  35452. max: maxWorkers
  35453. }
  35454. );
  35455. return worker;
  35456. };
  35457. const makeModernCompilerScssWorker = (resolvers, alias, _maxWorkers) => {
  35458. let compilerPromise;
  35459. const worker = {
  35460. async run(sassPath, data, options) {
  35461. const sass = (await import(pathToFileURL(sassPath).href)).default;
  35462. compilerPromise ??= sass.initAsyncCompiler();
  35463. const compiler = await compilerPromise;
  35464. const sassOptions = { ...options };
  35465. sassOptions.url = pathToFileURL(options.filename);
  35466. sassOptions.sourceMap = options.enableSourcemap;
  35467. const internalImporter = {
  35468. async canonicalize(url, context) {
  35469. const importer = context.containingUrl ? fileURLToPath(context.containingUrl) : options.filename;
  35470. const resolved = await resolvers.sass(url, cleanScssBugUrl(importer));
  35471. return resolved ? pathToFileURL(resolved) : null;
  35472. },
  35473. async load(canonicalUrl) {
  35474. const ext = path$n.extname(canonicalUrl.pathname);
  35475. let syntax = "scss";
  35476. if (ext === ".sass") {
  35477. syntax = "indented";
  35478. } else if (ext === ".css") {
  35479. syntax = "css";
  35480. }
  35481. const result2 = await rebaseUrls(
  35482. fileURLToPath(canonicalUrl),
  35483. options.filename,
  35484. alias,
  35485. "$",
  35486. resolvers.sass
  35487. );
  35488. const contents = result2.contents ?? await fsp.readFile(result2.file, "utf-8");
  35489. return { contents, syntax, sourceMapUrl: canonicalUrl };
  35490. }
  35491. };
  35492. sassOptions.importers = [
  35493. ...sassOptions.importers ?? [],
  35494. internalImporter
  35495. ];
  35496. const result = await compiler.compileStringAsync(data, sassOptions);
  35497. return {
  35498. css: result.css,
  35499. map: result.sourceMap ? JSON.stringify(result.sourceMap) : void 0,
  35500. stats: {
  35501. includedFiles: result.loadedUrls.filter((url) => url.protocol === "file:").map((url) => fileURLToPath(url))
  35502. }
  35503. };
  35504. },
  35505. async stop() {
  35506. (await compilerPromise)?.dispose();
  35507. compilerPromise = void 0;
  35508. }
  35509. };
  35510. return worker;
  35511. };
  35512. const scssProcessor = (maxWorkers) => {
  35513. const workerMap = /* @__PURE__ */ new Map();
  35514. return {
  35515. close() {
  35516. for (const worker of workerMap.values()) {
  35517. worker.stop();
  35518. }
  35519. },
  35520. async process(source, root, options, resolvers) {
  35521. const sassPackage = loadSassPackage(root);
  35522. const api = options.api ?? "legacy";
  35523. if (!workerMap.has(options.alias)) {
  35524. workerMap.set(
  35525. options.alias,
  35526. api === "modern-compiler" ? makeModernCompilerScssWorker(resolvers, options.alias) : api === "modern" ? makeModernScssWorker(resolvers, options.alias, maxWorkers) : makeScssWorker(
  35527. resolvers,
  35528. options.alias,
  35529. maxWorkers,
  35530. sassPackage.name
  35531. )
  35532. );
  35533. }
  35534. const worker = workerMap.get(options.alias);
  35535. const { content: data, map: additionalMap } = await getSource(
  35536. source,
  35537. options.filename,
  35538. options.additionalData,
  35539. options.enableSourcemap
  35540. );
  35541. const optionsWithoutAdditionalData = {
  35542. ...options,
  35543. additionalData: void 0
  35544. };
  35545. try {
  35546. const result = await worker.run(
  35547. sassPackage.path,
  35548. data,
  35549. optionsWithoutAdditionalData
  35550. );
  35551. const deps = result.stats.includedFiles.map((f) => cleanScssBugUrl(f));
  35552. const map2 = result.map ? JSON.parse(result.map.toString()) : void 0;
  35553. if (map2) {
  35554. map2.sources = map2.sources.map(
  35555. (url) => url.startsWith("file://") ? normalizePath$3(fileURLToPath(url)) : url
  35556. );
  35557. }
  35558. return {
  35559. code: result.css.toString(),
  35560. map: map2,
  35561. additionalMap,
  35562. deps
  35563. };
  35564. } catch (e) {
  35565. e.message = `[sass] ${e.message}`;
  35566. e.id = e.file;
  35567. e.frame = e.formatted;
  35568. return { code: "", error: e, deps: [] };
  35569. }
  35570. }
  35571. };
  35572. };
  35573. async function rebaseUrls(file, rootFile, alias, variablePrefix, resolver) {
  35574. file = path$n.resolve(file);
  35575. const fileDir = path$n.dirname(file);
  35576. const rootDir = path$n.dirname(rootFile);
  35577. if (fileDir === rootDir) {
  35578. return { file };
  35579. }
  35580. const content = await fsp.readFile(file, "utf-8");
  35581. const hasUrls = cssUrlRE.test(content);
  35582. const hasDataUris = cssDataUriRE.test(content);
  35583. const hasImportCss = importCssRE.test(content);
  35584. if (!hasUrls && !hasDataUris && !hasImportCss) {
  35585. return { file };
  35586. }
  35587. let rebased;
  35588. const rebaseFn = async (url) => {
  35589. if (url[0] === "/") return url;
  35590. if (url.startsWith(variablePrefix)) return url;
  35591. for (const { find } of alias) {
  35592. const matches = typeof find === "string" ? url.startsWith(find) : find.test(url);
  35593. if (matches) {
  35594. return url;
  35595. }
  35596. }
  35597. const absolute = await resolver(url, file) || path$n.resolve(fileDir, url);
  35598. const relative = path$n.relative(rootDir, absolute);
  35599. return normalizePath$3(relative);
  35600. };
  35601. if (hasImportCss) {
  35602. rebased = await rewriteImportCss(content, rebaseFn);
  35603. }
  35604. if (hasUrls) {
  35605. rebased = await rewriteCssUrls(rebased || content, rebaseFn);
  35606. }
  35607. if (hasDataUris) {
  35608. rebased = await rewriteCssDataUris(rebased || content, rebaseFn);
  35609. }
  35610. return {
  35611. file,
  35612. contents: rebased
  35613. };
  35614. }
  35615. const makeLessWorker = (resolvers, alias, maxWorkers) => {
  35616. const viteLessResolve = async (filename, dir, rootFile) => {
  35617. const resolved = await resolvers.less(filename, path$n.join(dir, "*"));
  35618. if (!resolved) return void 0;
  35619. const result = await rebaseUrls(
  35620. resolved,
  35621. rootFile,
  35622. alias,
  35623. "@",
  35624. resolvers.less
  35625. );
  35626. if (result) {
  35627. return {
  35628. resolved,
  35629. contents: "contents" in result ? result.contents : void 0
  35630. };
  35631. }
  35632. return result;
  35633. };
  35634. const worker = new WorkerWithFallback(
  35635. () => {
  35636. const fsp2 = require("node:fs/promises");
  35637. const path2 = require("node:path");
  35638. let ViteLessManager;
  35639. const createViteLessPlugin = (less, rootFile) => {
  35640. const { FileManager } = less;
  35641. ViteLessManager ??= class ViteManager extends FileManager {
  35642. rootFile;
  35643. constructor(rootFile2) {
  35644. super();
  35645. this.rootFile = rootFile2;
  35646. }
  35647. supports(filename) {
  35648. return !/^(?:https?:)?\/\//.test(filename);
  35649. }
  35650. supportsSync() {
  35651. return false;
  35652. }
  35653. async loadFile(filename, dir, opts, env) {
  35654. const result = await viteLessResolve(filename, dir, this.rootFile);
  35655. if (result) {
  35656. return {
  35657. filename: path2.resolve(result.resolved),
  35658. contents: result.contents ?? await fsp2.readFile(result.resolved, "utf-8")
  35659. };
  35660. } else {
  35661. return super.loadFile(filename, dir, opts, env);
  35662. }
  35663. }
  35664. };
  35665. return {
  35666. install(_, pluginManager) {
  35667. pluginManager.addFileManager(new ViteLessManager(rootFile));
  35668. },
  35669. minVersion: [3, 0, 0]
  35670. };
  35671. };
  35672. return async (lessPath, content, options) => {
  35673. const nodeLess = require(lessPath);
  35674. const viteResolverPlugin = createViteLessPlugin(
  35675. nodeLess,
  35676. options.filename
  35677. );
  35678. const result = await nodeLess.render(content, {
  35679. ...options,
  35680. plugins: [viteResolverPlugin, ...options.plugins || []],
  35681. ...options.enableSourcemap ? {
  35682. sourceMap: {
  35683. outputSourceFiles: true,
  35684. sourceMapFileInline: false
  35685. }
  35686. } : {}
  35687. });
  35688. return result;
  35689. };
  35690. },
  35691. {
  35692. parentFunctions: { viteLessResolve },
  35693. shouldUseFake(_lessPath, _content, options) {
  35694. return options.plugins?.length > 0;
  35695. },
  35696. max: maxWorkers
  35697. }
  35698. );
  35699. return worker;
  35700. };
  35701. const lessProcessor = (maxWorkers) => {
  35702. const workerMap = /* @__PURE__ */ new Map();
  35703. return {
  35704. close() {
  35705. for (const worker of workerMap.values()) {
  35706. worker.stop();
  35707. }
  35708. },
  35709. async process(source, root, options, resolvers) {
  35710. const lessPath = loadPreprocessorPath("less" /* less */, root);
  35711. if (!workerMap.has(options.alias)) {
  35712. workerMap.set(
  35713. options.alias,
  35714. makeLessWorker(resolvers, options.alias, maxWorkers)
  35715. );
  35716. }
  35717. const worker = workerMap.get(options.alias);
  35718. const { content, map: additionalMap } = await getSource(
  35719. source,
  35720. options.filename,
  35721. options.additionalData,
  35722. options.enableSourcemap
  35723. );
  35724. let result;
  35725. const optionsWithoutAdditionalData = {
  35726. ...options,
  35727. additionalData: void 0
  35728. };
  35729. try {
  35730. result = await worker.run(
  35731. lessPath,
  35732. content,
  35733. optionsWithoutAdditionalData
  35734. );
  35735. } catch (e) {
  35736. const error = e;
  35737. const normalizedError = new Error(
  35738. `[less] ${error.message || error.type}`
  35739. );
  35740. normalizedError.loc = {
  35741. file: error.filename || options.filename,
  35742. line: error.line,
  35743. column: error.column
  35744. };
  35745. return { code: "", error: normalizedError, deps: [] };
  35746. }
  35747. const map2 = result.map && JSON.parse(result.map);
  35748. if (map2) {
  35749. delete map2.sourcesContent;
  35750. }
  35751. return {
  35752. code: result.css.toString(),
  35753. map: map2,
  35754. additionalMap,
  35755. deps: result.imports
  35756. };
  35757. }
  35758. };
  35759. };
  35760. const makeStylWorker = (maxWorkers) => {
  35761. const worker = new WorkerWithFallback(
  35762. () => {
  35763. return async (stylusPath, content, root, options) => {
  35764. const nodeStylus = require(stylusPath);
  35765. const ref = nodeStylus(content, options);
  35766. if (options.define) {
  35767. for (const key in options.define) {
  35768. ref.define(key, options.define[key]);
  35769. }
  35770. }
  35771. if (options.enableSourcemap) {
  35772. ref.set("sourcemap", {
  35773. comment: false,
  35774. inline: false,
  35775. basePath: root
  35776. });
  35777. }
  35778. return {
  35779. code: ref.render(),
  35780. // @ts-expect-error sourcemap exists
  35781. map: ref.sourcemap,
  35782. deps: ref.deps()
  35783. };
  35784. };
  35785. },
  35786. {
  35787. shouldUseFake(_stylusPath, _content, _root, options) {
  35788. return !!(options.define && Object.values(options.define).some((d) => typeof d === "function"));
  35789. },
  35790. max: maxWorkers
  35791. }
  35792. );
  35793. return worker;
  35794. };
  35795. const stylProcessor = (maxWorkers) => {
  35796. const workerMap = /* @__PURE__ */ new Map();
  35797. return {
  35798. close() {
  35799. for (const worker of workerMap.values()) {
  35800. worker.stop();
  35801. }
  35802. },
  35803. async process(source, root, options, resolvers) {
  35804. const stylusPath = loadPreprocessorPath("stylus" /* stylus */, root);
  35805. if (!workerMap.has(options.alias)) {
  35806. workerMap.set(options.alias, makeStylWorker(maxWorkers));
  35807. }
  35808. const worker = workerMap.get(options.alias);
  35809. const { content, map: additionalMap } = await getSource(
  35810. source,
  35811. options.filename,
  35812. options.additionalData,
  35813. options.enableSourcemap,
  35814. "\n"
  35815. );
  35816. const importsDeps = (options.imports ?? []).map(
  35817. (dep) => path$n.resolve(dep)
  35818. );
  35819. const optionsWithoutAdditionalData = {
  35820. ...options,
  35821. additionalData: void 0
  35822. };
  35823. try {
  35824. const { code, map: map2, deps } = await worker.run(
  35825. stylusPath,
  35826. content,
  35827. root,
  35828. optionsWithoutAdditionalData
  35829. );
  35830. return {
  35831. code,
  35832. map: formatStylusSourceMap(map2, root),
  35833. additionalMap,
  35834. // Concat imports deps with computed deps
  35835. deps: [...deps, ...importsDeps]
  35836. };
  35837. } catch (e) {
  35838. const wrapped = new Error(`[stylus] ${e.message}`);
  35839. wrapped.name = e.name;
  35840. wrapped.stack = e.stack;
  35841. return { code: "", error: wrapped, deps: [] };
  35842. }
  35843. }
  35844. };
  35845. };
  35846. function formatStylusSourceMap(mapBefore, root) {
  35847. if (!mapBefore) return void 0;
  35848. const map2 = { ...mapBefore };
  35849. const resolveFromRoot = (p) => normalizePath$3(path$n.resolve(root, p));
  35850. if (map2.file) {
  35851. map2.file = resolveFromRoot(map2.file);
  35852. }
  35853. map2.sources = map2.sources.map(resolveFromRoot);
  35854. return map2;
  35855. }
  35856. async function getSource(source, filename, additionalData, enableSourcemap, sep = "") {
  35857. if (!additionalData) return { content: source };
  35858. if (typeof additionalData === "function") {
  35859. const newContent = await additionalData(source, filename);
  35860. if (typeof newContent === "string") {
  35861. return { content: newContent };
  35862. }
  35863. return newContent;
  35864. }
  35865. if (!enableSourcemap) {
  35866. return { content: additionalData + sep + source };
  35867. }
  35868. const ms = new MagicString(source);
  35869. ms.appendLeft(0, sep);
  35870. ms.appendLeft(0, additionalData);
  35871. const map2 = ms.generateMap({ hires: "boundary" });
  35872. map2.file = filename;
  35873. map2.sources = [filename];
  35874. return {
  35875. content: ms.toString(),
  35876. map: map2
  35877. };
  35878. }
  35879. const createPreprocessorWorkerController = (maxWorkers) => {
  35880. const scss = scssProcessor(maxWorkers);
  35881. const less = lessProcessor(maxWorkers);
  35882. const styl = stylProcessor(maxWorkers);
  35883. const sassProcess = (source, root, options, resolvers) => {
  35884. return scss.process(
  35885. source,
  35886. root,
  35887. { ...options, indentedSyntax: true, syntax: "indented" },
  35888. resolvers
  35889. );
  35890. };
  35891. const close = () => {
  35892. less.close();
  35893. scss.close();
  35894. styl.close();
  35895. };
  35896. return {
  35897. ["less" /* less */]: less.process,
  35898. ["scss" /* scss */]: scss.process,
  35899. ["sass" /* sass */]: sassProcess,
  35900. ["styl" /* styl */]: styl.process,
  35901. ["stylus" /* stylus */]: styl.process,
  35902. close
  35903. };
  35904. };
  35905. const normalizeMaxWorkers = (maxWorker) => {
  35906. if (maxWorker === void 0) return 0;
  35907. if (maxWorker === true) return void 0;
  35908. return maxWorker;
  35909. };
  35910. const preprocessorSet = /* @__PURE__ */ new Set([
  35911. "less" /* less */,
  35912. "sass" /* sass */,
  35913. "scss" /* scss */,
  35914. "styl" /* styl */,
  35915. "stylus" /* stylus */
  35916. ]);
  35917. function isPreProcessor(lang) {
  35918. return lang && preprocessorSet.has(lang);
  35919. }
  35920. const importLightningCSS = createCachedImport(() => import('lightningcss'));
  35921. async function compileLightningCSS(id, src, config, urlReplacer) {
  35922. const deps = /* @__PURE__ */ new Set();
  35923. const filename = cleanUrl(path$n.relative(config.root, id));
  35924. const toAbsolute = (filePath) => path$n.isAbsolute(filePath) ? filePath : path$n.join(config.root, filePath);
  35925. const res = styleAttrRE.test(id) ? (await importLightningCSS()).transformStyleAttribute({
  35926. filename,
  35927. code: Buffer.from(src),
  35928. targets: config.css?.lightningcss?.targets,
  35929. minify: config.isProduction && !!config.build.cssMinify,
  35930. analyzeDependencies: true
  35931. }) : await (await importLightningCSS()).bundleAsync({
  35932. ...config.css?.lightningcss,
  35933. filename,
  35934. resolver: {
  35935. read(filePath) {
  35936. if (filePath === filename) {
  35937. return src;
  35938. }
  35939. if (!filePath.endsWith(".css")) {
  35940. return src;
  35941. }
  35942. return fs__default.readFileSync(toAbsolute(filePath), "utf-8");
  35943. },
  35944. async resolve(id2, from) {
  35945. const publicFile = checkPublicFile(id2, config);
  35946. if (publicFile) {
  35947. return publicFile;
  35948. }
  35949. const resolved = await getAtImportResolvers(config).css(
  35950. id2,
  35951. toAbsolute(from)
  35952. );
  35953. if (resolved) {
  35954. deps.add(resolved);
  35955. return resolved;
  35956. }
  35957. return id2;
  35958. }
  35959. },
  35960. minify: config.isProduction && !!config.build.cssMinify,
  35961. sourceMap: config.command === "build" ? !!config.build.sourcemap : config.css?.devSourcemap,
  35962. analyzeDependencies: true,
  35963. cssModules: cssModuleRE.test(id) ? config.css?.lightningcss?.cssModules ?? true : void 0
  35964. });
  35965. let css = decoder.decode(res.code);
  35966. for (const dep of res.dependencies) {
  35967. switch (dep.type) {
  35968. case "url":
  35969. if (skipUrlReplacer(dep.url)) {
  35970. css = css.replace(dep.placeholder, () => dep.url);
  35971. break;
  35972. }
  35973. deps.add(dep.url);
  35974. if (urlReplacer) {
  35975. const replaceUrl = await urlReplacer(
  35976. dep.url,
  35977. toAbsolute(dep.loc.filePath)
  35978. );
  35979. css = css.replace(dep.placeholder, () => replaceUrl);
  35980. } else {
  35981. css = css.replace(dep.placeholder, () => dep.url);
  35982. }
  35983. break;
  35984. default:
  35985. throw new Error(`Unsupported dependency type: ${dep.type}`);
  35986. }
  35987. }
  35988. let modules;
  35989. if ("exports" in res && res.exports) {
  35990. modules = {};
  35991. const sortedEntries = Object.entries(res.exports).sort(
  35992. (a, b) => a[0].localeCompare(b[0])
  35993. );
  35994. for (const [key, value] of sortedEntries) {
  35995. modules[key] = value.name;
  35996. for (const c of value.composes) {
  35997. modules[key] += " " + c.name;
  35998. }
  35999. }
  36000. }
  36001. return {
  36002. code: css,
  36003. map: "map" in res ? res.map?.toString() : void 0,
  36004. deps,
  36005. modules
  36006. };
  36007. }
  36008. const map = {
  36009. chrome: "chrome",
  36010. edge: "edge",
  36011. firefox: "firefox",
  36012. hermes: false,
  36013. ie: "ie",
  36014. ios: "ios_saf",
  36015. node: false,
  36016. opera: "opera",
  36017. rhino: false,
  36018. safari: "safari"
  36019. };
  36020. const esMap = {
  36021. // https://caniuse.com/?search=es2015
  36022. 2015: ["chrome49", "edge13", "safari10", "firefox44", "opera36"],
  36023. // https://caniuse.com/?search=es2016
  36024. 2016: ["chrome50", "edge13", "safari10", "firefox43", "opera37"],
  36025. // https://caniuse.com/?search=es2017
  36026. 2017: ["chrome58", "edge15", "safari11", "firefox52", "opera45"],
  36027. // https://caniuse.com/?search=es2018
  36028. 2018: ["chrome63", "edge79", "safari12", "firefox58", "opera50"],
  36029. // https://caniuse.com/?search=es2019
  36030. 2019: ["chrome73", "edge79", "safari12.1", "firefox64", "opera60"],
  36031. // https://caniuse.com/?search=es2020
  36032. 2020: ["chrome80", "edge80", "safari14.1", "firefox80", "opera67"],
  36033. // https://caniuse.com/?search=es2021
  36034. 2021: ["chrome85", "edge85", "safari14.1", "firefox80", "opera71"],
  36035. // https://caniuse.com/?search=es2022
  36036. 2022: ["chrome94", "edge94", "safari16.4", "firefox93", "opera80"]
  36037. };
  36038. const esRE = /es(\d{4})/;
  36039. const versionRE = /\d/;
  36040. const convertTargetsCache = /* @__PURE__ */ new Map();
  36041. const convertTargets = (esbuildTarget) => {
  36042. if (!esbuildTarget) return {};
  36043. const cached = convertTargetsCache.get(esbuildTarget);
  36044. if (cached) return cached;
  36045. const targets = {};
  36046. const entriesWithoutES = arraify(esbuildTarget).flatMap((e) => {
  36047. const match = esRE.exec(e);
  36048. if (!match) return e;
  36049. const year = Number(match[1]);
  36050. if (!esMap[year]) throw new Error(`Unsupported target "${e}"`);
  36051. return esMap[year];
  36052. });
  36053. for (const entry of entriesWithoutES) {
  36054. if (entry === "esnext") continue;
  36055. const index = entry.search(versionRE);
  36056. if (index >= 0) {
  36057. const browser = map[entry.slice(0, index)];
  36058. if (browser === false) continue;
  36059. if (browser) {
  36060. const [major, minor = 0] = entry.slice(index).split(".").map((v) => parseInt(v, 10));
  36061. if (!isNaN(major) && !isNaN(minor)) {
  36062. const version = major << 16 | minor << 8;
  36063. if (!targets[browser] || version < targets[browser]) {
  36064. targets[browser] = version;
  36065. }
  36066. continue;
  36067. }
  36068. }
  36069. }
  36070. throw new Error(`Unsupported target "${entry}"`);
  36071. }
  36072. convertTargetsCache.set(esbuildTarget, targets);
  36073. return targets;
  36074. };
  36075. const HASH_RE = /#/g;
  36076. const AMPERSAND_RE = /&/g;
  36077. const SLASH_RE = /\//g;
  36078. const EQUAL_RE = /=/g;
  36079. const PLUS_RE = /\+/g;
  36080. const ENC_CARET_RE = /%5e/gi;
  36081. const ENC_BACKTICK_RE = /%60/gi;
  36082. const ENC_PIPE_RE = /%7c/gi;
  36083. const ENC_SPACE_RE = /%20/gi;
  36084. function encode(text) {
  36085. return encodeURI("" + text).replace(ENC_PIPE_RE, "|");
  36086. }
  36087. function encodeQueryValue(input) {
  36088. return encode(typeof input === "string" ? input : JSON.stringify(input)).replace(PLUS_RE, "%2B").replace(ENC_SPACE_RE, "+").replace(HASH_RE, "%23").replace(AMPERSAND_RE, "%26").replace(ENC_BACKTICK_RE, "`").replace(ENC_CARET_RE, "^").replace(SLASH_RE, "%2F");
  36089. }
  36090. function encodeQueryKey(text) {
  36091. return encodeQueryValue(text).replace(EQUAL_RE, "%3D");
  36092. }
  36093. function encodeQueryItem(key, value) {
  36094. if (typeof value === "number" || typeof value === "boolean") {
  36095. value = String(value);
  36096. }
  36097. if (!value) {
  36098. return encodeQueryKey(key);
  36099. }
  36100. if (Array.isArray(value)) {
  36101. return value.map((_value) => `${encodeQueryKey(key)}=${encodeQueryValue(_value)}`).join("&");
  36102. }
  36103. return `${encodeQueryKey(key)}=${encodeQueryValue(value)}`;
  36104. }
  36105. function stringifyQuery(query) {
  36106. return Object.keys(query).filter((k) => query[k] !== void 0).map((k) => encodeQueryItem(k, query[k])).filter(Boolean).join("&");
  36107. }
  36108. new Set(builtinModules);
  36109. function clearImports(imports) {
  36110. return (imports || "").replace(/(\/\/[^\n]*\n|\/\*.*\*\/)/g, "").replace(/\s+/g, " ");
  36111. }
  36112. function getImportNames(cleanedImports) {
  36113. const topLevelImports = cleanedImports.replace(/{([^}]*)}/, "");
  36114. const namespacedImport = topLevelImports.match(/\* as \s*(\S*)/)?.[1];
  36115. const defaultImport = topLevelImports.split(",").find((index) => !/[*{}]/.test(index))?.trim() || void 0;
  36116. return {
  36117. namespacedImport,
  36118. defaultImport
  36119. };
  36120. }
  36121. /**
  36122. * @typedef ErrnoExceptionFields
  36123. * @property {number | undefined} [errnode]
  36124. * @property {string | undefined} [code]
  36125. * @property {string | undefined} [path]
  36126. * @property {string | undefined} [syscall]
  36127. * @property {string | undefined} [url]
  36128. *
  36129. * @typedef {Error & ErrnoExceptionFields} ErrnoException
  36130. */
  36131. const own$1 = {}.hasOwnProperty;
  36132. const classRegExp = /^([A-Z][a-z\d]*)+$/;
  36133. // Sorted by a rough estimate on most frequently used entries.
  36134. const kTypes = new Set([
  36135. 'string',
  36136. 'function',
  36137. 'number',
  36138. 'object',
  36139. // Accept 'Function' and 'Object' as alternative to the lower cased version.
  36140. 'Function',
  36141. 'Object',
  36142. 'boolean',
  36143. 'bigint',
  36144. 'symbol'
  36145. ]);
  36146. /**
  36147. * Create a list string in the form like 'A and B' or 'A, B, ..., and Z'.
  36148. * We cannot use Intl.ListFormat because it's not available in
  36149. * --without-intl builds.
  36150. *
  36151. * @param {Array<string>} array
  36152. * An array of strings.
  36153. * @param {string} [type]
  36154. * The list type to be inserted before the last element.
  36155. * @returns {string}
  36156. */
  36157. function formatList(array, type = 'and') {
  36158. return array.length < 3
  36159. ? array.join(` ${type} `)
  36160. : `${array.slice(0, -1).join(', ')}, ${type} ${array[array.length - 1]}`
  36161. }
  36162. /** @type {Map<string, MessageFunction | string>} */
  36163. const messages = new Map();
  36164. const nodeInternalPrefix = '__node_internal_';
  36165. /** @type {number} */
  36166. let userStackTraceLimit;
  36167. createError(
  36168. 'ERR_INVALID_ARG_TYPE',
  36169. /**
  36170. * @param {string} name
  36171. * @param {Array<string> | string} expected
  36172. * @param {unknown} actual
  36173. */
  36174. (name, expected, actual) => {
  36175. assert$1(typeof name === 'string', "'name' must be a string");
  36176. if (!Array.isArray(expected)) {
  36177. expected = [expected];
  36178. }
  36179. let message = 'The ';
  36180. if (name.endsWith(' argument')) {
  36181. // For cases like 'first argument'
  36182. message += `${name} `;
  36183. } else {
  36184. const type = name.includes('.') ? 'property' : 'argument';
  36185. message += `"${name}" ${type} `;
  36186. }
  36187. message += 'must be ';
  36188. /** @type {Array<string>} */
  36189. const types = [];
  36190. /** @type {Array<string>} */
  36191. const instances = [];
  36192. /** @type {Array<string>} */
  36193. const other = [];
  36194. for (const value of expected) {
  36195. assert$1(
  36196. typeof value === 'string',
  36197. 'All expected entries have to be of type string'
  36198. );
  36199. if (kTypes.has(value)) {
  36200. types.push(value.toLowerCase());
  36201. } else if (classRegExp.exec(value) === null) {
  36202. assert$1(
  36203. value !== 'object',
  36204. 'The value "object" should be written as "Object"'
  36205. );
  36206. other.push(value);
  36207. } else {
  36208. instances.push(value);
  36209. }
  36210. }
  36211. // Special handle `object` in case other instances are allowed to outline
  36212. // the differences between each other.
  36213. if (instances.length > 0) {
  36214. const pos = types.indexOf('object');
  36215. if (pos !== -1) {
  36216. types.slice(pos, 1);
  36217. instances.push('Object');
  36218. }
  36219. }
  36220. if (types.length > 0) {
  36221. message += `${types.length > 1 ? 'one of type' : 'of type'} ${formatList(
  36222. types,
  36223. 'or'
  36224. )}`;
  36225. if (instances.length > 0 || other.length > 0) message += ' or ';
  36226. }
  36227. if (instances.length > 0) {
  36228. message += `an instance of ${formatList(instances, 'or')}`;
  36229. if (other.length > 0) message += ' or ';
  36230. }
  36231. if (other.length > 0) {
  36232. if (other.length > 1) {
  36233. message += `one of ${formatList(other, 'or')}`;
  36234. } else {
  36235. if (other[0].toLowerCase() !== other[0]) message += 'an ';
  36236. message += `${other[0]}`;
  36237. }
  36238. }
  36239. message += `. Received ${determineSpecificType(actual)}`;
  36240. return message
  36241. },
  36242. TypeError
  36243. );
  36244. createError(
  36245. 'ERR_INVALID_MODULE_SPECIFIER',
  36246. /**
  36247. * @param {string} request
  36248. * @param {string} reason
  36249. * @param {string} [base]
  36250. */
  36251. (request, reason, base = undefined) => {
  36252. return `Invalid module "${request}" ${reason}${
  36253. base ? ` imported from ${base}` : ''
  36254. }`
  36255. },
  36256. TypeError
  36257. );
  36258. createError(
  36259. 'ERR_INVALID_PACKAGE_CONFIG',
  36260. /**
  36261. * @param {string} path
  36262. * @param {string} [base]
  36263. * @param {string} [message]
  36264. */
  36265. (path, base, message) => {
  36266. return `Invalid package config ${path}${
  36267. base ? ` while importing ${base}` : ''
  36268. }${message ? `. ${message}` : ''}`
  36269. },
  36270. Error
  36271. );
  36272. createError(
  36273. 'ERR_INVALID_PACKAGE_TARGET',
  36274. /**
  36275. * @param {string} packagePath
  36276. * @param {string} key
  36277. * @param {unknown} target
  36278. * @param {boolean} [isImport=false]
  36279. * @param {string} [base]
  36280. */
  36281. (packagePath, key, target, isImport = false, base = undefined) => {
  36282. const relatedError =
  36283. typeof target === 'string' &&
  36284. !isImport &&
  36285. target.length > 0 &&
  36286. !target.startsWith('./');
  36287. if (key === '.') {
  36288. assert$1(isImport === false);
  36289. return (
  36290. `Invalid "exports" main target ${JSON.stringify(target)} defined ` +
  36291. `in the package config ${packagePath}package.json${
  36292. base ? ` imported from ${base}` : ''
  36293. }${relatedError ? '; targets must start with "./"' : ''}`
  36294. )
  36295. }
  36296. return `Invalid "${
  36297. isImport ? 'imports' : 'exports'
  36298. }" target ${JSON.stringify(
  36299. target
  36300. )} defined for '${key}' in the package config ${packagePath}package.json${
  36301. base ? ` imported from ${base}` : ''
  36302. }${relatedError ? '; targets must start with "./"' : ''}`
  36303. },
  36304. Error
  36305. );
  36306. createError(
  36307. 'ERR_MODULE_NOT_FOUND',
  36308. /**
  36309. * @param {string} path
  36310. * @param {string} base
  36311. * @param {boolean} [exactUrl]
  36312. */
  36313. (path, base, exactUrl = false) => {
  36314. return `Cannot find ${
  36315. exactUrl ? 'module' : 'package'
  36316. } '${path}' imported from ${base}`
  36317. },
  36318. Error
  36319. );
  36320. createError(
  36321. 'ERR_NETWORK_IMPORT_DISALLOWED',
  36322. "import of '%s' by %s is not supported: %s",
  36323. Error
  36324. );
  36325. createError(
  36326. 'ERR_PACKAGE_IMPORT_NOT_DEFINED',
  36327. /**
  36328. * @param {string} specifier
  36329. * @param {string} packagePath
  36330. * @param {string} base
  36331. */
  36332. (specifier, packagePath, base) => {
  36333. return `Package import specifier "${specifier}" is not defined${
  36334. packagePath ? ` in package ${packagePath}package.json` : ''
  36335. } imported from ${base}`
  36336. },
  36337. TypeError
  36338. );
  36339. createError(
  36340. 'ERR_PACKAGE_PATH_NOT_EXPORTED',
  36341. /**
  36342. * @param {string} packagePath
  36343. * @param {string} subpath
  36344. * @param {string} [base]
  36345. */
  36346. (packagePath, subpath, base = undefined) => {
  36347. if (subpath === '.')
  36348. return `No "exports" main defined in ${packagePath}package.json${
  36349. base ? ` imported from ${base}` : ''
  36350. }`
  36351. return `Package subpath '${subpath}' is not defined by "exports" in ${packagePath}package.json${
  36352. base ? ` imported from ${base}` : ''
  36353. }`
  36354. },
  36355. Error
  36356. );
  36357. createError(
  36358. 'ERR_UNSUPPORTED_DIR_IMPORT',
  36359. "Directory import '%s' is not supported " +
  36360. 'resolving ES modules imported from %s',
  36361. Error
  36362. );
  36363. createError(
  36364. 'ERR_UNSUPPORTED_RESOLVE_REQUEST',
  36365. 'Failed to resolve module specifier "%s" from "%s": Invalid relative URL or base scheme is not hierarchical.',
  36366. TypeError
  36367. );
  36368. createError(
  36369. 'ERR_UNKNOWN_FILE_EXTENSION',
  36370. /**
  36371. * @param {string} extension
  36372. * @param {string} path
  36373. */
  36374. (extension, path) => {
  36375. return `Unknown file extension "${extension}" for ${path}`
  36376. },
  36377. TypeError
  36378. );
  36379. createError(
  36380. 'ERR_INVALID_ARG_VALUE',
  36381. /**
  36382. * @param {string} name
  36383. * @param {unknown} value
  36384. * @param {string} [reason='is invalid']
  36385. */
  36386. (name, value, reason = 'is invalid') => {
  36387. let inspected = inspect(value);
  36388. if (inspected.length > 128) {
  36389. inspected = `${inspected.slice(0, 128)}...`;
  36390. }
  36391. const type = name.includes('.') ? 'property' : 'argument';
  36392. return `The ${type} '${name}' ${reason}. Received ${inspected}`
  36393. },
  36394. TypeError
  36395. // Note: extra classes have been shaken out.
  36396. // , RangeError
  36397. );
  36398. /**
  36399. * Utility function for registering the error codes. Only used here. Exported
  36400. * *only* to allow for testing.
  36401. * @param {string} sym
  36402. * @param {MessageFunction | string} value
  36403. * @param {ErrorConstructor} constructor
  36404. * @returns {new (...parameters: Array<any>) => Error}
  36405. */
  36406. function createError(sym, value, constructor) {
  36407. // Special case for SystemError that formats the error message differently
  36408. // The SystemErrors only have SystemError as their base classes.
  36409. messages.set(sym, value);
  36410. return makeNodeErrorWithCode(constructor, sym)
  36411. }
  36412. /**
  36413. * @param {ErrorConstructor} Base
  36414. * @param {string} key
  36415. * @returns {ErrorConstructor}
  36416. */
  36417. function makeNodeErrorWithCode(Base, key) {
  36418. // @ts-expect-error It’s a Node error.
  36419. return NodeError
  36420. /**
  36421. * @param {Array<unknown>} parameters
  36422. */
  36423. function NodeError(...parameters) {
  36424. const limit = Error.stackTraceLimit;
  36425. if (isErrorStackTraceLimitWritable()) Error.stackTraceLimit = 0;
  36426. const error = new Base();
  36427. // Reset the limit and setting the name property.
  36428. if (isErrorStackTraceLimitWritable()) Error.stackTraceLimit = limit;
  36429. const message = getMessage(key, parameters, error);
  36430. Object.defineProperties(error, {
  36431. // Note: no need to implement `kIsNodeError` symbol, would be hard,
  36432. // probably.
  36433. message: {
  36434. value: message,
  36435. enumerable: false,
  36436. writable: true,
  36437. configurable: true
  36438. },
  36439. toString: {
  36440. /** @this {Error} */
  36441. value() {
  36442. return `${this.name} [${key}]: ${this.message}`
  36443. },
  36444. enumerable: false,
  36445. writable: true,
  36446. configurable: true
  36447. }
  36448. });
  36449. captureLargerStackTrace(error);
  36450. // @ts-expect-error It’s a Node error.
  36451. error.code = key;
  36452. return error
  36453. }
  36454. }
  36455. /**
  36456. * @returns {boolean}
  36457. */
  36458. function isErrorStackTraceLimitWritable() {
  36459. // Do no touch Error.stackTraceLimit as V8 would attempt to install
  36460. // it again during deserialization.
  36461. try {
  36462. if (v8.startupSnapshot.isBuildingSnapshot()) {
  36463. return false
  36464. }
  36465. } catch {}
  36466. const desc = Object.getOwnPropertyDescriptor(Error, 'stackTraceLimit');
  36467. if (desc === undefined) {
  36468. return Object.isExtensible(Error)
  36469. }
  36470. return own$1.call(desc, 'writable') && desc.writable !== undefined
  36471. ? desc.writable
  36472. : desc.set !== undefined
  36473. }
  36474. /**
  36475. * This function removes unnecessary frames from Node.js core errors.
  36476. * @template {(...parameters: unknown[]) => unknown} T
  36477. * @param {T} wrappedFunction
  36478. * @returns {T}
  36479. */
  36480. function hideStackFrames(wrappedFunction) {
  36481. // We rename the functions that will be hidden to cut off the stacktrace
  36482. // at the outermost one
  36483. const hidden = nodeInternalPrefix + wrappedFunction.name;
  36484. Object.defineProperty(wrappedFunction, 'name', {value: hidden});
  36485. return wrappedFunction
  36486. }
  36487. const captureLargerStackTrace = hideStackFrames(
  36488. /**
  36489. * @param {Error} error
  36490. * @returns {Error}
  36491. */
  36492. // @ts-expect-error: fine
  36493. function (error) {
  36494. const stackTraceLimitIsWritable = isErrorStackTraceLimitWritable();
  36495. if (stackTraceLimitIsWritable) {
  36496. userStackTraceLimit = Error.stackTraceLimit;
  36497. Error.stackTraceLimit = Number.POSITIVE_INFINITY;
  36498. }
  36499. Error.captureStackTrace(error);
  36500. // Reset the limit
  36501. if (stackTraceLimitIsWritable) Error.stackTraceLimit = userStackTraceLimit;
  36502. return error
  36503. }
  36504. );
  36505. /**
  36506. * @param {string} key
  36507. * @param {Array<unknown>} parameters
  36508. * @param {Error} self
  36509. * @returns {string}
  36510. */
  36511. function getMessage(key, parameters, self) {
  36512. const message = messages.get(key);
  36513. assert$1(message !== undefined, 'expected `message` to be found');
  36514. if (typeof message === 'function') {
  36515. assert$1(
  36516. message.length <= parameters.length, // Default options do not count.
  36517. `Code: ${key}; The provided arguments length (${parameters.length}) does not ` +
  36518. `match the required ones (${message.length}).`
  36519. );
  36520. return Reflect.apply(message, self, parameters)
  36521. }
  36522. const regex = /%[dfijoOs]/g;
  36523. let expectedLength = 0;
  36524. while (regex.exec(message) !== null) expectedLength++;
  36525. assert$1(
  36526. expectedLength === parameters.length,
  36527. `Code: ${key}; The provided arguments length (${parameters.length}) does not ` +
  36528. `match the required ones (${expectedLength}).`
  36529. );
  36530. if (parameters.length === 0) return message
  36531. parameters.unshift(message);
  36532. return Reflect.apply(format$2, null, parameters)
  36533. }
  36534. /**
  36535. * Determine the specific type of a value for type-mismatch errors.
  36536. * @param {unknown} value
  36537. * @returns {string}
  36538. */
  36539. function determineSpecificType(value) {
  36540. if (value === null || value === undefined) {
  36541. return String(value)
  36542. }
  36543. if (typeof value === 'function' && value.name) {
  36544. return `function ${value.name}`
  36545. }
  36546. if (typeof value === 'object') {
  36547. if (value.constructor && value.constructor.name) {
  36548. return `an instance of ${value.constructor.name}`
  36549. }
  36550. return `${inspect(value, {depth: -1})}`
  36551. }
  36552. let inspected = inspect(value, {colors: false});
  36553. if (inspected.length > 28) {
  36554. inspected = `${inspected.slice(0, 25)}...`;
  36555. }
  36556. return `type ${typeof value} (${inspected})`
  36557. }
  36558. const ESM_STATIC_IMPORT_RE = /(?<=\s|^|;|\})import\s*([\s"']*(?<imports>[\p{L}\p{M}\w\t\n\r $*,/{}@.]+)from\s*)?["']\s*(?<specifier>(?<="\s*)[^"]*[^\s"](?=\s*")|(?<='\s*)[^']*[^\s'](?=\s*'))\s*["'][\s;]*/gmu;
  36559. const TYPE_RE = /^\s*?type\s/;
  36560. function parseStaticImport(matched) {
  36561. const cleanedImports = clearImports(matched.imports);
  36562. const namedImports = {};
  36563. const _matches = cleanedImports.match(/{([^}]*)}/)?.[1]?.split(",") || [];
  36564. for (const namedImport of _matches) {
  36565. const _match = namedImport.match(/^\s*(\S*) as (\S*)\s*$/);
  36566. const source = _match?.[1] || namedImport.trim();
  36567. const importName = _match?.[2] || source;
  36568. if (source && !TYPE_RE.test(source)) {
  36569. namedImports[source] = importName;
  36570. }
  36571. }
  36572. const { namespacedImport, defaultImport } = getImportNames(cleanedImports);
  36573. return {
  36574. ...matched,
  36575. defaultImport,
  36576. namespacedImport,
  36577. namedImports
  36578. };
  36579. }
  36580. const ESM_RE = /([\s;]|^)(import[\s\w*,{}]*from|import\s*["'*{]|export\b\s*(?:[*{]|default|class|type|function|const|var|let|async function)|import\.meta\b)/m;
  36581. const COMMENT_RE = /\/\*.+?\*\/|\/\/.*(?=[nr])/g;
  36582. function hasESMSyntax(code, opts = {}) {
  36583. if (opts.stripComments) {
  36584. code = code.replace(COMMENT_RE, "");
  36585. }
  36586. return ESM_RE.test(code);
  36587. }
  36588. const { isMatch: isMatch$1, scan } = micromatch$2;
  36589. function getAffectedGlobModules(file, server) {
  36590. const modules = [];
  36591. for (const [id, allGlobs] of server._importGlobMap) {
  36592. if (allGlobs.some(
  36593. ({ affirmed, negated }) => (!affirmed.length || affirmed.some((glob) => isMatch$1(file, glob))) && (!negated.length || negated.every((glob) => isMatch$1(file, glob)))
  36594. )) {
  36595. const mod = server.moduleGraph.getModuleById(id);
  36596. if (mod) modules.push(mod);
  36597. }
  36598. }
  36599. modules.forEach((i) => {
  36600. if (i?.file) server.moduleGraph.onFileChange(i.file);
  36601. });
  36602. return modules;
  36603. }
  36604. function importGlobPlugin(config) {
  36605. let server;
  36606. return {
  36607. name: "vite:import-glob",
  36608. configureServer(_server) {
  36609. server = _server;
  36610. server._importGlobMap.clear();
  36611. },
  36612. async transform(code, id) {
  36613. if (!code.includes("import.meta.glob")) return;
  36614. const result = await transformGlobImport(
  36615. code,
  36616. id,
  36617. config.root,
  36618. (im, _, options) => this.resolve(im, id, options).then((i) => i?.id || im),
  36619. config.experimental.importGlobRestoreExtension,
  36620. config.logger
  36621. );
  36622. if (result) {
  36623. if (server) {
  36624. const allGlobs = result.matches.map((i) => i.globsResolved);
  36625. server._importGlobMap.set(
  36626. id,
  36627. allGlobs.map((globs) => {
  36628. const affirmed = [];
  36629. const negated = [];
  36630. for (const glob of globs) {
  36631. (glob[0] === "!" ? negated : affirmed).push(glob);
  36632. }
  36633. return { affirmed, negated };
  36634. })
  36635. );
  36636. }
  36637. return transformStableResult(result.s, id, config);
  36638. }
  36639. }
  36640. };
  36641. }
  36642. const importGlobRE = /\bimport\.meta\.glob(?:<\w+>)?\s*\(/g;
  36643. const knownOptions = {
  36644. as: ["string"],
  36645. eager: ["boolean"],
  36646. import: ["string"],
  36647. exhaustive: ["boolean"],
  36648. query: ["object", "string"]
  36649. };
  36650. const forceDefaultAs = ["raw", "url"];
  36651. function err$1(e, pos) {
  36652. const error = new Error(e);
  36653. error.pos = pos;
  36654. return error;
  36655. }
  36656. function parseGlobOptions(rawOpts, optsStartIndex, logger) {
  36657. let opts = {};
  36658. try {
  36659. opts = evalValue(rawOpts);
  36660. } catch {
  36661. throw err$1(
  36662. "Vite is unable to parse the glob options as the value is not static",
  36663. optsStartIndex
  36664. );
  36665. }
  36666. if (opts == null) {
  36667. return {};
  36668. }
  36669. for (const key in opts) {
  36670. if (!(key in knownOptions)) {
  36671. throw err$1(`Unknown glob option "${key}"`, optsStartIndex);
  36672. }
  36673. const allowedTypes = knownOptions[key];
  36674. const valueType = typeof opts[key];
  36675. if (!allowedTypes.includes(valueType)) {
  36676. throw err$1(
  36677. `Expected glob option "${key}" to be of type ${allowedTypes.join(
  36678. " or "
  36679. )}, but got ${valueType}`,
  36680. optsStartIndex
  36681. );
  36682. }
  36683. }
  36684. if (typeof opts.query === "object") {
  36685. for (const key in opts.query) {
  36686. const value = opts.query[key];
  36687. if (!["string", "number", "boolean"].includes(typeof value)) {
  36688. throw err$1(
  36689. `Expected glob option "query.${key}" to be of type string, number, or boolean, but got ${typeof value}`,
  36690. optsStartIndex
  36691. );
  36692. }
  36693. }
  36694. opts.query = stringifyQuery(opts.query);
  36695. }
  36696. if (opts.as && logger) {
  36697. const importSuggestion = forceDefaultAs.includes(opts.as) ? `, import: 'default'` : "";
  36698. logger.warn(
  36699. colors$1.yellow(
  36700. `The glob option "as" has been deprecated in favour of "query". Please update \`as: '${opts.as}'\` to \`query: '?${opts.as}'${importSuggestion}\`.`
  36701. )
  36702. );
  36703. }
  36704. if (opts.as && forceDefaultAs.includes(opts.as)) {
  36705. if (opts.import && opts.import !== "default" && opts.import !== "*")
  36706. throw err$1(
  36707. `Option "import" can only be "default" or "*" when "as" is "${opts.as}", but got "${opts.import}"`,
  36708. optsStartIndex
  36709. );
  36710. opts.import = opts.import || "default";
  36711. }
  36712. if (opts.as && opts.query)
  36713. throw err$1(
  36714. 'Options "as" and "query" cannot be used together',
  36715. optsStartIndex
  36716. );
  36717. if (opts.as) opts.query = opts.as;
  36718. if (opts.query && opts.query[0] !== "?") opts.query = `?${opts.query}`;
  36719. return opts;
  36720. }
  36721. async function parseImportGlob(code, importer, root, resolveId, logger) {
  36722. let cleanCode;
  36723. try {
  36724. cleanCode = stripLiteral(code);
  36725. } catch (e) {
  36726. return [];
  36727. }
  36728. const matches = Array.from(cleanCode.matchAll(importGlobRE));
  36729. const tasks = matches.map(async (match, index) => {
  36730. const start = match.index;
  36731. const err2 = (msg) => {
  36732. const e = new Error(`Invalid glob import syntax: ${msg}`);
  36733. e.pos = start;
  36734. return e;
  36735. };
  36736. const end = findCorrespondingCloseParenthesisPosition(
  36737. cleanCode,
  36738. start + match[0].length
  36739. ) + 1;
  36740. if (end <= 0) {
  36741. throw err2("Close parenthesis not found");
  36742. }
  36743. const statementCode = code.slice(start, end);
  36744. const rootAst = (await parseAstAsync(statementCode)).body[0];
  36745. if (rootAst.type !== "ExpressionStatement") {
  36746. throw err2(`Expect CallExpression, got ${rootAst.type}`);
  36747. }
  36748. const ast = rootAst.expression;
  36749. if (ast.type !== "CallExpression") {
  36750. throw err2(`Expect CallExpression, got ${ast.type}`);
  36751. }
  36752. if (ast.arguments.length < 1 || ast.arguments.length > 2)
  36753. throw err2(`Expected 1-2 arguments, but got ${ast.arguments.length}`);
  36754. const arg1 = ast.arguments[0];
  36755. const arg2 = ast.arguments[1];
  36756. const globs = [];
  36757. const validateLiteral = (element) => {
  36758. if (!element) return;
  36759. if (element.type === "Literal") {
  36760. if (typeof element.value !== "string")
  36761. throw err2(
  36762. `Expected glob to be a string, but got "${typeof element.value}"`
  36763. );
  36764. globs.push(element.value);
  36765. } else if (element.type === "TemplateLiteral") {
  36766. if (element.expressions.length !== 0) {
  36767. throw err2(
  36768. `Expected glob to be a string, but got dynamic template literal`
  36769. );
  36770. }
  36771. globs.push(element.quasis[0].value.raw);
  36772. } else {
  36773. throw err2("Could only use literals");
  36774. }
  36775. };
  36776. if (arg1.type === "ArrayExpression") {
  36777. for (const element of arg1.elements) {
  36778. validateLiteral(element);
  36779. }
  36780. } else {
  36781. validateLiteral(arg1);
  36782. }
  36783. let options = {};
  36784. if (arg2) {
  36785. if (arg2.type !== "ObjectExpression")
  36786. throw err2(
  36787. `Expected the second argument to be an object literal, but got "${arg2.type}"`
  36788. );
  36789. options = parseGlobOptions(
  36790. code.slice(start + arg2.start, start + arg2.end),
  36791. start + arg2.start,
  36792. logger
  36793. );
  36794. }
  36795. const globsResolved = await Promise.all(
  36796. globs.map((glob) => toAbsoluteGlob(glob, root, importer, resolveId))
  36797. );
  36798. const isRelative = globs.every((i) => ".!".includes(i[0]));
  36799. return {
  36800. index,
  36801. globs,
  36802. globsResolved,
  36803. isRelative,
  36804. options,
  36805. start,
  36806. end
  36807. };
  36808. });
  36809. return (await Promise.all(tasks)).filter(Boolean);
  36810. }
  36811. function findCorrespondingCloseParenthesisPosition(cleanCode, openPos) {
  36812. const closePos = cleanCode.indexOf(")", openPos);
  36813. if (closePos < 0) return -1;
  36814. if (!cleanCode.slice(openPos, closePos).includes("(")) return closePos;
  36815. let remainingParenthesisCount = 0;
  36816. const cleanCodeLen = cleanCode.length;
  36817. for (let pos = openPos; pos < cleanCodeLen; pos++) {
  36818. switch (cleanCode[pos]) {
  36819. case "(": {
  36820. remainingParenthesisCount++;
  36821. break;
  36822. }
  36823. case ")": {
  36824. remainingParenthesisCount--;
  36825. if (remainingParenthesisCount <= 0) {
  36826. return pos;
  36827. }
  36828. }
  36829. }
  36830. }
  36831. return -1;
  36832. }
  36833. const importPrefix = "__vite_glob_";
  36834. const { basename, dirname, relative, join } = posix$1;
  36835. async function transformGlobImport(code, id, root, resolveId, restoreQueryExtension = false, logger) {
  36836. id = slash$1(id);
  36837. root = slash$1(root);
  36838. const isVirtual = isVirtualModule(id);
  36839. const dir = isVirtual ? void 0 : dirname(id);
  36840. const matches = await parseImportGlob(
  36841. code,
  36842. isVirtual ? void 0 : id,
  36843. root,
  36844. resolveId,
  36845. logger
  36846. );
  36847. const matchedFiles = /* @__PURE__ */ new Set();
  36848. if (!matches.length) return null;
  36849. const s = new MagicString(code);
  36850. const staticImports = (await Promise.all(
  36851. matches.map(
  36852. async ({ globsResolved, isRelative, options, index, start, end }) => {
  36853. const cwd = getCommonBase(globsResolved) ?? root;
  36854. const files = (await glob(globsResolved, {
  36855. cwd,
  36856. absolute: true,
  36857. dot: !!options.exhaustive,
  36858. ignore: options.exhaustive ? [] : [join(cwd, "**/node_modules/**")]
  36859. })).filter((file) => file !== id).sort();
  36860. const objectProps = [];
  36861. const staticImports2 = [];
  36862. const resolvePaths = (file) => {
  36863. if (!dir) {
  36864. if (isRelative)
  36865. throw new Error(
  36866. "In virtual modules, all globs must start with '/'"
  36867. );
  36868. const filePath2 = `/${relative(root, file)}`;
  36869. return { filePath: filePath2, importPath: filePath2 };
  36870. }
  36871. let importPath = relative(dir, file);
  36872. if (importPath[0] !== ".") importPath = `./${importPath}`;
  36873. let filePath;
  36874. if (isRelative) {
  36875. filePath = importPath;
  36876. } else {
  36877. filePath = relative(root, file);
  36878. if (filePath[0] !== ".") filePath = `/${filePath}`;
  36879. }
  36880. return { filePath, importPath };
  36881. };
  36882. files.forEach((file, i) => {
  36883. const paths = resolvePaths(file);
  36884. const filePath = paths.filePath;
  36885. let importPath = paths.importPath;
  36886. let importQuery = options.query ?? "";
  36887. if (importQuery && importQuery !== "?raw") {
  36888. const fileExtension = basename(file).split(".").slice(-1)[0];
  36889. if (fileExtension && restoreQueryExtension)
  36890. importQuery = `${importQuery}&lang.${fileExtension}`;
  36891. }
  36892. importPath = `${importPath}${importQuery}`;
  36893. const importKey = options.import && options.import !== "*" ? options.import : void 0;
  36894. if (options.eager) {
  36895. const variableName = `${importPrefix}${index}_${i}`;
  36896. const expression = importKey ? `{ ${importKey} as ${variableName} }` : `* as ${variableName}`;
  36897. staticImports2.push(
  36898. `import ${expression} from ${JSON.stringify(importPath)}`
  36899. );
  36900. objectProps.push(`${JSON.stringify(filePath)}: ${variableName}`);
  36901. } else {
  36902. let importStatement = `import(${JSON.stringify(importPath)})`;
  36903. if (importKey)
  36904. importStatement += `.then(m => m[${JSON.stringify(importKey)}])`;
  36905. objectProps.push(
  36906. `${JSON.stringify(filePath)}: () => ${importStatement}`
  36907. );
  36908. }
  36909. });
  36910. files.forEach((i) => matchedFiles.add(i));
  36911. const originalLineBreakCount = code.slice(start, end).match(/\n/g)?.length ?? 0;
  36912. const lineBreaks = originalLineBreakCount > 0 ? "\n".repeat(originalLineBreakCount) : "";
  36913. const replacement = `/* #__PURE__ */ Object.assign({${objectProps.join(
  36914. ","
  36915. )}${lineBreaks}})`;
  36916. s.overwrite(start, end, replacement);
  36917. return staticImports2;
  36918. }
  36919. )
  36920. )).flat();
  36921. if (staticImports.length) s.prepend(`${staticImports.join(";")};`);
  36922. return {
  36923. s,
  36924. matches,
  36925. files: matchedFiles
  36926. };
  36927. }
  36928. function globSafePath(path) {
  36929. return glob.escapePath(normalizePath$3(path));
  36930. }
  36931. function lastNthChar(str, n) {
  36932. return str.charAt(str.length - 1 - n);
  36933. }
  36934. function globSafeResolvedPath(resolved, glob) {
  36935. let numEqual = 0;
  36936. const maxEqual = Math.min(resolved.length, glob.length);
  36937. while (numEqual < maxEqual && lastNthChar(resolved, numEqual) === lastNthChar(glob, numEqual)) {
  36938. numEqual += 1;
  36939. }
  36940. const staticPartEnd = resolved.length - numEqual;
  36941. const staticPart = resolved.slice(0, staticPartEnd);
  36942. const dynamicPart = resolved.slice(staticPartEnd);
  36943. return globSafePath(staticPart) + dynamicPart;
  36944. }
  36945. async function toAbsoluteGlob(glob, root, importer, resolveId) {
  36946. let pre = "";
  36947. if (glob[0] === "!") {
  36948. pre = "!";
  36949. glob = glob.slice(1);
  36950. }
  36951. root = globSafePath(root);
  36952. const dir = importer ? globSafePath(dirname(importer)) : root;
  36953. if (glob[0] === "/") return pre + posix$1.join(root, glob.slice(1));
  36954. if (glob.startsWith("./")) return pre + posix$1.join(dir, glob.slice(2));
  36955. if (glob.startsWith("../")) return pre + posix$1.join(dir, glob);
  36956. if (glob.startsWith("**")) return pre + glob;
  36957. const isSubImportsPattern = glob[0] === "#" && glob.includes("*");
  36958. const resolved = normalizePath$3(
  36959. await resolveId(glob, importer, {
  36960. custom: { "vite:import-glob": { isSubImportsPattern } }
  36961. }) || glob
  36962. );
  36963. if (isAbsolute$1(resolved)) {
  36964. return pre + globSafeResolvedPath(resolved, glob);
  36965. }
  36966. throw new Error(
  36967. `Invalid glob: "${glob}" (resolved: "${resolved}"). It must start with '/' or './'`
  36968. );
  36969. }
  36970. function getCommonBase(globsResolved) {
  36971. const bases = globsResolved.filter((g) => g[0] !== "!").map((glob) => {
  36972. let { base } = scan(glob);
  36973. if (posix$1.basename(base).includes(".")) base = posix$1.dirname(base);
  36974. return base;
  36975. });
  36976. if (!bases.length) return null;
  36977. let commonAncestor = "";
  36978. const dirS = bases[0].split("/");
  36979. for (let i = 0; i < dirS.length; i++) {
  36980. const candidate = dirS.slice(0, i + 1).join("/");
  36981. if (bases.every((base) => base.startsWith(candidate)))
  36982. commonAncestor = candidate;
  36983. else break;
  36984. }
  36985. if (!commonAncestor) commonAncestor = "/";
  36986. return commonAncestor;
  36987. }
  36988. function isVirtualModule(id) {
  36989. return id.startsWith("virtual:") || id[0] === "\0" || !id.includes("/");
  36990. }
  36991. var src = {exports: {}};
  36992. var browser = {exports: {}};
  36993. var debug$f = {exports: {}};
  36994. /**
  36995. * Helpers.
  36996. */
  36997. var ms;
  36998. var hasRequiredMs;
  36999. function requireMs () {
  37000. if (hasRequiredMs) return ms;
  37001. hasRequiredMs = 1;
  37002. var s = 1000;
  37003. var m = s * 60;
  37004. var h = m * 60;
  37005. var d = h * 24;
  37006. var y = d * 365.25;
  37007. /**
  37008. * Parse or format the given `val`.
  37009. *
  37010. * Options:
  37011. *
  37012. * - `long` verbose formatting [false]
  37013. *
  37014. * @param {String|Number} val
  37015. * @param {Object} [options]
  37016. * @throws {Error} throw an error if val is not a non-empty string or a number
  37017. * @return {String|Number}
  37018. * @api public
  37019. */
  37020. ms = function(val, options) {
  37021. options = options || {};
  37022. var type = typeof val;
  37023. if (type === 'string' && val.length > 0) {
  37024. return parse(val);
  37025. } else if (type === 'number' && isNaN(val) === false) {
  37026. return options.long ? fmtLong(val) : fmtShort(val);
  37027. }
  37028. throw new Error(
  37029. 'val is not a non-empty string or a valid number. val=' +
  37030. JSON.stringify(val)
  37031. );
  37032. };
  37033. /**
  37034. * Parse the given `str` and return milliseconds.
  37035. *
  37036. * @param {String} str
  37037. * @return {Number}
  37038. * @api private
  37039. */
  37040. function parse(str) {
  37041. str = String(str);
  37042. if (str.length > 100) {
  37043. return;
  37044. }
  37045. var match = /^((?:\d+)?\.?\d+) *(milliseconds?|msecs?|ms|seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|years?|yrs?|y)?$/i.exec(
  37046. str
  37047. );
  37048. if (!match) {
  37049. return;
  37050. }
  37051. var n = parseFloat(match[1]);
  37052. var type = (match[2] || 'ms').toLowerCase();
  37053. switch (type) {
  37054. case 'years':
  37055. case 'year':
  37056. case 'yrs':
  37057. case 'yr':
  37058. case 'y':
  37059. return n * y;
  37060. case 'days':
  37061. case 'day':
  37062. case 'd':
  37063. return n * d;
  37064. case 'hours':
  37065. case 'hour':
  37066. case 'hrs':
  37067. case 'hr':
  37068. case 'h':
  37069. return n * h;
  37070. case 'minutes':
  37071. case 'minute':
  37072. case 'mins':
  37073. case 'min':
  37074. case 'm':
  37075. return n * m;
  37076. case 'seconds':
  37077. case 'second':
  37078. case 'secs':
  37079. case 'sec':
  37080. case 's':
  37081. return n * s;
  37082. case 'milliseconds':
  37083. case 'millisecond':
  37084. case 'msecs':
  37085. case 'msec':
  37086. case 'ms':
  37087. return n;
  37088. default:
  37089. return undefined;
  37090. }
  37091. }
  37092. /**
  37093. * Short format for `ms`.
  37094. *
  37095. * @param {Number} ms
  37096. * @return {String}
  37097. * @api private
  37098. */
  37099. function fmtShort(ms) {
  37100. if (ms >= d) {
  37101. return Math.round(ms / d) + 'd';
  37102. }
  37103. if (ms >= h) {
  37104. return Math.round(ms / h) + 'h';
  37105. }
  37106. if (ms >= m) {
  37107. return Math.round(ms / m) + 'm';
  37108. }
  37109. if (ms >= s) {
  37110. return Math.round(ms / s) + 's';
  37111. }
  37112. return ms + 'ms';
  37113. }
  37114. /**
  37115. * Long format for `ms`.
  37116. *
  37117. * @param {Number} ms
  37118. * @return {String}
  37119. * @api private
  37120. */
  37121. function fmtLong(ms) {
  37122. return plural(ms, d, 'day') ||
  37123. plural(ms, h, 'hour') ||
  37124. plural(ms, m, 'minute') ||
  37125. plural(ms, s, 'second') ||
  37126. ms + ' ms';
  37127. }
  37128. /**
  37129. * Pluralization helper.
  37130. */
  37131. function plural(ms, n, name) {
  37132. if (ms < n) {
  37133. return;
  37134. }
  37135. if (ms < n * 1.5) {
  37136. return Math.floor(ms / n) + ' ' + name;
  37137. }
  37138. return Math.ceil(ms / n) + ' ' + name + 's';
  37139. }
  37140. return ms;
  37141. }
  37142. var hasRequiredDebug;
  37143. function requireDebug () {
  37144. if (hasRequiredDebug) return debug$f.exports;
  37145. hasRequiredDebug = 1;
  37146. (function (module, exports) {
  37147. /**
  37148. * This is the common logic for both the Node.js and web browser
  37149. * implementations of `debug()`.
  37150. *
  37151. * Expose `debug()` as the module.
  37152. */
  37153. exports = module.exports = createDebug.debug = createDebug['default'] = createDebug;
  37154. exports.coerce = coerce;
  37155. exports.disable = disable;
  37156. exports.enable = enable;
  37157. exports.enabled = enabled;
  37158. exports.humanize = requireMs();
  37159. /**
  37160. * The currently active debug mode names, and names to skip.
  37161. */
  37162. exports.names = [];
  37163. exports.skips = [];
  37164. /**
  37165. * Map of special "%n" handling functions, for the debug "format" argument.
  37166. *
  37167. * Valid key names are a single, lower or upper-case letter, i.e. "n" and "N".
  37168. */
  37169. exports.formatters = {};
  37170. /**
  37171. * Previous log timestamp.
  37172. */
  37173. var prevTime;
  37174. /**
  37175. * Select a color.
  37176. * @param {String} namespace
  37177. * @return {Number}
  37178. * @api private
  37179. */
  37180. function selectColor(namespace) {
  37181. var hash = 0, i;
  37182. for (i in namespace) {
  37183. hash = ((hash << 5) - hash) + namespace.charCodeAt(i);
  37184. hash |= 0; // Convert to 32bit integer
  37185. }
  37186. return exports.colors[Math.abs(hash) % exports.colors.length];
  37187. }
  37188. /**
  37189. * Create a debugger with the given `namespace`.
  37190. *
  37191. * @param {String} namespace
  37192. * @return {Function}
  37193. * @api public
  37194. */
  37195. function createDebug(namespace) {
  37196. function debug() {
  37197. // disabled?
  37198. if (!debug.enabled) return;
  37199. var self = debug;
  37200. // set `diff` timestamp
  37201. var curr = +new Date();
  37202. var ms = curr - (prevTime || curr);
  37203. self.diff = ms;
  37204. self.prev = prevTime;
  37205. self.curr = curr;
  37206. prevTime = curr;
  37207. // turn the `arguments` into a proper Array
  37208. var args = new Array(arguments.length);
  37209. for (var i = 0; i < args.length; i++) {
  37210. args[i] = arguments[i];
  37211. }
  37212. args[0] = exports.coerce(args[0]);
  37213. if ('string' !== typeof args[0]) {
  37214. // anything else let's inspect with %O
  37215. args.unshift('%O');
  37216. }
  37217. // apply any `formatters` transformations
  37218. var index = 0;
  37219. args[0] = args[0].replace(/%([a-zA-Z%])/g, function(match, format) {
  37220. // if we encounter an escaped % then don't increase the array index
  37221. if (match === '%%') return match;
  37222. index++;
  37223. var formatter = exports.formatters[format];
  37224. if ('function' === typeof formatter) {
  37225. var val = args[index];
  37226. match = formatter.call(self, val);
  37227. // now we need to remove `args[index]` since it's inlined in the `format`
  37228. args.splice(index, 1);
  37229. index--;
  37230. }
  37231. return match;
  37232. });
  37233. // apply env-specific formatting (colors, etc.)
  37234. exports.formatArgs.call(self, args);
  37235. var logFn = debug.log || exports.log || console.log.bind(console);
  37236. logFn.apply(self, args);
  37237. }
  37238. debug.namespace = namespace;
  37239. debug.enabled = exports.enabled(namespace);
  37240. debug.useColors = exports.useColors();
  37241. debug.color = selectColor(namespace);
  37242. // env-specific initialization logic for debug instances
  37243. if ('function' === typeof exports.init) {
  37244. exports.init(debug);
  37245. }
  37246. return debug;
  37247. }
  37248. /**
  37249. * Enables a debug mode by namespaces. This can include modes
  37250. * separated by a colon and wildcards.
  37251. *
  37252. * @param {String} namespaces
  37253. * @api public
  37254. */
  37255. function enable(namespaces) {
  37256. exports.save(namespaces);
  37257. exports.names = [];
  37258. exports.skips = [];
  37259. var split = (typeof namespaces === 'string' ? namespaces : '').split(/[\s,]+/);
  37260. var len = split.length;
  37261. for (var i = 0; i < len; i++) {
  37262. if (!split[i]) continue; // ignore empty strings
  37263. namespaces = split[i].replace(/\*/g, '.*?');
  37264. if (namespaces[0] === '-') {
  37265. exports.skips.push(new RegExp('^' + namespaces.substr(1) + '$'));
  37266. } else {
  37267. exports.names.push(new RegExp('^' + namespaces + '$'));
  37268. }
  37269. }
  37270. }
  37271. /**
  37272. * Disable debug output.
  37273. *
  37274. * @api public
  37275. */
  37276. function disable() {
  37277. exports.enable('');
  37278. }
  37279. /**
  37280. * Returns true if the given mode name is enabled, false otherwise.
  37281. *
  37282. * @param {String} name
  37283. * @return {Boolean}
  37284. * @api public
  37285. */
  37286. function enabled(name) {
  37287. var i, len;
  37288. for (i = 0, len = exports.skips.length; i < len; i++) {
  37289. if (exports.skips[i].test(name)) {
  37290. return false;
  37291. }
  37292. }
  37293. for (i = 0, len = exports.names.length; i < len; i++) {
  37294. if (exports.names[i].test(name)) {
  37295. return true;
  37296. }
  37297. }
  37298. return false;
  37299. }
  37300. /**
  37301. * Coerce `val`.
  37302. *
  37303. * @param {Mixed} val
  37304. * @return {Mixed}
  37305. * @api private
  37306. */
  37307. function coerce(val) {
  37308. if (val instanceof Error) return val.stack || val.message;
  37309. return val;
  37310. }
  37311. } (debug$f, debug$f.exports));
  37312. return debug$f.exports;
  37313. }
  37314. /**
  37315. * This is the web browser implementation of `debug()`.
  37316. *
  37317. * Expose `debug()` as the module.
  37318. */
  37319. var hasRequiredBrowser;
  37320. function requireBrowser () {
  37321. if (hasRequiredBrowser) return browser.exports;
  37322. hasRequiredBrowser = 1;
  37323. (function (module, exports) {
  37324. exports = module.exports = requireDebug();
  37325. exports.log = log;
  37326. exports.formatArgs = formatArgs;
  37327. exports.save = save;
  37328. exports.load = load;
  37329. exports.useColors = useColors;
  37330. exports.storage = 'undefined' != typeof chrome
  37331. && 'undefined' != typeof chrome.storage
  37332. ? chrome.storage.local
  37333. : localstorage();
  37334. /**
  37335. * Colors.
  37336. */
  37337. exports.colors = [
  37338. 'lightseagreen',
  37339. 'forestgreen',
  37340. 'goldenrod',
  37341. 'dodgerblue',
  37342. 'darkorchid',
  37343. 'crimson'
  37344. ];
  37345. /**
  37346. * Currently only WebKit-based Web Inspectors, Firefox >= v31,
  37347. * and the Firebug extension (any Firefox version) are known
  37348. * to support "%c" CSS customizations.
  37349. *
  37350. * TODO: add a `localStorage` variable to explicitly enable/disable colors
  37351. */
  37352. function useColors() {
  37353. // NB: In an Electron preload script, document will be defined but not fully
  37354. // initialized. Since we know we're in Chrome, we'll just detect this case
  37355. // explicitly
  37356. if (typeof window !== 'undefined' && window.process && window.process.type === 'renderer') {
  37357. return true;
  37358. }
  37359. // is webkit? http://stackoverflow.com/a/16459606/376773
  37360. // document is undefined in react-native: https://github.com/facebook/react-native/pull/1632
  37361. return (typeof document !== 'undefined' && document.documentElement && document.documentElement.style && document.documentElement.style.WebkitAppearance) ||
  37362. // is firebug? http://stackoverflow.com/a/398120/376773
  37363. (typeof window !== 'undefined' && window.console && (window.console.firebug || (window.console.exception && window.console.table))) ||
  37364. // is firefox >= v31?
  37365. // https://developer.mozilla.org/en-US/docs/Tools/Web_Console#Styling_messages
  37366. (typeof navigator !== 'undefined' && navigator.userAgent && navigator.userAgent.toLowerCase().match(/firefox\/(\d+)/) && parseInt(RegExp.$1, 10) >= 31) ||
  37367. // double check webkit in userAgent just in case we are in a worker
  37368. (typeof navigator !== 'undefined' && navigator.userAgent && navigator.userAgent.toLowerCase().match(/applewebkit\/(\d+)/));
  37369. }
  37370. /**
  37371. * Map %j to `JSON.stringify()`, since no Web Inspectors do that by default.
  37372. */
  37373. exports.formatters.j = function(v) {
  37374. try {
  37375. return JSON.stringify(v);
  37376. } catch (err) {
  37377. return '[UnexpectedJSONParseError]: ' + err.message;
  37378. }
  37379. };
  37380. /**
  37381. * Colorize log arguments if enabled.
  37382. *
  37383. * @api public
  37384. */
  37385. function formatArgs(args) {
  37386. var useColors = this.useColors;
  37387. args[0] = (useColors ? '%c' : '')
  37388. + this.namespace
  37389. + (useColors ? ' %c' : ' ')
  37390. + args[0]
  37391. + (useColors ? '%c ' : ' ')
  37392. + '+' + exports.humanize(this.diff);
  37393. if (!useColors) return;
  37394. var c = 'color: ' + this.color;
  37395. args.splice(1, 0, c, 'color: inherit');
  37396. // the final "%c" is somewhat tricky, because there could be other
  37397. // arguments passed either before or after the %c, so we need to
  37398. // figure out the correct index to insert the CSS into
  37399. var index = 0;
  37400. var lastC = 0;
  37401. args[0].replace(/%[a-zA-Z%]/g, function(match) {
  37402. if ('%%' === match) return;
  37403. index++;
  37404. if ('%c' === match) {
  37405. // we only are interested in the *last* %c
  37406. // (the user may have provided their own)
  37407. lastC = index;
  37408. }
  37409. });
  37410. args.splice(lastC, 0, c);
  37411. }
  37412. /**
  37413. * Invokes `console.log()` when available.
  37414. * No-op when `console.log` is not a "function".
  37415. *
  37416. * @api public
  37417. */
  37418. function log() {
  37419. // this hackery is required for IE8/9, where
  37420. // the `console.log` function doesn't have 'apply'
  37421. return 'object' === typeof console
  37422. && console.log
  37423. && Function.prototype.apply.call(console.log, console, arguments);
  37424. }
  37425. /**
  37426. * Save `namespaces`.
  37427. *
  37428. * @param {String} namespaces
  37429. * @api private
  37430. */
  37431. function save(namespaces) {
  37432. try {
  37433. if (null == namespaces) {
  37434. exports.storage.removeItem('debug');
  37435. } else {
  37436. exports.storage.debug = namespaces;
  37437. }
  37438. } catch(e) {}
  37439. }
  37440. /**
  37441. * Load `namespaces`.
  37442. *
  37443. * @return {String} returns the previously persisted debug modes
  37444. * @api private
  37445. */
  37446. function load() {
  37447. var r;
  37448. try {
  37449. r = exports.storage.debug;
  37450. } catch(e) {}
  37451. // If debug isn't set in LS, and we're in Electron, try to load $DEBUG
  37452. if (!r && typeof process !== 'undefined' && 'env' in process) {
  37453. r = process.env.DEBUG;
  37454. }
  37455. return r;
  37456. }
  37457. /**
  37458. * Enable namespaces listed in `localStorage.debug` initially.
  37459. */
  37460. exports.enable(load());
  37461. /**
  37462. * Localstorage attempts to return the localstorage.
  37463. *
  37464. * This is necessary because safari throws
  37465. * when a user disables cookies/localstorage
  37466. * and you attempt to access it.
  37467. *
  37468. * @return {LocalStorage}
  37469. * @api private
  37470. */
  37471. function localstorage() {
  37472. try {
  37473. return window.localStorage;
  37474. } catch (e) {}
  37475. }
  37476. } (browser, browser.exports));
  37477. return browser.exports;
  37478. }
  37479. var node = {exports: {}};
  37480. /**
  37481. * Module dependencies.
  37482. */
  37483. var hasRequiredNode;
  37484. function requireNode () {
  37485. if (hasRequiredNode) return node.exports;
  37486. hasRequiredNode = 1;
  37487. (function (module, exports) {
  37488. var tty = require$$0$3;
  37489. var util = require$$0$5;
  37490. /**
  37491. * This is the Node.js implementation of `debug()`.
  37492. *
  37493. * Expose `debug()` as the module.
  37494. */
  37495. exports = module.exports = requireDebug();
  37496. exports.init = init;
  37497. exports.log = log;
  37498. exports.formatArgs = formatArgs;
  37499. exports.save = save;
  37500. exports.load = load;
  37501. exports.useColors = useColors;
  37502. /**
  37503. * Colors.
  37504. */
  37505. exports.colors = [6, 2, 3, 4, 5, 1];
  37506. /**
  37507. * Build up the default `inspectOpts` object from the environment variables.
  37508. *
  37509. * $ DEBUG_COLORS=no DEBUG_DEPTH=10 DEBUG_SHOW_HIDDEN=enabled node script.js
  37510. */
  37511. exports.inspectOpts = Object.keys(process.env).filter(function (key) {
  37512. return /^debug_/i.test(key);
  37513. }).reduce(function (obj, key) {
  37514. // camel-case
  37515. var prop = key
  37516. .substring(6)
  37517. .toLowerCase()
  37518. .replace(/_([a-z])/g, function (_, k) { return k.toUpperCase() });
  37519. // coerce string value into JS value
  37520. var val = process.env[key];
  37521. if (/^(yes|on|true|enabled)$/i.test(val)) val = true;
  37522. else if (/^(no|off|false|disabled)$/i.test(val)) val = false;
  37523. else if (val === 'null') val = null;
  37524. else val = Number(val);
  37525. obj[prop] = val;
  37526. return obj;
  37527. }, {});
  37528. /**
  37529. * The file descriptor to write the `debug()` calls to.
  37530. * Set the `DEBUG_FD` env variable to override with another value. i.e.:
  37531. *
  37532. * $ DEBUG_FD=3 node script.js 3>debug.log
  37533. */
  37534. var fd = parseInt(process.env.DEBUG_FD, 10) || 2;
  37535. if (1 !== fd && 2 !== fd) {
  37536. util.deprecate(function(){}, 'except for stderr(2) and stdout(1), any other usage of DEBUG_FD is deprecated. Override debug.log if you want to use a different log function (https://git.io/debug_fd)')();
  37537. }
  37538. var stream = 1 === fd ? process.stdout :
  37539. 2 === fd ? process.stderr :
  37540. createWritableStdioStream(fd);
  37541. /**
  37542. * Is stdout a TTY? Colored output is enabled when `true`.
  37543. */
  37544. function useColors() {
  37545. return 'colors' in exports.inspectOpts
  37546. ? Boolean(exports.inspectOpts.colors)
  37547. : tty.isatty(fd);
  37548. }
  37549. /**
  37550. * Map %o to `util.inspect()`, all on a single line.
  37551. */
  37552. exports.formatters.o = function(v) {
  37553. this.inspectOpts.colors = this.useColors;
  37554. return util.inspect(v, this.inspectOpts)
  37555. .split('\n').map(function(str) {
  37556. return str.trim()
  37557. }).join(' ');
  37558. };
  37559. /**
  37560. * Map %o to `util.inspect()`, allowing multiple lines if needed.
  37561. */
  37562. exports.formatters.O = function(v) {
  37563. this.inspectOpts.colors = this.useColors;
  37564. return util.inspect(v, this.inspectOpts);
  37565. };
  37566. /**
  37567. * Adds ANSI color escape codes if enabled.
  37568. *
  37569. * @api public
  37570. */
  37571. function formatArgs(args) {
  37572. var name = this.namespace;
  37573. var useColors = this.useColors;
  37574. if (useColors) {
  37575. var c = this.color;
  37576. var prefix = ' \u001b[3' + c + ';1m' + name + ' ' + '\u001b[0m';
  37577. args[0] = prefix + args[0].split('\n').join('\n' + prefix);
  37578. args.push('\u001b[3' + c + 'm+' + exports.humanize(this.diff) + '\u001b[0m');
  37579. } else {
  37580. args[0] = new Date().toUTCString()
  37581. + ' ' + name + ' ' + args[0];
  37582. }
  37583. }
  37584. /**
  37585. * Invokes `util.format()` with the specified arguments and writes to `stream`.
  37586. */
  37587. function log() {
  37588. return stream.write(util.format.apply(util, arguments) + '\n');
  37589. }
  37590. /**
  37591. * Save `namespaces`.
  37592. *
  37593. * @param {String} namespaces
  37594. * @api private
  37595. */
  37596. function save(namespaces) {
  37597. if (null == namespaces) {
  37598. // If you set a process.env field to null or undefined, it gets cast to the
  37599. // string 'null' or 'undefined'. Just delete instead.
  37600. delete process.env.DEBUG;
  37601. } else {
  37602. process.env.DEBUG = namespaces;
  37603. }
  37604. }
  37605. /**
  37606. * Load `namespaces`.
  37607. *
  37608. * @return {String} returns the previously persisted debug modes
  37609. * @api private
  37610. */
  37611. function load() {
  37612. return process.env.DEBUG;
  37613. }
  37614. /**
  37615. * Copied from `node/src/node.js`.
  37616. *
  37617. * XXX: It's lame that node doesn't expose this API out-of-the-box. It also
  37618. * relies on the undocumented `tty_wrap.guessHandleType()` which is also lame.
  37619. */
  37620. function createWritableStdioStream (fd) {
  37621. var stream;
  37622. var tty_wrap = process.binding('tty_wrap');
  37623. // Note stream._type is used for test-module-load-list.js
  37624. switch (tty_wrap.guessHandleType(fd)) {
  37625. case 'TTY':
  37626. stream = new tty.WriteStream(fd);
  37627. stream._type = 'tty';
  37628. // Hack to have stream not keep the event loop alive.
  37629. // See https://github.com/joyent/node/issues/1726
  37630. if (stream._handle && stream._handle.unref) {
  37631. stream._handle.unref();
  37632. }
  37633. break;
  37634. case 'FILE':
  37635. var fs = require$$0__default;
  37636. stream = new fs.SyncWriteStream(fd, { autoClose: false });
  37637. stream._type = 'fs';
  37638. break;
  37639. case 'PIPE':
  37640. case 'TCP':
  37641. var net = require$$4$1;
  37642. stream = new net.Socket({
  37643. fd: fd,
  37644. readable: false,
  37645. writable: true
  37646. });
  37647. // FIXME Should probably have an option in net.Socket to create a
  37648. // stream from an existing fd which is writable only. But for now
  37649. // we'll just add this hack and set the `readable` member to false.
  37650. // Test: ./node test/fixtures/echo.js < /etc/passwd
  37651. stream.readable = false;
  37652. stream.read = null;
  37653. stream._type = 'pipe';
  37654. // FIXME Hack to have stream not keep the event loop alive.
  37655. // See https://github.com/joyent/node/issues/1726
  37656. if (stream._handle && stream._handle.unref) {
  37657. stream._handle.unref();
  37658. }
  37659. break;
  37660. default:
  37661. // Probably an error on in uv_guess_handle()
  37662. throw new Error('Implement me. Unknown stream file type!');
  37663. }
  37664. // For supporting legacy API we put the FD here.
  37665. stream.fd = fd;
  37666. stream._isStdio = true;
  37667. return stream;
  37668. }
  37669. /**
  37670. * Init logic for `debug` instances.
  37671. *
  37672. * Create a new `inspectOpts` object in case `useColors` is set
  37673. * differently for a particular `debug` instance.
  37674. */
  37675. function init (debug) {
  37676. debug.inspectOpts = {};
  37677. var keys = Object.keys(exports.inspectOpts);
  37678. for (var i = 0; i < keys.length; i++) {
  37679. debug.inspectOpts[keys[i]] = exports.inspectOpts[keys[i]];
  37680. }
  37681. }
  37682. /**
  37683. * Enable namespaces listed in `process.env.DEBUG` initially.
  37684. */
  37685. exports.enable(load());
  37686. } (node, node.exports));
  37687. return node.exports;
  37688. }
  37689. /**
  37690. * Detect Electron renderer process, which is node, but we should
  37691. * treat as a browser.
  37692. */
  37693. if (typeof process !== 'undefined' && process.type === 'renderer') {
  37694. src.exports = requireBrowser();
  37695. } else {
  37696. src.exports = requireNode();
  37697. }
  37698. var srcExports = src.exports;
  37699. /*!
  37700. * encodeurl
  37701. * Copyright(c) 2016 Douglas Christopher Wilson
  37702. * MIT Licensed
  37703. */
  37704. /**
  37705. * Module exports.
  37706. * @public
  37707. */
  37708. var encodeurl = encodeUrl$1;
  37709. /**
  37710. * RegExp to match non-URL code points, *after* encoding (i.e. not including "%")
  37711. * and including invalid escape sequences.
  37712. * @private
  37713. */
  37714. var ENCODE_CHARS_REGEXP = /(?:[^\x21\x25\x26-\x3B\x3D\x3F-\x5B\x5D\x5F\x61-\x7A\x7E]|%(?:[^0-9A-Fa-f]|[0-9A-Fa-f][^0-9A-Fa-f]|$))+/g;
  37715. /**
  37716. * RegExp to match unmatched surrogate pair.
  37717. * @private
  37718. */
  37719. var UNMATCHED_SURROGATE_PAIR_REGEXP = /(^|[^\uD800-\uDBFF])[\uDC00-\uDFFF]|[\uD800-\uDBFF]([^\uDC00-\uDFFF]|$)/g;
  37720. /**
  37721. * String to replace unmatched surrogate pair with.
  37722. * @private
  37723. */
  37724. var UNMATCHED_SURROGATE_PAIR_REPLACE = '$1\uFFFD$2';
  37725. /**
  37726. * Encode a URL to a percent-encoded form, excluding already-encoded sequences.
  37727. *
  37728. * This function will take an already-encoded URL and encode all the non-URL
  37729. * code points. This function will not encode the "%" character unless it is
  37730. * not part of a valid sequence (`%20` will be left as-is, but `%foo` will
  37731. * be encoded as `%25foo`).
  37732. *
  37733. * This encode is meant to be "safe" and does not throw errors. It will try as
  37734. * hard as it can to properly encode the given URL, including replacing any raw,
  37735. * unpaired surrogate pairs with the Unicode replacement character prior to
  37736. * encoding.
  37737. *
  37738. * @param {string} url
  37739. * @return {string}
  37740. * @public
  37741. */
  37742. function encodeUrl$1 (url) {
  37743. return String(url)
  37744. .replace(UNMATCHED_SURROGATE_PAIR_REGEXP, UNMATCHED_SURROGATE_PAIR_REPLACE)
  37745. .replace(ENCODE_CHARS_REGEXP, encodeURI)
  37746. }
  37747. /*!
  37748. * escape-html
  37749. * Copyright(c) 2012-2013 TJ Holowaychuk
  37750. * Copyright(c) 2015 Andreas Lubbe
  37751. * Copyright(c) 2015 Tiancheng "Timothy" Gu
  37752. * MIT Licensed
  37753. */
  37754. /**
  37755. * Module variables.
  37756. * @private
  37757. */
  37758. var matchHtmlRegExp = /["'&<>]/;
  37759. /**
  37760. * Module exports.
  37761. * @public
  37762. */
  37763. var escapeHtml_1 = escapeHtml$1;
  37764. /**
  37765. * Escape special characters in the given string of html.
  37766. *
  37767. * @param {string} string The string to escape for inserting into HTML
  37768. * @return {string}
  37769. * @public
  37770. */
  37771. function escapeHtml$1(string) {
  37772. var str = '' + string;
  37773. var match = matchHtmlRegExp.exec(str);
  37774. if (!match) {
  37775. return str;
  37776. }
  37777. var escape;
  37778. var html = '';
  37779. var index = 0;
  37780. var lastIndex = 0;
  37781. for (index = match.index; index < str.length; index++) {
  37782. switch (str.charCodeAt(index)) {
  37783. case 34: // "
  37784. escape = '&quot;';
  37785. break;
  37786. case 38: // &
  37787. escape = '&amp;';
  37788. break;
  37789. case 39: // '
  37790. escape = '&#39;';
  37791. break;
  37792. case 60: // <
  37793. escape = '&lt;';
  37794. break;
  37795. case 62: // >
  37796. escape = '&gt;';
  37797. break;
  37798. default:
  37799. continue;
  37800. }
  37801. if (lastIndex !== index) {
  37802. html += str.substring(lastIndex, index);
  37803. }
  37804. lastIndex = index + 1;
  37805. html += escape;
  37806. }
  37807. return lastIndex !== index
  37808. ? html + str.substring(lastIndex, index)
  37809. : html;
  37810. }
  37811. var escapeHtml$2 = /*@__PURE__*/getDefaultExportFromCjs(escapeHtml_1);
  37812. var onFinished$2 = {exports: {}};
  37813. /*!
  37814. * ee-first
  37815. * Copyright(c) 2014 Jonathan Ong
  37816. * MIT Licensed
  37817. */
  37818. /**
  37819. * Module exports.
  37820. * @public
  37821. */
  37822. var eeFirst = first$1;
  37823. /**
  37824. * Get the first event in a set of event emitters and event pairs.
  37825. *
  37826. * @param {array} stuff
  37827. * @param {function} done
  37828. * @public
  37829. */
  37830. function first$1(stuff, done) {
  37831. if (!Array.isArray(stuff))
  37832. throw new TypeError('arg must be an array of [ee, events...] arrays')
  37833. var cleanups = [];
  37834. for (var i = 0; i < stuff.length; i++) {
  37835. var arr = stuff[i];
  37836. if (!Array.isArray(arr) || arr.length < 2)
  37837. throw new TypeError('each array member must be [ee, events...]')
  37838. var ee = arr[0];
  37839. for (var j = 1; j < arr.length; j++) {
  37840. var event = arr[j];
  37841. var fn = listener(event, callback);
  37842. // listen to the event
  37843. ee.on(event, fn);
  37844. // push this listener to the list of cleanups
  37845. cleanups.push({
  37846. ee: ee,
  37847. event: event,
  37848. fn: fn,
  37849. });
  37850. }
  37851. }
  37852. function callback() {
  37853. cleanup();
  37854. done.apply(null, arguments);
  37855. }
  37856. function cleanup() {
  37857. var x;
  37858. for (var i = 0; i < cleanups.length; i++) {
  37859. x = cleanups[i];
  37860. x.ee.removeListener(x.event, x.fn);
  37861. }
  37862. }
  37863. function thunk(fn) {
  37864. done = fn;
  37865. }
  37866. thunk.cancel = cleanup;
  37867. return thunk
  37868. }
  37869. /**
  37870. * Create the event listener.
  37871. * @private
  37872. */
  37873. function listener(event, done) {
  37874. return function onevent(arg1) {
  37875. var args = new Array(arguments.length);
  37876. var ee = this;
  37877. var err = event === 'error'
  37878. ? arg1
  37879. : null;
  37880. // copy args to prevent arguments escaping scope
  37881. for (var i = 0; i < args.length; i++) {
  37882. args[i] = arguments[i];
  37883. }
  37884. done(err, ee, event, args);
  37885. }
  37886. }
  37887. /*!
  37888. * on-finished
  37889. * Copyright(c) 2013 Jonathan Ong
  37890. * Copyright(c) 2014 Douglas Christopher Wilson
  37891. * MIT Licensed
  37892. */
  37893. /**
  37894. * Module exports.
  37895. * @public
  37896. */
  37897. onFinished$2.exports = onFinished$1;
  37898. onFinished$2.exports.isFinished = isFinished$1;
  37899. /**
  37900. * Module dependencies.
  37901. * @private
  37902. */
  37903. var first = eeFirst;
  37904. /**
  37905. * Variables.
  37906. * @private
  37907. */
  37908. /* istanbul ignore next */
  37909. var defer$2 = typeof setImmediate === 'function'
  37910. ? setImmediate
  37911. : function(fn){ process.nextTick(fn.bind.apply(fn, arguments)); };
  37912. /**
  37913. * Invoke callback when the response has finished, useful for
  37914. * cleaning up resources afterwards.
  37915. *
  37916. * @param {object} msg
  37917. * @param {function} listener
  37918. * @return {object}
  37919. * @public
  37920. */
  37921. function onFinished$1(msg, listener) {
  37922. if (isFinished$1(msg) !== false) {
  37923. defer$2(listener, null, msg);
  37924. return msg
  37925. }
  37926. // attach the listener to the message
  37927. attachListener(msg, listener);
  37928. return msg
  37929. }
  37930. /**
  37931. * Determine if message is already finished.
  37932. *
  37933. * @param {object} msg
  37934. * @return {boolean}
  37935. * @public
  37936. */
  37937. function isFinished$1(msg) {
  37938. var socket = msg.socket;
  37939. if (typeof msg.finished === 'boolean') {
  37940. // OutgoingMessage
  37941. return Boolean(msg.finished || (socket && !socket.writable))
  37942. }
  37943. if (typeof msg.complete === 'boolean') {
  37944. // IncomingMessage
  37945. return Boolean(msg.upgrade || !socket || !socket.readable || (msg.complete && !msg.readable))
  37946. }
  37947. // don't know
  37948. return undefined
  37949. }
  37950. /**
  37951. * Attach a finished listener to the message.
  37952. *
  37953. * @param {object} msg
  37954. * @param {function} callback
  37955. * @private
  37956. */
  37957. function attachFinishedListener(msg, callback) {
  37958. var eeMsg;
  37959. var eeSocket;
  37960. var finished = false;
  37961. function onFinish(error) {
  37962. eeMsg.cancel();
  37963. eeSocket.cancel();
  37964. finished = true;
  37965. callback(error);
  37966. }
  37967. // finished on first message event
  37968. eeMsg = eeSocket = first([[msg, 'end', 'finish']], onFinish);
  37969. function onSocket(socket) {
  37970. // remove listener
  37971. msg.removeListener('socket', onSocket);
  37972. if (finished) return
  37973. if (eeMsg !== eeSocket) return
  37974. // finished on first socket event
  37975. eeSocket = first([[socket, 'error', 'close']], onFinish);
  37976. }
  37977. if (msg.socket) {
  37978. // socket already assigned
  37979. onSocket(msg.socket);
  37980. return
  37981. }
  37982. // wait for socket to be assigned
  37983. msg.on('socket', onSocket);
  37984. if (msg.socket === undefined) {
  37985. // node.js 0.8 patch
  37986. patchAssignSocket(msg, onSocket);
  37987. }
  37988. }
  37989. /**
  37990. * Attach the listener to the message.
  37991. *
  37992. * @param {object} msg
  37993. * @return {function}
  37994. * @private
  37995. */
  37996. function attachListener(msg, listener) {
  37997. var attached = msg.__onFinished;
  37998. // create a private single listener with queue
  37999. if (!attached || !attached.queue) {
  38000. attached = msg.__onFinished = createListener(msg);
  38001. attachFinishedListener(msg, attached);
  38002. }
  38003. attached.queue.push(listener);
  38004. }
  38005. /**
  38006. * Create listener on message.
  38007. *
  38008. * @param {object} msg
  38009. * @return {function}
  38010. * @private
  38011. */
  38012. function createListener(msg) {
  38013. function listener(err) {
  38014. if (msg.__onFinished === listener) msg.__onFinished = null;
  38015. if (!listener.queue) return
  38016. var queue = listener.queue;
  38017. listener.queue = null;
  38018. for (var i = 0; i < queue.length; i++) {
  38019. queue[i](err, msg);
  38020. }
  38021. }
  38022. listener.queue = [];
  38023. return listener
  38024. }
  38025. /**
  38026. * Patch ServerResponse.prototype.assignSocket for node.js 0.8.
  38027. *
  38028. * @param {ServerResponse} res
  38029. * @param {function} callback
  38030. * @private
  38031. */
  38032. function patchAssignSocket(res, callback) {
  38033. var assignSocket = res.assignSocket;
  38034. if (typeof assignSocket !== 'function') return
  38035. // res.on('socket', callback) is broken in 0.8
  38036. res.assignSocket = function _assignSocket(socket) {
  38037. assignSocket.call(this, socket);
  38038. callback(socket);
  38039. };
  38040. }
  38041. var onFinishedExports = onFinished$2.exports;
  38042. var parseurl$1 = {exports: {}};
  38043. /*!
  38044. * parseurl
  38045. * Copyright(c) 2014 Jonathan Ong
  38046. * Copyright(c) 2014-2017 Douglas Christopher Wilson
  38047. * MIT Licensed
  38048. */
  38049. /**
  38050. * Module dependencies.
  38051. * @private
  38052. */
  38053. var url$3 = require$$0$9;
  38054. var parse$8 = url$3.parse;
  38055. var Url = url$3.Url;
  38056. /**
  38057. * Module exports.
  38058. * @public
  38059. */
  38060. parseurl$1.exports = parseurl;
  38061. parseurl$1.exports.original = originalurl;
  38062. /**
  38063. * Parse the `req` url with memoization.
  38064. *
  38065. * @param {ServerRequest} req
  38066. * @return {Object}
  38067. * @public
  38068. */
  38069. function parseurl (req) {
  38070. var url = req.url;
  38071. if (url === undefined) {
  38072. // URL is undefined
  38073. return undefined
  38074. }
  38075. var parsed = req._parsedUrl;
  38076. if (fresh(url, parsed)) {
  38077. // Return cached URL parse
  38078. return parsed
  38079. }
  38080. // Parse the URL
  38081. parsed = fastparse(url);
  38082. parsed._raw = url;
  38083. return (req._parsedUrl = parsed)
  38084. }
  38085. /**
  38086. * Parse the `req` original url with fallback and memoization.
  38087. *
  38088. * @param {ServerRequest} req
  38089. * @return {Object}
  38090. * @public
  38091. */
  38092. function originalurl (req) {
  38093. var url = req.originalUrl;
  38094. if (typeof url !== 'string') {
  38095. // Fallback
  38096. return parseurl(req)
  38097. }
  38098. var parsed = req._parsedOriginalUrl;
  38099. if (fresh(url, parsed)) {
  38100. // Return cached URL parse
  38101. return parsed
  38102. }
  38103. // Parse the URL
  38104. parsed = fastparse(url);
  38105. parsed._raw = url;
  38106. return (req._parsedOriginalUrl = parsed)
  38107. }
  38108. /**
  38109. * Parse the `str` url with fast-path short-cut.
  38110. *
  38111. * @param {string} str
  38112. * @return {Object}
  38113. * @private
  38114. */
  38115. function fastparse (str) {
  38116. if (typeof str !== 'string' || str.charCodeAt(0) !== 0x2f /* / */) {
  38117. return parse$8(str)
  38118. }
  38119. var pathname = str;
  38120. var query = null;
  38121. var search = null;
  38122. // This takes the regexp from https://github.com/joyent/node/pull/7878
  38123. // Which is /^(\/[^?#\s]*)(\?[^#\s]*)?$/
  38124. // And unrolls it into a for loop
  38125. for (var i = 1; i < str.length; i++) {
  38126. switch (str.charCodeAt(i)) {
  38127. case 0x3f: /* ? */
  38128. if (search === null) {
  38129. pathname = str.substring(0, i);
  38130. query = str.substring(i + 1);
  38131. search = str.substring(i);
  38132. }
  38133. break
  38134. case 0x09: /* \t */
  38135. case 0x0a: /* \n */
  38136. case 0x0c: /* \f */
  38137. case 0x0d: /* \r */
  38138. case 0x20: /* */
  38139. case 0x23: /* # */
  38140. case 0xa0:
  38141. case 0xfeff:
  38142. return parse$8(str)
  38143. }
  38144. }
  38145. var url = Url !== undefined
  38146. ? new Url()
  38147. : {};
  38148. url.path = str;
  38149. url.href = str;
  38150. url.pathname = pathname;
  38151. if (search !== null) {
  38152. url.query = query;
  38153. url.search = search;
  38154. }
  38155. return url
  38156. }
  38157. /**
  38158. * Determine if parsed is still fresh for url.
  38159. *
  38160. * @param {string} url
  38161. * @param {object} parsedUrl
  38162. * @return {boolean}
  38163. * @private
  38164. */
  38165. function fresh (url, parsedUrl) {
  38166. return typeof parsedUrl === 'object' &&
  38167. parsedUrl !== null &&
  38168. (Url === undefined || parsedUrl instanceof Url) &&
  38169. parsedUrl._raw === url
  38170. }
  38171. var parseurlExports = parseurl$1.exports;
  38172. var require$$0$1 = {
  38173. "100": "Continue",
  38174. "101": "Switching Protocols",
  38175. "102": "Processing",
  38176. "103": "Early Hints",
  38177. "200": "OK",
  38178. "201": "Created",
  38179. "202": "Accepted",
  38180. "203": "Non-Authoritative Information",
  38181. "204": "No Content",
  38182. "205": "Reset Content",
  38183. "206": "Partial Content",
  38184. "207": "Multi-Status",
  38185. "208": "Already Reported",
  38186. "226": "IM Used",
  38187. "300": "Multiple Choices",
  38188. "301": "Moved Permanently",
  38189. "302": "Found",
  38190. "303": "See Other",
  38191. "304": "Not Modified",
  38192. "305": "Use Proxy",
  38193. "306": "(Unused)",
  38194. "307": "Temporary Redirect",
  38195. "308": "Permanent Redirect",
  38196. "400": "Bad Request",
  38197. "401": "Unauthorized",
  38198. "402": "Payment Required",
  38199. "403": "Forbidden",
  38200. "404": "Not Found",
  38201. "405": "Method Not Allowed",
  38202. "406": "Not Acceptable",
  38203. "407": "Proxy Authentication Required",
  38204. "408": "Request Timeout",
  38205. "409": "Conflict",
  38206. "410": "Gone",
  38207. "411": "Length Required",
  38208. "412": "Precondition Failed",
  38209. "413": "Payload Too Large",
  38210. "414": "URI Too Long",
  38211. "415": "Unsupported Media Type",
  38212. "416": "Range Not Satisfiable",
  38213. "417": "Expectation Failed",
  38214. "418": "I'm a teapot",
  38215. "421": "Misdirected Request",
  38216. "422": "Unprocessable Entity",
  38217. "423": "Locked",
  38218. "424": "Failed Dependency",
  38219. "425": "Unordered Collection",
  38220. "426": "Upgrade Required",
  38221. "428": "Precondition Required",
  38222. "429": "Too Many Requests",
  38223. "431": "Request Header Fields Too Large",
  38224. "451": "Unavailable For Legal Reasons",
  38225. "500": "Internal Server Error",
  38226. "501": "Not Implemented",
  38227. "502": "Bad Gateway",
  38228. "503": "Service Unavailable",
  38229. "504": "Gateway Timeout",
  38230. "505": "HTTP Version Not Supported",
  38231. "506": "Variant Also Negotiates",
  38232. "507": "Insufficient Storage",
  38233. "508": "Loop Detected",
  38234. "509": "Bandwidth Limit Exceeded",
  38235. "510": "Not Extended",
  38236. "511": "Network Authentication Required"
  38237. };
  38238. /*!
  38239. * statuses
  38240. * Copyright(c) 2014 Jonathan Ong
  38241. * Copyright(c) 2016 Douglas Christopher Wilson
  38242. * MIT Licensed
  38243. */
  38244. /**
  38245. * Module dependencies.
  38246. * @private
  38247. */
  38248. var codes = require$$0$1;
  38249. /**
  38250. * Module exports.
  38251. * @public
  38252. */
  38253. var statuses$1 = status;
  38254. // status code to message map
  38255. status.STATUS_CODES = codes;
  38256. // array of status codes
  38257. status.codes = populateStatusesMap(status, codes);
  38258. // status codes for redirects
  38259. status.redirect = {
  38260. 300: true,
  38261. 301: true,
  38262. 302: true,
  38263. 303: true,
  38264. 305: true,
  38265. 307: true,
  38266. 308: true
  38267. };
  38268. // status codes for empty bodies
  38269. status.empty = {
  38270. 204: true,
  38271. 205: true,
  38272. 304: true
  38273. };
  38274. // status codes for when you should retry the request
  38275. status.retry = {
  38276. 502: true,
  38277. 503: true,
  38278. 504: true
  38279. };
  38280. /**
  38281. * Populate the statuses map for given codes.
  38282. * @private
  38283. */
  38284. function populateStatusesMap (statuses, codes) {
  38285. var arr = [];
  38286. Object.keys(codes).forEach(function forEachCode (code) {
  38287. var message = codes[code];
  38288. var status = Number(code);
  38289. // Populate properties
  38290. statuses[status] = message;
  38291. statuses[message] = status;
  38292. statuses[message.toLowerCase()] = status;
  38293. // Add to array
  38294. arr.push(status);
  38295. });
  38296. return arr
  38297. }
  38298. /**
  38299. * Get the status code.
  38300. *
  38301. * Given a number, this will throw if it is not a known status
  38302. * code, otherwise the code will be returned. Given a string,
  38303. * the string will be parsed for a number and return the code
  38304. * if valid, otherwise will lookup the code assuming this is
  38305. * the status message.
  38306. *
  38307. * @param {string|number} code
  38308. * @returns {number}
  38309. * @public
  38310. */
  38311. function status (code) {
  38312. if (typeof code === 'number') {
  38313. if (!status[code]) throw new Error('invalid status code: ' + code)
  38314. return code
  38315. }
  38316. if (typeof code !== 'string') {
  38317. throw new TypeError('code must be a number or string')
  38318. }
  38319. // '403'
  38320. var n = parseInt(code, 10);
  38321. if (!isNaN(n)) {
  38322. if (!status[n]) throw new Error('invalid status code: ' + n)
  38323. return n
  38324. }
  38325. n = status[code.toLowerCase()];
  38326. if (!n) throw new Error('invalid status message: "' + code + '"')
  38327. return n
  38328. }
  38329. /*!
  38330. * unpipe
  38331. * Copyright(c) 2015 Douglas Christopher Wilson
  38332. * MIT Licensed
  38333. */
  38334. /**
  38335. * Module exports.
  38336. * @public
  38337. */
  38338. var unpipe_1 = unpipe$1;
  38339. /**
  38340. * Determine if there are Node.js pipe-like data listeners.
  38341. * @private
  38342. */
  38343. function hasPipeDataListeners(stream) {
  38344. var listeners = stream.listeners('data');
  38345. for (var i = 0; i < listeners.length; i++) {
  38346. if (listeners[i].name === 'ondata') {
  38347. return true
  38348. }
  38349. }
  38350. return false
  38351. }
  38352. /**
  38353. * Unpipe a stream from all destinations.
  38354. *
  38355. * @param {object} stream
  38356. * @public
  38357. */
  38358. function unpipe$1(stream) {
  38359. if (!stream) {
  38360. throw new TypeError('argument stream is required')
  38361. }
  38362. if (typeof stream.unpipe === 'function') {
  38363. // new-style
  38364. stream.unpipe();
  38365. return
  38366. }
  38367. // Node.js 0.8 hack
  38368. if (!hasPipeDataListeners(stream)) {
  38369. return
  38370. }
  38371. var listener;
  38372. var listeners = stream.listeners('close');
  38373. for (var i = 0; i < listeners.length; i++) {
  38374. listener = listeners[i];
  38375. if (listener.name !== 'cleanup' && listener.name !== 'onclose') {
  38376. continue
  38377. }
  38378. // invoke the listener
  38379. listener.call(stream);
  38380. }
  38381. }
  38382. /*!
  38383. * finalhandler
  38384. * Copyright(c) 2014-2017 Douglas Christopher Wilson
  38385. * MIT Licensed
  38386. */
  38387. /**
  38388. * Module dependencies.
  38389. * @private
  38390. */
  38391. var debug$e = srcExports('finalhandler');
  38392. var encodeUrl = encodeurl;
  38393. var escapeHtml = escapeHtml_1;
  38394. var onFinished = onFinishedExports;
  38395. var parseUrl$2 = parseurlExports;
  38396. var statuses = statuses$1;
  38397. var unpipe = unpipe_1;
  38398. /**
  38399. * Module variables.
  38400. * @private
  38401. */
  38402. var DOUBLE_SPACE_REGEXP = /\x20{2}/g;
  38403. var NEWLINE_REGEXP = /\n/g;
  38404. /* istanbul ignore next */
  38405. var defer$1 = typeof setImmediate === 'function'
  38406. ? setImmediate
  38407. : function (fn) { process.nextTick(fn.bind.apply(fn, arguments)); };
  38408. var isFinished = onFinished.isFinished;
  38409. /**
  38410. * Create a minimal HTML document.
  38411. *
  38412. * @param {string} message
  38413. * @private
  38414. */
  38415. function createHtmlDocument (message) {
  38416. var body = escapeHtml(message)
  38417. .replace(NEWLINE_REGEXP, '<br>')
  38418. .replace(DOUBLE_SPACE_REGEXP, ' &nbsp;');
  38419. return '<!DOCTYPE html>\n' +
  38420. '<html lang="en">\n' +
  38421. '<head>\n' +
  38422. '<meta charset="utf-8">\n' +
  38423. '<title>Error</title>\n' +
  38424. '</head>\n' +
  38425. '<body>\n' +
  38426. '<pre>' + body + '</pre>\n' +
  38427. '</body>\n' +
  38428. '</html>\n'
  38429. }
  38430. /**
  38431. * Module exports.
  38432. * @public
  38433. */
  38434. var finalhandler_1 = finalhandler$1;
  38435. /**
  38436. * Create a function to handle the final response.
  38437. *
  38438. * @param {Request} req
  38439. * @param {Response} res
  38440. * @param {Object} [options]
  38441. * @return {Function}
  38442. * @public
  38443. */
  38444. function finalhandler$1 (req, res, options) {
  38445. var opts = options || {};
  38446. // get environment
  38447. var env = opts.env || process.env.NODE_ENV || 'development';
  38448. // get error callback
  38449. var onerror = opts.onerror;
  38450. return function (err) {
  38451. var headers;
  38452. var msg;
  38453. var status;
  38454. // ignore 404 on in-flight response
  38455. if (!err && headersSent(res)) {
  38456. debug$e('cannot 404 after headers sent');
  38457. return
  38458. }
  38459. // unhandled error
  38460. if (err) {
  38461. // respect status code from error
  38462. status = getErrorStatusCode(err);
  38463. if (status === undefined) {
  38464. // fallback to status code on response
  38465. status = getResponseStatusCode(res);
  38466. } else {
  38467. // respect headers from error
  38468. headers = getErrorHeaders(err);
  38469. }
  38470. // get error message
  38471. msg = getErrorMessage(err, status, env);
  38472. } else {
  38473. // not found
  38474. status = 404;
  38475. msg = 'Cannot ' + req.method + ' ' + encodeUrl(getResourceName(req));
  38476. }
  38477. debug$e('default %s', status);
  38478. // schedule onerror callback
  38479. if (err && onerror) {
  38480. defer$1(onerror, err, req, res);
  38481. }
  38482. // cannot actually respond
  38483. if (headersSent(res)) {
  38484. debug$e('cannot %d after headers sent', status);
  38485. req.socket.destroy();
  38486. return
  38487. }
  38488. // send response
  38489. send$2(req, res, status, headers, msg);
  38490. }
  38491. }
  38492. /**
  38493. * Get headers from Error object.
  38494. *
  38495. * @param {Error} err
  38496. * @return {object}
  38497. * @private
  38498. */
  38499. function getErrorHeaders (err) {
  38500. if (!err.headers || typeof err.headers !== 'object') {
  38501. return undefined
  38502. }
  38503. var headers = Object.create(null);
  38504. var keys = Object.keys(err.headers);
  38505. for (var i = 0; i < keys.length; i++) {
  38506. var key = keys[i];
  38507. headers[key] = err.headers[key];
  38508. }
  38509. return headers
  38510. }
  38511. /**
  38512. * Get message from Error object, fallback to status message.
  38513. *
  38514. * @param {Error} err
  38515. * @param {number} status
  38516. * @param {string} env
  38517. * @return {string}
  38518. * @private
  38519. */
  38520. function getErrorMessage (err, status, env) {
  38521. var msg;
  38522. if (env !== 'production') {
  38523. // use err.stack, which typically includes err.message
  38524. msg = err.stack;
  38525. // fallback to err.toString() when possible
  38526. if (!msg && typeof err.toString === 'function') {
  38527. msg = err.toString();
  38528. }
  38529. }
  38530. return msg || statuses[status]
  38531. }
  38532. /**
  38533. * Get status code from Error object.
  38534. *
  38535. * @param {Error} err
  38536. * @return {number}
  38537. * @private
  38538. */
  38539. function getErrorStatusCode (err) {
  38540. // check err.status
  38541. if (typeof err.status === 'number' && err.status >= 400 && err.status < 600) {
  38542. return err.status
  38543. }
  38544. // check err.statusCode
  38545. if (typeof err.statusCode === 'number' && err.statusCode >= 400 && err.statusCode < 600) {
  38546. return err.statusCode
  38547. }
  38548. return undefined
  38549. }
  38550. /**
  38551. * Get resource name for the request.
  38552. *
  38553. * This is typically just the original pathname of the request
  38554. * but will fallback to "resource" is that cannot be determined.
  38555. *
  38556. * @param {IncomingMessage} req
  38557. * @return {string}
  38558. * @private
  38559. */
  38560. function getResourceName (req) {
  38561. try {
  38562. return parseUrl$2.original(req).pathname
  38563. } catch (e) {
  38564. return 'resource'
  38565. }
  38566. }
  38567. /**
  38568. * Get status code from response.
  38569. *
  38570. * @param {OutgoingMessage} res
  38571. * @return {number}
  38572. * @private
  38573. */
  38574. function getResponseStatusCode (res) {
  38575. var status = res.statusCode;
  38576. // default status code to 500 if outside valid range
  38577. if (typeof status !== 'number' || status < 400 || status > 599) {
  38578. status = 500;
  38579. }
  38580. return status
  38581. }
  38582. /**
  38583. * Determine if the response headers have been sent.
  38584. *
  38585. * @param {object} res
  38586. * @returns {boolean}
  38587. * @private
  38588. */
  38589. function headersSent (res) {
  38590. return typeof res.headersSent !== 'boolean'
  38591. ? Boolean(res._header)
  38592. : res.headersSent
  38593. }
  38594. /**
  38595. * Send response.
  38596. *
  38597. * @param {IncomingMessage} req
  38598. * @param {OutgoingMessage} res
  38599. * @param {number} status
  38600. * @param {object} headers
  38601. * @param {string} message
  38602. * @private
  38603. */
  38604. function send$2 (req, res, status, headers, message) {
  38605. function write () {
  38606. // response body
  38607. var body = createHtmlDocument(message);
  38608. // response status
  38609. res.statusCode = status;
  38610. res.statusMessage = statuses[status];
  38611. // response headers
  38612. setHeaders(res, headers);
  38613. // security headers
  38614. res.setHeader('Content-Security-Policy', "default-src 'none'");
  38615. res.setHeader('X-Content-Type-Options', 'nosniff');
  38616. // standard headers
  38617. res.setHeader('Content-Type', 'text/html; charset=utf-8');
  38618. res.setHeader('Content-Length', Buffer.byteLength(body, 'utf8'));
  38619. if (req.method === 'HEAD') {
  38620. res.end();
  38621. return
  38622. }
  38623. res.end(body, 'utf8');
  38624. }
  38625. if (isFinished(req)) {
  38626. write();
  38627. return
  38628. }
  38629. // unpipe everything from the request
  38630. unpipe(req);
  38631. // flush the request
  38632. onFinished(req, write);
  38633. req.resume();
  38634. }
  38635. /**
  38636. * Set response headers from an object.
  38637. *
  38638. * @param {OutgoingMessage} res
  38639. * @param {object} headers
  38640. * @private
  38641. */
  38642. function setHeaders (res, headers) {
  38643. if (!headers) {
  38644. return
  38645. }
  38646. var keys = Object.keys(headers);
  38647. for (var i = 0; i < keys.length; i++) {
  38648. var key = keys[i];
  38649. res.setHeader(key, headers[key]);
  38650. }
  38651. }
  38652. var utilsMerge = {exports: {}};
  38653. /**
  38654. * Merge object b with object a.
  38655. *
  38656. * var a = { foo: 'bar' }
  38657. * , b = { bar: 'baz' };
  38658. *
  38659. * merge(a, b);
  38660. * // => { foo: 'bar', bar: 'baz' }
  38661. *
  38662. * @param {Object} a
  38663. * @param {Object} b
  38664. * @return {Object}
  38665. * @api public
  38666. */
  38667. (function (module, exports) {
  38668. module.exports = function(a, b){
  38669. if (a && b) {
  38670. for (var key in b) {
  38671. a[key] = b[key];
  38672. }
  38673. }
  38674. return a;
  38675. };
  38676. } (utilsMerge));
  38677. var utilsMergeExports = utilsMerge.exports;
  38678. /*!
  38679. * connect
  38680. * Copyright(c) 2010 Sencha Inc.
  38681. * Copyright(c) 2011 TJ Holowaychuk
  38682. * Copyright(c) 2015 Douglas Christopher Wilson
  38683. * MIT Licensed
  38684. */
  38685. /**
  38686. * Module dependencies.
  38687. * @private
  38688. */
  38689. var debug$d = srcExports('connect:dispatcher');
  38690. var EventEmitter$3 = require$$0$7.EventEmitter;
  38691. var finalhandler = finalhandler_1;
  38692. var http$4 = require$$1;
  38693. var merge = utilsMergeExports;
  38694. var parseUrl$1 = parseurlExports;
  38695. /**
  38696. * Module exports.
  38697. * @public
  38698. */
  38699. var connect = createServer$1;
  38700. /**
  38701. * Module variables.
  38702. * @private
  38703. */
  38704. var env = process.env.NODE_ENV || 'development';
  38705. var proto = {};
  38706. /* istanbul ignore next */
  38707. var defer = typeof setImmediate === 'function'
  38708. ? setImmediate
  38709. : function(fn){ process.nextTick(fn.bind.apply(fn, arguments)); };
  38710. /**
  38711. * Create a new connect server.
  38712. *
  38713. * @return {function}
  38714. * @public
  38715. */
  38716. function createServer$1() {
  38717. function app(req, res, next){ app.handle(req, res, next); }
  38718. merge(app, proto);
  38719. merge(app, EventEmitter$3.prototype);
  38720. app.route = '/';
  38721. app.stack = [];
  38722. return app;
  38723. }
  38724. /**
  38725. * Utilize the given middleware `handle` to the given `route`,
  38726. * defaulting to _/_. This "route" is the mount-point for the
  38727. * middleware, when given a value other than _/_ the middleware
  38728. * is only effective when that segment is present in the request's
  38729. * pathname.
  38730. *
  38731. * For example if we were to mount a function at _/admin_, it would
  38732. * be invoked on _/admin_, and _/admin/settings_, however it would
  38733. * not be invoked for _/_, or _/posts_.
  38734. *
  38735. * @param {String|Function|Server} route, callback or server
  38736. * @param {Function|Server} callback or server
  38737. * @return {Server} for chaining
  38738. * @public
  38739. */
  38740. proto.use = function use(route, fn) {
  38741. var handle = fn;
  38742. var path = route;
  38743. // default route to '/'
  38744. if (typeof route !== 'string') {
  38745. handle = route;
  38746. path = '/';
  38747. }
  38748. // wrap sub-apps
  38749. if (typeof handle.handle === 'function') {
  38750. var server = handle;
  38751. server.route = path;
  38752. handle = function (req, res, next) {
  38753. server.handle(req, res, next);
  38754. };
  38755. }
  38756. // wrap vanilla http.Servers
  38757. if (handle instanceof http$4.Server) {
  38758. handle = handle.listeners('request')[0];
  38759. }
  38760. // strip trailing slash
  38761. if (path[path.length - 1] === '/') {
  38762. path = path.slice(0, -1);
  38763. }
  38764. // add the middleware
  38765. debug$d('use %s %s', path || '/', handle.name || 'anonymous');
  38766. this.stack.push({ route: path, handle: handle });
  38767. return this;
  38768. };
  38769. /**
  38770. * Handle server requests, punting them down
  38771. * the middleware stack.
  38772. *
  38773. * @private
  38774. */
  38775. proto.handle = function handle(req, res, out) {
  38776. var index = 0;
  38777. var protohost = getProtohost(req.url) || '';
  38778. var removed = '';
  38779. var slashAdded = false;
  38780. var stack = this.stack;
  38781. // final function handler
  38782. var done = out || finalhandler(req, res, {
  38783. env: env,
  38784. onerror: logerror
  38785. });
  38786. // store the original URL
  38787. req.originalUrl = req.originalUrl || req.url;
  38788. function next(err) {
  38789. if (slashAdded) {
  38790. req.url = req.url.substr(1);
  38791. slashAdded = false;
  38792. }
  38793. if (removed.length !== 0) {
  38794. req.url = protohost + removed + req.url.substr(protohost.length);
  38795. removed = '';
  38796. }
  38797. // next callback
  38798. var layer = stack[index++];
  38799. // all done
  38800. if (!layer) {
  38801. defer(done, err);
  38802. return;
  38803. }
  38804. // route data
  38805. var path = parseUrl$1(req).pathname || '/';
  38806. var route = layer.route;
  38807. // skip this layer if the route doesn't match
  38808. if (path.toLowerCase().substr(0, route.length) !== route.toLowerCase()) {
  38809. return next(err);
  38810. }
  38811. // skip if route match does not border "/", ".", or end
  38812. var c = path.length > route.length && path[route.length];
  38813. if (c && c !== '/' && c !== '.') {
  38814. return next(err);
  38815. }
  38816. // trim off the part of the url that matches the route
  38817. if (route.length !== 0 && route !== '/') {
  38818. removed = route;
  38819. req.url = protohost + req.url.substr(protohost.length + removed.length);
  38820. // ensure leading slash
  38821. if (!protohost && req.url[0] !== '/') {
  38822. req.url = '/' + req.url;
  38823. slashAdded = true;
  38824. }
  38825. }
  38826. // call the layer handle
  38827. call(layer.handle, route, err, req, res, next);
  38828. }
  38829. next();
  38830. };
  38831. /**
  38832. * Listen for connections.
  38833. *
  38834. * This method takes the same arguments
  38835. * as node's `http.Server#listen()`.
  38836. *
  38837. * HTTP and HTTPS:
  38838. *
  38839. * If you run your application both as HTTP
  38840. * and HTTPS you may wrap them individually,
  38841. * since your Connect "server" is really just
  38842. * a JavaScript `Function`.
  38843. *
  38844. * var connect = require('connect')
  38845. * , http = require('http')
  38846. * , https = require('https');
  38847. *
  38848. * var app = connect();
  38849. *
  38850. * http.createServer(app).listen(80);
  38851. * https.createServer(options, app).listen(443);
  38852. *
  38853. * @return {http.Server}
  38854. * @api public
  38855. */
  38856. proto.listen = function listen() {
  38857. var server = http$4.createServer(this);
  38858. return server.listen.apply(server, arguments);
  38859. };
  38860. /**
  38861. * Invoke a route handle.
  38862. * @private
  38863. */
  38864. function call(handle, route, err, req, res, next) {
  38865. var arity = handle.length;
  38866. var error = err;
  38867. var hasError = Boolean(err);
  38868. debug$d('%s %s : %s', handle.name || '<anonymous>', route, req.originalUrl);
  38869. try {
  38870. if (hasError && arity === 4) {
  38871. // error-handling middleware
  38872. handle(err, req, res, next);
  38873. return;
  38874. } else if (!hasError && arity < 4) {
  38875. // request-handling middleware
  38876. handle(req, res, next);
  38877. return;
  38878. }
  38879. } catch (e) {
  38880. // replace the error
  38881. error = e;
  38882. }
  38883. // continue
  38884. next(error);
  38885. }
  38886. /**
  38887. * Log error using console.error.
  38888. *
  38889. * @param {Error} err
  38890. * @private
  38891. */
  38892. function logerror(err) {
  38893. if (env !== 'test') console.error(err.stack || err.toString());
  38894. }
  38895. /**
  38896. * Get get protocol + host for a URL.
  38897. *
  38898. * @param {string} url
  38899. * @private
  38900. */
  38901. function getProtohost(url) {
  38902. if (url.length === 0 || url[0] === '/') {
  38903. return undefined;
  38904. }
  38905. var fqdnIndex = url.indexOf('://');
  38906. return fqdnIndex !== -1 && url.lastIndexOf('?', fqdnIndex) === -1
  38907. ? url.substr(0, url.indexOf('/', 3 + fqdnIndex))
  38908. : undefined;
  38909. }
  38910. var connect$1 = /*@__PURE__*/getDefaultExportFromCjs(connect);
  38911. var lib = {exports: {}};
  38912. /*
  38913. object-assign
  38914. (c) Sindre Sorhus
  38915. @license MIT
  38916. */
  38917. /* eslint-disable no-unused-vars */
  38918. var getOwnPropertySymbols = Object.getOwnPropertySymbols;
  38919. var hasOwnProperty = Object.prototype.hasOwnProperty;
  38920. var propIsEnumerable = Object.prototype.propertyIsEnumerable;
  38921. function toObject(val) {
  38922. if (val === null || val === undefined) {
  38923. throw new TypeError('Object.assign cannot be called with null or undefined');
  38924. }
  38925. return Object(val);
  38926. }
  38927. function shouldUseNative() {
  38928. try {
  38929. if (!Object.assign) {
  38930. return false;
  38931. }
  38932. // Detect buggy property enumeration order in older V8 versions.
  38933. // https://bugs.chromium.org/p/v8/issues/detail?id=4118
  38934. var test1 = new String('abc'); // eslint-disable-line no-new-wrappers
  38935. test1[5] = 'de';
  38936. if (Object.getOwnPropertyNames(test1)[0] === '5') {
  38937. return false;
  38938. }
  38939. // https://bugs.chromium.org/p/v8/issues/detail?id=3056
  38940. var test2 = {};
  38941. for (var i = 0; i < 10; i++) {
  38942. test2['_' + String.fromCharCode(i)] = i;
  38943. }
  38944. var order2 = Object.getOwnPropertyNames(test2).map(function (n) {
  38945. return test2[n];
  38946. });
  38947. if (order2.join('') !== '0123456789') {
  38948. return false;
  38949. }
  38950. // https://bugs.chromium.org/p/v8/issues/detail?id=3056
  38951. var test3 = {};
  38952. 'abcdefghijklmnopqrst'.split('').forEach(function (letter) {
  38953. test3[letter] = letter;
  38954. });
  38955. if (Object.keys(Object.assign({}, test3)).join('') !==
  38956. 'abcdefghijklmnopqrst') {
  38957. return false;
  38958. }
  38959. return true;
  38960. } catch (err) {
  38961. // We don't expect any of the above to throw, but better to be safe.
  38962. return false;
  38963. }
  38964. }
  38965. var objectAssign = shouldUseNative() ? Object.assign : function (target, source) {
  38966. var from;
  38967. var to = toObject(target);
  38968. var symbols;
  38969. for (var s = 1; s < arguments.length; s++) {
  38970. from = Object(arguments[s]);
  38971. for (var key in from) {
  38972. if (hasOwnProperty.call(from, key)) {
  38973. to[key] = from[key];
  38974. }
  38975. }
  38976. if (getOwnPropertySymbols) {
  38977. symbols = getOwnPropertySymbols(from);
  38978. for (var i = 0; i < symbols.length; i++) {
  38979. if (propIsEnumerable.call(from, symbols[i])) {
  38980. to[symbols[i]] = from[symbols[i]];
  38981. }
  38982. }
  38983. }
  38984. }
  38985. return to;
  38986. };
  38987. var vary$1 = {exports: {}};
  38988. /*!
  38989. * vary
  38990. * Copyright(c) 2014-2017 Douglas Christopher Wilson
  38991. * MIT Licensed
  38992. */
  38993. /**
  38994. * Module exports.
  38995. */
  38996. vary$1.exports = vary;
  38997. vary$1.exports.append = append;
  38998. /**
  38999. * RegExp to match field-name in RFC 7230 sec 3.2
  39000. *
  39001. * field-name = token
  39002. * token = 1*tchar
  39003. * tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*"
  39004. * / "+" / "-" / "." / "^" / "_" / "`" / "|" / "~"
  39005. * / DIGIT / ALPHA
  39006. * ; any VCHAR, except delimiters
  39007. */
  39008. var FIELD_NAME_REGEXP = /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/;
  39009. /**
  39010. * Append a field to a vary header.
  39011. *
  39012. * @param {String} header
  39013. * @param {String|Array} field
  39014. * @return {String}
  39015. * @public
  39016. */
  39017. function append (header, field) {
  39018. if (typeof header !== 'string') {
  39019. throw new TypeError('header argument is required')
  39020. }
  39021. if (!field) {
  39022. throw new TypeError('field argument is required')
  39023. }
  39024. // get fields array
  39025. var fields = !Array.isArray(field)
  39026. ? parse$7(String(field))
  39027. : field;
  39028. // assert on invalid field names
  39029. for (var j = 0; j < fields.length; j++) {
  39030. if (!FIELD_NAME_REGEXP.test(fields[j])) {
  39031. throw new TypeError('field argument contains an invalid header name')
  39032. }
  39033. }
  39034. // existing, unspecified vary
  39035. if (header === '*') {
  39036. return header
  39037. }
  39038. // enumerate current values
  39039. var val = header;
  39040. var vals = parse$7(header.toLowerCase());
  39041. // unspecified vary
  39042. if (fields.indexOf('*') !== -1 || vals.indexOf('*') !== -1) {
  39043. return '*'
  39044. }
  39045. for (var i = 0; i < fields.length; i++) {
  39046. var fld = fields[i].toLowerCase();
  39047. // append value (case-preserving)
  39048. if (vals.indexOf(fld) === -1) {
  39049. vals.push(fld);
  39050. val = val
  39051. ? val + ', ' + fields[i]
  39052. : fields[i];
  39053. }
  39054. }
  39055. return val
  39056. }
  39057. /**
  39058. * Parse a vary header into an array.
  39059. *
  39060. * @param {String} header
  39061. * @return {Array}
  39062. * @private
  39063. */
  39064. function parse$7 (header) {
  39065. var end = 0;
  39066. var list = [];
  39067. var start = 0;
  39068. // gather tokens
  39069. for (var i = 0, len = header.length; i < len; i++) {
  39070. switch (header.charCodeAt(i)) {
  39071. case 0x20: /* */
  39072. if (start === end) {
  39073. start = end = i + 1;
  39074. }
  39075. break
  39076. case 0x2c: /* , */
  39077. list.push(header.substring(start, end));
  39078. start = end = i + 1;
  39079. break
  39080. default:
  39081. end = i + 1;
  39082. break
  39083. }
  39084. }
  39085. // final token
  39086. list.push(header.substring(start, end));
  39087. return list
  39088. }
  39089. /**
  39090. * Mark that a request is varied on a header field.
  39091. *
  39092. * @param {Object} res
  39093. * @param {String|Array} field
  39094. * @public
  39095. */
  39096. function vary (res, field) {
  39097. if (!res || !res.getHeader || !res.setHeader) {
  39098. // quack quack
  39099. throw new TypeError('res argument is required')
  39100. }
  39101. // get existing header
  39102. var val = res.getHeader('Vary') || '';
  39103. var header = Array.isArray(val)
  39104. ? val.join(', ')
  39105. : String(val);
  39106. // set new header
  39107. if ((val = append(header, field))) {
  39108. res.setHeader('Vary', val);
  39109. }
  39110. }
  39111. var varyExports = vary$1.exports;
  39112. (function () {
  39113. var assign = objectAssign;
  39114. var vary = varyExports;
  39115. var defaults = {
  39116. origin: '*',
  39117. methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
  39118. preflightContinue: false,
  39119. optionsSuccessStatus: 204
  39120. };
  39121. function isString(s) {
  39122. return typeof s === 'string' || s instanceof String;
  39123. }
  39124. function isOriginAllowed(origin, allowedOrigin) {
  39125. if (Array.isArray(allowedOrigin)) {
  39126. for (var i = 0; i < allowedOrigin.length; ++i) {
  39127. if (isOriginAllowed(origin, allowedOrigin[i])) {
  39128. return true;
  39129. }
  39130. }
  39131. return false;
  39132. } else if (isString(allowedOrigin)) {
  39133. return origin === allowedOrigin;
  39134. } else if (allowedOrigin instanceof RegExp) {
  39135. return allowedOrigin.test(origin);
  39136. } else {
  39137. return !!allowedOrigin;
  39138. }
  39139. }
  39140. function configureOrigin(options, req) {
  39141. var requestOrigin = req.headers.origin,
  39142. headers = [],
  39143. isAllowed;
  39144. if (!options.origin || options.origin === '*') {
  39145. // allow any origin
  39146. headers.push([{
  39147. key: 'Access-Control-Allow-Origin',
  39148. value: '*'
  39149. }]);
  39150. } else if (isString(options.origin)) {
  39151. // fixed origin
  39152. headers.push([{
  39153. key: 'Access-Control-Allow-Origin',
  39154. value: options.origin
  39155. }]);
  39156. headers.push([{
  39157. key: 'Vary',
  39158. value: 'Origin'
  39159. }]);
  39160. } else {
  39161. isAllowed = isOriginAllowed(requestOrigin, options.origin);
  39162. // reflect origin
  39163. headers.push([{
  39164. key: 'Access-Control-Allow-Origin',
  39165. value: isAllowed ? requestOrigin : false
  39166. }]);
  39167. headers.push([{
  39168. key: 'Vary',
  39169. value: 'Origin'
  39170. }]);
  39171. }
  39172. return headers;
  39173. }
  39174. function configureMethods(options) {
  39175. var methods = options.methods;
  39176. if (methods.join) {
  39177. methods = options.methods.join(','); // .methods is an array, so turn it into a string
  39178. }
  39179. return {
  39180. key: 'Access-Control-Allow-Methods',
  39181. value: methods
  39182. };
  39183. }
  39184. function configureCredentials(options) {
  39185. if (options.credentials === true) {
  39186. return {
  39187. key: 'Access-Control-Allow-Credentials',
  39188. value: 'true'
  39189. };
  39190. }
  39191. return null;
  39192. }
  39193. function configureAllowedHeaders(options, req) {
  39194. var allowedHeaders = options.allowedHeaders || options.headers;
  39195. var headers = [];
  39196. if (!allowedHeaders) {
  39197. allowedHeaders = req.headers['access-control-request-headers']; // .headers wasn't specified, so reflect the request headers
  39198. headers.push([{
  39199. key: 'Vary',
  39200. value: 'Access-Control-Request-Headers'
  39201. }]);
  39202. } else if (allowedHeaders.join) {
  39203. allowedHeaders = allowedHeaders.join(','); // .headers is an array, so turn it into a string
  39204. }
  39205. if (allowedHeaders && allowedHeaders.length) {
  39206. headers.push([{
  39207. key: 'Access-Control-Allow-Headers',
  39208. value: allowedHeaders
  39209. }]);
  39210. }
  39211. return headers;
  39212. }
  39213. function configureExposedHeaders(options) {
  39214. var headers = options.exposedHeaders;
  39215. if (!headers) {
  39216. return null;
  39217. } else if (headers.join) {
  39218. headers = headers.join(','); // .headers is an array, so turn it into a string
  39219. }
  39220. if (headers && headers.length) {
  39221. return {
  39222. key: 'Access-Control-Expose-Headers',
  39223. value: headers
  39224. };
  39225. }
  39226. return null;
  39227. }
  39228. function configureMaxAge(options) {
  39229. var maxAge = (typeof options.maxAge === 'number' || options.maxAge) && options.maxAge.toString();
  39230. if (maxAge && maxAge.length) {
  39231. return {
  39232. key: 'Access-Control-Max-Age',
  39233. value: maxAge
  39234. };
  39235. }
  39236. return null;
  39237. }
  39238. function applyHeaders(headers, res) {
  39239. for (var i = 0, n = headers.length; i < n; i++) {
  39240. var header = headers[i];
  39241. if (header) {
  39242. if (Array.isArray(header)) {
  39243. applyHeaders(header, res);
  39244. } else if (header.key === 'Vary' && header.value) {
  39245. vary(res, header.value);
  39246. } else if (header.value) {
  39247. res.setHeader(header.key, header.value);
  39248. }
  39249. }
  39250. }
  39251. }
  39252. function cors(options, req, res, next) {
  39253. var headers = [],
  39254. method = req.method && req.method.toUpperCase && req.method.toUpperCase();
  39255. if (method === 'OPTIONS') {
  39256. // preflight
  39257. headers.push(configureOrigin(options, req));
  39258. headers.push(configureCredentials(options));
  39259. headers.push(configureMethods(options));
  39260. headers.push(configureAllowedHeaders(options, req));
  39261. headers.push(configureMaxAge(options));
  39262. headers.push(configureExposedHeaders(options));
  39263. applyHeaders(headers, res);
  39264. if (options.preflightContinue) {
  39265. next();
  39266. } else {
  39267. // Safari (and potentially other browsers) need content-length 0,
  39268. // for 204 or they just hang waiting for a body
  39269. res.statusCode = options.optionsSuccessStatus;
  39270. res.setHeader('Content-Length', '0');
  39271. res.end();
  39272. }
  39273. } else {
  39274. // actual response
  39275. headers.push(configureOrigin(options, req));
  39276. headers.push(configureCredentials(options));
  39277. headers.push(configureExposedHeaders(options));
  39278. applyHeaders(headers, res);
  39279. next();
  39280. }
  39281. }
  39282. function middlewareWrapper(o) {
  39283. // if options are static (either via defaults or custom options passed in), wrap in a function
  39284. var optionsCallback = null;
  39285. if (typeof o === 'function') {
  39286. optionsCallback = o;
  39287. } else {
  39288. optionsCallback = function (req, cb) {
  39289. cb(null, o);
  39290. };
  39291. }
  39292. return function corsMiddleware(req, res, next) {
  39293. optionsCallback(req, function (err, options) {
  39294. if (err) {
  39295. next(err);
  39296. } else {
  39297. var corsOptions = assign({}, defaults, options);
  39298. var originCallback = null;
  39299. if (corsOptions.origin && typeof corsOptions.origin === 'function') {
  39300. originCallback = corsOptions.origin;
  39301. } else if (corsOptions.origin) {
  39302. originCallback = function (origin, cb) {
  39303. cb(null, corsOptions.origin);
  39304. };
  39305. }
  39306. if (originCallback) {
  39307. originCallback(req.headers.origin, function (err2, origin) {
  39308. if (err2 || !origin) {
  39309. next(err2);
  39310. } else {
  39311. corsOptions.origin = origin;
  39312. cors(corsOptions, req, res, next);
  39313. }
  39314. });
  39315. } else {
  39316. next();
  39317. }
  39318. }
  39319. });
  39320. };
  39321. }
  39322. // can pass either an options hash, an options delegate, or nothing
  39323. lib.exports = middlewareWrapper;
  39324. }());
  39325. var libExports = lib.exports;
  39326. var corsMiddleware = /*@__PURE__*/getDefaultExportFromCjs(libExports);
  39327. var chokidar = {};
  39328. const fs$8 = require$$0__default;
  39329. const { Readable } = require$$0$6;
  39330. const sysPath$3 = require$$0$4;
  39331. const { promisify: promisify$3 } = require$$0$5;
  39332. const picomatch$1 = picomatch$3;
  39333. const readdir$1 = promisify$3(fs$8.readdir);
  39334. const stat$3 = promisify$3(fs$8.stat);
  39335. const lstat$2 = promisify$3(fs$8.lstat);
  39336. const realpath$1 = promisify$3(fs$8.realpath);
  39337. /**
  39338. * @typedef {Object} EntryInfo
  39339. * @property {String} path
  39340. * @property {String} fullPath
  39341. * @property {fs.Stats=} stats
  39342. * @property {fs.Dirent=} dirent
  39343. * @property {String} basename
  39344. */
  39345. const BANG$2 = '!';
  39346. const RECURSIVE_ERROR_CODE = 'READDIRP_RECURSIVE_ERROR';
  39347. const NORMAL_FLOW_ERRORS = new Set(['ENOENT', 'EPERM', 'EACCES', 'ELOOP', RECURSIVE_ERROR_CODE]);
  39348. const FILE_TYPE = 'files';
  39349. const DIR_TYPE = 'directories';
  39350. const FILE_DIR_TYPE = 'files_directories';
  39351. const EVERYTHING_TYPE = 'all';
  39352. const ALL_TYPES = [FILE_TYPE, DIR_TYPE, FILE_DIR_TYPE, EVERYTHING_TYPE];
  39353. const isNormalFlowError = error => NORMAL_FLOW_ERRORS.has(error.code);
  39354. const [maj, min] = process.versions.node.split('.').slice(0, 2).map(n => Number.parseInt(n, 10));
  39355. const wantBigintFsStats = process.platform === 'win32' && (maj > 10 || (maj === 10 && min >= 5));
  39356. const normalizeFilter = filter => {
  39357. if (filter === undefined) return;
  39358. if (typeof filter === 'function') return filter;
  39359. if (typeof filter === 'string') {
  39360. const glob = picomatch$1(filter.trim());
  39361. return entry => glob(entry.basename);
  39362. }
  39363. if (Array.isArray(filter)) {
  39364. const positive = [];
  39365. const negative = [];
  39366. for (const item of filter) {
  39367. const trimmed = item.trim();
  39368. if (trimmed.charAt(0) === BANG$2) {
  39369. negative.push(picomatch$1(trimmed.slice(1)));
  39370. } else {
  39371. positive.push(picomatch$1(trimmed));
  39372. }
  39373. }
  39374. if (negative.length > 0) {
  39375. if (positive.length > 0) {
  39376. return entry =>
  39377. positive.some(f => f(entry.basename)) && !negative.some(f => f(entry.basename));
  39378. }
  39379. return entry => !negative.some(f => f(entry.basename));
  39380. }
  39381. return entry => positive.some(f => f(entry.basename));
  39382. }
  39383. };
  39384. class ReaddirpStream extends Readable {
  39385. static get defaultOptions() {
  39386. return {
  39387. root: '.',
  39388. /* eslint-disable no-unused-vars */
  39389. fileFilter: (path) => true,
  39390. directoryFilter: (path) => true,
  39391. /* eslint-enable no-unused-vars */
  39392. type: FILE_TYPE,
  39393. lstat: false,
  39394. depth: 2147483648,
  39395. alwaysStat: false
  39396. };
  39397. }
  39398. constructor(options = {}) {
  39399. super({
  39400. objectMode: true,
  39401. autoDestroy: true,
  39402. highWaterMark: options.highWaterMark || 4096
  39403. });
  39404. const opts = { ...ReaddirpStream.defaultOptions, ...options };
  39405. const { root, type } = opts;
  39406. this._fileFilter = normalizeFilter(opts.fileFilter);
  39407. this._directoryFilter = normalizeFilter(opts.directoryFilter);
  39408. const statMethod = opts.lstat ? lstat$2 : stat$3;
  39409. // Use bigint stats if it's windows and stat() supports options (node 10+).
  39410. if (wantBigintFsStats) {
  39411. this._stat = path => statMethod(path, { bigint: true });
  39412. } else {
  39413. this._stat = statMethod;
  39414. }
  39415. this._maxDepth = opts.depth;
  39416. this._wantsDir = [DIR_TYPE, FILE_DIR_TYPE, EVERYTHING_TYPE].includes(type);
  39417. this._wantsFile = [FILE_TYPE, FILE_DIR_TYPE, EVERYTHING_TYPE].includes(type);
  39418. this._wantsEverything = type === EVERYTHING_TYPE;
  39419. this._root = sysPath$3.resolve(root);
  39420. this._isDirent = ('Dirent' in fs$8) && !opts.alwaysStat;
  39421. this._statsProp = this._isDirent ? 'dirent' : 'stats';
  39422. this._rdOptions = { encoding: 'utf8', withFileTypes: this._isDirent };
  39423. // Launch stream with one parent, the root dir.
  39424. this.parents = [this._exploreDir(root, 1)];
  39425. this.reading = false;
  39426. this.parent = undefined;
  39427. }
  39428. async _read(batch) {
  39429. if (this.reading) return;
  39430. this.reading = true;
  39431. try {
  39432. while (!this.destroyed && batch > 0) {
  39433. const { path, depth, files = [] } = this.parent || {};
  39434. if (files.length > 0) {
  39435. const slice = files.splice(0, batch).map(dirent => this._formatEntry(dirent, path));
  39436. for (const entry of await Promise.all(slice)) {
  39437. if (this.destroyed) return;
  39438. const entryType = await this._getEntryType(entry);
  39439. if (entryType === 'directory' && this._directoryFilter(entry)) {
  39440. if (depth <= this._maxDepth) {
  39441. this.parents.push(this._exploreDir(entry.fullPath, depth + 1));
  39442. }
  39443. if (this._wantsDir) {
  39444. this.push(entry);
  39445. batch--;
  39446. }
  39447. } else if ((entryType === 'file' || this._includeAsFile(entry)) && this._fileFilter(entry)) {
  39448. if (this._wantsFile) {
  39449. this.push(entry);
  39450. batch--;
  39451. }
  39452. }
  39453. }
  39454. } else {
  39455. const parent = this.parents.pop();
  39456. if (!parent) {
  39457. this.push(null);
  39458. break;
  39459. }
  39460. this.parent = await parent;
  39461. if (this.destroyed) return;
  39462. }
  39463. }
  39464. } catch (error) {
  39465. this.destroy(error);
  39466. } finally {
  39467. this.reading = false;
  39468. }
  39469. }
  39470. async _exploreDir(path, depth) {
  39471. let files;
  39472. try {
  39473. files = await readdir$1(path, this._rdOptions);
  39474. } catch (error) {
  39475. this._onError(error);
  39476. }
  39477. return { files, depth, path };
  39478. }
  39479. async _formatEntry(dirent, path) {
  39480. let entry;
  39481. try {
  39482. const basename = this._isDirent ? dirent.name : dirent;
  39483. const fullPath = sysPath$3.resolve(sysPath$3.join(path, basename));
  39484. entry = { path: sysPath$3.relative(this._root, fullPath), fullPath, basename };
  39485. entry[this._statsProp] = this._isDirent ? dirent : await this._stat(fullPath);
  39486. } catch (err) {
  39487. this._onError(err);
  39488. }
  39489. return entry;
  39490. }
  39491. _onError(err) {
  39492. if (isNormalFlowError(err) && !this.destroyed) {
  39493. this.emit('warn', err);
  39494. } else {
  39495. this.destroy(err);
  39496. }
  39497. }
  39498. async _getEntryType(entry) {
  39499. // entry may be undefined, because a warning or an error were emitted
  39500. // and the statsProp is undefined
  39501. const stats = entry && entry[this._statsProp];
  39502. if (!stats) {
  39503. return;
  39504. }
  39505. if (stats.isFile()) {
  39506. return 'file';
  39507. }
  39508. if (stats.isDirectory()) {
  39509. return 'directory';
  39510. }
  39511. if (stats && stats.isSymbolicLink()) {
  39512. const full = entry.fullPath;
  39513. try {
  39514. const entryRealPath = await realpath$1(full);
  39515. const entryRealPathStats = await lstat$2(entryRealPath);
  39516. if (entryRealPathStats.isFile()) {
  39517. return 'file';
  39518. }
  39519. if (entryRealPathStats.isDirectory()) {
  39520. const len = entryRealPath.length;
  39521. if (full.startsWith(entryRealPath) && full.substr(len, 1) === sysPath$3.sep) {
  39522. const recursiveError = new Error(
  39523. `Circular symlink detected: "${full}" points to "${entryRealPath}"`
  39524. );
  39525. recursiveError.code = RECURSIVE_ERROR_CODE;
  39526. return this._onError(recursiveError);
  39527. }
  39528. return 'directory';
  39529. }
  39530. } catch (error) {
  39531. this._onError(error);
  39532. }
  39533. }
  39534. }
  39535. _includeAsFile(entry) {
  39536. const stats = entry && entry[this._statsProp];
  39537. return stats && this._wantsEverything && !stats.isDirectory();
  39538. }
  39539. }
  39540. /**
  39541. * @typedef {Object} ReaddirpArguments
  39542. * @property {Function=} fileFilter
  39543. * @property {Function=} directoryFilter
  39544. * @property {String=} type
  39545. * @property {Number=} depth
  39546. * @property {String=} root
  39547. * @property {Boolean=} lstat
  39548. * @property {Boolean=} bigint
  39549. */
  39550. /**
  39551. * Main function which ends up calling readdirRec and reads all files and directories in given root recursively.
  39552. * @param {String} root Root directory
  39553. * @param {ReaddirpArguments=} options Options to specify root (start directory), filters and recursion depth
  39554. */
  39555. const readdirp$1 = (root, options = {}) => {
  39556. let type = options.entryType || options.type;
  39557. if (type === 'both') type = FILE_DIR_TYPE; // backwards-compatibility
  39558. if (type) options.type = type;
  39559. if (!root) {
  39560. throw new Error('readdirp: root argument is required. Usage: readdirp(root, options)');
  39561. } else if (typeof root !== 'string') {
  39562. throw new TypeError('readdirp: root argument must be a string. Usage: readdirp(root, options)');
  39563. } else if (type && !ALL_TYPES.includes(type)) {
  39564. throw new Error(`readdirp: Invalid type passed. Use one of ${ALL_TYPES.join(', ')}`);
  39565. }
  39566. options.root = root;
  39567. return new ReaddirpStream(options);
  39568. };
  39569. const readdirpPromise = (root, options = {}) => {
  39570. return new Promise((resolve, reject) => {
  39571. const files = [];
  39572. readdirp$1(root, options)
  39573. .on('data', entry => files.push(entry))
  39574. .on('end', () => resolve(files))
  39575. .on('error', error => reject(error));
  39576. });
  39577. };
  39578. readdirp$1.promise = readdirpPromise;
  39579. readdirp$1.ReaddirpStream = ReaddirpStream;
  39580. readdirp$1.default = readdirp$1;
  39581. var readdirp_1 = readdirp$1;
  39582. var anymatch$2 = {exports: {}};
  39583. /*!
  39584. * normalize-path <https://github.com/jonschlinkert/normalize-path>
  39585. *
  39586. * Copyright (c) 2014-2018, Jon Schlinkert.
  39587. * Released under the MIT License.
  39588. */
  39589. var normalizePath$2 = function(path, stripTrailing) {
  39590. if (typeof path !== 'string') {
  39591. throw new TypeError('expected path to be a string');
  39592. }
  39593. if (path === '\\' || path === '/') return '/';
  39594. var len = path.length;
  39595. if (len <= 1) return path;
  39596. // ensure that win32 namespaces has two leading slashes, so that the path is
  39597. // handled properly by the win32 version of path.parse() after being normalized
  39598. // https://msdn.microsoft.com/library/windows/desktop/aa365247(v=vs.85).aspx#namespaces
  39599. var prefix = '';
  39600. if (len > 4 && path[3] === '\\') {
  39601. var ch = path[2];
  39602. if ((ch === '?' || ch === '.') && path.slice(0, 2) === '\\\\') {
  39603. path = path.slice(2);
  39604. prefix = '//';
  39605. }
  39606. }
  39607. var segs = path.split(/[/\\]+/);
  39608. if (stripTrailing !== false && segs[segs.length - 1] === '') {
  39609. segs.pop();
  39610. }
  39611. return prefix + segs.join('/');
  39612. };
  39613. var anymatch_1 = anymatch$2.exports;
  39614. Object.defineProperty(anymatch_1, "__esModule", { value: true });
  39615. const picomatch = picomatch$3;
  39616. const normalizePath$1 = normalizePath$2;
  39617. /**
  39618. * @typedef {(testString: string) => boolean} AnymatchFn
  39619. * @typedef {string|RegExp|AnymatchFn} AnymatchPattern
  39620. * @typedef {AnymatchPattern|AnymatchPattern[]} AnymatchMatcher
  39621. */
  39622. const BANG$1 = '!';
  39623. const DEFAULT_OPTIONS = {returnIndex: false};
  39624. const arrify$1 = (item) => Array.isArray(item) ? item : [item];
  39625. /**
  39626. * @param {AnymatchPattern} matcher
  39627. * @param {object} options
  39628. * @returns {AnymatchFn}
  39629. */
  39630. const createPattern = (matcher, options) => {
  39631. if (typeof matcher === 'function') {
  39632. return matcher;
  39633. }
  39634. if (typeof matcher === 'string') {
  39635. const glob = picomatch(matcher, options);
  39636. return (string) => matcher === string || glob(string);
  39637. }
  39638. if (matcher instanceof RegExp) {
  39639. return (string) => matcher.test(string);
  39640. }
  39641. return (string) => false;
  39642. };
  39643. /**
  39644. * @param {Array<Function>} patterns
  39645. * @param {Array<Function>} negPatterns
  39646. * @param {String|Array} args
  39647. * @param {Boolean} returnIndex
  39648. * @returns {boolean|number}
  39649. */
  39650. const matchPatterns = (patterns, negPatterns, args, returnIndex) => {
  39651. const isList = Array.isArray(args);
  39652. const _path = isList ? args[0] : args;
  39653. if (!isList && typeof _path !== 'string') {
  39654. throw new TypeError('anymatch: second argument must be a string: got ' +
  39655. Object.prototype.toString.call(_path))
  39656. }
  39657. const path = normalizePath$1(_path);
  39658. for (let index = 0; index < negPatterns.length; index++) {
  39659. const nglob = negPatterns[index];
  39660. if (nglob(path)) {
  39661. return returnIndex ? -1 : false;
  39662. }
  39663. }
  39664. const applied = isList && [path].concat(args.slice(1));
  39665. for (let index = 0; index < patterns.length; index++) {
  39666. const pattern = patterns[index];
  39667. if (isList ? pattern(...applied) : pattern(path)) {
  39668. return returnIndex ? index : true;
  39669. }
  39670. }
  39671. return returnIndex ? -1 : false;
  39672. };
  39673. /**
  39674. * @param {AnymatchMatcher} matchers
  39675. * @param {Array|string} testString
  39676. * @param {object} options
  39677. * @returns {boolean|number|Function}
  39678. */
  39679. const anymatch$1 = (matchers, testString, options = DEFAULT_OPTIONS) => {
  39680. if (matchers == null) {
  39681. throw new TypeError('anymatch: specify first argument');
  39682. }
  39683. const opts = typeof options === 'boolean' ? {returnIndex: options} : options;
  39684. const returnIndex = opts.returnIndex || false;
  39685. // Early cache for matchers.
  39686. const mtchers = arrify$1(matchers);
  39687. const negatedGlobs = mtchers
  39688. .filter(item => typeof item === 'string' && item.charAt(0) === BANG$1)
  39689. .map(item => item.slice(1))
  39690. .map(item => picomatch(item, opts));
  39691. const patterns = mtchers
  39692. .filter(item => typeof item !== 'string' || (typeof item === 'string' && item.charAt(0) !== BANG$1))
  39693. .map(matcher => createPattern(matcher, opts));
  39694. if (testString == null) {
  39695. return (testString, ri = false) => {
  39696. const returnIndex = typeof ri === 'boolean' ? ri : false;
  39697. return matchPatterns(patterns, negatedGlobs, testString, returnIndex);
  39698. }
  39699. }
  39700. return matchPatterns(patterns, negatedGlobs, testString, returnIndex);
  39701. };
  39702. anymatch$1.default = anymatch$1;
  39703. anymatch$2.exports = anymatch$1;
  39704. var anymatchExports = anymatch$2.exports;
  39705. var require$$0 = [
  39706. "3dm",
  39707. "3ds",
  39708. "3g2",
  39709. "3gp",
  39710. "7z",
  39711. "a",
  39712. "aac",
  39713. "adp",
  39714. "ai",
  39715. "aif",
  39716. "aiff",
  39717. "alz",
  39718. "ape",
  39719. "apk",
  39720. "appimage",
  39721. "ar",
  39722. "arj",
  39723. "asf",
  39724. "au",
  39725. "avi",
  39726. "bak",
  39727. "baml",
  39728. "bh",
  39729. "bin",
  39730. "bk",
  39731. "bmp",
  39732. "btif",
  39733. "bz2",
  39734. "bzip2",
  39735. "cab",
  39736. "caf",
  39737. "cgm",
  39738. "class",
  39739. "cmx",
  39740. "cpio",
  39741. "cr2",
  39742. "cur",
  39743. "dat",
  39744. "dcm",
  39745. "deb",
  39746. "dex",
  39747. "djvu",
  39748. "dll",
  39749. "dmg",
  39750. "dng",
  39751. "doc",
  39752. "docm",
  39753. "docx",
  39754. "dot",
  39755. "dotm",
  39756. "dra",
  39757. "DS_Store",
  39758. "dsk",
  39759. "dts",
  39760. "dtshd",
  39761. "dvb",
  39762. "dwg",
  39763. "dxf",
  39764. "ecelp4800",
  39765. "ecelp7470",
  39766. "ecelp9600",
  39767. "egg",
  39768. "eol",
  39769. "eot",
  39770. "epub",
  39771. "exe",
  39772. "f4v",
  39773. "fbs",
  39774. "fh",
  39775. "fla",
  39776. "flac",
  39777. "flatpak",
  39778. "fli",
  39779. "flv",
  39780. "fpx",
  39781. "fst",
  39782. "fvt",
  39783. "g3",
  39784. "gh",
  39785. "gif",
  39786. "graffle",
  39787. "gz",
  39788. "gzip",
  39789. "h261",
  39790. "h263",
  39791. "h264",
  39792. "icns",
  39793. "ico",
  39794. "ief",
  39795. "img",
  39796. "ipa",
  39797. "iso",
  39798. "jar",
  39799. "jpeg",
  39800. "jpg",
  39801. "jpgv",
  39802. "jpm",
  39803. "jxr",
  39804. "key",
  39805. "ktx",
  39806. "lha",
  39807. "lib",
  39808. "lvp",
  39809. "lz",
  39810. "lzh",
  39811. "lzma",
  39812. "lzo",
  39813. "m3u",
  39814. "m4a",
  39815. "m4v",
  39816. "mar",
  39817. "mdi",
  39818. "mht",
  39819. "mid",
  39820. "midi",
  39821. "mj2",
  39822. "mka",
  39823. "mkv",
  39824. "mmr",
  39825. "mng",
  39826. "mobi",
  39827. "mov",
  39828. "movie",
  39829. "mp3",
  39830. "mp4",
  39831. "mp4a",
  39832. "mpeg",
  39833. "mpg",
  39834. "mpga",
  39835. "mxu",
  39836. "nef",
  39837. "npx",
  39838. "numbers",
  39839. "nupkg",
  39840. "o",
  39841. "odp",
  39842. "ods",
  39843. "odt",
  39844. "oga",
  39845. "ogg",
  39846. "ogv",
  39847. "otf",
  39848. "ott",
  39849. "pages",
  39850. "pbm",
  39851. "pcx",
  39852. "pdb",
  39853. "pdf",
  39854. "pea",
  39855. "pgm",
  39856. "pic",
  39857. "png",
  39858. "pnm",
  39859. "pot",
  39860. "potm",
  39861. "potx",
  39862. "ppa",
  39863. "ppam",
  39864. "ppm",
  39865. "pps",
  39866. "ppsm",
  39867. "ppsx",
  39868. "ppt",
  39869. "pptm",
  39870. "pptx",
  39871. "psd",
  39872. "pya",
  39873. "pyc",
  39874. "pyo",
  39875. "pyv",
  39876. "qt",
  39877. "rar",
  39878. "ras",
  39879. "raw",
  39880. "resources",
  39881. "rgb",
  39882. "rip",
  39883. "rlc",
  39884. "rmf",
  39885. "rmvb",
  39886. "rpm",
  39887. "rtf",
  39888. "rz",
  39889. "s3m",
  39890. "s7z",
  39891. "scpt",
  39892. "sgi",
  39893. "shar",
  39894. "snap",
  39895. "sil",
  39896. "sketch",
  39897. "slk",
  39898. "smv",
  39899. "snk",
  39900. "so",
  39901. "stl",
  39902. "suo",
  39903. "sub",
  39904. "swf",
  39905. "tar",
  39906. "tbz",
  39907. "tbz2",
  39908. "tga",
  39909. "tgz",
  39910. "thmx",
  39911. "tif",
  39912. "tiff",
  39913. "tlz",
  39914. "ttc",
  39915. "ttf",
  39916. "txz",
  39917. "udf",
  39918. "uvh",
  39919. "uvi",
  39920. "uvm",
  39921. "uvp",
  39922. "uvs",
  39923. "uvu",
  39924. "viv",
  39925. "vob",
  39926. "war",
  39927. "wav",
  39928. "wax",
  39929. "wbmp",
  39930. "wdp",
  39931. "weba",
  39932. "webm",
  39933. "webp",
  39934. "whl",
  39935. "wim",
  39936. "wm",
  39937. "wma",
  39938. "wmv",
  39939. "wmx",
  39940. "woff",
  39941. "woff2",
  39942. "wrm",
  39943. "wvx",
  39944. "xbm",
  39945. "xif",
  39946. "xla",
  39947. "xlam",
  39948. "xls",
  39949. "xlsb",
  39950. "xlsm",
  39951. "xlsx",
  39952. "xlt",
  39953. "xltm",
  39954. "xltx",
  39955. "xm",
  39956. "xmind",
  39957. "xpi",
  39958. "xpm",
  39959. "xwd",
  39960. "xz",
  39961. "z",
  39962. "zip",
  39963. "zipx"
  39964. ];
  39965. var binaryExtensions$1 = require$$0;
  39966. const path$8 = require$$0$4;
  39967. const binaryExtensions = binaryExtensions$1;
  39968. const extensions = new Set(binaryExtensions);
  39969. var isBinaryPath$1 = filePath => extensions.has(path$8.extname(filePath).slice(1).toLowerCase());
  39970. var constants$1 = {};
  39971. (function (exports) {
  39972. const {sep} = require$$0$4;
  39973. const {platform} = process;
  39974. const os = require$$2;
  39975. exports.EV_ALL = 'all';
  39976. exports.EV_READY = 'ready';
  39977. exports.EV_ADD = 'add';
  39978. exports.EV_CHANGE = 'change';
  39979. exports.EV_ADD_DIR = 'addDir';
  39980. exports.EV_UNLINK = 'unlink';
  39981. exports.EV_UNLINK_DIR = 'unlinkDir';
  39982. exports.EV_RAW = 'raw';
  39983. exports.EV_ERROR = 'error';
  39984. exports.STR_DATA = 'data';
  39985. exports.STR_END = 'end';
  39986. exports.STR_CLOSE = 'close';
  39987. exports.FSEVENT_CREATED = 'created';
  39988. exports.FSEVENT_MODIFIED = 'modified';
  39989. exports.FSEVENT_DELETED = 'deleted';
  39990. exports.FSEVENT_MOVED = 'moved';
  39991. exports.FSEVENT_CLONED = 'cloned';
  39992. exports.FSEVENT_UNKNOWN = 'unknown';
  39993. exports.FSEVENT_FLAG_MUST_SCAN_SUBDIRS = 1;
  39994. exports.FSEVENT_TYPE_FILE = 'file';
  39995. exports.FSEVENT_TYPE_DIRECTORY = 'directory';
  39996. exports.FSEVENT_TYPE_SYMLINK = 'symlink';
  39997. exports.KEY_LISTENERS = 'listeners';
  39998. exports.KEY_ERR = 'errHandlers';
  39999. exports.KEY_RAW = 'rawEmitters';
  40000. exports.HANDLER_KEYS = [exports.KEY_LISTENERS, exports.KEY_ERR, exports.KEY_RAW];
  40001. exports.DOT_SLASH = `.${sep}`;
  40002. exports.BACK_SLASH_RE = /\\/g;
  40003. exports.DOUBLE_SLASH_RE = /\/\//;
  40004. exports.SLASH_OR_BACK_SLASH_RE = /[/\\]/;
  40005. exports.DOT_RE = /\..*\.(sw[px])$|~$|\.subl.*\.tmp/;
  40006. exports.REPLACER_RE = /^\.[/\\]/;
  40007. exports.SLASH = '/';
  40008. exports.SLASH_SLASH = '//';
  40009. exports.BRACE_START = '{';
  40010. exports.BANG = '!';
  40011. exports.ONE_DOT = '.';
  40012. exports.TWO_DOTS = '..';
  40013. exports.STAR = '*';
  40014. exports.GLOBSTAR = '**';
  40015. exports.ROOT_GLOBSTAR = '/**/*';
  40016. exports.SLASH_GLOBSTAR = '/**';
  40017. exports.DIR_SUFFIX = 'Dir';
  40018. exports.ANYMATCH_OPTS = {dot: true};
  40019. exports.STRING_TYPE = 'string';
  40020. exports.FUNCTION_TYPE = 'function';
  40021. exports.EMPTY_STR = '';
  40022. exports.EMPTY_FN = () => {};
  40023. exports.IDENTITY_FN = val => val;
  40024. exports.isWindows = platform === 'win32';
  40025. exports.isMacos = platform === 'darwin';
  40026. exports.isLinux = platform === 'linux';
  40027. exports.isIBMi = os.type() === 'OS400';
  40028. } (constants$1));
  40029. const fs$7 = require$$0__default;
  40030. const sysPath$2 = require$$0$4;
  40031. const { promisify: promisify$2 } = require$$0$5;
  40032. const isBinaryPath = isBinaryPath$1;
  40033. const {
  40034. isWindows: isWindows$2,
  40035. isLinux,
  40036. EMPTY_FN: EMPTY_FN$2,
  40037. EMPTY_STR: EMPTY_STR$1,
  40038. KEY_LISTENERS,
  40039. KEY_ERR,
  40040. KEY_RAW,
  40041. HANDLER_KEYS,
  40042. EV_CHANGE: EV_CHANGE$2,
  40043. EV_ADD: EV_ADD$2,
  40044. EV_ADD_DIR: EV_ADD_DIR$2,
  40045. EV_ERROR: EV_ERROR$2,
  40046. STR_DATA: STR_DATA$1,
  40047. STR_END: STR_END$2,
  40048. BRACE_START: BRACE_START$1,
  40049. STAR
  40050. } = constants$1;
  40051. const THROTTLE_MODE_WATCH = 'watch';
  40052. const open$2 = promisify$2(fs$7.open);
  40053. const stat$2 = promisify$2(fs$7.stat);
  40054. const lstat$1 = promisify$2(fs$7.lstat);
  40055. const close = promisify$2(fs$7.close);
  40056. const fsrealpath = promisify$2(fs$7.realpath);
  40057. const statMethods$1 = { lstat: lstat$1, stat: stat$2 };
  40058. // TODO: emit errors properly. Example: EMFILE on Macos.
  40059. const foreach = (val, fn) => {
  40060. if (val instanceof Set) {
  40061. val.forEach(fn);
  40062. } else {
  40063. fn(val);
  40064. }
  40065. };
  40066. const addAndConvert = (main, prop, item) => {
  40067. let container = main[prop];
  40068. if (!(container instanceof Set)) {
  40069. main[prop] = container = new Set([container]);
  40070. }
  40071. container.add(item);
  40072. };
  40073. const clearItem = cont => key => {
  40074. const set = cont[key];
  40075. if (set instanceof Set) {
  40076. set.clear();
  40077. } else {
  40078. delete cont[key];
  40079. }
  40080. };
  40081. const delFromSet = (main, prop, item) => {
  40082. const container = main[prop];
  40083. if (container instanceof Set) {
  40084. container.delete(item);
  40085. } else if (container === item) {
  40086. delete main[prop];
  40087. }
  40088. };
  40089. const isEmptySet = (val) => val instanceof Set ? val.size === 0 : !val;
  40090. /**
  40091. * @typedef {String} Path
  40092. */
  40093. // fs_watch helpers
  40094. // object to hold per-process fs_watch instances
  40095. // (may be shared across chokidar FSWatcher instances)
  40096. /**
  40097. * @typedef {Object} FsWatchContainer
  40098. * @property {Set} listeners
  40099. * @property {Set} errHandlers
  40100. * @property {Set} rawEmitters
  40101. * @property {fs.FSWatcher=} watcher
  40102. * @property {Boolean=} watcherUnusable
  40103. */
  40104. /**
  40105. * @type {Map<String,FsWatchContainer>}
  40106. */
  40107. const FsWatchInstances = new Map();
  40108. /**
  40109. * Instantiates the fs_watch interface
  40110. * @param {String} path to be watched
  40111. * @param {Object} options to be passed to fs_watch
  40112. * @param {Function} listener main event handler
  40113. * @param {Function} errHandler emits info about errors
  40114. * @param {Function} emitRaw emits raw event data
  40115. * @returns {fs.FSWatcher} new fsevents instance
  40116. */
  40117. function createFsWatchInstance(path, options, listener, errHandler, emitRaw) {
  40118. const handleEvent = (rawEvent, evPath) => {
  40119. listener(path);
  40120. emitRaw(rawEvent, evPath, {watchedPath: path});
  40121. // emit based on events occurring for files from a directory's watcher in
  40122. // case the file's watcher misses it (and rely on throttling to de-dupe)
  40123. if (evPath && path !== evPath) {
  40124. fsWatchBroadcast(
  40125. sysPath$2.resolve(path, evPath), KEY_LISTENERS, sysPath$2.join(path, evPath)
  40126. );
  40127. }
  40128. };
  40129. try {
  40130. return fs$7.watch(path, options, handleEvent);
  40131. } catch (error) {
  40132. errHandler(error);
  40133. }
  40134. }
  40135. /**
  40136. * Helper for passing fs_watch event data to a collection of listeners
  40137. * @param {Path} fullPath absolute path bound to fs_watch instance
  40138. * @param {String} type listener type
  40139. * @param {*=} val1 arguments to be passed to listeners
  40140. * @param {*=} val2
  40141. * @param {*=} val3
  40142. */
  40143. const fsWatchBroadcast = (fullPath, type, val1, val2, val3) => {
  40144. const cont = FsWatchInstances.get(fullPath);
  40145. if (!cont) return;
  40146. foreach(cont[type], (listener) => {
  40147. listener(val1, val2, val3);
  40148. });
  40149. };
  40150. /**
  40151. * Instantiates the fs_watch interface or binds listeners
  40152. * to an existing one covering the same file system entry
  40153. * @param {String} path
  40154. * @param {String} fullPath absolute path
  40155. * @param {Object} options to be passed to fs_watch
  40156. * @param {Object} handlers container for event listener functions
  40157. */
  40158. const setFsWatchListener = (path, fullPath, options, handlers) => {
  40159. const {listener, errHandler, rawEmitter} = handlers;
  40160. let cont = FsWatchInstances.get(fullPath);
  40161. /** @type {fs.FSWatcher=} */
  40162. let watcher;
  40163. if (!options.persistent) {
  40164. watcher = createFsWatchInstance(
  40165. path, options, listener, errHandler, rawEmitter
  40166. );
  40167. return watcher.close.bind(watcher);
  40168. }
  40169. if (cont) {
  40170. addAndConvert(cont, KEY_LISTENERS, listener);
  40171. addAndConvert(cont, KEY_ERR, errHandler);
  40172. addAndConvert(cont, KEY_RAW, rawEmitter);
  40173. } else {
  40174. watcher = createFsWatchInstance(
  40175. path,
  40176. options,
  40177. fsWatchBroadcast.bind(null, fullPath, KEY_LISTENERS),
  40178. errHandler, // no need to use broadcast here
  40179. fsWatchBroadcast.bind(null, fullPath, KEY_RAW)
  40180. );
  40181. if (!watcher) return;
  40182. watcher.on(EV_ERROR$2, async (error) => {
  40183. const broadcastErr = fsWatchBroadcast.bind(null, fullPath, KEY_ERR);
  40184. cont.watcherUnusable = true; // documented since Node 10.4.1
  40185. // Workaround for https://github.com/joyent/node/issues/4337
  40186. if (isWindows$2 && error.code === 'EPERM') {
  40187. try {
  40188. const fd = await open$2(path, 'r');
  40189. await close(fd);
  40190. broadcastErr(error);
  40191. } catch (err) {}
  40192. } else {
  40193. broadcastErr(error);
  40194. }
  40195. });
  40196. cont = {
  40197. listeners: listener,
  40198. errHandlers: errHandler,
  40199. rawEmitters: rawEmitter,
  40200. watcher
  40201. };
  40202. FsWatchInstances.set(fullPath, cont);
  40203. }
  40204. // const index = cont.listeners.indexOf(listener);
  40205. // removes this instance's listeners and closes the underlying fs_watch
  40206. // instance if there are no more listeners left
  40207. return () => {
  40208. delFromSet(cont, KEY_LISTENERS, listener);
  40209. delFromSet(cont, KEY_ERR, errHandler);
  40210. delFromSet(cont, KEY_RAW, rawEmitter);
  40211. if (isEmptySet(cont.listeners)) {
  40212. // Check to protect against issue gh-730.
  40213. // if (cont.watcherUnusable) {
  40214. cont.watcher.close();
  40215. // }
  40216. FsWatchInstances.delete(fullPath);
  40217. HANDLER_KEYS.forEach(clearItem(cont));
  40218. cont.watcher = undefined;
  40219. Object.freeze(cont);
  40220. }
  40221. };
  40222. };
  40223. // fs_watchFile helpers
  40224. // object to hold per-process fs_watchFile instances
  40225. // (may be shared across chokidar FSWatcher instances)
  40226. const FsWatchFileInstances = new Map();
  40227. /**
  40228. * Instantiates the fs_watchFile interface or binds listeners
  40229. * to an existing one covering the same file system entry
  40230. * @param {String} path to be watched
  40231. * @param {String} fullPath absolute path
  40232. * @param {Object} options options to be passed to fs_watchFile
  40233. * @param {Object} handlers container for event listener functions
  40234. * @returns {Function} closer
  40235. */
  40236. const setFsWatchFileListener = (path, fullPath, options, handlers) => {
  40237. const {listener, rawEmitter} = handlers;
  40238. let cont = FsWatchFileInstances.get(fullPath);
  40239. const copts = cont && cont.options;
  40240. if (copts && (copts.persistent < options.persistent || copts.interval > options.interval)) {
  40241. fs$7.unwatchFile(fullPath);
  40242. cont = undefined;
  40243. }
  40244. /* eslint-enable no-unused-vars, prefer-destructuring */
  40245. if (cont) {
  40246. addAndConvert(cont, KEY_LISTENERS, listener);
  40247. addAndConvert(cont, KEY_RAW, rawEmitter);
  40248. } else {
  40249. // TODO
  40250. // listeners.add(listener);
  40251. // rawEmitters.add(rawEmitter);
  40252. cont = {
  40253. listeners: listener,
  40254. rawEmitters: rawEmitter,
  40255. options,
  40256. watcher: fs$7.watchFile(fullPath, options, (curr, prev) => {
  40257. foreach(cont.rawEmitters, (rawEmitter) => {
  40258. rawEmitter(EV_CHANGE$2, fullPath, {curr, prev});
  40259. });
  40260. const currmtime = curr.mtimeMs;
  40261. if (curr.size !== prev.size || currmtime > prev.mtimeMs || currmtime === 0) {
  40262. foreach(cont.listeners, (listener) => listener(path, curr));
  40263. }
  40264. })
  40265. };
  40266. FsWatchFileInstances.set(fullPath, cont);
  40267. }
  40268. // const index = cont.listeners.indexOf(listener);
  40269. // Removes this instance's listeners and closes the underlying fs_watchFile
  40270. // instance if there are no more listeners left.
  40271. return () => {
  40272. delFromSet(cont, KEY_LISTENERS, listener);
  40273. delFromSet(cont, KEY_RAW, rawEmitter);
  40274. if (isEmptySet(cont.listeners)) {
  40275. FsWatchFileInstances.delete(fullPath);
  40276. fs$7.unwatchFile(fullPath);
  40277. cont.options = cont.watcher = undefined;
  40278. Object.freeze(cont);
  40279. }
  40280. };
  40281. };
  40282. /**
  40283. * @mixin
  40284. */
  40285. let NodeFsHandler$1 = class NodeFsHandler {
  40286. /**
  40287. * @param {import("../index").FSWatcher} fsW
  40288. */
  40289. constructor(fsW) {
  40290. this.fsw = fsW;
  40291. this._boundHandleError = (error) => fsW._handleError(error);
  40292. }
  40293. /**
  40294. * Watch file for changes with fs_watchFile or fs_watch.
  40295. * @param {String} path to file or dir
  40296. * @param {Function} listener on fs change
  40297. * @returns {Function} closer for the watcher instance
  40298. */
  40299. _watchWithNodeFs(path, listener) {
  40300. const opts = this.fsw.options;
  40301. const directory = sysPath$2.dirname(path);
  40302. const basename = sysPath$2.basename(path);
  40303. const parent = this.fsw._getWatchedDir(directory);
  40304. parent.add(basename);
  40305. const absolutePath = sysPath$2.resolve(path);
  40306. const options = {persistent: opts.persistent};
  40307. if (!listener) listener = EMPTY_FN$2;
  40308. let closer;
  40309. if (opts.usePolling) {
  40310. options.interval = opts.enableBinaryInterval && isBinaryPath(basename) ?
  40311. opts.binaryInterval : opts.interval;
  40312. closer = setFsWatchFileListener(path, absolutePath, options, {
  40313. listener,
  40314. rawEmitter: this.fsw._emitRaw
  40315. });
  40316. } else {
  40317. closer = setFsWatchListener(path, absolutePath, options, {
  40318. listener,
  40319. errHandler: this._boundHandleError,
  40320. rawEmitter: this.fsw._emitRaw
  40321. });
  40322. }
  40323. return closer;
  40324. }
  40325. /**
  40326. * Watch a file and emit add event if warranted.
  40327. * @param {Path} file Path
  40328. * @param {fs.Stats} stats result of fs_stat
  40329. * @param {Boolean} initialAdd was the file added at watch instantiation?
  40330. * @returns {Function} closer for the watcher instance
  40331. */
  40332. _handleFile(file, stats, initialAdd) {
  40333. if (this.fsw.closed) {
  40334. return;
  40335. }
  40336. const dirname = sysPath$2.dirname(file);
  40337. const basename = sysPath$2.basename(file);
  40338. const parent = this.fsw._getWatchedDir(dirname);
  40339. // stats is always present
  40340. let prevStats = stats;
  40341. // if the file is already being watched, do nothing
  40342. if (parent.has(basename)) return;
  40343. const listener = async (path, newStats) => {
  40344. if (!this.fsw._throttle(THROTTLE_MODE_WATCH, file, 5)) return;
  40345. if (!newStats || newStats.mtimeMs === 0) {
  40346. try {
  40347. const newStats = await stat$2(file);
  40348. if (this.fsw.closed) return;
  40349. // Check that change event was not fired because of changed only accessTime.
  40350. const at = newStats.atimeMs;
  40351. const mt = newStats.mtimeMs;
  40352. if (!at || at <= mt || mt !== prevStats.mtimeMs) {
  40353. this.fsw._emit(EV_CHANGE$2, file, newStats);
  40354. }
  40355. if (isLinux && prevStats.ino !== newStats.ino) {
  40356. this.fsw._closeFile(path);
  40357. prevStats = newStats;
  40358. this.fsw._addPathCloser(path, this._watchWithNodeFs(file, listener));
  40359. } else {
  40360. prevStats = newStats;
  40361. }
  40362. } catch (error) {
  40363. // Fix issues where mtime is null but file is still present
  40364. this.fsw._remove(dirname, basename);
  40365. }
  40366. // add is about to be emitted if file not already tracked in parent
  40367. } else if (parent.has(basename)) {
  40368. // Check that change event was not fired because of changed only accessTime.
  40369. const at = newStats.atimeMs;
  40370. const mt = newStats.mtimeMs;
  40371. if (!at || at <= mt || mt !== prevStats.mtimeMs) {
  40372. this.fsw._emit(EV_CHANGE$2, file, newStats);
  40373. }
  40374. prevStats = newStats;
  40375. }
  40376. };
  40377. // kick off the watcher
  40378. const closer = this._watchWithNodeFs(file, listener);
  40379. // emit an add event if we're supposed to
  40380. if (!(initialAdd && this.fsw.options.ignoreInitial) && this.fsw._isntIgnored(file)) {
  40381. if (!this.fsw._throttle(EV_ADD$2, file, 0)) return;
  40382. this.fsw._emit(EV_ADD$2, file, stats);
  40383. }
  40384. return closer;
  40385. }
  40386. /**
  40387. * Handle symlinks encountered while reading a dir.
  40388. * @param {Object} entry returned by readdirp
  40389. * @param {String} directory path of dir being read
  40390. * @param {String} path of this item
  40391. * @param {String} item basename of this item
  40392. * @returns {Promise<Boolean>} true if no more processing is needed for this entry.
  40393. */
  40394. async _handleSymlink(entry, directory, path, item) {
  40395. if (this.fsw.closed) {
  40396. return;
  40397. }
  40398. const full = entry.fullPath;
  40399. const dir = this.fsw._getWatchedDir(directory);
  40400. if (!this.fsw.options.followSymlinks) {
  40401. // watch symlink directly (don't follow) and detect changes
  40402. this.fsw._incrReadyCount();
  40403. let linkPath;
  40404. try {
  40405. linkPath = await fsrealpath(path);
  40406. } catch (e) {
  40407. this.fsw._emitReady();
  40408. return true;
  40409. }
  40410. if (this.fsw.closed) return;
  40411. if (dir.has(item)) {
  40412. if (this.fsw._symlinkPaths.get(full) !== linkPath) {
  40413. this.fsw._symlinkPaths.set(full, linkPath);
  40414. this.fsw._emit(EV_CHANGE$2, path, entry.stats);
  40415. }
  40416. } else {
  40417. dir.add(item);
  40418. this.fsw._symlinkPaths.set(full, linkPath);
  40419. this.fsw._emit(EV_ADD$2, path, entry.stats);
  40420. }
  40421. this.fsw._emitReady();
  40422. return true;
  40423. }
  40424. // don't follow the same symlink more than once
  40425. if (this.fsw._symlinkPaths.has(full)) {
  40426. return true;
  40427. }
  40428. this.fsw._symlinkPaths.set(full, true);
  40429. }
  40430. _handleRead(directory, initialAdd, wh, target, dir, depth, throttler) {
  40431. // Normalize the directory name on Windows
  40432. directory = sysPath$2.join(directory, EMPTY_STR$1);
  40433. if (!wh.hasGlob) {
  40434. throttler = this.fsw._throttle('readdir', directory, 1000);
  40435. if (!throttler) return;
  40436. }
  40437. const previous = this.fsw._getWatchedDir(wh.path);
  40438. const current = new Set();
  40439. let stream = this.fsw._readdirp(directory, {
  40440. fileFilter: entry => wh.filterPath(entry),
  40441. directoryFilter: entry => wh.filterDir(entry),
  40442. depth: 0
  40443. }).on(STR_DATA$1, async (entry) => {
  40444. if (this.fsw.closed) {
  40445. stream = undefined;
  40446. return;
  40447. }
  40448. const item = entry.path;
  40449. let path = sysPath$2.join(directory, item);
  40450. current.add(item);
  40451. if (entry.stats.isSymbolicLink() && await this._handleSymlink(entry, directory, path, item)) {
  40452. return;
  40453. }
  40454. if (this.fsw.closed) {
  40455. stream = undefined;
  40456. return;
  40457. }
  40458. // Files that present in current directory snapshot
  40459. // but absent in previous are added to watch list and
  40460. // emit `add` event.
  40461. if (item === target || !target && !previous.has(item)) {
  40462. this.fsw._incrReadyCount();
  40463. // ensure relativeness of path is preserved in case of watcher reuse
  40464. path = sysPath$2.join(dir, sysPath$2.relative(dir, path));
  40465. this._addToNodeFs(path, initialAdd, wh, depth + 1);
  40466. }
  40467. }).on(EV_ERROR$2, this._boundHandleError);
  40468. return new Promise(resolve =>
  40469. stream.once(STR_END$2, () => {
  40470. if (this.fsw.closed) {
  40471. stream = undefined;
  40472. return;
  40473. }
  40474. const wasThrottled = throttler ? throttler.clear() : false;
  40475. resolve();
  40476. // Files that absent in current directory snapshot
  40477. // but present in previous emit `remove` event
  40478. // and are removed from @watched[directory].
  40479. previous.getChildren().filter((item) => {
  40480. return item !== directory &&
  40481. !current.has(item) &&
  40482. // in case of intersecting globs;
  40483. // a path may have been filtered out of this readdir, but
  40484. // shouldn't be removed because it matches a different glob
  40485. (!wh.hasGlob || wh.filterPath({
  40486. fullPath: sysPath$2.resolve(directory, item)
  40487. }));
  40488. }).forEach((item) => {
  40489. this.fsw._remove(directory, item);
  40490. });
  40491. stream = undefined;
  40492. // one more time for any missed in case changes came in extremely quickly
  40493. if (wasThrottled) this._handleRead(directory, false, wh, target, dir, depth, throttler);
  40494. })
  40495. );
  40496. }
  40497. /**
  40498. * Read directory to add / remove files from `@watched` list and re-read it on change.
  40499. * @param {String} dir fs path
  40500. * @param {fs.Stats} stats
  40501. * @param {Boolean} initialAdd
  40502. * @param {Number} depth relative to user-supplied path
  40503. * @param {String} target child path targeted for watch
  40504. * @param {Object} wh Common watch helpers for this path
  40505. * @param {String} realpath
  40506. * @returns {Promise<Function>} closer for the watcher instance.
  40507. */
  40508. async _handleDir(dir, stats, initialAdd, depth, target, wh, realpath) {
  40509. const parentDir = this.fsw._getWatchedDir(sysPath$2.dirname(dir));
  40510. const tracked = parentDir.has(sysPath$2.basename(dir));
  40511. if (!(initialAdd && this.fsw.options.ignoreInitial) && !target && !tracked) {
  40512. if (!wh.hasGlob || wh.globFilter(dir)) this.fsw._emit(EV_ADD_DIR$2, dir, stats);
  40513. }
  40514. // ensure dir is tracked (harmless if redundant)
  40515. parentDir.add(sysPath$2.basename(dir));
  40516. this.fsw._getWatchedDir(dir);
  40517. let throttler;
  40518. let closer;
  40519. const oDepth = this.fsw.options.depth;
  40520. if ((oDepth == null || depth <= oDepth) && !this.fsw._symlinkPaths.has(realpath)) {
  40521. if (!target) {
  40522. await this._handleRead(dir, initialAdd, wh, target, dir, depth, throttler);
  40523. if (this.fsw.closed) return;
  40524. }
  40525. closer = this._watchWithNodeFs(dir, (dirPath, stats) => {
  40526. // if current directory is removed, do nothing
  40527. if (stats && stats.mtimeMs === 0) return;
  40528. this._handleRead(dirPath, false, wh, target, dir, depth, throttler);
  40529. });
  40530. }
  40531. return closer;
  40532. }
  40533. /**
  40534. * Handle added file, directory, or glob pattern.
  40535. * Delegates call to _handleFile / _handleDir after checks.
  40536. * @param {String} path to file or ir
  40537. * @param {Boolean} initialAdd was the file added at watch instantiation?
  40538. * @param {Object} priorWh depth relative to user-supplied path
  40539. * @param {Number} depth Child path actually targeted for watch
  40540. * @param {String=} target Child path actually targeted for watch
  40541. * @returns {Promise}
  40542. */
  40543. async _addToNodeFs(path, initialAdd, priorWh, depth, target) {
  40544. const ready = this.fsw._emitReady;
  40545. if (this.fsw._isIgnored(path) || this.fsw.closed) {
  40546. ready();
  40547. return false;
  40548. }
  40549. const wh = this.fsw._getWatchHelpers(path, depth);
  40550. if (!wh.hasGlob && priorWh) {
  40551. wh.hasGlob = priorWh.hasGlob;
  40552. wh.globFilter = priorWh.globFilter;
  40553. wh.filterPath = entry => priorWh.filterPath(entry);
  40554. wh.filterDir = entry => priorWh.filterDir(entry);
  40555. }
  40556. // evaluate what is at the path we're being asked to watch
  40557. try {
  40558. const stats = await statMethods$1[wh.statMethod](wh.watchPath);
  40559. if (this.fsw.closed) return;
  40560. if (this.fsw._isIgnored(wh.watchPath, stats)) {
  40561. ready();
  40562. return false;
  40563. }
  40564. const follow = this.fsw.options.followSymlinks && !path.includes(STAR) && !path.includes(BRACE_START$1);
  40565. let closer;
  40566. if (stats.isDirectory()) {
  40567. const absPath = sysPath$2.resolve(path);
  40568. const targetPath = follow ? await fsrealpath(path) : path;
  40569. if (this.fsw.closed) return;
  40570. closer = await this._handleDir(wh.watchPath, stats, initialAdd, depth, target, wh, targetPath);
  40571. if (this.fsw.closed) return;
  40572. // preserve this symlink's target path
  40573. if (absPath !== targetPath && targetPath !== undefined) {
  40574. this.fsw._symlinkPaths.set(absPath, targetPath);
  40575. }
  40576. } else if (stats.isSymbolicLink()) {
  40577. const targetPath = follow ? await fsrealpath(path) : path;
  40578. if (this.fsw.closed) return;
  40579. const parent = sysPath$2.dirname(wh.watchPath);
  40580. this.fsw._getWatchedDir(parent).add(wh.watchPath);
  40581. this.fsw._emit(EV_ADD$2, wh.watchPath, stats);
  40582. closer = await this._handleDir(parent, stats, initialAdd, depth, path, wh, targetPath);
  40583. if (this.fsw.closed) return;
  40584. // preserve this symlink's target path
  40585. if (targetPath !== undefined) {
  40586. this.fsw._symlinkPaths.set(sysPath$2.resolve(path), targetPath);
  40587. }
  40588. } else {
  40589. closer = this._handleFile(wh.watchPath, stats, initialAdd);
  40590. }
  40591. ready();
  40592. this.fsw._addPathCloser(path, closer);
  40593. return false;
  40594. } catch (error) {
  40595. if (this.fsw._handleError(error)) {
  40596. ready();
  40597. return path;
  40598. }
  40599. }
  40600. }
  40601. };
  40602. var nodefsHandler = NodeFsHandler$1;
  40603. var fseventsHandler = {exports: {}};
  40604. const fs$6 = require$$0__default;
  40605. const sysPath$1 = require$$0$4;
  40606. const { promisify: promisify$1 } = require$$0$5;
  40607. let fsevents;
  40608. try {
  40609. fsevents = __require('fsevents');
  40610. } catch (error) {
  40611. if (process.env.CHOKIDAR_PRINT_FSEVENTS_REQUIRE_ERROR) console.error(error);
  40612. }
  40613. if (fsevents) {
  40614. // TODO: real check
  40615. const mtch = process.version.match(/v(\d+)\.(\d+)/);
  40616. if (mtch && mtch[1] && mtch[2]) {
  40617. const maj = Number.parseInt(mtch[1], 10);
  40618. const min = Number.parseInt(mtch[2], 10);
  40619. if (maj === 8 && min < 16) {
  40620. fsevents = undefined;
  40621. }
  40622. }
  40623. }
  40624. const {
  40625. EV_ADD: EV_ADD$1,
  40626. EV_CHANGE: EV_CHANGE$1,
  40627. EV_ADD_DIR: EV_ADD_DIR$1,
  40628. EV_UNLINK: EV_UNLINK$1,
  40629. EV_ERROR: EV_ERROR$1,
  40630. STR_DATA,
  40631. STR_END: STR_END$1,
  40632. FSEVENT_CREATED,
  40633. FSEVENT_MODIFIED,
  40634. FSEVENT_DELETED,
  40635. FSEVENT_MOVED,
  40636. // FSEVENT_CLONED,
  40637. FSEVENT_UNKNOWN,
  40638. FSEVENT_FLAG_MUST_SCAN_SUBDIRS,
  40639. FSEVENT_TYPE_FILE,
  40640. FSEVENT_TYPE_DIRECTORY,
  40641. FSEVENT_TYPE_SYMLINK,
  40642. ROOT_GLOBSTAR,
  40643. DIR_SUFFIX,
  40644. DOT_SLASH,
  40645. FUNCTION_TYPE: FUNCTION_TYPE$1,
  40646. EMPTY_FN: EMPTY_FN$1,
  40647. IDENTITY_FN
  40648. } = constants$1;
  40649. const Depth = (value) => isNaN(value) ? {} : {depth: value};
  40650. const stat$1 = promisify$1(fs$6.stat);
  40651. const lstat = promisify$1(fs$6.lstat);
  40652. const realpath = promisify$1(fs$6.realpath);
  40653. const statMethods = { stat: stat$1, lstat };
  40654. /**
  40655. * @typedef {String} Path
  40656. */
  40657. /**
  40658. * @typedef {Object} FsEventsWatchContainer
  40659. * @property {Set<Function>} listeners
  40660. * @property {Function} rawEmitter
  40661. * @property {{stop: Function}} watcher
  40662. */
  40663. // fsevents instance helper functions
  40664. /**
  40665. * Object to hold per-process fsevents instances (may be shared across chokidar FSWatcher instances)
  40666. * @type {Map<Path,FsEventsWatchContainer>}
  40667. */
  40668. const FSEventsWatchers = new Map();
  40669. // Threshold of duplicate path prefixes at which to start
  40670. // consolidating going forward
  40671. const consolidateThreshhold = 10;
  40672. const wrongEventFlags = new Set([
  40673. 69888, 70400, 71424, 72704, 73472, 131328, 131840, 262912
  40674. ]);
  40675. /**
  40676. * Instantiates the fsevents interface
  40677. * @param {Path} path path to be watched
  40678. * @param {Function} callback called when fsevents is bound and ready
  40679. * @returns {{stop: Function}} new fsevents instance
  40680. */
  40681. const createFSEventsInstance = (path, callback) => {
  40682. const stop = fsevents.watch(path, callback);
  40683. return {stop};
  40684. };
  40685. /**
  40686. * Instantiates the fsevents interface or binds listeners to an existing one covering
  40687. * the same file tree.
  40688. * @param {Path} path - to be watched
  40689. * @param {Path} realPath - real path for symlinks
  40690. * @param {Function} listener - called when fsevents emits events
  40691. * @param {Function} rawEmitter - passes data to listeners of the 'raw' event
  40692. * @returns {Function} closer
  40693. */
  40694. function setFSEventsListener(path, realPath, listener, rawEmitter) {
  40695. let watchPath = sysPath$1.extname(realPath) ? sysPath$1.dirname(realPath) : realPath;
  40696. const parentPath = sysPath$1.dirname(watchPath);
  40697. let cont = FSEventsWatchers.get(watchPath);
  40698. // If we've accumulated a substantial number of paths that
  40699. // could have been consolidated by watching one directory
  40700. // above the current one, create a watcher on the parent
  40701. // path instead, so that we do consolidate going forward.
  40702. if (couldConsolidate(parentPath)) {
  40703. watchPath = parentPath;
  40704. }
  40705. const resolvedPath = sysPath$1.resolve(path);
  40706. const hasSymlink = resolvedPath !== realPath;
  40707. const filteredListener = (fullPath, flags, info) => {
  40708. if (hasSymlink) fullPath = fullPath.replace(realPath, resolvedPath);
  40709. if (
  40710. fullPath === resolvedPath ||
  40711. !fullPath.indexOf(resolvedPath + sysPath$1.sep)
  40712. ) listener(fullPath, flags, info);
  40713. };
  40714. // check if there is already a watcher on a parent path
  40715. // modifies `watchPath` to the parent path when it finds a match
  40716. let watchedParent = false;
  40717. for (const watchedPath of FSEventsWatchers.keys()) {
  40718. if (realPath.indexOf(sysPath$1.resolve(watchedPath) + sysPath$1.sep) === 0) {
  40719. watchPath = watchedPath;
  40720. cont = FSEventsWatchers.get(watchPath);
  40721. watchedParent = true;
  40722. break;
  40723. }
  40724. }
  40725. if (cont || watchedParent) {
  40726. cont.listeners.add(filteredListener);
  40727. } else {
  40728. cont = {
  40729. listeners: new Set([filteredListener]),
  40730. rawEmitter,
  40731. watcher: createFSEventsInstance(watchPath, (fullPath, flags) => {
  40732. if (!cont.listeners.size) return;
  40733. if (flags & FSEVENT_FLAG_MUST_SCAN_SUBDIRS) return;
  40734. const info = fsevents.getInfo(fullPath, flags);
  40735. cont.listeners.forEach(list => {
  40736. list(fullPath, flags, info);
  40737. });
  40738. cont.rawEmitter(info.event, fullPath, info);
  40739. })
  40740. };
  40741. FSEventsWatchers.set(watchPath, cont);
  40742. }
  40743. // removes this instance's listeners and closes the underlying fsevents
  40744. // instance if there are no more listeners left
  40745. return () => {
  40746. const lst = cont.listeners;
  40747. lst.delete(filteredListener);
  40748. if (!lst.size) {
  40749. FSEventsWatchers.delete(watchPath);
  40750. if (cont.watcher) return cont.watcher.stop().then(() => {
  40751. cont.rawEmitter = cont.watcher = undefined;
  40752. Object.freeze(cont);
  40753. });
  40754. }
  40755. };
  40756. }
  40757. // Decide whether or not we should start a new higher-level
  40758. // parent watcher
  40759. const couldConsolidate = (path) => {
  40760. let count = 0;
  40761. for (const watchPath of FSEventsWatchers.keys()) {
  40762. if (watchPath.indexOf(path) === 0) {
  40763. count++;
  40764. if (count >= consolidateThreshhold) {
  40765. return true;
  40766. }
  40767. }
  40768. }
  40769. return false;
  40770. };
  40771. // returns boolean indicating whether fsevents can be used
  40772. const canUse = () => fsevents && FSEventsWatchers.size < 128;
  40773. // determines subdirectory traversal levels from root to path
  40774. const calcDepth = (path, root) => {
  40775. let i = 0;
  40776. while (!path.indexOf(root) && (path = sysPath$1.dirname(path)) !== root) i++;
  40777. return i;
  40778. };
  40779. // returns boolean indicating whether the fsevents' event info has the same type
  40780. // as the one returned by fs.stat
  40781. const sameTypes = (info, stats) => (
  40782. info.type === FSEVENT_TYPE_DIRECTORY && stats.isDirectory() ||
  40783. info.type === FSEVENT_TYPE_SYMLINK && stats.isSymbolicLink() ||
  40784. info.type === FSEVENT_TYPE_FILE && stats.isFile()
  40785. );
  40786. /**
  40787. * @mixin
  40788. */
  40789. let FsEventsHandler$1 = class FsEventsHandler {
  40790. /**
  40791. * @param {import('../index').FSWatcher} fsw
  40792. */
  40793. constructor(fsw) {
  40794. this.fsw = fsw;
  40795. }
  40796. checkIgnored(path, stats) {
  40797. const ipaths = this.fsw._ignoredPaths;
  40798. if (this.fsw._isIgnored(path, stats)) {
  40799. ipaths.add(path);
  40800. if (stats && stats.isDirectory()) {
  40801. ipaths.add(path + ROOT_GLOBSTAR);
  40802. }
  40803. return true;
  40804. }
  40805. ipaths.delete(path);
  40806. ipaths.delete(path + ROOT_GLOBSTAR);
  40807. }
  40808. addOrChange(path, fullPath, realPath, parent, watchedDir, item, info, opts) {
  40809. const event = watchedDir.has(item) ? EV_CHANGE$1 : EV_ADD$1;
  40810. this.handleEvent(event, path, fullPath, realPath, parent, watchedDir, item, info, opts);
  40811. }
  40812. async checkExists(path, fullPath, realPath, parent, watchedDir, item, info, opts) {
  40813. try {
  40814. const stats = await stat$1(path);
  40815. if (this.fsw.closed) return;
  40816. if (sameTypes(info, stats)) {
  40817. this.addOrChange(path, fullPath, realPath, parent, watchedDir, item, info, opts);
  40818. } else {
  40819. this.handleEvent(EV_UNLINK$1, path, fullPath, realPath, parent, watchedDir, item, info, opts);
  40820. }
  40821. } catch (error) {
  40822. if (error.code === 'EACCES') {
  40823. this.addOrChange(path, fullPath, realPath, parent, watchedDir, item, info, opts);
  40824. } else {
  40825. this.handleEvent(EV_UNLINK$1, path, fullPath, realPath, parent, watchedDir, item, info, opts);
  40826. }
  40827. }
  40828. }
  40829. handleEvent(event, path, fullPath, realPath, parent, watchedDir, item, info, opts) {
  40830. if (this.fsw.closed || this.checkIgnored(path)) return;
  40831. if (event === EV_UNLINK$1) {
  40832. const isDirectory = info.type === FSEVENT_TYPE_DIRECTORY;
  40833. // suppress unlink events on never before seen files
  40834. if (isDirectory || watchedDir.has(item)) {
  40835. this.fsw._remove(parent, item, isDirectory);
  40836. }
  40837. } else {
  40838. if (event === EV_ADD$1) {
  40839. // track new directories
  40840. if (info.type === FSEVENT_TYPE_DIRECTORY) this.fsw._getWatchedDir(path);
  40841. if (info.type === FSEVENT_TYPE_SYMLINK && opts.followSymlinks) {
  40842. // push symlinks back to the top of the stack to get handled
  40843. const curDepth = opts.depth === undefined ?
  40844. undefined : calcDepth(fullPath, realPath) + 1;
  40845. return this._addToFsEvents(path, false, true, curDepth);
  40846. }
  40847. // track new paths
  40848. // (other than symlinks being followed, which will be tracked soon)
  40849. this.fsw._getWatchedDir(parent).add(item);
  40850. }
  40851. /**
  40852. * @type {'add'|'addDir'|'unlink'|'unlinkDir'}
  40853. */
  40854. const eventName = info.type === FSEVENT_TYPE_DIRECTORY ? event + DIR_SUFFIX : event;
  40855. this.fsw._emit(eventName, path);
  40856. if (eventName === EV_ADD_DIR$1) this._addToFsEvents(path, false, true);
  40857. }
  40858. }
  40859. /**
  40860. * Handle symlinks encountered during directory scan
  40861. * @param {String} watchPath - file/dir path to be watched with fsevents
  40862. * @param {String} realPath - real path (in case of symlinks)
  40863. * @param {Function} transform - path transformer
  40864. * @param {Function} globFilter - path filter in case a glob pattern was provided
  40865. * @returns {Function} closer for the watcher instance
  40866. */
  40867. _watchWithFsEvents(watchPath, realPath, transform, globFilter) {
  40868. if (this.fsw.closed || this.fsw._isIgnored(watchPath)) return;
  40869. const opts = this.fsw.options;
  40870. const watchCallback = async (fullPath, flags, info) => {
  40871. // PATCH: bypass the callback for better perf when fullPath hit the ignored file list
  40872. if (this.fsw.closed || this.fsw._isIgnored(fullPath)) return;
  40873. if (
  40874. opts.depth !== undefined &&
  40875. calcDepth(fullPath, realPath) > opts.depth
  40876. ) return;
  40877. const path = transform(sysPath$1.join(
  40878. watchPath, sysPath$1.relative(watchPath, fullPath)
  40879. ));
  40880. if (globFilter && !globFilter(path)) return;
  40881. // ensure directories are tracked
  40882. const parent = sysPath$1.dirname(path);
  40883. const item = sysPath$1.basename(path);
  40884. const watchedDir = this.fsw._getWatchedDir(
  40885. info.type === FSEVENT_TYPE_DIRECTORY ? path : parent
  40886. );
  40887. // correct for wrong events emitted
  40888. if (wrongEventFlags.has(flags) || info.event === FSEVENT_UNKNOWN) {
  40889. if (typeof opts.ignored === FUNCTION_TYPE$1) {
  40890. let stats;
  40891. try {
  40892. stats = await stat$1(path);
  40893. } catch (error) {}
  40894. if (this.fsw.closed) return;
  40895. if (this.checkIgnored(path, stats)) return;
  40896. if (sameTypes(info, stats)) {
  40897. this.addOrChange(path, fullPath, realPath, parent, watchedDir, item, info, opts);
  40898. } else {
  40899. this.handleEvent(EV_UNLINK$1, path, fullPath, realPath, parent, watchedDir, item, info, opts);
  40900. }
  40901. } else {
  40902. this.checkExists(path, fullPath, realPath, parent, watchedDir, item, info, opts);
  40903. }
  40904. } else {
  40905. switch (info.event) {
  40906. case FSEVENT_CREATED:
  40907. case FSEVENT_MODIFIED:
  40908. return this.addOrChange(path, fullPath, realPath, parent, watchedDir, item, info, opts);
  40909. case FSEVENT_DELETED:
  40910. case FSEVENT_MOVED:
  40911. return this.checkExists(path, fullPath, realPath, parent, watchedDir, item, info, opts);
  40912. }
  40913. }
  40914. };
  40915. const closer = setFSEventsListener(
  40916. watchPath,
  40917. realPath,
  40918. watchCallback,
  40919. this.fsw._emitRaw
  40920. );
  40921. this.fsw._emitReady();
  40922. return closer;
  40923. }
  40924. /**
  40925. * Handle symlinks encountered during directory scan
  40926. * @param {String} linkPath path to symlink
  40927. * @param {String} fullPath absolute path to the symlink
  40928. * @param {Function} transform pre-existing path transformer
  40929. * @param {Number} curDepth level of subdirectories traversed to where symlink is
  40930. * @returns {Promise<void>}
  40931. */
  40932. async _handleFsEventsSymlink(linkPath, fullPath, transform, curDepth) {
  40933. // don't follow the same symlink more than once
  40934. if (this.fsw.closed || this.fsw._symlinkPaths.has(fullPath)) return;
  40935. this.fsw._symlinkPaths.set(fullPath, true);
  40936. this.fsw._incrReadyCount();
  40937. try {
  40938. const linkTarget = await realpath(linkPath);
  40939. if (this.fsw.closed) return;
  40940. if (this.fsw._isIgnored(linkTarget)) {
  40941. return this.fsw._emitReady();
  40942. }
  40943. this.fsw._incrReadyCount();
  40944. // add the linkTarget for watching with a wrapper for transform
  40945. // that causes emitted paths to incorporate the link's path
  40946. this._addToFsEvents(linkTarget || linkPath, (path) => {
  40947. let aliasedPath = linkPath;
  40948. if (linkTarget && linkTarget !== DOT_SLASH) {
  40949. aliasedPath = path.replace(linkTarget, linkPath);
  40950. } else if (path !== DOT_SLASH) {
  40951. aliasedPath = sysPath$1.join(linkPath, path);
  40952. }
  40953. return transform(aliasedPath);
  40954. }, false, curDepth);
  40955. } catch(error) {
  40956. if (this.fsw._handleError(error)) {
  40957. return this.fsw._emitReady();
  40958. }
  40959. }
  40960. }
  40961. /**
  40962. *
  40963. * @param {Path} newPath
  40964. * @param {fs.Stats} stats
  40965. */
  40966. emitAdd(newPath, stats, processPath, opts, forceAdd) {
  40967. const pp = processPath(newPath);
  40968. const isDir = stats.isDirectory();
  40969. const dirObj = this.fsw._getWatchedDir(sysPath$1.dirname(pp));
  40970. const base = sysPath$1.basename(pp);
  40971. // ensure empty dirs get tracked
  40972. if (isDir) this.fsw._getWatchedDir(pp);
  40973. if (dirObj.has(base)) return;
  40974. dirObj.add(base);
  40975. if (!opts.ignoreInitial || forceAdd === true) {
  40976. this.fsw._emit(isDir ? EV_ADD_DIR$1 : EV_ADD$1, pp, stats);
  40977. }
  40978. }
  40979. initWatch(realPath, path, wh, processPath) {
  40980. if (this.fsw.closed) return;
  40981. const closer = this._watchWithFsEvents(
  40982. wh.watchPath,
  40983. sysPath$1.resolve(realPath || wh.watchPath),
  40984. processPath,
  40985. wh.globFilter
  40986. );
  40987. this.fsw._addPathCloser(path, closer);
  40988. }
  40989. /**
  40990. * Handle added path with fsevents
  40991. * @param {String} path file/dir path or glob pattern
  40992. * @param {Function|Boolean=} transform converts working path to what the user expects
  40993. * @param {Boolean=} forceAdd ensure add is emitted
  40994. * @param {Number=} priorDepth Level of subdirectories already traversed.
  40995. * @returns {Promise<void>}
  40996. */
  40997. async _addToFsEvents(path, transform, forceAdd, priorDepth) {
  40998. if (this.fsw.closed) {
  40999. return;
  41000. }
  41001. const opts = this.fsw.options;
  41002. const processPath = typeof transform === FUNCTION_TYPE$1 ? transform : IDENTITY_FN;
  41003. const wh = this.fsw._getWatchHelpers(path);
  41004. // evaluate what is at the path we're being asked to watch
  41005. try {
  41006. const stats = await statMethods[wh.statMethod](wh.watchPath);
  41007. if (this.fsw.closed) return;
  41008. if (this.fsw._isIgnored(wh.watchPath, stats)) {
  41009. throw null;
  41010. }
  41011. if (stats.isDirectory()) {
  41012. // emit addDir unless this is a glob parent
  41013. if (!wh.globFilter) this.emitAdd(processPath(path), stats, processPath, opts, forceAdd);
  41014. // don't recurse further if it would exceed depth setting
  41015. if (priorDepth && priorDepth > opts.depth) return;
  41016. // scan the contents of the dir
  41017. this.fsw._readdirp(wh.watchPath, {
  41018. fileFilter: entry => wh.filterPath(entry),
  41019. directoryFilter: entry => wh.filterDir(entry),
  41020. ...Depth(opts.depth - (priorDepth || 0))
  41021. }).on(STR_DATA, (entry) => {
  41022. // need to check filterPath on dirs b/c filterDir is less restrictive
  41023. if (this.fsw.closed) {
  41024. return;
  41025. }
  41026. if (entry.stats.isDirectory() && !wh.filterPath(entry)) return;
  41027. const joinedPath = sysPath$1.join(wh.watchPath, entry.path);
  41028. const {fullPath} = entry;
  41029. if (wh.followSymlinks && entry.stats.isSymbolicLink()) {
  41030. // preserve the current depth here since it can't be derived from
  41031. // real paths past the symlink
  41032. const curDepth = opts.depth === undefined ?
  41033. undefined : calcDepth(joinedPath, sysPath$1.resolve(wh.watchPath)) + 1;
  41034. this._handleFsEventsSymlink(joinedPath, fullPath, processPath, curDepth);
  41035. } else {
  41036. this.emitAdd(joinedPath, entry.stats, processPath, opts, forceAdd);
  41037. }
  41038. }).on(EV_ERROR$1, EMPTY_FN$1).on(STR_END$1, () => {
  41039. this.fsw._emitReady();
  41040. });
  41041. } else {
  41042. this.emitAdd(wh.watchPath, stats, processPath, opts, forceAdd);
  41043. this.fsw._emitReady();
  41044. }
  41045. } catch (error) {
  41046. if (!error || this.fsw._handleError(error)) {
  41047. // TODO: Strange thing: "should not choke on an ignored watch path" will be failed without 2 ready calls -__-
  41048. this.fsw._emitReady();
  41049. this.fsw._emitReady();
  41050. }
  41051. }
  41052. if (opts.persistent && forceAdd !== true) {
  41053. if (typeof transform === FUNCTION_TYPE$1) {
  41054. // realpath has already been resolved
  41055. this.initWatch(undefined, path, wh, processPath);
  41056. } else {
  41057. let realPath;
  41058. try {
  41059. realPath = await realpath(wh.watchPath);
  41060. } catch (e) {}
  41061. this.initWatch(realPath, path, wh, processPath);
  41062. }
  41063. }
  41064. }
  41065. };
  41066. fseventsHandler.exports = FsEventsHandler$1;
  41067. fseventsHandler.exports.canUse = canUse;
  41068. var fseventsHandlerExports = fseventsHandler.exports;
  41069. const { EventEmitter: EventEmitter$2 } = require$$0$7;
  41070. const fs$5 = require$$0__default;
  41071. const sysPath = require$$0$4;
  41072. const { promisify } = require$$0$5;
  41073. const readdirp = readdirp_1;
  41074. const anymatch = anymatchExports.default;
  41075. const globParent = globParent$2;
  41076. const isGlob = isGlob$2;
  41077. const braces = braces_1;
  41078. const normalizePath = normalizePath$2;
  41079. const NodeFsHandler = nodefsHandler;
  41080. const FsEventsHandler = fseventsHandlerExports;
  41081. const {
  41082. EV_ALL,
  41083. EV_READY,
  41084. EV_ADD,
  41085. EV_CHANGE,
  41086. EV_UNLINK,
  41087. EV_ADD_DIR,
  41088. EV_UNLINK_DIR,
  41089. EV_RAW,
  41090. EV_ERROR,
  41091. STR_CLOSE,
  41092. STR_END,
  41093. BACK_SLASH_RE,
  41094. DOUBLE_SLASH_RE,
  41095. SLASH_OR_BACK_SLASH_RE,
  41096. DOT_RE,
  41097. REPLACER_RE,
  41098. SLASH,
  41099. SLASH_SLASH,
  41100. BRACE_START,
  41101. BANG,
  41102. ONE_DOT,
  41103. TWO_DOTS,
  41104. GLOBSTAR,
  41105. SLASH_GLOBSTAR,
  41106. ANYMATCH_OPTS,
  41107. STRING_TYPE,
  41108. FUNCTION_TYPE,
  41109. EMPTY_STR,
  41110. EMPTY_FN,
  41111. isWindows: isWindows$1,
  41112. isMacos,
  41113. isIBMi
  41114. } = constants$1;
  41115. const stat = promisify(fs$5.stat);
  41116. const readdir = promisify(fs$5.readdir);
  41117. /**
  41118. * @typedef {String} Path
  41119. * @typedef {'all'|'add'|'addDir'|'change'|'unlink'|'unlinkDir'|'raw'|'error'|'ready'} EventName
  41120. * @typedef {'readdir'|'watch'|'add'|'remove'|'change'} ThrottleType
  41121. */
  41122. /**
  41123. *
  41124. * @typedef {Object} WatchHelpers
  41125. * @property {Boolean} followSymlinks
  41126. * @property {'stat'|'lstat'} statMethod
  41127. * @property {Path} path
  41128. * @property {Path} watchPath
  41129. * @property {Function} entryPath
  41130. * @property {Boolean} hasGlob
  41131. * @property {Object} globFilter
  41132. * @property {Function} filterPath
  41133. * @property {Function} filterDir
  41134. */
  41135. const arrify = (value = []) => Array.isArray(value) ? value : [value];
  41136. const flatten = (list, result = []) => {
  41137. list.forEach(item => {
  41138. if (Array.isArray(item)) {
  41139. flatten(item, result);
  41140. } else {
  41141. result.push(item);
  41142. }
  41143. });
  41144. return result;
  41145. };
  41146. const unifyPaths = (paths_) => {
  41147. /**
  41148. * @type {Array<String>}
  41149. */
  41150. const paths = flatten(arrify(paths_));
  41151. if (!paths.every(p => typeof p === STRING_TYPE)) {
  41152. throw new TypeError(`Non-string provided as watch path: ${paths}`);
  41153. }
  41154. return paths.map(normalizePathToUnix);
  41155. };
  41156. // If SLASH_SLASH occurs at the beginning of path, it is not replaced
  41157. // because "//StoragePC/DrivePool/Movies" is a valid network path
  41158. const toUnix = (string) => {
  41159. let str = string.replace(BACK_SLASH_RE, SLASH);
  41160. let prepend = false;
  41161. if (str.startsWith(SLASH_SLASH)) {
  41162. prepend = true;
  41163. }
  41164. while (str.match(DOUBLE_SLASH_RE)) {
  41165. str = str.replace(DOUBLE_SLASH_RE, SLASH);
  41166. }
  41167. if (prepend) {
  41168. str = SLASH + str;
  41169. }
  41170. return str;
  41171. };
  41172. // Our version of upath.normalize
  41173. // TODO: this is not equal to path-normalize module - investigate why
  41174. const normalizePathToUnix = (path) => toUnix(sysPath.normalize(toUnix(path)));
  41175. const normalizeIgnored = (cwd = EMPTY_STR) => (path) => {
  41176. if (typeof path !== STRING_TYPE) return path;
  41177. return normalizePathToUnix(sysPath.isAbsolute(path) ? path : sysPath.join(cwd, path));
  41178. };
  41179. const getAbsolutePath = (path, cwd) => {
  41180. if (sysPath.isAbsolute(path)) {
  41181. return path;
  41182. }
  41183. if (path.startsWith(BANG)) {
  41184. return BANG + sysPath.join(cwd, path.slice(1));
  41185. }
  41186. return sysPath.join(cwd, path);
  41187. };
  41188. const undef = (opts, key) => opts[key] === undefined;
  41189. /**
  41190. * Directory entry.
  41191. * @property {Path} path
  41192. * @property {Set<Path>} items
  41193. */
  41194. class DirEntry {
  41195. /**
  41196. * @param {Path} dir
  41197. * @param {Function} removeWatcher
  41198. */
  41199. constructor(dir, removeWatcher) {
  41200. this.path = dir;
  41201. this._removeWatcher = removeWatcher;
  41202. /** @type {Set<Path>} */
  41203. this.items = new Set();
  41204. }
  41205. add(item) {
  41206. const {items} = this;
  41207. if (!items) return;
  41208. if (item !== ONE_DOT && item !== TWO_DOTS) items.add(item);
  41209. }
  41210. async remove(item) {
  41211. const {items} = this;
  41212. if (!items) return;
  41213. items.delete(item);
  41214. if (items.size > 0) return;
  41215. const dir = this.path;
  41216. try {
  41217. await readdir(dir);
  41218. } catch (err) {
  41219. if (this._removeWatcher) {
  41220. this._removeWatcher(sysPath.dirname(dir), sysPath.basename(dir));
  41221. }
  41222. }
  41223. }
  41224. has(item) {
  41225. const {items} = this;
  41226. if (!items) return;
  41227. return items.has(item);
  41228. }
  41229. /**
  41230. * @returns {Array<String>}
  41231. */
  41232. getChildren() {
  41233. const {items} = this;
  41234. if (!items) return;
  41235. return [...items.values()];
  41236. }
  41237. dispose() {
  41238. this.items.clear();
  41239. delete this.path;
  41240. delete this._removeWatcher;
  41241. delete this.items;
  41242. Object.freeze(this);
  41243. }
  41244. }
  41245. const STAT_METHOD_F = 'stat';
  41246. const STAT_METHOD_L = 'lstat';
  41247. class WatchHelper {
  41248. constructor(path, watchPath, follow, fsw) {
  41249. this.fsw = fsw;
  41250. this.path = path = path.replace(REPLACER_RE, EMPTY_STR);
  41251. this.watchPath = watchPath;
  41252. this.fullWatchPath = sysPath.resolve(watchPath);
  41253. this.hasGlob = watchPath !== path;
  41254. /** @type {object|boolean} */
  41255. if (path === EMPTY_STR) this.hasGlob = false;
  41256. this.globSymlink = this.hasGlob && follow ? undefined : false;
  41257. this.globFilter = this.hasGlob ? anymatch(path, undefined, ANYMATCH_OPTS) : false;
  41258. this.dirParts = this.getDirParts(path);
  41259. this.dirParts.forEach((parts) => {
  41260. if (parts.length > 1) parts.pop();
  41261. });
  41262. this.followSymlinks = follow;
  41263. this.statMethod = follow ? STAT_METHOD_F : STAT_METHOD_L;
  41264. }
  41265. checkGlobSymlink(entry) {
  41266. // only need to resolve once
  41267. // first entry should always have entry.parentDir === EMPTY_STR
  41268. if (this.globSymlink === undefined) {
  41269. this.globSymlink = entry.fullParentDir === this.fullWatchPath ?
  41270. false : {realPath: entry.fullParentDir, linkPath: this.fullWatchPath};
  41271. }
  41272. if (this.globSymlink) {
  41273. return entry.fullPath.replace(this.globSymlink.realPath, this.globSymlink.linkPath);
  41274. }
  41275. return entry.fullPath;
  41276. }
  41277. entryPath(entry) {
  41278. return sysPath.join(this.watchPath,
  41279. sysPath.relative(this.watchPath, this.checkGlobSymlink(entry))
  41280. );
  41281. }
  41282. filterPath(entry) {
  41283. const {stats} = entry;
  41284. if (stats && stats.isSymbolicLink()) return this.filterDir(entry);
  41285. const resolvedPath = this.entryPath(entry);
  41286. const matchesGlob = this.hasGlob && typeof this.globFilter === FUNCTION_TYPE ?
  41287. this.globFilter(resolvedPath) : true;
  41288. return matchesGlob &&
  41289. this.fsw._isntIgnored(resolvedPath, stats) &&
  41290. this.fsw._hasReadPermissions(stats);
  41291. }
  41292. getDirParts(path) {
  41293. if (!this.hasGlob) return [];
  41294. const parts = [];
  41295. const expandedPath = path.includes(BRACE_START) ? braces.expand(path) : [path];
  41296. expandedPath.forEach((path) => {
  41297. parts.push(sysPath.relative(this.watchPath, path).split(SLASH_OR_BACK_SLASH_RE));
  41298. });
  41299. return parts;
  41300. }
  41301. filterDir(entry) {
  41302. if (this.hasGlob) {
  41303. const entryParts = this.getDirParts(this.checkGlobSymlink(entry));
  41304. let globstar = false;
  41305. this.unmatchedGlob = !this.dirParts.some((parts) => {
  41306. return parts.every((part, i) => {
  41307. if (part === GLOBSTAR) globstar = true;
  41308. return globstar || !entryParts[0][i] || anymatch(part, entryParts[0][i], ANYMATCH_OPTS);
  41309. });
  41310. });
  41311. }
  41312. return !this.unmatchedGlob && this.fsw._isntIgnored(this.entryPath(entry), entry.stats);
  41313. }
  41314. }
  41315. /**
  41316. * Watches files & directories for changes. Emitted events:
  41317. * `add`, `addDir`, `change`, `unlink`, `unlinkDir`, `all`, `error`
  41318. *
  41319. * new FSWatcher()
  41320. * .add(directories)
  41321. * .on('add', path => log('File', path, 'was added'))
  41322. */
  41323. class FSWatcher extends EventEmitter$2 {
  41324. // Not indenting methods for history sake; for now.
  41325. constructor(_opts) {
  41326. super();
  41327. const opts = {};
  41328. if (_opts) Object.assign(opts, _opts); // for frozen objects
  41329. /** @type {Map<String, DirEntry>} */
  41330. this._watched = new Map();
  41331. /** @type {Map<String, Array>} */
  41332. this._closers = new Map();
  41333. /** @type {Set<String>} */
  41334. this._ignoredPaths = new Set();
  41335. /** @type {Map<ThrottleType, Map>} */
  41336. this._throttled = new Map();
  41337. /** @type {Map<Path, String|Boolean>} */
  41338. this._symlinkPaths = new Map();
  41339. this._streams = new Set();
  41340. this.closed = false;
  41341. // Set up default options.
  41342. if (undef(opts, 'persistent')) opts.persistent = true;
  41343. if (undef(opts, 'ignoreInitial')) opts.ignoreInitial = false;
  41344. if (undef(opts, 'ignorePermissionErrors')) opts.ignorePermissionErrors = false;
  41345. if (undef(opts, 'interval')) opts.interval = 100;
  41346. if (undef(opts, 'binaryInterval')) opts.binaryInterval = 300;
  41347. if (undef(opts, 'disableGlobbing')) opts.disableGlobbing = false;
  41348. opts.enableBinaryInterval = opts.binaryInterval !== opts.interval;
  41349. // Enable fsevents on OS X when polling isn't explicitly enabled.
  41350. if (undef(opts, 'useFsEvents')) opts.useFsEvents = !opts.usePolling;
  41351. // If we can't use fsevents, ensure the options reflect it's disabled.
  41352. const canUseFsEvents = FsEventsHandler.canUse();
  41353. if (!canUseFsEvents) opts.useFsEvents = false;
  41354. // Use polling on Mac if not using fsevents.
  41355. // Other platforms use non-polling fs_watch.
  41356. if (undef(opts, 'usePolling') && !opts.useFsEvents) {
  41357. opts.usePolling = isMacos;
  41358. }
  41359. // Always default to polling on IBM i because fs.watch() is not available on IBM i.
  41360. if(isIBMi) {
  41361. opts.usePolling = true;
  41362. }
  41363. // Global override (useful for end-developers that need to force polling for all
  41364. // instances of chokidar, regardless of usage/dependency depth)
  41365. const envPoll = process.env.CHOKIDAR_USEPOLLING;
  41366. if (envPoll !== undefined) {
  41367. const envLower = envPoll.toLowerCase();
  41368. if (envLower === 'false' || envLower === '0') {
  41369. opts.usePolling = false;
  41370. } else if (envLower === 'true' || envLower === '1') {
  41371. opts.usePolling = true;
  41372. } else {
  41373. opts.usePolling = !!envLower;
  41374. }
  41375. }
  41376. const envInterval = process.env.CHOKIDAR_INTERVAL;
  41377. if (envInterval) {
  41378. opts.interval = Number.parseInt(envInterval, 10);
  41379. }
  41380. // Editor atomic write normalization enabled by default with fs.watch
  41381. if (undef(opts, 'atomic')) opts.atomic = !opts.usePolling && !opts.useFsEvents;
  41382. if (opts.atomic) this._pendingUnlinks = new Map();
  41383. if (undef(opts, 'followSymlinks')) opts.followSymlinks = true;
  41384. if (undef(opts, 'awaitWriteFinish')) opts.awaitWriteFinish = false;
  41385. if (opts.awaitWriteFinish === true) opts.awaitWriteFinish = {};
  41386. const awf = opts.awaitWriteFinish;
  41387. if (awf) {
  41388. if (!awf.stabilityThreshold) awf.stabilityThreshold = 2000;
  41389. if (!awf.pollInterval) awf.pollInterval = 100;
  41390. this._pendingWrites = new Map();
  41391. }
  41392. if (opts.ignored) opts.ignored = arrify(opts.ignored);
  41393. let readyCalls = 0;
  41394. this._emitReady = () => {
  41395. readyCalls++;
  41396. if (readyCalls >= this._readyCount) {
  41397. this._emitReady = EMPTY_FN;
  41398. this._readyEmitted = true;
  41399. // use process.nextTick to allow time for listener to be bound
  41400. process.nextTick(() => this.emit(EV_READY));
  41401. }
  41402. };
  41403. this._emitRaw = (...args) => this.emit(EV_RAW, ...args);
  41404. this._readyEmitted = false;
  41405. this.options = opts;
  41406. // Initialize with proper watcher.
  41407. if (opts.useFsEvents) {
  41408. this._fsEventsHandler = new FsEventsHandler(this);
  41409. } else {
  41410. this._nodeFsHandler = new NodeFsHandler(this);
  41411. }
  41412. // You’re frozen when your heart’s not open.
  41413. Object.freeze(opts);
  41414. }
  41415. // Public methods
  41416. /**
  41417. * Adds paths to be watched on an existing FSWatcher instance
  41418. * @param {Path|Array<Path>} paths_
  41419. * @param {String=} _origAdd private; for handling non-existent paths to be watched
  41420. * @param {Boolean=} _internal private; indicates a non-user add
  41421. * @returns {FSWatcher} for chaining
  41422. */
  41423. add(paths_, _origAdd, _internal) {
  41424. const {cwd, disableGlobbing} = this.options;
  41425. this.closed = false;
  41426. let paths = unifyPaths(paths_);
  41427. if (cwd) {
  41428. paths = paths.map((path) => {
  41429. const absPath = getAbsolutePath(path, cwd);
  41430. // Check `path` instead of `absPath` because the cwd portion can't be a glob
  41431. if (disableGlobbing || !isGlob(path)) {
  41432. return absPath;
  41433. }
  41434. return normalizePath(absPath);
  41435. });
  41436. }
  41437. // set aside negated glob strings
  41438. paths = paths.filter((path) => {
  41439. if (path.startsWith(BANG)) {
  41440. this._ignoredPaths.add(path.slice(1));
  41441. return false;
  41442. }
  41443. // if a path is being added that was previously ignored, stop ignoring it
  41444. this._ignoredPaths.delete(path);
  41445. this._ignoredPaths.delete(path + SLASH_GLOBSTAR);
  41446. // reset the cached userIgnored anymatch fn
  41447. // to make ignoredPaths changes effective
  41448. this._userIgnored = undefined;
  41449. return true;
  41450. });
  41451. if (this.options.useFsEvents && this._fsEventsHandler) {
  41452. if (!this._readyCount) this._readyCount = paths.length;
  41453. if (this.options.persistent) this._readyCount += paths.length;
  41454. paths.forEach((path) => this._fsEventsHandler._addToFsEvents(path));
  41455. } else {
  41456. if (!this._readyCount) this._readyCount = 0;
  41457. this._readyCount += paths.length;
  41458. Promise.all(
  41459. paths.map(async path => {
  41460. const res = await this._nodeFsHandler._addToNodeFs(path, !_internal, 0, 0, _origAdd);
  41461. if (res) this._emitReady();
  41462. return res;
  41463. })
  41464. ).then(results => {
  41465. if (this.closed) return;
  41466. results.filter(item => item).forEach(item => {
  41467. this.add(sysPath.dirname(item), sysPath.basename(_origAdd || item));
  41468. });
  41469. });
  41470. }
  41471. return this;
  41472. }
  41473. /**
  41474. * Close watchers or start ignoring events from specified paths.
  41475. * @param {Path|Array<Path>} paths_ - string or array of strings, file/directory paths and/or globs
  41476. * @returns {FSWatcher} for chaining
  41477. */
  41478. unwatch(paths_) {
  41479. if (this.closed) return this;
  41480. const paths = unifyPaths(paths_);
  41481. const {cwd} = this.options;
  41482. paths.forEach((path) => {
  41483. // convert to absolute path unless relative path already matches
  41484. if (!sysPath.isAbsolute(path) && !this._closers.has(path)) {
  41485. if (cwd) path = sysPath.join(cwd, path);
  41486. path = sysPath.resolve(path);
  41487. }
  41488. this._closePath(path);
  41489. this._ignoredPaths.add(path);
  41490. if (this._watched.has(path)) {
  41491. this._ignoredPaths.add(path + SLASH_GLOBSTAR);
  41492. }
  41493. // reset the cached userIgnored anymatch fn
  41494. // to make ignoredPaths changes effective
  41495. this._userIgnored = undefined;
  41496. });
  41497. return this;
  41498. }
  41499. /**
  41500. * Close watchers and remove all listeners from watched paths.
  41501. * @returns {Promise<void>}.
  41502. */
  41503. close() {
  41504. if (this.closed) return this._closePromise;
  41505. this.closed = true;
  41506. // Memory management.
  41507. this.removeAllListeners();
  41508. const closers = [];
  41509. this._closers.forEach(closerList => closerList.forEach(closer => {
  41510. const promise = closer();
  41511. if (promise instanceof Promise) closers.push(promise);
  41512. }));
  41513. this._streams.forEach(stream => stream.destroy());
  41514. this._userIgnored = undefined;
  41515. this._readyCount = 0;
  41516. this._readyEmitted = false;
  41517. this._watched.forEach(dirent => dirent.dispose());
  41518. ['closers', 'watched', 'streams', 'symlinkPaths', 'throttled'].forEach(key => {
  41519. this[`_${key}`].clear();
  41520. });
  41521. this._closePromise = closers.length ? Promise.all(closers).then(() => undefined) : Promise.resolve();
  41522. return this._closePromise;
  41523. }
  41524. /**
  41525. * Expose list of watched paths
  41526. * @returns {Object} for chaining
  41527. */
  41528. getWatched() {
  41529. const watchList = {};
  41530. this._watched.forEach((entry, dir) => {
  41531. const key = this.options.cwd ? sysPath.relative(this.options.cwd, dir) : dir;
  41532. watchList[key || ONE_DOT] = entry.getChildren().sort();
  41533. });
  41534. return watchList;
  41535. }
  41536. emitWithAll(event, args) {
  41537. this.emit(...args);
  41538. if (event !== EV_ERROR) this.emit(EV_ALL, ...args);
  41539. }
  41540. // Common helpers
  41541. // --------------
  41542. /**
  41543. * Normalize and emit events.
  41544. * Calling _emit DOES NOT MEAN emit() would be called!
  41545. * @param {EventName} event Type of event
  41546. * @param {Path} path File or directory path
  41547. * @param {*=} val1 arguments to be passed with event
  41548. * @param {*=} val2
  41549. * @param {*=} val3
  41550. * @returns the error if defined, otherwise the value of the FSWatcher instance's `closed` flag
  41551. */
  41552. async _emit(event, path, val1, val2, val3) {
  41553. if (this.closed) return;
  41554. const opts = this.options;
  41555. if (isWindows$1) path = sysPath.normalize(path);
  41556. if (opts.cwd) path = sysPath.relative(opts.cwd, path);
  41557. /** @type Array<any> */
  41558. const args = [event, path];
  41559. if (val3 !== undefined) args.push(val1, val2, val3);
  41560. else if (val2 !== undefined) args.push(val1, val2);
  41561. else if (val1 !== undefined) args.push(val1);
  41562. const awf = opts.awaitWriteFinish;
  41563. let pw;
  41564. if (awf && (pw = this._pendingWrites.get(path))) {
  41565. pw.lastChange = new Date();
  41566. return this;
  41567. }
  41568. if (opts.atomic) {
  41569. if (event === EV_UNLINK) {
  41570. this._pendingUnlinks.set(path, args);
  41571. setTimeout(() => {
  41572. this._pendingUnlinks.forEach((entry, path) => {
  41573. this.emit(...entry);
  41574. this.emit(EV_ALL, ...entry);
  41575. this._pendingUnlinks.delete(path);
  41576. });
  41577. }, typeof opts.atomic === 'number' ? opts.atomic : 100);
  41578. return this;
  41579. }
  41580. if (event === EV_ADD && this._pendingUnlinks.has(path)) {
  41581. event = args[0] = EV_CHANGE;
  41582. this._pendingUnlinks.delete(path);
  41583. }
  41584. }
  41585. if (awf && (event === EV_ADD || event === EV_CHANGE) && this._readyEmitted) {
  41586. const awfEmit = (err, stats) => {
  41587. if (err) {
  41588. event = args[0] = EV_ERROR;
  41589. args[1] = err;
  41590. this.emitWithAll(event, args);
  41591. } else if (stats) {
  41592. // if stats doesn't exist the file must have been deleted
  41593. if (args.length > 2) {
  41594. args[2] = stats;
  41595. } else {
  41596. args.push(stats);
  41597. }
  41598. this.emitWithAll(event, args);
  41599. }
  41600. };
  41601. this._awaitWriteFinish(path, awf.stabilityThreshold, event, awfEmit);
  41602. return this;
  41603. }
  41604. if (event === EV_CHANGE) {
  41605. const isThrottled = !this._throttle(EV_CHANGE, path, 50);
  41606. if (isThrottled) return this;
  41607. }
  41608. if (opts.alwaysStat && val1 === undefined &&
  41609. (event === EV_ADD || event === EV_ADD_DIR || event === EV_CHANGE)
  41610. ) {
  41611. const fullPath = opts.cwd ? sysPath.join(opts.cwd, path) : path;
  41612. let stats;
  41613. try {
  41614. stats = await stat(fullPath);
  41615. } catch (err) {}
  41616. // Suppress event when fs_stat fails, to avoid sending undefined 'stat'
  41617. if (!stats || this.closed) return;
  41618. args.push(stats);
  41619. }
  41620. this.emitWithAll(event, args);
  41621. return this;
  41622. }
  41623. /**
  41624. * Common handler for errors
  41625. * @param {Error} error
  41626. * @returns {Error|Boolean} The error if defined, otherwise the value of the FSWatcher instance's `closed` flag
  41627. */
  41628. _handleError(error) {
  41629. const code = error && error.code;
  41630. if (error && code !== 'ENOENT' && code !== 'ENOTDIR' &&
  41631. (!this.options.ignorePermissionErrors || (code !== 'EPERM' && code !== 'EACCES'))
  41632. ) {
  41633. this.emit(EV_ERROR, error);
  41634. }
  41635. return error || this.closed;
  41636. }
  41637. /**
  41638. * Helper utility for throttling
  41639. * @param {ThrottleType} actionType type being throttled
  41640. * @param {Path} path being acted upon
  41641. * @param {Number} timeout duration of time to suppress duplicate actions
  41642. * @returns {Object|false} tracking object or false if action should be suppressed
  41643. */
  41644. _throttle(actionType, path, timeout) {
  41645. if (!this._throttled.has(actionType)) {
  41646. this._throttled.set(actionType, new Map());
  41647. }
  41648. /** @type {Map<Path, Object>} */
  41649. const action = this._throttled.get(actionType);
  41650. /** @type {Object} */
  41651. const actionPath = action.get(path);
  41652. if (actionPath) {
  41653. actionPath.count++;
  41654. return false;
  41655. }
  41656. let timeoutObject;
  41657. const clear = () => {
  41658. const item = action.get(path);
  41659. const count = item ? item.count : 0;
  41660. action.delete(path);
  41661. clearTimeout(timeoutObject);
  41662. if (item) clearTimeout(item.timeoutObject);
  41663. return count;
  41664. };
  41665. timeoutObject = setTimeout(clear, timeout);
  41666. const thr = {timeoutObject, clear, count: 0};
  41667. action.set(path, thr);
  41668. return thr;
  41669. }
  41670. _incrReadyCount() {
  41671. return this._readyCount++;
  41672. }
  41673. /**
  41674. * Awaits write operation to finish.
  41675. * Polls a newly created file for size variations. When files size does not change for 'threshold' milliseconds calls callback.
  41676. * @param {Path} path being acted upon
  41677. * @param {Number} threshold Time in milliseconds a file size must be fixed before acknowledging write OP is finished
  41678. * @param {EventName} event
  41679. * @param {Function} awfEmit Callback to be called when ready for event to be emitted.
  41680. */
  41681. _awaitWriteFinish(path, threshold, event, awfEmit) {
  41682. let timeoutHandler;
  41683. let fullPath = path;
  41684. if (this.options.cwd && !sysPath.isAbsolute(path)) {
  41685. fullPath = sysPath.join(this.options.cwd, path);
  41686. }
  41687. const now = new Date();
  41688. const awaitWriteFinish = (prevStat) => {
  41689. fs$5.stat(fullPath, (err, curStat) => {
  41690. if (err || !this._pendingWrites.has(path)) {
  41691. if (err && err.code !== 'ENOENT') awfEmit(err);
  41692. return;
  41693. }
  41694. const now = Number(new Date());
  41695. if (prevStat && curStat.size !== prevStat.size) {
  41696. this._pendingWrites.get(path).lastChange = now;
  41697. }
  41698. const pw = this._pendingWrites.get(path);
  41699. const df = now - pw.lastChange;
  41700. if (df >= threshold) {
  41701. this._pendingWrites.delete(path);
  41702. awfEmit(undefined, curStat);
  41703. } else {
  41704. timeoutHandler = setTimeout(
  41705. awaitWriteFinish,
  41706. this.options.awaitWriteFinish.pollInterval,
  41707. curStat
  41708. );
  41709. }
  41710. });
  41711. };
  41712. if (!this._pendingWrites.has(path)) {
  41713. this._pendingWrites.set(path, {
  41714. lastChange: now,
  41715. cancelWait: () => {
  41716. this._pendingWrites.delete(path);
  41717. clearTimeout(timeoutHandler);
  41718. return event;
  41719. }
  41720. });
  41721. timeoutHandler = setTimeout(
  41722. awaitWriteFinish,
  41723. this.options.awaitWriteFinish.pollInterval
  41724. );
  41725. }
  41726. }
  41727. _getGlobIgnored() {
  41728. return [...this._ignoredPaths.values()];
  41729. }
  41730. /**
  41731. * Determines whether user has asked to ignore this path.
  41732. * @param {Path} path filepath or dir
  41733. * @param {fs.Stats=} stats result of fs.stat
  41734. * @returns {Boolean}
  41735. */
  41736. _isIgnored(path, stats) {
  41737. if (this.options.atomic && DOT_RE.test(path)) return true;
  41738. if (!this._userIgnored) {
  41739. const {cwd} = this.options;
  41740. const ign = this.options.ignored;
  41741. const ignored = ign && ign.map(normalizeIgnored(cwd));
  41742. const paths = arrify(ignored)
  41743. .filter((path) => typeof path === STRING_TYPE && !isGlob(path))
  41744. .map((path) => path + SLASH_GLOBSTAR);
  41745. const list = this._getGlobIgnored().map(normalizeIgnored(cwd)).concat(ignored, paths);
  41746. this._userIgnored = anymatch(list, undefined, ANYMATCH_OPTS);
  41747. }
  41748. return this._userIgnored([path, stats]);
  41749. }
  41750. _isntIgnored(path, stat) {
  41751. return !this._isIgnored(path, stat);
  41752. }
  41753. /**
  41754. * Provides a set of common helpers and properties relating to symlink and glob handling.
  41755. * @param {Path} path file, directory, or glob pattern being watched
  41756. * @param {Number=} depth at any depth > 0, this isn't a glob
  41757. * @returns {WatchHelper} object containing helpers for this path
  41758. */
  41759. _getWatchHelpers(path, depth) {
  41760. const watchPath = depth || this.options.disableGlobbing || !isGlob(path) ? path : globParent(path);
  41761. const follow = this.options.followSymlinks;
  41762. return new WatchHelper(path, watchPath, follow, this);
  41763. }
  41764. // Directory helpers
  41765. // -----------------
  41766. /**
  41767. * Provides directory tracking objects
  41768. * @param {String} directory path of the directory
  41769. * @returns {DirEntry} the directory's tracking object
  41770. */
  41771. _getWatchedDir(directory) {
  41772. if (!this._boundRemove) this._boundRemove = this._remove.bind(this);
  41773. const dir = sysPath.resolve(directory);
  41774. if (!this._watched.has(dir)) this._watched.set(dir, new DirEntry(dir, this._boundRemove));
  41775. return this._watched.get(dir);
  41776. }
  41777. // File helpers
  41778. // ------------
  41779. /**
  41780. * Check for read permissions.
  41781. * Based on this answer on SO: https://stackoverflow.com/a/11781404/1358405
  41782. * @param {fs.Stats} stats - object, result of fs_stat
  41783. * @returns {Boolean} indicates whether the file can be read
  41784. */
  41785. _hasReadPermissions(stats) {
  41786. if (this.options.ignorePermissionErrors) return true;
  41787. // stats.mode may be bigint
  41788. const md = stats && Number.parseInt(stats.mode, 10);
  41789. const st = md & 0o777;
  41790. const it = Number.parseInt(st.toString(8)[0], 10);
  41791. return Boolean(4 & it);
  41792. }
  41793. /**
  41794. * Handles emitting unlink events for
  41795. * files and directories, and via recursion, for
  41796. * files and directories within directories that are unlinked
  41797. * @param {String} directory within which the following item is located
  41798. * @param {String} item base path of item/directory
  41799. * @returns {void}
  41800. */
  41801. _remove(directory, item, isDirectory) {
  41802. // if what is being deleted is a directory, get that directory's paths
  41803. // for recursive deleting and cleaning of watched object
  41804. // if it is not a directory, nestedDirectoryChildren will be empty array
  41805. const path = sysPath.join(directory, item);
  41806. const fullPath = sysPath.resolve(path);
  41807. isDirectory = isDirectory != null
  41808. ? isDirectory
  41809. : this._watched.has(path) || this._watched.has(fullPath);
  41810. // prevent duplicate handling in case of arriving here nearly simultaneously
  41811. // via multiple paths (such as _handleFile and _handleDir)
  41812. if (!this._throttle('remove', path, 100)) return;
  41813. // if the only watched file is removed, watch for its return
  41814. if (!isDirectory && !this.options.useFsEvents && this._watched.size === 1) {
  41815. this.add(directory, item, true);
  41816. }
  41817. // This will create a new entry in the watched object in either case
  41818. // so we got to do the directory check beforehand
  41819. const wp = this._getWatchedDir(path);
  41820. const nestedDirectoryChildren = wp.getChildren();
  41821. // Recursively remove children directories / files.
  41822. nestedDirectoryChildren.forEach(nested => this._remove(path, nested));
  41823. // Check if item was on the watched list and remove it
  41824. const parent = this._getWatchedDir(directory);
  41825. const wasTracked = parent.has(item);
  41826. parent.remove(item);
  41827. // Fixes issue #1042 -> Relative paths were detected and added as symlinks
  41828. // (https://github.com/paulmillr/chokidar/blob/e1753ddbc9571bdc33b4a4af172d52cb6e611c10/lib/nodefs-handler.js#L612),
  41829. // but never removed from the map in case the path was deleted.
  41830. // This leads to an incorrect state if the path was recreated:
  41831. // https://github.com/paulmillr/chokidar/blob/e1753ddbc9571bdc33b4a4af172d52cb6e611c10/lib/nodefs-handler.js#L553
  41832. if (this._symlinkPaths.has(fullPath)) {
  41833. this._symlinkPaths.delete(fullPath);
  41834. }
  41835. // If we wait for this file to be fully written, cancel the wait.
  41836. let relPath = path;
  41837. if (this.options.cwd) relPath = sysPath.relative(this.options.cwd, path);
  41838. if (this.options.awaitWriteFinish && this._pendingWrites.has(relPath)) {
  41839. const event = this._pendingWrites.get(relPath).cancelWait();
  41840. if (event === EV_ADD) return;
  41841. }
  41842. // The Entry will either be a directory that just got removed
  41843. // or a bogus entry to a file, in either case we have to remove it
  41844. this._watched.delete(path);
  41845. this._watched.delete(fullPath);
  41846. const eventName = isDirectory ? EV_UNLINK_DIR : EV_UNLINK;
  41847. if (wasTracked && !this._isIgnored(path)) this._emit(eventName, path);
  41848. // Avoid conflicts if we later create another file with the same name
  41849. if (!this.options.useFsEvents) {
  41850. this._closePath(path);
  41851. }
  41852. }
  41853. /**
  41854. * Closes all watchers for a path
  41855. * @param {Path} path
  41856. */
  41857. _closePath(path) {
  41858. this._closeFile(path);
  41859. const dir = sysPath.dirname(path);
  41860. this._getWatchedDir(dir).remove(sysPath.basename(path));
  41861. }
  41862. /**
  41863. * Closes only file-specific watchers
  41864. * @param {Path} path
  41865. */
  41866. _closeFile(path) {
  41867. const closers = this._closers.get(path);
  41868. if (!closers) return;
  41869. closers.forEach(closer => closer());
  41870. this._closers.delete(path);
  41871. }
  41872. /**
  41873. *
  41874. * @param {Path} path
  41875. * @param {Function} closer
  41876. */
  41877. _addPathCloser(path, closer) {
  41878. if (!closer) return;
  41879. let list = this._closers.get(path);
  41880. if (!list) {
  41881. list = [];
  41882. this._closers.set(path, list);
  41883. }
  41884. list.push(closer);
  41885. }
  41886. _readdirp(root, opts) {
  41887. if (this.closed) return;
  41888. const options = {type: EV_ALL, alwaysStat: true, lstat: true, ...opts};
  41889. let stream = readdirp(root, options);
  41890. this._streams.add(stream);
  41891. stream.once(STR_CLOSE, () => {
  41892. stream = undefined;
  41893. });
  41894. stream.once(STR_END, () => {
  41895. if (stream) {
  41896. this._streams.delete(stream);
  41897. stream = undefined;
  41898. }
  41899. });
  41900. return stream;
  41901. }
  41902. }
  41903. // Export FSWatcher class
  41904. chokidar.FSWatcher = FSWatcher;
  41905. /**
  41906. * Instantiates watcher with paths to be tracked.
  41907. * @param {String|Array<String>} paths file/directory paths and/or globs
  41908. * @param {Object=} options chokidar opts
  41909. * @returns an instance of FSWatcher for chaining.
  41910. */
  41911. const watch = (paths, options) => {
  41912. const watcher = new FSWatcher(options);
  41913. watcher.add(paths);
  41914. return watcher;
  41915. };
  41916. chokidar.watch = watch;
  41917. var shellQuote$1 = {};
  41918. var quote = function quote(xs) {
  41919. return xs.map(function (s) {
  41920. if (s && typeof s === 'object') {
  41921. return s.op.replace(/(.)/g, '\\$1');
  41922. }
  41923. if ((/["\s]/).test(s) && !(/'/).test(s)) {
  41924. return "'" + s.replace(/(['\\])/g, '\\$1') + "'";
  41925. }
  41926. if ((/["'\s]/).test(s)) {
  41927. return '"' + s.replace(/(["\\$`!])/g, '\\$1') + '"';
  41928. }
  41929. return String(s).replace(/([A-Za-z]:)?([#!"$&'()*,:;<=>?@[\\\]^`{|}])/g, '$1\\$2');
  41930. }).join(' ');
  41931. };
  41932. // '<(' is process substitution operator and
  41933. // can be parsed the same as control operator
  41934. var CONTROL = '(?:' + [
  41935. '\\|\\|',
  41936. '\\&\\&',
  41937. ';;',
  41938. '\\|\\&',
  41939. '\\<\\(',
  41940. '\\<\\<\\<',
  41941. '>>',
  41942. '>\\&',
  41943. '<\\&',
  41944. '[&;()|<>]'
  41945. ].join('|') + ')';
  41946. var controlRE = new RegExp('^' + CONTROL + '$');
  41947. var META = '|&;()<> \\t';
  41948. var SINGLE_QUOTE = '"((\\\\"|[^"])*?)"';
  41949. var DOUBLE_QUOTE = '\'((\\\\\'|[^\'])*?)\'';
  41950. var hash = /^#$/;
  41951. var SQ = "'";
  41952. var DQ = '"';
  41953. var DS = '$';
  41954. var TOKEN = '';
  41955. var mult = 0x100000000; // Math.pow(16, 8);
  41956. for (var i = 0; i < 4; i++) {
  41957. TOKEN += (mult * Math.random()).toString(16);
  41958. }
  41959. var startsWithToken = new RegExp('^' + TOKEN);
  41960. function matchAll(s, r) {
  41961. var origIndex = r.lastIndex;
  41962. var matches = [];
  41963. var matchObj;
  41964. while ((matchObj = r.exec(s))) {
  41965. matches.push(matchObj);
  41966. if (r.lastIndex === matchObj.index) {
  41967. r.lastIndex += 1;
  41968. }
  41969. }
  41970. r.lastIndex = origIndex;
  41971. return matches;
  41972. }
  41973. function getVar(env, pre, key) {
  41974. var r = typeof env === 'function' ? env(key) : env[key];
  41975. if (typeof r === 'undefined' && key != '') {
  41976. r = '';
  41977. } else if (typeof r === 'undefined') {
  41978. r = '$';
  41979. }
  41980. if (typeof r === 'object') {
  41981. return pre + TOKEN + JSON.stringify(r) + TOKEN;
  41982. }
  41983. return pre + r;
  41984. }
  41985. function parseInternal(string, env, opts) {
  41986. if (!opts) {
  41987. opts = {};
  41988. }
  41989. var BS = opts.escape || '\\';
  41990. var BAREWORD = '(\\' + BS + '[\'"' + META + ']|[^\\s\'"' + META + '])+';
  41991. var chunker = new RegExp([
  41992. '(' + CONTROL + ')', // control chars
  41993. '(' + BAREWORD + '|' + SINGLE_QUOTE + '|' + DOUBLE_QUOTE + ')+'
  41994. ].join('|'), 'g');
  41995. var matches = matchAll(string, chunker);
  41996. if (matches.length === 0) {
  41997. return [];
  41998. }
  41999. if (!env) {
  42000. env = {};
  42001. }
  42002. var commented = false;
  42003. return matches.map(function (match) {
  42004. var s = match[0];
  42005. if (!s || commented) {
  42006. return void undefined;
  42007. }
  42008. if (controlRE.test(s)) {
  42009. return { op: s };
  42010. }
  42011. // Hand-written scanner/parser for Bash quoting rules:
  42012. //
  42013. // 1. inside single quotes, all characters are printed literally.
  42014. // 2. inside double quotes, all characters are printed literally
  42015. // except variables prefixed by '$' and backslashes followed by
  42016. // either a double quote or another backslash.
  42017. // 3. outside of any quotes, backslashes are treated as escape
  42018. // characters and not printed (unless they are themselves escaped)
  42019. // 4. quote context can switch mid-token if there is no whitespace
  42020. // between the two quote contexts (e.g. all'one'"token" parses as
  42021. // "allonetoken")
  42022. var quote = false;
  42023. var esc = false;
  42024. var out = '';
  42025. var isGlob = false;
  42026. var i;
  42027. function parseEnvVar() {
  42028. i += 1;
  42029. var varend;
  42030. var varname;
  42031. var char = s.charAt(i);
  42032. if (char === '{') {
  42033. i += 1;
  42034. if (s.charAt(i) === '}') {
  42035. throw new Error('Bad substitution: ' + s.slice(i - 2, i + 1));
  42036. }
  42037. varend = s.indexOf('}', i);
  42038. if (varend < 0) {
  42039. throw new Error('Bad substitution: ' + s.slice(i));
  42040. }
  42041. varname = s.slice(i, varend);
  42042. i = varend;
  42043. } else if ((/[*@#?$!_-]/).test(char)) {
  42044. varname = char;
  42045. i += 1;
  42046. } else {
  42047. var slicedFromI = s.slice(i);
  42048. varend = slicedFromI.match(/[^\w\d_]/);
  42049. if (!varend) {
  42050. varname = slicedFromI;
  42051. i = s.length;
  42052. } else {
  42053. varname = slicedFromI.slice(0, varend.index);
  42054. i += varend.index - 1;
  42055. }
  42056. }
  42057. return getVar(env, '', varname);
  42058. }
  42059. for (i = 0; i < s.length; i++) {
  42060. var c = s.charAt(i);
  42061. isGlob = isGlob || (!quote && (c === '*' || c === '?'));
  42062. if (esc) {
  42063. out += c;
  42064. esc = false;
  42065. } else if (quote) {
  42066. if (c === quote) {
  42067. quote = false;
  42068. } else if (quote == SQ) {
  42069. out += c;
  42070. } else { // Double quote
  42071. if (c === BS) {
  42072. i += 1;
  42073. c = s.charAt(i);
  42074. if (c === DQ || c === BS || c === DS) {
  42075. out += c;
  42076. } else {
  42077. out += BS + c;
  42078. }
  42079. } else if (c === DS) {
  42080. out += parseEnvVar();
  42081. } else {
  42082. out += c;
  42083. }
  42084. }
  42085. } else if (c === DQ || c === SQ) {
  42086. quote = c;
  42087. } else if (controlRE.test(c)) {
  42088. return { op: s };
  42089. } else if (hash.test(c)) {
  42090. commented = true;
  42091. var commentObj = { comment: string.slice(match.index + i + 1) };
  42092. if (out.length) {
  42093. return [out, commentObj];
  42094. }
  42095. return [commentObj];
  42096. } else if (c === BS) {
  42097. esc = true;
  42098. } else if (c === DS) {
  42099. out += parseEnvVar();
  42100. } else {
  42101. out += c;
  42102. }
  42103. }
  42104. if (isGlob) {
  42105. return { op: 'glob', pattern: out };
  42106. }
  42107. return out;
  42108. }).reduce(function (prev, arg) { // finalize parsed arguments
  42109. // TODO: replace this whole reduce with a concat
  42110. return typeof arg === 'undefined' ? prev : prev.concat(arg);
  42111. }, []);
  42112. }
  42113. var parse$6 = function parse(s, env, opts) {
  42114. var mapped = parseInternal(s, env, opts);
  42115. if (typeof env !== 'function') {
  42116. return mapped;
  42117. }
  42118. return mapped.reduce(function (acc, s) {
  42119. if (typeof s === 'object') {
  42120. return acc.concat(s);
  42121. }
  42122. var xs = s.split(RegExp('(' + TOKEN + '.*?' + TOKEN + ')', 'g'));
  42123. if (xs.length === 1) {
  42124. return acc.concat(xs[0]);
  42125. }
  42126. return acc.concat(xs.filter(Boolean).map(function (x) {
  42127. if (startsWithToken.test(x)) {
  42128. return JSON.parse(x.split(TOKEN)[1]);
  42129. }
  42130. return x;
  42131. }));
  42132. }, []);
  42133. };
  42134. shellQuote$1.quote = quote;
  42135. shellQuote$1.parse = parse$6;
  42136. var macos = {
  42137. '/Applications/Atom.app/Contents/MacOS/Atom': 'atom',
  42138. '/Applications/Atom Beta.app/Contents/MacOS/Atom Beta':
  42139. '/Applications/Atom Beta.app/Contents/MacOS/Atom Beta',
  42140. '/Applications/Brackets.app/Contents/MacOS/Brackets': 'brackets',
  42141. '/Applications/Sublime Text.app/Contents/MacOS/Sublime Text':
  42142. '/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl',
  42143. '/Applications/Sublime Text.app/Contents/MacOS/sublime_text':
  42144. '/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl',
  42145. '/Applications/Sublime Text 2.app/Contents/MacOS/Sublime Text 2':
  42146. '/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl',
  42147. '/Applications/Sublime Text Dev.app/Contents/MacOS/Sublime Text':
  42148. '/Applications/Sublime Text Dev.app/Contents/SharedSupport/bin/subl',
  42149. '/Applications/Visual Studio Code.app/Contents/MacOS/Electron': 'code',
  42150. '/Applications/Visual Studio Code - Insiders.app/Contents/MacOS/Electron':
  42151. 'code-insiders',
  42152. '/Applications/VSCodium.app/Contents/MacOS/Electron': 'codium',
  42153. '/Applications/Cursor.app/Contents/MacOS/Cursor': 'cursor',
  42154. '/Applications/AppCode.app/Contents/MacOS/appcode':
  42155. '/Applications/AppCode.app/Contents/MacOS/appcode',
  42156. '/Applications/CLion.app/Contents/MacOS/clion':
  42157. '/Applications/CLion.app/Contents/MacOS/clion',
  42158. '/Applications/IntelliJ IDEA.app/Contents/MacOS/idea':
  42159. '/Applications/IntelliJ IDEA.app/Contents/MacOS/idea',
  42160. '/Applications/IntelliJ IDEA Ultimate.app/Contents/MacOS/idea':
  42161. '/Applications/IntelliJ IDEA Ultimate.app/Contents/MacOS/idea',
  42162. '/Applications/IntelliJ IDEA Community Edition.app/Contents/MacOS/idea':
  42163. '/Applications/IntelliJ IDEA Community Edition.app/Contents/MacOS/idea',
  42164. '/Applications/PhpStorm.app/Contents/MacOS/phpstorm':
  42165. '/Applications/PhpStorm.app/Contents/MacOS/phpstorm',
  42166. '/Applications/PyCharm.app/Contents/MacOS/pycharm':
  42167. '/Applications/PyCharm.app/Contents/MacOS/pycharm',
  42168. '/Applications/PyCharm CE.app/Contents/MacOS/pycharm':
  42169. '/Applications/PyCharm CE.app/Contents/MacOS/pycharm',
  42170. '/Applications/RubyMine.app/Contents/MacOS/rubymine':
  42171. '/Applications/RubyMine.app/Contents/MacOS/rubymine',
  42172. '/Applications/WebStorm.app/Contents/MacOS/webstorm':
  42173. '/Applications/WebStorm.app/Contents/MacOS/webstorm',
  42174. '/Applications/MacVim.app/Contents/MacOS/MacVim': 'mvim',
  42175. '/Applications/GoLand.app/Contents/MacOS/goland':
  42176. '/Applications/GoLand.app/Contents/MacOS/goland',
  42177. '/Applications/Rider.app/Contents/MacOS/rider':
  42178. '/Applications/Rider.app/Contents/MacOS/rider',
  42179. '/Applications/Zed.app/Contents/MacOS/zed': 'zed'
  42180. };
  42181. var linux = {
  42182. atom: 'atom',
  42183. Brackets: 'brackets',
  42184. 'code-insiders': 'code-insiders',
  42185. code: 'code',
  42186. vscodium: 'vscodium',
  42187. codium: 'codium',
  42188. emacs: 'emacs',
  42189. gvim: 'gvim',
  42190. idea: 'idea',
  42191. 'idea.sh': 'idea',
  42192. phpstorm: 'phpstorm',
  42193. 'phpstorm.sh': 'phpstorm',
  42194. pycharm: 'pycharm',
  42195. 'pycharm.sh': 'pycharm',
  42196. rubymine: 'rubymine',
  42197. 'rubymine.sh': 'rubymine',
  42198. sublime_text: 'subl',
  42199. vim: 'vim',
  42200. webstorm: 'webstorm',
  42201. 'webstorm.sh': 'webstorm',
  42202. goland: 'goland',
  42203. 'goland.sh': 'goland',
  42204. rider: 'rider',
  42205. 'rider.sh': 'rider'
  42206. };
  42207. var windows$1 = [
  42208. 'Brackets.exe',
  42209. 'Code.exe',
  42210. 'Code - Insiders.exe',
  42211. 'VSCodium.exe',
  42212. 'atom.exe',
  42213. 'sublime_text.exe',
  42214. 'notepad++.exe',
  42215. 'clion.exe',
  42216. 'clion64.exe',
  42217. 'idea.exe',
  42218. 'idea64.exe',
  42219. 'phpstorm.exe',
  42220. 'phpstorm64.exe',
  42221. 'pycharm.exe',
  42222. 'pycharm64.exe',
  42223. 'rubymine.exe',
  42224. 'rubymine64.exe',
  42225. 'webstorm.exe',
  42226. 'webstorm64.exe',
  42227. 'goland.exe',
  42228. 'goland64.exe',
  42229. 'rider.exe',
  42230. 'rider64.exe'
  42231. ];
  42232. const path$7 = require$$0$4;
  42233. const shellQuote = shellQuote$1;
  42234. const childProcess$2 = require$$2$1;
  42235. // Map from full process name to binary that starts the process
  42236. // We can't just re-use full process name, because it will spawn a new instance
  42237. // of the app every time
  42238. const COMMON_EDITORS_MACOS = macos;
  42239. const COMMON_EDITORS_LINUX = linux;
  42240. const COMMON_EDITORS_WIN = windows$1;
  42241. var guess = function guessEditor (specifiedEditor) {
  42242. if (specifiedEditor) {
  42243. return shellQuote.parse(specifiedEditor)
  42244. }
  42245. if (process.env.LAUNCH_EDITOR) {
  42246. return [process.env.LAUNCH_EDITOR]
  42247. }
  42248. if (process.versions.webcontainer) {
  42249. return [process.env.EDITOR || 'code']
  42250. }
  42251. // We can find out which editor is currently running by:
  42252. // `ps x` on macOS and Linux
  42253. // `Get-Process` on Windows
  42254. try {
  42255. if (process.platform === 'darwin') {
  42256. const output = childProcess$2
  42257. .execSync('ps x -o comm=', {
  42258. stdio: ['pipe', 'pipe', 'ignore']
  42259. })
  42260. .toString();
  42261. const processNames = Object.keys(COMMON_EDITORS_MACOS);
  42262. const processList = output.split('\n');
  42263. for (let i = 0; i < processNames.length; i++) {
  42264. const processName = processNames[i];
  42265. // Find editor by exact match.
  42266. if (processList.includes(processName)) {
  42267. return [COMMON_EDITORS_MACOS[processName]]
  42268. }
  42269. const processNameWithoutApplications = processName.replace('/Applications', '');
  42270. // Find editor installation not in /Applications.
  42271. if (output.indexOf(processNameWithoutApplications) !== -1) {
  42272. // Use the CLI command if one is specified
  42273. if (processName !== COMMON_EDITORS_MACOS[processName]) {
  42274. return [COMMON_EDITORS_MACOS[processName]]
  42275. }
  42276. // Use a partial match to find the running process path. If one is found, use the
  42277. // existing path since it can be running from anywhere.
  42278. const runningProcess = processList.find((procName) => procName.endsWith(processNameWithoutApplications));
  42279. if (runningProcess !== undefined) {
  42280. return [runningProcess]
  42281. }
  42282. }
  42283. }
  42284. } else if (process.platform === 'win32') {
  42285. const output = childProcess$2
  42286. .execSync(
  42287. 'powershell -NoProfile -Command "Get-CimInstance -Query \\"select executablepath from win32_process where executablepath is not null\\" | % { $_.ExecutablePath }"',
  42288. {
  42289. stdio: ['pipe', 'pipe', 'ignore']
  42290. }
  42291. )
  42292. .toString();
  42293. const runningProcesses = output.split('\r\n');
  42294. for (let i = 0; i < runningProcesses.length; i++) {
  42295. const fullProcessPath = runningProcesses[i].trim();
  42296. const shortProcessName = path$7.basename(fullProcessPath);
  42297. if (COMMON_EDITORS_WIN.indexOf(shortProcessName) !== -1) {
  42298. return [fullProcessPath]
  42299. }
  42300. }
  42301. } else if (process.platform === 'linux') {
  42302. // --no-heading No header line
  42303. // x List all processes owned by you
  42304. // -o comm Need only names column
  42305. const output = childProcess$2
  42306. .execSync('ps x --no-heading -o comm --sort=comm', {
  42307. stdio: ['pipe', 'pipe', 'ignore']
  42308. })
  42309. .toString();
  42310. const processNames = Object.keys(COMMON_EDITORS_LINUX);
  42311. for (let i = 0; i < processNames.length; i++) {
  42312. const processName = processNames[i];
  42313. if (output.indexOf(processName) !== -1) {
  42314. return [COMMON_EDITORS_LINUX[processName]]
  42315. }
  42316. }
  42317. }
  42318. } catch (ignoreError) {
  42319. // Ignore...
  42320. }
  42321. // Last resort, use old skool env vars
  42322. if (process.env.VISUAL) {
  42323. return [process.env.VISUAL]
  42324. } else if (process.env.EDITOR) {
  42325. return [process.env.EDITOR]
  42326. }
  42327. return [null]
  42328. };
  42329. const path$6 = require$$0$4;
  42330. // normalize file/line numbers into command line args for specific editors
  42331. var getArgs = function getArgumentsForPosition (
  42332. editor,
  42333. fileName,
  42334. lineNumber,
  42335. columnNumber = 1
  42336. ) {
  42337. const editorBasename = path$6.basename(editor).replace(/\.(exe|cmd|bat)$/i, '');
  42338. switch (editorBasename) {
  42339. case 'atom':
  42340. case 'Atom':
  42341. case 'Atom Beta':
  42342. case 'subl':
  42343. case 'sublime':
  42344. case 'sublime_text':
  42345. case 'wstorm':
  42346. case 'charm':
  42347. case 'zed':
  42348. return [`${fileName}:${lineNumber}:${columnNumber}`]
  42349. case 'notepad++':
  42350. return ['-n' + lineNumber, '-c' + columnNumber, fileName]
  42351. case 'vim':
  42352. case 'mvim':
  42353. return [`+call cursor(${lineNumber}, ${columnNumber})`, fileName]
  42354. case 'joe':
  42355. case 'gvim':
  42356. return ['+' + `${lineNumber}`, fileName]
  42357. case 'emacs':
  42358. case 'emacsclient':
  42359. return [`+${lineNumber}:${columnNumber}`, fileName]
  42360. case 'rmate':
  42361. case 'mate':
  42362. case 'mine':
  42363. return ['--line', lineNumber, fileName]
  42364. case 'code':
  42365. case 'Code':
  42366. case 'code-insiders':
  42367. case 'Code - Insiders':
  42368. case 'codium':
  42369. case 'cursor':
  42370. case 'vscodium':
  42371. case 'VSCodium':
  42372. return ['-r', '-g', `${fileName}:${lineNumber}:${columnNumber}`]
  42373. case 'appcode':
  42374. case 'clion':
  42375. case 'clion64':
  42376. case 'idea':
  42377. case 'idea64':
  42378. case 'phpstorm':
  42379. case 'phpstorm64':
  42380. case 'pycharm':
  42381. case 'pycharm64':
  42382. case 'rubymine':
  42383. case 'rubymine64':
  42384. case 'webstorm':
  42385. case 'webstorm64':
  42386. case 'goland':
  42387. case 'goland64':
  42388. case 'rider':
  42389. case 'rider64':
  42390. return ['--line', lineNumber, '--column', columnNumber, fileName]
  42391. }
  42392. if (process.env.LAUNCH_EDITOR) {
  42393. return [fileName, lineNumber, columnNumber]
  42394. }
  42395. // For all others, drop the lineNumber until we have
  42396. // a mapping above, since providing the lineNumber incorrectly
  42397. // can result in errors or confusing behavior.
  42398. return [fileName]
  42399. };
  42400. /**
  42401. * Copyright (c) 2015-present, Facebook, Inc.
  42402. *
  42403. * This source code is licensed under the MIT license found in the
  42404. * LICENSE file at
  42405. * https://github.com/facebookincubator/create-react-app/blob/master/LICENSE
  42406. *
  42407. * Modified by Yuxi Evan You
  42408. */
  42409. const fs$4 = require$$0__default;
  42410. const os$1 = require$$2;
  42411. const path$5 = require$$0$4;
  42412. const colors = picocolorsExports;
  42413. const childProcess$1 = require$$2$1;
  42414. const guessEditor = guess;
  42415. const getArgumentsForPosition = getArgs;
  42416. function wrapErrorCallback (cb) {
  42417. return (fileName, errorMessage) => {
  42418. console.log();
  42419. console.log(
  42420. colors.red('Could not open ' + path$5.basename(fileName) + ' in the editor.')
  42421. );
  42422. if (errorMessage) {
  42423. if (errorMessage[errorMessage.length - 1] !== '.') {
  42424. errorMessage += '.';
  42425. }
  42426. console.log(
  42427. colors.red('The editor process exited with an error: ' + errorMessage)
  42428. );
  42429. }
  42430. console.log();
  42431. if (cb) cb(fileName, errorMessage);
  42432. }
  42433. }
  42434. function isTerminalEditor (editor) {
  42435. switch (editor) {
  42436. case 'vim':
  42437. case 'emacs':
  42438. case 'nano':
  42439. return true
  42440. }
  42441. return false
  42442. }
  42443. const positionRE = /:(\d+)(:(\d+))?$/;
  42444. function parseFile (file) {
  42445. const fileName = file.replace(positionRE, '');
  42446. const match = file.match(positionRE);
  42447. const lineNumber = match && match[1];
  42448. const columnNumber = match && match[3];
  42449. return {
  42450. fileName,
  42451. lineNumber,
  42452. columnNumber
  42453. }
  42454. }
  42455. let _childProcess = null;
  42456. function launchEditor (file, specifiedEditor, onErrorCallback) {
  42457. const parsed = parseFile(file);
  42458. let { fileName } = parsed;
  42459. const { lineNumber, columnNumber } = parsed;
  42460. if (!fs$4.existsSync(fileName)) {
  42461. return
  42462. }
  42463. if (typeof specifiedEditor === 'function') {
  42464. onErrorCallback = specifiedEditor;
  42465. specifiedEditor = undefined;
  42466. }
  42467. onErrorCallback = wrapErrorCallback(onErrorCallback);
  42468. const [editor, ...args] = guessEditor(specifiedEditor);
  42469. if (!editor) {
  42470. onErrorCallback(fileName, null);
  42471. return
  42472. }
  42473. if (
  42474. process.platform === 'linux' &&
  42475. fileName.startsWith('/mnt/') &&
  42476. /Microsoft/i.test(os$1.release())
  42477. ) {
  42478. // Assume WSL / "Bash on Ubuntu on Windows" is being used, and
  42479. // that the file exists on the Windows file system.
  42480. // `os.release()` is "4.4.0-43-Microsoft" in the current release
  42481. // build of WSL, see: https://github.com/Microsoft/BashOnWindows/issues/423#issuecomment-221627364
  42482. // When a Windows editor is specified, interop functionality can
  42483. // handle the path translation, but only if a relative path is used.
  42484. fileName = path$5.relative('', fileName);
  42485. }
  42486. if (lineNumber) {
  42487. const extraArgs = getArgumentsForPosition(editor, fileName, lineNumber, columnNumber);
  42488. args.push.apply(args, extraArgs);
  42489. } else {
  42490. args.push(fileName);
  42491. }
  42492. if (_childProcess && isTerminalEditor(editor)) {
  42493. // There's an existing editor process already and it's attached
  42494. // to the terminal, so go kill it. Otherwise two separate editor
  42495. // instances attach to the stdin/stdout which gets confusing.
  42496. _childProcess.kill('SIGKILL');
  42497. }
  42498. if (process.platform === 'win32') {
  42499. // On Windows, we need to use `exec` with the `shell: true` option,
  42500. // and some more sanitization is required.
  42501. // However, CMD.exe on Windows is vulnerable to RCE attacks given a file name of the
  42502. // form "C:\Users\myusername\Downloads\& curl 172.21.93.52".
  42503. // `create-react-app` used a safe file name pattern to validate user-provided file names:
  42504. // - https://github.com/facebook/create-react-app/pull/4866
  42505. // - https://github.com/facebook/create-react-app/pull/5431
  42506. // But that's not a viable solution for this package because
  42507. // it's depended on by so many meta frameworks that heavily rely on
  42508. // special characters in file names for filesystem-based routing.
  42509. // We need to at least:
  42510. // - Support `+` because it's used in SvelteKit and Vike
  42511. // - Support `$` because it's used in Remix
  42512. // - Support `(` and `)` because they are used in Analog, SolidStart, and Vike
  42513. // - Support `@` because it's used in Vike
  42514. // - Support `[` and `]` because they are widely used for [slug]
  42515. // So here we choose to use `^` to escape special characters instead.
  42516. // According to https://ss64.com/nt/syntax-esc.html,
  42517. // we can use `^` to escape `&`, `<`, `>`, `|`, `%`, and `^`
  42518. // I'm not sure if we have to escape all of these, but let's do it anyway
  42519. function escapeCmdArgs (cmdArgs) {
  42520. return cmdArgs.replace(/([&|<>,;=^])/g, '^$1')
  42521. }
  42522. // Need to double quote the editor path in case it contains spaces;
  42523. // If the fileName contains spaces, we also need to double quote it in the arguments
  42524. // However, there's a case that it's concatenated with line number and column number
  42525. // which is separated by `:`. We need to double quote the whole string in this case.
  42526. // Also, if the string contains the escape character `^`, it needs to be quoted, too.
  42527. function doubleQuoteIfNeeded(str) {
  42528. if (str.includes('^')) {
  42529. // If a string includes an escaped character, not only does it need to be quoted,
  42530. // but the quotes need to be escaped too.
  42531. return `^"${str}^"`
  42532. } else if (str.includes(' ')) {
  42533. return `"${str}"`
  42534. }
  42535. return str
  42536. }
  42537. const launchCommand = [editor, ...args.map(escapeCmdArgs)]
  42538. .map(doubleQuoteIfNeeded)
  42539. .join(' ');
  42540. _childProcess = childProcess$1.exec(launchCommand, {
  42541. stdio: 'inherit',
  42542. shell: true
  42543. });
  42544. } else {
  42545. _childProcess = childProcess$1.spawn(editor, args, { stdio: 'inherit' });
  42546. }
  42547. _childProcess.on('exit', function (errorCode) {
  42548. _childProcess = null;
  42549. if (errorCode) {
  42550. onErrorCallback(fileName, '(code ' + errorCode + ')');
  42551. }
  42552. });
  42553. _childProcess.on('error', function (error) {
  42554. let { code, message } = error;
  42555. if ('ENOENT' === code) {
  42556. message = `${message} ('${editor}' command does not exist in 'PATH')`;
  42557. }
  42558. onErrorCallback(fileName, message);
  42559. });
  42560. }
  42561. var launchEditor_1 = launchEditor;
  42562. const url$2 = require$$0$9;
  42563. const path$4 = require$$0$4;
  42564. const launch = launchEditor_1;
  42565. var launchEditorMiddleware = (specifiedEditor, srcRoot, onErrorCallback) => {
  42566. if (typeof specifiedEditor === 'function') {
  42567. onErrorCallback = specifiedEditor;
  42568. specifiedEditor = undefined;
  42569. }
  42570. if (typeof srcRoot === 'function') {
  42571. onErrorCallback = srcRoot;
  42572. srcRoot = undefined;
  42573. }
  42574. srcRoot = srcRoot || process.cwd();
  42575. return function launchEditorMiddleware (req, res) {
  42576. const { file } = url$2.parse(req.url, true).query || {};
  42577. if (!file) {
  42578. res.statusCode = 500;
  42579. res.end(`launch-editor-middleware: required query param "file" is missing.`);
  42580. } else {
  42581. launch(path$4.resolve(srcRoot, file), specifiedEditor, onErrorCallback);
  42582. res.end();
  42583. }
  42584. }
  42585. };
  42586. var launchEditorMiddleware$1 = /*@__PURE__*/getDefaultExportFromCjs(launchEditorMiddleware);
  42587. async function resolveHttpServer({ proxy }, app, httpsOptions) {
  42588. if (!httpsOptions) {
  42589. const { createServer } = await import('node:http');
  42590. return createServer(app);
  42591. }
  42592. if (proxy) {
  42593. const { createServer } = await import('node:https');
  42594. return createServer(httpsOptions, app);
  42595. } else {
  42596. const { createSecureServer } = await import('node:http2');
  42597. return createSecureServer(
  42598. {
  42599. // Manually increase the session memory to prevent 502 ENHANCE_YOUR_CALM
  42600. // errors on large numbers of requests
  42601. maxSessionMemory: 1e3,
  42602. ...httpsOptions,
  42603. allowHTTP1: true
  42604. },
  42605. // @ts-expect-error TODO: is this correct?
  42606. app
  42607. );
  42608. }
  42609. }
  42610. async function resolveHttpsConfig(https) {
  42611. if (!https) return void 0;
  42612. const [ca, cert, key, pfx] = await Promise.all([
  42613. readFileIfExists(https.ca),
  42614. readFileIfExists(https.cert),
  42615. readFileIfExists(https.key),
  42616. readFileIfExists(https.pfx)
  42617. ]);
  42618. return { ...https, ca, cert, key, pfx };
  42619. }
  42620. async function readFileIfExists(value) {
  42621. if (typeof value === "string") {
  42622. return fsp.readFile(path$n.resolve(value)).catch(() => value);
  42623. }
  42624. return value;
  42625. }
  42626. async function httpServerStart(httpServer, serverOptions) {
  42627. let { port, strictPort, host, logger } = serverOptions;
  42628. return new Promise((resolve, reject) => {
  42629. const onError = (e) => {
  42630. if (e.code === "EADDRINUSE") {
  42631. if (strictPort) {
  42632. httpServer.removeListener("error", onError);
  42633. reject(new Error(`Port ${port} is already in use`));
  42634. } else {
  42635. logger.info(`Port ${port} is in use, trying another one...`);
  42636. httpServer.listen(++port, host);
  42637. }
  42638. } else {
  42639. httpServer.removeListener("error", onError);
  42640. reject(e);
  42641. }
  42642. };
  42643. httpServer.on("error", onError);
  42644. httpServer.listen(port, host, () => {
  42645. httpServer.removeListener("error", onError);
  42646. resolve(port);
  42647. });
  42648. });
  42649. }
  42650. function setClientErrorHandler(server, logger) {
  42651. server.on("clientError", (err, socket) => {
  42652. let msg = "400 Bad Request";
  42653. if (err.code === "HPE_HEADER_OVERFLOW") {
  42654. msg = "431 Request Header Fields Too Large";
  42655. logger.warn(
  42656. colors$1.yellow(
  42657. "Server responded with status code 431. See https://vite.dev/guide/troubleshooting.html#_431-request-header-fields-too-large."
  42658. )
  42659. );
  42660. }
  42661. if (err.code === "ECONNRESET" || !socket.writable) {
  42662. return;
  42663. }
  42664. socket.end(`HTTP/1.1 ${msg}\r
  42665. \r
  42666. `);
  42667. });
  42668. }
  42669. const commonFsUtils = {
  42670. existsSync: fs__default.existsSync,
  42671. isDirectory,
  42672. tryResolveRealFile,
  42673. tryResolveRealFileWithExtensions,
  42674. tryResolveRealFileOrType
  42675. };
  42676. const cachedFsUtilsMap = /* @__PURE__ */ new WeakMap();
  42677. function getFsUtils(config) {
  42678. let fsUtils = cachedFsUtilsMap.get(config);
  42679. if (!fsUtils) {
  42680. if (config.command !== "serve" || config.server.fs.cachedChecks !== true || config.server.watch?.ignored || process.versions.pnp) {
  42681. fsUtils = commonFsUtils;
  42682. } else if (!config.resolve.preserveSymlinks && config.root !== getRealPath(config.root)) {
  42683. fsUtils = commonFsUtils;
  42684. } else {
  42685. fsUtils = createCachedFsUtils(config);
  42686. }
  42687. cachedFsUtilsMap.set(config, fsUtils);
  42688. }
  42689. return fsUtils;
  42690. }
  42691. function readDirCacheSync(file) {
  42692. let dirents;
  42693. try {
  42694. dirents = fs__default.readdirSync(file, { withFileTypes: true });
  42695. } catch {
  42696. return;
  42697. }
  42698. return direntsToDirentMap(dirents);
  42699. }
  42700. function direntsToDirentMap(fsDirents) {
  42701. const dirents = /* @__PURE__ */ new Map();
  42702. for (const dirent of fsDirents) {
  42703. const type = dirent.isDirectory() ? "directory" : dirent.isSymbolicLink() ? "symlink" : dirent.isFile() ? "file" : void 0;
  42704. if (type) {
  42705. dirents.set(dirent.name, { type });
  42706. }
  42707. }
  42708. return dirents;
  42709. }
  42710. function ensureFileMaybeSymlinkIsResolved(direntCache, filePath) {
  42711. if (direntCache.type !== "file_maybe_symlink") return;
  42712. const isSymlink = fs__default.lstatSync(filePath, { throwIfNoEntry: false })?.isSymbolicLink();
  42713. direntCache.type = isSymlink === void 0 ? "error" : isSymlink ? "symlink" : "file";
  42714. }
  42715. function pathUntilPart(root, parts, i) {
  42716. let p = root;
  42717. for (let k = 0; k < i; k++) p += "/" + parts[k];
  42718. return p;
  42719. }
  42720. function createCachedFsUtils(config) {
  42721. const root = config.root;
  42722. const rootDirPath = `${root}/`;
  42723. const rootCache = { type: "directory" };
  42724. const getDirentCacheSync = (parts) => {
  42725. let direntCache = rootCache;
  42726. for (let i = 0; i < parts.length; i++) {
  42727. if (direntCache.type === "directory") {
  42728. let dirPath;
  42729. if (!direntCache.dirents) {
  42730. dirPath = pathUntilPart(root, parts, i);
  42731. const dirents = readDirCacheSync(dirPath);
  42732. if (!dirents) {
  42733. direntCache.type = "error";
  42734. return;
  42735. }
  42736. direntCache.dirents = dirents;
  42737. }
  42738. const nextDirentCache = direntCache.dirents.get(parts[i]);
  42739. if (!nextDirentCache) {
  42740. return;
  42741. }
  42742. if (nextDirentCache.type === "directory_maybe_symlink") {
  42743. dirPath ??= pathUntilPart(root, parts, i + 1);
  42744. const isSymlink = fs__default.lstatSync(dirPath, { throwIfNoEntry: false })?.isSymbolicLink();
  42745. nextDirentCache.type = isSymlink ? "symlink" : "directory";
  42746. }
  42747. direntCache = nextDirentCache;
  42748. } else if (direntCache.type === "symlink") {
  42749. return direntCache;
  42750. } else if (direntCache.type === "error") {
  42751. return direntCache;
  42752. } else {
  42753. if (i !== parts.length - 1) {
  42754. return;
  42755. }
  42756. if (direntCache.type === "file_maybe_symlink") {
  42757. ensureFileMaybeSymlinkIsResolved(
  42758. direntCache,
  42759. pathUntilPart(root, parts, i)
  42760. );
  42761. return direntCache;
  42762. } else if (direntCache.type === "file") {
  42763. return direntCache;
  42764. } else {
  42765. return;
  42766. }
  42767. }
  42768. }
  42769. return direntCache;
  42770. };
  42771. function getDirentCacheFromPath(normalizedFile) {
  42772. if (normalizedFile[normalizedFile.length - 1] === "/") {
  42773. normalizedFile = normalizedFile.slice(0, -1);
  42774. }
  42775. if (normalizedFile === root) {
  42776. return rootCache;
  42777. }
  42778. if (!normalizedFile.startsWith(rootDirPath)) {
  42779. return void 0;
  42780. }
  42781. const pathFromRoot = normalizedFile.slice(rootDirPath.length);
  42782. const parts = pathFromRoot.split("/");
  42783. const direntCache = getDirentCacheSync(parts);
  42784. if (!direntCache || direntCache.type === "error") {
  42785. return false;
  42786. }
  42787. return direntCache;
  42788. }
  42789. function onPathAdd(file, type) {
  42790. const direntCache = getDirentCacheFromPath(
  42791. normalizePath$3(path$n.dirname(file))
  42792. );
  42793. if (direntCache && direntCache.type === "directory" && direntCache.dirents) {
  42794. direntCache.dirents.set(path$n.basename(file), { type });
  42795. }
  42796. }
  42797. function onPathUnlink(file) {
  42798. const direntCache = getDirentCacheFromPath(
  42799. normalizePath$3(path$n.dirname(file))
  42800. );
  42801. if (direntCache && direntCache.type === "directory" && direntCache.dirents) {
  42802. direntCache.dirents.delete(path$n.basename(file));
  42803. }
  42804. }
  42805. return {
  42806. existsSync(file) {
  42807. if (isInNodeModules$1(file)) {
  42808. return fs__default.existsSync(file);
  42809. }
  42810. const normalizedFile = normalizePath$3(file);
  42811. const direntCache = getDirentCacheFromPath(normalizedFile);
  42812. if (direntCache === void 0 || direntCache && direntCache.type === "symlink") {
  42813. return fs__default.existsSync(file);
  42814. }
  42815. return !!direntCache;
  42816. },
  42817. tryResolveRealFile(file, preserveSymlinks) {
  42818. if (isInNodeModules$1(file)) {
  42819. return tryResolveRealFile(file, preserveSymlinks);
  42820. }
  42821. const normalizedFile = normalizePath$3(file);
  42822. const direntCache = getDirentCacheFromPath(normalizedFile);
  42823. if (direntCache === void 0 || direntCache && direntCache.type === "symlink") {
  42824. return tryResolveRealFile(file, preserveSymlinks);
  42825. }
  42826. if (!direntCache || direntCache.type === "directory") {
  42827. return;
  42828. }
  42829. return normalizedFile;
  42830. },
  42831. tryResolveRealFileWithExtensions(file, extensions, preserveSymlinks) {
  42832. if (isInNodeModules$1(file)) {
  42833. return tryResolveRealFileWithExtensions(
  42834. file,
  42835. extensions,
  42836. preserveSymlinks
  42837. );
  42838. }
  42839. const normalizedFile = normalizePath$3(file);
  42840. const dirPath = path$n.posix.dirname(normalizedFile);
  42841. const direntCache = getDirentCacheFromPath(dirPath);
  42842. if (direntCache === void 0 || direntCache && direntCache.type === "symlink") {
  42843. return tryResolveRealFileWithExtensions(
  42844. file,
  42845. extensions,
  42846. preserveSymlinks
  42847. );
  42848. }
  42849. if (!direntCache || direntCache.type !== "directory") {
  42850. return;
  42851. }
  42852. if (!direntCache.dirents) {
  42853. const dirents = readDirCacheSync(dirPath);
  42854. if (!dirents) {
  42855. direntCache.type = "error";
  42856. return;
  42857. }
  42858. direntCache.dirents = dirents;
  42859. }
  42860. const base = path$n.posix.basename(normalizedFile);
  42861. for (const ext of extensions) {
  42862. const fileName = base + ext;
  42863. const fileDirentCache = direntCache.dirents.get(fileName);
  42864. if (fileDirentCache) {
  42865. const filePath = dirPath + "/" + fileName;
  42866. ensureFileMaybeSymlinkIsResolved(fileDirentCache, filePath);
  42867. if (fileDirentCache.type === "symlink") {
  42868. return tryResolveRealFile(filePath, preserveSymlinks);
  42869. }
  42870. if (fileDirentCache.type === "file") {
  42871. return filePath;
  42872. }
  42873. }
  42874. }
  42875. },
  42876. tryResolveRealFileOrType(file, preserveSymlinks) {
  42877. if (isInNodeModules$1(file)) {
  42878. return tryResolveRealFileOrType(file, preserveSymlinks);
  42879. }
  42880. const normalizedFile = normalizePath$3(file);
  42881. const direntCache = getDirentCacheFromPath(normalizedFile);
  42882. if (direntCache === void 0 || direntCache && direntCache.type === "symlink") {
  42883. return tryResolveRealFileOrType(file, preserveSymlinks);
  42884. }
  42885. if (!direntCache) {
  42886. return;
  42887. }
  42888. if (direntCache.type === "directory") {
  42889. return { type: "directory" };
  42890. }
  42891. return { path: normalizedFile, type: "file" };
  42892. },
  42893. isDirectory(dirPath) {
  42894. if (isInNodeModules$1(dirPath)) {
  42895. return isDirectory(dirPath);
  42896. }
  42897. const direntCache = getDirentCacheFromPath(normalizePath$3(dirPath));
  42898. if (direntCache === void 0 || direntCache && direntCache.type === "symlink") {
  42899. return isDirectory(dirPath);
  42900. }
  42901. return direntCache && direntCache.type === "directory";
  42902. },
  42903. initWatcher(watcher) {
  42904. watcher.on("add", (file) => {
  42905. onPathAdd(file, "file_maybe_symlink");
  42906. });
  42907. watcher.on("addDir", (dir) => {
  42908. onPathAdd(dir, "directory_maybe_symlink");
  42909. });
  42910. watcher.on("unlink", onPathUnlink);
  42911. watcher.on("unlinkDir", onPathUnlink);
  42912. }
  42913. };
  42914. }
  42915. function tryResolveRealFile(file, preserveSymlinks) {
  42916. const stat = tryStatSync(file);
  42917. if (stat?.isFile()) return getRealPath(file, preserveSymlinks);
  42918. }
  42919. function tryResolveRealFileWithExtensions(filePath, extensions, preserveSymlinks) {
  42920. for (const ext of extensions) {
  42921. const res = tryResolveRealFile(filePath + ext, preserveSymlinks);
  42922. if (res) return res;
  42923. }
  42924. }
  42925. function tryResolveRealFileOrType(file, preserveSymlinks) {
  42926. const fileStat = tryStatSync(file);
  42927. if (fileStat?.isFile()) {
  42928. return { path: getRealPath(file, preserveSymlinks), type: "file" };
  42929. }
  42930. if (fileStat?.isDirectory()) {
  42931. return { type: "directory" };
  42932. }
  42933. return;
  42934. }
  42935. function getRealPath(resolved, preserveSymlinks) {
  42936. if (!preserveSymlinks) {
  42937. resolved = safeRealpathSync(resolved);
  42938. }
  42939. return normalizePath$3(resolved);
  42940. }
  42941. function isDirectory(path2) {
  42942. const stat = tryStatSync(path2);
  42943. return stat?.isDirectory() ?? false;
  42944. }
  42945. /*!
  42946. * etag
  42947. * Copyright(c) 2014-2016 Douglas Christopher Wilson
  42948. * MIT Licensed
  42949. */
  42950. /**
  42951. * Module exports.
  42952. * @public
  42953. */
  42954. var etag_1 = etag;
  42955. /**
  42956. * Module dependencies.
  42957. * @private
  42958. */
  42959. var crypto = require$$3$1;
  42960. var Stats = require$$0__default.Stats;
  42961. /**
  42962. * Module variables.
  42963. * @private
  42964. */
  42965. var toString = Object.prototype.toString;
  42966. /**
  42967. * Generate an entity tag.
  42968. *
  42969. * @param {Buffer|string} entity
  42970. * @return {string}
  42971. * @private
  42972. */
  42973. function entitytag (entity) {
  42974. if (entity.length === 0) {
  42975. // fast-path empty
  42976. return '"0-2jmj7l5rSw0yVb/vlWAYkK/YBwk"'
  42977. }
  42978. // compute hash of entity
  42979. var hash = crypto
  42980. .createHash('sha1')
  42981. .update(entity, 'utf8')
  42982. .digest('base64')
  42983. .substring(0, 27);
  42984. // compute length of entity
  42985. var len = typeof entity === 'string'
  42986. ? Buffer.byteLength(entity, 'utf8')
  42987. : entity.length;
  42988. return '"' + len.toString(16) + '-' + hash + '"'
  42989. }
  42990. /**
  42991. * Create a simple ETag.
  42992. *
  42993. * @param {string|Buffer|Stats} entity
  42994. * @param {object} [options]
  42995. * @param {boolean} [options.weak]
  42996. * @return {String}
  42997. * @public
  42998. */
  42999. function etag (entity, options) {
  43000. if (entity == null) {
  43001. throw new TypeError('argument entity is required')
  43002. }
  43003. // support fs.Stats object
  43004. var isStats = isstats(entity);
  43005. var weak = options && typeof options.weak === 'boolean'
  43006. ? options.weak
  43007. : isStats;
  43008. // validate argument
  43009. if (!isStats && typeof entity !== 'string' && !Buffer.isBuffer(entity)) {
  43010. throw new TypeError('argument entity must be string, Buffer, or fs.Stats')
  43011. }
  43012. // generate entity tag
  43013. var tag = isStats
  43014. ? stattag(entity)
  43015. : entitytag(entity);
  43016. return weak
  43017. ? 'W/' + tag
  43018. : tag
  43019. }
  43020. /**
  43021. * Determine if object is a Stats object.
  43022. *
  43023. * @param {object} obj
  43024. * @return {boolean}
  43025. * @api private
  43026. */
  43027. function isstats (obj) {
  43028. // genuine fs.Stats
  43029. if (typeof Stats === 'function' && obj instanceof Stats) {
  43030. return true
  43031. }
  43032. // quack quack
  43033. return obj && typeof obj === 'object' &&
  43034. 'ctime' in obj && toString.call(obj.ctime) === '[object Date]' &&
  43035. 'mtime' in obj && toString.call(obj.mtime) === '[object Date]' &&
  43036. 'ino' in obj && typeof obj.ino === 'number' &&
  43037. 'size' in obj && typeof obj.size === 'number'
  43038. }
  43039. /**
  43040. * Generate a tag for a stat.
  43041. *
  43042. * @param {object} stat
  43043. * @return {string}
  43044. * @private
  43045. */
  43046. function stattag (stat) {
  43047. var mtime = stat.mtime.getTime().toString(16);
  43048. var size = stat.size.toString(16);
  43049. return '"' + size + '-' + mtime + '"'
  43050. }
  43051. var getEtag = /*@__PURE__*/getDefaultExportFromCjs(etag_1);
  43052. function e(e,n,r){throw new Error(r?`No known conditions for "${n}" specifier in "${e}" package`:`Missing "${n}" specifier in "${e}" package`)}function n(n,i,o,f){let s,u,l=r(n,o),c=function(e){let n=new Set(["default",...e.conditions||[]]);return e.unsafe||n.add(e.require?"require":"import"),e.unsafe||n.add(e.browser?"browser":"node"),n}(f||{}),a=i[l];if(void 0===a){let e,n,r,t;for(t in i)n&&t.length<n.length||("/"===t[t.length-1]&&l.startsWith(t)?(u=l.substring(t.length),n=t):t.length>1&&(r=t.indexOf("*",1),~r&&(e=RegExp("^"+t.substring(0,r)+"(.*)"+t.substring(1+r)).exec(l),e&&e[1]&&(u=e[1],n=t))));a=i[n];}return a||e(n,l),s=t(a,c),s||e(n,l,1),u&&function(e,n){let r,t=0,i=e.length,o=/[*]/g,f=/[/]$/;for(;t<i;t++)e[t]=o.test(r=e[t])?r.replace(o,n):f.test(r)?r+n:r;}(s,u),s}function r(e,n,r){if(e===n||"."===n)return ".";let t=e+"/",i=t.length,o=n.slice(0,i)===t,f=o?n.slice(i):n;return "#"===f[0]?f:o||!r?"./"===f.slice(0,2)?f:"./"+f:f}function t(e,n,r){if(e){if("string"==typeof e)return r&&r.add(e),[e];let i,o;if(Array.isArray(e)){for(o=r||new Set,i=0;i<e.length;i++)t(e[i],n,o);if(!r&&o.size)return [...o]}else for(i in e)if(n.has(i))return t(e[i],n,r)}}function o(e,r,t){let i,o=e.exports;if(o){if("string"==typeof o)o={".":o};else for(i in o){"."!==i[0]&&(o={".":o});break}return n(e.name,o,r||".",t)}}function f(e,r,t){if(e.imports)return n(e.name,e.imports,r,t)}
  43053. const normalizedClientEntry$1 = normalizePath$3(CLIENT_ENTRY);
  43054. const normalizedEnvEntry$1 = normalizePath$3(ENV_ENTRY);
  43055. const ERR_RESOLVE_PACKAGE_ENTRY_FAIL = "ERR_RESOLVE_PACKAGE_ENTRY_FAIL";
  43056. const browserExternalId = "__vite-browser-external";
  43057. const optionalPeerDepId = "__vite-optional-peer-dep";
  43058. const subpathImportsPrefix = "#";
  43059. const startsWithWordCharRE = /^\w/;
  43060. const debug$c = createDebugger("vite:resolve-details", {
  43061. onlyWhenFocused: true
  43062. });
  43063. function resolvePlugin(resolveOptions) {
  43064. const {
  43065. root,
  43066. isProduction,
  43067. asSrc,
  43068. ssrConfig,
  43069. preferRelative = false
  43070. } = resolveOptions;
  43071. const {
  43072. target: ssrTarget,
  43073. noExternal: ssrNoExternal,
  43074. external: ssrExternal
  43075. } = ssrConfig ?? {};
  43076. const rootInRoot = tryStatSync(path$n.join(root, root))?.isDirectory() ?? false;
  43077. return {
  43078. name: "vite:resolve",
  43079. async resolveId(id, importer, resolveOpts) {
  43080. if (id[0] === "\0" || id.startsWith("virtual:") || // When injected directly in html/client code
  43081. id.startsWith("/virtual:")) {
  43082. return;
  43083. }
  43084. const ssr = resolveOpts?.ssr === true;
  43085. const depsOptimizer = resolveOptions.getDepsOptimizer?.(ssr);
  43086. if (id.startsWith(browserExternalId)) {
  43087. return id;
  43088. }
  43089. const targetWeb = !ssr || ssrTarget === "webworker";
  43090. const isRequire = resolveOpts?.custom?.["node-resolve"]?.isRequire ?? false;
  43091. const ssrConditions = resolveOptions.ssrConfig?.resolve?.conditions || resolveOptions.conditions;
  43092. const options = {
  43093. isRequire,
  43094. ...resolveOptions,
  43095. scan: resolveOpts?.scan ?? resolveOptions.scan,
  43096. conditions: ssr ? ssrConditions : resolveOptions.conditions
  43097. };
  43098. const resolvedImports = resolveSubpathImports(
  43099. id,
  43100. importer,
  43101. options,
  43102. targetWeb
  43103. );
  43104. if (resolvedImports) {
  43105. id = resolvedImports;
  43106. if (resolveOpts.custom?.["vite:import-glob"]?.isSubImportsPattern) {
  43107. return normalizePath$3(path$n.join(root, id));
  43108. }
  43109. }
  43110. if (importer) {
  43111. if (isTsRequest(importer) || resolveOpts.custom?.depScan?.loader?.startsWith("ts")) {
  43112. options.isFromTsImporter = true;
  43113. } else {
  43114. const moduleLang = this.getModuleInfo(importer)?.meta?.vite?.lang;
  43115. options.isFromTsImporter = moduleLang && isTsRequest(`.${moduleLang}`);
  43116. }
  43117. }
  43118. let res;
  43119. if (asSrc && depsOptimizer?.isOptimizedDepUrl(id)) {
  43120. const optimizedPath = id.startsWith(FS_PREFIX) ? fsPathFromId(id) : normalizePath$3(path$n.resolve(root, id.slice(1)));
  43121. return optimizedPath;
  43122. }
  43123. if (asSrc && id.startsWith(FS_PREFIX)) {
  43124. res = fsPathFromId(id);
  43125. debug$c?.(`[@fs] ${colors$1.cyan(id)} -> ${colors$1.dim(res)}`);
  43126. return ensureVersionQuery(res, id, options, depsOptimizer);
  43127. }
  43128. if (asSrc && id[0] === "/" && (rootInRoot || !id.startsWith(withTrailingSlash(root)))) {
  43129. const fsPath = path$n.resolve(root, id.slice(1));
  43130. if (res = tryFsResolve(fsPath, options)) {
  43131. debug$c?.(`[url] ${colors$1.cyan(id)} -> ${colors$1.dim(res)}`);
  43132. return ensureVersionQuery(res, id, options, depsOptimizer);
  43133. }
  43134. }
  43135. if (id[0] === "." || (preferRelative || importer?.endsWith(".html")) && startsWithWordCharRE.test(id)) {
  43136. const basedir = importer ? path$n.dirname(importer) : process.cwd();
  43137. const fsPath = path$n.resolve(basedir, id);
  43138. const normalizedFsPath = normalizePath$3(fsPath);
  43139. if (depsOptimizer?.isOptimizedDepFile(normalizedFsPath)) {
  43140. if (!resolveOptions.isBuild && !DEP_VERSION_RE.test(normalizedFsPath)) {
  43141. const browserHash = optimizedDepInfoFromFile(
  43142. depsOptimizer.metadata,
  43143. normalizedFsPath
  43144. )?.browserHash;
  43145. if (browserHash) {
  43146. return injectQuery(normalizedFsPath, `v=${browserHash}`);
  43147. }
  43148. }
  43149. return normalizedFsPath;
  43150. }
  43151. if (targetWeb && options.mainFields.includes("browser") && (res = tryResolveBrowserMapping(fsPath, importer, options, true))) {
  43152. return res;
  43153. }
  43154. if (res = tryFsResolve(fsPath, options)) {
  43155. res = ensureVersionQuery(res, id, options, depsOptimizer);
  43156. debug$c?.(`[relative] ${colors$1.cyan(id)} -> ${colors$1.dim(res)}`);
  43157. if (!options.idOnly && !options.scan && options.isBuild && !importer?.endsWith(".html")) {
  43158. const resPkg = findNearestPackageData(
  43159. path$n.dirname(res),
  43160. options.packageCache
  43161. );
  43162. if (resPkg) {
  43163. return {
  43164. id: res,
  43165. moduleSideEffects: resPkg.hasSideEffects(res)
  43166. };
  43167. }
  43168. }
  43169. return res;
  43170. }
  43171. }
  43172. if (isWindows$3 && id[0] === "/") {
  43173. const basedir = importer ? path$n.dirname(importer) : process.cwd();
  43174. const fsPath = path$n.resolve(basedir, id);
  43175. if (res = tryFsResolve(fsPath, options)) {
  43176. debug$c?.(`[drive-relative] ${colors$1.cyan(id)} -> ${colors$1.dim(res)}`);
  43177. return ensureVersionQuery(res, id, options, depsOptimizer);
  43178. }
  43179. }
  43180. if (isNonDriveRelativeAbsolutePath(id) && (res = tryFsResolve(id, options))) {
  43181. debug$c?.(`[fs] ${colors$1.cyan(id)} -> ${colors$1.dim(res)}`);
  43182. return ensureVersionQuery(res, id, options, depsOptimizer);
  43183. }
  43184. if (isExternalUrl(id)) {
  43185. return options.idOnly ? id : { id, external: true };
  43186. }
  43187. if (isDataUrl(id)) {
  43188. return null;
  43189. }
  43190. if (bareImportRE.test(id)) {
  43191. const external = options.shouldExternalize?.(id, importer);
  43192. if (!external && asSrc && depsOptimizer && !options.scan && (res = await tryOptimizedResolve(
  43193. depsOptimizer,
  43194. id,
  43195. importer,
  43196. options.preserveSymlinks,
  43197. options.packageCache
  43198. ))) {
  43199. return res;
  43200. }
  43201. if (targetWeb && options.mainFields.includes("browser") && (res = tryResolveBrowserMapping(
  43202. id,
  43203. importer,
  43204. options,
  43205. false,
  43206. external
  43207. ))) {
  43208. return res;
  43209. }
  43210. if (res = tryNodeResolve(
  43211. id,
  43212. importer,
  43213. options,
  43214. targetWeb,
  43215. depsOptimizer,
  43216. ssr,
  43217. external
  43218. )) {
  43219. return res;
  43220. }
  43221. if (isBuiltin(id)) {
  43222. if (ssr) {
  43223. if (targetWeb && ssrNoExternal === true && // if both noExternal and external are true, noExternal will take the higher priority and bundle it.
  43224. // only if the id is explicitly listed in external, we will externalize it and skip this error.
  43225. (ssrExternal === true || !ssrExternal?.includes(id))) {
  43226. let message = `Cannot bundle Node.js built-in "${id}"`;
  43227. if (importer) {
  43228. message += ` imported from "${path$n.relative(
  43229. process.cwd(),
  43230. importer
  43231. )}"`;
  43232. }
  43233. message += `. Consider disabling ssr.noExternal or remove the built-in dependency.`;
  43234. this.error(message);
  43235. }
  43236. return options.idOnly ? id : { id, external: true, moduleSideEffects: false };
  43237. } else {
  43238. if (!asSrc) {
  43239. debug$c?.(
  43240. `externalized node built-in "${id}" to empty module. (imported by: ${colors$1.white(colors$1.dim(importer))})`
  43241. );
  43242. } else if (isProduction) {
  43243. this.warn(
  43244. `Module "${id}" has been externalized for browser compatibility, imported by "${importer}". See https://vite.dev/guide/troubleshooting.html#module-externalized-for-browser-compatibility for more details.`
  43245. );
  43246. }
  43247. return isProduction ? browserExternalId : `${browserExternalId}:${id}`;
  43248. }
  43249. }
  43250. }
  43251. debug$c?.(`[fallthrough] ${colors$1.dim(id)}`);
  43252. },
  43253. load(id) {
  43254. if (id.startsWith(browserExternalId)) {
  43255. if (isProduction) {
  43256. return `export default {}`;
  43257. } else {
  43258. id = id.slice(browserExternalId.length + 1);
  43259. return `export default new Proxy({}, {
  43260. get(_, key) {
  43261. throw new Error(\`Module "${id}" has been externalized for browser compatibility. Cannot access "${id}.\${key}" in client code. See https://vite.dev/guide/troubleshooting.html#module-externalized-for-browser-compatibility for more details.\`)
  43262. }
  43263. })`;
  43264. }
  43265. }
  43266. if (id.startsWith(optionalPeerDepId)) {
  43267. if (isProduction) {
  43268. return `export default {}`;
  43269. } else {
  43270. const [, peerDep, parentDep] = id.split(":");
  43271. return `throw new Error(\`Could not resolve "${peerDep}" imported by "${parentDep}". Is it installed?\`)`;
  43272. }
  43273. }
  43274. }
  43275. };
  43276. }
  43277. function resolveSubpathImports(id, importer, options, targetWeb) {
  43278. if (!importer || !id.startsWith(subpathImportsPrefix)) return;
  43279. const basedir = path$n.dirname(importer);
  43280. const pkgData = findNearestPackageData(basedir, options.packageCache);
  43281. if (!pkgData) return;
  43282. let { file: idWithoutPostfix, postfix } = splitFileAndPostfix(id.slice(1));
  43283. idWithoutPostfix = "#" + idWithoutPostfix;
  43284. let importsPath = resolveExportsOrImports(
  43285. pkgData.data,
  43286. idWithoutPostfix,
  43287. options,
  43288. targetWeb,
  43289. "imports"
  43290. );
  43291. if (importsPath?.[0] === ".") {
  43292. importsPath = path$n.relative(basedir, path$n.join(pkgData.dir, importsPath));
  43293. if (importsPath[0] !== ".") {
  43294. importsPath = `./${importsPath}`;
  43295. }
  43296. }
  43297. return importsPath + postfix;
  43298. }
  43299. function ensureVersionQuery(resolved, id, options, depsOptimizer) {
  43300. if (!options.isBuild && !options.scan && depsOptimizer && !(resolved === normalizedClientEntry$1 || resolved === normalizedEnvEntry$1)) {
  43301. const isNodeModule = isInNodeModules$1(id) || isInNodeModules$1(resolved);
  43302. if (isNodeModule && !DEP_VERSION_RE.test(resolved)) {
  43303. const versionHash = depsOptimizer.metadata.browserHash;
  43304. if (versionHash && isOptimizable(resolved, depsOptimizer.options)) {
  43305. resolved = injectQuery(resolved, `v=${versionHash}`);
  43306. }
  43307. }
  43308. }
  43309. return resolved;
  43310. }
  43311. function splitFileAndPostfix(path2) {
  43312. const file = cleanUrl(path2);
  43313. return { file, postfix: path2.slice(file.length) };
  43314. }
  43315. function tryFsResolve(fsPath, options, tryIndex = true, targetWeb = true, skipPackageJson = false) {
  43316. const hashIndex = fsPath.indexOf("#");
  43317. if (hashIndex >= 0 && isInNodeModules$1(fsPath)) {
  43318. const queryIndex = fsPath.indexOf("?");
  43319. if (queryIndex < 0 || queryIndex > hashIndex) {
  43320. const file2 = queryIndex > hashIndex ? fsPath.slice(0, queryIndex) : fsPath;
  43321. const res2 = tryCleanFsResolve(
  43322. file2,
  43323. options,
  43324. tryIndex,
  43325. targetWeb,
  43326. skipPackageJson
  43327. );
  43328. if (res2) return res2 + fsPath.slice(file2.length);
  43329. }
  43330. }
  43331. const { file, postfix } = splitFileAndPostfix(fsPath);
  43332. const res = tryCleanFsResolve(
  43333. file,
  43334. options,
  43335. tryIndex,
  43336. targetWeb,
  43337. skipPackageJson
  43338. );
  43339. if (res) return res + postfix;
  43340. }
  43341. const knownTsOutputRE = /\.(?:js|mjs|cjs|jsx)$/;
  43342. const isPossibleTsOutput = (url) => knownTsOutputRE.test(url);
  43343. function tryCleanFsResolve(file, options, tryIndex = true, targetWeb = true, skipPackageJson = false) {
  43344. const { tryPrefix, extensions, preserveSymlinks } = options;
  43345. const fsUtils = options.fsUtils ?? commonFsUtils;
  43346. const fileResult = fsUtils.tryResolveRealFileOrType(
  43347. file,
  43348. options.preserveSymlinks
  43349. );
  43350. if (fileResult?.path) return fileResult.path;
  43351. let res;
  43352. const possibleJsToTs = options.isFromTsImporter && isPossibleTsOutput(file);
  43353. if (possibleJsToTs || options.extensions.length || tryPrefix) {
  43354. const dirPath = path$n.dirname(file);
  43355. if (fsUtils.isDirectory(dirPath)) {
  43356. if (possibleJsToTs) {
  43357. const fileExt = path$n.extname(file);
  43358. const fileName = file.slice(0, -fileExt.length);
  43359. if (res = fsUtils.tryResolveRealFile(
  43360. fileName + fileExt.replace("js", "ts"),
  43361. preserveSymlinks
  43362. ))
  43363. return res;
  43364. if (fileExt === ".js" && (res = fsUtils.tryResolveRealFile(
  43365. fileName + ".tsx",
  43366. preserveSymlinks
  43367. )))
  43368. return res;
  43369. }
  43370. if (res = fsUtils.tryResolveRealFileWithExtensions(
  43371. file,
  43372. extensions,
  43373. preserveSymlinks
  43374. ))
  43375. return res;
  43376. if (tryPrefix) {
  43377. const prefixed = `${dirPath}/${options.tryPrefix}${path$n.basename(file)}`;
  43378. if (res = fsUtils.tryResolveRealFile(prefixed, preserveSymlinks))
  43379. return res;
  43380. if (res = fsUtils.tryResolveRealFileWithExtensions(
  43381. prefixed,
  43382. extensions,
  43383. preserveSymlinks
  43384. ))
  43385. return res;
  43386. }
  43387. }
  43388. }
  43389. if (tryIndex && fileResult?.type === "directory") {
  43390. const dirPath = file;
  43391. if (!skipPackageJson) {
  43392. let pkgPath = `${dirPath}/package.json`;
  43393. try {
  43394. if (fsUtils.existsSync(pkgPath)) {
  43395. if (!options.preserveSymlinks) {
  43396. pkgPath = safeRealpathSync(pkgPath);
  43397. }
  43398. const pkg = loadPackageData(pkgPath);
  43399. return resolvePackageEntry(dirPath, pkg, targetWeb, options);
  43400. }
  43401. } catch (e) {
  43402. if (e.code !== ERR_RESOLVE_PACKAGE_ENTRY_FAIL && e.code !== "ENOENT")
  43403. throw e;
  43404. }
  43405. }
  43406. if (res = fsUtils.tryResolveRealFileWithExtensions(
  43407. `${dirPath}/index`,
  43408. extensions,
  43409. preserveSymlinks
  43410. ))
  43411. return res;
  43412. if (tryPrefix) {
  43413. if (res = fsUtils.tryResolveRealFileWithExtensions(
  43414. `${dirPath}/${options.tryPrefix}index`,
  43415. extensions,
  43416. preserveSymlinks
  43417. ))
  43418. return res;
  43419. }
  43420. }
  43421. }
  43422. function tryNodeResolve(id, importer, options, targetWeb, depsOptimizer, ssr = false, externalize, allowLinkedExternal = true) {
  43423. const { root, dedupe, isBuild, preserveSymlinks, packageCache } = options;
  43424. const deepMatch = deepImportRE.exec(id);
  43425. const pkgId = deepMatch ? deepMatch[1] || deepMatch[2] : cleanUrl(id);
  43426. let basedir;
  43427. if (dedupe?.includes(pkgId)) {
  43428. basedir = root;
  43429. } else if (importer && path$n.isAbsolute(importer) && // css processing appends `*` for importer
  43430. (importer[importer.length - 1] === "*" || fs__default.existsSync(cleanUrl(importer)))) {
  43431. basedir = path$n.dirname(importer);
  43432. } else {
  43433. basedir = root;
  43434. }
  43435. let selfPkg = null;
  43436. if (!isBuiltin(id) && !id.includes("\0") && bareImportRE.test(id)) {
  43437. const selfPackageData = findNearestPackageData(basedir, packageCache);
  43438. selfPkg = selfPackageData?.data.exports && selfPackageData?.data.name === pkgId ? selfPackageData : null;
  43439. }
  43440. const pkg = selfPkg || resolvePackageData(pkgId, basedir, preserveSymlinks, packageCache);
  43441. if (!pkg) {
  43442. if (basedir !== root && // root has no peer dep
  43443. !isBuiltin(id) && !id.includes("\0") && bareImportRE.test(id)) {
  43444. const mainPkg = findNearestMainPackageData(basedir, packageCache)?.data;
  43445. if (mainPkg) {
  43446. const pkgName = getNpmPackageName(id);
  43447. if (pkgName != null && mainPkg.peerDependencies?.[pkgName] && mainPkg.peerDependenciesMeta?.[pkgName]?.optional) {
  43448. return {
  43449. id: `${optionalPeerDepId}:${id}:${mainPkg.name}`
  43450. };
  43451. }
  43452. }
  43453. }
  43454. return;
  43455. }
  43456. const resolveId = deepMatch ? resolveDeepImport : resolvePackageEntry;
  43457. const unresolvedId = deepMatch ? "." + id.slice(pkgId.length) : id;
  43458. let resolved;
  43459. try {
  43460. resolved = resolveId(unresolvedId, pkg, targetWeb, options);
  43461. } catch (err) {
  43462. if (!options.tryEsmOnly) {
  43463. throw err;
  43464. }
  43465. }
  43466. if (!resolved && options.tryEsmOnly) {
  43467. resolved = resolveId(unresolvedId, pkg, targetWeb, {
  43468. ...options,
  43469. isRequire: false,
  43470. mainFields: DEFAULT_MAIN_FIELDS,
  43471. extensions: DEFAULT_EXTENSIONS
  43472. });
  43473. }
  43474. if (!resolved) {
  43475. return;
  43476. }
  43477. const processResult = (resolved2) => {
  43478. if (!externalize) {
  43479. return resolved2;
  43480. }
  43481. if (!allowLinkedExternal && !isInNodeModules$1(resolved2.id)) {
  43482. return resolved2;
  43483. }
  43484. const resolvedExt = path$n.extname(resolved2.id);
  43485. if (resolvedExt && resolvedExt !== ".js" && resolvedExt !== ".mjs" && resolvedExt !== ".cjs") {
  43486. return resolved2;
  43487. }
  43488. let resolvedId = id;
  43489. if (deepMatch && !pkg?.data.exports && path$n.extname(id) !== resolvedExt) {
  43490. const index = resolved2.id.indexOf(id);
  43491. if (index > -1) {
  43492. resolvedId = resolved2.id.slice(index);
  43493. debug$c?.(
  43494. `[processResult] ${colors$1.cyan(id)} -> ${colors$1.dim(resolvedId)}`
  43495. );
  43496. }
  43497. }
  43498. return { ...resolved2, id: resolvedId, external: true };
  43499. };
  43500. if (!options.idOnly && (!options.scan && isBuild && !depsOptimizer || externalize)) {
  43501. return processResult({
  43502. id: resolved,
  43503. moduleSideEffects: pkg.hasSideEffects(resolved)
  43504. });
  43505. }
  43506. if (!options.ssrOptimizeCheck && (!isInNodeModules$1(resolved) || // linked
  43507. !depsOptimizer || // resolving before listening to the server
  43508. options.scan)) {
  43509. return { id: resolved };
  43510. }
  43511. const isJsType = depsOptimizer ? isOptimizable(resolved, depsOptimizer.options) : OPTIMIZABLE_ENTRY_RE.test(resolved);
  43512. let exclude = depsOptimizer?.options.exclude;
  43513. let include = depsOptimizer?.options.include;
  43514. if (options.ssrOptimizeCheck) {
  43515. exclude = options.ssrConfig?.optimizeDeps?.exclude;
  43516. include = options.ssrConfig?.optimizeDeps?.include;
  43517. }
  43518. const skipOptimization = !options.ssrOptimizeCheck && depsOptimizer?.options.noDiscovery || !isJsType || importer && isInNodeModules$1(importer) || exclude?.includes(pkgId) || exclude?.includes(id) || SPECIAL_QUERY_RE.test(resolved) || // During dev SSR, we don't have a way to reload the module graph if
  43519. // a non-optimized dep is found. So we need to skip optimization here.
  43520. // The only optimized deps are the ones explicitly listed in the config.
  43521. !options.ssrOptimizeCheck && !isBuild && ssr || // Only optimize non-external CJS deps during SSR by default
  43522. ssr && isFilePathESM(resolved, options.packageCache) && !(include?.includes(pkgId) || include?.includes(id));
  43523. if (options.ssrOptimizeCheck) {
  43524. return {
  43525. id: skipOptimization ? injectQuery(resolved, `__vite_skip_optimization`) : resolved
  43526. };
  43527. }
  43528. if (skipOptimization) {
  43529. if (!isBuild) {
  43530. const versionHash = depsOptimizer.metadata.browserHash;
  43531. if (versionHash && isJsType) {
  43532. resolved = injectQuery(resolved, `v=${versionHash}`);
  43533. }
  43534. }
  43535. } else {
  43536. const optimizedInfo = depsOptimizer.registerMissingImport(id, resolved);
  43537. resolved = depsOptimizer.getOptimizedDepId(optimizedInfo);
  43538. }
  43539. if (!options.idOnly && !options.scan && isBuild) {
  43540. return {
  43541. id: resolved,
  43542. moduleSideEffects: pkg.hasSideEffects(resolved)
  43543. };
  43544. } else {
  43545. return { id: resolved };
  43546. }
  43547. }
  43548. async function tryOptimizedResolve(depsOptimizer, id, importer, preserveSymlinks, packageCache) {
  43549. await depsOptimizer.scanProcessing;
  43550. const metadata = depsOptimizer.metadata;
  43551. const depInfo = optimizedDepInfoFromId(metadata, id);
  43552. if (depInfo) {
  43553. return depsOptimizer.getOptimizedDepId(depInfo);
  43554. }
  43555. if (!importer) return;
  43556. let idPkgDir;
  43557. const nestedIdMatch = `> ${id}`;
  43558. for (const optimizedData of metadata.depInfoList) {
  43559. if (!optimizedData.src) continue;
  43560. if (!optimizedData.id.endsWith(nestedIdMatch)) continue;
  43561. if (idPkgDir == null) {
  43562. const pkgName = getNpmPackageName(id);
  43563. if (!pkgName) break;
  43564. idPkgDir = resolvePackageData(
  43565. pkgName,
  43566. importer,
  43567. preserveSymlinks,
  43568. packageCache
  43569. )?.dir;
  43570. if (idPkgDir == null) break;
  43571. idPkgDir = normalizePath$3(idPkgDir);
  43572. }
  43573. if (optimizedData.src.startsWith(withTrailingSlash(idPkgDir))) {
  43574. return depsOptimizer.getOptimizedDepId(optimizedData);
  43575. }
  43576. }
  43577. }
  43578. function resolvePackageEntry(id, { dir, data, setResolvedCache, getResolvedCache }, targetWeb, options) {
  43579. const { file: idWithoutPostfix, postfix } = splitFileAndPostfix(id);
  43580. const cached = getResolvedCache(".", targetWeb);
  43581. if (cached) {
  43582. return cached + postfix;
  43583. }
  43584. try {
  43585. let entryPoint;
  43586. if (data.exports) {
  43587. entryPoint = resolveExportsOrImports(
  43588. data,
  43589. ".",
  43590. options,
  43591. targetWeb,
  43592. "exports"
  43593. );
  43594. }
  43595. if (!entryPoint) {
  43596. for (const field of options.mainFields) {
  43597. if (field === "browser") {
  43598. if (targetWeb) {
  43599. entryPoint = tryResolveBrowserEntry(dir, data, options);
  43600. if (entryPoint) {
  43601. break;
  43602. }
  43603. }
  43604. } else if (typeof data[field] === "string") {
  43605. entryPoint = data[field];
  43606. break;
  43607. }
  43608. }
  43609. }
  43610. entryPoint ||= data.main;
  43611. const entryPoints = entryPoint ? [entryPoint] : ["index.js", "index.json", "index.node"];
  43612. for (let entry of entryPoints) {
  43613. let skipPackageJson = false;
  43614. if (options.mainFields[0] === "sass" && !options.extensions.includes(path$n.extname(entry))) {
  43615. entry = "";
  43616. skipPackageJson = true;
  43617. } else {
  43618. const { browser: browserField } = data;
  43619. if (targetWeb && options.mainFields.includes("browser") && isObject$1(browserField)) {
  43620. entry = mapWithBrowserField(entry, browserField) || entry;
  43621. }
  43622. }
  43623. const entryPointPath = path$n.join(dir, entry);
  43624. const resolvedEntryPoint = tryFsResolve(
  43625. entryPointPath,
  43626. options,
  43627. true,
  43628. true,
  43629. skipPackageJson
  43630. );
  43631. if (resolvedEntryPoint) {
  43632. debug$c?.(
  43633. `[package entry] ${colors$1.cyan(idWithoutPostfix)} -> ${colors$1.dim(
  43634. resolvedEntryPoint
  43635. )}${postfix !== "" ? ` (postfix: ${postfix})` : ""}`
  43636. );
  43637. setResolvedCache(".", resolvedEntryPoint, targetWeb);
  43638. return resolvedEntryPoint + postfix;
  43639. }
  43640. }
  43641. } catch (e) {
  43642. packageEntryFailure(id, e.message);
  43643. }
  43644. packageEntryFailure(id);
  43645. }
  43646. function packageEntryFailure(id, details) {
  43647. const err = new Error(
  43648. `Failed to resolve entry for package "${id}". The package may have incorrect main/module/exports specified in its package.json` + (details ? ": " + details : ".")
  43649. );
  43650. err.code = ERR_RESOLVE_PACKAGE_ENTRY_FAIL;
  43651. throw err;
  43652. }
  43653. function resolveExportsOrImports(pkg, key, options, targetWeb, type) {
  43654. const additionalConditions = new Set(
  43655. options.overrideConditions || [
  43656. "production",
  43657. "development",
  43658. "module",
  43659. ...options.conditions
  43660. ]
  43661. );
  43662. const conditions = [...additionalConditions].filter((condition) => {
  43663. switch (condition) {
  43664. case "production":
  43665. return options.isProduction;
  43666. case "development":
  43667. return !options.isProduction;
  43668. }
  43669. return true;
  43670. });
  43671. const fn = type === "imports" ? f : o;
  43672. const result = fn(pkg, key, {
  43673. browser: targetWeb && !additionalConditions.has("node"),
  43674. require: options.isRequire && !additionalConditions.has("import"),
  43675. conditions
  43676. });
  43677. return result ? result[0] : void 0;
  43678. }
  43679. function resolveDeepImport(id, {
  43680. webResolvedImports,
  43681. setResolvedCache,
  43682. getResolvedCache,
  43683. dir,
  43684. data
  43685. }, targetWeb, options) {
  43686. const cache = getResolvedCache(id, targetWeb);
  43687. if (cache) {
  43688. return cache;
  43689. }
  43690. let relativeId = id;
  43691. const { exports: exportsField, browser: browserField } = data;
  43692. if (exportsField) {
  43693. if (isObject$1(exportsField) && !Array.isArray(exportsField)) {
  43694. const { file, postfix } = splitFileAndPostfix(relativeId);
  43695. const exportsId = resolveExportsOrImports(
  43696. data,
  43697. file,
  43698. options,
  43699. targetWeb,
  43700. "exports"
  43701. );
  43702. if (exportsId !== void 0) {
  43703. relativeId = exportsId + postfix;
  43704. } else {
  43705. relativeId = void 0;
  43706. }
  43707. } else {
  43708. relativeId = void 0;
  43709. }
  43710. if (!relativeId) {
  43711. throw new Error(
  43712. `Package subpath '${relativeId}' is not defined by "exports" in ${path$n.join(dir, "package.json")}.`
  43713. );
  43714. }
  43715. } else if (targetWeb && options.mainFields.includes("browser") && isObject$1(browserField)) {
  43716. const { file, postfix } = splitFileAndPostfix(relativeId);
  43717. const mapped = mapWithBrowserField(file, browserField);
  43718. if (mapped) {
  43719. relativeId = mapped + postfix;
  43720. } else if (mapped === false) {
  43721. return webResolvedImports[id] = browserExternalId;
  43722. }
  43723. }
  43724. if (relativeId) {
  43725. const resolved = tryFsResolve(
  43726. path$n.join(dir, relativeId),
  43727. options,
  43728. !exportsField,
  43729. // try index only if no exports field
  43730. targetWeb
  43731. );
  43732. if (resolved) {
  43733. debug$c?.(
  43734. `[node/deep-import] ${colors$1.cyan(id)} -> ${colors$1.dim(resolved)}`
  43735. );
  43736. setResolvedCache(id, resolved, targetWeb);
  43737. return resolved;
  43738. }
  43739. }
  43740. }
  43741. function tryResolveBrowserMapping(id, importer, options, isFilePath, externalize) {
  43742. let res;
  43743. const pkg = importer && findNearestPackageData(path$n.dirname(importer), options.packageCache);
  43744. if (pkg && isObject$1(pkg.data.browser)) {
  43745. const mapId = isFilePath ? "./" + slash$1(path$n.relative(pkg.dir, id)) : id;
  43746. const browserMappedPath = mapWithBrowserField(mapId, pkg.data.browser);
  43747. if (browserMappedPath) {
  43748. if (res = bareImportRE.test(browserMappedPath) ? tryNodeResolve(browserMappedPath, importer, options, true)?.id : tryFsResolve(path$n.join(pkg.dir, browserMappedPath), options)) {
  43749. debug$c?.(`[browser mapped] ${colors$1.cyan(id)} -> ${colors$1.dim(res)}`);
  43750. let result = { id: res };
  43751. if (options.idOnly) {
  43752. return result;
  43753. }
  43754. if (!options.scan && options.isBuild) {
  43755. const resPkg = findNearestPackageData(
  43756. path$n.dirname(res),
  43757. options.packageCache
  43758. );
  43759. if (resPkg) {
  43760. result = {
  43761. id: res,
  43762. moduleSideEffects: resPkg.hasSideEffects(res)
  43763. };
  43764. }
  43765. }
  43766. return externalize ? { ...result, external: true } : result;
  43767. }
  43768. } else if (browserMappedPath === false) {
  43769. return browserExternalId;
  43770. }
  43771. }
  43772. }
  43773. function tryResolveBrowserEntry(dir, data, options) {
  43774. const browserEntry = typeof data.browser === "string" ? data.browser : isObject$1(data.browser) && data.browser["."];
  43775. if (browserEntry) {
  43776. if (!options.isRequire && options.mainFields.includes("module") && typeof data.module === "string" && data.module !== browserEntry) {
  43777. const resolvedBrowserEntry = tryFsResolve(
  43778. path$n.join(dir, browserEntry),
  43779. options
  43780. );
  43781. if (resolvedBrowserEntry) {
  43782. const content = fs__default.readFileSync(resolvedBrowserEntry, "utf-8");
  43783. if (hasESMSyntax(content)) {
  43784. return browserEntry;
  43785. } else {
  43786. return data.module;
  43787. }
  43788. }
  43789. } else {
  43790. return browserEntry;
  43791. }
  43792. }
  43793. }
  43794. function mapWithBrowserField(relativePathInPkgDir, map) {
  43795. const normalizedPath = path$n.posix.normalize(relativePathInPkgDir);
  43796. for (const key in map) {
  43797. const normalizedKey = path$n.posix.normalize(key);
  43798. if (normalizedPath === normalizedKey || equalWithoutSuffix(normalizedPath, normalizedKey, ".js") || equalWithoutSuffix(normalizedPath, normalizedKey, "/index.js")) {
  43799. return map[key];
  43800. }
  43801. }
  43802. }
  43803. function equalWithoutSuffix(path2, key, suffix) {
  43804. return key.endsWith(suffix) && key.slice(0, -suffix.length) === path2;
  43805. }
  43806. const externalWithConversionNamespace = "vite:dep-pre-bundle:external-conversion";
  43807. const convertedExternalPrefix = "vite-dep-pre-bundle-external:";
  43808. const cjsExternalFacadeNamespace = "vite:cjs-external-facade";
  43809. const nonFacadePrefix = "vite-cjs-external-facade:";
  43810. const externalTypes = [
  43811. "css",
  43812. // supported pre-processor types
  43813. "less",
  43814. "sass",
  43815. "scss",
  43816. "styl",
  43817. "stylus",
  43818. "pcss",
  43819. "postcss",
  43820. // wasm
  43821. "wasm",
  43822. // known SFC types
  43823. "vue",
  43824. "svelte",
  43825. "marko",
  43826. "astro",
  43827. "imba",
  43828. // JSX/TSX may be configured to be compiled differently from how esbuild
  43829. // handles it by default, so exclude them as well
  43830. "jsx",
  43831. "tsx",
  43832. ...KNOWN_ASSET_TYPES
  43833. ];
  43834. function esbuildDepPlugin(qualified, external, config, ssr) {
  43835. const { extensions } = getDepOptimizationConfig(config, ssr);
  43836. const allExternalTypes = extensions ? externalTypes.filter((type) => !extensions?.includes("." + type)) : externalTypes;
  43837. const esmPackageCache = /* @__PURE__ */ new Map();
  43838. const cjsPackageCache = /* @__PURE__ */ new Map();
  43839. const _resolve = config.createResolver({
  43840. asSrc: false,
  43841. scan: true,
  43842. packageCache: esmPackageCache
  43843. });
  43844. const _resolveRequire = config.createResolver({
  43845. asSrc: false,
  43846. isRequire: true,
  43847. scan: true,
  43848. packageCache: cjsPackageCache
  43849. });
  43850. const resolve = (id, importer, kind, resolveDir) => {
  43851. let _importer;
  43852. {
  43853. _importer = importer in qualified ? qualified[importer] : importer;
  43854. }
  43855. const resolver = kind.startsWith("require") ? _resolveRequire : _resolve;
  43856. return resolver(id, _importer, void 0, ssr);
  43857. };
  43858. const resolveResult = (id, resolved) => {
  43859. if (resolved.startsWith(browserExternalId)) {
  43860. return {
  43861. path: id,
  43862. namespace: "browser-external"
  43863. };
  43864. }
  43865. if (resolved.startsWith(optionalPeerDepId)) {
  43866. return {
  43867. path: resolved,
  43868. namespace: "optional-peer-dep"
  43869. };
  43870. }
  43871. if (ssr && isBuiltin(resolved)) {
  43872. return;
  43873. }
  43874. if (isExternalUrl(resolved)) {
  43875. return {
  43876. path: resolved,
  43877. external: true
  43878. };
  43879. }
  43880. return {
  43881. path: path$n.resolve(resolved)
  43882. };
  43883. };
  43884. return {
  43885. name: "vite:dep-pre-bundle",
  43886. setup(build) {
  43887. build.onEnd(() => {
  43888. esmPackageCache.clear();
  43889. cjsPackageCache.clear();
  43890. });
  43891. build.onResolve(
  43892. {
  43893. filter: new RegExp(
  43894. `\\.(` + allExternalTypes.join("|") + `)(\\?.*)?$`
  43895. )
  43896. },
  43897. async ({ path: id, importer, kind }) => {
  43898. if (id.startsWith(convertedExternalPrefix)) {
  43899. return {
  43900. path: id.slice(convertedExternalPrefix.length),
  43901. external: true
  43902. };
  43903. }
  43904. const resolved = await resolve(id, importer, kind);
  43905. if (resolved) {
  43906. if (kind === "require-call") {
  43907. if (resolved.endsWith(".js")) {
  43908. return {
  43909. path: resolved,
  43910. external: false
  43911. };
  43912. }
  43913. return {
  43914. path: resolved,
  43915. namespace: externalWithConversionNamespace
  43916. };
  43917. }
  43918. return {
  43919. path: resolved,
  43920. external: true
  43921. };
  43922. }
  43923. }
  43924. );
  43925. build.onLoad(
  43926. { filter: /./, namespace: externalWithConversionNamespace },
  43927. (args) => {
  43928. const modulePath = `"${convertedExternalPrefix}${args.path}"`;
  43929. return {
  43930. contents: isCSSRequest(args.path) && !isModuleCSSRequest(args.path) ? `import ${modulePath};` : `export { default } from ${modulePath};export * from ${modulePath};`,
  43931. loader: "js"
  43932. };
  43933. }
  43934. );
  43935. function resolveEntry(id) {
  43936. const flatId = flattenId(id);
  43937. if (flatId in qualified) {
  43938. return {
  43939. path: qualified[flatId]
  43940. };
  43941. }
  43942. }
  43943. build.onResolve(
  43944. { filter: /^[\w@][^:]/ },
  43945. async ({ path: id, importer, kind }) => {
  43946. if (moduleListContains(external, id)) {
  43947. return {
  43948. path: id,
  43949. external: true
  43950. };
  43951. }
  43952. let entry;
  43953. if (!importer) {
  43954. if (entry = resolveEntry(id)) return entry;
  43955. const aliased = await _resolve(id, void 0, true);
  43956. if (aliased && (entry = resolveEntry(aliased))) {
  43957. return entry;
  43958. }
  43959. }
  43960. const resolved = await resolve(id, importer, kind);
  43961. if (resolved) {
  43962. return resolveResult(id, resolved);
  43963. }
  43964. }
  43965. );
  43966. build.onLoad(
  43967. { filter: /.*/, namespace: "browser-external" },
  43968. ({ path: path2 }) => {
  43969. if (config.isProduction) {
  43970. return {
  43971. contents: "module.exports = {}"
  43972. };
  43973. } else {
  43974. return {
  43975. // Return in CJS to intercept named imports. Use `Object.create` to
  43976. // create the Proxy in the prototype to workaround esbuild issue. Why?
  43977. //
  43978. // In short, esbuild cjs->esm flow:
  43979. // 1. Create empty object using `Object.create(Object.getPrototypeOf(module.exports))`.
  43980. // 2. Assign props of `module.exports` to the object.
  43981. // 3. Return object for ESM use.
  43982. //
  43983. // If we do `module.exports = new Proxy({}, {})`, step 1 returns empty object,
  43984. // step 2 does nothing as there's no props for `module.exports`. The final object
  43985. // is just an empty object.
  43986. //
  43987. // Creating the Proxy in the prototype satisfies step 1 immediately, which means
  43988. // the returned object is a Proxy that we can intercept.
  43989. //
  43990. // Note: Skip keys that are accessed by esbuild and browser devtools.
  43991. contents: `module.exports = Object.create(new Proxy({}, {
  43992. get(_, key) {
  43993. if (
  43994. key !== '__esModule' &&
  43995. key !== '__proto__' &&
  43996. key !== 'constructor' &&
  43997. key !== 'splice'
  43998. ) {
  43999. console.warn(\`Module "${path2}" has been externalized for browser compatibility. Cannot access "${path2}.\${key}" in client code. See https://vite.dev/guide/troubleshooting.html#module-externalized-for-browser-compatibility for more details.\`)
  44000. }
  44001. }
  44002. }))`
  44003. };
  44004. }
  44005. }
  44006. );
  44007. build.onLoad(
  44008. { filter: /.*/, namespace: "optional-peer-dep" },
  44009. ({ path: path2 }) => {
  44010. if (config.isProduction) {
  44011. return {
  44012. contents: "module.exports = {}"
  44013. };
  44014. } else {
  44015. const [, peerDep, parentDep] = path2.split(":");
  44016. return {
  44017. contents: `throw new Error(\`Could not resolve "${peerDep}" imported by "${parentDep}". Is it installed?\`)`
  44018. };
  44019. }
  44020. }
  44021. );
  44022. }
  44023. };
  44024. }
  44025. const matchesEntireLine = (text) => `^${escapeRegex(text)}$`;
  44026. function esbuildCjsExternalPlugin(externals, platform) {
  44027. return {
  44028. name: "cjs-external",
  44029. setup(build) {
  44030. const filter = new RegExp(externals.map(matchesEntireLine).join("|"));
  44031. build.onResolve({ filter: new RegExp(`^${nonFacadePrefix}`) }, (args) => {
  44032. return {
  44033. path: args.path.slice(nonFacadePrefix.length),
  44034. external: true
  44035. };
  44036. });
  44037. build.onResolve({ filter }, (args) => {
  44038. if (args.kind === "require-call" && platform !== "node") {
  44039. return {
  44040. path: args.path,
  44041. namespace: cjsExternalFacadeNamespace
  44042. };
  44043. }
  44044. return {
  44045. path: args.path,
  44046. external: true
  44047. };
  44048. });
  44049. build.onLoad(
  44050. { filter: /.*/, namespace: cjsExternalFacadeNamespace },
  44051. (args) => ({
  44052. contents: `import * as m from ${JSON.stringify(
  44053. nonFacadePrefix + args.path
  44054. )};module.exports = m;`
  44055. })
  44056. );
  44057. }
  44058. };
  44059. }
  44060. const debug$b = createDebugger("vite:ssr-external");
  44061. const isSsrExternalCache = /* @__PURE__ */ new WeakMap();
  44062. function shouldExternalizeForSSR(id, importer, config) {
  44063. let isSsrExternal = isSsrExternalCache.get(config);
  44064. if (!isSsrExternal) {
  44065. isSsrExternal = createIsSsrExternal(config);
  44066. isSsrExternalCache.set(config, isSsrExternal);
  44067. }
  44068. return isSsrExternal(id, importer);
  44069. }
  44070. function createIsConfiguredAsSsrExternal(config) {
  44071. const { ssr, root } = config;
  44072. const noExternal = ssr?.noExternal;
  44073. const noExternalFilter = noExternal !== "undefined" && typeof noExternal !== "boolean" && createFilter(void 0, noExternal, { resolve: false });
  44074. const targetConditions = config.ssr.resolve?.externalConditions || [];
  44075. const resolveOptions = {
  44076. ...config.resolve,
  44077. root,
  44078. isProduction: false,
  44079. isBuild: true,
  44080. conditions: targetConditions
  44081. };
  44082. const isExternalizable = (id, importer, configuredAsExternal) => {
  44083. if (!bareImportRE.test(id) || id.includes("\0")) {
  44084. return false;
  44085. }
  44086. try {
  44087. return !!tryNodeResolve(
  44088. id,
  44089. // Skip passing importer in build to avoid externalizing non-hoisted dependencies
  44090. // unresolvable from root (which would be unresolvable from output bundles also)
  44091. config.command === "build" ? void 0 : importer,
  44092. resolveOptions,
  44093. ssr?.target === "webworker",
  44094. void 0,
  44095. true,
  44096. // try to externalize, will return undefined or an object without
  44097. // a external flag if it isn't externalizable
  44098. true,
  44099. // Allow linked packages to be externalized if they are explicitly
  44100. // configured as external
  44101. !!configuredAsExternal
  44102. )?.external;
  44103. } catch (e) {
  44104. debug$b?.(
  44105. `Failed to node resolve "${id}". Skipping externalizing it by default.`
  44106. );
  44107. return false;
  44108. }
  44109. };
  44110. return (id, importer) => {
  44111. if (
  44112. // If this id is defined as external, force it as external
  44113. // Note that individual package entries are allowed in ssr.external
  44114. ssr.external !== true && ssr.external?.includes(id)
  44115. ) {
  44116. return true;
  44117. }
  44118. const pkgName = getNpmPackageName(id);
  44119. if (!pkgName) {
  44120. return isExternalizable(id, importer);
  44121. }
  44122. if (
  44123. // A package name in ssr.external externalizes every
  44124. // externalizable package entry
  44125. ssr.external !== true && ssr.external?.includes(pkgName)
  44126. ) {
  44127. return isExternalizable(id, importer, true);
  44128. }
  44129. if (typeof noExternal === "boolean") {
  44130. return !noExternal;
  44131. }
  44132. if (noExternalFilter && !noExternalFilter(pkgName)) {
  44133. return false;
  44134. }
  44135. return isExternalizable(id, importer, ssr.external === true);
  44136. };
  44137. }
  44138. function createIsSsrExternal(config) {
  44139. const processedIds = /* @__PURE__ */ new Map();
  44140. const isConfiguredAsExternal = createIsConfiguredAsSsrExternal(config);
  44141. return (id, importer) => {
  44142. if (processedIds.has(id)) {
  44143. return processedIds.get(id);
  44144. }
  44145. let external = false;
  44146. if (id[0] !== "." && !path$n.isAbsolute(id)) {
  44147. external = isBuiltin(id) || isConfiguredAsExternal(id, importer);
  44148. }
  44149. processedIds.set(id, external);
  44150. return external;
  44151. };
  44152. }
  44153. const jsonExtRE = /\.json(?:$|\?)(?!commonjs-(?:proxy|external))/;
  44154. const jsonLangs = `\\.(?:json|json5)(?:$|\\?)`;
  44155. const jsonLangRE = new RegExp(jsonLangs);
  44156. const isJSONRequest = (request) => jsonLangRE.test(request);
  44157. function jsonPlugin(options = {}, isBuild) {
  44158. return {
  44159. name: "vite:json",
  44160. transform(json, id) {
  44161. if (!jsonExtRE.test(id)) return null;
  44162. if (SPECIAL_QUERY_RE.test(id)) return null;
  44163. json = stripBomTag(json);
  44164. try {
  44165. if (options.stringify) {
  44166. if (isBuild) {
  44167. return {
  44168. // during build, parse then double-stringify to remove all
  44169. // unnecessary whitespaces to reduce bundle size.
  44170. code: `export default JSON.parse(${JSON.stringify(
  44171. JSON.stringify(JSON.parse(json))
  44172. )})`,
  44173. map: { mappings: "" }
  44174. };
  44175. } else {
  44176. return `export default JSON.parse(${JSON.stringify(json)})`;
  44177. }
  44178. }
  44179. const parsed = JSON.parse(json);
  44180. return {
  44181. code: dataToEsm(parsed, {
  44182. preferConst: true,
  44183. namedExports: options.namedExports
  44184. }),
  44185. map: { mappings: "" }
  44186. };
  44187. } catch (e) {
  44188. const position = extractJsonErrorPosition(e.message, json.length);
  44189. const msg = position ? `, invalid JSON syntax found at position ${position}` : `.`;
  44190. this.error(`Failed to parse JSON file` + msg, position);
  44191. }
  44192. }
  44193. };
  44194. }
  44195. function extractJsonErrorPosition(errorMessage, inputLength) {
  44196. if (errorMessage.startsWith("Unexpected end of JSON input")) {
  44197. return inputLength - 1;
  44198. }
  44199. const errorMessageList = /at position (\d+)/.exec(errorMessage);
  44200. return errorMessageList ? Math.max(parseInt(errorMessageList[1], 10) - 1, 0) : void 0;
  44201. }
  44202. const ERR_OPTIMIZE_DEPS_PROCESSING_ERROR = "ERR_OPTIMIZE_DEPS_PROCESSING_ERROR";
  44203. const ERR_OUTDATED_OPTIMIZED_DEP = "ERR_OUTDATED_OPTIMIZED_DEP";
  44204. const ERR_FILE_NOT_FOUND_IN_OPTIMIZED_DEP_DIR = "ERR_FILE_NOT_FOUND_IN_OPTIMIZED_DEP_DIR";
  44205. const debug$a = createDebugger("vite:optimize-deps");
  44206. function optimizedDepsPlugin(config) {
  44207. return {
  44208. name: "vite:optimized-deps",
  44209. resolveId(id, source, { ssr }) {
  44210. if (getDepsOptimizer(config, ssr)?.isOptimizedDepFile(id)) {
  44211. return id;
  44212. }
  44213. },
  44214. // this.load({ id }) isn't implemented in PluginContainer
  44215. // The logic to register an id to wait until it is processed
  44216. // is in importAnalysis, see call to delayDepsOptimizerUntil
  44217. async load(id, options) {
  44218. const ssr = options?.ssr === true;
  44219. const depsOptimizer = getDepsOptimizer(config, ssr);
  44220. if (depsOptimizer?.isOptimizedDepFile(id)) {
  44221. const metadata = depsOptimizer.metadata;
  44222. const file = cleanUrl(id);
  44223. const versionMatch = DEP_VERSION_RE.exec(file);
  44224. const browserHash = versionMatch ? versionMatch[1].split("=")[1] : void 0;
  44225. const info = optimizedDepInfoFromFile(metadata, file);
  44226. if (info) {
  44227. if (browserHash && info.browserHash !== browserHash) {
  44228. throwOutdatedRequest(id);
  44229. }
  44230. try {
  44231. await info.processing;
  44232. } catch {
  44233. throwProcessingError(id);
  44234. }
  44235. const newMetadata = depsOptimizer.metadata;
  44236. if (metadata !== newMetadata) {
  44237. const currentInfo = optimizedDepInfoFromFile(newMetadata, file);
  44238. if (info.browserHash !== currentInfo?.browserHash) {
  44239. throwOutdatedRequest(id);
  44240. }
  44241. }
  44242. }
  44243. debug$a?.(`load ${colors$1.cyan(file)}`);
  44244. try {
  44245. return await fsp.readFile(file, "utf-8");
  44246. } catch (e) {
  44247. const newMetadata = depsOptimizer.metadata;
  44248. if (optimizedDepInfoFromFile(newMetadata, file)) {
  44249. throwOutdatedRequest(id);
  44250. }
  44251. throwFileNotFoundInOptimizedDep(id);
  44252. }
  44253. }
  44254. }
  44255. };
  44256. }
  44257. function throwProcessingError(id) {
  44258. const err = new Error(
  44259. `Something unexpected happened while optimizing "${id}". The current page should have reloaded by now`
  44260. );
  44261. err.code = ERR_OPTIMIZE_DEPS_PROCESSING_ERROR;
  44262. throw err;
  44263. }
  44264. function throwOutdatedRequest(id) {
  44265. const err = new Error(
  44266. `There is a new version of the pre-bundle for "${id}", a page reload is going to ask for it.`
  44267. );
  44268. err.code = ERR_OUTDATED_OPTIMIZED_DEP;
  44269. throw err;
  44270. }
  44271. function throwFileNotFoundInOptimizedDep(id) {
  44272. const err = new Error(
  44273. `The file does not exist at "${id}" which is in the optimize deps directory. The dependency might be incompatible with the dep optimizer. Try adding it to \`optimizeDeps.exclude\`.`
  44274. );
  44275. err.code = ERR_FILE_NOT_FOUND_IN_OPTIMIZED_DEP_DIR;
  44276. throw err;
  44277. }
  44278. const nonJsRe = /\.json(?:$|\?)/;
  44279. const isNonJsRequest = (request) => nonJsRe.test(request);
  44280. const importMetaEnvMarker = "__vite_import_meta_env__";
  44281. const importMetaEnvKeyReCache = /* @__PURE__ */ new Map();
  44282. function definePlugin(config) {
  44283. const isBuild = config.command === "build";
  44284. const isBuildLib = isBuild && config.build.lib;
  44285. const processEnv = {};
  44286. if (!isBuildLib) {
  44287. const nodeEnv = process.env.NODE_ENV || config.mode;
  44288. Object.assign(processEnv, {
  44289. "process.env": `{}`,
  44290. "global.process.env": `{}`,
  44291. "globalThis.process.env": `{}`,
  44292. "process.env.NODE_ENV": JSON.stringify(nodeEnv),
  44293. "global.process.env.NODE_ENV": JSON.stringify(nodeEnv),
  44294. "globalThis.process.env.NODE_ENV": JSON.stringify(nodeEnv)
  44295. });
  44296. }
  44297. const importMetaKeys = {};
  44298. const importMetaEnvKeys = {};
  44299. const importMetaFallbackKeys = {};
  44300. if (isBuild) {
  44301. importMetaKeys["import.meta.hot"] = `undefined`;
  44302. for (const key in config.env) {
  44303. const val = JSON.stringify(config.env[key]);
  44304. importMetaKeys[`import.meta.env.${key}`] = val;
  44305. importMetaEnvKeys[key] = val;
  44306. }
  44307. importMetaKeys["import.meta.env.SSR"] = `undefined`;
  44308. importMetaFallbackKeys["import.meta.env"] = `undefined`;
  44309. }
  44310. const userDefine = {};
  44311. const userDefineEnv = {};
  44312. for (const key in config.define) {
  44313. userDefine[key] = handleDefineValue(config.define[key]);
  44314. if (isBuild && key.startsWith("import.meta.env.")) {
  44315. userDefineEnv[key.slice(16)] = config.define[key];
  44316. }
  44317. }
  44318. function generatePattern(ssr) {
  44319. const replaceProcessEnv = !ssr || config.ssr?.target === "webworker";
  44320. const define = {
  44321. ...replaceProcessEnv ? processEnv : {},
  44322. ...importMetaKeys,
  44323. ...userDefine,
  44324. ...importMetaFallbackKeys
  44325. };
  44326. if ("import.meta.env.SSR" in define) {
  44327. define["import.meta.env.SSR"] = ssr + "";
  44328. }
  44329. if ("import.meta.env" in define) {
  44330. define["import.meta.env"] = importMetaEnvMarker;
  44331. }
  44332. const importMetaEnvVal = serializeDefine({
  44333. ...importMetaEnvKeys,
  44334. SSR: ssr + "",
  44335. ...userDefineEnv
  44336. });
  44337. const patternKeys = Object.keys(userDefine);
  44338. if (replaceProcessEnv && Object.keys(processEnv).length) {
  44339. patternKeys.push("process.env");
  44340. }
  44341. if (Object.keys(importMetaKeys).length) {
  44342. patternKeys.push("import.meta.env", "import.meta.hot");
  44343. }
  44344. const pattern = patternKeys.length ? new RegExp(patternKeys.map(escapeRegex).join("|")) : null;
  44345. return [define, pattern, importMetaEnvVal];
  44346. }
  44347. const defaultPattern = generatePattern(false);
  44348. const ssrPattern = generatePattern(true);
  44349. return {
  44350. name: "vite:define",
  44351. async transform(code, id, options) {
  44352. const ssr = options?.ssr === true;
  44353. if (!ssr && !isBuild) {
  44354. return;
  44355. }
  44356. if (
  44357. // exclude html, css and static assets for performance
  44358. isHTMLRequest(id) || isCSSRequest(id) || isNonJsRequest(id) || config.assetsInclude(id)
  44359. ) {
  44360. return;
  44361. }
  44362. let [define, pattern, importMetaEnvVal] = ssr ? ssrPattern : defaultPattern;
  44363. if (!pattern) return;
  44364. pattern.lastIndex = 0;
  44365. if (!pattern.test(code)) return;
  44366. const hasDefineImportMetaEnv = "import.meta.env" in define;
  44367. let marker = importMetaEnvMarker;
  44368. if (hasDefineImportMetaEnv && code.includes(marker)) {
  44369. let i = 1;
  44370. do {
  44371. marker = importMetaEnvMarker + i++;
  44372. } while (code.includes(marker));
  44373. if (marker !== importMetaEnvMarker) {
  44374. define = { ...define, "import.meta.env": marker };
  44375. }
  44376. }
  44377. const result = await replaceDefine(code, id, define, config);
  44378. if (hasDefineImportMetaEnv) {
  44379. result.code = result.code.replaceAll(
  44380. getImportMetaEnvKeyRe(marker),
  44381. (m) => "undefined".padEnd(m.length)
  44382. );
  44383. if (result.code.includes(marker)) {
  44384. result.code = `const ${marker} = ${importMetaEnvVal};
  44385. ` + result.code;
  44386. if (result.map) {
  44387. const map = JSON.parse(result.map);
  44388. map.mappings = ";" + map.mappings;
  44389. result.map = map;
  44390. }
  44391. }
  44392. }
  44393. return result;
  44394. }
  44395. };
  44396. }
  44397. async function replaceDefine(code, id, define, config) {
  44398. const esbuildOptions = config.esbuild || {};
  44399. const result = await transform$1(code, {
  44400. loader: "js",
  44401. charset: esbuildOptions.charset ?? "utf8",
  44402. platform: "neutral",
  44403. define,
  44404. sourcefile: id,
  44405. sourcemap: config.command === "build" ? !!config.build.sourcemap : true
  44406. });
  44407. if (result.map.includes("<define:")) {
  44408. const originalMap = new TraceMap(result.map);
  44409. if (originalMap.sources.length >= 2) {
  44410. const sourceIndex = originalMap.sources.indexOf(id);
  44411. const decoded = decodedMap(originalMap);
  44412. decoded.sources = [id];
  44413. decoded.mappings = decoded.mappings.map(
  44414. (segments) => segments.filter((segment) => {
  44415. const index = segment[1];
  44416. segment[1] = 0;
  44417. return index === sourceIndex;
  44418. })
  44419. );
  44420. result.map = JSON.stringify(encodedMap(new TraceMap(decoded)));
  44421. }
  44422. }
  44423. return {
  44424. code: result.code,
  44425. map: result.map || null
  44426. };
  44427. }
  44428. function serializeDefine(define) {
  44429. let res = `{`;
  44430. const keys = Object.keys(define).sort();
  44431. for (let i = 0; i < keys.length; i++) {
  44432. const key = keys[i];
  44433. const val = define[key];
  44434. res += `${JSON.stringify(key)}: ${handleDefineValue(val)}`;
  44435. if (i !== keys.length - 1) {
  44436. res += `, `;
  44437. }
  44438. }
  44439. return res + `}`;
  44440. }
  44441. function handleDefineValue(value) {
  44442. if (typeof value === "undefined") return "undefined";
  44443. if (typeof value === "string") return value;
  44444. return JSON.stringify(value);
  44445. }
  44446. function getImportMetaEnvKeyRe(marker) {
  44447. let re = importMetaEnvKeyReCache.get(marker);
  44448. if (!re) {
  44449. re = new RegExp(`${marker}\\..+?\\b`, "g");
  44450. importMetaEnvKeyReCache.set(marker, re);
  44451. }
  44452. return re;
  44453. }
  44454. const normalizedClientEntry = normalizePath$3(CLIENT_ENTRY);
  44455. const normalizedEnvEntry = normalizePath$3(ENV_ENTRY);
  44456. function clientInjectionsPlugin(config) {
  44457. let injectConfigValues;
  44458. return {
  44459. name: "vite:client-inject",
  44460. async buildStart() {
  44461. const resolvedServerHostname = (await resolveHostname(config.server.host)).name;
  44462. const resolvedServerPort = config.server.port;
  44463. const devBase = config.base;
  44464. const serverHost = `${resolvedServerHostname}:${resolvedServerPort}${devBase}`;
  44465. let hmrConfig = config.server.hmr;
  44466. hmrConfig = isObject$1(hmrConfig) ? hmrConfig : void 0;
  44467. const host = hmrConfig?.host || null;
  44468. const protocol = hmrConfig?.protocol || null;
  44469. const timeout = hmrConfig?.timeout || 3e4;
  44470. const overlay = hmrConfig?.overlay !== false;
  44471. const isHmrServerSpecified = !!hmrConfig?.server;
  44472. const hmrConfigName = path$n.basename(config.configFile || "vite.config.js");
  44473. let port = hmrConfig?.clientPort || hmrConfig?.port || null;
  44474. if (config.server.middlewareMode && !isHmrServerSpecified) {
  44475. port ||= 24678;
  44476. }
  44477. let directTarget = hmrConfig?.host || resolvedServerHostname;
  44478. directTarget += `:${hmrConfig?.port || resolvedServerPort}`;
  44479. directTarget += devBase;
  44480. let hmrBase = devBase;
  44481. if (hmrConfig?.path) {
  44482. hmrBase = path$n.posix.join(hmrBase, hmrConfig.path);
  44483. }
  44484. const userDefine = {};
  44485. for (const key in config.define) {
  44486. if (!key.startsWith("import.meta.env.")) {
  44487. userDefine[key] = config.define[key];
  44488. }
  44489. }
  44490. const serializedDefines = serializeDefine(userDefine);
  44491. const modeReplacement = escapeReplacement(config.mode);
  44492. const baseReplacement = escapeReplacement(devBase);
  44493. const definesReplacement = () => serializedDefines;
  44494. const serverHostReplacement = escapeReplacement(serverHost);
  44495. const hmrProtocolReplacement = escapeReplacement(protocol);
  44496. const hmrHostnameReplacement = escapeReplacement(host);
  44497. const hmrPortReplacement = escapeReplacement(port);
  44498. const hmrDirectTargetReplacement = escapeReplacement(directTarget);
  44499. const hmrBaseReplacement = escapeReplacement(hmrBase);
  44500. const hmrTimeoutReplacement = escapeReplacement(timeout);
  44501. const hmrEnableOverlayReplacement = escapeReplacement(overlay);
  44502. const hmrConfigNameReplacement = escapeReplacement(hmrConfigName);
  44503. injectConfigValues = (code) => {
  44504. return code.replace(`__MODE__`, modeReplacement).replace(/__BASE__/g, baseReplacement).replace(`__DEFINES__`, definesReplacement).replace(`__SERVER_HOST__`, serverHostReplacement).replace(`__HMR_PROTOCOL__`, hmrProtocolReplacement).replace(`__HMR_HOSTNAME__`, hmrHostnameReplacement).replace(`__HMR_PORT__`, hmrPortReplacement).replace(`__HMR_DIRECT_TARGET__`, hmrDirectTargetReplacement).replace(`__HMR_BASE__`, hmrBaseReplacement).replace(`__HMR_TIMEOUT__`, hmrTimeoutReplacement).replace(`__HMR_ENABLE_OVERLAY__`, hmrEnableOverlayReplacement).replace(`__HMR_CONFIG_NAME__`, hmrConfigNameReplacement);
  44505. };
  44506. },
  44507. async transform(code, id, options) {
  44508. if (id === normalizedClientEntry || id === normalizedEnvEntry) {
  44509. return injectConfigValues(code);
  44510. } else if (!options?.ssr && code.includes("process.env.NODE_ENV")) {
  44511. const nodeEnv = config.define?.["process.env.NODE_ENV"] || JSON.stringify(process.env.NODE_ENV || config.mode);
  44512. return await replaceDefine(
  44513. code,
  44514. id,
  44515. {
  44516. "process.env.NODE_ENV": nodeEnv,
  44517. "global.process.env.NODE_ENV": nodeEnv,
  44518. "globalThis.process.env.NODE_ENV": nodeEnv
  44519. },
  44520. config
  44521. );
  44522. }
  44523. }
  44524. };
  44525. }
  44526. function escapeReplacement(value) {
  44527. const jsonValue = JSON.stringify(value);
  44528. return () => jsonValue;
  44529. }
  44530. const wasmHelperId = "\0vite/wasm-helper.js";
  44531. const wasmHelper = async (opts = {}, url) => {
  44532. let result;
  44533. if (url.startsWith("data:")) {
  44534. const urlContent = url.replace(/^data:.*?base64,/, "");
  44535. let bytes;
  44536. if (typeof Buffer === "function" && typeof Buffer.from === "function") {
  44537. bytes = Buffer.from(urlContent, "base64");
  44538. } else if (typeof atob === "function") {
  44539. const binaryString = atob(urlContent);
  44540. bytes = new Uint8Array(binaryString.length);
  44541. for (let i = 0; i < binaryString.length; i++) {
  44542. bytes[i] = binaryString.charCodeAt(i);
  44543. }
  44544. } else {
  44545. throw new Error(
  44546. "Failed to decode base64-encoded data URL, Buffer and atob are not supported"
  44547. );
  44548. }
  44549. result = await WebAssembly.instantiate(bytes, opts);
  44550. } else {
  44551. const response = await fetch(url);
  44552. const contentType = response.headers.get("Content-Type") || "";
  44553. if ("instantiateStreaming" in WebAssembly && contentType.startsWith("application/wasm")) {
  44554. result = await WebAssembly.instantiateStreaming(response, opts);
  44555. } else {
  44556. const buffer = await response.arrayBuffer();
  44557. result = await WebAssembly.instantiate(buffer, opts);
  44558. }
  44559. }
  44560. return result.instance;
  44561. };
  44562. const wasmHelperCode = wasmHelper.toString();
  44563. const wasmHelperPlugin = (config) => {
  44564. return {
  44565. name: "vite:wasm-helper",
  44566. resolveId(id) {
  44567. if (id === wasmHelperId) {
  44568. return id;
  44569. }
  44570. },
  44571. async load(id) {
  44572. if (id === wasmHelperId) {
  44573. return `export default ${wasmHelperCode}`;
  44574. }
  44575. if (!id.endsWith(".wasm?init")) {
  44576. return;
  44577. }
  44578. const url = await fileToUrl$1(id, config, this);
  44579. return `
  44580. import initWasm from "${wasmHelperId}"
  44581. export default opts => initWasm(opts, ${JSON.stringify(url)})
  44582. `;
  44583. }
  44584. };
  44585. };
  44586. const wasmFallbackPlugin = () => {
  44587. return {
  44588. name: "vite:wasm-fallback",
  44589. async load(id) {
  44590. if (!id.endsWith(".wasm")) {
  44591. return;
  44592. }
  44593. throw new Error(
  44594. '"ESM integration proposal for Wasm" is not supported currently. Use vite-plugin-wasm or other community plugins to handle this. Alternatively, you can use `.wasm?init` or `.wasm?url`. See https://vite.dev/guide/features.html#webassembly for more details.'
  44595. );
  44596. }
  44597. };
  44598. };
  44599. const workerOrSharedWorkerRE = /(?:\?|&)(worker|sharedworker)(?:&|$)/;
  44600. const workerFileRE = /(?:\?|&)worker_file&type=(\w+)(?:&|$)/;
  44601. const inlineRE = /[?&]inline\b/;
  44602. const WORKER_FILE_ID = "worker_file";
  44603. const workerCache = /* @__PURE__ */ new WeakMap();
  44604. function saveEmitWorkerAsset(config, asset) {
  44605. const workerMap = workerCache.get(config.mainConfig || config);
  44606. workerMap.assets.set(asset.fileName, asset);
  44607. }
  44608. async function bundleWorkerEntry(config, id) {
  44609. const input = cleanUrl(id);
  44610. const newBundleChain = [...config.bundleChain, input];
  44611. if (config.bundleChain.includes(input)) {
  44612. throw new Error(
  44613. `Circular worker imports detected. Vite does not support it. Import chain: ${newBundleChain.map((id2) => prettifyUrl(id2, config.root)).join(" -> ")}`
  44614. );
  44615. }
  44616. const { rollup } = await import('rollup');
  44617. const { plugins, rollupOptions, format } = config.worker;
  44618. const bundle = await rollup({
  44619. ...rollupOptions,
  44620. input,
  44621. plugins: await plugins(newBundleChain),
  44622. onwarn(warning, warn) {
  44623. onRollupWarning(warning, warn, config);
  44624. },
  44625. preserveEntrySignatures: false
  44626. });
  44627. let chunk;
  44628. try {
  44629. const workerOutputConfig = config.worker.rollupOptions.output;
  44630. const workerConfig = workerOutputConfig ? Array.isArray(workerOutputConfig) ? workerOutputConfig[0] || {} : workerOutputConfig : {};
  44631. const {
  44632. output: [outputChunk, ...outputChunks]
  44633. } = await bundle.generate({
  44634. entryFileNames: path$n.posix.join(
  44635. config.build.assetsDir,
  44636. "[name]-[hash].js"
  44637. ),
  44638. chunkFileNames: path$n.posix.join(
  44639. config.build.assetsDir,
  44640. "[name]-[hash].js"
  44641. ),
  44642. assetFileNames: path$n.posix.join(
  44643. config.build.assetsDir,
  44644. "[name]-[hash].[ext]"
  44645. ),
  44646. ...workerConfig,
  44647. format,
  44648. sourcemap: config.build.sourcemap
  44649. });
  44650. chunk = outputChunk;
  44651. outputChunks.forEach((outputChunk2) => {
  44652. if (outputChunk2.type === "asset") {
  44653. saveEmitWorkerAsset(config, outputChunk2);
  44654. } else if (outputChunk2.type === "chunk") {
  44655. saveEmitWorkerAsset(config, {
  44656. fileName: outputChunk2.fileName,
  44657. originalFileName: null,
  44658. source: outputChunk2.code
  44659. });
  44660. }
  44661. });
  44662. } finally {
  44663. await bundle.close();
  44664. }
  44665. return emitSourcemapForWorkerEntry(config, chunk);
  44666. }
  44667. function emitSourcemapForWorkerEntry(config, chunk) {
  44668. const { map: sourcemap } = chunk;
  44669. if (sourcemap) {
  44670. if (config.build.sourcemap === "hidden" || config.build.sourcemap === true) {
  44671. const data = sourcemap.toString();
  44672. const mapFileName = chunk.fileName + ".map";
  44673. saveEmitWorkerAsset(config, {
  44674. fileName: mapFileName,
  44675. originalFileName: null,
  44676. source: data
  44677. });
  44678. }
  44679. }
  44680. return chunk;
  44681. }
  44682. const workerAssetUrlRE = /__VITE_WORKER_ASSET__([a-z\d]{8})__/g;
  44683. function encodeWorkerAssetFileName(fileName, workerCache2) {
  44684. const { fileNameHash } = workerCache2;
  44685. const hash = getHash(fileName);
  44686. if (!fileNameHash.get(hash)) {
  44687. fileNameHash.set(hash, fileName);
  44688. }
  44689. return `__VITE_WORKER_ASSET__${hash}__`;
  44690. }
  44691. async function workerFileToUrl(config, id) {
  44692. const workerMap = workerCache.get(config.mainConfig || config);
  44693. let fileName = workerMap.bundle.get(id);
  44694. if (!fileName) {
  44695. const outputChunk = await bundleWorkerEntry(config, id);
  44696. fileName = outputChunk.fileName;
  44697. saveEmitWorkerAsset(config, {
  44698. fileName,
  44699. originalFileName: null,
  44700. source: outputChunk.code
  44701. });
  44702. workerMap.bundle.set(id, fileName);
  44703. }
  44704. return encodeWorkerAssetFileName(fileName, workerMap);
  44705. }
  44706. function webWorkerPostPlugin() {
  44707. return {
  44708. name: "vite:worker-post",
  44709. resolveImportMeta(property, { format }) {
  44710. if (format === "iife") {
  44711. if (!property) {
  44712. return `{
  44713. url: self.location.href
  44714. }`;
  44715. }
  44716. if (property === "url") {
  44717. return "self.location.href";
  44718. }
  44719. }
  44720. return null;
  44721. }
  44722. };
  44723. }
  44724. function webWorkerPlugin(config) {
  44725. const isBuild = config.command === "build";
  44726. let server;
  44727. const isWorker = config.isWorker;
  44728. return {
  44729. name: "vite:worker",
  44730. configureServer(_server) {
  44731. server = _server;
  44732. },
  44733. buildStart() {
  44734. if (isWorker) {
  44735. return;
  44736. }
  44737. workerCache.set(config, {
  44738. assets: /* @__PURE__ */ new Map(),
  44739. bundle: /* @__PURE__ */ new Map(),
  44740. fileNameHash: /* @__PURE__ */ new Map()
  44741. });
  44742. },
  44743. load(id) {
  44744. if (isBuild && workerOrSharedWorkerRE.test(id)) {
  44745. return "";
  44746. }
  44747. },
  44748. shouldTransformCachedModule({ id }) {
  44749. if (isBuild && config.build.watch && workerOrSharedWorkerRE.test(id)) {
  44750. return true;
  44751. }
  44752. },
  44753. async transform(raw, id) {
  44754. const workerFileMatch = workerFileRE.exec(id);
  44755. if (workerFileMatch) {
  44756. const workerType2 = workerFileMatch[1];
  44757. let injectEnv = "";
  44758. const scriptPath = JSON.stringify(
  44759. path$n.posix.join(config.base, ENV_PUBLIC_PATH)
  44760. );
  44761. if (workerType2 === "classic") {
  44762. injectEnv = `importScripts(${scriptPath})
  44763. `;
  44764. } else if (workerType2 === "module") {
  44765. injectEnv = `import ${scriptPath}
  44766. `;
  44767. } else if (workerType2 === "ignore") {
  44768. if (isBuild) {
  44769. injectEnv = "";
  44770. } else if (server) {
  44771. const { moduleGraph } = server;
  44772. const module = moduleGraph.getModuleById(ENV_ENTRY);
  44773. injectEnv = module?.transformResult?.code || "";
  44774. }
  44775. }
  44776. if (injectEnv) {
  44777. const s = new MagicString(raw);
  44778. s.prepend(injectEnv + ";\n");
  44779. return {
  44780. code: s.toString(),
  44781. map: s.generateMap({ hires: "boundary" })
  44782. };
  44783. }
  44784. return;
  44785. }
  44786. const workerMatch = workerOrSharedWorkerRE.exec(id);
  44787. if (!workerMatch) return;
  44788. const { format } = config.worker;
  44789. const workerConstructor = workerMatch[1] === "sharedworker" ? "SharedWorker" : "Worker";
  44790. const workerType = isBuild ? format === "es" ? "module" : "classic" : "module";
  44791. const workerTypeOption = `{
  44792. ${workerType === "module" ? `type: "module",` : ""}
  44793. name: options?.name
  44794. }`;
  44795. let urlCode;
  44796. if (isBuild) {
  44797. if (isWorker && config.bundleChain.at(-1) === cleanUrl(id)) {
  44798. urlCode = "self.location.href";
  44799. } else if (inlineRE.test(id)) {
  44800. const chunk = await bundleWorkerEntry(config, id);
  44801. const encodedJs = `const encodedJs = "${Buffer.from(
  44802. chunk.code
  44803. ).toString("base64")}";`;
  44804. const code = (
  44805. // Using blob URL for SharedWorker results in multiple instances of a same worker
  44806. workerConstructor === "Worker" ? `${encodedJs}
  44807. const decodeBase64 = (base64) => Uint8Array.from(atob(base64), c => c.charCodeAt(0));
  44808. const blob = typeof self !== "undefined" && self.Blob && new Blob([${workerType === "classic" ? "" : (
  44809. // `URL` is always available, in `Worker[type="module"]`
  44810. `'URL.revokeObjectURL(import.meta.url);',`
  44811. )}decodeBase64(encodedJs)], { type: "text/javascript;charset=utf-8" });
  44812. export default function WorkerWrapper(options) {
  44813. let objURL;
  44814. try {
  44815. objURL = blob && (self.URL || self.webkitURL).createObjectURL(blob);
  44816. if (!objURL) throw ''
  44817. const worker = new ${workerConstructor}(objURL, ${workerTypeOption});
  44818. worker.addEventListener("error", () => {
  44819. (self.URL || self.webkitURL).revokeObjectURL(objURL);
  44820. });
  44821. return worker;
  44822. } catch(e) {
  44823. return new ${workerConstructor}(
  44824. "data:text/javascript;base64," + encodedJs,
  44825. ${workerTypeOption}
  44826. );
  44827. }${// For module workers, we should not revoke the URL until the worker runs,
  44828. // otherwise the worker fails to run
  44829. workerType === "classic" ? ` finally {
  44830. objURL && (self.URL || self.webkitURL).revokeObjectURL(objURL);
  44831. }` : ""}
  44832. }` : `${encodedJs}
  44833. export default function WorkerWrapper(options) {
  44834. return new ${workerConstructor}(
  44835. "data:text/javascript;base64," + encodedJs,
  44836. ${workerTypeOption}
  44837. );
  44838. }
  44839. `
  44840. );
  44841. return {
  44842. code,
  44843. // Empty sourcemap to suppress Rollup warning
  44844. map: { mappings: "" }
  44845. };
  44846. } else {
  44847. urlCode = JSON.stringify(await workerFileToUrl(config, id));
  44848. }
  44849. } else {
  44850. let url = await fileToUrl$1(cleanUrl(id), config, this);
  44851. url = injectQuery(url, `${WORKER_FILE_ID}&type=${workerType}`);
  44852. urlCode = JSON.stringify(url);
  44853. }
  44854. if (urlRE.test(id)) {
  44855. return {
  44856. code: `export default ${urlCode}`,
  44857. map: { mappings: "" }
  44858. // Empty sourcemap to suppress Rollup warning
  44859. };
  44860. }
  44861. return {
  44862. code: `export default function WorkerWrapper(options) {
  44863. return new ${workerConstructor}(
  44864. ${urlCode},
  44865. ${workerTypeOption}
  44866. );
  44867. }`,
  44868. map: { mappings: "" }
  44869. // Empty sourcemap to suppress Rollup warning
  44870. };
  44871. },
  44872. renderChunk(code, chunk, outputOptions) {
  44873. let s;
  44874. const result = () => {
  44875. return s && {
  44876. code: s.toString(),
  44877. map: config.build.sourcemap ? s.generateMap({ hires: "boundary" }) : null
  44878. };
  44879. };
  44880. workerAssetUrlRE.lastIndex = 0;
  44881. if (workerAssetUrlRE.test(code)) {
  44882. const toRelativeRuntime = createToImportMetaURLBasedRelativeRuntime(
  44883. outputOptions.format,
  44884. config.isWorker
  44885. );
  44886. let match;
  44887. s = new MagicString(code);
  44888. workerAssetUrlRE.lastIndex = 0;
  44889. const workerMap = workerCache.get(config.mainConfig || config);
  44890. const { fileNameHash } = workerMap;
  44891. while (match = workerAssetUrlRE.exec(code)) {
  44892. const [full, hash] = match;
  44893. const filename = fileNameHash.get(hash);
  44894. const replacement = toOutputFilePathInJS(
  44895. filename,
  44896. "asset",
  44897. chunk.fileName,
  44898. "js",
  44899. config,
  44900. toRelativeRuntime
  44901. );
  44902. const replacementString = typeof replacement === "string" ? JSON.stringify(encodeURIPath(replacement)).slice(1, -1) : `"+${replacement.runtime}+"`;
  44903. s.update(match.index, match.index + full.length, replacementString);
  44904. }
  44905. }
  44906. return result();
  44907. },
  44908. generateBundle(opts, bundle) {
  44909. if (opts.__vite_skip_asset_emit__ || isWorker) {
  44910. return;
  44911. }
  44912. const workerMap = workerCache.get(config);
  44913. workerMap.assets.forEach((asset) => {
  44914. const duplicateAsset = bundle[asset.fileName];
  44915. if (duplicateAsset) {
  44916. const content = duplicateAsset.type === "asset" ? duplicateAsset.source : duplicateAsset.code;
  44917. if (isSameContent(content, asset.source)) {
  44918. return;
  44919. }
  44920. }
  44921. this.emitFile({
  44922. type: "asset",
  44923. fileName: asset.fileName,
  44924. originalFileName: asset.originalFileName,
  44925. source: asset.source
  44926. });
  44927. });
  44928. workerMap.assets.clear();
  44929. }
  44930. };
  44931. }
  44932. function isSameContent(a, b) {
  44933. if (typeof a === "string") {
  44934. if (typeof b === "string") {
  44935. return a === b;
  44936. }
  44937. return Buffer.from(a).equals(b);
  44938. }
  44939. return Buffer.from(b).equals(a);
  44940. }
  44941. function preAliasPlugin(config) {
  44942. const findPatterns = getAliasPatterns(config.resolve.alias);
  44943. const isConfiguredAsExternal = createIsConfiguredAsSsrExternal(config);
  44944. const isBuild = config.command === "build";
  44945. const fsUtils = getFsUtils(config);
  44946. return {
  44947. name: "vite:pre-alias",
  44948. async resolveId(id, importer, options) {
  44949. const ssr = options?.ssr === true;
  44950. const depsOptimizer = !isBuild && getDepsOptimizer(config, ssr);
  44951. if (importer && depsOptimizer && bareImportRE.test(id) && !options?.scan && id !== "@vite/client" && id !== "@vite/env") {
  44952. if (findPatterns.find((pattern) => matches(pattern, id))) {
  44953. const optimizedId = await tryOptimizedResolve(
  44954. depsOptimizer,
  44955. id,
  44956. importer,
  44957. config.resolve.preserveSymlinks,
  44958. config.packageCache
  44959. );
  44960. if (optimizedId) {
  44961. return optimizedId;
  44962. }
  44963. if (depsOptimizer.options.noDiscovery) {
  44964. return;
  44965. }
  44966. const resolved = await this.resolve(id, importer, {
  44967. ...options,
  44968. custom: { ...options.custom, "vite:pre-alias": true }
  44969. });
  44970. if (resolved && !depsOptimizer.isOptimizedDepFile(resolved.id)) {
  44971. const optimizeDeps = depsOptimizer.options;
  44972. const resolvedId = cleanUrl(resolved.id);
  44973. const isVirtual = resolvedId === id || resolvedId.includes("\0");
  44974. if (!isVirtual && fsUtils.existsSync(resolvedId) && !moduleListContains(optimizeDeps.exclude, id) && path$n.isAbsolute(resolvedId) && (isInNodeModules$1(resolvedId) || optimizeDeps.include?.includes(id)) && isOptimizable(resolvedId, optimizeDeps) && !(isBuild && ssr && isConfiguredAsExternal(id, importer)) && (!ssr || optimizeAliasReplacementForSSR(resolvedId, optimizeDeps))) {
  44975. const optimizedInfo = depsOptimizer.registerMissingImport(
  44976. id,
  44977. resolvedId
  44978. );
  44979. return { id: depsOptimizer.getOptimizedDepId(optimizedInfo) };
  44980. }
  44981. }
  44982. return resolved;
  44983. }
  44984. }
  44985. }
  44986. };
  44987. }
  44988. function optimizeAliasReplacementForSSR(id, optimizeDeps) {
  44989. if (optimizeDeps.include?.includes(id)) {
  44990. return true;
  44991. }
  44992. return false;
  44993. }
  44994. function matches(pattern, importee) {
  44995. if (pattern instanceof RegExp) {
  44996. return pattern.test(importee);
  44997. }
  44998. if (importee.length < pattern.length) {
  44999. return false;
  45000. }
  45001. if (importee === pattern) {
  45002. return true;
  45003. }
  45004. return importee.startsWith(withTrailingSlash(pattern));
  45005. }
  45006. function getAliasPatterns(entries) {
  45007. if (!entries) {
  45008. return [];
  45009. }
  45010. if (Array.isArray(entries)) {
  45011. return entries.map((entry) => entry.find);
  45012. }
  45013. return Object.entries(entries).map(([find]) => find);
  45014. }
  45015. function getAliasPatternMatcher(entries) {
  45016. const patterns = getAliasPatterns(entries);
  45017. return (importee) => patterns.some((pattern) => matches(pattern, importee));
  45018. }
  45019. function err(e, pos) {
  45020. const error = new Error(e);
  45021. error.pos = pos;
  45022. return error;
  45023. }
  45024. function parseWorkerOptions(rawOpts, optsStartIndex) {
  45025. let opts = {};
  45026. try {
  45027. opts = evalValue(rawOpts);
  45028. } catch {
  45029. throw err(
  45030. "Vite is unable to parse the worker options as the value is not static.To ignore this error, please use /* @vite-ignore */ in the worker options.",
  45031. optsStartIndex
  45032. );
  45033. }
  45034. if (opts == null) {
  45035. return {};
  45036. }
  45037. if (typeof opts !== "object") {
  45038. throw err(
  45039. `Expected worker options to be an object, got ${typeof opts}`,
  45040. optsStartIndex
  45041. );
  45042. }
  45043. return opts;
  45044. }
  45045. function getWorkerType(raw, clean, i) {
  45046. const commaIndex = clean.indexOf(",", i);
  45047. if (commaIndex === -1) {
  45048. return "classic";
  45049. }
  45050. const endIndex = clean.indexOf(")", i);
  45051. if (commaIndex > endIndex) {
  45052. return "classic";
  45053. }
  45054. const workerOptString = raw.substring(commaIndex + 1, endIndex).replace(/\}[\s\S]*,/g, "}");
  45055. const hasViteIgnore = hasViteIgnoreRE.test(workerOptString);
  45056. if (hasViteIgnore) {
  45057. return "ignore";
  45058. }
  45059. const cleanWorkerOptString = clean.substring(commaIndex + 1, endIndex).trim();
  45060. if (!cleanWorkerOptString.length) {
  45061. return "classic";
  45062. }
  45063. const workerOpts = parseWorkerOptions(workerOptString, commaIndex + 1);
  45064. if (workerOpts.type && (workerOpts.type === "module" || workerOpts.type === "classic")) {
  45065. return workerOpts.type;
  45066. }
  45067. return "classic";
  45068. }
  45069. function isIncludeWorkerImportMetaUrl(code) {
  45070. if ((code.includes("new Worker") || code.includes("new SharedWorker")) && code.includes("new URL") && code.includes(`import.meta.url`)) {
  45071. return true;
  45072. }
  45073. return false;
  45074. }
  45075. function workerImportMetaUrlPlugin(config) {
  45076. const isBuild = config.command === "build";
  45077. let workerResolver;
  45078. const fsResolveOptions = {
  45079. ...config.resolve,
  45080. root: config.root,
  45081. isProduction: config.isProduction,
  45082. isBuild: config.command === "build",
  45083. packageCache: config.packageCache,
  45084. ssrConfig: config.ssr,
  45085. asSrc: true
  45086. };
  45087. return {
  45088. name: "vite:worker-import-meta-url",
  45089. shouldTransformCachedModule({ code }) {
  45090. if (isBuild && config.build.watch && isIncludeWorkerImportMetaUrl(code)) {
  45091. return true;
  45092. }
  45093. },
  45094. async transform(code, id, options) {
  45095. if (!options?.ssr && isIncludeWorkerImportMetaUrl(code)) {
  45096. let s;
  45097. const cleanString = stripLiteral(code);
  45098. const workerImportMetaUrlRE = /\bnew\s+(?:Worker|SharedWorker)\s*\(\s*(new\s+URL\s*\(\s*('[^']+'|"[^"]+"|`[^`]+`)\s*,\s*import\.meta\.url\s*\))/dg;
  45099. let match;
  45100. while (match = workerImportMetaUrlRE.exec(cleanString)) {
  45101. const [[, endIndex], [expStart, expEnd], [urlStart, urlEnd]] = match.indices;
  45102. const rawUrl = code.slice(urlStart, urlEnd);
  45103. if (rawUrl[0] === "`" && rawUrl.includes("${")) {
  45104. this.error(
  45105. `\`new URL(url, import.meta.url)\` is not supported in dynamic template string.`,
  45106. expStart
  45107. );
  45108. }
  45109. s ||= new MagicString(code);
  45110. const workerType = getWorkerType(code, cleanString, endIndex);
  45111. const url = rawUrl.slice(1, -1);
  45112. let file;
  45113. if (url[0] === ".") {
  45114. file = path$n.resolve(path$n.dirname(id), url);
  45115. file = tryFsResolve(file, fsResolveOptions) ?? file;
  45116. } else {
  45117. workerResolver ??= config.createResolver({
  45118. extensions: [],
  45119. tryIndex: false,
  45120. preferRelative: true
  45121. });
  45122. file = await workerResolver(url, id);
  45123. file ??= url[0] === "/" ? slash$1(path$n.join(config.publicDir, url)) : slash$1(path$n.resolve(path$n.dirname(id), url));
  45124. }
  45125. if (isBuild && config.isWorker && config.bundleChain.at(-1) === cleanUrl(file)) {
  45126. s.update(expStart, expEnd, "self.location.href");
  45127. } else {
  45128. let builtUrl;
  45129. if (isBuild) {
  45130. builtUrl = await workerFileToUrl(config, file);
  45131. } else {
  45132. builtUrl = await fileToUrl$1(cleanUrl(file), config, this);
  45133. builtUrl = injectQuery(
  45134. builtUrl,
  45135. `${WORKER_FILE_ID}&type=${workerType}`
  45136. );
  45137. }
  45138. s.update(
  45139. expStart,
  45140. expEnd,
  45141. `new URL(/* @vite-ignore */ ${JSON.stringify(builtUrl)}, import.meta.url)`
  45142. );
  45143. }
  45144. }
  45145. if (s) {
  45146. return transformStableResult(s, id, config);
  45147. }
  45148. return null;
  45149. }
  45150. }
  45151. };
  45152. }
  45153. function assetImportMetaUrlPlugin(config) {
  45154. const { publicDir } = config;
  45155. let assetResolver;
  45156. const fsResolveOptions = {
  45157. ...config.resolve,
  45158. root: config.root,
  45159. isProduction: config.isProduction,
  45160. isBuild: config.command === "build",
  45161. packageCache: config.packageCache,
  45162. ssrConfig: config.ssr,
  45163. asSrc: true
  45164. };
  45165. return {
  45166. name: "vite:asset-import-meta-url",
  45167. async transform(code, id, options) {
  45168. if (!options?.ssr && id !== preloadHelperId && id !== CLIENT_ENTRY && code.includes("new URL") && code.includes(`import.meta.url`)) {
  45169. let s;
  45170. const assetImportMetaUrlRE = /\bnew\s+URL\s*\(\s*('[^']+'|"[^"]+"|`[^`]+`)\s*,\s*import\.meta\.url\s*(?:,\s*)?\)/dg;
  45171. const cleanString = stripLiteral(code);
  45172. let match;
  45173. while (match = assetImportMetaUrlRE.exec(cleanString)) {
  45174. const [[startIndex, endIndex], [urlStart, urlEnd]] = match.indices;
  45175. if (hasViteIgnoreRE.test(code.slice(startIndex, urlStart))) continue;
  45176. const rawUrl = code.slice(urlStart, urlEnd);
  45177. if (!s) s = new MagicString(code);
  45178. if (rawUrl[0] === "`" && rawUrl.includes("${")) {
  45179. const queryDelimiterIndex = getQueryDelimiterIndex(rawUrl);
  45180. const hasQueryDelimiter = queryDelimiterIndex !== -1;
  45181. const pureUrl = hasQueryDelimiter ? rawUrl.slice(0, queryDelimiterIndex) + "`" : rawUrl;
  45182. const queryString = hasQueryDelimiter ? rawUrl.slice(queryDelimiterIndex, -1) : "";
  45183. const ast = this.parse(pureUrl);
  45184. const templateLiteral = ast.body[0].expression;
  45185. if (templateLiteral.expressions.length) {
  45186. const pattern = buildGlobPattern(templateLiteral);
  45187. if (pattern.startsWith("**")) {
  45188. continue;
  45189. }
  45190. const globOptions = {
  45191. eager: true,
  45192. import: "default",
  45193. // A hack to allow 'as' & 'query' exist at the same time
  45194. query: injectQuery(queryString, "url")
  45195. };
  45196. s.update(
  45197. startIndex,
  45198. endIndex,
  45199. `new URL((import.meta.glob(${JSON.stringify(
  45200. pattern
  45201. )}, ${JSON.stringify(
  45202. globOptions
  45203. )}))[${pureUrl}], import.meta.url)`
  45204. );
  45205. continue;
  45206. }
  45207. }
  45208. const url = rawUrl.slice(1, -1);
  45209. let file;
  45210. if (url[0] === ".") {
  45211. file = slash$1(path$n.resolve(path$n.dirname(id), url));
  45212. file = tryFsResolve(file, fsResolveOptions) ?? file;
  45213. } else {
  45214. assetResolver ??= config.createResolver({
  45215. extensions: [],
  45216. mainFields: [],
  45217. tryIndex: false,
  45218. preferRelative: true
  45219. });
  45220. file = await assetResolver(url, id);
  45221. file ??= url[0] === "/" ? slash$1(path$n.join(publicDir, url)) : slash$1(path$n.resolve(path$n.dirname(id), url));
  45222. }
  45223. let builtUrl;
  45224. if (file) {
  45225. try {
  45226. if (publicDir && isParentDirectory(publicDir, file)) {
  45227. const publicPath = "/" + path$n.posix.relative(publicDir, file);
  45228. builtUrl = await fileToUrl$1(publicPath, config, this);
  45229. } else {
  45230. builtUrl = await fileToUrl$1(file, config, this);
  45231. }
  45232. } catch {
  45233. }
  45234. }
  45235. if (!builtUrl) {
  45236. const rawExp = code.slice(startIndex, endIndex);
  45237. config.logger.warnOnce(
  45238. `
  45239. ${rawExp} doesn't exist at build time, it will remain unchanged to be resolved at runtime. If this is intended, you can use the /* @vite-ignore */ comment to suppress this warning.`
  45240. );
  45241. builtUrl = url;
  45242. }
  45243. s.update(
  45244. startIndex,
  45245. endIndex,
  45246. `new URL(${JSON.stringify(builtUrl)}, import.meta.url)`
  45247. );
  45248. }
  45249. if (s) {
  45250. return transformStableResult(s, id, config);
  45251. }
  45252. }
  45253. return null;
  45254. }
  45255. };
  45256. }
  45257. function buildGlobPattern(ast) {
  45258. let pattern = "";
  45259. let lastElementIndex = -1;
  45260. for (const exp of ast.expressions) {
  45261. for (let i = lastElementIndex + 1; i < ast.quasis.length; i++) {
  45262. const el = ast.quasis[i];
  45263. if (el.end < exp.start) {
  45264. pattern += el.value.raw;
  45265. lastElementIndex = i;
  45266. }
  45267. }
  45268. pattern += "**";
  45269. }
  45270. for (let i = lastElementIndex + 1; i < ast.quasis.length; i++) {
  45271. pattern += ast.quasis[i].value.raw;
  45272. }
  45273. return pattern;
  45274. }
  45275. function getQueryDelimiterIndex(rawUrl) {
  45276. let bracketsStack = 0;
  45277. for (let i = 0; i < rawUrl.length; i++) {
  45278. if (rawUrl[i] === "{") {
  45279. bracketsStack++;
  45280. } else if (rawUrl[i] === "}") {
  45281. bracketsStack--;
  45282. } else if (rawUrl[i] === "?" && bracketsStack === 0) {
  45283. return i;
  45284. }
  45285. }
  45286. return -1;
  45287. }
  45288. function metadataPlugin() {
  45289. return {
  45290. name: "vite:build-metadata",
  45291. async renderChunk(_code, chunk) {
  45292. chunk.viteMetadata = {
  45293. importedAssets: /* @__PURE__ */ new Set(),
  45294. importedCss: /* @__PURE__ */ new Set()
  45295. };
  45296. return null;
  45297. }
  45298. };
  45299. }
  45300. // Astring is a tiny and fast JavaScript code generator from an ESTree-compliant AST.
  45301. //
  45302. // Astring was written by David Bonnet and released under an MIT license.
  45303. //
  45304. // The Git repository for Astring is available at:
  45305. // https://github.com/davidbonnet/astring.git
  45306. //
  45307. // Please use the GitHub bug tracker to report issues:
  45308. // https://github.com/davidbonnet/astring/issues
  45309. /* c8 ignore if */
  45310. if (!String.prototype.repeat) {
  45311. /* c8 ignore next */
  45312. throw new Error(
  45313. 'String.prototype.repeat is undefined, see https://github.com/davidbonnet/astring#installation',
  45314. )
  45315. }
  45316. /* c8 ignore if */
  45317. if (!String.prototype.endsWith) {
  45318. /* c8 ignore next */
  45319. throw new Error(
  45320. 'String.prototype.endsWith is undefined, see https://github.com/davidbonnet/astring#installation',
  45321. )
  45322. }
  45323. class VariableDynamicImportError extends Error {}
  45324. /* eslint-disable-next-line no-template-curly-in-string */
  45325. const example = 'For example: import(`./foo/${bar}.js`).';
  45326. function sanitizeString(str) {
  45327. if (str === '') return str;
  45328. if (str.includes('*')) {
  45329. throw new VariableDynamicImportError('A dynamic import cannot contain * characters.');
  45330. }
  45331. return glob.escapePath(str);
  45332. }
  45333. function templateLiteralToGlob(node) {
  45334. let glob = '';
  45335. for (let i = 0; i < node.quasis.length; i += 1) {
  45336. glob += sanitizeString(node.quasis[i].value.raw);
  45337. if (node.expressions[i]) {
  45338. glob += expressionToGlob(node.expressions[i]);
  45339. }
  45340. }
  45341. return glob;
  45342. }
  45343. function callExpressionToGlob(node) {
  45344. const { callee } = node;
  45345. if (
  45346. callee.type === 'MemberExpression' &&
  45347. callee.property.type === 'Identifier' &&
  45348. callee.property.name === 'concat'
  45349. ) {
  45350. return `${expressionToGlob(callee.object)}${node.arguments.map(expressionToGlob).join('')}`;
  45351. }
  45352. return '*';
  45353. }
  45354. function binaryExpressionToGlob(node) {
  45355. if (node.operator !== '+') {
  45356. throw new VariableDynamicImportError(`${node.operator} operator is not supported.`);
  45357. }
  45358. return `${expressionToGlob(node.left)}${expressionToGlob(node.right)}`;
  45359. }
  45360. function expressionToGlob(node) {
  45361. switch (node.type) {
  45362. case 'TemplateLiteral':
  45363. return templateLiteralToGlob(node);
  45364. case 'CallExpression':
  45365. return callExpressionToGlob(node);
  45366. case 'BinaryExpression':
  45367. return binaryExpressionToGlob(node);
  45368. case 'Literal': {
  45369. return sanitizeString(node.value);
  45370. }
  45371. default:
  45372. return '*';
  45373. }
  45374. }
  45375. const defaultProtocol = 'file:';
  45376. const ignoredProtocols = ['data:', 'http:', 'https:'];
  45377. function shouldIgnore(glob) {
  45378. const containsAsterisk = glob.includes('*');
  45379. const globURL = new URL(glob, defaultProtocol);
  45380. const containsIgnoredProtocol = ignoredProtocols.some(
  45381. (ignoredProtocol) => ignoredProtocol === globURL.protocol
  45382. );
  45383. return !containsAsterisk || containsIgnoredProtocol;
  45384. }
  45385. function dynamicImportToGlob(node, sourceString) {
  45386. let glob = expressionToGlob(node);
  45387. if (shouldIgnore(glob)) {
  45388. return null;
  45389. }
  45390. glob = glob.replace(/\*\*/g, '*');
  45391. if (glob.startsWith('*')) {
  45392. throw new VariableDynamicImportError(
  45393. `invalid import "${sourceString}". It cannot be statically analyzed. Variable dynamic imports must start with ./ and be limited to a specific directory. ${example}`
  45394. );
  45395. }
  45396. if (glob.startsWith('/')) {
  45397. throw new VariableDynamicImportError(
  45398. `invalid import "${sourceString}". Variable absolute imports are not supported, imports must start with ./ in the static part of the import. ${example}`
  45399. );
  45400. }
  45401. if (!glob.startsWith('./') && !glob.startsWith('../')) {
  45402. throw new VariableDynamicImportError(
  45403. `invalid import "${sourceString}". Variable bare imports are not supported, imports must start with ./ in the static part of the import. ${example}`
  45404. );
  45405. }
  45406. // Disallow ./*.ext
  45407. const ownDirectoryStarExtension = /^\.\/\*\.[\w]+$/;
  45408. if (ownDirectoryStarExtension.test(glob)) {
  45409. throw new VariableDynamicImportError(
  45410. `${
  45411. `invalid import "${sourceString}". Variable imports cannot import their own directory, ` +
  45412. 'place imports in a separate directory or make the import filename more specific. '
  45413. }${example}`
  45414. );
  45415. }
  45416. if (require$$0$4.extname(glob) === '') {
  45417. throw new VariableDynamicImportError(
  45418. `invalid import "${sourceString}". A file extension must be included in the static part of the import. ${example}`
  45419. );
  45420. }
  45421. return glob;
  45422. }
  45423. const dynamicImportHelperId = "\0vite/dynamic-import-helper.js";
  45424. const relativePathRE = /^\.{1,2}\//;
  45425. const hasDynamicImportRE = /\bimport\s*[(/]/;
  45426. const dynamicImportHelper = (glob, path, segs) => {
  45427. const v = glob[path];
  45428. if (v) {
  45429. return typeof v === "function" ? v() : Promise.resolve(v);
  45430. }
  45431. return new Promise((_, reject) => {
  45432. (typeof queueMicrotask === "function" ? queueMicrotask : setTimeout)(
  45433. reject.bind(
  45434. null,
  45435. new Error(
  45436. "Unknown variable dynamic import: " + path + (path.split("/").length !== segs ? ". Note that variables only represent file names one level deep." : "")
  45437. )
  45438. )
  45439. );
  45440. });
  45441. };
  45442. function parseDynamicImportPattern(strings) {
  45443. const filename = strings.slice(1, -1);
  45444. const ast = parseAst(strings).body[0].expression;
  45445. const userPatternQuery = dynamicImportToGlob(ast, filename);
  45446. if (!userPatternQuery) {
  45447. return null;
  45448. }
  45449. const [userPattern] = userPatternQuery.split(
  45450. // ? is escaped on posix OS
  45451. requestQueryMaybeEscapedSplitRE,
  45452. 2
  45453. );
  45454. let [rawPattern, search] = filename.split(requestQuerySplitRE, 2);
  45455. let globParams = null;
  45456. if (search) {
  45457. search = "?" + search;
  45458. if (workerOrSharedWorkerRE.test(search) || urlRE.test(search) || rawRE.test(search)) {
  45459. globParams = {
  45460. query: search,
  45461. import: "*"
  45462. };
  45463. } else {
  45464. globParams = {
  45465. query: search
  45466. };
  45467. }
  45468. }
  45469. return {
  45470. globParams,
  45471. userPattern,
  45472. rawPattern
  45473. };
  45474. }
  45475. async function transformDynamicImport(importSource, importer, resolve, root) {
  45476. if (importSource[1] !== "." && importSource[1] !== "/") {
  45477. const resolvedFileName = await resolve(importSource.slice(1, -1), importer);
  45478. if (!resolvedFileName) {
  45479. return null;
  45480. }
  45481. const relativeFileName = normalizePath$3(
  45482. posix$1.relative(
  45483. posix$1.dirname(normalizePath$3(importer)),
  45484. normalizePath$3(resolvedFileName)
  45485. )
  45486. );
  45487. importSource = "`" + (relativeFileName[0] === "." ? "" : "./") + relativeFileName + "`";
  45488. }
  45489. const dynamicImportPattern = parseDynamicImportPattern(importSource);
  45490. if (!dynamicImportPattern) {
  45491. return null;
  45492. }
  45493. const { globParams, rawPattern, userPattern } = dynamicImportPattern;
  45494. const params = globParams ? `, ${JSON.stringify(globParams)}` : "";
  45495. let newRawPattern = posix$1.relative(
  45496. posix$1.dirname(importer),
  45497. await toAbsoluteGlob(rawPattern, root, importer, resolve)
  45498. );
  45499. if (!relativePathRE.test(newRawPattern)) {
  45500. newRawPattern = `./${newRawPattern}`;
  45501. }
  45502. const exp = `(import.meta.glob(${JSON.stringify(userPattern)}${params}))`;
  45503. return {
  45504. rawPattern: newRawPattern,
  45505. pattern: userPattern,
  45506. glob: exp
  45507. };
  45508. }
  45509. function dynamicImportVarsPlugin(config) {
  45510. const resolve = config.createResolver({
  45511. preferRelative: true,
  45512. tryIndex: false,
  45513. extensions: []
  45514. });
  45515. const { include, exclude, warnOnError } = config.build.dynamicImportVarsOptions;
  45516. const filter = createFilter(include, exclude);
  45517. return {
  45518. name: "vite:dynamic-import-vars",
  45519. resolveId(id) {
  45520. if (id === dynamicImportHelperId) {
  45521. return id;
  45522. }
  45523. },
  45524. load(id) {
  45525. if (id === dynamicImportHelperId) {
  45526. return "export default " + dynamicImportHelper.toString();
  45527. }
  45528. },
  45529. async transform(source, importer) {
  45530. if (!filter(importer) || importer === CLIENT_ENTRY || !hasDynamicImportRE.test(source)) {
  45531. return;
  45532. }
  45533. await init;
  45534. let imports = [];
  45535. try {
  45536. imports = parse$d(source)[0];
  45537. } catch (e) {
  45538. return null;
  45539. }
  45540. if (!imports.length) {
  45541. return null;
  45542. }
  45543. let s;
  45544. let needDynamicImportHelper = false;
  45545. for (let index = 0; index < imports.length; index++) {
  45546. const {
  45547. s: start,
  45548. e: end,
  45549. ss: expStart,
  45550. se: expEnd,
  45551. d: dynamicIndex
  45552. } = imports[index];
  45553. if (dynamicIndex === -1 || source[start] !== "`") {
  45554. continue;
  45555. }
  45556. if (hasViteIgnoreRE.test(source.slice(expStart, expEnd))) {
  45557. continue;
  45558. }
  45559. s ||= new MagicString(source);
  45560. let result;
  45561. try {
  45562. result = await transformDynamicImport(
  45563. source.slice(start, end),
  45564. importer,
  45565. resolve,
  45566. config.root
  45567. );
  45568. } catch (error) {
  45569. if (warnOnError) {
  45570. this.warn(error);
  45571. } else {
  45572. this.error(error);
  45573. }
  45574. }
  45575. if (!result) {
  45576. continue;
  45577. }
  45578. const { rawPattern, glob } = result;
  45579. needDynamicImportHelper = true;
  45580. s.overwrite(
  45581. expStart,
  45582. expEnd,
  45583. `__variableDynamicImportRuntimeHelper(${glob}, \`${rawPattern}\`, ${rawPattern.split("/").length})`
  45584. );
  45585. }
  45586. if (s) {
  45587. if (needDynamicImportHelper) {
  45588. s.prepend(
  45589. `import __variableDynamicImportRuntimeHelper from "${dynamicImportHelperId}";`
  45590. );
  45591. }
  45592. return transformStableResult(s, importer, config);
  45593. }
  45594. }
  45595. };
  45596. }
  45597. async function resolvePlugins(config, prePlugins, normalPlugins, postPlugins) {
  45598. const isBuild = config.command === "build";
  45599. const isWorker = config.isWorker;
  45600. const buildPlugins = isBuild ? await (await Promise.resolve().then(function () { return build$1; })).resolveBuildPlugins(config) : { pre: [], post: [] };
  45601. const { modulePreload } = config.build;
  45602. const depsOptimizerEnabled = !isBuild && (isDepsOptimizerEnabled(config, false) || isDepsOptimizerEnabled(config, true));
  45603. return [
  45604. depsOptimizerEnabled ? optimizedDepsPlugin(config) : null,
  45605. isBuild ? metadataPlugin() : null,
  45606. !isWorker ? watchPackageDataPlugin(config.packageCache) : null,
  45607. preAliasPlugin(config),
  45608. alias$1({
  45609. entries: config.resolve.alias,
  45610. customResolver: viteAliasCustomResolver
  45611. }),
  45612. ...prePlugins,
  45613. modulePreload !== false && modulePreload.polyfill ? modulePreloadPolyfillPlugin(config) : null,
  45614. resolvePlugin({
  45615. ...config.resolve,
  45616. root: config.root,
  45617. isProduction: config.isProduction,
  45618. isBuild,
  45619. packageCache: config.packageCache,
  45620. ssrConfig: config.ssr,
  45621. asSrc: true,
  45622. fsUtils: getFsUtils(config),
  45623. getDepsOptimizer: isBuild ? void 0 : (ssr) => getDepsOptimizer(config, ssr),
  45624. shouldExternalize: isBuild && config.build.ssr ? (id, importer) => shouldExternalizeForSSR(id, importer, config) : void 0
  45625. }),
  45626. htmlInlineProxyPlugin(config),
  45627. cssPlugin(config),
  45628. config.esbuild !== false ? esbuildPlugin(config) : null,
  45629. jsonPlugin(
  45630. {
  45631. namedExports: true,
  45632. ...config.json
  45633. },
  45634. isBuild
  45635. ),
  45636. wasmHelperPlugin(config),
  45637. webWorkerPlugin(config),
  45638. assetPlugin(config),
  45639. ...normalPlugins,
  45640. wasmFallbackPlugin(),
  45641. definePlugin(config),
  45642. cssPostPlugin(config),
  45643. isBuild && buildHtmlPlugin(config),
  45644. workerImportMetaUrlPlugin(config),
  45645. assetImportMetaUrlPlugin(config),
  45646. ...buildPlugins.pre,
  45647. dynamicImportVarsPlugin(config),
  45648. importGlobPlugin(config),
  45649. ...postPlugins,
  45650. ...buildPlugins.post,
  45651. // internal server-only plugins are always applied after everything else
  45652. ...isBuild ? [] : [
  45653. clientInjectionsPlugin(config),
  45654. cssAnalysisPlugin(config),
  45655. importAnalysisPlugin(config)
  45656. ]
  45657. ].filter(Boolean);
  45658. }
  45659. function createPluginHookUtils(plugins) {
  45660. const sortedPluginsCache = /* @__PURE__ */ new Map();
  45661. function getSortedPlugins(hookName) {
  45662. if (sortedPluginsCache.has(hookName))
  45663. return sortedPluginsCache.get(hookName);
  45664. const sorted = getSortedPluginsByHook(hookName, plugins);
  45665. sortedPluginsCache.set(hookName, sorted);
  45666. return sorted;
  45667. }
  45668. function getSortedPluginHooks(hookName) {
  45669. const plugins2 = getSortedPlugins(hookName);
  45670. return plugins2.map((p) => getHookHandler(p[hookName])).filter(Boolean);
  45671. }
  45672. return {
  45673. getSortedPlugins,
  45674. getSortedPluginHooks
  45675. };
  45676. }
  45677. function getSortedPluginsByHook(hookName, plugins) {
  45678. const sortedPlugins = [];
  45679. let pre = 0, normal = 0, post = 0;
  45680. for (const plugin of plugins) {
  45681. const hook = plugin[hookName];
  45682. if (hook) {
  45683. if (typeof hook === "object") {
  45684. if (hook.order === "pre") {
  45685. sortedPlugins.splice(pre++, 0, plugin);
  45686. continue;
  45687. }
  45688. if (hook.order === "post") {
  45689. sortedPlugins.splice(pre + normal + post++, 0, plugin);
  45690. continue;
  45691. }
  45692. }
  45693. sortedPlugins.splice(pre + normal++, 0, plugin);
  45694. }
  45695. }
  45696. return sortedPlugins;
  45697. }
  45698. function getHookHandler(hook) {
  45699. return typeof hook === "object" ? hook.handler : hook;
  45700. }
  45701. const viteAliasCustomResolver = async function(id, importer, options) {
  45702. const resolved = await this.resolve(id, importer, options);
  45703. return resolved || { id, meta: { "vite:alias": { noResolved: true } } };
  45704. };
  45705. function ansiRegex({onlyFirst = false} = {}) {
  45706. const pattern = [
  45707. '[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)',
  45708. '(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~]))'
  45709. ].join('|');
  45710. return new RegExp(pattern, onlyFirst ? undefined : 'g');
  45711. }
  45712. const regex = ansiRegex();
  45713. function stripAnsi(string) {
  45714. if (typeof string !== 'string') {
  45715. throw new TypeError(`Expected a \`string\`, got \`${typeof string}\``);
  45716. }
  45717. // Even though the regex is global, we don't need to reset the `.lastIndex`
  45718. // because unlike `.exec()` and `.test()`, `.replace()` does it automatically
  45719. // and doing it manually has a performance penalty.
  45720. return string.replace(regex, '');
  45721. }
  45722. function prepareError(err) {
  45723. return {
  45724. message: stripAnsi(err.message),
  45725. stack: stripAnsi(cleanStack(err.stack || "")),
  45726. id: err.id,
  45727. frame: stripAnsi(err.frame || ""),
  45728. plugin: err.plugin,
  45729. pluginCode: err.pluginCode?.toString(),
  45730. loc: err.loc
  45731. };
  45732. }
  45733. function buildErrorMessage(err, args = [], includeStack = true) {
  45734. if (err.plugin) args.push(` Plugin: ${colors$1.magenta(err.plugin)}`);
  45735. const loc = err.loc ? `:${err.loc.line}:${err.loc.column}` : "";
  45736. if (err.id) args.push(` File: ${colors$1.cyan(err.id)}${loc}`);
  45737. if (err.frame) args.push(colors$1.yellow(pad$1(err.frame)));
  45738. if (includeStack && err.stack) args.push(pad$1(cleanStack(err.stack)));
  45739. return args.join("\n");
  45740. }
  45741. function cleanStack(stack) {
  45742. return stack.split(/\n/).filter((l) => /^\s*at/.test(l)).join("\n");
  45743. }
  45744. function logError(server, err) {
  45745. const msg = buildErrorMessage(err, [
  45746. colors$1.red(`Internal server error: ${err.message}`)
  45747. ]);
  45748. server.config.logger.error(msg, {
  45749. clear: true,
  45750. timestamp: true,
  45751. error: err
  45752. });
  45753. server.hot.send({
  45754. type: "error",
  45755. err: prepareError(err)
  45756. });
  45757. }
  45758. function errorMiddleware(server, allowNext = false) {
  45759. return function viteErrorMiddleware(err, _req, res, next) {
  45760. logError(server, err);
  45761. if (allowNext) {
  45762. next();
  45763. } else {
  45764. res.statusCode = 500;
  45765. res.end(`
  45766. <!DOCTYPE html>
  45767. <html lang="en">
  45768. <head>
  45769. <meta charset="UTF-8" />
  45770. <title>Error</title>
  45771. <script type="module">
  45772. const error = ${JSON.stringify(prepareError(err)).replace(
  45773. /</g,
  45774. "\\u003c"
  45775. )}
  45776. try {
  45777. const { ErrorOverlay } = await import(${JSON.stringify(path$n.posix.join(server.config.base, CLIENT_PUBLIC_PATH))})
  45778. document.body.appendChild(new ErrorOverlay(error))
  45779. } catch {
  45780. const h = (tag, text) => {
  45781. const el = document.createElement(tag)
  45782. el.textContent = text
  45783. return el
  45784. }
  45785. document.body.appendChild(h('h1', 'Internal Server Error'))
  45786. document.body.appendChild(h('h2', error.message))
  45787. document.body.appendChild(h('pre', error.stack))
  45788. document.body.appendChild(h('p', '(Error overlay failed to load)'))
  45789. }
  45790. <\/script>
  45791. </head>
  45792. <body>
  45793. </body>
  45794. </html>
  45795. `);
  45796. }
  45797. };
  45798. }
  45799. const noop$3 = () => {
  45800. };
  45801. const EMPTY_OBJECT = Object.freeze({});
  45802. const debugSourcemapCombineFilter = process.env.DEBUG_VITE_SOURCEMAP_COMBINE_FILTER;
  45803. const debugSourcemapCombine = createDebugger("vite:sourcemap-combine", {
  45804. onlyWhenFocused: true
  45805. });
  45806. const debugResolve = createDebugger("vite:resolve");
  45807. const debugPluginResolve = createDebugger("vite:plugin-resolve", {
  45808. onlyWhenFocused: "vite:plugin"
  45809. });
  45810. const debugPluginTransform = createDebugger("vite:plugin-transform", {
  45811. onlyWhenFocused: "vite:plugin"
  45812. });
  45813. const ERR_CLOSED_SERVER = "ERR_CLOSED_SERVER";
  45814. function throwClosedServerError() {
  45815. const err = new Error(
  45816. "The server is being restarted or closed. Request is outdated"
  45817. );
  45818. err.code = ERR_CLOSED_SERVER;
  45819. throw err;
  45820. }
  45821. async function createPluginContainer(config, moduleGraph, watcher) {
  45822. const container = new PluginContainer(config, moduleGraph, watcher);
  45823. await container.resolveRollupOptions();
  45824. return container;
  45825. }
  45826. class PluginContainer {
  45827. /**
  45828. * @internal use `createPluginContainer` instead
  45829. */
  45830. constructor(config, moduleGraph, watcher, plugins = config.plugins) {
  45831. this.config = config;
  45832. this.moduleGraph = moduleGraph;
  45833. this.watcher = watcher;
  45834. this.plugins = plugins;
  45835. this.minimalContext = {
  45836. meta: {
  45837. rollupVersion,
  45838. watchMode: true
  45839. },
  45840. debug: noop$3,
  45841. info: noop$3,
  45842. warn: noop$3,
  45843. // @ts-expect-error noop
  45844. error: noop$3
  45845. };
  45846. const utils = createPluginHookUtils(plugins);
  45847. this.getSortedPlugins = utils.getSortedPlugins;
  45848. this.getSortedPluginHooks = utils.getSortedPluginHooks;
  45849. }
  45850. _pluginContextMap = /* @__PURE__ */ new Map();
  45851. _pluginContextMapSsr = /* @__PURE__ */ new Map();
  45852. _resolvedRollupOptions;
  45853. _processesing = /* @__PURE__ */ new Set();
  45854. _seenResolves = {};
  45855. _closed = false;
  45856. // _addedFiles from the `load()` hook gets saved here so it can be reused in the `transform()` hook
  45857. _moduleNodeToLoadAddedImports = /* @__PURE__ */ new WeakMap();
  45858. getSortedPluginHooks;
  45859. getSortedPlugins;
  45860. watchFiles = /* @__PURE__ */ new Set();
  45861. minimalContext;
  45862. _updateModuleLoadAddedImports(id, addedImports) {
  45863. const module = this.moduleGraph?.getModuleById(id);
  45864. if (module) {
  45865. this._moduleNodeToLoadAddedImports.set(module, addedImports);
  45866. }
  45867. }
  45868. _getAddedImports(id) {
  45869. const module = this.moduleGraph?.getModuleById(id);
  45870. return module ? this._moduleNodeToLoadAddedImports.get(module) || null : null;
  45871. }
  45872. getModuleInfo(id) {
  45873. const module = this.moduleGraph?.getModuleById(id);
  45874. if (!module) {
  45875. return null;
  45876. }
  45877. if (!module.info) {
  45878. module.info = new Proxy(
  45879. { id, meta: module.meta || EMPTY_OBJECT },
  45880. // throw when an unsupported ModuleInfo property is accessed,
  45881. // so that incompatible plugins fail in a non-cryptic way.
  45882. {
  45883. get(info, key) {
  45884. if (key in info) {
  45885. return info[key];
  45886. }
  45887. if (key === "then") {
  45888. return void 0;
  45889. }
  45890. throw Error(
  45891. `[vite] The "${key}" property of ModuleInfo is not supported.`
  45892. );
  45893. }
  45894. }
  45895. );
  45896. }
  45897. return module.info ?? null;
  45898. }
  45899. // keeps track of hook promises so that we can wait for them all to finish upon closing the server
  45900. handleHookPromise(maybePromise) {
  45901. if (!maybePromise?.then) {
  45902. return maybePromise;
  45903. }
  45904. const promise = maybePromise;
  45905. this._processesing.add(promise);
  45906. return promise.finally(() => this._processesing.delete(promise));
  45907. }
  45908. get options() {
  45909. return this._resolvedRollupOptions;
  45910. }
  45911. async resolveRollupOptions() {
  45912. if (!this._resolvedRollupOptions) {
  45913. let options = this.config.build.rollupOptions;
  45914. for (const optionsHook of this.getSortedPluginHooks("options")) {
  45915. if (this._closed) {
  45916. throwClosedServerError();
  45917. }
  45918. options = await this.handleHookPromise(
  45919. optionsHook.call(this.minimalContext, options)
  45920. ) || options;
  45921. }
  45922. this._resolvedRollupOptions = options;
  45923. }
  45924. return this._resolvedRollupOptions;
  45925. }
  45926. _getPluginContext(plugin, ssr) {
  45927. const map = ssr ? this._pluginContextMapSsr : this._pluginContextMap;
  45928. if (!map.has(plugin)) {
  45929. const ctx = new PluginContext(plugin, this, ssr);
  45930. map.set(plugin, ctx);
  45931. }
  45932. return map.get(plugin);
  45933. }
  45934. // parallel, ignores returns
  45935. async hookParallel(hookName, context, args) {
  45936. const parallelPromises = [];
  45937. for (const plugin of this.getSortedPlugins(hookName)) {
  45938. const hook = plugin[hookName];
  45939. if (!hook) continue;
  45940. const handler = getHookHandler(hook);
  45941. if (hook.sequential) {
  45942. await Promise.all(parallelPromises);
  45943. parallelPromises.length = 0;
  45944. await handler.apply(context(plugin), args(plugin));
  45945. } else {
  45946. parallelPromises.push(handler.apply(context(plugin), args(plugin)));
  45947. }
  45948. }
  45949. await Promise.all(parallelPromises);
  45950. }
  45951. async buildStart(_options) {
  45952. await this.handleHookPromise(
  45953. this.hookParallel(
  45954. "buildStart",
  45955. (plugin) => this._getPluginContext(plugin, false),
  45956. () => [this.options]
  45957. )
  45958. );
  45959. }
  45960. async resolveId(rawId, importer = join$2(this.config.root, "index.html"), options) {
  45961. const skip = options?.skip;
  45962. const ssr = options?.ssr;
  45963. const scan = !!options?.scan;
  45964. const ctx = new ResolveIdContext(this, !!ssr, skip, scan);
  45965. const resolveStart = debugResolve ? performance$1.now() : 0;
  45966. let id = null;
  45967. const partial = {};
  45968. for (const plugin of this.getSortedPlugins("resolveId")) {
  45969. if (this._closed && !ssr) throwClosedServerError();
  45970. if (!plugin.resolveId) continue;
  45971. if (skip?.has(plugin)) continue;
  45972. ctx._plugin = plugin;
  45973. const pluginResolveStart = debugPluginResolve ? performance$1.now() : 0;
  45974. const handler = getHookHandler(plugin.resolveId);
  45975. const result = await this.handleHookPromise(
  45976. handler.call(ctx, rawId, importer, {
  45977. attributes: options?.attributes ?? {},
  45978. custom: options?.custom,
  45979. isEntry: !!options?.isEntry,
  45980. ssr,
  45981. scan
  45982. })
  45983. );
  45984. if (!result) continue;
  45985. if (typeof result === "string") {
  45986. id = result;
  45987. } else {
  45988. id = result.id;
  45989. Object.assign(partial, result);
  45990. }
  45991. debugPluginResolve?.(
  45992. timeFrom(pluginResolveStart),
  45993. plugin.name,
  45994. prettifyUrl(id, this.config.root)
  45995. );
  45996. break;
  45997. }
  45998. if (debugResolve && rawId !== id && !rawId.startsWith(FS_PREFIX)) {
  45999. const key = rawId + id;
  46000. if (!this._seenResolves[key]) {
  46001. this._seenResolves[key] = true;
  46002. debugResolve(
  46003. `${timeFrom(resolveStart)} ${colors$1.cyan(rawId)} -> ${colors$1.dim(
  46004. id
  46005. )}`
  46006. );
  46007. }
  46008. }
  46009. if (id) {
  46010. partial.id = isExternalUrl(id) ? id : normalizePath$3(id);
  46011. return partial;
  46012. } else {
  46013. return null;
  46014. }
  46015. }
  46016. async load(id, options) {
  46017. const ssr = options?.ssr;
  46018. const ctx = new LoadPluginContext(this, !!ssr);
  46019. for (const plugin of this.getSortedPlugins("load")) {
  46020. if (this._closed && !ssr) throwClosedServerError();
  46021. if (!plugin.load) continue;
  46022. ctx._plugin = plugin;
  46023. const handler = getHookHandler(plugin.load);
  46024. const result = await this.handleHookPromise(
  46025. handler.call(ctx, id, { ssr })
  46026. );
  46027. if (result != null) {
  46028. if (isObject$1(result)) {
  46029. ctx._updateModuleInfo(id, result);
  46030. }
  46031. this._updateModuleLoadAddedImports(id, ctx._addedImports);
  46032. return result;
  46033. }
  46034. }
  46035. this._updateModuleLoadAddedImports(id, ctx._addedImports);
  46036. return null;
  46037. }
  46038. async transform(code, id, options) {
  46039. const inMap = options?.inMap;
  46040. const ssr = options?.ssr;
  46041. const ctx = new TransformPluginContext(
  46042. this,
  46043. id,
  46044. code,
  46045. inMap,
  46046. !!ssr
  46047. );
  46048. ctx._addedImports = this._getAddedImports(id);
  46049. for (const plugin of this.getSortedPlugins("transform")) {
  46050. if (this._closed && !ssr) throwClosedServerError();
  46051. if (!plugin.transform) continue;
  46052. ctx._updateActiveInfo(plugin, id, code);
  46053. const start = debugPluginTransform ? performance$1.now() : 0;
  46054. let result;
  46055. const handler = getHookHandler(plugin.transform);
  46056. try {
  46057. result = await this.handleHookPromise(
  46058. handler.call(ctx, code, id, { ssr })
  46059. );
  46060. } catch (e) {
  46061. ctx.error(e);
  46062. }
  46063. if (!result) continue;
  46064. debugPluginTransform?.(
  46065. timeFrom(start),
  46066. plugin.name,
  46067. prettifyUrl(id, this.config.root)
  46068. );
  46069. if (isObject$1(result)) {
  46070. if (result.code !== void 0) {
  46071. code = result.code;
  46072. if (result.map) {
  46073. if (debugSourcemapCombine) {
  46074. result.map.name = plugin.name;
  46075. }
  46076. ctx.sourcemapChain.push(result.map);
  46077. }
  46078. }
  46079. ctx._updateModuleInfo(id, result);
  46080. } else {
  46081. code = result;
  46082. }
  46083. }
  46084. return {
  46085. code,
  46086. map: ctx._getCombinedSourcemap()
  46087. };
  46088. }
  46089. async watchChange(id, change) {
  46090. await this.hookParallel(
  46091. "watchChange",
  46092. (plugin) => this._getPluginContext(plugin, false),
  46093. () => [id, change]
  46094. );
  46095. }
  46096. async close() {
  46097. if (this._closed) return;
  46098. this._closed = true;
  46099. await Promise.allSettled(Array.from(this._processesing));
  46100. await this.hookParallel(
  46101. "buildEnd",
  46102. (plugin) => this._getPluginContext(plugin, false),
  46103. () => []
  46104. );
  46105. await this.hookParallel(
  46106. "closeBundle",
  46107. (plugin) => this._getPluginContext(plugin, false),
  46108. () => []
  46109. );
  46110. }
  46111. }
  46112. class PluginContext {
  46113. constructor(_plugin, _container, ssr) {
  46114. this._plugin = _plugin;
  46115. this._container = _container;
  46116. this.ssr = ssr;
  46117. this.meta = this._container.minimalContext.meta;
  46118. }
  46119. _scan = false;
  46120. _resolveSkips;
  46121. _activeId = null;
  46122. _activeCode = null;
  46123. meta;
  46124. parse(code, opts) {
  46125. return parseAst(code, opts);
  46126. }
  46127. getModuleInfo(id) {
  46128. return this._container.getModuleInfo(id);
  46129. }
  46130. async resolve(id, importer, options) {
  46131. let skip;
  46132. if (options?.skipSelf !== false && this._plugin) {
  46133. skip = new Set(this._resolveSkips);
  46134. skip.add(this._plugin);
  46135. }
  46136. let out = await this._container.resolveId(id, importer, {
  46137. attributes: options?.attributes,
  46138. custom: options?.custom,
  46139. isEntry: !!options?.isEntry,
  46140. skip,
  46141. ssr: this.ssr,
  46142. scan: this._scan
  46143. });
  46144. if (typeof out === "string") out = { id: out };
  46145. return out;
  46146. }
  46147. async load(options) {
  46148. await this._container.moduleGraph?.ensureEntryFromUrl(
  46149. unwrapId$1(options.id),
  46150. this.ssr
  46151. );
  46152. this._updateModuleInfo(options.id, options);
  46153. const loadResult = await this._container.load(options.id, {
  46154. ssr: this.ssr
  46155. });
  46156. const code = typeof loadResult === "object" ? loadResult?.code : loadResult;
  46157. if (code != null) {
  46158. await this._container.transform(code, options.id, { ssr: this.ssr });
  46159. }
  46160. const moduleInfo = this.getModuleInfo(options.id);
  46161. if (!moduleInfo) throw Error(`Failed to load module with id ${options.id}`);
  46162. return moduleInfo;
  46163. }
  46164. _updateModuleInfo(id, { meta }) {
  46165. if (meta) {
  46166. const moduleInfo = this.getModuleInfo(id);
  46167. if (moduleInfo) {
  46168. moduleInfo.meta = { ...moduleInfo.meta, ...meta };
  46169. }
  46170. }
  46171. }
  46172. getModuleIds() {
  46173. return this._container.moduleGraph ? this._container.moduleGraph.idToModuleMap.keys() : Array.prototype[Symbol.iterator]();
  46174. }
  46175. addWatchFile(id) {
  46176. this._container.watchFiles.add(id);
  46177. if (this._container.watcher)
  46178. ensureWatchedFile(
  46179. this._container.watcher,
  46180. id,
  46181. this._container.config.root
  46182. );
  46183. }
  46184. getWatchFiles() {
  46185. return [...this._container.watchFiles];
  46186. }
  46187. emitFile(assetOrFile) {
  46188. this._warnIncompatibleMethod(`emitFile`);
  46189. return "";
  46190. }
  46191. setAssetSource() {
  46192. this._warnIncompatibleMethod(`setAssetSource`);
  46193. }
  46194. getFileName() {
  46195. this._warnIncompatibleMethod(`getFileName`);
  46196. return "";
  46197. }
  46198. warn(e, position) {
  46199. const err = this._formatError(typeof e === "function" ? e() : e, position);
  46200. const msg = buildErrorMessage(
  46201. err,
  46202. [colors$1.yellow(`warning: ${err.message}`)],
  46203. false
  46204. );
  46205. this._container.config.logger.warn(msg, {
  46206. clear: true,
  46207. timestamp: true
  46208. });
  46209. }
  46210. error(e, position) {
  46211. throw this._formatError(e, position);
  46212. }
  46213. debug = noop$3;
  46214. info = noop$3;
  46215. _formatError(e, position) {
  46216. const err = typeof e === "string" ? new Error(e) : e;
  46217. if (err.pluginCode) {
  46218. return err;
  46219. }
  46220. if (this._plugin) err.plugin = this._plugin.name;
  46221. if (this._activeId && !err.id) err.id = this._activeId;
  46222. if (this._activeCode) {
  46223. err.pluginCode = this._activeCode;
  46224. const pos = position ?? err.pos ?? err.position;
  46225. if (pos != null) {
  46226. let errLocation;
  46227. try {
  46228. errLocation = numberToPos(this._activeCode, pos);
  46229. } catch (err2) {
  46230. this._container.config.logger.error(
  46231. colors$1.red(
  46232. `Error in error handler:
  46233. ${err2.stack || err2.message}
  46234. `
  46235. ),
  46236. // print extra newline to separate the two errors
  46237. { error: err2 }
  46238. );
  46239. throw err;
  46240. }
  46241. err.loc = err.loc || {
  46242. file: err.id,
  46243. ...errLocation
  46244. };
  46245. err.frame = err.frame || generateCodeFrame(this._activeCode, pos);
  46246. } else if (err.loc) {
  46247. if (!err.frame) {
  46248. let code = this._activeCode;
  46249. if (err.loc.file) {
  46250. err.id = normalizePath$3(err.loc.file);
  46251. try {
  46252. code = fs__default.readFileSync(err.loc.file, "utf-8");
  46253. } catch {
  46254. }
  46255. }
  46256. err.frame = generateCodeFrame(code, err.loc);
  46257. }
  46258. } else if (err.line && err.column) {
  46259. err.loc = {
  46260. file: err.id,
  46261. line: err.line,
  46262. column: err.column
  46263. };
  46264. err.frame = err.frame || generateCodeFrame(this._activeCode, err.loc);
  46265. }
  46266. if (this instanceof TransformPluginContext && typeof err.loc?.line === "number" && typeof err.loc?.column === "number") {
  46267. const rawSourceMap = this._getCombinedSourcemap();
  46268. if (rawSourceMap && "version" in rawSourceMap) {
  46269. const traced = new TraceMap(rawSourceMap);
  46270. const { source, line, column } = originalPositionFor$1(traced, {
  46271. line: Number(err.loc.line),
  46272. column: Number(err.loc.column)
  46273. });
  46274. if (source && line != null && column != null) {
  46275. err.loc = { file: source, line, column };
  46276. }
  46277. }
  46278. }
  46279. } else if (err.loc) {
  46280. if (!err.frame) {
  46281. let code = err.pluginCode;
  46282. if (err.loc.file) {
  46283. err.id = normalizePath$3(err.loc.file);
  46284. if (!code) {
  46285. try {
  46286. code = fs__default.readFileSync(err.loc.file, "utf-8");
  46287. } catch {
  46288. }
  46289. }
  46290. }
  46291. if (code) {
  46292. err.frame = generateCodeFrame(`${code}`, err.loc);
  46293. }
  46294. }
  46295. }
  46296. if (typeof err.loc?.column !== "number" && typeof err.loc?.line !== "number" && !err.loc?.file) {
  46297. delete err.loc;
  46298. }
  46299. return err;
  46300. }
  46301. _warnIncompatibleMethod(method) {
  46302. this._container.config.logger.warn(
  46303. colors$1.cyan(`[plugin:${this._plugin.name}] `) + colors$1.yellow(
  46304. `context method ${colors$1.bold(
  46305. `${method}()`
  46306. )} is not supported in serve mode. This plugin is likely not vite-compatible.`
  46307. )
  46308. );
  46309. }
  46310. }
  46311. class ResolveIdContext extends PluginContext {
  46312. constructor(container, ssr, skip, scan) {
  46313. super(null, container, ssr);
  46314. this._resolveSkips = skip;
  46315. this._scan = scan;
  46316. }
  46317. }
  46318. class LoadPluginContext extends PluginContext {
  46319. _addedImports = null;
  46320. constructor(container, ssr) {
  46321. super(null, container, ssr);
  46322. }
  46323. addWatchFile(id) {
  46324. if (!this._addedImports) {
  46325. this._addedImports = /* @__PURE__ */ new Set();
  46326. }
  46327. this._addedImports.add(id);
  46328. super.addWatchFile(id);
  46329. }
  46330. }
  46331. class TransformPluginContext extends LoadPluginContext {
  46332. filename;
  46333. originalCode;
  46334. originalSourcemap = null;
  46335. sourcemapChain = [];
  46336. combinedMap = null;
  46337. constructor(container, id, code, inMap, ssr) {
  46338. super(container, ssr);
  46339. this.filename = id;
  46340. this.originalCode = code;
  46341. if (inMap) {
  46342. if (debugSourcemapCombine) {
  46343. inMap.name = "$inMap";
  46344. }
  46345. this.sourcemapChain.push(inMap);
  46346. }
  46347. }
  46348. _getCombinedSourcemap() {
  46349. if (debugSourcemapCombine && debugSourcemapCombineFilter && this.filename.includes(debugSourcemapCombineFilter)) {
  46350. debugSourcemapCombine("----------", this.filename);
  46351. debugSourcemapCombine(this.combinedMap);
  46352. debugSourcemapCombine(this.sourcemapChain);
  46353. debugSourcemapCombine("----------");
  46354. }
  46355. let combinedMap = this.combinedMap;
  46356. if (combinedMap && !("version" in combinedMap) && combinedMap.mappings === "") {
  46357. this.sourcemapChain.length = 0;
  46358. return combinedMap;
  46359. }
  46360. for (let m of this.sourcemapChain) {
  46361. if (typeof m === "string") m = JSON.parse(m);
  46362. if (!("version" in m)) {
  46363. if (m.mappings === "") {
  46364. combinedMap = { mappings: "" };
  46365. break;
  46366. }
  46367. combinedMap = null;
  46368. break;
  46369. }
  46370. if (!combinedMap) {
  46371. const sm = m;
  46372. if (sm.sources.length === 1 && !sm.sources[0]) {
  46373. combinedMap = {
  46374. ...sm,
  46375. sources: [this.filename],
  46376. sourcesContent: [this.originalCode]
  46377. };
  46378. } else {
  46379. combinedMap = sm;
  46380. }
  46381. } else {
  46382. combinedMap = combineSourcemaps(cleanUrl(this.filename), [
  46383. m,
  46384. combinedMap
  46385. ]);
  46386. }
  46387. }
  46388. if (combinedMap !== this.combinedMap) {
  46389. this.combinedMap = combinedMap;
  46390. this.sourcemapChain.length = 0;
  46391. }
  46392. return this.combinedMap;
  46393. }
  46394. getCombinedSourcemap() {
  46395. const map = this._getCombinedSourcemap();
  46396. if (!map || !("version" in map) && map.mappings === "") {
  46397. return new MagicString(this.originalCode).generateMap({
  46398. includeContent: true,
  46399. hires: "boundary",
  46400. source: cleanUrl(this.filename)
  46401. });
  46402. }
  46403. return map;
  46404. }
  46405. _updateActiveInfo(plugin, id, code) {
  46406. this._plugin = plugin;
  46407. this._activeId = id;
  46408. this._activeCode = code;
  46409. }
  46410. }
  46411. const debug$9 = createDebugger("vite:deps");
  46412. const htmlTypesRE = /\.(html|vue|svelte|astro|imba)$/;
  46413. const importsRE = /(?<!\/\/.*)(?<=^|;|\*\/)\s*import(?!\s+type)(?:[\w*{}\n\r\t, ]+from)?\s*("[^"]+"|'[^']+')\s*(?=$|;|\/\/|\/\*)/gm;
  46414. function scanImports(config) {
  46415. const start = performance$1.now();
  46416. const deps = {};
  46417. const missing = {};
  46418. let entries;
  46419. const scanContext = { cancelled: false };
  46420. const esbuildContext = computeEntries(
  46421. config
  46422. ).then((computedEntries) => {
  46423. entries = computedEntries;
  46424. if (!entries.length) {
  46425. if (!config.optimizeDeps.entries && !config.optimizeDeps.include) {
  46426. config.logger.warn(
  46427. colors$1.yellow(
  46428. "(!) Could not auto-determine entry point from rollupOptions or html files and there are no explicit optimizeDeps.include patterns. Skipping dependency pre-bundling."
  46429. )
  46430. );
  46431. }
  46432. return;
  46433. }
  46434. if (scanContext.cancelled) return;
  46435. debug$9?.(
  46436. `Crawling dependencies using entries: ${entries.map((entry) => `
  46437. ${colors$1.dim(entry)}`).join("")}`
  46438. );
  46439. return prepareEsbuildScanner(config, entries, deps, missing, scanContext);
  46440. });
  46441. const result = esbuildContext.then((context) => {
  46442. function disposeContext() {
  46443. return context?.dispose().catch((e) => {
  46444. config.logger.error("Failed to dispose esbuild context", { error: e });
  46445. });
  46446. }
  46447. if (!context || scanContext?.cancelled) {
  46448. disposeContext();
  46449. return { deps: {}, missing: {} };
  46450. }
  46451. return context.rebuild().then(() => {
  46452. return {
  46453. // Ensure a fixed order so hashes are stable and improve logs
  46454. deps: orderedDependencies(deps),
  46455. missing
  46456. };
  46457. }).finally(() => {
  46458. return disposeContext();
  46459. });
  46460. }).catch(async (e) => {
  46461. if (e.errors && e.message.includes("The build was canceled")) {
  46462. return { deps: {}, missing: {} };
  46463. }
  46464. const prependMessage = colors$1.red(` Failed to scan for dependencies from entries:
  46465. ${entries.join("\n")}
  46466. `);
  46467. if (e.errors) {
  46468. const msgs = await formatMessages(e.errors, {
  46469. kind: "error",
  46470. color: true
  46471. });
  46472. e.message = prependMessage + msgs.join("\n");
  46473. } else {
  46474. e.message = prependMessage + e.message;
  46475. }
  46476. throw e;
  46477. }).finally(() => {
  46478. if (debug$9) {
  46479. const duration = (performance$1.now() - start).toFixed(2);
  46480. const depsStr = Object.keys(orderedDependencies(deps)).sort().map((id) => `
  46481. ${colors$1.cyan(id)} -> ${colors$1.dim(deps[id])}`).join("") || colors$1.dim("no dependencies found");
  46482. debug$9(`Scan completed in ${duration}ms: ${depsStr}`);
  46483. }
  46484. });
  46485. return {
  46486. cancel: async () => {
  46487. scanContext.cancelled = true;
  46488. return esbuildContext.then((context) => context?.cancel());
  46489. },
  46490. result
  46491. };
  46492. }
  46493. async function computeEntries(config) {
  46494. let entries = [];
  46495. const explicitEntryPatterns = config.optimizeDeps.entries;
  46496. const buildInput = config.build.rollupOptions?.input;
  46497. if (explicitEntryPatterns) {
  46498. entries = await globEntries(explicitEntryPatterns, config);
  46499. } else if (buildInput) {
  46500. const resolvePath = (p) => path$n.resolve(config.root, p);
  46501. if (typeof buildInput === "string") {
  46502. entries = [resolvePath(buildInput)];
  46503. } else if (Array.isArray(buildInput)) {
  46504. entries = buildInput.map(resolvePath);
  46505. } else if (isObject$1(buildInput)) {
  46506. entries = Object.values(buildInput).map(resolvePath);
  46507. } else {
  46508. throw new Error("invalid rollupOptions.input value.");
  46509. }
  46510. } else {
  46511. entries = await globEntries("**/*.html", config);
  46512. }
  46513. entries = entries.filter(
  46514. (entry) => isScannable(entry, config.optimizeDeps.extensions) && fs__default.existsSync(entry)
  46515. );
  46516. return entries;
  46517. }
  46518. async function prepareEsbuildScanner(config, entries, deps, missing, scanContext) {
  46519. const container = await createPluginContainer(config);
  46520. if (scanContext?.cancelled) return;
  46521. const plugin = esbuildScanPlugin(config, container, deps, missing, entries);
  46522. const { plugins = [], ...esbuildOptions } = config.optimizeDeps?.esbuildOptions ?? {};
  46523. let tsconfigRaw = esbuildOptions.tsconfigRaw;
  46524. if (!tsconfigRaw && !esbuildOptions.tsconfig) {
  46525. const tsconfigResult = await loadTsconfigJsonForFile(
  46526. path$n.join(config.root, "_dummy.js")
  46527. );
  46528. if (tsconfigResult.compilerOptions?.experimentalDecorators) {
  46529. tsconfigRaw = { compilerOptions: { experimentalDecorators: true } };
  46530. }
  46531. }
  46532. return await esbuild.context({
  46533. absWorkingDir: process.cwd(),
  46534. write: false,
  46535. stdin: {
  46536. contents: entries.map((e) => `import ${JSON.stringify(e)}`).join("\n"),
  46537. loader: "js"
  46538. },
  46539. bundle: true,
  46540. format: "esm",
  46541. logLevel: "silent",
  46542. plugins: [...plugins, plugin],
  46543. ...esbuildOptions,
  46544. tsconfigRaw
  46545. });
  46546. }
  46547. function orderedDependencies(deps) {
  46548. const depsList = Object.entries(deps);
  46549. depsList.sort((a, b) => a[0].localeCompare(b[0]));
  46550. return Object.fromEntries(depsList);
  46551. }
  46552. function globEntries(pattern, config) {
  46553. const resolvedPatterns = arraify(pattern);
  46554. if (resolvedPatterns.every((str) => !glob.isDynamicPattern(str))) {
  46555. return resolvedPatterns.map(
  46556. (p) => normalizePath$3(path$n.resolve(config.root, p))
  46557. );
  46558. }
  46559. return glob(pattern, {
  46560. cwd: config.root,
  46561. ignore: [
  46562. "**/node_modules/**",
  46563. `**/${config.build.outDir}/**`,
  46564. // if there aren't explicit entries, also ignore other common folders
  46565. ...config.optimizeDeps.entries ? [] : [`**/__tests__/**`, `**/coverage/**`]
  46566. ],
  46567. absolute: true,
  46568. suppressErrors: true
  46569. // suppress EACCES errors
  46570. });
  46571. }
  46572. const scriptRE = /(<script(?:\s+[a-z_:][-\w:]*(?:\s*=\s*(?:"[^"]*"|'[^']*'|[^"'<>=\s]+))?)*\s*>)(.*?)<\/script>/gis;
  46573. const commentRE = /<!--.*?-->/gs;
  46574. const srcRE = /\bsrc\s*=\s*(?:"([^"]+)"|'([^']+)'|([^\s'">]+))/i;
  46575. const typeRE = /\btype\s*=\s*(?:"([^"]+)"|'([^']+)'|([^\s'">]+))/i;
  46576. const langRE = /\blang\s*=\s*(?:"([^"]+)"|'([^']+)'|([^\s'">]+))/i;
  46577. const svelteScriptModuleRE = /\bcontext\s*=\s*(?:"([^"]+)"|'([^']+)'|([^\s'">]+))/i;
  46578. const svelteModuleRE = /\smodule\b/i;
  46579. function esbuildScanPlugin(config, container, depImports, missing, entries) {
  46580. const seen = /* @__PURE__ */ new Map();
  46581. const resolve = async (id, importer, options) => {
  46582. const key = id + (importer && path$n.dirname(importer));
  46583. if (seen.has(key)) {
  46584. return seen.get(key);
  46585. }
  46586. const resolved = await container.resolveId(
  46587. id,
  46588. importer && normalizePath$3(importer),
  46589. {
  46590. ...options,
  46591. scan: true
  46592. }
  46593. );
  46594. const res = resolved?.id;
  46595. seen.set(key, res);
  46596. return res;
  46597. };
  46598. const include = config.optimizeDeps?.include;
  46599. const exclude = [
  46600. ...config.optimizeDeps?.exclude || [],
  46601. "@vite/client",
  46602. "@vite/env"
  46603. ];
  46604. const isUnlessEntry = (path2) => !entries.includes(path2);
  46605. const externalUnlessEntry = ({ path: path2 }) => ({
  46606. path: path2,
  46607. external: isUnlessEntry(path2)
  46608. });
  46609. const doTransformGlobImport = async (contents, id, loader) => {
  46610. let transpiledContents;
  46611. if (loader !== "js") {
  46612. transpiledContents = (await transform$1(contents, { loader })).code;
  46613. } else {
  46614. transpiledContents = contents;
  46615. }
  46616. const result = await transformGlobImport(
  46617. transpiledContents,
  46618. id,
  46619. config.root,
  46620. resolve
  46621. );
  46622. return result?.s.toString() || transpiledContents;
  46623. };
  46624. return {
  46625. name: "vite:dep-scan",
  46626. setup(build) {
  46627. const scripts = {};
  46628. build.onResolve({ filter: externalRE }, ({ path: path2 }) => ({
  46629. path: path2,
  46630. external: true
  46631. }));
  46632. build.onResolve({ filter: dataUrlRE }, ({ path: path2 }) => ({
  46633. path: path2,
  46634. external: true
  46635. }));
  46636. build.onResolve({ filter: virtualModuleRE }, ({ path: path2 }) => {
  46637. return {
  46638. // strip prefix to get valid filesystem path so esbuild can resolve imports in the file
  46639. path: path2.replace(virtualModulePrefix, ""),
  46640. namespace: "script"
  46641. };
  46642. });
  46643. build.onLoad({ filter: /.*/, namespace: "script" }, ({ path: path2 }) => {
  46644. return scripts[path2];
  46645. });
  46646. build.onResolve({ filter: htmlTypesRE }, async ({ path: path2, importer }) => {
  46647. const resolved = await resolve(path2, importer);
  46648. if (!resolved) return;
  46649. if (isInNodeModules$1(resolved) && isOptimizable(resolved, config.optimizeDeps))
  46650. return;
  46651. return {
  46652. path: resolved,
  46653. namespace: "html"
  46654. };
  46655. });
  46656. const htmlTypeOnLoadCallback = async ({ path: p }) => {
  46657. let raw = await fsp.readFile(p, "utf-8");
  46658. raw = raw.replace(commentRE, "<!---->");
  46659. const isHtml = p.endsWith(".html");
  46660. let js = "";
  46661. let scriptId = 0;
  46662. const matches = raw.matchAll(scriptRE);
  46663. for (const [, openTag, content] of matches) {
  46664. const typeMatch = typeRE.exec(openTag);
  46665. const type = typeMatch && (typeMatch[1] || typeMatch[2] || typeMatch[3]);
  46666. const langMatch = langRE.exec(openTag);
  46667. const lang = langMatch && (langMatch[1] || langMatch[2] || langMatch[3]);
  46668. if (isHtml && type !== "module") {
  46669. continue;
  46670. }
  46671. if (type && !(type.includes("javascript") || type.includes("ecmascript") || type === "module")) {
  46672. continue;
  46673. }
  46674. let loader = "js";
  46675. if (lang === "ts" || lang === "tsx" || lang === "jsx") {
  46676. loader = lang;
  46677. } else if (p.endsWith(".astro")) {
  46678. loader = "ts";
  46679. }
  46680. const srcMatch = srcRE.exec(openTag);
  46681. if (srcMatch) {
  46682. const src = srcMatch[1] || srcMatch[2] || srcMatch[3];
  46683. js += `import ${JSON.stringify(src)}
  46684. `;
  46685. } else if (content.trim()) {
  46686. const contents = content + (loader.startsWith("ts") ? extractImportPaths(content) : "");
  46687. const key = `${p}?id=${scriptId++}`;
  46688. if (contents.includes("import.meta.glob")) {
  46689. scripts[key] = {
  46690. loader: "js",
  46691. // since it is transpiled
  46692. contents: await doTransformGlobImport(contents, p, loader),
  46693. resolveDir: normalizePath$3(path$n.dirname(p)),
  46694. pluginData: {
  46695. htmlType: { loader }
  46696. }
  46697. };
  46698. } else {
  46699. scripts[key] = {
  46700. loader,
  46701. contents,
  46702. resolveDir: normalizePath$3(path$n.dirname(p)),
  46703. pluginData: {
  46704. htmlType: { loader }
  46705. }
  46706. };
  46707. }
  46708. const virtualModulePath = JSON.stringify(virtualModulePrefix + key);
  46709. let addedImport = false;
  46710. if (p.endsWith(".svelte")) {
  46711. let isModule = svelteModuleRE.test(openTag);
  46712. if (!isModule) {
  46713. const contextMatch = svelteScriptModuleRE.exec(openTag);
  46714. const context = contextMatch && (contextMatch[1] || contextMatch[2] || contextMatch[3]);
  46715. isModule = context === "module";
  46716. }
  46717. if (!isModule) {
  46718. addedImport = true;
  46719. js += `import ${virtualModulePath}
  46720. `;
  46721. }
  46722. }
  46723. if (!addedImport) {
  46724. js += `export * from ${virtualModulePath}
  46725. `;
  46726. }
  46727. }
  46728. }
  46729. if (!p.endsWith(".vue") || !js.includes("export default")) {
  46730. js += "\nexport default {}";
  46731. }
  46732. return {
  46733. loader: "js",
  46734. contents: js
  46735. };
  46736. };
  46737. build.onLoad(
  46738. { filter: htmlTypesRE, namespace: "html" },
  46739. htmlTypeOnLoadCallback
  46740. );
  46741. build.onLoad(
  46742. { filter: htmlTypesRE, namespace: "file" },
  46743. htmlTypeOnLoadCallback
  46744. );
  46745. build.onResolve(
  46746. {
  46747. // avoid matching windows volume
  46748. filter: /^[\w@][^:]/
  46749. },
  46750. async ({ path: id, importer, pluginData }) => {
  46751. if (moduleListContains(exclude, id)) {
  46752. return externalUnlessEntry({ path: id });
  46753. }
  46754. if (depImports[id]) {
  46755. return externalUnlessEntry({ path: id });
  46756. }
  46757. const resolved = await resolve(id, importer, {
  46758. custom: {
  46759. depScan: { loader: pluginData?.htmlType?.loader }
  46760. }
  46761. });
  46762. if (resolved) {
  46763. if (shouldExternalizeDep(resolved, id)) {
  46764. return externalUnlessEntry({ path: id });
  46765. }
  46766. if (isInNodeModules$1(resolved) || include?.includes(id)) {
  46767. if (isOptimizable(resolved, config.optimizeDeps)) {
  46768. depImports[id] = resolved;
  46769. }
  46770. return externalUnlessEntry({ path: id });
  46771. } else if (isScannable(resolved, config.optimizeDeps.extensions)) {
  46772. const namespace = htmlTypesRE.test(resolved) ? "html" : void 0;
  46773. return {
  46774. path: path$n.resolve(resolved),
  46775. namespace
  46776. };
  46777. } else {
  46778. return externalUnlessEntry({ path: id });
  46779. }
  46780. } else {
  46781. missing[id] = normalizePath$3(importer);
  46782. }
  46783. }
  46784. );
  46785. const setupExternalize = (filter, doExternalize) => {
  46786. build.onResolve({ filter }, ({ path: path2 }) => {
  46787. return {
  46788. path: path2,
  46789. external: doExternalize(path2)
  46790. };
  46791. });
  46792. };
  46793. setupExternalize(CSS_LANGS_RE, isUnlessEntry);
  46794. setupExternalize(/\.(json|json5|wasm)$/, isUnlessEntry);
  46795. setupExternalize(
  46796. new RegExp(`\\.(${KNOWN_ASSET_TYPES.join("|")})$`),
  46797. isUnlessEntry
  46798. );
  46799. setupExternalize(SPECIAL_QUERY_RE, () => true);
  46800. build.onResolve(
  46801. {
  46802. filter: /.*/
  46803. },
  46804. async ({ path: id, importer, pluginData }) => {
  46805. const resolved = await resolve(id, importer, {
  46806. custom: {
  46807. depScan: { loader: pluginData?.htmlType?.loader }
  46808. }
  46809. });
  46810. if (resolved) {
  46811. if (shouldExternalizeDep(resolved, id) || !isScannable(resolved, config.optimizeDeps.extensions)) {
  46812. return externalUnlessEntry({ path: id });
  46813. }
  46814. const namespace = htmlTypesRE.test(resolved) ? "html" : void 0;
  46815. return {
  46816. path: path$n.resolve(cleanUrl(resolved)),
  46817. namespace
  46818. };
  46819. } else {
  46820. return externalUnlessEntry({ path: id });
  46821. }
  46822. }
  46823. );
  46824. build.onLoad({ filter: JS_TYPES_RE }, async ({ path: id }) => {
  46825. let ext = path$n.extname(id).slice(1);
  46826. if (ext === "mjs") ext = "js";
  46827. let contents = await fsp.readFile(id, "utf-8");
  46828. if (ext.endsWith("x") && config.esbuild && config.esbuild.jsxInject) {
  46829. contents = config.esbuild.jsxInject + `
  46830. ` + contents;
  46831. }
  46832. const loader = config.optimizeDeps?.esbuildOptions?.loader?.[`.${ext}`] || ext;
  46833. if (contents.includes("import.meta.glob")) {
  46834. return {
  46835. loader: "js",
  46836. // since it is transpiled,
  46837. contents: await doTransformGlobImport(contents, id, loader)
  46838. };
  46839. }
  46840. return {
  46841. loader,
  46842. contents
  46843. };
  46844. });
  46845. build.onLoad({ filter: /.*/, namespace: "file" }, () => {
  46846. return {
  46847. loader: "js",
  46848. contents: "export default {}"
  46849. };
  46850. });
  46851. }
  46852. };
  46853. }
  46854. function extractImportPaths(code) {
  46855. code = code.replace(multilineCommentsRE, "/* */").replace(singlelineCommentsRE, "");
  46856. let js = "";
  46857. let m;
  46858. importsRE.lastIndex = 0;
  46859. while ((m = importsRE.exec(code)) != null) {
  46860. js += `
  46861. import ${m[1]}`;
  46862. }
  46863. return js;
  46864. }
  46865. function shouldExternalizeDep(resolvedId, rawId) {
  46866. if (!path$n.isAbsolute(resolvedId)) {
  46867. return true;
  46868. }
  46869. if (resolvedId === rawId || resolvedId.includes("\0")) {
  46870. return true;
  46871. }
  46872. return false;
  46873. }
  46874. function isScannable(id, extensions) {
  46875. return JS_TYPES_RE.test(id) || htmlTypesRE.test(id) || extensions?.includes(path$n.extname(id)) || false;
  46876. }
  46877. function createOptimizeDepsIncludeResolver(config, ssr) {
  46878. const resolve = config.createResolver({
  46879. asSrc: false,
  46880. scan: true,
  46881. ssrOptimizeCheck: ssr,
  46882. ssrConfig: config.ssr,
  46883. packageCache: /* @__PURE__ */ new Map()
  46884. });
  46885. return async (id) => {
  46886. const lastArrowIndex = id.lastIndexOf(">");
  46887. if (lastArrowIndex === -1) {
  46888. return await resolve(id, void 0, void 0, ssr);
  46889. }
  46890. const nestedRoot = id.substring(0, lastArrowIndex).trim();
  46891. const nestedPath = id.substring(lastArrowIndex + 1).trim();
  46892. const basedir = nestedResolveBasedir(
  46893. nestedRoot,
  46894. config.root,
  46895. config.resolve.preserveSymlinks
  46896. );
  46897. return await resolve(
  46898. nestedPath,
  46899. path$n.resolve(basedir, "package.json"),
  46900. void 0,
  46901. ssr
  46902. );
  46903. };
  46904. }
  46905. function expandGlobIds(id, config) {
  46906. const pkgName = getNpmPackageName(id);
  46907. if (!pkgName) return [];
  46908. const pkgData = resolvePackageData(
  46909. pkgName,
  46910. config.root,
  46911. config.resolve.preserveSymlinks,
  46912. config.packageCache
  46913. );
  46914. if (!pkgData) return [];
  46915. const pattern = "." + id.slice(pkgName.length);
  46916. const exports = pkgData.data.exports;
  46917. if (exports) {
  46918. if (typeof exports === "string" || Array.isArray(exports)) {
  46919. return [pkgName];
  46920. }
  46921. const possibleExportPaths = [];
  46922. for (const key in exports) {
  46923. if (key[0] === ".") {
  46924. if (key.includes("*")) {
  46925. const exportsValue = getFirstExportStringValue(exports[key]);
  46926. if (!exportsValue) continue;
  46927. const exportValuePattern = exportsValue.replace(/\*/g, "**/*");
  46928. const exportsValueGlobRe = new RegExp(
  46929. exportsValue.split("*").map(escapeRegex).join("(.*)")
  46930. );
  46931. possibleExportPaths.push(
  46932. ...glob.sync(exportValuePattern, {
  46933. cwd: pkgData.dir,
  46934. ignore: ["node_modules"]
  46935. }).map((filePath) => {
  46936. if (exportsValue.startsWith("./") && !filePath.startsWith("./")) {
  46937. filePath = "./" + filePath;
  46938. }
  46939. const matched2 = exportsValueGlobRe.exec(slash$1(filePath));
  46940. if (matched2) {
  46941. let allGlobSame = matched2.length === 2;
  46942. if (!allGlobSame) {
  46943. allGlobSame = true;
  46944. for (let i = 2; i < matched2.length; i++) {
  46945. if (matched2[i] !== matched2[i - 1]) {
  46946. allGlobSame = false;
  46947. break;
  46948. }
  46949. }
  46950. }
  46951. if (allGlobSame) {
  46952. return key.replace("*", matched2[1]).slice(2);
  46953. }
  46954. }
  46955. return "";
  46956. }).filter(Boolean)
  46957. );
  46958. } else {
  46959. possibleExportPaths.push(key.slice(2));
  46960. }
  46961. }
  46962. }
  46963. const matched = micromatch$2(possibleExportPaths, pattern).map(
  46964. (match) => path$n.posix.join(pkgName, match)
  46965. );
  46966. matched.unshift(pkgName);
  46967. return matched;
  46968. } else {
  46969. const matched = glob.sync(pattern, { cwd: pkgData.dir, ignore: ["node_modules"] }).map((match) => path$n.posix.join(pkgName, slash$1(match)));
  46970. matched.unshift(pkgName);
  46971. return matched;
  46972. }
  46973. }
  46974. function getFirstExportStringValue(obj) {
  46975. if (typeof obj === "string") {
  46976. return obj;
  46977. } else if (Array.isArray(obj)) {
  46978. return obj[0];
  46979. } else {
  46980. for (const key in obj) {
  46981. return getFirstExportStringValue(obj[key]);
  46982. }
  46983. }
  46984. }
  46985. function nestedResolveBasedir(id, basedir, preserveSymlinks = false) {
  46986. const pkgs = id.split(">").map((pkg) => pkg.trim());
  46987. for (const pkg of pkgs) {
  46988. basedir = resolvePackageData(pkg, basedir, preserveSymlinks)?.dir || basedir;
  46989. }
  46990. return basedir;
  46991. }
  46992. const debug$8 = createDebugger("vite:deps");
  46993. const debounceMs = 100;
  46994. const depsOptimizerMap = /* @__PURE__ */ new WeakMap();
  46995. const devSsrDepsOptimizerMap = /* @__PURE__ */ new WeakMap();
  46996. function getDepsOptimizer(config, ssr) {
  46997. return (ssr ? devSsrDepsOptimizerMap : depsOptimizerMap).get(config);
  46998. }
  46999. async function initDepsOptimizer(config, server) {
  47000. if (!getDepsOptimizer(config, false)) {
  47001. await createDepsOptimizer(config, server);
  47002. }
  47003. }
  47004. let creatingDevSsrOptimizer;
  47005. async function initDevSsrDepsOptimizer(config, server) {
  47006. if (getDepsOptimizer(config, true)) {
  47007. return;
  47008. }
  47009. if (creatingDevSsrOptimizer) {
  47010. return creatingDevSsrOptimizer;
  47011. }
  47012. creatingDevSsrOptimizer = async function() {
  47013. const ssr = false;
  47014. if (!getDepsOptimizer(config, ssr)) {
  47015. await initDepsOptimizer(config, server);
  47016. }
  47017. await getDepsOptimizer(config, ssr).scanProcessing;
  47018. await createDevSsrDepsOptimizer(config);
  47019. creatingDevSsrOptimizer = void 0;
  47020. }();
  47021. return await creatingDevSsrOptimizer;
  47022. }
  47023. async function createDepsOptimizer(config, server) {
  47024. const { logger } = config;
  47025. const ssr = false;
  47026. const sessionTimestamp = Date.now().toString();
  47027. const cachedMetadata = await loadCachedDepOptimizationMetadata(config, ssr);
  47028. let debounceProcessingHandle;
  47029. let closed = false;
  47030. let metadata = cachedMetadata || initDepsOptimizerMetadata(config, ssr, sessionTimestamp);
  47031. const options = getDepOptimizationConfig(config, ssr);
  47032. const { noDiscovery, holdUntilCrawlEnd } = options;
  47033. const depsOptimizer = {
  47034. metadata,
  47035. registerMissingImport,
  47036. run: () => debouncedProcessing(0),
  47037. isOptimizedDepFile: createIsOptimizedDepFile(config),
  47038. isOptimizedDepUrl: createIsOptimizedDepUrl(config),
  47039. getOptimizedDepId: (depInfo) => `${depInfo.file}?v=${depInfo.browserHash}`,
  47040. close,
  47041. options
  47042. };
  47043. depsOptimizerMap.set(config, depsOptimizer);
  47044. let newDepsDiscovered = false;
  47045. let newDepsToLog = [];
  47046. let newDepsToLogHandle;
  47047. const logNewlyDiscoveredDeps = () => {
  47048. if (newDepsToLog.length) {
  47049. logger.info(
  47050. colors$1.green(
  47051. `\u2728 new dependencies optimized: ${depsLogString(newDepsToLog)}`
  47052. ),
  47053. {
  47054. timestamp: true
  47055. }
  47056. );
  47057. newDepsToLog = [];
  47058. }
  47059. };
  47060. let discoveredDepsWhileScanning = [];
  47061. const logDiscoveredDepsWhileScanning = () => {
  47062. if (discoveredDepsWhileScanning.length) {
  47063. logger.info(
  47064. colors$1.green(
  47065. `\u2728 discovered while scanning: ${depsLogString(
  47066. discoveredDepsWhileScanning
  47067. )}`
  47068. ),
  47069. {
  47070. timestamp: true
  47071. }
  47072. );
  47073. discoveredDepsWhileScanning = [];
  47074. }
  47075. };
  47076. let depOptimizationProcessing = promiseWithResolvers();
  47077. let depOptimizationProcessingQueue = [];
  47078. const resolveEnqueuedProcessingPromises = () => {
  47079. for (const processing of depOptimizationProcessingQueue) {
  47080. processing.resolve();
  47081. }
  47082. depOptimizationProcessingQueue = [];
  47083. };
  47084. let enqueuedRerun;
  47085. let currentlyProcessing = false;
  47086. let firstRunCalled = !!cachedMetadata;
  47087. let warnAboutMissedDependencies = false;
  47088. let waitingForCrawlEnd = false;
  47089. if (!cachedMetadata) {
  47090. server._onCrawlEnd(onCrawlEnd);
  47091. waitingForCrawlEnd = true;
  47092. }
  47093. let optimizationResult;
  47094. let discover;
  47095. async function close() {
  47096. closed = true;
  47097. await Promise.allSettled([
  47098. discover?.cancel(),
  47099. depsOptimizer.scanProcessing,
  47100. optimizationResult?.cancel()
  47101. ]);
  47102. }
  47103. if (!cachedMetadata) {
  47104. currentlyProcessing = true;
  47105. const manuallyIncludedDeps = {};
  47106. await addManuallyIncludedOptimizeDeps(manuallyIncludedDeps, config, ssr);
  47107. const manuallyIncludedDepsInfo = toDiscoveredDependencies(
  47108. config,
  47109. manuallyIncludedDeps,
  47110. ssr,
  47111. sessionTimestamp
  47112. );
  47113. for (const depInfo of Object.values(manuallyIncludedDepsInfo)) {
  47114. addOptimizedDepInfo(metadata, "discovered", {
  47115. ...depInfo,
  47116. processing: depOptimizationProcessing.promise
  47117. });
  47118. newDepsDiscovered = true;
  47119. }
  47120. if (noDiscovery) {
  47121. runOptimizer();
  47122. } else {
  47123. depsOptimizer.scanProcessing = new Promise((resolve) => {
  47124. (async () => {
  47125. try {
  47126. debug$8?.(colors$1.green(`scanning for dependencies...`));
  47127. discover = discoverProjectDependencies(config);
  47128. const deps = await discover.result;
  47129. discover = void 0;
  47130. const manuallyIncluded = Object.keys(manuallyIncludedDepsInfo);
  47131. discoveredDepsWhileScanning.push(
  47132. ...Object.keys(metadata.discovered).filter(
  47133. (dep) => !deps[dep] && !manuallyIncluded.includes(dep)
  47134. )
  47135. );
  47136. for (const id of Object.keys(deps)) {
  47137. if (!metadata.discovered[id]) {
  47138. addMissingDep(id, deps[id]);
  47139. }
  47140. }
  47141. const knownDeps = prepareKnownDeps();
  47142. startNextDiscoveredBatch();
  47143. optimizationResult = runOptimizeDeps(config, knownDeps, ssr);
  47144. if (!holdUntilCrawlEnd) {
  47145. optimizationResult.result.then((result) => {
  47146. if (!waitingForCrawlEnd) return;
  47147. optimizationResult = void 0;
  47148. runOptimizer(result);
  47149. });
  47150. }
  47151. } catch (e) {
  47152. logger.error(e.stack || e.message);
  47153. } finally {
  47154. resolve();
  47155. depsOptimizer.scanProcessing = void 0;
  47156. }
  47157. })();
  47158. });
  47159. }
  47160. }
  47161. function startNextDiscoveredBatch() {
  47162. newDepsDiscovered = false;
  47163. depOptimizationProcessingQueue.push(depOptimizationProcessing);
  47164. depOptimizationProcessing = promiseWithResolvers();
  47165. }
  47166. function prepareKnownDeps() {
  47167. const knownDeps = {};
  47168. for (const dep of Object.keys(metadata.optimized)) {
  47169. knownDeps[dep] = { ...metadata.optimized[dep] };
  47170. }
  47171. for (const dep of Object.keys(metadata.discovered)) {
  47172. const { processing, ...info } = metadata.discovered[dep];
  47173. knownDeps[dep] = info;
  47174. }
  47175. return knownDeps;
  47176. }
  47177. async function runOptimizer(preRunResult) {
  47178. const isRerun = firstRunCalled;
  47179. firstRunCalled = true;
  47180. enqueuedRerun = void 0;
  47181. if (debounceProcessingHandle) clearTimeout(debounceProcessingHandle);
  47182. if (closed) {
  47183. currentlyProcessing = false;
  47184. return;
  47185. }
  47186. currentlyProcessing = true;
  47187. try {
  47188. let processingResult;
  47189. if (preRunResult) {
  47190. processingResult = preRunResult;
  47191. } else {
  47192. const knownDeps = prepareKnownDeps();
  47193. startNextDiscoveredBatch();
  47194. optimizationResult = runOptimizeDeps(config, knownDeps, ssr);
  47195. processingResult = await optimizationResult.result;
  47196. optimizationResult = void 0;
  47197. }
  47198. if (closed) {
  47199. currentlyProcessing = false;
  47200. processingResult.cancel();
  47201. resolveEnqueuedProcessingPromises();
  47202. return;
  47203. }
  47204. const newData = processingResult.metadata;
  47205. const needsInteropMismatch = findInteropMismatches(
  47206. metadata.discovered,
  47207. newData.optimized
  47208. );
  47209. const needsReload = needsInteropMismatch.length > 0 || metadata.hash !== newData.hash || Object.keys(metadata.optimized).some((dep) => {
  47210. return metadata.optimized[dep].fileHash !== newData.optimized[dep].fileHash;
  47211. });
  47212. const commitProcessing = async () => {
  47213. await processingResult.commit();
  47214. for (const id in metadata.discovered) {
  47215. if (!newData.optimized[id]) {
  47216. addOptimizedDepInfo(newData, "discovered", metadata.discovered[id]);
  47217. }
  47218. }
  47219. if (!needsReload) {
  47220. newData.browserHash = metadata.browserHash;
  47221. for (const dep in newData.chunks) {
  47222. newData.chunks[dep].browserHash = metadata.browserHash;
  47223. }
  47224. for (const dep in newData.optimized) {
  47225. newData.optimized[dep].browserHash = (metadata.optimized[dep] || metadata.discovered[dep]).browserHash;
  47226. }
  47227. }
  47228. for (const o in newData.optimized) {
  47229. const discovered = metadata.discovered[o];
  47230. if (discovered) {
  47231. const optimized = newData.optimized[o];
  47232. discovered.browserHash = optimized.browserHash;
  47233. discovered.fileHash = optimized.fileHash;
  47234. discovered.needsInterop = optimized.needsInterop;
  47235. discovered.processing = void 0;
  47236. }
  47237. }
  47238. if (isRerun) {
  47239. newDepsToLog.push(
  47240. ...Object.keys(newData.optimized).filter(
  47241. (dep) => !metadata.optimized[dep]
  47242. )
  47243. );
  47244. }
  47245. metadata = depsOptimizer.metadata = newData;
  47246. resolveEnqueuedProcessingPromises();
  47247. };
  47248. if (!needsReload) {
  47249. await commitProcessing();
  47250. if (!debug$8) {
  47251. if (newDepsToLogHandle) clearTimeout(newDepsToLogHandle);
  47252. newDepsToLogHandle = setTimeout(() => {
  47253. newDepsToLogHandle = void 0;
  47254. logNewlyDiscoveredDeps();
  47255. if (warnAboutMissedDependencies) {
  47256. logDiscoveredDepsWhileScanning();
  47257. logger.info(
  47258. colors$1.magenta(
  47259. `\u2757 add these dependencies to optimizeDeps.include to speed up cold start`
  47260. ),
  47261. { timestamp: true }
  47262. );
  47263. warnAboutMissedDependencies = false;
  47264. }
  47265. }, 2 * debounceMs);
  47266. } else {
  47267. debug$8(
  47268. colors$1.green(
  47269. `\u2728 ${!isRerun ? `dependencies optimized` : `optimized dependencies unchanged`}`
  47270. )
  47271. );
  47272. }
  47273. } else {
  47274. if (newDepsDiscovered) {
  47275. processingResult.cancel();
  47276. debug$8?.(
  47277. colors$1.green(
  47278. `\u2728 delaying reload as new dependencies have been found...`
  47279. )
  47280. );
  47281. } else {
  47282. await commitProcessing();
  47283. if (!debug$8) {
  47284. if (newDepsToLogHandle) clearTimeout(newDepsToLogHandle);
  47285. newDepsToLogHandle = void 0;
  47286. logNewlyDiscoveredDeps();
  47287. if (warnAboutMissedDependencies) {
  47288. logDiscoveredDepsWhileScanning();
  47289. logger.info(
  47290. colors$1.magenta(
  47291. `\u2757 add these dependencies to optimizeDeps.include to avoid a full page reload during cold start`
  47292. ),
  47293. { timestamp: true }
  47294. );
  47295. warnAboutMissedDependencies = false;
  47296. }
  47297. }
  47298. logger.info(
  47299. colors$1.green(`\u2728 optimized dependencies changed. reloading`),
  47300. {
  47301. timestamp: true
  47302. }
  47303. );
  47304. if (needsInteropMismatch.length > 0) {
  47305. logger.warn(
  47306. `Mixed ESM and CJS detected in ${colors$1.yellow(
  47307. needsInteropMismatch.join(", ")
  47308. )}, add ${needsInteropMismatch.length === 1 ? "it" : "them"} to optimizeDeps.needsInterop to speed up cold start`,
  47309. {
  47310. timestamp: true
  47311. }
  47312. );
  47313. }
  47314. fullReload();
  47315. }
  47316. }
  47317. } catch (e) {
  47318. logger.error(
  47319. colors$1.red(`error while updating dependencies:
  47320. ${e.stack}`),
  47321. { timestamp: true, error: e }
  47322. );
  47323. resolveEnqueuedProcessingPromises();
  47324. metadata.discovered = {};
  47325. }
  47326. currentlyProcessing = false;
  47327. enqueuedRerun?.();
  47328. }
  47329. function fullReload() {
  47330. server.moduleGraph.invalidateAll();
  47331. server.hot.send({
  47332. type: "full-reload",
  47333. path: "*"
  47334. });
  47335. }
  47336. async function rerun() {
  47337. const deps = Object.keys(metadata.discovered);
  47338. const depsString = depsLogString(deps);
  47339. debug$8?.(colors$1.green(`new dependencies found: ${depsString}`));
  47340. runOptimizer();
  47341. }
  47342. function getDiscoveredBrowserHash(hash, deps, missing) {
  47343. return getHash(
  47344. hash + JSON.stringify(deps) + JSON.stringify(missing) + sessionTimestamp
  47345. );
  47346. }
  47347. function registerMissingImport(id, resolved) {
  47348. const optimized = metadata.optimized[id];
  47349. if (optimized) {
  47350. return optimized;
  47351. }
  47352. const chunk = metadata.chunks[id];
  47353. if (chunk) {
  47354. return chunk;
  47355. }
  47356. let missing = metadata.discovered[id];
  47357. if (missing) {
  47358. return missing;
  47359. }
  47360. missing = addMissingDep(id, resolved);
  47361. if (!waitingForCrawlEnd) {
  47362. debouncedProcessing();
  47363. }
  47364. return missing;
  47365. }
  47366. function addMissingDep(id, resolved) {
  47367. newDepsDiscovered = true;
  47368. return addOptimizedDepInfo(metadata, "discovered", {
  47369. id,
  47370. file: getOptimizedDepPath(id, config, ssr),
  47371. src: resolved,
  47372. // Adding a browserHash to this missing dependency that is unique to
  47373. // the current state of known + missing deps. If its optimizeDeps run
  47374. // doesn't alter the bundled files of previous known dependencies,
  47375. // we don't need a full reload and this browserHash will be kept
  47376. browserHash: getDiscoveredBrowserHash(
  47377. metadata.hash,
  47378. depsFromOptimizedDepInfo(metadata.optimized),
  47379. depsFromOptimizedDepInfo(metadata.discovered)
  47380. ),
  47381. // loading of this pre-bundled dep needs to await for its processing
  47382. // promise to be resolved
  47383. processing: depOptimizationProcessing.promise,
  47384. exportsData: extractExportsData(resolved, config, ssr)
  47385. });
  47386. }
  47387. function debouncedProcessing(timeout = debounceMs) {
  47388. enqueuedRerun = void 0;
  47389. if (debounceProcessingHandle) clearTimeout(debounceProcessingHandle);
  47390. if (newDepsToLogHandle) clearTimeout(newDepsToLogHandle);
  47391. newDepsToLogHandle = void 0;
  47392. debounceProcessingHandle = setTimeout(() => {
  47393. debounceProcessingHandle = void 0;
  47394. enqueuedRerun = rerun;
  47395. if (!currentlyProcessing) {
  47396. enqueuedRerun();
  47397. }
  47398. }, timeout);
  47399. }
  47400. async function onCrawlEnd() {
  47401. waitingForCrawlEnd = false;
  47402. debug$8?.(colors$1.green(`\u2728 static imports crawl ended`));
  47403. if (closed) {
  47404. return;
  47405. }
  47406. await depsOptimizer.scanProcessing;
  47407. if (optimizationResult && !config.optimizeDeps.noDiscovery) {
  47408. const afterScanResult = optimizationResult.result;
  47409. optimizationResult = void 0;
  47410. const result = await afterScanResult;
  47411. currentlyProcessing = false;
  47412. const crawlDeps = Object.keys(metadata.discovered);
  47413. const scanDeps = Object.keys(result.metadata.optimized);
  47414. if (scanDeps.length === 0 && crawlDeps.length === 0) {
  47415. debug$8?.(
  47416. colors$1.green(
  47417. `\u2728 no dependencies found by the scanner or crawling static imports`
  47418. )
  47419. );
  47420. startNextDiscoveredBatch();
  47421. runOptimizer(result);
  47422. return;
  47423. }
  47424. const needsInteropMismatch = findInteropMismatches(
  47425. metadata.discovered,
  47426. result.metadata.optimized
  47427. );
  47428. const scannerMissedDeps = crawlDeps.some((dep) => !scanDeps.includes(dep));
  47429. const outdatedResult = needsInteropMismatch.length > 0 || scannerMissedDeps;
  47430. if (outdatedResult) {
  47431. result.cancel();
  47432. for (const dep of scanDeps) {
  47433. if (!crawlDeps.includes(dep)) {
  47434. addMissingDep(dep, result.metadata.optimized[dep].src);
  47435. }
  47436. }
  47437. if (scannerMissedDeps) {
  47438. debug$8?.(
  47439. colors$1.yellow(
  47440. `\u2728 new dependencies were found while crawling that weren't detected by the scanner`
  47441. )
  47442. );
  47443. }
  47444. debug$8?.(colors$1.green(`\u2728 re-running optimizer`));
  47445. debouncedProcessing(0);
  47446. } else {
  47447. debug$8?.(
  47448. colors$1.green(
  47449. `\u2728 using post-scan optimizer result, the scanner found every used dependency`
  47450. )
  47451. );
  47452. startNextDiscoveredBatch();
  47453. runOptimizer(result);
  47454. }
  47455. } else if (!holdUntilCrawlEnd) {
  47456. if (newDepsDiscovered) {
  47457. debug$8?.(
  47458. colors$1.green(
  47459. `\u2728 new dependencies were found while crawling static imports, re-running optimizer`
  47460. )
  47461. );
  47462. warnAboutMissedDependencies = true;
  47463. debouncedProcessing(0);
  47464. }
  47465. } else {
  47466. const crawlDeps = Object.keys(metadata.discovered);
  47467. currentlyProcessing = false;
  47468. if (crawlDeps.length === 0) {
  47469. debug$8?.(
  47470. colors$1.green(
  47471. `\u2728 no dependencies found while crawling the static imports`
  47472. )
  47473. );
  47474. firstRunCalled = true;
  47475. }
  47476. debouncedProcessing(0);
  47477. }
  47478. }
  47479. }
  47480. async function createDevSsrDepsOptimizer(config) {
  47481. const metadata = await optimizeServerSsrDeps(config);
  47482. const depsOptimizer = {
  47483. metadata,
  47484. isOptimizedDepFile: createIsOptimizedDepFile(config),
  47485. isOptimizedDepUrl: createIsOptimizedDepUrl(config),
  47486. getOptimizedDepId: (depInfo) => `${depInfo.file}?v=${depInfo.browserHash}`,
  47487. registerMissingImport: () => {
  47488. throw new Error(
  47489. "Vite Internal Error: registerMissingImport is not supported in dev SSR"
  47490. );
  47491. },
  47492. // noop, there is no scanning during dev SSR
  47493. // the optimizer blocks the server start
  47494. run: () => {
  47495. },
  47496. close: async () => {
  47497. },
  47498. options: config.ssr.optimizeDeps
  47499. };
  47500. devSsrDepsOptimizerMap.set(config, depsOptimizer);
  47501. }
  47502. function findInteropMismatches(discovered, optimized) {
  47503. const needsInteropMismatch = [];
  47504. for (const dep in discovered) {
  47505. const discoveredDepInfo = discovered[dep];
  47506. if (discoveredDepInfo.needsInterop === void 0) continue;
  47507. const depInfo = optimized[dep];
  47508. if (!depInfo) continue;
  47509. if (depInfo.needsInterop !== discoveredDepInfo.needsInterop) {
  47510. needsInteropMismatch.push(dep);
  47511. debug$8?.(colors$1.cyan(`\u2728 needsInterop mismatch detected for ${dep}`));
  47512. }
  47513. }
  47514. return needsInteropMismatch;
  47515. }
  47516. const debug$7 = createDebugger("vite:deps");
  47517. const jsExtensionRE = /\.js$/i;
  47518. const jsMapExtensionRE = /\.js\.map$/i;
  47519. async function optimizeDeps(config, force = config.optimizeDeps.force, asCommand = false) {
  47520. const log = asCommand ? config.logger.info : debug$7;
  47521. const ssr = false;
  47522. const cachedMetadata = await loadCachedDepOptimizationMetadata(
  47523. config,
  47524. ssr,
  47525. force,
  47526. asCommand
  47527. );
  47528. if (cachedMetadata) {
  47529. return cachedMetadata;
  47530. }
  47531. const deps = await discoverProjectDependencies(config).result;
  47532. await addManuallyIncludedOptimizeDeps(deps, config, ssr);
  47533. const depsString = depsLogString(Object.keys(deps));
  47534. log?.(colors$1.green(`Optimizing dependencies:
  47535. ${depsString}`));
  47536. const depsInfo = toDiscoveredDependencies(config, deps, ssr);
  47537. const result = await runOptimizeDeps(config, depsInfo, ssr).result;
  47538. await result.commit();
  47539. return result.metadata;
  47540. }
  47541. async function optimizeServerSsrDeps(config) {
  47542. const ssr = true;
  47543. const cachedMetadata = await loadCachedDepOptimizationMetadata(
  47544. config,
  47545. ssr,
  47546. config.optimizeDeps.force,
  47547. false
  47548. );
  47549. if (cachedMetadata) {
  47550. return cachedMetadata;
  47551. }
  47552. const deps = {};
  47553. await addManuallyIncludedOptimizeDeps(deps, config, ssr);
  47554. const depsInfo = toDiscoveredDependencies(config, deps, ssr);
  47555. const result = await runOptimizeDeps(config, depsInfo, ssr).result;
  47556. await result.commit();
  47557. return result.metadata;
  47558. }
  47559. function initDepsOptimizerMetadata(config, ssr, timestamp) {
  47560. const { lockfileHash, configHash, hash } = getDepHash(config, ssr);
  47561. return {
  47562. hash,
  47563. lockfileHash,
  47564. configHash,
  47565. browserHash: getOptimizedBrowserHash(hash, {}, timestamp),
  47566. optimized: {},
  47567. chunks: {},
  47568. discovered: {},
  47569. depInfoList: []
  47570. };
  47571. }
  47572. function addOptimizedDepInfo(metadata, type, depInfo) {
  47573. metadata[type][depInfo.id] = depInfo;
  47574. metadata.depInfoList.push(depInfo);
  47575. return depInfo;
  47576. }
  47577. let firstLoadCachedDepOptimizationMetadata = true;
  47578. async function loadCachedDepOptimizationMetadata(config, ssr, force = config.optimizeDeps.force, asCommand = false) {
  47579. const log = asCommand ? config.logger.info : debug$7;
  47580. if (firstLoadCachedDepOptimizationMetadata) {
  47581. firstLoadCachedDepOptimizationMetadata = false;
  47582. setTimeout(() => cleanupDepsCacheStaleDirs(config), 0);
  47583. }
  47584. const depsCacheDir = getDepsCacheDir(config, ssr);
  47585. if (!force) {
  47586. let cachedMetadata;
  47587. try {
  47588. const cachedMetadataPath = path$n.join(depsCacheDir, METADATA_FILENAME);
  47589. cachedMetadata = parseDepsOptimizerMetadata(
  47590. await fsp.readFile(cachedMetadataPath, "utf-8"),
  47591. depsCacheDir
  47592. );
  47593. } catch (e) {
  47594. }
  47595. if (cachedMetadata) {
  47596. if (cachedMetadata.lockfileHash !== getLockfileHash(config)) {
  47597. config.logger.info(
  47598. "Re-optimizing dependencies because lockfile has changed"
  47599. );
  47600. } else if (cachedMetadata.configHash !== getConfigHash(config, ssr)) {
  47601. config.logger.info(
  47602. "Re-optimizing dependencies because vite config has changed"
  47603. );
  47604. } else {
  47605. log?.("Hash is consistent. Skipping. Use --force to override.");
  47606. return cachedMetadata;
  47607. }
  47608. }
  47609. } else {
  47610. config.logger.info("Forced re-optimization of dependencies");
  47611. }
  47612. debug$7?.(colors$1.green(`removing old cache dir ${depsCacheDir}`));
  47613. await fsp.rm(depsCacheDir, { recursive: true, force: true });
  47614. }
  47615. function discoverProjectDependencies(config) {
  47616. const { cancel, result } = scanImports(config);
  47617. return {
  47618. cancel,
  47619. result: result.then(({ deps, missing }) => {
  47620. const missingIds = Object.keys(missing);
  47621. if (missingIds.length) {
  47622. throw new Error(
  47623. `The following dependencies are imported but could not be resolved:
  47624. ${missingIds.map(
  47625. (id) => `${colors$1.cyan(id)} ${colors$1.white(
  47626. colors$1.dim(`(imported by ${missing[id]})`)
  47627. )}`
  47628. ).join(`
  47629. `)}
  47630. Are they installed?`
  47631. );
  47632. }
  47633. return deps;
  47634. })
  47635. };
  47636. }
  47637. function toDiscoveredDependencies(config, deps, ssr, timestamp) {
  47638. const browserHash = getOptimizedBrowserHash(
  47639. getDepHash(config, ssr).hash,
  47640. deps,
  47641. timestamp
  47642. );
  47643. const discovered = {};
  47644. for (const id in deps) {
  47645. const src = deps[id];
  47646. discovered[id] = {
  47647. id,
  47648. file: getOptimizedDepPath(id, config, ssr),
  47649. src,
  47650. browserHash,
  47651. exportsData: extractExportsData(src, config, ssr)
  47652. };
  47653. }
  47654. return discovered;
  47655. }
  47656. function depsLogString(qualifiedIds) {
  47657. return colors$1.yellow(qualifiedIds.join(`, `));
  47658. }
  47659. function runOptimizeDeps(resolvedConfig, depsInfo, ssr) {
  47660. const optimizerContext = { cancelled: false };
  47661. const config = {
  47662. ...resolvedConfig,
  47663. command: "build"
  47664. };
  47665. const depsCacheDir = getDepsCacheDir(resolvedConfig, ssr);
  47666. const processingCacheDir = getProcessingDepsCacheDir(resolvedConfig, ssr);
  47667. fs__default.mkdirSync(processingCacheDir, { recursive: true });
  47668. debug$7?.(colors$1.green(`creating package.json in ${processingCacheDir}`));
  47669. fs__default.writeFileSync(
  47670. path$n.resolve(processingCacheDir, "package.json"),
  47671. `{
  47672. "type": "module"
  47673. }
  47674. `
  47675. );
  47676. const metadata = initDepsOptimizerMetadata(config, ssr);
  47677. metadata.browserHash = getOptimizedBrowserHash(
  47678. metadata.hash,
  47679. depsFromOptimizedDepInfo(depsInfo)
  47680. );
  47681. const qualifiedIds = Object.keys(depsInfo);
  47682. let cleaned = false;
  47683. let committed = false;
  47684. const cleanUp = () => {
  47685. if (!cleaned && !committed) {
  47686. cleaned = true;
  47687. debug$7?.(colors$1.green(`removing cache dir ${processingCacheDir}`));
  47688. try {
  47689. fs__default.rmSync(processingCacheDir, { recursive: true, force: true });
  47690. } catch (error) {
  47691. }
  47692. }
  47693. };
  47694. const successfulResult = {
  47695. metadata,
  47696. cancel: cleanUp,
  47697. commit: async () => {
  47698. if (cleaned) {
  47699. throw new Error(
  47700. "Can not commit a Deps Optimization run as it was cancelled"
  47701. );
  47702. }
  47703. committed = true;
  47704. const dataPath = path$n.join(processingCacheDir, METADATA_FILENAME);
  47705. debug$7?.(
  47706. colors$1.green(`creating ${METADATA_FILENAME} in ${processingCacheDir}`)
  47707. );
  47708. fs__default.writeFileSync(
  47709. dataPath,
  47710. stringifyDepsOptimizerMetadata(metadata, depsCacheDir)
  47711. );
  47712. const temporaryPath = depsCacheDir + getTempSuffix();
  47713. const depsCacheDirPresent = fs__default.existsSync(depsCacheDir);
  47714. if (isWindows$3) {
  47715. if (depsCacheDirPresent) {
  47716. debug$7?.(colors$1.green(`renaming ${depsCacheDir} to ${temporaryPath}`));
  47717. await safeRename(depsCacheDir, temporaryPath);
  47718. }
  47719. debug$7?.(
  47720. colors$1.green(`renaming ${processingCacheDir} to ${depsCacheDir}`)
  47721. );
  47722. await safeRename(processingCacheDir, depsCacheDir);
  47723. } else {
  47724. if (depsCacheDirPresent) {
  47725. debug$7?.(colors$1.green(`renaming ${depsCacheDir} to ${temporaryPath}`));
  47726. fs__default.renameSync(depsCacheDir, temporaryPath);
  47727. }
  47728. debug$7?.(
  47729. colors$1.green(`renaming ${processingCacheDir} to ${depsCacheDir}`)
  47730. );
  47731. fs__default.renameSync(processingCacheDir, depsCacheDir);
  47732. }
  47733. if (depsCacheDirPresent) {
  47734. debug$7?.(colors$1.green(`removing cache temp dir ${temporaryPath}`));
  47735. fsp.rm(temporaryPath, { recursive: true, force: true });
  47736. }
  47737. }
  47738. };
  47739. if (!qualifiedIds.length) {
  47740. return {
  47741. cancel: async () => cleanUp(),
  47742. result: Promise.resolve(successfulResult)
  47743. };
  47744. }
  47745. const cancelledResult = {
  47746. metadata,
  47747. commit: async () => cleanUp(),
  47748. cancel: cleanUp
  47749. };
  47750. const start = performance$1.now();
  47751. const preparedRun = prepareEsbuildOptimizerRun(
  47752. resolvedConfig,
  47753. depsInfo,
  47754. ssr,
  47755. processingCacheDir,
  47756. optimizerContext
  47757. );
  47758. const runResult = preparedRun.then(({ context, idToExports }) => {
  47759. function disposeContext() {
  47760. return context?.dispose().catch((e) => {
  47761. config.logger.error("Failed to dispose esbuild context", { error: e });
  47762. });
  47763. }
  47764. if (!context || optimizerContext.cancelled) {
  47765. disposeContext();
  47766. return cancelledResult;
  47767. }
  47768. return context.rebuild().then((result) => {
  47769. const meta = result.metafile;
  47770. const processingCacheDirOutputPath = path$n.relative(
  47771. process.cwd(),
  47772. processingCacheDir
  47773. );
  47774. for (const id in depsInfo) {
  47775. const output = esbuildOutputFromId(
  47776. meta.outputs,
  47777. id,
  47778. processingCacheDir
  47779. );
  47780. const { exportsData, ...info } = depsInfo[id];
  47781. addOptimizedDepInfo(metadata, "optimized", {
  47782. ...info,
  47783. // We only need to hash the output.imports in to check for stability, but adding the hash
  47784. // and file path gives us a unique hash that may be useful for other things in the future
  47785. fileHash: getHash(
  47786. metadata.hash + depsInfo[id].file + JSON.stringify(output.imports)
  47787. ),
  47788. browserHash: metadata.browserHash,
  47789. // After bundling we have more information and can warn the user about legacy packages
  47790. // that require manual configuration
  47791. needsInterop: needsInterop(
  47792. config,
  47793. ssr,
  47794. id,
  47795. idToExports[id],
  47796. output
  47797. )
  47798. });
  47799. }
  47800. for (const o of Object.keys(meta.outputs)) {
  47801. if (!jsMapExtensionRE.test(o)) {
  47802. const id = path$n.relative(processingCacheDirOutputPath, o).replace(jsExtensionRE, "");
  47803. const file = getOptimizedDepPath(id, resolvedConfig, ssr);
  47804. if (!findOptimizedDepInfoInRecord(
  47805. metadata.optimized,
  47806. (depInfo) => depInfo.file === file
  47807. )) {
  47808. addOptimizedDepInfo(metadata, "chunks", {
  47809. id,
  47810. file,
  47811. needsInterop: false,
  47812. browserHash: metadata.browserHash
  47813. });
  47814. }
  47815. }
  47816. }
  47817. debug$7?.(
  47818. `Dependencies bundled in ${(performance$1.now() - start).toFixed(2)}ms`
  47819. );
  47820. return successfulResult;
  47821. }).catch((e) => {
  47822. if (e.errors && e.message.includes("The build was canceled")) {
  47823. return cancelledResult;
  47824. }
  47825. throw e;
  47826. }).finally(() => {
  47827. return disposeContext();
  47828. });
  47829. });
  47830. runResult.catch(() => {
  47831. cleanUp();
  47832. });
  47833. return {
  47834. async cancel() {
  47835. optimizerContext.cancelled = true;
  47836. const { context } = await preparedRun;
  47837. await context?.cancel();
  47838. cleanUp();
  47839. },
  47840. result: runResult
  47841. };
  47842. }
  47843. async function prepareEsbuildOptimizerRun(resolvedConfig, depsInfo, ssr, processingCacheDir, optimizerContext) {
  47844. const config = {
  47845. ...resolvedConfig,
  47846. command: "build"
  47847. };
  47848. const flatIdDeps = {};
  47849. const idToExports = {};
  47850. const optimizeDeps2 = getDepOptimizationConfig(config, ssr);
  47851. const { plugins: pluginsFromConfig = [], ...esbuildOptions } = optimizeDeps2?.esbuildOptions ?? {};
  47852. await Promise.all(
  47853. Object.keys(depsInfo).map(async (id) => {
  47854. const src = depsInfo[id].src;
  47855. const exportsData = await (depsInfo[id].exportsData ?? extractExportsData(src, config, ssr));
  47856. if (exportsData.jsxLoader && !esbuildOptions.loader?.[".js"]) {
  47857. esbuildOptions.loader = {
  47858. ".js": "jsx",
  47859. ...esbuildOptions.loader
  47860. };
  47861. }
  47862. const flatId = flattenId(id);
  47863. flatIdDeps[flatId] = src;
  47864. idToExports[id] = exportsData;
  47865. })
  47866. );
  47867. if (optimizerContext.cancelled) return { context: void 0, idToExports };
  47868. const define = {
  47869. "process.env.NODE_ENV": JSON.stringify(process.env.NODE_ENV || config.mode)
  47870. };
  47871. const platform = ssr && config.ssr?.target !== "webworker" ? "node" : "browser";
  47872. const external = [...optimizeDeps2?.exclude ?? []];
  47873. const plugins = [...pluginsFromConfig];
  47874. if (external.length) {
  47875. plugins.push(esbuildCjsExternalPlugin(external, platform));
  47876. }
  47877. plugins.push(esbuildDepPlugin(flatIdDeps, external, config, ssr));
  47878. const context = await esbuild.context({
  47879. absWorkingDir: process.cwd(),
  47880. entryPoints: Object.keys(flatIdDeps),
  47881. bundle: true,
  47882. // We can't use platform 'neutral', as esbuild has custom handling
  47883. // when the platform is 'node' or 'browser' that can't be emulated
  47884. // by using mainFields and conditions
  47885. platform,
  47886. define,
  47887. format: "esm",
  47888. // See https://github.com/evanw/esbuild/issues/1921#issuecomment-1152991694
  47889. banner: platform === "node" ? {
  47890. js: `import { createRequire } from 'module';const require = createRequire(import.meta.url);`
  47891. } : void 0,
  47892. target: ESBUILD_MODULES_TARGET,
  47893. external,
  47894. logLevel: "error",
  47895. splitting: true,
  47896. sourcemap: true,
  47897. outdir: processingCacheDir,
  47898. ignoreAnnotations: true,
  47899. metafile: true,
  47900. plugins,
  47901. charset: "utf8",
  47902. ...esbuildOptions,
  47903. supported: {
  47904. ...defaultEsbuildSupported,
  47905. ...esbuildOptions.supported
  47906. }
  47907. });
  47908. return { context, idToExports };
  47909. }
  47910. async function addManuallyIncludedOptimizeDeps(deps, config, ssr) {
  47911. const { logger } = config;
  47912. const optimizeDeps2 = getDepOptimizationConfig(config, ssr);
  47913. const optimizeDepsInclude = optimizeDeps2?.include ?? [];
  47914. if (optimizeDepsInclude.length) {
  47915. const unableToOptimize = (id, msg) => {
  47916. if (optimizeDepsInclude.includes(id)) {
  47917. logger.warn(
  47918. `${msg}: ${colors$1.cyan(id)}, present in '${ssr ? "ssr." : ""}optimizeDeps.include'`
  47919. );
  47920. }
  47921. };
  47922. const includes = [...optimizeDepsInclude];
  47923. for (let i = 0; i < includes.length; i++) {
  47924. const id = includes[i];
  47925. if (glob.isDynamicPattern(id)) {
  47926. const globIds = expandGlobIds(id, config);
  47927. includes.splice(i, 1, ...globIds);
  47928. i += globIds.length - 1;
  47929. }
  47930. }
  47931. const resolve = createOptimizeDepsIncludeResolver(config, ssr);
  47932. for (const id of includes) {
  47933. const normalizedId = normalizeId(id);
  47934. if (!deps[normalizedId]) {
  47935. const entry = await resolve(id);
  47936. if (entry) {
  47937. if (isOptimizable(entry, optimizeDeps2)) {
  47938. if (!entry.endsWith("?__vite_skip_optimization")) {
  47939. deps[normalizedId] = entry;
  47940. }
  47941. } else {
  47942. unableToOptimize(id, "Cannot optimize dependency");
  47943. }
  47944. } else {
  47945. unableToOptimize(id, "Failed to resolve dependency");
  47946. }
  47947. }
  47948. }
  47949. }
  47950. }
  47951. function depsFromOptimizedDepInfo(depsInfo) {
  47952. const obj = {};
  47953. for (const key in depsInfo) {
  47954. obj[key] = depsInfo[key].src;
  47955. }
  47956. return obj;
  47957. }
  47958. function getOptimizedDepPath(id, config, ssr) {
  47959. return normalizePath$3(
  47960. path$n.resolve(getDepsCacheDir(config, ssr), flattenId(id) + ".js")
  47961. );
  47962. }
  47963. function getDepsCacheSuffix(ssr) {
  47964. return ssr ? "_ssr" : "";
  47965. }
  47966. function getDepsCacheDir(config, ssr) {
  47967. return getDepsCacheDirPrefix(config) + getDepsCacheSuffix(ssr);
  47968. }
  47969. function getProcessingDepsCacheDir(config, ssr) {
  47970. return getDepsCacheDirPrefix(config) + getDepsCacheSuffix(ssr) + getTempSuffix();
  47971. }
  47972. function getTempSuffix() {
  47973. return "_temp_" + getHash(
  47974. `${process.pid}:${Date.now().toString()}:${Math.random().toString(16).slice(2)}`
  47975. );
  47976. }
  47977. function getDepsCacheDirPrefix(config) {
  47978. return normalizePath$3(path$n.resolve(config.cacheDir, "deps"));
  47979. }
  47980. function createIsOptimizedDepFile(config) {
  47981. const depsCacheDirPrefix = getDepsCacheDirPrefix(config);
  47982. return (id) => id.startsWith(depsCacheDirPrefix);
  47983. }
  47984. function createIsOptimizedDepUrl(config) {
  47985. const { root } = config;
  47986. const depsCacheDir = getDepsCacheDirPrefix(config);
  47987. const depsCacheDirRelative = normalizePath$3(path$n.relative(root, depsCacheDir));
  47988. const depsCacheDirPrefix = depsCacheDirRelative.startsWith("../") ? (
  47989. // if the cache directory is outside root, the url prefix would be something
  47990. // like '/@fs/absolute/path/to/node_modules/.vite'
  47991. `/@fs/${removeLeadingSlash(normalizePath$3(depsCacheDir))}`
  47992. ) : (
  47993. // if the cache directory is inside root, the url prefix would be something
  47994. // like '/node_modules/.vite'
  47995. `/${depsCacheDirRelative}`
  47996. );
  47997. return function isOptimizedDepUrl(url) {
  47998. return url.startsWith(depsCacheDirPrefix);
  47999. };
  48000. }
  48001. function parseDepsOptimizerMetadata(jsonMetadata, depsCacheDir) {
  48002. const { hash, lockfileHash, configHash, browserHash, optimized, chunks } = JSON.parse(jsonMetadata, (key, value) => {
  48003. if (key === "file" || key === "src") {
  48004. return normalizePath$3(path$n.resolve(depsCacheDir, value));
  48005. }
  48006. return value;
  48007. });
  48008. if (!chunks || Object.values(optimized).some((depInfo) => !depInfo.fileHash)) {
  48009. return;
  48010. }
  48011. const metadata = {
  48012. hash,
  48013. lockfileHash,
  48014. configHash,
  48015. browserHash,
  48016. optimized: {},
  48017. discovered: {},
  48018. chunks: {},
  48019. depInfoList: []
  48020. };
  48021. for (const id of Object.keys(optimized)) {
  48022. addOptimizedDepInfo(metadata, "optimized", {
  48023. ...optimized[id],
  48024. id,
  48025. browserHash
  48026. });
  48027. }
  48028. for (const id of Object.keys(chunks)) {
  48029. addOptimizedDepInfo(metadata, "chunks", {
  48030. ...chunks[id],
  48031. id,
  48032. browserHash,
  48033. needsInterop: false
  48034. });
  48035. }
  48036. return metadata;
  48037. }
  48038. function stringifyDepsOptimizerMetadata(metadata, depsCacheDir) {
  48039. const { hash, configHash, lockfileHash, browserHash, optimized, chunks } = metadata;
  48040. return JSON.stringify(
  48041. {
  48042. hash,
  48043. configHash,
  48044. lockfileHash,
  48045. browserHash,
  48046. optimized: Object.fromEntries(
  48047. Object.values(optimized).map(
  48048. ({ id, src, file, fileHash, needsInterop: needsInterop2 }) => [
  48049. id,
  48050. {
  48051. src,
  48052. file,
  48053. fileHash,
  48054. needsInterop: needsInterop2
  48055. }
  48056. ]
  48057. )
  48058. ),
  48059. chunks: Object.fromEntries(
  48060. Object.values(chunks).map(({ id, file }) => [id, { file }])
  48061. )
  48062. },
  48063. (key, value) => {
  48064. if (key === "file" || key === "src") {
  48065. return normalizePath$3(path$n.relative(depsCacheDir, value));
  48066. }
  48067. return value;
  48068. },
  48069. 2
  48070. );
  48071. }
  48072. function esbuildOutputFromId(outputs, id, cacheDirOutputPath) {
  48073. const cwd = process.cwd();
  48074. const flatId = flattenId(id) + ".js";
  48075. const normalizedOutputPath = normalizePath$3(
  48076. path$n.relative(cwd, path$n.join(cacheDirOutputPath, flatId))
  48077. );
  48078. const output = outputs[normalizedOutputPath];
  48079. if (output) {
  48080. return output;
  48081. }
  48082. for (const [key, value] of Object.entries(outputs)) {
  48083. if (normalizePath$3(path$n.relative(cwd, key)) === normalizedOutputPath) {
  48084. return value;
  48085. }
  48086. }
  48087. }
  48088. async function extractExportsData(filePath, config, ssr) {
  48089. await init;
  48090. const optimizeDeps2 = getDepOptimizationConfig(config, ssr);
  48091. const esbuildOptions = optimizeDeps2?.esbuildOptions ?? {};
  48092. if (optimizeDeps2.extensions?.some((ext) => filePath.endsWith(ext))) {
  48093. const result = await build$3({
  48094. ...esbuildOptions,
  48095. entryPoints: [filePath],
  48096. write: false,
  48097. format: "esm"
  48098. });
  48099. const [, exports2, , hasModuleSyntax2] = parse$d(result.outputFiles[0].text);
  48100. return {
  48101. hasModuleSyntax: hasModuleSyntax2,
  48102. exports: exports2.map((e) => e.n)
  48103. };
  48104. }
  48105. let parseResult;
  48106. let usedJsxLoader = false;
  48107. const entryContent = await fsp.readFile(filePath, "utf-8");
  48108. try {
  48109. parseResult = parse$d(entryContent);
  48110. } catch {
  48111. const loader = esbuildOptions.loader?.[path$n.extname(filePath)] || "jsx";
  48112. debug$7?.(
  48113. `Unable to parse: ${filePath}.
  48114. Trying again with a ${loader} transform.`
  48115. );
  48116. const transformed = await transformWithEsbuild(entryContent, filePath, {
  48117. loader
  48118. });
  48119. parseResult = parse$d(transformed.code);
  48120. usedJsxLoader = true;
  48121. }
  48122. const [, exports, , hasModuleSyntax] = parseResult;
  48123. const exportsData = {
  48124. hasModuleSyntax,
  48125. exports: exports.map((e) => e.n),
  48126. jsxLoader: usedJsxLoader
  48127. };
  48128. return exportsData;
  48129. }
  48130. function needsInterop(config, ssr, id, exportsData, output) {
  48131. if (getDepOptimizationConfig(config, ssr)?.needsInterop?.includes(id)) {
  48132. return true;
  48133. }
  48134. const { hasModuleSyntax, exports } = exportsData;
  48135. if (!hasModuleSyntax) {
  48136. return true;
  48137. }
  48138. if (output) {
  48139. const generatedExports = output.exports;
  48140. if (!generatedExports || isSingleDefaultExport(generatedExports) && !isSingleDefaultExport(exports)) {
  48141. return true;
  48142. }
  48143. }
  48144. return false;
  48145. }
  48146. function isSingleDefaultExport(exports) {
  48147. return exports.length === 1 && exports[0] === "default";
  48148. }
  48149. const lockfileFormats = [
  48150. { name: "package-lock.json", checkPatches: true, manager: "npm" },
  48151. { name: "yarn.lock", checkPatches: true, manager: "yarn" },
  48152. // Included in lockfile for v2+
  48153. { name: "pnpm-lock.yaml", checkPatches: false, manager: "pnpm" },
  48154. // Included in lockfile
  48155. { name: "bun.lockb", checkPatches: true, manager: "bun" }
  48156. ].sort((_, { manager }) => {
  48157. return process.env.npm_config_user_agent?.startsWith(manager) ? 1 : -1;
  48158. });
  48159. const lockfileNames = lockfileFormats.map((l) => l.name);
  48160. function getConfigHash(config, ssr) {
  48161. const optimizeDeps2 = getDepOptimizationConfig(config, ssr);
  48162. const content = JSON.stringify(
  48163. {
  48164. mode: process.env.NODE_ENV || config.mode,
  48165. root: config.root,
  48166. resolve: config.resolve,
  48167. assetsInclude: config.assetsInclude,
  48168. plugins: config.plugins.map((p) => p.name),
  48169. optimizeDeps: {
  48170. include: optimizeDeps2?.include ? unique(optimizeDeps2.include).sort() : void 0,
  48171. exclude: optimizeDeps2?.exclude ? unique(optimizeDeps2.exclude).sort() : void 0,
  48172. esbuildOptions: {
  48173. ...optimizeDeps2?.esbuildOptions,
  48174. plugins: optimizeDeps2?.esbuildOptions?.plugins?.map((p) => p.name)
  48175. }
  48176. }
  48177. },
  48178. (_, value) => {
  48179. if (typeof value === "function" || value instanceof RegExp) {
  48180. return value.toString();
  48181. }
  48182. return value;
  48183. }
  48184. );
  48185. return getHash(content);
  48186. }
  48187. function getLockfileHash(config, ssr) {
  48188. const lockfilePath = lookupFile(config.root, lockfileNames);
  48189. let content = lockfilePath ? fs__default.readFileSync(lockfilePath, "utf-8") : "";
  48190. if (lockfilePath) {
  48191. const lockfileName = path$n.basename(lockfilePath);
  48192. const { checkPatches } = lockfileFormats.find(
  48193. (f) => f.name === lockfileName
  48194. );
  48195. if (checkPatches) {
  48196. const fullPath = path$n.join(path$n.dirname(lockfilePath), "patches");
  48197. const stat = tryStatSync(fullPath);
  48198. if (stat?.isDirectory()) {
  48199. content += stat.mtimeMs.toString();
  48200. }
  48201. }
  48202. }
  48203. return getHash(content);
  48204. }
  48205. function getDepHash(config, ssr) {
  48206. const lockfileHash = getLockfileHash(config);
  48207. const configHash = getConfigHash(config, ssr);
  48208. const hash = getHash(lockfileHash + configHash);
  48209. return {
  48210. hash,
  48211. lockfileHash,
  48212. configHash
  48213. };
  48214. }
  48215. function getOptimizedBrowserHash(hash, deps, timestamp = "") {
  48216. return getHash(hash + JSON.stringify(deps) + timestamp);
  48217. }
  48218. function optimizedDepInfoFromId(metadata, id) {
  48219. return metadata.optimized[id] || metadata.discovered[id] || metadata.chunks[id];
  48220. }
  48221. function optimizedDepInfoFromFile(metadata, file) {
  48222. return metadata.depInfoList.find((depInfo) => depInfo.file === file);
  48223. }
  48224. function findOptimizedDepInfoInRecord(dependenciesInfo, callbackFn) {
  48225. for (const o of Object.keys(dependenciesInfo)) {
  48226. const info = dependenciesInfo[o];
  48227. if (callbackFn(info, o)) {
  48228. return info;
  48229. }
  48230. }
  48231. }
  48232. async function optimizedDepNeedsInterop(metadata, file, config, ssr) {
  48233. const depInfo = optimizedDepInfoFromFile(metadata, file);
  48234. if (depInfo?.src && depInfo.needsInterop === void 0) {
  48235. depInfo.exportsData ??= extractExportsData(depInfo.src, config, ssr);
  48236. depInfo.needsInterop = needsInterop(
  48237. config,
  48238. ssr,
  48239. depInfo.id,
  48240. await depInfo.exportsData
  48241. );
  48242. }
  48243. return depInfo?.needsInterop;
  48244. }
  48245. const MAX_TEMP_DIR_AGE_MS = 24 * 60 * 60 * 1e3;
  48246. async function cleanupDepsCacheStaleDirs(config) {
  48247. try {
  48248. const cacheDir = path$n.resolve(config.cacheDir);
  48249. if (fs__default.existsSync(cacheDir)) {
  48250. const dirents = await fsp.readdir(cacheDir, { withFileTypes: true });
  48251. for (const dirent of dirents) {
  48252. if (dirent.isDirectory() && dirent.name.includes("_temp_")) {
  48253. const tempDirPath = path$n.resolve(config.cacheDir, dirent.name);
  48254. const stats = await fsp.stat(tempDirPath).catch((_) => null);
  48255. if (stats?.mtime && Date.now() - stats.mtime.getTime() > MAX_TEMP_DIR_AGE_MS) {
  48256. debug$7?.(`removing stale cache temp dir ${tempDirPath}`);
  48257. await fsp.rm(tempDirPath, { recursive: true, force: true });
  48258. }
  48259. }
  48260. }
  48261. }
  48262. } catch (err) {
  48263. config.logger.error(err);
  48264. }
  48265. }
  48266. const GRACEFUL_RENAME_TIMEOUT = 5e3;
  48267. const safeRename = promisify$4(function gracefulRename(from, to, cb) {
  48268. const start = Date.now();
  48269. let backoff = 0;
  48270. fs__default.rename(from, to, function CB(er) {
  48271. if (er && (er.code === "EACCES" || er.code === "EPERM") && Date.now() - start < GRACEFUL_RENAME_TIMEOUT) {
  48272. setTimeout(function() {
  48273. fs__default.stat(to, function(stater, st) {
  48274. if (stater && stater.code === "ENOENT") fs__default.rename(from, to, CB);
  48275. else CB(er);
  48276. });
  48277. }, backoff);
  48278. if (backoff < 100) backoff += 10;
  48279. return;
  48280. }
  48281. if (cb) cb(er);
  48282. });
  48283. });
  48284. var index$1 = {
  48285. __proto__: null,
  48286. addManuallyIncludedOptimizeDeps: addManuallyIncludedOptimizeDeps,
  48287. addOptimizedDepInfo: addOptimizedDepInfo,
  48288. cleanupDepsCacheStaleDirs: cleanupDepsCacheStaleDirs,
  48289. createIsOptimizedDepFile: createIsOptimizedDepFile,
  48290. createIsOptimizedDepUrl: createIsOptimizedDepUrl,
  48291. depsFromOptimizedDepInfo: depsFromOptimizedDepInfo,
  48292. depsLogString: depsLogString,
  48293. discoverProjectDependencies: discoverProjectDependencies,
  48294. extractExportsData: extractExportsData,
  48295. getDepsCacheDir: getDepsCacheDir,
  48296. getDepsOptimizer: getDepsOptimizer,
  48297. getOptimizedDepPath: getOptimizedDepPath,
  48298. initDepsOptimizer: initDepsOptimizer,
  48299. initDepsOptimizerMetadata: initDepsOptimizerMetadata,
  48300. initDevSsrDepsOptimizer: initDevSsrDepsOptimizer,
  48301. loadCachedDepOptimizationMetadata: loadCachedDepOptimizationMetadata,
  48302. optimizeDeps: optimizeDeps,
  48303. optimizeServerSsrDeps: optimizeServerSsrDeps,
  48304. optimizedDepInfoFromFile: optimizedDepInfoFromFile,
  48305. optimizedDepInfoFromId: optimizedDepInfoFromId,
  48306. optimizedDepNeedsInterop: optimizedDepNeedsInterop,
  48307. runOptimizeDeps: runOptimizeDeps,
  48308. toDiscoveredDependencies: toDiscoveredDependencies
  48309. };
  48310. function totalist(dir, callback, pre='') {
  48311. dir = resolve$3('.', dir);
  48312. let arr = readdirSync(dir);
  48313. let i=0, abs, stats;
  48314. for (; i < arr.length; i++) {
  48315. abs = join$1(dir, arr[i]);
  48316. stats = statSync$1(abs);
  48317. stats.isDirectory()
  48318. ? totalist(abs, callback, join$1(pre, arr[i]))
  48319. : callback(join$1(pre, arr[i]), abs, stats);
  48320. }
  48321. }
  48322. /**
  48323. * @typedef ParsedURL
  48324. * @type {import('.').ParsedURL}
  48325. */
  48326. /**
  48327. * @typedef Request
  48328. * @property {string} url
  48329. * @property {ParsedURL} _parsedUrl
  48330. */
  48331. /**
  48332. * @param {Request} req
  48333. * @returns {ParsedURL|void}
  48334. */
  48335. function parse$5(req) {
  48336. let raw = req.url;
  48337. if (raw == null) return;
  48338. let prev = req._parsedUrl;
  48339. if (prev && prev.raw === raw) return prev;
  48340. let pathname=raw, search='', query;
  48341. if (raw.length > 1) {
  48342. let idx = raw.indexOf('?', 1);
  48343. if (idx !== -1) {
  48344. search = raw.substring(idx);
  48345. pathname = raw.substring(0, idx);
  48346. if (search.length > 1) {
  48347. query = qs.parse(search.substring(1));
  48348. }
  48349. }
  48350. }
  48351. return req._parsedUrl = { pathname, search, query, raw };
  48352. }
  48353. const noop$2 = () => {};
  48354. function isMatch(uri, arr) {
  48355. for (let i=0; i < arr.length; i++) {
  48356. if (arr[i].test(uri)) return true;
  48357. }
  48358. }
  48359. function toAssume(uri, extns) {
  48360. let i=0, x, len=uri.length - 1;
  48361. if (uri.charCodeAt(len) === 47) {
  48362. uri = uri.substring(0, len);
  48363. }
  48364. let arr=[], tmp=`${uri}/index`;
  48365. for (; i < extns.length; i++) {
  48366. x = extns[i] ? `.${extns[i]}` : '';
  48367. if (uri) arr.push(uri + x);
  48368. arr.push(tmp + x);
  48369. }
  48370. return arr;
  48371. }
  48372. function viaCache(cache, uri, extns) {
  48373. let i=0, data, arr=toAssume(uri, extns);
  48374. for (; i < arr.length; i++) {
  48375. if (data = cache[arr[i]]) return data;
  48376. }
  48377. }
  48378. function viaLocal(dir, isEtag, uri, extns, shouldServe) {
  48379. let i=0, arr=toAssume(uri, extns);
  48380. let abs, stats, name, headers;
  48381. for (; i < arr.length; i++) {
  48382. abs = normalize$1(join$1(dir, name=arr[i]));
  48383. if (abs.startsWith(dir) && require$$0$2.existsSync(abs)) {
  48384. stats = require$$0$2.statSync(abs);
  48385. if (stats.isDirectory()) continue;
  48386. if (shouldServe && !shouldServe(abs)) continue;
  48387. headers = toHeaders(name, stats, isEtag);
  48388. headers['Cache-Control'] = isEtag ? 'no-cache' : 'no-store';
  48389. return { abs, stats, headers };
  48390. }
  48391. }
  48392. }
  48393. function is404(req, res) {
  48394. return (res.statusCode=404,res.end());
  48395. }
  48396. function send$1(req, res, file, stats, headers) {
  48397. let code=200, tmp, opts={};
  48398. headers = { ...headers };
  48399. for (let key in headers) {
  48400. tmp = res.getHeader(key);
  48401. if (tmp) headers[key] = tmp;
  48402. }
  48403. if (tmp = res.getHeader('content-type')) {
  48404. headers['Content-Type'] = tmp;
  48405. }
  48406. if (req.headers.range) {
  48407. code = 206;
  48408. let [x, y] = req.headers.range.replace('bytes=', '').split('-');
  48409. let end = opts.end = parseInt(y, 10) || stats.size - 1;
  48410. let start = opts.start = parseInt(x, 10) || 0;
  48411. if (end >= stats.size) {
  48412. end = stats.size - 1;
  48413. }
  48414. if (start >= stats.size) {
  48415. res.setHeader('Content-Range', `bytes */${stats.size}`);
  48416. res.statusCode = 416;
  48417. return res.end();
  48418. }
  48419. headers['Content-Range'] = `bytes ${start}-${end}/${stats.size}`;
  48420. headers['Content-Length'] = (end - start + 1);
  48421. headers['Accept-Ranges'] = 'bytes';
  48422. }
  48423. res.writeHead(code, headers);
  48424. require$$0$2.createReadStream(file, opts).pipe(res);
  48425. }
  48426. const ENCODING = {
  48427. '.br': 'br',
  48428. '.gz': 'gzip',
  48429. };
  48430. function toHeaders(name, stats, isEtag) {
  48431. let enc = ENCODING[name.slice(-3)];
  48432. let ctype = lookup(name.slice(0, enc && -3)) || '';
  48433. if (ctype === 'text/html') ctype += ';charset=utf-8';
  48434. let headers = {
  48435. 'Content-Length': stats.size,
  48436. 'Content-Type': ctype,
  48437. 'Last-Modified': stats.mtime.toUTCString(),
  48438. };
  48439. if (enc) headers['Content-Encoding'] = enc;
  48440. if (isEtag) headers['ETag'] = `W/"${stats.size}-${stats.mtime.getTime()}"`;
  48441. return headers;
  48442. }
  48443. function sirv (dir, opts={}) {
  48444. dir = resolve$3(dir || '.');
  48445. let isNotFound = opts.onNoMatch || is404;
  48446. let setHeaders = opts.setHeaders || noop$2;
  48447. let extensions = opts.extensions || ['html', 'htm'];
  48448. let gzips = opts.gzip && extensions.map(x => `${x}.gz`).concat('gz');
  48449. let brots = opts.brotli && extensions.map(x => `${x}.br`).concat('br');
  48450. const FILES = {};
  48451. let fallback = '/';
  48452. let isEtag = !!opts.etag;
  48453. let isSPA = !!opts.single;
  48454. if (typeof opts.single === 'string') {
  48455. let idx = opts.single.lastIndexOf('.');
  48456. fallback += !!~idx ? opts.single.substring(0, idx) : opts.single;
  48457. }
  48458. let ignores = [];
  48459. if (opts.ignores !== false) {
  48460. ignores.push(/[/]([A-Za-z\s\d~$._-]+\.\w+){1,}$/); // any extn
  48461. if (opts.dotfiles) ignores.push(/\/\.\w/);
  48462. else ignores.push(/\/\.well-known/);
  48463. [].concat(opts.ignores || []).forEach(x => {
  48464. ignores.push(new RegExp(x, 'i'));
  48465. });
  48466. }
  48467. let cc = opts.maxAge != null && `public,max-age=${opts.maxAge}`;
  48468. if (cc && opts.immutable) cc += ',immutable';
  48469. else if (cc && opts.maxAge === 0) cc += ',must-revalidate';
  48470. if (!opts.dev) {
  48471. totalist(dir, (name, abs, stats) => {
  48472. if (/\.well-known[\\+\/]/.test(name)) ; // keep
  48473. else if (!opts.dotfiles && /(^\.|[\\+|\/+]\.)/.test(name)) return;
  48474. let headers = toHeaders(name, stats, isEtag);
  48475. if (cc) headers['Cache-Control'] = cc;
  48476. FILES['/' + name.normalize().replace(/\\+/g, '/')] = { abs, stats, headers };
  48477. });
  48478. }
  48479. let lookup = opts.dev ? viaLocal.bind(0, dir, isEtag) : viaCache.bind(0, FILES);
  48480. return function (req, res, next) {
  48481. let extns = [''];
  48482. let pathname = parse$5(req).pathname;
  48483. let val = req.headers['accept-encoding'] || '';
  48484. if (gzips && val.includes('gzip')) extns.unshift(...gzips);
  48485. if (brots && /(br|brotli)/i.test(val)) extns.unshift(...brots);
  48486. extns.push(...extensions); // [...br, ...gz, orig, ...exts]
  48487. if (pathname.indexOf('%') !== -1) {
  48488. try { pathname = decodeURI(pathname); }
  48489. catch (err) { /* malform uri */ }
  48490. }
  48491. let data = lookup(pathname, extns, opts.shouldServe) || isSPA && !isMatch(pathname, ignores) && lookup(fallback, extns, opts.shouldServe);
  48492. if (!data) return next ? next() : isNotFound(req, res);
  48493. if (isEtag && req.headers['if-none-match'] === data.headers['ETag']) {
  48494. res.writeHead(304);
  48495. return res.end();
  48496. }
  48497. if (gzips || brots) {
  48498. res.setHeader('Vary', 'Accept-Encoding');
  48499. }
  48500. setHeaders(res, pathname, data.stats);
  48501. send$1(req, res, data.abs, data.stats, data.headers);
  48502. };
  48503. }
  48504. const knownJavascriptExtensionRE = /\.[tj]sx?$/;
  48505. const sirvOptions = ({
  48506. getHeaders
  48507. }) => {
  48508. return {
  48509. dev: true,
  48510. etag: true,
  48511. extensions: [],
  48512. setHeaders(res, pathname) {
  48513. if (knownJavascriptExtensionRE.test(pathname)) {
  48514. res.setHeader("Content-Type", "text/javascript");
  48515. }
  48516. const headers = getHeaders();
  48517. if (headers) {
  48518. for (const name in headers) {
  48519. res.setHeader(name, headers[name]);
  48520. }
  48521. }
  48522. }
  48523. };
  48524. };
  48525. function servePublicMiddleware(server, publicFiles) {
  48526. const dir = server.config.publicDir;
  48527. const serve = sirv(
  48528. dir,
  48529. sirvOptions({
  48530. getHeaders: () => server.config.server.headers
  48531. })
  48532. );
  48533. const toFilePath = (url) => {
  48534. let filePath = cleanUrl(url);
  48535. if (filePath.indexOf("%") !== -1) {
  48536. try {
  48537. filePath = decodeURI(filePath);
  48538. } catch (err) {
  48539. }
  48540. }
  48541. return normalizePath$3(filePath);
  48542. };
  48543. return function viteServePublicMiddleware(req, res, next) {
  48544. if (publicFiles && !publicFiles.has(toFilePath(req.url)) || isImportRequest(req.url) || isInternalRequest(req.url) || // for `/public-file.js?url` to be transformed
  48545. urlRE.test(req.url)) {
  48546. return next();
  48547. }
  48548. serve(req, res, next);
  48549. };
  48550. }
  48551. function serveStaticMiddleware(server) {
  48552. const dir = server.config.root;
  48553. const serve = sirv(
  48554. dir,
  48555. sirvOptions({
  48556. getHeaders: () => server.config.server.headers
  48557. })
  48558. );
  48559. return function viteServeStaticMiddleware(req, res, next) {
  48560. const cleanedUrl = cleanUrl(req.url);
  48561. if (cleanedUrl[cleanedUrl.length - 1] === "/" || path$n.extname(cleanedUrl) === ".html" || isInternalRequest(req.url)) {
  48562. return next();
  48563. }
  48564. const url = new URL(req.url.replace(/^\/{2,}/, "/"), "http://example.com");
  48565. const pathname = decodeURI(url.pathname);
  48566. let redirectedPathname;
  48567. for (const { find, replacement } of server.config.resolve.alias) {
  48568. const matches = typeof find === "string" ? pathname.startsWith(find) : find.test(pathname);
  48569. if (matches) {
  48570. redirectedPathname = pathname.replace(find, replacement);
  48571. break;
  48572. }
  48573. }
  48574. if (redirectedPathname) {
  48575. if (redirectedPathname.startsWith(withTrailingSlash(dir))) {
  48576. redirectedPathname = redirectedPathname.slice(dir.length);
  48577. }
  48578. }
  48579. const resolvedPathname = redirectedPathname || pathname;
  48580. let fileUrl = path$n.resolve(dir, removeLeadingSlash(resolvedPathname));
  48581. if (resolvedPathname[resolvedPathname.length - 1] === "/" && fileUrl[fileUrl.length - 1] !== "/") {
  48582. fileUrl = withTrailingSlash(fileUrl);
  48583. }
  48584. if (!ensureServingAccess(fileUrl, server, res, next)) {
  48585. return;
  48586. }
  48587. if (redirectedPathname) {
  48588. url.pathname = encodeURI(redirectedPathname);
  48589. req.url = url.href.slice(url.origin.length);
  48590. }
  48591. serve(req, res, next);
  48592. };
  48593. }
  48594. function serveRawFsMiddleware(server) {
  48595. const serveFromRoot = sirv(
  48596. "/",
  48597. sirvOptions({ getHeaders: () => server.config.server.headers })
  48598. );
  48599. return function viteServeRawFsMiddleware(req, res, next) {
  48600. const url = new URL(req.url.replace(/^\/{2,}/, "/"), "http://example.com");
  48601. if (url.pathname.startsWith(FS_PREFIX)) {
  48602. const pathname = decodeURI(url.pathname);
  48603. if (!ensureServingAccess(
  48604. slash$1(path$n.resolve(fsPathFromId(pathname))),
  48605. server,
  48606. res,
  48607. next
  48608. )) {
  48609. return;
  48610. }
  48611. let newPathname = pathname.slice(FS_PREFIX.length);
  48612. if (isWindows$3) newPathname = newPathname.replace(/^[A-Z]:/i, "");
  48613. url.pathname = encodeURI(newPathname);
  48614. req.url = url.href.slice(url.origin.length);
  48615. serveFromRoot(req, res, next);
  48616. } else {
  48617. next();
  48618. }
  48619. };
  48620. }
  48621. function isFileServingAllowed(url, server) {
  48622. if (!server.config.server.fs.strict) return true;
  48623. const file = fsPathFromUrl(url);
  48624. if (server._fsDenyGlob(file)) return false;
  48625. if (server.moduleGraph.safeModulesPath.has(file)) return true;
  48626. if (server.config.server.fs.allow.some(
  48627. (uri) => isSameFileUri(uri, file) || isParentDirectory(uri, file)
  48628. ))
  48629. return true;
  48630. return false;
  48631. }
  48632. function ensureServingAccess(url, server, res, next) {
  48633. if (isFileServingAllowed(url, server)) {
  48634. return true;
  48635. }
  48636. if (isFileReadable(cleanUrl(url))) {
  48637. const urlMessage = `The request url "${url}" is outside of Vite serving allow list.`;
  48638. const hintMessage = `
  48639. ${server.config.server.fs.allow.map((i) => `- ${i}`).join("\n")}
  48640. Refer to docs https://vite.dev/config/server-options.html#server-fs-allow for configurations and more details.`;
  48641. server.config.logger.error(urlMessage);
  48642. server.config.logger.warnOnce(hintMessage + "\n");
  48643. res.statusCode = 403;
  48644. res.write(renderRestrictedErrorHTML(urlMessage + "\n" + hintMessage));
  48645. res.end();
  48646. } else {
  48647. next();
  48648. }
  48649. return false;
  48650. }
  48651. function renderRestrictedErrorHTML(msg) {
  48652. const html = String.raw;
  48653. return html`
  48654. <body>
  48655. <h1>403 Restricted</h1>
  48656. <p>${escapeHtml$2(msg).replace(/\n/g, "<br/>")}</p>
  48657. <style>
  48658. body {
  48659. padding: 1em 2em;
  48660. }
  48661. </style>
  48662. </body>
  48663. `;
  48664. }
  48665. const ERR_LOAD_URL = "ERR_LOAD_URL";
  48666. const ERR_LOAD_PUBLIC_URL = "ERR_LOAD_PUBLIC_URL";
  48667. const debugLoad = createDebugger("vite:load");
  48668. const debugTransform = createDebugger("vite:transform");
  48669. const debugCache$1 = createDebugger("vite:cache");
  48670. function transformRequest(url, server, options = {}) {
  48671. if (server._restartPromise && !options.ssr) throwClosedServerError();
  48672. const cacheKey = (options.ssr ? "ssr:" : options.html ? "html:" : "") + url;
  48673. const timestamp = Date.now();
  48674. const pending = server._pendingRequests.get(cacheKey);
  48675. if (pending) {
  48676. return server.moduleGraph.getModuleByUrl(removeTimestampQuery(url), options.ssr).then((module) => {
  48677. if (!module || pending.timestamp > module.lastInvalidationTimestamp) {
  48678. return pending.request;
  48679. } else {
  48680. pending.abort();
  48681. return transformRequest(url, server, options);
  48682. }
  48683. });
  48684. }
  48685. const request = doTransform(url, server, options, timestamp);
  48686. let cleared = false;
  48687. const clearCache = () => {
  48688. if (!cleared) {
  48689. server._pendingRequests.delete(cacheKey);
  48690. cleared = true;
  48691. }
  48692. };
  48693. server._pendingRequests.set(cacheKey, {
  48694. request,
  48695. timestamp,
  48696. abort: clearCache
  48697. });
  48698. return request.finally(clearCache);
  48699. }
  48700. async function doTransform(url, server, options, timestamp) {
  48701. url = removeTimestampQuery(url);
  48702. const { config, pluginContainer } = server;
  48703. const ssr = !!options.ssr;
  48704. if (ssr && isDepsOptimizerEnabled(config, true)) {
  48705. await initDevSsrDepsOptimizer(config, server);
  48706. }
  48707. let module = await server.moduleGraph.getModuleByUrl(url, ssr);
  48708. if (module) {
  48709. const cached = await getCachedTransformResult(
  48710. url,
  48711. module,
  48712. server,
  48713. ssr,
  48714. timestamp
  48715. );
  48716. if (cached) return cached;
  48717. }
  48718. const resolved = module ? void 0 : await pluginContainer.resolveId(url, void 0, { ssr }) ?? void 0;
  48719. const id = module?.id ?? resolved?.id ?? url;
  48720. module ??= server.moduleGraph.getModuleById(id);
  48721. if (module) {
  48722. await server.moduleGraph._ensureEntryFromUrl(url, ssr, void 0, resolved);
  48723. const cached = await getCachedTransformResult(
  48724. url,
  48725. module,
  48726. server,
  48727. ssr,
  48728. timestamp
  48729. );
  48730. if (cached) return cached;
  48731. }
  48732. const result = loadAndTransform(
  48733. id,
  48734. url,
  48735. server,
  48736. options,
  48737. timestamp,
  48738. module,
  48739. resolved
  48740. );
  48741. if (!ssr) {
  48742. const depsOptimizer = getDepsOptimizer(config, ssr);
  48743. if (!depsOptimizer?.isOptimizedDepFile(id)) {
  48744. server._registerRequestProcessing(id, () => result);
  48745. }
  48746. }
  48747. return result;
  48748. }
  48749. async function getCachedTransformResult(url, module, server, ssr, timestamp) {
  48750. const prettyUrl = debugCache$1 ? prettifyUrl(url, server.config.root) : "";
  48751. const softInvalidatedTransformResult = module && await handleModuleSoftInvalidation(module, ssr, timestamp, server);
  48752. if (softInvalidatedTransformResult) {
  48753. debugCache$1?.(`[memory-hmr] ${prettyUrl}`);
  48754. return softInvalidatedTransformResult;
  48755. }
  48756. const cached = module && (ssr ? module.ssrTransformResult : module.transformResult);
  48757. if (cached) {
  48758. debugCache$1?.(`[memory] ${prettyUrl}`);
  48759. return cached;
  48760. }
  48761. }
  48762. async function loadAndTransform(id, url, server, options, timestamp, mod, resolved) {
  48763. const { config, pluginContainer, moduleGraph } = server;
  48764. const { logger } = config;
  48765. const prettyUrl = debugLoad || debugTransform ? prettifyUrl(url, config.root) : "";
  48766. const ssr = !!options.ssr;
  48767. const file = cleanUrl(id);
  48768. let code = null;
  48769. let map = null;
  48770. const loadStart = debugLoad ? performance$1.now() : 0;
  48771. const loadResult = await pluginContainer.load(id, { ssr });
  48772. if (loadResult == null) {
  48773. if (options.html && !id.endsWith(".html")) {
  48774. return null;
  48775. }
  48776. if (options.ssr || isFileServingAllowed(file, server)) {
  48777. try {
  48778. code = await fsp.readFile(file, "utf-8");
  48779. debugLoad?.(`${timeFrom(loadStart)} [fs] ${prettyUrl}`);
  48780. } catch (e) {
  48781. if (e.code !== "ENOENT") {
  48782. if (e.code === "EISDIR") {
  48783. e.message = `${e.message} ${file}`;
  48784. }
  48785. throw e;
  48786. }
  48787. }
  48788. if (code != null) {
  48789. ensureWatchedFile(server.watcher, file, config.root);
  48790. }
  48791. }
  48792. if (code) {
  48793. try {
  48794. const extracted = await extractSourcemapFromFile(code, file);
  48795. if (extracted) {
  48796. code = extracted.code;
  48797. map = extracted.map;
  48798. }
  48799. } catch (e) {
  48800. logger.warn(`Failed to load source map for ${file}.
  48801. ${e}`, {
  48802. timestamp: true
  48803. });
  48804. }
  48805. }
  48806. } else {
  48807. debugLoad?.(`${timeFrom(loadStart)} [plugin] ${prettyUrl}`);
  48808. if (isObject$1(loadResult)) {
  48809. code = loadResult.code;
  48810. map = loadResult.map;
  48811. } else {
  48812. code = loadResult;
  48813. }
  48814. }
  48815. if (code == null) {
  48816. const isPublicFile = checkPublicFile(url, config);
  48817. let publicDirName = path$n.relative(config.root, config.publicDir);
  48818. if (publicDirName[0] !== ".") publicDirName = "/" + publicDirName;
  48819. const msg = isPublicFile ? `This file is in ${publicDirName} and will be copied as-is during build without going through the plugin transforms, and therefore should not be imported from source code. It can only be referenced via HTML tags.` : `Does the file exist?`;
  48820. const importerMod = server.moduleGraph.idToModuleMap.get(id)?.importers.values().next().value;
  48821. const importer = importerMod?.file || importerMod?.url;
  48822. const err = new Error(
  48823. `Failed to load url ${url} (resolved id: ${id})${importer ? ` in ${importer}` : ""}. ${msg}`
  48824. );
  48825. err.code = isPublicFile ? ERR_LOAD_PUBLIC_URL : ERR_LOAD_URL;
  48826. throw err;
  48827. }
  48828. if (server._restartPromise && !ssr) throwClosedServerError();
  48829. mod ??= await moduleGraph._ensureEntryFromUrl(url, ssr, void 0, resolved);
  48830. const transformStart = debugTransform ? performance$1.now() : 0;
  48831. const transformResult = await pluginContainer.transform(code, id, {
  48832. inMap: map,
  48833. ssr
  48834. });
  48835. const originalCode = code;
  48836. if (transformResult == null || isObject$1(transformResult) && transformResult.code == null) {
  48837. debugTransform?.(
  48838. timeFrom(transformStart) + colors$1.dim(` [skipped] ${prettyUrl}`)
  48839. );
  48840. } else {
  48841. debugTransform?.(`${timeFrom(transformStart)} ${prettyUrl}`);
  48842. code = transformResult.code;
  48843. map = transformResult.map;
  48844. }
  48845. let normalizedMap;
  48846. if (typeof map === "string") {
  48847. normalizedMap = JSON.parse(map);
  48848. } else if (map) {
  48849. normalizedMap = map;
  48850. } else {
  48851. normalizedMap = null;
  48852. }
  48853. if (normalizedMap && "version" in normalizedMap && mod.file) {
  48854. if (normalizedMap.mappings) {
  48855. await injectSourcesContent(normalizedMap, mod.file, logger);
  48856. }
  48857. const sourcemapPath = `${mod.file}.map`;
  48858. applySourcemapIgnoreList(
  48859. normalizedMap,
  48860. sourcemapPath,
  48861. config.server.sourcemapIgnoreList,
  48862. logger
  48863. );
  48864. if (path$n.isAbsolute(mod.file)) {
  48865. let modDirname;
  48866. for (let sourcesIndex = 0; sourcesIndex < normalizedMap.sources.length; ++sourcesIndex) {
  48867. const sourcePath = normalizedMap.sources[sourcesIndex];
  48868. if (sourcePath) {
  48869. if (path$n.isAbsolute(sourcePath)) {
  48870. modDirname ??= path$n.dirname(mod.file);
  48871. normalizedMap.sources[sourcesIndex] = path$n.relative(
  48872. modDirname,
  48873. sourcePath
  48874. );
  48875. }
  48876. }
  48877. }
  48878. }
  48879. }
  48880. if (server._restartPromise && !ssr) throwClosedServerError();
  48881. const result = ssr && !server.config.experimental.skipSsrTransform ? await server.ssrTransform(code, normalizedMap, url, originalCode) : {
  48882. code,
  48883. map: normalizedMap,
  48884. etag: getEtag(code, { weak: true })
  48885. };
  48886. if (timestamp > mod.lastInvalidationTimestamp)
  48887. moduleGraph.updateModuleTransformResult(mod, result, ssr);
  48888. return result;
  48889. }
  48890. async function handleModuleSoftInvalidation(mod, ssr, timestamp, server) {
  48891. const transformResult = ssr ? mod.ssrInvalidationState : mod.invalidationState;
  48892. if (ssr) mod.ssrInvalidationState = void 0;
  48893. else mod.invalidationState = void 0;
  48894. if (!transformResult || transformResult === "HARD_INVALIDATED") return;
  48895. if (ssr ? mod.ssrTransformResult : mod.transformResult) {
  48896. throw new Error(
  48897. `Internal server error: Soft-invalidated module "${mod.url}" should not have existing transform result`
  48898. );
  48899. }
  48900. let result;
  48901. if (ssr) {
  48902. result = transformResult;
  48903. } else {
  48904. await init;
  48905. const source = transformResult.code;
  48906. const s = new MagicString(source);
  48907. const [imports] = parse$d(source, mod.id || void 0);
  48908. for (const imp of imports) {
  48909. let rawUrl = source.slice(imp.s, imp.e);
  48910. if (rawUrl === "import.meta") continue;
  48911. const hasQuotes = rawUrl[0] === '"' || rawUrl[0] === "'";
  48912. if (hasQuotes) {
  48913. rawUrl = rawUrl.slice(1, -1);
  48914. }
  48915. const urlWithoutTimestamp = removeTimestampQuery(rawUrl);
  48916. const hmrUrl = unwrapId$1(
  48917. stripBase(removeImportQuery(urlWithoutTimestamp), server.config.base)
  48918. );
  48919. for (const importedMod of mod.clientImportedModules) {
  48920. if (importedMod.url !== hmrUrl) continue;
  48921. if (importedMod.lastHMRTimestamp > 0) {
  48922. const replacedUrl = injectQuery(
  48923. urlWithoutTimestamp,
  48924. `t=${importedMod.lastHMRTimestamp}`
  48925. );
  48926. const start = hasQuotes ? imp.s + 1 : imp.s;
  48927. const end = hasQuotes ? imp.e - 1 : imp.e;
  48928. s.overwrite(start, end, replacedUrl);
  48929. }
  48930. if (imp.d === -1 && server.config.server.preTransformRequests) {
  48931. server.warmupRequest(hmrUrl, { ssr });
  48932. }
  48933. break;
  48934. }
  48935. }
  48936. const code = s.toString();
  48937. result = {
  48938. ...transformResult,
  48939. code,
  48940. etag: getEtag(code, { weak: true })
  48941. };
  48942. }
  48943. if (timestamp > mod.lastInvalidationTimestamp)
  48944. server.moduleGraph.updateModuleTransformResult(mod, result, ssr);
  48945. return result;
  48946. }
  48947. function analyzeImportedModDifference(mod, rawId, moduleType, metadata) {
  48948. if (metadata?.isDynamicImport) return;
  48949. if (metadata?.importedNames?.length) {
  48950. const missingBindings = metadata.importedNames.filter((s) => !(s in mod));
  48951. if (missingBindings.length) {
  48952. const lastBinding = missingBindings[missingBindings.length - 1];
  48953. if (moduleType === "module") {
  48954. throw new SyntaxError(
  48955. `[vite] The requested module '${rawId}' does not provide an export named '${lastBinding}'`
  48956. );
  48957. } else {
  48958. throw new SyntaxError(`[vite] Named export '${lastBinding}' not found. The requested module '${rawId}' is a CommonJS module, which may not support all module.exports as named exports.
  48959. CommonJS modules can always be imported via the default export, for example using:
  48960. import pkg from '${rawId}';
  48961. const {${missingBindings.join(", ")}} = pkg;
  48962. `);
  48963. }
  48964. }
  48965. }
  48966. }
  48967. /**
  48968. * @param {import('estree').Node} param
  48969. * @returns {string[]}
  48970. */
  48971. function extract_names(param) {
  48972. return extract_identifiers(param).map((node) => node.name);
  48973. }
  48974. /**
  48975. * @param {import('estree').Node} param
  48976. * @param {import('estree').Identifier[]} nodes
  48977. * @returns {import('estree').Identifier[]}
  48978. */
  48979. function extract_identifiers(param, nodes = []) {
  48980. switch (param.type) {
  48981. case 'Identifier':
  48982. nodes.push(param);
  48983. break;
  48984. case 'MemberExpression':
  48985. let object = param;
  48986. while (object.type === 'MemberExpression') {
  48987. object = /** @type {any} */ (object.object);
  48988. }
  48989. nodes.push(/** @type {any} */ (object));
  48990. break;
  48991. case 'ObjectPattern':
  48992. for (const prop of param.properties) {
  48993. if (prop.type === 'RestElement') {
  48994. extract_identifiers(prop.argument, nodes);
  48995. } else {
  48996. extract_identifiers(prop.value, nodes);
  48997. }
  48998. }
  48999. break;
  49000. case 'ArrayPattern':
  49001. for (const element of param.elements) {
  49002. if (element) extract_identifiers(element, nodes);
  49003. }
  49004. break;
  49005. case 'RestElement':
  49006. extract_identifiers(param.argument, nodes);
  49007. break;
  49008. case 'AssignmentPattern':
  49009. extract_identifiers(param.left, nodes);
  49010. break;
  49011. }
  49012. return nodes;
  49013. }
  49014. /**
  49015. * @typedef { import('estree').Node} Node
  49016. * @typedef {{
  49017. * skip: () => void;
  49018. * remove: () => void;
  49019. * replace: (node: Node) => void;
  49020. * }} WalkerContext
  49021. */
  49022. class WalkerBase {
  49023. constructor() {
  49024. /** @type {boolean} */
  49025. this.should_skip = false;
  49026. /** @type {boolean} */
  49027. this.should_remove = false;
  49028. /** @type {Node | null} */
  49029. this.replacement = null;
  49030. /** @type {WalkerContext} */
  49031. this.context = {
  49032. skip: () => (this.should_skip = true),
  49033. remove: () => (this.should_remove = true),
  49034. replace: (node) => (this.replacement = node)
  49035. };
  49036. }
  49037. /**
  49038. * @template {Node} Parent
  49039. * @param {Parent | null | undefined} parent
  49040. * @param {keyof Parent | null | undefined} prop
  49041. * @param {number | null | undefined} index
  49042. * @param {Node} node
  49043. */
  49044. replace(parent, prop, index, node) {
  49045. if (parent && prop) {
  49046. if (index != null) {
  49047. /** @type {Array<Node>} */ (parent[prop])[index] = node;
  49048. } else {
  49049. /** @type {Node} */ (parent[prop]) = node;
  49050. }
  49051. }
  49052. }
  49053. /**
  49054. * @template {Node} Parent
  49055. * @param {Parent | null | undefined} parent
  49056. * @param {keyof Parent | null | undefined} prop
  49057. * @param {number | null | undefined} index
  49058. */
  49059. remove(parent, prop, index) {
  49060. if (parent && prop) {
  49061. if (index !== null && index !== undefined) {
  49062. /** @type {Array<Node>} */ (parent[prop]).splice(index, 1);
  49063. } else {
  49064. delete parent[prop];
  49065. }
  49066. }
  49067. }
  49068. }
  49069. /**
  49070. * @typedef { import('estree').Node} Node
  49071. * @typedef { import('./walker.js').WalkerContext} WalkerContext
  49072. * @typedef {(
  49073. * this: WalkerContext,
  49074. * node: Node,
  49075. * parent: Node | null,
  49076. * key: string | number | symbol | null | undefined,
  49077. * index: number | null | undefined
  49078. * ) => void} SyncHandler
  49079. */
  49080. class SyncWalker extends WalkerBase {
  49081. /**
  49082. *
  49083. * @param {SyncHandler} [enter]
  49084. * @param {SyncHandler} [leave]
  49085. */
  49086. constructor(enter, leave) {
  49087. super();
  49088. /** @type {boolean} */
  49089. this.should_skip = false;
  49090. /** @type {boolean} */
  49091. this.should_remove = false;
  49092. /** @type {Node | null} */
  49093. this.replacement = null;
  49094. /** @type {WalkerContext} */
  49095. this.context = {
  49096. skip: () => (this.should_skip = true),
  49097. remove: () => (this.should_remove = true),
  49098. replace: (node) => (this.replacement = node)
  49099. };
  49100. /** @type {SyncHandler | undefined} */
  49101. this.enter = enter;
  49102. /** @type {SyncHandler | undefined} */
  49103. this.leave = leave;
  49104. }
  49105. /**
  49106. * @template {Node} Parent
  49107. * @param {Node} node
  49108. * @param {Parent | null} parent
  49109. * @param {keyof Parent} [prop]
  49110. * @param {number | null} [index]
  49111. * @returns {Node | null}
  49112. */
  49113. visit(node, parent, prop, index) {
  49114. if (node) {
  49115. if (this.enter) {
  49116. const _should_skip = this.should_skip;
  49117. const _should_remove = this.should_remove;
  49118. const _replacement = this.replacement;
  49119. this.should_skip = false;
  49120. this.should_remove = false;
  49121. this.replacement = null;
  49122. this.enter.call(this.context, node, parent, prop, index);
  49123. if (this.replacement) {
  49124. node = this.replacement;
  49125. this.replace(parent, prop, index, node);
  49126. }
  49127. if (this.should_remove) {
  49128. this.remove(parent, prop, index);
  49129. }
  49130. const skipped = this.should_skip;
  49131. const removed = this.should_remove;
  49132. this.should_skip = _should_skip;
  49133. this.should_remove = _should_remove;
  49134. this.replacement = _replacement;
  49135. if (skipped) return node;
  49136. if (removed) return null;
  49137. }
  49138. /** @type {keyof Node} */
  49139. let key;
  49140. for (key in node) {
  49141. /** @type {unknown} */
  49142. const value = node[key];
  49143. if (value && typeof value === 'object') {
  49144. if (Array.isArray(value)) {
  49145. const nodes = /** @type {Array<unknown>} */ (value);
  49146. for (let i = 0; i < nodes.length; i += 1) {
  49147. const item = nodes[i];
  49148. if (isNode(item)) {
  49149. if (!this.visit(item, node, key, i)) {
  49150. // removed
  49151. i--;
  49152. }
  49153. }
  49154. }
  49155. } else if (isNode(value)) {
  49156. this.visit(value, node, key, null);
  49157. }
  49158. }
  49159. }
  49160. if (this.leave) {
  49161. const _replacement = this.replacement;
  49162. const _should_remove = this.should_remove;
  49163. this.replacement = null;
  49164. this.should_remove = false;
  49165. this.leave.call(this.context, node, parent, prop, index);
  49166. if (this.replacement) {
  49167. node = this.replacement;
  49168. this.replace(parent, prop, index, node);
  49169. }
  49170. if (this.should_remove) {
  49171. this.remove(parent, prop, index);
  49172. }
  49173. const removed = this.should_remove;
  49174. this.replacement = _replacement;
  49175. this.should_remove = _should_remove;
  49176. if (removed) return null;
  49177. }
  49178. }
  49179. return node;
  49180. }
  49181. }
  49182. /**
  49183. * Ducktype a node.
  49184. *
  49185. * @param {unknown} value
  49186. * @returns {value is Node}
  49187. */
  49188. function isNode(value) {
  49189. return (
  49190. value !== null && typeof value === 'object' && 'type' in value && typeof value.type === 'string'
  49191. );
  49192. }
  49193. /**
  49194. * @typedef {import('estree').Node} Node
  49195. * @typedef {import('./sync.js').SyncHandler} SyncHandler
  49196. * @typedef {import('./async.js').AsyncHandler} AsyncHandler
  49197. */
  49198. /**
  49199. * @param {Node} ast
  49200. * @param {{
  49201. * enter?: SyncHandler
  49202. * leave?: SyncHandler
  49203. * }} walker
  49204. * @returns {Node | null}
  49205. */
  49206. function walk$1(ast, { enter, leave }) {
  49207. const instance = new SyncWalker(enter, leave);
  49208. return instance.visit(ast, null);
  49209. }
  49210. const ssrModuleExportsKey = `__vite_ssr_exports__`;
  49211. const ssrImportKey = `__vite_ssr_import__`;
  49212. const ssrDynamicImportKey = `__vite_ssr_dynamic_import__`;
  49213. const ssrExportAllKey = `__vite_ssr_exportAll__`;
  49214. const ssrImportMetaKey = `__vite_ssr_import_meta__`;
  49215. const hashbangRE = /^#!.*\n/;
  49216. async function ssrTransform(code, inMap, url, originalCode, options) {
  49217. if (options?.json?.stringify && isJSONRequest(url)) {
  49218. return ssrTransformJSON(code, inMap);
  49219. }
  49220. return ssrTransformScript(code, inMap, url, originalCode);
  49221. }
  49222. async function ssrTransformJSON(code, inMap) {
  49223. return {
  49224. code: code.replace("export default", `${ssrModuleExportsKey}.default =`),
  49225. map: inMap,
  49226. deps: [],
  49227. dynamicDeps: []
  49228. };
  49229. }
  49230. async function ssrTransformScript(code, inMap, url, originalCode) {
  49231. const s = new MagicString(code);
  49232. let ast;
  49233. try {
  49234. ast = await parseAstAsync(code);
  49235. } catch (err) {
  49236. if (!err.loc || !err.loc.line) throw err;
  49237. const line = err.loc.line;
  49238. throw new Error(
  49239. `Parse failure: ${err.message}
  49240. At file: ${url}
  49241. Contents of line ${line}: ${code.split("\n")[line - 1]}`
  49242. );
  49243. }
  49244. let uid = 0;
  49245. const deps = /* @__PURE__ */ new Set();
  49246. const dynamicDeps = /* @__PURE__ */ new Set();
  49247. const idToImportMap = /* @__PURE__ */ new Map();
  49248. const declaredConst = /* @__PURE__ */ new Set();
  49249. const hoistIndex = hashbangRE.exec(code)?.[0].length ?? 0;
  49250. function defineImport(index, source, metadata) {
  49251. deps.add(source);
  49252. const importId = `__vite_ssr_import_${uid++}__`;
  49253. if (metadata && (metadata.importedNames == null || metadata.importedNames.length === 0)) {
  49254. metadata = void 0;
  49255. }
  49256. const metadataStr = metadata ? `, ${JSON.stringify(metadata)}` : "";
  49257. s.appendLeft(
  49258. index,
  49259. `const ${importId} = await ${ssrImportKey}(${JSON.stringify(
  49260. source
  49261. )}${metadataStr});
  49262. `
  49263. );
  49264. return importId;
  49265. }
  49266. function defineExport(position, name, local = name) {
  49267. s.appendLeft(
  49268. position,
  49269. `
  49270. Object.defineProperty(${ssrModuleExportsKey}, "${name}", { enumerable: true, configurable: true, get(){ return ${local} }});`
  49271. );
  49272. }
  49273. const imports = [];
  49274. const exports = [];
  49275. for (const node of ast.body) {
  49276. if (node.type === "ImportDeclaration") {
  49277. imports.push(node);
  49278. } else if (node.type === "ExportNamedDeclaration" || node.type === "ExportDefaultDeclaration" || node.type === "ExportAllDeclaration") {
  49279. exports.push(node);
  49280. }
  49281. }
  49282. for (const node of imports) {
  49283. const importId = defineImport(hoistIndex, node.source.value, {
  49284. importedNames: node.specifiers.map((s2) => {
  49285. if (s2.type === "ImportSpecifier")
  49286. return s2.imported.type === "Identifier" ? s2.imported.name : (
  49287. // @ts-expect-error TODO: Estree types don't consider arbitrary module namespace specifiers yet
  49288. s2.imported.value
  49289. );
  49290. else if (s2.type === "ImportDefaultSpecifier") return "default";
  49291. }).filter(isDefined)
  49292. });
  49293. s.remove(node.start, node.end);
  49294. for (const spec of node.specifiers) {
  49295. if (spec.type === "ImportSpecifier") {
  49296. if (spec.imported.type === "Identifier") {
  49297. idToImportMap.set(
  49298. spec.local.name,
  49299. `${importId}.${spec.imported.name}`
  49300. );
  49301. } else {
  49302. idToImportMap.set(
  49303. spec.local.name,
  49304. `${importId}[${// @ts-expect-error TODO: Estree types don't consider arbitrary module namespace specifiers yet
  49305. JSON.stringify(spec.imported.value)}]`
  49306. );
  49307. }
  49308. } else if (spec.type === "ImportDefaultSpecifier") {
  49309. idToImportMap.set(spec.local.name, `${importId}.default`);
  49310. } else {
  49311. idToImportMap.set(spec.local.name, importId);
  49312. }
  49313. }
  49314. }
  49315. for (const node of exports) {
  49316. if (node.type === "ExportNamedDeclaration") {
  49317. if (node.declaration) {
  49318. if (node.declaration.type === "FunctionDeclaration" || node.declaration.type === "ClassDeclaration") {
  49319. defineExport(node.end, node.declaration.id.name);
  49320. } else {
  49321. for (const declaration of node.declaration.declarations) {
  49322. const names = extract_names(declaration.id);
  49323. for (const name of names) {
  49324. defineExport(node.end, name);
  49325. }
  49326. }
  49327. }
  49328. s.remove(node.start, node.declaration.start);
  49329. } else {
  49330. s.remove(node.start, node.end);
  49331. if (node.source) {
  49332. const importId = defineImport(
  49333. node.start,
  49334. node.source.value,
  49335. {
  49336. importedNames: node.specifiers.map((s2) => s2.local.name)
  49337. }
  49338. );
  49339. for (const spec of node.specifiers) {
  49340. const exportedAs = spec.exported.type === "Identifier" ? spec.exported.name : (
  49341. // @ts-expect-error TODO: Estree types don't consider arbitrary module namespace specifiers yet
  49342. spec.exported.value
  49343. );
  49344. defineExport(
  49345. node.start,
  49346. exportedAs,
  49347. `${importId}.${spec.local.name}`
  49348. );
  49349. }
  49350. } else {
  49351. for (const spec of node.specifiers) {
  49352. const local = spec.local.name;
  49353. const binding = idToImportMap.get(local);
  49354. const exportedAs = spec.exported.type === "Identifier" ? spec.exported.name : (
  49355. // @ts-expect-error TODO: Estree types don't consider arbitrary module namespace specifiers yet
  49356. spec.exported.value
  49357. );
  49358. defineExport(node.end, exportedAs, binding || local);
  49359. }
  49360. }
  49361. }
  49362. }
  49363. if (node.type === "ExportDefaultDeclaration") {
  49364. const expressionTypes = ["FunctionExpression", "ClassExpression"];
  49365. if ("id" in node.declaration && node.declaration.id && !expressionTypes.includes(node.declaration.type)) {
  49366. const { name } = node.declaration.id;
  49367. s.remove(
  49368. node.start,
  49369. node.start + 15
  49370. /* 'export default '.length */
  49371. );
  49372. s.append(
  49373. `
  49374. Object.defineProperty(${ssrModuleExportsKey}, "default", { enumerable: true, configurable: true, value: ${name} });`
  49375. );
  49376. } else {
  49377. s.update(
  49378. node.start,
  49379. node.start + 14,
  49380. `${ssrModuleExportsKey}.default =`
  49381. );
  49382. }
  49383. }
  49384. if (node.type === "ExportAllDeclaration") {
  49385. s.remove(node.start, node.end);
  49386. const importId = defineImport(node.start, node.source.value);
  49387. if (node.exported) {
  49388. defineExport(node.start, node.exported.name, `${importId}`);
  49389. } else {
  49390. s.appendLeft(node.start, `${ssrExportAllKey}(${importId});
  49391. `);
  49392. }
  49393. }
  49394. }
  49395. walk(ast, {
  49396. onIdentifier(id, parent, parentStack) {
  49397. const grandparent = parentStack[1];
  49398. const binding = idToImportMap.get(id.name);
  49399. if (!binding) {
  49400. return;
  49401. }
  49402. if (isStaticProperty(parent) && parent.shorthand) {
  49403. if (!isNodeInPattern(parent) || isInDestructuringAssignment(parent, parentStack)) {
  49404. s.appendLeft(id.end, `: ${binding}`);
  49405. }
  49406. } else if (parent.type === "PropertyDefinition" && grandparent?.type === "ClassBody" || parent.type === "ClassDeclaration" && id === parent.superClass) {
  49407. if (!declaredConst.has(id.name)) {
  49408. declaredConst.add(id.name);
  49409. const topNode = parentStack[parentStack.length - 2];
  49410. s.prependRight(topNode.start, `const ${id.name} = ${binding};
  49411. `);
  49412. }
  49413. } else if (
  49414. // don't transform class name identifier
  49415. !(parent.type === "ClassExpression" && id === parent.id)
  49416. ) {
  49417. s.update(id.start, id.end, binding);
  49418. }
  49419. },
  49420. onImportMeta(node) {
  49421. s.update(node.start, node.end, ssrImportMetaKey);
  49422. },
  49423. onDynamicImport(node) {
  49424. s.update(node.start, node.start + 6, ssrDynamicImportKey);
  49425. if (node.type === "ImportExpression" && node.source.type === "Literal") {
  49426. dynamicDeps.add(node.source.value);
  49427. }
  49428. }
  49429. });
  49430. let map = s.generateMap({ hires: "boundary" });
  49431. map.sources = [path$n.basename(url)];
  49432. map.sourcesContent = [originalCode];
  49433. if (inMap && inMap.mappings && "sources" in inMap && inMap.sources.length > 0) {
  49434. map = combineSourcemaps(url, [
  49435. map,
  49436. inMap
  49437. ]);
  49438. }
  49439. return {
  49440. code: s.toString(),
  49441. map,
  49442. deps: [...deps],
  49443. dynamicDeps: [...dynamicDeps]
  49444. };
  49445. }
  49446. const isNodeInPatternWeakSet = /* @__PURE__ */ new WeakSet();
  49447. const setIsNodeInPattern = (node) => isNodeInPatternWeakSet.add(node);
  49448. const isNodeInPattern = (node) => isNodeInPatternWeakSet.has(node);
  49449. function walk(root, { onIdentifier, onImportMeta, onDynamicImport }) {
  49450. const parentStack = [];
  49451. const varKindStack = [];
  49452. const scopeMap = /* @__PURE__ */ new WeakMap();
  49453. const identifiers = [];
  49454. const setScope = (node, name) => {
  49455. let scopeIds = scopeMap.get(node);
  49456. if (scopeIds && scopeIds.has(name)) {
  49457. return;
  49458. }
  49459. if (!scopeIds) {
  49460. scopeIds = /* @__PURE__ */ new Set();
  49461. scopeMap.set(node, scopeIds);
  49462. }
  49463. scopeIds.add(name);
  49464. };
  49465. function isInScope(name, parents) {
  49466. return parents.some((node) => node && scopeMap.get(node)?.has(name));
  49467. }
  49468. function handlePattern(p, parentScope) {
  49469. if (p.type === "Identifier") {
  49470. setScope(parentScope, p.name);
  49471. } else if (p.type === "RestElement") {
  49472. handlePattern(p.argument, parentScope);
  49473. } else if (p.type === "ObjectPattern") {
  49474. p.properties.forEach((property) => {
  49475. if (property.type === "RestElement") {
  49476. setScope(parentScope, property.argument.name);
  49477. } else {
  49478. handlePattern(property.value, parentScope);
  49479. }
  49480. });
  49481. } else if (p.type === "ArrayPattern") {
  49482. p.elements.forEach((element) => {
  49483. if (element) {
  49484. handlePattern(element, parentScope);
  49485. }
  49486. });
  49487. } else if (p.type === "AssignmentPattern") {
  49488. handlePattern(p.left, parentScope);
  49489. } else {
  49490. setScope(parentScope, p.name);
  49491. }
  49492. }
  49493. walk$1(root, {
  49494. enter(node, parent) {
  49495. if (node.type === "ImportDeclaration") {
  49496. return this.skip();
  49497. }
  49498. if (parent && !(parent.type === "IfStatement" && node === parent.alternate)) {
  49499. parentStack.unshift(parent);
  49500. }
  49501. if (node.type === "VariableDeclaration") {
  49502. varKindStack.unshift(node.kind);
  49503. }
  49504. if (node.type === "MetaProperty" && node.meta.name === "import") {
  49505. onImportMeta(node);
  49506. } else if (node.type === "ImportExpression") {
  49507. onDynamicImport(node);
  49508. }
  49509. if (node.type === "Identifier") {
  49510. if (!isInScope(node.name, parentStack) && isRefIdentifier(node, parent, parentStack)) {
  49511. identifiers.push([node, parentStack.slice(0)]);
  49512. }
  49513. } else if (isFunction$1(node)) {
  49514. if (node.type === "FunctionDeclaration") {
  49515. const parentScope = findParentScope(parentStack);
  49516. if (parentScope) {
  49517. setScope(parentScope, node.id.name);
  49518. }
  49519. }
  49520. if (node.type === "FunctionExpression" && node.id) {
  49521. setScope(node, node.id.name);
  49522. }
  49523. node.params.forEach((p) => {
  49524. if (p.type === "ObjectPattern" || p.type === "ArrayPattern") {
  49525. handlePattern(p, node);
  49526. return;
  49527. }
  49528. walk$1(p.type === "AssignmentPattern" ? p.left : p, {
  49529. enter(child, parent2) {
  49530. if (parent2?.type === "AssignmentPattern" && parent2?.right === child) {
  49531. return this.skip();
  49532. }
  49533. if (child.type !== "Identifier") return;
  49534. if (isStaticPropertyKey(child, parent2)) return;
  49535. if (parent2?.type === "TemplateLiteral" && parent2?.expressions.includes(child) || parent2?.type === "CallExpression" && parent2?.callee === child) {
  49536. return;
  49537. }
  49538. setScope(node, child.name);
  49539. }
  49540. });
  49541. });
  49542. } else if (node.type === "ClassDeclaration") {
  49543. const parentScope = findParentScope(parentStack);
  49544. if (parentScope) {
  49545. setScope(parentScope, node.id.name);
  49546. }
  49547. } else if (node.type === "ClassExpression" && node.id) {
  49548. setScope(node, node.id.name);
  49549. } else if (node.type === "Property" && parent.type === "ObjectPattern") {
  49550. setIsNodeInPattern(node);
  49551. } else if (node.type === "VariableDeclarator") {
  49552. const parentFunction = findParentScope(
  49553. parentStack,
  49554. varKindStack[0] === "var"
  49555. );
  49556. if (parentFunction) {
  49557. handlePattern(node.id, parentFunction);
  49558. }
  49559. } else if (node.type === "CatchClause" && node.param) {
  49560. handlePattern(node.param, node);
  49561. }
  49562. },
  49563. leave(node, parent) {
  49564. if (parent && !(parent.type === "IfStatement" && node === parent.alternate)) {
  49565. parentStack.shift();
  49566. }
  49567. if (node.type === "VariableDeclaration") {
  49568. varKindStack.shift();
  49569. }
  49570. }
  49571. });
  49572. identifiers.forEach(([node, stack]) => {
  49573. if (!isInScope(node.name, stack)) onIdentifier(node, stack[0], stack);
  49574. });
  49575. }
  49576. function isRefIdentifier(id, parent, parentStack) {
  49577. if (parent.type === "CatchClause" || (parent.type === "VariableDeclarator" || parent.type === "ClassDeclaration") && parent.id === id) {
  49578. return false;
  49579. }
  49580. if (isFunction$1(parent)) {
  49581. if (parent.id === id) {
  49582. return false;
  49583. }
  49584. if (parent.params.includes(id)) {
  49585. return false;
  49586. }
  49587. }
  49588. if (parent.type === "MethodDefinition" && !parent.computed) {
  49589. return false;
  49590. }
  49591. if (isStaticPropertyKey(id, parent)) {
  49592. return false;
  49593. }
  49594. if (isNodeInPattern(parent) && parent.value === id) {
  49595. return false;
  49596. }
  49597. if (parent.type === "ArrayPattern" && !isInDestructuringAssignment(parent, parentStack)) {
  49598. return false;
  49599. }
  49600. if (parent.type === "MemberExpression" && parent.property === id && !parent.computed) {
  49601. return false;
  49602. }
  49603. if (parent.type === "ExportSpecifier") {
  49604. return false;
  49605. }
  49606. if (id.name === "arguments") {
  49607. return false;
  49608. }
  49609. return true;
  49610. }
  49611. const isStaticProperty = (node) => node && node.type === "Property" && !node.computed;
  49612. const isStaticPropertyKey = (node, parent) => isStaticProperty(parent) && parent.key === node;
  49613. const functionNodeTypeRE = /Function(?:Expression|Declaration)$|Method$/;
  49614. function isFunction$1(node) {
  49615. return functionNodeTypeRE.test(node.type);
  49616. }
  49617. const blockNodeTypeRE = /^BlockStatement$|^For(?:In|Of)?Statement$/;
  49618. function isBlock(node) {
  49619. return blockNodeTypeRE.test(node.type);
  49620. }
  49621. function findParentScope(parentStack, isVar = false) {
  49622. return parentStack.find(isVar ? isFunction$1 : isBlock);
  49623. }
  49624. function isInDestructuringAssignment(parent, parentStack) {
  49625. if (parent && (parent.type === "Property" || parent.type === "ArrayPattern")) {
  49626. return parentStack.some((i) => i.type === "AssignmentExpression");
  49627. }
  49628. return false;
  49629. }
  49630. let offset;
  49631. function calculateOffsetOnce() {
  49632. if (offset !== void 0) {
  49633. return;
  49634. }
  49635. try {
  49636. new Function("throw new Error(1)")();
  49637. } catch (e) {
  49638. const match = /:(\d+):\d+\)$/.exec(e.stack.split("\n")[1]);
  49639. offset = match ? +match[1] - 1 : 0;
  49640. }
  49641. }
  49642. function ssrRewriteStacktrace(stack, moduleGraph) {
  49643. calculateOffsetOnce();
  49644. return stack.split("\n").map((line) => {
  49645. return line.replace(
  49646. /^ {4}at (?:(\S.*?)\s\()?(.+?):(\d+)(?::(\d+))?\)?/,
  49647. (input, varName, id, line2, column) => {
  49648. if (!id) return input;
  49649. const mod = moduleGraph.idToModuleMap.get(id);
  49650. const rawSourceMap = mod?.ssrTransformResult?.map;
  49651. if (!rawSourceMap) {
  49652. return input;
  49653. }
  49654. const traced = new TraceMap(rawSourceMap);
  49655. const pos = originalPositionFor$1(traced, {
  49656. line: Number(line2) - offset,
  49657. // stacktrace's column is 1-indexed, but sourcemap's one is 0-indexed
  49658. column: Number(column) - 1
  49659. });
  49660. if (!pos.source || pos.line == null || pos.column == null) {
  49661. return input;
  49662. }
  49663. const trimmedVarName = varName.trim();
  49664. const sourceFile = path$n.resolve(path$n.dirname(id), pos.source);
  49665. const source = `${sourceFile}:${pos.line}:${pos.column + 1}`;
  49666. if (!trimmedVarName || trimmedVarName === "eval") {
  49667. return ` at ${source}`;
  49668. } else {
  49669. return ` at ${trimmedVarName} (${source})`;
  49670. }
  49671. }
  49672. );
  49673. }).join("\n");
  49674. }
  49675. function rebindErrorStacktrace(e, stacktrace) {
  49676. const { configurable, writable } = Object.getOwnPropertyDescriptor(
  49677. e,
  49678. "stack"
  49679. );
  49680. if (configurable) {
  49681. Object.defineProperty(e, "stack", {
  49682. value: stacktrace,
  49683. enumerable: true,
  49684. configurable: true,
  49685. writable: true
  49686. });
  49687. } else if (writable) {
  49688. e.stack = stacktrace;
  49689. }
  49690. }
  49691. const rewroteStacktraces = /* @__PURE__ */ new WeakSet();
  49692. function ssrFixStacktrace(e, moduleGraph) {
  49693. if (!e.stack) return;
  49694. if (rewroteStacktraces.has(e)) return;
  49695. const stacktrace = ssrRewriteStacktrace(e.stack, moduleGraph);
  49696. rebindErrorStacktrace(e, stacktrace);
  49697. rewroteStacktraces.add(e);
  49698. }
  49699. const pendingModules = /* @__PURE__ */ new Map();
  49700. const pendingModuleDependencyGraph = /* @__PURE__ */ new Map();
  49701. const importErrors = /* @__PURE__ */ new WeakMap();
  49702. async function ssrLoadModule(url, server, fixStacktrace) {
  49703. url = unwrapId$1(url);
  49704. const pending = pendingModules.get(url);
  49705. if (pending) {
  49706. return pending;
  49707. }
  49708. const modulePromise = instantiateModule(url, server, fixStacktrace);
  49709. pendingModules.set(url, modulePromise);
  49710. modulePromise.catch(() => {
  49711. }).finally(() => {
  49712. pendingModules.delete(url);
  49713. });
  49714. return modulePromise;
  49715. }
  49716. async function instantiateModule(url, server, fixStacktrace) {
  49717. const { moduleGraph } = server;
  49718. const mod = await moduleGraph.ensureEntryFromUrl(url, true);
  49719. if (mod.ssrError) {
  49720. throw mod.ssrError;
  49721. }
  49722. if (mod.ssrModule) {
  49723. return mod.ssrModule;
  49724. }
  49725. const result = mod.ssrTransformResult || await transformRequest(url, server, { ssr: true });
  49726. if (!result) {
  49727. throw new Error(`failed to load module for ssr: ${url}`);
  49728. }
  49729. const ssrModule = {
  49730. [Symbol.toStringTag]: "Module"
  49731. };
  49732. Object.defineProperty(ssrModule, "__esModule", { value: true });
  49733. mod.ssrModule = ssrModule;
  49734. const osNormalizedFilename = isWindows$3 ? path$n.resolve(mod.file) : mod.file;
  49735. const ssrImportMeta = {
  49736. dirname: path$n.dirname(osNormalizedFilename),
  49737. filename: osNormalizedFilename,
  49738. // The filesystem URL, matching native Node.js modules
  49739. url: pathToFileURL(mod.file).toString()
  49740. };
  49741. const {
  49742. isProduction,
  49743. resolve: { dedupe, preserveSymlinks },
  49744. root,
  49745. ssr
  49746. } = server.config;
  49747. const overrideConditions = ssr.resolve?.externalConditions || [];
  49748. const resolveOptions = {
  49749. mainFields: ["main"],
  49750. conditions: [],
  49751. overrideConditions: [...overrideConditions, "production", "development"],
  49752. extensions: [".js", ".cjs", ".json"],
  49753. dedupe,
  49754. preserveSymlinks,
  49755. isBuild: false,
  49756. isProduction,
  49757. root,
  49758. ssrConfig: ssr,
  49759. legacyProxySsrExternalModules: server.config.legacy?.proxySsrExternalModules,
  49760. packageCache: server.config.packageCache
  49761. };
  49762. const ssrImport = async (dep, metadata) => {
  49763. try {
  49764. if (dep[0] !== "." && dep[0] !== "/") {
  49765. return await nodeImport(dep, mod.file, resolveOptions, metadata);
  49766. }
  49767. dep = unwrapId$1(dep);
  49768. if (!metadata?.isDynamicImport) {
  49769. addPendingModuleDependency(url, dep);
  49770. if (checkModuleDependencyExists(dep, url)) {
  49771. const depSsrModule = moduleGraph.urlToModuleMap.get(dep)?.ssrModule;
  49772. if (!depSsrModule) {
  49773. throw new Error(
  49774. "[vite] The dependency module is not yet fully initialized due to circular dependency. This is a bug in Vite SSR"
  49775. );
  49776. }
  49777. return depSsrModule;
  49778. }
  49779. }
  49780. return ssrLoadModule(dep, server, fixStacktrace);
  49781. } catch (err) {
  49782. importErrors.set(err, { importee: dep });
  49783. throw err;
  49784. }
  49785. };
  49786. const ssrDynamicImport = (dep) => {
  49787. if (dep[0] === ".") {
  49788. dep = path$n.posix.resolve(path$n.dirname(url), dep);
  49789. }
  49790. return ssrImport(dep, { isDynamicImport: true });
  49791. };
  49792. function ssrExportAll(sourceModule) {
  49793. for (const key in sourceModule) {
  49794. if (key !== "default" && key !== "__esModule") {
  49795. Object.defineProperty(ssrModule, key, {
  49796. enumerable: true,
  49797. configurable: true,
  49798. get() {
  49799. return sourceModule[key];
  49800. }
  49801. });
  49802. }
  49803. }
  49804. }
  49805. let sourceMapSuffix = "";
  49806. if (result.map && "version" in result.map) {
  49807. const moduleSourceMap = Object.assign({}, result.map, {
  49808. mappings: ";".repeat(asyncFunctionDeclarationPaddingLineCount) + result.map.mappings
  49809. });
  49810. sourceMapSuffix = `
  49811. //# ${SOURCEMAPPING_URL}=${genSourceMapUrl(moduleSourceMap)}`;
  49812. }
  49813. try {
  49814. const initModule = new AsyncFunction(
  49815. ssrModuleExportsKey,
  49816. ssrImportMetaKey,
  49817. ssrImportKey,
  49818. ssrDynamicImportKey,
  49819. ssrExportAllKey,
  49820. '"use strict";' + result.code + `
  49821. //# sourceURL=${mod.id}${sourceMapSuffix}`
  49822. );
  49823. await initModule(
  49824. ssrModule,
  49825. ssrImportMeta,
  49826. ssrImport,
  49827. ssrDynamicImport,
  49828. ssrExportAll
  49829. );
  49830. } catch (e) {
  49831. mod.ssrError = e;
  49832. const errorData = importErrors.get(e);
  49833. if (e.stack && fixStacktrace) {
  49834. ssrFixStacktrace(e, moduleGraph);
  49835. }
  49836. server.config.logger.error(
  49837. colors$1.red(
  49838. `Error when evaluating SSR module ${url}:` + (errorData?.importee ? ` failed to import "${errorData.importee}"` : "") + `
  49839. |- ${e.stack}
  49840. `
  49841. ),
  49842. {
  49843. timestamp: true,
  49844. clear: server.config.clearScreen,
  49845. error: e
  49846. }
  49847. );
  49848. throw e;
  49849. } finally {
  49850. pendingModuleDependencyGraph.delete(url);
  49851. }
  49852. return Object.freeze(ssrModule);
  49853. }
  49854. function addPendingModuleDependency(originUrl, depUrl) {
  49855. if (pendingModuleDependencyGraph.has(originUrl)) {
  49856. pendingModuleDependencyGraph.get(originUrl).add(depUrl);
  49857. } else {
  49858. pendingModuleDependencyGraph.set(originUrl, /* @__PURE__ */ new Set([depUrl]));
  49859. }
  49860. }
  49861. function checkModuleDependencyExists(originUrl, targetUrl) {
  49862. const visited = /* @__PURE__ */ new Set();
  49863. const stack = [originUrl];
  49864. while (stack.length) {
  49865. const currentUrl = stack.pop();
  49866. if (currentUrl === targetUrl) {
  49867. return true;
  49868. }
  49869. if (!visited.has(currentUrl)) {
  49870. visited.add(currentUrl);
  49871. const dependencies = pendingModuleDependencyGraph.get(currentUrl);
  49872. if (dependencies) {
  49873. for (const depUrl of dependencies) {
  49874. if (!visited.has(depUrl)) {
  49875. stack.push(depUrl);
  49876. }
  49877. }
  49878. }
  49879. }
  49880. }
  49881. return false;
  49882. }
  49883. async function nodeImport(id, importer, resolveOptions, metadata) {
  49884. let url;
  49885. let filePath;
  49886. if (id.startsWith("data:") || isExternalUrl(id) || isBuiltin(id)) {
  49887. url = id;
  49888. } else {
  49889. const resolved = tryNodeResolve(
  49890. id,
  49891. importer,
  49892. { ...resolveOptions, tryEsmOnly: true },
  49893. false,
  49894. void 0,
  49895. true
  49896. );
  49897. if (!resolved) {
  49898. const err = new Error(
  49899. `Cannot find module '${id}' imported from '${importer}'`
  49900. );
  49901. err.code = "ERR_MODULE_NOT_FOUND";
  49902. throw err;
  49903. }
  49904. filePath = resolved.id;
  49905. url = pathToFileURL(resolved.id).toString();
  49906. }
  49907. const mod = await import(url);
  49908. if (resolveOptions.legacyProxySsrExternalModules) {
  49909. return proxyESM(mod);
  49910. } else if (filePath) {
  49911. analyzeImportedModDifference(
  49912. mod,
  49913. id,
  49914. isFilePathESM(filePath, resolveOptions.packageCache) ? "module" : void 0,
  49915. metadata
  49916. );
  49917. return mod;
  49918. } else {
  49919. return mod;
  49920. }
  49921. }
  49922. function proxyESM(mod) {
  49923. if (isPrimitive(mod)) return { default: mod };
  49924. let defaultExport = "default" in mod ? mod.default : mod;
  49925. if (!isPrimitive(defaultExport) && "__esModule" in defaultExport) {
  49926. mod = defaultExport;
  49927. if ("default" in defaultExport) {
  49928. defaultExport = defaultExport.default;
  49929. }
  49930. }
  49931. return new Proxy(mod, {
  49932. get(mod2, prop) {
  49933. if (prop === "default") return defaultExport;
  49934. return mod2[prop] ?? defaultExport?.[prop];
  49935. }
  49936. });
  49937. }
  49938. function isPrimitive(value) {
  49939. return !value || typeof value !== "object" && typeof value !== "function";
  49940. }
  49941. var isWsl$2 = {exports: {}};
  49942. const fs$3 = require$$0__default;
  49943. let isDocker$2;
  49944. function hasDockerEnv() {
  49945. try {
  49946. fs$3.statSync('/.dockerenv');
  49947. return true;
  49948. } catch (_) {
  49949. return false;
  49950. }
  49951. }
  49952. function hasDockerCGroup() {
  49953. try {
  49954. return fs$3.readFileSync('/proc/self/cgroup', 'utf8').includes('docker');
  49955. } catch (_) {
  49956. return false;
  49957. }
  49958. }
  49959. var isDocker_1 = () => {
  49960. if (isDocker$2 === undefined) {
  49961. isDocker$2 = hasDockerEnv() || hasDockerCGroup();
  49962. }
  49963. return isDocker$2;
  49964. };
  49965. const os = require$$2;
  49966. const fs$2 = require$$0__default;
  49967. const isDocker$1 = isDocker_1;
  49968. const isWsl$1 = () => {
  49969. if (process.platform !== 'linux') {
  49970. return false;
  49971. }
  49972. if (os.release().toLowerCase().includes('microsoft')) {
  49973. if (isDocker$1()) {
  49974. return false;
  49975. }
  49976. return true;
  49977. }
  49978. try {
  49979. return fs$2.readFileSync('/proc/version', 'utf8').toLowerCase().includes('microsoft') ?
  49980. !isDocker$1() : false;
  49981. } catch (_) {
  49982. return false;
  49983. }
  49984. };
  49985. if (process.env.__IS_WSL_TEST__) {
  49986. isWsl$2.exports = isWsl$1;
  49987. } else {
  49988. isWsl$2.exports = isWsl$1();
  49989. }
  49990. var isWslExports = isWsl$2.exports;
  49991. var defineLazyProp = (object, propertyName, fn) => {
  49992. const define = value => Object.defineProperty(object, propertyName, {value, enumerable: true, writable: true});
  49993. Object.defineProperty(object, propertyName, {
  49994. configurable: true,
  49995. enumerable: true,
  49996. get() {
  49997. const result = fn();
  49998. define(result);
  49999. return result;
  50000. },
  50001. set(value) {
  50002. define(value);
  50003. }
  50004. });
  50005. return object;
  50006. };
  50007. const path$3 = require$$0$4;
  50008. const childProcess = require$$2$1;
  50009. const {promises: fs$1, constants: fsConstants} = require$$0__default;
  50010. const isWsl = isWslExports;
  50011. const isDocker = isDocker_1;
  50012. const defineLazyProperty = defineLazyProp;
  50013. // Path to included `xdg-open`.
  50014. const localXdgOpenPath = path$3.join(__dirname, 'xdg-open');
  50015. const {platform, arch} = process;
  50016. // Podman detection
  50017. const hasContainerEnv = () => {
  50018. try {
  50019. fs$1.statSync('/run/.containerenv');
  50020. return true;
  50021. } catch {
  50022. return false;
  50023. }
  50024. };
  50025. let cachedResult;
  50026. function isInsideContainer() {
  50027. if (cachedResult === undefined) {
  50028. cachedResult = hasContainerEnv() || isDocker();
  50029. }
  50030. return cachedResult;
  50031. }
  50032. /**
  50033. Get the mount point for fixed drives in WSL.
  50034. @inner
  50035. @returns {string} The mount point.
  50036. */
  50037. const getWslDrivesMountPoint = (() => {
  50038. // Default value for "root" param
  50039. // according to https://docs.microsoft.com/en-us/windows/wsl/wsl-config
  50040. const defaultMountPoint = '/mnt/';
  50041. let mountPoint;
  50042. return async function () {
  50043. if (mountPoint) {
  50044. // Return memoized mount point value
  50045. return mountPoint;
  50046. }
  50047. const configFilePath = '/etc/wsl.conf';
  50048. let isConfigFileExists = false;
  50049. try {
  50050. await fs$1.access(configFilePath, fsConstants.F_OK);
  50051. isConfigFileExists = true;
  50052. } catch {}
  50053. if (!isConfigFileExists) {
  50054. return defaultMountPoint;
  50055. }
  50056. const configContent = await fs$1.readFile(configFilePath, {encoding: 'utf8'});
  50057. const configMountPoint = /(?<!#.*)root\s*=\s*(?<mountPoint>.*)/g.exec(configContent);
  50058. if (!configMountPoint) {
  50059. return defaultMountPoint;
  50060. }
  50061. mountPoint = configMountPoint.groups.mountPoint.trim();
  50062. mountPoint = mountPoint.endsWith('/') ? mountPoint : `${mountPoint}/`;
  50063. return mountPoint;
  50064. };
  50065. })();
  50066. const pTryEach = async (array, mapper) => {
  50067. let latestError;
  50068. for (const item of array) {
  50069. try {
  50070. return await mapper(item); // eslint-disable-line no-await-in-loop
  50071. } catch (error) {
  50072. latestError = error;
  50073. }
  50074. }
  50075. throw latestError;
  50076. };
  50077. const baseOpen = async options => {
  50078. options = {
  50079. wait: false,
  50080. background: false,
  50081. newInstance: false,
  50082. allowNonzeroExitCode: false,
  50083. ...options
  50084. };
  50085. if (Array.isArray(options.app)) {
  50086. return pTryEach(options.app, singleApp => baseOpen({
  50087. ...options,
  50088. app: singleApp
  50089. }));
  50090. }
  50091. let {name: app, arguments: appArguments = []} = options.app || {};
  50092. appArguments = [...appArguments];
  50093. if (Array.isArray(app)) {
  50094. return pTryEach(app, appName => baseOpen({
  50095. ...options,
  50096. app: {
  50097. name: appName,
  50098. arguments: appArguments
  50099. }
  50100. }));
  50101. }
  50102. let command;
  50103. const cliArguments = [];
  50104. const childProcessOptions = {};
  50105. if (platform === 'darwin') {
  50106. command = 'open';
  50107. if (options.wait) {
  50108. cliArguments.push('--wait-apps');
  50109. }
  50110. if (options.background) {
  50111. cliArguments.push('--background');
  50112. }
  50113. if (options.newInstance) {
  50114. cliArguments.push('--new');
  50115. }
  50116. if (app) {
  50117. cliArguments.push('-a', app);
  50118. }
  50119. } else if (platform === 'win32' || (isWsl && !isInsideContainer() && !app)) {
  50120. const mountPoint = await getWslDrivesMountPoint();
  50121. command = isWsl ?
  50122. `${mountPoint}c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe` :
  50123. `${process.env.SYSTEMROOT}\\System32\\WindowsPowerShell\\v1.0\\powershell`;
  50124. cliArguments.push(
  50125. '-NoProfile',
  50126. '-NonInteractive',
  50127. '–ExecutionPolicy',
  50128. 'Bypass',
  50129. '-EncodedCommand'
  50130. );
  50131. if (!isWsl) {
  50132. childProcessOptions.windowsVerbatimArguments = true;
  50133. }
  50134. const encodedArguments = ['Start'];
  50135. if (options.wait) {
  50136. encodedArguments.push('-Wait');
  50137. }
  50138. if (app) {
  50139. // Double quote with double quotes to ensure the inner quotes are passed through.
  50140. // Inner quotes are delimited for PowerShell interpretation with backticks.
  50141. encodedArguments.push(`"\`"${app}\`""`, '-ArgumentList');
  50142. if (options.target) {
  50143. appArguments.unshift(options.target);
  50144. }
  50145. } else if (options.target) {
  50146. encodedArguments.push(`"${options.target}"`);
  50147. }
  50148. if (appArguments.length > 0) {
  50149. appArguments = appArguments.map(arg => `"\`"${arg}\`""`);
  50150. encodedArguments.push(appArguments.join(','));
  50151. }
  50152. // Using Base64-encoded command, accepted by PowerShell, to allow special characters.
  50153. options.target = Buffer.from(encodedArguments.join(' '), 'utf16le').toString('base64');
  50154. } else {
  50155. if (app) {
  50156. command = app;
  50157. } else {
  50158. // When bundled by Webpack, there's no actual package file path and no local `xdg-open`.
  50159. const isBundled = !__dirname || __dirname === '/';
  50160. // Check if local `xdg-open` exists and is executable.
  50161. let exeLocalXdgOpen = false;
  50162. try {
  50163. await fs$1.access(localXdgOpenPath, fsConstants.X_OK);
  50164. exeLocalXdgOpen = true;
  50165. } catch {}
  50166. const useSystemXdgOpen = process.versions.electron ||
  50167. platform === 'android' || isBundled || !exeLocalXdgOpen;
  50168. command = useSystemXdgOpen ? 'xdg-open' : localXdgOpenPath;
  50169. }
  50170. if (appArguments.length > 0) {
  50171. cliArguments.push(...appArguments);
  50172. }
  50173. if (!options.wait) {
  50174. // `xdg-open` will block the process unless stdio is ignored
  50175. // and it's detached from the parent even if it's unref'd.
  50176. childProcessOptions.stdio = 'ignore';
  50177. childProcessOptions.detached = true;
  50178. }
  50179. }
  50180. if (options.target) {
  50181. cliArguments.push(options.target);
  50182. }
  50183. if (platform === 'darwin' && appArguments.length > 0) {
  50184. cliArguments.push('--args', ...appArguments);
  50185. }
  50186. const subprocess = childProcess.spawn(command, cliArguments, childProcessOptions);
  50187. if (options.wait) {
  50188. return new Promise((resolve, reject) => {
  50189. subprocess.once('error', reject);
  50190. subprocess.once('close', exitCode => {
  50191. if (!options.allowNonzeroExitCode && exitCode > 0) {
  50192. reject(new Error(`Exited with code ${exitCode}`));
  50193. return;
  50194. }
  50195. resolve(subprocess);
  50196. });
  50197. });
  50198. }
  50199. subprocess.unref();
  50200. return subprocess;
  50201. };
  50202. const open = (target, options) => {
  50203. if (typeof target !== 'string') {
  50204. throw new TypeError('Expected a `target`');
  50205. }
  50206. return baseOpen({
  50207. ...options,
  50208. target
  50209. });
  50210. };
  50211. const openApp = (name, options) => {
  50212. if (typeof name !== 'string') {
  50213. throw new TypeError('Expected a `name`');
  50214. }
  50215. const {arguments: appArguments = []} = options || {};
  50216. if (appArguments !== undefined && appArguments !== null && !Array.isArray(appArguments)) {
  50217. throw new TypeError('Expected `appArguments` as Array type');
  50218. }
  50219. return baseOpen({
  50220. ...options,
  50221. app: {
  50222. name,
  50223. arguments: appArguments
  50224. }
  50225. });
  50226. };
  50227. function detectArchBinary(binary) {
  50228. if (typeof binary === 'string' || Array.isArray(binary)) {
  50229. return binary;
  50230. }
  50231. const {[arch]: archBinary} = binary;
  50232. if (!archBinary) {
  50233. throw new Error(`${arch} is not supported`);
  50234. }
  50235. return archBinary;
  50236. }
  50237. function detectPlatformBinary({[platform]: platformBinary}, {wsl}) {
  50238. if (wsl && isWsl) {
  50239. return detectArchBinary(wsl);
  50240. }
  50241. if (!platformBinary) {
  50242. throw new Error(`${platform} is not supported`);
  50243. }
  50244. return detectArchBinary(platformBinary);
  50245. }
  50246. const apps = {};
  50247. defineLazyProperty(apps, 'chrome', () => detectPlatformBinary({
  50248. darwin: 'google chrome',
  50249. win32: 'chrome',
  50250. linux: ['google-chrome', 'google-chrome-stable', 'chromium']
  50251. }, {
  50252. wsl: {
  50253. ia32: '/mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe',
  50254. x64: ['/mnt/c/Program Files/Google/Chrome/Application/chrome.exe', '/mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe']
  50255. }
  50256. }));
  50257. defineLazyProperty(apps, 'firefox', () => detectPlatformBinary({
  50258. darwin: 'firefox',
  50259. win32: 'C:\\Program Files\\Mozilla Firefox\\firefox.exe',
  50260. linux: 'firefox'
  50261. }, {
  50262. wsl: '/mnt/c/Program Files/Mozilla Firefox/firefox.exe'
  50263. }));
  50264. defineLazyProperty(apps, 'edge', () => detectPlatformBinary({
  50265. darwin: 'microsoft edge',
  50266. win32: 'msedge',
  50267. linux: ['microsoft-edge', 'microsoft-edge-dev']
  50268. }, {
  50269. wsl: '/mnt/c/Program Files (x86)/Microsoft/Edge/Application/msedge.exe'
  50270. }));
  50271. open.apps = apps;
  50272. open.openApp = openApp;
  50273. var open_1 = open;
  50274. var open$1 = /*@__PURE__*/getDefaultExportFromCjs(open_1);
  50275. var crossSpawn = {exports: {}};
  50276. var windows;
  50277. var hasRequiredWindows;
  50278. function requireWindows () {
  50279. if (hasRequiredWindows) return windows;
  50280. hasRequiredWindows = 1;
  50281. windows = isexe;
  50282. isexe.sync = sync;
  50283. var fs = require$$0__default;
  50284. function checkPathExt (path, options) {
  50285. var pathext = options.pathExt !== undefined ?
  50286. options.pathExt : process.env.PATHEXT;
  50287. if (!pathext) {
  50288. return true
  50289. }
  50290. pathext = pathext.split(';');
  50291. if (pathext.indexOf('') !== -1) {
  50292. return true
  50293. }
  50294. for (var i = 0; i < pathext.length; i++) {
  50295. var p = pathext[i].toLowerCase();
  50296. if (p && path.substr(-p.length).toLowerCase() === p) {
  50297. return true
  50298. }
  50299. }
  50300. return false
  50301. }
  50302. function checkStat (stat, path, options) {
  50303. if (!stat.isSymbolicLink() && !stat.isFile()) {
  50304. return false
  50305. }
  50306. return checkPathExt(path, options)
  50307. }
  50308. function isexe (path, options, cb) {
  50309. fs.stat(path, function (er, stat) {
  50310. cb(er, er ? false : checkStat(stat, path, options));
  50311. });
  50312. }
  50313. function sync (path, options) {
  50314. return checkStat(fs.statSync(path), path, options)
  50315. }
  50316. return windows;
  50317. }
  50318. var mode;
  50319. var hasRequiredMode;
  50320. function requireMode () {
  50321. if (hasRequiredMode) return mode;
  50322. hasRequiredMode = 1;
  50323. mode = isexe;
  50324. isexe.sync = sync;
  50325. var fs = require$$0__default;
  50326. function isexe (path, options, cb) {
  50327. fs.stat(path, function (er, stat) {
  50328. cb(er, er ? false : checkStat(stat, options));
  50329. });
  50330. }
  50331. function sync (path, options) {
  50332. return checkStat(fs.statSync(path), options)
  50333. }
  50334. function checkStat (stat, options) {
  50335. return stat.isFile() && checkMode(stat, options)
  50336. }
  50337. function checkMode (stat, options) {
  50338. var mod = stat.mode;
  50339. var uid = stat.uid;
  50340. var gid = stat.gid;
  50341. var myUid = options.uid !== undefined ?
  50342. options.uid : process.getuid && process.getuid();
  50343. var myGid = options.gid !== undefined ?
  50344. options.gid : process.getgid && process.getgid();
  50345. var u = parseInt('100', 8);
  50346. var g = parseInt('010', 8);
  50347. var o = parseInt('001', 8);
  50348. var ug = u | g;
  50349. var ret = (mod & o) ||
  50350. (mod & g) && gid === myGid ||
  50351. (mod & u) && uid === myUid ||
  50352. (mod & ug) && myUid === 0;
  50353. return ret
  50354. }
  50355. return mode;
  50356. }
  50357. var core;
  50358. if (process.platform === 'win32' || commonjsGlobal.TESTING_WINDOWS) {
  50359. core = requireWindows();
  50360. } else {
  50361. core = requireMode();
  50362. }
  50363. var isexe_1 = isexe$1;
  50364. isexe$1.sync = sync;
  50365. function isexe$1 (path, options, cb) {
  50366. if (typeof options === 'function') {
  50367. cb = options;
  50368. options = {};
  50369. }
  50370. if (!cb) {
  50371. if (typeof Promise !== 'function') {
  50372. throw new TypeError('callback not provided')
  50373. }
  50374. return new Promise(function (resolve, reject) {
  50375. isexe$1(path, options || {}, function (er, is) {
  50376. if (er) {
  50377. reject(er);
  50378. } else {
  50379. resolve(is);
  50380. }
  50381. });
  50382. })
  50383. }
  50384. core(path, options || {}, function (er, is) {
  50385. // ignore EACCES because that just means we aren't allowed to run it
  50386. if (er) {
  50387. if (er.code === 'EACCES' || options && options.ignoreErrors) {
  50388. er = null;
  50389. is = false;
  50390. }
  50391. }
  50392. cb(er, is);
  50393. });
  50394. }
  50395. function sync (path, options) {
  50396. // my kingdom for a filtered catch
  50397. try {
  50398. return core.sync(path, options || {})
  50399. } catch (er) {
  50400. if (options && options.ignoreErrors || er.code === 'EACCES') {
  50401. return false
  50402. } else {
  50403. throw er
  50404. }
  50405. }
  50406. }
  50407. const isWindows = process.platform === 'win32' ||
  50408. process.env.OSTYPE === 'cygwin' ||
  50409. process.env.OSTYPE === 'msys';
  50410. const path$2 = require$$0$4;
  50411. const COLON = isWindows ? ';' : ':';
  50412. const isexe = isexe_1;
  50413. const getNotFoundError = (cmd) =>
  50414. Object.assign(new Error(`not found: ${cmd}`), { code: 'ENOENT' });
  50415. const getPathInfo = (cmd, opt) => {
  50416. const colon = opt.colon || COLON;
  50417. // If it has a slash, then we don't bother searching the pathenv.
  50418. // just check the file itself, and that's it.
  50419. const pathEnv = cmd.match(/\//) || isWindows && cmd.match(/\\/) ? ['']
  50420. : (
  50421. [
  50422. // windows always checks the cwd first
  50423. ...(isWindows ? [process.cwd()] : []),
  50424. ...(opt.path || process.env.PATH ||
  50425. /* istanbul ignore next: very unusual */ '').split(colon),
  50426. ]
  50427. );
  50428. const pathExtExe = isWindows
  50429. ? opt.pathExt || process.env.PATHEXT || '.EXE;.CMD;.BAT;.COM'
  50430. : '';
  50431. const pathExt = isWindows ? pathExtExe.split(colon) : [''];
  50432. if (isWindows) {
  50433. if (cmd.indexOf('.') !== -1 && pathExt[0] !== '')
  50434. pathExt.unshift('');
  50435. }
  50436. return {
  50437. pathEnv,
  50438. pathExt,
  50439. pathExtExe,
  50440. }
  50441. };
  50442. const which$1 = (cmd, opt, cb) => {
  50443. if (typeof opt === 'function') {
  50444. cb = opt;
  50445. opt = {};
  50446. }
  50447. if (!opt)
  50448. opt = {};
  50449. const { pathEnv, pathExt, pathExtExe } = getPathInfo(cmd, opt);
  50450. const found = [];
  50451. const step = i => new Promise((resolve, reject) => {
  50452. if (i === pathEnv.length)
  50453. return opt.all && found.length ? resolve(found)
  50454. : reject(getNotFoundError(cmd))
  50455. const ppRaw = pathEnv[i];
  50456. const pathPart = /^".*"$/.test(ppRaw) ? ppRaw.slice(1, -1) : ppRaw;
  50457. const pCmd = path$2.join(pathPart, cmd);
  50458. const p = !pathPart && /^\.[\\\/]/.test(cmd) ? cmd.slice(0, 2) + pCmd
  50459. : pCmd;
  50460. resolve(subStep(p, i, 0));
  50461. });
  50462. const subStep = (p, i, ii) => new Promise((resolve, reject) => {
  50463. if (ii === pathExt.length)
  50464. return resolve(step(i + 1))
  50465. const ext = pathExt[ii];
  50466. isexe(p + ext, { pathExt: pathExtExe }, (er, is) => {
  50467. if (!er && is) {
  50468. if (opt.all)
  50469. found.push(p + ext);
  50470. else
  50471. return resolve(p + ext)
  50472. }
  50473. return resolve(subStep(p, i, ii + 1))
  50474. });
  50475. });
  50476. return cb ? step(0).then(res => cb(null, res), cb) : step(0)
  50477. };
  50478. const whichSync = (cmd, opt) => {
  50479. opt = opt || {};
  50480. const { pathEnv, pathExt, pathExtExe } = getPathInfo(cmd, opt);
  50481. const found = [];
  50482. for (let i = 0; i < pathEnv.length; i ++) {
  50483. const ppRaw = pathEnv[i];
  50484. const pathPart = /^".*"$/.test(ppRaw) ? ppRaw.slice(1, -1) : ppRaw;
  50485. const pCmd = path$2.join(pathPart, cmd);
  50486. const p = !pathPart && /^\.[\\\/]/.test(cmd) ? cmd.slice(0, 2) + pCmd
  50487. : pCmd;
  50488. for (let j = 0; j < pathExt.length; j ++) {
  50489. const cur = p + pathExt[j];
  50490. try {
  50491. const is = isexe.sync(cur, { pathExt: pathExtExe });
  50492. if (is) {
  50493. if (opt.all)
  50494. found.push(cur);
  50495. else
  50496. return cur
  50497. }
  50498. } catch (ex) {}
  50499. }
  50500. }
  50501. if (opt.all && found.length)
  50502. return found
  50503. if (opt.nothrow)
  50504. return null
  50505. throw getNotFoundError(cmd)
  50506. };
  50507. var which_1 = which$1;
  50508. which$1.sync = whichSync;
  50509. var pathKey$1 = {exports: {}};
  50510. const pathKey = (options = {}) => {
  50511. const environment = options.env || process.env;
  50512. const platform = options.platform || process.platform;
  50513. if (platform !== 'win32') {
  50514. return 'PATH';
  50515. }
  50516. return Object.keys(environment).reverse().find(key => key.toUpperCase() === 'PATH') || 'Path';
  50517. };
  50518. pathKey$1.exports = pathKey;
  50519. // TODO: Remove this for the next major release
  50520. pathKey$1.exports.default = pathKey;
  50521. var pathKeyExports = pathKey$1.exports;
  50522. const path$1 = require$$0$4;
  50523. const which = which_1;
  50524. const getPathKey = pathKeyExports;
  50525. function resolveCommandAttempt(parsed, withoutPathExt) {
  50526. const env = parsed.options.env || process.env;
  50527. const cwd = process.cwd();
  50528. const hasCustomCwd = parsed.options.cwd != null;
  50529. // Worker threads do not have process.chdir()
  50530. const shouldSwitchCwd = hasCustomCwd && process.chdir !== undefined && !process.chdir.disabled;
  50531. // If a custom `cwd` was specified, we need to change the process cwd
  50532. // because `which` will do stat calls but does not support a custom cwd
  50533. if (shouldSwitchCwd) {
  50534. try {
  50535. process.chdir(parsed.options.cwd);
  50536. } catch (err) {
  50537. /* Empty */
  50538. }
  50539. }
  50540. let resolved;
  50541. try {
  50542. resolved = which.sync(parsed.command, {
  50543. path: env[getPathKey({ env })],
  50544. pathExt: withoutPathExt ? path$1.delimiter : undefined,
  50545. });
  50546. } catch (e) {
  50547. /* Empty */
  50548. } finally {
  50549. if (shouldSwitchCwd) {
  50550. process.chdir(cwd);
  50551. }
  50552. }
  50553. // If we successfully resolved, ensure that an absolute path is returned
  50554. // Note that when a custom `cwd` was used, we need to resolve to an absolute path based on it
  50555. if (resolved) {
  50556. resolved = path$1.resolve(hasCustomCwd ? parsed.options.cwd : '', resolved);
  50557. }
  50558. return resolved;
  50559. }
  50560. function resolveCommand$1(parsed) {
  50561. return resolveCommandAttempt(parsed) || resolveCommandAttempt(parsed, true);
  50562. }
  50563. var resolveCommand_1 = resolveCommand$1;
  50564. var _escape = {};
  50565. // See http://www.robvanderwoude.com/escapechars.php
  50566. const metaCharsRegExp = /([()\][%!^"`<>&|;, *?])/g;
  50567. function escapeCommand(arg) {
  50568. // Escape meta chars
  50569. arg = arg.replace(metaCharsRegExp, '^$1');
  50570. return arg;
  50571. }
  50572. function escapeArgument(arg, doubleEscapeMetaChars) {
  50573. // Convert to string
  50574. arg = `${arg}`;
  50575. // Algorithm below is based on https://qntm.org/cmd
  50576. // Sequence of backslashes followed by a double quote:
  50577. // double up all the backslashes and escape the double quote
  50578. arg = arg.replace(/(\\*)"/g, '$1$1\\"');
  50579. // Sequence of backslashes followed by the end of the string
  50580. // (which will become a double quote later):
  50581. // double up all the backslashes
  50582. arg = arg.replace(/(\\*)$/, '$1$1');
  50583. // All other backslashes occur literally
  50584. // Quote the whole thing:
  50585. arg = `"${arg}"`;
  50586. // Escape meta chars
  50587. arg = arg.replace(metaCharsRegExp, '^$1');
  50588. // Double escape meta chars if necessary
  50589. if (doubleEscapeMetaChars) {
  50590. arg = arg.replace(metaCharsRegExp, '^$1');
  50591. }
  50592. return arg;
  50593. }
  50594. _escape.command = escapeCommand;
  50595. _escape.argument = escapeArgument;
  50596. var shebangRegex$1 = /^#!(.*)/;
  50597. const shebangRegex = shebangRegex$1;
  50598. var shebangCommand$1 = (string = '') => {
  50599. const match = string.match(shebangRegex);
  50600. if (!match) {
  50601. return null;
  50602. }
  50603. const [path, argument] = match[0].replace(/#! ?/, '').split(' ');
  50604. const binary = path.split('/').pop();
  50605. if (binary === 'env') {
  50606. return argument;
  50607. }
  50608. return argument ? `${binary} ${argument}` : binary;
  50609. };
  50610. const fs = require$$0__default;
  50611. const shebangCommand = shebangCommand$1;
  50612. function readShebang$1(command) {
  50613. // Read the first 150 bytes from the file
  50614. const size = 150;
  50615. const buffer = Buffer.alloc(size);
  50616. let fd;
  50617. try {
  50618. fd = fs.openSync(command, 'r');
  50619. fs.readSync(fd, buffer, 0, size, 0);
  50620. fs.closeSync(fd);
  50621. } catch (e) { /* Empty */ }
  50622. // Attempt to extract shebang (null is returned if not a shebang)
  50623. return shebangCommand(buffer.toString());
  50624. }
  50625. var readShebang_1 = readShebang$1;
  50626. const path = require$$0$4;
  50627. const resolveCommand = resolveCommand_1;
  50628. const escape$1 = _escape;
  50629. const readShebang = readShebang_1;
  50630. const isWin$1 = process.platform === 'win32';
  50631. const isExecutableRegExp = /\.(?:com|exe)$/i;
  50632. const isCmdShimRegExp = /node_modules[\\/].bin[\\/][^\\/]+\.cmd$/i;
  50633. function detectShebang(parsed) {
  50634. parsed.file = resolveCommand(parsed);
  50635. const shebang = parsed.file && readShebang(parsed.file);
  50636. if (shebang) {
  50637. parsed.args.unshift(parsed.file);
  50638. parsed.command = shebang;
  50639. return resolveCommand(parsed);
  50640. }
  50641. return parsed.file;
  50642. }
  50643. function parseNonShell(parsed) {
  50644. if (!isWin$1) {
  50645. return parsed;
  50646. }
  50647. // Detect & add support for shebangs
  50648. const commandFile = detectShebang(parsed);
  50649. // We don't need a shell if the command filename is an executable
  50650. const needsShell = !isExecutableRegExp.test(commandFile);
  50651. // If a shell is required, use cmd.exe and take care of escaping everything correctly
  50652. // Note that `forceShell` is an hidden option used only in tests
  50653. if (parsed.options.forceShell || needsShell) {
  50654. // Need to double escape meta chars if the command is a cmd-shim located in `node_modules/.bin/`
  50655. // The cmd-shim simply calls execute the package bin file with NodeJS, proxying any argument
  50656. // Because the escape of metachars with ^ gets interpreted when the cmd.exe is first called,
  50657. // we need to double escape them
  50658. const needsDoubleEscapeMetaChars = isCmdShimRegExp.test(commandFile);
  50659. // Normalize posix paths into OS compatible paths (e.g.: foo/bar -> foo\bar)
  50660. // This is necessary otherwise it will always fail with ENOENT in those cases
  50661. parsed.command = path.normalize(parsed.command);
  50662. // Escape command & arguments
  50663. parsed.command = escape$1.command(parsed.command);
  50664. parsed.args = parsed.args.map((arg) => escape$1.argument(arg, needsDoubleEscapeMetaChars));
  50665. const shellCommand = [parsed.command].concat(parsed.args).join(' ');
  50666. parsed.args = ['/d', '/s', '/c', `"${shellCommand}"`];
  50667. parsed.command = process.env.comspec || 'cmd.exe';
  50668. parsed.options.windowsVerbatimArguments = true; // Tell node's spawn that the arguments are already escaped
  50669. }
  50670. return parsed;
  50671. }
  50672. function parse$4(command, args, options) {
  50673. // Normalize arguments, similar to nodejs
  50674. if (args && !Array.isArray(args)) {
  50675. options = args;
  50676. args = null;
  50677. }
  50678. args = args ? args.slice(0) : []; // Clone array to avoid changing the original
  50679. options = Object.assign({}, options); // Clone object to avoid changing the original
  50680. // Build our parsed object
  50681. const parsed = {
  50682. command,
  50683. args,
  50684. options,
  50685. file: undefined,
  50686. original: {
  50687. command,
  50688. args,
  50689. },
  50690. };
  50691. // Delegate further parsing to shell or non-shell
  50692. return options.shell ? parsed : parseNonShell(parsed);
  50693. }
  50694. var parse_1 = parse$4;
  50695. const isWin = process.platform === 'win32';
  50696. function notFoundError(original, syscall) {
  50697. return Object.assign(new Error(`${syscall} ${original.command} ENOENT`), {
  50698. code: 'ENOENT',
  50699. errno: 'ENOENT',
  50700. syscall: `${syscall} ${original.command}`,
  50701. path: original.command,
  50702. spawnargs: original.args,
  50703. });
  50704. }
  50705. function hookChildProcess(cp, parsed) {
  50706. if (!isWin) {
  50707. return;
  50708. }
  50709. const originalEmit = cp.emit;
  50710. cp.emit = function (name, arg1) {
  50711. // If emitting "exit" event and exit code is 1, we need to check if
  50712. // the command exists and emit an "error" instead
  50713. // See https://github.com/IndigoUnited/node-cross-spawn/issues/16
  50714. if (name === 'exit') {
  50715. const err = verifyENOENT(arg1, parsed);
  50716. if (err) {
  50717. return originalEmit.call(cp, 'error', err);
  50718. }
  50719. }
  50720. return originalEmit.apply(cp, arguments); // eslint-disable-line prefer-rest-params
  50721. };
  50722. }
  50723. function verifyENOENT(status, parsed) {
  50724. if (isWin && status === 1 && !parsed.file) {
  50725. return notFoundError(parsed.original, 'spawn');
  50726. }
  50727. return null;
  50728. }
  50729. function verifyENOENTSync(status, parsed) {
  50730. if (isWin && status === 1 && !parsed.file) {
  50731. return notFoundError(parsed.original, 'spawnSync');
  50732. }
  50733. return null;
  50734. }
  50735. var enoent$1 = {
  50736. hookChildProcess,
  50737. verifyENOENT,
  50738. verifyENOENTSync,
  50739. notFoundError,
  50740. };
  50741. const cp = require$$2$1;
  50742. const parse$3 = parse_1;
  50743. const enoent = enoent$1;
  50744. function spawn(command, args, options) {
  50745. // Parse the arguments
  50746. const parsed = parse$3(command, args, options);
  50747. // Spawn the child process
  50748. const spawned = cp.spawn(parsed.command, parsed.args, parsed.options);
  50749. // Hook into child process "exit" event to emit an error if the command
  50750. // does not exists, see: https://github.com/IndigoUnited/node-cross-spawn/issues/16
  50751. enoent.hookChildProcess(spawned, parsed);
  50752. return spawned;
  50753. }
  50754. function spawnSync(command, args, options) {
  50755. // Parse the arguments
  50756. const parsed = parse$3(command, args, options);
  50757. // Spawn the child process
  50758. const result = cp.spawnSync(parsed.command, parsed.args, parsed.options);
  50759. // Analyze if the command does not exist, see: https://github.com/IndigoUnited/node-cross-spawn/issues/16
  50760. result.error = result.error || enoent.verifyENOENTSync(result.status, parsed);
  50761. return result;
  50762. }
  50763. crossSpawn.exports = spawn;
  50764. crossSpawn.exports.spawn = spawn;
  50765. crossSpawn.exports.sync = spawnSync;
  50766. crossSpawn.exports._parse = parse$3;
  50767. crossSpawn.exports._enoent = enoent;
  50768. var crossSpawnExports = crossSpawn.exports;
  50769. var spawn$1 = /*@__PURE__*/getDefaultExportFromCjs(crossSpawnExports);
  50770. function openBrowser(url, opt, logger) {
  50771. const browser = process.env.BROWSER || "";
  50772. if (browser.toLowerCase().endsWith(".js")) {
  50773. executeNodeScript(browser, url, logger);
  50774. } else if (browser.toLowerCase() !== "none") {
  50775. const browserArgs = process.env.BROWSER_ARGS ? process.env.BROWSER_ARGS.split(" ") : [];
  50776. startBrowserProcess(browser, browserArgs, url, logger);
  50777. }
  50778. }
  50779. function executeNodeScript(scriptPath, url, logger) {
  50780. const extraArgs = process.argv.slice(2);
  50781. const child = spawn$1(process.execPath, [scriptPath, ...extraArgs, url], {
  50782. stdio: "inherit"
  50783. });
  50784. child.on("close", (code) => {
  50785. if (code !== 0) {
  50786. logger.error(
  50787. colors$1.red(
  50788. `
  50789. The script specified as BROWSER environment variable failed.
  50790. ${colors$1.cyan(
  50791. scriptPath
  50792. )} exited with code ${code}.`
  50793. ),
  50794. { error: null }
  50795. );
  50796. }
  50797. });
  50798. }
  50799. const supportedChromiumBrowsers = [
  50800. "Google Chrome Canary",
  50801. "Google Chrome Dev",
  50802. "Google Chrome Beta",
  50803. "Google Chrome",
  50804. "Microsoft Edge",
  50805. "Brave Browser",
  50806. "Vivaldi",
  50807. "Chromium"
  50808. ];
  50809. async function startBrowserProcess(browser, browserArgs, url, logger) {
  50810. const preferredOSXBrowser = browser === "google chrome" ? "Google Chrome" : browser;
  50811. const shouldTryOpenChromeWithAppleScript = process.platform === "darwin" && (!preferredOSXBrowser || supportedChromiumBrowsers.includes(preferredOSXBrowser));
  50812. if (shouldTryOpenChromeWithAppleScript) {
  50813. try {
  50814. const ps = await execAsync("ps cax");
  50815. const openedBrowser = preferredOSXBrowser && ps.includes(preferredOSXBrowser) ? preferredOSXBrowser : supportedChromiumBrowsers.find((b) => ps.includes(b));
  50816. if (openedBrowser) {
  50817. await execAsync(
  50818. `osascript openChrome.applescript "${encodeURI(
  50819. url
  50820. )}" "${openedBrowser}"`,
  50821. {
  50822. cwd: join$2(VITE_PACKAGE_DIR, "bin")
  50823. }
  50824. );
  50825. return true;
  50826. }
  50827. } catch (err) {
  50828. }
  50829. }
  50830. if (process.platform === "darwin" && browser === "open") {
  50831. browser = void 0;
  50832. }
  50833. try {
  50834. const options = browser ? { app: { name: browser, arguments: browserArgs } } : {};
  50835. new Promise((_, reject) => {
  50836. open$1(url, options).then((subprocess) => {
  50837. subprocess.on("error", reject);
  50838. }).catch(reject);
  50839. }).catch((err) => {
  50840. logger.error(err.stack || err.message);
  50841. });
  50842. return true;
  50843. } catch (err) {
  50844. return false;
  50845. }
  50846. }
  50847. function execAsync(command, options) {
  50848. return new Promise((resolve, reject) => {
  50849. exec(command, options, (error, stdout) => {
  50850. if (error) {
  50851. reject(error);
  50852. } else {
  50853. resolve(stdout.toString());
  50854. }
  50855. });
  50856. });
  50857. }
  50858. function bindCLIShortcuts(server, opts) {
  50859. if (!server.httpServer || !process.stdin.isTTY || process.env.CI) {
  50860. return;
  50861. }
  50862. const isDev = isDevServer(server);
  50863. if (isDev) {
  50864. server._shortcutsOptions = opts;
  50865. }
  50866. if (opts?.print) {
  50867. server.config.logger.info(
  50868. colors$1.dim(colors$1.green(" \u279C")) + colors$1.dim(" press ") + colors$1.bold("h + enter") + colors$1.dim(" to show help")
  50869. );
  50870. }
  50871. const shortcuts = (opts?.customShortcuts ?? []).concat(
  50872. isDev ? BASE_DEV_SHORTCUTS : BASE_PREVIEW_SHORTCUTS
  50873. );
  50874. let actionRunning = false;
  50875. const onInput = async (input) => {
  50876. if (actionRunning) return;
  50877. if (input === "h") {
  50878. const loggedKeys = /* @__PURE__ */ new Set();
  50879. server.config.logger.info("\n Shortcuts");
  50880. for (const shortcut2 of shortcuts) {
  50881. if (loggedKeys.has(shortcut2.key)) continue;
  50882. loggedKeys.add(shortcut2.key);
  50883. if (shortcut2.action == null) continue;
  50884. server.config.logger.info(
  50885. colors$1.dim(" press ") + colors$1.bold(`${shortcut2.key} + enter`) + colors$1.dim(` to ${shortcut2.description}`)
  50886. );
  50887. }
  50888. return;
  50889. }
  50890. const shortcut = shortcuts.find((shortcut2) => shortcut2.key === input);
  50891. if (!shortcut || shortcut.action == null) return;
  50892. actionRunning = true;
  50893. await shortcut.action(server);
  50894. actionRunning = false;
  50895. };
  50896. const rl = readline.createInterface({ input: process.stdin });
  50897. rl.on("line", onInput);
  50898. server.httpServer.on("close", () => rl.close());
  50899. }
  50900. const BASE_DEV_SHORTCUTS = [
  50901. {
  50902. key: "r",
  50903. description: "restart the server",
  50904. async action(server) {
  50905. await restartServerWithUrls(server);
  50906. }
  50907. },
  50908. {
  50909. key: "u",
  50910. description: "show server url",
  50911. action(server) {
  50912. server.config.logger.info("");
  50913. server.printUrls();
  50914. }
  50915. },
  50916. {
  50917. key: "o",
  50918. description: "open in browser",
  50919. action(server) {
  50920. server.openBrowser();
  50921. }
  50922. },
  50923. {
  50924. key: "c",
  50925. description: "clear console",
  50926. action(server) {
  50927. server.config.logger.clearScreen("error");
  50928. }
  50929. },
  50930. {
  50931. key: "q",
  50932. description: "quit",
  50933. async action(server) {
  50934. try {
  50935. await server.close();
  50936. } finally {
  50937. process.exit();
  50938. }
  50939. }
  50940. }
  50941. ];
  50942. const BASE_PREVIEW_SHORTCUTS = [
  50943. {
  50944. key: "o",
  50945. description: "open in browser",
  50946. action(server) {
  50947. const url = server.resolvedUrls?.local[0] ?? server.resolvedUrls?.network[0];
  50948. if (url) {
  50949. openBrowser(url, true, server.config.logger);
  50950. } else {
  50951. server.config.logger.warn("No URL available to open in browser");
  50952. }
  50953. }
  50954. },
  50955. {
  50956. key: "q",
  50957. description: "quit",
  50958. async action(server) {
  50959. try {
  50960. await server.close();
  50961. } finally {
  50962. process.exit();
  50963. }
  50964. }
  50965. }
  50966. ];
  50967. function getResolvedOutDirs(root, outDir, outputOptions) {
  50968. const resolvedOutDir = path$n.resolve(root, outDir);
  50969. if (!outputOptions) return /* @__PURE__ */ new Set([resolvedOutDir]);
  50970. return new Set(
  50971. arraify(outputOptions).map(
  50972. ({ dir }) => dir ? path$n.resolve(root, dir) : resolvedOutDir
  50973. )
  50974. );
  50975. }
  50976. function resolveEmptyOutDir(emptyOutDir, root, outDirs, logger) {
  50977. if (emptyOutDir != null) return emptyOutDir;
  50978. for (const outDir of outDirs) {
  50979. if (!normalizePath$3(outDir).startsWith(withTrailingSlash(root))) {
  50980. logger?.warn(
  50981. colors$1.yellow(
  50982. `
  50983. ${colors$1.bold(`(!)`)} outDir ${colors$1.white(
  50984. colors$1.dim(outDir)
  50985. )} is not inside project root and will not be emptied.
  50986. Use --emptyOutDir to override.
  50987. `
  50988. )
  50989. );
  50990. return false;
  50991. }
  50992. }
  50993. return true;
  50994. }
  50995. function resolveChokidarOptions(options, resolvedOutDirs, emptyOutDir, cacheDir) {
  50996. const { ignored: ignoredList, ...otherOptions } = options ?? {};
  50997. const ignored = [
  50998. "**/.git/**",
  50999. "**/node_modules/**",
  51000. "**/test-results/**",
  51001. // Playwright
  51002. glob.escapePath(cacheDir) + "/**",
  51003. ...arraify(ignoredList || [])
  51004. ];
  51005. if (emptyOutDir) {
  51006. ignored.push(
  51007. ...[...resolvedOutDirs].map((outDir) => glob.escapePath(outDir) + "/**")
  51008. );
  51009. }
  51010. const resolvedWatchOptions = {
  51011. ignored,
  51012. ignoreInitial: true,
  51013. ignorePermissionErrors: true,
  51014. ...otherOptions
  51015. };
  51016. return resolvedWatchOptions;
  51017. }
  51018. class NoopWatcher extends EventEmitter$4 {
  51019. constructor(options) {
  51020. super();
  51021. this.options = options;
  51022. }
  51023. add() {
  51024. return this;
  51025. }
  51026. unwatch() {
  51027. return this;
  51028. }
  51029. getWatched() {
  51030. return {};
  51031. }
  51032. ref() {
  51033. return this;
  51034. }
  51035. unref() {
  51036. return this;
  51037. }
  51038. async close() {
  51039. }
  51040. }
  51041. function createNoopWatcher(options) {
  51042. return new NoopWatcher(options);
  51043. }
  51044. async function fetchModule(server, url, importer, options = {}) {
  51045. if (url.startsWith("data:") || isBuiltin(url)) {
  51046. return { externalize: url, type: "builtin" };
  51047. }
  51048. if (isExternalUrl(url)) {
  51049. return { externalize: url, type: "network" };
  51050. }
  51051. if (url[0] !== "." && url[0] !== "/") {
  51052. const {
  51053. isProduction,
  51054. resolve: { dedupe, preserveSymlinks },
  51055. root,
  51056. ssr
  51057. } = server.config;
  51058. const overrideConditions = ssr.resolve?.externalConditions || [];
  51059. const resolveOptions = {
  51060. mainFields: ["main"],
  51061. conditions: [],
  51062. overrideConditions: [...overrideConditions, "production", "development"],
  51063. extensions: [".js", ".cjs", ".json"],
  51064. dedupe,
  51065. preserveSymlinks,
  51066. isBuild: false,
  51067. isProduction,
  51068. root,
  51069. ssrConfig: ssr,
  51070. packageCache: server.config.packageCache
  51071. };
  51072. const resolved = tryNodeResolve(
  51073. url,
  51074. importer,
  51075. { ...resolveOptions, tryEsmOnly: true },
  51076. false,
  51077. void 0,
  51078. true
  51079. );
  51080. if (!resolved) {
  51081. const err = new Error(
  51082. `Cannot find module '${url}' imported from '${importer}'`
  51083. );
  51084. err.code = "ERR_MODULE_NOT_FOUND";
  51085. throw err;
  51086. }
  51087. const file = pathToFileURL(resolved.id).toString();
  51088. const type = isFilePathESM(resolved.id, server.config.packageCache) ? "module" : "commonjs";
  51089. return { externalize: file, type };
  51090. }
  51091. url = unwrapId$1(url);
  51092. let result = await server.transformRequest(url, { ssr: true });
  51093. if (!result) {
  51094. throw new Error(
  51095. `[vite] transform failed for module '${url}'${importer ? ` imported from '${importer}'` : ""}.`
  51096. );
  51097. }
  51098. const mod = await server.moduleGraph.getModuleByUrl(url, true);
  51099. if (!mod) {
  51100. throw new Error(
  51101. `[vite] cannot find module '${url}' ${importer ? ` imported from '${importer}'` : ""}.`
  51102. );
  51103. }
  51104. if (options.inlineSourceMap !== false) {
  51105. result = inlineSourceMap(mod, result, options.processSourceMap);
  51106. }
  51107. if (result.code[0] === "#")
  51108. result.code = result.code.replace(/^#!.*/, (s) => " ".repeat(s.length));
  51109. return { code: result.code, file: mod.file };
  51110. }
  51111. const OTHER_SOURCE_MAP_REGEXP = new RegExp(
  51112. `//# ${SOURCEMAPPING_URL}=data:application/json[^,]+base64,([A-Za-z0-9+/=]+)$`,
  51113. "gm"
  51114. );
  51115. function inlineSourceMap(mod, result, processSourceMap) {
  51116. const map = result.map;
  51117. let code = result.code;
  51118. if (!map || !("version" in map) || code.includes(VITE_RUNTIME_SOURCEMAPPING_SOURCE))
  51119. return result;
  51120. OTHER_SOURCE_MAP_REGEXP.lastIndex = 0;
  51121. if (OTHER_SOURCE_MAP_REGEXP.test(code))
  51122. code = code.replace(OTHER_SOURCE_MAP_REGEXP, "");
  51123. const sourceMap = processSourceMap?.(map) || map;
  51124. result.code = `${code.trimEnd()}
  51125. //# sourceURL=${mod.id}
  51126. ${VITE_RUNTIME_SOURCEMAPPING_SOURCE}
  51127. //# ${SOURCEMAPPING_URL}=${genSourceMapUrl(sourceMap)}
  51128. `;
  51129. return result;
  51130. }
  51131. function ssrFetchModule(server, id, importer) {
  51132. return fetchModule(server, id, importer, {
  51133. processSourceMap(map) {
  51134. return Object.assign({}, map, {
  51135. mappings: ";".repeat(asyncFunctionDeclarationPaddingLineCount) + map.mappings
  51136. });
  51137. }
  51138. });
  51139. }
  51140. var bufferUtil$1 = {exports: {}};
  51141. const BINARY_TYPES$2 = ['nodebuffer', 'arraybuffer', 'fragments'];
  51142. const hasBlob$1 = typeof Blob !== 'undefined';
  51143. if (hasBlob$1) BINARY_TYPES$2.push('blob');
  51144. var constants = {
  51145. BINARY_TYPES: BINARY_TYPES$2,
  51146. EMPTY_BUFFER: Buffer.alloc(0),
  51147. GUID: '258EAFA5-E914-47DA-95CA-C5AB0DC85B11',
  51148. hasBlob: hasBlob$1,
  51149. kForOnEventAttribute: Symbol('kIsForOnEventAttribute'),
  51150. kListener: Symbol('kListener'),
  51151. kStatusCode: Symbol('status-code'),
  51152. kWebSocket: Symbol('websocket'),
  51153. NOOP: () => {}
  51154. };
  51155. const { EMPTY_BUFFER: EMPTY_BUFFER$3 } = constants;
  51156. const FastBuffer$2 = Buffer[Symbol.species];
  51157. /**
  51158. * Merges an array of buffers into a new buffer.
  51159. *
  51160. * @param {Buffer[]} list The array of buffers to concat
  51161. * @param {Number} totalLength The total length of buffers in the list
  51162. * @return {Buffer} The resulting buffer
  51163. * @public
  51164. */
  51165. function concat$1(list, totalLength) {
  51166. if (list.length === 0) return EMPTY_BUFFER$3;
  51167. if (list.length === 1) return list[0];
  51168. const target = Buffer.allocUnsafe(totalLength);
  51169. let offset = 0;
  51170. for (let i = 0; i < list.length; i++) {
  51171. const buf = list[i];
  51172. target.set(buf, offset);
  51173. offset += buf.length;
  51174. }
  51175. if (offset < totalLength) {
  51176. return new FastBuffer$2(target.buffer, target.byteOffset, offset);
  51177. }
  51178. return target;
  51179. }
  51180. /**
  51181. * Masks a buffer using the given mask.
  51182. *
  51183. * @param {Buffer} source The buffer to mask
  51184. * @param {Buffer} mask The mask to use
  51185. * @param {Buffer} output The buffer where to store the result
  51186. * @param {Number} offset The offset at which to start writing
  51187. * @param {Number} length The number of bytes to mask.
  51188. * @public
  51189. */
  51190. function _mask(source, mask, output, offset, length) {
  51191. for (let i = 0; i < length; i++) {
  51192. output[offset + i] = source[i] ^ mask[i & 3];
  51193. }
  51194. }
  51195. /**
  51196. * Unmasks a buffer using the given mask.
  51197. *
  51198. * @param {Buffer} buffer The buffer to unmask
  51199. * @param {Buffer} mask The mask to use
  51200. * @public
  51201. */
  51202. function _unmask(buffer, mask) {
  51203. for (let i = 0; i < buffer.length; i++) {
  51204. buffer[i] ^= mask[i & 3];
  51205. }
  51206. }
  51207. /**
  51208. * Converts a buffer to an `ArrayBuffer`.
  51209. *
  51210. * @param {Buffer} buf The buffer to convert
  51211. * @return {ArrayBuffer} Converted buffer
  51212. * @public
  51213. */
  51214. function toArrayBuffer$1(buf) {
  51215. if (buf.length === buf.buffer.byteLength) {
  51216. return buf.buffer;
  51217. }
  51218. return buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.length);
  51219. }
  51220. /**
  51221. * Converts `data` to a `Buffer`.
  51222. *
  51223. * @param {*} data The data to convert
  51224. * @return {Buffer} The buffer
  51225. * @throws {TypeError}
  51226. * @public
  51227. */
  51228. function toBuffer$2(data) {
  51229. toBuffer$2.readOnly = true;
  51230. if (Buffer.isBuffer(data)) return data;
  51231. let buf;
  51232. if (data instanceof ArrayBuffer) {
  51233. buf = new FastBuffer$2(data);
  51234. } else if (ArrayBuffer.isView(data)) {
  51235. buf = new FastBuffer$2(data.buffer, data.byteOffset, data.byteLength);
  51236. } else {
  51237. buf = Buffer.from(data);
  51238. toBuffer$2.readOnly = false;
  51239. }
  51240. return buf;
  51241. }
  51242. bufferUtil$1.exports = {
  51243. concat: concat$1,
  51244. mask: _mask,
  51245. toArrayBuffer: toArrayBuffer$1,
  51246. toBuffer: toBuffer$2,
  51247. unmask: _unmask
  51248. };
  51249. /* istanbul ignore else */
  51250. if (!process.env.WS_NO_BUFFER_UTIL) {
  51251. try {
  51252. const bufferUtil = require('bufferutil');
  51253. bufferUtil$1.exports.mask = function (source, mask, output, offset, length) {
  51254. if (length < 48) _mask(source, mask, output, offset, length);
  51255. else bufferUtil.mask(source, mask, output, offset, length);
  51256. };
  51257. bufferUtil$1.exports.unmask = function (buffer, mask) {
  51258. if (buffer.length < 32) _unmask(buffer, mask);
  51259. else bufferUtil.unmask(buffer, mask);
  51260. };
  51261. } catch (e) {
  51262. // Continue regardless of the error.
  51263. }
  51264. }
  51265. var bufferUtilExports = bufferUtil$1.exports;
  51266. const kDone = Symbol('kDone');
  51267. const kRun = Symbol('kRun');
  51268. /**
  51269. * A very simple job queue with adjustable concurrency. Adapted from
  51270. * https://github.com/STRML/async-limiter
  51271. */
  51272. let Limiter$1 = class Limiter {
  51273. /**
  51274. * Creates a new `Limiter`.
  51275. *
  51276. * @param {Number} [concurrency=Infinity] The maximum number of jobs allowed
  51277. * to run concurrently
  51278. */
  51279. constructor(concurrency) {
  51280. this[kDone] = () => {
  51281. this.pending--;
  51282. this[kRun]();
  51283. };
  51284. this.concurrency = concurrency || Infinity;
  51285. this.jobs = [];
  51286. this.pending = 0;
  51287. }
  51288. /**
  51289. * Adds a job to the queue.
  51290. *
  51291. * @param {Function} job The job to run
  51292. * @public
  51293. */
  51294. add(job) {
  51295. this.jobs.push(job);
  51296. this[kRun]();
  51297. }
  51298. /**
  51299. * Removes a job from the queue and runs it if possible.
  51300. *
  51301. * @private
  51302. */
  51303. [kRun]() {
  51304. if (this.pending === this.concurrency) return;
  51305. if (this.jobs.length) {
  51306. const job = this.jobs.shift();
  51307. this.pending++;
  51308. job(this[kDone]);
  51309. }
  51310. }
  51311. };
  51312. var limiter = Limiter$1;
  51313. const zlib = zlib$1;
  51314. const bufferUtil = bufferUtilExports;
  51315. const Limiter = limiter;
  51316. const { kStatusCode: kStatusCode$2 } = constants;
  51317. const FastBuffer$1 = Buffer[Symbol.species];
  51318. const TRAILER = Buffer.from([0x00, 0x00, 0xff, 0xff]);
  51319. const kPerMessageDeflate = Symbol('permessage-deflate');
  51320. const kTotalLength = Symbol('total-length');
  51321. const kCallback = Symbol('callback');
  51322. const kBuffers = Symbol('buffers');
  51323. const kError$1 = Symbol('error');
  51324. //
  51325. // We limit zlib concurrency, which prevents severe memory fragmentation
  51326. // as documented in https://github.com/nodejs/node/issues/8871#issuecomment-250915913
  51327. // and https://github.com/websockets/ws/issues/1202
  51328. //
  51329. // Intentionally global; it's the global thread pool that's an issue.
  51330. //
  51331. let zlibLimiter;
  51332. /**
  51333. * permessage-deflate implementation.
  51334. */
  51335. let PerMessageDeflate$4 = class PerMessageDeflate {
  51336. /**
  51337. * Creates a PerMessageDeflate instance.
  51338. *
  51339. * @param {Object} [options] Configuration options
  51340. * @param {(Boolean|Number)} [options.clientMaxWindowBits] Advertise support
  51341. * for, or request, a custom client window size
  51342. * @param {Boolean} [options.clientNoContextTakeover=false] Advertise/
  51343. * acknowledge disabling of client context takeover
  51344. * @param {Number} [options.concurrencyLimit=10] The number of concurrent
  51345. * calls to zlib
  51346. * @param {(Boolean|Number)} [options.serverMaxWindowBits] Request/confirm the
  51347. * use of a custom server window size
  51348. * @param {Boolean} [options.serverNoContextTakeover=false] Request/accept
  51349. * disabling of server context takeover
  51350. * @param {Number} [options.threshold=1024] Size (in bytes) below which
  51351. * messages should not be compressed if context takeover is disabled
  51352. * @param {Object} [options.zlibDeflateOptions] Options to pass to zlib on
  51353. * deflate
  51354. * @param {Object} [options.zlibInflateOptions] Options to pass to zlib on
  51355. * inflate
  51356. * @param {Boolean} [isServer=false] Create the instance in either server or
  51357. * client mode
  51358. * @param {Number} [maxPayload=0] The maximum allowed message length
  51359. */
  51360. constructor(options, isServer, maxPayload) {
  51361. this._maxPayload = maxPayload | 0;
  51362. this._options = options || {};
  51363. this._threshold =
  51364. this._options.threshold !== undefined ? this._options.threshold : 1024;
  51365. this._isServer = !!isServer;
  51366. this._deflate = null;
  51367. this._inflate = null;
  51368. this.params = null;
  51369. if (!zlibLimiter) {
  51370. const concurrency =
  51371. this._options.concurrencyLimit !== undefined
  51372. ? this._options.concurrencyLimit
  51373. : 10;
  51374. zlibLimiter = new Limiter(concurrency);
  51375. }
  51376. }
  51377. /**
  51378. * @type {String}
  51379. */
  51380. static get extensionName() {
  51381. return 'permessage-deflate';
  51382. }
  51383. /**
  51384. * Create an extension negotiation offer.
  51385. *
  51386. * @return {Object} Extension parameters
  51387. * @public
  51388. */
  51389. offer() {
  51390. const params = {};
  51391. if (this._options.serverNoContextTakeover) {
  51392. params.server_no_context_takeover = true;
  51393. }
  51394. if (this._options.clientNoContextTakeover) {
  51395. params.client_no_context_takeover = true;
  51396. }
  51397. if (this._options.serverMaxWindowBits) {
  51398. params.server_max_window_bits = this._options.serverMaxWindowBits;
  51399. }
  51400. if (this._options.clientMaxWindowBits) {
  51401. params.client_max_window_bits = this._options.clientMaxWindowBits;
  51402. } else if (this._options.clientMaxWindowBits == null) {
  51403. params.client_max_window_bits = true;
  51404. }
  51405. return params;
  51406. }
  51407. /**
  51408. * Accept an extension negotiation offer/response.
  51409. *
  51410. * @param {Array} configurations The extension negotiation offers/reponse
  51411. * @return {Object} Accepted configuration
  51412. * @public
  51413. */
  51414. accept(configurations) {
  51415. configurations = this.normalizeParams(configurations);
  51416. this.params = this._isServer
  51417. ? this.acceptAsServer(configurations)
  51418. : this.acceptAsClient(configurations);
  51419. return this.params;
  51420. }
  51421. /**
  51422. * Releases all resources used by the extension.
  51423. *
  51424. * @public
  51425. */
  51426. cleanup() {
  51427. if (this._inflate) {
  51428. this._inflate.close();
  51429. this._inflate = null;
  51430. }
  51431. if (this._deflate) {
  51432. const callback = this._deflate[kCallback];
  51433. this._deflate.close();
  51434. this._deflate = null;
  51435. if (callback) {
  51436. callback(
  51437. new Error(
  51438. 'The deflate stream was closed while data was being processed'
  51439. )
  51440. );
  51441. }
  51442. }
  51443. }
  51444. /**
  51445. * Accept an extension negotiation offer.
  51446. *
  51447. * @param {Array} offers The extension negotiation offers
  51448. * @return {Object} Accepted configuration
  51449. * @private
  51450. */
  51451. acceptAsServer(offers) {
  51452. const opts = this._options;
  51453. const accepted = offers.find((params) => {
  51454. if (
  51455. (opts.serverNoContextTakeover === false &&
  51456. params.server_no_context_takeover) ||
  51457. (params.server_max_window_bits &&
  51458. (opts.serverMaxWindowBits === false ||
  51459. (typeof opts.serverMaxWindowBits === 'number' &&
  51460. opts.serverMaxWindowBits > params.server_max_window_bits))) ||
  51461. (typeof opts.clientMaxWindowBits === 'number' &&
  51462. !params.client_max_window_bits)
  51463. ) {
  51464. return false;
  51465. }
  51466. return true;
  51467. });
  51468. if (!accepted) {
  51469. throw new Error('None of the extension offers can be accepted');
  51470. }
  51471. if (opts.serverNoContextTakeover) {
  51472. accepted.server_no_context_takeover = true;
  51473. }
  51474. if (opts.clientNoContextTakeover) {
  51475. accepted.client_no_context_takeover = true;
  51476. }
  51477. if (typeof opts.serverMaxWindowBits === 'number') {
  51478. accepted.server_max_window_bits = opts.serverMaxWindowBits;
  51479. }
  51480. if (typeof opts.clientMaxWindowBits === 'number') {
  51481. accepted.client_max_window_bits = opts.clientMaxWindowBits;
  51482. } else if (
  51483. accepted.client_max_window_bits === true ||
  51484. opts.clientMaxWindowBits === false
  51485. ) {
  51486. delete accepted.client_max_window_bits;
  51487. }
  51488. return accepted;
  51489. }
  51490. /**
  51491. * Accept the extension negotiation response.
  51492. *
  51493. * @param {Array} response The extension negotiation response
  51494. * @return {Object} Accepted configuration
  51495. * @private
  51496. */
  51497. acceptAsClient(response) {
  51498. const params = response[0];
  51499. if (
  51500. this._options.clientNoContextTakeover === false &&
  51501. params.client_no_context_takeover
  51502. ) {
  51503. throw new Error('Unexpected parameter "client_no_context_takeover"');
  51504. }
  51505. if (!params.client_max_window_bits) {
  51506. if (typeof this._options.clientMaxWindowBits === 'number') {
  51507. params.client_max_window_bits = this._options.clientMaxWindowBits;
  51508. }
  51509. } else if (
  51510. this._options.clientMaxWindowBits === false ||
  51511. (typeof this._options.clientMaxWindowBits === 'number' &&
  51512. params.client_max_window_bits > this._options.clientMaxWindowBits)
  51513. ) {
  51514. throw new Error(
  51515. 'Unexpected or invalid parameter "client_max_window_bits"'
  51516. );
  51517. }
  51518. return params;
  51519. }
  51520. /**
  51521. * Normalize parameters.
  51522. *
  51523. * @param {Array} configurations The extension negotiation offers/reponse
  51524. * @return {Array} The offers/response with normalized parameters
  51525. * @private
  51526. */
  51527. normalizeParams(configurations) {
  51528. configurations.forEach((params) => {
  51529. Object.keys(params).forEach((key) => {
  51530. let value = params[key];
  51531. if (value.length > 1) {
  51532. throw new Error(`Parameter "${key}" must have only a single value`);
  51533. }
  51534. value = value[0];
  51535. if (key === 'client_max_window_bits') {
  51536. if (value !== true) {
  51537. const num = +value;
  51538. if (!Number.isInteger(num) || num < 8 || num > 15) {
  51539. throw new TypeError(
  51540. `Invalid value for parameter "${key}": ${value}`
  51541. );
  51542. }
  51543. value = num;
  51544. } else if (!this._isServer) {
  51545. throw new TypeError(
  51546. `Invalid value for parameter "${key}": ${value}`
  51547. );
  51548. }
  51549. } else if (key === 'server_max_window_bits') {
  51550. const num = +value;
  51551. if (!Number.isInteger(num) || num < 8 || num > 15) {
  51552. throw new TypeError(
  51553. `Invalid value for parameter "${key}": ${value}`
  51554. );
  51555. }
  51556. value = num;
  51557. } else if (
  51558. key === 'client_no_context_takeover' ||
  51559. key === 'server_no_context_takeover'
  51560. ) {
  51561. if (value !== true) {
  51562. throw new TypeError(
  51563. `Invalid value for parameter "${key}": ${value}`
  51564. );
  51565. }
  51566. } else {
  51567. throw new Error(`Unknown parameter "${key}"`);
  51568. }
  51569. params[key] = value;
  51570. });
  51571. });
  51572. return configurations;
  51573. }
  51574. /**
  51575. * Decompress data. Concurrency limited.
  51576. *
  51577. * @param {Buffer} data Compressed data
  51578. * @param {Boolean} fin Specifies whether or not this is the last fragment
  51579. * @param {Function} callback Callback
  51580. * @public
  51581. */
  51582. decompress(data, fin, callback) {
  51583. zlibLimiter.add((done) => {
  51584. this._decompress(data, fin, (err, result) => {
  51585. done();
  51586. callback(err, result);
  51587. });
  51588. });
  51589. }
  51590. /**
  51591. * Compress data. Concurrency limited.
  51592. *
  51593. * @param {(Buffer|String)} data Data to compress
  51594. * @param {Boolean} fin Specifies whether or not this is the last fragment
  51595. * @param {Function} callback Callback
  51596. * @public
  51597. */
  51598. compress(data, fin, callback) {
  51599. zlibLimiter.add((done) => {
  51600. this._compress(data, fin, (err, result) => {
  51601. done();
  51602. callback(err, result);
  51603. });
  51604. });
  51605. }
  51606. /**
  51607. * Decompress data.
  51608. *
  51609. * @param {Buffer} data Compressed data
  51610. * @param {Boolean} fin Specifies whether or not this is the last fragment
  51611. * @param {Function} callback Callback
  51612. * @private
  51613. */
  51614. _decompress(data, fin, callback) {
  51615. const endpoint = this._isServer ? 'client' : 'server';
  51616. if (!this._inflate) {
  51617. const key = `${endpoint}_max_window_bits`;
  51618. const windowBits =
  51619. typeof this.params[key] !== 'number'
  51620. ? zlib.Z_DEFAULT_WINDOWBITS
  51621. : this.params[key];
  51622. this._inflate = zlib.createInflateRaw({
  51623. ...this._options.zlibInflateOptions,
  51624. windowBits
  51625. });
  51626. this._inflate[kPerMessageDeflate] = this;
  51627. this._inflate[kTotalLength] = 0;
  51628. this._inflate[kBuffers] = [];
  51629. this._inflate.on('error', inflateOnError);
  51630. this._inflate.on('data', inflateOnData);
  51631. }
  51632. this._inflate[kCallback] = callback;
  51633. this._inflate.write(data);
  51634. if (fin) this._inflate.write(TRAILER);
  51635. this._inflate.flush(() => {
  51636. const err = this._inflate[kError$1];
  51637. if (err) {
  51638. this._inflate.close();
  51639. this._inflate = null;
  51640. callback(err);
  51641. return;
  51642. }
  51643. const data = bufferUtil.concat(
  51644. this._inflate[kBuffers],
  51645. this._inflate[kTotalLength]
  51646. );
  51647. if (this._inflate._readableState.endEmitted) {
  51648. this._inflate.close();
  51649. this._inflate = null;
  51650. } else {
  51651. this._inflate[kTotalLength] = 0;
  51652. this._inflate[kBuffers] = [];
  51653. if (fin && this.params[`${endpoint}_no_context_takeover`]) {
  51654. this._inflate.reset();
  51655. }
  51656. }
  51657. callback(null, data);
  51658. });
  51659. }
  51660. /**
  51661. * Compress data.
  51662. *
  51663. * @param {(Buffer|String)} data Data to compress
  51664. * @param {Boolean} fin Specifies whether or not this is the last fragment
  51665. * @param {Function} callback Callback
  51666. * @private
  51667. */
  51668. _compress(data, fin, callback) {
  51669. const endpoint = this._isServer ? 'server' : 'client';
  51670. if (!this._deflate) {
  51671. const key = `${endpoint}_max_window_bits`;
  51672. const windowBits =
  51673. typeof this.params[key] !== 'number'
  51674. ? zlib.Z_DEFAULT_WINDOWBITS
  51675. : this.params[key];
  51676. this._deflate = zlib.createDeflateRaw({
  51677. ...this._options.zlibDeflateOptions,
  51678. windowBits
  51679. });
  51680. this._deflate[kTotalLength] = 0;
  51681. this._deflate[kBuffers] = [];
  51682. this._deflate.on('data', deflateOnData);
  51683. }
  51684. this._deflate[kCallback] = callback;
  51685. this._deflate.write(data);
  51686. this._deflate.flush(zlib.Z_SYNC_FLUSH, () => {
  51687. if (!this._deflate) {
  51688. //
  51689. // The deflate stream was closed while data was being processed.
  51690. //
  51691. return;
  51692. }
  51693. let data = bufferUtil.concat(
  51694. this._deflate[kBuffers],
  51695. this._deflate[kTotalLength]
  51696. );
  51697. if (fin) {
  51698. data = new FastBuffer$1(data.buffer, data.byteOffset, data.length - 4);
  51699. }
  51700. //
  51701. // Ensure that the callback will not be called again in
  51702. // `PerMessageDeflate#cleanup()`.
  51703. //
  51704. this._deflate[kCallback] = null;
  51705. this._deflate[kTotalLength] = 0;
  51706. this._deflate[kBuffers] = [];
  51707. if (fin && this.params[`${endpoint}_no_context_takeover`]) {
  51708. this._deflate.reset();
  51709. }
  51710. callback(null, data);
  51711. });
  51712. }
  51713. };
  51714. var permessageDeflate = PerMessageDeflate$4;
  51715. /**
  51716. * The listener of the `zlib.DeflateRaw` stream `'data'` event.
  51717. *
  51718. * @param {Buffer} chunk A chunk of data
  51719. * @private
  51720. */
  51721. function deflateOnData(chunk) {
  51722. this[kBuffers].push(chunk);
  51723. this[kTotalLength] += chunk.length;
  51724. }
  51725. /**
  51726. * The listener of the `zlib.InflateRaw` stream `'data'` event.
  51727. *
  51728. * @param {Buffer} chunk A chunk of data
  51729. * @private
  51730. */
  51731. function inflateOnData(chunk) {
  51732. this[kTotalLength] += chunk.length;
  51733. if (
  51734. this[kPerMessageDeflate]._maxPayload < 1 ||
  51735. this[kTotalLength] <= this[kPerMessageDeflate]._maxPayload
  51736. ) {
  51737. this[kBuffers].push(chunk);
  51738. return;
  51739. }
  51740. this[kError$1] = new RangeError('Max payload size exceeded');
  51741. this[kError$1].code = 'WS_ERR_UNSUPPORTED_MESSAGE_LENGTH';
  51742. this[kError$1][kStatusCode$2] = 1009;
  51743. this.removeListener('data', inflateOnData);
  51744. this.reset();
  51745. }
  51746. /**
  51747. * The listener of the `zlib.InflateRaw` stream `'error'` event.
  51748. *
  51749. * @param {Error} err The emitted error
  51750. * @private
  51751. */
  51752. function inflateOnError(err) {
  51753. //
  51754. // There is no need to call `Zlib#close()` as the handle is automatically
  51755. // closed when an error is emitted.
  51756. //
  51757. this[kPerMessageDeflate]._inflate = null;
  51758. err[kStatusCode$2] = 1007;
  51759. this[kCallback](err);
  51760. }
  51761. var validation = {exports: {}};
  51762. const { isUtf8 } = require$$0$a;
  51763. const { hasBlob } = constants;
  51764. //
  51765. // Allowed token characters:
  51766. //
  51767. // '!', '#', '$', '%', '&', ''', '*', '+', '-',
  51768. // '.', 0-9, A-Z, '^', '_', '`', a-z, '|', '~'
  51769. //
  51770. // tokenChars[32] === 0 // ' '
  51771. // tokenChars[33] === 1 // '!'
  51772. // tokenChars[34] === 0 // '"'
  51773. // ...
  51774. //
  51775. // prettier-ignore
  51776. const tokenChars$2 = [
  51777. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0 - 15
  51778. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 16 - 31
  51779. 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, // 32 - 47
  51780. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, // 48 - 63
  51781. 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 64 - 79
  51782. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, // 80 - 95
  51783. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 96 - 111
  51784. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0 // 112 - 127
  51785. ];
  51786. /**
  51787. * Checks if a status code is allowed in a close frame.
  51788. *
  51789. * @param {Number} code The status code
  51790. * @return {Boolean} `true` if the status code is valid, else `false`
  51791. * @public
  51792. */
  51793. function isValidStatusCode$2(code) {
  51794. return (
  51795. (code >= 1000 &&
  51796. code <= 1014 &&
  51797. code !== 1004 &&
  51798. code !== 1005 &&
  51799. code !== 1006) ||
  51800. (code >= 3000 && code <= 4999)
  51801. );
  51802. }
  51803. /**
  51804. * Checks if a given buffer contains only correct UTF-8.
  51805. * Ported from https://www.cl.cam.ac.uk/%7Emgk25/ucs/utf8_check.c by
  51806. * Markus Kuhn.
  51807. *
  51808. * @param {Buffer} buf The buffer to check
  51809. * @return {Boolean} `true` if `buf` contains only correct UTF-8, else `false`
  51810. * @public
  51811. */
  51812. function _isValidUTF8(buf) {
  51813. const len = buf.length;
  51814. let i = 0;
  51815. while (i < len) {
  51816. if ((buf[i] & 0x80) === 0) {
  51817. // 0xxxxxxx
  51818. i++;
  51819. } else if ((buf[i] & 0xe0) === 0xc0) {
  51820. // 110xxxxx 10xxxxxx
  51821. if (
  51822. i + 1 === len ||
  51823. (buf[i + 1] & 0xc0) !== 0x80 ||
  51824. (buf[i] & 0xfe) === 0xc0 // Overlong
  51825. ) {
  51826. return false;
  51827. }
  51828. i += 2;
  51829. } else if ((buf[i] & 0xf0) === 0xe0) {
  51830. // 1110xxxx 10xxxxxx 10xxxxxx
  51831. if (
  51832. i + 2 >= len ||
  51833. (buf[i + 1] & 0xc0) !== 0x80 ||
  51834. (buf[i + 2] & 0xc0) !== 0x80 ||
  51835. (buf[i] === 0xe0 && (buf[i + 1] & 0xe0) === 0x80) || // Overlong
  51836. (buf[i] === 0xed && (buf[i + 1] & 0xe0) === 0xa0) // Surrogate (U+D800 - U+DFFF)
  51837. ) {
  51838. return false;
  51839. }
  51840. i += 3;
  51841. } else if ((buf[i] & 0xf8) === 0xf0) {
  51842. // 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
  51843. if (
  51844. i + 3 >= len ||
  51845. (buf[i + 1] & 0xc0) !== 0x80 ||
  51846. (buf[i + 2] & 0xc0) !== 0x80 ||
  51847. (buf[i + 3] & 0xc0) !== 0x80 ||
  51848. (buf[i] === 0xf0 && (buf[i + 1] & 0xf0) === 0x80) || // Overlong
  51849. (buf[i] === 0xf4 && buf[i + 1] > 0x8f) ||
  51850. buf[i] > 0xf4 // > U+10FFFF
  51851. ) {
  51852. return false;
  51853. }
  51854. i += 4;
  51855. } else {
  51856. return false;
  51857. }
  51858. }
  51859. return true;
  51860. }
  51861. /**
  51862. * Determines whether a value is a `Blob`.
  51863. *
  51864. * @param {*} value The value to be tested
  51865. * @return {Boolean} `true` if `value` is a `Blob`, else `false`
  51866. * @private
  51867. */
  51868. function isBlob$2(value) {
  51869. return (
  51870. hasBlob &&
  51871. typeof value === 'object' &&
  51872. typeof value.arrayBuffer === 'function' &&
  51873. typeof value.type === 'string' &&
  51874. typeof value.stream === 'function' &&
  51875. (value[Symbol.toStringTag] === 'Blob' ||
  51876. value[Symbol.toStringTag] === 'File')
  51877. );
  51878. }
  51879. validation.exports = {
  51880. isBlob: isBlob$2,
  51881. isValidStatusCode: isValidStatusCode$2,
  51882. isValidUTF8: _isValidUTF8,
  51883. tokenChars: tokenChars$2
  51884. };
  51885. if (isUtf8) {
  51886. validation.exports.isValidUTF8 = function (buf) {
  51887. return buf.length < 24 ? _isValidUTF8(buf) : isUtf8(buf);
  51888. };
  51889. } /* istanbul ignore else */ else if (!process.env.WS_NO_UTF_8_VALIDATE) {
  51890. try {
  51891. const isValidUTF8 = require('utf-8-validate');
  51892. validation.exports.isValidUTF8 = function (buf) {
  51893. return buf.length < 32 ? _isValidUTF8(buf) : isValidUTF8(buf);
  51894. };
  51895. } catch (e) {
  51896. // Continue regardless of the error.
  51897. }
  51898. }
  51899. var validationExports = validation.exports;
  51900. const { Writable: Writable$1 } = require$$0$6;
  51901. const PerMessageDeflate$3 = permessageDeflate;
  51902. const {
  51903. BINARY_TYPES: BINARY_TYPES$1,
  51904. EMPTY_BUFFER: EMPTY_BUFFER$2,
  51905. kStatusCode: kStatusCode$1,
  51906. kWebSocket: kWebSocket$3
  51907. } = constants;
  51908. const { concat, toArrayBuffer, unmask } = bufferUtilExports;
  51909. const { isValidStatusCode: isValidStatusCode$1, isValidUTF8 } = validationExports;
  51910. const FastBuffer = Buffer[Symbol.species];
  51911. const GET_INFO = 0;
  51912. const GET_PAYLOAD_LENGTH_16 = 1;
  51913. const GET_PAYLOAD_LENGTH_64 = 2;
  51914. const GET_MASK = 3;
  51915. const GET_DATA = 4;
  51916. const INFLATING = 5;
  51917. const DEFER_EVENT = 6;
  51918. /**
  51919. * HyBi Receiver implementation.
  51920. *
  51921. * @extends Writable
  51922. */
  51923. let Receiver$1 = class Receiver extends Writable$1 {
  51924. /**
  51925. * Creates a Receiver instance.
  51926. *
  51927. * @param {Object} [options] Options object
  51928. * @param {Boolean} [options.allowSynchronousEvents=true] Specifies whether
  51929. * any of the `'message'`, `'ping'`, and `'pong'` events can be emitted
  51930. * multiple times in the same tick
  51931. * @param {String} [options.binaryType=nodebuffer] The type for binary data
  51932. * @param {Object} [options.extensions] An object containing the negotiated
  51933. * extensions
  51934. * @param {Boolean} [options.isServer=false] Specifies whether to operate in
  51935. * client or server mode
  51936. * @param {Number} [options.maxPayload=0] The maximum allowed message length
  51937. * @param {Boolean} [options.skipUTF8Validation=false] Specifies whether or
  51938. * not to skip UTF-8 validation for text and close messages
  51939. */
  51940. constructor(options = {}) {
  51941. super();
  51942. this._allowSynchronousEvents =
  51943. options.allowSynchronousEvents !== undefined
  51944. ? options.allowSynchronousEvents
  51945. : true;
  51946. this._binaryType = options.binaryType || BINARY_TYPES$1[0];
  51947. this._extensions = options.extensions || {};
  51948. this._isServer = !!options.isServer;
  51949. this._maxPayload = options.maxPayload | 0;
  51950. this._skipUTF8Validation = !!options.skipUTF8Validation;
  51951. this[kWebSocket$3] = undefined;
  51952. this._bufferedBytes = 0;
  51953. this._buffers = [];
  51954. this._compressed = false;
  51955. this._payloadLength = 0;
  51956. this._mask = undefined;
  51957. this._fragmented = 0;
  51958. this._masked = false;
  51959. this._fin = false;
  51960. this._opcode = 0;
  51961. this._totalPayloadLength = 0;
  51962. this._messageLength = 0;
  51963. this._fragments = [];
  51964. this._errored = false;
  51965. this._loop = false;
  51966. this._state = GET_INFO;
  51967. }
  51968. /**
  51969. * Implements `Writable.prototype._write()`.
  51970. *
  51971. * @param {Buffer} chunk The chunk of data to write
  51972. * @param {String} encoding The character encoding of `chunk`
  51973. * @param {Function} cb Callback
  51974. * @private
  51975. */
  51976. _write(chunk, encoding, cb) {
  51977. if (this._opcode === 0x08 && this._state == GET_INFO) return cb();
  51978. this._bufferedBytes += chunk.length;
  51979. this._buffers.push(chunk);
  51980. this.startLoop(cb);
  51981. }
  51982. /**
  51983. * Consumes `n` bytes from the buffered data.
  51984. *
  51985. * @param {Number} n The number of bytes to consume
  51986. * @return {Buffer} The consumed bytes
  51987. * @private
  51988. */
  51989. consume(n) {
  51990. this._bufferedBytes -= n;
  51991. if (n === this._buffers[0].length) return this._buffers.shift();
  51992. if (n < this._buffers[0].length) {
  51993. const buf = this._buffers[0];
  51994. this._buffers[0] = new FastBuffer(
  51995. buf.buffer,
  51996. buf.byteOffset + n,
  51997. buf.length - n
  51998. );
  51999. return new FastBuffer(buf.buffer, buf.byteOffset, n);
  52000. }
  52001. const dst = Buffer.allocUnsafe(n);
  52002. do {
  52003. const buf = this._buffers[0];
  52004. const offset = dst.length - n;
  52005. if (n >= buf.length) {
  52006. dst.set(this._buffers.shift(), offset);
  52007. } else {
  52008. dst.set(new Uint8Array(buf.buffer, buf.byteOffset, n), offset);
  52009. this._buffers[0] = new FastBuffer(
  52010. buf.buffer,
  52011. buf.byteOffset + n,
  52012. buf.length - n
  52013. );
  52014. }
  52015. n -= buf.length;
  52016. } while (n > 0);
  52017. return dst;
  52018. }
  52019. /**
  52020. * Starts the parsing loop.
  52021. *
  52022. * @param {Function} cb Callback
  52023. * @private
  52024. */
  52025. startLoop(cb) {
  52026. this._loop = true;
  52027. do {
  52028. switch (this._state) {
  52029. case GET_INFO:
  52030. this.getInfo(cb);
  52031. break;
  52032. case GET_PAYLOAD_LENGTH_16:
  52033. this.getPayloadLength16(cb);
  52034. break;
  52035. case GET_PAYLOAD_LENGTH_64:
  52036. this.getPayloadLength64(cb);
  52037. break;
  52038. case GET_MASK:
  52039. this.getMask();
  52040. break;
  52041. case GET_DATA:
  52042. this.getData(cb);
  52043. break;
  52044. case INFLATING:
  52045. case DEFER_EVENT:
  52046. this._loop = false;
  52047. return;
  52048. }
  52049. } while (this._loop);
  52050. if (!this._errored) cb();
  52051. }
  52052. /**
  52053. * Reads the first two bytes of a frame.
  52054. *
  52055. * @param {Function} cb Callback
  52056. * @private
  52057. */
  52058. getInfo(cb) {
  52059. if (this._bufferedBytes < 2) {
  52060. this._loop = false;
  52061. return;
  52062. }
  52063. const buf = this.consume(2);
  52064. if ((buf[0] & 0x30) !== 0x00) {
  52065. const error = this.createError(
  52066. RangeError,
  52067. 'RSV2 and RSV3 must be clear',
  52068. true,
  52069. 1002,
  52070. 'WS_ERR_UNEXPECTED_RSV_2_3'
  52071. );
  52072. cb(error);
  52073. return;
  52074. }
  52075. const compressed = (buf[0] & 0x40) === 0x40;
  52076. if (compressed && !this._extensions[PerMessageDeflate$3.extensionName]) {
  52077. const error = this.createError(
  52078. RangeError,
  52079. 'RSV1 must be clear',
  52080. true,
  52081. 1002,
  52082. 'WS_ERR_UNEXPECTED_RSV_1'
  52083. );
  52084. cb(error);
  52085. return;
  52086. }
  52087. this._fin = (buf[0] & 0x80) === 0x80;
  52088. this._opcode = buf[0] & 0x0f;
  52089. this._payloadLength = buf[1] & 0x7f;
  52090. if (this._opcode === 0x00) {
  52091. if (compressed) {
  52092. const error = this.createError(
  52093. RangeError,
  52094. 'RSV1 must be clear',
  52095. true,
  52096. 1002,
  52097. 'WS_ERR_UNEXPECTED_RSV_1'
  52098. );
  52099. cb(error);
  52100. return;
  52101. }
  52102. if (!this._fragmented) {
  52103. const error = this.createError(
  52104. RangeError,
  52105. 'invalid opcode 0',
  52106. true,
  52107. 1002,
  52108. 'WS_ERR_INVALID_OPCODE'
  52109. );
  52110. cb(error);
  52111. return;
  52112. }
  52113. this._opcode = this._fragmented;
  52114. } else if (this._opcode === 0x01 || this._opcode === 0x02) {
  52115. if (this._fragmented) {
  52116. const error = this.createError(
  52117. RangeError,
  52118. `invalid opcode ${this._opcode}`,
  52119. true,
  52120. 1002,
  52121. 'WS_ERR_INVALID_OPCODE'
  52122. );
  52123. cb(error);
  52124. return;
  52125. }
  52126. this._compressed = compressed;
  52127. } else if (this._opcode > 0x07 && this._opcode < 0x0b) {
  52128. if (!this._fin) {
  52129. const error = this.createError(
  52130. RangeError,
  52131. 'FIN must be set',
  52132. true,
  52133. 1002,
  52134. 'WS_ERR_EXPECTED_FIN'
  52135. );
  52136. cb(error);
  52137. return;
  52138. }
  52139. if (compressed) {
  52140. const error = this.createError(
  52141. RangeError,
  52142. 'RSV1 must be clear',
  52143. true,
  52144. 1002,
  52145. 'WS_ERR_UNEXPECTED_RSV_1'
  52146. );
  52147. cb(error);
  52148. return;
  52149. }
  52150. if (
  52151. this._payloadLength > 0x7d ||
  52152. (this._opcode === 0x08 && this._payloadLength === 1)
  52153. ) {
  52154. const error = this.createError(
  52155. RangeError,
  52156. `invalid payload length ${this._payloadLength}`,
  52157. true,
  52158. 1002,
  52159. 'WS_ERR_INVALID_CONTROL_PAYLOAD_LENGTH'
  52160. );
  52161. cb(error);
  52162. return;
  52163. }
  52164. } else {
  52165. const error = this.createError(
  52166. RangeError,
  52167. `invalid opcode ${this._opcode}`,
  52168. true,
  52169. 1002,
  52170. 'WS_ERR_INVALID_OPCODE'
  52171. );
  52172. cb(error);
  52173. return;
  52174. }
  52175. if (!this._fin && !this._fragmented) this._fragmented = this._opcode;
  52176. this._masked = (buf[1] & 0x80) === 0x80;
  52177. if (this._isServer) {
  52178. if (!this._masked) {
  52179. const error = this.createError(
  52180. RangeError,
  52181. 'MASK must be set',
  52182. true,
  52183. 1002,
  52184. 'WS_ERR_EXPECTED_MASK'
  52185. );
  52186. cb(error);
  52187. return;
  52188. }
  52189. } else if (this._masked) {
  52190. const error = this.createError(
  52191. RangeError,
  52192. 'MASK must be clear',
  52193. true,
  52194. 1002,
  52195. 'WS_ERR_UNEXPECTED_MASK'
  52196. );
  52197. cb(error);
  52198. return;
  52199. }
  52200. if (this._payloadLength === 126) this._state = GET_PAYLOAD_LENGTH_16;
  52201. else if (this._payloadLength === 127) this._state = GET_PAYLOAD_LENGTH_64;
  52202. else this.haveLength(cb);
  52203. }
  52204. /**
  52205. * Gets extended payload length (7+16).
  52206. *
  52207. * @param {Function} cb Callback
  52208. * @private
  52209. */
  52210. getPayloadLength16(cb) {
  52211. if (this._bufferedBytes < 2) {
  52212. this._loop = false;
  52213. return;
  52214. }
  52215. this._payloadLength = this.consume(2).readUInt16BE(0);
  52216. this.haveLength(cb);
  52217. }
  52218. /**
  52219. * Gets extended payload length (7+64).
  52220. *
  52221. * @param {Function} cb Callback
  52222. * @private
  52223. */
  52224. getPayloadLength64(cb) {
  52225. if (this._bufferedBytes < 8) {
  52226. this._loop = false;
  52227. return;
  52228. }
  52229. const buf = this.consume(8);
  52230. const num = buf.readUInt32BE(0);
  52231. //
  52232. // The maximum safe integer in JavaScript is 2^53 - 1. An error is returned
  52233. // if payload length is greater than this number.
  52234. //
  52235. if (num > Math.pow(2, 53 - 32) - 1) {
  52236. const error = this.createError(
  52237. RangeError,
  52238. 'Unsupported WebSocket frame: payload length > 2^53 - 1',
  52239. false,
  52240. 1009,
  52241. 'WS_ERR_UNSUPPORTED_DATA_PAYLOAD_LENGTH'
  52242. );
  52243. cb(error);
  52244. return;
  52245. }
  52246. this._payloadLength = num * Math.pow(2, 32) + buf.readUInt32BE(4);
  52247. this.haveLength(cb);
  52248. }
  52249. /**
  52250. * Payload length has been read.
  52251. *
  52252. * @param {Function} cb Callback
  52253. * @private
  52254. */
  52255. haveLength(cb) {
  52256. if (this._payloadLength && this._opcode < 0x08) {
  52257. this._totalPayloadLength += this._payloadLength;
  52258. if (this._totalPayloadLength > this._maxPayload && this._maxPayload > 0) {
  52259. const error = this.createError(
  52260. RangeError,
  52261. 'Max payload size exceeded',
  52262. false,
  52263. 1009,
  52264. 'WS_ERR_UNSUPPORTED_MESSAGE_LENGTH'
  52265. );
  52266. cb(error);
  52267. return;
  52268. }
  52269. }
  52270. if (this._masked) this._state = GET_MASK;
  52271. else this._state = GET_DATA;
  52272. }
  52273. /**
  52274. * Reads mask bytes.
  52275. *
  52276. * @private
  52277. */
  52278. getMask() {
  52279. if (this._bufferedBytes < 4) {
  52280. this._loop = false;
  52281. return;
  52282. }
  52283. this._mask = this.consume(4);
  52284. this._state = GET_DATA;
  52285. }
  52286. /**
  52287. * Reads data bytes.
  52288. *
  52289. * @param {Function} cb Callback
  52290. * @private
  52291. */
  52292. getData(cb) {
  52293. let data = EMPTY_BUFFER$2;
  52294. if (this._payloadLength) {
  52295. if (this._bufferedBytes < this._payloadLength) {
  52296. this._loop = false;
  52297. return;
  52298. }
  52299. data = this.consume(this._payloadLength);
  52300. if (
  52301. this._masked &&
  52302. (this._mask[0] | this._mask[1] | this._mask[2] | this._mask[3]) !== 0
  52303. ) {
  52304. unmask(data, this._mask);
  52305. }
  52306. }
  52307. if (this._opcode > 0x07) {
  52308. this.controlMessage(data, cb);
  52309. return;
  52310. }
  52311. if (this._compressed) {
  52312. this._state = INFLATING;
  52313. this.decompress(data, cb);
  52314. return;
  52315. }
  52316. if (data.length) {
  52317. //
  52318. // This message is not compressed so its length is the sum of the payload
  52319. // length of all fragments.
  52320. //
  52321. this._messageLength = this._totalPayloadLength;
  52322. this._fragments.push(data);
  52323. }
  52324. this.dataMessage(cb);
  52325. }
  52326. /**
  52327. * Decompresses data.
  52328. *
  52329. * @param {Buffer} data Compressed data
  52330. * @param {Function} cb Callback
  52331. * @private
  52332. */
  52333. decompress(data, cb) {
  52334. const perMessageDeflate = this._extensions[PerMessageDeflate$3.extensionName];
  52335. perMessageDeflate.decompress(data, this._fin, (err, buf) => {
  52336. if (err) return cb(err);
  52337. if (buf.length) {
  52338. this._messageLength += buf.length;
  52339. if (this._messageLength > this._maxPayload && this._maxPayload > 0) {
  52340. const error = this.createError(
  52341. RangeError,
  52342. 'Max payload size exceeded',
  52343. false,
  52344. 1009,
  52345. 'WS_ERR_UNSUPPORTED_MESSAGE_LENGTH'
  52346. );
  52347. cb(error);
  52348. return;
  52349. }
  52350. this._fragments.push(buf);
  52351. }
  52352. this.dataMessage(cb);
  52353. if (this._state === GET_INFO) this.startLoop(cb);
  52354. });
  52355. }
  52356. /**
  52357. * Handles a data message.
  52358. *
  52359. * @param {Function} cb Callback
  52360. * @private
  52361. */
  52362. dataMessage(cb) {
  52363. if (!this._fin) {
  52364. this._state = GET_INFO;
  52365. return;
  52366. }
  52367. const messageLength = this._messageLength;
  52368. const fragments = this._fragments;
  52369. this._totalPayloadLength = 0;
  52370. this._messageLength = 0;
  52371. this._fragmented = 0;
  52372. this._fragments = [];
  52373. if (this._opcode === 2) {
  52374. let data;
  52375. if (this._binaryType === 'nodebuffer') {
  52376. data = concat(fragments, messageLength);
  52377. } else if (this._binaryType === 'arraybuffer') {
  52378. data = toArrayBuffer(concat(fragments, messageLength));
  52379. } else if (this._binaryType === 'blob') {
  52380. data = new Blob(fragments);
  52381. } else {
  52382. data = fragments;
  52383. }
  52384. if (this._allowSynchronousEvents) {
  52385. this.emit('message', data, true);
  52386. this._state = GET_INFO;
  52387. } else {
  52388. this._state = DEFER_EVENT;
  52389. setImmediate(() => {
  52390. this.emit('message', data, true);
  52391. this._state = GET_INFO;
  52392. this.startLoop(cb);
  52393. });
  52394. }
  52395. } else {
  52396. const buf = concat(fragments, messageLength);
  52397. if (!this._skipUTF8Validation && !isValidUTF8(buf)) {
  52398. const error = this.createError(
  52399. Error,
  52400. 'invalid UTF-8 sequence',
  52401. true,
  52402. 1007,
  52403. 'WS_ERR_INVALID_UTF8'
  52404. );
  52405. cb(error);
  52406. return;
  52407. }
  52408. if (this._state === INFLATING || this._allowSynchronousEvents) {
  52409. this.emit('message', buf, false);
  52410. this._state = GET_INFO;
  52411. } else {
  52412. this._state = DEFER_EVENT;
  52413. setImmediate(() => {
  52414. this.emit('message', buf, false);
  52415. this._state = GET_INFO;
  52416. this.startLoop(cb);
  52417. });
  52418. }
  52419. }
  52420. }
  52421. /**
  52422. * Handles a control message.
  52423. *
  52424. * @param {Buffer} data Data to handle
  52425. * @return {(Error|RangeError|undefined)} A possible error
  52426. * @private
  52427. */
  52428. controlMessage(data, cb) {
  52429. if (this._opcode === 0x08) {
  52430. if (data.length === 0) {
  52431. this._loop = false;
  52432. this.emit('conclude', 1005, EMPTY_BUFFER$2);
  52433. this.end();
  52434. } else {
  52435. const code = data.readUInt16BE(0);
  52436. if (!isValidStatusCode$1(code)) {
  52437. const error = this.createError(
  52438. RangeError,
  52439. `invalid status code ${code}`,
  52440. true,
  52441. 1002,
  52442. 'WS_ERR_INVALID_CLOSE_CODE'
  52443. );
  52444. cb(error);
  52445. return;
  52446. }
  52447. const buf = new FastBuffer(
  52448. data.buffer,
  52449. data.byteOffset + 2,
  52450. data.length - 2
  52451. );
  52452. if (!this._skipUTF8Validation && !isValidUTF8(buf)) {
  52453. const error = this.createError(
  52454. Error,
  52455. 'invalid UTF-8 sequence',
  52456. true,
  52457. 1007,
  52458. 'WS_ERR_INVALID_UTF8'
  52459. );
  52460. cb(error);
  52461. return;
  52462. }
  52463. this._loop = false;
  52464. this.emit('conclude', code, buf);
  52465. this.end();
  52466. }
  52467. this._state = GET_INFO;
  52468. return;
  52469. }
  52470. if (this._allowSynchronousEvents) {
  52471. this.emit(this._opcode === 0x09 ? 'ping' : 'pong', data);
  52472. this._state = GET_INFO;
  52473. } else {
  52474. this._state = DEFER_EVENT;
  52475. setImmediate(() => {
  52476. this.emit(this._opcode === 0x09 ? 'ping' : 'pong', data);
  52477. this._state = GET_INFO;
  52478. this.startLoop(cb);
  52479. });
  52480. }
  52481. }
  52482. /**
  52483. * Builds an error object.
  52484. *
  52485. * @param {function(new:Error|RangeError)} ErrorCtor The error constructor
  52486. * @param {String} message The error message
  52487. * @param {Boolean} prefix Specifies whether or not to add a default prefix to
  52488. * `message`
  52489. * @param {Number} statusCode The status code
  52490. * @param {String} errorCode The exposed error code
  52491. * @return {(Error|RangeError)} The error
  52492. * @private
  52493. */
  52494. createError(ErrorCtor, message, prefix, statusCode, errorCode) {
  52495. this._loop = false;
  52496. this._errored = true;
  52497. const err = new ErrorCtor(
  52498. prefix ? `Invalid WebSocket frame: ${message}` : message
  52499. );
  52500. Error.captureStackTrace(err, this.createError);
  52501. err.code = errorCode;
  52502. err[kStatusCode$1] = statusCode;
  52503. return err;
  52504. }
  52505. };
  52506. var receiver = Receiver$1;
  52507. /* eslint no-unused-vars: ["error", { "varsIgnorePattern": "^Duplex" }] */
  52508. const { randomFillSync } = require$$3$1;
  52509. const PerMessageDeflate$2 = permessageDeflate;
  52510. const { EMPTY_BUFFER: EMPTY_BUFFER$1, kWebSocket: kWebSocket$2, NOOP: NOOP$2 } = constants;
  52511. const { isBlob: isBlob$1, isValidStatusCode } = validationExports;
  52512. const { mask: applyMask, toBuffer: toBuffer$1 } = bufferUtilExports;
  52513. const kByteLength = Symbol('kByteLength');
  52514. const maskBuffer = Buffer.alloc(4);
  52515. const RANDOM_POOL_SIZE = 8 * 1024;
  52516. let randomPool;
  52517. let randomPoolPointer = RANDOM_POOL_SIZE;
  52518. const DEFAULT = 0;
  52519. const DEFLATING = 1;
  52520. const GET_BLOB_DATA = 2;
  52521. /**
  52522. * HyBi Sender implementation.
  52523. */
  52524. let Sender$1 = class Sender {
  52525. /**
  52526. * Creates a Sender instance.
  52527. *
  52528. * @param {Duplex} socket The connection socket
  52529. * @param {Object} [extensions] An object containing the negotiated extensions
  52530. * @param {Function} [generateMask] The function used to generate the masking
  52531. * key
  52532. */
  52533. constructor(socket, extensions, generateMask) {
  52534. this._extensions = extensions || {};
  52535. if (generateMask) {
  52536. this._generateMask = generateMask;
  52537. this._maskBuffer = Buffer.alloc(4);
  52538. }
  52539. this._socket = socket;
  52540. this._firstFragment = true;
  52541. this._compress = false;
  52542. this._bufferedBytes = 0;
  52543. this._queue = [];
  52544. this._state = DEFAULT;
  52545. this.onerror = NOOP$2;
  52546. this[kWebSocket$2] = undefined;
  52547. }
  52548. /**
  52549. * Frames a piece of data according to the HyBi WebSocket protocol.
  52550. *
  52551. * @param {(Buffer|String)} data The data to frame
  52552. * @param {Object} options Options object
  52553. * @param {Boolean} [options.fin=false] Specifies whether or not to set the
  52554. * FIN bit
  52555. * @param {Function} [options.generateMask] The function used to generate the
  52556. * masking key
  52557. * @param {Boolean} [options.mask=false] Specifies whether or not to mask
  52558. * `data`
  52559. * @param {Buffer} [options.maskBuffer] The buffer used to store the masking
  52560. * key
  52561. * @param {Number} options.opcode The opcode
  52562. * @param {Boolean} [options.readOnly=false] Specifies whether `data` can be
  52563. * modified
  52564. * @param {Boolean} [options.rsv1=false] Specifies whether or not to set the
  52565. * RSV1 bit
  52566. * @return {(Buffer|String)[]} The framed data
  52567. * @public
  52568. */
  52569. static frame(data, options) {
  52570. let mask;
  52571. let merge = false;
  52572. let offset = 2;
  52573. let skipMasking = false;
  52574. if (options.mask) {
  52575. mask = options.maskBuffer || maskBuffer;
  52576. if (options.generateMask) {
  52577. options.generateMask(mask);
  52578. } else {
  52579. if (randomPoolPointer === RANDOM_POOL_SIZE) {
  52580. /* istanbul ignore else */
  52581. if (randomPool === undefined) {
  52582. //
  52583. // This is lazily initialized because server-sent frames must not
  52584. // be masked so it may never be used.
  52585. //
  52586. randomPool = Buffer.alloc(RANDOM_POOL_SIZE);
  52587. }
  52588. randomFillSync(randomPool, 0, RANDOM_POOL_SIZE);
  52589. randomPoolPointer = 0;
  52590. }
  52591. mask[0] = randomPool[randomPoolPointer++];
  52592. mask[1] = randomPool[randomPoolPointer++];
  52593. mask[2] = randomPool[randomPoolPointer++];
  52594. mask[3] = randomPool[randomPoolPointer++];
  52595. }
  52596. skipMasking = (mask[0] | mask[1] | mask[2] | mask[3]) === 0;
  52597. offset = 6;
  52598. }
  52599. let dataLength;
  52600. if (typeof data === 'string') {
  52601. if (
  52602. (!options.mask || skipMasking) &&
  52603. options[kByteLength] !== undefined
  52604. ) {
  52605. dataLength = options[kByteLength];
  52606. } else {
  52607. data = Buffer.from(data);
  52608. dataLength = data.length;
  52609. }
  52610. } else {
  52611. dataLength = data.length;
  52612. merge = options.mask && options.readOnly && !skipMasking;
  52613. }
  52614. let payloadLength = dataLength;
  52615. if (dataLength >= 65536) {
  52616. offset += 8;
  52617. payloadLength = 127;
  52618. } else if (dataLength > 125) {
  52619. offset += 2;
  52620. payloadLength = 126;
  52621. }
  52622. const target = Buffer.allocUnsafe(merge ? dataLength + offset : offset);
  52623. target[0] = options.fin ? options.opcode | 0x80 : options.opcode;
  52624. if (options.rsv1) target[0] |= 0x40;
  52625. target[1] = payloadLength;
  52626. if (payloadLength === 126) {
  52627. target.writeUInt16BE(dataLength, 2);
  52628. } else if (payloadLength === 127) {
  52629. target[2] = target[3] = 0;
  52630. target.writeUIntBE(dataLength, 4, 6);
  52631. }
  52632. if (!options.mask) return [target, data];
  52633. target[1] |= 0x80;
  52634. target[offset - 4] = mask[0];
  52635. target[offset - 3] = mask[1];
  52636. target[offset - 2] = mask[2];
  52637. target[offset - 1] = mask[3];
  52638. if (skipMasking) return [target, data];
  52639. if (merge) {
  52640. applyMask(data, mask, target, offset, dataLength);
  52641. return [target];
  52642. }
  52643. applyMask(data, mask, data, 0, dataLength);
  52644. return [target, data];
  52645. }
  52646. /**
  52647. * Sends a close message to the other peer.
  52648. *
  52649. * @param {Number} [code] The status code component of the body
  52650. * @param {(String|Buffer)} [data] The message component of the body
  52651. * @param {Boolean} [mask=false] Specifies whether or not to mask the message
  52652. * @param {Function} [cb] Callback
  52653. * @public
  52654. */
  52655. close(code, data, mask, cb) {
  52656. let buf;
  52657. if (code === undefined) {
  52658. buf = EMPTY_BUFFER$1;
  52659. } else if (typeof code !== 'number' || !isValidStatusCode(code)) {
  52660. throw new TypeError('First argument must be a valid error code number');
  52661. } else if (data === undefined || !data.length) {
  52662. buf = Buffer.allocUnsafe(2);
  52663. buf.writeUInt16BE(code, 0);
  52664. } else {
  52665. const length = Buffer.byteLength(data);
  52666. if (length > 123) {
  52667. throw new RangeError('The message must not be greater than 123 bytes');
  52668. }
  52669. buf = Buffer.allocUnsafe(2 + length);
  52670. buf.writeUInt16BE(code, 0);
  52671. if (typeof data === 'string') {
  52672. buf.write(data, 2);
  52673. } else {
  52674. buf.set(data, 2);
  52675. }
  52676. }
  52677. const options = {
  52678. [kByteLength]: buf.length,
  52679. fin: true,
  52680. generateMask: this._generateMask,
  52681. mask,
  52682. maskBuffer: this._maskBuffer,
  52683. opcode: 0x08,
  52684. readOnly: false,
  52685. rsv1: false
  52686. };
  52687. if (this._state !== DEFAULT) {
  52688. this.enqueue([this.dispatch, buf, false, options, cb]);
  52689. } else {
  52690. this.sendFrame(Sender.frame(buf, options), cb);
  52691. }
  52692. }
  52693. /**
  52694. * Sends a ping message to the other peer.
  52695. *
  52696. * @param {*} data The message to send
  52697. * @param {Boolean} [mask=false] Specifies whether or not to mask `data`
  52698. * @param {Function} [cb] Callback
  52699. * @public
  52700. */
  52701. ping(data, mask, cb) {
  52702. let byteLength;
  52703. let readOnly;
  52704. if (typeof data === 'string') {
  52705. byteLength = Buffer.byteLength(data);
  52706. readOnly = false;
  52707. } else if (isBlob$1(data)) {
  52708. byteLength = data.size;
  52709. readOnly = false;
  52710. } else {
  52711. data = toBuffer$1(data);
  52712. byteLength = data.length;
  52713. readOnly = toBuffer$1.readOnly;
  52714. }
  52715. if (byteLength > 125) {
  52716. throw new RangeError('The data size must not be greater than 125 bytes');
  52717. }
  52718. const options = {
  52719. [kByteLength]: byteLength,
  52720. fin: true,
  52721. generateMask: this._generateMask,
  52722. mask,
  52723. maskBuffer: this._maskBuffer,
  52724. opcode: 0x09,
  52725. readOnly,
  52726. rsv1: false
  52727. };
  52728. if (isBlob$1(data)) {
  52729. if (this._state !== DEFAULT) {
  52730. this.enqueue([this.getBlobData, data, false, options, cb]);
  52731. } else {
  52732. this.getBlobData(data, false, options, cb);
  52733. }
  52734. } else if (this._state !== DEFAULT) {
  52735. this.enqueue([this.dispatch, data, false, options, cb]);
  52736. } else {
  52737. this.sendFrame(Sender.frame(data, options), cb);
  52738. }
  52739. }
  52740. /**
  52741. * Sends a pong message to the other peer.
  52742. *
  52743. * @param {*} data The message to send
  52744. * @param {Boolean} [mask=false] Specifies whether or not to mask `data`
  52745. * @param {Function} [cb] Callback
  52746. * @public
  52747. */
  52748. pong(data, mask, cb) {
  52749. let byteLength;
  52750. let readOnly;
  52751. if (typeof data === 'string') {
  52752. byteLength = Buffer.byteLength(data);
  52753. readOnly = false;
  52754. } else if (isBlob$1(data)) {
  52755. byteLength = data.size;
  52756. readOnly = false;
  52757. } else {
  52758. data = toBuffer$1(data);
  52759. byteLength = data.length;
  52760. readOnly = toBuffer$1.readOnly;
  52761. }
  52762. if (byteLength > 125) {
  52763. throw new RangeError('The data size must not be greater than 125 bytes');
  52764. }
  52765. const options = {
  52766. [kByteLength]: byteLength,
  52767. fin: true,
  52768. generateMask: this._generateMask,
  52769. mask,
  52770. maskBuffer: this._maskBuffer,
  52771. opcode: 0x0a,
  52772. readOnly,
  52773. rsv1: false
  52774. };
  52775. if (isBlob$1(data)) {
  52776. if (this._state !== DEFAULT) {
  52777. this.enqueue([this.getBlobData, data, false, options, cb]);
  52778. } else {
  52779. this.getBlobData(data, false, options, cb);
  52780. }
  52781. } else if (this._state !== DEFAULT) {
  52782. this.enqueue([this.dispatch, data, false, options, cb]);
  52783. } else {
  52784. this.sendFrame(Sender.frame(data, options), cb);
  52785. }
  52786. }
  52787. /**
  52788. * Sends a data message to the other peer.
  52789. *
  52790. * @param {*} data The message to send
  52791. * @param {Object} options Options object
  52792. * @param {Boolean} [options.binary=false] Specifies whether `data` is binary
  52793. * or text
  52794. * @param {Boolean} [options.compress=false] Specifies whether or not to
  52795. * compress `data`
  52796. * @param {Boolean} [options.fin=false] Specifies whether the fragment is the
  52797. * last one
  52798. * @param {Boolean} [options.mask=false] Specifies whether or not to mask
  52799. * `data`
  52800. * @param {Function} [cb] Callback
  52801. * @public
  52802. */
  52803. send(data, options, cb) {
  52804. const perMessageDeflate = this._extensions[PerMessageDeflate$2.extensionName];
  52805. let opcode = options.binary ? 2 : 1;
  52806. let rsv1 = options.compress;
  52807. let byteLength;
  52808. let readOnly;
  52809. if (typeof data === 'string') {
  52810. byteLength = Buffer.byteLength(data);
  52811. readOnly = false;
  52812. } else if (isBlob$1(data)) {
  52813. byteLength = data.size;
  52814. readOnly = false;
  52815. } else {
  52816. data = toBuffer$1(data);
  52817. byteLength = data.length;
  52818. readOnly = toBuffer$1.readOnly;
  52819. }
  52820. if (this._firstFragment) {
  52821. this._firstFragment = false;
  52822. if (
  52823. rsv1 &&
  52824. perMessageDeflate &&
  52825. perMessageDeflate.params[
  52826. perMessageDeflate._isServer
  52827. ? 'server_no_context_takeover'
  52828. : 'client_no_context_takeover'
  52829. ]
  52830. ) {
  52831. rsv1 = byteLength >= perMessageDeflate._threshold;
  52832. }
  52833. this._compress = rsv1;
  52834. } else {
  52835. rsv1 = false;
  52836. opcode = 0;
  52837. }
  52838. if (options.fin) this._firstFragment = true;
  52839. const opts = {
  52840. [kByteLength]: byteLength,
  52841. fin: options.fin,
  52842. generateMask: this._generateMask,
  52843. mask: options.mask,
  52844. maskBuffer: this._maskBuffer,
  52845. opcode,
  52846. readOnly,
  52847. rsv1
  52848. };
  52849. if (isBlob$1(data)) {
  52850. if (this._state !== DEFAULT) {
  52851. this.enqueue([this.getBlobData, data, this._compress, opts, cb]);
  52852. } else {
  52853. this.getBlobData(data, this._compress, opts, cb);
  52854. }
  52855. } else if (this._state !== DEFAULT) {
  52856. this.enqueue([this.dispatch, data, this._compress, opts, cb]);
  52857. } else {
  52858. this.dispatch(data, this._compress, opts, cb);
  52859. }
  52860. }
  52861. /**
  52862. * Gets the contents of a blob as binary data.
  52863. *
  52864. * @param {Blob} blob The blob
  52865. * @param {Boolean} [compress=false] Specifies whether or not to compress
  52866. * the data
  52867. * @param {Object} options Options object
  52868. * @param {Boolean} [options.fin=false] Specifies whether or not to set the
  52869. * FIN bit
  52870. * @param {Function} [options.generateMask] The function used to generate the
  52871. * masking key
  52872. * @param {Boolean} [options.mask=false] Specifies whether or not to mask
  52873. * `data`
  52874. * @param {Buffer} [options.maskBuffer] The buffer used to store the masking
  52875. * key
  52876. * @param {Number} options.opcode The opcode
  52877. * @param {Boolean} [options.readOnly=false] Specifies whether `data` can be
  52878. * modified
  52879. * @param {Boolean} [options.rsv1=false] Specifies whether or not to set the
  52880. * RSV1 bit
  52881. * @param {Function} [cb] Callback
  52882. * @private
  52883. */
  52884. getBlobData(blob, compress, options, cb) {
  52885. this._bufferedBytes += options[kByteLength];
  52886. this._state = GET_BLOB_DATA;
  52887. blob
  52888. .arrayBuffer()
  52889. .then((arrayBuffer) => {
  52890. if (this._socket.destroyed) {
  52891. const err = new Error(
  52892. 'The socket was closed while the blob was being read'
  52893. );
  52894. //
  52895. // `callCallbacks` is called in the next tick to ensure that errors
  52896. // that might be thrown in the callbacks behave like errors thrown
  52897. // outside the promise chain.
  52898. //
  52899. process.nextTick(callCallbacks, this, err, cb);
  52900. return;
  52901. }
  52902. this._bufferedBytes -= options[kByteLength];
  52903. const data = toBuffer$1(arrayBuffer);
  52904. if (!compress) {
  52905. this._state = DEFAULT;
  52906. this.sendFrame(Sender.frame(data, options), cb);
  52907. this.dequeue();
  52908. } else {
  52909. this.dispatch(data, compress, options, cb);
  52910. }
  52911. })
  52912. .catch((err) => {
  52913. //
  52914. // `onError` is called in the next tick for the same reason that
  52915. // `callCallbacks` above is.
  52916. //
  52917. process.nextTick(onError, this, err, cb);
  52918. });
  52919. }
  52920. /**
  52921. * Dispatches a message.
  52922. *
  52923. * @param {(Buffer|String)} data The message to send
  52924. * @param {Boolean} [compress=false] Specifies whether or not to compress
  52925. * `data`
  52926. * @param {Object} options Options object
  52927. * @param {Boolean} [options.fin=false] Specifies whether or not to set the
  52928. * FIN bit
  52929. * @param {Function} [options.generateMask] The function used to generate the
  52930. * masking key
  52931. * @param {Boolean} [options.mask=false] Specifies whether or not to mask
  52932. * `data`
  52933. * @param {Buffer} [options.maskBuffer] The buffer used to store the masking
  52934. * key
  52935. * @param {Number} options.opcode The opcode
  52936. * @param {Boolean} [options.readOnly=false] Specifies whether `data` can be
  52937. * modified
  52938. * @param {Boolean} [options.rsv1=false] Specifies whether or not to set the
  52939. * RSV1 bit
  52940. * @param {Function} [cb] Callback
  52941. * @private
  52942. */
  52943. dispatch(data, compress, options, cb) {
  52944. if (!compress) {
  52945. this.sendFrame(Sender.frame(data, options), cb);
  52946. return;
  52947. }
  52948. const perMessageDeflate = this._extensions[PerMessageDeflate$2.extensionName];
  52949. this._bufferedBytes += options[kByteLength];
  52950. this._state = DEFLATING;
  52951. perMessageDeflate.compress(data, options.fin, (_, buf) => {
  52952. if (this._socket.destroyed) {
  52953. const err = new Error(
  52954. 'The socket was closed while data was being compressed'
  52955. );
  52956. callCallbacks(this, err, cb);
  52957. return;
  52958. }
  52959. this._bufferedBytes -= options[kByteLength];
  52960. this._state = DEFAULT;
  52961. options.readOnly = false;
  52962. this.sendFrame(Sender.frame(buf, options), cb);
  52963. this.dequeue();
  52964. });
  52965. }
  52966. /**
  52967. * Executes queued send operations.
  52968. *
  52969. * @private
  52970. */
  52971. dequeue() {
  52972. while (this._state === DEFAULT && this._queue.length) {
  52973. const params = this._queue.shift();
  52974. this._bufferedBytes -= params[3][kByteLength];
  52975. Reflect.apply(params[0], this, params.slice(1));
  52976. }
  52977. }
  52978. /**
  52979. * Enqueues a send operation.
  52980. *
  52981. * @param {Array} params Send operation parameters.
  52982. * @private
  52983. */
  52984. enqueue(params) {
  52985. this._bufferedBytes += params[3][kByteLength];
  52986. this._queue.push(params);
  52987. }
  52988. /**
  52989. * Sends a frame.
  52990. *
  52991. * @param {Buffer[]} list The frame to send
  52992. * @param {Function} [cb] Callback
  52993. * @private
  52994. */
  52995. sendFrame(list, cb) {
  52996. if (list.length === 2) {
  52997. this._socket.cork();
  52998. this._socket.write(list[0]);
  52999. this._socket.write(list[1], cb);
  53000. this._socket.uncork();
  53001. } else {
  53002. this._socket.write(list[0], cb);
  53003. }
  53004. }
  53005. };
  53006. var sender = Sender$1;
  53007. /**
  53008. * Calls queued callbacks with an error.
  53009. *
  53010. * @param {Sender} sender The `Sender` instance
  53011. * @param {Error} err The error to call the callbacks with
  53012. * @param {Function} [cb] The first callback
  53013. * @private
  53014. */
  53015. function callCallbacks(sender, err, cb) {
  53016. if (typeof cb === 'function') cb(err);
  53017. for (let i = 0; i < sender._queue.length; i++) {
  53018. const params = sender._queue[i];
  53019. const callback = params[params.length - 1];
  53020. if (typeof callback === 'function') callback(err);
  53021. }
  53022. }
  53023. /**
  53024. * Handles a `Sender` error.
  53025. *
  53026. * @param {Sender} sender The `Sender` instance
  53027. * @param {Error} err The error
  53028. * @param {Function} [cb] The first pending callback
  53029. * @private
  53030. */
  53031. function onError(sender, err, cb) {
  53032. callCallbacks(sender, err, cb);
  53033. sender.onerror(err);
  53034. }
  53035. const { kForOnEventAttribute: kForOnEventAttribute$1, kListener: kListener$1 } = constants;
  53036. const kCode = Symbol('kCode');
  53037. const kData = Symbol('kData');
  53038. const kError = Symbol('kError');
  53039. const kMessage = Symbol('kMessage');
  53040. const kReason = Symbol('kReason');
  53041. const kTarget = Symbol('kTarget');
  53042. const kType = Symbol('kType');
  53043. const kWasClean = Symbol('kWasClean');
  53044. /**
  53045. * Class representing an event.
  53046. */
  53047. let Event$1 = class Event {
  53048. /**
  53049. * Create a new `Event`.
  53050. *
  53051. * @param {String} type The name of the event
  53052. * @throws {TypeError} If the `type` argument is not specified
  53053. */
  53054. constructor(type) {
  53055. this[kTarget] = null;
  53056. this[kType] = type;
  53057. }
  53058. /**
  53059. * @type {*}
  53060. */
  53061. get target() {
  53062. return this[kTarget];
  53063. }
  53064. /**
  53065. * @type {String}
  53066. */
  53067. get type() {
  53068. return this[kType];
  53069. }
  53070. };
  53071. Object.defineProperty(Event$1.prototype, 'target', { enumerable: true });
  53072. Object.defineProperty(Event$1.prototype, 'type', { enumerable: true });
  53073. /**
  53074. * Class representing a close event.
  53075. *
  53076. * @extends Event
  53077. */
  53078. class CloseEvent extends Event$1 {
  53079. /**
  53080. * Create a new `CloseEvent`.
  53081. *
  53082. * @param {String} type The name of the event
  53083. * @param {Object} [options] A dictionary object that allows for setting
  53084. * attributes via object members of the same name
  53085. * @param {Number} [options.code=0] The status code explaining why the
  53086. * connection was closed
  53087. * @param {String} [options.reason=''] A human-readable string explaining why
  53088. * the connection was closed
  53089. * @param {Boolean} [options.wasClean=false] Indicates whether or not the
  53090. * connection was cleanly closed
  53091. */
  53092. constructor(type, options = {}) {
  53093. super(type);
  53094. this[kCode] = options.code === undefined ? 0 : options.code;
  53095. this[kReason] = options.reason === undefined ? '' : options.reason;
  53096. this[kWasClean] = options.wasClean === undefined ? false : options.wasClean;
  53097. }
  53098. /**
  53099. * @type {Number}
  53100. */
  53101. get code() {
  53102. return this[kCode];
  53103. }
  53104. /**
  53105. * @type {String}
  53106. */
  53107. get reason() {
  53108. return this[kReason];
  53109. }
  53110. /**
  53111. * @type {Boolean}
  53112. */
  53113. get wasClean() {
  53114. return this[kWasClean];
  53115. }
  53116. }
  53117. Object.defineProperty(CloseEvent.prototype, 'code', { enumerable: true });
  53118. Object.defineProperty(CloseEvent.prototype, 'reason', { enumerable: true });
  53119. Object.defineProperty(CloseEvent.prototype, 'wasClean', { enumerable: true });
  53120. /**
  53121. * Class representing an error event.
  53122. *
  53123. * @extends Event
  53124. */
  53125. class ErrorEvent extends Event$1 {
  53126. /**
  53127. * Create a new `ErrorEvent`.
  53128. *
  53129. * @param {String} type The name of the event
  53130. * @param {Object} [options] A dictionary object that allows for setting
  53131. * attributes via object members of the same name
  53132. * @param {*} [options.error=null] The error that generated this event
  53133. * @param {String} [options.message=''] The error message
  53134. */
  53135. constructor(type, options = {}) {
  53136. super(type);
  53137. this[kError] = options.error === undefined ? null : options.error;
  53138. this[kMessage] = options.message === undefined ? '' : options.message;
  53139. }
  53140. /**
  53141. * @type {*}
  53142. */
  53143. get error() {
  53144. return this[kError];
  53145. }
  53146. /**
  53147. * @type {String}
  53148. */
  53149. get message() {
  53150. return this[kMessage];
  53151. }
  53152. }
  53153. Object.defineProperty(ErrorEvent.prototype, 'error', { enumerable: true });
  53154. Object.defineProperty(ErrorEvent.prototype, 'message', { enumerable: true });
  53155. /**
  53156. * Class representing a message event.
  53157. *
  53158. * @extends Event
  53159. */
  53160. class MessageEvent extends Event$1 {
  53161. /**
  53162. * Create a new `MessageEvent`.
  53163. *
  53164. * @param {String} type The name of the event
  53165. * @param {Object} [options] A dictionary object that allows for setting
  53166. * attributes via object members of the same name
  53167. * @param {*} [options.data=null] The message content
  53168. */
  53169. constructor(type, options = {}) {
  53170. super(type);
  53171. this[kData] = options.data === undefined ? null : options.data;
  53172. }
  53173. /**
  53174. * @type {*}
  53175. */
  53176. get data() {
  53177. return this[kData];
  53178. }
  53179. }
  53180. Object.defineProperty(MessageEvent.prototype, 'data', { enumerable: true });
  53181. /**
  53182. * This provides methods for emulating the `EventTarget` interface. It's not
  53183. * meant to be used directly.
  53184. *
  53185. * @mixin
  53186. */
  53187. const EventTarget = {
  53188. /**
  53189. * Register an event listener.
  53190. *
  53191. * @param {String} type A string representing the event type to listen for
  53192. * @param {(Function|Object)} handler The listener to add
  53193. * @param {Object} [options] An options object specifies characteristics about
  53194. * the event listener
  53195. * @param {Boolean} [options.once=false] A `Boolean` indicating that the
  53196. * listener should be invoked at most once after being added. If `true`,
  53197. * the listener would be automatically removed when invoked.
  53198. * @public
  53199. */
  53200. addEventListener(type, handler, options = {}) {
  53201. for (const listener of this.listeners(type)) {
  53202. if (
  53203. !options[kForOnEventAttribute$1] &&
  53204. listener[kListener$1] === handler &&
  53205. !listener[kForOnEventAttribute$1]
  53206. ) {
  53207. return;
  53208. }
  53209. }
  53210. let wrapper;
  53211. if (type === 'message') {
  53212. wrapper = function onMessage(data, isBinary) {
  53213. const event = new MessageEvent('message', {
  53214. data: isBinary ? data : data.toString()
  53215. });
  53216. event[kTarget] = this;
  53217. callListener(handler, this, event);
  53218. };
  53219. } else if (type === 'close') {
  53220. wrapper = function onClose(code, message) {
  53221. const event = new CloseEvent('close', {
  53222. code,
  53223. reason: message.toString(),
  53224. wasClean: this._closeFrameReceived && this._closeFrameSent
  53225. });
  53226. event[kTarget] = this;
  53227. callListener(handler, this, event);
  53228. };
  53229. } else if (type === 'error') {
  53230. wrapper = function onError(error) {
  53231. const event = new ErrorEvent('error', {
  53232. error,
  53233. message: error.message
  53234. });
  53235. event[kTarget] = this;
  53236. callListener(handler, this, event);
  53237. };
  53238. } else if (type === 'open') {
  53239. wrapper = function onOpen() {
  53240. const event = new Event$1('open');
  53241. event[kTarget] = this;
  53242. callListener(handler, this, event);
  53243. };
  53244. } else {
  53245. return;
  53246. }
  53247. wrapper[kForOnEventAttribute$1] = !!options[kForOnEventAttribute$1];
  53248. wrapper[kListener$1] = handler;
  53249. if (options.once) {
  53250. this.once(type, wrapper);
  53251. } else {
  53252. this.on(type, wrapper);
  53253. }
  53254. },
  53255. /**
  53256. * Remove an event listener.
  53257. *
  53258. * @param {String} type A string representing the event type to remove
  53259. * @param {(Function|Object)} handler The listener to remove
  53260. * @public
  53261. */
  53262. removeEventListener(type, handler) {
  53263. for (const listener of this.listeners(type)) {
  53264. if (listener[kListener$1] === handler && !listener[kForOnEventAttribute$1]) {
  53265. this.removeListener(type, listener);
  53266. break;
  53267. }
  53268. }
  53269. }
  53270. };
  53271. var eventTarget = {
  53272. CloseEvent,
  53273. ErrorEvent,
  53274. Event: Event$1,
  53275. EventTarget,
  53276. MessageEvent
  53277. };
  53278. /**
  53279. * Call an event listener
  53280. *
  53281. * @param {(Function|Object)} listener The listener to call
  53282. * @param {*} thisArg The value to use as `this`` when calling the listener
  53283. * @param {Event} event The event to pass to the listener
  53284. * @private
  53285. */
  53286. function callListener(listener, thisArg, event) {
  53287. if (typeof listener === 'object' && listener.handleEvent) {
  53288. listener.handleEvent.call(listener, event);
  53289. } else {
  53290. listener.call(thisArg, event);
  53291. }
  53292. }
  53293. const { tokenChars: tokenChars$1 } = validationExports;
  53294. /**
  53295. * Adds an offer to the map of extension offers or a parameter to the map of
  53296. * parameters.
  53297. *
  53298. * @param {Object} dest The map of extension offers or parameters
  53299. * @param {String} name The extension or parameter name
  53300. * @param {(Object|Boolean|String)} elem The extension parameters or the
  53301. * parameter value
  53302. * @private
  53303. */
  53304. function push(dest, name, elem) {
  53305. if (dest[name] === undefined) dest[name] = [elem];
  53306. else dest[name].push(elem);
  53307. }
  53308. /**
  53309. * Parses the `Sec-WebSocket-Extensions` header into an object.
  53310. *
  53311. * @param {String} header The field value of the header
  53312. * @return {Object} The parsed object
  53313. * @public
  53314. */
  53315. function parse$2(header) {
  53316. const offers = Object.create(null);
  53317. let params = Object.create(null);
  53318. let mustUnescape = false;
  53319. let isEscaping = false;
  53320. let inQuotes = false;
  53321. let extensionName;
  53322. let paramName;
  53323. let start = -1;
  53324. let code = -1;
  53325. let end = -1;
  53326. let i = 0;
  53327. for (; i < header.length; i++) {
  53328. code = header.charCodeAt(i);
  53329. if (extensionName === undefined) {
  53330. if (end === -1 && tokenChars$1[code] === 1) {
  53331. if (start === -1) start = i;
  53332. } else if (
  53333. i !== 0 &&
  53334. (code === 0x20 /* ' ' */ || code === 0x09) /* '\t' */
  53335. ) {
  53336. if (end === -1 && start !== -1) end = i;
  53337. } else if (code === 0x3b /* ';' */ || code === 0x2c /* ',' */) {
  53338. if (start === -1) {
  53339. throw new SyntaxError(`Unexpected character at index ${i}`);
  53340. }
  53341. if (end === -1) end = i;
  53342. const name = header.slice(start, end);
  53343. if (code === 0x2c) {
  53344. push(offers, name, params);
  53345. params = Object.create(null);
  53346. } else {
  53347. extensionName = name;
  53348. }
  53349. start = end = -1;
  53350. } else {
  53351. throw new SyntaxError(`Unexpected character at index ${i}`);
  53352. }
  53353. } else if (paramName === undefined) {
  53354. if (end === -1 && tokenChars$1[code] === 1) {
  53355. if (start === -1) start = i;
  53356. } else if (code === 0x20 || code === 0x09) {
  53357. if (end === -1 && start !== -1) end = i;
  53358. } else if (code === 0x3b || code === 0x2c) {
  53359. if (start === -1) {
  53360. throw new SyntaxError(`Unexpected character at index ${i}`);
  53361. }
  53362. if (end === -1) end = i;
  53363. push(params, header.slice(start, end), true);
  53364. if (code === 0x2c) {
  53365. push(offers, extensionName, params);
  53366. params = Object.create(null);
  53367. extensionName = undefined;
  53368. }
  53369. start = end = -1;
  53370. } else if (code === 0x3d /* '=' */ && start !== -1 && end === -1) {
  53371. paramName = header.slice(start, i);
  53372. start = end = -1;
  53373. } else {
  53374. throw new SyntaxError(`Unexpected character at index ${i}`);
  53375. }
  53376. } else {
  53377. //
  53378. // The value of a quoted-string after unescaping must conform to the
  53379. // token ABNF, so only token characters are valid.
  53380. // Ref: https://tools.ietf.org/html/rfc6455#section-9.1
  53381. //
  53382. if (isEscaping) {
  53383. if (tokenChars$1[code] !== 1) {
  53384. throw new SyntaxError(`Unexpected character at index ${i}`);
  53385. }
  53386. if (start === -1) start = i;
  53387. else if (!mustUnescape) mustUnescape = true;
  53388. isEscaping = false;
  53389. } else if (inQuotes) {
  53390. if (tokenChars$1[code] === 1) {
  53391. if (start === -1) start = i;
  53392. } else if (code === 0x22 /* '"' */ && start !== -1) {
  53393. inQuotes = false;
  53394. end = i;
  53395. } else if (code === 0x5c /* '\' */) {
  53396. isEscaping = true;
  53397. } else {
  53398. throw new SyntaxError(`Unexpected character at index ${i}`);
  53399. }
  53400. } else if (code === 0x22 && header.charCodeAt(i - 1) === 0x3d) {
  53401. inQuotes = true;
  53402. } else if (end === -1 && tokenChars$1[code] === 1) {
  53403. if (start === -1) start = i;
  53404. } else if (start !== -1 && (code === 0x20 || code === 0x09)) {
  53405. if (end === -1) end = i;
  53406. } else if (code === 0x3b || code === 0x2c) {
  53407. if (start === -1) {
  53408. throw new SyntaxError(`Unexpected character at index ${i}`);
  53409. }
  53410. if (end === -1) end = i;
  53411. let value = header.slice(start, end);
  53412. if (mustUnescape) {
  53413. value = value.replace(/\\/g, '');
  53414. mustUnescape = false;
  53415. }
  53416. push(params, paramName, value);
  53417. if (code === 0x2c) {
  53418. push(offers, extensionName, params);
  53419. params = Object.create(null);
  53420. extensionName = undefined;
  53421. }
  53422. paramName = undefined;
  53423. start = end = -1;
  53424. } else {
  53425. throw new SyntaxError(`Unexpected character at index ${i}`);
  53426. }
  53427. }
  53428. }
  53429. if (start === -1 || inQuotes || code === 0x20 || code === 0x09) {
  53430. throw new SyntaxError('Unexpected end of input');
  53431. }
  53432. if (end === -1) end = i;
  53433. const token = header.slice(start, end);
  53434. if (extensionName === undefined) {
  53435. push(offers, token, params);
  53436. } else {
  53437. if (paramName === undefined) {
  53438. push(params, token, true);
  53439. } else if (mustUnescape) {
  53440. push(params, paramName, token.replace(/\\/g, ''));
  53441. } else {
  53442. push(params, paramName, token);
  53443. }
  53444. push(offers, extensionName, params);
  53445. }
  53446. return offers;
  53447. }
  53448. /**
  53449. * Builds the `Sec-WebSocket-Extensions` header field value.
  53450. *
  53451. * @param {Object} extensions The map of extensions and parameters to format
  53452. * @return {String} A string representing the given object
  53453. * @public
  53454. */
  53455. function format$1(extensions) {
  53456. return Object.keys(extensions)
  53457. .map((extension) => {
  53458. let configurations = extensions[extension];
  53459. if (!Array.isArray(configurations)) configurations = [configurations];
  53460. return configurations
  53461. .map((params) => {
  53462. return [extension]
  53463. .concat(
  53464. Object.keys(params).map((k) => {
  53465. let values = params[k];
  53466. if (!Array.isArray(values)) values = [values];
  53467. return values
  53468. .map((v) => (v === true ? k : `${k}=${v}`))
  53469. .join('; ');
  53470. })
  53471. )
  53472. .join('; ');
  53473. })
  53474. .join(', ');
  53475. })
  53476. .join(', ');
  53477. }
  53478. var extension$1 = { format: format$1, parse: parse$2 };
  53479. /* eslint no-unused-vars: ["error", { "varsIgnorePattern": "^Duplex|Readable$", "caughtErrors": "none" }] */
  53480. const EventEmitter$1 = require$$0$7;
  53481. const https$2 = require$$1$1;
  53482. const http$3 = require$$1;
  53483. const net = require$$4$1;
  53484. const tls = require$$4$2;
  53485. const { randomBytes, createHash: createHash$1 } = require$$3$1;
  53486. const { URL: URL$2 } = require$$0$9;
  53487. const PerMessageDeflate$1 = permessageDeflate;
  53488. const Receiver = receiver;
  53489. const Sender = sender;
  53490. const { isBlob } = validationExports;
  53491. const {
  53492. BINARY_TYPES,
  53493. EMPTY_BUFFER,
  53494. GUID: GUID$1,
  53495. kForOnEventAttribute,
  53496. kListener,
  53497. kStatusCode,
  53498. kWebSocket: kWebSocket$1,
  53499. NOOP: NOOP$1
  53500. } = constants;
  53501. const {
  53502. EventTarget: { addEventListener, removeEventListener }
  53503. } = eventTarget;
  53504. const { format, parse: parse$1 } = extension$1;
  53505. const { toBuffer } = bufferUtilExports;
  53506. const closeTimeout = 30 * 1000;
  53507. const kAborted = Symbol('kAborted');
  53508. const protocolVersions = [8, 13];
  53509. const readyStates = ['CONNECTING', 'OPEN', 'CLOSING', 'CLOSED'];
  53510. const subprotocolRegex = /^[!#$%&'*+\-.0-9A-Z^_`|a-z~]+$/;
  53511. /**
  53512. * Class representing a WebSocket.
  53513. *
  53514. * @extends EventEmitter
  53515. */
  53516. let WebSocket$1 = class WebSocket extends EventEmitter$1 {
  53517. /**
  53518. * Create a new `WebSocket`.
  53519. *
  53520. * @param {(String|URL)} address The URL to which to connect
  53521. * @param {(String|String[])} [protocols] The subprotocols
  53522. * @param {Object} [options] Connection options
  53523. */
  53524. constructor(address, protocols, options) {
  53525. super();
  53526. this._binaryType = BINARY_TYPES[0];
  53527. this._closeCode = 1006;
  53528. this._closeFrameReceived = false;
  53529. this._closeFrameSent = false;
  53530. this._closeMessage = EMPTY_BUFFER;
  53531. this._closeTimer = null;
  53532. this._errorEmitted = false;
  53533. this._extensions = {};
  53534. this._paused = false;
  53535. this._protocol = '';
  53536. this._readyState = WebSocket.CONNECTING;
  53537. this._receiver = null;
  53538. this._sender = null;
  53539. this._socket = null;
  53540. if (address !== null) {
  53541. this._bufferedAmount = 0;
  53542. this._isServer = false;
  53543. this._redirects = 0;
  53544. if (protocols === undefined) {
  53545. protocols = [];
  53546. } else if (!Array.isArray(protocols)) {
  53547. if (typeof protocols === 'object' && protocols !== null) {
  53548. options = protocols;
  53549. protocols = [];
  53550. } else {
  53551. protocols = [protocols];
  53552. }
  53553. }
  53554. initAsClient(this, address, protocols, options);
  53555. } else {
  53556. this._autoPong = options.autoPong;
  53557. this._isServer = true;
  53558. }
  53559. }
  53560. /**
  53561. * For historical reasons, the custom "nodebuffer" type is used by the default
  53562. * instead of "blob".
  53563. *
  53564. * @type {String}
  53565. */
  53566. get binaryType() {
  53567. return this._binaryType;
  53568. }
  53569. set binaryType(type) {
  53570. if (!BINARY_TYPES.includes(type)) return;
  53571. this._binaryType = type;
  53572. //
  53573. // Allow to change `binaryType` on the fly.
  53574. //
  53575. if (this._receiver) this._receiver._binaryType = type;
  53576. }
  53577. /**
  53578. * @type {Number}
  53579. */
  53580. get bufferedAmount() {
  53581. if (!this._socket) return this._bufferedAmount;
  53582. return this._socket._writableState.length + this._sender._bufferedBytes;
  53583. }
  53584. /**
  53585. * @type {String}
  53586. */
  53587. get extensions() {
  53588. return Object.keys(this._extensions).join();
  53589. }
  53590. /**
  53591. * @type {Boolean}
  53592. */
  53593. get isPaused() {
  53594. return this._paused;
  53595. }
  53596. /**
  53597. * @type {Function}
  53598. */
  53599. /* istanbul ignore next */
  53600. get onclose() {
  53601. return null;
  53602. }
  53603. /**
  53604. * @type {Function}
  53605. */
  53606. /* istanbul ignore next */
  53607. get onerror() {
  53608. return null;
  53609. }
  53610. /**
  53611. * @type {Function}
  53612. */
  53613. /* istanbul ignore next */
  53614. get onopen() {
  53615. return null;
  53616. }
  53617. /**
  53618. * @type {Function}
  53619. */
  53620. /* istanbul ignore next */
  53621. get onmessage() {
  53622. return null;
  53623. }
  53624. /**
  53625. * @type {String}
  53626. */
  53627. get protocol() {
  53628. return this._protocol;
  53629. }
  53630. /**
  53631. * @type {Number}
  53632. */
  53633. get readyState() {
  53634. return this._readyState;
  53635. }
  53636. /**
  53637. * @type {String}
  53638. */
  53639. get url() {
  53640. return this._url;
  53641. }
  53642. /**
  53643. * Set up the socket and the internal resources.
  53644. *
  53645. * @param {Duplex} socket The network socket between the server and client
  53646. * @param {Buffer} head The first packet of the upgraded stream
  53647. * @param {Object} options Options object
  53648. * @param {Boolean} [options.allowSynchronousEvents=false] Specifies whether
  53649. * any of the `'message'`, `'ping'`, and `'pong'` events can be emitted
  53650. * multiple times in the same tick
  53651. * @param {Function} [options.generateMask] The function used to generate the
  53652. * masking key
  53653. * @param {Number} [options.maxPayload=0] The maximum allowed message size
  53654. * @param {Boolean} [options.skipUTF8Validation=false] Specifies whether or
  53655. * not to skip UTF-8 validation for text and close messages
  53656. * @private
  53657. */
  53658. setSocket(socket, head, options) {
  53659. const receiver = new Receiver({
  53660. allowSynchronousEvents: options.allowSynchronousEvents,
  53661. binaryType: this.binaryType,
  53662. extensions: this._extensions,
  53663. isServer: this._isServer,
  53664. maxPayload: options.maxPayload,
  53665. skipUTF8Validation: options.skipUTF8Validation
  53666. });
  53667. const sender = new Sender(socket, this._extensions, options.generateMask);
  53668. this._receiver = receiver;
  53669. this._sender = sender;
  53670. this._socket = socket;
  53671. receiver[kWebSocket$1] = this;
  53672. sender[kWebSocket$1] = this;
  53673. socket[kWebSocket$1] = this;
  53674. receiver.on('conclude', receiverOnConclude);
  53675. receiver.on('drain', receiverOnDrain);
  53676. receiver.on('error', receiverOnError);
  53677. receiver.on('message', receiverOnMessage);
  53678. receiver.on('ping', receiverOnPing);
  53679. receiver.on('pong', receiverOnPong);
  53680. sender.onerror = senderOnError;
  53681. //
  53682. // These methods may not be available if `socket` is just a `Duplex`.
  53683. //
  53684. if (socket.setTimeout) socket.setTimeout(0);
  53685. if (socket.setNoDelay) socket.setNoDelay();
  53686. if (head.length > 0) socket.unshift(head);
  53687. socket.on('close', socketOnClose);
  53688. socket.on('data', socketOnData);
  53689. socket.on('end', socketOnEnd);
  53690. socket.on('error', socketOnError$1);
  53691. this._readyState = WebSocket.OPEN;
  53692. this.emit('open');
  53693. }
  53694. /**
  53695. * Emit the `'close'` event.
  53696. *
  53697. * @private
  53698. */
  53699. emitClose() {
  53700. if (!this._socket) {
  53701. this._readyState = WebSocket.CLOSED;
  53702. this.emit('close', this._closeCode, this._closeMessage);
  53703. return;
  53704. }
  53705. if (this._extensions[PerMessageDeflate$1.extensionName]) {
  53706. this._extensions[PerMessageDeflate$1.extensionName].cleanup();
  53707. }
  53708. this._receiver.removeAllListeners();
  53709. this._readyState = WebSocket.CLOSED;
  53710. this.emit('close', this._closeCode, this._closeMessage);
  53711. }
  53712. /**
  53713. * Start a closing handshake.
  53714. *
  53715. * +----------+ +-----------+ +----------+
  53716. * - - -|ws.close()|-->|close frame|-->|ws.close()|- - -
  53717. * | +----------+ +-----------+ +----------+ |
  53718. * +----------+ +-----------+ |
  53719. * CLOSING |ws.close()|<--|close frame|<--+-----+ CLOSING
  53720. * +----------+ +-----------+ |
  53721. * | | | +---+ |
  53722. * +------------------------+-->|fin| - - - -
  53723. * | +---+ | +---+
  53724. * - - - - -|fin|<---------------------+
  53725. * +---+
  53726. *
  53727. * @param {Number} [code] Status code explaining why the connection is closing
  53728. * @param {(String|Buffer)} [data] The reason why the connection is
  53729. * closing
  53730. * @public
  53731. */
  53732. close(code, data) {
  53733. if (this.readyState === WebSocket.CLOSED) return;
  53734. if (this.readyState === WebSocket.CONNECTING) {
  53735. const msg = 'WebSocket was closed before the connection was established';
  53736. abortHandshake$1(this, this._req, msg);
  53737. return;
  53738. }
  53739. if (this.readyState === WebSocket.CLOSING) {
  53740. if (
  53741. this._closeFrameSent &&
  53742. (this._closeFrameReceived || this._receiver._writableState.errorEmitted)
  53743. ) {
  53744. this._socket.end();
  53745. }
  53746. return;
  53747. }
  53748. this._readyState = WebSocket.CLOSING;
  53749. this._sender.close(code, data, !this._isServer, (err) => {
  53750. //
  53751. // This error is handled by the `'error'` listener on the socket. We only
  53752. // want to know if the close frame has been sent here.
  53753. //
  53754. if (err) return;
  53755. this._closeFrameSent = true;
  53756. if (
  53757. this._closeFrameReceived ||
  53758. this._receiver._writableState.errorEmitted
  53759. ) {
  53760. this._socket.end();
  53761. }
  53762. });
  53763. setCloseTimer(this);
  53764. }
  53765. /**
  53766. * Pause the socket.
  53767. *
  53768. * @public
  53769. */
  53770. pause() {
  53771. if (
  53772. this.readyState === WebSocket.CONNECTING ||
  53773. this.readyState === WebSocket.CLOSED
  53774. ) {
  53775. return;
  53776. }
  53777. this._paused = true;
  53778. this._socket.pause();
  53779. }
  53780. /**
  53781. * Send a ping.
  53782. *
  53783. * @param {*} [data] The data to send
  53784. * @param {Boolean} [mask] Indicates whether or not to mask `data`
  53785. * @param {Function} [cb] Callback which is executed when the ping is sent
  53786. * @public
  53787. */
  53788. ping(data, mask, cb) {
  53789. if (this.readyState === WebSocket.CONNECTING) {
  53790. throw new Error('WebSocket is not open: readyState 0 (CONNECTING)');
  53791. }
  53792. if (typeof data === 'function') {
  53793. cb = data;
  53794. data = mask = undefined;
  53795. } else if (typeof mask === 'function') {
  53796. cb = mask;
  53797. mask = undefined;
  53798. }
  53799. if (typeof data === 'number') data = data.toString();
  53800. if (this.readyState !== WebSocket.OPEN) {
  53801. sendAfterClose(this, data, cb);
  53802. return;
  53803. }
  53804. if (mask === undefined) mask = !this._isServer;
  53805. this._sender.ping(data || EMPTY_BUFFER, mask, cb);
  53806. }
  53807. /**
  53808. * Send a pong.
  53809. *
  53810. * @param {*} [data] The data to send
  53811. * @param {Boolean} [mask] Indicates whether or not to mask `data`
  53812. * @param {Function} [cb] Callback which is executed when the pong is sent
  53813. * @public
  53814. */
  53815. pong(data, mask, cb) {
  53816. if (this.readyState === WebSocket.CONNECTING) {
  53817. throw new Error('WebSocket is not open: readyState 0 (CONNECTING)');
  53818. }
  53819. if (typeof data === 'function') {
  53820. cb = data;
  53821. data = mask = undefined;
  53822. } else if (typeof mask === 'function') {
  53823. cb = mask;
  53824. mask = undefined;
  53825. }
  53826. if (typeof data === 'number') data = data.toString();
  53827. if (this.readyState !== WebSocket.OPEN) {
  53828. sendAfterClose(this, data, cb);
  53829. return;
  53830. }
  53831. if (mask === undefined) mask = !this._isServer;
  53832. this._sender.pong(data || EMPTY_BUFFER, mask, cb);
  53833. }
  53834. /**
  53835. * Resume the socket.
  53836. *
  53837. * @public
  53838. */
  53839. resume() {
  53840. if (
  53841. this.readyState === WebSocket.CONNECTING ||
  53842. this.readyState === WebSocket.CLOSED
  53843. ) {
  53844. return;
  53845. }
  53846. this._paused = false;
  53847. if (!this._receiver._writableState.needDrain) this._socket.resume();
  53848. }
  53849. /**
  53850. * Send a data message.
  53851. *
  53852. * @param {*} data The message to send
  53853. * @param {Object} [options] Options object
  53854. * @param {Boolean} [options.binary] Specifies whether `data` is binary or
  53855. * text
  53856. * @param {Boolean} [options.compress] Specifies whether or not to compress
  53857. * `data`
  53858. * @param {Boolean} [options.fin=true] Specifies whether the fragment is the
  53859. * last one
  53860. * @param {Boolean} [options.mask] Specifies whether or not to mask `data`
  53861. * @param {Function} [cb] Callback which is executed when data is written out
  53862. * @public
  53863. */
  53864. send(data, options, cb) {
  53865. if (this.readyState === WebSocket.CONNECTING) {
  53866. throw new Error('WebSocket is not open: readyState 0 (CONNECTING)');
  53867. }
  53868. if (typeof options === 'function') {
  53869. cb = options;
  53870. options = {};
  53871. }
  53872. if (typeof data === 'number') data = data.toString();
  53873. if (this.readyState !== WebSocket.OPEN) {
  53874. sendAfterClose(this, data, cb);
  53875. return;
  53876. }
  53877. const opts = {
  53878. binary: typeof data !== 'string',
  53879. mask: !this._isServer,
  53880. compress: true,
  53881. fin: true,
  53882. ...options
  53883. };
  53884. if (!this._extensions[PerMessageDeflate$1.extensionName]) {
  53885. opts.compress = false;
  53886. }
  53887. this._sender.send(data || EMPTY_BUFFER, opts, cb);
  53888. }
  53889. /**
  53890. * Forcibly close the connection.
  53891. *
  53892. * @public
  53893. */
  53894. terminate() {
  53895. if (this.readyState === WebSocket.CLOSED) return;
  53896. if (this.readyState === WebSocket.CONNECTING) {
  53897. const msg = 'WebSocket was closed before the connection was established';
  53898. abortHandshake$1(this, this._req, msg);
  53899. return;
  53900. }
  53901. if (this._socket) {
  53902. this._readyState = WebSocket.CLOSING;
  53903. this._socket.destroy();
  53904. }
  53905. }
  53906. };
  53907. /**
  53908. * @constant {Number} CONNECTING
  53909. * @memberof WebSocket
  53910. */
  53911. Object.defineProperty(WebSocket$1, 'CONNECTING', {
  53912. enumerable: true,
  53913. value: readyStates.indexOf('CONNECTING')
  53914. });
  53915. /**
  53916. * @constant {Number} CONNECTING
  53917. * @memberof WebSocket.prototype
  53918. */
  53919. Object.defineProperty(WebSocket$1.prototype, 'CONNECTING', {
  53920. enumerable: true,
  53921. value: readyStates.indexOf('CONNECTING')
  53922. });
  53923. /**
  53924. * @constant {Number} OPEN
  53925. * @memberof WebSocket
  53926. */
  53927. Object.defineProperty(WebSocket$1, 'OPEN', {
  53928. enumerable: true,
  53929. value: readyStates.indexOf('OPEN')
  53930. });
  53931. /**
  53932. * @constant {Number} OPEN
  53933. * @memberof WebSocket.prototype
  53934. */
  53935. Object.defineProperty(WebSocket$1.prototype, 'OPEN', {
  53936. enumerable: true,
  53937. value: readyStates.indexOf('OPEN')
  53938. });
  53939. /**
  53940. * @constant {Number} CLOSING
  53941. * @memberof WebSocket
  53942. */
  53943. Object.defineProperty(WebSocket$1, 'CLOSING', {
  53944. enumerable: true,
  53945. value: readyStates.indexOf('CLOSING')
  53946. });
  53947. /**
  53948. * @constant {Number} CLOSING
  53949. * @memberof WebSocket.prototype
  53950. */
  53951. Object.defineProperty(WebSocket$1.prototype, 'CLOSING', {
  53952. enumerable: true,
  53953. value: readyStates.indexOf('CLOSING')
  53954. });
  53955. /**
  53956. * @constant {Number} CLOSED
  53957. * @memberof WebSocket
  53958. */
  53959. Object.defineProperty(WebSocket$1, 'CLOSED', {
  53960. enumerable: true,
  53961. value: readyStates.indexOf('CLOSED')
  53962. });
  53963. /**
  53964. * @constant {Number} CLOSED
  53965. * @memberof WebSocket.prototype
  53966. */
  53967. Object.defineProperty(WebSocket$1.prototype, 'CLOSED', {
  53968. enumerable: true,
  53969. value: readyStates.indexOf('CLOSED')
  53970. });
  53971. [
  53972. 'binaryType',
  53973. 'bufferedAmount',
  53974. 'extensions',
  53975. 'isPaused',
  53976. 'protocol',
  53977. 'readyState',
  53978. 'url'
  53979. ].forEach((property) => {
  53980. Object.defineProperty(WebSocket$1.prototype, property, { enumerable: true });
  53981. });
  53982. //
  53983. // Add the `onopen`, `onerror`, `onclose`, and `onmessage` attributes.
  53984. // See https://html.spec.whatwg.org/multipage/comms.html#the-websocket-interface
  53985. //
  53986. ['open', 'error', 'close', 'message'].forEach((method) => {
  53987. Object.defineProperty(WebSocket$1.prototype, `on${method}`, {
  53988. enumerable: true,
  53989. get() {
  53990. for (const listener of this.listeners(method)) {
  53991. if (listener[kForOnEventAttribute]) return listener[kListener];
  53992. }
  53993. return null;
  53994. },
  53995. set(handler) {
  53996. for (const listener of this.listeners(method)) {
  53997. if (listener[kForOnEventAttribute]) {
  53998. this.removeListener(method, listener);
  53999. break;
  54000. }
  54001. }
  54002. if (typeof handler !== 'function') return;
  54003. this.addEventListener(method, handler, {
  54004. [kForOnEventAttribute]: true
  54005. });
  54006. }
  54007. });
  54008. });
  54009. WebSocket$1.prototype.addEventListener = addEventListener;
  54010. WebSocket$1.prototype.removeEventListener = removeEventListener;
  54011. var websocket = WebSocket$1;
  54012. /**
  54013. * Initialize a WebSocket client.
  54014. *
  54015. * @param {WebSocket} websocket The client to initialize
  54016. * @param {(String|URL)} address The URL to which to connect
  54017. * @param {Array} protocols The subprotocols
  54018. * @param {Object} [options] Connection options
  54019. * @param {Boolean} [options.allowSynchronousEvents=true] Specifies whether any
  54020. * of the `'message'`, `'ping'`, and `'pong'` events can be emitted multiple
  54021. * times in the same tick
  54022. * @param {Boolean} [options.autoPong=true] Specifies whether or not to
  54023. * automatically send a pong in response to a ping
  54024. * @param {Function} [options.finishRequest] A function which can be used to
  54025. * customize the headers of each http request before it is sent
  54026. * @param {Boolean} [options.followRedirects=false] Whether or not to follow
  54027. * redirects
  54028. * @param {Function} [options.generateMask] The function used to generate the
  54029. * masking key
  54030. * @param {Number} [options.handshakeTimeout] Timeout in milliseconds for the
  54031. * handshake request
  54032. * @param {Number} [options.maxPayload=104857600] The maximum allowed message
  54033. * size
  54034. * @param {Number} [options.maxRedirects=10] The maximum number of redirects
  54035. * allowed
  54036. * @param {String} [options.origin] Value of the `Origin` or
  54037. * `Sec-WebSocket-Origin` header
  54038. * @param {(Boolean|Object)} [options.perMessageDeflate=true] Enable/disable
  54039. * permessage-deflate
  54040. * @param {Number} [options.protocolVersion=13] Value of the
  54041. * `Sec-WebSocket-Version` header
  54042. * @param {Boolean} [options.skipUTF8Validation=false] Specifies whether or
  54043. * not to skip UTF-8 validation for text and close messages
  54044. * @private
  54045. */
  54046. function initAsClient(websocket, address, protocols, options) {
  54047. const opts = {
  54048. allowSynchronousEvents: true,
  54049. autoPong: true,
  54050. protocolVersion: protocolVersions[1],
  54051. maxPayload: 100 * 1024 * 1024,
  54052. skipUTF8Validation: false,
  54053. perMessageDeflate: true,
  54054. followRedirects: false,
  54055. maxRedirects: 10,
  54056. ...options,
  54057. socketPath: undefined,
  54058. hostname: undefined,
  54059. protocol: undefined,
  54060. timeout: undefined,
  54061. method: 'GET',
  54062. host: undefined,
  54063. path: undefined,
  54064. port: undefined
  54065. };
  54066. websocket._autoPong = opts.autoPong;
  54067. if (!protocolVersions.includes(opts.protocolVersion)) {
  54068. throw new RangeError(
  54069. `Unsupported protocol version: ${opts.protocolVersion} ` +
  54070. `(supported versions: ${protocolVersions.join(', ')})`
  54071. );
  54072. }
  54073. let parsedUrl;
  54074. if (address instanceof URL$2) {
  54075. parsedUrl = address;
  54076. } else {
  54077. try {
  54078. parsedUrl = new URL$2(address);
  54079. } catch (e) {
  54080. throw new SyntaxError(`Invalid URL: ${address}`);
  54081. }
  54082. }
  54083. if (parsedUrl.protocol === 'http:') {
  54084. parsedUrl.protocol = 'ws:';
  54085. } else if (parsedUrl.protocol === 'https:') {
  54086. parsedUrl.protocol = 'wss:';
  54087. }
  54088. websocket._url = parsedUrl.href;
  54089. const isSecure = parsedUrl.protocol === 'wss:';
  54090. const isIpcUrl = parsedUrl.protocol === 'ws+unix:';
  54091. let invalidUrlMessage;
  54092. if (parsedUrl.protocol !== 'ws:' && !isSecure && !isIpcUrl) {
  54093. invalidUrlMessage =
  54094. 'The URL\'s protocol must be one of "ws:", "wss:", ' +
  54095. '"http:", "https", or "ws+unix:"';
  54096. } else if (isIpcUrl && !parsedUrl.pathname) {
  54097. invalidUrlMessage = "The URL's pathname is empty";
  54098. } else if (parsedUrl.hash) {
  54099. invalidUrlMessage = 'The URL contains a fragment identifier';
  54100. }
  54101. if (invalidUrlMessage) {
  54102. const err = new SyntaxError(invalidUrlMessage);
  54103. if (websocket._redirects === 0) {
  54104. throw err;
  54105. } else {
  54106. emitErrorAndClose(websocket, err);
  54107. return;
  54108. }
  54109. }
  54110. const defaultPort = isSecure ? 443 : 80;
  54111. const key = randomBytes(16).toString('base64');
  54112. const request = isSecure ? https$2.request : http$3.request;
  54113. const protocolSet = new Set();
  54114. let perMessageDeflate;
  54115. opts.createConnection =
  54116. opts.createConnection || (isSecure ? tlsConnect : netConnect);
  54117. opts.defaultPort = opts.defaultPort || defaultPort;
  54118. opts.port = parsedUrl.port || defaultPort;
  54119. opts.host = parsedUrl.hostname.startsWith('[')
  54120. ? parsedUrl.hostname.slice(1, -1)
  54121. : parsedUrl.hostname;
  54122. opts.headers = {
  54123. ...opts.headers,
  54124. 'Sec-WebSocket-Version': opts.protocolVersion,
  54125. 'Sec-WebSocket-Key': key,
  54126. Connection: 'Upgrade',
  54127. Upgrade: 'websocket'
  54128. };
  54129. opts.path = parsedUrl.pathname + parsedUrl.search;
  54130. opts.timeout = opts.handshakeTimeout;
  54131. if (opts.perMessageDeflate) {
  54132. perMessageDeflate = new PerMessageDeflate$1(
  54133. opts.perMessageDeflate !== true ? opts.perMessageDeflate : {},
  54134. false,
  54135. opts.maxPayload
  54136. );
  54137. opts.headers['Sec-WebSocket-Extensions'] = format({
  54138. [PerMessageDeflate$1.extensionName]: perMessageDeflate.offer()
  54139. });
  54140. }
  54141. if (protocols.length) {
  54142. for (const protocol of protocols) {
  54143. if (
  54144. typeof protocol !== 'string' ||
  54145. !subprotocolRegex.test(protocol) ||
  54146. protocolSet.has(protocol)
  54147. ) {
  54148. throw new SyntaxError(
  54149. 'An invalid or duplicated subprotocol was specified'
  54150. );
  54151. }
  54152. protocolSet.add(protocol);
  54153. }
  54154. opts.headers['Sec-WebSocket-Protocol'] = protocols.join(',');
  54155. }
  54156. if (opts.origin) {
  54157. if (opts.protocolVersion < 13) {
  54158. opts.headers['Sec-WebSocket-Origin'] = opts.origin;
  54159. } else {
  54160. opts.headers.Origin = opts.origin;
  54161. }
  54162. }
  54163. if (parsedUrl.username || parsedUrl.password) {
  54164. opts.auth = `${parsedUrl.username}:${parsedUrl.password}`;
  54165. }
  54166. if (isIpcUrl) {
  54167. const parts = opts.path.split(':');
  54168. opts.socketPath = parts[0];
  54169. opts.path = parts[1];
  54170. }
  54171. let req;
  54172. if (opts.followRedirects) {
  54173. if (websocket._redirects === 0) {
  54174. websocket._originalIpc = isIpcUrl;
  54175. websocket._originalSecure = isSecure;
  54176. websocket._originalHostOrSocketPath = isIpcUrl
  54177. ? opts.socketPath
  54178. : parsedUrl.host;
  54179. const headers = options && options.headers;
  54180. //
  54181. // Shallow copy the user provided options so that headers can be changed
  54182. // without mutating the original object.
  54183. //
  54184. options = { ...options, headers: {} };
  54185. if (headers) {
  54186. for (const [key, value] of Object.entries(headers)) {
  54187. options.headers[key.toLowerCase()] = value;
  54188. }
  54189. }
  54190. } else if (websocket.listenerCount('redirect') === 0) {
  54191. const isSameHost = isIpcUrl
  54192. ? websocket._originalIpc
  54193. ? opts.socketPath === websocket._originalHostOrSocketPath
  54194. : false
  54195. : websocket._originalIpc
  54196. ? false
  54197. : parsedUrl.host === websocket._originalHostOrSocketPath;
  54198. if (!isSameHost || (websocket._originalSecure && !isSecure)) {
  54199. //
  54200. // Match curl 7.77.0 behavior and drop the following headers. These
  54201. // headers are also dropped when following a redirect to a subdomain.
  54202. //
  54203. delete opts.headers.authorization;
  54204. delete opts.headers.cookie;
  54205. if (!isSameHost) delete opts.headers.host;
  54206. opts.auth = undefined;
  54207. }
  54208. }
  54209. //
  54210. // Match curl 7.77.0 behavior and make the first `Authorization` header win.
  54211. // If the `Authorization` header is set, then there is nothing to do as it
  54212. // will take precedence.
  54213. //
  54214. if (opts.auth && !options.headers.authorization) {
  54215. options.headers.authorization =
  54216. 'Basic ' + Buffer.from(opts.auth).toString('base64');
  54217. }
  54218. req = websocket._req = request(opts);
  54219. if (websocket._redirects) {
  54220. //
  54221. // Unlike what is done for the `'upgrade'` event, no early exit is
  54222. // triggered here if the user calls `websocket.close()` or
  54223. // `websocket.terminate()` from a listener of the `'redirect'` event. This
  54224. // is because the user can also call `request.destroy()` with an error
  54225. // before calling `websocket.close()` or `websocket.terminate()` and this
  54226. // would result in an error being emitted on the `request` object with no
  54227. // `'error'` event listeners attached.
  54228. //
  54229. websocket.emit('redirect', websocket.url, req);
  54230. }
  54231. } else {
  54232. req = websocket._req = request(opts);
  54233. }
  54234. if (opts.timeout) {
  54235. req.on('timeout', () => {
  54236. abortHandshake$1(websocket, req, 'Opening handshake has timed out');
  54237. });
  54238. }
  54239. req.on('error', (err) => {
  54240. if (req === null || req[kAborted]) return;
  54241. req = websocket._req = null;
  54242. emitErrorAndClose(websocket, err);
  54243. });
  54244. req.on('response', (res) => {
  54245. const location = res.headers.location;
  54246. const statusCode = res.statusCode;
  54247. if (
  54248. location &&
  54249. opts.followRedirects &&
  54250. statusCode >= 300 &&
  54251. statusCode < 400
  54252. ) {
  54253. if (++websocket._redirects > opts.maxRedirects) {
  54254. abortHandshake$1(websocket, req, 'Maximum redirects exceeded');
  54255. return;
  54256. }
  54257. req.abort();
  54258. let addr;
  54259. try {
  54260. addr = new URL$2(location, address);
  54261. } catch (e) {
  54262. const err = new SyntaxError(`Invalid URL: ${location}`);
  54263. emitErrorAndClose(websocket, err);
  54264. return;
  54265. }
  54266. initAsClient(websocket, addr, protocols, options);
  54267. } else if (!websocket.emit('unexpected-response', req, res)) {
  54268. abortHandshake$1(
  54269. websocket,
  54270. req,
  54271. `Unexpected server response: ${res.statusCode}`
  54272. );
  54273. }
  54274. });
  54275. req.on('upgrade', (res, socket, head) => {
  54276. websocket.emit('upgrade', res);
  54277. //
  54278. // The user may have closed the connection from a listener of the
  54279. // `'upgrade'` event.
  54280. //
  54281. if (websocket.readyState !== WebSocket$1.CONNECTING) return;
  54282. req = websocket._req = null;
  54283. const upgrade = res.headers.upgrade;
  54284. if (upgrade === undefined || upgrade.toLowerCase() !== 'websocket') {
  54285. abortHandshake$1(websocket, socket, 'Invalid Upgrade header');
  54286. return;
  54287. }
  54288. const digest = createHash$1('sha1')
  54289. .update(key + GUID$1)
  54290. .digest('base64');
  54291. if (res.headers['sec-websocket-accept'] !== digest) {
  54292. abortHandshake$1(websocket, socket, 'Invalid Sec-WebSocket-Accept header');
  54293. return;
  54294. }
  54295. const serverProt = res.headers['sec-websocket-protocol'];
  54296. let protError;
  54297. if (serverProt !== undefined) {
  54298. if (!protocolSet.size) {
  54299. protError = 'Server sent a subprotocol but none was requested';
  54300. } else if (!protocolSet.has(serverProt)) {
  54301. protError = 'Server sent an invalid subprotocol';
  54302. }
  54303. } else if (protocolSet.size) {
  54304. protError = 'Server sent no subprotocol';
  54305. }
  54306. if (protError) {
  54307. abortHandshake$1(websocket, socket, protError);
  54308. return;
  54309. }
  54310. if (serverProt) websocket._protocol = serverProt;
  54311. const secWebSocketExtensions = res.headers['sec-websocket-extensions'];
  54312. if (secWebSocketExtensions !== undefined) {
  54313. if (!perMessageDeflate) {
  54314. const message =
  54315. 'Server sent a Sec-WebSocket-Extensions header but no extension ' +
  54316. 'was requested';
  54317. abortHandshake$1(websocket, socket, message);
  54318. return;
  54319. }
  54320. let extensions;
  54321. try {
  54322. extensions = parse$1(secWebSocketExtensions);
  54323. } catch (err) {
  54324. const message = 'Invalid Sec-WebSocket-Extensions header';
  54325. abortHandshake$1(websocket, socket, message);
  54326. return;
  54327. }
  54328. const extensionNames = Object.keys(extensions);
  54329. if (
  54330. extensionNames.length !== 1 ||
  54331. extensionNames[0] !== PerMessageDeflate$1.extensionName
  54332. ) {
  54333. const message = 'Server indicated an extension that was not requested';
  54334. abortHandshake$1(websocket, socket, message);
  54335. return;
  54336. }
  54337. try {
  54338. perMessageDeflate.accept(extensions[PerMessageDeflate$1.extensionName]);
  54339. } catch (err) {
  54340. const message = 'Invalid Sec-WebSocket-Extensions header';
  54341. abortHandshake$1(websocket, socket, message);
  54342. return;
  54343. }
  54344. websocket._extensions[PerMessageDeflate$1.extensionName] =
  54345. perMessageDeflate;
  54346. }
  54347. websocket.setSocket(socket, head, {
  54348. allowSynchronousEvents: opts.allowSynchronousEvents,
  54349. generateMask: opts.generateMask,
  54350. maxPayload: opts.maxPayload,
  54351. skipUTF8Validation: opts.skipUTF8Validation
  54352. });
  54353. });
  54354. if (opts.finishRequest) {
  54355. opts.finishRequest(req, websocket);
  54356. } else {
  54357. req.end();
  54358. }
  54359. }
  54360. /**
  54361. * Emit the `'error'` and `'close'` events.
  54362. *
  54363. * @param {WebSocket} websocket The WebSocket instance
  54364. * @param {Error} The error to emit
  54365. * @private
  54366. */
  54367. function emitErrorAndClose(websocket, err) {
  54368. websocket._readyState = WebSocket$1.CLOSING;
  54369. //
  54370. // The following assignment is practically useless and is done only for
  54371. // consistency.
  54372. //
  54373. websocket._errorEmitted = true;
  54374. websocket.emit('error', err);
  54375. websocket.emitClose();
  54376. }
  54377. /**
  54378. * Create a `net.Socket` and initiate a connection.
  54379. *
  54380. * @param {Object} options Connection options
  54381. * @return {net.Socket} The newly created socket used to start the connection
  54382. * @private
  54383. */
  54384. function netConnect(options) {
  54385. options.path = options.socketPath;
  54386. return net.connect(options);
  54387. }
  54388. /**
  54389. * Create a `tls.TLSSocket` and initiate a connection.
  54390. *
  54391. * @param {Object} options Connection options
  54392. * @return {tls.TLSSocket} The newly created socket used to start the connection
  54393. * @private
  54394. */
  54395. function tlsConnect(options) {
  54396. options.path = undefined;
  54397. if (!options.servername && options.servername !== '') {
  54398. options.servername = net.isIP(options.host) ? '' : options.host;
  54399. }
  54400. return tls.connect(options);
  54401. }
  54402. /**
  54403. * Abort the handshake and emit an error.
  54404. *
  54405. * @param {WebSocket} websocket The WebSocket instance
  54406. * @param {(http.ClientRequest|net.Socket|tls.Socket)} stream The request to
  54407. * abort or the socket to destroy
  54408. * @param {String} message The error message
  54409. * @private
  54410. */
  54411. function abortHandshake$1(websocket, stream, message) {
  54412. websocket._readyState = WebSocket$1.CLOSING;
  54413. const err = new Error(message);
  54414. Error.captureStackTrace(err, abortHandshake$1);
  54415. if (stream.setHeader) {
  54416. stream[kAborted] = true;
  54417. stream.abort();
  54418. if (stream.socket && !stream.socket.destroyed) {
  54419. //
  54420. // On Node.js >= 14.3.0 `request.abort()` does not destroy the socket if
  54421. // called after the request completed. See
  54422. // https://github.com/websockets/ws/issues/1869.
  54423. //
  54424. stream.socket.destroy();
  54425. }
  54426. process.nextTick(emitErrorAndClose, websocket, err);
  54427. } else {
  54428. stream.destroy(err);
  54429. stream.once('error', websocket.emit.bind(websocket, 'error'));
  54430. stream.once('close', websocket.emitClose.bind(websocket));
  54431. }
  54432. }
  54433. /**
  54434. * Handle cases where the `ping()`, `pong()`, or `send()` methods are called
  54435. * when the `readyState` attribute is `CLOSING` or `CLOSED`.
  54436. *
  54437. * @param {WebSocket} websocket The WebSocket instance
  54438. * @param {*} [data] The data to send
  54439. * @param {Function} [cb] Callback
  54440. * @private
  54441. */
  54442. function sendAfterClose(websocket, data, cb) {
  54443. if (data) {
  54444. const length = isBlob(data) ? data.size : toBuffer(data).length;
  54445. //
  54446. // The `_bufferedAmount` property is used only when the peer is a client and
  54447. // the opening handshake fails. Under these circumstances, in fact, the
  54448. // `setSocket()` method is not called, so the `_socket` and `_sender`
  54449. // properties are set to `null`.
  54450. //
  54451. if (websocket._socket) websocket._sender._bufferedBytes += length;
  54452. else websocket._bufferedAmount += length;
  54453. }
  54454. if (cb) {
  54455. const err = new Error(
  54456. `WebSocket is not open: readyState ${websocket.readyState} ` +
  54457. `(${readyStates[websocket.readyState]})`
  54458. );
  54459. process.nextTick(cb, err);
  54460. }
  54461. }
  54462. /**
  54463. * The listener of the `Receiver` `'conclude'` event.
  54464. *
  54465. * @param {Number} code The status code
  54466. * @param {Buffer} reason The reason for closing
  54467. * @private
  54468. */
  54469. function receiverOnConclude(code, reason) {
  54470. const websocket = this[kWebSocket$1];
  54471. websocket._closeFrameReceived = true;
  54472. websocket._closeMessage = reason;
  54473. websocket._closeCode = code;
  54474. if (websocket._socket[kWebSocket$1] === undefined) return;
  54475. websocket._socket.removeListener('data', socketOnData);
  54476. process.nextTick(resume, websocket._socket);
  54477. if (code === 1005) websocket.close();
  54478. else websocket.close(code, reason);
  54479. }
  54480. /**
  54481. * The listener of the `Receiver` `'drain'` event.
  54482. *
  54483. * @private
  54484. */
  54485. function receiverOnDrain() {
  54486. const websocket = this[kWebSocket$1];
  54487. if (!websocket.isPaused) websocket._socket.resume();
  54488. }
  54489. /**
  54490. * The listener of the `Receiver` `'error'` event.
  54491. *
  54492. * @param {(RangeError|Error)} err The emitted error
  54493. * @private
  54494. */
  54495. function receiverOnError(err) {
  54496. const websocket = this[kWebSocket$1];
  54497. if (websocket._socket[kWebSocket$1] !== undefined) {
  54498. websocket._socket.removeListener('data', socketOnData);
  54499. //
  54500. // On Node.js < 14.0.0 the `'error'` event is emitted synchronously. See
  54501. // https://github.com/websockets/ws/issues/1940.
  54502. //
  54503. process.nextTick(resume, websocket._socket);
  54504. websocket.close(err[kStatusCode]);
  54505. }
  54506. if (!websocket._errorEmitted) {
  54507. websocket._errorEmitted = true;
  54508. websocket.emit('error', err);
  54509. }
  54510. }
  54511. /**
  54512. * The listener of the `Receiver` `'finish'` event.
  54513. *
  54514. * @private
  54515. */
  54516. function receiverOnFinish() {
  54517. this[kWebSocket$1].emitClose();
  54518. }
  54519. /**
  54520. * The listener of the `Receiver` `'message'` event.
  54521. *
  54522. * @param {Buffer|ArrayBuffer|Buffer[])} data The message
  54523. * @param {Boolean} isBinary Specifies whether the message is binary or not
  54524. * @private
  54525. */
  54526. function receiverOnMessage(data, isBinary) {
  54527. this[kWebSocket$1].emit('message', data, isBinary);
  54528. }
  54529. /**
  54530. * The listener of the `Receiver` `'ping'` event.
  54531. *
  54532. * @param {Buffer} data The data included in the ping frame
  54533. * @private
  54534. */
  54535. function receiverOnPing(data) {
  54536. const websocket = this[kWebSocket$1];
  54537. if (websocket._autoPong) websocket.pong(data, !this._isServer, NOOP$1);
  54538. websocket.emit('ping', data);
  54539. }
  54540. /**
  54541. * The listener of the `Receiver` `'pong'` event.
  54542. *
  54543. * @param {Buffer} data The data included in the pong frame
  54544. * @private
  54545. */
  54546. function receiverOnPong(data) {
  54547. this[kWebSocket$1].emit('pong', data);
  54548. }
  54549. /**
  54550. * Resume a readable stream
  54551. *
  54552. * @param {Readable} stream The readable stream
  54553. * @private
  54554. */
  54555. function resume(stream) {
  54556. stream.resume();
  54557. }
  54558. /**
  54559. * The `Sender` error event handler.
  54560. *
  54561. * @param {Error} The error
  54562. * @private
  54563. */
  54564. function senderOnError(err) {
  54565. const websocket = this[kWebSocket$1];
  54566. if (websocket.readyState === WebSocket$1.CLOSED) return;
  54567. if (websocket.readyState === WebSocket$1.OPEN) {
  54568. websocket._readyState = WebSocket$1.CLOSING;
  54569. setCloseTimer(websocket);
  54570. }
  54571. //
  54572. // `socket.end()` is used instead of `socket.destroy()` to allow the other
  54573. // peer to finish sending queued data. There is no need to set a timer here
  54574. // because `CLOSING` means that it is already set or not needed.
  54575. //
  54576. this._socket.end();
  54577. if (!websocket._errorEmitted) {
  54578. websocket._errorEmitted = true;
  54579. websocket.emit('error', err);
  54580. }
  54581. }
  54582. /**
  54583. * Set a timer to destroy the underlying raw socket of a WebSocket.
  54584. *
  54585. * @param {WebSocket} websocket The WebSocket instance
  54586. * @private
  54587. */
  54588. function setCloseTimer(websocket) {
  54589. websocket._closeTimer = setTimeout(
  54590. websocket._socket.destroy.bind(websocket._socket),
  54591. closeTimeout
  54592. );
  54593. }
  54594. /**
  54595. * The listener of the socket `'close'` event.
  54596. *
  54597. * @private
  54598. */
  54599. function socketOnClose() {
  54600. const websocket = this[kWebSocket$1];
  54601. this.removeListener('close', socketOnClose);
  54602. this.removeListener('data', socketOnData);
  54603. this.removeListener('end', socketOnEnd);
  54604. websocket._readyState = WebSocket$1.CLOSING;
  54605. let chunk;
  54606. //
  54607. // The close frame might not have been received or the `'end'` event emitted,
  54608. // for example, if the socket was destroyed due to an error. Ensure that the
  54609. // `receiver` stream is closed after writing any remaining buffered data to
  54610. // it. If the readable side of the socket is in flowing mode then there is no
  54611. // buffered data as everything has been already written and `readable.read()`
  54612. // will return `null`. If instead, the socket is paused, any possible buffered
  54613. // data will be read as a single chunk.
  54614. //
  54615. if (
  54616. !this._readableState.endEmitted &&
  54617. !websocket._closeFrameReceived &&
  54618. !websocket._receiver._writableState.errorEmitted &&
  54619. (chunk = websocket._socket.read()) !== null
  54620. ) {
  54621. websocket._receiver.write(chunk);
  54622. }
  54623. websocket._receiver.end();
  54624. this[kWebSocket$1] = undefined;
  54625. clearTimeout(websocket._closeTimer);
  54626. if (
  54627. websocket._receiver._writableState.finished ||
  54628. websocket._receiver._writableState.errorEmitted
  54629. ) {
  54630. websocket.emitClose();
  54631. } else {
  54632. websocket._receiver.on('error', receiverOnFinish);
  54633. websocket._receiver.on('finish', receiverOnFinish);
  54634. }
  54635. }
  54636. /**
  54637. * The listener of the socket `'data'` event.
  54638. *
  54639. * @param {Buffer} chunk A chunk of data
  54640. * @private
  54641. */
  54642. function socketOnData(chunk) {
  54643. if (!this[kWebSocket$1]._receiver.write(chunk)) {
  54644. this.pause();
  54645. }
  54646. }
  54647. /**
  54648. * The listener of the socket `'end'` event.
  54649. *
  54650. * @private
  54651. */
  54652. function socketOnEnd() {
  54653. const websocket = this[kWebSocket$1];
  54654. websocket._readyState = WebSocket$1.CLOSING;
  54655. websocket._receiver.end();
  54656. this.end();
  54657. }
  54658. /**
  54659. * The listener of the socket `'error'` event.
  54660. *
  54661. * @private
  54662. */
  54663. function socketOnError$1() {
  54664. const websocket = this[kWebSocket$1];
  54665. this.removeListener('error', socketOnError$1);
  54666. this.on('error', NOOP$1);
  54667. if (websocket) {
  54668. websocket._readyState = WebSocket$1.CLOSING;
  54669. this.destroy();
  54670. }
  54671. }
  54672. const { tokenChars } = validationExports;
  54673. /**
  54674. * Parses the `Sec-WebSocket-Protocol` header into a set of subprotocol names.
  54675. *
  54676. * @param {String} header The field value of the header
  54677. * @return {Set} The subprotocol names
  54678. * @public
  54679. */
  54680. function parse(header) {
  54681. const protocols = new Set();
  54682. let start = -1;
  54683. let end = -1;
  54684. let i = 0;
  54685. for (i; i < header.length; i++) {
  54686. const code = header.charCodeAt(i);
  54687. if (end === -1 && tokenChars[code] === 1) {
  54688. if (start === -1) start = i;
  54689. } else if (
  54690. i !== 0 &&
  54691. (code === 0x20 /* ' ' */ || code === 0x09) /* '\t' */
  54692. ) {
  54693. if (end === -1 && start !== -1) end = i;
  54694. } else if (code === 0x2c /* ',' */) {
  54695. if (start === -1) {
  54696. throw new SyntaxError(`Unexpected character at index ${i}`);
  54697. }
  54698. if (end === -1) end = i;
  54699. const protocol = header.slice(start, end);
  54700. if (protocols.has(protocol)) {
  54701. throw new SyntaxError(`The "${protocol}" subprotocol is duplicated`);
  54702. }
  54703. protocols.add(protocol);
  54704. start = end = -1;
  54705. } else {
  54706. throw new SyntaxError(`Unexpected character at index ${i}`);
  54707. }
  54708. }
  54709. if (start === -1 || end !== -1) {
  54710. throw new SyntaxError('Unexpected end of input');
  54711. }
  54712. const protocol = header.slice(start, i);
  54713. if (protocols.has(protocol)) {
  54714. throw new SyntaxError(`The "${protocol}" subprotocol is duplicated`);
  54715. }
  54716. protocols.add(protocol);
  54717. return protocols;
  54718. }
  54719. var subprotocol$1 = { parse };
  54720. /* eslint no-unused-vars: ["error", { "varsIgnorePattern": "^Duplex$", "caughtErrors": "none" }] */
  54721. const EventEmitter = require$$0$7;
  54722. const http$2 = require$$1;
  54723. const { createHash } = require$$3$1;
  54724. const extension = extension$1;
  54725. const PerMessageDeflate = permessageDeflate;
  54726. const subprotocol = subprotocol$1;
  54727. const WebSocket = websocket;
  54728. const { GUID, kWebSocket } = constants;
  54729. const keyRegex = /^[+/0-9A-Za-z]{22}==$/;
  54730. const RUNNING = 0;
  54731. const CLOSING = 1;
  54732. const CLOSED = 2;
  54733. /**
  54734. * Class representing a WebSocket server.
  54735. *
  54736. * @extends EventEmitter
  54737. */
  54738. class WebSocketServer extends EventEmitter {
  54739. /**
  54740. * Create a `WebSocketServer` instance.
  54741. *
  54742. * @param {Object} options Configuration options
  54743. * @param {Boolean} [options.allowSynchronousEvents=true] Specifies whether
  54744. * any of the `'message'`, `'ping'`, and `'pong'` events can be emitted
  54745. * multiple times in the same tick
  54746. * @param {Boolean} [options.autoPong=true] Specifies whether or not to
  54747. * automatically send a pong in response to a ping
  54748. * @param {Number} [options.backlog=511] The maximum length of the queue of
  54749. * pending connections
  54750. * @param {Boolean} [options.clientTracking=true] Specifies whether or not to
  54751. * track clients
  54752. * @param {Function} [options.handleProtocols] A hook to handle protocols
  54753. * @param {String} [options.host] The hostname where to bind the server
  54754. * @param {Number} [options.maxPayload=104857600] The maximum allowed message
  54755. * size
  54756. * @param {Boolean} [options.noServer=false] Enable no server mode
  54757. * @param {String} [options.path] Accept only connections matching this path
  54758. * @param {(Boolean|Object)} [options.perMessageDeflate=false] Enable/disable
  54759. * permessage-deflate
  54760. * @param {Number} [options.port] The port where to bind the server
  54761. * @param {(http.Server|https.Server)} [options.server] A pre-created HTTP/S
  54762. * server to use
  54763. * @param {Boolean} [options.skipUTF8Validation=false] Specifies whether or
  54764. * not to skip UTF-8 validation for text and close messages
  54765. * @param {Function} [options.verifyClient] A hook to reject connections
  54766. * @param {Function} [options.WebSocket=WebSocket] Specifies the `WebSocket`
  54767. * class to use. It must be the `WebSocket` class or class that extends it
  54768. * @param {Function} [callback] A listener for the `listening` event
  54769. */
  54770. constructor(options, callback) {
  54771. super();
  54772. options = {
  54773. allowSynchronousEvents: true,
  54774. autoPong: true,
  54775. maxPayload: 100 * 1024 * 1024,
  54776. skipUTF8Validation: false,
  54777. perMessageDeflate: false,
  54778. handleProtocols: null,
  54779. clientTracking: true,
  54780. verifyClient: null,
  54781. noServer: false,
  54782. backlog: null, // use default (511 as implemented in net.js)
  54783. server: null,
  54784. host: null,
  54785. path: null,
  54786. port: null,
  54787. WebSocket,
  54788. ...options
  54789. };
  54790. if (
  54791. (options.port == null && !options.server && !options.noServer) ||
  54792. (options.port != null && (options.server || options.noServer)) ||
  54793. (options.server && options.noServer)
  54794. ) {
  54795. throw new TypeError(
  54796. 'One and only one of the "port", "server", or "noServer" options ' +
  54797. 'must be specified'
  54798. );
  54799. }
  54800. if (options.port != null) {
  54801. this._server = http$2.createServer((req, res) => {
  54802. const body = http$2.STATUS_CODES[426];
  54803. res.writeHead(426, {
  54804. 'Content-Length': body.length,
  54805. 'Content-Type': 'text/plain'
  54806. });
  54807. res.end(body);
  54808. });
  54809. this._server.listen(
  54810. options.port,
  54811. options.host,
  54812. options.backlog,
  54813. callback
  54814. );
  54815. } else if (options.server) {
  54816. this._server = options.server;
  54817. }
  54818. if (this._server) {
  54819. const emitConnection = this.emit.bind(this, 'connection');
  54820. this._removeListeners = addListeners(this._server, {
  54821. listening: this.emit.bind(this, 'listening'),
  54822. error: this.emit.bind(this, 'error'),
  54823. upgrade: (req, socket, head) => {
  54824. this.handleUpgrade(req, socket, head, emitConnection);
  54825. }
  54826. });
  54827. }
  54828. if (options.perMessageDeflate === true) options.perMessageDeflate = {};
  54829. if (options.clientTracking) {
  54830. this.clients = new Set();
  54831. this._shouldEmitClose = false;
  54832. }
  54833. this.options = options;
  54834. this._state = RUNNING;
  54835. }
  54836. /**
  54837. * Returns the bound address, the address family name, and port of the server
  54838. * as reported by the operating system if listening on an IP socket.
  54839. * If the server is listening on a pipe or UNIX domain socket, the name is
  54840. * returned as a string.
  54841. *
  54842. * @return {(Object|String|null)} The address of the server
  54843. * @public
  54844. */
  54845. address() {
  54846. if (this.options.noServer) {
  54847. throw new Error('The server is operating in "noServer" mode');
  54848. }
  54849. if (!this._server) return null;
  54850. return this._server.address();
  54851. }
  54852. /**
  54853. * Stop the server from accepting new connections and emit the `'close'` event
  54854. * when all existing connections are closed.
  54855. *
  54856. * @param {Function} [cb] A one-time listener for the `'close'` event
  54857. * @public
  54858. */
  54859. close(cb) {
  54860. if (this._state === CLOSED) {
  54861. if (cb) {
  54862. this.once('close', () => {
  54863. cb(new Error('The server is not running'));
  54864. });
  54865. }
  54866. process.nextTick(emitClose, this);
  54867. return;
  54868. }
  54869. if (cb) this.once('close', cb);
  54870. if (this._state === CLOSING) return;
  54871. this._state = CLOSING;
  54872. if (this.options.noServer || this.options.server) {
  54873. if (this._server) {
  54874. this._removeListeners();
  54875. this._removeListeners = this._server = null;
  54876. }
  54877. if (this.clients) {
  54878. if (!this.clients.size) {
  54879. process.nextTick(emitClose, this);
  54880. } else {
  54881. this._shouldEmitClose = true;
  54882. }
  54883. } else {
  54884. process.nextTick(emitClose, this);
  54885. }
  54886. } else {
  54887. const server = this._server;
  54888. this._removeListeners();
  54889. this._removeListeners = this._server = null;
  54890. //
  54891. // The HTTP/S server was created internally. Close it, and rely on its
  54892. // `'close'` event.
  54893. //
  54894. server.close(() => {
  54895. emitClose(this);
  54896. });
  54897. }
  54898. }
  54899. /**
  54900. * See if a given request should be handled by this server instance.
  54901. *
  54902. * @param {http.IncomingMessage} req Request object to inspect
  54903. * @return {Boolean} `true` if the request is valid, else `false`
  54904. * @public
  54905. */
  54906. shouldHandle(req) {
  54907. if (this.options.path) {
  54908. const index = req.url.indexOf('?');
  54909. const pathname = index !== -1 ? req.url.slice(0, index) : req.url;
  54910. if (pathname !== this.options.path) return false;
  54911. }
  54912. return true;
  54913. }
  54914. /**
  54915. * Handle a HTTP Upgrade request.
  54916. *
  54917. * @param {http.IncomingMessage} req The request object
  54918. * @param {Duplex} socket The network socket between the server and client
  54919. * @param {Buffer} head The first packet of the upgraded stream
  54920. * @param {Function} cb Callback
  54921. * @public
  54922. */
  54923. handleUpgrade(req, socket, head, cb) {
  54924. socket.on('error', socketOnError);
  54925. const key = req.headers['sec-websocket-key'];
  54926. const upgrade = req.headers.upgrade;
  54927. const version = +req.headers['sec-websocket-version'];
  54928. if (req.method !== 'GET') {
  54929. const message = 'Invalid HTTP method';
  54930. abortHandshakeOrEmitwsClientError(this, req, socket, 405, message);
  54931. return;
  54932. }
  54933. if (upgrade === undefined || upgrade.toLowerCase() !== 'websocket') {
  54934. const message = 'Invalid Upgrade header';
  54935. abortHandshakeOrEmitwsClientError(this, req, socket, 400, message);
  54936. return;
  54937. }
  54938. if (key === undefined || !keyRegex.test(key)) {
  54939. const message = 'Missing or invalid Sec-WebSocket-Key header';
  54940. abortHandshakeOrEmitwsClientError(this, req, socket, 400, message);
  54941. return;
  54942. }
  54943. if (version !== 8 && version !== 13) {
  54944. const message = 'Missing or invalid Sec-WebSocket-Version header';
  54945. abortHandshakeOrEmitwsClientError(this, req, socket, 400, message);
  54946. return;
  54947. }
  54948. if (!this.shouldHandle(req)) {
  54949. abortHandshake(socket, 400);
  54950. return;
  54951. }
  54952. const secWebSocketProtocol = req.headers['sec-websocket-protocol'];
  54953. let protocols = new Set();
  54954. if (secWebSocketProtocol !== undefined) {
  54955. try {
  54956. protocols = subprotocol.parse(secWebSocketProtocol);
  54957. } catch (err) {
  54958. const message = 'Invalid Sec-WebSocket-Protocol header';
  54959. abortHandshakeOrEmitwsClientError(this, req, socket, 400, message);
  54960. return;
  54961. }
  54962. }
  54963. const secWebSocketExtensions = req.headers['sec-websocket-extensions'];
  54964. const extensions = {};
  54965. if (
  54966. this.options.perMessageDeflate &&
  54967. secWebSocketExtensions !== undefined
  54968. ) {
  54969. const perMessageDeflate = new PerMessageDeflate(
  54970. this.options.perMessageDeflate,
  54971. true,
  54972. this.options.maxPayload
  54973. );
  54974. try {
  54975. const offers = extension.parse(secWebSocketExtensions);
  54976. if (offers[PerMessageDeflate.extensionName]) {
  54977. perMessageDeflate.accept(offers[PerMessageDeflate.extensionName]);
  54978. extensions[PerMessageDeflate.extensionName] = perMessageDeflate;
  54979. }
  54980. } catch (err) {
  54981. const message =
  54982. 'Invalid or unacceptable Sec-WebSocket-Extensions header';
  54983. abortHandshakeOrEmitwsClientError(this, req, socket, 400, message);
  54984. return;
  54985. }
  54986. }
  54987. //
  54988. // Optionally call external client verification handler.
  54989. //
  54990. if (this.options.verifyClient) {
  54991. const info = {
  54992. origin:
  54993. req.headers[`${version === 8 ? 'sec-websocket-origin' : 'origin'}`],
  54994. secure: !!(req.socket.authorized || req.socket.encrypted),
  54995. req
  54996. };
  54997. if (this.options.verifyClient.length === 2) {
  54998. this.options.verifyClient(info, (verified, code, message, headers) => {
  54999. if (!verified) {
  55000. return abortHandshake(socket, code || 401, message, headers);
  55001. }
  55002. this.completeUpgrade(
  55003. extensions,
  55004. key,
  55005. protocols,
  55006. req,
  55007. socket,
  55008. head,
  55009. cb
  55010. );
  55011. });
  55012. return;
  55013. }
  55014. if (!this.options.verifyClient(info)) return abortHandshake(socket, 401);
  55015. }
  55016. this.completeUpgrade(extensions, key, protocols, req, socket, head, cb);
  55017. }
  55018. /**
  55019. * Upgrade the connection to WebSocket.
  55020. *
  55021. * @param {Object} extensions The accepted extensions
  55022. * @param {String} key The value of the `Sec-WebSocket-Key` header
  55023. * @param {Set} protocols The subprotocols
  55024. * @param {http.IncomingMessage} req The request object
  55025. * @param {Duplex} socket The network socket between the server and client
  55026. * @param {Buffer} head The first packet of the upgraded stream
  55027. * @param {Function} cb Callback
  55028. * @throws {Error} If called more than once with the same socket
  55029. * @private
  55030. */
  55031. completeUpgrade(extensions, key, protocols, req, socket, head, cb) {
  55032. //
  55033. // Destroy the socket if the client has already sent a FIN packet.
  55034. //
  55035. if (!socket.readable || !socket.writable) return socket.destroy();
  55036. if (socket[kWebSocket]) {
  55037. throw new Error(
  55038. 'server.handleUpgrade() was called more than once with the same ' +
  55039. 'socket, possibly due to a misconfiguration'
  55040. );
  55041. }
  55042. if (this._state > RUNNING) return abortHandshake(socket, 503);
  55043. const digest = createHash('sha1')
  55044. .update(key + GUID)
  55045. .digest('base64');
  55046. const headers = [
  55047. 'HTTP/1.1 101 Switching Protocols',
  55048. 'Upgrade: websocket',
  55049. 'Connection: Upgrade',
  55050. `Sec-WebSocket-Accept: ${digest}`
  55051. ];
  55052. const ws = new this.options.WebSocket(null, undefined, this.options);
  55053. if (protocols.size) {
  55054. //
  55055. // Optionally call external protocol selection handler.
  55056. //
  55057. const protocol = this.options.handleProtocols
  55058. ? this.options.handleProtocols(protocols, req)
  55059. : protocols.values().next().value;
  55060. if (protocol) {
  55061. headers.push(`Sec-WebSocket-Protocol: ${protocol}`);
  55062. ws._protocol = protocol;
  55063. }
  55064. }
  55065. if (extensions[PerMessageDeflate.extensionName]) {
  55066. const params = extensions[PerMessageDeflate.extensionName].params;
  55067. const value = extension.format({
  55068. [PerMessageDeflate.extensionName]: [params]
  55069. });
  55070. headers.push(`Sec-WebSocket-Extensions: ${value}`);
  55071. ws._extensions = extensions;
  55072. }
  55073. //
  55074. // Allow external modification/inspection of handshake headers.
  55075. //
  55076. this.emit('headers', headers, req);
  55077. socket.write(headers.concat('\r\n').join('\r\n'));
  55078. socket.removeListener('error', socketOnError);
  55079. ws.setSocket(socket, head, {
  55080. allowSynchronousEvents: this.options.allowSynchronousEvents,
  55081. maxPayload: this.options.maxPayload,
  55082. skipUTF8Validation: this.options.skipUTF8Validation
  55083. });
  55084. if (this.clients) {
  55085. this.clients.add(ws);
  55086. ws.on('close', () => {
  55087. this.clients.delete(ws);
  55088. if (this._shouldEmitClose && !this.clients.size) {
  55089. process.nextTick(emitClose, this);
  55090. }
  55091. });
  55092. }
  55093. cb(ws, req);
  55094. }
  55095. }
  55096. var websocketServer = WebSocketServer;
  55097. /**
  55098. * Add event listeners on an `EventEmitter` using a map of <event, listener>
  55099. * pairs.
  55100. *
  55101. * @param {EventEmitter} server The event emitter
  55102. * @param {Object.<String, Function>} map The listeners to add
  55103. * @return {Function} A function that will remove the added listeners when
  55104. * called
  55105. * @private
  55106. */
  55107. function addListeners(server, map) {
  55108. for (const event of Object.keys(map)) server.on(event, map[event]);
  55109. return function removeListeners() {
  55110. for (const event of Object.keys(map)) {
  55111. server.removeListener(event, map[event]);
  55112. }
  55113. };
  55114. }
  55115. /**
  55116. * Emit a `'close'` event on an `EventEmitter`.
  55117. *
  55118. * @param {EventEmitter} server The event emitter
  55119. * @private
  55120. */
  55121. function emitClose(server) {
  55122. server._state = CLOSED;
  55123. server.emit('close');
  55124. }
  55125. /**
  55126. * Handle socket errors.
  55127. *
  55128. * @private
  55129. */
  55130. function socketOnError() {
  55131. this.destroy();
  55132. }
  55133. /**
  55134. * Close the connection when preconditions are not fulfilled.
  55135. *
  55136. * @param {Duplex} socket The socket of the upgrade request
  55137. * @param {Number} code The HTTP response status code
  55138. * @param {String} [message] The HTTP response body
  55139. * @param {Object} [headers] Additional HTTP response headers
  55140. * @private
  55141. */
  55142. function abortHandshake(socket, code, message, headers) {
  55143. //
  55144. // The socket is writable unless the user destroyed or ended it before calling
  55145. // `server.handleUpgrade()` or in the `verifyClient` function, which is a user
  55146. // error. Handling this does not make much sense as the worst that can happen
  55147. // is that some of the data written by the user might be discarded due to the
  55148. // call to `socket.end()` below, which triggers an `'error'` event that in
  55149. // turn causes the socket to be destroyed.
  55150. //
  55151. message = message || http$2.STATUS_CODES[code];
  55152. headers = {
  55153. Connection: 'close',
  55154. 'Content-Type': 'text/html',
  55155. 'Content-Length': Buffer.byteLength(message),
  55156. ...headers
  55157. };
  55158. socket.once('finish', socket.destroy);
  55159. socket.end(
  55160. `HTTP/1.1 ${code} ${http$2.STATUS_CODES[code]}\r\n` +
  55161. Object.keys(headers)
  55162. .map((h) => `${h}: ${headers[h]}`)
  55163. .join('\r\n') +
  55164. '\r\n\r\n' +
  55165. message
  55166. );
  55167. }
  55168. /**
  55169. * Emit a `'wsClientError'` event on a `WebSocketServer` if there is at least
  55170. * one listener for it, otherwise call `abortHandshake()`.
  55171. *
  55172. * @param {WebSocketServer} server The WebSocket server
  55173. * @param {http.IncomingMessage} req The request object
  55174. * @param {Duplex} socket The socket of the upgrade request
  55175. * @param {Number} code The HTTP response status code
  55176. * @param {String} message The HTTP response body
  55177. * @private
  55178. */
  55179. function abortHandshakeOrEmitwsClientError(server, req, socket, code, message) {
  55180. if (server.listenerCount('wsClientError')) {
  55181. const err = new Error(message);
  55182. Error.captureStackTrace(err, abortHandshakeOrEmitwsClientError);
  55183. server.emit('wsClientError', err, socket, req);
  55184. } else {
  55185. abortHandshake(socket, code, message);
  55186. }
  55187. }
  55188. var WebSocketServerRaw_ = /*@__PURE__*/getDefaultExportFromCjs(websocketServer);
  55189. const WebSocketServerRaw = process.versions.bun ? (
  55190. // @ts-expect-error: Bun defines `import.meta.require`
  55191. import.meta.require("ws").WebSocketServer
  55192. ) : WebSocketServerRaw_;
  55193. const HMR_HEADER = "vite-hmr";
  55194. const wsServerEvents = [
  55195. "connection",
  55196. "error",
  55197. "headers",
  55198. "listening",
  55199. "message"
  55200. ];
  55201. function noop$1() {
  55202. }
  55203. function createWebSocketServer(server, config, httpsOptions) {
  55204. if (config.server.ws === false) {
  55205. return {
  55206. name: "ws",
  55207. get clients() {
  55208. return /* @__PURE__ */ new Set();
  55209. },
  55210. async close() {
  55211. },
  55212. on: noop$1,
  55213. off: noop$1,
  55214. listen: noop$1,
  55215. send: noop$1
  55216. };
  55217. }
  55218. let wss;
  55219. let wsHttpServer = void 0;
  55220. const hmr = isObject$1(config.server.hmr) && config.server.hmr;
  55221. const hmrServer = hmr && hmr.server;
  55222. const hmrPort = hmr && hmr.port;
  55223. const portsAreCompatible = !hmrPort || hmrPort === config.server.port;
  55224. const wsServer = hmrServer || portsAreCompatible && server;
  55225. let hmrServerWsListener;
  55226. const customListeners = /* @__PURE__ */ new Map();
  55227. const clientsMap = /* @__PURE__ */ new WeakMap();
  55228. const port = hmrPort || 24678;
  55229. const host = hmr && hmr.host || void 0;
  55230. if (wsServer) {
  55231. let hmrBase = config.base;
  55232. const hmrPath = hmr ? hmr.path : void 0;
  55233. if (hmrPath) {
  55234. hmrBase = path$n.posix.join(hmrBase, hmrPath);
  55235. }
  55236. wss = new WebSocketServerRaw({ noServer: true });
  55237. hmrServerWsListener = (req, socket, head) => {
  55238. if (req.headers["sec-websocket-protocol"] === HMR_HEADER && req.url === hmrBase) {
  55239. wss.handleUpgrade(req, socket, head, (ws) => {
  55240. wss.emit("connection", ws, req);
  55241. });
  55242. }
  55243. };
  55244. wsServer.on("upgrade", hmrServerWsListener);
  55245. } else {
  55246. const route = (_, res) => {
  55247. const statusCode = 426;
  55248. const body = STATUS_CODES[statusCode];
  55249. if (!body)
  55250. throw new Error(`No body text found for the ${statusCode} status code`);
  55251. res.writeHead(statusCode, {
  55252. "Content-Length": body.length,
  55253. "Content-Type": "text/plain"
  55254. });
  55255. res.end(body);
  55256. };
  55257. if (httpsOptions) {
  55258. wsHttpServer = createServer$2(httpsOptions, route);
  55259. } else {
  55260. wsHttpServer = createServer$3(route);
  55261. }
  55262. wss = new WebSocketServerRaw({ server: wsHttpServer });
  55263. }
  55264. wss.on("connection", (socket) => {
  55265. socket.on("message", (raw) => {
  55266. if (!customListeners.size) return;
  55267. let parsed;
  55268. try {
  55269. parsed = JSON.parse(String(raw));
  55270. } catch {
  55271. }
  55272. if (!parsed || parsed.type !== "custom" || !parsed.event) return;
  55273. const listeners = customListeners.get(parsed.event);
  55274. if (!listeners?.size) return;
  55275. const client = getSocketClient(socket);
  55276. listeners.forEach((listener) => listener(parsed.data, client));
  55277. });
  55278. socket.on("error", (err) => {
  55279. config.logger.error(`${colors$1.red(`ws error:`)}
  55280. ${err.stack}`, {
  55281. timestamp: true,
  55282. error: err
  55283. });
  55284. });
  55285. socket.send(JSON.stringify({ type: "connected" }));
  55286. if (bufferedError) {
  55287. socket.send(JSON.stringify(bufferedError));
  55288. bufferedError = null;
  55289. }
  55290. });
  55291. wss.on("error", (e) => {
  55292. if (e.code === "EADDRINUSE") {
  55293. config.logger.error(
  55294. colors$1.red(`WebSocket server error: Port is already in use`),
  55295. { error: e }
  55296. );
  55297. } else {
  55298. config.logger.error(
  55299. colors$1.red(`WebSocket server error:
  55300. ${e.stack || e.message}`),
  55301. { error: e }
  55302. );
  55303. }
  55304. });
  55305. function getSocketClient(socket) {
  55306. if (!clientsMap.has(socket)) {
  55307. clientsMap.set(socket, {
  55308. send: (...args) => {
  55309. let payload;
  55310. if (typeof args[0] === "string") {
  55311. payload = {
  55312. type: "custom",
  55313. event: args[0],
  55314. data: args[1]
  55315. };
  55316. } else {
  55317. payload = args[0];
  55318. }
  55319. socket.send(JSON.stringify(payload));
  55320. },
  55321. socket
  55322. });
  55323. }
  55324. return clientsMap.get(socket);
  55325. }
  55326. let bufferedError = null;
  55327. return {
  55328. name: "ws",
  55329. listen: () => {
  55330. wsHttpServer?.listen(port, host);
  55331. },
  55332. on: (event, fn) => {
  55333. if (wsServerEvents.includes(event)) wss.on(event, fn);
  55334. else {
  55335. if (!customListeners.has(event)) {
  55336. customListeners.set(event, /* @__PURE__ */ new Set());
  55337. }
  55338. customListeners.get(event).add(fn);
  55339. }
  55340. },
  55341. off: (event, fn) => {
  55342. if (wsServerEvents.includes(event)) {
  55343. wss.off(event, fn);
  55344. } else {
  55345. customListeners.get(event)?.delete(fn);
  55346. }
  55347. },
  55348. get clients() {
  55349. return new Set(Array.from(wss.clients).map(getSocketClient));
  55350. },
  55351. send(...args) {
  55352. let payload;
  55353. if (typeof args[0] === "string") {
  55354. payload = {
  55355. type: "custom",
  55356. event: args[0],
  55357. data: args[1]
  55358. };
  55359. } else {
  55360. payload = args[0];
  55361. }
  55362. if (payload.type === "error" && !wss.clients.size) {
  55363. bufferedError = payload;
  55364. return;
  55365. }
  55366. const stringified = JSON.stringify(payload);
  55367. wss.clients.forEach((client) => {
  55368. if (client.readyState === 1) {
  55369. client.send(stringified);
  55370. }
  55371. });
  55372. },
  55373. close() {
  55374. if (hmrServerWsListener && wsServer) {
  55375. wsServer.off("upgrade", hmrServerWsListener);
  55376. }
  55377. return new Promise((resolve, reject) => {
  55378. wss.clients.forEach((client) => {
  55379. client.terminate();
  55380. });
  55381. wss.close((err) => {
  55382. if (err) {
  55383. reject(err);
  55384. } else {
  55385. if (wsHttpServer) {
  55386. wsHttpServer.close((err2) => {
  55387. if (err2) {
  55388. reject(err2);
  55389. } else {
  55390. resolve();
  55391. }
  55392. });
  55393. } else {
  55394. resolve();
  55395. }
  55396. }
  55397. });
  55398. });
  55399. }
  55400. };
  55401. }
  55402. function baseMiddleware(rawBase, middlewareMode) {
  55403. return function viteBaseMiddleware(req, res, next) {
  55404. const url = req.url;
  55405. const pathname = cleanUrl(url);
  55406. const base = rawBase;
  55407. if (pathname.startsWith(base)) {
  55408. req.url = stripBase(url, base);
  55409. return next();
  55410. }
  55411. if (middlewareMode) {
  55412. return next();
  55413. }
  55414. if (pathname === "/" || pathname === "/index.html") {
  55415. res.writeHead(302, {
  55416. Location: base + url.slice(pathname.length)
  55417. });
  55418. res.end();
  55419. return;
  55420. }
  55421. const redirectPath = withTrailingSlash(url) !== base ? joinUrlSegments(base, url) : base;
  55422. if (req.headers.accept?.includes("text/html")) {
  55423. res.writeHead(404, {
  55424. "Content-Type": "text/html"
  55425. });
  55426. res.end(
  55427. `The server is configured with a public base URL of ${base} - did you mean to visit <a href="${redirectPath}">${redirectPath}</a> instead?`
  55428. );
  55429. return;
  55430. } else {
  55431. res.writeHead(404, {
  55432. "Content-Type": "text/plain"
  55433. });
  55434. res.end(
  55435. `The server is configured with a public base URL of ${base} - did you mean to visit ${redirectPath} instead?`
  55436. );
  55437. return;
  55438. }
  55439. };
  55440. }
  55441. var httpProxy$3 = {exports: {}};
  55442. var eventemitter3 = {exports: {}};
  55443. (function (module) {
  55444. var has = Object.prototype.hasOwnProperty
  55445. , prefix = '~';
  55446. /**
  55447. * Constructor to create a storage for our `EE` objects.
  55448. * An `Events` instance is a plain object whose properties are event names.
  55449. *
  55450. * @constructor
  55451. * @private
  55452. */
  55453. function Events() {}
  55454. //
  55455. // We try to not inherit from `Object.prototype`. In some engines creating an
  55456. // instance in this way is faster than calling `Object.create(null)` directly.
  55457. // If `Object.create(null)` is not supported we prefix the event names with a
  55458. // character to make sure that the built-in object properties are not
  55459. // overridden or used as an attack vector.
  55460. //
  55461. if (Object.create) {
  55462. Events.prototype = Object.create(null);
  55463. //
  55464. // This hack is needed because the `__proto__` property is still inherited in
  55465. // some old browsers like Android 4, iPhone 5.1, Opera 11 and Safari 5.
  55466. //
  55467. if (!new Events().__proto__) prefix = false;
  55468. }
  55469. /**
  55470. * Representation of a single event listener.
  55471. *
  55472. * @param {Function} fn The listener function.
  55473. * @param {*} context The context to invoke the listener with.
  55474. * @param {Boolean} [once=false] Specify if the listener is a one-time listener.
  55475. * @constructor
  55476. * @private
  55477. */
  55478. function EE(fn, context, once) {
  55479. this.fn = fn;
  55480. this.context = context;
  55481. this.once = once || false;
  55482. }
  55483. /**
  55484. * Add a listener for a given event.
  55485. *
  55486. * @param {EventEmitter} emitter Reference to the `EventEmitter` instance.
  55487. * @param {(String|Symbol)} event The event name.
  55488. * @param {Function} fn The listener function.
  55489. * @param {*} context The context to invoke the listener with.
  55490. * @param {Boolean} once Specify if the listener is a one-time listener.
  55491. * @returns {EventEmitter}
  55492. * @private
  55493. */
  55494. function addListener(emitter, event, fn, context, once) {
  55495. if (typeof fn !== 'function') {
  55496. throw new TypeError('The listener must be a function');
  55497. }
  55498. var listener = new EE(fn, context || emitter, once)
  55499. , evt = prefix ? prefix + event : event;
  55500. if (!emitter._events[evt]) emitter._events[evt] = listener, emitter._eventsCount++;
  55501. else if (!emitter._events[evt].fn) emitter._events[evt].push(listener);
  55502. else emitter._events[evt] = [emitter._events[evt], listener];
  55503. return emitter;
  55504. }
  55505. /**
  55506. * Clear event by name.
  55507. *
  55508. * @param {EventEmitter} emitter Reference to the `EventEmitter` instance.
  55509. * @param {(String|Symbol)} evt The Event name.
  55510. * @private
  55511. */
  55512. function clearEvent(emitter, evt) {
  55513. if (--emitter._eventsCount === 0) emitter._events = new Events();
  55514. else delete emitter._events[evt];
  55515. }
  55516. /**
  55517. * Minimal `EventEmitter` interface that is molded against the Node.js
  55518. * `EventEmitter` interface.
  55519. *
  55520. * @constructor
  55521. * @public
  55522. */
  55523. function EventEmitter() {
  55524. this._events = new Events();
  55525. this._eventsCount = 0;
  55526. }
  55527. /**
  55528. * Return an array listing the events for which the emitter has registered
  55529. * listeners.
  55530. *
  55531. * @returns {Array}
  55532. * @public
  55533. */
  55534. EventEmitter.prototype.eventNames = function eventNames() {
  55535. var names = []
  55536. , events
  55537. , name;
  55538. if (this._eventsCount === 0) return names;
  55539. for (name in (events = this._events)) {
  55540. if (has.call(events, name)) names.push(prefix ? name.slice(1) : name);
  55541. }
  55542. if (Object.getOwnPropertySymbols) {
  55543. return names.concat(Object.getOwnPropertySymbols(events));
  55544. }
  55545. return names;
  55546. };
  55547. /**
  55548. * Return the listeners registered for a given event.
  55549. *
  55550. * @param {(String|Symbol)} event The event name.
  55551. * @returns {Array} The registered listeners.
  55552. * @public
  55553. */
  55554. EventEmitter.prototype.listeners = function listeners(event) {
  55555. var evt = prefix ? prefix + event : event
  55556. , handlers = this._events[evt];
  55557. if (!handlers) return [];
  55558. if (handlers.fn) return [handlers.fn];
  55559. for (var i = 0, l = handlers.length, ee = new Array(l); i < l; i++) {
  55560. ee[i] = handlers[i].fn;
  55561. }
  55562. return ee;
  55563. };
  55564. /**
  55565. * Return the number of listeners listening to a given event.
  55566. *
  55567. * @param {(String|Symbol)} event The event name.
  55568. * @returns {Number} The number of listeners.
  55569. * @public
  55570. */
  55571. EventEmitter.prototype.listenerCount = function listenerCount(event) {
  55572. var evt = prefix ? prefix + event : event
  55573. , listeners = this._events[evt];
  55574. if (!listeners) return 0;
  55575. if (listeners.fn) return 1;
  55576. return listeners.length;
  55577. };
  55578. /**
  55579. * Calls each of the listeners registered for a given event.
  55580. *
  55581. * @param {(String|Symbol)} event The event name.
  55582. * @returns {Boolean} `true` if the event had listeners, else `false`.
  55583. * @public
  55584. */
  55585. EventEmitter.prototype.emit = function emit(event, a1, a2, a3, a4, a5) {
  55586. var evt = prefix ? prefix + event : event;
  55587. if (!this._events[evt]) return false;
  55588. var listeners = this._events[evt]
  55589. , len = arguments.length
  55590. , args
  55591. , i;
  55592. if (listeners.fn) {
  55593. if (listeners.once) this.removeListener(event, listeners.fn, undefined, true);
  55594. switch (len) {
  55595. case 1: return listeners.fn.call(listeners.context), true;
  55596. case 2: return listeners.fn.call(listeners.context, a1), true;
  55597. case 3: return listeners.fn.call(listeners.context, a1, a2), true;
  55598. case 4: return listeners.fn.call(listeners.context, a1, a2, a3), true;
  55599. case 5: return listeners.fn.call(listeners.context, a1, a2, a3, a4), true;
  55600. case 6: return listeners.fn.call(listeners.context, a1, a2, a3, a4, a5), true;
  55601. }
  55602. for (i = 1, args = new Array(len -1); i < len; i++) {
  55603. args[i - 1] = arguments[i];
  55604. }
  55605. listeners.fn.apply(listeners.context, args);
  55606. } else {
  55607. var length = listeners.length
  55608. , j;
  55609. for (i = 0; i < length; i++) {
  55610. if (listeners[i].once) this.removeListener(event, listeners[i].fn, undefined, true);
  55611. switch (len) {
  55612. case 1: listeners[i].fn.call(listeners[i].context); break;
  55613. case 2: listeners[i].fn.call(listeners[i].context, a1); break;
  55614. case 3: listeners[i].fn.call(listeners[i].context, a1, a2); break;
  55615. case 4: listeners[i].fn.call(listeners[i].context, a1, a2, a3); break;
  55616. default:
  55617. if (!args) for (j = 1, args = new Array(len -1); j < len; j++) {
  55618. args[j - 1] = arguments[j];
  55619. }
  55620. listeners[i].fn.apply(listeners[i].context, args);
  55621. }
  55622. }
  55623. }
  55624. return true;
  55625. };
  55626. /**
  55627. * Add a listener for a given event.
  55628. *
  55629. * @param {(String|Symbol)} event The event name.
  55630. * @param {Function} fn The listener function.
  55631. * @param {*} [context=this] The context to invoke the listener with.
  55632. * @returns {EventEmitter} `this`.
  55633. * @public
  55634. */
  55635. EventEmitter.prototype.on = function on(event, fn, context) {
  55636. return addListener(this, event, fn, context, false);
  55637. };
  55638. /**
  55639. * Add a one-time listener for a given event.
  55640. *
  55641. * @param {(String|Symbol)} event The event name.
  55642. * @param {Function} fn The listener function.
  55643. * @param {*} [context=this] The context to invoke the listener with.
  55644. * @returns {EventEmitter} `this`.
  55645. * @public
  55646. */
  55647. EventEmitter.prototype.once = function once(event, fn, context) {
  55648. return addListener(this, event, fn, context, true);
  55649. };
  55650. /**
  55651. * Remove the listeners of a given event.
  55652. *
  55653. * @param {(String|Symbol)} event The event name.
  55654. * @param {Function} fn Only remove the listeners that match this function.
  55655. * @param {*} context Only remove the listeners that have this context.
  55656. * @param {Boolean} once Only remove one-time listeners.
  55657. * @returns {EventEmitter} `this`.
  55658. * @public
  55659. */
  55660. EventEmitter.prototype.removeListener = function removeListener(event, fn, context, once) {
  55661. var evt = prefix ? prefix + event : event;
  55662. if (!this._events[evt]) return this;
  55663. if (!fn) {
  55664. clearEvent(this, evt);
  55665. return this;
  55666. }
  55667. var listeners = this._events[evt];
  55668. if (listeners.fn) {
  55669. if (
  55670. listeners.fn === fn &&
  55671. (!once || listeners.once) &&
  55672. (!context || listeners.context === context)
  55673. ) {
  55674. clearEvent(this, evt);
  55675. }
  55676. } else {
  55677. for (var i = 0, events = [], length = listeners.length; i < length; i++) {
  55678. if (
  55679. listeners[i].fn !== fn ||
  55680. (once && !listeners[i].once) ||
  55681. (context && listeners[i].context !== context)
  55682. ) {
  55683. events.push(listeners[i]);
  55684. }
  55685. }
  55686. //
  55687. // Reset the array, or remove it completely if we have no more listeners.
  55688. //
  55689. if (events.length) this._events[evt] = events.length === 1 ? events[0] : events;
  55690. else clearEvent(this, evt);
  55691. }
  55692. return this;
  55693. };
  55694. /**
  55695. * Remove all listeners, or those of the specified event.
  55696. *
  55697. * @param {(String|Symbol)} [event] The event name.
  55698. * @returns {EventEmitter} `this`.
  55699. * @public
  55700. */
  55701. EventEmitter.prototype.removeAllListeners = function removeAllListeners(event) {
  55702. var evt;
  55703. if (event) {
  55704. evt = prefix ? prefix + event : event;
  55705. if (this._events[evt]) clearEvent(this, evt);
  55706. } else {
  55707. this._events = new Events();
  55708. this._eventsCount = 0;
  55709. }
  55710. return this;
  55711. };
  55712. //
  55713. // Alias methods names because people roll like that.
  55714. //
  55715. EventEmitter.prototype.off = EventEmitter.prototype.removeListener;
  55716. EventEmitter.prototype.addListener = EventEmitter.prototype.on;
  55717. //
  55718. // Expose the prefix.
  55719. //
  55720. EventEmitter.prefixed = prefix;
  55721. //
  55722. // Allow `EventEmitter` to be imported as module namespace.
  55723. //
  55724. EventEmitter.EventEmitter = EventEmitter;
  55725. //
  55726. // Expose the module.
  55727. //
  55728. {
  55729. module.exports = EventEmitter;
  55730. }
  55731. } (eventemitter3));
  55732. var eventemitter3Exports = eventemitter3.exports;
  55733. var common$3 = {};
  55734. /**
  55735. * Check if we're required to add a port number.
  55736. *
  55737. * @see https://url.spec.whatwg.org/#default-port
  55738. * @param {Number|String} port Port number we need to check
  55739. * @param {String} protocol Protocol we need to check against.
  55740. * @returns {Boolean} Is it a default port for the given protocol
  55741. * @api private
  55742. */
  55743. var requiresPort = function required(port, protocol) {
  55744. protocol = protocol.split(':')[0];
  55745. port = +port;
  55746. if (!port) return false;
  55747. switch (protocol) {
  55748. case 'http':
  55749. case 'ws':
  55750. return port !== 80;
  55751. case 'https':
  55752. case 'wss':
  55753. return port !== 443;
  55754. case 'ftp':
  55755. return port !== 21;
  55756. case 'gopher':
  55757. return port !== 70;
  55758. case 'file':
  55759. return false;
  55760. }
  55761. return port !== 0;
  55762. };
  55763. (function (exports) {
  55764. var common = exports,
  55765. url = require$$0$9,
  55766. required = requiresPort;
  55767. var upgradeHeader = /(^|,)\s*upgrade\s*($|,)/i,
  55768. isSSL = /^https|wss/;
  55769. /**
  55770. * Simple Regex for testing if protocol is https
  55771. */
  55772. common.isSSL = isSSL;
  55773. /**
  55774. * Copies the right headers from `options` and `req` to
  55775. * `outgoing` which is then used to fire the proxied
  55776. * request.
  55777. *
  55778. * Examples:
  55779. *
  55780. * common.setupOutgoing(outgoing, options, req)
  55781. * // => { host: ..., hostname: ...}
  55782. *
  55783. * @param {Object} Outgoing Base object to be filled with required properties
  55784. * @param {Object} Options Config object passed to the proxy
  55785. * @param {ClientRequest} Req Request Object
  55786. * @param {String} Forward String to select forward or target
  55787. * @return {Object} Outgoing Object with all required properties set
  55788. *
  55789. * @api private
  55790. */
  55791. common.setupOutgoing = function(outgoing, options, req, forward) {
  55792. outgoing.port = options[forward || 'target'].port ||
  55793. (isSSL.test(options[forward || 'target'].protocol) ? 443 : 80);
  55794. ['host', 'hostname', 'socketPath', 'pfx', 'key',
  55795. 'passphrase', 'cert', 'ca', 'ciphers', 'secureProtocol'].forEach(
  55796. function(e) { outgoing[e] = options[forward || 'target'][e]; }
  55797. );
  55798. outgoing.method = options.method || req.method;
  55799. outgoing.headers = Object.assign({}, req.headers);
  55800. if (options.headers){
  55801. Object.assign(outgoing.headers, options.headers);
  55802. }
  55803. if (options.auth) {
  55804. outgoing.auth = options.auth;
  55805. }
  55806. if (options.ca) {
  55807. outgoing.ca = options.ca;
  55808. }
  55809. if (isSSL.test(options[forward || 'target'].protocol)) {
  55810. outgoing.rejectUnauthorized = (typeof options.secure === "undefined") ? true : options.secure;
  55811. }
  55812. outgoing.agent = options.agent || false;
  55813. outgoing.localAddress = options.localAddress;
  55814. //
  55815. // Remark: If we are false and not upgrading, set the connection: close. This is the right thing to do
  55816. // as node core doesn't handle this COMPLETELY properly yet.
  55817. //
  55818. if (!outgoing.agent) {
  55819. outgoing.headers = outgoing.headers || {};
  55820. if (typeof outgoing.headers.connection !== 'string'
  55821. || !upgradeHeader.test(outgoing.headers.connection)
  55822. ) { outgoing.headers.connection = 'close'; }
  55823. }
  55824. // the final path is target path + relative path requested by user:
  55825. var target = options[forward || 'target'];
  55826. var targetPath = target && options.prependPath !== false
  55827. ? (target.path || '')
  55828. : '';
  55829. //
  55830. // Remark: Can we somehow not use url.parse as a perf optimization?
  55831. //
  55832. var outgoingPath = !options.toProxy
  55833. ? (url.parse(req.url).path || '')
  55834. : req.url;
  55835. //
  55836. // Remark: ignorePath will just straight up ignore whatever the request's
  55837. // path is. This can be labeled as FOOT-GUN material if you do not know what
  55838. // you are doing and are using conflicting options.
  55839. //
  55840. outgoingPath = !options.ignorePath ? outgoingPath : '';
  55841. outgoing.path = common.urlJoin(targetPath, outgoingPath);
  55842. if (options.changeOrigin) {
  55843. outgoing.headers.host =
  55844. required(outgoing.port, options[forward || 'target'].protocol) && !hasPort(outgoing.host)
  55845. ? outgoing.host + ':' + outgoing.port
  55846. : outgoing.host;
  55847. }
  55848. return outgoing;
  55849. };
  55850. /**
  55851. * Set the proper configuration for sockets,
  55852. * set no delay and set keep alive, also set
  55853. * the timeout to 0.
  55854. *
  55855. * Examples:
  55856. *
  55857. * common.setupSocket(socket)
  55858. * // => Socket
  55859. *
  55860. * @param {Socket} Socket instance to setup
  55861. * @return {Socket} Return the configured socket.
  55862. *
  55863. * @api private
  55864. */
  55865. common.setupSocket = function(socket) {
  55866. socket.setTimeout(0);
  55867. socket.setNoDelay(true);
  55868. socket.setKeepAlive(true, 0);
  55869. return socket;
  55870. };
  55871. /**
  55872. * Get the port number from the host. Or guess it based on the connection type.
  55873. *
  55874. * @param {Request} req Incoming HTTP request.
  55875. *
  55876. * @return {String} The port number.
  55877. *
  55878. * @api private
  55879. */
  55880. common.getPort = function(req) {
  55881. var res = req.headers.host ? req.headers.host.match(/:(\d+)/) : '';
  55882. return res ?
  55883. res[1] :
  55884. common.hasEncryptedConnection(req) ? '443' : '80';
  55885. };
  55886. /**
  55887. * Check if the request has an encrypted connection.
  55888. *
  55889. * @param {Request} req Incoming HTTP request.
  55890. *
  55891. * @return {Boolean} Whether the connection is encrypted or not.
  55892. *
  55893. * @api private
  55894. */
  55895. common.hasEncryptedConnection = function(req) {
  55896. return Boolean(req.connection.encrypted || req.connection.pair);
  55897. };
  55898. /**
  55899. * OS-agnostic join (doesn't break on URLs like path.join does on Windows)>
  55900. *
  55901. * @return {String} The generated path.
  55902. *
  55903. * @api private
  55904. */
  55905. common.urlJoin = function() {
  55906. //
  55907. // We do not want to mess with the query string. All we want to touch is the path.
  55908. //
  55909. var args = Array.prototype.slice.call(arguments),
  55910. lastIndex = args.length - 1,
  55911. last = args[lastIndex],
  55912. lastSegs = last.split('?'),
  55913. retSegs;
  55914. args[lastIndex] = lastSegs.shift();
  55915. //
  55916. // Join all strings, but remove empty strings so we don't get extra slashes from
  55917. // joining e.g. ['', 'am']
  55918. //
  55919. retSegs = [
  55920. args.filter(Boolean).join('/')
  55921. .replace(/\/+/g, '/')
  55922. .replace('http:/', 'http://')
  55923. .replace('https:/', 'https://')
  55924. ];
  55925. // Only join the query string if it exists so we don't have trailing a '?'
  55926. // on every request
  55927. // Handle case where there could be multiple ? in the URL.
  55928. retSegs.push.apply(retSegs, lastSegs);
  55929. return retSegs.join('?')
  55930. };
  55931. /**
  55932. * Rewrites or removes the domain of a cookie header
  55933. *
  55934. * @param {String|Array} Header
  55935. * @param {Object} Config, mapping of domain to rewritten domain.
  55936. * '*' key to match any domain, null value to remove the domain.
  55937. *
  55938. * @api private
  55939. */
  55940. common.rewriteCookieProperty = function rewriteCookieProperty(header, config, property) {
  55941. if (Array.isArray(header)) {
  55942. return header.map(function (headerElement) {
  55943. return rewriteCookieProperty(headerElement, config, property);
  55944. });
  55945. }
  55946. return header.replace(new RegExp("(;\\s*" + property + "=)([^;]+)", 'i'), function(match, prefix, previousValue) {
  55947. var newValue;
  55948. if (previousValue in config) {
  55949. newValue = config[previousValue];
  55950. } else if ('*' in config) {
  55951. newValue = config['*'];
  55952. } else {
  55953. //no match, return previous value
  55954. return match;
  55955. }
  55956. if (newValue) {
  55957. //replace value
  55958. return prefix + newValue;
  55959. } else {
  55960. //remove value
  55961. return '';
  55962. }
  55963. });
  55964. };
  55965. /**
  55966. * Check the host and see if it potentially has a port in it (keep it simple)
  55967. *
  55968. * @returns {Boolean} Whether we have one or not
  55969. *
  55970. * @api private
  55971. */
  55972. function hasPort(host) {
  55973. return !!~host.indexOf(':');
  55974. }} (common$3));
  55975. var url$1 = require$$0$9,
  55976. common$2 = common$3;
  55977. var redirectRegex = /^201|30(1|2|7|8)$/;
  55978. /*!
  55979. * Array of passes.
  55980. *
  55981. * A `pass` is just a function that is executed on `req, res, options`
  55982. * so that you can easily add new checks while still keeping the base
  55983. * flexible.
  55984. */
  55985. var webOutgoing = { // <--
  55986. /**
  55987. * If is a HTTP 1.0 request, remove chunk headers
  55988. *
  55989. * @param {ClientRequest} Req Request object
  55990. * @param {IncomingMessage} Res Response object
  55991. * @param {proxyResponse} Res Response object from the proxy request
  55992. *
  55993. * @api private
  55994. */
  55995. removeChunked: function removeChunked(req, res, proxyRes) {
  55996. if (req.httpVersion === '1.0') {
  55997. delete proxyRes.headers['transfer-encoding'];
  55998. }
  55999. },
  56000. /**
  56001. * If is a HTTP 1.0 request, set the correct connection header
  56002. * or if connection header not present, then use `keep-alive`
  56003. *
  56004. * @param {ClientRequest} Req Request object
  56005. * @param {IncomingMessage} Res Response object
  56006. * @param {proxyResponse} Res Response object from the proxy request
  56007. *
  56008. * @api private
  56009. */
  56010. setConnection: function setConnection(req, res, proxyRes) {
  56011. if (req.httpVersion === '1.0') {
  56012. proxyRes.headers.connection = req.headers.connection || 'close';
  56013. } else if (req.httpVersion !== '2.0' && !proxyRes.headers.connection) {
  56014. proxyRes.headers.connection = req.headers.connection || 'keep-alive';
  56015. }
  56016. },
  56017. setRedirectHostRewrite: function setRedirectHostRewrite(req, res, proxyRes, options) {
  56018. if ((options.hostRewrite || options.autoRewrite || options.protocolRewrite)
  56019. && proxyRes.headers['location']
  56020. && redirectRegex.test(proxyRes.statusCode)) {
  56021. var target = url$1.parse(options.target);
  56022. var u = url$1.parse(proxyRes.headers['location']);
  56023. // make sure the redirected host matches the target host before rewriting
  56024. if (target.host != u.host) {
  56025. return;
  56026. }
  56027. if (options.hostRewrite) {
  56028. u.host = options.hostRewrite;
  56029. } else if (options.autoRewrite) {
  56030. u.host = req.headers['host'];
  56031. }
  56032. if (options.protocolRewrite) {
  56033. u.protocol = options.protocolRewrite;
  56034. }
  56035. proxyRes.headers['location'] = u.format();
  56036. }
  56037. },
  56038. /**
  56039. * Copy headers from proxyResponse to response
  56040. * set each header in response object.
  56041. *
  56042. * @param {ClientRequest} Req Request object
  56043. * @param {IncomingMessage} Res Response object
  56044. * @param {proxyResponse} Res Response object from the proxy request
  56045. * @param {Object} Options options.cookieDomainRewrite: Config to rewrite cookie domain
  56046. *
  56047. * @api private
  56048. */
  56049. writeHeaders: function writeHeaders(req, res, proxyRes, options) {
  56050. var rewriteCookieDomainConfig = options.cookieDomainRewrite,
  56051. rewriteCookiePathConfig = options.cookiePathRewrite,
  56052. preserveHeaderKeyCase = options.preserveHeaderKeyCase,
  56053. rawHeaderKeyMap,
  56054. setHeader = function(key, header) {
  56055. if (header == undefined) return;
  56056. if (rewriteCookieDomainConfig && key.toLowerCase() === 'set-cookie') {
  56057. header = common$2.rewriteCookieProperty(header, rewriteCookieDomainConfig, 'domain');
  56058. }
  56059. if (rewriteCookiePathConfig && key.toLowerCase() === 'set-cookie') {
  56060. header = common$2.rewriteCookieProperty(header, rewriteCookiePathConfig, 'path');
  56061. }
  56062. res.setHeader(String(key).trim(), header);
  56063. };
  56064. if (typeof rewriteCookieDomainConfig === 'string') { //also test for ''
  56065. rewriteCookieDomainConfig = { '*': rewriteCookieDomainConfig };
  56066. }
  56067. if (typeof rewriteCookiePathConfig === 'string') { //also test for ''
  56068. rewriteCookiePathConfig = { '*': rewriteCookiePathConfig };
  56069. }
  56070. // message.rawHeaders is added in: v0.11.6
  56071. // https://nodejs.org/api/http.html#http_message_rawheaders
  56072. if (preserveHeaderKeyCase && proxyRes.rawHeaders != undefined) {
  56073. rawHeaderKeyMap = {};
  56074. for (var i = 0; i < proxyRes.rawHeaders.length; i += 2) {
  56075. var key = proxyRes.rawHeaders[i];
  56076. rawHeaderKeyMap[key.toLowerCase()] = key;
  56077. }
  56078. }
  56079. Object.keys(proxyRes.headers).forEach(function(key) {
  56080. var header = proxyRes.headers[key];
  56081. if (preserveHeaderKeyCase && rawHeaderKeyMap) {
  56082. key = rawHeaderKeyMap[key] || key;
  56083. }
  56084. setHeader(key, header);
  56085. });
  56086. },
  56087. /**
  56088. * Set the statusCode from the proxyResponse
  56089. *
  56090. * @param {ClientRequest} Req Request object
  56091. * @param {IncomingMessage} Res Response object
  56092. * @param {proxyResponse} Res Response object from the proxy request
  56093. *
  56094. * @api private
  56095. */
  56096. writeStatusCode: function writeStatusCode(req, res, proxyRes) {
  56097. // From Node.js docs: response.writeHead(statusCode[, statusMessage][, headers])
  56098. if(proxyRes.statusMessage) {
  56099. res.statusCode = proxyRes.statusCode;
  56100. res.statusMessage = proxyRes.statusMessage;
  56101. } else {
  56102. res.statusCode = proxyRes.statusCode;
  56103. }
  56104. }
  56105. };
  56106. var followRedirects$1 = {exports: {}};
  56107. var debug$6;
  56108. var debug_1 = function () {
  56109. if (!debug$6) {
  56110. try {
  56111. /* eslint global-require: off */
  56112. debug$6 = srcExports$1("follow-redirects");
  56113. }
  56114. catch (error) { /* */ }
  56115. if (typeof debug$6 !== "function") {
  56116. debug$6 = function () { /* */ };
  56117. }
  56118. }
  56119. debug$6.apply(null, arguments);
  56120. };
  56121. var url = require$$0$9;
  56122. var URL$1 = url.URL;
  56123. var http$1 = require$$1;
  56124. var https$1 = require$$1$1;
  56125. var Writable = require$$0$6.Writable;
  56126. var assert = require$$4$3;
  56127. var debug$5 = debug_1;
  56128. // Whether to use the native URL object or the legacy url module
  56129. var useNativeURL = false;
  56130. try {
  56131. assert(new URL$1());
  56132. }
  56133. catch (error) {
  56134. useNativeURL = error.code === "ERR_INVALID_URL";
  56135. }
  56136. // URL fields to preserve in copy operations
  56137. var preservedUrlFields = [
  56138. "auth",
  56139. "host",
  56140. "hostname",
  56141. "href",
  56142. "path",
  56143. "pathname",
  56144. "port",
  56145. "protocol",
  56146. "query",
  56147. "search",
  56148. "hash",
  56149. ];
  56150. // Create handlers that pass events from native requests
  56151. var events = ["abort", "aborted", "connect", "error", "socket", "timeout"];
  56152. var eventHandlers = Object.create(null);
  56153. events.forEach(function (event) {
  56154. eventHandlers[event] = function (arg1, arg2, arg3) {
  56155. this._redirectable.emit(event, arg1, arg2, arg3);
  56156. };
  56157. });
  56158. // Error types with codes
  56159. var InvalidUrlError = createErrorType(
  56160. "ERR_INVALID_URL",
  56161. "Invalid URL",
  56162. TypeError
  56163. );
  56164. var RedirectionError = createErrorType(
  56165. "ERR_FR_REDIRECTION_FAILURE",
  56166. "Redirected request failed"
  56167. );
  56168. var TooManyRedirectsError = createErrorType(
  56169. "ERR_FR_TOO_MANY_REDIRECTS",
  56170. "Maximum number of redirects exceeded",
  56171. RedirectionError
  56172. );
  56173. var MaxBodyLengthExceededError = createErrorType(
  56174. "ERR_FR_MAX_BODY_LENGTH_EXCEEDED",
  56175. "Request body larger than maxBodyLength limit"
  56176. );
  56177. var WriteAfterEndError = createErrorType(
  56178. "ERR_STREAM_WRITE_AFTER_END",
  56179. "write after end"
  56180. );
  56181. // istanbul ignore next
  56182. var destroy = Writable.prototype.destroy || noop;
  56183. // An HTTP(S) request that can be redirected
  56184. function RedirectableRequest(options, responseCallback) {
  56185. // Initialize the request
  56186. Writable.call(this);
  56187. this._sanitizeOptions(options);
  56188. this._options = options;
  56189. this._ended = false;
  56190. this._ending = false;
  56191. this._redirectCount = 0;
  56192. this._redirects = [];
  56193. this._requestBodyLength = 0;
  56194. this._requestBodyBuffers = [];
  56195. // Attach a callback if passed
  56196. if (responseCallback) {
  56197. this.on("response", responseCallback);
  56198. }
  56199. // React to responses of native requests
  56200. var self = this;
  56201. this._onNativeResponse = function (response) {
  56202. try {
  56203. self._processResponse(response);
  56204. }
  56205. catch (cause) {
  56206. self.emit("error", cause instanceof RedirectionError ?
  56207. cause : new RedirectionError({ cause: cause }));
  56208. }
  56209. };
  56210. // Perform the first request
  56211. this._performRequest();
  56212. }
  56213. RedirectableRequest.prototype = Object.create(Writable.prototype);
  56214. RedirectableRequest.prototype.abort = function () {
  56215. destroyRequest(this._currentRequest);
  56216. this._currentRequest.abort();
  56217. this.emit("abort");
  56218. };
  56219. RedirectableRequest.prototype.destroy = function (error) {
  56220. destroyRequest(this._currentRequest, error);
  56221. destroy.call(this, error);
  56222. return this;
  56223. };
  56224. // Writes buffered data to the current native request
  56225. RedirectableRequest.prototype.write = function (data, encoding, callback) {
  56226. // Writing is not allowed if end has been called
  56227. if (this._ending) {
  56228. throw new WriteAfterEndError();
  56229. }
  56230. // Validate input and shift parameters if necessary
  56231. if (!isString(data) && !isBuffer(data)) {
  56232. throw new TypeError("data should be a string, Buffer or Uint8Array");
  56233. }
  56234. if (isFunction(encoding)) {
  56235. callback = encoding;
  56236. encoding = null;
  56237. }
  56238. // Ignore empty buffers, since writing them doesn't invoke the callback
  56239. // https://github.com/nodejs/node/issues/22066
  56240. if (data.length === 0) {
  56241. if (callback) {
  56242. callback();
  56243. }
  56244. return;
  56245. }
  56246. // Only write when we don't exceed the maximum body length
  56247. if (this._requestBodyLength + data.length <= this._options.maxBodyLength) {
  56248. this._requestBodyLength += data.length;
  56249. this._requestBodyBuffers.push({ data: data, encoding: encoding });
  56250. this._currentRequest.write(data, encoding, callback);
  56251. }
  56252. // Error when we exceed the maximum body length
  56253. else {
  56254. this.emit("error", new MaxBodyLengthExceededError());
  56255. this.abort();
  56256. }
  56257. };
  56258. // Ends the current native request
  56259. RedirectableRequest.prototype.end = function (data, encoding, callback) {
  56260. // Shift parameters if necessary
  56261. if (isFunction(data)) {
  56262. callback = data;
  56263. data = encoding = null;
  56264. }
  56265. else if (isFunction(encoding)) {
  56266. callback = encoding;
  56267. encoding = null;
  56268. }
  56269. // Write data if needed and end
  56270. if (!data) {
  56271. this._ended = this._ending = true;
  56272. this._currentRequest.end(null, null, callback);
  56273. }
  56274. else {
  56275. var self = this;
  56276. var currentRequest = this._currentRequest;
  56277. this.write(data, encoding, function () {
  56278. self._ended = true;
  56279. currentRequest.end(null, null, callback);
  56280. });
  56281. this._ending = true;
  56282. }
  56283. };
  56284. // Sets a header value on the current native request
  56285. RedirectableRequest.prototype.setHeader = function (name, value) {
  56286. this._options.headers[name] = value;
  56287. this._currentRequest.setHeader(name, value);
  56288. };
  56289. // Clears a header value on the current native request
  56290. RedirectableRequest.prototype.removeHeader = function (name) {
  56291. delete this._options.headers[name];
  56292. this._currentRequest.removeHeader(name);
  56293. };
  56294. // Global timeout for all underlying requests
  56295. RedirectableRequest.prototype.setTimeout = function (msecs, callback) {
  56296. var self = this;
  56297. // Destroys the socket on timeout
  56298. function destroyOnTimeout(socket) {
  56299. socket.setTimeout(msecs);
  56300. socket.removeListener("timeout", socket.destroy);
  56301. socket.addListener("timeout", socket.destroy);
  56302. }
  56303. // Sets up a timer to trigger a timeout event
  56304. function startTimer(socket) {
  56305. if (self._timeout) {
  56306. clearTimeout(self._timeout);
  56307. }
  56308. self._timeout = setTimeout(function () {
  56309. self.emit("timeout");
  56310. clearTimer();
  56311. }, msecs);
  56312. destroyOnTimeout(socket);
  56313. }
  56314. // Stops a timeout from triggering
  56315. function clearTimer() {
  56316. // Clear the timeout
  56317. if (self._timeout) {
  56318. clearTimeout(self._timeout);
  56319. self._timeout = null;
  56320. }
  56321. // Clean up all attached listeners
  56322. self.removeListener("abort", clearTimer);
  56323. self.removeListener("error", clearTimer);
  56324. self.removeListener("response", clearTimer);
  56325. self.removeListener("close", clearTimer);
  56326. if (callback) {
  56327. self.removeListener("timeout", callback);
  56328. }
  56329. if (!self.socket) {
  56330. self._currentRequest.removeListener("socket", startTimer);
  56331. }
  56332. }
  56333. // Attach callback if passed
  56334. if (callback) {
  56335. this.on("timeout", callback);
  56336. }
  56337. // Start the timer if or when the socket is opened
  56338. if (this.socket) {
  56339. startTimer(this.socket);
  56340. }
  56341. else {
  56342. this._currentRequest.once("socket", startTimer);
  56343. }
  56344. // Clean up on events
  56345. this.on("socket", destroyOnTimeout);
  56346. this.on("abort", clearTimer);
  56347. this.on("error", clearTimer);
  56348. this.on("response", clearTimer);
  56349. this.on("close", clearTimer);
  56350. return this;
  56351. };
  56352. // Proxy all other public ClientRequest methods
  56353. [
  56354. "flushHeaders", "getHeader",
  56355. "setNoDelay", "setSocketKeepAlive",
  56356. ].forEach(function (method) {
  56357. RedirectableRequest.prototype[method] = function (a, b) {
  56358. return this._currentRequest[method](a, b);
  56359. };
  56360. });
  56361. // Proxy all public ClientRequest properties
  56362. ["aborted", "connection", "socket"].forEach(function (property) {
  56363. Object.defineProperty(RedirectableRequest.prototype, property, {
  56364. get: function () { return this._currentRequest[property]; },
  56365. });
  56366. });
  56367. RedirectableRequest.prototype._sanitizeOptions = function (options) {
  56368. // Ensure headers are always present
  56369. if (!options.headers) {
  56370. options.headers = {};
  56371. }
  56372. // Since http.request treats host as an alias of hostname,
  56373. // but the url module interprets host as hostname plus port,
  56374. // eliminate the host property to avoid confusion.
  56375. if (options.host) {
  56376. // Use hostname if set, because it has precedence
  56377. if (!options.hostname) {
  56378. options.hostname = options.host;
  56379. }
  56380. delete options.host;
  56381. }
  56382. // Complete the URL object when necessary
  56383. if (!options.pathname && options.path) {
  56384. var searchPos = options.path.indexOf("?");
  56385. if (searchPos < 0) {
  56386. options.pathname = options.path;
  56387. }
  56388. else {
  56389. options.pathname = options.path.substring(0, searchPos);
  56390. options.search = options.path.substring(searchPos);
  56391. }
  56392. }
  56393. };
  56394. // Executes the next native request (initial or redirect)
  56395. RedirectableRequest.prototype._performRequest = function () {
  56396. // Load the native protocol
  56397. var protocol = this._options.protocol;
  56398. var nativeProtocol = this._options.nativeProtocols[protocol];
  56399. if (!nativeProtocol) {
  56400. throw new TypeError("Unsupported protocol " + protocol);
  56401. }
  56402. // If specified, use the agent corresponding to the protocol
  56403. // (HTTP and HTTPS use different types of agents)
  56404. if (this._options.agents) {
  56405. var scheme = protocol.slice(0, -1);
  56406. this._options.agent = this._options.agents[scheme];
  56407. }
  56408. // Create the native request and set up its event handlers
  56409. var request = this._currentRequest =
  56410. nativeProtocol.request(this._options, this._onNativeResponse);
  56411. request._redirectable = this;
  56412. for (var event of events) {
  56413. request.on(event, eventHandlers[event]);
  56414. }
  56415. // RFC7230§5.3.1: When making a request directly to an origin server, […]
  56416. // a client MUST send only the absolute path […] as the request-target.
  56417. this._currentUrl = /^\//.test(this._options.path) ?
  56418. url.format(this._options) :
  56419. // When making a request to a proxy, […]
  56420. // a client MUST send the target URI in absolute-form […].
  56421. this._options.path;
  56422. // End a redirected request
  56423. // (The first request must be ended explicitly with RedirectableRequest#end)
  56424. if (this._isRedirect) {
  56425. // Write the request entity and end
  56426. var i = 0;
  56427. var self = this;
  56428. var buffers = this._requestBodyBuffers;
  56429. (function writeNext(error) {
  56430. // Only write if this request has not been redirected yet
  56431. /* istanbul ignore else */
  56432. if (request === self._currentRequest) {
  56433. // Report any write errors
  56434. /* istanbul ignore if */
  56435. if (error) {
  56436. self.emit("error", error);
  56437. }
  56438. // Write the next buffer if there are still left
  56439. else if (i < buffers.length) {
  56440. var buffer = buffers[i++];
  56441. /* istanbul ignore else */
  56442. if (!request.finished) {
  56443. request.write(buffer.data, buffer.encoding, writeNext);
  56444. }
  56445. }
  56446. // End the request if `end` has been called on us
  56447. else if (self._ended) {
  56448. request.end();
  56449. }
  56450. }
  56451. }());
  56452. }
  56453. };
  56454. // Processes a response from the current native request
  56455. RedirectableRequest.prototype._processResponse = function (response) {
  56456. // Store the redirected response
  56457. var statusCode = response.statusCode;
  56458. if (this._options.trackRedirects) {
  56459. this._redirects.push({
  56460. url: this._currentUrl,
  56461. headers: response.headers,
  56462. statusCode: statusCode,
  56463. });
  56464. }
  56465. // RFC7231§6.4: The 3xx (Redirection) class of status code indicates
  56466. // that further action needs to be taken by the user agent in order to
  56467. // fulfill the request. If a Location header field is provided,
  56468. // the user agent MAY automatically redirect its request to the URI
  56469. // referenced by the Location field value,
  56470. // even if the specific status code is not understood.
  56471. // If the response is not a redirect; return it as-is
  56472. var location = response.headers.location;
  56473. if (!location || this._options.followRedirects === false ||
  56474. statusCode < 300 || statusCode >= 400) {
  56475. response.responseUrl = this._currentUrl;
  56476. response.redirects = this._redirects;
  56477. this.emit("response", response);
  56478. // Clean up
  56479. this._requestBodyBuffers = [];
  56480. return;
  56481. }
  56482. // The response is a redirect, so abort the current request
  56483. destroyRequest(this._currentRequest);
  56484. // Discard the remainder of the response to avoid waiting for data
  56485. response.destroy();
  56486. // RFC7231§6.4: A client SHOULD detect and intervene
  56487. // in cyclical redirections (i.e., "infinite" redirection loops).
  56488. if (++this._redirectCount > this._options.maxRedirects) {
  56489. throw new TooManyRedirectsError();
  56490. }
  56491. // Store the request headers if applicable
  56492. var requestHeaders;
  56493. var beforeRedirect = this._options.beforeRedirect;
  56494. if (beforeRedirect) {
  56495. requestHeaders = Object.assign({
  56496. // The Host header was set by nativeProtocol.request
  56497. Host: response.req.getHeader("host"),
  56498. }, this._options.headers);
  56499. }
  56500. // RFC7231§6.4: Automatic redirection needs to done with
  56501. // care for methods not known to be safe, […]
  56502. // RFC7231§6.4.2–3: For historical reasons, a user agent MAY change
  56503. // the request method from POST to GET for the subsequent request.
  56504. var method = this._options.method;
  56505. if ((statusCode === 301 || statusCode === 302) && this._options.method === "POST" ||
  56506. // RFC7231§6.4.4: The 303 (See Other) status code indicates that
  56507. // the server is redirecting the user agent to a different resource […]
  56508. // A user agent can perform a retrieval request targeting that URI
  56509. // (a GET or HEAD request if using HTTP) […]
  56510. (statusCode === 303) && !/^(?:GET|HEAD)$/.test(this._options.method)) {
  56511. this._options.method = "GET";
  56512. // Drop a possible entity and headers related to it
  56513. this._requestBodyBuffers = [];
  56514. removeMatchingHeaders(/^content-/i, this._options.headers);
  56515. }
  56516. // Drop the Host header, as the redirect might lead to a different host
  56517. var currentHostHeader = removeMatchingHeaders(/^host$/i, this._options.headers);
  56518. // If the redirect is relative, carry over the host of the last request
  56519. var currentUrlParts = parseUrl(this._currentUrl);
  56520. var currentHost = currentHostHeader || currentUrlParts.host;
  56521. var currentUrl = /^\w+:/.test(location) ? this._currentUrl :
  56522. url.format(Object.assign(currentUrlParts, { host: currentHost }));
  56523. // Create the redirected request
  56524. var redirectUrl = resolveUrl(location, currentUrl);
  56525. debug$5("redirecting to", redirectUrl.href);
  56526. this._isRedirect = true;
  56527. spreadUrlObject(redirectUrl, this._options);
  56528. // Drop confidential headers when redirecting to a less secure protocol
  56529. // or to a different domain that is not a superdomain
  56530. if (redirectUrl.protocol !== currentUrlParts.protocol &&
  56531. redirectUrl.protocol !== "https:" ||
  56532. redirectUrl.host !== currentHost &&
  56533. !isSubdomain(redirectUrl.host, currentHost)) {
  56534. removeMatchingHeaders(/^(?:(?:proxy-)?authorization|cookie)$/i, this._options.headers);
  56535. }
  56536. // Evaluate the beforeRedirect callback
  56537. if (isFunction(beforeRedirect)) {
  56538. var responseDetails = {
  56539. headers: response.headers,
  56540. statusCode: statusCode,
  56541. };
  56542. var requestDetails = {
  56543. url: currentUrl,
  56544. method: method,
  56545. headers: requestHeaders,
  56546. };
  56547. beforeRedirect(this._options, responseDetails, requestDetails);
  56548. this._sanitizeOptions(this._options);
  56549. }
  56550. // Perform the redirected request
  56551. this._performRequest();
  56552. };
  56553. // Wraps the key/value object of protocols with redirect functionality
  56554. function wrap(protocols) {
  56555. // Default settings
  56556. var exports = {
  56557. maxRedirects: 21,
  56558. maxBodyLength: 10 * 1024 * 1024,
  56559. };
  56560. // Wrap each protocol
  56561. var nativeProtocols = {};
  56562. Object.keys(protocols).forEach(function (scheme) {
  56563. var protocol = scheme + ":";
  56564. var nativeProtocol = nativeProtocols[protocol] = protocols[scheme];
  56565. var wrappedProtocol = exports[scheme] = Object.create(nativeProtocol);
  56566. // Executes a request, following redirects
  56567. function request(input, options, callback) {
  56568. // Parse parameters, ensuring that input is an object
  56569. if (isURL(input)) {
  56570. input = spreadUrlObject(input);
  56571. }
  56572. else if (isString(input)) {
  56573. input = spreadUrlObject(parseUrl(input));
  56574. }
  56575. else {
  56576. callback = options;
  56577. options = validateUrl(input);
  56578. input = { protocol: protocol };
  56579. }
  56580. if (isFunction(options)) {
  56581. callback = options;
  56582. options = null;
  56583. }
  56584. // Set defaults
  56585. options = Object.assign({
  56586. maxRedirects: exports.maxRedirects,
  56587. maxBodyLength: exports.maxBodyLength,
  56588. }, input, options);
  56589. options.nativeProtocols = nativeProtocols;
  56590. if (!isString(options.host) && !isString(options.hostname)) {
  56591. options.hostname = "::1";
  56592. }
  56593. assert.equal(options.protocol, protocol, "protocol mismatch");
  56594. debug$5("options", options);
  56595. return new RedirectableRequest(options, callback);
  56596. }
  56597. // Executes a GET request, following redirects
  56598. function get(input, options, callback) {
  56599. var wrappedRequest = wrappedProtocol.request(input, options, callback);
  56600. wrappedRequest.end();
  56601. return wrappedRequest;
  56602. }
  56603. // Expose the properties on the wrapped protocol
  56604. Object.defineProperties(wrappedProtocol, {
  56605. request: { value: request, configurable: true, enumerable: true, writable: true },
  56606. get: { value: get, configurable: true, enumerable: true, writable: true },
  56607. });
  56608. });
  56609. return exports;
  56610. }
  56611. function noop() { /* empty */ }
  56612. function parseUrl(input) {
  56613. var parsed;
  56614. /* istanbul ignore else */
  56615. if (useNativeURL) {
  56616. parsed = new URL$1(input);
  56617. }
  56618. else {
  56619. // Ensure the URL is valid and absolute
  56620. parsed = validateUrl(url.parse(input));
  56621. if (!isString(parsed.protocol)) {
  56622. throw new InvalidUrlError({ input });
  56623. }
  56624. }
  56625. return parsed;
  56626. }
  56627. function resolveUrl(relative, base) {
  56628. /* istanbul ignore next */
  56629. return useNativeURL ? new URL$1(relative, base) : parseUrl(url.resolve(base, relative));
  56630. }
  56631. function validateUrl(input) {
  56632. if (/^\[/.test(input.hostname) && !/^\[[:0-9a-f]+\]$/i.test(input.hostname)) {
  56633. throw new InvalidUrlError({ input: input.href || input });
  56634. }
  56635. if (/^\[/.test(input.host) && !/^\[[:0-9a-f]+\](:\d+)?$/i.test(input.host)) {
  56636. throw new InvalidUrlError({ input: input.href || input });
  56637. }
  56638. return input;
  56639. }
  56640. function spreadUrlObject(urlObject, target) {
  56641. var spread = target || {};
  56642. for (var key of preservedUrlFields) {
  56643. spread[key] = urlObject[key];
  56644. }
  56645. // Fix IPv6 hostname
  56646. if (spread.hostname.startsWith("[")) {
  56647. spread.hostname = spread.hostname.slice(1, -1);
  56648. }
  56649. // Ensure port is a number
  56650. if (spread.port !== "") {
  56651. spread.port = Number(spread.port);
  56652. }
  56653. // Concatenate path
  56654. spread.path = spread.search ? spread.pathname + spread.search : spread.pathname;
  56655. return spread;
  56656. }
  56657. function removeMatchingHeaders(regex, headers) {
  56658. var lastValue;
  56659. for (var header in headers) {
  56660. if (regex.test(header)) {
  56661. lastValue = headers[header];
  56662. delete headers[header];
  56663. }
  56664. }
  56665. return (lastValue === null || typeof lastValue === "undefined") ?
  56666. undefined : String(lastValue).trim();
  56667. }
  56668. function createErrorType(code, message, baseClass) {
  56669. // Create constructor
  56670. function CustomError(properties) {
  56671. Error.captureStackTrace(this, this.constructor);
  56672. Object.assign(this, properties || {});
  56673. this.code = code;
  56674. this.message = this.cause ? message + ": " + this.cause.message : message;
  56675. }
  56676. // Attach constructor and set default properties
  56677. CustomError.prototype = new (baseClass || Error)();
  56678. Object.defineProperties(CustomError.prototype, {
  56679. constructor: {
  56680. value: CustomError,
  56681. enumerable: false,
  56682. },
  56683. name: {
  56684. value: "Error [" + code + "]",
  56685. enumerable: false,
  56686. },
  56687. });
  56688. return CustomError;
  56689. }
  56690. function destroyRequest(request, error) {
  56691. for (var event of events) {
  56692. request.removeListener(event, eventHandlers[event]);
  56693. }
  56694. request.on("error", noop);
  56695. request.destroy(error);
  56696. }
  56697. function isSubdomain(subdomain, domain) {
  56698. assert(isString(subdomain) && isString(domain));
  56699. var dot = subdomain.length - domain.length - 1;
  56700. return dot > 0 && subdomain[dot] === "." && subdomain.endsWith(domain);
  56701. }
  56702. function isString(value) {
  56703. return typeof value === "string" || value instanceof String;
  56704. }
  56705. function isFunction(value) {
  56706. return typeof value === "function";
  56707. }
  56708. function isBuffer(value) {
  56709. return typeof value === "object" && ("length" in value);
  56710. }
  56711. function isURL(value) {
  56712. return URL$1 && value instanceof URL$1;
  56713. }
  56714. // Exports
  56715. followRedirects$1.exports = wrap({ http: http$1, https: https$1 });
  56716. followRedirects$1.exports.wrap = wrap;
  56717. var followRedirectsExports = followRedirects$1.exports;
  56718. var httpNative = require$$1,
  56719. httpsNative = require$$1$1,
  56720. web_o = webOutgoing,
  56721. common$1 = common$3,
  56722. followRedirects = followRedirectsExports;
  56723. web_o = Object.keys(web_o).map(function(pass) {
  56724. return web_o[pass];
  56725. });
  56726. var nativeAgents = { http: httpNative, https: httpsNative };
  56727. /*!
  56728. * Array of passes.
  56729. *
  56730. * A `pass` is just a function that is executed on `req, res, options`
  56731. * so that you can easily add new checks while still keeping the base
  56732. * flexible.
  56733. */
  56734. var webIncoming = {
  56735. /**
  56736. * Sets `content-length` to '0' if request is of DELETE type.
  56737. *
  56738. * @param {ClientRequest} Req Request object
  56739. * @param {IncomingMessage} Res Response object
  56740. * @param {Object} Options Config object passed to the proxy
  56741. *
  56742. * @api private
  56743. */
  56744. deleteLength: function deleteLength(req, res, options) {
  56745. if((req.method === 'DELETE' || req.method === 'OPTIONS')
  56746. && !req.headers['content-length']) {
  56747. req.headers['content-length'] = '0';
  56748. delete req.headers['transfer-encoding'];
  56749. }
  56750. },
  56751. /**
  56752. * Sets timeout in request socket if it was specified in options.
  56753. *
  56754. * @param {ClientRequest} Req Request object
  56755. * @param {IncomingMessage} Res Response object
  56756. * @param {Object} Options Config object passed to the proxy
  56757. *
  56758. * @api private
  56759. */
  56760. timeout: function timeout(req, res, options) {
  56761. if(options.timeout) {
  56762. req.socket.setTimeout(options.timeout);
  56763. }
  56764. },
  56765. /**
  56766. * Sets `x-forwarded-*` headers if specified in config.
  56767. *
  56768. * @param {ClientRequest} Req Request object
  56769. * @param {IncomingMessage} Res Response object
  56770. * @param {Object} Options Config object passed to the proxy
  56771. *
  56772. * @api private
  56773. */
  56774. XHeaders: function XHeaders(req, res, options) {
  56775. if(!options.xfwd) return;
  56776. var encrypted = req.isSpdy || common$1.hasEncryptedConnection(req);
  56777. var values = {
  56778. for : req.connection.remoteAddress || req.socket.remoteAddress,
  56779. port : common$1.getPort(req),
  56780. proto: encrypted ? 'https' : 'http'
  56781. };
  56782. ['for', 'port', 'proto'].forEach(function(header) {
  56783. req.headers['x-forwarded-' + header] =
  56784. (req.headers['x-forwarded-' + header] || '') +
  56785. (req.headers['x-forwarded-' + header] ? ',' : '') +
  56786. values[header];
  56787. });
  56788. req.headers['x-forwarded-host'] = req.headers['x-forwarded-host'] || req.headers['host'] || '';
  56789. },
  56790. /**
  56791. * Does the actual proxying. If `forward` is enabled fires up
  56792. * a ForwardStream, same happens for ProxyStream. The request
  56793. * just dies otherwise.
  56794. *
  56795. * @param {ClientRequest} Req Request object
  56796. * @param {IncomingMessage} Res Response object
  56797. * @param {Object} Options Config object passed to the proxy
  56798. *
  56799. * @api private
  56800. */
  56801. stream: function stream(req, res, options, _, server, clb) {
  56802. // And we begin!
  56803. server.emit('start', req, res, options.target || options.forward);
  56804. var agents = options.followRedirects ? followRedirects : nativeAgents;
  56805. var http = agents.http;
  56806. var https = agents.https;
  56807. if(options.forward) {
  56808. // If forward enable, so just pipe the request
  56809. var forwardReq = (options.forward.protocol === 'https:' ? https : http).request(
  56810. common$1.setupOutgoing(options.ssl || {}, options, req, 'forward')
  56811. );
  56812. // error handler (e.g. ECONNRESET, ECONNREFUSED)
  56813. // Handle errors on incoming request as well as it makes sense to
  56814. var forwardError = createErrorHandler(forwardReq, options.forward);
  56815. req.on('error', forwardError);
  56816. forwardReq.on('error', forwardError);
  56817. (options.buffer || req).pipe(forwardReq);
  56818. if(!options.target) { return res.end(); }
  56819. }
  56820. // Request initalization
  56821. var proxyReq = (options.target.protocol === 'https:' ? https : http).request(
  56822. common$1.setupOutgoing(options.ssl || {}, options, req)
  56823. );
  56824. // Enable developers to modify the proxyReq before headers are sent
  56825. proxyReq.on('socket', function(socket) {
  56826. if(server && !proxyReq.getHeader('expect')) {
  56827. server.emit('proxyReq', proxyReq, req, res, options);
  56828. }
  56829. });
  56830. // allow outgoing socket to timeout so that we could
  56831. // show an error page at the initial request
  56832. if(options.proxyTimeout) {
  56833. proxyReq.setTimeout(options.proxyTimeout, function() {
  56834. proxyReq.abort();
  56835. });
  56836. }
  56837. // Ensure we abort proxy if request is aborted
  56838. req.on('aborted', function () {
  56839. proxyReq.abort();
  56840. });
  56841. // handle errors in proxy and incoming request, just like for forward proxy
  56842. var proxyError = createErrorHandler(proxyReq, options.target);
  56843. req.on('error', proxyError);
  56844. proxyReq.on('error', proxyError);
  56845. function createErrorHandler(proxyReq, url) {
  56846. return function proxyError(err) {
  56847. if (req.socket.destroyed && err.code === 'ECONNRESET') {
  56848. server.emit('econnreset', err, req, res, url);
  56849. return proxyReq.abort();
  56850. }
  56851. if (clb) {
  56852. clb(err, req, res, url);
  56853. } else {
  56854. server.emit('error', err, req, res, url);
  56855. }
  56856. }
  56857. }
  56858. (options.buffer || req).pipe(proxyReq);
  56859. proxyReq.on('response', function(proxyRes) {
  56860. if(server) { server.emit('proxyRes', proxyRes, req, res); }
  56861. if(!res.headersSent && !options.selfHandleResponse) {
  56862. for(var i=0; i < web_o.length; i++) {
  56863. if(web_o[i](req, res, proxyRes, options)) { break; }
  56864. }
  56865. }
  56866. if (!res.finished) {
  56867. // Allow us to listen when the proxy has completed
  56868. proxyRes.on('end', function () {
  56869. if (server) server.emit('end', req, res, proxyRes);
  56870. });
  56871. // We pipe to the response unless its expected to be handled by the user
  56872. if (!options.selfHandleResponse) proxyRes.pipe(res);
  56873. } else {
  56874. if (server) server.emit('end', req, res, proxyRes);
  56875. }
  56876. });
  56877. }
  56878. };
  56879. var http = require$$1,
  56880. https = require$$1$1,
  56881. common = common$3;
  56882. /*!
  56883. * Array of passes.
  56884. *
  56885. * A `pass` is just a function that is executed on `req, socket, options`
  56886. * so that you can easily add new checks while still keeping the base
  56887. * flexible.
  56888. */
  56889. /*
  56890. * Websockets Passes
  56891. *
  56892. */
  56893. var wsIncoming = {
  56894. /**
  56895. * WebSocket requests must have the `GET` method and
  56896. * the `upgrade:websocket` header
  56897. *
  56898. * @param {ClientRequest} Req Request object
  56899. * @param {Socket} Websocket
  56900. *
  56901. * @api private
  56902. */
  56903. checkMethodAndHeader : function checkMethodAndHeader(req, socket) {
  56904. if (req.method !== 'GET' || !req.headers.upgrade) {
  56905. socket.destroy();
  56906. return true;
  56907. }
  56908. if (req.headers.upgrade.toLowerCase() !== 'websocket') {
  56909. socket.destroy();
  56910. return true;
  56911. }
  56912. },
  56913. /**
  56914. * Sets `x-forwarded-*` headers if specified in config.
  56915. *
  56916. * @param {ClientRequest} Req Request object
  56917. * @param {Socket} Websocket
  56918. * @param {Object} Options Config object passed to the proxy
  56919. *
  56920. * @api private
  56921. */
  56922. XHeaders : function XHeaders(req, socket, options) {
  56923. if(!options.xfwd) return;
  56924. var values = {
  56925. for : req.connection.remoteAddress || req.socket.remoteAddress,
  56926. port : common.getPort(req),
  56927. proto: common.hasEncryptedConnection(req) ? 'wss' : 'ws'
  56928. };
  56929. ['for', 'port', 'proto'].forEach(function(header) {
  56930. req.headers['x-forwarded-' + header] =
  56931. (req.headers['x-forwarded-' + header] || '') +
  56932. (req.headers['x-forwarded-' + header] ? ',' : '') +
  56933. values[header];
  56934. });
  56935. },
  56936. /**
  56937. * Does the actual proxying. Make the request and upgrade it
  56938. * send the Switching Protocols request and pipe the sockets.
  56939. *
  56940. * @param {ClientRequest} Req Request object
  56941. * @param {Socket} Websocket
  56942. * @param {Object} Options Config object passed to the proxy
  56943. *
  56944. * @api private
  56945. */
  56946. stream : function stream(req, socket, options, head, server, clb) {
  56947. var createHttpHeader = function(line, headers) {
  56948. return Object.keys(headers).reduce(function (head, key) {
  56949. var value = headers[key];
  56950. if (!Array.isArray(value)) {
  56951. head.push(key + ': ' + value);
  56952. return head;
  56953. }
  56954. for (var i = 0; i < value.length; i++) {
  56955. head.push(key + ': ' + value[i]);
  56956. }
  56957. return head;
  56958. }, [line])
  56959. .join('\r\n') + '\r\n\r\n';
  56960. };
  56961. common.setupSocket(socket);
  56962. if (head && head.length) socket.unshift(head);
  56963. var proxyReq = (common.isSSL.test(options.target.protocol) ? https : http).request(
  56964. common.setupOutgoing(options.ssl || {}, options, req)
  56965. );
  56966. // Enable developers to modify the proxyReq before headers are sent
  56967. if (server) { server.emit('proxyReqWs', proxyReq, req, socket, options, head); }
  56968. // Error Handler
  56969. proxyReq.on('error', onOutgoingError);
  56970. proxyReq.on('response', function (res) {
  56971. // if upgrade event isn't going to happen, close the socket
  56972. if (!res.upgrade) {
  56973. socket.write(createHttpHeader('HTTP/' + res.httpVersion + ' ' + res.statusCode + ' ' + res.statusMessage, res.headers));
  56974. res.pipe(socket);
  56975. }
  56976. });
  56977. proxyReq.on('upgrade', function(proxyRes, proxySocket, proxyHead) {
  56978. proxySocket.on('error', onOutgoingError);
  56979. // Allow us to listen when the websocket has completed
  56980. proxySocket.on('end', function () {
  56981. server.emit('close', proxyRes, proxySocket, proxyHead);
  56982. });
  56983. // The pipe below will end proxySocket if socket closes cleanly, but not
  56984. // if it errors (eg, vanishes from the net and starts returning
  56985. // EHOSTUNREACH). We need to do that explicitly.
  56986. socket.on('error', function () {
  56987. proxySocket.end();
  56988. });
  56989. common.setupSocket(proxySocket);
  56990. if (proxyHead && proxyHead.length) proxySocket.unshift(proxyHead);
  56991. //
  56992. // Remark: Handle writing the headers to the socket when switching protocols
  56993. // Also handles when a header is an array
  56994. //
  56995. socket.write(createHttpHeader('HTTP/1.1 101 Switching Protocols', proxyRes.headers));
  56996. proxySocket.pipe(socket).pipe(proxySocket);
  56997. server.emit('open', proxySocket);
  56998. server.emit('proxySocket', proxySocket); //DEPRECATED.
  56999. });
  57000. return proxyReq.end(); // XXX: CHECK IF THIS IS THIS CORRECT
  57001. function onOutgoingError(err) {
  57002. if (clb) {
  57003. clb(err, req, socket);
  57004. } else {
  57005. server.emit('error', err, req, socket);
  57006. }
  57007. socket.end();
  57008. }
  57009. }
  57010. };
  57011. (function (module) {
  57012. var httpProxy = module.exports,
  57013. parse_url = require$$0$9.parse,
  57014. EE3 = eventemitter3Exports,
  57015. http = require$$1,
  57016. https = require$$1$1,
  57017. web = webIncoming,
  57018. ws = wsIncoming;
  57019. httpProxy.Server = ProxyServer;
  57020. /**
  57021. * Returns a function that creates the loader for
  57022. * either `ws` or `web`'s passes.
  57023. *
  57024. * Examples:
  57025. *
  57026. * httpProxy.createRightProxy('ws')
  57027. * // => [Function]
  57028. *
  57029. * @param {String} Type Either 'ws' or 'web'
  57030. * @return {Function} Loader Function that when called returns an iterator for the right passes
  57031. *
  57032. * @api private
  57033. */
  57034. function createRightProxy(type) {
  57035. return function(options) {
  57036. return function(req, res /*, [head], [opts] */) {
  57037. var passes = (type === 'ws') ? this.wsPasses : this.webPasses,
  57038. args = [].slice.call(arguments),
  57039. cntr = args.length - 1,
  57040. head, cbl;
  57041. /* optional args parse begin */
  57042. if(typeof args[cntr] === 'function') {
  57043. cbl = args[cntr];
  57044. cntr--;
  57045. }
  57046. var requestOptions = options;
  57047. if(
  57048. !(args[cntr] instanceof Buffer) &&
  57049. args[cntr] !== res
  57050. ) {
  57051. //Copy global options
  57052. requestOptions = Object.assign({}, options);
  57053. //Overwrite with request options
  57054. Object.assign(requestOptions, args[cntr]);
  57055. cntr--;
  57056. }
  57057. if(args[cntr] instanceof Buffer) {
  57058. head = args[cntr];
  57059. }
  57060. /* optional args parse end */
  57061. ['target', 'forward'].forEach(function(e) {
  57062. if (typeof requestOptions[e] === 'string')
  57063. requestOptions[e] = parse_url(requestOptions[e]);
  57064. });
  57065. if (!requestOptions.target && !requestOptions.forward) {
  57066. return this.emit('error', new Error('Must provide a proper URL as target'));
  57067. }
  57068. for(var i=0; i < passes.length; i++) {
  57069. /**
  57070. * Call of passes functions
  57071. * pass(req, res, options, head)
  57072. *
  57073. * In WebSockets case the `res` variable
  57074. * refer to the connection socket
  57075. * pass(req, socket, options, head)
  57076. */
  57077. if(passes[i](req, res, requestOptions, head, this, cbl)) { // passes can return a truthy value to halt the loop
  57078. break;
  57079. }
  57080. }
  57081. };
  57082. };
  57083. }
  57084. httpProxy.createRightProxy = createRightProxy;
  57085. function ProxyServer(options) {
  57086. EE3.call(this);
  57087. options = options || {};
  57088. options.prependPath = options.prependPath === false ? false : true;
  57089. this.web = this.proxyRequest = createRightProxy('web')(options);
  57090. this.ws = this.proxyWebsocketRequest = createRightProxy('ws')(options);
  57091. this.options = options;
  57092. this.webPasses = Object.keys(web).map(function(pass) {
  57093. return web[pass];
  57094. });
  57095. this.wsPasses = Object.keys(ws).map(function(pass) {
  57096. return ws[pass];
  57097. });
  57098. this.on('error', this.onError, this);
  57099. }
  57100. require$$0$5.inherits(ProxyServer, EE3);
  57101. ProxyServer.prototype.onError = function (err) {
  57102. //
  57103. // Remark: Replicate node core behavior using EE3
  57104. // so we force people to handle their own errors
  57105. //
  57106. if(this.listeners('error').length === 1) {
  57107. throw err;
  57108. }
  57109. };
  57110. ProxyServer.prototype.listen = function(port, hostname) {
  57111. var self = this,
  57112. closure = function(req, res) { self.web(req, res); };
  57113. this._server = this.options.ssl ?
  57114. https.createServer(this.options.ssl, closure) :
  57115. http.createServer(closure);
  57116. if(this.options.ws) {
  57117. this._server.on('upgrade', function(req, socket, head) { self.ws(req, socket, head); });
  57118. }
  57119. this._server.listen(port, hostname);
  57120. return this;
  57121. };
  57122. ProxyServer.prototype.close = function(callback) {
  57123. var self = this;
  57124. if (this._server) {
  57125. this._server.close(done);
  57126. }
  57127. // Wrap callback to nullify server after all open connections are closed.
  57128. function done() {
  57129. self._server = null;
  57130. if (callback) {
  57131. callback.apply(null, arguments);
  57132. }
  57133. } };
  57134. ProxyServer.prototype.before = function(type, passName, callback) {
  57135. if (type !== 'ws' && type !== 'web') {
  57136. throw new Error('type must be `web` or `ws`');
  57137. }
  57138. var passes = (type === 'ws') ? this.wsPasses : this.webPasses,
  57139. i = false;
  57140. passes.forEach(function(v, idx) {
  57141. if(v.name === passName) i = idx;
  57142. });
  57143. if(i === false) throw new Error('No such pass');
  57144. passes.splice(i, 0, callback);
  57145. };
  57146. ProxyServer.prototype.after = function(type, passName, callback) {
  57147. if (type !== 'ws' && type !== 'web') {
  57148. throw new Error('type must be `web` or `ws`');
  57149. }
  57150. var passes = (type === 'ws') ? this.wsPasses : this.webPasses,
  57151. i = false;
  57152. passes.forEach(function(v, idx) {
  57153. if(v.name === passName) i = idx;
  57154. });
  57155. if(i === false) throw new Error('No such pass');
  57156. passes.splice(i++, 0, callback);
  57157. };
  57158. } (httpProxy$3));
  57159. var httpProxyExports = httpProxy$3.exports;
  57160. // Use explicit /index.js to help browserify negociation in require '/lib/http-proxy' (!)
  57161. var ProxyServer = httpProxyExports.Server;
  57162. /**
  57163. * Creates the proxy server.
  57164. *
  57165. * Examples:
  57166. *
  57167. * httpProxy.createProxyServer({ .. }, 8000)
  57168. * // => '{ web: [Function], ws: [Function] ... }'
  57169. *
  57170. * @param {Object} Options Config object passed to the proxy
  57171. *
  57172. * @return {Object} Proxy Proxy object with handlers for `ws` and `web` requests
  57173. *
  57174. * @api public
  57175. */
  57176. function createProxyServer(options) {
  57177. /*
  57178. * `options` is needed and it must have the following layout:
  57179. *
  57180. * {
  57181. * target : <url string to be parsed with the url module>
  57182. * forward: <url string to be parsed with the url module>
  57183. * agent : <object to be passed to http(s).request>
  57184. * ssl : <object to be passed to https.createServer()>
  57185. * ws : <true/false, if you want to proxy websockets>
  57186. * xfwd : <true/false, adds x-forward headers>
  57187. * secure : <true/false, verify SSL certificate>
  57188. * toProxy: <true/false, explicitly specify if we are proxying to another proxy>
  57189. * prependPath: <true/false, Default: true - specify whether you want to prepend the target's path to the proxy path>
  57190. * ignorePath: <true/false, Default: false - specify whether you want to ignore the proxy path of the incoming request>
  57191. * localAddress : <Local interface string to bind for outgoing connections>
  57192. * changeOrigin: <true/false, Default: false - changes the origin of the host header to the target URL>
  57193. * preserveHeaderKeyCase: <true/false, Default: false - specify whether you want to keep letter case of response header key >
  57194. * auth : Basic authentication i.e. 'user:password' to compute an Authorization header.
  57195. * hostRewrite: rewrites the location hostname on (201/301/302/307/308) redirects, Default: null.
  57196. * autoRewrite: rewrites the location host/port on (201/301/302/307/308) redirects based on requested host/port. Default: false.
  57197. * protocolRewrite: rewrites the location protocol on (201/301/302/307/308) redirects to 'http' or 'https'. Default: null.
  57198. * }
  57199. *
  57200. * NOTE: `options.ws` and `options.ssl` are optional.
  57201. * `options.target and `options.forward` cannot be
  57202. * both missing
  57203. * }
  57204. */
  57205. return new ProxyServer(options);
  57206. }
  57207. ProxyServer.createProxyServer = createProxyServer;
  57208. ProxyServer.createServer = createProxyServer;
  57209. ProxyServer.createProxy = createProxyServer;
  57210. /**
  57211. * Export the proxy "Server" as the main export.
  57212. */
  57213. var httpProxy$2 = ProxyServer;
  57214. /*!
  57215. * Caron dimonio, con occhi di bragia
  57216. * loro accennando, tutte le raccoglie;
  57217. * batte col remo qualunque s’adagia
  57218. *
  57219. * Charon the demon, with the eyes of glede,
  57220. * Beckoning to them, collects them all together,
  57221. * Beats with his oar whoever lags behind
  57222. *
  57223. * Dante - The Divine Comedy (Canto III)
  57224. */
  57225. var httpProxy = httpProxy$2;
  57226. var httpProxy$1 = /*@__PURE__*/getDefaultExportFromCjs(httpProxy);
  57227. const debug$4 = createDebugger("vite:proxy");
  57228. const rewriteOriginHeader = (proxyReq, options, config) => {
  57229. if (options.rewriteWsOrigin) {
  57230. const { target } = options;
  57231. if (proxyReq.headersSent) {
  57232. config.logger.warn(
  57233. colors$1.yellow(
  57234. `Unable to rewrite Origin header as headers are already sent.`
  57235. )
  57236. );
  57237. return;
  57238. }
  57239. if (proxyReq.getHeader("origin") && target) {
  57240. const changedOrigin = typeof target === "object" ? `${target.protocol}//${target.host}` : target;
  57241. proxyReq.setHeader("origin", changedOrigin);
  57242. }
  57243. }
  57244. };
  57245. function proxyMiddleware(httpServer, options, config) {
  57246. const proxies = {};
  57247. Object.keys(options).forEach((context) => {
  57248. let opts = options[context];
  57249. if (!opts) {
  57250. return;
  57251. }
  57252. if (typeof opts === "string") {
  57253. opts = { target: opts, changeOrigin: true };
  57254. }
  57255. const proxy = httpProxy$1.createProxyServer(opts);
  57256. if (opts.configure) {
  57257. opts.configure(proxy, opts);
  57258. }
  57259. proxy.on("error", (err, req, originalRes) => {
  57260. const res = originalRes;
  57261. if (!res) {
  57262. config.logger.error(
  57263. `${colors$1.red(`http proxy error: ${err.message}`)}
  57264. ${err.stack}`,
  57265. {
  57266. timestamp: true,
  57267. error: err
  57268. }
  57269. );
  57270. } else if ("req" in res) {
  57271. config.logger.error(
  57272. `${colors$1.red(`http proxy error: ${originalRes.req.url}`)}
  57273. ${err.stack}`,
  57274. {
  57275. timestamp: true,
  57276. error: err
  57277. }
  57278. );
  57279. if (!res.headersSent && !res.writableEnded) {
  57280. res.writeHead(500, {
  57281. "Content-Type": "text/plain"
  57282. }).end();
  57283. }
  57284. } else {
  57285. config.logger.error(`${colors$1.red(`ws proxy error:`)}
  57286. ${err.stack}`, {
  57287. timestamp: true,
  57288. error: err
  57289. });
  57290. res.end();
  57291. }
  57292. });
  57293. proxy.on("proxyReqWs", (proxyReq, req, socket, options2, head) => {
  57294. rewriteOriginHeader(proxyReq, options2, config);
  57295. socket.on("error", (err) => {
  57296. config.logger.error(
  57297. `${colors$1.red(`ws proxy socket error:`)}
  57298. ${err.stack}`,
  57299. {
  57300. timestamp: true,
  57301. error: err
  57302. }
  57303. );
  57304. });
  57305. });
  57306. proxy.on("proxyRes", (proxyRes, req, res) => {
  57307. res.on("close", () => {
  57308. if (!res.writableEnded) {
  57309. debug$4?.("destroying proxyRes in proxyRes close event");
  57310. proxyRes.destroy();
  57311. }
  57312. });
  57313. });
  57314. proxies[context] = [proxy, { ...opts }];
  57315. });
  57316. if (httpServer) {
  57317. httpServer.on("upgrade", (req, socket, head) => {
  57318. const url = req.url;
  57319. for (const context in proxies) {
  57320. if (doesProxyContextMatchUrl(context, url)) {
  57321. const [proxy, opts] = proxies[context];
  57322. if (opts.ws || opts.target?.toString().startsWith("ws:") || opts.target?.toString().startsWith("wss:")) {
  57323. if (opts.rewrite) {
  57324. req.url = opts.rewrite(url);
  57325. }
  57326. debug$4?.(`${req.url} -> ws ${opts.target}`);
  57327. proxy.ws(req, socket, head);
  57328. return;
  57329. }
  57330. }
  57331. }
  57332. });
  57333. }
  57334. return function viteProxyMiddleware(req, res, next) {
  57335. const url = req.url;
  57336. for (const context in proxies) {
  57337. if (doesProxyContextMatchUrl(context, url)) {
  57338. const [proxy, opts] = proxies[context];
  57339. const options2 = {};
  57340. if (opts.bypass) {
  57341. const bypassResult = opts.bypass(req, res, opts);
  57342. if (typeof bypassResult === "string") {
  57343. req.url = bypassResult;
  57344. debug$4?.(`bypass: ${req.url} -> ${bypassResult}`);
  57345. return next();
  57346. } else if (bypassResult === false) {
  57347. debug$4?.(`bypass: ${req.url} -> 404`);
  57348. res.statusCode = 404;
  57349. return res.end();
  57350. }
  57351. }
  57352. debug$4?.(`${req.url} -> ${opts.target || opts.forward}`);
  57353. if (opts.rewrite) {
  57354. req.url = opts.rewrite(req.url);
  57355. }
  57356. proxy.web(req, res, options2);
  57357. return;
  57358. }
  57359. }
  57360. next();
  57361. };
  57362. }
  57363. function doesProxyContextMatchUrl(context, url) {
  57364. return context[0] === "^" && new RegExp(context).test(url) || url.startsWith(context);
  57365. }
  57366. const debug$3 = createDebugger("vite:html-fallback");
  57367. function htmlFallbackMiddleware(root, spaFallback, fsUtils = commonFsUtils) {
  57368. return function viteHtmlFallbackMiddleware(req, res, next) {
  57369. if (
  57370. // Only accept GET or HEAD
  57371. req.method !== "GET" && req.method !== "HEAD" || // Exclude default favicon requests
  57372. req.url === "/favicon.ico" || // Require Accept: text/html or */*
  57373. !(req.headers.accept === void 0 || // equivalent to `Accept: */*`
  57374. req.headers.accept === "" || // equivalent to `Accept: */*`
  57375. req.headers.accept.includes("text/html") || req.headers.accept.includes("*/*"))
  57376. ) {
  57377. return next();
  57378. }
  57379. const url = cleanUrl(req.url);
  57380. const pathname = decodeURIComponent(url);
  57381. if (pathname.endsWith(".html")) {
  57382. const filePath = path$n.join(root, pathname);
  57383. if (fsUtils.existsSync(filePath)) {
  57384. debug$3?.(`Rewriting ${req.method} ${req.url} to ${url}`);
  57385. req.url = url;
  57386. return next();
  57387. }
  57388. } else if (pathname[pathname.length - 1] === "/") {
  57389. const filePath = path$n.join(root, pathname, "index.html");
  57390. if (fsUtils.existsSync(filePath)) {
  57391. const newUrl = url + "index.html";
  57392. debug$3?.(`Rewriting ${req.method} ${req.url} to ${newUrl}`);
  57393. req.url = newUrl;
  57394. return next();
  57395. }
  57396. } else {
  57397. const filePath = path$n.join(root, pathname + ".html");
  57398. if (fsUtils.existsSync(filePath)) {
  57399. const newUrl = url + ".html";
  57400. debug$3?.(`Rewriting ${req.method} ${req.url} to ${newUrl}`);
  57401. req.url = newUrl;
  57402. return next();
  57403. }
  57404. }
  57405. if (spaFallback) {
  57406. debug$3?.(`Rewriting ${req.method} ${req.url} to /index.html`);
  57407. req.url = "/index.html";
  57408. }
  57409. next();
  57410. };
  57411. }
  57412. const debug$2 = createDebugger("vite:send", {
  57413. onlyWhenFocused: true
  57414. });
  57415. const alias = {
  57416. js: "text/javascript",
  57417. css: "text/css",
  57418. html: "text/html",
  57419. json: "application/json"
  57420. };
  57421. function send(req, res, content, type, options) {
  57422. const {
  57423. etag = getEtag(content, { weak: true }),
  57424. cacheControl = "no-cache",
  57425. headers,
  57426. map
  57427. } = options;
  57428. if (res.writableEnded) {
  57429. return;
  57430. }
  57431. if (req.headers["if-none-match"] === etag) {
  57432. res.statusCode = 304;
  57433. res.end();
  57434. return;
  57435. }
  57436. res.setHeader("Content-Type", alias[type] || type);
  57437. res.setHeader("Cache-Control", cacheControl);
  57438. res.setHeader("Etag", etag);
  57439. if (headers) {
  57440. for (const name in headers) {
  57441. res.setHeader(name, headers[name]);
  57442. }
  57443. }
  57444. if (map && "version" in map && map.mappings) {
  57445. if (type === "js" || type === "css") {
  57446. content = getCodeWithSourcemap(type, content.toString(), map);
  57447. }
  57448. } else if (type === "js" && (!map || map.mappings !== "")) {
  57449. const code = content.toString();
  57450. if (convertSourceMap.mapFileCommentRegex.test(code)) {
  57451. debug$2?.(`Skipped injecting fallback sourcemap for ${req.url}`);
  57452. } else {
  57453. const urlWithoutTimestamp = removeTimestampQuery(req.url);
  57454. const ms = new MagicString(code);
  57455. content = getCodeWithSourcemap(
  57456. type,
  57457. code,
  57458. ms.generateMap({
  57459. source: path$n.basename(urlWithoutTimestamp),
  57460. hires: "boundary",
  57461. includeContent: true
  57462. })
  57463. );
  57464. }
  57465. }
  57466. res.statusCode = 200;
  57467. res.end(content);
  57468. return;
  57469. }
  57470. const debugCache = createDebugger("vite:cache");
  57471. const knownIgnoreList = /* @__PURE__ */ new Set(["/", "/favicon.ico"]);
  57472. function cachedTransformMiddleware(server) {
  57473. return function viteCachedTransformMiddleware(req, res, next) {
  57474. const ifNoneMatch = req.headers["if-none-match"];
  57475. if (ifNoneMatch) {
  57476. const moduleByEtag = server.moduleGraph.getModuleByEtag(ifNoneMatch);
  57477. if (moduleByEtag?.transformResult?.etag === ifNoneMatch && moduleByEtag?.url === req.url) {
  57478. const maybeMixedEtag = isCSSRequest(req.url);
  57479. if (!maybeMixedEtag) {
  57480. debugCache?.(`[304] ${prettifyUrl(req.url, server.config.root)}`);
  57481. res.statusCode = 304;
  57482. return res.end();
  57483. }
  57484. }
  57485. }
  57486. next();
  57487. };
  57488. }
  57489. function transformMiddleware(server) {
  57490. const { root, publicDir } = server.config;
  57491. const publicDirInRoot = publicDir.startsWith(withTrailingSlash(root));
  57492. const publicPath = `${publicDir.slice(root.length)}/`;
  57493. return async function viteTransformMiddleware(req, res, next) {
  57494. if (req.method !== "GET" || knownIgnoreList.has(req.url)) {
  57495. return next();
  57496. }
  57497. let url;
  57498. try {
  57499. url = decodeURI(removeTimestampQuery(req.url)).replace(
  57500. NULL_BYTE_PLACEHOLDER,
  57501. "\0"
  57502. );
  57503. } catch (e) {
  57504. return next(e);
  57505. }
  57506. const withoutQuery = cleanUrl(url);
  57507. try {
  57508. const isSourceMap = withoutQuery.endsWith(".map");
  57509. if (isSourceMap) {
  57510. const depsOptimizer = getDepsOptimizer(server.config, false);
  57511. if (depsOptimizer?.isOptimizedDepUrl(url)) {
  57512. const sourcemapPath = url.startsWith(FS_PREFIX) ? fsPathFromId(url) : normalizePath$3(path$n.resolve(server.config.root, url.slice(1)));
  57513. try {
  57514. const map = JSON.parse(
  57515. await fsp.readFile(sourcemapPath, "utf-8")
  57516. );
  57517. applySourcemapIgnoreList(
  57518. map,
  57519. sourcemapPath,
  57520. server.config.server.sourcemapIgnoreList,
  57521. server.config.logger
  57522. );
  57523. return send(req, res, JSON.stringify(map), "json", {
  57524. headers: server.config.server.headers
  57525. });
  57526. } catch (e) {
  57527. const dummySourceMap = {
  57528. version: 3,
  57529. file: sourcemapPath.replace(/\.map$/, ""),
  57530. sources: [],
  57531. sourcesContent: [],
  57532. names: [],
  57533. mappings: ";;;;;;;;;"
  57534. };
  57535. return send(req, res, JSON.stringify(dummySourceMap), "json", {
  57536. cacheControl: "no-cache",
  57537. headers: server.config.server.headers
  57538. });
  57539. }
  57540. } else {
  57541. const originalUrl = url.replace(/\.map($|\?)/, "$1");
  57542. const map = (await server.moduleGraph.getModuleByUrl(originalUrl, false))?.transformResult?.map;
  57543. if (map) {
  57544. return send(req, res, JSON.stringify(map), "json", {
  57545. headers: server.config.server.headers
  57546. });
  57547. } else {
  57548. return next();
  57549. }
  57550. }
  57551. }
  57552. if (publicDirInRoot && url.startsWith(publicPath)) {
  57553. warnAboutExplicitPublicPathInUrl(url);
  57554. }
  57555. if ((rawRE.test(url) || urlRE.test(url)) && !ensureServingAccess(url, server, res, next)) {
  57556. return;
  57557. }
  57558. if (isJSRequest(url) || isImportRequest(url) || isCSSRequest(url) || isHTMLProxy(url)) {
  57559. url = removeImportQuery(url);
  57560. url = unwrapId$1(url);
  57561. if (isCSSRequest(url)) {
  57562. if (req.headers.accept?.includes("text/css") && !isDirectRequest(url)) {
  57563. url = injectQuery(url, "direct");
  57564. }
  57565. const ifNoneMatch = req.headers["if-none-match"];
  57566. if (ifNoneMatch && (await server.moduleGraph.getModuleByUrl(url, false))?.transformResult?.etag === ifNoneMatch) {
  57567. debugCache?.(`[304] ${prettifyUrl(url, server.config.root)}`);
  57568. res.statusCode = 304;
  57569. return res.end();
  57570. }
  57571. }
  57572. const result = await transformRequest(url, server, {
  57573. html: req.headers.accept?.includes("text/html")
  57574. });
  57575. if (result) {
  57576. const depsOptimizer = getDepsOptimizer(server.config, false);
  57577. const type = isDirectCSSRequest(url) ? "css" : "js";
  57578. const isDep = DEP_VERSION_RE.test(url) || depsOptimizer?.isOptimizedDepUrl(url);
  57579. return send(req, res, result.code, type, {
  57580. etag: result.etag,
  57581. // allow browser to cache npm deps!
  57582. cacheControl: isDep ? "max-age=31536000,immutable" : "no-cache",
  57583. headers: server.config.server.headers,
  57584. map: result.map
  57585. });
  57586. }
  57587. }
  57588. } catch (e) {
  57589. if (e?.code === ERR_OPTIMIZE_DEPS_PROCESSING_ERROR) {
  57590. if (!res.writableEnded) {
  57591. res.statusCode = 504;
  57592. res.statusMessage = "Optimize Deps Processing Error";
  57593. res.end();
  57594. }
  57595. server.config.logger.error(e.message);
  57596. return;
  57597. }
  57598. if (e?.code === ERR_OUTDATED_OPTIMIZED_DEP) {
  57599. if (!res.writableEnded) {
  57600. res.statusCode = 504;
  57601. res.statusMessage = "Outdated Optimize Dep";
  57602. res.end();
  57603. }
  57604. return;
  57605. }
  57606. if (e?.code === ERR_CLOSED_SERVER) {
  57607. if (!res.writableEnded) {
  57608. res.statusCode = 504;
  57609. res.statusMessage = "Outdated Request";
  57610. res.end();
  57611. }
  57612. return;
  57613. }
  57614. if (e?.code === ERR_FILE_NOT_FOUND_IN_OPTIMIZED_DEP_DIR) {
  57615. if (!res.writableEnded) {
  57616. res.statusCode = 404;
  57617. res.end();
  57618. }
  57619. server.config.logger.warn(colors$1.yellow(e.message));
  57620. return;
  57621. }
  57622. if (e?.code === ERR_LOAD_URL) {
  57623. return next();
  57624. }
  57625. return next(e);
  57626. }
  57627. next();
  57628. };
  57629. function warnAboutExplicitPublicPathInUrl(url) {
  57630. let warning;
  57631. if (isImportRequest(url)) {
  57632. const rawUrl = removeImportQuery(url);
  57633. if (urlRE.test(url)) {
  57634. warning = `Assets in the public directory are served at the root path.
  57635. Instead of ${colors$1.cyan(rawUrl)}, use ${colors$1.cyan(
  57636. rawUrl.replace(publicPath, "/")
  57637. )}.`;
  57638. } else {
  57639. warning = `Assets in public directory cannot be imported from JavaScript.
  57640. If you intend to import that asset, put the file in the src directory, and use ${colors$1.cyan(
  57641. rawUrl.replace(publicPath, "/src/")
  57642. )} instead of ${colors$1.cyan(rawUrl)}.
  57643. If you intend to use the URL of that asset, use ${colors$1.cyan(
  57644. injectQuery(rawUrl.replace(publicPath, "/"), "url")
  57645. )}.`;
  57646. }
  57647. } else {
  57648. warning = `Files in the public directory are served at the root path.
  57649. Instead of ${colors$1.cyan(url)}, use ${colors$1.cyan(
  57650. url.replace(publicPath, "/")
  57651. )}.`;
  57652. }
  57653. server.config.logger.warn(colors$1.yellow(warning));
  57654. }
  57655. }
  57656. function createDevHtmlTransformFn(config) {
  57657. const [preHooks, normalHooks, postHooks] = resolveHtmlTransforms(
  57658. config.plugins,
  57659. config.logger
  57660. );
  57661. const transformHooks = [
  57662. preImportMapHook(config),
  57663. injectCspNonceMetaTagHook(config),
  57664. ...preHooks,
  57665. htmlEnvHook(config),
  57666. devHtmlHook,
  57667. ...normalHooks,
  57668. ...postHooks,
  57669. injectNonceAttributeTagHook(config),
  57670. postImportMapHook()
  57671. ];
  57672. return (server, url, html, originalUrl) => {
  57673. return applyHtmlTransforms(html, transformHooks, {
  57674. path: url,
  57675. filename: getHtmlFilename(url, server),
  57676. server,
  57677. originalUrl
  57678. });
  57679. };
  57680. }
  57681. function getHtmlFilename(url, server) {
  57682. if (url.startsWith(FS_PREFIX)) {
  57683. return decodeURIComponent(fsPathFromId(url));
  57684. } else {
  57685. return decodeURIComponent(
  57686. normalizePath$3(path$n.join(server.config.root, url.slice(1)))
  57687. );
  57688. }
  57689. }
  57690. function shouldPreTransform(url, config) {
  57691. return !checkPublicFile(url, config) && (isJSRequest(url) || isCSSRequest(url));
  57692. }
  57693. const wordCharRE = /\w/;
  57694. function isBareRelative(url) {
  57695. return wordCharRE.test(url[0]) && !url.includes(":");
  57696. }
  57697. const isSrcSet = (attr) => attr.name === "srcset" && attr.prefix === void 0;
  57698. const processNodeUrl = (url, useSrcSetReplacer, config, htmlPath, originalUrl, server, isClassicScriptLink) => {
  57699. const replacer = (url2) => {
  57700. if (server?.moduleGraph) {
  57701. const mod = server.moduleGraph.urlToModuleMap.get(url2);
  57702. if (mod && mod.lastHMRTimestamp > 0) {
  57703. url2 = injectQuery(url2, `t=${mod.lastHMRTimestamp}`);
  57704. }
  57705. }
  57706. if (url2[0] === "/" && url2[1] !== "/" || // #3230 if some request url (localhost:3000/a/b) return to fallback html, the relative assets
  57707. // path will add `/a/` prefix, it will caused 404.
  57708. //
  57709. // skip if url contains `:` as it implies a url protocol or Windows path that we don't want to replace.
  57710. //
  57711. // rewrite `./index.js` -> `localhost:5173/a/index.js`.
  57712. // rewrite `../index.js` -> `localhost:5173/index.js`.
  57713. // rewrite `relative/index.js` -> `localhost:5173/a/relative/index.js`.
  57714. (url2[0] === "." || isBareRelative(url2)) && originalUrl && originalUrl !== "/" && htmlPath === "/index.html") {
  57715. url2 = path$n.posix.join(config.base, url2);
  57716. }
  57717. if (server && !isClassicScriptLink && shouldPreTransform(url2, config)) {
  57718. let preTransformUrl;
  57719. if (url2[0] === "/" && url2[1] !== "/") {
  57720. preTransformUrl = url2;
  57721. } else if (url2[0] === "." || isBareRelative(url2)) {
  57722. preTransformUrl = path$n.posix.join(
  57723. config.base,
  57724. path$n.posix.dirname(htmlPath),
  57725. url2
  57726. );
  57727. }
  57728. if (preTransformUrl) {
  57729. try {
  57730. preTransformUrl = decodeURI(preTransformUrl);
  57731. } catch (err) {
  57732. return url2;
  57733. }
  57734. preTransformRequest(server, preTransformUrl, config.decodedBase);
  57735. }
  57736. }
  57737. return url2;
  57738. };
  57739. const processedUrl = useSrcSetReplacer ? processSrcSetSync(url, ({ url: url2 }) => replacer(url2)) : replacer(url);
  57740. return processedUrl;
  57741. };
  57742. const devHtmlHook = async (html, { path: htmlPath, filename, server, originalUrl }) => {
  57743. const { config, moduleGraph, watcher } = server;
  57744. const base = config.base || "/";
  57745. const decodedBase = config.decodedBase || "/";
  57746. let proxyModulePath;
  57747. let proxyModuleUrl;
  57748. const trailingSlash = htmlPath.endsWith("/");
  57749. if (!trailingSlash && getFsUtils(config).existsSync(filename)) {
  57750. proxyModulePath = htmlPath;
  57751. proxyModuleUrl = proxyModulePath;
  57752. } else {
  57753. const validPath = `${htmlPath}${trailingSlash ? "index.html" : ""}`;
  57754. proxyModulePath = `\0${validPath}`;
  57755. proxyModuleUrl = wrapId$1(proxyModulePath);
  57756. }
  57757. proxyModuleUrl = joinUrlSegments(decodedBase, proxyModuleUrl);
  57758. const s = new MagicString(html);
  57759. let inlineModuleIndex = -1;
  57760. const proxyCacheUrl = decodeURI(
  57761. cleanUrl(proxyModulePath).replace(normalizePath$3(config.root), "")
  57762. );
  57763. const styleUrl = [];
  57764. const inlineStyles = [];
  57765. const addInlineModule = (node, ext) => {
  57766. inlineModuleIndex++;
  57767. const contentNode = node.childNodes[0];
  57768. const code = contentNode.value;
  57769. let map;
  57770. if (proxyModulePath[0] !== "\0") {
  57771. map = new MagicString(html).snip(
  57772. contentNode.sourceCodeLocation.startOffset,
  57773. contentNode.sourceCodeLocation.endOffset
  57774. ).generateMap({ hires: "boundary" });
  57775. map.sources = [filename];
  57776. map.file = filename;
  57777. }
  57778. addToHTMLProxyCache(config, proxyCacheUrl, inlineModuleIndex, { code, map });
  57779. const modulePath = `${proxyModuleUrl}?html-proxy&index=${inlineModuleIndex}.${ext}`;
  57780. const module = server?.moduleGraph.getModuleById(modulePath);
  57781. if (module) {
  57782. server?.moduleGraph.invalidateModule(module);
  57783. }
  57784. s.update(
  57785. node.sourceCodeLocation.startOffset,
  57786. node.sourceCodeLocation.endOffset,
  57787. `<script type="module" src="${modulePath}"><\/script>`
  57788. );
  57789. preTransformRequest(server, modulePath, decodedBase);
  57790. };
  57791. await traverseHtml(html, filename, (node) => {
  57792. if (!nodeIsElement(node)) {
  57793. return;
  57794. }
  57795. if (node.nodeName === "script") {
  57796. const { src, sourceCodeLocation, isModule } = getScriptInfo(node);
  57797. if (src) {
  57798. const processedUrl = processNodeUrl(
  57799. src.value,
  57800. isSrcSet(src),
  57801. config,
  57802. htmlPath,
  57803. originalUrl,
  57804. server,
  57805. !isModule
  57806. );
  57807. if (processedUrl !== src.value) {
  57808. overwriteAttrValue(s, sourceCodeLocation, processedUrl);
  57809. }
  57810. } else if (isModule && node.childNodes.length) {
  57811. addInlineModule(node, "js");
  57812. } else if (node.childNodes.length) {
  57813. const scriptNode = node.childNodes[node.childNodes.length - 1];
  57814. for (const {
  57815. url,
  57816. start,
  57817. end
  57818. } of extractImportExpressionFromClassicScript(scriptNode)) {
  57819. const processedUrl = processNodeUrl(
  57820. url,
  57821. false,
  57822. config,
  57823. htmlPath,
  57824. originalUrl
  57825. );
  57826. if (processedUrl !== url) {
  57827. s.update(start, end, processedUrl);
  57828. }
  57829. }
  57830. }
  57831. }
  57832. const inlineStyle = findNeedTransformStyleAttribute(node);
  57833. if (inlineStyle) {
  57834. inlineModuleIndex++;
  57835. inlineStyles.push({
  57836. index: inlineModuleIndex,
  57837. location: inlineStyle.location,
  57838. code: inlineStyle.attr.value
  57839. });
  57840. }
  57841. if (node.nodeName === "style" && node.childNodes.length) {
  57842. const children = node.childNodes[0];
  57843. styleUrl.push({
  57844. start: children.sourceCodeLocation.startOffset,
  57845. end: children.sourceCodeLocation.endOffset,
  57846. code: children.value
  57847. });
  57848. }
  57849. const assetAttrs = assetAttrsConfig[node.nodeName];
  57850. if (assetAttrs) {
  57851. for (const p of node.attrs) {
  57852. const attrKey = getAttrKey(p);
  57853. if (p.value && assetAttrs.includes(attrKey)) {
  57854. const processedUrl = processNodeUrl(
  57855. p.value,
  57856. isSrcSet(p),
  57857. config,
  57858. htmlPath,
  57859. originalUrl
  57860. );
  57861. if (processedUrl !== p.value) {
  57862. overwriteAttrValue(
  57863. s,
  57864. node.sourceCodeLocation.attrs[attrKey],
  57865. processedUrl
  57866. );
  57867. }
  57868. }
  57869. }
  57870. }
  57871. });
  57872. await Promise.all([
  57873. ...styleUrl.map(async ({ start, end, code }, index) => {
  57874. const url = `${proxyModulePath}?html-proxy&direct&index=${index}.css`;
  57875. const mod = await moduleGraph.ensureEntryFromUrl(url, false);
  57876. ensureWatchedFile(watcher, mod.file, config.root);
  57877. const result = await server.pluginContainer.transform(code, mod.id);
  57878. let content = "";
  57879. if (result) {
  57880. if (result.map && "version" in result.map) {
  57881. if (result.map.mappings) {
  57882. await injectSourcesContent(
  57883. result.map,
  57884. proxyModulePath,
  57885. config.logger
  57886. );
  57887. }
  57888. content = getCodeWithSourcemap("css", result.code, result.map);
  57889. } else {
  57890. content = result.code;
  57891. }
  57892. }
  57893. s.overwrite(start, end, content);
  57894. }),
  57895. ...inlineStyles.map(async ({ index, location, code }) => {
  57896. const url = `${proxyModulePath}?html-proxy&inline-css&style-attr&index=${index}.css`;
  57897. const mod = await moduleGraph.ensureEntryFromUrl(url, false);
  57898. ensureWatchedFile(watcher, mod.file, config.root);
  57899. await server?.pluginContainer.transform(code, mod.id);
  57900. const hash = getHash(cleanUrl(mod.id));
  57901. const result = htmlProxyResult.get(`${hash}_${index}`);
  57902. overwriteAttrValue(s, location, result ?? "");
  57903. })
  57904. ]);
  57905. html = s.toString();
  57906. return {
  57907. html,
  57908. tags: [
  57909. {
  57910. tag: "script",
  57911. attrs: {
  57912. type: "module",
  57913. src: path$n.posix.join(base, CLIENT_PUBLIC_PATH)
  57914. },
  57915. injectTo: "head-prepend"
  57916. }
  57917. ]
  57918. };
  57919. };
  57920. function indexHtmlMiddleware(root, server) {
  57921. const isDev = isDevServer(server);
  57922. const fsUtils = getFsUtils(server.config);
  57923. return async function viteIndexHtmlMiddleware(req, res, next) {
  57924. if (res.writableEnded) {
  57925. return next();
  57926. }
  57927. const url = req.url && cleanUrl(req.url);
  57928. if (url?.endsWith(".html") && req.headers["sec-fetch-dest"] !== "script") {
  57929. let filePath;
  57930. if (isDev && url.startsWith(FS_PREFIX)) {
  57931. filePath = decodeURIComponent(fsPathFromId(url));
  57932. } else {
  57933. filePath = path$n.join(root, decodeURIComponent(url));
  57934. }
  57935. if (fsUtils.existsSync(filePath)) {
  57936. const headers = isDev ? server.config.server.headers : server.config.preview.headers;
  57937. try {
  57938. let html = await fsp.readFile(filePath, "utf-8");
  57939. if (isDev) {
  57940. html = await server.transformIndexHtml(url, html, req.originalUrl);
  57941. }
  57942. return send(req, res, html, "html", { headers });
  57943. } catch (e) {
  57944. return next(e);
  57945. }
  57946. }
  57947. }
  57948. next();
  57949. };
  57950. }
  57951. function preTransformRequest(server, decodedUrl, decodedBase) {
  57952. if (!server.config.server.preTransformRequests) return;
  57953. decodedUrl = unwrapId$1(stripBase(decodedUrl, decodedBase));
  57954. server.warmupRequest(decodedUrl);
  57955. }
  57956. const logTime = createDebugger("vite:time");
  57957. function timeMiddleware(root) {
  57958. return function viteTimeMiddleware(req, res, next) {
  57959. const start = performance$1.now();
  57960. const end = res.end;
  57961. res.end = (...args) => {
  57962. logTime?.(`${timeFrom(start)} ${prettifyUrl(req.url, root)}`);
  57963. return end.call(res, ...args);
  57964. };
  57965. next();
  57966. };
  57967. }
  57968. class ModuleNode {
  57969. /**
  57970. * Public served url path, starts with /
  57971. */
  57972. url;
  57973. /**
  57974. * Resolved file system path + query
  57975. */
  57976. id = null;
  57977. file = null;
  57978. type;
  57979. info;
  57980. meta;
  57981. importers = /* @__PURE__ */ new Set();
  57982. clientImportedModules = /* @__PURE__ */ new Set();
  57983. ssrImportedModules = /* @__PURE__ */ new Set();
  57984. acceptedHmrDeps = /* @__PURE__ */ new Set();
  57985. acceptedHmrExports = null;
  57986. importedBindings = null;
  57987. isSelfAccepting;
  57988. transformResult = null;
  57989. ssrTransformResult = null;
  57990. ssrModule = null;
  57991. ssrError = null;
  57992. lastHMRTimestamp = 0;
  57993. /**
  57994. * `import.meta.hot.invalidate` is called by the client.
  57995. * If there's multiple clients, multiple `invalidate` request is received.
  57996. * This property is used to dedupe those request to avoid multiple updates happening.
  57997. * @internal
  57998. */
  57999. lastHMRInvalidationReceived = false;
  58000. lastInvalidationTimestamp = 0;
  58001. /**
  58002. * If the module only needs to update its imports timestamp (e.g. within an HMR chain),
  58003. * it is considered soft-invalidated. In this state, its `transformResult` should exist,
  58004. * and the next `transformRequest` for this module will replace the timestamps.
  58005. *
  58006. * By default the value is `undefined` if it's not soft/hard-invalidated. If it gets
  58007. * soft-invalidated, this will contain the previous `transformResult` value. If it gets
  58008. * hard-invalidated, this will be set to `'HARD_INVALIDATED'`.
  58009. * @internal
  58010. */
  58011. invalidationState;
  58012. /**
  58013. * @internal
  58014. */
  58015. ssrInvalidationState;
  58016. /**
  58017. * The module urls that are statically imported in the code. This information is separated
  58018. * out from `importedModules` as only importers that statically import the module can be
  58019. * soft invalidated. Other imports (e.g. watched files) needs the importer to be hard invalidated.
  58020. * @internal
  58021. */
  58022. staticImportedUrls;
  58023. /**
  58024. * @param setIsSelfAccepting - set `false` to set `isSelfAccepting` later. e.g. #7870
  58025. */
  58026. constructor(url, setIsSelfAccepting = true) {
  58027. this.url = url;
  58028. this.type = isDirectCSSRequest(url) ? "css" : "js";
  58029. if (setIsSelfAccepting) {
  58030. this.isSelfAccepting = false;
  58031. }
  58032. }
  58033. get importedModules() {
  58034. const importedModules = new Set(this.clientImportedModules);
  58035. for (const module of this.ssrImportedModules) {
  58036. importedModules.add(module);
  58037. }
  58038. return importedModules;
  58039. }
  58040. }
  58041. class ModuleGraph {
  58042. constructor(resolveId) {
  58043. this.resolveId = resolveId;
  58044. }
  58045. urlToModuleMap = /* @__PURE__ */ new Map();
  58046. idToModuleMap = /* @__PURE__ */ new Map();
  58047. etagToModuleMap = /* @__PURE__ */ new Map();
  58048. // a single file may corresponds to multiple modules with different queries
  58049. fileToModulesMap = /* @__PURE__ */ new Map();
  58050. safeModulesPath = /* @__PURE__ */ new Set();
  58051. /**
  58052. * @internal
  58053. */
  58054. _unresolvedUrlToModuleMap = /* @__PURE__ */ new Map();
  58055. /**
  58056. * @internal
  58057. */
  58058. _ssrUnresolvedUrlToModuleMap = /* @__PURE__ */ new Map();
  58059. /** @internal */
  58060. _hasResolveFailedErrorModules = /* @__PURE__ */ new Set();
  58061. async getModuleByUrl(rawUrl, ssr) {
  58062. rawUrl = removeImportQuery(removeTimestampQuery(rawUrl));
  58063. const mod = this._getUnresolvedUrlToModule(rawUrl, ssr);
  58064. if (mod) {
  58065. return mod;
  58066. }
  58067. const [url] = await this._resolveUrl(rawUrl, ssr);
  58068. return this.urlToModuleMap.get(url);
  58069. }
  58070. getModuleById(id) {
  58071. return this.idToModuleMap.get(removeTimestampQuery(id));
  58072. }
  58073. getModulesByFile(file) {
  58074. return this.fileToModulesMap.get(file);
  58075. }
  58076. onFileChange(file) {
  58077. const mods = this.getModulesByFile(file);
  58078. if (mods) {
  58079. const seen = /* @__PURE__ */ new Set();
  58080. mods.forEach((mod) => {
  58081. this.invalidateModule(mod, seen);
  58082. });
  58083. }
  58084. }
  58085. onFileDelete(file) {
  58086. const mods = this.getModulesByFile(file);
  58087. if (mods) {
  58088. mods.forEach((mod) => {
  58089. mod.importedModules.forEach((importedMod) => {
  58090. importedMod.importers.delete(mod);
  58091. });
  58092. });
  58093. }
  58094. }
  58095. invalidateModule(mod, seen = /* @__PURE__ */ new Set(), timestamp = Date.now(), isHmr = false, softInvalidate = false) {
  58096. const prevInvalidationState = mod.invalidationState;
  58097. const prevSsrInvalidationState = mod.ssrInvalidationState;
  58098. if (softInvalidate) {
  58099. mod.invalidationState ??= mod.transformResult ?? "HARD_INVALIDATED";
  58100. mod.ssrInvalidationState ??= mod.ssrTransformResult ?? "HARD_INVALIDATED";
  58101. } else {
  58102. mod.invalidationState = "HARD_INVALIDATED";
  58103. mod.ssrInvalidationState = "HARD_INVALIDATED";
  58104. }
  58105. if (seen.has(mod) && prevInvalidationState === mod.invalidationState && prevSsrInvalidationState === mod.ssrInvalidationState) {
  58106. return;
  58107. }
  58108. seen.add(mod);
  58109. if (isHmr) {
  58110. mod.lastHMRTimestamp = timestamp;
  58111. mod.lastHMRInvalidationReceived = false;
  58112. } else {
  58113. mod.lastInvalidationTimestamp = timestamp;
  58114. }
  58115. const etag = mod.transformResult?.etag;
  58116. if (etag) this.etagToModuleMap.delete(etag);
  58117. mod.transformResult = null;
  58118. mod.ssrTransformResult = null;
  58119. mod.ssrModule = null;
  58120. mod.ssrError = null;
  58121. mod.importers.forEach((importer) => {
  58122. if (!importer.acceptedHmrDeps.has(mod)) {
  58123. const shouldSoftInvalidateImporter = (importer.staticImportedUrls?.has(mod.url) || softInvalidate) && importer.type !== "css";
  58124. this.invalidateModule(
  58125. importer,
  58126. seen,
  58127. timestamp,
  58128. isHmr,
  58129. shouldSoftInvalidateImporter
  58130. );
  58131. }
  58132. });
  58133. this._hasResolveFailedErrorModules.delete(mod);
  58134. }
  58135. invalidateAll() {
  58136. const timestamp = Date.now();
  58137. const seen = /* @__PURE__ */ new Set();
  58138. this.idToModuleMap.forEach((mod) => {
  58139. this.invalidateModule(mod, seen, timestamp);
  58140. });
  58141. }
  58142. /**
  58143. * Update the module graph based on a module's updated imports information
  58144. * If there are dependencies that no longer have any importers, they are
  58145. * returned as a Set.
  58146. *
  58147. * @param staticImportedUrls Subset of `importedModules` where they're statically imported in code.
  58148. * This is only used for soft invalidations so `undefined` is fine but may cause more runtime processing.
  58149. */
  58150. async updateModuleInfo(mod, importedModules, importedBindings, acceptedModules, acceptedExports, isSelfAccepting, ssr, staticImportedUrls) {
  58151. mod.isSelfAccepting = isSelfAccepting;
  58152. const prevImports = ssr ? mod.ssrImportedModules : mod.clientImportedModules;
  58153. let noLongerImported;
  58154. let resolvePromises = [];
  58155. let resolveResults = new Array(importedModules.size);
  58156. let index = 0;
  58157. for (const imported of importedModules) {
  58158. const nextIndex = index++;
  58159. if (typeof imported === "string") {
  58160. resolvePromises.push(
  58161. this.ensureEntryFromUrl(imported, ssr).then((dep) => {
  58162. dep.importers.add(mod);
  58163. resolveResults[nextIndex] = dep;
  58164. })
  58165. );
  58166. } else {
  58167. imported.importers.add(mod);
  58168. resolveResults[nextIndex] = imported;
  58169. }
  58170. }
  58171. if (resolvePromises.length) {
  58172. await Promise.all(resolvePromises);
  58173. }
  58174. const nextImports = new Set(resolveResults);
  58175. if (ssr) {
  58176. mod.ssrImportedModules = nextImports;
  58177. } else {
  58178. mod.clientImportedModules = nextImports;
  58179. }
  58180. prevImports.forEach((dep) => {
  58181. if (!mod.clientImportedModules.has(dep) && !mod.ssrImportedModules.has(dep)) {
  58182. dep.importers.delete(mod);
  58183. if (!dep.importers.size) {
  58184. (noLongerImported || (noLongerImported = /* @__PURE__ */ new Set())).add(dep);
  58185. }
  58186. }
  58187. });
  58188. resolvePromises = [];
  58189. resolveResults = new Array(acceptedModules.size);
  58190. index = 0;
  58191. for (const accepted of acceptedModules) {
  58192. const nextIndex = index++;
  58193. if (typeof accepted === "string") {
  58194. resolvePromises.push(
  58195. this.ensureEntryFromUrl(accepted, ssr).then((dep) => {
  58196. resolveResults[nextIndex] = dep;
  58197. })
  58198. );
  58199. } else {
  58200. resolveResults[nextIndex] = accepted;
  58201. }
  58202. }
  58203. if (resolvePromises.length) {
  58204. await Promise.all(resolvePromises);
  58205. }
  58206. mod.acceptedHmrDeps = new Set(resolveResults);
  58207. mod.staticImportedUrls = staticImportedUrls;
  58208. mod.acceptedHmrExports = acceptedExports;
  58209. mod.importedBindings = importedBindings;
  58210. return noLongerImported;
  58211. }
  58212. async ensureEntryFromUrl(rawUrl, ssr, setIsSelfAccepting = true) {
  58213. return this._ensureEntryFromUrl(rawUrl, ssr, setIsSelfAccepting);
  58214. }
  58215. /**
  58216. * @internal
  58217. */
  58218. async _ensureEntryFromUrl(rawUrl, ssr, setIsSelfAccepting = true, resolved) {
  58219. rawUrl = removeImportQuery(removeTimestampQuery(rawUrl));
  58220. let mod = this._getUnresolvedUrlToModule(rawUrl, ssr);
  58221. if (mod) {
  58222. return mod;
  58223. }
  58224. const modPromise = (async () => {
  58225. const [url, resolvedId, meta] = await this._resolveUrl(
  58226. rawUrl,
  58227. ssr,
  58228. resolved
  58229. );
  58230. mod = this.idToModuleMap.get(resolvedId);
  58231. if (!mod) {
  58232. mod = new ModuleNode(url, setIsSelfAccepting);
  58233. if (meta) mod.meta = meta;
  58234. this.urlToModuleMap.set(url, mod);
  58235. mod.id = resolvedId;
  58236. this.idToModuleMap.set(resolvedId, mod);
  58237. const file = mod.file = cleanUrl(resolvedId);
  58238. let fileMappedModules = this.fileToModulesMap.get(file);
  58239. if (!fileMappedModules) {
  58240. fileMappedModules = /* @__PURE__ */ new Set();
  58241. this.fileToModulesMap.set(file, fileMappedModules);
  58242. }
  58243. fileMappedModules.add(mod);
  58244. } else if (!this.urlToModuleMap.has(url)) {
  58245. this.urlToModuleMap.set(url, mod);
  58246. }
  58247. this._setUnresolvedUrlToModule(rawUrl, mod, ssr);
  58248. return mod;
  58249. })();
  58250. this._setUnresolvedUrlToModule(rawUrl, modPromise, ssr);
  58251. return modPromise;
  58252. }
  58253. // some deps, like a css file referenced via @import, don't have its own
  58254. // url because they are inlined into the main css import. But they still
  58255. // need to be represented in the module graph so that they can trigger
  58256. // hmr in the importing css file.
  58257. createFileOnlyEntry(file) {
  58258. file = normalizePath$3(file);
  58259. let fileMappedModules = this.fileToModulesMap.get(file);
  58260. if (!fileMappedModules) {
  58261. fileMappedModules = /* @__PURE__ */ new Set();
  58262. this.fileToModulesMap.set(file, fileMappedModules);
  58263. }
  58264. const url = `${FS_PREFIX}${file}`;
  58265. for (const m of fileMappedModules) {
  58266. if (m.url === url || m.id === file) {
  58267. return m;
  58268. }
  58269. }
  58270. const mod = new ModuleNode(url);
  58271. mod.file = file;
  58272. fileMappedModules.add(mod);
  58273. return mod;
  58274. }
  58275. // for incoming urls, it is important to:
  58276. // 1. remove the HMR timestamp query (?t=xxxx) and the ?import query
  58277. // 2. resolve its extension so that urls with or without extension all map to
  58278. // the same module
  58279. async resolveUrl(url, ssr) {
  58280. url = removeImportQuery(removeTimestampQuery(url));
  58281. const mod = await this._getUnresolvedUrlToModule(url, ssr);
  58282. if (mod?.id) {
  58283. return [mod.url, mod.id, mod.meta];
  58284. }
  58285. return this._resolveUrl(url, ssr);
  58286. }
  58287. updateModuleTransformResult(mod, result, ssr) {
  58288. if (ssr) {
  58289. mod.ssrTransformResult = result;
  58290. } else {
  58291. const prevEtag = mod.transformResult?.etag;
  58292. if (prevEtag) this.etagToModuleMap.delete(prevEtag);
  58293. mod.transformResult = result;
  58294. if (result?.etag) this.etagToModuleMap.set(result.etag, mod);
  58295. }
  58296. }
  58297. getModuleByEtag(etag) {
  58298. return this.etagToModuleMap.get(etag);
  58299. }
  58300. /**
  58301. * @internal
  58302. */
  58303. _getUnresolvedUrlToModule(url, ssr) {
  58304. return (ssr ? this._ssrUnresolvedUrlToModuleMap : this._unresolvedUrlToModuleMap).get(url);
  58305. }
  58306. /**
  58307. * @internal
  58308. */
  58309. _setUnresolvedUrlToModule(url, mod, ssr) {
  58310. (ssr ? this._ssrUnresolvedUrlToModuleMap : this._unresolvedUrlToModuleMap).set(url, mod);
  58311. }
  58312. /**
  58313. * @internal
  58314. */
  58315. async _resolveUrl(url, ssr, alreadyResolved) {
  58316. const resolved = alreadyResolved ?? await this.resolveId(url, !!ssr);
  58317. const resolvedId = resolved?.id || url;
  58318. if (url !== resolvedId && !url.includes("\0") && !url.startsWith(`virtual:`)) {
  58319. const ext = extname$1(cleanUrl(resolvedId));
  58320. if (ext) {
  58321. const pathname = cleanUrl(url);
  58322. if (!pathname.endsWith(ext)) {
  58323. url = pathname + ext + url.slice(pathname.length);
  58324. }
  58325. }
  58326. }
  58327. return [url, resolvedId, resolved?.meta];
  58328. }
  58329. }
  58330. function notFoundMiddleware() {
  58331. return function vite404Middleware(_, res) {
  58332. res.statusCode = 404;
  58333. res.end();
  58334. };
  58335. }
  58336. const ROOT_FILES = [
  58337. // '.git',
  58338. // https://pnpm.io/workspaces/
  58339. "pnpm-workspace.yaml",
  58340. // https://rushjs.io/pages/advanced/config_files/
  58341. // 'rush.json',
  58342. // https://nx.dev/latest/react/getting-started/nx-setup
  58343. // 'workspace.json',
  58344. // 'nx.json',
  58345. // https://github.com/lerna/lerna#lernajson
  58346. "lerna.json"
  58347. ];
  58348. function hasWorkspacePackageJSON(root) {
  58349. const path = join$2(root, "package.json");
  58350. if (!isFileReadable(path)) {
  58351. return false;
  58352. }
  58353. try {
  58354. const content = JSON.parse(fs__default.readFileSync(path, "utf-8")) || {};
  58355. return !!content.workspaces;
  58356. } catch {
  58357. return false;
  58358. }
  58359. }
  58360. function hasRootFile(root) {
  58361. return ROOT_FILES.some((file) => fs__default.existsSync(join$2(root, file)));
  58362. }
  58363. function hasPackageJSON(root) {
  58364. const path = join$2(root, "package.json");
  58365. return fs__default.existsSync(path);
  58366. }
  58367. function searchForPackageRoot(current, root = current) {
  58368. if (hasPackageJSON(current)) return current;
  58369. const dir = dirname$2(current);
  58370. if (!dir || dir === current) return root;
  58371. return searchForPackageRoot(dir, root);
  58372. }
  58373. function searchForWorkspaceRoot(current, root = searchForPackageRoot(current)) {
  58374. if (hasRootFile(current)) return current;
  58375. if (hasWorkspacePackageJSON(current)) return current;
  58376. const dir = dirname$2(current);
  58377. if (!dir || dir === current) return root;
  58378. return searchForWorkspaceRoot(dir, root);
  58379. }
  58380. function warmupFiles(server) {
  58381. const options = server.config.server.warmup;
  58382. const root = server.config.root;
  58383. if (options?.clientFiles?.length) {
  58384. mapFiles(options.clientFiles, root).then((files) => {
  58385. for (const file of files) {
  58386. warmupFile(server, file, false);
  58387. }
  58388. });
  58389. }
  58390. if (options?.ssrFiles?.length) {
  58391. mapFiles(options.ssrFiles, root).then((files) => {
  58392. for (const file of files) {
  58393. warmupFile(server, file, true);
  58394. }
  58395. });
  58396. }
  58397. }
  58398. async function warmupFile(server, file, ssr) {
  58399. if (file.endsWith(".html")) {
  58400. const url = htmlFileToUrl(file, server.config.root);
  58401. if (url) {
  58402. try {
  58403. const html = await fsp.readFile(file, "utf-8");
  58404. await server.transformIndexHtml(url, html);
  58405. } catch (e) {
  58406. server.config.logger.error(
  58407. `Pre-transform error (${colors$1.cyan(file)}): ${e.message}`,
  58408. {
  58409. error: e,
  58410. timestamp: true
  58411. }
  58412. );
  58413. }
  58414. }
  58415. } else {
  58416. const url = fileToUrl(file, server.config.root);
  58417. await server.warmupRequest(url, { ssr });
  58418. }
  58419. }
  58420. function htmlFileToUrl(file, root) {
  58421. const url = path$n.relative(root, file);
  58422. if (url[0] === ".") return;
  58423. return "/" + normalizePath$3(url);
  58424. }
  58425. function fileToUrl(file, root) {
  58426. const url = path$n.relative(root, file);
  58427. if (url[0] === ".") {
  58428. return path$n.posix.join(FS_PREFIX, normalizePath$3(file));
  58429. }
  58430. return "/" + normalizePath$3(url);
  58431. }
  58432. function mapFiles(files, root) {
  58433. return glob(files, {
  58434. cwd: root,
  58435. absolute: true
  58436. });
  58437. }
  58438. function createServer(inlineConfig = {}) {
  58439. return _createServer(inlineConfig, { hotListen: true });
  58440. }
  58441. async function _createServer(inlineConfig = {}, options) {
  58442. const config = await resolveConfig(inlineConfig, "serve");
  58443. const initPublicFilesPromise = initPublicFiles(config);
  58444. const { root, server: serverConfig } = config;
  58445. const httpsOptions = await resolveHttpsConfig(config.server.https);
  58446. const { middlewareMode } = serverConfig;
  58447. const resolvedOutDirs = getResolvedOutDirs(
  58448. config.root,
  58449. config.build.outDir,
  58450. config.build.rollupOptions?.output
  58451. );
  58452. const emptyOutDir = resolveEmptyOutDir(
  58453. config.build.emptyOutDir,
  58454. config.root,
  58455. resolvedOutDirs
  58456. );
  58457. const resolvedWatchOptions = resolveChokidarOptions(
  58458. {
  58459. disableGlobbing: true,
  58460. ...serverConfig.watch
  58461. },
  58462. resolvedOutDirs,
  58463. emptyOutDir,
  58464. config.cacheDir
  58465. );
  58466. const middlewares = connect$1();
  58467. const httpServer = middlewareMode ? null : await resolveHttpServer(serverConfig, middlewares, httpsOptions);
  58468. const ws = createWebSocketServer(httpServer, config, httpsOptions);
  58469. const hot = createHMRBroadcaster().addChannel(ws).addChannel(createServerHMRChannel());
  58470. if (typeof config.server.hmr === "object" && config.server.hmr.channels) {
  58471. config.server.hmr.channels.forEach((channel) => hot.addChannel(channel));
  58472. }
  58473. const publicFiles = await initPublicFilesPromise;
  58474. const { publicDir } = config;
  58475. if (httpServer) {
  58476. setClientErrorHandler(httpServer, config.logger);
  58477. }
  58478. const watchEnabled = serverConfig.watch !== null;
  58479. const watcher = watchEnabled ? chokidar.watch(
  58480. // config file dependencies and env file might be outside of root
  58481. [
  58482. root,
  58483. ...config.configFileDependencies,
  58484. ...getEnvFilesForMode(config.mode, config.envDir),
  58485. // Watch the public directory explicitly because it might be outside
  58486. // of the root directory.
  58487. ...publicDir && publicFiles ? [publicDir] : []
  58488. ],
  58489. resolvedWatchOptions
  58490. ) : createNoopWatcher(resolvedWatchOptions);
  58491. const moduleGraph = new ModuleGraph(
  58492. (url, ssr) => container.resolveId(url, void 0, { ssr })
  58493. );
  58494. const container = await createPluginContainer(config, moduleGraph, watcher);
  58495. const closeHttpServer = createServerCloseFn(httpServer);
  58496. const devHtmlTransformFn = createDevHtmlTransformFn(config);
  58497. const onCrawlEndCallbacks = [];
  58498. const crawlEndFinder = setupOnCrawlEnd(() => {
  58499. onCrawlEndCallbacks.forEach((cb) => cb());
  58500. });
  58501. function waitForRequestsIdle(ignoredId) {
  58502. return crawlEndFinder.waitForRequestsIdle(ignoredId);
  58503. }
  58504. function _registerRequestProcessing(id, done) {
  58505. crawlEndFinder.registerRequestProcessing(id, done);
  58506. }
  58507. function _onCrawlEnd(cb) {
  58508. onCrawlEndCallbacks.push(cb);
  58509. }
  58510. let server = {
  58511. config,
  58512. middlewares,
  58513. httpServer,
  58514. watcher,
  58515. pluginContainer: container,
  58516. ws,
  58517. hot,
  58518. moduleGraph,
  58519. resolvedUrls: null,
  58520. // will be set on listen
  58521. ssrTransform(code, inMap, url, originalCode = code) {
  58522. return ssrTransform(code, inMap, url, originalCode, server.config);
  58523. },
  58524. transformRequest(url, options2) {
  58525. return transformRequest(url, server, options2);
  58526. },
  58527. async warmupRequest(url, options2) {
  58528. try {
  58529. await transformRequest(url, server, options2);
  58530. } catch (e) {
  58531. if (e?.code === ERR_OUTDATED_OPTIMIZED_DEP || e?.code === ERR_CLOSED_SERVER) {
  58532. return;
  58533. }
  58534. server.config.logger.error(`Pre-transform error: ${e.message}`, {
  58535. error: e,
  58536. timestamp: true
  58537. });
  58538. }
  58539. },
  58540. transformIndexHtml(url, html, originalUrl) {
  58541. return devHtmlTransformFn(server, url, html, originalUrl);
  58542. },
  58543. async ssrLoadModule(url, opts) {
  58544. return ssrLoadModule(url, server, opts?.fixStacktrace);
  58545. },
  58546. async ssrFetchModule(url, importer) {
  58547. return ssrFetchModule(server, url, importer);
  58548. },
  58549. ssrFixStacktrace(e) {
  58550. ssrFixStacktrace(e, moduleGraph);
  58551. },
  58552. ssrRewriteStacktrace(stack) {
  58553. return ssrRewriteStacktrace(stack, moduleGraph);
  58554. },
  58555. async reloadModule(module) {
  58556. if (serverConfig.hmr !== false && module.file) {
  58557. updateModules(module.file, [module], Date.now(), server);
  58558. }
  58559. },
  58560. async listen(port, isRestart) {
  58561. await startServer(server, port);
  58562. if (httpServer) {
  58563. server.resolvedUrls = await resolveServerUrls(
  58564. httpServer,
  58565. config.server,
  58566. config
  58567. );
  58568. if (!isRestart && config.server.open) server.openBrowser();
  58569. }
  58570. return server;
  58571. },
  58572. openBrowser() {
  58573. const options2 = server.config.server;
  58574. const url = server.resolvedUrls?.local[0] ?? server.resolvedUrls?.network[0];
  58575. if (url) {
  58576. const path2 = typeof options2.open === "string" ? new URL(options2.open, url).href : url;
  58577. if (server.config.server.preTransformRequests) {
  58578. setTimeout(() => {
  58579. const getMethod = path2.startsWith("https:") ? get$1 : get$2;
  58580. getMethod(
  58581. path2,
  58582. {
  58583. headers: {
  58584. // Allow the history middleware to redirect to /index.html
  58585. Accept: "text/html"
  58586. }
  58587. },
  58588. (res) => {
  58589. res.on("end", () => {
  58590. });
  58591. }
  58592. ).on("error", () => {
  58593. }).end();
  58594. }, 0);
  58595. }
  58596. openBrowser(path2, true, server.config.logger);
  58597. } else {
  58598. server.config.logger.warn("No URL available to open in browser");
  58599. }
  58600. },
  58601. async close() {
  58602. if (!middlewareMode) {
  58603. teardownSIGTERMListener(closeServerAndExit);
  58604. }
  58605. await Promise.allSettled([
  58606. watcher.close(),
  58607. hot.close(),
  58608. container.close(),
  58609. crawlEndFinder?.cancel(),
  58610. getDepsOptimizer(server.config)?.close(),
  58611. getDepsOptimizer(server.config, true)?.close(),
  58612. closeHttpServer()
  58613. ]);
  58614. while (server._pendingRequests.size > 0) {
  58615. await Promise.allSettled(
  58616. [...server._pendingRequests.values()].map(
  58617. (pending) => pending.request
  58618. )
  58619. );
  58620. }
  58621. server.resolvedUrls = null;
  58622. },
  58623. printUrls() {
  58624. if (server.resolvedUrls) {
  58625. printServerUrls(
  58626. server.resolvedUrls,
  58627. serverConfig.host,
  58628. config.logger.info
  58629. );
  58630. } else if (middlewareMode) {
  58631. throw new Error("cannot print server URLs in middleware mode.");
  58632. } else {
  58633. throw new Error(
  58634. "cannot print server URLs before server.listen is called."
  58635. );
  58636. }
  58637. },
  58638. bindCLIShortcuts(options2) {
  58639. bindCLIShortcuts(server, options2);
  58640. },
  58641. async restart(forceOptimize) {
  58642. if (!server._restartPromise) {
  58643. server._forceOptimizeOnRestart = !!forceOptimize;
  58644. server._restartPromise = restartServer(server).finally(() => {
  58645. server._restartPromise = null;
  58646. server._forceOptimizeOnRestart = false;
  58647. });
  58648. }
  58649. return server._restartPromise;
  58650. },
  58651. waitForRequestsIdle,
  58652. _registerRequestProcessing,
  58653. _onCrawlEnd,
  58654. _setInternalServer(_server) {
  58655. server = _server;
  58656. },
  58657. _restartPromise: null,
  58658. _importGlobMap: /* @__PURE__ */ new Map(),
  58659. _forceOptimizeOnRestart: false,
  58660. _pendingRequests: /* @__PURE__ */ new Map(),
  58661. _fsDenyGlob: picomatch$4(
  58662. // matchBase: true does not work as it's documented
  58663. // https://github.com/micromatch/picomatch/issues/89
  58664. // convert patterns without `/` on our side for now
  58665. config.server.fs.deny.map(
  58666. (pattern) => pattern.includes("/") ? pattern : `**/${pattern}`
  58667. ),
  58668. {
  58669. matchBase: false,
  58670. nocase: true,
  58671. dot: true
  58672. }
  58673. ),
  58674. _shortcutsOptions: void 0
  58675. };
  58676. const reflexServer = new Proxy(server, {
  58677. get: (_, property) => {
  58678. return server[property];
  58679. },
  58680. set: (_, property, value) => {
  58681. server[property] = value;
  58682. return true;
  58683. }
  58684. });
  58685. const closeServerAndExit = async () => {
  58686. try {
  58687. await server.close();
  58688. } finally {
  58689. process.exit();
  58690. }
  58691. };
  58692. if (!middlewareMode) {
  58693. setupSIGTERMListener(closeServerAndExit);
  58694. }
  58695. const onHMRUpdate = async (type, file) => {
  58696. if (serverConfig.hmr !== false) {
  58697. try {
  58698. await handleHMRUpdate(type, file, server);
  58699. } catch (err) {
  58700. hot.send({
  58701. type: "error",
  58702. err: prepareError(err)
  58703. });
  58704. }
  58705. }
  58706. };
  58707. const onFileAddUnlink = async (file, isUnlink) => {
  58708. file = normalizePath$3(file);
  58709. await container.watchChange(file, { event: isUnlink ? "delete" : "create" });
  58710. if (publicDir && publicFiles) {
  58711. if (file.startsWith(publicDir)) {
  58712. const path2 = file.slice(publicDir.length);
  58713. publicFiles[isUnlink ? "delete" : "add"](path2);
  58714. if (!isUnlink) {
  58715. const moduleWithSamePath = await moduleGraph.getModuleByUrl(path2);
  58716. const etag = moduleWithSamePath?.transformResult?.etag;
  58717. if (etag) {
  58718. moduleGraph.etagToModuleMap.delete(etag);
  58719. }
  58720. }
  58721. }
  58722. }
  58723. if (isUnlink) moduleGraph.onFileDelete(file);
  58724. await onHMRUpdate(isUnlink ? "delete" : "create", file);
  58725. };
  58726. watcher.on("change", async (file) => {
  58727. file = normalizePath$3(file);
  58728. await container.watchChange(file, { event: "update" });
  58729. moduleGraph.onFileChange(file);
  58730. await onHMRUpdate("update", file);
  58731. });
  58732. getFsUtils(config).initWatcher?.(watcher);
  58733. watcher.on("add", (file) => {
  58734. onFileAddUnlink(file, false);
  58735. });
  58736. watcher.on("unlink", (file) => {
  58737. onFileAddUnlink(file, true);
  58738. });
  58739. hot.on("vite:invalidate", async ({ path: path2, message }) => {
  58740. const mod = moduleGraph.urlToModuleMap.get(path2);
  58741. if (mod && mod.isSelfAccepting && mod.lastHMRTimestamp > 0 && !mod.lastHMRInvalidationReceived) {
  58742. mod.lastHMRInvalidationReceived = true;
  58743. config.logger.info(
  58744. colors$1.yellow(`hmr invalidate `) + colors$1.dim(path2) + (message ? ` ${message}` : ""),
  58745. { timestamp: true }
  58746. );
  58747. const file = getShortName(mod.file, config.root);
  58748. updateModules(
  58749. file,
  58750. [...mod.importers],
  58751. mod.lastHMRTimestamp,
  58752. server,
  58753. true
  58754. );
  58755. }
  58756. });
  58757. if (!middlewareMode && httpServer) {
  58758. httpServer.once("listening", () => {
  58759. serverConfig.port = httpServer.address().port;
  58760. });
  58761. }
  58762. const postHooks = [];
  58763. for (const hook of config.getSortedPluginHooks("configureServer")) {
  58764. postHooks.push(await hook(reflexServer));
  58765. }
  58766. if (process.env.DEBUG) {
  58767. middlewares.use(timeMiddleware(root));
  58768. }
  58769. const { cors } = serverConfig;
  58770. if (cors !== false) {
  58771. middlewares.use(corsMiddleware(typeof cors === "boolean" ? {} : cors));
  58772. }
  58773. middlewares.use(cachedTransformMiddleware(server));
  58774. const { proxy } = serverConfig;
  58775. if (proxy) {
  58776. const middlewareServer = (isObject$1(middlewareMode) ? middlewareMode.server : null) || httpServer;
  58777. middlewares.use(proxyMiddleware(middlewareServer, proxy, config));
  58778. }
  58779. if (config.base !== "/") {
  58780. middlewares.use(baseMiddleware(config.rawBase, !!middlewareMode));
  58781. }
  58782. middlewares.use("/__open-in-editor", launchEditorMiddleware$1());
  58783. middlewares.use(function viteHMRPingMiddleware(req, res, next) {
  58784. if (req.headers["accept"] === "text/x-vite-ping") {
  58785. res.writeHead(204).end();
  58786. } else {
  58787. next();
  58788. }
  58789. });
  58790. if (publicDir) {
  58791. middlewares.use(servePublicMiddleware(server, publicFiles));
  58792. }
  58793. middlewares.use(transformMiddleware(server));
  58794. middlewares.use(serveRawFsMiddleware(server));
  58795. middlewares.use(serveStaticMiddleware(server));
  58796. if (config.appType === "spa" || config.appType === "mpa") {
  58797. middlewares.use(
  58798. htmlFallbackMiddleware(
  58799. root,
  58800. config.appType === "spa",
  58801. getFsUtils(config)
  58802. )
  58803. );
  58804. }
  58805. postHooks.forEach((fn) => fn && fn());
  58806. if (config.appType === "spa" || config.appType === "mpa") {
  58807. middlewares.use(indexHtmlMiddleware(root, server));
  58808. middlewares.use(notFoundMiddleware());
  58809. }
  58810. middlewares.use(errorMiddleware(server, !!middlewareMode));
  58811. let initingServer;
  58812. let serverInited = false;
  58813. const initServer = async () => {
  58814. if (serverInited) return;
  58815. if (initingServer) return initingServer;
  58816. initingServer = async function() {
  58817. await container.buildStart({});
  58818. if (isDepsOptimizerEnabled(config, false)) {
  58819. await initDepsOptimizer(config, server);
  58820. }
  58821. warmupFiles(server);
  58822. initingServer = void 0;
  58823. serverInited = true;
  58824. }();
  58825. return initingServer;
  58826. };
  58827. if (!middlewareMode && httpServer) {
  58828. const listen = httpServer.listen.bind(httpServer);
  58829. httpServer.listen = async (port, ...args) => {
  58830. try {
  58831. hot.listen();
  58832. await initServer();
  58833. } catch (e) {
  58834. httpServer.emit("error", e);
  58835. return;
  58836. }
  58837. return listen(port, ...args);
  58838. };
  58839. } else {
  58840. if (options.hotListen) {
  58841. hot.listen();
  58842. }
  58843. await initServer();
  58844. }
  58845. return server;
  58846. }
  58847. async function startServer(server, inlinePort) {
  58848. const httpServer = server.httpServer;
  58849. if (!httpServer) {
  58850. throw new Error("Cannot call server.listen in middleware mode.");
  58851. }
  58852. const options = server.config.server;
  58853. const hostname = await resolveHostname(options.host);
  58854. const configPort = inlinePort ?? options.port;
  58855. const port = (!configPort || configPort === server._configServerPort ? server._currentServerPort : configPort) ?? DEFAULT_DEV_PORT;
  58856. server._configServerPort = configPort;
  58857. const serverPort = await httpServerStart(httpServer, {
  58858. port,
  58859. strictPort: options.strictPort,
  58860. host: hostname.host,
  58861. logger: server.config.logger
  58862. });
  58863. server._currentServerPort = serverPort;
  58864. }
  58865. function createServerCloseFn(server) {
  58866. if (!server) {
  58867. return () => Promise.resolve();
  58868. }
  58869. let hasListened = false;
  58870. const openSockets = /* @__PURE__ */ new Set();
  58871. server.on("connection", (socket) => {
  58872. openSockets.add(socket);
  58873. socket.on("close", () => {
  58874. openSockets.delete(socket);
  58875. });
  58876. });
  58877. server.once("listening", () => {
  58878. hasListened = true;
  58879. });
  58880. return () => new Promise((resolve, reject) => {
  58881. openSockets.forEach((s) => s.destroy());
  58882. if (hasListened) {
  58883. server.close((err) => {
  58884. if (err) {
  58885. reject(err);
  58886. } else {
  58887. resolve();
  58888. }
  58889. });
  58890. } else {
  58891. resolve();
  58892. }
  58893. });
  58894. }
  58895. function resolvedAllowDir(root, dir) {
  58896. return normalizePath$3(path$n.resolve(root, dir));
  58897. }
  58898. function resolveServerOptions(root, raw, logger) {
  58899. const server = {
  58900. preTransformRequests: true,
  58901. ...raw,
  58902. sourcemapIgnoreList: raw?.sourcemapIgnoreList === false ? () => false : raw?.sourcemapIgnoreList || isInNodeModules$1,
  58903. middlewareMode: raw?.middlewareMode || false
  58904. };
  58905. let allowDirs = server.fs?.allow;
  58906. const deny = server.fs?.deny || [".env", ".env.*", "*.{crt,pem}"];
  58907. if (!allowDirs) {
  58908. allowDirs = [searchForWorkspaceRoot(root)];
  58909. }
  58910. if (process.versions.pnp) {
  58911. try {
  58912. const enableGlobalCache = execSync("yarn config get enableGlobalCache", { cwd: root }).toString().trim() === "true";
  58913. const yarnCacheDir = execSync(
  58914. `yarn config get ${enableGlobalCache ? "globalFolder" : "cacheFolder"}`,
  58915. { cwd: root }
  58916. ).toString().trim();
  58917. allowDirs.push(yarnCacheDir);
  58918. } catch (e) {
  58919. logger.warn(`Get yarn cache dir error: ${e.message}`, {
  58920. timestamp: true
  58921. });
  58922. }
  58923. }
  58924. allowDirs = allowDirs.map((i) => resolvedAllowDir(root, i));
  58925. const resolvedClientDir = resolvedAllowDir(root, CLIENT_DIR);
  58926. if (!allowDirs.some((dir) => isParentDirectory(dir, resolvedClientDir))) {
  58927. allowDirs.push(resolvedClientDir);
  58928. }
  58929. server.fs = {
  58930. strict: server.fs?.strict ?? true,
  58931. allow: allowDirs,
  58932. deny,
  58933. cachedChecks: server.fs?.cachedChecks
  58934. };
  58935. if (server.origin?.endsWith("/")) {
  58936. server.origin = server.origin.slice(0, -1);
  58937. logger.warn(
  58938. colors$1.yellow(
  58939. `${colors$1.bold("(!)")} server.origin should not end with "/". Using "${server.origin}" instead.`
  58940. )
  58941. );
  58942. }
  58943. return server;
  58944. }
  58945. async function restartServer(server) {
  58946. global.__vite_start_time = performance$1.now();
  58947. const shortcutsOptions = server._shortcutsOptions;
  58948. let inlineConfig = server.config.inlineConfig;
  58949. if (server._forceOptimizeOnRestart) {
  58950. inlineConfig = mergeConfig(inlineConfig, {
  58951. optimizeDeps: {
  58952. force: true
  58953. }
  58954. });
  58955. }
  58956. {
  58957. let newServer = null;
  58958. try {
  58959. newServer = await _createServer(inlineConfig, { hotListen: false });
  58960. } catch (err) {
  58961. server.config.logger.error(err.message, {
  58962. timestamp: true
  58963. });
  58964. server.config.logger.error("server restart failed", { timestamp: true });
  58965. return;
  58966. }
  58967. await server.close();
  58968. const middlewares = server.middlewares;
  58969. newServer._configServerPort = server._configServerPort;
  58970. newServer._currentServerPort = server._currentServerPort;
  58971. Object.assign(server, newServer);
  58972. middlewares.stack = newServer.middlewares.stack;
  58973. server.middlewares = middlewares;
  58974. newServer._setInternalServer(server);
  58975. }
  58976. const {
  58977. logger,
  58978. server: { port, middlewareMode }
  58979. } = server.config;
  58980. if (!middlewareMode) {
  58981. await server.listen(port, true);
  58982. } else {
  58983. server.hot.listen();
  58984. }
  58985. logger.info("server restarted.", { timestamp: true });
  58986. if (shortcutsOptions) {
  58987. shortcutsOptions.print = false;
  58988. bindCLIShortcuts(server, shortcutsOptions);
  58989. }
  58990. }
  58991. async function restartServerWithUrls(server) {
  58992. if (server.config.server.middlewareMode) {
  58993. await server.restart();
  58994. return;
  58995. }
  58996. const { port: prevPort, host: prevHost } = server.config.server;
  58997. const prevUrls = server.resolvedUrls;
  58998. await server.restart();
  58999. const {
  59000. logger,
  59001. server: { port, host }
  59002. } = server.config;
  59003. if ((port ?? DEFAULT_DEV_PORT) !== (prevPort ?? DEFAULT_DEV_PORT) || host !== prevHost || diffDnsOrderChange(prevUrls, server.resolvedUrls)) {
  59004. logger.info("");
  59005. server.printUrls();
  59006. }
  59007. }
  59008. const callCrawlEndIfIdleAfterMs = 50;
  59009. function setupOnCrawlEnd(onCrawlEnd) {
  59010. const registeredIds = /* @__PURE__ */ new Set();
  59011. const seenIds = /* @__PURE__ */ new Set();
  59012. const onCrawlEndPromiseWithResolvers = promiseWithResolvers();
  59013. let timeoutHandle;
  59014. let cancelled = false;
  59015. function cancel() {
  59016. cancelled = true;
  59017. }
  59018. let crawlEndCalled = false;
  59019. function callOnCrawlEnd() {
  59020. if (!cancelled && !crawlEndCalled) {
  59021. crawlEndCalled = true;
  59022. onCrawlEnd();
  59023. }
  59024. onCrawlEndPromiseWithResolvers.resolve();
  59025. }
  59026. function registerRequestProcessing(id, done) {
  59027. if (!seenIds.has(id)) {
  59028. seenIds.add(id);
  59029. registeredIds.add(id);
  59030. done().catch(() => {
  59031. }).finally(() => markIdAsDone(id));
  59032. }
  59033. }
  59034. function waitForRequestsIdle(ignoredId) {
  59035. if (ignoredId) {
  59036. seenIds.add(ignoredId);
  59037. markIdAsDone(ignoredId);
  59038. } else {
  59039. checkIfCrawlEndAfterTimeout();
  59040. }
  59041. return onCrawlEndPromiseWithResolvers.promise;
  59042. }
  59043. function markIdAsDone(id) {
  59044. registeredIds.delete(id);
  59045. checkIfCrawlEndAfterTimeout();
  59046. }
  59047. function checkIfCrawlEndAfterTimeout() {
  59048. if (cancelled || registeredIds.size > 0) return;
  59049. if (timeoutHandle) clearTimeout(timeoutHandle);
  59050. timeoutHandle = setTimeout(
  59051. callOnCrawlEndWhenIdle,
  59052. callCrawlEndIfIdleAfterMs
  59053. );
  59054. }
  59055. async function callOnCrawlEndWhenIdle() {
  59056. if (cancelled || registeredIds.size > 0) return;
  59057. callOnCrawlEnd();
  59058. }
  59059. return {
  59060. registerRequestProcessing,
  59061. waitForRequestsIdle,
  59062. cancel
  59063. };
  59064. }
  59065. var index = {
  59066. __proto__: null,
  59067. _createServer: _createServer,
  59068. createServer: createServer,
  59069. createServerCloseFn: createServerCloseFn,
  59070. resolveServerOptions: resolveServerOptions,
  59071. restartServerWithUrls: restartServerWithUrls
  59072. };
  59073. const debugHmr = createDebugger("vite:hmr");
  59074. const whitespaceRE = /\s/;
  59075. const normalizedClientDir = normalizePath$3(CLIENT_DIR);
  59076. function getShortName(file, root) {
  59077. return file.startsWith(withTrailingSlash(root)) ? path$n.posix.relative(root, file) : file;
  59078. }
  59079. async function handleHMRUpdate(type, file, server) {
  59080. const { hot, config, moduleGraph } = server;
  59081. const shortFile = getShortName(file, config.root);
  59082. const isConfig = file === config.configFile;
  59083. const isConfigDependency = config.configFileDependencies.some(
  59084. (name) => file === name
  59085. );
  59086. const isEnv = config.inlineConfig.envFile !== false && getEnvFilesForMode(config.mode, config.envDir).includes(file);
  59087. if (isConfig || isConfigDependency || isEnv) {
  59088. debugHmr?.(`[config change] ${colors$1.dim(shortFile)}`);
  59089. config.logger.info(
  59090. colors$1.green(
  59091. `${normalizePath$3(
  59092. path$n.relative(process.cwd(), file)
  59093. )} changed, restarting server...`
  59094. ),
  59095. { clear: true, timestamp: true }
  59096. );
  59097. try {
  59098. await restartServerWithUrls(server);
  59099. } catch (e) {
  59100. config.logger.error(colors$1.red(e));
  59101. }
  59102. return;
  59103. }
  59104. debugHmr?.(`[file change] ${colors$1.dim(shortFile)}`);
  59105. if (file.startsWith(withTrailingSlash(normalizedClientDir))) {
  59106. hot.send({
  59107. type: "full-reload",
  59108. path: "*",
  59109. triggeredBy: path$n.resolve(config.root, file)
  59110. });
  59111. return;
  59112. }
  59113. const mods = new Set(moduleGraph.getModulesByFile(file));
  59114. if (type === "create") {
  59115. for (const mod of moduleGraph._hasResolveFailedErrorModules) {
  59116. mods.add(mod);
  59117. }
  59118. }
  59119. if (type === "create" || type === "delete") {
  59120. for (const mod of getAffectedGlobModules(file, server)) {
  59121. mods.add(mod);
  59122. }
  59123. }
  59124. const timestamp = Date.now();
  59125. const hmrContext = {
  59126. file,
  59127. timestamp,
  59128. modules: [...mods],
  59129. read: () => readModifiedFile(file),
  59130. server
  59131. };
  59132. if (type === "update") {
  59133. for (const hook of config.getSortedPluginHooks("handleHotUpdate")) {
  59134. const filteredModules = await hook(hmrContext);
  59135. if (filteredModules) {
  59136. hmrContext.modules = filteredModules;
  59137. }
  59138. }
  59139. }
  59140. if (!hmrContext.modules.length) {
  59141. if (file.endsWith(".html")) {
  59142. config.logger.info(colors$1.green(`page reload `) + colors$1.dim(shortFile), {
  59143. clear: true,
  59144. timestamp: true
  59145. });
  59146. hot.send({
  59147. type: "full-reload",
  59148. path: config.server.middlewareMode ? "*" : "/" + normalizePath$3(path$n.relative(config.root, file))
  59149. });
  59150. } else {
  59151. debugHmr?.(`[no modules matched] ${colors$1.dim(shortFile)}`);
  59152. }
  59153. return;
  59154. }
  59155. updateModules(shortFile, hmrContext.modules, timestamp, server);
  59156. }
  59157. function updateModules(file, modules, timestamp, { config, hot, moduleGraph }, afterInvalidation) {
  59158. const updates = [];
  59159. const invalidatedModules = /* @__PURE__ */ new Set();
  59160. const traversedModules = /* @__PURE__ */ new Set();
  59161. let needFullReload = modules.length === 0;
  59162. for (const mod of modules) {
  59163. const boundaries = [];
  59164. const hasDeadEnd = propagateUpdate(mod, traversedModules, boundaries);
  59165. moduleGraph.invalidateModule(mod, invalidatedModules, timestamp, true);
  59166. if (needFullReload) {
  59167. continue;
  59168. }
  59169. if (hasDeadEnd) {
  59170. needFullReload = hasDeadEnd;
  59171. continue;
  59172. }
  59173. updates.push(
  59174. ...boundaries.map(
  59175. ({ boundary, acceptedVia, isWithinCircularImport }) => ({
  59176. type: `${boundary.type}-update`,
  59177. timestamp,
  59178. path: normalizeHmrUrl(boundary.url),
  59179. acceptedPath: normalizeHmrUrl(acceptedVia.url),
  59180. explicitImportRequired: boundary.type === "js" ? isExplicitImportRequired(acceptedVia.url) : false,
  59181. isWithinCircularImport,
  59182. // browser modules are invalidated by changing ?t= query,
  59183. // but in ssr we control the module system, so we can directly remove them form cache
  59184. ssrInvalidates: getSSRInvalidatedImporters(acceptedVia)
  59185. })
  59186. )
  59187. );
  59188. }
  59189. if (needFullReload) {
  59190. const reason = typeof needFullReload === "string" ? colors$1.dim(` (${needFullReload})`) : "";
  59191. config.logger.info(
  59192. colors$1.green(`page reload `) + colors$1.dim(file) + reason,
  59193. { clear: !afterInvalidation, timestamp: true }
  59194. );
  59195. hot.send({
  59196. type: "full-reload",
  59197. triggeredBy: path$n.resolve(config.root, file)
  59198. });
  59199. return;
  59200. }
  59201. if (updates.length === 0) {
  59202. debugHmr?.(colors$1.yellow(`no update happened `) + colors$1.dim(file));
  59203. return;
  59204. }
  59205. config.logger.info(
  59206. colors$1.green(`hmr update `) + colors$1.dim([...new Set(updates.map((u) => u.path))].join(", ")),
  59207. { clear: !afterInvalidation, timestamp: true }
  59208. );
  59209. hot.send({
  59210. type: "update",
  59211. updates
  59212. });
  59213. }
  59214. function populateSSRImporters(module, timestamp, seen = /* @__PURE__ */ new Set()) {
  59215. module.ssrImportedModules.forEach((importer) => {
  59216. if (seen.has(importer)) {
  59217. return;
  59218. }
  59219. if (importer.lastHMRTimestamp === timestamp || importer.lastInvalidationTimestamp === timestamp) {
  59220. seen.add(importer);
  59221. populateSSRImporters(importer, timestamp, seen);
  59222. }
  59223. });
  59224. return seen;
  59225. }
  59226. function getSSRInvalidatedImporters(module) {
  59227. return [...populateSSRImporters(module, module.lastHMRTimestamp)].map(
  59228. (m) => m.file
  59229. );
  59230. }
  59231. function areAllImportsAccepted(importedBindings, acceptedExports) {
  59232. for (const binding of importedBindings) {
  59233. if (!acceptedExports.has(binding)) {
  59234. return false;
  59235. }
  59236. }
  59237. return true;
  59238. }
  59239. function propagateUpdate(node, traversedModules, boundaries, currentChain = [node]) {
  59240. if (traversedModules.has(node)) {
  59241. return false;
  59242. }
  59243. traversedModules.add(node);
  59244. if (node.id && node.isSelfAccepting === void 0) {
  59245. debugHmr?.(
  59246. `[propagate update] stop propagation because not analyzed: ${colors$1.dim(
  59247. node.id
  59248. )}`
  59249. );
  59250. return false;
  59251. }
  59252. if (node.isSelfAccepting) {
  59253. boundaries.push({
  59254. boundary: node,
  59255. acceptedVia: node,
  59256. isWithinCircularImport: isNodeWithinCircularImports(node, currentChain)
  59257. });
  59258. for (const importer of node.importers) {
  59259. if (isCSSRequest(importer.url) && !currentChain.includes(importer)) {
  59260. propagateUpdate(
  59261. importer,
  59262. traversedModules,
  59263. boundaries,
  59264. currentChain.concat(importer)
  59265. );
  59266. }
  59267. }
  59268. return false;
  59269. }
  59270. if (node.acceptedHmrExports) {
  59271. boundaries.push({
  59272. boundary: node,
  59273. acceptedVia: node,
  59274. isWithinCircularImport: isNodeWithinCircularImports(node, currentChain)
  59275. });
  59276. } else {
  59277. if (!node.importers.size) {
  59278. return true;
  59279. }
  59280. if (!isCSSRequest(node.url) && [...node.importers].every((i) => isCSSRequest(i.url))) {
  59281. return true;
  59282. }
  59283. }
  59284. for (const importer of node.importers) {
  59285. const subChain = currentChain.concat(importer);
  59286. if (importer.acceptedHmrDeps.has(node)) {
  59287. boundaries.push({
  59288. boundary: importer,
  59289. acceptedVia: node,
  59290. isWithinCircularImport: isNodeWithinCircularImports(importer, subChain)
  59291. });
  59292. continue;
  59293. }
  59294. if (node.id && node.acceptedHmrExports && importer.importedBindings) {
  59295. const importedBindingsFromNode = importer.importedBindings.get(node.id);
  59296. if (importedBindingsFromNode && areAllImportsAccepted(importedBindingsFromNode, node.acceptedHmrExports)) {
  59297. continue;
  59298. }
  59299. }
  59300. if (!currentChain.includes(importer) && propagateUpdate(importer, traversedModules, boundaries, subChain)) {
  59301. return true;
  59302. }
  59303. }
  59304. return false;
  59305. }
  59306. function isNodeWithinCircularImports(node, nodeChain, currentChain = [node], traversedModules = /* @__PURE__ */ new Set()) {
  59307. if (traversedModules.has(node)) {
  59308. return false;
  59309. }
  59310. traversedModules.add(node);
  59311. for (const importer of node.importers) {
  59312. if (importer === node) continue;
  59313. if (isCSSRequest(importer.url)) continue;
  59314. const importerIndex = nodeChain.indexOf(importer);
  59315. if (importerIndex > -1) {
  59316. if (debugHmr) {
  59317. const importChain = [
  59318. importer,
  59319. ...[...currentChain].reverse(),
  59320. ...nodeChain.slice(importerIndex, -1).reverse()
  59321. ];
  59322. debugHmr(
  59323. colors$1.yellow(`circular imports detected: `) + importChain.map((m) => colors$1.dim(m.url)).join(" -> ")
  59324. );
  59325. }
  59326. return true;
  59327. }
  59328. if (!currentChain.includes(importer)) {
  59329. const result = isNodeWithinCircularImports(
  59330. importer,
  59331. nodeChain,
  59332. currentChain.concat(importer),
  59333. traversedModules
  59334. );
  59335. if (result) return result;
  59336. }
  59337. }
  59338. return false;
  59339. }
  59340. function handlePrunedModules(mods, { hot }) {
  59341. const t = Date.now();
  59342. mods.forEach((mod) => {
  59343. mod.lastHMRTimestamp = t;
  59344. mod.lastHMRInvalidationReceived = false;
  59345. debugHmr?.(`[dispose] ${colors$1.dim(mod.file)}`);
  59346. });
  59347. hot.send({
  59348. type: "prune",
  59349. paths: [...mods].map((m) => m.url)
  59350. });
  59351. }
  59352. function lexAcceptedHmrDeps(code, start, urls) {
  59353. let state = 0 /* inCall */;
  59354. let prevState = 0 /* inCall */;
  59355. let currentDep = "";
  59356. function addDep(index) {
  59357. urls.add({
  59358. url: currentDep,
  59359. start: index - currentDep.length - 1,
  59360. end: index + 1
  59361. });
  59362. currentDep = "";
  59363. }
  59364. for (let i = start; i < code.length; i++) {
  59365. const char = code.charAt(i);
  59366. switch (state) {
  59367. case 0 /* inCall */:
  59368. case 4 /* inArray */:
  59369. if (char === `'`) {
  59370. prevState = state;
  59371. state = 1 /* inSingleQuoteString */;
  59372. } else if (char === `"`) {
  59373. prevState = state;
  59374. state = 2 /* inDoubleQuoteString */;
  59375. } else if (char === "`") {
  59376. prevState = state;
  59377. state = 3 /* inTemplateString */;
  59378. } else if (whitespaceRE.test(char)) {
  59379. continue;
  59380. } else {
  59381. if (state === 0 /* inCall */) {
  59382. if (char === `[`) {
  59383. state = 4 /* inArray */;
  59384. } else {
  59385. return true;
  59386. }
  59387. } else if (state === 4 /* inArray */) {
  59388. if (char === `]`) {
  59389. return false;
  59390. } else if (char === ",") {
  59391. continue;
  59392. } else {
  59393. error(i);
  59394. }
  59395. }
  59396. }
  59397. break;
  59398. case 1 /* inSingleQuoteString */:
  59399. if (char === `'`) {
  59400. addDep(i);
  59401. if (prevState === 0 /* inCall */) {
  59402. return false;
  59403. } else {
  59404. state = prevState;
  59405. }
  59406. } else {
  59407. currentDep += char;
  59408. }
  59409. break;
  59410. case 2 /* inDoubleQuoteString */:
  59411. if (char === `"`) {
  59412. addDep(i);
  59413. if (prevState === 0 /* inCall */) {
  59414. return false;
  59415. } else {
  59416. state = prevState;
  59417. }
  59418. } else {
  59419. currentDep += char;
  59420. }
  59421. break;
  59422. case 3 /* inTemplateString */:
  59423. if (char === "`") {
  59424. addDep(i);
  59425. if (prevState === 0 /* inCall */) {
  59426. return false;
  59427. } else {
  59428. state = prevState;
  59429. }
  59430. } else if (char === "$" && code.charAt(i + 1) === "{") {
  59431. error(i);
  59432. } else {
  59433. currentDep += char;
  59434. }
  59435. break;
  59436. default:
  59437. throw new Error("unknown import.meta.hot lexer state");
  59438. }
  59439. }
  59440. return false;
  59441. }
  59442. function lexAcceptedHmrExports(code, start, exportNames) {
  59443. const urls = /* @__PURE__ */ new Set();
  59444. lexAcceptedHmrDeps(code, start, urls);
  59445. for (const { url } of urls) {
  59446. exportNames.add(url);
  59447. }
  59448. return urls.size > 0;
  59449. }
  59450. function normalizeHmrUrl(url) {
  59451. if (url[0] !== "." && url[0] !== "/") {
  59452. url = wrapId$1(url);
  59453. }
  59454. return url;
  59455. }
  59456. function error(pos) {
  59457. const err = new Error(
  59458. `import.meta.hot.accept() can only accept string literals or an Array of string literals.`
  59459. );
  59460. err.pos = pos;
  59461. throw err;
  59462. }
  59463. async function readModifiedFile(file) {
  59464. const content = await fsp.readFile(file, "utf-8");
  59465. if (!content) {
  59466. const mtime = (await fsp.stat(file)).mtimeMs;
  59467. for (let n = 0; n < 10; n++) {
  59468. await new Promise((r) => setTimeout(r, 10));
  59469. const newMtime = (await fsp.stat(file)).mtimeMs;
  59470. if (newMtime !== mtime) {
  59471. break;
  59472. }
  59473. }
  59474. return await fsp.readFile(file, "utf-8");
  59475. } else {
  59476. return content;
  59477. }
  59478. }
  59479. function createHMRBroadcaster() {
  59480. const channels = [];
  59481. const readyChannels = /* @__PURE__ */ new WeakSet();
  59482. const broadcaster = {
  59483. get channels() {
  59484. return [...channels];
  59485. },
  59486. addChannel(channel) {
  59487. if (channels.some((c) => c.name === channel.name)) {
  59488. throw new Error(`HMR channel "${channel.name}" is already defined.`);
  59489. }
  59490. channels.push(channel);
  59491. return broadcaster;
  59492. },
  59493. on(event, listener) {
  59494. if (event === "connection") {
  59495. const channels2 = this.channels;
  59496. channels2.forEach(
  59497. (channel) => channel.on("connection", () => {
  59498. readyChannels.add(channel);
  59499. if (channels2.every((c) => readyChannels.has(c))) {
  59500. listener();
  59501. }
  59502. })
  59503. );
  59504. return;
  59505. }
  59506. channels.forEach((channel) => channel.on(event, listener));
  59507. return;
  59508. },
  59509. off(event, listener) {
  59510. channels.forEach((channel) => channel.off(event, listener));
  59511. return;
  59512. },
  59513. send(...args) {
  59514. channels.forEach((channel) => channel.send(...args));
  59515. },
  59516. listen() {
  59517. channels.forEach((channel) => channel.listen());
  59518. },
  59519. close() {
  59520. return Promise.all(channels.map((channel) => channel.close()));
  59521. }
  59522. };
  59523. return broadcaster;
  59524. }
  59525. function createServerHMRChannel() {
  59526. const innerEmitter = new EventEmitter$4();
  59527. const outsideEmitter = new EventEmitter$4();
  59528. return {
  59529. name: "ssr",
  59530. send(...args) {
  59531. let payload;
  59532. if (typeof args[0] === "string") {
  59533. payload = {
  59534. type: "custom",
  59535. event: args[0],
  59536. data: args[1]
  59537. };
  59538. } else {
  59539. payload = args[0];
  59540. }
  59541. outsideEmitter.emit("send", payload);
  59542. },
  59543. off(event, listener) {
  59544. innerEmitter.off(event, listener);
  59545. },
  59546. on: (event, listener) => {
  59547. innerEmitter.on(event, listener);
  59548. },
  59549. close() {
  59550. innerEmitter.removeAllListeners();
  59551. outsideEmitter.removeAllListeners();
  59552. },
  59553. listen() {
  59554. innerEmitter.emit("connection");
  59555. },
  59556. api: {
  59557. innerEmitter,
  59558. outsideEmitter
  59559. }
  59560. };
  59561. }
  59562. const debug$1 = createDebugger("vite:import-analysis");
  59563. const clientDir = normalizePath$3(CLIENT_DIR);
  59564. const skipRE = /\.(?:map|json)(?:$|\?)/;
  59565. const canSkipImportAnalysis = (id) => skipRE.test(id) || isDirectCSSRequest(id);
  59566. const optimizedDepChunkRE = /\/chunk-[A-Z\d]{8}\.js/;
  59567. const optimizedDepDynamicRE = /-[A-Z\d]{8}\.js/;
  59568. const hasViteIgnoreRE = /\/\*\s*@vite-ignore\s*\*\//;
  59569. const urlIsStringRE = /^(?:'.*'|".*"|`.*`)$/;
  59570. const templateLiteralRE = /^\s*`(.*)`\s*$/;
  59571. function isExplicitImportRequired(url) {
  59572. return !isJSRequest(url) && !isCSSRequest(url);
  59573. }
  59574. function extractImportedBindings(id, source, importSpec, importedBindings) {
  59575. let bindings = importedBindings.get(id);
  59576. if (!bindings) {
  59577. bindings = /* @__PURE__ */ new Set();
  59578. importedBindings.set(id, bindings);
  59579. }
  59580. const isDynamic = importSpec.d > -1;
  59581. const isMeta = importSpec.d === -2;
  59582. if (isDynamic || isMeta) {
  59583. bindings.add("*");
  59584. return;
  59585. }
  59586. const exp = source.slice(importSpec.ss, importSpec.se);
  59587. ESM_STATIC_IMPORT_RE.lastIndex = 0;
  59588. const match = ESM_STATIC_IMPORT_RE.exec(exp);
  59589. if (!match) {
  59590. return;
  59591. }
  59592. const staticImport = {
  59593. type: "static",
  59594. code: match[0],
  59595. start: match.index,
  59596. end: match.index + match[0].length,
  59597. imports: match.groups.imports,
  59598. specifier: match.groups.specifier
  59599. };
  59600. const parsed = parseStaticImport(staticImport);
  59601. if (!parsed) {
  59602. return;
  59603. }
  59604. if (parsed.namespacedImport) {
  59605. bindings.add("*");
  59606. }
  59607. if (parsed.defaultImport) {
  59608. bindings.add("default");
  59609. }
  59610. if (parsed.namedImports) {
  59611. for (const name of Object.keys(parsed.namedImports)) {
  59612. bindings.add(name);
  59613. }
  59614. }
  59615. }
  59616. function importAnalysisPlugin(config) {
  59617. const { root, base } = config;
  59618. const fsUtils = getFsUtils(config);
  59619. const clientPublicPath = path$n.posix.join(base, CLIENT_PUBLIC_PATH);
  59620. const enablePartialAccept = config.experimental?.hmrPartialAccept;
  59621. const matchAlias = getAliasPatternMatcher(config.resolve.alias);
  59622. let server;
  59623. let _env;
  59624. let _ssrEnv;
  59625. function getEnv(ssr) {
  59626. if (!_ssrEnv || !_env) {
  59627. const importMetaEnvKeys = {};
  59628. const userDefineEnv = {};
  59629. for (const key in config.env) {
  59630. importMetaEnvKeys[key] = JSON.stringify(config.env[key]);
  59631. }
  59632. for (const key in config.define) {
  59633. if (key.startsWith("import.meta.env.")) {
  59634. userDefineEnv[key.slice(16)] = config.define[key];
  59635. }
  59636. }
  59637. const env = `import.meta.env = ${serializeDefine({
  59638. ...importMetaEnvKeys,
  59639. SSR: "__vite_ssr__",
  59640. ...userDefineEnv
  59641. })};`;
  59642. _ssrEnv = env.replace("__vite_ssr__", "true");
  59643. _env = env.replace("__vite_ssr__", "false");
  59644. }
  59645. return ssr ? _ssrEnv : _env;
  59646. }
  59647. return {
  59648. name: "vite:import-analysis",
  59649. configureServer(_server) {
  59650. server = _server;
  59651. },
  59652. async transform(source, importer, options) {
  59653. if (!server) {
  59654. return null;
  59655. }
  59656. const ssr = options?.ssr === true;
  59657. if (canSkipImportAnalysis(importer)) {
  59658. debug$1?.(colors$1.dim(`[skipped] ${prettifyUrl(importer, root)}`));
  59659. return null;
  59660. }
  59661. const msAtStart = debug$1 ? performance$1.now() : 0;
  59662. await init;
  59663. let imports;
  59664. let exports;
  59665. source = stripBomTag(source);
  59666. try {
  59667. [imports, exports] = parse$d(source);
  59668. } catch (_e) {
  59669. const e = _e;
  59670. const { message, showCodeFrame } = createParseErrorInfo(
  59671. importer,
  59672. source
  59673. );
  59674. this.error(message, showCodeFrame ? e.idx : void 0);
  59675. }
  59676. const depsOptimizer = getDepsOptimizer(config, ssr);
  59677. const { moduleGraph } = server;
  59678. const importerModule = moduleGraph.getModuleById(importer);
  59679. if (!importerModule) {
  59680. throwOutdatedRequest(importer);
  59681. }
  59682. if (!imports.length && !this._addedImports) {
  59683. importerModule.isSelfAccepting = false;
  59684. debug$1?.(
  59685. `${timeFrom(msAtStart)} ${colors$1.dim(
  59686. `[no imports] ${prettifyUrl(importer, root)}`
  59687. )}`
  59688. );
  59689. return source;
  59690. }
  59691. let hasHMR = false;
  59692. let isSelfAccepting = false;
  59693. let hasEnv = false;
  59694. let needQueryInjectHelper = false;
  59695. let s;
  59696. const str = () => s || (s = new MagicString(source));
  59697. let isPartiallySelfAccepting = false;
  59698. const importedBindings = enablePartialAccept ? /* @__PURE__ */ new Map() : null;
  59699. const toAbsoluteUrl = (url) => path$n.posix.resolve(path$n.posix.dirname(importerModule.url), url);
  59700. const normalizeUrl = async (url, pos, forceSkipImportAnalysis = false) => {
  59701. url = stripBase(url, base);
  59702. let importerFile = importer;
  59703. const optimizeDeps = getDepOptimizationConfig(config, ssr);
  59704. if (moduleListContains(optimizeDeps?.exclude, url)) {
  59705. if (depsOptimizer) {
  59706. await depsOptimizer.scanProcessing;
  59707. for (const optimizedModule of depsOptimizer.metadata.depInfoList) {
  59708. if (!optimizedModule.src) continue;
  59709. if (optimizedModule.file === importerModule.file) {
  59710. importerFile = optimizedModule.src;
  59711. }
  59712. }
  59713. }
  59714. }
  59715. const resolved = await this.resolve(url, importerFile);
  59716. if (!resolved || resolved.meta?.["vite:alias"]?.noResolved) {
  59717. if (ssr) {
  59718. return [url, url];
  59719. }
  59720. importerModule.isSelfAccepting = false;
  59721. moduleGraph._hasResolveFailedErrorModules.add(importerModule);
  59722. return this.error(
  59723. `Failed to resolve import "${url}" from "${normalizePath$3(
  59724. path$n.relative(process.cwd(), importerFile)
  59725. )}". Does the file exist?`,
  59726. pos
  59727. );
  59728. }
  59729. if (isExternalUrl(resolved.id)) {
  59730. return [resolved.id, resolved.id];
  59731. }
  59732. const isRelative = url[0] === ".";
  59733. const isSelfImport = !isRelative && cleanUrl(url) === cleanUrl(importer);
  59734. if (resolved.id.startsWith(withTrailingSlash(root))) {
  59735. url = resolved.id.slice(root.length);
  59736. } else if (depsOptimizer?.isOptimizedDepFile(resolved.id) || // vite-plugin-react isn't following the leading \0 virtual module convention.
  59737. // This is a temporary hack to avoid expensive fs checks for React apps.
  59738. // We'll remove this as soon we're able to fix the react plugins.
  59739. resolved.id !== "/@react-refresh" && path$n.isAbsolute(resolved.id) && fsUtils.existsSync(cleanUrl(resolved.id))) {
  59740. url = path$n.posix.join(FS_PREFIX, resolved.id);
  59741. } else {
  59742. url = resolved.id;
  59743. }
  59744. if (url[0] !== "." && url[0] !== "/") {
  59745. url = wrapId$1(resolved.id);
  59746. }
  59747. if (!ssr) {
  59748. if (isExplicitImportRequired(url)) {
  59749. url = injectQuery(url, "import");
  59750. } else if ((isRelative || isSelfImport) && !DEP_VERSION_RE.test(url)) {
  59751. const versionMatch = DEP_VERSION_RE.exec(importer);
  59752. if (versionMatch) {
  59753. url = injectQuery(url, versionMatch[1]);
  59754. }
  59755. }
  59756. try {
  59757. const depModule = await moduleGraph._ensureEntryFromUrl(
  59758. unwrapId$1(url),
  59759. ssr,
  59760. canSkipImportAnalysis(url) || forceSkipImportAnalysis,
  59761. resolved
  59762. );
  59763. if (depModule.lastHMRTimestamp > 0) {
  59764. url = injectQuery(url, `t=${depModule.lastHMRTimestamp}`);
  59765. }
  59766. } catch (e) {
  59767. e.pos = pos;
  59768. throw e;
  59769. }
  59770. url = joinUrlSegments(base, url);
  59771. }
  59772. return [url, resolved.id];
  59773. };
  59774. const orderedImportedUrls = new Array(imports.length);
  59775. const orderedAcceptedUrls = new Array(
  59776. imports.length
  59777. );
  59778. const orderedAcceptedExports = new Array(
  59779. imports.length
  59780. );
  59781. await Promise.all(
  59782. imports.map(async (importSpecifier, index) => {
  59783. const {
  59784. s: start,
  59785. e: end,
  59786. ss: expStart,
  59787. se: expEnd,
  59788. d: dynamicIndex,
  59789. a: attributeIndex
  59790. } = importSpecifier;
  59791. let specifier = importSpecifier.n;
  59792. const rawUrl = source.slice(start, end);
  59793. if (rawUrl === "import.meta") {
  59794. const prop = source.slice(end, end + 4);
  59795. if (prop === ".hot") {
  59796. hasHMR = true;
  59797. const endHot = end + 4 + (source[end + 4] === "?" ? 1 : 0);
  59798. if (source.slice(endHot, endHot + 7) === ".accept") {
  59799. if (source.slice(endHot, endHot + 14) === ".acceptExports") {
  59800. const importAcceptedExports = orderedAcceptedExports[index] = /* @__PURE__ */ new Set();
  59801. lexAcceptedHmrExports(
  59802. source,
  59803. source.indexOf("(", endHot + 14) + 1,
  59804. importAcceptedExports
  59805. );
  59806. isPartiallySelfAccepting = true;
  59807. } else {
  59808. const importAcceptedUrls = orderedAcceptedUrls[index] = /* @__PURE__ */ new Set();
  59809. if (lexAcceptedHmrDeps(
  59810. source,
  59811. source.indexOf("(", endHot + 7) + 1,
  59812. importAcceptedUrls
  59813. )) {
  59814. isSelfAccepting = true;
  59815. }
  59816. }
  59817. }
  59818. } else if (prop === ".env") {
  59819. hasEnv = true;
  59820. }
  59821. return;
  59822. } else if (templateLiteralRE.test(rawUrl)) {
  59823. if (!(rawUrl.includes("${") && rawUrl.includes("}"))) {
  59824. specifier = rawUrl.replace(templateLiteralRE, "$1");
  59825. }
  59826. }
  59827. const isDynamicImport = dynamicIndex > -1;
  59828. if (!isDynamicImport && attributeIndex > -1) {
  59829. str().remove(end + 1, expEnd);
  59830. }
  59831. if (specifier !== void 0) {
  59832. if (isExternalUrl(specifier) || isDataUrl(specifier)) {
  59833. return;
  59834. }
  59835. if (ssr && !matchAlias(specifier)) {
  59836. if (shouldExternalizeForSSR(specifier, importer, config)) {
  59837. return;
  59838. }
  59839. if (isBuiltin(specifier)) {
  59840. return;
  59841. }
  59842. }
  59843. if (specifier === clientPublicPath) {
  59844. return;
  59845. }
  59846. if (specifier[0] === "/" && !(config.assetsInclude(cleanUrl(specifier)) || urlRE.test(specifier)) && checkPublicFile(specifier, config)) {
  59847. throw new Error(
  59848. `Cannot import non-asset file ${specifier} which is inside /public. JS/CSS files inside /public are copied as-is on build and can only be referenced via <script src> or <link href> in html. If you want to get the URL of that file, use ${injectQuery(
  59849. specifier,
  59850. "url"
  59851. )} instead.`
  59852. );
  59853. }
  59854. const [url, resolvedId] = await normalizeUrl(specifier, start);
  59855. server?.moduleGraph.safeModulesPath.add(
  59856. fsPathFromUrl(stripBase(url, base))
  59857. );
  59858. if (url !== specifier) {
  59859. let rewriteDone = false;
  59860. if (depsOptimizer?.isOptimizedDepFile(resolvedId) && !optimizedDepChunkRE.test(resolvedId)) {
  59861. const file = cleanUrl(resolvedId);
  59862. const needsInterop = await optimizedDepNeedsInterop(
  59863. depsOptimizer.metadata,
  59864. file,
  59865. config,
  59866. ssr
  59867. );
  59868. if (needsInterop === void 0) {
  59869. if (!optimizedDepDynamicRE.test(file)) {
  59870. config.logger.error(
  59871. colors$1.red(
  59872. `Vite Error, ${url} optimized info should be defined`
  59873. )
  59874. );
  59875. }
  59876. } else if (needsInterop) {
  59877. debug$1?.(`${url} needs interop`);
  59878. interopNamedImports(
  59879. str(),
  59880. importSpecifier,
  59881. url,
  59882. index,
  59883. importer,
  59884. config
  59885. );
  59886. rewriteDone = true;
  59887. }
  59888. } else if (url.includes(browserExternalId) && source.slice(expStart, start).includes("{")) {
  59889. interopNamedImports(
  59890. str(),
  59891. importSpecifier,
  59892. url,
  59893. index,
  59894. importer,
  59895. config
  59896. );
  59897. rewriteDone = true;
  59898. }
  59899. if (!rewriteDone) {
  59900. const rewrittenUrl = JSON.stringify(url);
  59901. const s2 = isDynamicImport ? start : start - 1;
  59902. const e = isDynamicImport ? end : end + 1;
  59903. str().overwrite(s2, e, rewrittenUrl, {
  59904. contentOnly: true
  59905. });
  59906. }
  59907. }
  59908. const hmrUrl = unwrapId$1(stripBase(url, base));
  59909. const isLocalImport = !isExternalUrl(hmrUrl) && !isDataUrl(hmrUrl);
  59910. if (isLocalImport) {
  59911. orderedImportedUrls[index] = hmrUrl;
  59912. }
  59913. if (enablePartialAccept && importedBindings) {
  59914. extractImportedBindings(
  59915. resolvedId,
  59916. source,
  59917. importSpecifier,
  59918. importedBindings
  59919. );
  59920. }
  59921. if (!isDynamicImport && isLocalImport && config.server.preTransformRequests) {
  59922. const url2 = removeImportQuery(hmrUrl);
  59923. server.warmupRequest(url2, { ssr });
  59924. }
  59925. } else if (!importer.startsWith(withTrailingSlash(clientDir))) {
  59926. if (!isInNodeModules$1(importer)) {
  59927. const hasViteIgnore = hasViteIgnoreRE.test(
  59928. // complete expression inside parens
  59929. source.slice(dynamicIndex + 1, end)
  59930. );
  59931. if (!hasViteIgnore) {
  59932. this.warn(
  59933. `
  59934. ` + colors$1.cyan(importerModule.file) + `
  59935. ` + colors$1.reset(generateCodeFrame(source, start, end)) + colors$1.yellow(
  59936. `
  59937. The above dynamic import cannot be analyzed by Vite.
  59938. See ${colors$1.blue(
  59939. `https://github.com/rollup/plugins/tree/master/packages/dynamic-import-vars#limitations`
  59940. )} for supported dynamic import formats. If this is intended to be left as-is, you can use the /* @vite-ignore */ comment inside the import() call to suppress this warning.
  59941. `
  59942. )
  59943. );
  59944. }
  59945. }
  59946. if (!ssr) {
  59947. if (!urlIsStringRE.test(rawUrl) || isExplicitImportRequired(rawUrl.slice(1, -1))) {
  59948. needQueryInjectHelper = true;
  59949. str().overwrite(
  59950. start,
  59951. end,
  59952. `__vite__injectQuery(${rawUrl}, 'import')`,
  59953. { contentOnly: true }
  59954. );
  59955. }
  59956. }
  59957. }
  59958. })
  59959. );
  59960. const _orderedImportedUrls = orderedImportedUrls.filter(isDefined);
  59961. const importedUrls = new Set(_orderedImportedUrls);
  59962. const staticImportedUrls = new Set(
  59963. _orderedImportedUrls.map((url) => removeTimestampQuery(url))
  59964. );
  59965. const acceptedUrls = mergeAcceptedUrls(orderedAcceptedUrls);
  59966. const acceptedExports = mergeAcceptedUrls(orderedAcceptedExports);
  59967. const isClassicWorker = importer.includes(WORKER_FILE_ID) && importer.includes("type=classic");
  59968. if (hasEnv && !isClassicWorker) {
  59969. str().prepend(getEnv(ssr));
  59970. }
  59971. if (hasHMR && !ssr && !isClassicWorker) {
  59972. debugHmr?.(
  59973. `${isSelfAccepting ? `[self-accepts]` : isPartiallySelfAccepting ? `[accepts-exports]` : acceptedUrls.size ? `[accepts-deps]` : `[detected api usage]`} ${prettifyUrl(importer, root)}`
  59974. );
  59975. str().prepend(
  59976. `import { createHotContext as __vite__createHotContext } from "${clientPublicPath}";import.meta.hot = __vite__createHotContext(${JSON.stringify(
  59977. normalizeHmrUrl(importerModule.url)
  59978. )});`
  59979. );
  59980. }
  59981. if (needQueryInjectHelper) {
  59982. if (isClassicWorker) {
  59983. str().append("\n" + __vite__injectQuery.toString());
  59984. } else {
  59985. str().prepend(
  59986. `import { injectQuery as __vite__injectQuery } from "${clientPublicPath}";`
  59987. );
  59988. }
  59989. }
  59990. const normalizedAcceptedUrls = /* @__PURE__ */ new Set();
  59991. for (const { url, start, end } of acceptedUrls) {
  59992. const [normalized] = await moduleGraph.resolveUrl(
  59993. toAbsoluteUrl(url),
  59994. ssr
  59995. );
  59996. normalizedAcceptedUrls.add(normalized);
  59997. str().overwrite(start, end, JSON.stringify(normalized), {
  59998. contentOnly: true
  59999. });
  60000. }
  60001. if (!isCSSRequest(importer) || SPECIAL_QUERY_RE.test(importer)) {
  60002. const pluginImports = this._addedImports;
  60003. if (pluginImports) {
  60004. (await Promise.all(
  60005. [...pluginImports].map((id) => normalizeUrl(id, 0, true))
  60006. )).forEach(([url]) => importedUrls.add(url));
  60007. }
  60008. if (ssr && importerModule.isSelfAccepting) {
  60009. isSelfAccepting = true;
  60010. }
  60011. if (!isSelfAccepting && isPartiallySelfAccepting && acceptedExports.size >= exports.length && exports.every((e) => acceptedExports.has(e.n))) {
  60012. isSelfAccepting = true;
  60013. }
  60014. const prunedImports = await moduleGraph.updateModuleInfo(
  60015. importerModule,
  60016. importedUrls,
  60017. importedBindings,
  60018. normalizedAcceptedUrls,
  60019. isPartiallySelfAccepting ? acceptedExports : null,
  60020. isSelfAccepting,
  60021. ssr,
  60022. staticImportedUrls
  60023. );
  60024. if (hasHMR && prunedImports) {
  60025. handlePrunedModules(prunedImports, server);
  60026. }
  60027. }
  60028. debug$1?.(
  60029. `${timeFrom(msAtStart)} ${colors$1.dim(
  60030. `[${importedUrls.size} imports rewritten] ${prettifyUrl(
  60031. importer,
  60032. root
  60033. )}`
  60034. )}`
  60035. );
  60036. if (s) {
  60037. return transformStableResult(s, importer, config);
  60038. } else {
  60039. return source;
  60040. }
  60041. }
  60042. };
  60043. }
  60044. function mergeAcceptedUrls(orderedUrls) {
  60045. const acceptedUrls = /* @__PURE__ */ new Set();
  60046. for (const urls of orderedUrls) {
  60047. if (!urls) continue;
  60048. for (const url of urls) acceptedUrls.add(url);
  60049. }
  60050. return acceptedUrls;
  60051. }
  60052. function createParseErrorInfo(importer, source) {
  60053. const isVue = importer.endsWith(".vue");
  60054. const isJsx = importer.endsWith(".jsx") || importer.endsWith(".tsx");
  60055. const maybeJSX = !isVue && isJSRequest(importer);
  60056. const probablyBinary = source.includes(
  60057. "\uFFFD"
  60058. );
  60059. const msg = isVue ? `Install @vitejs/plugin-vue to handle .vue files.` : maybeJSX ? isJsx ? `If you use tsconfig.json, make sure to not set jsx to preserve.` : `If you are using JSX, make sure to name the file with the .jsx or .tsx extension.` : `You may need to install appropriate plugins to handle the ${path$n.extname(
  60060. importer
  60061. )} file format, or if it's an asset, add "**/*${path$n.extname(
  60062. importer
  60063. )}" to \`assetsInclude\` in your configuration.`;
  60064. return {
  60065. message: `Failed to parse source for import analysis because the content contains invalid JS syntax. ` + msg,
  60066. showCodeFrame: !probablyBinary
  60067. };
  60068. }
  60069. const interopHelper = (m) => m?.__esModule ? m : { ...typeof m === "object" && !Array.isArray(m) || typeof m === "function" ? m : {}, default: m };
  60070. function interopNamedImports(str, importSpecifier, rewrittenUrl, importIndex, importer, config) {
  60071. const source = str.original;
  60072. const {
  60073. s: start,
  60074. e: end,
  60075. ss: expStart,
  60076. se: expEnd,
  60077. d: dynamicIndex
  60078. } = importSpecifier;
  60079. const exp = source.slice(expStart, expEnd);
  60080. if (dynamicIndex > -1) {
  60081. str.overwrite(
  60082. expStart,
  60083. expEnd,
  60084. `import('${rewrittenUrl}').then(m => (${interopHelper.toString()})(m.default))` + getLineBreaks(exp),
  60085. { contentOnly: true }
  60086. );
  60087. } else {
  60088. const rawUrl = source.slice(start, end);
  60089. const rewritten = transformCjsImport(
  60090. exp,
  60091. rewrittenUrl,
  60092. rawUrl,
  60093. importIndex,
  60094. importer,
  60095. config
  60096. );
  60097. if (rewritten) {
  60098. str.overwrite(expStart, expEnd, rewritten + getLineBreaks(exp), {
  60099. contentOnly: true
  60100. });
  60101. } else {
  60102. str.overwrite(
  60103. start,
  60104. end,
  60105. rewrittenUrl + getLineBreaks(source.slice(start, end)),
  60106. {
  60107. contentOnly: true
  60108. }
  60109. );
  60110. }
  60111. }
  60112. }
  60113. function getLineBreaks(str) {
  60114. return str.includes("\n") ? "\n".repeat(str.split("\n").length - 1) : "";
  60115. }
  60116. function transformCjsImport(importExp, url, rawUrl, importIndex, importer, config) {
  60117. const node = parseAst(importExp).body[0];
  60118. if (config.command === "serve" && node.type === "ExportAllDeclaration" && !node.exported) {
  60119. config.logger.warn(
  60120. colors$1.yellow(
  60121. `
  60122. Unable to interop \`${importExp}\` in ${importer}, this may lose module exports. Please export "${rawUrl}" as ESM or use named exports instead, e.g. \`export { A, B } from "${rawUrl}"\``
  60123. )
  60124. );
  60125. } else if (node.type === "ImportDeclaration" || node.type === "ExportNamedDeclaration") {
  60126. if (!node.specifiers.length) {
  60127. return `import "${url}"`;
  60128. }
  60129. const importNames = [];
  60130. const exportNames = [];
  60131. let defaultExports = "";
  60132. for (const spec of node.specifiers) {
  60133. if (spec.type === "ImportSpecifier" && spec.imported.type === "Identifier") {
  60134. const importedName = spec.imported.name;
  60135. const localName = spec.local.name;
  60136. importNames.push({ importedName, localName });
  60137. } else if (spec.type === "ImportDefaultSpecifier") {
  60138. importNames.push({
  60139. importedName: "default",
  60140. localName: spec.local.name
  60141. });
  60142. } else if (spec.type === "ImportNamespaceSpecifier") {
  60143. importNames.push({ importedName: "*", localName: spec.local.name });
  60144. } else if (spec.type === "ExportSpecifier" && spec.exported.type === "Identifier") {
  60145. const importedName = spec.local.name;
  60146. const exportedName = spec.exported.name;
  60147. if (exportedName === "default") {
  60148. defaultExports = makeLegalIdentifier(
  60149. `__vite__cjsExportDefault_${importIndex}`
  60150. );
  60151. importNames.push({ importedName, localName: defaultExports });
  60152. } else {
  60153. const localName = makeLegalIdentifier(
  60154. `__vite__cjsExport_${exportedName}`
  60155. );
  60156. importNames.push({ importedName, localName });
  60157. exportNames.push(`${localName} as ${exportedName}`);
  60158. }
  60159. }
  60160. }
  60161. const cjsModuleName = makeLegalIdentifier(
  60162. `__vite__cjsImport${importIndex}_${rawUrl}`
  60163. );
  60164. const lines = [`import ${cjsModuleName} from "${url}"`];
  60165. importNames.forEach(({ importedName, localName }) => {
  60166. if (importedName === "*") {
  60167. lines.push(
  60168. `const ${localName} = (${interopHelper.toString()})(${cjsModuleName})`
  60169. );
  60170. } else if (importedName === "default") {
  60171. lines.push(
  60172. `const ${localName} = ${cjsModuleName}.__esModule ? ${cjsModuleName}.default : ${cjsModuleName}`
  60173. );
  60174. } else {
  60175. lines.push(`const ${localName} = ${cjsModuleName}["${importedName}"]`);
  60176. }
  60177. });
  60178. if (defaultExports) {
  60179. lines.push(`export default ${defaultExports}`);
  60180. }
  60181. if (exportNames.length) {
  60182. lines.push(`export { ${exportNames.join(", ")} }`);
  60183. }
  60184. return lines.join("; ");
  60185. }
  60186. }
  60187. function __vite__injectQuery(url, queryToInject) {
  60188. if (url[0] !== "." && url[0] !== "/") {
  60189. return url;
  60190. }
  60191. const pathname = url.replace(/[?#].*$/, "");
  60192. const { search, hash } = new URL(url, "http://vite.dev");
  60193. return `${pathname}?${queryToInject}${search ? `&` + search.slice(1) : ""}${hash || ""}`;
  60194. }
  60195. const isModernFlag = `__VITE_IS_MODERN__`;
  60196. const preloadMethod = `__vitePreload`;
  60197. const preloadMarker = `__VITE_PRELOAD__`;
  60198. const preloadHelperId = "\0vite/preload-helper.js";
  60199. const preloadMarkerRE = new RegExp(preloadMarker, "g");
  60200. const dynamicImportPrefixRE = /import\s*\(/;
  60201. const dynamicImportTreeshakenRE = /((?:\bconst\s+|\blet\s+|\bvar\s+|,\s*)(\{[^{}.=]+\})\s*=\s*await\s+import\([^)]+\))|(\(\s*await\s+import\([^)]+\)\s*\)(\??\.[\w$]+))|\bimport\([^)]+\)(\s*\.then\(\s*(?:function\s*)?\(\s*\{([^{}.=]+)\}\))/g;
  60202. function toRelativePath(filename, importer) {
  60203. const relPath = path$n.posix.relative(path$n.posix.dirname(importer), filename);
  60204. return relPath[0] === "." ? relPath : `./${relPath}`;
  60205. }
  60206. function indexOfMatchInSlice(str, reg, pos = 0) {
  60207. reg.lastIndex = pos;
  60208. const result = reg.exec(str);
  60209. return result?.index ?? -1;
  60210. }
  60211. function detectScriptRel() {
  60212. const relList = typeof document !== "undefined" && document.createElement("link").relList;
  60213. return relList && relList.supports && relList.supports("modulepreload") ? "modulepreload" : "preload";
  60214. }
  60215. function preload(baseModule, deps, importerUrl) {
  60216. let promise = Promise.resolve();
  60217. if (__VITE_IS_MODERN__ && deps && deps.length > 0) {
  60218. const links = document.getElementsByTagName("link");
  60219. const cspNonceMeta = document.querySelector(
  60220. "meta[property=csp-nonce]"
  60221. );
  60222. const cspNonce = cspNonceMeta?.nonce || cspNonceMeta?.getAttribute("nonce");
  60223. promise = Promise.allSettled(
  60224. deps.map((dep) => {
  60225. dep = assetsURL(dep, importerUrl);
  60226. if (dep in seen) return;
  60227. seen[dep] = true;
  60228. const isCss = dep.endsWith(".css");
  60229. const cssSelector = isCss ? '[rel="stylesheet"]' : "";
  60230. const isBaseRelative = !!importerUrl;
  60231. if (isBaseRelative) {
  60232. for (let i = links.length - 1; i >= 0; i--) {
  60233. const link2 = links[i];
  60234. if (link2.href === dep && (!isCss || link2.rel === "stylesheet")) {
  60235. return;
  60236. }
  60237. }
  60238. } else if (document.querySelector(`link[href="${dep}"]${cssSelector}`)) {
  60239. return;
  60240. }
  60241. const link = document.createElement("link");
  60242. link.rel = isCss ? "stylesheet" : scriptRel;
  60243. if (!isCss) {
  60244. link.as = "script";
  60245. }
  60246. link.crossOrigin = "";
  60247. link.href = dep;
  60248. if (cspNonce) {
  60249. link.setAttribute("nonce", cspNonce);
  60250. }
  60251. document.head.appendChild(link);
  60252. if (isCss) {
  60253. return new Promise((res, rej) => {
  60254. link.addEventListener("load", res);
  60255. link.addEventListener(
  60256. "error",
  60257. () => rej(new Error(`Unable to preload CSS for ${dep}`))
  60258. );
  60259. });
  60260. }
  60261. })
  60262. );
  60263. }
  60264. function handlePreloadError(err) {
  60265. const e = new Event("vite:preloadError", {
  60266. cancelable: true
  60267. });
  60268. e.payload = err;
  60269. window.dispatchEvent(e);
  60270. if (!e.defaultPrevented) {
  60271. throw err;
  60272. }
  60273. }
  60274. return promise.then((res) => {
  60275. for (const item of res || []) {
  60276. if (item.status !== "rejected") continue;
  60277. handlePreloadError(item.reason);
  60278. }
  60279. return baseModule().catch(handlePreloadError);
  60280. });
  60281. }
  60282. function buildImportAnalysisPlugin(config) {
  60283. const ssr = !!config.build.ssr;
  60284. const isWorker = config.isWorker;
  60285. const insertPreload = !(ssr || !!config.build.lib || isWorker);
  60286. const renderBuiltUrl = config.experimental.renderBuiltUrl;
  60287. const isRelativeBase = config.base === "./" || config.base === "";
  60288. const { modulePreload } = config.build;
  60289. const scriptRel2 = modulePreload && modulePreload.polyfill ? `'modulepreload'` : `(${detectScriptRel.toString()})()`;
  60290. const assetsURL2 = renderBuiltUrl || isRelativeBase ? (
  60291. // If `experimental.renderBuiltUrl` is used, the dependencies might be relative to the current chunk.
  60292. // If relative base is used, the dependencies are relative to the current chunk.
  60293. // The importerUrl is passed as third parameter to __vitePreload in this case
  60294. `function(dep, importerUrl) { return new URL(dep, importerUrl).href }`
  60295. ) : (
  60296. // If the base isn't relative, then the deps are relative to the projects `outDir` and the base
  60297. // is appended inside __vitePreload too.
  60298. `function(dep) { return ${JSON.stringify(config.base)}+dep }`
  60299. );
  60300. const preloadCode = `const scriptRel = ${scriptRel2};const assetsURL = ${assetsURL2};const seen = {};export const ${preloadMethod} = ${preload.toString()}`;
  60301. return {
  60302. name: "vite:build-import-analysis",
  60303. resolveId(id) {
  60304. if (id === preloadHelperId) {
  60305. return id;
  60306. }
  60307. },
  60308. load(id) {
  60309. if (id === preloadHelperId) {
  60310. return preloadCode;
  60311. }
  60312. },
  60313. async transform(source, importer) {
  60314. if (isInNodeModules$1(importer) && !dynamicImportPrefixRE.test(source)) {
  60315. return;
  60316. }
  60317. await init;
  60318. let imports = [];
  60319. try {
  60320. imports = parse$d(source)[0];
  60321. } catch (_e) {
  60322. const e = _e;
  60323. const { message, showCodeFrame } = createParseErrorInfo(
  60324. importer,
  60325. source
  60326. );
  60327. this.error(message, showCodeFrame ? e.idx : void 0);
  60328. }
  60329. if (!imports.length) {
  60330. return null;
  60331. }
  60332. const dynamicImports = {};
  60333. if (insertPreload) {
  60334. let match;
  60335. while (match = dynamicImportTreeshakenRE.exec(source)) {
  60336. if (match[1]) {
  60337. dynamicImports[dynamicImportTreeshakenRE.lastIndex] = {
  60338. declaration: `const ${match[2]}`,
  60339. names: match[2]?.trim()
  60340. };
  60341. continue;
  60342. }
  60343. if (match[3]) {
  60344. let names2 = /\.([^.?]+)/.exec(match[4])?.[1] || "";
  60345. if (names2 === "default") {
  60346. names2 = "default: __vite_default__";
  60347. }
  60348. dynamicImports[dynamicImportTreeshakenRE.lastIndex - match[4]?.length - 1] = { declaration: `const {${names2}}`, names: `{ ${names2} }` };
  60349. continue;
  60350. }
  60351. const names = match[6]?.trim();
  60352. dynamicImports[dynamicImportTreeshakenRE.lastIndex - match[5]?.length] = { declaration: `const {${names}}`, names: `{ ${names} }` };
  60353. }
  60354. }
  60355. let s;
  60356. const str = () => s || (s = new MagicString(source));
  60357. let needPreloadHelper = false;
  60358. for (let index = 0; index < imports.length; index++) {
  60359. const {
  60360. s: start,
  60361. e: end,
  60362. ss: expStart,
  60363. se: expEnd,
  60364. d: dynamicIndex,
  60365. a: attributeIndex
  60366. } = imports[index];
  60367. const isDynamicImport = dynamicIndex > -1;
  60368. if (!isDynamicImport && attributeIndex > -1) {
  60369. str().remove(end + 1, expEnd);
  60370. }
  60371. if (isDynamicImport && insertPreload && // Only preload static urls
  60372. (source[start] === '"' || source[start] === "'" || source[start] === "`")) {
  60373. needPreloadHelper = true;
  60374. const { declaration, names } = dynamicImports[expEnd] || {};
  60375. if (names) {
  60376. str().prependLeft(
  60377. expStart,
  60378. `${preloadMethod}(async () => { ${declaration} = await `
  60379. );
  60380. str().appendRight(expEnd, `;return ${names}}`);
  60381. } else {
  60382. str().prependLeft(expStart, `${preloadMethod}(() => `);
  60383. }
  60384. str().appendRight(
  60385. expEnd,
  60386. `,${isModernFlag}?${preloadMarker}:void 0${renderBuiltUrl || isRelativeBase ? ",import.meta.url" : ""})`
  60387. );
  60388. }
  60389. }
  60390. if (needPreloadHelper && insertPreload && !source.includes(`const ${preloadMethod} =`)) {
  60391. str().prepend(`import { ${preloadMethod} } from "${preloadHelperId}";`);
  60392. }
  60393. if (s) {
  60394. return {
  60395. code: s.toString(),
  60396. map: config.build.sourcemap ? s.generateMap({ hires: "boundary" }) : null
  60397. };
  60398. }
  60399. },
  60400. renderChunk(code, _, { format }) {
  60401. if (code.indexOf(isModernFlag) > -1) {
  60402. const re = new RegExp(isModernFlag, "g");
  60403. const isModern = String(format === "es");
  60404. if (config.build.sourcemap) {
  60405. const s = new MagicString(code);
  60406. let match;
  60407. while (match = re.exec(code)) {
  60408. s.update(match.index, match.index + isModernFlag.length, isModern);
  60409. }
  60410. return {
  60411. code: s.toString(),
  60412. map: s.generateMap({ hires: "boundary" })
  60413. };
  60414. } else {
  60415. return code.replace(re, isModern);
  60416. }
  60417. }
  60418. return null;
  60419. },
  60420. generateBundle({ format }, bundle) {
  60421. if (format !== "es") {
  60422. return;
  60423. }
  60424. if (!insertPreload) {
  60425. const removedPureCssFiles = removedPureCssFilesCache.get(config);
  60426. if (removedPureCssFiles && removedPureCssFiles.size > 0) {
  60427. for (const file in bundle) {
  60428. const chunk = bundle[file];
  60429. if (chunk.type === "chunk" && chunk.code.includes("import")) {
  60430. const code = chunk.code;
  60431. let imports;
  60432. try {
  60433. imports = parse$d(code)[0].filter((i) => i.d > -1);
  60434. } catch (e) {
  60435. const loc = numberToPos(code, e.idx);
  60436. this.error({
  60437. name: e.name,
  60438. message: e.message,
  60439. stack: e.stack,
  60440. cause: e.cause,
  60441. pos: e.idx,
  60442. loc: { ...loc, file: chunk.fileName },
  60443. frame: generateCodeFrame(code, loc)
  60444. });
  60445. }
  60446. for (const imp of imports) {
  60447. const {
  60448. n: name,
  60449. s: start,
  60450. e: end,
  60451. ss: expStart,
  60452. se: expEnd
  60453. } = imp;
  60454. let url = name;
  60455. if (!url) {
  60456. const rawUrl = code.slice(start, end);
  60457. if (rawUrl[0] === `"` && rawUrl[rawUrl.length - 1] === `"`)
  60458. url = rawUrl.slice(1, -1);
  60459. }
  60460. if (!url) continue;
  60461. const normalizedFile = path$n.posix.join(
  60462. path$n.posix.dirname(chunk.fileName),
  60463. url
  60464. );
  60465. if (removedPureCssFiles.has(normalizedFile)) {
  60466. chunk.code = chunk.code.slice(0, expStart) + `Promise.resolve({${"".padEnd(expEnd - expStart - 19, " ")}})` + chunk.code.slice(expEnd);
  60467. }
  60468. }
  60469. }
  60470. }
  60471. }
  60472. return;
  60473. }
  60474. for (const file in bundle) {
  60475. const chunk = bundle[file];
  60476. if (chunk.type === "chunk" && chunk.code.indexOf(preloadMarker) > -1) {
  60477. const code = chunk.code;
  60478. let imports;
  60479. try {
  60480. imports = parse$d(code)[0].filter((i) => i.d > -1);
  60481. } catch (e) {
  60482. const loc = numberToPos(code, e.idx);
  60483. this.error({
  60484. name: e.name,
  60485. message: e.message,
  60486. stack: e.stack,
  60487. cause: e.cause,
  60488. pos: e.idx,
  60489. loc: { ...loc, file: chunk.fileName },
  60490. frame: generateCodeFrame(code, loc)
  60491. });
  60492. }
  60493. const s = new MagicString(code);
  60494. const rewroteMarkerStartPos = /* @__PURE__ */ new Set();
  60495. const fileDeps = [];
  60496. const addFileDep = (url, runtime = false) => {
  60497. const index = fileDeps.findIndex((dep) => dep.url === url);
  60498. if (index === -1) {
  60499. return fileDeps.push({ url, runtime }) - 1;
  60500. } else {
  60501. return index;
  60502. }
  60503. };
  60504. if (imports.length) {
  60505. for (let index = 0; index < imports.length; index++) {
  60506. const {
  60507. n: name,
  60508. s: start,
  60509. e: end,
  60510. ss: expStart,
  60511. se: expEnd
  60512. } = imports[index];
  60513. let url = name;
  60514. if (!url) {
  60515. const rawUrl = code.slice(start, end);
  60516. if (rawUrl[0] === `"` && rawUrl[rawUrl.length - 1] === `"`)
  60517. url = rawUrl.slice(1, -1);
  60518. }
  60519. const deps = /* @__PURE__ */ new Set();
  60520. let hasRemovedPureCssChunk = false;
  60521. let normalizedFile = void 0;
  60522. if (url) {
  60523. normalizedFile = path$n.posix.join(
  60524. path$n.posix.dirname(chunk.fileName),
  60525. url
  60526. );
  60527. const ownerFilename = chunk.fileName;
  60528. const analyzed = /* @__PURE__ */ new Set();
  60529. const addDeps = (filename) => {
  60530. if (filename === ownerFilename) return;
  60531. if (analyzed.has(filename)) return;
  60532. analyzed.add(filename);
  60533. const chunk2 = bundle[filename];
  60534. if (chunk2) {
  60535. deps.add(chunk2.fileName);
  60536. if (chunk2.type === "chunk") {
  60537. chunk2.imports.forEach(addDeps);
  60538. chunk2.viteMetadata.importedCss.forEach((file2) => {
  60539. deps.add(file2);
  60540. });
  60541. }
  60542. } else {
  60543. const removedPureCssFiles = removedPureCssFilesCache.get(config);
  60544. const chunk3 = removedPureCssFiles.get(filename);
  60545. if (chunk3) {
  60546. if (chunk3.viteMetadata.importedCss.size) {
  60547. chunk3.viteMetadata.importedCss.forEach((file2) => {
  60548. deps.add(file2);
  60549. });
  60550. hasRemovedPureCssChunk = true;
  60551. }
  60552. s.update(expStart, expEnd, "Promise.resolve({})");
  60553. }
  60554. }
  60555. };
  60556. addDeps(normalizedFile);
  60557. }
  60558. let markerStartPos2 = indexOfMatchInSlice(
  60559. code,
  60560. preloadMarkerRE,
  60561. end
  60562. );
  60563. if (markerStartPos2 === -1 && imports.length === 1) {
  60564. markerStartPos2 = indexOfMatchInSlice(code, preloadMarkerRE);
  60565. }
  60566. if (markerStartPos2 > 0) {
  60567. let depsArray = deps.size > 1 || // main chunk is removed
  60568. hasRemovedPureCssChunk && deps.size > 0 ? modulePreload === false ? (
  60569. // CSS deps use the same mechanism as module preloads, so even if disabled,
  60570. // we still need to pass these deps to the preload helper in dynamic imports.
  60571. [...deps].filter((d) => d.endsWith(".css"))
  60572. ) : [...deps] : [];
  60573. const resolveDependencies = modulePreload ? modulePreload.resolveDependencies : void 0;
  60574. if (resolveDependencies && normalizedFile) {
  60575. const cssDeps = [];
  60576. const otherDeps = [];
  60577. for (const dep of depsArray) {
  60578. (dep.endsWith(".css") ? cssDeps : otherDeps).push(dep);
  60579. }
  60580. depsArray = [
  60581. ...resolveDependencies(normalizedFile, otherDeps, {
  60582. hostId: file,
  60583. hostType: "js"
  60584. }),
  60585. ...cssDeps
  60586. ];
  60587. }
  60588. let renderedDeps;
  60589. if (renderBuiltUrl) {
  60590. renderedDeps = depsArray.map((dep) => {
  60591. const replacement = toOutputFilePathInJS(
  60592. dep,
  60593. "asset",
  60594. chunk.fileName,
  60595. "js",
  60596. config,
  60597. toRelativePath
  60598. );
  60599. if (typeof replacement === "string") {
  60600. return addFileDep(replacement);
  60601. }
  60602. return addFileDep(replacement.runtime, true);
  60603. });
  60604. } else {
  60605. renderedDeps = depsArray.map(
  60606. (d) => (
  60607. // Don't include the assets dir if the default asset file names
  60608. // are used, the path will be reconstructed by the import preload helper
  60609. isRelativeBase ? addFileDep(toRelativePath(d, file)) : addFileDep(d)
  60610. )
  60611. );
  60612. }
  60613. s.update(
  60614. markerStartPos2,
  60615. markerStartPos2 + preloadMarker.length,
  60616. renderedDeps.length > 0 ? `__vite__mapDeps([${renderedDeps.join(",")}])` : `[]`
  60617. );
  60618. rewroteMarkerStartPos.add(markerStartPos2);
  60619. }
  60620. }
  60621. }
  60622. if (fileDeps.length > 0) {
  60623. const fileDepsCode = `[${fileDeps.map(
  60624. (fileDep) => fileDep.runtime ? fileDep.url : JSON.stringify(fileDep.url)
  60625. ).join(",")}]`;
  60626. const mapDepsCode = `const __vite__mapDeps=(i,m=__vite__mapDeps,d=(m.f||(m.f=${fileDepsCode})))=>i.map(i=>d[i]);
  60627. `;
  60628. if (code.startsWith("#!")) {
  60629. s.prependLeft(code.indexOf("\n") + 1, mapDepsCode);
  60630. } else {
  60631. s.prepend(mapDepsCode);
  60632. }
  60633. }
  60634. let markerStartPos = indexOfMatchInSlice(code, preloadMarkerRE);
  60635. while (markerStartPos >= 0) {
  60636. if (!rewroteMarkerStartPos.has(markerStartPos)) {
  60637. s.update(
  60638. markerStartPos,
  60639. markerStartPos + preloadMarker.length,
  60640. "void 0"
  60641. );
  60642. }
  60643. markerStartPos = indexOfMatchInSlice(
  60644. code,
  60645. preloadMarkerRE,
  60646. markerStartPos + preloadMarker.length
  60647. );
  60648. }
  60649. if (s.hasChanged()) {
  60650. chunk.code = s.toString();
  60651. if (config.build.sourcemap && chunk.map) {
  60652. const nextMap = s.generateMap({
  60653. source: chunk.fileName,
  60654. hires: "boundary"
  60655. });
  60656. const map = combineSourcemaps(chunk.fileName, [
  60657. nextMap,
  60658. chunk.map
  60659. ]);
  60660. map.toUrl = () => genSourceMapUrl(map);
  60661. chunk.map = map;
  60662. if (config.build.sourcemap === "inline") {
  60663. chunk.code = chunk.code.replace(
  60664. convertSourceMap.mapFileCommentRegex,
  60665. ""
  60666. );
  60667. chunk.code += `
  60668. //# sourceMappingURL=${genSourceMapUrl(map)}`;
  60669. } else if (config.build.sourcemap) {
  60670. const mapAsset = bundle[chunk.fileName + ".map"];
  60671. if (mapAsset && mapAsset.type === "asset") {
  60672. mapAsset.source = map.toString();
  60673. }
  60674. }
  60675. }
  60676. }
  60677. }
  60678. }
  60679. }
  60680. };
  60681. }
  60682. function ssrManifestPlugin(config) {
  60683. const ssrManifest = {};
  60684. const base = config.base;
  60685. return {
  60686. name: "vite:ssr-manifest",
  60687. generateBundle(_options, bundle) {
  60688. for (const file in bundle) {
  60689. const chunk = bundle[file];
  60690. if (chunk.type === "chunk") {
  60691. for (const id in chunk.modules) {
  60692. const normalizedId = normalizePath$3(relative$2(config.root, id));
  60693. const mappedChunks = ssrManifest[normalizedId] ?? (ssrManifest[normalizedId] = []);
  60694. if (!chunk.isEntry) {
  60695. mappedChunks.push(joinUrlSegments(base, chunk.fileName));
  60696. chunk.viteMetadata.importedCss.forEach((file2) => {
  60697. mappedChunks.push(joinUrlSegments(base, file2));
  60698. });
  60699. }
  60700. chunk.viteMetadata.importedAssets.forEach((file2) => {
  60701. mappedChunks.push(joinUrlSegments(base, file2));
  60702. });
  60703. }
  60704. if (chunk.code.includes(preloadMethod)) {
  60705. const code = chunk.code;
  60706. let imports = [];
  60707. try {
  60708. imports = parse$d(code)[0].filter((i) => i.n && i.d > -1);
  60709. } catch (_e) {
  60710. const e = _e;
  60711. const loc = numberToPos(code, e.idx);
  60712. this.error({
  60713. name: e.name,
  60714. message: e.message,
  60715. stack: e.stack,
  60716. cause: e.cause,
  60717. pos: e.idx,
  60718. loc: { ...loc, file: chunk.fileName },
  60719. frame: generateCodeFrame(code, loc)
  60720. });
  60721. }
  60722. if (imports.length) {
  60723. for (let index = 0; index < imports.length; index++) {
  60724. const { s: start, e: end, n: name } = imports[index];
  60725. const url = code.slice(start, end);
  60726. const deps = [];
  60727. const ownerFilename = chunk.fileName;
  60728. const analyzed = /* @__PURE__ */ new Set();
  60729. const addDeps = (filename) => {
  60730. if (filename === ownerFilename) return;
  60731. if (analyzed.has(filename)) return;
  60732. analyzed.add(filename);
  60733. const chunk2 = bundle[filename];
  60734. if (chunk2) {
  60735. chunk2.viteMetadata.importedCss.forEach((file2) => {
  60736. deps.push(joinUrlSegments(base, file2));
  60737. });
  60738. chunk2.imports.forEach(addDeps);
  60739. }
  60740. };
  60741. const normalizedFile = normalizePath$3(
  60742. join$2(dirname$2(chunk.fileName), url.slice(1, -1))
  60743. );
  60744. addDeps(normalizedFile);
  60745. ssrManifest[basename$2(name)] = deps;
  60746. }
  60747. }
  60748. }
  60749. }
  60750. }
  60751. this.emitFile({
  60752. fileName: typeof config.build.ssrManifest === "string" ? config.build.ssrManifest : ".vite/ssr-manifest.json",
  60753. type: "asset",
  60754. source: JSON.stringify(sortObjectKeys(ssrManifest), void 0, 2)
  60755. });
  60756. }
  60757. };
  60758. }
  60759. function loadFallbackPlugin() {
  60760. return {
  60761. name: "vite:load-fallback",
  60762. async load(id) {
  60763. try {
  60764. const cleanedId = cleanUrl(id);
  60765. const content = await fsp.readFile(cleanedId, "utf-8");
  60766. this.addWatchFile(cleanedId);
  60767. return content;
  60768. } catch (e) {
  60769. const content = await fsp.readFile(id, "utf-8");
  60770. this.addWatchFile(id);
  60771. return content;
  60772. }
  60773. }
  60774. };
  60775. }
  60776. function completeSystemWrapPlugin() {
  60777. const SystemJSWrapRE = /System.register\(.*?(\(exports\)|\(\))/g;
  60778. return {
  60779. name: "vite:force-systemjs-wrap-complete",
  60780. renderChunk(code, chunk, opts) {
  60781. if (opts.format === "system") {
  60782. return {
  60783. code: code.replace(
  60784. SystemJSWrapRE,
  60785. (s, s1) => s.replace(s1, "(exports, module)")
  60786. ),
  60787. map: null
  60788. };
  60789. }
  60790. }
  60791. };
  60792. }
  60793. function resolveBuildOptions(raw, logger, root) {
  60794. const deprecatedPolyfillModulePreload = raw?.polyfillModulePreload;
  60795. if (raw) {
  60796. const { polyfillModulePreload, ...rest } = raw;
  60797. raw = rest;
  60798. if (deprecatedPolyfillModulePreload !== void 0) {
  60799. logger.warn(
  60800. "polyfillModulePreload is deprecated. Use modulePreload.polyfill instead."
  60801. );
  60802. }
  60803. if (deprecatedPolyfillModulePreload === false && raw.modulePreload === void 0) {
  60804. raw.modulePreload = { polyfill: false };
  60805. }
  60806. }
  60807. const modulePreload = raw?.modulePreload;
  60808. const defaultModulePreload = {
  60809. polyfill: true
  60810. };
  60811. const defaultBuildOptions = {
  60812. outDir: "dist",
  60813. assetsDir: "assets",
  60814. assetsInlineLimit: DEFAULT_ASSETS_INLINE_LIMIT,
  60815. cssCodeSplit: !raw?.lib,
  60816. sourcemap: false,
  60817. rollupOptions: {},
  60818. minify: raw?.ssr ? false : "esbuild",
  60819. terserOptions: {},
  60820. write: true,
  60821. emptyOutDir: null,
  60822. copyPublicDir: true,
  60823. manifest: false,
  60824. lib: false,
  60825. ssr: false,
  60826. ssrManifest: false,
  60827. ssrEmitAssets: false,
  60828. reportCompressedSize: true,
  60829. chunkSizeWarningLimit: 500,
  60830. watch: null
  60831. };
  60832. const userBuildOptions = raw ? mergeConfig(defaultBuildOptions, raw) : defaultBuildOptions;
  60833. const resolved = {
  60834. target: "modules",
  60835. cssTarget: false,
  60836. ...userBuildOptions,
  60837. commonjsOptions: {
  60838. include: [/node_modules/],
  60839. extensions: [".js", ".cjs"],
  60840. ...userBuildOptions.commonjsOptions
  60841. },
  60842. dynamicImportVarsOptions: {
  60843. warnOnError: true,
  60844. exclude: [/node_modules/],
  60845. ...userBuildOptions.dynamicImportVarsOptions
  60846. },
  60847. // Resolve to false | object
  60848. modulePreload: modulePreload === false ? false : typeof modulePreload === "object" ? {
  60849. ...defaultModulePreload,
  60850. ...modulePreload
  60851. } : defaultModulePreload
  60852. };
  60853. if (resolved.target === "modules") {
  60854. resolved.target = ESBUILD_MODULES_TARGET;
  60855. } else if (resolved.target === "esnext" && resolved.minify === "terser") {
  60856. try {
  60857. const terserPackageJsonPath = requireResolveFromRootWithFallback(
  60858. root,
  60859. "terser/package.json"
  60860. );
  60861. const terserPackageJson = JSON.parse(
  60862. fs__default.readFileSync(terserPackageJsonPath, "utf-8")
  60863. );
  60864. const v = terserPackageJson.version.split(".");
  60865. if (v[0] === "5" && v[1] < 16) {
  60866. resolved.target = "es2021";
  60867. }
  60868. } catch {
  60869. }
  60870. }
  60871. if (!resolved.cssTarget) {
  60872. resolved.cssTarget = resolved.target;
  60873. }
  60874. if (resolved.minify === "false") {
  60875. resolved.minify = false;
  60876. } else if (resolved.minify === true) {
  60877. resolved.minify = "esbuild";
  60878. }
  60879. if (resolved.cssMinify == null) {
  60880. resolved.cssMinify = !!resolved.minify;
  60881. }
  60882. return resolved;
  60883. }
  60884. async function resolveBuildPlugins(config) {
  60885. const options = config.build;
  60886. const { commonjsOptions } = options;
  60887. const usePluginCommonjs = !Array.isArray(commonjsOptions?.include) || commonjsOptions?.include.length !== 0;
  60888. const rollupOptionsPlugins = options.rollupOptions.plugins;
  60889. return {
  60890. pre: [
  60891. completeSystemWrapPlugin(),
  60892. ...usePluginCommonjs ? [commonjs(options.commonjsOptions)] : [],
  60893. dataURIPlugin(),
  60894. ...(await asyncFlatten(arraify(rollupOptionsPlugins))).filter(
  60895. Boolean
  60896. ),
  60897. ...config.isWorker ? [webWorkerPostPlugin()] : []
  60898. ],
  60899. post: [
  60900. buildImportAnalysisPlugin(config),
  60901. ...config.esbuild !== false ? [buildEsbuildPlugin(config)] : [],
  60902. ...options.minify ? [terserPlugin(config)] : [],
  60903. ...!config.isWorker ? [
  60904. ...options.manifest ? [manifestPlugin(config)] : [],
  60905. ...options.ssrManifest ? [ssrManifestPlugin(config)] : [],
  60906. buildReporterPlugin(config)
  60907. ] : [],
  60908. loadFallbackPlugin()
  60909. ]
  60910. };
  60911. }
  60912. async function build(inlineConfig = {}) {
  60913. const config = await resolveConfig(
  60914. inlineConfig,
  60915. "build",
  60916. "production",
  60917. "production"
  60918. );
  60919. const options = config.build;
  60920. const { root, logger, packageCache } = config;
  60921. const ssr = !!options.ssr;
  60922. const libOptions = options.lib;
  60923. logger.info(
  60924. colors$1.cyan(
  60925. `vite v${VERSION} ${colors$1.green(
  60926. `building ${ssr ? `SSR bundle ` : ``}for ${config.mode}...`
  60927. )}`
  60928. )
  60929. );
  60930. const resolve = (p) => path$n.resolve(root, p);
  60931. const input = libOptions ? options.rollupOptions?.input || (typeof libOptions.entry === "string" ? resolve(libOptions.entry) : Array.isArray(libOptions.entry) ? libOptions.entry.map(resolve) : Object.fromEntries(
  60932. Object.entries(libOptions.entry).map(([alias, file]) => [
  60933. alias,
  60934. resolve(file)
  60935. ])
  60936. )) : typeof options.ssr === "string" ? resolve(options.ssr) : options.rollupOptions?.input || resolve("index.html");
  60937. if (ssr && typeof input === "string" && input.endsWith(".html")) {
  60938. throw new Error(
  60939. `rollupOptions.input should not be an html file when building for SSR. Please specify a dedicated SSR entry.`
  60940. );
  60941. }
  60942. if (config.build.cssCodeSplit === false) {
  60943. const inputs = typeof input === "string" ? [input] : Array.isArray(input) ? input : Object.values(input);
  60944. if (inputs.some((input2) => input2.endsWith(".css"))) {
  60945. throw new Error(
  60946. `When "build.cssCodeSplit: false" is set, "rollupOptions.input" should not include CSS files.`
  60947. );
  60948. }
  60949. }
  60950. const outDir = resolve(options.outDir);
  60951. const plugins = ssr ? config.plugins.map((p) => injectSsrFlagToHooks(p)) : config.plugins;
  60952. const rollupOptions = {
  60953. preserveEntrySignatures: ssr ? "allow-extension" : libOptions ? "strict" : false,
  60954. cache: config.build.watch ? void 0 : false,
  60955. ...options.rollupOptions,
  60956. input,
  60957. plugins,
  60958. external: options.rollupOptions?.external,
  60959. onwarn(warning, warn) {
  60960. onRollupWarning(warning, warn, config);
  60961. }
  60962. };
  60963. function extractStack(e) {
  60964. const { stack, name = "Error", message } = e;
  60965. if (!stack) {
  60966. return stack;
  60967. }
  60968. const expectedPrefix = `${name}: ${message}
  60969. `;
  60970. if (stack.startsWith(expectedPrefix)) {
  60971. return stack.slice(expectedPrefix.length);
  60972. }
  60973. return stack;
  60974. }
  60975. const normalizeCodeFrame = (frame) => {
  60976. const trimmedPadding = frame.replace(/^\n|\n$/g, "");
  60977. return `
  60978. ${trimmedPadding}
  60979. `;
  60980. };
  60981. const enhanceRollupError = (e) => {
  60982. const stackOnly = extractStack(e);
  60983. let msg = colors$1.red((e.plugin ? `[${e.plugin}] ` : "") + e.message);
  60984. if (e.id) {
  60985. msg += `
  60986. file: ${colors$1.cyan(
  60987. e.id + (e.loc ? `:${e.loc.line}:${e.loc.column}` : "")
  60988. )}`;
  60989. }
  60990. if (e.frame) {
  60991. msg += `
  60992. ` + colors$1.yellow(normalizeCodeFrame(e.frame));
  60993. }
  60994. e.message = msg;
  60995. if (stackOnly !== void 0) {
  60996. e.stack = `${e.message}
  60997. ${stackOnly}`;
  60998. }
  60999. };
  61000. const outputBuildError = (e) => {
  61001. enhanceRollupError(e);
  61002. clearLine();
  61003. logger.error(e.message, { error: e });
  61004. };
  61005. let bundle;
  61006. let startTime;
  61007. try {
  61008. const buildOutputOptions = (output = {}) => {
  61009. if (output.output) {
  61010. logger.warn(
  61011. `You've set "rollupOptions.output.output" in your config. This is deprecated and will override all Vite.js default output options. Please use "rollupOptions.output" instead.`
  61012. );
  61013. }
  61014. if (output.file) {
  61015. throw new Error(
  61016. `Vite does not support "rollupOptions.output.file". Please use "rollupOptions.output.dir" and "rollupOptions.output.entryFileNames" instead.`
  61017. );
  61018. }
  61019. if (output.sourcemap) {
  61020. logger.warnOnce(
  61021. colors$1.yellow(
  61022. `Vite does not support "rollupOptions.output.sourcemap". Please use "build.sourcemap" instead.`
  61023. )
  61024. );
  61025. }
  61026. const ssrNodeBuild = ssr && config.ssr.target === "node";
  61027. const ssrWorkerBuild = ssr && config.ssr.target === "webworker";
  61028. const format = output.format || "es";
  61029. const jsExt = ssrNodeBuild || libOptions ? resolveOutputJsExtension(
  61030. format,
  61031. findNearestPackageData(root, packageCache)?.data.type
  61032. ) : "js";
  61033. return {
  61034. dir: outDir,
  61035. // Default format is 'es' for regular and for SSR builds
  61036. format,
  61037. exports: "auto",
  61038. sourcemap: options.sourcemap,
  61039. name: libOptions ? libOptions.name : void 0,
  61040. hoistTransitiveImports: libOptions ? false : void 0,
  61041. // es2015 enables `generatedCode.symbols`
  61042. // - #764 add `Symbol.toStringTag` when build es module into cjs chunk
  61043. // - #1048 add `Symbol.toStringTag` for module default export
  61044. generatedCode: "es2015",
  61045. entryFileNames: ssr ? `[name].${jsExt}` : libOptions ? ({ name }) => resolveLibFilename(
  61046. libOptions,
  61047. format,
  61048. name,
  61049. root,
  61050. jsExt,
  61051. packageCache
  61052. ) : path$n.posix.join(options.assetsDir, `[name]-[hash].${jsExt}`),
  61053. chunkFileNames: libOptions ? `[name]-[hash].${jsExt}` : path$n.posix.join(options.assetsDir, `[name]-[hash].${jsExt}`),
  61054. assetFileNames: libOptions ? `[name].[ext]` : path$n.posix.join(options.assetsDir, `[name]-[hash].[ext]`),
  61055. inlineDynamicImports: output.format === "umd" || output.format === "iife" || ssrWorkerBuild && (typeof input === "string" || Object.keys(input).length === 1),
  61056. ...output
  61057. };
  61058. };
  61059. const outputs = resolveBuildOutputs(
  61060. options.rollupOptions?.output,
  61061. libOptions,
  61062. logger
  61063. );
  61064. const normalizedOutputs = [];
  61065. if (Array.isArray(outputs)) {
  61066. for (const resolvedOutput of outputs) {
  61067. normalizedOutputs.push(buildOutputOptions(resolvedOutput));
  61068. }
  61069. } else {
  61070. normalizedOutputs.push(buildOutputOptions(outputs));
  61071. }
  61072. const resolvedOutDirs = getResolvedOutDirs(
  61073. root,
  61074. options.outDir,
  61075. options.rollupOptions?.output
  61076. );
  61077. const emptyOutDir = resolveEmptyOutDir(
  61078. options.emptyOutDir,
  61079. root,
  61080. resolvedOutDirs,
  61081. logger
  61082. );
  61083. if (config.build.watch) {
  61084. logger.info(colors$1.cyan(`
  61085. watching for file changes...`));
  61086. const resolvedChokidarOptions = resolveChokidarOptions(
  61087. config.build.watch.chokidar,
  61088. resolvedOutDirs,
  61089. emptyOutDir,
  61090. config.cacheDir
  61091. );
  61092. const { watch } = await import('rollup');
  61093. const watcher = watch({
  61094. ...rollupOptions,
  61095. output: normalizedOutputs,
  61096. watch: {
  61097. ...config.build.watch,
  61098. chokidar: resolvedChokidarOptions
  61099. }
  61100. });
  61101. watcher.on("event", (event) => {
  61102. if (event.code === "BUNDLE_START") {
  61103. logger.info(colors$1.cyan(`
  61104. build started...`));
  61105. if (options.write) {
  61106. prepareOutDir(resolvedOutDirs, emptyOutDir, config);
  61107. }
  61108. } else if (event.code === "BUNDLE_END") {
  61109. event.result.close();
  61110. logger.info(colors$1.cyan(`built in ${event.duration}ms.`));
  61111. } else if (event.code === "ERROR") {
  61112. outputBuildError(event.error);
  61113. }
  61114. });
  61115. return watcher;
  61116. }
  61117. const { rollup } = await import('rollup');
  61118. startTime = Date.now();
  61119. bundle = await rollup(rollupOptions);
  61120. if (options.write) {
  61121. prepareOutDir(resolvedOutDirs, emptyOutDir, config);
  61122. }
  61123. const res = [];
  61124. for (const output of normalizedOutputs) {
  61125. res.push(await bundle[options.write ? "write" : "generate"](output));
  61126. }
  61127. logger.info(
  61128. `${colors$1.green(`\u2713 built in ${displayTime(Date.now() - startTime)}`)}`
  61129. );
  61130. return Array.isArray(outputs) ? res : res[0];
  61131. } catch (e) {
  61132. enhanceRollupError(e);
  61133. clearLine();
  61134. if (startTime) {
  61135. logger.error(
  61136. `${colors$1.red("x")} Build failed in ${displayTime(Date.now() - startTime)}`
  61137. );
  61138. startTime = void 0;
  61139. }
  61140. throw e;
  61141. } finally {
  61142. if (bundle) await bundle.close();
  61143. }
  61144. }
  61145. function prepareOutDir(outDirs, emptyOutDir, config) {
  61146. const outDirsArray = [...outDirs];
  61147. for (const outDir of outDirs) {
  61148. if (emptyOutDir !== false && fs__default.existsSync(outDir)) {
  61149. const skipDirs = outDirsArray.map((dir) => {
  61150. const relative = path$n.relative(outDir, dir);
  61151. if (relative && !relative.startsWith("..") && !path$n.isAbsolute(relative)) {
  61152. return relative;
  61153. }
  61154. return "";
  61155. }).filter(Boolean);
  61156. emptyDir(outDir, [...skipDirs, ".git"]);
  61157. }
  61158. if (config.build.copyPublicDir && config.publicDir && fs__default.existsSync(config.publicDir)) {
  61159. if (!areSeparateFolders(outDir, config.publicDir)) {
  61160. config.logger.warn(
  61161. colors$1.yellow(
  61162. `
  61163. ${colors$1.bold(
  61164. `(!)`
  61165. )} The public directory feature may not work correctly. outDir ${colors$1.white(
  61166. colors$1.dim(outDir)
  61167. )} and publicDir ${colors$1.white(
  61168. colors$1.dim(config.publicDir)
  61169. )} are not separate folders.
  61170. `
  61171. )
  61172. );
  61173. }
  61174. copyDir(config.publicDir, outDir);
  61175. }
  61176. }
  61177. }
  61178. function getPkgName(name) {
  61179. return name?.[0] === "@" ? name.split("/")[1] : name;
  61180. }
  61181. function resolveOutputJsExtension(format, type = "commonjs") {
  61182. if (type === "module") {
  61183. return format === "cjs" || format === "umd" ? "cjs" : "js";
  61184. } else {
  61185. return format === "es" ? "mjs" : "js";
  61186. }
  61187. }
  61188. function resolveLibFilename(libOptions, format, entryName, root, extension, packageCache) {
  61189. if (typeof libOptions.fileName === "function") {
  61190. return libOptions.fileName(format, entryName);
  61191. }
  61192. const packageJson = findNearestPackageData(root, packageCache)?.data;
  61193. const name = libOptions.fileName || (packageJson && typeof libOptions.entry === "string" ? getPkgName(packageJson.name) : entryName);
  61194. if (!name)
  61195. throw new Error(
  61196. 'Name in package.json is required if option "build.lib.fileName" is not provided.'
  61197. );
  61198. extension ??= resolveOutputJsExtension(format, packageJson?.type);
  61199. if (format === "cjs" || format === "es") {
  61200. return `${name}.${extension}`;
  61201. }
  61202. return `${name}.${format}.${extension}`;
  61203. }
  61204. function resolveBuildOutputs(outputs, libOptions, logger) {
  61205. if (libOptions) {
  61206. const libHasMultipleEntries = typeof libOptions.entry !== "string" && Object.values(libOptions.entry).length > 1;
  61207. const libFormats = libOptions.formats || (libHasMultipleEntries ? ["es", "cjs"] : ["es", "umd"]);
  61208. if (!Array.isArray(outputs)) {
  61209. if (libFormats.includes("umd") || libFormats.includes("iife")) {
  61210. if (libHasMultipleEntries) {
  61211. throw new Error(
  61212. 'Multiple entry points are not supported when output formats include "umd" or "iife".'
  61213. );
  61214. }
  61215. if (!libOptions.name) {
  61216. throw new Error(
  61217. 'Option "build.lib.name" is required when output formats include "umd" or "iife".'
  61218. );
  61219. }
  61220. }
  61221. return libFormats.map((format) => ({ ...outputs, format }));
  61222. }
  61223. if (libOptions.formats) {
  61224. logger.warn(
  61225. colors$1.yellow(
  61226. '"build.lib.formats" will be ignored because "build.rollupOptions.output" is already an array format.'
  61227. )
  61228. );
  61229. }
  61230. outputs.forEach((output) => {
  61231. if ((output.format === "umd" || output.format === "iife") && !output.name) {
  61232. throw new Error(
  61233. 'Entries in "build.rollupOptions.output" must specify "name" when the format is "umd" or "iife".'
  61234. );
  61235. }
  61236. });
  61237. }
  61238. return outputs;
  61239. }
  61240. const warningIgnoreList = [`CIRCULAR_DEPENDENCY`, `THIS_IS_UNDEFINED`];
  61241. const dynamicImportWarningIgnoreList = [
  61242. `Unsupported expression`,
  61243. `statically analyzed`
  61244. ];
  61245. function clearLine() {
  61246. const tty = process.stdout.isTTY && !process.env.CI;
  61247. if (tty) {
  61248. process.stdout.clearLine(0);
  61249. process.stdout.cursorTo(0);
  61250. }
  61251. }
  61252. function onRollupWarning(warning, warn, config) {
  61253. const viteWarn = (warnLog) => {
  61254. let warning2;
  61255. if (typeof warnLog === "function") {
  61256. warning2 = warnLog();
  61257. } else {
  61258. warning2 = warnLog;
  61259. }
  61260. if (typeof warning2 === "object") {
  61261. if (warning2.code === "UNRESOLVED_IMPORT") {
  61262. const id = warning2.id;
  61263. const exporter = warning2.exporter;
  61264. if (!id || !id.endsWith("?commonjs-external")) {
  61265. throw new Error(
  61266. `[vite]: Rollup failed to resolve import "${exporter}" from "${id}".
  61267. This is most likely unintended because it can break your application at runtime.
  61268. If you do want to externalize this module explicitly add it to
  61269. \`build.rollupOptions.external\``
  61270. );
  61271. }
  61272. }
  61273. if (warning2.plugin === "rollup-plugin-dynamic-import-variables" && dynamicImportWarningIgnoreList.some(
  61274. (msg) => warning2.message.includes(msg)
  61275. )) {
  61276. return;
  61277. }
  61278. if (warningIgnoreList.includes(warning2.code)) {
  61279. return;
  61280. }
  61281. if (warning2.code === "PLUGIN_WARNING") {
  61282. config.logger.warn(
  61283. `${colors$1.bold(
  61284. colors$1.yellow(`[plugin:${warning2.plugin}]`)
  61285. )} ${colors$1.yellow(warning2.message)}`
  61286. );
  61287. return;
  61288. }
  61289. }
  61290. warn(warnLog);
  61291. };
  61292. clearLine();
  61293. const userOnWarn = config.build.rollupOptions?.onwarn;
  61294. if (userOnWarn) {
  61295. userOnWarn(warning, viteWarn);
  61296. } else {
  61297. viteWarn(warning);
  61298. }
  61299. }
  61300. function resolveUserExternal(user, id, parentId, isResolved) {
  61301. if (typeof user === "function") {
  61302. return user(id, parentId, isResolved);
  61303. } else if (Array.isArray(user)) {
  61304. return user.some((test) => isExternal(id, test));
  61305. } else {
  61306. return isExternal(id, user);
  61307. }
  61308. }
  61309. function isExternal(id, test) {
  61310. if (typeof test === "string") {
  61311. return id === test;
  61312. } else {
  61313. return test.test(id);
  61314. }
  61315. }
  61316. function injectSsrFlagToHooks(plugin) {
  61317. const { resolveId, load, transform } = plugin;
  61318. return {
  61319. ...plugin,
  61320. resolveId: wrapSsrResolveId(resolveId),
  61321. load: wrapSsrLoad(load),
  61322. transform: wrapSsrTransform(transform)
  61323. };
  61324. }
  61325. function wrapSsrResolveId(hook) {
  61326. if (!hook) return;
  61327. const fn = getHookHandler(hook);
  61328. const handler = function(id, importer, options) {
  61329. return fn.call(this, id, importer, injectSsrFlag(options));
  61330. };
  61331. if ("handler" in hook) {
  61332. return {
  61333. ...hook,
  61334. handler
  61335. };
  61336. } else {
  61337. return handler;
  61338. }
  61339. }
  61340. function wrapSsrLoad(hook) {
  61341. if (!hook) return;
  61342. const fn = getHookHandler(hook);
  61343. const handler = function(id, ...args) {
  61344. return fn.call(this, id, injectSsrFlag(args[0]));
  61345. };
  61346. if ("handler" in hook) {
  61347. return {
  61348. ...hook,
  61349. handler
  61350. };
  61351. } else {
  61352. return handler;
  61353. }
  61354. }
  61355. function wrapSsrTransform(hook) {
  61356. if (!hook) return;
  61357. const fn = getHookHandler(hook);
  61358. const handler = function(code, importer, ...args) {
  61359. return fn.call(this, code, importer, injectSsrFlag(args[0]));
  61360. };
  61361. if ("handler" in hook) {
  61362. return {
  61363. ...hook,
  61364. handler
  61365. };
  61366. } else {
  61367. return handler;
  61368. }
  61369. }
  61370. function injectSsrFlag(options) {
  61371. return { ...options ?? {}, ssr: true };
  61372. }
  61373. const needsEscapeRegEx = /[\n\r'\\\u2028\u2029]/;
  61374. const quoteNewlineRegEx = /([\n\r'\u2028\u2029])/g;
  61375. const backSlashRegEx = /\\/g;
  61376. function escapeId(id) {
  61377. if (!needsEscapeRegEx.test(id)) return id;
  61378. return id.replace(backSlashRegEx, "\\\\").replace(quoteNewlineRegEx, "\\$1");
  61379. }
  61380. const getResolveUrl = (path2, URL = "URL") => `new ${URL}(${path2}).href`;
  61381. const getRelativeUrlFromDocument = (relativePath, umd = false) => getResolveUrl(
  61382. `'${escapeId(partialEncodeURIPath(relativePath))}', ${umd ? `typeof document === 'undefined' ? location.href : ` : ""}document.currentScript && document.currentScript.tagName.toUpperCase() === 'SCRIPT' && document.currentScript.src || document.baseURI`
  61383. );
  61384. const getFileUrlFromFullPath = (path2) => `require('u' + 'rl').pathToFileURL(${path2}).href`;
  61385. const getFileUrlFromRelativePath = (path2) => getFileUrlFromFullPath(`__dirname + '/${escapeId(path2)}'`);
  61386. const relativeUrlMechanisms = {
  61387. amd: (relativePath) => {
  61388. if (relativePath[0] !== ".") relativePath = "./" + relativePath;
  61389. return getResolveUrl(
  61390. `require.toUrl('${escapeId(relativePath)}'), document.baseURI`
  61391. );
  61392. },
  61393. cjs: (relativePath) => `(typeof document === 'undefined' ? ${getFileUrlFromRelativePath(
  61394. relativePath
  61395. )} : ${getRelativeUrlFromDocument(relativePath)})`,
  61396. es: (relativePath) => getResolveUrl(
  61397. `'${escapeId(partialEncodeURIPath(relativePath))}', import.meta.url`
  61398. ),
  61399. iife: (relativePath) => getRelativeUrlFromDocument(relativePath),
  61400. // NOTE: make sure rollup generate `module` params
  61401. system: (relativePath) => getResolveUrl(
  61402. `'${escapeId(partialEncodeURIPath(relativePath))}', module.meta.url`
  61403. ),
  61404. umd: (relativePath) => `(typeof document === 'undefined' && typeof location === 'undefined' ? ${getFileUrlFromRelativePath(
  61405. relativePath
  61406. )} : ${getRelativeUrlFromDocument(relativePath, true)})`
  61407. };
  61408. const customRelativeUrlMechanisms = {
  61409. ...relativeUrlMechanisms,
  61410. "worker-iife": (relativePath) => getResolveUrl(
  61411. `'${escapeId(partialEncodeURIPath(relativePath))}', self.location.href`
  61412. )
  61413. };
  61414. function toOutputFilePathInJS(filename, type, hostId, hostType, config, toRelative) {
  61415. const { renderBuiltUrl } = config.experimental;
  61416. let relative = config.base === "" || config.base === "./";
  61417. if (renderBuiltUrl) {
  61418. const result = renderBuiltUrl(filename, {
  61419. hostId,
  61420. hostType,
  61421. type,
  61422. ssr: !!config.build.ssr
  61423. });
  61424. if (typeof result === "object") {
  61425. if (result.runtime) {
  61426. return { runtime: result.runtime };
  61427. }
  61428. if (typeof result.relative === "boolean") {
  61429. relative = result.relative;
  61430. }
  61431. } else if (result) {
  61432. return result;
  61433. }
  61434. }
  61435. if (relative && !config.build.ssr) {
  61436. return toRelative(filename, hostId);
  61437. }
  61438. return joinUrlSegments(config.decodedBase, filename);
  61439. }
  61440. function createToImportMetaURLBasedRelativeRuntime(format, isWorker) {
  61441. const formatLong = isWorker && format === "iife" ? "worker-iife" : format;
  61442. const toRelativePath = customRelativeUrlMechanisms[formatLong];
  61443. return (filename, importer) => ({
  61444. runtime: toRelativePath(
  61445. path$n.posix.relative(path$n.dirname(importer), filename)
  61446. )
  61447. });
  61448. }
  61449. function toOutputFilePathWithoutRuntime(filename, type, hostId, hostType, config, toRelative) {
  61450. const { renderBuiltUrl } = config.experimental;
  61451. let relative = config.base === "" || config.base === "./";
  61452. if (renderBuiltUrl) {
  61453. const result = renderBuiltUrl(filename, {
  61454. hostId,
  61455. hostType,
  61456. type,
  61457. ssr: !!config.build.ssr
  61458. });
  61459. if (typeof result === "object") {
  61460. if (result.runtime) {
  61461. throw new Error(
  61462. `{ runtime: "${result.runtime}" } is not supported for assets in ${hostType} files: ${filename}`
  61463. );
  61464. }
  61465. if (typeof result.relative === "boolean") {
  61466. relative = result.relative;
  61467. }
  61468. } else if (result) {
  61469. return result;
  61470. }
  61471. }
  61472. if (relative && !config.build.ssr) {
  61473. return toRelative(filename, hostId);
  61474. } else {
  61475. return joinUrlSegments(config.decodedBase, filename);
  61476. }
  61477. }
  61478. const toOutputFilePathInCss = toOutputFilePathWithoutRuntime;
  61479. const toOutputFilePathInHtml = toOutputFilePathWithoutRuntime;
  61480. function areSeparateFolders(a, b) {
  61481. const na = normalizePath$3(a);
  61482. const nb = normalizePath$3(b);
  61483. return na !== nb && !na.startsWith(withTrailingSlash(nb)) && !nb.startsWith(withTrailingSlash(na));
  61484. }
  61485. var build$1 = {
  61486. __proto__: null,
  61487. build: build,
  61488. createToImportMetaURLBasedRelativeRuntime: createToImportMetaURLBasedRelativeRuntime,
  61489. onRollupWarning: onRollupWarning,
  61490. resolveBuildOptions: resolveBuildOptions,
  61491. resolveBuildOutputs: resolveBuildOutputs,
  61492. resolveBuildPlugins: resolveBuildPlugins,
  61493. resolveLibFilename: resolveLibFilename,
  61494. resolveUserExternal: resolveUserExternal,
  61495. toOutputFilePathInCss: toOutputFilePathInCss,
  61496. toOutputFilePathInHtml: toOutputFilePathInHtml,
  61497. toOutputFilePathInJS: toOutputFilePathInJS,
  61498. toOutputFilePathWithoutRuntime: toOutputFilePathWithoutRuntime
  61499. };
  61500. // NOTE: supports Node 6.x
  61501. const NOOP = () => {};
  61502. const MIMES = /text|javascript|\/json|xml/i;
  61503. /**
  61504. * @param {any} chunk
  61505. * @param {BufferEncoding} enc
  61506. * @returns {number}
  61507. */
  61508. function getChunkSize(chunk, enc) {
  61509. return chunk ? Buffer.byteLength(chunk, enc) : 0;
  61510. }
  61511. /**
  61512. * @param {import('./index.d.mts').Options} [options]
  61513. * @returns {import('./index.d.mts').Middleware}
  61514. */
  61515. function compression ({ threshold = 1024, level = -1, brotli = false, gzip = true, mimes = MIMES } = {}) {
  61516. const brotliOpts = (typeof brotli === 'object' && brotli) || {};
  61517. const gzipOpts = (typeof gzip === 'object' && gzip) || {};
  61518. // disable Brotli on Node<12.7 where it is unsupported:
  61519. if (!zlib$1.createBrotliCompress) brotli = false;
  61520. return (req, res, next = NOOP) => {
  61521. const accept = req.headers['accept-encoding'] + '';
  61522. const encoding = ((brotli && accept.match(/\bbr\b/)) || (gzip && accept.match(/\bgzip\b/)) || [])[0];
  61523. // skip if no response body or no supported encoding:
  61524. if (req.method === 'HEAD' || !encoding) return next();
  61525. /** @type {zlib.Gzip | zlib.BrotliCompress} */
  61526. let compress;
  61527. /** @type {Array<[string, function]>?} */
  61528. let pendingListeners = [];
  61529. let pendingStatus = 0;
  61530. let started = false;
  61531. let size = 0;
  61532. function start() {
  61533. started = true;
  61534. // @ts-ignore
  61535. size = res.getHeader('Content-Length') | 0 || size;
  61536. const compressible = mimes.test(
  61537. String(res.getHeader('Content-Type') || 'text/plain')
  61538. );
  61539. const cleartext = !res.getHeader('Content-Encoding');
  61540. const listeners = pendingListeners || [];
  61541. if (compressible && cleartext && size >= threshold) {
  61542. res.setHeader('Content-Encoding', encoding);
  61543. res.removeHeader('Content-Length');
  61544. if (encoding === 'br') {
  61545. compress = zlib$1.createBrotliCompress({
  61546. params: Object.assign({
  61547. [zlib$1.constants.BROTLI_PARAM_QUALITY]: level,
  61548. [zlib$1.constants.BROTLI_PARAM_SIZE_HINT]: size,
  61549. }, brotliOpts)
  61550. });
  61551. } else {
  61552. compress = zlib$1.createGzip(
  61553. Object.assign({ level }, gzipOpts)
  61554. );
  61555. }
  61556. // backpressure
  61557. compress.on('data', chunk => write.call(res, chunk) || compress.pause());
  61558. on.call(res, 'drain', () => compress.resume());
  61559. compress.on('end', () => end.call(res));
  61560. listeners.forEach(p => compress.on.apply(compress, p));
  61561. } else {
  61562. pendingListeners = null;
  61563. listeners.forEach(p => on.apply(res, p));
  61564. }
  61565. writeHead.call(res, pendingStatus || res.statusCode);
  61566. }
  61567. const { end, write, on, writeHead } = res;
  61568. res.writeHead = function (status, reason, headers) {
  61569. if (typeof reason !== 'string') [headers, reason] = [reason, headers];
  61570. if (headers) for (let k in headers) res.setHeader(k, headers[k]);
  61571. pendingStatus = status;
  61572. return this;
  61573. };
  61574. res.write = function (chunk, enc) {
  61575. size += getChunkSize(chunk, enc);
  61576. if (!started) start();
  61577. if (!compress) return write.apply(this, arguments);
  61578. return compress.write.apply(compress, arguments);
  61579. };
  61580. res.end = function (chunk, enc) {
  61581. if (arguments.length > 0 && typeof chunk !== 'function') {
  61582. size += getChunkSize(chunk, enc);
  61583. }
  61584. if (!started) start();
  61585. if (!compress) return end.apply(this, arguments);
  61586. return compress.end.apply(compress, arguments);
  61587. };
  61588. res.on = function (type, listener) {
  61589. if (!pendingListeners) on.call(this, type, listener);
  61590. else if (compress) compress.on(type, listener);
  61591. else pendingListeners.push([type, listener]);
  61592. return this;
  61593. };
  61594. next();
  61595. };
  61596. }
  61597. function resolvePreviewOptions(preview2, server) {
  61598. return {
  61599. port: preview2?.port,
  61600. strictPort: preview2?.strictPort ?? server.strictPort,
  61601. host: preview2?.host ?? server.host,
  61602. https: preview2?.https ?? server.https,
  61603. open: preview2?.open ?? server.open,
  61604. proxy: preview2?.proxy ?? server.proxy,
  61605. cors: preview2?.cors ?? server.cors,
  61606. headers: preview2?.headers ?? server.headers
  61607. };
  61608. }
  61609. async function preview(inlineConfig = {}) {
  61610. const config = await resolveConfig(
  61611. inlineConfig,
  61612. "serve",
  61613. "production",
  61614. "production",
  61615. true
  61616. );
  61617. const distDir = path$n.resolve(config.root, config.build.outDir);
  61618. if (!fs__default.existsSync(distDir) && // error if no plugins implement `configurePreviewServer`
  61619. config.plugins.every((plugin) => !plugin.configurePreviewServer) && // error if called in CLI only. programmatic usage could access `httpServer`
  61620. // and affect file serving
  61621. process.argv[1]?.endsWith(path$n.normalize("bin/vite.js")) && process.argv[2] === "preview") {
  61622. throw new Error(
  61623. `The directory "${config.build.outDir}" does not exist. Did you build your project?`
  61624. );
  61625. }
  61626. const app = connect$1();
  61627. const httpServer = await resolveHttpServer(
  61628. config.preview,
  61629. app,
  61630. await resolveHttpsConfig(config.preview?.https)
  61631. );
  61632. setClientErrorHandler(httpServer, config.logger);
  61633. const options = config.preview;
  61634. const logger = config.logger;
  61635. const closeHttpServer = createServerCloseFn(httpServer);
  61636. const server = {
  61637. config,
  61638. middlewares: app,
  61639. httpServer,
  61640. async close() {
  61641. teardownSIGTERMListener(closeServerAndExit);
  61642. await closeHttpServer();
  61643. },
  61644. resolvedUrls: null,
  61645. printUrls() {
  61646. if (server.resolvedUrls) {
  61647. printServerUrls(server.resolvedUrls, options.host, logger.info);
  61648. } else {
  61649. throw new Error("cannot print server URLs before server is listening.");
  61650. }
  61651. },
  61652. bindCLIShortcuts(options2) {
  61653. bindCLIShortcuts(server, options2);
  61654. }
  61655. };
  61656. const closeServerAndExit = async () => {
  61657. try {
  61658. await server.close();
  61659. } finally {
  61660. process.exit();
  61661. }
  61662. };
  61663. setupSIGTERMListener(closeServerAndExit);
  61664. const postHooks = [];
  61665. for (const hook of config.getSortedPluginHooks("configurePreviewServer")) {
  61666. postHooks.push(await hook(server));
  61667. }
  61668. const { cors } = config.preview;
  61669. if (cors !== false) {
  61670. app.use(corsMiddleware(typeof cors === "boolean" ? {} : cors));
  61671. }
  61672. const { proxy } = config.preview;
  61673. if (proxy) {
  61674. app.use(proxyMiddleware(httpServer, proxy, config));
  61675. }
  61676. app.use(compression());
  61677. if (config.base !== "/") {
  61678. app.use(baseMiddleware(config.rawBase, false));
  61679. }
  61680. const headers = config.preview.headers;
  61681. const viteAssetMiddleware = (...args) => sirv(distDir, {
  61682. etag: true,
  61683. dev: true,
  61684. extensions: [],
  61685. ignores: false,
  61686. setHeaders(res) {
  61687. if (headers) {
  61688. for (const name in headers) {
  61689. res.setHeader(name, headers[name]);
  61690. }
  61691. }
  61692. },
  61693. shouldServe(filePath) {
  61694. return shouldServeFile(filePath, distDir);
  61695. }
  61696. })(...args);
  61697. app.use(viteAssetMiddleware);
  61698. if (config.appType === "spa" || config.appType === "mpa") {
  61699. app.use(htmlFallbackMiddleware(distDir, config.appType === "spa"));
  61700. }
  61701. postHooks.forEach((fn) => fn && fn());
  61702. if (config.appType === "spa" || config.appType === "mpa") {
  61703. app.use(indexHtmlMiddleware(distDir, server));
  61704. app.use(notFoundMiddleware());
  61705. }
  61706. const hostname = await resolveHostname(options.host);
  61707. const port = options.port ?? DEFAULT_PREVIEW_PORT;
  61708. await httpServerStart(httpServer, {
  61709. port,
  61710. strictPort: options.strictPort,
  61711. host: hostname.host,
  61712. logger
  61713. });
  61714. server.resolvedUrls = await resolveServerUrls(
  61715. httpServer,
  61716. config.preview,
  61717. config
  61718. );
  61719. if (options.open) {
  61720. const url = server.resolvedUrls?.local[0] ?? server.resolvedUrls?.network[0];
  61721. if (url) {
  61722. const path2 = typeof options.open === "string" ? new URL(options.open, url).href : url;
  61723. openBrowser(path2, true, logger);
  61724. }
  61725. }
  61726. return server;
  61727. }
  61728. var preview$1 = {
  61729. __proto__: null,
  61730. preview: preview,
  61731. resolvePreviewOptions: resolvePreviewOptions
  61732. };
  61733. function resolveSSROptions(ssr, preserveSymlinks) {
  61734. ssr ??= {};
  61735. const optimizeDeps = ssr.optimizeDeps ?? {};
  61736. const target = "node";
  61737. return {
  61738. target,
  61739. ...ssr,
  61740. optimizeDeps: {
  61741. ...optimizeDeps,
  61742. noDiscovery: true,
  61743. // always true for ssr
  61744. esbuildOptions: {
  61745. preserveSymlinks,
  61746. ...optimizeDeps.esbuildOptions
  61747. }
  61748. }
  61749. };
  61750. }
  61751. const debug = createDebugger("vite:config");
  61752. const promisifiedRealpath = promisify$4(fs__default.realpath);
  61753. function defineConfig(config) {
  61754. return config;
  61755. }
  61756. function checkBadCharactersInPath(path2, logger) {
  61757. const badChars = [];
  61758. if (path2.includes("#")) {
  61759. badChars.push("#");
  61760. }
  61761. if (path2.includes("?")) {
  61762. badChars.push("?");
  61763. }
  61764. if (badChars.length > 0) {
  61765. const charString = badChars.map((c) => `"${c}"`).join(" and ");
  61766. const inflectedChars = badChars.length > 1 ? "characters" : "character";
  61767. logger.warn(
  61768. colors$1.yellow(
  61769. `The project root contains the ${charString} ${inflectedChars} (${colors$1.cyan(
  61770. path2
  61771. )}), which may not work when running Vite. Consider renaming the directory to remove the characters.`
  61772. )
  61773. );
  61774. }
  61775. }
  61776. async function resolveConfig(inlineConfig, command, defaultMode = "development", defaultNodeEnv = "development", isPreview = false) {
  61777. let config = inlineConfig;
  61778. let configFileDependencies = [];
  61779. let mode = inlineConfig.mode || defaultMode;
  61780. const isNodeEnvSet = !!process.env.NODE_ENV;
  61781. const packageCache = /* @__PURE__ */ new Map();
  61782. if (!isNodeEnvSet) {
  61783. process.env.NODE_ENV = defaultNodeEnv;
  61784. }
  61785. const configEnv = {
  61786. mode,
  61787. command,
  61788. isSsrBuild: command === "build" && !!config.build?.ssr,
  61789. isPreview
  61790. };
  61791. let { configFile } = config;
  61792. if (configFile !== false) {
  61793. const loadResult = await loadConfigFromFile(
  61794. configEnv,
  61795. configFile,
  61796. config.root,
  61797. config.logLevel,
  61798. config.customLogger
  61799. );
  61800. if (loadResult) {
  61801. config = mergeConfig(loadResult.config, config);
  61802. configFile = loadResult.path;
  61803. configFileDependencies = loadResult.dependencies;
  61804. }
  61805. }
  61806. mode = inlineConfig.mode || config.mode || mode;
  61807. configEnv.mode = mode;
  61808. const filterPlugin = (p) => {
  61809. if (!p) {
  61810. return false;
  61811. } else if (!p.apply) {
  61812. return true;
  61813. } else if (typeof p.apply === "function") {
  61814. return p.apply({ ...config, mode }, configEnv);
  61815. } else {
  61816. return p.apply === command;
  61817. }
  61818. };
  61819. const rawUserPlugins = (await asyncFlatten(config.plugins || [])).filter(filterPlugin);
  61820. const [prePlugins, normalPlugins, postPlugins] = sortUserPlugins(rawUserPlugins);
  61821. const userPlugins = [...prePlugins, ...normalPlugins, ...postPlugins];
  61822. config = await runConfigHook(config, userPlugins, configEnv);
  61823. const logger = createLogger(config.logLevel, {
  61824. allowClearScreen: config.clearScreen,
  61825. customLogger: config.customLogger
  61826. });
  61827. const resolvedRoot = normalizePath$3(
  61828. config.root ? path$n.resolve(config.root) : process.cwd()
  61829. );
  61830. checkBadCharactersInPath(resolvedRoot, logger);
  61831. const clientAlias = [
  61832. {
  61833. find: /^\/?@vite\/env/,
  61834. replacement: path$n.posix.join(FS_PREFIX, normalizePath$3(ENV_ENTRY))
  61835. },
  61836. {
  61837. find: /^\/?@vite\/client/,
  61838. replacement: path$n.posix.join(FS_PREFIX, normalizePath$3(CLIENT_ENTRY))
  61839. }
  61840. ];
  61841. const resolvedAlias = normalizeAlias(
  61842. mergeAlias(clientAlias, config.resolve?.alias || [])
  61843. );
  61844. const resolveOptions = {
  61845. mainFields: config.resolve?.mainFields ?? DEFAULT_MAIN_FIELDS,
  61846. conditions: config.resolve?.conditions ?? [],
  61847. extensions: config.resolve?.extensions ?? DEFAULT_EXTENSIONS,
  61848. dedupe: config.resolve?.dedupe ?? [],
  61849. preserveSymlinks: config.resolve?.preserveSymlinks ?? false,
  61850. alias: resolvedAlias
  61851. };
  61852. if (
  61853. // @ts-expect-error removed field
  61854. config.resolve?.browserField === false && resolveOptions.mainFields.includes("browser")
  61855. ) {
  61856. logger.warn(
  61857. colors$1.yellow(
  61858. `\`resolve.browserField\` is set to false, but the option is removed in favour of the 'browser' string in \`resolve.mainFields\`. You may want to update \`resolve.mainFields\` to remove the 'browser' string and preserve the previous browser behaviour.`
  61859. )
  61860. );
  61861. }
  61862. const envDir = config.envDir ? normalizePath$3(path$n.resolve(resolvedRoot, config.envDir)) : resolvedRoot;
  61863. const userEnv = inlineConfig.envFile !== false && loadEnv(mode, envDir, resolveEnvPrefix(config));
  61864. const userNodeEnv = process.env.VITE_USER_NODE_ENV;
  61865. if (!isNodeEnvSet && userNodeEnv) {
  61866. if (userNodeEnv === "development") {
  61867. process.env.NODE_ENV = "development";
  61868. } else {
  61869. logger.warn(
  61870. `NODE_ENV=${userNodeEnv} is not supported in the .env file. Only NODE_ENV=development is supported to create a development build of your project. If you need to set process.env.NODE_ENV, you can set it in the Vite config instead.`
  61871. );
  61872. }
  61873. }
  61874. const isProduction = process.env.NODE_ENV === "production";
  61875. const isBuild = command === "build";
  61876. const relativeBaseShortcut = config.base === "" || config.base === "./";
  61877. const resolvedBase = relativeBaseShortcut ? !isBuild || config.build?.ssr ? "/" : "./" : resolveBaseUrl(config.base, isBuild, logger) ?? "/";
  61878. const resolvedBuildOptions = resolveBuildOptions(
  61879. config.build,
  61880. logger,
  61881. resolvedRoot
  61882. );
  61883. const pkgDir = findNearestPackageData(resolvedRoot, packageCache)?.dir;
  61884. const cacheDir = normalizePath$3(
  61885. config.cacheDir ? path$n.resolve(resolvedRoot, config.cacheDir) : pkgDir ? path$n.join(pkgDir, `node_modules/.vite`) : path$n.join(resolvedRoot, `.vite`)
  61886. );
  61887. const assetsFilter = config.assetsInclude && (!Array.isArray(config.assetsInclude) || config.assetsInclude.length) ? createFilter(config.assetsInclude) : () => false;
  61888. const createResolver = (options) => {
  61889. let aliasContainer;
  61890. let resolverContainer;
  61891. return async (id, importer, aliasOnly, ssr2) => {
  61892. let container;
  61893. if (aliasOnly) {
  61894. container = aliasContainer || (aliasContainer = await createPluginContainer({
  61895. ...resolved,
  61896. plugins: [alias$1({ entries: resolved.resolve.alias })]
  61897. }));
  61898. } else {
  61899. container = resolverContainer || (resolverContainer = await createPluginContainer({
  61900. ...resolved,
  61901. plugins: [
  61902. alias$1({ entries: resolved.resolve.alias }),
  61903. resolvePlugin({
  61904. ...resolved.resolve,
  61905. root: resolvedRoot,
  61906. isProduction,
  61907. isBuild: command === "build",
  61908. ssrConfig: resolved.ssr,
  61909. asSrc: true,
  61910. preferRelative: false,
  61911. tryIndex: true,
  61912. ...options,
  61913. idOnly: true,
  61914. fsUtils: getFsUtils(resolved)
  61915. })
  61916. ]
  61917. }));
  61918. }
  61919. return (await container.resolveId(id, importer, {
  61920. ssr: ssr2,
  61921. scan: options?.scan
  61922. }))?.id;
  61923. };
  61924. };
  61925. const { publicDir } = config;
  61926. const resolvedPublicDir = publicDir !== false && publicDir !== "" ? normalizePath$3(
  61927. path$n.resolve(
  61928. resolvedRoot,
  61929. typeof publicDir === "string" ? publicDir : "public"
  61930. )
  61931. ) : "";
  61932. const server = resolveServerOptions(resolvedRoot, config.server, logger);
  61933. const ssr = resolveSSROptions(config.ssr, resolveOptions.preserveSymlinks);
  61934. const optimizeDeps = config.optimizeDeps || {};
  61935. const BASE_URL = resolvedBase;
  61936. let resolved;
  61937. let createUserWorkerPlugins = config.worker?.plugins;
  61938. if (Array.isArray(createUserWorkerPlugins)) {
  61939. createUserWorkerPlugins = () => config.worker?.plugins;
  61940. logger.warn(
  61941. colors$1.yellow(
  61942. `worker.plugins is now a function that returns an array of plugins. Please update your Vite config accordingly.
  61943. `
  61944. )
  61945. );
  61946. }
  61947. const createWorkerPlugins = async function(bundleChain) {
  61948. const rawWorkerUserPlugins = (await asyncFlatten(createUserWorkerPlugins?.() || [])).filter(filterPlugin);
  61949. let workerConfig = mergeConfig({}, config);
  61950. const [workerPrePlugins, workerNormalPlugins, workerPostPlugins] = sortUserPlugins(rawWorkerUserPlugins);
  61951. const workerUserPlugins = [
  61952. ...workerPrePlugins,
  61953. ...workerNormalPlugins,
  61954. ...workerPostPlugins
  61955. ];
  61956. workerConfig = await runConfigHook(
  61957. workerConfig,
  61958. workerUserPlugins,
  61959. configEnv
  61960. );
  61961. const workerResolved = {
  61962. ...workerConfig,
  61963. ...resolved,
  61964. isWorker: true,
  61965. mainConfig: resolved,
  61966. bundleChain
  61967. };
  61968. const resolvedWorkerPlugins = await resolvePlugins(
  61969. workerResolved,
  61970. workerPrePlugins,
  61971. workerNormalPlugins,
  61972. workerPostPlugins
  61973. );
  61974. await Promise.all(
  61975. createPluginHookUtils(resolvedWorkerPlugins).getSortedPluginHooks("configResolved").map((hook) => hook(workerResolved))
  61976. );
  61977. return resolvedWorkerPlugins;
  61978. };
  61979. const resolvedWorkerOptions = {
  61980. format: config.worker?.format || "iife",
  61981. plugins: createWorkerPlugins,
  61982. rollupOptions: config.worker?.rollupOptions || {}
  61983. };
  61984. const base = withTrailingSlash(resolvedBase);
  61985. resolved = {
  61986. configFile: configFile ? normalizePath$3(configFile) : void 0,
  61987. configFileDependencies: configFileDependencies.map(
  61988. (name) => normalizePath$3(path$n.resolve(name))
  61989. ),
  61990. inlineConfig,
  61991. root: resolvedRoot,
  61992. base,
  61993. decodedBase: decodeURI(base),
  61994. rawBase: resolvedBase,
  61995. resolve: resolveOptions,
  61996. publicDir: resolvedPublicDir,
  61997. cacheDir,
  61998. command,
  61999. mode,
  62000. ssr,
  62001. isWorker: false,
  62002. mainConfig: null,
  62003. bundleChain: [],
  62004. isProduction,
  62005. plugins: userPlugins,
  62006. css: resolveCSSOptions(config.css),
  62007. esbuild: config.esbuild === false ? false : {
  62008. jsxDev: !isProduction,
  62009. ...config.esbuild
  62010. },
  62011. server,
  62012. build: resolvedBuildOptions,
  62013. preview: resolvePreviewOptions(config.preview, server),
  62014. envDir,
  62015. env: {
  62016. ...userEnv,
  62017. BASE_URL,
  62018. MODE: mode,
  62019. DEV: !isProduction,
  62020. PROD: isProduction
  62021. },
  62022. assetsInclude(file) {
  62023. return DEFAULT_ASSETS_RE.test(file) || assetsFilter(file);
  62024. },
  62025. logger,
  62026. packageCache,
  62027. createResolver,
  62028. optimizeDeps: {
  62029. holdUntilCrawlEnd: true,
  62030. ...optimizeDeps,
  62031. esbuildOptions: {
  62032. preserveSymlinks: resolveOptions.preserveSymlinks,
  62033. ...optimizeDeps.esbuildOptions
  62034. }
  62035. },
  62036. worker: resolvedWorkerOptions,
  62037. appType: config.appType ?? "spa",
  62038. experimental: {
  62039. importGlobRestoreExtension: false,
  62040. hmrPartialAccept: false,
  62041. ...config.experimental
  62042. },
  62043. getSortedPlugins: void 0,
  62044. getSortedPluginHooks: void 0
  62045. };
  62046. resolved = {
  62047. ...config,
  62048. ...resolved
  62049. };
  62050. resolved.plugins = await resolvePlugins(
  62051. resolved,
  62052. prePlugins,
  62053. normalPlugins,
  62054. postPlugins
  62055. );
  62056. Object.assign(resolved, createPluginHookUtils(resolved.plugins));
  62057. await Promise.all(
  62058. resolved.getSortedPluginHooks("configResolved").map((hook) => hook(resolved))
  62059. );
  62060. optimizeDepsDisabledBackwardCompatibility(resolved, resolved.optimizeDeps);
  62061. optimizeDepsDisabledBackwardCompatibility(
  62062. resolved,
  62063. resolved.ssr.optimizeDeps,
  62064. "ssr."
  62065. );
  62066. debug?.(`using resolved config: %O`, {
  62067. ...resolved,
  62068. plugins: resolved.plugins.map((p) => p.name),
  62069. worker: {
  62070. ...resolved.worker,
  62071. plugins: `() => plugins`
  62072. }
  62073. });
  62074. if (config.build?.terserOptions && config.build.minify && config.build.minify !== "terser") {
  62075. logger.warn(
  62076. colors$1.yellow(
  62077. `build.terserOptions is specified but build.minify is not set to use Terser. Note Vite now defaults to use esbuild for minification. If you still prefer Terser, set build.minify to "terser".`
  62078. )
  62079. );
  62080. }
  62081. const outputOption = config.build?.rollupOptions?.output ?? [];
  62082. if (Array.isArray(outputOption)) {
  62083. const assetFileNamesList = outputOption.map(
  62084. (output) => output.assetFileNames
  62085. );
  62086. if (assetFileNamesList.length > 1) {
  62087. const firstAssetFileNames = assetFileNamesList[0];
  62088. const hasDifferentReference = assetFileNamesList.some(
  62089. (assetFileNames) => assetFileNames !== firstAssetFileNames
  62090. );
  62091. if (hasDifferentReference) {
  62092. resolved.logger.warn(
  62093. colors$1.yellow(`
  62094. assetFileNames isn't equal for every build.rollupOptions.output. A single pattern across all outputs is supported by Vite.
  62095. `)
  62096. );
  62097. }
  62098. }
  62099. }
  62100. if (
  62101. // @ts-expect-error Option removed
  62102. config.legacy?.buildSsrCjsExternalHeuristics || // @ts-expect-error Option removed
  62103. config.ssr?.format === "cjs"
  62104. ) {
  62105. resolved.logger.warn(
  62106. colors$1.yellow(`
  62107. (!) Experimental legacy.buildSsrCjsExternalHeuristics and ssr.format were be removed in Vite 5.
  62108. The only SSR Output format is ESM. Find more information at https://github.com/vitejs/vite/discussions/13816.
  62109. `)
  62110. );
  62111. }
  62112. const resolvedBuildOutDir = normalizePath$3(
  62113. path$n.resolve(resolved.root, resolved.build.outDir)
  62114. );
  62115. if (isParentDirectory(resolvedBuildOutDir, resolved.root) || resolvedBuildOutDir === resolved.root) {
  62116. resolved.logger.warn(
  62117. colors$1.yellow(`
  62118. (!) build.outDir must not be the same directory of root or a parent directory of root as this could cause Vite to overwriting source files with build outputs.
  62119. `)
  62120. );
  62121. }
  62122. return resolved;
  62123. }
  62124. function resolveBaseUrl(base = "/", isBuild, logger) {
  62125. if (base[0] === ".") {
  62126. logger.warn(
  62127. colors$1.yellow(
  62128. colors$1.bold(
  62129. `(!) invalid "base" option: "${base}". The value can only be an absolute URL, "./", or an empty string.`
  62130. )
  62131. )
  62132. );
  62133. return "/";
  62134. }
  62135. const isExternal = isExternalUrl(base);
  62136. if (!isExternal && base[0] !== "/") {
  62137. logger.warn(
  62138. colors$1.yellow(
  62139. colors$1.bold(`(!) "base" option should start with a slash.`)
  62140. )
  62141. );
  62142. }
  62143. if (!isBuild || !isExternal) {
  62144. base = new URL(base, "http://vite.dev").pathname;
  62145. if (base[0] !== "/") {
  62146. base = "/" + base;
  62147. }
  62148. }
  62149. return base;
  62150. }
  62151. function sortUserPlugins(plugins) {
  62152. const prePlugins = [];
  62153. const postPlugins = [];
  62154. const normalPlugins = [];
  62155. if (plugins) {
  62156. plugins.flat().forEach((p) => {
  62157. if (p.enforce === "pre") prePlugins.push(p);
  62158. else if (p.enforce === "post") postPlugins.push(p);
  62159. else normalPlugins.push(p);
  62160. });
  62161. }
  62162. return [prePlugins, normalPlugins, postPlugins];
  62163. }
  62164. async function loadConfigFromFile(configEnv, configFile, configRoot = process.cwd(), logLevel, customLogger) {
  62165. const start = performance$1.now();
  62166. const getTime = () => `${(performance$1.now() - start).toFixed(2)}ms`;
  62167. let resolvedPath;
  62168. if (configFile) {
  62169. resolvedPath = path$n.resolve(configFile);
  62170. } else {
  62171. for (const filename of DEFAULT_CONFIG_FILES) {
  62172. const filePath = path$n.resolve(configRoot, filename);
  62173. if (!fs__default.existsSync(filePath)) continue;
  62174. resolvedPath = filePath;
  62175. break;
  62176. }
  62177. }
  62178. if (!resolvedPath) {
  62179. debug?.("no config file found.");
  62180. return null;
  62181. }
  62182. const isESM = typeof process.versions.deno === "string" || isFilePathESM(resolvedPath);
  62183. try {
  62184. const bundled = await bundleConfigFile(resolvedPath, isESM);
  62185. const userConfig = await loadConfigFromBundledFile(
  62186. resolvedPath,
  62187. bundled.code,
  62188. isESM
  62189. );
  62190. debug?.(`bundled config file loaded in ${getTime()}`);
  62191. const config = await (typeof userConfig === "function" ? userConfig(configEnv) : userConfig);
  62192. if (!isObject$1(config)) {
  62193. throw new Error(`config must export or return an object.`);
  62194. }
  62195. return {
  62196. path: normalizePath$3(resolvedPath),
  62197. config,
  62198. dependencies: bundled.dependencies
  62199. };
  62200. } catch (e) {
  62201. createLogger(logLevel, { customLogger }).error(
  62202. colors$1.red(`failed to load config from ${resolvedPath}`),
  62203. {
  62204. error: e
  62205. }
  62206. );
  62207. throw e;
  62208. }
  62209. }
  62210. async function bundleConfigFile(fileName, isESM) {
  62211. const dirnameVarName = "__vite_injected_original_dirname";
  62212. const filenameVarName = "__vite_injected_original_filename";
  62213. const importMetaUrlVarName = "__vite_injected_original_import_meta_url";
  62214. const result = await build$3({
  62215. absWorkingDir: process.cwd(),
  62216. entryPoints: [fileName],
  62217. write: false,
  62218. target: [`node${process.versions.node}`],
  62219. platform: "node",
  62220. bundle: true,
  62221. format: isESM ? "esm" : "cjs",
  62222. mainFields: ["main"],
  62223. sourcemap: "inline",
  62224. metafile: true,
  62225. define: {
  62226. __dirname: dirnameVarName,
  62227. __filename: filenameVarName,
  62228. "import.meta.url": importMetaUrlVarName,
  62229. "import.meta.dirname": dirnameVarName,
  62230. "import.meta.filename": filenameVarName
  62231. },
  62232. plugins: [
  62233. {
  62234. name: "externalize-deps",
  62235. setup(build2) {
  62236. const packageCache = /* @__PURE__ */ new Map();
  62237. const resolveByViteResolver = (id, importer, isRequire) => {
  62238. return tryNodeResolve(
  62239. id,
  62240. importer,
  62241. {
  62242. root: path$n.dirname(fileName),
  62243. isBuild: true,
  62244. isProduction: true,
  62245. preferRelative: false,
  62246. tryIndex: true,
  62247. mainFields: [],
  62248. conditions: [],
  62249. overrideConditions: ["node"],
  62250. dedupe: [],
  62251. extensions: DEFAULT_EXTENSIONS,
  62252. preserveSymlinks: false,
  62253. packageCache,
  62254. isRequire
  62255. },
  62256. false
  62257. )?.id;
  62258. };
  62259. build2.onResolve(
  62260. { filter: /^[^.].*/ },
  62261. async ({ path: id, importer, kind }) => {
  62262. if (kind === "entry-point" || path$n.isAbsolute(id) || isNodeBuiltin(id)) {
  62263. return;
  62264. }
  62265. if (isBuiltin(id)) {
  62266. return { external: true };
  62267. }
  62268. const isImport = isESM || kind === "dynamic-import";
  62269. let idFsPath;
  62270. try {
  62271. idFsPath = resolveByViteResolver(id, importer, !isImport);
  62272. } catch (e) {
  62273. if (!isImport) {
  62274. let canResolveWithImport = false;
  62275. try {
  62276. canResolveWithImport = !!resolveByViteResolver(
  62277. id,
  62278. importer,
  62279. false
  62280. );
  62281. } catch {
  62282. }
  62283. if (canResolveWithImport) {
  62284. throw new Error(
  62285. `Failed to resolve ${JSON.stringify(
  62286. id
  62287. )}. This package is ESM only but it was tried to load by \`require\`. See https://vite.dev/guide/troubleshooting.html#this-package-is-esm-only for more details.`
  62288. );
  62289. }
  62290. }
  62291. throw e;
  62292. }
  62293. if (idFsPath && isImport) {
  62294. idFsPath = pathToFileURL(idFsPath).href;
  62295. }
  62296. if (idFsPath && !isImport && isFilePathESM(idFsPath, packageCache)) {
  62297. throw new Error(
  62298. `${JSON.stringify(
  62299. id
  62300. )} resolved to an ESM file. ESM file cannot be loaded by \`require\`. See https://vite.dev/guide/troubleshooting.html#this-package-is-esm-only for more details.`
  62301. );
  62302. }
  62303. return {
  62304. path: idFsPath,
  62305. external: true
  62306. };
  62307. }
  62308. );
  62309. }
  62310. },
  62311. {
  62312. name: "inject-file-scope-variables",
  62313. setup(build2) {
  62314. build2.onLoad({ filter: /\.[cm]?[jt]s$/ }, async (args) => {
  62315. const contents = await fsp.readFile(args.path, "utf-8");
  62316. const injectValues = `const ${dirnameVarName} = ${JSON.stringify(
  62317. path$n.dirname(args.path)
  62318. )};const ${filenameVarName} = ${JSON.stringify(args.path)};const ${importMetaUrlVarName} = ${JSON.stringify(
  62319. pathToFileURL(args.path).href
  62320. )};`;
  62321. return {
  62322. loader: args.path.endsWith("ts") ? "ts" : "js",
  62323. contents: injectValues + contents
  62324. };
  62325. });
  62326. }
  62327. }
  62328. ]
  62329. });
  62330. const { text } = result.outputFiles[0];
  62331. return {
  62332. code: text,
  62333. dependencies: result.metafile ? Object.keys(result.metafile.inputs) : []
  62334. };
  62335. }
  62336. const _require = createRequire$1(import.meta.url);
  62337. async function loadConfigFromBundledFile(fileName, bundledCode, isESM) {
  62338. if (isESM) {
  62339. const fileBase = `${fileName}.timestamp-${Date.now()}-${Math.random().toString(16).slice(2)}`;
  62340. const fileNameTmp = `${fileBase}.mjs`;
  62341. const fileUrl = `${pathToFileURL(fileBase)}.mjs`;
  62342. await fsp.writeFile(fileNameTmp, bundledCode);
  62343. try {
  62344. return (await import(fileUrl)).default;
  62345. } finally {
  62346. fs__default.unlink(fileNameTmp, () => {
  62347. });
  62348. }
  62349. } else {
  62350. const extension = path$n.extname(fileName);
  62351. const realFileName = await promisifiedRealpath(fileName);
  62352. const loaderExt = extension in _require.extensions ? extension : ".js";
  62353. const defaultLoader = _require.extensions[loaderExt];
  62354. _require.extensions[loaderExt] = (module, filename) => {
  62355. if (filename === realFileName) {
  62356. module._compile(bundledCode, filename);
  62357. } else {
  62358. defaultLoader(module, filename);
  62359. }
  62360. };
  62361. delete _require.cache[_require.resolve(fileName)];
  62362. const raw = _require(fileName);
  62363. _require.extensions[loaderExt] = defaultLoader;
  62364. return raw.__esModule ? raw.default : raw;
  62365. }
  62366. }
  62367. async function runConfigHook(config, plugins, configEnv) {
  62368. let conf = config;
  62369. for (const p of getSortedPluginsByHook("config", plugins)) {
  62370. const hook = p.config;
  62371. const handler = getHookHandler(hook);
  62372. if (handler) {
  62373. const res = await handler(conf, configEnv);
  62374. if (res) {
  62375. conf = mergeConfig(conf, res);
  62376. }
  62377. }
  62378. }
  62379. return conf;
  62380. }
  62381. function getDepOptimizationConfig(config, ssr) {
  62382. return ssr ? config.ssr.optimizeDeps : config.optimizeDeps;
  62383. }
  62384. function isDepsOptimizerEnabled(config, ssr) {
  62385. const optimizeDeps = getDepOptimizationConfig(config, ssr);
  62386. return !(optimizeDeps.noDiscovery && !optimizeDeps.include?.length);
  62387. }
  62388. function optimizeDepsDisabledBackwardCompatibility(resolved, optimizeDeps, optimizeDepsPath = "") {
  62389. const optimizeDepsDisabled = optimizeDeps.disabled;
  62390. if (optimizeDepsDisabled !== void 0) {
  62391. if (optimizeDepsDisabled === true || optimizeDepsDisabled === "dev") {
  62392. const commonjsOptionsInclude = resolved.build?.commonjsOptions?.include;
  62393. const commonjsPluginDisabled = Array.isArray(commonjsOptionsInclude) && commonjsOptionsInclude.length === 0;
  62394. optimizeDeps.noDiscovery = true;
  62395. optimizeDeps.include = void 0;
  62396. if (commonjsPluginDisabled) {
  62397. resolved.build.commonjsOptions.include = void 0;
  62398. }
  62399. resolved.logger.warn(
  62400. colors$1.yellow(`(!) Experimental ${optimizeDepsPath}optimizeDeps.disabled and deps pre-bundling during build were removed in Vite 5.1.
  62401. To disable the deps optimizer, set ${optimizeDepsPath}optimizeDeps.noDiscovery to true and ${optimizeDepsPath}optimizeDeps.include as undefined or empty.
  62402. Please remove ${optimizeDepsPath}optimizeDeps.disabled from your config.
  62403. ${commonjsPluginDisabled ? "Empty config.build.commonjsOptions.include will be ignored to support CJS during build. This config should also be removed." : ""}
  62404. `)
  62405. );
  62406. } else if (optimizeDepsDisabled === false || optimizeDepsDisabled === "build") {
  62407. resolved.logger.warn(
  62408. colors$1.yellow(`(!) Experimental ${optimizeDepsPath}optimizeDeps.disabled and deps pre-bundling during build were removed in Vite 5.1.
  62409. Setting it to ${optimizeDepsDisabled} now has no effect.
  62410. Please remove ${optimizeDepsPath}optimizeDeps.disabled from your config.
  62411. `)
  62412. );
  62413. }
  62414. }
  62415. }
  62416. export { colors$1 as A, getDefaultExportFromCjs as B, commonjsGlobal as C, index$1 as D, index as E, build$1 as F, preview$1 as G, arraify as a, build as b, createServer as c, defineConfig as d, preprocessCSS as e, formatPostcssSourceMap as f, buildErrorMessage as g, fetchModule as h, isInNodeModules$1 as i, mergeAlias as j, createFilter as k, loadConfigFromFile as l, mergeConfig as m, normalizePath$3 as n, optimizeDeps as o, preview as p, rollupVersion as q, resolveConfig as r, sortUserPlugins as s, transformWithEsbuild as t, send as u, createLogger as v, searchForWorkspaceRoot as w, isFileServingAllowed as x, loadEnv as y, resolveEnvPrefix as z };