dep-BK3b2jBa.js 2.0 MB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783257842578525786257872578825789257902579125792257932579425795257962579725798257992580025801258022580325804258052580625807258082580925810258112581225813258142581525816258172581825819258202582125822258232582425825258262582725828258292583025831258322583325834258352583625837258382583925840258412584225843258442584525846258472584825849258502585125852258532585425855258562585725858258592586025861258622586325864258652586625867258682586925870258712587225873258742587525876258772587825879258802588125882258832588425885258862588725888258892589025891258922589325894258952589625897258982589925900259012590225903259042590525906259072590825909259102591125912259132591425915259162591725918259192592025921259222592325924259252592625927259282592925930259312593225933259342593525936259372593825939259402594125942259432594425945259462594725948259492595025951259522595325954259552595625957259582595925960259612596225963259642596525966259672596825969259702597125972259732597425975259762597725978259792598025981259822598325984259852598625987259882598925990259912599225993259942599525996259972599825999260002600126002260032600426005260062600726008260092601026011260122601326014260152601626017260182601926020260212602226023260242602526026260272602826029260302603126032260332603426035260362603726038260392604026041260422604326044260452604626047260482604926050260512605226053260542605526056260572605826059260602606126062260632606426065260662606726068260692607026071260722607326074260752607626077260782607926080260812608226083260842608526086260872608826089260902609126092260932609426095260962609726098260992610026101261022610326104261052610626107261082610926110261112611226113261142611526116261172611826119261202612126122261232612426125261262612726128261292613026131261322613326134261352613626137261382613926140261412614226143261442614526146261472614826149261502615126152261532615426155261562615726158261592616026161261622616326164261652616626167261682616926170261712617226173261742617526176261772617826179261802618126182261832618426185261862618726188261892619026191261922619326194261952619626197261982619926200262012620226203262042620526206262072620826209262102621126212262132621426215262162621726218262192622026221262222622326224262252622626227262282622926230262312623226233262342623526236262372623826239262402624126242262432624426245262462624726248262492625026251262522625326254262552625626257262582625926260262612626226263262642626526266262672626826269262702627126272262732627426275262762627726278262792628026281262822628326284262852628626287262882628926290262912629226293262942629526296262972629826299263002630126302263032630426305263062630726308263092631026311263122631326314263152631626317263182631926320263212632226323263242632526326263272632826329263302633126332263332633426335263362633726338263392634026341263422634326344263452634626347263482634926350263512635226353263542635526356263572635826359263602636126362263632636426365263662636726368263692637026371263722637326374263752637626377263782637926380263812638226383263842638526386263872638826389263902639126392263932639426395263962639726398263992640026401264022640326404264052640626407264082640926410264112641226413264142641526416264172641826419264202642126422264232642426425264262642726428264292643026431264322643326434264352643626437264382643926440264412644226443264442644526446264472644826449264502645126452264532645426455264562645726458264592646026461264622646326464264652646626467264682646926470264712647226473264742647526476264772647826479264802648126482264832648426485264862648726488264892649026491264922649326494264952649626497264982649926500265012650226503265042650526506265072650826509265102651126512265132651426515265162651726518265192652026521265222652326524265252652626527265282652926530265312653226533265342653526536265372653826539265402654126542265432654426545265462654726548265492655026551265522655326554265552655626557265582655926560265612656226563265642656526566265672656826569265702657126572265732657426575265762657726578265792658026581265822658326584265852658626587265882658926590265912659226593265942659526596265972659826599266002660126602266032660426605266062660726608266092661026611266122661326614266152661626617266182661926620266212662226623266242662526626266272662826629266302663126632266332663426635266362663726638266392664026641266422664326644266452664626647266482664926650266512665226653266542665526656266572665826659266602666126662266632666426665266662666726668266692667026671266722667326674266752667626677266782667926680266812668226683266842668526686266872668826689266902669126692266932669426695266962669726698266992670026701267022670326704267052670626707267082670926710267112671226713267142671526716267172671826719267202672126722267232672426725267262672726728267292673026731267322673326734267352673626737267382673926740267412674226743267442674526746267472674826749267502675126752267532675426755267562675726758267592676026761267622676326764267652676626767267682676926770267712677226773267742677526776267772677826779267802678126782267832678426785267862678726788267892679026791267922679326794267952679626797267982679926800268012680226803268042680526806268072680826809268102681126812268132681426815268162681726818268192682026821268222682326824268252682626827268282682926830268312683226833268342683526836268372683826839268402684126842268432684426845268462684726848268492685026851268522685326854268552685626857268582685926860268612686226863268642686526866268672686826869268702687126872268732687426875268762687726878268792688026881268822688326884268852688626887268882688926890268912689226893268942689526896268972689826899269002690126902269032690426905269062690726908269092691026911269122691326914269152691626917269182691926920269212692226923269242692526926269272692826929269302693126932269332693426935269362693726938269392694026941269422694326944269452694626947269482694926950269512695226953269542695526956269572695826959269602696126962269632696426965269662696726968269692697026971269722697326974269752697626977269782697926980269812698226983269842698526986269872698826989269902699126992269932699426995269962699726998269992700027001270022700327004270052700627007270082700927010270112701227013270142701527016270172701827019270202702127022270232702427025270262702727028270292703027031270322703327034270352703627037270382703927040270412704227043270442704527046270472704827049270502705127052270532705427055270562705727058270592706027061270622706327064270652706627067270682706927070270712707227073270742707527076270772707827079270802708127082270832708427085270862708727088270892709027091270922709327094270952709627097270982709927100271012710227103271042710527106271072710827109271102711127112271132711427115271162711727118271192712027121271222712327124271252712627127271282712927130271312713227133271342713527136271372713827139271402714127142271432714427145271462714727148271492715027151271522715327154271552715627157271582715927160271612716227163271642716527166271672716827169271702717127172271732717427175271762717727178271792718027181271822718327184271852718627187271882718927190271912719227193271942719527196271972719827199272002720127202272032720427205272062720727208272092721027211272122721327214272152721627217272182721927220272212722227223272242722527226272272722827229272302723127232272332723427235272362723727238272392724027241272422724327244272452724627247272482724927250272512725227253272542725527256272572725827259272602726127262272632726427265272662726727268272692727027271272722727327274272752727627277272782727927280272812728227283272842728527286272872728827289272902729127292272932729427295272962729727298272992730027301273022730327304273052730627307273082730927310273112731227313273142731527316273172731827319273202732127322273232732427325273262732727328273292733027331273322733327334273352733627337273382733927340273412734227343273442734527346273472734827349273502735127352273532735427355273562735727358273592736027361273622736327364273652736627367273682736927370273712737227373273742737527376273772737827379273802738127382273832738427385273862738727388273892739027391273922739327394273952739627397273982739927400274012740227403274042740527406274072740827409274102741127412274132741427415274162741727418274192742027421274222742327424274252742627427274282742927430274312743227433274342743527436274372743827439274402744127442274432744427445274462744727448274492745027451274522745327454274552745627457274582745927460274612746227463274642746527466274672746827469274702747127472274732747427475274762747727478274792748027481274822748327484274852748627487274882748927490274912749227493274942749527496274972749827499275002750127502275032750427505275062750727508275092751027511275122751327514275152751627517275182751927520275212752227523275242752527526275272752827529275302753127532275332753427535275362753727538275392754027541275422754327544275452754627547275482754927550275512755227553275542755527556275572755827559275602756127562275632756427565275662756727568275692757027571275722757327574275752757627577275782757927580275812758227583275842758527586275872758827589275902759127592275932759427595275962759727598275992760027601276022760327604276052760627607276082760927610276112761227613276142761527616276172761827619276202762127622276232762427625276262762727628276292763027631276322763327634276352763627637276382763927640276412764227643276442764527646276472764827649276502765127652276532765427655276562765727658276592766027661276622766327664276652766627667276682766927670276712767227673276742767527676276772767827679276802768127682276832768427685276862768727688276892769027691276922769327694276952769627697276982769927700277012770227703277042770527706277072770827709277102771127712277132771427715277162771727718277192772027721277222772327724277252772627727277282772927730277312773227733277342773527736277372773827739277402774127742277432774427745277462774727748277492775027751277522775327754277552775627757277582775927760277612776227763277642776527766277672776827769277702777127772277732777427775277762777727778277792778027781277822778327784277852778627787277882778927790277912779227793277942779527796277972779827799278002780127802278032780427805278062780727808278092781027811278122781327814278152781627817278182781927820278212782227823278242782527826278272782827829278302783127832278332783427835278362783727838278392784027841278422784327844278452784627847278482784927850278512785227853278542785527856278572785827859278602786127862278632786427865278662786727868278692787027871278722787327874278752787627877278782787927880278812788227883278842788527886278872788827889278902789127892278932789427895278962789727898278992790027901279022790327904279052790627907279082790927910279112791227913279142791527916279172791827919279202792127922279232792427925279262792727928279292793027931279322793327934279352793627937279382793927940279412794227943279442794527946279472794827949279502795127952279532795427955279562795727958279592796027961279622796327964279652796627967279682796927970279712797227973279742797527976279772797827979279802798127982279832798427985279862798727988279892799027991279922799327994279952799627997279982799928000280012800228003280042800528006280072800828009280102801128012280132801428015280162801728018280192802028021280222802328024280252802628027280282802928030280312803228033280342803528036280372803828039280402804128042280432804428045280462804728048280492805028051280522805328054280552805628057280582805928060280612806228063280642806528066280672806828069280702807128072280732807428075280762807728078280792808028081280822808328084280852808628087280882808928090280912809228093280942809528096280972809828099281002810128102281032810428105281062810728108281092811028111281122811328114281152811628117281182811928120281212812228123281242812528126281272812828129281302813128132281332813428135281362813728138281392814028141281422814328144281452814628147281482814928150281512815228153281542815528156281572815828159281602816128162281632816428165281662816728168281692817028171281722817328174281752817628177281782817928180281812818228183281842818528186281872818828189281902819128192281932819428195281962819728198281992820028201282022820328204282052820628207282082820928210282112821228213282142821528216282172821828219282202822128222282232822428225282262822728228282292823028231282322823328234282352823628237282382823928240282412824228243282442824528246282472824828249282502825128252282532825428255282562825728258282592826028261282622826328264282652826628267282682826928270282712827228273282742827528276282772827828279282802828128282282832828428285282862828728288282892829028291282922829328294282952829628297282982829928300283012830228303283042830528306283072830828309283102831128312283132831428315283162831728318283192832028321283222832328324283252832628327283282832928330283312833228333283342833528336283372833828339283402834128342283432834428345283462834728348283492835028351283522835328354283552835628357283582835928360283612836228363283642836528366283672836828369283702837128372283732837428375283762837728378283792838028381283822838328384283852838628387283882838928390283912839228393283942839528396283972839828399284002840128402284032840428405284062840728408284092841028411284122841328414284152841628417284182841928420284212842228423284242842528426284272842828429284302843128432284332843428435284362843728438284392844028441284422844328444284452844628447284482844928450284512845228453284542845528456284572845828459284602846128462284632846428465284662846728468284692847028471284722847328474284752847628477284782847928480284812848228483284842848528486284872848828489284902849128492284932849428495284962849728498284992850028501285022850328504285052850628507285082850928510285112851228513285142851528516285172851828519285202852128522285232852428525285262852728528285292853028531285322853328534285352853628537285382853928540285412854228543285442854528546285472854828549285502855128552285532855428555285562855728558285592856028561285622856328564285652856628567285682856928570285712857228573285742857528576285772857828579285802858128582285832858428585285862858728588285892859028591285922859328594285952859628597285982859928600286012860228603286042860528606286072860828609286102861128612286132861428615286162861728618286192862028621286222862328624286252862628627286282862928630286312863228633286342863528636286372863828639286402864128642286432864428645286462864728648286492865028651286522865328654286552865628657286582865928660286612866228663286642866528666286672866828669286702867128672286732867428675286762867728678286792868028681286822868328684286852868628687286882868928690286912869228693286942869528696286972869828699287002870128702287032870428705287062870728708287092871028711287122871328714287152871628717287182871928720287212872228723287242872528726287272872828729287302873128732287332873428735287362873728738287392874028741287422874328744287452874628747287482874928750287512875228753287542875528756287572875828759287602876128762287632876428765287662876728768287692877028771287722877328774287752877628777287782877928780287812878228783287842878528786287872878828789287902879128792287932879428795287962879728798287992880028801288022880328804288052880628807288082880928810288112881228813288142881528816288172881828819288202882128822288232882428825288262882728828288292883028831288322883328834288352883628837288382883928840288412884228843288442884528846288472884828849288502885128852288532885428855288562885728858288592886028861288622886328864288652886628867288682886928870288712887228873288742887528876288772887828879288802888128882288832888428885288862888728888288892889028891288922889328894288952889628897288982889928900289012890228903289042890528906289072890828909289102891128912289132891428915289162891728918289192892028921289222892328924289252892628927289282892928930289312893228933289342893528936289372893828939289402894128942289432894428945289462894728948289492895028951289522895328954289552895628957289582895928960289612896228963289642896528966289672896828969289702897128972289732897428975289762897728978289792898028981289822898328984289852898628987289882898928990289912899228993289942899528996289972899828999290002900129002290032900429005290062900729008290092901029011290122901329014290152901629017290182901929020290212902229023290242902529026290272902829029290302903129032290332903429035290362903729038290392904029041290422904329044290452904629047290482904929050290512905229053290542905529056290572905829059290602906129062290632906429065290662906729068290692907029071290722907329074290752907629077290782907929080290812908229083290842908529086290872908829089290902909129092290932909429095290962909729098290992910029101291022910329104291052910629107291082910929110291112911229113291142911529116291172911829119291202912129122291232912429125291262912729128291292913029131291322913329134291352913629137291382913929140291412914229143291442914529146291472914829149291502915129152291532915429155291562915729158291592916029161291622916329164291652916629167291682916929170291712917229173291742917529176291772917829179291802918129182291832918429185291862918729188291892919029191291922919329194291952919629197291982919929200292012920229203292042920529206292072920829209292102921129212292132921429215292162921729218292192922029221292222922329224292252922629227292282922929230292312923229233292342923529236292372923829239292402924129242292432924429245292462924729248292492925029251292522925329254292552925629257292582925929260292612926229263292642926529266292672926829269292702927129272292732927429275292762927729278292792928029281292822928329284292852928629287292882928929290292912929229293292942929529296292972929829299293002930129302293032930429305293062930729308293092931029311293122931329314293152931629317293182931929320293212932229323293242932529326293272932829329293302933129332293332933429335293362933729338293392934029341293422934329344293452934629347293482934929350293512935229353293542935529356293572935829359293602936129362293632936429365293662936729368293692937029371293722937329374293752937629377293782937929380293812938229383293842938529386293872938829389293902939129392293932939429395293962939729398293992940029401294022940329404294052940629407294082940929410294112941229413294142941529416294172941829419294202942129422294232942429425294262942729428294292943029431294322943329434294352943629437294382943929440294412944229443294442944529446294472944829449294502945129452294532945429455294562945729458294592946029461294622946329464294652946629467294682946929470294712947229473294742947529476294772947829479294802948129482294832948429485294862948729488294892949029491294922949329494294952949629497294982949929500295012950229503295042950529506295072950829509295102951129512295132951429515295162951729518295192952029521295222952329524295252952629527295282952929530295312953229533295342953529536295372953829539295402954129542295432954429545295462954729548295492955029551295522955329554295552955629557295582955929560295612956229563295642956529566295672956829569295702957129572295732957429575295762957729578295792958029581295822958329584295852958629587295882958929590295912959229593295942959529596295972959829599296002960129602296032960429605296062960729608296092961029611296122961329614296152961629617296182961929620296212962229623296242962529626296272962829629296302963129632296332963429635296362963729638296392964029641296422964329644296452964629647296482964929650296512965229653296542965529656296572965829659296602966129662296632966429665296662966729668296692967029671296722967329674296752967629677296782967929680296812968229683296842968529686296872968829689296902969129692296932969429695296962969729698296992970029701297022970329704297052970629707297082970929710297112971229713297142971529716297172971829719297202972129722297232972429725297262972729728297292973029731297322973329734297352973629737297382973929740297412974229743297442974529746297472974829749297502975129752297532975429755297562975729758297592976029761297622976329764297652976629767297682976929770297712977229773297742977529776297772977829779297802978129782297832978429785297862978729788297892979029791297922979329794297952979629797297982979929800298012980229803298042980529806298072980829809298102981129812298132981429815298162981729818298192982029821298222982329824298252982629827298282982929830298312983229833298342983529836298372983829839298402984129842298432984429845298462984729848298492985029851298522985329854298552985629857298582985929860298612986229863298642986529866298672986829869298702987129872298732987429875298762987729878298792988029881298822988329884298852988629887298882988929890298912989229893298942989529896298972989829899299002990129902299032990429905299062990729908299092991029911299122991329914299152991629917299182991929920299212992229923299242992529926299272992829929299302993129932299332993429935299362993729938299392994029941299422994329944299452994629947299482994929950299512995229953299542995529956299572995829959299602996129962299632996429965299662996729968299692997029971299722997329974299752997629977299782997929980299812998229983299842998529986299872998829989299902999129992299932999429995299962999729998299993000030001300023000330004300053000630007300083000930010300113001230013300143001530016300173001830019300203002130022300233002430025300263002730028300293003030031300323003330034300353003630037300383003930040300413004230043300443004530046300473004830049300503005130052300533005430055300563005730058300593006030061300623006330064300653006630067300683006930070300713007230073300743007530076300773007830079300803008130082300833008430085300863008730088300893009030091300923009330094300953009630097300983009930100301013010230103301043010530106301073010830109301103011130112301133011430115301163011730118301193012030121301223012330124301253012630127301283012930130301313013230133301343013530136301373013830139301403014130142301433014430145301463014730148301493015030151301523015330154301553015630157301583015930160301613016230163301643016530166301673016830169301703017130172301733017430175301763017730178301793018030181301823018330184301853018630187301883018930190301913019230193301943019530196301973019830199302003020130202302033020430205302063020730208302093021030211302123021330214302153021630217302183021930220302213022230223302243022530226302273022830229302303023130232302333023430235302363023730238302393024030241302423024330244302453024630247302483024930250302513025230253302543025530256302573025830259302603026130262302633026430265302663026730268302693027030271302723027330274302753027630277302783027930280302813028230283302843028530286302873028830289302903029130292302933029430295302963029730298302993030030301303023030330304303053030630307303083030930310303113031230313303143031530316303173031830319303203032130322303233032430325303263032730328303293033030331303323033330334303353033630337303383033930340303413034230343303443034530346303473034830349303503035130352303533035430355303563035730358303593036030361303623036330364303653036630367303683036930370303713037230373303743037530376303773037830379303803038130382303833038430385303863038730388303893039030391303923039330394303953039630397303983039930400304013040230403304043040530406304073040830409304103041130412304133041430415304163041730418304193042030421304223042330424304253042630427304283042930430304313043230433304343043530436304373043830439304403044130442304433044430445304463044730448304493045030451304523045330454304553045630457304583045930460304613046230463304643046530466304673046830469304703047130472304733047430475304763047730478304793048030481304823048330484304853048630487304883048930490304913049230493304943049530496304973049830499305003050130502305033050430505305063050730508305093051030511305123051330514305153051630517305183051930520305213052230523305243052530526305273052830529305303053130532305333053430535305363053730538305393054030541305423054330544305453054630547305483054930550305513055230553305543055530556305573055830559305603056130562305633056430565305663056730568305693057030571305723057330574305753057630577305783057930580305813058230583305843058530586305873058830589305903059130592305933059430595305963059730598305993060030601306023060330604306053060630607306083060930610306113061230613306143061530616306173061830619306203062130622306233062430625306263062730628306293063030631306323063330634306353063630637306383063930640306413064230643306443064530646306473064830649306503065130652306533065430655306563065730658306593066030661306623066330664306653066630667306683066930670306713067230673306743067530676306773067830679306803068130682306833068430685306863068730688306893069030691306923069330694306953069630697306983069930700307013070230703307043070530706307073070830709307103071130712307133071430715307163071730718307193072030721307223072330724307253072630727307283072930730307313073230733307343073530736307373073830739307403074130742307433074430745307463074730748307493075030751307523075330754307553075630757307583075930760307613076230763307643076530766307673076830769307703077130772307733077430775307763077730778307793078030781307823078330784307853078630787307883078930790307913079230793307943079530796307973079830799308003080130802308033080430805308063080730808308093081030811308123081330814308153081630817308183081930820308213082230823308243082530826308273082830829308303083130832308333083430835308363083730838308393084030841308423084330844308453084630847308483084930850308513085230853308543085530856308573085830859308603086130862308633086430865308663086730868308693087030871308723087330874308753087630877308783087930880308813088230883308843088530886308873088830889308903089130892308933089430895308963089730898308993090030901309023090330904309053090630907309083090930910309113091230913309143091530916309173091830919309203092130922309233092430925309263092730928309293093030931309323093330934309353093630937309383093930940309413094230943309443094530946309473094830949309503095130952309533095430955309563095730958309593096030961309623096330964309653096630967309683096930970309713097230973309743097530976309773097830979309803098130982309833098430985309863098730988309893099030991309923099330994309953099630997309983099931000310013100231003310043100531006310073100831009310103101131012310133101431015310163101731018310193102031021310223102331024310253102631027310283102931030310313103231033310343103531036310373103831039310403104131042310433104431045310463104731048310493105031051310523105331054310553105631057310583105931060310613106231063310643106531066310673106831069310703107131072310733107431075310763107731078310793108031081310823108331084310853108631087310883108931090310913109231093310943109531096310973109831099311003110131102311033110431105311063110731108311093111031111311123111331114311153111631117311183111931120311213112231123311243112531126311273112831129311303113131132311333113431135311363113731138311393114031141311423114331144311453114631147311483114931150311513115231153311543115531156311573115831159311603116131162311633116431165311663116731168311693117031171311723117331174311753117631177311783117931180311813118231183311843118531186311873118831189311903119131192311933119431195311963119731198311993120031201312023120331204312053120631207312083120931210312113121231213312143121531216312173121831219312203122131222312233122431225312263122731228312293123031231312323123331234312353123631237312383123931240312413124231243312443124531246312473124831249312503125131252312533125431255312563125731258312593126031261312623126331264312653126631267312683126931270312713127231273312743127531276312773127831279312803128131282312833128431285312863128731288312893129031291312923129331294312953129631297312983129931300313013130231303313043130531306313073130831309313103131131312313133131431315313163131731318313193132031321313223132331324313253132631327313283132931330313313133231333313343133531336313373133831339313403134131342313433134431345313463134731348313493135031351313523135331354313553135631357313583135931360313613136231363313643136531366313673136831369313703137131372313733137431375313763137731378313793138031381313823138331384313853138631387313883138931390313913139231393313943139531396313973139831399314003140131402314033140431405314063140731408314093141031411314123141331414314153141631417314183141931420314213142231423314243142531426314273142831429314303143131432314333143431435314363143731438314393144031441314423144331444314453144631447314483144931450314513145231453314543145531456314573145831459314603146131462314633146431465314663146731468314693147031471314723147331474314753147631477314783147931480314813148231483314843148531486314873148831489314903149131492314933149431495314963149731498314993150031501315023150331504315053150631507315083150931510315113151231513315143151531516315173151831519315203152131522315233152431525315263152731528315293153031531315323153331534315353153631537315383153931540315413154231543315443154531546315473154831549315503155131552315533155431555315563155731558315593156031561315623156331564315653156631567315683156931570315713157231573315743157531576315773157831579315803158131582315833158431585315863158731588315893159031591315923159331594315953159631597315983159931600316013160231603316043160531606316073160831609316103161131612316133161431615316163161731618316193162031621316223162331624316253162631627316283162931630316313163231633316343163531636316373163831639316403164131642316433164431645316463164731648316493165031651316523165331654316553165631657316583165931660316613166231663316643166531666316673166831669316703167131672316733167431675316763167731678316793168031681316823168331684316853168631687316883168931690316913169231693316943169531696316973169831699317003170131702317033170431705317063170731708317093171031711317123171331714317153171631717317183171931720317213172231723317243172531726317273172831729317303173131732317333173431735317363173731738317393174031741317423174331744317453174631747317483174931750317513175231753317543175531756317573175831759317603176131762317633176431765317663176731768317693177031771317723177331774317753177631777317783177931780317813178231783317843178531786317873178831789317903179131792317933179431795317963179731798317993180031801318023180331804318053180631807318083180931810318113181231813318143181531816318173181831819318203182131822318233182431825318263182731828318293183031831318323183331834318353183631837318383183931840318413184231843318443184531846318473184831849318503185131852318533185431855318563185731858318593186031861318623186331864318653186631867318683186931870318713187231873318743187531876318773187831879318803188131882318833188431885318863188731888318893189031891318923189331894318953189631897318983189931900319013190231903319043190531906319073190831909319103191131912319133191431915319163191731918319193192031921319223192331924319253192631927319283192931930319313193231933319343193531936319373193831939319403194131942319433194431945319463194731948319493195031951319523195331954319553195631957319583195931960319613196231963319643196531966319673196831969319703197131972319733197431975319763197731978319793198031981319823198331984319853198631987319883198931990319913199231993319943199531996319973199831999320003200132002320033200432005320063200732008320093201032011320123201332014320153201632017320183201932020320213202232023320243202532026320273202832029320303203132032320333203432035320363203732038320393204032041320423204332044320453204632047320483204932050320513205232053320543205532056320573205832059320603206132062320633206432065320663206732068320693207032071320723207332074320753207632077320783207932080320813208232083320843208532086320873208832089320903209132092320933209432095320963209732098320993210032101321023210332104321053210632107321083210932110321113211232113321143211532116321173211832119321203212132122321233212432125321263212732128321293213032131321323213332134321353213632137321383213932140321413214232143321443214532146321473214832149321503215132152321533215432155321563215732158321593216032161321623216332164321653216632167321683216932170321713217232173321743217532176321773217832179321803218132182321833218432185321863218732188321893219032191321923219332194321953219632197321983219932200322013220232203322043220532206322073220832209322103221132212322133221432215322163221732218322193222032221322223222332224322253222632227322283222932230322313223232233322343223532236322373223832239322403224132242322433224432245322463224732248322493225032251322523225332254322553225632257322583225932260322613226232263322643226532266322673226832269322703227132272322733227432275322763227732278322793228032281322823228332284322853228632287322883228932290322913229232293322943229532296322973229832299323003230132302323033230432305323063230732308323093231032311323123231332314323153231632317323183231932320323213232232323323243232532326323273232832329323303233132332323333233432335323363233732338323393234032341323423234332344323453234632347323483234932350323513235232353323543235532356323573235832359323603236132362323633236432365323663236732368323693237032371323723237332374323753237632377323783237932380323813238232383323843238532386323873238832389323903239132392323933239432395323963239732398323993240032401324023240332404324053240632407324083240932410324113241232413324143241532416324173241832419324203242132422324233242432425324263242732428324293243032431324323243332434324353243632437324383243932440324413244232443324443244532446324473244832449324503245132452324533245432455324563245732458324593246032461324623246332464324653246632467324683246932470324713247232473324743247532476324773247832479324803248132482324833248432485324863248732488324893249032491324923249332494324953249632497324983249932500325013250232503325043250532506325073250832509325103251132512325133251432515325163251732518325193252032521325223252332524325253252632527325283252932530325313253232533325343253532536325373253832539325403254132542325433254432545325463254732548325493255032551325523255332554325553255632557325583255932560325613256232563325643256532566325673256832569325703257132572325733257432575325763257732578325793258032581325823258332584325853258632587325883258932590325913259232593325943259532596325973259832599326003260132602326033260432605326063260732608326093261032611326123261332614326153261632617326183261932620326213262232623326243262532626326273262832629326303263132632326333263432635326363263732638326393264032641326423264332644326453264632647326483264932650326513265232653326543265532656326573265832659326603266132662326633266432665326663266732668326693267032671326723267332674326753267632677326783267932680326813268232683326843268532686326873268832689326903269132692326933269432695326963269732698326993270032701327023270332704327053270632707327083270932710327113271232713327143271532716327173271832719327203272132722327233272432725327263272732728327293273032731327323273332734327353273632737327383273932740327413274232743327443274532746327473274832749327503275132752327533275432755327563275732758327593276032761327623276332764327653276632767327683276932770327713277232773327743277532776327773277832779327803278132782327833278432785327863278732788327893279032791327923279332794327953279632797327983279932800328013280232803328043280532806328073280832809328103281132812328133281432815328163281732818328193282032821328223282332824328253282632827328283282932830328313283232833328343283532836328373283832839328403284132842328433284432845328463284732848328493285032851328523285332854328553285632857328583285932860328613286232863328643286532866328673286832869328703287132872328733287432875328763287732878328793288032881328823288332884328853288632887328883288932890328913289232893328943289532896328973289832899329003290132902329033290432905329063290732908329093291032911329123291332914329153291632917329183291932920329213292232923329243292532926329273292832929329303293132932329333293432935329363293732938329393294032941329423294332944329453294632947329483294932950329513295232953329543295532956329573295832959329603296132962329633296432965329663296732968329693297032971329723297332974329753297632977329783297932980329813298232983329843298532986329873298832989329903299132992329933299432995329963299732998329993300033001330023300333004330053300633007330083300933010330113301233013330143301533016330173301833019330203302133022330233302433025330263302733028330293303033031330323303333034330353303633037330383303933040330413304233043330443304533046330473304833049330503305133052330533305433055330563305733058330593306033061330623306333064330653306633067330683306933070330713307233073330743307533076330773307833079330803308133082330833308433085330863308733088330893309033091330923309333094330953309633097330983309933100331013310233103331043310533106331073310833109331103311133112331133311433115331163311733118331193312033121331223312333124331253312633127331283312933130331313313233133331343313533136331373313833139331403314133142331433314433145331463314733148331493315033151331523315333154331553315633157331583315933160331613316233163331643316533166331673316833169331703317133172331733317433175331763317733178331793318033181331823318333184331853318633187331883318933190331913319233193331943319533196331973319833199332003320133202332033320433205332063320733208332093321033211332123321333214332153321633217332183321933220332213322233223332243322533226332273322833229332303323133232332333323433235332363323733238332393324033241332423324333244332453324633247332483324933250332513325233253332543325533256332573325833259332603326133262332633326433265332663326733268332693327033271332723327333274332753327633277332783327933280332813328233283332843328533286332873328833289332903329133292332933329433295332963329733298332993330033301333023330333304333053330633307333083330933310333113331233313333143331533316333173331833319333203332133322333233332433325333263332733328333293333033331333323333333334333353333633337333383333933340333413334233343333443334533346333473334833349333503335133352333533335433355333563335733358333593336033361333623336333364333653336633367333683336933370333713337233373333743337533376333773337833379333803338133382333833338433385333863338733388333893339033391333923339333394333953339633397333983339933400334013340233403334043340533406334073340833409334103341133412334133341433415334163341733418334193342033421334223342333424334253342633427334283342933430334313343233433334343343533436334373343833439334403344133442334433344433445334463344733448334493345033451334523345333454334553345633457334583345933460334613346233463334643346533466334673346833469334703347133472334733347433475334763347733478334793348033481334823348333484334853348633487334883348933490334913349233493334943349533496334973349833499335003350133502335033350433505335063350733508335093351033511335123351333514335153351633517335183351933520335213352233523335243352533526335273352833529335303353133532335333353433535335363353733538335393354033541335423354333544335453354633547335483354933550335513355233553335543355533556335573355833559335603356133562335633356433565335663356733568335693357033571335723357333574335753357633577335783357933580335813358233583335843358533586335873358833589335903359133592335933359433595335963359733598335993360033601336023360333604336053360633607336083360933610336113361233613336143361533616336173361833619336203362133622336233362433625336263362733628336293363033631336323363333634336353363633637336383363933640336413364233643336443364533646336473364833649336503365133652336533365433655336563365733658336593366033661336623366333664336653366633667336683366933670336713367233673336743367533676336773367833679336803368133682336833368433685336863368733688336893369033691336923369333694336953369633697336983369933700337013370233703337043370533706337073370833709337103371133712337133371433715337163371733718337193372033721337223372333724337253372633727337283372933730337313373233733337343373533736337373373833739337403374133742337433374433745337463374733748337493375033751337523375333754337553375633757337583375933760337613376233763337643376533766337673376833769337703377133772337733377433775337763377733778337793378033781337823378333784337853378633787337883378933790337913379233793337943379533796337973379833799338003380133802338033380433805338063380733808338093381033811338123381333814338153381633817338183381933820338213382233823338243382533826338273382833829338303383133832338333383433835338363383733838338393384033841338423384333844338453384633847338483384933850338513385233853338543385533856338573385833859338603386133862338633386433865338663386733868338693387033871338723387333874338753387633877338783387933880338813388233883338843388533886338873388833889338903389133892338933389433895338963389733898338993390033901339023390333904339053390633907339083390933910339113391233913339143391533916339173391833919339203392133922339233392433925339263392733928339293393033931339323393333934339353393633937339383393933940339413394233943339443394533946339473394833949339503395133952339533395433955339563395733958339593396033961339623396333964339653396633967339683396933970339713397233973339743397533976339773397833979339803398133982339833398433985339863398733988339893399033991339923399333994339953399633997339983399934000340013400234003340043400534006340073400834009340103401134012340133401434015340163401734018340193402034021340223402334024340253402634027340283402934030340313403234033340343403534036340373403834039340403404134042340433404434045340463404734048340493405034051340523405334054340553405634057340583405934060340613406234063340643406534066340673406834069340703407134072340733407434075340763407734078340793408034081340823408334084340853408634087340883408934090340913409234093340943409534096340973409834099341003410134102341033410434105341063410734108341093411034111341123411334114341153411634117341183411934120341213412234123341243412534126341273412834129341303413134132341333413434135341363413734138341393414034141341423414334144341453414634147341483414934150341513415234153341543415534156341573415834159341603416134162341633416434165341663416734168341693417034171341723417334174341753417634177341783417934180341813418234183341843418534186341873418834189341903419134192341933419434195341963419734198341993420034201342023420334204342053420634207342083420934210342113421234213342143421534216342173421834219342203422134222342233422434225342263422734228342293423034231342323423334234342353423634237342383423934240342413424234243342443424534246342473424834249342503425134252342533425434255342563425734258342593426034261342623426334264342653426634267342683426934270342713427234273342743427534276342773427834279342803428134282342833428434285342863428734288342893429034291342923429334294342953429634297342983429934300343013430234303343043430534306343073430834309343103431134312343133431434315343163431734318343193432034321343223432334324343253432634327343283432934330343313433234333343343433534336343373433834339343403434134342343433434434345343463434734348343493435034351343523435334354343553435634357343583435934360343613436234363343643436534366343673436834369343703437134372343733437434375343763437734378343793438034381343823438334384343853438634387343883438934390343913439234393343943439534396343973439834399344003440134402344033440434405344063440734408344093441034411344123441334414344153441634417344183441934420344213442234423344243442534426344273442834429344303443134432344333443434435344363443734438344393444034441344423444334444344453444634447344483444934450344513445234453344543445534456344573445834459344603446134462344633446434465344663446734468344693447034471344723447334474344753447634477344783447934480344813448234483344843448534486344873448834489344903449134492344933449434495344963449734498344993450034501345023450334504345053450634507345083450934510345113451234513345143451534516345173451834519345203452134522345233452434525345263452734528345293453034531345323453334534345353453634537345383453934540345413454234543345443454534546345473454834549345503455134552345533455434555345563455734558345593456034561345623456334564345653456634567345683456934570345713457234573345743457534576345773457834579345803458134582345833458434585345863458734588345893459034591345923459334594345953459634597345983459934600346013460234603346043460534606346073460834609346103461134612346133461434615346163461734618346193462034621346223462334624346253462634627346283462934630346313463234633346343463534636346373463834639346403464134642346433464434645346463464734648346493465034651346523465334654346553465634657346583465934660346613466234663346643466534666346673466834669346703467134672346733467434675346763467734678346793468034681346823468334684346853468634687346883468934690346913469234693346943469534696346973469834699347003470134702347033470434705347063470734708347093471034711347123471334714347153471634717347183471934720347213472234723347243472534726347273472834729347303473134732347333473434735347363473734738347393474034741347423474334744347453474634747347483474934750347513475234753347543475534756347573475834759347603476134762347633476434765347663476734768347693477034771347723477334774347753477634777347783477934780347813478234783347843478534786347873478834789347903479134792347933479434795347963479734798347993480034801348023480334804348053480634807348083480934810348113481234813348143481534816348173481834819348203482134822348233482434825348263482734828348293483034831348323483334834348353483634837348383483934840348413484234843348443484534846348473484834849348503485134852348533485434855348563485734858348593486034861348623486334864348653486634867348683486934870348713487234873348743487534876348773487834879348803488134882348833488434885348863488734888348893489034891348923489334894348953489634897348983489934900349013490234903349043490534906349073490834909349103491134912349133491434915349163491734918349193492034921349223492334924349253492634927349283492934930349313493234933349343493534936349373493834939349403494134942349433494434945349463494734948349493495034951349523495334954349553495634957349583495934960349613496234963349643496534966349673496834969349703497134972349733497434975349763497734978349793498034981349823498334984349853498634987349883498934990349913499234993349943499534996349973499834999350003500135002350033500435005350063500735008350093501035011350123501335014350153501635017350183501935020350213502235023350243502535026350273502835029350303503135032350333503435035350363503735038350393504035041350423504335044350453504635047350483504935050350513505235053350543505535056350573505835059350603506135062350633506435065350663506735068350693507035071350723507335074350753507635077350783507935080350813508235083350843508535086350873508835089350903509135092350933509435095350963509735098350993510035101351023510335104351053510635107351083510935110351113511235113351143511535116351173511835119351203512135122351233512435125351263512735128351293513035131351323513335134351353513635137351383513935140351413514235143351443514535146351473514835149351503515135152351533515435155351563515735158351593516035161351623516335164351653516635167351683516935170351713517235173351743517535176351773517835179351803518135182351833518435185351863518735188351893519035191351923519335194351953519635197351983519935200352013520235203352043520535206352073520835209352103521135212352133521435215352163521735218352193522035221352223522335224352253522635227352283522935230352313523235233352343523535236352373523835239352403524135242352433524435245352463524735248352493525035251352523525335254352553525635257352583525935260352613526235263352643526535266352673526835269352703527135272352733527435275352763527735278352793528035281352823528335284352853528635287352883528935290352913529235293352943529535296352973529835299353003530135302353033530435305353063530735308353093531035311353123531335314353153531635317353183531935320353213532235323353243532535326353273532835329353303533135332353333533435335353363533735338353393534035341353423534335344353453534635347353483534935350353513535235353353543535535356353573535835359353603536135362353633536435365353663536735368353693537035371353723537335374353753537635377353783537935380353813538235383353843538535386353873538835389353903539135392353933539435395353963539735398353993540035401354023540335404354053540635407354083540935410354113541235413354143541535416354173541835419354203542135422354233542435425354263542735428354293543035431354323543335434354353543635437354383543935440354413544235443354443544535446354473544835449354503545135452354533545435455354563545735458354593546035461354623546335464354653546635467354683546935470354713547235473354743547535476354773547835479354803548135482354833548435485354863548735488354893549035491354923549335494354953549635497354983549935500355013550235503355043550535506355073550835509355103551135512355133551435515355163551735518355193552035521355223552335524355253552635527355283552935530355313553235533355343553535536355373553835539355403554135542355433554435545355463554735548355493555035551355523555335554355553555635557355583555935560355613556235563355643556535566355673556835569355703557135572355733557435575355763557735578355793558035581355823558335584355853558635587355883558935590355913559235593355943559535596355973559835599356003560135602356033560435605356063560735608356093561035611356123561335614356153561635617356183561935620356213562235623356243562535626356273562835629356303563135632356333563435635356363563735638356393564035641356423564335644356453564635647356483564935650356513565235653356543565535656356573565835659356603566135662356633566435665356663566735668356693567035671356723567335674356753567635677356783567935680356813568235683356843568535686356873568835689356903569135692356933569435695356963569735698356993570035701357023570335704357053570635707357083570935710357113571235713357143571535716357173571835719357203572135722357233572435725357263572735728357293573035731357323573335734357353573635737357383573935740357413574235743357443574535746357473574835749357503575135752357533575435755357563575735758357593576035761357623576335764357653576635767357683576935770357713577235773357743577535776357773577835779357803578135782357833578435785357863578735788357893579035791357923579335794357953579635797357983579935800358013580235803358043580535806358073580835809358103581135812358133581435815358163581735818358193582035821358223582335824358253582635827358283582935830358313583235833358343583535836358373583835839358403584135842358433584435845358463584735848358493585035851358523585335854358553585635857358583585935860358613586235863358643586535866358673586835869358703587135872358733587435875358763587735878358793588035881358823588335884358853588635887358883588935890358913589235893358943589535896358973589835899359003590135902359033590435905359063590735908359093591035911359123591335914359153591635917359183591935920359213592235923359243592535926359273592835929359303593135932359333593435935359363593735938359393594035941359423594335944359453594635947359483594935950359513595235953359543595535956359573595835959359603596135962359633596435965359663596735968359693597035971359723597335974359753597635977359783597935980359813598235983359843598535986359873598835989359903599135992359933599435995359963599735998359993600036001360023600336004360053600636007360083600936010360113601236013360143601536016360173601836019360203602136022360233602436025360263602736028360293603036031360323603336034360353603636037360383603936040360413604236043360443604536046360473604836049360503605136052360533605436055360563605736058360593606036061360623606336064360653606636067360683606936070360713607236073360743607536076360773607836079360803608136082360833608436085360863608736088360893609036091360923609336094360953609636097360983609936100361013610236103361043610536106361073610836109361103611136112361133611436115361163611736118361193612036121361223612336124361253612636127361283612936130361313613236133361343613536136361373613836139361403614136142361433614436145361463614736148361493615036151361523615336154361553615636157361583615936160361613616236163361643616536166361673616836169361703617136172361733617436175361763617736178361793618036181361823618336184361853618636187361883618936190361913619236193361943619536196361973619836199362003620136202362033620436205362063620736208362093621036211362123621336214362153621636217362183621936220362213622236223362243622536226362273622836229362303623136232362333623436235362363623736238362393624036241362423624336244362453624636247362483624936250362513625236253362543625536256362573625836259362603626136262362633626436265362663626736268362693627036271362723627336274362753627636277362783627936280362813628236283362843628536286362873628836289362903629136292362933629436295362963629736298362993630036301363023630336304363053630636307363083630936310363113631236313363143631536316363173631836319363203632136322363233632436325363263632736328363293633036331363323633336334363353633636337363383633936340363413634236343363443634536346363473634836349363503635136352363533635436355363563635736358363593636036361363623636336364363653636636367363683636936370363713637236373363743637536376363773637836379363803638136382363833638436385363863638736388363893639036391363923639336394363953639636397363983639936400364013640236403364043640536406364073640836409364103641136412364133641436415364163641736418364193642036421364223642336424364253642636427364283642936430364313643236433364343643536436364373643836439364403644136442364433644436445364463644736448364493645036451364523645336454364553645636457364583645936460364613646236463364643646536466364673646836469364703647136472364733647436475364763647736478364793648036481364823648336484364853648636487364883648936490364913649236493364943649536496364973649836499365003650136502365033650436505365063650736508365093651036511365123651336514365153651636517365183651936520365213652236523365243652536526365273652836529365303653136532365333653436535365363653736538365393654036541365423654336544365453654636547365483654936550365513655236553365543655536556365573655836559365603656136562365633656436565365663656736568365693657036571365723657336574365753657636577365783657936580365813658236583365843658536586365873658836589365903659136592365933659436595365963659736598365993660036601366023660336604366053660636607366083660936610366113661236613366143661536616366173661836619366203662136622366233662436625366263662736628366293663036631366323663336634366353663636637366383663936640366413664236643366443664536646366473664836649366503665136652366533665436655366563665736658366593666036661366623666336664366653666636667366683666936670366713667236673366743667536676366773667836679366803668136682366833668436685366863668736688366893669036691366923669336694366953669636697366983669936700367013670236703367043670536706367073670836709367103671136712367133671436715367163671736718367193672036721367223672336724367253672636727367283672936730367313673236733367343673536736367373673836739367403674136742367433674436745367463674736748367493675036751367523675336754367553675636757367583675936760367613676236763367643676536766367673676836769367703677136772367733677436775367763677736778367793678036781367823678336784367853678636787367883678936790367913679236793367943679536796367973679836799368003680136802368033680436805368063680736808368093681036811368123681336814368153681636817368183681936820368213682236823368243682536826368273682836829368303683136832368333683436835368363683736838368393684036841368423684336844368453684636847368483684936850368513685236853368543685536856368573685836859368603686136862368633686436865368663686736868368693687036871368723687336874368753687636877368783687936880368813688236883368843688536886368873688836889368903689136892368933689436895368963689736898368993690036901369023690336904369053690636907369083690936910369113691236913369143691536916369173691836919369203692136922369233692436925369263692736928369293693036931369323693336934369353693636937369383693936940369413694236943369443694536946369473694836949369503695136952369533695436955369563695736958369593696036961369623696336964369653696636967369683696936970369713697236973369743697536976369773697836979369803698136982369833698436985369863698736988369893699036991369923699336994369953699636997369983699937000370013700237003370043700537006370073700837009370103701137012370133701437015370163701737018370193702037021370223702337024370253702637027370283702937030370313703237033370343703537036370373703837039370403704137042370433704437045370463704737048370493705037051370523705337054370553705637057370583705937060370613706237063370643706537066370673706837069370703707137072370733707437075370763707737078370793708037081370823708337084370853708637087370883708937090370913709237093370943709537096370973709837099371003710137102371033710437105371063710737108371093711037111371123711337114371153711637117371183711937120371213712237123371243712537126371273712837129371303713137132371333713437135371363713737138371393714037141371423714337144371453714637147371483714937150371513715237153371543715537156371573715837159371603716137162371633716437165371663716737168371693717037171371723717337174371753717637177371783717937180371813718237183371843718537186371873718837189371903719137192371933719437195371963719737198371993720037201372023720337204372053720637207372083720937210372113721237213372143721537216372173721837219372203722137222372233722437225372263722737228372293723037231372323723337234372353723637237372383723937240372413724237243372443724537246372473724837249372503725137252372533725437255372563725737258372593726037261372623726337264372653726637267372683726937270372713727237273372743727537276372773727837279372803728137282372833728437285372863728737288372893729037291372923729337294372953729637297372983729937300373013730237303373043730537306373073730837309373103731137312373133731437315373163731737318373193732037321373223732337324373253732637327373283732937330373313733237333373343733537336373373733837339373403734137342373433734437345373463734737348373493735037351373523735337354373553735637357373583735937360373613736237363373643736537366373673736837369373703737137372373733737437375373763737737378373793738037381373823738337384373853738637387373883738937390373913739237393373943739537396373973739837399374003740137402374033740437405374063740737408374093741037411374123741337414374153741637417374183741937420374213742237423374243742537426374273742837429374303743137432374333743437435374363743737438374393744037441374423744337444374453744637447374483744937450374513745237453374543745537456374573745837459374603746137462374633746437465374663746737468374693747037471374723747337474374753747637477374783747937480374813748237483374843748537486374873748837489374903749137492374933749437495374963749737498374993750037501375023750337504375053750637507375083750937510375113751237513375143751537516375173751837519375203752137522375233752437525375263752737528375293753037531375323753337534375353753637537375383753937540375413754237543375443754537546375473754837549375503755137552375533755437555375563755737558375593756037561375623756337564375653756637567375683756937570375713757237573375743757537576375773757837579375803758137582375833758437585375863758737588375893759037591375923759337594375953759637597375983759937600376013760237603376043760537606376073760837609376103761137612376133761437615376163761737618376193762037621376223762337624376253762637627376283762937630376313763237633376343763537636376373763837639376403764137642376433764437645376463764737648376493765037651376523765337654376553765637657376583765937660376613766237663376643766537666376673766837669376703767137672376733767437675376763767737678376793768037681376823768337684376853768637687376883768937690376913769237693376943769537696376973769837699377003770137702377033770437705377063770737708377093771037711377123771337714377153771637717377183771937720377213772237723377243772537726377273772837729377303773137732377333773437735377363773737738377393774037741377423774337744377453774637747377483774937750377513775237753377543775537756377573775837759377603776137762377633776437765377663776737768377693777037771377723777337774377753777637777377783777937780377813778237783377843778537786377873778837789377903779137792377933779437795377963779737798377993780037801378023780337804378053780637807378083780937810378113781237813378143781537816378173781837819378203782137822378233782437825378263782737828378293783037831378323783337834378353783637837378383783937840378413784237843378443784537846378473784837849378503785137852378533785437855378563785737858378593786037861378623786337864378653786637867378683786937870378713787237873378743787537876378773787837879378803788137882378833788437885378863788737888378893789037891378923789337894378953789637897378983789937900379013790237903379043790537906379073790837909379103791137912379133791437915379163791737918379193792037921379223792337924379253792637927379283792937930379313793237933379343793537936379373793837939379403794137942379433794437945379463794737948379493795037951379523795337954379553795637957379583795937960379613796237963379643796537966379673796837969379703797137972379733797437975379763797737978379793798037981379823798337984379853798637987379883798937990379913799237993379943799537996379973799837999380003800138002380033800438005380063800738008380093801038011380123801338014380153801638017380183801938020380213802238023380243802538026380273802838029380303803138032380333803438035380363803738038380393804038041380423804338044380453804638047380483804938050380513805238053380543805538056380573805838059380603806138062380633806438065380663806738068380693807038071380723807338074380753807638077380783807938080380813808238083380843808538086380873808838089380903809138092380933809438095380963809738098380993810038101381023810338104381053810638107381083810938110381113811238113381143811538116381173811838119381203812138122381233812438125381263812738128381293813038131381323813338134381353813638137381383813938140381413814238143381443814538146381473814838149381503815138152381533815438155381563815738158381593816038161381623816338164381653816638167381683816938170381713817238173381743817538176381773817838179381803818138182381833818438185381863818738188381893819038191381923819338194381953819638197381983819938200382013820238203382043820538206382073820838209382103821138212382133821438215382163821738218382193822038221382223822338224382253822638227382283822938230382313823238233382343823538236382373823838239382403824138242382433824438245382463824738248382493825038251382523825338254382553825638257382583825938260382613826238263382643826538266382673826838269382703827138272382733827438275382763827738278382793828038281382823828338284382853828638287382883828938290382913829238293382943829538296382973829838299383003830138302383033830438305383063830738308383093831038311383123831338314383153831638317383183831938320383213832238323383243832538326383273832838329383303833138332383333833438335383363833738338383393834038341383423834338344383453834638347383483834938350383513835238353383543835538356383573835838359383603836138362383633836438365383663836738368383693837038371383723837338374383753837638377383783837938380383813838238383383843838538386383873838838389383903839138392383933839438395383963839738398383993840038401384023840338404384053840638407384083840938410384113841238413384143841538416384173841838419384203842138422384233842438425384263842738428384293843038431384323843338434384353843638437384383843938440384413844238443384443844538446384473844838449384503845138452384533845438455384563845738458384593846038461384623846338464384653846638467384683846938470384713847238473384743847538476384773847838479384803848138482384833848438485384863848738488384893849038491384923849338494384953849638497384983849938500385013850238503385043850538506385073850838509385103851138512385133851438515385163851738518385193852038521385223852338524385253852638527385283852938530385313853238533385343853538536385373853838539385403854138542385433854438545385463854738548385493855038551385523855338554385553855638557385583855938560385613856238563385643856538566385673856838569385703857138572385733857438575385763857738578385793858038581385823858338584385853858638587385883858938590385913859238593385943859538596385973859838599386003860138602386033860438605386063860738608386093861038611386123861338614386153861638617386183861938620386213862238623386243862538626386273862838629386303863138632386333863438635386363863738638386393864038641386423864338644386453864638647386483864938650386513865238653386543865538656386573865838659386603866138662386633866438665386663866738668386693867038671386723867338674386753867638677386783867938680386813868238683386843868538686386873868838689386903869138692386933869438695386963869738698386993870038701387023870338704387053870638707387083870938710387113871238713387143871538716387173871838719387203872138722387233872438725387263872738728387293873038731387323873338734387353873638737387383873938740387413874238743387443874538746387473874838749387503875138752387533875438755387563875738758387593876038761387623876338764387653876638767387683876938770387713877238773387743877538776387773877838779387803878138782387833878438785387863878738788387893879038791387923879338794387953879638797387983879938800388013880238803388043880538806388073880838809388103881138812388133881438815388163881738818388193882038821388223882338824388253882638827388283882938830388313883238833388343883538836388373883838839388403884138842388433884438845388463884738848388493885038851388523885338854388553885638857388583885938860388613886238863388643886538866388673886838869388703887138872388733887438875388763887738878388793888038881388823888338884388853888638887388883888938890388913889238893388943889538896388973889838899389003890138902389033890438905389063890738908389093891038911389123891338914389153891638917389183891938920389213892238923389243892538926389273892838929389303893138932389333893438935389363893738938389393894038941389423894338944389453894638947389483894938950389513895238953389543895538956389573895838959389603896138962389633896438965389663896738968389693897038971389723897338974389753897638977389783897938980389813898238983389843898538986389873898838989389903899138992389933899438995389963899738998389993900039001390023900339004390053900639007390083900939010390113901239013390143901539016390173901839019390203902139022390233902439025390263902739028390293903039031390323903339034390353903639037390383903939040390413904239043390443904539046390473904839049390503905139052390533905439055390563905739058390593906039061390623906339064390653906639067390683906939070390713907239073390743907539076390773907839079390803908139082390833908439085390863908739088390893909039091390923909339094390953909639097390983909939100391013910239103391043910539106391073910839109391103911139112391133911439115391163911739118391193912039121391223912339124391253912639127391283912939130391313913239133391343913539136391373913839139391403914139142391433914439145391463914739148391493915039151391523915339154391553915639157391583915939160391613916239163391643916539166391673916839169391703917139172391733917439175391763917739178391793918039181391823918339184391853918639187391883918939190391913919239193391943919539196391973919839199392003920139202392033920439205392063920739208392093921039211392123921339214392153921639217392183921939220392213922239223392243922539226392273922839229392303923139232392333923439235392363923739238392393924039241392423924339244392453924639247392483924939250392513925239253392543925539256392573925839259392603926139262392633926439265392663926739268392693927039271392723927339274392753927639277392783927939280392813928239283392843928539286392873928839289392903929139292392933929439295392963929739298392993930039301393023930339304393053930639307393083930939310393113931239313393143931539316393173931839319393203932139322393233932439325393263932739328393293933039331393323933339334393353933639337393383933939340393413934239343393443934539346393473934839349393503935139352393533935439355393563935739358393593936039361393623936339364393653936639367393683936939370393713937239373393743937539376393773937839379393803938139382393833938439385393863938739388393893939039391393923939339394393953939639397393983939939400394013940239403394043940539406394073940839409394103941139412394133941439415394163941739418394193942039421394223942339424394253942639427394283942939430394313943239433394343943539436394373943839439394403944139442394433944439445394463944739448394493945039451394523945339454394553945639457394583945939460394613946239463394643946539466394673946839469394703947139472394733947439475394763947739478394793948039481394823948339484394853948639487394883948939490394913949239493394943949539496394973949839499395003950139502395033950439505395063950739508395093951039511395123951339514395153951639517395183951939520395213952239523395243952539526395273952839529395303953139532395333953439535395363953739538395393954039541395423954339544395453954639547395483954939550395513955239553395543955539556395573955839559395603956139562395633956439565395663956739568395693957039571395723957339574395753957639577395783957939580395813958239583395843958539586395873958839589395903959139592395933959439595395963959739598395993960039601396023960339604396053960639607396083960939610396113961239613396143961539616396173961839619396203962139622396233962439625396263962739628396293963039631396323963339634396353963639637396383963939640396413964239643396443964539646396473964839649396503965139652396533965439655396563965739658396593966039661396623966339664396653966639667396683966939670396713967239673396743967539676396773967839679396803968139682396833968439685396863968739688396893969039691396923969339694396953969639697396983969939700397013970239703397043970539706397073970839709397103971139712397133971439715397163971739718397193972039721397223972339724397253972639727397283972939730397313973239733397343973539736397373973839739397403974139742397433974439745397463974739748397493975039751397523975339754397553975639757397583975939760397613976239763397643976539766397673976839769397703977139772397733977439775397763977739778397793978039781397823978339784397853978639787397883978939790397913979239793397943979539796397973979839799398003980139802398033980439805398063980739808398093981039811398123981339814398153981639817398183981939820398213982239823398243982539826398273982839829398303983139832398333983439835398363983739838398393984039841398423984339844398453984639847398483984939850398513985239853398543985539856398573985839859398603986139862398633986439865398663986739868398693987039871398723987339874398753987639877398783987939880398813988239883398843988539886398873988839889398903989139892398933989439895398963989739898398993990039901399023990339904399053990639907399083990939910399113991239913399143991539916399173991839919399203992139922399233992439925399263992739928399293993039931399323993339934399353993639937399383993939940399413994239943399443994539946399473994839949399503995139952399533995439955399563995739958399593996039961399623996339964399653996639967399683996939970399713997239973399743997539976399773997839979399803998139982399833998439985399863998739988399893999039991399923999339994399953999639997399983999940000400014000240003400044000540006400074000840009400104001140012400134001440015400164001740018400194002040021400224002340024400254002640027400284002940030400314003240033400344003540036400374003840039400404004140042400434004440045400464004740048400494005040051400524005340054400554005640057400584005940060400614006240063400644006540066400674006840069400704007140072400734007440075400764007740078400794008040081400824008340084400854008640087400884008940090400914009240093400944009540096400974009840099401004010140102401034010440105401064010740108401094011040111401124011340114401154011640117401184011940120401214012240123401244012540126401274012840129401304013140132401334013440135401364013740138401394014040141401424014340144401454014640147401484014940150401514015240153401544015540156401574015840159401604016140162401634016440165401664016740168401694017040171401724017340174401754017640177401784017940180401814018240183401844018540186401874018840189401904019140192401934019440195401964019740198401994020040201402024020340204402054020640207402084020940210402114021240213402144021540216402174021840219402204022140222402234022440225402264022740228402294023040231402324023340234402354023640237402384023940240402414024240243402444024540246402474024840249402504025140252402534025440255402564025740258402594026040261402624026340264402654026640267402684026940270402714027240273402744027540276402774027840279402804028140282402834028440285402864028740288402894029040291402924029340294402954029640297402984029940300403014030240303403044030540306403074030840309403104031140312403134031440315403164031740318403194032040321403224032340324403254032640327403284032940330403314033240333403344033540336403374033840339403404034140342403434034440345403464034740348403494035040351403524035340354403554035640357403584035940360403614036240363403644036540366403674036840369403704037140372403734037440375403764037740378403794038040381403824038340384403854038640387403884038940390403914039240393403944039540396403974039840399404004040140402404034040440405404064040740408404094041040411404124041340414404154041640417404184041940420404214042240423404244042540426404274042840429404304043140432404334043440435404364043740438404394044040441404424044340444404454044640447404484044940450404514045240453404544045540456404574045840459404604046140462404634046440465404664046740468404694047040471404724047340474404754047640477404784047940480404814048240483404844048540486404874048840489404904049140492404934049440495404964049740498404994050040501405024050340504405054050640507405084050940510405114051240513405144051540516405174051840519405204052140522405234052440525405264052740528405294053040531405324053340534405354053640537405384053940540405414054240543405444054540546405474054840549405504055140552405534055440555405564055740558405594056040561405624056340564405654056640567405684056940570405714057240573405744057540576405774057840579405804058140582405834058440585405864058740588405894059040591405924059340594405954059640597405984059940600406014060240603406044060540606406074060840609406104061140612406134061440615406164061740618406194062040621406224062340624406254062640627406284062940630406314063240633406344063540636406374063840639406404064140642406434064440645406464064740648406494065040651406524065340654406554065640657406584065940660406614066240663406644066540666406674066840669406704067140672406734067440675406764067740678406794068040681406824068340684406854068640687406884068940690406914069240693406944069540696406974069840699407004070140702407034070440705407064070740708407094071040711407124071340714407154071640717407184071940720407214072240723407244072540726407274072840729407304073140732407334073440735407364073740738407394074040741407424074340744407454074640747407484074940750407514075240753407544075540756407574075840759407604076140762407634076440765407664076740768407694077040771407724077340774407754077640777407784077940780407814078240783407844078540786407874078840789407904079140792407934079440795407964079740798407994080040801408024080340804408054080640807408084080940810408114081240813408144081540816408174081840819408204082140822408234082440825408264082740828408294083040831408324083340834408354083640837408384083940840408414084240843408444084540846408474084840849408504085140852408534085440855408564085740858408594086040861408624086340864408654086640867408684086940870408714087240873408744087540876408774087840879408804088140882408834088440885408864088740888408894089040891408924089340894408954089640897408984089940900409014090240903409044090540906409074090840909409104091140912409134091440915409164091740918409194092040921409224092340924409254092640927409284092940930409314093240933409344093540936409374093840939409404094140942409434094440945409464094740948409494095040951409524095340954409554095640957409584095940960409614096240963409644096540966409674096840969409704097140972409734097440975409764097740978409794098040981409824098340984409854098640987409884098940990409914099240993409944099540996409974099840999410004100141002410034100441005410064100741008410094101041011410124101341014410154101641017410184101941020410214102241023410244102541026410274102841029410304103141032410334103441035410364103741038410394104041041410424104341044410454104641047410484104941050410514105241053410544105541056410574105841059410604106141062410634106441065410664106741068410694107041071410724107341074410754107641077410784107941080410814108241083410844108541086410874108841089410904109141092410934109441095410964109741098410994110041101411024110341104411054110641107411084110941110411114111241113411144111541116411174111841119411204112141122411234112441125411264112741128411294113041131411324113341134411354113641137411384113941140411414114241143411444114541146411474114841149411504115141152411534115441155411564115741158411594116041161411624116341164411654116641167411684116941170411714117241173411744117541176411774117841179411804118141182411834118441185411864118741188411894119041191411924119341194411954119641197411984119941200412014120241203412044120541206412074120841209412104121141212412134121441215412164121741218412194122041221412224122341224412254122641227412284122941230412314123241233412344123541236412374123841239412404124141242412434124441245412464124741248412494125041251412524125341254412554125641257412584125941260412614126241263412644126541266412674126841269412704127141272412734127441275412764127741278412794128041281412824128341284412854128641287412884128941290412914129241293412944129541296412974129841299413004130141302413034130441305413064130741308413094131041311413124131341314413154131641317413184131941320413214132241323413244132541326413274132841329413304133141332413334133441335413364133741338413394134041341413424134341344413454134641347413484134941350413514135241353413544135541356413574135841359413604136141362413634136441365413664136741368413694137041371413724137341374413754137641377413784137941380413814138241383413844138541386413874138841389413904139141392413934139441395413964139741398413994140041401414024140341404414054140641407414084140941410414114141241413414144141541416414174141841419414204142141422414234142441425414264142741428414294143041431414324143341434414354143641437414384143941440414414144241443414444144541446414474144841449414504145141452414534145441455414564145741458414594146041461414624146341464414654146641467414684146941470414714147241473414744147541476414774147841479414804148141482414834148441485414864148741488414894149041491414924149341494414954149641497414984149941500415014150241503415044150541506415074150841509415104151141512415134151441515415164151741518415194152041521415224152341524415254152641527415284152941530415314153241533415344153541536415374153841539415404154141542415434154441545415464154741548415494155041551415524155341554415554155641557415584155941560415614156241563415644156541566415674156841569415704157141572415734157441575415764157741578415794158041581415824158341584415854158641587415884158941590415914159241593415944159541596415974159841599416004160141602416034160441605416064160741608416094161041611416124161341614416154161641617416184161941620416214162241623416244162541626416274162841629416304163141632416334163441635416364163741638416394164041641416424164341644416454164641647416484164941650416514165241653416544165541656416574165841659416604166141662416634166441665416664166741668416694167041671416724167341674416754167641677416784167941680416814168241683416844168541686416874168841689416904169141692416934169441695416964169741698416994170041701417024170341704417054170641707417084170941710417114171241713417144171541716417174171841719417204172141722417234172441725417264172741728417294173041731417324173341734417354173641737417384173941740417414174241743417444174541746417474174841749417504175141752417534175441755417564175741758417594176041761417624176341764417654176641767417684176941770417714177241773417744177541776417774177841779417804178141782417834178441785417864178741788417894179041791417924179341794417954179641797417984179941800418014180241803418044180541806418074180841809418104181141812418134181441815418164181741818418194182041821418224182341824418254182641827418284182941830418314183241833418344183541836418374183841839418404184141842418434184441845418464184741848418494185041851418524185341854418554185641857418584185941860418614186241863418644186541866418674186841869418704187141872418734187441875418764187741878418794188041881418824188341884418854188641887418884188941890418914189241893418944189541896418974189841899419004190141902419034190441905419064190741908419094191041911419124191341914419154191641917419184191941920419214192241923419244192541926419274192841929419304193141932419334193441935419364193741938419394194041941419424194341944419454194641947419484194941950419514195241953419544195541956419574195841959419604196141962419634196441965419664196741968419694197041971419724197341974419754197641977419784197941980419814198241983419844198541986419874198841989419904199141992419934199441995419964199741998419994200042001420024200342004420054200642007420084200942010420114201242013420144201542016420174201842019420204202142022420234202442025420264202742028420294203042031420324203342034420354203642037420384203942040420414204242043420444204542046420474204842049420504205142052420534205442055420564205742058420594206042061420624206342064420654206642067420684206942070420714207242073420744207542076420774207842079420804208142082420834208442085420864208742088420894209042091420924209342094420954209642097420984209942100421014210242103421044210542106421074210842109421104211142112421134211442115421164211742118421194212042121421224212342124421254212642127421284212942130421314213242133421344213542136421374213842139421404214142142421434214442145421464214742148421494215042151421524215342154421554215642157421584215942160421614216242163421644216542166421674216842169421704217142172421734217442175421764217742178421794218042181421824218342184421854218642187421884218942190421914219242193421944219542196421974219842199422004220142202422034220442205422064220742208422094221042211422124221342214422154221642217422184221942220422214222242223422244222542226422274222842229422304223142232422334223442235422364223742238422394224042241422424224342244422454224642247422484224942250422514225242253422544225542256422574225842259422604226142262422634226442265422664226742268422694227042271422724227342274422754227642277422784227942280422814228242283422844228542286422874228842289422904229142292422934229442295422964229742298422994230042301423024230342304423054230642307423084230942310423114231242313423144231542316423174231842319423204232142322423234232442325423264232742328423294233042331423324233342334423354233642337423384233942340423414234242343423444234542346423474234842349423504235142352423534235442355423564235742358423594236042361423624236342364423654236642367423684236942370423714237242373423744237542376423774237842379423804238142382423834238442385423864238742388423894239042391423924239342394423954239642397423984239942400424014240242403424044240542406424074240842409424104241142412424134241442415424164241742418424194242042421424224242342424424254242642427424284242942430424314243242433424344243542436424374243842439424404244142442424434244442445424464244742448424494245042451424524245342454424554245642457424584245942460424614246242463424644246542466424674246842469424704247142472424734247442475424764247742478424794248042481424824248342484424854248642487424884248942490424914249242493424944249542496424974249842499425004250142502425034250442505425064250742508425094251042511425124251342514425154251642517425184251942520425214252242523425244252542526425274252842529425304253142532425334253442535425364253742538425394254042541425424254342544425454254642547425484254942550425514255242553425544255542556425574255842559425604256142562425634256442565425664256742568425694257042571425724257342574425754257642577425784257942580425814258242583425844258542586425874258842589425904259142592425934259442595425964259742598425994260042601426024260342604426054260642607426084260942610426114261242613426144261542616426174261842619426204262142622426234262442625426264262742628426294263042631426324263342634426354263642637426384263942640426414264242643426444264542646426474264842649426504265142652426534265442655426564265742658426594266042661426624266342664426654266642667426684266942670426714267242673426744267542676426774267842679426804268142682426834268442685426864268742688426894269042691426924269342694426954269642697426984269942700427014270242703427044270542706427074270842709427104271142712427134271442715427164271742718427194272042721427224272342724427254272642727427284272942730427314273242733427344273542736427374273842739427404274142742427434274442745427464274742748427494275042751427524275342754427554275642757427584275942760427614276242763427644276542766427674276842769427704277142772427734277442775427764277742778427794278042781427824278342784427854278642787427884278942790427914279242793427944279542796427974279842799428004280142802428034280442805428064280742808428094281042811428124281342814428154281642817428184281942820428214282242823428244282542826428274282842829428304283142832428334283442835428364283742838428394284042841428424284342844428454284642847428484284942850428514285242853428544285542856428574285842859428604286142862428634286442865428664286742868428694287042871428724287342874428754287642877428784287942880428814288242883428844288542886428874288842889428904289142892428934289442895428964289742898428994290042901429024290342904429054290642907429084290942910429114291242913429144291542916429174291842919429204292142922429234292442925429264292742928429294293042931429324293342934429354293642937429384293942940429414294242943429444294542946429474294842949429504295142952429534295442955429564295742958429594296042961429624296342964429654296642967429684296942970429714297242973429744297542976429774297842979429804298142982429834298442985429864298742988429894299042991429924299342994429954299642997429984299943000430014300243003430044300543006430074300843009430104301143012430134301443015430164301743018430194302043021430224302343024430254302643027430284302943030430314303243033430344303543036430374303843039430404304143042430434304443045430464304743048430494305043051430524305343054430554305643057430584305943060430614306243063430644306543066430674306843069430704307143072430734307443075430764307743078430794308043081430824308343084430854308643087430884308943090430914309243093430944309543096430974309843099431004310143102431034310443105431064310743108431094311043111431124311343114431154311643117431184311943120431214312243123431244312543126431274312843129431304313143132431334313443135431364313743138431394314043141431424314343144431454314643147431484314943150431514315243153431544315543156431574315843159431604316143162431634316443165431664316743168431694317043171431724317343174431754317643177431784317943180431814318243183431844318543186431874318843189431904319143192431934319443195431964319743198431994320043201432024320343204432054320643207432084320943210432114321243213432144321543216432174321843219432204322143222432234322443225432264322743228432294323043231432324323343234432354323643237432384323943240432414324243243432444324543246432474324843249432504325143252432534325443255432564325743258432594326043261432624326343264432654326643267432684326943270432714327243273432744327543276432774327843279432804328143282432834328443285432864328743288432894329043291432924329343294432954329643297432984329943300433014330243303433044330543306433074330843309433104331143312433134331443315433164331743318433194332043321433224332343324433254332643327433284332943330433314333243333433344333543336433374333843339433404334143342433434334443345433464334743348433494335043351433524335343354433554335643357433584335943360433614336243363433644336543366433674336843369433704337143372433734337443375433764337743378433794338043381433824338343384433854338643387433884338943390433914339243393433944339543396433974339843399434004340143402434034340443405434064340743408434094341043411434124341343414434154341643417434184341943420434214342243423434244342543426434274342843429434304343143432434334343443435434364343743438434394344043441434424344343444434454344643447434484344943450434514345243453434544345543456434574345843459434604346143462434634346443465434664346743468434694347043471434724347343474434754347643477434784347943480434814348243483434844348543486434874348843489434904349143492434934349443495434964349743498434994350043501435024350343504435054350643507435084350943510435114351243513435144351543516435174351843519435204352143522435234352443525435264352743528435294353043531435324353343534435354353643537435384353943540435414354243543435444354543546435474354843549435504355143552435534355443555435564355743558435594356043561435624356343564435654356643567435684356943570435714357243573435744357543576435774357843579435804358143582435834358443585435864358743588435894359043591435924359343594435954359643597435984359943600436014360243603436044360543606436074360843609436104361143612436134361443615436164361743618436194362043621436224362343624436254362643627436284362943630436314363243633436344363543636436374363843639436404364143642436434364443645436464364743648436494365043651436524365343654436554365643657436584365943660436614366243663436644366543666436674366843669436704367143672436734367443675436764367743678436794368043681436824368343684436854368643687436884368943690436914369243693436944369543696436974369843699437004370143702437034370443705437064370743708437094371043711437124371343714437154371643717437184371943720437214372243723437244372543726437274372843729437304373143732437334373443735437364373743738437394374043741437424374343744437454374643747437484374943750437514375243753437544375543756437574375843759437604376143762437634376443765437664376743768437694377043771437724377343774437754377643777437784377943780437814378243783437844378543786437874378843789437904379143792437934379443795437964379743798437994380043801438024380343804438054380643807438084380943810438114381243813438144381543816438174381843819438204382143822438234382443825438264382743828438294383043831438324383343834438354383643837438384383943840438414384243843438444384543846438474384843849438504385143852438534385443855438564385743858438594386043861438624386343864438654386643867438684386943870438714387243873438744387543876438774387843879438804388143882438834388443885438864388743888438894389043891438924389343894438954389643897438984389943900439014390243903439044390543906439074390843909439104391143912439134391443915439164391743918439194392043921439224392343924439254392643927439284392943930439314393243933439344393543936439374393843939439404394143942439434394443945439464394743948439494395043951439524395343954439554395643957439584395943960439614396243963439644396543966439674396843969439704397143972439734397443975439764397743978439794398043981439824398343984439854398643987439884398943990439914399243993439944399543996439974399843999440004400144002440034400444005440064400744008440094401044011440124401344014440154401644017440184401944020440214402244023440244402544026440274402844029440304403144032440334403444035440364403744038440394404044041440424404344044440454404644047440484404944050440514405244053440544405544056440574405844059440604406144062440634406444065440664406744068440694407044071440724407344074440754407644077440784407944080440814408244083440844408544086440874408844089440904409144092440934409444095440964409744098440994410044101441024410344104441054410644107441084410944110441114411244113441144411544116441174411844119441204412144122441234412444125441264412744128441294413044131441324413344134441354413644137441384413944140441414414244143441444414544146441474414844149441504415144152441534415444155441564415744158441594416044161441624416344164441654416644167441684416944170441714417244173441744417544176441774417844179441804418144182441834418444185441864418744188441894419044191441924419344194441954419644197441984419944200442014420244203442044420544206442074420844209442104421144212442134421444215442164421744218442194422044221442224422344224442254422644227442284422944230442314423244233442344423544236442374423844239442404424144242442434424444245442464424744248442494425044251442524425344254442554425644257442584425944260442614426244263442644426544266442674426844269442704427144272442734427444275442764427744278442794428044281442824428344284442854428644287442884428944290442914429244293442944429544296442974429844299443004430144302443034430444305443064430744308443094431044311443124431344314443154431644317443184431944320443214432244323443244432544326443274432844329443304433144332443334433444335443364433744338443394434044341443424434344344443454434644347443484434944350443514435244353443544435544356443574435844359443604436144362443634436444365443664436744368443694437044371443724437344374443754437644377443784437944380443814438244383443844438544386443874438844389443904439144392443934439444395443964439744398443994440044401444024440344404444054440644407444084440944410444114441244413444144441544416444174441844419444204442144422444234442444425444264442744428444294443044431444324443344434444354443644437444384443944440444414444244443444444444544446444474444844449444504445144452444534445444455444564445744458444594446044461444624446344464444654446644467444684446944470444714447244473444744447544476444774447844479444804448144482444834448444485444864448744488444894449044491444924449344494444954449644497444984449944500445014450244503445044450544506445074450844509445104451144512445134451444515445164451744518445194452044521445224452344524445254452644527445284452944530445314453244533445344453544536445374453844539445404454144542445434454444545445464454744548445494455044551445524455344554445554455644557445584455944560445614456244563445644456544566445674456844569445704457144572445734457444575445764457744578445794458044581445824458344584445854458644587445884458944590445914459244593445944459544596445974459844599446004460144602446034460444605446064460744608446094461044611446124461344614446154461644617446184461944620446214462244623446244462544626446274462844629446304463144632446334463444635446364463744638446394464044641446424464344644446454464644647446484464944650446514465244653446544465544656446574465844659446604466144662446634466444665446664466744668446694467044671446724467344674446754467644677446784467944680446814468244683446844468544686446874468844689446904469144692446934469444695446964469744698446994470044701447024470344704447054470644707447084470944710447114471244713447144471544716447174471844719447204472144722447234472444725447264472744728447294473044731447324473344734447354473644737447384473944740447414474244743447444474544746447474474844749447504475144752447534475444755447564475744758447594476044761447624476344764447654476644767447684476944770447714477244773447744477544776447774477844779447804478144782447834478444785447864478744788447894479044791447924479344794447954479644797447984479944800448014480244803448044480544806448074480844809448104481144812448134481444815448164481744818448194482044821448224482344824448254482644827448284482944830448314483244833448344483544836448374483844839448404484144842448434484444845448464484744848448494485044851448524485344854448554485644857448584485944860448614486244863448644486544866448674486844869448704487144872448734487444875448764487744878448794488044881448824488344884448854488644887448884488944890448914489244893448944489544896448974489844899449004490144902449034490444905449064490744908449094491044911449124491344914449154491644917449184491944920449214492244923449244492544926449274492844929449304493144932449334493444935449364493744938449394494044941449424494344944449454494644947449484494944950449514495244953449544495544956449574495844959449604496144962449634496444965449664496744968449694497044971449724497344974449754497644977449784497944980449814498244983449844498544986449874498844989449904499144992449934499444995449964499744998449994500045001450024500345004450054500645007450084500945010450114501245013450144501545016450174501845019450204502145022450234502445025450264502745028450294503045031450324503345034450354503645037450384503945040450414504245043450444504545046450474504845049450504505145052450534505445055450564505745058450594506045061450624506345064450654506645067450684506945070450714507245073450744507545076450774507845079450804508145082450834508445085450864508745088450894509045091450924509345094450954509645097450984509945100451014510245103451044510545106451074510845109451104511145112451134511445115451164511745118451194512045121451224512345124451254512645127451284512945130451314513245133451344513545136451374513845139451404514145142451434514445145451464514745148451494515045151451524515345154451554515645157451584515945160451614516245163451644516545166451674516845169451704517145172451734517445175451764517745178451794518045181451824518345184451854518645187451884518945190451914519245193451944519545196451974519845199452004520145202452034520445205452064520745208452094521045211452124521345214452154521645217452184521945220452214522245223452244522545226452274522845229452304523145232452334523445235452364523745238452394524045241452424524345244452454524645247452484524945250452514525245253452544525545256452574525845259452604526145262452634526445265452664526745268452694527045271452724527345274452754527645277452784527945280452814528245283452844528545286452874528845289452904529145292452934529445295452964529745298452994530045301453024530345304453054530645307453084530945310453114531245313453144531545316453174531845319453204532145322453234532445325453264532745328453294533045331453324533345334453354533645337453384533945340453414534245343453444534545346453474534845349453504535145352453534535445355453564535745358453594536045361453624536345364453654536645367453684536945370453714537245373453744537545376453774537845379453804538145382453834538445385453864538745388453894539045391453924539345394453954539645397453984539945400454014540245403454044540545406454074540845409454104541145412454134541445415454164541745418454194542045421454224542345424454254542645427454284542945430454314543245433454344543545436454374543845439454404544145442454434544445445454464544745448454494545045451454524545345454454554545645457454584545945460454614546245463454644546545466454674546845469454704547145472454734547445475454764547745478454794548045481454824548345484454854548645487454884548945490454914549245493454944549545496454974549845499455004550145502455034550445505455064550745508455094551045511455124551345514455154551645517455184551945520455214552245523455244552545526455274552845529455304553145532455334553445535455364553745538455394554045541455424554345544455454554645547455484554945550455514555245553455544555545556455574555845559455604556145562455634556445565455664556745568455694557045571455724557345574455754557645577455784557945580455814558245583455844558545586455874558845589455904559145592455934559445595455964559745598455994560045601456024560345604456054560645607456084560945610456114561245613456144561545616456174561845619456204562145622456234562445625456264562745628456294563045631456324563345634456354563645637456384563945640456414564245643456444564545646456474564845649456504565145652456534565445655456564565745658456594566045661456624566345664456654566645667456684566945670456714567245673456744567545676456774567845679456804568145682456834568445685456864568745688456894569045691456924569345694456954569645697456984569945700457014570245703457044570545706457074570845709457104571145712457134571445715457164571745718457194572045721457224572345724457254572645727457284572945730457314573245733457344573545736457374573845739457404574145742457434574445745457464574745748457494575045751457524575345754457554575645757457584575945760457614576245763457644576545766457674576845769457704577145772457734577445775457764577745778457794578045781457824578345784457854578645787457884578945790457914579245793457944579545796457974579845799458004580145802458034580445805458064580745808458094581045811458124581345814458154581645817458184581945820458214582245823458244582545826458274582845829458304583145832458334583445835458364583745838458394584045841458424584345844458454584645847458484584945850458514585245853458544585545856458574585845859458604586145862458634586445865458664586745868458694587045871458724587345874458754587645877458784587945880458814588245883458844588545886458874588845889458904589145892458934589445895458964589745898458994590045901459024590345904459054590645907459084590945910459114591245913459144591545916459174591845919459204592145922459234592445925459264592745928459294593045931459324593345934459354593645937459384593945940459414594245943459444594545946459474594845949459504595145952459534595445955459564595745958459594596045961459624596345964459654596645967459684596945970459714597245973459744597545976459774597845979459804598145982459834598445985459864598745988459894599045991459924599345994459954599645997459984599946000460014600246003460044600546006460074600846009460104601146012460134601446015460164601746018460194602046021460224602346024460254602646027460284602946030460314603246033460344603546036460374603846039460404604146042460434604446045460464604746048460494605046051460524605346054460554605646057460584605946060460614606246063460644606546066460674606846069460704607146072460734607446075460764607746078460794608046081460824608346084460854608646087460884608946090460914609246093460944609546096460974609846099461004610146102461034610446105461064610746108461094611046111461124611346114461154611646117461184611946120461214612246123461244612546126461274612846129461304613146132461334613446135461364613746138461394614046141461424614346144461454614646147461484614946150461514615246153461544615546156461574615846159461604616146162461634616446165461664616746168461694617046171461724617346174461754617646177461784617946180461814618246183461844618546186461874618846189461904619146192461934619446195461964619746198461994620046201462024620346204462054620646207462084620946210462114621246213462144621546216462174621846219462204622146222462234622446225462264622746228462294623046231462324623346234462354623646237462384623946240462414624246243462444624546246462474624846249462504625146252462534625446255462564625746258462594626046261462624626346264462654626646267462684626946270462714627246273462744627546276462774627846279462804628146282462834628446285462864628746288462894629046291462924629346294462954629646297462984629946300463014630246303463044630546306463074630846309463104631146312463134631446315463164631746318463194632046321463224632346324463254632646327463284632946330463314633246333463344633546336463374633846339463404634146342463434634446345463464634746348463494635046351463524635346354463554635646357463584635946360463614636246363463644636546366463674636846369463704637146372463734637446375463764637746378463794638046381463824638346384463854638646387463884638946390463914639246393463944639546396463974639846399464004640146402464034640446405464064640746408464094641046411464124641346414464154641646417464184641946420464214642246423464244642546426464274642846429464304643146432464334643446435464364643746438464394644046441464424644346444464454644646447464484644946450464514645246453464544645546456464574645846459464604646146462464634646446465464664646746468464694647046471464724647346474464754647646477464784647946480464814648246483464844648546486464874648846489464904649146492464934649446495464964649746498464994650046501465024650346504465054650646507465084650946510465114651246513465144651546516465174651846519465204652146522465234652446525465264652746528465294653046531465324653346534465354653646537465384653946540465414654246543465444654546546465474654846549465504655146552465534655446555465564655746558465594656046561465624656346564465654656646567465684656946570465714657246573465744657546576465774657846579465804658146582465834658446585465864658746588465894659046591465924659346594465954659646597465984659946600466014660246603466044660546606466074660846609466104661146612466134661446615466164661746618466194662046621466224662346624466254662646627466284662946630466314663246633466344663546636466374663846639466404664146642466434664446645466464664746648466494665046651466524665346654466554665646657466584665946660466614666246663466644666546666466674666846669466704667146672466734667446675466764667746678466794668046681466824668346684466854668646687466884668946690466914669246693466944669546696466974669846699467004670146702467034670446705467064670746708467094671046711467124671346714467154671646717467184671946720467214672246723467244672546726467274672846729467304673146732467334673446735467364673746738467394674046741467424674346744467454674646747467484674946750467514675246753467544675546756467574675846759467604676146762467634676446765467664676746768467694677046771467724677346774467754677646777467784677946780467814678246783467844678546786467874678846789467904679146792467934679446795467964679746798467994680046801468024680346804468054680646807468084680946810468114681246813468144681546816468174681846819468204682146822468234682446825468264682746828468294683046831468324683346834468354683646837468384683946840468414684246843468444684546846468474684846849468504685146852468534685446855468564685746858468594686046861468624686346864468654686646867468684686946870468714687246873468744687546876468774687846879468804688146882468834688446885468864688746888468894689046891468924689346894468954689646897468984689946900469014690246903469044690546906469074690846909469104691146912469134691446915469164691746918469194692046921469224692346924469254692646927469284692946930469314693246933469344693546936469374693846939469404694146942469434694446945469464694746948469494695046951469524695346954469554695646957469584695946960469614696246963469644696546966469674696846969469704697146972469734697446975469764697746978469794698046981469824698346984469854698646987469884698946990469914699246993469944699546996469974699846999470004700147002470034700447005470064700747008470094701047011470124701347014470154701647017470184701947020470214702247023470244702547026470274702847029470304703147032470334703447035470364703747038470394704047041470424704347044470454704647047470484704947050470514705247053470544705547056470574705847059470604706147062470634706447065470664706747068470694707047071470724707347074470754707647077470784707947080470814708247083470844708547086470874708847089470904709147092470934709447095470964709747098470994710047101471024710347104471054710647107471084710947110471114711247113471144711547116471174711847119471204712147122471234712447125471264712747128471294713047131471324713347134471354713647137471384713947140471414714247143471444714547146471474714847149471504715147152471534715447155471564715747158471594716047161471624716347164471654716647167471684716947170471714717247173471744717547176471774717847179471804718147182471834718447185471864718747188471894719047191471924719347194471954719647197471984719947200472014720247203472044720547206472074720847209472104721147212472134721447215472164721747218472194722047221472224722347224472254722647227472284722947230472314723247233472344723547236472374723847239472404724147242472434724447245472464724747248472494725047251472524725347254472554725647257472584725947260472614726247263472644726547266472674726847269472704727147272472734727447275472764727747278472794728047281472824728347284472854728647287472884728947290472914729247293472944729547296472974729847299473004730147302473034730447305473064730747308473094731047311473124731347314473154731647317473184731947320473214732247323473244732547326473274732847329473304733147332473334733447335473364733747338473394734047341473424734347344473454734647347473484734947350473514735247353473544735547356473574735847359473604736147362473634736447365473664736747368473694737047371473724737347374473754737647377473784737947380473814738247383473844738547386473874738847389473904739147392473934739447395473964739747398473994740047401474024740347404474054740647407474084740947410474114741247413474144741547416474174741847419474204742147422474234742447425474264742747428474294743047431474324743347434474354743647437474384743947440474414744247443474444744547446474474744847449474504745147452474534745447455474564745747458474594746047461474624746347464474654746647467474684746947470474714747247473474744747547476474774747847479474804748147482474834748447485474864748747488474894749047491474924749347494474954749647497474984749947500475014750247503475044750547506475074750847509475104751147512475134751447515475164751747518475194752047521475224752347524475254752647527475284752947530475314753247533475344753547536475374753847539475404754147542475434754447545475464754747548475494755047551475524755347554475554755647557475584755947560475614756247563475644756547566475674756847569475704757147572475734757447575475764757747578475794758047581475824758347584475854758647587475884758947590475914759247593475944759547596475974759847599476004760147602476034760447605476064760747608476094761047611476124761347614476154761647617476184761947620476214762247623476244762547626476274762847629476304763147632476334763447635476364763747638476394764047641476424764347644476454764647647476484764947650476514765247653476544765547656476574765847659476604766147662476634766447665476664766747668476694767047671476724767347674476754767647677476784767947680476814768247683476844768547686476874768847689476904769147692476934769447695476964769747698476994770047701477024770347704477054770647707477084770947710477114771247713477144771547716477174771847719477204772147722477234772447725477264772747728477294773047731477324773347734477354773647737477384773947740477414774247743477444774547746477474774847749477504775147752477534775447755477564775747758477594776047761477624776347764477654776647767477684776947770477714777247773477744777547776477774777847779477804778147782477834778447785477864778747788477894779047791477924779347794477954779647797477984779947800478014780247803478044780547806478074780847809478104781147812478134781447815478164781747818478194782047821478224782347824478254782647827478284782947830478314783247833478344783547836478374783847839478404784147842478434784447845478464784747848478494785047851478524785347854478554785647857478584785947860478614786247863478644786547866478674786847869478704787147872478734787447875478764787747878478794788047881478824788347884478854788647887478884788947890478914789247893478944789547896478974789847899479004790147902479034790447905479064790747908479094791047911479124791347914479154791647917479184791947920479214792247923479244792547926479274792847929479304793147932479334793447935479364793747938479394794047941479424794347944479454794647947479484794947950479514795247953479544795547956479574795847959479604796147962479634796447965479664796747968479694797047971479724797347974479754797647977479784797947980479814798247983479844798547986479874798847989479904799147992479934799447995479964799747998479994800048001480024800348004480054800648007480084800948010480114801248013480144801548016480174801848019480204802148022480234802448025480264802748028480294803048031480324803348034480354803648037480384803948040480414804248043480444804548046480474804848049480504805148052480534805448055480564805748058480594806048061480624806348064480654806648067480684806948070480714807248073480744807548076480774807848079480804808148082480834808448085480864808748088480894809048091480924809348094480954809648097480984809948100481014810248103481044810548106481074810848109481104811148112481134811448115481164811748118481194812048121481224812348124481254812648127481284812948130481314813248133481344813548136481374813848139481404814148142481434814448145481464814748148481494815048151481524815348154481554815648157481584815948160481614816248163481644816548166481674816848169481704817148172481734817448175481764817748178481794818048181481824818348184481854818648187481884818948190481914819248193481944819548196481974819848199482004820148202482034820448205482064820748208482094821048211482124821348214482154821648217482184821948220482214822248223482244822548226482274822848229482304823148232482334823448235482364823748238482394824048241482424824348244482454824648247482484824948250482514825248253482544825548256482574825848259482604826148262482634826448265482664826748268482694827048271482724827348274482754827648277482784827948280482814828248283482844828548286482874828848289482904829148292482934829448295482964829748298482994830048301483024830348304483054830648307483084830948310483114831248313483144831548316483174831848319483204832148322483234832448325483264832748328483294833048331483324833348334483354833648337483384833948340483414834248343483444834548346483474834848349483504835148352483534835448355483564835748358483594836048361483624836348364483654836648367483684836948370483714837248373483744837548376483774837848379483804838148382483834838448385483864838748388483894839048391483924839348394483954839648397483984839948400484014840248403484044840548406484074840848409484104841148412484134841448415484164841748418484194842048421484224842348424484254842648427484284842948430484314843248433484344843548436484374843848439484404844148442484434844448445484464844748448484494845048451484524845348454484554845648457484584845948460484614846248463484644846548466484674846848469484704847148472484734847448475484764847748478484794848048481484824848348484484854848648487484884848948490484914849248493484944849548496484974849848499485004850148502485034850448505485064850748508485094851048511485124851348514485154851648517485184851948520485214852248523485244852548526485274852848529485304853148532485334853448535485364853748538485394854048541485424854348544485454854648547485484854948550485514855248553485544855548556485574855848559485604856148562485634856448565485664856748568485694857048571485724857348574485754857648577485784857948580485814858248583485844858548586485874858848589485904859148592485934859448595485964859748598485994860048601486024860348604486054860648607486084860948610486114861248613486144861548616486174861848619486204862148622486234862448625486264862748628486294863048631486324863348634486354863648637486384863948640486414864248643486444864548646486474864848649486504865148652486534865448655486564865748658486594866048661486624866348664486654866648667486684866948670486714867248673486744867548676486774867848679486804868148682486834868448685486864868748688486894869048691486924869348694486954869648697486984869948700487014870248703487044870548706487074870848709487104871148712487134871448715487164871748718487194872048721487224872348724487254872648727487284872948730487314873248733487344873548736487374873848739487404874148742487434874448745487464874748748487494875048751487524875348754487554875648757487584875948760487614876248763487644876548766487674876848769487704877148772487734877448775487764877748778487794878048781487824878348784487854878648787487884878948790487914879248793487944879548796487974879848799488004880148802488034880448805488064880748808488094881048811488124881348814488154881648817488184881948820488214882248823488244882548826488274882848829488304883148832488334883448835488364883748838488394884048841488424884348844488454884648847488484884948850488514885248853488544885548856488574885848859488604886148862488634886448865488664886748868488694887048871488724887348874488754887648877488784887948880488814888248883488844888548886488874888848889488904889148892488934889448895488964889748898488994890048901489024890348904489054890648907489084890948910489114891248913489144891548916489174891848919489204892148922489234892448925489264892748928489294893048931489324893348934489354893648937489384893948940489414894248943489444894548946489474894848949489504895148952489534895448955489564895748958489594896048961489624896348964489654896648967489684896948970489714897248973489744897548976489774897848979489804898148982489834898448985489864898748988489894899048991489924899348994489954899648997489984899949000490014900249003490044900549006490074900849009490104901149012490134901449015490164901749018490194902049021490224902349024490254902649027490284902949030490314903249033490344903549036490374903849039490404904149042490434904449045490464904749048490494905049051490524905349054490554905649057490584905949060490614906249063490644906549066490674906849069490704907149072490734907449075490764907749078490794908049081490824908349084490854908649087490884908949090490914909249093490944909549096490974909849099491004910149102491034910449105491064910749108491094911049111491124911349114491154911649117491184911949120491214912249123491244912549126491274912849129491304913149132491334913449135491364913749138491394914049141491424914349144491454914649147491484914949150491514915249153491544915549156491574915849159491604916149162491634916449165491664916749168491694917049171491724917349174491754917649177491784917949180491814918249183491844918549186491874918849189491904919149192491934919449195491964919749198491994920049201492024920349204492054920649207492084920949210492114921249213492144921549216492174921849219492204922149222492234922449225492264922749228492294923049231492324923349234492354923649237492384923949240492414924249243492444924549246492474924849249492504925149252492534925449255492564925749258492594926049261492624926349264492654926649267492684926949270492714927249273492744927549276492774927849279492804928149282492834928449285492864928749288492894929049291492924929349294492954929649297492984929949300493014930249303493044930549306493074930849309493104931149312493134931449315493164931749318493194932049321493224932349324493254932649327493284932949330493314933249333493344933549336493374933849339493404934149342493434934449345493464934749348493494935049351493524935349354493554935649357493584935949360493614936249363493644936549366493674936849369493704937149372493734937449375493764937749378493794938049381493824938349384493854938649387493884938949390493914939249393493944939549396493974939849399494004940149402494034940449405494064940749408494094941049411494124941349414494154941649417494184941949420494214942249423494244942549426494274942849429494304943149432494334943449435494364943749438494394944049441494424944349444494454944649447494484944949450494514945249453494544945549456494574945849459494604946149462494634946449465494664946749468494694947049471494724947349474494754947649477494784947949480494814948249483494844948549486494874948849489494904949149492494934949449495494964949749498494994950049501495024950349504495054950649507495084950949510495114951249513495144951549516495174951849519495204952149522495234952449525495264952749528495294953049531495324953349534495354953649537495384953949540495414954249543495444954549546495474954849549495504955149552495534955449555495564955749558495594956049561495624956349564495654956649567495684956949570495714957249573495744957549576495774957849579495804958149582495834958449585495864958749588495894959049591495924959349594495954959649597495984959949600496014960249603496044960549606496074960849609496104961149612496134961449615496164961749618496194962049621496224962349624496254962649627496284962949630496314963249633496344963549636496374963849639496404964149642496434964449645496464964749648496494965049651496524965349654496554965649657496584965949660496614966249663496644966549666496674966849669496704967149672496734967449675496764967749678496794968049681496824968349684496854968649687496884968949690496914969249693496944969549696496974969849699497004970149702497034970449705497064970749708497094971049711497124971349714497154971649717497184971949720497214972249723497244972549726497274972849729497304973149732497334973449735497364973749738497394974049741497424974349744497454974649747497484974949750497514975249753497544975549756497574975849759497604976149762497634976449765497664976749768497694977049771497724977349774497754977649777497784977949780497814978249783497844978549786497874978849789497904979149792497934979449795497964979749798497994980049801498024980349804498054980649807498084980949810498114981249813498144981549816498174981849819498204982149822498234982449825498264982749828498294983049831498324983349834498354983649837498384983949840498414984249843498444984549846498474984849849498504985149852498534985449855498564985749858498594986049861498624986349864498654986649867498684986949870498714987249873498744987549876498774987849879498804988149882498834988449885498864988749888498894989049891498924989349894498954989649897498984989949900499014990249903499044990549906499074990849909499104991149912499134991449915499164991749918499194992049921499224992349924499254992649927499284992949930499314993249933499344993549936499374993849939499404994149942499434994449945499464994749948499494995049951499524995349954499554995649957499584995949960499614996249963499644996549966499674996849969499704997149972499734997449975499764997749978499794998049981499824998349984499854998649987499884998949990499914999249993499944999549996499974999849999500005000150002500035000450005500065000750008500095001050011500125001350014500155001650017500185001950020500215002250023500245002550026500275002850029500305003150032500335003450035500365003750038500395004050041500425004350044500455004650047500485004950050500515005250053500545005550056500575005850059500605006150062500635006450065500665006750068500695007050071500725007350074500755007650077500785007950080500815008250083500845008550086500875008850089500905009150092500935009450095500965009750098500995010050101501025010350104501055010650107501085010950110501115011250113501145011550116501175011850119501205012150122501235012450125501265012750128501295013050131501325013350134501355013650137501385013950140501415014250143501445014550146501475014850149501505015150152501535015450155501565015750158501595016050161501625016350164501655016650167501685016950170501715017250173501745017550176501775017850179501805018150182501835018450185501865018750188501895019050191501925019350194501955019650197501985019950200502015020250203502045020550206502075020850209502105021150212502135021450215502165021750218502195022050221502225022350224502255022650227502285022950230502315023250233502345023550236502375023850239502405024150242502435024450245502465024750248502495025050251502525025350254502555025650257502585025950260502615026250263502645026550266502675026850269502705027150272502735027450275502765027750278502795028050281502825028350284502855028650287502885028950290502915029250293502945029550296502975029850299503005030150302503035030450305503065030750308503095031050311503125031350314503155031650317503185031950320503215032250323503245032550326503275032850329503305033150332503335033450335503365033750338503395034050341503425034350344503455034650347503485034950350503515035250353503545035550356503575035850359503605036150362503635036450365503665036750368503695037050371503725037350374503755037650377503785037950380503815038250383503845038550386503875038850389503905039150392503935039450395503965039750398503995040050401504025040350404504055040650407504085040950410504115041250413504145041550416504175041850419504205042150422504235042450425504265042750428504295043050431504325043350434504355043650437504385043950440504415044250443504445044550446504475044850449504505045150452504535045450455504565045750458504595046050461504625046350464504655046650467504685046950470504715047250473504745047550476504775047850479504805048150482504835048450485504865048750488504895049050491504925049350494504955049650497504985049950500505015050250503505045050550506505075050850509505105051150512505135051450515505165051750518505195052050521505225052350524505255052650527505285052950530505315053250533505345053550536505375053850539505405054150542505435054450545505465054750548505495055050551505525055350554505555055650557505585055950560505615056250563505645056550566505675056850569505705057150572505735057450575505765057750578505795058050581505825058350584505855058650587505885058950590505915059250593505945059550596505975059850599506005060150602506035060450605506065060750608506095061050611506125061350614506155061650617506185061950620506215062250623506245062550626506275062850629506305063150632506335063450635506365063750638506395064050641506425064350644506455064650647506485064950650506515065250653506545065550656506575065850659506605066150662506635066450665506665066750668506695067050671506725067350674506755067650677506785067950680506815068250683506845068550686506875068850689506905069150692506935069450695506965069750698506995070050701507025070350704507055070650707507085070950710507115071250713507145071550716507175071850719507205072150722507235072450725507265072750728507295073050731507325073350734507355073650737507385073950740507415074250743507445074550746507475074850749507505075150752507535075450755507565075750758507595076050761507625076350764507655076650767507685076950770507715077250773507745077550776507775077850779507805078150782507835078450785507865078750788507895079050791507925079350794507955079650797507985079950800508015080250803508045080550806508075080850809508105081150812508135081450815508165081750818508195082050821508225082350824508255082650827508285082950830508315083250833508345083550836508375083850839508405084150842508435084450845508465084750848508495085050851508525085350854508555085650857508585085950860508615086250863508645086550866508675086850869508705087150872508735087450875508765087750878508795088050881508825088350884508855088650887508885088950890508915089250893508945089550896508975089850899509005090150902509035090450905509065090750908509095091050911509125091350914509155091650917509185091950920509215092250923509245092550926509275092850929509305093150932509335093450935509365093750938509395094050941509425094350944509455094650947509485094950950509515095250953509545095550956509575095850959509605096150962509635096450965509665096750968509695097050971509725097350974509755097650977509785097950980509815098250983509845098550986509875098850989509905099150992509935099450995509965099750998509995100051001510025100351004510055100651007510085100951010510115101251013510145101551016510175101851019510205102151022510235102451025510265102751028510295103051031510325103351034510355103651037510385103951040510415104251043510445104551046510475104851049510505105151052510535105451055510565105751058510595106051061510625106351064510655106651067510685106951070510715107251073510745107551076510775107851079510805108151082510835108451085510865108751088510895109051091510925109351094510955109651097510985109951100511015110251103511045110551106511075110851109511105111151112511135111451115511165111751118511195112051121511225112351124511255112651127511285112951130511315113251133511345113551136511375113851139511405114151142511435114451145511465114751148511495115051151511525115351154511555115651157511585115951160511615116251163511645116551166511675116851169511705117151172511735117451175511765117751178511795118051181511825118351184511855118651187511885118951190511915119251193511945119551196511975119851199512005120151202512035120451205512065120751208512095121051211512125121351214512155121651217512185121951220512215122251223512245122551226512275122851229512305123151232512335123451235512365123751238512395124051241512425124351244512455124651247512485124951250512515125251253512545125551256512575125851259512605126151262512635126451265512665126751268512695127051271512725127351274512755127651277512785127951280512815128251283512845128551286512875128851289512905129151292512935129451295512965129751298512995130051301513025130351304513055130651307513085130951310513115131251313513145131551316513175131851319513205132151322513235132451325513265132751328513295133051331513325133351334513355133651337513385133951340513415134251343513445134551346513475134851349513505135151352513535135451355513565135751358513595136051361513625136351364513655136651367513685136951370513715137251373513745137551376513775137851379513805138151382513835138451385513865138751388513895139051391513925139351394513955139651397513985139951400514015140251403514045140551406514075140851409514105141151412514135141451415514165141751418514195142051421514225142351424514255142651427514285142951430514315143251433514345143551436514375143851439514405144151442514435144451445514465144751448514495145051451514525145351454514555145651457514585145951460514615146251463514645146551466514675146851469514705147151472514735147451475514765147751478514795148051481514825148351484514855148651487514885148951490514915149251493514945149551496514975149851499515005150151502515035150451505515065150751508515095151051511515125151351514515155151651517515185151951520515215152251523515245152551526515275152851529515305153151532515335153451535515365153751538515395154051541515425154351544515455154651547515485154951550515515155251553515545155551556515575155851559515605156151562515635156451565515665156751568515695157051571515725157351574515755157651577515785157951580515815158251583515845158551586515875158851589515905159151592515935159451595515965159751598515995160051601516025160351604516055160651607516085160951610516115161251613516145161551616516175161851619516205162151622516235162451625516265162751628516295163051631516325163351634516355163651637516385163951640516415164251643516445164551646516475164851649516505165151652516535165451655516565165751658516595166051661516625166351664516655166651667516685166951670516715167251673516745167551676516775167851679516805168151682516835168451685516865168751688516895169051691516925169351694516955169651697516985169951700517015170251703517045170551706517075170851709517105171151712517135171451715517165171751718517195172051721517225172351724517255172651727517285172951730517315173251733517345173551736517375173851739517405174151742517435174451745517465174751748517495175051751517525175351754517555175651757517585175951760517615176251763517645176551766517675176851769517705177151772517735177451775517765177751778517795178051781517825178351784517855178651787517885178951790517915179251793517945179551796517975179851799518005180151802518035180451805518065180751808518095181051811518125181351814518155181651817518185181951820518215182251823518245182551826518275182851829518305183151832518335183451835518365183751838518395184051841518425184351844518455184651847518485184951850518515185251853518545185551856518575185851859518605186151862518635186451865518665186751868518695187051871518725187351874518755187651877518785187951880518815188251883518845188551886518875188851889518905189151892518935189451895518965189751898518995190051901519025190351904519055190651907519085190951910519115191251913519145191551916519175191851919519205192151922519235192451925519265192751928519295193051931519325193351934519355193651937519385193951940519415194251943519445194551946519475194851949519505195151952519535195451955519565195751958519595196051961519625196351964519655196651967519685196951970519715197251973519745197551976519775197851979519805198151982519835198451985519865198751988519895199051991519925199351994519955199651997519985199952000520015200252003520045200552006520075200852009520105201152012520135201452015520165201752018520195202052021520225202352024520255202652027520285202952030520315203252033520345203552036520375203852039520405204152042520435204452045520465204752048520495205052051520525205352054520555205652057520585205952060520615206252063520645206552066520675206852069520705207152072520735207452075520765207752078520795208052081520825208352084520855208652087520885208952090520915209252093520945209552096520975209852099521005210152102521035210452105521065210752108521095211052111521125211352114521155211652117521185211952120521215212252123521245212552126521275212852129521305213152132521335213452135521365213752138521395214052141521425214352144521455214652147521485214952150521515215252153521545215552156521575215852159521605216152162521635216452165521665216752168521695217052171521725217352174521755217652177521785217952180521815218252183521845218552186521875218852189521905219152192521935219452195521965219752198521995220052201522025220352204522055220652207522085220952210522115221252213522145221552216522175221852219522205222152222522235222452225522265222752228522295223052231522325223352234522355223652237522385223952240522415224252243522445224552246522475224852249522505225152252522535225452255522565225752258522595226052261522625226352264522655226652267522685226952270522715227252273522745227552276522775227852279522805228152282522835228452285522865228752288522895229052291522925229352294522955229652297522985229952300523015230252303523045230552306523075230852309523105231152312523135231452315523165231752318523195232052321523225232352324523255232652327523285232952330523315233252333523345233552336523375233852339523405234152342523435234452345523465234752348523495235052351523525235352354523555235652357523585235952360523615236252363523645236552366523675236852369523705237152372523735237452375523765237752378523795238052381523825238352384523855238652387523885238952390523915239252393523945239552396523975239852399524005240152402524035240452405524065240752408524095241052411524125241352414524155241652417524185241952420524215242252423524245242552426524275242852429524305243152432524335243452435524365243752438524395244052441524425244352444524455244652447524485244952450524515245252453524545245552456524575245852459524605246152462524635246452465524665246752468524695247052471524725247352474524755247652477524785247952480524815248252483524845248552486524875248852489524905249152492524935249452495524965249752498524995250052501525025250352504525055250652507525085250952510525115251252513525145251552516525175251852519525205252152522525235252452525525265252752528525295253052531525325253352534525355253652537525385253952540525415254252543525445254552546525475254852549525505255152552525535255452555525565255752558525595256052561525625256352564525655256652567525685256952570525715257252573525745257552576525775257852579525805258152582525835258452585525865258752588525895259052591525925259352594525955259652597525985259952600526015260252603526045260552606526075260852609526105261152612526135261452615526165261752618526195262052621526225262352624526255262652627526285262952630526315263252633526345263552636526375263852639526405264152642526435264452645526465264752648526495265052651526525265352654526555265652657526585265952660526615266252663526645266552666526675266852669526705267152672526735267452675526765267752678526795268052681526825268352684526855268652687526885268952690526915269252693526945269552696526975269852699527005270152702527035270452705527065270752708527095271052711527125271352714527155271652717527185271952720527215272252723527245272552726527275272852729527305273152732527335273452735527365273752738527395274052741527425274352744527455274652747527485274952750527515275252753527545275552756527575275852759527605276152762527635276452765527665276752768527695277052771527725277352774527755277652777527785277952780527815278252783527845278552786527875278852789527905279152792527935279452795527965279752798527995280052801528025280352804528055280652807528085280952810528115281252813528145281552816528175281852819528205282152822528235282452825528265282752828528295283052831528325283352834528355283652837528385283952840528415284252843528445284552846528475284852849528505285152852528535285452855528565285752858528595286052861528625286352864528655286652867528685286952870528715287252873528745287552876528775287852879528805288152882528835288452885528865288752888528895289052891528925289352894528955289652897528985289952900529015290252903529045290552906529075290852909529105291152912529135291452915529165291752918529195292052921529225292352924529255292652927529285292952930529315293252933529345293552936529375293852939529405294152942529435294452945529465294752948529495295052951529525295352954529555295652957529585295952960529615296252963529645296552966529675296852969529705297152972529735297452975529765297752978529795298052981529825298352984529855298652987529885298952990529915299252993529945299552996529975299852999530005300153002530035300453005530065300753008530095301053011530125301353014530155301653017530185301953020530215302253023530245302553026530275302853029530305303153032530335303453035530365303753038530395304053041530425304353044530455304653047530485304953050530515305253053530545305553056530575305853059530605306153062530635306453065530665306753068530695307053071530725307353074530755307653077530785307953080530815308253083530845308553086530875308853089530905309153092530935309453095530965309753098530995310053101531025310353104531055310653107531085310953110531115311253113531145311553116531175311853119531205312153122531235312453125531265312753128531295313053131531325313353134531355313653137531385313953140531415314253143531445314553146531475314853149531505315153152531535315453155531565315753158531595316053161531625316353164531655316653167531685316953170531715317253173531745317553176531775317853179531805318153182531835318453185531865318753188531895319053191531925319353194531955319653197531985319953200532015320253203532045320553206532075320853209532105321153212532135321453215532165321753218532195322053221532225322353224532255322653227532285322953230532315323253233532345323553236532375323853239532405324153242532435324453245532465324753248532495325053251532525325353254532555325653257532585325953260532615326253263532645326553266532675326853269532705327153272532735327453275532765327753278532795328053281532825328353284532855328653287532885328953290532915329253293532945329553296532975329853299533005330153302533035330453305533065330753308533095331053311533125331353314533155331653317533185331953320533215332253323533245332553326533275332853329533305333153332533335333453335533365333753338533395334053341533425334353344533455334653347533485334953350533515335253353533545335553356533575335853359533605336153362533635336453365533665336753368533695337053371533725337353374533755337653377533785337953380533815338253383533845338553386533875338853389533905339153392533935339453395533965339753398533995340053401534025340353404534055340653407534085340953410534115341253413534145341553416534175341853419534205342153422534235342453425534265342753428534295343053431534325343353434534355343653437534385343953440534415344253443534445344553446534475344853449534505345153452534535345453455534565345753458534595346053461534625346353464534655346653467534685346953470534715347253473534745347553476534775347853479534805348153482534835348453485534865348753488534895349053491534925349353494534955349653497534985349953500535015350253503535045350553506535075350853509535105351153512535135351453515535165351753518535195352053521535225352353524535255352653527535285352953530535315353253533535345353553536535375353853539535405354153542535435354453545535465354753548535495355053551535525355353554535555355653557535585355953560535615356253563535645356553566535675356853569535705357153572535735357453575535765357753578535795358053581535825358353584535855358653587535885358953590535915359253593535945359553596535975359853599536005360153602536035360453605536065360753608536095361053611536125361353614536155361653617536185361953620536215362253623536245362553626536275362853629536305363153632536335363453635536365363753638536395364053641536425364353644536455364653647536485364953650536515365253653536545365553656536575365853659536605366153662536635366453665536665366753668536695367053671536725367353674536755367653677536785367953680536815368253683536845368553686536875368853689536905369153692536935369453695536965369753698536995370053701537025370353704537055370653707537085370953710537115371253713537145371553716537175371853719537205372153722537235372453725537265372753728537295373053731537325373353734537355373653737537385373953740537415374253743537445374553746537475374853749537505375153752537535375453755537565375753758537595376053761537625376353764537655376653767537685376953770537715377253773537745377553776537775377853779537805378153782537835378453785537865378753788537895379053791537925379353794537955379653797537985379953800538015380253803538045380553806538075380853809538105381153812538135381453815538165381753818538195382053821538225382353824538255382653827538285382953830538315383253833538345383553836538375383853839538405384153842538435384453845538465384753848538495385053851538525385353854538555385653857538585385953860538615386253863538645386553866538675386853869538705387153872538735387453875538765387753878538795388053881538825388353884538855388653887538885388953890538915389253893538945389553896538975389853899539005390153902539035390453905539065390753908539095391053911539125391353914539155391653917539185391953920539215392253923539245392553926539275392853929539305393153932539335393453935539365393753938539395394053941539425394353944539455394653947539485394953950539515395253953539545395553956539575395853959539605396153962539635396453965539665396753968539695397053971539725397353974539755397653977539785397953980539815398253983539845398553986539875398853989539905399153992539935399453995539965399753998539995400054001540025400354004540055400654007540085400954010540115401254013540145401554016540175401854019540205402154022540235402454025540265402754028540295403054031540325403354034540355403654037540385403954040540415404254043540445404554046540475404854049540505405154052540535405454055540565405754058540595406054061540625406354064540655406654067540685406954070540715407254073540745407554076540775407854079540805408154082540835408454085540865408754088540895409054091540925409354094540955409654097540985409954100541015410254103541045410554106541075410854109541105411154112541135411454115541165411754118541195412054121541225412354124541255412654127541285412954130541315413254133541345413554136541375413854139541405414154142541435414454145541465414754148541495415054151541525415354154541555415654157541585415954160541615416254163541645416554166541675416854169541705417154172541735417454175541765417754178541795418054181541825418354184541855418654187541885418954190541915419254193541945419554196541975419854199542005420154202542035420454205542065420754208542095421054211542125421354214542155421654217542185421954220542215422254223542245422554226542275422854229542305423154232542335423454235542365423754238542395424054241542425424354244542455424654247542485424954250542515425254253542545425554256542575425854259542605426154262542635426454265542665426754268542695427054271542725427354274542755427654277542785427954280542815428254283542845428554286542875428854289542905429154292542935429454295542965429754298542995430054301543025430354304543055430654307543085430954310543115431254313543145431554316543175431854319543205432154322543235432454325543265432754328543295433054331543325433354334543355433654337543385433954340543415434254343543445434554346543475434854349543505435154352543535435454355543565435754358543595436054361543625436354364543655436654367543685436954370543715437254373543745437554376543775437854379543805438154382543835438454385543865438754388543895439054391543925439354394543955439654397543985439954400544015440254403544045440554406544075440854409544105441154412544135441454415544165441754418544195442054421544225442354424544255442654427544285442954430544315443254433544345443554436544375443854439544405444154442544435444454445544465444754448544495445054451544525445354454544555445654457544585445954460544615446254463544645446554466544675446854469544705447154472544735447454475544765447754478544795448054481544825448354484544855448654487544885448954490544915449254493544945449554496544975449854499545005450154502545035450454505545065450754508545095451054511545125451354514545155451654517545185451954520545215452254523545245452554526545275452854529545305453154532545335453454535545365453754538545395454054541545425454354544545455454654547545485454954550545515455254553545545455554556545575455854559545605456154562545635456454565545665456754568545695457054571545725457354574545755457654577545785457954580545815458254583545845458554586545875458854589545905459154592545935459454595545965459754598545995460054601546025460354604546055460654607546085460954610546115461254613546145461554616546175461854619546205462154622546235462454625546265462754628546295463054631546325463354634546355463654637546385463954640546415464254643546445464554646546475464854649546505465154652546535465454655546565465754658546595466054661546625466354664546655466654667546685466954670546715467254673546745467554676546775467854679546805468154682546835468454685546865468754688546895469054691546925469354694546955469654697546985469954700547015470254703547045470554706547075470854709547105471154712547135471454715547165471754718547195472054721547225472354724547255472654727547285472954730547315473254733547345473554736547375473854739547405474154742547435474454745547465474754748547495475054751547525475354754547555475654757547585475954760547615476254763547645476554766547675476854769547705477154772547735477454775547765477754778547795478054781547825478354784547855478654787547885478954790547915479254793547945479554796547975479854799548005480154802548035480454805548065480754808548095481054811548125481354814548155481654817548185481954820548215482254823548245482554826548275482854829548305483154832548335483454835548365483754838548395484054841548425484354844548455484654847548485484954850548515485254853548545485554856548575485854859548605486154862548635486454865548665486754868548695487054871548725487354874548755487654877548785487954880548815488254883548845488554886548875488854889548905489154892548935489454895548965489754898548995490054901549025490354904549055490654907549085490954910549115491254913549145491554916549175491854919549205492154922549235492454925549265492754928549295493054931549325493354934549355493654937549385493954940549415494254943549445494554946549475494854949549505495154952549535495454955549565495754958549595496054961549625496354964549655496654967549685496954970549715497254973549745497554976549775497854979549805498154982549835498454985549865498754988549895499054991549925499354994549955499654997549985499955000550015500255003550045500555006550075500855009550105501155012550135501455015550165501755018550195502055021550225502355024550255502655027550285502955030550315503255033550345503555036550375503855039550405504155042550435504455045550465504755048550495505055051550525505355054550555505655057550585505955060550615506255063550645506555066550675506855069550705507155072550735507455075550765507755078550795508055081550825508355084550855508655087550885508955090550915509255093550945509555096550975509855099551005510155102551035510455105551065510755108551095511055111551125511355114551155511655117551185511955120551215512255123551245512555126551275512855129551305513155132551335513455135551365513755138551395514055141551425514355144551455514655147551485514955150551515515255153551545515555156551575515855159551605516155162551635516455165551665516755168551695517055171551725517355174551755517655177551785517955180551815518255183551845518555186551875518855189551905519155192551935519455195551965519755198551995520055201552025520355204552055520655207552085520955210552115521255213552145521555216552175521855219552205522155222552235522455225552265522755228552295523055231552325523355234552355523655237552385523955240552415524255243552445524555246552475524855249552505525155252552535525455255552565525755258552595526055261552625526355264552655526655267552685526955270552715527255273552745527555276552775527855279552805528155282552835528455285552865528755288552895529055291552925529355294552955529655297552985529955300553015530255303553045530555306553075530855309553105531155312553135531455315553165531755318553195532055321553225532355324553255532655327553285532955330553315533255333553345533555336553375533855339553405534155342553435534455345553465534755348553495535055351553525535355354553555535655357553585535955360553615536255363553645536555366553675536855369553705537155372553735537455375553765537755378553795538055381553825538355384553855538655387553885538955390553915539255393553945539555396553975539855399554005540155402554035540455405554065540755408554095541055411554125541355414554155541655417554185541955420554215542255423554245542555426554275542855429554305543155432554335543455435554365543755438554395544055441554425544355444554455544655447554485544955450554515545255453554545545555456554575545855459554605546155462554635546455465554665546755468554695547055471554725547355474554755547655477554785547955480554815548255483554845548555486554875548855489554905549155492554935549455495554965549755498554995550055501555025550355504555055550655507555085550955510555115551255513555145551555516555175551855519555205552155522555235552455525555265552755528555295553055531555325553355534555355553655537555385553955540555415554255543555445554555546555475554855549555505555155552555535555455555555565555755558555595556055561555625556355564555655556655567555685556955570555715557255573555745557555576555775557855579555805558155582555835558455585555865558755588555895559055591555925559355594555955559655597555985559955600556015560255603556045560555606556075560855609556105561155612556135561455615556165561755618556195562055621556225562355624556255562655627556285562955630556315563255633556345563555636556375563855639556405564155642556435564455645556465564755648556495565055651556525565355654556555565655657556585565955660556615566255663556645566555666556675566855669556705567155672556735567455675556765567755678556795568055681556825568355684556855568655687556885568955690556915569255693556945569555696556975569855699557005570155702557035570455705557065570755708557095571055711557125571355714557155571655717557185571955720557215572255723557245572555726557275572855729557305573155732557335573455735557365573755738557395574055741557425574355744557455574655747557485574955750557515575255753557545575555756557575575855759557605576155762557635576455765557665576755768557695577055771557725577355774557755577655777557785577955780557815578255783557845578555786557875578855789557905579155792557935579455795557965579755798557995580055801558025580355804558055580655807558085580955810558115581255813558145581555816558175581855819558205582155822558235582455825558265582755828558295583055831558325583355834558355583655837558385583955840558415584255843558445584555846558475584855849558505585155852558535585455855558565585755858558595586055861558625586355864558655586655867558685586955870558715587255873558745587555876558775587855879558805588155882558835588455885558865588755888558895589055891558925589355894558955589655897558985589955900559015590255903559045590555906559075590855909559105591155912559135591455915559165591755918559195592055921559225592355924559255592655927559285592955930559315593255933559345593555936559375593855939559405594155942559435594455945559465594755948559495595055951559525595355954559555595655957559585595955960559615596255963559645596555966559675596855969559705597155972559735597455975559765597755978559795598055981559825598355984559855598655987559885598955990559915599255993559945599555996559975599855999560005600156002560035600456005560065600756008560095601056011560125601356014560155601656017560185601956020560215602256023560245602556026560275602856029560305603156032560335603456035560365603756038560395604056041560425604356044560455604656047560485604956050560515605256053560545605556056560575605856059560605606156062560635606456065560665606756068560695607056071560725607356074560755607656077560785607956080560815608256083560845608556086560875608856089560905609156092560935609456095560965609756098560995610056101561025610356104561055610656107561085610956110561115611256113561145611556116561175611856119561205612156122561235612456125561265612756128561295613056131561325613356134561355613656137561385613956140561415614256143561445614556146561475614856149561505615156152561535615456155561565615756158561595616056161561625616356164561655616656167561685616956170561715617256173561745617556176561775617856179561805618156182561835618456185561865618756188561895619056191561925619356194561955619656197561985619956200562015620256203562045620556206562075620856209562105621156212562135621456215562165621756218562195622056221562225622356224562255622656227562285622956230562315623256233562345623556236562375623856239562405624156242562435624456245562465624756248562495625056251562525625356254562555625656257562585625956260562615626256263562645626556266562675626856269562705627156272562735627456275562765627756278562795628056281562825628356284562855628656287562885628956290562915629256293562945629556296562975629856299563005630156302563035630456305563065630756308563095631056311563125631356314563155631656317563185631956320563215632256323563245632556326563275632856329563305633156332563335633456335563365633756338563395634056341563425634356344563455634656347563485634956350563515635256353563545635556356563575635856359563605636156362563635636456365563665636756368563695637056371563725637356374563755637656377563785637956380563815638256383563845638556386563875638856389563905639156392563935639456395563965639756398563995640056401564025640356404564055640656407564085640956410564115641256413564145641556416564175641856419564205642156422564235642456425564265642756428564295643056431564325643356434564355643656437564385643956440564415644256443564445644556446564475644856449564505645156452564535645456455564565645756458564595646056461564625646356464564655646656467564685646956470564715647256473564745647556476564775647856479564805648156482564835648456485564865648756488564895649056491564925649356494564955649656497564985649956500565015650256503565045650556506565075650856509565105651156512565135651456515565165651756518565195652056521565225652356524565255652656527565285652956530565315653256533565345653556536565375653856539565405654156542565435654456545565465654756548565495655056551565525655356554565555655656557565585655956560565615656256563565645656556566565675656856569565705657156572565735657456575565765657756578565795658056581565825658356584565855658656587565885658956590565915659256593565945659556596565975659856599566005660156602566035660456605566065660756608566095661056611566125661356614566155661656617566185661956620566215662256623566245662556626566275662856629566305663156632566335663456635566365663756638566395664056641566425664356644566455664656647566485664956650566515665256653566545665556656566575665856659566605666156662566635666456665566665666756668566695667056671566725667356674566755667656677566785667956680566815668256683566845668556686566875668856689566905669156692566935669456695566965669756698566995670056701567025670356704567055670656707567085670956710567115671256713567145671556716567175671856719567205672156722567235672456725567265672756728567295673056731567325673356734567355673656737567385673956740567415674256743567445674556746567475674856749567505675156752567535675456755567565675756758567595676056761567625676356764567655676656767567685676956770567715677256773567745677556776567775677856779567805678156782567835678456785567865678756788567895679056791567925679356794567955679656797567985679956800568015680256803568045680556806568075680856809568105681156812568135681456815568165681756818568195682056821568225682356824568255682656827568285682956830568315683256833568345683556836568375683856839568405684156842568435684456845568465684756848568495685056851568525685356854568555685656857568585685956860568615686256863568645686556866568675686856869568705687156872568735687456875568765687756878568795688056881568825688356884568855688656887568885688956890568915689256893568945689556896568975689856899569005690156902569035690456905569065690756908569095691056911569125691356914569155691656917569185691956920569215692256923569245692556926569275692856929569305693156932569335693456935569365693756938569395694056941569425694356944569455694656947569485694956950569515695256953569545695556956569575695856959569605696156962569635696456965569665696756968569695697056971569725697356974569755697656977569785697956980569815698256983569845698556986569875698856989569905699156992569935699456995569965699756998569995700057001570025700357004570055700657007570085700957010570115701257013570145701557016570175701857019570205702157022570235702457025570265702757028570295703057031570325703357034570355703657037570385703957040570415704257043570445704557046570475704857049570505705157052570535705457055570565705757058570595706057061570625706357064570655706657067570685706957070570715707257073570745707557076570775707857079570805708157082570835708457085570865708757088570895709057091570925709357094570955709657097570985709957100571015710257103571045710557106571075710857109571105711157112571135711457115571165711757118571195712057121571225712357124571255712657127571285712957130571315713257133571345713557136571375713857139571405714157142571435714457145571465714757148571495715057151571525715357154571555715657157571585715957160571615716257163571645716557166571675716857169571705717157172571735717457175571765717757178571795718057181571825718357184571855718657187571885718957190571915719257193571945719557196571975719857199572005720157202572035720457205572065720757208572095721057211572125721357214572155721657217572185721957220572215722257223572245722557226572275722857229572305723157232572335723457235572365723757238572395724057241572425724357244572455724657247572485724957250572515725257253572545725557256572575725857259572605726157262572635726457265572665726757268572695727057271572725727357274572755727657277572785727957280572815728257283572845728557286572875728857289572905729157292572935729457295572965729757298572995730057301573025730357304573055730657307573085730957310573115731257313573145731557316573175731857319573205732157322573235732457325573265732757328573295733057331573325733357334573355733657337573385733957340573415734257343573445734557346573475734857349573505735157352573535735457355573565735757358573595736057361573625736357364573655736657367573685736957370573715737257373573745737557376573775737857379573805738157382573835738457385573865738757388573895739057391573925739357394573955739657397573985739957400574015740257403574045740557406574075740857409574105741157412574135741457415574165741757418574195742057421574225742357424574255742657427574285742957430574315743257433574345743557436574375743857439574405744157442574435744457445574465744757448574495745057451574525745357454574555745657457574585745957460574615746257463574645746557466574675746857469574705747157472574735747457475574765747757478574795748057481574825748357484574855748657487574885748957490574915749257493574945749557496574975749857499575005750157502575035750457505575065750757508575095751057511575125751357514575155751657517575185751957520575215752257523575245752557526575275752857529575305753157532575335753457535575365753757538575395754057541575425754357544575455754657547575485754957550575515755257553575545755557556575575755857559575605756157562575635756457565575665756757568575695757057571575725757357574575755757657577575785757957580575815758257583575845758557586575875758857589575905759157592575935759457595575965759757598575995760057601576025760357604576055760657607576085760957610576115761257613576145761557616576175761857619576205762157622576235762457625576265762757628576295763057631576325763357634576355763657637576385763957640576415764257643576445764557646576475764857649576505765157652576535765457655576565765757658576595766057661576625766357664576655766657667576685766957670576715767257673576745767557676576775767857679576805768157682576835768457685576865768757688576895769057691576925769357694576955769657697576985769957700577015770257703577045770557706577075770857709577105771157712577135771457715577165771757718577195772057721577225772357724577255772657727577285772957730577315773257733577345773557736577375773857739577405774157742577435774457745577465774757748577495775057751577525775357754577555775657757577585775957760577615776257763577645776557766577675776857769577705777157772577735777457775577765777757778577795778057781577825778357784577855778657787577885778957790577915779257793577945779557796577975779857799578005780157802578035780457805578065780757808578095781057811578125781357814578155781657817578185781957820578215782257823578245782557826578275782857829578305783157832578335783457835578365783757838578395784057841578425784357844578455784657847578485784957850578515785257853578545785557856578575785857859578605786157862578635786457865578665786757868578695787057871578725787357874578755787657877578785787957880578815788257883578845788557886578875788857889578905789157892578935789457895578965789757898578995790057901579025790357904579055790657907579085790957910579115791257913579145791557916579175791857919579205792157922579235792457925579265792757928579295793057931579325793357934579355793657937579385793957940579415794257943579445794557946579475794857949579505795157952579535795457955579565795757958579595796057961579625796357964579655796657967579685796957970579715797257973579745797557976579775797857979579805798157982579835798457985579865798757988579895799057991579925799357994579955799657997579985799958000580015800258003580045800558006580075800858009580105801158012580135801458015580165801758018580195802058021580225802358024580255802658027580285802958030580315803258033580345803558036580375803858039580405804158042580435804458045580465804758048580495805058051580525805358054580555805658057580585805958060580615806258063580645806558066580675806858069580705807158072580735807458075580765807758078580795808058081580825808358084580855808658087580885808958090580915809258093580945809558096580975809858099581005810158102581035810458105581065810758108581095811058111581125811358114581155811658117581185811958120581215812258123581245812558126581275812858129581305813158132581335813458135581365813758138581395814058141581425814358144581455814658147581485814958150581515815258153581545815558156581575815858159581605816158162581635816458165581665816758168581695817058171581725817358174581755817658177581785817958180581815818258183581845818558186581875818858189581905819158192581935819458195581965819758198581995820058201582025820358204582055820658207582085820958210582115821258213582145821558216582175821858219582205822158222582235822458225582265822758228582295823058231582325823358234582355823658237582385823958240582415824258243582445824558246582475824858249582505825158252582535825458255582565825758258582595826058261582625826358264582655826658267582685826958270582715827258273582745827558276582775827858279582805828158282582835828458285582865828758288582895829058291582925829358294582955829658297582985829958300583015830258303583045830558306583075830858309583105831158312583135831458315583165831758318583195832058321583225832358324583255832658327583285832958330583315833258333583345833558336583375833858339583405834158342583435834458345583465834758348583495835058351583525835358354583555835658357583585835958360583615836258363583645836558366583675836858369583705837158372583735837458375583765837758378583795838058381583825838358384583855838658387583885838958390583915839258393583945839558396583975839858399584005840158402584035840458405584065840758408584095841058411584125841358414584155841658417584185841958420584215842258423584245842558426584275842858429584305843158432584335843458435584365843758438584395844058441584425844358444584455844658447584485844958450584515845258453584545845558456584575845858459584605846158462584635846458465584665846758468584695847058471584725847358474584755847658477584785847958480584815848258483584845848558486584875848858489584905849158492584935849458495584965849758498584995850058501585025850358504585055850658507585085850958510585115851258513585145851558516585175851858519585205852158522585235852458525585265852758528585295853058531585325853358534585355853658537585385853958540585415854258543585445854558546585475854858549585505855158552585535855458555585565855758558585595856058561585625856358564585655856658567585685856958570585715857258573585745857558576585775857858579585805858158582585835858458585585865858758588585895859058591585925859358594585955859658597585985859958600586015860258603586045860558606586075860858609586105861158612586135861458615586165861758618586195862058621586225862358624586255862658627586285862958630586315863258633586345863558636586375863858639586405864158642586435864458645586465864758648586495865058651586525865358654586555865658657586585865958660586615866258663586645866558666586675866858669586705867158672586735867458675586765867758678586795868058681586825868358684586855868658687586885868958690586915869258693586945869558696586975869858699587005870158702587035870458705587065870758708587095871058711587125871358714587155871658717587185871958720587215872258723587245872558726587275872858729587305873158732587335873458735587365873758738587395874058741587425874358744587455874658747587485874958750587515875258753587545875558756587575875858759587605876158762587635876458765587665876758768587695877058771587725877358774587755877658777587785877958780587815878258783587845878558786587875878858789587905879158792587935879458795587965879758798587995880058801588025880358804588055880658807588085880958810588115881258813588145881558816588175881858819588205882158822588235882458825588265882758828588295883058831588325883358834588355883658837588385883958840588415884258843588445884558846588475884858849588505885158852588535885458855588565885758858588595886058861588625886358864588655886658867588685886958870588715887258873588745887558876588775887858879588805888158882588835888458885588865888758888588895889058891588925889358894588955889658897588985889958900589015890258903589045890558906589075890858909589105891158912589135891458915589165891758918589195892058921589225892358924589255892658927589285892958930589315893258933589345893558936589375893858939589405894158942589435894458945589465894758948589495895058951589525895358954589555895658957589585895958960589615896258963589645896558966589675896858969589705897158972589735897458975589765897758978589795898058981589825898358984589855898658987589885898958990589915899258993589945899558996589975899858999590005900159002590035900459005590065900759008590095901059011590125901359014590155901659017590185901959020590215902259023590245902559026590275902859029590305903159032590335903459035590365903759038590395904059041590425904359044590455904659047590485904959050590515905259053590545905559056590575905859059590605906159062590635906459065590665906759068590695907059071590725907359074590755907659077590785907959080590815908259083590845908559086590875908859089590905909159092590935909459095590965909759098590995910059101591025910359104591055910659107591085910959110591115911259113591145911559116591175911859119591205912159122591235912459125591265912759128591295913059131591325913359134591355913659137591385913959140591415914259143591445914559146591475914859149591505915159152591535915459155591565915759158591595916059161591625916359164591655916659167591685916959170591715917259173591745917559176591775917859179591805918159182591835918459185591865918759188591895919059191591925919359194591955919659197591985919959200592015920259203592045920559206592075920859209592105921159212592135921459215592165921759218592195922059221592225922359224592255922659227592285922959230592315923259233592345923559236592375923859239592405924159242592435924459245592465924759248592495925059251592525925359254592555925659257592585925959260592615926259263592645926559266592675926859269592705927159272592735927459275592765927759278592795928059281592825928359284592855928659287592885928959290592915929259293592945929559296592975929859299593005930159302593035930459305593065930759308593095931059311593125931359314593155931659317593185931959320593215932259323593245932559326593275932859329593305933159332593335933459335593365933759338593395934059341593425934359344593455934659347593485934959350593515935259353593545935559356593575935859359593605936159362593635936459365593665936759368593695937059371593725937359374593755937659377593785937959380593815938259383593845938559386593875938859389593905939159392593935939459395593965939759398593995940059401594025940359404594055940659407594085940959410594115941259413594145941559416594175941859419594205942159422594235942459425594265942759428594295943059431594325943359434594355943659437594385943959440594415944259443594445944559446594475944859449594505945159452594535945459455594565945759458594595946059461594625946359464594655946659467594685946959470594715947259473594745947559476594775947859479594805948159482594835948459485594865948759488594895949059491594925949359494594955949659497594985949959500595015950259503595045950559506595075950859509595105951159512595135951459515595165951759518595195952059521595225952359524595255952659527595285952959530595315953259533595345953559536595375953859539595405954159542595435954459545595465954759548595495955059551595525955359554595555955659557595585955959560595615956259563595645956559566595675956859569595705957159572595735957459575595765957759578595795958059581595825958359584595855958659587595885958959590595915959259593595945959559596595975959859599596005960159602596035960459605596065960759608596095961059611596125961359614596155961659617596185961959620596215962259623596245962559626596275962859629596305963159632596335963459635596365963759638596395964059641596425964359644596455964659647596485964959650596515965259653596545965559656596575965859659596605966159662596635966459665596665966759668596695967059671596725967359674596755967659677596785967959680596815968259683596845968559686596875968859689596905969159692596935969459695596965969759698596995970059701597025970359704597055970659707597085970959710597115971259713597145971559716597175971859719597205972159722597235972459725597265972759728597295973059731597325973359734597355973659737597385973959740597415974259743597445974559746597475974859749597505975159752597535975459755597565975759758597595976059761597625976359764597655976659767597685976959770597715977259773597745977559776597775977859779597805978159782597835978459785597865978759788597895979059791597925979359794597955979659797597985979959800598015980259803598045980559806598075980859809598105981159812598135981459815598165981759818598195982059821598225982359824598255982659827598285982959830598315983259833598345983559836598375983859839598405984159842598435984459845598465984759848598495985059851598525985359854598555985659857598585985959860598615986259863598645986559866598675986859869598705987159872598735987459875598765987759878598795988059881598825988359884598855988659887598885988959890598915989259893598945989559896598975989859899599005990159902599035990459905599065990759908599095991059911599125991359914599155991659917599185991959920599215992259923599245992559926599275992859929599305993159932599335993459935599365993759938599395994059941599425994359944599455994659947599485994959950599515995259953599545995559956599575995859959599605996159962599635996459965599665996759968599695997059971599725997359974599755997659977599785997959980599815998259983599845998559986599875998859989599905999159992599935999459995599965999759998599996000060001600026000360004600056000660007600086000960010600116001260013600146001560016600176001860019600206002160022600236002460025600266002760028600296003060031600326003360034600356003660037600386003960040600416004260043600446004560046600476004860049600506005160052600536005460055600566005760058600596006060061600626006360064600656006660067600686006960070600716007260073600746007560076600776007860079600806008160082600836008460085600866008760088600896009060091600926009360094600956009660097600986009960100601016010260103601046010560106601076010860109601106011160112601136011460115601166011760118601196012060121601226012360124601256012660127601286012960130601316013260133601346013560136601376013860139601406014160142601436014460145601466014760148601496015060151601526015360154601556015660157601586015960160601616016260163601646016560166601676016860169601706017160172601736017460175601766017760178601796018060181601826018360184601856018660187601886018960190601916019260193601946019560196601976019860199602006020160202602036020460205602066020760208602096021060211602126021360214602156021660217602186021960220602216022260223602246022560226602276022860229602306023160232602336023460235602366023760238602396024060241602426024360244602456024660247602486024960250602516025260253602546025560256602576025860259602606026160262602636026460265602666026760268602696027060271602726027360274602756027660277602786027960280602816028260283602846028560286602876028860289602906029160292602936029460295602966029760298602996030060301603026030360304603056030660307603086030960310603116031260313603146031560316603176031860319603206032160322603236032460325603266032760328603296033060331603326033360334603356033660337603386033960340603416034260343603446034560346603476034860349603506035160352603536035460355603566035760358603596036060361603626036360364603656036660367603686036960370603716037260373603746037560376603776037860379603806038160382603836038460385603866038760388603896039060391603926039360394603956039660397603986039960400604016040260403604046040560406604076040860409604106041160412604136041460415604166041760418604196042060421604226042360424604256042660427604286042960430604316043260433604346043560436604376043860439604406044160442604436044460445604466044760448604496045060451604526045360454604556045660457604586045960460604616046260463604646046560466604676046860469604706047160472604736047460475604766047760478604796048060481604826048360484604856048660487604886048960490604916049260493604946049560496604976049860499605006050160502605036050460505605066050760508605096051060511605126051360514605156051660517605186051960520605216052260523605246052560526605276052860529605306053160532605336053460535605366053760538605396054060541605426054360544605456054660547605486054960550605516055260553605546055560556605576055860559605606056160562605636056460565605666056760568605696057060571605726057360574605756057660577605786057960580605816058260583605846058560586605876058860589605906059160592605936059460595605966059760598605996060060601606026060360604606056060660607606086060960610606116061260613606146061560616606176061860619606206062160622606236062460625606266062760628606296063060631606326063360634606356063660637606386063960640606416064260643606446064560646606476064860649606506065160652606536065460655606566065760658606596066060661606626066360664606656066660667606686066960670606716067260673606746067560676606776067860679606806068160682606836068460685606866068760688606896069060691606926069360694606956069660697606986069960700607016070260703607046070560706607076070860709607106071160712607136071460715607166071760718607196072060721607226072360724607256072660727607286072960730607316073260733607346073560736607376073860739607406074160742607436074460745607466074760748607496075060751607526075360754607556075660757607586075960760607616076260763607646076560766607676076860769607706077160772607736077460775607766077760778607796078060781607826078360784607856078660787607886078960790607916079260793607946079560796607976079860799608006080160802608036080460805608066080760808608096081060811608126081360814608156081660817608186081960820608216082260823608246082560826608276082860829608306083160832608336083460835608366083760838608396084060841608426084360844608456084660847608486084960850608516085260853608546085560856608576085860859608606086160862608636086460865608666086760868608696087060871608726087360874608756087660877608786087960880608816088260883608846088560886608876088860889608906089160892608936089460895608966089760898608996090060901609026090360904609056090660907609086090960910609116091260913609146091560916609176091860919609206092160922609236092460925609266092760928609296093060931609326093360934609356093660937609386093960940609416094260943609446094560946609476094860949609506095160952609536095460955609566095760958609596096060961609626096360964609656096660967609686096960970609716097260973609746097560976609776097860979609806098160982609836098460985609866098760988609896099060991609926099360994609956099660997609986099961000610016100261003610046100561006610076100861009610106101161012610136101461015610166101761018610196102061021610226102361024610256102661027610286102961030610316103261033610346103561036610376103861039610406104161042610436104461045610466104761048610496105061051610526105361054610556105661057610586105961060610616106261063610646106561066610676106861069610706107161072610736107461075610766107761078610796108061081610826108361084610856108661087610886108961090610916109261093610946109561096610976109861099611006110161102611036110461105611066110761108611096111061111611126111361114611156111661117611186111961120611216112261123611246112561126611276112861129611306113161132611336113461135611366113761138611396114061141611426114361144611456114661147611486114961150611516115261153611546115561156611576115861159611606116161162611636116461165611666116761168611696117061171611726117361174611756117661177611786117961180611816118261183611846118561186611876118861189611906119161192611936119461195611966119761198611996120061201612026120361204612056120661207612086120961210612116121261213612146121561216612176121861219612206122161222612236122461225612266122761228612296123061231612326123361234612356123661237612386123961240612416124261243612446124561246612476124861249612506125161252612536125461255612566125761258612596126061261612626126361264612656126661267612686126961270612716127261273612746127561276612776127861279612806128161282612836128461285612866128761288612896129061291612926129361294612956129661297612986129961300613016130261303613046130561306613076130861309613106131161312613136131461315613166131761318613196132061321613226132361324613256132661327613286132961330613316133261333613346133561336613376133861339613406134161342613436134461345613466134761348613496135061351613526135361354613556135661357613586135961360613616136261363613646136561366613676136861369613706137161372613736137461375613766137761378613796138061381613826138361384613856138661387613886138961390613916139261393613946139561396613976139861399614006140161402614036140461405614066140761408614096141061411614126141361414614156141661417614186141961420614216142261423614246142561426614276142861429614306143161432614336143461435614366143761438614396144061441614426144361444614456144661447614486144961450614516145261453614546145561456614576145861459614606146161462614636146461465614666146761468614696147061471614726147361474614756147661477614786147961480614816148261483614846148561486614876148861489614906149161492614936149461495614966149761498614996150061501615026150361504615056150661507615086150961510615116151261513615146151561516615176151861519615206152161522615236152461525615266152761528615296153061531615326153361534615356153661537615386153961540615416154261543615446154561546615476154861549615506155161552615536155461555615566155761558615596156061561615626156361564615656156661567615686156961570615716157261573615746157561576615776157861579615806158161582615836158461585615866158761588615896159061591615926159361594615956159661597615986159961600616016160261603616046160561606616076160861609616106161161612616136161461615616166161761618616196162061621616226162361624616256162661627616286162961630616316163261633616346163561636616376163861639616406164161642616436164461645616466164761648616496165061651616526165361654616556165661657616586165961660616616166261663616646166561666616676166861669616706167161672616736167461675616766167761678616796168061681616826168361684616856168661687616886168961690616916169261693616946169561696616976169861699617006170161702617036170461705617066170761708617096171061711617126171361714617156171661717617186171961720617216172261723617246172561726617276172861729617306173161732617336173461735617366173761738617396174061741617426174361744617456174661747617486174961750617516175261753617546175561756617576175861759617606176161762617636176461765617666176761768617696177061771617726177361774617756177661777617786177961780617816178261783617846178561786617876178861789617906179161792617936179461795617966179761798617996180061801618026180361804618056180661807618086180961810618116181261813618146181561816618176181861819618206182161822618236182461825618266182761828618296183061831618326183361834618356183661837618386183961840618416184261843618446184561846618476184861849618506185161852618536185461855618566185761858618596186061861618626186361864618656186661867618686186961870618716187261873618746187561876618776187861879618806188161882618836188461885618866188761888618896189061891618926189361894618956189661897618986189961900619016190261903619046190561906619076190861909619106191161912619136191461915619166191761918619196192061921619226192361924619256192661927619286192961930619316193261933619346193561936619376193861939619406194161942619436194461945619466194761948619496195061951619526195361954619556195661957619586195961960619616196261963619646196561966619676196861969619706197161972619736197461975619766197761978619796198061981619826198361984619856198661987619886198961990619916199261993619946199561996619976199861999620006200162002620036200462005620066200762008620096201062011620126201362014620156201662017620186201962020620216202262023620246202562026620276202862029620306203162032620336203462035620366203762038620396204062041620426204362044620456204662047620486204962050620516205262053620546205562056620576205862059620606206162062620636206462065620666206762068620696207062071620726207362074620756207662077620786207962080620816208262083620846208562086620876208862089620906209162092620936209462095620966209762098620996210062101621026210362104621056210662107621086210962110621116211262113621146211562116621176211862119621206212162122621236212462125621266212762128621296213062131621326213362134621356213662137621386213962140621416214262143621446214562146621476214862149621506215162152621536215462155621566215762158621596216062161621626216362164621656216662167621686216962170621716217262173621746217562176621776217862179621806218162182621836218462185621866218762188621896219062191621926219362194621956219662197621986219962200622016220262203622046220562206622076220862209622106221162212622136221462215622166221762218622196222062221622226222362224622256222662227622286222962230622316223262233622346223562236622376223862239622406224162242622436224462245622466224762248622496225062251622526225362254622556225662257622586225962260622616226262263622646226562266622676226862269622706227162272622736227462275622766227762278622796228062281622826228362284622856228662287622886228962290622916229262293622946229562296622976229862299623006230162302623036230462305623066230762308623096231062311623126231362314623156231662317623186231962320623216232262323623246232562326623276232862329623306233162332623336233462335623366233762338623396234062341623426234362344623456234662347623486234962350623516235262353623546235562356623576235862359623606236162362623636236462365623666236762368623696237062371623726237362374623756237662377623786237962380623816238262383623846238562386623876238862389623906239162392623936239462395623966239762398623996240062401624026240362404624056240662407624086240962410624116241262413624146241562416624176241862419624206242162422624236242462425624266242762428624296243062431624326243362434624356243662437624386243962440624416244262443624446244562446624476244862449624506245162452624536245462455624566245762458624596246062461624626246362464624656246662467624686246962470624716247262473624746247562476624776247862479624806248162482624836248462485624866248762488624896249062491624926249362494624956249662497624986249962500625016250262503625046250562506625076250862509625106251162512625136251462515625166251762518625196252062521625226252362524625256252662527625286252962530625316253262533625346253562536625376253862539625406254162542625436254462545625466254762548625496255062551625526255362554625556255662557625586255962560625616256262563625646256562566625676256862569625706257162572625736257462575625766257762578625796258062581625826258362584625856258662587625886258962590625916259262593625946259562596625976259862599626006260162602626036260462605626066260762608626096261062611626126261362614626156261662617626186261962620626216262262623626246262562626626276262862629626306263162632626336263462635626366263762638626396264062641626426264362644626456264662647626486264962650626516265262653626546265562656626576265862659626606266162662626636266462665626666266762668626696267062671626726267362674626756267662677626786267962680626816268262683626846268562686626876268862689626906269162692626936269462695626966269762698626996270062701627026270362704627056270662707627086270962710627116271262713627146271562716627176271862719627206272162722627236272462725627266272762728627296273062731627326273362734627356273662737627386273962740627416274262743627446274562746627476274862749627506275162752627536275462755627566275762758627596276062761627626276362764627656276662767627686276962770627716277262773627746277562776627776277862779627806278162782627836278462785627866278762788627896279062791627926279362794627956279662797627986279962800628016280262803628046280562806628076280862809628106281162812628136281462815628166281762818628196282062821628226282362824628256282662827628286282962830628316283262833628346283562836628376283862839628406284162842628436284462845628466284762848628496285062851628526285362854628556285662857628586285962860628616286262863628646286562866628676286862869628706287162872628736287462875628766287762878628796288062881628826288362884628856288662887628886288962890628916289262893628946289562896628976289862899629006290162902629036290462905629066290762908629096291062911629126291362914629156291662917629186291962920629216292262923629246292562926629276292862929629306293162932629336293462935629366293762938629396294062941629426294362944629456294662947629486294962950629516295262953629546295562956629576295862959629606296162962629636296462965629666296762968629696297062971629726297362974629756297662977629786297962980629816298262983629846298562986629876298862989629906299162992629936299462995629966299762998629996300063001630026300363004630056300663007630086300963010630116301263013630146301563016630176301863019630206302163022630236302463025630266302763028630296303063031630326303363034630356303663037630386303963040630416304263043630446304563046630476304863049630506305163052630536305463055630566305763058630596306063061630626306363064630656306663067630686306963070630716307263073630746307563076630776307863079630806308163082630836308463085630866308763088630896309063091630926309363094630956309663097630986309963100631016310263103631046310563106631076310863109631106311163112631136311463115631166311763118631196312063121631226312363124631256312663127631286312963130631316313263133631346313563136631376313863139631406314163142631436314463145631466314763148631496315063151631526315363154631556315663157631586315963160631616316263163631646316563166631676316863169631706317163172631736317463175631766317763178631796318063181631826318363184631856318663187631886318963190631916319263193631946319563196631976319863199632006320163202632036320463205632066320763208632096321063211632126321363214632156321663217632186321963220632216322263223632246322563226632276322863229632306323163232632336323463235632366323763238632396324063241632426324363244632456324663247632486324963250632516325263253632546325563256632576325863259632606326163262632636326463265632666326763268632696327063271632726327363274632756327663277632786327963280632816328263283632846328563286632876328863289632906329163292632936329463295632966329763298632996330063301633026330363304633056330663307633086330963310633116331263313633146331563316633176331863319633206332163322633236332463325633266332763328633296333063331633326333363334633356333663337633386333963340633416334263343633446334563346633476334863349633506335163352633536335463355633566335763358633596336063361633626336363364633656336663367633686336963370633716337263373633746337563376633776337863379633806338163382633836338463385633866338763388633896339063391633926339363394633956339663397633986339963400634016340263403634046340563406634076340863409634106341163412634136341463415634166341763418634196342063421634226342363424634256342663427634286342963430634316343263433634346343563436634376343863439634406344163442634436344463445634466344763448634496345063451634526345363454634556345663457634586345963460634616346263463634646346563466634676346863469634706347163472634736347463475634766347763478634796348063481634826348363484634856348663487634886348963490634916349263493634946349563496634976349863499635006350163502635036350463505635066350763508635096351063511635126351363514635156351663517635186351963520635216352263523635246352563526635276352863529635306353163532635336353463535635366353763538635396354063541635426354363544635456354663547635486354963550635516355263553635546355563556635576355863559635606356163562635636356463565635666356763568635696357063571635726357363574635756357663577635786357963580635816358263583635846358563586635876358863589635906359163592635936359463595635966359763598635996360063601636026360363604636056360663607636086360963610636116361263613636146361563616636176361863619636206362163622636236362463625636266362763628636296363063631636326363363634636356363663637636386363963640636416364263643636446364563646636476364863649636506365163652636536365463655636566365763658636596366063661636626366363664636656366663667636686366963670636716367263673636746367563676636776367863679636806368163682636836368463685636866368763688636896369063691636926369363694636956369663697636986369963700637016370263703637046370563706637076370863709637106371163712637136371463715637166371763718637196372063721637226372363724637256372663727637286372963730637316373263733637346373563736637376373863739637406374163742637436374463745637466374763748637496375063751637526375363754637556375663757637586375963760637616376263763637646376563766637676376863769637706377163772637736377463775637766377763778637796378063781637826378363784637856378663787637886378963790637916379263793637946379563796637976379863799638006380163802638036380463805638066380763808638096381063811638126381363814638156381663817638186381963820638216382263823638246382563826638276382863829638306383163832638336383463835638366383763838638396384063841638426384363844638456384663847638486384963850638516385263853638546385563856638576385863859638606386163862638636386463865638666386763868638696387063871638726387363874638756387663877638786387963880638816388263883638846388563886638876388863889638906389163892638936389463895638966389763898638996390063901639026390363904639056390663907639086390963910639116391263913639146391563916639176391863919639206392163922639236392463925639266392763928639296393063931639326393363934639356393663937639386393963940639416394263943639446394563946639476394863949639506395163952639536395463955639566395763958639596396063961639626396363964639656396663967639686396963970639716397263973639746397563976639776397863979639806398163982639836398463985639866398763988639896399063991639926399363994639956399663997639986399964000640016400264003640046400564006640076400864009640106401164012640136401464015640166401764018640196402064021640226402364024640256402664027640286402964030640316403264033640346403564036640376403864039640406404164042640436404464045640466404764048640496405064051640526405364054640556405664057640586405964060640616406264063640646406564066640676406864069640706407164072640736407464075640766407764078640796408064081640826408364084640856408664087640886408964090640916409264093640946409564096640976409864099641006410164102641036410464105641066410764108641096411064111641126411364114641156411664117641186411964120641216412264123641246412564126641276412864129641306413164132641336413464135641366413764138641396414064141641426414364144641456414664147641486414964150641516415264153641546415564156641576415864159641606416164162641636416464165641666416764168641696417064171641726417364174641756417664177641786417964180641816418264183641846418564186641876418864189641906419164192641936419464195641966419764198641996420064201642026420364204642056420664207642086420964210642116421264213642146421564216642176421864219642206422164222642236422464225642266422764228642296423064231642326423364234642356423664237642386423964240642416424264243642446424564246642476424864249642506425164252642536425464255642566425764258642596426064261642626426364264642656426664267642686426964270642716427264273642746427564276642776427864279642806428164282642836428464285642866428764288642896429064291642926429364294642956429664297642986429964300643016430264303643046430564306643076430864309643106431164312643136431464315643166431764318643196432064321643226432364324643256432664327643286432964330643316433264333643346433564336643376433864339643406434164342643436434464345643466434764348643496435064351643526435364354643556435664357643586435964360643616436264363643646436564366643676436864369643706437164372643736437464375643766437764378643796438064381643826438364384643856438664387643886438964390643916439264393643946439564396643976439864399644006440164402644036440464405644066440764408644096441064411644126441364414644156441664417644186441964420644216442264423644246442564426644276442864429644306443164432644336443464435644366443764438644396444064441644426444364444644456444664447644486444964450644516445264453644546445564456644576445864459644606446164462644636446464465644666446764468644696447064471644726447364474644756447664477644786447964480644816448264483644846448564486644876448864489644906449164492644936449464495644966449764498644996450064501645026450364504645056450664507645086450964510645116451264513645146451564516645176451864519645206452164522645236452464525645266452764528645296453064531645326453364534645356453664537645386453964540645416454264543645446454564546645476454864549645506455164552645536455464555645566455764558645596456064561645626456364564645656456664567645686456964570645716457264573645746457564576645776457864579645806458164582645836458464585645866458764588645896459064591645926459364594645956459664597645986459964600646016460264603646046460564606646076460864609646106461164612646136461464615646166461764618646196462064621646226462364624646256462664627646286462964630646316463264633646346463564636646376463864639646406464164642646436464464645646466464764648646496465064651646526465364654646556465664657646586465964660646616466264663646646466564666646676466864669646706467164672646736467464675646766467764678646796468064681646826468364684646856468664687646886468964690646916469264693646946469564696646976469864699647006470164702647036470464705647066470764708647096471064711647126471364714647156471664717647186471964720647216472264723647246472564726647276472864729647306473164732647336473464735647366473764738647396474064741647426474364744647456474664747647486474964750647516475264753647546475564756647576475864759647606476164762647636476464765647666476764768647696477064771647726477364774647756477664777647786477964780647816478264783647846478564786647876478864789647906479164792647936479464795647966479764798647996480064801648026480364804648056480664807648086480964810648116481264813648146481564816648176481864819648206482164822648236482464825648266482764828648296483064831648326483364834648356483664837648386483964840648416484264843648446484564846648476484864849648506485164852648536485464855648566485764858648596486064861648626486364864648656486664867648686486964870648716487264873648746487564876648776487864879648806488164882648836488464885648866488764888648896489064891648926489364894648956489664897648986489964900649016490264903649046490564906649076490864909649106491164912649136491464915649166491764918649196492064921649226492364924649256492664927649286492964930649316493264933649346493564936649376493864939649406494164942649436494464945649466494764948649496495064951649526495364954649556495664957649586495964960649616496264963649646496564966649676496864969649706497164972649736497464975649766497764978649796498064981649826498364984649856498664987649886498964990649916499264993649946499564996649976499864999650006500165002650036500465005650066500765008650096501065011650126501365014650156501665017650186501965020650216502265023650246502565026650276502865029650306503165032650336503465035650366503765038650396504065041650426504365044650456504665047650486504965050650516505265053650546505565056650576505865059650606506165062650636506465065650666506765068650696507065071650726507365074650756507665077650786507965080650816508265083650846508565086650876508865089650906509165092650936509465095650966509765098650996510065101651026510365104651056510665107651086510965110651116511265113651146511565116651176511865119651206512165122651236512465125651266512765128651296513065131651326513365134651356513665137651386513965140651416514265143651446514565146651476514865149651506515165152651536515465155651566515765158651596516065161651626516365164651656516665167651686516965170651716517265173651746517565176651776517865179651806518165182651836518465185651866518765188651896519065191651926519365194651956519665197651986519965200652016520265203652046520565206652076520865209652106521165212652136521465215652166521765218652196522065221652226522365224652256522665227652286522965230652316523265233652346523565236652376523865239652406524165242652436524465245652466524765248652496525065251652526525365254652556525665257652586525965260652616526265263652646526565266652676526865269652706527165272652736527465275652766527765278652796528065281652826528365284652856528665287652886528965290652916529265293652946529565296652976529865299653006530165302653036530465305653066530765308653096531065311653126531365314653156531665317653186531965320653216532265323653246532565326653276532865329653306533165332653336533465335653366533765338653396534065341653426534365344653456534665347653486534965350653516535265353653546535565356653576535865359653606536165362653636536465365653666536765368653696537065371653726537365374653756537665377653786537965380653816538265383653846538565386653876538865389653906539165392653936539465395653966539765398653996540065401654026540365404654056540665407654086540965410654116541265413654146541565416654176541865419654206542165422654236542465425654266542765428654296543065431654326543365434654356543665437654386543965440654416544265443654446544565446654476544865449654506545165452654536545465455654566545765458654596546065461654626546365464654656546665467654686546965470654716547265473654746547565476654776547865479654806548165482654836548465485654866548765488654896549065491654926549365494654956549665497654986549965500655016550265503655046550565506655076550865509655106551165512655136551465515655166551765518655196552065521655226552365524655256552665527655286552965530655316553265533655346553565536655376553865539655406554165542655436554465545655466554765548655496555065551655526555365554655556555665557655586555965560655616556265563655646556565566655676556865569655706557165572655736557465575655766557765578655796558065581655826558365584655856558665587655886558965590655916559265593655946559565596655976559865599656006560165602656036560465605656066560765608656096561065611656126561365614656156561665617656186561965620656216562265623656246562565626656276562865629656306563165632656336563465635656366563765638656396564065641656426564365644656456564665647656486564965650656516565265653656546565565656656576565865659656606566165662656636566465665656666566765668656696567065671656726567365674656756567665677656786567965680656816568265683656846568565686656876568865689656906569165692656936569465695656966569765698656996570065701657026570365704657056570665707657086570965710657116571265713657146571565716657176571865719657206572165722657236572465725657266572765728657296573065731657326573365734657356573665737657386573965740657416574265743657446574565746657476574865749657506575165752657536575465755657566575765758657596576065761657626576365764657656576665767657686576965770657716577265773657746577565776657776577865779657806578165782657836578465785657866578765788657896579065791657926579365794657956579665797657986579965800658016580265803658046580565806658076580865809658106581165812658136581465815658166581765818658196582065821658226582365824658256582665827658286582965830658316583265833658346583565836658376583865839658406584165842658436584465845658466584765848658496585065851658526585365854658556585665857658586585965860658616586265863658646586565866658676586865869658706587165872658736587465875658766587765878658796588065881658826588365884658856588665887658886588965890658916589265893658946589565896658976589865899659006590165902659036590465905659066590765908659096591065911659126591365914659156591665917659186591965920659216592265923659246592565926659276592865929659306593165932659336593465935659366593765938659396594065941659426594365944659456594665947659486594965950659516595265953659546595565956659576595865959659606596165962659636596465965659666596765968659696597065971659726597365974659756597665977659786597965980659816598265983659846598565986659876598865989659906599165992659936599465995659966599765998659996600066001660026600366004660056600666007660086600966010660116601266013660146601566016660176601866019660206602166022660236602466025660266602766028660296603066031660326603366034660356603666037660386603966040660416604266043660446604566046660476604866049660506605166052660536605466055660566605766058660596606066061660626606366064660656606666067660686606966070660716607266073660746607566076660776607866079660806608166082660836608466085660866608766088660896609066091660926609366094660956609666097660986609966100661016610266103661046610566106661076610866109661106611166112661136611466115661166611766118661196612066121661226612366124661256612666127661286612966130661316613266133661346613566136661376613866139661406614166142661436614466145661466614766148661496615066151661526615366154661556615666157661586615966160661616616266163661646616566166661676616866169661706617166172661736617466175661766617766178661796618066181661826618366184661856618666187661886618966190661916619266193661946619566196661976619866199662006620166202662036620466205662066620766208662096621066211662126621366214662156621666217662186621966220662216622266223662246622566226662276622866229662306623166232662336623466235662366623766238662396624066241662426624366244662456624666247662486624966250662516625266253662546625566256662576625866259662606626166262662636626466265662666626766268662696627066271662726627366274662756627666277662786627966280662816628266283662846628566286662876628866289662906629166292662936629466295662966629766298662996630066301663026630366304663056630666307663086630966310663116631266313663146631566316663176631866319663206632166322663236632466325663266632766328663296633066331663326633366334663356633666337663386633966340663416634266343663446634566346663476634866349663506635166352663536635466355663566635766358663596636066361663626636366364663656636666367663686636966370663716637266373663746637566376663776637866379663806638166382663836638466385663866638766388663896639066391663926639366394663956639666397663986639966400664016640266403664046640566406664076640866409664106641166412664136641466415664166641766418664196642066421664226642366424664256642666427664286642966430664316643266433664346643566436664376643866439664406644166442664436644466445664466644766448664496645066451664526645366454664556645666457664586645966460664616646266463664646646566466664676646866469664706647166472664736647466475664766647766478664796648066481664826648366484664856648666487664886648966490664916649266493664946649566496664976649866499665006650166502665036650466505665066650766508665096651066511665126651366514665156651666517665186651966520665216652266523665246652566526665276652866529665306653166532665336653466535665366653766538665396654066541665426654366544665456654666547665486654966550665516655266553665546655566556665576655866559665606656166562665636656466565665666656766568665696657066571665726657366574665756657666577665786657966580665816658266583665846658566586665876658866589665906659166592665936659466595665966659766598665996660066601666026660366604666056660666607666086660966610666116661266613666146661566616666176661866619666206662166622666236662466625666266662766628666296663066631666326663366634666356663666637666386663966640666416664266643666446664566646666476664866649666506665166652666536665466655666566665766658666596666066661666626666366664666656666666667666686666966670666716667266673666746667566676666776667866679666806668166682666836668466685666866668766688666896669066691666926669366694666956669666697666986669966700667016670266703667046670566706667076670866709667106671166712667136671466715667166671766718667196672066721667226672366724667256672666727667286672966730667316673266733667346673566736667376673866739667406674166742667436674466745667466674766748667496675066751667526675366754667556675666757667586675966760667616676266763667646676566766667676676866769667706677166772667736677466775667766677766778667796678066781667826678366784667856678666787667886678966790667916679266793667946679566796667976679866799668006680166802668036680466805668066680766808668096681066811668126681366814668156681666817668186681966820668216682266823668246682566826668276682866829668306683166832668336683466835668366683766838668396684066841668426684366844668456684666847668486684966850668516685266853668546685566856668576685866859668606686166862668636686466865668666686766868668696687066871668726687366874668756687666877668786687966880668816688266883668846688566886668876688866889668906689166892668936689466895668966689766898668996690066901669026690366904669056690666907669086690966910669116691266913669146691566916669176691866919669206692166922669236692466925669266692766928669296693066931669326693366934669356693666937669386693966940669416694266943669446694566946669476694866949669506695166952669536695466955669566695766958669596696066961669626696366964669656696666967669686696966970669716697266973669746697566976669776697866979669806698166982669836698466985669866698766988669896699066991669926699366994669956699666997669986699967000670016700267003670046700567006670076700867009670106701167012670136701467015670166701767018670196702067021670226702367024670256702667027670286702967030670316703267033670346703567036670376703867039670406704167042670436704467045670466704767048
  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 crypto$2, { createHash as createHash$2 } from 'node:crypto';
  10. import require$$0$3 from 'tty';
  11. 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';
  12. import esbuild, { transform as transform$1, formatMessages, build as build$3 } from 'esbuild';
  13. 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, defaultAllowedOrigins, DEFAULT_DEV_PORT, CLIENT_DIR, VERSION, DEFAULT_PREVIEW_PORT, DEFAULT_ASSETS_RE, DEFAULT_CONFIG_FILES } from '../constants.js';
  14. import * as require$$0$2 from 'fs';
  15. import require$$0__default, { lstatSync, readdir as readdir$4, readdirSync, readlinkSync, realpathSync as realpathSync$1, existsSync, readFileSync, statSync as statSync$1 } from 'fs';
  16. import { EventEmitter as EventEmitter$4 } from 'node:events';
  17. import Stream$1 from 'node:stream';
  18. import { StringDecoder } from 'node:string_decoder';
  19. import { exec, execSync } from 'node:child_process';
  20. import { createServer as createServer$3, STATUS_CODES, get as get$2 } from 'node:http';
  21. import { createServer as createServer$2, get as get$1 } from 'node:https';
  22. import require$$0$5 from 'util';
  23. import require$$4$1 from 'net';
  24. import require$$0$7 from 'events';
  25. import require$$0$9 from 'url';
  26. import require$$1 from 'http';
  27. import require$$0$6 from 'stream';
  28. import require$$2 from 'os';
  29. import require$$2$1 from 'child_process';
  30. import os$5 from 'node:os';
  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 net$1 from 'node:net';
  46. import require$$4$3 from 'assert';
  47. import { gzip } from 'node:zlib';
  48. import { fileURLToPath as __cjs_fileURLToPath } from 'node:url';
  49. import { dirname as __cjs_dirname } from 'node:path';
  50. import { createRequire as __cjs_createRequire } from 'node:module';
  51. const __filename = __cjs_fileURLToPath(import.meta.url);
  52. const __dirname = __cjs_dirname(__filename);
  53. const require = __cjs_createRequire(import.meta.url);
  54. const __require = require;
  55. var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
  56. function getDefaultExportFromCjs (x) {
  57. return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
  58. }
  59. function getAugmentedNamespace(n) {
  60. if (n.__esModule) return n;
  61. var f = n.default;
  62. if (typeof f == "function") {
  63. var a = function a () {
  64. if (this instanceof a) {
  65. return Reflect.construct(f, arguments, this.constructor);
  66. }
  67. return f.apply(this, arguments);
  68. };
  69. a.prototype = f.prototype;
  70. } else a = {};
  71. Object.defineProperty(a, '__esModule', {value: true});
  72. Object.keys(n).forEach(function (k) {
  73. var d = Object.getOwnPropertyDescriptor(n, k);
  74. Object.defineProperty(a, k, d.get ? d : {
  75. enumerable: true,
  76. get: function () {
  77. return n[k];
  78. }
  79. });
  80. });
  81. return a;
  82. }
  83. function commonjsRequire(path) {
  84. 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.');
  85. }
  86. var picocolors = {exports: {}};
  87. let argv = process.argv || [],
  88. env$1 = process.env;
  89. let isColorSupported =
  90. !("NO_COLOR" in env$1 || argv.includes("--no-color")) &&
  91. ("FORCE_COLOR" in env$1 ||
  92. argv.includes("--color") ||
  93. process.platform === "win32" ||
  94. (commonjsRequire != null && require$$0$3.isatty(1) && env$1.TERM !== "dumb") ||
  95. "CI" in env$1);
  96. let formatter =
  97. (open, close, replace = open) =>
  98. input => {
  99. let string = "" + input;
  100. let index = string.indexOf(close, open.length);
  101. return ~index
  102. ? open + replaceClose(string, close, replace, index) + close
  103. : open + string + close
  104. };
  105. let replaceClose = (string, close, replace, index) => {
  106. let result = "";
  107. let cursor = 0;
  108. do {
  109. result += string.substring(cursor, index) + replace;
  110. cursor = index + close.length;
  111. index = string.indexOf(close, cursor);
  112. } while (~index)
  113. return result + string.substring(cursor)
  114. };
  115. let createColors = (enabled = isColorSupported) => {
  116. let init = enabled ? formatter : () => String;
  117. return {
  118. isColorSupported: enabled,
  119. reset: init("\x1b[0m", "\x1b[0m"),
  120. bold: init("\x1b[1m", "\x1b[22m", "\x1b[22m\x1b[1m"),
  121. dim: init("\x1b[2m", "\x1b[22m", "\x1b[22m\x1b[2m"),
  122. italic: init("\x1b[3m", "\x1b[23m"),
  123. underline: init("\x1b[4m", "\x1b[24m"),
  124. inverse: init("\x1b[7m", "\x1b[27m"),
  125. hidden: init("\x1b[8m", "\x1b[28m"),
  126. strikethrough: init("\x1b[9m", "\x1b[29m"),
  127. black: init("\x1b[30m", "\x1b[39m"),
  128. red: init("\x1b[31m", "\x1b[39m"),
  129. green: init("\x1b[32m", "\x1b[39m"),
  130. yellow: init("\x1b[33m", "\x1b[39m"),
  131. blue: init("\x1b[34m", "\x1b[39m"),
  132. magenta: init("\x1b[35m", "\x1b[39m"),
  133. cyan: init("\x1b[36m", "\x1b[39m"),
  134. white: init("\x1b[37m", "\x1b[39m"),
  135. gray: init("\x1b[90m", "\x1b[39m"),
  136. bgBlack: init("\x1b[40m", "\x1b[49m"),
  137. bgRed: init("\x1b[41m", "\x1b[49m"),
  138. bgGreen: init("\x1b[42m", "\x1b[49m"),
  139. bgYellow: init("\x1b[43m", "\x1b[49m"),
  140. bgBlue: init("\x1b[44m", "\x1b[49m"),
  141. bgMagenta: init("\x1b[45m", "\x1b[49m"),
  142. bgCyan: init("\x1b[46m", "\x1b[49m"),
  143. bgWhite: init("\x1b[47m", "\x1b[49m"),
  144. }
  145. };
  146. picocolors.exports = createColors();
  147. picocolors.exports.createColors = createColors;
  148. var picocolorsExports = picocolors.exports;
  149. var colors$1 = /*@__PURE__*/getDefaultExportFromCjs(picocolorsExports);
  150. function matches$1(pattern, importee) {
  151. if (pattern instanceof RegExp) {
  152. return pattern.test(importee);
  153. }
  154. if (importee.length < pattern.length) {
  155. return false;
  156. }
  157. if (importee === pattern) {
  158. return true;
  159. }
  160. // eslint-disable-next-line prefer-template
  161. return importee.startsWith(pattern + '/');
  162. }
  163. function getEntries({ entries, customResolver }) {
  164. if (!entries) {
  165. return [];
  166. }
  167. const resolverFunctionFromOptions = resolveCustomResolver(customResolver);
  168. if (Array.isArray(entries)) {
  169. return entries.map((entry) => {
  170. return {
  171. find: entry.find,
  172. replacement: entry.replacement,
  173. resolverFunction: resolveCustomResolver(entry.customResolver) || resolverFunctionFromOptions
  174. };
  175. });
  176. }
  177. return Object.entries(entries).map(([key, value]) => {
  178. return { find: key, replacement: value, resolverFunction: resolverFunctionFromOptions };
  179. });
  180. }
  181. function getHookFunction(hook) {
  182. if (typeof hook === 'function') {
  183. return hook;
  184. }
  185. if (hook && 'handler' in hook && typeof hook.handler === 'function') {
  186. return hook.handler;
  187. }
  188. return null;
  189. }
  190. function resolveCustomResolver(customResolver) {
  191. if (typeof customResolver === 'function') {
  192. return customResolver;
  193. }
  194. if (customResolver) {
  195. return getHookFunction(customResolver.resolveId);
  196. }
  197. return null;
  198. }
  199. function alias$1(options = {}) {
  200. const entries = getEntries(options);
  201. if (entries.length === 0) {
  202. return {
  203. name: 'alias',
  204. resolveId: () => null
  205. };
  206. }
  207. return {
  208. name: 'alias',
  209. async buildStart(inputOptions) {
  210. 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)); }));
  211. },
  212. resolveId(importee, importer, resolveOptions) {
  213. // First match is supposed to be the correct one
  214. const matchedEntry = entries.find((entry) => matches$1(entry.find, importee));
  215. if (!matchedEntry) {
  216. return null;
  217. }
  218. const updatedId = importee.replace(matchedEntry.find, matchedEntry.replacement);
  219. if (matchedEntry.resolverFunction) {
  220. return matchedEntry.resolverFunction.call(this, updatedId, importer, resolveOptions);
  221. }
  222. return this.resolve(updatedId, importer, Object.assign({ skipSelf: true }, resolveOptions)).then((resolved) => {
  223. if (resolved)
  224. return resolved;
  225. if (!require$$0$4.isAbsolute(updatedId)) {
  226. this.warn(`rewrote ${importee} to ${updatedId} but was not an abolute path and was not handled by other plugins. ` +
  227. `This will lead to duplicated modules for the same path. ` +
  228. `To avoid duplicating modules, you should resolve to an absolute path.`);
  229. }
  230. return { id: updatedId };
  231. });
  232. }
  233. };
  234. }
  235. const VALID_ID_PREFIX = `/@id/`;
  236. const NULL_BYTE_PLACEHOLDER = `__x00__`;
  237. let SOURCEMAPPING_URL = "sourceMa";
  238. SOURCEMAPPING_URL += "ppingURL";
  239. const VITE_RUNTIME_SOURCEMAPPING_SOURCE = "//# sourceMappingSource=vite-runtime";
  240. const isWindows$3 = typeof process !== "undefined" && process.platform === "win32";
  241. function wrapId$1(id) {
  242. return id.startsWith(VALID_ID_PREFIX) ? id : VALID_ID_PREFIX + id.replace("\0", NULL_BYTE_PLACEHOLDER);
  243. }
  244. function unwrapId$1(id) {
  245. return id.startsWith(VALID_ID_PREFIX) ? id.slice(VALID_ID_PREFIX.length).replace(NULL_BYTE_PLACEHOLDER, "\0") : id;
  246. }
  247. const windowsSlashRE = /\\/g;
  248. function slash$1(p) {
  249. return p.replace(windowsSlashRE, "/");
  250. }
  251. const postfixRE = /[?#].*$/;
  252. function cleanUrl(url) {
  253. return url.replace(postfixRE, "");
  254. }
  255. function withTrailingSlash(path) {
  256. if (path[path.length - 1] !== "/") {
  257. return `${path}/`;
  258. }
  259. return path;
  260. }
  261. const AsyncFunction = async function() {
  262. }.constructor;
  263. const asyncFunctionDeclarationPaddingLineCount = /* @__PURE__ */ (() => {
  264. const body = "/*code*/";
  265. const source = new AsyncFunction("a", "b", body).toString();
  266. return source.slice(0, source.indexOf(body)).split("\n").length - 1;
  267. })();
  268. // @ts-check
  269. /** @typedef { import('estree').BaseNode} BaseNode */
  270. /** @typedef {{
  271. skip: () => void;
  272. remove: () => void;
  273. replace: (node: BaseNode) => void;
  274. }} WalkerContext */
  275. let WalkerBase$1 = class WalkerBase {
  276. constructor() {
  277. /** @type {boolean} */
  278. this.should_skip = false;
  279. /** @type {boolean} */
  280. this.should_remove = false;
  281. /** @type {BaseNode | null} */
  282. this.replacement = null;
  283. /** @type {WalkerContext} */
  284. this.context = {
  285. skip: () => (this.should_skip = true),
  286. remove: () => (this.should_remove = true),
  287. replace: (node) => (this.replacement = node)
  288. };
  289. }
  290. /**
  291. *
  292. * @param {any} parent
  293. * @param {string} prop
  294. * @param {number} index
  295. * @param {BaseNode} node
  296. */
  297. replace(parent, prop, index, node) {
  298. if (parent) {
  299. if (index !== null) {
  300. parent[prop][index] = node;
  301. } else {
  302. parent[prop] = node;
  303. }
  304. }
  305. }
  306. /**
  307. *
  308. * @param {any} parent
  309. * @param {string} prop
  310. * @param {number} index
  311. */
  312. remove(parent, prop, index) {
  313. if (parent) {
  314. if (index !== null) {
  315. parent[prop].splice(index, 1);
  316. } else {
  317. delete parent[prop];
  318. }
  319. }
  320. }
  321. };
  322. // @ts-check
  323. /** @typedef { import('estree').BaseNode} BaseNode */
  324. /** @typedef { import('./walker.js').WalkerContext} WalkerContext */
  325. /** @typedef {(
  326. * this: WalkerContext,
  327. * node: BaseNode,
  328. * parent: BaseNode,
  329. * key: string,
  330. * index: number
  331. * ) => void} SyncHandler */
  332. let SyncWalker$1 = class SyncWalker extends WalkerBase$1 {
  333. /**
  334. *
  335. * @param {SyncHandler} enter
  336. * @param {SyncHandler} leave
  337. */
  338. constructor(enter, leave) {
  339. super();
  340. /** @type {SyncHandler} */
  341. this.enter = enter;
  342. /** @type {SyncHandler} */
  343. this.leave = leave;
  344. }
  345. /**
  346. *
  347. * @param {BaseNode} node
  348. * @param {BaseNode} parent
  349. * @param {string} [prop]
  350. * @param {number} [index]
  351. * @returns {BaseNode}
  352. */
  353. visit(node, parent, prop, index) {
  354. if (node) {
  355. if (this.enter) {
  356. const _should_skip = this.should_skip;
  357. const _should_remove = this.should_remove;
  358. const _replacement = this.replacement;
  359. this.should_skip = false;
  360. this.should_remove = false;
  361. this.replacement = null;
  362. this.enter.call(this.context, node, parent, prop, index);
  363. if (this.replacement) {
  364. node = this.replacement;
  365. this.replace(parent, prop, index, node);
  366. }
  367. if (this.should_remove) {
  368. this.remove(parent, prop, index);
  369. }
  370. const skipped = this.should_skip;
  371. const removed = this.should_remove;
  372. this.should_skip = _should_skip;
  373. this.should_remove = _should_remove;
  374. this.replacement = _replacement;
  375. if (skipped) return node;
  376. if (removed) return null;
  377. }
  378. for (const key in node) {
  379. const value = node[key];
  380. if (typeof value !== "object") {
  381. continue;
  382. } else if (Array.isArray(value)) {
  383. for (let i = 0; i < value.length; i += 1) {
  384. if (value[i] !== null && typeof value[i].type === 'string') {
  385. if (!this.visit(value[i], node, key, i)) {
  386. // removed
  387. i--;
  388. }
  389. }
  390. }
  391. } else if (value !== null && typeof value.type === "string") {
  392. this.visit(value, node, key, null);
  393. }
  394. }
  395. if (this.leave) {
  396. const _replacement = this.replacement;
  397. const _should_remove = this.should_remove;
  398. this.replacement = null;
  399. this.should_remove = false;
  400. this.leave.call(this.context, node, parent, prop, index);
  401. if (this.replacement) {
  402. node = this.replacement;
  403. this.replace(parent, prop, index, node);
  404. }
  405. if (this.should_remove) {
  406. this.remove(parent, prop, index);
  407. }
  408. const removed = this.should_remove;
  409. this.replacement = _replacement;
  410. this.should_remove = _should_remove;
  411. if (removed) return null;
  412. }
  413. }
  414. return node;
  415. }
  416. };
  417. // @ts-check
  418. /** @typedef { import('estree').BaseNode} BaseNode */
  419. /** @typedef { import('./sync.js').SyncHandler} SyncHandler */
  420. /** @typedef { import('./async.js').AsyncHandler} AsyncHandler */
  421. /**
  422. *
  423. * @param {BaseNode} ast
  424. * @param {{
  425. * enter?: SyncHandler
  426. * leave?: SyncHandler
  427. * }} walker
  428. * @returns {BaseNode}
  429. */
  430. function walk$3(ast, { enter, leave }) {
  431. const instance = new SyncWalker$1(enter, leave);
  432. return instance.visit(ast, null);
  433. }
  434. var utils$k = {};
  435. const path$m = require$$0$4;
  436. const WIN_SLASH = '\\\\/';
  437. const WIN_NO_SLASH = `[^${WIN_SLASH}]`;
  438. /**
  439. * Posix glob regex
  440. */
  441. const DOT_LITERAL = '\\.';
  442. const PLUS_LITERAL = '\\+';
  443. const QMARK_LITERAL = '\\?';
  444. const SLASH_LITERAL = '\\/';
  445. const ONE_CHAR = '(?=.)';
  446. const QMARK = '[^/]';
  447. const END_ANCHOR = `(?:${SLASH_LITERAL}|$)`;
  448. const START_ANCHOR = `(?:^|${SLASH_LITERAL})`;
  449. const DOTS_SLASH = `${DOT_LITERAL}{1,2}${END_ANCHOR}`;
  450. const NO_DOT = `(?!${DOT_LITERAL})`;
  451. const NO_DOTS = `(?!${START_ANCHOR}${DOTS_SLASH})`;
  452. const NO_DOT_SLASH = `(?!${DOT_LITERAL}{0,1}${END_ANCHOR})`;
  453. const NO_DOTS_SLASH = `(?!${DOTS_SLASH})`;
  454. const QMARK_NO_DOT = `[^.${SLASH_LITERAL}]`;
  455. const STAR$1 = `${QMARK}*?`;
  456. const POSIX_CHARS = {
  457. DOT_LITERAL,
  458. PLUS_LITERAL,
  459. QMARK_LITERAL,
  460. SLASH_LITERAL,
  461. ONE_CHAR,
  462. QMARK,
  463. END_ANCHOR,
  464. DOTS_SLASH,
  465. NO_DOT,
  466. NO_DOTS,
  467. NO_DOT_SLASH,
  468. NO_DOTS_SLASH,
  469. QMARK_NO_DOT,
  470. STAR: STAR$1,
  471. START_ANCHOR
  472. };
  473. /**
  474. * Windows glob regex
  475. */
  476. const WINDOWS_CHARS = {
  477. ...POSIX_CHARS,
  478. SLASH_LITERAL: `[${WIN_SLASH}]`,
  479. QMARK: WIN_NO_SLASH,
  480. STAR: `${WIN_NO_SLASH}*?`,
  481. DOTS_SLASH: `${DOT_LITERAL}{1,2}(?:[${WIN_SLASH}]|$)`,
  482. NO_DOT: `(?!${DOT_LITERAL})`,
  483. NO_DOTS: `(?!(?:^|[${WIN_SLASH}])${DOT_LITERAL}{1,2}(?:[${WIN_SLASH}]|$))`,
  484. NO_DOT_SLASH: `(?!${DOT_LITERAL}{0,1}(?:[${WIN_SLASH}]|$))`,
  485. NO_DOTS_SLASH: `(?!${DOT_LITERAL}{1,2}(?:[${WIN_SLASH}]|$))`,
  486. QMARK_NO_DOT: `[^.${WIN_SLASH}]`,
  487. START_ANCHOR: `(?:^|[${WIN_SLASH}])`,
  488. END_ANCHOR: `(?:[${WIN_SLASH}]|$)`
  489. };
  490. /**
  491. * POSIX Bracket Regex
  492. */
  493. const POSIX_REGEX_SOURCE$1 = {
  494. alnum: 'a-zA-Z0-9',
  495. alpha: 'a-zA-Z',
  496. ascii: '\\x00-\\x7F',
  497. blank: ' \\t',
  498. cntrl: '\\x00-\\x1F\\x7F',
  499. digit: '0-9',
  500. graph: '\\x21-\\x7E',
  501. lower: 'a-z',
  502. print: '\\x20-\\x7E ',
  503. punct: '\\-!"#$%&\'()\\*+,./:;<=>?@[\\]^_`{|}~',
  504. space: ' \\t\\r\\n\\v\\f',
  505. upper: 'A-Z',
  506. word: 'A-Za-z0-9_',
  507. xdigit: 'A-Fa-f0-9'
  508. };
  509. var constants$6 = {
  510. MAX_LENGTH: 1024 * 64,
  511. POSIX_REGEX_SOURCE: POSIX_REGEX_SOURCE$1,
  512. // regular expressions
  513. REGEX_BACKSLASH: /\\(?![*+?^${}(|)[\]])/g,
  514. REGEX_NON_SPECIAL_CHARS: /^[^@![\].,$*+?^{}()|\\/]+/,
  515. REGEX_SPECIAL_CHARS: /[-*+?.^${}(|)[\]]/,
  516. REGEX_SPECIAL_CHARS_BACKREF: /(\\?)((\W)(\3*))/g,
  517. REGEX_SPECIAL_CHARS_GLOBAL: /([-*+?.^${}(|)[\]])/g,
  518. REGEX_REMOVE_BACKSLASH: /(?:\[.*?[^\\]\]|\\(?=.))/g,
  519. // Replace globs with equivalent patterns to reduce parsing time.
  520. REPLACEMENTS: {
  521. '***': '*',
  522. '**/**': '**',
  523. '**/**/**': '**'
  524. },
  525. // Digits
  526. CHAR_0: 48, /* 0 */
  527. CHAR_9: 57, /* 9 */
  528. // Alphabet chars.
  529. CHAR_UPPERCASE_A: 65, /* A */
  530. CHAR_LOWERCASE_A: 97, /* a */
  531. CHAR_UPPERCASE_Z: 90, /* Z */
  532. CHAR_LOWERCASE_Z: 122, /* z */
  533. CHAR_LEFT_PARENTHESES: 40, /* ( */
  534. CHAR_RIGHT_PARENTHESES: 41, /* ) */
  535. CHAR_ASTERISK: 42, /* * */
  536. // Non-alphabetic chars.
  537. CHAR_AMPERSAND: 38, /* & */
  538. CHAR_AT: 64, /* @ */
  539. CHAR_BACKWARD_SLASH: 92, /* \ */
  540. CHAR_CARRIAGE_RETURN: 13, /* \r */
  541. CHAR_CIRCUMFLEX_ACCENT: 94, /* ^ */
  542. CHAR_COLON: 58, /* : */
  543. CHAR_COMMA: 44, /* , */
  544. CHAR_DOT: 46, /* . */
  545. CHAR_DOUBLE_QUOTE: 34, /* " */
  546. CHAR_EQUAL: 61, /* = */
  547. CHAR_EXCLAMATION_MARK: 33, /* ! */
  548. CHAR_FORM_FEED: 12, /* \f */
  549. CHAR_FORWARD_SLASH: 47, /* / */
  550. CHAR_GRAVE_ACCENT: 96, /* ` */
  551. CHAR_HASH: 35, /* # */
  552. CHAR_HYPHEN_MINUS: 45, /* - */
  553. CHAR_LEFT_ANGLE_BRACKET: 60, /* < */
  554. CHAR_LEFT_CURLY_BRACE: 123, /* { */
  555. CHAR_LEFT_SQUARE_BRACKET: 91, /* [ */
  556. CHAR_LINE_FEED: 10, /* \n */
  557. CHAR_NO_BREAK_SPACE: 160, /* \u00A0 */
  558. CHAR_PERCENT: 37, /* % */
  559. CHAR_PLUS: 43, /* + */
  560. CHAR_QUESTION_MARK: 63, /* ? */
  561. CHAR_RIGHT_ANGLE_BRACKET: 62, /* > */
  562. CHAR_RIGHT_CURLY_BRACE: 125, /* } */
  563. CHAR_RIGHT_SQUARE_BRACKET: 93, /* ] */
  564. CHAR_SEMICOLON: 59, /* ; */
  565. CHAR_SINGLE_QUOTE: 39, /* ' */
  566. CHAR_SPACE: 32, /* */
  567. CHAR_TAB: 9, /* \t */
  568. CHAR_UNDERSCORE: 95, /* _ */
  569. CHAR_VERTICAL_LINE: 124, /* | */
  570. CHAR_ZERO_WIDTH_NOBREAK_SPACE: 65279, /* \uFEFF */
  571. SEP: path$m.sep,
  572. /**
  573. * Create EXTGLOB_CHARS
  574. */
  575. extglobChars(chars) {
  576. return {
  577. '!': { type: 'negate', open: '(?:(?!(?:', close: `))${chars.STAR})` },
  578. '?': { type: 'qmark', open: '(?:', close: ')?' },
  579. '+': { type: 'plus', open: '(?:', close: ')+' },
  580. '*': { type: 'star', open: '(?:', close: ')*' },
  581. '@': { type: 'at', open: '(?:', close: ')' }
  582. };
  583. },
  584. /**
  585. * Create GLOB_CHARS
  586. */
  587. globChars(win32) {
  588. return win32 === true ? WINDOWS_CHARS : POSIX_CHARS;
  589. }
  590. };
  591. (function (exports) {
  592. const path = require$$0$4;
  593. const win32 = process.platform === 'win32';
  594. const {
  595. REGEX_BACKSLASH,
  596. REGEX_REMOVE_BACKSLASH,
  597. REGEX_SPECIAL_CHARS,
  598. REGEX_SPECIAL_CHARS_GLOBAL
  599. } = constants$6;
  600. exports.isObject = val => val !== null && typeof val === 'object' && !Array.isArray(val);
  601. exports.hasRegexChars = str => REGEX_SPECIAL_CHARS.test(str);
  602. exports.isRegexChar = str => str.length === 1 && exports.hasRegexChars(str);
  603. exports.escapeRegex = str => str.replace(REGEX_SPECIAL_CHARS_GLOBAL, '\\$1');
  604. exports.toPosixSlashes = str => str.replace(REGEX_BACKSLASH, '/');
  605. exports.removeBackslashes = str => {
  606. return str.replace(REGEX_REMOVE_BACKSLASH, match => {
  607. return match === '\\' ? '' : match;
  608. });
  609. };
  610. exports.supportsLookbehinds = () => {
  611. const segs = process.version.slice(1).split('.').map(Number);
  612. if (segs.length === 3 && segs[0] >= 9 || (segs[0] === 8 && segs[1] >= 10)) {
  613. return true;
  614. }
  615. return false;
  616. };
  617. exports.isWindows = options => {
  618. if (options && typeof options.windows === 'boolean') {
  619. return options.windows;
  620. }
  621. return win32 === true || path.sep === '\\';
  622. };
  623. exports.escapeLast = (input, char, lastIdx) => {
  624. const idx = input.lastIndexOf(char, lastIdx);
  625. if (idx === -1) return input;
  626. if (input[idx - 1] === '\\') return exports.escapeLast(input, char, idx - 1);
  627. return `${input.slice(0, idx)}\\${input.slice(idx)}`;
  628. };
  629. exports.removePrefix = (input, state = {}) => {
  630. let output = input;
  631. if (output.startsWith('./')) {
  632. output = output.slice(2);
  633. state.prefix = './';
  634. }
  635. return output;
  636. };
  637. exports.wrapOutput = (input, state = {}, options = {}) => {
  638. const prepend = options.contains ? '' : '^';
  639. const append = options.contains ? '' : '$';
  640. let output = `${prepend}(?:${input})${append}`;
  641. if (state.negated === true) {
  642. output = `(?:^(?!${output}).*$)`;
  643. }
  644. return output;
  645. };
  646. } (utils$k));
  647. const utils$j = utils$k;
  648. const {
  649. CHAR_ASTERISK, /* * */
  650. CHAR_AT, /* @ */
  651. CHAR_BACKWARD_SLASH, /* \ */
  652. CHAR_COMMA: CHAR_COMMA$1, /* , */
  653. CHAR_DOT: CHAR_DOT$1, /* . */
  654. CHAR_EXCLAMATION_MARK, /* ! */
  655. CHAR_FORWARD_SLASH, /* / */
  656. CHAR_LEFT_CURLY_BRACE: CHAR_LEFT_CURLY_BRACE$1, /* { */
  657. CHAR_LEFT_PARENTHESES: CHAR_LEFT_PARENTHESES$1, /* ( */
  658. CHAR_LEFT_SQUARE_BRACKET: CHAR_LEFT_SQUARE_BRACKET$1, /* [ */
  659. CHAR_PLUS, /* + */
  660. CHAR_QUESTION_MARK, /* ? */
  661. CHAR_RIGHT_CURLY_BRACE: CHAR_RIGHT_CURLY_BRACE$1, /* } */
  662. CHAR_RIGHT_PARENTHESES: CHAR_RIGHT_PARENTHESES$1, /* ) */
  663. CHAR_RIGHT_SQUARE_BRACKET: CHAR_RIGHT_SQUARE_BRACKET$1 /* ] */
  664. } = constants$6;
  665. const isPathSeparator = code => {
  666. return code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH;
  667. };
  668. const depth = token => {
  669. if (token.isPrefix !== true) {
  670. token.depth = token.isGlobstar ? Infinity : 1;
  671. }
  672. };
  673. /**
  674. * Quickly scans a glob pattern and returns an object with a handful of
  675. * useful properties, like `isGlob`, `path` (the leading non-glob, if it exists),
  676. * `glob` (the actual pattern), `negated` (true if the path starts with `!` but not
  677. * with `!(`) and `negatedExtglob` (true if the path starts with `!(`).
  678. *
  679. * ```js
  680. * const pm = require('picomatch');
  681. * console.log(pm.scan('foo/bar/*.js'));
  682. * { isGlob: true, input: 'foo/bar/*.js', base: 'foo/bar', glob: '*.js' }
  683. * ```
  684. * @param {String} `str`
  685. * @param {Object} `options`
  686. * @return {Object} Returns an object with tokens and regex source string.
  687. * @api public
  688. */
  689. const scan$2 = (input, options) => {
  690. const opts = options || {};
  691. const length = input.length - 1;
  692. const scanToEnd = opts.parts === true || opts.scanToEnd === true;
  693. const slashes = [];
  694. const tokens = [];
  695. const parts = [];
  696. let str = input;
  697. let index = -1;
  698. let start = 0;
  699. let lastIndex = 0;
  700. let isBrace = false;
  701. let isBracket = false;
  702. let isGlob = false;
  703. let isExtglob = false;
  704. let isGlobstar = false;
  705. let braceEscaped = false;
  706. let backslashes = false;
  707. let negated = false;
  708. let negatedExtglob = false;
  709. let finished = false;
  710. let braces = 0;
  711. let prev;
  712. let code;
  713. let token = { value: '', depth: 0, isGlob: false };
  714. const eos = () => index >= length;
  715. const peek = () => str.charCodeAt(index + 1);
  716. const advance = () => {
  717. prev = code;
  718. return str.charCodeAt(++index);
  719. };
  720. while (index < length) {
  721. code = advance();
  722. let next;
  723. if (code === CHAR_BACKWARD_SLASH) {
  724. backslashes = token.backslashes = true;
  725. code = advance();
  726. if (code === CHAR_LEFT_CURLY_BRACE$1) {
  727. braceEscaped = true;
  728. }
  729. continue;
  730. }
  731. if (braceEscaped === true || code === CHAR_LEFT_CURLY_BRACE$1) {
  732. braces++;
  733. while (eos() !== true && (code = advance())) {
  734. if (code === CHAR_BACKWARD_SLASH) {
  735. backslashes = token.backslashes = true;
  736. advance();
  737. continue;
  738. }
  739. if (code === CHAR_LEFT_CURLY_BRACE$1) {
  740. braces++;
  741. continue;
  742. }
  743. if (braceEscaped !== true && code === CHAR_DOT$1 && (code = advance()) === CHAR_DOT$1) {
  744. isBrace = token.isBrace = true;
  745. isGlob = token.isGlob = true;
  746. finished = true;
  747. if (scanToEnd === true) {
  748. continue;
  749. }
  750. break;
  751. }
  752. if (braceEscaped !== true && code === CHAR_COMMA$1) {
  753. isBrace = token.isBrace = true;
  754. isGlob = token.isGlob = true;
  755. finished = true;
  756. if (scanToEnd === true) {
  757. continue;
  758. }
  759. break;
  760. }
  761. if (code === CHAR_RIGHT_CURLY_BRACE$1) {
  762. braces--;
  763. if (braces === 0) {
  764. braceEscaped = false;
  765. isBrace = token.isBrace = true;
  766. finished = true;
  767. break;
  768. }
  769. }
  770. }
  771. if (scanToEnd === true) {
  772. continue;
  773. }
  774. break;
  775. }
  776. if (code === CHAR_FORWARD_SLASH) {
  777. slashes.push(index);
  778. tokens.push(token);
  779. token = { value: '', depth: 0, isGlob: false };
  780. if (finished === true) continue;
  781. if (prev === CHAR_DOT$1 && index === (start + 1)) {
  782. start += 2;
  783. continue;
  784. }
  785. lastIndex = index + 1;
  786. continue;
  787. }
  788. if (opts.noext !== true) {
  789. const isExtglobChar = code === CHAR_PLUS
  790. || code === CHAR_AT
  791. || code === CHAR_ASTERISK
  792. || code === CHAR_QUESTION_MARK
  793. || code === CHAR_EXCLAMATION_MARK;
  794. if (isExtglobChar === true && peek() === CHAR_LEFT_PARENTHESES$1) {
  795. isGlob = token.isGlob = true;
  796. isExtglob = token.isExtglob = true;
  797. finished = true;
  798. if (code === CHAR_EXCLAMATION_MARK && index === start) {
  799. negatedExtglob = true;
  800. }
  801. if (scanToEnd === true) {
  802. while (eos() !== true && (code = advance())) {
  803. if (code === CHAR_BACKWARD_SLASH) {
  804. backslashes = token.backslashes = true;
  805. code = advance();
  806. continue;
  807. }
  808. if (code === CHAR_RIGHT_PARENTHESES$1) {
  809. isGlob = token.isGlob = true;
  810. finished = true;
  811. break;
  812. }
  813. }
  814. continue;
  815. }
  816. break;
  817. }
  818. }
  819. if (code === CHAR_ASTERISK) {
  820. if (prev === CHAR_ASTERISK) isGlobstar = token.isGlobstar = true;
  821. isGlob = token.isGlob = true;
  822. finished = true;
  823. if (scanToEnd === true) {
  824. continue;
  825. }
  826. break;
  827. }
  828. if (code === CHAR_QUESTION_MARK) {
  829. isGlob = token.isGlob = true;
  830. finished = true;
  831. if (scanToEnd === true) {
  832. continue;
  833. }
  834. break;
  835. }
  836. if (code === CHAR_LEFT_SQUARE_BRACKET$1) {
  837. while (eos() !== true && (next = advance())) {
  838. if (next === CHAR_BACKWARD_SLASH) {
  839. backslashes = token.backslashes = true;
  840. advance();
  841. continue;
  842. }
  843. if (next === CHAR_RIGHT_SQUARE_BRACKET$1) {
  844. isBracket = token.isBracket = true;
  845. isGlob = token.isGlob = true;
  846. finished = true;
  847. break;
  848. }
  849. }
  850. if (scanToEnd === true) {
  851. continue;
  852. }
  853. break;
  854. }
  855. if (opts.nonegate !== true && code === CHAR_EXCLAMATION_MARK && index === start) {
  856. negated = token.negated = true;
  857. start++;
  858. continue;
  859. }
  860. if (opts.noparen !== true && code === CHAR_LEFT_PARENTHESES$1) {
  861. isGlob = token.isGlob = true;
  862. if (scanToEnd === true) {
  863. while (eos() !== true && (code = advance())) {
  864. if (code === CHAR_LEFT_PARENTHESES$1) {
  865. backslashes = token.backslashes = true;
  866. code = advance();
  867. continue;
  868. }
  869. if (code === CHAR_RIGHT_PARENTHESES$1) {
  870. finished = true;
  871. break;
  872. }
  873. }
  874. continue;
  875. }
  876. break;
  877. }
  878. if (isGlob === true) {
  879. finished = true;
  880. if (scanToEnd === true) {
  881. continue;
  882. }
  883. break;
  884. }
  885. }
  886. if (opts.noext === true) {
  887. isExtglob = false;
  888. isGlob = false;
  889. }
  890. let base = str;
  891. let prefix = '';
  892. let glob = '';
  893. if (start > 0) {
  894. prefix = str.slice(0, start);
  895. str = str.slice(start);
  896. lastIndex -= start;
  897. }
  898. if (base && isGlob === true && lastIndex > 0) {
  899. base = str.slice(0, lastIndex);
  900. glob = str.slice(lastIndex);
  901. } else if (isGlob === true) {
  902. base = '';
  903. glob = str;
  904. } else {
  905. base = str;
  906. }
  907. if (base && base !== '' && base !== '/' && base !== str) {
  908. if (isPathSeparator(base.charCodeAt(base.length - 1))) {
  909. base = base.slice(0, -1);
  910. }
  911. }
  912. if (opts.unescape === true) {
  913. if (glob) glob = utils$j.removeBackslashes(glob);
  914. if (base && backslashes === true) {
  915. base = utils$j.removeBackslashes(base);
  916. }
  917. }
  918. const state = {
  919. prefix,
  920. input,
  921. start,
  922. base,
  923. glob,
  924. isBrace,
  925. isBracket,
  926. isGlob,
  927. isExtglob,
  928. isGlobstar,
  929. negated,
  930. negatedExtglob
  931. };
  932. if (opts.tokens === true) {
  933. state.maxDepth = 0;
  934. if (!isPathSeparator(code)) {
  935. tokens.push(token);
  936. }
  937. state.tokens = tokens;
  938. }
  939. if (opts.parts === true || opts.tokens === true) {
  940. let prevIndex;
  941. for (let idx = 0; idx < slashes.length; idx++) {
  942. const n = prevIndex ? prevIndex + 1 : start;
  943. const i = slashes[idx];
  944. const value = input.slice(n, i);
  945. if (opts.tokens) {
  946. if (idx === 0 && start !== 0) {
  947. tokens[idx].isPrefix = true;
  948. tokens[idx].value = prefix;
  949. } else {
  950. tokens[idx].value = value;
  951. }
  952. depth(tokens[idx]);
  953. state.maxDepth += tokens[idx].depth;
  954. }
  955. if (idx !== 0 || value !== '') {
  956. parts.push(value);
  957. }
  958. prevIndex = i;
  959. }
  960. if (prevIndex && prevIndex + 1 < input.length) {
  961. const value = input.slice(prevIndex + 1);
  962. parts.push(value);
  963. if (opts.tokens) {
  964. tokens[tokens.length - 1].value = value;
  965. depth(tokens[tokens.length - 1]);
  966. state.maxDepth += tokens[tokens.length - 1].depth;
  967. }
  968. }
  969. state.slashes = slashes;
  970. state.parts = parts;
  971. }
  972. return state;
  973. };
  974. var scan_1 = scan$2;
  975. const constants$5 = constants$6;
  976. const utils$i = utils$k;
  977. /**
  978. * Constants
  979. */
  980. const {
  981. MAX_LENGTH: MAX_LENGTH$1,
  982. POSIX_REGEX_SOURCE,
  983. REGEX_NON_SPECIAL_CHARS,
  984. REGEX_SPECIAL_CHARS_BACKREF,
  985. REPLACEMENTS
  986. } = constants$5;
  987. /**
  988. * Helpers
  989. */
  990. const expandRange = (args, options) => {
  991. if (typeof options.expandRange === 'function') {
  992. return options.expandRange(...args, options);
  993. }
  994. args.sort();
  995. const value = `[${args.join('-')}]`;
  996. return value;
  997. };
  998. /**
  999. * Create the message for a syntax error
  1000. */
  1001. const syntaxError = (type, char) => {
  1002. return `Missing ${type}: "${char}" - use "\\\\${char}" to match literal characters`;
  1003. };
  1004. /**
  1005. * Parse the given input string.
  1006. * @param {String} input
  1007. * @param {Object} options
  1008. * @return {Object}
  1009. */
  1010. const parse$g = (input, options) => {
  1011. if (typeof input !== 'string') {
  1012. throw new TypeError('Expected a string');
  1013. }
  1014. input = REPLACEMENTS[input] || input;
  1015. const opts = { ...options };
  1016. const max = typeof opts.maxLength === 'number' ? Math.min(MAX_LENGTH$1, opts.maxLength) : MAX_LENGTH$1;
  1017. let len = input.length;
  1018. if (len > max) {
  1019. throw new SyntaxError(`Input length: ${len}, exceeds maximum allowed length: ${max}`);
  1020. }
  1021. const bos = { type: 'bos', value: '', output: opts.prepend || '' };
  1022. const tokens = [bos];
  1023. const capture = opts.capture ? '' : '?:';
  1024. const win32 = utils$i.isWindows(options);
  1025. // create constants based on platform, for windows or posix
  1026. const PLATFORM_CHARS = constants$5.globChars(win32);
  1027. const EXTGLOB_CHARS = constants$5.extglobChars(PLATFORM_CHARS);
  1028. const {
  1029. DOT_LITERAL,
  1030. PLUS_LITERAL,
  1031. SLASH_LITERAL,
  1032. ONE_CHAR,
  1033. DOTS_SLASH,
  1034. NO_DOT,
  1035. NO_DOT_SLASH,
  1036. NO_DOTS_SLASH,
  1037. QMARK,
  1038. QMARK_NO_DOT,
  1039. STAR,
  1040. START_ANCHOR
  1041. } = PLATFORM_CHARS;
  1042. const globstar = opts => {
  1043. return `(${capture}(?:(?!${START_ANCHOR}${opts.dot ? DOTS_SLASH : DOT_LITERAL}).)*?)`;
  1044. };
  1045. const nodot = opts.dot ? '' : NO_DOT;
  1046. const qmarkNoDot = opts.dot ? QMARK : QMARK_NO_DOT;
  1047. let star = opts.bash === true ? globstar(opts) : STAR;
  1048. if (opts.capture) {
  1049. star = `(${star})`;
  1050. }
  1051. // minimatch options support
  1052. if (typeof opts.noext === 'boolean') {
  1053. opts.noextglob = opts.noext;
  1054. }
  1055. const state = {
  1056. input,
  1057. index: -1,
  1058. start: 0,
  1059. dot: opts.dot === true,
  1060. consumed: '',
  1061. output: '',
  1062. prefix: '',
  1063. backtrack: false,
  1064. negated: false,
  1065. brackets: 0,
  1066. braces: 0,
  1067. parens: 0,
  1068. quotes: 0,
  1069. globstar: false,
  1070. tokens
  1071. };
  1072. input = utils$i.removePrefix(input, state);
  1073. len = input.length;
  1074. const extglobs = [];
  1075. const braces = [];
  1076. const stack = [];
  1077. let prev = bos;
  1078. let value;
  1079. /**
  1080. * Tokenizing helpers
  1081. */
  1082. const eos = () => state.index === len - 1;
  1083. const peek = state.peek = (n = 1) => input[state.index + n];
  1084. const advance = state.advance = () => input[++state.index] || '';
  1085. const remaining = () => input.slice(state.index + 1);
  1086. const consume = (value = '', num = 0) => {
  1087. state.consumed += value;
  1088. state.index += num;
  1089. };
  1090. const append = token => {
  1091. state.output += token.output != null ? token.output : token.value;
  1092. consume(token.value);
  1093. };
  1094. const negate = () => {
  1095. let count = 1;
  1096. while (peek() === '!' && (peek(2) !== '(' || peek(3) === '?')) {
  1097. advance();
  1098. state.start++;
  1099. count++;
  1100. }
  1101. if (count % 2 === 0) {
  1102. return false;
  1103. }
  1104. state.negated = true;
  1105. state.start++;
  1106. return true;
  1107. };
  1108. const increment = type => {
  1109. state[type]++;
  1110. stack.push(type);
  1111. };
  1112. const decrement = type => {
  1113. state[type]--;
  1114. stack.pop();
  1115. };
  1116. /**
  1117. * Push tokens onto the tokens array. This helper speeds up
  1118. * tokenizing by 1) helping us avoid backtracking as much as possible,
  1119. * and 2) helping us avoid creating extra tokens when consecutive
  1120. * characters are plain text. This improves performance and simplifies
  1121. * lookbehinds.
  1122. */
  1123. const push = tok => {
  1124. if (prev.type === 'globstar') {
  1125. const isBrace = state.braces > 0 && (tok.type === 'comma' || tok.type === 'brace');
  1126. const isExtglob = tok.extglob === true || (extglobs.length && (tok.type === 'pipe' || tok.type === 'paren'));
  1127. if (tok.type !== 'slash' && tok.type !== 'paren' && !isBrace && !isExtglob) {
  1128. state.output = state.output.slice(0, -prev.output.length);
  1129. prev.type = 'star';
  1130. prev.value = '*';
  1131. prev.output = star;
  1132. state.output += prev.output;
  1133. }
  1134. }
  1135. if (extglobs.length && tok.type !== 'paren') {
  1136. extglobs[extglobs.length - 1].inner += tok.value;
  1137. }
  1138. if (tok.value || tok.output) append(tok);
  1139. if (prev && prev.type === 'text' && tok.type === 'text') {
  1140. prev.value += tok.value;
  1141. prev.output = (prev.output || '') + tok.value;
  1142. return;
  1143. }
  1144. tok.prev = prev;
  1145. tokens.push(tok);
  1146. prev = tok;
  1147. };
  1148. const extglobOpen = (type, value) => {
  1149. const token = { ...EXTGLOB_CHARS[value], conditions: 1, inner: '' };
  1150. token.prev = prev;
  1151. token.parens = state.parens;
  1152. token.output = state.output;
  1153. const output = (opts.capture ? '(' : '') + token.open;
  1154. increment('parens');
  1155. push({ type, value, output: state.output ? '' : ONE_CHAR });
  1156. push({ type: 'paren', extglob: true, value: advance(), output });
  1157. extglobs.push(token);
  1158. };
  1159. const extglobClose = token => {
  1160. let output = token.close + (opts.capture ? ')' : '');
  1161. let rest;
  1162. if (token.type === 'negate') {
  1163. let extglobStar = star;
  1164. if (token.inner && token.inner.length > 1 && token.inner.includes('/')) {
  1165. extglobStar = globstar(opts);
  1166. }
  1167. if (extglobStar !== star || eos() || /^\)+$/.test(remaining())) {
  1168. output = token.close = `)$))${extglobStar}`;
  1169. }
  1170. if (token.inner.includes('*') && (rest = remaining()) && /^\.[^\\/.]+$/.test(rest)) {
  1171. // Any non-magical string (`.ts`) or even nested expression (`.{ts,tsx}`) can follow after the closing parenthesis.
  1172. // In this case, we need to parse the string and use it in the output of the original pattern.
  1173. // Suitable patterns: `/!(*.d).ts`, `/!(*.d).{ts,tsx}`, `**/!(*-dbg).@(js)`.
  1174. //
  1175. // Disabling the `fastpaths` option due to a problem with parsing strings as `.ts` in the pattern like `**/!(*.d).ts`.
  1176. const expression = parse$g(rest, { ...options, fastpaths: false }).output;
  1177. output = token.close = `)${expression})${extglobStar})`;
  1178. }
  1179. if (token.prev.type === 'bos') {
  1180. state.negatedExtglob = true;
  1181. }
  1182. }
  1183. push({ type: 'paren', extglob: true, value, output });
  1184. decrement('parens');
  1185. };
  1186. /**
  1187. * Fast paths
  1188. */
  1189. if (opts.fastpaths !== false && !/(^[*!]|[/()[\]{}"])/.test(input)) {
  1190. let backslashes = false;
  1191. let output = input.replace(REGEX_SPECIAL_CHARS_BACKREF, (m, esc, chars, first, rest, index) => {
  1192. if (first === '\\') {
  1193. backslashes = true;
  1194. return m;
  1195. }
  1196. if (first === '?') {
  1197. if (esc) {
  1198. return esc + first + (rest ? QMARK.repeat(rest.length) : '');
  1199. }
  1200. if (index === 0) {
  1201. return qmarkNoDot + (rest ? QMARK.repeat(rest.length) : '');
  1202. }
  1203. return QMARK.repeat(chars.length);
  1204. }
  1205. if (first === '.') {
  1206. return DOT_LITERAL.repeat(chars.length);
  1207. }
  1208. if (first === '*') {
  1209. if (esc) {
  1210. return esc + first + (rest ? star : '');
  1211. }
  1212. return star;
  1213. }
  1214. return esc ? m : `\\${m}`;
  1215. });
  1216. if (backslashes === true) {
  1217. if (opts.unescape === true) {
  1218. output = output.replace(/\\/g, '');
  1219. } else {
  1220. output = output.replace(/\\+/g, m => {
  1221. return m.length % 2 === 0 ? '\\\\' : (m ? '\\' : '');
  1222. });
  1223. }
  1224. }
  1225. if (output === input && opts.contains === true) {
  1226. state.output = input;
  1227. return state;
  1228. }
  1229. state.output = utils$i.wrapOutput(output, state, options);
  1230. return state;
  1231. }
  1232. /**
  1233. * Tokenize input until we reach end-of-string
  1234. */
  1235. while (!eos()) {
  1236. value = advance();
  1237. if (value === '\u0000') {
  1238. continue;
  1239. }
  1240. /**
  1241. * Escaped characters
  1242. */
  1243. if (value === '\\') {
  1244. const next = peek();
  1245. if (next === '/' && opts.bash !== true) {
  1246. continue;
  1247. }
  1248. if (next === '.' || next === ';') {
  1249. continue;
  1250. }
  1251. if (!next) {
  1252. value += '\\';
  1253. push({ type: 'text', value });
  1254. continue;
  1255. }
  1256. // collapse slashes to reduce potential for exploits
  1257. const match = /^\\+/.exec(remaining());
  1258. let slashes = 0;
  1259. if (match && match[0].length > 2) {
  1260. slashes = match[0].length;
  1261. state.index += slashes;
  1262. if (slashes % 2 !== 0) {
  1263. value += '\\';
  1264. }
  1265. }
  1266. if (opts.unescape === true) {
  1267. value = advance();
  1268. } else {
  1269. value += advance();
  1270. }
  1271. if (state.brackets === 0) {
  1272. push({ type: 'text', value });
  1273. continue;
  1274. }
  1275. }
  1276. /**
  1277. * If we're inside a regex character class, continue
  1278. * until we reach the closing bracket.
  1279. */
  1280. if (state.brackets > 0 && (value !== ']' || prev.value === '[' || prev.value === '[^')) {
  1281. if (opts.posix !== false && value === ':') {
  1282. const inner = prev.value.slice(1);
  1283. if (inner.includes('[')) {
  1284. prev.posix = true;
  1285. if (inner.includes(':')) {
  1286. const idx = prev.value.lastIndexOf('[');
  1287. const pre = prev.value.slice(0, idx);
  1288. const rest = prev.value.slice(idx + 2);
  1289. const posix = POSIX_REGEX_SOURCE[rest];
  1290. if (posix) {
  1291. prev.value = pre + posix;
  1292. state.backtrack = true;
  1293. advance();
  1294. if (!bos.output && tokens.indexOf(prev) === 1) {
  1295. bos.output = ONE_CHAR;
  1296. }
  1297. continue;
  1298. }
  1299. }
  1300. }
  1301. }
  1302. if ((value === '[' && peek() !== ':') || (value === '-' && peek() === ']')) {
  1303. value = `\\${value}`;
  1304. }
  1305. if (value === ']' && (prev.value === '[' || prev.value === '[^')) {
  1306. value = `\\${value}`;
  1307. }
  1308. if (opts.posix === true && value === '!' && prev.value === '[') {
  1309. value = '^';
  1310. }
  1311. prev.value += value;
  1312. append({ value });
  1313. continue;
  1314. }
  1315. /**
  1316. * If we're inside a quoted string, continue
  1317. * until we reach the closing double quote.
  1318. */
  1319. if (state.quotes === 1 && value !== '"') {
  1320. value = utils$i.escapeRegex(value);
  1321. prev.value += value;
  1322. append({ value });
  1323. continue;
  1324. }
  1325. /**
  1326. * Double quotes
  1327. */
  1328. if (value === '"') {
  1329. state.quotes = state.quotes === 1 ? 0 : 1;
  1330. if (opts.keepQuotes === true) {
  1331. push({ type: 'text', value });
  1332. }
  1333. continue;
  1334. }
  1335. /**
  1336. * Parentheses
  1337. */
  1338. if (value === '(') {
  1339. increment('parens');
  1340. push({ type: 'paren', value });
  1341. continue;
  1342. }
  1343. if (value === ')') {
  1344. if (state.parens === 0 && opts.strictBrackets === true) {
  1345. throw new SyntaxError(syntaxError('opening', '('));
  1346. }
  1347. const extglob = extglobs[extglobs.length - 1];
  1348. if (extglob && state.parens === extglob.parens + 1) {
  1349. extglobClose(extglobs.pop());
  1350. continue;
  1351. }
  1352. push({ type: 'paren', value, output: state.parens ? ')' : '\\)' });
  1353. decrement('parens');
  1354. continue;
  1355. }
  1356. /**
  1357. * Square brackets
  1358. */
  1359. if (value === '[') {
  1360. if (opts.nobracket === true || !remaining().includes(']')) {
  1361. if (opts.nobracket !== true && opts.strictBrackets === true) {
  1362. throw new SyntaxError(syntaxError('closing', ']'));
  1363. }
  1364. value = `\\${value}`;
  1365. } else {
  1366. increment('brackets');
  1367. }
  1368. push({ type: 'bracket', value });
  1369. continue;
  1370. }
  1371. if (value === ']') {
  1372. if (opts.nobracket === true || (prev && prev.type === 'bracket' && prev.value.length === 1)) {
  1373. push({ type: 'text', value, output: `\\${value}` });
  1374. continue;
  1375. }
  1376. if (state.brackets === 0) {
  1377. if (opts.strictBrackets === true) {
  1378. throw new SyntaxError(syntaxError('opening', '['));
  1379. }
  1380. push({ type: 'text', value, output: `\\${value}` });
  1381. continue;
  1382. }
  1383. decrement('brackets');
  1384. const prevValue = prev.value.slice(1);
  1385. if (prev.posix !== true && prevValue[0] === '^' && !prevValue.includes('/')) {
  1386. value = `/${value}`;
  1387. }
  1388. prev.value += value;
  1389. append({ value });
  1390. // when literal brackets are explicitly disabled
  1391. // assume we should match with a regex character class
  1392. if (opts.literalBrackets === false || utils$i.hasRegexChars(prevValue)) {
  1393. continue;
  1394. }
  1395. const escaped = utils$i.escapeRegex(prev.value);
  1396. state.output = state.output.slice(0, -prev.value.length);
  1397. // when literal brackets are explicitly enabled
  1398. // assume we should escape the brackets to match literal characters
  1399. if (opts.literalBrackets === true) {
  1400. state.output += escaped;
  1401. prev.value = escaped;
  1402. continue;
  1403. }
  1404. // when the user specifies nothing, try to match both
  1405. prev.value = `(${capture}${escaped}|${prev.value})`;
  1406. state.output += prev.value;
  1407. continue;
  1408. }
  1409. /**
  1410. * Braces
  1411. */
  1412. if (value === '{' && opts.nobrace !== true) {
  1413. increment('braces');
  1414. const open = {
  1415. type: 'brace',
  1416. value,
  1417. output: '(',
  1418. outputIndex: state.output.length,
  1419. tokensIndex: state.tokens.length
  1420. };
  1421. braces.push(open);
  1422. push(open);
  1423. continue;
  1424. }
  1425. if (value === '}') {
  1426. const brace = braces[braces.length - 1];
  1427. if (opts.nobrace === true || !brace) {
  1428. push({ type: 'text', value, output: value });
  1429. continue;
  1430. }
  1431. let output = ')';
  1432. if (brace.dots === true) {
  1433. const arr = tokens.slice();
  1434. const range = [];
  1435. for (let i = arr.length - 1; i >= 0; i--) {
  1436. tokens.pop();
  1437. if (arr[i].type === 'brace') {
  1438. break;
  1439. }
  1440. if (arr[i].type !== 'dots') {
  1441. range.unshift(arr[i].value);
  1442. }
  1443. }
  1444. output = expandRange(range, opts);
  1445. state.backtrack = true;
  1446. }
  1447. if (brace.comma !== true && brace.dots !== true) {
  1448. const out = state.output.slice(0, brace.outputIndex);
  1449. const toks = state.tokens.slice(brace.tokensIndex);
  1450. brace.value = brace.output = '\\{';
  1451. value = output = '\\}';
  1452. state.output = out;
  1453. for (const t of toks) {
  1454. state.output += (t.output || t.value);
  1455. }
  1456. }
  1457. push({ type: 'brace', value, output });
  1458. decrement('braces');
  1459. braces.pop();
  1460. continue;
  1461. }
  1462. /**
  1463. * Pipes
  1464. */
  1465. if (value === '|') {
  1466. if (extglobs.length > 0) {
  1467. extglobs[extglobs.length - 1].conditions++;
  1468. }
  1469. push({ type: 'text', value });
  1470. continue;
  1471. }
  1472. /**
  1473. * Commas
  1474. */
  1475. if (value === ',') {
  1476. let output = value;
  1477. const brace = braces[braces.length - 1];
  1478. if (brace && stack[stack.length - 1] === 'braces') {
  1479. brace.comma = true;
  1480. output = '|';
  1481. }
  1482. push({ type: 'comma', value, output });
  1483. continue;
  1484. }
  1485. /**
  1486. * Slashes
  1487. */
  1488. if (value === '/') {
  1489. // if the beginning of the glob is "./", advance the start
  1490. // to the current index, and don't add the "./" characters
  1491. // to the state. This greatly simplifies lookbehinds when
  1492. // checking for BOS characters like "!" and "." (not "./")
  1493. if (prev.type === 'dot' && state.index === state.start + 1) {
  1494. state.start = state.index + 1;
  1495. state.consumed = '';
  1496. state.output = '';
  1497. tokens.pop();
  1498. prev = bos; // reset "prev" to the first token
  1499. continue;
  1500. }
  1501. push({ type: 'slash', value, output: SLASH_LITERAL });
  1502. continue;
  1503. }
  1504. /**
  1505. * Dots
  1506. */
  1507. if (value === '.') {
  1508. if (state.braces > 0 && prev.type === 'dot') {
  1509. if (prev.value === '.') prev.output = DOT_LITERAL;
  1510. const brace = braces[braces.length - 1];
  1511. prev.type = 'dots';
  1512. prev.output += value;
  1513. prev.value += value;
  1514. brace.dots = true;
  1515. continue;
  1516. }
  1517. if ((state.braces + state.parens) === 0 && prev.type !== 'bos' && prev.type !== 'slash') {
  1518. push({ type: 'text', value, output: DOT_LITERAL });
  1519. continue;
  1520. }
  1521. push({ type: 'dot', value, output: DOT_LITERAL });
  1522. continue;
  1523. }
  1524. /**
  1525. * Question marks
  1526. */
  1527. if (value === '?') {
  1528. const isGroup = prev && prev.value === '(';
  1529. if (!isGroup && opts.noextglob !== true && peek() === '(' && peek(2) !== '?') {
  1530. extglobOpen('qmark', value);
  1531. continue;
  1532. }
  1533. if (prev && prev.type === 'paren') {
  1534. const next = peek();
  1535. let output = value;
  1536. if (next === '<' && !utils$i.supportsLookbehinds()) {
  1537. throw new Error('Node.js v10 or higher is required for regex lookbehinds');
  1538. }
  1539. if ((prev.value === '(' && !/[!=<:]/.test(next)) || (next === '<' && !/<([!=]|\w+>)/.test(remaining()))) {
  1540. output = `\\${value}`;
  1541. }
  1542. push({ type: 'text', value, output });
  1543. continue;
  1544. }
  1545. if (opts.dot !== true && (prev.type === 'slash' || prev.type === 'bos')) {
  1546. push({ type: 'qmark', value, output: QMARK_NO_DOT });
  1547. continue;
  1548. }
  1549. push({ type: 'qmark', value, output: QMARK });
  1550. continue;
  1551. }
  1552. /**
  1553. * Exclamation
  1554. */
  1555. if (value === '!') {
  1556. if (opts.noextglob !== true && peek() === '(') {
  1557. if (peek(2) !== '?' || !/[!=<:]/.test(peek(3))) {
  1558. extglobOpen('negate', value);
  1559. continue;
  1560. }
  1561. }
  1562. if (opts.nonegate !== true && state.index === 0) {
  1563. negate();
  1564. continue;
  1565. }
  1566. }
  1567. /**
  1568. * Plus
  1569. */
  1570. if (value === '+') {
  1571. if (opts.noextglob !== true && peek() === '(' && peek(2) !== '?') {
  1572. extglobOpen('plus', value);
  1573. continue;
  1574. }
  1575. if ((prev && prev.value === '(') || opts.regex === false) {
  1576. push({ type: 'plus', value, output: PLUS_LITERAL });
  1577. continue;
  1578. }
  1579. if ((prev && (prev.type === 'bracket' || prev.type === 'paren' || prev.type === 'brace')) || state.parens > 0) {
  1580. push({ type: 'plus', value });
  1581. continue;
  1582. }
  1583. push({ type: 'plus', value: PLUS_LITERAL });
  1584. continue;
  1585. }
  1586. /**
  1587. * Plain text
  1588. */
  1589. if (value === '@') {
  1590. if (opts.noextglob !== true && peek() === '(' && peek(2) !== '?') {
  1591. push({ type: 'at', extglob: true, value, output: '' });
  1592. continue;
  1593. }
  1594. push({ type: 'text', value });
  1595. continue;
  1596. }
  1597. /**
  1598. * Plain text
  1599. */
  1600. if (value !== '*') {
  1601. if (value === '$' || value === '^') {
  1602. value = `\\${value}`;
  1603. }
  1604. const match = REGEX_NON_SPECIAL_CHARS.exec(remaining());
  1605. if (match) {
  1606. value += match[0];
  1607. state.index += match[0].length;
  1608. }
  1609. push({ type: 'text', value });
  1610. continue;
  1611. }
  1612. /**
  1613. * Stars
  1614. */
  1615. if (prev && (prev.type === 'globstar' || prev.star === true)) {
  1616. prev.type = 'star';
  1617. prev.star = true;
  1618. prev.value += value;
  1619. prev.output = star;
  1620. state.backtrack = true;
  1621. state.globstar = true;
  1622. consume(value);
  1623. continue;
  1624. }
  1625. let rest = remaining();
  1626. if (opts.noextglob !== true && /^\([^?]/.test(rest)) {
  1627. extglobOpen('star', value);
  1628. continue;
  1629. }
  1630. if (prev.type === 'star') {
  1631. if (opts.noglobstar === true) {
  1632. consume(value);
  1633. continue;
  1634. }
  1635. const prior = prev.prev;
  1636. const before = prior.prev;
  1637. const isStart = prior.type === 'slash' || prior.type === 'bos';
  1638. const afterStar = before && (before.type === 'star' || before.type === 'globstar');
  1639. if (opts.bash === true && (!isStart || (rest[0] && rest[0] !== '/'))) {
  1640. push({ type: 'star', value, output: '' });
  1641. continue;
  1642. }
  1643. const isBrace = state.braces > 0 && (prior.type === 'comma' || prior.type === 'brace');
  1644. const isExtglob = extglobs.length && (prior.type === 'pipe' || prior.type === 'paren');
  1645. if (!isStart && prior.type !== 'paren' && !isBrace && !isExtglob) {
  1646. push({ type: 'star', value, output: '' });
  1647. continue;
  1648. }
  1649. // strip consecutive `/**/`
  1650. while (rest.slice(0, 3) === '/**') {
  1651. const after = input[state.index + 4];
  1652. if (after && after !== '/') {
  1653. break;
  1654. }
  1655. rest = rest.slice(3);
  1656. consume('/**', 3);
  1657. }
  1658. if (prior.type === 'bos' && eos()) {
  1659. prev.type = 'globstar';
  1660. prev.value += value;
  1661. prev.output = globstar(opts);
  1662. state.output = prev.output;
  1663. state.globstar = true;
  1664. consume(value);
  1665. continue;
  1666. }
  1667. if (prior.type === 'slash' && prior.prev.type !== 'bos' && !afterStar && eos()) {
  1668. state.output = state.output.slice(0, -(prior.output + prev.output).length);
  1669. prior.output = `(?:${prior.output}`;
  1670. prev.type = 'globstar';
  1671. prev.output = globstar(opts) + (opts.strictSlashes ? ')' : '|$)');
  1672. prev.value += value;
  1673. state.globstar = true;
  1674. state.output += prior.output + prev.output;
  1675. consume(value);
  1676. continue;
  1677. }
  1678. if (prior.type === 'slash' && prior.prev.type !== 'bos' && rest[0] === '/') {
  1679. const end = rest[1] !== void 0 ? '|$' : '';
  1680. state.output = state.output.slice(0, -(prior.output + prev.output).length);
  1681. prior.output = `(?:${prior.output}`;
  1682. prev.type = 'globstar';
  1683. prev.output = `${globstar(opts)}${SLASH_LITERAL}|${SLASH_LITERAL}${end})`;
  1684. prev.value += value;
  1685. state.output += prior.output + prev.output;
  1686. state.globstar = true;
  1687. consume(value + advance());
  1688. push({ type: 'slash', value: '/', output: '' });
  1689. continue;
  1690. }
  1691. if (prior.type === 'bos' && rest[0] === '/') {
  1692. prev.type = 'globstar';
  1693. prev.value += value;
  1694. prev.output = `(?:^|${SLASH_LITERAL}|${globstar(opts)}${SLASH_LITERAL})`;
  1695. state.output = prev.output;
  1696. state.globstar = true;
  1697. consume(value + advance());
  1698. push({ type: 'slash', value: '/', output: '' });
  1699. continue;
  1700. }
  1701. // remove single star from output
  1702. state.output = state.output.slice(0, -prev.output.length);
  1703. // reset previous token to globstar
  1704. prev.type = 'globstar';
  1705. prev.output = globstar(opts);
  1706. prev.value += value;
  1707. // reset output with globstar
  1708. state.output += prev.output;
  1709. state.globstar = true;
  1710. consume(value);
  1711. continue;
  1712. }
  1713. const token = { type: 'star', value, output: star };
  1714. if (opts.bash === true) {
  1715. token.output = '.*?';
  1716. if (prev.type === 'bos' || prev.type === 'slash') {
  1717. token.output = nodot + token.output;
  1718. }
  1719. push(token);
  1720. continue;
  1721. }
  1722. if (prev && (prev.type === 'bracket' || prev.type === 'paren') && opts.regex === true) {
  1723. token.output = value;
  1724. push(token);
  1725. continue;
  1726. }
  1727. if (state.index === state.start || prev.type === 'slash' || prev.type === 'dot') {
  1728. if (prev.type === 'dot') {
  1729. state.output += NO_DOT_SLASH;
  1730. prev.output += NO_DOT_SLASH;
  1731. } else if (opts.dot === true) {
  1732. state.output += NO_DOTS_SLASH;
  1733. prev.output += NO_DOTS_SLASH;
  1734. } else {
  1735. state.output += nodot;
  1736. prev.output += nodot;
  1737. }
  1738. if (peek() !== '*') {
  1739. state.output += ONE_CHAR;
  1740. prev.output += ONE_CHAR;
  1741. }
  1742. }
  1743. push(token);
  1744. }
  1745. while (state.brackets > 0) {
  1746. if (opts.strictBrackets === true) throw new SyntaxError(syntaxError('closing', ']'));
  1747. state.output = utils$i.escapeLast(state.output, '[');
  1748. decrement('brackets');
  1749. }
  1750. while (state.parens > 0) {
  1751. if (opts.strictBrackets === true) throw new SyntaxError(syntaxError('closing', ')'));
  1752. state.output = utils$i.escapeLast(state.output, '(');
  1753. decrement('parens');
  1754. }
  1755. while (state.braces > 0) {
  1756. if (opts.strictBrackets === true) throw new SyntaxError(syntaxError('closing', '}'));
  1757. state.output = utils$i.escapeLast(state.output, '{');
  1758. decrement('braces');
  1759. }
  1760. if (opts.strictSlashes !== true && (prev.type === 'star' || prev.type === 'bracket')) {
  1761. push({ type: 'maybe_slash', value: '', output: `${SLASH_LITERAL}?` });
  1762. }
  1763. // rebuild the output if we had to backtrack at any point
  1764. if (state.backtrack === true) {
  1765. state.output = '';
  1766. for (const token of state.tokens) {
  1767. state.output += token.output != null ? token.output : token.value;
  1768. if (token.suffix) {
  1769. state.output += token.suffix;
  1770. }
  1771. }
  1772. }
  1773. return state;
  1774. };
  1775. /**
  1776. * Fast paths for creating regular expressions for common glob patterns.
  1777. * This can significantly speed up processing and has very little downside
  1778. * impact when none of the fast paths match.
  1779. */
  1780. parse$g.fastpaths = (input, options) => {
  1781. const opts = { ...options };
  1782. const max = typeof opts.maxLength === 'number' ? Math.min(MAX_LENGTH$1, opts.maxLength) : MAX_LENGTH$1;
  1783. const len = input.length;
  1784. if (len > max) {
  1785. throw new SyntaxError(`Input length: ${len}, exceeds maximum allowed length: ${max}`);
  1786. }
  1787. input = REPLACEMENTS[input] || input;
  1788. const win32 = utils$i.isWindows(options);
  1789. // create constants based on platform, for windows or posix
  1790. const {
  1791. DOT_LITERAL,
  1792. SLASH_LITERAL,
  1793. ONE_CHAR,
  1794. DOTS_SLASH,
  1795. NO_DOT,
  1796. NO_DOTS,
  1797. NO_DOTS_SLASH,
  1798. STAR,
  1799. START_ANCHOR
  1800. } = constants$5.globChars(win32);
  1801. const nodot = opts.dot ? NO_DOTS : NO_DOT;
  1802. const slashDot = opts.dot ? NO_DOTS_SLASH : NO_DOT;
  1803. const capture = opts.capture ? '' : '?:';
  1804. const state = { negated: false, prefix: '' };
  1805. let star = opts.bash === true ? '.*?' : STAR;
  1806. if (opts.capture) {
  1807. star = `(${star})`;
  1808. }
  1809. const globstar = opts => {
  1810. if (opts.noglobstar === true) return star;
  1811. return `(${capture}(?:(?!${START_ANCHOR}${opts.dot ? DOTS_SLASH : DOT_LITERAL}).)*?)`;
  1812. };
  1813. const create = str => {
  1814. switch (str) {
  1815. case '*':
  1816. return `${nodot}${ONE_CHAR}${star}`;
  1817. case '.*':
  1818. return `${DOT_LITERAL}${ONE_CHAR}${star}`;
  1819. case '*.*':
  1820. return `${nodot}${star}${DOT_LITERAL}${ONE_CHAR}${star}`;
  1821. case '*/*':
  1822. return `${nodot}${star}${SLASH_LITERAL}${ONE_CHAR}${slashDot}${star}`;
  1823. case '**':
  1824. return nodot + globstar(opts);
  1825. case '**/*':
  1826. return `(?:${nodot}${globstar(opts)}${SLASH_LITERAL})?${slashDot}${ONE_CHAR}${star}`;
  1827. case '**/*.*':
  1828. return `(?:${nodot}${globstar(opts)}${SLASH_LITERAL})?${slashDot}${star}${DOT_LITERAL}${ONE_CHAR}${star}`;
  1829. case '**/.*':
  1830. return `(?:${nodot}${globstar(opts)}${SLASH_LITERAL})?${DOT_LITERAL}${ONE_CHAR}${star}`;
  1831. default: {
  1832. const match = /^(.*?)\.(\w+)$/.exec(str);
  1833. if (!match) return;
  1834. const source = create(match[1]);
  1835. if (!source) return;
  1836. return source + DOT_LITERAL + match[2];
  1837. }
  1838. }
  1839. };
  1840. const output = utils$i.removePrefix(input, state);
  1841. let source = create(output);
  1842. if (source && opts.strictSlashes !== true) {
  1843. source += `${SLASH_LITERAL}?`;
  1844. }
  1845. return source;
  1846. };
  1847. var parse_1$3 = parse$g;
  1848. const path$l = require$$0$4;
  1849. const scan$1 = scan_1;
  1850. const parse$f = parse_1$3;
  1851. const utils$h = utils$k;
  1852. const constants$4 = constants$6;
  1853. const isObject$3 = val => val && typeof val === 'object' && !Array.isArray(val);
  1854. /**
  1855. * Creates a matcher function from one or more glob patterns. The
  1856. * returned function takes a string to match as its first argument,
  1857. * and returns true if the string is a match. The returned matcher
  1858. * function also takes a boolean as the second argument that, when true,
  1859. * returns an object with additional information.
  1860. *
  1861. * ```js
  1862. * const picomatch = require('picomatch');
  1863. * // picomatch(glob[, options]);
  1864. *
  1865. * const isMatch = picomatch('*.!(*a)');
  1866. * console.log(isMatch('a.a')); //=> false
  1867. * console.log(isMatch('a.b')); //=> true
  1868. * ```
  1869. * @name picomatch
  1870. * @param {String|Array} `globs` One or more glob patterns.
  1871. * @param {Object=} `options`
  1872. * @return {Function=} Returns a matcher function.
  1873. * @api public
  1874. */
  1875. const picomatch$5 = (glob, options, returnState = false) => {
  1876. if (Array.isArray(glob)) {
  1877. const fns = glob.map(input => picomatch$5(input, options, returnState));
  1878. const arrayMatcher = str => {
  1879. for (const isMatch of fns) {
  1880. const state = isMatch(str);
  1881. if (state) return state;
  1882. }
  1883. return false;
  1884. };
  1885. return arrayMatcher;
  1886. }
  1887. const isState = isObject$3(glob) && glob.tokens && glob.input;
  1888. if (glob === '' || (typeof glob !== 'string' && !isState)) {
  1889. throw new TypeError('Expected pattern to be a non-empty string');
  1890. }
  1891. const opts = options || {};
  1892. const posix = utils$h.isWindows(options);
  1893. const regex = isState
  1894. ? picomatch$5.compileRe(glob, options)
  1895. : picomatch$5.makeRe(glob, options, false, true);
  1896. const state = regex.state;
  1897. delete regex.state;
  1898. let isIgnored = () => false;
  1899. if (opts.ignore) {
  1900. const ignoreOpts = { ...options, ignore: null, onMatch: null, onResult: null };
  1901. isIgnored = picomatch$5(opts.ignore, ignoreOpts, returnState);
  1902. }
  1903. const matcher = (input, returnObject = false) => {
  1904. const { isMatch, match, output } = picomatch$5.test(input, regex, options, { glob, posix });
  1905. const result = { glob, state, regex, posix, input, output, match, isMatch };
  1906. if (typeof opts.onResult === 'function') {
  1907. opts.onResult(result);
  1908. }
  1909. if (isMatch === false) {
  1910. result.isMatch = false;
  1911. return returnObject ? result : false;
  1912. }
  1913. if (isIgnored(input)) {
  1914. if (typeof opts.onIgnore === 'function') {
  1915. opts.onIgnore(result);
  1916. }
  1917. result.isMatch = false;
  1918. return returnObject ? result : false;
  1919. }
  1920. if (typeof opts.onMatch === 'function') {
  1921. opts.onMatch(result);
  1922. }
  1923. return returnObject ? result : true;
  1924. };
  1925. if (returnState) {
  1926. matcher.state = state;
  1927. }
  1928. return matcher;
  1929. };
  1930. /**
  1931. * Test `input` with the given `regex`. This is used by the main
  1932. * `picomatch()` function to test the input string.
  1933. *
  1934. * ```js
  1935. * const picomatch = require('picomatch');
  1936. * // picomatch.test(input, regex[, options]);
  1937. *
  1938. * console.log(picomatch.test('foo/bar', /^(?:([^/]*?)\/([^/]*?))$/));
  1939. * // { isMatch: true, match: [ 'foo/', 'foo', 'bar' ], output: 'foo/bar' }
  1940. * ```
  1941. * @param {String} `input` String to test.
  1942. * @param {RegExp} `regex`
  1943. * @return {Object} Returns an object with matching info.
  1944. * @api public
  1945. */
  1946. picomatch$5.test = (input, regex, options, { glob, posix } = {}) => {
  1947. if (typeof input !== 'string') {
  1948. throw new TypeError('Expected input to be a string');
  1949. }
  1950. if (input === '') {
  1951. return { isMatch: false, output: '' };
  1952. }
  1953. const opts = options || {};
  1954. const format = opts.format || (posix ? utils$h.toPosixSlashes : null);
  1955. let match = input === glob;
  1956. let output = (match && format) ? format(input) : input;
  1957. if (match === false) {
  1958. output = format ? format(input) : input;
  1959. match = output === glob;
  1960. }
  1961. if (match === false || opts.capture === true) {
  1962. if (opts.matchBase === true || opts.basename === true) {
  1963. match = picomatch$5.matchBase(input, regex, options, posix);
  1964. } else {
  1965. match = regex.exec(output);
  1966. }
  1967. }
  1968. return { isMatch: Boolean(match), match, output };
  1969. };
  1970. /**
  1971. * Match the basename of a filepath.
  1972. *
  1973. * ```js
  1974. * const picomatch = require('picomatch');
  1975. * // picomatch.matchBase(input, glob[, options]);
  1976. * console.log(picomatch.matchBase('foo/bar.js', '*.js'); // true
  1977. * ```
  1978. * @param {String} `input` String to test.
  1979. * @param {RegExp|String} `glob` Glob pattern or regex created by [.makeRe](#makeRe).
  1980. * @return {Boolean}
  1981. * @api public
  1982. */
  1983. picomatch$5.matchBase = (input, glob, options, posix = utils$h.isWindows(options)) => {
  1984. const regex = glob instanceof RegExp ? glob : picomatch$5.makeRe(glob, options);
  1985. return regex.test(path$l.basename(input));
  1986. };
  1987. /**
  1988. * Returns true if **any** of the given glob `patterns` match the specified `string`.
  1989. *
  1990. * ```js
  1991. * const picomatch = require('picomatch');
  1992. * // picomatch.isMatch(string, patterns[, options]);
  1993. *
  1994. * console.log(picomatch.isMatch('a.a', ['b.*', '*.a'])); //=> true
  1995. * console.log(picomatch.isMatch('a.a', 'b.*')); //=> false
  1996. * ```
  1997. * @param {String|Array} str The string to test.
  1998. * @param {String|Array} patterns One or more glob patterns to use for matching.
  1999. * @param {Object} [options] See available [options](#options).
  2000. * @return {Boolean} Returns true if any patterns match `str`
  2001. * @api public
  2002. */
  2003. picomatch$5.isMatch = (str, patterns, options) => picomatch$5(patterns, options)(str);
  2004. /**
  2005. * Parse a glob pattern to create the source string for a regular
  2006. * expression.
  2007. *
  2008. * ```js
  2009. * const picomatch = require('picomatch');
  2010. * const result = picomatch.parse(pattern[, options]);
  2011. * ```
  2012. * @param {String} `pattern`
  2013. * @param {Object} `options`
  2014. * @return {Object} Returns an object with useful properties and output to be used as a regex source string.
  2015. * @api public
  2016. */
  2017. picomatch$5.parse = (pattern, options) => {
  2018. if (Array.isArray(pattern)) return pattern.map(p => picomatch$5.parse(p, options));
  2019. return parse$f(pattern, { ...options, fastpaths: false });
  2020. };
  2021. /**
  2022. * Scan a glob pattern to separate the pattern into segments.
  2023. *
  2024. * ```js
  2025. * const picomatch = require('picomatch');
  2026. * // picomatch.scan(input[, options]);
  2027. *
  2028. * const result = picomatch.scan('!./foo/*.js');
  2029. * console.log(result);
  2030. * { prefix: '!./',
  2031. * input: '!./foo/*.js',
  2032. * start: 3,
  2033. * base: 'foo',
  2034. * glob: '*.js',
  2035. * isBrace: false,
  2036. * isBracket: false,
  2037. * isGlob: true,
  2038. * isExtglob: false,
  2039. * isGlobstar: false,
  2040. * negated: true }
  2041. * ```
  2042. * @param {String} `input` Glob pattern to scan.
  2043. * @param {Object} `options`
  2044. * @return {Object} Returns an object with
  2045. * @api public
  2046. */
  2047. picomatch$5.scan = (input, options) => scan$1(input, options);
  2048. /**
  2049. * Compile a regular expression from the `state` object returned by the
  2050. * [parse()](#parse) method.
  2051. *
  2052. * @param {Object} `state`
  2053. * @param {Object} `options`
  2054. * @param {Boolean} `returnOutput` Intended for implementors, this argument allows you to return the raw output from the parser.
  2055. * @param {Boolean} `returnState` Adds the state to a `state` property on the returned regex. Useful for implementors and debugging.
  2056. * @return {RegExp}
  2057. * @api public
  2058. */
  2059. picomatch$5.compileRe = (state, options, returnOutput = false, returnState = false) => {
  2060. if (returnOutput === true) {
  2061. return state.output;
  2062. }
  2063. const opts = options || {};
  2064. const prepend = opts.contains ? '' : '^';
  2065. const append = opts.contains ? '' : '$';
  2066. let source = `${prepend}(?:${state.output})${append}`;
  2067. if (state && state.negated === true) {
  2068. source = `^(?!${source}).*$`;
  2069. }
  2070. const regex = picomatch$5.toRegex(source, options);
  2071. if (returnState === true) {
  2072. regex.state = state;
  2073. }
  2074. return regex;
  2075. };
  2076. /**
  2077. * Create a regular expression from a parsed glob pattern.
  2078. *
  2079. * ```js
  2080. * const picomatch = require('picomatch');
  2081. * const state = picomatch.parse('*.js');
  2082. * // picomatch.compileRe(state[, options]);
  2083. *
  2084. * console.log(picomatch.compileRe(state));
  2085. * //=> /^(?:(?!\.)(?=.)[^/]*?\.js)$/
  2086. * ```
  2087. * @param {String} `state` The object returned from the `.parse` method.
  2088. * @param {Object} `options`
  2089. * @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.
  2090. * @param {Boolean} `returnState` Implementors may use this argument to return the state from the parsed glob with the returned regular expression.
  2091. * @return {RegExp} Returns a regex created from the given pattern.
  2092. * @api public
  2093. */
  2094. picomatch$5.makeRe = (input, options = {}, returnOutput = false, returnState = false) => {
  2095. if (!input || typeof input !== 'string') {
  2096. throw new TypeError('Expected a non-empty string');
  2097. }
  2098. let parsed = { negated: false, fastpaths: true };
  2099. if (options.fastpaths !== false && (input[0] === '.' || input[0] === '*')) {
  2100. parsed.output = parse$f.fastpaths(input, options);
  2101. }
  2102. if (!parsed.output) {
  2103. parsed = parse$f(input, options);
  2104. }
  2105. return picomatch$5.compileRe(parsed, options, returnOutput, returnState);
  2106. };
  2107. /**
  2108. * Create a regular expression from the given regex source string.
  2109. *
  2110. * ```js
  2111. * const picomatch = require('picomatch');
  2112. * // picomatch.toRegex(source[, options]);
  2113. *
  2114. * const { output } = picomatch.parse('*.js');
  2115. * console.log(picomatch.toRegex(output));
  2116. * //=> /^(?:(?!\.)(?=.)[^/]*?\.js)$/
  2117. * ```
  2118. * @param {String} `source` Regular expression source string.
  2119. * @param {Object} `options`
  2120. * @return {RegExp}
  2121. * @api public
  2122. */
  2123. picomatch$5.toRegex = (source, options) => {
  2124. try {
  2125. const opts = options || {};
  2126. return new RegExp(source, opts.flags || (opts.nocase ? 'i' : ''));
  2127. } catch (err) {
  2128. if (options && options.debug === true) throw err;
  2129. return /$^/;
  2130. }
  2131. };
  2132. /**
  2133. * Picomatch constants.
  2134. * @return {Object}
  2135. */
  2136. picomatch$5.constants = constants$4;
  2137. /**
  2138. * Expose "picomatch"
  2139. */
  2140. var picomatch_1 = picomatch$5;
  2141. var picomatch$3 = picomatch_1;
  2142. var picomatch$4 = /*@__PURE__*/getDefaultExportFromCjs(picomatch$3);
  2143. const extractors = {
  2144. ArrayPattern(names, param) {
  2145. for (const element of param.elements) {
  2146. if (element)
  2147. extractors[element.type](names, element);
  2148. }
  2149. },
  2150. AssignmentPattern(names, param) {
  2151. extractors[param.left.type](names, param.left);
  2152. },
  2153. Identifier(names, param) {
  2154. names.push(param.name);
  2155. },
  2156. MemberExpression() { },
  2157. ObjectPattern(names, param) {
  2158. for (const prop of param.properties) {
  2159. // @ts-ignore Typescript reports that this is not a valid type
  2160. if (prop.type === 'RestElement') {
  2161. extractors.RestElement(names, prop);
  2162. }
  2163. else {
  2164. extractors[prop.value.type](names, prop.value);
  2165. }
  2166. }
  2167. },
  2168. RestElement(names, param) {
  2169. extractors[param.argument.type](names, param.argument);
  2170. }
  2171. };
  2172. const extractAssignedNames = function extractAssignedNames(param) {
  2173. const names = [];
  2174. extractors[param.type](names, param);
  2175. return names;
  2176. };
  2177. const blockDeclarations = {
  2178. const: true,
  2179. let: true
  2180. };
  2181. class Scope {
  2182. constructor(options = {}) {
  2183. this.parent = options.parent;
  2184. this.isBlockScope = !!options.block;
  2185. this.declarations = Object.create(null);
  2186. if (options.params) {
  2187. options.params.forEach((param) => {
  2188. extractAssignedNames(param).forEach((name) => {
  2189. this.declarations[name] = true;
  2190. });
  2191. });
  2192. }
  2193. }
  2194. addDeclaration(node, isBlockDeclaration, isVar) {
  2195. if (!isBlockDeclaration && this.isBlockScope) {
  2196. // it's a `var` or function node, and this
  2197. // is a block scope, so we need to go up
  2198. this.parent.addDeclaration(node, isBlockDeclaration, isVar);
  2199. }
  2200. else if (node.id) {
  2201. extractAssignedNames(node.id).forEach((name) => {
  2202. this.declarations[name] = true;
  2203. });
  2204. }
  2205. }
  2206. contains(name) {
  2207. return this.declarations[name] || (this.parent ? this.parent.contains(name) : false);
  2208. }
  2209. }
  2210. const attachScopes = function attachScopes(ast, propertyName = 'scope') {
  2211. let scope = new Scope();
  2212. walk$3(ast, {
  2213. enter(n, parent) {
  2214. const node = n;
  2215. // function foo () {...}
  2216. // class Foo {...}
  2217. if (/(Function|Class)Declaration/.test(node.type)) {
  2218. scope.addDeclaration(node, false, false);
  2219. }
  2220. // var foo = 1
  2221. if (node.type === 'VariableDeclaration') {
  2222. const { kind } = node;
  2223. const isBlockDeclaration = blockDeclarations[kind];
  2224. node.declarations.forEach((declaration) => {
  2225. scope.addDeclaration(declaration, isBlockDeclaration, true);
  2226. });
  2227. }
  2228. let newScope;
  2229. // create new function scope
  2230. if (/Function/.test(node.type)) {
  2231. const func = node;
  2232. newScope = new Scope({
  2233. parent: scope,
  2234. block: false,
  2235. params: func.params
  2236. });
  2237. // named function expressions - the name is considered
  2238. // part of the function's scope
  2239. if (func.type === 'FunctionExpression' && func.id) {
  2240. newScope.addDeclaration(func, false, false);
  2241. }
  2242. }
  2243. // create new for scope
  2244. if (/For(In|Of)?Statement/.test(node.type)) {
  2245. newScope = new Scope({
  2246. parent: scope,
  2247. block: true
  2248. });
  2249. }
  2250. // create new block scope
  2251. if (node.type === 'BlockStatement' && !/Function/.test(parent.type)) {
  2252. newScope = new Scope({
  2253. parent: scope,
  2254. block: true
  2255. });
  2256. }
  2257. // catch clause has its own block scope
  2258. if (node.type === 'CatchClause') {
  2259. newScope = new Scope({
  2260. parent: scope,
  2261. params: node.param ? [node.param] : [],
  2262. block: true
  2263. });
  2264. }
  2265. if (newScope) {
  2266. Object.defineProperty(node, propertyName, {
  2267. value: newScope,
  2268. configurable: true
  2269. });
  2270. scope = newScope;
  2271. }
  2272. },
  2273. leave(n) {
  2274. const node = n;
  2275. if (node[propertyName])
  2276. scope = scope.parent;
  2277. }
  2278. });
  2279. return scope;
  2280. };
  2281. // Helper since Typescript can't detect readonly arrays with Array.isArray
  2282. function isArray(arg) {
  2283. return Array.isArray(arg);
  2284. }
  2285. function ensureArray(thing) {
  2286. if (isArray(thing))
  2287. return thing;
  2288. if (thing == null)
  2289. return [];
  2290. return [thing];
  2291. }
  2292. const normalizePath$5 = function normalizePath(filename) {
  2293. return filename.split(win32.sep).join(posix.sep);
  2294. };
  2295. function getMatcherString(id, resolutionBase) {
  2296. if (resolutionBase === false || isAbsolute(id) || id.startsWith('**')) {
  2297. return normalizePath$5(id);
  2298. }
  2299. // resolve('') is valid and will default to process.cwd()
  2300. const basePath = normalizePath$5(resolve$3(resolutionBase || ''))
  2301. // escape all possible (posix + win) path characters that might interfere with regex
  2302. .replace(/[-^$*+?.()|[\]{}]/g, '\\$&');
  2303. // Note that we use posix.join because:
  2304. // 1. the basePath has been normalized to use /
  2305. // 2. the incoming glob (id) matcher, also uses /
  2306. // otherwise Node will force backslash (\) on windows
  2307. return posix.join(basePath, normalizePath$5(id));
  2308. }
  2309. const createFilter$1 = function createFilter(include, exclude, options) {
  2310. const resolutionBase = options && options.resolve;
  2311. const getMatcher = (id) => id instanceof RegExp
  2312. ? id
  2313. : {
  2314. test: (what) => {
  2315. // this refactor is a tad overly verbose but makes for easy debugging
  2316. const pattern = getMatcherString(id, resolutionBase);
  2317. const fn = picomatch$4(pattern, { dot: true });
  2318. const result = fn(what);
  2319. return result;
  2320. }
  2321. };
  2322. const includeMatchers = ensureArray(include).map(getMatcher);
  2323. const excludeMatchers = ensureArray(exclude).map(getMatcher);
  2324. return function result(id) {
  2325. if (typeof id !== 'string')
  2326. return false;
  2327. if (/\0/.test(id))
  2328. return false;
  2329. const pathId = normalizePath$5(id);
  2330. for (let i = 0; i < excludeMatchers.length; ++i) {
  2331. const matcher = excludeMatchers[i];
  2332. if (matcher.test(pathId))
  2333. return false;
  2334. }
  2335. for (let i = 0; i < includeMatchers.length; ++i) {
  2336. const matcher = includeMatchers[i];
  2337. if (matcher.test(pathId))
  2338. return true;
  2339. }
  2340. return !includeMatchers.length;
  2341. };
  2342. };
  2343. 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';
  2344. 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';
  2345. const forbiddenIdentifiers = new Set(`${reservedWords} ${builtins}`.split(' '));
  2346. forbiddenIdentifiers.add('');
  2347. const makeLegalIdentifier = function makeLegalIdentifier(str) {
  2348. let identifier = str
  2349. .replace(/-(\w)/g, (_, letter) => letter.toUpperCase())
  2350. .replace(/[^$_a-zA-Z0-9]/g, '_');
  2351. if (/\d/.test(identifier[0]) || forbiddenIdentifiers.has(identifier)) {
  2352. identifier = `_${identifier}`;
  2353. }
  2354. return identifier || '_';
  2355. };
  2356. function stringify$8(obj) {
  2357. return (JSON.stringify(obj) || 'undefined').replace(/[\u2028\u2029]/g, (char) => `\\u${`000${char.charCodeAt(0).toString(16)}`.slice(-4)}`);
  2358. }
  2359. function serializeArray(arr, indent, baseIndent) {
  2360. let output = '[';
  2361. const separator = indent ? `\n${baseIndent}${indent}` : '';
  2362. for (let i = 0; i < arr.length; i++) {
  2363. const key = arr[i];
  2364. output += `${i > 0 ? ',' : ''}${separator}${serialize(key, indent, baseIndent + indent)}`;
  2365. }
  2366. return `${output}${indent ? `\n${baseIndent}` : ''}]`;
  2367. }
  2368. function serializeObject(obj, indent, baseIndent) {
  2369. let output = '{';
  2370. const separator = indent ? `\n${baseIndent}${indent}` : '';
  2371. const entries = Object.entries(obj);
  2372. for (let i = 0; i < entries.length; i++) {
  2373. const [key, value] = entries[i];
  2374. const stringKey = makeLegalIdentifier(key) === key ? key : stringify$8(key);
  2375. output += `${i > 0 ? ',' : ''}${separator}${stringKey}:${indent ? ' ' : ''}${serialize(value, indent, baseIndent + indent)}`;
  2376. }
  2377. return `${output}${indent ? `\n${baseIndent}` : ''}}`;
  2378. }
  2379. function serialize(obj, indent, baseIndent) {
  2380. if (typeof obj === 'object' && obj !== null) {
  2381. if (Array.isArray(obj))
  2382. return serializeArray(obj, indent, baseIndent);
  2383. if (obj instanceof Date)
  2384. return `new Date(${obj.getTime()})`;
  2385. if (obj instanceof RegExp)
  2386. return obj.toString();
  2387. return serializeObject(obj, indent, baseIndent);
  2388. }
  2389. if (typeof obj === 'number') {
  2390. if (obj === Infinity)
  2391. return 'Infinity';
  2392. if (obj === -Infinity)
  2393. return '-Infinity';
  2394. if (obj === 0)
  2395. return 1 / obj === Infinity ? '0' : '-0';
  2396. if (obj !== obj)
  2397. return 'NaN'; // eslint-disable-line no-self-compare
  2398. }
  2399. if (typeof obj === 'symbol') {
  2400. const key = Symbol.keyFor(obj);
  2401. // eslint-disable-next-line no-undefined
  2402. if (key !== undefined)
  2403. return `Symbol.for(${stringify$8(key)})`;
  2404. }
  2405. if (typeof obj === 'bigint')
  2406. return `${obj}n`;
  2407. return stringify$8(obj);
  2408. }
  2409. // isWellFormed exists from Node.js 20
  2410. const hasStringIsWellFormed = 'isWellFormed' in String.prototype;
  2411. function isWellFormedString(input) {
  2412. // @ts-expect-error String::isWellFormed exists from ES2024. tsconfig lib is set to ES6
  2413. if (hasStringIsWellFormed)
  2414. return input.isWellFormed();
  2415. // https://github.com/tc39/proposal-is-usv-string/blob/main/README.md#algorithm
  2416. return !/\p{Surrogate}/u.test(input);
  2417. }
  2418. const dataToEsm = function dataToEsm(data, options = {}) {
  2419. var _a, _b;
  2420. const t = options.compact ? '' : 'indent' in options ? options.indent : '\t';
  2421. const _ = options.compact ? '' : ' ';
  2422. const n = options.compact ? '' : '\n';
  2423. const declarationType = options.preferConst ? 'const' : 'var';
  2424. if (options.namedExports === false ||
  2425. typeof data !== 'object' ||
  2426. Array.isArray(data) ||
  2427. data instanceof Date ||
  2428. data instanceof RegExp ||
  2429. data === null) {
  2430. const code = serialize(data, options.compact ? null : t, '');
  2431. const magic = _ || (/^[{[\-\/]/.test(code) ? '' : ' '); // eslint-disable-line no-useless-escape
  2432. return `export default${magic}${code};`;
  2433. }
  2434. let maxUnderbarPrefixLength = 0;
  2435. for (const key of Object.keys(data)) {
  2436. const underbarPrefixLength = (_b = (_a = key.match(/^(_+)/)) === null || _a === void 0 ? void 0 : _a[0].length) !== null && _b !== void 0 ? _b : 0;
  2437. if (underbarPrefixLength > maxUnderbarPrefixLength) {
  2438. maxUnderbarPrefixLength = underbarPrefixLength;
  2439. }
  2440. }
  2441. const arbitraryNamePrefix = `${'_'.repeat(maxUnderbarPrefixLength + 1)}arbitrary`;
  2442. let namedExportCode = '';
  2443. const defaultExportRows = [];
  2444. const arbitraryNameExportRows = [];
  2445. for (const [key, value] of Object.entries(data)) {
  2446. if (key === makeLegalIdentifier(key)) {
  2447. if (options.objectShorthand)
  2448. defaultExportRows.push(key);
  2449. else
  2450. defaultExportRows.push(`${key}:${_}${key}`);
  2451. namedExportCode += `export ${declarationType} ${key}${_}=${_}${serialize(value, options.compact ? null : t, '')};${n}`;
  2452. }
  2453. else {
  2454. defaultExportRows.push(`${stringify$8(key)}:${_}${serialize(value, options.compact ? null : t, '')}`);
  2455. if (options.includeArbitraryNames && isWellFormedString(key)) {
  2456. const variableName = `${arbitraryNamePrefix}${arbitraryNameExportRows.length}`;
  2457. namedExportCode += `${declarationType} ${variableName}${_}=${_}${serialize(value, options.compact ? null : t, '')};${n}`;
  2458. arbitraryNameExportRows.push(`${variableName} as ${JSON.stringify(key)}`);
  2459. }
  2460. }
  2461. }
  2462. const arbitraryExportCode = arbitraryNameExportRows.length > 0
  2463. ? `export${_}{${n}${t}${arbitraryNameExportRows.join(`,${n}${t}`)}${n}};${n}`
  2464. : '';
  2465. const defaultExportCode = `export default${_}{${n}${t}${defaultExportRows.join(`,${n}${t}`)}${n}};${n}`;
  2466. return `${namedExportCode}${arbitraryExportCode}${defaultExportCode}`;
  2467. };
  2468. var path$k = require$$0$4;
  2469. var commondir = function (basedir, relfiles) {
  2470. if (relfiles) {
  2471. var files = relfiles.map(function (r) {
  2472. return path$k.resolve(basedir, r);
  2473. });
  2474. }
  2475. else {
  2476. var files = basedir;
  2477. }
  2478. var res = files.slice(1).reduce(function (ps, file) {
  2479. if (!file.match(/^([A-Za-z]:)?\/|\\/)) {
  2480. throw new Error('relative path without a basedir');
  2481. }
  2482. var xs = file.split(/\/+|\\+/);
  2483. for (
  2484. var i = 0;
  2485. ps[i] === xs[i] && i < Math.min(ps.length, xs.length);
  2486. i++
  2487. );
  2488. return ps.slice(0, i);
  2489. }, files[0].split(/\/+|\\+/));
  2490. // Windows correctly handles paths with forward-slashes
  2491. return res.length > 1 ? res.join('/') : '/'
  2492. };
  2493. var getCommonDir = /*@__PURE__*/getDefaultExportFromCjs(commondir);
  2494. var balancedMatch = balanced$1;
  2495. function balanced$1(a, b, str) {
  2496. if (a instanceof RegExp) a = maybeMatch(a, str);
  2497. if (b instanceof RegExp) b = maybeMatch(b, str);
  2498. var r = range$1(a, b, str);
  2499. return r && {
  2500. start: r[0],
  2501. end: r[1],
  2502. pre: str.slice(0, r[0]),
  2503. body: str.slice(r[0] + a.length, r[1]),
  2504. post: str.slice(r[1] + b.length)
  2505. };
  2506. }
  2507. function maybeMatch(reg, str) {
  2508. var m = str.match(reg);
  2509. return m ? m[0] : null;
  2510. }
  2511. balanced$1.range = range$1;
  2512. function range$1(a, b, str) {
  2513. var begs, beg, left, right, result;
  2514. var ai = str.indexOf(a);
  2515. var bi = str.indexOf(b, ai + 1);
  2516. var i = ai;
  2517. if (ai >= 0 && bi > 0) {
  2518. if(a===b) {
  2519. return [ai, bi];
  2520. }
  2521. begs = [];
  2522. left = str.length;
  2523. while (i >= 0 && !result) {
  2524. if (i == ai) {
  2525. begs.push(i);
  2526. ai = str.indexOf(a, i + 1);
  2527. } else if (begs.length == 1) {
  2528. result = [ begs.pop(), bi ];
  2529. } else {
  2530. beg = begs.pop();
  2531. if (beg < left) {
  2532. left = beg;
  2533. right = bi;
  2534. }
  2535. bi = str.indexOf(b, i + 1);
  2536. }
  2537. i = ai < bi && ai >= 0 ? ai : bi;
  2538. }
  2539. if (begs.length) {
  2540. result = [ left, right ];
  2541. }
  2542. }
  2543. return result;
  2544. }
  2545. var balanced = balancedMatch;
  2546. var braceExpansion = expandTop;
  2547. var escSlash = '\0SLASH'+Math.random()+'\0';
  2548. var escOpen = '\0OPEN'+Math.random()+'\0';
  2549. var escClose = '\0CLOSE'+Math.random()+'\0';
  2550. var escComma = '\0COMMA'+Math.random()+'\0';
  2551. var escPeriod = '\0PERIOD'+Math.random()+'\0';
  2552. function numeric(str) {
  2553. return parseInt(str, 10) == str
  2554. ? parseInt(str, 10)
  2555. : str.charCodeAt(0);
  2556. }
  2557. function escapeBraces(str) {
  2558. return str.split('\\\\').join(escSlash)
  2559. .split('\\{').join(escOpen)
  2560. .split('\\}').join(escClose)
  2561. .split('\\,').join(escComma)
  2562. .split('\\.').join(escPeriod);
  2563. }
  2564. function unescapeBraces(str) {
  2565. return str.split(escSlash).join('\\')
  2566. .split(escOpen).join('{')
  2567. .split(escClose).join('}')
  2568. .split(escComma).join(',')
  2569. .split(escPeriod).join('.');
  2570. }
  2571. // Basically just str.split(","), but handling cases
  2572. // where we have nested braced sections, which should be
  2573. // treated as individual members, like {a,{b,c},d}
  2574. function parseCommaParts(str) {
  2575. if (!str)
  2576. return [''];
  2577. var parts = [];
  2578. var m = balanced('{', '}', str);
  2579. if (!m)
  2580. return str.split(',');
  2581. var pre = m.pre;
  2582. var body = m.body;
  2583. var post = m.post;
  2584. var p = pre.split(',');
  2585. p[p.length-1] += '{' + body + '}';
  2586. var postParts = parseCommaParts(post);
  2587. if (post.length) {
  2588. p[p.length-1] += postParts.shift();
  2589. p.push.apply(p, postParts);
  2590. }
  2591. parts.push.apply(parts, p);
  2592. return parts;
  2593. }
  2594. function expandTop(str) {
  2595. if (!str)
  2596. return [];
  2597. // I don't know why Bash 4.3 does this, but it does.
  2598. // Anything starting with {} will have the first two bytes preserved
  2599. // but *only* at the top level, so {},a}b will not expand to anything,
  2600. // but a{},b}c will be expanded to [a}c,abc].
  2601. // One could argue that this is a bug in Bash, but since the goal of
  2602. // this module is to match Bash's rules, we escape a leading {}
  2603. if (str.substr(0, 2) === '{}') {
  2604. str = '\\{\\}' + str.substr(2);
  2605. }
  2606. return expand$3(escapeBraces(str), true).map(unescapeBraces);
  2607. }
  2608. function embrace(str) {
  2609. return '{' + str + '}';
  2610. }
  2611. function isPadded(el) {
  2612. return /^-?0\d/.test(el);
  2613. }
  2614. function lte(i, y) {
  2615. return i <= y;
  2616. }
  2617. function gte(i, y) {
  2618. return i >= y;
  2619. }
  2620. function expand$3(str, isTop) {
  2621. var expansions = [];
  2622. var m = balanced('{', '}', str);
  2623. if (!m) return [str];
  2624. // no need to expand pre, since it is guaranteed to be free of brace-sets
  2625. var pre = m.pre;
  2626. var post = m.post.length
  2627. ? expand$3(m.post, false)
  2628. : [''];
  2629. if (/\$$/.test(m.pre)) {
  2630. for (var k = 0; k < post.length; k++) {
  2631. var expansion = pre+ '{' + m.body + '}' + post[k];
  2632. expansions.push(expansion);
  2633. }
  2634. } else {
  2635. var isNumericSequence = /^-?\d+\.\.-?\d+(?:\.\.-?\d+)?$/.test(m.body);
  2636. var isAlphaSequence = /^[a-zA-Z]\.\.[a-zA-Z](?:\.\.-?\d+)?$/.test(m.body);
  2637. var isSequence = isNumericSequence || isAlphaSequence;
  2638. var isOptions = m.body.indexOf(',') >= 0;
  2639. if (!isSequence && !isOptions) {
  2640. // {a},b}
  2641. if (m.post.match(/,.*\}/)) {
  2642. str = m.pre + '{' + m.body + escClose + m.post;
  2643. return expand$3(str);
  2644. }
  2645. return [str];
  2646. }
  2647. var n;
  2648. if (isSequence) {
  2649. n = m.body.split(/\.\./);
  2650. } else {
  2651. n = parseCommaParts(m.body);
  2652. if (n.length === 1) {
  2653. // x{{a,b}}y ==> x{a}y x{b}y
  2654. n = expand$3(n[0], false).map(embrace);
  2655. if (n.length === 1) {
  2656. return post.map(function(p) {
  2657. return m.pre + n[0] + p;
  2658. });
  2659. }
  2660. }
  2661. }
  2662. // at this point, n is the parts, and we know it's not a comma set
  2663. // with a single entry.
  2664. var N;
  2665. if (isSequence) {
  2666. var x = numeric(n[0]);
  2667. var y = numeric(n[1]);
  2668. var width = Math.max(n[0].length, n[1].length);
  2669. var incr = n.length == 3
  2670. ? Math.abs(numeric(n[2]))
  2671. : 1;
  2672. var test = lte;
  2673. var reverse = y < x;
  2674. if (reverse) {
  2675. incr *= -1;
  2676. test = gte;
  2677. }
  2678. var pad = n.some(isPadded);
  2679. N = [];
  2680. for (var i = x; test(i, y); i += incr) {
  2681. var c;
  2682. if (isAlphaSequence) {
  2683. c = String.fromCharCode(i);
  2684. if (c === '\\')
  2685. c = '';
  2686. } else {
  2687. c = String(i);
  2688. if (pad) {
  2689. var need = width - c.length;
  2690. if (need > 0) {
  2691. var z = new Array(need + 1).join('0');
  2692. if (i < 0)
  2693. c = '-' + z + c.slice(1);
  2694. else
  2695. c = z + c;
  2696. }
  2697. }
  2698. }
  2699. N.push(c);
  2700. }
  2701. } else {
  2702. N = [];
  2703. for (var j = 0; j < n.length; j++) {
  2704. N.push.apply(N, expand$3(n[j], false));
  2705. }
  2706. }
  2707. for (var j = 0; j < N.length; j++) {
  2708. for (var k = 0; k < post.length; k++) {
  2709. var expansion = pre + N[j] + post[k];
  2710. if (!isTop || isSequence || expansion)
  2711. expansions.push(expansion);
  2712. }
  2713. }
  2714. }
  2715. return expansions;
  2716. }
  2717. var expand$4 = /*@__PURE__*/getDefaultExportFromCjs(braceExpansion);
  2718. const MAX_PATTERN_LENGTH = 1024 * 64;
  2719. const assertValidPattern = (pattern) => {
  2720. if (typeof pattern !== 'string') {
  2721. throw new TypeError('invalid pattern');
  2722. }
  2723. if (pattern.length > MAX_PATTERN_LENGTH) {
  2724. throw new TypeError('pattern is too long');
  2725. }
  2726. };
  2727. // translate the various posix character classes into unicode properties
  2728. // this works across all unicode locales
  2729. // { <posix class>: [<translation>, /u flag required, negated]
  2730. const posixClasses = {
  2731. '[:alnum:]': ['\\p{L}\\p{Nl}\\p{Nd}', true],
  2732. '[:alpha:]': ['\\p{L}\\p{Nl}', true],
  2733. '[:ascii:]': ['\\x' + '00-\\x' + '7f', false],
  2734. '[:blank:]': ['\\p{Zs}\\t', true],
  2735. '[:cntrl:]': ['\\p{Cc}', true],
  2736. '[:digit:]': ['\\p{Nd}', true],
  2737. '[:graph:]': ['\\p{Z}\\p{C}', true, true],
  2738. '[:lower:]': ['\\p{Ll}', true],
  2739. '[:print:]': ['\\p{C}', true],
  2740. '[:punct:]': ['\\p{P}', true],
  2741. '[:space:]': ['\\p{Z}\\t\\r\\n\\v\\f', true],
  2742. '[:upper:]': ['\\p{Lu}', true],
  2743. '[:word:]': ['\\p{L}\\p{Nl}\\p{Nd}\\p{Pc}', true],
  2744. '[:xdigit:]': ['A-Fa-f0-9', false],
  2745. };
  2746. // only need to escape a few things inside of brace expressions
  2747. // escapes: [ \ ] -
  2748. const braceEscape = (s) => s.replace(/[[\]\\-]/g, '\\$&');
  2749. // escape all regexp magic characters
  2750. const regexpEscape = (s) => s.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
  2751. // everything has already been escaped, we just have to join
  2752. const rangesToString = (ranges) => ranges.join('');
  2753. // takes a glob string at a posix brace expression, and returns
  2754. // an equivalent regular expression source, and boolean indicating
  2755. // whether the /u flag needs to be applied, and the number of chars
  2756. // consumed to parse the character class.
  2757. // This also removes out of order ranges, and returns ($.) if the
  2758. // entire class just no good.
  2759. const parseClass = (glob, position) => {
  2760. const pos = position;
  2761. /* c8 ignore start */
  2762. if (glob.charAt(pos) !== '[') {
  2763. throw new Error('not in a brace expression');
  2764. }
  2765. /* c8 ignore stop */
  2766. const ranges = [];
  2767. const negs = [];
  2768. let i = pos + 1;
  2769. let sawStart = false;
  2770. let uflag = false;
  2771. let escaping = false;
  2772. let negate = false;
  2773. let endPos = pos;
  2774. let rangeStart = '';
  2775. WHILE: while (i < glob.length) {
  2776. const c = glob.charAt(i);
  2777. if ((c === '!' || c === '^') && i === pos + 1) {
  2778. negate = true;
  2779. i++;
  2780. continue;
  2781. }
  2782. if (c === ']' && sawStart && !escaping) {
  2783. endPos = i + 1;
  2784. break;
  2785. }
  2786. sawStart = true;
  2787. if (c === '\\') {
  2788. if (!escaping) {
  2789. escaping = true;
  2790. i++;
  2791. continue;
  2792. }
  2793. // escaped \ char, fall through and treat like normal char
  2794. }
  2795. if (c === '[' && !escaping) {
  2796. // either a posix class, a collation equivalent, or just a [
  2797. for (const [cls, [unip, u, neg]] of Object.entries(posixClasses)) {
  2798. if (glob.startsWith(cls, i)) {
  2799. // invalid, [a-[] is fine, but not [a-[:alpha]]
  2800. if (rangeStart) {
  2801. return ['$.', false, glob.length - pos, true];
  2802. }
  2803. i += cls.length;
  2804. if (neg)
  2805. negs.push(unip);
  2806. else
  2807. ranges.push(unip);
  2808. uflag = uflag || u;
  2809. continue WHILE;
  2810. }
  2811. }
  2812. }
  2813. // now it's just a normal character, effectively
  2814. escaping = false;
  2815. if (rangeStart) {
  2816. // throw this range away if it's not valid, but others
  2817. // can still match.
  2818. if (c > rangeStart) {
  2819. ranges.push(braceEscape(rangeStart) + '-' + braceEscape(c));
  2820. }
  2821. else if (c === rangeStart) {
  2822. ranges.push(braceEscape(c));
  2823. }
  2824. rangeStart = '';
  2825. i++;
  2826. continue;
  2827. }
  2828. // now might be the start of a range.
  2829. // can be either c-d or c-] or c<more...>] or c] at this point
  2830. if (glob.startsWith('-]', i + 1)) {
  2831. ranges.push(braceEscape(c + '-'));
  2832. i += 2;
  2833. continue;
  2834. }
  2835. if (glob.startsWith('-', i + 1)) {
  2836. rangeStart = c;
  2837. i += 2;
  2838. continue;
  2839. }
  2840. // not the start of a range, just a single character
  2841. ranges.push(braceEscape(c));
  2842. i++;
  2843. }
  2844. if (endPos < i) {
  2845. // didn't see the end of the class, not a valid class,
  2846. // but might still be valid as a literal match.
  2847. return ['', false, 0, false];
  2848. }
  2849. // if we got no ranges and no negates, then we have a range that
  2850. // cannot possibly match anything, and that poisons the whole glob
  2851. if (!ranges.length && !negs.length) {
  2852. return ['$.', false, glob.length - pos, true];
  2853. }
  2854. // if we got one positive range, and it's a single character, then that's
  2855. // not actually a magic pattern, it's just that one literal character.
  2856. // we should not treat that as "magic", we should just return the literal
  2857. // character. [_] is a perfectly valid way to escape glob magic chars.
  2858. if (negs.length === 0 &&
  2859. ranges.length === 1 &&
  2860. /^\\?.$/.test(ranges[0]) &&
  2861. !negate) {
  2862. const r = ranges[0].length === 2 ? ranges[0].slice(-1) : ranges[0];
  2863. return [regexpEscape(r), false, endPos - pos, false];
  2864. }
  2865. const sranges = '[' + (negate ? '^' : '') + rangesToString(ranges) + ']';
  2866. const snegs = '[' + (negate ? '' : '^') + rangesToString(negs) + ']';
  2867. const comb = ranges.length && negs.length
  2868. ? '(' + sranges + '|' + snegs + ')'
  2869. : ranges.length
  2870. ? sranges
  2871. : snegs;
  2872. return [comb, uflag, endPos - pos, true];
  2873. };
  2874. /**
  2875. * Un-escape a string that has been escaped with {@link escape}.
  2876. *
  2877. * If the {@link windowsPathsNoEscape} option is used, then square-brace
  2878. * escapes are removed, but not backslash escapes. For example, it will turn
  2879. * the string `'[*]'` into `*`, but it will not turn `'\\*'` into `'*'`,
  2880. * becuase `\` is a path separator in `windowsPathsNoEscape` mode.
  2881. *
  2882. * When `windowsPathsNoEscape` is not set, then both brace escapes and
  2883. * backslash escapes are removed.
  2884. *
  2885. * Slashes (and backslashes in `windowsPathsNoEscape` mode) cannot be escaped
  2886. * or unescaped.
  2887. */
  2888. const unescape$1 = (s, { windowsPathsNoEscape = false, } = {}) => {
  2889. return windowsPathsNoEscape
  2890. ? s.replace(/\[([^\/\\])\]/g, '$1')
  2891. : s.replace(/((?!\\).|^)\[([^\/\\])\]/g, '$1$2').replace(/\\([^\/])/g, '$1');
  2892. };
  2893. // parse a single path portion
  2894. const types$1 = new Set(['!', '?', '+', '*', '@']);
  2895. const isExtglobType = (c) => types$1.has(c);
  2896. // Patterns that get prepended to bind to the start of either the
  2897. // entire string, or just a single path portion, to prevent dots
  2898. // and/or traversal patterns, when needed.
  2899. // Exts don't need the ^ or / bit, because the root binds that already.
  2900. const startNoTraversal = '(?!(?:^|/)\\.\\.?(?:$|/))';
  2901. const startNoDot = '(?!\\.)';
  2902. // characters that indicate a start of pattern needs the "no dots" bit,
  2903. // because a dot *might* be matched. ( is not in the list, because in
  2904. // the case of a child extglob, it will handle the prevention itself.
  2905. const addPatternStart = new Set(['[', '.']);
  2906. // cases where traversal is A-OK, no dot prevention needed
  2907. const justDots = new Set(['..', '.']);
  2908. const reSpecials = new Set('().*{}+?[]^$\\!');
  2909. const regExpEscape$1 = (s) => s.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
  2910. // any single thing other than /
  2911. const qmark$1 = '[^/]';
  2912. // * => any number of characters
  2913. const star$1 = qmark$1 + '*?';
  2914. // use + when we need to ensure that *something* matches, because the * is
  2915. // the only thing in the path portion.
  2916. const starNoEmpty = qmark$1 + '+?';
  2917. // remove the \ chars that we added if we end up doing a nonmagic compare
  2918. // const deslash = (s: string) => s.replace(/\\(.)/g, '$1')
  2919. class AST {
  2920. type;
  2921. #root;
  2922. #hasMagic;
  2923. #uflag = false;
  2924. #parts = [];
  2925. #parent;
  2926. #parentIndex;
  2927. #negs;
  2928. #filledNegs = false;
  2929. #options;
  2930. #toString;
  2931. // set to true if it's an extglob with no children
  2932. // (which really means one child of '')
  2933. #emptyExt = false;
  2934. constructor(type, parent, options = {}) {
  2935. this.type = type;
  2936. // extglobs are inherently magical
  2937. if (type)
  2938. this.#hasMagic = true;
  2939. this.#parent = parent;
  2940. this.#root = this.#parent ? this.#parent.#root : this;
  2941. this.#options = this.#root === this ? options : this.#root.#options;
  2942. this.#negs = this.#root === this ? [] : this.#root.#negs;
  2943. if (type === '!' && !this.#root.#filledNegs)
  2944. this.#negs.push(this);
  2945. this.#parentIndex = this.#parent ? this.#parent.#parts.length : 0;
  2946. }
  2947. get hasMagic() {
  2948. /* c8 ignore start */
  2949. if (this.#hasMagic !== undefined)
  2950. return this.#hasMagic;
  2951. /* c8 ignore stop */
  2952. for (const p of this.#parts) {
  2953. if (typeof p === 'string')
  2954. continue;
  2955. if (p.type || p.hasMagic)
  2956. return (this.#hasMagic = true);
  2957. }
  2958. // note: will be undefined until we generate the regexp src and find out
  2959. return this.#hasMagic;
  2960. }
  2961. // reconstructs the pattern
  2962. toString() {
  2963. if (this.#toString !== undefined)
  2964. return this.#toString;
  2965. if (!this.type) {
  2966. return (this.#toString = this.#parts.map(p => String(p)).join(''));
  2967. }
  2968. else {
  2969. return (this.#toString =
  2970. this.type + '(' + this.#parts.map(p => String(p)).join('|') + ')');
  2971. }
  2972. }
  2973. #fillNegs() {
  2974. /* c8 ignore start */
  2975. if (this !== this.#root)
  2976. throw new Error('should only call on root');
  2977. if (this.#filledNegs)
  2978. return this;
  2979. /* c8 ignore stop */
  2980. // call toString() once to fill this out
  2981. this.toString();
  2982. this.#filledNegs = true;
  2983. let n;
  2984. while ((n = this.#negs.pop())) {
  2985. if (n.type !== '!')
  2986. continue;
  2987. // walk up the tree, appending everthing that comes AFTER parentIndex
  2988. let p = n;
  2989. let pp = p.#parent;
  2990. while (pp) {
  2991. for (let i = p.#parentIndex + 1; !pp.type && i < pp.#parts.length; i++) {
  2992. for (const part of n.#parts) {
  2993. /* c8 ignore start */
  2994. if (typeof part === 'string') {
  2995. throw new Error('string part in extglob AST??');
  2996. }
  2997. /* c8 ignore stop */
  2998. part.copyIn(pp.#parts[i]);
  2999. }
  3000. }
  3001. p = pp;
  3002. pp = p.#parent;
  3003. }
  3004. }
  3005. return this;
  3006. }
  3007. push(...parts) {
  3008. for (const p of parts) {
  3009. if (p === '')
  3010. continue;
  3011. /* c8 ignore start */
  3012. if (typeof p !== 'string' && !(p instanceof AST && p.#parent === this)) {
  3013. throw new Error('invalid part: ' + p);
  3014. }
  3015. /* c8 ignore stop */
  3016. this.#parts.push(p);
  3017. }
  3018. }
  3019. toJSON() {
  3020. const ret = this.type === null
  3021. ? this.#parts.slice().map(p => (typeof p === 'string' ? p : p.toJSON()))
  3022. : [this.type, ...this.#parts.map(p => p.toJSON())];
  3023. if (this.isStart() && !this.type)
  3024. ret.unshift([]);
  3025. if (this.isEnd() &&
  3026. (this === this.#root ||
  3027. (this.#root.#filledNegs && this.#parent?.type === '!'))) {
  3028. ret.push({});
  3029. }
  3030. return ret;
  3031. }
  3032. isStart() {
  3033. if (this.#root === this)
  3034. return true;
  3035. // if (this.type) return !!this.#parent?.isStart()
  3036. if (!this.#parent?.isStart())
  3037. return false;
  3038. if (this.#parentIndex === 0)
  3039. return true;
  3040. // if everything AHEAD of this is a negation, then it's still the "start"
  3041. const p = this.#parent;
  3042. for (let i = 0; i < this.#parentIndex; i++) {
  3043. const pp = p.#parts[i];
  3044. if (!(pp instanceof AST && pp.type === '!')) {
  3045. return false;
  3046. }
  3047. }
  3048. return true;
  3049. }
  3050. isEnd() {
  3051. if (this.#root === this)
  3052. return true;
  3053. if (this.#parent?.type === '!')
  3054. return true;
  3055. if (!this.#parent?.isEnd())
  3056. return false;
  3057. if (!this.type)
  3058. return this.#parent?.isEnd();
  3059. // if not root, it'll always have a parent
  3060. /* c8 ignore start */
  3061. const pl = this.#parent ? this.#parent.#parts.length : 0;
  3062. /* c8 ignore stop */
  3063. return this.#parentIndex === pl - 1;
  3064. }
  3065. copyIn(part) {
  3066. if (typeof part === 'string')
  3067. this.push(part);
  3068. else
  3069. this.push(part.clone(this));
  3070. }
  3071. clone(parent) {
  3072. const c = new AST(this.type, parent);
  3073. for (const p of this.#parts) {
  3074. c.copyIn(p);
  3075. }
  3076. return c;
  3077. }
  3078. static #parseAST(str, ast, pos, opt) {
  3079. let escaping = false;
  3080. let inBrace = false;
  3081. let braceStart = -1;
  3082. let braceNeg = false;
  3083. if (ast.type === null) {
  3084. // outside of a extglob, append until we find a start
  3085. let i = pos;
  3086. let acc = '';
  3087. while (i < str.length) {
  3088. const c = str.charAt(i++);
  3089. // still accumulate escapes at this point, but we do ignore
  3090. // starts that are escaped
  3091. if (escaping || c === '\\') {
  3092. escaping = !escaping;
  3093. acc += c;
  3094. continue;
  3095. }
  3096. if (inBrace) {
  3097. if (i === braceStart + 1) {
  3098. if (c === '^' || c === '!') {
  3099. braceNeg = true;
  3100. }
  3101. }
  3102. else if (c === ']' && !(i === braceStart + 2 && braceNeg)) {
  3103. inBrace = false;
  3104. }
  3105. acc += c;
  3106. continue;
  3107. }
  3108. else if (c === '[') {
  3109. inBrace = true;
  3110. braceStart = i;
  3111. braceNeg = false;
  3112. acc += c;
  3113. continue;
  3114. }
  3115. if (!opt.noext && isExtglobType(c) && str.charAt(i) === '(') {
  3116. ast.push(acc);
  3117. acc = '';
  3118. const ext = new AST(c, ast);
  3119. i = AST.#parseAST(str, ext, i, opt);
  3120. ast.push(ext);
  3121. continue;
  3122. }
  3123. acc += c;
  3124. }
  3125. ast.push(acc);
  3126. return i;
  3127. }
  3128. // some kind of extglob, pos is at the (
  3129. // find the next | or )
  3130. let i = pos + 1;
  3131. let part = new AST(null, ast);
  3132. const parts = [];
  3133. let acc = '';
  3134. while (i < str.length) {
  3135. const c = str.charAt(i++);
  3136. // still accumulate escapes at this point, but we do ignore
  3137. // starts that are escaped
  3138. if (escaping || c === '\\') {
  3139. escaping = !escaping;
  3140. acc += c;
  3141. continue;
  3142. }
  3143. if (inBrace) {
  3144. if (i === braceStart + 1) {
  3145. if (c === '^' || c === '!') {
  3146. braceNeg = true;
  3147. }
  3148. }
  3149. else if (c === ']' && !(i === braceStart + 2 && braceNeg)) {
  3150. inBrace = false;
  3151. }
  3152. acc += c;
  3153. continue;
  3154. }
  3155. else if (c === '[') {
  3156. inBrace = true;
  3157. braceStart = i;
  3158. braceNeg = false;
  3159. acc += c;
  3160. continue;
  3161. }
  3162. if (isExtglobType(c) && str.charAt(i) === '(') {
  3163. part.push(acc);
  3164. acc = '';
  3165. const ext = new AST(c, part);
  3166. part.push(ext);
  3167. i = AST.#parseAST(str, ext, i, opt);
  3168. continue;
  3169. }
  3170. if (c === '|') {
  3171. part.push(acc);
  3172. acc = '';
  3173. parts.push(part);
  3174. part = new AST(null, ast);
  3175. continue;
  3176. }
  3177. if (c === ')') {
  3178. if (acc === '' && ast.#parts.length === 0) {
  3179. ast.#emptyExt = true;
  3180. }
  3181. part.push(acc);
  3182. acc = '';
  3183. ast.push(...parts, part);
  3184. return i;
  3185. }
  3186. acc += c;
  3187. }
  3188. // unfinished extglob
  3189. // if we got here, it was a malformed extglob! not an extglob, but
  3190. // maybe something else in there.
  3191. ast.type = null;
  3192. ast.#hasMagic = undefined;
  3193. ast.#parts = [str.substring(pos - 1)];
  3194. return i;
  3195. }
  3196. static fromGlob(pattern, options = {}) {
  3197. const ast = new AST(null, undefined, options);
  3198. AST.#parseAST(pattern, ast, 0, options);
  3199. return ast;
  3200. }
  3201. // returns the regular expression if there's magic, or the unescaped
  3202. // string if not.
  3203. toMMPattern() {
  3204. // should only be called on root
  3205. /* c8 ignore start */
  3206. if (this !== this.#root)
  3207. return this.#root.toMMPattern();
  3208. /* c8 ignore stop */
  3209. const glob = this.toString();
  3210. const [re, body, hasMagic, uflag] = this.toRegExpSource();
  3211. // if we're in nocase mode, and not nocaseMagicOnly, then we do
  3212. // still need a regular expression if we have to case-insensitively
  3213. // match capital/lowercase characters.
  3214. const anyMagic = hasMagic ||
  3215. this.#hasMagic ||
  3216. (this.#options.nocase &&
  3217. !this.#options.nocaseMagicOnly &&
  3218. glob.toUpperCase() !== glob.toLowerCase());
  3219. if (!anyMagic) {
  3220. return body;
  3221. }
  3222. const flags = (this.#options.nocase ? 'i' : '') + (uflag ? 'u' : '');
  3223. return Object.assign(new RegExp(`^${re}$`, flags), {
  3224. _src: re,
  3225. _glob: glob,
  3226. });
  3227. }
  3228. get options() {
  3229. return this.#options;
  3230. }
  3231. // returns the string match, the regexp source, whether there's magic
  3232. // in the regexp (so a regular expression is required) and whether or
  3233. // not the uflag is needed for the regular expression (for posix classes)
  3234. // TODO: instead of injecting the start/end at this point, just return
  3235. // the BODY of the regexp, along with the start/end portions suitable
  3236. // for binding the start/end in either a joined full-path makeRe context
  3237. // (where we bind to (^|/), or a standalone matchPart context (where
  3238. // we bind to ^, and not /). Otherwise slashes get duped!
  3239. //
  3240. // In part-matching mode, the start is:
  3241. // - if not isStart: nothing
  3242. // - if traversal possible, but not allowed: ^(?!\.\.?$)
  3243. // - if dots allowed or not possible: ^
  3244. // - if dots possible and not allowed: ^(?!\.)
  3245. // end is:
  3246. // - if not isEnd(): nothing
  3247. // - else: $
  3248. //
  3249. // In full-path matching mode, we put the slash at the START of the
  3250. // pattern, so start is:
  3251. // - if first pattern: same as part-matching mode
  3252. // - if not isStart(): nothing
  3253. // - if traversal possible, but not allowed: /(?!\.\.?(?:$|/))
  3254. // - if dots allowed or not possible: /
  3255. // - if dots possible and not allowed: /(?!\.)
  3256. // end is:
  3257. // - if last pattern, same as part-matching mode
  3258. // - else nothing
  3259. //
  3260. // Always put the (?:$|/) on negated tails, though, because that has to be
  3261. // there to bind the end of the negated pattern portion, and it's easier to
  3262. // just stick it in now rather than try to inject it later in the middle of
  3263. // the pattern.
  3264. //
  3265. // We can just always return the same end, and leave it up to the caller
  3266. // to know whether it's going to be used joined or in parts.
  3267. // And, if the start is adjusted slightly, can do the same there:
  3268. // - if not isStart: nothing
  3269. // - if traversal possible, but not allowed: (?:/|^)(?!\.\.?$)
  3270. // - if dots allowed or not possible: (?:/|^)
  3271. // - if dots possible and not allowed: (?:/|^)(?!\.)
  3272. //
  3273. // But it's better to have a simpler binding without a conditional, for
  3274. // performance, so probably better to return both start options.
  3275. //
  3276. // Then the caller just ignores the end if it's not the first pattern,
  3277. // and the start always gets applied.
  3278. //
  3279. // But that's always going to be $ if it's the ending pattern, or nothing,
  3280. // so the caller can just attach $ at the end of the pattern when building.
  3281. //
  3282. // So the todo is:
  3283. // - better detect what kind of start is needed
  3284. // - return both flavors of starting pattern
  3285. // - attach $ at the end of the pattern when creating the actual RegExp
  3286. //
  3287. // Ah, but wait, no, that all only applies to the root when the first pattern
  3288. // is not an extglob. If the first pattern IS an extglob, then we need all
  3289. // that dot prevention biz to live in the extglob portions, because eg
  3290. // +(*|.x*) can match .xy but not .yx.
  3291. //
  3292. // So, return the two flavors if it's #root and the first child is not an
  3293. // AST, otherwise leave it to the child AST to handle it, and there,
  3294. // use the (?:^|/) style of start binding.
  3295. //
  3296. // Even simplified further:
  3297. // - Since the start for a join is eg /(?!\.) and the start for a part
  3298. // is ^(?!\.), we can just prepend (?!\.) to the pattern (either root
  3299. // or start or whatever) and prepend ^ or / at the Regexp construction.
  3300. toRegExpSource(allowDot) {
  3301. const dot = allowDot ?? !!this.#options.dot;
  3302. if (this.#root === this)
  3303. this.#fillNegs();
  3304. if (!this.type) {
  3305. const noEmpty = this.isStart() && this.isEnd();
  3306. const src = this.#parts
  3307. .map(p => {
  3308. const [re, _, hasMagic, uflag] = typeof p === 'string'
  3309. ? AST.#parseGlob(p, this.#hasMagic, noEmpty)
  3310. : p.toRegExpSource(allowDot);
  3311. this.#hasMagic = this.#hasMagic || hasMagic;
  3312. this.#uflag = this.#uflag || uflag;
  3313. return re;
  3314. })
  3315. .join('');
  3316. let start = '';
  3317. if (this.isStart()) {
  3318. if (typeof this.#parts[0] === 'string') {
  3319. // this is the string that will match the start of the pattern,
  3320. // so we need to protect against dots and such.
  3321. // '.' and '..' cannot match unless the pattern is that exactly,
  3322. // even if it starts with . or dot:true is set.
  3323. const dotTravAllowed = this.#parts.length === 1 && justDots.has(this.#parts[0]);
  3324. if (!dotTravAllowed) {
  3325. const aps = addPatternStart;
  3326. // check if we have a possibility of matching . or ..,
  3327. // and prevent that.
  3328. const needNoTrav =
  3329. // dots are allowed, and the pattern starts with [ or .
  3330. (dot && aps.has(src.charAt(0))) ||
  3331. // the pattern starts with \., and then [ or .
  3332. (src.startsWith('\\.') && aps.has(src.charAt(2))) ||
  3333. // the pattern starts with \.\., and then [ or .
  3334. (src.startsWith('\\.\\.') && aps.has(src.charAt(4)));
  3335. // no need to prevent dots if it can't match a dot, or if a
  3336. // sub-pattern will be preventing it anyway.
  3337. const needNoDot = !dot && !allowDot && aps.has(src.charAt(0));
  3338. start = needNoTrav ? startNoTraversal : needNoDot ? startNoDot : '';
  3339. }
  3340. }
  3341. }
  3342. // append the "end of path portion" pattern to negation tails
  3343. let end = '';
  3344. if (this.isEnd() &&
  3345. this.#root.#filledNegs &&
  3346. this.#parent?.type === '!') {
  3347. end = '(?:$|\\/)';
  3348. }
  3349. const final = start + src + end;
  3350. return [
  3351. final,
  3352. unescape$1(src),
  3353. (this.#hasMagic = !!this.#hasMagic),
  3354. this.#uflag,
  3355. ];
  3356. }
  3357. // We need to calculate the body *twice* if it's a repeat pattern
  3358. // at the start, once in nodot mode, then again in dot mode, so a
  3359. // pattern like *(?) can match 'x.y'
  3360. const repeated = this.type === '*' || this.type === '+';
  3361. // some kind of extglob
  3362. const start = this.type === '!' ? '(?:(?!(?:' : '(?:';
  3363. let body = this.#partsToRegExp(dot);
  3364. if (this.isStart() && this.isEnd() && !body && this.type !== '!') {
  3365. // invalid extglob, has to at least be *something* present, if it's
  3366. // the entire path portion.
  3367. const s = this.toString();
  3368. this.#parts = [s];
  3369. this.type = null;
  3370. this.#hasMagic = undefined;
  3371. return [s, unescape$1(this.toString()), false, false];
  3372. }
  3373. // XXX abstract out this map method
  3374. let bodyDotAllowed = !repeated || allowDot || dot || !startNoDot
  3375. ? ''
  3376. : this.#partsToRegExp(true);
  3377. if (bodyDotAllowed === body) {
  3378. bodyDotAllowed = '';
  3379. }
  3380. if (bodyDotAllowed) {
  3381. body = `(?:${body})(?:${bodyDotAllowed})*?`;
  3382. }
  3383. // an empty !() is exactly equivalent to a starNoEmpty
  3384. let final = '';
  3385. if (this.type === '!' && this.#emptyExt) {
  3386. final = (this.isStart() && !dot ? startNoDot : '') + starNoEmpty;
  3387. }
  3388. else {
  3389. const close = this.type === '!'
  3390. ? // !() must match something,but !(x) can match ''
  3391. '))' +
  3392. (this.isStart() && !dot && !allowDot ? startNoDot : '') +
  3393. star$1 +
  3394. ')'
  3395. : this.type === '@'
  3396. ? ')'
  3397. : this.type === '?'
  3398. ? ')?'
  3399. : this.type === '+' && bodyDotAllowed
  3400. ? ')'
  3401. : this.type === '*' && bodyDotAllowed
  3402. ? `)?`
  3403. : `)${this.type}`;
  3404. final = start + body + close;
  3405. }
  3406. return [
  3407. final,
  3408. unescape$1(body),
  3409. (this.#hasMagic = !!this.#hasMagic),
  3410. this.#uflag,
  3411. ];
  3412. }
  3413. #partsToRegExp(dot) {
  3414. return this.#parts
  3415. .map(p => {
  3416. // extglob ASTs should only contain parent ASTs
  3417. /* c8 ignore start */
  3418. if (typeof p === 'string') {
  3419. throw new Error('string type in extglob ast??');
  3420. }
  3421. /* c8 ignore stop */
  3422. // can ignore hasMagic, because extglobs are already always magic
  3423. const [re, _, _hasMagic, uflag] = p.toRegExpSource(dot);
  3424. this.#uflag = this.#uflag || uflag;
  3425. return re;
  3426. })
  3427. .filter(p => !(this.isStart() && this.isEnd()) || !!p)
  3428. .join('|');
  3429. }
  3430. static #parseGlob(glob, hasMagic, noEmpty = false) {
  3431. let escaping = false;
  3432. let re = '';
  3433. let uflag = false;
  3434. for (let i = 0; i < glob.length; i++) {
  3435. const c = glob.charAt(i);
  3436. if (escaping) {
  3437. escaping = false;
  3438. re += (reSpecials.has(c) ? '\\' : '') + c;
  3439. continue;
  3440. }
  3441. if (c === '\\') {
  3442. if (i === glob.length - 1) {
  3443. re += '\\\\';
  3444. }
  3445. else {
  3446. escaping = true;
  3447. }
  3448. continue;
  3449. }
  3450. if (c === '[') {
  3451. const [src, needUflag, consumed, magic] = parseClass(glob, i);
  3452. if (consumed) {
  3453. re += src;
  3454. uflag = uflag || needUflag;
  3455. i += consumed - 1;
  3456. hasMagic = hasMagic || magic;
  3457. continue;
  3458. }
  3459. }
  3460. if (c === '*') {
  3461. if (noEmpty && glob === '*')
  3462. re += starNoEmpty;
  3463. else
  3464. re += star$1;
  3465. hasMagic = true;
  3466. continue;
  3467. }
  3468. if (c === '?') {
  3469. re += qmark$1;
  3470. hasMagic = true;
  3471. continue;
  3472. }
  3473. re += regExpEscape$1(c);
  3474. }
  3475. return [re, unescape$1(glob), !!hasMagic, uflag];
  3476. }
  3477. }
  3478. /**
  3479. * Escape all magic characters in a glob pattern.
  3480. *
  3481. * If the {@link windowsPathsNoEscape | GlobOptions.windowsPathsNoEscape}
  3482. * option is used, then characters are escaped by wrapping in `[]`, because
  3483. * a magic character wrapped in a character class can only be satisfied by
  3484. * that exact character. In this mode, `\` is _not_ escaped, because it is
  3485. * not interpreted as a magic character, but instead as a path separator.
  3486. */
  3487. const escape$2 = (s, { windowsPathsNoEscape = false, } = {}) => {
  3488. // don't need to escape +@! because we escape the parens
  3489. // that make those magic, and escaping ! as [!] isn't valid,
  3490. // because [!]] is a valid glob class meaning not ']'.
  3491. return windowsPathsNoEscape
  3492. ? s.replace(/[?*()[\]]/g, '[$&]')
  3493. : s.replace(/[?*()[\]\\]/g, '\\$&');
  3494. };
  3495. const minimatch = (p, pattern, options = {}) => {
  3496. assertValidPattern(pattern);
  3497. // shortcut: comments match nothing.
  3498. if (!options.nocomment && pattern.charAt(0) === '#') {
  3499. return false;
  3500. }
  3501. return new Minimatch(pattern, options).match(p);
  3502. };
  3503. // Optimized checking for the most common glob patterns.
  3504. const starDotExtRE = /^\*+([^+@!?\*\[\(]*)$/;
  3505. const starDotExtTest = (ext) => (f) => !f.startsWith('.') && f.endsWith(ext);
  3506. const starDotExtTestDot = (ext) => (f) => f.endsWith(ext);
  3507. const starDotExtTestNocase = (ext) => {
  3508. ext = ext.toLowerCase();
  3509. return (f) => !f.startsWith('.') && f.toLowerCase().endsWith(ext);
  3510. };
  3511. const starDotExtTestNocaseDot = (ext) => {
  3512. ext = ext.toLowerCase();
  3513. return (f) => f.toLowerCase().endsWith(ext);
  3514. };
  3515. const starDotStarRE = /^\*+\.\*+$/;
  3516. const starDotStarTest = (f) => !f.startsWith('.') && f.includes('.');
  3517. const starDotStarTestDot = (f) => f !== '.' && f !== '..' && f.includes('.');
  3518. const dotStarRE = /^\.\*+$/;
  3519. const dotStarTest = (f) => f !== '.' && f !== '..' && f.startsWith('.');
  3520. const starRE = /^\*+$/;
  3521. const starTest = (f) => f.length !== 0 && !f.startsWith('.');
  3522. const starTestDot = (f) => f.length !== 0 && f !== '.' && f !== '..';
  3523. const qmarksRE = /^\?+([^+@!?\*\[\(]*)?$/;
  3524. const qmarksTestNocase = ([$0, ext = '']) => {
  3525. const noext = qmarksTestNoExt([$0]);
  3526. if (!ext)
  3527. return noext;
  3528. ext = ext.toLowerCase();
  3529. return (f) => noext(f) && f.toLowerCase().endsWith(ext);
  3530. };
  3531. const qmarksTestNocaseDot = ([$0, ext = '']) => {
  3532. const noext = qmarksTestNoExtDot([$0]);
  3533. if (!ext)
  3534. return noext;
  3535. ext = ext.toLowerCase();
  3536. return (f) => noext(f) && f.toLowerCase().endsWith(ext);
  3537. };
  3538. const qmarksTestDot = ([$0, ext = '']) => {
  3539. const noext = qmarksTestNoExtDot([$0]);
  3540. return !ext ? noext : (f) => noext(f) && f.endsWith(ext);
  3541. };
  3542. const qmarksTest = ([$0, ext = '']) => {
  3543. const noext = qmarksTestNoExt([$0]);
  3544. return !ext ? noext : (f) => noext(f) && f.endsWith(ext);
  3545. };
  3546. const qmarksTestNoExt = ([$0]) => {
  3547. const len = $0.length;
  3548. return (f) => f.length === len && !f.startsWith('.');
  3549. };
  3550. const qmarksTestNoExtDot = ([$0]) => {
  3551. const len = $0.length;
  3552. return (f) => f.length === len && f !== '.' && f !== '..';
  3553. };
  3554. /* c8 ignore start */
  3555. const defaultPlatform$2 = (typeof process === 'object' && process
  3556. ? (typeof process.env === 'object' &&
  3557. process.env &&
  3558. process.env.__MINIMATCH_TESTING_PLATFORM__) ||
  3559. process.platform
  3560. : 'posix');
  3561. const path$j = {
  3562. win32: { sep: '\\' },
  3563. posix: { sep: '/' },
  3564. };
  3565. /* c8 ignore stop */
  3566. const sep = defaultPlatform$2 === 'win32' ? path$j.win32.sep : path$j.posix.sep;
  3567. minimatch.sep = sep;
  3568. const GLOBSTAR$2 = Symbol('globstar **');
  3569. minimatch.GLOBSTAR = GLOBSTAR$2;
  3570. // any single thing other than /
  3571. // don't need to escape / when using new RegExp()
  3572. const qmark = '[^/]';
  3573. // * => any number of characters
  3574. const star = qmark + '*?';
  3575. // ** when dots are allowed. Anything goes, except .. and .
  3576. // not (^ or / followed by one or two dots followed by $ or /),
  3577. // followed by anything, any number of times.
  3578. const twoStarDot = '(?:(?!(?:\\/|^)(?:\\.{1,2})($|\\/)).)*?';
  3579. // not a ^ or / followed by a dot,
  3580. // followed by anything, any number of times.
  3581. const twoStarNoDot = '(?:(?!(?:\\/|^)\\.).)*?';
  3582. const filter$1 = (pattern, options = {}) => (p) => minimatch(p, pattern, options);
  3583. minimatch.filter = filter$1;
  3584. const ext = (a, b = {}) => Object.assign({}, a, b);
  3585. const defaults = (def) => {
  3586. if (!def || typeof def !== 'object' || !Object.keys(def).length) {
  3587. return minimatch;
  3588. }
  3589. const orig = minimatch;
  3590. const m = (p, pattern, options = {}) => orig(p, pattern, ext(def, options));
  3591. return Object.assign(m, {
  3592. Minimatch: class Minimatch extends orig.Minimatch {
  3593. constructor(pattern, options = {}) {
  3594. super(pattern, ext(def, options));
  3595. }
  3596. static defaults(options) {
  3597. return orig.defaults(ext(def, options)).Minimatch;
  3598. }
  3599. },
  3600. AST: class AST extends orig.AST {
  3601. /* c8 ignore start */
  3602. constructor(type, parent, options = {}) {
  3603. super(type, parent, ext(def, options));
  3604. }
  3605. /* c8 ignore stop */
  3606. static fromGlob(pattern, options = {}) {
  3607. return orig.AST.fromGlob(pattern, ext(def, options));
  3608. }
  3609. },
  3610. unescape: (s, options = {}) => orig.unescape(s, ext(def, options)),
  3611. escape: (s, options = {}) => orig.escape(s, ext(def, options)),
  3612. filter: (pattern, options = {}) => orig.filter(pattern, ext(def, options)),
  3613. defaults: (options) => orig.defaults(ext(def, options)),
  3614. makeRe: (pattern, options = {}) => orig.makeRe(pattern, ext(def, options)),
  3615. braceExpand: (pattern, options = {}) => orig.braceExpand(pattern, ext(def, options)),
  3616. match: (list, pattern, options = {}) => orig.match(list, pattern, ext(def, options)),
  3617. sep: orig.sep,
  3618. GLOBSTAR: GLOBSTAR$2,
  3619. });
  3620. };
  3621. minimatch.defaults = defaults;
  3622. // Brace expansion:
  3623. // a{b,c}d -> abd acd
  3624. // a{b,}c -> abc ac
  3625. // a{0..3}d -> a0d a1d a2d a3d
  3626. // a{b,c{d,e}f}g -> abg acdfg acefg
  3627. // a{b,c}d{e,f}g -> abdeg acdeg abdeg abdfg
  3628. //
  3629. // Invalid sets are not expanded.
  3630. // a{2..}b -> a{2..}b
  3631. // a{b}c -> a{b}c
  3632. const braceExpand = (pattern, options = {}) => {
  3633. assertValidPattern(pattern);
  3634. // Thanks to Yeting Li <https://github.com/yetingli> for
  3635. // improving this regexp to avoid a ReDOS vulnerability.
  3636. if (options.nobrace || !/\{(?:(?!\{).)*\}/.test(pattern)) {
  3637. // shortcut. no need to expand.
  3638. return [pattern];
  3639. }
  3640. return expand$4(pattern);
  3641. };
  3642. minimatch.braceExpand = braceExpand;
  3643. // parse a component of the expanded set.
  3644. // At this point, no pattern may contain "/" in it
  3645. // so we're going to return a 2d array, where each entry is the full
  3646. // pattern, split on '/', and then turned into a regular expression.
  3647. // A regexp is made at the end which joins each array with an
  3648. // escaped /, and another full one which joins each regexp with |.
  3649. //
  3650. // Following the lead of Bash 4.1, note that "**" only has special meaning
  3651. // when it is the *only* thing in a path portion. Otherwise, any series
  3652. // of * is equivalent to a single *. Globstar behavior is enabled by
  3653. // default, and can be disabled by setting options.noglobstar.
  3654. const makeRe$1 = (pattern, options = {}) => new Minimatch(pattern, options).makeRe();
  3655. minimatch.makeRe = makeRe$1;
  3656. const match = (list, pattern, options = {}) => {
  3657. const mm = new Minimatch(pattern, options);
  3658. list = list.filter(f => mm.match(f));
  3659. if (mm.options.nonull && !list.length) {
  3660. list.push(pattern);
  3661. }
  3662. return list;
  3663. };
  3664. minimatch.match = match;
  3665. // replace stuff like \* with *
  3666. const globMagic = /[?*]|[+@!]\(.*?\)|\[|\]/;
  3667. const regExpEscape = (s) => s.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
  3668. class Minimatch {
  3669. options;
  3670. set;
  3671. pattern;
  3672. windowsPathsNoEscape;
  3673. nonegate;
  3674. negate;
  3675. comment;
  3676. empty;
  3677. preserveMultipleSlashes;
  3678. partial;
  3679. globSet;
  3680. globParts;
  3681. nocase;
  3682. isWindows;
  3683. platform;
  3684. windowsNoMagicRoot;
  3685. regexp;
  3686. constructor(pattern, options = {}) {
  3687. assertValidPattern(pattern);
  3688. options = options || {};
  3689. this.options = options;
  3690. this.pattern = pattern;
  3691. this.platform = options.platform || defaultPlatform$2;
  3692. this.isWindows = this.platform === 'win32';
  3693. this.windowsPathsNoEscape =
  3694. !!options.windowsPathsNoEscape || options.allowWindowsEscape === false;
  3695. if (this.windowsPathsNoEscape) {
  3696. this.pattern = this.pattern.replace(/\\/g, '/');
  3697. }
  3698. this.preserveMultipleSlashes = !!options.preserveMultipleSlashes;
  3699. this.regexp = null;
  3700. this.negate = false;
  3701. this.nonegate = !!options.nonegate;
  3702. this.comment = false;
  3703. this.empty = false;
  3704. this.partial = !!options.partial;
  3705. this.nocase = !!this.options.nocase;
  3706. this.windowsNoMagicRoot =
  3707. options.windowsNoMagicRoot !== undefined
  3708. ? options.windowsNoMagicRoot
  3709. : !!(this.isWindows && this.nocase);
  3710. this.globSet = [];
  3711. this.globParts = [];
  3712. this.set = [];
  3713. // make the set of regexps etc.
  3714. this.make();
  3715. }
  3716. hasMagic() {
  3717. if (this.options.magicalBraces && this.set.length > 1) {
  3718. return true;
  3719. }
  3720. for (const pattern of this.set) {
  3721. for (const part of pattern) {
  3722. if (typeof part !== 'string')
  3723. return true;
  3724. }
  3725. }
  3726. return false;
  3727. }
  3728. debug(..._) { }
  3729. make() {
  3730. const pattern = this.pattern;
  3731. const options = this.options;
  3732. // empty patterns and comments match nothing.
  3733. if (!options.nocomment && pattern.charAt(0) === '#') {
  3734. this.comment = true;
  3735. return;
  3736. }
  3737. if (!pattern) {
  3738. this.empty = true;
  3739. return;
  3740. }
  3741. // step 1: figure out negation, etc.
  3742. this.parseNegate();
  3743. // step 2: expand braces
  3744. this.globSet = [...new Set(this.braceExpand())];
  3745. if (options.debug) {
  3746. this.debug = (...args) => console.error(...args);
  3747. }
  3748. this.debug(this.pattern, this.globSet);
  3749. // step 3: now we have a set, so turn each one into a series of
  3750. // path-portion matching patterns.
  3751. // These will be regexps, except in the case of "**", which is
  3752. // set to the GLOBSTAR object for globstar behavior,
  3753. // and will not contain any / characters
  3754. //
  3755. // First, we preprocess to make the glob pattern sets a bit simpler
  3756. // and deduped. There are some perf-killing patterns that can cause
  3757. // problems with a glob walk, but we can simplify them down a bit.
  3758. const rawGlobParts = this.globSet.map(s => this.slashSplit(s));
  3759. this.globParts = this.preprocess(rawGlobParts);
  3760. this.debug(this.pattern, this.globParts);
  3761. // glob --> regexps
  3762. let set = this.globParts.map((s, _, __) => {
  3763. if (this.isWindows && this.windowsNoMagicRoot) {
  3764. // check if it's a drive or unc path.
  3765. const isUNC = s[0] === '' &&
  3766. s[1] === '' &&
  3767. (s[2] === '?' || !globMagic.test(s[2])) &&
  3768. !globMagic.test(s[3]);
  3769. const isDrive = /^[a-z]:/i.test(s[0]);
  3770. if (isUNC) {
  3771. return [...s.slice(0, 4), ...s.slice(4).map(ss => this.parse(ss))];
  3772. }
  3773. else if (isDrive) {
  3774. return [s[0], ...s.slice(1).map(ss => this.parse(ss))];
  3775. }
  3776. }
  3777. return s.map(ss => this.parse(ss));
  3778. });
  3779. this.debug(this.pattern, set);
  3780. // filter out everything that didn't compile properly.
  3781. this.set = set.filter(s => s.indexOf(false) === -1);
  3782. // do not treat the ? in UNC paths as magic
  3783. if (this.isWindows) {
  3784. for (let i = 0; i < this.set.length; i++) {
  3785. const p = this.set[i];
  3786. if (p[0] === '' &&
  3787. p[1] === '' &&
  3788. this.globParts[i][2] === '?' &&
  3789. typeof p[3] === 'string' &&
  3790. /^[a-z]:$/i.test(p[3])) {
  3791. p[2] = '?';
  3792. }
  3793. }
  3794. }
  3795. this.debug(this.pattern, this.set);
  3796. }
  3797. // various transforms to equivalent pattern sets that are
  3798. // faster to process in a filesystem walk. The goal is to
  3799. // eliminate what we can, and push all ** patterns as far
  3800. // to the right as possible, even if it increases the number
  3801. // of patterns that we have to process.
  3802. preprocess(globParts) {
  3803. // if we're not in globstar mode, then turn all ** into *
  3804. if (this.options.noglobstar) {
  3805. for (let i = 0; i < globParts.length; i++) {
  3806. for (let j = 0; j < globParts[i].length; j++) {
  3807. if (globParts[i][j] === '**') {
  3808. globParts[i][j] = '*';
  3809. }
  3810. }
  3811. }
  3812. }
  3813. const { optimizationLevel = 1 } = this.options;
  3814. if (optimizationLevel >= 2) {
  3815. // aggressive optimization for the purpose of fs walking
  3816. globParts = this.firstPhasePreProcess(globParts);
  3817. globParts = this.secondPhasePreProcess(globParts);
  3818. }
  3819. else if (optimizationLevel >= 1) {
  3820. // just basic optimizations to remove some .. parts
  3821. globParts = this.levelOneOptimize(globParts);
  3822. }
  3823. else {
  3824. // just collapse multiple ** portions into one
  3825. globParts = this.adjascentGlobstarOptimize(globParts);
  3826. }
  3827. return globParts;
  3828. }
  3829. // just get rid of adjascent ** portions
  3830. adjascentGlobstarOptimize(globParts) {
  3831. return globParts.map(parts => {
  3832. let gs = -1;
  3833. while (-1 !== (gs = parts.indexOf('**', gs + 1))) {
  3834. let i = gs;
  3835. while (parts[i + 1] === '**') {
  3836. i++;
  3837. }
  3838. if (i !== gs) {
  3839. parts.splice(gs, i - gs);
  3840. }
  3841. }
  3842. return parts;
  3843. });
  3844. }
  3845. // get rid of adjascent ** and resolve .. portions
  3846. levelOneOptimize(globParts) {
  3847. return globParts.map(parts => {
  3848. parts = parts.reduce((set, part) => {
  3849. const prev = set[set.length - 1];
  3850. if (part === '**' && prev === '**') {
  3851. return set;
  3852. }
  3853. if (part === '..') {
  3854. if (prev && prev !== '..' && prev !== '.' && prev !== '**') {
  3855. set.pop();
  3856. return set;
  3857. }
  3858. }
  3859. set.push(part);
  3860. return set;
  3861. }, []);
  3862. return parts.length === 0 ? [''] : parts;
  3863. });
  3864. }
  3865. levelTwoFileOptimize(parts) {
  3866. if (!Array.isArray(parts)) {
  3867. parts = this.slashSplit(parts);
  3868. }
  3869. let didSomething = false;
  3870. do {
  3871. didSomething = false;
  3872. // <pre>/<e>/<rest> -> <pre>/<rest>
  3873. if (!this.preserveMultipleSlashes) {
  3874. for (let i = 1; i < parts.length - 1; i++) {
  3875. const p = parts[i];
  3876. // don't squeeze out UNC patterns
  3877. if (i === 1 && p === '' && parts[0] === '')
  3878. continue;
  3879. if (p === '.' || p === '') {
  3880. didSomething = true;
  3881. parts.splice(i, 1);
  3882. i--;
  3883. }
  3884. }
  3885. if (parts[0] === '.' &&
  3886. parts.length === 2 &&
  3887. (parts[1] === '.' || parts[1] === '')) {
  3888. didSomething = true;
  3889. parts.pop();
  3890. }
  3891. }
  3892. // <pre>/<p>/../<rest> -> <pre>/<rest>
  3893. let dd = 0;
  3894. while (-1 !== (dd = parts.indexOf('..', dd + 1))) {
  3895. const p = parts[dd - 1];
  3896. if (p && p !== '.' && p !== '..' && p !== '**') {
  3897. didSomething = true;
  3898. parts.splice(dd - 1, 2);
  3899. dd -= 2;
  3900. }
  3901. }
  3902. } while (didSomething);
  3903. return parts.length === 0 ? [''] : parts;
  3904. }
  3905. // First phase: single-pattern processing
  3906. // <pre> is 1 or more portions
  3907. // <rest> is 1 or more portions
  3908. // <p> is any portion other than ., .., '', or **
  3909. // <e> is . or ''
  3910. //
  3911. // **/.. is *brutal* for filesystem walking performance, because
  3912. // it effectively resets the recursive walk each time it occurs,
  3913. // and ** cannot be reduced out by a .. pattern part like a regexp
  3914. // or most strings (other than .., ., and '') can be.
  3915. //
  3916. // <pre>/**/../<p>/<p>/<rest> -> {<pre>/../<p>/<p>/<rest>,<pre>/**/<p>/<p>/<rest>}
  3917. // <pre>/<e>/<rest> -> <pre>/<rest>
  3918. // <pre>/<p>/../<rest> -> <pre>/<rest>
  3919. // **/**/<rest> -> **/<rest>
  3920. //
  3921. // **/*/<rest> -> */**/<rest> <== not valid because ** doesn't follow
  3922. // this WOULD be allowed if ** did follow symlinks, or * didn't
  3923. firstPhasePreProcess(globParts) {
  3924. let didSomething = false;
  3925. do {
  3926. didSomething = false;
  3927. // <pre>/**/../<p>/<p>/<rest> -> {<pre>/../<p>/<p>/<rest>,<pre>/**/<p>/<p>/<rest>}
  3928. for (let parts of globParts) {
  3929. let gs = -1;
  3930. while (-1 !== (gs = parts.indexOf('**', gs + 1))) {
  3931. let gss = gs;
  3932. while (parts[gss + 1] === '**') {
  3933. // <pre>/**/**/<rest> -> <pre>/**/<rest>
  3934. gss++;
  3935. }
  3936. // eg, if gs is 2 and gss is 4, that means we have 3 **
  3937. // parts, and can remove 2 of them.
  3938. if (gss > gs) {
  3939. parts.splice(gs + 1, gss - gs);
  3940. }
  3941. let next = parts[gs + 1];
  3942. const p = parts[gs + 2];
  3943. const p2 = parts[gs + 3];
  3944. if (next !== '..')
  3945. continue;
  3946. if (!p ||
  3947. p === '.' ||
  3948. p === '..' ||
  3949. !p2 ||
  3950. p2 === '.' ||
  3951. p2 === '..') {
  3952. continue;
  3953. }
  3954. didSomething = true;
  3955. // edit parts in place, and push the new one
  3956. parts.splice(gs, 1);
  3957. const other = parts.slice(0);
  3958. other[gs] = '**';
  3959. globParts.push(other);
  3960. gs--;
  3961. }
  3962. // <pre>/<e>/<rest> -> <pre>/<rest>
  3963. if (!this.preserveMultipleSlashes) {
  3964. for (let i = 1; i < parts.length - 1; i++) {
  3965. const p = parts[i];
  3966. // don't squeeze out UNC patterns
  3967. if (i === 1 && p === '' && parts[0] === '')
  3968. continue;
  3969. if (p === '.' || p === '') {
  3970. didSomething = true;
  3971. parts.splice(i, 1);
  3972. i--;
  3973. }
  3974. }
  3975. if (parts[0] === '.' &&
  3976. parts.length === 2 &&
  3977. (parts[1] === '.' || parts[1] === '')) {
  3978. didSomething = true;
  3979. parts.pop();
  3980. }
  3981. }
  3982. // <pre>/<p>/../<rest> -> <pre>/<rest>
  3983. let dd = 0;
  3984. while (-1 !== (dd = parts.indexOf('..', dd + 1))) {
  3985. const p = parts[dd - 1];
  3986. if (p && p !== '.' && p !== '..' && p !== '**') {
  3987. didSomething = true;
  3988. const needDot = dd === 1 && parts[dd + 1] === '**';
  3989. const splin = needDot ? ['.'] : [];
  3990. parts.splice(dd - 1, 2, ...splin);
  3991. if (parts.length === 0)
  3992. parts.push('');
  3993. dd -= 2;
  3994. }
  3995. }
  3996. }
  3997. } while (didSomething);
  3998. return globParts;
  3999. }
  4000. // second phase: multi-pattern dedupes
  4001. // {<pre>/*/<rest>,<pre>/<p>/<rest>} -> <pre>/*/<rest>
  4002. // {<pre>/<rest>,<pre>/<rest>} -> <pre>/<rest>
  4003. // {<pre>/**/<rest>,<pre>/<rest>} -> <pre>/**/<rest>
  4004. //
  4005. // {<pre>/**/<rest>,<pre>/**/<p>/<rest>} -> <pre>/**/<rest>
  4006. // ^-- not valid because ** doens't follow symlinks
  4007. secondPhasePreProcess(globParts) {
  4008. for (let i = 0; i < globParts.length - 1; i++) {
  4009. for (let j = i + 1; j < globParts.length; j++) {
  4010. const matched = this.partsMatch(globParts[i], globParts[j], !this.preserveMultipleSlashes);
  4011. if (matched) {
  4012. globParts[i] = [];
  4013. globParts[j] = matched;
  4014. break;
  4015. }
  4016. }
  4017. }
  4018. return globParts.filter(gs => gs.length);
  4019. }
  4020. partsMatch(a, b, emptyGSMatch = false) {
  4021. let ai = 0;
  4022. let bi = 0;
  4023. let result = [];
  4024. let which = '';
  4025. while (ai < a.length && bi < b.length) {
  4026. if (a[ai] === b[bi]) {
  4027. result.push(which === 'b' ? b[bi] : a[ai]);
  4028. ai++;
  4029. bi++;
  4030. }
  4031. else if (emptyGSMatch && a[ai] === '**' && b[bi] === a[ai + 1]) {
  4032. result.push(a[ai]);
  4033. ai++;
  4034. }
  4035. else if (emptyGSMatch && b[bi] === '**' && a[ai] === b[bi + 1]) {
  4036. result.push(b[bi]);
  4037. bi++;
  4038. }
  4039. else if (a[ai] === '*' &&
  4040. b[bi] &&
  4041. (this.options.dot || !b[bi].startsWith('.')) &&
  4042. b[bi] !== '**') {
  4043. if (which === 'b')
  4044. return false;
  4045. which = 'a';
  4046. result.push(a[ai]);
  4047. ai++;
  4048. bi++;
  4049. }
  4050. else if (b[bi] === '*' &&
  4051. a[ai] &&
  4052. (this.options.dot || !a[ai].startsWith('.')) &&
  4053. a[ai] !== '**') {
  4054. if (which === 'a')
  4055. return false;
  4056. which = 'b';
  4057. result.push(b[bi]);
  4058. ai++;
  4059. bi++;
  4060. }
  4061. else {
  4062. return false;
  4063. }
  4064. }
  4065. // if we fall out of the loop, it means they two are identical
  4066. // as long as their lengths match
  4067. return a.length === b.length && result;
  4068. }
  4069. parseNegate() {
  4070. if (this.nonegate)
  4071. return;
  4072. const pattern = this.pattern;
  4073. let negate = false;
  4074. let negateOffset = 0;
  4075. for (let i = 0; i < pattern.length && pattern.charAt(i) === '!'; i++) {
  4076. negate = !negate;
  4077. negateOffset++;
  4078. }
  4079. if (negateOffset)
  4080. this.pattern = pattern.slice(negateOffset);
  4081. this.negate = negate;
  4082. }
  4083. // set partial to true to test if, for example,
  4084. // "/a/b" matches the start of "/*/b/*/d"
  4085. // Partial means, if you run out of file before you run
  4086. // out of pattern, then that's fine, as long as all
  4087. // the parts match.
  4088. matchOne(file, pattern, partial = false) {
  4089. const options = this.options;
  4090. // UNC paths like //?/X:/... can match X:/... and vice versa
  4091. // Drive letters in absolute drive or unc paths are always compared
  4092. // case-insensitively.
  4093. if (this.isWindows) {
  4094. const fileDrive = typeof file[0] === 'string' && /^[a-z]:$/i.test(file[0]);
  4095. const fileUNC = !fileDrive &&
  4096. file[0] === '' &&
  4097. file[1] === '' &&
  4098. file[2] === '?' &&
  4099. /^[a-z]:$/i.test(file[3]);
  4100. const patternDrive = typeof pattern[0] === 'string' && /^[a-z]:$/i.test(pattern[0]);
  4101. const patternUNC = !patternDrive &&
  4102. pattern[0] === '' &&
  4103. pattern[1] === '' &&
  4104. pattern[2] === '?' &&
  4105. typeof pattern[3] === 'string' &&
  4106. /^[a-z]:$/i.test(pattern[3]);
  4107. const fdi = fileUNC ? 3 : fileDrive ? 0 : undefined;
  4108. const pdi = patternUNC ? 3 : patternDrive ? 0 : undefined;
  4109. if (typeof fdi === 'number' && typeof pdi === 'number') {
  4110. const [fd, pd] = [file[fdi], pattern[pdi]];
  4111. if (fd.toLowerCase() === pd.toLowerCase()) {
  4112. pattern[pdi] = fd;
  4113. if (pdi > fdi) {
  4114. pattern = pattern.slice(pdi);
  4115. }
  4116. else if (fdi > pdi) {
  4117. file = file.slice(fdi);
  4118. }
  4119. }
  4120. }
  4121. }
  4122. // resolve and reduce . and .. portions in the file as well.
  4123. // dont' need to do the second phase, because it's only one string[]
  4124. const { optimizationLevel = 1 } = this.options;
  4125. if (optimizationLevel >= 2) {
  4126. file = this.levelTwoFileOptimize(file);
  4127. }
  4128. this.debug('matchOne', this, { file, pattern });
  4129. this.debug('matchOne', file.length, pattern.length);
  4130. for (var fi = 0, pi = 0, fl = file.length, pl = pattern.length; fi < fl && pi < pl; fi++, pi++) {
  4131. this.debug('matchOne loop');
  4132. var p = pattern[pi];
  4133. var f = file[fi];
  4134. this.debug(pattern, p, f);
  4135. // should be impossible.
  4136. // some invalid regexp stuff in the set.
  4137. /* c8 ignore start */
  4138. if (p === false) {
  4139. return false;
  4140. }
  4141. /* c8 ignore stop */
  4142. if (p === GLOBSTAR$2) {
  4143. this.debug('GLOBSTAR', [pattern, p, f]);
  4144. // "**"
  4145. // a/**/b/**/c would match the following:
  4146. // a/b/x/y/z/c
  4147. // a/x/y/z/b/c
  4148. // a/b/x/b/x/c
  4149. // a/b/c
  4150. // To do this, take the rest of the pattern after
  4151. // the **, and see if it would match the file remainder.
  4152. // If so, return success.
  4153. // If not, the ** "swallows" a segment, and try again.
  4154. // This is recursively awful.
  4155. //
  4156. // a/**/b/**/c matching a/b/x/y/z/c
  4157. // - a matches a
  4158. // - doublestar
  4159. // - matchOne(b/x/y/z/c, b/**/c)
  4160. // - b matches b
  4161. // - doublestar
  4162. // - matchOne(x/y/z/c, c) -> no
  4163. // - matchOne(y/z/c, c) -> no
  4164. // - matchOne(z/c, c) -> no
  4165. // - matchOne(c, c) yes, hit
  4166. var fr = fi;
  4167. var pr = pi + 1;
  4168. if (pr === pl) {
  4169. this.debug('** at the end');
  4170. // a ** at the end will just swallow the rest.
  4171. // We have found a match.
  4172. // however, it will not swallow /.x, unless
  4173. // options.dot is set.
  4174. // . and .. are *never* matched by **, for explosively
  4175. // exponential reasons.
  4176. for (; fi < fl; fi++) {
  4177. if (file[fi] === '.' ||
  4178. file[fi] === '..' ||
  4179. (!options.dot && file[fi].charAt(0) === '.'))
  4180. return false;
  4181. }
  4182. return true;
  4183. }
  4184. // ok, let's see if we can swallow whatever we can.
  4185. while (fr < fl) {
  4186. var swallowee = file[fr];
  4187. this.debug('\nglobstar while', file, fr, pattern, pr, swallowee);
  4188. // XXX remove this slice. Just pass the start index.
  4189. if (this.matchOne(file.slice(fr), pattern.slice(pr), partial)) {
  4190. this.debug('globstar found match!', fr, fl, swallowee);
  4191. // found a match.
  4192. return true;
  4193. }
  4194. else {
  4195. // can't swallow "." or ".." ever.
  4196. // can only swallow ".foo" when explicitly asked.
  4197. if (swallowee === '.' ||
  4198. swallowee === '..' ||
  4199. (!options.dot && swallowee.charAt(0) === '.')) {
  4200. this.debug('dot detected!', file, fr, pattern, pr);
  4201. break;
  4202. }
  4203. // ** swallows a segment, and continue.
  4204. this.debug('globstar swallow a segment, and continue');
  4205. fr++;
  4206. }
  4207. }
  4208. // no match was found.
  4209. // However, in partial mode, we can't say this is necessarily over.
  4210. /* c8 ignore start */
  4211. if (partial) {
  4212. // ran out of file
  4213. this.debug('\n>>> no match, partial?', file, fr, pattern, pr);
  4214. if (fr === fl) {
  4215. return true;
  4216. }
  4217. }
  4218. /* c8 ignore stop */
  4219. return false;
  4220. }
  4221. // something other than **
  4222. // non-magic patterns just have to match exactly
  4223. // patterns with magic have been turned into regexps.
  4224. let hit;
  4225. if (typeof p === 'string') {
  4226. hit = f === p;
  4227. this.debug('string match', p, f, hit);
  4228. }
  4229. else {
  4230. hit = p.test(f);
  4231. this.debug('pattern match', p, f, hit);
  4232. }
  4233. if (!hit)
  4234. return false;
  4235. }
  4236. // Note: ending in / means that we'll get a final ""
  4237. // at the end of the pattern. This can only match a
  4238. // corresponding "" at the end of the file.
  4239. // If the file ends in /, then it can only match a
  4240. // a pattern that ends in /, unless the pattern just
  4241. // doesn't have any more for it. But, a/b/ should *not*
  4242. // match "a/b/*", even though "" matches against the
  4243. // [^/]*? pattern, except in partial mode, where it might
  4244. // simply not be reached yet.
  4245. // However, a/b/ should still satisfy a/*
  4246. // now either we fell off the end of the pattern, or we're done.
  4247. if (fi === fl && pi === pl) {
  4248. // ran out of pattern and filename at the same time.
  4249. // an exact hit!
  4250. return true;
  4251. }
  4252. else if (fi === fl) {
  4253. // ran out of file, but still had pattern left.
  4254. // this is ok if we're doing the match as part of
  4255. // a glob fs traversal.
  4256. return partial;
  4257. }
  4258. else if (pi === pl) {
  4259. // ran out of pattern, still have file left.
  4260. // this is only acceptable if we're on the very last
  4261. // empty segment of a file with a trailing slash.
  4262. // a/* should match a/b/
  4263. return fi === fl - 1 && file[fi] === '';
  4264. /* c8 ignore start */
  4265. }
  4266. else {
  4267. // should be unreachable.
  4268. throw new Error('wtf?');
  4269. }
  4270. /* c8 ignore stop */
  4271. }
  4272. braceExpand() {
  4273. return braceExpand(this.pattern, this.options);
  4274. }
  4275. parse(pattern) {
  4276. assertValidPattern(pattern);
  4277. const options = this.options;
  4278. // shortcuts
  4279. if (pattern === '**')
  4280. return GLOBSTAR$2;
  4281. if (pattern === '')
  4282. return '';
  4283. // far and away, the most common glob pattern parts are
  4284. // *, *.*, and *.<ext> Add a fast check method for those.
  4285. let m;
  4286. let fastTest = null;
  4287. if ((m = pattern.match(starRE))) {
  4288. fastTest = options.dot ? starTestDot : starTest;
  4289. }
  4290. else if ((m = pattern.match(starDotExtRE))) {
  4291. fastTest = (options.nocase
  4292. ? options.dot
  4293. ? starDotExtTestNocaseDot
  4294. : starDotExtTestNocase
  4295. : options.dot
  4296. ? starDotExtTestDot
  4297. : starDotExtTest)(m[1]);
  4298. }
  4299. else if ((m = pattern.match(qmarksRE))) {
  4300. fastTest = (options.nocase
  4301. ? options.dot
  4302. ? qmarksTestNocaseDot
  4303. : qmarksTestNocase
  4304. : options.dot
  4305. ? qmarksTestDot
  4306. : qmarksTest)(m);
  4307. }
  4308. else if ((m = pattern.match(starDotStarRE))) {
  4309. fastTest = options.dot ? starDotStarTestDot : starDotStarTest;
  4310. }
  4311. else if ((m = pattern.match(dotStarRE))) {
  4312. fastTest = dotStarTest;
  4313. }
  4314. const re = AST.fromGlob(pattern, this.options).toMMPattern();
  4315. if (fastTest && typeof re === 'object') {
  4316. // Avoids overriding in frozen environments
  4317. Reflect.defineProperty(re, 'test', { value: fastTest });
  4318. }
  4319. return re;
  4320. }
  4321. makeRe() {
  4322. if (this.regexp || this.regexp === false)
  4323. return this.regexp;
  4324. // at this point, this.set is a 2d array of partial
  4325. // pattern strings, or "**".
  4326. //
  4327. // It's better to use .match(). This function shouldn't
  4328. // be used, really, but it's pretty convenient sometimes,
  4329. // when you just want to work with a regex.
  4330. const set = this.set;
  4331. if (!set.length) {
  4332. this.regexp = false;
  4333. return this.regexp;
  4334. }
  4335. const options = this.options;
  4336. const twoStar = options.noglobstar
  4337. ? star
  4338. : options.dot
  4339. ? twoStarDot
  4340. : twoStarNoDot;
  4341. const flags = new Set(options.nocase ? ['i'] : []);
  4342. // regexpify non-globstar patterns
  4343. // if ** is only item, then we just do one twoStar
  4344. // if ** is first, and there are more, prepend (\/|twoStar\/)? to next
  4345. // if ** is last, append (\/twoStar|) to previous
  4346. // if ** is in the middle, append (\/|\/twoStar\/) to previous
  4347. // then filter out GLOBSTAR symbols
  4348. let re = set
  4349. .map(pattern => {
  4350. const pp = pattern.map(p => {
  4351. if (p instanceof RegExp) {
  4352. for (const f of p.flags.split(''))
  4353. flags.add(f);
  4354. }
  4355. return typeof p === 'string'
  4356. ? regExpEscape(p)
  4357. : p === GLOBSTAR$2
  4358. ? GLOBSTAR$2
  4359. : p._src;
  4360. });
  4361. pp.forEach((p, i) => {
  4362. const next = pp[i + 1];
  4363. const prev = pp[i - 1];
  4364. if (p !== GLOBSTAR$2 || prev === GLOBSTAR$2) {
  4365. return;
  4366. }
  4367. if (prev === undefined) {
  4368. if (next !== undefined && next !== GLOBSTAR$2) {
  4369. pp[i + 1] = '(?:\\/|' + twoStar + '\\/)?' + next;
  4370. }
  4371. else {
  4372. pp[i] = twoStar;
  4373. }
  4374. }
  4375. else if (next === undefined) {
  4376. pp[i - 1] = prev + '(?:\\/|' + twoStar + ')?';
  4377. }
  4378. else if (next !== GLOBSTAR$2) {
  4379. pp[i - 1] = prev + '(?:\\/|\\/' + twoStar + '\\/)' + next;
  4380. pp[i + 1] = GLOBSTAR$2;
  4381. }
  4382. });
  4383. return pp.filter(p => p !== GLOBSTAR$2).join('/');
  4384. })
  4385. .join('|');
  4386. // need to wrap in parens if we had more than one thing with |,
  4387. // otherwise only the first will be anchored to ^ and the last to $
  4388. const [open, close] = set.length > 1 ? ['(?:', ')'] : ['', ''];
  4389. // must match entire pattern
  4390. // ending in a * or ** will make it less strict.
  4391. re = '^' + open + re + close + '$';
  4392. // can match anything, as long as it's not this.
  4393. if (this.negate)
  4394. re = '^(?!' + re + ').+$';
  4395. try {
  4396. this.regexp = new RegExp(re, [...flags].join(''));
  4397. /* c8 ignore start */
  4398. }
  4399. catch (ex) {
  4400. // should be impossible
  4401. this.regexp = false;
  4402. }
  4403. /* c8 ignore stop */
  4404. return this.regexp;
  4405. }
  4406. slashSplit(p) {
  4407. // if p starts with // on windows, we preserve that
  4408. // so that UNC paths aren't broken. Otherwise, any number of
  4409. // / characters are coalesced into one, unless
  4410. // preserveMultipleSlashes is set to true.
  4411. if (this.preserveMultipleSlashes) {
  4412. return p.split('/');
  4413. }
  4414. else if (this.isWindows && /^\/\/[^\/]+/.test(p)) {
  4415. // add an extra '' for the one we lose
  4416. return ['', ...p.split(/\/+/)];
  4417. }
  4418. else {
  4419. return p.split(/\/+/);
  4420. }
  4421. }
  4422. match(f, partial = this.partial) {
  4423. this.debug('match', f, this.pattern);
  4424. // short-circuit in the case of busted things.
  4425. // comments, etc.
  4426. if (this.comment) {
  4427. return false;
  4428. }
  4429. if (this.empty) {
  4430. return f === '';
  4431. }
  4432. if (f === '/' && partial) {
  4433. return true;
  4434. }
  4435. const options = this.options;
  4436. // windows: need to use /, not \
  4437. if (this.isWindows) {
  4438. f = f.split('\\').join('/');
  4439. }
  4440. // treat the test path as a set of pathparts.
  4441. const ff = this.slashSplit(f);
  4442. this.debug(this.pattern, 'split', ff);
  4443. // just ONE of the pattern sets in this.set needs to match
  4444. // in order for it to be valid. If negating, then just one
  4445. // match means that we have failed.
  4446. // Either way, return on the first hit.
  4447. const set = this.set;
  4448. this.debug(this.pattern, 'set', set);
  4449. // Find the basename of the path by looking for the last non-empty segment
  4450. let filename = ff[ff.length - 1];
  4451. if (!filename) {
  4452. for (let i = ff.length - 2; !filename && i >= 0; i--) {
  4453. filename = ff[i];
  4454. }
  4455. }
  4456. for (let i = 0; i < set.length; i++) {
  4457. const pattern = set[i];
  4458. let file = ff;
  4459. if (options.matchBase && pattern.length === 1) {
  4460. file = [filename];
  4461. }
  4462. const hit = this.matchOne(file, pattern, partial);
  4463. if (hit) {
  4464. if (options.flipNegate) {
  4465. return true;
  4466. }
  4467. return !this.negate;
  4468. }
  4469. }
  4470. // didn't get any hits. this is success if it's a negative
  4471. // pattern, failure otherwise.
  4472. if (options.flipNegate) {
  4473. return false;
  4474. }
  4475. return this.negate;
  4476. }
  4477. static defaults(def) {
  4478. return minimatch.defaults(def).Minimatch;
  4479. }
  4480. }
  4481. /* c8 ignore stop */
  4482. minimatch.AST = AST;
  4483. minimatch.Minimatch = Minimatch;
  4484. minimatch.escape = escape$2;
  4485. minimatch.unescape = unescape$1;
  4486. /**
  4487. * @module LRUCache
  4488. */
  4489. const perf = typeof performance === 'object' &&
  4490. performance &&
  4491. typeof performance.now === 'function'
  4492. ? performance
  4493. : Date;
  4494. const warned$1 = new Set();
  4495. /* c8 ignore start */
  4496. const PROCESS = (typeof process === 'object' && !!process ? process : {});
  4497. /* c8 ignore start */
  4498. const emitWarning = (msg, type, code, fn) => {
  4499. typeof PROCESS.emitWarning === 'function'
  4500. ? PROCESS.emitWarning(msg, type, code, fn)
  4501. : console.error(`[${code}] ${type}: ${msg}`);
  4502. };
  4503. let AC = globalThis.AbortController;
  4504. let AS = globalThis.AbortSignal;
  4505. /* c8 ignore start */
  4506. if (typeof AC === 'undefined') {
  4507. //@ts-ignore
  4508. AS = class AbortSignal {
  4509. onabort;
  4510. _onabort = [];
  4511. reason;
  4512. aborted = false;
  4513. addEventListener(_, fn) {
  4514. this._onabort.push(fn);
  4515. }
  4516. };
  4517. //@ts-ignore
  4518. AC = class AbortController {
  4519. constructor() {
  4520. warnACPolyfill();
  4521. }
  4522. signal = new AS();
  4523. abort(reason) {
  4524. if (this.signal.aborted)
  4525. return;
  4526. //@ts-ignore
  4527. this.signal.reason = reason;
  4528. //@ts-ignore
  4529. this.signal.aborted = true;
  4530. //@ts-ignore
  4531. for (const fn of this.signal._onabort) {
  4532. fn(reason);
  4533. }
  4534. this.signal.onabort?.(reason);
  4535. }
  4536. };
  4537. let printACPolyfillWarning = PROCESS.env?.LRU_CACHE_IGNORE_AC_WARNING !== '1';
  4538. const warnACPolyfill = () => {
  4539. if (!printACPolyfillWarning)
  4540. return;
  4541. printACPolyfillWarning = false;
  4542. emitWarning('AbortController is not defined. If using lru-cache in ' +
  4543. 'node 14, load an AbortController polyfill from the ' +
  4544. '`node-abort-controller` package. A minimal polyfill is ' +
  4545. 'provided for use by LRUCache.fetch(), but it should not be ' +
  4546. 'relied upon in other contexts (eg, passing it to other APIs that ' +
  4547. 'use AbortController/AbortSignal might have undesirable effects). ' +
  4548. 'You may disable this with LRU_CACHE_IGNORE_AC_WARNING=1 in the env.', 'NO_ABORT_CONTROLLER', 'ENOTSUP', warnACPolyfill);
  4549. };
  4550. }
  4551. /* c8 ignore stop */
  4552. const shouldWarn = (code) => !warned$1.has(code);
  4553. const isPosInt = (n) => n && n === Math.floor(n) && n > 0 && isFinite(n);
  4554. /* c8 ignore start */
  4555. // This is a little bit ridiculous, tbh.
  4556. // The maximum array length is 2^32-1 or thereabouts on most JS impls.
  4557. // And well before that point, you're caching the entire world, I mean,
  4558. // that's ~32GB of just integers for the next/prev links, plus whatever
  4559. // else to hold that many keys and values. Just filling the memory with
  4560. // zeroes at init time is brutal when you get that big.
  4561. // But why not be complete?
  4562. // Maybe in the future, these limits will have expanded.
  4563. const getUintArray = (max) => !isPosInt(max)
  4564. ? null
  4565. : max <= Math.pow(2, 8)
  4566. ? Uint8Array
  4567. : max <= Math.pow(2, 16)
  4568. ? Uint16Array
  4569. : max <= Math.pow(2, 32)
  4570. ? Uint32Array
  4571. : max <= Number.MAX_SAFE_INTEGER
  4572. ? ZeroArray
  4573. : null;
  4574. /* c8 ignore stop */
  4575. class ZeroArray extends Array {
  4576. constructor(size) {
  4577. super(size);
  4578. this.fill(0);
  4579. }
  4580. }
  4581. class Stack {
  4582. heap;
  4583. length;
  4584. // private constructor
  4585. static #constructing = false;
  4586. static create(max) {
  4587. const HeapCls = getUintArray(max);
  4588. if (!HeapCls)
  4589. return [];
  4590. Stack.#constructing = true;
  4591. const s = new Stack(max, HeapCls);
  4592. Stack.#constructing = false;
  4593. return s;
  4594. }
  4595. constructor(max, HeapCls) {
  4596. /* c8 ignore start */
  4597. if (!Stack.#constructing) {
  4598. throw new TypeError('instantiate Stack using Stack.create(n)');
  4599. }
  4600. /* c8 ignore stop */
  4601. this.heap = new HeapCls(max);
  4602. this.length = 0;
  4603. }
  4604. push(n) {
  4605. this.heap[this.length++] = n;
  4606. }
  4607. pop() {
  4608. return this.heap[--this.length];
  4609. }
  4610. }
  4611. /**
  4612. * Default export, the thing you're using this module to get.
  4613. *
  4614. * The `K` and `V` types define the key and value types, respectively. The
  4615. * optional `FC` type defines the type of the `context` object passed to
  4616. * `cache.fetch()` and `cache.memo()`.
  4617. *
  4618. * Keys and values **must not** be `null` or `undefined`.
  4619. *
  4620. * All properties from the options object (with the exception of `max`,
  4621. * `maxSize`, `fetchMethod`, `memoMethod`, `dispose` and `disposeAfter`) are
  4622. * added as normal public members. (The listed options are read-only getters.)
  4623. *
  4624. * Changing any of these will alter the defaults for subsequent method calls.
  4625. */
  4626. class LRUCache {
  4627. // options that cannot be changed without disaster
  4628. #max;
  4629. #maxSize;
  4630. #dispose;
  4631. #disposeAfter;
  4632. #fetchMethod;
  4633. #memoMethod;
  4634. /**
  4635. * {@link LRUCache.OptionsBase.ttl}
  4636. */
  4637. ttl;
  4638. /**
  4639. * {@link LRUCache.OptionsBase.ttlResolution}
  4640. */
  4641. ttlResolution;
  4642. /**
  4643. * {@link LRUCache.OptionsBase.ttlAutopurge}
  4644. */
  4645. ttlAutopurge;
  4646. /**
  4647. * {@link LRUCache.OptionsBase.updateAgeOnGet}
  4648. */
  4649. updateAgeOnGet;
  4650. /**
  4651. * {@link LRUCache.OptionsBase.updateAgeOnHas}
  4652. */
  4653. updateAgeOnHas;
  4654. /**
  4655. * {@link LRUCache.OptionsBase.allowStale}
  4656. */
  4657. allowStale;
  4658. /**
  4659. * {@link LRUCache.OptionsBase.noDisposeOnSet}
  4660. */
  4661. noDisposeOnSet;
  4662. /**
  4663. * {@link LRUCache.OptionsBase.noUpdateTTL}
  4664. */
  4665. noUpdateTTL;
  4666. /**
  4667. * {@link LRUCache.OptionsBase.maxEntrySize}
  4668. */
  4669. maxEntrySize;
  4670. /**
  4671. * {@link LRUCache.OptionsBase.sizeCalculation}
  4672. */
  4673. sizeCalculation;
  4674. /**
  4675. * {@link LRUCache.OptionsBase.noDeleteOnFetchRejection}
  4676. */
  4677. noDeleteOnFetchRejection;
  4678. /**
  4679. * {@link LRUCache.OptionsBase.noDeleteOnStaleGet}
  4680. */
  4681. noDeleteOnStaleGet;
  4682. /**
  4683. * {@link LRUCache.OptionsBase.allowStaleOnFetchAbort}
  4684. */
  4685. allowStaleOnFetchAbort;
  4686. /**
  4687. * {@link LRUCache.OptionsBase.allowStaleOnFetchRejection}
  4688. */
  4689. allowStaleOnFetchRejection;
  4690. /**
  4691. * {@link LRUCache.OptionsBase.ignoreFetchAbort}
  4692. */
  4693. ignoreFetchAbort;
  4694. // computed properties
  4695. #size;
  4696. #calculatedSize;
  4697. #keyMap;
  4698. #keyList;
  4699. #valList;
  4700. #next;
  4701. #prev;
  4702. #head;
  4703. #tail;
  4704. #free;
  4705. #disposed;
  4706. #sizes;
  4707. #starts;
  4708. #ttls;
  4709. #hasDispose;
  4710. #hasFetchMethod;
  4711. #hasDisposeAfter;
  4712. /**
  4713. * Do not call this method unless you need to inspect the
  4714. * inner workings of the cache. If anything returned by this
  4715. * object is modified in any way, strange breakage may occur.
  4716. *
  4717. * These fields are private for a reason!
  4718. *
  4719. * @internal
  4720. */
  4721. static unsafeExposeInternals(c) {
  4722. return {
  4723. // properties
  4724. starts: c.#starts,
  4725. ttls: c.#ttls,
  4726. sizes: c.#sizes,
  4727. keyMap: c.#keyMap,
  4728. keyList: c.#keyList,
  4729. valList: c.#valList,
  4730. next: c.#next,
  4731. prev: c.#prev,
  4732. get head() {
  4733. return c.#head;
  4734. },
  4735. get tail() {
  4736. return c.#tail;
  4737. },
  4738. free: c.#free,
  4739. // methods
  4740. isBackgroundFetch: (p) => c.#isBackgroundFetch(p),
  4741. backgroundFetch: (k, index, options, context) => c.#backgroundFetch(k, index, options, context),
  4742. moveToTail: (index) => c.#moveToTail(index),
  4743. indexes: (options) => c.#indexes(options),
  4744. rindexes: (options) => c.#rindexes(options),
  4745. isStale: (index) => c.#isStale(index),
  4746. };
  4747. }
  4748. // Protected read-only members
  4749. /**
  4750. * {@link LRUCache.OptionsBase.max} (read-only)
  4751. */
  4752. get max() {
  4753. return this.#max;
  4754. }
  4755. /**
  4756. * {@link LRUCache.OptionsBase.maxSize} (read-only)
  4757. */
  4758. get maxSize() {
  4759. return this.#maxSize;
  4760. }
  4761. /**
  4762. * The total computed size of items in the cache (read-only)
  4763. */
  4764. get calculatedSize() {
  4765. return this.#calculatedSize;
  4766. }
  4767. /**
  4768. * The number of items stored in the cache (read-only)
  4769. */
  4770. get size() {
  4771. return this.#size;
  4772. }
  4773. /**
  4774. * {@link LRUCache.OptionsBase.fetchMethod} (read-only)
  4775. */
  4776. get fetchMethod() {
  4777. return this.#fetchMethod;
  4778. }
  4779. get memoMethod() {
  4780. return this.#memoMethod;
  4781. }
  4782. /**
  4783. * {@link LRUCache.OptionsBase.dispose} (read-only)
  4784. */
  4785. get dispose() {
  4786. return this.#dispose;
  4787. }
  4788. /**
  4789. * {@link LRUCache.OptionsBase.disposeAfter} (read-only)
  4790. */
  4791. get disposeAfter() {
  4792. return this.#disposeAfter;
  4793. }
  4794. constructor(options) {
  4795. 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;
  4796. if (max !== 0 && !isPosInt(max)) {
  4797. throw new TypeError('max option must be a nonnegative integer');
  4798. }
  4799. const UintArray = max ? getUintArray(max) : Array;
  4800. if (!UintArray) {
  4801. throw new Error('invalid max value: ' + max);
  4802. }
  4803. this.#max = max;
  4804. this.#maxSize = maxSize;
  4805. this.maxEntrySize = maxEntrySize || this.#maxSize;
  4806. this.sizeCalculation = sizeCalculation;
  4807. if (this.sizeCalculation) {
  4808. if (!this.#maxSize && !this.maxEntrySize) {
  4809. throw new TypeError('cannot set sizeCalculation without setting maxSize or maxEntrySize');
  4810. }
  4811. if (typeof this.sizeCalculation !== 'function') {
  4812. throw new TypeError('sizeCalculation set to non-function');
  4813. }
  4814. }
  4815. if (memoMethod !== undefined &&
  4816. typeof memoMethod !== 'function') {
  4817. throw new TypeError('memoMethod must be a function if defined');
  4818. }
  4819. this.#memoMethod = memoMethod;
  4820. if (fetchMethod !== undefined &&
  4821. typeof fetchMethod !== 'function') {
  4822. throw new TypeError('fetchMethod must be a function if specified');
  4823. }
  4824. this.#fetchMethod = fetchMethod;
  4825. this.#hasFetchMethod = !!fetchMethod;
  4826. this.#keyMap = new Map();
  4827. this.#keyList = new Array(max).fill(undefined);
  4828. this.#valList = new Array(max).fill(undefined);
  4829. this.#next = new UintArray(max);
  4830. this.#prev = new UintArray(max);
  4831. this.#head = 0;
  4832. this.#tail = 0;
  4833. this.#free = Stack.create(max);
  4834. this.#size = 0;
  4835. this.#calculatedSize = 0;
  4836. if (typeof dispose === 'function') {
  4837. this.#dispose = dispose;
  4838. }
  4839. if (typeof disposeAfter === 'function') {
  4840. this.#disposeAfter = disposeAfter;
  4841. this.#disposed = [];
  4842. }
  4843. else {
  4844. this.#disposeAfter = undefined;
  4845. this.#disposed = undefined;
  4846. }
  4847. this.#hasDispose = !!this.#dispose;
  4848. this.#hasDisposeAfter = !!this.#disposeAfter;
  4849. this.noDisposeOnSet = !!noDisposeOnSet;
  4850. this.noUpdateTTL = !!noUpdateTTL;
  4851. this.noDeleteOnFetchRejection = !!noDeleteOnFetchRejection;
  4852. this.allowStaleOnFetchRejection = !!allowStaleOnFetchRejection;
  4853. this.allowStaleOnFetchAbort = !!allowStaleOnFetchAbort;
  4854. this.ignoreFetchAbort = !!ignoreFetchAbort;
  4855. // NB: maxEntrySize is set to maxSize if it's set
  4856. if (this.maxEntrySize !== 0) {
  4857. if (this.#maxSize !== 0) {
  4858. if (!isPosInt(this.#maxSize)) {
  4859. throw new TypeError('maxSize must be a positive integer if specified');
  4860. }
  4861. }
  4862. if (!isPosInt(this.maxEntrySize)) {
  4863. throw new TypeError('maxEntrySize must be a positive integer if specified');
  4864. }
  4865. this.#initializeSizeTracking();
  4866. }
  4867. this.allowStale = !!allowStale;
  4868. this.noDeleteOnStaleGet = !!noDeleteOnStaleGet;
  4869. this.updateAgeOnGet = !!updateAgeOnGet;
  4870. this.updateAgeOnHas = !!updateAgeOnHas;
  4871. this.ttlResolution =
  4872. isPosInt(ttlResolution) || ttlResolution === 0
  4873. ? ttlResolution
  4874. : 1;
  4875. this.ttlAutopurge = !!ttlAutopurge;
  4876. this.ttl = ttl || 0;
  4877. if (this.ttl) {
  4878. if (!isPosInt(this.ttl)) {
  4879. throw new TypeError('ttl must be a positive integer if specified');
  4880. }
  4881. this.#initializeTTLTracking();
  4882. }
  4883. // do not allow completely unbounded caches
  4884. if (this.#max === 0 && this.ttl === 0 && this.#maxSize === 0) {
  4885. throw new TypeError('At least one of max, maxSize, or ttl is required');
  4886. }
  4887. if (!this.ttlAutopurge && !this.#max && !this.#maxSize) {
  4888. const code = 'LRU_CACHE_UNBOUNDED';
  4889. if (shouldWarn(code)) {
  4890. warned$1.add(code);
  4891. const msg = 'TTL caching without ttlAutopurge, max, or maxSize can ' +
  4892. 'result in unbounded memory consumption.';
  4893. emitWarning(msg, 'UnboundedCacheWarning', code, LRUCache);
  4894. }
  4895. }
  4896. }
  4897. /**
  4898. * Return the number of ms left in the item's TTL. If item is not in cache,
  4899. * returns `0`. Returns `Infinity` if item is in cache without a defined TTL.
  4900. */
  4901. getRemainingTTL(key) {
  4902. return this.#keyMap.has(key) ? Infinity : 0;
  4903. }
  4904. #initializeTTLTracking() {
  4905. const ttls = new ZeroArray(this.#max);
  4906. const starts = new ZeroArray(this.#max);
  4907. this.#ttls = ttls;
  4908. this.#starts = starts;
  4909. this.#setItemTTL = (index, ttl, start = perf.now()) => {
  4910. starts[index] = ttl !== 0 ? start : 0;
  4911. ttls[index] = ttl;
  4912. if (ttl !== 0 && this.ttlAutopurge) {
  4913. const t = setTimeout(() => {
  4914. if (this.#isStale(index)) {
  4915. this.#delete(this.#keyList[index], 'expire');
  4916. }
  4917. }, ttl + 1);
  4918. // unref() not supported on all platforms
  4919. /* c8 ignore start */
  4920. if (t.unref) {
  4921. t.unref();
  4922. }
  4923. /* c8 ignore stop */
  4924. }
  4925. };
  4926. this.#updateItemAge = index => {
  4927. starts[index] = ttls[index] !== 0 ? perf.now() : 0;
  4928. };
  4929. this.#statusTTL = (status, index) => {
  4930. if (ttls[index]) {
  4931. const ttl = ttls[index];
  4932. const start = starts[index];
  4933. /* c8 ignore next */
  4934. if (!ttl || !start)
  4935. return;
  4936. status.ttl = ttl;
  4937. status.start = start;
  4938. status.now = cachedNow || getNow();
  4939. const age = status.now - start;
  4940. status.remainingTTL = ttl - age;
  4941. }
  4942. };
  4943. // debounce calls to perf.now() to 1s so we're not hitting
  4944. // that costly call repeatedly.
  4945. let cachedNow = 0;
  4946. const getNow = () => {
  4947. const n = perf.now();
  4948. if (this.ttlResolution > 0) {
  4949. cachedNow = n;
  4950. const t = setTimeout(() => (cachedNow = 0), this.ttlResolution);
  4951. // not available on all platforms
  4952. /* c8 ignore start */
  4953. if (t.unref) {
  4954. t.unref();
  4955. }
  4956. /* c8 ignore stop */
  4957. }
  4958. return n;
  4959. };
  4960. this.getRemainingTTL = key => {
  4961. const index = this.#keyMap.get(key);
  4962. if (index === undefined) {
  4963. return 0;
  4964. }
  4965. const ttl = ttls[index];
  4966. const start = starts[index];
  4967. if (!ttl || !start) {
  4968. return Infinity;
  4969. }
  4970. const age = (cachedNow || getNow()) - start;
  4971. return ttl - age;
  4972. };
  4973. this.#isStale = index => {
  4974. const s = starts[index];
  4975. const t = ttls[index];
  4976. return !!t && !!s && (cachedNow || getNow()) - s > t;
  4977. };
  4978. }
  4979. // conditionally set private methods related to TTL
  4980. #updateItemAge = () => { };
  4981. #statusTTL = () => { };
  4982. #setItemTTL = () => { };
  4983. /* c8 ignore stop */
  4984. #isStale = () => false;
  4985. #initializeSizeTracking() {
  4986. const sizes = new ZeroArray(this.#max);
  4987. this.#calculatedSize = 0;
  4988. this.#sizes = sizes;
  4989. this.#removeItemSize = index => {
  4990. this.#calculatedSize -= sizes[index];
  4991. sizes[index] = 0;
  4992. };
  4993. this.#requireSize = (k, v, size, sizeCalculation) => {
  4994. // provisionally accept background fetches.
  4995. // actual value size will be checked when they return.
  4996. if (this.#isBackgroundFetch(v)) {
  4997. return 0;
  4998. }
  4999. if (!isPosInt(size)) {
  5000. if (sizeCalculation) {
  5001. if (typeof sizeCalculation !== 'function') {
  5002. throw new TypeError('sizeCalculation must be a function');
  5003. }
  5004. size = sizeCalculation(v, k);
  5005. if (!isPosInt(size)) {
  5006. throw new TypeError('sizeCalculation return invalid (expect positive integer)');
  5007. }
  5008. }
  5009. else {
  5010. throw new TypeError('invalid size value (must be positive integer). ' +
  5011. 'When maxSize or maxEntrySize is used, sizeCalculation ' +
  5012. 'or size must be set.');
  5013. }
  5014. }
  5015. return size;
  5016. };
  5017. this.#addItemSize = (index, size, status) => {
  5018. sizes[index] = size;
  5019. if (this.#maxSize) {
  5020. const maxSize = this.#maxSize - sizes[index];
  5021. while (this.#calculatedSize > maxSize) {
  5022. this.#evict(true);
  5023. }
  5024. }
  5025. this.#calculatedSize += sizes[index];
  5026. if (status) {
  5027. status.entrySize = size;
  5028. status.totalCalculatedSize = this.#calculatedSize;
  5029. }
  5030. };
  5031. }
  5032. #removeItemSize = _i => { };
  5033. #addItemSize = (_i, _s, _st) => { };
  5034. #requireSize = (_k, _v, size, sizeCalculation) => {
  5035. if (size || sizeCalculation) {
  5036. throw new TypeError('cannot set size without setting maxSize or maxEntrySize on cache');
  5037. }
  5038. return 0;
  5039. };
  5040. *#indexes({ allowStale = this.allowStale } = {}) {
  5041. if (this.#size) {
  5042. for (let i = this.#tail; true;) {
  5043. if (!this.#isValidIndex(i)) {
  5044. break;
  5045. }
  5046. if (allowStale || !this.#isStale(i)) {
  5047. yield i;
  5048. }
  5049. if (i === this.#head) {
  5050. break;
  5051. }
  5052. else {
  5053. i = this.#prev[i];
  5054. }
  5055. }
  5056. }
  5057. }
  5058. *#rindexes({ allowStale = this.allowStale } = {}) {
  5059. if (this.#size) {
  5060. for (let i = this.#head; true;) {
  5061. if (!this.#isValidIndex(i)) {
  5062. break;
  5063. }
  5064. if (allowStale || !this.#isStale(i)) {
  5065. yield i;
  5066. }
  5067. if (i === this.#tail) {
  5068. break;
  5069. }
  5070. else {
  5071. i = this.#next[i];
  5072. }
  5073. }
  5074. }
  5075. }
  5076. #isValidIndex(index) {
  5077. return (index !== undefined &&
  5078. this.#keyMap.get(this.#keyList[index]) === index);
  5079. }
  5080. /**
  5081. * Return a generator yielding `[key, value]` pairs,
  5082. * in order from most recently used to least recently used.
  5083. */
  5084. *entries() {
  5085. for (const i of this.#indexes()) {
  5086. if (this.#valList[i] !== undefined &&
  5087. this.#keyList[i] !== undefined &&
  5088. !this.#isBackgroundFetch(this.#valList[i])) {
  5089. yield [this.#keyList[i], this.#valList[i]];
  5090. }
  5091. }
  5092. }
  5093. /**
  5094. * Inverse order version of {@link LRUCache.entries}
  5095. *
  5096. * Return a generator yielding `[key, value]` pairs,
  5097. * in order from least recently used to most recently used.
  5098. */
  5099. *rentries() {
  5100. for (const i of this.#rindexes()) {
  5101. if (this.#valList[i] !== undefined &&
  5102. this.#keyList[i] !== undefined &&
  5103. !this.#isBackgroundFetch(this.#valList[i])) {
  5104. yield [this.#keyList[i], this.#valList[i]];
  5105. }
  5106. }
  5107. }
  5108. /**
  5109. * Return a generator yielding the keys in the cache,
  5110. * in order from most recently used to least recently used.
  5111. */
  5112. *keys() {
  5113. for (const i of this.#indexes()) {
  5114. const k = this.#keyList[i];
  5115. if (k !== undefined &&
  5116. !this.#isBackgroundFetch(this.#valList[i])) {
  5117. yield k;
  5118. }
  5119. }
  5120. }
  5121. /**
  5122. * Inverse order version of {@link LRUCache.keys}
  5123. *
  5124. * Return a generator yielding the keys in the cache,
  5125. * in order from least recently used to most recently used.
  5126. */
  5127. *rkeys() {
  5128. for (const i of this.#rindexes()) {
  5129. const k = this.#keyList[i];
  5130. if (k !== undefined &&
  5131. !this.#isBackgroundFetch(this.#valList[i])) {
  5132. yield k;
  5133. }
  5134. }
  5135. }
  5136. /**
  5137. * Return a generator yielding the values in the cache,
  5138. * in order from most recently used to least recently used.
  5139. */
  5140. *values() {
  5141. for (const i of this.#indexes()) {
  5142. const v = this.#valList[i];
  5143. if (v !== undefined &&
  5144. !this.#isBackgroundFetch(this.#valList[i])) {
  5145. yield this.#valList[i];
  5146. }
  5147. }
  5148. }
  5149. /**
  5150. * Inverse order version of {@link LRUCache.values}
  5151. *
  5152. * Return a generator yielding the values in the cache,
  5153. * in order from least recently used to most recently used.
  5154. */
  5155. *rvalues() {
  5156. for (const i of this.#rindexes()) {
  5157. const v = this.#valList[i];
  5158. if (v !== undefined &&
  5159. !this.#isBackgroundFetch(this.#valList[i])) {
  5160. yield this.#valList[i];
  5161. }
  5162. }
  5163. }
  5164. /**
  5165. * Iterating over the cache itself yields the same results as
  5166. * {@link LRUCache.entries}
  5167. */
  5168. [Symbol.iterator]() {
  5169. return this.entries();
  5170. }
  5171. /**
  5172. * A String value that is used in the creation of the default string
  5173. * description of an object. Called by the built-in method
  5174. * `Object.prototype.toString`.
  5175. */
  5176. [Symbol.toStringTag] = 'LRUCache';
  5177. /**
  5178. * Find a value for which the supplied fn method returns a truthy value,
  5179. * similar to `Array.find()`. fn is called as `fn(value, key, cache)`.
  5180. */
  5181. find(fn, getOptions = {}) {
  5182. for (const i of this.#indexes()) {
  5183. const v = this.#valList[i];
  5184. const value = this.#isBackgroundFetch(v)
  5185. ? v.__staleWhileFetching
  5186. : v;
  5187. if (value === undefined)
  5188. continue;
  5189. if (fn(value, this.#keyList[i], this)) {
  5190. return this.get(this.#keyList[i], getOptions);
  5191. }
  5192. }
  5193. }
  5194. /**
  5195. * Call the supplied function on each item in the cache, in order from most
  5196. * recently used to least recently used.
  5197. *
  5198. * `fn` is called as `fn(value, key, cache)`.
  5199. *
  5200. * If `thisp` is provided, function will be called in the `this`-context of
  5201. * the provided object, or the cache if no `thisp` object is provided.
  5202. *
  5203. * Does not update age or recenty of use, or iterate over stale values.
  5204. */
  5205. forEach(fn, thisp = this) {
  5206. for (const i of this.#indexes()) {
  5207. const v = this.#valList[i];
  5208. const value = this.#isBackgroundFetch(v)
  5209. ? v.__staleWhileFetching
  5210. : v;
  5211. if (value === undefined)
  5212. continue;
  5213. fn.call(thisp, value, this.#keyList[i], this);
  5214. }
  5215. }
  5216. /**
  5217. * The same as {@link LRUCache.forEach} but items are iterated over in
  5218. * reverse order. (ie, less recently used items are iterated over first.)
  5219. */
  5220. rforEach(fn, thisp = this) {
  5221. for (const i of this.#rindexes()) {
  5222. const v = this.#valList[i];
  5223. const value = this.#isBackgroundFetch(v)
  5224. ? v.__staleWhileFetching
  5225. : v;
  5226. if (value === undefined)
  5227. continue;
  5228. fn.call(thisp, value, this.#keyList[i], this);
  5229. }
  5230. }
  5231. /**
  5232. * Delete any stale entries. Returns true if anything was removed,
  5233. * false otherwise.
  5234. */
  5235. purgeStale() {
  5236. let deleted = false;
  5237. for (const i of this.#rindexes({ allowStale: true })) {
  5238. if (this.#isStale(i)) {
  5239. this.#delete(this.#keyList[i], 'expire');
  5240. deleted = true;
  5241. }
  5242. }
  5243. return deleted;
  5244. }
  5245. /**
  5246. * Get the extended info about a given entry, to get its value, size, and
  5247. * TTL info simultaneously. Returns `undefined` if the key is not present.
  5248. *
  5249. * Unlike {@link LRUCache#dump}, which is designed to be portable and survive
  5250. * serialization, the `start` value is always the current timestamp, and the
  5251. * `ttl` is a calculated remaining time to live (negative if expired).
  5252. *
  5253. * Always returns stale values, if their info is found in the cache, so be
  5254. * sure to check for expirations (ie, a negative {@link LRUCache.Entry#ttl})
  5255. * if relevant.
  5256. */
  5257. info(key) {
  5258. const i = this.#keyMap.get(key);
  5259. if (i === undefined)
  5260. return undefined;
  5261. const v = this.#valList[i];
  5262. const value = this.#isBackgroundFetch(v)
  5263. ? v.__staleWhileFetching
  5264. : v;
  5265. if (value === undefined)
  5266. return undefined;
  5267. const entry = { value };
  5268. if (this.#ttls && this.#starts) {
  5269. const ttl = this.#ttls[i];
  5270. const start = this.#starts[i];
  5271. if (ttl && start) {
  5272. const remain = ttl - (perf.now() - start);
  5273. entry.ttl = remain;
  5274. entry.start = Date.now();
  5275. }
  5276. }
  5277. if (this.#sizes) {
  5278. entry.size = this.#sizes[i];
  5279. }
  5280. return entry;
  5281. }
  5282. /**
  5283. * Return an array of [key, {@link LRUCache.Entry}] tuples which can be
  5284. * passed to {@link LRLUCache#load}.
  5285. *
  5286. * The `start` fields are calculated relative to a portable `Date.now()`
  5287. * timestamp, even if `performance.now()` is available.
  5288. *
  5289. * Stale entries are always included in the `dump`, even if
  5290. * {@link LRUCache.OptionsBase.allowStale} is false.
  5291. *
  5292. * Note: this returns an actual array, not a generator, so it can be more
  5293. * easily passed around.
  5294. */
  5295. dump() {
  5296. const arr = [];
  5297. for (const i of this.#indexes({ allowStale: true })) {
  5298. const key = this.#keyList[i];
  5299. const v = this.#valList[i];
  5300. const value = this.#isBackgroundFetch(v)
  5301. ? v.__staleWhileFetching
  5302. : v;
  5303. if (value === undefined || key === undefined)
  5304. continue;
  5305. const entry = { value };
  5306. if (this.#ttls && this.#starts) {
  5307. entry.ttl = this.#ttls[i];
  5308. // always dump the start relative to a portable timestamp
  5309. // it's ok for this to be a bit slow, it's a rare operation.
  5310. const age = perf.now() - this.#starts[i];
  5311. entry.start = Math.floor(Date.now() - age);
  5312. }
  5313. if (this.#sizes) {
  5314. entry.size = this.#sizes[i];
  5315. }
  5316. arr.unshift([key, entry]);
  5317. }
  5318. return arr;
  5319. }
  5320. /**
  5321. * Reset the cache and load in the items in entries in the order listed.
  5322. *
  5323. * The shape of the resulting cache may be different if the same options are
  5324. * not used in both caches.
  5325. *
  5326. * The `start` fields are assumed to be calculated relative to a portable
  5327. * `Date.now()` timestamp, even if `performance.now()` is available.
  5328. */
  5329. load(arr) {
  5330. this.clear();
  5331. for (const [key, entry] of arr) {
  5332. if (entry.start) {
  5333. // entry.start is a portable timestamp, but we may be using
  5334. // node's performance.now(), so calculate the offset, so that
  5335. // we get the intended remaining TTL, no matter how long it's
  5336. // been on ice.
  5337. //
  5338. // it's ok for this to be a bit slow, it's a rare operation.
  5339. const age = Date.now() - entry.start;
  5340. entry.start = perf.now() - age;
  5341. }
  5342. this.set(key, entry.value, entry);
  5343. }
  5344. }
  5345. /**
  5346. * Add a value to the cache.
  5347. *
  5348. * Note: if `undefined` is specified as a value, this is an alias for
  5349. * {@link LRUCache#delete}
  5350. *
  5351. * Fields on the {@link LRUCache.SetOptions} options param will override
  5352. * their corresponding values in the constructor options for the scope
  5353. * of this single `set()` operation.
  5354. *
  5355. * If `start` is provided, then that will set the effective start
  5356. * time for the TTL calculation. Note that this must be a previous
  5357. * value of `performance.now()` if supported, or a previous value of
  5358. * `Date.now()` if not.
  5359. *
  5360. * Options object may also include `size`, which will prevent
  5361. * calling the `sizeCalculation` function and just use the specified
  5362. * number if it is a positive integer, and `noDisposeOnSet` which
  5363. * will prevent calling a `dispose` function in the case of
  5364. * overwrites.
  5365. *
  5366. * If the `size` (or return value of `sizeCalculation`) for a given
  5367. * entry is greater than `maxEntrySize`, then the item will not be
  5368. * added to the cache.
  5369. *
  5370. * Will update the recency of the entry.
  5371. *
  5372. * If the value is `undefined`, then this is an alias for
  5373. * `cache.delete(key)`. `undefined` is never stored in the cache.
  5374. */
  5375. set(k, v, setOptions = {}) {
  5376. if (v === undefined) {
  5377. this.delete(k);
  5378. return this;
  5379. }
  5380. const { ttl = this.ttl, start, noDisposeOnSet = this.noDisposeOnSet, sizeCalculation = this.sizeCalculation, status, } = setOptions;
  5381. let { noUpdateTTL = this.noUpdateTTL } = setOptions;
  5382. const size = this.#requireSize(k, v, setOptions.size || 0, sizeCalculation);
  5383. // if the item doesn't fit, don't do anything
  5384. // NB: maxEntrySize set to maxSize by default
  5385. if (this.maxEntrySize && size > this.maxEntrySize) {
  5386. if (status) {
  5387. status.set = 'miss';
  5388. status.maxEntrySizeExceeded = true;
  5389. }
  5390. // have to delete, in case something is there already.
  5391. this.#delete(k, 'set');
  5392. return this;
  5393. }
  5394. let index = this.#size === 0 ? undefined : this.#keyMap.get(k);
  5395. if (index === undefined) {
  5396. // addition
  5397. index = (this.#size === 0
  5398. ? this.#tail
  5399. : this.#free.length !== 0
  5400. ? this.#free.pop()
  5401. : this.#size === this.#max
  5402. ? this.#evict(false)
  5403. : this.#size);
  5404. this.#keyList[index] = k;
  5405. this.#valList[index] = v;
  5406. this.#keyMap.set(k, index);
  5407. this.#next[this.#tail] = index;
  5408. this.#prev[index] = this.#tail;
  5409. this.#tail = index;
  5410. this.#size++;
  5411. this.#addItemSize(index, size, status);
  5412. if (status)
  5413. status.set = 'add';
  5414. noUpdateTTL = false;
  5415. }
  5416. else {
  5417. // update
  5418. this.#moveToTail(index);
  5419. const oldVal = this.#valList[index];
  5420. if (v !== oldVal) {
  5421. if (this.#hasFetchMethod && this.#isBackgroundFetch(oldVal)) {
  5422. oldVal.__abortController.abort(new Error('replaced'));
  5423. const { __staleWhileFetching: s } = oldVal;
  5424. if (s !== undefined && !noDisposeOnSet) {
  5425. if (this.#hasDispose) {
  5426. this.#dispose?.(s, k, 'set');
  5427. }
  5428. if (this.#hasDisposeAfter) {
  5429. this.#disposed?.push([s, k, 'set']);
  5430. }
  5431. }
  5432. }
  5433. else if (!noDisposeOnSet) {
  5434. if (this.#hasDispose) {
  5435. this.#dispose?.(oldVal, k, 'set');
  5436. }
  5437. if (this.#hasDisposeAfter) {
  5438. this.#disposed?.push([oldVal, k, 'set']);
  5439. }
  5440. }
  5441. this.#removeItemSize(index);
  5442. this.#addItemSize(index, size, status);
  5443. this.#valList[index] = v;
  5444. if (status) {
  5445. status.set = 'replace';
  5446. const oldValue = oldVal && this.#isBackgroundFetch(oldVal)
  5447. ? oldVal.__staleWhileFetching
  5448. : oldVal;
  5449. if (oldValue !== undefined)
  5450. status.oldValue = oldValue;
  5451. }
  5452. }
  5453. else if (status) {
  5454. status.set = 'update';
  5455. }
  5456. }
  5457. if (ttl !== 0 && !this.#ttls) {
  5458. this.#initializeTTLTracking();
  5459. }
  5460. if (this.#ttls) {
  5461. if (!noUpdateTTL) {
  5462. this.#setItemTTL(index, ttl, start);
  5463. }
  5464. if (status)
  5465. this.#statusTTL(status, index);
  5466. }
  5467. if (!noDisposeOnSet && this.#hasDisposeAfter && this.#disposed) {
  5468. const dt = this.#disposed;
  5469. let task;
  5470. while ((task = dt?.shift())) {
  5471. this.#disposeAfter?.(...task);
  5472. }
  5473. }
  5474. return this;
  5475. }
  5476. /**
  5477. * Evict the least recently used item, returning its value or
  5478. * `undefined` if cache is empty.
  5479. */
  5480. pop() {
  5481. try {
  5482. while (this.#size) {
  5483. const val = this.#valList[this.#head];
  5484. this.#evict(true);
  5485. if (this.#isBackgroundFetch(val)) {
  5486. if (val.__staleWhileFetching) {
  5487. return val.__staleWhileFetching;
  5488. }
  5489. }
  5490. else if (val !== undefined) {
  5491. return val;
  5492. }
  5493. }
  5494. }
  5495. finally {
  5496. if (this.#hasDisposeAfter && this.#disposed) {
  5497. const dt = this.#disposed;
  5498. let task;
  5499. while ((task = dt?.shift())) {
  5500. this.#disposeAfter?.(...task);
  5501. }
  5502. }
  5503. }
  5504. }
  5505. #evict(free) {
  5506. const head = this.#head;
  5507. const k = this.#keyList[head];
  5508. const v = this.#valList[head];
  5509. if (this.#hasFetchMethod && this.#isBackgroundFetch(v)) {
  5510. v.__abortController.abort(new Error('evicted'));
  5511. }
  5512. else if (this.#hasDispose || this.#hasDisposeAfter) {
  5513. if (this.#hasDispose) {
  5514. this.#dispose?.(v, k, 'evict');
  5515. }
  5516. if (this.#hasDisposeAfter) {
  5517. this.#disposed?.push([v, k, 'evict']);
  5518. }
  5519. }
  5520. this.#removeItemSize(head);
  5521. // if we aren't about to use the index, then null these out
  5522. if (free) {
  5523. this.#keyList[head] = undefined;
  5524. this.#valList[head] = undefined;
  5525. this.#free.push(head);
  5526. }
  5527. if (this.#size === 1) {
  5528. this.#head = this.#tail = 0;
  5529. this.#free.length = 0;
  5530. }
  5531. else {
  5532. this.#head = this.#next[head];
  5533. }
  5534. this.#keyMap.delete(k);
  5535. this.#size--;
  5536. return head;
  5537. }
  5538. /**
  5539. * Check if a key is in the cache, without updating the recency of use.
  5540. * Will return false if the item is stale, even though it is technically
  5541. * in the cache.
  5542. *
  5543. * Check if a key is in the cache, without updating the recency of
  5544. * use. Age is updated if {@link LRUCache.OptionsBase.updateAgeOnHas} is set
  5545. * to `true` in either the options or the constructor.
  5546. *
  5547. * Will return `false` if the item is stale, even though it is technically in
  5548. * the cache. The difference can be determined (if it matters) by using a
  5549. * `status` argument, and inspecting the `has` field.
  5550. *
  5551. * Will not update item age unless
  5552. * {@link LRUCache.OptionsBase.updateAgeOnHas} is set.
  5553. */
  5554. has(k, hasOptions = {}) {
  5555. const { updateAgeOnHas = this.updateAgeOnHas, status } = hasOptions;
  5556. const index = this.#keyMap.get(k);
  5557. if (index !== undefined) {
  5558. const v = this.#valList[index];
  5559. if (this.#isBackgroundFetch(v) &&
  5560. v.__staleWhileFetching === undefined) {
  5561. return false;
  5562. }
  5563. if (!this.#isStale(index)) {
  5564. if (updateAgeOnHas) {
  5565. this.#updateItemAge(index);
  5566. }
  5567. if (status) {
  5568. status.has = 'hit';
  5569. this.#statusTTL(status, index);
  5570. }
  5571. return true;
  5572. }
  5573. else if (status) {
  5574. status.has = 'stale';
  5575. this.#statusTTL(status, index);
  5576. }
  5577. }
  5578. else if (status) {
  5579. status.has = 'miss';
  5580. }
  5581. return false;
  5582. }
  5583. /**
  5584. * Like {@link LRUCache#get} but doesn't update recency or delete stale
  5585. * items.
  5586. *
  5587. * Returns `undefined` if the item is stale, unless
  5588. * {@link LRUCache.OptionsBase.allowStale} is set.
  5589. */
  5590. peek(k, peekOptions = {}) {
  5591. const { allowStale = this.allowStale } = peekOptions;
  5592. const index = this.#keyMap.get(k);
  5593. if (index === undefined ||
  5594. (!allowStale && this.#isStale(index))) {
  5595. return;
  5596. }
  5597. const v = this.#valList[index];
  5598. // either stale and allowed, or forcing a refresh of non-stale value
  5599. return this.#isBackgroundFetch(v) ? v.__staleWhileFetching : v;
  5600. }
  5601. #backgroundFetch(k, index, options, context) {
  5602. const v = index === undefined ? undefined : this.#valList[index];
  5603. if (this.#isBackgroundFetch(v)) {
  5604. return v;
  5605. }
  5606. const ac = new AC();
  5607. const { signal } = options;
  5608. // when/if our AC signals, then stop listening to theirs.
  5609. signal?.addEventListener('abort', () => ac.abort(signal.reason), {
  5610. signal: ac.signal,
  5611. });
  5612. const fetchOpts = {
  5613. signal: ac.signal,
  5614. options,
  5615. context,
  5616. };
  5617. const cb = (v, updateCache = false) => {
  5618. const { aborted } = ac.signal;
  5619. const ignoreAbort = options.ignoreFetchAbort && v !== undefined;
  5620. if (options.status) {
  5621. if (aborted && !updateCache) {
  5622. options.status.fetchAborted = true;
  5623. options.status.fetchError = ac.signal.reason;
  5624. if (ignoreAbort)
  5625. options.status.fetchAbortIgnored = true;
  5626. }
  5627. else {
  5628. options.status.fetchResolved = true;
  5629. }
  5630. }
  5631. if (aborted && !ignoreAbort && !updateCache) {
  5632. return fetchFail(ac.signal.reason);
  5633. }
  5634. // either we didn't abort, and are still here, or we did, and ignored
  5635. const bf = p;
  5636. if (this.#valList[index] === p) {
  5637. if (v === undefined) {
  5638. if (bf.__staleWhileFetching) {
  5639. this.#valList[index] = bf.__staleWhileFetching;
  5640. }
  5641. else {
  5642. this.#delete(k, 'fetch');
  5643. }
  5644. }
  5645. else {
  5646. if (options.status)
  5647. options.status.fetchUpdated = true;
  5648. this.set(k, v, fetchOpts.options);
  5649. }
  5650. }
  5651. return v;
  5652. };
  5653. const eb = (er) => {
  5654. if (options.status) {
  5655. options.status.fetchRejected = true;
  5656. options.status.fetchError = er;
  5657. }
  5658. return fetchFail(er);
  5659. };
  5660. const fetchFail = (er) => {
  5661. const { aborted } = ac.signal;
  5662. const allowStaleAborted = aborted && options.allowStaleOnFetchAbort;
  5663. const allowStale = allowStaleAborted || options.allowStaleOnFetchRejection;
  5664. const noDelete = allowStale || options.noDeleteOnFetchRejection;
  5665. const bf = p;
  5666. if (this.#valList[index] === p) {
  5667. // if we allow stale on fetch rejections, then we need to ensure that
  5668. // the stale value is not removed from the cache when the fetch fails.
  5669. const del = !noDelete || bf.__staleWhileFetching === undefined;
  5670. if (del) {
  5671. this.#delete(k, 'fetch');
  5672. }
  5673. else if (!allowStaleAborted) {
  5674. // still replace the *promise* with the stale value,
  5675. // since we are done with the promise at this point.
  5676. // leave it untouched if we're still waiting for an
  5677. // aborted background fetch that hasn't yet returned.
  5678. this.#valList[index] = bf.__staleWhileFetching;
  5679. }
  5680. }
  5681. if (allowStale) {
  5682. if (options.status && bf.__staleWhileFetching !== undefined) {
  5683. options.status.returnedStale = true;
  5684. }
  5685. return bf.__staleWhileFetching;
  5686. }
  5687. else if (bf.__returned === bf) {
  5688. throw er;
  5689. }
  5690. };
  5691. const pcall = (res, rej) => {
  5692. const fmp = this.#fetchMethod?.(k, v, fetchOpts);
  5693. if (fmp && fmp instanceof Promise) {
  5694. fmp.then(v => res(v === undefined ? undefined : v), rej);
  5695. }
  5696. // ignored, we go until we finish, regardless.
  5697. // defer check until we are actually aborting,
  5698. // so fetchMethod can override.
  5699. ac.signal.addEventListener('abort', () => {
  5700. if (!options.ignoreFetchAbort ||
  5701. options.allowStaleOnFetchAbort) {
  5702. res(undefined);
  5703. // when it eventually resolves, update the cache.
  5704. if (options.allowStaleOnFetchAbort) {
  5705. res = v => cb(v, true);
  5706. }
  5707. }
  5708. });
  5709. };
  5710. if (options.status)
  5711. options.status.fetchDispatched = true;
  5712. const p = new Promise(pcall).then(cb, eb);
  5713. const bf = Object.assign(p, {
  5714. __abortController: ac,
  5715. __staleWhileFetching: v,
  5716. __returned: undefined,
  5717. });
  5718. if (index === undefined) {
  5719. // internal, don't expose status.
  5720. this.set(k, bf, { ...fetchOpts.options, status: undefined });
  5721. index = this.#keyMap.get(k);
  5722. }
  5723. else {
  5724. this.#valList[index] = bf;
  5725. }
  5726. return bf;
  5727. }
  5728. #isBackgroundFetch(p) {
  5729. if (!this.#hasFetchMethod)
  5730. return false;
  5731. const b = p;
  5732. return (!!b &&
  5733. b instanceof Promise &&
  5734. b.hasOwnProperty('__staleWhileFetching') &&
  5735. b.__abortController instanceof AC);
  5736. }
  5737. async fetch(k, fetchOptions = {}) {
  5738. const {
  5739. // get options
  5740. allowStale = this.allowStale, updateAgeOnGet = this.updateAgeOnGet, noDeleteOnStaleGet = this.noDeleteOnStaleGet,
  5741. // set options
  5742. ttl = this.ttl, noDisposeOnSet = this.noDisposeOnSet, size = 0, sizeCalculation = this.sizeCalculation, noUpdateTTL = this.noUpdateTTL,
  5743. // fetch exclusive options
  5744. noDeleteOnFetchRejection = this.noDeleteOnFetchRejection, allowStaleOnFetchRejection = this.allowStaleOnFetchRejection, ignoreFetchAbort = this.ignoreFetchAbort, allowStaleOnFetchAbort = this.allowStaleOnFetchAbort, context, forceRefresh = false, status, signal, } = fetchOptions;
  5745. if (!this.#hasFetchMethod) {
  5746. if (status)
  5747. status.fetch = 'get';
  5748. return this.get(k, {
  5749. allowStale,
  5750. updateAgeOnGet,
  5751. noDeleteOnStaleGet,
  5752. status,
  5753. });
  5754. }
  5755. const options = {
  5756. allowStale,
  5757. updateAgeOnGet,
  5758. noDeleteOnStaleGet,
  5759. ttl,
  5760. noDisposeOnSet,
  5761. size,
  5762. sizeCalculation,
  5763. noUpdateTTL,
  5764. noDeleteOnFetchRejection,
  5765. allowStaleOnFetchRejection,
  5766. allowStaleOnFetchAbort,
  5767. ignoreFetchAbort,
  5768. status,
  5769. signal,
  5770. };
  5771. let index = this.#keyMap.get(k);
  5772. if (index === undefined) {
  5773. if (status)
  5774. status.fetch = 'miss';
  5775. const p = this.#backgroundFetch(k, index, options, context);
  5776. return (p.__returned = p);
  5777. }
  5778. else {
  5779. // in cache, maybe already fetching
  5780. const v = this.#valList[index];
  5781. if (this.#isBackgroundFetch(v)) {
  5782. const stale = allowStale && v.__staleWhileFetching !== undefined;
  5783. if (status) {
  5784. status.fetch = 'inflight';
  5785. if (stale)
  5786. status.returnedStale = true;
  5787. }
  5788. return stale ? v.__staleWhileFetching : (v.__returned = v);
  5789. }
  5790. // if we force a refresh, that means do NOT serve the cached value,
  5791. // unless we are already in the process of refreshing the cache.
  5792. const isStale = this.#isStale(index);
  5793. if (!forceRefresh && !isStale) {
  5794. if (status)
  5795. status.fetch = 'hit';
  5796. this.#moveToTail(index);
  5797. if (updateAgeOnGet) {
  5798. this.#updateItemAge(index);
  5799. }
  5800. if (status)
  5801. this.#statusTTL(status, index);
  5802. return v;
  5803. }
  5804. // ok, it is stale or a forced refresh, and not already fetching.
  5805. // refresh the cache.
  5806. const p = this.#backgroundFetch(k, index, options, context);
  5807. const hasStale = p.__staleWhileFetching !== undefined;
  5808. const staleVal = hasStale && allowStale;
  5809. if (status) {
  5810. status.fetch = isStale ? 'stale' : 'refresh';
  5811. if (staleVal && isStale)
  5812. status.returnedStale = true;
  5813. }
  5814. return staleVal ? p.__staleWhileFetching : (p.__returned = p);
  5815. }
  5816. }
  5817. async forceFetch(k, fetchOptions = {}) {
  5818. const v = await this.fetch(k, fetchOptions);
  5819. if (v === undefined)
  5820. throw new Error('fetch() returned undefined');
  5821. return v;
  5822. }
  5823. memo(k, memoOptions = {}) {
  5824. const memoMethod = this.#memoMethod;
  5825. if (!memoMethod) {
  5826. throw new Error('no memoMethod provided to constructor');
  5827. }
  5828. const { context, forceRefresh, ...options } = memoOptions;
  5829. const v = this.get(k, options);
  5830. if (!forceRefresh && v !== undefined)
  5831. return v;
  5832. const vv = memoMethod(k, v, {
  5833. options,
  5834. context,
  5835. });
  5836. this.set(k, vv, options);
  5837. return vv;
  5838. }
  5839. /**
  5840. * Return a value from the cache. Will update the recency of the cache
  5841. * entry found.
  5842. *
  5843. * If the key is not found, get() will return `undefined`.
  5844. */
  5845. get(k, getOptions = {}) {
  5846. const { allowStale = this.allowStale, updateAgeOnGet = this.updateAgeOnGet, noDeleteOnStaleGet = this.noDeleteOnStaleGet, status, } = getOptions;
  5847. const index = this.#keyMap.get(k);
  5848. if (index !== undefined) {
  5849. const value = this.#valList[index];
  5850. const fetching = this.#isBackgroundFetch(value);
  5851. if (status)
  5852. this.#statusTTL(status, index);
  5853. if (this.#isStale(index)) {
  5854. if (status)
  5855. status.get = 'stale';
  5856. // delete only if not an in-flight background fetch
  5857. if (!fetching) {
  5858. if (!noDeleteOnStaleGet) {
  5859. this.#delete(k, 'expire');
  5860. }
  5861. if (status && allowStale)
  5862. status.returnedStale = true;
  5863. return allowStale ? value : undefined;
  5864. }
  5865. else {
  5866. if (status &&
  5867. allowStale &&
  5868. value.__staleWhileFetching !== undefined) {
  5869. status.returnedStale = true;
  5870. }
  5871. return allowStale ? value.__staleWhileFetching : undefined;
  5872. }
  5873. }
  5874. else {
  5875. if (status)
  5876. status.get = 'hit';
  5877. // if we're currently fetching it, we don't actually have it yet
  5878. // it's not stale, which means this isn't a staleWhileRefetching.
  5879. // If it's not stale, and fetching, AND has a __staleWhileFetching
  5880. // value, then that means the user fetched with {forceRefresh:true},
  5881. // so it's safe to return that value.
  5882. if (fetching) {
  5883. return value.__staleWhileFetching;
  5884. }
  5885. this.#moveToTail(index);
  5886. if (updateAgeOnGet) {
  5887. this.#updateItemAge(index);
  5888. }
  5889. return value;
  5890. }
  5891. }
  5892. else if (status) {
  5893. status.get = 'miss';
  5894. }
  5895. }
  5896. #connect(p, n) {
  5897. this.#prev[n] = p;
  5898. this.#next[p] = n;
  5899. }
  5900. #moveToTail(index) {
  5901. // if tail already, nothing to do
  5902. // if head, move head to next[index]
  5903. // else
  5904. // move next[prev[index]] to next[index] (head has no prev)
  5905. // move prev[next[index]] to prev[index]
  5906. // prev[index] = tail
  5907. // next[tail] = index
  5908. // tail = index
  5909. if (index !== this.#tail) {
  5910. if (index === this.#head) {
  5911. this.#head = this.#next[index];
  5912. }
  5913. else {
  5914. this.#connect(this.#prev[index], this.#next[index]);
  5915. }
  5916. this.#connect(this.#tail, index);
  5917. this.#tail = index;
  5918. }
  5919. }
  5920. /**
  5921. * Deletes a key out of the cache.
  5922. *
  5923. * Returns true if the key was deleted, false otherwise.
  5924. */
  5925. delete(k) {
  5926. return this.#delete(k, 'delete');
  5927. }
  5928. #delete(k, reason) {
  5929. let deleted = false;
  5930. if (this.#size !== 0) {
  5931. const index = this.#keyMap.get(k);
  5932. if (index !== undefined) {
  5933. deleted = true;
  5934. if (this.#size === 1) {
  5935. this.#clear(reason);
  5936. }
  5937. else {
  5938. this.#removeItemSize(index);
  5939. const v = this.#valList[index];
  5940. if (this.#isBackgroundFetch(v)) {
  5941. v.__abortController.abort(new Error('deleted'));
  5942. }
  5943. else if (this.#hasDispose || this.#hasDisposeAfter) {
  5944. if (this.#hasDispose) {
  5945. this.#dispose?.(v, k, reason);
  5946. }
  5947. if (this.#hasDisposeAfter) {
  5948. this.#disposed?.push([v, k, reason]);
  5949. }
  5950. }
  5951. this.#keyMap.delete(k);
  5952. this.#keyList[index] = undefined;
  5953. this.#valList[index] = undefined;
  5954. if (index === this.#tail) {
  5955. this.#tail = this.#prev[index];
  5956. }
  5957. else if (index === this.#head) {
  5958. this.#head = this.#next[index];
  5959. }
  5960. else {
  5961. const pi = this.#prev[index];
  5962. this.#next[pi] = this.#next[index];
  5963. const ni = this.#next[index];
  5964. this.#prev[ni] = this.#prev[index];
  5965. }
  5966. this.#size--;
  5967. this.#free.push(index);
  5968. }
  5969. }
  5970. }
  5971. if (this.#hasDisposeAfter && this.#disposed?.length) {
  5972. const dt = this.#disposed;
  5973. let task;
  5974. while ((task = dt?.shift())) {
  5975. this.#disposeAfter?.(...task);
  5976. }
  5977. }
  5978. return deleted;
  5979. }
  5980. /**
  5981. * Clear the cache entirely, throwing away all values.
  5982. */
  5983. clear() {
  5984. return this.#clear('delete');
  5985. }
  5986. #clear(reason) {
  5987. for (const index of this.#rindexes({ allowStale: true })) {
  5988. const v = this.#valList[index];
  5989. if (this.#isBackgroundFetch(v)) {
  5990. v.__abortController.abort(new Error('deleted'));
  5991. }
  5992. else {
  5993. const k = this.#keyList[index];
  5994. if (this.#hasDispose) {
  5995. this.#dispose?.(v, k, reason);
  5996. }
  5997. if (this.#hasDisposeAfter) {
  5998. this.#disposed?.push([v, k, reason]);
  5999. }
  6000. }
  6001. }
  6002. this.#keyMap.clear();
  6003. this.#valList.fill(undefined);
  6004. this.#keyList.fill(undefined);
  6005. if (this.#ttls && this.#starts) {
  6006. this.#ttls.fill(0);
  6007. this.#starts.fill(0);
  6008. }
  6009. if (this.#sizes) {
  6010. this.#sizes.fill(0);
  6011. }
  6012. this.#head = 0;
  6013. this.#tail = 0;
  6014. this.#free.length = 0;
  6015. this.#calculatedSize = 0;
  6016. this.#size = 0;
  6017. if (this.#hasDisposeAfter && this.#disposed) {
  6018. const dt = this.#disposed;
  6019. let task;
  6020. while ((task = dt?.shift())) {
  6021. this.#disposeAfter?.(...task);
  6022. }
  6023. }
  6024. }
  6025. }
  6026. const proc = typeof process === 'object' && process
  6027. ? process
  6028. : {
  6029. stdout: null,
  6030. stderr: null,
  6031. };
  6032. /**
  6033. * Return true if the argument is a Minipass stream, Node stream, or something
  6034. * else that Minipass can interact with.
  6035. */
  6036. const isStream = (s) => !!s &&
  6037. typeof s === 'object' &&
  6038. (s instanceof Minipass ||
  6039. s instanceof Stream$1 ||
  6040. isReadable(s) ||
  6041. isWritable(s));
  6042. /**
  6043. * Return true if the argument is a valid {@link Minipass.Readable}
  6044. */
  6045. const isReadable = (s) => !!s &&
  6046. typeof s === 'object' &&
  6047. s instanceof EventEmitter$4 &&
  6048. typeof s.pipe === 'function' &&
  6049. // node core Writable streams have a pipe() method, but it throws
  6050. s.pipe !== Stream$1.Writable.prototype.pipe;
  6051. /**
  6052. * Return true if the argument is a valid {@link Minipass.Writable}
  6053. */
  6054. const isWritable = (s) => !!s &&
  6055. typeof s === 'object' &&
  6056. s instanceof EventEmitter$4 &&
  6057. typeof s.write === 'function' &&
  6058. typeof s.end === 'function';
  6059. const EOF = Symbol('EOF');
  6060. const MAYBE_EMIT_END = Symbol('maybeEmitEnd');
  6061. const EMITTED_END = Symbol('emittedEnd');
  6062. const EMITTING_END = Symbol('emittingEnd');
  6063. const EMITTED_ERROR = Symbol('emittedError');
  6064. const CLOSED$1 = Symbol('closed');
  6065. const READ = Symbol('read');
  6066. const FLUSH = Symbol('flush');
  6067. const FLUSHCHUNK = Symbol('flushChunk');
  6068. const ENCODING$1 = Symbol('encoding');
  6069. const DECODER = Symbol('decoder');
  6070. const FLOWING = Symbol('flowing');
  6071. const PAUSED = Symbol('paused');
  6072. const RESUME = Symbol('resume');
  6073. const BUFFER = Symbol('buffer');
  6074. const PIPES = Symbol('pipes');
  6075. const BUFFERLENGTH = Symbol('bufferLength');
  6076. const BUFFERPUSH = Symbol('bufferPush');
  6077. const BUFFERSHIFT = Symbol('bufferShift');
  6078. const OBJECTMODE = Symbol('objectMode');
  6079. // internal event when stream is destroyed
  6080. const DESTROYED = Symbol('destroyed');
  6081. // internal event when stream has an error
  6082. const ERROR = Symbol('error');
  6083. const EMITDATA = Symbol('emitData');
  6084. const EMITEND = Symbol('emitEnd');
  6085. const EMITEND2 = Symbol('emitEnd2');
  6086. const ASYNC = Symbol('async');
  6087. const ABORT = Symbol('abort');
  6088. const ABORTED = Symbol('aborted');
  6089. const SIGNAL = Symbol('signal');
  6090. const DATALISTENERS = Symbol('dataListeners');
  6091. const DISCARDED = Symbol('discarded');
  6092. const defer$3 = (fn) => Promise.resolve().then(fn);
  6093. const nodefer = (fn) => fn();
  6094. const isEndish = (ev) => ev === 'end' || ev === 'finish' || ev === 'prefinish';
  6095. const isArrayBufferLike = (b) => b instanceof ArrayBuffer ||
  6096. (!!b &&
  6097. typeof b === 'object' &&
  6098. b.constructor &&
  6099. b.constructor.name === 'ArrayBuffer' &&
  6100. b.byteLength >= 0);
  6101. const isArrayBufferView = (b) => !Buffer.isBuffer(b) && ArrayBuffer.isView(b);
  6102. /**
  6103. * Internal class representing a pipe to a destination stream.
  6104. *
  6105. * @internal
  6106. */
  6107. class Pipe {
  6108. src;
  6109. dest;
  6110. opts;
  6111. ondrain;
  6112. constructor(src, dest, opts) {
  6113. this.src = src;
  6114. this.dest = dest;
  6115. this.opts = opts;
  6116. this.ondrain = () => src[RESUME]();
  6117. this.dest.on('drain', this.ondrain);
  6118. }
  6119. unpipe() {
  6120. this.dest.removeListener('drain', this.ondrain);
  6121. }
  6122. // only here for the prototype
  6123. /* c8 ignore start */
  6124. proxyErrors(_er) { }
  6125. /* c8 ignore stop */
  6126. end() {
  6127. this.unpipe();
  6128. if (this.opts.end)
  6129. this.dest.end();
  6130. }
  6131. }
  6132. /**
  6133. * Internal class representing a pipe to a destination stream where
  6134. * errors are proxied.
  6135. *
  6136. * @internal
  6137. */
  6138. class PipeProxyErrors extends Pipe {
  6139. unpipe() {
  6140. this.src.removeListener('error', this.proxyErrors);
  6141. super.unpipe();
  6142. }
  6143. constructor(src, dest, opts) {
  6144. super(src, dest, opts);
  6145. this.proxyErrors = er => dest.emit('error', er);
  6146. src.on('error', this.proxyErrors);
  6147. }
  6148. }
  6149. const isObjectModeOptions = (o) => !!o.objectMode;
  6150. const isEncodingOptions = (o) => !o.objectMode && !!o.encoding && o.encoding !== 'buffer';
  6151. /**
  6152. * Main export, the Minipass class
  6153. *
  6154. * `RType` is the type of data emitted, defaults to Buffer
  6155. *
  6156. * `WType` is the type of data to be written, if RType is buffer or string,
  6157. * then any {@link Minipass.ContiguousData} is allowed.
  6158. *
  6159. * `Events` is the set of event handler signatures that this object
  6160. * will emit, see {@link Minipass.Events}
  6161. */
  6162. class Minipass extends EventEmitter$4 {
  6163. [FLOWING] = false;
  6164. [PAUSED] = false;
  6165. [PIPES] = [];
  6166. [BUFFER] = [];
  6167. [OBJECTMODE];
  6168. [ENCODING$1];
  6169. [ASYNC];
  6170. [DECODER];
  6171. [EOF] = false;
  6172. [EMITTED_END] = false;
  6173. [EMITTING_END] = false;
  6174. [CLOSED$1] = false;
  6175. [EMITTED_ERROR] = null;
  6176. [BUFFERLENGTH] = 0;
  6177. [DESTROYED] = false;
  6178. [SIGNAL];
  6179. [ABORTED] = false;
  6180. [DATALISTENERS] = 0;
  6181. [DISCARDED] = false;
  6182. /**
  6183. * true if the stream can be written
  6184. */
  6185. writable = true;
  6186. /**
  6187. * true if the stream can be read
  6188. */
  6189. readable = true;
  6190. /**
  6191. * If `RType` is Buffer, then options do not need to be provided.
  6192. * Otherwise, an options object must be provided to specify either
  6193. * {@link Minipass.SharedOptions.objectMode} or
  6194. * {@link Minipass.SharedOptions.encoding}, as appropriate.
  6195. */
  6196. constructor(...args) {
  6197. const options = (args[0] ||
  6198. {});
  6199. super();
  6200. if (options.objectMode && typeof options.encoding === 'string') {
  6201. throw new TypeError('Encoding and objectMode may not be used together');
  6202. }
  6203. if (isObjectModeOptions(options)) {
  6204. this[OBJECTMODE] = true;
  6205. this[ENCODING$1] = null;
  6206. }
  6207. else if (isEncodingOptions(options)) {
  6208. this[ENCODING$1] = options.encoding;
  6209. this[OBJECTMODE] = false;
  6210. }
  6211. else {
  6212. this[OBJECTMODE] = false;
  6213. this[ENCODING$1] = null;
  6214. }
  6215. this[ASYNC] = !!options.async;
  6216. this[DECODER] = this[ENCODING$1]
  6217. ? new StringDecoder(this[ENCODING$1])
  6218. : null;
  6219. //@ts-ignore - private option for debugging and testing
  6220. if (options && options.debugExposeBuffer === true) {
  6221. Object.defineProperty(this, 'buffer', { get: () => this[BUFFER] });
  6222. }
  6223. //@ts-ignore - private option for debugging and testing
  6224. if (options && options.debugExposePipes === true) {
  6225. Object.defineProperty(this, 'pipes', { get: () => this[PIPES] });
  6226. }
  6227. const { signal } = options;
  6228. if (signal) {
  6229. this[SIGNAL] = signal;
  6230. if (signal.aborted) {
  6231. this[ABORT]();
  6232. }
  6233. else {
  6234. signal.addEventListener('abort', () => this[ABORT]());
  6235. }
  6236. }
  6237. }
  6238. /**
  6239. * The amount of data stored in the buffer waiting to be read.
  6240. *
  6241. * For Buffer strings, this will be the total byte length.
  6242. * For string encoding streams, this will be the string character length,
  6243. * according to JavaScript's `string.length` logic.
  6244. * For objectMode streams, this is a count of the items waiting to be
  6245. * emitted.
  6246. */
  6247. get bufferLength() {
  6248. return this[BUFFERLENGTH];
  6249. }
  6250. /**
  6251. * The `BufferEncoding` currently in use, or `null`
  6252. */
  6253. get encoding() {
  6254. return this[ENCODING$1];
  6255. }
  6256. /**
  6257. * @deprecated - This is a read only property
  6258. */
  6259. set encoding(_enc) {
  6260. throw new Error('Encoding must be set at instantiation time');
  6261. }
  6262. /**
  6263. * @deprecated - Encoding may only be set at instantiation time
  6264. */
  6265. setEncoding(_enc) {
  6266. throw new Error('Encoding must be set at instantiation time');
  6267. }
  6268. /**
  6269. * True if this is an objectMode stream
  6270. */
  6271. get objectMode() {
  6272. return this[OBJECTMODE];
  6273. }
  6274. /**
  6275. * @deprecated - This is a read-only property
  6276. */
  6277. set objectMode(_om) {
  6278. throw new Error('objectMode must be set at instantiation time');
  6279. }
  6280. /**
  6281. * true if this is an async stream
  6282. */
  6283. get ['async']() {
  6284. return this[ASYNC];
  6285. }
  6286. /**
  6287. * Set to true to make this stream async.
  6288. *
  6289. * Once set, it cannot be unset, as this would potentially cause incorrect
  6290. * behavior. Ie, a sync stream can be made async, but an async stream
  6291. * cannot be safely made sync.
  6292. */
  6293. set ['async'](a) {
  6294. this[ASYNC] = this[ASYNC] || !!a;
  6295. }
  6296. // drop everything and get out of the flow completely
  6297. [ABORT]() {
  6298. this[ABORTED] = true;
  6299. this.emit('abort', this[SIGNAL]?.reason);
  6300. this.destroy(this[SIGNAL]?.reason);
  6301. }
  6302. /**
  6303. * True if the stream has been aborted.
  6304. */
  6305. get aborted() {
  6306. return this[ABORTED];
  6307. }
  6308. /**
  6309. * No-op setter. Stream aborted status is set via the AbortSignal provided
  6310. * in the constructor options.
  6311. */
  6312. set aborted(_) { }
  6313. write(chunk, encoding, cb) {
  6314. if (this[ABORTED])
  6315. return false;
  6316. if (this[EOF])
  6317. throw new Error('write after end');
  6318. if (this[DESTROYED]) {
  6319. this.emit('error', Object.assign(new Error('Cannot call write after a stream was destroyed'), { code: 'ERR_STREAM_DESTROYED' }));
  6320. return true;
  6321. }
  6322. if (typeof encoding === 'function') {
  6323. cb = encoding;
  6324. encoding = 'utf8';
  6325. }
  6326. if (!encoding)
  6327. encoding = 'utf8';
  6328. const fn = this[ASYNC] ? defer$3 : nodefer;
  6329. // convert array buffers and typed array views into buffers
  6330. // at some point in the future, we may want to do the opposite!
  6331. // leave strings and buffers as-is
  6332. // anything is only allowed if in object mode, so throw
  6333. if (!this[OBJECTMODE] && !Buffer.isBuffer(chunk)) {
  6334. if (isArrayBufferView(chunk)) {
  6335. //@ts-ignore - sinful unsafe type changing
  6336. chunk = Buffer.from(chunk.buffer, chunk.byteOffset, chunk.byteLength);
  6337. }
  6338. else if (isArrayBufferLike(chunk)) {
  6339. //@ts-ignore - sinful unsafe type changing
  6340. chunk = Buffer.from(chunk);
  6341. }
  6342. else if (typeof chunk !== 'string') {
  6343. throw new Error('Non-contiguous data written to non-objectMode stream');
  6344. }
  6345. }
  6346. // handle object mode up front, since it's simpler
  6347. // this yields better performance, fewer checks later.
  6348. if (this[OBJECTMODE]) {
  6349. // maybe impossible?
  6350. /* c8 ignore start */
  6351. if (this[FLOWING] && this[BUFFERLENGTH] !== 0)
  6352. this[FLUSH](true);
  6353. /* c8 ignore stop */
  6354. if (this[FLOWING])
  6355. this.emit('data', chunk);
  6356. else
  6357. this[BUFFERPUSH](chunk);
  6358. if (this[BUFFERLENGTH] !== 0)
  6359. this.emit('readable');
  6360. if (cb)
  6361. fn(cb);
  6362. return this[FLOWING];
  6363. }
  6364. // at this point the chunk is a buffer or string
  6365. // don't buffer it up or send it to the decoder
  6366. if (!chunk.length) {
  6367. if (this[BUFFERLENGTH] !== 0)
  6368. this.emit('readable');
  6369. if (cb)
  6370. fn(cb);
  6371. return this[FLOWING];
  6372. }
  6373. // fast-path writing strings of same encoding to a stream with
  6374. // an empty buffer, skipping the buffer/decoder dance
  6375. if (typeof chunk === 'string' &&
  6376. // unless it is a string already ready for us to use
  6377. !(encoding === this[ENCODING$1] && !this[DECODER]?.lastNeed)) {
  6378. //@ts-ignore - sinful unsafe type change
  6379. chunk = Buffer.from(chunk, encoding);
  6380. }
  6381. if (Buffer.isBuffer(chunk) && this[ENCODING$1]) {
  6382. //@ts-ignore - sinful unsafe type change
  6383. chunk = this[DECODER].write(chunk);
  6384. }
  6385. // Note: flushing CAN potentially switch us into not-flowing mode
  6386. if (this[FLOWING] && this[BUFFERLENGTH] !== 0)
  6387. this[FLUSH](true);
  6388. if (this[FLOWING])
  6389. this.emit('data', chunk);
  6390. else
  6391. this[BUFFERPUSH](chunk);
  6392. if (this[BUFFERLENGTH] !== 0)
  6393. this.emit('readable');
  6394. if (cb)
  6395. fn(cb);
  6396. return this[FLOWING];
  6397. }
  6398. /**
  6399. * Low-level explicit read method.
  6400. *
  6401. * In objectMode, the argument is ignored, and one item is returned if
  6402. * available.
  6403. *
  6404. * `n` is the number of bytes (or in the case of encoding streams,
  6405. * characters) to consume. If `n` is not provided, then the entire buffer
  6406. * is returned, or `null` is returned if no data is available.
  6407. *
  6408. * If `n` is greater that the amount of data in the internal buffer,
  6409. * then `null` is returned.
  6410. */
  6411. read(n) {
  6412. if (this[DESTROYED])
  6413. return null;
  6414. this[DISCARDED] = false;
  6415. if (this[BUFFERLENGTH] === 0 ||
  6416. n === 0 ||
  6417. (n && n > this[BUFFERLENGTH])) {
  6418. this[MAYBE_EMIT_END]();
  6419. return null;
  6420. }
  6421. if (this[OBJECTMODE])
  6422. n = null;
  6423. if (this[BUFFER].length > 1 && !this[OBJECTMODE]) {
  6424. // not object mode, so if we have an encoding, then RType is string
  6425. // otherwise, must be Buffer
  6426. this[BUFFER] = [
  6427. (this[ENCODING$1]
  6428. ? this[BUFFER].join('')
  6429. : Buffer.concat(this[BUFFER], this[BUFFERLENGTH])),
  6430. ];
  6431. }
  6432. const ret = this[READ](n || null, this[BUFFER][0]);
  6433. this[MAYBE_EMIT_END]();
  6434. return ret;
  6435. }
  6436. [READ](n, chunk) {
  6437. if (this[OBJECTMODE])
  6438. this[BUFFERSHIFT]();
  6439. else {
  6440. const c = chunk;
  6441. if (n === c.length || n === null)
  6442. this[BUFFERSHIFT]();
  6443. else if (typeof c === 'string') {
  6444. this[BUFFER][0] = c.slice(n);
  6445. chunk = c.slice(0, n);
  6446. this[BUFFERLENGTH] -= n;
  6447. }
  6448. else {
  6449. this[BUFFER][0] = c.subarray(n);
  6450. chunk = c.subarray(0, n);
  6451. this[BUFFERLENGTH] -= n;
  6452. }
  6453. }
  6454. this.emit('data', chunk);
  6455. if (!this[BUFFER].length && !this[EOF])
  6456. this.emit('drain');
  6457. return chunk;
  6458. }
  6459. end(chunk, encoding, cb) {
  6460. if (typeof chunk === 'function') {
  6461. cb = chunk;
  6462. chunk = undefined;
  6463. }
  6464. if (typeof encoding === 'function') {
  6465. cb = encoding;
  6466. encoding = 'utf8';
  6467. }
  6468. if (chunk !== undefined)
  6469. this.write(chunk, encoding);
  6470. if (cb)
  6471. this.once('end', cb);
  6472. this[EOF] = true;
  6473. this.writable = false;
  6474. // if we haven't written anything, then go ahead and emit,
  6475. // even if we're not reading.
  6476. // we'll re-emit if a new 'end' listener is added anyway.
  6477. // This makes MP more suitable to write-only use cases.
  6478. if (this[FLOWING] || !this[PAUSED])
  6479. this[MAYBE_EMIT_END]();
  6480. return this;
  6481. }
  6482. // don't let the internal resume be overwritten
  6483. [RESUME]() {
  6484. if (this[DESTROYED])
  6485. return;
  6486. if (!this[DATALISTENERS] && !this[PIPES].length) {
  6487. this[DISCARDED] = true;
  6488. }
  6489. this[PAUSED] = false;
  6490. this[FLOWING] = true;
  6491. this.emit('resume');
  6492. if (this[BUFFER].length)
  6493. this[FLUSH]();
  6494. else if (this[EOF])
  6495. this[MAYBE_EMIT_END]();
  6496. else
  6497. this.emit('drain');
  6498. }
  6499. /**
  6500. * Resume the stream if it is currently in a paused state
  6501. *
  6502. * If called when there are no pipe destinations or `data` event listeners,
  6503. * this will place the stream in a "discarded" state, where all data will
  6504. * be thrown away. The discarded state is removed if a pipe destination or
  6505. * data handler is added, if pause() is called, or if any synchronous or
  6506. * asynchronous iteration is started.
  6507. */
  6508. resume() {
  6509. return this[RESUME]();
  6510. }
  6511. /**
  6512. * Pause the stream
  6513. */
  6514. pause() {
  6515. this[FLOWING] = false;
  6516. this[PAUSED] = true;
  6517. this[DISCARDED] = false;
  6518. }
  6519. /**
  6520. * true if the stream has been forcibly destroyed
  6521. */
  6522. get destroyed() {
  6523. return this[DESTROYED];
  6524. }
  6525. /**
  6526. * true if the stream is currently in a flowing state, meaning that
  6527. * any writes will be immediately emitted.
  6528. */
  6529. get flowing() {
  6530. return this[FLOWING];
  6531. }
  6532. /**
  6533. * true if the stream is currently in a paused state
  6534. */
  6535. get paused() {
  6536. return this[PAUSED];
  6537. }
  6538. [BUFFERPUSH](chunk) {
  6539. if (this[OBJECTMODE])
  6540. this[BUFFERLENGTH] += 1;
  6541. else
  6542. this[BUFFERLENGTH] += chunk.length;
  6543. this[BUFFER].push(chunk);
  6544. }
  6545. [BUFFERSHIFT]() {
  6546. if (this[OBJECTMODE])
  6547. this[BUFFERLENGTH] -= 1;
  6548. else
  6549. this[BUFFERLENGTH] -= this[BUFFER][0].length;
  6550. return this[BUFFER].shift();
  6551. }
  6552. [FLUSH](noDrain = false) {
  6553. do { } while (this[FLUSHCHUNK](this[BUFFERSHIFT]()) &&
  6554. this[BUFFER].length);
  6555. if (!noDrain && !this[BUFFER].length && !this[EOF])
  6556. this.emit('drain');
  6557. }
  6558. [FLUSHCHUNK](chunk) {
  6559. this.emit('data', chunk);
  6560. return this[FLOWING];
  6561. }
  6562. /**
  6563. * Pipe all data emitted by this stream into the destination provided.
  6564. *
  6565. * Triggers the flow of data.
  6566. */
  6567. pipe(dest, opts) {
  6568. if (this[DESTROYED])
  6569. return dest;
  6570. this[DISCARDED] = false;
  6571. const ended = this[EMITTED_END];
  6572. opts = opts || {};
  6573. if (dest === proc.stdout || dest === proc.stderr)
  6574. opts.end = false;
  6575. else
  6576. opts.end = opts.end !== false;
  6577. opts.proxyErrors = !!opts.proxyErrors;
  6578. // piping an ended stream ends immediately
  6579. if (ended) {
  6580. if (opts.end)
  6581. dest.end();
  6582. }
  6583. else {
  6584. // "as" here just ignores the WType, which pipes don't care about,
  6585. // since they're only consuming from us, and writing to the dest
  6586. this[PIPES].push(!opts.proxyErrors
  6587. ? new Pipe(this, dest, opts)
  6588. : new PipeProxyErrors(this, dest, opts));
  6589. if (this[ASYNC])
  6590. defer$3(() => this[RESUME]());
  6591. else
  6592. this[RESUME]();
  6593. }
  6594. return dest;
  6595. }
  6596. /**
  6597. * Fully unhook a piped destination stream.
  6598. *
  6599. * If the destination stream was the only consumer of this stream (ie,
  6600. * there are no other piped destinations or `'data'` event listeners)
  6601. * then the flow of data will stop until there is another consumer or
  6602. * {@link Minipass#resume} is explicitly called.
  6603. */
  6604. unpipe(dest) {
  6605. const p = this[PIPES].find(p => p.dest === dest);
  6606. if (p) {
  6607. if (this[PIPES].length === 1) {
  6608. if (this[FLOWING] && this[DATALISTENERS] === 0) {
  6609. this[FLOWING] = false;
  6610. }
  6611. this[PIPES] = [];
  6612. }
  6613. else
  6614. this[PIPES].splice(this[PIPES].indexOf(p), 1);
  6615. p.unpipe();
  6616. }
  6617. }
  6618. /**
  6619. * Alias for {@link Minipass#on}
  6620. */
  6621. addListener(ev, handler) {
  6622. return this.on(ev, handler);
  6623. }
  6624. /**
  6625. * Mostly identical to `EventEmitter.on`, with the following
  6626. * behavior differences to prevent data loss and unnecessary hangs:
  6627. *
  6628. * - Adding a 'data' event handler will trigger the flow of data
  6629. *
  6630. * - Adding a 'readable' event handler when there is data waiting to be read
  6631. * will cause 'readable' to be emitted immediately.
  6632. *
  6633. * - Adding an 'endish' event handler ('end', 'finish', etc.) which has
  6634. * already passed will cause the event to be emitted immediately and all
  6635. * handlers removed.
  6636. *
  6637. * - Adding an 'error' event handler after an error has been emitted will
  6638. * cause the event to be re-emitted immediately with the error previously
  6639. * raised.
  6640. */
  6641. on(ev, handler) {
  6642. const ret = super.on(ev, handler);
  6643. if (ev === 'data') {
  6644. this[DISCARDED] = false;
  6645. this[DATALISTENERS]++;
  6646. if (!this[PIPES].length && !this[FLOWING]) {
  6647. this[RESUME]();
  6648. }
  6649. }
  6650. else if (ev === 'readable' && this[BUFFERLENGTH] !== 0) {
  6651. super.emit('readable');
  6652. }
  6653. else if (isEndish(ev) && this[EMITTED_END]) {
  6654. super.emit(ev);
  6655. this.removeAllListeners(ev);
  6656. }
  6657. else if (ev === 'error' && this[EMITTED_ERROR]) {
  6658. const h = handler;
  6659. if (this[ASYNC])
  6660. defer$3(() => h.call(this, this[EMITTED_ERROR]));
  6661. else
  6662. h.call(this, this[EMITTED_ERROR]);
  6663. }
  6664. return ret;
  6665. }
  6666. /**
  6667. * Alias for {@link Minipass#off}
  6668. */
  6669. removeListener(ev, handler) {
  6670. return this.off(ev, handler);
  6671. }
  6672. /**
  6673. * Mostly identical to `EventEmitter.off`
  6674. *
  6675. * If a 'data' event handler is removed, and it was the last consumer
  6676. * (ie, there are no pipe destinations or other 'data' event listeners),
  6677. * then the flow of data will stop until there is another consumer or
  6678. * {@link Minipass#resume} is explicitly called.
  6679. */
  6680. off(ev, handler) {
  6681. const ret = super.off(ev, handler);
  6682. // if we previously had listeners, and now we don't, and we don't
  6683. // have any pipes, then stop the flow, unless it's been explicitly
  6684. // put in a discarded flowing state via stream.resume().
  6685. if (ev === 'data') {
  6686. this[DATALISTENERS] = this.listeners('data').length;
  6687. if (this[DATALISTENERS] === 0 &&
  6688. !this[DISCARDED] &&
  6689. !this[PIPES].length) {
  6690. this[FLOWING] = false;
  6691. }
  6692. }
  6693. return ret;
  6694. }
  6695. /**
  6696. * Mostly identical to `EventEmitter.removeAllListeners`
  6697. *
  6698. * If all 'data' event handlers are removed, and they were the last consumer
  6699. * (ie, there are no pipe destinations), then the flow of data will stop
  6700. * until there is another consumer or {@link Minipass#resume} is explicitly
  6701. * called.
  6702. */
  6703. removeAllListeners(ev) {
  6704. const ret = super.removeAllListeners(ev);
  6705. if (ev === 'data' || ev === undefined) {
  6706. this[DATALISTENERS] = 0;
  6707. if (!this[DISCARDED] && !this[PIPES].length) {
  6708. this[FLOWING] = false;
  6709. }
  6710. }
  6711. return ret;
  6712. }
  6713. /**
  6714. * true if the 'end' event has been emitted
  6715. */
  6716. get emittedEnd() {
  6717. return this[EMITTED_END];
  6718. }
  6719. [MAYBE_EMIT_END]() {
  6720. if (!this[EMITTING_END] &&
  6721. !this[EMITTED_END] &&
  6722. !this[DESTROYED] &&
  6723. this[BUFFER].length === 0 &&
  6724. this[EOF]) {
  6725. this[EMITTING_END] = true;
  6726. this.emit('end');
  6727. this.emit('prefinish');
  6728. this.emit('finish');
  6729. if (this[CLOSED$1])
  6730. this.emit('close');
  6731. this[EMITTING_END] = false;
  6732. }
  6733. }
  6734. /**
  6735. * Mostly identical to `EventEmitter.emit`, with the following
  6736. * behavior differences to prevent data loss and unnecessary hangs:
  6737. *
  6738. * If the stream has been destroyed, and the event is something other
  6739. * than 'close' or 'error', then `false` is returned and no handlers
  6740. * are called.
  6741. *
  6742. * If the event is 'end', and has already been emitted, then the event
  6743. * is ignored. If the stream is in a paused or non-flowing state, then
  6744. * the event will be deferred until data flow resumes. If the stream is
  6745. * async, then handlers will be called on the next tick rather than
  6746. * immediately.
  6747. *
  6748. * If the event is 'close', and 'end' has not yet been emitted, then
  6749. * the event will be deferred until after 'end' is emitted.
  6750. *
  6751. * If the event is 'error', and an AbortSignal was provided for the stream,
  6752. * and there are no listeners, then the event is ignored, matching the
  6753. * behavior of node core streams in the presense of an AbortSignal.
  6754. *
  6755. * If the event is 'finish' or 'prefinish', then all listeners will be
  6756. * removed after emitting the event, to prevent double-firing.
  6757. */
  6758. emit(ev, ...args) {
  6759. const data = args[0];
  6760. // error and close are only events allowed after calling destroy()
  6761. if (ev !== 'error' &&
  6762. ev !== 'close' &&
  6763. ev !== DESTROYED &&
  6764. this[DESTROYED]) {
  6765. return false;
  6766. }
  6767. else if (ev === 'data') {
  6768. return !this[OBJECTMODE] && !data
  6769. ? false
  6770. : this[ASYNC]
  6771. ? (defer$3(() => this[EMITDATA](data)), true)
  6772. : this[EMITDATA](data);
  6773. }
  6774. else if (ev === 'end') {
  6775. return this[EMITEND]();
  6776. }
  6777. else if (ev === 'close') {
  6778. this[CLOSED$1] = true;
  6779. // don't emit close before 'end' and 'finish'
  6780. if (!this[EMITTED_END] && !this[DESTROYED])
  6781. return false;
  6782. const ret = super.emit('close');
  6783. this.removeAllListeners('close');
  6784. return ret;
  6785. }
  6786. else if (ev === 'error') {
  6787. this[EMITTED_ERROR] = data;
  6788. super.emit(ERROR, data);
  6789. const ret = !this[SIGNAL] || this.listeners('error').length
  6790. ? super.emit('error', data)
  6791. : false;
  6792. this[MAYBE_EMIT_END]();
  6793. return ret;
  6794. }
  6795. else if (ev === 'resume') {
  6796. const ret = super.emit('resume');
  6797. this[MAYBE_EMIT_END]();
  6798. return ret;
  6799. }
  6800. else if (ev === 'finish' || ev === 'prefinish') {
  6801. const ret = super.emit(ev);
  6802. this.removeAllListeners(ev);
  6803. return ret;
  6804. }
  6805. // Some other unknown event
  6806. const ret = super.emit(ev, ...args);
  6807. this[MAYBE_EMIT_END]();
  6808. return ret;
  6809. }
  6810. [EMITDATA](data) {
  6811. for (const p of this[PIPES]) {
  6812. if (p.dest.write(data) === false)
  6813. this.pause();
  6814. }
  6815. const ret = this[DISCARDED] ? false : super.emit('data', data);
  6816. this[MAYBE_EMIT_END]();
  6817. return ret;
  6818. }
  6819. [EMITEND]() {
  6820. if (this[EMITTED_END])
  6821. return false;
  6822. this[EMITTED_END] = true;
  6823. this.readable = false;
  6824. return this[ASYNC]
  6825. ? (defer$3(() => this[EMITEND2]()), true)
  6826. : this[EMITEND2]();
  6827. }
  6828. [EMITEND2]() {
  6829. if (this[DECODER]) {
  6830. const data = this[DECODER].end();
  6831. if (data) {
  6832. for (const p of this[PIPES]) {
  6833. p.dest.write(data);
  6834. }
  6835. if (!this[DISCARDED])
  6836. super.emit('data', data);
  6837. }
  6838. }
  6839. for (const p of this[PIPES]) {
  6840. p.end();
  6841. }
  6842. const ret = super.emit('end');
  6843. this.removeAllListeners('end');
  6844. return ret;
  6845. }
  6846. /**
  6847. * Return a Promise that resolves to an array of all emitted data once
  6848. * the stream ends.
  6849. */
  6850. async collect() {
  6851. const buf = Object.assign([], {
  6852. dataLength: 0,
  6853. });
  6854. if (!this[OBJECTMODE])
  6855. buf.dataLength = 0;
  6856. // set the promise first, in case an error is raised
  6857. // by triggering the flow here.
  6858. const p = this.promise();
  6859. this.on('data', c => {
  6860. buf.push(c);
  6861. if (!this[OBJECTMODE])
  6862. buf.dataLength += c.length;
  6863. });
  6864. await p;
  6865. return buf;
  6866. }
  6867. /**
  6868. * Return a Promise that resolves to the concatenation of all emitted data
  6869. * once the stream ends.
  6870. *
  6871. * Not allowed on objectMode streams.
  6872. */
  6873. async concat() {
  6874. if (this[OBJECTMODE]) {
  6875. throw new Error('cannot concat in objectMode');
  6876. }
  6877. const buf = await this.collect();
  6878. return (this[ENCODING$1]
  6879. ? buf.join('')
  6880. : Buffer.concat(buf, buf.dataLength));
  6881. }
  6882. /**
  6883. * Return a void Promise that resolves once the stream ends.
  6884. */
  6885. async promise() {
  6886. return new Promise((resolve, reject) => {
  6887. this.on(DESTROYED, () => reject(new Error('stream destroyed')));
  6888. this.on('error', er => reject(er));
  6889. this.on('end', () => resolve());
  6890. });
  6891. }
  6892. /**
  6893. * Asynchronous `for await of` iteration.
  6894. *
  6895. * This will continue emitting all chunks until the stream terminates.
  6896. */
  6897. [Symbol.asyncIterator]() {
  6898. // set this up front, in case the consumer doesn't call next()
  6899. // right away.
  6900. this[DISCARDED] = false;
  6901. let stopped = false;
  6902. const stop = async () => {
  6903. this.pause();
  6904. stopped = true;
  6905. return { value: undefined, done: true };
  6906. };
  6907. const next = () => {
  6908. if (stopped)
  6909. return stop();
  6910. const res = this.read();
  6911. if (res !== null)
  6912. return Promise.resolve({ done: false, value: res });
  6913. if (this[EOF])
  6914. return stop();
  6915. let resolve;
  6916. let reject;
  6917. const onerr = (er) => {
  6918. this.off('data', ondata);
  6919. this.off('end', onend);
  6920. this.off(DESTROYED, ondestroy);
  6921. stop();
  6922. reject(er);
  6923. };
  6924. const ondata = (value) => {
  6925. this.off('error', onerr);
  6926. this.off('end', onend);
  6927. this.off(DESTROYED, ondestroy);
  6928. this.pause();
  6929. resolve({ value, done: !!this[EOF] });
  6930. };
  6931. const onend = () => {
  6932. this.off('error', onerr);
  6933. this.off('data', ondata);
  6934. this.off(DESTROYED, ondestroy);
  6935. stop();
  6936. resolve({ done: true, value: undefined });
  6937. };
  6938. const ondestroy = () => onerr(new Error('stream destroyed'));
  6939. return new Promise((res, rej) => {
  6940. reject = rej;
  6941. resolve = res;
  6942. this.once(DESTROYED, ondestroy);
  6943. this.once('error', onerr);
  6944. this.once('end', onend);
  6945. this.once('data', ondata);
  6946. });
  6947. };
  6948. return {
  6949. next,
  6950. throw: stop,
  6951. return: stop,
  6952. [Symbol.asyncIterator]() {
  6953. return this;
  6954. },
  6955. };
  6956. }
  6957. /**
  6958. * Synchronous `for of` iteration.
  6959. *
  6960. * The iteration will terminate when the internal buffer runs out, even
  6961. * if the stream has not yet terminated.
  6962. */
  6963. [Symbol.iterator]() {
  6964. // set this up front, in case the consumer doesn't call next()
  6965. // right away.
  6966. this[DISCARDED] = false;
  6967. let stopped = false;
  6968. const stop = () => {
  6969. this.pause();
  6970. this.off(ERROR, stop);
  6971. this.off(DESTROYED, stop);
  6972. this.off('end', stop);
  6973. stopped = true;
  6974. return { done: true, value: undefined };
  6975. };
  6976. const next = () => {
  6977. if (stopped)
  6978. return stop();
  6979. const value = this.read();
  6980. return value === null ? stop() : { done: false, value };
  6981. };
  6982. this.once('end', stop);
  6983. this.once(ERROR, stop);
  6984. this.once(DESTROYED, stop);
  6985. return {
  6986. next,
  6987. throw: stop,
  6988. return: stop,
  6989. [Symbol.iterator]() {
  6990. return this;
  6991. },
  6992. };
  6993. }
  6994. /**
  6995. * Destroy a stream, preventing it from being used for any further purpose.
  6996. *
  6997. * If the stream has a `close()` method, then it will be called on
  6998. * destruction.
  6999. *
  7000. * After destruction, any attempt to write data, read data, or emit most
  7001. * events will be ignored.
  7002. *
  7003. * If an error argument is provided, then it will be emitted in an
  7004. * 'error' event.
  7005. */
  7006. destroy(er) {
  7007. if (this[DESTROYED]) {
  7008. if (er)
  7009. this.emit('error', er);
  7010. else
  7011. this.emit(DESTROYED);
  7012. return this;
  7013. }
  7014. this[DESTROYED] = true;
  7015. this[DISCARDED] = true;
  7016. // throw away all buffered data, it's never coming out
  7017. this[BUFFER].length = 0;
  7018. this[BUFFERLENGTH] = 0;
  7019. const wc = this;
  7020. if (typeof wc.close === 'function' && !this[CLOSED$1])
  7021. wc.close();
  7022. if (er)
  7023. this.emit('error', er);
  7024. // if no error to emit, still reject pending promises
  7025. else
  7026. this.emit(DESTROYED);
  7027. return this;
  7028. }
  7029. /**
  7030. * Alias for {@link isStream}
  7031. *
  7032. * Former export location, maintained for backwards compatibility.
  7033. *
  7034. * @deprecated
  7035. */
  7036. static get isStream() {
  7037. return isStream;
  7038. }
  7039. }
  7040. const realpathSync = realpathSync$1.native;
  7041. const defaultFS = {
  7042. lstatSync,
  7043. readdir: readdir$4,
  7044. readdirSync,
  7045. readlinkSync,
  7046. realpathSync,
  7047. promises: {
  7048. lstat: lstat$3,
  7049. readdir: readdir$5,
  7050. readlink,
  7051. realpath: realpath$2,
  7052. },
  7053. };
  7054. // if they just gave us require('fs') then use our default
  7055. const fsFromOption = (fsOption) => !fsOption || fsOption === defaultFS || fsOption === fs$j ?
  7056. defaultFS
  7057. : {
  7058. ...defaultFS,
  7059. ...fsOption,
  7060. promises: {
  7061. ...defaultFS.promises,
  7062. ...(fsOption.promises || {}),
  7063. },
  7064. };
  7065. // turn something like //?/c:/ into c:\
  7066. const uncDriveRegexp = /^\\\\\?\\([a-z]:)\\?$/i;
  7067. const uncToDrive = (rootPath) => rootPath.replace(/\//g, '\\').replace(uncDriveRegexp, '$1\\');
  7068. // windows paths are separated by either / or \
  7069. const eitherSep = /[\\\/]/;
  7070. const UNKNOWN = 0; // may not even exist, for all we know
  7071. const IFIFO = 0b0001;
  7072. const IFCHR = 0b0010;
  7073. const IFDIR = 0b0100;
  7074. const IFBLK = 0b0110;
  7075. const IFREG = 0b1000;
  7076. const IFLNK = 0b1010;
  7077. const IFSOCK = 0b1100;
  7078. const IFMT = 0b1111;
  7079. // mask to unset low 4 bits
  7080. const IFMT_UNKNOWN = ~IFMT;
  7081. // set after successfully calling readdir() and getting entries.
  7082. const READDIR_CALLED = 0b0000_0001_0000;
  7083. // set after a successful lstat()
  7084. const LSTAT_CALLED = 0b0000_0010_0000;
  7085. // set if an entry (or one of its parents) is definitely not a dir
  7086. const ENOTDIR = 0b0000_0100_0000;
  7087. // set if an entry (or one of its parents) does not exist
  7088. // (can also be set on lstat errors like EACCES or ENAMETOOLONG)
  7089. const ENOENT = 0b0000_1000_0000;
  7090. // cannot have child entries -- also verify &IFMT is either IFDIR or IFLNK
  7091. // set if we fail to readlink
  7092. const ENOREADLINK = 0b0001_0000_0000;
  7093. // set if we know realpath() will fail
  7094. const ENOREALPATH = 0b0010_0000_0000;
  7095. const ENOCHILD = ENOTDIR | ENOENT | ENOREALPATH;
  7096. const TYPEMASK = 0b0011_1111_1111;
  7097. const entToType = (s) => s.isFile() ? IFREG
  7098. : s.isDirectory() ? IFDIR
  7099. : s.isSymbolicLink() ? IFLNK
  7100. : s.isCharacterDevice() ? IFCHR
  7101. : s.isBlockDevice() ? IFBLK
  7102. : s.isSocket() ? IFSOCK
  7103. : s.isFIFO() ? IFIFO
  7104. : UNKNOWN;
  7105. // normalize unicode path names
  7106. const normalizeCache = new Map();
  7107. const normalize = (s) => {
  7108. const c = normalizeCache.get(s);
  7109. if (c)
  7110. return c;
  7111. const n = s.normalize('NFKD');
  7112. normalizeCache.set(s, n);
  7113. return n;
  7114. };
  7115. const normalizeNocaseCache = new Map();
  7116. const normalizeNocase = (s) => {
  7117. const c = normalizeNocaseCache.get(s);
  7118. if (c)
  7119. return c;
  7120. const n = normalize(s.toLowerCase());
  7121. normalizeNocaseCache.set(s, n);
  7122. return n;
  7123. };
  7124. /**
  7125. * An LRUCache for storing resolved path strings or Path objects.
  7126. * @internal
  7127. */
  7128. class ResolveCache extends LRUCache {
  7129. constructor() {
  7130. super({ max: 256 });
  7131. }
  7132. }
  7133. // In order to prevent blowing out the js heap by allocating hundreds of
  7134. // thousands of Path entries when walking extremely large trees, the "children"
  7135. // in this tree are represented by storing an array of Path entries in an
  7136. // LRUCache, indexed by the parent. At any time, Path.children() may return an
  7137. // empty array, indicating that it doesn't know about any of its children, and
  7138. // thus has to rebuild that cache. This is fine, it just means that we don't
  7139. // benefit as much from having the cached entries, but huge directory walks
  7140. // don't blow out the stack, and smaller ones are still as fast as possible.
  7141. //
  7142. //It does impose some complexity when building up the readdir data, because we
  7143. //need to pass a reference to the children array that we started with.
  7144. /**
  7145. * an LRUCache for storing child entries.
  7146. * @internal
  7147. */
  7148. class ChildrenCache extends LRUCache {
  7149. constructor(maxSize = 16 * 1024) {
  7150. super({
  7151. maxSize,
  7152. // parent + children
  7153. sizeCalculation: a => a.length + 1,
  7154. });
  7155. }
  7156. }
  7157. const setAsCwd = Symbol('PathScurry setAsCwd');
  7158. /**
  7159. * Path objects are sort of like a super-powered
  7160. * {@link https://nodejs.org/docs/latest/api/fs.html#class-fsdirent fs.Dirent}
  7161. *
  7162. * Each one represents a single filesystem entry on disk, which may or may not
  7163. * exist. It includes methods for reading various types of information via
  7164. * lstat, readlink, and readdir, and caches all information to the greatest
  7165. * degree possible.
  7166. *
  7167. * Note that fs operations that would normally throw will instead return an
  7168. * "empty" value. This is in order to prevent excessive overhead from error
  7169. * stack traces.
  7170. */
  7171. class PathBase {
  7172. /**
  7173. * the basename of this path
  7174. *
  7175. * **Important**: *always* test the path name against any test string
  7176. * usingthe {@link isNamed} method, and not by directly comparing this
  7177. * string. Otherwise, unicode path strings that the system sees as identical
  7178. * will not be properly treated as the same path, leading to incorrect
  7179. * behavior and possible security issues.
  7180. */
  7181. name;
  7182. /**
  7183. * the Path entry corresponding to the path root.
  7184. *
  7185. * @internal
  7186. */
  7187. root;
  7188. /**
  7189. * All roots found within the current PathScurry family
  7190. *
  7191. * @internal
  7192. */
  7193. roots;
  7194. /**
  7195. * a reference to the parent path, or undefined in the case of root entries
  7196. *
  7197. * @internal
  7198. */
  7199. parent;
  7200. /**
  7201. * boolean indicating whether paths are compared case-insensitively
  7202. * @internal
  7203. */
  7204. nocase;
  7205. /**
  7206. * boolean indicating that this path is the current working directory
  7207. * of the PathScurry collection that contains it.
  7208. */
  7209. isCWD = false;
  7210. // potential default fs override
  7211. #fs;
  7212. // Stats fields
  7213. #dev;
  7214. get dev() {
  7215. return this.#dev;
  7216. }
  7217. #mode;
  7218. get mode() {
  7219. return this.#mode;
  7220. }
  7221. #nlink;
  7222. get nlink() {
  7223. return this.#nlink;
  7224. }
  7225. #uid;
  7226. get uid() {
  7227. return this.#uid;
  7228. }
  7229. #gid;
  7230. get gid() {
  7231. return this.#gid;
  7232. }
  7233. #rdev;
  7234. get rdev() {
  7235. return this.#rdev;
  7236. }
  7237. #blksize;
  7238. get blksize() {
  7239. return this.#blksize;
  7240. }
  7241. #ino;
  7242. get ino() {
  7243. return this.#ino;
  7244. }
  7245. #size;
  7246. get size() {
  7247. return this.#size;
  7248. }
  7249. #blocks;
  7250. get blocks() {
  7251. return this.#blocks;
  7252. }
  7253. #atimeMs;
  7254. get atimeMs() {
  7255. return this.#atimeMs;
  7256. }
  7257. #mtimeMs;
  7258. get mtimeMs() {
  7259. return this.#mtimeMs;
  7260. }
  7261. #ctimeMs;
  7262. get ctimeMs() {
  7263. return this.#ctimeMs;
  7264. }
  7265. #birthtimeMs;
  7266. get birthtimeMs() {
  7267. return this.#birthtimeMs;
  7268. }
  7269. #atime;
  7270. get atime() {
  7271. return this.#atime;
  7272. }
  7273. #mtime;
  7274. get mtime() {
  7275. return this.#mtime;
  7276. }
  7277. #ctime;
  7278. get ctime() {
  7279. return this.#ctime;
  7280. }
  7281. #birthtime;
  7282. get birthtime() {
  7283. return this.#birthtime;
  7284. }
  7285. #matchName;
  7286. #depth;
  7287. #fullpath;
  7288. #fullpathPosix;
  7289. #relative;
  7290. #relativePosix;
  7291. #type;
  7292. #children;
  7293. #linkTarget;
  7294. #realpath;
  7295. /**
  7296. * This property is for compatibility with the Dirent class as of
  7297. * Node v20, where Dirent['parentPath'] refers to the path of the
  7298. * directory that was passed to readdir. For root entries, it's the path
  7299. * to the entry itself.
  7300. */
  7301. get parentPath() {
  7302. return (this.parent || this).fullpath();
  7303. }
  7304. /**
  7305. * Deprecated alias for Dirent['parentPath'] Somewhat counterintuitively,
  7306. * this property refers to the *parent* path, not the path object itself.
  7307. */
  7308. get path() {
  7309. return this.parentPath;
  7310. }
  7311. /**
  7312. * Do not create new Path objects directly. They should always be accessed
  7313. * via the PathScurry class or other methods on the Path class.
  7314. *
  7315. * @internal
  7316. */
  7317. constructor(name, type = UNKNOWN, root, roots, nocase, children, opts) {
  7318. this.name = name;
  7319. this.#matchName = nocase ? normalizeNocase(name) : normalize(name);
  7320. this.#type = type & TYPEMASK;
  7321. this.nocase = nocase;
  7322. this.roots = roots;
  7323. this.root = root || this;
  7324. this.#children = children;
  7325. this.#fullpath = opts.fullpath;
  7326. this.#relative = opts.relative;
  7327. this.#relativePosix = opts.relativePosix;
  7328. this.parent = opts.parent;
  7329. if (this.parent) {
  7330. this.#fs = this.parent.#fs;
  7331. }
  7332. else {
  7333. this.#fs = fsFromOption(opts.fs);
  7334. }
  7335. }
  7336. /**
  7337. * Returns the depth of the Path object from its root.
  7338. *
  7339. * For example, a path at `/foo/bar` would have a depth of 2.
  7340. */
  7341. depth() {
  7342. if (this.#depth !== undefined)
  7343. return this.#depth;
  7344. if (!this.parent)
  7345. return (this.#depth = 0);
  7346. return (this.#depth = this.parent.depth() + 1);
  7347. }
  7348. /**
  7349. * @internal
  7350. */
  7351. childrenCache() {
  7352. return this.#children;
  7353. }
  7354. /**
  7355. * Get the Path object referenced by the string path, resolved from this Path
  7356. */
  7357. resolve(path) {
  7358. if (!path) {
  7359. return this;
  7360. }
  7361. const rootPath = this.getRootString(path);
  7362. const dir = path.substring(rootPath.length);
  7363. const dirParts = dir.split(this.splitSep);
  7364. const result = rootPath ?
  7365. this.getRoot(rootPath).#resolveParts(dirParts)
  7366. : this.#resolveParts(dirParts);
  7367. return result;
  7368. }
  7369. #resolveParts(dirParts) {
  7370. let p = this;
  7371. for (const part of dirParts) {
  7372. p = p.child(part);
  7373. }
  7374. return p;
  7375. }
  7376. /**
  7377. * Returns the cached children Path objects, if still available. If they
  7378. * have fallen out of the cache, then returns an empty array, and resets the
  7379. * READDIR_CALLED bit, so that future calls to readdir() will require an fs
  7380. * lookup.
  7381. *
  7382. * @internal
  7383. */
  7384. children() {
  7385. const cached = this.#children.get(this);
  7386. if (cached) {
  7387. return cached;
  7388. }
  7389. const children = Object.assign([], { provisional: 0 });
  7390. this.#children.set(this, children);
  7391. this.#type &= ~READDIR_CALLED;
  7392. return children;
  7393. }
  7394. /**
  7395. * Resolves a path portion and returns or creates the child Path.
  7396. *
  7397. * Returns `this` if pathPart is `''` or `'.'`, or `parent` if pathPart is
  7398. * `'..'`.
  7399. *
  7400. * This should not be called directly. If `pathPart` contains any path
  7401. * separators, it will lead to unsafe undefined behavior.
  7402. *
  7403. * Use `Path.resolve()` instead.
  7404. *
  7405. * @internal
  7406. */
  7407. child(pathPart, opts) {
  7408. if (pathPart === '' || pathPart === '.') {
  7409. return this;
  7410. }
  7411. if (pathPart === '..') {
  7412. return this.parent || this;
  7413. }
  7414. // find the child
  7415. const children = this.children();
  7416. const name = this.nocase ? normalizeNocase(pathPart) : normalize(pathPart);
  7417. for (const p of children) {
  7418. if (p.#matchName === name) {
  7419. return p;
  7420. }
  7421. }
  7422. // didn't find it, create provisional child, since it might not
  7423. // actually exist. If we know the parent isn't a dir, then
  7424. // in fact it CAN'T exist.
  7425. const s = this.parent ? this.sep : '';
  7426. const fullpath = this.#fullpath ? this.#fullpath + s + pathPart : undefined;
  7427. const pchild = this.newChild(pathPart, UNKNOWN, {
  7428. ...opts,
  7429. parent: this,
  7430. fullpath,
  7431. });
  7432. if (!this.canReaddir()) {
  7433. pchild.#type |= ENOENT;
  7434. }
  7435. // don't have to update provisional, because if we have real children,
  7436. // then provisional is set to children.length, otherwise a lower number
  7437. children.push(pchild);
  7438. return pchild;
  7439. }
  7440. /**
  7441. * The relative path from the cwd. If it does not share an ancestor with
  7442. * the cwd, then this ends up being equivalent to the fullpath()
  7443. */
  7444. relative() {
  7445. if (this.isCWD)
  7446. return '';
  7447. if (this.#relative !== undefined) {
  7448. return this.#relative;
  7449. }
  7450. const name = this.name;
  7451. const p = this.parent;
  7452. if (!p) {
  7453. return (this.#relative = this.name);
  7454. }
  7455. const pv = p.relative();
  7456. return pv + (!pv || !p.parent ? '' : this.sep) + name;
  7457. }
  7458. /**
  7459. * The relative path from the cwd, using / as the path separator.
  7460. * If it does not share an ancestor with
  7461. * the cwd, then this ends up being equivalent to the fullpathPosix()
  7462. * On posix systems, this is identical to relative().
  7463. */
  7464. relativePosix() {
  7465. if (this.sep === '/')
  7466. return this.relative();
  7467. if (this.isCWD)
  7468. return '';
  7469. if (this.#relativePosix !== undefined)
  7470. return this.#relativePosix;
  7471. const name = this.name;
  7472. const p = this.parent;
  7473. if (!p) {
  7474. return (this.#relativePosix = this.fullpathPosix());
  7475. }
  7476. const pv = p.relativePosix();
  7477. return pv + (!pv || !p.parent ? '' : '/') + name;
  7478. }
  7479. /**
  7480. * The fully resolved path string for this Path entry
  7481. */
  7482. fullpath() {
  7483. if (this.#fullpath !== undefined) {
  7484. return this.#fullpath;
  7485. }
  7486. const name = this.name;
  7487. const p = this.parent;
  7488. if (!p) {
  7489. return (this.#fullpath = this.name);
  7490. }
  7491. const pv = p.fullpath();
  7492. const fp = pv + (!p.parent ? '' : this.sep) + name;
  7493. return (this.#fullpath = fp);
  7494. }
  7495. /**
  7496. * On platforms other than windows, this is identical to fullpath.
  7497. *
  7498. * On windows, this is overridden to return the forward-slash form of the
  7499. * full UNC path.
  7500. */
  7501. fullpathPosix() {
  7502. if (this.#fullpathPosix !== undefined)
  7503. return this.#fullpathPosix;
  7504. if (this.sep === '/')
  7505. return (this.#fullpathPosix = this.fullpath());
  7506. if (!this.parent) {
  7507. const p = this.fullpath().replace(/\\/g, '/');
  7508. if (/^[a-z]:\//i.test(p)) {
  7509. return (this.#fullpathPosix = `//?/${p}`);
  7510. }
  7511. else {
  7512. return (this.#fullpathPosix = p);
  7513. }
  7514. }
  7515. const p = this.parent;
  7516. const pfpp = p.fullpathPosix();
  7517. const fpp = pfpp + (!pfpp || !p.parent ? '' : '/') + this.name;
  7518. return (this.#fullpathPosix = fpp);
  7519. }
  7520. /**
  7521. * Is the Path of an unknown type?
  7522. *
  7523. * Note that we might know *something* about it if there has been a previous
  7524. * filesystem operation, for example that it does not exist, or is not a
  7525. * link, or whether it has child entries.
  7526. */
  7527. isUnknown() {
  7528. return (this.#type & IFMT) === UNKNOWN;
  7529. }
  7530. isType(type) {
  7531. return this[`is${type}`]();
  7532. }
  7533. getType() {
  7534. return (this.isUnknown() ? 'Unknown'
  7535. : this.isDirectory() ? 'Directory'
  7536. : this.isFile() ? 'File'
  7537. : this.isSymbolicLink() ? 'SymbolicLink'
  7538. : this.isFIFO() ? 'FIFO'
  7539. : this.isCharacterDevice() ? 'CharacterDevice'
  7540. : this.isBlockDevice() ? 'BlockDevice'
  7541. : /* c8 ignore start */ this.isSocket() ? 'Socket'
  7542. : 'Unknown');
  7543. /* c8 ignore stop */
  7544. }
  7545. /**
  7546. * Is the Path a regular file?
  7547. */
  7548. isFile() {
  7549. return (this.#type & IFMT) === IFREG;
  7550. }
  7551. /**
  7552. * Is the Path a directory?
  7553. */
  7554. isDirectory() {
  7555. return (this.#type & IFMT) === IFDIR;
  7556. }
  7557. /**
  7558. * Is the path a character device?
  7559. */
  7560. isCharacterDevice() {
  7561. return (this.#type & IFMT) === IFCHR;
  7562. }
  7563. /**
  7564. * Is the path a block device?
  7565. */
  7566. isBlockDevice() {
  7567. return (this.#type & IFMT) === IFBLK;
  7568. }
  7569. /**
  7570. * Is the path a FIFO pipe?
  7571. */
  7572. isFIFO() {
  7573. return (this.#type & IFMT) === IFIFO;
  7574. }
  7575. /**
  7576. * Is the path a socket?
  7577. */
  7578. isSocket() {
  7579. return (this.#type & IFMT) === IFSOCK;
  7580. }
  7581. /**
  7582. * Is the path a symbolic link?
  7583. */
  7584. isSymbolicLink() {
  7585. return (this.#type & IFLNK) === IFLNK;
  7586. }
  7587. /**
  7588. * Return the entry if it has been subject of a successful lstat, or
  7589. * undefined otherwise.
  7590. *
  7591. * Does not read the filesystem, so an undefined result *could* simply
  7592. * mean that we haven't called lstat on it.
  7593. */
  7594. lstatCached() {
  7595. return this.#type & LSTAT_CALLED ? this : undefined;
  7596. }
  7597. /**
  7598. * Return the cached link target if the entry has been the subject of a
  7599. * successful readlink, or undefined otherwise.
  7600. *
  7601. * Does not read the filesystem, so an undefined result *could* just mean we
  7602. * don't have any cached data. Only use it if you are very sure that a
  7603. * readlink() has been called at some point.
  7604. */
  7605. readlinkCached() {
  7606. return this.#linkTarget;
  7607. }
  7608. /**
  7609. * Returns the cached realpath target if the entry has been the subject
  7610. * of a successful realpath, or undefined otherwise.
  7611. *
  7612. * Does not read the filesystem, so an undefined result *could* just mean we
  7613. * don't have any cached data. Only use it if you are very sure that a
  7614. * realpath() has been called at some point.
  7615. */
  7616. realpathCached() {
  7617. return this.#realpath;
  7618. }
  7619. /**
  7620. * Returns the cached child Path entries array if the entry has been the
  7621. * subject of a successful readdir(), or [] otherwise.
  7622. *
  7623. * Does not read the filesystem, so an empty array *could* just mean we
  7624. * don't have any cached data. Only use it if you are very sure that a
  7625. * readdir() has been called recently enough to still be valid.
  7626. */
  7627. readdirCached() {
  7628. const children = this.children();
  7629. return children.slice(0, children.provisional);
  7630. }
  7631. /**
  7632. * Return true if it's worth trying to readlink. Ie, we don't (yet) have
  7633. * any indication that readlink will definitely fail.
  7634. *
  7635. * Returns false if the path is known to not be a symlink, if a previous
  7636. * readlink failed, or if the entry does not exist.
  7637. */
  7638. canReadlink() {
  7639. if (this.#linkTarget)
  7640. return true;
  7641. if (!this.parent)
  7642. return false;
  7643. // cases where it cannot possibly succeed
  7644. const ifmt = this.#type & IFMT;
  7645. return !((ifmt !== UNKNOWN && ifmt !== IFLNK) ||
  7646. this.#type & ENOREADLINK ||
  7647. this.#type & ENOENT);
  7648. }
  7649. /**
  7650. * Return true if readdir has previously been successfully called on this
  7651. * path, indicating that cachedReaddir() is likely valid.
  7652. */
  7653. calledReaddir() {
  7654. return !!(this.#type & READDIR_CALLED);
  7655. }
  7656. /**
  7657. * Returns true if the path is known to not exist. That is, a previous lstat
  7658. * or readdir failed to verify its existence when that would have been
  7659. * expected, or a parent entry was marked either enoent or enotdir.
  7660. */
  7661. isENOENT() {
  7662. return !!(this.#type & ENOENT);
  7663. }
  7664. /**
  7665. * Return true if the path is a match for the given path name. This handles
  7666. * case sensitivity and unicode normalization.
  7667. *
  7668. * Note: even on case-sensitive systems, it is **not** safe to test the
  7669. * equality of the `.name` property to determine whether a given pathname
  7670. * matches, due to unicode normalization mismatches.
  7671. *
  7672. * Always use this method instead of testing the `path.name` property
  7673. * directly.
  7674. */
  7675. isNamed(n) {
  7676. return !this.nocase ?
  7677. this.#matchName === normalize(n)
  7678. : this.#matchName === normalizeNocase(n);
  7679. }
  7680. /**
  7681. * Return the Path object corresponding to the target of a symbolic link.
  7682. *
  7683. * If the Path is not a symbolic link, or if the readlink call fails for any
  7684. * reason, `undefined` is returned.
  7685. *
  7686. * Result is cached, and thus may be outdated if the filesystem is mutated.
  7687. */
  7688. async readlink() {
  7689. const target = this.#linkTarget;
  7690. if (target) {
  7691. return target;
  7692. }
  7693. if (!this.canReadlink()) {
  7694. return undefined;
  7695. }
  7696. /* c8 ignore start */
  7697. // already covered by the canReadlink test, here for ts grumples
  7698. if (!this.parent) {
  7699. return undefined;
  7700. }
  7701. /* c8 ignore stop */
  7702. try {
  7703. const read = await this.#fs.promises.readlink(this.fullpath());
  7704. const linkTarget = (await this.parent.realpath())?.resolve(read);
  7705. if (linkTarget) {
  7706. return (this.#linkTarget = linkTarget);
  7707. }
  7708. }
  7709. catch (er) {
  7710. this.#readlinkFail(er.code);
  7711. return undefined;
  7712. }
  7713. }
  7714. /**
  7715. * Synchronous {@link PathBase.readlink}
  7716. */
  7717. readlinkSync() {
  7718. const target = this.#linkTarget;
  7719. if (target) {
  7720. return target;
  7721. }
  7722. if (!this.canReadlink()) {
  7723. return undefined;
  7724. }
  7725. /* c8 ignore start */
  7726. // already covered by the canReadlink test, here for ts grumples
  7727. if (!this.parent) {
  7728. return undefined;
  7729. }
  7730. /* c8 ignore stop */
  7731. try {
  7732. const read = this.#fs.readlinkSync(this.fullpath());
  7733. const linkTarget = this.parent.realpathSync()?.resolve(read);
  7734. if (linkTarget) {
  7735. return (this.#linkTarget = linkTarget);
  7736. }
  7737. }
  7738. catch (er) {
  7739. this.#readlinkFail(er.code);
  7740. return undefined;
  7741. }
  7742. }
  7743. #readdirSuccess(children) {
  7744. // succeeded, mark readdir called bit
  7745. this.#type |= READDIR_CALLED;
  7746. // mark all remaining provisional children as ENOENT
  7747. for (let p = children.provisional; p < children.length; p++) {
  7748. const c = children[p];
  7749. if (c)
  7750. c.#markENOENT();
  7751. }
  7752. }
  7753. #markENOENT() {
  7754. // mark as UNKNOWN and ENOENT
  7755. if (this.#type & ENOENT)
  7756. return;
  7757. this.#type = (this.#type | ENOENT) & IFMT_UNKNOWN;
  7758. this.#markChildrenENOENT();
  7759. }
  7760. #markChildrenENOENT() {
  7761. // all children are provisional and do not exist
  7762. const children = this.children();
  7763. children.provisional = 0;
  7764. for (const p of children) {
  7765. p.#markENOENT();
  7766. }
  7767. }
  7768. #markENOREALPATH() {
  7769. this.#type |= ENOREALPATH;
  7770. this.#markENOTDIR();
  7771. }
  7772. // save the information when we know the entry is not a dir
  7773. #markENOTDIR() {
  7774. // entry is not a directory, so any children can't exist.
  7775. // this *should* be impossible, since any children created
  7776. // after it's been marked ENOTDIR should be marked ENOENT,
  7777. // so it won't even get to this point.
  7778. /* c8 ignore start */
  7779. if (this.#type & ENOTDIR)
  7780. return;
  7781. /* c8 ignore stop */
  7782. let t = this.#type;
  7783. // this could happen if we stat a dir, then delete it,
  7784. // then try to read it or one of its children.
  7785. if ((t & IFMT) === IFDIR)
  7786. t &= IFMT_UNKNOWN;
  7787. this.#type = t | ENOTDIR;
  7788. this.#markChildrenENOENT();
  7789. }
  7790. #readdirFail(code = '') {
  7791. // markENOTDIR and markENOENT also set provisional=0
  7792. if (code === 'ENOTDIR' || code === 'EPERM') {
  7793. this.#markENOTDIR();
  7794. }
  7795. else if (code === 'ENOENT') {
  7796. this.#markENOENT();
  7797. }
  7798. else {
  7799. this.children().provisional = 0;
  7800. }
  7801. }
  7802. #lstatFail(code = '') {
  7803. // Windows just raises ENOENT in this case, disable for win CI
  7804. /* c8 ignore start */
  7805. if (code === 'ENOTDIR') {
  7806. // already know it has a parent by this point
  7807. const p = this.parent;
  7808. p.#markENOTDIR();
  7809. }
  7810. else if (code === 'ENOENT') {
  7811. /* c8 ignore stop */
  7812. this.#markENOENT();
  7813. }
  7814. }
  7815. #readlinkFail(code = '') {
  7816. let ter = this.#type;
  7817. ter |= ENOREADLINK;
  7818. if (code === 'ENOENT')
  7819. ter |= ENOENT;
  7820. // windows gets a weird error when you try to readlink a file
  7821. if (code === 'EINVAL' || code === 'UNKNOWN') {
  7822. // exists, but not a symlink, we don't know WHAT it is, so remove
  7823. // all IFMT bits.
  7824. ter &= IFMT_UNKNOWN;
  7825. }
  7826. this.#type = ter;
  7827. // windows just gets ENOENT in this case. We do cover the case,
  7828. // just disabled because it's impossible on Windows CI
  7829. /* c8 ignore start */
  7830. if (code === 'ENOTDIR' && this.parent) {
  7831. this.parent.#markENOTDIR();
  7832. }
  7833. /* c8 ignore stop */
  7834. }
  7835. #readdirAddChild(e, c) {
  7836. return (this.#readdirMaybePromoteChild(e, c) ||
  7837. this.#readdirAddNewChild(e, c));
  7838. }
  7839. #readdirAddNewChild(e, c) {
  7840. // alloc new entry at head, so it's never provisional
  7841. const type = entToType(e);
  7842. const child = this.newChild(e.name, type, { parent: this });
  7843. const ifmt = child.#type & IFMT;
  7844. if (ifmt !== IFDIR && ifmt !== IFLNK && ifmt !== UNKNOWN) {
  7845. child.#type |= ENOTDIR;
  7846. }
  7847. c.unshift(child);
  7848. c.provisional++;
  7849. return child;
  7850. }
  7851. #readdirMaybePromoteChild(e, c) {
  7852. for (let p = c.provisional; p < c.length; p++) {
  7853. const pchild = c[p];
  7854. const name = this.nocase ? normalizeNocase(e.name) : normalize(e.name);
  7855. if (name !== pchild.#matchName) {
  7856. continue;
  7857. }
  7858. return this.#readdirPromoteChild(e, pchild, p, c);
  7859. }
  7860. }
  7861. #readdirPromoteChild(e, p, index, c) {
  7862. const v = p.name;
  7863. // retain any other flags, but set ifmt from dirent
  7864. p.#type = (p.#type & IFMT_UNKNOWN) | entToType(e);
  7865. // case sensitivity fixing when we learn the true name.
  7866. if (v !== e.name)
  7867. p.name = e.name;
  7868. // just advance provisional index (potentially off the list),
  7869. // otherwise we have to splice/pop it out and re-insert at head
  7870. if (index !== c.provisional) {
  7871. if (index === c.length - 1)
  7872. c.pop();
  7873. else
  7874. c.splice(index, 1);
  7875. c.unshift(p);
  7876. }
  7877. c.provisional++;
  7878. return p;
  7879. }
  7880. /**
  7881. * Call lstat() on this Path, and update all known information that can be
  7882. * determined.
  7883. *
  7884. * Note that unlike `fs.lstat()`, the returned value does not contain some
  7885. * information, such as `mode`, `dev`, `nlink`, and `ino`. If that
  7886. * information is required, you will need to call `fs.lstat` yourself.
  7887. *
  7888. * If the Path refers to a nonexistent file, or if the lstat call fails for
  7889. * any reason, `undefined` is returned. Otherwise the updated Path object is
  7890. * returned.
  7891. *
  7892. * Results are cached, and thus may be out of date if the filesystem is
  7893. * mutated.
  7894. */
  7895. async lstat() {
  7896. if ((this.#type & ENOENT) === 0) {
  7897. try {
  7898. this.#applyStat(await this.#fs.promises.lstat(this.fullpath()));
  7899. return this;
  7900. }
  7901. catch (er) {
  7902. this.#lstatFail(er.code);
  7903. }
  7904. }
  7905. }
  7906. /**
  7907. * synchronous {@link PathBase.lstat}
  7908. */
  7909. lstatSync() {
  7910. if ((this.#type & ENOENT) === 0) {
  7911. try {
  7912. this.#applyStat(this.#fs.lstatSync(this.fullpath()));
  7913. return this;
  7914. }
  7915. catch (er) {
  7916. this.#lstatFail(er.code);
  7917. }
  7918. }
  7919. }
  7920. #applyStat(st) {
  7921. const { atime, atimeMs, birthtime, birthtimeMs, blksize, blocks, ctime, ctimeMs, dev, gid, ino, mode, mtime, mtimeMs, nlink, rdev, size, uid, } = st;
  7922. this.#atime = atime;
  7923. this.#atimeMs = atimeMs;
  7924. this.#birthtime = birthtime;
  7925. this.#birthtimeMs = birthtimeMs;
  7926. this.#blksize = blksize;
  7927. this.#blocks = blocks;
  7928. this.#ctime = ctime;
  7929. this.#ctimeMs = ctimeMs;
  7930. this.#dev = dev;
  7931. this.#gid = gid;
  7932. this.#ino = ino;
  7933. this.#mode = mode;
  7934. this.#mtime = mtime;
  7935. this.#mtimeMs = mtimeMs;
  7936. this.#nlink = nlink;
  7937. this.#rdev = rdev;
  7938. this.#size = size;
  7939. this.#uid = uid;
  7940. const ifmt = entToType(st);
  7941. // retain any other flags, but set the ifmt
  7942. this.#type = (this.#type & IFMT_UNKNOWN) | ifmt | LSTAT_CALLED;
  7943. if (ifmt !== UNKNOWN && ifmt !== IFDIR && ifmt !== IFLNK) {
  7944. this.#type |= ENOTDIR;
  7945. }
  7946. }
  7947. #onReaddirCB = [];
  7948. #readdirCBInFlight = false;
  7949. #callOnReaddirCB(children) {
  7950. this.#readdirCBInFlight = false;
  7951. const cbs = this.#onReaddirCB.slice();
  7952. this.#onReaddirCB.length = 0;
  7953. cbs.forEach(cb => cb(null, children));
  7954. }
  7955. /**
  7956. * Standard node-style callback interface to get list of directory entries.
  7957. *
  7958. * If the Path cannot or does not contain any children, then an empty array
  7959. * is returned.
  7960. *
  7961. * Results are cached, and thus may be out of date if the filesystem is
  7962. * mutated.
  7963. *
  7964. * @param cb The callback called with (er, entries). Note that the `er`
  7965. * param is somewhat extraneous, as all readdir() errors are handled and
  7966. * simply result in an empty set of entries being returned.
  7967. * @param allowZalgo Boolean indicating that immediately known results should
  7968. * *not* be deferred with `queueMicrotask`. Defaults to `false`. Release
  7969. * zalgo at your peril, the dark pony lord is devious and unforgiving.
  7970. */
  7971. readdirCB(cb, allowZalgo = false) {
  7972. if (!this.canReaddir()) {
  7973. if (allowZalgo)
  7974. cb(null, []);
  7975. else
  7976. queueMicrotask(() => cb(null, []));
  7977. return;
  7978. }
  7979. const children = this.children();
  7980. if (this.calledReaddir()) {
  7981. const c = children.slice(0, children.provisional);
  7982. if (allowZalgo)
  7983. cb(null, c);
  7984. else
  7985. queueMicrotask(() => cb(null, c));
  7986. return;
  7987. }
  7988. // don't have to worry about zalgo at this point.
  7989. this.#onReaddirCB.push(cb);
  7990. if (this.#readdirCBInFlight) {
  7991. return;
  7992. }
  7993. this.#readdirCBInFlight = true;
  7994. // else read the directory, fill up children
  7995. // de-provisionalize any provisional children.
  7996. const fullpath = this.fullpath();
  7997. this.#fs.readdir(fullpath, { withFileTypes: true }, (er, entries) => {
  7998. if (er) {
  7999. this.#readdirFail(er.code);
  8000. children.provisional = 0;
  8001. }
  8002. else {
  8003. // if we didn't get an error, we always get entries.
  8004. //@ts-ignore
  8005. for (const e of entries) {
  8006. this.#readdirAddChild(e, children);
  8007. }
  8008. this.#readdirSuccess(children);
  8009. }
  8010. this.#callOnReaddirCB(children.slice(0, children.provisional));
  8011. return;
  8012. });
  8013. }
  8014. #asyncReaddirInFlight;
  8015. /**
  8016. * Return an array of known child entries.
  8017. *
  8018. * If the Path cannot or does not contain any children, then an empty array
  8019. * is returned.
  8020. *
  8021. * Results are cached, and thus may be out of date if the filesystem is
  8022. * mutated.
  8023. */
  8024. async readdir() {
  8025. if (!this.canReaddir()) {
  8026. return [];
  8027. }
  8028. const children = this.children();
  8029. if (this.calledReaddir()) {
  8030. return children.slice(0, children.provisional);
  8031. }
  8032. // else read the directory, fill up children
  8033. // de-provisionalize any provisional children.
  8034. const fullpath = this.fullpath();
  8035. if (this.#asyncReaddirInFlight) {
  8036. await this.#asyncReaddirInFlight;
  8037. }
  8038. else {
  8039. /* c8 ignore start */
  8040. let resolve = () => { };
  8041. /* c8 ignore stop */
  8042. this.#asyncReaddirInFlight = new Promise(res => (resolve = res));
  8043. try {
  8044. for (const e of await this.#fs.promises.readdir(fullpath, {
  8045. withFileTypes: true,
  8046. })) {
  8047. this.#readdirAddChild(e, children);
  8048. }
  8049. this.#readdirSuccess(children);
  8050. }
  8051. catch (er) {
  8052. this.#readdirFail(er.code);
  8053. children.provisional = 0;
  8054. }
  8055. this.#asyncReaddirInFlight = undefined;
  8056. resolve();
  8057. }
  8058. return children.slice(0, children.provisional);
  8059. }
  8060. /**
  8061. * synchronous {@link PathBase.readdir}
  8062. */
  8063. readdirSync() {
  8064. if (!this.canReaddir()) {
  8065. return [];
  8066. }
  8067. const children = this.children();
  8068. if (this.calledReaddir()) {
  8069. return children.slice(0, children.provisional);
  8070. }
  8071. // else read the directory, fill up children
  8072. // de-provisionalize any provisional children.
  8073. const fullpath = this.fullpath();
  8074. try {
  8075. for (const e of this.#fs.readdirSync(fullpath, {
  8076. withFileTypes: true,
  8077. })) {
  8078. this.#readdirAddChild(e, children);
  8079. }
  8080. this.#readdirSuccess(children);
  8081. }
  8082. catch (er) {
  8083. this.#readdirFail(er.code);
  8084. children.provisional = 0;
  8085. }
  8086. return children.slice(0, children.provisional);
  8087. }
  8088. canReaddir() {
  8089. if (this.#type & ENOCHILD)
  8090. return false;
  8091. const ifmt = IFMT & this.#type;
  8092. // we always set ENOTDIR when setting IFMT, so should be impossible
  8093. /* c8 ignore start */
  8094. if (!(ifmt === UNKNOWN || ifmt === IFDIR || ifmt === IFLNK)) {
  8095. return false;
  8096. }
  8097. /* c8 ignore stop */
  8098. return true;
  8099. }
  8100. shouldWalk(dirs, walkFilter) {
  8101. return ((this.#type & IFDIR) === IFDIR &&
  8102. !(this.#type & ENOCHILD) &&
  8103. !dirs.has(this) &&
  8104. (!walkFilter || walkFilter(this)));
  8105. }
  8106. /**
  8107. * Return the Path object corresponding to path as resolved
  8108. * by realpath(3).
  8109. *
  8110. * If the realpath call fails for any reason, `undefined` is returned.
  8111. *
  8112. * Result is cached, and thus may be outdated if the filesystem is mutated.
  8113. * On success, returns a Path object.
  8114. */
  8115. async realpath() {
  8116. if (this.#realpath)
  8117. return this.#realpath;
  8118. if ((ENOREALPATH | ENOREADLINK | ENOENT) & this.#type)
  8119. return undefined;
  8120. try {
  8121. const rp = await this.#fs.promises.realpath(this.fullpath());
  8122. return (this.#realpath = this.resolve(rp));
  8123. }
  8124. catch (_) {
  8125. this.#markENOREALPATH();
  8126. }
  8127. }
  8128. /**
  8129. * Synchronous {@link realpath}
  8130. */
  8131. realpathSync() {
  8132. if (this.#realpath)
  8133. return this.#realpath;
  8134. if ((ENOREALPATH | ENOREADLINK | ENOENT) & this.#type)
  8135. return undefined;
  8136. try {
  8137. const rp = this.#fs.realpathSync(this.fullpath());
  8138. return (this.#realpath = this.resolve(rp));
  8139. }
  8140. catch (_) {
  8141. this.#markENOREALPATH();
  8142. }
  8143. }
  8144. /**
  8145. * Internal method to mark this Path object as the scurry cwd,
  8146. * called by {@link PathScurry#chdir}
  8147. *
  8148. * @internal
  8149. */
  8150. [setAsCwd](oldCwd) {
  8151. if (oldCwd === this)
  8152. return;
  8153. oldCwd.isCWD = false;
  8154. this.isCWD = true;
  8155. const changed = new Set([]);
  8156. let rp = [];
  8157. let p = this;
  8158. while (p && p.parent) {
  8159. changed.add(p);
  8160. p.#relative = rp.join(this.sep);
  8161. p.#relativePosix = rp.join('/');
  8162. p = p.parent;
  8163. rp.push('..');
  8164. }
  8165. // now un-memoize parents of old cwd
  8166. p = oldCwd;
  8167. while (p && p.parent && !changed.has(p)) {
  8168. p.#relative = undefined;
  8169. p.#relativePosix = undefined;
  8170. p = p.parent;
  8171. }
  8172. }
  8173. }
  8174. /**
  8175. * Path class used on win32 systems
  8176. *
  8177. * Uses `'\\'` as the path separator for returned paths, either `'\\'` or `'/'`
  8178. * as the path separator for parsing paths.
  8179. */
  8180. class PathWin32 extends PathBase {
  8181. /**
  8182. * Separator for generating path strings.
  8183. */
  8184. sep = '\\';
  8185. /**
  8186. * Separator for parsing path strings.
  8187. */
  8188. splitSep = eitherSep;
  8189. /**
  8190. * Do not create new Path objects directly. They should always be accessed
  8191. * via the PathScurry class or other methods on the Path class.
  8192. *
  8193. * @internal
  8194. */
  8195. constructor(name, type = UNKNOWN, root, roots, nocase, children, opts) {
  8196. super(name, type, root, roots, nocase, children, opts);
  8197. }
  8198. /**
  8199. * @internal
  8200. */
  8201. newChild(name, type = UNKNOWN, opts = {}) {
  8202. return new PathWin32(name, type, this.root, this.roots, this.nocase, this.childrenCache(), opts);
  8203. }
  8204. /**
  8205. * @internal
  8206. */
  8207. getRootString(path) {
  8208. return win32$1.parse(path).root;
  8209. }
  8210. /**
  8211. * @internal
  8212. */
  8213. getRoot(rootPath) {
  8214. rootPath = uncToDrive(rootPath.toUpperCase());
  8215. if (rootPath === this.root.name) {
  8216. return this.root;
  8217. }
  8218. // ok, not that one, check if it matches another we know about
  8219. for (const [compare, root] of Object.entries(this.roots)) {
  8220. if (this.sameRoot(rootPath, compare)) {
  8221. return (this.roots[rootPath] = root);
  8222. }
  8223. }
  8224. // otherwise, have to create a new one.
  8225. return (this.roots[rootPath] = new PathScurryWin32(rootPath, this).root);
  8226. }
  8227. /**
  8228. * @internal
  8229. */
  8230. sameRoot(rootPath, compare = this.root.name) {
  8231. // windows can (rarely) have case-sensitive filesystem, but
  8232. // UNC and drive letters are always case-insensitive, and canonically
  8233. // represented uppercase.
  8234. rootPath = rootPath
  8235. .toUpperCase()
  8236. .replace(/\//g, '\\')
  8237. .replace(uncDriveRegexp, '$1\\');
  8238. return rootPath === compare;
  8239. }
  8240. }
  8241. /**
  8242. * Path class used on all posix systems.
  8243. *
  8244. * Uses `'/'` as the path separator.
  8245. */
  8246. class PathPosix extends PathBase {
  8247. /**
  8248. * separator for parsing path strings
  8249. */
  8250. splitSep = '/';
  8251. /**
  8252. * separator for generating path strings
  8253. */
  8254. sep = '/';
  8255. /**
  8256. * Do not create new Path objects directly. They should always be accessed
  8257. * via the PathScurry class or other methods on the Path class.
  8258. *
  8259. * @internal
  8260. */
  8261. constructor(name, type = UNKNOWN, root, roots, nocase, children, opts) {
  8262. super(name, type, root, roots, nocase, children, opts);
  8263. }
  8264. /**
  8265. * @internal
  8266. */
  8267. getRootString(path) {
  8268. return path.startsWith('/') ? '/' : '';
  8269. }
  8270. /**
  8271. * @internal
  8272. */
  8273. getRoot(_rootPath) {
  8274. return this.root;
  8275. }
  8276. /**
  8277. * @internal
  8278. */
  8279. newChild(name, type = UNKNOWN, opts = {}) {
  8280. return new PathPosix(name, type, this.root, this.roots, this.nocase, this.childrenCache(), opts);
  8281. }
  8282. }
  8283. /**
  8284. * The base class for all PathScurry classes, providing the interface for path
  8285. * resolution and filesystem operations.
  8286. *
  8287. * Typically, you should *not* instantiate this class directly, but rather one
  8288. * of the platform-specific classes, or the exported {@link PathScurry} which
  8289. * defaults to the current platform.
  8290. */
  8291. class PathScurryBase {
  8292. /**
  8293. * The root Path entry for the current working directory of this Scurry
  8294. */
  8295. root;
  8296. /**
  8297. * The string path for the root of this Scurry's current working directory
  8298. */
  8299. rootPath;
  8300. /**
  8301. * A collection of all roots encountered, referenced by rootPath
  8302. */
  8303. roots;
  8304. /**
  8305. * The Path entry corresponding to this PathScurry's current working directory.
  8306. */
  8307. cwd;
  8308. #resolveCache;
  8309. #resolvePosixCache;
  8310. #children;
  8311. /**
  8312. * Perform path comparisons case-insensitively.
  8313. *
  8314. * Defaults true on Darwin and Windows systems, false elsewhere.
  8315. */
  8316. nocase;
  8317. #fs;
  8318. /**
  8319. * This class should not be instantiated directly.
  8320. *
  8321. * Use PathScurryWin32, PathScurryDarwin, PathScurryPosix, or PathScurry
  8322. *
  8323. * @internal
  8324. */
  8325. constructor(cwd = process.cwd(), pathImpl, sep, { nocase, childrenCacheSize = 16 * 1024, fs = defaultFS, } = {}) {
  8326. this.#fs = fsFromOption(fs);
  8327. if (cwd instanceof URL || cwd.startsWith('file://')) {
  8328. cwd = fileURLToPath(cwd);
  8329. }
  8330. // resolve and split root, and then add to the store.
  8331. // this is the only time we call path.resolve()
  8332. const cwdPath = pathImpl.resolve(cwd);
  8333. this.roots = Object.create(null);
  8334. this.rootPath = this.parseRootPath(cwdPath);
  8335. this.#resolveCache = new ResolveCache();
  8336. this.#resolvePosixCache = new ResolveCache();
  8337. this.#children = new ChildrenCache(childrenCacheSize);
  8338. const split = cwdPath.substring(this.rootPath.length).split(sep);
  8339. // resolve('/') leaves '', splits to [''], we don't want that.
  8340. if (split.length === 1 && !split[0]) {
  8341. split.pop();
  8342. }
  8343. /* c8 ignore start */
  8344. if (nocase === undefined) {
  8345. throw new TypeError('must provide nocase setting to PathScurryBase ctor');
  8346. }
  8347. /* c8 ignore stop */
  8348. this.nocase = nocase;
  8349. this.root = this.newRoot(this.#fs);
  8350. this.roots[this.rootPath] = this.root;
  8351. let prev = this.root;
  8352. let len = split.length - 1;
  8353. const joinSep = pathImpl.sep;
  8354. let abs = this.rootPath;
  8355. let sawFirst = false;
  8356. for (const part of split) {
  8357. const l = len--;
  8358. prev = prev.child(part, {
  8359. relative: new Array(l).fill('..').join(joinSep),
  8360. relativePosix: new Array(l).fill('..').join('/'),
  8361. fullpath: (abs += (sawFirst ? '' : joinSep) + part),
  8362. });
  8363. sawFirst = true;
  8364. }
  8365. this.cwd = prev;
  8366. }
  8367. /**
  8368. * Get the depth of a provided path, string, or the cwd
  8369. */
  8370. depth(path = this.cwd) {
  8371. if (typeof path === 'string') {
  8372. path = this.cwd.resolve(path);
  8373. }
  8374. return path.depth();
  8375. }
  8376. /**
  8377. * Return the cache of child entries. Exposed so subclasses can create
  8378. * child Path objects in a platform-specific way.
  8379. *
  8380. * @internal
  8381. */
  8382. childrenCache() {
  8383. return this.#children;
  8384. }
  8385. /**
  8386. * Resolve one or more path strings to a resolved string
  8387. *
  8388. * Same interface as require('path').resolve.
  8389. *
  8390. * Much faster than path.resolve() when called multiple times for the same
  8391. * path, because the resolved Path objects are cached. Much slower
  8392. * otherwise.
  8393. */
  8394. resolve(...paths) {
  8395. // first figure out the minimum number of paths we have to test
  8396. // we always start at cwd, but any absolutes will bump the start
  8397. let r = '';
  8398. for (let i = paths.length - 1; i >= 0; i--) {
  8399. const p = paths[i];
  8400. if (!p || p === '.')
  8401. continue;
  8402. r = r ? `${p}/${r}` : p;
  8403. if (this.isAbsolute(p)) {
  8404. break;
  8405. }
  8406. }
  8407. const cached = this.#resolveCache.get(r);
  8408. if (cached !== undefined) {
  8409. return cached;
  8410. }
  8411. const result = this.cwd.resolve(r).fullpath();
  8412. this.#resolveCache.set(r, result);
  8413. return result;
  8414. }
  8415. /**
  8416. * Resolve one or more path strings to a resolved string, returning
  8417. * the posix path. Identical to .resolve() on posix systems, but on
  8418. * windows will return a forward-slash separated UNC path.
  8419. *
  8420. * Same interface as require('path').resolve.
  8421. *
  8422. * Much faster than path.resolve() when called multiple times for the same
  8423. * path, because the resolved Path objects are cached. Much slower
  8424. * otherwise.
  8425. */
  8426. resolvePosix(...paths) {
  8427. // first figure out the minimum number of paths we have to test
  8428. // we always start at cwd, but any absolutes will bump the start
  8429. let r = '';
  8430. for (let i = paths.length - 1; i >= 0; i--) {
  8431. const p = paths[i];
  8432. if (!p || p === '.')
  8433. continue;
  8434. r = r ? `${p}/${r}` : p;
  8435. if (this.isAbsolute(p)) {
  8436. break;
  8437. }
  8438. }
  8439. const cached = this.#resolvePosixCache.get(r);
  8440. if (cached !== undefined) {
  8441. return cached;
  8442. }
  8443. const result = this.cwd.resolve(r).fullpathPosix();
  8444. this.#resolvePosixCache.set(r, result);
  8445. return result;
  8446. }
  8447. /**
  8448. * find the relative path from the cwd to the supplied path string or entry
  8449. */
  8450. relative(entry = this.cwd) {
  8451. if (typeof entry === 'string') {
  8452. entry = this.cwd.resolve(entry);
  8453. }
  8454. return entry.relative();
  8455. }
  8456. /**
  8457. * find the relative path from the cwd to the supplied path string or
  8458. * entry, using / as the path delimiter, even on Windows.
  8459. */
  8460. relativePosix(entry = this.cwd) {
  8461. if (typeof entry === 'string') {
  8462. entry = this.cwd.resolve(entry);
  8463. }
  8464. return entry.relativePosix();
  8465. }
  8466. /**
  8467. * Return the basename for the provided string or Path object
  8468. */
  8469. basename(entry = this.cwd) {
  8470. if (typeof entry === 'string') {
  8471. entry = this.cwd.resolve(entry);
  8472. }
  8473. return entry.name;
  8474. }
  8475. /**
  8476. * Return the dirname for the provided string or Path object
  8477. */
  8478. dirname(entry = this.cwd) {
  8479. if (typeof entry === 'string') {
  8480. entry = this.cwd.resolve(entry);
  8481. }
  8482. return (entry.parent || entry).fullpath();
  8483. }
  8484. async readdir(entry = this.cwd, opts = {
  8485. withFileTypes: true,
  8486. }) {
  8487. if (typeof entry === 'string') {
  8488. entry = this.cwd.resolve(entry);
  8489. }
  8490. else if (!(entry instanceof PathBase)) {
  8491. opts = entry;
  8492. entry = this.cwd;
  8493. }
  8494. const { withFileTypes } = opts;
  8495. if (!entry.canReaddir()) {
  8496. return [];
  8497. }
  8498. else {
  8499. const p = await entry.readdir();
  8500. return withFileTypes ? p : p.map(e => e.name);
  8501. }
  8502. }
  8503. readdirSync(entry = this.cwd, opts = {
  8504. withFileTypes: true,
  8505. }) {
  8506. if (typeof entry === 'string') {
  8507. entry = this.cwd.resolve(entry);
  8508. }
  8509. else if (!(entry instanceof PathBase)) {
  8510. opts = entry;
  8511. entry = this.cwd;
  8512. }
  8513. const { withFileTypes = true } = opts;
  8514. if (!entry.canReaddir()) {
  8515. return [];
  8516. }
  8517. else if (withFileTypes) {
  8518. return entry.readdirSync();
  8519. }
  8520. else {
  8521. return entry.readdirSync().map(e => e.name);
  8522. }
  8523. }
  8524. /**
  8525. * Call lstat() on the string or Path object, and update all known
  8526. * information that can be determined.
  8527. *
  8528. * Note that unlike `fs.lstat()`, the returned value does not contain some
  8529. * information, such as `mode`, `dev`, `nlink`, and `ino`. If that
  8530. * information is required, you will need to call `fs.lstat` yourself.
  8531. *
  8532. * If the Path refers to a nonexistent file, or if the lstat call fails for
  8533. * any reason, `undefined` is returned. Otherwise the updated Path object is
  8534. * returned.
  8535. *
  8536. * Results are cached, and thus may be out of date if the filesystem is
  8537. * mutated.
  8538. */
  8539. async lstat(entry = this.cwd) {
  8540. if (typeof entry === 'string') {
  8541. entry = this.cwd.resolve(entry);
  8542. }
  8543. return entry.lstat();
  8544. }
  8545. /**
  8546. * synchronous {@link PathScurryBase.lstat}
  8547. */
  8548. lstatSync(entry = this.cwd) {
  8549. if (typeof entry === 'string') {
  8550. entry = this.cwd.resolve(entry);
  8551. }
  8552. return entry.lstatSync();
  8553. }
  8554. async readlink(entry = this.cwd, { withFileTypes } = {
  8555. withFileTypes: false,
  8556. }) {
  8557. if (typeof entry === 'string') {
  8558. entry = this.cwd.resolve(entry);
  8559. }
  8560. else if (!(entry instanceof PathBase)) {
  8561. withFileTypes = entry.withFileTypes;
  8562. entry = this.cwd;
  8563. }
  8564. const e = await entry.readlink();
  8565. return withFileTypes ? e : e?.fullpath();
  8566. }
  8567. readlinkSync(entry = this.cwd, { withFileTypes } = {
  8568. withFileTypes: false,
  8569. }) {
  8570. if (typeof entry === 'string') {
  8571. entry = this.cwd.resolve(entry);
  8572. }
  8573. else if (!(entry instanceof PathBase)) {
  8574. withFileTypes = entry.withFileTypes;
  8575. entry = this.cwd;
  8576. }
  8577. const e = entry.readlinkSync();
  8578. return withFileTypes ? e : e?.fullpath();
  8579. }
  8580. async realpath(entry = this.cwd, { withFileTypes } = {
  8581. withFileTypes: false,
  8582. }) {
  8583. if (typeof entry === 'string') {
  8584. entry = this.cwd.resolve(entry);
  8585. }
  8586. else if (!(entry instanceof PathBase)) {
  8587. withFileTypes = entry.withFileTypes;
  8588. entry = this.cwd;
  8589. }
  8590. const e = await entry.realpath();
  8591. return withFileTypes ? e : e?.fullpath();
  8592. }
  8593. realpathSync(entry = this.cwd, { withFileTypes } = {
  8594. withFileTypes: false,
  8595. }) {
  8596. if (typeof entry === 'string') {
  8597. entry = this.cwd.resolve(entry);
  8598. }
  8599. else if (!(entry instanceof PathBase)) {
  8600. withFileTypes = entry.withFileTypes;
  8601. entry = this.cwd;
  8602. }
  8603. const e = entry.realpathSync();
  8604. return withFileTypes ? e : e?.fullpath();
  8605. }
  8606. async walk(entry = this.cwd, opts = {}) {
  8607. if (typeof entry === 'string') {
  8608. entry = this.cwd.resolve(entry);
  8609. }
  8610. else if (!(entry instanceof PathBase)) {
  8611. opts = entry;
  8612. entry = this.cwd;
  8613. }
  8614. const { withFileTypes = true, follow = false, filter, walkFilter, } = opts;
  8615. const results = [];
  8616. if (!filter || filter(entry)) {
  8617. results.push(withFileTypes ? entry : entry.fullpath());
  8618. }
  8619. const dirs = new Set();
  8620. const walk = (dir, cb) => {
  8621. dirs.add(dir);
  8622. dir.readdirCB((er, entries) => {
  8623. /* c8 ignore start */
  8624. if (er) {
  8625. return cb(er);
  8626. }
  8627. /* c8 ignore stop */
  8628. let len = entries.length;
  8629. if (!len)
  8630. return cb();
  8631. const next = () => {
  8632. if (--len === 0) {
  8633. cb();
  8634. }
  8635. };
  8636. for (const e of entries) {
  8637. if (!filter || filter(e)) {
  8638. results.push(withFileTypes ? e : e.fullpath());
  8639. }
  8640. if (follow && e.isSymbolicLink()) {
  8641. e.realpath()
  8642. .then(r => (r?.isUnknown() ? r.lstat() : r))
  8643. .then(r => r?.shouldWalk(dirs, walkFilter) ? walk(r, next) : next());
  8644. }
  8645. else {
  8646. if (e.shouldWalk(dirs, walkFilter)) {
  8647. walk(e, next);
  8648. }
  8649. else {
  8650. next();
  8651. }
  8652. }
  8653. }
  8654. }, true); // zalgooooooo
  8655. };
  8656. const start = entry;
  8657. return new Promise((res, rej) => {
  8658. walk(start, er => {
  8659. /* c8 ignore start */
  8660. if (er)
  8661. return rej(er);
  8662. /* c8 ignore stop */
  8663. res(results);
  8664. });
  8665. });
  8666. }
  8667. walkSync(entry = this.cwd, opts = {}) {
  8668. if (typeof entry === 'string') {
  8669. entry = this.cwd.resolve(entry);
  8670. }
  8671. else if (!(entry instanceof PathBase)) {
  8672. opts = entry;
  8673. entry = this.cwd;
  8674. }
  8675. const { withFileTypes = true, follow = false, filter, walkFilter, } = opts;
  8676. const results = [];
  8677. if (!filter || filter(entry)) {
  8678. results.push(withFileTypes ? entry : entry.fullpath());
  8679. }
  8680. const dirs = new Set([entry]);
  8681. for (const dir of dirs) {
  8682. const entries = dir.readdirSync();
  8683. for (const e of entries) {
  8684. if (!filter || filter(e)) {
  8685. results.push(withFileTypes ? e : e.fullpath());
  8686. }
  8687. let r = e;
  8688. if (e.isSymbolicLink()) {
  8689. if (!(follow && (r = e.realpathSync())))
  8690. continue;
  8691. if (r.isUnknown())
  8692. r.lstatSync();
  8693. }
  8694. if (r.shouldWalk(dirs, walkFilter)) {
  8695. dirs.add(r);
  8696. }
  8697. }
  8698. }
  8699. return results;
  8700. }
  8701. /**
  8702. * Support for `for await`
  8703. *
  8704. * Alias for {@link PathScurryBase.iterate}
  8705. *
  8706. * Note: As of Node 19, this is very slow, compared to other methods of
  8707. * walking. Consider using {@link PathScurryBase.stream} if memory overhead
  8708. * and backpressure are concerns, or {@link PathScurryBase.walk} if not.
  8709. */
  8710. [Symbol.asyncIterator]() {
  8711. return this.iterate();
  8712. }
  8713. iterate(entry = this.cwd, options = {}) {
  8714. // iterating async over the stream is significantly more performant,
  8715. // especially in the warm-cache scenario, because it buffers up directory
  8716. // entries in the background instead of waiting for a yield for each one.
  8717. if (typeof entry === 'string') {
  8718. entry = this.cwd.resolve(entry);
  8719. }
  8720. else if (!(entry instanceof PathBase)) {
  8721. options = entry;
  8722. entry = this.cwd;
  8723. }
  8724. return this.stream(entry, options)[Symbol.asyncIterator]();
  8725. }
  8726. /**
  8727. * Iterating over a PathScurry performs a synchronous walk.
  8728. *
  8729. * Alias for {@link PathScurryBase.iterateSync}
  8730. */
  8731. [Symbol.iterator]() {
  8732. return this.iterateSync();
  8733. }
  8734. *iterateSync(entry = this.cwd, opts = {}) {
  8735. if (typeof entry === 'string') {
  8736. entry = this.cwd.resolve(entry);
  8737. }
  8738. else if (!(entry instanceof PathBase)) {
  8739. opts = entry;
  8740. entry = this.cwd;
  8741. }
  8742. const { withFileTypes = true, follow = false, filter, walkFilter, } = opts;
  8743. if (!filter || filter(entry)) {
  8744. yield withFileTypes ? entry : entry.fullpath();
  8745. }
  8746. const dirs = new Set([entry]);
  8747. for (const dir of dirs) {
  8748. const entries = dir.readdirSync();
  8749. for (const e of entries) {
  8750. if (!filter || filter(e)) {
  8751. yield withFileTypes ? e : e.fullpath();
  8752. }
  8753. let r = e;
  8754. if (e.isSymbolicLink()) {
  8755. if (!(follow && (r = e.realpathSync())))
  8756. continue;
  8757. if (r.isUnknown())
  8758. r.lstatSync();
  8759. }
  8760. if (r.shouldWalk(dirs, walkFilter)) {
  8761. dirs.add(r);
  8762. }
  8763. }
  8764. }
  8765. }
  8766. stream(entry = this.cwd, opts = {}) {
  8767. if (typeof entry === 'string') {
  8768. entry = this.cwd.resolve(entry);
  8769. }
  8770. else if (!(entry instanceof PathBase)) {
  8771. opts = entry;
  8772. entry = this.cwd;
  8773. }
  8774. const { withFileTypes = true, follow = false, filter, walkFilter, } = opts;
  8775. const results = new Minipass({ objectMode: true });
  8776. if (!filter || filter(entry)) {
  8777. results.write(withFileTypes ? entry : entry.fullpath());
  8778. }
  8779. const dirs = new Set();
  8780. const queue = [entry];
  8781. let processing = 0;
  8782. const process = () => {
  8783. let paused = false;
  8784. while (!paused) {
  8785. const dir = queue.shift();
  8786. if (!dir) {
  8787. if (processing === 0)
  8788. results.end();
  8789. return;
  8790. }
  8791. processing++;
  8792. dirs.add(dir);
  8793. const onReaddir = (er, entries, didRealpaths = false) => {
  8794. /* c8 ignore start */
  8795. if (er)
  8796. return results.emit('error', er);
  8797. /* c8 ignore stop */
  8798. if (follow && !didRealpaths) {
  8799. const promises = [];
  8800. for (const e of entries) {
  8801. if (e.isSymbolicLink()) {
  8802. promises.push(e
  8803. .realpath()
  8804. .then((r) => r?.isUnknown() ? r.lstat() : r));
  8805. }
  8806. }
  8807. if (promises.length) {
  8808. Promise.all(promises).then(() => onReaddir(null, entries, true));
  8809. return;
  8810. }
  8811. }
  8812. for (const e of entries) {
  8813. if (e && (!filter || filter(e))) {
  8814. if (!results.write(withFileTypes ? e : e.fullpath())) {
  8815. paused = true;
  8816. }
  8817. }
  8818. }
  8819. processing--;
  8820. for (const e of entries) {
  8821. const r = e.realpathCached() || e;
  8822. if (r.shouldWalk(dirs, walkFilter)) {
  8823. queue.push(r);
  8824. }
  8825. }
  8826. if (paused && !results.flowing) {
  8827. results.once('drain', process);
  8828. }
  8829. else if (!sync) {
  8830. process();
  8831. }
  8832. };
  8833. // zalgo containment
  8834. let sync = true;
  8835. dir.readdirCB(onReaddir, true);
  8836. sync = false;
  8837. }
  8838. };
  8839. process();
  8840. return results;
  8841. }
  8842. streamSync(entry = this.cwd, opts = {}) {
  8843. if (typeof entry === 'string') {
  8844. entry = this.cwd.resolve(entry);
  8845. }
  8846. else if (!(entry instanceof PathBase)) {
  8847. opts = entry;
  8848. entry = this.cwd;
  8849. }
  8850. const { withFileTypes = true, follow = false, filter, walkFilter, } = opts;
  8851. const results = new Minipass({ objectMode: true });
  8852. const dirs = new Set();
  8853. if (!filter || filter(entry)) {
  8854. results.write(withFileTypes ? entry : entry.fullpath());
  8855. }
  8856. const queue = [entry];
  8857. let processing = 0;
  8858. const process = () => {
  8859. let paused = false;
  8860. while (!paused) {
  8861. const dir = queue.shift();
  8862. if (!dir) {
  8863. if (processing === 0)
  8864. results.end();
  8865. return;
  8866. }
  8867. processing++;
  8868. dirs.add(dir);
  8869. const entries = dir.readdirSync();
  8870. for (const e of entries) {
  8871. if (!filter || filter(e)) {
  8872. if (!results.write(withFileTypes ? e : e.fullpath())) {
  8873. paused = true;
  8874. }
  8875. }
  8876. }
  8877. processing--;
  8878. for (const e of entries) {
  8879. let r = e;
  8880. if (e.isSymbolicLink()) {
  8881. if (!(follow && (r = e.realpathSync())))
  8882. continue;
  8883. if (r.isUnknown())
  8884. r.lstatSync();
  8885. }
  8886. if (r.shouldWalk(dirs, walkFilter)) {
  8887. queue.push(r);
  8888. }
  8889. }
  8890. }
  8891. if (paused && !results.flowing)
  8892. results.once('drain', process);
  8893. };
  8894. process();
  8895. return results;
  8896. }
  8897. chdir(path = this.cwd) {
  8898. const oldCwd = this.cwd;
  8899. this.cwd = typeof path === 'string' ? this.cwd.resolve(path) : path;
  8900. this.cwd[setAsCwd](oldCwd);
  8901. }
  8902. }
  8903. /**
  8904. * Windows implementation of {@link PathScurryBase}
  8905. *
  8906. * Defaults to case insensitve, uses `'\\'` to generate path strings. Uses
  8907. * {@link PathWin32} for Path objects.
  8908. */
  8909. class PathScurryWin32 extends PathScurryBase {
  8910. /**
  8911. * separator for generating path strings
  8912. */
  8913. sep = '\\';
  8914. constructor(cwd = process.cwd(), opts = {}) {
  8915. const { nocase = true } = opts;
  8916. super(cwd, win32$1, '\\', { ...opts, nocase });
  8917. this.nocase = nocase;
  8918. for (let p = this.cwd; p; p = p.parent) {
  8919. p.nocase = this.nocase;
  8920. }
  8921. }
  8922. /**
  8923. * @internal
  8924. */
  8925. parseRootPath(dir) {
  8926. // if the path starts with a single separator, it's not a UNC, and we'll
  8927. // just get separator as the root, and driveFromUNC will return \
  8928. // In that case, mount \ on the root from the cwd.
  8929. return win32$1.parse(dir).root.toUpperCase();
  8930. }
  8931. /**
  8932. * @internal
  8933. */
  8934. newRoot(fs) {
  8935. return new PathWin32(this.rootPath, IFDIR, undefined, this.roots, this.nocase, this.childrenCache(), { fs });
  8936. }
  8937. /**
  8938. * Return true if the provided path string is an absolute path
  8939. */
  8940. isAbsolute(p) {
  8941. return (p.startsWith('/') || p.startsWith('\\') || /^[a-z]:(\/|\\)/i.test(p));
  8942. }
  8943. }
  8944. /**
  8945. * {@link PathScurryBase} implementation for all posix systems other than Darwin.
  8946. *
  8947. * Defaults to case-sensitive matching, uses `'/'` to generate path strings.
  8948. *
  8949. * Uses {@link PathPosix} for Path objects.
  8950. */
  8951. class PathScurryPosix extends PathScurryBase {
  8952. /**
  8953. * separator for generating path strings
  8954. */
  8955. sep = '/';
  8956. constructor(cwd = process.cwd(), opts = {}) {
  8957. const { nocase = false } = opts;
  8958. super(cwd, posix$1, '/', { ...opts, nocase });
  8959. this.nocase = nocase;
  8960. }
  8961. /**
  8962. * @internal
  8963. */
  8964. parseRootPath(_dir) {
  8965. return '/';
  8966. }
  8967. /**
  8968. * @internal
  8969. */
  8970. newRoot(fs) {
  8971. return new PathPosix(this.rootPath, IFDIR, undefined, this.roots, this.nocase, this.childrenCache(), { fs });
  8972. }
  8973. /**
  8974. * Return true if the provided path string is an absolute path
  8975. */
  8976. isAbsolute(p) {
  8977. return p.startsWith('/');
  8978. }
  8979. }
  8980. /**
  8981. * {@link PathScurryBase} implementation for Darwin (macOS) systems.
  8982. *
  8983. * Defaults to case-insensitive matching, uses `'/'` for generating path
  8984. * strings.
  8985. *
  8986. * Uses {@link PathPosix} for Path objects.
  8987. */
  8988. class PathScurryDarwin extends PathScurryPosix {
  8989. constructor(cwd = process.cwd(), opts = {}) {
  8990. const { nocase = true } = opts;
  8991. super(cwd, { ...opts, nocase });
  8992. }
  8993. }
  8994. /**
  8995. * Default {@link PathBase} implementation for the current platform.
  8996. *
  8997. * {@link PathWin32} on Windows systems, {@link PathPosix} on all others.
  8998. */
  8999. process.platform === 'win32' ? PathWin32 : PathPosix;
  9000. /**
  9001. * Default {@link PathScurryBase} implementation for the current platform.
  9002. *
  9003. * {@link PathScurryWin32} on Windows systems, {@link PathScurryDarwin} on
  9004. * Darwin (macOS) systems, {@link PathScurryPosix} on all others.
  9005. */
  9006. const PathScurry = process.platform === 'win32' ? PathScurryWin32
  9007. : process.platform === 'darwin' ? PathScurryDarwin
  9008. : PathScurryPosix;
  9009. // this is just a very light wrapper around 2 arrays with an offset index
  9010. const isPatternList = (pl) => pl.length >= 1;
  9011. const isGlobList = (gl) => gl.length >= 1;
  9012. /**
  9013. * An immutable-ish view on an array of glob parts and their parsed
  9014. * results
  9015. */
  9016. class Pattern {
  9017. #patternList;
  9018. #globList;
  9019. #index;
  9020. length;
  9021. #platform;
  9022. #rest;
  9023. #globString;
  9024. #isDrive;
  9025. #isUNC;
  9026. #isAbsolute;
  9027. #followGlobstar = true;
  9028. constructor(patternList, globList, index, platform) {
  9029. if (!isPatternList(patternList)) {
  9030. throw new TypeError('empty pattern list');
  9031. }
  9032. if (!isGlobList(globList)) {
  9033. throw new TypeError('empty glob list');
  9034. }
  9035. if (globList.length !== patternList.length) {
  9036. throw new TypeError('mismatched pattern list and glob list lengths');
  9037. }
  9038. this.length = patternList.length;
  9039. if (index < 0 || index >= this.length) {
  9040. throw new TypeError('index out of range');
  9041. }
  9042. this.#patternList = patternList;
  9043. this.#globList = globList;
  9044. this.#index = index;
  9045. this.#platform = platform;
  9046. // normalize root entries of absolute patterns on initial creation.
  9047. if (this.#index === 0) {
  9048. // c: => ['c:/']
  9049. // C:/ => ['C:/']
  9050. // C:/x => ['C:/', 'x']
  9051. // //host/share => ['//host/share/']
  9052. // //host/share/ => ['//host/share/']
  9053. // //host/share/x => ['//host/share/', 'x']
  9054. // /etc => ['/', 'etc']
  9055. // / => ['/']
  9056. if (this.isUNC()) {
  9057. // '' / '' / 'host' / 'share'
  9058. const [p0, p1, p2, p3, ...prest] = this.#patternList;
  9059. const [g0, g1, g2, g3, ...grest] = this.#globList;
  9060. if (prest[0] === '') {
  9061. // ends in /
  9062. prest.shift();
  9063. grest.shift();
  9064. }
  9065. const p = [p0, p1, p2, p3, ''].join('/');
  9066. const g = [g0, g1, g2, g3, ''].join('/');
  9067. this.#patternList = [p, ...prest];
  9068. this.#globList = [g, ...grest];
  9069. this.length = this.#patternList.length;
  9070. }
  9071. else if (this.isDrive() || this.isAbsolute()) {
  9072. const [p1, ...prest] = this.#patternList;
  9073. const [g1, ...grest] = this.#globList;
  9074. if (prest[0] === '') {
  9075. // ends in /
  9076. prest.shift();
  9077. grest.shift();
  9078. }
  9079. const p = p1 + '/';
  9080. const g = g1 + '/';
  9081. this.#patternList = [p, ...prest];
  9082. this.#globList = [g, ...grest];
  9083. this.length = this.#patternList.length;
  9084. }
  9085. }
  9086. }
  9087. /**
  9088. * The first entry in the parsed list of patterns
  9089. */
  9090. pattern() {
  9091. return this.#patternList[this.#index];
  9092. }
  9093. /**
  9094. * true of if pattern() returns a string
  9095. */
  9096. isString() {
  9097. return typeof this.#patternList[this.#index] === 'string';
  9098. }
  9099. /**
  9100. * true of if pattern() returns GLOBSTAR
  9101. */
  9102. isGlobstar() {
  9103. return this.#patternList[this.#index] === GLOBSTAR$2;
  9104. }
  9105. /**
  9106. * true if pattern() returns a regexp
  9107. */
  9108. isRegExp() {
  9109. return this.#patternList[this.#index] instanceof RegExp;
  9110. }
  9111. /**
  9112. * The /-joined set of glob parts that make up this pattern
  9113. */
  9114. globString() {
  9115. return (this.#globString =
  9116. this.#globString ||
  9117. (this.#index === 0 ?
  9118. this.isAbsolute() ?
  9119. this.#globList[0] + this.#globList.slice(1).join('/')
  9120. : this.#globList.join('/')
  9121. : this.#globList.slice(this.#index).join('/')));
  9122. }
  9123. /**
  9124. * true if there are more pattern parts after this one
  9125. */
  9126. hasMore() {
  9127. return this.length > this.#index + 1;
  9128. }
  9129. /**
  9130. * The rest of the pattern after this part, or null if this is the end
  9131. */
  9132. rest() {
  9133. if (this.#rest !== undefined)
  9134. return this.#rest;
  9135. if (!this.hasMore())
  9136. return (this.#rest = null);
  9137. this.#rest = new Pattern(this.#patternList, this.#globList, this.#index + 1, this.#platform);
  9138. this.#rest.#isAbsolute = this.#isAbsolute;
  9139. this.#rest.#isUNC = this.#isUNC;
  9140. this.#rest.#isDrive = this.#isDrive;
  9141. return this.#rest;
  9142. }
  9143. /**
  9144. * true if the pattern represents a //unc/path/ on windows
  9145. */
  9146. isUNC() {
  9147. const pl = this.#patternList;
  9148. return this.#isUNC !== undefined ?
  9149. this.#isUNC
  9150. : (this.#isUNC =
  9151. this.#platform === 'win32' &&
  9152. this.#index === 0 &&
  9153. pl[0] === '' &&
  9154. pl[1] === '' &&
  9155. typeof pl[2] === 'string' &&
  9156. !!pl[2] &&
  9157. typeof pl[3] === 'string' &&
  9158. !!pl[3]);
  9159. }
  9160. // pattern like C:/...
  9161. // split = ['C:', ...]
  9162. // XXX: would be nice to handle patterns like `c:*` to test the cwd
  9163. // in c: for *, but I don't know of a way to even figure out what that
  9164. // cwd is without actually chdir'ing into it?
  9165. /**
  9166. * True if the pattern starts with a drive letter on Windows
  9167. */
  9168. isDrive() {
  9169. const pl = this.#patternList;
  9170. return this.#isDrive !== undefined ?
  9171. this.#isDrive
  9172. : (this.#isDrive =
  9173. this.#platform === 'win32' &&
  9174. this.#index === 0 &&
  9175. this.length > 1 &&
  9176. typeof pl[0] === 'string' &&
  9177. /^[a-z]:$/i.test(pl[0]));
  9178. }
  9179. // pattern = '/' or '/...' or '/x/...'
  9180. // split = ['', ''] or ['', ...] or ['', 'x', ...]
  9181. // Drive and UNC both considered absolute on windows
  9182. /**
  9183. * True if the pattern is rooted on an absolute path
  9184. */
  9185. isAbsolute() {
  9186. const pl = this.#patternList;
  9187. return this.#isAbsolute !== undefined ?
  9188. this.#isAbsolute
  9189. : (this.#isAbsolute =
  9190. (pl[0] === '' && pl.length > 1) ||
  9191. this.isDrive() ||
  9192. this.isUNC());
  9193. }
  9194. /**
  9195. * consume the root of the pattern, and return it
  9196. */
  9197. root() {
  9198. const p = this.#patternList[0];
  9199. return (typeof p === 'string' && this.isAbsolute() && this.#index === 0) ?
  9200. p
  9201. : '';
  9202. }
  9203. /**
  9204. * Check to see if the current globstar pattern is allowed to follow
  9205. * a symbolic link.
  9206. */
  9207. checkFollowGlobstar() {
  9208. return !(this.#index === 0 ||
  9209. !this.isGlobstar() ||
  9210. !this.#followGlobstar);
  9211. }
  9212. /**
  9213. * Mark that the current globstar pattern is following a symbolic link
  9214. */
  9215. markFollowGlobstar() {
  9216. if (this.#index === 0 || !this.isGlobstar() || !this.#followGlobstar)
  9217. return false;
  9218. this.#followGlobstar = false;
  9219. return true;
  9220. }
  9221. }
  9222. // give it a pattern, and it'll be able to tell you if
  9223. // a given path should be ignored.
  9224. // Ignoring a path ignores its children if the pattern ends in /**
  9225. // Ignores are always parsed in dot:true mode
  9226. const defaultPlatform$1 = (typeof process === 'object' &&
  9227. process &&
  9228. typeof process.platform === 'string') ?
  9229. process.platform
  9230. : 'linux';
  9231. /**
  9232. * Class used to process ignored patterns
  9233. */
  9234. class Ignore {
  9235. relative;
  9236. relativeChildren;
  9237. absolute;
  9238. absoluteChildren;
  9239. platform;
  9240. mmopts;
  9241. constructor(ignored, { nobrace, nocase, noext, noglobstar, platform = defaultPlatform$1, }) {
  9242. this.relative = [];
  9243. this.absolute = [];
  9244. this.relativeChildren = [];
  9245. this.absoluteChildren = [];
  9246. this.platform = platform;
  9247. this.mmopts = {
  9248. dot: true,
  9249. nobrace,
  9250. nocase,
  9251. noext,
  9252. noglobstar,
  9253. optimizationLevel: 2,
  9254. platform,
  9255. nocomment: true,
  9256. nonegate: true,
  9257. };
  9258. for (const ign of ignored)
  9259. this.add(ign);
  9260. }
  9261. add(ign) {
  9262. // this is a little weird, but it gives us a clean set of optimized
  9263. // minimatch matchers, without getting tripped up if one of them
  9264. // ends in /** inside a brace section, and it's only inefficient at
  9265. // the start of the walk, not along it.
  9266. // It'd be nice if the Pattern class just had a .test() method, but
  9267. // handling globstars is a bit of a pita, and that code already lives
  9268. // in minimatch anyway.
  9269. // Another way would be if maybe Minimatch could take its set/globParts
  9270. // as an option, and then we could at least just use Pattern to test
  9271. // for absolute-ness.
  9272. // Yet another way, Minimatch could take an array of glob strings, and
  9273. // a cwd option, and do the right thing.
  9274. const mm = new Minimatch(ign, this.mmopts);
  9275. for (let i = 0; i < mm.set.length; i++) {
  9276. const parsed = mm.set[i];
  9277. const globParts = mm.globParts[i];
  9278. /* c8 ignore start */
  9279. if (!parsed || !globParts) {
  9280. throw new Error('invalid pattern object');
  9281. }
  9282. // strip off leading ./ portions
  9283. // https://github.com/isaacs/node-glob/issues/570
  9284. while (parsed[0] === '.' && globParts[0] === '.') {
  9285. parsed.shift();
  9286. globParts.shift();
  9287. }
  9288. /* c8 ignore stop */
  9289. const p = new Pattern(parsed, globParts, 0, this.platform);
  9290. const m = new Minimatch(p.globString(), this.mmopts);
  9291. const children = globParts[globParts.length - 1] === '**';
  9292. const absolute = p.isAbsolute();
  9293. if (absolute)
  9294. this.absolute.push(m);
  9295. else
  9296. this.relative.push(m);
  9297. if (children) {
  9298. if (absolute)
  9299. this.absoluteChildren.push(m);
  9300. else
  9301. this.relativeChildren.push(m);
  9302. }
  9303. }
  9304. }
  9305. ignored(p) {
  9306. const fullpath = p.fullpath();
  9307. const fullpaths = `${fullpath}/`;
  9308. const relative = p.relative() || '.';
  9309. const relatives = `${relative}/`;
  9310. for (const m of this.relative) {
  9311. if (m.match(relative) || m.match(relatives))
  9312. return true;
  9313. }
  9314. for (const m of this.absolute) {
  9315. if (m.match(fullpath) || m.match(fullpaths))
  9316. return true;
  9317. }
  9318. return false;
  9319. }
  9320. childrenIgnored(p) {
  9321. const fullpath = p.fullpath() + '/';
  9322. const relative = (p.relative() || '.') + '/';
  9323. for (const m of this.relativeChildren) {
  9324. if (m.match(relative))
  9325. return true;
  9326. }
  9327. for (const m of this.absoluteChildren) {
  9328. if (m.match(fullpath))
  9329. return true;
  9330. }
  9331. return false;
  9332. }
  9333. }
  9334. // synchronous utility for filtering entries and calculating subwalks
  9335. /**
  9336. * A cache of which patterns have been processed for a given Path
  9337. */
  9338. class HasWalkedCache {
  9339. store;
  9340. constructor(store = new Map()) {
  9341. this.store = store;
  9342. }
  9343. copy() {
  9344. return new HasWalkedCache(new Map(this.store));
  9345. }
  9346. hasWalked(target, pattern) {
  9347. return this.store.get(target.fullpath())?.has(pattern.globString());
  9348. }
  9349. storeWalked(target, pattern) {
  9350. const fullpath = target.fullpath();
  9351. const cached = this.store.get(fullpath);
  9352. if (cached)
  9353. cached.add(pattern.globString());
  9354. else
  9355. this.store.set(fullpath, new Set([pattern.globString()]));
  9356. }
  9357. }
  9358. /**
  9359. * A record of which paths have been matched in a given walk step,
  9360. * and whether they only are considered a match if they are a directory,
  9361. * and whether their absolute or relative path should be returned.
  9362. */
  9363. class MatchRecord {
  9364. store = new Map();
  9365. add(target, absolute, ifDir) {
  9366. const n = (absolute ? 2 : 0) | (ifDir ? 1 : 0);
  9367. const current = this.store.get(target);
  9368. this.store.set(target, current === undefined ? n : n & current);
  9369. }
  9370. // match, absolute, ifdir
  9371. entries() {
  9372. return [...this.store.entries()].map(([path, n]) => [
  9373. path,
  9374. !!(n & 2),
  9375. !!(n & 1),
  9376. ]);
  9377. }
  9378. }
  9379. /**
  9380. * A collection of patterns that must be processed in a subsequent step
  9381. * for a given path.
  9382. */
  9383. class SubWalks {
  9384. store = new Map();
  9385. add(target, pattern) {
  9386. if (!target.canReaddir()) {
  9387. return;
  9388. }
  9389. const subs = this.store.get(target);
  9390. if (subs) {
  9391. if (!subs.find(p => p.globString() === pattern.globString())) {
  9392. subs.push(pattern);
  9393. }
  9394. }
  9395. else
  9396. this.store.set(target, [pattern]);
  9397. }
  9398. get(target) {
  9399. const subs = this.store.get(target);
  9400. /* c8 ignore start */
  9401. if (!subs) {
  9402. throw new Error('attempting to walk unknown path');
  9403. }
  9404. /* c8 ignore stop */
  9405. return subs;
  9406. }
  9407. entries() {
  9408. return this.keys().map(k => [k, this.store.get(k)]);
  9409. }
  9410. keys() {
  9411. return [...this.store.keys()].filter(t => t.canReaddir());
  9412. }
  9413. }
  9414. /**
  9415. * The class that processes patterns for a given path.
  9416. *
  9417. * Handles child entry filtering, and determining whether a path's
  9418. * directory contents must be read.
  9419. */
  9420. class Processor {
  9421. hasWalkedCache;
  9422. matches = new MatchRecord();
  9423. subwalks = new SubWalks();
  9424. patterns;
  9425. follow;
  9426. dot;
  9427. opts;
  9428. constructor(opts, hasWalkedCache) {
  9429. this.opts = opts;
  9430. this.follow = !!opts.follow;
  9431. this.dot = !!opts.dot;
  9432. this.hasWalkedCache =
  9433. hasWalkedCache ? hasWalkedCache.copy() : new HasWalkedCache();
  9434. }
  9435. processPatterns(target, patterns) {
  9436. this.patterns = patterns;
  9437. const processingSet = patterns.map(p => [target, p]);
  9438. // map of paths to the magic-starting subwalks they need to walk
  9439. // first item in patterns is the filter
  9440. for (let [t, pattern] of processingSet) {
  9441. this.hasWalkedCache.storeWalked(t, pattern);
  9442. const root = pattern.root();
  9443. const absolute = pattern.isAbsolute() && this.opts.absolute !== false;
  9444. // start absolute patterns at root
  9445. if (root) {
  9446. t = t.resolve(root === '/' && this.opts.root !== undefined ?
  9447. this.opts.root
  9448. : root);
  9449. const rest = pattern.rest();
  9450. if (!rest) {
  9451. this.matches.add(t, true, false);
  9452. continue;
  9453. }
  9454. else {
  9455. pattern = rest;
  9456. }
  9457. }
  9458. if (t.isENOENT())
  9459. continue;
  9460. let p;
  9461. let rest;
  9462. let changed = false;
  9463. while (typeof (p = pattern.pattern()) === 'string' &&
  9464. (rest = pattern.rest())) {
  9465. const c = t.resolve(p);
  9466. t = c;
  9467. pattern = rest;
  9468. changed = true;
  9469. }
  9470. p = pattern.pattern();
  9471. rest = pattern.rest();
  9472. if (changed) {
  9473. if (this.hasWalkedCache.hasWalked(t, pattern))
  9474. continue;
  9475. this.hasWalkedCache.storeWalked(t, pattern);
  9476. }
  9477. // now we have either a final string for a known entry,
  9478. // more strings for an unknown entry,
  9479. // or a pattern starting with magic, mounted on t.
  9480. if (typeof p === 'string') {
  9481. // must not be final entry, otherwise we would have
  9482. // concatenated it earlier.
  9483. const ifDir = p === '..' || p === '' || p === '.';
  9484. this.matches.add(t.resolve(p), absolute, ifDir);
  9485. continue;
  9486. }
  9487. else if (p === GLOBSTAR$2) {
  9488. // if no rest, match and subwalk pattern
  9489. // if rest, process rest and subwalk pattern
  9490. // if it's a symlink, but we didn't get here by way of a
  9491. // globstar match (meaning it's the first time THIS globstar
  9492. // has traversed a symlink), then we follow it. Otherwise, stop.
  9493. if (!t.isSymbolicLink() ||
  9494. this.follow ||
  9495. pattern.checkFollowGlobstar()) {
  9496. this.subwalks.add(t, pattern);
  9497. }
  9498. const rp = rest?.pattern();
  9499. const rrest = rest?.rest();
  9500. if (!rest || ((rp === '' || rp === '.') && !rrest)) {
  9501. // only HAS to be a dir if it ends in **/ or **/.
  9502. // but ending in ** will match files as well.
  9503. this.matches.add(t, absolute, rp === '' || rp === '.');
  9504. }
  9505. else {
  9506. if (rp === '..') {
  9507. // this would mean you're matching **/.. at the fs root,
  9508. // and no thanks, I'm not gonna test that specific case.
  9509. /* c8 ignore start */
  9510. const tp = t.parent || t;
  9511. /* c8 ignore stop */
  9512. if (!rrest)
  9513. this.matches.add(tp, absolute, true);
  9514. else if (!this.hasWalkedCache.hasWalked(tp, rrest)) {
  9515. this.subwalks.add(tp, rrest);
  9516. }
  9517. }
  9518. }
  9519. }
  9520. else if (p instanceof RegExp) {
  9521. this.subwalks.add(t, pattern);
  9522. }
  9523. }
  9524. return this;
  9525. }
  9526. subwalkTargets() {
  9527. return this.subwalks.keys();
  9528. }
  9529. child() {
  9530. return new Processor(this.opts, this.hasWalkedCache);
  9531. }
  9532. // return a new Processor containing the subwalks for each
  9533. // child entry, and a set of matches, and
  9534. // a hasWalkedCache that's a copy of this one
  9535. // then we're going to call
  9536. filterEntries(parent, entries) {
  9537. const patterns = this.subwalks.get(parent);
  9538. // put matches and entry walks into the results processor
  9539. const results = this.child();
  9540. for (const e of entries) {
  9541. for (const pattern of patterns) {
  9542. const absolute = pattern.isAbsolute();
  9543. const p = pattern.pattern();
  9544. const rest = pattern.rest();
  9545. if (p === GLOBSTAR$2) {
  9546. results.testGlobstar(e, pattern, rest, absolute);
  9547. }
  9548. else if (p instanceof RegExp) {
  9549. results.testRegExp(e, p, rest, absolute);
  9550. }
  9551. else {
  9552. results.testString(e, p, rest, absolute);
  9553. }
  9554. }
  9555. }
  9556. return results;
  9557. }
  9558. testGlobstar(e, pattern, rest, absolute) {
  9559. if (this.dot || !e.name.startsWith('.')) {
  9560. if (!pattern.hasMore()) {
  9561. this.matches.add(e, absolute, false);
  9562. }
  9563. if (e.canReaddir()) {
  9564. // if we're in follow mode or it's not a symlink, just keep
  9565. // testing the same pattern. If there's more after the globstar,
  9566. // then this symlink consumes the globstar. If not, then we can
  9567. // follow at most ONE symlink along the way, so we mark it, which
  9568. // also checks to ensure that it wasn't already marked.
  9569. if (this.follow || !e.isSymbolicLink()) {
  9570. this.subwalks.add(e, pattern);
  9571. }
  9572. else if (e.isSymbolicLink()) {
  9573. if (rest && pattern.checkFollowGlobstar()) {
  9574. this.subwalks.add(e, rest);
  9575. }
  9576. else if (pattern.markFollowGlobstar()) {
  9577. this.subwalks.add(e, pattern);
  9578. }
  9579. }
  9580. }
  9581. }
  9582. // if the NEXT thing matches this entry, then also add
  9583. // the rest.
  9584. if (rest) {
  9585. const rp = rest.pattern();
  9586. if (typeof rp === 'string' &&
  9587. // dots and empty were handled already
  9588. rp !== '..' &&
  9589. rp !== '' &&
  9590. rp !== '.') {
  9591. this.testString(e, rp, rest.rest(), absolute);
  9592. }
  9593. else if (rp === '..') {
  9594. /* c8 ignore start */
  9595. const ep = e.parent || e;
  9596. /* c8 ignore stop */
  9597. this.subwalks.add(ep, rest);
  9598. }
  9599. else if (rp instanceof RegExp) {
  9600. this.testRegExp(e, rp, rest.rest(), absolute);
  9601. }
  9602. }
  9603. }
  9604. testRegExp(e, p, rest, absolute) {
  9605. if (!p.test(e.name))
  9606. return;
  9607. if (!rest) {
  9608. this.matches.add(e, absolute, false);
  9609. }
  9610. else {
  9611. this.subwalks.add(e, rest);
  9612. }
  9613. }
  9614. testString(e, p, rest, absolute) {
  9615. // should never happen?
  9616. if (!e.isNamed(p))
  9617. return;
  9618. if (!rest) {
  9619. this.matches.add(e, absolute, false);
  9620. }
  9621. else {
  9622. this.subwalks.add(e, rest);
  9623. }
  9624. }
  9625. }
  9626. /**
  9627. * Single-use utility classes to provide functionality to the {@link Glob}
  9628. * methods.
  9629. *
  9630. * @module
  9631. */
  9632. const makeIgnore = (ignore, opts) => typeof ignore === 'string' ? new Ignore([ignore], opts)
  9633. : Array.isArray(ignore) ? new Ignore(ignore, opts)
  9634. : ignore;
  9635. /**
  9636. * basic walking utilities that all the glob walker types use
  9637. */
  9638. class GlobUtil {
  9639. path;
  9640. patterns;
  9641. opts;
  9642. seen = new Set();
  9643. paused = false;
  9644. aborted = false;
  9645. #onResume = [];
  9646. #ignore;
  9647. #sep;
  9648. signal;
  9649. maxDepth;
  9650. includeChildMatches;
  9651. constructor(patterns, path, opts) {
  9652. this.patterns = patterns;
  9653. this.path = path;
  9654. this.opts = opts;
  9655. this.#sep = !opts.posix && opts.platform === 'win32' ? '\\' : '/';
  9656. this.includeChildMatches = opts.includeChildMatches !== false;
  9657. if (opts.ignore || !this.includeChildMatches) {
  9658. this.#ignore = makeIgnore(opts.ignore ?? [], opts);
  9659. if (!this.includeChildMatches &&
  9660. typeof this.#ignore.add !== 'function') {
  9661. const m = 'cannot ignore child matches, ignore lacks add() method.';
  9662. throw new Error(m);
  9663. }
  9664. }
  9665. // ignore, always set with maxDepth, but it's optional on the
  9666. // GlobOptions type
  9667. /* c8 ignore start */
  9668. this.maxDepth = opts.maxDepth || Infinity;
  9669. /* c8 ignore stop */
  9670. if (opts.signal) {
  9671. this.signal = opts.signal;
  9672. this.signal.addEventListener('abort', () => {
  9673. this.#onResume.length = 0;
  9674. });
  9675. }
  9676. }
  9677. #ignored(path) {
  9678. return this.seen.has(path) || !!this.#ignore?.ignored?.(path);
  9679. }
  9680. #childrenIgnored(path) {
  9681. return !!this.#ignore?.childrenIgnored?.(path);
  9682. }
  9683. // backpressure mechanism
  9684. pause() {
  9685. this.paused = true;
  9686. }
  9687. resume() {
  9688. /* c8 ignore start */
  9689. if (this.signal?.aborted)
  9690. return;
  9691. /* c8 ignore stop */
  9692. this.paused = false;
  9693. let fn = undefined;
  9694. while (!this.paused && (fn = this.#onResume.shift())) {
  9695. fn();
  9696. }
  9697. }
  9698. onResume(fn) {
  9699. if (this.signal?.aborted)
  9700. return;
  9701. /* c8 ignore start */
  9702. if (!this.paused) {
  9703. fn();
  9704. }
  9705. else {
  9706. /* c8 ignore stop */
  9707. this.#onResume.push(fn);
  9708. }
  9709. }
  9710. // do the requisite realpath/stat checking, and return the path
  9711. // to add or undefined to filter it out.
  9712. async matchCheck(e, ifDir) {
  9713. if (ifDir && this.opts.nodir)
  9714. return undefined;
  9715. let rpc;
  9716. if (this.opts.realpath) {
  9717. rpc = e.realpathCached() || (await e.realpath());
  9718. if (!rpc)
  9719. return undefined;
  9720. e = rpc;
  9721. }
  9722. const needStat = e.isUnknown() || this.opts.stat;
  9723. const s = needStat ? await e.lstat() : e;
  9724. if (this.opts.follow && this.opts.nodir && s?.isSymbolicLink()) {
  9725. const target = await s.realpath();
  9726. /* c8 ignore start */
  9727. if (target && (target.isUnknown() || this.opts.stat)) {
  9728. await target.lstat();
  9729. }
  9730. /* c8 ignore stop */
  9731. }
  9732. return this.matchCheckTest(s, ifDir);
  9733. }
  9734. matchCheckTest(e, ifDir) {
  9735. return (e &&
  9736. (this.maxDepth === Infinity || e.depth() <= this.maxDepth) &&
  9737. (!ifDir || e.canReaddir()) &&
  9738. (!this.opts.nodir || !e.isDirectory()) &&
  9739. (!this.opts.nodir ||
  9740. !this.opts.follow ||
  9741. !e.isSymbolicLink() ||
  9742. !e.realpathCached()?.isDirectory()) &&
  9743. !this.#ignored(e)) ?
  9744. e
  9745. : undefined;
  9746. }
  9747. matchCheckSync(e, ifDir) {
  9748. if (ifDir && this.opts.nodir)
  9749. return undefined;
  9750. let rpc;
  9751. if (this.opts.realpath) {
  9752. rpc = e.realpathCached() || e.realpathSync();
  9753. if (!rpc)
  9754. return undefined;
  9755. e = rpc;
  9756. }
  9757. const needStat = e.isUnknown() || this.opts.stat;
  9758. const s = needStat ? e.lstatSync() : e;
  9759. if (this.opts.follow && this.opts.nodir && s?.isSymbolicLink()) {
  9760. const target = s.realpathSync();
  9761. if (target && (target?.isUnknown() || this.opts.stat)) {
  9762. target.lstatSync();
  9763. }
  9764. }
  9765. return this.matchCheckTest(s, ifDir);
  9766. }
  9767. matchFinish(e, absolute) {
  9768. if (this.#ignored(e))
  9769. return;
  9770. // we know we have an ignore if this is false, but TS doesn't
  9771. if (!this.includeChildMatches && this.#ignore?.add) {
  9772. const ign = `${e.relativePosix()}/**`;
  9773. this.#ignore.add(ign);
  9774. }
  9775. const abs = this.opts.absolute === undefined ? absolute : this.opts.absolute;
  9776. this.seen.add(e);
  9777. const mark = this.opts.mark && e.isDirectory() ? this.#sep : '';
  9778. // ok, we have what we need!
  9779. if (this.opts.withFileTypes) {
  9780. this.matchEmit(e);
  9781. }
  9782. else if (abs) {
  9783. const abs = this.opts.posix ? e.fullpathPosix() : e.fullpath();
  9784. this.matchEmit(abs + mark);
  9785. }
  9786. else {
  9787. const rel = this.opts.posix ? e.relativePosix() : e.relative();
  9788. const pre = this.opts.dotRelative && !rel.startsWith('..' + this.#sep) ?
  9789. '.' + this.#sep
  9790. : '';
  9791. this.matchEmit(!rel ? '.' + mark : pre + rel + mark);
  9792. }
  9793. }
  9794. async match(e, absolute, ifDir) {
  9795. const p = await this.matchCheck(e, ifDir);
  9796. if (p)
  9797. this.matchFinish(p, absolute);
  9798. }
  9799. matchSync(e, absolute, ifDir) {
  9800. const p = this.matchCheckSync(e, ifDir);
  9801. if (p)
  9802. this.matchFinish(p, absolute);
  9803. }
  9804. walkCB(target, patterns, cb) {
  9805. /* c8 ignore start */
  9806. if (this.signal?.aborted)
  9807. cb();
  9808. /* c8 ignore stop */
  9809. this.walkCB2(target, patterns, new Processor(this.opts), cb);
  9810. }
  9811. walkCB2(target, patterns, processor, cb) {
  9812. if (this.#childrenIgnored(target))
  9813. return cb();
  9814. if (this.signal?.aborted)
  9815. cb();
  9816. if (this.paused) {
  9817. this.onResume(() => this.walkCB2(target, patterns, processor, cb));
  9818. return;
  9819. }
  9820. processor.processPatterns(target, patterns);
  9821. // done processing. all of the above is sync, can be abstracted out.
  9822. // subwalks is a map of paths to the entry filters they need
  9823. // matches is a map of paths to [absolute, ifDir] tuples.
  9824. let tasks = 1;
  9825. const next = () => {
  9826. if (--tasks === 0)
  9827. cb();
  9828. };
  9829. for (const [m, absolute, ifDir] of processor.matches.entries()) {
  9830. if (this.#ignored(m))
  9831. continue;
  9832. tasks++;
  9833. this.match(m, absolute, ifDir).then(() => next());
  9834. }
  9835. for (const t of processor.subwalkTargets()) {
  9836. if (this.maxDepth !== Infinity && t.depth() >= this.maxDepth) {
  9837. continue;
  9838. }
  9839. tasks++;
  9840. const childrenCached = t.readdirCached();
  9841. if (t.calledReaddir())
  9842. this.walkCB3(t, childrenCached, processor, next);
  9843. else {
  9844. t.readdirCB((_, entries) => this.walkCB3(t, entries, processor, next), true);
  9845. }
  9846. }
  9847. next();
  9848. }
  9849. walkCB3(target, entries, processor, cb) {
  9850. processor = processor.filterEntries(target, entries);
  9851. let tasks = 1;
  9852. const next = () => {
  9853. if (--tasks === 0)
  9854. cb();
  9855. };
  9856. for (const [m, absolute, ifDir] of processor.matches.entries()) {
  9857. if (this.#ignored(m))
  9858. continue;
  9859. tasks++;
  9860. this.match(m, absolute, ifDir).then(() => next());
  9861. }
  9862. for (const [target, patterns] of processor.subwalks.entries()) {
  9863. tasks++;
  9864. this.walkCB2(target, patterns, processor.child(), next);
  9865. }
  9866. next();
  9867. }
  9868. walkCBSync(target, patterns, cb) {
  9869. /* c8 ignore start */
  9870. if (this.signal?.aborted)
  9871. cb();
  9872. /* c8 ignore stop */
  9873. this.walkCB2Sync(target, patterns, new Processor(this.opts), cb);
  9874. }
  9875. walkCB2Sync(target, patterns, processor, cb) {
  9876. if (this.#childrenIgnored(target))
  9877. return cb();
  9878. if (this.signal?.aborted)
  9879. cb();
  9880. if (this.paused) {
  9881. this.onResume(() => this.walkCB2Sync(target, patterns, processor, cb));
  9882. return;
  9883. }
  9884. processor.processPatterns(target, patterns);
  9885. // done processing. all of the above is sync, can be abstracted out.
  9886. // subwalks is a map of paths to the entry filters they need
  9887. // matches is a map of paths to [absolute, ifDir] tuples.
  9888. let tasks = 1;
  9889. const next = () => {
  9890. if (--tasks === 0)
  9891. cb();
  9892. };
  9893. for (const [m, absolute, ifDir] of processor.matches.entries()) {
  9894. if (this.#ignored(m))
  9895. continue;
  9896. this.matchSync(m, absolute, ifDir);
  9897. }
  9898. for (const t of processor.subwalkTargets()) {
  9899. if (this.maxDepth !== Infinity && t.depth() >= this.maxDepth) {
  9900. continue;
  9901. }
  9902. tasks++;
  9903. const children = t.readdirSync();
  9904. this.walkCB3Sync(t, children, processor, next);
  9905. }
  9906. next();
  9907. }
  9908. walkCB3Sync(target, entries, processor, cb) {
  9909. processor = processor.filterEntries(target, entries);
  9910. let tasks = 1;
  9911. const next = () => {
  9912. if (--tasks === 0)
  9913. cb();
  9914. };
  9915. for (const [m, absolute, ifDir] of processor.matches.entries()) {
  9916. if (this.#ignored(m))
  9917. continue;
  9918. this.matchSync(m, absolute, ifDir);
  9919. }
  9920. for (const [target, patterns] of processor.subwalks.entries()) {
  9921. tasks++;
  9922. this.walkCB2Sync(target, patterns, processor.child(), next);
  9923. }
  9924. next();
  9925. }
  9926. }
  9927. class GlobWalker extends GlobUtil {
  9928. matches = new Set();
  9929. constructor(patterns, path, opts) {
  9930. super(patterns, path, opts);
  9931. }
  9932. matchEmit(e) {
  9933. this.matches.add(e);
  9934. }
  9935. async walk() {
  9936. if (this.signal?.aborted)
  9937. throw this.signal.reason;
  9938. if (this.path.isUnknown()) {
  9939. await this.path.lstat();
  9940. }
  9941. await new Promise((res, rej) => {
  9942. this.walkCB(this.path, this.patterns, () => {
  9943. if (this.signal?.aborted) {
  9944. rej(this.signal.reason);
  9945. }
  9946. else {
  9947. res(this.matches);
  9948. }
  9949. });
  9950. });
  9951. return this.matches;
  9952. }
  9953. walkSync() {
  9954. if (this.signal?.aborted)
  9955. throw this.signal.reason;
  9956. if (this.path.isUnknown()) {
  9957. this.path.lstatSync();
  9958. }
  9959. // nothing for the callback to do, because this never pauses
  9960. this.walkCBSync(this.path, this.patterns, () => {
  9961. if (this.signal?.aborted)
  9962. throw this.signal.reason;
  9963. });
  9964. return this.matches;
  9965. }
  9966. }
  9967. class GlobStream extends GlobUtil {
  9968. results;
  9969. constructor(patterns, path, opts) {
  9970. super(patterns, path, opts);
  9971. this.results = new Minipass({
  9972. signal: this.signal,
  9973. objectMode: true,
  9974. });
  9975. this.results.on('drain', () => this.resume());
  9976. this.results.on('resume', () => this.resume());
  9977. }
  9978. matchEmit(e) {
  9979. this.results.write(e);
  9980. if (!this.results.flowing)
  9981. this.pause();
  9982. }
  9983. stream() {
  9984. const target = this.path;
  9985. if (target.isUnknown()) {
  9986. target.lstat().then(() => {
  9987. this.walkCB(target, this.patterns, () => this.results.end());
  9988. });
  9989. }
  9990. else {
  9991. this.walkCB(target, this.patterns, () => this.results.end());
  9992. }
  9993. return this.results;
  9994. }
  9995. streamSync() {
  9996. if (this.path.isUnknown()) {
  9997. this.path.lstatSync();
  9998. }
  9999. this.walkCBSync(this.path, this.patterns, () => this.results.end());
  10000. return this.results;
  10001. }
  10002. }
  10003. // if no process global, just call it linux.
  10004. // so we default to case-sensitive, / separators
  10005. const defaultPlatform = (typeof process === 'object' &&
  10006. process &&
  10007. typeof process.platform === 'string') ?
  10008. process.platform
  10009. : 'linux';
  10010. /**
  10011. * An object that can perform glob pattern traversals.
  10012. */
  10013. class Glob {
  10014. absolute;
  10015. cwd;
  10016. root;
  10017. dot;
  10018. dotRelative;
  10019. follow;
  10020. ignore;
  10021. magicalBraces;
  10022. mark;
  10023. matchBase;
  10024. maxDepth;
  10025. nobrace;
  10026. nocase;
  10027. nodir;
  10028. noext;
  10029. noglobstar;
  10030. pattern;
  10031. platform;
  10032. realpath;
  10033. scurry;
  10034. stat;
  10035. signal;
  10036. windowsPathsNoEscape;
  10037. withFileTypes;
  10038. includeChildMatches;
  10039. /**
  10040. * The options provided to the constructor.
  10041. */
  10042. opts;
  10043. /**
  10044. * An array of parsed immutable {@link Pattern} objects.
  10045. */
  10046. patterns;
  10047. /**
  10048. * All options are stored as properties on the `Glob` object.
  10049. *
  10050. * See {@link GlobOptions} for full options descriptions.
  10051. *
  10052. * Note that a previous `Glob` object can be passed as the
  10053. * `GlobOptions` to another `Glob` instantiation to re-use settings
  10054. * and caches with a new pattern.
  10055. *
  10056. * Traversal functions can be called multiple times to run the walk
  10057. * again.
  10058. */
  10059. constructor(pattern, opts) {
  10060. /* c8 ignore start */
  10061. if (!opts)
  10062. throw new TypeError('glob options required');
  10063. /* c8 ignore stop */
  10064. this.withFileTypes = !!opts.withFileTypes;
  10065. this.signal = opts.signal;
  10066. this.follow = !!opts.follow;
  10067. this.dot = !!opts.dot;
  10068. this.dotRelative = !!opts.dotRelative;
  10069. this.nodir = !!opts.nodir;
  10070. this.mark = !!opts.mark;
  10071. if (!opts.cwd) {
  10072. this.cwd = '';
  10073. }
  10074. else if (opts.cwd instanceof URL || opts.cwd.startsWith('file://')) {
  10075. opts.cwd = fileURLToPath(opts.cwd);
  10076. }
  10077. this.cwd = opts.cwd || '';
  10078. this.root = opts.root;
  10079. this.magicalBraces = !!opts.magicalBraces;
  10080. this.nobrace = !!opts.nobrace;
  10081. this.noext = !!opts.noext;
  10082. this.realpath = !!opts.realpath;
  10083. this.absolute = opts.absolute;
  10084. this.includeChildMatches = opts.includeChildMatches !== false;
  10085. this.noglobstar = !!opts.noglobstar;
  10086. this.matchBase = !!opts.matchBase;
  10087. this.maxDepth =
  10088. typeof opts.maxDepth === 'number' ? opts.maxDepth : Infinity;
  10089. this.stat = !!opts.stat;
  10090. this.ignore = opts.ignore;
  10091. if (this.withFileTypes && this.absolute !== undefined) {
  10092. throw new Error('cannot set absolute and withFileTypes:true');
  10093. }
  10094. if (typeof pattern === 'string') {
  10095. pattern = [pattern];
  10096. }
  10097. this.windowsPathsNoEscape =
  10098. !!opts.windowsPathsNoEscape ||
  10099. opts.allowWindowsEscape ===
  10100. false;
  10101. if (this.windowsPathsNoEscape) {
  10102. pattern = pattern.map(p => p.replace(/\\/g, '/'));
  10103. }
  10104. if (this.matchBase) {
  10105. if (opts.noglobstar) {
  10106. throw new TypeError('base matching requires globstar');
  10107. }
  10108. pattern = pattern.map(p => (p.includes('/') ? p : `./**/${p}`));
  10109. }
  10110. this.pattern = pattern;
  10111. this.platform = opts.platform || defaultPlatform;
  10112. this.opts = { ...opts, platform: this.platform };
  10113. if (opts.scurry) {
  10114. this.scurry = opts.scurry;
  10115. if (opts.nocase !== undefined &&
  10116. opts.nocase !== opts.scurry.nocase) {
  10117. throw new Error('nocase option contradicts provided scurry option');
  10118. }
  10119. }
  10120. else {
  10121. const Scurry = opts.platform === 'win32' ? PathScurryWin32
  10122. : opts.platform === 'darwin' ? PathScurryDarwin
  10123. : opts.platform ? PathScurryPosix
  10124. : PathScurry;
  10125. this.scurry = new Scurry(this.cwd, {
  10126. nocase: opts.nocase,
  10127. fs: opts.fs,
  10128. });
  10129. }
  10130. this.nocase = this.scurry.nocase;
  10131. // If you do nocase:true on a case-sensitive file system, then
  10132. // we need to use regexps instead of strings for non-magic
  10133. // path portions, because statting `aBc` won't return results
  10134. // for the file `AbC` for example.
  10135. const nocaseMagicOnly = this.platform === 'darwin' || this.platform === 'win32';
  10136. const mmo = {
  10137. // default nocase based on platform
  10138. ...opts,
  10139. dot: this.dot,
  10140. matchBase: this.matchBase,
  10141. nobrace: this.nobrace,
  10142. nocase: this.nocase,
  10143. nocaseMagicOnly,
  10144. nocomment: true,
  10145. noext: this.noext,
  10146. nonegate: true,
  10147. optimizationLevel: 2,
  10148. platform: this.platform,
  10149. windowsPathsNoEscape: this.windowsPathsNoEscape,
  10150. debug: !!this.opts.debug,
  10151. };
  10152. const mms = this.pattern.map(p => new Minimatch(p, mmo));
  10153. const [matchSet, globParts] = mms.reduce((set, m) => {
  10154. set[0].push(...m.set);
  10155. set[1].push(...m.globParts);
  10156. return set;
  10157. }, [[], []]);
  10158. this.patterns = matchSet.map((set, i) => {
  10159. const g = globParts[i];
  10160. /* c8 ignore start */
  10161. if (!g)
  10162. throw new Error('invalid pattern object');
  10163. /* c8 ignore stop */
  10164. return new Pattern(set, g, 0, this.platform);
  10165. });
  10166. }
  10167. async walk() {
  10168. // Walkers always return array of Path objects, so we just have to
  10169. // coerce them into the right shape. It will have already called
  10170. // realpath() if the option was set to do so, so we know that's cached.
  10171. // start out knowing the cwd, at least
  10172. return [
  10173. ...(await new GlobWalker(this.patterns, this.scurry.cwd, {
  10174. ...this.opts,
  10175. maxDepth: this.maxDepth !== Infinity ?
  10176. this.maxDepth + this.scurry.cwd.depth()
  10177. : Infinity,
  10178. platform: this.platform,
  10179. nocase: this.nocase,
  10180. includeChildMatches: this.includeChildMatches,
  10181. }).walk()),
  10182. ];
  10183. }
  10184. walkSync() {
  10185. return [
  10186. ...new GlobWalker(this.patterns, this.scurry.cwd, {
  10187. ...this.opts,
  10188. maxDepth: this.maxDepth !== Infinity ?
  10189. this.maxDepth + this.scurry.cwd.depth()
  10190. : Infinity,
  10191. platform: this.platform,
  10192. nocase: this.nocase,
  10193. includeChildMatches: this.includeChildMatches,
  10194. }).walkSync(),
  10195. ];
  10196. }
  10197. stream() {
  10198. return new GlobStream(this.patterns, this.scurry.cwd, {
  10199. ...this.opts,
  10200. maxDepth: this.maxDepth !== Infinity ?
  10201. this.maxDepth + this.scurry.cwd.depth()
  10202. : Infinity,
  10203. platform: this.platform,
  10204. nocase: this.nocase,
  10205. includeChildMatches: this.includeChildMatches,
  10206. }).stream();
  10207. }
  10208. streamSync() {
  10209. return new GlobStream(this.patterns, this.scurry.cwd, {
  10210. ...this.opts,
  10211. maxDepth: this.maxDepth !== Infinity ?
  10212. this.maxDepth + this.scurry.cwd.depth()
  10213. : Infinity,
  10214. platform: this.platform,
  10215. nocase: this.nocase,
  10216. includeChildMatches: this.includeChildMatches,
  10217. }).streamSync();
  10218. }
  10219. /**
  10220. * Default sync iteration function. Returns a Generator that
  10221. * iterates over the results.
  10222. */
  10223. iterateSync() {
  10224. return this.streamSync()[Symbol.iterator]();
  10225. }
  10226. [Symbol.iterator]() {
  10227. return this.iterateSync();
  10228. }
  10229. /**
  10230. * Default async iteration function. Returns an AsyncGenerator that
  10231. * iterates over the results.
  10232. */
  10233. iterate() {
  10234. return this.stream()[Symbol.asyncIterator]();
  10235. }
  10236. [Symbol.asyncIterator]() {
  10237. return this.iterate();
  10238. }
  10239. }
  10240. /**
  10241. * Return true if the patterns provided contain any magic glob characters,
  10242. * given the options provided.
  10243. *
  10244. * Brace expansion is not considered "magic" unless the `magicalBraces` option
  10245. * is set, as brace expansion just turns one string into an array of strings.
  10246. * So a pattern like `'x{a,b}y'` would return `false`, because `'xay'` and
  10247. * `'xby'` both do not contain any magic glob characters, and it's treated the
  10248. * same as if you had called it on `['xay', 'xby']`. When `magicalBraces:true`
  10249. * is in the options, brace expansion _is_ treated as a pattern having magic.
  10250. */
  10251. const hasMagic = (pattern, options = {}) => {
  10252. if (!Array.isArray(pattern)) {
  10253. pattern = [pattern];
  10254. }
  10255. for (const p of pattern) {
  10256. if (new Minimatch(p, options).hasMagic())
  10257. return true;
  10258. }
  10259. return false;
  10260. };
  10261. function globStreamSync(pattern, options = {}) {
  10262. return new Glob(pattern, options).streamSync();
  10263. }
  10264. function globStream(pattern, options = {}) {
  10265. return new Glob(pattern, options).stream();
  10266. }
  10267. function globSync(pattern, options = {}) {
  10268. return new Glob(pattern, options).walkSync();
  10269. }
  10270. async function glob_(pattern, options = {}) {
  10271. return new Glob(pattern, options).walk();
  10272. }
  10273. function globIterateSync(pattern, options = {}) {
  10274. return new Glob(pattern, options).iterateSync();
  10275. }
  10276. function globIterate(pattern, options = {}) {
  10277. return new Glob(pattern, options).iterate();
  10278. }
  10279. // aliases: glob.sync.stream() glob.stream.sync() glob.sync() etc
  10280. const streamSync = globStreamSync;
  10281. const stream$5 = Object.assign(globStream, { sync: globStreamSync });
  10282. const iterateSync = globIterateSync;
  10283. const iterate = Object.assign(globIterate, {
  10284. sync: globIterateSync,
  10285. });
  10286. const sync$9 = Object.assign(globSync, {
  10287. stream: globStreamSync,
  10288. iterate: globIterateSync,
  10289. });
  10290. const glob$1 = Object.assign(glob_, {
  10291. glob: glob_,
  10292. globSync,
  10293. sync: sync$9,
  10294. globStream,
  10295. stream: stream$5,
  10296. globStreamSync,
  10297. streamSync,
  10298. globIterate,
  10299. iterate,
  10300. globIterateSync,
  10301. iterateSync,
  10302. Glob,
  10303. hasMagic,
  10304. escape: escape$2,
  10305. unescape: unescape$1,
  10306. });
  10307. glob$1.glob = glob$1;
  10308. const comma = ','.charCodeAt(0);
  10309. const semicolon = ';'.charCodeAt(0);
  10310. const chars$1 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
  10311. const intToChar = new Uint8Array(64); // 64 possible chars.
  10312. const charToInt = new Uint8Array(128); // z is 122 in ASCII
  10313. for (let i = 0; i < chars$1.length; i++) {
  10314. const c = chars$1.charCodeAt(i);
  10315. intToChar[i] = c;
  10316. charToInt[c] = i;
  10317. }
  10318. function decodeInteger(reader, relative) {
  10319. let value = 0;
  10320. let shift = 0;
  10321. let integer = 0;
  10322. do {
  10323. const c = reader.next();
  10324. integer = charToInt[c];
  10325. value |= (integer & 31) << shift;
  10326. shift += 5;
  10327. } while (integer & 32);
  10328. const shouldNegate = value & 1;
  10329. value >>>= 1;
  10330. if (shouldNegate) {
  10331. value = -0x80000000 | -value;
  10332. }
  10333. return relative + value;
  10334. }
  10335. function encodeInteger(builder, num, relative) {
  10336. let delta = num - relative;
  10337. delta = delta < 0 ? (-delta << 1) | 1 : delta << 1;
  10338. do {
  10339. let clamped = delta & 0b011111;
  10340. delta >>>= 5;
  10341. if (delta > 0)
  10342. clamped |= 0b100000;
  10343. builder.write(intToChar[clamped]);
  10344. } while (delta > 0);
  10345. return num;
  10346. }
  10347. function hasMoreVlq(reader, max) {
  10348. if (reader.pos >= max)
  10349. return false;
  10350. return reader.peek() !== comma;
  10351. }
  10352. const bufLength = 1024 * 16;
  10353. // Provide a fallback for older environments.
  10354. const td = typeof TextDecoder !== 'undefined'
  10355. ? /* #__PURE__ */ new TextDecoder()
  10356. : typeof Buffer !== 'undefined'
  10357. ? {
  10358. decode(buf) {
  10359. const out = Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength);
  10360. return out.toString();
  10361. },
  10362. }
  10363. : {
  10364. decode(buf) {
  10365. let out = '';
  10366. for (let i = 0; i < buf.length; i++) {
  10367. out += String.fromCharCode(buf[i]);
  10368. }
  10369. return out;
  10370. },
  10371. };
  10372. class StringWriter {
  10373. constructor() {
  10374. this.pos = 0;
  10375. this.out = '';
  10376. this.buffer = new Uint8Array(bufLength);
  10377. }
  10378. write(v) {
  10379. const { buffer } = this;
  10380. buffer[this.pos++] = v;
  10381. if (this.pos === bufLength) {
  10382. this.out += td.decode(buffer);
  10383. this.pos = 0;
  10384. }
  10385. }
  10386. flush() {
  10387. const { buffer, out, pos } = this;
  10388. return pos > 0 ? out + td.decode(buffer.subarray(0, pos)) : out;
  10389. }
  10390. }
  10391. class StringReader {
  10392. constructor(buffer) {
  10393. this.pos = 0;
  10394. this.buffer = buffer;
  10395. }
  10396. next() {
  10397. return this.buffer.charCodeAt(this.pos++);
  10398. }
  10399. peek() {
  10400. return this.buffer.charCodeAt(this.pos);
  10401. }
  10402. indexOf(char) {
  10403. const { buffer, pos } = this;
  10404. const idx = buffer.indexOf(char, pos);
  10405. return idx === -1 ? buffer.length : idx;
  10406. }
  10407. }
  10408. function decode(mappings) {
  10409. const { length } = mappings;
  10410. const reader = new StringReader(mappings);
  10411. const decoded = [];
  10412. let genColumn = 0;
  10413. let sourcesIndex = 0;
  10414. let sourceLine = 0;
  10415. let sourceColumn = 0;
  10416. let namesIndex = 0;
  10417. do {
  10418. const semi = reader.indexOf(';');
  10419. const line = [];
  10420. let sorted = true;
  10421. let lastCol = 0;
  10422. genColumn = 0;
  10423. while (reader.pos < semi) {
  10424. let seg;
  10425. genColumn = decodeInteger(reader, genColumn);
  10426. if (genColumn < lastCol)
  10427. sorted = false;
  10428. lastCol = genColumn;
  10429. if (hasMoreVlq(reader, semi)) {
  10430. sourcesIndex = decodeInteger(reader, sourcesIndex);
  10431. sourceLine = decodeInteger(reader, sourceLine);
  10432. sourceColumn = decodeInteger(reader, sourceColumn);
  10433. if (hasMoreVlq(reader, semi)) {
  10434. namesIndex = decodeInteger(reader, namesIndex);
  10435. seg = [genColumn, sourcesIndex, sourceLine, sourceColumn, namesIndex];
  10436. }
  10437. else {
  10438. seg = [genColumn, sourcesIndex, sourceLine, sourceColumn];
  10439. }
  10440. }
  10441. else {
  10442. seg = [genColumn];
  10443. }
  10444. line.push(seg);
  10445. reader.pos++;
  10446. }
  10447. if (!sorted)
  10448. sort(line);
  10449. decoded.push(line);
  10450. reader.pos = semi + 1;
  10451. } while (reader.pos <= length);
  10452. return decoded;
  10453. }
  10454. function sort(line) {
  10455. line.sort(sortComparator$1);
  10456. }
  10457. function sortComparator$1(a, b) {
  10458. return a[0] - b[0];
  10459. }
  10460. function encode$1(decoded) {
  10461. const writer = new StringWriter();
  10462. let sourcesIndex = 0;
  10463. let sourceLine = 0;
  10464. let sourceColumn = 0;
  10465. let namesIndex = 0;
  10466. for (let i = 0; i < decoded.length; i++) {
  10467. const line = decoded[i];
  10468. if (i > 0)
  10469. writer.write(semicolon);
  10470. if (line.length === 0)
  10471. continue;
  10472. let genColumn = 0;
  10473. for (let j = 0; j < line.length; j++) {
  10474. const segment = line[j];
  10475. if (j > 0)
  10476. writer.write(comma);
  10477. genColumn = encodeInteger(writer, segment[0], genColumn);
  10478. if (segment.length === 1)
  10479. continue;
  10480. sourcesIndex = encodeInteger(writer, segment[1], sourcesIndex);
  10481. sourceLine = encodeInteger(writer, segment[2], sourceLine);
  10482. sourceColumn = encodeInteger(writer, segment[3], sourceColumn);
  10483. if (segment.length === 4)
  10484. continue;
  10485. namesIndex = encodeInteger(writer, segment[4], namesIndex);
  10486. }
  10487. }
  10488. return writer.flush();
  10489. }
  10490. class BitSet {
  10491. constructor(arg) {
  10492. this.bits = arg instanceof BitSet ? arg.bits.slice() : [];
  10493. }
  10494. add(n) {
  10495. this.bits[n >> 5] |= 1 << (n & 31);
  10496. }
  10497. has(n) {
  10498. return !!(this.bits[n >> 5] & (1 << (n & 31)));
  10499. }
  10500. }
  10501. class Chunk {
  10502. constructor(start, end, content) {
  10503. this.start = start;
  10504. this.end = end;
  10505. this.original = content;
  10506. this.intro = '';
  10507. this.outro = '';
  10508. this.content = content;
  10509. this.storeName = false;
  10510. this.edited = false;
  10511. {
  10512. this.previous = null;
  10513. this.next = null;
  10514. }
  10515. }
  10516. appendLeft(content) {
  10517. this.outro += content;
  10518. }
  10519. appendRight(content) {
  10520. this.intro = this.intro + content;
  10521. }
  10522. clone() {
  10523. const chunk = new Chunk(this.start, this.end, this.original);
  10524. chunk.intro = this.intro;
  10525. chunk.outro = this.outro;
  10526. chunk.content = this.content;
  10527. chunk.storeName = this.storeName;
  10528. chunk.edited = this.edited;
  10529. return chunk;
  10530. }
  10531. contains(index) {
  10532. return this.start < index && index < this.end;
  10533. }
  10534. eachNext(fn) {
  10535. let chunk = this;
  10536. while (chunk) {
  10537. fn(chunk);
  10538. chunk = chunk.next;
  10539. }
  10540. }
  10541. eachPrevious(fn) {
  10542. let chunk = this;
  10543. while (chunk) {
  10544. fn(chunk);
  10545. chunk = chunk.previous;
  10546. }
  10547. }
  10548. edit(content, storeName, contentOnly) {
  10549. this.content = content;
  10550. if (!contentOnly) {
  10551. this.intro = '';
  10552. this.outro = '';
  10553. }
  10554. this.storeName = storeName;
  10555. this.edited = true;
  10556. return this;
  10557. }
  10558. prependLeft(content) {
  10559. this.outro = content + this.outro;
  10560. }
  10561. prependRight(content) {
  10562. this.intro = content + this.intro;
  10563. }
  10564. reset() {
  10565. this.intro = '';
  10566. this.outro = '';
  10567. if (this.edited) {
  10568. this.content = this.original;
  10569. this.storeName = false;
  10570. this.edited = false;
  10571. }
  10572. }
  10573. split(index) {
  10574. const sliceIndex = index - this.start;
  10575. const originalBefore = this.original.slice(0, sliceIndex);
  10576. const originalAfter = this.original.slice(sliceIndex);
  10577. this.original = originalBefore;
  10578. const newChunk = new Chunk(index, this.end, originalAfter);
  10579. newChunk.outro = this.outro;
  10580. this.outro = '';
  10581. this.end = index;
  10582. if (this.edited) {
  10583. // after split we should save the edit content record into the correct chunk
  10584. // to make sure sourcemap correct
  10585. // For example:
  10586. // ' test'.trim()
  10587. // split -> ' ' + 'test'
  10588. // ✔️ edit -> '' + 'test'
  10589. // ✖️ edit -> 'test' + ''
  10590. // TODO is this block necessary?...
  10591. newChunk.edit('', false);
  10592. this.content = '';
  10593. } else {
  10594. this.content = originalBefore;
  10595. }
  10596. newChunk.next = this.next;
  10597. if (newChunk.next) newChunk.next.previous = newChunk;
  10598. newChunk.previous = this;
  10599. this.next = newChunk;
  10600. return newChunk;
  10601. }
  10602. toString() {
  10603. return this.intro + this.content + this.outro;
  10604. }
  10605. trimEnd(rx) {
  10606. this.outro = this.outro.replace(rx, '');
  10607. if (this.outro.length) return true;
  10608. const trimmed = this.content.replace(rx, '');
  10609. if (trimmed.length) {
  10610. if (trimmed !== this.content) {
  10611. this.split(this.start + trimmed.length).edit('', undefined, true);
  10612. if (this.edited) {
  10613. // save the change, if it has been edited
  10614. this.edit(trimmed, this.storeName, true);
  10615. }
  10616. }
  10617. return true;
  10618. } else {
  10619. this.edit('', undefined, true);
  10620. this.intro = this.intro.replace(rx, '');
  10621. if (this.intro.length) return true;
  10622. }
  10623. }
  10624. trimStart(rx) {
  10625. this.intro = this.intro.replace(rx, '');
  10626. if (this.intro.length) return true;
  10627. const trimmed = this.content.replace(rx, '');
  10628. if (trimmed.length) {
  10629. if (trimmed !== this.content) {
  10630. const newChunk = this.split(this.end - trimmed.length);
  10631. if (this.edited) {
  10632. // save the change, if it has been edited
  10633. newChunk.edit(trimmed, this.storeName, true);
  10634. }
  10635. this.edit('', undefined, true);
  10636. }
  10637. return true;
  10638. } else {
  10639. this.edit('', undefined, true);
  10640. this.outro = this.outro.replace(rx, '');
  10641. if (this.outro.length) return true;
  10642. }
  10643. }
  10644. }
  10645. function getBtoa() {
  10646. if (typeof globalThis !== 'undefined' && typeof globalThis.btoa === 'function') {
  10647. return (str) => globalThis.btoa(unescape(encodeURIComponent(str)));
  10648. } else if (typeof Buffer === 'function') {
  10649. return (str) => Buffer.from(str, 'utf-8').toString('base64');
  10650. } else {
  10651. return () => {
  10652. throw new Error('Unsupported environment: `window.btoa` or `Buffer` should be supported.');
  10653. };
  10654. }
  10655. }
  10656. const btoa$1 = /*#__PURE__*/ getBtoa();
  10657. let SourceMap$1 = class SourceMap {
  10658. constructor(properties) {
  10659. this.version = 3;
  10660. this.file = properties.file;
  10661. this.sources = properties.sources;
  10662. this.sourcesContent = properties.sourcesContent;
  10663. this.names = properties.names;
  10664. this.mappings = encode$1(properties.mappings);
  10665. if (typeof properties.x_google_ignoreList !== 'undefined') {
  10666. this.x_google_ignoreList = properties.x_google_ignoreList;
  10667. }
  10668. }
  10669. toString() {
  10670. return JSON.stringify(this);
  10671. }
  10672. toUrl() {
  10673. return 'data:application/json;charset=utf-8;base64,' + btoa$1(this.toString());
  10674. }
  10675. };
  10676. function guessIndent(code) {
  10677. const lines = code.split('\n');
  10678. const tabbed = lines.filter((line) => /^\t+/.test(line));
  10679. const spaced = lines.filter((line) => /^ {2,}/.test(line));
  10680. if (tabbed.length === 0 && spaced.length === 0) {
  10681. return null;
  10682. }
  10683. // More lines tabbed than spaced? Assume tabs, and
  10684. // default to tabs in the case of a tie (or nothing
  10685. // to go on)
  10686. if (tabbed.length >= spaced.length) {
  10687. return '\t';
  10688. }
  10689. // Otherwise, we need to guess the multiple
  10690. const min = spaced.reduce((previous, current) => {
  10691. const numSpaces = /^ +/.exec(current)[0].length;
  10692. return Math.min(numSpaces, previous);
  10693. }, Infinity);
  10694. return new Array(min + 1).join(' ');
  10695. }
  10696. function getRelativePath(from, to) {
  10697. const fromParts = from.split(/[/\\]/);
  10698. const toParts = to.split(/[/\\]/);
  10699. fromParts.pop(); // get dirname
  10700. while (fromParts[0] === toParts[0]) {
  10701. fromParts.shift();
  10702. toParts.shift();
  10703. }
  10704. if (fromParts.length) {
  10705. let i = fromParts.length;
  10706. while (i--) fromParts[i] = '..';
  10707. }
  10708. return fromParts.concat(toParts).join('/');
  10709. }
  10710. const toString$1 = Object.prototype.toString;
  10711. function isObject$2(thing) {
  10712. return toString$1.call(thing) === '[object Object]';
  10713. }
  10714. function getLocator(source) {
  10715. const originalLines = source.split('\n');
  10716. const lineOffsets = [];
  10717. for (let i = 0, pos = 0; i < originalLines.length; i++) {
  10718. lineOffsets.push(pos);
  10719. pos += originalLines[i].length + 1;
  10720. }
  10721. return function locate(index) {
  10722. let i = 0;
  10723. let j = lineOffsets.length;
  10724. while (i < j) {
  10725. const m = (i + j) >> 1;
  10726. if (index < lineOffsets[m]) {
  10727. j = m;
  10728. } else {
  10729. i = m + 1;
  10730. }
  10731. }
  10732. const line = i - 1;
  10733. const column = index - lineOffsets[line];
  10734. return { line, column };
  10735. };
  10736. }
  10737. const wordRegex = /\w/;
  10738. class Mappings {
  10739. constructor(hires) {
  10740. this.hires = hires;
  10741. this.generatedCodeLine = 0;
  10742. this.generatedCodeColumn = 0;
  10743. this.raw = [];
  10744. this.rawSegments = this.raw[this.generatedCodeLine] = [];
  10745. this.pending = null;
  10746. }
  10747. addEdit(sourceIndex, content, loc, nameIndex) {
  10748. if (content.length) {
  10749. const contentLengthMinusOne = content.length - 1;
  10750. let contentLineEnd = content.indexOf('\n', 0);
  10751. let previousContentLineEnd = -1;
  10752. // Loop through each line in the content and add a segment, but stop if the last line is empty,
  10753. // else code afterwards would fill one line too many
  10754. while (contentLineEnd >= 0 && contentLengthMinusOne > contentLineEnd) {
  10755. const segment = [this.generatedCodeColumn, sourceIndex, loc.line, loc.column];
  10756. if (nameIndex >= 0) {
  10757. segment.push(nameIndex);
  10758. }
  10759. this.rawSegments.push(segment);
  10760. this.generatedCodeLine += 1;
  10761. this.raw[this.generatedCodeLine] = this.rawSegments = [];
  10762. this.generatedCodeColumn = 0;
  10763. previousContentLineEnd = contentLineEnd;
  10764. contentLineEnd = content.indexOf('\n', contentLineEnd + 1);
  10765. }
  10766. const segment = [this.generatedCodeColumn, sourceIndex, loc.line, loc.column];
  10767. if (nameIndex >= 0) {
  10768. segment.push(nameIndex);
  10769. }
  10770. this.rawSegments.push(segment);
  10771. this.advance(content.slice(previousContentLineEnd + 1));
  10772. } else if (this.pending) {
  10773. this.rawSegments.push(this.pending);
  10774. this.advance(content);
  10775. }
  10776. this.pending = null;
  10777. }
  10778. addUneditedChunk(sourceIndex, chunk, original, loc, sourcemapLocations) {
  10779. let originalCharIndex = chunk.start;
  10780. let first = true;
  10781. // when iterating each char, check if it's in a word boundary
  10782. let charInHiresBoundary = false;
  10783. while (originalCharIndex < chunk.end) {
  10784. if (this.hires || first || sourcemapLocations.has(originalCharIndex)) {
  10785. const segment = [this.generatedCodeColumn, sourceIndex, loc.line, loc.column];
  10786. if (this.hires === 'boundary') {
  10787. // in hires "boundary", group segments per word boundary than per char
  10788. if (wordRegex.test(original[originalCharIndex])) {
  10789. // for first char in the boundary found, start the boundary by pushing a segment
  10790. if (!charInHiresBoundary) {
  10791. this.rawSegments.push(segment);
  10792. charInHiresBoundary = true;
  10793. }
  10794. } else {
  10795. // for non-word char, end the boundary by pushing a segment
  10796. this.rawSegments.push(segment);
  10797. charInHiresBoundary = false;
  10798. }
  10799. } else {
  10800. this.rawSegments.push(segment);
  10801. }
  10802. }
  10803. if (original[originalCharIndex] === '\n') {
  10804. loc.line += 1;
  10805. loc.column = 0;
  10806. this.generatedCodeLine += 1;
  10807. this.raw[this.generatedCodeLine] = this.rawSegments = [];
  10808. this.generatedCodeColumn = 0;
  10809. first = true;
  10810. } else {
  10811. loc.column += 1;
  10812. this.generatedCodeColumn += 1;
  10813. first = false;
  10814. }
  10815. originalCharIndex += 1;
  10816. }
  10817. this.pending = null;
  10818. }
  10819. advance(str) {
  10820. if (!str) return;
  10821. const lines = str.split('\n');
  10822. if (lines.length > 1) {
  10823. for (let i = 0; i < lines.length - 1; i++) {
  10824. this.generatedCodeLine++;
  10825. this.raw[this.generatedCodeLine] = this.rawSegments = [];
  10826. }
  10827. this.generatedCodeColumn = 0;
  10828. }
  10829. this.generatedCodeColumn += lines[lines.length - 1].length;
  10830. }
  10831. }
  10832. const n$1 = '\n';
  10833. const warned = {
  10834. insertLeft: false,
  10835. insertRight: false,
  10836. storeName: false,
  10837. };
  10838. class MagicString {
  10839. constructor(string, options = {}) {
  10840. const chunk = new Chunk(0, string.length, string);
  10841. Object.defineProperties(this, {
  10842. original: { writable: true, value: string },
  10843. outro: { writable: true, value: '' },
  10844. intro: { writable: true, value: '' },
  10845. firstChunk: { writable: true, value: chunk },
  10846. lastChunk: { writable: true, value: chunk },
  10847. lastSearchedChunk: { writable: true, value: chunk },
  10848. byStart: { writable: true, value: {} },
  10849. byEnd: { writable: true, value: {} },
  10850. filename: { writable: true, value: options.filename },
  10851. indentExclusionRanges: { writable: true, value: options.indentExclusionRanges },
  10852. sourcemapLocations: { writable: true, value: new BitSet() },
  10853. storedNames: { writable: true, value: {} },
  10854. indentStr: { writable: true, value: undefined },
  10855. ignoreList: { writable: true, value: options.ignoreList },
  10856. });
  10857. this.byStart[0] = chunk;
  10858. this.byEnd[string.length] = chunk;
  10859. }
  10860. addSourcemapLocation(char) {
  10861. this.sourcemapLocations.add(char);
  10862. }
  10863. append(content) {
  10864. if (typeof content !== 'string') throw new TypeError('outro content must be a string');
  10865. this.outro += content;
  10866. return this;
  10867. }
  10868. appendLeft(index, content) {
  10869. if (typeof content !== 'string') throw new TypeError('inserted content must be a string');
  10870. this._split(index);
  10871. const chunk = this.byEnd[index];
  10872. if (chunk) {
  10873. chunk.appendLeft(content);
  10874. } else {
  10875. this.intro += content;
  10876. }
  10877. return this;
  10878. }
  10879. appendRight(index, content) {
  10880. if (typeof content !== 'string') throw new TypeError('inserted content must be a string');
  10881. this._split(index);
  10882. const chunk = this.byStart[index];
  10883. if (chunk) {
  10884. chunk.appendRight(content);
  10885. } else {
  10886. this.outro += content;
  10887. }
  10888. return this;
  10889. }
  10890. clone() {
  10891. const cloned = new MagicString(this.original, { filename: this.filename });
  10892. let originalChunk = this.firstChunk;
  10893. let clonedChunk = (cloned.firstChunk = cloned.lastSearchedChunk = originalChunk.clone());
  10894. while (originalChunk) {
  10895. cloned.byStart[clonedChunk.start] = clonedChunk;
  10896. cloned.byEnd[clonedChunk.end] = clonedChunk;
  10897. const nextOriginalChunk = originalChunk.next;
  10898. const nextClonedChunk = nextOriginalChunk && nextOriginalChunk.clone();
  10899. if (nextClonedChunk) {
  10900. clonedChunk.next = nextClonedChunk;
  10901. nextClonedChunk.previous = clonedChunk;
  10902. clonedChunk = nextClonedChunk;
  10903. }
  10904. originalChunk = nextOriginalChunk;
  10905. }
  10906. cloned.lastChunk = clonedChunk;
  10907. if (this.indentExclusionRanges) {
  10908. cloned.indentExclusionRanges = this.indentExclusionRanges.slice();
  10909. }
  10910. cloned.sourcemapLocations = new BitSet(this.sourcemapLocations);
  10911. cloned.intro = this.intro;
  10912. cloned.outro = this.outro;
  10913. return cloned;
  10914. }
  10915. generateDecodedMap(options) {
  10916. options = options || {};
  10917. const sourceIndex = 0;
  10918. const names = Object.keys(this.storedNames);
  10919. const mappings = new Mappings(options.hires);
  10920. const locate = getLocator(this.original);
  10921. if (this.intro) {
  10922. mappings.advance(this.intro);
  10923. }
  10924. this.firstChunk.eachNext((chunk) => {
  10925. const loc = locate(chunk.start);
  10926. if (chunk.intro.length) mappings.advance(chunk.intro);
  10927. if (chunk.edited) {
  10928. mappings.addEdit(
  10929. sourceIndex,
  10930. chunk.content,
  10931. loc,
  10932. chunk.storeName ? names.indexOf(chunk.original) : -1,
  10933. );
  10934. } else {
  10935. mappings.addUneditedChunk(sourceIndex, chunk, this.original, loc, this.sourcemapLocations);
  10936. }
  10937. if (chunk.outro.length) mappings.advance(chunk.outro);
  10938. });
  10939. return {
  10940. file: options.file ? options.file.split(/[/\\]/).pop() : undefined,
  10941. sources: [
  10942. options.source ? getRelativePath(options.file || '', options.source) : options.file || '',
  10943. ],
  10944. sourcesContent: options.includeContent ? [this.original] : undefined,
  10945. names,
  10946. mappings: mappings.raw,
  10947. x_google_ignoreList: this.ignoreList ? [sourceIndex] : undefined,
  10948. };
  10949. }
  10950. generateMap(options) {
  10951. return new SourceMap$1(this.generateDecodedMap(options));
  10952. }
  10953. _ensureindentStr() {
  10954. if (this.indentStr === undefined) {
  10955. this.indentStr = guessIndent(this.original);
  10956. }
  10957. }
  10958. _getRawIndentString() {
  10959. this._ensureindentStr();
  10960. return this.indentStr;
  10961. }
  10962. getIndentString() {
  10963. this._ensureindentStr();
  10964. return this.indentStr === null ? '\t' : this.indentStr;
  10965. }
  10966. indent(indentStr, options) {
  10967. const pattern = /^[^\r\n]/gm;
  10968. if (isObject$2(indentStr)) {
  10969. options = indentStr;
  10970. indentStr = undefined;
  10971. }
  10972. if (indentStr === undefined) {
  10973. this._ensureindentStr();
  10974. indentStr = this.indentStr || '\t';
  10975. }
  10976. if (indentStr === '') return this; // noop
  10977. options = options || {};
  10978. // Process exclusion ranges
  10979. const isExcluded = {};
  10980. if (options.exclude) {
  10981. const exclusions =
  10982. typeof options.exclude[0] === 'number' ? [options.exclude] : options.exclude;
  10983. exclusions.forEach((exclusion) => {
  10984. for (let i = exclusion[0]; i < exclusion[1]; i += 1) {
  10985. isExcluded[i] = true;
  10986. }
  10987. });
  10988. }
  10989. let shouldIndentNextCharacter = options.indentStart !== false;
  10990. const replacer = (match) => {
  10991. if (shouldIndentNextCharacter) return `${indentStr}${match}`;
  10992. shouldIndentNextCharacter = true;
  10993. return match;
  10994. };
  10995. this.intro = this.intro.replace(pattern, replacer);
  10996. let charIndex = 0;
  10997. let chunk = this.firstChunk;
  10998. while (chunk) {
  10999. const end = chunk.end;
  11000. if (chunk.edited) {
  11001. if (!isExcluded[charIndex]) {
  11002. chunk.content = chunk.content.replace(pattern, replacer);
  11003. if (chunk.content.length) {
  11004. shouldIndentNextCharacter = chunk.content[chunk.content.length - 1] === '\n';
  11005. }
  11006. }
  11007. } else {
  11008. charIndex = chunk.start;
  11009. while (charIndex < end) {
  11010. if (!isExcluded[charIndex]) {
  11011. const char = this.original[charIndex];
  11012. if (char === '\n') {
  11013. shouldIndentNextCharacter = true;
  11014. } else if (char !== '\r' && shouldIndentNextCharacter) {
  11015. shouldIndentNextCharacter = false;
  11016. if (charIndex === chunk.start) {
  11017. chunk.prependRight(indentStr);
  11018. } else {
  11019. this._splitChunk(chunk, charIndex);
  11020. chunk = chunk.next;
  11021. chunk.prependRight(indentStr);
  11022. }
  11023. }
  11024. }
  11025. charIndex += 1;
  11026. }
  11027. }
  11028. charIndex = chunk.end;
  11029. chunk = chunk.next;
  11030. }
  11031. this.outro = this.outro.replace(pattern, replacer);
  11032. return this;
  11033. }
  11034. insert() {
  11035. throw new Error(
  11036. 'magicString.insert(...) is deprecated. Use prependRight(...) or appendLeft(...)',
  11037. );
  11038. }
  11039. insertLeft(index, content) {
  11040. if (!warned.insertLeft) {
  11041. console.warn(
  11042. 'magicString.insertLeft(...) is deprecated. Use magicString.appendLeft(...) instead',
  11043. ); // eslint-disable-line no-console
  11044. warned.insertLeft = true;
  11045. }
  11046. return this.appendLeft(index, content);
  11047. }
  11048. insertRight(index, content) {
  11049. if (!warned.insertRight) {
  11050. console.warn(
  11051. 'magicString.insertRight(...) is deprecated. Use magicString.prependRight(...) instead',
  11052. ); // eslint-disable-line no-console
  11053. warned.insertRight = true;
  11054. }
  11055. return this.prependRight(index, content);
  11056. }
  11057. move(start, end, index) {
  11058. if (index >= start && index <= end) throw new Error('Cannot move a selection inside itself');
  11059. this._split(start);
  11060. this._split(end);
  11061. this._split(index);
  11062. const first = this.byStart[start];
  11063. const last = this.byEnd[end];
  11064. const oldLeft = first.previous;
  11065. const oldRight = last.next;
  11066. const newRight = this.byStart[index];
  11067. if (!newRight && last === this.lastChunk) return this;
  11068. const newLeft = newRight ? newRight.previous : this.lastChunk;
  11069. if (oldLeft) oldLeft.next = oldRight;
  11070. if (oldRight) oldRight.previous = oldLeft;
  11071. if (newLeft) newLeft.next = first;
  11072. if (newRight) newRight.previous = last;
  11073. if (!first.previous) this.firstChunk = last.next;
  11074. if (!last.next) {
  11075. this.lastChunk = first.previous;
  11076. this.lastChunk.next = null;
  11077. }
  11078. first.previous = newLeft;
  11079. last.next = newRight || null;
  11080. if (!newLeft) this.firstChunk = first;
  11081. if (!newRight) this.lastChunk = last;
  11082. return this;
  11083. }
  11084. overwrite(start, end, content, options) {
  11085. options = options || {};
  11086. return this.update(start, end, content, { ...options, overwrite: !options.contentOnly });
  11087. }
  11088. update(start, end, content, options) {
  11089. if (typeof content !== 'string') throw new TypeError('replacement content must be a string');
  11090. if (this.original.length !== 0) {
  11091. while (start < 0) start += this.original.length;
  11092. while (end < 0) end += this.original.length;
  11093. }
  11094. if (end > this.original.length) throw new Error('end is out of bounds');
  11095. if (start === end)
  11096. throw new Error(
  11097. 'Cannot overwrite a zero-length range – use appendLeft or prependRight instead',
  11098. );
  11099. this._split(start);
  11100. this._split(end);
  11101. if (options === true) {
  11102. if (!warned.storeName) {
  11103. console.warn(
  11104. 'The final argument to magicString.overwrite(...) should be an options object. See https://github.com/rich-harris/magic-string',
  11105. ); // eslint-disable-line no-console
  11106. warned.storeName = true;
  11107. }
  11108. options = { storeName: true };
  11109. }
  11110. const storeName = options !== undefined ? options.storeName : false;
  11111. const overwrite = options !== undefined ? options.overwrite : false;
  11112. if (storeName) {
  11113. const original = this.original.slice(start, end);
  11114. Object.defineProperty(this.storedNames, original, {
  11115. writable: true,
  11116. value: true,
  11117. enumerable: true,
  11118. });
  11119. }
  11120. const first = this.byStart[start];
  11121. const last = this.byEnd[end];
  11122. if (first) {
  11123. let chunk = first;
  11124. while (chunk !== last) {
  11125. if (chunk.next !== this.byStart[chunk.end]) {
  11126. throw new Error('Cannot overwrite across a split point');
  11127. }
  11128. chunk = chunk.next;
  11129. chunk.edit('', false);
  11130. }
  11131. first.edit(content, storeName, !overwrite);
  11132. } else {
  11133. // must be inserting at the end
  11134. const newChunk = new Chunk(start, end, '').edit(content, storeName);
  11135. // TODO last chunk in the array may not be the last chunk, if it's moved...
  11136. last.next = newChunk;
  11137. newChunk.previous = last;
  11138. }
  11139. return this;
  11140. }
  11141. prepend(content) {
  11142. if (typeof content !== 'string') throw new TypeError('outro content must be a string');
  11143. this.intro = content + this.intro;
  11144. return this;
  11145. }
  11146. prependLeft(index, content) {
  11147. if (typeof content !== 'string') throw new TypeError('inserted content must be a string');
  11148. this._split(index);
  11149. const chunk = this.byEnd[index];
  11150. if (chunk) {
  11151. chunk.prependLeft(content);
  11152. } else {
  11153. this.intro = content + this.intro;
  11154. }
  11155. return this;
  11156. }
  11157. prependRight(index, content) {
  11158. if (typeof content !== 'string') throw new TypeError('inserted content must be a string');
  11159. this._split(index);
  11160. const chunk = this.byStart[index];
  11161. if (chunk) {
  11162. chunk.prependRight(content);
  11163. } else {
  11164. this.outro = content + this.outro;
  11165. }
  11166. return this;
  11167. }
  11168. remove(start, end) {
  11169. if (this.original.length !== 0) {
  11170. while (start < 0) start += this.original.length;
  11171. while (end < 0) end += this.original.length;
  11172. }
  11173. if (start === end) return this;
  11174. if (start < 0 || end > this.original.length) throw new Error('Character is out of bounds');
  11175. if (start > end) throw new Error('end must be greater than start');
  11176. this._split(start);
  11177. this._split(end);
  11178. let chunk = this.byStart[start];
  11179. while (chunk) {
  11180. chunk.intro = '';
  11181. chunk.outro = '';
  11182. chunk.edit('');
  11183. chunk = end > chunk.end ? this.byStart[chunk.end] : null;
  11184. }
  11185. return this;
  11186. }
  11187. reset(start, end) {
  11188. if (this.original.length !== 0) {
  11189. while (start < 0) start += this.original.length;
  11190. while (end < 0) end += this.original.length;
  11191. }
  11192. if (start === end) return this;
  11193. if (start < 0 || end > this.original.length) throw new Error('Character is out of bounds');
  11194. if (start > end) throw new Error('end must be greater than start');
  11195. this._split(start);
  11196. this._split(end);
  11197. let chunk = this.byStart[start];
  11198. while (chunk) {
  11199. chunk.reset();
  11200. chunk = end > chunk.end ? this.byStart[chunk.end] : null;
  11201. }
  11202. return this;
  11203. }
  11204. lastChar() {
  11205. if (this.outro.length) return this.outro[this.outro.length - 1];
  11206. let chunk = this.lastChunk;
  11207. do {
  11208. if (chunk.outro.length) return chunk.outro[chunk.outro.length - 1];
  11209. if (chunk.content.length) return chunk.content[chunk.content.length - 1];
  11210. if (chunk.intro.length) return chunk.intro[chunk.intro.length - 1];
  11211. } while ((chunk = chunk.previous));
  11212. if (this.intro.length) return this.intro[this.intro.length - 1];
  11213. return '';
  11214. }
  11215. lastLine() {
  11216. let lineIndex = this.outro.lastIndexOf(n$1);
  11217. if (lineIndex !== -1) return this.outro.substr(lineIndex + 1);
  11218. let lineStr = this.outro;
  11219. let chunk = this.lastChunk;
  11220. do {
  11221. if (chunk.outro.length > 0) {
  11222. lineIndex = chunk.outro.lastIndexOf(n$1);
  11223. if (lineIndex !== -1) return chunk.outro.substr(lineIndex + 1) + lineStr;
  11224. lineStr = chunk.outro + lineStr;
  11225. }
  11226. if (chunk.content.length > 0) {
  11227. lineIndex = chunk.content.lastIndexOf(n$1);
  11228. if (lineIndex !== -1) return chunk.content.substr(lineIndex + 1) + lineStr;
  11229. lineStr = chunk.content + lineStr;
  11230. }
  11231. if (chunk.intro.length > 0) {
  11232. lineIndex = chunk.intro.lastIndexOf(n$1);
  11233. if (lineIndex !== -1) return chunk.intro.substr(lineIndex + 1) + lineStr;
  11234. lineStr = chunk.intro + lineStr;
  11235. }
  11236. } while ((chunk = chunk.previous));
  11237. lineIndex = this.intro.lastIndexOf(n$1);
  11238. if (lineIndex !== -1) return this.intro.substr(lineIndex + 1) + lineStr;
  11239. return this.intro + lineStr;
  11240. }
  11241. slice(start = 0, end = this.original.length) {
  11242. if (this.original.length !== 0) {
  11243. while (start < 0) start += this.original.length;
  11244. while (end < 0) end += this.original.length;
  11245. }
  11246. let result = '';
  11247. // find start chunk
  11248. let chunk = this.firstChunk;
  11249. while (chunk && (chunk.start > start || chunk.end <= start)) {
  11250. // found end chunk before start
  11251. if (chunk.start < end && chunk.end >= end) {
  11252. return result;
  11253. }
  11254. chunk = chunk.next;
  11255. }
  11256. if (chunk && chunk.edited && chunk.start !== start)
  11257. throw new Error(`Cannot use replaced character ${start} as slice start anchor.`);
  11258. const startChunk = chunk;
  11259. while (chunk) {
  11260. if (chunk.intro && (startChunk !== chunk || chunk.start === start)) {
  11261. result += chunk.intro;
  11262. }
  11263. const containsEnd = chunk.start < end && chunk.end >= end;
  11264. if (containsEnd && chunk.edited && chunk.end !== end)
  11265. throw new Error(`Cannot use replaced character ${end} as slice end anchor.`);
  11266. const sliceStart = startChunk === chunk ? start - chunk.start : 0;
  11267. const sliceEnd = containsEnd ? chunk.content.length + end - chunk.end : chunk.content.length;
  11268. result += chunk.content.slice(sliceStart, sliceEnd);
  11269. if (chunk.outro && (!containsEnd || chunk.end === end)) {
  11270. result += chunk.outro;
  11271. }
  11272. if (containsEnd) {
  11273. break;
  11274. }
  11275. chunk = chunk.next;
  11276. }
  11277. return result;
  11278. }
  11279. // TODO deprecate this? not really very useful
  11280. snip(start, end) {
  11281. const clone = this.clone();
  11282. clone.remove(0, start);
  11283. clone.remove(end, clone.original.length);
  11284. return clone;
  11285. }
  11286. _split(index) {
  11287. if (this.byStart[index] || this.byEnd[index]) return;
  11288. let chunk = this.lastSearchedChunk;
  11289. const searchForward = index > chunk.end;
  11290. while (chunk) {
  11291. if (chunk.contains(index)) return this._splitChunk(chunk, index);
  11292. chunk = searchForward ? this.byStart[chunk.end] : this.byEnd[chunk.start];
  11293. }
  11294. }
  11295. _splitChunk(chunk, index) {
  11296. if (chunk.edited && chunk.content.length) {
  11297. // zero-length edited chunks are a special case (overlapping replacements)
  11298. const loc = getLocator(this.original)(index);
  11299. throw new Error(
  11300. `Cannot split a chunk that has already been edited (${loc.line}:${loc.column} – "${chunk.original}")`,
  11301. );
  11302. }
  11303. const newChunk = chunk.split(index);
  11304. this.byEnd[index] = chunk;
  11305. this.byStart[index] = newChunk;
  11306. this.byEnd[newChunk.end] = newChunk;
  11307. if (chunk === this.lastChunk) this.lastChunk = newChunk;
  11308. this.lastSearchedChunk = chunk;
  11309. return true;
  11310. }
  11311. toString() {
  11312. let str = this.intro;
  11313. let chunk = this.firstChunk;
  11314. while (chunk) {
  11315. str += chunk.toString();
  11316. chunk = chunk.next;
  11317. }
  11318. return str + this.outro;
  11319. }
  11320. isEmpty() {
  11321. let chunk = this.firstChunk;
  11322. do {
  11323. if (
  11324. (chunk.intro.length && chunk.intro.trim()) ||
  11325. (chunk.content.length && chunk.content.trim()) ||
  11326. (chunk.outro.length && chunk.outro.trim())
  11327. )
  11328. return false;
  11329. } while ((chunk = chunk.next));
  11330. return true;
  11331. }
  11332. length() {
  11333. let chunk = this.firstChunk;
  11334. let length = 0;
  11335. do {
  11336. length += chunk.intro.length + chunk.content.length + chunk.outro.length;
  11337. } while ((chunk = chunk.next));
  11338. return length;
  11339. }
  11340. trimLines() {
  11341. return this.trim('[\\r\\n]');
  11342. }
  11343. trim(charType) {
  11344. return this.trimStart(charType).trimEnd(charType);
  11345. }
  11346. trimEndAborted(charType) {
  11347. const rx = new RegExp((charType || '\\s') + '+$');
  11348. this.outro = this.outro.replace(rx, '');
  11349. if (this.outro.length) return true;
  11350. let chunk = this.lastChunk;
  11351. do {
  11352. const end = chunk.end;
  11353. const aborted = chunk.trimEnd(rx);
  11354. // if chunk was trimmed, we have a new lastChunk
  11355. if (chunk.end !== end) {
  11356. if (this.lastChunk === chunk) {
  11357. this.lastChunk = chunk.next;
  11358. }
  11359. this.byEnd[chunk.end] = chunk;
  11360. this.byStart[chunk.next.start] = chunk.next;
  11361. this.byEnd[chunk.next.end] = chunk.next;
  11362. }
  11363. if (aborted) return true;
  11364. chunk = chunk.previous;
  11365. } while (chunk);
  11366. return false;
  11367. }
  11368. trimEnd(charType) {
  11369. this.trimEndAborted(charType);
  11370. return this;
  11371. }
  11372. trimStartAborted(charType) {
  11373. const rx = new RegExp('^' + (charType || '\\s') + '+');
  11374. this.intro = this.intro.replace(rx, '');
  11375. if (this.intro.length) return true;
  11376. let chunk = this.firstChunk;
  11377. do {
  11378. const end = chunk.end;
  11379. const aborted = chunk.trimStart(rx);
  11380. if (chunk.end !== end) {
  11381. // special case...
  11382. if (chunk === this.lastChunk) this.lastChunk = chunk.next;
  11383. this.byEnd[chunk.end] = chunk;
  11384. this.byStart[chunk.next.start] = chunk.next;
  11385. this.byEnd[chunk.next.end] = chunk.next;
  11386. }
  11387. if (aborted) return true;
  11388. chunk = chunk.next;
  11389. } while (chunk);
  11390. return false;
  11391. }
  11392. trimStart(charType) {
  11393. this.trimStartAborted(charType);
  11394. return this;
  11395. }
  11396. hasChanged() {
  11397. return this.original !== this.toString();
  11398. }
  11399. _replaceRegexp(searchValue, replacement) {
  11400. function getReplacement(match, str) {
  11401. if (typeof replacement === 'string') {
  11402. return replacement.replace(/\$(\$|&|\d+)/g, (_, i) => {
  11403. // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#specifying_a_string_as_a_parameter
  11404. if (i === '$') return '$';
  11405. if (i === '&') return match[0];
  11406. const num = +i;
  11407. if (num < match.length) return match[+i];
  11408. return `$${i}`;
  11409. });
  11410. } else {
  11411. return replacement(...match, match.index, str, match.groups);
  11412. }
  11413. }
  11414. function matchAll(re, str) {
  11415. let match;
  11416. const matches = [];
  11417. while ((match = re.exec(str))) {
  11418. matches.push(match);
  11419. }
  11420. return matches;
  11421. }
  11422. if (searchValue.global) {
  11423. const matches = matchAll(searchValue, this.original);
  11424. matches.forEach((match) => {
  11425. if (match.index != null) {
  11426. const replacement = getReplacement(match, this.original);
  11427. if (replacement !== match[0]) {
  11428. this.overwrite(
  11429. match.index,
  11430. match.index + match[0].length,
  11431. replacement
  11432. );
  11433. }
  11434. }
  11435. });
  11436. } else {
  11437. const match = this.original.match(searchValue);
  11438. if (match && match.index != null) {
  11439. const replacement = getReplacement(match, this.original);
  11440. if (replacement !== match[0]) {
  11441. this.overwrite(
  11442. match.index,
  11443. match.index + match[0].length,
  11444. replacement
  11445. );
  11446. }
  11447. }
  11448. }
  11449. return this;
  11450. }
  11451. _replaceString(string, replacement) {
  11452. const { original } = this;
  11453. const index = original.indexOf(string);
  11454. if (index !== -1) {
  11455. this.overwrite(index, index + string.length, replacement);
  11456. }
  11457. return this;
  11458. }
  11459. replace(searchValue, replacement) {
  11460. if (typeof searchValue === 'string') {
  11461. return this._replaceString(searchValue, replacement);
  11462. }
  11463. return this._replaceRegexp(searchValue, replacement);
  11464. }
  11465. _replaceAllString(string, replacement) {
  11466. const { original } = this;
  11467. const stringLength = string.length;
  11468. for (
  11469. let index = original.indexOf(string);
  11470. index !== -1;
  11471. index = original.indexOf(string, index + stringLength)
  11472. ) {
  11473. const previous = original.slice(index, index + stringLength);
  11474. if (previous !== replacement)
  11475. this.overwrite(index, index + stringLength, replacement);
  11476. }
  11477. return this;
  11478. }
  11479. replaceAll(searchValue, replacement) {
  11480. if (typeof searchValue === 'string') {
  11481. return this._replaceAllString(searchValue, replacement);
  11482. }
  11483. if (!searchValue.global) {
  11484. throw new TypeError(
  11485. 'MagicString.prototype.replaceAll called with a non-global RegExp argument',
  11486. );
  11487. }
  11488. return this._replaceRegexp(searchValue, replacement);
  11489. }
  11490. }
  11491. function isReference(node, parent) {
  11492. if (node.type === 'MemberExpression') {
  11493. return !node.computed && isReference(node.object, node);
  11494. }
  11495. if (node.type === 'Identifier') {
  11496. if (!parent)
  11497. return true;
  11498. switch (parent.type) {
  11499. // disregard `bar` in `foo.bar`
  11500. case 'MemberExpression': return parent.computed || node === parent.object;
  11501. // disregard the `foo` in `class {foo(){}}` but keep it in `class {[foo](){}}`
  11502. case 'MethodDefinition': return parent.computed;
  11503. // disregard the `foo` in `class {foo=bar}` but keep it in `class {[foo]=bar}` and `class {bar=foo}`
  11504. case 'FieldDefinition': return parent.computed || node === parent.value;
  11505. // disregard the `bar` in `{ bar: foo }`, but keep it in `{ [bar]: foo }`
  11506. case 'Property': return parent.computed || node === parent.value;
  11507. // disregard the `bar` in `export { foo as bar }` or
  11508. // the foo in `import { foo as bar }`
  11509. case 'ExportSpecifier':
  11510. case 'ImportSpecifier': return node === parent.local;
  11511. // disregard the `foo` in `foo: while (...) { ... break foo; ... continue foo;}`
  11512. case 'LabeledStatement':
  11513. case 'BreakStatement':
  11514. case 'ContinueStatement': return false;
  11515. default: return true;
  11516. }
  11517. }
  11518. return false;
  11519. }
  11520. var version$2 = "26.0.1";
  11521. var peerDependencies = {
  11522. rollup: "^2.68.0||^3.0.0||^4.0.0"
  11523. };
  11524. function tryParse(parse, code, id) {
  11525. try {
  11526. return parse(code, { allowReturnOutsideFunction: true });
  11527. } catch (err) {
  11528. err.message += ` in ${id}`;
  11529. throw err;
  11530. }
  11531. }
  11532. const firstpassGlobal = /\b(?:require|module|exports|global)\b/;
  11533. const firstpassNoGlobal = /\b(?:require|module|exports)\b/;
  11534. function hasCjsKeywords(code, ignoreGlobal) {
  11535. const firstpass = ignoreGlobal ? firstpassNoGlobal : firstpassGlobal;
  11536. return firstpass.test(code);
  11537. }
  11538. /* eslint-disable no-underscore-dangle */
  11539. function analyzeTopLevelStatements(parse, code, id) {
  11540. const ast = tryParse(parse, code, id);
  11541. let isEsModule = false;
  11542. let hasDefaultExport = false;
  11543. let hasNamedExports = false;
  11544. for (const node of ast.body) {
  11545. switch (node.type) {
  11546. case 'ExportDefaultDeclaration':
  11547. isEsModule = true;
  11548. hasDefaultExport = true;
  11549. break;
  11550. case 'ExportNamedDeclaration':
  11551. isEsModule = true;
  11552. if (node.declaration) {
  11553. hasNamedExports = true;
  11554. } else {
  11555. for (const specifier of node.specifiers) {
  11556. if (specifier.exported.name === 'default') {
  11557. hasDefaultExport = true;
  11558. } else {
  11559. hasNamedExports = true;
  11560. }
  11561. }
  11562. }
  11563. break;
  11564. case 'ExportAllDeclaration':
  11565. isEsModule = true;
  11566. if (node.exported && node.exported.name === 'default') {
  11567. hasDefaultExport = true;
  11568. } else {
  11569. hasNamedExports = true;
  11570. }
  11571. break;
  11572. case 'ImportDeclaration':
  11573. isEsModule = true;
  11574. break;
  11575. }
  11576. }
  11577. return { isEsModule, hasDefaultExport, hasNamedExports, ast };
  11578. }
  11579. /* eslint-disable import/prefer-default-export */
  11580. function deconflict(scopes, globals, identifier) {
  11581. let i = 1;
  11582. let deconflicted = makeLegalIdentifier(identifier);
  11583. const hasConflicts = () =>
  11584. scopes.some((scope) => scope.contains(deconflicted)) || globals.has(deconflicted);
  11585. while (hasConflicts()) {
  11586. deconflicted = makeLegalIdentifier(`${identifier}_${i}`);
  11587. i += 1;
  11588. }
  11589. for (const scope of scopes) {
  11590. scope.declarations[deconflicted] = true;
  11591. }
  11592. return deconflicted;
  11593. }
  11594. function getName(id) {
  11595. const name = makeLegalIdentifier(basename$1(id, extname(id)));
  11596. if (name !== 'index') {
  11597. return name;
  11598. }
  11599. return makeLegalIdentifier(basename$1(dirname$1(id)));
  11600. }
  11601. function normalizePathSlashes(path) {
  11602. return path.replace(/\\/g, '/');
  11603. }
  11604. const getVirtualPathForDynamicRequirePath = (path, commonDir) =>
  11605. `/${normalizePathSlashes(relative$1(commonDir, path))}`;
  11606. function capitalize(name) {
  11607. return name[0].toUpperCase() + name.slice(1);
  11608. }
  11609. function getStrictRequiresFilter({ strictRequires }) {
  11610. switch (strictRequires) {
  11611. case true:
  11612. return { strictRequiresFilter: () => true, detectCyclesAndConditional: false };
  11613. // eslint-disable-next-line no-undefined
  11614. case undefined:
  11615. case 'auto':
  11616. case 'debug':
  11617. case null:
  11618. return { strictRequiresFilter: () => false, detectCyclesAndConditional: true };
  11619. case false:
  11620. return { strictRequiresFilter: () => false, detectCyclesAndConditional: false };
  11621. default:
  11622. if (typeof strictRequires === 'string' || Array.isArray(strictRequires)) {
  11623. return {
  11624. strictRequiresFilter: createFilter$1(strictRequires),
  11625. detectCyclesAndConditional: false
  11626. };
  11627. }
  11628. throw new Error('Unexpected value for "strictRequires" option.');
  11629. }
  11630. }
  11631. function getPackageEntryPoint(dirPath) {
  11632. let entryPoint = 'index.js';
  11633. try {
  11634. if (existsSync(join$1(dirPath, 'package.json'))) {
  11635. entryPoint =
  11636. JSON.parse(readFileSync(join$1(dirPath, 'package.json'), { encoding: 'utf8' })).main ||
  11637. entryPoint;
  11638. }
  11639. } catch (ignored) {
  11640. // ignored
  11641. }
  11642. return entryPoint;
  11643. }
  11644. function isDirectory$1(path) {
  11645. try {
  11646. if (statSync$1(path).isDirectory()) return true;
  11647. } catch (ignored) {
  11648. // Nothing to do here
  11649. }
  11650. return false;
  11651. }
  11652. function getDynamicRequireModules(patterns, dynamicRequireRoot) {
  11653. const dynamicRequireModules = new Map();
  11654. const dirNames = new Set();
  11655. for (const pattern of !patterns || Array.isArray(patterns) ? patterns || [] : [patterns]) {
  11656. const isNegated = pattern.startsWith('!');
  11657. const modifyMap = (targetPath, resolvedPath) =>
  11658. isNegated
  11659. ? dynamicRequireModules.delete(targetPath)
  11660. : dynamicRequireModules.set(targetPath, resolvedPath);
  11661. for (const path of glob$1
  11662. .sync(isNegated ? pattern.substr(1) : pattern)
  11663. .sort((a, b) => a.localeCompare(b, 'en'))) {
  11664. const resolvedPath = resolve$3(path);
  11665. const requirePath = normalizePathSlashes(resolvedPath);
  11666. if (isDirectory$1(resolvedPath)) {
  11667. dirNames.add(resolvedPath);
  11668. const modulePath = resolve$3(join$1(resolvedPath, getPackageEntryPoint(path)));
  11669. modifyMap(requirePath, modulePath);
  11670. modifyMap(normalizePathSlashes(modulePath), modulePath);
  11671. } else {
  11672. dirNames.add(dirname$1(resolvedPath));
  11673. modifyMap(requirePath, resolvedPath);
  11674. }
  11675. }
  11676. }
  11677. return {
  11678. commonDir: dirNames.size ? getCommonDir([...dirNames, dynamicRequireRoot]) : null,
  11679. dynamicRequireModules
  11680. };
  11681. }
  11682. 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.');`;
  11683. const COMMONJS_REQUIRE_EXPORT = 'commonjsRequire';
  11684. const CREATE_COMMONJS_REQUIRE_EXPORT = 'createCommonjsRequire';
  11685. function getDynamicModuleRegistry(
  11686. isDynamicRequireModulesEnabled,
  11687. dynamicRequireModules,
  11688. commonDir,
  11689. ignoreDynamicRequires
  11690. ) {
  11691. if (!isDynamicRequireModulesEnabled) {
  11692. return `export function ${COMMONJS_REQUIRE_EXPORT}(path) {
  11693. ${FAILED_REQUIRE_ERROR}
  11694. }`;
  11695. }
  11696. const dynamicModuleImports = [...dynamicRequireModules.values()]
  11697. .map(
  11698. (id, index) =>
  11699. `import ${
  11700. id.endsWith('.json') ? `json${index}` : `{ __require as require${index} }`
  11701. } from ${JSON.stringify(id)};`
  11702. )
  11703. .join('\n');
  11704. const dynamicModuleProps = [...dynamicRequireModules.keys()]
  11705. .map(
  11706. (id, index) =>
  11707. `\t\t${JSON.stringify(getVirtualPathForDynamicRequirePath(id, commonDir))}: ${
  11708. id.endsWith('.json') ? `function () { return json${index}; }` : `require${index}`
  11709. }`
  11710. )
  11711. .join(',\n');
  11712. return `${dynamicModuleImports}
  11713. var dynamicModules;
  11714. function getDynamicModules() {
  11715. return dynamicModules || (dynamicModules = {
  11716. ${dynamicModuleProps}
  11717. });
  11718. }
  11719. export function ${CREATE_COMMONJS_REQUIRE_EXPORT}(originalModuleDir) {
  11720. function handleRequire(path) {
  11721. var resolvedPath = commonjsResolve(path, originalModuleDir);
  11722. if (resolvedPath !== null) {
  11723. return getDynamicModules()[resolvedPath]();
  11724. }
  11725. ${ignoreDynamicRequires ? 'return require(path);' : FAILED_REQUIRE_ERROR}
  11726. }
  11727. handleRequire.resolve = function (path) {
  11728. var resolvedPath = commonjsResolve(path, originalModuleDir);
  11729. if (resolvedPath !== null) {
  11730. return resolvedPath;
  11731. }
  11732. return require.resolve(path);
  11733. }
  11734. return handleRequire;
  11735. }
  11736. function commonjsResolve (path, originalModuleDir) {
  11737. var shouldTryNodeModules = isPossibleNodeModulesPath(path);
  11738. path = normalize(path);
  11739. var relPath;
  11740. if (path[0] === '/') {
  11741. originalModuleDir = '';
  11742. }
  11743. var modules = getDynamicModules();
  11744. var checkedExtensions = ['', '.js', '.json'];
  11745. while (true) {
  11746. if (!shouldTryNodeModules) {
  11747. relPath = normalize(originalModuleDir + '/' + path);
  11748. } else {
  11749. relPath = normalize(originalModuleDir + '/node_modules/' + path);
  11750. }
  11751. if (relPath.endsWith('/..')) {
  11752. break; // Travelled too far up, avoid infinite loop
  11753. }
  11754. for (var extensionIndex = 0; extensionIndex < checkedExtensions.length; extensionIndex++) {
  11755. var resolvedPath = relPath + checkedExtensions[extensionIndex];
  11756. if (modules[resolvedPath]) {
  11757. return resolvedPath;
  11758. }
  11759. }
  11760. if (!shouldTryNodeModules) break;
  11761. var nextDir = normalize(originalModuleDir + '/..');
  11762. if (nextDir === originalModuleDir) break;
  11763. originalModuleDir = nextDir;
  11764. }
  11765. return null;
  11766. }
  11767. function isPossibleNodeModulesPath (modulePath) {
  11768. var c0 = modulePath[0];
  11769. if (c0 === '/' || c0 === '\\\\') return false;
  11770. var c1 = modulePath[1], c2 = modulePath[2];
  11771. if ((c0 === '.' && (!c1 || c1 === '/' || c1 === '\\\\')) ||
  11772. (c0 === '.' && c1 === '.' && (!c2 || c2 === '/' || c2 === '\\\\'))) return false;
  11773. if (c1 === ':' && (c2 === '/' || c2 === '\\\\')) return false;
  11774. return true;
  11775. }
  11776. function normalize (path) {
  11777. path = path.replace(/\\\\/g, '/');
  11778. var parts = path.split('/');
  11779. var slashed = parts[0] === '';
  11780. for (var i = 1; i < parts.length; i++) {
  11781. if (parts[i] === '.' || parts[i] === '') {
  11782. parts.splice(i--, 1);
  11783. }
  11784. }
  11785. for (var i = 1; i < parts.length; i++) {
  11786. if (parts[i] !== '..') continue;
  11787. if (i > 0 && parts[i - 1] !== '..' && parts[i - 1] !== '.') {
  11788. parts.splice(--i, 2);
  11789. i--;
  11790. }
  11791. }
  11792. path = parts.join('/');
  11793. if (slashed && path[0] !== '/') path = '/' + path;
  11794. else if (path.length === 0) path = '.';
  11795. return path;
  11796. }`;
  11797. }
  11798. const isWrappedId = (id, suffix) => id.endsWith(suffix);
  11799. const wrapId = (id, suffix) => `\0${id}${suffix}`;
  11800. const unwrapId = (wrappedId, suffix) => wrappedId.slice(1, -suffix.length);
  11801. const PROXY_SUFFIX = '?commonjs-proxy';
  11802. const WRAPPED_SUFFIX = '?commonjs-wrapped';
  11803. const EXTERNAL_SUFFIX = '?commonjs-external';
  11804. const EXPORTS_SUFFIX = '?commonjs-exports';
  11805. const MODULE_SUFFIX = '?commonjs-module';
  11806. const ENTRY_SUFFIX = '?commonjs-entry';
  11807. const ES_IMPORT_SUFFIX = '?commonjs-es-import';
  11808. const DYNAMIC_MODULES_ID = '\0commonjs-dynamic-modules';
  11809. const HELPERS_ID = '\0commonjsHelpers.js';
  11810. const IS_WRAPPED_COMMONJS = 'withRequireFunction';
  11811. // `x['default']` is used instead of `x.default` for backward compatibility with ES3 browsers.
  11812. // Minifiers like uglify will usually transpile it back if compatibility with ES3 is not enabled.
  11813. // This could be improved by inspecting Rollup's "generatedCode" option
  11814. const HELPERS = `
  11815. export var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
  11816. export function getDefaultExportFromCjs (x) {
  11817. return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
  11818. }
  11819. export function getDefaultExportFromNamespaceIfPresent (n) {
  11820. return n && Object.prototype.hasOwnProperty.call(n, 'default') ? n['default'] : n;
  11821. }
  11822. export function getDefaultExportFromNamespaceIfNotNamed (n) {
  11823. return n && Object.prototype.hasOwnProperty.call(n, 'default') && Object.keys(n).length === 1 ? n['default'] : n;
  11824. }
  11825. export function getAugmentedNamespace(n) {
  11826. if (n.__esModule) return n;
  11827. var f = n.default;
  11828. if (typeof f == "function") {
  11829. var a = function a () {
  11830. if (this instanceof a) {
  11831. return Reflect.construct(f, arguments, this.constructor);
  11832. }
  11833. return f.apply(this, arguments);
  11834. };
  11835. a.prototype = f.prototype;
  11836. } else a = {};
  11837. Object.defineProperty(a, '__esModule', {value: true});
  11838. Object.keys(n).forEach(function (k) {
  11839. var d = Object.getOwnPropertyDescriptor(n, k);
  11840. Object.defineProperty(a, k, d.get ? d : {
  11841. enumerable: true,
  11842. get: function () {
  11843. return n[k];
  11844. }
  11845. });
  11846. });
  11847. return a;
  11848. }
  11849. `;
  11850. function getHelpersModule() {
  11851. return HELPERS;
  11852. }
  11853. function getUnknownRequireProxy(id, requireReturnsDefault) {
  11854. if (requireReturnsDefault === true || id.endsWith('.json')) {
  11855. return `export { default } from ${JSON.stringify(id)};`;
  11856. }
  11857. const name = getName(id);
  11858. const exported =
  11859. requireReturnsDefault === 'auto'
  11860. ? `import { getDefaultExportFromNamespaceIfNotNamed } from "${HELPERS_ID}"; export default /*@__PURE__*/getDefaultExportFromNamespaceIfNotNamed(${name});`
  11861. : requireReturnsDefault === 'preferred'
  11862. ? `import { getDefaultExportFromNamespaceIfPresent } from "${HELPERS_ID}"; export default /*@__PURE__*/getDefaultExportFromNamespaceIfPresent(${name});`
  11863. : !requireReturnsDefault
  11864. ? `import { getAugmentedNamespace } from "${HELPERS_ID}"; export default /*@__PURE__*/getAugmentedNamespace(${name});`
  11865. : `export default ${name};`;
  11866. return `import * as ${name} from ${JSON.stringify(id)}; ${exported}`;
  11867. }
  11868. async function getStaticRequireProxy(id, requireReturnsDefault, loadModule) {
  11869. const name = getName(id);
  11870. const {
  11871. meta: { commonjs: commonjsMeta }
  11872. } = await loadModule({ id });
  11873. if (!commonjsMeta) {
  11874. return getUnknownRequireProxy(id, requireReturnsDefault);
  11875. }
  11876. if (commonjsMeta.isCommonJS) {
  11877. return `export { __moduleExports as default } from ${JSON.stringify(id)};`;
  11878. }
  11879. if (!requireReturnsDefault) {
  11880. return `import { getAugmentedNamespace } from "${HELPERS_ID}"; import * as ${name} from ${JSON.stringify(
  11881. id
  11882. )}; export default /*@__PURE__*/getAugmentedNamespace(${name});`;
  11883. }
  11884. if (
  11885. requireReturnsDefault !== true &&
  11886. (requireReturnsDefault === 'namespace' ||
  11887. !commonjsMeta.hasDefaultExport ||
  11888. (requireReturnsDefault === 'auto' && commonjsMeta.hasNamedExports))
  11889. ) {
  11890. return `import * as ${name} from ${JSON.stringify(id)}; export default ${name};`;
  11891. }
  11892. return `export { default } from ${JSON.stringify(id)};`;
  11893. }
  11894. function getEntryProxy(id, defaultIsModuleExports, getModuleInfo, shebang) {
  11895. const {
  11896. meta: { commonjs: commonjsMeta },
  11897. hasDefaultExport
  11898. } = getModuleInfo(id);
  11899. if (!commonjsMeta || commonjsMeta.isCommonJS !== IS_WRAPPED_COMMONJS) {
  11900. const stringifiedId = JSON.stringify(id);
  11901. let code = `export * from ${stringifiedId};`;
  11902. if (hasDefaultExport) {
  11903. code += `export { default } from ${stringifiedId};`;
  11904. }
  11905. return shebang + code;
  11906. }
  11907. const result = getEsImportProxy(id, defaultIsModuleExports);
  11908. return {
  11909. ...result,
  11910. code: shebang + result.code
  11911. };
  11912. }
  11913. function getEsImportProxy(id, defaultIsModuleExports) {
  11914. const name = getName(id);
  11915. const exportsName = `${name}Exports`;
  11916. const requireModule = `require${capitalize(name)}`;
  11917. let code =
  11918. `import { getDefaultExportFromCjs } from "${HELPERS_ID}";\n` +
  11919. `import { __require as ${requireModule} } from ${JSON.stringify(id)};\n` +
  11920. `var ${exportsName} = ${requireModule}();\n` +
  11921. `export { ${exportsName} as __moduleExports };`;
  11922. if (defaultIsModuleExports === true) {
  11923. code += `\nexport { ${exportsName} as default };`;
  11924. } else {
  11925. code += `export default /*@__PURE__*/getDefaultExportFromCjs(${exportsName});`;
  11926. }
  11927. return {
  11928. code,
  11929. syntheticNamedExports: '__moduleExports'
  11930. };
  11931. }
  11932. /* eslint-disable no-param-reassign, no-undefined */
  11933. function getCandidatesForExtension(resolved, extension) {
  11934. return [resolved + extension, `${resolved}${sep$1}index${extension}`];
  11935. }
  11936. function getCandidates(resolved, extensions) {
  11937. return extensions.reduce(
  11938. (paths, extension) => paths.concat(getCandidatesForExtension(resolved, extension)),
  11939. [resolved]
  11940. );
  11941. }
  11942. function resolveExtensions(importee, importer, extensions) {
  11943. // not our problem
  11944. if (importee[0] !== '.' || !importer) return undefined;
  11945. const resolved = resolve$3(dirname$1(importer), importee);
  11946. const candidates = getCandidates(resolved, extensions);
  11947. for (let i = 0; i < candidates.length; i += 1) {
  11948. try {
  11949. const stats = statSync$1(candidates[i]);
  11950. if (stats.isFile()) return { id: candidates[i] };
  11951. } catch (err) {
  11952. /* noop */
  11953. }
  11954. }
  11955. return undefined;
  11956. }
  11957. function getResolveId(extensions, isPossibleCjsId) {
  11958. const currentlyResolving = new Map();
  11959. return {
  11960. /**
  11961. * This is a Maps of importers to Sets of require sources being resolved at
  11962. * the moment by resolveRequireSourcesAndUpdateMeta
  11963. */
  11964. currentlyResolving,
  11965. async resolveId(importee, importer, resolveOptions) {
  11966. const customOptions = resolveOptions.custom;
  11967. // All logic below is specific to ES imports.
  11968. // Also, if we do not skip this logic for requires that are resolved while
  11969. // transforming a commonjs file, it can easily lead to deadlocks.
  11970. if (
  11971. customOptions &&
  11972. customOptions['node-resolve'] &&
  11973. customOptions['node-resolve'].isRequire
  11974. ) {
  11975. return null;
  11976. }
  11977. const currentlyResolvingForParent = currentlyResolving.get(importer);
  11978. if (currentlyResolvingForParent && currentlyResolvingForParent.has(importee)) {
  11979. this.warn({
  11980. code: 'THIS_RESOLVE_WITHOUT_OPTIONS',
  11981. message:
  11982. '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.',
  11983. url: 'https://rollupjs.org/guide/en/#resolveid'
  11984. });
  11985. return null;
  11986. }
  11987. if (isWrappedId(importee, WRAPPED_SUFFIX)) {
  11988. return unwrapId(importee, WRAPPED_SUFFIX);
  11989. }
  11990. if (
  11991. importee.endsWith(ENTRY_SUFFIX) ||
  11992. isWrappedId(importee, MODULE_SUFFIX) ||
  11993. isWrappedId(importee, EXPORTS_SUFFIX) ||
  11994. isWrappedId(importee, PROXY_SUFFIX) ||
  11995. isWrappedId(importee, ES_IMPORT_SUFFIX) ||
  11996. isWrappedId(importee, EXTERNAL_SUFFIX) ||
  11997. importee.startsWith(HELPERS_ID) ||
  11998. importee === DYNAMIC_MODULES_ID
  11999. ) {
  12000. return importee;
  12001. }
  12002. if (importer) {
  12003. if (
  12004. importer === DYNAMIC_MODULES_ID ||
  12005. // Proxies are only importing resolved ids, no need to resolve again
  12006. isWrappedId(importer, PROXY_SUFFIX) ||
  12007. isWrappedId(importer, ES_IMPORT_SUFFIX) ||
  12008. importer.endsWith(ENTRY_SUFFIX)
  12009. ) {
  12010. return importee;
  12011. }
  12012. if (isWrappedId(importer, EXTERNAL_SUFFIX)) {
  12013. // We need to return null for unresolved imports so that the proper warning is shown
  12014. if (
  12015. !(await this.resolve(
  12016. importee,
  12017. importer,
  12018. Object.assign({ skipSelf: true }, resolveOptions)
  12019. ))
  12020. ) {
  12021. return null;
  12022. }
  12023. // For other external imports, we need to make sure they are handled as external
  12024. return { id: importee, external: true };
  12025. }
  12026. }
  12027. if (importee.startsWith('\0')) {
  12028. return null;
  12029. }
  12030. // If this is an entry point or ESM import, we need to figure out if the importee is wrapped and
  12031. // if that is the case, we need to add a proxy.
  12032. const resolved =
  12033. (await this.resolve(
  12034. importee,
  12035. importer,
  12036. Object.assign({ skipSelf: true }, resolveOptions)
  12037. )) || resolveExtensions(importee, importer, extensions);
  12038. // Make sure that even if other plugins resolve again, we ignore our own proxies
  12039. if (
  12040. !resolved ||
  12041. resolved.external ||
  12042. resolved.id.endsWith(ENTRY_SUFFIX) ||
  12043. isWrappedId(resolved.id, ES_IMPORT_SUFFIX) ||
  12044. !isPossibleCjsId(resolved.id)
  12045. ) {
  12046. return resolved;
  12047. }
  12048. const moduleInfo = await this.load(resolved);
  12049. const {
  12050. meta: { commonjs: commonjsMeta }
  12051. } = moduleInfo;
  12052. if (commonjsMeta) {
  12053. const { isCommonJS } = commonjsMeta;
  12054. if (isCommonJS) {
  12055. if (resolveOptions.isEntry) {
  12056. moduleInfo.moduleSideEffects = true;
  12057. // We must not precede entry proxies with a `\0` as that will mess up relative external resolution
  12058. return resolved.id + ENTRY_SUFFIX;
  12059. }
  12060. if (isCommonJS === IS_WRAPPED_COMMONJS) {
  12061. return { id: wrapId(resolved.id, ES_IMPORT_SUFFIX), meta: { commonjs: { resolved } } };
  12062. }
  12063. }
  12064. }
  12065. return resolved;
  12066. }
  12067. };
  12068. }
  12069. function getRequireResolver(extensions, detectCyclesAndConditional, currentlyResolving) {
  12070. const knownCjsModuleTypes = Object.create(null);
  12071. const requiredIds = Object.create(null);
  12072. const unconditionallyRequiredIds = Object.create(null);
  12073. const dependencies = Object.create(null);
  12074. const getDependencies = (id) => dependencies[id] || (dependencies[id] = new Set());
  12075. const isCyclic = (id) => {
  12076. const dependenciesToCheck = new Set(getDependencies(id));
  12077. for (const dependency of dependenciesToCheck) {
  12078. if (dependency === id) {
  12079. return true;
  12080. }
  12081. for (const childDependency of getDependencies(dependency)) {
  12082. dependenciesToCheck.add(childDependency);
  12083. }
  12084. }
  12085. return false;
  12086. };
  12087. // Once a module is listed here, its type (wrapped or not) is fixed and may
  12088. // not change for the rest of the current build, to not break already
  12089. // transformed modules.
  12090. const fullyAnalyzedModules = Object.create(null);
  12091. const getTypeForFullyAnalyzedModule = (id) => {
  12092. const knownType = knownCjsModuleTypes[id];
  12093. if (knownType !== true || !detectCyclesAndConditional || fullyAnalyzedModules[id]) {
  12094. return knownType;
  12095. }
  12096. if (isCyclic(id)) {
  12097. return (knownCjsModuleTypes[id] = IS_WRAPPED_COMMONJS);
  12098. }
  12099. return knownType;
  12100. };
  12101. const setInitialParentType = (id, initialCommonJSType) => {
  12102. // Fully analyzed modules may never change type
  12103. if (fullyAnalyzedModules[id]) {
  12104. return;
  12105. }
  12106. knownCjsModuleTypes[id] = initialCommonJSType;
  12107. if (
  12108. detectCyclesAndConditional &&
  12109. knownCjsModuleTypes[id] === true &&
  12110. requiredIds[id] &&
  12111. !unconditionallyRequiredIds[id]
  12112. ) {
  12113. knownCjsModuleTypes[id] = IS_WRAPPED_COMMONJS;
  12114. }
  12115. };
  12116. const analyzeRequiredModule = async (parentId, resolved, isConditional, loadModule) => {
  12117. const childId = resolved.id;
  12118. requiredIds[childId] = true;
  12119. if (!(isConditional || knownCjsModuleTypes[parentId] === IS_WRAPPED_COMMONJS)) {
  12120. unconditionallyRequiredIds[childId] = true;
  12121. }
  12122. getDependencies(parentId).add(childId);
  12123. if (!isCyclic(childId)) {
  12124. // This makes sure the current transform handler waits for all direct
  12125. // dependencies to be loaded and transformed and therefore for all
  12126. // transitive CommonJS dependencies to be loaded as well so that all
  12127. // cycles have been found and knownCjsModuleTypes is reliable.
  12128. await loadModule(resolved);
  12129. }
  12130. };
  12131. const getTypeForImportedModule = async (resolved, loadModule) => {
  12132. if (resolved.id in knownCjsModuleTypes) {
  12133. // This handles cyclic ES dependencies
  12134. return knownCjsModuleTypes[resolved.id];
  12135. }
  12136. const {
  12137. meta: { commonjs }
  12138. } = await loadModule(resolved);
  12139. return (commonjs && commonjs.isCommonJS) || false;
  12140. };
  12141. return {
  12142. getWrappedIds: () =>
  12143. Object.keys(knownCjsModuleTypes).filter(
  12144. (id) => knownCjsModuleTypes[id] === IS_WRAPPED_COMMONJS
  12145. ),
  12146. isRequiredId: (id) => requiredIds[id],
  12147. async shouldTransformCachedModule({
  12148. id: parentId,
  12149. resolvedSources,
  12150. meta: { commonjs: parentMeta }
  12151. }) {
  12152. // We explicitly track ES modules to handle circular imports
  12153. if (!(parentMeta && parentMeta.isCommonJS)) knownCjsModuleTypes[parentId] = false;
  12154. if (isWrappedId(parentId, ES_IMPORT_SUFFIX)) return false;
  12155. const parentRequires = parentMeta && parentMeta.requires;
  12156. if (parentRequires) {
  12157. setInitialParentType(parentId, parentMeta.initialCommonJSType);
  12158. await Promise.all(
  12159. parentRequires.map(({ resolved, isConditional }) =>
  12160. analyzeRequiredModule(parentId, resolved, isConditional, this.load)
  12161. )
  12162. );
  12163. if (getTypeForFullyAnalyzedModule(parentId) !== parentMeta.isCommonJS) {
  12164. return true;
  12165. }
  12166. for (const {
  12167. resolved: { id }
  12168. } of parentRequires) {
  12169. if (getTypeForFullyAnalyzedModule(id) !== parentMeta.isRequiredCommonJS[id]) {
  12170. return true;
  12171. }
  12172. }
  12173. // Now that we decided to go with the cached copy, neither the parent
  12174. // module nor any of its children may change types anymore
  12175. fullyAnalyzedModules[parentId] = true;
  12176. for (const {
  12177. resolved: { id }
  12178. } of parentRequires) {
  12179. fullyAnalyzedModules[id] = true;
  12180. }
  12181. }
  12182. const parentRequireSet = new Set((parentRequires || []).map(({ resolved: { id } }) => id));
  12183. return (
  12184. await Promise.all(
  12185. Object.keys(resolvedSources)
  12186. .map((source) => resolvedSources[source])
  12187. .filter(({ id, external }) => !(external || parentRequireSet.has(id)))
  12188. .map(async (resolved) => {
  12189. if (isWrappedId(resolved.id, ES_IMPORT_SUFFIX)) {
  12190. return (
  12191. (await getTypeForImportedModule(
  12192. (
  12193. await this.load({ id: resolved.id })
  12194. ).meta.commonjs.resolved,
  12195. this.load
  12196. )) !== IS_WRAPPED_COMMONJS
  12197. );
  12198. }
  12199. return (await getTypeForImportedModule(resolved, this.load)) === IS_WRAPPED_COMMONJS;
  12200. })
  12201. )
  12202. ).some((shouldTransform) => shouldTransform);
  12203. },
  12204. /* eslint-disable no-param-reassign */
  12205. resolveRequireSourcesAndUpdateMeta:
  12206. (rollupContext) => async (parentId, isParentCommonJS, parentMeta, sources) => {
  12207. parentMeta.initialCommonJSType = isParentCommonJS;
  12208. parentMeta.requires = [];
  12209. parentMeta.isRequiredCommonJS = Object.create(null);
  12210. setInitialParentType(parentId, isParentCommonJS);
  12211. const currentlyResolvingForParent = currentlyResolving.get(parentId) || new Set();
  12212. currentlyResolving.set(parentId, currentlyResolvingForParent);
  12213. const requireTargets = await Promise.all(
  12214. sources.map(async ({ source, isConditional }) => {
  12215. // Never analyze or proxy internal modules
  12216. if (source.startsWith('\0')) {
  12217. return { id: source, allowProxy: false };
  12218. }
  12219. currentlyResolvingForParent.add(source);
  12220. const resolved =
  12221. (await rollupContext.resolve(source, parentId, {
  12222. skipSelf: false,
  12223. custom: { 'node-resolve': { isRequire: true } }
  12224. })) || resolveExtensions(source, parentId, extensions);
  12225. currentlyResolvingForParent.delete(source);
  12226. if (!resolved) {
  12227. return { id: wrapId(source, EXTERNAL_SUFFIX), allowProxy: false };
  12228. }
  12229. const childId = resolved.id;
  12230. if (resolved.external) {
  12231. return { id: wrapId(childId, EXTERNAL_SUFFIX), allowProxy: false };
  12232. }
  12233. parentMeta.requires.push({ resolved, isConditional });
  12234. await analyzeRequiredModule(parentId, resolved, isConditional, rollupContext.load);
  12235. return { id: childId, allowProxy: true };
  12236. })
  12237. );
  12238. parentMeta.isCommonJS = getTypeForFullyAnalyzedModule(parentId);
  12239. fullyAnalyzedModules[parentId] = true;
  12240. return requireTargets.map(({ id: dependencyId, allowProxy }, index) => {
  12241. // eslint-disable-next-line no-multi-assign
  12242. const isCommonJS = (parentMeta.isRequiredCommonJS[dependencyId] =
  12243. getTypeForFullyAnalyzedModule(dependencyId));
  12244. fullyAnalyzedModules[dependencyId] = true;
  12245. return {
  12246. source: sources[index].source,
  12247. id: allowProxy
  12248. ? isCommonJS === IS_WRAPPED_COMMONJS
  12249. ? wrapId(dependencyId, WRAPPED_SUFFIX)
  12250. : wrapId(dependencyId, PROXY_SUFFIX)
  12251. : dependencyId,
  12252. isCommonJS
  12253. };
  12254. });
  12255. },
  12256. isCurrentlyResolving(source, parentId) {
  12257. const currentlyResolvingForParent = currentlyResolving.get(parentId);
  12258. return currentlyResolvingForParent && currentlyResolvingForParent.has(source);
  12259. }
  12260. };
  12261. }
  12262. function validateVersion(actualVersion, peerDependencyVersion, name) {
  12263. const versionRegexp = /\^(\d+\.\d+\.\d+)/g;
  12264. let minMajor = Infinity;
  12265. let minMinor = Infinity;
  12266. let minPatch = Infinity;
  12267. let foundVersion;
  12268. // eslint-disable-next-line no-cond-assign
  12269. while ((foundVersion = versionRegexp.exec(peerDependencyVersion))) {
  12270. const [foundMajor, foundMinor, foundPatch] = foundVersion[1].split('.').map(Number);
  12271. if (foundMajor < minMajor) {
  12272. minMajor = foundMajor;
  12273. minMinor = foundMinor;
  12274. minPatch = foundPatch;
  12275. }
  12276. }
  12277. if (!actualVersion) {
  12278. throw new Error(
  12279. `Insufficient ${name} version: "@rollup/plugin-commonjs" requires at least ${name}@${minMajor}.${minMinor}.${minPatch}.`
  12280. );
  12281. }
  12282. const [major, minor, patch] = actualVersion.split('.').map(Number);
  12283. if (
  12284. major < minMajor ||
  12285. (major === minMajor && (minor < minMinor || (minor === minMinor && patch < minPatch)))
  12286. ) {
  12287. throw new Error(
  12288. `Insufficient ${name} version: "@rollup/plugin-commonjs" requires at least ${name}@${minMajor}.${minMinor}.${minPatch} but found ${name}@${actualVersion}.`
  12289. );
  12290. }
  12291. }
  12292. const operators = {
  12293. '==': (x) => equals(x.left, x.right, false),
  12294. '!=': (x) => not(operators['=='](x)),
  12295. '===': (x) => equals(x.left, x.right, true),
  12296. '!==': (x) => not(operators['==='](x)),
  12297. '!': (x) => isFalsy(x.argument),
  12298. '&&': (x) => isTruthy(x.left) && isTruthy(x.right),
  12299. '||': (x) => isTruthy(x.left) || isTruthy(x.right)
  12300. };
  12301. function not(value) {
  12302. return value === null ? value : !value;
  12303. }
  12304. function equals(a, b, strict) {
  12305. if (a.type !== b.type) return null;
  12306. // eslint-disable-next-line eqeqeq
  12307. if (a.type === 'Literal') return strict ? a.value === b.value : a.value == b.value;
  12308. return null;
  12309. }
  12310. function isTruthy(node) {
  12311. if (!node) return false;
  12312. if (node.type === 'Literal') return !!node.value;
  12313. if (node.type === 'ParenthesizedExpression') return isTruthy(node.expression);
  12314. if (node.operator in operators) return operators[node.operator](node);
  12315. return null;
  12316. }
  12317. function isFalsy(node) {
  12318. return not(isTruthy(node));
  12319. }
  12320. function getKeypath(node) {
  12321. const parts = [];
  12322. while (node.type === 'MemberExpression') {
  12323. if (node.computed) return null;
  12324. parts.unshift(node.property.name);
  12325. // eslint-disable-next-line no-param-reassign
  12326. node = node.object;
  12327. }
  12328. if (node.type !== 'Identifier') return null;
  12329. const { name } = node;
  12330. parts.unshift(name);
  12331. return { name, keypath: parts.join('.') };
  12332. }
  12333. const KEY_COMPILED_ESM = '__esModule';
  12334. function getDefineCompiledEsmType(node) {
  12335. const definedPropertyWithExports = getDefinePropertyCallName(node, 'exports');
  12336. const definedProperty =
  12337. definedPropertyWithExports || getDefinePropertyCallName(node, 'module.exports');
  12338. if (definedProperty && definedProperty.key === KEY_COMPILED_ESM) {
  12339. return isTruthy(definedProperty.value)
  12340. ? definedPropertyWithExports
  12341. ? 'exports'
  12342. : 'module'
  12343. : false;
  12344. }
  12345. return false;
  12346. }
  12347. function getDefinePropertyCallName(node, targetName) {
  12348. const {
  12349. callee: { object, property }
  12350. } = node;
  12351. if (!object || object.type !== 'Identifier' || object.name !== 'Object') return;
  12352. if (!property || property.type !== 'Identifier' || property.name !== 'defineProperty') return;
  12353. if (node.arguments.length !== 3) return;
  12354. const targetNames = targetName.split('.');
  12355. const [target, key, value] = node.arguments;
  12356. if (targetNames.length === 1) {
  12357. if (target.type !== 'Identifier' || target.name !== targetNames[0]) {
  12358. return;
  12359. }
  12360. }
  12361. if (targetNames.length === 2) {
  12362. if (
  12363. target.type !== 'MemberExpression' ||
  12364. target.object.name !== targetNames[0] ||
  12365. target.property.name !== targetNames[1]
  12366. ) {
  12367. return;
  12368. }
  12369. }
  12370. if (value.type !== 'ObjectExpression' || !value.properties) return;
  12371. const valueProperty = value.properties.find((p) => p.key && p.key.name === 'value');
  12372. if (!valueProperty || !valueProperty.value) return;
  12373. // eslint-disable-next-line consistent-return
  12374. return { key: key.value, value: valueProperty.value };
  12375. }
  12376. function isShorthandProperty(parent) {
  12377. return parent && parent.type === 'Property' && parent.shorthand;
  12378. }
  12379. function wrapCode(magicString, uses, moduleName, exportsName, indentExclusionRanges) {
  12380. const args = [];
  12381. const passedArgs = [];
  12382. if (uses.module) {
  12383. args.push('module');
  12384. passedArgs.push(moduleName);
  12385. }
  12386. if (uses.exports) {
  12387. args.push('exports');
  12388. passedArgs.push(uses.module ? `${moduleName}.exports` : exportsName);
  12389. }
  12390. magicString
  12391. .trim()
  12392. .indent('\t', { exclude: indentExclusionRanges })
  12393. .prepend(`(function (${args.join(', ')}) {\n`)
  12394. // For some reason, this line is only indented correctly when using a
  12395. // require-wrapper if we have this leading space
  12396. .append(` \n} (${passedArgs.join(', ')}));`);
  12397. }
  12398. function rewriteExportsAndGetExportsBlock(
  12399. magicString,
  12400. moduleName,
  12401. exportsName,
  12402. exportedExportsName,
  12403. wrapped,
  12404. moduleExportsAssignments,
  12405. firstTopLevelModuleExportsAssignment,
  12406. exportsAssignmentsByName,
  12407. topLevelAssignments,
  12408. defineCompiledEsmExpressions,
  12409. deconflictedExportNames,
  12410. code,
  12411. HELPERS_NAME,
  12412. exportMode,
  12413. defaultIsModuleExports,
  12414. usesRequireWrapper,
  12415. requireName
  12416. ) {
  12417. const exports = [];
  12418. const exportDeclarations = [];
  12419. if (usesRequireWrapper) {
  12420. getExportsWhenUsingRequireWrapper(
  12421. magicString,
  12422. wrapped,
  12423. exportMode,
  12424. exports,
  12425. moduleExportsAssignments,
  12426. exportsAssignmentsByName,
  12427. moduleName,
  12428. exportsName,
  12429. requireName,
  12430. defineCompiledEsmExpressions
  12431. );
  12432. } else if (exportMode === 'replace') {
  12433. getExportsForReplacedModuleExports(
  12434. magicString,
  12435. exports,
  12436. exportDeclarations,
  12437. moduleExportsAssignments,
  12438. firstTopLevelModuleExportsAssignment,
  12439. exportsName,
  12440. defaultIsModuleExports,
  12441. HELPERS_NAME
  12442. );
  12443. } else {
  12444. if (exportMode === 'module') {
  12445. exportDeclarations.push(`var ${exportedExportsName} = ${moduleName}.exports`);
  12446. exports.push(`${exportedExportsName} as __moduleExports`);
  12447. } else {
  12448. exports.push(`${exportsName} as __moduleExports`);
  12449. }
  12450. if (wrapped) {
  12451. exportDeclarations.push(
  12452. getDefaultExportDeclaration(exportedExportsName, defaultIsModuleExports, HELPERS_NAME)
  12453. );
  12454. } else {
  12455. getExports(
  12456. magicString,
  12457. exports,
  12458. exportDeclarations,
  12459. moduleExportsAssignments,
  12460. exportsAssignmentsByName,
  12461. deconflictedExportNames,
  12462. topLevelAssignments,
  12463. moduleName,
  12464. exportsName,
  12465. exportedExportsName,
  12466. defineCompiledEsmExpressions,
  12467. HELPERS_NAME,
  12468. defaultIsModuleExports,
  12469. exportMode
  12470. );
  12471. }
  12472. }
  12473. if (exports.length) {
  12474. exportDeclarations.push(`export { ${exports.join(', ')} }`);
  12475. }
  12476. return `\n\n${exportDeclarations.join(';\n')};`;
  12477. }
  12478. function getExportsWhenUsingRequireWrapper(
  12479. magicString,
  12480. wrapped,
  12481. exportMode,
  12482. exports,
  12483. moduleExportsAssignments,
  12484. exportsAssignmentsByName,
  12485. moduleName,
  12486. exportsName,
  12487. requireName,
  12488. defineCompiledEsmExpressions
  12489. ) {
  12490. exports.push(`${requireName} as __require`);
  12491. if (wrapped) return;
  12492. if (exportMode === 'replace') {
  12493. rewriteModuleExportsAssignments(magicString, moduleExportsAssignments, exportsName);
  12494. } else {
  12495. rewriteModuleExportsAssignments(magicString, moduleExportsAssignments, `${moduleName}.exports`);
  12496. // Collect and rewrite named exports
  12497. for (const [exportName, { nodes }] of exportsAssignmentsByName) {
  12498. for (const { node, type } of nodes) {
  12499. magicString.overwrite(
  12500. node.start,
  12501. node.left.end,
  12502. `${
  12503. exportMode === 'module' && type === 'module' ? `${moduleName}.exports` : exportsName
  12504. }.${exportName}`
  12505. );
  12506. }
  12507. }
  12508. replaceDefineCompiledEsmExpressionsAndGetIfRestorable(
  12509. defineCompiledEsmExpressions,
  12510. magicString,
  12511. exportMode,
  12512. moduleName,
  12513. exportsName
  12514. );
  12515. }
  12516. }
  12517. function getExportsForReplacedModuleExports(
  12518. magicString,
  12519. exports,
  12520. exportDeclarations,
  12521. moduleExportsAssignments,
  12522. firstTopLevelModuleExportsAssignment,
  12523. exportsName,
  12524. defaultIsModuleExports,
  12525. HELPERS_NAME
  12526. ) {
  12527. for (const { left } of moduleExportsAssignments) {
  12528. magicString.overwrite(left.start, left.end, exportsName);
  12529. }
  12530. magicString.prependRight(firstTopLevelModuleExportsAssignment.left.start, 'var ');
  12531. exports.push(`${exportsName} as __moduleExports`);
  12532. exportDeclarations.push(
  12533. getDefaultExportDeclaration(exportsName, defaultIsModuleExports, HELPERS_NAME)
  12534. );
  12535. }
  12536. function getDefaultExportDeclaration(exportedExportsName, defaultIsModuleExports, HELPERS_NAME) {
  12537. return `export default ${
  12538. defaultIsModuleExports === true
  12539. ? exportedExportsName
  12540. : defaultIsModuleExports === false
  12541. ? `${exportedExportsName}.default`
  12542. : `/*@__PURE__*/${HELPERS_NAME}.getDefaultExportFromCjs(${exportedExportsName})`
  12543. }`;
  12544. }
  12545. function getExports(
  12546. magicString,
  12547. exports,
  12548. exportDeclarations,
  12549. moduleExportsAssignments,
  12550. exportsAssignmentsByName,
  12551. deconflictedExportNames,
  12552. topLevelAssignments,
  12553. moduleName,
  12554. exportsName,
  12555. exportedExportsName,
  12556. defineCompiledEsmExpressions,
  12557. HELPERS_NAME,
  12558. defaultIsModuleExports,
  12559. exportMode
  12560. ) {
  12561. let deconflictedDefaultExportName;
  12562. // Collect and rewrite module.exports assignments
  12563. for (const { left } of moduleExportsAssignments) {
  12564. magicString.overwrite(left.start, left.end, `${moduleName}.exports`);
  12565. }
  12566. // Collect and rewrite named exports
  12567. for (const [exportName, { nodes }] of exportsAssignmentsByName) {
  12568. const deconflicted = deconflictedExportNames[exportName];
  12569. let needsDeclaration = true;
  12570. for (const { node, type } of nodes) {
  12571. let replacement = `${deconflicted} = ${
  12572. exportMode === 'module' && type === 'module' ? `${moduleName}.exports` : exportsName
  12573. }.${exportName}`;
  12574. if (needsDeclaration && topLevelAssignments.has(node)) {
  12575. replacement = `var ${replacement}`;
  12576. needsDeclaration = false;
  12577. }
  12578. magicString.overwrite(node.start, node.left.end, replacement);
  12579. }
  12580. if (needsDeclaration) {
  12581. magicString.prepend(`var ${deconflicted};\n`);
  12582. }
  12583. if (exportName === 'default') {
  12584. deconflictedDefaultExportName = deconflicted;
  12585. } else {
  12586. exports.push(exportName === deconflicted ? exportName : `${deconflicted} as ${exportName}`);
  12587. }
  12588. }
  12589. const isRestorableCompiledEsm = replaceDefineCompiledEsmExpressionsAndGetIfRestorable(
  12590. defineCompiledEsmExpressions,
  12591. magicString,
  12592. exportMode,
  12593. moduleName,
  12594. exportsName
  12595. );
  12596. if (
  12597. defaultIsModuleExports === false ||
  12598. (defaultIsModuleExports === 'auto' &&
  12599. isRestorableCompiledEsm &&
  12600. moduleExportsAssignments.length === 0)
  12601. ) {
  12602. // If there is no deconflictedDefaultExportName, then we use the namespace as
  12603. // fallback because there can be no "default" property on the namespace
  12604. exports.push(`${deconflictedDefaultExportName || exportedExportsName} as default`);
  12605. } else if (
  12606. defaultIsModuleExports === true ||
  12607. (!isRestorableCompiledEsm && moduleExportsAssignments.length === 0)
  12608. ) {
  12609. exports.push(`${exportedExportsName} as default`);
  12610. } else {
  12611. exportDeclarations.push(
  12612. getDefaultExportDeclaration(exportedExportsName, defaultIsModuleExports, HELPERS_NAME)
  12613. );
  12614. }
  12615. }
  12616. function rewriteModuleExportsAssignments(magicString, moduleExportsAssignments, exportsName) {
  12617. for (const { left } of moduleExportsAssignments) {
  12618. magicString.overwrite(left.start, left.end, exportsName);
  12619. }
  12620. }
  12621. function replaceDefineCompiledEsmExpressionsAndGetIfRestorable(
  12622. defineCompiledEsmExpressions,
  12623. magicString,
  12624. exportMode,
  12625. moduleName,
  12626. exportsName
  12627. ) {
  12628. let isRestorableCompiledEsm = false;
  12629. for (const { node, type } of defineCompiledEsmExpressions) {
  12630. isRestorableCompiledEsm = true;
  12631. const moduleExportsExpression =
  12632. node.type === 'CallExpression' ? node.arguments[0] : node.left.object;
  12633. magicString.overwrite(
  12634. moduleExportsExpression.start,
  12635. moduleExportsExpression.end,
  12636. exportMode === 'module' && type === 'module' ? `${moduleName}.exports` : exportsName
  12637. );
  12638. }
  12639. return isRestorableCompiledEsm;
  12640. }
  12641. function isRequireExpression(node, scope) {
  12642. if (!node) return false;
  12643. if (node.type !== 'CallExpression') return false;
  12644. // Weird case of `require()` or `module.require()` without arguments
  12645. if (node.arguments.length === 0) return false;
  12646. return isRequire(node.callee, scope);
  12647. }
  12648. function isRequire(node, scope) {
  12649. return (
  12650. (node.type === 'Identifier' && node.name === 'require' && !scope.contains('require')) ||
  12651. (node.type === 'MemberExpression' && isModuleRequire(node, scope))
  12652. );
  12653. }
  12654. function isModuleRequire({ object, property }, scope) {
  12655. return (
  12656. object.type === 'Identifier' &&
  12657. object.name === 'module' &&
  12658. property.type === 'Identifier' &&
  12659. property.name === 'require' &&
  12660. !scope.contains('module')
  12661. );
  12662. }
  12663. function hasDynamicArguments(node) {
  12664. return (
  12665. node.arguments.length > 1 ||
  12666. (node.arguments[0].type !== 'Literal' &&
  12667. (node.arguments[0].type !== 'TemplateLiteral' || node.arguments[0].expressions.length > 0))
  12668. );
  12669. }
  12670. const reservedMethod = { resolve: true, cache: true, main: true };
  12671. function isNodeRequirePropertyAccess(parent) {
  12672. return parent && parent.property && reservedMethod[parent.property.name];
  12673. }
  12674. function getRequireStringArg(node) {
  12675. return node.arguments[0].type === 'Literal'
  12676. ? node.arguments[0].value
  12677. : node.arguments[0].quasis[0].value.cooked;
  12678. }
  12679. function getRequireHandlers() {
  12680. const requireExpressions = [];
  12681. function addRequireExpression(
  12682. sourceId,
  12683. node,
  12684. scope,
  12685. usesReturnValue,
  12686. isInsideTryBlock,
  12687. isInsideConditional,
  12688. toBeRemoved
  12689. ) {
  12690. requireExpressions.push({
  12691. sourceId,
  12692. node,
  12693. scope,
  12694. usesReturnValue,
  12695. isInsideTryBlock,
  12696. isInsideConditional,
  12697. toBeRemoved
  12698. });
  12699. }
  12700. async function rewriteRequireExpressionsAndGetImportBlock(
  12701. magicString,
  12702. topLevelDeclarations,
  12703. reassignedNames,
  12704. helpersName,
  12705. dynamicRequireName,
  12706. moduleName,
  12707. exportsName,
  12708. id,
  12709. exportMode,
  12710. resolveRequireSourcesAndUpdateMeta,
  12711. needsRequireWrapper,
  12712. isEsModule,
  12713. isDynamicRequireModulesEnabled,
  12714. getIgnoreTryCatchRequireStatementMode,
  12715. commonjsMeta
  12716. ) {
  12717. const imports = [];
  12718. imports.push(`import * as ${helpersName} from "${HELPERS_ID}"`);
  12719. if (dynamicRequireName) {
  12720. imports.push(
  12721. `import { ${
  12722. isDynamicRequireModulesEnabled ? CREATE_COMMONJS_REQUIRE_EXPORT : COMMONJS_REQUIRE_EXPORT
  12723. } as ${dynamicRequireName} } from "${DYNAMIC_MODULES_ID}"`
  12724. );
  12725. }
  12726. if (exportMode === 'module') {
  12727. imports.push(
  12728. `import { __module as ${moduleName} } from ${JSON.stringify(wrapId(id, MODULE_SUFFIX))}`,
  12729. `var ${exportsName} = ${moduleName}.exports`
  12730. );
  12731. } else if (exportMode === 'exports') {
  12732. imports.push(
  12733. `import { __exports as ${exportsName} } from ${JSON.stringify(wrapId(id, EXPORTS_SUFFIX))}`
  12734. );
  12735. }
  12736. const requiresBySource = collectSources(requireExpressions);
  12737. const requireTargets = await resolveRequireSourcesAndUpdateMeta(
  12738. id,
  12739. needsRequireWrapper ? IS_WRAPPED_COMMONJS : !isEsModule,
  12740. commonjsMeta,
  12741. Object.keys(requiresBySource).map((source) => {
  12742. return {
  12743. source,
  12744. isConditional: requiresBySource[source].every((require) => require.isInsideConditional)
  12745. };
  12746. })
  12747. );
  12748. processRequireExpressions(
  12749. imports,
  12750. requireTargets,
  12751. requiresBySource,
  12752. getIgnoreTryCatchRequireStatementMode,
  12753. magicString
  12754. );
  12755. return imports.length ? `${imports.join(';\n')};\n\n` : '';
  12756. }
  12757. return {
  12758. addRequireExpression,
  12759. rewriteRequireExpressionsAndGetImportBlock
  12760. };
  12761. }
  12762. function collectSources(requireExpressions) {
  12763. const requiresBySource = Object.create(null);
  12764. for (const requireExpression of requireExpressions) {
  12765. const { sourceId } = requireExpression;
  12766. if (!requiresBySource[sourceId]) {
  12767. requiresBySource[sourceId] = [];
  12768. }
  12769. const requires = requiresBySource[sourceId];
  12770. requires.push(requireExpression);
  12771. }
  12772. return requiresBySource;
  12773. }
  12774. function processRequireExpressions(
  12775. imports,
  12776. requireTargets,
  12777. requiresBySource,
  12778. getIgnoreTryCatchRequireStatementMode,
  12779. magicString
  12780. ) {
  12781. const generateRequireName = getGenerateRequireName();
  12782. for (const { source, id: resolvedId, isCommonJS } of requireTargets) {
  12783. const requires = requiresBySource[source];
  12784. const name = generateRequireName(requires);
  12785. let usesRequired = false;
  12786. let needsImport = false;
  12787. for (const { node, usesReturnValue, toBeRemoved, isInsideTryBlock } of requires) {
  12788. const { canConvertRequire, shouldRemoveRequire } =
  12789. isInsideTryBlock && isWrappedId(resolvedId, EXTERNAL_SUFFIX)
  12790. ? getIgnoreTryCatchRequireStatementMode(source)
  12791. : { canConvertRequire: true, shouldRemoveRequire: false };
  12792. if (shouldRemoveRequire) {
  12793. if (usesReturnValue) {
  12794. magicString.overwrite(node.start, node.end, 'undefined');
  12795. } else {
  12796. magicString.remove(toBeRemoved.start, toBeRemoved.end);
  12797. }
  12798. } else if (canConvertRequire) {
  12799. needsImport = true;
  12800. if (isCommonJS === IS_WRAPPED_COMMONJS) {
  12801. magicString.overwrite(node.start, node.end, `${name}()`);
  12802. } else if (usesReturnValue) {
  12803. usesRequired = true;
  12804. magicString.overwrite(node.start, node.end, name);
  12805. } else {
  12806. magicString.remove(toBeRemoved.start, toBeRemoved.end);
  12807. }
  12808. }
  12809. }
  12810. if (needsImport) {
  12811. if (isCommonJS === IS_WRAPPED_COMMONJS) {
  12812. imports.push(`import { __require as ${name} } from ${JSON.stringify(resolvedId)}`);
  12813. } else {
  12814. imports.push(`import ${usesRequired ? `${name} from ` : ''}${JSON.stringify(resolvedId)}`);
  12815. }
  12816. }
  12817. }
  12818. }
  12819. function getGenerateRequireName() {
  12820. let uid = 0;
  12821. return (requires) => {
  12822. let name;
  12823. const hasNameConflict = ({ scope }) => scope.contains(name);
  12824. do {
  12825. name = `require$$${uid}`;
  12826. uid += 1;
  12827. } while (requires.some(hasNameConflict));
  12828. return name;
  12829. };
  12830. }
  12831. /* eslint-disable no-param-reassign, no-shadow, no-underscore-dangle, no-continue */
  12832. const exportsPattern = /^(?:module\.)?exports(?:\.([a-zA-Z_$][a-zA-Z_$0-9]*))?$/;
  12833. const functionType = /^(?:FunctionDeclaration|FunctionExpression|ArrowFunctionExpression)$/;
  12834. // There are three different types of CommonJS modules, described by their
  12835. // "exportMode":
  12836. // - exports: Only assignments to (module.)exports properties
  12837. // - replace: A single assignment to module.exports itself
  12838. // - module: Anything else
  12839. // Special cases:
  12840. // - usesRequireWrapper
  12841. // - isWrapped
  12842. async function transformCommonjs(
  12843. parse,
  12844. code,
  12845. id,
  12846. isEsModule,
  12847. ignoreGlobal,
  12848. ignoreRequire,
  12849. ignoreDynamicRequires,
  12850. getIgnoreTryCatchRequireStatementMode,
  12851. sourceMap,
  12852. isDynamicRequireModulesEnabled,
  12853. dynamicRequireModules,
  12854. commonDir,
  12855. astCache,
  12856. defaultIsModuleExports,
  12857. needsRequireWrapper,
  12858. resolveRequireSourcesAndUpdateMeta,
  12859. isRequired,
  12860. checkDynamicRequire,
  12861. commonjsMeta
  12862. ) {
  12863. const ast = astCache || tryParse(parse, code, id);
  12864. const magicString = new MagicString(code);
  12865. const uses = {
  12866. module: false,
  12867. exports: false,
  12868. global: false,
  12869. require: false
  12870. };
  12871. const virtualDynamicRequirePath =
  12872. isDynamicRequireModulesEnabled && getVirtualPathForDynamicRequirePath(dirname$1(id), commonDir);
  12873. let scope = attachScopes(ast, 'scope');
  12874. let lexicalDepth = 0;
  12875. let programDepth = 0;
  12876. let classBodyDepth = 0;
  12877. let currentTryBlockEnd = null;
  12878. let shouldWrap = false;
  12879. const globals = new Set();
  12880. // A conditionalNode is a node for which execution is not guaranteed. If such a node is a require
  12881. // or contains nested requires, those should be handled as function calls unless there is an
  12882. // unconditional require elsewhere.
  12883. let currentConditionalNodeEnd = null;
  12884. const conditionalNodes = new Set();
  12885. const { addRequireExpression, rewriteRequireExpressionsAndGetImportBlock } = getRequireHandlers();
  12886. // See which names are assigned to. This is necessary to prevent
  12887. // illegally replacing `var foo = require('foo')` with `import foo from 'foo'`,
  12888. // where `foo` is later reassigned. (This happens in the wild. CommonJS, sigh)
  12889. const reassignedNames = new Set();
  12890. const topLevelDeclarations = [];
  12891. const skippedNodes = new Set();
  12892. const moduleAccessScopes = new Set([scope]);
  12893. const exportsAccessScopes = new Set([scope]);
  12894. const moduleExportsAssignments = [];
  12895. let firstTopLevelModuleExportsAssignment = null;
  12896. const exportsAssignmentsByName = new Map();
  12897. const topLevelAssignments = new Set();
  12898. const topLevelDefineCompiledEsmExpressions = [];
  12899. const replacedGlobal = [];
  12900. const replacedDynamicRequires = [];
  12901. const importedVariables = new Set();
  12902. const indentExclusionRanges = [];
  12903. walk$3(ast, {
  12904. enter(node, parent) {
  12905. if (skippedNodes.has(node)) {
  12906. this.skip();
  12907. return;
  12908. }
  12909. if (currentTryBlockEnd !== null && node.start > currentTryBlockEnd) {
  12910. currentTryBlockEnd = null;
  12911. }
  12912. if (currentConditionalNodeEnd !== null && node.start > currentConditionalNodeEnd) {
  12913. currentConditionalNodeEnd = null;
  12914. }
  12915. if (currentConditionalNodeEnd === null && conditionalNodes.has(node)) {
  12916. currentConditionalNodeEnd = node.end;
  12917. }
  12918. programDepth += 1;
  12919. if (node.scope) ({ scope } = node);
  12920. if (functionType.test(node.type)) lexicalDepth += 1;
  12921. if (sourceMap) {
  12922. magicString.addSourcemapLocation(node.start);
  12923. magicString.addSourcemapLocation(node.end);
  12924. }
  12925. // eslint-disable-next-line default-case
  12926. switch (node.type) {
  12927. case 'AssignmentExpression':
  12928. if (node.left.type === 'MemberExpression') {
  12929. const flattened = getKeypath(node.left);
  12930. if (!flattened || scope.contains(flattened.name)) return;
  12931. const exportsPatternMatch = exportsPattern.exec(flattened.keypath);
  12932. if (!exportsPatternMatch || flattened.keypath === 'exports') return;
  12933. const [, exportName] = exportsPatternMatch;
  12934. uses[flattened.name] = true;
  12935. // we're dealing with `module.exports = ...` or `[module.]exports.foo = ...` –
  12936. if (flattened.keypath === 'module.exports') {
  12937. moduleExportsAssignments.push(node);
  12938. if (programDepth > 3) {
  12939. moduleAccessScopes.add(scope);
  12940. } else if (!firstTopLevelModuleExportsAssignment) {
  12941. firstTopLevelModuleExportsAssignment = node;
  12942. }
  12943. } else if (exportName === KEY_COMPILED_ESM) {
  12944. if (programDepth > 3) {
  12945. shouldWrap = true;
  12946. } else {
  12947. // The "type" is either "module" or "exports" to discern
  12948. // assignments to module.exports vs exports if needed
  12949. topLevelDefineCompiledEsmExpressions.push({ node, type: flattened.name });
  12950. }
  12951. } else {
  12952. const exportsAssignments = exportsAssignmentsByName.get(exportName) || {
  12953. nodes: [],
  12954. scopes: new Set()
  12955. };
  12956. exportsAssignments.nodes.push({ node, type: flattened.name });
  12957. exportsAssignments.scopes.add(scope);
  12958. exportsAccessScopes.add(scope);
  12959. exportsAssignmentsByName.set(exportName, exportsAssignments);
  12960. if (programDepth <= 3) {
  12961. topLevelAssignments.add(node);
  12962. }
  12963. }
  12964. skippedNodes.add(node.left);
  12965. } else {
  12966. for (const name of extractAssignedNames(node.left)) {
  12967. reassignedNames.add(name);
  12968. }
  12969. }
  12970. return;
  12971. case 'CallExpression': {
  12972. const defineCompiledEsmType = getDefineCompiledEsmType(node);
  12973. if (defineCompiledEsmType) {
  12974. if (programDepth === 3 && parent.type === 'ExpressionStatement') {
  12975. // skip special handling for [module.]exports until we know we render this
  12976. skippedNodes.add(node.arguments[0]);
  12977. topLevelDefineCompiledEsmExpressions.push({ node, type: defineCompiledEsmType });
  12978. } else {
  12979. shouldWrap = true;
  12980. }
  12981. return;
  12982. }
  12983. // Transform require.resolve
  12984. if (
  12985. isDynamicRequireModulesEnabled &&
  12986. node.callee.object &&
  12987. isRequire(node.callee.object, scope) &&
  12988. node.callee.property.name === 'resolve'
  12989. ) {
  12990. checkDynamicRequire(node.start);
  12991. uses.require = true;
  12992. const requireNode = node.callee.object;
  12993. replacedDynamicRequires.push(requireNode);
  12994. skippedNodes.add(node.callee);
  12995. return;
  12996. }
  12997. if (!isRequireExpression(node, scope)) {
  12998. const keypath = getKeypath(node.callee);
  12999. if (keypath && importedVariables.has(keypath.name)) {
  13000. // Heuristic to deoptimize requires after a required function has been called
  13001. currentConditionalNodeEnd = Infinity;
  13002. }
  13003. return;
  13004. }
  13005. skippedNodes.add(node.callee);
  13006. uses.require = true;
  13007. if (hasDynamicArguments(node)) {
  13008. if (isDynamicRequireModulesEnabled) {
  13009. checkDynamicRequire(node.start);
  13010. }
  13011. if (!ignoreDynamicRequires) {
  13012. replacedDynamicRequires.push(node.callee);
  13013. }
  13014. return;
  13015. }
  13016. const requireStringArg = getRequireStringArg(node);
  13017. if (!ignoreRequire(requireStringArg)) {
  13018. const usesReturnValue = parent.type !== 'ExpressionStatement';
  13019. const toBeRemoved =
  13020. parent.type === 'ExpressionStatement' &&
  13021. (!currentConditionalNodeEnd ||
  13022. // We should completely remove requires directly in a try-catch
  13023. // so that Rollup can remove up the try-catch
  13024. (currentTryBlockEnd !== null && currentTryBlockEnd < currentConditionalNodeEnd))
  13025. ? parent
  13026. : node;
  13027. addRequireExpression(
  13028. requireStringArg,
  13029. node,
  13030. scope,
  13031. usesReturnValue,
  13032. currentTryBlockEnd !== null,
  13033. currentConditionalNodeEnd !== null,
  13034. toBeRemoved
  13035. );
  13036. if (parent.type === 'VariableDeclarator' && parent.id.type === 'Identifier') {
  13037. for (const name of extractAssignedNames(parent.id)) {
  13038. importedVariables.add(name);
  13039. }
  13040. }
  13041. }
  13042. return;
  13043. }
  13044. case 'ClassBody':
  13045. classBodyDepth += 1;
  13046. return;
  13047. case 'ConditionalExpression':
  13048. case 'IfStatement':
  13049. // skip dead branches
  13050. if (isFalsy(node.test)) {
  13051. skippedNodes.add(node.consequent);
  13052. } else if (isTruthy(node.test)) {
  13053. if (node.alternate) {
  13054. skippedNodes.add(node.alternate);
  13055. }
  13056. } else {
  13057. conditionalNodes.add(node.consequent);
  13058. if (node.alternate) {
  13059. conditionalNodes.add(node.alternate);
  13060. }
  13061. }
  13062. return;
  13063. case 'ArrowFunctionExpression':
  13064. case 'FunctionDeclaration':
  13065. case 'FunctionExpression':
  13066. // requires in functions should be conditional unless it is an IIFE
  13067. if (
  13068. currentConditionalNodeEnd === null &&
  13069. !(parent.type === 'CallExpression' && parent.callee === node)
  13070. ) {
  13071. currentConditionalNodeEnd = node.end;
  13072. }
  13073. return;
  13074. case 'Identifier': {
  13075. const { name } = node;
  13076. if (
  13077. !isReference(node, parent) ||
  13078. scope.contains(name) ||
  13079. (parent.type === 'PropertyDefinition' && parent.key === node)
  13080. )
  13081. return;
  13082. switch (name) {
  13083. case 'require':
  13084. uses.require = true;
  13085. if (isNodeRequirePropertyAccess(parent)) {
  13086. return;
  13087. }
  13088. if (!ignoreDynamicRequires) {
  13089. if (isShorthandProperty(parent)) {
  13090. // as key and value are the same object, isReference regards
  13091. // both as references, so we need to skip now
  13092. skippedNodes.add(parent.value);
  13093. magicString.prependRight(node.start, 'require: ');
  13094. }
  13095. replacedDynamicRequires.push(node);
  13096. }
  13097. return;
  13098. case 'module':
  13099. case 'exports':
  13100. shouldWrap = true;
  13101. uses[name] = true;
  13102. return;
  13103. case 'global':
  13104. uses.global = true;
  13105. if (!ignoreGlobal) {
  13106. replacedGlobal.push(node);
  13107. }
  13108. return;
  13109. case 'define':
  13110. magicString.overwrite(node.start, node.end, 'undefined', {
  13111. storeName: true
  13112. });
  13113. return;
  13114. default:
  13115. globals.add(name);
  13116. return;
  13117. }
  13118. }
  13119. case 'LogicalExpression':
  13120. // skip dead branches
  13121. if (node.operator === '&&') {
  13122. if (isFalsy(node.left)) {
  13123. skippedNodes.add(node.right);
  13124. } else if (!isTruthy(node.left)) {
  13125. conditionalNodes.add(node.right);
  13126. }
  13127. } else if (node.operator === '||') {
  13128. if (isTruthy(node.left)) {
  13129. skippedNodes.add(node.right);
  13130. } else if (!isFalsy(node.left)) {
  13131. conditionalNodes.add(node.right);
  13132. }
  13133. }
  13134. return;
  13135. case 'MemberExpression':
  13136. if (!isDynamicRequireModulesEnabled && isModuleRequire(node, scope)) {
  13137. uses.require = true;
  13138. replacedDynamicRequires.push(node);
  13139. skippedNodes.add(node.object);
  13140. skippedNodes.add(node.property);
  13141. }
  13142. return;
  13143. case 'ReturnStatement':
  13144. // if top-level return, we need to wrap it
  13145. if (lexicalDepth === 0) {
  13146. shouldWrap = true;
  13147. }
  13148. return;
  13149. case 'ThisExpression':
  13150. // rewrite top-level `this` as `commonjsHelpers.commonjsGlobal`
  13151. if (lexicalDepth === 0 && !classBodyDepth) {
  13152. uses.global = true;
  13153. if (!ignoreGlobal) {
  13154. replacedGlobal.push(node);
  13155. }
  13156. }
  13157. return;
  13158. case 'TryStatement':
  13159. if (currentTryBlockEnd === null) {
  13160. currentTryBlockEnd = node.block.end;
  13161. }
  13162. if (currentConditionalNodeEnd === null) {
  13163. currentConditionalNodeEnd = node.end;
  13164. }
  13165. return;
  13166. case 'UnaryExpression':
  13167. // rewrite `typeof module`, `typeof module.exports` and `typeof exports` (https://github.com/rollup/rollup-plugin-commonjs/issues/151)
  13168. if (node.operator === 'typeof') {
  13169. const flattened = getKeypath(node.argument);
  13170. if (!flattened) return;
  13171. if (scope.contains(flattened.name)) return;
  13172. if (
  13173. !isEsModule &&
  13174. (flattened.keypath === 'module.exports' ||
  13175. flattened.keypath === 'module' ||
  13176. flattened.keypath === 'exports')
  13177. ) {
  13178. magicString.overwrite(node.start, node.end, `'object'`, {
  13179. storeName: false
  13180. });
  13181. }
  13182. }
  13183. return;
  13184. case 'VariableDeclaration':
  13185. if (!scope.parent) {
  13186. topLevelDeclarations.push(node);
  13187. }
  13188. return;
  13189. case 'TemplateElement':
  13190. if (node.value.raw.includes('\n')) {
  13191. indentExclusionRanges.push([node.start, node.end]);
  13192. }
  13193. }
  13194. },
  13195. leave(node) {
  13196. programDepth -= 1;
  13197. if (node.scope) scope = scope.parent;
  13198. if (functionType.test(node.type)) lexicalDepth -= 1;
  13199. if (node.type === 'ClassBody') classBodyDepth -= 1;
  13200. }
  13201. });
  13202. const nameBase = getName(id);
  13203. const exportsName = deconflict([...exportsAccessScopes], globals, nameBase);
  13204. const moduleName = deconflict([...moduleAccessScopes], globals, `${nameBase}Module`);
  13205. const requireName = deconflict([scope], globals, `require${capitalize(nameBase)}`);
  13206. const isRequiredName = deconflict([scope], globals, `hasRequired${capitalize(nameBase)}`);
  13207. const helpersName = deconflict([scope], globals, 'commonjsHelpers');
  13208. const dynamicRequireName =
  13209. replacedDynamicRequires.length > 0 &&
  13210. deconflict(
  13211. [scope],
  13212. globals,
  13213. isDynamicRequireModulesEnabled ? CREATE_COMMONJS_REQUIRE_EXPORT : COMMONJS_REQUIRE_EXPORT
  13214. );
  13215. const deconflictedExportNames = Object.create(null);
  13216. for (const [exportName, { scopes }] of exportsAssignmentsByName) {
  13217. deconflictedExportNames[exportName] = deconflict([...scopes], globals, exportName);
  13218. }
  13219. for (const node of replacedGlobal) {
  13220. magicString.overwrite(node.start, node.end, `${helpersName}.commonjsGlobal`, {
  13221. storeName: true
  13222. });
  13223. }
  13224. for (const node of replacedDynamicRequires) {
  13225. magicString.overwrite(
  13226. node.start,
  13227. node.end,
  13228. isDynamicRequireModulesEnabled
  13229. ? `${dynamicRequireName}(${JSON.stringify(virtualDynamicRequirePath)})`
  13230. : dynamicRequireName,
  13231. {
  13232. contentOnly: true,
  13233. storeName: true
  13234. }
  13235. );
  13236. }
  13237. // We cannot wrap ES/mixed modules
  13238. shouldWrap = !isEsModule && (shouldWrap || (uses.exports && moduleExportsAssignments.length > 0));
  13239. if (
  13240. !(
  13241. shouldWrap ||
  13242. isRequired ||
  13243. needsRequireWrapper ||
  13244. uses.module ||
  13245. uses.exports ||
  13246. uses.require ||
  13247. topLevelDefineCompiledEsmExpressions.length > 0
  13248. ) &&
  13249. (ignoreGlobal || !uses.global)
  13250. ) {
  13251. return { meta: { commonjs: { isCommonJS: false } } };
  13252. }
  13253. let leadingComment = '';
  13254. if (code.startsWith('/*')) {
  13255. const commentEnd = code.indexOf('*/', 2) + 2;
  13256. leadingComment = `${code.slice(0, commentEnd)}\n`;
  13257. magicString.remove(0, commentEnd).trim();
  13258. }
  13259. let shebang = '';
  13260. if (code.startsWith('#!')) {
  13261. const shebangEndPosition = code.indexOf('\n') + 1;
  13262. shebang = code.slice(0, shebangEndPosition);
  13263. magicString.remove(0, shebangEndPosition).trim();
  13264. }
  13265. const exportMode = isEsModule
  13266. ? 'none'
  13267. : shouldWrap
  13268. ? uses.module
  13269. ? 'module'
  13270. : 'exports'
  13271. : firstTopLevelModuleExportsAssignment
  13272. ? exportsAssignmentsByName.size === 0 && topLevelDefineCompiledEsmExpressions.length === 0
  13273. ? 'replace'
  13274. : 'module'
  13275. : moduleExportsAssignments.length === 0
  13276. ? 'exports'
  13277. : 'module';
  13278. const exportedExportsName =
  13279. exportMode === 'module' ? deconflict([], globals, `${nameBase}Exports`) : exportsName;
  13280. const importBlock = await rewriteRequireExpressionsAndGetImportBlock(
  13281. magicString,
  13282. topLevelDeclarations,
  13283. reassignedNames,
  13284. helpersName,
  13285. dynamicRequireName,
  13286. moduleName,
  13287. exportsName,
  13288. id,
  13289. exportMode,
  13290. resolveRequireSourcesAndUpdateMeta,
  13291. needsRequireWrapper,
  13292. isEsModule,
  13293. isDynamicRequireModulesEnabled,
  13294. getIgnoreTryCatchRequireStatementMode,
  13295. commonjsMeta
  13296. );
  13297. const usesRequireWrapper = commonjsMeta.isCommonJS === IS_WRAPPED_COMMONJS;
  13298. const exportBlock = isEsModule
  13299. ? ''
  13300. : rewriteExportsAndGetExportsBlock(
  13301. magicString,
  13302. moduleName,
  13303. exportsName,
  13304. exportedExportsName,
  13305. shouldWrap,
  13306. moduleExportsAssignments,
  13307. firstTopLevelModuleExportsAssignment,
  13308. exportsAssignmentsByName,
  13309. topLevelAssignments,
  13310. topLevelDefineCompiledEsmExpressions,
  13311. deconflictedExportNames,
  13312. code,
  13313. helpersName,
  13314. exportMode,
  13315. defaultIsModuleExports,
  13316. usesRequireWrapper,
  13317. requireName
  13318. );
  13319. if (shouldWrap) {
  13320. wrapCode(magicString, uses, moduleName, exportsName, indentExclusionRanges);
  13321. }
  13322. if (usesRequireWrapper) {
  13323. magicString.trim().indent('\t', {
  13324. exclude: indentExclusionRanges
  13325. });
  13326. const exported = exportMode === 'module' ? `${moduleName}.exports` : exportsName;
  13327. magicString.prepend(
  13328. `var ${isRequiredName};
  13329. function ${requireName} () {
  13330. \tif (${isRequiredName}) return ${exported};
  13331. \t${isRequiredName} = 1;
  13332. `
  13333. ).append(`
  13334. \treturn ${exported};
  13335. }`);
  13336. if (exportMode === 'replace') {
  13337. magicString.prepend(`var ${exportsName};\n`);
  13338. }
  13339. }
  13340. magicString
  13341. .trim()
  13342. .prepend(shebang + leadingComment + importBlock)
  13343. .append(exportBlock);
  13344. return {
  13345. code: magicString.toString(),
  13346. map: sourceMap ? magicString.generateMap() : null,
  13347. syntheticNamedExports: isEsModule || usesRequireWrapper ? false : '__moduleExports',
  13348. meta: { commonjs: { ...commonjsMeta, shebang } }
  13349. };
  13350. }
  13351. const PLUGIN_NAME = 'commonjs';
  13352. function commonjs(options = {}) {
  13353. const {
  13354. ignoreGlobal,
  13355. ignoreDynamicRequires,
  13356. requireReturnsDefault: requireReturnsDefaultOption,
  13357. defaultIsModuleExports: defaultIsModuleExportsOption,
  13358. esmExternals
  13359. } = options;
  13360. const extensions = options.extensions || ['.js'];
  13361. const filter = createFilter$1(options.include, options.exclude);
  13362. const isPossibleCjsId = (id) => {
  13363. const extName = extname(id);
  13364. return extName === '.cjs' || (extensions.includes(extName) && filter(id));
  13365. };
  13366. const { strictRequiresFilter, detectCyclesAndConditional } = getStrictRequiresFilter(options);
  13367. const getRequireReturnsDefault =
  13368. typeof requireReturnsDefaultOption === 'function'
  13369. ? requireReturnsDefaultOption
  13370. : () => requireReturnsDefaultOption;
  13371. let esmExternalIds;
  13372. const isEsmExternal =
  13373. typeof esmExternals === 'function'
  13374. ? esmExternals
  13375. : Array.isArray(esmExternals)
  13376. ? ((esmExternalIds = new Set(esmExternals)), (id) => esmExternalIds.has(id))
  13377. : () => esmExternals;
  13378. const getDefaultIsModuleExports =
  13379. typeof defaultIsModuleExportsOption === 'function'
  13380. ? defaultIsModuleExportsOption
  13381. : () =>
  13382. typeof defaultIsModuleExportsOption === 'boolean' ? defaultIsModuleExportsOption : 'auto';
  13383. const dynamicRequireRoot =
  13384. typeof options.dynamicRequireRoot === 'string'
  13385. ? resolve$3(options.dynamicRequireRoot)
  13386. : process.cwd();
  13387. const { commonDir, dynamicRequireModules } = getDynamicRequireModules(
  13388. options.dynamicRequireTargets,
  13389. dynamicRequireRoot
  13390. );
  13391. const isDynamicRequireModulesEnabled = dynamicRequireModules.size > 0;
  13392. const ignoreRequire =
  13393. typeof options.ignore === 'function'
  13394. ? options.ignore
  13395. : Array.isArray(options.ignore)
  13396. ? (id) => options.ignore.includes(id)
  13397. : () => false;
  13398. const getIgnoreTryCatchRequireStatementMode = (id) => {
  13399. const mode =
  13400. typeof options.ignoreTryCatch === 'function'
  13401. ? options.ignoreTryCatch(id)
  13402. : Array.isArray(options.ignoreTryCatch)
  13403. ? options.ignoreTryCatch.includes(id)
  13404. : typeof options.ignoreTryCatch !== 'undefined'
  13405. ? options.ignoreTryCatch
  13406. : true;
  13407. return {
  13408. canConvertRequire: mode !== 'remove' && mode !== true,
  13409. shouldRemoveRequire: mode === 'remove'
  13410. };
  13411. };
  13412. const { currentlyResolving, resolveId } = getResolveId(extensions, isPossibleCjsId);
  13413. const sourceMap = options.sourceMap !== false;
  13414. // Initialized in buildStart
  13415. let requireResolver;
  13416. function transformAndCheckExports(code, id) {
  13417. const normalizedId = normalizePathSlashes(id);
  13418. const { isEsModule, hasDefaultExport, hasNamedExports, ast } = analyzeTopLevelStatements(
  13419. this.parse,
  13420. code,
  13421. id
  13422. );
  13423. const commonjsMeta = this.getModuleInfo(id).meta.commonjs || {};
  13424. if (hasDefaultExport) {
  13425. commonjsMeta.hasDefaultExport = true;
  13426. }
  13427. if (hasNamedExports) {
  13428. commonjsMeta.hasNamedExports = true;
  13429. }
  13430. if (
  13431. !dynamicRequireModules.has(normalizedId) &&
  13432. (!(hasCjsKeywords(code, ignoreGlobal) || requireResolver.isRequiredId(id)) ||
  13433. (isEsModule && !options.transformMixedEsModules))
  13434. ) {
  13435. commonjsMeta.isCommonJS = false;
  13436. return { meta: { commonjs: commonjsMeta } };
  13437. }
  13438. const needsRequireWrapper =
  13439. !isEsModule && (dynamicRequireModules.has(normalizedId) || strictRequiresFilter(id));
  13440. const checkDynamicRequire = (position) => {
  13441. const normalizedDynamicRequireRoot = normalizePathSlashes(dynamicRequireRoot);
  13442. if (normalizedId.indexOf(normalizedDynamicRequireRoot) !== 0) {
  13443. this.error(
  13444. {
  13445. code: 'DYNAMIC_REQUIRE_OUTSIDE_ROOT',
  13446. normalizedId,
  13447. normalizedDynamicRequireRoot,
  13448. message: `"${normalizedId}" contains dynamic require statements but it is not within the current dynamicRequireRoot "${normalizedDynamicRequireRoot}". You should set dynamicRequireRoot to "${dirname$1(
  13449. normalizedId
  13450. )}" or one of its parent directories.`
  13451. },
  13452. position
  13453. );
  13454. }
  13455. };
  13456. return transformCommonjs(
  13457. this.parse,
  13458. code,
  13459. id,
  13460. isEsModule,
  13461. ignoreGlobal || isEsModule,
  13462. ignoreRequire,
  13463. ignoreDynamicRequires && !isDynamicRequireModulesEnabled,
  13464. getIgnoreTryCatchRequireStatementMode,
  13465. sourceMap,
  13466. isDynamicRequireModulesEnabled,
  13467. dynamicRequireModules,
  13468. commonDir,
  13469. ast,
  13470. getDefaultIsModuleExports(id),
  13471. needsRequireWrapper,
  13472. requireResolver.resolveRequireSourcesAndUpdateMeta(this),
  13473. requireResolver.isRequiredId(id),
  13474. checkDynamicRequire,
  13475. commonjsMeta
  13476. );
  13477. }
  13478. return {
  13479. name: PLUGIN_NAME,
  13480. version: version$2,
  13481. options(rawOptions) {
  13482. // We inject the resolver in the beginning so that "catch-all-resolver" like node-resolver
  13483. // do not prevent our plugin from resolving entry points ot proxies.
  13484. const plugins = Array.isArray(rawOptions.plugins)
  13485. ? [...rawOptions.plugins]
  13486. : rawOptions.plugins
  13487. ? [rawOptions.plugins]
  13488. : [];
  13489. plugins.unshift({
  13490. name: 'commonjs--resolver',
  13491. resolveId
  13492. });
  13493. return { ...rawOptions, plugins };
  13494. },
  13495. buildStart({ plugins }) {
  13496. validateVersion(this.meta.rollupVersion, peerDependencies.rollup, 'rollup');
  13497. const nodeResolve = plugins.find(({ name }) => name === 'node-resolve');
  13498. if (nodeResolve) {
  13499. validateVersion(nodeResolve.version, '^13.0.6', '@rollup/plugin-node-resolve');
  13500. }
  13501. if (options.namedExports != null) {
  13502. this.warn(
  13503. 'The namedExports option from "@rollup/plugin-commonjs" is deprecated. Named exports are now handled automatically.'
  13504. );
  13505. }
  13506. requireResolver = getRequireResolver(
  13507. extensions,
  13508. detectCyclesAndConditional,
  13509. currentlyResolving
  13510. );
  13511. },
  13512. buildEnd() {
  13513. if (options.strictRequires === 'debug') {
  13514. const wrappedIds = requireResolver.getWrappedIds();
  13515. if (wrappedIds.length) {
  13516. this.warn({
  13517. code: 'WRAPPED_IDS',
  13518. ids: wrappedIds,
  13519. message: `The commonjs plugin automatically wrapped the following files:\n[\n${wrappedIds
  13520. .map((id) => `\t${JSON.stringify(relative$1(process.cwd(), id))}`)
  13521. .join(',\n')}\n]`
  13522. });
  13523. } else {
  13524. this.warn({
  13525. code: 'WRAPPED_IDS',
  13526. ids: wrappedIds,
  13527. message: 'The commonjs plugin did not wrap any files.'
  13528. });
  13529. }
  13530. }
  13531. },
  13532. load(id) {
  13533. if (id === HELPERS_ID) {
  13534. return getHelpersModule();
  13535. }
  13536. if (isWrappedId(id, MODULE_SUFFIX)) {
  13537. const name = getName(unwrapId(id, MODULE_SUFFIX));
  13538. return {
  13539. code: `var ${name} = {exports: {}}; export {${name} as __module}`,
  13540. meta: { commonjs: { isCommonJS: false } }
  13541. };
  13542. }
  13543. if (isWrappedId(id, EXPORTS_SUFFIX)) {
  13544. const name = getName(unwrapId(id, EXPORTS_SUFFIX));
  13545. return {
  13546. code: `var ${name} = {}; export {${name} as __exports}`,
  13547. meta: { commonjs: { isCommonJS: false } }
  13548. };
  13549. }
  13550. if (isWrappedId(id, EXTERNAL_SUFFIX)) {
  13551. const actualId = unwrapId(id, EXTERNAL_SUFFIX);
  13552. return getUnknownRequireProxy(
  13553. actualId,
  13554. isEsmExternal(actualId) ? getRequireReturnsDefault(actualId) : true
  13555. );
  13556. }
  13557. // entry suffix is just appended to not mess up relative external resolution
  13558. if (id.endsWith(ENTRY_SUFFIX)) {
  13559. const acutalId = id.slice(0, -ENTRY_SUFFIX.length);
  13560. const {
  13561. meta: { commonjs: commonjsMeta }
  13562. } = this.getModuleInfo(acutalId);
  13563. const shebang = commonjsMeta?.shebang ?? '';
  13564. return getEntryProxy(
  13565. acutalId,
  13566. getDefaultIsModuleExports(acutalId),
  13567. this.getModuleInfo,
  13568. shebang
  13569. );
  13570. }
  13571. if (isWrappedId(id, ES_IMPORT_SUFFIX)) {
  13572. const actualId = unwrapId(id, ES_IMPORT_SUFFIX);
  13573. return getEsImportProxy(actualId, getDefaultIsModuleExports(actualId));
  13574. }
  13575. if (id === DYNAMIC_MODULES_ID) {
  13576. return getDynamicModuleRegistry(
  13577. isDynamicRequireModulesEnabled,
  13578. dynamicRequireModules,
  13579. commonDir,
  13580. ignoreDynamicRequires
  13581. );
  13582. }
  13583. if (isWrappedId(id, PROXY_SUFFIX)) {
  13584. const actualId = unwrapId(id, PROXY_SUFFIX);
  13585. return getStaticRequireProxy(actualId, getRequireReturnsDefault(actualId), this.load);
  13586. }
  13587. return null;
  13588. },
  13589. shouldTransformCachedModule(...args) {
  13590. return requireResolver.shouldTransformCachedModule.call(this, ...args);
  13591. },
  13592. transform(code, id) {
  13593. if (!isPossibleCjsId(id)) return null;
  13594. try {
  13595. return transformAndCheckExports.call(this, code, id);
  13596. } catch (err) {
  13597. return this.error(err, err.pos);
  13598. }
  13599. }
  13600. };
  13601. }
  13602. // Matches the scheme of a URL, eg "http://"
  13603. const schemeRegex = /^[\w+.-]+:\/\//;
  13604. /**
  13605. * Matches the parts of a URL:
  13606. * 1. Scheme, including ":", guaranteed.
  13607. * 2. User/password, including "@", optional.
  13608. * 3. Host, guaranteed.
  13609. * 4. Port, including ":", optional.
  13610. * 5. Path, including "/", optional.
  13611. * 6. Query, including "?", optional.
  13612. * 7. Hash, including "#", optional.
  13613. */
  13614. const urlRegex = /^([\w+.-]+:)\/\/([^@/#?]*@)?([^:/#?]*)(:\d+)?(\/[^#?]*)?(\?[^#]*)?(#.*)?/;
  13615. /**
  13616. * File URLs are weird. They dont' need the regular `//` in the scheme, they may or may not start
  13617. * with a leading `/`, they can have a domain (but only if they don't start with a Windows drive).
  13618. *
  13619. * 1. Host, optional.
  13620. * 2. Path, which may include "/", guaranteed.
  13621. * 3. Query, including "?", optional.
  13622. * 4. Hash, including "#", optional.
  13623. */
  13624. const fileRegex = /^file:(?:\/\/((?![a-z]:)[^/#?]*)?)?(\/?[^#?]*)(\?[^#]*)?(#.*)?/i;
  13625. function isAbsoluteUrl(input) {
  13626. return schemeRegex.test(input);
  13627. }
  13628. function isSchemeRelativeUrl(input) {
  13629. return input.startsWith('//');
  13630. }
  13631. function isAbsolutePath(input) {
  13632. return input.startsWith('/');
  13633. }
  13634. function isFileUrl(input) {
  13635. return input.startsWith('file:');
  13636. }
  13637. function isRelative(input) {
  13638. return /^[.?#]/.test(input);
  13639. }
  13640. function parseAbsoluteUrl(input) {
  13641. const match = urlRegex.exec(input);
  13642. return makeUrl(match[1], match[2] || '', match[3], match[4] || '', match[5] || '/', match[6] || '', match[7] || '');
  13643. }
  13644. function parseFileUrl(input) {
  13645. const match = fileRegex.exec(input);
  13646. const path = match[2];
  13647. return makeUrl('file:', '', match[1] || '', '', isAbsolutePath(path) ? path : '/' + path, match[3] || '', match[4] || '');
  13648. }
  13649. function makeUrl(scheme, user, host, port, path, query, hash) {
  13650. return {
  13651. scheme,
  13652. user,
  13653. host,
  13654. port,
  13655. path,
  13656. query,
  13657. hash,
  13658. type: 7 /* Absolute */,
  13659. };
  13660. }
  13661. function parseUrl$3(input) {
  13662. if (isSchemeRelativeUrl(input)) {
  13663. const url = parseAbsoluteUrl('http:' + input);
  13664. url.scheme = '';
  13665. url.type = 6 /* SchemeRelative */;
  13666. return url;
  13667. }
  13668. if (isAbsolutePath(input)) {
  13669. const url = parseAbsoluteUrl('http://foo.com' + input);
  13670. url.scheme = '';
  13671. url.host = '';
  13672. url.type = 5 /* AbsolutePath */;
  13673. return url;
  13674. }
  13675. if (isFileUrl(input))
  13676. return parseFileUrl(input);
  13677. if (isAbsoluteUrl(input))
  13678. return parseAbsoluteUrl(input);
  13679. const url = parseAbsoluteUrl('http://foo.com/' + input);
  13680. url.scheme = '';
  13681. url.host = '';
  13682. url.type = input
  13683. ? input.startsWith('?')
  13684. ? 3 /* Query */
  13685. : input.startsWith('#')
  13686. ? 2 /* Hash */
  13687. : 4 /* RelativePath */
  13688. : 1 /* Empty */;
  13689. return url;
  13690. }
  13691. function stripPathFilename(path) {
  13692. // If a path ends with a parent directory "..", then it's a relative path with excess parent
  13693. // paths. It's not a file, so we can't strip it.
  13694. if (path.endsWith('/..'))
  13695. return path;
  13696. const index = path.lastIndexOf('/');
  13697. return path.slice(0, index + 1);
  13698. }
  13699. function mergePaths(url, base) {
  13700. normalizePath$4(base, base.type);
  13701. // If the path is just a "/", then it was an empty path to begin with (remember, we're a relative
  13702. // path).
  13703. if (url.path === '/') {
  13704. url.path = base.path;
  13705. }
  13706. else {
  13707. // Resolution happens relative to the base path's directory, not the file.
  13708. url.path = stripPathFilename(base.path) + url.path;
  13709. }
  13710. }
  13711. /**
  13712. * The path can have empty directories "//", unneeded parents "foo/..", or current directory
  13713. * "foo/.". We need to normalize to a standard representation.
  13714. */
  13715. function normalizePath$4(url, type) {
  13716. const rel = type <= 4 /* RelativePath */;
  13717. const pieces = url.path.split('/');
  13718. // We need to preserve the first piece always, so that we output a leading slash. The item at
  13719. // pieces[0] is an empty string.
  13720. let pointer = 1;
  13721. // Positive is the number of real directories we've output, used for popping a parent directory.
  13722. // Eg, "foo/bar/.." will have a positive 2, and we can decrement to be left with just "foo".
  13723. let positive = 0;
  13724. // We need to keep a trailing slash if we encounter an empty directory (eg, splitting "foo/" will
  13725. // generate `["foo", ""]` pieces). And, if we pop a parent directory. But once we encounter a
  13726. // real directory, we won't need to append, unless the other conditions happen again.
  13727. let addTrailingSlash = false;
  13728. for (let i = 1; i < pieces.length; i++) {
  13729. const piece = pieces[i];
  13730. // An empty directory, could be a trailing slash, or just a double "//" in the path.
  13731. if (!piece) {
  13732. addTrailingSlash = true;
  13733. continue;
  13734. }
  13735. // If we encounter a real directory, then we don't need to append anymore.
  13736. addTrailingSlash = false;
  13737. // A current directory, which we can always drop.
  13738. if (piece === '.')
  13739. continue;
  13740. // A parent directory, we need to see if there are any real directories we can pop. Else, we
  13741. // have an excess of parents, and we'll need to keep the "..".
  13742. if (piece === '..') {
  13743. if (positive) {
  13744. addTrailingSlash = true;
  13745. positive--;
  13746. pointer--;
  13747. }
  13748. else if (rel) {
  13749. // If we're in a relativePath, then we need to keep the excess parents. Else, in an absolute
  13750. // URL, protocol relative URL, or an absolute path, we don't need to keep excess.
  13751. pieces[pointer++] = piece;
  13752. }
  13753. continue;
  13754. }
  13755. // We've encountered a real directory. Move it to the next insertion pointer, which accounts for
  13756. // any popped or dropped directories.
  13757. pieces[pointer++] = piece;
  13758. positive++;
  13759. }
  13760. let path = '';
  13761. for (let i = 1; i < pointer; i++) {
  13762. path += '/' + pieces[i];
  13763. }
  13764. if (!path || (addTrailingSlash && !path.endsWith('/..'))) {
  13765. path += '/';
  13766. }
  13767. url.path = path;
  13768. }
  13769. /**
  13770. * Attempts to resolve `input` URL/path relative to `base`.
  13771. */
  13772. function resolve$2(input, base) {
  13773. if (!input && !base)
  13774. return '';
  13775. const url = parseUrl$3(input);
  13776. let inputType = url.type;
  13777. if (base && inputType !== 7 /* Absolute */) {
  13778. const baseUrl = parseUrl$3(base);
  13779. const baseType = baseUrl.type;
  13780. switch (inputType) {
  13781. case 1 /* Empty */:
  13782. url.hash = baseUrl.hash;
  13783. // fall through
  13784. case 2 /* Hash */:
  13785. url.query = baseUrl.query;
  13786. // fall through
  13787. case 3 /* Query */:
  13788. case 4 /* RelativePath */:
  13789. mergePaths(url, baseUrl);
  13790. // fall through
  13791. case 5 /* AbsolutePath */:
  13792. // The host, user, and port are joined, you can't copy one without the others.
  13793. url.user = baseUrl.user;
  13794. url.host = baseUrl.host;
  13795. url.port = baseUrl.port;
  13796. // fall through
  13797. case 6 /* SchemeRelative */:
  13798. // The input doesn't have a schema at least, so we need to copy at least that over.
  13799. url.scheme = baseUrl.scheme;
  13800. }
  13801. if (baseType > inputType)
  13802. inputType = baseType;
  13803. }
  13804. normalizePath$4(url, inputType);
  13805. const queryHash = url.query + url.hash;
  13806. switch (inputType) {
  13807. // This is impossible, because of the empty checks at the start of the function.
  13808. // case UrlType.Empty:
  13809. case 2 /* Hash */:
  13810. case 3 /* Query */:
  13811. return queryHash;
  13812. case 4 /* RelativePath */: {
  13813. // The first char is always a "/", and we need it to be relative.
  13814. const path = url.path.slice(1);
  13815. if (!path)
  13816. return queryHash || '.';
  13817. if (isRelative(base || input) && !isRelative(path)) {
  13818. // If base started with a leading ".", or there is no base and input started with a ".",
  13819. // then we need to ensure that the relative path starts with a ".". We don't know if
  13820. // relative starts with a "..", though, so check before prepending.
  13821. return './' + path + queryHash;
  13822. }
  13823. return path + queryHash;
  13824. }
  13825. case 5 /* AbsolutePath */:
  13826. return url.path + queryHash;
  13827. default:
  13828. return url.scheme + '//' + url.user + url.host + url.port + url.path + queryHash;
  13829. }
  13830. }
  13831. function resolve$1(input, base) {
  13832. // The base is always treated as a directory, if it's not empty.
  13833. // https://github.com/mozilla/source-map/blob/8cb3ee57/lib/util.js#L327
  13834. // https://github.com/chromium/chromium/blob/da4adbb3/third_party/blink/renderer/devtools/front_end/sdk/SourceMap.js#L400-L401
  13835. if (base && !base.endsWith('/'))
  13836. base += '/';
  13837. return resolve$2(input, base);
  13838. }
  13839. /**
  13840. * Removes everything after the last "/", but leaves the slash.
  13841. */
  13842. function stripFilename(path) {
  13843. if (!path)
  13844. return '';
  13845. const index = path.lastIndexOf('/');
  13846. return path.slice(0, index + 1);
  13847. }
  13848. const COLUMN$1 = 0;
  13849. const SOURCES_INDEX$1 = 1;
  13850. const SOURCE_LINE$1 = 2;
  13851. const SOURCE_COLUMN$1 = 3;
  13852. const NAMES_INDEX$1 = 4;
  13853. function maybeSort(mappings, owned) {
  13854. const unsortedIndex = nextUnsortedSegmentLine(mappings, 0);
  13855. if (unsortedIndex === mappings.length)
  13856. return mappings;
  13857. // If we own the array (meaning we parsed it from JSON), then we're free to directly mutate it. If
  13858. // not, we do not want to modify the consumer's input array.
  13859. if (!owned)
  13860. mappings = mappings.slice();
  13861. for (let i = unsortedIndex; i < mappings.length; i = nextUnsortedSegmentLine(mappings, i + 1)) {
  13862. mappings[i] = sortSegments(mappings[i], owned);
  13863. }
  13864. return mappings;
  13865. }
  13866. function nextUnsortedSegmentLine(mappings, start) {
  13867. for (let i = start; i < mappings.length; i++) {
  13868. if (!isSorted(mappings[i]))
  13869. return i;
  13870. }
  13871. return mappings.length;
  13872. }
  13873. function isSorted(line) {
  13874. for (let j = 1; j < line.length; j++) {
  13875. if (line[j][COLUMN$1] < line[j - 1][COLUMN$1]) {
  13876. return false;
  13877. }
  13878. }
  13879. return true;
  13880. }
  13881. function sortSegments(line, owned) {
  13882. if (!owned)
  13883. line = line.slice();
  13884. return line.sort(sortComparator);
  13885. }
  13886. function sortComparator(a, b) {
  13887. return a[COLUMN$1] - b[COLUMN$1];
  13888. }
  13889. let found = false;
  13890. /**
  13891. * A binary search implementation that returns the index if a match is found.
  13892. * If no match is found, then the left-index (the index associated with the item that comes just
  13893. * before the desired index) is returned. To maintain proper sort order, a splice would happen at
  13894. * the next index:
  13895. *
  13896. * ```js
  13897. * const array = [1, 3];
  13898. * const needle = 2;
  13899. * const index = binarySearch(array, needle, (item, needle) => item - needle);
  13900. *
  13901. * assert.equal(index, 0);
  13902. * array.splice(index + 1, 0, needle);
  13903. * assert.deepEqual(array, [1, 2, 3]);
  13904. * ```
  13905. */
  13906. function binarySearch(haystack, needle, low, high) {
  13907. while (low <= high) {
  13908. const mid = low + ((high - low) >> 1);
  13909. const cmp = haystack[mid][COLUMN$1] - needle;
  13910. if (cmp === 0) {
  13911. found = true;
  13912. return mid;
  13913. }
  13914. if (cmp < 0) {
  13915. low = mid + 1;
  13916. }
  13917. else {
  13918. high = mid - 1;
  13919. }
  13920. }
  13921. found = false;
  13922. return low - 1;
  13923. }
  13924. function upperBound(haystack, needle, index) {
  13925. for (let i = index + 1; i < haystack.length; index = i++) {
  13926. if (haystack[i][COLUMN$1] !== needle)
  13927. break;
  13928. }
  13929. return index;
  13930. }
  13931. function lowerBound(haystack, needle, index) {
  13932. for (let i = index - 1; i >= 0; index = i--) {
  13933. if (haystack[i][COLUMN$1] !== needle)
  13934. break;
  13935. }
  13936. return index;
  13937. }
  13938. function memoizedState() {
  13939. return {
  13940. lastKey: -1,
  13941. lastNeedle: -1,
  13942. lastIndex: -1,
  13943. };
  13944. }
  13945. /**
  13946. * This overly complicated beast is just to record the last tested line/column and the resulting
  13947. * index, allowing us to skip a few tests if mappings are monotonically increasing.
  13948. */
  13949. function memoizedBinarySearch(haystack, needle, state, key) {
  13950. const { lastKey, lastNeedle, lastIndex } = state;
  13951. let low = 0;
  13952. let high = haystack.length - 1;
  13953. if (key === lastKey) {
  13954. if (needle === lastNeedle) {
  13955. found = lastIndex !== -1 && haystack[lastIndex][COLUMN$1] === needle;
  13956. return lastIndex;
  13957. }
  13958. if (needle >= lastNeedle) {
  13959. // lastIndex may be -1 if the previous needle was not found.
  13960. low = lastIndex === -1 ? 0 : lastIndex;
  13961. }
  13962. else {
  13963. high = lastIndex;
  13964. }
  13965. }
  13966. state.lastKey = key;
  13967. state.lastNeedle = needle;
  13968. return (state.lastIndex = binarySearch(haystack, needle, low, high));
  13969. }
  13970. const LINE_GTR_ZERO = '`line` must be greater than 0 (lines start at line 1)';
  13971. const COL_GTR_EQ_ZERO = '`column` must be greater than or equal to 0 (columns start at column 0)';
  13972. const LEAST_UPPER_BOUND = -1;
  13973. const GREATEST_LOWER_BOUND = 1;
  13974. class TraceMap {
  13975. constructor(map, mapUrl) {
  13976. const isString = typeof map === 'string';
  13977. if (!isString && map._decodedMemo)
  13978. return map;
  13979. const parsed = (isString ? JSON.parse(map) : map);
  13980. const { version, file, names, sourceRoot, sources, sourcesContent } = parsed;
  13981. this.version = version;
  13982. this.file = file;
  13983. this.names = names || [];
  13984. this.sourceRoot = sourceRoot;
  13985. this.sources = sources;
  13986. this.sourcesContent = sourcesContent;
  13987. this.ignoreList = parsed.ignoreList || parsed.x_google_ignoreList || undefined;
  13988. const from = resolve$1(sourceRoot || '', stripFilename(mapUrl));
  13989. this.resolvedSources = sources.map((s) => resolve$1(s || '', from));
  13990. const { mappings } = parsed;
  13991. if (typeof mappings === 'string') {
  13992. this._encoded = mappings;
  13993. this._decoded = undefined;
  13994. }
  13995. else {
  13996. this._encoded = undefined;
  13997. this._decoded = maybeSort(mappings, isString);
  13998. }
  13999. this._decodedMemo = memoizedState();
  14000. this._bySources = undefined;
  14001. this._bySourceMemos = undefined;
  14002. }
  14003. }
  14004. /**
  14005. * Typescript doesn't allow friend access to private fields, so this just casts the map into a type
  14006. * with public access modifiers.
  14007. */
  14008. function cast$2(map) {
  14009. return map;
  14010. }
  14011. /**
  14012. * Returns the encoded (VLQ string) form of the SourceMap's mappings field.
  14013. */
  14014. function encodedMappings(map) {
  14015. var _a;
  14016. var _b;
  14017. return ((_a = (_b = cast$2(map))._encoded) !== null && _a !== void 0 ? _a : (_b._encoded = encode$1(cast$2(map)._decoded)));
  14018. }
  14019. /**
  14020. * Returns the decoded (array of lines of segments) form of the SourceMap's mappings field.
  14021. */
  14022. function decodedMappings(map) {
  14023. var _a;
  14024. return ((_a = cast$2(map))._decoded || (_a._decoded = decode(cast$2(map)._encoded)));
  14025. }
  14026. /**
  14027. * A low-level API to find the segment associated with a generated line/column (think, from a
  14028. * stack trace). Line and column here are 0-based, unlike `originalPositionFor`.
  14029. */
  14030. function traceSegment(map, line, column) {
  14031. const decoded = decodedMappings(map);
  14032. // It's common for parent source maps to have pointers to lines that have no
  14033. // mapping (like a "//# sourceMappingURL=") at the end of the child file.
  14034. if (line >= decoded.length)
  14035. return null;
  14036. const segments = decoded[line];
  14037. const index = traceSegmentInternal(segments, cast$2(map)._decodedMemo, line, column, GREATEST_LOWER_BOUND);
  14038. return index === -1 ? null : segments[index];
  14039. }
  14040. /**
  14041. * A higher-level API to find the source/line/column associated with a generated line/column
  14042. * (think, from a stack trace). Line is 1-based, but column is 0-based, due to legacy behavior in
  14043. * `source-map` library.
  14044. */
  14045. function originalPositionFor$1(map, needle) {
  14046. let { line, column, bias } = needle;
  14047. line--;
  14048. if (line < 0)
  14049. throw new Error(LINE_GTR_ZERO);
  14050. if (column < 0)
  14051. throw new Error(COL_GTR_EQ_ZERO);
  14052. const decoded = decodedMappings(map);
  14053. // It's common for parent source maps to have pointers to lines that have no
  14054. // mapping (like a "//# sourceMappingURL=") at the end of the child file.
  14055. if (line >= decoded.length)
  14056. return OMapping(null, null, null, null);
  14057. const segments = decoded[line];
  14058. const index = traceSegmentInternal(segments, cast$2(map)._decodedMemo, line, column, bias || GREATEST_LOWER_BOUND);
  14059. if (index === -1)
  14060. return OMapping(null, null, null, null);
  14061. const segment = segments[index];
  14062. if (segment.length === 1)
  14063. return OMapping(null, null, null, null);
  14064. const { names, resolvedSources } = map;
  14065. 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);
  14066. }
  14067. /**
  14068. * Returns a sourcemap object (with decoded mappings) suitable for passing to a library that expects
  14069. * a sourcemap, or to JSON.stringify.
  14070. */
  14071. function decodedMap(map) {
  14072. return clone(map, decodedMappings(map));
  14073. }
  14074. /**
  14075. * Returns a sourcemap object (with encoded mappings) suitable for passing to a library that expects
  14076. * a sourcemap, or to JSON.stringify.
  14077. */
  14078. function encodedMap(map) {
  14079. return clone(map, encodedMappings(map));
  14080. }
  14081. function clone(map, mappings) {
  14082. return {
  14083. version: map.version,
  14084. file: map.file,
  14085. names: map.names,
  14086. sourceRoot: map.sourceRoot,
  14087. sources: map.sources,
  14088. sourcesContent: map.sourcesContent,
  14089. mappings,
  14090. ignoreList: map.ignoreList || map.x_google_ignoreList,
  14091. };
  14092. }
  14093. function OMapping(source, line, column, name) {
  14094. return { source, line, column, name };
  14095. }
  14096. function traceSegmentInternal(segments, memo, line, column, bias) {
  14097. let index = memoizedBinarySearch(segments, column, memo, line);
  14098. if (found) {
  14099. index = (bias === LEAST_UPPER_BOUND ? upperBound : lowerBound)(segments, column, index);
  14100. }
  14101. else if (bias === LEAST_UPPER_BOUND)
  14102. index++;
  14103. if (index === -1 || index === segments.length)
  14104. return -1;
  14105. return index;
  14106. }
  14107. /**
  14108. * SetArray acts like a `Set` (allowing only one occurrence of a string `key`), but provides the
  14109. * index of the `key` in the backing array.
  14110. *
  14111. * This is designed to allow synchronizing a second array with the contents of the backing array,
  14112. * like how in a sourcemap `sourcesContent[i]` is the source content associated with `source[i]`,
  14113. * and there are never duplicates.
  14114. */
  14115. class SetArray {
  14116. constructor() {
  14117. this._indexes = { __proto__: null };
  14118. this.array = [];
  14119. }
  14120. }
  14121. /**
  14122. * Typescript doesn't allow friend access to private fields, so this just casts the set into a type
  14123. * with public access modifiers.
  14124. */
  14125. function cast$1(set) {
  14126. return set;
  14127. }
  14128. /**
  14129. * Gets the index associated with `key` in the backing array, if it is already present.
  14130. */
  14131. function get(setarr, key) {
  14132. return cast$1(setarr)._indexes[key];
  14133. }
  14134. /**
  14135. * Puts `key` into the backing array, if it is not already present. Returns
  14136. * the index of the `key` in the backing array.
  14137. */
  14138. function put(setarr, key) {
  14139. // The key may or may not be present. If it is present, it's a number.
  14140. const index = get(setarr, key);
  14141. if (index !== undefined)
  14142. return index;
  14143. const { array, _indexes: indexes } = cast$1(setarr);
  14144. const length = array.push(key);
  14145. return (indexes[key] = length - 1);
  14146. }
  14147. /**
  14148. * Removes the key, if it exists in the set.
  14149. */
  14150. function remove(setarr, key) {
  14151. const index = get(setarr, key);
  14152. if (index === undefined)
  14153. return;
  14154. const { array, _indexes: indexes } = cast$1(setarr);
  14155. for (let i = index + 1; i < array.length; i++) {
  14156. const k = array[i];
  14157. array[i - 1] = k;
  14158. indexes[k]--;
  14159. }
  14160. indexes[key] = undefined;
  14161. array.pop();
  14162. }
  14163. const COLUMN = 0;
  14164. const SOURCES_INDEX = 1;
  14165. const SOURCE_LINE = 2;
  14166. const SOURCE_COLUMN = 3;
  14167. const NAMES_INDEX = 4;
  14168. const NO_NAME = -1;
  14169. /**
  14170. * Provides the state to generate a sourcemap.
  14171. */
  14172. class GenMapping {
  14173. constructor({ file, sourceRoot } = {}) {
  14174. this._names = new SetArray();
  14175. this._sources = new SetArray();
  14176. this._sourcesContent = [];
  14177. this._mappings = [];
  14178. this.file = file;
  14179. this.sourceRoot = sourceRoot;
  14180. this._ignoreList = new SetArray();
  14181. }
  14182. }
  14183. /**
  14184. * Typescript doesn't allow friend access to private fields, so this just casts the map into a type
  14185. * with public access modifiers.
  14186. */
  14187. function cast(map) {
  14188. return map;
  14189. }
  14190. /**
  14191. * Same as `addSegment`, but will only add the segment if it generates useful information in the
  14192. * resulting map. This only works correctly if segments are added **in order**, meaning you should
  14193. * not add a segment with a lower generated line/column than one that came before.
  14194. */
  14195. const maybeAddSegment = (map, genLine, genColumn, source, sourceLine, sourceColumn, name, content) => {
  14196. return addSegmentInternal(true, map, genLine, genColumn, source, sourceLine, sourceColumn, name);
  14197. };
  14198. /**
  14199. * Adds/removes the content of the source file to the source map.
  14200. */
  14201. function setSourceContent(map, source, content) {
  14202. const { _sources: sources, _sourcesContent: sourcesContent } = cast(map);
  14203. const index = put(sources, source);
  14204. sourcesContent[index] = content;
  14205. }
  14206. function setIgnore(map, source, ignore = true) {
  14207. const { _sources: sources, _sourcesContent: sourcesContent, _ignoreList: ignoreList } = cast(map);
  14208. const index = put(sources, source);
  14209. if (index === sourcesContent.length)
  14210. sourcesContent[index] = null;
  14211. if (ignore)
  14212. put(ignoreList, index);
  14213. else
  14214. remove(ignoreList, index);
  14215. }
  14216. /**
  14217. * Returns a sourcemap object (with decoded mappings) suitable for passing to a library that expects
  14218. * a sourcemap, or to JSON.stringify.
  14219. */
  14220. function toDecodedMap(map) {
  14221. const { _mappings: mappings, _sources: sources, _sourcesContent: sourcesContent, _names: names, _ignoreList: ignoreList, } = cast(map);
  14222. removeEmptyFinalLines(mappings);
  14223. return {
  14224. version: 3,
  14225. file: map.file || undefined,
  14226. names: names.array,
  14227. sourceRoot: map.sourceRoot || undefined,
  14228. sources: sources.array,
  14229. sourcesContent,
  14230. mappings,
  14231. ignoreList: ignoreList.array,
  14232. };
  14233. }
  14234. /**
  14235. * Returns a sourcemap object (with encoded mappings) suitable for passing to a library that expects
  14236. * a sourcemap, or to JSON.stringify.
  14237. */
  14238. function toEncodedMap(map) {
  14239. const decoded = toDecodedMap(map);
  14240. return Object.assign(Object.assign({}, decoded), { mappings: encode$1(decoded.mappings) });
  14241. }
  14242. // This split declaration is only so that terser can elminiate the static initialization block.
  14243. function addSegmentInternal(skipable, map, genLine, genColumn, source, sourceLine, sourceColumn, name, content) {
  14244. const { _mappings: mappings, _sources: sources, _sourcesContent: sourcesContent, _names: names, } = cast(map);
  14245. const line = getLine(mappings, genLine);
  14246. const index = getColumnIndex(line, genColumn);
  14247. if (!source) {
  14248. if (skipSourceless(line, index))
  14249. return;
  14250. return insert(line, index, [genColumn]);
  14251. }
  14252. const sourcesIndex = put(sources, source);
  14253. const namesIndex = name ? put(names, name) : NO_NAME;
  14254. if (sourcesIndex === sourcesContent.length)
  14255. sourcesContent[sourcesIndex] = null;
  14256. if (skipSource(line, index, sourcesIndex, sourceLine, sourceColumn, namesIndex)) {
  14257. return;
  14258. }
  14259. return insert(line, index, name
  14260. ? [genColumn, sourcesIndex, sourceLine, sourceColumn, namesIndex]
  14261. : [genColumn, sourcesIndex, sourceLine, sourceColumn]);
  14262. }
  14263. function getLine(mappings, index) {
  14264. for (let i = mappings.length; i <= index; i++) {
  14265. mappings[i] = [];
  14266. }
  14267. return mappings[index];
  14268. }
  14269. function getColumnIndex(line, genColumn) {
  14270. let index = line.length;
  14271. for (let i = index - 1; i >= 0; index = i--) {
  14272. const current = line[i];
  14273. if (genColumn >= current[COLUMN])
  14274. break;
  14275. }
  14276. return index;
  14277. }
  14278. function insert(array, index, value) {
  14279. for (let i = array.length; i > index; i--) {
  14280. array[i] = array[i - 1];
  14281. }
  14282. array[index] = value;
  14283. }
  14284. function removeEmptyFinalLines(mappings) {
  14285. const { length } = mappings;
  14286. let len = length;
  14287. for (let i = len - 1; i >= 0; len = i, i--) {
  14288. if (mappings[i].length > 0)
  14289. break;
  14290. }
  14291. if (len < length)
  14292. mappings.length = len;
  14293. }
  14294. function skipSourceless(line, index) {
  14295. // The start of a line is already sourceless, so adding a sourceless segment to the beginning
  14296. // doesn't generate any useful information.
  14297. if (index === 0)
  14298. return true;
  14299. const prev = line[index - 1];
  14300. // If the previous segment is also sourceless, then adding another sourceless segment doesn't
  14301. // genrate any new information. Else, this segment will end the source/named segment and point to
  14302. // a sourceless position, which is useful.
  14303. return prev.length === 1;
  14304. }
  14305. function skipSource(line, index, sourcesIndex, sourceLine, sourceColumn, namesIndex) {
  14306. // A source/named segment at the start of a line gives position at that genColumn
  14307. if (index === 0)
  14308. return false;
  14309. const prev = line[index - 1];
  14310. // If the previous segment is sourceless, then we're transitioning to a source.
  14311. if (prev.length === 1)
  14312. return false;
  14313. // If the previous segment maps to the exact same source position, then this segment doesn't
  14314. // provide any new position information.
  14315. return (sourcesIndex === prev[SOURCES_INDEX] &&
  14316. sourceLine === prev[SOURCE_LINE] &&
  14317. sourceColumn === prev[SOURCE_COLUMN] &&
  14318. namesIndex === (prev.length === 5 ? prev[NAMES_INDEX] : NO_NAME));
  14319. }
  14320. const SOURCELESS_MAPPING = /* #__PURE__ */ SegmentObject('', -1, -1, '', null, false);
  14321. const EMPTY_SOURCES = [];
  14322. function SegmentObject(source, line, column, name, content, ignore) {
  14323. return { source, line, column, name, content, ignore };
  14324. }
  14325. function Source(map, sources, source, content, ignore) {
  14326. return {
  14327. map,
  14328. sources,
  14329. source,
  14330. content,
  14331. ignore,
  14332. };
  14333. }
  14334. /**
  14335. * MapSource represents a single sourcemap, with the ability to trace mappings into its child nodes
  14336. * (which may themselves be SourceMapTrees).
  14337. */
  14338. function MapSource(map, sources) {
  14339. return Source(map, sources, '', null, false);
  14340. }
  14341. /**
  14342. * A "leaf" node in the sourcemap tree, representing an original, unmodified source file. Recursive
  14343. * segment tracing ends at the `OriginalSource`.
  14344. */
  14345. function OriginalSource(source, content, ignore) {
  14346. return Source(null, EMPTY_SOURCES, source, content, ignore);
  14347. }
  14348. /**
  14349. * traceMappings is only called on the root level SourceMapTree, and begins the process of
  14350. * resolving each mapping in terms of the original source files.
  14351. */
  14352. function traceMappings(tree) {
  14353. // TODO: Eventually support sourceRoot, which has to be removed because the sources are already
  14354. // fully resolved. We'll need to make sources relative to the sourceRoot before adding them.
  14355. const gen = new GenMapping({ file: tree.map.file });
  14356. const { sources: rootSources, map } = tree;
  14357. const rootNames = map.names;
  14358. const rootMappings = decodedMappings(map);
  14359. for (let i = 0; i < rootMappings.length; i++) {
  14360. const segments = rootMappings[i];
  14361. for (let j = 0; j < segments.length; j++) {
  14362. const segment = segments[j];
  14363. const genCol = segment[0];
  14364. let traced = SOURCELESS_MAPPING;
  14365. // 1-length segments only move the current generated column, there's no source information
  14366. // to gather from it.
  14367. if (segment.length !== 1) {
  14368. const source = rootSources[segment[1]];
  14369. traced = originalPositionFor(source, segment[2], segment[3], segment.length === 5 ? rootNames[segment[4]] : '');
  14370. // If the trace is invalid, then the trace ran into a sourcemap that doesn't contain a
  14371. // respective segment into an original source.
  14372. if (traced == null)
  14373. continue;
  14374. }
  14375. const { column, line, name, content, source, ignore } = traced;
  14376. maybeAddSegment(gen, i, genCol, source, line, column, name);
  14377. if (source && content != null)
  14378. setSourceContent(gen, source, content);
  14379. if (ignore)
  14380. setIgnore(gen, source, true);
  14381. }
  14382. }
  14383. return gen;
  14384. }
  14385. /**
  14386. * originalPositionFor is only called on children SourceMapTrees. It recurses down into its own
  14387. * child SourceMapTrees, until we find the original source map.
  14388. */
  14389. function originalPositionFor(source, line, column, name) {
  14390. if (!source.map) {
  14391. return SegmentObject(source.source, line, column, name, source.content, source.ignore);
  14392. }
  14393. const segment = traceSegment(source.map, line, column);
  14394. // If we couldn't find a segment, then this doesn't exist in the sourcemap.
  14395. if (segment == null)
  14396. return null;
  14397. // 1-length segments only move the current generated column, there's no source information
  14398. // to gather from it.
  14399. if (segment.length === 1)
  14400. return SOURCELESS_MAPPING;
  14401. return originalPositionFor(source.sources[segment[1]], segment[2], segment[3], segment.length === 5 ? source.map.names[segment[4]] : name);
  14402. }
  14403. function asArray(value) {
  14404. if (Array.isArray(value))
  14405. return value;
  14406. return [value];
  14407. }
  14408. /**
  14409. * Recursively builds a tree structure out of sourcemap files, with each node
  14410. * being either an `OriginalSource` "leaf" or a `SourceMapTree` composed of
  14411. * `OriginalSource`s and `SourceMapTree`s.
  14412. *
  14413. * Every sourcemap is composed of a collection of source files and mappings
  14414. * into locations of those source files. When we generate a `SourceMapTree` for
  14415. * the sourcemap, we attempt to load each source file's own sourcemap. If it
  14416. * does not have an associated sourcemap, it is considered an original,
  14417. * unmodified source file.
  14418. */
  14419. function buildSourceMapTree(input, loader) {
  14420. const maps = asArray(input).map((m) => new TraceMap(m, ''));
  14421. const map = maps.pop();
  14422. for (let i = 0; i < maps.length; i++) {
  14423. if (maps[i].sources.length > 1) {
  14424. throw new Error(`Transformation map ${i} must have exactly one source file.\n` +
  14425. 'Did you specify these with the most recent transformation maps first?');
  14426. }
  14427. }
  14428. let tree = build$2(map, loader, '', 0);
  14429. for (let i = maps.length - 1; i >= 0; i--) {
  14430. tree = MapSource(maps[i], [tree]);
  14431. }
  14432. return tree;
  14433. }
  14434. function build$2(map, loader, importer, importerDepth) {
  14435. const { resolvedSources, sourcesContent, ignoreList } = map;
  14436. const depth = importerDepth + 1;
  14437. const children = resolvedSources.map((sourceFile, i) => {
  14438. // The loading context gives the loader more information about why this file is being loaded
  14439. // (eg, from which importer). It also allows the loader to override the location of the loaded
  14440. // sourcemap/original source, or to override the content in the sourcesContent field if it's
  14441. // an unmodified source file.
  14442. const ctx = {
  14443. importer,
  14444. depth,
  14445. source: sourceFile || '',
  14446. content: undefined,
  14447. ignore: undefined,
  14448. };
  14449. // Use the provided loader callback to retrieve the file's sourcemap.
  14450. // TODO: We should eventually support async loading of sourcemap files.
  14451. const sourceMap = loader(ctx.source, ctx);
  14452. const { source, content, ignore } = ctx;
  14453. // If there is a sourcemap, then we need to recurse into it to load its source files.
  14454. if (sourceMap)
  14455. return build$2(new TraceMap(sourceMap, source), loader, source, depth);
  14456. // Else, it's an unmodified source file.
  14457. // The contents of this unmodified source file can be overridden via the loader context,
  14458. // allowing it to be explicitly null or a string. If it remains undefined, we fall back to
  14459. // the importing sourcemap's `sourcesContent` field.
  14460. const sourceContent = content !== undefined ? content : sourcesContent ? sourcesContent[i] : null;
  14461. const ignored = ignore !== undefined ? ignore : ignoreList ? ignoreList.includes(i) : false;
  14462. return OriginalSource(source, sourceContent, ignored);
  14463. });
  14464. return MapSource(map, children);
  14465. }
  14466. /**
  14467. * A SourceMap v3 compatible sourcemap, which only includes fields that were
  14468. * provided to it.
  14469. */
  14470. class SourceMap {
  14471. constructor(map, options) {
  14472. const out = options.decodedMappings ? toDecodedMap(map) : toEncodedMap(map);
  14473. this.version = out.version; // SourceMap spec says this should be first.
  14474. this.file = out.file;
  14475. this.mappings = out.mappings;
  14476. this.names = out.names;
  14477. this.ignoreList = out.ignoreList;
  14478. this.sourceRoot = out.sourceRoot;
  14479. this.sources = out.sources;
  14480. if (!options.excludeContent) {
  14481. this.sourcesContent = out.sourcesContent;
  14482. }
  14483. }
  14484. toString() {
  14485. return JSON.stringify(this);
  14486. }
  14487. }
  14488. /**
  14489. * Traces through all the mappings in the root sourcemap, through the sources
  14490. * (and their sourcemaps), all the way back to the original source location.
  14491. *
  14492. * `loader` will be called every time we encounter a source file. If it returns
  14493. * a sourcemap, we will recurse into that sourcemap to continue the trace. If
  14494. * it returns a falsey value, that source file is treated as an original,
  14495. * unmodified source file.
  14496. *
  14497. * Pass `excludeContent` to exclude any self-containing source file content
  14498. * from the output sourcemap.
  14499. *
  14500. * Pass `decodedMappings` to receive a SourceMap with decoded (instead of
  14501. * VLQ encoded) mappings.
  14502. */
  14503. function remapping(input, loader, options) {
  14504. const opts = { excludeContent: !!options, decodedMappings: false };
  14505. const tree = buildSourceMapTree(input, loader);
  14506. return new SourceMap(traceMappings(tree), opts);
  14507. }
  14508. var src$3 = {exports: {}};
  14509. var browser$3 = {exports: {}};
  14510. /**
  14511. * Helpers.
  14512. */
  14513. var ms$1;
  14514. var hasRequiredMs$1;
  14515. function requireMs$1 () {
  14516. if (hasRequiredMs$1) return ms$1;
  14517. hasRequiredMs$1 = 1;
  14518. var s = 1000;
  14519. var m = s * 60;
  14520. var h = m * 60;
  14521. var d = h * 24;
  14522. var w = d * 7;
  14523. var y = d * 365.25;
  14524. /**
  14525. * Parse or format the given `val`.
  14526. *
  14527. * Options:
  14528. *
  14529. * - `long` verbose formatting [false]
  14530. *
  14531. * @param {String|Number} val
  14532. * @param {Object} [options]
  14533. * @throws {Error} throw an error if val is not a non-empty string or a number
  14534. * @return {String|Number}
  14535. * @api public
  14536. */
  14537. ms$1 = function(val, options) {
  14538. options = options || {};
  14539. var type = typeof val;
  14540. if (type === 'string' && val.length > 0) {
  14541. return parse(val);
  14542. } else if (type === 'number' && isFinite(val)) {
  14543. return options.long ? fmtLong(val) : fmtShort(val);
  14544. }
  14545. throw new Error(
  14546. 'val is not a non-empty string or a valid number. val=' +
  14547. JSON.stringify(val)
  14548. );
  14549. };
  14550. /**
  14551. * Parse the given `str` and return milliseconds.
  14552. *
  14553. * @param {String} str
  14554. * @return {Number}
  14555. * @api private
  14556. */
  14557. function parse(str) {
  14558. str = String(str);
  14559. if (str.length > 100) {
  14560. return;
  14561. }
  14562. 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(
  14563. str
  14564. );
  14565. if (!match) {
  14566. return;
  14567. }
  14568. var n = parseFloat(match[1]);
  14569. var type = (match[2] || 'ms').toLowerCase();
  14570. switch (type) {
  14571. case 'years':
  14572. case 'year':
  14573. case 'yrs':
  14574. case 'yr':
  14575. case 'y':
  14576. return n * y;
  14577. case 'weeks':
  14578. case 'week':
  14579. case 'w':
  14580. return n * w;
  14581. case 'days':
  14582. case 'day':
  14583. case 'd':
  14584. return n * d;
  14585. case 'hours':
  14586. case 'hour':
  14587. case 'hrs':
  14588. case 'hr':
  14589. case 'h':
  14590. return n * h;
  14591. case 'minutes':
  14592. case 'minute':
  14593. case 'mins':
  14594. case 'min':
  14595. case 'm':
  14596. return n * m;
  14597. case 'seconds':
  14598. case 'second':
  14599. case 'secs':
  14600. case 'sec':
  14601. case 's':
  14602. return n * s;
  14603. case 'milliseconds':
  14604. case 'millisecond':
  14605. case 'msecs':
  14606. case 'msec':
  14607. case 'ms':
  14608. return n;
  14609. default:
  14610. return undefined;
  14611. }
  14612. }
  14613. /**
  14614. * Short format for `ms`.
  14615. *
  14616. * @param {Number} ms
  14617. * @return {String}
  14618. * @api private
  14619. */
  14620. function fmtShort(ms) {
  14621. var msAbs = Math.abs(ms);
  14622. if (msAbs >= d) {
  14623. return Math.round(ms / d) + 'd';
  14624. }
  14625. if (msAbs >= h) {
  14626. return Math.round(ms / h) + 'h';
  14627. }
  14628. if (msAbs >= m) {
  14629. return Math.round(ms / m) + 'm';
  14630. }
  14631. if (msAbs >= s) {
  14632. return Math.round(ms / s) + 's';
  14633. }
  14634. return ms + 'ms';
  14635. }
  14636. /**
  14637. * Long format for `ms`.
  14638. *
  14639. * @param {Number} ms
  14640. * @return {String}
  14641. * @api private
  14642. */
  14643. function fmtLong(ms) {
  14644. var msAbs = Math.abs(ms);
  14645. if (msAbs >= d) {
  14646. return plural(ms, msAbs, d, 'day');
  14647. }
  14648. if (msAbs >= h) {
  14649. return plural(ms, msAbs, h, 'hour');
  14650. }
  14651. if (msAbs >= m) {
  14652. return plural(ms, msAbs, m, 'minute');
  14653. }
  14654. if (msAbs >= s) {
  14655. return plural(ms, msAbs, s, 'second');
  14656. }
  14657. return ms + ' ms';
  14658. }
  14659. /**
  14660. * Pluralization helper.
  14661. */
  14662. function plural(ms, msAbs, n, name) {
  14663. var isPlural = msAbs >= n * 1.5;
  14664. return Math.round(ms / n) + ' ' + name + (isPlural ? 's' : '');
  14665. }
  14666. return ms$1;
  14667. }
  14668. var common$b;
  14669. var hasRequiredCommon;
  14670. function requireCommon () {
  14671. if (hasRequiredCommon) return common$b;
  14672. hasRequiredCommon = 1;
  14673. /**
  14674. * This is the common logic for both the Node.js and web browser
  14675. * implementations of `debug()`.
  14676. */
  14677. function setup(env) {
  14678. createDebug.debug = createDebug;
  14679. createDebug.default = createDebug;
  14680. createDebug.coerce = coerce;
  14681. createDebug.disable = disable;
  14682. createDebug.enable = enable;
  14683. createDebug.enabled = enabled;
  14684. createDebug.humanize = requireMs$1();
  14685. createDebug.destroy = destroy;
  14686. Object.keys(env).forEach(key => {
  14687. createDebug[key] = env[key];
  14688. });
  14689. /**
  14690. * The currently active debug mode names, and names to skip.
  14691. */
  14692. createDebug.names = [];
  14693. createDebug.skips = [];
  14694. /**
  14695. * Map of special "%n" handling functions, for the debug "format" argument.
  14696. *
  14697. * Valid key names are a single, lower or upper-case letter, i.e. "n" and "N".
  14698. */
  14699. createDebug.formatters = {};
  14700. /**
  14701. * Selects a color for a debug namespace
  14702. * @param {String} namespace The namespace string for the debug instance to be colored
  14703. * @return {Number|String} An ANSI color code for the given namespace
  14704. * @api private
  14705. */
  14706. function selectColor(namespace) {
  14707. let hash = 0;
  14708. for (let i = 0; i < namespace.length; i++) {
  14709. hash = ((hash << 5) - hash) + namespace.charCodeAt(i);
  14710. hash |= 0; // Convert to 32bit integer
  14711. }
  14712. return createDebug.colors[Math.abs(hash) % createDebug.colors.length];
  14713. }
  14714. createDebug.selectColor = selectColor;
  14715. /**
  14716. * Create a debugger with the given `namespace`.
  14717. *
  14718. * @param {String} namespace
  14719. * @return {Function}
  14720. * @api public
  14721. */
  14722. function createDebug(namespace) {
  14723. let prevTime;
  14724. let enableOverride = null;
  14725. let namespacesCache;
  14726. let enabledCache;
  14727. function debug(...args) {
  14728. // Disabled?
  14729. if (!debug.enabled) {
  14730. return;
  14731. }
  14732. const self = debug;
  14733. // Set `diff` timestamp
  14734. const curr = Number(new Date());
  14735. const ms = curr - (prevTime || curr);
  14736. self.diff = ms;
  14737. self.prev = prevTime;
  14738. self.curr = curr;
  14739. prevTime = curr;
  14740. args[0] = createDebug.coerce(args[0]);
  14741. if (typeof args[0] !== 'string') {
  14742. // Anything else let's inspect with %O
  14743. args.unshift('%O');
  14744. }
  14745. // Apply any `formatters` transformations
  14746. let index = 0;
  14747. args[0] = args[0].replace(/%([a-zA-Z%])/g, (match, format) => {
  14748. // If we encounter an escaped % then don't increase the array index
  14749. if (match === '%%') {
  14750. return '%';
  14751. }
  14752. index++;
  14753. const formatter = createDebug.formatters[format];
  14754. if (typeof formatter === 'function') {
  14755. const val = args[index];
  14756. match = formatter.call(self, val);
  14757. // Now we need to remove `args[index]` since it's inlined in the `format`
  14758. args.splice(index, 1);
  14759. index--;
  14760. }
  14761. return match;
  14762. });
  14763. // Apply env-specific formatting (colors, etc.)
  14764. createDebug.formatArgs.call(self, args);
  14765. const logFn = self.log || createDebug.log;
  14766. logFn.apply(self, args);
  14767. }
  14768. debug.namespace = namespace;
  14769. debug.useColors = createDebug.useColors();
  14770. debug.color = createDebug.selectColor(namespace);
  14771. debug.extend = extend;
  14772. debug.destroy = createDebug.destroy; // XXX Temporary. Will be removed in the next major release.
  14773. Object.defineProperty(debug, 'enabled', {
  14774. enumerable: true,
  14775. configurable: false,
  14776. get: () => {
  14777. if (enableOverride !== null) {
  14778. return enableOverride;
  14779. }
  14780. if (namespacesCache !== createDebug.namespaces) {
  14781. namespacesCache = createDebug.namespaces;
  14782. enabledCache = createDebug.enabled(namespace);
  14783. }
  14784. return enabledCache;
  14785. },
  14786. set: v => {
  14787. enableOverride = v;
  14788. }
  14789. });
  14790. // Env-specific initialization logic for debug instances
  14791. if (typeof createDebug.init === 'function') {
  14792. createDebug.init(debug);
  14793. }
  14794. return debug;
  14795. }
  14796. function extend(namespace, delimiter) {
  14797. const newDebug = createDebug(this.namespace + (typeof delimiter === 'undefined' ? ':' : delimiter) + namespace);
  14798. newDebug.log = this.log;
  14799. return newDebug;
  14800. }
  14801. /**
  14802. * Enables a debug mode by namespaces. This can include modes
  14803. * separated by a colon and wildcards.
  14804. *
  14805. * @param {String} namespaces
  14806. * @api public
  14807. */
  14808. function enable(namespaces) {
  14809. createDebug.save(namespaces);
  14810. createDebug.namespaces = namespaces;
  14811. createDebug.names = [];
  14812. createDebug.skips = [];
  14813. let i;
  14814. const split = (typeof namespaces === 'string' ? namespaces : '').split(/[\s,]+/);
  14815. const len = split.length;
  14816. for (i = 0; i < len; i++) {
  14817. if (!split[i]) {
  14818. // ignore empty strings
  14819. continue;
  14820. }
  14821. namespaces = split[i].replace(/\*/g, '.*?');
  14822. if (namespaces[0] === '-') {
  14823. createDebug.skips.push(new RegExp('^' + namespaces.slice(1) + '$'));
  14824. } else {
  14825. createDebug.names.push(new RegExp('^' + namespaces + '$'));
  14826. }
  14827. }
  14828. }
  14829. /**
  14830. * Disable debug output.
  14831. *
  14832. * @return {String} namespaces
  14833. * @api public
  14834. */
  14835. function disable() {
  14836. const namespaces = [
  14837. ...createDebug.names.map(toNamespace),
  14838. ...createDebug.skips.map(toNamespace).map(namespace => '-' + namespace)
  14839. ].join(',');
  14840. createDebug.enable('');
  14841. return namespaces;
  14842. }
  14843. /**
  14844. * Returns true if the given mode name is enabled, false otherwise.
  14845. *
  14846. * @param {String} name
  14847. * @return {Boolean}
  14848. * @api public
  14849. */
  14850. function enabled(name) {
  14851. if (name[name.length - 1] === '*') {
  14852. return true;
  14853. }
  14854. let i;
  14855. let len;
  14856. for (i = 0, len = createDebug.skips.length; i < len; i++) {
  14857. if (createDebug.skips[i].test(name)) {
  14858. return false;
  14859. }
  14860. }
  14861. for (i = 0, len = createDebug.names.length; i < len; i++) {
  14862. if (createDebug.names[i].test(name)) {
  14863. return true;
  14864. }
  14865. }
  14866. return false;
  14867. }
  14868. /**
  14869. * Convert regexp to namespace
  14870. *
  14871. * @param {RegExp} regxep
  14872. * @return {String} namespace
  14873. * @api private
  14874. */
  14875. function toNamespace(regexp) {
  14876. return regexp.toString()
  14877. .substring(2, regexp.toString().length - 2)
  14878. .replace(/\.\*\?$/, '*');
  14879. }
  14880. /**
  14881. * Coerce `val`.
  14882. *
  14883. * @param {Mixed} val
  14884. * @return {Mixed}
  14885. * @api private
  14886. */
  14887. function coerce(val) {
  14888. if (val instanceof Error) {
  14889. return val.stack || val.message;
  14890. }
  14891. return val;
  14892. }
  14893. /**
  14894. * XXX DO NOT USE. This is a temporary stub function.
  14895. * XXX It WILL be removed in the next major release.
  14896. */
  14897. function destroy() {
  14898. console.warn('Instance method `debug.destroy()` is deprecated and no longer does anything. It will be removed in the next major version of `debug`.');
  14899. }
  14900. createDebug.enable(createDebug.load());
  14901. return createDebug;
  14902. }
  14903. common$b = setup;
  14904. return common$b;
  14905. }
  14906. /* eslint-env browser */
  14907. var hasRequiredBrowser$1;
  14908. function requireBrowser$1 () {
  14909. if (hasRequiredBrowser$1) return browser$3.exports;
  14910. hasRequiredBrowser$1 = 1;
  14911. (function (module, exports) {
  14912. /**
  14913. * This is the web browser implementation of `debug()`.
  14914. */
  14915. exports.formatArgs = formatArgs;
  14916. exports.save = save;
  14917. exports.load = load;
  14918. exports.useColors = useColors;
  14919. exports.storage = localstorage();
  14920. exports.destroy = (() => {
  14921. let warned = false;
  14922. return () => {
  14923. if (!warned) {
  14924. warned = true;
  14925. console.warn('Instance method `debug.destroy()` is deprecated and no longer does anything. It will be removed in the next major version of `debug`.');
  14926. }
  14927. };
  14928. })();
  14929. /**
  14930. * Colors.
  14931. */
  14932. exports.colors = [
  14933. '#0000CC',
  14934. '#0000FF',
  14935. '#0033CC',
  14936. '#0033FF',
  14937. '#0066CC',
  14938. '#0066FF',
  14939. '#0099CC',
  14940. '#0099FF',
  14941. '#00CC00',
  14942. '#00CC33',
  14943. '#00CC66',
  14944. '#00CC99',
  14945. '#00CCCC',
  14946. '#00CCFF',
  14947. '#3300CC',
  14948. '#3300FF',
  14949. '#3333CC',
  14950. '#3333FF',
  14951. '#3366CC',
  14952. '#3366FF',
  14953. '#3399CC',
  14954. '#3399FF',
  14955. '#33CC00',
  14956. '#33CC33',
  14957. '#33CC66',
  14958. '#33CC99',
  14959. '#33CCCC',
  14960. '#33CCFF',
  14961. '#6600CC',
  14962. '#6600FF',
  14963. '#6633CC',
  14964. '#6633FF',
  14965. '#66CC00',
  14966. '#66CC33',
  14967. '#9900CC',
  14968. '#9900FF',
  14969. '#9933CC',
  14970. '#9933FF',
  14971. '#99CC00',
  14972. '#99CC33',
  14973. '#CC0000',
  14974. '#CC0033',
  14975. '#CC0066',
  14976. '#CC0099',
  14977. '#CC00CC',
  14978. '#CC00FF',
  14979. '#CC3300',
  14980. '#CC3333',
  14981. '#CC3366',
  14982. '#CC3399',
  14983. '#CC33CC',
  14984. '#CC33FF',
  14985. '#CC6600',
  14986. '#CC6633',
  14987. '#CC9900',
  14988. '#CC9933',
  14989. '#CCCC00',
  14990. '#CCCC33',
  14991. '#FF0000',
  14992. '#FF0033',
  14993. '#FF0066',
  14994. '#FF0099',
  14995. '#FF00CC',
  14996. '#FF00FF',
  14997. '#FF3300',
  14998. '#FF3333',
  14999. '#FF3366',
  15000. '#FF3399',
  15001. '#FF33CC',
  15002. '#FF33FF',
  15003. '#FF6600',
  15004. '#FF6633',
  15005. '#FF9900',
  15006. '#FF9933',
  15007. '#FFCC00',
  15008. '#FFCC33'
  15009. ];
  15010. /**
  15011. * Currently only WebKit-based Web Inspectors, Firefox >= v31,
  15012. * and the Firebug extension (any Firefox version) are known
  15013. * to support "%c" CSS customizations.
  15014. *
  15015. * TODO: add a `localStorage` variable to explicitly enable/disable colors
  15016. */
  15017. // eslint-disable-next-line complexity
  15018. function useColors() {
  15019. // NB: In an Electron preload script, document will be defined but not fully
  15020. // initialized. Since we know we're in Chrome, we'll just detect this case
  15021. // explicitly
  15022. if (typeof window !== 'undefined' && window.process && (window.process.type === 'renderer' || window.process.__nwjs)) {
  15023. return true;
  15024. }
  15025. // Internet Explorer and Edge do not support colors.
  15026. if (typeof navigator !== 'undefined' && navigator.userAgent && navigator.userAgent.toLowerCase().match(/(edge|trident)\/(\d+)/)) {
  15027. return false;
  15028. }
  15029. let m;
  15030. // Is webkit? http://stackoverflow.com/a/16459606/376773
  15031. // document is undefined in react-native: https://github.com/facebook/react-native/pull/1632
  15032. return (typeof document !== 'undefined' && document.documentElement && document.documentElement.style && document.documentElement.style.WebkitAppearance) ||
  15033. // Is firebug? http://stackoverflow.com/a/398120/376773
  15034. (typeof window !== 'undefined' && window.console && (window.console.firebug || (window.console.exception && window.console.table))) ||
  15035. // Is firefox >= v31?
  15036. // https://developer.mozilla.org/en-US/docs/Tools/Web_Console#Styling_messages
  15037. (typeof navigator !== 'undefined' && navigator.userAgent && (m = navigator.userAgent.toLowerCase().match(/firefox\/(\d+)/)) && parseInt(m[1], 10) >= 31) ||
  15038. // Double check webkit in userAgent just in case we are in a worker
  15039. (typeof navigator !== 'undefined' && navigator.userAgent && navigator.userAgent.toLowerCase().match(/applewebkit\/(\d+)/));
  15040. }
  15041. /**
  15042. * Colorize log arguments if enabled.
  15043. *
  15044. * @api public
  15045. */
  15046. function formatArgs(args) {
  15047. args[0] = (this.useColors ? '%c' : '') +
  15048. this.namespace +
  15049. (this.useColors ? ' %c' : ' ') +
  15050. args[0] +
  15051. (this.useColors ? '%c ' : ' ') +
  15052. '+' + module.exports.humanize(this.diff);
  15053. if (!this.useColors) {
  15054. return;
  15055. }
  15056. const c = 'color: ' + this.color;
  15057. args.splice(1, 0, c, 'color: inherit');
  15058. // The final "%c" is somewhat tricky, because there could be other
  15059. // arguments passed either before or after the %c, so we need to
  15060. // figure out the correct index to insert the CSS into
  15061. let index = 0;
  15062. let lastC = 0;
  15063. args[0].replace(/%[a-zA-Z%]/g, match => {
  15064. if (match === '%%') {
  15065. return;
  15066. }
  15067. index++;
  15068. if (match === '%c') {
  15069. // We only are interested in the *last* %c
  15070. // (the user may have provided their own)
  15071. lastC = index;
  15072. }
  15073. });
  15074. args.splice(lastC, 0, c);
  15075. }
  15076. /**
  15077. * Invokes `console.debug()` when available.
  15078. * No-op when `console.debug` is not a "function".
  15079. * If `console.debug` is not available, falls back
  15080. * to `console.log`.
  15081. *
  15082. * @api public
  15083. */
  15084. exports.log = console.debug || console.log || (() => {});
  15085. /**
  15086. * Save `namespaces`.
  15087. *
  15088. * @param {String} namespaces
  15089. * @api private
  15090. */
  15091. function save(namespaces) {
  15092. try {
  15093. if (namespaces) {
  15094. exports.storage.setItem('debug', namespaces);
  15095. } else {
  15096. exports.storage.removeItem('debug');
  15097. }
  15098. } catch (error) {
  15099. // Swallow
  15100. // XXX (@Qix-) should we be logging these?
  15101. }
  15102. }
  15103. /**
  15104. * Load `namespaces`.
  15105. *
  15106. * @return {String} returns the previously persisted debug modes
  15107. * @api private
  15108. */
  15109. function load() {
  15110. let r;
  15111. try {
  15112. r = exports.storage.getItem('debug');
  15113. } catch (error) {
  15114. // Swallow
  15115. // XXX (@Qix-) should we be logging these?
  15116. }
  15117. // If debug isn't set in LS, and we're in Electron, try to load $DEBUG
  15118. if (!r && typeof process !== 'undefined' && 'env' in process) {
  15119. r = process.env.DEBUG;
  15120. }
  15121. return r;
  15122. }
  15123. /**
  15124. * Localstorage attempts to return the localstorage.
  15125. *
  15126. * This is necessary because safari throws
  15127. * when a user disables cookies/localstorage
  15128. * and you attempt to access it.
  15129. *
  15130. * @return {LocalStorage}
  15131. * @api private
  15132. */
  15133. function localstorage() {
  15134. try {
  15135. // TVMLKit (Apple TV JS Runtime) does not have a window object, just localStorage in the global context
  15136. // The Browser also has localStorage in the global context.
  15137. return localStorage;
  15138. } catch (error) {
  15139. // Swallow
  15140. // XXX (@Qix-) should we be logging these?
  15141. }
  15142. }
  15143. module.exports = requireCommon()(exports);
  15144. const {formatters} = module.exports;
  15145. /**
  15146. * Map %j to `JSON.stringify()`, since no Web Inspectors do that by default.
  15147. */
  15148. formatters.j = function (v) {
  15149. try {
  15150. return JSON.stringify(v);
  15151. } catch (error) {
  15152. return '[UnexpectedJSONParseError]: ' + error.message;
  15153. }
  15154. };
  15155. } (browser$3, browser$3.exports));
  15156. return browser$3.exports;
  15157. }
  15158. var node$1 = {exports: {}};
  15159. /**
  15160. * Module dependencies.
  15161. */
  15162. var hasRequiredNode$1;
  15163. function requireNode$1 () {
  15164. if (hasRequiredNode$1) return node$1.exports;
  15165. hasRequiredNode$1 = 1;
  15166. (function (module, exports) {
  15167. const tty = require$$0$3;
  15168. const util = require$$0$5;
  15169. /**
  15170. * This is the Node.js implementation of `debug()`.
  15171. */
  15172. exports.init = init;
  15173. exports.log = log;
  15174. exports.formatArgs = formatArgs;
  15175. exports.save = save;
  15176. exports.load = load;
  15177. exports.useColors = useColors;
  15178. exports.destroy = util.deprecate(
  15179. () => {},
  15180. 'Instance method `debug.destroy()` is deprecated and no longer does anything. It will be removed in the next major version of `debug`.'
  15181. );
  15182. /**
  15183. * Colors.
  15184. */
  15185. exports.colors = [6, 2, 3, 4, 5, 1];
  15186. try {
  15187. // Optional dependency (as in, doesn't need to be installed, NOT like optionalDependencies in package.json)
  15188. // eslint-disable-next-line import/no-extraneous-dependencies
  15189. const supportsColor = require('supports-color');
  15190. if (supportsColor && (supportsColor.stderr || supportsColor).level >= 2) {
  15191. exports.colors = [
  15192. 20,
  15193. 21,
  15194. 26,
  15195. 27,
  15196. 32,
  15197. 33,
  15198. 38,
  15199. 39,
  15200. 40,
  15201. 41,
  15202. 42,
  15203. 43,
  15204. 44,
  15205. 45,
  15206. 56,
  15207. 57,
  15208. 62,
  15209. 63,
  15210. 68,
  15211. 69,
  15212. 74,
  15213. 75,
  15214. 76,
  15215. 77,
  15216. 78,
  15217. 79,
  15218. 80,
  15219. 81,
  15220. 92,
  15221. 93,
  15222. 98,
  15223. 99,
  15224. 112,
  15225. 113,
  15226. 128,
  15227. 129,
  15228. 134,
  15229. 135,
  15230. 148,
  15231. 149,
  15232. 160,
  15233. 161,
  15234. 162,
  15235. 163,
  15236. 164,
  15237. 165,
  15238. 166,
  15239. 167,
  15240. 168,
  15241. 169,
  15242. 170,
  15243. 171,
  15244. 172,
  15245. 173,
  15246. 178,
  15247. 179,
  15248. 184,
  15249. 185,
  15250. 196,
  15251. 197,
  15252. 198,
  15253. 199,
  15254. 200,
  15255. 201,
  15256. 202,
  15257. 203,
  15258. 204,
  15259. 205,
  15260. 206,
  15261. 207,
  15262. 208,
  15263. 209,
  15264. 214,
  15265. 215,
  15266. 220,
  15267. 221
  15268. ];
  15269. }
  15270. } catch (error) {
  15271. // Swallow - we only care if `supports-color` is available; it doesn't have to be.
  15272. }
  15273. /**
  15274. * Build up the default `inspectOpts` object from the environment variables.
  15275. *
  15276. * $ DEBUG_COLORS=no DEBUG_DEPTH=10 DEBUG_SHOW_HIDDEN=enabled node script.js
  15277. */
  15278. exports.inspectOpts = Object.keys(process.env).filter(key => {
  15279. return /^debug_/i.test(key);
  15280. }).reduce((obj, key) => {
  15281. // Camel-case
  15282. const prop = key
  15283. .substring(6)
  15284. .toLowerCase()
  15285. .replace(/_([a-z])/g, (_, k) => {
  15286. return k.toUpperCase();
  15287. });
  15288. // Coerce string value into JS value
  15289. let val = process.env[key];
  15290. if (/^(yes|on|true|enabled)$/i.test(val)) {
  15291. val = true;
  15292. } else if (/^(no|off|false|disabled)$/i.test(val)) {
  15293. val = false;
  15294. } else if (val === 'null') {
  15295. val = null;
  15296. } else {
  15297. val = Number(val);
  15298. }
  15299. obj[prop] = val;
  15300. return obj;
  15301. }, {});
  15302. /**
  15303. * Is stdout a TTY? Colored output is enabled when `true`.
  15304. */
  15305. function useColors() {
  15306. return 'colors' in exports.inspectOpts ?
  15307. Boolean(exports.inspectOpts.colors) :
  15308. tty.isatty(process.stderr.fd);
  15309. }
  15310. /**
  15311. * Adds ANSI color escape codes if enabled.
  15312. *
  15313. * @api public
  15314. */
  15315. function formatArgs(args) {
  15316. const {namespace: name, useColors} = this;
  15317. if (useColors) {
  15318. const c = this.color;
  15319. const colorCode = '\u001B[3' + (c < 8 ? c : '8;5;' + c);
  15320. const prefix = ` ${colorCode};1m${name} \u001B[0m`;
  15321. args[0] = prefix + args[0].split('\n').join('\n' + prefix);
  15322. args.push(colorCode + 'm+' + module.exports.humanize(this.diff) + '\u001B[0m');
  15323. } else {
  15324. args[0] = getDate() + name + ' ' + args[0];
  15325. }
  15326. }
  15327. function getDate() {
  15328. if (exports.inspectOpts.hideDate) {
  15329. return '';
  15330. }
  15331. return new Date().toISOString() + ' ';
  15332. }
  15333. /**
  15334. * Invokes `util.formatWithOptions()` with the specified arguments and writes to stderr.
  15335. */
  15336. function log(...args) {
  15337. return process.stderr.write(util.formatWithOptions(exports.inspectOpts, ...args) + '\n');
  15338. }
  15339. /**
  15340. * Save `namespaces`.
  15341. *
  15342. * @param {String} namespaces
  15343. * @api private
  15344. */
  15345. function save(namespaces) {
  15346. if (namespaces) {
  15347. process.env.DEBUG = namespaces;
  15348. } else {
  15349. // If you set a process.env field to null or undefined, it gets cast to the
  15350. // string 'null' or 'undefined'. Just delete instead.
  15351. delete process.env.DEBUG;
  15352. }
  15353. }
  15354. /**
  15355. * Load `namespaces`.
  15356. *
  15357. * @return {String} returns the previously persisted debug modes
  15358. * @api private
  15359. */
  15360. function load() {
  15361. return process.env.DEBUG;
  15362. }
  15363. /**
  15364. * Init logic for `debug` instances.
  15365. *
  15366. * Create a new `inspectOpts` object in case `useColors` is set
  15367. * differently for a particular `debug` instance.
  15368. */
  15369. function init(debug) {
  15370. debug.inspectOpts = {};
  15371. const keys = Object.keys(exports.inspectOpts);
  15372. for (let i = 0; i < keys.length; i++) {
  15373. debug.inspectOpts[keys[i]] = exports.inspectOpts[keys[i]];
  15374. }
  15375. }
  15376. module.exports = requireCommon()(exports);
  15377. const {formatters} = module.exports;
  15378. /**
  15379. * Map %o to `util.inspect()`, all on a single line.
  15380. */
  15381. formatters.o = function (v) {
  15382. this.inspectOpts.colors = this.useColors;
  15383. return util.inspect(v, this.inspectOpts)
  15384. .split('\n')
  15385. .map(str => str.trim())
  15386. .join(' ');
  15387. };
  15388. /**
  15389. * Map %O to `util.inspect()`, allowing multiple lines if needed.
  15390. */
  15391. formatters.O = function (v) {
  15392. this.inspectOpts.colors = this.useColors;
  15393. return util.inspect(v, this.inspectOpts);
  15394. };
  15395. } (node$1, node$1.exports));
  15396. return node$1.exports;
  15397. }
  15398. /**
  15399. * Detect Electron renderer / nwjs process, which is node, but we should
  15400. * treat as a browser.
  15401. */
  15402. if (typeof process === 'undefined' || process.type === 'renderer' || process.browser === true || process.__nwjs) {
  15403. src$3.exports = requireBrowser$1();
  15404. } else {
  15405. src$3.exports = requireNode$1();
  15406. }
  15407. var srcExports$1 = src$3.exports;
  15408. var debug$i = /*@__PURE__*/getDefaultExportFromCjs(srcExports$1);
  15409. let pnp;
  15410. if (process.versions.pnp) {
  15411. try {
  15412. pnp = createRequire$1(import.meta.url)("pnpapi");
  15413. } catch {
  15414. }
  15415. }
  15416. function invalidatePackageData(packageCache, pkgPath) {
  15417. const pkgDir = normalizePath$3(path$n.dirname(pkgPath));
  15418. packageCache.forEach((pkg, cacheKey) => {
  15419. if (pkg.dir === pkgDir) {
  15420. packageCache.delete(cacheKey);
  15421. }
  15422. });
  15423. }
  15424. function resolvePackageData(pkgName, basedir, preserveSymlinks = false, packageCache) {
  15425. if (pnp) {
  15426. const cacheKey = getRpdCacheKey(pkgName, basedir, preserveSymlinks);
  15427. if (packageCache?.has(cacheKey)) return packageCache.get(cacheKey);
  15428. try {
  15429. const pkg = pnp.resolveToUnqualified(pkgName, basedir, {
  15430. considerBuiltins: false
  15431. });
  15432. if (!pkg) return null;
  15433. const pkgData = loadPackageData(path$n.join(pkg, "package.json"));
  15434. packageCache?.set(cacheKey, pkgData);
  15435. return pkgData;
  15436. } catch {
  15437. return null;
  15438. }
  15439. }
  15440. const originalBasedir = basedir;
  15441. while (basedir) {
  15442. if (packageCache) {
  15443. const cached = getRpdCache(
  15444. packageCache,
  15445. pkgName,
  15446. basedir,
  15447. originalBasedir,
  15448. preserveSymlinks
  15449. );
  15450. if (cached) return cached;
  15451. }
  15452. const pkg = path$n.join(basedir, "node_modules", pkgName, "package.json");
  15453. try {
  15454. if (fs__default.existsSync(pkg)) {
  15455. const pkgPath = preserveSymlinks ? pkg : safeRealpathSync(pkg);
  15456. const pkgData = loadPackageData(pkgPath);
  15457. if (packageCache) {
  15458. setRpdCache(
  15459. packageCache,
  15460. pkgData,
  15461. pkgName,
  15462. basedir,
  15463. originalBasedir,
  15464. preserveSymlinks
  15465. );
  15466. }
  15467. return pkgData;
  15468. }
  15469. } catch {
  15470. }
  15471. const nextBasedir = path$n.dirname(basedir);
  15472. if (nextBasedir === basedir) break;
  15473. basedir = nextBasedir;
  15474. }
  15475. return null;
  15476. }
  15477. function findNearestPackageData(basedir, packageCache) {
  15478. const originalBasedir = basedir;
  15479. while (basedir) {
  15480. if (packageCache) {
  15481. const cached = getFnpdCache(packageCache, basedir, originalBasedir);
  15482. if (cached) return cached;
  15483. }
  15484. const pkgPath = path$n.join(basedir, "package.json");
  15485. if (tryStatSync(pkgPath)?.isFile()) {
  15486. try {
  15487. const pkgData = loadPackageData(pkgPath);
  15488. if (packageCache) {
  15489. setFnpdCache(packageCache, pkgData, basedir, originalBasedir);
  15490. }
  15491. return pkgData;
  15492. } catch {
  15493. }
  15494. }
  15495. const nextBasedir = path$n.dirname(basedir);
  15496. if (nextBasedir === basedir) break;
  15497. basedir = nextBasedir;
  15498. }
  15499. return null;
  15500. }
  15501. function findNearestMainPackageData(basedir, packageCache) {
  15502. const nearestPackage = findNearestPackageData(basedir, packageCache);
  15503. return nearestPackage && (nearestPackage.data.name ? nearestPackage : findNearestMainPackageData(
  15504. path$n.dirname(nearestPackage.dir),
  15505. packageCache
  15506. ));
  15507. }
  15508. function loadPackageData(pkgPath) {
  15509. const data = JSON.parse(fs__default.readFileSync(pkgPath, "utf-8"));
  15510. const pkgDir = normalizePath$3(path$n.dirname(pkgPath));
  15511. const { sideEffects } = data;
  15512. let hasSideEffects;
  15513. if (typeof sideEffects === "boolean") {
  15514. hasSideEffects = () => sideEffects;
  15515. } else if (Array.isArray(sideEffects)) {
  15516. if (sideEffects.length <= 0) {
  15517. hasSideEffects = () => false;
  15518. } else {
  15519. const finalPackageSideEffects = sideEffects.map((sideEffect) => {
  15520. if (sideEffect.includes("/")) {
  15521. return sideEffect;
  15522. }
  15523. return `**/${sideEffect}`;
  15524. });
  15525. hasSideEffects = createFilter(finalPackageSideEffects, null, {
  15526. resolve: pkgDir
  15527. });
  15528. }
  15529. } else {
  15530. hasSideEffects = () => null;
  15531. }
  15532. const pkg = {
  15533. dir: pkgDir,
  15534. data,
  15535. hasSideEffects,
  15536. webResolvedImports: {},
  15537. nodeResolvedImports: {},
  15538. setResolvedCache(key, entry, targetWeb) {
  15539. if (targetWeb) {
  15540. pkg.webResolvedImports[key] = entry;
  15541. } else {
  15542. pkg.nodeResolvedImports[key] = entry;
  15543. }
  15544. },
  15545. getResolvedCache(key, targetWeb) {
  15546. if (targetWeb) {
  15547. return pkg.webResolvedImports[key];
  15548. } else {
  15549. return pkg.nodeResolvedImports[key];
  15550. }
  15551. }
  15552. };
  15553. return pkg;
  15554. }
  15555. function watchPackageDataPlugin(packageCache) {
  15556. const watchQueue = /* @__PURE__ */ new Set();
  15557. const watchedDirs = /* @__PURE__ */ new Set();
  15558. const watchFileStub = (id) => {
  15559. watchQueue.add(id);
  15560. };
  15561. let watchFile = watchFileStub;
  15562. const setPackageData = packageCache.set.bind(packageCache);
  15563. packageCache.set = (id, pkg) => {
  15564. if (!isInNodeModules$1(pkg.dir) && !watchedDirs.has(pkg.dir)) {
  15565. watchedDirs.add(pkg.dir);
  15566. watchFile(path$n.join(pkg.dir, "package.json"));
  15567. }
  15568. return setPackageData(id, pkg);
  15569. };
  15570. return {
  15571. name: "vite:watch-package-data",
  15572. buildStart() {
  15573. watchFile = this.addWatchFile.bind(this);
  15574. watchQueue.forEach(watchFile);
  15575. watchQueue.clear();
  15576. },
  15577. buildEnd() {
  15578. watchFile = watchFileStub;
  15579. },
  15580. watchChange(id) {
  15581. if (id.endsWith("/package.json")) {
  15582. invalidatePackageData(packageCache, path$n.normalize(id));
  15583. }
  15584. }
  15585. };
  15586. }
  15587. function getRpdCache(packageCache, pkgName, basedir, originalBasedir, preserveSymlinks) {
  15588. const cacheKey = getRpdCacheKey(pkgName, basedir, preserveSymlinks);
  15589. const pkgData = packageCache.get(cacheKey);
  15590. if (pkgData) {
  15591. traverseBetweenDirs(originalBasedir, basedir, (dir) => {
  15592. packageCache.set(getRpdCacheKey(pkgName, dir, preserveSymlinks), pkgData);
  15593. });
  15594. return pkgData;
  15595. }
  15596. }
  15597. function setRpdCache(packageCache, pkgData, pkgName, basedir, originalBasedir, preserveSymlinks) {
  15598. packageCache.set(getRpdCacheKey(pkgName, basedir, preserveSymlinks), pkgData);
  15599. traverseBetweenDirs(originalBasedir, basedir, (dir) => {
  15600. packageCache.set(getRpdCacheKey(pkgName, dir, preserveSymlinks), pkgData);
  15601. });
  15602. }
  15603. function getRpdCacheKey(pkgName, basedir, preserveSymlinks) {
  15604. return `rpd_${pkgName}_${basedir}_${preserveSymlinks}`;
  15605. }
  15606. function getFnpdCache(packageCache, basedir, originalBasedir) {
  15607. const cacheKey = getFnpdCacheKey(basedir);
  15608. const pkgData = packageCache.get(cacheKey);
  15609. if (pkgData) {
  15610. traverseBetweenDirs(originalBasedir, basedir, (dir) => {
  15611. packageCache.set(getFnpdCacheKey(dir), pkgData);
  15612. });
  15613. return pkgData;
  15614. }
  15615. }
  15616. function setFnpdCache(packageCache, pkgData, basedir, originalBasedir) {
  15617. packageCache.set(getFnpdCacheKey(basedir), pkgData);
  15618. traverseBetweenDirs(originalBasedir, basedir, (dir) => {
  15619. packageCache.set(getFnpdCacheKey(dir), pkgData);
  15620. });
  15621. }
  15622. function getFnpdCacheKey(basedir) {
  15623. return `fnpd_${basedir}`;
  15624. }
  15625. function traverseBetweenDirs(longerDir, shorterDir, cb) {
  15626. while (longerDir !== shorterDir) {
  15627. cb(longerDir);
  15628. longerDir = path$n.dirname(longerDir);
  15629. }
  15630. }
  15631. const createFilter = createFilter$1;
  15632. const replaceSlashOrColonRE = /[/:]/g;
  15633. const replaceDotRE = /\./g;
  15634. const replaceNestedIdRE = /\s*>\s*/g;
  15635. const replaceHashRE = /#/g;
  15636. const flattenId = (id) => {
  15637. const flatId = limitFlattenIdLength(
  15638. id.replace(replaceSlashOrColonRE, "_").replace(replaceDotRE, "__").replace(replaceNestedIdRE, "___").replace(replaceHashRE, "____")
  15639. );
  15640. return flatId;
  15641. };
  15642. const FLATTEN_ID_HASH_LENGTH = 8;
  15643. const FLATTEN_ID_MAX_FILE_LENGTH = 170;
  15644. const limitFlattenIdLength = (id, limit = FLATTEN_ID_MAX_FILE_LENGTH) => {
  15645. if (id.length <= limit) {
  15646. return id;
  15647. }
  15648. return id.slice(0, limit - (FLATTEN_ID_HASH_LENGTH + 1)) + "_" + getHash(id);
  15649. };
  15650. const normalizeId = (id) => id.replace(replaceNestedIdRE, " > ");
  15651. const NODE_BUILTIN_NAMESPACE = "node:";
  15652. const NPM_BUILTIN_NAMESPACE = "npm:";
  15653. const BUN_BUILTIN_NAMESPACE = "bun:";
  15654. const nodeBuiltins = builtinModules.filter((id) => !id.includes(":"));
  15655. function isBuiltin(id) {
  15656. if (process.versions.deno && id.startsWith(NPM_BUILTIN_NAMESPACE)) return true;
  15657. if (process.versions.bun && id.startsWith(BUN_BUILTIN_NAMESPACE)) return true;
  15658. return isNodeBuiltin(id);
  15659. }
  15660. function isNodeBuiltin(id) {
  15661. if (id.startsWith(NODE_BUILTIN_NAMESPACE)) return true;
  15662. return nodeBuiltins.includes(id);
  15663. }
  15664. function isInNodeModules$1(id) {
  15665. return id.includes("node_modules");
  15666. }
  15667. function moduleListContains(moduleList, id) {
  15668. return moduleList?.some(
  15669. (m) => m === id || id.startsWith(withTrailingSlash(m))
  15670. );
  15671. }
  15672. function isOptimizable(id, optimizeDeps) {
  15673. const { extensions } = optimizeDeps;
  15674. return OPTIMIZABLE_ENTRY_RE.test(id) || (extensions?.some((ext) => id.endsWith(ext)) ?? false);
  15675. }
  15676. const bareImportRE = /^(?![a-zA-Z]:)[\w@](?!.*:\/\/)/;
  15677. const deepImportRE = /^([^@][^/]*)\/|^(@[^/]+\/[^/]+)\//;
  15678. const _require$1 = createRequire$1(import.meta.url);
  15679. function resolveDependencyVersion(dep, pkgRelativePath = "../../package.json") {
  15680. const pkgPath = path$n.resolve(_require$1.resolve(dep), pkgRelativePath);
  15681. return JSON.parse(fs__default.readFileSync(pkgPath, "utf-8")).version;
  15682. }
  15683. const rollupVersion = resolveDependencyVersion("rollup");
  15684. const filter = process.env.VITE_DEBUG_FILTER;
  15685. const DEBUG = process.env.DEBUG;
  15686. function createDebugger(namespace, options = {}) {
  15687. const log = debug$i(namespace);
  15688. const { onlyWhenFocused } = options;
  15689. let enabled = log.enabled;
  15690. if (enabled && onlyWhenFocused) {
  15691. const ns = typeof onlyWhenFocused === "string" ? onlyWhenFocused : namespace;
  15692. enabled = !!DEBUG?.includes(ns);
  15693. }
  15694. if (enabled) {
  15695. return (...args) => {
  15696. if (!filter || args.some((a) => a?.includes?.(filter))) {
  15697. log(...args);
  15698. }
  15699. };
  15700. }
  15701. }
  15702. function testCaseInsensitiveFS() {
  15703. if (!CLIENT_ENTRY.endsWith("client.mjs")) {
  15704. throw new Error(
  15705. `cannot test case insensitive FS, CLIENT_ENTRY const doesn't contain client.mjs`
  15706. );
  15707. }
  15708. if (!fs__default.existsSync(CLIENT_ENTRY)) {
  15709. throw new Error(
  15710. "cannot test case insensitive FS, CLIENT_ENTRY does not point to an existing file: " + CLIENT_ENTRY
  15711. );
  15712. }
  15713. return fs__default.existsSync(CLIENT_ENTRY.replace("client.mjs", "cLiEnT.mjs"));
  15714. }
  15715. const urlCanParse = (
  15716. // eslint-disable-next-line n/no-unsupported-features/node-builtins
  15717. URL$3.canParse ?? // URL.canParse is supported from Node.js 18.17.0+, 20.0.0+
  15718. ((path2, base) => {
  15719. try {
  15720. new URL$3(path2, base);
  15721. return true;
  15722. } catch {
  15723. return false;
  15724. }
  15725. })
  15726. );
  15727. const isCaseInsensitiveFS = testCaseInsensitiveFS();
  15728. const VOLUME_RE = /^[A-Z]:/i;
  15729. function normalizePath$3(id) {
  15730. return path$n.posix.normalize(isWindows$3 ? slash$1(id) : id);
  15731. }
  15732. function fsPathFromId(id) {
  15733. const fsPath = normalizePath$3(
  15734. id.startsWith(FS_PREFIX) ? id.slice(FS_PREFIX.length) : id
  15735. );
  15736. return fsPath[0] === "/" || VOLUME_RE.test(fsPath) ? fsPath : `/${fsPath}`;
  15737. }
  15738. function fsPathFromUrl(url) {
  15739. return fsPathFromId(cleanUrl(url));
  15740. }
  15741. function isParentDirectory(dir, file) {
  15742. dir = withTrailingSlash(dir);
  15743. return file.startsWith(dir) || isCaseInsensitiveFS && file.toLowerCase().startsWith(dir.toLowerCase());
  15744. }
  15745. function isSameFileUri(file1, file2) {
  15746. return file1 === file2 || isCaseInsensitiveFS && file1.toLowerCase() === file2.toLowerCase();
  15747. }
  15748. const externalRE = /^(https?:)?\/\//;
  15749. const isExternalUrl = (url) => externalRE.test(url);
  15750. const dataUrlRE = /^\s*data:/i;
  15751. const isDataUrl = (url) => dataUrlRE.test(url);
  15752. const virtualModuleRE = /^virtual-module:.*/;
  15753. const virtualModulePrefix = "virtual-module:";
  15754. const knownJsSrcRE = /\.(?:[jt]sx?|m[jt]s|vue|marko|svelte|astro|imba|mdx)(?:$|\?)/;
  15755. const isJSRequest = (url) => {
  15756. url = cleanUrl(url);
  15757. if (knownJsSrcRE.test(url)) {
  15758. return true;
  15759. }
  15760. if (!path$n.extname(url) && url[url.length - 1] !== "/") {
  15761. return true;
  15762. }
  15763. return false;
  15764. };
  15765. const knownTsRE = /\.(?:ts|mts|cts|tsx)(?:$|\?)/;
  15766. const isTsRequest = (url) => knownTsRE.test(url);
  15767. const importQueryRE = /(\?|&)import=?(?:&|$)/;
  15768. const directRequestRE$1 = /(\?|&)direct=?(?:&|$)/;
  15769. const internalPrefixes = [
  15770. FS_PREFIX,
  15771. VALID_ID_PREFIX,
  15772. CLIENT_PUBLIC_PATH,
  15773. ENV_PUBLIC_PATH
  15774. ];
  15775. const InternalPrefixRE = new RegExp(`^(?:${internalPrefixes.join("|")})`);
  15776. const trailingSeparatorRE = /[?&]$/;
  15777. const isImportRequest = (url) => importQueryRE.test(url);
  15778. const isInternalRequest = (url) => InternalPrefixRE.test(url);
  15779. function removeImportQuery(url) {
  15780. return url.replace(importQueryRE, "$1").replace(trailingSeparatorRE, "");
  15781. }
  15782. function removeDirectQuery(url) {
  15783. return url.replace(directRequestRE$1, "$1").replace(trailingSeparatorRE, "");
  15784. }
  15785. const urlRE$1 = /(\?|&)url(?:&|$)/;
  15786. const rawRE$1 = /(\?|&)raw(?:&|$)/;
  15787. function removeUrlQuery(url) {
  15788. return url.replace(urlRE$1, "$1").replace(trailingSeparatorRE, "");
  15789. }
  15790. const replacePercentageRE = /%/g;
  15791. function injectQuery(url, queryToInject) {
  15792. const resolvedUrl = new URL$3(
  15793. url.replace(replacePercentageRE, "%25"),
  15794. "relative:///"
  15795. );
  15796. const { search, hash } = resolvedUrl;
  15797. let pathname = cleanUrl(url);
  15798. pathname = isWindows$3 ? slash$1(pathname) : pathname;
  15799. return `${pathname}?${queryToInject}${search ? `&` + search.slice(1) : ""}${hash ?? ""}`;
  15800. }
  15801. const timestampRE = /\bt=\d{13}&?\b/;
  15802. function removeTimestampQuery(url) {
  15803. return url.replace(timestampRE, "").replace(trailingSeparatorRE, "");
  15804. }
  15805. async function asyncReplace(input, re, replacer) {
  15806. let match;
  15807. let remaining = input;
  15808. let rewritten = "";
  15809. while (match = re.exec(remaining)) {
  15810. rewritten += remaining.slice(0, match.index);
  15811. rewritten += await replacer(match);
  15812. remaining = remaining.slice(match.index + match[0].length);
  15813. }
  15814. rewritten += remaining;
  15815. return rewritten;
  15816. }
  15817. function timeFrom(start, subtract = 0) {
  15818. const time = performance$1.now() - start - subtract;
  15819. const timeString = (time.toFixed(2) + `ms`).padEnd(5, " ");
  15820. if (time < 10) {
  15821. return colors$1.green(timeString);
  15822. } else if (time < 50) {
  15823. return colors$1.yellow(timeString);
  15824. } else {
  15825. return colors$1.red(timeString);
  15826. }
  15827. }
  15828. function prettifyUrl(url, root) {
  15829. url = removeTimestampQuery(url);
  15830. const isAbsoluteFile = url.startsWith(root);
  15831. if (isAbsoluteFile || url.startsWith(FS_PREFIX)) {
  15832. const file = path$n.posix.relative(
  15833. root,
  15834. isAbsoluteFile ? url : fsPathFromId(url)
  15835. );
  15836. return colors$1.dim(file);
  15837. } else {
  15838. return colors$1.dim(url);
  15839. }
  15840. }
  15841. function isObject$1(value) {
  15842. return Object.prototype.toString.call(value) === "[object Object]";
  15843. }
  15844. function isDefined(value) {
  15845. return value != null;
  15846. }
  15847. function tryStatSync(file) {
  15848. try {
  15849. return fs__default.statSync(file, { throwIfNoEntry: false });
  15850. } catch {
  15851. }
  15852. }
  15853. function lookupFile(dir, fileNames) {
  15854. while (dir) {
  15855. for (const fileName of fileNames) {
  15856. const fullPath = path$n.join(dir, fileName);
  15857. if (tryStatSync(fullPath)?.isFile()) return fullPath;
  15858. }
  15859. const parentDir = path$n.dirname(dir);
  15860. if (parentDir === dir) return;
  15861. dir = parentDir;
  15862. }
  15863. }
  15864. function isFilePathESM(filePath, packageCache) {
  15865. if (/\.m[jt]s$/.test(filePath)) {
  15866. return true;
  15867. } else if (/\.c[jt]s$/.test(filePath)) {
  15868. return false;
  15869. } else {
  15870. try {
  15871. const pkg = findNearestPackageData(path$n.dirname(filePath), packageCache);
  15872. return pkg?.data.type === "module";
  15873. } catch {
  15874. return false;
  15875. }
  15876. }
  15877. }
  15878. const splitRE = /\r?\n/g;
  15879. const range = 2;
  15880. function pad$1(source, n = 2) {
  15881. const lines = source.split(splitRE);
  15882. return lines.map((l) => ` `.repeat(n) + l).join(`
  15883. `);
  15884. }
  15885. function posToNumber(source, pos) {
  15886. if (typeof pos === "number") return pos;
  15887. const lines = source.split(splitRE);
  15888. const { line, column } = pos;
  15889. let start = 0;
  15890. for (let i = 0; i < line - 1 && i < lines.length; i++) {
  15891. start += lines[i].length + 1;
  15892. }
  15893. return start + column;
  15894. }
  15895. function numberToPos(source, offset) {
  15896. if (typeof offset !== "number") return offset;
  15897. if (offset > source.length) {
  15898. throw new Error(
  15899. `offset is longer than source length! offset ${offset} > length ${source.length}`
  15900. );
  15901. }
  15902. const lines = source.split(splitRE);
  15903. let counted = 0;
  15904. let line = 0;
  15905. let column = 0;
  15906. for (; line < lines.length; line++) {
  15907. const lineLength = lines[line].length + 1;
  15908. if (counted + lineLength >= offset) {
  15909. column = offset - counted + 1;
  15910. break;
  15911. }
  15912. counted += lineLength;
  15913. }
  15914. return { line: line + 1, column };
  15915. }
  15916. function generateCodeFrame(source, start = 0, end) {
  15917. start = Math.max(posToNumber(source, start), 0);
  15918. end = Math.min(
  15919. end !== void 0 ? posToNumber(source, end) : start,
  15920. source.length
  15921. );
  15922. const lines = source.split(splitRE);
  15923. let count = 0;
  15924. const res = [];
  15925. for (let i = 0; i < lines.length; i++) {
  15926. count += lines[i].length;
  15927. if (count >= start) {
  15928. for (let j = i - range; j <= i + range || end > count; j++) {
  15929. if (j < 0 || j >= lines.length) continue;
  15930. const line = j + 1;
  15931. res.push(
  15932. `${line}${" ".repeat(Math.max(3 - String(line).length, 0))}| ${lines[j]}`
  15933. );
  15934. const lineLength = lines[j].length;
  15935. if (j === i) {
  15936. const pad2 = Math.max(start - (count - lineLength), 0);
  15937. const length = Math.max(
  15938. 1,
  15939. end > count ? lineLength - pad2 : end - start
  15940. );
  15941. res.push(` | ` + " ".repeat(pad2) + "^".repeat(length));
  15942. } else if (j > i) {
  15943. if (end > count) {
  15944. const length = Math.max(Math.min(end - count, lineLength), 1);
  15945. res.push(` | ` + "^".repeat(length));
  15946. }
  15947. count += lineLength + 1;
  15948. }
  15949. }
  15950. break;
  15951. }
  15952. count++;
  15953. }
  15954. return res.join("\n");
  15955. }
  15956. function isFileReadable(filename) {
  15957. if (!tryStatSync(filename)) {
  15958. return false;
  15959. }
  15960. try {
  15961. fs__default.accessSync(filename, fs__default.constants.R_OK);
  15962. return true;
  15963. } catch {
  15964. return false;
  15965. }
  15966. }
  15967. const splitFirstDirRE = /(.+?)[\\/](.+)/;
  15968. function emptyDir(dir, skip) {
  15969. const skipInDir = [];
  15970. let nested = null;
  15971. if (skip?.length) {
  15972. for (const file of skip) {
  15973. if (path$n.dirname(file) !== ".") {
  15974. const matched = splitFirstDirRE.exec(file);
  15975. if (matched) {
  15976. nested ??= /* @__PURE__ */ new Map();
  15977. const [, nestedDir, skipPath] = matched;
  15978. let nestedSkip = nested.get(nestedDir);
  15979. if (!nestedSkip) {
  15980. nestedSkip = [];
  15981. nested.set(nestedDir, nestedSkip);
  15982. }
  15983. if (!nestedSkip.includes(skipPath)) {
  15984. nestedSkip.push(skipPath);
  15985. }
  15986. }
  15987. } else {
  15988. skipInDir.push(file);
  15989. }
  15990. }
  15991. }
  15992. for (const file of fs__default.readdirSync(dir)) {
  15993. if (skipInDir.includes(file)) {
  15994. continue;
  15995. }
  15996. if (nested?.has(file)) {
  15997. emptyDir(path$n.resolve(dir, file), nested.get(file));
  15998. } else {
  15999. fs__default.rmSync(path$n.resolve(dir, file), { recursive: true, force: true });
  16000. }
  16001. }
  16002. }
  16003. function copyDir(srcDir, destDir) {
  16004. fs__default.mkdirSync(destDir, { recursive: true });
  16005. for (const file of fs__default.readdirSync(srcDir)) {
  16006. const srcFile = path$n.resolve(srcDir, file);
  16007. if (srcFile === destDir) {
  16008. continue;
  16009. }
  16010. const destFile = path$n.resolve(destDir, file);
  16011. const stat = fs__default.statSync(srcFile);
  16012. if (stat.isDirectory()) {
  16013. copyDir(srcFile, destFile);
  16014. } else {
  16015. fs__default.copyFileSync(srcFile, destFile);
  16016. }
  16017. }
  16018. }
  16019. const ERR_SYMLINK_IN_RECURSIVE_READDIR = "ERR_SYMLINK_IN_RECURSIVE_READDIR";
  16020. async function recursiveReaddir(dir) {
  16021. if (!fs__default.existsSync(dir)) {
  16022. return [];
  16023. }
  16024. let dirents;
  16025. try {
  16026. dirents = await fsp.readdir(dir, { withFileTypes: true });
  16027. } catch (e) {
  16028. if (e.code === "EACCES") {
  16029. return [];
  16030. }
  16031. throw e;
  16032. }
  16033. if (dirents.some((dirent) => dirent.isSymbolicLink())) {
  16034. const err = new Error(
  16035. "Symbolic links are not supported in recursiveReaddir"
  16036. );
  16037. err.code = ERR_SYMLINK_IN_RECURSIVE_READDIR;
  16038. throw err;
  16039. }
  16040. const files = await Promise.all(
  16041. dirents.map((dirent) => {
  16042. const res = path$n.resolve(dir, dirent.name);
  16043. return dirent.isDirectory() ? recursiveReaddir(res) : normalizePath$3(res);
  16044. })
  16045. );
  16046. return files.flat(1);
  16047. }
  16048. let safeRealpathSync = isWindows$3 ? windowsSafeRealPathSync : fs__default.realpathSync.native;
  16049. const windowsNetworkMap = /* @__PURE__ */ new Map();
  16050. function windowsMappedRealpathSync(path2) {
  16051. const realPath = fs__default.realpathSync.native(path2);
  16052. if (realPath.startsWith("\\\\")) {
  16053. for (const [network, volume] of windowsNetworkMap) {
  16054. if (realPath.startsWith(network)) return realPath.replace(network, volume);
  16055. }
  16056. }
  16057. return realPath;
  16058. }
  16059. const parseNetUseRE = /^\w* +(\w:) +([^ ]+)\s/;
  16060. let firstSafeRealPathSyncRun = false;
  16061. function windowsSafeRealPathSync(path2) {
  16062. if (!firstSafeRealPathSyncRun) {
  16063. optimizeSafeRealPathSync();
  16064. firstSafeRealPathSyncRun = true;
  16065. }
  16066. return fs__default.realpathSync(path2);
  16067. }
  16068. function optimizeSafeRealPathSync() {
  16069. const nodeVersion = process.versions.node.split(".").map(Number);
  16070. if (nodeVersion[0] < 18 || nodeVersion[0] === 18 && nodeVersion[1] < 10) {
  16071. safeRealpathSync = fs__default.realpathSync;
  16072. return;
  16073. }
  16074. try {
  16075. fs__default.realpathSync.native(path$n.resolve("./"));
  16076. } catch (error) {
  16077. if (error.message.includes("EISDIR: illegal operation on a directory")) {
  16078. safeRealpathSync = fs__default.realpathSync;
  16079. return;
  16080. }
  16081. }
  16082. exec("net use", (error, stdout) => {
  16083. if (error) return;
  16084. const lines = stdout.split("\n");
  16085. for (const line of lines) {
  16086. const m = parseNetUseRE.exec(line);
  16087. if (m) windowsNetworkMap.set(m[2], m[1]);
  16088. }
  16089. if (windowsNetworkMap.size === 0) {
  16090. safeRealpathSync = fs__default.realpathSync.native;
  16091. } else {
  16092. safeRealpathSync = windowsMappedRealpathSync;
  16093. }
  16094. });
  16095. }
  16096. function ensureWatchedFile(watcher, file, root) {
  16097. if (file && // only need to watch if out of root
  16098. !file.startsWith(withTrailingSlash(root)) && // some rollup plugins use null bytes for private resolved Ids
  16099. !file.includes("\0") && fs__default.existsSync(file)) {
  16100. watcher.add(path$n.resolve(file));
  16101. }
  16102. }
  16103. const escapedSpaceCharacters = /(?: |\\t|\\n|\\f|\\r)+/g;
  16104. const imageSetUrlRE = /^(?:[\w\-]+\(.*?\)|'.*?'|".*?"|\S*)/;
  16105. function joinSrcset(ret) {
  16106. return ret.map(({ url, descriptor }) => url + (descriptor ? ` ${descriptor}` : "")).join(", ");
  16107. }
  16108. function splitSrcSetDescriptor(srcs) {
  16109. return splitSrcSet(srcs).map((s) => {
  16110. const src = s.replace(escapedSpaceCharacters, " ").trim();
  16111. const url = imageSetUrlRE.exec(src)?.[0] ?? "";
  16112. return {
  16113. url,
  16114. descriptor: src.slice(url.length).trim()
  16115. };
  16116. }).filter(({ url }) => !!url);
  16117. }
  16118. function processSrcSet(srcs, replacer) {
  16119. return Promise.all(
  16120. splitSrcSetDescriptor(srcs).map(async ({ url, descriptor }) => ({
  16121. url: await replacer({ url, descriptor }),
  16122. descriptor
  16123. }))
  16124. ).then(joinSrcset);
  16125. }
  16126. function processSrcSetSync(srcs, replacer) {
  16127. return joinSrcset(
  16128. splitSrcSetDescriptor(srcs).map(({ url, descriptor }) => ({
  16129. url: replacer({ url, descriptor }),
  16130. descriptor
  16131. }))
  16132. );
  16133. }
  16134. const cleanSrcSetRE = /(?:url|image|gradient|cross-fade)\([^)]*\)|"([^"]|(?<=\\)")*"|'([^']|(?<=\\)')*'|data:\w+\/[\w.+\-]+;base64,[\w+/=]+|\?\S+,/g;
  16135. function splitSrcSet(srcs) {
  16136. const parts = [];
  16137. const cleanedSrcs = srcs.replace(cleanSrcSetRE, blankReplacer);
  16138. let startIndex = 0;
  16139. let splitIndex;
  16140. do {
  16141. splitIndex = cleanedSrcs.indexOf(",", startIndex);
  16142. parts.push(
  16143. srcs.slice(startIndex, splitIndex !== -1 ? splitIndex : void 0)
  16144. );
  16145. startIndex = splitIndex + 1;
  16146. } while (splitIndex !== -1);
  16147. return parts;
  16148. }
  16149. const windowsDriveRE = /^[A-Z]:/;
  16150. const replaceWindowsDriveRE = /^([A-Z]):\//;
  16151. const linuxAbsolutePathRE = /^\/[^/]/;
  16152. function escapeToLinuxLikePath(path2) {
  16153. if (windowsDriveRE.test(path2)) {
  16154. return path2.replace(replaceWindowsDriveRE, "/windows/$1/");
  16155. }
  16156. if (linuxAbsolutePathRE.test(path2)) {
  16157. return `/linux${path2}`;
  16158. }
  16159. return path2;
  16160. }
  16161. const revertWindowsDriveRE = /^\/windows\/([A-Z])\//;
  16162. function unescapeToLinuxLikePath(path2) {
  16163. if (path2.startsWith("/linux/")) {
  16164. return path2.slice("/linux".length);
  16165. }
  16166. if (path2.startsWith("/windows/")) {
  16167. return path2.replace(revertWindowsDriveRE, "$1:/");
  16168. }
  16169. return path2;
  16170. }
  16171. const nullSourceMap = {
  16172. names: [],
  16173. sources: [],
  16174. mappings: "",
  16175. version: 3
  16176. };
  16177. function combineSourcemaps(filename, sourcemapList) {
  16178. if (sourcemapList.length === 0 || sourcemapList.every((m) => m.sources.length === 0)) {
  16179. return { ...nullSourceMap };
  16180. }
  16181. sourcemapList = sourcemapList.map((sourcemap) => {
  16182. const newSourcemaps = { ...sourcemap };
  16183. newSourcemaps.sources = sourcemap.sources.map(
  16184. (source) => source ? escapeToLinuxLikePath(source) : null
  16185. );
  16186. if (sourcemap.sourceRoot) {
  16187. newSourcemaps.sourceRoot = escapeToLinuxLikePath(sourcemap.sourceRoot);
  16188. }
  16189. return newSourcemaps;
  16190. });
  16191. let map;
  16192. let mapIndex = 1;
  16193. const useArrayInterface = sourcemapList.slice(0, -1).find((m) => m.sources.length !== 1) === void 0;
  16194. if (useArrayInterface) {
  16195. map = remapping(sourcemapList, () => null);
  16196. } else {
  16197. map = remapping(sourcemapList[0], function loader(sourcefile) {
  16198. const mapForSources = sourcemapList.slice(mapIndex).find((s) => s.sources.includes(sourcefile));
  16199. if (mapForSources) {
  16200. mapIndex++;
  16201. return mapForSources;
  16202. }
  16203. return null;
  16204. });
  16205. }
  16206. if (!map.file) {
  16207. delete map.file;
  16208. }
  16209. map.sources = map.sources.map(
  16210. (source) => source ? unescapeToLinuxLikePath(source) : source
  16211. );
  16212. map.file = filename;
  16213. return map;
  16214. }
  16215. function unique(arr) {
  16216. return Array.from(new Set(arr));
  16217. }
  16218. async function getLocalhostAddressIfDiffersFromDNS() {
  16219. const [nodeResult, dnsResult] = await Promise.all([
  16220. promises.lookup("localhost"),
  16221. promises.lookup("localhost", { verbatim: true })
  16222. ]);
  16223. const isSame = nodeResult.family === dnsResult.family && nodeResult.address === dnsResult.address;
  16224. return isSame ? void 0 : nodeResult.address;
  16225. }
  16226. function diffDnsOrderChange(oldUrls, newUrls) {
  16227. return !(oldUrls === newUrls || oldUrls && newUrls && arrayEqual(oldUrls.local, newUrls.local) && arrayEqual(oldUrls.network, newUrls.network));
  16228. }
  16229. async function resolveHostname(optionsHost) {
  16230. let host;
  16231. if (optionsHost === void 0 || optionsHost === false) {
  16232. host = "localhost";
  16233. } else if (optionsHost === true) {
  16234. host = void 0;
  16235. } else {
  16236. host = optionsHost;
  16237. }
  16238. let name = host === void 0 || wildcardHosts.has(host) ? "localhost" : host;
  16239. if (host === "localhost") {
  16240. const localhostAddr = await getLocalhostAddressIfDiffersFromDNS();
  16241. if (localhostAddr) {
  16242. name = localhostAddr;
  16243. }
  16244. }
  16245. return { host, name };
  16246. }
  16247. async function resolveServerUrls(server, options, config) {
  16248. const address = server.address();
  16249. const isAddressInfo = (x) => x?.address;
  16250. if (!isAddressInfo(address)) {
  16251. return { local: [], network: [] };
  16252. }
  16253. const local = [];
  16254. const network = [];
  16255. const hostname = await resolveHostname(options.host);
  16256. const protocol = options.https ? "https" : "http";
  16257. const port = address.port;
  16258. const base = config.rawBase === "./" || config.rawBase === "" ? "/" : config.rawBase;
  16259. if (hostname.host !== void 0 && !wildcardHosts.has(hostname.host)) {
  16260. let hostnameName = hostname.name;
  16261. if (hostnameName.includes(":")) {
  16262. hostnameName = `[${hostnameName}]`;
  16263. }
  16264. const address2 = `${protocol}://${hostnameName}:${port}${base}`;
  16265. if (loopbackHosts.has(hostname.host)) {
  16266. local.push(address2);
  16267. } else {
  16268. network.push(address2);
  16269. }
  16270. } else {
  16271. Object.values(os$5.networkInterfaces()).flatMap((nInterface) => nInterface ?? []).filter(
  16272. (detail) => detail && detail.address && (detail.family === "IPv4" || // @ts-expect-error Node 18.0 - 18.3 returns number
  16273. detail.family === 4)
  16274. ).forEach((detail) => {
  16275. let host = detail.address.replace("127.0.0.1", hostname.name);
  16276. if (host.includes(":")) {
  16277. host = `[${host}]`;
  16278. }
  16279. const url = `${protocol}://${host}:${port}${base}`;
  16280. if (detail.address.includes("127.0.0.1")) {
  16281. local.push(url);
  16282. } else {
  16283. network.push(url);
  16284. }
  16285. });
  16286. }
  16287. return { local, network };
  16288. }
  16289. function arraify(target) {
  16290. return Array.isArray(target) ? target : [target];
  16291. }
  16292. const multilineCommentsRE = /\/\*[^*]*\*+(?:[^/*][^*]*\*+)*\//g;
  16293. const singlelineCommentsRE = /\/\/.*/g;
  16294. const requestQuerySplitRE = /\?(?!.*[/|}])/;
  16295. const requestQueryMaybeEscapedSplitRE = /\\?\?(?!.*[/|}])/;
  16296. const blankReplacer = (match) => " ".repeat(match.length);
  16297. function getHash(text, length = 8) {
  16298. const h = createHash$2("sha256").update(text).digest("hex").substring(0, length);
  16299. if (length <= 64) return h;
  16300. return h.padEnd(length, "_");
  16301. }
  16302. const _dirname = path$n.dirname(fileURLToPath(import.meta.url));
  16303. const requireResolveFromRootWithFallback = (root, id) => {
  16304. const found = resolvePackageData(id, root) || resolvePackageData(id, _dirname);
  16305. if (!found) {
  16306. const error = new Error(`${JSON.stringify(id)} not found.`);
  16307. error.code = "MODULE_NOT_FOUND";
  16308. throw error;
  16309. }
  16310. return _require$1.resolve(id, { paths: [root, _dirname] });
  16311. };
  16312. function emptyCssComments(raw) {
  16313. return raw.replace(multilineCommentsRE, blankReplacer);
  16314. }
  16315. function backwardCompatibleWorkerPlugins(plugins) {
  16316. if (Array.isArray(plugins)) {
  16317. return plugins;
  16318. }
  16319. if (typeof plugins === "function") {
  16320. return plugins();
  16321. }
  16322. return [];
  16323. }
  16324. function mergeConfigRecursively(defaults, overrides, rootPath) {
  16325. const merged = { ...defaults };
  16326. for (const key in overrides) {
  16327. const value = overrides[key];
  16328. if (value == null) {
  16329. continue;
  16330. }
  16331. const existing = merged[key];
  16332. if (existing == null) {
  16333. merged[key] = value;
  16334. continue;
  16335. }
  16336. if (key === "alias" && (rootPath === "resolve" || rootPath === "")) {
  16337. merged[key] = mergeAlias(existing, value);
  16338. continue;
  16339. } else if (key === "assetsInclude" && rootPath === "") {
  16340. merged[key] = [].concat(existing, value);
  16341. continue;
  16342. } else if (key === "noExternal" && rootPath === "ssr" && (existing === true || value === true)) {
  16343. merged[key] = true;
  16344. continue;
  16345. } else if (key === "plugins" && rootPath === "worker") {
  16346. merged[key] = () => [
  16347. ...backwardCompatibleWorkerPlugins(existing),
  16348. ...backwardCompatibleWorkerPlugins(value)
  16349. ];
  16350. continue;
  16351. } else if (key === "server" && rootPath === "server.hmr") {
  16352. merged[key] = value;
  16353. continue;
  16354. }
  16355. if (Array.isArray(existing) || Array.isArray(value)) {
  16356. merged[key] = [...arraify(existing), ...arraify(value)];
  16357. continue;
  16358. }
  16359. if (isObject$1(existing) && isObject$1(value)) {
  16360. merged[key] = mergeConfigRecursively(
  16361. existing,
  16362. value,
  16363. rootPath ? `${rootPath}.${key}` : key
  16364. );
  16365. continue;
  16366. }
  16367. merged[key] = value;
  16368. }
  16369. return merged;
  16370. }
  16371. function mergeConfig(defaults, overrides, isRoot = true) {
  16372. if (typeof defaults === "function" || typeof overrides === "function") {
  16373. throw new Error(`Cannot merge config in form of callback`);
  16374. }
  16375. return mergeConfigRecursively(defaults, overrides, isRoot ? "" : ".");
  16376. }
  16377. function mergeAlias(a, b) {
  16378. if (!a) return b;
  16379. if (!b) return a;
  16380. if (isObject$1(a) && isObject$1(b)) {
  16381. return { ...a, ...b };
  16382. }
  16383. return [...normalizeAlias(b), ...normalizeAlias(a)];
  16384. }
  16385. function normalizeAlias(o = []) {
  16386. return Array.isArray(o) ? o.map(normalizeSingleAlias) : Object.keys(o).map(
  16387. (find) => normalizeSingleAlias({
  16388. find,
  16389. replacement: o[find]
  16390. })
  16391. );
  16392. }
  16393. function normalizeSingleAlias({
  16394. find,
  16395. replacement,
  16396. customResolver
  16397. }) {
  16398. if (typeof find === "string" && find[find.length - 1] === "/" && replacement[replacement.length - 1] === "/") {
  16399. find = find.slice(0, find.length - 1);
  16400. replacement = replacement.slice(0, replacement.length - 1);
  16401. }
  16402. const alias = {
  16403. find,
  16404. replacement
  16405. };
  16406. if (customResolver) {
  16407. alias.customResolver = customResolver;
  16408. }
  16409. return alias;
  16410. }
  16411. function transformStableResult(s, id, config) {
  16412. return {
  16413. code: s.toString(),
  16414. map: config.command === "build" && config.build.sourcemap ? s.generateMap({ hires: "boundary", source: id }) : null
  16415. };
  16416. }
  16417. async function asyncFlatten(arr) {
  16418. do {
  16419. arr = (await Promise.all(arr)).flat(Infinity);
  16420. } while (arr.some((v) => v?.then));
  16421. return arr;
  16422. }
  16423. function stripBomTag(content) {
  16424. if (content.charCodeAt(0) === 65279) {
  16425. return content.slice(1);
  16426. }
  16427. return content;
  16428. }
  16429. const windowsDrivePathPrefixRE = /^[A-Za-z]:[/\\]/;
  16430. const isNonDriveRelativeAbsolutePath = (p) => {
  16431. if (!isWindows$3) return p[0] === "/";
  16432. return windowsDrivePathPrefixRE.test(p);
  16433. };
  16434. function shouldServeFile(filePath, root) {
  16435. if (!isCaseInsensitiveFS) return true;
  16436. return hasCorrectCase(filePath, root);
  16437. }
  16438. function hasCorrectCase(file, assets) {
  16439. if (file === assets) return true;
  16440. const parent = path$n.dirname(file);
  16441. if (fs__default.readdirSync(parent).includes(path$n.basename(file))) {
  16442. return hasCorrectCase(parent, assets);
  16443. }
  16444. return false;
  16445. }
  16446. function joinUrlSegments(a, b) {
  16447. if (!a || !b) {
  16448. return a || b || "";
  16449. }
  16450. if (a[a.length - 1] === "/") {
  16451. a = a.substring(0, a.length - 1);
  16452. }
  16453. if (b[0] !== "/") {
  16454. b = "/" + b;
  16455. }
  16456. return a + b;
  16457. }
  16458. function removeLeadingSlash(str) {
  16459. return str[0] === "/" ? str.slice(1) : str;
  16460. }
  16461. function stripBase(path2, base) {
  16462. if (path2 === base) {
  16463. return "/";
  16464. }
  16465. const devBase = withTrailingSlash(base);
  16466. return path2.startsWith(devBase) ? path2.slice(devBase.length - 1) : path2;
  16467. }
  16468. function arrayEqual(a, b) {
  16469. if (a === b) return true;
  16470. if (a.length !== b.length) return false;
  16471. for (let i = 0; i < a.length; i++) {
  16472. if (a[i] !== b[i]) return false;
  16473. }
  16474. return true;
  16475. }
  16476. function evalValue(rawValue) {
  16477. const fn = new Function(`
  16478. var console, exports, global, module, process, require
  16479. return (
  16480. ${rawValue}
  16481. )
  16482. `);
  16483. return fn();
  16484. }
  16485. function getNpmPackageName(importPath) {
  16486. const parts = importPath.split("/");
  16487. if (parts[0][0] === "@") {
  16488. if (!parts[1]) return null;
  16489. return `${parts[0]}/${parts[1]}`;
  16490. } else {
  16491. return parts[0];
  16492. }
  16493. }
  16494. const escapeRegexRE = /[-/\\^$*+?.()|[\]{}]/g;
  16495. function escapeRegex(str) {
  16496. return str.replace(escapeRegexRE, "\\$&");
  16497. }
  16498. function getPackageManagerCommand(type = "install") {
  16499. const packageManager = process.env.npm_config_user_agent?.split(" ")[0].split("/")[0] || "npm";
  16500. switch (type) {
  16501. case "install":
  16502. return packageManager === "npm" ? "npm install" : `${packageManager} add`;
  16503. case "uninstall":
  16504. return packageManager === "npm" ? "npm uninstall" : `${packageManager} remove`;
  16505. case "update":
  16506. return packageManager === "yarn" ? "yarn upgrade" : `${packageManager} update`;
  16507. default:
  16508. throw new TypeError(`Unknown command type: ${type}`);
  16509. }
  16510. }
  16511. function isDevServer(server) {
  16512. return "pluginContainer" in server;
  16513. }
  16514. function promiseWithResolvers() {
  16515. let resolve;
  16516. let reject;
  16517. const promise = new Promise((_resolve, _reject) => {
  16518. resolve = _resolve;
  16519. reject = _reject;
  16520. });
  16521. return { promise, resolve, reject };
  16522. }
  16523. function createSerialPromiseQueue() {
  16524. let previousTask;
  16525. return {
  16526. async run(f) {
  16527. const thisTask = f();
  16528. const depTasks = Promise.all([previousTask, thisTask]);
  16529. previousTask = depTasks;
  16530. const [, result] = await depTasks;
  16531. if (previousTask === depTasks) {
  16532. previousTask = void 0;
  16533. }
  16534. return result;
  16535. }
  16536. };
  16537. }
  16538. function sortObjectKeys(obj) {
  16539. const sorted = {};
  16540. for (const key of Object.keys(obj).sort()) {
  16541. sorted[key] = obj[key];
  16542. }
  16543. return sorted;
  16544. }
  16545. function displayTime(time) {
  16546. if (time < 1e3) {
  16547. return `${time}ms`;
  16548. }
  16549. time = time / 1e3;
  16550. if (time < 60) {
  16551. return `${time.toFixed(2)}s`;
  16552. }
  16553. const mins = parseInt((time / 60).toString());
  16554. const seconds = time % 60;
  16555. return `${mins}m${seconds < 1 ? "" : ` ${seconds.toFixed(0)}s`}`;
  16556. }
  16557. function encodeURIPath(uri) {
  16558. if (uri.startsWith("data:")) return uri;
  16559. const filePath = cleanUrl(uri);
  16560. const postfix = filePath !== uri ? uri.slice(filePath.length) : "";
  16561. return encodeURI(filePath) + postfix;
  16562. }
  16563. function partialEncodeURIPath(uri) {
  16564. if (uri.startsWith("data:")) return uri;
  16565. const filePath = cleanUrl(uri);
  16566. const postfix = filePath !== uri ? uri.slice(filePath.length) : "";
  16567. return filePath.replaceAll("%", "%25") + postfix;
  16568. }
  16569. const setupSIGTERMListener = (callback) => {
  16570. process.once("SIGTERM", callback);
  16571. if (process.env.CI !== "true") {
  16572. process.stdin.on("end", callback);
  16573. }
  16574. };
  16575. const teardownSIGTERMListener = (callback) => {
  16576. process.off("SIGTERM", callback);
  16577. if (process.env.CI !== "true") {
  16578. process.stdin.off("end", callback);
  16579. }
  16580. };
  16581. const LogLevels = {
  16582. silent: 0,
  16583. error: 1,
  16584. warn: 2,
  16585. info: 3
  16586. };
  16587. let lastType;
  16588. let lastMsg;
  16589. let sameCount = 0;
  16590. function clearScreen() {
  16591. const repeatCount = process.stdout.rows - 2;
  16592. const blank = repeatCount > 0 ? "\n".repeat(repeatCount) : "";
  16593. console.log(blank);
  16594. readline.cursorTo(process.stdout, 0, 0);
  16595. readline.clearScreenDown(process.stdout);
  16596. }
  16597. let timeFormatter;
  16598. function getTimeFormatter() {
  16599. timeFormatter ??= new Intl.DateTimeFormat(void 0, {
  16600. hour: "numeric",
  16601. minute: "numeric",
  16602. second: "numeric"
  16603. });
  16604. return timeFormatter;
  16605. }
  16606. function createLogger(level = "info", options = {}) {
  16607. if (options.customLogger) {
  16608. return options.customLogger;
  16609. }
  16610. const loggedErrors = /* @__PURE__ */ new WeakSet();
  16611. const { prefix = "[vite]", allowClearScreen = true } = options;
  16612. const thresh = LogLevels[level];
  16613. const canClearScreen = allowClearScreen && process.stdout.isTTY && !process.env.CI;
  16614. const clear = canClearScreen ? clearScreen : () => {
  16615. };
  16616. function format(type, msg, options2 = {}) {
  16617. if (options2.timestamp) {
  16618. let tag = "";
  16619. if (type === "info") {
  16620. tag = colors$1.cyan(colors$1.bold(prefix));
  16621. } else if (type === "warn") {
  16622. tag = colors$1.yellow(colors$1.bold(prefix));
  16623. } else {
  16624. tag = colors$1.red(colors$1.bold(prefix));
  16625. }
  16626. return `${colors$1.dim(getTimeFormatter().format(/* @__PURE__ */ new Date()))} ${tag} ${msg}`;
  16627. } else {
  16628. return msg;
  16629. }
  16630. }
  16631. function output(type, msg, options2 = {}) {
  16632. if (thresh >= LogLevels[type]) {
  16633. const method = type === "info" ? "log" : type;
  16634. if (options2.error) {
  16635. loggedErrors.add(options2.error);
  16636. }
  16637. if (canClearScreen) {
  16638. if (type === lastType && msg === lastMsg) {
  16639. sameCount++;
  16640. clear();
  16641. console[method](
  16642. format(type, msg, options2),
  16643. colors$1.yellow(`(x${sameCount + 1})`)
  16644. );
  16645. } else {
  16646. sameCount = 0;
  16647. lastMsg = msg;
  16648. lastType = type;
  16649. if (options2.clear) {
  16650. clear();
  16651. }
  16652. console[method](format(type, msg, options2));
  16653. }
  16654. } else {
  16655. console[method](format(type, msg, options2));
  16656. }
  16657. }
  16658. }
  16659. const warnedMessages = /* @__PURE__ */ new Set();
  16660. const logger = {
  16661. hasWarned: false,
  16662. info(msg, opts) {
  16663. output("info", msg, opts);
  16664. },
  16665. warn(msg, opts) {
  16666. logger.hasWarned = true;
  16667. output("warn", msg, opts);
  16668. },
  16669. warnOnce(msg, opts) {
  16670. if (warnedMessages.has(msg)) return;
  16671. logger.hasWarned = true;
  16672. output("warn", msg, opts);
  16673. warnedMessages.add(msg);
  16674. },
  16675. error(msg, opts) {
  16676. logger.hasWarned = true;
  16677. output("error", msg, opts);
  16678. },
  16679. clearScreen(type) {
  16680. if (thresh >= LogLevels[type]) {
  16681. clear();
  16682. }
  16683. },
  16684. hasErrorLogged(error) {
  16685. return loggedErrors.has(error);
  16686. }
  16687. };
  16688. return logger;
  16689. }
  16690. function printServerUrls(urls, optionsHost, info) {
  16691. const colorUrl = (url) => colors$1.cyan(url.replace(/:(\d+)\//, (_, port) => `:${colors$1.bold(port)}/`));
  16692. for (const url of urls.local) {
  16693. info(` ${colors$1.green("\u279C")} ${colors$1.bold("Local")}: ${colorUrl(url)}`);
  16694. }
  16695. for (const url of urls.network) {
  16696. info(` ${colors$1.green("\u279C")} ${colors$1.bold("Network")}: ${colorUrl(url)}`);
  16697. }
  16698. if (urls.network.length === 0 && optionsHost === void 0) {
  16699. info(
  16700. colors$1.dim(` ${colors$1.green("\u279C")} ${colors$1.bold("Network")}: use `) + colors$1.bold("--host") + colors$1.dim(" to expose")
  16701. );
  16702. }
  16703. }
  16704. const groups = [
  16705. { name: "Assets", color: colors$1.green },
  16706. { name: "CSS", color: colors$1.magenta },
  16707. { name: "JS", color: colors$1.cyan }
  16708. ];
  16709. const COMPRESSIBLE_ASSETS_RE = /\.(?:html|json|svg|txt|xml|xhtml)$/;
  16710. function buildReporterPlugin(config) {
  16711. const compress = promisify$4(gzip);
  16712. const chunkLimit = config.build.chunkSizeWarningLimit;
  16713. const numberFormatter = new Intl.NumberFormat("en", {
  16714. maximumFractionDigits: 2,
  16715. minimumFractionDigits: 2
  16716. });
  16717. const displaySize = (bytes) => {
  16718. return `${numberFormatter.format(bytes / 1e3)} kB`;
  16719. };
  16720. const tty = process.stdout.isTTY && !process.env.CI;
  16721. const shouldLogInfo = LogLevels[config.logLevel || "info"] >= LogLevels.info;
  16722. let hasTransformed = false;
  16723. let hasRenderedChunk = false;
  16724. let hasCompressChunk = false;
  16725. let transformedCount = 0;
  16726. let chunkCount = 0;
  16727. let compressedCount = 0;
  16728. async function getCompressedSize(code) {
  16729. if (config.build.ssr || !config.build.reportCompressedSize) {
  16730. return null;
  16731. }
  16732. if (shouldLogInfo && !hasCompressChunk) {
  16733. if (!tty) {
  16734. config.logger.info("computing gzip size...");
  16735. } else {
  16736. writeLine("computing gzip size (0)...");
  16737. }
  16738. hasCompressChunk = true;
  16739. }
  16740. const compressed = await compress(
  16741. typeof code === "string" ? code : Buffer.from(code)
  16742. );
  16743. compressedCount++;
  16744. if (shouldLogInfo && tty) {
  16745. writeLine(`computing gzip size (${compressedCount})...`);
  16746. }
  16747. return compressed.length;
  16748. }
  16749. const logTransform = throttle((id) => {
  16750. writeLine(
  16751. `transforming (${transformedCount}) ${colors$1.dim(
  16752. path$n.relative(config.root, id)
  16753. )}`
  16754. );
  16755. });
  16756. return {
  16757. name: "vite:reporter",
  16758. transform(_, id) {
  16759. transformedCount++;
  16760. if (shouldLogInfo) {
  16761. if (!tty) {
  16762. if (!hasTransformed) {
  16763. config.logger.info(`transforming...`);
  16764. }
  16765. } else {
  16766. if (id.includes(`?`)) return;
  16767. logTransform(id);
  16768. }
  16769. hasTransformed = true;
  16770. }
  16771. return null;
  16772. },
  16773. buildStart() {
  16774. transformedCount = 0;
  16775. },
  16776. buildEnd() {
  16777. if (shouldLogInfo) {
  16778. if (tty) {
  16779. clearLine$1();
  16780. }
  16781. config.logger.info(
  16782. `${colors$1.green(`\u2713`)} ${transformedCount} modules transformed.`
  16783. );
  16784. }
  16785. },
  16786. renderStart() {
  16787. chunkCount = 0;
  16788. compressedCount = 0;
  16789. },
  16790. renderChunk(code, chunk, options) {
  16791. if (!options.inlineDynamicImports) {
  16792. for (const id of chunk.moduleIds) {
  16793. const module = this.getModuleInfo(id);
  16794. if (!module) continue;
  16795. if (module.importers.length && module.dynamicImporters.length) {
  16796. const detectedIneffectiveDynamicImport = module.dynamicImporters.some(
  16797. (id2) => !isInNodeModules$1(id2) && chunk.moduleIds.includes(id2)
  16798. );
  16799. if (detectedIneffectiveDynamicImport) {
  16800. this.warn(
  16801. `
  16802. (!) ${module.id} is dynamically imported by ${module.dynamicImporters.join(
  16803. ", "
  16804. )} but also statically imported by ${module.importers.join(
  16805. ", "
  16806. )}, dynamic import will not move module into another chunk.
  16807. `
  16808. );
  16809. }
  16810. }
  16811. }
  16812. }
  16813. chunkCount++;
  16814. if (shouldLogInfo) {
  16815. if (!tty) {
  16816. if (!hasRenderedChunk) {
  16817. config.logger.info("rendering chunks...");
  16818. }
  16819. } else {
  16820. writeLine(`rendering chunks (${chunkCount})...`);
  16821. }
  16822. hasRenderedChunk = true;
  16823. }
  16824. return null;
  16825. },
  16826. generateBundle() {
  16827. if (shouldLogInfo && tty) clearLine$1();
  16828. },
  16829. async writeBundle({ dir: outDir }, output) {
  16830. let hasLargeChunks = false;
  16831. if (shouldLogInfo) {
  16832. const entries = (await Promise.all(
  16833. Object.values(output).map(
  16834. async (chunk) => {
  16835. if (chunk.type === "chunk") {
  16836. return {
  16837. name: chunk.fileName,
  16838. group: "JS",
  16839. size: chunk.code.length,
  16840. compressedSize: await getCompressedSize(chunk.code),
  16841. mapSize: chunk.map ? chunk.map.toString().length : null
  16842. };
  16843. } else {
  16844. if (chunk.fileName.endsWith(".map")) return null;
  16845. const isCSS = chunk.fileName.endsWith(".css");
  16846. const isCompressible = isCSS || COMPRESSIBLE_ASSETS_RE.test(chunk.fileName);
  16847. return {
  16848. name: chunk.fileName,
  16849. group: isCSS ? "CSS" : "Assets",
  16850. size: chunk.source.length,
  16851. mapSize: null,
  16852. // Rollup doesn't support CSS maps?
  16853. compressedSize: isCompressible ? await getCompressedSize(chunk.source) : null
  16854. };
  16855. }
  16856. }
  16857. )
  16858. )).filter(isDefined);
  16859. if (tty) clearLine$1();
  16860. let longest = 0;
  16861. let biggestSize = 0;
  16862. let biggestMap = 0;
  16863. let biggestCompressSize = 0;
  16864. for (const entry of entries) {
  16865. if (entry.name.length > longest) longest = entry.name.length;
  16866. if (entry.size > biggestSize) biggestSize = entry.size;
  16867. if (entry.mapSize && entry.mapSize > biggestMap) {
  16868. biggestMap = entry.mapSize;
  16869. }
  16870. if (entry.compressedSize && entry.compressedSize > biggestCompressSize) {
  16871. biggestCompressSize = entry.compressedSize;
  16872. }
  16873. }
  16874. const sizePad = displaySize(biggestSize).length;
  16875. const mapPad = displaySize(biggestMap).length;
  16876. const compressPad = displaySize(biggestCompressSize).length;
  16877. const relativeOutDir = normalizePath$3(
  16878. path$n.relative(
  16879. config.root,
  16880. path$n.resolve(config.root, outDir ?? config.build.outDir)
  16881. )
  16882. );
  16883. const assetsDir = path$n.join(config.build.assetsDir, "/");
  16884. for (const group of groups) {
  16885. const filtered = entries.filter((e) => e.group === group.name);
  16886. if (!filtered.length) continue;
  16887. for (const entry of filtered.sort((a, z) => a.size - z.size)) {
  16888. const isLarge = group.name === "JS" && entry.size / 1e3 > chunkLimit;
  16889. if (isLarge) hasLargeChunks = true;
  16890. const sizeColor = isLarge ? colors$1.yellow : colors$1.dim;
  16891. let log = colors$1.dim(withTrailingSlash(relativeOutDir));
  16892. log += !config.build.lib && entry.name.startsWith(withTrailingSlash(assetsDir)) ? colors$1.dim(assetsDir) + group.color(
  16893. entry.name.slice(assetsDir.length).padEnd(longest + 2 - assetsDir.length)
  16894. ) : group.color(entry.name.padEnd(longest + 2));
  16895. log += colors$1.bold(
  16896. sizeColor(displaySize(entry.size).padStart(sizePad))
  16897. );
  16898. if (entry.compressedSize) {
  16899. log += colors$1.dim(
  16900. ` \u2502 gzip: ${displaySize(entry.compressedSize).padStart(
  16901. compressPad
  16902. )}`
  16903. );
  16904. }
  16905. if (entry.mapSize) {
  16906. log += colors$1.dim(
  16907. ` \u2502 map: ${displaySize(entry.mapSize).padStart(mapPad)}`
  16908. );
  16909. }
  16910. config.logger.info(log);
  16911. }
  16912. }
  16913. } else {
  16914. hasLargeChunks = Object.values(output).some((chunk) => {
  16915. return chunk.type === "chunk" && chunk.code.length / 1e3 > chunkLimit;
  16916. });
  16917. }
  16918. if (hasLargeChunks && config.build.minify && !config.build.lib && !config.build.ssr) {
  16919. config.logger.warn(
  16920. colors$1.yellow(
  16921. `
  16922. (!) Some chunks are larger than ${chunkLimit} kB after minification. Consider:
  16923. - Using dynamic import() to code-split the application
  16924. - Use build.rollupOptions.output.manualChunks to improve chunking: https://rollupjs.org/configuration-options/#output-manualchunks
  16925. - Adjust chunk size limit for this warning via build.chunkSizeWarningLimit.`
  16926. )
  16927. );
  16928. }
  16929. }
  16930. };
  16931. }
  16932. function writeLine(output) {
  16933. clearLine$1();
  16934. if (output.length < process.stdout.columns) {
  16935. process.stdout.write(output);
  16936. } else {
  16937. process.stdout.write(output.substring(0, process.stdout.columns - 1));
  16938. }
  16939. }
  16940. function clearLine$1() {
  16941. process.stdout.clearLine(0);
  16942. process.stdout.cursorTo(0);
  16943. }
  16944. function throttle(fn) {
  16945. let timerHandle = null;
  16946. return (...args) => {
  16947. if (timerHandle) return;
  16948. fn(...args);
  16949. timerHandle = setTimeout(() => {
  16950. timerHandle = null;
  16951. }, 100);
  16952. };
  16953. }
  16954. const POSIX_SEP_RE = new RegExp('\\' + path$n.posix.sep, 'g');
  16955. const NATIVE_SEP_RE = new RegExp('\\' + path$n.sep, 'g');
  16956. /** @type {Map<string,RegExp>}*/
  16957. const PATTERN_REGEX_CACHE = new Map();
  16958. const GLOB_ALL_PATTERN = `**/*`;
  16959. const TS_EXTENSIONS = ['.ts', '.tsx', '.mts', '.cts'];
  16960. const JS_EXTENSIONS = ['.js', '.jsx', '.mjs', '.cjs'];
  16961. const TSJS_EXTENSIONS = TS_EXTENSIONS.concat(JS_EXTENSIONS);
  16962. const TS_EXTENSIONS_RE_GROUP = `\\.(?:${TS_EXTENSIONS.map((ext) => ext.substring(1)).join('|')})`;
  16963. const TSJS_EXTENSIONS_RE_GROUP = `\\.(?:${TSJS_EXTENSIONS.map((ext) => ext.substring(1)).join(
  16964. '|'
  16965. )})`;
  16966. const IS_POSIX = path$n.posix.sep === path$n.sep;
  16967. /**
  16968. * @template T
  16969. * @returns {{resolve:(result:T)=>void, reject:(error:any)=>void, promise: Promise<T>}}
  16970. */
  16971. function makePromise() {
  16972. let resolve, reject;
  16973. const promise = new Promise((res, rej) => {
  16974. resolve = res;
  16975. reject = rej;
  16976. });
  16977. return { promise, resolve, reject };
  16978. }
  16979. /**
  16980. * @param {string} filename
  16981. * @param {import('./cache.js').TSConfckCache} [cache]
  16982. * @returns {Promise<string|void>}
  16983. */
  16984. async function resolveTSConfigJson(filename, cache) {
  16985. if (path$n.extname(filename) !== '.json') {
  16986. return; // ignore files that are not json
  16987. }
  16988. const tsconfig = path$n.resolve(filename);
  16989. if (cache && (cache.hasParseResult(tsconfig) || cache.hasParseResult(filename))) {
  16990. return tsconfig;
  16991. }
  16992. return promises$1.stat(tsconfig).then((stat) => {
  16993. if (stat.isFile() || stat.isFIFO()) {
  16994. return tsconfig;
  16995. } else {
  16996. throw new Error(`${filename} exists but is not a regular file.`);
  16997. }
  16998. });
  16999. }
  17000. /**
  17001. *
  17002. * @param {string} dir an absolute directory path
  17003. * @returns {boolean} if dir path includes a node_modules segment
  17004. */
  17005. const isInNodeModules = IS_POSIX
  17006. ? (dir) => dir.includes('/node_modules/')
  17007. : (dir) => dir.match(/[/\\]node_modules[/\\]/);
  17008. /**
  17009. * convert posix separator to native separator
  17010. *
  17011. * eg.
  17012. * windows: C:/foo/bar -> c:\foo\bar
  17013. * linux: /foo/bar -> /foo/bar
  17014. *
  17015. * @param {string} filename with posix separators
  17016. * @returns {string} filename with native separators
  17017. */
  17018. const posix2native = IS_POSIX
  17019. ? (filename) => filename
  17020. : (filename) => filename.replace(POSIX_SEP_RE, path$n.sep);
  17021. /**
  17022. * convert native separator to posix separator
  17023. *
  17024. * eg.
  17025. * windows: C:\foo\bar -> c:/foo/bar
  17026. * linux: /foo/bar -> /foo/bar
  17027. *
  17028. * @param {string} filename - filename with native separators
  17029. * @returns {string} filename with posix separators
  17030. */
  17031. const native2posix = IS_POSIX
  17032. ? (filename) => filename
  17033. : (filename) => filename.replace(NATIVE_SEP_RE, path$n.posix.sep);
  17034. /**
  17035. * converts params to native separator, resolves path and converts native back to posix
  17036. *
  17037. * needed on windows to handle posix paths in tsconfig
  17038. *
  17039. * @param dir {string|null} directory to resolve from
  17040. * @param filename {string} filename or pattern to resolve
  17041. * @returns string
  17042. */
  17043. const resolve2posix = IS_POSIX
  17044. ? (dir, filename) => (dir ? path$n.resolve(dir, filename) : path$n.resolve(filename))
  17045. : (dir, filename) =>
  17046. native2posix(
  17047. dir
  17048. ? path$n.resolve(posix2native(dir), posix2native(filename))
  17049. : path$n.resolve(posix2native(filename))
  17050. );
  17051. /**
  17052. *
  17053. * @param {import('./public.d.ts').TSConfckParseResult} result
  17054. * @param {import('./public.d.ts').TSConfckParseOptions} [options]
  17055. * @returns {string[]}
  17056. */
  17057. function resolveReferencedTSConfigFiles(result, options) {
  17058. const dir = path$n.dirname(result.tsconfigFile);
  17059. return result.tsconfig.references.map((ref) => {
  17060. const refPath = ref.path.endsWith('.json')
  17061. ? ref.path
  17062. : path$n.join(ref.path, options?.configName ?? 'tsconfig.json');
  17063. return resolve2posix(dir, refPath);
  17064. });
  17065. }
  17066. /**
  17067. * @param {string} filename
  17068. * @param {import('./public.d.ts').TSConfckParseResult} result
  17069. * @returns {import('./public.d.ts').TSConfckParseResult}
  17070. */
  17071. function resolveSolutionTSConfig(filename, result) {
  17072. const allowJs = result.tsconfig.compilerOptions?.allowJs;
  17073. const extensions = allowJs ? TSJS_EXTENSIONS : TS_EXTENSIONS;
  17074. if (
  17075. result.referenced &&
  17076. extensions.some((ext) => filename.endsWith(ext)) &&
  17077. !isIncluded(filename, result)
  17078. ) {
  17079. const solutionTSConfig = result.referenced.find((referenced) =>
  17080. isIncluded(filename, referenced)
  17081. );
  17082. if (solutionTSConfig) {
  17083. return solutionTSConfig;
  17084. }
  17085. }
  17086. return result;
  17087. }
  17088. /**
  17089. *
  17090. * @param {string} filename
  17091. * @param {import('./public.d.ts').TSConfckParseResult} result
  17092. * @returns {boolean}
  17093. */
  17094. function isIncluded(filename, result) {
  17095. const dir = native2posix(path$n.dirname(result.tsconfigFile));
  17096. const files = (result.tsconfig.files || []).map((file) => resolve2posix(dir, file));
  17097. const absoluteFilename = resolve2posix(null, filename);
  17098. if (files.includes(filename)) {
  17099. return true;
  17100. }
  17101. const allowJs = result.tsconfig.compilerOptions?.allowJs;
  17102. const isIncluded = isGlobMatch(
  17103. absoluteFilename,
  17104. dir,
  17105. result.tsconfig.include || (result.tsconfig.files ? [] : [GLOB_ALL_PATTERN]),
  17106. allowJs
  17107. );
  17108. if (isIncluded) {
  17109. const isExcluded = isGlobMatch(absoluteFilename, dir, result.tsconfig.exclude || [], allowJs);
  17110. return !isExcluded;
  17111. }
  17112. return false;
  17113. }
  17114. /**
  17115. * test filenames agains glob patterns in tsconfig
  17116. *
  17117. * @param filename {string} posix style abolute path to filename to test
  17118. * @param dir {string} posix style absolute path to directory of tsconfig containing patterns
  17119. * @param patterns {string[]} glob patterns to match against
  17120. * @param allowJs {boolean} allowJs setting in tsconfig to include js extensions in checks
  17121. * @returns {boolean} true when at least one pattern matches filename
  17122. */
  17123. function isGlobMatch(filename, dir, patterns, allowJs) {
  17124. const extensions = allowJs ? TSJS_EXTENSIONS : TS_EXTENSIONS;
  17125. return patterns.some((pattern) => {
  17126. // filename must end with part of pattern that comes after last wildcard
  17127. let lastWildcardIndex = pattern.length;
  17128. let hasWildcard = false;
  17129. let hasExtension = false;
  17130. let hasSlash = false;
  17131. let lastSlashIndex = -1;
  17132. for (let i = pattern.length - 1; i > -1; i--) {
  17133. const c = pattern[i];
  17134. if (!hasWildcard) {
  17135. if (c === '*' || c === '?') {
  17136. lastWildcardIndex = i;
  17137. hasWildcard = true;
  17138. }
  17139. }
  17140. if (!hasSlash) {
  17141. if (c === '.') {
  17142. hasExtension = true;
  17143. } else if (c === '/') {
  17144. lastSlashIndex = i;
  17145. hasSlash = true;
  17146. }
  17147. }
  17148. if (hasWildcard && hasSlash) {
  17149. break;
  17150. }
  17151. }
  17152. if (!hasExtension && (!hasWildcard || lastWildcardIndex < lastSlashIndex)) {
  17153. // add implicit glob
  17154. pattern += `${pattern.endsWith('/') ? '' : '/'}${GLOB_ALL_PATTERN}`;
  17155. lastWildcardIndex = pattern.length - 1;
  17156. hasWildcard = true;
  17157. }
  17158. // if pattern does not end with wildcard, filename must end with pattern after last wildcard
  17159. if (
  17160. lastWildcardIndex < pattern.length - 1 &&
  17161. !filename.endsWith(pattern.slice(lastWildcardIndex + 1))
  17162. ) {
  17163. return false;
  17164. }
  17165. // if pattern ends with *, filename must end with a default extension
  17166. if (pattern.endsWith('*') && !extensions.some((ext) => filename.endsWith(ext))) {
  17167. return false;
  17168. }
  17169. // for **/* , filename must start with the dir
  17170. if (pattern === GLOB_ALL_PATTERN) {
  17171. return filename.startsWith(`${dir}/`);
  17172. }
  17173. const resolvedPattern = resolve2posix(dir, pattern);
  17174. // filename must start with part of pattern that comes before first wildcard
  17175. let firstWildcardIndex = -1;
  17176. for (let i = 0; i < resolvedPattern.length; i++) {
  17177. if (resolvedPattern[i] === '*' || resolvedPattern[i] === '?') {
  17178. firstWildcardIndex = i;
  17179. hasWildcard = true;
  17180. break;
  17181. }
  17182. }
  17183. if (
  17184. firstWildcardIndex > 1 &&
  17185. !filename.startsWith(resolvedPattern.slice(0, firstWildcardIndex - 1))
  17186. ) {
  17187. return false;
  17188. }
  17189. if (!hasWildcard) {
  17190. // no wildcard in pattern, filename must be equal to resolved pattern
  17191. return filename === resolvedPattern;
  17192. } else if (
  17193. firstWildcardIndex + GLOB_ALL_PATTERN.length ===
  17194. resolvedPattern.length - (pattern.length - 1 - lastWildcardIndex) &&
  17195. resolvedPattern.slice(firstWildcardIndex, firstWildcardIndex + GLOB_ALL_PATTERN.length) ===
  17196. GLOB_ALL_PATTERN
  17197. ) {
  17198. // singular glob-all pattern and we already validated prefix and suffix matches
  17199. return true;
  17200. }
  17201. // complex pattern, use regex to check it
  17202. if (PATTERN_REGEX_CACHE.has(resolvedPattern)) {
  17203. return PATTERN_REGEX_CACHE.get(resolvedPattern).test(filename);
  17204. }
  17205. const regex = pattern2regex(resolvedPattern, allowJs);
  17206. PATTERN_REGEX_CACHE.set(resolvedPattern, regex);
  17207. return regex.test(filename);
  17208. });
  17209. }
  17210. /**
  17211. * @param {string} resolvedPattern
  17212. * @param {boolean} allowJs
  17213. * @returns {RegExp}
  17214. */
  17215. function pattern2regex(resolvedPattern, allowJs) {
  17216. let regexStr = '^';
  17217. for (let i = 0; i < resolvedPattern.length; i++) {
  17218. const char = resolvedPattern[i];
  17219. if (char === '?') {
  17220. regexStr += '[^\\/]';
  17221. continue;
  17222. }
  17223. if (char === '*') {
  17224. if (resolvedPattern[i + 1] === '*' && resolvedPattern[i + 2] === '/') {
  17225. i += 2;
  17226. regexStr += '(?:[^\\/]*\\/)*'; // zero or more path segments
  17227. continue;
  17228. }
  17229. regexStr += '[^\\/]*';
  17230. continue;
  17231. }
  17232. if ('/.+^${}()|[]\\'.includes(char)) {
  17233. regexStr += `\\`;
  17234. }
  17235. regexStr += char;
  17236. }
  17237. // add known file endings if pattern ends on *
  17238. if (resolvedPattern.endsWith('*')) {
  17239. regexStr += allowJs ? TSJS_EXTENSIONS_RE_GROUP : TS_EXTENSIONS_RE_GROUP;
  17240. }
  17241. regexStr += '$';
  17242. return new RegExp(regexStr);
  17243. }
  17244. /**
  17245. * replace tokens like ${configDir}
  17246. * @param {any} tsconfig
  17247. * @param {string} configDir
  17248. * @returns {any}
  17249. */
  17250. function replaceTokens(tsconfig, configDir) {
  17251. return JSON.parse(
  17252. JSON.stringify(tsconfig)
  17253. // replace ${configDir}
  17254. .replaceAll(/"\${configDir}/g, `"${native2posix(configDir)}`)
  17255. );
  17256. }
  17257. /**
  17258. * find the closest tsconfig.json file
  17259. *
  17260. * @param {string} filename - path to file to find tsconfig for (absolute or relative to cwd)
  17261. * @param {import('./public.d.ts').TSConfckFindOptions} [options] - options
  17262. * @returns {Promise<string|null>} absolute path to closest tsconfig.json or null if not found
  17263. */
  17264. async function find(filename, options) {
  17265. let dir = path$n.dirname(path$n.resolve(filename));
  17266. if (options?.ignoreNodeModules && isInNodeModules(dir)) {
  17267. return null;
  17268. }
  17269. const cache = options?.cache;
  17270. const configName = options?.configName ?? 'tsconfig.json';
  17271. if (cache?.hasConfigPath(dir, configName)) {
  17272. return cache.getConfigPath(dir, configName);
  17273. }
  17274. const { /** @type {Promise<string|null>} */ promise, resolve, reject } = makePromise();
  17275. if (options?.root && !path$n.isAbsolute(options.root)) {
  17276. options.root = path$n.resolve(options.root);
  17277. }
  17278. findUp(dir, { promise, resolve, reject }, options);
  17279. return promise;
  17280. }
  17281. /**
  17282. *
  17283. * @param {string} dir
  17284. * @param {{promise:Promise<string|null>,resolve:(result:string|null)=>void,reject:(err:any)=>void}} madePromise
  17285. * @param {import('./public.d.ts').TSConfckFindOptions} [options] - options
  17286. */
  17287. function findUp(dir, { resolve, reject, promise }, options) {
  17288. const { cache, root, configName } = options ?? {};
  17289. if (cache) {
  17290. if (cache.hasConfigPath(dir, configName)) {
  17291. let cached;
  17292. try {
  17293. cached = cache.getConfigPath(dir, configName);
  17294. } catch (e) {
  17295. reject(e);
  17296. return;
  17297. }
  17298. if (cached?.then) {
  17299. cached.then(resolve).catch(reject);
  17300. } else {
  17301. resolve(cached);
  17302. }
  17303. } else {
  17304. cache.setConfigPath(dir, promise, configName);
  17305. }
  17306. }
  17307. const tsconfig = path$n.join(dir, options?.configName ?? 'tsconfig.json');
  17308. fs__default.stat(tsconfig, (err, stats) => {
  17309. if (stats && (stats.isFile() || stats.isFIFO())) {
  17310. resolve(tsconfig);
  17311. } else if (err?.code !== 'ENOENT') {
  17312. reject(err);
  17313. } else {
  17314. let parent;
  17315. if (root === dir || (parent = path$n.dirname(dir)) === dir) {
  17316. resolve(null);
  17317. } else {
  17318. findUp(parent, { promise, resolve, reject }, options);
  17319. }
  17320. }
  17321. });
  17322. }
  17323. /*
  17324. this file contains code from strip-bom and strip-json-comments by Sindre Sorhus
  17325. https://github.com/sindresorhus/strip-json-comments/blob/v4.0.0/index.js
  17326. https://github.com/sindresorhus/strip-bom/blob/v5.0.0/index.js
  17327. licensed under MIT, see ../LICENSE
  17328. */
  17329. /**
  17330. * convert content of tsconfig.json to regular json
  17331. *
  17332. * @param {string} tsconfigJson - content of tsconfig.json
  17333. * @returns {string} content as regular json, comments and dangling commas have been replaced with whitespace
  17334. */
  17335. function toJson(tsconfigJson) {
  17336. const stripped = stripDanglingComma(stripJsonComments(stripBom(tsconfigJson)));
  17337. if (stripped.trim() === '') {
  17338. // only whitespace left after stripping, return empty object so that JSON.parse still works
  17339. return '{}';
  17340. } else {
  17341. return stripped;
  17342. }
  17343. }
  17344. /**
  17345. * replace dangling commas from pseudo-json string with single space
  17346. * implementation heavily inspired by strip-json-comments
  17347. *
  17348. * @param {string} pseudoJson
  17349. * @returns {string}
  17350. */
  17351. function stripDanglingComma(pseudoJson) {
  17352. let insideString = false;
  17353. let offset = 0;
  17354. let result = '';
  17355. let danglingCommaPos = null;
  17356. for (let i = 0; i < pseudoJson.length; i++) {
  17357. const currentCharacter = pseudoJson[i];
  17358. if (currentCharacter === '"') {
  17359. const escaped = isEscaped(pseudoJson, i);
  17360. if (!escaped) {
  17361. insideString = !insideString;
  17362. }
  17363. }
  17364. if (insideString) {
  17365. danglingCommaPos = null;
  17366. continue;
  17367. }
  17368. if (currentCharacter === ',') {
  17369. danglingCommaPos = i;
  17370. continue;
  17371. }
  17372. if (danglingCommaPos) {
  17373. if (currentCharacter === '}' || currentCharacter === ']') {
  17374. result += pseudoJson.slice(offset, danglingCommaPos) + ' ';
  17375. offset = danglingCommaPos + 1;
  17376. danglingCommaPos = null;
  17377. } else if (!currentCharacter.match(/\s/)) {
  17378. danglingCommaPos = null;
  17379. }
  17380. }
  17381. }
  17382. return result + pseudoJson.substring(offset);
  17383. }
  17384. // start strip-json-comments
  17385. /**
  17386. *
  17387. * @param {string} jsonString
  17388. * @param {number} quotePosition
  17389. * @returns {boolean}
  17390. */
  17391. function isEscaped(jsonString, quotePosition) {
  17392. let index = quotePosition - 1;
  17393. let backslashCount = 0;
  17394. while (jsonString[index] === '\\') {
  17395. index -= 1;
  17396. backslashCount += 1;
  17397. }
  17398. return Boolean(backslashCount % 2);
  17399. }
  17400. /**
  17401. *
  17402. * @param {string} string
  17403. * @param {number?} start
  17404. * @param {number?} end
  17405. */
  17406. function strip(string, start, end) {
  17407. return string.slice(start, end).replace(/\S/g, ' ');
  17408. }
  17409. const singleComment = Symbol('singleComment');
  17410. const multiComment = Symbol('multiComment');
  17411. /**
  17412. * @param {string} jsonString
  17413. * @returns {string}
  17414. */
  17415. function stripJsonComments(jsonString) {
  17416. let isInsideString = false;
  17417. /** @type {false | symbol} */
  17418. let isInsideComment = false;
  17419. let offset = 0;
  17420. let result = '';
  17421. for (let index = 0; index < jsonString.length; index++) {
  17422. const currentCharacter = jsonString[index];
  17423. const nextCharacter = jsonString[index + 1];
  17424. if (!isInsideComment && currentCharacter === '"') {
  17425. const escaped = isEscaped(jsonString, index);
  17426. if (!escaped) {
  17427. isInsideString = !isInsideString;
  17428. }
  17429. }
  17430. if (isInsideString) {
  17431. continue;
  17432. }
  17433. if (!isInsideComment && currentCharacter + nextCharacter === '//') {
  17434. result += jsonString.slice(offset, index);
  17435. offset = index;
  17436. isInsideComment = singleComment;
  17437. index++;
  17438. } else if (isInsideComment === singleComment && currentCharacter + nextCharacter === '\r\n') {
  17439. index++;
  17440. isInsideComment = false;
  17441. result += strip(jsonString, offset, index);
  17442. offset = index;
  17443. } else if (isInsideComment === singleComment && currentCharacter === '\n') {
  17444. isInsideComment = false;
  17445. result += strip(jsonString, offset, index);
  17446. offset = index;
  17447. } else if (!isInsideComment && currentCharacter + nextCharacter === '/*') {
  17448. result += jsonString.slice(offset, index);
  17449. offset = index;
  17450. isInsideComment = multiComment;
  17451. index++;
  17452. } else if (isInsideComment === multiComment && currentCharacter + nextCharacter === '*/') {
  17453. index++;
  17454. isInsideComment = false;
  17455. result += strip(jsonString, offset, index + 1);
  17456. offset = index + 1;
  17457. }
  17458. }
  17459. return result + (isInsideComment ? strip(jsonString.slice(offset)) : jsonString.slice(offset));
  17460. }
  17461. // end strip-json-comments
  17462. // start strip-bom
  17463. /**
  17464. * @param {string} string
  17465. * @returns {string}
  17466. */
  17467. function stripBom(string) {
  17468. // Catches EFBBBF (UTF-8 BOM) because the buffer-to-string
  17469. // conversion translates it to FEFF (UTF-16 BOM).
  17470. if (string.charCodeAt(0) === 0xfeff) {
  17471. return string.slice(1);
  17472. }
  17473. return string;
  17474. }
  17475. // end strip-bom
  17476. const not_found_result = {
  17477. tsconfigFile: null,
  17478. tsconfig: {}
  17479. };
  17480. /**
  17481. * parse the closest tsconfig.json file
  17482. *
  17483. * @param {string} filename - path to a tsconfig .json or a source file or directory (absolute or relative to cwd)
  17484. * @param {import('./public.d.ts').TSConfckParseOptions} [options] - options
  17485. * @returns {Promise<import('./public.d.ts').TSConfckParseResult>}
  17486. * @throws {TSConfckParseError}
  17487. */
  17488. async function parse$e(filename, options) {
  17489. /** @type {import('./cache.js').TSConfckCache} */
  17490. const cache = options?.cache;
  17491. if (cache?.hasParseResult(filename)) {
  17492. return getParsedDeep(filename, cache, options);
  17493. }
  17494. const {
  17495. resolve,
  17496. reject,
  17497. /** @type {Promise<import('./public.d.ts').TSConfckParseResult>}*/
  17498. promise
  17499. } = makePromise();
  17500. cache?.setParseResult(filename, promise, true);
  17501. try {
  17502. let tsconfigFile =
  17503. (await resolveTSConfigJson(filename, cache)) || (await find(filename, options));
  17504. if (!tsconfigFile) {
  17505. resolve(not_found_result);
  17506. return promise;
  17507. }
  17508. let result;
  17509. if (filename !== tsconfigFile && cache?.hasParseResult(tsconfigFile)) {
  17510. result = await getParsedDeep(tsconfigFile, cache, options);
  17511. } else {
  17512. result = await parseFile$1(tsconfigFile, cache, filename === tsconfigFile);
  17513. await Promise.all([parseExtends(result, cache), parseReferences(result, options)]);
  17514. }
  17515. result.tsconfig = replaceTokens(result.tsconfig, path$n.dirname(tsconfigFile));
  17516. resolve(resolveSolutionTSConfig(filename, result));
  17517. } catch (e) {
  17518. reject(e);
  17519. }
  17520. return promise;
  17521. }
  17522. /**
  17523. * ensure extends and references are parsed
  17524. *
  17525. * @param {string} filename - cached file
  17526. * @param {import('./cache.js').TSConfckCache} cache - cache
  17527. * @param {import('./public.d.ts').TSConfckParseOptions} options - options
  17528. */
  17529. async function getParsedDeep(filename, cache, options) {
  17530. const result = await cache.getParseResult(filename);
  17531. if (
  17532. (result.tsconfig.extends && !result.extended) ||
  17533. (result.tsconfig.references && !result.referenced)
  17534. ) {
  17535. const promise = Promise.all([
  17536. parseExtends(result, cache),
  17537. parseReferences(result, options)
  17538. ]).then(() => result);
  17539. cache.setParseResult(filename, promise, true);
  17540. return promise;
  17541. }
  17542. return result;
  17543. }
  17544. /**
  17545. *
  17546. * @param {string} tsconfigFile - path to tsconfig file
  17547. * @param {import('./cache.js').TSConfckCache} [cache] - cache
  17548. * @param {boolean} [skipCache] - skip cache
  17549. * @returns {Promise<import('./public.d.ts').TSConfckParseResult>}
  17550. */
  17551. async function parseFile$1(tsconfigFile, cache, skipCache) {
  17552. if (
  17553. !skipCache &&
  17554. cache?.hasParseResult(tsconfigFile) &&
  17555. !cache.getParseResult(tsconfigFile)._isRootFile_
  17556. ) {
  17557. return cache.getParseResult(tsconfigFile);
  17558. }
  17559. const promise = promises$1
  17560. .readFile(tsconfigFile, 'utf-8')
  17561. .then(toJson)
  17562. .then((json) => {
  17563. const parsed = JSON.parse(json);
  17564. applyDefaults(parsed, tsconfigFile);
  17565. return {
  17566. tsconfigFile,
  17567. tsconfig: normalizeTSConfig(parsed, path$n.dirname(tsconfigFile))
  17568. };
  17569. })
  17570. .catch((e) => {
  17571. throw new TSConfckParseError(
  17572. `parsing ${tsconfigFile} failed: ${e}`,
  17573. 'PARSE_FILE',
  17574. tsconfigFile,
  17575. e
  17576. );
  17577. });
  17578. if (
  17579. !skipCache &&
  17580. (!cache?.hasParseResult(tsconfigFile) || !cache.getParseResult(tsconfigFile)._isRootFile_)
  17581. ) {
  17582. cache?.setParseResult(tsconfigFile, promise);
  17583. }
  17584. return promise;
  17585. }
  17586. /**
  17587. * normalize to match the output of ts.parseJsonConfigFileContent
  17588. *
  17589. * @param {any} tsconfig - typescript tsconfig output
  17590. * @param {string} dir - directory
  17591. */
  17592. function normalizeTSConfig(tsconfig, dir) {
  17593. // set baseUrl to absolute path
  17594. const baseUrl = tsconfig.compilerOptions?.baseUrl;
  17595. if (baseUrl && !baseUrl.startsWith('${') && !path$n.isAbsolute(baseUrl)) {
  17596. tsconfig.compilerOptions.baseUrl = resolve2posix(dir, baseUrl);
  17597. }
  17598. return tsconfig;
  17599. }
  17600. /**
  17601. *
  17602. * @param {import('./public.d.ts').TSConfckParseResult} result
  17603. * @param {import('./public.d.ts').TSConfckParseOptions} [options]
  17604. * @returns {Promise<void>}
  17605. */
  17606. async function parseReferences(result, options) {
  17607. if (!result.tsconfig.references) {
  17608. return;
  17609. }
  17610. const referencedFiles = resolveReferencedTSConfigFiles(result, options);
  17611. const referenced = await Promise.all(
  17612. referencedFiles.map((file) => parseFile$1(file, options?.cache))
  17613. );
  17614. await Promise.all(referenced.map((ref) => parseExtends(ref, options?.cache)));
  17615. referenced.forEach((ref) => {
  17616. ref.solution = result;
  17617. });
  17618. result.referenced = referenced;
  17619. }
  17620. /**
  17621. * @param {import('./public.d.ts').TSConfckParseResult} result
  17622. * @param {import('./cache.js').TSConfckCache}[cache]
  17623. * @returns {Promise<void>}
  17624. */
  17625. async function parseExtends(result, cache) {
  17626. if (!result.tsconfig.extends) {
  17627. return;
  17628. }
  17629. // use result as first element in extended
  17630. // but dereference tsconfig so that mergeExtended can modify the original without affecting extended[0]
  17631. /** @type {import('./public.d.ts').TSConfckParseResult[]} */
  17632. const extended = [
  17633. { tsconfigFile: result.tsconfigFile, tsconfig: JSON.parse(JSON.stringify(result.tsconfig)) }
  17634. ];
  17635. // flatten extends graph into extended
  17636. let pos = 0;
  17637. /** @type {string[]} */
  17638. const extendsPath = [];
  17639. let currentBranchDepth = 0;
  17640. while (pos < extended.length) {
  17641. const extending = extended[pos];
  17642. extendsPath.push(extending.tsconfigFile);
  17643. if (extending.tsconfig.extends) {
  17644. // keep following this branch
  17645. currentBranchDepth += 1;
  17646. /** @type {string[]} */
  17647. let resolvedExtends;
  17648. if (!Array.isArray(extending.tsconfig.extends)) {
  17649. resolvedExtends = [resolveExtends(extending.tsconfig.extends, extending.tsconfigFile)];
  17650. } else {
  17651. // reverse because typescript 5.0 treats ['a','b','c'] as c extends b extends a
  17652. resolvedExtends = extending.tsconfig.extends
  17653. .reverse()
  17654. .map((ex) => resolveExtends(ex, extending.tsconfigFile));
  17655. }
  17656. const circularExtends = resolvedExtends.find((tsconfigFile) =>
  17657. extendsPath.includes(tsconfigFile)
  17658. );
  17659. if (circularExtends) {
  17660. const circle = extendsPath.concat([circularExtends]).join(' -> ');
  17661. throw new TSConfckParseError(
  17662. `Circular dependency in "extends": ${circle}`,
  17663. 'EXTENDS_CIRCULAR',
  17664. result.tsconfigFile
  17665. );
  17666. }
  17667. // add new extends to the list directly after current
  17668. extended.splice(
  17669. pos + 1,
  17670. 0,
  17671. ...(await Promise.all(resolvedExtends.map((file) => parseFile$1(file, cache))))
  17672. );
  17673. } else {
  17674. // reached a leaf, backtrack to the last branching point and continue
  17675. extendsPath.splice(-currentBranchDepth);
  17676. currentBranchDepth = 0;
  17677. }
  17678. pos = pos + 1;
  17679. }
  17680. result.extended = extended;
  17681. // skip first as it is the original config
  17682. for (const ext of result.extended.slice(1)) {
  17683. extendTSConfig(result, ext);
  17684. }
  17685. }
  17686. /**
  17687. *
  17688. * @param {string} extended
  17689. * @param {string} from
  17690. * @returns {string}
  17691. */
  17692. function resolveExtends(extended, from) {
  17693. if (extended === '..') {
  17694. // see #149
  17695. extended = '../tsconfig.json';
  17696. }
  17697. const req = createRequire$2(from);
  17698. let error;
  17699. try {
  17700. return req.resolve(extended);
  17701. } catch (e) {
  17702. error = e;
  17703. }
  17704. if (extended[0] !== '.' && !path$n.isAbsolute(extended)) {
  17705. try {
  17706. return req.resolve(`${extended}/tsconfig.json`);
  17707. } catch (e) {
  17708. error = e;
  17709. }
  17710. }
  17711. throw new TSConfckParseError(
  17712. `failed to resolve "extends":"${extended}" in ${from}`,
  17713. 'EXTENDS_RESOLVE',
  17714. from,
  17715. error
  17716. );
  17717. }
  17718. // references, extends and custom keys are not carried over
  17719. const EXTENDABLE_KEYS = [
  17720. 'compilerOptions',
  17721. 'files',
  17722. 'include',
  17723. 'exclude',
  17724. 'watchOptions',
  17725. 'compileOnSave',
  17726. 'typeAcquisition',
  17727. 'buildOptions'
  17728. ];
  17729. /**
  17730. *
  17731. * @param {import('./public.d.ts').TSConfckParseResult} extending
  17732. * @param {import('./public.d.ts').TSConfckParseResult} extended
  17733. * @returns void
  17734. */
  17735. function extendTSConfig(extending, extended) {
  17736. const extendingConfig = extending.tsconfig;
  17737. const extendedConfig = extended.tsconfig;
  17738. const relativePath = native2posix(
  17739. path$n.relative(path$n.dirname(extending.tsconfigFile), path$n.dirname(extended.tsconfigFile))
  17740. );
  17741. for (const key of Object.keys(extendedConfig).filter((key) => EXTENDABLE_KEYS.includes(key))) {
  17742. if (key === 'compilerOptions') {
  17743. if (!extendingConfig.compilerOptions) {
  17744. extendingConfig.compilerOptions = {};
  17745. }
  17746. for (const option of Object.keys(extendedConfig.compilerOptions)) {
  17747. if (Object.prototype.hasOwnProperty.call(extendingConfig.compilerOptions, option)) {
  17748. continue; // already set
  17749. }
  17750. extendingConfig.compilerOptions[option] = rebaseRelative(
  17751. option,
  17752. extendedConfig.compilerOptions[option],
  17753. relativePath
  17754. );
  17755. }
  17756. } else if (extendingConfig[key] === undefined) {
  17757. if (key === 'watchOptions') {
  17758. extendingConfig.watchOptions = {};
  17759. for (const option of Object.keys(extendedConfig.watchOptions)) {
  17760. extendingConfig.watchOptions[option] = rebaseRelative(
  17761. option,
  17762. extendedConfig.watchOptions[option],
  17763. relativePath
  17764. );
  17765. }
  17766. } else {
  17767. extendingConfig[key] = rebaseRelative(key, extendedConfig[key], relativePath);
  17768. }
  17769. }
  17770. }
  17771. }
  17772. const REBASE_KEYS = [
  17773. // root
  17774. 'files',
  17775. 'include',
  17776. 'exclude',
  17777. // compilerOptions
  17778. 'baseUrl',
  17779. 'rootDir',
  17780. 'rootDirs',
  17781. 'typeRoots',
  17782. 'outDir',
  17783. 'outFile',
  17784. 'declarationDir',
  17785. // watchOptions
  17786. 'excludeDirectories',
  17787. 'excludeFiles'
  17788. ];
  17789. /** @typedef {string | string[]} PathValue */
  17790. /**
  17791. *
  17792. * @param {string} key
  17793. * @param {PathValue} value
  17794. * @param {string} prependPath
  17795. * @returns {PathValue}
  17796. */
  17797. function rebaseRelative(key, value, prependPath) {
  17798. if (!REBASE_KEYS.includes(key)) {
  17799. return value;
  17800. }
  17801. if (Array.isArray(value)) {
  17802. return value.map((x) => rebasePath(x, prependPath));
  17803. } else {
  17804. return rebasePath(value, prependPath);
  17805. }
  17806. }
  17807. /**
  17808. *
  17809. * @param {string} value
  17810. * @param {string} prependPath
  17811. * @returns {string}
  17812. */
  17813. function rebasePath(value, prependPath) {
  17814. if (path$n.isAbsolute(value) || value.startsWith('${configDir}')) {
  17815. return value;
  17816. } else {
  17817. // relative paths use posix syntax in tsconfig
  17818. return path$n.posix.normalize(path$n.posix.join(prependPath, value));
  17819. }
  17820. }
  17821. class TSConfckParseError extends Error {
  17822. /**
  17823. * error code
  17824. * @type {string}
  17825. */
  17826. code;
  17827. /**
  17828. * error cause
  17829. * @type { Error | undefined}
  17830. */
  17831. cause;
  17832. /**
  17833. * absolute path of tsconfig file where the error happened
  17834. * @type {string}
  17835. */
  17836. tsconfigFile;
  17837. /**
  17838. *
  17839. * @param {string} message - error message
  17840. * @param {string} code - error code
  17841. * @param {string} tsconfigFile - path to tsconfig file
  17842. * @param {Error?} cause - cause of this error
  17843. */
  17844. constructor(message, code, tsconfigFile, cause) {
  17845. super(message);
  17846. // Set the prototype explicitly.
  17847. Object.setPrototypeOf(this, TSConfckParseError.prototype);
  17848. this.name = TSConfckParseError.name;
  17849. this.code = code;
  17850. this.cause = cause;
  17851. this.tsconfigFile = tsconfigFile;
  17852. }
  17853. }
  17854. /**
  17855. *
  17856. * @param {any} tsconfig
  17857. * @param {string} tsconfigFile
  17858. */
  17859. function applyDefaults(tsconfig, tsconfigFile) {
  17860. if (isJSConfig(tsconfigFile)) {
  17861. tsconfig.compilerOptions = {
  17862. ...DEFAULT_JSCONFIG_COMPILER_OPTIONS,
  17863. ...tsconfig.compilerOptions
  17864. };
  17865. }
  17866. }
  17867. const DEFAULT_JSCONFIG_COMPILER_OPTIONS = {
  17868. allowJs: true,
  17869. maxNodeModuleJsDepth: 2,
  17870. allowSyntheticDefaultImports: true,
  17871. skipLibCheck: true,
  17872. noEmit: true
  17873. };
  17874. /**
  17875. * @param {string} configFileName
  17876. */
  17877. function isJSConfig(configFileName) {
  17878. return path$n.basename(configFileName) === 'jsconfig.json';
  17879. }
  17880. /** @template T */
  17881. class TSConfckCache {
  17882. /**
  17883. * clear cache, use this if you have a long running process and tsconfig files have been added,changed or deleted
  17884. */
  17885. clear() {
  17886. this.#configPaths.clear();
  17887. this.#parsed.clear();
  17888. }
  17889. /**
  17890. * has cached closest config for files in dir
  17891. * @param {string} dir
  17892. * @param {string} [configName=tsconfig.json]
  17893. * @returns {boolean}
  17894. */
  17895. hasConfigPath(dir, configName = 'tsconfig.json') {
  17896. return this.#configPaths.has(`${dir}/${configName}`);
  17897. }
  17898. /**
  17899. * get cached closest tsconfig for files in dir
  17900. * @param {string} dir
  17901. * @param {string} [configName=tsconfig.json]
  17902. * @returns {Promise<string|null>|string|null}
  17903. * @throws {unknown} if cached value is an error
  17904. */
  17905. getConfigPath(dir, configName = 'tsconfig.json') {
  17906. const key = `${dir}/${configName}`;
  17907. const value = this.#configPaths.get(key);
  17908. if (value == null || value.length || value.then) {
  17909. return value;
  17910. } else {
  17911. throw value;
  17912. }
  17913. }
  17914. /**
  17915. * has parsed tsconfig for file
  17916. * @param {string} file
  17917. * @returns {boolean}
  17918. */
  17919. hasParseResult(file) {
  17920. return this.#parsed.has(file);
  17921. }
  17922. /**
  17923. * get parsed tsconfig for file
  17924. * @param {string} file
  17925. * @returns {Promise<T>|T}
  17926. * @throws {unknown} if cached value is an error
  17927. */
  17928. getParseResult(file) {
  17929. const value = this.#parsed.get(file);
  17930. if (value.then || value.tsconfig) {
  17931. return value;
  17932. } else {
  17933. throw value; // cached error, rethrow
  17934. }
  17935. }
  17936. /**
  17937. * @internal
  17938. * @private
  17939. * @param file
  17940. * @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()
  17941. * @param {Promise<T>} result
  17942. */
  17943. setParseResult(file, result, isRootFile = false) {
  17944. // _isRootFile_ is a temporary property for Promise result, used to prevent deadlock with cache
  17945. Object.defineProperty(result, '_isRootFile_', {
  17946. value: isRootFile,
  17947. writable: false,
  17948. enumerable: false,
  17949. configurable: false
  17950. });
  17951. this.#parsed.set(file, result);
  17952. result
  17953. .then((parsed) => {
  17954. if (this.#parsed.get(file) === result) {
  17955. this.#parsed.set(file, parsed);
  17956. }
  17957. })
  17958. .catch((e) => {
  17959. if (this.#parsed.get(file) === result) {
  17960. this.#parsed.set(file, e);
  17961. }
  17962. });
  17963. }
  17964. /**
  17965. * @internal
  17966. * @private
  17967. * @param {string} dir
  17968. * @param {Promise<string|null>} configPath
  17969. * @param {string} [configName=tsconfig.json]
  17970. */
  17971. setConfigPath(dir, configPath, configName = 'tsconfig.json') {
  17972. const key = `${dir}/${configName}`;
  17973. this.#configPaths.set(key, configPath);
  17974. configPath
  17975. .then((path) => {
  17976. if (this.#configPaths.get(key) === configPath) {
  17977. this.#configPaths.set(key, path);
  17978. }
  17979. })
  17980. .catch((e) => {
  17981. if (this.#configPaths.get(key) === configPath) {
  17982. this.#configPaths.set(key, e);
  17983. }
  17984. });
  17985. }
  17986. /**
  17987. * map directories to their closest tsconfig.json
  17988. * @internal
  17989. * @private
  17990. * @type{Map<string,(Promise<string|null>|string|null)>}
  17991. */
  17992. #configPaths = new Map();
  17993. /**
  17994. * map files to their parsed tsconfig result
  17995. * @internal
  17996. * @private
  17997. * @type {Map<string,(Promise<T>|T)> }
  17998. */
  17999. #parsed = new Map();
  18000. }
  18001. const debug$h = createDebugger("vite:esbuild");
  18002. const IIFE_BEGIN_RE = /(?:const|var)\s+\S+\s*=\s*function\([^()]*\)\s*\{\s*"use strict";/;
  18003. const validExtensionRE = /\.\w+$/;
  18004. const jsxExtensionsRE = /\.(?:j|t)sx\b/;
  18005. const defaultEsbuildSupported = {
  18006. "dynamic-import": true,
  18007. "import-meta": true
  18008. };
  18009. let server;
  18010. async function transformWithEsbuild(code, filename, options, inMap) {
  18011. let loader = options?.loader;
  18012. if (!loader) {
  18013. const ext = path$n.extname(validExtensionRE.test(filename) ? filename : cleanUrl(filename)).slice(1);
  18014. if (ext === "cjs" || ext === "mjs") {
  18015. loader = "js";
  18016. } else if (ext === "cts" || ext === "mts") {
  18017. loader = "ts";
  18018. } else {
  18019. loader = ext;
  18020. }
  18021. }
  18022. let tsconfigRaw = options?.tsconfigRaw;
  18023. if (typeof tsconfigRaw !== "string") {
  18024. const meaningfulFields = [
  18025. "alwaysStrict",
  18026. "experimentalDecorators",
  18027. "importsNotUsedAsValues",
  18028. "jsx",
  18029. "jsxFactory",
  18030. "jsxFragmentFactory",
  18031. "jsxImportSource",
  18032. "preserveValueImports",
  18033. "target",
  18034. "useDefineForClassFields",
  18035. "verbatimModuleSyntax"
  18036. ];
  18037. const compilerOptionsForFile = {};
  18038. if (loader === "ts" || loader === "tsx") {
  18039. const loadedTsconfig = await loadTsconfigJsonForFile(filename);
  18040. const loadedCompilerOptions = loadedTsconfig.compilerOptions ?? {};
  18041. for (const field of meaningfulFields) {
  18042. if (field in loadedCompilerOptions) {
  18043. compilerOptionsForFile[field] = loadedCompilerOptions[field];
  18044. }
  18045. }
  18046. }
  18047. const compilerOptions = {
  18048. ...compilerOptionsForFile,
  18049. ...tsconfigRaw?.compilerOptions
  18050. };
  18051. if (compilerOptions.useDefineForClassFields === void 0 && compilerOptions.target === void 0) {
  18052. compilerOptions.useDefineForClassFields = false;
  18053. }
  18054. if (options) {
  18055. options.jsx && (compilerOptions.jsx = void 0);
  18056. options.jsxFactory && (compilerOptions.jsxFactory = void 0);
  18057. options.jsxFragment && (compilerOptions.jsxFragmentFactory = void 0);
  18058. options.jsxImportSource && (compilerOptions.jsxImportSource = void 0);
  18059. }
  18060. tsconfigRaw = {
  18061. ...tsconfigRaw,
  18062. compilerOptions
  18063. };
  18064. }
  18065. const resolvedOptions = {
  18066. sourcemap: true,
  18067. // ensure source file name contains full query
  18068. sourcefile: filename,
  18069. ...options,
  18070. loader,
  18071. tsconfigRaw
  18072. };
  18073. delete resolvedOptions.include;
  18074. delete resolvedOptions.exclude;
  18075. delete resolvedOptions.jsxInject;
  18076. try {
  18077. const result = await transform$1(code, resolvedOptions);
  18078. let map;
  18079. if (inMap && resolvedOptions.sourcemap) {
  18080. const nextMap = JSON.parse(result.map);
  18081. nextMap.sourcesContent = [];
  18082. map = combineSourcemaps(filename, [
  18083. nextMap,
  18084. inMap
  18085. ]);
  18086. } else {
  18087. map = resolvedOptions.sourcemap && resolvedOptions.sourcemap !== "inline" ? JSON.parse(result.map) : { mappings: "" };
  18088. }
  18089. return {
  18090. ...result,
  18091. map
  18092. };
  18093. } catch (e) {
  18094. debug$h?.(`esbuild error with options used: `, resolvedOptions);
  18095. if (e.errors) {
  18096. e.frame = "";
  18097. e.errors.forEach((m) => {
  18098. if (m.text === "Experimental decorators are not currently enabled" || m.text === "Parameter decorators only work when experimental decorators are enabled") {
  18099. m.text += '. Vite 5 now uses esbuild 0.18 and you need to enable them by adding "experimentalDecorators": true in your "tsconfig.json" file.';
  18100. }
  18101. e.frame += `
  18102. ` + prettifyMessage(m, code);
  18103. });
  18104. e.loc = e.errors[0].location;
  18105. }
  18106. throw e;
  18107. }
  18108. }
  18109. function esbuildPlugin(config) {
  18110. const options = config.esbuild;
  18111. const { jsxInject, include, exclude, ...esbuildTransformOptions } = options;
  18112. const filter = createFilter(include || /\.(m?ts|[jt]sx)$/, exclude || /\.js$/);
  18113. const transformOptions = {
  18114. target: "esnext",
  18115. charset: "utf8",
  18116. ...esbuildTransformOptions,
  18117. minify: false,
  18118. minifyIdentifiers: false,
  18119. minifySyntax: false,
  18120. minifyWhitespace: false,
  18121. treeShaking: false,
  18122. // keepNames is not needed when minify is disabled.
  18123. // Also transforming multiple times with keepNames enabled breaks
  18124. // tree-shaking. (#9164)
  18125. keepNames: false,
  18126. supported: {
  18127. ...defaultEsbuildSupported,
  18128. ...esbuildTransformOptions.supported
  18129. }
  18130. };
  18131. return {
  18132. name: "vite:esbuild",
  18133. configureServer(_server) {
  18134. server = _server;
  18135. server.watcher.on("add", reloadOnTsconfigChange).on("change", reloadOnTsconfigChange).on("unlink", reloadOnTsconfigChange);
  18136. },
  18137. buildEnd() {
  18138. server = null;
  18139. },
  18140. async transform(code, id) {
  18141. if (filter(id) || filter(cleanUrl(id))) {
  18142. const result = await transformWithEsbuild(code, id, transformOptions);
  18143. if (result.warnings.length) {
  18144. result.warnings.forEach((m) => {
  18145. this.warn(prettifyMessage(m, code));
  18146. });
  18147. }
  18148. if (jsxInject && jsxExtensionsRE.test(id)) {
  18149. result.code = jsxInject + ";" + result.code;
  18150. }
  18151. return {
  18152. code: result.code,
  18153. map: result.map
  18154. };
  18155. }
  18156. }
  18157. };
  18158. }
  18159. const rollupToEsbuildFormatMap = {
  18160. es: "esm",
  18161. cjs: "cjs",
  18162. // passing `var Lib = (() => {})()` to esbuild with format = "iife"
  18163. // will turn it to `(() => { var Lib = (() => {})() })()`,
  18164. // so we remove the format config to tell esbuild not doing this
  18165. //
  18166. // although esbuild doesn't change format, there is still possibility
  18167. // that `{ treeShaking: true }` removes a top-level no-side-effect variable
  18168. // like: `var Lib = 1`, which becomes `` after esbuild transforming,
  18169. // but thankfully rollup does not do this optimization now
  18170. iife: void 0
  18171. };
  18172. const buildEsbuildPlugin = (config) => {
  18173. return {
  18174. name: "vite:esbuild-transpile",
  18175. async renderChunk(code, chunk, opts) {
  18176. if (opts.__vite_skip_esbuild__) {
  18177. return null;
  18178. }
  18179. const options = resolveEsbuildTranspileOptions(config, opts.format);
  18180. if (!options) {
  18181. return null;
  18182. }
  18183. const res = await transformWithEsbuild(code, chunk.fileName, options);
  18184. if (config.build.lib) {
  18185. const esbuildCode = res.code;
  18186. const contentIndex = opts.format === "iife" ? Math.max(esbuildCode.search(IIFE_BEGIN_RE), 0) : opts.format === "umd" ? esbuildCode.indexOf(`(function(`) : 0;
  18187. if (contentIndex > 0) {
  18188. const esbuildHelpers = esbuildCode.slice(0, contentIndex);
  18189. res.code = esbuildCode.slice(contentIndex).replace(`"use strict";`, `"use strict";` + esbuildHelpers);
  18190. }
  18191. }
  18192. return res;
  18193. }
  18194. };
  18195. };
  18196. function resolveEsbuildTranspileOptions(config, format) {
  18197. const target = config.build.target;
  18198. const minify = config.build.minify === "esbuild";
  18199. if ((!target || target === "esnext") && !minify) {
  18200. return null;
  18201. }
  18202. const isEsLibBuild = config.build.lib && format === "es";
  18203. const esbuildOptions = config.esbuild || {};
  18204. const options = {
  18205. charset: "utf8",
  18206. ...esbuildOptions,
  18207. loader: "js",
  18208. target: target || void 0,
  18209. format: rollupToEsbuildFormatMap[format],
  18210. supported: {
  18211. ...defaultEsbuildSupported,
  18212. ...esbuildOptions.supported
  18213. }
  18214. };
  18215. if (!minify) {
  18216. return {
  18217. ...options,
  18218. minify: false,
  18219. minifyIdentifiers: false,
  18220. minifySyntax: false,
  18221. minifyWhitespace: false,
  18222. treeShaking: false
  18223. };
  18224. }
  18225. if (options.minifyIdentifiers != null || options.minifySyntax != null || options.minifyWhitespace != null) {
  18226. if (isEsLibBuild) {
  18227. return {
  18228. ...options,
  18229. minify: false,
  18230. minifyIdentifiers: options.minifyIdentifiers ?? true,
  18231. minifySyntax: options.minifySyntax ?? true,
  18232. minifyWhitespace: false,
  18233. treeShaking: true
  18234. };
  18235. } else {
  18236. return {
  18237. ...options,
  18238. minify: false,
  18239. minifyIdentifiers: options.minifyIdentifiers ?? true,
  18240. minifySyntax: options.minifySyntax ?? true,
  18241. minifyWhitespace: options.minifyWhitespace ?? true,
  18242. treeShaking: true
  18243. };
  18244. }
  18245. }
  18246. if (isEsLibBuild) {
  18247. return {
  18248. ...options,
  18249. minify: false,
  18250. minifyIdentifiers: true,
  18251. minifySyntax: true,
  18252. minifyWhitespace: false,
  18253. treeShaking: true
  18254. };
  18255. } else {
  18256. return {
  18257. ...options,
  18258. minify: true,
  18259. treeShaking: true
  18260. };
  18261. }
  18262. }
  18263. function prettifyMessage(m, code) {
  18264. let res = colors$1.yellow(m.text);
  18265. if (m.location) {
  18266. res += `
  18267. ` + generateCodeFrame(code, m.location);
  18268. }
  18269. return res + `
  18270. `;
  18271. }
  18272. let tsconfckCache;
  18273. async function loadTsconfigJsonForFile(filename) {
  18274. try {
  18275. if (!tsconfckCache) {
  18276. tsconfckCache = new TSConfckCache();
  18277. }
  18278. const result = await parse$e(filename, {
  18279. cache: tsconfckCache,
  18280. ignoreNodeModules: true
  18281. });
  18282. if (server && result.tsconfigFile) {
  18283. ensureWatchedFile(server.watcher, result.tsconfigFile, server.config.root);
  18284. }
  18285. return result.tsconfig;
  18286. } catch (e) {
  18287. if (e instanceof TSConfckParseError) {
  18288. if (server && e.tsconfigFile) {
  18289. ensureWatchedFile(server.watcher, e.tsconfigFile, server.config.root);
  18290. }
  18291. }
  18292. throw e;
  18293. }
  18294. }
  18295. async function reloadOnTsconfigChange(changedFile) {
  18296. if (!server) return;
  18297. if (path$n.basename(changedFile) === "tsconfig.json" || changedFile.endsWith(".json") && tsconfckCache?.hasParseResult(changedFile)) {
  18298. server.config.logger.info(
  18299. `changed tsconfig file detected: ${changedFile} - Clearing cache and forcing full-reload to ensure TypeScript is compiled with updated config values.`,
  18300. { clear: server.config.clearScreen, timestamp: true }
  18301. );
  18302. server.moduleGraph.invalidateAll();
  18303. tsconfckCache?.clear();
  18304. if (server) {
  18305. server.hot.send({
  18306. type: "full-reload",
  18307. path: "*"
  18308. });
  18309. }
  18310. }
  18311. }
  18312. // src/realWorker.ts
  18313. var Worker = class {
  18314. /** @internal */
  18315. _code;
  18316. /** @internal */
  18317. _parentFunctions;
  18318. /** @internal */
  18319. _max;
  18320. /** @internal */
  18321. _pool;
  18322. /** @internal */
  18323. _idlePool;
  18324. /** @internal */
  18325. _queue;
  18326. constructor(fn, options = {}) {
  18327. this._code = genWorkerCode(fn, options.parentFunctions ?? {});
  18328. this._parentFunctions = options.parentFunctions ?? {};
  18329. const defaultMax = Math.max(
  18330. 1,
  18331. // os.availableParallelism is available from Node.js 18.14.0
  18332. (os$5.availableParallelism?.() ?? os$5.cpus().length) - 1
  18333. );
  18334. this._max = options.max || defaultMax;
  18335. this._pool = [];
  18336. this._idlePool = [];
  18337. this._queue = [];
  18338. }
  18339. async run(...args) {
  18340. const worker = await this._getAvailableWorker();
  18341. return new Promise((resolve, reject) => {
  18342. worker.currentResolve = resolve;
  18343. worker.currentReject = reject;
  18344. worker.postMessage({ type: "run", args });
  18345. });
  18346. }
  18347. stop() {
  18348. this._pool.forEach((w) => w.unref());
  18349. this._queue.forEach(
  18350. ([, reject]) => reject(
  18351. new Error("Main worker pool stopped before a worker was available.")
  18352. )
  18353. );
  18354. this._pool = [];
  18355. this._idlePool = [];
  18356. this._queue = [];
  18357. }
  18358. /** @internal */
  18359. async _getAvailableWorker() {
  18360. if (this._idlePool.length) {
  18361. return this._idlePool.shift();
  18362. }
  18363. if (this._pool.length < this._max) {
  18364. const worker = new Worker$1(this._code, { eval: true });
  18365. worker.on("message", async (args) => {
  18366. if (args.type === "run") {
  18367. if ("result" in args) {
  18368. worker.currentResolve && worker.currentResolve(args.result);
  18369. worker.currentResolve = null;
  18370. } else {
  18371. if (args.error instanceof ReferenceError) {
  18372. args.error.message += ". Maybe you forgot to pass the function to parentFunction?";
  18373. }
  18374. worker.currentReject && worker.currentReject(args.error);
  18375. worker.currentReject = null;
  18376. }
  18377. this._assignDoneWorker(worker);
  18378. } else if (args.type === "parentFunction") {
  18379. try {
  18380. const result = await this._parentFunctions[args.name](...args.args);
  18381. worker.postMessage({ type: "parentFunction", id: args.id, result });
  18382. } catch (e) {
  18383. worker.postMessage({
  18384. type: "parentFunction",
  18385. id: args.id,
  18386. error: e
  18387. });
  18388. }
  18389. }
  18390. });
  18391. worker.on("error", (err) => {
  18392. worker.currentReject && worker.currentReject(err);
  18393. worker.currentReject = null;
  18394. });
  18395. worker.on("exit", (code) => {
  18396. const i = this._pool.indexOf(worker);
  18397. if (i > -1)
  18398. this._pool.splice(i, 1);
  18399. if (code !== 0 && worker.currentReject) {
  18400. worker.currentReject(
  18401. new Error(`Worker stopped with non-0 exit code ${code}`)
  18402. );
  18403. worker.currentReject = null;
  18404. }
  18405. });
  18406. this._pool.push(worker);
  18407. return worker;
  18408. }
  18409. let resolve;
  18410. let reject;
  18411. const onWorkerAvailablePromise = new Promise((r, rj) => {
  18412. resolve = r;
  18413. reject = rj;
  18414. });
  18415. this._queue.push([resolve, reject]);
  18416. return onWorkerAvailablePromise;
  18417. }
  18418. /** @internal */
  18419. _assignDoneWorker(worker) {
  18420. if (this._queue.length) {
  18421. const [resolve] = this._queue.shift();
  18422. resolve(worker);
  18423. return;
  18424. }
  18425. this._idlePool.push(worker);
  18426. }
  18427. };
  18428. function genWorkerCode(fn, parentFunctions) {
  18429. const createParentFunctionCaller = (parentPort) => {
  18430. let id = 0;
  18431. const resolvers = /* @__PURE__ */ new Map();
  18432. const call = (key) => async (...args) => {
  18433. id++;
  18434. let resolve, reject;
  18435. const promise = new Promise((res, rej) => {
  18436. resolve = res;
  18437. reject = rej;
  18438. });
  18439. resolvers.set(id, { resolve, reject });
  18440. parentPort.postMessage({ type: "parentFunction", id, name: key, args });
  18441. return await promise;
  18442. };
  18443. const receive = (id2, args) => {
  18444. if (resolvers.has(id2)) {
  18445. const { resolve, reject } = resolvers.get(id2);
  18446. resolvers.delete(id2);
  18447. if ("result" in args) {
  18448. resolve(args.result);
  18449. } else {
  18450. reject(args.error);
  18451. }
  18452. }
  18453. };
  18454. return { call, receive };
  18455. };
  18456. return `
  18457. const { parentPort } = require('worker_threads')
  18458. const parentFunctionCaller = (${createParentFunctionCaller.toString()})(parentPort)
  18459. const doWork = (() => {
  18460. ${Object.keys(parentFunctions).map(
  18461. (key) => `const ${key} = parentFunctionCaller.call(${JSON.stringify(key)});`
  18462. ).join("\n")}
  18463. return (${fn.toString()})()
  18464. })()
  18465. parentPort.on('message', async (args) => {
  18466. if (args.type === 'run') {
  18467. try {
  18468. const res = await doWork(...args.args)
  18469. parentPort.postMessage({ type: 'run', result: res })
  18470. } catch (e) {
  18471. parentPort.postMessage({ type: 'run', error: e })
  18472. }
  18473. } else if (args.type === 'parentFunction') {
  18474. parentFunctionCaller.receive(args.id, args)
  18475. }
  18476. })
  18477. `;
  18478. }
  18479. var FakeWorker = class {
  18480. /** @internal */
  18481. _fn;
  18482. constructor(fn, options = {}) {
  18483. const argsAndCode = genFakeWorkerArgsAndCode(
  18484. fn,
  18485. options.parentFunctions ?? {}
  18486. );
  18487. const require2 = createRequire$1(import.meta.url);
  18488. this._fn = new Function(...argsAndCode)(require2, options.parentFunctions);
  18489. }
  18490. async run(...args) {
  18491. try {
  18492. return await this._fn(...args);
  18493. } catch (err) {
  18494. if (err instanceof ReferenceError) {
  18495. err.message += ". Maybe you forgot to pass the function to parentFunction?";
  18496. }
  18497. throw err;
  18498. }
  18499. }
  18500. stop() {
  18501. }
  18502. };
  18503. function genFakeWorkerArgsAndCode(fn, parentFunctions) {
  18504. return [
  18505. "require",
  18506. "parentFunctions",
  18507. `
  18508. ${Object.keys(parentFunctions).map((key) => `const ${key} = parentFunctions[${JSON.stringify(key)}];`).join("\n")}
  18509. return (${fn.toString()})()
  18510. `
  18511. ];
  18512. }
  18513. // src/workerWithFallback.ts
  18514. var WorkerWithFallback = class {
  18515. /** @internal */
  18516. _disableReal;
  18517. /** @internal */
  18518. _realWorker;
  18519. /** @internal */
  18520. _fakeWorker;
  18521. /** @internal */
  18522. _shouldUseFake;
  18523. constructor(fn, options) {
  18524. this._disableReal = options.max !== void 0 && options.max <= 0;
  18525. this._realWorker = new Worker(fn, options);
  18526. this._fakeWorker = new FakeWorker(fn, options);
  18527. this._shouldUseFake = options.shouldUseFake;
  18528. }
  18529. async run(...args) {
  18530. const useFake = this._disableReal || this._shouldUseFake(...args);
  18531. return this[useFake ? "_fakeWorker" : "_realWorker"].run(...args);
  18532. }
  18533. stop() {
  18534. this._realWorker.stop();
  18535. this._fakeWorker.stop();
  18536. }
  18537. };
  18538. let terserPath;
  18539. const loadTerserPath = (root) => {
  18540. if (terserPath) return terserPath;
  18541. try {
  18542. terserPath = requireResolveFromRootWithFallback(root, "terser");
  18543. } catch (e) {
  18544. if (e.code === "MODULE_NOT_FOUND") {
  18545. throw new Error(
  18546. "terser not found. Since Vite v3, terser has become an optional dependency. You need to install it."
  18547. );
  18548. } else {
  18549. const message = new Error(`terser failed to load:
  18550. ${e.message}`);
  18551. message.stack = e.stack + "\n" + message.stack;
  18552. throw message;
  18553. }
  18554. }
  18555. return terserPath;
  18556. };
  18557. function terserPlugin(config) {
  18558. const { maxWorkers, ...terserOptions } = config.build.terserOptions;
  18559. const makeWorker = () => new Worker(
  18560. () => async (terserPath2, code, options) => {
  18561. const terser = require(terserPath2);
  18562. return terser.minify(code, options);
  18563. },
  18564. {
  18565. max: maxWorkers
  18566. }
  18567. );
  18568. let worker;
  18569. return {
  18570. name: "vite:terser",
  18571. async renderChunk(code, _chunk, outputOptions) {
  18572. if (config.build.minify !== "terser" && // @ts-expect-error injected by @vitejs/plugin-legacy
  18573. !outputOptions.__vite_force_terser__) {
  18574. return null;
  18575. }
  18576. if (config.build.lib && outputOptions.format === "es") {
  18577. return null;
  18578. }
  18579. worker ||= makeWorker();
  18580. const terserPath2 = loadTerserPath(config.root);
  18581. const res = await worker.run(terserPath2, code, {
  18582. safari10: true,
  18583. ...terserOptions,
  18584. sourceMap: !!outputOptions.sourcemap,
  18585. module: outputOptions.format.startsWith("es"),
  18586. toplevel: outputOptions.format === "cjs"
  18587. });
  18588. return {
  18589. code: res.code,
  18590. map: res.map
  18591. };
  18592. },
  18593. closeBundle() {
  18594. worker?.stop();
  18595. }
  18596. };
  18597. }
  18598. const mimes = {
  18599. "3g2": "video/3gpp2",
  18600. "3gp": "video/3gpp",
  18601. "3gpp": "video/3gpp",
  18602. "3mf": "model/3mf",
  18603. "aac": "audio/aac",
  18604. "ac": "application/pkix-attr-cert",
  18605. "adp": "audio/adpcm",
  18606. "adts": "audio/aac",
  18607. "ai": "application/postscript",
  18608. "aml": "application/automationml-aml+xml",
  18609. "amlx": "application/automationml-amlx+zip",
  18610. "amr": "audio/amr",
  18611. "apng": "image/apng",
  18612. "appcache": "text/cache-manifest",
  18613. "appinstaller": "application/appinstaller",
  18614. "appx": "application/appx",
  18615. "appxbundle": "application/appxbundle",
  18616. "asc": "application/pgp-keys",
  18617. "atom": "application/atom+xml",
  18618. "atomcat": "application/atomcat+xml",
  18619. "atomdeleted": "application/atomdeleted+xml",
  18620. "atomsvc": "application/atomsvc+xml",
  18621. "au": "audio/basic",
  18622. "avci": "image/avci",
  18623. "avcs": "image/avcs",
  18624. "avif": "image/avif",
  18625. "aw": "application/applixware",
  18626. "bdoc": "application/bdoc",
  18627. "bin": "application/octet-stream",
  18628. "bmp": "image/bmp",
  18629. "bpk": "application/octet-stream",
  18630. "btf": "image/prs.btif",
  18631. "btif": "image/prs.btif",
  18632. "buffer": "application/octet-stream",
  18633. "ccxml": "application/ccxml+xml",
  18634. "cdfx": "application/cdfx+xml",
  18635. "cdmia": "application/cdmi-capability",
  18636. "cdmic": "application/cdmi-container",
  18637. "cdmid": "application/cdmi-domain",
  18638. "cdmio": "application/cdmi-object",
  18639. "cdmiq": "application/cdmi-queue",
  18640. "cer": "application/pkix-cert",
  18641. "cgm": "image/cgm",
  18642. "cjs": "application/node",
  18643. "class": "application/java-vm",
  18644. "coffee": "text/coffeescript",
  18645. "conf": "text/plain",
  18646. "cpl": "application/cpl+xml",
  18647. "cpt": "application/mac-compactpro",
  18648. "crl": "application/pkix-crl",
  18649. "css": "text/css",
  18650. "csv": "text/csv",
  18651. "cu": "application/cu-seeme",
  18652. "cwl": "application/cwl",
  18653. "cww": "application/prs.cww",
  18654. "davmount": "application/davmount+xml",
  18655. "dbk": "application/docbook+xml",
  18656. "deb": "application/octet-stream",
  18657. "def": "text/plain",
  18658. "deploy": "application/octet-stream",
  18659. "dib": "image/bmp",
  18660. "disposition-notification": "message/disposition-notification",
  18661. "dist": "application/octet-stream",
  18662. "distz": "application/octet-stream",
  18663. "dll": "application/octet-stream",
  18664. "dmg": "application/octet-stream",
  18665. "dms": "application/octet-stream",
  18666. "doc": "application/msword",
  18667. "dot": "application/msword",
  18668. "dpx": "image/dpx",
  18669. "drle": "image/dicom-rle",
  18670. "dsc": "text/prs.lines.tag",
  18671. "dssc": "application/dssc+der",
  18672. "dtd": "application/xml-dtd",
  18673. "dump": "application/octet-stream",
  18674. "dwd": "application/atsc-dwd+xml",
  18675. "ear": "application/java-archive",
  18676. "ecma": "application/ecmascript",
  18677. "elc": "application/octet-stream",
  18678. "emf": "image/emf",
  18679. "eml": "message/rfc822",
  18680. "emma": "application/emma+xml",
  18681. "emotionml": "application/emotionml+xml",
  18682. "eps": "application/postscript",
  18683. "epub": "application/epub+zip",
  18684. "exe": "application/octet-stream",
  18685. "exi": "application/exi",
  18686. "exp": "application/express",
  18687. "exr": "image/aces",
  18688. "ez": "application/andrew-inset",
  18689. "fdf": "application/fdf",
  18690. "fdt": "application/fdt+xml",
  18691. "fits": "image/fits",
  18692. "g3": "image/g3fax",
  18693. "gbr": "application/rpki-ghostbusters",
  18694. "geojson": "application/geo+json",
  18695. "gif": "image/gif",
  18696. "glb": "model/gltf-binary",
  18697. "gltf": "model/gltf+json",
  18698. "gml": "application/gml+xml",
  18699. "gpx": "application/gpx+xml",
  18700. "gram": "application/srgs",
  18701. "grxml": "application/srgs+xml",
  18702. "gxf": "application/gxf",
  18703. "gz": "application/gzip",
  18704. "h261": "video/h261",
  18705. "h263": "video/h263",
  18706. "h264": "video/h264",
  18707. "heic": "image/heic",
  18708. "heics": "image/heic-sequence",
  18709. "heif": "image/heif",
  18710. "heifs": "image/heif-sequence",
  18711. "hej2": "image/hej2k",
  18712. "held": "application/atsc-held+xml",
  18713. "hjson": "application/hjson",
  18714. "hlp": "application/winhlp",
  18715. "hqx": "application/mac-binhex40",
  18716. "hsj2": "image/hsj2",
  18717. "htm": "text/html",
  18718. "html": "text/html",
  18719. "ics": "text/calendar",
  18720. "ief": "image/ief",
  18721. "ifb": "text/calendar",
  18722. "iges": "model/iges",
  18723. "igs": "model/iges",
  18724. "img": "application/octet-stream",
  18725. "in": "text/plain",
  18726. "ini": "text/plain",
  18727. "ink": "application/inkml+xml",
  18728. "inkml": "application/inkml+xml",
  18729. "ipfix": "application/ipfix",
  18730. "iso": "application/octet-stream",
  18731. "its": "application/its+xml",
  18732. "jade": "text/jade",
  18733. "jar": "application/java-archive",
  18734. "jhc": "image/jphc",
  18735. "jls": "image/jls",
  18736. "jp2": "image/jp2",
  18737. "jpe": "image/jpeg",
  18738. "jpeg": "image/jpeg",
  18739. "jpf": "image/jpx",
  18740. "jpg": "image/jpeg",
  18741. "jpg2": "image/jp2",
  18742. "jpgm": "image/jpm",
  18743. "jpgv": "video/jpeg",
  18744. "jph": "image/jph",
  18745. "jpm": "image/jpm",
  18746. "jpx": "image/jpx",
  18747. "js": "text/javascript",
  18748. "json": "application/json",
  18749. "json5": "application/json5",
  18750. "jsonld": "application/ld+json",
  18751. "jsonml": "application/jsonml+json",
  18752. "jsx": "text/jsx",
  18753. "jt": "model/jt",
  18754. "jxr": "image/jxr",
  18755. "jxra": "image/jxra",
  18756. "jxrs": "image/jxrs",
  18757. "jxs": "image/jxs",
  18758. "jxsc": "image/jxsc",
  18759. "jxsi": "image/jxsi",
  18760. "jxss": "image/jxss",
  18761. "kar": "audio/midi",
  18762. "ktx": "image/ktx",
  18763. "ktx2": "image/ktx2",
  18764. "less": "text/less",
  18765. "lgr": "application/lgr+xml",
  18766. "list": "text/plain",
  18767. "litcoffee": "text/coffeescript",
  18768. "log": "text/plain",
  18769. "lostxml": "application/lost+xml",
  18770. "lrf": "application/octet-stream",
  18771. "m1v": "video/mpeg",
  18772. "m21": "application/mp21",
  18773. "m2a": "audio/mpeg",
  18774. "m2v": "video/mpeg",
  18775. "m3a": "audio/mpeg",
  18776. "m4a": "audio/mp4",
  18777. "m4p": "application/mp4",
  18778. "m4s": "video/iso.segment",
  18779. "ma": "application/mathematica",
  18780. "mads": "application/mads+xml",
  18781. "maei": "application/mmt-aei+xml",
  18782. "man": "text/troff",
  18783. "manifest": "text/cache-manifest",
  18784. "map": "application/json",
  18785. "mar": "application/octet-stream",
  18786. "markdown": "text/markdown",
  18787. "mathml": "application/mathml+xml",
  18788. "mb": "application/mathematica",
  18789. "mbox": "application/mbox",
  18790. "md": "text/markdown",
  18791. "mdx": "text/mdx",
  18792. "me": "text/troff",
  18793. "mesh": "model/mesh",
  18794. "meta4": "application/metalink4+xml",
  18795. "metalink": "application/metalink+xml",
  18796. "mets": "application/mets+xml",
  18797. "mft": "application/rpki-manifest",
  18798. "mid": "audio/midi",
  18799. "midi": "audio/midi",
  18800. "mime": "message/rfc822",
  18801. "mj2": "video/mj2",
  18802. "mjp2": "video/mj2",
  18803. "mjs": "text/javascript",
  18804. "mml": "text/mathml",
  18805. "mods": "application/mods+xml",
  18806. "mov": "video/quicktime",
  18807. "mp2": "audio/mpeg",
  18808. "mp21": "application/mp21",
  18809. "mp2a": "audio/mpeg",
  18810. "mp3": "audio/mpeg",
  18811. "mp4": "video/mp4",
  18812. "mp4a": "audio/mp4",
  18813. "mp4s": "application/mp4",
  18814. "mp4v": "video/mp4",
  18815. "mpd": "application/dash+xml",
  18816. "mpe": "video/mpeg",
  18817. "mpeg": "video/mpeg",
  18818. "mpf": "application/media-policy-dataset+xml",
  18819. "mpg": "video/mpeg",
  18820. "mpg4": "video/mp4",
  18821. "mpga": "audio/mpeg",
  18822. "mpp": "application/dash-patch+xml",
  18823. "mrc": "application/marc",
  18824. "mrcx": "application/marcxml+xml",
  18825. "ms": "text/troff",
  18826. "mscml": "application/mediaservercontrol+xml",
  18827. "msh": "model/mesh",
  18828. "msi": "application/octet-stream",
  18829. "msix": "application/msix",
  18830. "msixbundle": "application/msixbundle",
  18831. "msm": "application/octet-stream",
  18832. "msp": "application/octet-stream",
  18833. "mtl": "model/mtl",
  18834. "musd": "application/mmt-usd+xml",
  18835. "mxf": "application/mxf",
  18836. "mxmf": "audio/mobile-xmf",
  18837. "mxml": "application/xv+xml",
  18838. "n3": "text/n3",
  18839. "nb": "application/mathematica",
  18840. "nq": "application/n-quads",
  18841. "nt": "application/n-triples",
  18842. "obj": "model/obj",
  18843. "oda": "application/oda",
  18844. "oga": "audio/ogg",
  18845. "ogg": "audio/ogg",
  18846. "ogv": "video/ogg",
  18847. "ogx": "application/ogg",
  18848. "omdoc": "application/omdoc+xml",
  18849. "onepkg": "application/onenote",
  18850. "onetmp": "application/onenote",
  18851. "onetoc": "application/onenote",
  18852. "onetoc2": "application/onenote",
  18853. "opf": "application/oebps-package+xml",
  18854. "opus": "audio/ogg",
  18855. "otf": "font/otf",
  18856. "owl": "application/rdf+xml",
  18857. "oxps": "application/oxps",
  18858. "p10": "application/pkcs10",
  18859. "p7c": "application/pkcs7-mime",
  18860. "p7m": "application/pkcs7-mime",
  18861. "p7s": "application/pkcs7-signature",
  18862. "p8": "application/pkcs8",
  18863. "pdf": "application/pdf",
  18864. "pfr": "application/font-tdpfr",
  18865. "pgp": "application/pgp-encrypted",
  18866. "pkg": "application/octet-stream",
  18867. "pki": "application/pkixcmp",
  18868. "pkipath": "application/pkix-pkipath",
  18869. "pls": "application/pls+xml",
  18870. "png": "image/png",
  18871. "prc": "model/prc",
  18872. "prf": "application/pics-rules",
  18873. "provx": "application/provenance+xml",
  18874. "ps": "application/postscript",
  18875. "pskcxml": "application/pskc+xml",
  18876. "pti": "image/prs.pti",
  18877. "qt": "video/quicktime",
  18878. "raml": "application/raml+yaml",
  18879. "rapd": "application/route-apd+xml",
  18880. "rdf": "application/rdf+xml",
  18881. "relo": "application/p2p-overlay+xml",
  18882. "rif": "application/reginfo+xml",
  18883. "rl": "application/resource-lists+xml",
  18884. "rld": "application/resource-lists-diff+xml",
  18885. "rmi": "audio/midi",
  18886. "rnc": "application/relax-ng-compact-syntax",
  18887. "rng": "application/xml",
  18888. "roa": "application/rpki-roa",
  18889. "roff": "text/troff",
  18890. "rq": "application/sparql-query",
  18891. "rs": "application/rls-services+xml",
  18892. "rsat": "application/atsc-rsat+xml",
  18893. "rsd": "application/rsd+xml",
  18894. "rsheet": "application/urc-ressheet+xml",
  18895. "rss": "application/rss+xml",
  18896. "rtf": "text/rtf",
  18897. "rtx": "text/richtext",
  18898. "rusd": "application/route-usd+xml",
  18899. "s3m": "audio/s3m",
  18900. "sbml": "application/sbml+xml",
  18901. "scq": "application/scvp-cv-request",
  18902. "scs": "application/scvp-cv-response",
  18903. "sdp": "application/sdp",
  18904. "senmlx": "application/senml+xml",
  18905. "sensmlx": "application/sensml+xml",
  18906. "ser": "application/java-serialized-object",
  18907. "setpay": "application/set-payment-initiation",
  18908. "setreg": "application/set-registration-initiation",
  18909. "sgi": "image/sgi",
  18910. "sgm": "text/sgml",
  18911. "sgml": "text/sgml",
  18912. "shex": "text/shex",
  18913. "shf": "application/shf+xml",
  18914. "shtml": "text/html",
  18915. "sieve": "application/sieve",
  18916. "sig": "application/pgp-signature",
  18917. "sil": "audio/silk",
  18918. "silo": "model/mesh",
  18919. "siv": "application/sieve",
  18920. "slim": "text/slim",
  18921. "slm": "text/slim",
  18922. "sls": "application/route-s-tsid+xml",
  18923. "smi": "application/smil+xml",
  18924. "smil": "application/smil+xml",
  18925. "snd": "audio/basic",
  18926. "so": "application/octet-stream",
  18927. "spdx": "text/spdx",
  18928. "spp": "application/scvp-vp-response",
  18929. "spq": "application/scvp-vp-request",
  18930. "spx": "audio/ogg",
  18931. "sql": "application/sql",
  18932. "sru": "application/sru+xml",
  18933. "srx": "application/sparql-results+xml",
  18934. "ssdl": "application/ssdl+xml",
  18935. "ssml": "application/ssml+xml",
  18936. "stk": "application/hyperstudio",
  18937. "stl": "model/stl",
  18938. "stpx": "model/step+xml",
  18939. "stpxz": "model/step-xml+zip",
  18940. "stpz": "model/step+zip",
  18941. "styl": "text/stylus",
  18942. "stylus": "text/stylus",
  18943. "svg": "image/svg+xml",
  18944. "svgz": "image/svg+xml",
  18945. "swidtag": "application/swid+xml",
  18946. "t": "text/troff",
  18947. "t38": "image/t38",
  18948. "td": "application/urc-targetdesc+xml",
  18949. "tei": "application/tei+xml",
  18950. "teicorpus": "application/tei+xml",
  18951. "text": "text/plain",
  18952. "tfi": "application/thraud+xml",
  18953. "tfx": "image/tiff-fx",
  18954. "tif": "image/tiff",
  18955. "tiff": "image/tiff",
  18956. "toml": "application/toml",
  18957. "tr": "text/troff",
  18958. "trig": "application/trig",
  18959. "ts": "video/mp2t",
  18960. "tsd": "application/timestamped-data",
  18961. "tsv": "text/tab-separated-values",
  18962. "ttc": "font/collection",
  18963. "ttf": "font/ttf",
  18964. "ttl": "text/turtle",
  18965. "ttml": "application/ttml+xml",
  18966. "txt": "text/plain",
  18967. "u3d": "model/u3d",
  18968. "u8dsn": "message/global-delivery-status",
  18969. "u8hdr": "message/global-headers",
  18970. "u8mdn": "message/global-disposition-notification",
  18971. "u8msg": "message/global",
  18972. "ubj": "application/ubjson",
  18973. "uri": "text/uri-list",
  18974. "uris": "text/uri-list",
  18975. "urls": "text/uri-list",
  18976. "vcard": "text/vcard",
  18977. "vrml": "model/vrml",
  18978. "vtt": "text/vtt",
  18979. "vxml": "application/voicexml+xml",
  18980. "war": "application/java-archive",
  18981. "wasm": "application/wasm",
  18982. "wav": "audio/wav",
  18983. "weba": "audio/webm",
  18984. "webm": "video/webm",
  18985. "webmanifest": "application/manifest+json",
  18986. "webp": "image/webp",
  18987. "wgsl": "text/wgsl",
  18988. "wgt": "application/widget",
  18989. "wif": "application/watcherinfo+xml",
  18990. "wmf": "image/wmf",
  18991. "woff": "font/woff",
  18992. "woff2": "font/woff2",
  18993. "wrl": "model/vrml",
  18994. "wsdl": "application/wsdl+xml",
  18995. "wspolicy": "application/wspolicy+xml",
  18996. "x3d": "model/x3d+xml",
  18997. "x3db": "model/x3d+fastinfoset",
  18998. "x3dbz": "model/x3d+binary",
  18999. "x3dv": "model/x3d-vrml",
  19000. "x3dvz": "model/x3d+vrml",
  19001. "x3dz": "model/x3d+xml",
  19002. "xaml": "application/xaml+xml",
  19003. "xav": "application/xcap-att+xml",
  19004. "xca": "application/xcap-caps+xml",
  19005. "xcs": "application/calendar+xml",
  19006. "xdf": "application/xcap-diff+xml",
  19007. "xdssc": "application/dssc+xml",
  19008. "xel": "application/xcap-el+xml",
  19009. "xenc": "application/xenc+xml",
  19010. "xer": "application/patch-ops-error+xml",
  19011. "xfdf": "application/xfdf",
  19012. "xht": "application/xhtml+xml",
  19013. "xhtml": "application/xhtml+xml",
  19014. "xhvml": "application/xv+xml",
  19015. "xlf": "application/xliff+xml",
  19016. "xm": "audio/xm",
  19017. "xml": "text/xml",
  19018. "xns": "application/xcap-ns+xml",
  19019. "xop": "application/xop+xml",
  19020. "xpl": "application/xproc+xml",
  19021. "xsd": "application/xml",
  19022. "xsf": "application/prs.xsf+xml",
  19023. "xsl": "application/xml",
  19024. "xslt": "application/xml",
  19025. "xspf": "application/xspf+xml",
  19026. "xvm": "application/xv+xml",
  19027. "xvml": "application/xv+xml",
  19028. "yaml": "text/yaml",
  19029. "yang": "application/yang",
  19030. "yin": "application/yin+xml",
  19031. "yml": "text/yaml",
  19032. "zip": "application/zip"
  19033. };
  19034. function lookup(extn) {
  19035. let tmp = ('' + extn).trim().toLowerCase();
  19036. let idx = tmp.lastIndexOf('.');
  19037. return mimes[!~idx ? tmp : tmp.substring(++idx)];
  19038. }
  19039. const publicFilesMap = /* @__PURE__ */ new WeakMap();
  19040. async function initPublicFiles(config) {
  19041. let fileNames;
  19042. try {
  19043. fileNames = await recursiveReaddir(config.publicDir);
  19044. } catch (e) {
  19045. if (e.code === ERR_SYMLINK_IN_RECURSIVE_READDIR) {
  19046. return;
  19047. }
  19048. throw e;
  19049. }
  19050. const publicFiles = new Set(
  19051. fileNames.map((fileName) => fileName.slice(config.publicDir.length))
  19052. );
  19053. publicFilesMap.set(config, publicFiles);
  19054. return publicFiles;
  19055. }
  19056. function getPublicFiles(config) {
  19057. return publicFilesMap.get(config);
  19058. }
  19059. function checkPublicFile(url, config) {
  19060. const { publicDir } = config;
  19061. if (!publicDir || url[0] !== "/") {
  19062. return;
  19063. }
  19064. const fileName = cleanUrl(url);
  19065. const publicFiles = getPublicFiles(config);
  19066. if (publicFiles) {
  19067. return publicFiles.has(fileName) ? normalizePath$3(path$n.join(publicDir, fileName)) : void 0;
  19068. }
  19069. const publicFile = normalizePath$3(path$n.join(publicDir, fileName));
  19070. if (!publicFile.startsWith(withTrailingSlash(publicDir))) {
  19071. return;
  19072. }
  19073. return fs__default.existsSync(publicFile) ? publicFile : void 0;
  19074. }
  19075. const assetUrlRE = /__VITE_ASSET__([\w$]+)__(?:\$_(.*?)__)?/g;
  19076. const jsSourceMapRE = /\.[cm]?js\.map$/;
  19077. const assetCache = /* @__PURE__ */ new WeakMap();
  19078. const generatedAssets = /* @__PURE__ */ new WeakMap();
  19079. function registerCustomMime() {
  19080. mimes["ico"] = "image/x-icon";
  19081. mimes["flac"] = "audio/flac";
  19082. mimes["eot"] = "application/vnd.ms-fontobject";
  19083. }
  19084. function renderAssetUrlInJS(ctx, config, chunk, opts, code) {
  19085. const toRelativeRuntime = createToImportMetaURLBasedRelativeRuntime(
  19086. opts.format,
  19087. config.isWorker
  19088. );
  19089. let match;
  19090. let s;
  19091. assetUrlRE.lastIndex = 0;
  19092. while (match = assetUrlRE.exec(code)) {
  19093. s ||= new MagicString(code);
  19094. const [full, referenceId, postfix = ""] = match;
  19095. const file = ctx.getFileName(referenceId);
  19096. chunk.viteMetadata.importedAssets.add(cleanUrl(file));
  19097. const filename = file + postfix;
  19098. const replacement = toOutputFilePathInJS(
  19099. filename,
  19100. "asset",
  19101. chunk.fileName,
  19102. "js",
  19103. config,
  19104. toRelativeRuntime
  19105. );
  19106. const replacementString = typeof replacement === "string" ? JSON.stringify(encodeURIPath(replacement)).slice(1, -1) : `"+${replacement.runtime}+"`;
  19107. s.update(match.index, match.index + full.length, replacementString);
  19108. }
  19109. const publicAssetUrlMap = publicAssetUrlCache.get(config);
  19110. publicAssetUrlRE.lastIndex = 0;
  19111. while (match = publicAssetUrlRE.exec(code)) {
  19112. s ||= new MagicString(code);
  19113. const [full, hash] = match;
  19114. const publicUrl = publicAssetUrlMap.get(hash).slice(1);
  19115. const replacement = toOutputFilePathInJS(
  19116. publicUrl,
  19117. "public",
  19118. chunk.fileName,
  19119. "js",
  19120. config,
  19121. toRelativeRuntime
  19122. );
  19123. const replacementString = typeof replacement === "string" ? JSON.stringify(encodeURIPath(replacement)).slice(1, -1) : `"+${replacement.runtime}+"`;
  19124. s.update(match.index, match.index + full.length, replacementString);
  19125. }
  19126. return s;
  19127. }
  19128. function assetPlugin(config) {
  19129. registerCustomMime();
  19130. let moduleGraph;
  19131. return {
  19132. name: "vite:asset",
  19133. buildStart() {
  19134. assetCache.set(config, /* @__PURE__ */ new Map());
  19135. generatedAssets.set(config, /* @__PURE__ */ new Map());
  19136. },
  19137. configureServer(server) {
  19138. moduleGraph = server.moduleGraph;
  19139. },
  19140. resolveId(id) {
  19141. if (!config.assetsInclude(cleanUrl(id)) && !urlRE$1.test(id)) {
  19142. return;
  19143. }
  19144. const publicFile = checkPublicFile(id, config);
  19145. if (publicFile) {
  19146. return id;
  19147. }
  19148. },
  19149. async load(id) {
  19150. if (id[0] === "\0") {
  19151. return;
  19152. }
  19153. if (rawRE$1.test(id)) {
  19154. const file = checkPublicFile(id, config) || cleanUrl(id);
  19155. this.addWatchFile(file);
  19156. return `export default ${JSON.stringify(
  19157. await fsp.readFile(file, "utf-8")
  19158. )}`;
  19159. }
  19160. if (!urlRE$1.test(id) && !config.assetsInclude(cleanUrl(id))) {
  19161. return;
  19162. }
  19163. id = removeUrlQuery(id);
  19164. let url = await fileToUrl$1(id, config, this);
  19165. if (moduleGraph) {
  19166. const mod = moduleGraph.getModuleById(id);
  19167. if (mod && mod.lastHMRTimestamp > 0) {
  19168. url = injectQuery(url, `t=${mod.lastHMRTimestamp}`);
  19169. }
  19170. }
  19171. return {
  19172. code: `export default ${JSON.stringify(encodeURIPath(url))}`,
  19173. // Force rollup to keep this module from being shared between other entry points if it's an entrypoint.
  19174. // If the resulting chunk is empty, it will be removed in generateBundle.
  19175. moduleSideEffects: config.command === "build" && this.getModuleInfo(id)?.isEntry ? "no-treeshake" : false,
  19176. meta: config.command === "build" ? { "vite:asset": true } : void 0
  19177. };
  19178. },
  19179. renderChunk(code, chunk, opts) {
  19180. const s = renderAssetUrlInJS(this, config, chunk, opts, code);
  19181. if (s) {
  19182. return {
  19183. code: s.toString(),
  19184. map: config.build.sourcemap ? s.generateMap({ hires: "boundary" }) : null
  19185. };
  19186. } else {
  19187. return null;
  19188. }
  19189. },
  19190. generateBundle(_, bundle) {
  19191. for (const file in bundle) {
  19192. const chunk = bundle[file];
  19193. if (chunk.type === "chunk" && chunk.isEntry && chunk.moduleIds.length === 1 && config.assetsInclude(chunk.moduleIds[0]) && this.getModuleInfo(chunk.moduleIds[0])?.meta["vite:asset"]) {
  19194. delete bundle[file];
  19195. }
  19196. }
  19197. if (config.command === "build" && config.build.ssr && !config.build.ssrEmitAssets) {
  19198. for (const file in bundle) {
  19199. if (bundle[file].type === "asset" && !file.endsWith("ssr-manifest.json") && !jsSourceMapRE.test(file)) {
  19200. delete bundle[file];
  19201. }
  19202. }
  19203. }
  19204. }
  19205. };
  19206. }
  19207. async function fileToUrl$1(id, config, ctx) {
  19208. if (config.command === "serve") {
  19209. return fileToDevUrl(id, config);
  19210. } else {
  19211. return fileToBuiltUrl(id, config, ctx);
  19212. }
  19213. }
  19214. function fileToDevUrl(id, config, skipBase = false) {
  19215. let rtn;
  19216. if (checkPublicFile(id, config)) {
  19217. rtn = id;
  19218. } else if (id.startsWith(withTrailingSlash(config.root))) {
  19219. rtn = "/" + path$n.posix.relative(config.root, id);
  19220. } else {
  19221. rtn = path$n.posix.join(FS_PREFIX, id);
  19222. }
  19223. if (skipBase) {
  19224. return rtn;
  19225. }
  19226. const base = joinUrlSegments(config.server?.origin ?? "", config.decodedBase);
  19227. return joinUrlSegments(base, removeLeadingSlash(rtn));
  19228. }
  19229. function getPublicAssetFilename(hash, config) {
  19230. return publicAssetUrlCache.get(config)?.get(hash);
  19231. }
  19232. const publicAssetUrlCache = /* @__PURE__ */ new WeakMap();
  19233. const publicAssetUrlRE = /__VITE_PUBLIC_ASSET__([a-z\d]{8})__/g;
  19234. function publicFileToBuiltUrl(url, config) {
  19235. if (config.command !== "build") {
  19236. return joinUrlSegments(config.decodedBase, url);
  19237. }
  19238. const hash = getHash(url);
  19239. let cache = publicAssetUrlCache.get(config);
  19240. if (!cache) {
  19241. cache = /* @__PURE__ */ new Map();
  19242. publicAssetUrlCache.set(config, cache);
  19243. }
  19244. if (!cache.get(hash)) {
  19245. cache.set(hash, url);
  19246. }
  19247. return `__VITE_PUBLIC_ASSET__${hash}__`;
  19248. }
  19249. const GIT_LFS_PREFIX = Buffer$1.from("version https://git-lfs.github.com");
  19250. function isGitLfsPlaceholder(content) {
  19251. if (content.length < GIT_LFS_PREFIX.length) return false;
  19252. return GIT_LFS_PREFIX.compare(content, 0, GIT_LFS_PREFIX.length) === 0;
  19253. }
  19254. async function fileToBuiltUrl(id, config, pluginContext, skipPublicCheck = false, forceInline) {
  19255. if (!skipPublicCheck && checkPublicFile(id, config)) {
  19256. return publicFileToBuiltUrl(id, config);
  19257. }
  19258. const cache = assetCache.get(config);
  19259. const cached = cache.get(id);
  19260. if (cached) {
  19261. return cached;
  19262. }
  19263. const file = cleanUrl(id);
  19264. const content = await fsp.readFile(file);
  19265. let url;
  19266. if (shouldInline(config, file, id, content, pluginContext, forceInline)) {
  19267. if (config.build.lib && isGitLfsPlaceholder(content)) {
  19268. config.logger.warn(
  19269. colors$1.yellow(`Inlined file ${id} was not downloaded via Git LFS`)
  19270. );
  19271. }
  19272. if (file.endsWith(".svg")) {
  19273. url = svgToDataURL(content);
  19274. } else {
  19275. const mimeType = lookup(file) ?? "application/octet-stream";
  19276. url = `data:${mimeType};base64,${content.toString("base64")}`;
  19277. }
  19278. } else {
  19279. const { search, hash } = parse$h(id);
  19280. const postfix = (search || "") + (hash || "");
  19281. const originalFileName = normalizePath$3(path$n.relative(config.root, file));
  19282. const referenceId = pluginContext.emitFile({
  19283. type: "asset",
  19284. // Ignore directory structure for asset file names
  19285. name: path$n.basename(file),
  19286. originalFileName,
  19287. source: content
  19288. });
  19289. generatedAssets.get(config).set(referenceId, { originalFileName });
  19290. url = `__VITE_ASSET__${referenceId}__${postfix ? `$_${postfix}__` : ``}`;
  19291. }
  19292. cache.set(id, url);
  19293. return url;
  19294. }
  19295. async function urlToBuiltUrl(url, importer, config, pluginContext, forceInline) {
  19296. if (checkPublicFile(url, config)) {
  19297. return publicFileToBuiltUrl(url, config);
  19298. }
  19299. const file = url[0] === "/" ? path$n.join(config.root, url) : path$n.join(path$n.dirname(importer), url);
  19300. return fileToBuiltUrl(
  19301. file,
  19302. config,
  19303. pluginContext,
  19304. // skip public check since we just did it above
  19305. true,
  19306. forceInline
  19307. );
  19308. }
  19309. const shouldInline = (config, file, id, content, pluginContext, forceInline) => {
  19310. if (config.build.lib) return true;
  19311. if (pluginContext.getModuleInfo(id)?.isEntry) return false;
  19312. if (forceInline !== void 0) return forceInline;
  19313. let limit;
  19314. if (typeof config.build.assetsInlineLimit === "function") {
  19315. const userShouldInline = config.build.assetsInlineLimit(file, content);
  19316. if (userShouldInline != null) return userShouldInline;
  19317. limit = DEFAULT_ASSETS_INLINE_LIMIT;
  19318. } else {
  19319. limit = Number(config.build.assetsInlineLimit);
  19320. }
  19321. if (file.endsWith(".html")) return false;
  19322. if (file.endsWith(".svg") && id.includes("#")) return false;
  19323. return content.length < limit && !isGitLfsPlaceholder(content);
  19324. };
  19325. const nestedQuotesRE = /"[^"']*'[^"]*"|'[^'"]*"[^']*'/;
  19326. function svgToDataURL(content) {
  19327. const stringContent = content.toString();
  19328. if (stringContent.includes("<text") || stringContent.includes("<foreignObject") || nestedQuotesRE.test(stringContent)) {
  19329. return `data:image/svg+xml;base64,${content.toString("base64")}`;
  19330. } else {
  19331. return "data:image/svg+xml," + stringContent.trim().replaceAll(/>\s+</g, "><").replaceAll('"', "'").replaceAll("%", "%25").replaceAll("#", "%23").replaceAll("<", "%3c").replaceAll(">", "%3e").replaceAll(/\s+/g, "%20");
  19332. }
  19333. }
  19334. const endsWithJSRE = /\.[cm]?js$/;
  19335. function manifestPlugin(config) {
  19336. const manifest = {};
  19337. let outputCount;
  19338. return {
  19339. name: "vite:manifest",
  19340. buildStart() {
  19341. outputCount = 0;
  19342. },
  19343. generateBundle({ format }, bundle) {
  19344. function getChunkName(chunk) {
  19345. return getChunkOriginalFileName(chunk, config.root, format);
  19346. }
  19347. function getInternalImports(imports) {
  19348. const filteredImports = [];
  19349. for (const file of imports) {
  19350. if (bundle[file] === void 0) {
  19351. continue;
  19352. }
  19353. filteredImports.push(getChunkName(bundle[file]));
  19354. }
  19355. return filteredImports;
  19356. }
  19357. function createChunk(chunk) {
  19358. const manifestChunk = {
  19359. file: chunk.fileName,
  19360. name: chunk.name
  19361. };
  19362. if (chunk.facadeModuleId) {
  19363. manifestChunk.src = getChunkName(chunk);
  19364. }
  19365. if (chunk.isEntry) {
  19366. manifestChunk.isEntry = true;
  19367. }
  19368. if (chunk.isDynamicEntry) {
  19369. manifestChunk.isDynamicEntry = true;
  19370. }
  19371. if (chunk.imports.length) {
  19372. const internalImports = getInternalImports(chunk.imports);
  19373. if (internalImports.length > 0) {
  19374. manifestChunk.imports = internalImports;
  19375. }
  19376. }
  19377. if (chunk.dynamicImports.length) {
  19378. const internalImports = getInternalImports(chunk.dynamicImports);
  19379. if (internalImports.length > 0) {
  19380. manifestChunk.dynamicImports = internalImports;
  19381. }
  19382. }
  19383. if (chunk.viteMetadata?.importedCss.size) {
  19384. manifestChunk.css = [...chunk.viteMetadata.importedCss];
  19385. }
  19386. if (chunk.viteMetadata?.importedAssets.size) {
  19387. manifestChunk.assets = [...chunk.viteMetadata.importedAssets];
  19388. }
  19389. return manifestChunk;
  19390. }
  19391. function createAsset(asset, src, isEntry) {
  19392. const manifestChunk = {
  19393. file: asset.fileName,
  19394. src
  19395. };
  19396. if (isEntry) manifestChunk.isEntry = true;
  19397. return manifestChunk;
  19398. }
  19399. const assets = generatedAssets.get(config);
  19400. const entryCssAssetFileNames = /* @__PURE__ */ new Set();
  19401. for (const [id, asset] of assets.entries()) {
  19402. if (asset.isEntry) {
  19403. try {
  19404. const fileName = this.getFileName(id);
  19405. entryCssAssetFileNames.add(fileName);
  19406. } catch (error) {
  19407. assets.delete(id);
  19408. }
  19409. }
  19410. }
  19411. const fileNameToAsset = /* @__PURE__ */ new Map();
  19412. for (const file in bundle) {
  19413. const chunk = bundle[file];
  19414. if (chunk.type === "chunk") {
  19415. manifest[getChunkName(chunk)] = createChunk(chunk);
  19416. } else if (chunk.type === "asset" && typeof chunk.name === "string") {
  19417. const src = chunk.originalFileName ?? chunk.name;
  19418. const isEntry = entryCssAssetFileNames.has(chunk.fileName);
  19419. const asset = createAsset(chunk, src, isEntry);
  19420. const file2 = manifest[src]?.file;
  19421. if (file2 && endsWithJSRE.test(file2)) continue;
  19422. manifest[src] = asset;
  19423. fileNameToAsset.set(chunk.fileName, asset);
  19424. }
  19425. }
  19426. for (const [referenceId, { originalFileName }] of assets.entries()) {
  19427. if (!manifest[originalFileName]) {
  19428. const fileName = this.getFileName(referenceId);
  19429. const asset = fileNameToAsset.get(fileName);
  19430. if (asset) {
  19431. manifest[originalFileName] = asset;
  19432. }
  19433. }
  19434. }
  19435. outputCount++;
  19436. const output = config.build.rollupOptions?.output;
  19437. const outputLength = Array.isArray(output) ? output.length : 1;
  19438. if (outputCount >= outputLength) {
  19439. this.emitFile({
  19440. fileName: typeof config.build.manifest === "string" ? config.build.manifest : ".vite/manifest.json",
  19441. type: "asset",
  19442. source: JSON.stringify(sortObjectKeys(manifest), void 0, 2)
  19443. });
  19444. }
  19445. }
  19446. };
  19447. }
  19448. function getChunkOriginalFileName(chunk, root, format) {
  19449. if (chunk.facadeModuleId) {
  19450. let name = normalizePath$3(path$n.relative(root, chunk.facadeModuleId));
  19451. if (format === "system" && !chunk.name.includes("-legacy")) {
  19452. const ext = path$n.extname(name);
  19453. const endPos = ext.length !== 0 ? -ext.length : void 0;
  19454. name = name.slice(0, endPos) + `-legacy` + ext;
  19455. }
  19456. return name.replace(/\0/g, "");
  19457. } else {
  19458. return `_` + path$n.basename(chunk.fileName);
  19459. }
  19460. }
  19461. const dataUriRE = /^([^/]+\/[^;,]+)(;base64)?,([\s\S]*)$/;
  19462. const base64RE = /base64/i;
  19463. const dataUriPrefix = `\0/@data-uri/`;
  19464. function dataURIPlugin() {
  19465. let resolved;
  19466. return {
  19467. name: "vite:data-uri",
  19468. buildStart() {
  19469. resolved = /* @__PURE__ */ new Map();
  19470. },
  19471. resolveId(id) {
  19472. if (!id.trimStart().startsWith("data:")) {
  19473. return;
  19474. }
  19475. const uri = new URL$3(id);
  19476. if (uri.protocol !== "data:") {
  19477. return;
  19478. }
  19479. const match = dataUriRE.exec(uri.pathname);
  19480. if (!match) {
  19481. return;
  19482. }
  19483. const [, mime, format, data] = match;
  19484. if (mime !== "text/javascript") {
  19485. throw new Error(
  19486. `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.`
  19487. );
  19488. }
  19489. const base64 = format && base64RE.test(format.substring(1));
  19490. const content = base64 ? Buffer.from(data, "base64").toString("utf-8") : data;
  19491. resolved.set(id, content);
  19492. return dataUriPrefix + id;
  19493. },
  19494. load(id) {
  19495. if (id.startsWith(dataUriPrefix)) {
  19496. return resolved.get(id.slice(dataUriPrefix.length));
  19497. }
  19498. }
  19499. };
  19500. }
  19501. /* es-module-lexer 1.5.4 */
  19502. 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;
  19503. var convertSourceMap$1 = {};
  19504. (function (exports) {
  19505. Object.defineProperty(exports, 'commentRegex', {
  19506. get: function getCommentRegex () {
  19507. // Groups: 1: media type, 2: MIME type, 3: charset, 4: encoding, 5: data.
  19508. return /^\s*?\/[\/\*][@#]\s+?sourceMappingURL=data:(((?:application|text)\/json)(?:;charset=([^;,]+?)?)?)?(?:;(base64))?,(.*?)$/mg;
  19509. }
  19510. });
  19511. Object.defineProperty(exports, 'mapFileCommentRegex', {
  19512. get: function getMapFileCommentRegex () {
  19513. // Matches sourceMappingURL in either // or /* comment styles.
  19514. return /(?:\/\/[@#][ \t]+?sourceMappingURL=([^\s'"`]+?)[ \t]*?$)|(?:\/\*[@#][ \t]+sourceMappingURL=([^*]+?)[ \t]*?(?:\*\/){1}[ \t]*?$)/mg;
  19515. }
  19516. });
  19517. var decodeBase64;
  19518. if (typeof Buffer !== 'undefined') {
  19519. if (typeof Buffer.from === 'function') {
  19520. decodeBase64 = decodeBase64WithBufferFrom;
  19521. } else {
  19522. decodeBase64 = decodeBase64WithNewBuffer;
  19523. }
  19524. } else {
  19525. decodeBase64 = decodeBase64WithAtob;
  19526. }
  19527. function decodeBase64WithBufferFrom(base64) {
  19528. return Buffer.from(base64, 'base64').toString();
  19529. }
  19530. function decodeBase64WithNewBuffer(base64) {
  19531. if (typeof value === 'number') {
  19532. throw new TypeError('The value to decode must not be of type number.');
  19533. }
  19534. return new Buffer(base64, 'base64').toString();
  19535. }
  19536. function decodeBase64WithAtob(base64) {
  19537. return decodeURIComponent(escape(atob(base64)));
  19538. }
  19539. function stripComment(sm) {
  19540. return sm.split(',').pop();
  19541. }
  19542. function readFromFileMap(sm, read) {
  19543. var r = exports.mapFileCommentRegex.exec(sm);
  19544. // for some odd reason //# .. captures in 1 and /* .. */ in 2
  19545. var filename = r[1] || r[2];
  19546. try {
  19547. var sm = read(filename);
  19548. if (sm != null && typeof sm.catch === 'function') {
  19549. return sm.catch(throwError);
  19550. } else {
  19551. return sm;
  19552. }
  19553. } catch (e) {
  19554. throwError(e);
  19555. }
  19556. function throwError(e) {
  19557. throw new Error('An error occurred while trying to read the map file at ' + filename + '\n' + e.stack);
  19558. }
  19559. }
  19560. function Converter (sm, opts) {
  19561. opts = opts || {};
  19562. if (opts.hasComment) {
  19563. sm = stripComment(sm);
  19564. }
  19565. if (opts.encoding === 'base64') {
  19566. sm = decodeBase64(sm);
  19567. } else if (opts.encoding === 'uri') {
  19568. sm = decodeURIComponent(sm);
  19569. }
  19570. if (opts.isJSON || opts.encoding) {
  19571. sm = JSON.parse(sm);
  19572. }
  19573. this.sourcemap = sm;
  19574. }
  19575. Converter.prototype.toJSON = function (space) {
  19576. return JSON.stringify(this.sourcemap, null, space);
  19577. };
  19578. if (typeof Buffer !== 'undefined') {
  19579. if (typeof Buffer.from === 'function') {
  19580. Converter.prototype.toBase64 = encodeBase64WithBufferFrom;
  19581. } else {
  19582. Converter.prototype.toBase64 = encodeBase64WithNewBuffer;
  19583. }
  19584. } else {
  19585. Converter.prototype.toBase64 = encodeBase64WithBtoa;
  19586. }
  19587. function encodeBase64WithBufferFrom() {
  19588. var json = this.toJSON();
  19589. return Buffer.from(json, 'utf8').toString('base64');
  19590. }
  19591. function encodeBase64WithNewBuffer() {
  19592. var json = this.toJSON();
  19593. if (typeof json === 'number') {
  19594. throw new TypeError('The json to encode must not be of type number.');
  19595. }
  19596. return new Buffer(json, 'utf8').toString('base64');
  19597. }
  19598. function encodeBase64WithBtoa() {
  19599. var json = this.toJSON();
  19600. return btoa(unescape(encodeURIComponent(json)));
  19601. }
  19602. Converter.prototype.toURI = function () {
  19603. var json = this.toJSON();
  19604. return encodeURIComponent(json);
  19605. };
  19606. Converter.prototype.toComment = function (options) {
  19607. var encoding, content, data;
  19608. if (options != null && options.encoding === 'uri') {
  19609. encoding = '';
  19610. content = this.toURI();
  19611. } else {
  19612. encoding = ';base64';
  19613. content = this.toBase64();
  19614. }
  19615. data = 'sourceMappingURL=data:application/json;charset=utf-8' + encoding + ',' + content;
  19616. return options != null && options.multiline ? '/*# ' + data + ' */' : '//# ' + data;
  19617. };
  19618. // returns copy instead of original
  19619. Converter.prototype.toObject = function () {
  19620. return JSON.parse(this.toJSON());
  19621. };
  19622. Converter.prototype.addProperty = function (key, value) {
  19623. if (this.sourcemap.hasOwnProperty(key)) throw new Error('property "' + key + '" already exists on the sourcemap, use set property instead');
  19624. return this.setProperty(key, value);
  19625. };
  19626. Converter.prototype.setProperty = function (key, value) {
  19627. this.sourcemap[key] = value;
  19628. return this;
  19629. };
  19630. Converter.prototype.getProperty = function (key) {
  19631. return this.sourcemap[key];
  19632. };
  19633. exports.fromObject = function (obj) {
  19634. return new Converter(obj);
  19635. };
  19636. exports.fromJSON = function (json) {
  19637. return new Converter(json, { isJSON: true });
  19638. };
  19639. exports.fromURI = function (uri) {
  19640. return new Converter(uri, { encoding: 'uri' });
  19641. };
  19642. exports.fromBase64 = function (base64) {
  19643. return new Converter(base64, { encoding: 'base64' });
  19644. };
  19645. exports.fromComment = function (comment) {
  19646. var m, encoding;
  19647. comment = comment
  19648. .replace(/^\/\*/g, '//')
  19649. .replace(/\*\/$/g, '');
  19650. m = exports.commentRegex.exec(comment);
  19651. encoding = m && m[4] || 'uri';
  19652. return new Converter(comment, { encoding: encoding, hasComment: true });
  19653. };
  19654. function makeConverter(sm) {
  19655. return new Converter(sm, { isJSON: true });
  19656. }
  19657. exports.fromMapFileComment = function (comment, read) {
  19658. if (typeof read === 'string') {
  19659. throw new Error(
  19660. 'String directory paths are no longer supported with `fromMapFileComment`\n' +
  19661. 'Please review the Upgrading documentation at https://github.com/thlorenz/convert-source-map#upgrading'
  19662. )
  19663. }
  19664. var sm = readFromFileMap(comment, read);
  19665. if (sm != null && typeof sm.then === 'function') {
  19666. return sm.then(makeConverter);
  19667. } else {
  19668. return makeConverter(sm);
  19669. }
  19670. };
  19671. // Finds last sourcemap comment in file or returns null if none was found
  19672. exports.fromSource = function (content) {
  19673. var m = content.match(exports.commentRegex);
  19674. return m ? exports.fromComment(m.pop()) : null;
  19675. };
  19676. // Finds last sourcemap comment in file or returns null if none was found
  19677. exports.fromMapFileSource = function (content, read) {
  19678. if (typeof read === 'string') {
  19679. throw new Error(
  19680. 'String directory paths are no longer supported with `fromMapFileSource`\n' +
  19681. 'Please review the Upgrading documentation at https://github.com/thlorenz/convert-source-map#upgrading'
  19682. )
  19683. }
  19684. var m = content.match(exports.mapFileCommentRegex);
  19685. return m ? exports.fromMapFileComment(m.pop(), read) : null;
  19686. };
  19687. exports.removeComments = function (src) {
  19688. return src.replace(exports.commentRegex, '');
  19689. };
  19690. exports.removeMapFileComments = function (src) {
  19691. return src.replace(exports.mapFileCommentRegex, '');
  19692. };
  19693. exports.generateMapFileComment = function (file, options) {
  19694. var data = 'sourceMappingURL=' + file;
  19695. return options && options.multiline ? '/*# ' + data + ' */' : '//# ' + data;
  19696. };
  19697. } (convertSourceMap$1));
  19698. var convertSourceMap = /*@__PURE__*/getDefaultExportFromCjs(convertSourceMap$1);
  19699. const debug$g = createDebugger("vite:sourcemap", {
  19700. onlyWhenFocused: true
  19701. });
  19702. const virtualSourceRE = /^(?:dep:|browser-external:|virtual:)|\0/;
  19703. async function computeSourceRoute(map, file) {
  19704. let sourceRoot;
  19705. try {
  19706. sourceRoot = await fsp.realpath(
  19707. path$n.resolve(path$n.dirname(file), map.sourceRoot || "")
  19708. );
  19709. } catch {
  19710. }
  19711. return sourceRoot;
  19712. }
  19713. async function injectSourcesContent(map, file, logger) {
  19714. let sourceRootPromise;
  19715. const missingSources = [];
  19716. const sourcesContent = map.sourcesContent || [];
  19717. const sourcesContentPromises = [];
  19718. for (let index = 0; index < map.sources.length; index++) {
  19719. const sourcePath = map.sources[index];
  19720. if (sourcesContent[index] == null && sourcePath && !virtualSourceRE.test(sourcePath)) {
  19721. sourcesContentPromises.push(
  19722. (async () => {
  19723. sourceRootPromise ??= computeSourceRoute(map, file);
  19724. const sourceRoot = await sourceRootPromise;
  19725. let resolvedSourcePath = cleanUrl(decodeURI(sourcePath));
  19726. if (sourceRoot) {
  19727. resolvedSourcePath = path$n.resolve(sourceRoot, resolvedSourcePath);
  19728. }
  19729. sourcesContent[index] = await fsp.readFile(resolvedSourcePath, "utf-8").catch(() => {
  19730. missingSources.push(resolvedSourcePath);
  19731. return null;
  19732. });
  19733. })()
  19734. );
  19735. }
  19736. }
  19737. await Promise.all(sourcesContentPromises);
  19738. map.sourcesContent = sourcesContent;
  19739. if (missingSources.length) {
  19740. logger.warnOnce(`Sourcemap for "${file}" points to missing source files`);
  19741. debug$g?.(`Missing sources:
  19742. ` + missingSources.join(`
  19743. `));
  19744. }
  19745. }
  19746. function genSourceMapUrl(map) {
  19747. if (typeof map !== "string") {
  19748. map = JSON.stringify(map);
  19749. }
  19750. return `data:application/json;base64,${Buffer.from(map).toString("base64")}`;
  19751. }
  19752. function getCodeWithSourcemap(type, code, map) {
  19753. if (debug$g) {
  19754. code += `
  19755. /*${JSON.stringify(map, null, 2).replace(/\*\//g, "*\\/")}*/
  19756. `;
  19757. }
  19758. if (type === "js") {
  19759. code += `
  19760. //# sourceMappingURL=${genSourceMapUrl(map)}`;
  19761. } else if (type === "css") {
  19762. code += `
  19763. /*# sourceMappingURL=${genSourceMapUrl(map)} */`;
  19764. }
  19765. return code;
  19766. }
  19767. function applySourcemapIgnoreList(map, sourcemapPath, sourcemapIgnoreList, logger) {
  19768. let { x_google_ignoreList } = map;
  19769. if (x_google_ignoreList === void 0) {
  19770. x_google_ignoreList = [];
  19771. }
  19772. for (let sourcesIndex = 0; sourcesIndex < map.sources.length; ++sourcesIndex) {
  19773. const sourcePath = map.sources[sourcesIndex];
  19774. if (!sourcePath) continue;
  19775. const ignoreList = sourcemapIgnoreList(
  19776. path$n.isAbsolute(sourcePath) ? sourcePath : path$n.resolve(path$n.dirname(sourcemapPath), sourcePath),
  19777. sourcemapPath
  19778. );
  19779. if (logger && typeof ignoreList !== "boolean") {
  19780. logger.warn("sourcemapIgnoreList function must return a boolean.");
  19781. }
  19782. if (ignoreList && !x_google_ignoreList.includes(sourcesIndex)) {
  19783. x_google_ignoreList.push(sourcesIndex);
  19784. }
  19785. }
  19786. if (x_google_ignoreList.length > 0) {
  19787. if (!map.x_google_ignoreList) map.x_google_ignoreList = x_google_ignoreList;
  19788. }
  19789. }
  19790. async function extractSourcemapFromFile(code, filePath) {
  19791. const map = (convertSourceMap.fromSource(code) || await convertSourceMap.fromMapFileSource(
  19792. code,
  19793. createConvertSourceMapReadMap(filePath)
  19794. ))?.toObject();
  19795. if (map) {
  19796. return {
  19797. code: code.replace(convertSourceMap.mapFileCommentRegex, blankReplacer),
  19798. map
  19799. };
  19800. }
  19801. }
  19802. function createConvertSourceMapReadMap(originalFileName) {
  19803. return (filename) => {
  19804. return fsp.readFile(
  19805. path$n.resolve(path$n.dirname(originalFileName), filename),
  19806. "utf-8"
  19807. );
  19808. };
  19809. }
  19810. var tasks = {};
  19811. var utils$g = {};
  19812. var array$1 = {};
  19813. Object.defineProperty(array$1, "__esModule", { value: true });
  19814. array$1.splitWhen = array$1.flatten = void 0;
  19815. function flatten$1(items) {
  19816. return items.reduce((collection, item) => [].concat(collection, item), []);
  19817. }
  19818. array$1.flatten = flatten$1;
  19819. function splitWhen(items, predicate) {
  19820. const result = [[]];
  19821. let groupIndex = 0;
  19822. for (const item of items) {
  19823. if (predicate(item)) {
  19824. groupIndex++;
  19825. result[groupIndex] = [];
  19826. }
  19827. else {
  19828. result[groupIndex].push(item);
  19829. }
  19830. }
  19831. return result;
  19832. }
  19833. array$1.splitWhen = splitWhen;
  19834. var errno$1 = {};
  19835. Object.defineProperty(errno$1, "__esModule", { value: true });
  19836. errno$1.isEnoentCodeError = void 0;
  19837. function isEnoentCodeError(error) {
  19838. return error.code === 'ENOENT';
  19839. }
  19840. errno$1.isEnoentCodeError = isEnoentCodeError;
  19841. var fs$i = {};
  19842. Object.defineProperty(fs$i, "__esModule", { value: true });
  19843. fs$i.createDirentFromStats = void 0;
  19844. let DirentFromStats$1 = class DirentFromStats {
  19845. constructor(name, stats) {
  19846. this.name = name;
  19847. this.isBlockDevice = stats.isBlockDevice.bind(stats);
  19848. this.isCharacterDevice = stats.isCharacterDevice.bind(stats);
  19849. this.isDirectory = stats.isDirectory.bind(stats);
  19850. this.isFIFO = stats.isFIFO.bind(stats);
  19851. this.isFile = stats.isFile.bind(stats);
  19852. this.isSocket = stats.isSocket.bind(stats);
  19853. this.isSymbolicLink = stats.isSymbolicLink.bind(stats);
  19854. }
  19855. };
  19856. function createDirentFromStats$1(name, stats) {
  19857. return new DirentFromStats$1(name, stats);
  19858. }
  19859. fs$i.createDirentFromStats = createDirentFromStats$1;
  19860. var path$i = {};
  19861. Object.defineProperty(path$i, "__esModule", { value: true });
  19862. 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;
  19863. const os$4 = require$$2;
  19864. const path$h = require$$0$4;
  19865. const IS_WINDOWS_PLATFORM = os$4.platform() === 'win32';
  19866. const LEADING_DOT_SEGMENT_CHARACTERS_COUNT = 2; // ./ or .\\
  19867. /**
  19868. * All non-escaped special characters.
  19869. * Posix: ()*?[]{|}, !+@ before (, ! at the beginning, \\ before non-special characters.
  19870. * Windows: (){}[], !+@ before (, ! at the beginning.
  19871. */
  19872. const POSIX_UNESCAPED_GLOB_SYMBOLS_RE = /(\\?)([()*?[\]{|}]|^!|[!+@](?=\()|\\(?![!()*+?@[\]{|}]))/g;
  19873. const WINDOWS_UNESCAPED_GLOB_SYMBOLS_RE = /(\\?)([()[\]{}]|^!|[!+@](?=\())/g;
  19874. /**
  19875. * The device path (\\.\ or \\?\).
  19876. * https://learn.microsoft.com/en-us/dotnet/standard/io/file-path-formats#dos-device-paths
  19877. */
  19878. const DOS_DEVICE_PATH_RE = /^\\\\([.?])/;
  19879. /**
  19880. * All backslashes except those escaping special characters.
  19881. * Windows: !()+@{}
  19882. * https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file#naming-conventions
  19883. */
  19884. const WINDOWS_BACKSLASHES_RE = /\\(?![!()+@[\]{}])/g;
  19885. /**
  19886. * Designed to work only with simple paths: `dir\\file`.
  19887. */
  19888. function unixify(filepath) {
  19889. return filepath.replace(/\\/g, '/');
  19890. }
  19891. path$i.unixify = unixify;
  19892. function makeAbsolute(cwd, filepath) {
  19893. return path$h.resolve(cwd, filepath);
  19894. }
  19895. path$i.makeAbsolute = makeAbsolute;
  19896. function removeLeadingDotSegment(entry) {
  19897. // We do not use `startsWith` because this is 10x slower than current implementation for some cases.
  19898. // eslint-disable-next-line @typescript-eslint/prefer-string-starts-ends-with
  19899. if (entry.charAt(0) === '.') {
  19900. const secondCharactery = entry.charAt(1);
  19901. if (secondCharactery === '/' || secondCharactery === '\\') {
  19902. return entry.slice(LEADING_DOT_SEGMENT_CHARACTERS_COUNT);
  19903. }
  19904. }
  19905. return entry;
  19906. }
  19907. path$i.removeLeadingDotSegment = removeLeadingDotSegment;
  19908. path$i.escape = IS_WINDOWS_PLATFORM ? escapeWindowsPath : escapePosixPath;
  19909. function escapeWindowsPath(pattern) {
  19910. return pattern.replace(WINDOWS_UNESCAPED_GLOB_SYMBOLS_RE, '\\$2');
  19911. }
  19912. path$i.escapeWindowsPath = escapeWindowsPath;
  19913. function escapePosixPath(pattern) {
  19914. return pattern.replace(POSIX_UNESCAPED_GLOB_SYMBOLS_RE, '\\$2');
  19915. }
  19916. path$i.escapePosixPath = escapePosixPath;
  19917. path$i.convertPathToPattern = IS_WINDOWS_PLATFORM ? convertWindowsPathToPattern : convertPosixPathToPattern;
  19918. function convertWindowsPathToPattern(filepath) {
  19919. return escapeWindowsPath(filepath)
  19920. .replace(DOS_DEVICE_PATH_RE, '//$1')
  19921. .replace(WINDOWS_BACKSLASHES_RE, '/');
  19922. }
  19923. path$i.convertWindowsPathToPattern = convertWindowsPathToPattern;
  19924. function convertPosixPathToPattern(filepath) {
  19925. return escapePosixPath(filepath);
  19926. }
  19927. path$i.convertPosixPathToPattern = convertPosixPathToPattern;
  19928. var pattern$1 = {};
  19929. /*!
  19930. * is-extglob <https://github.com/jonschlinkert/is-extglob>
  19931. *
  19932. * Copyright (c) 2014-2016, Jon Schlinkert.
  19933. * Licensed under the MIT License.
  19934. */
  19935. var isExtglob$1 = function isExtglob(str) {
  19936. if (typeof str !== 'string' || str === '') {
  19937. return false;
  19938. }
  19939. var match;
  19940. while ((match = /(\\).|([@?!+*]\(.*\))/g.exec(str))) {
  19941. if (match[2]) return true;
  19942. str = str.slice(match.index + match[0].length);
  19943. }
  19944. return false;
  19945. };
  19946. /*!
  19947. * is-glob <https://github.com/jonschlinkert/is-glob>
  19948. *
  19949. * Copyright (c) 2014-2017, Jon Schlinkert.
  19950. * Released under the MIT License.
  19951. */
  19952. var isExtglob = isExtglob$1;
  19953. var chars = { '{': '}', '(': ')', '[': ']'};
  19954. var strictCheck = function(str) {
  19955. if (str[0] === '!') {
  19956. return true;
  19957. }
  19958. var index = 0;
  19959. var pipeIndex = -2;
  19960. var closeSquareIndex = -2;
  19961. var closeCurlyIndex = -2;
  19962. var closeParenIndex = -2;
  19963. var backSlashIndex = -2;
  19964. while (index < str.length) {
  19965. if (str[index] === '*') {
  19966. return true;
  19967. }
  19968. if (str[index + 1] === '?' && /[\].+)]/.test(str[index])) {
  19969. return true;
  19970. }
  19971. if (closeSquareIndex !== -1 && str[index] === '[' && str[index + 1] !== ']') {
  19972. if (closeSquareIndex < index) {
  19973. closeSquareIndex = str.indexOf(']', index);
  19974. }
  19975. if (closeSquareIndex > index) {
  19976. if (backSlashIndex === -1 || backSlashIndex > closeSquareIndex) {
  19977. return true;
  19978. }
  19979. backSlashIndex = str.indexOf('\\', index);
  19980. if (backSlashIndex === -1 || backSlashIndex > closeSquareIndex) {
  19981. return true;
  19982. }
  19983. }
  19984. }
  19985. if (closeCurlyIndex !== -1 && str[index] === '{' && str[index + 1] !== '}') {
  19986. closeCurlyIndex = str.indexOf('}', index);
  19987. if (closeCurlyIndex > index) {
  19988. backSlashIndex = str.indexOf('\\', index);
  19989. if (backSlashIndex === -1 || backSlashIndex > closeCurlyIndex) {
  19990. return true;
  19991. }
  19992. }
  19993. }
  19994. if (closeParenIndex !== -1 && str[index] === '(' && str[index + 1] === '?' && /[:!=]/.test(str[index + 2]) && str[index + 3] !== ')') {
  19995. closeParenIndex = str.indexOf(')', index);
  19996. if (closeParenIndex > index) {
  19997. backSlashIndex = str.indexOf('\\', index);
  19998. if (backSlashIndex === -1 || backSlashIndex > closeParenIndex) {
  19999. return true;
  20000. }
  20001. }
  20002. }
  20003. if (pipeIndex !== -1 && str[index] === '(' && str[index + 1] !== '|') {
  20004. if (pipeIndex < index) {
  20005. pipeIndex = str.indexOf('|', index);
  20006. }
  20007. if (pipeIndex !== -1 && str[pipeIndex + 1] !== ')') {
  20008. closeParenIndex = str.indexOf(')', pipeIndex);
  20009. if (closeParenIndex > pipeIndex) {
  20010. backSlashIndex = str.indexOf('\\', pipeIndex);
  20011. if (backSlashIndex === -1 || backSlashIndex > closeParenIndex) {
  20012. return true;
  20013. }
  20014. }
  20015. }
  20016. }
  20017. if (str[index] === '\\') {
  20018. var open = str[index + 1];
  20019. index += 2;
  20020. var close = chars[open];
  20021. if (close) {
  20022. var n = str.indexOf(close, index);
  20023. if (n !== -1) {
  20024. index = n + 1;
  20025. }
  20026. }
  20027. if (str[index] === '!') {
  20028. return true;
  20029. }
  20030. } else {
  20031. index++;
  20032. }
  20033. }
  20034. return false;
  20035. };
  20036. var relaxedCheck = function(str) {
  20037. if (str[0] === '!') {
  20038. return true;
  20039. }
  20040. var index = 0;
  20041. while (index < str.length) {
  20042. if (/[*?{}()[\]]/.test(str[index])) {
  20043. return true;
  20044. }
  20045. if (str[index] === '\\') {
  20046. var open = str[index + 1];
  20047. index += 2;
  20048. var close = chars[open];
  20049. if (close) {
  20050. var n = str.indexOf(close, index);
  20051. if (n !== -1) {
  20052. index = n + 1;
  20053. }
  20054. }
  20055. if (str[index] === '!') {
  20056. return true;
  20057. }
  20058. } else {
  20059. index++;
  20060. }
  20061. }
  20062. return false;
  20063. };
  20064. var isGlob$2 = function isGlob(str, options) {
  20065. if (typeof str !== 'string' || str === '') {
  20066. return false;
  20067. }
  20068. if (isExtglob(str)) {
  20069. return true;
  20070. }
  20071. var check = strictCheck;
  20072. // optionally relax check
  20073. if (options && options.strict === false) {
  20074. check = relaxedCheck;
  20075. }
  20076. return check(str);
  20077. };
  20078. var isGlob$1 = isGlob$2;
  20079. var pathPosixDirname = require$$0$4.posix.dirname;
  20080. var isWin32 = require$$2.platform() === 'win32';
  20081. var slash = '/';
  20082. var backslash = /\\/g;
  20083. var enclosure = /[\{\[].*[\}\]]$/;
  20084. var globby = /(^|[^\\])([\{\[]|\([^\)]+$)/;
  20085. var escaped = /\\([\!\*\?\|\[\]\(\)\{\}])/g;
  20086. /**
  20087. * @param {string} str
  20088. * @param {Object} opts
  20089. * @param {boolean} [opts.flipBackslashes=true]
  20090. * @returns {string}
  20091. */
  20092. var globParent$2 = function globParent(str, opts) {
  20093. var options = Object.assign({ flipBackslashes: true }, opts);
  20094. // flip windows path separators
  20095. if (options.flipBackslashes && isWin32 && str.indexOf(slash) < 0) {
  20096. str = str.replace(backslash, slash);
  20097. }
  20098. // special case for strings ending in enclosure containing path separator
  20099. if (enclosure.test(str)) {
  20100. str += slash;
  20101. }
  20102. // preserves full path in case of trailing path separator
  20103. str += 'a';
  20104. // remove path parts that are globby
  20105. do {
  20106. str = pathPosixDirname(str);
  20107. } while (isGlob$1(str) || globby.test(str));
  20108. // remove escape chars and return result
  20109. return str.replace(escaped, '$1');
  20110. };
  20111. var utils$f = {};
  20112. (function (exports) {
  20113. exports.isInteger = num => {
  20114. if (typeof num === 'number') {
  20115. return Number.isInteger(num);
  20116. }
  20117. if (typeof num === 'string' && num.trim() !== '') {
  20118. return Number.isInteger(Number(num));
  20119. }
  20120. return false;
  20121. };
  20122. /**
  20123. * Find a node of the given type
  20124. */
  20125. exports.find = (node, type) => node.nodes.find(node => node.type === type);
  20126. /**
  20127. * Find a node of the given type
  20128. */
  20129. exports.exceedsLimit = (min, max, step = 1, limit) => {
  20130. if (limit === false) return false;
  20131. if (!exports.isInteger(min) || !exports.isInteger(max)) return false;
  20132. return ((Number(max) - Number(min)) / Number(step)) >= limit;
  20133. };
  20134. /**
  20135. * Escape the given node with '\\' before node.value
  20136. */
  20137. exports.escapeNode = (block, n = 0, type) => {
  20138. const node = block.nodes[n];
  20139. if (!node) return;
  20140. if ((type && node.type === type) || node.type === 'open' || node.type === 'close') {
  20141. if (node.escaped !== true) {
  20142. node.value = '\\' + node.value;
  20143. node.escaped = true;
  20144. }
  20145. }
  20146. };
  20147. /**
  20148. * Returns true if the given brace node should be enclosed in literal braces
  20149. */
  20150. exports.encloseBrace = node => {
  20151. if (node.type !== 'brace') return false;
  20152. if ((node.commas >> 0 + node.ranges >> 0) === 0) {
  20153. node.invalid = true;
  20154. return true;
  20155. }
  20156. return false;
  20157. };
  20158. /**
  20159. * Returns true if a brace node is invalid.
  20160. */
  20161. exports.isInvalidBrace = block => {
  20162. if (block.type !== 'brace') return false;
  20163. if (block.invalid === true || block.dollar) return true;
  20164. if ((block.commas >> 0 + block.ranges >> 0) === 0) {
  20165. block.invalid = true;
  20166. return true;
  20167. }
  20168. if (block.open !== true || block.close !== true) {
  20169. block.invalid = true;
  20170. return true;
  20171. }
  20172. return false;
  20173. };
  20174. /**
  20175. * Returns true if a node is an open or close node
  20176. */
  20177. exports.isOpenOrClose = node => {
  20178. if (node.type === 'open' || node.type === 'close') {
  20179. return true;
  20180. }
  20181. return node.open === true || node.close === true;
  20182. };
  20183. /**
  20184. * Reduce an array of text nodes.
  20185. */
  20186. exports.reduce = nodes => nodes.reduce((acc, node) => {
  20187. if (node.type === 'text') acc.push(node.value);
  20188. if (node.type === 'range') node.type = 'text';
  20189. return acc;
  20190. }, []);
  20191. /**
  20192. * Flatten an array
  20193. */
  20194. exports.flatten = (...args) => {
  20195. const result = [];
  20196. const flat = arr => {
  20197. for (let i = 0; i < arr.length; i++) {
  20198. const ele = arr[i];
  20199. if (Array.isArray(ele)) {
  20200. flat(ele);
  20201. continue;
  20202. }
  20203. if (ele !== undefined) {
  20204. result.push(ele);
  20205. }
  20206. }
  20207. return result;
  20208. };
  20209. flat(args);
  20210. return result;
  20211. };
  20212. } (utils$f));
  20213. const utils$e = utils$f;
  20214. var stringify$7 = (ast, options = {}) => {
  20215. const stringify = (node, parent = {}) => {
  20216. const invalidBlock = options.escapeInvalid && utils$e.isInvalidBrace(parent);
  20217. const invalidNode = node.invalid === true && options.escapeInvalid === true;
  20218. let output = '';
  20219. if (node.value) {
  20220. if ((invalidBlock || invalidNode) && utils$e.isOpenOrClose(node)) {
  20221. return '\\' + node.value;
  20222. }
  20223. return node.value;
  20224. }
  20225. if (node.value) {
  20226. return node.value;
  20227. }
  20228. if (node.nodes) {
  20229. for (const child of node.nodes) {
  20230. output += stringify(child);
  20231. }
  20232. }
  20233. return output;
  20234. };
  20235. return stringify(ast);
  20236. };
  20237. /*!
  20238. * is-number <https://github.com/jonschlinkert/is-number>
  20239. *
  20240. * Copyright (c) 2014-present, Jon Schlinkert.
  20241. * Released under the MIT License.
  20242. */
  20243. var isNumber$2 = function(num) {
  20244. if (typeof num === 'number') {
  20245. return num - num === 0;
  20246. }
  20247. if (typeof num === 'string' && num.trim() !== '') {
  20248. return Number.isFinite ? Number.isFinite(+num) : isFinite(+num);
  20249. }
  20250. return false;
  20251. };
  20252. /*!
  20253. * to-regex-range <https://github.com/micromatch/to-regex-range>
  20254. *
  20255. * Copyright (c) 2015-present, Jon Schlinkert.
  20256. * Released under the MIT License.
  20257. */
  20258. const isNumber$1 = isNumber$2;
  20259. const toRegexRange$1 = (min, max, options) => {
  20260. if (isNumber$1(min) === false) {
  20261. throw new TypeError('toRegexRange: expected the first argument to be a number');
  20262. }
  20263. if (max === void 0 || min === max) {
  20264. return String(min);
  20265. }
  20266. if (isNumber$1(max) === false) {
  20267. throw new TypeError('toRegexRange: expected the second argument to be a number.');
  20268. }
  20269. let opts = { relaxZeros: true, ...options };
  20270. if (typeof opts.strictZeros === 'boolean') {
  20271. opts.relaxZeros = opts.strictZeros === false;
  20272. }
  20273. let relax = String(opts.relaxZeros);
  20274. let shorthand = String(opts.shorthand);
  20275. let capture = String(opts.capture);
  20276. let wrap = String(opts.wrap);
  20277. let cacheKey = min + ':' + max + '=' + relax + shorthand + capture + wrap;
  20278. if (toRegexRange$1.cache.hasOwnProperty(cacheKey)) {
  20279. return toRegexRange$1.cache[cacheKey].result;
  20280. }
  20281. let a = Math.min(min, max);
  20282. let b = Math.max(min, max);
  20283. if (Math.abs(a - b) === 1) {
  20284. let result = min + '|' + max;
  20285. if (opts.capture) {
  20286. return `(${result})`;
  20287. }
  20288. if (opts.wrap === false) {
  20289. return result;
  20290. }
  20291. return `(?:${result})`;
  20292. }
  20293. let isPadded = hasPadding(min) || hasPadding(max);
  20294. let state = { min, max, a, b };
  20295. let positives = [];
  20296. let negatives = [];
  20297. if (isPadded) {
  20298. state.isPadded = isPadded;
  20299. state.maxLen = String(state.max).length;
  20300. }
  20301. if (a < 0) {
  20302. let newMin = b < 0 ? Math.abs(b) : 1;
  20303. negatives = splitToPatterns(newMin, Math.abs(a), state, opts);
  20304. a = state.a = 0;
  20305. }
  20306. if (b >= 0) {
  20307. positives = splitToPatterns(a, b, state, opts);
  20308. }
  20309. state.negatives = negatives;
  20310. state.positives = positives;
  20311. state.result = collatePatterns(negatives, positives);
  20312. if (opts.capture === true) {
  20313. state.result = `(${state.result})`;
  20314. } else if (opts.wrap !== false && (positives.length + negatives.length) > 1) {
  20315. state.result = `(?:${state.result})`;
  20316. }
  20317. toRegexRange$1.cache[cacheKey] = state;
  20318. return state.result;
  20319. };
  20320. function collatePatterns(neg, pos, options) {
  20321. let onlyNegative = filterPatterns(neg, pos, '-', false) || [];
  20322. let onlyPositive = filterPatterns(pos, neg, '', false) || [];
  20323. let intersected = filterPatterns(neg, pos, '-?', true) || [];
  20324. let subpatterns = onlyNegative.concat(intersected).concat(onlyPositive);
  20325. return subpatterns.join('|');
  20326. }
  20327. function splitToRanges(min, max) {
  20328. let nines = 1;
  20329. let zeros = 1;
  20330. let stop = countNines(min, nines);
  20331. let stops = new Set([max]);
  20332. while (min <= stop && stop <= max) {
  20333. stops.add(stop);
  20334. nines += 1;
  20335. stop = countNines(min, nines);
  20336. }
  20337. stop = countZeros(max + 1, zeros) - 1;
  20338. while (min < stop && stop <= max) {
  20339. stops.add(stop);
  20340. zeros += 1;
  20341. stop = countZeros(max + 1, zeros) - 1;
  20342. }
  20343. stops = [...stops];
  20344. stops.sort(compare);
  20345. return stops;
  20346. }
  20347. /**
  20348. * Convert a range to a regex pattern
  20349. * @param {Number} `start`
  20350. * @param {Number} `stop`
  20351. * @return {String}
  20352. */
  20353. function rangeToPattern(start, stop, options) {
  20354. if (start === stop) {
  20355. return { pattern: start, count: [], digits: 0 };
  20356. }
  20357. let zipped = zip(start, stop);
  20358. let digits = zipped.length;
  20359. let pattern = '';
  20360. let count = 0;
  20361. for (let i = 0; i < digits; i++) {
  20362. let [startDigit, stopDigit] = zipped[i];
  20363. if (startDigit === stopDigit) {
  20364. pattern += startDigit;
  20365. } else if (startDigit !== '0' || stopDigit !== '9') {
  20366. pattern += toCharacterClass(startDigit, stopDigit);
  20367. } else {
  20368. count++;
  20369. }
  20370. }
  20371. if (count) {
  20372. pattern += options.shorthand === true ? '\\d' : '[0-9]';
  20373. }
  20374. return { pattern, count: [count], digits };
  20375. }
  20376. function splitToPatterns(min, max, tok, options) {
  20377. let ranges = splitToRanges(min, max);
  20378. let tokens = [];
  20379. let start = min;
  20380. let prev;
  20381. for (let i = 0; i < ranges.length; i++) {
  20382. let max = ranges[i];
  20383. let obj = rangeToPattern(String(start), String(max), options);
  20384. let zeros = '';
  20385. if (!tok.isPadded && prev && prev.pattern === obj.pattern) {
  20386. if (prev.count.length > 1) {
  20387. prev.count.pop();
  20388. }
  20389. prev.count.push(obj.count[0]);
  20390. prev.string = prev.pattern + toQuantifier(prev.count);
  20391. start = max + 1;
  20392. continue;
  20393. }
  20394. if (tok.isPadded) {
  20395. zeros = padZeros(max, tok, options);
  20396. }
  20397. obj.string = zeros + obj.pattern + toQuantifier(obj.count);
  20398. tokens.push(obj);
  20399. start = max + 1;
  20400. prev = obj;
  20401. }
  20402. return tokens;
  20403. }
  20404. function filterPatterns(arr, comparison, prefix, intersection, options) {
  20405. let result = [];
  20406. for (let ele of arr) {
  20407. let { string } = ele;
  20408. // only push if _both_ are negative...
  20409. if (!intersection && !contains(comparison, 'string', string)) {
  20410. result.push(prefix + string);
  20411. }
  20412. // or _both_ are positive
  20413. if (intersection && contains(comparison, 'string', string)) {
  20414. result.push(prefix + string);
  20415. }
  20416. }
  20417. return result;
  20418. }
  20419. /**
  20420. * Zip strings
  20421. */
  20422. function zip(a, b) {
  20423. let arr = [];
  20424. for (let i = 0; i < a.length; i++) arr.push([a[i], b[i]]);
  20425. return arr;
  20426. }
  20427. function compare(a, b) {
  20428. return a > b ? 1 : b > a ? -1 : 0;
  20429. }
  20430. function contains(arr, key, val) {
  20431. return arr.some(ele => ele[key] === val);
  20432. }
  20433. function countNines(min, len) {
  20434. return Number(String(min).slice(0, -len) + '9'.repeat(len));
  20435. }
  20436. function countZeros(integer, zeros) {
  20437. return integer - (integer % Math.pow(10, zeros));
  20438. }
  20439. function toQuantifier(digits) {
  20440. let [start = 0, stop = ''] = digits;
  20441. if (stop || start > 1) {
  20442. return `{${start + (stop ? ',' + stop : '')}}`;
  20443. }
  20444. return '';
  20445. }
  20446. function toCharacterClass(a, b, options) {
  20447. return `[${a}${(b - a === 1) ? '' : '-'}${b}]`;
  20448. }
  20449. function hasPadding(str) {
  20450. return /^-?(0+)\d/.test(str);
  20451. }
  20452. function padZeros(value, tok, options) {
  20453. if (!tok.isPadded) {
  20454. return value;
  20455. }
  20456. let diff = Math.abs(tok.maxLen - String(value).length);
  20457. let relax = options.relaxZeros !== false;
  20458. switch (diff) {
  20459. case 0:
  20460. return '';
  20461. case 1:
  20462. return relax ? '0?' : '0';
  20463. case 2:
  20464. return relax ? '0{0,2}' : '00';
  20465. default: {
  20466. return relax ? `0{0,${diff}}` : `0{${diff}}`;
  20467. }
  20468. }
  20469. }
  20470. /**
  20471. * Cache
  20472. */
  20473. toRegexRange$1.cache = {};
  20474. toRegexRange$1.clearCache = () => (toRegexRange$1.cache = {});
  20475. /**
  20476. * Expose `toRegexRange`
  20477. */
  20478. var toRegexRange_1 = toRegexRange$1;
  20479. /*!
  20480. * fill-range <https://github.com/jonschlinkert/fill-range>
  20481. *
  20482. * Copyright (c) 2014-present, Jon Schlinkert.
  20483. * Licensed under the MIT License.
  20484. */
  20485. const util$1 = require$$0$5;
  20486. const toRegexRange = toRegexRange_1;
  20487. const isObject = val => val !== null && typeof val === 'object' && !Array.isArray(val);
  20488. const transform = toNumber => {
  20489. return value => toNumber === true ? Number(value) : String(value);
  20490. };
  20491. const isValidValue = value => {
  20492. return typeof value === 'number' || (typeof value === 'string' && value !== '');
  20493. };
  20494. const isNumber = num => Number.isInteger(+num);
  20495. const zeros = input => {
  20496. let value = `${input}`;
  20497. let index = -1;
  20498. if (value[0] === '-') value = value.slice(1);
  20499. if (value === '0') return false;
  20500. while (value[++index] === '0');
  20501. return index > 0;
  20502. };
  20503. const stringify$6 = (start, end, options) => {
  20504. if (typeof start === 'string' || typeof end === 'string') {
  20505. return true;
  20506. }
  20507. return options.stringify === true;
  20508. };
  20509. const pad = (input, maxLength, toNumber) => {
  20510. if (maxLength > 0) {
  20511. let dash = input[0] === '-' ? '-' : '';
  20512. if (dash) input = input.slice(1);
  20513. input = (dash + input.padStart(dash ? maxLength - 1 : maxLength, '0'));
  20514. }
  20515. if (toNumber === false) {
  20516. return String(input);
  20517. }
  20518. return input;
  20519. };
  20520. const toMaxLen = (input, maxLength) => {
  20521. let negative = input[0] === '-' ? '-' : '';
  20522. if (negative) {
  20523. input = input.slice(1);
  20524. maxLength--;
  20525. }
  20526. while (input.length < maxLength) input = '0' + input;
  20527. return negative ? ('-' + input) : input;
  20528. };
  20529. const toSequence = (parts, options, maxLen) => {
  20530. parts.negatives.sort((a, b) => a < b ? -1 : a > b ? 1 : 0);
  20531. parts.positives.sort((a, b) => a < b ? -1 : a > b ? 1 : 0);
  20532. let prefix = options.capture ? '' : '?:';
  20533. let positives = '';
  20534. let negatives = '';
  20535. let result;
  20536. if (parts.positives.length) {
  20537. positives = parts.positives.map(v => toMaxLen(String(v), maxLen)).join('|');
  20538. }
  20539. if (parts.negatives.length) {
  20540. negatives = `-(${prefix}${parts.negatives.map(v => toMaxLen(String(v), maxLen)).join('|')})`;
  20541. }
  20542. if (positives && negatives) {
  20543. result = `${positives}|${negatives}`;
  20544. } else {
  20545. result = positives || negatives;
  20546. }
  20547. if (options.wrap) {
  20548. return `(${prefix}${result})`;
  20549. }
  20550. return result;
  20551. };
  20552. const toRange = (a, b, isNumbers, options) => {
  20553. if (isNumbers) {
  20554. return toRegexRange(a, b, { wrap: false, ...options });
  20555. }
  20556. let start = String.fromCharCode(a);
  20557. if (a === b) return start;
  20558. let stop = String.fromCharCode(b);
  20559. return `[${start}-${stop}]`;
  20560. };
  20561. const toRegex = (start, end, options) => {
  20562. if (Array.isArray(start)) {
  20563. let wrap = options.wrap === true;
  20564. let prefix = options.capture ? '' : '?:';
  20565. return wrap ? `(${prefix}${start.join('|')})` : start.join('|');
  20566. }
  20567. return toRegexRange(start, end, options);
  20568. };
  20569. const rangeError = (...args) => {
  20570. return new RangeError('Invalid range arguments: ' + util$1.inspect(...args));
  20571. };
  20572. const invalidRange = (start, end, options) => {
  20573. if (options.strictRanges === true) throw rangeError([start, end]);
  20574. return [];
  20575. };
  20576. const invalidStep = (step, options) => {
  20577. if (options.strictRanges === true) {
  20578. throw new TypeError(`Expected step "${step}" to be a number`);
  20579. }
  20580. return [];
  20581. };
  20582. const fillNumbers = (start, end, step = 1, options = {}) => {
  20583. let a = Number(start);
  20584. let b = Number(end);
  20585. if (!Number.isInteger(a) || !Number.isInteger(b)) {
  20586. if (options.strictRanges === true) throw rangeError([start, end]);
  20587. return [];
  20588. }
  20589. // fix negative zero
  20590. if (a === 0) a = 0;
  20591. if (b === 0) b = 0;
  20592. let descending = a > b;
  20593. let startString = String(start);
  20594. let endString = String(end);
  20595. let stepString = String(step);
  20596. step = Math.max(Math.abs(step), 1);
  20597. let padded = zeros(startString) || zeros(endString) || zeros(stepString);
  20598. let maxLen = padded ? Math.max(startString.length, endString.length, stepString.length) : 0;
  20599. let toNumber = padded === false && stringify$6(start, end, options) === false;
  20600. let format = options.transform || transform(toNumber);
  20601. if (options.toRegex && step === 1) {
  20602. return toRange(toMaxLen(start, maxLen), toMaxLen(end, maxLen), true, options);
  20603. }
  20604. let parts = { negatives: [], positives: [] };
  20605. let push = num => parts[num < 0 ? 'negatives' : 'positives'].push(Math.abs(num));
  20606. let range = [];
  20607. let index = 0;
  20608. while (descending ? a >= b : a <= b) {
  20609. if (options.toRegex === true && step > 1) {
  20610. push(a);
  20611. } else {
  20612. range.push(pad(format(a, index), maxLen, toNumber));
  20613. }
  20614. a = descending ? a - step : a + step;
  20615. index++;
  20616. }
  20617. if (options.toRegex === true) {
  20618. return step > 1
  20619. ? toSequence(parts, options, maxLen)
  20620. : toRegex(range, null, { wrap: false, ...options });
  20621. }
  20622. return range;
  20623. };
  20624. const fillLetters = (start, end, step = 1, options = {}) => {
  20625. if ((!isNumber(start) && start.length > 1) || (!isNumber(end) && end.length > 1)) {
  20626. return invalidRange(start, end, options);
  20627. }
  20628. let format = options.transform || (val => String.fromCharCode(val));
  20629. let a = `${start}`.charCodeAt(0);
  20630. let b = `${end}`.charCodeAt(0);
  20631. let descending = a > b;
  20632. let min = Math.min(a, b);
  20633. let max = Math.max(a, b);
  20634. if (options.toRegex && step === 1) {
  20635. return toRange(min, max, false, options);
  20636. }
  20637. let range = [];
  20638. let index = 0;
  20639. while (descending ? a >= b : a <= b) {
  20640. range.push(format(a, index));
  20641. a = descending ? a - step : a + step;
  20642. index++;
  20643. }
  20644. if (options.toRegex === true) {
  20645. return toRegex(range, null, { wrap: false, options });
  20646. }
  20647. return range;
  20648. };
  20649. const fill$2 = (start, end, step, options = {}) => {
  20650. if (end == null && isValidValue(start)) {
  20651. return [start];
  20652. }
  20653. if (!isValidValue(start) || !isValidValue(end)) {
  20654. return invalidRange(start, end, options);
  20655. }
  20656. if (typeof step === 'function') {
  20657. return fill$2(start, end, 1, { transform: step });
  20658. }
  20659. if (isObject(step)) {
  20660. return fill$2(start, end, 0, step);
  20661. }
  20662. let opts = { ...options };
  20663. if (opts.capture === true) opts.wrap = true;
  20664. step = step || opts.step || 1;
  20665. if (!isNumber(step)) {
  20666. if (step != null && !isObject(step)) return invalidStep(step, opts);
  20667. return fill$2(start, end, 1, step);
  20668. }
  20669. if (isNumber(start) && isNumber(end)) {
  20670. return fillNumbers(start, end, step, opts);
  20671. }
  20672. return fillLetters(start, end, Math.max(Math.abs(step), 1), opts);
  20673. };
  20674. var fillRange = fill$2;
  20675. const fill$1 = fillRange;
  20676. const utils$d = utils$f;
  20677. const compile$1 = (ast, options = {}) => {
  20678. const walk = (node, parent = {}) => {
  20679. const invalidBlock = utils$d.isInvalidBrace(parent);
  20680. const invalidNode = node.invalid === true && options.escapeInvalid === true;
  20681. const invalid = invalidBlock === true || invalidNode === true;
  20682. const prefix = options.escapeInvalid === true ? '\\' : '';
  20683. let output = '';
  20684. if (node.isOpen === true) {
  20685. return prefix + node.value;
  20686. }
  20687. if (node.isClose === true) {
  20688. console.log('node.isClose', prefix, node.value);
  20689. return prefix + node.value;
  20690. }
  20691. if (node.type === 'open') {
  20692. return invalid ? prefix + node.value : '(';
  20693. }
  20694. if (node.type === 'close') {
  20695. return invalid ? prefix + node.value : ')';
  20696. }
  20697. if (node.type === 'comma') {
  20698. return node.prev.type === 'comma' ? '' : invalid ? node.value : '|';
  20699. }
  20700. if (node.value) {
  20701. return node.value;
  20702. }
  20703. if (node.nodes && node.ranges > 0) {
  20704. const args = utils$d.reduce(node.nodes);
  20705. const range = fill$1(...args, { ...options, wrap: false, toRegex: true, strictZeros: true });
  20706. if (range.length !== 0) {
  20707. return args.length > 1 && range.length > 1 ? `(${range})` : range;
  20708. }
  20709. }
  20710. if (node.nodes) {
  20711. for (const child of node.nodes) {
  20712. output += walk(child, node);
  20713. }
  20714. }
  20715. return output;
  20716. };
  20717. return walk(ast);
  20718. };
  20719. var compile_1 = compile$1;
  20720. const fill = fillRange;
  20721. const stringify$5 = stringify$7;
  20722. const utils$c = utils$f;
  20723. const append$1 = (queue = '', stash = '', enclose = false) => {
  20724. const result = [];
  20725. queue = [].concat(queue);
  20726. stash = [].concat(stash);
  20727. if (!stash.length) return queue;
  20728. if (!queue.length) {
  20729. return enclose ? utils$c.flatten(stash).map(ele => `{${ele}}`) : stash;
  20730. }
  20731. for (const item of queue) {
  20732. if (Array.isArray(item)) {
  20733. for (const value of item) {
  20734. result.push(append$1(value, stash, enclose));
  20735. }
  20736. } else {
  20737. for (let ele of stash) {
  20738. if (enclose === true && typeof ele === 'string') ele = `{${ele}}`;
  20739. result.push(Array.isArray(ele) ? append$1(item, ele, enclose) : item + ele);
  20740. }
  20741. }
  20742. }
  20743. return utils$c.flatten(result);
  20744. };
  20745. const expand$2 = (ast, options = {}) => {
  20746. const rangeLimit = options.rangeLimit === undefined ? 1000 : options.rangeLimit;
  20747. const walk = (node, parent = {}) => {
  20748. node.queue = [];
  20749. let p = parent;
  20750. let q = parent.queue;
  20751. while (p.type !== 'brace' && p.type !== 'root' && p.parent) {
  20752. p = p.parent;
  20753. q = p.queue;
  20754. }
  20755. if (node.invalid || node.dollar) {
  20756. q.push(append$1(q.pop(), stringify$5(node, options)));
  20757. return;
  20758. }
  20759. if (node.type === 'brace' && node.invalid !== true && node.nodes.length === 2) {
  20760. q.push(append$1(q.pop(), ['{}']));
  20761. return;
  20762. }
  20763. if (node.nodes && node.ranges > 0) {
  20764. const args = utils$c.reduce(node.nodes);
  20765. if (utils$c.exceedsLimit(...args, options.step, rangeLimit)) {
  20766. throw new RangeError('expanded array length exceeds range limit. Use options.rangeLimit to increase or disable the limit.');
  20767. }
  20768. let range = fill(...args, options);
  20769. if (range.length === 0) {
  20770. range = stringify$5(node, options);
  20771. }
  20772. q.push(append$1(q.pop(), range));
  20773. node.nodes = [];
  20774. return;
  20775. }
  20776. const enclose = utils$c.encloseBrace(node);
  20777. let queue = node.queue;
  20778. let block = node;
  20779. while (block.type !== 'brace' && block.type !== 'root' && block.parent) {
  20780. block = block.parent;
  20781. queue = block.queue;
  20782. }
  20783. for (let i = 0; i < node.nodes.length; i++) {
  20784. const child = node.nodes[i];
  20785. if (child.type === 'comma' && node.type === 'brace') {
  20786. if (i === 1) queue.push('');
  20787. queue.push('');
  20788. continue;
  20789. }
  20790. if (child.type === 'close') {
  20791. q.push(append$1(q.pop(), queue, enclose));
  20792. continue;
  20793. }
  20794. if (child.value && child.type !== 'open') {
  20795. queue.push(append$1(queue.pop(), child.value));
  20796. continue;
  20797. }
  20798. if (child.nodes) {
  20799. walk(child, node);
  20800. }
  20801. }
  20802. return queue;
  20803. };
  20804. return utils$c.flatten(walk(ast));
  20805. };
  20806. var expand_1$1 = expand$2;
  20807. var constants$3 = {
  20808. MAX_LENGTH: 10000,
  20809. // Digits
  20810. CHAR_0: '0', /* 0 */
  20811. CHAR_9: '9', /* 9 */
  20812. // Alphabet chars.
  20813. CHAR_UPPERCASE_A: 'A', /* A */
  20814. CHAR_LOWERCASE_A: 'a', /* a */
  20815. CHAR_UPPERCASE_Z: 'Z', /* Z */
  20816. CHAR_LOWERCASE_Z: 'z', /* z */
  20817. CHAR_LEFT_PARENTHESES: '(', /* ( */
  20818. CHAR_RIGHT_PARENTHESES: ')', /* ) */
  20819. CHAR_ASTERISK: '*', /* * */
  20820. // Non-alphabetic chars.
  20821. CHAR_AMPERSAND: '&', /* & */
  20822. CHAR_AT: '@', /* @ */
  20823. CHAR_BACKSLASH: '\\', /* \ */
  20824. CHAR_BACKTICK: '`', /* ` */
  20825. CHAR_CARRIAGE_RETURN: '\r', /* \r */
  20826. CHAR_CIRCUMFLEX_ACCENT: '^', /* ^ */
  20827. CHAR_COLON: ':', /* : */
  20828. CHAR_COMMA: ',', /* , */
  20829. CHAR_DOLLAR: '$', /* . */
  20830. CHAR_DOT: '.', /* . */
  20831. CHAR_DOUBLE_QUOTE: '"', /* " */
  20832. CHAR_EQUAL: '=', /* = */
  20833. CHAR_EXCLAMATION_MARK: '!', /* ! */
  20834. CHAR_FORM_FEED: '\f', /* \f */
  20835. CHAR_FORWARD_SLASH: '/', /* / */
  20836. CHAR_HASH: '#', /* # */
  20837. CHAR_HYPHEN_MINUS: '-', /* - */
  20838. CHAR_LEFT_ANGLE_BRACKET: '<', /* < */
  20839. CHAR_LEFT_CURLY_BRACE: '{', /* { */
  20840. CHAR_LEFT_SQUARE_BRACKET: '[', /* [ */
  20841. CHAR_LINE_FEED: '\n', /* \n */
  20842. CHAR_NO_BREAK_SPACE: '\u00A0', /* \u00A0 */
  20843. CHAR_PERCENT: '%', /* % */
  20844. CHAR_PLUS: '+', /* + */
  20845. CHAR_QUESTION_MARK: '?', /* ? */
  20846. CHAR_RIGHT_ANGLE_BRACKET: '>', /* > */
  20847. CHAR_RIGHT_CURLY_BRACE: '}', /* } */
  20848. CHAR_RIGHT_SQUARE_BRACKET: ']', /* ] */
  20849. CHAR_SEMICOLON: ';', /* ; */
  20850. CHAR_SINGLE_QUOTE: '\'', /* ' */
  20851. CHAR_SPACE: ' ', /* */
  20852. CHAR_TAB: '\t', /* \t */
  20853. CHAR_UNDERSCORE: '_', /* _ */
  20854. CHAR_VERTICAL_LINE: '|', /* | */
  20855. CHAR_ZERO_WIDTH_NOBREAK_SPACE: '\uFEFF' /* \uFEFF */
  20856. };
  20857. const stringify$4 = stringify$7;
  20858. /**
  20859. * Constants
  20860. */
  20861. const {
  20862. MAX_LENGTH,
  20863. CHAR_BACKSLASH, /* \ */
  20864. CHAR_BACKTICK, /* ` */
  20865. CHAR_COMMA, /* , */
  20866. CHAR_DOT, /* . */
  20867. CHAR_LEFT_PARENTHESES, /* ( */
  20868. CHAR_RIGHT_PARENTHESES, /* ) */
  20869. CHAR_LEFT_CURLY_BRACE, /* { */
  20870. CHAR_RIGHT_CURLY_BRACE, /* } */
  20871. CHAR_LEFT_SQUARE_BRACKET, /* [ */
  20872. CHAR_RIGHT_SQUARE_BRACKET, /* ] */
  20873. CHAR_DOUBLE_QUOTE, /* " */
  20874. CHAR_SINGLE_QUOTE, /* ' */
  20875. CHAR_NO_BREAK_SPACE,
  20876. CHAR_ZERO_WIDTH_NOBREAK_SPACE
  20877. } = constants$3;
  20878. /**
  20879. * parse
  20880. */
  20881. const parse$c = (input, options = {}) => {
  20882. if (typeof input !== 'string') {
  20883. throw new TypeError('Expected a string');
  20884. }
  20885. const opts = options || {};
  20886. const max = typeof opts.maxLength === 'number' ? Math.min(MAX_LENGTH, opts.maxLength) : MAX_LENGTH;
  20887. if (input.length > max) {
  20888. throw new SyntaxError(`Input length (${input.length}), exceeds max characters (${max})`);
  20889. }
  20890. const ast = { type: 'root', input, nodes: [] };
  20891. const stack = [ast];
  20892. let block = ast;
  20893. let prev = ast;
  20894. let brackets = 0;
  20895. const length = input.length;
  20896. let index = 0;
  20897. let depth = 0;
  20898. let value;
  20899. /**
  20900. * Helpers
  20901. */
  20902. const advance = () => input[index++];
  20903. const push = node => {
  20904. if (node.type === 'text' && prev.type === 'dot') {
  20905. prev.type = 'text';
  20906. }
  20907. if (prev && prev.type === 'text' && node.type === 'text') {
  20908. prev.value += node.value;
  20909. return;
  20910. }
  20911. block.nodes.push(node);
  20912. node.parent = block;
  20913. node.prev = prev;
  20914. prev = node;
  20915. return node;
  20916. };
  20917. push({ type: 'bos' });
  20918. while (index < length) {
  20919. block = stack[stack.length - 1];
  20920. value = advance();
  20921. /**
  20922. * Invalid chars
  20923. */
  20924. if (value === CHAR_ZERO_WIDTH_NOBREAK_SPACE || value === CHAR_NO_BREAK_SPACE) {
  20925. continue;
  20926. }
  20927. /**
  20928. * Escaped chars
  20929. */
  20930. if (value === CHAR_BACKSLASH) {
  20931. push({ type: 'text', value: (options.keepEscaping ? value : '') + advance() });
  20932. continue;
  20933. }
  20934. /**
  20935. * Right square bracket (literal): ']'
  20936. */
  20937. if (value === CHAR_RIGHT_SQUARE_BRACKET) {
  20938. push({ type: 'text', value: '\\' + value });
  20939. continue;
  20940. }
  20941. /**
  20942. * Left square bracket: '['
  20943. */
  20944. if (value === CHAR_LEFT_SQUARE_BRACKET) {
  20945. brackets++;
  20946. let next;
  20947. while (index < length && (next = advance())) {
  20948. value += next;
  20949. if (next === CHAR_LEFT_SQUARE_BRACKET) {
  20950. brackets++;
  20951. continue;
  20952. }
  20953. if (next === CHAR_BACKSLASH) {
  20954. value += advance();
  20955. continue;
  20956. }
  20957. if (next === CHAR_RIGHT_SQUARE_BRACKET) {
  20958. brackets--;
  20959. if (brackets === 0) {
  20960. break;
  20961. }
  20962. }
  20963. }
  20964. push({ type: 'text', value });
  20965. continue;
  20966. }
  20967. /**
  20968. * Parentheses
  20969. */
  20970. if (value === CHAR_LEFT_PARENTHESES) {
  20971. block = push({ type: 'paren', nodes: [] });
  20972. stack.push(block);
  20973. push({ type: 'text', value });
  20974. continue;
  20975. }
  20976. if (value === CHAR_RIGHT_PARENTHESES) {
  20977. if (block.type !== 'paren') {
  20978. push({ type: 'text', value });
  20979. continue;
  20980. }
  20981. block = stack.pop();
  20982. push({ type: 'text', value });
  20983. block = stack[stack.length - 1];
  20984. continue;
  20985. }
  20986. /**
  20987. * Quotes: '|"|`
  20988. */
  20989. if (value === CHAR_DOUBLE_QUOTE || value === CHAR_SINGLE_QUOTE || value === CHAR_BACKTICK) {
  20990. const open = value;
  20991. let next;
  20992. if (options.keepQuotes !== true) {
  20993. value = '';
  20994. }
  20995. while (index < length && (next = advance())) {
  20996. if (next === CHAR_BACKSLASH) {
  20997. value += next + advance();
  20998. continue;
  20999. }
  21000. if (next === open) {
  21001. if (options.keepQuotes === true) value += next;
  21002. break;
  21003. }
  21004. value += next;
  21005. }
  21006. push({ type: 'text', value });
  21007. continue;
  21008. }
  21009. /**
  21010. * Left curly brace: '{'
  21011. */
  21012. if (value === CHAR_LEFT_CURLY_BRACE) {
  21013. depth++;
  21014. const dollar = prev.value && prev.value.slice(-1) === '$' || block.dollar === true;
  21015. const brace = {
  21016. type: 'brace',
  21017. open: true,
  21018. close: false,
  21019. dollar,
  21020. depth,
  21021. commas: 0,
  21022. ranges: 0,
  21023. nodes: []
  21024. };
  21025. block = push(brace);
  21026. stack.push(block);
  21027. push({ type: 'open', value });
  21028. continue;
  21029. }
  21030. /**
  21031. * Right curly brace: '}'
  21032. */
  21033. if (value === CHAR_RIGHT_CURLY_BRACE) {
  21034. if (block.type !== 'brace') {
  21035. push({ type: 'text', value });
  21036. continue;
  21037. }
  21038. const type = 'close';
  21039. block = stack.pop();
  21040. block.close = true;
  21041. push({ type, value });
  21042. depth--;
  21043. block = stack[stack.length - 1];
  21044. continue;
  21045. }
  21046. /**
  21047. * Comma: ','
  21048. */
  21049. if (value === CHAR_COMMA && depth > 0) {
  21050. if (block.ranges > 0) {
  21051. block.ranges = 0;
  21052. const open = block.nodes.shift();
  21053. block.nodes = [open, { type: 'text', value: stringify$4(block) }];
  21054. }
  21055. push({ type: 'comma', value });
  21056. block.commas++;
  21057. continue;
  21058. }
  21059. /**
  21060. * Dot: '.'
  21061. */
  21062. if (value === CHAR_DOT && depth > 0 && block.commas === 0) {
  21063. const siblings = block.nodes;
  21064. if (depth === 0 || siblings.length === 0) {
  21065. push({ type: 'text', value });
  21066. continue;
  21067. }
  21068. if (prev.type === 'dot') {
  21069. block.range = [];
  21070. prev.value += value;
  21071. prev.type = 'range';
  21072. if (block.nodes.length !== 3 && block.nodes.length !== 5) {
  21073. block.invalid = true;
  21074. block.ranges = 0;
  21075. prev.type = 'text';
  21076. continue;
  21077. }
  21078. block.ranges++;
  21079. block.args = [];
  21080. continue;
  21081. }
  21082. if (prev.type === 'range') {
  21083. siblings.pop();
  21084. const before = siblings[siblings.length - 1];
  21085. before.value += prev.value + value;
  21086. prev = before;
  21087. block.ranges--;
  21088. continue;
  21089. }
  21090. push({ type: 'dot', value });
  21091. continue;
  21092. }
  21093. /**
  21094. * Text
  21095. */
  21096. push({ type: 'text', value });
  21097. }
  21098. // Mark imbalanced braces and brackets as invalid
  21099. do {
  21100. block = stack.pop();
  21101. if (block.type !== 'root') {
  21102. block.nodes.forEach(node => {
  21103. if (!node.nodes) {
  21104. if (node.type === 'open') node.isOpen = true;
  21105. if (node.type === 'close') node.isClose = true;
  21106. if (!node.nodes) node.type = 'text';
  21107. node.invalid = true;
  21108. }
  21109. });
  21110. // get the location of the block on parent.nodes (block's siblings)
  21111. const parent = stack[stack.length - 1];
  21112. const index = parent.nodes.indexOf(block);
  21113. // replace the (invalid) block with it's nodes
  21114. parent.nodes.splice(index, 1, ...block.nodes);
  21115. }
  21116. } while (stack.length > 0);
  21117. push({ type: 'eos' });
  21118. return ast;
  21119. };
  21120. var parse_1$2 = parse$c;
  21121. const stringify$3 = stringify$7;
  21122. const compile = compile_1;
  21123. const expand$1 = expand_1$1;
  21124. const parse$b = parse_1$2;
  21125. /**
  21126. * Expand the given pattern or create a regex-compatible string.
  21127. *
  21128. * ```js
  21129. * const braces = require('braces');
  21130. * console.log(braces('{a,b,c}', { compile: true })); //=> ['(a|b|c)']
  21131. * console.log(braces('{a,b,c}')); //=> ['a', 'b', 'c']
  21132. * ```
  21133. * @param {String} `str`
  21134. * @param {Object} `options`
  21135. * @return {String}
  21136. * @api public
  21137. */
  21138. const braces$2 = (input, options = {}) => {
  21139. let output = [];
  21140. if (Array.isArray(input)) {
  21141. for (const pattern of input) {
  21142. const result = braces$2.create(pattern, options);
  21143. if (Array.isArray(result)) {
  21144. output.push(...result);
  21145. } else {
  21146. output.push(result);
  21147. }
  21148. }
  21149. } else {
  21150. output = [].concat(braces$2.create(input, options));
  21151. }
  21152. if (options && options.expand === true && options.nodupes === true) {
  21153. output = [...new Set(output)];
  21154. }
  21155. return output;
  21156. };
  21157. /**
  21158. * Parse the given `str` with the given `options`.
  21159. *
  21160. * ```js
  21161. * // braces.parse(pattern, [, options]);
  21162. * const ast = braces.parse('a/{b,c}/d');
  21163. * console.log(ast);
  21164. * ```
  21165. * @param {String} pattern Brace pattern to parse
  21166. * @param {Object} options
  21167. * @return {Object} Returns an AST
  21168. * @api public
  21169. */
  21170. braces$2.parse = (input, options = {}) => parse$b(input, options);
  21171. /**
  21172. * Creates a braces string from an AST, or an AST node.
  21173. *
  21174. * ```js
  21175. * const braces = require('braces');
  21176. * let ast = braces.parse('foo/{a,b}/bar');
  21177. * console.log(stringify(ast.nodes[2])); //=> '{a,b}'
  21178. * ```
  21179. * @param {String} `input` Brace pattern or AST.
  21180. * @param {Object} `options`
  21181. * @return {Array} Returns an array of expanded values.
  21182. * @api public
  21183. */
  21184. braces$2.stringify = (input, options = {}) => {
  21185. if (typeof input === 'string') {
  21186. return stringify$3(braces$2.parse(input, options), options);
  21187. }
  21188. return stringify$3(input, options);
  21189. };
  21190. /**
  21191. * Compiles a brace pattern into a regex-compatible, optimized string.
  21192. * This method is called by the main [braces](#braces) function by default.
  21193. *
  21194. * ```js
  21195. * const braces = require('braces');
  21196. * console.log(braces.compile('a/{b,c}/d'));
  21197. * //=> ['a/(b|c)/d']
  21198. * ```
  21199. * @param {String} `input` Brace pattern or AST.
  21200. * @param {Object} `options`
  21201. * @return {Array} Returns an array of expanded values.
  21202. * @api public
  21203. */
  21204. braces$2.compile = (input, options = {}) => {
  21205. if (typeof input === 'string') {
  21206. input = braces$2.parse(input, options);
  21207. }
  21208. return compile(input, options);
  21209. };
  21210. /**
  21211. * Expands a brace pattern into an array. This method is called by the
  21212. * main [braces](#braces) function when `options.expand` is true. Before
  21213. * using this method it's recommended that you read the [performance notes](#performance))
  21214. * and advantages of using [.compile](#compile) instead.
  21215. *
  21216. * ```js
  21217. * const braces = require('braces');
  21218. * console.log(braces.expand('a/{b,c}/d'));
  21219. * //=> ['a/b/d', 'a/c/d'];
  21220. * ```
  21221. * @param {String} `pattern` Brace pattern
  21222. * @param {Object} `options`
  21223. * @return {Array} Returns an array of expanded values.
  21224. * @api public
  21225. */
  21226. braces$2.expand = (input, options = {}) => {
  21227. if (typeof input === 'string') {
  21228. input = braces$2.parse(input, options);
  21229. }
  21230. let result = expand$1(input, options);
  21231. // filter out empty strings if specified
  21232. if (options.noempty === true) {
  21233. result = result.filter(Boolean);
  21234. }
  21235. // filter out duplicates if specified
  21236. if (options.nodupes === true) {
  21237. result = [...new Set(result)];
  21238. }
  21239. return result;
  21240. };
  21241. /**
  21242. * Processes a brace pattern and returns either an expanded array
  21243. * (if `options.expand` is true), a highly optimized regex-compatible string.
  21244. * This method is called by the main [braces](#braces) function.
  21245. *
  21246. * ```js
  21247. * const braces = require('braces');
  21248. * console.log(braces.create('user-{200..300}/project-{a,b,c}-{1..10}'))
  21249. * //=> 'user-(20[0-9]|2[1-9][0-9]|300)/project-(a|b|c)-([1-9]|10)'
  21250. * ```
  21251. * @param {String} `pattern` Brace pattern
  21252. * @param {Object} `options`
  21253. * @return {Array} Returns an array of expanded values.
  21254. * @api public
  21255. */
  21256. braces$2.create = (input, options = {}) => {
  21257. if (input === '' || input.length < 3) {
  21258. return [input];
  21259. }
  21260. return options.expand !== true
  21261. ? braces$2.compile(input, options)
  21262. : braces$2.expand(input, options);
  21263. };
  21264. /**
  21265. * Expose "braces"
  21266. */
  21267. var braces_1 = braces$2;
  21268. const util = require$$0$5;
  21269. const braces$1 = braces_1;
  21270. const picomatch$2 = picomatch$3;
  21271. const utils$b = utils$k;
  21272. const isEmptyString = v => v === '' || v === './';
  21273. const hasBraces = v => {
  21274. const index = v.indexOf('{');
  21275. return index > -1 && v.indexOf('}', index) > -1;
  21276. };
  21277. /**
  21278. * Returns an array of strings that match one or more glob patterns.
  21279. *
  21280. * ```js
  21281. * const mm = require('micromatch');
  21282. * // mm(list, patterns[, options]);
  21283. *
  21284. * console.log(mm(['a.js', 'a.txt'], ['*.js']));
  21285. * //=> [ 'a.js' ]
  21286. * ```
  21287. * @param {String|Array<string>} `list` List of strings to match.
  21288. * @param {String|Array<string>} `patterns` One or more glob patterns to use for matching.
  21289. * @param {Object} `options` See available [options](#options)
  21290. * @return {Array} Returns an array of matches
  21291. * @summary false
  21292. * @api public
  21293. */
  21294. const micromatch$1 = (list, patterns, options) => {
  21295. patterns = [].concat(patterns);
  21296. list = [].concat(list);
  21297. let omit = new Set();
  21298. let keep = new Set();
  21299. let items = new Set();
  21300. let negatives = 0;
  21301. let onResult = state => {
  21302. items.add(state.output);
  21303. if (options && options.onResult) {
  21304. options.onResult(state);
  21305. }
  21306. };
  21307. for (let i = 0; i < patterns.length; i++) {
  21308. let isMatch = picomatch$2(String(patterns[i]), { ...options, onResult }, true);
  21309. let negated = isMatch.state.negated || isMatch.state.negatedExtglob;
  21310. if (negated) negatives++;
  21311. for (let item of list) {
  21312. let matched = isMatch(item, true);
  21313. let match = negated ? !matched.isMatch : matched.isMatch;
  21314. if (!match) continue;
  21315. if (negated) {
  21316. omit.add(matched.output);
  21317. } else {
  21318. omit.delete(matched.output);
  21319. keep.add(matched.output);
  21320. }
  21321. }
  21322. }
  21323. let result = negatives === patterns.length ? [...items] : [...keep];
  21324. let matches = result.filter(item => !omit.has(item));
  21325. if (options && matches.length === 0) {
  21326. if (options.failglob === true) {
  21327. throw new Error(`No matches found for "${patterns.join(', ')}"`);
  21328. }
  21329. if (options.nonull === true || options.nullglob === true) {
  21330. return options.unescape ? patterns.map(p => p.replace(/\\/g, '')) : patterns;
  21331. }
  21332. }
  21333. return matches;
  21334. };
  21335. /**
  21336. * Backwards compatibility
  21337. */
  21338. micromatch$1.match = micromatch$1;
  21339. /**
  21340. * Returns a matcher function from the given glob `pattern` and `options`.
  21341. * The returned function takes a string to match as its only argument and returns
  21342. * true if the string is a match.
  21343. *
  21344. * ```js
  21345. * const mm = require('micromatch');
  21346. * // mm.matcher(pattern[, options]);
  21347. *
  21348. * const isMatch = mm.matcher('*.!(*a)');
  21349. * console.log(isMatch('a.a')); //=> false
  21350. * console.log(isMatch('a.b')); //=> true
  21351. * ```
  21352. * @param {String} `pattern` Glob pattern
  21353. * @param {Object} `options`
  21354. * @return {Function} Returns a matcher function.
  21355. * @api public
  21356. */
  21357. micromatch$1.matcher = (pattern, options) => picomatch$2(pattern, options);
  21358. /**
  21359. * Returns true if **any** of the given glob `patterns` match the specified `string`.
  21360. *
  21361. * ```js
  21362. * const mm = require('micromatch');
  21363. * // mm.isMatch(string, patterns[, options]);
  21364. *
  21365. * console.log(mm.isMatch('a.a', ['b.*', '*.a'])); //=> true
  21366. * console.log(mm.isMatch('a.a', 'b.*')); //=> false
  21367. * ```
  21368. * @param {String} `str` The string to test.
  21369. * @param {String|Array} `patterns` One or more glob patterns to use for matching.
  21370. * @param {Object} `[options]` See available [options](#options).
  21371. * @return {Boolean} Returns true if any patterns match `str`
  21372. * @api public
  21373. */
  21374. micromatch$1.isMatch = (str, patterns, options) => picomatch$2(patterns, options)(str);
  21375. /**
  21376. * Backwards compatibility
  21377. */
  21378. micromatch$1.any = micromatch$1.isMatch;
  21379. /**
  21380. * Returns a list of strings that _**do not match any**_ of the given `patterns`.
  21381. *
  21382. * ```js
  21383. * const mm = require('micromatch');
  21384. * // mm.not(list, patterns[, options]);
  21385. *
  21386. * console.log(mm.not(['a.a', 'b.b', 'c.c'], '*.a'));
  21387. * //=> ['b.b', 'c.c']
  21388. * ```
  21389. * @param {Array} `list` Array of strings to match.
  21390. * @param {String|Array} `patterns` One or more glob pattern to use for matching.
  21391. * @param {Object} `options` See available [options](#options) for changing how matches are performed
  21392. * @return {Array} Returns an array of strings that **do not match** the given patterns.
  21393. * @api public
  21394. */
  21395. micromatch$1.not = (list, patterns, options = {}) => {
  21396. patterns = [].concat(patterns).map(String);
  21397. let result = new Set();
  21398. let items = [];
  21399. let onResult = state => {
  21400. if (options.onResult) options.onResult(state);
  21401. items.push(state.output);
  21402. };
  21403. let matches = new Set(micromatch$1(list, patterns, { ...options, onResult }));
  21404. for (let item of items) {
  21405. if (!matches.has(item)) {
  21406. result.add(item);
  21407. }
  21408. }
  21409. return [...result];
  21410. };
  21411. /**
  21412. * Returns true if the given `string` contains the given pattern. Similar
  21413. * to [.isMatch](#isMatch) but the pattern can match any part of the string.
  21414. *
  21415. * ```js
  21416. * var mm = require('micromatch');
  21417. * // mm.contains(string, pattern[, options]);
  21418. *
  21419. * console.log(mm.contains('aa/bb/cc', '*b'));
  21420. * //=> true
  21421. * console.log(mm.contains('aa/bb/cc', '*d'));
  21422. * //=> false
  21423. * ```
  21424. * @param {String} `str` The string to match.
  21425. * @param {String|Array} `patterns` Glob pattern to use for matching.
  21426. * @param {Object} `options` See available [options](#options) for changing how matches are performed
  21427. * @return {Boolean} Returns true if any of the patterns matches any part of `str`.
  21428. * @api public
  21429. */
  21430. micromatch$1.contains = (str, pattern, options) => {
  21431. if (typeof str !== 'string') {
  21432. throw new TypeError(`Expected a string: "${util.inspect(str)}"`);
  21433. }
  21434. if (Array.isArray(pattern)) {
  21435. return pattern.some(p => micromatch$1.contains(str, p, options));
  21436. }
  21437. if (typeof pattern === 'string') {
  21438. if (isEmptyString(str) || isEmptyString(pattern)) {
  21439. return false;
  21440. }
  21441. if (str.includes(pattern) || (str.startsWith('./') && str.slice(2).includes(pattern))) {
  21442. return true;
  21443. }
  21444. }
  21445. return micromatch$1.isMatch(str, pattern, { ...options, contains: true });
  21446. };
  21447. /**
  21448. * Filter the keys of the given object with the given `glob` pattern
  21449. * and `options`. Does not attempt to match nested keys. If you need this feature,
  21450. * use [glob-object][] instead.
  21451. *
  21452. * ```js
  21453. * const mm = require('micromatch');
  21454. * // mm.matchKeys(object, patterns[, options]);
  21455. *
  21456. * const obj = { aa: 'a', ab: 'b', ac: 'c' };
  21457. * console.log(mm.matchKeys(obj, '*b'));
  21458. * //=> { ab: 'b' }
  21459. * ```
  21460. * @param {Object} `object` The object with keys to filter.
  21461. * @param {String|Array} `patterns` One or more glob patterns to use for matching.
  21462. * @param {Object} `options` See available [options](#options) for changing how matches are performed
  21463. * @return {Object} Returns an object with only keys that match the given patterns.
  21464. * @api public
  21465. */
  21466. micromatch$1.matchKeys = (obj, patterns, options) => {
  21467. if (!utils$b.isObject(obj)) {
  21468. throw new TypeError('Expected the first argument to be an object');
  21469. }
  21470. let keys = micromatch$1(Object.keys(obj), patterns, options);
  21471. let res = {};
  21472. for (let key of keys) res[key] = obj[key];
  21473. return res;
  21474. };
  21475. /**
  21476. * Returns true if some of the strings in the given `list` match any of the given glob `patterns`.
  21477. *
  21478. * ```js
  21479. * const mm = require('micromatch');
  21480. * // mm.some(list, patterns[, options]);
  21481. *
  21482. * console.log(mm.some(['foo.js', 'bar.js'], ['*.js', '!foo.js']));
  21483. * // true
  21484. * console.log(mm.some(['foo.js'], ['*.js', '!foo.js']));
  21485. * // false
  21486. * ```
  21487. * @param {String|Array} `list` The string or array of strings to test. Returns as soon as the first match is found.
  21488. * @param {String|Array} `patterns` One or more glob patterns to use for matching.
  21489. * @param {Object} `options` See available [options](#options) for changing how matches are performed
  21490. * @return {Boolean} Returns true if any `patterns` matches any of the strings in `list`
  21491. * @api public
  21492. */
  21493. micromatch$1.some = (list, patterns, options) => {
  21494. let items = [].concat(list);
  21495. for (let pattern of [].concat(patterns)) {
  21496. let isMatch = picomatch$2(String(pattern), options);
  21497. if (items.some(item => isMatch(item))) {
  21498. return true;
  21499. }
  21500. }
  21501. return false;
  21502. };
  21503. /**
  21504. * Returns true if every string in the given `list` matches
  21505. * any of the given glob `patterns`.
  21506. *
  21507. * ```js
  21508. * const mm = require('micromatch');
  21509. * // mm.every(list, patterns[, options]);
  21510. *
  21511. * console.log(mm.every('foo.js', ['foo.js']));
  21512. * // true
  21513. * console.log(mm.every(['foo.js', 'bar.js'], ['*.js']));
  21514. * // true
  21515. * console.log(mm.every(['foo.js', 'bar.js'], ['*.js', '!foo.js']));
  21516. * // false
  21517. * console.log(mm.every(['foo.js'], ['*.js', '!foo.js']));
  21518. * // false
  21519. * ```
  21520. * @param {String|Array} `list` The string or array of strings to test.
  21521. * @param {String|Array} `patterns` One or more glob patterns to use for matching.
  21522. * @param {Object} `options` See available [options](#options) for changing how matches are performed
  21523. * @return {Boolean} Returns true if all `patterns` matches all of the strings in `list`
  21524. * @api public
  21525. */
  21526. micromatch$1.every = (list, patterns, options) => {
  21527. let items = [].concat(list);
  21528. for (let pattern of [].concat(patterns)) {
  21529. let isMatch = picomatch$2(String(pattern), options);
  21530. if (!items.every(item => isMatch(item))) {
  21531. return false;
  21532. }
  21533. }
  21534. return true;
  21535. };
  21536. /**
  21537. * Returns true if **all** of the given `patterns` match
  21538. * the specified string.
  21539. *
  21540. * ```js
  21541. * const mm = require('micromatch');
  21542. * // mm.all(string, patterns[, options]);
  21543. *
  21544. * console.log(mm.all('foo.js', ['foo.js']));
  21545. * // true
  21546. *
  21547. * console.log(mm.all('foo.js', ['*.js', '!foo.js']));
  21548. * // false
  21549. *
  21550. * console.log(mm.all('foo.js', ['*.js', 'foo.js']));
  21551. * // true
  21552. *
  21553. * console.log(mm.all('foo.js', ['*.js', 'f*', '*o*', '*o.js']));
  21554. * // true
  21555. * ```
  21556. * @param {String|Array} `str` The string to test.
  21557. * @param {String|Array} `patterns` One or more glob patterns to use for matching.
  21558. * @param {Object} `options` See available [options](#options) for changing how matches are performed
  21559. * @return {Boolean} Returns true if any patterns match `str`
  21560. * @api public
  21561. */
  21562. micromatch$1.all = (str, patterns, options) => {
  21563. if (typeof str !== 'string') {
  21564. throw new TypeError(`Expected a string: "${util.inspect(str)}"`);
  21565. }
  21566. return [].concat(patterns).every(p => picomatch$2(p, options)(str));
  21567. };
  21568. /**
  21569. * Returns an array of matches captured by `pattern` in `string, or `null` if the pattern did not match.
  21570. *
  21571. * ```js
  21572. * const mm = require('micromatch');
  21573. * // mm.capture(pattern, string[, options]);
  21574. *
  21575. * console.log(mm.capture('test/*.js', 'test/foo.js'));
  21576. * //=> ['foo']
  21577. * console.log(mm.capture('test/*.js', 'foo/bar.css'));
  21578. * //=> null
  21579. * ```
  21580. * @param {String} `glob` Glob pattern to use for matching.
  21581. * @param {String} `input` String to match
  21582. * @param {Object} `options` See available [options](#options) for changing how matches are performed
  21583. * @return {Array|null} Returns an array of captures if the input matches the glob pattern, otherwise `null`.
  21584. * @api public
  21585. */
  21586. micromatch$1.capture = (glob, input, options) => {
  21587. let posix = utils$b.isWindows(options);
  21588. let regex = picomatch$2.makeRe(String(glob), { ...options, capture: true });
  21589. let match = regex.exec(posix ? utils$b.toPosixSlashes(input) : input);
  21590. if (match) {
  21591. return match.slice(1).map(v => v === void 0 ? '' : v);
  21592. }
  21593. };
  21594. /**
  21595. * Create a regular expression from the given glob `pattern`.
  21596. *
  21597. * ```js
  21598. * const mm = require('micromatch');
  21599. * // mm.makeRe(pattern[, options]);
  21600. *
  21601. * console.log(mm.makeRe('*.js'));
  21602. * //=> /^(?:(\.[\\\/])?(?!\.)(?=.)[^\/]*?\.js)$/
  21603. * ```
  21604. * @param {String} `pattern` A glob pattern to convert to regex.
  21605. * @param {Object} `options`
  21606. * @return {RegExp} Returns a regex created from the given pattern.
  21607. * @api public
  21608. */
  21609. micromatch$1.makeRe = (...args) => picomatch$2.makeRe(...args);
  21610. /**
  21611. * Scan a glob pattern to separate the pattern into segments. Used
  21612. * by the [split](#split) method.
  21613. *
  21614. * ```js
  21615. * const mm = require('micromatch');
  21616. * const state = mm.scan(pattern[, options]);
  21617. * ```
  21618. * @param {String} `pattern`
  21619. * @param {Object} `options`
  21620. * @return {Object} Returns an object with
  21621. * @api public
  21622. */
  21623. micromatch$1.scan = (...args) => picomatch$2.scan(...args);
  21624. /**
  21625. * Parse a glob pattern to create the source string for a regular
  21626. * expression.
  21627. *
  21628. * ```js
  21629. * const mm = require('micromatch');
  21630. * const state = mm.parse(pattern[, options]);
  21631. * ```
  21632. * @param {String} `glob`
  21633. * @param {Object} `options`
  21634. * @return {Object} Returns an object with useful properties and output to be used as regex source string.
  21635. * @api public
  21636. */
  21637. micromatch$1.parse = (patterns, options) => {
  21638. let res = [];
  21639. for (let pattern of [].concat(patterns || [])) {
  21640. for (let str of braces$1(String(pattern), options)) {
  21641. res.push(picomatch$2.parse(str, options));
  21642. }
  21643. }
  21644. return res;
  21645. };
  21646. /**
  21647. * Process the given brace `pattern`.
  21648. *
  21649. * ```js
  21650. * const { braces } = require('micromatch');
  21651. * console.log(braces('foo/{a,b,c}/bar'));
  21652. * //=> [ 'foo/(a|b|c)/bar' ]
  21653. *
  21654. * console.log(braces('foo/{a,b,c}/bar', { expand: true }));
  21655. * //=> [ 'foo/a/bar', 'foo/b/bar', 'foo/c/bar' ]
  21656. * ```
  21657. * @param {String} `pattern` String with brace pattern to process.
  21658. * @param {Object} `options` Any [options](#options) to change how expansion is performed. See the [braces][] library for all available options.
  21659. * @return {Array}
  21660. * @api public
  21661. */
  21662. micromatch$1.braces = (pattern, options) => {
  21663. if (typeof pattern !== 'string') throw new TypeError('Expected a string');
  21664. if ((options && options.nobrace === true) || !hasBraces(pattern)) {
  21665. return [pattern];
  21666. }
  21667. return braces$1(pattern, options);
  21668. };
  21669. /**
  21670. * Expand braces
  21671. */
  21672. micromatch$1.braceExpand = (pattern, options) => {
  21673. if (typeof pattern !== 'string') throw new TypeError('Expected a string');
  21674. return micromatch$1.braces(pattern, { ...options, expand: true });
  21675. };
  21676. /**
  21677. * Expose micromatch
  21678. */
  21679. // exposed for tests
  21680. micromatch$1.hasBraces = hasBraces;
  21681. var micromatch_1 = micromatch$1;
  21682. var micromatch$2 = /*@__PURE__*/getDefaultExportFromCjs(micromatch_1);
  21683. Object.defineProperty(pattern$1, "__esModule", { value: true });
  21684. 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;
  21685. const path$g = require$$0$4;
  21686. const globParent$1 = globParent$2;
  21687. const micromatch = micromatch_1;
  21688. const GLOBSTAR$1 = '**';
  21689. const ESCAPE_SYMBOL = '\\';
  21690. const COMMON_GLOB_SYMBOLS_RE = /[*?]|^!/;
  21691. const REGEX_CHARACTER_CLASS_SYMBOLS_RE = /\[[^[]*]/;
  21692. const REGEX_GROUP_SYMBOLS_RE = /(?:^|[^!*+?@])\([^(]*\|[^|]*\)/;
  21693. const GLOB_EXTENSION_SYMBOLS_RE = /[!*+?@]\([^(]*\)/;
  21694. const BRACE_EXPANSION_SEPARATORS_RE = /,|\.\./;
  21695. /**
  21696. * Matches a sequence of two or more consecutive slashes, excluding the first two slashes at the beginning of the string.
  21697. * The latter is due to the presence of the device path at the beginning of the UNC path.
  21698. */
  21699. const DOUBLE_SLASH_RE$1 = /(?!^)\/{2,}/g;
  21700. function isStaticPattern(pattern, options = {}) {
  21701. return !isDynamicPattern(pattern, options);
  21702. }
  21703. pattern$1.isStaticPattern = isStaticPattern;
  21704. function isDynamicPattern(pattern, options = {}) {
  21705. /**
  21706. * A special case with an empty string is necessary for matching patterns that start with a forward slash.
  21707. * An empty string cannot be a dynamic pattern.
  21708. * For example, the pattern `/lib/*` will be spread into parts: '', 'lib', '*'.
  21709. */
  21710. if (pattern === '') {
  21711. return false;
  21712. }
  21713. /**
  21714. * When the `caseSensitiveMatch` option is disabled, all patterns must be marked as dynamic, because we cannot check
  21715. * filepath directly (without read directory).
  21716. */
  21717. if (options.caseSensitiveMatch === false || pattern.includes(ESCAPE_SYMBOL)) {
  21718. return true;
  21719. }
  21720. if (COMMON_GLOB_SYMBOLS_RE.test(pattern) || REGEX_CHARACTER_CLASS_SYMBOLS_RE.test(pattern) || REGEX_GROUP_SYMBOLS_RE.test(pattern)) {
  21721. return true;
  21722. }
  21723. if (options.extglob !== false && GLOB_EXTENSION_SYMBOLS_RE.test(pattern)) {
  21724. return true;
  21725. }
  21726. if (options.braceExpansion !== false && hasBraceExpansion(pattern)) {
  21727. return true;
  21728. }
  21729. return false;
  21730. }
  21731. pattern$1.isDynamicPattern = isDynamicPattern;
  21732. function hasBraceExpansion(pattern) {
  21733. const openingBraceIndex = pattern.indexOf('{');
  21734. if (openingBraceIndex === -1) {
  21735. return false;
  21736. }
  21737. const closingBraceIndex = pattern.indexOf('}', openingBraceIndex + 1);
  21738. if (closingBraceIndex === -1) {
  21739. return false;
  21740. }
  21741. const braceContent = pattern.slice(openingBraceIndex, closingBraceIndex);
  21742. return BRACE_EXPANSION_SEPARATORS_RE.test(braceContent);
  21743. }
  21744. function convertToPositivePattern(pattern) {
  21745. return isNegativePattern(pattern) ? pattern.slice(1) : pattern;
  21746. }
  21747. pattern$1.convertToPositivePattern = convertToPositivePattern;
  21748. function convertToNegativePattern(pattern) {
  21749. return '!' + pattern;
  21750. }
  21751. pattern$1.convertToNegativePattern = convertToNegativePattern;
  21752. function isNegativePattern(pattern) {
  21753. return pattern.startsWith('!') && pattern[1] !== '(';
  21754. }
  21755. pattern$1.isNegativePattern = isNegativePattern;
  21756. function isPositivePattern(pattern) {
  21757. return !isNegativePattern(pattern);
  21758. }
  21759. pattern$1.isPositivePattern = isPositivePattern;
  21760. function getNegativePatterns(patterns) {
  21761. return patterns.filter(isNegativePattern);
  21762. }
  21763. pattern$1.getNegativePatterns = getNegativePatterns;
  21764. function getPositivePatterns$1(patterns) {
  21765. return patterns.filter(isPositivePattern);
  21766. }
  21767. pattern$1.getPositivePatterns = getPositivePatterns$1;
  21768. /**
  21769. * Returns patterns that can be applied inside the current directory.
  21770. *
  21771. * @example
  21772. * // ['./*', '*', 'a/*']
  21773. * getPatternsInsideCurrentDirectory(['./*', '*', 'a/*', '../*', './../*'])
  21774. */
  21775. function getPatternsInsideCurrentDirectory(patterns) {
  21776. return patterns.filter((pattern) => !isPatternRelatedToParentDirectory(pattern));
  21777. }
  21778. pattern$1.getPatternsInsideCurrentDirectory = getPatternsInsideCurrentDirectory;
  21779. /**
  21780. * Returns patterns to be expanded relative to (outside) the current directory.
  21781. *
  21782. * @example
  21783. * // ['../*', './../*']
  21784. * getPatternsInsideCurrentDirectory(['./*', '*', 'a/*', '../*', './../*'])
  21785. */
  21786. function getPatternsOutsideCurrentDirectory(patterns) {
  21787. return patterns.filter(isPatternRelatedToParentDirectory);
  21788. }
  21789. pattern$1.getPatternsOutsideCurrentDirectory = getPatternsOutsideCurrentDirectory;
  21790. function isPatternRelatedToParentDirectory(pattern) {
  21791. return pattern.startsWith('..') || pattern.startsWith('./..');
  21792. }
  21793. pattern$1.isPatternRelatedToParentDirectory = isPatternRelatedToParentDirectory;
  21794. function getBaseDirectory(pattern) {
  21795. return globParent$1(pattern, { flipBackslashes: false });
  21796. }
  21797. pattern$1.getBaseDirectory = getBaseDirectory;
  21798. function hasGlobStar(pattern) {
  21799. return pattern.includes(GLOBSTAR$1);
  21800. }
  21801. pattern$1.hasGlobStar = hasGlobStar;
  21802. function endsWithSlashGlobStar(pattern) {
  21803. return pattern.endsWith('/' + GLOBSTAR$1);
  21804. }
  21805. pattern$1.endsWithSlashGlobStar = endsWithSlashGlobStar;
  21806. function isAffectDepthOfReadingPattern(pattern) {
  21807. const basename = path$g.basename(pattern);
  21808. return endsWithSlashGlobStar(pattern) || isStaticPattern(basename);
  21809. }
  21810. pattern$1.isAffectDepthOfReadingPattern = isAffectDepthOfReadingPattern;
  21811. function expandPatternsWithBraceExpansion(patterns) {
  21812. return patterns.reduce((collection, pattern) => {
  21813. return collection.concat(expandBraceExpansion(pattern));
  21814. }, []);
  21815. }
  21816. pattern$1.expandPatternsWithBraceExpansion = expandPatternsWithBraceExpansion;
  21817. function expandBraceExpansion(pattern) {
  21818. const patterns = micromatch.braces(pattern, { expand: true, nodupes: true, keepEscaping: true });
  21819. /**
  21820. * Sort the patterns by length so that the same depth patterns are processed side by side.
  21821. * `a/{b,}/{c,}/*` – `['a///*', 'a/b//*', 'a//c/*', 'a/b/c/*']`
  21822. */
  21823. patterns.sort((a, b) => a.length - b.length);
  21824. /**
  21825. * Micromatch can return an empty string in the case of patterns like `{a,}`.
  21826. */
  21827. return patterns.filter((pattern) => pattern !== '');
  21828. }
  21829. pattern$1.expandBraceExpansion = expandBraceExpansion;
  21830. function getPatternParts(pattern, options) {
  21831. let { parts } = micromatch.scan(pattern, Object.assign(Object.assign({}, options), { parts: true }));
  21832. /**
  21833. * The scan method returns an empty array in some cases.
  21834. * See micromatch/picomatch#58 for more details.
  21835. */
  21836. if (parts.length === 0) {
  21837. parts = [pattern];
  21838. }
  21839. /**
  21840. * The scan method does not return an empty part for the pattern with a forward slash.
  21841. * This is another part of micromatch/picomatch#58.
  21842. */
  21843. if (parts[0].startsWith('/')) {
  21844. parts[0] = parts[0].slice(1);
  21845. parts.unshift('');
  21846. }
  21847. return parts;
  21848. }
  21849. pattern$1.getPatternParts = getPatternParts;
  21850. function makeRe(pattern, options) {
  21851. return micromatch.makeRe(pattern, options);
  21852. }
  21853. pattern$1.makeRe = makeRe;
  21854. function convertPatternsToRe(patterns, options) {
  21855. return patterns.map((pattern) => makeRe(pattern, options));
  21856. }
  21857. pattern$1.convertPatternsToRe = convertPatternsToRe;
  21858. function matchAny(entry, patternsRe) {
  21859. return patternsRe.some((patternRe) => patternRe.test(entry));
  21860. }
  21861. pattern$1.matchAny = matchAny;
  21862. /**
  21863. * This package only works with forward slashes as a path separator.
  21864. * Because of this, we cannot use the standard `path.normalize` method, because on Windows platform it will use of backslashes.
  21865. */
  21866. function removeDuplicateSlashes(pattern) {
  21867. return pattern.replace(DOUBLE_SLASH_RE$1, '/');
  21868. }
  21869. pattern$1.removeDuplicateSlashes = removeDuplicateSlashes;
  21870. var stream$4 = {};
  21871. /*
  21872. * merge2
  21873. * https://github.com/teambition/merge2
  21874. *
  21875. * Copyright (c) 2014-2020 Teambition
  21876. * Licensed under the MIT license.
  21877. */
  21878. const Stream = require$$0$6;
  21879. const PassThrough = Stream.PassThrough;
  21880. const slice = Array.prototype.slice;
  21881. var merge2_1 = merge2$1;
  21882. function merge2$1 () {
  21883. const streamsQueue = [];
  21884. const args = slice.call(arguments);
  21885. let merging = false;
  21886. let options = args[args.length - 1];
  21887. if (options && !Array.isArray(options) && options.pipe == null) {
  21888. args.pop();
  21889. } else {
  21890. options = {};
  21891. }
  21892. const doEnd = options.end !== false;
  21893. const doPipeError = options.pipeError === true;
  21894. if (options.objectMode == null) {
  21895. options.objectMode = true;
  21896. }
  21897. if (options.highWaterMark == null) {
  21898. options.highWaterMark = 64 * 1024;
  21899. }
  21900. const mergedStream = PassThrough(options);
  21901. function addStream () {
  21902. for (let i = 0, len = arguments.length; i < len; i++) {
  21903. streamsQueue.push(pauseStreams(arguments[i], options));
  21904. }
  21905. mergeStream();
  21906. return this
  21907. }
  21908. function mergeStream () {
  21909. if (merging) {
  21910. return
  21911. }
  21912. merging = true;
  21913. let streams = streamsQueue.shift();
  21914. if (!streams) {
  21915. process.nextTick(endStream);
  21916. return
  21917. }
  21918. if (!Array.isArray(streams)) {
  21919. streams = [streams];
  21920. }
  21921. let pipesCount = streams.length + 1;
  21922. function next () {
  21923. if (--pipesCount > 0) {
  21924. return
  21925. }
  21926. merging = false;
  21927. mergeStream();
  21928. }
  21929. function pipe (stream) {
  21930. function onend () {
  21931. stream.removeListener('merge2UnpipeEnd', onend);
  21932. stream.removeListener('end', onend);
  21933. if (doPipeError) {
  21934. stream.removeListener('error', onerror);
  21935. }
  21936. next();
  21937. }
  21938. function onerror (err) {
  21939. mergedStream.emit('error', err);
  21940. }
  21941. // skip ended stream
  21942. if (stream._readableState.endEmitted) {
  21943. return next()
  21944. }
  21945. stream.on('merge2UnpipeEnd', onend);
  21946. stream.on('end', onend);
  21947. if (doPipeError) {
  21948. stream.on('error', onerror);
  21949. }
  21950. stream.pipe(mergedStream, { end: false });
  21951. // compatible for old stream
  21952. stream.resume();
  21953. }
  21954. for (let i = 0; i < streams.length; i++) {
  21955. pipe(streams[i]);
  21956. }
  21957. next();
  21958. }
  21959. function endStream () {
  21960. merging = false;
  21961. // emit 'queueDrain' when all streams merged.
  21962. mergedStream.emit('queueDrain');
  21963. if (doEnd) {
  21964. mergedStream.end();
  21965. }
  21966. }
  21967. mergedStream.setMaxListeners(0);
  21968. mergedStream.add = addStream;
  21969. mergedStream.on('unpipe', function (stream) {
  21970. stream.emit('merge2UnpipeEnd');
  21971. });
  21972. if (args.length) {
  21973. addStream.apply(null, args);
  21974. }
  21975. return mergedStream
  21976. }
  21977. // check and pause streams for pipe.
  21978. function pauseStreams (streams, options) {
  21979. if (!Array.isArray(streams)) {
  21980. // Backwards-compat with old-style streams
  21981. if (!streams._readableState && streams.pipe) {
  21982. streams = streams.pipe(PassThrough(options));
  21983. }
  21984. if (!streams._readableState || !streams.pause || !streams.pipe) {
  21985. throw new Error('Only readable stream can be merged.')
  21986. }
  21987. streams.pause();
  21988. } else {
  21989. for (let i = 0, len = streams.length; i < len; i++) {
  21990. streams[i] = pauseStreams(streams[i], options);
  21991. }
  21992. }
  21993. return streams
  21994. }
  21995. Object.defineProperty(stream$4, "__esModule", { value: true });
  21996. stream$4.merge = void 0;
  21997. const merge2 = merge2_1;
  21998. function merge$1(streams) {
  21999. const mergedStream = merge2(streams);
  22000. streams.forEach((stream) => {
  22001. stream.once('error', (error) => mergedStream.emit('error', error));
  22002. });
  22003. mergedStream.once('close', () => propagateCloseEventToSources(streams));
  22004. mergedStream.once('end', () => propagateCloseEventToSources(streams));
  22005. return mergedStream;
  22006. }
  22007. stream$4.merge = merge$1;
  22008. function propagateCloseEventToSources(streams) {
  22009. streams.forEach((stream) => stream.emit('close'));
  22010. }
  22011. var string$2 = {};
  22012. Object.defineProperty(string$2, "__esModule", { value: true });
  22013. string$2.isEmpty = string$2.isString = void 0;
  22014. function isString$1(input) {
  22015. return typeof input === 'string';
  22016. }
  22017. string$2.isString = isString$1;
  22018. function isEmpty$1(input) {
  22019. return input === '';
  22020. }
  22021. string$2.isEmpty = isEmpty$1;
  22022. Object.defineProperty(utils$g, "__esModule", { value: true });
  22023. utils$g.string = utils$g.stream = utils$g.pattern = utils$g.path = utils$g.fs = utils$g.errno = utils$g.array = void 0;
  22024. const array = array$1;
  22025. utils$g.array = array;
  22026. const errno = errno$1;
  22027. utils$g.errno = errno;
  22028. const fs$h = fs$i;
  22029. utils$g.fs = fs$h;
  22030. const path$f = path$i;
  22031. utils$g.path = path$f;
  22032. const pattern = pattern$1;
  22033. utils$g.pattern = pattern;
  22034. const stream$3 = stream$4;
  22035. utils$g.stream = stream$3;
  22036. const string$1 = string$2;
  22037. utils$g.string = string$1;
  22038. Object.defineProperty(tasks, "__esModule", { value: true });
  22039. tasks.convertPatternGroupToTask = tasks.convertPatternGroupsToTasks = tasks.groupPatternsByBaseDirectory = tasks.getNegativePatternsAsPositive = tasks.getPositivePatterns = tasks.convertPatternsToTasks = tasks.generate = void 0;
  22040. const utils$a = utils$g;
  22041. function generate(input, settings) {
  22042. const patterns = processPatterns(input, settings);
  22043. const ignore = processPatterns(settings.ignore, settings);
  22044. const positivePatterns = getPositivePatterns(patterns);
  22045. const negativePatterns = getNegativePatternsAsPositive(patterns, ignore);
  22046. const staticPatterns = positivePatterns.filter((pattern) => utils$a.pattern.isStaticPattern(pattern, settings));
  22047. const dynamicPatterns = positivePatterns.filter((pattern) => utils$a.pattern.isDynamicPattern(pattern, settings));
  22048. const staticTasks = convertPatternsToTasks(staticPatterns, negativePatterns, /* dynamic */ false);
  22049. const dynamicTasks = convertPatternsToTasks(dynamicPatterns, negativePatterns, /* dynamic */ true);
  22050. return staticTasks.concat(dynamicTasks);
  22051. }
  22052. tasks.generate = generate;
  22053. function processPatterns(input, settings) {
  22054. let patterns = input;
  22055. /**
  22056. * The original pattern like `{,*,**,a/*}` can lead to problems checking the depth when matching entry
  22057. * and some problems with the micromatch package (see fast-glob issues: #365, #394).
  22058. *
  22059. * To solve this problem, we expand all patterns containing brace expansion. This can lead to a slight slowdown
  22060. * in matching in the case of a large set of patterns after expansion.
  22061. */
  22062. if (settings.braceExpansion) {
  22063. patterns = utils$a.pattern.expandPatternsWithBraceExpansion(patterns);
  22064. }
  22065. /**
  22066. * If the `baseNameMatch` option is enabled, we must add globstar to patterns, so that they can be used
  22067. * at any nesting level.
  22068. *
  22069. * We do this here, because otherwise we have to complicate the filtering logic. For example, we need to change
  22070. * the pattern in the filter before creating a regular expression. There is no need to change the patterns
  22071. * in the application. Only on the input.
  22072. */
  22073. if (settings.baseNameMatch) {
  22074. patterns = patterns.map((pattern) => pattern.includes('/') ? pattern : `**/${pattern}`);
  22075. }
  22076. /**
  22077. * This method also removes duplicate slashes that may have been in the pattern or formed as a result of expansion.
  22078. */
  22079. return patterns.map((pattern) => utils$a.pattern.removeDuplicateSlashes(pattern));
  22080. }
  22081. /**
  22082. * Returns tasks grouped by basic pattern directories.
  22083. *
  22084. * Patterns that can be found inside (`./`) and outside (`../`) the current directory are handled separately.
  22085. * This is necessary because directory traversal starts at the base directory and goes deeper.
  22086. */
  22087. function convertPatternsToTasks(positive, negative, dynamic) {
  22088. const tasks = [];
  22089. const patternsOutsideCurrentDirectory = utils$a.pattern.getPatternsOutsideCurrentDirectory(positive);
  22090. const patternsInsideCurrentDirectory = utils$a.pattern.getPatternsInsideCurrentDirectory(positive);
  22091. const outsideCurrentDirectoryGroup = groupPatternsByBaseDirectory(patternsOutsideCurrentDirectory);
  22092. const insideCurrentDirectoryGroup = groupPatternsByBaseDirectory(patternsInsideCurrentDirectory);
  22093. tasks.push(...convertPatternGroupsToTasks(outsideCurrentDirectoryGroup, negative, dynamic));
  22094. /*
  22095. * For the sake of reducing future accesses to the file system, we merge all tasks within the current directory
  22096. * into a global task, if at least one pattern refers to the root (`.`). In this case, the global task covers the rest.
  22097. */
  22098. if ('.' in insideCurrentDirectoryGroup) {
  22099. tasks.push(convertPatternGroupToTask('.', patternsInsideCurrentDirectory, negative, dynamic));
  22100. }
  22101. else {
  22102. tasks.push(...convertPatternGroupsToTasks(insideCurrentDirectoryGroup, negative, dynamic));
  22103. }
  22104. return tasks;
  22105. }
  22106. tasks.convertPatternsToTasks = convertPatternsToTasks;
  22107. function getPositivePatterns(patterns) {
  22108. return utils$a.pattern.getPositivePatterns(patterns);
  22109. }
  22110. tasks.getPositivePatterns = getPositivePatterns;
  22111. function getNegativePatternsAsPositive(patterns, ignore) {
  22112. const negative = utils$a.pattern.getNegativePatterns(patterns).concat(ignore);
  22113. const positive = negative.map(utils$a.pattern.convertToPositivePattern);
  22114. return positive;
  22115. }
  22116. tasks.getNegativePatternsAsPositive = getNegativePatternsAsPositive;
  22117. function groupPatternsByBaseDirectory(patterns) {
  22118. const group = {};
  22119. return patterns.reduce((collection, pattern) => {
  22120. const base = utils$a.pattern.getBaseDirectory(pattern);
  22121. if (base in collection) {
  22122. collection[base].push(pattern);
  22123. }
  22124. else {
  22125. collection[base] = [pattern];
  22126. }
  22127. return collection;
  22128. }, group);
  22129. }
  22130. tasks.groupPatternsByBaseDirectory = groupPatternsByBaseDirectory;
  22131. function convertPatternGroupsToTasks(positive, negative, dynamic) {
  22132. return Object.keys(positive).map((base) => {
  22133. return convertPatternGroupToTask(base, positive[base], negative, dynamic);
  22134. });
  22135. }
  22136. tasks.convertPatternGroupsToTasks = convertPatternGroupsToTasks;
  22137. function convertPatternGroupToTask(base, positive, negative, dynamic) {
  22138. return {
  22139. dynamic,
  22140. positive,
  22141. negative,
  22142. base,
  22143. patterns: [].concat(positive, negative.map(utils$a.pattern.convertToNegativePattern))
  22144. };
  22145. }
  22146. tasks.convertPatternGroupToTask = convertPatternGroupToTask;
  22147. var async$7 = {};
  22148. var async$6 = {};
  22149. var out$3 = {};
  22150. var async$5 = {};
  22151. var async$4 = {};
  22152. var out$2 = {};
  22153. var async$3 = {};
  22154. var out$1 = {};
  22155. var async$2 = {};
  22156. Object.defineProperty(async$2, "__esModule", { value: true });
  22157. async$2.read = void 0;
  22158. function read$3(path, settings, callback) {
  22159. settings.fs.lstat(path, (lstatError, lstat) => {
  22160. if (lstatError !== null) {
  22161. callFailureCallback$2(callback, lstatError);
  22162. return;
  22163. }
  22164. if (!lstat.isSymbolicLink() || !settings.followSymbolicLink) {
  22165. callSuccessCallback$2(callback, lstat);
  22166. return;
  22167. }
  22168. settings.fs.stat(path, (statError, stat) => {
  22169. if (statError !== null) {
  22170. if (settings.throwErrorOnBrokenSymbolicLink) {
  22171. callFailureCallback$2(callback, statError);
  22172. return;
  22173. }
  22174. callSuccessCallback$2(callback, lstat);
  22175. return;
  22176. }
  22177. if (settings.markSymbolicLink) {
  22178. stat.isSymbolicLink = () => true;
  22179. }
  22180. callSuccessCallback$2(callback, stat);
  22181. });
  22182. });
  22183. }
  22184. async$2.read = read$3;
  22185. function callFailureCallback$2(callback, error) {
  22186. callback(error);
  22187. }
  22188. function callSuccessCallback$2(callback, result) {
  22189. callback(null, result);
  22190. }
  22191. var sync$8 = {};
  22192. Object.defineProperty(sync$8, "__esModule", { value: true });
  22193. sync$8.read = void 0;
  22194. function read$2(path, settings) {
  22195. const lstat = settings.fs.lstatSync(path);
  22196. if (!lstat.isSymbolicLink() || !settings.followSymbolicLink) {
  22197. return lstat;
  22198. }
  22199. try {
  22200. const stat = settings.fs.statSync(path);
  22201. if (settings.markSymbolicLink) {
  22202. stat.isSymbolicLink = () => true;
  22203. }
  22204. return stat;
  22205. }
  22206. catch (error) {
  22207. if (!settings.throwErrorOnBrokenSymbolicLink) {
  22208. return lstat;
  22209. }
  22210. throw error;
  22211. }
  22212. }
  22213. sync$8.read = read$2;
  22214. var settings$3 = {};
  22215. var fs$g = {};
  22216. (function (exports) {
  22217. Object.defineProperty(exports, "__esModule", { value: true });
  22218. exports.createFileSystemAdapter = exports.FILE_SYSTEM_ADAPTER = void 0;
  22219. const fs = require$$0__default;
  22220. exports.FILE_SYSTEM_ADAPTER = {
  22221. lstat: fs.lstat,
  22222. stat: fs.stat,
  22223. lstatSync: fs.lstatSync,
  22224. statSync: fs.statSync
  22225. };
  22226. function createFileSystemAdapter(fsMethods) {
  22227. if (fsMethods === undefined) {
  22228. return exports.FILE_SYSTEM_ADAPTER;
  22229. }
  22230. return Object.assign(Object.assign({}, exports.FILE_SYSTEM_ADAPTER), fsMethods);
  22231. }
  22232. exports.createFileSystemAdapter = createFileSystemAdapter;
  22233. } (fs$g));
  22234. Object.defineProperty(settings$3, "__esModule", { value: true });
  22235. const fs$f = fs$g;
  22236. let Settings$2 = class Settings {
  22237. constructor(_options = {}) {
  22238. this._options = _options;
  22239. this.followSymbolicLink = this._getValue(this._options.followSymbolicLink, true);
  22240. this.fs = fs$f.createFileSystemAdapter(this._options.fs);
  22241. this.markSymbolicLink = this._getValue(this._options.markSymbolicLink, false);
  22242. this.throwErrorOnBrokenSymbolicLink = this._getValue(this._options.throwErrorOnBrokenSymbolicLink, true);
  22243. }
  22244. _getValue(option, value) {
  22245. return option !== null && option !== void 0 ? option : value;
  22246. }
  22247. };
  22248. settings$3.default = Settings$2;
  22249. Object.defineProperty(out$1, "__esModule", { value: true });
  22250. out$1.statSync = out$1.stat = out$1.Settings = void 0;
  22251. const async$1 = async$2;
  22252. const sync$7 = sync$8;
  22253. const settings_1$3 = settings$3;
  22254. out$1.Settings = settings_1$3.default;
  22255. function stat$4(path, optionsOrSettingsOrCallback, callback) {
  22256. if (typeof optionsOrSettingsOrCallback === 'function') {
  22257. async$1.read(path, getSettings$2(), optionsOrSettingsOrCallback);
  22258. return;
  22259. }
  22260. async$1.read(path, getSettings$2(optionsOrSettingsOrCallback), callback);
  22261. }
  22262. out$1.stat = stat$4;
  22263. function statSync(path, optionsOrSettings) {
  22264. const settings = getSettings$2(optionsOrSettings);
  22265. return sync$7.read(path, settings);
  22266. }
  22267. out$1.statSync = statSync;
  22268. function getSettings$2(settingsOrOptions = {}) {
  22269. if (settingsOrOptions instanceof settings_1$3.default) {
  22270. return settingsOrOptions;
  22271. }
  22272. return new settings_1$3.default(settingsOrOptions);
  22273. }
  22274. /*! queue-microtask. MIT License. Feross Aboukhadijeh <https://feross.org/opensource> */
  22275. let promise;
  22276. var queueMicrotask_1 = typeof queueMicrotask === 'function'
  22277. ? queueMicrotask.bind(typeof window !== 'undefined' ? window : commonjsGlobal)
  22278. // reuse resolved promise, and allocate it lazily
  22279. : cb => (promise || (promise = Promise.resolve()))
  22280. .then(cb)
  22281. .catch(err => setTimeout(() => { throw err }, 0));
  22282. /*! run-parallel. MIT License. Feross Aboukhadijeh <https://feross.org/opensource> */
  22283. var runParallel_1 = runParallel;
  22284. const queueMicrotask$1 = queueMicrotask_1;
  22285. function runParallel (tasks, cb) {
  22286. let results, pending, keys;
  22287. let isSync = true;
  22288. if (Array.isArray(tasks)) {
  22289. results = [];
  22290. pending = tasks.length;
  22291. } else {
  22292. keys = Object.keys(tasks);
  22293. results = {};
  22294. pending = keys.length;
  22295. }
  22296. function done (err) {
  22297. function end () {
  22298. if (cb) cb(err, results);
  22299. cb = null;
  22300. }
  22301. if (isSync) queueMicrotask$1(end);
  22302. else end();
  22303. }
  22304. function each (i, err, result) {
  22305. results[i] = result;
  22306. if (--pending === 0 || err) {
  22307. done(err);
  22308. }
  22309. }
  22310. if (!pending) {
  22311. // empty
  22312. done(null);
  22313. } else if (keys) {
  22314. // object
  22315. keys.forEach(function (key) {
  22316. tasks[key](function (err, result) { each(key, err, result); });
  22317. });
  22318. } else {
  22319. // array
  22320. tasks.forEach(function (task, i) {
  22321. task(function (err, result) { each(i, err, result); });
  22322. });
  22323. }
  22324. isSync = false;
  22325. }
  22326. var constants$2 = {};
  22327. Object.defineProperty(constants$2, "__esModule", { value: true });
  22328. constants$2.IS_SUPPORT_READDIR_WITH_FILE_TYPES = void 0;
  22329. const NODE_PROCESS_VERSION_PARTS = process.versions.node.split('.');
  22330. if (NODE_PROCESS_VERSION_PARTS[0] === undefined || NODE_PROCESS_VERSION_PARTS[1] === undefined) {
  22331. throw new Error(`Unexpected behavior. The 'process.versions.node' variable has invalid value: ${process.versions.node}`);
  22332. }
  22333. const MAJOR_VERSION = Number.parseInt(NODE_PROCESS_VERSION_PARTS[0], 10);
  22334. const MINOR_VERSION = Number.parseInt(NODE_PROCESS_VERSION_PARTS[1], 10);
  22335. const SUPPORTED_MAJOR_VERSION = 10;
  22336. const SUPPORTED_MINOR_VERSION = 10;
  22337. const IS_MATCHED_BY_MAJOR = MAJOR_VERSION > SUPPORTED_MAJOR_VERSION;
  22338. const IS_MATCHED_BY_MAJOR_AND_MINOR = MAJOR_VERSION === SUPPORTED_MAJOR_VERSION && MINOR_VERSION >= SUPPORTED_MINOR_VERSION;
  22339. /**
  22340. * IS `true` for Node.js 10.10 and greater.
  22341. */
  22342. constants$2.IS_SUPPORT_READDIR_WITH_FILE_TYPES = IS_MATCHED_BY_MAJOR || IS_MATCHED_BY_MAJOR_AND_MINOR;
  22343. var utils$9 = {};
  22344. var fs$e = {};
  22345. Object.defineProperty(fs$e, "__esModule", { value: true });
  22346. fs$e.createDirentFromStats = void 0;
  22347. class DirentFromStats {
  22348. constructor(name, stats) {
  22349. this.name = name;
  22350. this.isBlockDevice = stats.isBlockDevice.bind(stats);
  22351. this.isCharacterDevice = stats.isCharacterDevice.bind(stats);
  22352. this.isDirectory = stats.isDirectory.bind(stats);
  22353. this.isFIFO = stats.isFIFO.bind(stats);
  22354. this.isFile = stats.isFile.bind(stats);
  22355. this.isSocket = stats.isSocket.bind(stats);
  22356. this.isSymbolicLink = stats.isSymbolicLink.bind(stats);
  22357. }
  22358. }
  22359. function createDirentFromStats(name, stats) {
  22360. return new DirentFromStats(name, stats);
  22361. }
  22362. fs$e.createDirentFromStats = createDirentFromStats;
  22363. Object.defineProperty(utils$9, "__esModule", { value: true });
  22364. utils$9.fs = void 0;
  22365. const fs$d = fs$e;
  22366. utils$9.fs = fs$d;
  22367. var common$a = {};
  22368. Object.defineProperty(common$a, "__esModule", { value: true });
  22369. common$a.joinPathSegments = void 0;
  22370. function joinPathSegments$1(a, b, separator) {
  22371. /**
  22372. * The correct handling of cases when the first segment is a root (`/`, `C:/`) or UNC path (`//?/C:/`).
  22373. */
  22374. if (a.endsWith(separator)) {
  22375. return a + b;
  22376. }
  22377. return a + separator + b;
  22378. }
  22379. common$a.joinPathSegments = joinPathSegments$1;
  22380. Object.defineProperty(async$3, "__esModule", { value: true });
  22381. async$3.readdir = async$3.readdirWithFileTypes = async$3.read = void 0;
  22382. const fsStat$5 = out$1;
  22383. const rpl = runParallel_1;
  22384. const constants_1$1 = constants$2;
  22385. const utils$8 = utils$9;
  22386. const common$9 = common$a;
  22387. function read$1(directory, settings, callback) {
  22388. if (!settings.stats && constants_1$1.IS_SUPPORT_READDIR_WITH_FILE_TYPES) {
  22389. readdirWithFileTypes$1(directory, settings, callback);
  22390. return;
  22391. }
  22392. readdir$3(directory, settings, callback);
  22393. }
  22394. async$3.read = read$1;
  22395. function readdirWithFileTypes$1(directory, settings, callback) {
  22396. settings.fs.readdir(directory, { withFileTypes: true }, (readdirError, dirents) => {
  22397. if (readdirError !== null) {
  22398. callFailureCallback$1(callback, readdirError);
  22399. return;
  22400. }
  22401. const entries = dirents.map((dirent) => ({
  22402. dirent,
  22403. name: dirent.name,
  22404. path: common$9.joinPathSegments(directory, dirent.name, settings.pathSegmentSeparator)
  22405. }));
  22406. if (!settings.followSymbolicLinks) {
  22407. callSuccessCallback$1(callback, entries);
  22408. return;
  22409. }
  22410. const tasks = entries.map((entry) => makeRplTaskEntry(entry, settings));
  22411. rpl(tasks, (rplError, rplEntries) => {
  22412. if (rplError !== null) {
  22413. callFailureCallback$1(callback, rplError);
  22414. return;
  22415. }
  22416. callSuccessCallback$1(callback, rplEntries);
  22417. });
  22418. });
  22419. }
  22420. async$3.readdirWithFileTypes = readdirWithFileTypes$1;
  22421. function makeRplTaskEntry(entry, settings) {
  22422. return (done) => {
  22423. if (!entry.dirent.isSymbolicLink()) {
  22424. done(null, entry);
  22425. return;
  22426. }
  22427. settings.fs.stat(entry.path, (statError, stats) => {
  22428. if (statError !== null) {
  22429. if (settings.throwErrorOnBrokenSymbolicLink) {
  22430. done(statError);
  22431. return;
  22432. }
  22433. done(null, entry);
  22434. return;
  22435. }
  22436. entry.dirent = utils$8.fs.createDirentFromStats(entry.name, stats);
  22437. done(null, entry);
  22438. });
  22439. };
  22440. }
  22441. function readdir$3(directory, settings, callback) {
  22442. settings.fs.readdir(directory, (readdirError, names) => {
  22443. if (readdirError !== null) {
  22444. callFailureCallback$1(callback, readdirError);
  22445. return;
  22446. }
  22447. const tasks = names.map((name) => {
  22448. const path = common$9.joinPathSegments(directory, name, settings.pathSegmentSeparator);
  22449. return (done) => {
  22450. fsStat$5.stat(path, settings.fsStatSettings, (error, stats) => {
  22451. if (error !== null) {
  22452. done(error);
  22453. return;
  22454. }
  22455. const entry = {
  22456. name,
  22457. path,
  22458. dirent: utils$8.fs.createDirentFromStats(name, stats)
  22459. };
  22460. if (settings.stats) {
  22461. entry.stats = stats;
  22462. }
  22463. done(null, entry);
  22464. });
  22465. };
  22466. });
  22467. rpl(tasks, (rplError, entries) => {
  22468. if (rplError !== null) {
  22469. callFailureCallback$1(callback, rplError);
  22470. return;
  22471. }
  22472. callSuccessCallback$1(callback, entries);
  22473. });
  22474. });
  22475. }
  22476. async$3.readdir = readdir$3;
  22477. function callFailureCallback$1(callback, error) {
  22478. callback(error);
  22479. }
  22480. function callSuccessCallback$1(callback, result) {
  22481. callback(null, result);
  22482. }
  22483. var sync$6 = {};
  22484. Object.defineProperty(sync$6, "__esModule", { value: true });
  22485. sync$6.readdir = sync$6.readdirWithFileTypes = sync$6.read = void 0;
  22486. const fsStat$4 = out$1;
  22487. const constants_1 = constants$2;
  22488. const utils$7 = utils$9;
  22489. const common$8 = common$a;
  22490. function read(directory, settings) {
  22491. if (!settings.stats && constants_1.IS_SUPPORT_READDIR_WITH_FILE_TYPES) {
  22492. return readdirWithFileTypes(directory, settings);
  22493. }
  22494. return readdir$2(directory, settings);
  22495. }
  22496. sync$6.read = read;
  22497. function readdirWithFileTypes(directory, settings) {
  22498. const dirents = settings.fs.readdirSync(directory, { withFileTypes: true });
  22499. return dirents.map((dirent) => {
  22500. const entry = {
  22501. dirent,
  22502. name: dirent.name,
  22503. path: common$8.joinPathSegments(directory, dirent.name, settings.pathSegmentSeparator)
  22504. };
  22505. if (entry.dirent.isSymbolicLink() && settings.followSymbolicLinks) {
  22506. try {
  22507. const stats = settings.fs.statSync(entry.path);
  22508. entry.dirent = utils$7.fs.createDirentFromStats(entry.name, stats);
  22509. }
  22510. catch (error) {
  22511. if (settings.throwErrorOnBrokenSymbolicLink) {
  22512. throw error;
  22513. }
  22514. }
  22515. }
  22516. return entry;
  22517. });
  22518. }
  22519. sync$6.readdirWithFileTypes = readdirWithFileTypes;
  22520. function readdir$2(directory, settings) {
  22521. const names = settings.fs.readdirSync(directory);
  22522. return names.map((name) => {
  22523. const entryPath = common$8.joinPathSegments(directory, name, settings.pathSegmentSeparator);
  22524. const stats = fsStat$4.statSync(entryPath, settings.fsStatSettings);
  22525. const entry = {
  22526. name,
  22527. path: entryPath,
  22528. dirent: utils$7.fs.createDirentFromStats(name, stats)
  22529. };
  22530. if (settings.stats) {
  22531. entry.stats = stats;
  22532. }
  22533. return entry;
  22534. });
  22535. }
  22536. sync$6.readdir = readdir$2;
  22537. var settings$2 = {};
  22538. var fs$c = {};
  22539. (function (exports) {
  22540. Object.defineProperty(exports, "__esModule", { value: true });
  22541. exports.createFileSystemAdapter = exports.FILE_SYSTEM_ADAPTER = void 0;
  22542. const fs = require$$0__default;
  22543. exports.FILE_SYSTEM_ADAPTER = {
  22544. lstat: fs.lstat,
  22545. stat: fs.stat,
  22546. lstatSync: fs.lstatSync,
  22547. statSync: fs.statSync,
  22548. readdir: fs.readdir,
  22549. readdirSync: fs.readdirSync
  22550. };
  22551. function createFileSystemAdapter(fsMethods) {
  22552. if (fsMethods === undefined) {
  22553. return exports.FILE_SYSTEM_ADAPTER;
  22554. }
  22555. return Object.assign(Object.assign({}, exports.FILE_SYSTEM_ADAPTER), fsMethods);
  22556. }
  22557. exports.createFileSystemAdapter = createFileSystemAdapter;
  22558. } (fs$c));
  22559. Object.defineProperty(settings$2, "__esModule", { value: true });
  22560. const path$e = require$$0$4;
  22561. const fsStat$3 = out$1;
  22562. const fs$b = fs$c;
  22563. let Settings$1 = class Settings {
  22564. constructor(_options = {}) {
  22565. this._options = _options;
  22566. this.followSymbolicLinks = this._getValue(this._options.followSymbolicLinks, false);
  22567. this.fs = fs$b.createFileSystemAdapter(this._options.fs);
  22568. this.pathSegmentSeparator = this._getValue(this._options.pathSegmentSeparator, path$e.sep);
  22569. this.stats = this._getValue(this._options.stats, false);
  22570. this.throwErrorOnBrokenSymbolicLink = this._getValue(this._options.throwErrorOnBrokenSymbolicLink, true);
  22571. this.fsStatSettings = new fsStat$3.Settings({
  22572. followSymbolicLink: this.followSymbolicLinks,
  22573. fs: this.fs,
  22574. throwErrorOnBrokenSymbolicLink: this.throwErrorOnBrokenSymbolicLink
  22575. });
  22576. }
  22577. _getValue(option, value) {
  22578. return option !== null && option !== void 0 ? option : value;
  22579. }
  22580. };
  22581. settings$2.default = Settings$1;
  22582. Object.defineProperty(out$2, "__esModule", { value: true });
  22583. out$2.Settings = out$2.scandirSync = out$2.scandir = void 0;
  22584. const async = async$3;
  22585. const sync$5 = sync$6;
  22586. const settings_1$2 = settings$2;
  22587. out$2.Settings = settings_1$2.default;
  22588. function scandir(path, optionsOrSettingsOrCallback, callback) {
  22589. if (typeof optionsOrSettingsOrCallback === 'function') {
  22590. async.read(path, getSettings$1(), optionsOrSettingsOrCallback);
  22591. return;
  22592. }
  22593. async.read(path, getSettings$1(optionsOrSettingsOrCallback), callback);
  22594. }
  22595. out$2.scandir = scandir;
  22596. function scandirSync(path, optionsOrSettings) {
  22597. const settings = getSettings$1(optionsOrSettings);
  22598. return sync$5.read(path, settings);
  22599. }
  22600. out$2.scandirSync = scandirSync;
  22601. function getSettings$1(settingsOrOptions = {}) {
  22602. if (settingsOrOptions instanceof settings_1$2.default) {
  22603. return settingsOrOptions;
  22604. }
  22605. return new settings_1$2.default(settingsOrOptions);
  22606. }
  22607. var queue = {exports: {}};
  22608. function reusify$1 (Constructor) {
  22609. var head = new Constructor();
  22610. var tail = head;
  22611. function get () {
  22612. var current = head;
  22613. if (current.next) {
  22614. head = current.next;
  22615. } else {
  22616. head = new Constructor();
  22617. tail = head;
  22618. }
  22619. current.next = null;
  22620. return current
  22621. }
  22622. function release (obj) {
  22623. tail.next = obj;
  22624. tail = obj;
  22625. }
  22626. return {
  22627. get: get,
  22628. release: release
  22629. }
  22630. }
  22631. var reusify_1 = reusify$1;
  22632. /* eslint-disable no-var */
  22633. var reusify = reusify_1;
  22634. function fastqueue (context, worker, _concurrency) {
  22635. if (typeof context === 'function') {
  22636. _concurrency = worker;
  22637. worker = context;
  22638. context = null;
  22639. }
  22640. if (!(_concurrency >= 1)) {
  22641. throw new Error('fastqueue concurrency must be equal to or greater than 1')
  22642. }
  22643. var cache = reusify(Task);
  22644. var queueHead = null;
  22645. var queueTail = null;
  22646. var _running = 0;
  22647. var errorHandler = null;
  22648. var self = {
  22649. push: push,
  22650. drain: noop$4,
  22651. saturated: noop$4,
  22652. pause: pause,
  22653. paused: false,
  22654. get concurrency () {
  22655. return _concurrency
  22656. },
  22657. set concurrency (value) {
  22658. if (!(value >= 1)) {
  22659. throw new Error('fastqueue concurrency must be equal to or greater than 1')
  22660. }
  22661. _concurrency = value;
  22662. if (self.paused) return
  22663. for (; queueHead && _running < _concurrency;) {
  22664. _running++;
  22665. release();
  22666. }
  22667. },
  22668. running: running,
  22669. resume: resume,
  22670. idle: idle,
  22671. length: length,
  22672. getQueue: getQueue,
  22673. unshift: unshift,
  22674. empty: noop$4,
  22675. kill: kill,
  22676. killAndDrain: killAndDrain,
  22677. error: error
  22678. };
  22679. return self
  22680. function running () {
  22681. return _running
  22682. }
  22683. function pause () {
  22684. self.paused = true;
  22685. }
  22686. function length () {
  22687. var current = queueHead;
  22688. var counter = 0;
  22689. while (current) {
  22690. current = current.next;
  22691. counter++;
  22692. }
  22693. return counter
  22694. }
  22695. function getQueue () {
  22696. var current = queueHead;
  22697. var tasks = [];
  22698. while (current) {
  22699. tasks.push(current.value);
  22700. current = current.next;
  22701. }
  22702. return tasks
  22703. }
  22704. function resume () {
  22705. if (!self.paused) return
  22706. self.paused = false;
  22707. if (queueHead === null) {
  22708. _running++;
  22709. release();
  22710. return
  22711. }
  22712. for (; queueHead && _running < _concurrency;) {
  22713. _running++;
  22714. release();
  22715. }
  22716. }
  22717. function idle () {
  22718. return _running === 0 && self.length() === 0
  22719. }
  22720. function push (value, done) {
  22721. var current = cache.get();
  22722. current.context = context;
  22723. current.release = release;
  22724. current.value = value;
  22725. current.callback = done || noop$4;
  22726. current.errorHandler = errorHandler;
  22727. if (_running >= _concurrency || self.paused) {
  22728. if (queueTail) {
  22729. queueTail.next = current;
  22730. queueTail = current;
  22731. } else {
  22732. queueHead = current;
  22733. queueTail = current;
  22734. self.saturated();
  22735. }
  22736. } else {
  22737. _running++;
  22738. worker.call(context, current.value, current.worked);
  22739. }
  22740. }
  22741. function unshift (value, done) {
  22742. var current = cache.get();
  22743. current.context = context;
  22744. current.release = release;
  22745. current.value = value;
  22746. current.callback = done || noop$4;
  22747. current.errorHandler = errorHandler;
  22748. if (_running >= _concurrency || self.paused) {
  22749. if (queueHead) {
  22750. current.next = queueHead;
  22751. queueHead = current;
  22752. } else {
  22753. queueHead = current;
  22754. queueTail = current;
  22755. self.saturated();
  22756. }
  22757. } else {
  22758. _running++;
  22759. worker.call(context, current.value, current.worked);
  22760. }
  22761. }
  22762. function release (holder) {
  22763. if (holder) {
  22764. cache.release(holder);
  22765. }
  22766. var next = queueHead;
  22767. if (next && _running <= _concurrency) {
  22768. if (!self.paused) {
  22769. if (queueTail === queueHead) {
  22770. queueTail = null;
  22771. }
  22772. queueHead = next.next;
  22773. next.next = null;
  22774. worker.call(context, next.value, next.worked);
  22775. if (queueTail === null) {
  22776. self.empty();
  22777. }
  22778. } else {
  22779. _running--;
  22780. }
  22781. } else if (--_running === 0) {
  22782. self.drain();
  22783. }
  22784. }
  22785. function kill () {
  22786. queueHead = null;
  22787. queueTail = null;
  22788. self.drain = noop$4;
  22789. }
  22790. function killAndDrain () {
  22791. queueHead = null;
  22792. queueTail = null;
  22793. self.drain();
  22794. self.drain = noop$4;
  22795. }
  22796. function error (handler) {
  22797. errorHandler = handler;
  22798. }
  22799. }
  22800. function noop$4 () {}
  22801. function Task () {
  22802. this.value = null;
  22803. this.callback = noop$4;
  22804. this.next = null;
  22805. this.release = noop$4;
  22806. this.context = null;
  22807. this.errorHandler = null;
  22808. var self = this;
  22809. this.worked = function worked (err, result) {
  22810. var callback = self.callback;
  22811. var errorHandler = self.errorHandler;
  22812. var val = self.value;
  22813. self.value = null;
  22814. self.callback = noop$4;
  22815. if (self.errorHandler) {
  22816. errorHandler(err, val);
  22817. }
  22818. callback.call(self.context, err, result);
  22819. self.release(self);
  22820. };
  22821. }
  22822. function queueAsPromised (context, worker, _concurrency) {
  22823. if (typeof context === 'function') {
  22824. _concurrency = worker;
  22825. worker = context;
  22826. context = null;
  22827. }
  22828. function asyncWrapper (arg, cb) {
  22829. worker.call(this, arg)
  22830. .then(function (res) {
  22831. cb(null, res);
  22832. }, cb);
  22833. }
  22834. var queue = fastqueue(context, asyncWrapper, _concurrency);
  22835. var pushCb = queue.push;
  22836. var unshiftCb = queue.unshift;
  22837. queue.push = push;
  22838. queue.unshift = unshift;
  22839. queue.drained = drained;
  22840. return queue
  22841. function push (value) {
  22842. var p = new Promise(function (resolve, reject) {
  22843. pushCb(value, function (err, result) {
  22844. if (err) {
  22845. reject(err);
  22846. return
  22847. }
  22848. resolve(result);
  22849. });
  22850. });
  22851. // Let's fork the promise chain to
  22852. // make the error bubble up to the user but
  22853. // not lead to a unhandledRejection
  22854. p.catch(noop$4);
  22855. return p
  22856. }
  22857. function unshift (value) {
  22858. var p = new Promise(function (resolve, reject) {
  22859. unshiftCb(value, function (err, result) {
  22860. if (err) {
  22861. reject(err);
  22862. return
  22863. }
  22864. resolve(result);
  22865. });
  22866. });
  22867. // Let's fork the promise chain to
  22868. // make the error bubble up to the user but
  22869. // not lead to a unhandledRejection
  22870. p.catch(noop$4);
  22871. return p
  22872. }
  22873. function drained () {
  22874. if (queue.idle()) {
  22875. return new Promise(function (resolve) {
  22876. resolve();
  22877. })
  22878. }
  22879. var previousDrain = queue.drain;
  22880. var p = new Promise(function (resolve) {
  22881. queue.drain = function () {
  22882. previousDrain();
  22883. resolve();
  22884. };
  22885. });
  22886. return p
  22887. }
  22888. }
  22889. queue.exports = fastqueue;
  22890. queue.exports.promise = queueAsPromised;
  22891. var queueExports = queue.exports;
  22892. var common$7 = {};
  22893. Object.defineProperty(common$7, "__esModule", { value: true });
  22894. common$7.joinPathSegments = common$7.replacePathSegmentSeparator = common$7.isAppliedFilter = common$7.isFatalError = void 0;
  22895. function isFatalError(settings, error) {
  22896. if (settings.errorFilter === null) {
  22897. return true;
  22898. }
  22899. return !settings.errorFilter(error);
  22900. }
  22901. common$7.isFatalError = isFatalError;
  22902. function isAppliedFilter(filter, value) {
  22903. return filter === null || filter(value);
  22904. }
  22905. common$7.isAppliedFilter = isAppliedFilter;
  22906. function replacePathSegmentSeparator(filepath, separator) {
  22907. return filepath.split(/[/\\]/).join(separator);
  22908. }
  22909. common$7.replacePathSegmentSeparator = replacePathSegmentSeparator;
  22910. function joinPathSegments(a, b, separator) {
  22911. if (a === '') {
  22912. return b;
  22913. }
  22914. /**
  22915. * The correct handling of cases when the first segment is a root (`/`, `C:/`) or UNC path (`//?/C:/`).
  22916. */
  22917. if (a.endsWith(separator)) {
  22918. return a + b;
  22919. }
  22920. return a + separator + b;
  22921. }
  22922. common$7.joinPathSegments = joinPathSegments;
  22923. var reader$1 = {};
  22924. Object.defineProperty(reader$1, "__esModule", { value: true });
  22925. const common$6 = common$7;
  22926. let Reader$1 = class Reader {
  22927. constructor(_root, _settings) {
  22928. this._root = _root;
  22929. this._settings = _settings;
  22930. this._root = common$6.replacePathSegmentSeparator(_root, _settings.pathSegmentSeparator);
  22931. }
  22932. };
  22933. reader$1.default = Reader$1;
  22934. Object.defineProperty(async$4, "__esModule", { value: true });
  22935. const events_1 = require$$0$7;
  22936. const fsScandir$2 = out$2;
  22937. const fastq = queueExports;
  22938. const common$5 = common$7;
  22939. const reader_1$4 = reader$1;
  22940. class AsyncReader extends reader_1$4.default {
  22941. constructor(_root, _settings) {
  22942. super(_root, _settings);
  22943. this._settings = _settings;
  22944. this._scandir = fsScandir$2.scandir;
  22945. this._emitter = new events_1.EventEmitter();
  22946. this._queue = fastq(this._worker.bind(this), this._settings.concurrency);
  22947. this._isFatalError = false;
  22948. this._isDestroyed = false;
  22949. this._queue.drain = () => {
  22950. if (!this._isFatalError) {
  22951. this._emitter.emit('end');
  22952. }
  22953. };
  22954. }
  22955. read() {
  22956. this._isFatalError = false;
  22957. this._isDestroyed = false;
  22958. setImmediate(() => {
  22959. this._pushToQueue(this._root, this._settings.basePath);
  22960. });
  22961. return this._emitter;
  22962. }
  22963. get isDestroyed() {
  22964. return this._isDestroyed;
  22965. }
  22966. destroy() {
  22967. if (this._isDestroyed) {
  22968. throw new Error('The reader is already destroyed');
  22969. }
  22970. this._isDestroyed = true;
  22971. this._queue.killAndDrain();
  22972. }
  22973. onEntry(callback) {
  22974. this._emitter.on('entry', callback);
  22975. }
  22976. onError(callback) {
  22977. this._emitter.once('error', callback);
  22978. }
  22979. onEnd(callback) {
  22980. this._emitter.once('end', callback);
  22981. }
  22982. _pushToQueue(directory, base) {
  22983. const queueItem = { directory, base };
  22984. this._queue.push(queueItem, (error) => {
  22985. if (error !== null) {
  22986. this._handleError(error);
  22987. }
  22988. });
  22989. }
  22990. _worker(item, done) {
  22991. this._scandir(item.directory, this._settings.fsScandirSettings, (error, entries) => {
  22992. if (error !== null) {
  22993. done(error, undefined);
  22994. return;
  22995. }
  22996. for (const entry of entries) {
  22997. this._handleEntry(entry, item.base);
  22998. }
  22999. done(null, undefined);
  23000. });
  23001. }
  23002. _handleError(error) {
  23003. if (this._isDestroyed || !common$5.isFatalError(this._settings, error)) {
  23004. return;
  23005. }
  23006. this._isFatalError = true;
  23007. this._isDestroyed = true;
  23008. this._emitter.emit('error', error);
  23009. }
  23010. _handleEntry(entry, base) {
  23011. if (this._isDestroyed || this._isFatalError) {
  23012. return;
  23013. }
  23014. const fullpath = entry.path;
  23015. if (base !== undefined) {
  23016. entry.path = common$5.joinPathSegments(base, entry.name, this._settings.pathSegmentSeparator);
  23017. }
  23018. if (common$5.isAppliedFilter(this._settings.entryFilter, entry)) {
  23019. this._emitEntry(entry);
  23020. }
  23021. if (entry.dirent.isDirectory() && common$5.isAppliedFilter(this._settings.deepFilter, entry)) {
  23022. this._pushToQueue(fullpath, base === undefined ? undefined : entry.path);
  23023. }
  23024. }
  23025. _emitEntry(entry) {
  23026. this._emitter.emit('entry', entry);
  23027. }
  23028. }
  23029. async$4.default = AsyncReader;
  23030. Object.defineProperty(async$5, "__esModule", { value: true });
  23031. const async_1$4 = async$4;
  23032. class AsyncProvider {
  23033. constructor(_root, _settings) {
  23034. this._root = _root;
  23035. this._settings = _settings;
  23036. this._reader = new async_1$4.default(this._root, this._settings);
  23037. this._storage = [];
  23038. }
  23039. read(callback) {
  23040. this._reader.onError((error) => {
  23041. callFailureCallback(callback, error);
  23042. });
  23043. this._reader.onEntry((entry) => {
  23044. this._storage.push(entry);
  23045. });
  23046. this._reader.onEnd(() => {
  23047. callSuccessCallback(callback, this._storage);
  23048. });
  23049. this._reader.read();
  23050. }
  23051. }
  23052. async$5.default = AsyncProvider;
  23053. function callFailureCallback(callback, error) {
  23054. callback(error);
  23055. }
  23056. function callSuccessCallback(callback, entries) {
  23057. callback(null, entries);
  23058. }
  23059. var stream$2 = {};
  23060. Object.defineProperty(stream$2, "__esModule", { value: true });
  23061. const stream_1$5 = require$$0$6;
  23062. const async_1$3 = async$4;
  23063. class StreamProvider {
  23064. constructor(_root, _settings) {
  23065. this._root = _root;
  23066. this._settings = _settings;
  23067. this._reader = new async_1$3.default(this._root, this._settings);
  23068. this._stream = new stream_1$5.Readable({
  23069. objectMode: true,
  23070. read: () => { },
  23071. destroy: () => {
  23072. if (!this._reader.isDestroyed) {
  23073. this._reader.destroy();
  23074. }
  23075. }
  23076. });
  23077. }
  23078. read() {
  23079. this._reader.onError((error) => {
  23080. this._stream.emit('error', error);
  23081. });
  23082. this._reader.onEntry((entry) => {
  23083. this._stream.push(entry);
  23084. });
  23085. this._reader.onEnd(() => {
  23086. this._stream.push(null);
  23087. });
  23088. this._reader.read();
  23089. return this._stream;
  23090. }
  23091. }
  23092. stream$2.default = StreamProvider;
  23093. var sync$4 = {};
  23094. var sync$3 = {};
  23095. Object.defineProperty(sync$3, "__esModule", { value: true });
  23096. const fsScandir$1 = out$2;
  23097. const common$4 = common$7;
  23098. const reader_1$3 = reader$1;
  23099. class SyncReader extends reader_1$3.default {
  23100. constructor() {
  23101. super(...arguments);
  23102. this._scandir = fsScandir$1.scandirSync;
  23103. this._storage = [];
  23104. this._queue = new Set();
  23105. }
  23106. read() {
  23107. this._pushToQueue(this._root, this._settings.basePath);
  23108. this._handleQueue();
  23109. return this._storage;
  23110. }
  23111. _pushToQueue(directory, base) {
  23112. this._queue.add({ directory, base });
  23113. }
  23114. _handleQueue() {
  23115. for (const item of this._queue.values()) {
  23116. this._handleDirectory(item.directory, item.base);
  23117. }
  23118. }
  23119. _handleDirectory(directory, base) {
  23120. try {
  23121. const entries = this._scandir(directory, this._settings.fsScandirSettings);
  23122. for (const entry of entries) {
  23123. this._handleEntry(entry, base);
  23124. }
  23125. }
  23126. catch (error) {
  23127. this._handleError(error);
  23128. }
  23129. }
  23130. _handleError(error) {
  23131. if (!common$4.isFatalError(this._settings, error)) {
  23132. return;
  23133. }
  23134. throw error;
  23135. }
  23136. _handleEntry(entry, base) {
  23137. const fullpath = entry.path;
  23138. if (base !== undefined) {
  23139. entry.path = common$4.joinPathSegments(base, entry.name, this._settings.pathSegmentSeparator);
  23140. }
  23141. if (common$4.isAppliedFilter(this._settings.entryFilter, entry)) {
  23142. this._pushToStorage(entry);
  23143. }
  23144. if (entry.dirent.isDirectory() && common$4.isAppliedFilter(this._settings.deepFilter, entry)) {
  23145. this._pushToQueue(fullpath, base === undefined ? undefined : entry.path);
  23146. }
  23147. }
  23148. _pushToStorage(entry) {
  23149. this._storage.push(entry);
  23150. }
  23151. }
  23152. sync$3.default = SyncReader;
  23153. Object.defineProperty(sync$4, "__esModule", { value: true });
  23154. const sync_1$3 = sync$3;
  23155. class SyncProvider {
  23156. constructor(_root, _settings) {
  23157. this._root = _root;
  23158. this._settings = _settings;
  23159. this._reader = new sync_1$3.default(this._root, this._settings);
  23160. }
  23161. read() {
  23162. return this._reader.read();
  23163. }
  23164. }
  23165. sync$4.default = SyncProvider;
  23166. var settings$1 = {};
  23167. Object.defineProperty(settings$1, "__esModule", { value: true });
  23168. const path$d = require$$0$4;
  23169. const fsScandir = out$2;
  23170. class Settings {
  23171. constructor(_options = {}) {
  23172. this._options = _options;
  23173. this.basePath = this._getValue(this._options.basePath, undefined);
  23174. this.concurrency = this._getValue(this._options.concurrency, Number.POSITIVE_INFINITY);
  23175. this.deepFilter = this._getValue(this._options.deepFilter, null);
  23176. this.entryFilter = this._getValue(this._options.entryFilter, null);
  23177. this.errorFilter = this._getValue(this._options.errorFilter, null);
  23178. this.pathSegmentSeparator = this._getValue(this._options.pathSegmentSeparator, path$d.sep);
  23179. this.fsScandirSettings = new fsScandir.Settings({
  23180. followSymbolicLinks: this._options.followSymbolicLinks,
  23181. fs: this._options.fs,
  23182. pathSegmentSeparator: this._options.pathSegmentSeparator,
  23183. stats: this._options.stats,
  23184. throwErrorOnBrokenSymbolicLink: this._options.throwErrorOnBrokenSymbolicLink
  23185. });
  23186. }
  23187. _getValue(option, value) {
  23188. return option !== null && option !== void 0 ? option : value;
  23189. }
  23190. }
  23191. settings$1.default = Settings;
  23192. Object.defineProperty(out$3, "__esModule", { value: true });
  23193. out$3.Settings = out$3.walkStream = out$3.walkSync = out$3.walk = void 0;
  23194. const async_1$2 = async$5;
  23195. const stream_1$4 = stream$2;
  23196. const sync_1$2 = sync$4;
  23197. const settings_1$1 = settings$1;
  23198. out$3.Settings = settings_1$1.default;
  23199. function walk$2(directory, optionsOrSettingsOrCallback, callback) {
  23200. if (typeof optionsOrSettingsOrCallback === 'function') {
  23201. new async_1$2.default(directory, getSettings()).read(optionsOrSettingsOrCallback);
  23202. return;
  23203. }
  23204. new async_1$2.default(directory, getSettings(optionsOrSettingsOrCallback)).read(callback);
  23205. }
  23206. out$3.walk = walk$2;
  23207. function walkSync(directory, optionsOrSettings) {
  23208. const settings = getSettings(optionsOrSettings);
  23209. const provider = new sync_1$2.default(directory, settings);
  23210. return provider.read();
  23211. }
  23212. out$3.walkSync = walkSync;
  23213. function walkStream(directory, optionsOrSettings) {
  23214. const settings = getSettings(optionsOrSettings);
  23215. const provider = new stream_1$4.default(directory, settings);
  23216. return provider.read();
  23217. }
  23218. out$3.walkStream = walkStream;
  23219. function getSettings(settingsOrOptions = {}) {
  23220. if (settingsOrOptions instanceof settings_1$1.default) {
  23221. return settingsOrOptions;
  23222. }
  23223. return new settings_1$1.default(settingsOrOptions);
  23224. }
  23225. var reader = {};
  23226. Object.defineProperty(reader, "__esModule", { value: true });
  23227. const path$c = require$$0$4;
  23228. const fsStat$2 = out$1;
  23229. const utils$6 = utils$g;
  23230. class Reader {
  23231. constructor(_settings) {
  23232. this._settings = _settings;
  23233. this._fsStatSettings = new fsStat$2.Settings({
  23234. followSymbolicLink: this._settings.followSymbolicLinks,
  23235. fs: this._settings.fs,
  23236. throwErrorOnBrokenSymbolicLink: this._settings.followSymbolicLinks
  23237. });
  23238. }
  23239. _getFullEntryPath(filepath) {
  23240. return path$c.resolve(this._settings.cwd, filepath);
  23241. }
  23242. _makeEntry(stats, pattern) {
  23243. const entry = {
  23244. name: pattern,
  23245. path: pattern,
  23246. dirent: utils$6.fs.createDirentFromStats(pattern, stats)
  23247. };
  23248. if (this._settings.stats) {
  23249. entry.stats = stats;
  23250. }
  23251. return entry;
  23252. }
  23253. _isFatalError(error) {
  23254. return !utils$6.errno.isEnoentCodeError(error) && !this._settings.suppressErrors;
  23255. }
  23256. }
  23257. reader.default = Reader;
  23258. var stream$1 = {};
  23259. Object.defineProperty(stream$1, "__esModule", { value: true });
  23260. const stream_1$3 = require$$0$6;
  23261. const fsStat$1 = out$1;
  23262. const fsWalk$2 = out$3;
  23263. const reader_1$2 = reader;
  23264. class ReaderStream extends reader_1$2.default {
  23265. constructor() {
  23266. super(...arguments);
  23267. this._walkStream = fsWalk$2.walkStream;
  23268. this._stat = fsStat$1.stat;
  23269. }
  23270. dynamic(root, options) {
  23271. return this._walkStream(root, options);
  23272. }
  23273. static(patterns, options) {
  23274. const filepaths = patterns.map(this._getFullEntryPath, this);
  23275. const stream = new stream_1$3.PassThrough({ objectMode: true });
  23276. stream._write = (index, _enc, done) => {
  23277. return this._getEntry(filepaths[index], patterns[index], options)
  23278. .then((entry) => {
  23279. if (entry !== null && options.entryFilter(entry)) {
  23280. stream.push(entry);
  23281. }
  23282. if (index === filepaths.length - 1) {
  23283. stream.end();
  23284. }
  23285. done();
  23286. })
  23287. .catch(done);
  23288. };
  23289. for (let i = 0; i < filepaths.length; i++) {
  23290. stream.write(i);
  23291. }
  23292. return stream;
  23293. }
  23294. _getEntry(filepath, pattern, options) {
  23295. return this._getStat(filepath)
  23296. .then((stats) => this._makeEntry(stats, pattern))
  23297. .catch((error) => {
  23298. if (options.errorFilter(error)) {
  23299. return null;
  23300. }
  23301. throw error;
  23302. });
  23303. }
  23304. _getStat(filepath) {
  23305. return new Promise((resolve, reject) => {
  23306. this._stat(filepath, this._fsStatSettings, (error, stats) => {
  23307. return error === null ? resolve(stats) : reject(error);
  23308. });
  23309. });
  23310. }
  23311. }
  23312. stream$1.default = ReaderStream;
  23313. Object.defineProperty(async$6, "__esModule", { value: true });
  23314. const fsWalk$1 = out$3;
  23315. const reader_1$1 = reader;
  23316. const stream_1$2 = stream$1;
  23317. class ReaderAsync extends reader_1$1.default {
  23318. constructor() {
  23319. super(...arguments);
  23320. this._walkAsync = fsWalk$1.walk;
  23321. this._readerStream = new stream_1$2.default(this._settings);
  23322. }
  23323. dynamic(root, options) {
  23324. return new Promise((resolve, reject) => {
  23325. this._walkAsync(root, options, (error, entries) => {
  23326. if (error === null) {
  23327. resolve(entries);
  23328. }
  23329. else {
  23330. reject(error);
  23331. }
  23332. });
  23333. });
  23334. }
  23335. async static(patterns, options) {
  23336. const entries = [];
  23337. const stream = this._readerStream.static(patterns, options);
  23338. // After #235, replace it with an asynchronous iterator.
  23339. return new Promise((resolve, reject) => {
  23340. stream.once('error', reject);
  23341. stream.on('data', (entry) => entries.push(entry));
  23342. stream.once('end', () => resolve(entries));
  23343. });
  23344. }
  23345. }
  23346. async$6.default = ReaderAsync;
  23347. var provider = {};
  23348. var deep = {};
  23349. var partial = {};
  23350. var matcher = {};
  23351. Object.defineProperty(matcher, "__esModule", { value: true });
  23352. const utils$5 = utils$g;
  23353. class Matcher {
  23354. constructor(_patterns, _settings, _micromatchOptions) {
  23355. this._patterns = _patterns;
  23356. this._settings = _settings;
  23357. this._micromatchOptions = _micromatchOptions;
  23358. this._storage = [];
  23359. this._fillStorage();
  23360. }
  23361. _fillStorage() {
  23362. for (const pattern of this._patterns) {
  23363. const segments = this._getPatternSegments(pattern);
  23364. const sections = this._splitSegmentsIntoSections(segments);
  23365. this._storage.push({
  23366. complete: sections.length <= 1,
  23367. pattern,
  23368. segments,
  23369. sections
  23370. });
  23371. }
  23372. }
  23373. _getPatternSegments(pattern) {
  23374. const parts = utils$5.pattern.getPatternParts(pattern, this._micromatchOptions);
  23375. return parts.map((part) => {
  23376. const dynamic = utils$5.pattern.isDynamicPattern(part, this._settings);
  23377. if (!dynamic) {
  23378. return {
  23379. dynamic: false,
  23380. pattern: part
  23381. };
  23382. }
  23383. return {
  23384. dynamic: true,
  23385. pattern: part,
  23386. patternRe: utils$5.pattern.makeRe(part, this._micromatchOptions)
  23387. };
  23388. });
  23389. }
  23390. _splitSegmentsIntoSections(segments) {
  23391. return utils$5.array.splitWhen(segments, (segment) => segment.dynamic && utils$5.pattern.hasGlobStar(segment.pattern));
  23392. }
  23393. }
  23394. matcher.default = Matcher;
  23395. Object.defineProperty(partial, "__esModule", { value: true });
  23396. const matcher_1 = matcher;
  23397. class PartialMatcher extends matcher_1.default {
  23398. match(filepath) {
  23399. const parts = filepath.split('/');
  23400. const levels = parts.length;
  23401. const patterns = this._storage.filter((info) => !info.complete || info.segments.length > levels);
  23402. for (const pattern of patterns) {
  23403. const section = pattern.sections[0];
  23404. /**
  23405. * In this case, the pattern has a globstar and we must read all directories unconditionally,
  23406. * but only if the level has reached the end of the first group.
  23407. *
  23408. * fixtures/{a,b}/**
  23409. * ^ true/false ^ always true
  23410. */
  23411. if (!pattern.complete && levels > section.length) {
  23412. return true;
  23413. }
  23414. const match = parts.every((part, index) => {
  23415. const segment = pattern.segments[index];
  23416. if (segment.dynamic && segment.patternRe.test(part)) {
  23417. return true;
  23418. }
  23419. if (!segment.dynamic && segment.pattern === part) {
  23420. return true;
  23421. }
  23422. return false;
  23423. });
  23424. if (match) {
  23425. return true;
  23426. }
  23427. }
  23428. return false;
  23429. }
  23430. }
  23431. partial.default = PartialMatcher;
  23432. Object.defineProperty(deep, "__esModule", { value: true });
  23433. const utils$4 = utils$g;
  23434. const partial_1 = partial;
  23435. class DeepFilter {
  23436. constructor(_settings, _micromatchOptions) {
  23437. this._settings = _settings;
  23438. this._micromatchOptions = _micromatchOptions;
  23439. }
  23440. getFilter(basePath, positive, negative) {
  23441. const matcher = this._getMatcher(positive);
  23442. const negativeRe = this._getNegativePatternsRe(negative);
  23443. return (entry) => this._filter(basePath, entry, matcher, negativeRe);
  23444. }
  23445. _getMatcher(patterns) {
  23446. return new partial_1.default(patterns, this._settings, this._micromatchOptions);
  23447. }
  23448. _getNegativePatternsRe(patterns) {
  23449. const affectDepthOfReadingPatterns = patterns.filter(utils$4.pattern.isAffectDepthOfReadingPattern);
  23450. return utils$4.pattern.convertPatternsToRe(affectDepthOfReadingPatterns, this._micromatchOptions);
  23451. }
  23452. _filter(basePath, entry, matcher, negativeRe) {
  23453. if (this._isSkippedByDeep(basePath, entry.path)) {
  23454. return false;
  23455. }
  23456. if (this._isSkippedSymbolicLink(entry)) {
  23457. return false;
  23458. }
  23459. const filepath = utils$4.path.removeLeadingDotSegment(entry.path);
  23460. if (this._isSkippedByPositivePatterns(filepath, matcher)) {
  23461. return false;
  23462. }
  23463. return this._isSkippedByNegativePatterns(filepath, negativeRe);
  23464. }
  23465. _isSkippedByDeep(basePath, entryPath) {
  23466. /**
  23467. * Avoid unnecessary depth calculations when it doesn't matter.
  23468. */
  23469. if (this._settings.deep === Infinity) {
  23470. return false;
  23471. }
  23472. return this._getEntryLevel(basePath, entryPath) >= this._settings.deep;
  23473. }
  23474. _getEntryLevel(basePath, entryPath) {
  23475. const entryPathDepth = entryPath.split('/').length;
  23476. if (basePath === '') {
  23477. return entryPathDepth;
  23478. }
  23479. const basePathDepth = basePath.split('/').length;
  23480. return entryPathDepth - basePathDepth;
  23481. }
  23482. _isSkippedSymbolicLink(entry) {
  23483. return !this._settings.followSymbolicLinks && entry.dirent.isSymbolicLink();
  23484. }
  23485. _isSkippedByPositivePatterns(entryPath, matcher) {
  23486. return !this._settings.baseNameMatch && !matcher.match(entryPath);
  23487. }
  23488. _isSkippedByNegativePatterns(entryPath, patternsRe) {
  23489. return !utils$4.pattern.matchAny(entryPath, patternsRe);
  23490. }
  23491. }
  23492. deep.default = DeepFilter;
  23493. var entry$1 = {};
  23494. Object.defineProperty(entry$1, "__esModule", { value: true });
  23495. const utils$3 = utils$g;
  23496. class EntryFilter {
  23497. constructor(_settings, _micromatchOptions) {
  23498. this._settings = _settings;
  23499. this._micromatchOptions = _micromatchOptions;
  23500. this.index = new Map();
  23501. }
  23502. getFilter(positive, negative) {
  23503. const positiveRe = utils$3.pattern.convertPatternsToRe(positive, this._micromatchOptions);
  23504. const negativeRe = utils$3.pattern.convertPatternsToRe(negative, Object.assign(Object.assign({}, this._micromatchOptions), { dot: true }));
  23505. return (entry) => this._filter(entry, positiveRe, negativeRe);
  23506. }
  23507. _filter(entry, positiveRe, negativeRe) {
  23508. const filepath = utils$3.path.removeLeadingDotSegment(entry.path);
  23509. if (this._settings.unique && this._isDuplicateEntry(filepath)) {
  23510. return false;
  23511. }
  23512. if (this._onlyFileFilter(entry) || this._onlyDirectoryFilter(entry)) {
  23513. return false;
  23514. }
  23515. if (this._isSkippedByAbsoluteNegativePatterns(filepath, negativeRe)) {
  23516. return false;
  23517. }
  23518. const isDirectory = entry.dirent.isDirectory();
  23519. const isMatched = this._isMatchToPatterns(filepath, positiveRe, isDirectory) && !this._isMatchToPatterns(filepath, negativeRe, isDirectory);
  23520. if (this._settings.unique && isMatched) {
  23521. this._createIndexRecord(filepath);
  23522. }
  23523. return isMatched;
  23524. }
  23525. _isDuplicateEntry(filepath) {
  23526. return this.index.has(filepath);
  23527. }
  23528. _createIndexRecord(filepath) {
  23529. this.index.set(filepath, undefined);
  23530. }
  23531. _onlyFileFilter(entry) {
  23532. return this._settings.onlyFiles && !entry.dirent.isFile();
  23533. }
  23534. _onlyDirectoryFilter(entry) {
  23535. return this._settings.onlyDirectories && !entry.dirent.isDirectory();
  23536. }
  23537. _isSkippedByAbsoluteNegativePatterns(entryPath, patternsRe) {
  23538. if (!this._settings.absolute) {
  23539. return false;
  23540. }
  23541. const fullpath = utils$3.path.makeAbsolute(this._settings.cwd, entryPath);
  23542. return utils$3.pattern.matchAny(fullpath, patternsRe);
  23543. }
  23544. _isMatchToPatterns(filepath, patternsRe, isDirectory) {
  23545. // Trying to match files and directories by patterns.
  23546. const isMatched = utils$3.pattern.matchAny(filepath, patternsRe);
  23547. // A pattern with a trailling slash can be used for directory matching.
  23548. // To apply such pattern, we need to add a tralling slash to the path.
  23549. if (!isMatched && isDirectory) {
  23550. return utils$3.pattern.matchAny(filepath + '/', patternsRe);
  23551. }
  23552. return isMatched;
  23553. }
  23554. }
  23555. entry$1.default = EntryFilter;
  23556. var error$1 = {};
  23557. Object.defineProperty(error$1, "__esModule", { value: true });
  23558. const utils$2 = utils$g;
  23559. class ErrorFilter {
  23560. constructor(_settings) {
  23561. this._settings = _settings;
  23562. }
  23563. getFilter() {
  23564. return (error) => this._isNonFatalError(error);
  23565. }
  23566. _isNonFatalError(error) {
  23567. return utils$2.errno.isEnoentCodeError(error) || this._settings.suppressErrors;
  23568. }
  23569. }
  23570. error$1.default = ErrorFilter;
  23571. var entry = {};
  23572. Object.defineProperty(entry, "__esModule", { value: true });
  23573. const utils$1 = utils$g;
  23574. class EntryTransformer {
  23575. constructor(_settings) {
  23576. this._settings = _settings;
  23577. }
  23578. getTransformer() {
  23579. return (entry) => this._transform(entry);
  23580. }
  23581. _transform(entry) {
  23582. let filepath = entry.path;
  23583. if (this._settings.absolute) {
  23584. filepath = utils$1.path.makeAbsolute(this._settings.cwd, filepath);
  23585. filepath = utils$1.path.unixify(filepath);
  23586. }
  23587. if (this._settings.markDirectories && entry.dirent.isDirectory()) {
  23588. filepath += '/';
  23589. }
  23590. if (!this._settings.objectMode) {
  23591. return filepath;
  23592. }
  23593. return Object.assign(Object.assign({}, entry), { path: filepath });
  23594. }
  23595. }
  23596. entry.default = EntryTransformer;
  23597. Object.defineProperty(provider, "__esModule", { value: true });
  23598. const path$b = require$$0$4;
  23599. const deep_1 = deep;
  23600. const entry_1 = entry$1;
  23601. const error_1 = error$1;
  23602. const entry_2 = entry;
  23603. class Provider {
  23604. constructor(_settings) {
  23605. this._settings = _settings;
  23606. this.errorFilter = new error_1.default(this._settings);
  23607. this.entryFilter = new entry_1.default(this._settings, this._getMicromatchOptions());
  23608. this.deepFilter = new deep_1.default(this._settings, this._getMicromatchOptions());
  23609. this.entryTransformer = new entry_2.default(this._settings);
  23610. }
  23611. _getRootDirectory(task) {
  23612. return path$b.resolve(this._settings.cwd, task.base);
  23613. }
  23614. _getReaderOptions(task) {
  23615. const basePath = task.base === '.' ? '' : task.base;
  23616. return {
  23617. basePath,
  23618. pathSegmentSeparator: '/',
  23619. concurrency: this._settings.concurrency,
  23620. deepFilter: this.deepFilter.getFilter(basePath, task.positive, task.negative),
  23621. entryFilter: this.entryFilter.getFilter(task.positive, task.negative),
  23622. errorFilter: this.errorFilter.getFilter(),
  23623. followSymbolicLinks: this._settings.followSymbolicLinks,
  23624. fs: this._settings.fs,
  23625. stats: this._settings.stats,
  23626. throwErrorOnBrokenSymbolicLink: this._settings.throwErrorOnBrokenSymbolicLink,
  23627. transform: this.entryTransformer.getTransformer()
  23628. };
  23629. }
  23630. _getMicromatchOptions() {
  23631. return {
  23632. dot: this._settings.dot,
  23633. matchBase: this._settings.baseNameMatch,
  23634. nobrace: !this._settings.braceExpansion,
  23635. nocase: !this._settings.caseSensitiveMatch,
  23636. noext: !this._settings.extglob,
  23637. noglobstar: !this._settings.globstar,
  23638. posix: true,
  23639. strictSlashes: false
  23640. };
  23641. }
  23642. }
  23643. provider.default = Provider;
  23644. Object.defineProperty(async$7, "__esModule", { value: true });
  23645. const async_1$1 = async$6;
  23646. const provider_1$2 = provider;
  23647. class ProviderAsync extends provider_1$2.default {
  23648. constructor() {
  23649. super(...arguments);
  23650. this._reader = new async_1$1.default(this._settings);
  23651. }
  23652. async read(task) {
  23653. const root = this._getRootDirectory(task);
  23654. const options = this._getReaderOptions(task);
  23655. const entries = await this.api(root, task, options);
  23656. return entries.map((entry) => options.transform(entry));
  23657. }
  23658. api(root, task, options) {
  23659. if (task.dynamic) {
  23660. return this._reader.dynamic(root, options);
  23661. }
  23662. return this._reader.static(task.patterns, options);
  23663. }
  23664. }
  23665. async$7.default = ProviderAsync;
  23666. var stream = {};
  23667. Object.defineProperty(stream, "__esModule", { value: true });
  23668. const stream_1$1 = require$$0$6;
  23669. const stream_2 = stream$1;
  23670. const provider_1$1 = provider;
  23671. class ProviderStream extends provider_1$1.default {
  23672. constructor() {
  23673. super(...arguments);
  23674. this._reader = new stream_2.default(this._settings);
  23675. }
  23676. read(task) {
  23677. const root = this._getRootDirectory(task);
  23678. const options = this._getReaderOptions(task);
  23679. const source = this.api(root, task, options);
  23680. const destination = new stream_1$1.Readable({ objectMode: true, read: () => { } });
  23681. source
  23682. .once('error', (error) => destination.emit('error', error))
  23683. .on('data', (entry) => destination.emit('data', options.transform(entry)))
  23684. .once('end', () => destination.emit('end'));
  23685. destination
  23686. .once('close', () => source.destroy());
  23687. return destination;
  23688. }
  23689. api(root, task, options) {
  23690. if (task.dynamic) {
  23691. return this._reader.dynamic(root, options);
  23692. }
  23693. return this._reader.static(task.patterns, options);
  23694. }
  23695. }
  23696. stream.default = ProviderStream;
  23697. var sync$2 = {};
  23698. var sync$1 = {};
  23699. Object.defineProperty(sync$1, "__esModule", { value: true });
  23700. const fsStat = out$1;
  23701. const fsWalk = out$3;
  23702. const reader_1 = reader;
  23703. class ReaderSync extends reader_1.default {
  23704. constructor() {
  23705. super(...arguments);
  23706. this._walkSync = fsWalk.walkSync;
  23707. this._statSync = fsStat.statSync;
  23708. }
  23709. dynamic(root, options) {
  23710. return this._walkSync(root, options);
  23711. }
  23712. static(patterns, options) {
  23713. const entries = [];
  23714. for (const pattern of patterns) {
  23715. const filepath = this._getFullEntryPath(pattern);
  23716. const entry = this._getEntry(filepath, pattern, options);
  23717. if (entry === null || !options.entryFilter(entry)) {
  23718. continue;
  23719. }
  23720. entries.push(entry);
  23721. }
  23722. return entries;
  23723. }
  23724. _getEntry(filepath, pattern, options) {
  23725. try {
  23726. const stats = this._getStat(filepath);
  23727. return this._makeEntry(stats, pattern);
  23728. }
  23729. catch (error) {
  23730. if (options.errorFilter(error)) {
  23731. return null;
  23732. }
  23733. throw error;
  23734. }
  23735. }
  23736. _getStat(filepath) {
  23737. return this._statSync(filepath, this._fsStatSettings);
  23738. }
  23739. }
  23740. sync$1.default = ReaderSync;
  23741. Object.defineProperty(sync$2, "__esModule", { value: true });
  23742. const sync_1$1 = sync$1;
  23743. const provider_1 = provider;
  23744. class ProviderSync extends provider_1.default {
  23745. constructor() {
  23746. super(...arguments);
  23747. this._reader = new sync_1$1.default(this._settings);
  23748. }
  23749. read(task) {
  23750. const root = this._getRootDirectory(task);
  23751. const options = this._getReaderOptions(task);
  23752. const entries = this.api(root, task, options);
  23753. return entries.map(options.transform);
  23754. }
  23755. api(root, task, options) {
  23756. if (task.dynamic) {
  23757. return this._reader.dynamic(root, options);
  23758. }
  23759. return this._reader.static(task.patterns, options);
  23760. }
  23761. }
  23762. sync$2.default = ProviderSync;
  23763. var settings = {};
  23764. (function (exports) {
  23765. Object.defineProperty(exports, "__esModule", { value: true });
  23766. exports.DEFAULT_FILE_SYSTEM_ADAPTER = void 0;
  23767. const fs = require$$0__default;
  23768. const os = require$$2;
  23769. /**
  23770. * The `os.cpus` method can return zero. We expect the number of cores to be greater than zero.
  23771. * https://github.com/nodejs/node/blob/7faeddf23a98c53896f8b574a6e66589e8fb1eb8/lib/os.js#L106-L107
  23772. */
  23773. const CPU_COUNT = Math.max(os.cpus().length, 1);
  23774. exports.DEFAULT_FILE_SYSTEM_ADAPTER = {
  23775. lstat: fs.lstat,
  23776. lstatSync: fs.lstatSync,
  23777. stat: fs.stat,
  23778. statSync: fs.statSync,
  23779. readdir: fs.readdir,
  23780. readdirSync: fs.readdirSync
  23781. };
  23782. class Settings {
  23783. constructor(_options = {}) {
  23784. this._options = _options;
  23785. this.absolute = this._getValue(this._options.absolute, false);
  23786. this.baseNameMatch = this._getValue(this._options.baseNameMatch, false);
  23787. this.braceExpansion = this._getValue(this._options.braceExpansion, true);
  23788. this.caseSensitiveMatch = this._getValue(this._options.caseSensitiveMatch, true);
  23789. this.concurrency = this._getValue(this._options.concurrency, CPU_COUNT);
  23790. this.cwd = this._getValue(this._options.cwd, process.cwd());
  23791. this.deep = this._getValue(this._options.deep, Infinity);
  23792. this.dot = this._getValue(this._options.dot, false);
  23793. this.extglob = this._getValue(this._options.extglob, true);
  23794. this.followSymbolicLinks = this._getValue(this._options.followSymbolicLinks, true);
  23795. this.fs = this._getFileSystemMethods(this._options.fs);
  23796. this.globstar = this._getValue(this._options.globstar, true);
  23797. this.ignore = this._getValue(this._options.ignore, []);
  23798. this.markDirectories = this._getValue(this._options.markDirectories, false);
  23799. this.objectMode = this._getValue(this._options.objectMode, false);
  23800. this.onlyDirectories = this._getValue(this._options.onlyDirectories, false);
  23801. this.onlyFiles = this._getValue(this._options.onlyFiles, true);
  23802. this.stats = this._getValue(this._options.stats, false);
  23803. this.suppressErrors = this._getValue(this._options.suppressErrors, false);
  23804. this.throwErrorOnBrokenSymbolicLink = this._getValue(this._options.throwErrorOnBrokenSymbolicLink, false);
  23805. this.unique = this._getValue(this._options.unique, true);
  23806. if (this.onlyDirectories) {
  23807. this.onlyFiles = false;
  23808. }
  23809. if (this.stats) {
  23810. this.objectMode = true;
  23811. }
  23812. // Remove the cast to the array in the next major (#404).
  23813. this.ignore = [].concat(this.ignore);
  23814. }
  23815. _getValue(option, value) {
  23816. return option === undefined ? value : option;
  23817. }
  23818. _getFileSystemMethods(methods = {}) {
  23819. return Object.assign(Object.assign({}, exports.DEFAULT_FILE_SYSTEM_ADAPTER), methods);
  23820. }
  23821. }
  23822. exports.default = Settings;
  23823. } (settings));
  23824. const taskManager = tasks;
  23825. const async_1 = async$7;
  23826. const stream_1 = stream;
  23827. const sync_1 = sync$2;
  23828. const settings_1 = settings;
  23829. const utils = utils$g;
  23830. async function FastGlob(source, options) {
  23831. assertPatternsInput(source);
  23832. const works = getWorks(source, async_1.default, options);
  23833. const result = await Promise.all(works);
  23834. return utils.array.flatten(result);
  23835. }
  23836. // https://github.com/typescript-eslint/typescript-eslint/issues/60
  23837. // eslint-disable-next-line no-redeclare
  23838. (function (FastGlob) {
  23839. FastGlob.glob = FastGlob;
  23840. FastGlob.globSync = sync;
  23841. FastGlob.globStream = stream;
  23842. FastGlob.async = FastGlob;
  23843. function sync(source, options) {
  23844. assertPatternsInput(source);
  23845. const works = getWorks(source, sync_1.default, options);
  23846. return utils.array.flatten(works);
  23847. }
  23848. FastGlob.sync = sync;
  23849. function stream(source, options) {
  23850. assertPatternsInput(source);
  23851. const works = getWorks(source, stream_1.default, options);
  23852. /**
  23853. * The stream returned by the provider cannot work with an asynchronous iterator.
  23854. * To support asynchronous iterators, regardless of the number of tasks, we always multiplex streams.
  23855. * This affects performance (+25%). I don't see best solution right now.
  23856. */
  23857. return utils.stream.merge(works);
  23858. }
  23859. FastGlob.stream = stream;
  23860. function generateTasks(source, options) {
  23861. assertPatternsInput(source);
  23862. const patterns = [].concat(source);
  23863. const settings = new settings_1.default(options);
  23864. return taskManager.generate(patterns, settings);
  23865. }
  23866. FastGlob.generateTasks = generateTasks;
  23867. function isDynamicPattern(source, options) {
  23868. assertPatternsInput(source);
  23869. const settings = new settings_1.default(options);
  23870. return utils.pattern.isDynamicPattern(source, settings);
  23871. }
  23872. FastGlob.isDynamicPattern = isDynamicPattern;
  23873. function escapePath(source) {
  23874. assertPatternsInput(source);
  23875. return utils.path.escape(source);
  23876. }
  23877. FastGlob.escapePath = escapePath;
  23878. function convertPathToPattern(source) {
  23879. assertPatternsInput(source);
  23880. return utils.path.convertPathToPattern(source);
  23881. }
  23882. FastGlob.convertPathToPattern = convertPathToPattern;
  23883. (function (posix) {
  23884. function escapePath(source) {
  23885. assertPatternsInput(source);
  23886. return utils.path.escapePosixPath(source);
  23887. }
  23888. posix.escapePath = escapePath;
  23889. function convertPathToPattern(source) {
  23890. assertPatternsInput(source);
  23891. return utils.path.convertPosixPathToPattern(source);
  23892. }
  23893. posix.convertPathToPattern = convertPathToPattern;
  23894. })(FastGlob.posix || (FastGlob.posix = {}));
  23895. (function (win32) {
  23896. function escapePath(source) {
  23897. assertPatternsInput(source);
  23898. return utils.path.escapeWindowsPath(source);
  23899. }
  23900. win32.escapePath = escapePath;
  23901. function convertPathToPattern(source) {
  23902. assertPatternsInput(source);
  23903. return utils.path.convertWindowsPathToPattern(source);
  23904. }
  23905. win32.convertPathToPattern = convertPathToPattern;
  23906. })(FastGlob.win32 || (FastGlob.win32 = {}));
  23907. })(FastGlob || (FastGlob = {}));
  23908. function getWorks(source, _Provider, options) {
  23909. const patterns = [].concat(source);
  23910. const settings = new settings_1.default(options);
  23911. const tasks = taskManager.generate(patterns, settings);
  23912. const provider = new _Provider(settings);
  23913. return tasks.map(provider.read, provider);
  23914. }
  23915. function assertPatternsInput(input) {
  23916. const source = [].concat(input);
  23917. const isValidSource = source.every((item) => utils.string.isString(item) && !utils.string.isEmpty(item));
  23918. if (!isValidSource) {
  23919. throw new TypeError('Patterns must be a string (non empty) or an array of strings');
  23920. }
  23921. }
  23922. var out = FastGlob;
  23923. var glob = /*@__PURE__*/getDefaultExportFromCjs(out);
  23924. var src$2 = {};
  23925. // @ts-check
  23926. const path$a = require$$0$4;
  23927. const fs$a = require$$0__default;
  23928. const os$3 = require$$2;
  23929. const fsReadFileAsync = fs$a.promises.readFile;
  23930. /** @type {(name: string, sync: boolean) => string[]} */
  23931. function getDefaultSearchPlaces(name, sync) {
  23932. return [
  23933. 'package.json',
  23934. `.${name}rc.json`,
  23935. `.${name}rc.js`,
  23936. `.${name}rc.cjs`,
  23937. ...(sync ? [] : [`.${name}rc.mjs`]),
  23938. `.config/${name}rc`,
  23939. `.config/${name}rc.json`,
  23940. `.config/${name}rc.js`,
  23941. `.config/${name}rc.cjs`,
  23942. ...(sync ? [] : [`.config/${name}rc.mjs`]),
  23943. `${name}.config.js`,
  23944. `${name}.config.cjs`,
  23945. ...(sync ? [] : [`${name}.config.mjs`]),
  23946. ];
  23947. }
  23948. /**
  23949. * @type {(p: string) => string}
  23950. *
  23951. * see #17
  23952. * On *nix, if cwd is not under homedir,
  23953. * the last path will be '', ('/build' -> '')
  23954. * but it should be '/' actually.
  23955. * And on Windows, this will never happen. ('C:\build' -> 'C:')
  23956. */
  23957. function parentDir(p) {
  23958. return path$a.dirname(p) || path$a.sep;
  23959. }
  23960. /** @type {import('./index').LoaderSync} */
  23961. const jsonLoader = (_, content) => JSON.parse(content);
  23962. // Use plain require in webpack context for dynamic import
  23963. const requireFunc = typeof __webpack_require__ === "function" ? __non_webpack_require__ : __require;
  23964. /** @type {import('./index').LoadersSync} */
  23965. const defaultLoadersSync = Object.freeze({
  23966. '.js': requireFunc,
  23967. '.json': requireFunc,
  23968. '.cjs': requireFunc,
  23969. noExt: jsonLoader,
  23970. });
  23971. src$2.defaultLoadersSync = defaultLoadersSync;
  23972. /** @type {import('./index').Loader} */
  23973. const dynamicImport = async id => {
  23974. try {
  23975. const mod = await import(/* webpackIgnore: true */ id);
  23976. return mod.default;
  23977. } catch (e) {
  23978. try {
  23979. return requireFunc(id);
  23980. } catch (/** @type {any} */ requireE) {
  23981. if (
  23982. requireE.code === 'ERR_REQUIRE_ESM' ||
  23983. (requireE instanceof SyntaxError &&
  23984. requireE
  23985. .toString()
  23986. .includes('Cannot use import statement outside a module'))
  23987. ) {
  23988. throw e;
  23989. }
  23990. throw requireE;
  23991. }
  23992. }
  23993. };
  23994. /** @type {import('./index').Loaders} */
  23995. const defaultLoaders = Object.freeze({
  23996. '.js': dynamicImport,
  23997. '.mjs': dynamicImport,
  23998. '.cjs': dynamicImport,
  23999. '.json': jsonLoader,
  24000. noExt: jsonLoader,
  24001. });
  24002. src$2.defaultLoaders = defaultLoaders;
  24003. /**
  24004. * @param {string} name
  24005. * @param {import('./index').Options | import('./index').OptionsSync} options
  24006. * @param {boolean} sync
  24007. * @returns {Required<import('./index').Options | import('./index').OptionsSync>}
  24008. */
  24009. function getOptions(name, options, sync) {
  24010. /** @type {Required<import('./index').Options>} */
  24011. const conf = {
  24012. stopDir: os$3.homedir(),
  24013. searchPlaces: getDefaultSearchPlaces(name, sync),
  24014. ignoreEmptySearchPlaces: true,
  24015. cache: true,
  24016. transform: x => x,
  24017. packageProp: [name],
  24018. ...options,
  24019. loaders: {
  24020. ...(sync ? defaultLoadersSync : defaultLoaders),
  24021. ...options.loaders,
  24022. },
  24023. };
  24024. conf.searchPlaces.forEach(place => {
  24025. const key = path$a.extname(place) || 'noExt';
  24026. const loader = conf.loaders[key];
  24027. if (!loader) {
  24028. throw new Error(`Missing loader for extension "${place}"`);
  24029. }
  24030. if (typeof loader !== 'function') {
  24031. throw new Error(
  24032. `Loader for extension "${place}" is not a function: Received ${typeof loader}.`,
  24033. );
  24034. }
  24035. });
  24036. return conf;
  24037. }
  24038. /** @type {(props: string | string[], obj: Record<string, any>) => unknown} */
  24039. function getPackageProp(props, obj) {
  24040. if (typeof props === 'string' && props in obj) return obj[props];
  24041. return (
  24042. (Array.isArray(props) ? props : props.split('.')).reduce(
  24043. (acc, prop) => (acc === undefined ? acc : acc[prop]),
  24044. obj,
  24045. ) || null
  24046. );
  24047. }
  24048. /** @param {string} filepath */
  24049. function validateFilePath(filepath) {
  24050. if (!filepath) throw new Error('load must pass a non-empty string');
  24051. }
  24052. /** @type {(loader: import('./index').Loader, ext: string) => void} */
  24053. function validateLoader(loader, ext) {
  24054. if (!loader) throw new Error(`No loader specified for extension "${ext}"`);
  24055. if (typeof loader !== 'function') throw new Error('loader is not a function');
  24056. }
  24057. /** @type {(enableCache: boolean) => <T>(c: Map<string, T>, filepath: string, res: T) => T} */
  24058. const makeEmplace = enableCache => (c, filepath, res) => {
  24059. if (enableCache) c.set(filepath, res);
  24060. return res;
  24061. };
  24062. /** @type {import('./index').lilconfig} */
  24063. src$2.lilconfig = function lilconfig(name, options) {
  24064. const {
  24065. ignoreEmptySearchPlaces,
  24066. loaders,
  24067. packageProp,
  24068. searchPlaces,
  24069. stopDir,
  24070. transform,
  24071. cache,
  24072. } = getOptions(name, options ?? {}, false);
  24073. const searchCache = new Map();
  24074. const loadCache = new Map();
  24075. const emplace = makeEmplace(cache);
  24076. return {
  24077. async search(searchFrom = process.cwd()) {
  24078. /** @type {import('./index').LilconfigResult} */
  24079. const result = {
  24080. config: null,
  24081. filepath: '',
  24082. };
  24083. /** @type {Set<string>} */
  24084. const visited = new Set();
  24085. let dir = searchFrom;
  24086. dirLoop: while (true) {
  24087. if (cache) {
  24088. const r = searchCache.get(dir);
  24089. if (r !== undefined) {
  24090. for (const p of visited) searchCache.set(p, r);
  24091. return r;
  24092. }
  24093. visited.add(dir);
  24094. }
  24095. for (const searchPlace of searchPlaces) {
  24096. const filepath = path$a.join(dir, searchPlace);
  24097. try {
  24098. await fs$a.promises.access(filepath);
  24099. } catch {
  24100. continue;
  24101. }
  24102. const content = String(await fsReadFileAsync(filepath));
  24103. const loaderKey = path$a.extname(searchPlace) || 'noExt';
  24104. const loader = loaders[loaderKey];
  24105. // handle package.json
  24106. if (searchPlace === 'package.json') {
  24107. const pkg = await loader(filepath, content);
  24108. const maybeConfig = getPackageProp(packageProp, pkg);
  24109. if (maybeConfig != null) {
  24110. result.config = maybeConfig;
  24111. result.filepath = filepath;
  24112. break dirLoop;
  24113. }
  24114. continue;
  24115. }
  24116. // handle other type of configs
  24117. const isEmpty = content.trim() === '';
  24118. if (isEmpty && ignoreEmptySearchPlaces) continue;
  24119. if (isEmpty) {
  24120. result.isEmpty = true;
  24121. result.config = undefined;
  24122. } else {
  24123. validateLoader(loader, loaderKey);
  24124. result.config = await loader(filepath, content);
  24125. }
  24126. result.filepath = filepath;
  24127. break dirLoop;
  24128. }
  24129. if (dir === stopDir || dir === parentDir(dir)) break dirLoop;
  24130. dir = parentDir(dir);
  24131. }
  24132. const transformed =
  24133. // not found
  24134. result.filepath === '' && result.config === null
  24135. ? transform(null)
  24136. : transform(result);
  24137. if (cache) {
  24138. for (const p of visited) searchCache.set(p, transformed);
  24139. }
  24140. return transformed;
  24141. },
  24142. async load(filepath) {
  24143. validateFilePath(filepath);
  24144. const absPath = path$a.resolve(process.cwd(), filepath);
  24145. if (cache && loadCache.has(absPath)) {
  24146. return loadCache.get(absPath);
  24147. }
  24148. const {base, ext} = path$a.parse(absPath);
  24149. const loaderKey = ext || 'noExt';
  24150. const loader = loaders[loaderKey];
  24151. validateLoader(loader, loaderKey);
  24152. const content = String(await fsReadFileAsync(absPath));
  24153. if (base === 'package.json') {
  24154. const pkg = await loader(absPath, content);
  24155. return emplace(
  24156. loadCache,
  24157. absPath,
  24158. transform({
  24159. config: getPackageProp(packageProp, pkg),
  24160. filepath: absPath,
  24161. }),
  24162. );
  24163. }
  24164. /** @type {import('./index').LilconfigResult} */
  24165. const result = {
  24166. config: null,
  24167. filepath: absPath,
  24168. };
  24169. // handle other type of configs
  24170. const isEmpty = content.trim() === '';
  24171. if (isEmpty && ignoreEmptySearchPlaces)
  24172. return emplace(
  24173. loadCache,
  24174. absPath,
  24175. transform({
  24176. config: undefined,
  24177. filepath: absPath,
  24178. isEmpty: true,
  24179. }),
  24180. );
  24181. // cosmiconfig returns undefined for empty files
  24182. result.config = isEmpty ? undefined : await loader(absPath, content);
  24183. return emplace(
  24184. loadCache,
  24185. absPath,
  24186. transform(isEmpty ? {...result, isEmpty, config: undefined} : result),
  24187. );
  24188. },
  24189. clearLoadCache() {
  24190. if (cache) loadCache.clear();
  24191. },
  24192. clearSearchCache() {
  24193. if (cache) searchCache.clear();
  24194. },
  24195. clearCaches() {
  24196. if (cache) {
  24197. loadCache.clear();
  24198. searchCache.clear();
  24199. }
  24200. },
  24201. };
  24202. };
  24203. /** @type {import('./index').lilconfigSync} */
  24204. src$2.lilconfigSync = function lilconfigSync(name, options) {
  24205. const {
  24206. ignoreEmptySearchPlaces,
  24207. loaders,
  24208. packageProp,
  24209. searchPlaces,
  24210. stopDir,
  24211. transform,
  24212. cache,
  24213. } = getOptions(name, options ?? {}, true);
  24214. const searchCache = new Map();
  24215. const loadCache = new Map();
  24216. const emplace = makeEmplace(cache);
  24217. return {
  24218. search(searchFrom = process.cwd()) {
  24219. /** @type {import('./index').LilconfigResult} */
  24220. const result = {
  24221. config: null,
  24222. filepath: '',
  24223. };
  24224. /** @type {Set<string>} */
  24225. const visited = new Set();
  24226. let dir = searchFrom;
  24227. dirLoop: while (true) {
  24228. if (cache) {
  24229. const r = searchCache.get(dir);
  24230. if (r !== undefined) {
  24231. for (const p of visited) searchCache.set(p, r);
  24232. return r;
  24233. }
  24234. visited.add(dir);
  24235. }
  24236. for (const searchPlace of searchPlaces) {
  24237. const filepath = path$a.join(dir, searchPlace);
  24238. try {
  24239. fs$a.accessSync(filepath);
  24240. } catch {
  24241. continue;
  24242. }
  24243. const loaderKey = path$a.extname(searchPlace) || 'noExt';
  24244. const loader = loaders[loaderKey];
  24245. const content = String(fs$a.readFileSync(filepath));
  24246. // handle package.json
  24247. if (searchPlace === 'package.json') {
  24248. const pkg = loader(filepath, content);
  24249. const maybeConfig = getPackageProp(packageProp, pkg);
  24250. if (maybeConfig != null) {
  24251. result.config = maybeConfig;
  24252. result.filepath = filepath;
  24253. break dirLoop;
  24254. }
  24255. continue;
  24256. }
  24257. // handle other type of configs
  24258. const isEmpty = content.trim() === '';
  24259. if (isEmpty && ignoreEmptySearchPlaces) continue;
  24260. if (isEmpty) {
  24261. result.isEmpty = true;
  24262. result.config = undefined;
  24263. } else {
  24264. validateLoader(loader, loaderKey);
  24265. result.config = loader(filepath, content);
  24266. }
  24267. result.filepath = filepath;
  24268. break dirLoop;
  24269. }
  24270. if (dir === stopDir || dir === parentDir(dir)) break dirLoop;
  24271. dir = parentDir(dir);
  24272. }
  24273. const transformed =
  24274. // not found
  24275. result.filepath === '' && result.config === null
  24276. ? transform(null)
  24277. : transform(result);
  24278. if (cache) {
  24279. for (const p of visited) searchCache.set(p, transformed);
  24280. }
  24281. return transformed;
  24282. },
  24283. load(filepath) {
  24284. validateFilePath(filepath);
  24285. const absPath = path$a.resolve(process.cwd(), filepath);
  24286. if (cache && loadCache.has(absPath)) {
  24287. return loadCache.get(absPath);
  24288. }
  24289. const {base, ext} = path$a.parse(absPath);
  24290. const loaderKey = ext || 'noExt';
  24291. const loader = loaders[loaderKey];
  24292. validateLoader(loader, loaderKey);
  24293. const content = String(fs$a.readFileSync(absPath));
  24294. if (base === 'package.json') {
  24295. const pkg = loader(absPath, content);
  24296. return transform({
  24297. config: getPackageProp(packageProp, pkg),
  24298. filepath: absPath,
  24299. });
  24300. }
  24301. const result = {
  24302. config: null,
  24303. filepath: absPath,
  24304. };
  24305. // handle other type of configs
  24306. const isEmpty = content.trim() === '';
  24307. if (isEmpty && ignoreEmptySearchPlaces)
  24308. return emplace(
  24309. loadCache,
  24310. absPath,
  24311. transform({
  24312. filepath: absPath,
  24313. config: undefined,
  24314. isEmpty: true,
  24315. }),
  24316. );
  24317. // cosmiconfig returns undefined for empty files
  24318. result.config = isEmpty ? undefined : loader(absPath, content);
  24319. return emplace(
  24320. loadCache,
  24321. absPath,
  24322. transform(isEmpty ? {...result, isEmpty, config: undefined} : result),
  24323. );
  24324. },
  24325. clearLoadCache() {
  24326. if (cache) loadCache.clear();
  24327. },
  24328. clearSearchCache() {
  24329. if (cache) searchCache.clear();
  24330. },
  24331. clearCaches() {
  24332. if (cache) {
  24333. loadCache.clear();
  24334. searchCache.clear();
  24335. }
  24336. },
  24337. };
  24338. };
  24339. const ALIAS = Symbol.for('yaml.alias');
  24340. const DOC = Symbol.for('yaml.document');
  24341. const MAP = Symbol.for('yaml.map');
  24342. const PAIR = Symbol.for('yaml.pair');
  24343. const SCALAR$1 = Symbol.for('yaml.scalar');
  24344. const SEQ = Symbol.for('yaml.seq');
  24345. const NODE_TYPE = Symbol.for('yaml.node.type');
  24346. const isAlias = (node) => !!node && typeof node === 'object' && node[NODE_TYPE] === ALIAS;
  24347. const isDocument = (node) => !!node && typeof node === 'object' && node[NODE_TYPE] === DOC;
  24348. const isMap = (node) => !!node && typeof node === 'object' && node[NODE_TYPE] === MAP;
  24349. const isPair = (node) => !!node && typeof node === 'object' && node[NODE_TYPE] === PAIR;
  24350. const isScalar$1 = (node) => !!node && typeof node === 'object' && node[NODE_TYPE] === SCALAR$1;
  24351. const isSeq = (node) => !!node && typeof node === 'object' && node[NODE_TYPE] === SEQ;
  24352. function isCollection$1(node) {
  24353. if (node && typeof node === 'object')
  24354. switch (node[NODE_TYPE]) {
  24355. case MAP:
  24356. case SEQ:
  24357. return true;
  24358. }
  24359. return false;
  24360. }
  24361. function isNode$1(node) {
  24362. if (node && typeof node === 'object')
  24363. switch (node[NODE_TYPE]) {
  24364. case ALIAS:
  24365. case MAP:
  24366. case SCALAR$1:
  24367. case SEQ:
  24368. return true;
  24369. }
  24370. return false;
  24371. }
  24372. const hasAnchor = (node) => (isScalar$1(node) || isCollection$1(node)) && !!node.anchor;
  24373. const BREAK$1 = Symbol('break visit');
  24374. const SKIP$1 = Symbol('skip children');
  24375. const REMOVE$1 = Symbol('remove node');
  24376. /**
  24377. * Apply a visitor to an AST node or document.
  24378. *
  24379. * Walks through the tree (depth-first) starting from `node`, calling a
  24380. * `visitor` function with three arguments:
  24381. * - `key`: For sequence values and map `Pair`, the node's index in the
  24382. * collection. Within a `Pair`, `'key'` or `'value'`, correspondingly.
  24383. * `null` for the root node.
  24384. * - `node`: The current node.
  24385. * - `path`: The ancestry of the current node.
  24386. *
  24387. * The return value of the visitor may be used to control the traversal:
  24388. * - `undefined` (default): Do nothing and continue
  24389. * - `visit.SKIP`: Do not visit the children of this node, continue with next
  24390. * sibling
  24391. * - `visit.BREAK`: Terminate traversal completely
  24392. * - `visit.REMOVE`: Remove the current node, then continue with the next one
  24393. * - `Node`: Replace the current node, then continue by visiting it
  24394. * - `number`: While iterating the items of a sequence or map, set the index
  24395. * of the next step. This is useful especially if the index of the current
  24396. * node has changed.
  24397. *
  24398. * If `visitor` is a single function, it will be called with all values
  24399. * encountered in the tree, including e.g. `null` values. Alternatively,
  24400. * separate visitor functions may be defined for each `Map`, `Pair`, `Seq`,
  24401. * `Alias` and `Scalar` node. To define the same visitor function for more than
  24402. * one node type, use the `Collection` (map and seq), `Value` (map, seq & scalar)
  24403. * and `Node` (alias, map, seq & scalar) targets. Of all these, only the most
  24404. * specific defined one will be used for each node.
  24405. */
  24406. function visit$1(node, visitor) {
  24407. const visitor_ = initVisitor(visitor);
  24408. if (isDocument(node)) {
  24409. const cd = visit_(null, node.contents, visitor_, Object.freeze([node]));
  24410. if (cd === REMOVE$1)
  24411. node.contents = null;
  24412. }
  24413. else
  24414. visit_(null, node, visitor_, Object.freeze([]));
  24415. }
  24416. // Without the `as symbol` casts, TS declares these in the `visit`
  24417. // namespace using `var`, but then complains about that because
  24418. // `unique symbol` must be `const`.
  24419. /** Terminate visit traversal completely */
  24420. visit$1.BREAK = BREAK$1;
  24421. /** Do not visit the children of the current node */
  24422. visit$1.SKIP = SKIP$1;
  24423. /** Remove the current node */
  24424. visit$1.REMOVE = REMOVE$1;
  24425. function visit_(key, node, visitor, path) {
  24426. const ctrl = callVisitor(key, node, visitor, path);
  24427. if (isNode$1(ctrl) || isPair(ctrl)) {
  24428. replaceNode(key, path, ctrl);
  24429. return visit_(key, ctrl, visitor, path);
  24430. }
  24431. if (typeof ctrl !== 'symbol') {
  24432. if (isCollection$1(node)) {
  24433. path = Object.freeze(path.concat(node));
  24434. for (let i = 0; i < node.items.length; ++i) {
  24435. const ci = visit_(i, node.items[i], visitor, path);
  24436. if (typeof ci === 'number')
  24437. i = ci - 1;
  24438. else if (ci === BREAK$1)
  24439. return BREAK$1;
  24440. else if (ci === REMOVE$1) {
  24441. node.items.splice(i, 1);
  24442. i -= 1;
  24443. }
  24444. }
  24445. }
  24446. else if (isPair(node)) {
  24447. path = Object.freeze(path.concat(node));
  24448. const ck = visit_('key', node.key, visitor, path);
  24449. if (ck === BREAK$1)
  24450. return BREAK$1;
  24451. else if (ck === REMOVE$1)
  24452. node.key = null;
  24453. const cv = visit_('value', node.value, visitor, path);
  24454. if (cv === BREAK$1)
  24455. return BREAK$1;
  24456. else if (cv === REMOVE$1)
  24457. node.value = null;
  24458. }
  24459. }
  24460. return ctrl;
  24461. }
  24462. /**
  24463. * Apply an async visitor to an AST node or document.
  24464. *
  24465. * Walks through the tree (depth-first) starting from `node`, calling a
  24466. * `visitor` function with three arguments:
  24467. * - `key`: For sequence values and map `Pair`, the node's index in the
  24468. * collection. Within a `Pair`, `'key'` or `'value'`, correspondingly.
  24469. * `null` for the root node.
  24470. * - `node`: The current node.
  24471. * - `path`: The ancestry of the current node.
  24472. *
  24473. * The return value of the visitor may be used to control the traversal:
  24474. * - `Promise`: Must resolve to one of the following values
  24475. * - `undefined` (default): Do nothing and continue
  24476. * - `visit.SKIP`: Do not visit the children of this node, continue with next
  24477. * sibling
  24478. * - `visit.BREAK`: Terminate traversal completely
  24479. * - `visit.REMOVE`: Remove the current node, then continue with the next one
  24480. * - `Node`: Replace the current node, then continue by visiting it
  24481. * - `number`: While iterating the items of a sequence or map, set the index
  24482. * of the next step. This is useful especially if the index of the current
  24483. * node has changed.
  24484. *
  24485. * If `visitor` is a single function, it will be called with all values
  24486. * encountered in the tree, including e.g. `null` values. Alternatively,
  24487. * separate visitor functions may be defined for each `Map`, `Pair`, `Seq`,
  24488. * `Alias` and `Scalar` node. To define the same visitor function for more than
  24489. * one node type, use the `Collection` (map and seq), `Value` (map, seq & scalar)
  24490. * and `Node` (alias, map, seq & scalar) targets. Of all these, only the most
  24491. * specific defined one will be used for each node.
  24492. */
  24493. async function visitAsync(node, visitor) {
  24494. const visitor_ = initVisitor(visitor);
  24495. if (isDocument(node)) {
  24496. const cd = await visitAsync_(null, node.contents, visitor_, Object.freeze([node]));
  24497. if (cd === REMOVE$1)
  24498. node.contents = null;
  24499. }
  24500. else
  24501. await visitAsync_(null, node, visitor_, Object.freeze([]));
  24502. }
  24503. // Without the `as symbol` casts, TS declares these in the `visit`
  24504. // namespace using `var`, but then complains about that because
  24505. // `unique symbol` must be `const`.
  24506. /** Terminate visit traversal completely */
  24507. visitAsync.BREAK = BREAK$1;
  24508. /** Do not visit the children of the current node */
  24509. visitAsync.SKIP = SKIP$1;
  24510. /** Remove the current node */
  24511. visitAsync.REMOVE = REMOVE$1;
  24512. async function visitAsync_(key, node, visitor, path) {
  24513. const ctrl = await callVisitor(key, node, visitor, path);
  24514. if (isNode$1(ctrl) || isPair(ctrl)) {
  24515. replaceNode(key, path, ctrl);
  24516. return visitAsync_(key, ctrl, visitor, path);
  24517. }
  24518. if (typeof ctrl !== 'symbol') {
  24519. if (isCollection$1(node)) {
  24520. path = Object.freeze(path.concat(node));
  24521. for (let i = 0; i < node.items.length; ++i) {
  24522. const ci = await visitAsync_(i, node.items[i], visitor, path);
  24523. if (typeof ci === 'number')
  24524. i = ci - 1;
  24525. else if (ci === BREAK$1)
  24526. return BREAK$1;
  24527. else if (ci === REMOVE$1) {
  24528. node.items.splice(i, 1);
  24529. i -= 1;
  24530. }
  24531. }
  24532. }
  24533. else if (isPair(node)) {
  24534. path = Object.freeze(path.concat(node));
  24535. const ck = await visitAsync_('key', node.key, visitor, path);
  24536. if (ck === BREAK$1)
  24537. return BREAK$1;
  24538. else if (ck === REMOVE$1)
  24539. node.key = null;
  24540. const cv = await visitAsync_('value', node.value, visitor, path);
  24541. if (cv === BREAK$1)
  24542. return BREAK$1;
  24543. else if (cv === REMOVE$1)
  24544. node.value = null;
  24545. }
  24546. }
  24547. return ctrl;
  24548. }
  24549. function initVisitor(visitor) {
  24550. if (typeof visitor === 'object' &&
  24551. (visitor.Collection || visitor.Node || visitor.Value)) {
  24552. return Object.assign({
  24553. Alias: visitor.Node,
  24554. Map: visitor.Node,
  24555. Scalar: visitor.Node,
  24556. Seq: visitor.Node
  24557. }, visitor.Value && {
  24558. Map: visitor.Value,
  24559. Scalar: visitor.Value,
  24560. Seq: visitor.Value
  24561. }, visitor.Collection && {
  24562. Map: visitor.Collection,
  24563. Seq: visitor.Collection
  24564. }, visitor);
  24565. }
  24566. return visitor;
  24567. }
  24568. function callVisitor(key, node, visitor, path) {
  24569. if (typeof visitor === 'function')
  24570. return visitor(key, node, path);
  24571. if (isMap(node))
  24572. return visitor.Map?.(key, node, path);
  24573. if (isSeq(node))
  24574. return visitor.Seq?.(key, node, path);
  24575. if (isPair(node))
  24576. return visitor.Pair?.(key, node, path);
  24577. if (isScalar$1(node))
  24578. return visitor.Scalar?.(key, node, path);
  24579. if (isAlias(node))
  24580. return visitor.Alias?.(key, node, path);
  24581. return undefined;
  24582. }
  24583. function replaceNode(key, path, node) {
  24584. const parent = path[path.length - 1];
  24585. if (isCollection$1(parent)) {
  24586. parent.items[key] = node;
  24587. }
  24588. else if (isPair(parent)) {
  24589. if (key === 'key')
  24590. parent.key = node;
  24591. else
  24592. parent.value = node;
  24593. }
  24594. else if (isDocument(parent)) {
  24595. parent.contents = node;
  24596. }
  24597. else {
  24598. const pt = isAlias(parent) ? 'alias' : 'scalar';
  24599. throw new Error(`Cannot replace node with ${pt} parent`);
  24600. }
  24601. }
  24602. const escapeChars = {
  24603. '!': '%21',
  24604. ',': '%2C',
  24605. '[': '%5B',
  24606. ']': '%5D',
  24607. '{': '%7B',
  24608. '}': '%7D'
  24609. };
  24610. const escapeTagName = (tn) => tn.replace(/[!,[\]{}]/g, ch => escapeChars[ch]);
  24611. class Directives {
  24612. constructor(yaml, tags) {
  24613. /**
  24614. * The directives-end/doc-start marker `---`. If `null`, a marker may still be
  24615. * included in the document's stringified representation.
  24616. */
  24617. this.docStart = null;
  24618. /** The doc-end marker `...`. */
  24619. this.docEnd = false;
  24620. this.yaml = Object.assign({}, Directives.defaultYaml, yaml);
  24621. this.tags = Object.assign({}, Directives.defaultTags, tags);
  24622. }
  24623. clone() {
  24624. const copy = new Directives(this.yaml, this.tags);
  24625. copy.docStart = this.docStart;
  24626. return copy;
  24627. }
  24628. /**
  24629. * During parsing, get a Directives instance for the current document and
  24630. * update the stream state according to the current version's spec.
  24631. */
  24632. atDocument() {
  24633. const res = new Directives(this.yaml, this.tags);
  24634. switch (this.yaml.version) {
  24635. case '1.1':
  24636. this.atNextDocument = true;
  24637. break;
  24638. case '1.2':
  24639. this.atNextDocument = false;
  24640. this.yaml = {
  24641. explicit: Directives.defaultYaml.explicit,
  24642. version: '1.2'
  24643. };
  24644. this.tags = Object.assign({}, Directives.defaultTags);
  24645. break;
  24646. }
  24647. return res;
  24648. }
  24649. /**
  24650. * @param onError - May be called even if the action was successful
  24651. * @returns `true` on success
  24652. */
  24653. add(line, onError) {
  24654. if (this.atNextDocument) {
  24655. this.yaml = { explicit: Directives.defaultYaml.explicit, version: '1.1' };
  24656. this.tags = Object.assign({}, Directives.defaultTags);
  24657. this.atNextDocument = false;
  24658. }
  24659. const parts = line.trim().split(/[ \t]+/);
  24660. const name = parts.shift();
  24661. switch (name) {
  24662. case '%TAG': {
  24663. if (parts.length !== 2) {
  24664. onError(0, '%TAG directive should contain exactly two parts');
  24665. if (parts.length < 2)
  24666. return false;
  24667. }
  24668. const [handle, prefix] = parts;
  24669. this.tags[handle] = prefix;
  24670. return true;
  24671. }
  24672. case '%YAML': {
  24673. this.yaml.explicit = true;
  24674. if (parts.length !== 1) {
  24675. onError(0, '%YAML directive should contain exactly one part');
  24676. return false;
  24677. }
  24678. const [version] = parts;
  24679. if (version === '1.1' || version === '1.2') {
  24680. this.yaml.version = version;
  24681. return true;
  24682. }
  24683. else {
  24684. const isValid = /^\d+\.\d+$/.test(version);
  24685. onError(6, `Unsupported YAML version ${version}`, isValid);
  24686. return false;
  24687. }
  24688. }
  24689. default:
  24690. onError(0, `Unknown directive ${name}`, true);
  24691. return false;
  24692. }
  24693. }
  24694. /**
  24695. * Resolves a tag, matching handles to those defined in %TAG directives.
  24696. *
  24697. * @returns Resolved tag, which may also be the non-specific tag `'!'` or a
  24698. * `'!local'` tag, or `null` if unresolvable.
  24699. */
  24700. tagName(source, onError) {
  24701. if (source === '!')
  24702. return '!'; // non-specific tag
  24703. if (source[0] !== '!') {
  24704. onError(`Not a valid tag: ${source}`);
  24705. return null;
  24706. }
  24707. if (source[1] === '<') {
  24708. const verbatim = source.slice(2, -1);
  24709. if (verbatim === '!' || verbatim === '!!') {
  24710. onError(`Verbatim tags aren't resolved, so ${source} is invalid.`);
  24711. return null;
  24712. }
  24713. if (source[source.length - 1] !== '>')
  24714. onError('Verbatim tags must end with a >');
  24715. return verbatim;
  24716. }
  24717. const [, handle, suffix] = source.match(/^(.*!)([^!]*)$/s);
  24718. if (!suffix)
  24719. onError(`The ${source} tag has no suffix`);
  24720. const prefix = this.tags[handle];
  24721. if (prefix) {
  24722. try {
  24723. return prefix + decodeURIComponent(suffix);
  24724. }
  24725. catch (error) {
  24726. onError(String(error));
  24727. return null;
  24728. }
  24729. }
  24730. if (handle === '!')
  24731. return source; // local tag
  24732. onError(`Could not resolve tag: ${source}`);
  24733. return null;
  24734. }
  24735. /**
  24736. * Given a fully resolved tag, returns its printable string form,
  24737. * taking into account current tag prefixes and defaults.
  24738. */
  24739. tagString(tag) {
  24740. for (const [handle, prefix] of Object.entries(this.tags)) {
  24741. if (tag.startsWith(prefix))
  24742. return handle + escapeTagName(tag.substring(prefix.length));
  24743. }
  24744. return tag[0] === '!' ? tag : `!<${tag}>`;
  24745. }
  24746. toString(doc) {
  24747. const lines = this.yaml.explicit
  24748. ? [`%YAML ${this.yaml.version || '1.2'}`]
  24749. : [];
  24750. const tagEntries = Object.entries(this.tags);
  24751. let tagNames;
  24752. if (doc && tagEntries.length > 0 && isNode$1(doc.contents)) {
  24753. const tags = {};
  24754. visit$1(doc.contents, (_key, node) => {
  24755. if (isNode$1(node) && node.tag)
  24756. tags[node.tag] = true;
  24757. });
  24758. tagNames = Object.keys(tags);
  24759. }
  24760. else
  24761. tagNames = [];
  24762. for (const [handle, prefix] of tagEntries) {
  24763. if (handle === '!!' && prefix === 'tag:yaml.org,2002:')
  24764. continue;
  24765. if (!doc || tagNames.some(tn => tn.startsWith(prefix)))
  24766. lines.push(`%TAG ${handle} ${prefix}`);
  24767. }
  24768. return lines.join('\n');
  24769. }
  24770. }
  24771. Directives.defaultYaml = { explicit: false, version: '1.2' };
  24772. Directives.defaultTags = { '!!': 'tag:yaml.org,2002:' };
  24773. /**
  24774. * Verify that the input string is a valid anchor.
  24775. *
  24776. * Will throw on errors.
  24777. */
  24778. function anchorIsValid(anchor) {
  24779. if (/[\x00-\x19\s,[\]{}]/.test(anchor)) {
  24780. const sa = JSON.stringify(anchor);
  24781. const msg = `Anchor must not contain whitespace or control characters: ${sa}`;
  24782. throw new Error(msg);
  24783. }
  24784. return true;
  24785. }
  24786. function anchorNames(root) {
  24787. const anchors = new Set();
  24788. visit$1(root, {
  24789. Value(_key, node) {
  24790. if (node.anchor)
  24791. anchors.add(node.anchor);
  24792. }
  24793. });
  24794. return anchors;
  24795. }
  24796. /** Find a new anchor name with the given `prefix` and a one-indexed suffix. */
  24797. function findNewAnchor(prefix, exclude) {
  24798. for (let i = 1; true; ++i) {
  24799. const name = `${prefix}${i}`;
  24800. if (!exclude.has(name))
  24801. return name;
  24802. }
  24803. }
  24804. function createNodeAnchors(doc, prefix) {
  24805. const aliasObjects = [];
  24806. const sourceObjects = new Map();
  24807. let prevAnchors = null;
  24808. return {
  24809. onAnchor: (source) => {
  24810. aliasObjects.push(source);
  24811. if (!prevAnchors)
  24812. prevAnchors = anchorNames(doc);
  24813. const anchor = findNewAnchor(prefix, prevAnchors);
  24814. prevAnchors.add(anchor);
  24815. return anchor;
  24816. },
  24817. /**
  24818. * With circular references, the source node is only resolved after all
  24819. * of its child nodes are. This is why anchors are set only after all of
  24820. * the nodes have been created.
  24821. */
  24822. setAnchors: () => {
  24823. for (const source of aliasObjects) {
  24824. const ref = sourceObjects.get(source);
  24825. if (typeof ref === 'object' &&
  24826. ref.anchor &&
  24827. (isScalar$1(ref.node) || isCollection$1(ref.node))) {
  24828. ref.node.anchor = ref.anchor;
  24829. }
  24830. else {
  24831. const error = new Error('Failed to resolve repeated object (this should not happen)');
  24832. error.source = source;
  24833. throw error;
  24834. }
  24835. }
  24836. },
  24837. sourceObjects
  24838. };
  24839. }
  24840. /**
  24841. * Applies the JSON.parse reviver algorithm as defined in the ECMA-262 spec,
  24842. * in section 24.5.1.1 "Runtime Semantics: InternalizeJSONProperty" of the
  24843. * 2021 edition: https://tc39.es/ecma262/#sec-json.parse
  24844. *
  24845. * Includes extensions for handling Map and Set objects.
  24846. */
  24847. function applyReviver(reviver, obj, key, val) {
  24848. if (val && typeof val === 'object') {
  24849. if (Array.isArray(val)) {
  24850. for (let i = 0, len = val.length; i < len; ++i) {
  24851. const v0 = val[i];
  24852. const v1 = applyReviver(reviver, val, String(i), v0);
  24853. if (v1 === undefined)
  24854. delete val[i];
  24855. else if (v1 !== v0)
  24856. val[i] = v1;
  24857. }
  24858. }
  24859. else if (val instanceof Map) {
  24860. for (const k of Array.from(val.keys())) {
  24861. const v0 = val.get(k);
  24862. const v1 = applyReviver(reviver, val, k, v0);
  24863. if (v1 === undefined)
  24864. val.delete(k);
  24865. else if (v1 !== v0)
  24866. val.set(k, v1);
  24867. }
  24868. }
  24869. else if (val instanceof Set) {
  24870. for (const v0 of Array.from(val)) {
  24871. const v1 = applyReviver(reviver, val, v0, v0);
  24872. if (v1 === undefined)
  24873. val.delete(v0);
  24874. else if (v1 !== v0) {
  24875. val.delete(v0);
  24876. val.add(v1);
  24877. }
  24878. }
  24879. }
  24880. else {
  24881. for (const [k, v0] of Object.entries(val)) {
  24882. const v1 = applyReviver(reviver, val, k, v0);
  24883. if (v1 === undefined)
  24884. delete val[k];
  24885. else if (v1 !== v0)
  24886. val[k] = v1;
  24887. }
  24888. }
  24889. }
  24890. return reviver.call(obj, key, val);
  24891. }
  24892. /**
  24893. * Recursively convert any node or its contents to native JavaScript
  24894. *
  24895. * @param value - The input value
  24896. * @param arg - If `value` defines a `toJSON()` method, use this
  24897. * as its first argument
  24898. * @param ctx - Conversion context, originally set in Document#toJS(). If
  24899. * `{ keep: true }` is not set, output should be suitable for JSON
  24900. * stringification.
  24901. */
  24902. function toJS(value, arg, ctx) {
  24903. // eslint-disable-next-line @typescript-eslint/no-unsafe-return
  24904. if (Array.isArray(value))
  24905. return value.map((v, i) => toJS(v, String(i), ctx));
  24906. if (value && typeof value.toJSON === 'function') {
  24907. // eslint-disable-next-line @typescript-eslint/no-unsafe-call
  24908. if (!ctx || !hasAnchor(value))
  24909. return value.toJSON(arg, ctx);
  24910. const data = { aliasCount: 0, count: 1, res: undefined };
  24911. ctx.anchors.set(value, data);
  24912. ctx.onCreate = res => {
  24913. data.res = res;
  24914. delete ctx.onCreate;
  24915. };
  24916. const res = value.toJSON(arg, ctx);
  24917. if (ctx.onCreate)
  24918. ctx.onCreate(res);
  24919. return res;
  24920. }
  24921. if (typeof value === 'bigint' && !ctx?.keep)
  24922. return Number(value);
  24923. return value;
  24924. }
  24925. class NodeBase {
  24926. constructor(type) {
  24927. Object.defineProperty(this, NODE_TYPE, { value: type });
  24928. }
  24929. /** Create a copy of this node. */
  24930. clone() {
  24931. const copy = Object.create(Object.getPrototypeOf(this), Object.getOwnPropertyDescriptors(this));
  24932. if (this.range)
  24933. copy.range = this.range.slice();
  24934. return copy;
  24935. }
  24936. /** A plain JavaScript representation of this node. */
  24937. toJS(doc, { mapAsMap, maxAliasCount, onAnchor, reviver } = {}) {
  24938. if (!isDocument(doc))
  24939. throw new TypeError('A document argument is required');
  24940. const ctx = {
  24941. anchors: new Map(),
  24942. doc,
  24943. keep: true,
  24944. mapAsMap: mapAsMap === true,
  24945. mapKeyWarned: false,
  24946. maxAliasCount: typeof maxAliasCount === 'number' ? maxAliasCount : 100
  24947. };
  24948. const res = toJS(this, '', ctx);
  24949. if (typeof onAnchor === 'function')
  24950. for (const { count, res } of ctx.anchors.values())
  24951. onAnchor(res, count);
  24952. return typeof reviver === 'function'
  24953. ? applyReviver(reviver, { '': res }, '', res)
  24954. : res;
  24955. }
  24956. }
  24957. class Alias extends NodeBase {
  24958. constructor(source) {
  24959. super(ALIAS);
  24960. this.source = source;
  24961. Object.defineProperty(this, 'tag', {
  24962. set() {
  24963. throw new Error('Alias nodes cannot have tags');
  24964. }
  24965. });
  24966. }
  24967. /**
  24968. * Resolve the value of this alias within `doc`, finding the last
  24969. * instance of the `source` anchor before this node.
  24970. */
  24971. resolve(doc) {
  24972. let found = undefined;
  24973. visit$1(doc, {
  24974. Node: (_key, node) => {
  24975. if (node === this)
  24976. return visit$1.BREAK;
  24977. if (node.anchor === this.source)
  24978. found = node;
  24979. }
  24980. });
  24981. return found;
  24982. }
  24983. toJSON(_arg, ctx) {
  24984. if (!ctx)
  24985. return { source: this.source };
  24986. const { anchors, doc, maxAliasCount } = ctx;
  24987. const source = this.resolve(doc);
  24988. if (!source) {
  24989. const msg = `Unresolved alias (the anchor must be set before the alias): ${this.source}`;
  24990. throw new ReferenceError(msg);
  24991. }
  24992. let data = anchors.get(source);
  24993. if (!data) {
  24994. // Resolve anchors for Node.prototype.toJS()
  24995. toJS(source, null, ctx);
  24996. data = anchors.get(source);
  24997. }
  24998. /* istanbul ignore if */
  24999. if (!data || data.res === undefined) {
  25000. const msg = 'This should not happen: Alias anchor was not resolved?';
  25001. throw new ReferenceError(msg);
  25002. }
  25003. if (maxAliasCount >= 0) {
  25004. data.count += 1;
  25005. if (data.aliasCount === 0)
  25006. data.aliasCount = getAliasCount(doc, source, anchors);
  25007. if (data.count * data.aliasCount > maxAliasCount) {
  25008. const msg = 'Excessive alias count indicates a resource exhaustion attack';
  25009. throw new ReferenceError(msg);
  25010. }
  25011. }
  25012. return data.res;
  25013. }
  25014. toString(ctx, _onComment, _onChompKeep) {
  25015. const src = `*${this.source}`;
  25016. if (ctx) {
  25017. anchorIsValid(this.source);
  25018. if (ctx.options.verifyAliasOrder && !ctx.anchors.has(this.source)) {
  25019. const msg = `Unresolved alias (the anchor must be set before the alias): ${this.source}`;
  25020. throw new Error(msg);
  25021. }
  25022. if (ctx.implicitKey)
  25023. return `${src} `;
  25024. }
  25025. return src;
  25026. }
  25027. }
  25028. function getAliasCount(doc, node, anchors) {
  25029. if (isAlias(node)) {
  25030. const source = node.resolve(doc);
  25031. const anchor = anchors && source && anchors.get(source);
  25032. return anchor ? anchor.count * anchor.aliasCount : 0;
  25033. }
  25034. else if (isCollection$1(node)) {
  25035. let count = 0;
  25036. for (const item of node.items) {
  25037. const c = getAliasCount(doc, item, anchors);
  25038. if (c > count)
  25039. count = c;
  25040. }
  25041. return count;
  25042. }
  25043. else if (isPair(node)) {
  25044. const kc = getAliasCount(doc, node.key, anchors);
  25045. const vc = getAliasCount(doc, node.value, anchors);
  25046. return Math.max(kc, vc);
  25047. }
  25048. return 1;
  25049. }
  25050. const isScalarValue = (value) => !value || (typeof value !== 'function' && typeof value !== 'object');
  25051. class Scalar extends NodeBase {
  25052. constructor(value) {
  25053. super(SCALAR$1);
  25054. this.value = value;
  25055. }
  25056. toJSON(arg, ctx) {
  25057. return ctx?.keep ? this.value : toJS(this.value, arg, ctx);
  25058. }
  25059. toString() {
  25060. return String(this.value);
  25061. }
  25062. }
  25063. Scalar.BLOCK_FOLDED = 'BLOCK_FOLDED';
  25064. Scalar.BLOCK_LITERAL = 'BLOCK_LITERAL';
  25065. Scalar.PLAIN = 'PLAIN';
  25066. Scalar.QUOTE_DOUBLE = 'QUOTE_DOUBLE';
  25067. Scalar.QUOTE_SINGLE = 'QUOTE_SINGLE';
  25068. const defaultTagPrefix = 'tag:yaml.org,2002:';
  25069. function findTagObject(value, tagName, tags) {
  25070. if (tagName) {
  25071. const match = tags.filter(t => t.tag === tagName);
  25072. const tagObj = match.find(t => !t.format) ?? match[0];
  25073. if (!tagObj)
  25074. throw new Error(`Tag ${tagName} not found`);
  25075. return tagObj;
  25076. }
  25077. return tags.find(t => t.identify?.(value) && !t.format);
  25078. }
  25079. function createNode(value, tagName, ctx) {
  25080. if (isDocument(value))
  25081. value = value.contents;
  25082. if (isNode$1(value))
  25083. return value;
  25084. if (isPair(value)) {
  25085. const map = ctx.schema[MAP].createNode?.(ctx.schema, null, ctx);
  25086. map.items.push(value);
  25087. return map;
  25088. }
  25089. if (value instanceof String ||
  25090. value instanceof Number ||
  25091. value instanceof Boolean ||
  25092. (typeof BigInt !== 'undefined' && value instanceof BigInt) // not supported everywhere
  25093. ) {
  25094. // https://tc39.es/ecma262/#sec-serializejsonproperty
  25095. value = value.valueOf();
  25096. }
  25097. const { aliasDuplicateObjects, onAnchor, onTagObj, schema, sourceObjects } = ctx;
  25098. // Detect duplicate references to the same object & use Alias nodes for all
  25099. // after first. The `ref` wrapper allows for circular references to resolve.
  25100. let ref = undefined;
  25101. if (aliasDuplicateObjects && value && typeof value === 'object') {
  25102. ref = sourceObjects.get(value);
  25103. if (ref) {
  25104. if (!ref.anchor)
  25105. ref.anchor = onAnchor(value);
  25106. return new Alias(ref.anchor);
  25107. }
  25108. else {
  25109. ref = { anchor: null, node: null };
  25110. sourceObjects.set(value, ref);
  25111. }
  25112. }
  25113. if (tagName?.startsWith('!!'))
  25114. tagName = defaultTagPrefix + tagName.slice(2);
  25115. let tagObj = findTagObject(value, tagName, schema.tags);
  25116. if (!tagObj) {
  25117. if (value && typeof value.toJSON === 'function') {
  25118. // eslint-disable-next-line @typescript-eslint/no-unsafe-call
  25119. value = value.toJSON();
  25120. }
  25121. if (!value || typeof value !== 'object') {
  25122. const node = new Scalar(value);
  25123. if (ref)
  25124. ref.node = node;
  25125. return node;
  25126. }
  25127. tagObj =
  25128. value instanceof Map
  25129. ? schema[MAP]
  25130. : Symbol.iterator in Object(value)
  25131. ? schema[SEQ]
  25132. : schema[MAP];
  25133. }
  25134. if (onTagObj) {
  25135. onTagObj(tagObj);
  25136. delete ctx.onTagObj;
  25137. }
  25138. const node = tagObj?.createNode
  25139. ? tagObj.createNode(ctx.schema, value, ctx)
  25140. : typeof tagObj?.nodeClass?.from === 'function'
  25141. ? tagObj.nodeClass.from(ctx.schema, value, ctx)
  25142. : new Scalar(value);
  25143. if (tagName)
  25144. node.tag = tagName;
  25145. else if (!tagObj.default)
  25146. node.tag = tagObj.tag;
  25147. if (ref)
  25148. ref.node = node;
  25149. return node;
  25150. }
  25151. function collectionFromPath(schema, path, value) {
  25152. let v = value;
  25153. for (let i = path.length - 1; i >= 0; --i) {
  25154. const k = path[i];
  25155. if (typeof k === 'number' && Number.isInteger(k) && k >= 0) {
  25156. const a = [];
  25157. a[k] = v;
  25158. v = a;
  25159. }
  25160. else {
  25161. v = new Map([[k, v]]);
  25162. }
  25163. }
  25164. return createNode(v, undefined, {
  25165. aliasDuplicateObjects: false,
  25166. keepUndefined: false,
  25167. onAnchor: () => {
  25168. throw new Error('This should not happen, please report a bug.');
  25169. },
  25170. schema,
  25171. sourceObjects: new Map()
  25172. });
  25173. }
  25174. // Type guard is intentionally a little wrong so as to be more useful,
  25175. // as it does not cover untypable empty non-string iterables (e.g. []).
  25176. const isEmptyPath = (path) => path == null ||
  25177. (typeof path === 'object' && !!path[Symbol.iterator]().next().done);
  25178. class Collection extends NodeBase {
  25179. constructor(type, schema) {
  25180. super(type);
  25181. Object.defineProperty(this, 'schema', {
  25182. value: schema,
  25183. configurable: true,
  25184. enumerable: false,
  25185. writable: true
  25186. });
  25187. }
  25188. /**
  25189. * Create a copy of this collection.
  25190. *
  25191. * @param schema - If defined, overwrites the original's schema
  25192. */
  25193. clone(schema) {
  25194. const copy = Object.create(Object.getPrototypeOf(this), Object.getOwnPropertyDescriptors(this));
  25195. if (schema)
  25196. copy.schema = schema;
  25197. copy.items = copy.items.map(it => isNode$1(it) || isPair(it) ? it.clone(schema) : it);
  25198. if (this.range)
  25199. copy.range = this.range.slice();
  25200. return copy;
  25201. }
  25202. /**
  25203. * Adds a value to the collection. For `!!map` and `!!omap` the value must
  25204. * be a Pair instance or a `{ key, value }` object, which may not have a key
  25205. * that already exists in the map.
  25206. */
  25207. addIn(path, value) {
  25208. if (isEmptyPath(path))
  25209. this.add(value);
  25210. else {
  25211. const [key, ...rest] = path;
  25212. const node = this.get(key, true);
  25213. if (isCollection$1(node))
  25214. node.addIn(rest, value);
  25215. else if (node === undefined && this.schema)
  25216. this.set(key, collectionFromPath(this.schema, rest, value));
  25217. else
  25218. throw new Error(`Expected YAML collection at ${key}. Remaining path: ${rest}`);
  25219. }
  25220. }
  25221. /**
  25222. * Removes a value from the collection.
  25223. * @returns `true` if the item was found and removed.
  25224. */
  25225. deleteIn(path) {
  25226. const [key, ...rest] = path;
  25227. if (rest.length === 0)
  25228. return this.delete(key);
  25229. const node = this.get(key, true);
  25230. if (isCollection$1(node))
  25231. return node.deleteIn(rest);
  25232. else
  25233. throw new Error(`Expected YAML collection at ${key}. Remaining path: ${rest}`);
  25234. }
  25235. /**
  25236. * Returns item at `key`, or `undefined` if not found. By default unwraps
  25237. * scalar values from their surrounding node; to disable set `keepScalar` to
  25238. * `true` (collections are always returned intact).
  25239. */
  25240. getIn(path, keepScalar) {
  25241. const [key, ...rest] = path;
  25242. const node = this.get(key, true);
  25243. if (rest.length === 0)
  25244. return !keepScalar && isScalar$1(node) ? node.value : node;
  25245. else
  25246. return isCollection$1(node) ? node.getIn(rest, keepScalar) : undefined;
  25247. }
  25248. hasAllNullValues(allowScalar) {
  25249. return this.items.every(node => {
  25250. if (!isPair(node))
  25251. return false;
  25252. const n = node.value;
  25253. return (n == null ||
  25254. (allowScalar &&
  25255. isScalar$1(n) &&
  25256. n.value == null &&
  25257. !n.commentBefore &&
  25258. !n.comment &&
  25259. !n.tag));
  25260. });
  25261. }
  25262. /**
  25263. * Checks if the collection includes a value with the key `key`.
  25264. */
  25265. hasIn(path) {
  25266. const [key, ...rest] = path;
  25267. if (rest.length === 0)
  25268. return this.has(key);
  25269. const node = this.get(key, true);
  25270. return isCollection$1(node) ? node.hasIn(rest) : false;
  25271. }
  25272. /**
  25273. * Sets a value in this collection. For `!!set`, `value` needs to be a
  25274. * boolean to add/remove the item from the set.
  25275. */
  25276. setIn(path, value) {
  25277. const [key, ...rest] = path;
  25278. if (rest.length === 0) {
  25279. this.set(key, value);
  25280. }
  25281. else {
  25282. const node = this.get(key, true);
  25283. if (isCollection$1(node))
  25284. node.setIn(rest, value);
  25285. else if (node === undefined && this.schema)
  25286. this.set(key, collectionFromPath(this.schema, rest, value));
  25287. else
  25288. throw new Error(`Expected YAML collection at ${key}. Remaining path: ${rest}`);
  25289. }
  25290. }
  25291. }
  25292. /**
  25293. * Stringifies a comment.
  25294. *
  25295. * Empty comment lines are left empty,
  25296. * lines consisting of a single space are replaced by `#`,
  25297. * and all other lines are prefixed with a `#`.
  25298. */
  25299. const stringifyComment = (str) => str.replace(/^(?!$)(?: $)?/gm, '#');
  25300. function indentComment(comment, indent) {
  25301. if (/^\n+$/.test(comment))
  25302. return comment.substring(1);
  25303. return indent ? comment.replace(/^(?! *$)/gm, indent) : comment;
  25304. }
  25305. const lineComment = (str, indent, comment) => str.endsWith('\n')
  25306. ? indentComment(comment, indent)
  25307. : comment.includes('\n')
  25308. ? '\n' + indentComment(comment, indent)
  25309. : (str.endsWith(' ') ? '' : ' ') + comment;
  25310. const FOLD_FLOW = 'flow';
  25311. const FOLD_BLOCK = 'block';
  25312. const FOLD_QUOTED = 'quoted';
  25313. /**
  25314. * Tries to keep input at up to `lineWidth` characters, splitting only on spaces
  25315. * not followed by newlines or spaces unless `mode` is `'quoted'`. Lines are
  25316. * terminated with `\n` and started with `indent`.
  25317. */
  25318. function foldFlowLines(text, indent, mode = 'flow', { indentAtStart, lineWidth = 80, minContentWidth = 20, onFold, onOverflow } = {}) {
  25319. if (!lineWidth || lineWidth < 0)
  25320. return text;
  25321. if (lineWidth < minContentWidth)
  25322. minContentWidth = 0;
  25323. const endStep = Math.max(1 + minContentWidth, 1 + lineWidth - indent.length);
  25324. if (text.length <= endStep)
  25325. return text;
  25326. const folds = [];
  25327. const escapedFolds = {};
  25328. let end = lineWidth - indent.length;
  25329. if (typeof indentAtStart === 'number') {
  25330. if (indentAtStart > lineWidth - Math.max(2, minContentWidth))
  25331. folds.push(0);
  25332. else
  25333. end = lineWidth - indentAtStart;
  25334. }
  25335. let split = undefined;
  25336. let prev = undefined;
  25337. let overflow = false;
  25338. let i = -1;
  25339. let escStart = -1;
  25340. let escEnd = -1;
  25341. if (mode === FOLD_BLOCK) {
  25342. i = consumeMoreIndentedLines(text, i, indent.length);
  25343. if (i !== -1)
  25344. end = i + endStep;
  25345. }
  25346. for (let ch; (ch = text[(i += 1)]);) {
  25347. if (mode === FOLD_QUOTED && ch === '\\') {
  25348. escStart = i;
  25349. switch (text[i + 1]) {
  25350. case 'x':
  25351. i += 3;
  25352. break;
  25353. case 'u':
  25354. i += 5;
  25355. break;
  25356. case 'U':
  25357. i += 9;
  25358. break;
  25359. default:
  25360. i += 1;
  25361. }
  25362. escEnd = i;
  25363. }
  25364. if (ch === '\n') {
  25365. if (mode === FOLD_BLOCK)
  25366. i = consumeMoreIndentedLines(text, i, indent.length);
  25367. end = i + indent.length + endStep;
  25368. split = undefined;
  25369. }
  25370. else {
  25371. if (ch === ' ' &&
  25372. prev &&
  25373. prev !== ' ' &&
  25374. prev !== '\n' &&
  25375. prev !== '\t') {
  25376. // space surrounded by non-space can be replaced with newline + indent
  25377. const next = text[i + 1];
  25378. if (next && next !== ' ' && next !== '\n' && next !== '\t')
  25379. split = i;
  25380. }
  25381. if (i >= end) {
  25382. if (split) {
  25383. folds.push(split);
  25384. end = split + endStep;
  25385. split = undefined;
  25386. }
  25387. else if (mode === FOLD_QUOTED) {
  25388. // white-space collected at end may stretch past lineWidth
  25389. while (prev === ' ' || prev === '\t') {
  25390. prev = ch;
  25391. ch = text[(i += 1)];
  25392. overflow = true;
  25393. }
  25394. // Account for newline escape, but don't break preceding escape
  25395. const j = i > escEnd + 1 ? i - 2 : escStart - 1;
  25396. // Bail out if lineWidth & minContentWidth are shorter than an escape string
  25397. if (escapedFolds[j])
  25398. return text;
  25399. folds.push(j);
  25400. escapedFolds[j] = true;
  25401. end = j + endStep;
  25402. split = undefined;
  25403. }
  25404. else {
  25405. overflow = true;
  25406. }
  25407. }
  25408. }
  25409. prev = ch;
  25410. }
  25411. if (overflow && onOverflow)
  25412. onOverflow();
  25413. if (folds.length === 0)
  25414. return text;
  25415. if (onFold)
  25416. onFold();
  25417. let res = text.slice(0, folds[0]);
  25418. for (let i = 0; i < folds.length; ++i) {
  25419. const fold = folds[i];
  25420. const end = folds[i + 1] || text.length;
  25421. if (fold === 0)
  25422. res = `\n${indent}${text.slice(0, end)}`;
  25423. else {
  25424. if (mode === FOLD_QUOTED && escapedFolds[fold])
  25425. res += `${text[fold]}\\`;
  25426. res += `\n${indent}${text.slice(fold + 1, end)}`;
  25427. }
  25428. }
  25429. return res;
  25430. }
  25431. /**
  25432. * Presumes `i + 1` is at the start of a line
  25433. * @returns index of last newline in more-indented block
  25434. */
  25435. function consumeMoreIndentedLines(text, i, indent) {
  25436. let end = i;
  25437. let start = i + 1;
  25438. let ch = text[start];
  25439. while (ch === ' ' || ch === '\t') {
  25440. if (i < start + indent) {
  25441. ch = text[++i];
  25442. }
  25443. else {
  25444. do {
  25445. ch = text[++i];
  25446. } while (ch && ch !== '\n');
  25447. end = i;
  25448. start = i + 1;
  25449. ch = text[start];
  25450. }
  25451. }
  25452. return end;
  25453. }
  25454. const getFoldOptions = (ctx, isBlock) => ({
  25455. indentAtStart: isBlock ? ctx.indent.length : ctx.indentAtStart,
  25456. lineWidth: ctx.options.lineWidth,
  25457. minContentWidth: ctx.options.minContentWidth
  25458. });
  25459. // Also checks for lines starting with %, as parsing the output as YAML 1.1 will
  25460. // presume that's starting a new document.
  25461. const containsDocumentMarker = (str) => /^(%|---|\.\.\.)/m.test(str);
  25462. function lineLengthOverLimit(str, lineWidth, indentLength) {
  25463. if (!lineWidth || lineWidth < 0)
  25464. return false;
  25465. const limit = lineWidth - indentLength;
  25466. const strLen = str.length;
  25467. if (strLen <= limit)
  25468. return false;
  25469. for (let i = 0, start = 0; i < strLen; ++i) {
  25470. if (str[i] === '\n') {
  25471. if (i - start > limit)
  25472. return true;
  25473. start = i + 1;
  25474. if (strLen - start <= limit)
  25475. return false;
  25476. }
  25477. }
  25478. return true;
  25479. }
  25480. function doubleQuotedString(value, ctx) {
  25481. const json = JSON.stringify(value);
  25482. if (ctx.options.doubleQuotedAsJSON)
  25483. return json;
  25484. const { implicitKey } = ctx;
  25485. const minMultiLineLength = ctx.options.doubleQuotedMinMultiLineLength;
  25486. const indent = ctx.indent || (containsDocumentMarker(value) ? ' ' : '');
  25487. let str = '';
  25488. let start = 0;
  25489. for (let i = 0, ch = json[i]; ch; ch = json[++i]) {
  25490. if (ch === ' ' && json[i + 1] === '\\' && json[i + 2] === 'n') {
  25491. // space before newline needs to be escaped to not be folded
  25492. str += json.slice(start, i) + '\\ ';
  25493. i += 1;
  25494. start = i;
  25495. ch = '\\';
  25496. }
  25497. if (ch === '\\')
  25498. switch (json[i + 1]) {
  25499. case 'u':
  25500. {
  25501. str += json.slice(start, i);
  25502. const code = json.substr(i + 2, 4);
  25503. switch (code) {
  25504. case '0000':
  25505. str += '\\0';
  25506. break;
  25507. case '0007':
  25508. str += '\\a';
  25509. break;
  25510. case '000b':
  25511. str += '\\v';
  25512. break;
  25513. case '001b':
  25514. str += '\\e';
  25515. break;
  25516. case '0085':
  25517. str += '\\N';
  25518. break;
  25519. case '00a0':
  25520. str += '\\_';
  25521. break;
  25522. case '2028':
  25523. str += '\\L';
  25524. break;
  25525. case '2029':
  25526. str += '\\P';
  25527. break;
  25528. default:
  25529. if (code.substr(0, 2) === '00')
  25530. str += '\\x' + code.substr(2);
  25531. else
  25532. str += json.substr(i, 6);
  25533. }
  25534. i += 5;
  25535. start = i + 1;
  25536. }
  25537. break;
  25538. case 'n':
  25539. if (implicitKey ||
  25540. json[i + 2] === '"' ||
  25541. json.length < minMultiLineLength) {
  25542. i += 1;
  25543. }
  25544. else {
  25545. // folding will eat first newline
  25546. str += json.slice(start, i) + '\n\n';
  25547. while (json[i + 2] === '\\' &&
  25548. json[i + 3] === 'n' &&
  25549. json[i + 4] !== '"') {
  25550. str += '\n';
  25551. i += 2;
  25552. }
  25553. str += indent;
  25554. // space after newline needs to be escaped to not be folded
  25555. if (json[i + 2] === ' ')
  25556. str += '\\';
  25557. i += 1;
  25558. start = i + 1;
  25559. }
  25560. break;
  25561. default:
  25562. i += 1;
  25563. }
  25564. }
  25565. str = start ? str + json.slice(start) : json;
  25566. return implicitKey
  25567. ? str
  25568. : foldFlowLines(str, indent, FOLD_QUOTED, getFoldOptions(ctx, false));
  25569. }
  25570. function singleQuotedString(value, ctx) {
  25571. if (ctx.options.singleQuote === false ||
  25572. (ctx.implicitKey && value.includes('\n')) ||
  25573. /[ \t]\n|\n[ \t]/.test(value) // single quoted string can't have leading or trailing whitespace around newline
  25574. )
  25575. return doubleQuotedString(value, ctx);
  25576. const indent = ctx.indent || (containsDocumentMarker(value) ? ' ' : '');
  25577. const res = "'" + value.replace(/'/g, "''").replace(/\n+/g, `$&\n${indent}`) + "'";
  25578. return ctx.implicitKey
  25579. ? res
  25580. : foldFlowLines(res, indent, FOLD_FLOW, getFoldOptions(ctx, false));
  25581. }
  25582. function quotedString(value, ctx) {
  25583. const { singleQuote } = ctx.options;
  25584. let qs;
  25585. if (singleQuote === false)
  25586. qs = doubleQuotedString;
  25587. else {
  25588. const hasDouble = value.includes('"');
  25589. const hasSingle = value.includes("'");
  25590. if (hasDouble && !hasSingle)
  25591. qs = singleQuotedString;
  25592. else if (hasSingle && !hasDouble)
  25593. qs = doubleQuotedString;
  25594. else
  25595. qs = singleQuote ? singleQuotedString : doubleQuotedString;
  25596. }
  25597. return qs(value, ctx);
  25598. }
  25599. // The negative lookbehind avoids a polynomial search,
  25600. // but isn't supported yet on Safari: https://caniuse.com/js-regexp-lookbehind
  25601. let blockEndNewlines;
  25602. try {
  25603. blockEndNewlines = new RegExp('(^|(?<!\n))\n+(?!\n|$)', 'g');
  25604. }
  25605. catch {
  25606. blockEndNewlines = /\n+(?!\n|$)/g;
  25607. }
  25608. function blockString({ comment, type, value }, ctx, onComment, onChompKeep) {
  25609. const { blockQuote, commentString, lineWidth } = ctx.options;
  25610. // 1. Block can't end in whitespace unless the last line is non-empty.
  25611. // 2. Strings consisting of only whitespace are best rendered explicitly.
  25612. if (!blockQuote || /\n[\t ]+$/.test(value) || /^\s*$/.test(value)) {
  25613. return quotedString(value, ctx);
  25614. }
  25615. const indent = ctx.indent ||
  25616. (ctx.forceBlockIndent || containsDocumentMarker(value) ? ' ' : '');
  25617. const literal = blockQuote === 'literal'
  25618. ? true
  25619. : blockQuote === 'folded' || type === Scalar.BLOCK_FOLDED
  25620. ? false
  25621. : type === Scalar.BLOCK_LITERAL
  25622. ? true
  25623. : !lineLengthOverLimit(value, lineWidth, indent.length);
  25624. if (!value)
  25625. return literal ? '|\n' : '>\n';
  25626. // determine chomping from whitespace at value end
  25627. let chomp;
  25628. let endStart;
  25629. for (endStart = value.length; endStart > 0; --endStart) {
  25630. const ch = value[endStart - 1];
  25631. if (ch !== '\n' && ch !== '\t' && ch !== ' ')
  25632. break;
  25633. }
  25634. let end = value.substring(endStart);
  25635. const endNlPos = end.indexOf('\n');
  25636. if (endNlPos === -1) {
  25637. chomp = '-'; // strip
  25638. }
  25639. else if (value === end || endNlPos !== end.length - 1) {
  25640. chomp = '+'; // keep
  25641. if (onChompKeep)
  25642. onChompKeep();
  25643. }
  25644. else {
  25645. chomp = ''; // clip
  25646. }
  25647. if (end) {
  25648. value = value.slice(0, -end.length);
  25649. if (end[end.length - 1] === '\n')
  25650. end = end.slice(0, -1);
  25651. end = end.replace(blockEndNewlines, `$&${indent}`);
  25652. }
  25653. // determine indent indicator from whitespace at value start
  25654. let startWithSpace = false;
  25655. let startEnd;
  25656. let startNlPos = -1;
  25657. for (startEnd = 0; startEnd < value.length; ++startEnd) {
  25658. const ch = value[startEnd];
  25659. if (ch === ' ')
  25660. startWithSpace = true;
  25661. else if (ch === '\n')
  25662. startNlPos = startEnd;
  25663. else
  25664. break;
  25665. }
  25666. let start = value.substring(0, startNlPos < startEnd ? startNlPos + 1 : startEnd);
  25667. if (start) {
  25668. value = value.substring(start.length);
  25669. start = start.replace(/\n+/g, `$&${indent}`);
  25670. }
  25671. const indentSize = indent ? '2' : '1'; // root is at -1
  25672. let header = (literal ? '|' : '>') + (startWithSpace ? indentSize : '') + chomp;
  25673. if (comment) {
  25674. header += ' ' + commentString(comment.replace(/ ?[\r\n]+/g, ' '));
  25675. if (onComment)
  25676. onComment();
  25677. }
  25678. if (literal) {
  25679. value = value.replace(/\n+/g, `$&${indent}`);
  25680. return `${header}\n${indent}${start}${value}${end}`;
  25681. }
  25682. value = value
  25683. .replace(/\n+/g, '\n$&')
  25684. .replace(/(?:^|\n)([\t ].*)(?:([\n\t ]*)\n(?![\n\t ]))?/g, '$1$2') // more-indented lines aren't folded
  25685. // ^ more-ind. ^ empty ^ capture next empty lines only at end of indent
  25686. .replace(/\n+/g, `$&${indent}`);
  25687. const body = foldFlowLines(`${start}${value}${end}`, indent, FOLD_BLOCK, getFoldOptions(ctx, true));
  25688. return `${header}\n${indent}${body}`;
  25689. }
  25690. function plainString(item, ctx, onComment, onChompKeep) {
  25691. const { type, value } = item;
  25692. const { actualString, implicitKey, indent, indentStep, inFlow } = ctx;
  25693. if ((implicitKey && value.includes('\n')) ||
  25694. (inFlow && /[[\]{},]/.test(value))) {
  25695. return quotedString(value, ctx);
  25696. }
  25697. if (!value ||
  25698. /^[\n\t ,[\]{}#&*!|>'"%@`]|^[?-]$|^[?-][ \t]|[\n:][ \t]|[ \t]\n|[\n\t ]#|[\n\t :]$/.test(value)) {
  25699. // not allowed:
  25700. // - empty string, '-' or '?'
  25701. // - start with an indicator character (except [?:-]) or /[?-] /
  25702. // - '\n ', ': ' or ' \n' anywhere
  25703. // - '#' not preceded by a non-space char
  25704. // - end with ' ' or ':'
  25705. return implicitKey || inFlow || !value.includes('\n')
  25706. ? quotedString(value, ctx)
  25707. : blockString(item, ctx, onComment, onChompKeep);
  25708. }
  25709. if (!implicitKey &&
  25710. !inFlow &&
  25711. type !== Scalar.PLAIN &&
  25712. value.includes('\n')) {
  25713. // Where allowed & type not set explicitly, prefer block style for multiline strings
  25714. return blockString(item, ctx, onComment, onChompKeep);
  25715. }
  25716. if (containsDocumentMarker(value)) {
  25717. if (indent === '') {
  25718. ctx.forceBlockIndent = true;
  25719. return blockString(item, ctx, onComment, onChompKeep);
  25720. }
  25721. else if (implicitKey && indent === indentStep) {
  25722. return quotedString(value, ctx);
  25723. }
  25724. }
  25725. const str = value.replace(/\n+/g, `$&\n${indent}`);
  25726. // Verify that output will be parsed as a string, as e.g. plain numbers and
  25727. // booleans get parsed with those types in v1.2 (e.g. '42', 'true' & '0.9e-3'),
  25728. // and others in v1.1.
  25729. if (actualString) {
  25730. const test = (tag) => tag.default && tag.tag !== 'tag:yaml.org,2002:str' && tag.test?.test(str);
  25731. const { compat, tags } = ctx.doc.schema;
  25732. if (tags.some(test) || compat?.some(test))
  25733. return quotedString(value, ctx);
  25734. }
  25735. return implicitKey
  25736. ? str
  25737. : foldFlowLines(str, indent, FOLD_FLOW, getFoldOptions(ctx, false));
  25738. }
  25739. function stringifyString(item, ctx, onComment, onChompKeep) {
  25740. const { implicitKey, inFlow } = ctx;
  25741. const ss = typeof item.value === 'string'
  25742. ? item
  25743. : Object.assign({}, item, { value: String(item.value) });
  25744. let { type } = item;
  25745. if (type !== Scalar.QUOTE_DOUBLE) {
  25746. // force double quotes on control characters & unpaired surrogates
  25747. if (/[\x00-\x08\x0b-\x1f\x7f-\x9f\u{D800}-\u{DFFF}]/u.test(ss.value))
  25748. type = Scalar.QUOTE_DOUBLE;
  25749. }
  25750. const _stringify = (_type) => {
  25751. switch (_type) {
  25752. case Scalar.BLOCK_FOLDED:
  25753. case Scalar.BLOCK_LITERAL:
  25754. return implicitKey || inFlow
  25755. ? quotedString(ss.value, ctx) // blocks are not valid inside flow containers
  25756. : blockString(ss, ctx, onComment, onChompKeep);
  25757. case Scalar.QUOTE_DOUBLE:
  25758. return doubleQuotedString(ss.value, ctx);
  25759. case Scalar.QUOTE_SINGLE:
  25760. return singleQuotedString(ss.value, ctx);
  25761. case Scalar.PLAIN:
  25762. return plainString(ss, ctx, onComment, onChompKeep);
  25763. default:
  25764. return null;
  25765. }
  25766. };
  25767. let res = _stringify(type);
  25768. if (res === null) {
  25769. const { defaultKeyType, defaultStringType } = ctx.options;
  25770. const t = (implicitKey && defaultKeyType) || defaultStringType;
  25771. res = _stringify(t);
  25772. if (res === null)
  25773. throw new Error(`Unsupported default string type ${t}`);
  25774. }
  25775. return res;
  25776. }
  25777. function createStringifyContext(doc, options) {
  25778. const opt = Object.assign({
  25779. blockQuote: true,
  25780. commentString: stringifyComment,
  25781. defaultKeyType: null,
  25782. defaultStringType: 'PLAIN',
  25783. directives: null,
  25784. doubleQuotedAsJSON: false,
  25785. doubleQuotedMinMultiLineLength: 40,
  25786. falseStr: 'false',
  25787. flowCollectionPadding: true,
  25788. indentSeq: true,
  25789. lineWidth: 80,
  25790. minContentWidth: 20,
  25791. nullStr: 'null',
  25792. simpleKeys: false,
  25793. singleQuote: null,
  25794. trueStr: 'true',
  25795. verifyAliasOrder: true
  25796. }, doc.schema.toStringOptions, options);
  25797. let inFlow;
  25798. switch (opt.collectionStyle) {
  25799. case 'block':
  25800. inFlow = false;
  25801. break;
  25802. case 'flow':
  25803. inFlow = true;
  25804. break;
  25805. default:
  25806. inFlow = null;
  25807. }
  25808. return {
  25809. anchors: new Set(),
  25810. doc,
  25811. flowCollectionPadding: opt.flowCollectionPadding ? ' ' : '',
  25812. indent: '',
  25813. indentStep: typeof opt.indent === 'number' ? ' '.repeat(opt.indent) : ' ',
  25814. inFlow,
  25815. options: opt
  25816. };
  25817. }
  25818. function getTagObject(tags, item) {
  25819. if (item.tag) {
  25820. const match = tags.filter(t => t.tag === item.tag);
  25821. if (match.length > 0)
  25822. return match.find(t => t.format === item.format) ?? match[0];
  25823. }
  25824. let tagObj = undefined;
  25825. let obj;
  25826. if (isScalar$1(item)) {
  25827. obj = item.value;
  25828. const match = tags.filter(t => t.identify?.(obj));
  25829. tagObj =
  25830. match.find(t => t.format === item.format) ?? match.find(t => !t.format);
  25831. }
  25832. else {
  25833. obj = item;
  25834. tagObj = tags.find(t => t.nodeClass && obj instanceof t.nodeClass);
  25835. }
  25836. if (!tagObj) {
  25837. const name = obj?.constructor?.name ?? typeof obj;
  25838. throw new Error(`Tag not resolved for ${name} value`);
  25839. }
  25840. return tagObj;
  25841. }
  25842. // needs to be called before value stringifier to allow for circular anchor refs
  25843. function stringifyProps(node, tagObj, { anchors, doc }) {
  25844. if (!doc.directives)
  25845. return '';
  25846. const props = [];
  25847. const anchor = (isScalar$1(node) || isCollection$1(node)) && node.anchor;
  25848. if (anchor && anchorIsValid(anchor)) {
  25849. anchors.add(anchor);
  25850. props.push(`&${anchor}`);
  25851. }
  25852. const tag = node.tag ? node.tag : tagObj.default ? null : tagObj.tag;
  25853. if (tag)
  25854. props.push(doc.directives.tagString(tag));
  25855. return props.join(' ');
  25856. }
  25857. function stringify$2(item, ctx, onComment, onChompKeep) {
  25858. if (isPair(item))
  25859. return item.toString(ctx, onComment, onChompKeep);
  25860. if (isAlias(item)) {
  25861. if (ctx.doc.directives)
  25862. return item.toString(ctx);
  25863. if (ctx.resolvedAliases?.has(item)) {
  25864. throw new TypeError(`Cannot stringify circular structure without alias nodes`);
  25865. }
  25866. else {
  25867. if (ctx.resolvedAliases)
  25868. ctx.resolvedAliases.add(item);
  25869. else
  25870. ctx.resolvedAliases = new Set([item]);
  25871. item = item.resolve(ctx.doc);
  25872. }
  25873. }
  25874. let tagObj = undefined;
  25875. const node = isNode$1(item)
  25876. ? item
  25877. : ctx.doc.createNode(item, { onTagObj: o => (tagObj = o) });
  25878. if (!tagObj)
  25879. tagObj = getTagObject(ctx.doc.schema.tags, node);
  25880. const props = stringifyProps(node, tagObj, ctx);
  25881. if (props.length > 0)
  25882. ctx.indentAtStart = (ctx.indentAtStart ?? 0) + props.length + 1;
  25883. const str = typeof tagObj.stringify === 'function'
  25884. ? tagObj.stringify(node, ctx, onComment, onChompKeep)
  25885. : isScalar$1(node)
  25886. ? stringifyString(node, ctx, onComment, onChompKeep)
  25887. : node.toString(ctx, onComment, onChompKeep);
  25888. if (!props)
  25889. return str;
  25890. return isScalar$1(node) || str[0] === '{' || str[0] === '['
  25891. ? `${props} ${str}`
  25892. : `${props}\n${ctx.indent}${str}`;
  25893. }
  25894. function stringifyPair({ key, value }, ctx, onComment, onChompKeep) {
  25895. const { allNullValues, doc, indent, indentStep, options: { commentString, indentSeq, simpleKeys } } = ctx;
  25896. let keyComment = (isNode$1(key) && key.comment) || null;
  25897. if (simpleKeys) {
  25898. if (keyComment) {
  25899. throw new Error('With simple keys, key nodes cannot have comments');
  25900. }
  25901. if (isCollection$1(key) || (!isNode$1(key) && typeof key === 'object')) {
  25902. const msg = 'With simple keys, collection cannot be used as a key value';
  25903. throw new Error(msg);
  25904. }
  25905. }
  25906. let explicitKey = !simpleKeys &&
  25907. (!key ||
  25908. (keyComment && value == null && !ctx.inFlow) ||
  25909. isCollection$1(key) ||
  25910. (isScalar$1(key)
  25911. ? key.type === Scalar.BLOCK_FOLDED || key.type === Scalar.BLOCK_LITERAL
  25912. : typeof key === 'object'));
  25913. ctx = Object.assign({}, ctx, {
  25914. allNullValues: false,
  25915. implicitKey: !explicitKey && (simpleKeys || !allNullValues),
  25916. indent: indent + indentStep
  25917. });
  25918. let keyCommentDone = false;
  25919. let chompKeep = false;
  25920. let str = stringify$2(key, ctx, () => (keyCommentDone = true), () => (chompKeep = true));
  25921. if (!explicitKey && !ctx.inFlow && str.length > 1024) {
  25922. if (simpleKeys)
  25923. throw new Error('With simple keys, single line scalar must not span more than 1024 characters');
  25924. explicitKey = true;
  25925. }
  25926. if (ctx.inFlow) {
  25927. if (allNullValues || value == null) {
  25928. if (keyCommentDone && onComment)
  25929. onComment();
  25930. return str === '' ? '?' : explicitKey ? `? ${str}` : str;
  25931. }
  25932. }
  25933. else if ((allNullValues && !simpleKeys) || (value == null && explicitKey)) {
  25934. str = `? ${str}`;
  25935. if (keyComment && !keyCommentDone) {
  25936. str += lineComment(str, ctx.indent, commentString(keyComment));
  25937. }
  25938. else if (chompKeep && onChompKeep)
  25939. onChompKeep();
  25940. return str;
  25941. }
  25942. if (keyCommentDone)
  25943. keyComment = null;
  25944. if (explicitKey) {
  25945. if (keyComment)
  25946. str += lineComment(str, ctx.indent, commentString(keyComment));
  25947. str = `? ${str}\n${indent}:`;
  25948. }
  25949. else {
  25950. str = `${str}:`;
  25951. if (keyComment)
  25952. str += lineComment(str, ctx.indent, commentString(keyComment));
  25953. }
  25954. let vsb, vcb, valueComment;
  25955. if (isNode$1(value)) {
  25956. vsb = !!value.spaceBefore;
  25957. vcb = value.commentBefore;
  25958. valueComment = value.comment;
  25959. }
  25960. else {
  25961. vsb = false;
  25962. vcb = null;
  25963. valueComment = null;
  25964. if (value && typeof value === 'object')
  25965. value = doc.createNode(value);
  25966. }
  25967. ctx.implicitKey = false;
  25968. if (!explicitKey && !keyComment && isScalar$1(value))
  25969. ctx.indentAtStart = str.length + 1;
  25970. chompKeep = false;
  25971. if (!indentSeq &&
  25972. indentStep.length >= 2 &&
  25973. !ctx.inFlow &&
  25974. !explicitKey &&
  25975. isSeq(value) &&
  25976. !value.flow &&
  25977. !value.tag &&
  25978. !value.anchor) {
  25979. // If indentSeq === false, consider '- ' as part of indentation where possible
  25980. ctx.indent = ctx.indent.substring(2);
  25981. }
  25982. let valueCommentDone = false;
  25983. const valueStr = stringify$2(value, ctx, () => (valueCommentDone = true), () => (chompKeep = true));
  25984. let ws = ' ';
  25985. if (keyComment || vsb || vcb) {
  25986. ws = vsb ? '\n' : '';
  25987. if (vcb) {
  25988. const cs = commentString(vcb);
  25989. ws += `\n${indentComment(cs, ctx.indent)}`;
  25990. }
  25991. if (valueStr === '' && !ctx.inFlow) {
  25992. if (ws === '\n')
  25993. ws = '\n\n';
  25994. }
  25995. else {
  25996. ws += `\n${ctx.indent}`;
  25997. }
  25998. }
  25999. else if (!explicitKey && isCollection$1(value)) {
  26000. const vs0 = valueStr[0];
  26001. const nl0 = valueStr.indexOf('\n');
  26002. const hasNewline = nl0 !== -1;
  26003. const flow = ctx.inFlow ?? value.flow ?? value.items.length === 0;
  26004. if (hasNewline || !flow) {
  26005. let hasPropsLine = false;
  26006. if (hasNewline && (vs0 === '&' || vs0 === '!')) {
  26007. let sp0 = valueStr.indexOf(' ');
  26008. if (vs0 === '&' &&
  26009. sp0 !== -1 &&
  26010. sp0 < nl0 &&
  26011. valueStr[sp0 + 1] === '!') {
  26012. sp0 = valueStr.indexOf(' ', sp0 + 1);
  26013. }
  26014. if (sp0 === -1 || nl0 < sp0)
  26015. hasPropsLine = true;
  26016. }
  26017. if (!hasPropsLine)
  26018. ws = `\n${ctx.indent}`;
  26019. }
  26020. }
  26021. else if (valueStr === '' || valueStr[0] === '\n') {
  26022. ws = '';
  26023. }
  26024. str += ws + valueStr;
  26025. if (ctx.inFlow) {
  26026. if (valueCommentDone && onComment)
  26027. onComment();
  26028. }
  26029. else if (valueComment && !valueCommentDone) {
  26030. str += lineComment(str, ctx.indent, commentString(valueComment));
  26031. }
  26032. else if (chompKeep && onChompKeep) {
  26033. onChompKeep();
  26034. }
  26035. return str;
  26036. }
  26037. function warn(logLevel, warning) {
  26038. if (logLevel === 'debug' || logLevel === 'warn') {
  26039. // https://github.com/typescript-eslint/typescript-eslint/issues/7478
  26040. // eslint-disable-next-line @typescript-eslint/prefer-optional-chain
  26041. if (typeof process !== 'undefined' && process.emitWarning)
  26042. process.emitWarning(warning);
  26043. else
  26044. console.warn(warning);
  26045. }
  26046. }
  26047. const MERGE_KEY = '<<';
  26048. function addPairToJSMap(ctx, map, { key, value }) {
  26049. if (ctx?.doc.schema.merge && isMergeKey(key)) {
  26050. value = isAlias(value) ? value.resolve(ctx.doc) : value;
  26051. if (isSeq(value))
  26052. for (const it of value.items)
  26053. mergeToJSMap(ctx, map, it);
  26054. else if (Array.isArray(value))
  26055. for (const it of value)
  26056. mergeToJSMap(ctx, map, it);
  26057. else
  26058. mergeToJSMap(ctx, map, value);
  26059. }
  26060. else {
  26061. const jsKey = toJS(key, '', ctx);
  26062. if (map instanceof Map) {
  26063. map.set(jsKey, toJS(value, jsKey, ctx));
  26064. }
  26065. else if (map instanceof Set) {
  26066. map.add(jsKey);
  26067. }
  26068. else {
  26069. const stringKey = stringifyKey(key, jsKey, ctx);
  26070. const jsValue = toJS(value, stringKey, ctx);
  26071. if (stringKey in map)
  26072. Object.defineProperty(map, stringKey, {
  26073. value: jsValue,
  26074. writable: true,
  26075. enumerable: true,
  26076. configurable: true
  26077. });
  26078. else
  26079. map[stringKey] = jsValue;
  26080. }
  26081. }
  26082. return map;
  26083. }
  26084. const isMergeKey = (key) => key === MERGE_KEY ||
  26085. (isScalar$1(key) &&
  26086. key.value === MERGE_KEY &&
  26087. (!key.type || key.type === Scalar.PLAIN));
  26088. // If the value associated with a merge key is a single mapping node, each of
  26089. // its key/value pairs is inserted into the current mapping, unless the key
  26090. // already exists in it. If the value associated with the merge key is a
  26091. // sequence, then this sequence is expected to contain mapping nodes and each
  26092. // of these nodes is merged in turn according to its order in the sequence.
  26093. // Keys in mapping nodes earlier in the sequence override keys specified in
  26094. // later mapping nodes. -- http://yaml.org/type/merge.html
  26095. function mergeToJSMap(ctx, map, value) {
  26096. const source = ctx && isAlias(value) ? value.resolve(ctx.doc) : value;
  26097. if (!isMap(source))
  26098. throw new Error('Merge sources must be maps or map aliases');
  26099. const srcMap = source.toJSON(null, ctx, Map);
  26100. for (const [key, value] of srcMap) {
  26101. if (map instanceof Map) {
  26102. if (!map.has(key))
  26103. map.set(key, value);
  26104. }
  26105. else if (map instanceof Set) {
  26106. map.add(key);
  26107. }
  26108. else if (!Object.prototype.hasOwnProperty.call(map, key)) {
  26109. Object.defineProperty(map, key, {
  26110. value,
  26111. writable: true,
  26112. enumerable: true,
  26113. configurable: true
  26114. });
  26115. }
  26116. }
  26117. return map;
  26118. }
  26119. function stringifyKey(key, jsKey, ctx) {
  26120. if (jsKey === null)
  26121. return '';
  26122. if (typeof jsKey !== 'object')
  26123. return String(jsKey);
  26124. if (isNode$1(key) && ctx?.doc) {
  26125. const strCtx = createStringifyContext(ctx.doc, {});
  26126. strCtx.anchors = new Set();
  26127. for (const node of ctx.anchors.keys())
  26128. strCtx.anchors.add(node.anchor);
  26129. strCtx.inFlow = true;
  26130. strCtx.inStringifyKey = true;
  26131. const strKey = key.toString(strCtx);
  26132. if (!ctx.mapKeyWarned) {
  26133. let jsonStr = JSON.stringify(strKey);
  26134. if (jsonStr.length > 40)
  26135. jsonStr = jsonStr.substring(0, 36) + '..."';
  26136. 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.`);
  26137. ctx.mapKeyWarned = true;
  26138. }
  26139. return strKey;
  26140. }
  26141. return JSON.stringify(jsKey);
  26142. }
  26143. function createPair(key, value, ctx) {
  26144. const k = createNode(key, undefined, ctx);
  26145. const v = createNode(value, undefined, ctx);
  26146. return new Pair(k, v);
  26147. }
  26148. class Pair {
  26149. constructor(key, value = null) {
  26150. Object.defineProperty(this, NODE_TYPE, { value: PAIR });
  26151. this.key = key;
  26152. this.value = value;
  26153. }
  26154. clone(schema) {
  26155. let { key, value } = this;
  26156. if (isNode$1(key))
  26157. key = key.clone(schema);
  26158. if (isNode$1(value))
  26159. value = value.clone(schema);
  26160. return new Pair(key, value);
  26161. }
  26162. toJSON(_, ctx) {
  26163. const pair = ctx?.mapAsMap ? new Map() : {};
  26164. return addPairToJSMap(ctx, pair, this);
  26165. }
  26166. toString(ctx, onComment, onChompKeep) {
  26167. return ctx?.doc
  26168. ? stringifyPair(this, ctx, onComment, onChompKeep)
  26169. : JSON.stringify(this);
  26170. }
  26171. }
  26172. function stringifyCollection(collection, ctx, options) {
  26173. const flow = ctx.inFlow ?? collection.flow;
  26174. const stringify = flow ? stringifyFlowCollection : stringifyBlockCollection;
  26175. return stringify(collection, ctx, options);
  26176. }
  26177. function stringifyBlockCollection({ comment, items }, ctx, { blockItemPrefix, flowChars, itemIndent, onChompKeep, onComment }) {
  26178. const { indent, options: { commentString } } = ctx;
  26179. const itemCtx = Object.assign({}, ctx, { indent: itemIndent, type: null });
  26180. let chompKeep = false; // flag for the preceding node's status
  26181. const lines = [];
  26182. for (let i = 0; i < items.length; ++i) {
  26183. const item = items[i];
  26184. let comment = null;
  26185. if (isNode$1(item)) {
  26186. if (!chompKeep && item.spaceBefore)
  26187. lines.push('');
  26188. addCommentBefore(ctx, lines, item.commentBefore, chompKeep);
  26189. if (item.comment)
  26190. comment = item.comment;
  26191. }
  26192. else if (isPair(item)) {
  26193. const ik = isNode$1(item.key) ? item.key : null;
  26194. if (ik) {
  26195. if (!chompKeep && ik.spaceBefore)
  26196. lines.push('');
  26197. addCommentBefore(ctx, lines, ik.commentBefore, chompKeep);
  26198. }
  26199. }
  26200. chompKeep = false;
  26201. let str = stringify$2(item, itemCtx, () => (comment = null), () => (chompKeep = true));
  26202. if (comment)
  26203. str += lineComment(str, itemIndent, commentString(comment));
  26204. if (chompKeep && comment)
  26205. chompKeep = false;
  26206. lines.push(blockItemPrefix + str);
  26207. }
  26208. let str;
  26209. if (lines.length === 0) {
  26210. str = flowChars.start + flowChars.end;
  26211. }
  26212. else {
  26213. str = lines[0];
  26214. for (let i = 1; i < lines.length; ++i) {
  26215. const line = lines[i];
  26216. str += line ? `\n${indent}${line}` : '\n';
  26217. }
  26218. }
  26219. if (comment) {
  26220. str += '\n' + indentComment(commentString(comment), indent);
  26221. if (onComment)
  26222. onComment();
  26223. }
  26224. else if (chompKeep && onChompKeep)
  26225. onChompKeep();
  26226. return str;
  26227. }
  26228. function stringifyFlowCollection({ items }, ctx, { flowChars, itemIndent }) {
  26229. const { indent, indentStep, flowCollectionPadding: fcPadding, options: { commentString } } = ctx;
  26230. itemIndent += indentStep;
  26231. const itemCtx = Object.assign({}, ctx, {
  26232. indent: itemIndent,
  26233. inFlow: true,
  26234. type: null
  26235. });
  26236. let reqNewline = false;
  26237. let linesAtValue = 0;
  26238. const lines = [];
  26239. for (let i = 0; i < items.length; ++i) {
  26240. const item = items[i];
  26241. let comment = null;
  26242. if (isNode$1(item)) {
  26243. if (item.spaceBefore)
  26244. lines.push('');
  26245. addCommentBefore(ctx, lines, item.commentBefore, false);
  26246. if (item.comment)
  26247. comment = item.comment;
  26248. }
  26249. else if (isPair(item)) {
  26250. const ik = isNode$1(item.key) ? item.key : null;
  26251. if (ik) {
  26252. if (ik.spaceBefore)
  26253. lines.push('');
  26254. addCommentBefore(ctx, lines, ik.commentBefore, false);
  26255. if (ik.comment)
  26256. reqNewline = true;
  26257. }
  26258. const iv = isNode$1(item.value) ? item.value : null;
  26259. if (iv) {
  26260. if (iv.comment)
  26261. comment = iv.comment;
  26262. if (iv.commentBefore)
  26263. reqNewline = true;
  26264. }
  26265. else if (item.value == null && ik?.comment) {
  26266. comment = ik.comment;
  26267. }
  26268. }
  26269. if (comment)
  26270. reqNewline = true;
  26271. let str = stringify$2(item, itemCtx, () => (comment = null));
  26272. if (i < items.length - 1)
  26273. str += ',';
  26274. if (comment)
  26275. str += lineComment(str, itemIndent, commentString(comment));
  26276. if (!reqNewline && (lines.length > linesAtValue || str.includes('\n')))
  26277. reqNewline = true;
  26278. lines.push(str);
  26279. linesAtValue = lines.length;
  26280. }
  26281. const { start, end } = flowChars;
  26282. if (lines.length === 0) {
  26283. return start + end;
  26284. }
  26285. else {
  26286. if (!reqNewline) {
  26287. const len = lines.reduce((sum, line) => sum + line.length + 2, 2);
  26288. reqNewline = ctx.options.lineWidth > 0 && len > ctx.options.lineWidth;
  26289. }
  26290. if (reqNewline) {
  26291. let str = start;
  26292. for (const line of lines)
  26293. str += line ? `\n${indentStep}${indent}${line}` : '\n';
  26294. return `${str}\n${indent}${end}`;
  26295. }
  26296. else {
  26297. return `${start}${fcPadding}${lines.join(' ')}${fcPadding}${end}`;
  26298. }
  26299. }
  26300. }
  26301. function addCommentBefore({ indent, options: { commentString } }, lines, comment, chompKeep) {
  26302. if (comment && chompKeep)
  26303. comment = comment.replace(/^\n+/, '');
  26304. if (comment) {
  26305. const ic = indentComment(commentString(comment), indent);
  26306. lines.push(ic.trimStart()); // Avoid double indent on first line
  26307. }
  26308. }
  26309. function findPair(items, key) {
  26310. const k = isScalar$1(key) ? key.value : key;
  26311. for (const it of items) {
  26312. if (isPair(it)) {
  26313. if (it.key === key || it.key === k)
  26314. return it;
  26315. if (isScalar$1(it.key) && it.key.value === k)
  26316. return it;
  26317. }
  26318. }
  26319. return undefined;
  26320. }
  26321. class YAMLMap extends Collection {
  26322. static get tagName() {
  26323. return 'tag:yaml.org,2002:map';
  26324. }
  26325. constructor(schema) {
  26326. super(MAP, schema);
  26327. this.items = [];
  26328. }
  26329. /**
  26330. * A generic collection parsing method that can be extended
  26331. * to other node classes that inherit from YAMLMap
  26332. */
  26333. static from(schema, obj, ctx) {
  26334. const { keepUndefined, replacer } = ctx;
  26335. const map = new this(schema);
  26336. const add = (key, value) => {
  26337. if (typeof replacer === 'function')
  26338. value = replacer.call(obj, key, value);
  26339. else if (Array.isArray(replacer) && !replacer.includes(key))
  26340. return;
  26341. if (value !== undefined || keepUndefined)
  26342. map.items.push(createPair(key, value, ctx));
  26343. };
  26344. if (obj instanceof Map) {
  26345. for (const [key, value] of obj)
  26346. add(key, value);
  26347. }
  26348. else if (obj && typeof obj === 'object') {
  26349. for (const key of Object.keys(obj))
  26350. add(key, obj[key]);
  26351. }
  26352. if (typeof schema.sortMapEntries === 'function') {
  26353. map.items.sort(schema.sortMapEntries);
  26354. }
  26355. return map;
  26356. }
  26357. /**
  26358. * Adds a value to the collection.
  26359. *
  26360. * @param overwrite - If not set `true`, using a key that is already in the
  26361. * collection will throw. Otherwise, overwrites the previous value.
  26362. */
  26363. add(pair, overwrite) {
  26364. let _pair;
  26365. if (isPair(pair))
  26366. _pair = pair;
  26367. else if (!pair || typeof pair !== 'object' || !('key' in pair)) {
  26368. // In TypeScript, this never happens.
  26369. _pair = new Pair(pair, pair?.value);
  26370. }
  26371. else
  26372. _pair = new Pair(pair.key, pair.value);
  26373. const prev = findPair(this.items, _pair.key);
  26374. const sortEntries = this.schema?.sortMapEntries;
  26375. if (prev) {
  26376. if (!overwrite)
  26377. throw new Error(`Key ${_pair.key} already set`);
  26378. // For scalars, keep the old node & its comments and anchors
  26379. if (isScalar$1(prev.value) && isScalarValue(_pair.value))
  26380. prev.value.value = _pair.value;
  26381. else
  26382. prev.value = _pair.value;
  26383. }
  26384. else if (sortEntries) {
  26385. const i = this.items.findIndex(item => sortEntries(_pair, item) < 0);
  26386. if (i === -1)
  26387. this.items.push(_pair);
  26388. else
  26389. this.items.splice(i, 0, _pair);
  26390. }
  26391. else {
  26392. this.items.push(_pair);
  26393. }
  26394. }
  26395. delete(key) {
  26396. const it = findPair(this.items, key);
  26397. if (!it)
  26398. return false;
  26399. const del = this.items.splice(this.items.indexOf(it), 1);
  26400. return del.length > 0;
  26401. }
  26402. get(key, keepScalar) {
  26403. const it = findPair(this.items, key);
  26404. const node = it?.value;
  26405. return (!keepScalar && isScalar$1(node) ? node.value : node) ?? undefined;
  26406. }
  26407. has(key) {
  26408. return !!findPair(this.items, key);
  26409. }
  26410. set(key, value) {
  26411. this.add(new Pair(key, value), true);
  26412. }
  26413. /**
  26414. * @param ctx - Conversion context, originally set in Document#toJS()
  26415. * @param {Class} Type - If set, forces the returned collection type
  26416. * @returns Instance of Type, Map, or Object
  26417. */
  26418. toJSON(_, ctx, Type) {
  26419. const map = Type ? new Type() : ctx?.mapAsMap ? new Map() : {};
  26420. if (ctx?.onCreate)
  26421. ctx.onCreate(map);
  26422. for (const item of this.items)
  26423. addPairToJSMap(ctx, map, item);
  26424. return map;
  26425. }
  26426. toString(ctx, onComment, onChompKeep) {
  26427. if (!ctx)
  26428. return JSON.stringify(this);
  26429. for (const item of this.items) {
  26430. if (!isPair(item))
  26431. throw new Error(`Map items must all be pairs; found ${JSON.stringify(item)} instead`);
  26432. }
  26433. if (!ctx.allNullValues && this.hasAllNullValues(false))
  26434. ctx = Object.assign({}, ctx, { allNullValues: true });
  26435. return stringifyCollection(this, ctx, {
  26436. blockItemPrefix: '',
  26437. flowChars: { start: '{', end: '}' },
  26438. itemIndent: ctx.indent || '',
  26439. onChompKeep,
  26440. onComment
  26441. });
  26442. }
  26443. }
  26444. const map$1 = {
  26445. collection: 'map',
  26446. default: true,
  26447. nodeClass: YAMLMap,
  26448. tag: 'tag:yaml.org,2002:map',
  26449. resolve(map, onError) {
  26450. if (!isMap(map))
  26451. onError('Expected a mapping for this tag');
  26452. return map;
  26453. },
  26454. createNode: (schema, obj, ctx) => YAMLMap.from(schema, obj, ctx)
  26455. };
  26456. class YAMLSeq extends Collection {
  26457. static get tagName() {
  26458. return 'tag:yaml.org,2002:seq';
  26459. }
  26460. constructor(schema) {
  26461. super(SEQ, schema);
  26462. this.items = [];
  26463. }
  26464. add(value) {
  26465. this.items.push(value);
  26466. }
  26467. /**
  26468. * Removes a value from the collection.
  26469. *
  26470. * `key` must contain a representation of an integer for this to succeed.
  26471. * It may be wrapped in a `Scalar`.
  26472. *
  26473. * @returns `true` if the item was found and removed.
  26474. */
  26475. delete(key) {
  26476. const idx = asItemIndex(key);
  26477. if (typeof idx !== 'number')
  26478. return false;
  26479. const del = this.items.splice(idx, 1);
  26480. return del.length > 0;
  26481. }
  26482. get(key, keepScalar) {
  26483. const idx = asItemIndex(key);
  26484. if (typeof idx !== 'number')
  26485. return undefined;
  26486. const it = this.items[idx];
  26487. return !keepScalar && isScalar$1(it) ? it.value : it;
  26488. }
  26489. /**
  26490. * Checks if the collection includes a value with the key `key`.
  26491. *
  26492. * `key` must contain a representation of an integer for this to succeed.
  26493. * It may be wrapped in a `Scalar`.
  26494. */
  26495. has(key) {
  26496. const idx = asItemIndex(key);
  26497. return typeof idx === 'number' && idx < this.items.length;
  26498. }
  26499. /**
  26500. * Sets a value in this collection. For `!!set`, `value` needs to be a
  26501. * boolean to add/remove the item from the set.
  26502. *
  26503. * If `key` does not contain a representation of an integer, this will throw.
  26504. * It may be wrapped in a `Scalar`.
  26505. */
  26506. set(key, value) {
  26507. const idx = asItemIndex(key);
  26508. if (typeof idx !== 'number')
  26509. throw new Error(`Expected a valid index, not ${key}.`);
  26510. const prev = this.items[idx];
  26511. if (isScalar$1(prev) && isScalarValue(value))
  26512. prev.value = value;
  26513. else
  26514. this.items[idx] = value;
  26515. }
  26516. toJSON(_, ctx) {
  26517. const seq = [];
  26518. if (ctx?.onCreate)
  26519. ctx.onCreate(seq);
  26520. let i = 0;
  26521. for (const item of this.items)
  26522. seq.push(toJS(item, String(i++), ctx));
  26523. return seq;
  26524. }
  26525. toString(ctx, onComment, onChompKeep) {
  26526. if (!ctx)
  26527. return JSON.stringify(this);
  26528. return stringifyCollection(this, ctx, {
  26529. blockItemPrefix: '- ',
  26530. flowChars: { start: '[', end: ']' },
  26531. itemIndent: (ctx.indent || '') + ' ',
  26532. onChompKeep,
  26533. onComment
  26534. });
  26535. }
  26536. static from(schema, obj, ctx) {
  26537. const { replacer } = ctx;
  26538. const seq = new this(schema);
  26539. if (obj && Symbol.iterator in Object(obj)) {
  26540. let i = 0;
  26541. for (let it of obj) {
  26542. if (typeof replacer === 'function') {
  26543. const key = obj instanceof Set ? it : String(i++);
  26544. it = replacer.call(obj, key, it);
  26545. }
  26546. seq.items.push(createNode(it, undefined, ctx));
  26547. }
  26548. }
  26549. return seq;
  26550. }
  26551. }
  26552. function asItemIndex(key) {
  26553. let idx = isScalar$1(key) ? key.value : key;
  26554. if (idx && typeof idx === 'string')
  26555. idx = Number(idx);
  26556. return typeof idx === 'number' && Number.isInteger(idx) && idx >= 0
  26557. ? idx
  26558. : null;
  26559. }
  26560. const seq = {
  26561. collection: 'seq',
  26562. default: true,
  26563. nodeClass: YAMLSeq,
  26564. tag: 'tag:yaml.org,2002:seq',
  26565. resolve(seq, onError) {
  26566. if (!isSeq(seq))
  26567. onError('Expected a sequence for this tag');
  26568. return seq;
  26569. },
  26570. createNode: (schema, obj, ctx) => YAMLSeq.from(schema, obj, ctx)
  26571. };
  26572. const string = {
  26573. identify: value => typeof value === 'string',
  26574. default: true,
  26575. tag: 'tag:yaml.org,2002:str',
  26576. resolve: str => str,
  26577. stringify(item, ctx, onComment, onChompKeep) {
  26578. ctx = Object.assign({ actualString: true }, ctx);
  26579. return stringifyString(item, ctx, onComment, onChompKeep);
  26580. }
  26581. };
  26582. const nullTag = {
  26583. identify: value => value == null,
  26584. createNode: () => new Scalar(null),
  26585. default: true,
  26586. tag: 'tag:yaml.org,2002:null',
  26587. test: /^(?:~|[Nn]ull|NULL)?$/,
  26588. resolve: () => new Scalar(null),
  26589. stringify: ({ source }, ctx) => typeof source === 'string' && nullTag.test.test(source)
  26590. ? source
  26591. : ctx.options.nullStr
  26592. };
  26593. const boolTag = {
  26594. identify: value => typeof value === 'boolean',
  26595. default: true,
  26596. tag: 'tag:yaml.org,2002:bool',
  26597. test: /^(?:[Tt]rue|TRUE|[Ff]alse|FALSE)$/,
  26598. resolve: str => new Scalar(str[0] === 't' || str[0] === 'T'),
  26599. stringify({ source, value }, ctx) {
  26600. if (source && boolTag.test.test(source)) {
  26601. const sv = source[0] === 't' || source[0] === 'T';
  26602. if (value === sv)
  26603. return source;
  26604. }
  26605. return value ? ctx.options.trueStr : ctx.options.falseStr;
  26606. }
  26607. };
  26608. function stringifyNumber({ format, minFractionDigits, tag, value }) {
  26609. if (typeof value === 'bigint')
  26610. return String(value);
  26611. const num = typeof value === 'number' ? value : Number(value);
  26612. if (!isFinite(num))
  26613. return isNaN(num) ? '.nan' : num < 0 ? '-.inf' : '.inf';
  26614. let n = JSON.stringify(value);
  26615. if (!format &&
  26616. minFractionDigits &&
  26617. (!tag || tag === 'tag:yaml.org,2002:float') &&
  26618. /^\d/.test(n)) {
  26619. let i = n.indexOf('.');
  26620. if (i < 0) {
  26621. i = n.length;
  26622. n += '.';
  26623. }
  26624. let d = minFractionDigits - (n.length - i - 1);
  26625. while (d-- > 0)
  26626. n += '0';
  26627. }
  26628. return n;
  26629. }
  26630. const floatNaN$1 = {
  26631. identify: value => typeof value === 'number',
  26632. default: true,
  26633. tag: 'tag:yaml.org,2002:float',
  26634. test: /^(?:[-+]?\.(?:inf|Inf|INF)|\.nan|\.NaN|\.NAN)$/,
  26635. resolve: str => str.slice(-3).toLowerCase() === 'nan'
  26636. ? NaN
  26637. : str[0] === '-'
  26638. ? Number.NEGATIVE_INFINITY
  26639. : Number.POSITIVE_INFINITY,
  26640. stringify: stringifyNumber
  26641. };
  26642. const floatExp$1 = {
  26643. identify: value => typeof value === 'number',
  26644. default: true,
  26645. tag: 'tag:yaml.org,2002:float',
  26646. format: 'EXP',
  26647. test: /^[-+]?(?:\.[0-9]+|[0-9]+(?:\.[0-9]*)?)[eE][-+]?[0-9]+$/,
  26648. resolve: str => parseFloat(str),
  26649. stringify(node) {
  26650. const num = Number(node.value);
  26651. return isFinite(num) ? num.toExponential() : stringifyNumber(node);
  26652. }
  26653. };
  26654. const float$1 = {
  26655. identify: value => typeof value === 'number',
  26656. default: true,
  26657. tag: 'tag:yaml.org,2002:float',
  26658. test: /^[-+]?(?:\.[0-9]+|[0-9]+\.[0-9]*)$/,
  26659. resolve(str) {
  26660. const node = new Scalar(parseFloat(str));
  26661. const dot = str.indexOf('.');
  26662. if (dot !== -1 && str[str.length - 1] === '0')
  26663. node.minFractionDigits = str.length - dot - 1;
  26664. return node;
  26665. },
  26666. stringify: stringifyNumber
  26667. };
  26668. const intIdentify$2 = (value) => typeof value === 'bigint' || Number.isInteger(value);
  26669. const intResolve$1 = (str, offset, radix, { intAsBigInt }) => (intAsBigInt ? BigInt(str) : parseInt(str.substring(offset), radix));
  26670. function intStringify$1(node, radix, prefix) {
  26671. const { value } = node;
  26672. if (intIdentify$2(value) && value >= 0)
  26673. return prefix + value.toString(radix);
  26674. return stringifyNumber(node);
  26675. }
  26676. const intOct$1 = {
  26677. identify: value => intIdentify$2(value) && value >= 0,
  26678. default: true,
  26679. tag: 'tag:yaml.org,2002:int',
  26680. format: 'OCT',
  26681. test: /^0o[0-7]+$/,
  26682. resolve: (str, _onError, opt) => intResolve$1(str, 2, 8, opt),
  26683. stringify: node => intStringify$1(node, 8, '0o')
  26684. };
  26685. const int$1 = {
  26686. identify: intIdentify$2,
  26687. default: true,
  26688. tag: 'tag:yaml.org,2002:int',
  26689. test: /^[-+]?[0-9]+$/,
  26690. resolve: (str, _onError, opt) => intResolve$1(str, 0, 10, opt),
  26691. stringify: stringifyNumber
  26692. };
  26693. const intHex$1 = {
  26694. identify: value => intIdentify$2(value) && value >= 0,
  26695. default: true,
  26696. tag: 'tag:yaml.org,2002:int',
  26697. format: 'HEX',
  26698. test: /^0x[0-9a-fA-F]+$/,
  26699. resolve: (str, _onError, opt) => intResolve$1(str, 2, 16, opt),
  26700. stringify: node => intStringify$1(node, 16, '0x')
  26701. };
  26702. const schema$2 = [
  26703. map$1,
  26704. seq,
  26705. string,
  26706. nullTag,
  26707. boolTag,
  26708. intOct$1,
  26709. int$1,
  26710. intHex$1,
  26711. floatNaN$1,
  26712. floatExp$1,
  26713. float$1
  26714. ];
  26715. function intIdentify$1(value) {
  26716. return typeof value === 'bigint' || Number.isInteger(value);
  26717. }
  26718. const stringifyJSON = ({ value }) => JSON.stringify(value);
  26719. const jsonScalars = [
  26720. {
  26721. identify: value => typeof value === 'string',
  26722. default: true,
  26723. tag: 'tag:yaml.org,2002:str',
  26724. resolve: str => str,
  26725. stringify: stringifyJSON
  26726. },
  26727. {
  26728. identify: value => value == null,
  26729. createNode: () => new Scalar(null),
  26730. default: true,
  26731. tag: 'tag:yaml.org,2002:null',
  26732. test: /^null$/,
  26733. resolve: () => null,
  26734. stringify: stringifyJSON
  26735. },
  26736. {
  26737. identify: value => typeof value === 'boolean',
  26738. default: true,
  26739. tag: 'tag:yaml.org,2002:bool',
  26740. test: /^true|false$/,
  26741. resolve: str => str === 'true',
  26742. stringify: stringifyJSON
  26743. },
  26744. {
  26745. identify: intIdentify$1,
  26746. default: true,
  26747. tag: 'tag:yaml.org,2002:int',
  26748. test: /^-?(?:0|[1-9][0-9]*)$/,
  26749. resolve: (str, _onError, { intAsBigInt }) => intAsBigInt ? BigInt(str) : parseInt(str, 10),
  26750. stringify: ({ value }) => intIdentify$1(value) ? value.toString() : JSON.stringify(value)
  26751. },
  26752. {
  26753. identify: value => typeof value === 'number',
  26754. default: true,
  26755. tag: 'tag:yaml.org,2002:float',
  26756. test: /^-?(?:0|[1-9][0-9]*)(?:\.[0-9]*)?(?:[eE][-+]?[0-9]+)?$/,
  26757. resolve: str => parseFloat(str),
  26758. stringify: stringifyJSON
  26759. }
  26760. ];
  26761. const jsonError = {
  26762. default: true,
  26763. tag: '',
  26764. test: /^/,
  26765. resolve(str, onError) {
  26766. onError(`Unresolved plain scalar ${JSON.stringify(str)}`);
  26767. return str;
  26768. }
  26769. };
  26770. const schema$1 = [map$1, seq].concat(jsonScalars, jsonError);
  26771. const binary = {
  26772. identify: value => value instanceof Uint8Array, // Buffer inherits from Uint8Array
  26773. default: false,
  26774. tag: 'tag:yaml.org,2002:binary',
  26775. /**
  26776. * Returns a Buffer in node and an Uint8Array in browsers
  26777. *
  26778. * To use the resulting buffer as an image, you'll want to do something like:
  26779. *
  26780. * const blob = new Blob([buffer], { type: 'image/jpeg' })
  26781. * document.querySelector('#photo').src = URL.createObjectURL(blob)
  26782. */
  26783. resolve(src, onError) {
  26784. if (typeof Buffer === 'function') {
  26785. return Buffer.from(src, 'base64');
  26786. }
  26787. else if (typeof atob === 'function') {
  26788. // On IE 11, atob() can't handle newlines
  26789. const str = atob(src.replace(/[\n\r]/g, ''));
  26790. const buffer = new Uint8Array(str.length);
  26791. for (let i = 0; i < str.length; ++i)
  26792. buffer[i] = str.charCodeAt(i);
  26793. return buffer;
  26794. }
  26795. else {
  26796. onError('This environment does not support reading binary tags; either Buffer or atob is required');
  26797. return src;
  26798. }
  26799. },
  26800. stringify({ comment, type, value }, ctx, onComment, onChompKeep) {
  26801. const buf = value; // checked earlier by binary.identify()
  26802. let str;
  26803. if (typeof Buffer === 'function') {
  26804. str =
  26805. buf instanceof Buffer
  26806. ? buf.toString('base64')
  26807. : Buffer.from(buf.buffer).toString('base64');
  26808. }
  26809. else if (typeof btoa === 'function') {
  26810. let s = '';
  26811. for (let i = 0; i < buf.length; ++i)
  26812. s += String.fromCharCode(buf[i]);
  26813. str = btoa(s);
  26814. }
  26815. else {
  26816. throw new Error('This environment does not support writing binary tags; either Buffer or btoa is required');
  26817. }
  26818. if (!type)
  26819. type = Scalar.BLOCK_LITERAL;
  26820. if (type !== Scalar.QUOTE_DOUBLE) {
  26821. const lineWidth = Math.max(ctx.options.lineWidth - ctx.indent.length, ctx.options.minContentWidth);
  26822. const n = Math.ceil(str.length / lineWidth);
  26823. const lines = new Array(n);
  26824. for (let i = 0, o = 0; i < n; ++i, o += lineWidth) {
  26825. lines[i] = str.substr(o, lineWidth);
  26826. }
  26827. str = lines.join(type === Scalar.BLOCK_LITERAL ? '\n' : ' ');
  26828. }
  26829. return stringifyString({ comment, type, value: str }, ctx, onComment, onChompKeep);
  26830. }
  26831. };
  26832. function resolvePairs(seq, onError) {
  26833. if (isSeq(seq)) {
  26834. for (let i = 0; i < seq.items.length; ++i) {
  26835. let item = seq.items[i];
  26836. if (isPair(item))
  26837. continue;
  26838. else if (isMap(item)) {
  26839. if (item.items.length > 1)
  26840. onError('Each pair must have its own sequence indicator');
  26841. const pair = item.items[0] || new Pair(new Scalar(null));
  26842. if (item.commentBefore)
  26843. pair.key.commentBefore = pair.key.commentBefore
  26844. ? `${item.commentBefore}\n${pair.key.commentBefore}`
  26845. : item.commentBefore;
  26846. if (item.comment) {
  26847. const cn = pair.value ?? pair.key;
  26848. cn.comment = cn.comment
  26849. ? `${item.comment}\n${cn.comment}`
  26850. : item.comment;
  26851. }
  26852. item = pair;
  26853. }
  26854. seq.items[i] = isPair(item) ? item : new Pair(item);
  26855. }
  26856. }
  26857. else
  26858. onError('Expected a sequence for this tag');
  26859. return seq;
  26860. }
  26861. function createPairs(schema, iterable, ctx) {
  26862. const { replacer } = ctx;
  26863. const pairs = new YAMLSeq(schema);
  26864. pairs.tag = 'tag:yaml.org,2002:pairs';
  26865. let i = 0;
  26866. if (iterable && Symbol.iterator in Object(iterable))
  26867. for (let it of iterable) {
  26868. if (typeof replacer === 'function')
  26869. it = replacer.call(iterable, String(i++), it);
  26870. let key, value;
  26871. if (Array.isArray(it)) {
  26872. if (it.length === 2) {
  26873. key = it[0];
  26874. value = it[1];
  26875. }
  26876. else
  26877. throw new TypeError(`Expected [key, value] tuple: ${it}`);
  26878. }
  26879. else if (it && it instanceof Object) {
  26880. const keys = Object.keys(it);
  26881. if (keys.length === 1) {
  26882. key = keys[0];
  26883. value = it[key];
  26884. }
  26885. else {
  26886. throw new TypeError(`Expected tuple with one key, not ${keys.length} keys`);
  26887. }
  26888. }
  26889. else {
  26890. key = it;
  26891. }
  26892. pairs.items.push(createPair(key, value, ctx));
  26893. }
  26894. return pairs;
  26895. }
  26896. const pairs = {
  26897. collection: 'seq',
  26898. default: false,
  26899. tag: 'tag:yaml.org,2002:pairs',
  26900. resolve: resolvePairs,
  26901. createNode: createPairs
  26902. };
  26903. class YAMLOMap extends YAMLSeq {
  26904. constructor() {
  26905. super();
  26906. this.add = YAMLMap.prototype.add.bind(this);
  26907. this.delete = YAMLMap.prototype.delete.bind(this);
  26908. this.get = YAMLMap.prototype.get.bind(this);
  26909. this.has = YAMLMap.prototype.has.bind(this);
  26910. this.set = YAMLMap.prototype.set.bind(this);
  26911. this.tag = YAMLOMap.tag;
  26912. }
  26913. /**
  26914. * If `ctx` is given, the return type is actually `Map<unknown, unknown>`,
  26915. * but TypeScript won't allow widening the signature of a child method.
  26916. */
  26917. toJSON(_, ctx) {
  26918. if (!ctx)
  26919. return super.toJSON(_);
  26920. const map = new Map();
  26921. if (ctx?.onCreate)
  26922. ctx.onCreate(map);
  26923. for (const pair of this.items) {
  26924. let key, value;
  26925. if (isPair(pair)) {
  26926. key = toJS(pair.key, '', ctx);
  26927. value = toJS(pair.value, key, ctx);
  26928. }
  26929. else {
  26930. key = toJS(pair, '', ctx);
  26931. }
  26932. if (map.has(key))
  26933. throw new Error('Ordered maps must not include duplicate keys');
  26934. map.set(key, value);
  26935. }
  26936. return map;
  26937. }
  26938. static from(schema, iterable, ctx) {
  26939. const pairs = createPairs(schema, iterable, ctx);
  26940. const omap = new this();
  26941. omap.items = pairs.items;
  26942. return omap;
  26943. }
  26944. }
  26945. YAMLOMap.tag = 'tag:yaml.org,2002:omap';
  26946. const omap = {
  26947. collection: 'seq',
  26948. identify: value => value instanceof Map,
  26949. nodeClass: YAMLOMap,
  26950. default: false,
  26951. tag: 'tag:yaml.org,2002:omap',
  26952. resolve(seq, onError) {
  26953. const pairs = resolvePairs(seq, onError);
  26954. const seenKeys = [];
  26955. for (const { key } of pairs.items) {
  26956. if (isScalar$1(key)) {
  26957. if (seenKeys.includes(key.value)) {
  26958. onError(`Ordered maps must not include duplicate keys: ${key.value}`);
  26959. }
  26960. else {
  26961. seenKeys.push(key.value);
  26962. }
  26963. }
  26964. }
  26965. return Object.assign(new YAMLOMap(), pairs);
  26966. },
  26967. createNode: (schema, iterable, ctx) => YAMLOMap.from(schema, iterable, ctx)
  26968. };
  26969. function boolStringify({ value, source }, ctx) {
  26970. const boolObj = value ? trueTag : falseTag;
  26971. if (source && boolObj.test.test(source))
  26972. return source;
  26973. return value ? ctx.options.trueStr : ctx.options.falseStr;
  26974. }
  26975. const trueTag = {
  26976. identify: value => value === true,
  26977. default: true,
  26978. tag: 'tag:yaml.org,2002:bool',
  26979. test: /^(?:Y|y|[Yy]es|YES|[Tt]rue|TRUE|[Oo]n|ON)$/,
  26980. resolve: () => new Scalar(true),
  26981. stringify: boolStringify
  26982. };
  26983. const falseTag = {
  26984. identify: value => value === false,
  26985. default: true,
  26986. tag: 'tag:yaml.org,2002:bool',
  26987. test: /^(?:N|n|[Nn]o|NO|[Ff]alse|FALSE|[Oo]ff|OFF)$/,
  26988. resolve: () => new Scalar(false),
  26989. stringify: boolStringify
  26990. };
  26991. const floatNaN = {
  26992. identify: value => typeof value === 'number',
  26993. default: true,
  26994. tag: 'tag:yaml.org,2002:float',
  26995. test: /^(?:[-+]?\.(?:inf|Inf|INF)|\.nan|\.NaN|\.NAN)$/,
  26996. resolve: (str) => str.slice(-3).toLowerCase() === 'nan'
  26997. ? NaN
  26998. : str[0] === '-'
  26999. ? Number.NEGATIVE_INFINITY
  27000. : Number.POSITIVE_INFINITY,
  27001. stringify: stringifyNumber
  27002. };
  27003. const floatExp = {
  27004. identify: value => typeof value === 'number',
  27005. default: true,
  27006. tag: 'tag:yaml.org,2002:float',
  27007. format: 'EXP',
  27008. test: /^[-+]?(?:[0-9][0-9_]*)?(?:\.[0-9_]*)?[eE][-+]?[0-9]+$/,
  27009. resolve: (str) => parseFloat(str.replace(/_/g, '')),
  27010. stringify(node) {
  27011. const num = Number(node.value);
  27012. return isFinite(num) ? num.toExponential() : stringifyNumber(node);
  27013. }
  27014. };
  27015. const float = {
  27016. identify: value => typeof value === 'number',
  27017. default: true,
  27018. tag: 'tag:yaml.org,2002:float',
  27019. test: /^[-+]?(?:[0-9][0-9_]*)?\.[0-9_]*$/,
  27020. resolve(str) {
  27021. const node = new Scalar(parseFloat(str.replace(/_/g, '')));
  27022. const dot = str.indexOf('.');
  27023. if (dot !== -1) {
  27024. const f = str.substring(dot + 1).replace(/_/g, '');
  27025. if (f[f.length - 1] === '0')
  27026. node.minFractionDigits = f.length;
  27027. }
  27028. return node;
  27029. },
  27030. stringify: stringifyNumber
  27031. };
  27032. const intIdentify = (value) => typeof value === 'bigint' || Number.isInteger(value);
  27033. function intResolve(str, offset, radix, { intAsBigInt }) {
  27034. const sign = str[0];
  27035. if (sign === '-' || sign === '+')
  27036. offset += 1;
  27037. str = str.substring(offset).replace(/_/g, '');
  27038. if (intAsBigInt) {
  27039. switch (radix) {
  27040. case 2:
  27041. str = `0b${str}`;
  27042. break;
  27043. case 8:
  27044. str = `0o${str}`;
  27045. break;
  27046. case 16:
  27047. str = `0x${str}`;
  27048. break;
  27049. }
  27050. const n = BigInt(str);
  27051. return sign === '-' ? BigInt(-1) * n : n;
  27052. }
  27053. const n = parseInt(str, radix);
  27054. return sign === '-' ? -1 * n : n;
  27055. }
  27056. function intStringify(node, radix, prefix) {
  27057. const { value } = node;
  27058. if (intIdentify(value)) {
  27059. const str = value.toString(radix);
  27060. return value < 0 ? '-' + prefix + str.substr(1) : prefix + str;
  27061. }
  27062. return stringifyNumber(node);
  27063. }
  27064. const intBin = {
  27065. identify: intIdentify,
  27066. default: true,
  27067. tag: 'tag:yaml.org,2002:int',
  27068. format: 'BIN',
  27069. test: /^[-+]?0b[0-1_]+$/,
  27070. resolve: (str, _onError, opt) => intResolve(str, 2, 2, opt),
  27071. stringify: node => intStringify(node, 2, '0b')
  27072. };
  27073. const intOct = {
  27074. identify: intIdentify,
  27075. default: true,
  27076. tag: 'tag:yaml.org,2002:int',
  27077. format: 'OCT',
  27078. test: /^[-+]?0[0-7_]+$/,
  27079. resolve: (str, _onError, opt) => intResolve(str, 1, 8, opt),
  27080. stringify: node => intStringify(node, 8, '0')
  27081. };
  27082. const int = {
  27083. identify: intIdentify,
  27084. default: true,
  27085. tag: 'tag:yaml.org,2002:int',
  27086. test: /^[-+]?[0-9][0-9_]*$/,
  27087. resolve: (str, _onError, opt) => intResolve(str, 0, 10, opt),
  27088. stringify: stringifyNumber
  27089. };
  27090. const intHex = {
  27091. identify: intIdentify,
  27092. default: true,
  27093. tag: 'tag:yaml.org,2002:int',
  27094. format: 'HEX',
  27095. test: /^[-+]?0x[0-9a-fA-F_]+$/,
  27096. resolve: (str, _onError, opt) => intResolve(str, 2, 16, opt),
  27097. stringify: node => intStringify(node, 16, '0x')
  27098. };
  27099. class YAMLSet extends YAMLMap {
  27100. constructor(schema) {
  27101. super(schema);
  27102. this.tag = YAMLSet.tag;
  27103. }
  27104. add(key) {
  27105. let pair;
  27106. if (isPair(key))
  27107. pair = key;
  27108. else if (key &&
  27109. typeof key === 'object' &&
  27110. 'key' in key &&
  27111. 'value' in key &&
  27112. key.value === null)
  27113. pair = new Pair(key.key, null);
  27114. else
  27115. pair = new Pair(key, null);
  27116. const prev = findPair(this.items, pair.key);
  27117. if (!prev)
  27118. this.items.push(pair);
  27119. }
  27120. /**
  27121. * If `keepPair` is `true`, returns the Pair matching `key`.
  27122. * Otherwise, returns the value of that Pair's key.
  27123. */
  27124. get(key, keepPair) {
  27125. const pair = findPair(this.items, key);
  27126. return !keepPair && isPair(pair)
  27127. ? isScalar$1(pair.key)
  27128. ? pair.key.value
  27129. : pair.key
  27130. : pair;
  27131. }
  27132. set(key, value) {
  27133. if (typeof value !== 'boolean')
  27134. throw new Error(`Expected boolean value for set(key, value) in a YAML set, not ${typeof value}`);
  27135. const prev = findPair(this.items, key);
  27136. if (prev && !value) {
  27137. this.items.splice(this.items.indexOf(prev), 1);
  27138. }
  27139. else if (!prev && value) {
  27140. this.items.push(new Pair(key));
  27141. }
  27142. }
  27143. toJSON(_, ctx) {
  27144. return super.toJSON(_, ctx, Set);
  27145. }
  27146. toString(ctx, onComment, onChompKeep) {
  27147. if (!ctx)
  27148. return JSON.stringify(this);
  27149. if (this.hasAllNullValues(true))
  27150. return super.toString(Object.assign({}, ctx, { allNullValues: true }), onComment, onChompKeep);
  27151. else
  27152. throw new Error('Set items must all have null values');
  27153. }
  27154. static from(schema, iterable, ctx) {
  27155. const { replacer } = ctx;
  27156. const set = new this(schema);
  27157. if (iterable && Symbol.iterator in Object(iterable))
  27158. for (let value of iterable) {
  27159. if (typeof replacer === 'function')
  27160. value = replacer.call(iterable, value, value);
  27161. set.items.push(createPair(value, null, ctx));
  27162. }
  27163. return set;
  27164. }
  27165. }
  27166. YAMLSet.tag = 'tag:yaml.org,2002:set';
  27167. const set = {
  27168. collection: 'map',
  27169. identify: value => value instanceof Set,
  27170. nodeClass: YAMLSet,
  27171. default: false,
  27172. tag: 'tag:yaml.org,2002:set',
  27173. createNode: (schema, iterable, ctx) => YAMLSet.from(schema, iterable, ctx),
  27174. resolve(map, onError) {
  27175. if (isMap(map)) {
  27176. if (map.hasAllNullValues(true))
  27177. return Object.assign(new YAMLSet(), map);
  27178. else
  27179. onError('Set items must all have null values');
  27180. }
  27181. else
  27182. onError('Expected a mapping for this tag');
  27183. return map;
  27184. }
  27185. };
  27186. /** Internal types handle bigint as number, because TS can't figure it out. */
  27187. function parseSexagesimal(str, asBigInt) {
  27188. const sign = str[0];
  27189. const parts = sign === '-' || sign === '+' ? str.substring(1) : str;
  27190. const num = (n) => asBigInt ? BigInt(n) : Number(n);
  27191. const res = parts
  27192. .replace(/_/g, '')
  27193. .split(':')
  27194. .reduce((res, p) => res * num(60) + num(p), num(0));
  27195. return (sign === '-' ? num(-1) * res : res);
  27196. }
  27197. /**
  27198. * hhhh:mm:ss.sss
  27199. *
  27200. * Internal types handle bigint as number, because TS can't figure it out.
  27201. */
  27202. function stringifySexagesimal(node) {
  27203. let { value } = node;
  27204. let num = (n) => n;
  27205. if (typeof value === 'bigint')
  27206. num = n => BigInt(n);
  27207. else if (isNaN(value) || !isFinite(value))
  27208. return stringifyNumber(node);
  27209. let sign = '';
  27210. if (value < 0) {
  27211. sign = '-';
  27212. value *= num(-1);
  27213. }
  27214. const _60 = num(60);
  27215. const parts = [value % _60]; // seconds, including ms
  27216. if (value < 60) {
  27217. parts.unshift(0); // at least one : is required
  27218. }
  27219. else {
  27220. value = (value - parts[0]) / _60;
  27221. parts.unshift(value % _60); // minutes
  27222. if (value >= 60) {
  27223. value = (value - parts[0]) / _60;
  27224. parts.unshift(value); // hours
  27225. }
  27226. }
  27227. return (sign +
  27228. parts
  27229. .map(n => String(n).padStart(2, '0'))
  27230. .join(':')
  27231. .replace(/000000\d*$/, '') // % 60 may introduce error
  27232. );
  27233. }
  27234. const intTime = {
  27235. identify: value => typeof value === 'bigint' || Number.isInteger(value),
  27236. default: true,
  27237. tag: 'tag:yaml.org,2002:int',
  27238. format: 'TIME',
  27239. test: /^[-+]?[0-9][0-9_]*(?::[0-5]?[0-9])+$/,
  27240. resolve: (str, _onError, { intAsBigInt }) => parseSexagesimal(str, intAsBigInt),
  27241. stringify: stringifySexagesimal
  27242. };
  27243. const floatTime = {
  27244. identify: value => typeof value === 'number',
  27245. default: true,
  27246. tag: 'tag:yaml.org,2002:float',
  27247. format: 'TIME',
  27248. test: /^[-+]?[0-9][0-9_]*(?::[0-5]?[0-9])+\.[0-9_]*$/,
  27249. resolve: str => parseSexagesimal(str, false),
  27250. stringify: stringifySexagesimal
  27251. };
  27252. const timestamp = {
  27253. identify: value => value instanceof Date,
  27254. default: true,
  27255. tag: 'tag:yaml.org,2002:timestamp',
  27256. // If the time zone is omitted, the timestamp is assumed to be specified in UTC. The time part
  27257. // may be omitted altogether, resulting in a date format. In such a case, the time part is
  27258. // assumed to be 00:00:00Z (start of day, UTC).
  27259. test: RegExp('^([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})' + // YYYY-Mm-Dd
  27260. '(?:' + // time is optional
  27261. '(?:t|T|[ \\t]+)' + // t | T | whitespace
  27262. '([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2}(\\.[0-9]+)?)' + // Hh:Mm:Ss(.ss)?
  27263. '(?:[ \\t]*(Z|[-+][012]?[0-9](?::[0-9]{2})?))?' + // Z | +5 | -03:30
  27264. ')?$'),
  27265. resolve(str) {
  27266. const match = str.match(timestamp.test);
  27267. if (!match)
  27268. throw new Error('!!timestamp expects a date, starting with yyyy-mm-dd');
  27269. const [, year, month, day, hour, minute, second] = match.map(Number);
  27270. const millisec = match[7] ? Number((match[7] + '00').substr(1, 3)) : 0;
  27271. let date = Date.UTC(year, month - 1, day, hour || 0, minute || 0, second || 0, millisec);
  27272. const tz = match[8];
  27273. if (tz && tz !== 'Z') {
  27274. let d = parseSexagesimal(tz, false);
  27275. if (Math.abs(d) < 30)
  27276. d *= 60;
  27277. date -= 60000 * d;
  27278. }
  27279. return new Date(date);
  27280. },
  27281. stringify: ({ value }) => value.toISOString().replace(/((T00:00)?:00)?\.000Z$/, '')
  27282. };
  27283. const schema = [
  27284. map$1,
  27285. seq,
  27286. string,
  27287. nullTag,
  27288. trueTag,
  27289. falseTag,
  27290. intBin,
  27291. intOct,
  27292. int,
  27293. intHex,
  27294. floatNaN,
  27295. floatExp,
  27296. float,
  27297. binary,
  27298. omap,
  27299. pairs,
  27300. set,
  27301. intTime,
  27302. floatTime,
  27303. timestamp
  27304. ];
  27305. const schemas = new Map([
  27306. ['core', schema$2],
  27307. ['failsafe', [map$1, seq, string]],
  27308. ['json', schema$1],
  27309. ['yaml11', schema],
  27310. ['yaml-1.1', schema]
  27311. ]);
  27312. const tagsByName = {
  27313. binary,
  27314. bool: boolTag,
  27315. float: float$1,
  27316. floatExp: floatExp$1,
  27317. floatNaN: floatNaN$1,
  27318. floatTime,
  27319. int: int$1,
  27320. intHex: intHex$1,
  27321. intOct: intOct$1,
  27322. intTime,
  27323. map: map$1,
  27324. null: nullTag,
  27325. omap,
  27326. pairs,
  27327. seq,
  27328. set,
  27329. timestamp
  27330. };
  27331. const coreKnownTags = {
  27332. 'tag:yaml.org,2002:binary': binary,
  27333. 'tag:yaml.org,2002:omap': omap,
  27334. 'tag:yaml.org,2002:pairs': pairs,
  27335. 'tag:yaml.org,2002:set': set,
  27336. 'tag:yaml.org,2002:timestamp': timestamp
  27337. };
  27338. function getTags(customTags, schemaName) {
  27339. let tags = schemas.get(schemaName);
  27340. if (!tags) {
  27341. if (Array.isArray(customTags))
  27342. tags = [];
  27343. else {
  27344. const keys = Array.from(schemas.keys())
  27345. .filter(key => key !== 'yaml11')
  27346. .map(key => JSON.stringify(key))
  27347. .join(', ');
  27348. throw new Error(`Unknown schema "${schemaName}"; use one of ${keys} or define customTags array`);
  27349. }
  27350. }
  27351. if (Array.isArray(customTags)) {
  27352. for (const tag of customTags)
  27353. tags = tags.concat(tag);
  27354. }
  27355. else if (typeof customTags === 'function') {
  27356. tags = customTags(tags.slice());
  27357. }
  27358. return tags.map(tag => {
  27359. if (typeof tag !== 'string')
  27360. return tag;
  27361. const tagObj = tagsByName[tag];
  27362. if (tagObj)
  27363. return tagObj;
  27364. const keys = Object.keys(tagsByName)
  27365. .map(key => JSON.stringify(key))
  27366. .join(', ');
  27367. throw new Error(`Unknown custom tag "${tag}"; use one of ${keys}`);
  27368. });
  27369. }
  27370. const sortMapEntriesByKey = (a, b) => a.key < b.key ? -1 : a.key > b.key ? 1 : 0;
  27371. class Schema {
  27372. constructor({ compat, customTags, merge, resolveKnownTags, schema, sortMapEntries, toStringDefaults }) {
  27373. this.compat = Array.isArray(compat)
  27374. ? getTags(compat, 'compat')
  27375. : compat
  27376. ? getTags(null, compat)
  27377. : null;
  27378. this.merge = !!merge;
  27379. this.name = (typeof schema === 'string' && schema) || 'core';
  27380. this.knownTags = resolveKnownTags ? coreKnownTags : {};
  27381. this.tags = getTags(customTags, this.name);
  27382. this.toStringOptions = toStringDefaults ?? null;
  27383. Object.defineProperty(this, MAP, { value: map$1 });
  27384. Object.defineProperty(this, SCALAR$1, { value: string });
  27385. Object.defineProperty(this, SEQ, { value: seq });
  27386. // Used by createMap()
  27387. this.sortMapEntries =
  27388. typeof sortMapEntries === 'function'
  27389. ? sortMapEntries
  27390. : sortMapEntries === true
  27391. ? sortMapEntriesByKey
  27392. : null;
  27393. }
  27394. clone() {
  27395. const copy = Object.create(Schema.prototype, Object.getOwnPropertyDescriptors(this));
  27396. copy.tags = this.tags.slice();
  27397. return copy;
  27398. }
  27399. }
  27400. function stringifyDocument(doc, options) {
  27401. const lines = [];
  27402. let hasDirectives = options.directives === true;
  27403. if (options.directives !== false && doc.directives) {
  27404. const dir = doc.directives.toString(doc);
  27405. if (dir) {
  27406. lines.push(dir);
  27407. hasDirectives = true;
  27408. }
  27409. else if (doc.directives.docStart)
  27410. hasDirectives = true;
  27411. }
  27412. if (hasDirectives)
  27413. lines.push('---');
  27414. const ctx = createStringifyContext(doc, options);
  27415. const { commentString } = ctx.options;
  27416. if (doc.commentBefore) {
  27417. if (lines.length !== 1)
  27418. lines.unshift('');
  27419. const cs = commentString(doc.commentBefore);
  27420. lines.unshift(indentComment(cs, ''));
  27421. }
  27422. let chompKeep = false;
  27423. let contentComment = null;
  27424. if (doc.contents) {
  27425. if (isNode$1(doc.contents)) {
  27426. if (doc.contents.spaceBefore && hasDirectives)
  27427. lines.push('');
  27428. if (doc.contents.commentBefore) {
  27429. const cs = commentString(doc.contents.commentBefore);
  27430. lines.push(indentComment(cs, ''));
  27431. }
  27432. // top-level block scalars need to be indented if followed by a comment
  27433. ctx.forceBlockIndent = !!doc.comment;
  27434. contentComment = doc.contents.comment;
  27435. }
  27436. const onChompKeep = contentComment ? undefined : () => (chompKeep = true);
  27437. let body = stringify$2(doc.contents, ctx, () => (contentComment = null), onChompKeep);
  27438. if (contentComment)
  27439. body += lineComment(body, '', commentString(contentComment));
  27440. if ((body[0] === '|' || body[0] === '>') &&
  27441. lines[lines.length - 1] === '---') {
  27442. // Top-level block scalars with a preceding doc marker ought to use the
  27443. // same line for their header.
  27444. lines[lines.length - 1] = `--- ${body}`;
  27445. }
  27446. else
  27447. lines.push(body);
  27448. }
  27449. else {
  27450. lines.push(stringify$2(doc.contents, ctx));
  27451. }
  27452. if (doc.directives?.docEnd) {
  27453. if (doc.comment) {
  27454. const cs = commentString(doc.comment);
  27455. if (cs.includes('\n')) {
  27456. lines.push('...');
  27457. lines.push(indentComment(cs, ''));
  27458. }
  27459. else {
  27460. lines.push(`... ${cs}`);
  27461. }
  27462. }
  27463. else {
  27464. lines.push('...');
  27465. }
  27466. }
  27467. else {
  27468. let dc = doc.comment;
  27469. if (dc && chompKeep)
  27470. dc = dc.replace(/^\n+/, '');
  27471. if (dc) {
  27472. if ((!chompKeep || contentComment) && lines[lines.length - 1] !== '')
  27473. lines.push('');
  27474. lines.push(indentComment(commentString(dc), ''));
  27475. }
  27476. }
  27477. return lines.join('\n') + '\n';
  27478. }
  27479. class Document {
  27480. constructor(value, replacer, options) {
  27481. /** A comment before this Document */
  27482. this.commentBefore = null;
  27483. /** A comment immediately after this Document */
  27484. this.comment = null;
  27485. /** Errors encountered during parsing. */
  27486. this.errors = [];
  27487. /** Warnings encountered during parsing. */
  27488. this.warnings = [];
  27489. Object.defineProperty(this, NODE_TYPE, { value: DOC });
  27490. let _replacer = null;
  27491. if (typeof replacer === 'function' || Array.isArray(replacer)) {
  27492. _replacer = replacer;
  27493. }
  27494. else if (options === undefined && replacer) {
  27495. options = replacer;
  27496. replacer = undefined;
  27497. }
  27498. const opt = Object.assign({
  27499. intAsBigInt: false,
  27500. keepSourceTokens: false,
  27501. logLevel: 'warn',
  27502. prettyErrors: true,
  27503. strict: true,
  27504. uniqueKeys: true,
  27505. version: '1.2'
  27506. }, options);
  27507. this.options = opt;
  27508. let { version } = opt;
  27509. if (options?._directives) {
  27510. this.directives = options._directives.atDocument();
  27511. if (this.directives.yaml.explicit)
  27512. version = this.directives.yaml.version;
  27513. }
  27514. else
  27515. this.directives = new Directives({ version });
  27516. this.setSchema(version, options);
  27517. // @ts-expect-error We can't really know that this matches Contents.
  27518. this.contents =
  27519. value === undefined ? null : this.createNode(value, _replacer, options);
  27520. }
  27521. /**
  27522. * Create a deep copy of this Document and its contents.
  27523. *
  27524. * Custom Node values that inherit from `Object` still refer to their original instances.
  27525. */
  27526. clone() {
  27527. const copy = Object.create(Document.prototype, {
  27528. [NODE_TYPE]: { value: DOC }
  27529. });
  27530. copy.commentBefore = this.commentBefore;
  27531. copy.comment = this.comment;
  27532. copy.errors = this.errors.slice();
  27533. copy.warnings = this.warnings.slice();
  27534. copy.options = Object.assign({}, this.options);
  27535. if (this.directives)
  27536. copy.directives = this.directives.clone();
  27537. copy.schema = this.schema.clone();
  27538. // @ts-expect-error We can't really know that this matches Contents.
  27539. copy.contents = isNode$1(this.contents)
  27540. ? this.contents.clone(copy.schema)
  27541. : this.contents;
  27542. if (this.range)
  27543. copy.range = this.range.slice();
  27544. return copy;
  27545. }
  27546. /** Adds a value to the document. */
  27547. add(value) {
  27548. if (assertCollection(this.contents))
  27549. this.contents.add(value);
  27550. }
  27551. /** Adds a value to the document. */
  27552. addIn(path, value) {
  27553. if (assertCollection(this.contents))
  27554. this.contents.addIn(path, value);
  27555. }
  27556. /**
  27557. * Create a new `Alias` node, ensuring that the target `node` has the required anchor.
  27558. *
  27559. * If `node` already has an anchor, `name` is ignored.
  27560. * Otherwise, the `node.anchor` value will be set to `name`,
  27561. * or if an anchor with that name is already present in the document,
  27562. * `name` will be used as a prefix for a new unique anchor.
  27563. * If `name` is undefined, the generated anchor will use 'a' as a prefix.
  27564. */
  27565. createAlias(node, name) {
  27566. if (!node.anchor) {
  27567. const prev = anchorNames(this);
  27568. node.anchor =
  27569. // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
  27570. !name || prev.has(name) ? findNewAnchor(name || 'a', prev) : name;
  27571. }
  27572. return new Alias(node.anchor);
  27573. }
  27574. createNode(value, replacer, options) {
  27575. let _replacer = undefined;
  27576. if (typeof replacer === 'function') {
  27577. value = replacer.call({ '': value }, '', value);
  27578. _replacer = replacer;
  27579. }
  27580. else if (Array.isArray(replacer)) {
  27581. const keyToStr = (v) => typeof v === 'number' || v instanceof String || v instanceof Number;
  27582. const asStr = replacer.filter(keyToStr).map(String);
  27583. if (asStr.length > 0)
  27584. replacer = replacer.concat(asStr);
  27585. _replacer = replacer;
  27586. }
  27587. else if (options === undefined && replacer) {
  27588. options = replacer;
  27589. replacer = undefined;
  27590. }
  27591. const { aliasDuplicateObjects, anchorPrefix, flow, keepUndefined, onTagObj, tag } = options ?? {};
  27592. const { onAnchor, setAnchors, sourceObjects } = createNodeAnchors(this,
  27593. // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
  27594. anchorPrefix || 'a');
  27595. const ctx = {
  27596. aliasDuplicateObjects: aliasDuplicateObjects ?? true,
  27597. keepUndefined: keepUndefined ?? false,
  27598. onAnchor,
  27599. onTagObj,
  27600. replacer: _replacer,
  27601. schema: this.schema,
  27602. sourceObjects
  27603. };
  27604. const node = createNode(value, tag, ctx);
  27605. if (flow && isCollection$1(node))
  27606. node.flow = true;
  27607. setAnchors();
  27608. return node;
  27609. }
  27610. /**
  27611. * Convert a key and a value into a `Pair` using the current schema,
  27612. * recursively wrapping all values as `Scalar` or `Collection` nodes.
  27613. */
  27614. createPair(key, value, options = {}) {
  27615. const k = this.createNode(key, null, options);
  27616. const v = this.createNode(value, null, options);
  27617. return new Pair(k, v);
  27618. }
  27619. /**
  27620. * Removes a value from the document.
  27621. * @returns `true` if the item was found and removed.
  27622. */
  27623. delete(key) {
  27624. return assertCollection(this.contents) ? this.contents.delete(key) : false;
  27625. }
  27626. /**
  27627. * Removes a value from the document.
  27628. * @returns `true` if the item was found and removed.
  27629. */
  27630. deleteIn(path) {
  27631. if (isEmptyPath(path)) {
  27632. if (this.contents == null)
  27633. return false;
  27634. // @ts-expect-error Presumed impossible if Strict extends false
  27635. this.contents = null;
  27636. return true;
  27637. }
  27638. return assertCollection(this.contents)
  27639. ? this.contents.deleteIn(path)
  27640. : false;
  27641. }
  27642. /**
  27643. * Returns item at `key`, or `undefined` if not found. By default unwraps
  27644. * scalar values from their surrounding node; to disable set `keepScalar` to
  27645. * `true` (collections are always returned intact).
  27646. */
  27647. get(key, keepScalar) {
  27648. return isCollection$1(this.contents)
  27649. ? this.contents.get(key, keepScalar)
  27650. : undefined;
  27651. }
  27652. /**
  27653. * Returns item at `path`, or `undefined` if not found. By default unwraps
  27654. * scalar values from their surrounding node; to disable set `keepScalar` to
  27655. * `true` (collections are always returned intact).
  27656. */
  27657. getIn(path, keepScalar) {
  27658. if (isEmptyPath(path))
  27659. return !keepScalar && isScalar$1(this.contents)
  27660. ? this.contents.value
  27661. : this.contents;
  27662. return isCollection$1(this.contents)
  27663. ? this.contents.getIn(path, keepScalar)
  27664. : undefined;
  27665. }
  27666. /**
  27667. * Checks if the document includes a value with the key `key`.
  27668. */
  27669. has(key) {
  27670. return isCollection$1(this.contents) ? this.contents.has(key) : false;
  27671. }
  27672. /**
  27673. * Checks if the document includes a value at `path`.
  27674. */
  27675. hasIn(path) {
  27676. if (isEmptyPath(path))
  27677. return this.contents !== undefined;
  27678. return isCollection$1(this.contents) ? this.contents.hasIn(path) : false;
  27679. }
  27680. /**
  27681. * Sets a value in this document. For `!!set`, `value` needs to be a
  27682. * boolean to add/remove the item from the set.
  27683. */
  27684. set(key, value) {
  27685. if (this.contents == null) {
  27686. // @ts-expect-error We can't really know that this matches Contents.
  27687. this.contents = collectionFromPath(this.schema, [key], value);
  27688. }
  27689. else if (assertCollection(this.contents)) {
  27690. this.contents.set(key, value);
  27691. }
  27692. }
  27693. /**
  27694. * Sets a value in this document. For `!!set`, `value` needs to be a
  27695. * boolean to add/remove the item from the set.
  27696. */
  27697. setIn(path, value) {
  27698. if (isEmptyPath(path)) {
  27699. // @ts-expect-error We can't really know that this matches Contents.
  27700. this.contents = value;
  27701. }
  27702. else if (this.contents == null) {
  27703. // @ts-expect-error We can't really know that this matches Contents.
  27704. this.contents = collectionFromPath(this.schema, Array.from(path), value);
  27705. }
  27706. else if (assertCollection(this.contents)) {
  27707. this.contents.setIn(path, value);
  27708. }
  27709. }
  27710. /**
  27711. * Change the YAML version and schema used by the document.
  27712. * A `null` version disables support for directives, explicit tags, anchors, and aliases.
  27713. * It also requires the `schema` option to be given as a `Schema` instance value.
  27714. *
  27715. * Overrides all previously set schema options.
  27716. */
  27717. setSchema(version, options = {}) {
  27718. if (typeof version === 'number')
  27719. version = String(version);
  27720. let opt;
  27721. switch (version) {
  27722. case '1.1':
  27723. if (this.directives)
  27724. this.directives.yaml.version = '1.1';
  27725. else
  27726. this.directives = new Directives({ version: '1.1' });
  27727. opt = { merge: true, resolveKnownTags: false, schema: 'yaml-1.1' };
  27728. break;
  27729. case '1.2':
  27730. case 'next':
  27731. if (this.directives)
  27732. this.directives.yaml.version = version;
  27733. else
  27734. this.directives = new Directives({ version });
  27735. opt = { merge: false, resolveKnownTags: true, schema: 'core' };
  27736. break;
  27737. case null:
  27738. if (this.directives)
  27739. delete this.directives;
  27740. opt = null;
  27741. break;
  27742. default: {
  27743. const sv = JSON.stringify(version);
  27744. throw new Error(`Expected '1.1', '1.2' or null as first argument, but found: ${sv}`);
  27745. }
  27746. }
  27747. // Not using `instanceof Schema` to allow for duck typing
  27748. if (options.schema instanceof Object)
  27749. this.schema = options.schema;
  27750. else if (opt)
  27751. this.schema = new Schema(Object.assign(opt, options));
  27752. else
  27753. throw new Error(`With a null YAML version, the { schema: Schema } option is required`);
  27754. }
  27755. // json & jsonArg are only used from toJSON()
  27756. toJS({ json, jsonArg, mapAsMap, maxAliasCount, onAnchor, reviver } = {}) {
  27757. const ctx = {
  27758. anchors: new Map(),
  27759. doc: this,
  27760. keep: !json,
  27761. mapAsMap: mapAsMap === true,
  27762. mapKeyWarned: false,
  27763. maxAliasCount: typeof maxAliasCount === 'number' ? maxAliasCount : 100
  27764. };
  27765. const res = toJS(this.contents, jsonArg ?? '', ctx);
  27766. if (typeof onAnchor === 'function')
  27767. for (const { count, res } of ctx.anchors.values())
  27768. onAnchor(res, count);
  27769. return typeof reviver === 'function'
  27770. ? applyReviver(reviver, { '': res }, '', res)
  27771. : res;
  27772. }
  27773. /**
  27774. * A JSON representation of the document `contents`.
  27775. *
  27776. * @param jsonArg Used by `JSON.stringify` to indicate the array index or
  27777. * property name.
  27778. */
  27779. toJSON(jsonArg, onAnchor) {
  27780. return this.toJS({ json: true, jsonArg, mapAsMap: false, onAnchor });
  27781. }
  27782. /** A YAML representation of the document. */
  27783. toString(options = {}) {
  27784. if (this.errors.length > 0)
  27785. throw new Error('Document with errors cannot be stringified');
  27786. if ('indent' in options &&
  27787. (!Number.isInteger(options.indent) || Number(options.indent) <= 0)) {
  27788. const s = JSON.stringify(options.indent);
  27789. throw new Error(`"indent" option must be a positive integer, not ${s}`);
  27790. }
  27791. return stringifyDocument(this, options);
  27792. }
  27793. }
  27794. function assertCollection(contents) {
  27795. if (isCollection$1(contents))
  27796. return true;
  27797. throw new Error('Expected a YAML collection as document contents');
  27798. }
  27799. class YAMLError extends Error {
  27800. constructor(name, pos, code, message) {
  27801. super();
  27802. this.name = name;
  27803. this.code = code;
  27804. this.message = message;
  27805. this.pos = pos;
  27806. }
  27807. }
  27808. class YAMLParseError extends YAMLError {
  27809. constructor(pos, code, message) {
  27810. super('YAMLParseError', pos, code, message);
  27811. }
  27812. }
  27813. class YAMLWarning extends YAMLError {
  27814. constructor(pos, code, message) {
  27815. super('YAMLWarning', pos, code, message);
  27816. }
  27817. }
  27818. const prettifyError = (src, lc) => (error) => {
  27819. if (error.pos[0] === -1)
  27820. return;
  27821. error.linePos = error.pos.map(pos => lc.linePos(pos));
  27822. const { line, col } = error.linePos[0];
  27823. error.message += ` at line ${line}, column ${col}`;
  27824. let ci = col - 1;
  27825. let lineStr = src
  27826. .substring(lc.lineStarts[line - 1], lc.lineStarts[line])
  27827. .replace(/[\n\r]+$/, '');
  27828. // Trim to max 80 chars, keeping col position near the middle
  27829. if (ci >= 60 && lineStr.length > 80) {
  27830. const trimStart = Math.min(ci - 39, lineStr.length - 79);
  27831. lineStr = '…' + lineStr.substring(trimStart);
  27832. ci -= trimStart - 1;
  27833. }
  27834. if (lineStr.length > 80)
  27835. lineStr = lineStr.substring(0, 79) + '…';
  27836. // Include previous line in context if pointing at line start
  27837. if (line > 1 && /^ *$/.test(lineStr.substring(0, ci))) {
  27838. // Regexp won't match if start is trimmed
  27839. let prev = src.substring(lc.lineStarts[line - 2], lc.lineStarts[line - 1]);
  27840. if (prev.length > 80)
  27841. prev = prev.substring(0, 79) + '…\n';
  27842. lineStr = prev + lineStr;
  27843. }
  27844. if (/[^ ]/.test(lineStr)) {
  27845. let count = 1;
  27846. const end = error.linePos[1];
  27847. if (end && end.line === line && end.col > col) {
  27848. count = Math.max(1, Math.min(end.col - col, 80 - ci));
  27849. }
  27850. const pointer = ' '.repeat(ci) + '^'.repeat(count);
  27851. error.message += `:\n\n${lineStr}\n${pointer}\n`;
  27852. }
  27853. };
  27854. function resolveProps(tokens, { flow, indicator, next, offset, onError, parentIndent, startOnNewline }) {
  27855. let spaceBefore = false;
  27856. let atNewline = startOnNewline;
  27857. let hasSpace = startOnNewline;
  27858. let comment = '';
  27859. let commentSep = '';
  27860. let hasNewline = false;
  27861. let reqSpace = false;
  27862. let tab = null;
  27863. let anchor = null;
  27864. let tag = null;
  27865. let newlineAfterProp = null;
  27866. let comma = null;
  27867. let found = null;
  27868. let start = null;
  27869. for (const token of tokens) {
  27870. if (reqSpace) {
  27871. if (token.type !== 'space' &&
  27872. token.type !== 'newline' &&
  27873. token.type !== 'comma')
  27874. onError(token.offset, 'MISSING_CHAR', 'Tags and anchors must be separated from the next token by white space');
  27875. reqSpace = false;
  27876. }
  27877. if (tab) {
  27878. if (atNewline && token.type !== 'comment' && token.type !== 'newline') {
  27879. onError(tab, 'TAB_AS_INDENT', 'Tabs are not allowed as indentation');
  27880. }
  27881. tab = null;
  27882. }
  27883. switch (token.type) {
  27884. case 'space':
  27885. // At the doc level, tabs at line start may be parsed
  27886. // as leading white space rather than indentation.
  27887. // In a flow collection, only the parser handles indent.
  27888. if (!flow &&
  27889. (indicator !== 'doc-start' || next?.type !== 'flow-collection') &&
  27890. token.source.includes('\t')) {
  27891. tab = token;
  27892. }
  27893. hasSpace = true;
  27894. break;
  27895. case 'comment': {
  27896. if (!hasSpace)
  27897. onError(token, 'MISSING_CHAR', 'Comments must be separated from other tokens by white space characters');
  27898. const cb = token.source.substring(1) || ' ';
  27899. if (!comment)
  27900. comment = cb;
  27901. else
  27902. comment += commentSep + cb;
  27903. commentSep = '';
  27904. atNewline = false;
  27905. break;
  27906. }
  27907. case 'newline':
  27908. if (atNewline) {
  27909. if (comment)
  27910. comment += token.source;
  27911. else
  27912. spaceBefore = true;
  27913. }
  27914. else
  27915. commentSep += token.source;
  27916. atNewline = true;
  27917. hasNewline = true;
  27918. if (anchor || tag)
  27919. newlineAfterProp = token;
  27920. hasSpace = true;
  27921. break;
  27922. case 'anchor':
  27923. if (anchor)
  27924. onError(token, 'MULTIPLE_ANCHORS', 'A node can have at most one anchor');
  27925. if (token.source.endsWith(':'))
  27926. onError(token.offset + token.source.length - 1, 'BAD_ALIAS', 'Anchor ending in : is ambiguous', true);
  27927. anchor = token;
  27928. if (start === null)
  27929. start = token.offset;
  27930. atNewline = false;
  27931. hasSpace = false;
  27932. reqSpace = true;
  27933. break;
  27934. case 'tag': {
  27935. if (tag)
  27936. onError(token, 'MULTIPLE_TAGS', 'A node can have at most one tag');
  27937. tag = token;
  27938. if (start === null)
  27939. start = token.offset;
  27940. atNewline = false;
  27941. hasSpace = false;
  27942. reqSpace = true;
  27943. break;
  27944. }
  27945. case indicator:
  27946. // Could here handle preceding comments differently
  27947. if (anchor || tag)
  27948. onError(token, 'BAD_PROP_ORDER', `Anchors and tags must be after the ${token.source} indicator`);
  27949. if (found)
  27950. onError(token, 'UNEXPECTED_TOKEN', `Unexpected ${token.source} in ${flow ?? 'collection'}`);
  27951. found = token;
  27952. atNewline =
  27953. indicator === 'seq-item-ind' || indicator === 'explicit-key-ind';
  27954. hasSpace = false;
  27955. break;
  27956. case 'comma':
  27957. if (flow) {
  27958. if (comma)
  27959. onError(token, 'UNEXPECTED_TOKEN', `Unexpected , in ${flow}`);
  27960. comma = token;
  27961. atNewline = false;
  27962. hasSpace = false;
  27963. break;
  27964. }
  27965. // else fallthrough
  27966. default:
  27967. onError(token, 'UNEXPECTED_TOKEN', `Unexpected ${token.type} token`);
  27968. atNewline = false;
  27969. hasSpace = false;
  27970. }
  27971. }
  27972. const last = tokens[tokens.length - 1];
  27973. const end = last ? last.offset + last.source.length : offset;
  27974. if (reqSpace &&
  27975. next &&
  27976. next.type !== 'space' &&
  27977. next.type !== 'newline' &&
  27978. next.type !== 'comma' &&
  27979. (next.type !== 'scalar' || next.source !== '')) {
  27980. onError(next.offset, 'MISSING_CHAR', 'Tags and anchors must be separated from the next token by white space');
  27981. }
  27982. if (tab &&
  27983. ((atNewline && tab.indent <= parentIndent) ||
  27984. next?.type === 'block-map' ||
  27985. next?.type === 'block-seq'))
  27986. onError(tab, 'TAB_AS_INDENT', 'Tabs are not allowed as indentation');
  27987. return {
  27988. comma,
  27989. found,
  27990. spaceBefore,
  27991. comment,
  27992. hasNewline,
  27993. anchor,
  27994. tag,
  27995. newlineAfterProp,
  27996. end,
  27997. start: start ?? end
  27998. };
  27999. }
  28000. function containsNewline(key) {
  28001. if (!key)
  28002. return null;
  28003. switch (key.type) {
  28004. case 'alias':
  28005. case 'scalar':
  28006. case 'double-quoted-scalar':
  28007. case 'single-quoted-scalar':
  28008. if (key.source.includes('\n'))
  28009. return true;
  28010. if (key.end)
  28011. for (const st of key.end)
  28012. if (st.type === 'newline')
  28013. return true;
  28014. return false;
  28015. case 'flow-collection':
  28016. for (const it of key.items) {
  28017. for (const st of it.start)
  28018. if (st.type === 'newline')
  28019. return true;
  28020. if (it.sep)
  28021. for (const st of it.sep)
  28022. if (st.type === 'newline')
  28023. return true;
  28024. if (containsNewline(it.key) || containsNewline(it.value))
  28025. return true;
  28026. }
  28027. return false;
  28028. default:
  28029. return true;
  28030. }
  28031. }
  28032. function flowIndentCheck(indent, fc, onError) {
  28033. if (fc?.type === 'flow-collection') {
  28034. const end = fc.end[0];
  28035. if (end.indent === indent &&
  28036. (end.source === ']' || end.source === '}') &&
  28037. containsNewline(fc)) {
  28038. const msg = 'Flow end indicator should be more indented than parent';
  28039. onError(end, 'BAD_INDENT', msg, true);
  28040. }
  28041. }
  28042. }
  28043. function mapIncludes(ctx, items, search) {
  28044. const { uniqueKeys } = ctx.options;
  28045. if (uniqueKeys === false)
  28046. return false;
  28047. const isEqual = typeof uniqueKeys === 'function'
  28048. ? uniqueKeys
  28049. : (a, b) => a === b ||
  28050. (isScalar$1(a) &&
  28051. isScalar$1(b) &&
  28052. a.value === b.value &&
  28053. !(a.value === '<<' && ctx.schema.merge));
  28054. return items.some(pair => isEqual(pair.key, search));
  28055. }
  28056. const startColMsg = 'All mapping items must start at the same column';
  28057. function resolveBlockMap({ composeNode, composeEmptyNode }, ctx, bm, onError, tag) {
  28058. const NodeClass = tag?.nodeClass ?? YAMLMap;
  28059. const map = new NodeClass(ctx.schema);
  28060. if (ctx.atRoot)
  28061. ctx.atRoot = false;
  28062. let offset = bm.offset;
  28063. let commentEnd = null;
  28064. for (const collItem of bm.items) {
  28065. const { start, key, sep, value } = collItem;
  28066. // key properties
  28067. const keyProps = resolveProps(start, {
  28068. indicator: 'explicit-key-ind',
  28069. next: key ?? sep?.[0],
  28070. offset,
  28071. onError,
  28072. parentIndent: bm.indent,
  28073. startOnNewline: true
  28074. });
  28075. const implicitKey = !keyProps.found;
  28076. if (implicitKey) {
  28077. if (key) {
  28078. if (key.type === 'block-seq')
  28079. onError(offset, 'BLOCK_AS_IMPLICIT_KEY', 'A block sequence may not be used as an implicit map key');
  28080. else if ('indent' in key && key.indent !== bm.indent)
  28081. onError(offset, 'BAD_INDENT', startColMsg);
  28082. }
  28083. if (!keyProps.anchor && !keyProps.tag && !sep) {
  28084. commentEnd = keyProps.end;
  28085. if (keyProps.comment) {
  28086. if (map.comment)
  28087. map.comment += '\n' + keyProps.comment;
  28088. else
  28089. map.comment = keyProps.comment;
  28090. }
  28091. continue;
  28092. }
  28093. if (keyProps.newlineAfterProp || containsNewline(key)) {
  28094. onError(key ?? start[start.length - 1], 'MULTILINE_IMPLICIT_KEY', 'Implicit keys need to be on a single line');
  28095. }
  28096. }
  28097. else if (keyProps.found?.indent !== bm.indent) {
  28098. onError(offset, 'BAD_INDENT', startColMsg);
  28099. }
  28100. // key value
  28101. const keyStart = keyProps.end;
  28102. const keyNode = key
  28103. ? composeNode(ctx, key, keyProps, onError)
  28104. : composeEmptyNode(ctx, keyStart, start, null, keyProps, onError);
  28105. if (ctx.schema.compat)
  28106. flowIndentCheck(bm.indent, key, onError);
  28107. if (mapIncludes(ctx, map.items, keyNode))
  28108. onError(keyStart, 'DUPLICATE_KEY', 'Map keys must be unique');
  28109. // value properties
  28110. const valueProps = resolveProps(sep ?? [], {
  28111. indicator: 'map-value-ind',
  28112. next: value,
  28113. offset: keyNode.range[2],
  28114. onError,
  28115. parentIndent: bm.indent,
  28116. startOnNewline: !key || key.type === 'block-scalar'
  28117. });
  28118. offset = valueProps.end;
  28119. if (valueProps.found) {
  28120. if (implicitKey) {
  28121. if (value?.type === 'block-map' && !valueProps.hasNewline)
  28122. onError(offset, 'BLOCK_AS_IMPLICIT_KEY', 'Nested mappings are not allowed in compact mappings');
  28123. if (ctx.options.strict &&
  28124. keyProps.start < valueProps.found.offset - 1024)
  28125. onError(keyNode.range, 'KEY_OVER_1024_CHARS', 'The : indicator must be at most 1024 chars after the start of an implicit block mapping key');
  28126. }
  28127. // value value
  28128. const valueNode = value
  28129. ? composeNode(ctx, value, valueProps, onError)
  28130. : composeEmptyNode(ctx, offset, sep, null, valueProps, onError);
  28131. if (ctx.schema.compat)
  28132. flowIndentCheck(bm.indent, value, onError);
  28133. offset = valueNode.range[2];
  28134. const pair = new Pair(keyNode, valueNode);
  28135. if (ctx.options.keepSourceTokens)
  28136. pair.srcToken = collItem;
  28137. map.items.push(pair);
  28138. }
  28139. else {
  28140. // key with no value
  28141. if (implicitKey)
  28142. onError(keyNode.range, 'MISSING_CHAR', 'Implicit map keys need to be followed by map values');
  28143. if (valueProps.comment) {
  28144. if (keyNode.comment)
  28145. keyNode.comment += '\n' + valueProps.comment;
  28146. else
  28147. keyNode.comment = valueProps.comment;
  28148. }
  28149. const pair = new Pair(keyNode);
  28150. if (ctx.options.keepSourceTokens)
  28151. pair.srcToken = collItem;
  28152. map.items.push(pair);
  28153. }
  28154. }
  28155. if (commentEnd && commentEnd < offset)
  28156. onError(commentEnd, 'IMPOSSIBLE', 'Map comment with trailing content');
  28157. map.range = [bm.offset, offset, commentEnd ?? offset];
  28158. return map;
  28159. }
  28160. function resolveBlockSeq({ composeNode, composeEmptyNode }, ctx, bs, onError, tag) {
  28161. const NodeClass = tag?.nodeClass ?? YAMLSeq;
  28162. const seq = new NodeClass(ctx.schema);
  28163. if (ctx.atRoot)
  28164. ctx.atRoot = false;
  28165. let offset = bs.offset;
  28166. let commentEnd = null;
  28167. for (const { start, value } of bs.items) {
  28168. const props = resolveProps(start, {
  28169. indicator: 'seq-item-ind',
  28170. next: value,
  28171. offset,
  28172. onError,
  28173. parentIndent: bs.indent,
  28174. startOnNewline: true
  28175. });
  28176. if (!props.found) {
  28177. if (props.anchor || props.tag || value) {
  28178. if (value && value.type === 'block-seq')
  28179. onError(props.end, 'BAD_INDENT', 'All sequence items must start at the same column');
  28180. else
  28181. onError(offset, 'MISSING_CHAR', 'Sequence item without - indicator');
  28182. }
  28183. else {
  28184. commentEnd = props.end;
  28185. if (props.comment)
  28186. seq.comment = props.comment;
  28187. continue;
  28188. }
  28189. }
  28190. const node = value
  28191. ? composeNode(ctx, value, props, onError)
  28192. : composeEmptyNode(ctx, props.end, start, null, props, onError);
  28193. if (ctx.schema.compat)
  28194. flowIndentCheck(bs.indent, value, onError);
  28195. offset = node.range[2];
  28196. seq.items.push(node);
  28197. }
  28198. seq.range = [bs.offset, offset, commentEnd ?? offset];
  28199. return seq;
  28200. }
  28201. function resolveEnd(end, offset, reqSpace, onError) {
  28202. let comment = '';
  28203. if (end) {
  28204. let hasSpace = false;
  28205. let sep = '';
  28206. for (const token of end) {
  28207. const { source, type } = token;
  28208. switch (type) {
  28209. case 'space':
  28210. hasSpace = true;
  28211. break;
  28212. case 'comment': {
  28213. if (reqSpace && !hasSpace)
  28214. onError(token, 'MISSING_CHAR', 'Comments must be separated from other tokens by white space characters');
  28215. const cb = source.substring(1) || ' ';
  28216. if (!comment)
  28217. comment = cb;
  28218. else
  28219. comment += sep + cb;
  28220. sep = '';
  28221. break;
  28222. }
  28223. case 'newline':
  28224. if (comment)
  28225. sep += source;
  28226. hasSpace = true;
  28227. break;
  28228. default:
  28229. onError(token, 'UNEXPECTED_TOKEN', `Unexpected ${type} at node end`);
  28230. }
  28231. offset += source.length;
  28232. }
  28233. }
  28234. return { comment, offset };
  28235. }
  28236. const blockMsg = 'Block collections are not allowed within flow collections';
  28237. const isBlock$1 = (token) => token && (token.type === 'block-map' || token.type === 'block-seq');
  28238. function resolveFlowCollection({ composeNode, composeEmptyNode }, ctx, fc, onError, tag) {
  28239. const isMap = fc.start.source === '{';
  28240. const fcName = isMap ? 'flow map' : 'flow sequence';
  28241. const NodeClass = (tag?.nodeClass ?? (isMap ? YAMLMap : YAMLSeq));
  28242. const coll = new NodeClass(ctx.schema);
  28243. coll.flow = true;
  28244. const atRoot = ctx.atRoot;
  28245. if (atRoot)
  28246. ctx.atRoot = false;
  28247. let offset = fc.offset + fc.start.source.length;
  28248. for (let i = 0; i < fc.items.length; ++i) {
  28249. const collItem = fc.items[i];
  28250. const { start, key, sep, value } = collItem;
  28251. const props = resolveProps(start, {
  28252. flow: fcName,
  28253. indicator: 'explicit-key-ind',
  28254. next: key ?? sep?.[0],
  28255. offset,
  28256. onError,
  28257. parentIndent: fc.indent,
  28258. startOnNewline: false
  28259. });
  28260. if (!props.found) {
  28261. if (!props.anchor && !props.tag && !sep && !value) {
  28262. if (i === 0 && props.comma)
  28263. onError(props.comma, 'UNEXPECTED_TOKEN', `Unexpected , in ${fcName}`);
  28264. else if (i < fc.items.length - 1)
  28265. onError(props.start, 'UNEXPECTED_TOKEN', `Unexpected empty item in ${fcName}`);
  28266. if (props.comment) {
  28267. if (coll.comment)
  28268. coll.comment += '\n' + props.comment;
  28269. else
  28270. coll.comment = props.comment;
  28271. }
  28272. offset = props.end;
  28273. continue;
  28274. }
  28275. if (!isMap && ctx.options.strict && containsNewline(key))
  28276. onError(key, // checked by containsNewline()
  28277. 'MULTILINE_IMPLICIT_KEY', 'Implicit keys of flow sequence pairs need to be on a single line');
  28278. }
  28279. if (i === 0) {
  28280. if (props.comma)
  28281. onError(props.comma, 'UNEXPECTED_TOKEN', `Unexpected , in ${fcName}`);
  28282. }
  28283. else {
  28284. if (!props.comma)
  28285. onError(props.start, 'MISSING_CHAR', `Missing , between ${fcName} items`);
  28286. if (props.comment) {
  28287. let prevItemComment = '';
  28288. loop: for (const st of start) {
  28289. switch (st.type) {
  28290. case 'comma':
  28291. case 'space':
  28292. break;
  28293. case 'comment':
  28294. prevItemComment = st.source.substring(1);
  28295. break loop;
  28296. default:
  28297. break loop;
  28298. }
  28299. }
  28300. if (prevItemComment) {
  28301. let prev = coll.items[coll.items.length - 1];
  28302. if (isPair(prev))
  28303. prev = prev.value ?? prev.key;
  28304. if (prev.comment)
  28305. prev.comment += '\n' + prevItemComment;
  28306. else
  28307. prev.comment = prevItemComment;
  28308. props.comment = props.comment.substring(prevItemComment.length + 1);
  28309. }
  28310. }
  28311. }
  28312. if (!isMap && !sep && !props.found) {
  28313. // item is a value in a seq
  28314. // → key & sep are empty, start does not include ? or :
  28315. const valueNode = value
  28316. ? composeNode(ctx, value, props, onError)
  28317. : composeEmptyNode(ctx, props.end, sep, null, props, onError);
  28318. coll.items.push(valueNode);
  28319. offset = valueNode.range[2];
  28320. if (isBlock$1(value))
  28321. onError(valueNode.range, 'BLOCK_IN_FLOW', blockMsg);
  28322. }
  28323. else {
  28324. // item is a key+value pair
  28325. // key value
  28326. const keyStart = props.end;
  28327. const keyNode = key
  28328. ? composeNode(ctx, key, props, onError)
  28329. : composeEmptyNode(ctx, keyStart, start, null, props, onError);
  28330. if (isBlock$1(key))
  28331. onError(keyNode.range, 'BLOCK_IN_FLOW', blockMsg);
  28332. // value properties
  28333. const valueProps = resolveProps(sep ?? [], {
  28334. flow: fcName,
  28335. indicator: 'map-value-ind',
  28336. next: value,
  28337. offset: keyNode.range[2],
  28338. onError,
  28339. parentIndent: fc.indent,
  28340. startOnNewline: false
  28341. });
  28342. if (valueProps.found) {
  28343. if (!isMap && !props.found && ctx.options.strict) {
  28344. if (sep)
  28345. for (const st of sep) {
  28346. if (st === valueProps.found)
  28347. break;
  28348. if (st.type === 'newline') {
  28349. onError(st, 'MULTILINE_IMPLICIT_KEY', 'Implicit keys of flow sequence pairs need to be on a single line');
  28350. break;
  28351. }
  28352. }
  28353. if (props.start < valueProps.found.offset - 1024)
  28354. onError(valueProps.found, 'KEY_OVER_1024_CHARS', 'The : indicator must be at most 1024 chars after the start of an implicit flow sequence key');
  28355. }
  28356. }
  28357. else if (value) {
  28358. if ('source' in value && value.source && value.source[0] === ':')
  28359. onError(value, 'MISSING_CHAR', `Missing space after : in ${fcName}`);
  28360. else
  28361. onError(valueProps.start, 'MISSING_CHAR', `Missing , or : between ${fcName} items`);
  28362. }
  28363. // value value
  28364. const valueNode = value
  28365. ? composeNode(ctx, value, valueProps, onError)
  28366. : valueProps.found
  28367. ? composeEmptyNode(ctx, valueProps.end, sep, null, valueProps, onError)
  28368. : null;
  28369. if (valueNode) {
  28370. if (isBlock$1(value))
  28371. onError(valueNode.range, 'BLOCK_IN_FLOW', blockMsg);
  28372. }
  28373. else if (valueProps.comment) {
  28374. if (keyNode.comment)
  28375. keyNode.comment += '\n' + valueProps.comment;
  28376. else
  28377. keyNode.comment = valueProps.comment;
  28378. }
  28379. const pair = new Pair(keyNode, valueNode);
  28380. if (ctx.options.keepSourceTokens)
  28381. pair.srcToken = collItem;
  28382. if (isMap) {
  28383. const map = coll;
  28384. if (mapIncludes(ctx, map.items, keyNode))
  28385. onError(keyStart, 'DUPLICATE_KEY', 'Map keys must be unique');
  28386. map.items.push(pair);
  28387. }
  28388. else {
  28389. const map = new YAMLMap(ctx.schema);
  28390. map.flow = true;
  28391. map.items.push(pair);
  28392. coll.items.push(map);
  28393. }
  28394. offset = valueNode ? valueNode.range[2] : valueProps.end;
  28395. }
  28396. }
  28397. const expectedEnd = isMap ? '}' : ']';
  28398. const [ce, ...ee] = fc.end;
  28399. let cePos = offset;
  28400. if (ce && ce.source === expectedEnd)
  28401. cePos = ce.offset + ce.source.length;
  28402. else {
  28403. const name = fcName[0].toUpperCase() + fcName.substring(1);
  28404. const msg = atRoot
  28405. ? `${name} must end with a ${expectedEnd}`
  28406. : `${name} in block collection must be sufficiently indented and end with a ${expectedEnd}`;
  28407. onError(offset, atRoot ? 'MISSING_CHAR' : 'BAD_INDENT', msg);
  28408. if (ce && ce.source.length !== 1)
  28409. ee.unshift(ce);
  28410. }
  28411. if (ee.length > 0) {
  28412. const end = resolveEnd(ee, cePos, ctx.options.strict, onError);
  28413. if (end.comment) {
  28414. if (coll.comment)
  28415. coll.comment += '\n' + end.comment;
  28416. else
  28417. coll.comment = end.comment;
  28418. }
  28419. coll.range = [fc.offset, cePos, end.offset];
  28420. }
  28421. else {
  28422. coll.range = [fc.offset, cePos, cePos];
  28423. }
  28424. return coll;
  28425. }
  28426. function resolveCollection(CN, ctx, token, onError, tagName, tag) {
  28427. const coll = token.type === 'block-map'
  28428. ? resolveBlockMap(CN, ctx, token, onError, tag)
  28429. : token.type === 'block-seq'
  28430. ? resolveBlockSeq(CN, ctx, token, onError, tag)
  28431. : resolveFlowCollection(CN, ctx, token, onError, tag);
  28432. const Coll = coll.constructor;
  28433. // If we got a tagName matching the class, or the tag name is '!',
  28434. // then use the tagName from the node class used to create it.
  28435. if (tagName === '!' || tagName === Coll.tagName) {
  28436. coll.tag = Coll.tagName;
  28437. return coll;
  28438. }
  28439. if (tagName)
  28440. coll.tag = tagName;
  28441. return coll;
  28442. }
  28443. function composeCollection(CN, ctx, token, props, onError) {
  28444. const tagToken = props.tag;
  28445. const tagName = !tagToken
  28446. ? null
  28447. : ctx.directives.tagName(tagToken.source, msg => onError(tagToken, 'TAG_RESOLVE_FAILED', msg));
  28448. if (token.type === 'block-seq') {
  28449. const { anchor, newlineAfterProp: nl } = props;
  28450. const lastProp = anchor && tagToken
  28451. ? anchor.offset > tagToken.offset
  28452. ? anchor
  28453. : tagToken
  28454. : (anchor ?? tagToken);
  28455. if (lastProp && (!nl || nl.offset < lastProp.offset)) {
  28456. const message = 'Missing newline after block sequence props';
  28457. onError(lastProp, 'MISSING_CHAR', message);
  28458. }
  28459. }
  28460. const expType = token.type === 'block-map'
  28461. ? 'map'
  28462. : token.type === 'block-seq'
  28463. ? 'seq'
  28464. : token.start.source === '{'
  28465. ? 'map'
  28466. : 'seq';
  28467. // shortcut: check if it's a generic YAMLMap or YAMLSeq
  28468. // before jumping into the custom tag logic.
  28469. if (!tagToken ||
  28470. !tagName ||
  28471. tagName === '!' ||
  28472. (tagName === YAMLMap.tagName && expType === 'map') ||
  28473. (tagName === YAMLSeq.tagName && expType === 'seq')) {
  28474. return resolveCollection(CN, ctx, token, onError, tagName);
  28475. }
  28476. let tag = ctx.schema.tags.find(t => t.tag === tagName && t.collection === expType);
  28477. if (!tag) {
  28478. const kt = ctx.schema.knownTags[tagName];
  28479. if (kt && kt.collection === expType) {
  28480. ctx.schema.tags.push(Object.assign({}, kt, { default: false }));
  28481. tag = kt;
  28482. }
  28483. else {
  28484. if (kt?.collection) {
  28485. onError(tagToken, 'BAD_COLLECTION_TYPE', `${kt.tag} used for ${expType} collection, but expects ${kt.collection}`, true);
  28486. }
  28487. else {
  28488. onError(tagToken, 'TAG_RESOLVE_FAILED', `Unresolved tag: ${tagName}`, true);
  28489. }
  28490. return resolveCollection(CN, ctx, token, onError, tagName);
  28491. }
  28492. }
  28493. const coll = resolveCollection(CN, ctx, token, onError, tagName, tag);
  28494. const res = tag.resolve?.(coll, msg => onError(tagToken, 'TAG_RESOLVE_FAILED', msg), ctx.options) ?? coll;
  28495. const node = isNode$1(res)
  28496. ? res
  28497. : new Scalar(res);
  28498. node.range = coll.range;
  28499. node.tag = tagName;
  28500. if (tag?.format)
  28501. node.format = tag.format;
  28502. return node;
  28503. }
  28504. function resolveBlockScalar(ctx, scalar, onError) {
  28505. const start = scalar.offset;
  28506. const header = parseBlockScalarHeader(scalar, ctx.options.strict, onError);
  28507. if (!header)
  28508. return { value: '', type: null, comment: '', range: [start, start, start] };
  28509. const type = header.mode === '>' ? Scalar.BLOCK_FOLDED : Scalar.BLOCK_LITERAL;
  28510. const lines = scalar.source ? splitLines(scalar.source) : [];
  28511. // determine the end of content & start of chomping
  28512. let chompStart = lines.length;
  28513. for (let i = lines.length - 1; i >= 0; --i) {
  28514. const content = lines[i][1];
  28515. if (content === '' || content === '\r')
  28516. chompStart = i;
  28517. else
  28518. break;
  28519. }
  28520. // shortcut for empty contents
  28521. if (chompStart === 0) {
  28522. const value = header.chomp === '+' && lines.length > 0
  28523. ? '\n'.repeat(Math.max(1, lines.length - 1))
  28524. : '';
  28525. let end = start + header.length;
  28526. if (scalar.source)
  28527. end += scalar.source.length;
  28528. return { value, type, comment: header.comment, range: [start, end, end] };
  28529. }
  28530. // find the indentation level to trim from start
  28531. let trimIndent = scalar.indent + header.indent;
  28532. let offset = scalar.offset + header.length;
  28533. let contentStart = 0;
  28534. for (let i = 0; i < chompStart; ++i) {
  28535. const [indent, content] = lines[i];
  28536. if (content === '' || content === '\r') {
  28537. if (header.indent === 0 && indent.length > trimIndent)
  28538. trimIndent = indent.length;
  28539. }
  28540. else {
  28541. if (indent.length < trimIndent) {
  28542. const message = 'Block scalars with more-indented leading empty lines must use an explicit indentation indicator';
  28543. onError(offset + indent.length, 'MISSING_CHAR', message);
  28544. }
  28545. if (header.indent === 0)
  28546. trimIndent = indent.length;
  28547. contentStart = i;
  28548. if (trimIndent === 0 && !ctx.atRoot) {
  28549. const message = 'Block scalar values in collections must be indented';
  28550. onError(offset, 'BAD_INDENT', message);
  28551. }
  28552. break;
  28553. }
  28554. offset += indent.length + content.length + 1;
  28555. }
  28556. // include trailing more-indented empty lines in content
  28557. for (let i = lines.length - 1; i >= chompStart; --i) {
  28558. if (lines[i][0].length > trimIndent)
  28559. chompStart = i + 1;
  28560. }
  28561. let value = '';
  28562. let sep = '';
  28563. let prevMoreIndented = false;
  28564. // leading whitespace is kept intact
  28565. for (let i = 0; i < contentStart; ++i)
  28566. value += lines[i][0].slice(trimIndent) + '\n';
  28567. for (let i = contentStart; i < chompStart; ++i) {
  28568. let [indent, content] = lines[i];
  28569. offset += indent.length + content.length + 1;
  28570. const crlf = content[content.length - 1] === '\r';
  28571. if (crlf)
  28572. content = content.slice(0, -1);
  28573. /* istanbul ignore if already caught in lexer */
  28574. if (content && indent.length < trimIndent) {
  28575. const src = header.indent
  28576. ? 'explicit indentation indicator'
  28577. : 'first line';
  28578. const message = `Block scalar lines must not be less indented than their ${src}`;
  28579. onError(offset - content.length - (crlf ? 2 : 1), 'BAD_INDENT', message);
  28580. indent = '';
  28581. }
  28582. if (type === Scalar.BLOCK_LITERAL) {
  28583. value += sep + indent.slice(trimIndent) + content;
  28584. sep = '\n';
  28585. }
  28586. else if (indent.length > trimIndent || content[0] === '\t') {
  28587. // more-indented content within a folded block
  28588. if (sep === ' ')
  28589. sep = '\n';
  28590. else if (!prevMoreIndented && sep === '\n')
  28591. sep = '\n\n';
  28592. value += sep + indent.slice(trimIndent) + content;
  28593. sep = '\n';
  28594. prevMoreIndented = true;
  28595. }
  28596. else if (content === '') {
  28597. // empty line
  28598. if (sep === '\n')
  28599. value += '\n';
  28600. else
  28601. sep = '\n';
  28602. }
  28603. else {
  28604. value += sep + content;
  28605. sep = ' ';
  28606. prevMoreIndented = false;
  28607. }
  28608. }
  28609. switch (header.chomp) {
  28610. case '-':
  28611. break;
  28612. case '+':
  28613. for (let i = chompStart; i < lines.length; ++i)
  28614. value += '\n' + lines[i][0].slice(trimIndent);
  28615. if (value[value.length - 1] !== '\n')
  28616. value += '\n';
  28617. break;
  28618. default:
  28619. value += '\n';
  28620. }
  28621. const end = start + header.length + scalar.source.length;
  28622. return { value, type, comment: header.comment, range: [start, end, end] };
  28623. }
  28624. function parseBlockScalarHeader({ offset, props }, strict, onError) {
  28625. /* istanbul ignore if should not happen */
  28626. if (props[0].type !== 'block-scalar-header') {
  28627. onError(props[0], 'IMPOSSIBLE', 'Block scalar header not found');
  28628. return null;
  28629. }
  28630. const { source } = props[0];
  28631. const mode = source[0];
  28632. let indent = 0;
  28633. let chomp = '';
  28634. let error = -1;
  28635. for (let i = 1; i < source.length; ++i) {
  28636. const ch = source[i];
  28637. if (!chomp && (ch === '-' || ch === '+'))
  28638. chomp = ch;
  28639. else {
  28640. const n = Number(ch);
  28641. if (!indent && n)
  28642. indent = n;
  28643. else if (error === -1)
  28644. error = offset + i;
  28645. }
  28646. }
  28647. if (error !== -1)
  28648. onError(error, 'UNEXPECTED_TOKEN', `Block scalar header includes extra characters: ${source}`);
  28649. let hasSpace = false;
  28650. let comment = '';
  28651. let length = source.length;
  28652. for (let i = 1; i < props.length; ++i) {
  28653. const token = props[i];
  28654. switch (token.type) {
  28655. case 'space':
  28656. hasSpace = true;
  28657. // fallthrough
  28658. case 'newline':
  28659. length += token.source.length;
  28660. break;
  28661. case 'comment':
  28662. if (strict && !hasSpace) {
  28663. const message = 'Comments must be separated from other tokens by white space characters';
  28664. onError(token, 'MISSING_CHAR', message);
  28665. }
  28666. length += token.source.length;
  28667. comment = token.source.substring(1);
  28668. break;
  28669. case 'error':
  28670. onError(token, 'UNEXPECTED_TOKEN', token.message);
  28671. length += token.source.length;
  28672. break;
  28673. /* istanbul ignore next should not happen */
  28674. default: {
  28675. const message = `Unexpected token in block scalar header: ${token.type}`;
  28676. onError(token, 'UNEXPECTED_TOKEN', message);
  28677. const ts = token.source;
  28678. if (ts && typeof ts === 'string')
  28679. length += ts.length;
  28680. }
  28681. }
  28682. }
  28683. return { mode, indent, chomp, comment, length };
  28684. }
  28685. /** @returns Array of lines split up as `[indent, content]` */
  28686. function splitLines(source) {
  28687. const split = source.split(/\n( *)/);
  28688. const first = split[0];
  28689. const m = first.match(/^( *)/);
  28690. const line0 = m?.[1]
  28691. ? [m[1], first.slice(m[1].length)]
  28692. : ['', first];
  28693. const lines = [line0];
  28694. for (let i = 1; i < split.length; i += 2)
  28695. lines.push([split[i], split[i + 1]]);
  28696. return lines;
  28697. }
  28698. function resolveFlowScalar(scalar, strict, onError) {
  28699. const { offset, type, source, end } = scalar;
  28700. let _type;
  28701. let value;
  28702. const _onError = (rel, code, msg) => onError(offset + rel, code, msg);
  28703. switch (type) {
  28704. case 'scalar':
  28705. _type = Scalar.PLAIN;
  28706. value = plainValue(source, _onError);
  28707. break;
  28708. case 'single-quoted-scalar':
  28709. _type = Scalar.QUOTE_SINGLE;
  28710. value = singleQuotedValue(source, _onError);
  28711. break;
  28712. case 'double-quoted-scalar':
  28713. _type = Scalar.QUOTE_DOUBLE;
  28714. value = doubleQuotedValue(source, _onError);
  28715. break;
  28716. /* istanbul ignore next should not happen */
  28717. default:
  28718. onError(scalar, 'UNEXPECTED_TOKEN', `Expected a flow scalar value, but found: ${type}`);
  28719. return {
  28720. value: '',
  28721. type: null,
  28722. comment: '',
  28723. range: [offset, offset + source.length, offset + source.length]
  28724. };
  28725. }
  28726. const valueEnd = offset + source.length;
  28727. const re = resolveEnd(end, valueEnd, strict, onError);
  28728. return {
  28729. value,
  28730. type: _type,
  28731. comment: re.comment,
  28732. range: [offset, valueEnd, re.offset]
  28733. };
  28734. }
  28735. function plainValue(source, onError) {
  28736. let badChar = '';
  28737. switch (source[0]) {
  28738. /* istanbul ignore next should not happen */
  28739. case '\t':
  28740. badChar = 'a tab character';
  28741. break;
  28742. case ',':
  28743. badChar = 'flow indicator character ,';
  28744. break;
  28745. case '%':
  28746. badChar = 'directive indicator character %';
  28747. break;
  28748. case '|':
  28749. case '>': {
  28750. badChar = `block scalar indicator ${source[0]}`;
  28751. break;
  28752. }
  28753. case '@':
  28754. case '`': {
  28755. badChar = `reserved character ${source[0]}`;
  28756. break;
  28757. }
  28758. }
  28759. if (badChar)
  28760. onError(0, 'BAD_SCALAR_START', `Plain value cannot start with ${badChar}`);
  28761. return foldLines(source);
  28762. }
  28763. function singleQuotedValue(source, onError) {
  28764. if (source[source.length - 1] !== "'" || source.length === 1)
  28765. onError(source.length, 'MISSING_CHAR', "Missing closing 'quote");
  28766. return foldLines(source.slice(1, -1)).replace(/''/g, "'");
  28767. }
  28768. function foldLines(source) {
  28769. /**
  28770. * The negative lookbehind here and in the `re` RegExp is to
  28771. * prevent causing a polynomial search time in certain cases.
  28772. *
  28773. * The try-catch is for Safari, which doesn't support this yet:
  28774. * https://caniuse.com/js-regexp-lookbehind
  28775. */
  28776. let first, line;
  28777. try {
  28778. first = new RegExp('(.*?)(?<![ \t])[ \t]*\r?\n', 'sy');
  28779. line = new RegExp('[ \t]*(.*?)(?:(?<![ \t])[ \t]*)?\r?\n', 'sy');
  28780. }
  28781. catch (_) {
  28782. first = /(.*?)[ \t]*\r?\n/sy;
  28783. line = /[ \t]*(.*?)[ \t]*\r?\n/sy;
  28784. }
  28785. let match = first.exec(source);
  28786. if (!match)
  28787. return source;
  28788. let res = match[1];
  28789. let sep = ' ';
  28790. let pos = first.lastIndex;
  28791. line.lastIndex = pos;
  28792. while ((match = line.exec(source))) {
  28793. if (match[1] === '') {
  28794. if (sep === '\n')
  28795. res += sep;
  28796. else
  28797. sep = '\n';
  28798. }
  28799. else {
  28800. res += sep + match[1];
  28801. sep = ' ';
  28802. }
  28803. pos = line.lastIndex;
  28804. }
  28805. const last = /[ \t]*(.*)/sy;
  28806. last.lastIndex = pos;
  28807. match = last.exec(source);
  28808. return res + sep + (match?.[1] ?? '');
  28809. }
  28810. function doubleQuotedValue(source, onError) {
  28811. let res = '';
  28812. for (let i = 1; i < source.length - 1; ++i) {
  28813. const ch = source[i];
  28814. if (ch === '\r' && source[i + 1] === '\n')
  28815. continue;
  28816. if (ch === '\n') {
  28817. const { fold, offset } = foldNewline(source, i);
  28818. res += fold;
  28819. i = offset;
  28820. }
  28821. else if (ch === '\\') {
  28822. let next = source[++i];
  28823. const cc = escapeCodes[next];
  28824. if (cc)
  28825. res += cc;
  28826. else if (next === '\n') {
  28827. // skip escaped newlines, but still trim the following line
  28828. next = source[i + 1];
  28829. while (next === ' ' || next === '\t')
  28830. next = source[++i + 1];
  28831. }
  28832. else if (next === '\r' && source[i + 1] === '\n') {
  28833. // skip escaped CRLF newlines, but still trim the following line
  28834. next = source[++i + 1];
  28835. while (next === ' ' || next === '\t')
  28836. next = source[++i + 1];
  28837. }
  28838. else if (next === 'x' || next === 'u' || next === 'U') {
  28839. const length = { x: 2, u: 4, U: 8 }[next];
  28840. res += parseCharCode(source, i + 1, length, onError);
  28841. i += length;
  28842. }
  28843. else {
  28844. const raw = source.substr(i - 1, 2);
  28845. onError(i - 1, 'BAD_DQ_ESCAPE', `Invalid escape sequence ${raw}`);
  28846. res += raw;
  28847. }
  28848. }
  28849. else if (ch === ' ' || ch === '\t') {
  28850. // trim trailing whitespace
  28851. const wsStart = i;
  28852. let next = source[i + 1];
  28853. while (next === ' ' || next === '\t')
  28854. next = source[++i + 1];
  28855. if (next !== '\n' && !(next === '\r' && source[i + 2] === '\n'))
  28856. res += i > wsStart ? source.slice(wsStart, i + 1) : ch;
  28857. }
  28858. else {
  28859. res += ch;
  28860. }
  28861. }
  28862. if (source[source.length - 1] !== '"' || source.length === 1)
  28863. onError(source.length, 'MISSING_CHAR', 'Missing closing "quote');
  28864. return res;
  28865. }
  28866. /**
  28867. * Fold a single newline into a space, multiple newlines to N - 1 newlines.
  28868. * Presumes `source[offset] === '\n'`
  28869. */
  28870. function foldNewline(source, offset) {
  28871. let fold = '';
  28872. let ch = source[offset + 1];
  28873. while (ch === ' ' || ch === '\t' || ch === '\n' || ch === '\r') {
  28874. if (ch === '\r' && source[offset + 2] !== '\n')
  28875. break;
  28876. if (ch === '\n')
  28877. fold += '\n';
  28878. offset += 1;
  28879. ch = source[offset + 1];
  28880. }
  28881. if (!fold)
  28882. fold = ' ';
  28883. return { fold, offset };
  28884. }
  28885. const escapeCodes = {
  28886. '0': '\0', // null character
  28887. a: '\x07', // bell character
  28888. b: '\b', // backspace
  28889. e: '\x1b', // escape character
  28890. f: '\f', // form feed
  28891. n: '\n', // line feed
  28892. r: '\r', // carriage return
  28893. t: '\t', // horizontal tab
  28894. v: '\v', // vertical tab
  28895. N: '\u0085', // Unicode next line
  28896. _: '\u00a0', // Unicode non-breaking space
  28897. L: '\u2028', // Unicode line separator
  28898. P: '\u2029', // Unicode paragraph separator
  28899. ' ': ' ',
  28900. '"': '"',
  28901. '/': '/',
  28902. '\\': '\\',
  28903. '\t': '\t'
  28904. };
  28905. function parseCharCode(source, offset, length, onError) {
  28906. const cc = source.substr(offset, length);
  28907. const ok = cc.length === length && /^[0-9a-fA-F]+$/.test(cc);
  28908. const code = ok ? parseInt(cc, 16) : NaN;
  28909. if (isNaN(code)) {
  28910. const raw = source.substr(offset - 2, length + 2);
  28911. onError(offset - 2, 'BAD_DQ_ESCAPE', `Invalid escape sequence ${raw}`);
  28912. return raw;
  28913. }
  28914. return String.fromCodePoint(code);
  28915. }
  28916. function composeScalar(ctx, token, tagToken, onError) {
  28917. const { value, type, comment, range } = token.type === 'block-scalar'
  28918. ? resolveBlockScalar(ctx, token, onError)
  28919. : resolveFlowScalar(token, ctx.options.strict, onError);
  28920. const tagName = tagToken
  28921. ? ctx.directives.tagName(tagToken.source, msg => onError(tagToken, 'TAG_RESOLVE_FAILED', msg))
  28922. : null;
  28923. const tag = tagToken && tagName
  28924. ? findScalarTagByName(ctx.schema, value, tagName, tagToken, onError)
  28925. : token.type === 'scalar'
  28926. ? findScalarTagByTest(ctx, value, token, onError)
  28927. : ctx.schema[SCALAR$1];
  28928. let scalar;
  28929. try {
  28930. const res = tag.resolve(value, msg => onError(tagToken ?? token, 'TAG_RESOLVE_FAILED', msg), ctx.options);
  28931. scalar = isScalar$1(res) ? res : new Scalar(res);
  28932. }
  28933. catch (error) {
  28934. const msg = error instanceof Error ? error.message : String(error);
  28935. onError(tagToken ?? token, 'TAG_RESOLVE_FAILED', msg);
  28936. scalar = new Scalar(value);
  28937. }
  28938. scalar.range = range;
  28939. scalar.source = value;
  28940. if (type)
  28941. scalar.type = type;
  28942. if (tagName)
  28943. scalar.tag = tagName;
  28944. if (tag.format)
  28945. scalar.format = tag.format;
  28946. if (comment)
  28947. scalar.comment = comment;
  28948. return scalar;
  28949. }
  28950. function findScalarTagByName(schema, value, tagName, tagToken, onError) {
  28951. if (tagName === '!')
  28952. return schema[SCALAR$1]; // non-specific tag
  28953. const matchWithTest = [];
  28954. for (const tag of schema.tags) {
  28955. if (!tag.collection && tag.tag === tagName) {
  28956. if (tag.default && tag.test)
  28957. matchWithTest.push(tag);
  28958. else
  28959. return tag;
  28960. }
  28961. }
  28962. for (const tag of matchWithTest)
  28963. if (tag.test?.test(value))
  28964. return tag;
  28965. const kt = schema.knownTags[tagName];
  28966. if (kt && !kt.collection) {
  28967. // Ensure that the known tag is available for stringifying,
  28968. // but does not get used by default.
  28969. schema.tags.push(Object.assign({}, kt, { default: false, test: undefined }));
  28970. return kt;
  28971. }
  28972. onError(tagToken, 'TAG_RESOLVE_FAILED', `Unresolved tag: ${tagName}`, tagName !== 'tag:yaml.org,2002:str');
  28973. return schema[SCALAR$1];
  28974. }
  28975. function findScalarTagByTest({ directives, schema }, value, token, onError) {
  28976. const tag = schema.tags.find(tag => tag.default && tag.test?.test(value)) || schema[SCALAR$1];
  28977. if (schema.compat) {
  28978. const compat = schema.compat.find(tag => tag.default && tag.test?.test(value)) ??
  28979. schema[SCALAR$1];
  28980. if (tag.tag !== compat.tag) {
  28981. const ts = directives.tagString(tag.tag);
  28982. const cs = directives.tagString(compat.tag);
  28983. const msg = `Value may be parsed as either ${ts} or ${cs}`;
  28984. onError(token, 'TAG_RESOLVE_FAILED', msg, true);
  28985. }
  28986. }
  28987. return tag;
  28988. }
  28989. function emptyScalarPosition(offset, before, pos) {
  28990. if (before) {
  28991. if (pos === null)
  28992. pos = before.length;
  28993. for (let i = pos - 1; i >= 0; --i) {
  28994. let st = before[i];
  28995. switch (st.type) {
  28996. case 'space':
  28997. case 'comment':
  28998. case 'newline':
  28999. offset -= st.source.length;
  29000. continue;
  29001. }
  29002. // Technically, an empty scalar is immediately after the last non-empty
  29003. // node, but it's more useful to place it after any whitespace.
  29004. st = before[++i];
  29005. while (st?.type === 'space') {
  29006. offset += st.source.length;
  29007. st = before[++i];
  29008. }
  29009. break;
  29010. }
  29011. }
  29012. return offset;
  29013. }
  29014. const CN = { composeNode, composeEmptyNode };
  29015. function composeNode(ctx, token, props, onError) {
  29016. const { spaceBefore, comment, anchor, tag } = props;
  29017. let node;
  29018. let isSrcToken = true;
  29019. switch (token.type) {
  29020. case 'alias':
  29021. node = composeAlias(ctx, token, onError);
  29022. if (anchor || tag)
  29023. onError(token, 'ALIAS_PROPS', 'An alias node must not specify any properties');
  29024. break;
  29025. case 'scalar':
  29026. case 'single-quoted-scalar':
  29027. case 'double-quoted-scalar':
  29028. case 'block-scalar':
  29029. node = composeScalar(ctx, token, tag, onError);
  29030. if (anchor)
  29031. node.anchor = anchor.source.substring(1);
  29032. break;
  29033. case 'block-map':
  29034. case 'block-seq':
  29035. case 'flow-collection':
  29036. node = composeCollection(CN, ctx, token, props, onError);
  29037. if (anchor)
  29038. node.anchor = anchor.source.substring(1);
  29039. break;
  29040. default: {
  29041. const message = token.type === 'error'
  29042. ? token.message
  29043. : `Unsupported token (type: ${token.type})`;
  29044. onError(token, 'UNEXPECTED_TOKEN', message);
  29045. node = composeEmptyNode(ctx, token.offset, undefined, null, props, onError);
  29046. isSrcToken = false;
  29047. }
  29048. }
  29049. if (anchor && node.anchor === '')
  29050. onError(anchor, 'BAD_ALIAS', 'Anchor cannot be an empty string');
  29051. if (spaceBefore)
  29052. node.spaceBefore = true;
  29053. if (comment) {
  29054. if (token.type === 'scalar' && token.source === '')
  29055. node.comment = comment;
  29056. else
  29057. node.commentBefore = comment;
  29058. }
  29059. // @ts-expect-error Type checking misses meaning of isSrcToken
  29060. if (ctx.options.keepSourceTokens && isSrcToken)
  29061. node.srcToken = token;
  29062. return node;
  29063. }
  29064. function composeEmptyNode(ctx, offset, before, pos, { spaceBefore, comment, anchor, tag, end }, onError) {
  29065. const token = {
  29066. type: 'scalar',
  29067. offset: emptyScalarPosition(offset, before, pos),
  29068. indent: -1,
  29069. source: ''
  29070. };
  29071. const node = composeScalar(ctx, token, tag, onError);
  29072. if (anchor) {
  29073. node.anchor = anchor.source.substring(1);
  29074. if (node.anchor === '')
  29075. onError(anchor, 'BAD_ALIAS', 'Anchor cannot be an empty string');
  29076. }
  29077. if (spaceBefore)
  29078. node.spaceBefore = true;
  29079. if (comment) {
  29080. node.comment = comment;
  29081. node.range[2] = end;
  29082. }
  29083. return node;
  29084. }
  29085. function composeAlias({ options }, { offset, source, end }, onError) {
  29086. const alias = new Alias(source.substring(1));
  29087. if (alias.source === '')
  29088. onError(offset, 'BAD_ALIAS', 'Alias cannot be an empty string');
  29089. if (alias.source.endsWith(':'))
  29090. onError(offset + source.length - 1, 'BAD_ALIAS', 'Alias ending in : is ambiguous', true);
  29091. const valueEnd = offset + source.length;
  29092. const re = resolveEnd(end, valueEnd, options.strict, onError);
  29093. alias.range = [offset, valueEnd, re.offset];
  29094. if (re.comment)
  29095. alias.comment = re.comment;
  29096. return alias;
  29097. }
  29098. function composeDoc(options, directives, { offset, start, value, end }, onError) {
  29099. const opts = Object.assign({ _directives: directives }, options);
  29100. const doc = new Document(undefined, opts);
  29101. const ctx = {
  29102. atRoot: true,
  29103. directives: doc.directives,
  29104. options: doc.options,
  29105. schema: doc.schema
  29106. };
  29107. const props = resolveProps(start, {
  29108. indicator: 'doc-start',
  29109. next: value ?? end?.[0],
  29110. offset,
  29111. onError,
  29112. parentIndent: 0,
  29113. startOnNewline: true
  29114. });
  29115. if (props.found) {
  29116. doc.directives.docStart = true;
  29117. if (value &&
  29118. (value.type === 'block-map' || value.type === 'block-seq') &&
  29119. !props.hasNewline)
  29120. onError(props.end, 'MISSING_CHAR', 'Block collection cannot start on same line with directives-end marker');
  29121. }
  29122. // @ts-expect-error If Contents is set, let's trust the user
  29123. doc.contents = value
  29124. ? composeNode(ctx, value, props, onError)
  29125. : composeEmptyNode(ctx, props.end, start, null, props, onError);
  29126. const contentEnd = doc.contents.range[2];
  29127. const re = resolveEnd(end, contentEnd, false, onError);
  29128. if (re.comment)
  29129. doc.comment = re.comment;
  29130. doc.range = [offset, contentEnd, re.offset];
  29131. return doc;
  29132. }
  29133. function getErrorPos(src) {
  29134. if (typeof src === 'number')
  29135. return [src, src + 1];
  29136. if (Array.isArray(src))
  29137. return src.length === 2 ? src : [src[0], src[1]];
  29138. const { offset, source } = src;
  29139. return [offset, offset + (typeof source === 'string' ? source.length : 1)];
  29140. }
  29141. function parsePrelude(prelude) {
  29142. let comment = '';
  29143. let atComment = false;
  29144. let afterEmptyLine = false;
  29145. for (let i = 0; i < prelude.length; ++i) {
  29146. const source = prelude[i];
  29147. switch (source[0]) {
  29148. case '#':
  29149. comment +=
  29150. (comment === '' ? '' : afterEmptyLine ? '\n\n' : '\n') +
  29151. (source.substring(1) || ' ');
  29152. atComment = true;
  29153. afterEmptyLine = false;
  29154. break;
  29155. case '%':
  29156. if (prelude[i + 1]?.[0] !== '#')
  29157. i += 1;
  29158. atComment = false;
  29159. break;
  29160. default:
  29161. // This may be wrong after doc-end, but in that case it doesn't matter
  29162. if (!atComment)
  29163. afterEmptyLine = true;
  29164. atComment = false;
  29165. }
  29166. }
  29167. return { comment, afterEmptyLine };
  29168. }
  29169. /**
  29170. * Compose a stream of CST nodes into a stream of YAML Documents.
  29171. *
  29172. * ```ts
  29173. * import { Composer, Parser } from 'yaml'
  29174. *
  29175. * const src: string = ...
  29176. * const tokens = new Parser().parse(src)
  29177. * const docs = new Composer().compose(tokens)
  29178. * ```
  29179. */
  29180. class Composer {
  29181. constructor(options = {}) {
  29182. this.doc = null;
  29183. this.atDirectives = false;
  29184. this.prelude = [];
  29185. this.errors = [];
  29186. this.warnings = [];
  29187. this.onError = (source, code, message, warning) => {
  29188. const pos = getErrorPos(source);
  29189. if (warning)
  29190. this.warnings.push(new YAMLWarning(pos, code, message));
  29191. else
  29192. this.errors.push(new YAMLParseError(pos, code, message));
  29193. };
  29194. // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
  29195. this.directives = new Directives({ version: options.version || '1.2' });
  29196. this.options = options;
  29197. }
  29198. decorate(doc, afterDoc) {
  29199. const { comment, afterEmptyLine } = parsePrelude(this.prelude);
  29200. //console.log({ dc: doc.comment, prelude, comment })
  29201. if (comment) {
  29202. const dc = doc.contents;
  29203. if (afterDoc) {
  29204. doc.comment = doc.comment ? `${doc.comment}\n${comment}` : comment;
  29205. }
  29206. else if (afterEmptyLine || doc.directives.docStart || !dc) {
  29207. doc.commentBefore = comment;
  29208. }
  29209. else if (isCollection$1(dc) && !dc.flow && dc.items.length > 0) {
  29210. let it = dc.items[0];
  29211. if (isPair(it))
  29212. it = it.key;
  29213. const cb = it.commentBefore;
  29214. it.commentBefore = cb ? `${comment}\n${cb}` : comment;
  29215. }
  29216. else {
  29217. const cb = dc.commentBefore;
  29218. dc.commentBefore = cb ? `${comment}\n${cb}` : comment;
  29219. }
  29220. }
  29221. if (afterDoc) {
  29222. Array.prototype.push.apply(doc.errors, this.errors);
  29223. Array.prototype.push.apply(doc.warnings, this.warnings);
  29224. }
  29225. else {
  29226. doc.errors = this.errors;
  29227. doc.warnings = this.warnings;
  29228. }
  29229. this.prelude = [];
  29230. this.errors = [];
  29231. this.warnings = [];
  29232. }
  29233. /**
  29234. * Current stream status information.
  29235. *
  29236. * Mostly useful at the end of input for an empty stream.
  29237. */
  29238. streamInfo() {
  29239. return {
  29240. comment: parsePrelude(this.prelude).comment,
  29241. directives: this.directives,
  29242. errors: this.errors,
  29243. warnings: this.warnings
  29244. };
  29245. }
  29246. /**
  29247. * Compose tokens into documents.
  29248. *
  29249. * @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.
  29250. * @param endOffset - Should be set if `forceDoc` is also set, to set the document range end and to indicate errors correctly.
  29251. */
  29252. *compose(tokens, forceDoc = false, endOffset = -1) {
  29253. for (const token of tokens)
  29254. yield* this.next(token);
  29255. yield* this.end(forceDoc, endOffset);
  29256. }
  29257. /** Advance the composer by one CST token. */
  29258. *next(token) {
  29259. switch (token.type) {
  29260. case 'directive':
  29261. this.directives.add(token.source, (offset, message, warning) => {
  29262. const pos = getErrorPos(token);
  29263. pos[0] += offset;
  29264. this.onError(pos, 'BAD_DIRECTIVE', message, warning);
  29265. });
  29266. this.prelude.push(token.source);
  29267. this.atDirectives = true;
  29268. break;
  29269. case 'document': {
  29270. const doc = composeDoc(this.options, this.directives, token, this.onError);
  29271. if (this.atDirectives && !doc.directives.docStart)
  29272. this.onError(token, 'MISSING_CHAR', 'Missing directives-end/doc-start indicator line');
  29273. this.decorate(doc, false);
  29274. if (this.doc)
  29275. yield this.doc;
  29276. this.doc = doc;
  29277. this.atDirectives = false;
  29278. break;
  29279. }
  29280. case 'byte-order-mark':
  29281. case 'space':
  29282. break;
  29283. case 'comment':
  29284. case 'newline':
  29285. this.prelude.push(token.source);
  29286. break;
  29287. case 'error': {
  29288. const msg = token.source
  29289. ? `${token.message}: ${JSON.stringify(token.source)}`
  29290. : token.message;
  29291. const error = new YAMLParseError(getErrorPos(token), 'UNEXPECTED_TOKEN', msg);
  29292. if (this.atDirectives || !this.doc)
  29293. this.errors.push(error);
  29294. else
  29295. this.doc.errors.push(error);
  29296. break;
  29297. }
  29298. case 'doc-end': {
  29299. if (!this.doc) {
  29300. const msg = 'Unexpected doc-end without preceding document';
  29301. this.errors.push(new YAMLParseError(getErrorPos(token), 'UNEXPECTED_TOKEN', msg));
  29302. break;
  29303. }
  29304. this.doc.directives.docEnd = true;
  29305. const end = resolveEnd(token.end, token.offset + token.source.length, this.doc.options.strict, this.onError);
  29306. this.decorate(this.doc, true);
  29307. if (end.comment) {
  29308. const dc = this.doc.comment;
  29309. this.doc.comment = dc ? `${dc}\n${end.comment}` : end.comment;
  29310. }
  29311. this.doc.range[2] = end.offset;
  29312. break;
  29313. }
  29314. default:
  29315. this.errors.push(new YAMLParseError(getErrorPos(token), 'UNEXPECTED_TOKEN', `Unsupported token ${token.type}`));
  29316. }
  29317. }
  29318. /**
  29319. * Call at end of input to yield any remaining document.
  29320. *
  29321. * @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.
  29322. * @param endOffset - Should be set if `forceDoc` is also set, to set the document range end and to indicate errors correctly.
  29323. */
  29324. *end(forceDoc = false, endOffset = -1) {
  29325. if (this.doc) {
  29326. this.decorate(this.doc, true);
  29327. yield this.doc;
  29328. this.doc = null;
  29329. }
  29330. else if (forceDoc) {
  29331. const opts = Object.assign({ _directives: this.directives }, this.options);
  29332. const doc = new Document(undefined, opts);
  29333. if (this.atDirectives)
  29334. this.onError(endOffset, 'MISSING_CHAR', 'Missing directives-end indicator line');
  29335. doc.range = [0, endOffset, endOffset];
  29336. this.decorate(doc, false);
  29337. yield doc;
  29338. }
  29339. }
  29340. }
  29341. function resolveAsScalar(token, strict = true, onError) {
  29342. if (token) {
  29343. const _onError = (pos, code, message) => {
  29344. const offset = typeof pos === 'number' ? pos : Array.isArray(pos) ? pos[0] : pos.offset;
  29345. if (onError)
  29346. onError(offset, code, message);
  29347. else
  29348. throw new YAMLParseError([offset, offset + 1], code, message);
  29349. };
  29350. switch (token.type) {
  29351. case 'scalar':
  29352. case 'single-quoted-scalar':
  29353. case 'double-quoted-scalar':
  29354. return resolveFlowScalar(token, strict, _onError);
  29355. case 'block-scalar':
  29356. return resolveBlockScalar({ options: { strict } }, token, _onError);
  29357. }
  29358. }
  29359. return null;
  29360. }
  29361. /**
  29362. * Create a new scalar token with `value`
  29363. *
  29364. * Values that represent an actual string but may be parsed as a different type should use a `type` other than `'PLAIN'`,
  29365. * as this function does not support any schema operations and won't check for such conflicts.
  29366. *
  29367. * @param value The string representation of the value, which will have its content properly indented.
  29368. * @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.
  29369. * @param context.implicitKey Being within an implicit key may affect the resolved type of the token's value.
  29370. * @param context.indent The indent level of the token.
  29371. * @param context.inFlow Is this scalar within a flow collection? This may affect the resolved type of the token's value.
  29372. * @param context.offset The offset position of the token.
  29373. * @param context.type The preferred type of the scalar token. If undefined, the previous type of the `token` will be used, defaulting to `'PLAIN'`.
  29374. */
  29375. function createScalarToken(value, context) {
  29376. const { implicitKey = false, indent, inFlow = false, offset = -1, type = 'PLAIN' } = context;
  29377. const source = stringifyString({ type, value }, {
  29378. implicitKey,
  29379. indent: indent > 0 ? ' '.repeat(indent) : '',
  29380. inFlow,
  29381. options: { blockQuote: true, lineWidth: -1 }
  29382. });
  29383. const end = context.end ?? [
  29384. { type: 'newline', offset: -1, indent, source: '\n' }
  29385. ];
  29386. switch (source[0]) {
  29387. case '|':
  29388. case '>': {
  29389. const he = source.indexOf('\n');
  29390. const head = source.substring(0, he);
  29391. const body = source.substring(he + 1) + '\n';
  29392. const props = [
  29393. { type: 'block-scalar-header', offset, indent, source: head }
  29394. ];
  29395. if (!addEndtoBlockProps(props, end))
  29396. props.push({ type: 'newline', offset: -1, indent, source: '\n' });
  29397. return { type: 'block-scalar', offset, indent, props, source: body };
  29398. }
  29399. case '"':
  29400. return { type: 'double-quoted-scalar', offset, indent, source, end };
  29401. case "'":
  29402. return { type: 'single-quoted-scalar', offset, indent, source, end };
  29403. default:
  29404. return { type: 'scalar', offset, indent, source, end };
  29405. }
  29406. }
  29407. /**
  29408. * Set the value of `token` to the given string `value`, overwriting any previous contents and type that it may have.
  29409. *
  29410. * Best efforts are made to retain any comments previously associated with the `token`,
  29411. * though all contents within a collection's `items` will be overwritten.
  29412. *
  29413. * Values that represent an actual string but may be parsed as a different type should use a `type` other than `'PLAIN'`,
  29414. * as this function does not support any schema operations and won't check for such conflicts.
  29415. *
  29416. * @param token Any token. If it does not include an `indent` value, the value will be stringified as if it were an implicit key.
  29417. * @param value The string representation of the value, which will have its content properly indented.
  29418. * @param context.afterKey In most cases, values after a key should have an additional level of indentation.
  29419. * @param context.implicitKey Being within an implicit key may affect the resolved type of the token's value.
  29420. * @param context.inFlow Being within a flow collection may affect the resolved type of the token's value.
  29421. * @param context.type The preferred type of the scalar token. If undefined, the previous type of the `token` will be used, defaulting to `'PLAIN'`.
  29422. */
  29423. function setScalarValue(token, value, context = {}) {
  29424. let { afterKey = false, implicitKey = false, inFlow = false, type } = context;
  29425. let indent = 'indent' in token ? token.indent : null;
  29426. if (afterKey && typeof indent === 'number')
  29427. indent += 2;
  29428. if (!type)
  29429. switch (token.type) {
  29430. case 'single-quoted-scalar':
  29431. type = 'QUOTE_SINGLE';
  29432. break;
  29433. case 'double-quoted-scalar':
  29434. type = 'QUOTE_DOUBLE';
  29435. break;
  29436. case 'block-scalar': {
  29437. const header = token.props[0];
  29438. if (header.type !== 'block-scalar-header')
  29439. throw new Error('Invalid block scalar header');
  29440. type = header.source[0] === '>' ? 'BLOCK_FOLDED' : 'BLOCK_LITERAL';
  29441. break;
  29442. }
  29443. default:
  29444. type = 'PLAIN';
  29445. }
  29446. const source = stringifyString({ type, value }, {
  29447. implicitKey: implicitKey || indent === null,
  29448. indent: indent !== null && indent > 0 ? ' '.repeat(indent) : '',
  29449. inFlow,
  29450. options: { blockQuote: true, lineWidth: -1 }
  29451. });
  29452. switch (source[0]) {
  29453. case '|':
  29454. case '>':
  29455. setBlockScalarValue(token, source);
  29456. break;
  29457. case '"':
  29458. setFlowScalarValue(token, source, 'double-quoted-scalar');
  29459. break;
  29460. case "'":
  29461. setFlowScalarValue(token, source, 'single-quoted-scalar');
  29462. break;
  29463. default:
  29464. setFlowScalarValue(token, source, 'scalar');
  29465. }
  29466. }
  29467. function setBlockScalarValue(token, source) {
  29468. const he = source.indexOf('\n');
  29469. const head = source.substring(0, he);
  29470. const body = source.substring(he + 1) + '\n';
  29471. if (token.type === 'block-scalar') {
  29472. const header = token.props[0];
  29473. if (header.type !== 'block-scalar-header')
  29474. throw new Error('Invalid block scalar header');
  29475. header.source = head;
  29476. token.source = body;
  29477. }
  29478. else {
  29479. const { offset } = token;
  29480. const indent = 'indent' in token ? token.indent : -1;
  29481. const props = [
  29482. { type: 'block-scalar-header', offset, indent, source: head }
  29483. ];
  29484. if (!addEndtoBlockProps(props, 'end' in token ? token.end : undefined))
  29485. props.push({ type: 'newline', offset: -1, indent, source: '\n' });
  29486. for (const key of Object.keys(token))
  29487. if (key !== 'type' && key !== 'offset')
  29488. delete token[key];
  29489. Object.assign(token, { type: 'block-scalar', indent, props, source: body });
  29490. }
  29491. }
  29492. /** @returns `true` if last token is a newline */
  29493. function addEndtoBlockProps(props, end) {
  29494. if (end)
  29495. for (const st of end)
  29496. switch (st.type) {
  29497. case 'space':
  29498. case 'comment':
  29499. props.push(st);
  29500. break;
  29501. case 'newline':
  29502. props.push(st);
  29503. return true;
  29504. }
  29505. return false;
  29506. }
  29507. function setFlowScalarValue(token, source, type) {
  29508. switch (token.type) {
  29509. case 'scalar':
  29510. case 'double-quoted-scalar':
  29511. case 'single-quoted-scalar':
  29512. token.type = type;
  29513. token.source = source;
  29514. break;
  29515. case 'block-scalar': {
  29516. const end = token.props.slice(1);
  29517. let oa = source.length;
  29518. if (token.props[0].type === 'block-scalar-header')
  29519. oa -= token.props[0].source.length;
  29520. for (const tok of end)
  29521. tok.offset += oa;
  29522. delete token.props;
  29523. Object.assign(token, { type, source, end });
  29524. break;
  29525. }
  29526. case 'block-map':
  29527. case 'block-seq': {
  29528. const offset = token.offset + source.length;
  29529. const nl = { type: 'newline', offset, indent: token.indent, source: '\n' };
  29530. delete token.items;
  29531. Object.assign(token, { type, source, end: [nl] });
  29532. break;
  29533. }
  29534. default: {
  29535. const indent = 'indent' in token ? token.indent : -1;
  29536. const end = 'end' in token && Array.isArray(token.end)
  29537. ? token.end.filter(st => st.type === 'space' ||
  29538. st.type === 'comment' ||
  29539. st.type === 'newline')
  29540. : [];
  29541. for (const key of Object.keys(token))
  29542. if (key !== 'type' && key !== 'offset')
  29543. delete token[key];
  29544. Object.assign(token, { type, indent, source, end });
  29545. }
  29546. }
  29547. }
  29548. /**
  29549. * Stringify a CST document, token, or collection item
  29550. *
  29551. * Fair warning: This applies no validation whatsoever, and
  29552. * simply concatenates the sources in their logical order.
  29553. */
  29554. const stringify$1 = (cst) => 'type' in cst ? stringifyToken(cst) : stringifyItem(cst);
  29555. function stringifyToken(token) {
  29556. switch (token.type) {
  29557. case 'block-scalar': {
  29558. let res = '';
  29559. for (const tok of token.props)
  29560. res += stringifyToken(tok);
  29561. return res + token.source;
  29562. }
  29563. case 'block-map':
  29564. case 'block-seq': {
  29565. let res = '';
  29566. for (const item of token.items)
  29567. res += stringifyItem(item);
  29568. return res;
  29569. }
  29570. case 'flow-collection': {
  29571. let res = token.start.source;
  29572. for (const item of token.items)
  29573. res += stringifyItem(item);
  29574. for (const st of token.end)
  29575. res += st.source;
  29576. return res;
  29577. }
  29578. case 'document': {
  29579. let res = stringifyItem(token);
  29580. if (token.end)
  29581. for (const st of token.end)
  29582. res += st.source;
  29583. return res;
  29584. }
  29585. default: {
  29586. let res = token.source;
  29587. if ('end' in token && token.end)
  29588. for (const st of token.end)
  29589. res += st.source;
  29590. return res;
  29591. }
  29592. }
  29593. }
  29594. function stringifyItem({ start, key, sep, value }) {
  29595. let res = '';
  29596. for (const st of start)
  29597. res += st.source;
  29598. if (key)
  29599. res += stringifyToken(key);
  29600. if (sep)
  29601. for (const st of sep)
  29602. res += st.source;
  29603. if (value)
  29604. res += stringifyToken(value);
  29605. return res;
  29606. }
  29607. const BREAK = Symbol('break visit');
  29608. const SKIP = Symbol('skip children');
  29609. const REMOVE = Symbol('remove item');
  29610. /**
  29611. * Apply a visitor to a CST document or item.
  29612. *
  29613. * Walks through the tree (depth-first) starting from the root, calling a
  29614. * `visitor` function with two arguments when entering each item:
  29615. * - `item`: The current item, which included the following members:
  29616. * - `start: SourceToken[]` – Source tokens before the key or value,
  29617. * possibly including its anchor or tag.
  29618. * - `key?: Token | null` – Set for pair values. May then be `null`, if
  29619. * the key before the `:` separator is empty.
  29620. * - `sep?: SourceToken[]` – Source tokens between the key and the value,
  29621. * which should include the `:` map value indicator if `value` is set.
  29622. * - `value?: Token` – The value of a sequence item, or of a map pair.
  29623. * - `path`: The steps from the root to the current node, as an array of
  29624. * `['key' | 'value', number]` tuples.
  29625. *
  29626. * The return value of the visitor may be used to control the traversal:
  29627. * - `undefined` (default): Do nothing and continue
  29628. * - `visit.SKIP`: Do not visit the children of this token, continue with
  29629. * next sibling
  29630. * - `visit.BREAK`: Terminate traversal completely
  29631. * - `visit.REMOVE`: Remove the current item, then continue with the next one
  29632. * - `number`: Set the index of the next step. This is useful especially if
  29633. * the index of the current token has changed.
  29634. * - `function`: Define the next visitor for this item. After the original
  29635. * visitor is called on item entry, next visitors are called after handling
  29636. * a non-empty `key` and when exiting the item.
  29637. */
  29638. function visit(cst, visitor) {
  29639. if ('type' in cst && cst.type === 'document')
  29640. cst = { start: cst.start, value: cst.value };
  29641. _visit(Object.freeze([]), cst, visitor);
  29642. }
  29643. // Without the `as symbol` casts, TS declares these in the `visit`
  29644. // namespace using `var`, but then complains about that because
  29645. // `unique symbol` must be `const`.
  29646. /** Terminate visit traversal completely */
  29647. visit.BREAK = BREAK;
  29648. /** Do not visit the children of the current item */
  29649. visit.SKIP = SKIP;
  29650. /** Remove the current item */
  29651. visit.REMOVE = REMOVE;
  29652. /** Find the item at `path` from `cst` as the root */
  29653. visit.itemAtPath = (cst, path) => {
  29654. let item = cst;
  29655. for (const [field, index] of path) {
  29656. const tok = item?.[field];
  29657. if (tok && 'items' in tok) {
  29658. item = tok.items[index];
  29659. }
  29660. else
  29661. return undefined;
  29662. }
  29663. return item;
  29664. };
  29665. /**
  29666. * Get the immediate parent collection of the item at `path` from `cst` as the root.
  29667. *
  29668. * Throws an error if the collection is not found, which should never happen if the item itself exists.
  29669. */
  29670. visit.parentCollection = (cst, path) => {
  29671. const parent = visit.itemAtPath(cst, path.slice(0, -1));
  29672. const field = path[path.length - 1][0];
  29673. const coll = parent?.[field];
  29674. if (coll && 'items' in coll)
  29675. return coll;
  29676. throw new Error('Parent collection not found');
  29677. };
  29678. function _visit(path, item, visitor) {
  29679. let ctrl = visitor(item, path);
  29680. if (typeof ctrl === 'symbol')
  29681. return ctrl;
  29682. for (const field of ['key', 'value']) {
  29683. const token = item[field];
  29684. if (token && 'items' in token) {
  29685. for (let i = 0; i < token.items.length; ++i) {
  29686. const ci = _visit(Object.freeze(path.concat([[field, i]])), token.items[i], visitor);
  29687. if (typeof ci === 'number')
  29688. i = ci - 1;
  29689. else if (ci === BREAK)
  29690. return BREAK;
  29691. else if (ci === REMOVE) {
  29692. token.items.splice(i, 1);
  29693. i -= 1;
  29694. }
  29695. }
  29696. if (typeof ctrl === 'function' && field === 'key')
  29697. ctrl = ctrl(item, path);
  29698. }
  29699. }
  29700. return typeof ctrl === 'function' ? ctrl(item, path) : ctrl;
  29701. }
  29702. /** The byte order mark */
  29703. const BOM = '\u{FEFF}';
  29704. /** Start of doc-mode */
  29705. const DOCUMENT = '\x02'; // C0: Start of Text
  29706. /** Unexpected end of flow-mode */
  29707. const FLOW_END = '\x18'; // C0: Cancel
  29708. /** Next token is a scalar value */
  29709. const SCALAR = '\x1f'; // C0: Unit Separator
  29710. /** @returns `true` if `token` is a flow or block collection */
  29711. const isCollection = (token) => !!token && 'items' in token;
  29712. /** @returns `true` if `token` is a flow or block scalar; not an alias */
  29713. const isScalar = (token) => !!token &&
  29714. (token.type === 'scalar' ||
  29715. token.type === 'single-quoted-scalar' ||
  29716. token.type === 'double-quoted-scalar' ||
  29717. token.type === 'block-scalar');
  29718. /* istanbul ignore next */
  29719. /** Get a printable representation of a lexer token */
  29720. function prettyToken(token) {
  29721. switch (token) {
  29722. case BOM:
  29723. return '<BOM>';
  29724. case DOCUMENT:
  29725. return '<DOC>';
  29726. case FLOW_END:
  29727. return '<FLOW_END>';
  29728. case SCALAR:
  29729. return '<SCALAR>';
  29730. default:
  29731. return JSON.stringify(token);
  29732. }
  29733. }
  29734. /** Identify the type of a lexer token. May return `null` for unknown tokens. */
  29735. function tokenType(source) {
  29736. switch (source) {
  29737. case BOM:
  29738. return 'byte-order-mark';
  29739. case DOCUMENT:
  29740. return 'doc-mode';
  29741. case FLOW_END:
  29742. return 'flow-error-end';
  29743. case SCALAR:
  29744. return 'scalar';
  29745. case '---':
  29746. return 'doc-start';
  29747. case '...':
  29748. return 'doc-end';
  29749. case '':
  29750. case '\n':
  29751. case '\r\n':
  29752. return 'newline';
  29753. case '-':
  29754. return 'seq-item-ind';
  29755. case '?':
  29756. return 'explicit-key-ind';
  29757. case ':':
  29758. return 'map-value-ind';
  29759. case '{':
  29760. return 'flow-map-start';
  29761. case '}':
  29762. return 'flow-map-end';
  29763. case '[':
  29764. return 'flow-seq-start';
  29765. case ']':
  29766. return 'flow-seq-end';
  29767. case ',':
  29768. return 'comma';
  29769. }
  29770. switch (source[0]) {
  29771. case ' ':
  29772. case '\t':
  29773. return 'space';
  29774. case '#':
  29775. return 'comment';
  29776. case '%':
  29777. return 'directive-line';
  29778. case '*':
  29779. return 'alias';
  29780. case '&':
  29781. return 'anchor';
  29782. case '!':
  29783. return 'tag';
  29784. case "'":
  29785. return 'single-quoted-scalar';
  29786. case '"':
  29787. return 'double-quoted-scalar';
  29788. case '|':
  29789. case '>':
  29790. return 'block-scalar-header';
  29791. }
  29792. return null;
  29793. }
  29794. var cst = {
  29795. __proto__: null,
  29796. BOM: BOM,
  29797. DOCUMENT: DOCUMENT,
  29798. FLOW_END: FLOW_END,
  29799. SCALAR: SCALAR,
  29800. createScalarToken: createScalarToken,
  29801. isCollection: isCollection,
  29802. isScalar: isScalar,
  29803. prettyToken: prettyToken,
  29804. resolveAsScalar: resolveAsScalar,
  29805. setScalarValue: setScalarValue,
  29806. stringify: stringify$1,
  29807. tokenType: tokenType,
  29808. visit: visit
  29809. };
  29810. /*
  29811. START -> stream
  29812. stream
  29813. directive -> line-end -> stream
  29814. indent + line-end -> stream
  29815. [else] -> line-start
  29816. line-end
  29817. comment -> line-end
  29818. newline -> .
  29819. input-end -> END
  29820. line-start
  29821. doc-start -> doc
  29822. doc-end -> stream
  29823. [else] -> indent -> block-start
  29824. block-start
  29825. seq-item-start -> block-start
  29826. explicit-key-start -> block-start
  29827. map-value-start -> block-start
  29828. [else] -> doc
  29829. doc
  29830. line-end -> line-start
  29831. spaces -> doc
  29832. anchor -> doc
  29833. tag -> doc
  29834. flow-start -> flow -> doc
  29835. flow-end -> error -> doc
  29836. seq-item-start -> error -> doc
  29837. explicit-key-start -> error -> doc
  29838. map-value-start -> doc
  29839. alias -> doc
  29840. quote-start -> quoted-scalar -> doc
  29841. block-scalar-header -> line-end -> block-scalar(min) -> line-start
  29842. [else] -> plain-scalar(false, min) -> doc
  29843. flow
  29844. line-end -> flow
  29845. spaces -> flow
  29846. anchor -> flow
  29847. tag -> flow
  29848. flow-start -> flow -> flow
  29849. flow-end -> .
  29850. seq-item-start -> error -> flow
  29851. explicit-key-start -> flow
  29852. map-value-start -> flow
  29853. alias -> flow
  29854. quote-start -> quoted-scalar -> flow
  29855. comma -> flow
  29856. [else] -> plain-scalar(true, 0) -> flow
  29857. quoted-scalar
  29858. quote-end -> .
  29859. [else] -> quoted-scalar
  29860. block-scalar(min)
  29861. newline + peek(indent < min) -> .
  29862. [else] -> block-scalar(min)
  29863. plain-scalar(is-flow, min)
  29864. scalar-end(is-flow) -> .
  29865. peek(newline + (indent < min)) -> .
  29866. [else] -> plain-scalar(min)
  29867. */
  29868. function isEmpty(ch) {
  29869. switch (ch) {
  29870. case undefined:
  29871. case ' ':
  29872. case '\n':
  29873. case '\r':
  29874. case '\t':
  29875. return true;
  29876. default:
  29877. return false;
  29878. }
  29879. }
  29880. const hexDigits = new Set('0123456789ABCDEFabcdef');
  29881. const tagChars = new Set("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-#;/?:@&=+$_.!~*'()");
  29882. const flowIndicatorChars = new Set(',[]{}');
  29883. const invalidAnchorChars = new Set(' ,[]{}\n\r\t');
  29884. const isNotAnchorChar = (ch) => !ch || invalidAnchorChars.has(ch);
  29885. /**
  29886. * Splits an input string into lexical tokens, i.e. smaller strings that are
  29887. * easily identifiable by `tokens.tokenType()`.
  29888. *
  29889. * Lexing starts always in a "stream" context. Incomplete input may be buffered
  29890. * until a complete token can be emitted.
  29891. *
  29892. * In addition to slices of the original input, the following control characters
  29893. * may also be emitted:
  29894. *
  29895. * - `\x02` (Start of Text): A document starts with the next token
  29896. * - `\x18` (Cancel): Unexpected end of flow-mode (indicates an error)
  29897. * - `\x1f` (Unit Separator): Next token is a scalar value
  29898. * - `\u{FEFF}` (Byte order mark): Emitted separately outside documents
  29899. */
  29900. class Lexer {
  29901. constructor() {
  29902. /**
  29903. * Flag indicating whether the end of the current buffer marks the end of
  29904. * all input
  29905. */
  29906. this.atEnd = false;
  29907. /**
  29908. * Explicit indent set in block scalar header, as an offset from the current
  29909. * minimum indent, so e.g. set to 1 from a header `|2+`. Set to -1 if not
  29910. * explicitly set.
  29911. */
  29912. this.blockScalarIndent = -1;
  29913. /**
  29914. * Block scalars that include a + (keep) chomping indicator in their header
  29915. * include trailing empty lines, which are otherwise excluded from the
  29916. * scalar's contents.
  29917. */
  29918. this.blockScalarKeep = false;
  29919. /** Current input */
  29920. this.buffer = '';
  29921. /**
  29922. * Flag noting whether the map value indicator : can immediately follow this
  29923. * node within a flow context.
  29924. */
  29925. this.flowKey = false;
  29926. /** Count of surrounding flow collection levels. */
  29927. this.flowLevel = 0;
  29928. /**
  29929. * Minimum level of indentation required for next lines to be parsed as a
  29930. * part of the current scalar value.
  29931. */
  29932. this.indentNext = 0;
  29933. /** Indentation level of the current line. */
  29934. this.indentValue = 0;
  29935. /** Position of the next \n character. */
  29936. this.lineEndPos = null;
  29937. /** Stores the state of the lexer if reaching the end of incpomplete input */
  29938. this.next = null;
  29939. /** A pointer to `buffer`; the current position of the lexer. */
  29940. this.pos = 0;
  29941. }
  29942. /**
  29943. * Generate YAML tokens from the `source` string. If `incomplete`,
  29944. * a part of the last line may be left as a buffer for the next call.
  29945. *
  29946. * @returns A generator of lexical tokens
  29947. */
  29948. *lex(source, incomplete = false) {
  29949. if (source) {
  29950. if (typeof source !== 'string')
  29951. throw TypeError('source is not a string');
  29952. this.buffer = this.buffer ? this.buffer + source : source;
  29953. this.lineEndPos = null;
  29954. }
  29955. this.atEnd = !incomplete;
  29956. let next = this.next ?? 'stream';
  29957. while (next && (incomplete || this.hasChars(1)))
  29958. next = yield* this.parseNext(next);
  29959. }
  29960. atLineEnd() {
  29961. let i = this.pos;
  29962. let ch = this.buffer[i];
  29963. while (ch === ' ' || ch === '\t')
  29964. ch = this.buffer[++i];
  29965. if (!ch || ch === '#' || ch === '\n')
  29966. return true;
  29967. if (ch === '\r')
  29968. return this.buffer[i + 1] === '\n';
  29969. return false;
  29970. }
  29971. charAt(n) {
  29972. return this.buffer[this.pos + n];
  29973. }
  29974. continueScalar(offset) {
  29975. let ch = this.buffer[offset];
  29976. if (this.indentNext > 0) {
  29977. let indent = 0;
  29978. while (ch === ' ')
  29979. ch = this.buffer[++indent + offset];
  29980. if (ch === '\r') {
  29981. const next = this.buffer[indent + offset + 1];
  29982. if (next === '\n' || (!next && !this.atEnd))
  29983. return offset + indent + 1;
  29984. }
  29985. return ch === '\n' || indent >= this.indentNext || (!ch && !this.atEnd)
  29986. ? offset + indent
  29987. : -1;
  29988. }
  29989. if (ch === '-' || ch === '.') {
  29990. const dt = this.buffer.substr(offset, 3);
  29991. if ((dt === '---' || dt === '...') && isEmpty(this.buffer[offset + 3]))
  29992. return -1;
  29993. }
  29994. return offset;
  29995. }
  29996. getLine() {
  29997. let end = this.lineEndPos;
  29998. if (typeof end !== 'number' || (end !== -1 && end < this.pos)) {
  29999. end = this.buffer.indexOf('\n', this.pos);
  30000. this.lineEndPos = end;
  30001. }
  30002. if (end === -1)
  30003. return this.atEnd ? this.buffer.substring(this.pos) : null;
  30004. if (this.buffer[end - 1] === '\r')
  30005. end -= 1;
  30006. return this.buffer.substring(this.pos, end);
  30007. }
  30008. hasChars(n) {
  30009. return this.pos + n <= this.buffer.length;
  30010. }
  30011. setNext(state) {
  30012. this.buffer = this.buffer.substring(this.pos);
  30013. this.pos = 0;
  30014. this.lineEndPos = null;
  30015. this.next = state;
  30016. return null;
  30017. }
  30018. peek(n) {
  30019. return this.buffer.substr(this.pos, n);
  30020. }
  30021. *parseNext(next) {
  30022. switch (next) {
  30023. case 'stream':
  30024. return yield* this.parseStream();
  30025. case 'line-start':
  30026. return yield* this.parseLineStart();
  30027. case 'block-start':
  30028. return yield* this.parseBlockStart();
  30029. case 'doc':
  30030. return yield* this.parseDocument();
  30031. case 'flow':
  30032. return yield* this.parseFlowCollection();
  30033. case 'quoted-scalar':
  30034. return yield* this.parseQuotedScalar();
  30035. case 'block-scalar':
  30036. return yield* this.parseBlockScalar();
  30037. case 'plain-scalar':
  30038. return yield* this.parsePlainScalar();
  30039. }
  30040. }
  30041. *parseStream() {
  30042. let line = this.getLine();
  30043. if (line === null)
  30044. return this.setNext('stream');
  30045. if (line[0] === BOM) {
  30046. yield* this.pushCount(1);
  30047. line = line.substring(1);
  30048. }
  30049. if (line[0] === '%') {
  30050. let dirEnd = line.length;
  30051. let cs = line.indexOf('#');
  30052. while (cs !== -1) {
  30053. const ch = line[cs - 1];
  30054. if (ch === ' ' || ch === '\t') {
  30055. dirEnd = cs - 1;
  30056. break;
  30057. }
  30058. else {
  30059. cs = line.indexOf('#', cs + 1);
  30060. }
  30061. }
  30062. while (true) {
  30063. const ch = line[dirEnd - 1];
  30064. if (ch === ' ' || ch === '\t')
  30065. dirEnd -= 1;
  30066. else
  30067. break;
  30068. }
  30069. const n = (yield* this.pushCount(dirEnd)) + (yield* this.pushSpaces(true));
  30070. yield* this.pushCount(line.length - n); // possible comment
  30071. this.pushNewline();
  30072. return 'stream';
  30073. }
  30074. if (this.atLineEnd()) {
  30075. const sp = yield* this.pushSpaces(true);
  30076. yield* this.pushCount(line.length - sp);
  30077. yield* this.pushNewline();
  30078. return 'stream';
  30079. }
  30080. yield DOCUMENT;
  30081. return yield* this.parseLineStart();
  30082. }
  30083. *parseLineStart() {
  30084. const ch = this.charAt(0);
  30085. if (!ch && !this.atEnd)
  30086. return this.setNext('line-start');
  30087. if (ch === '-' || ch === '.') {
  30088. if (!this.atEnd && !this.hasChars(4))
  30089. return this.setNext('line-start');
  30090. const s = this.peek(3);
  30091. if ((s === '---' || s === '...') && isEmpty(this.charAt(3))) {
  30092. yield* this.pushCount(3);
  30093. this.indentValue = 0;
  30094. this.indentNext = 0;
  30095. return s === '---' ? 'doc' : 'stream';
  30096. }
  30097. }
  30098. this.indentValue = yield* this.pushSpaces(false);
  30099. if (this.indentNext > this.indentValue && !isEmpty(this.charAt(1)))
  30100. this.indentNext = this.indentValue;
  30101. return yield* this.parseBlockStart();
  30102. }
  30103. *parseBlockStart() {
  30104. const [ch0, ch1] = this.peek(2);
  30105. if (!ch1 && !this.atEnd)
  30106. return this.setNext('block-start');
  30107. if ((ch0 === '-' || ch0 === '?' || ch0 === ':') && isEmpty(ch1)) {
  30108. const n = (yield* this.pushCount(1)) + (yield* this.pushSpaces(true));
  30109. this.indentNext = this.indentValue + 1;
  30110. this.indentValue += n;
  30111. return yield* this.parseBlockStart();
  30112. }
  30113. return 'doc';
  30114. }
  30115. *parseDocument() {
  30116. yield* this.pushSpaces(true);
  30117. const line = this.getLine();
  30118. if (line === null)
  30119. return this.setNext('doc');
  30120. let n = yield* this.pushIndicators();
  30121. switch (line[n]) {
  30122. case '#':
  30123. yield* this.pushCount(line.length - n);
  30124. // fallthrough
  30125. case undefined:
  30126. yield* this.pushNewline();
  30127. return yield* this.parseLineStart();
  30128. case '{':
  30129. case '[':
  30130. yield* this.pushCount(1);
  30131. this.flowKey = false;
  30132. this.flowLevel = 1;
  30133. return 'flow';
  30134. case '}':
  30135. case ']':
  30136. // this is an error
  30137. yield* this.pushCount(1);
  30138. return 'doc';
  30139. case '*':
  30140. yield* this.pushUntil(isNotAnchorChar);
  30141. return 'doc';
  30142. case '"':
  30143. case "'":
  30144. return yield* this.parseQuotedScalar();
  30145. case '|':
  30146. case '>':
  30147. n += yield* this.parseBlockScalarHeader();
  30148. n += yield* this.pushSpaces(true);
  30149. yield* this.pushCount(line.length - n);
  30150. yield* this.pushNewline();
  30151. return yield* this.parseBlockScalar();
  30152. default:
  30153. return yield* this.parsePlainScalar();
  30154. }
  30155. }
  30156. *parseFlowCollection() {
  30157. let nl, sp;
  30158. let indent = -1;
  30159. do {
  30160. nl = yield* this.pushNewline();
  30161. if (nl > 0) {
  30162. sp = yield* this.pushSpaces(false);
  30163. this.indentValue = indent = sp;
  30164. }
  30165. else {
  30166. sp = 0;
  30167. }
  30168. sp += yield* this.pushSpaces(true);
  30169. } while (nl + sp > 0);
  30170. const line = this.getLine();
  30171. if (line === null)
  30172. return this.setNext('flow');
  30173. if ((indent !== -1 && indent < this.indentNext && line[0] !== '#') ||
  30174. (indent === 0 &&
  30175. (line.startsWith('---') || line.startsWith('...')) &&
  30176. isEmpty(line[3]))) {
  30177. // Allowing for the terminal ] or } at the same (rather than greater)
  30178. // indent level as the initial [ or { is technically invalid, but
  30179. // failing here would be surprising to users.
  30180. const atFlowEndMarker = indent === this.indentNext - 1 &&
  30181. this.flowLevel === 1 &&
  30182. (line[0] === ']' || line[0] === '}');
  30183. if (!atFlowEndMarker) {
  30184. // this is an error
  30185. this.flowLevel = 0;
  30186. yield FLOW_END;
  30187. return yield* this.parseLineStart();
  30188. }
  30189. }
  30190. let n = 0;
  30191. while (line[n] === ',') {
  30192. n += yield* this.pushCount(1);
  30193. n += yield* this.pushSpaces(true);
  30194. this.flowKey = false;
  30195. }
  30196. n += yield* this.pushIndicators();
  30197. switch (line[n]) {
  30198. case undefined:
  30199. return 'flow';
  30200. case '#':
  30201. yield* this.pushCount(line.length - n);
  30202. return 'flow';
  30203. case '{':
  30204. case '[':
  30205. yield* this.pushCount(1);
  30206. this.flowKey = false;
  30207. this.flowLevel += 1;
  30208. return 'flow';
  30209. case '}':
  30210. case ']':
  30211. yield* this.pushCount(1);
  30212. this.flowKey = true;
  30213. this.flowLevel -= 1;
  30214. return this.flowLevel ? 'flow' : 'doc';
  30215. case '*':
  30216. yield* this.pushUntil(isNotAnchorChar);
  30217. return 'flow';
  30218. case '"':
  30219. case "'":
  30220. this.flowKey = true;
  30221. return yield* this.parseQuotedScalar();
  30222. case ':': {
  30223. const next = this.charAt(1);
  30224. if (this.flowKey || isEmpty(next) || next === ',') {
  30225. this.flowKey = false;
  30226. yield* this.pushCount(1);
  30227. yield* this.pushSpaces(true);
  30228. return 'flow';
  30229. }
  30230. }
  30231. // fallthrough
  30232. default:
  30233. this.flowKey = false;
  30234. return yield* this.parsePlainScalar();
  30235. }
  30236. }
  30237. *parseQuotedScalar() {
  30238. const quote = this.charAt(0);
  30239. let end = this.buffer.indexOf(quote, this.pos + 1);
  30240. if (quote === "'") {
  30241. while (end !== -1 && this.buffer[end + 1] === "'")
  30242. end = this.buffer.indexOf("'", end + 2);
  30243. }
  30244. else {
  30245. // double-quote
  30246. while (end !== -1) {
  30247. let n = 0;
  30248. while (this.buffer[end - 1 - n] === '\\')
  30249. n += 1;
  30250. if (n % 2 === 0)
  30251. break;
  30252. end = this.buffer.indexOf('"', end + 1);
  30253. }
  30254. }
  30255. // Only looking for newlines within the quotes
  30256. const qb = this.buffer.substring(0, end);
  30257. let nl = qb.indexOf('\n', this.pos);
  30258. if (nl !== -1) {
  30259. while (nl !== -1) {
  30260. const cs = this.continueScalar(nl + 1);
  30261. if (cs === -1)
  30262. break;
  30263. nl = qb.indexOf('\n', cs);
  30264. }
  30265. if (nl !== -1) {
  30266. // this is an error caused by an unexpected unindent
  30267. end = nl - (qb[nl - 1] === '\r' ? 2 : 1);
  30268. }
  30269. }
  30270. if (end === -1) {
  30271. if (!this.atEnd)
  30272. return this.setNext('quoted-scalar');
  30273. end = this.buffer.length;
  30274. }
  30275. yield* this.pushToIndex(end + 1, false);
  30276. return this.flowLevel ? 'flow' : 'doc';
  30277. }
  30278. *parseBlockScalarHeader() {
  30279. this.blockScalarIndent = -1;
  30280. this.blockScalarKeep = false;
  30281. let i = this.pos;
  30282. while (true) {
  30283. const ch = this.buffer[++i];
  30284. if (ch === '+')
  30285. this.blockScalarKeep = true;
  30286. else if (ch > '0' && ch <= '9')
  30287. this.blockScalarIndent = Number(ch) - 1;
  30288. else if (ch !== '-')
  30289. break;
  30290. }
  30291. return yield* this.pushUntil(ch => isEmpty(ch) || ch === '#');
  30292. }
  30293. *parseBlockScalar() {
  30294. let nl = this.pos - 1; // may be -1 if this.pos === 0
  30295. let indent = 0;
  30296. let ch;
  30297. loop: for (let i = this.pos; (ch = this.buffer[i]); ++i) {
  30298. switch (ch) {
  30299. case ' ':
  30300. indent += 1;
  30301. break;
  30302. case '\n':
  30303. nl = i;
  30304. indent = 0;
  30305. break;
  30306. case '\r': {
  30307. const next = this.buffer[i + 1];
  30308. if (!next && !this.atEnd)
  30309. return this.setNext('block-scalar');
  30310. if (next === '\n')
  30311. break;
  30312. } // fallthrough
  30313. default:
  30314. break loop;
  30315. }
  30316. }
  30317. if (!ch && !this.atEnd)
  30318. return this.setNext('block-scalar');
  30319. if (indent >= this.indentNext) {
  30320. if (this.blockScalarIndent === -1)
  30321. this.indentNext = indent;
  30322. else {
  30323. this.indentNext =
  30324. this.blockScalarIndent + (this.indentNext === 0 ? 1 : this.indentNext);
  30325. }
  30326. do {
  30327. const cs = this.continueScalar(nl + 1);
  30328. if (cs === -1)
  30329. break;
  30330. nl = this.buffer.indexOf('\n', cs);
  30331. } while (nl !== -1);
  30332. if (nl === -1) {
  30333. if (!this.atEnd)
  30334. return this.setNext('block-scalar');
  30335. nl = this.buffer.length;
  30336. }
  30337. }
  30338. // Trailing insufficiently indented tabs are invalid.
  30339. // To catch that during parsing, we include them in the block scalar value.
  30340. let i = nl + 1;
  30341. ch = this.buffer[i];
  30342. while (ch === ' ')
  30343. ch = this.buffer[++i];
  30344. if (ch === '\t') {
  30345. while (ch === '\t' || ch === ' ' || ch === '\r' || ch === '\n')
  30346. ch = this.buffer[++i];
  30347. nl = i - 1;
  30348. }
  30349. else if (!this.blockScalarKeep) {
  30350. do {
  30351. let i = nl - 1;
  30352. let ch = this.buffer[i];
  30353. if (ch === '\r')
  30354. ch = this.buffer[--i];
  30355. const lastChar = i; // Drop the line if last char not more indented
  30356. while (ch === ' ')
  30357. ch = this.buffer[--i];
  30358. if (ch === '\n' && i >= this.pos && i + 1 + indent > lastChar)
  30359. nl = i;
  30360. else
  30361. break;
  30362. } while (true);
  30363. }
  30364. yield SCALAR;
  30365. yield* this.pushToIndex(nl + 1, true);
  30366. return yield* this.parseLineStart();
  30367. }
  30368. *parsePlainScalar() {
  30369. const inFlow = this.flowLevel > 0;
  30370. let end = this.pos - 1;
  30371. let i = this.pos - 1;
  30372. let ch;
  30373. while ((ch = this.buffer[++i])) {
  30374. if (ch === ':') {
  30375. const next = this.buffer[i + 1];
  30376. if (isEmpty(next) || (inFlow && flowIndicatorChars.has(next)))
  30377. break;
  30378. end = i;
  30379. }
  30380. else if (isEmpty(ch)) {
  30381. let next = this.buffer[i + 1];
  30382. if (ch === '\r') {
  30383. if (next === '\n') {
  30384. i += 1;
  30385. ch = '\n';
  30386. next = this.buffer[i + 1];
  30387. }
  30388. else
  30389. end = i;
  30390. }
  30391. if (next === '#' || (inFlow && flowIndicatorChars.has(next)))
  30392. break;
  30393. if (ch === '\n') {
  30394. const cs = this.continueScalar(i + 1);
  30395. if (cs === -1)
  30396. break;
  30397. i = Math.max(i, cs - 2); // to advance, but still account for ' #'
  30398. }
  30399. }
  30400. else {
  30401. if (inFlow && flowIndicatorChars.has(ch))
  30402. break;
  30403. end = i;
  30404. }
  30405. }
  30406. if (!ch && !this.atEnd)
  30407. return this.setNext('plain-scalar');
  30408. yield SCALAR;
  30409. yield* this.pushToIndex(end + 1, true);
  30410. return inFlow ? 'flow' : 'doc';
  30411. }
  30412. *pushCount(n) {
  30413. if (n > 0) {
  30414. yield this.buffer.substr(this.pos, n);
  30415. this.pos += n;
  30416. return n;
  30417. }
  30418. return 0;
  30419. }
  30420. *pushToIndex(i, allowEmpty) {
  30421. const s = this.buffer.slice(this.pos, i);
  30422. if (s) {
  30423. yield s;
  30424. this.pos += s.length;
  30425. return s.length;
  30426. }
  30427. else if (allowEmpty)
  30428. yield '';
  30429. return 0;
  30430. }
  30431. *pushIndicators() {
  30432. switch (this.charAt(0)) {
  30433. case '!':
  30434. return ((yield* this.pushTag()) +
  30435. (yield* this.pushSpaces(true)) +
  30436. (yield* this.pushIndicators()));
  30437. case '&':
  30438. return ((yield* this.pushUntil(isNotAnchorChar)) +
  30439. (yield* this.pushSpaces(true)) +
  30440. (yield* this.pushIndicators()));
  30441. case '-': // this is an error
  30442. case '?': // this is an error outside flow collections
  30443. case ':': {
  30444. const inFlow = this.flowLevel > 0;
  30445. const ch1 = this.charAt(1);
  30446. if (isEmpty(ch1) || (inFlow && flowIndicatorChars.has(ch1))) {
  30447. if (!inFlow)
  30448. this.indentNext = this.indentValue + 1;
  30449. else if (this.flowKey)
  30450. this.flowKey = false;
  30451. return ((yield* this.pushCount(1)) +
  30452. (yield* this.pushSpaces(true)) +
  30453. (yield* this.pushIndicators()));
  30454. }
  30455. }
  30456. }
  30457. return 0;
  30458. }
  30459. *pushTag() {
  30460. if (this.charAt(1) === '<') {
  30461. let i = this.pos + 2;
  30462. let ch = this.buffer[i];
  30463. while (!isEmpty(ch) && ch !== '>')
  30464. ch = this.buffer[++i];
  30465. return yield* this.pushToIndex(ch === '>' ? i + 1 : i, false);
  30466. }
  30467. else {
  30468. let i = this.pos + 1;
  30469. let ch = this.buffer[i];
  30470. while (ch) {
  30471. if (tagChars.has(ch))
  30472. ch = this.buffer[++i];
  30473. else if (ch === '%' &&
  30474. hexDigits.has(this.buffer[i + 1]) &&
  30475. hexDigits.has(this.buffer[i + 2])) {
  30476. ch = this.buffer[(i += 3)];
  30477. }
  30478. else
  30479. break;
  30480. }
  30481. return yield* this.pushToIndex(i, false);
  30482. }
  30483. }
  30484. *pushNewline() {
  30485. const ch = this.buffer[this.pos];
  30486. if (ch === '\n')
  30487. return yield* this.pushCount(1);
  30488. else if (ch === '\r' && this.charAt(1) === '\n')
  30489. return yield* this.pushCount(2);
  30490. else
  30491. return 0;
  30492. }
  30493. *pushSpaces(allowTabs) {
  30494. let i = this.pos - 1;
  30495. let ch;
  30496. do {
  30497. ch = this.buffer[++i];
  30498. } while (ch === ' ' || (allowTabs && ch === '\t'));
  30499. const n = i - this.pos;
  30500. if (n > 0) {
  30501. yield this.buffer.substr(this.pos, n);
  30502. this.pos = i;
  30503. }
  30504. return n;
  30505. }
  30506. *pushUntil(test) {
  30507. let i = this.pos;
  30508. let ch = this.buffer[i];
  30509. while (!test(ch))
  30510. ch = this.buffer[++i];
  30511. return yield* this.pushToIndex(i, false);
  30512. }
  30513. }
  30514. /**
  30515. * Tracks newlines during parsing in order to provide an efficient API for
  30516. * determining the one-indexed `{ line, col }` position for any offset
  30517. * within the input.
  30518. */
  30519. class LineCounter {
  30520. constructor() {
  30521. this.lineStarts = [];
  30522. /**
  30523. * Should be called in ascending order. Otherwise, call
  30524. * `lineCounter.lineStarts.sort()` before calling `linePos()`.
  30525. */
  30526. this.addNewLine = (offset) => this.lineStarts.push(offset);
  30527. /**
  30528. * Performs a binary search and returns the 1-indexed { line, col }
  30529. * position of `offset`. If `line === 0`, `addNewLine` has never been
  30530. * called or `offset` is before the first known newline.
  30531. */
  30532. this.linePos = (offset) => {
  30533. let low = 0;
  30534. let high = this.lineStarts.length;
  30535. while (low < high) {
  30536. const mid = (low + high) >> 1; // Math.floor((low + high) / 2)
  30537. if (this.lineStarts[mid] < offset)
  30538. low = mid + 1;
  30539. else
  30540. high = mid;
  30541. }
  30542. if (this.lineStarts[low] === offset)
  30543. return { line: low + 1, col: 1 };
  30544. if (low === 0)
  30545. return { line: 0, col: offset };
  30546. const start = this.lineStarts[low - 1];
  30547. return { line: low, col: offset - start + 1 };
  30548. };
  30549. }
  30550. }
  30551. function includesToken(list, type) {
  30552. for (let i = 0; i < list.length; ++i)
  30553. if (list[i].type === type)
  30554. return true;
  30555. return false;
  30556. }
  30557. function findNonEmptyIndex(list) {
  30558. for (let i = 0; i < list.length; ++i) {
  30559. switch (list[i].type) {
  30560. case 'space':
  30561. case 'comment':
  30562. case 'newline':
  30563. break;
  30564. default:
  30565. return i;
  30566. }
  30567. }
  30568. return -1;
  30569. }
  30570. function isFlowToken(token) {
  30571. switch (token?.type) {
  30572. case 'alias':
  30573. case 'scalar':
  30574. case 'single-quoted-scalar':
  30575. case 'double-quoted-scalar':
  30576. case 'flow-collection':
  30577. return true;
  30578. default:
  30579. return false;
  30580. }
  30581. }
  30582. function getPrevProps(parent) {
  30583. switch (parent.type) {
  30584. case 'document':
  30585. return parent.start;
  30586. case 'block-map': {
  30587. const it = parent.items[parent.items.length - 1];
  30588. return it.sep ?? it.start;
  30589. }
  30590. case 'block-seq':
  30591. return parent.items[parent.items.length - 1].start;
  30592. /* istanbul ignore next should not happen */
  30593. default:
  30594. return [];
  30595. }
  30596. }
  30597. /** Note: May modify input array */
  30598. function getFirstKeyStartProps(prev) {
  30599. if (prev.length === 0)
  30600. return [];
  30601. let i = prev.length;
  30602. loop: while (--i >= 0) {
  30603. switch (prev[i].type) {
  30604. case 'doc-start':
  30605. case 'explicit-key-ind':
  30606. case 'map-value-ind':
  30607. case 'seq-item-ind':
  30608. case 'newline':
  30609. break loop;
  30610. }
  30611. }
  30612. return prev.splice(i, prev.length);
  30613. }
  30614. function fixFlowSeqItems(fc) {
  30615. if (fc.start.type === 'flow-seq-start') {
  30616. for (const it of fc.items) {
  30617. if (it.sep &&
  30618. !it.value &&
  30619. !includesToken(it.start, 'explicit-key-ind') &&
  30620. !includesToken(it.sep, 'map-value-ind')) {
  30621. if (it.key)
  30622. it.value = it.key;
  30623. delete it.key;
  30624. if (isFlowToken(it.value)) {
  30625. if (it.value.end)
  30626. Array.prototype.push.apply(it.value.end, it.sep);
  30627. else
  30628. it.value.end = it.sep;
  30629. }
  30630. else
  30631. Array.prototype.push.apply(it.start, it.sep);
  30632. delete it.sep;
  30633. }
  30634. }
  30635. }
  30636. }
  30637. /**
  30638. * A YAML concrete syntax tree (CST) parser
  30639. *
  30640. * ```ts
  30641. * const src: string = ...
  30642. * for (const token of new Parser().parse(src)) {
  30643. * // token: Token
  30644. * }
  30645. * ```
  30646. *
  30647. * To use the parser with a user-provided lexer:
  30648. *
  30649. * ```ts
  30650. * function* parse(source: string, lexer: Lexer) {
  30651. * const parser = new Parser()
  30652. * for (const lexeme of lexer.lex(source))
  30653. * yield* parser.next(lexeme)
  30654. * yield* parser.end()
  30655. * }
  30656. *
  30657. * const src: string = ...
  30658. * const lexer = new Lexer()
  30659. * for (const token of parse(src, lexer)) {
  30660. * // token: Token
  30661. * }
  30662. * ```
  30663. */
  30664. class Parser {
  30665. /**
  30666. * @param onNewLine - If defined, called separately with the start position of
  30667. * each new line (in `parse()`, including the start of input).
  30668. */
  30669. constructor(onNewLine) {
  30670. /** If true, space and sequence indicators count as indentation */
  30671. this.atNewLine = true;
  30672. /** If true, next token is a scalar value */
  30673. this.atScalar = false;
  30674. /** Current indentation level */
  30675. this.indent = 0;
  30676. /** Current offset since the start of parsing */
  30677. this.offset = 0;
  30678. /** On the same line with a block map key */
  30679. this.onKeyLine = false;
  30680. /** Top indicates the node that's currently being built */
  30681. this.stack = [];
  30682. /** The source of the current token, set in parse() */
  30683. this.source = '';
  30684. /** The type of the current token, set in parse() */
  30685. this.type = '';
  30686. // Must be defined after `next()`
  30687. this.lexer = new Lexer();
  30688. this.onNewLine = onNewLine;
  30689. }
  30690. /**
  30691. * Parse `source` as a YAML stream.
  30692. * If `incomplete`, a part of the last line may be left as a buffer for the next call.
  30693. *
  30694. * Errors are not thrown, but yielded as `{ type: 'error', message }` tokens.
  30695. *
  30696. * @returns A generator of tokens representing each directive, document, and other structure.
  30697. */
  30698. *parse(source, incomplete = false) {
  30699. if (this.onNewLine && this.offset === 0)
  30700. this.onNewLine(0);
  30701. for (const lexeme of this.lexer.lex(source, incomplete))
  30702. yield* this.next(lexeme);
  30703. if (!incomplete)
  30704. yield* this.end();
  30705. }
  30706. /**
  30707. * Advance the parser by the `source` of one lexical token.
  30708. */
  30709. *next(source) {
  30710. this.source = source;
  30711. if (this.atScalar) {
  30712. this.atScalar = false;
  30713. yield* this.step();
  30714. this.offset += source.length;
  30715. return;
  30716. }
  30717. const type = tokenType(source);
  30718. if (!type) {
  30719. const message = `Not a YAML token: ${source}`;
  30720. yield* this.pop({ type: 'error', offset: this.offset, message, source });
  30721. this.offset += source.length;
  30722. }
  30723. else if (type === 'scalar') {
  30724. this.atNewLine = false;
  30725. this.atScalar = true;
  30726. this.type = 'scalar';
  30727. }
  30728. else {
  30729. this.type = type;
  30730. yield* this.step();
  30731. switch (type) {
  30732. case 'newline':
  30733. this.atNewLine = true;
  30734. this.indent = 0;
  30735. if (this.onNewLine)
  30736. this.onNewLine(this.offset + source.length);
  30737. break;
  30738. case 'space':
  30739. if (this.atNewLine && source[0] === ' ')
  30740. this.indent += source.length;
  30741. break;
  30742. case 'explicit-key-ind':
  30743. case 'map-value-ind':
  30744. case 'seq-item-ind':
  30745. if (this.atNewLine)
  30746. this.indent += source.length;
  30747. break;
  30748. case 'doc-mode':
  30749. case 'flow-error-end':
  30750. return;
  30751. default:
  30752. this.atNewLine = false;
  30753. }
  30754. this.offset += source.length;
  30755. }
  30756. }
  30757. /** Call at end of input to push out any remaining constructions */
  30758. *end() {
  30759. while (this.stack.length > 0)
  30760. yield* this.pop();
  30761. }
  30762. get sourceToken() {
  30763. const st = {
  30764. type: this.type,
  30765. offset: this.offset,
  30766. indent: this.indent,
  30767. source: this.source
  30768. };
  30769. return st;
  30770. }
  30771. *step() {
  30772. const top = this.peek(1);
  30773. if (this.type === 'doc-end' && (!top || top.type !== 'doc-end')) {
  30774. while (this.stack.length > 0)
  30775. yield* this.pop();
  30776. this.stack.push({
  30777. type: 'doc-end',
  30778. offset: this.offset,
  30779. source: this.source
  30780. });
  30781. return;
  30782. }
  30783. if (!top)
  30784. return yield* this.stream();
  30785. switch (top.type) {
  30786. case 'document':
  30787. return yield* this.document(top);
  30788. case 'alias':
  30789. case 'scalar':
  30790. case 'single-quoted-scalar':
  30791. case 'double-quoted-scalar':
  30792. return yield* this.scalar(top);
  30793. case 'block-scalar':
  30794. return yield* this.blockScalar(top);
  30795. case 'block-map':
  30796. return yield* this.blockMap(top);
  30797. case 'block-seq':
  30798. return yield* this.blockSequence(top);
  30799. case 'flow-collection':
  30800. return yield* this.flowCollection(top);
  30801. case 'doc-end':
  30802. return yield* this.documentEnd(top);
  30803. }
  30804. /* istanbul ignore next should not happen */
  30805. yield* this.pop();
  30806. }
  30807. peek(n) {
  30808. return this.stack[this.stack.length - n];
  30809. }
  30810. *pop(error) {
  30811. const token = error ?? this.stack.pop();
  30812. /* istanbul ignore if should not happen */
  30813. if (!token) {
  30814. const message = 'Tried to pop an empty stack';
  30815. yield { type: 'error', offset: this.offset, source: '', message };
  30816. }
  30817. else if (this.stack.length === 0) {
  30818. yield token;
  30819. }
  30820. else {
  30821. const top = this.peek(1);
  30822. if (token.type === 'block-scalar') {
  30823. // Block scalars use their parent rather than header indent
  30824. token.indent = 'indent' in top ? top.indent : 0;
  30825. }
  30826. else if (token.type === 'flow-collection' && top.type === 'document') {
  30827. // Ignore all indent for top-level flow collections
  30828. token.indent = 0;
  30829. }
  30830. if (token.type === 'flow-collection')
  30831. fixFlowSeqItems(token);
  30832. switch (top.type) {
  30833. case 'document':
  30834. top.value = token;
  30835. break;
  30836. case 'block-scalar':
  30837. top.props.push(token); // error
  30838. break;
  30839. case 'block-map': {
  30840. const it = top.items[top.items.length - 1];
  30841. if (it.value) {
  30842. top.items.push({ start: [], key: token, sep: [] });
  30843. this.onKeyLine = true;
  30844. return;
  30845. }
  30846. else if (it.sep) {
  30847. it.value = token;
  30848. }
  30849. else {
  30850. Object.assign(it, { key: token, sep: [] });
  30851. this.onKeyLine = !it.explicitKey;
  30852. return;
  30853. }
  30854. break;
  30855. }
  30856. case 'block-seq': {
  30857. const it = top.items[top.items.length - 1];
  30858. if (it.value)
  30859. top.items.push({ start: [], value: token });
  30860. else
  30861. it.value = token;
  30862. break;
  30863. }
  30864. case 'flow-collection': {
  30865. const it = top.items[top.items.length - 1];
  30866. if (!it || it.value)
  30867. top.items.push({ start: [], key: token, sep: [] });
  30868. else if (it.sep)
  30869. it.value = token;
  30870. else
  30871. Object.assign(it, { key: token, sep: [] });
  30872. return;
  30873. }
  30874. /* istanbul ignore next should not happen */
  30875. default:
  30876. yield* this.pop();
  30877. yield* this.pop(token);
  30878. }
  30879. if ((top.type === 'document' ||
  30880. top.type === 'block-map' ||
  30881. top.type === 'block-seq') &&
  30882. (token.type === 'block-map' || token.type === 'block-seq')) {
  30883. const last = token.items[token.items.length - 1];
  30884. if (last &&
  30885. !last.sep &&
  30886. !last.value &&
  30887. last.start.length > 0 &&
  30888. findNonEmptyIndex(last.start) === -1 &&
  30889. (token.indent === 0 ||
  30890. last.start.every(st => st.type !== 'comment' || st.indent < token.indent))) {
  30891. if (top.type === 'document')
  30892. top.end = last.start;
  30893. else
  30894. top.items.push({ start: last.start });
  30895. token.items.splice(-1, 1);
  30896. }
  30897. }
  30898. }
  30899. }
  30900. *stream() {
  30901. switch (this.type) {
  30902. case 'directive-line':
  30903. yield { type: 'directive', offset: this.offset, source: this.source };
  30904. return;
  30905. case 'byte-order-mark':
  30906. case 'space':
  30907. case 'comment':
  30908. case 'newline':
  30909. yield this.sourceToken;
  30910. return;
  30911. case 'doc-mode':
  30912. case 'doc-start': {
  30913. const doc = {
  30914. type: 'document',
  30915. offset: this.offset,
  30916. start: []
  30917. };
  30918. if (this.type === 'doc-start')
  30919. doc.start.push(this.sourceToken);
  30920. this.stack.push(doc);
  30921. return;
  30922. }
  30923. }
  30924. yield {
  30925. type: 'error',
  30926. offset: this.offset,
  30927. message: `Unexpected ${this.type} token in YAML stream`,
  30928. source: this.source
  30929. };
  30930. }
  30931. *document(doc) {
  30932. if (doc.value)
  30933. return yield* this.lineEnd(doc);
  30934. switch (this.type) {
  30935. case 'doc-start': {
  30936. if (findNonEmptyIndex(doc.start) !== -1) {
  30937. yield* this.pop();
  30938. yield* this.step();
  30939. }
  30940. else
  30941. doc.start.push(this.sourceToken);
  30942. return;
  30943. }
  30944. case 'anchor':
  30945. case 'tag':
  30946. case 'space':
  30947. case 'comment':
  30948. case 'newline':
  30949. doc.start.push(this.sourceToken);
  30950. return;
  30951. }
  30952. const bv = this.startBlockValue(doc);
  30953. if (bv)
  30954. this.stack.push(bv);
  30955. else {
  30956. yield {
  30957. type: 'error',
  30958. offset: this.offset,
  30959. message: `Unexpected ${this.type} token in YAML document`,
  30960. source: this.source
  30961. };
  30962. }
  30963. }
  30964. *scalar(scalar) {
  30965. if (this.type === 'map-value-ind') {
  30966. const prev = getPrevProps(this.peek(2));
  30967. const start = getFirstKeyStartProps(prev);
  30968. let sep;
  30969. if (scalar.end) {
  30970. sep = scalar.end;
  30971. sep.push(this.sourceToken);
  30972. delete scalar.end;
  30973. }
  30974. else
  30975. sep = [this.sourceToken];
  30976. const map = {
  30977. type: 'block-map',
  30978. offset: scalar.offset,
  30979. indent: scalar.indent,
  30980. items: [{ start, key: scalar, sep }]
  30981. };
  30982. this.onKeyLine = true;
  30983. this.stack[this.stack.length - 1] = map;
  30984. }
  30985. else
  30986. yield* this.lineEnd(scalar);
  30987. }
  30988. *blockScalar(scalar) {
  30989. switch (this.type) {
  30990. case 'space':
  30991. case 'comment':
  30992. case 'newline':
  30993. scalar.props.push(this.sourceToken);
  30994. return;
  30995. case 'scalar':
  30996. scalar.source = this.source;
  30997. // block-scalar source includes trailing newline
  30998. this.atNewLine = true;
  30999. this.indent = 0;
  31000. if (this.onNewLine) {
  31001. let nl = this.source.indexOf('\n') + 1;
  31002. while (nl !== 0) {
  31003. this.onNewLine(this.offset + nl);
  31004. nl = this.source.indexOf('\n', nl) + 1;
  31005. }
  31006. }
  31007. yield* this.pop();
  31008. break;
  31009. /* istanbul ignore next should not happen */
  31010. default:
  31011. yield* this.pop();
  31012. yield* this.step();
  31013. }
  31014. }
  31015. *blockMap(map) {
  31016. const it = map.items[map.items.length - 1];
  31017. // it.sep is true-ish if pair already has key or : separator
  31018. switch (this.type) {
  31019. case 'newline':
  31020. this.onKeyLine = false;
  31021. if (it.value) {
  31022. const end = 'end' in it.value ? it.value.end : undefined;
  31023. const last = Array.isArray(end) ? end[end.length - 1] : undefined;
  31024. if (last?.type === 'comment')
  31025. end?.push(this.sourceToken);
  31026. else
  31027. map.items.push({ start: [this.sourceToken] });
  31028. }
  31029. else if (it.sep) {
  31030. it.sep.push(this.sourceToken);
  31031. }
  31032. else {
  31033. it.start.push(this.sourceToken);
  31034. }
  31035. return;
  31036. case 'space':
  31037. case 'comment':
  31038. if (it.value) {
  31039. map.items.push({ start: [this.sourceToken] });
  31040. }
  31041. else if (it.sep) {
  31042. it.sep.push(this.sourceToken);
  31043. }
  31044. else {
  31045. if (this.atIndentedComment(it.start, map.indent)) {
  31046. const prev = map.items[map.items.length - 2];
  31047. const end = prev?.value?.end;
  31048. if (Array.isArray(end)) {
  31049. Array.prototype.push.apply(end, it.start);
  31050. end.push(this.sourceToken);
  31051. map.items.pop();
  31052. return;
  31053. }
  31054. }
  31055. it.start.push(this.sourceToken);
  31056. }
  31057. return;
  31058. }
  31059. if (this.indent >= map.indent) {
  31060. const atMapIndent = !this.onKeyLine && this.indent === map.indent;
  31061. const atNextItem = atMapIndent &&
  31062. (it.sep || it.explicitKey) &&
  31063. this.type !== 'seq-item-ind';
  31064. // For empty nodes, assign newline-separated not indented empty tokens to following node
  31065. let start = [];
  31066. if (atNextItem && it.sep && !it.value) {
  31067. const nl = [];
  31068. for (let i = 0; i < it.sep.length; ++i) {
  31069. const st = it.sep[i];
  31070. switch (st.type) {
  31071. case 'newline':
  31072. nl.push(i);
  31073. break;
  31074. case 'space':
  31075. break;
  31076. case 'comment':
  31077. if (st.indent > map.indent)
  31078. nl.length = 0;
  31079. break;
  31080. default:
  31081. nl.length = 0;
  31082. }
  31083. }
  31084. if (nl.length >= 2)
  31085. start = it.sep.splice(nl[1]);
  31086. }
  31087. switch (this.type) {
  31088. case 'anchor':
  31089. case 'tag':
  31090. if (atNextItem || it.value) {
  31091. start.push(this.sourceToken);
  31092. map.items.push({ start });
  31093. this.onKeyLine = true;
  31094. }
  31095. else if (it.sep) {
  31096. it.sep.push(this.sourceToken);
  31097. }
  31098. else {
  31099. it.start.push(this.sourceToken);
  31100. }
  31101. return;
  31102. case 'explicit-key-ind':
  31103. if (!it.sep && !it.explicitKey) {
  31104. it.start.push(this.sourceToken);
  31105. it.explicitKey = true;
  31106. }
  31107. else if (atNextItem || it.value) {
  31108. start.push(this.sourceToken);
  31109. map.items.push({ start, explicitKey: true });
  31110. }
  31111. else {
  31112. this.stack.push({
  31113. type: 'block-map',
  31114. offset: this.offset,
  31115. indent: this.indent,
  31116. items: [{ start: [this.sourceToken], explicitKey: true }]
  31117. });
  31118. }
  31119. this.onKeyLine = true;
  31120. return;
  31121. case 'map-value-ind':
  31122. if (it.explicitKey) {
  31123. if (!it.sep) {
  31124. if (includesToken(it.start, 'newline')) {
  31125. Object.assign(it, { key: null, sep: [this.sourceToken] });
  31126. }
  31127. else {
  31128. const start = getFirstKeyStartProps(it.start);
  31129. this.stack.push({
  31130. type: 'block-map',
  31131. offset: this.offset,
  31132. indent: this.indent,
  31133. items: [{ start, key: null, sep: [this.sourceToken] }]
  31134. });
  31135. }
  31136. }
  31137. else if (it.value) {
  31138. map.items.push({ start: [], key: null, sep: [this.sourceToken] });
  31139. }
  31140. else if (includesToken(it.sep, 'map-value-ind')) {
  31141. this.stack.push({
  31142. type: 'block-map',
  31143. offset: this.offset,
  31144. indent: this.indent,
  31145. items: [{ start, key: null, sep: [this.sourceToken] }]
  31146. });
  31147. }
  31148. else if (isFlowToken(it.key) &&
  31149. !includesToken(it.sep, 'newline')) {
  31150. const start = getFirstKeyStartProps(it.start);
  31151. const key = it.key;
  31152. const sep = it.sep;
  31153. sep.push(this.sourceToken);
  31154. // @ts-expect-error type guard is wrong here
  31155. delete it.key, delete it.sep;
  31156. this.stack.push({
  31157. type: 'block-map',
  31158. offset: this.offset,
  31159. indent: this.indent,
  31160. items: [{ start, key, sep }]
  31161. });
  31162. }
  31163. else if (start.length > 0) {
  31164. // Not actually at next item
  31165. it.sep = it.sep.concat(start, this.sourceToken);
  31166. }
  31167. else {
  31168. it.sep.push(this.sourceToken);
  31169. }
  31170. }
  31171. else {
  31172. if (!it.sep) {
  31173. Object.assign(it, { key: null, sep: [this.sourceToken] });
  31174. }
  31175. else if (it.value || atNextItem) {
  31176. map.items.push({ start, key: null, sep: [this.sourceToken] });
  31177. }
  31178. else if (includesToken(it.sep, 'map-value-ind')) {
  31179. this.stack.push({
  31180. type: 'block-map',
  31181. offset: this.offset,
  31182. indent: this.indent,
  31183. items: [{ start: [], key: null, sep: [this.sourceToken] }]
  31184. });
  31185. }
  31186. else {
  31187. it.sep.push(this.sourceToken);
  31188. }
  31189. }
  31190. this.onKeyLine = true;
  31191. return;
  31192. case 'alias':
  31193. case 'scalar':
  31194. case 'single-quoted-scalar':
  31195. case 'double-quoted-scalar': {
  31196. const fs = this.flowScalar(this.type);
  31197. if (atNextItem || it.value) {
  31198. map.items.push({ start, key: fs, sep: [] });
  31199. this.onKeyLine = true;
  31200. }
  31201. else if (it.sep) {
  31202. this.stack.push(fs);
  31203. }
  31204. else {
  31205. Object.assign(it, { key: fs, sep: [] });
  31206. this.onKeyLine = true;
  31207. }
  31208. return;
  31209. }
  31210. default: {
  31211. const bv = this.startBlockValue(map);
  31212. if (bv) {
  31213. if (atMapIndent && bv.type !== 'block-seq') {
  31214. map.items.push({ start });
  31215. }
  31216. this.stack.push(bv);
  31217. return;
  31218. }
  31219. }
  31220. }
  31221. }
  31222. yield* this.pop();
  31223. yield* this.step();
  31224. }
  31225. *blockSequence(seq) {
  31226. const it = seq.items[seq.items.length - 1];
  31227. switch (this.type) {
  31228. case 'newline':
  31229. if (it.value) {
  31230. const end = 'end' in it.value ? it.value.end : undefined;
  31231. const last = Array.isArray(end) ? end[end.length - 1] : undefined;
  31232. if (last?.type === 'comment')
  31233. end?.push(this.sourceToken);
  31234. else
  31235. seq.items.push({ start: [this.sourceToken] });
  31236. }
  31237. else
  31238. it.start.push(this.sourceToken);
  31239. return;
  31240. case 'space':
  31241. case 'comment':
  31242. if (it.value)
  31243. seq.items.push({ start: [this.sourceToken] });
  31244. else {
  31245. if (this.atIndentedComment(it.start, seq.indent)) {
  31246. const prev = seq.items[seq.items.length - 2];
  31247. const end = prev?.value?.end;
  31248. if (Array.isArray(end)) {
  31249. Array.prototype.push.apply(end, it.start);
  31250. end.push(this.sourceToken);
  31251. seq.items.pop();
  31252. return;
  31253. }
  31254. }
  31255. it.start.push(this.sourceToken);
  31256. }
  31257. return;
  31258. case 'anchor':
  31259. case 'tag':
  31260. if (it.value || this.indent <= seq.indent)
  31261. break;
  31262. it.start.push(this.sourceToken);
  31263. return;
  31264. case 'seq-item-ind':
  31265. if (this.indent !== seq.indent)
  31266. break;
  31267. if (it.value || includesToken(it.start, 'seq-item-ind'))
  31268. seq.items.push({ start: [this.sourceToken] });
  31269. else
  31270. it.start.push(this.sourceToken);
  31271. return;
  31272. }
  31273. if (this.indent > seq.indent) {
  31274. const bv = this.startBlockValue(seq);
  31275. if (bv) {
  31276. this.stack.push(bv);
  31277. return;
  31278. }
  31279. }
  31280. yield* this.pop();
  31281. yield* this.step();
  31282. }
  31283. *flowCollection(fc) {
  31284. const it = fc.items[fc.items.length - 1];
  31285. if (this.type === 'flow-error-end') {
  31286. let top;
  31287. do {
  31288. yield* this.pop();
  31289. top = this.peek(1);
  31290. } while (top && top.type === 'flow-collection');
  31291. }
  31292. else if (fc.end.length === 0) {
  31293. switch (this.type) {
  31294. case 'comma':
  31295. case 'explicit-key-ind':
  31296. if (!it || it.sep)
  31297. fc.items.push({ start: [this.sourceToken] });
  31298. else
  31299. it.start.push(this.sourceToken);
  31300. return;
  31301. case 'map-value-ind':
  31302. if (!it || it.value)
  31303. fc.items.push({ start: [], key: null, sep: [this.sourceToken] });
  31304. else if (it.sep)
  31305. it.sep.push(this.sourceToken);
  31306. else
  31307. Object.assign(it, { key: null, sep: [this.sourceToken] });
  31308. return;
  31309. case 'space':
  31310. case 'comment':
  31311. case 'newline':
  31312. case 'anchor':
  31313. case 'tag':
  31314. if (!it || it.value)
  31315. fc.items.push({ start: [this.sourceToken] });
  31316. else if (it.sep)
  31317. it.sep.push(this.sourceToken);
  31318. else
  31319. it.start.push(this.sourceToken);
  31320. return;
  31321. case 'alias':
  31322. case 'scalar':
  31323. case 'single-quoted-scalar':
  31324. case 'double-quoted-scalar': {
  31325. const fs = this.flowScalar(this.type);
  31326. if (!it || it.value)
  31327. fc.items.push({ start: [], key: fs, sep: [] });
  31328. else if (it.sep)
  31329. this.stack.push(fs);
  31330. else
  31331. Object.assign(it, { key: fs, sep: [] });
  31332. return;
  31333. }
  31334. case 'flow-map-end':
  31335. case 'flow-seq-end':
  31336. fc.end.push(this.sourceToken);
  31337. return;
  31338. }
  31339. const bv = this.startBlockValue(fc);
  31340. /* istanbul ignore else should not happen */
  31341. if (bv)
  31342. this.stack.push(bv);
  31343. else {
  31344. yield* this.pop();
  31345. yield* this.step();
  31346. }
  31347. }
  31348. else {
  31349. const parent = this.peek(2);
  31350. if (parent.type === 'block-map' &&
  31351. ((this.type === 'map-value-ind' && parent.indent === fc.indent) ||
  31352. (this.type === 'newline' &&
  31353. !parent.items[parent.items.length - 1].sep))) {
  31354. yield* this.pop();
  31355. yield* this.step();
  31356. }
  31357. else if (this.type === 'map-value-ind' &&
  31358. parent.type !== 'flow-collection') {
  31359. const prev = getPrevProps(parent);
  31360. const start = getFirstKeyStartProps(prev);
  31361. fixFlowSeqItems(fc);
  31362. const sep = fc.end.splice(1, fc.end.length);
  31363. sep.push(this.sourceToken);
  31364. const map = {
  31365. type: 'block-map',
  31366. offset: fc.offset,
  31367. indent: fc.indent,
  31368. items: [{ start, key: fc, sep }]
  31369. };
  31370. this.onKeyLine = true;
  31371. this.stack[this.stack.length - 1] = map;
  31372. }
  31373. else {
  31374. yield* this.lineEnd(fc);
  31375. }
  31376. }
  31377. }
  31378. flowScalar(type) {
  31379. if (this.onNewLine) {
  31380. let nl = this.source.indexOf('\n') + 1;
  31381. while (nl !== 0) {
  31382. this.onNewLine(this.offset + nl);
  31383. nl = this.source.indexOf('\n', nl) + 1;
  31384. }
  31385. }
  31386. return {
  31387. type,
  31388. offset: this.offset,
  31389. indent: this.indent,
  31390. source: this.source
  31391. };
  31392. }
  31393. startBlockValue(parent) {
  31394. switch (this.type) {
  31395. case 'alias':
  31396. case 'scalar':
  31397. case 'single-quoted-scalar':
  31398. case 'double-quoted-scalar':
  31399. return this.flowScalar(this.type);
  31400. case 'block-scalar-header':
  31401. return {
  31402. type: 'block-scalar',
  31403. offset: this.offset,
  31404. indent: this.indent,
  31405. props: [this.sourceToken],
  31406. source: ''
  31407. };
  31408. case 'flow-map-start':
  31409. case 'flow-seq-start':
  31410. return {
  31411. type: 'flow-collection',
  31412. offset: this.offset,
  31413. indent: this.indent,
  31414. start: this.sourceToken,
  31415. items: [],
  31416. end: []
  31417. };
  31418. case 'seq-item-ind':
  31419. return {
  31420. type: 'block-seq',
  31421. offset: this.offset,
  31422. indent: this.indent,
  31423. items: [{ start: [this.sourceToken] }]
  31424. };
  31425. case 'explicit-key-ind': {
  31426. this.onKeyLine = true;
  31427. const prev = getPrevProps(parent);
  31428. const start = getFirstKeyStartProps(prev);
  31429. start.push(this.sourceToken);
  31430. return {
  31431. type: 'block-map',
  31432. offset: this.offset,
  31433. indent: this.indent,
  31434. items: [{ start, explicitKey: true }]
  31435. };
  31436. }
  31437. case 'map-value-ind': {
  31438. this.onKeyLine = true;
  31439. const prev = getPrevProps(parent);
  31440. const start = getFirstKeyStartProps(prev);
  31441. return {
  31442. type: 'block-map',
  31443. offset: this.offset,
  31444. indent: this.indent,
  31445. items: [{ start, key: null, sep: [this.sourceToken] }]
  31446. };
  31447. }
  31448. }
  31449. return null;
  31450. }
  31451. atIndentedComment(start, indent) {
  31452. if (this.type !== 'comment')
  31453. return false;
  31454. if (this.indent <= indent)
  31455. return false;
  31456. return start.every(st => st.type === 'newline' || st.type === 'space');
  31457. }
  31458. *documentEnd(docEnd) {
  31459. if (this.type !== 'doc-mode') {
  31460. if (docEnd.end)
  31461. docEnd.end.push(this.sourceToken);
  31462. else
  31463. docEnd.end = [this.sourceToken];
  31464. if (this.type === 'newline')
  31465. yield* this.pop();
  31466. }
  31467. }
  31468. *lineEnd(token) {
  31469. switch (this.type) {
  31470. case 'comma':
  31471. case 'doc-start':
  31472. case 'doc-end':
  31473. case 'flow-seq-end':
  31474. case 'flow-map-end':
  31475. case 'map-value-ind':
  31476. yield* this.pop();
  31477. yield* this.step();
  31478. break;
  31479. case 'newline':
  31480. this.onKeyLine = false;
  31481. // fallthrough
  31482. case 'space':
  31483. case 'comment':
  31484. default:
  31485. // all other values are errors
  31486. if (token.end)
  31487. token.end.push(this.sourceToken);
  31488. else
  31489. token.end = [this.sourceToken];
  31490. if (this.type === 'newline')
  31491. yield* this.pop();
  31492. }
  31493. }
  31494. }
  31495. function parseOptions(options) {
  31496. const prettyErrors = options.prettyErrors !== false;
  31497. const lineCounter = options.lineCounter || (prettyErrors && new LineCounter()) || null;
  31498. return { lineCounter, prettyErrors };
  31499. }
  31500. /**
  31501. * Parse the input as a stream of YAML documents.
  31502. *
  31503. * Documents should be separated from each other by `...` or `---` marker lines.
  31504. *
  31505. * @returns If an empty `docs` array is returned, it will be of type
  31506. * EmptyStream and contain additional stream information. In
  31507. * TypeScript, you should use `'empty' in docs` as a type guard for it.
  31508. */
  31509. function parseAllDocuments(source, options = {}) {
  31510. const { lineCounter, prettyErrors } = parseOptions(options);
  31511. const parser = new Parser(lineCounter?.addNewLine);
  31512. const composer = new Composer(options);
  31513. const docs = Array.from(composer.compose(parser.parse(source)));
  31514. if (prettyErrors && lineCounter)
  31515. for (const doc of docs) {
  31516. doc.errors.forEach(prettifyError(source, lineCounter));
  31517. doc.warnings.forEach(prettifyError(source, lineCounter));
  31518. }
  31519. if (docs.length > 0)
  31520. return docs;
  31521. return Object.assign([], { empty: true }, composer.streamInfo());
  31522. }
  31523. /** Parse an input string into a single YAML.Document */
  31524. function parseDocument(source, options = {}) {
  31525. const { lineCounter, prettyErrors } = parseOptions(options);
  31526. const parser = new Parser(lineCounter?.addNewLine);
  31527. const composer = new Composer(options);
  31528. // `doc` is always set by compose.end(true) at the very latest
  31529. let doc = null;
  31530. for (const _doc of composer.compose(parser.parse(source), true, source.length)) {
  31531. if (!doc)
  31532. doc = _doc;
  31533. else if (doc.options.logLevel !== 'silent') {
  31534. doc.errors.push(new YAMLParseError(_doc.range.slice(0, 2), 'MULTIPLE_DOCS', 'Source contains multiple documents; please use YAML.parseAllDocuments()'));
  31535. break;
  31536. }
  31537. }
  31538. if (prettyErrors && lineCounter) {
  31539. doc.errors.forEach(prettifyError(source, lineCounter));
  31540. doc.warnings.forEach(prettifyError(source, lineCounter));
  31541. }
  31542. return doc;
  31543. }
  31544. function parse$a(src, reviver, options) {
  31545. let _reviver = undefined;
  31546. if (typeof reviver === 'function') {
  31547. _reviver = reviver;
  31548. }
  31549. else if (options === undefined && reviver && typeof reviver === 'object') {
  31550. options = reviver;
  31551. }
  31552. const doc = parseDocument(src, options);
  31553. if (!doc)
  31554. return null;
  31555. doc.warnings.forEach(warning => warn(doc.options.logLevel, warning));
  31556. if (doc.errors.length > 0) {
  31557. if (doc.options.logLevel !== 'silent')
  31558. throw doc.errors[0];
  31559. else
  31560. doc.errors = [];
  31561. }
  31562. return doc.toJS(Object.assign({ reviver: _reviver }, options));
  31563. }
  31564. function stringify(value, replacer, options) {
  31565. let _replacer = null;
  31566. if (typeof replacer === 'function' || Array.isArray(replacer)) {
  31567. _replacer = replacer;
  31568. }
  31569. else if (options === undefined && replacer) {
  31570. options = replacer;
  31571. }
  31572. if (typeof options === 'string')
  31573. options = options.length;
  31574. if (typeof options === 'number') {
  31575. const indent = Math.round(options);
  31576. options = indent < 1 ? undefined : indent > 8 ? { indent: 8 } : { indent };
  31577. }
  31578. if (value === undefined) {
  31579. const { keepUndefined } = options ?? replacer ?? {};
  31580. if (!keepUndefined)
  31581. return undefined;
  31582. }
  31583. return new Document(value, _replacer, options).toString(options);
  31584. }
  31585. var YAML = {
  31586. __proto__: null,
  31587. Alias: Alias,
  31588. CST: cst,
  31589. Composer: Composer,
  31590. Document: Document,
  31591. Lexer: Lexer,
  31592. LineCounter: LineCounter,
  31593. Pair: Pair,
  31594. Parser: Parser,
  31595. Scalar: Scalar,
  31596. Schema: Schema,
  31597. YAMLError: YAMLError,
  31598. YAMLMap: YAMLMap,
  31599. YAMLParseError: YAMLParseError,
  31600. YAMLSeq: YAMLSeq,
  31601. YAMLWarning: YAMLWarning,
  31602. isAlias: isAlias,
  31603. isCollection: isCollection$1,
  31604. isDocument: isDocument,
  31605. isMap: isMap,
  31606. isNode: isNode$1,
  31607. isPair: isPair,
  31608. isScalar: isScalar$1,
  31609. isSeq: isSeq,
  31610. parse: parse$a,
  31611. parseAllDocuments: parseAllDocuments,
  31612. parseDocument: parseDocument,
  31613. stringify: stringify,
  31614. visit: visit$1,
  31615. visitAsync: visitAsync
  31616. };
  31617. // `export * as default from ...` fails on Webpack v4
  31618. // https://github.com/eemeli/yaml/issues/228
  31619. var browser$2 = {
  31620. __proto__: null,
  31621. Alias: Alias,
  31622. CST: cst,
  31623. Composer: Composer,
  31624. Document: Document,
  31625. Lexer: Lexer,
  31626. LineCounter: LineCounter,
  31627. Pair: Pair,
  31628. Parser: Parser,
  31629. Scalar: Scalar,
  31630. Schema: Schema,
  31631. YAMLError: YAMLError,
  31632. YAMLMap: YAMLMap,
  31633. YAMLParseError: YAMLParseError,
  31634. YAMLSeq: YAMLSeq,
  31635. YAMLWarning: YAMLWarning,
  31636. default: YAML,
  31637. isAlias: isAlias,
  31638. isCollection: isCollection$1,
  31639. isDocument: isDocument,
  31640. isMap: isMap,
  31641. isNode: isNode$1,
  31642. isPair: isPair,
  31643. isScalar: isScalar$1,
  31644. isSeq: isSeq,
  31645. parse: parse$a,
  31646. parseAllDocuments: parseAllDocuments,
  31647. parseDocument: parseDocument,
  31648. stringify: stringify,
  31649. visit: visit$1,
  31650. visitAsync: visitAsync
  31651. };
  31652. var require$$3 = /*@__PURE__*/getAugmentedNamespace(browser$2);
  31653. // eslint-disable-next-line n/no-deprecated-api
  31654. const { createRequire, createRequireFromPath } = require$$0$8;
  31655. function req$2 (name, rootFile) {
  31656. const create = createRequire || createRequireFromPath;
  31657. const require = create(rootFile);
  31658. return require(name)
  31659. }
  31660. var req_1 = req$2;
  31661. const req$1 = req_1;
  31662. /**
  31663. * Load Options
  31664. *
  31665. * @private
  31666. * @method options
  31667. *
  31668. * @param {Object} config PostCSS Config
  31669. *
  31670. * @return {Object} options PostCSS Options
  31671. */
  31672. const options = (config, file) => {
  31673. if (config.parser && typeof config.parser === 'string') {
  31674. try {
  31675. config.parser = req$1(config.parser, file);
  31676. } catch (err) {
  31677. throw new Error(`Loading PostCSS Parser failed: ${err.message}\n\n(@${file})`)
  31678. }
  31679. }
  31680. if (config.syntax && typeof config.syntax === 'string') {
  31681. try {
  31682. config.syntax = req$1(config.syntax, file);
  31683. } catch (err) {
  31684. throw new Error(`Loading PostCSS Syntax failed: ${err.message}\n\n(@${file})`)
  31685. }
  31686. }
  31687. if (config.stringifier && typeof config.stringifier === 'string') {
  31688. try {
  31689. config.stringifier = req$1(config.stringifier, file);
  31690. } catch (err) {
  31691. throw new Error(`Loading PostCSS Stringifier failed: ${err.message}\n\n(@${file})`)
  31692. }
  31693. }
  31694. if (config.plugins) {
  31695. delete config.plugins;
  31696. }
  31697. return config
  31698. };
  31699. var options_1 = options;
  31700. const req = req_1;
  31701. /**
  31702. * Plugin Loader
  31703. *
  31704. * @private
  31705. * @method load
  31706. *
  31707. * @param {String} plugin PostCSS Plugin Name
  31708. * @param {Object} options PostCSS Plugin Options
  31709. *
  31710. * @return {Function} PostCSS Plugin
  31711. */
  31712. const load = (plugin, options, file) => {
  31713. try {
  31714. if (
  31715. options === null ||
  31716. options === undefined ||
  31717. Object.keys(options).length === 0
  31718. ) {
  31719. return req(plugin, file)
  31720. } else {
  31721. return req(plugin, file)(options)
  31722. }
  31723. } catch (err) {
  31724. throw new Error(`Loading PostCSS Plugin failed: ${err.message}\n\n(@${file})`)
  31725. }
  31726. };
  31727. /**
  31728. * Load Plugins
  31729. *
  31730. * @private
  31731. * @method plugins
  31732. *
  31733. * @param {Object} config PostCSS Config Plugins
  31734. *
  31735. * @return {Array} plugins PostCSS Plugins
  31736. */
  31737. const plugins = (config, file) => {
  31738. let plugins = [];
  31739. if (Array.isArray(config.plugins)) {
  31740. plugins = config.plugins.filter(Boolean);
  31741. } else {
  31742. plugins = Object.keys(config.plugins)
  31743. .filter((plugin) => {
  31744. return config.plugins[plugin] !== false ? plugin : ''
  31745. })
  31746. .map((plugin) => {
  31747. return load(plugin, config.plugins[plugin], file)
  31748. });
  31749. }
  31750. if (plugins.length && plugins.length > 0) {
  31751. plugins.forEach((plugin, i) => {
  31752. if (plugin.default) {
  31753. plugin = plugin.default;
  31754. }
  31755. if (plugin.postcss === true) {
  31756. plugin = plugin();
  31757. } else if (plugin.postcss) {
  31758. plugin = plugin.postcss;
  31759. }
  31760. if (
  31761. // eslint-disable-next-line
  31762. !(
  31763. (typeof plugin === 'object' && Array.isArray(plugin.plugins)) ||
  31764. (typeof plugin === 'object' && plugin.postcssPlugin) ||
  31765. (typeof plugin === 'function')
  31766. )
  31767. ) {
  31768. throw new TypeError(`Invalid PostCSS Plugin found at: plugins[${i}]\n\n(@${file})`)
  31769. }
  31770. });
  31771. }
  31772. return plugins
  31773. };
  31774. var plugins_1 = plugins;
  31775. const resolve = require$$0$4.resolve;
  31776. const url$4 = require$$0$9;
  31777. const config$1 = src$2;
  31778. const yaml = require$$3;
  31779. const loadOptions = options_1;
  31780. const loadPlugins = plugins_1;
  31781. /* istanbul ignore next */
  31782. const interopRequireDefault = (obj) => obj && obj.__esModule ? obj : { default: obj };
  31783. /**
  31784. * Process the result from cosmiconfig
  31785. *
  31786. * @param {Object} ctx Config Context
  31787. * @param {Object} result Cosmiconfig result
  31788. *
  31789. * @return {Object} PostCSS Config
  31790. */
  31791. const processResult = (ctx, result) => {
  31792. const file = result.filepath || '';
  31793. let config = interopRequireDefault(result.config).default || {};
  31794. if (typeof config === 'function') {
  31795. config = config(ctx);
  31796. } else {
  31797. config = Object.assign({}, config, ctx);
  31798. }
  31799. if (!config.plugins) {
  31800. config.plugins = [];
  31801. }
  31802. return {
  31803. plugins: loadPlugins(config, file),
  31804. options: loadOptions(config, file),
  31805. file
  31806. }
  31807. };
  31808. /**
  31809. * Builds the Config Context
  31810. *
  31811. * @param {Object} ctx Config Context
  31812. *
  31813. * @return {Object} Config Context
  31814. */
  31815. const createContext = (ctx) => {
  31816. /**
  31817. * @type {Object}
  31818. *
  31819. * @prop {String} cwd=process.cwd() Config search start location
  31820. * @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`
  31821. */
  31822. ctx = Object.assign({
  31823. cwd: process.cwd(),
  31824. env: process.env.NODE_ENV
  31825. }, ctx);
  31826. if (!ctx.env) {
  31827. process.env.NODE_ENV = 'development';
  31828. }
  31829. return ctx
  31830. };
  31831. const importDefault = async filepath => {
  31832. const module = await import(url$4.pathToFileURL(filepath).href);
  31833. return module.default
  31834. };
  31835. const addTypeScriptLoader = (options = {}, loader) => {
  31836. const moduleName = 'postcss';
  31837. return {
  31838. ...options,
  31839. searchPlaces: [
  31840. ...(options.searchPlaces || []),
  31841. 'package.json',
  31842. `.${moduleName}rc`,
  31843. `.${moduleName}rc.json`,
  31844. `.${moduleName}rc.yaml`,
  31845. `.${moduleName}rc.yml`,
  31846. `.${moduleName}rc.ts`,
  31847. `.${moduleName}rc.cts`,
  31848. `.${moduleName}rc.js`,
  31849. `.${moduleName}rc.cjs`,
  31850. `.${moduleName}rc.mjs`,
  31851. `${moduleName}.config.ts`,
  31852. `${moduleName}.config.cts`,
  31853. `${moduleName}.config.js`,
  31854. `${moduleName}.config.cjs`,
  31855. `${moduleName}.config.mjs`
  31856. ],
  31857. loaders: {
  31858. ...options.loaders,
  31859. '.yaml': (filepath, content) => yaml.parse(content),
  31860. '.yml': (filepath, content) => yaml.parse(content),
  31861. '.js': importDefault,
  31862. '.cjs': importDefault,
  31863. '.mjs': importDefault,
  31864. '.ts': loader,
  31865. '.cts': loader
  31866. }
  31867. }
  31868. };
  31869. const withTypeScriptLoader = (rcFunc) => {
  31870. return (ctx, path, options) => {
  31871. return rcFunc(ctx, path, addTypeScriptLoader(options, (configFile) => {
  31872. let registerer = { enabled () {} };
  31873. try {
  31874. // Register TypeScript compiler instance
  31875. registerer = __require('ts-node').register({
  31876. // transpile to cjs even if compilerOptions.module in tsconfig is not Node16/NodeNext.
  31877. moduleTypes: { '**/*.cts': 'cjs' }
  31878. });
  31879. return __require(configFile)
  31880. } catch (err) {
  31881. if (err.code === 'MODULE_NOT_FOUND') {
  31882. throw new Error(
  31883. `'ts-node' is required for the TypeScript configuration files. Make sure it is installed\nError: ${err.message}`
  31884. )
  31885. }
  31886. throw err
  31887. } finally {
  31888. registerer.enabled(false);
  31889. }
  31890. }))
  31891. }
  31892. };
  31893. /**
  31894. * Load Config
  31895. *
  31896. * @method rc
  31897. *
  31898. * @param {Object} ctx Config Context
  31899. * @param {String} path Config Path
  31900. * @param {Object} options Config Options
  31901. *
  31902. * @return {Promise} config PostCSS Config
  31903. */
  31904. const rc = withTypeScriptLoader((ctx, path, options) => {
  31905. /**
  31906. * @type {Object} The full Config Context
  31907. */
  31908. ctx = createContext(ctx);
  31909. /**
  31910. * @type {String} `process.cwd()`
  31911. */
  31912. path = path ? resolve(path) : process.cwd();
  31913. return config$1.lilconfig('postcss', options)
  31914. .search(path)
  31915. .then((result) => {
  31916. if (!result) {
  31917. throw new Error(`No PostCSS Config found in: ${path}`)
  31918. }
  31919. return processResult(ctx, result)
  31920. })
  31921. });
  31922. /**
  31923. * Autoload Config for PostCSS
  31924. *
  31925. * @author Michael Ciniawsky @michael-ciniawsky <michael.ciniawsky@gmail.com>
  31926. * @license MIT
  31927. *
  31928. * @module postcss-load-config
  31929. * @version 2.1.0
  31930. *
  31931. * @requires comsiconfig
  31932. * @requires ./options
  31933. * @requires ./plugins
  31934. */
  31935. var src$1 = rc;
  31936. var postcssrc = /*@__PURE__*/getDefaultExportFromCjs(src$1);
  31937. // Copyright 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023 Simon Lydell
  31938. // License: MIT.
  31939. var HashbangComment, Identifier, JSXIdentifier, JSXPunctuator, JSXString, JSXText, KeywordsWithExpressionAfter, KeywordsWithNoLineTerminatorAfter, LineTerminatorSequence, MultiLineComment, Newline, NumericLiteral, Punctuator, RegularExpressionLiteral, SingleLineComment, StringLiteral, Template, TokensNotPrecedingObjectLiteral, TokensPrecedingExpression, WhiteSpace;
  31940. RegularExpressionLiteral = /\/(?![*\/])(?:\[(?:[^\]\\\n\r\u2028\u2029]+|\\.)*\]|[^\/\\\n\r\u2028\u2029]+|\\.)*(\/[$_\u200C\u200D\p{ID_Continue}]*|\\)?/yu;
  31941. Punctuator = /--|\+\+|=>|\.{3}|\??\.(?!\d)|(?:&&|\|\||\?\?|[+\-%&|^]|\*{1,2}|<{1,2}|>{1,3}|!=?|={1,2}|\/(?![\/*]))=?|[?~,:;[\](){}]/y;
  31942. Identifier = /(\x23?)(?=[$_\p{ID_Start}\\])(?:[$_\u200C\u200D\p{ID_Continue}]+|\\u[\da-fA-F]{4}|\\u\{[\da-fA-F]+\})+/yu;
  31943. StringLiteral = /(['"])(?:[^'"\\\n\r]+|(?!\1)['"]|\\(?:\r\n|[^]))*(\1)?/y;
  31944. 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;
  31945. Template = /[`}](?:[^`\\$]+|\\[^]|\$(?!\{))*(`|\$\{)?/y;
  31946. WhiteSpace = /[\t\v\f\ufeff\p{Zs}]+/yu;
  31947. LineTerminatorSequence = /\r?\n|[\r\u2028\u2029]/y;
  31948. MultiLineComment = /\/\*(?:[^*]+|\*(?!\/))*(\*\/)?/y;
  31949. SingleLineComment = /\/\/.*/y;
  31950. HashbangComment = /^#!.*/;
  31951. JSXPunctuator = /[<>.:={}]|\/(?![\/*])/y;
  31952. JSXIdentifier = /[$_\p{ID_Start}][$_\u200C\u200D\p{ID_Continue}-]*/yu;
  31953. JSXString = /(['"])(?:[^'"]+|(?!\1)['"])*(\1)?/y;
  31954. JSXText = /[^<>{}]+/y;
  31955. TokensPrecedingExpression = /^(?:[\/+-]|\.{3}|\?(?:InterpolationIn(?:JSX|Template)|NoLineTerminatorHere|NonExpressionParenEnd|UnaryIncDec))?$|[{}([,;<>=*%&|^!~?:]$/;
  31956. TokensNotPrecedingObjectLiteral = /^(?:=>|[;\]){}]|else|\?(?:NoLineTerminatorHere|NonExpressionParenEnd))?$/;
  31957. KeywordsWithExpressionAfter = /^(?:await|case|default|delete|do|else|instanceof|new|return|throw|typeof|void|yield)$/;
  31958. KeywordsWithNoLineTerminatorAfter = /^(?:return|throw|yield)$/;
  31959. Newline = RegExp(LineTerminatorSequence.source);
  31960. var jsTokens_1 = function*(input, {jsx = false} = {}) {
  31961. var braces, firstCodePoint, isExpression, lastIndex, lastSignificantToken, length, match, mode, nextLastIndex, nextLastSignificantToken, parenNesting, postfixIncDec, punctuator, stack;
  31962. ({length} = input);
  31963. lastIndex = 0;
  31964. lastSignificantToken = "";
  31965. stack = [
  31966. {tag: "JS"}
  31967. ];
  31968. braces = [];
  31969. parenNesting = 0;
  31970. postfixIncDec = false;
  31971. if (match = HashbangComment.exec(input)) {
  31972. yield ({
  31973. type: "HashbangComment",
  31974. value: match[0]
  31975. });
  31976. lastIndex = match[0].length;
  31977. }
  31978. while (lastIndex < length) {
  31979. mode = stack[stack.length - 1];
  31980. switch (mode.tag) {
  31981. case "JS":
  31982. case "JSNonExpressionParen":
  31983. case "InterpolationInTemplate":
  31984. case "InterpolationInJSX":
  31985. if (input[lastIndex] === "/" && (TokensPrecedingExpression.test(lastSignificantToken) || KeywordsWithExpressionAfter.test(lastSignificantToken))) {
  31986. RegularExpressionLiteral.lastIndex = lastIndex;
  31987. if (match = RegularExpressionLiteral.exec(input)) {
  31988. lastIndex = RegularExpressionLiteral.lastIndex;
  31989. lastSignificantToken = match[0];
  31990. postfixIncDec = true;
  31991. yield ({
  31992. type: "RegularExpressionLiteral",
  31993. value: match[0],
  31994. closed: match[1] !== void 0 && match[1] !== "\\"
  31995. });
  31996. continue;
  31997. }
  31998. }
  31999. Punctuator.lastIndex = lastIndex;
  32000. if (match = Punctuator.exec(input)) {
  32001. punctuator = match[0];
  32002. nextLastIndex = Punctuator.lastIndex;
  32003. nextLastSignificantToken = punctuator;
  32004. switch (punctuator) {
  32005. case "(":
  32006. if (lastSignificantToken === "?NonExpressionParenKeyword") {
  32007. stack.push({
  32008. tag: "JSNonExpressionParen",
  32009. nesting: parenNesting
  32010. });
  32011. }
  32012. parenNesting++;
  32013. postfixIncDec = false;
  32014. break;
  32015. case ")":
  32016. parenNesting--;
  32017. postfixIncDec = true;
  32018. if (mode.tag === "JSNonExpressionParen" && parenNesting === mode.nesting) {
  32019. stack.pop();
  32020. nextLastSignificantToken = "?NonExpressionParenEnd";
  32021. postfixIncDec = false;
  32022. }
  32023. break;
  32024. case "{":
  32025. Punctuator.lastIndex = 0;
  32026. isExpression = !TokensNotPrecedingObjectLiteral.test(lastSignificantToken) && (TokensPrecedingExpression.test(lastSignificantToken) || KeywordsWithExpressionAfter.test(lastSignificantToken));
  32027. braces.push(isExpression);
  32028. postfixIncDec = false;
  32029. break;
  32030. case "}":
  32031. switch (mode.tag) {
  32032. case "InterpolationInTemplate":
  32033. if (braces.length === mode.nesting) {
  32034. Template.lastIndex = lastIndex;
  32035. match = Template.exec(input);
  32036. lastIndex = Template.lastIndex;
  32037. lastSignificantToken = match[0];
  32038. if (match[1] === "${") {
  32039. lastSignificantToken = "?InterpolationInTemplate";
  32040. postfixIncDec = false;
  32041. yield ({
  32042. type: "TemplateMiddle",
  32043. value: match[0]
  32044. });
  32045. } else {
  32046. stack.pop();
  32047. postfixIncDec = true;
  32048. yield ({
  32049. type: "TemplateTail",
  32050. value: match[0],
  32051. closed: match[1] === "`"
  32052. });
  32053. }
  32054. continue;
  32055. }
  32056. break;
  32057. case "InterpolationInJSX":
  32058. if (braces.length === mode.nesting) {
  32059. stack.pop();
  32060. lastIndex += 1;
  32061. lastSignificantToken = "}";
  32062. yield ({
  32063. type: "JSXPunctuator",
  32064. value: "}"
  32065. });
  32066. continue;
  32067. }
  32068. }
  32069. postfixIncDec = braces.pop();
  32070. nextLastSignificantToken = postfixIncDec ? "?ExpressionBraceEnd" : "}";
  32071. break;
  32072. case "]":
  32073. postfixIncDec = true;
  32074. break;
  32075. case "++":
  32076. case "--":
  32077. nextLastSignificantToken = postfixIncDec ? "?PostfixIncDec" : "?UnaryIncDec";
  32078. break;
  32079. case "<":
  32080. if (jsx && (TokensPrecedingExpression.test(lastSignificantToken) || KeywordsWithExpressionAfter.test(lastSignificantToken))) {
  32081. stack.push({tag: "JSXTag"});
  32082. lastIndex += 1;
  32083. lastSignificantToken = "<";
  32084. yield ({
  32085. type: "JSXPunctuator",
  32086. value: punctuator
  32087. });
  32088. continue;
  32089. }
  32090. postfixIncDec = false;
  32091. break;
  32092. default:
  32093. postfixIncDec = false;
  32094. }
  32095. lastIndex = nextLastIndex;
  32096. lastSignificantToken = nextLastSignificantToken;
  32097. yield ({
  32098. type: "Punctuator",
  32099. value: punctuator
  32100. });
  32101. continue;
  32102. }
  32103. Identifier.lastIndex = lastIndex;
  32104. if (match = Identifier.exec(input)) {
  32105. lastIndex = Identifier.lastIndex;
  32106. nextLastSignificantToken = match[0];
  32107. switch (match[0]) {
  32108. case "for":
  32109. case "if":
  32110. case "while":
  32111. case "with":
  32112. if (lastSignificantToken !== "." && lastSignificantToken !== "?.") {
  32113. nextLastSignificantToken = "?NonExpressionParenKeyword";
  32114. }
  32115. }
  32116. lastSignificantToken = nextLastSignificantToken;
  32117. postfixIncDec = !KeywordsWithExpressionAfter.test(match[0]);
  32118. yield ({
  32119. type: match[1] === "#" ? "PrivateIdentifier" : "IdentifierName",
  32120. value: match[0]
  32121. });
  32122. continue;
  32123. }
  32124. StringLiteral.lastIndex = lastIndex;
  32125. if (match = StringLiteral.exec(input)) {
  32126. lastIndex = StringLiteral.lastIndex;
  32127. lastSignificantToken = match[0];
  32128. postfixIncDec = true;
  32129. yield ({
  32130. type: "StringLiteral",
  32131. value: match[0],
  32132. closed: match[2] !== void 0
  32133. });
  32134. continue;
  32135. }
  32136. NumericLiteral.lastIndex = lastIndex;
  32137. if (match = NumericLiteral.exec(input)) {
  32138. lastIndex = NumericLiteral.lastIndex;
  32139. lastSignificantToken = match[0];
  32140. postfixIncDec = true;
  32141. yield ({
  32142. type: "NumericLiteral",
  32143. value: match[0]
  32144. });
  32145. continue;
  32146. }
  32147. Template.lastIndex = lastIndex;
  32148. if (match = Template.exec(input)) {
  32149. lastIndex = Template.lastIndex;
  32150. lastSignificantToken = match[0];
  32151. if (match[1] === "${") {
  32152. lastSignificantToken = "?InterpolationInTemplate";
  32153. stack.push({
  32154. tag: "InterpolationInTemplate",
  32155. nesting: braces.length
  32156. });
  32157. postfixIncDec = false;
  32158. yield ({
  32159. type: "TemplateHead",
  32160. value: match[0]
  32161. });
  32162. } else {
  32163. postfixIncDec = true;
  32164. yield ({
  32165. type: "NoSubstitutionTemplate",
  32166. value: match[0],
  32167. closed: match[1] === "`"
  32168. });
  32169. }
  32170. continue;
  32171. }
  32172. break;
  32173. case "JSXTag":
  32174. case "JSXTagEnd":
  32175. JSXPunctuator.lastIndex = lastIndex;
  32176. if (match = JSXPunctuator.exec(input)) {
  32177. lastIndex = JSXPunctuator.lastIndex;
  32178. nextLastSignificantToken = match[0];
  32179. switch (match[0]) {
  32180. case "<":
  32181. stack.push({tag: "JSXTag"});
  32182. break;
  32183. case ">":
  32184. stack.pop();
  32185. if (lastSignificantToken === "/" || mode.tag === "JSXTagEnd") {
  32186. nextLastSignificantToken = "?JSX";
  32187. postfixIncDec = true;
  32188. } else {
  32189. stack.push({tag: "JSXChildren"});
  32190. }
  32191. break;
  32192. case "{":
  32193. stack.push({
  32194. tag: "InterpolationInJSX",
  32195. nesting: braces.length
  32196. });
  32197. nextLastSignificantToken = "?InterpolationInJSX";
  32198. postfixIncDec = false;
  32199. break;
  32200. case "/":
  32201. if (lastSignificantToken === "<") {
  32202. stack.pop();
  32203. if (stack[stack.length - 1].tag === "JSXChildren") {
  32204. stack.pop();
  32205. }
  32206. stack.push({tag: "JSXTagEnd"});
  32207. }
  32208. }
  32209. lastSignificantToken = nextLastSignificantToken;
  32210. yield ({
  32211. type: "JSXPunctuator",
  32212. value: match[0]
  32213. });
  32214. continue;
  32215. }
  32216. JSXIdentifier.lastIndex = lastIndex;
  32217. if (match = JSXIdentifier.exec(input)) {
  32218. lastIndex = JSXIdentifier.lastIndex;
  32219. lastSignificantToken = match[0];
  32220. yield ({
  32221. type: "JSXIdentifier",
  32222. value: match[0]
  32223. });
  32224. continue;
  32225. }
  32226. JSXString.lastIndex = lastIndex;
  32227. if (match = JSXString.exec(input)) {
  32228. lastIndex = JSXString.lastIndex;
  32229. lastSignificantToken = match[0];
  32230. yield ({
  32231. type: "JSXString",
  32232. value: match[0],
  32233. closed: match[2] !== void 0
  32234. });
  32235. continue;
  32236. }
  32237. break;
  32238. case "JSXChildren":
  32239. JSXText.lastIndex = lastIndex;
  32240. if (match = JSXText.exec(input)) {
  32241. lastIndex = JSXText.lastIndex;
  32242. lastSignificantToken = match[0];
  32243. yield ({
  32244. type: "JSXText",
  32245. value: match[0]
  32246. });
  32247. continue;
  32248. }
  32249. switch (input[lastIndex]) {
  32250. case "<":
  32251. stack.push({tag: "JSXTag"});
  32252. lastIndex++;
  32253. lastSignificantToken = "<";
  32254. yield ({
  32255. type: "JSXPunctuator",
  32256. value: "<"
  32257. });
  32258. continue;
  32259. case "{":
  32260. stack.push({
  32261. tag: "InterpolationInJSX",
  32262. nesting: braces.length
  32263. });
  32264. lastIndex++;
  32265. lastSignificantToken = "?InterpolationInJSX";
  32266. postfixIncDec = false;
  32267. yield ({
  32268. type: "JSXPunctuator",
  32269. value: "{"
  32270. });
  32271. continue;
  32272. }
  32273. }
  32274. WhiteSpace.lastIndex = lastIndex;
  32275. if (match = WhiteSpace.exec(input)) {
  32276. lastIndex = WhiteSpace.lastIndex;
  32277. yield ({
  32278. type: "WhiteSpace",
  32279. value: match[0]
  32280. });
  32281. continue;
  32282. }
  32283. LineTerminatorSequence.lastIndex = lastIndex;
  32284. if (match = LineTerminatorSequence.exec(input)) {
  32285. lastIndex = LineTerminatorSequence.lastIndex;
  32286. postfixIncDec = false;
  32287. if (KeywordsWithNoLineTerminatorAfter.test(lastSignificantToken)) {
  32288. lastSignificantToken = "?NoLineTerminatorHere";
  32289. }
  32290. yield ({
  32291. type: "LineTerminatorSequence",
  32292. value: match[0]
  32293. });
  32294. continue;
  32295. }
  32296. MultiLineComment.lastIndex = lastIndex;
  32297. if (match = MultiLineComment.exec(input)) {
  32298. lastIndex = MultiLineComment.lastIndex;
  32299. if (Newline.test(match[0])) {
  32300. postfixIncDec = false;
  32301. if (KeywordsWithNoLineTerminatorAfter.test(lastSignificantToken)) {
  32302. lastSignificantToken = "?NoLineTerminatorHere";
  32303. }
  32304. }
  32305. yield ({
  32306. type: "MultiLineComment",
  32307. value: match[0],
  32308. closed: match[1] !== void 0
  32309. });
  32310. continue;
  32311. }
  32312. SingleLineComment.lastIndex = lastIndex;
  32313. if (match = SingleLineComment.exec(input)) {
  32314. lastIndex = SingleLineComment.lastIndex;
  32315. postfixIncDec = false;
  32316. yield ({
  32317. type: "SingleLineComment",
  32318. value: match[0]
  32319. });
  32320. continue;
  32321. }
  32322. firstCodePoint = String.fromCodePoint(input.codePointAt(lastIndex));
  32323. lastIndex += firstCodePoint.length;
  32324. lastSignificantToken = firstCodePoint;
  32325. postfixIncDec = false;
  32326. yield ({
  32327. type: mode.tag.startsWith("JSX") ? "JSXInvalid" : "Invalid",
  32328. value: firstCodePoint
  32329. });
  32330. }
  32331. return void 0;
  32332. };
  32333. var jsTokens = /*@__PURE__*/getDefaultExportFromCjs(jsTokens_1);
  32334. function stripLiteralJsTokens(code, options) {
  32335. const FILL = " ";
  32336. const FILL_COMMENT = " ";
  32337. let result = "";
  32338. const tokens = [];
  32339. for (const token of jsTokens(code, { jsx: false })) {
  32340. tokens.push(token);
  32341. if (token.type === "SingleLineComment") {
  32342. result += FILL_COMMENT.repeat(token.value.length);
  32343. continue;
  32344. }
  32345. if (token.type === "MultiLineComment") {
  32346. result += token.value.replace(/[^\n]/g, FILL_COMMENT);
  32347. continue;
  32348. }
  32349. if (token.type === "StringLiteral") {
  32350. if (!token.closed) {
  32351. result += token.value;
  32352. continue;
  32353. }
  32354. const body = token.value.slice(1, -1);
  32355. {
  32356. result += token.value[0] + FILL.repeat(body.length) + token.value[token.value.length - 1];
  32357. continue;
  32358. }
  32359. }
  32360. if (token.type === "NoSubstitutionTemplate") {
  32361. const body = token.value.slice(1, -1);
  32362. {
  32363. result += `\`${body.replace(/[^\n]/g, FILL)}\``;
  32364. continue;
  32365. }
  32366. }
  32367. if (token.type === "RegularExpressionLiteral") {
  32368. const body = token.value;
  32369. {
  32370. result += body.replace(/\/(.*)\/(\w?)$/g, (_, $1, $2) => `/${FILL.repeat($1.length)}/${$2}`);
  32371. continue;
  32372. }
  32373. }
  32374. if (token.type === "TemplateHead") {
  32375. const body = token.value.slice(1, -2);
  32376. {
  32377. result += `\`${body.replace(/[^\n]/g, FILL)}\${`;
  32378. continue;
  32379. }
  32380. }
  32381. if (token.type === "TemplateTail") {
  32382. const body = token.value.slice(0, -2);
  32383. {
  32384. result += `}${body.replace(/[^\n]/g, FILL)}\``;
  32385. continue;
  32386. }
  32387. }
  32388. if (token.type === "TemplateMiddle") {
  32389. const body = token.value.slice(1, -2);
  32390. {
  32391. result += `}${body.replace(/[^\n]/g, FILL)}\${`;
  32392. continue;
  32393. }
  32394. }
  32395. result += token.value;
  32396. }
  32397. return {
  32398. result,
  32399. tokens
  32400. };
  32401. }
  32402. function stripLiteral(code, options) {
  32403. return stripLiteralDetailed(code).result;
  32404. }
  32405. function stripLiteralDetailed(code, options) {
  32406. return stripLiteralJsTokens(code);
  32407. }
  32408. var main$1 = {exports: {}};
  32409. var name = "dotenv";
  32410. var version$1 = "16.4.5";
  32411. var description = "Loads environment variables from .env file";
  32412. var main = "lib/main.js";
  32413. var types = "lib/main.d.ts";
  32414. var exports = {
  32415. ".": {
  32416. types: "./lib/main.d.ts",
  32417. require: "./lib/main.js",
  32418. "default": "./lib/main.js"
  32419. },
  32420. "./config": "./config.js",
  32421. "./config.js": "./config.js",
  32422. "./lib/env-options": "./lib/env-options.js",
  32423. "./lib/env-options.js": "./lib/env-options.js",
  32424. "./lib/cli-options": "./lib/cli-options.js",
  32425. "./lib/cli-options.js": "./lib/cli-options.js",
  32426. "./package.json": "./package.json"
  32427. };
  32428. var scripts = {
  32429. "dts-check": "tsc --project tests/types/tsconfig.json",
  32430. lint: "standard",
  32431. "lint-readme": "standard-markdown",
  32432. pretest: "npm run lint && npm run dts-check",
  32433. test: "tap tests/*.js --100 -Rspec",
  32434. "test:coverage": "tap --coverage-report=lcov",
  32435. prerelease: "npm test",
  32436. release: "standard-version"
  32437. };
  32438. var repository = {
  32439. type: "git",
  32440. url: "git://github.com/motdotla/dotenv.git"
  32441. };
  32442. var funding = "https://dotenvx.com";
  32443. var keywords = [
  32444. "dotenv",
  32445. "env",
  32446. ".env",
  32447. "environment",
  32448. "variables",
  32449. "config",
  32450. "settings"
  32451. ];
  32452. var readmeFilename = "README.md";
  32453. var license = "BSD-2-Clause";
  32454. var devDependencies = {
  32455. "@definitelytyped/dtslint": "^0.0.133",
  32456. "@types/node": "^18.11.3",
  32457. decache: "^4.6.1",
  32458. sinon: "^14.0.1",
  32459. standard: "^17.0.0",
  32460. "standard-markdown": "^7.1.0",
  32461. "standard-version": "^9.5.0",
  32462. tap: "^16.3.0",
  32463. tar: "^6.1.11",
  32464. typescript: "^4.8.4"
  32465. };
  32466. var engines = {
  32467. node: ">=12"
  32468. };
  32469. var browser$1 = {
  32470. fs: false
  32471. };
  32472. var require$$4 = {
  32473. name: name,
  32474. version: version$1,
  32475. description: description,
  32476. main: main,
  32477. types: types,
  32478. exports: exports,
  32479. scripts: scripts,
  32480. repository: repository,
  32481. funding: funding,
  32482. keywords: keywords,
  32483. readmeFilename: readmeFilename,
  32484. license: license,
  32485. devDependencies: devDependencies,
  32486. engines: engines,
  32487. browser: browser$1
  32488. };
  32489. const fs$9 = require$$0__default;
  32490. const path$9 = require$$0$4;
  32491. const os$2 = require$$2;
  32492. const crypto$1 = require$$3$1;
  32493. const packageJson = require$$4;
  32494. const version = packageJson.version;
  32495. const LINE = /(?:^|^)\s*(?:export\s+)?([\w.-]+)(?:\s*=\s*?|:\s+?)(\s*'(?:\\'|[^'])*'|\s*"(?:\\"|[^"])*"|\s*`(?:\\`|[^`])*`|[^#\r\n]+)?\s*(?:#.*)?(?:$|$)/mg;
  32496. // Parse src into an Object
  32497. function parse$9 (src) {
  32498. const obj = {};
  32499. // Convert buffer to string
  32500. let lines = src.toString();
  32501. // Convert line breaks to same format
  32502. lines = lines.replace(/\r\n?/mg, '\n');
  32503. let match;
  32504. while ((match = LINE.exec(lines)) != null) {
  32505. const key = match[1];
  32506. // Default undefined or null to empty string
  32507. let value = (match[2] || '');
  32508. // Remove whitespace
  32509. value = value.trim();
  32510. // Check if double quoted
  32511. const maybeQuote = value[0];
  32512. // Remove surrounding quotes
  32513. value = value.replace(/^(['"`])([\s\S]*)\1$/mg, '$2');
  32514. // Expand newlines if double quoted
  32515. if (maybeQuote === '"') {
  32516. value = value.replace(/\\n/g, '\n');
  32517. value = value.replace(/\\r/g, '\r');
  32518. }
  32519. // Add to object
  32520. obj[key] = value;
  32521. }
  32522. return obj
  32523. }
  32524. function _parseVault (options) {
  32525. const vaultPath = _vaultPath(options);
  32526. // Parse .env.vault
  32527. const result = DotenvModule.configDotenv({ path: vaultPath });
  32528. if (!result.parsed) {
  32529. const err = new Error(`MISSING_DATA: Cannot parse ${vaultPath} for an unknown reason`);
  32530. err.code = 'MISSING_DATA';
  32531. throw err
  32532. }
  32533. // handle scenario for comma separated keys - for use with key rotation
  32534. // example: DOTENV_KEY="dotenv://:key_1234@dotenvx.com/vault/.env.vault?environment=prod,dotenv://:key_7890@dotenvx.com/vault/.env.vault?environment=prod"
  32535. const keys = _dotenvKey(options).split(',');
  32536. const length = keys.length;
  32537. let decrypted;
  32538. for (let i = 0; i < length; i++) {
  32539. try {
  32540. // Get full key
  32541. const key = keys[i].trim();
  32542. // Get instructions for decrypt
  32543. const attrs = _instructions(result, key);
  32544. // Decrypt
  32545. decrypted = DotenvModule.decrypt(attrs.ciphertext, attrs.key);
  32546. break
  32547. } catch (error) {
  32548. // last key
  32549. if (i + 1 >= length) {
  32550. throw error
  32551. }
  32552. // try next key
  32553. }
  32554. }
  32555. // Parse decrypted .env string
  32556. return DotenvModule.parse(decrypted)
  32557. }
  32558. function _log (message) {
  32559. console.log(`[dotenv@${version}][INFO] ${message}`);
  32560. }
  32561. function _warn (message) {
  32562. console.log(`[dotenv@${version}][WARN] ${message}`);
  32563. }
  32564. function _debug (message) {
  32565. console.log(`[dotenv@${version}][DEBUG] ${message}`);
  32566. }
  32567. function _dotenvKey (options) {
  32568. // prioritize developer directly setting options.DOTENV_KEY
  32569. if (options && options.DOTENV_KEY && options.DOTENV_KEY.length > 0) {
  32570. return options.DOTENV_KEY
  32571. }
  32572. // secondary infra already contains a DOTENV_KEY environment variable
  32573. if (process.env.DOTENV_KEY && process.env.DOTENV_KEY.length > 0) {
  32574. return process.env.DOTENV_KEY
  32575. }
  32576. // fallback to empty string
  32577. return ''
  32578. }
  32579. function _instructions (result, dotenvKey) {
  32580. // Parse DOTENV_KEY. Format is a URI
  32581. let uri;
  32582. try {
  32583. uri = new URL(dotenvKey);
  32584. } catch (error) {
  32585. if (error.code === 'ERR_INVALID_URL') {
  32586. 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');
  32587. err.code = 'INVALID_DOTENV_KEY';
  32588. throw err
  32589. }
  32590. throw error
  32591. }
  32592. // Get decrypt key
  32593. const key = uri.password;
  32594. if (!key) {
  32595. const err = new Error('INVALID_DOTENV_KEY: Missing key part');
  32596. err.code = 'INVALID_DOTENV_KEY';
  32597. throw err
  32598. }
  32599. // Get environment
  32600. const environment = uri.searchParams.get('environment');
  32601. if (!environment) {
  32602. const err = new Error('INVALID_DOTENV_KEY: Missing environment part');
  32603. err.code = 'INVALID_DOTENV_KEY';
  32604. throw err
  32605. }
  32606. // Get ciphertext payload
  32607. const environmentKey = `DOTENV_VAULT_${environment.toUpperCase()}`;
  32608. const ciphertext = result.parsed[environmentKey]; // DOTENV_VAULT_PRODUCTION
  32609. if (!ciphertext) {
  32610. const err = new Error(`NOT_FOUND_DOTENV_ENVIRONMENT: Cannot locate environment ${environmentKey} in your .env.vault file.`);
  32611. err.code = 'NOT_FOUND_DOTENV_ENVIRONMENT';
  32612. throw err
  32613. }
  32614. return { ciphertext, key }
  32615. }
  32616. function _vaultPath (options) {
  32617. let possibleVaultPath = null;
  32618. if (options && options.path && options.path.length > 0) {
  32619. if (Array.isArray(options.path)) {
  32620. for (const filepath of options.path) {
  32621. if (fs$9.existsSync(filepath)) {
  32622. possibleVaultPath = filepath.endsWith('.vault') ? filepath : `${filepath}.vault`;
  32623. }
  32624. }
  32625. } else {
  32626. possibleVaultPath = options.path.endsWith('.vault') ? options.path : `${options.path}.vault`;
  32627. }
  32628. } else {
  32629. possibleVaultPath = path$9.resolve(process.cwd(), '.env.vault');
  32630. }
  32631. if (fs$9.existsSync(possibleVaultPath)) {
  32632. return possibleVaultPath
  32633. }
  32634. return null
  32635. }
  32636. function _resolveHome (envPath) {
  32637. return envPath[0] === '~' ? path$9.join(os$2.homedir(), envPath.slice(1)) : envPath
  32638. }
  32639. function _configVault (options) {
  32640. _log('Loading env from encrypted .env.vault');
  32641. const parsed = DotenvModule._parseVault(options);
  32642. let processEnv = process.env;
  32643. if (options && options.processEnv != null) {
  32644. processEnv = options.processEnv;
  32645. }
  32646. DotenvModule.populate(processEnv, parsed, options);
  32647. return { parsed }
  32648. }
  32649. function configDotenv (options) {
  32650. const dotenvPath = path$9.resolve(process.cwd(), '.env');
  32651. let encoding = 'utf8';
  32652. const debug = Boolean(options && options.debug);
  32653. if (options && options.encoding) {
  32654. encoding = options.encoding;
  32655. } else {
  32656. if (debug) {
  32657. _debug('No encoding is specified. UTF-8 is used by default');
  32658. }
  32659. }
  32660. let optionPaths = [dotenvPath]; // default, look for .env
  32661. if (options && options.path) {
  32662. if (!Array.isArray(options.path)) {
  32663. optionPaths = [_resolveHome(options.path)];
  32664. } else {
  32665. optionPaths = []; // reset default
  32666. for (const filepath of options.path) {
  32667. optionPaths.push(_resolveHome(filepath));
  32668. }
  32669. }
  32670. }
  32671. // Build the parsed data in a temporary object (because we need to return it). Once we have the final
  32672. // parsed data, we will combine it with process.env (or options.processEnv if provided).
  32673. let lastError;
  32674. const parsedAll = {};
  32675. for (const path of optionPaths) {
  32676. try {
  32677. // Specifying an encoding returns a string instead of a buffer
  32678. const parsed = DotenvModule.parse(fs$9.readFileSync(path, { encoding }));
  32679. DotenvModule.populate(parsedAll, parsed, options);
  32680. } catch (e) {
  32681. if (debug) {
  32682. _debug(`Failed to load ${path} ${e.message}`);
  32683. }
  32684. lastError = e;
  32685. }
  32686. }
  32687. let processEnv = process.env;
  32688. if (options && options.processEnv != null) {
  32689. processEnv = options.processEnv;
  32690. }
  32691. DotenvModule.populate(processEnv, parsedAll, options);
  32692. if (lastError) {
  32693. return { parsed: parsedAll, error: lastError }
  32694. } else {
  32695. return { parsed: parsedAll }
  32696. }
  32697. }
  32698. // Populates process.env from .env file
  32699. function config (options) {
  32700. // fallback to original dotenv if DOTENV_KEY is not set
  32701. if (_dotenvKey(options).length === 0) {
  32702. return DotenvModule.configDotenv(options)
  32703. }
  32704. const vaultPath = _vaultPath(options);
  32705. // dotenvKey exists but .env.vault file does not exist
  32706. if (!vaultPath) {
  32707. _warn(`You set DOTENV_KEY but you are missing a .env.vault file at ${vaultPath}. Did you forget to build it?`);
  32708. return DotenvModule.configDotenv(options)
  32709. }
  32710. return DotenvModule._configVault(options)
  32711. }
  32712. function decrypt (encrypted, keyStr) {
  32713. const key = Buffer.from(keyStr.slice(-64), 'hex');
  32714. let ciphertext = Buffer.from(encrypted, 'base64');
  32715. const nonce = ciphertext.subarray(0, 12);
  32716. const authTag = ciphertext.subarray(-16);
  32717. ciphertext = ciphertext.subarray(12, -16);
  32718. try {
  32719. const aesgcm = crypto$1.createDecipheriv('aes-256-gcm', key, nonce);
  32720. aesgcm.setAuthTag(authTag);
  32721. return `${aesgcm.update(ciphertext)}${aesgcm.final()}`
  32722. } catch (error) {
  32723. const isRange = error instanceof RangeError;
  32724. const invalidKeyLength = error.message === 'Invalid key length';
  32725. const decryptionFailed = error.message === 'Unsupported state or unable to authenticate data';
  32726. if (isRange || invalidKeyLength) {
  32727. const err = new Error('INVALID_DOTENV_KEY: It must be 64 characters long (or more)');
  32728. err.code = 'INVALID_DOTENV_KEY';
  32729. throw err
  32730. } else if (decryptionFailed) {
  32731. const err = new Error('DECRYPTION_FAILED: Please check your DOTENV_KEY');
  32732. err.code = 'DECRYPTION_FAILED';
  32733. throw err
  32734. } else {
  32735. throw error
  32736. }
  32737. }
  32738. }
  32739. // Populate process.env with parsed values
  32740. function populate (processEnv, parsed, options = {}) {
  32741. const debug = Boolean(options && options.debug);
  32742. const override = Boolean(options && options.override);
  32743. if (typeof parsed !== 'object') {
  32744. const err = new Error('OBJECT_REQUIRED: Please check the processEnv argument being passed to populate');
  32745. err.code = 'OBJECT_REQUIRED';
  32746. throw err
  32747. }
  32748. // Set process.env
  32749. for (const key of Object.keys(parsed)) {
  32750. if (Object.prototype.hasOwnProperty.call(processEnv, key)) {
  32751. if (override === true) {
  32752. processEnv[key] = parsed[key];
  32753. }
  32754. if (debug) {
  32755. if (override === true) {
  32756. _debug(`"${key}" is already defined and WAS overwritten`);
  32757. } else {
  32758. _debug(`"${key}" is already defined and was NOT overwritten`);
  32759. }
  32760. }
  32761. } else {
  32762. processEnv[key] = parsed[key];
  32763. }
  32764. }
  32765. }
  32766. const DotenvModule = {
  32767. configDotenv,
  32768. _configVault,
  32769. _parseVault,
  32770. config,
  32771. decrypt,
  32772. parse: parse$9,
  32773. populate
  32774. };
  32775. main$1.exports.configDotenv = DotenvModule.configDotenv;
  32776. main$1.exports._configVault = DotenvModule._configVault;
  32777. main$1.exports._parseVault = DotenvModule._parseVault;
  32778. main$1.exports.config = DotenvModule.config;
  32779. main$1.exports.decrypt = DotenvModule.decrypt;
  32780. var parse_1$1 = main$1.exports.parse = DotenvModule.parse;
  32781. main$1.exports.populate = DotenvModule.populate;
  32782. main$1.exports = DotenvModule;
  32783. // * /
  32784. // * (\\)? # is it escaped with a backslash?
  32785. // * (\$) # literal $
  32786. // * (?!\() # shouldnt be followed by parenthesis
  32787. // * (\{?) # first brace wrap opening
  32788. // * ([\w.]+) # key
  32789. // * (?::-((?:\$\{(?:\$\{(?:\$\{[^}]*\}|[^}])*}|[^}])*}|[^}])+))? # optional default nested 3 times
  32790. // * (\}?) # last brace warp closing
  32791. // * /xi
  32792. const DOTENV_SUBSTITUTION_REGEX = /(\\)?(\$)(?!\()(\{?)([\w.]+)(?::?-((?:\$\{(?:\$\{(?:\$\{[^}]*\}|[^}])*}|[^}])*}|[^}])+))?(\}?)/gi;
  32793. function _resolveEscapeSequences (value) {
  32794. return value.replace(/\\\$/g, '$')
  32795. }
  32796. function interpolate (value, processEnv, parsed) {
  32797. return value.replace(DOTENV_SUBSTITUTION_REGEX, (match, escaped, dollarSign, openBrace, key, defaultValue, closeBrace) => {
  32798. if (escaped === '\\') {
  32799. return match.slice(1)
  32800. } else {
  32801. if (processEnv[key]) {
  32802. if (processEnv[key] === parsed[key]) {
  32803. return processEnv[key]
  32804. } else {
  32805. // scenario: PASSWORD_EXPAND_NESTED=${PASSWORD_EXPAND}
  32806. return interpolate(processEnv[key], processEnv, parsed)
  32807. }
  32808. }
  32809. if (parsed[key]) {
  32810. // avoid recursion from EXPAND_SELF=$EXPAND_SELF
  32811. if (parsed[key] === value) {
  32812. return parsed[key]
  32813. } else {
  32814. return interpolate(parsed[key], processEnv, parsed)
  32815. }
  32816. }
  32817. if (defaultValue) {
  32818. if (defaultValue.startsWith('$')) {
  32819. return interpolate(defaultValue, processEnv, parsed)
  32820. } else {
  32821. return defaultValue
  32822. }
  32823. }
  32824. return ''
  32825. }
  32826. })
  32827. }
  32828. function expand (options) {
  32829. let processEnv = process.env;
  32830. if (options && options.processEnv != null) {
  32831. processEnv = options.processEnv;
  32832. }
  32833. for (const key in options.parsed) {
  32834. let value = options.parsed[key];
  32835. const inProcessEnv = Object.prototype.hasOwnProperty.call(processEnv, key);
  32836. if (inProcessEnv) {
  32837. if (processEnv[key] === options.parsed[key]) {
  32838. // assume was set to processEnv from the .env file if the values match and therefore interpolate
  32839. value = interpolate(value, processEnv, options.parsed);
  32840. } else {
  32841. // do not interpolate - assume processEnv had the intended value even if containing a $.
  32842. value = processEnv[key];
  32843. }
  32844. } else {
  32845. // not inProcessEnv so assume interpolation for this .env key
  32846. value = interpolate(value, processEnv, options.parsed);
  32847. }
  32848. options.parsed[key] = _resolveEscapeSequences(value);
  32849. }
  32850. for (const processKey in options.parsed) {
  32851. processEnv[processKey] = options.parsed[processKey];
  32852. }
  32853. return options
  32854. }
  32855. var expand_1 = expand;
  32856. function getEnvFilesForMode(mode, envDir) {
  32857. return [
  32858. /** default file */
  32859. `.env`,
  32860. /** local file */
  32861. `.env.local`,
  32862. /** mode file */
  32863. `.env.${mode}`,
  32864. /** mode local file */
  32865. `.env.${mode}.local`
  32866. ].map((file) => normalizePath$3(path$n.join(envDir, file)));
  32867. }
  32868. function loadEnv(mode, envDir, prefixes = "VITE_") {
  32869. if (mode === "local") {
  32870. throw new Error(
  32871. `"local" cannot be used as a mode name because it conflicts with the .local postfix for .env files.`
  32872. );
  32873. }
  32874. prefixes = arraify(prefixes);
  32875. const env = {};
  32876. const envFiles = getEnvFilesForMode(mode, envDir);
  32877. const parsed = Object.fromEntries(
  32878. envFiles.flatMap((filePath) => {
  32879. if (!tryStatSync(filePath)?.isFile()) return [];
  32880. return Object.entries(parse_1$1(fs__default.readFileSync(filePath)));
  32881. })
  32882. );
  32883. if (parsed.NODE_ENV && process.env.VITE_USER_NODE_ENV === void 0) {
  32884. process.env.VITE_USER_NODE_ENV = parsed.NODE_ENV;
  32885. }
  32886. if (parsed.BROWSER && process.env.BROWSER === void 0) {
  32887. process.env.BROWSER = parsed.BROWSER;
  32888. }
  32889. if (parsed.BROWSER_ARGS && process.env.BROWSER_ARGS === void 0) {
  32890. process.env.BROWSER_ARGS = parsed.BROWSER_ARGS;
  32891. }
  32892. const processEnv = { ...process.env };
  32893. expand_1({ parsed, processEnv });
  32894. for (const [key, value] of Object.entries(parsed)) {
  32895. if (prefixes.some((prefix) => key.startsWith(prefix))) {
  32896. env[key] = value;
  32897. }
  32898. }
  32899. for (const key in process.env) {
  32900. if (prefixes.some((prefix) => key.startsWith(prefix))) {
  32901. env[key] = process.env[key];
  32902. }
  32903. }
  32904. return env;
  32905. }
  32906. function resolveEnvPrefix({
  32907. envPrefix = "VITE_"
  32908. }) {
  32909. envPrefix = arraify(envPrefix);
  32910. if (envPrefix.includes("")) {
  32911. throw new Error(
  32912. `envPrefix option contains value '', which could lead unexpected exposure of sensitive information.`
  32913. );
  32914. }
  32915. return envPrefix;
  32916. }
  32917. const modulePreloadPolyfillId = "vite/modulepreload-polyfill";
  32918. const resolvedModulePreloadPolyfillId = "\0" + modulePreloadPolyfillId + ".js";
  32919. function modulePreloadPolyfillPlugin(config) {
  32920. const skip = config.command !== "build" || config.build.ssr;
  32921. let polyfillString;
  32922. return {
  32923. name: "vite:modulepreload-polyfill",
  32924. resolveId(id) {
  32925. if (id === modulePreloadPolyfillId) {
  32926. return resolvedModulePreloadPolyfillId;
  32927. }
  32928. },
  32929. load(id) {
  32930. if (id === resolvedModulePreloadPolyfillId) {
  32931. if (skip) {
  32932. return "";
  32933. }
  32934. if (!polyfillString) {
  32935. polyfillString = `${isModernFlag}&&(${polyfill.toString()}());`;
  32936. }
  32937. return { code: polyfillString, moduleSideEffects: true };
  32938. }
  32939. }
  32940. };
  32941. }
  32942. function polyfill() {
  32943. const relList = document.createElement("link").relList;
  32944. if (relList && relList.supports && relList.supports("modulepreload")) {
  32945. return;
  32946. }
  32947. for (const link of document.querySelectorAll('link[rel="modulepreload"]')) {
  32948. processPreload(link);
  32949. }
  32950. new MutationObserver((mutations) => {
  32951. for (const mutation of mutations) {
  32952. if (mutation.type !== "childList") {
  32953. continue;
  32954. }
  32955. for (const node of mutation.addedNodes) {
  32956. if (node.tagName === "LINK" && node.rel === "modulepreload")
  32957. processPreload(node);
  32958. }
  32959. }
  32960. }).observe(document, { childList: true, subtree: true });
  32961. function getFetchOpts(link) {
  32962. const fetchOpts = {};
  32963. if (link.integrity) fetchOpts.integrity = link.integrity;
  32964. if (link.referrerPolicy) fetchOpts.referrerPolicy = link.referrerPolicy;
  32965. if (link.crossOrigin === "use-credentials")
  32966. fetchOpts.credentials = "include";
  32967. else if (link.crossOrigin === "anonymous") fetchOpts.credentials = "omit";
  32968. else fetchOpts.credentials = "same-origin";
  32969. return fetchOpts;
  32970. }
  32971. function processPreload(link) {
  32972. if (link.ep)
  32973. return;
  32974. link.ep = true;
  32975. const fetchOpts = getFetchOpts(link);
  32976. fetch(link.href, fetchOpts);
  32977. }
  32978. }
  32979. const htmlProxyRE$1 = /\?html-proxy=?(?:&inline-css)?(?:&style-attr)?&index=(\d+)\.(?:js|css)$/;
  32980. const isHtmlProxyRE = /\?html-proxy\b/;
  32981. const inlineCSSRE$1 = /__VITE_INLINE_CSS__([a-z\d]{8}_\d+)__/g;
  32982. const inlineImportRE = /(?<!(?<!\.\.)\.)\bimport\s*\(("(?:[^"]|(?<=\\)")*"|'(?:[^']|(?<=\\)')*')\)/dg;
  32983. const htmlLangRE = /\.(?:html|htm)$/;
  32984. const spaceRe = /[\t\n\f\r ]/;
  32985. const importMapRE = /[ \t]*<script[^>]*type\s*=\s*(?:"importmap"|'importmap'|importmap)[^>]*>.*?<\/script>/is;
  32986. const moduleScriptRE = /[ \t]*<script[^>]*type\s*=\s*(?:"module"|'module'|module)[^>]*>/i;
  32987. const modulePreloadLinkRE = /[ \t]*<link[^>]*rel\s*=\s*(?:"modulepreload"|'modulepreload'|modulepreload)[\s\S]*?\/>/i;
  32988. const importMapAppendRE = new RegExp(
  32989. [moduleScriptRE, modulePreloadLinkRE].map((r) => r.source).join("|"),
  32990. "i"
  32991. );
  32992. const isHTMLProxy = (id) => isHtmlProxyRE.test(id);
  32993. const isHTMLRequest = (request) => htmlLangRE.test(request);
  32994. const htmlProxyMap = /* @__PURE__ */ new WeakMap();
  32995. const htmlProxyResult = /* @__PURE__ */ new Map();
  32996. function htmlInlineProxyPlugin(config) {
  32997. htmlProxyMap.set(config, /* @__PURE__ */ new Map());
  32998. return {
  32999. name: "vite:html-inline-proxy",
  33000. resolveId(id) {
  33001. if (isHTMLProxy(id)) {
  33002. return id;
  33003. }
  33004. },
  33005. load(id) {
  33006. const proxyMatch = htmlProxyRE$1.exec(id);
  33007. if (proxyMatch) {
  33008. const index = Number(proxyMatch[1]);
  33009. const file = cleanUrl(id);
  33010. const url = file.replace(normalizePath$3(config.root), "");
  33011. const result = htmlProxyMap.get(config).get(url)?.[index];
  33012. if (result) {
  33013. return result;
  33014. } else {
  33015. throw new Error(`No matching HTML proxy module found from ${id}`);
  33016. }
  33017. }
  33018. }
  33019. };
  33020. }
  33021. function addToHTMLProxyCache(config, filePath, index, result) {
  33022. if (!htmlProxyMap.get(config)) {
  33023. htmlProxyMap.set(config, /* @__PURE__ */ new Map());
  33024. }
  33025. if (!htmlProxyMap.get(config).get(filePath)) {
  33026. htmlProxyMap.get(config).set(filePath, []);
  33027. }
  33028. htmlProxyMap.get(config).get(filePath)[index] = result;
  33029. }
  33030. function addToHTMLProxyTransformResult(hash, code) {
  33031. htmlProxyResult.set(hash, code);
  33032. }
  33033. const assetAttrsConfig = {
  33034. link: ["href"],
  33035. video: ["src", "poster"],
  33036. source: ["src", "srcset"],
  33037. img: ["src", "srcset"],
  33038. image: ["xlink:href", "href"],
  33039. use: ["xlink:href", "href"]
  33040. };
  33041. const noInlineLinkRels = /* @__PURE__ */ new Set([
  33042. "icon",
  33043. "apple-touch-icon",
  33044. "apple-touch-startup-image",
  33045. "manifest"
  33046. ]);
  33047. const isAsyncScriptMap = /* @__PURE__ */ new WeakMap();
  33048. function nodeIsElement(node) {
  33049. return node.nodeName[0] !== "#";
  33050. }
  33051. function traverseNodes(node, visitor) {
  33052. if (node.nodeName === "template") {
  33053. node = node.content;
  33054. }
  33055. visitor(node);
  33056. if (nodeIsElement(node) || node.nodeName === "#document" || node.nodeName === "#document-fragment") {
  33057. node.childNodes.forEach((childNode) => traverseNodes(childNode, visitor));
  33058. }
  33059. }
  33060. async function traverseHtml(html, filePath, visitor) {
  33061. const { parse } = await import('./dep-D-7KCb9p.js');
  33062. const ast = parse(html, {
  33063. scriptingEnabled: false,
  33064. // parse inside <noscript>
  33065. sourceCodeLocationInfo: true,
  33066. onParseError: (e) => {
  33067. handleParseError(e, html, filePath);
  33068. }
  33069. });
  33070. traverseNodes(ast, visitor);
  33071. }
  33072. function getScriptInfo(node) {
  33073. let src;
  33074. let sourceCodeLocation;
  33075. let isModule = false;
  33076. let isAsync = false;
  33077. for (const p of node.attrs) {
  33078. if (p.prefix !== void 0) continue;
  33079. if (p.name === "src") {
  33080. if (!src) {
  33081. src = p;
  33082. sourceCodeLocation = node.sourceCodeLocation?.attrs["src"];
  33083. }
  33084. } else if (p.name === "type" && p.value && p.value === "module") {
  33085. isModule = true;
  33086. } else if (p.name === "async") {
  33087. isAsync = true;
  33088. }
  33089. }
  33090. return { src, sourceCodeLocation, isModule, isAsync };
  33091. }
  33092. const attrValueStartRE = /=\s*(.)/;
  33093. function overwriteAttrValue(s, sourceCodeLocation, newValue) {
  33094. const srcString = s.slice(
  33095. sourceCodeLocation.startOffset,
  33096. sourceCodeLocation.endOffset
  33097. );
  33098. const valueStart = attrValueStartRE.exec(srcString);
  33099. if (!valueStart) {
  33100. throw new Error(
  33101. `[vite:html] internal error, failed to overwrite attribute value`
  33102. );
  33103. }
  33104. const wrapOffset = valueStart[1] === '"' || valueStart[1] === "'" ? 1 : 0;
  33105. const valueOffset = valueStart.index + valueStart[0].length - 1;
  33106. s.update(
  33107. sourceCodeLocation.startOffset + valueOffset + wrapOffset,
  33108. sourceCodeLocation.endOffset - wrapOffset,
  33109. newValue
  33110. );
  33111. return s;
  33112. }
  33113. function formatParseError(parserError, id, html) {
  33114. const formattedError = {
  33115. code: parserError.code,
  33116. message: `parse5 error code ${parserError.code}`,
  33117. frame: generateCodeFrame(
  33118. html,
  33119. parserError.startOffset,
  33120. parserError.endOffset
  33121. ),
  33122. loc: {
  33123. file: id,
  33124. line: parserError.startLine,
  33125. column: parserError.startCol
  33126. }
  33127. };
  33128. return formattedError;
  33129. }
  33130. function handleParseError(parserError, html, filePath) {
  33131. switch (parserError.code) {
  33132. case "missing-doctype":
  33133. return;
  33134. case "abandoned-head-element-child":
  33135. return;
  33136. case "duplicate-attribute":
  33137. return;
  33138. case "non-void-html-element-start-tag-with-trailing-solidus":
  33139. return;
  33140. }
  33141. const parseError = formatParseError(parserError, filePath, html);
  33142. throw new Error(
  33143. `Unable to parse HTML; ${parseError.message}
  33144. at ${parseError.loc.file}:${parseError.loc.line}:${parseError.loc.column}
  33145. ${parseError.frame}`
  33146. );
  33147. }
  33148. function buildHtmlPlugin(config) {
  33149. const [preHooks, normalHooks, postHooks] = resolveHtmlTransforms(
  33150. config.plugins,
  33151. config.logger
  33152. );
  33153. preHooks.unshift(injectCspNonceMetaTagHook(config));
  33154. preHooks.unshift(preImportMapHook(config));
  33155. preHooks.push(htmlEnvHook(config));
  33156. postHooks.push(injectNonceAttributeTagHook(config));
  33157. postHooks.push(postImportMapHook());
  33158. const processedHtml = /* @__PURE__ */ new Map();
  33159. const isExcludedUrl = (url) => url[0] === "#" || isExternalUrl(url) || isDataUrl(url);
  33160. isAsyncScriptMap.set(config, /* @__PURE__ */ new Map());
  33161. return {
  33162. name: "vite:build-html",
  33163. async transform(html, id) {
  33164. if (id.endsWith(".html")) {
  33165. id = normalizePath$3(id);
  33166. const relativeUrlPath = normalizePath$3(path$n.relative(config.root, id));
  33167. const publicPath = `/${relativeUrlPath}`;
  33168. const publicBase = getBaseInHTML(relativeUrlPath, config);
  33169. const publicToRelative = (filename, importer) => publicBase + filename;
  33170. const toOutputPublicFilePath = (url) => toOutputFilePathInHtml(
  33171. url.slice(1),
  33172. "public",
  33173. relativeUrlPath,
  33174. "html",
  33175. config,
  33176. publicToRelative
  33177. );
  33178. const nodeStartWithLeadingWhitespace = (node) => {
  33179. const startOffset = node.sourceCodeLocation.startOffset;
  33180. if (startOffset === 0) return 0;
  33181. const lineStartOffset = startOffset - node.sourceCodeLocation.startCol;
  33182. let isLineEmpty = false;
  33183. try {
  33184. const line = s.slice(Math.max(0, lineStartOffset), startOffset);
  33185. isLineEmpty = !line.trim();
  33186. } catch {
  33187. }
  33188. return isLineEmpty ? lineStartOffset : startOffset;
  33189. };
  33190. html = await applyHtmlTransforms(html, preHooks, {
  33191. path: publicPath,
  33192. filename: id
  33193. });
  33194. let js = "";
  33195. const s = new MagicString(html);
  33196. const scriptUrls = [];
  33197. const styleUrls = [];
  33198. let inlineModuleIndex = -1;
  33199. let everyScriptIsAsync = true;
  33200. let someScriptsAreAsync = false;
  33201. let someScriptsAreDefer = false;
  33202. const assetUrlsPromises = [];
  33203. const namedOutput = Object.keys(
  33204. config?.build?.rollupOptions?.input || {}
  33205. );
  33206. const processAssetUrl = async (url, shouldInline) => {
  33207. if (url !== "" && // Empty attribute
  33208. !namedOutput.includes(url) && // Direct reference to named output
  33209. !namedOutput.includes(removeLeadingSlash(url))) {
  33210. try {
  33211. return await urlToBuiltUrl(url, id, config, this, shouldInline);
  33212. } catch (e) {
  33213. if (e.code !== "ENOENT") {
  33214. throw e;
  33215. }
  33216. }
  33217. }
  33218. return url;
  33219. };
  33220. await traverseHtml(html, id, (node) => {
  33221. if (!nodeIsElement(node)) {
  33222. return;
  33223. }
  33224. let shouldRemove = false;
  33225. if (node.nodeName === "script") {
  33226. const { src, sourceCodeLocation, isModule, isAsync } = getScriptInfo(node);
  33227. const url = src && src.value;
  33228. const isPublicFile = !!(url && checkPublicFile(url, config));
  33229. if (isPublicFile) {
  33230. overwriteAttrValue(
  33231. s,
  33232. sourceCodeLocation,
  33233. partialEncodeURIPath(toOutputPublicFilePath(url))
  33234. );
  33235. }
  33236. if (isModule) {
  33237. inlineModuleIndex++;
  33238. if (url && !isExcludedUrl(url) && !isPublicFile) {
  33239. js += `
  33240. import ${JSON.stringify(url)}`;
  33241. shouldRemove = true;
  33242. } else if (node.childNodes.length) {
  33243. const scriptNode = node.childNodes.pop();
  33244. const contents = scriptNode.value;
  33245. const filePath = id.replace(normalizePath$3(config.root), "");
  33246. addToHTMLProxyCache(config, filePath, inlineModuleIndex, {
  33247. code: contents
  33248. });
  33249. js += `
  33250. import "${id}?html-proxy&index=${inlineModuleIndex}.js"`;
  33251. shouldRemove = true;
  33252. }
  33253. everyScriptIsAsync &&= isAsync;
  33254. someScriptsAreAsync ||= isAsync;
  33255. someScriptsAreDefer ||= !isAsync;
  33256. } else if (url && !isPublicFile) {
  33257. if (!isExcludedUrl(url)) {
  33258. config.logger.warn(
  33259. `<script src="${url}"> in "${publicPath}" can't be bundled without type="module" attribute`
  33260. );
  33261. }
  33262. } else if (node.childNodes.length) {
  33263. const scriptNode = node.childNodes.pop();
  33264. scriptUrls.push(
  33265. ...extractImportExpressionFromClassicScript(scriptNode)
  33266. );
  33267. }
  33268. }
  33269. const assetAttrs = assetAttrsConfig[node.nodeName];
  33270. if (assetAttrs) {
  33271. for (const p of node.attrs) {
  33272. const attrKey = getAttrKey(p);
  33273. if (p.value && assetAttrs.includes(attrKey)) {
  33274. if (attrKey === "srcset") {
  33275. assetUrlsPromises.push(
  33276. (async () => {
  33277. const processedEncodedUrl = await processSrcSet(
  33278. p.value,
  33279. async ({ url }) => {
  33280. const decodedUrl = decodeURI(url);
  33281. if (!isExcludedUrl(decodedUrl)) {
  33282. const result = await processAssetUrl(url);
  33283. return result !== decodedUrl ? encodeURIPath(result) : url;
  33284. }
  33285. return url;
  33286. }
  33287. );
  33288. if (processedEncodedUrl !== p.value) {
  33289. overwriteAttrValue(
  33290. s,
  33291. getAttrSourceCodeLocation(node, attrKey),
  33292. processedEncodedUrl
  33293. );
  33294. }
  33295. })()
  33296. );
  33297. } else {
  33298. const url = decodeURI(p.value);
  33299. if (checkPublicFile(url, config)) {
  33300. overwriteAttrValue(
  33301. s,
  33302. getAttrSourceCodeLocation(node, attrKey),
  33303. partialEncodeURIPath(toOutputPublicFilePath(url))
  33304. );
  33305. } else if (!isExcludedUrl(url)) {
  33306. if (node.nodeName === "link" && isCSSRequest(url) && // should not be converted if following attributes are present (#6748)
  33307. !node.attrs.some(
  33308. (p2) => p2.prefix === void 0 && (p2.name === "media" || p2.name === "disabled")
  33309. )) {
  33310. const importExpression = `
  33311. import ${JSON.stringify(url)}`;
  33312. styleUrls.push({
  33313. url,
  33314. start: nodeStartWithLeadingWhitespace(node),
  33315. end: node.sourceCodeLocation.endOffset
  33316. });
  33317. js += importExpression;
  33318. } else {
  33319. const isNoInlineLink = node.nodeName === "link" && node.attrs.some(
  33320. (p2) => p2.name === "rel" && parseRelAttr(p2.value).some(
  33321. (v) => noInlineLinkRels.has(v)
  33322. )
  33323. );
  33324. const shouldInline = isNoInlineLink ? false : void 0;
  33325. assetUrlsPromises.push(
  33326. (async () => {
  33327. const processedUrl = await processAssetUrl(
  33328. url,
  33329. shouldInline
  33330. );
  33331. if (processedUrl !== url) {
  33332. overwriteAttrValue(
  33333. s,
  33334. getAttrSourceCodeLocation(node, attrKey),
  33335. partialEncodeURIPath(processedUrl)
  33336. );
  33337. }
  33338. })()
  33339. );
  33340. }
  33341. }
  33342. }
  33343. }
  33344. }
  33345. }
  33346. const inlineStyle = findNeedTransformStyleAttribute(node);
  33347. if (inlineStyle) {
  33348. inlineModuleIndex++;
  33349. const code = inlineStyle.attr.value;
  33350. const filePath = id.replace(normalizePath$3(config.root), "");
  33351. addToHTMLProxyCache(config, filePath, inlineModuleIndex, { code });
  33352. js += `
  33353. import "${id}?html-proxy&inline-css&style-attr&index=${inlineModuleIndex}.css"`;
  33354. const hash = getHash(cleanUrl(id));
  33355. overwriteAttrValue(
  33356. s,
  33357. inlineStyle.location,
  33358. `__VITE_INLINE_CSS__${hash}_${inlineModuleIndex}__`
  33359. );
  33360. }
  33361. if (node.nodeName === "style" && node.childNodes.length) {
  33362. const styleNode = node.childNodes.pop();
  33363. const filePath = id.replace(normalizePath$3(config.root), "");
  33364. inlineModuleIndex++;
  33365. addToHTMLProxyCache(config, filePath, inlineModuleIndex, {
  33366. code: styleNode.value
  33367. });
  33368. js += `
  33369. import "${id}?html-proxy&inline-css&index=${inlineModuleIndex}.css"`;
  33370. const hash = getHash(cleanUrl(id));
  33371. s.update(
  33372. styleNode.sourceCodeLocation.startOffset,
  33373. styleNode.sourceCodeLocation.endOffset,
  33374. `__VITE_INLINE_CSS__${hash}_${inlineModuleIndex}__`
  33375. );
  33376. }
  33377. if (shouldRemove) {
  33378. s.remove(
  33379. nodeStartWithLeadingWhitespace(node),
  33380. node.sourceCodeLocation.endOffset
  33381. );
  33382. }
  33383. });
  33384. isAsyncScriptMap.get(config).set(id, everyScriptIsAsync);
  33385. if (someScriptsAreAsync && someScriptsAreDefer) {
  33386. config.logger.warn(
  33387. `
  33388. 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.`
  33389. );
  33390. }
  33391. await Promise.all(assetUrlsPromises);
  33392. for (const { start, end, url } of scriptUrls) {
  33393. if (checkPublicFile(url, config)) {
  33394. s.update(
  33395. start,
  33396. end,
  33397. partialEncodeURIPath(toOutputPublicFilePath(url))
  33398. );
  33399. } else if (!isExcludedUrl(url)) {
  33400. s.update(
  33401. start,
  33402. end,
  33403. partialEncodeURIPath(await urlToBuiltUrl(url, id, config, this))
  33404. );
  33405. }
  33406. }
  33407. const resolvedStyleUrls = await Promise.all(
  33408. styleUrls.map(async (styleUrl) => ({
  33409. ...styleUrl,
  33410. resolved: await this.resolve(styleUrl.url, id)
  33411. }))
  33412. );
  33413. for (const { start, end, url, resolved } of resolvedStyleUrls) {
  33414. if (resolved == null) {
  33415. config.logger.warnOnce(
  33416. `
  33417. ${url} doesn't exist at build time, it will remain unchanged to be resolved at runtime`
  33418. );
  33419. const importExpression = `
  33420. import ${JSON.stringify(url)}`;
  33421. js = js.replace(importExpression, "");
  33422. } else {
  33423. s.remove(start, end);
  33424. }
  33425. }
  33426. processedHtml.set(id, s.toString());
  33427. const { modulePreload } = config.build;
  33428. if (modulePreload !== false && modulePreload.polyfill && (someScriptsAreAsync || someScriptsAreDefer)) {
  33429. js = `import "${modulePreloadPolyfillId}";
  33430. ${js}`;
  33431. }
  33432. return { code: js, moduleSideEffects: "no-treeshake" };
  33433. }
  33434. },
  33435. async generateBundle(options, bundle) {
  33436. const analyzedChunk = /* @__PURE__ */ new Map();
  33437. const inlineEntryChunk = /* @__PURE__ */ new Set();
  33438. const getImportedChunks = (chunk, seen = /* @__PURE__ */ new Set()) => {
  33439. const chunks = [];
  33440. chunk.imports.forEach((file) => {
  33441. const importee = bundle[file];
  33442. if (importee?.type === "chunk" && !seen.has(file)) {
  33443. seen.add(file);
  33444. chunks.push(...getImportedChunks(importee, seen));
  33445. chunks.push(importee);
  33446. }
  33447. });
  33448. return chunks;
  33449. };
  33450. const toScriptTag = (chunk, toOutputPath, isAsync) => ({
  33451. tag: "script",
  33452. attrs: {
  33453. ...isAsync ? { async: true } : {},
  33454. type: "module",
  33455. // crossorigin must be set not only for serving assets in a different origin
  33456. // but also to make it possible to preload the script using `<link rel="preload">`.
  33457. // `<script type="module">` used to fetch the script with credential mode `omit`,
  33458. // however `crossorigin` attribute cannot specify that value.
  33459. // https://developer.chrome.com/blog/modulepreload/#ok-so-why-doesnt-link-relpreload-work-for-modules:~:text=For%20%3Cscript%3E,of%20other%20modules.
  33460. // 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
  33461. crossorigin: true,
  33462. src: toOutputPath(chunk.fileName)
  33463. }
  33464. });
  33465. const toPreloadTag = (filename, toOutputPath) => ({
  33466. tag: "link",
  33467. attrs: {
  33468. rel: "modulepreload",
  33469. crossorigin: true,
  33470. href: toOutputPath(filename)
  33471. }
  33472. });
  33473. const getCssTagsForChunk = (chunk, toOutputPath, seen = /* @__PURE__ */ new Set()) => {
  33474. const tags = [];
  33475. if (!analyzedChunk.has(chunk)) {
  33476. analyzedChunk.set(chunk, 1);
  33477. chunk.imports.forEach((file) => {
  33478. const importee = bundle[file];
  33479. if (importee?.type === "chunk") {
  33480. tags.push(...getCssTagsForChunk(importee, toOutputPath, seen));
  33481. }
  33482. });
  33483. }
  33484. chunk.viteMetadata.importedCss.forEach((file) => {
  33485. if (!seen.has(file)) {
  33486. seen.add(file);
  33487. tags.push({
  33488. tag: "link",
  33489. attrs: {
  33490. rel: "stylesheet",
  33491. crossorigin: true,
  33492. href: toOutputPath(file)
  33493. }
  33494. });
  33495. }
  33496. });
  33497. return tags;
  33498. };
  33499. for (const [normalizedId, html] of processedHtml) {
  33500. const relativeUrlPath = normalizePath$3(
  33501. path$n.relative(config.root, normalizedId)
  33502. );
  33503. const assetsBase = getBaseInHTML(relativeUrlPath, config);
  33504. const toOutputFilePath = (filename, type) => {
  33505. if (isExternalUrl(filename)) {
  33506. return filename;
  33507. } else {
  33508. return toOutputFilePathInHtml(
  33509. filename,
  33510. type,
  33511. relativeUrlPath,
  33512. "html",
  33513. config,
  33514. (filename2, importer) => assetsBase + filename2
  33515. );
  33516. }
  33517. };
  33518. const toOutputAssetFilePath = (filename) => toOutputFilePath(filename, "asset");
  33519. const toOutputPublicAssetFilePath = (filename) => toOutputFilePath(filename, "public");
  33520. const isAsync = isAsyncScriptMap.get(config).get(normalizedId);
  33521. let result = html;
  33522. const chunk = Object.values(bundle).find(
  33523. (chunk2) => chunk2.type === "chunk" && chunk2.isEntry && chunk2.facadeModuleId && normalizePath$3(chunk2.facadeModuleId) === normalizedId
  33524. );
  33525. let canInlineEntry = false;
  33526. if (chunk) {
  33527. if (options.format === "es" && isEntirelyImport(chunk.code)) {
  33528. canInlineEntry = true;
  33529. }
  33530. const imports = getImportedChunks(chunk);
  33531. let assetTags;
  33532. if (canInlineEntry) {
  33533. assetTags = imports.map(
  33534. (chunk2) => toScriptTag(chunk2, toOutputAssetFilePath, isAsync)
  33535. );
  33536. } else {
  33537. assetTags = [toScriptTag(chunk, toOutputAssetFilePath, isAsync)];
  33538. const { modulePreload } = config.build;
  33539. if (modulePreload !== false) {
  33540. const resolveDependencies = typeof modulePreload === "object" && modulePreload.resolveDependencies;
  33541. const importsFileNames = imports.map((chunk2) => chunk2.fileName);
  33542. const resolvedDeps = resolveDependencies ? resolveDependencies(chunk.fileName, importsFileNames, {
  33543. hostId: relativeUrlPath,
  33544. hostType: "html"
  33545. }) : importsFileNames;
  33546. assetTags.push(
  33547. ...resolvedDeps.map(
  33548. (i) => toPreloadTag(i, toOutputAssetFilePath)
  33549. )
  33550. );
  33551. }
  33552. }
  33553. assetTags.push(...getCssTagsForChunk(chunk, toOutputAssetFilePath));
  33554. result = injectToHead(result, assetTags);
  33555. }
  33556. if (!config.build.cssCodeSplit) {
  33557. const cssChunk = Object.values(bundle).find(
  33558. (chunk2) => chunk2.type === "asset" && chunk2.name === "style.css"
  33559. );
  33560. if (cssChunk) {
  33561. result = injectToHead(result, [
  33562. {
  33563. tag: "link",
  33564. attrs: {
  33565. rel: "stylesheet",
  33566. crossorigin: true,
  33567. href: toOutputAssetFilePath(cssChunk.fileName)
  33568. }
  33569. }
  33570. ]);
  33571. }
  33572. }
  33573. let match;
  33574. let s;
  33575. inlineCSSRE$1.lastIndex = 0;
  33576. while (match = inlineCSSRE$1.exec(result)) {
  33577. s ||= new MagicString(result);
  33578. const { 0: full, 1: scopedName } = match;
  33579. const cssTransformedCode = htmlProxyResult.get(scopedName);
  33580. s.update(match.index, match.index + full.length, cssTransformedCode);
  33581. }
  33582. if (s) {
  33583. result = s.toString();
  33584. }
  33585. result = await applyHtmlTransforms(
  33586. result,
  33587. [...normalHooks, ...postHooks],
  33588. {
  33589. path: "/" + relativeUrlPath,
  33590. filename: normalizedId,
  33591. bundle,
  33592. chunk
  33593. }
  33594. );
  33595. result = result.replace(assetUrlRE, (_, fileHash, postfix = "") => {
  33596. const file = this.getFileName(fileHash);
  33597. if (chunk) {
  33598. chunk.viteMetadata.importedAssets.add(cleanUrl(file));
  33599. }
  33600. return encodeURIPath(toOutputAssetFilePath(file)) + postfix;
  33601. });
  33602. result = result.replace(publicAssetUrlRE, (_, fileHash) => {
  33603. const publicAssetPath = toOutputPublicAssetFilePath(
  33604. getPublicAssetFilename(fileHash, config)
  33605. );
  33606. return encodeURIPath(
  33607. urlCanParse(publicAssetPath) ? publicAssetPath : normalizePath$3(publicAssetPath)
  33608. );
  33609. });
  33610. if (chunk && canInlineEntry) {
  33611. inlineEntryChunk.add(chunk.fileName);
  33612. }
  33613. const shortEmitName = normalizePath$3(
  33614. path$n.relative(config.root, normalizedId)
  33615. );
  33616. this.emitFile({
  33617. type: "asset",
  33618. originalFileName: normalizedId,
  33619. fileName: shortEmitName,
  33620. source: result
  33621. });
  33622. }
  33623. for (const fileName of inlineEntryChunk) {
  33624. delete bundle[fileName];
  33625. }
  33626. }
  33627. };
  33628. }
  33629. function parseRelAttr(attr) {
  33630. return attr.split(spaceRe).map((v) => v.toLowerCase());
  33631. }
  33632. function findNeedTransformStyleAttribute(node) {
  33633. const attr = node.attrs.find(
  33634. (prop) => prop.prefix === void 0 && prop.name === "style" && // only url(...) or image-set(...) in css need to emit file
  33635. (prop.value.includes("url(") || prop.value.includes("image-set("))
  33636. );
  33637. if (!attr) return void 0;
  33638. const location = node.sourceCodeLocation?.attrs?.["style"];
  33639. return { attr, location };
  33640. }
  33641. function extractImportExpressionFromClassicScript(scriptTextNode) {
  33642. const startOffset = scriptTextNode.sourceCodeLocation.startOffset;
  33643. const cleanCode = stripLiteral(scriptTextNode.value);
  33644. const scriptUrls = [];
  33645. let match;
  33646. inlineImportRE.lastIndex = 0;
  33647. while (match = inlineImportRE.exec(cleanCode)) {
  33648. const [, [urlStart, urlEnd]] = match.indices;
  33649. const start = urlStart + 1;
  33650. const end = urlEnd - 1;
  33651. scriptUrls.push({
  33652. start: start + startOffset,
  33653. end: end + startOffset,
  33654. url: scriptTextNode.value.slice(start, end)
  33655. });
  33656. }
  33657. return scriptUrls;
  33658. }
  33659. function preImportMapHook(config) {
  33660. return (html, ctx) => {
  33661. const importMapIndex = html.search(importMapRE);
  33662. if (importMapIndex < 0) return;
  33663. const importMapAppendIndex = html.search(importMapAppendRE);
  33664. if (importMapAppendIndex < 0) return;
  33665. if (importMapAppendIndex < importMapIndex) {
  33666. const relativeHtml = normalizePath$3(
  33667. path$n.relative(config.root, ctx.filename)
  33668. );
  33669. config.logger.warnOnce(
  33670. colors$1.yellow(
  33671. colors$1.bold(
  33672. `(!) <script type="importmap"> should come before <script type="module"> and <link rel="modulepreload"> in /${relativeHtml}`
  33673. )
  33674. )
  33675. );
  33676. }
  33677. };
  33678. }
  33679. function postImportMapHook() {
  33680. return (html) => {
  33681. if (!importMapAppendRE.test(html)) return;
  33682. let importMap;
  33683. html = html.replace(importMapRE, (match) => {
  33684. importMap = match;
  33685. return "";
  33686. });
  33687. if (importMap) {
  33688. html = html.replace(
  33689. importMapAppendRE,
  33690. (match) => `${importMap}
  33691. ${match}`
  33692. );
  33693. }
  33694. return html;
  33695. };
  33696. }
  33697. function injectCspNonceMetaTagHook(config) {
  33698. return () => {
  33699. if (!config.html?.cspNonce) return;
  33700. return [
  33701. {
  33702. tag: "meta",
  33703. injectTo: "head",
  33704. // use nonce attribute so that it's hidden
  33705. // https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/nonce#accessing_nonces_and_nonce_hiding
  33706. attrs: { property: "csp-nonce", nonce: config.html.cspNonce }
  33707. }
  33708. ];
  33709. };
  33710. }
  33711. function htmlEnvHook(config) {
  33712. const pattern = /%(\S+?)%/g;
  33713. const envPrefix = resolveEnvPrefix({ envPrefix: config.envPrefix });
  33714. const env = { ...config.env };
  33715. for (const key in config.define) {
  33716. if (key.startsWith(`import.meta.env.`)) {
  33717. const val = config.define[key];
  33718. if (typeof val === "string") {
  33719. try {
  33720. const parsed = JSON.parse(val);
  33721. env[key.slice(16)] = typeof parsed === "string" ? parsed : val;
  33722. } catch {
  33723. env[key.slice(16)] = val;
  33724. }
  33725. } else {
  33726. env[key.slice(16)] = JSON.stringify(val);
  33727. }
  33728. }
  33729. }
  33730. return (html, ctx) => {
  33731. return html.replace(pattern, (text, key) => {
  33732. if (key in env) {
  33733. return env[key];
  33734. } else {
  33735. if (envPrefix.some((prefix) => key.startsWith(prefix))) {
  33736. const relativeHtml = normalizePath$3(
  33737. path$n.relative(config.root, ctx.filename)
  33738. );
  33739. config.logger.warn(
  33740. colors$1.yellow(
  33741. colors$1.bold(
  33742. `(!) ${text} is not defined in env variables found in /${relativeHtml}. Is the variable mistyped?`
  33743. )
  33744. )
  33745. );
  33746. }
  33747. return text;
  33748. }
  33749. });
  33750. };
  33751. }
  33752. function injectNonceAttributeTagHook(config) {
  33753. const processRelType = /* @__PURE__ */ new Set(["stylesheet", "modulepreload", "preload"]);
  33754. return async (html, { filename }) => {
  33755. const nonce = config.html?.cspNonce;
  33756. if (!nonce) return;
  33757. const s = new MagicString(html);
  33758. await traverseHtml(html, filename, (node) => {
  33759. if (!nodeIsElement(node)) {
  33760. return;
  33761. }
  33762. const { nodeName, attrs, sourceCodeLocation } = node;
  33763. if (nodeName === "script" || nodeName === "style" || nodeName === "link" && attrs.some(
  33764. (attr) => attr.name === "rel" && parseRelAttr(attr.value).some((a) => processRelType.has(a))
  33765. )) {
  33766. if (attrs.some(({ name }) => name === "nonce")) {
  33767. return;
  33768. }
  33769. const startTagEndOffset = sourceCodeLocation.startTag.endOffset;
  33770. const appendOffset = html[startTagEndOffset - 2] === "/" ? 2 : 1;
  33771. s.appendRight(startTagEndOffset - appendOffset, ` nonce="${nonce}"`);
  33772. }
  33773. });
  33774. return s.toString();
  33775. };
  33776. }
  33777. function resolveHtmlTransforms(plugins, logger) {
  33778. const preHooks = [];
  33779. const normalHooks = [];
  33780. const postHooks = [];
  33781. for (const plugin of plugins) {
  33782. const hook = plugin.transformIndexHtml;
  33783. if (!hook) continue;
  33784. if (typeof hook === "function") {
  33785. normalHooks.push(hook);
  33786. } else {
  33787. if (!("order" in hook) && "enforce" in hook) {
  33788. logger.warnOnce(
  33789. colors$1.yellow(
  33790. `plugin '${plugin.name}' uses deprecated 'enforce' option. Use 'order' option instead.`
  33791. )
  33792. );
  33793. }
  33794. if (!("handler" in hook) && "transform" in hook) {
  33795. logger.warnOnce(
  33796. colors$1.yellow(
  33797. `plugin '${plugin.name}' uses deprecated 'transform' option. Use 'handler' option instead.`
  33798. )
  33799. );
  33800. }
  33801. const order = hook.order ?? (hook.enforce === "pre" ? "pre" : void 0);
  33802. const handler = hook.handler ?? hook.transform;
  33803. if (order === "pre") {
  33804. preHooks.push(handler);
  33805. } else if (order === "post") {
  33806. postHooks.push(handler);
  33807. } else {
  33808. normalHooks.push(handler);
  33809. }
  33810. }
  33811. }
  33812. return [preHooks, normalHooks, postHooks];
  33813. }
  33814. const elementsAllowedInHead = /* @__PURE__ */ new Set([
  33815. "title",
  33816. "base",
  33817. "link",
  33818. "style",
  33819. "meta",
  33820. "script",
  33821. "noscript",
  33822. "template"
  33823. ]);
  33824. function headTagInsertCheck(tags, ctx) {
  33825. if (!tags.length) return;
  33826. const { logger } = ctx.server?.config || {};
  33827. const disallowedTags = tags.filter(
  33828. (tagDescriptor) => !elementsAllowedInHead.has(tagDescriptor.tag)
  33829. );
  33830. if (disallowedTags.length) {
  33831. const dedupedTags = unique(
  33832. disallowedTags.map((tagDescriptor) => `<${tagDescriptor.tag}>`)
  33833. );
  33834. logger?.warn(
  33835. colors$1.yellow(
  33836. colors$1.bold(
  33837. `[${dedupedTags.join(",")}] can not be used inside the <head> Element, please check the 'injectTo' value`
  33838. )
  33839. )
  33840. );
  33841. }
  33842. }
  33843. async function applyHtmlTransforms(html, hooks, ctx) {
  33844. for (const hook of hooks) {
  33845. const res = await hook(html, ctx);
  33846. if (!res) {
  33847. continue;
  33848. }
  33849. if (typeof res === "string") {
  33850. html = res;
  33851. } else {
  33852. let tags;
  33853. if (Array.isArray(res)) {
  33854. tags = res;
  33855. } else {
  33856. html = res.html || html;
  33857. tags = res.tags;
  33858. }
  33859. let headTags;
  33860. let headPrependTags;
  33861. let bodyTags;
  33862. let bodyPrependTags;
  33863. for (const tag of tags) {
  33864. switch (tag.injectTo) {
  33865. case "body":
  33866. (bodyTags ??= []).push(tag);
  33867. break;
  33868. case "body-prepend":
  33869. (bodyPrependTags ??= []).push(tag);
  33870. break;
  33871. case "head":
  33872. (headTags ??= []).push(tag);
  33873. break;
  33874. default:
  33875. (headPrependTags ??= []).push(tag);
  33876. }
  33877. }
  33878. headTagInsertCheck([...headTags || [], ...headPrependTags || []], ctx);
  33879. if (headPrependTags) html = injectToHead(html, headPrependTags, true);
  33880. if (headTags) html = injectToHead(html, headTags);
  33881. if (bodyPrependTags) html = injectToBody(html, bodyPrependTags, true);
  33882. if (bodyTags) html = injectToBody(html, bodyTags);
  33883. }
  33884. }
  33885. return html;
  33886. }
  33887. const importRE = /\bimport\s*(?:"[^"]*[^\\]"|'[^']*[^\\]');*/g;
  33888. const commentRE$1 = /\/\*[\s\S]*?\*\/|\/\/.*$/gm;
  33889. function isEntirelyImport(code) {
  33890. return !code.replace(importRE, "").replace(commentRE$1, "").trim().length;
  33891. }
  33892. function getBaseInHTML(urlRelativePath, config) {
  33893. return config.base === "./" || config.base === "" ? path$n.posix.join(
  33894. path$n.posix.relative(urlRelativePath, "").slice(0, -2),
  33895. "./"
  33896. ) : config.base;
  33897. }
  33898. const headInjectRE = /([ \t]*)<\/head>/i;
  33899. const headPrependInjectRE = /([ \t]*)<head[^>]*>/i;
  33900. const htmlInjectRE = /<\/html>/i;
  33901. const htmlPrependInjectRE = /([ \t]*)<html[^>]*>/i;
  33902. const bodyInjectRE = /([ \t]*)<\/body>/i;
  33903. const bodyPrependInjectRE = /([ \t]*)<body[^>]*>/i;
  33904. const doctypePrependInjectRE = /<!doctype html>/i;
  33905. function injectToHead(html, tags, prepend = false) {
  33906. if (tags.length === 0) return html;
  33907. if (prepend) {
  33908. if (headPrependInjectRE.test(html)) {
  33909. return html.replace(
  33910. headPrependInjectRE,
  33911. (match, p1) => `${match}
  33912. ${serializeTags(tags, incrementIndent(p1))}`
  33913. );
  33914. }
  33915. } else {
  33916. if (headInjectRE.test(html)) {
  33917. return html.replace(
  33918. headInjectRE,
  33919. (match, p1) => `${serializeTags(tags, incrementIndent(p1))}${match}`
  33920. );
  33921. }
  33922. if (bodyPrependInjectRE.test(html)) {
  33923. return html.replace(
  33924. bodyPrependInjectRE,
  33925. (match, p1) => `${serializeTags(tags, p1)}
  33926. ${match}`
  33927. );
  33928. }
  33929. }
  33930. return prependInjectFallback(html, tags);
  33931. }
  33932. function injectToBody(html, tags, prepend = false) {
  33933. if (tags.length === 0) return html;
  33934. if (prepend) {
  33935. if (bodyPrependInjectRE.test(html)) {
  33936. return html.replace(
  33937. bodyPrependInjectRE,
  33938. (match, p1) => `${match}
  33939. ${serializeTags(tags, incrementIndent(p1))}`
  33940. );
  33941. }
  33942. if (headInjectRE.test(html)) {
  33943. return html.replace(
  33944. headInjectRE,
  33945. (match, p1) => `${match}
  33946. ${serializeTags(tags, p1)}`
  33947. );
  33948. }
  33949. return prependInjectFallback(html, tags);
  33950. } else {
  33951. if (bodyInjectRE.test(html)) {
  33952. return html.replace(
  33953. bodyInjectRE,
  33954. (match, p1) => `${serializeTags(tags, incrementIndent(p1))}${match}`
  33955. );
  33956. }
  33957. if (htmlInjectRE.test(html)) {
  33958. return html.replace(htmlInjectRE, `${serializeTags(tags)}
  33959. $&`);
  33960. }
  33961. return html + `
  33962. ` + serializeTags(tags);
  33963. }
  33964. }
  33965. function prependInjectFallback(html, tags) {
  33966. if (htmlPrependInjectRE.test(html)) {
  33967. return html.replace(htmlPrependInjectRE, `$&
  33968. ${serializeTags(tags)}`);
  33969. }
  33970. if (doctypePrependInjectRE.test(html)) {
  33971. return html.replace(doctypePrependInjectRE, `$&
  33972. ${serializeTags(tags)}`);
  33973. }
  33974. return serializeTags(tags) + html;
  33975. }
  33976. const unaryTags = /* @__PURE__ */ new Set(["link", "meta", "base"]);
  33977. function serializeTag({ tag, attrs, children }, indent = "") {
  33978. if (unaryTags.has(tag)) {
  33979. return `<${tag}${serializeAttrs(attrs)}>`;
  33980. } else {
  33981. return `<${tag}${serializeAttrs(attrs)}>${serializeTags(
  33982. children,
  33983. incrementIndent(indent)
  33984. )}</${tag}>`;
  33985. }
  33986. }
  33987. function serializeTags(tags, indent = "") {
  33988. if (typeof tags === "string") {
  33989. return tags;
  33990. } else if (tags && tags.length) {
  33991. return tags.map((tag) => `${indent}${serializeTag(tag, indent)}
  33992. `).join("");
  33993. }
  33994. return "";
  33995. }
  33996. function serializeAttrs(attrs) {
  33997. let res = "";
  33998. for (const key in attrs) {
  33999. if (typeof attrs[key] === "boolean") {
  34000. res += attrs[key] ? ` ${key}` : ``;
  34001. } else {
  34002. res += ` ${key}=${JSON.stringify(attrs[key])}`;
  34003. }
  34004. }
  34005. return res;
  34006. }
  34007. function incrementIndent(indent = "") {
  34008. return `${indent}${indent[0] === " " ? " " : " "}`;
  34009. }
  34010. function getAttrKey(attr) {
  34011. return attr.prefix === void 0 ? attr.name : `${attr.prefix}:${attr.name}`;
  34012. }
  34013. function getAttrSourceCodeLocation(node, attrKey) {
  34014. return node.sourceCodeLocation.attrs[attrKey];
  34015. }
  34016. const decoder = new TextDecoder();
  34017. function resolveCSSOptions(options) {
  34018. if (options?.transformer === "lightningcss") {
  34019. return {
  34020. ...options,
  34021. lightningcss: {
  34022. ...options.lightningcss,
  34023. targets: options.lightningcss?.targets ?? convertTargets(ESBUILD_MODULES_TARGET)
  34024. }
  34025. };
  34026. }
  34027. return { ...options, lightningcss: void 0 };
  34028. }
  34029. const cssModuleRE = new RegExp(`\\.module${CSS_LANGS_RE.source}`);
  34030. const directRequestRE = /[?&]direct\b/;
  34031. const htmlProxyRE = /[?&]html-proxy\b/;
  34032. const htmlProxyIndexRE = /&index=(\d+)/;
  34033. const commonjsProxyRE = /\?commonjs-proxy/;
  34034. const inlineRE$2 = /[?&]inline\b/;
  34035. const inlineCSSRE = /[?&]inline-css\b/;
  34036. const styleAttrRE = /[?&]style-attr\b/;
  34037. const functionCallRE = /^[A-Z_][\w-]*\(/i;
  34038. const transformOnlyRE = /[?&]transform-only\b/;
  34039. const nonEscapedDoubleQuoteRe = /(?<!\\)"/g;
  34040. const cssBundleName = "style.css";
  34041. const isCSSRequest = (request) => CSS_LANGS_RE.test(request);
  34042. const isModuleCSSRequest = (request) => cssModuleRE.test(request);
  34043. const isDirectCSSRequest = (request) => CSS_LANGS_RE.test(request) && directRequestRE.test(request);
  34044. const isDirectRequest = (request) => directRequestRE.test(request);
  34045. const cssModulesCache = /* @__PURE__ */ new WeakMap();
  34046. const removedPureCssFilesCache = /* @__PURE__ */ new WeakMap();
  34047. const postcssConfigCache = /* @__PURE__ */ new WeakMap();
  34048. function encodePublicUrlsInCSS(config) {
  34049. return config.command === "build";
  34050. }
  34051. const cssUrlAssetRE = /__VITE_CSS_URL__([\da-f]+)__/g;
  34052. function cssPlugin(config) {
  34053. const isBuild = config.command === "build";
  34054. let moduleCache;
  34055. const resolveUrl = config.createResolver({
  34056. preferRelative: true,
  34057. tryIndex: false,
  34058. extensions: []
  34059. });
  34060. let preprocessorWorkerController;
  34061. if (config.css?.transformer !== "lightningcss") {
  34062. resolvePostcssConfig(config);
  34063. }
  34064. return {
  34065. name: "vite:css",
  34066. buildStart() {
  34067. moduleCache = /* @__PURE__ */ new Map();
  34068. cssModulesCache.set(config, moduleCache);
  34069. removedPureCssFilesCache.set(config, /* @__PURE__ */ new Map());
  34070. preprocessorWorkerController = createPreprocessorWorkerController(
  34071. normalizeMaxWorkers(config.css.preprocessorMaxWorkers)
  34072. );
  34073. preprocessorWorkerControllerCache.set(
  34074. config,
  34075. preprocessorWorkerController
  34076. );
  34077. },
  34078. buildEnd() {
  34079. preprocessorWorkerController?.close();
  34080. },
  34081. async load(id) {
  34082. if (!isCSSRequest(id)) return;
  34083. if (urlRE$1.test(id)) {
  34084. if (isModuleCSSRequest(id)) {
  34085. throw new Error(
  34086. `?url is not supported with CSS modules. (tried to import ${JSON.stringify(
  34087. id
  34088. )})`
  34089. );
  34090. }
  34091. if (isBuild) {
  34092. id = injectQuery(removeUrlQuery(id), "transform-only");
  34093. return `import ${JSON.stringify(id)};export default "__VITE_CSS_URL__${Buffer.from(id).toString(
  34094. "hex"
  34095. )}__"`;
  34096. }
  34097. }
  34098. },
  34099. async transform(raw, id) {
  34100. if (!isCSSRequest(id) || commonjsProxyRE.test(id) || SPECIAL_QUERY_RE.test(id)) {
  34101. return;
  34102. }
  34103. const urlReplacer = async (url, importer) => {
  34104. const decodedUrl = decodeURI(url);
  34105. if (checkPublicFile(decodedUrl, config)) {
  34106. if (encodePublicUrlsInCSS(config)) {
  34107. return publicFileToBuiltUrl(decodedUrl, config);
  34108. } else {
  34109. return joinUrlSegments(config.base, decodedUrl);
  34110. }
  34111. }
  34112. const [id2, fragment] = decodedUrl.split("#");
  34113. let resolved = await resolveUrl(id2, importer);
  34114. if (resolved) {
  34115. if (fragment) resolved += "#" + fragment;
  34116. return fileToUrl$1(resolved, config, this);
  34117. }
  34118. if (config.command === "build") {
  34119. const isExternal = config.build.rollupOptions.external ? resolveUserExternal(
  34120. config.build.rollupOptions.external,
  34121. decodedUrl,
  34122. // use URL as id since id could not be resolved
  34123. id2,
  34124. false
  34125. ) : false;
  34126. if (!isExternal) {
  34127. config.logger.warnOnce(
  34128. `
  34129. ${decodedUrl} referenced in ${id2} didn't resolve at build time, it will remain unchanged to be resolved at runtime`
  34130. );
  34131. }
  34132. }
  34133. return url;
  34134. };
  34135. const {
  34136. code: css,
  34137. modules,
  34138. deps,
  34139. map: map2
  34140. } = await compileCSS(
  34141. id,
  34142. raw,
  34143. config,
  34144. preprocessorWorkerController,
  34145. urlReplacer
  34146. );
  34147. if (modules) {
  34148. moduleCache.set(id, modules);
  34149. }
  34150. if (deps) {
  34151. for (const file of deps) {
  34152. this.addWatchFile(file);
  34153. }
  34154. }
  34155. return {
  34156. code: css,
  34157. map: map2
  34158. };
  34159. }
  34160. };
  34161. }
  34162. function cssPostPlugin(config) {
  34163. const styles = /* @__PURE__ */ new Map();
  34164. let codeSplitEmitQueue = createSerialPromiseQueue();
  34165. const urlEmitQueue = createSerialPromiseQueue();
  34166. let pureCssChunks;
  34167. let hasEmitted = false;
  34168. let chunkCSSMap;
  34169. const rollupOptionsOutput = config.build.rollupOptions.output;
  34170. const assetFileNames = (Array.isArray(rollupOptionsOutput) ? rollupOptionsOutput[0] : rollupOptionsOutput)?.assetFileNames;
  34171. const getCssAssetDirname = (cssAssetName) => {
  34172. const cssAssetNameDir = path$n.dirname(cssAssetName);
  34173. if (!assetFileNames) {
  34174. return path$n.join(config.build.assetsDir, cssAssetNameDir);
  34175. } else if (typeof assetFileNames === "string") {
  34176. return path$n.join(path$n.dirname(assetFileNames), cssAssetNameDir);
  34177. } else {
  34178. return path$n.dirname(
  34179. assetFileNames({
  34180. type: "asset",
  34181. name: cssAssetName,
  34182. originalFileName: null,
  34183. source: "/* vite internal call, ignore */"
  34184. })
  34185. );
  34186. }
  34187. };
  34188. return {
  34189. name: "vite:css-post",
  34190. renderStart() {
  34191. pureCssChunks = /* @__PURE__ */ new Set();
  34192. hasEmitted = false;
  34193. chunkCSSMap = /* @__PURE__ */ new Map();
  34194. codeSplitEmitQueue = createSerialPromiseQueue();
  34195. },
  34196. async transform(css, id, options) {
  34197. if (!isCSSRequest(id) || commonjsProxyRE.test(id) || SPECIAL_QUERY_RE.test(id)) {
  34198. return;
  34199. }
  34200. css = stripBomTag(css);
  34201. const inlineCSS = inlineCSSRE.test(id);
  34202. const isHTMLProxy = htmlProxyRE.test(id);
  34203. if (inlineCSS && isHTMLProxy) {
  34204. if (styleAttrRE.test(id)) {
  34205. css = css.replace(/"/g, "&quot;");
  34206. }
  34207. const index = htmlProxyIndexRE.exec(id)?.[1];
  34208. if (index == null) {
  34209. throw new Error(`HTML proxy index in "${id}" not found`);
  34210. }
  34211. addToHTMLProxyTransformResult(
  34212. `${getHash(cleanUrl(id))}_${Number.parseInt(index)}`,
  34213. css
  34214. );
  34215. return `export default ''`;
  34216. }
  34217. const inlined = inlineRE$2.test(id);
  34218. const modules = cssModulesCache.get(config).get(id);
  34219. const modulesCode = modules && !inlined && dataToEsm(modules, { namedExports: true, preferConst: true });
  34220. if (config.command === "serve") {
  34221. const getContentWithSourcemap = async (content) => {
  34222. if (config.css?.devSourcemap) {
  34223. const sourcemap = this.getCombinedSourcemap();
  34224. if (sourcemap.mappings) {
  34225. await injectSourcesContent(sourcemap, cleanUrl(id), config.logger);
  34226. }
  34227. return getCodeWithSourcemap("css", content, sourcemap);
  34228. }
  34229. return content;
  34230. };
  34231. if (isDirectCSSRequest(id)) {
  34232. return null;
  34233. }
  34234. if (options?.ssr) {
  34235. return modulesCode || `export default ${JSON.stringify(css)}`;
  34236. }
  34237. if (inlined) {
  34238. return `export default ${JSON.stringify(css)}`;
  34239. }
  34240. const cssContent = await getContentWithSourcemap(css);
  34241. const code2 = [
  34242. `import { updateStyle as __vite__updateStyle, removeStyle as __vite__removeStyle } from ${JSON.stringify(
  34243. path$n.posix.join(config.base, CLIENT_PUBLIC_PATH)
  34244. )}`,
  34245. `const __vite__id = ${JSON.stringify(id)}`,
  34246. `const __vite__css = ${JSON.stringify(cssContent)}`,
  34247. `__vite__updateStyle(__vite__id, __vite__css)`,
  34248. // css modules exports change on edit so it can't self accept
  34249. `${modulesCode || "import.meta.hot.accept()"}`,
  34250. `import.meta.hot.prune(() => __vite__removeStyle(__vite__id))`
  34251. ].join("\n");
  34252. return { code: code2, map: { mappings: "" } };
  34253. }
  34254. if (!inlined) {
  34255. styles.set(id, css);
  34256. }
  34257. let code;
  34258. if (modulesCode) {
  34259. code = modulesCode;
  34260. } else if (inlined) {
  34261. let content = css;
  34262. if (config.build.cssMinify) {
  34263. content = await minifyCSS(content, config, true);
  34264. }
  34265. code = `export default ${JSON.stringify(content)}`;
  34266. } else {
  34267. code = "";
  34268. }
  34269. return {
  34270. code,
  34271. map: { mappings: "" },
  34272. // avoid the css module from being tree-shaken so that we can retrieve
  34273. // it in renderChunk()
  34274. moduleSideEffects: modulesCode || inlined ? false : "no-treeshake"
  34275. };
  34276. },
  34277. async renderChunk(code, chunk, opts) {
  34278. let chunkCSS = "";
  34279. const isJsChunkEmpty = code === "" && !chunk.isEntry;
  34280. let isPureCssChunk = true;
  34281. const ids = Object.keys(chunk.modules);
  34282. for (const id of ids) {
  34283. if (styles.has(id)) {
  34284. if (!transformOnlyRE.test(id)) {
  34285. chunkCSS += styles.get(id);
  34286. if (cssModuleRE.test(id)) {
  34287. isPureCssChunk = false;
  34288. }
  34289. }
  34290. } else if (!isJsChunkEmpty) {
  34291. isPureCssChunk = false;
  34292. }
  34293. }
  34294. const publicAssetUrlMap = publicAssetUrlCache.get(config);
  34295. const resolveAssetUrlsInCss = (chunkCSS2, cssAssetName) => {
  34296. const encodedPublicUrls = encodePublicUrlsInCSS(config);
  34297. const relative = config.base === "./" || config.base === "";
  34298. const cssAssetDirname = encodedPublicUrls || relative ? slash$1(getCssAssetDirname(cssAssetName)) : void 0;
  34299. const toRelative = (filename) => {
  34300. const relativePath = normalizePath$3(
  34301. path$n.relative(cssAssetDirname, filename)
  34302. );
  34303. return relativePath[0] === "." ? relativePath : "./" + relativePath;
  34304. };
  34305. chunkCSS2 = chunkCSS2.replace(assetUrlRE, (_, fileHash, postfix = "") => {
  34306. const filename = this.getFileName(fileHash) + postfix;
  34307. chunk.viteMetadata.importedAssets.add(cleanUrl(filename));
  34308. return encodeURIPath(
  34309. toOutputFilePathInCss(
  34310. filename,
  34311. "asset",
  34312. cssAssetName,
  34313. "css",
  34314. config,
  34315. toRelative
  34316. )
  34317. );
  34318. });
  34319. if (encodedPublicUrls) {
  34320. const relativePathToPublicFromCSS = normalizePath$3(
  34321. path$n.relative(cssAssetDirname, "")
  34322. );
  34323. chunkCSS2 = chunkCSS2.replace(publicAssetUrlRE, (_, hash) => {
  34324. const publicUrl = publicAssetUrlMap.get(hash).slice(1);
  34325. return encodeURIPath(
  34326. toOutputFilePathInCss(
  34327. publicUrl,
  34328. "public",
  34329. cssAssetName,
  34330. "css",
  34331. config,
  34332. () => `${relativePathToPublicFromCSS}/${publicUrl}`
  34333. )
  34334. );
  34335. });
  34336. }
  34337. return chunkCSS2;
  34338. };
  34339. function ensureFileExt(name, ext) {
  34340. return normalizePath$3(
  34341. path$n.format({ ...path$n.parse(name), base: void 0, ext })
  34342. );
  34343. }
  34344. let s;
  34345. const urlEmitTasks = [];
  34346. if (code.includes("__VITE_CSS_URL__")) {
  34347. let match;
  34348. cssUrlAssetRE.lastIndex = 0;
  34349. while (match = cssUrlAssetRE.exec(code)) {
  34350. const [full, idHex] = match;
  34351. const id = Buffer.from(idHex, "hex").toString();
  34352. const originalFileName = cleanUrl(id);
  34353. const cssAssetName = ensureFileExt(
  34354. path$n.basename(originalFileName),
  34355. ".css"
  34356. );
  34357. if (!styles.has(id)) {
  34358. throw new Error(
  34359. `css content for ${JSON.stringify(id)} was not found`
  34360. );
  34361. }
  34362. let cssContent = styles.get(id);
  34363. cssContent = resolveAssetUrlsInCss(cssContent, cssAssetName);
  34364. urlEmitTasks.push({
  34365. cssAssetName,
  34366. originalFileName,
  34367. content: cssContent,
  34368. start: match.index,
  34369. end: match.index + full.length
  34370. });
  34371. }
  34372. }
  34373. await urlEmitQueue.run(
  34374. async () => Promise.all(
  34375. urlEmitTasks.map(async (info) => {
  34376. info.content = await finalizeCss(info.content, true, config);
  34377. })
  34378. )
  34379. );
  34380. if (urlEmitTasks.length > 0) {
  34381. const toRelativeRuntime = createToImportMetaURLBasedRelativeRuntime(
  34382. opts.format,
  34383. config.isWorker
  34384. );
  34385. s ||= new MagicString(code);
  34386. for (const {
  34387. cssAssetName,
  34388. originalFileName,
  34389. content,
  34390. start,
  34391. end
  34392. } of urlEmitTasks) {
  34393. const referenceId = this.emitFile({
  34394. type: "asset",
  34395. name: cssAssetName,
  34396. originalFileName,
  34397. source: content
  34398. });
  34399. generatedAssets.get(config).set(referenceId, { originalFileName });
  34400. const filename = this.getFileName(referenceId);
  34401. chunk.viteMetadata.importedAssets.add(cleanUrl(filename));
  34402. const replacement = toOutputFilePathInJS(
  34403. filename,
  34404. "asset",
  34405. chunk.fileName,
  34406. "js",
  34407. config,
  34408. toRelativeRuntime
  34409. );
  34410. const replacementString = typeof replacement === "string" ? JSON.stringify(encodeURIPath(replacement)).slice(1, -1) : `"+${replacement.runtime}+"`;
  34411. s.update(start, end, replacementString);
  34412. }
  34413. }
  34414. if (chunkCSS) {
  34415. if (isPureCssChunk && (opts.format === "es" || opts.format === "cjs")) {
  34416. pureCssChunks.add(chunk);
  34417. }
  34418. if (config.build.cssCodeSplit) {
  34419. if (opts.format === "es" || opts.format === "cjs") {
  34420. const isEntry = chunk.isEntry && isPureCssChunk;
  34421. const cssFullAssetName = ensureFileExt(chunk.name, ".css");
  34422. const cssAssetName = chunk.isEntry && (!chunk.facadeModuleId || !isCSSRequest(chunk.facadeModuleId)) ? path$n.basename(cssFullAssetName) : cssFullAssetName;
  34423. const originalFileName = getChunkOriginalFileName(
  34424. chunk,
  34425. config.root,
  34426. opts.format
  34427. );
  34428. chunkCSS = resolveAssetUrlsInCss(chunkCSS, cssAssetName);
  34429. chunkCSS = await codeSplitEmitQueue.run(async () => {
  34430. return finalizeCss(chunkCSS, true, config);
  34431. });
  34432. const referenceId = this.emitFile({
  34433. type: "asset",
  34434. name: cssAssetName,
  34435. originalFileName,
  34436. source: chunkCSS
  34437. });
  34438. generatedAssets.get(config).set(referenceId, { originalFileName, isEntry });
  34439. chunk.viteMetadata.importedCss.add(this.getFileName(referenceId));
  34440. } else if (!config.build.ssr) {
  34441. chunkCSS = await finalizeCss(chunkCSS, true, config);
  34442. let cssString = JSON.stringify(chunkCSS);
  34443. cssString = renderAssetUrlInJS(
  34444. this,
  34445. config,
  34446. chunk,
  34447. opts,
  34448. cssString
  34449. )?.toString() || cssString;
  34450. const style = `__vite_style__`;
  34451. const injectCode = `var ${style} = document.createElement('style');${style}.textContent = ${cssString};document.head.appendChild(${style});`;
  34452. let injectionPoint;
  34453. const wrapIdx = code.indexOf("System.register");
  34454. if (wrapIdx >= 0) {
  34455. const executeFnStart = code.indexOf("execute:", wrapIdx);
  34456. injectionPoint = code.indexOf("{", executeFnStart) + 1;
  34457. } else {
  34458. const insertMark = "'use strict';";
  34459. injectionPoint = code.indexOf(insertMark) + insertMark.length;
  34460. }
  34461. s ||= new MagicString(code);
  34462. s.appendRight(injectionPoint, injectCode);
  34463. }
  34464. } else {
  34465. chunkCSS = resolveAssetUrlsInCss(chunkCSS, cssBundleName);
  34466. chunkCSSMap.set(chunk.fileName, chunkCSS);
  34467. }
  34468. }
  34469. if (s) {
  34470. if (config.build.sourcemap) {
  34471. return {
  34472. code: s.toString(),
  34473. map: s.generateMap({ hires: "boundary" })
  34474. };
  34475. } else {
  34476. return { code: s.toString() };
  34477. }
  34478. }
  34479. return null;
  34480. },
  34481. augmentChunkHash(chunk) {
  34482. if (chunk.viteMetadata?.importedCss.size) {
  34483. let hash = "";
  34484. for (const id of chunk.viteMetadata.importedCss) {
  34485. hash += id;
  34486. }
  34487. return hash;
  34488. }
  34489. },
  34490. async generateBundle(opts, bundle) {
  34491. if (opts.__vite_skip_asset_emit__) {
  34492. return;
  34493. }
  34494. function extractCss() {
  34495. let css = "";
  34496. const collected = /* @__PURE__ */ new Set();
  34497. const dynamicImports = /* @__PURE__ */ new Set();
  34498. function collect(chunk) {
  34499. if (!chunk || chunk.type !== "chunk" || collected.has(chunk)) return;
  34500. collected.add(chunk);
  34501. chunk.imports.forEach((importName) => collect(bundle[importName]));
  34502. chunk.dynamicImports.forEach(
  34503. (importName) => dynamicImports.add(importName)
  34504. );
  34505. css += chunkCSSMap.get(chunk.preliminaryFileName) ?? "";
  34506. }
  34507. for (const chunk of Object.values(bundle)) {
  34508. if (chunk.type === "chunk" && chunk.isEntry) {
  34509. collect(chunk);
  34510. }
  34511. }
  34512. for (const chunkName of dynamicImports) {
  34513. collect(bundle[chunkName]);
  34514. }
  34515. return css;
  34516. }
  34517. let extractedCss = !hasEmitted && extractCss();
  34518. if (extractedCss) {
  34519. hasEmitted = true;
  34520. extractedCss = await finalizeCss(extractedCss, true, config);
  34521. this.emitFile({
  34522. name: cssBundleName,
  34523. type: "asset",
  34524. source: extractedCss
  34525. });
  34526. }
  34527. if (pureCssChunks.size) {
  34528. const prelimaryNameToChunkMap = Object.fromEntries(
  34529. Object.values(bundle).filter((chunk) => chunk.type === "chunk").map((chunk) => [chunk.preliminaryFileName, chunk.fileName])
  34530. );
  34531. const pureCssChunkNames = [...pureCssChunks].map((pureCssChunk) => prelimaryNameToChunkMap[pureCssChunk.fileName]).filter(Boolean);
  34532. const replaceEmptyChunk = getEmptyChunkReplacer(
  34533. pureCssChunkNames,
  34534. opts.format
  34535. );
  34536. for (const file in bundle) {
  34537. const chunk = bundle[file];
  34538. if (chunk.type === "chunk") {
  34539. let chunkImportsPureCssChunk = false;
  34540. chunk.imports = chunk.imports.filter((file2) => {
  34541. if (pureCssChunkNames.includes(file2)) {
  34542. const { importedCss, importedAssets } = bundle[file2].viteMetadata;
  34543. importedCss.forEach(
  34544. (file3) => chunk.viteMetadata.importedCss.add(file3)
  34545. );
  34546. importedAssets.forEach(
  34547. (file3) => chunk.viteMetadata.importedAssets.add(file3)
  34548. );
  34549. chunkImportsPureCssChunk = true;
  34550. return false;
  34551. }
  34552. return true;
  34553. });
  34554. if (chunkImportsPureCssChunk) {
  34555. chunk.code = replaceEmptyChunk(chunk.code);
  34556. }
  34557. }
  34558. }
  34559. const removedPureCssFiles = removedPureCssFilesCache.get(config);
  34560. pureCssChunkNames.forEach((fileName) => {
  34561. removedPureCssFiles.set(fileName, bundle[fileName]);
  34562. delete bundle[fileName];
  34563. delete bundle[`${fileName}.map`];
  34564. });
  34565. }
  34566. const cssAssets = Object.values(bundle).filter(
  34567. (asset) => asset.type === "asset" && asset.fileName.endsWith(".css")
  34568. );
  34569. for (const cssAsset of cssAssets) {
  34570. if (typeof cssAsset.source === "string") {
  34571. cssAsset.source = cssAsset.source.replace(viteHashUpdateMarkerRE, "");
  34572. }
  34573. }
  34574. }
  34575. };
  34576. }
  34577. function cssAnalysisPlugin(config) {
  34578. let server;
  34579. return {
  34580. name: "vite:css-analysis",
  34581. configureServer(_server) {
  34582. server = _server;
  34583. },
  34584. async transform(_, id, options) {
  34585. if (!isCSSRequest(id) || commonjsProxyRE.test(id) || SPECIAL_QUERY_RE.test(id)) {
  34586. return;
  34587. }
  34588. const ssr = options?.ssr === true;
  34589. const { moduleGraph } = server;
  34590. const thisModule = moduleGraph.getModuleById(id);
  34591. if (thisModule) {
  34592. const isSelfAccepting = !cssModulesCache.get(config)?.get(id) && !inlineRE$2.test(id) && !htmlProxyRE.test(id);
  34593. const pluginImports = this._addedImports;
  34594. if (pluginImports) {
  34595. const depModules = /* @__PURE__ */ new Set();
  34596. for (const file of pluginImports) {
  34597. depModules.add(
  34598. isCSSRequest(file) ? moduleGraph.createFileOnlyEntry(file) : await moduleGraph.ensureEntryFromUrl(
  34599. fileToDevUrl(
  34600. file,
  34601. config,
  34602. /* skipBase */
  34603. true
  34604. ),
  34605. ssr
  34606. )
  34607. );
  34608. }
  34609. moduleGraph.updateModuleInfo(
  34610. thisModule,
  34611. depModules,
  34612. null,
  34613. // The root CSS proxy module is self-accepting and should not
  34614. // have an explicit accept list
  34615. /* @__PURE__ */ new Set(),
  34616. null,
  34617. isSelfAccepting,
  34618. ssr
  34619. );
  34620. } else {
  34621. thisModule.isSelfAccepting = isSelfAccepting;
  34622. }
  34623. }
  34624. }
  34625. };
  34626. }
  34627. function getEmptyChunkReplacer(pureCssChunkNames, outputFormat) {
  34628. const emptyChunkFiles = pureCssChunkNames.map((file) => path$n.basename(file)).join("|").replace(/\./g, "\\.");
  34629. const emptyChunkRE = new RegExp(
  34630. outputFormat === "es" ? `\\bimport\\s*["'][^"']*(?:${emptyChunkFiles})["'];` : `(\\b|,\\s*)require\\(\\s*["'][^"']*(?:${emptyChunkFiles})["']\\)(;|,)`,
  34631. "g"
  34632. );
  34633. return (code) => code.replace(
  34634. emptyChunkRE,
  34635. // remove css import while preserving source map location
  34636. (m) => outputFormat === "es" ? `/* empty css ${"".padEnd(m.length - 15)}*/` : `${m.at(-1)}/* empty css ${"".padEnd(m.length - 16)}*/`
  34637. );
  34638. }
  34639. function createCSSResolvers(config) {
  34640. let cssResolve;
  34641. let sassResolve;
  34642. let lessResolve;
  34643. return {
  34644. get css() {
  34645. return cssResolve || (cssResolve = config.createResolver({
  34646. extensions: [".css"],
  34647. mainFields: ["style"],
  34648. conditions: ["style"],
  34649. tryIndex: false,
  34650. preferRelative: true
  34651. }));
  34652. },
  34653. get sass() {
  34654. if (!sassResolve) {
  34655. const resolver = config.createResolver({
  34656. extensions: [".scss", ".sass", ".css"],
  34657. mainFields: ["sass", "style"],
  34658. conditions: ["sass", "style"],
  34659. tryIndex: true,
  34660. tryPrefix: "_",
  34661. preferRelative: true
  34662. });
  34663. sassResolve = async (...args) => {
  34664. if (args[0].startsWith("file://")) {
  34665. args[0] = fileURLToPath(args[0]);
  34666. }
  34667. return resolver(...args);
  34668. };
  34669. }
  34670. return sassResolve;
  34671. },
  34672. get less() {
  34673. return lessResolve || (lessResolve = config.createResolver({
  34674. extensions: [".less", ".css"],
  34675. mainFields: ["less", "style"],
  34676. conditions: ["less", "style"],
  34677. tryIndex: false,
  34678. preferRelative: true
  34679. }));
  34680. }
  34681. };
  34682. }
  34683. function getCssResolversKeys(resolvers) {
  34684. return Object.keys(resolvers);
  34685. }
  34686. async function compileCSSPreprocessors(id, lang, code, config, workerController) {
  34687. const { preprocessorOptions, devSourcemap } = config.css ?? {};
  34688. const atImportResolvers = getAtImportResolvers(config);
  34689. const preProcessor = workerController[lang];
  34690. let opts = preprocessorOptions && preprocessorOptions[lang] || {};
  34691. switch (lang) {
  34692. case "scss" /* scss */:
  34693. case "sass" /* sass */:
  34694. opts = {
  34695. includePaths: ["node_modules"],
  34696. alias: config.resolve.alias,
  34697. ...opts
  34698. };
  34699. break;
  34700. case "less" /* less */:
  34701. case "styl" /* styl */:
  34702. case "stylus" /* stylus */:
  34703. opts = {
  34704. paths: ["node_modules"],
  34705. alias: config.resolve.alias,
  34706. ...opts
  34707. };
  34708. }
  34709. opts.filename = cleanUrl(id);
  34710. opts.enableSourcemap = devSourcemap ?? false;
  34711. const preprocessResult = await preProcessor(
  34712. code,
  34713. config.root,
  34714. opts,
  34715. atImportResolvers
  34716. );
  34717. if (preprocessResult.error) {
  34718. throw preprocessResult.error;
  34719. }
  34720. let deps;
  34721. if (preprocessResult.deps) {
  34722. const normalizedFilename = normalizePath$3(opts.filename);
  34723. deps = new Set(
  34724. [...preprocessResult.deps].filter(
  34725. (dep) => normalizePath$3(dep) !== normalizedFilename
  34726. )
  34727. );
  34728. }
  34729. return {
  34730. code: preprocessResult.code,
  34731. map: combineSourcemapsIfExists(
  34732. opts.filename,
  34733. preprocessResult.map,
  34734. preprocessResult.additionalMap
  34735. ),
  34736. deps
  34737. };
  34738. }
  34739. const configToAtImportResolvers = /* @__PURE__ */ new WeakMap();
  34740. function getAtImportResolvers(config) {
  34741. let atImportResolvers = configToAtImportResolvers.get(config);
  34742. if (!atImportResolvers) {
  34743. atImportResolvers = createCSSResolvers(config);
  34744. configToAtImportResolvers.set(config, atImportResolvers);
  34745. }
  34746. return atImportResolvers;
  34747. }
  34748. async function compileCSS(id, code, config, workerController, urlReplacer) {
  34749. if (config.css?.transformer === "lightningcss") {
  34750. return compileLightningCSS(id, code, config, urlReplacer);
  34751. }
  34752. const { modules: modulesOptions, devSourcemap } = config.css || {};
  34753. const isModule = modulesOptions !== false && cssModuleRE.test(id);
  34754. const needInlineImport = code.includes("@import");
  34755. const hasUrl = cssUrlRE.test(code) || cssImageSetRE.test(code);
  34756. const lang = CSS_LANGS_RE.exec(id)?.[1];
  34757. const postcssConfig = await resolvePostcssConfig(config);
  34758. if (lang === "css" && !postcssConfig && !isModule && !needInlineImport && !hasUrl) {
  34759. return { code, map: null };
  34760. }
  34761. let modules;
  34762. const deps = /* @__PURE__ */ new Set();
  34763. let preprocessorMap;
  34764. if (isPreProcessor(lang)) {
  34765. const preprocessorResult = await compileCSSPreprocessors(
  34766. id,
  34767. lang,
  34768. code,
  34769. config,
  34770. workerController
  34771. );
  34772. code = preprocessorResult.code;
  34773. preprocessorMap = preprocessorResult.map;
  34774. preprocessorResult.deps?.forEach((dep) => deps.add(dep));
  34775. }
  34776. const atImportResolvers = getAtImportResolvers(config);
  34777. const postcssOptions = postcssConfig && postcssConfig.options || {};
  34778. const postcssPlugins = postcssConfig && postcssConfig.plugins ? postcssConfig.plugins.slice() : [];
  34779. if (needInlineImport) {
  34780. postcssPlugins.unshift(
  34781. (await importPostcssImport()).default({
  34782. async resolve(id2, basedir) {
  34783. const publicFile = checkPublicFile(id2, config);
  34784. if (publicFile) {
  34785. return publicFile;
  34786. }
  34787. const resolved = await atImportResolvers.css(
  34788. id2,
  34789. path$n.join(basedir, "*")
  34790. );
  34791. if (resolved) {
  34792. return path$n.resolve(resolved);
  34793. }
  34794. if (!path$n.isAbsolute(id2)) {
  34795. config.logger.error(
  34796. colors$1.red(
  34797. `Unable to resolve \`@import "${id2}"\` from ${basedir}`
  34798. )
  34799. );
  34800. }
  34801. return id2;
  34802. },
  34803. async load(id2) {
  34804. const code2 = await fs__default.promises.readFile(id2, "utf-8");
  34805. const lang2 = CSS_LANGS_RE.exec(id2)?.[1];
  34806. if (isPreProcessor(lang2)) {
  34807. const result = await compileCSSPreprocessors(
  34808. id2,
  34809. lang2,
  34810. code2,
  34811. config,
  34812. workerController
  34813. );
  34814. result.deps?.forEach((dep) => deps.add(dep));
  34815. return result.code;
  34816. }
  34817. return code2;
  34818. },
  34819. nameLayer(index) {
  34820. return `vite--anon-layer-${getHash(id)}-${index}`;
  34821. }
  34822. })
  34823. );
  34824. }
  34825. if (urlReplacer) {
  34826. postcssPlugins.push(
  34827. UrlRewritePostcssPlugin({
  34828. replacer: urlReplacer,
  34829. logger: config.logger
  34830. })
  34831. );
  34832. }
  34833. if (isModule) {
  34834. postcssPlugins.unshift(
  34835. (await importPostcssModules()).default({
  34836. ...modulesOptions,
  34837. localsConvention: modulesOptions?.localsConvention,
  34838. getJSON(cssFileName, _modules, outputFileName) {
  34839. modules = _modules;
  34840. if (modulesOptions && typeof modulesOptions.getJSON === "function") {
  34841. modulesOptions.getJSON(cssFileName, _modules, outputFileName);
  34842. }
  34843. },
  34844. async resolve(id2, importer) {
  34845. for (const key of getCssResolversKeys(atImportResolvers)) {
  34846. const resolved = await atImportResolvers[key](id2, importer);
  34847. if (resolved) {
  34848. return path$n.resolve(resolved);
  34849. }
  34850. }
  34851. return id2;
  34852. }
  34853. })
  34854. );
  34855. }
  34856. if (!postcssPlugins.length) {
  34857. return {
  34858. code,
  34859. map: preprocessorMap,
  34860. deps
  34861. };
  34862. }
  34863. let postcssResult;
  34864. try {
  34865. const source = removeDirectQuery(id);
  34866. const postcss = await importPostcss();
  34867. postcssResult = await postcss.default(postcssPlugins).process(code, {
  34868. ...postcssOptions,
  34869. parser: lang === "sss" ? loadSss(config.root) : postcssOptions.parser,
  34870. to: source,
  34871. from: source,
  34872. ...devSourcemap ? {
  34873. map: {
  34874. inline: false,
  34875. annotation: false,
  34876. // postcss may return virtual files
  34877. // we cannot obtain content of them, so this needs to be enabled
  34878. sourcesContent: true
  34879. // when "prev: preprocessorMap", the result map may include duplicate filename in `postcssResult.map.sources`
  34880. // prev: preprocessorMap,
  34881. }
  34882. } : {}
  34883. });
  34884. for (const message of postcssResult.messages) {
  34885. if (message.type === "dependency") {
  34886. deps.add(normalizePath$3(message.file));
  34887. } else if (message.type === "dir-dependency") {
  34888. const { dir, glob: globPattern = "**" } = message;
  34889. const pattern = glob.escapePath(normalizePath$3(path$n.resolve(path$n.dirname(id), dir))) + `/` + globPattern;
  34890. const files = glob.sync(pattern, {
  34891. ignore: ["**/node_modules/**"]
  34892. });
  34893. for (let i = 0; i < files.length; i++) {
  34894. deps.add(files[i]);
  34895. }
  34896. } else if (message.type === "warning") {
  34897. const warning = message;
  34898. let msg = `[vite:css] ${warning.text}`;
  34899. msg += `
  34900. ${generateCodeFrame(
  34901. code,
  34902. {
  34903. line: warning.line,
  34904. column: warning.column - 1
  34905. // 1-based
  34906. },
  34907. warning.endLine !== void 0 && warning.endColumn !== void 0 ? {
  34908. line: warning.endLine,
  34909. column: warning.endColumn - 1
  34910. // 1-based
  34911. } : void 0
  34912. )}`;
  34913. config.logger.warn(colors$1.yellow(msg));
  34914. }
  34915. }
  34916. } catch (e) {
  34917. e.message = `[postcss] ${e.message}`;
  34918. e.code = code;
  34919. e.loc = {
  34920. file: e.file,
  34921. line: e.line,
  34922. column: e.column - 1
  34923. // 1-based
  34924. };
  34925. throw e;
  34926. }
  34927. if (!devSourcemap) {
  34928. return {
  34929. ast: postcssResult,
  34930. code: postcssResult.css,
  34931. map: { mappings: "" },
  34932. modules,
  34933. deps
  34934. };
  34935. }
  34936. const rawPostcssMap = postcssResult.map.toJSON();
  34937. const postcssMap = await formatPostcssSourceMap(
  34938. // version property of rawPostcssMap is declared as string
  34939. // but actually it is a number
  34940. rawPostcssMap,
  34941. cleanUrl(id)
  34942. );
  34943. return {
  34944. ast: postcssResult,
  34945. code: postcssResult.css,
  34946. map: combineSourcemapsIfExists(cleanUrl(id), postcssMap, preprocessorMap),
  34947. modules,
  34948. deps
  34949. };
  34950. }
  34951. function createCachedImport(imp) {
  34952. let cached;
  34953. return () => {
  34954. if (!cached) {
  34955. cached = imp().then((module) => {
  34956. cached = module;
  34957. return module;
  34958. });
  34959. }
  34960. return cached;
  34961. };
  34962. }
  34963. const importPostcssImport = createCachedImport(() => import('./dep-BB45zftN.js').then(function (n) { return n.i; }));
  34964. const importPostcssModules = createCachedImport(() => import('./dep-Dnp7gl8U.js').then(function (n) { return n.i; }));
  34965. const importPostcss = createCachedImport(() => import('postcss'));
  34966. const preprocessorWorkerControllerCache = /* @__PURE__ */ new WeakMap();
  34967. let alwaysFakeWorkerWorkerControllerCache;
  34968. async function preprocessCSS(code, filename, config) {
  34969. let workerController = preprocessorWorkerControllerCache.get(config);
  34970. if (!workerController) {
  34971. alwaysFakeWorkerWorkerControllerCache ||= createPreprocessorWorkerController(0);
  34972. workerController = alwaysFakeWorkerWorkerControllerCache;
  34973. }
  34974. return await compileCSS(filename, code, config, workerController);
  34975. }
  34976. async function formatPostcssSourceMap(rawMap, file) {
  34977. const inputFileDir = path$n.dirname(file);
  34978. const sources = rawMap.sources.map((source) => {
  34979. const cleanSource = cleanUrl(decodeURIComponent(source));
  34980. if (cleanSource[0] === "<" && cleanSource[cleanSource.length - 1] === ">") {
  34981. return `\0${cleanSource}`;
  34982. }
  34983. return normalizePath$3(path$n.resolve(inputFileDir, cleanSource));
  34984. });
  34985. return {
  34986. file,
  34987. mappings: rawMap.mappings,
  34988. names: rawMap.names,
  34989. sources,
  34990. sourcesContent: rawMap.sourcesContent,
  34991. version: rawMap.version
  34992. };
  34993. }
  34994. function combineSourcemapsIfExists(filename, map1, map2) {
  34995. return map1 && map2 ? combineSourcemaps(filename, [
  34996. // type of version property of ExistingRawSourceMap is number
  34997. // but it is always 3
  34998. map1,
  34999. map2
  35000. ]) : map1;
  35001. }
  35002. const viteHashUpdateMarker = "/*$vite$:1*/";
  35003. const viteHashUpdateMarkerRE = /\/\*\$vite\$:\d+\*\//;
  35004. async function finalizeCss(css, minify, config) {
  35005. if (css.includes("@import") || css.includes("@charset")) {
  35006. css = await hoistAtRules(css);
  35007. }
  35008. if (config.build.cssMinify) {
  35009. css = await minifyCSS(css, config, false);
  35010. }
  35011. css += viteHashUpdateMarker;
  35012. return css;
  35013. }
  35014. async function resolvePostcssConfig(config) {
  35015. let result = postcssConfigCache.get(config);
  35016. if (result !== void 0) {
  35017. return await result;
  35018. }
  35019. const inlineOptions = config.css?.postcss;
  35020. if (isObject$1(inlineOptions)) {
  35021. const options = { ...inlineOptions };
  35022. delete options.plugins;
  35023. result = {
  35024. options,
  35025. plugins: inlineOptions.plugins || []
  35026. };
  35027. } else {
  35028. const searchPath = typeof inlineOptions === "string" ? inlineOptions : config.root;
  35029. result = postcssrc({}, searchPath).catch((e) => {
  35030. if (!e.message.includes("No PostCSS Config found")) {
  35031. if (e instanceof Error) {
  35032. const { name, message, stack } = e;
  35033. e.name = "Failed to load PostCSS config";
  35034. e.message = `Failed to load PostCSS config (searchPath: ${searchPath}): [${name}] ${message}
  35035. ${stack}`;
  35036. e.stack = "";
  35037. throw e;
  35038. } else {
  35039. throw new Error(`Failed to load PostCSS config: ${e}`);
  35040. }
  35041. }
  35042. return null;
  35043. });
  35044. result.then((resolved) => {
  35045. postcssConfigCache.set(config, resolved);
  35046. });
  35047. }
  35048. postcssConfigCache.set(config, result);
  35049. return result;
  35050. }
  35051. const cssUrlRE = /(?<=^|[^\w\-\u0080-\uffff])url\((\s*('[^']+'|"[^"]+")\s*|[^'")]+)\)/;
  35052. const cssDataUriRE = /(?<=^|[^\w\-\u0080-\uffff])data-uri\((\s*('[^']+'|"[^"]+")\s*|[^'")]+)\)/;
  35053. const importCssRE = /@import ('[^']+\.css'|"[^"]+\.css"|[^'")]+\.css)/;
  35054. const cssImageSetRE = /(?<=image-set\()((?:[\w\-]{1,256}\([^)]*\)|[^)])*)(?=\))/;
  35055. const UrlRewritePostcssPlugin = (opts) => {
  35056. if (!opts) {
  35057. throw new Error("base or replace is required");
  35058. }
  35059. return {
  35060. postcssPlugin: "vite-url-rewrite",
  35061. Once(root) {
  35062. const promises = [];
  35063. root.walkDecls((declaration) => {
  35064. const importer = declaration.source?.input.file;
  35065. if (!importer) {
  35066. opts.logger.warnOnce(
  35067. "\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."
  35068. );
  35069. }
  35070. const isCssUrl = cssUrlRE.test(declaration.value);
  35071. const isCssImageSet = cssImageSetRE.test(declaration.value);
  35072. if (isCssUrl || isCssImageSet) {
  35073. const replacerForDeclaration = (rawUrl) => {
  35074. return opts.replacer(rawUrl, importer);
  35075. };
  35076. const rewriterToUse = isCssImageSet ? rewriteCssImageSet : rewriteCssUrls;
  35077. promises.push(
  35078. rewriterToUse(declaration.value, replacerForDeclaration).then(
  35079. (url) => {
  35080. declaration.value = url;
  35081. }
  35082. )
  35083. );
  35084. }
  35085. });
  35086. if (promises.length) {
  35087. return Promise.all(promises);
  35088. }
  35089. }
  35090. };
  35091. };
  35092. UrlRewritePostcssPlugin.postcss = true;
  35093. function rewriteCssUrls(css, replacer) {
  35094. return asyncReplace(css, cssUrlRE, async (match) => {
  35095. const [matched, rawUrl] = match;
  35096. return await doUrlReplace(rawUrl.trim(), matched, replacer);
  35097. });
  35098. }
  35099. function rewriteCssDataUris(css, replacer) {
  35100. return asyncReplace(css, cssDataUriRE, async (match) => {
  35101. const [matched, rawUrl] = match;
  35102. return await doUrlReplace(rawUrl.trim(), matched, replacer, "data-uri");
  35103. });
  35104. }
  35105. function rewriteImportCss(css, replacer) {
  35106. return asyncReplace(css, importCssRE, async (match) => {
  35107. const [matched, rawUrl] = match;
  35108. return await doImportCSSReplace(rawUrl, matched, replacer);
  35109. });
  35110. }
  35111. const cssNotProcessedRE = /(?:gradient|element|cross-fade|image)\(/;
  35112. async function rewriteCssImageSet(css, replacer) {
  35113. return await asyncReplace(css, cssImageSetRE, async (match) => {
  35114. const [, rawUrl] = match;
  35115. const url = await processSrcSet(rawUrl, async ({ url: url2 }) => {
  35116. if (cssUrlRE.test(url2)) {
  35117. return await rewriteCssUrls(url2, replacer);
  35118. }
  35119. if (!cssNotProcessedRE.test(url2)) {
  35120. return await doUrlReplace(url2, url2, replacer);
  35121. }
  35122. return url2;
  35123. });
  35124. return url;
  35125. });
  35126. }
  35127. function skipUrlReplacer(rawUrl) {
  35128. return isExternalUrl(rawUrl) || isDataUrl(rawUrl) || rawUrl[0] === "#" || functionCallRE.test(rawUrl);
  35129. }
  35130. async function doUrlReplace(rawUrl, matched, replacer, funcName = "url") {
  35131. let wrap = "";
  35132. const first = rawUrl[0];
  35133. if (first === `"` || first === `'`) {
  35134. wrap = first;
  35135. rawUrl = rawUrl.slice(1, -1);
  35136. }
  35137. if (skipUrlReplacer(rawUrl)) {
  35138. return matched;
  35139. }
  35140. let newUrl = await replacer(rawUrl);
  35141. if (wrap === "" && newUrl !== encodeURI(newUrl)) {
  35142. wrap = '"';
  35143. }
  35144. if (wrap === "'" && newUrl.includes("'")) {
  35145. wrap = '"';
  35146. }
  35147. if (wrap === '"' && newUrl.includes('"')) {
  35148. newUrl = newUrl.replace(nonEscapedDoubleQuoteRe, '\\"');
  35149. }
  35150. return `${funcName}(${wrap}${newUrl}${wrap})`;
  35151. }
  35152. async function doImportCSSReplace(rawUrl, matched, replacer) {
  35153. let wrap = "";
  35154. const first = rawUrl[0];
  35155. if (first === `"` || first === `'`) {
  35156. wrap = first;
  35157. rawUrl = rawUrl.slice(1, -1);
  35158. }
  35159. if (isExternalUrl(rawUrl) || isDataUrl(rawUrl) || rawUrl[0] === "#") {
  35160. return matched;
  35161. }
  35162. return `@import ${wrap}${await replacer(rawUrl)}${wrap}`;
  35163. }
  35164. async function minifyCSS(css, config, inlined) {
  35165. if (config.build.cssMinify === "lightningcss") {
  35166. const { code, warnings } = (await importLightningCSS()).transform({
  35167. ...config.css?.lightningcss,
  35168. targets: convertTargets(config.build.cssTarget),
  35169. cssModules: void 0,
  35170. filename: cssBundleName,
  35171. code: Buffer.from(css),
  35172. minify: true
  35173. });
  35174. if (warnings.length) {
  35175. config.logger.warn(
  35176. colors$1.yellow(
  35177. `warnings when minifying css:
  35178. ${warnings.map((w) => w.message).join("\n")}`
  35179. )
  35180. );
  35181. }
  35182. return decoder.decode(code) + (inlined ? "" : "\n");
  35183. }
  35184. try {
  35185. const { code, warnings } = await transform$1(css, {
  35186. loader: "css",
  35187. target: config.build.cssTarget || void 0,
  35188. ...resolveMinifyCssEsbuildOptions(config.esbuild || {})
  35189. });
  35190. if (warnings.length) {
  35191. const msgs = await formatMessages(warnings, { kind: "warning" });
  35192. config.logger.warn(
  35193. colors$1.yellow(`warnings when minifying css:
  35194. ${msgs.join("\n")}`)
  35195. );
  35196. }
  35197. return inlined ? code.trimEnd() : code;
  35198. } catch (e) {
  35199. if (e.errors) {
  35200. e.message = "[esbuild css minify] " + e.message;
  35201. const msgs = await formatMessages(e.errors, { kind: "error" });
  35202. e.frame = "\n" + msgs.join("\n");
  35203. e.loc = e.errors[0].location;
  35204. }
  35205. throw e;
  35206. }
  35207. }
  35208. function resolveMinifyCssEsbuildOptions(options) {
  35209. const base = {
  35210. charset: options.charset ?? "utf8",
  35211. logLevel: options.logLevel,
  35212. logLimit: options.logLimit,
  35213. logOverride: options.logOverride,
  35214. legalComments: options.legalComments
  35215. };
  35216. if (options.minifyIdentifiers != null || options.minifySyntax != null || options.minifyWhitespace != null) {
  35217. return {
  35218. ...base,
  35219. minifyIdentifiers: options.minifyIdentifiers ?? true,
  35220. minifySyntax: options.minifySyntax ?? true,
  35221. minifyWhitespace: options.minifyWhitespace ?? true
  35222. };
  35223. } else {
  35224. return { ...base, minify: true };
  35225. }
  35226. }
  35227. const atImportRE = /@import(?:\s*(?:url\([^)]*\)|"(?:[^"]|(?<=\\)")*"|'(?:[^']|(?<=\\)')*').*?|[^;]*);/g;
  35228. const atCharsetRE = /@charset(?:\s*(?:"(?:[^"]|(?<=\\)")*"|'(?:[^']|(?<=\\)')*').*?|[^;]*);/g;
  35229. async function hoistAtRules(css) {
  35230. const s = new MagicString(css);
  35231. const cleanCss = emptyCssComments(css);
  35232. let match;
  35233. atImportRE.lastIndex = 0;
  35234. while (match = atImportRE.exec(cleanCss)) {
  35235. s.remove(match.index, match.index + match[0].length);
  35236. s.appendLeft(0, match[0]);
  35237. }
  35238. atCharsetRE.lastIndex = 0;
  35239. let foundCharset = false;
  35240. while (match = atCharsetRE.exec(cleanCss)) {
  35241. s.remove(match.index, match.index + match[0].length);
  35242. if (!foundCharset) {
  35243. s.prepend(match[0]);
  35244. foundCharset = true;
  35245. }
  35246. }
  35247. return s.toString();
  35248. }
  35249. const loadedPreprocessorPath = {};
  35250. function loadPreprocessorPath(lang, root) {
  35251. const cached = loadedPreprocessorPath[lang];
  35252. if (cached) {
  35253. return cached;
  35254. }
  35255. try {
  35256. const resolved = requireResolveFromRootWithFallback(root, lang);
  35257. return loadedPreprocessorPath[lang] = resolved;
  35258. } catch (e) {
  35259. if (e.code === "MODULE_NOT_FOUND") {
  35260. const installCommand = getPackageManagerCommand("install");
  35261. throw new Error(
  35262. `Preprocessor dependency "${lang}" not found. Did you install it? Try \`${installCommand} -D ${lang}\`.`
  35263. );
  35264. } else {
  35265. const message = new Error(
  35266. `Preprocessor dependency "${lang}" failed to load:
  35267. ${e.message}`
  35268. );
  35269. message.stack = e.stack + "\n" + message.stack;
  35270. throw message;
  35271. }
  35272. }
  35273. }
  35274. function loadSassPackage(root) {
  35275. try {
  35276. const path2 = loadPreprocessorPath("sass-embedded", root);
  35277. return { name: "sass-embedded", path: path2 };
  35278. } catch (e1) {
  35279. try {
  35280. const path2 = loadPreprocessorPath("sass" /* sass */, root);
  35281. return { name: "sass", path: path2 };
  35282. } catch (e2) {
  35283. throw e1;
  35284. }
  35285. }
  35286. }
  35287. let cachedSss;
  35288. function loadSss(root) {
  35289. if (cachedSss) return cachedSss;
  35290. const sssPath = loadPreprocessorPath("sugarss" /* sss */, root);
  35291. cachedSss = createRequire$1(import.meta.url)(sssPath);
  35292. return cachedSss;
  35293. }
  35294. function cleanScssBugUrl(url) {
  35295. if (
  35296. // check bug via `window` and `location` global
  35297. typeof window !== "undefined" && typeof location !== "undefined" && typeof location?.href === "string"
  35298. ) {
  35299. const prefix = location.href.replace(/\/$/, "");
  35300. return url.replace(prefix, "");
  35301. } else {
  35302. return url;
  35303. }
  35304. }
  35305. function fixScssBugImportValue(data) {
  35306. if (
  35307. // check bug via `window` and `location` global
  35308. typeof window !== "undefined" && typeof location !== "undefined" && data && "file" in data && (!("contents" in data) || data.contents == null)
  35309. ) {
  35310. data.contents = fs__default.readFileSync(data.file, "utf-8");
  35311. }
  35312. return data;
  35313. }
  35314. const makeScssWorker = (resolvers, alias, maxWorkers, packageName) => {
  35315. const internalImporter = async (url, importer, filename) => {
  35316. importer = cleanScssBugUrl(importer);
  35317. const resolved = await resolvers.sass(url, importer);
  35318. if (resolved) {
  35319. try {
  35320. const data = await rebaseUrls(
  35321. resolved,
  35322. filename,
  35323. alias,
  35324. "$",
  35325. resolvers.sass
  35326. );
  35327. if (packageName === "sass-embedded") {
  35328. return data;
  35329. }
  35330. return fixScssBugImportValue(data);
  35331. } catch (data) {
  35332. return data;
  35333. }
  35334. } else {
  35335. return null;
  35336. }
  35337. };
  35338. const worker = new WorkerWithFallback(
  35339. () => async (sassPath, data, options) => {
  35340. const sass = require(sassPath);
  35341. const path2 = require("node:path");
  35342. const _internalImporter = (url, importer2, done) => {
  35343. internalImporter(url, importer2, options.filename).then(
  35344. (data2) => done?.(data2)
  35345. );
  35346. };
  35347. const importer = [_internalImporter];
  35348. if (options.importer) {
  35349. Array.isArray(options.importer) ? importer.unshift(...options.importer) : importer.unshift(options.importer);
  35350. }
  35351. const finalOptions = {
  35352. ...options,
  35353. data,
  35354. file: options.filename,
  35355. outFile: options.filename,
  35356. importer,
  35357. ...options.enableSourcemap ? {
  35358. sourceMap: true,
  35359. omitSourceMapUrl: true,
  35360. sourceMapRoot: path2.dirname(options.filename)
  35361. } : {}
  35362. };
  35363. return new Promise((resolve, reject) => {
  35364. sass.render(finalOptions, (err, res) => {
  35365. if (err) {
  35366. reject(err);
  35367. } else {
  35368. resolve({
  35369. css: res.css.toString(),
  35370. map: res.map?.toString(),
  35371. stats: res.stats
  35372. });
  35373. }
  35374. });
  35375. });
  35376. },
  35377. {
  35378. parentFunctions: { internalImporter },
  35379. shouldUseFake(_sassPath, _data, options) {
  35380. return !!(options.functions && Object.keys(options.functions).length > 0 || options.importer && (!Array.isArray(options.importer) || options.importer.length > 0));
  35381. },
  35382. max: maxWorkers
  35383. }
  35384. );
  35385. return worker;
  35386. };
  35387. const makeModernScssWorker = (resolvers, alias, maxWorkers) => {
  35388. const internalCanonicalize = async (url, importer) => {
  35389. importer = cleanScssBugUrl(importer);
  35390. const resolved = await resolvers.sass(url, importer);
  35391. return resolved ?? null;
  35392. };
  35393. const internalLoad = async (file, rootFile) => {
  35394. const result = await rebaseUrls(file, rootFile, alias, "$", resolvers.sass);
  35395. if (result.contents) {
  35396. return result.contents;
  35397. }
  35398. return await fsp.readFile(result.file, "utf-8");
  35399. };
  35400. const worker = new WorkerWithFallback(
  35401. () => async (sassPath, data, options) => {
  35402. const sass = require(sassPath);
  35403. const path2 = require("node:path");
  35404. const { fileURLToPath: fileURLToPath2, pathToFileURL: pathToFileURL2 } = (
  35405. // eslint-disable-next-line no-restricted-globals
  35406. require("node:url")
  35407. );
  35408. const sassOptions = { ...options };
  35409. sassOptions.url = pathToFileURL2(options.filename);
  35410. sassOptions.sourceMap = options.enableSourcemap;
  35411. const internalImporter = {
  35412. async canonicalize(url, context) {
  35413. const importer = context.containingUrl ? fileURLToPath2(context.containingUrl) : options.filename;
  35414. const resolved = await internalCanonicalize(url, importer);
  35415. return resolved ? pathToFileURL2(resolved) : null;
  35416. },
  35417. async load(canonicalUrl) {
  35418. const ext = path2.extname(canonicalUrl.pathname);
  35419. let syntax = "scss";
  35420. if (ext === ".sass") {
  35421. syntax = "indented";
  35422. } else if (ext === ".css") {
  35423. syntax = "css";
  35424. }
  35425. const contents = await internalLoad(
  35426. fileURLToPath2(canonicalUrl),
  35427. options.filename
  35428. );
  35429. return { contents, syntax, sourceMapUrl: canonicalUrl };
  35430. }
  35431. };
  35432. sassOptions.importers = [
  35433. ...sassOptions.importers ?? [],
  35434. internalImporter
  35435. ];
  35436. const result = await sass.compileStringAsync(data, sassOptions);
  35437. return {
  35438. css: result.css,
  35439. map: result.sourceMap ? JSON.stringify(result.sourceMap) : void 0,
  35440. stats: {
  35441. includedFiles: result.loadedUrls.filter((url) => url.protocol === "file:").map((url) => fileURLToPath2(url))
  35442. }
  35443. };
  35444. },
  35445. {
  35446. parentFunctions: {
  35447. internalCanonicalize,
  35448. internalLoad
  35449. },
  35450. shouldUseFake(_sassPath, _data, options) {
  35451. return !!(options.functions && Object.keys(options.functions).length > 0 || options.importers && (!Array.isArray(options.importers) || options.importers.length > 0));
  35452. },
  35453. max: maxWorkers
  35454. }
  35455. );
  35456. return worker;
  35457. };
  35458. const makeModernCompilerScssWorker = (resolvers, alias, _maxWorkers) => {
  35459. let compilerPromise;
  35460. const worker = {
  35461. async run(sassPath, data, options) {
  35462. const sass = (await import(pathToFileURL(sassPath).href)).default;
  35463. compilerPromise ??= sass.initAsyncCompiler();
  35464. const compiler = await compilerPromise;
  35465. const sassOptions = { ...options };
  35466. sassOptions.url = pathToFileURL(options.filename);
  35467. sassOptions.sourceMap = options.enableSourcemap;
  35468. const internalImporter = {
  35469. async canonicalize(url, context) {
  35470. const importer = context.containingUrl ? fileURLToPath(context.containingUrl) : options.filename;
  35471. const resolved = await resolvers.sass(url, cleanScssBugUrl(importer));
  35472. return resolved ? pathToFileURL(resolved) : null;
  35473. },
  35474. async load(canonicalUrl) {
  35475. const ext = path$n.extname(canonicalUrl.pathname);
  35476. let syntax = "scss";
  35477. if (ext === ".sass") {
  35478. syntax = "indented";
  35479. } else if (ext === ".css") {
  35480. syntax = "css";
  35481. }
  35482. const result2 = await rebaseUrls(
  35483. fileURLToPath(canonicalUrl),
  35484. options.filename,
  35485. alias,
  35486. "$",
  35487. resolvers.sass
  35488. );
  35489. const contents = result2.contents ?? await fsp.readFile(result2.file, "utf-8");
  35490. return { contents, syntax, sourceMapUrl: canonicalUrl };
  35491. }
  35492. };
  35493. sassOptions.importers = [
  35494. ...sassOptions.importers ?? [],
  35495. internalImporter
  35496. ];
  35497. const result = await compiler.compileStringAsync(data, sassOptions);
  35498. return {
  35499. css: result.css,
  35500. map: result.sourceMap ? JSON.stringify(result.sourceMap) : void 0,
  35501. stats: {
  35502. includedFiles: result.loadedUrls.filter((url) => url.protocol === "file:").map((url) => fileURLToPath(url))
  35503. }
  35504. };
  35505. },
  35506. async stop() {
  35507. (await compilerPromise)?.dispose();
  35508. compilerPromise = void 0;
  35509. }
  35510. };
  35511. return worker;
  35512. };
  35513. const scssProcessor = (maxWorkers) => {
  35514. const workerMap = /* @__PURE__ */ new Map();
  35515. return {
  35516. close() {
  35517. for (const worker of workerMap.values()) {
  35518. worker.stop();
  35519. }
  35520. },
  35521. async process(source, root, options, resolvers) {
  35522. const sassPackage = loadSassPackage(root);
  35523. const api = options.api ?? "legacy";
  35524. if (!workerMap.has(options.alias)) {
  35525. workerMap.set(
  35526. options.alias,
  35527. api === "modern-compiler" ? makeModernCompilerScssWorker(resolvers, options.alias) : api === "modern" ? makeModernScssWorker(resolvers, options.alias, maxWorkers) : makeScssWorker(
  35528. resolvers,
  35529. options.alias,
  35530. maxWorkers,
  35531. sassPackage.name
  35532. )
  35533. );
  35534. }
  35535. const worker = workerMap.get(options.alias);
  35536. const { content: data, map: additionalMap } = await getSource(
  35537. source,
  35538. options.filename,
  35539. options.additionalData,
  35540. options.enableSourcemap
  35541. );
  35542. const optionsWithoutAdditionalData = {
  35543. ...options,
  35544. additionalData: void 0
  35545. };
  35546. try {
  35547. const result = await worker.run(
  35548. sassPackage.path,
  35549. data,
  35550. optionsWithoutAdditionalData
  35551. );
  35552. const deps = result.stats.includedFiles.map((f) => cleanScssBugUrl(f));
  35553. const map2 = result.map ? JSON.parse(result.map.toString()) : void 0;
  35554. if (map2) {
  35555. map2.sources = map2.sources.map(
  35556. (url) => url.startsWith("file://") ? normalizePath$3(fileURLToPath(url)) : url
  35557. );
  35558. }
  35559. return {
  35560. code: result.css.toString(),
  35561. map: map2,
  35562. additionalMap,
  35563. deps
  35564. };
  35565. } catch (e) {
  35566. e.message = `[sass] ${e.message}`;
  35567. e.id = e.file;
  35568. e.frame = e.formatted;
  35569. return { code: "", error: e, deps: [] };
  35570. }
  35571. }
  35572. };
  35573. };
  35574. async function rebaseUrls(file, rootFile, alias, variablePrefix, resolver) {
  35575. file = path$n.resolve(file);
  35576. const fileDir = path$n.dirname(file);
  35577. const rootDir = path$n.dirname(rootFile);
  35578. if (fileDir === rootDir) {
  35579. return { file };
  35580. }
  35581. const content = await fsp.readFile(file, "utf-8");
  35582. const hasUrls = cssUrlRE.test(content);
  35583. const hasDataUris = cssDataUriRE.test(content);
  35584. const hasImportCss = importCssRE.test(content);
  35585. if (!hasUrls && !hasDataUris && !hasImportCss) {
  35586. return { file };
  35587. }
  35588. let rebased;
  35589. const rebaseFn = async (url) => {
  35590. if (url[0] === "/") return url;
  35591. if (url.startsWith(variablePrefix)) return url;
  35592. for (const { find } of alias) {
  35593. const matches = typeof find === "string" ? url.startsWith(find) : find.test(url);
  35594. if (matches) {
  35595. return url;
  35596. }
  35597. }
  35598. const absolute = await resolver(url, file) || path$n.resolve(fileDir, url);
  35599. const relative = path$n.relative(rootDir, absolute);
  35600. return normalizePath$3(relative);
  35601. };
  35602. if (hasImportCss) {
  35603. rebased = await rewriteImportCss(content, rebaseFn);
  35604. }
  35605. if (hasUrls) {
  35606. rebased = await rewriteCssUrls(rebased || content, rebaseFn);
  35607. }
  35608. if (hasDataUris) {
  35609. rebased = await rewriteCssDataUris(rebased || content, rebaseFn);
  35610. }
  35611. return {
  35612. file,
  35613. contents: rebased
  35614. };
  35615. }
  35616. const makeLessWorker = (resolvers, alias, maxWorkers) => {
  35617. const viteLessResolve = async (filename, dir, rootFile) => {
  35618. const resolved = await resolvers.less(filename, path$n.join(dir, "*"));
  35619. if (!resolved) return void 0;
  35620. const result = await rebaseUrls(
  35621. resolved,
  35622. rootFile,
  35623. alias,
  35624. "@",
  35625. resolvers.less
  35626. );
  35627. if (result) {
  35628. return {
  35629. resolved,
  35630. contents: "contents" in result ? result.contents : void 0
  35631. };
  35632. }
  35633. return result;
  35634. };
  35635. const worker = new WorkerWithFallback(
  35636. () => {
  35637. const fsp2 = require("node:fs/promises");
  35638. const path2 = require("node:path");
  35639. let ViteLessManager;
  35640. const createViteLessPlugin = (less, rootFile) => {
  35641. const { FileManager } = less;
  35642. ViteLessManager ??= class ViteManager extends FileManager {
  35643. rootFile;
  35644. constructor(rootFile2) {
  35645. super();
  35646. this.rootFile = rootFile2;
  35647. }
  35648. supports(filename) {
  35649. return !/^(?:https?:)?\/\//.test(filename);
  35650. }
  35651. supportsSync() {
  35652. return false;
  35653. }
  35654. async loadFile(filename, dir, opts, env) {
  35655. const result = await viteLessResolve(filename, dir, this.rootFile);
  35656. if (result) {
  35657. return {
  35658. filename: path2.resolve(result.resolved),
  35659. contents: result.contents ?? await fsp2.readFile(result.resolved, "utf-8")
  35660. };
  35661. } else {
  35662. return super.loadFile(filename, dir, opts, env);
  35663. }
  35664. }
  35665. };
  35666. return {
  35667. install(_, pluginManager) {
  35668. pluginManager.addFileManager(new ViteLessManager(rootFile));
  35669. },
  35670. minVersion: [3, 0, 0]
  35671. };
  35672. };
  35673. return async (lessPath, content, options) => {
  35674. const nodeLess = require(lessPath);
  35675. const viteResolverPlugin = createViteLessPlugin(
  35676. nodeLess,
  35677. options.filename
  35678. );
  35679. const result = await nodeLess.render(content, {
  35680. ...options,
  35681. plugins: [viteResolverPlugin, ...options.plugins || []],
  35682. ...options.enableSourcemap ? {
  35683. sourceMap: {
  35684. outputSourceFiles: true,
  35685. sourceMapFileInline: false
  35686. }
  35687. } : {}
  35688. });
  35689. return result;
  35690. };
  35691. },
  35692. {
  35693. parentFunctions: { viteLessResolve },
  35694. shouldUseFake(_lessPath, _content, options) {
  35695. return options.plugins?.length > 0;
  35696. },
  35697. max: maxWorkers
  35698. }
  35699. );
  35700. return worker;
  35701. };
  35702. const lessProcessor = (maxWorkers) => {
  35703. const workerMap = /* @__PURE__ */ new Map();
  35704. return {
  35705. close() {
  35706. for (const worker of workerMap.values()) {
  35707. worker.stop();
  35708. }
  35709. },
  35710. async process(source, root, options, resolvers) {
  35711. const lessPath = loadPreprocessorPath("less" /* less */, root);
  35712. if (!workerMap.has(options.alias)) {
  35713. workerMap.set(
  35714. options.alias,
  35715. makeLessWorker(resolvers, options.alias, maxWorkers)
  35716. );
  35717. }
  35718. const worker = workerMap.get(options.alias);
  35719. const { content, map: additionalMap } = await getSource(
  35720. source,
  35721. options.filename,
  35722. options.additionalData,
  35723. options.enableSourcemap
  35724. );
  35725. let result;
  35726. const optionsWithoutAdditionalData = {
  35727. ...options,
  35728. additionalData: void 0
  35729. };
  35730. try {
  35731. result = await worker.run(
  35732. lessPath,
  35733. content,
  35734. optionsWithoutAdditionalData
  35735. );
  35736. } catch (e) {
  35737. const error = e;
  35738. const normalizedError = new Error(
  35739. `[less] ${error.message || error.type}`
  35740. );
  35741. normalizedError.loc = {
  35742. file: error.filename || options.filename,
  35743. line: error.line,
  35744. column: error.column
  35745. };
  35746. return { code: "", error: normalizedError, deps: [] };
  35747. }
  35748. const map2 = result.map && JSON.parse(result.map);
  35749. if (map2) {
  35750. delete map2.sourcesContent;
  35751. }
  35752. return {
  35753. code: result.css.toString(),
  35754. map: map2,
  35755. additionalMap,
  35756. deps: result.imports
  35757. };
  35758. }
  35759. };
  35760. };
  35761. const makeStylWorker = (maxWorkers) => {
  35762. const worker = new WorkerWithFallback(
  35763. () => {
  35764. return async (stylusPath, content, root, options) => {
  35765. const nodeStylus = require(stylusPath);
  35766. const ref = nodeStylus(content, options);
  35767. if (options.define) {
  35768. for (const key in options.define) {
  35769. ref.define(key, options.define[key]);
  35770. }
  35771. }
  35772. if (options.enableSourcemap) {
  35773. ref.set("sourcemap", {
  35774. comment: false,
  35775. inline: false,
  35776. basePath: root
  35777. });
  35778. }
  35779. return {
  35780. code: ref.render(),
  35781. // @ts-expect-error sourcemap exists
  35782. map: ref.sourcemap,
  35783. deps: ref.deps()
  35784. };
  35785. };
  35786. },
  35787. {
  35788. shouldUseFake(_stylusPath, _content, _root, options) {
  35789. return !!(options.define && Object.values(options.define).some((d) => typeof d === "function"));
  35790. },
  35791. max: maxWorkers
  35792. }
  35793. );
  35794. return worker;
  35795. };
  35796. const stylProcessor = (maxWorkers) => {
  35797. const workerMap = /* @__PURE__ */ new Map();
  35798. return {
  35799. close() {
  35800. for (const worker of workerMap.values()) {
  35801. worker.stop();
  35802. }
  35803. },
  35804. async process(source, root, options, resolvers) {
  35805. const stylusPath = loadPreprocessorPath("stylus" /* stylus */, root);
  35806. if (!workerMap.has(options.alias)) {
  35807. workerMap.set(options.alias, makeStylWorker(maxWorkers));
  35808. }
  35809. const worker = workerMap.get(options.alias);
  35810. const { content, map: additionalMap } = await getSource(
  35811. source,
  35812. options.filename,
  35813. options.additionalData,
  35814. options.enableSourcemap,
  35815. "\n"
  35816. );
  35817. const importsDeps = (options.imports ?? []).map(
  35818. (dep) => path$n.resolve(dep)
  35819. );
  35820. const optionsWithoutAdditionalData = {
  35821. ...options,
  35822. additionalData: void 0
  35823. };
  35824. try {
  35825. const { code, map: map2, deps } = await worker.run(
  35826. stylusPath,
  35827. content,
  35828. root,
  35829. optionsWithoutAdditionalData
  35830. );
  35831. return {
  35832. code,
  35833. map: formatStylusSourceMap(map2, root),
  35834. additionalMap,
  35835. // Concat imports deps with computed deps
  35836. deps: [...deps, ...importsDeps]
  35837. };
  35838. } catch (e) {
  35839. const wrapped = new Error(`[stylus] ${e.message}`);
  35840. wrapped.name = e.name;
  35841. wrapped.stack = e.stack;
  35842. return { code: "", error: wrapped, deps: [] };
  35843. }
  35844. }
  35845. };
  35846. };
  35847. function formatStylusSourceMap(mapBefore, root) {
  35848. if (!mapBefore) return void 0;
  35849. const map2 = { ...mapBefore };
  35850. const resolveFromRoot = (p) => normalizePath$3(path$n.resolve(root, p));
  35851. if (map2.file) {
  35852. map2.file = resolveFromRoot(map2.file);
  35853. }
  35854. map2.sources = map2.sources.map(resolveFromRoot);
  35855. return map2;
  35856. }
  35857. async function getSource(source, filename, additionalData, enableSourcemap, sep = "") {
  35858. if (!additionalData) return { content: source };
  35859. if (typeof additionalData === "function") {
  35860. const newContent = await additionalData(source, filename);
  35861. if (typeof newContent === "string") {
  35862. return { content: newContent };
  35863. }
  35864. return newContent;
  35865. }
  35866. if (!enableSourcemap) {
  35867. return { content: additionalData + sep + source };
  35868. }
  35869. const ms = new MagicString(source);
  35870. ms.appendLeft(0, sep);
  35871. ms.appendLeft(0, additionalData);
  35872. const map2 = ms.generateMap({ hires: "boundary" });
  35873. map2.file = filename;
  35874. map2.sources = [filename];
  35875. return {
  35876. content: ms.toString(),
  35877. map: map2
  35878. };
  35879. }
  35880. const createPreprocessorWorkerController = (maxWorkers) => {
  35881. const scss = scssProcessor(maxWorkers);
  35882. const less = lessProcessor(maxWorkers);
  35883. const styl = stylProcessor(maxWorkers);
  35884. const sassProcess = (source, root, options, resolvers) => {
  35885. return scss.process(
  35886. source,
  35887. root,
  35888. { ...options, indentedSyntax: true, syntax: "indented" },
  35889. resolvers
  35890. );
  35891. };
  35892. const close = () => {
  35893. less.close();
  35894. scss.close();
  35895. styl.close();
  35896. };
  35897. return {
  35898. ["less" /* less */]: less.process,
  35899. ["scss" /* scss */]: scss.process,
  35900. ["sass" /* sass */]: sassProcess,
  35901. ["styl" /* styl */]: styl.process,
  35902. ["stylus" /* stylus */]: styl.process,
  35903. close
  35904. };
  35905. };
  35906. const normalizeMaxWorkers = (maxWorker) => {
  35907. if (maxWorker === void 0) return 0;
  35908. if (maxWorker === true) return void 0;
  35909. return maxWorker;
  35910. };
  35911. const preprocessorSet = /* @__PURE__ */ new Set([
  35912. "less" /* less */,
  35913. "sass" /* sass */,
  35914. "scss" /* scss */,
  35915. "styl" /* styl */,
  35916. "stylus" /* stylus */
  35917. ]);
  35918. function isPreProcessor(lang) {
  35919. return lang && preprocessorSet.has(lang);
  35920. }
  35921. const importLightningCSS = createCachedImport(() => import('lightningcss'));
  35922. async function compileLightningCSS(id, src, config, urlReplacer) {
  35923. const deps = /* @__PURE__ */ new Set();
  35924. const filename = cleanUrl(path$n.relative(config.root, id));
  35925. const toAbsolute = (filePath) => path$n.isAbsolute(filePath) ? filePath : path$n.join(config.root, filePath);
  35926. const res = styleAttrRE.test(id) ? (await importLightningCSS()).transformStyleAttribute({
  35927. filename,
  35928. code: Buffer.from(src),
  35929. targets: config.css?.lightningcss?.targets,
  35930. minify: config.isProduction && !!config.build.cssMinify,
  35931. analyzeDependencies: true
  35932. }) : await (await importLightningCSS()).bundleAsync({
  35933. ...config.css?.lightningcss,
  35934. filename,
  35935. resolver: {
  35936. read(filePath) {
  35937. if (filePath === filename) {
  35938. return src;
  35939. }
  35940. if (!filePath.endsWith(".css")) {
  35941. return src;
  35942. }
  35943. return fs__default.readFileSync(toAbsolute(filePath), "utf-8");
  35944. },
  35945. async resolve(id2, from) {
  35946. const publicFile = checkPublicFile(id2, config);
  35947. if (publicFile) {
  35948. return publicFile;
  35949. }
  35950. const resolved = await getAtImportResolvers(config).css(
  35951. id2,
  35952. toAbsolute(from)
  35953. );
  35954. if (resolved) {
  35955. deps.add(resolved);
  35956. return resolved;
  35957. }
  35958. return id2;
  35959. }
  35960. },
  35961. minify: config.isProduction && !!config.build.cssMinify,
  35962. sourceMap: config.command === "build" ? !!config.build.sourcemap : config.css?.devSourcemap,
  35963. analyzeDependencies: true,
  35964. cssModules: cssModuleRE.test(id) ? config.css?.lightningcss?.cssModules ?? true : void 0
  35965. });
  35966. let css = decoder.decode(res.code);
  35967. for (const dep of res.dependencies) {
  35968. switch (dep.type) {
  35969. case "url":
  35970. if (skipUrlReplacer(dep.url)) {
  35971. css = css.replace(dep.placeholder, () => dep.url);
  35972. break;
  35973. }
  35974. deps.add(dep.url);
  35975. if (urlReplacer) {
  35976. const replaceUrl = await urlReplacer(
  35977. dep.url,
  35978. toAbsolute(dep.loc.filePath)
  35979. );
  35980. css = css.replace(dep.placeholder, () => replaceUrl);
  35981. } else {
  35982. css = css.replace(dep.placeholder, () => dep.url);
  35983. }
  35984. break;
  35985. default:
  35986. throw new Error(`Unsupported dependency type: ${dep.type}`);
  35987. }
  35988. }
  35989. let modules;
  35990. if ("exports" in res && res.exports) {
  35991. modules = {};
  35992. const sortedEntries = Object.entries(res.exports).sort(
  35993. (a, b) => a[0].localeCompare(b[0])
  35994. );
  35995. for (const [key, value] of sortedEntries) {
  35996. modules[key] = value.name;
  35997. for (const c of value.composes) {
  35998. modules[key] += " " + c.name;
  35999. }
  36000. }
  36001. }
  36002. return {
  36003. code: css,
  36004. map: "map" in res ? res.map?.toString() : void 0,
  36005. deps,
  36006. modules
  36007. };
  36008. }
  36009. const map = {
  36010. chrome: "chrome",
  36011. edge: "edge",
  36012. firefox: "firefox",
  36013. hermes: false,
  36014. ie: "ie",
  36015. ios: "ios_saf",
  36016. node: false,
  36017. opera: "opera",
  36018. rhino: false,
  36019. safari: "safari"
  36020. };
  36021. const esMap = {
  36022. // https://caniuse.com/?search=es2015
  36023. 2015: ["chrome49", "edge13", "safari10", "firefox44", "opera36"],
  36024. // https://caniuse.com/?search=es2016
  36025. 2016: ["chrome50", "edge13", "safari10", "firefox43", "opera37"],
  36026. // https://caniuse.com/?search=es2017
  36027. 2017: ["chrome58", "edge15", "safari11", "firefox52", "opera45"],
  36028. // https://caniuse.com/?search=es2018
  36029. 2018: ["chrome63", "edge79", "safari12", "firefox58", "opera50"],
  36030. // https://caniuse.com/?search=es2019
  36031. 2019: ["chrome73", "edge79", "safari12.1", "firefox64", "opera60"],
  36032. // https://caniuse.com/?search=es2020
  36033. 2020: ["chrome80", "edge80", "safari14.1", "firefox80", "opera67"],
  36034. // https://caniuse.com/?search=es2021
  36035. 2021: ["chrome85", "edge85", "safari14.1", "firefox80", "opera71"],
  36036. // https://caniuse.com/?search=es2022
  36037. 2022: ["chrome94", "edge94", "safari16.4", "firefox93", "opera80"]
  36038. };
  36039. const esRE = /es(\d{4})/;
  36040. const versionRE = /\d/;
  36041. const convertTargetsCache = /* @__PURE__ */ new Map();
  36042. const convertTargets = (esbuildTarget) => {
  36043. if (!esbuildTarget) return {};
  36044. const cached = convertTargetsCache.get(esbuildTarget);
  36045. if (cached) return cached;
  36046. const targets = {};
  36047. const entriesWithoutES = arraify(esbuildTarget).flatMap((e) => {
  36048. const match = esRE.exec(e);
  36049. if (!match) return e;
  36050. const year = Number(match[1]);
  36051. if (!esMap[year]) throw new Error(`Unsupported target "${e}"`);
  36052. return esMap[year];
  36053. });
  36054. for (const entry of entriesWithoutES) {
  36055. if (entry === "esnext") continue;
  36056. const index = entry.search(versionRE);
  36057. if (index >= 0) {
  36058. const browser = map[entry.slice(0, index)];
  36059. if (browser === false) continue;
  36060. if (browser) {
  36061. const [major, minor = 0] = entry.slice(index).split(".").map((v) => parseInt(v, 10));
  36062. if (!isNaN(major) && !isNaN(minor)) {
  36063. const version = major << 16 | minor << 8;
  36064. if (!targets[browser] || version < targets[browser]) {
  36065. targets[browser] = version;
  36066. }
  36067. continue;
  36068. }
  36069. }
  36070. }
  36071. throw new Error(`Unsupported target "${entry}"`);
  36072. }
  36073. convertTargetsCache.set(esbuildTarget, targets);
  36074. return targets;
  36075. };
  36076. const HASH_RE = /#/g;
  36077. const AMPERSAND_RE = /&/g;
  36078. const SLASH_RE = /\//g;
  36079. const EQUAL_RE = /=/g;
  36080. const PLUS_RE = /\+/g;
  36081. const ENC_CARET_RE = /%5e/gi;
  36082. const ENC_BACKTICK_RE = /%60/gi;
  36083. const ENC_PIPE_RE = /%7c/gi;
  36084. const ENC_SPACE_RE = /%20/gi;
  36085. function encode(text) {
  36086. return encodeURI("" + text).replace(ENC_PIPE_RE, "|");
  36087. }
  36088. function encodeQueryValue(input) {
  36089. 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");
  36090. }
  36091. function encodeQueryKey(text) {
  36092. return encodeQueryValue(text).replace(EQUAL_RE, "%3D");
  36093. }
  36094. function encodeQueryItem(key, value) {
  36095. if (typeof value === "number" || typeof value === "boolean") {
  36096. value = String(value);
  36097. }
  36098. if (!value) {
  36099. return encodeQueryKey(key);
  36100. }
  36101. if (Array.isArray(value)) {
  36102. return value.map((_value) => `${encodeQueryKey(key)}=${encodeQueryValue(_value)}`).join("&");
  36103. }
  36104. return `${encodeQueryKey(key)}=${encodeQueryValue(value)}`;
  36105. }
  36106. function stringifyQuery(query) {
  36107. return Object.keys(query).filter((k) => query[k] !== void 0).map((k) => encodeQueryItem(k, query[k])).filter(Boolean).join("&");
  36108. }
  36109. new Set(builtinModules);
  36110. function clearImports(imports) {
  36111. return (imports || "").replace(/(\/\/[^\n]*\n|\/\*.*\*\/)/g, "").replace(/\s+/g, " ");
  36112. }
  36113. function getImportNames(cleanedImports) {
  36114. const topLevelImports = cleanedImports.replace(/{([^}]*)}/, "");
  36115. const namespacedImport = topLevelImports.match(/\* as \s*(\S*)/)?.[1];
  36116. const defaultImport = topLevelImports.split(",").find((index) => !/[*{}]/.test(index))?.trim() || void 0;
  36117. return {
  36118. namespacedImport,
  36119. defaultImport
  36120. };
  36121. }
  36122. /**
  36123. * @typedef ErrnoExceptionFields
  36124. * @property {number | undefined} [errnode]
  36125. * @property {string | undefined} [code]
  36126. * @property {string | undefined} [path]
  36127. * @property {string | undefined} [syscall]
  36128. * @property {string | undefined} [url]
  36129. *
  36130. * @typedef {Error & ErrnoExceptionFields} ErrnoException
  36131. */
  36132. const own$1 = {}.hasOwnProperty;
  36133. const classRegExp = /^([A-Z][a-z\d]*)+$/;
  36134. // Sorted by a rough estimate on most frequently used entries.
  36135. const kTypes = new Set([
  36136. 'string',
  36137. 'function',
  36138. 'number',
  36139. 'object',
  36140. // Accept 'Function' and 'Object' as alternative to the lower cased version.
  36141. 'Function',
  36142. 'Object',
  36143. 'boolean',
  36144. 'bigint',
  36145. 'symbol'
  36146. ]);
  36147. /**
  36148. * Create a list string in the form like 'A and B' or 'A, B, ..., and Z'.
  36149. * We cannot use Intl.ListFormat because it's not available in
  36150. * --without-intl builds.
  36151. *
  36152. * @param {Array<string>} array
  36153. * An array of strings.
  36154. * @param {string} [type]
  36155. * The list type to be inserted before the last element.
  36156. * @returns {string}
  36157. */
  36158. function formatList(array, type = 'and') {
  36159. return array.length < 3
  36160. ? array.join(` ${type} `)
  36161. : `${array.slice(0, -1).join(', ')}, ${type} ${array[array.length - 1]}`
  36162. }
  36163. /** @type {Map<string, MessageFunction | string>} */
  36164. const messages = new Map();
  36165. const nodeInternalPrefix = '__node_internal_';
  36166. /** @type {number} */
  36167. let userStackTraceLimit;
  36168. createError(
  36169. 'ERR_INVALID_ARG_TYPE',
  36170. /**
  36171. * @param {string} name
  36172. * @param {Array<string> | string} expected
  36173. * @param {unknown} actual
  36174. */
  36175. (name, expected, actual) => {
  36176. assert$1(typeof name === 'string', "'name' must be a string");
  36177. if (!Array.isArray(expected)) {
  36178. expected = [expected];
  36179. }
  36180. let message = 'The ';
  36181. if (name.endsWith(' argument')) {
  36182. // For cases like 'first argument'
  36183. message += `${name} `;
  36184. } else {
  36185. const type = name.includes('.') ? 'property' : 'argument';
  36186. message += `"${name}" ${type} `;
  36187. }
  36188. message += 'must be ';
  36189. /** @type {Array<string>} */
  36190. const types = [];
  36191. /** @type {Array<string>} */
  36192. const instances = [];
  36193. /** @type {Array<string>} */
  36194. const other = [];
  36195. for (const value of expected) {
  36196. assert$1(
  36197. typeof value === 'string',
  36198. 'All expected entries have to be of type string'
  36199. );
  36200. if (kTypes.has(value)) {
  36201. types.push(value.toLowerCase());
  36202. } else if (classRegExp.exec(value) === null) {
  36203. assert$1(
  36204. value !== 'object',
  36205. 'The value "object" should be written as "Object"'
  36206. );
  36207. other.push(value);
  36208. } else {
  36209. instances.push(value);
  36210. }
  36211. }
  36212. // Special handle `object` in case other instances are allowed to outline
  36213. // the differences between each other.
  36214. if (instances.length > 0) {
  36215. const pos = types.indexOf('object');
  36216. if (pos !== -1) {
  36217. types.slice(pos, 1);
  36218. instances.push('Object');
  36219. }
  36220. }
  36221. if (types.length > 0) {
  36222. message += `${types.length > 1 ? 'one of type' : 'of type'} ${formatList(
  36223. types,
  36224. 'or'
  36225. )}`;
  36226. if (instances.length > 0 || other.length > 0) message += ' or ';
  36227. }
  36228. if (instances.length > 0) {
  36229. message += `an instance of ${formatList(instances, 'or')}`;
  36230. if (other.length > 0) message += ' or ';
  36231. }
  36232. if (other.length > 0) {
  36233. if (other.length > 1) {
  36234. message += `one of ${formatList(other, 'or')}`;
  36235. } else {
  36236. if (other[0].toLowerCase() !== other[0]) message += 'an ';
  36237. message += `${other[0]}`;
  36238. }
  36239. }
  36240. message += `. Received ${determineSpecificType(actual)}`;
  36241. return message
  36242. },
  36243. TypeError
  36244. );
  36245. createError(
  36246. 'ERR_INVALID_MODULE_SPECIFIER',
  36247. /**
  36248. * @param {string} request
  36249. * @param {string} reason
  36250. * @param {string} [base]
  36251. */
  36252. (request, reason, base = undefined) => {
  36253. return `Invalid module "${request}" ${reason}${
  36254. base ? ` imported from ${base}` : ''
  36255. }`
  36256. },
  36257. TypeError
  36258. );
  36259. createError(
  36260. 'ERR_INVALID_PACKAGE_CONFIG',
  36261. /**
  36262. * @param {string} path
  36263. * @param {string} [base]
  36264. * @param {string} [message]
  36265. */
  36266. (path, base, message) => {
  36267. return `Invalid package config ${path}${
  36268. base ? ` while importing ${base}` : ''
  36269. }${message ? `. ${message}` : ''}`
  36270. },
  36271. Error
  36272. );
  36273. createError(
  36274. 'ERR_INVALID_PACKAGE_TARGET',
  36275. /**
  36276. * @param {string} packagePath
  36277. * @param {string} key
  36278. * @param {unknown} target
  36279. * @param {boolean} [isImport=false]
  36280. * @param {string} [base]
  36281. */
  36282. (packagePath, key, target, isImport = false, base = undefined) => {
  36283. const relatedError =
  36284. typeof target === 'string' &&
  36285. !isImport &&
  36286. target.length > 0 &&
  36287. !target.startsWith('./');
  36288. if (key === '.') {
  36289. assert$1(isImport === false);
  36290. return (
  36291. `Invalid "exports" main target ${JSON.stringify(target)} defined ` +
  36292. `in the package config ${packagePath}package.json${
  36293. base ? ` imported from ${base}` : ''
  36294. }${relatedError ? '; targets must start with "./"' : ''}`
  36295. )
  36296. }
  36297. return `Invalid "${
  36298. isImport ? 'imports' : 'exports'
  36299. }" target ${JSON.stringify(
  36300. target
  36301. )} defined for '${key}' in the package config ${packagePath}package.json${
  36302. base ? ` imported from ${base}` : ''
  36303. }${relatedError ? '; targets must start with "./"' : ''}`
  36304. },
  36305. Error
  36306. );
  36307. createError(
  36308. 'ERR_MODULE_NOT_FOUND',
  36309. /**
  36310. * @param {string} path
  36311. * @param {string} base
  36312. * @param {boolean} [exactUrl]
  36313. */
  36314. (path, base, exactUrl = false) => {
  36315. return `Cannot find ${
  36316. exactUrl ? 'module' : 'package'
  36317. } '${path}' imported from ${base}`
  36318. },
  36319. Error
  36320. );
  36321. createError(
  36322. 'ERR_NETWORK_IMPORT_DISALLOWED',
  36323. "import of '%s' by %s is not supported: %s",
  36324. Error
  36325. );
  36326. createError(
  36327. 'ERR_PACKAGE_IMPORT_NOT_DEFINED',
  36328. /**
  36329. * @param {string} specifier
  36330. * @param {string} packagePath
  36331. * @param {string} base
  36332. */
  36333. (specifier, packagePath, base) => {
  36334. return `Package import specifier "${specifier}" is not defined${
  36335. packagePath ? ` in package ${packagePath}package.json` : ''
  36336. } imported from ${base}`
  36337. },
  36338. TypeError
  36339. );
  36340. createError(
  36341. 'ERR_PACKAGE_PATH_NOT_EXPORTED',
  36342. /**
  36343. * @param {string} packagePath
  36344. * @param {string} subpath
  36345. * @param {string} [base]
  36346. */
  36347. (packagePath, subpath, base = undefined) => {
  36348. if (subpath === '.')
  36349. return `No "exports" main defined in ${packagePath}package.json${
  36350. base ? ` imported from ${base}` : ''
  36351. }`
  36352. return `Package subpath '${subpath}' is not defined by "exports" in ${packagePath}package.json${
  36353. base ? ` imported from ${base}` : ''
  36354. }`
  36355. },
  36356. Error
  36357. );
  36358. createError(
  36359. 'ERR_UNSUPPORTED_DIR_IMPORT',
  36360. "Directory import '%s' is not supported " +
  36361. 'resolving ES modules imported from %s',
  36362. Error
  36363. );
  36364. createError(
  36365. 'ERR_UNSUPPORTED_RESOLVE_REQUEST',
  36366. 'Failed to resolve module specifier "%s" from "%s": Invalid relative URL or base scheme is not hierarchical.',
  36367. TypeError
  36368. );
  36369. createError(
  36370. 'ERR_UNKNOWN_FILE_EXTENSION',
  36371. /**
  36372. * @param {string} extension
  36373. * @param {string} path
  36374. */
  36375. (extension, path) => {
  36376. return `Unknown file extension "${extension}" for ${path}`
  36377. },
  36378. TypeError
  36379. );
  36380. createError(
  36381. 'ERR_INVALID_ARG_VALUE',
  36382. /**
  36383. * @param {string} name
  36384. * @param {unknown} value
  36385. * @param {string} [reason='is invalid']
  36386. */
  36387. (name, value, reason = 'is invalid') => {
  36388. let inspected = inspect(value);
  36389. if (inspected.length > 128) {
  36390. inspected = `${inspected.slice(0, 128)}...`;
  36391. }
  36392. const type = name.includes('.') ? 'property' : 'argument';
  36393. return `The ${type} '${name}' ${reason}. Received ${inspected}`
  36394. },
  36395. TypeError
  36396. // Note: extra classes have been shaken out.
  36397. // , RangeError
  36398. );
  36399. /**
  36400. * Utility function for registering the error codes. Only used here. Exported
  36401. * *only* to allow for testing.
  36402. * @param {string} sym
  36403. * @param {MessageFunction | string} value
  36404. * @param {ErrorConstructor} constructor
  36405. * @returns {new (...parameters: Array<any>) => Error}
  36406. */
  36407. function createError(sym, value, constructor) {
  36408. // Special case for SystemError that formats the error message differently
  36409. // The SystemErrors only have SystemError as their base classes.
  36410. messages.set(sym, value);
  36411. return makeNodeErrorWithCode(constructor, sym)
  36412. }
  36413. /**
  36414. * @param {ErrorConstructor} Base
  36415. * @param {string} key
  36416. * @returns {ErrorConstructor}
  36417. */
  36418. function makeNodeErrorWithCode(Base, key) {
  36419. // @ts-expect-error It’s a Node error.
  36420. return NodeError
  36421. /**
  36422. * @param {Array<unknown>} parameters
  36423. */
  36424. function NodeError(...parameters) {
  36425. const limit = Error.stackTraceLimit;
  36426. if (isErrorStackTraceLimitWritable()) Error.stackTraceLimit = 0;
  36427. const error = new Base();
  36428. // Reset the limit and setting the name property.
  36429. if (isErrorStackTraceLimitWritable()) Error.stackTraceLimit = limit;
  36430. const message = getMessage(key, parameters, error);
  36431. Object.defineProperties(error, {
  36432. // Note: no need to implement `kIsNodeError` symbol, would be hard,
  36433. // probably.
  36434. message: {
  36435. value: message,
  36436. enumerable: false,
  36437. writable: true,
  36438. configurable: true
  36439. },
  36440. toString: {
  36441. /** @this {Error} */
  36442. value() {
  36443. return `${this.name} [${key}]: ${this.message}`
  36444. },
  36445. enumerable: false,
  36446. writable: true,
  36447. configurable: true
  36448. }
  36449. });
  36450. captureLargerStackTrace(error);
  36451. // @ts-expect-error It’s a Node error.
  36452. error.code = key;
  36453. return error
  36454. }
  36455. }
  36456. /**
  36457. * @returns {boolean}
  36458. */
  36459. function isErrorStackTraceLimitWritable() {
  36460. // Do no touch Error.stackTraceLimit as V8 would attempt to install
  36461. // it again during deserialization.
  36462. try {
  36463. if (v8.startupSnapshot.isBuildingSnapshot()) {
  36464. return false
  36465. }
  36466. } catch {}
  36467. const desc = Object.getOwnPropertyDescriptor(Error, 'stackTraceLimit');
  36468. if (desc === undefined) {
  36469. return Object.isExtensible(Error)
  36470. }
  36471. return own$1.call(desc, 'writable') && desc.writable !== undefined
  36472. ? desc.writable
  36473. : desc.set !== undefined
  36474. }
  36475. /**
  36476. * This function removes unnecessary frames from Node.js core errors.
  36477. * @template {(...parameters: unknown[]) => unknown} T
  36478. * @param {T} wrappedFunction
  36479. * @returns {T}
  36480. */
  36481. function hideStackFrames(wrappedFunction) {
  36482. // We rename the functions that will be hidden to cut off the stacktrace
  36483. // at the outermost one
  36484. const hidden = nodeInternalPrefix + wrappedFunction.name;
  36485. Object.defineProperty(wrappedFunction, 'name', {value: hidden});
  36486. return wrappedFunction
  36487. }
  36488. const captureLargerStackTrace = hideStackFrames(
  36489. /**
  36490. * @param {Error} error
  36491. * @returns {Error}
  36492. */
  36493. // @ts-expect-error: fine
  36494. function (error) {
  36495. const stackTraceLimitIsWritable = isErrorStackTraceLimitWritable();
  36496. if (stackTraceLimitIsWritable) {
  36497. userStackTraceLimit = Error.stackTraceLimit;
  36498. Error.stackTraceLimit = Number.POSITIVE_INFINITY;
  36499. }
  36500. Error.captureStackTrace(error);
  36501. // Reset the limit
  36502. if (stackTraceLimitIsWritable) Error.stackTraceLimit = userStackTraceLimit;
  36503. return error
  36504. }
  36505. );
  36506. /**
  36507. * @param {string} key
  36508. * @param {Array<unknown>} parameters
  36509. * @param {Error} self
  36510. * @returns {string}
  36511. */
  36512. function getMessage(key, parameters, self) {
  36513. const message = messages.get(key);
  36514. assert$1(message !== undefined, 'expected `message` to be found');
  36515. if (typeof message === 'function') {
  36516. assert$1(
  36517. message.length <= parameters.length, // Default options do not count.
  36518. `Code: ${key}; The provided arguments length (${parameters.length}) does not ` +
  36519. `match the required ones (${message.length}).`
  36520. );
  36521. return Reflect.apply(message, self, parameters)
  36522. }
  36523. const regex = /%[dfijoOs]/g;
  36524. let expectedLength = 0;
  36525. while (regex.exec(message) !== null) expectedLength++;
  36526. assert$1(
  36527. expectedLength === parameters.length,
  36528. `Code: ${key}; The provided arguments length (${parameters.length}) does not ` +
  36529. `match the required ones (${expectedLength}).`
  36530. );
  36531. if (parameters.length === 0) return message
  36532. parameters.unshift(message);
  36533. return Reflect.apply(format$2, null, parameters)
  36534. }
  36535. /**
  36536. * Determine the specific type of a value for type-mismatch errors.
  36537. * @param {unknown} value
  36538. * @returns {string}
  36539. */
  36540. function determineSpecificType(value) {
  36541. if (value === null || value === undefined) {
  36542. return String(value)
  36543. }
  36544. if (typeof value === 'function' && value.name) {
  36545. return `function ${value.name}`
  36546. }
  36547. if (typeof value === 'object') {
  36548. if (value.constructor && value.constructor.name) {
  36549. return `an instance of ${value.constructor.name}`
  36550. }
  36551. return `${inspect(value, {depth: -1})}`
  36552. }
  36553. let inspected = inspect(value, {colors: false});
  36554. if (inspected.length > 28) {
  36555. inspected = `${inspected.slice(0, 25)}...`;
  36556. }
  36557. return `type ${typeof value} (${inspected})`
  36558. }
  36559. 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;
  36560. const TYPE_RE = /^\s*?type\s/;
  36561. function parseStaticImport(matched) {
  36562. const cleanedImports = clearImports(matched.imports);
  36563. const namedImports = {};
  36564. const _matches = cleanedImports.match(/{([^}]*)}/)?.[1]?.split(",") || [];
  36565. for (const namedImport of _matches) {
  36566. const _match = namedImport.match(/^\s*(\S*) as (\S*)\s*$/);
  36567. const source = _match?.[1] || namedImport.trim();
  36568. const importName = _match?.[2] || source;
  36569. if (source && !TYPE_RE.test(source)) {
  36570. namedImports[source] = importName;
  36571. }
  36572. }
  36573. const { namespacedImport, defaultImport } = getImportNames(cleanedImports);
  36574. return {
  36575. ...matched,
  36576. defaultImport,
  36577. namespacedImport,
  36578. namedImports
  36579. };
  36580. }
  36581. 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;
  36582. const COMMENT_RE = /\/\*.+?\*\/|\/\/.*(?=[nr])/g;
  36583. function hasESMSyntax(code, opts = {}) {
  36584. if (opts.stripComments) {
  36585. code = code.replace(COMMENT_RE, "");
  36586. }
  36587. return ESM_RE.test(code);
  36588. }
  36589. const { isMatch: isMatch$1, scan } = micromatch$2;
  36590. function getAffectedGlobModules(file, server) {
  36591. const modules = [];
  36592. for (const [id, allGlobs] of server._importGlobMap) {
  36593. if (allGlobs.some(
  36594. ({ affirmed, negated }) => (!affirmed.length || affirmed.some((glob) => isMatch$1(file, glob))) && (!negated.length || negated.every((glob) => isMatch$1(file, glob)))
  36595. )) {
  36596. const mod = server.moduleGraph.getModuleById(id);
  36597. if (mod) modules.push(mod);
  36598. }
  36599. }
  36600. modules.forEach((i) => {
  36601. if (i?.file) server.moduleGraph.onFileChange(i.file);
  36602. });
  36603. return modules;
  36604. }
  36605. function importGlobPlugin(config) {
  36606. let server;
  36607. return {
  36608. name: "vite:import-glob",
  36609. configureServer(_server) {
  36610. server = _server;
  36611. server._importGlobMap.clear();
  36612. },
  36613. async transform(code, id) {
  36614. if (!code.includes("import.meta.glob")) return;
  36615. const result = await transformGlobImport(
  36616. code,
  36617. id,
  36618. config.root,
  36619. (im, _, options) => this.resolve(im, id, options).then((i) => i?.id || im),
  36620. config.experimental.importGlobRestoreExtension,
  36621. config.logger
  36622. );
  36623. if (result) {
  36624. if (server) {
  36625. const allGlobs = result.matches.map((i) => i.globsResolved);
  36626. server._importGlobMap.set(
  36627. id,
  36628. allGlobs.map((globs) => {
  36629. const affirmed = [];
  36630. const negated = [];
  36631. for (const glob of globs) {
  36632. (glob[0] === "!" ? negated : affirmed).push(glob);
  36633. }
  36634. return { affirmed, negated };
  36635. })
  36636. );
  36637. }
  36638. return transformStableResult(result.s, id, config);
  36639. }
  36640. }
  36641. };
  36642. }
  36643. const importGlobRE = /\bimport\.meta\.glob(?:<\w+>)?\s*\(/g;
  36644. const knownOptions = {
  36645. as: ["string"],
  36646. eager: ["boolean"],
  36647. import: ["string"],
  36648. exhaustive: ["boolean"],
  36649. query: ["object", "string"]
  36650. };
  36651. const forceDefaultAs = ["raw", "url"];
  36652. function err$1(e, pos) {
  36653. const error = new Error(e);
  36654. error.pos = pos;
  36655. return error;
  36656. }
  36657. function parseGlobOptions(rawOpts, optsStartIndex, logger) {
  36658. let opts = {};
  36659. try {
  36660. opts = evalValue(rawOpts);
  36661. } catch {
  36662. throw err$1(
  36663. "Vite is unable to parse the glob options as the value is not static",
  36664. optsStartIndex
  36665. );
  36666. }
  36667. if (opts == null) {
  36668. return {};
  36669. }
  36670. for (const key in opts) {
  36671. if (!(key in knownOptions)) {
  36672. throw err$1(`Unknown glob option "${key}"`, optsStartIndex);
  36673. }
  36674. const allowedTypes = knownOptions[key];
  36675. const valueType = typeof opts[key];
  36676. if (!allowedTypes.includes(valueType)) {
  36677. throw err$1(
  36678. `Expected glob option "${key}" to be of type ${allowedTypes.join(
  36679. " or "
  36680. )}, but got ${valueType}`,
  36681. optsStartIndex
  36682. );
  36683. }
  36684. }
  36685. if (typeof opts.query === "object") {
  36686. for (const key in opts.query) {
  36687. const value = opts.query[key];
  36688. if (!["string", "number", "boolean"].includes(typeof value)) {
  36689. throw err$1(
  36690. `Expected glob option "query.${key}" to be of type string, number, or boolean, but got ${typeof value}`,
  36691. optsStartIndex
  36692. );
  36693. }
  36694. }
  36695. opts.query = stringifyQuery(opts.query);
  36696. }
  36697. if (opts.as && logger) {
  36698. const importSuggestion = forceDefaultAs.includes(opts.as) ? `, import: 'default'` : "";
  36699. logger.warn(
  36700. colors$1.yellow(
  36701. `The glob option "as" has been deprecated in favour of "query". Please update \`as: '${opts.as}'\` to \`query: '?${opts.as}'${importSuggestion}\`.`
  36702. )
  36703. );
  36704. }
  36705. if (opts.as && forceDefaultAs.includes(opts.as)) {
  36706. if (opts.import && opts.import !== "default" && opts.import !== "*")
  36707. throw err$1(
  36708. `Option "import" can only be "default" or "*" when "as" is "${opts.as}", but got "${opts.import}"`,
  36709. optsStartIndex
  36710. );
  36711. opts.import = opts.import || "default";
  36712. }
  36713. if (opts.as && opts.query)
  36714. throw err$1(
  36715. 'Options "as" and "query" cannot be used together',
  36716. optsStartIndex
  36717. );
  36718. if (opts.as) opts.query = opts.as;
  36719. if (opts.query && opts.query[0] !== "?") opts.query = `?${opts.query}`;
  36720. return opts;
  36721. }
  36722. async function parseImportGlob(code, importer, root, resolveId, logger) {
  36723. let cleanCode;
  36724. try {
  36725. cleanCode = stripLiteral(code);
  36726. } catch (e) {
  36727. return [];
  36728. }
  36729. const matches = Array.from(cleanCode.matchAll(importGlobRE));
  36730. const tasks = matches.map(async (match, index) => {
  36731. const start = match.index;
  36732. const err2 = (msg) => {
  36733. const e = new Error(`Invalid glob import syntax: ${msg}`);
  36734. e.pos = start;
  36735. return e;
  36736. };
  36737. const end = findCorrespondingCloseParenthesisPosition(
  36738. cleanCode,
  36739. start + match[0].length
  36740. ) + 1;
  36741. if (end <= 0) {
  36742. throw err2("Close parenthesis not found");
  36743. }
  36744. const statementCode = code.slice(start, end);
  36745. const rootAst = (await parseAstAsync(statementCode)).body[0];
  36746. if (rootAst.type !== "ExpressionStatement") {
  36747. throw err2(`Expect CallExpression, got ${rootAst.type}`);
  36748. }
  36749. const ast = rootAst.expression;
  36750. if (ast.type !== "CallExpression") {
  36751. throw err2(`Expect CallExpression, got ${ast.type}`);
  36752. }
  36753. if (ast.arguments.length < 1 || ast.arguments.length > 2)
  36754. throw err2(`Expected 1-2 arguments, but got ${ast.arguments.length}`);
  36755. const arg1 = ast.arguments[0];
  36756. const arg2 = ast.arguments[1];
  36757. const globs = [];
  36758. const validateLiteral = (element) => {
  36759. if (!element) return;
  36760. if (element.type === "Literal") {
  36761. if (typeof element.value !== "string")
  36762. throw err2(
  36763. `Expected glob to be a string, but got "${typeof element.value}"`
  36764. );
  36765. globs.push(element.value);
  36766. } else if (element.type === "TemplateLiteral") {
  36767. if (element.expressions.length !== 0) {
  36768. throw err2(
  36769. `Expected glob to be a string, but got dynamic template literal`
  36770. );
  36771. }
  36772. globs.push(element.quasis[0].value.raw);
  36773. } else {
  36774. throw err2("Could only use literals");
  36775. }
  36776. };
  36777. if (arg1.type === "ArrayExpression") {
  36778. for (const element of arg1.elements) {
  36779. validateLiteral(element);
  36780. }
  36781. } else {
  36782. validateLiteral(arg1);
  36783. }
  36784. let options = {};
  36785. if (arg2) {
  36786. if (arg2.type !== "ObjectExpression")
  36787. throw err2(
  36788. `Expected the second argument to be an object literal, but got "${arg2.type}"`
  36789. );
  36790. options = parseGlobOptions(
  36791. code.slice(start + arg2.start, start + arg2.end),
  36792. start + arg2.start,
  36793. logger
  36794. );
  36795. }
  36796. const globsResolved = await Promise.all(
  36797. globs.map((glob) => toAbsoluteGlob(glob, root, importer, resolveId))
  36798. );
  36799. const isRelative = globs.every((i) => ".!".includes(i[0]));
  36800. return {
  36801. index,
  36802. globs,
  36803. globsResolved,
  36804. isRelative,
  36805. options,
  36806. start,
  36807. end
  36808. };
  36809. });
  36810. return (await Promise.all(tasks)).filter(Boolean);
  36811. }
  36812. function findCorrespondingCloseParenthesisPosition(cleanCode, openPos) {
  36813. const closePos = cleanCode.indexOf(")", openPos);
  36814. if (closePos < 0) return -1;
  36815. if (!cleanCode.slice(openPos, closePos).includes("(")) return closePos;
  36816. let remainingParenthesisCount = 0;
  36817. const cleanCodeLen = cleanCode.length;
  36818. for (let pos = openPos; pos < cleanCodeLen; pos++) {
  36819. switch (cleanCode[pos]) {
  36820. case "(": {
  36821. remainingParenthesisCount++;
  36822. break;
  36823. }
  36824. case ")": {
  36825. remainingParenthesisCount--;
  36826. if (remainingParenthesisCount <= 0) {
  36827. return pos;
  36828. }
  36829. }
  36830. }
  36831. }
  36832. return -1;
  36833. }
  36834. const importPrefix = "__vite_glob_";
  36835. const { basename, dirname, relative, join } = posix$1;
  36836. async function transformGlobImport(code, id, root, resolveId, restoreQueryExtension = false, logger) {
  36837. id = slash$1(id);
  36838. root = slash$1(root);
  36839. const isVirtual = isVirtualModule(id);
  36840. const dir = isVirtual ? void 0 : dirname(id);
  36841. const matches = await parseImportGlob(
  36842. code,
  36843. isVirtual ? void 0 : id,
  36844. root,
  36845. resolveId,
  36846. logger
  36847. );
  36848. const matchedFiles = /* @__PURE__ */ new Set();
  36849. if (!matches.length) return null;
  36850. const s = new MagicString(code);
  36851. const staticImports = (await Promise.all(
  36852. matches.map(
  36853. async ({ globsResolved, isRelative, options, index, start, end }) => {
  36854. const cwd = getCommonBase(globsResolved) ?? root;
  36855. const files = (await glob(globsResolved, {
  36856. cwd,
  36857. absolute: true,
  36858. dot: !!options.exhaustive,
  36859. ignore: options.exhaustive ? [] : [join(cwd, "**/node_modules/**")]
  36860. })).filter((file) => file !== id).sort();
  36861. const objectProps = [];
  36862. const staticImports2 = [];
  36863. const resolvePaths = (file) => {
  36864. if (!dir) {
  36865. if (isRelative)
  36866. throw new Error(
  36867. "In virtual modules, all globs must start with '/'"
  36868. );
  36869. const filePath2 = `/${relative(root, file)}`;
  36870. return { filePath: filePath2, importPath: filePath2 };
  36871. }
  36872. let importPath = relative(dir, file);
  36873. if (importPath[0] !== ".") importPath = `./${importPath}`;
  36874. let filePath;
  36875. if (isRelative) {
  36876. filePath = importPath;
  36877. } else {
  36878. filePath = relative(root, file);
  36879. if (filePath[0] !== ".") filePath = `/${filePath}`;
  36880. }
  36881. return { filePath, importPath };
  36882. };
  36883. files.forEach((file, i) => {
  36884. const paths = resolvePaths(file);
  36885. const filePath = paths.filePath;
  36886. let importPath = paths.importPath;
  36887. let importQuery = options.query ?? "";
  36888. if (importQuery && importQuery !== "?raw") {
  36889. const fileExtension = basename(file).split(".").slice(-1)[0];
  36890. if (fileExtension && restoreQueryExtension)
  36891. importQuery = `${importQuery}&lang.${fileExtension}`;
  36892. }
  36893. importPath = `${importPath}${importQuery}`;
  36894. const importKey = options.import && options.import !== "*" ? options.import : void 0;
  36895. if (options.eager) {
  36896. const variableName = `${importPrefix}${index}_${i}`;
  36897. const expression = importKey ? `{ ${importKey} as ${variableName} }` : `* as ${variableName}`;
  36898. staticImports2.push(
  36899. `import ${expression} from ${JSON.stringify(importPath)}`
  36900. );
  36901. objectProps.push(`${JSON.stringify(filePath)}: ${variableName}`);
  36902. } else {
  36903. let importStatement = `import(${JSON.stringify(importPath)})`;
  36904. if (importKey)
  36905. importStatement += `.then(m => m[${JSON.stringify(importKey)}])`;
  36906. objectProps.push(
  36907. `${JSON.stringify(filePath)}: () => ${importStatement}`
  36908. );
  36909. }
  36910. });
  36911. files.forEach((i) => matchedFiles.add(i));
  36912. const originalLineBreakCount = code.slice(start, end).match(/\n/g)?.length ?? 0;
  36913. const lineBreaks = originalLineBreakCount > 0 ? "\n".repeat(originalLineBreakCount) : "";
  36914. const replacement = `/* #__PURE__ */ Object.assign({${objectProps.join(
  36915. ","
  36916. )}${lineBreaks}})`;
  36917. s.overwrite(start, end, replacement);
  36918. return staticImports2;
  36919. }
  36920. )
  36921. )).flat();
  36922. if (staticImports.length) s.prepend(`${staticImports.join(";")};`);
  36923. return {
  36924. s,
  36925. matches,
  36926. files: matchedFiles
  36927. };
  36928. }
  36929. function globSafePath(path) {
  36930. return glob.escapePath(normalizePath$3(path));
  36931. }
  36932. function lastNthChar(str, n) {
  36933. return str.charAt(str.length - 1 - n);
  36934. }
  36935. function globSafeResolvedPath(resolved, glob) {
  36936. let numEqual = 0;
  36937. const maxEqual = Math.min(resolved.length, glob.length);
  36938. while (numEqual < maxEqual && lastNthChar(resolved, numEqual) === lastNthChar(glob, numEqual)) {
  36939. numEqual += 1;
  36940. }
  36941. const staticPartEnd = resolved.length - numEqual;
  36942. const staticPart = resolved.slice(0, staticPartEnd);
  36943. const dynamicPart = resolved.slice(staticPartEnd);
  36944. return globSafePath(staticPart) + dynamicPart;
  36945. }
  36946. async function toAbsoluteGlob(glob, root, importer, resolveId) {
  36947. let pre = "";
  36948. if (glob[0] === "!") {
  36949. pre = "!";
  36950. glob = glob.slice(1);
  36951. }
  36952. root = globSafePath(root);
  36953. const dir = importer ? globSafePath(dirname(importer)) : root;
  36954. if (glob[0] === "/") return pre + posix$1.join(root, glob.slice(1));
  36955. if (glob.startsWith("./")) return pre + posix$1.join(dir, glob.slice(2));
  36956. if (glob.startsWith("../")) return pre + posix$1.join(dir, glob);
  36957. if (glob.startsWith("**")) return pre + glob;
  36958. const isSubImportsPattern = glob[0] === "#" && glob.includes("*");
  36959. const resolved = normalizePath$3(
  36960. await resolveId(glob, importer, {
  36961. custom: { "vite:import-glob": { isSubImportsPattern } }
  36962. }) || glob
  36963. );
  36964. if (isAbsolute$1(resolved)) {
  36965. return pre + globSafeResolvedPath(resolved, glob);
  36966. }
  36967. throw new Error(
  36968. `Invalid glob: "${glob}" (resolved: "${resolved}"). It must start with '/' or './'`
  36969. );
  36970. }
  36971. function getCommonBase(globsResolved) {
  36972. const bases = globsResolved.filter((g) => g[0] !== "!").map((glob) => {
  36973. let { base } = scan(glob);
  36974. if (posix$1.basename(base).includes(".")) base = posix$1.dirname(base);
  36975. return base;
  36976. });
  36977. if (!bases.length) return null;
  36978. let commonAncestor = "";
  36979. const dirS = bases[0].split("/");
  36980. for (let i = 0; i < dirS.length; i++) {
  36981. const candidate = dirS.slice(0, i + 1).join("/");
  36982. if (bases.every((base) => base.startsWith(candidate)))
  36983. commonAncestor = candidate;
  36984. else break;
  36985. }
  36986. if (!commonAncestor) commonAncestor = "/";
  36987. return commonAncestor;
  36988. }
  36989. function isVirtualModule(id) {
  36990. return id.startsWith("virtual:") || id[0] === "\0" || !id.includes("/");
  36991. }
  36992. var src = {exports: {}};
  36993. var browser = {exports: {}};
  36994. var debug$f = {exports: {}};
  36995. /**
  36996. * Helpers.
  36997. */
  36998. var ms;
  36999. var hasRequiredMs;
  37000. function requireMs () {
  37001. if (hasRequiredMs) return ms;
  37002. hasRequiredMs = 1;
  37003. var s = 1000;
  37004. var m = s * 60;
  37005. var h = m * 60;
  37006. var d = h * 24;
  37007. var y = d * 365.25;
  37008. /**
  37009. * Parse or format the given `val`.
  37010. *
  37011. * Options:
  37012. *
  37013. * - `long` verbose formatting [false]
  37014. *
  37015. * @param {String|Number} val
  37016. * @param {Object} [options]
  37017. * @throws {Error} throw an error if val is not a non-empty string or a number
  37018. * @return {String|Number}
  37019. * @api public
  37020. */
  37021. ms = function(val, options) {
  37022. options = options || {};
  37023. var type = typeof val;
  37024. if (type === 'string' && val.length > 0) {
  37025. return parse(val);
  37026. } else if (type === 'number' && isNaN(val) === false) {
  37027. return options.long ? fmtLong(val) : fmtShort(val);
  37028. }
  37029. throw new Error(
  37030. 'val is not a non-empty string or a valid number. val=' +
  37031. JSON.stringify(val)
  37032. );
  37033. };
  37034. /**
  37035. * Parse the given `str` and return milliseconds.
  37036. *
  37037. * @param {String} str
  37038. * @return {Number}
  37039. * @api private
  37040. */
  37041. function parse(str) {
  37042. str = String(str);
  37043. if (str.length > 100) {
  37044. return;
  37045. }
  37046. var match = /^((?:\d+)?\.?\d+) *(milliseconds?|msecs?|ms|seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|years?|yrs?|y)?$/i.exec(
  37047. str
  37048. );
  37049. if (!match) {
  37050. return;
  37051. }
  37052. var n = parseFloat(match[1]);
  37053. var type = (match[2] || 'ms').toLowerCase();
  37054. switch (type) {
  37055. case 'years':
  37056. case 'year':
  37057. case 'yrs':
  37058. case 'yr':
  37059. case 'y':
  37060. return n * y;
  37061. case 'days':
  37062. case 'day':
  37063. case 'd':
  37064. return n * d;
  37065. case 'hours':
  37066. case 'hour':
  37067. case 'hrs':
  37068. case 'hr':
  37069. case 'h':
  37070. return n * h;
  37071. case 'minutes':
  37072. case 'minute':
  37073. case 'mins':
  37074. case 'min':
  37075. case 'm':
  37076. return n * m;
  37077. case 'seconds':
  37078. case 'second':
  37079. case 'secs':
  37080. case 'sec':
  37081. case 's':
  37082. return n * s;
  37083. case 'milliseconds':
  37084. case 'millisecond':
  37085. case 'msecs':
  37086. case 'msec':
  37087. case 'ms':
  37088. return n;
  37089. default:
  37090. return undefined;
  37091. }
  37092. }
  37093. /**
  37094. * Short format for `ms`.
  37095. *
  37096. * @param {Number} ms
  37097. * @return {String}
  37098. * @api private
  37099. */
  37100. function fmtShort(ms) {
  37101. if (ms >= d) {
  37102. return Math.round(ms / d) + 'd';
  37103. }
  37104. if (ms >= h) {
  37105. return Math.round(ms / h) + 'h';
  37106. }
  37107. if (ms >= m) {
  37108. return Math.round(ms / m) + 'm';
  37109. }
  37110. if (ms >= s) {
  37111. return Math.round(ms / s) + 's';
  37112. }
  37113. return ms + 'ms';
  37114. }
  37115. /**
  37116. * Long format for `ms`.
  37117. *
  37118. * @param {Number} ms
  37119. * @return {String}
  37120. * @api private
  37121. */
  37122. function fmtLong(ms) {
  37123. return plural(ms, d, 'day') ||
  37124. plural(ms, h, 'hour') ||
  37125. plural(ms, m, 'minute') ||
  37126. plural(ms, s, 'second') ||
  37127. ms + ' ms';
  37128. }
  37129. /**
  37130. * Pluralization helper.
  37131. */
  37132. function plural(ms, n, name) {
  37133. if (ms < n) {
  37134. return;
  37135. }
  37136. if (ms < n * 1.5) {
  37137. return Math.floor(ms / n) + ' ' + name;
  37138. }
  37139. return Math.ceil(ms / n) + ' ' + name + 's';
  37140. }
  37141. return ms;
  37142. }
  37143. var hasRequiredDebug;
  37144. function requireDebug () {
  37145. if (hasRequiredDebug) return debug$f.exports;
  37146. hasRequiredDebug = 1;
  37147. (function (module, exports) {
  37148. /**
  37149. * This is the common logic for both the Node.js and web browser
  37150. * implementations of `debug()`.
  37151. *
  37152. * Expose `debug()` as the module.
  37153. */
  37154. exports = module.exports = createDebug.debug = createDebug['default'] = createDebug;
  37155. exports.coerce = coerce;
  37156. exports.disable = disable;
  37157. exports.enable = enable;
  37158. exports.enabled = enabled;
  37159. exports.humanize = requireMs();
  37160. /**
  37161. * The currently active debug mode names, and names to skip.
  37162. */
  37163. exports.names = [];
  37164. exports.skips = [];
  37165. /**
  37166. * Map of special "%n" handling functions, for the debug "format" argument.
  37167. *
  37168. * Valid key names are a single, lower or upper-case letter, i.e. "n" and "N".
  37169. */
  37170. exports.formatters = {};
  37171. /**
  37172. * Previous log timestamp.
  37173. */
  37174. var prevTime;
  37175. /**
  37176. * Select a color.
  37177. * @param {String} namespace
  37178. * @return {Number}
  37179. * @api private
  37180. */
  37181. function selectColor(namespace) {
  37182. var hash = 0, i;
  37183. for (i in namespace) {
  37184. hash = ((hash << 5) - hash) + namespace.charCodeAt(i);
  37185. hash |= 0; // Convert to 32bit integer
  37186. }
  37187. return exports.colors[Math.abs(hash) % exports.colors.length];
  37188. }
  37189. /**
  37190. * Create a debugger with the given `namespace`.
  37191. *
  37192. * @param {String} namespace
  37193. * @return {Function}
  37194. * @api public
  37195. */
  37196. function createDebug(namespace) {
  37197. function debug() {
  37198. // disabled?
  37199. if (!debug.enabled) return;
  37200. var self = debug;
  37201. // set `diff` timestamp
  37202. var curr = +new Date();
  37203. var ms = curr - (prevTime || curr);
  37204. self.diff = ms;
  37205. self.prev = prevTime;
  37206. self.curr = curr;
  37207. prevTime = curr;
  37208. // turn the `arguments` into a proper Array
  37209. var args = new Array(arguments.length);
  37210. for (var i = 0; i < args.length; i++) {
  37211. args[i] = arguments[i];
  37212. }
  37213. args[0] = exports.coerce(args[0]);
  37214. if ('string' !== typeof args[0]) {
  37215. // anything else let's inspect with %O
  37216. args.unshift('%O');
  37217. }
  37218. // apply any `formatters` transformations
  37219. var index = 0;
  37220. args[0] = args[0].replace(/%([a-zA-Z%])/g, function(match, format) {
  37221. // if we encounter an escaped % then don't increase the array index
  37222. if (match === '%%') return match;
  37223. index++;
  37224. var formatter = exports.formatters[format];
  37225. if ('function' === typeof formatter) {
  37226. var val = args[index];
  37227. match = formatter.call(self, val);
  37228. // now we need to remove `args[index]` since it's inlined in the `format`
  37229. args.splice(index, 1);
  37230. index--;
  37231. }
  37232. return match;
  37233. });
  37234. // apply env-specific formatting (colors, etc.)
  37235. exports.formatArgs.call(self, args);
  37236. var logFn = debug.log || exports.log || console.log.bind(console);
  37237. logFn.apply(self, args);
  37238. }
  37239. debug.namespace = namespace;
  37240. debug.enabled = exports.enabled(namespace);
  37241. debug.useColors = exports.useColors();
  37242. debug.color = selectColor(namespace);
  37243. // env-specific initialization logic for debug instances
  37244. if ('function' === typeof exports.init) {
  37245. exports.init(debug);
  37246. }
  37247. return debug;
  37248. }
  37249. /**
  37250. * Enables a debug mode by namespaces. This can include modes
  37251. * separated by a colon and wildcards.
  37252. *
  37253. * @param {String} namespaces
  37254. * @api public
  37255. */
  37256. function enable(namespaces) {
  37257. exports.save(namespaces);
  37258. exports.names = [];
  37259. exports.skips = [];
  37260. var split = (typeof namespaces === 'string' ? namespaces : '').split(/[\s,]+/);
  37261. var len = split.length;
  37262. for (var i = 0; i < len; i++) {
  37263. if (!split[i]) continue; // ignore empty strings
  37264. namespaces = split[i].replace(/\*/g, '.*?');
  37265. if (namespaces[0] === '-') {
  37266. exports.skips.push(new RegExp('^' + namespaces.substr(1) + '$'));
  37267. } else {
  37268. exports.names.push(new RegExp('^' + namespaces + '$'));
  37269. }
  37270. }
  37271. }
  37272. /**
  37273. * Disable debug output.
  37274. *
  37275. * @api public
  37276. */
  37277. function disable() {
  37278. exports.enable('');
  37279. }
  37280. /**
  37281. * Returns true if the given mode name is enabled, false otherwise.
  37282. *
  37283. * @param {String} name
  37284. * @return {Boolean}
  37285. * @api public
  37286. */
  37287. function enabled(name) {
  37288. var i, len;
  37289. for (i = 0, len = exports.skips.length; i < len; i++) {
  37290. if (exports.skips[i].test(name)) {
  37291. return false;
  37292. }
  37293. }
  37294. for (i = 0, len = exports.names.length; i < len; i++) {
  37295. if (exports.names[i].test(name)) {
  37296. return true;
  37297. }
  37298. }
  37299. return false;
  37300. }
  37301. /**
  37302. * Coerce `val`.
  37303. *
  37304. * @param {Mixed} val
  37305. * @return {Mixed}
  37306. * @api private
  37307. */
  37308. function coerce(val) {
  37309. if (val instanceof Error) return val.stack || val.message;
  37310. return val;
  37311. }
  37312. } (debug$f, debug$f.exports));
  37313. return debug$f.exports;
  37314. }
  37315. /**
  37316. * This is the web browser implementation of `debug()`.
  37317. *
  37318. * Expose `debug()` as the module.
  37319. */
  37320. var hasRequiredBrowser;
  37321. function requireBrowser () {
  37322. if (hasRequiredBrowser) return browser.exports;
  37323. hasRequiredBrowser = 1;
  37324. (function (module, exports) {
  37325. exports = module.exports = requireDebug();
  37326. exports.log = log;
  37327. exports.formatArgs = formatArgs;
  37328. exports.save = save;
  37329. exports.load = load;
  37330. exports.useColors = useColors;
  37331. exports.storage = 'undefined' != typeof chrome
  37332. && 'undefined' != typeof chrome.storage
  37333. ? chrome.storage.local
  37334. : localstorage();
  37335. /**
  37336. * Colors.
  37337. */
  37338. exports.colors = [
  37339. 'lightseagreen',
  37340. 'forestgreen',
  37341. 'goldenrod',
  37342. 'dodgerblue',
  37343. 'darkorchid',
  37344. 'crimson'
  37345. ];
  37346. /**
  37347. * Currently only WebKit-based Web Inspectors, Firefox >= v31,
  37348. * and the Firebug extension (any Firefox version) are known
  37349. * to support "%c" CSS customizations.
  37350. *
  37351. * TODO: add a `localStorage` variable to explicitly enable/disable colors
  37352. */
  37353. function useColors() {
  37354. // NB: In an Electron preload script, document will be defined but not fully
  37355. // initialized. Since we know we're in Chrome, we'll just detect this case
  37356. // explicitly
  37357. if (typeof window !== 'undefined' && window.process && window.process.type === 'renderer') {
  37358. return true;
  37359. }
  37360. // is webkit? http://stackoverflow.com/a/16459606/376773
  37361. // document is undefined in react-native: https://github.com/facebook/react-native/pull/1632
  37362. return (typeof document !== 'undefined' && document.documentElement && document.documentElement.style && document.documentElement.style.WebkitAppearance) ||
  37363. // is firebug? http://stackoverflow.com/a/398120/376773
  37364. (typeof window !== 'undefined' && window.console && (window.console.firebug || (window.console.exception && window.console.table))) ||
  37365. // is firefox >= v31?
  37366. // https://developer.mozilla.org/en-US/docs/Tools/Web_Console#Styling_messages
  37367. (typeof navigator !== 'undefined' && navigator.userAgent && navigator.userAgent.toLowerCase().match(/firefox\/(\d+)/) && parseInt(RegExp.$1, 10) >= 31) ||
  37368. // double check webkit in userAgent just in case we are in a worker
  37369. (typeof navigator !== 'undefined' && navigator.userAgent && navigator.userAgent.toLowerCase().match(/applewebkit\/(\d+)/));
  37370. }
  37371. /**
  37372. * Map %j to `JSON.stringify()`, since no Web Inspectors do that by default.
  37373. */
  37374. exports.formatters.j = function(v) {
  37375. try {
  37376. return JSON.stringify(v);
  37377. } catch (err) {
  37378. return '[UnexpectedJSONParseError]: ' + err.message;
  37379. }
  37380. };
  37381. /**
  37382. * Colorize log arguments if enabled.
  37383. *
  37384. * @api public
  37385. */
  37386. function formatArgs(args) {
  37387. var useColors = this.useColors;
  37388. args[0] = (useColors ? '%c' : '')
  37389. + this.namespace
  37390. + (useColors ? ' %c' : ' ')
  37391. + args[0]
  37392. + (useColors ? '%c ' : ' ')
  37393. + '+' + exports.humanize(this.diff);
  37394. if (!useColors) return;
  37395. var c = 'color: ' + this.color;
  37396. args.splice(1, 0, c, 'color: inherit');
  37397. // the final "%c" is somewhat tricky, because there could be other
  37398. // arguments passed either before or after the %c, so we need to
  37399. // figure out the correct index to insert the CSS into
  37400. var index = 0;
  37401. var lastC = 0;
  37402. args[0].replace(/%[a-zA-Z%]/g, function(match) {
  37403. if ('%%' === match) return;
  37404. index++;
  37405. if ('%c' === match) {
  37406. // we only are interested in the *last* %c
  37407. // (the user may have provided their own)
  37408. lastC = index;
  37409. }
  37410. });
  37411. args.splice(lastC, 0, c);
  37412. }
  37413. /**
  37414. * Invokes `console.log()` when available.
  37415. * No-op when `console.log` is not a "function".
  37416. *
  37417. * @api public
  37418. */
  37419. function log() {
  37420. // this hackery is required for IE8/9, where
  37421. // the `console.log` function doesn't have 'apply'
  37422. return 'object' === typeof console
  37423. && console.log
  37424. && Function.prototype.apply.call(console.log, console, arguments);
  37425. }
  37426. /**
  37427. * Save `namespaces`.
  37428. *
  37429. * @param {String} namespaces
  37430. * @api private
  37431. */
  37432. function save(namespaces) {
  37433. try {
  37434. if (null == namespaces) {
  37435. exports.storage.removeItem('debug');
  37436. } else {
  37437. exports.storage.debug = namespaces;
  37438. }
  37439. } catch(e) {}
  37440. }
  37441. /**
  37442. * Load `namespaces`.
  37443. *
  37444. * @return {String} returns the previously persisted debug modes
  37445. * @api private
  37446. */
  37447. function load() {
  37448. var r;
  37449. try {
  37450. r = exports.storage.debug;
  37451. } catch(e) {}
  37452. // If debug isn't set in LS, and we're in Electron, try to load $DEBUG
  37453. if (!r && typeof process !== 'undefined' && 'env' in process) {
  37454. r = process.env.DEBUG;
  37455. }
  37456. return r;
  37457. }
  37458. /**
  37459. * Enable namespaces listed in `localStorage.debug` initially.
  37460. */
  37461. exports.enable(load());
  37462. /**
  37463. * Localstorage attempts to return the localstorage.
  37464. *
  37465. * This is necessary because safari throws
  37466. * when a user disables cookies/localstorage
  37467. * and you attempt to access it.
  37468. *
  37469. * @return {LocalStorage}
  37470. * @api private
  37471. */
  37472. function localstorage() {
  37473. try {
  37474. return window.localStorage;
  37475. } catch (e) {}
  37476. }
  37477. } (browser, browser.exports));
  37478. return browser.exports;
  37479. }
  37480. var node = {exports: {}};
  37481. /**
  37482. * Module dependencies.
  37483. */
  37484. var hasRequiredNode;
  37485. function requireNode () {
  37486. if (hasRequiredNode) return node.exports;
  37487. hasRequiredNode = 1;
  37488. (function (module, exports) {
  37489. var tty = require$$0$3;
  37490. var util = require$$0$5;
  37491. /**
  37492. * This is the Node.js implementation of `debug()`.
  37493. *
  37494. * Expose `debug()` as the module.
  37495. */
  37496. exports = module.exports = requireDebug();
  37497. exports.init = init;
  37498. exports.log = log;
  37499. exports.formatArgs = formatArgs;
  37500. exports.save = save;
  37501. exports.load = load;
  37502. exports.useColors = useColors;
  37503. /**
  37504. * Colors.
  37505. */
  37506. exports.colors = [6, 2, 3, 4, 5, 1];
  37507. /**
  37508. * Build up the default `inspectOpts` object from the environment variables.
  37509. *
  37510. * $ DEBUG_COLORS=no DEBUG_DEPTH=10 DEBUG_SHOW_HIDDEN=enabled node script.js
  37511. */
  37512. exports.inspectOpts = Object.keys(process.env).filter(function (key) {
  37513. return /^debug_/i.test(key);
  37514. }).reduce(function (obj, key) {
  37515. // camel-case
  37516. var prop = key
  37517. .substring(6)
  37518. .toLowerCase()
  37519. .replace(/_([a-z])/g, function (_, k) { return k.toUpperCase() });
  37520. // coerce string value into JS value
  37521. var val = process.env[key];
  37522. if (/^(yes|on|true|enabled)$/i.test(val)) val = true;
  37523. else if (/^(no|off|false|disabled)$/i.test(val)) val = false;
  37524. else if (val === 'null') val = null;
  37525. else val = Number(val);
  37526. obj[prop] = val;
  37527. return obj;
  37528. }, {});
  37529. /**
  37530. * The file descriptor to write the `debug()` calls to.
  37531. * Set the `DEBUG_FD` env variable to override with another value. i.e.:
  37532. *
  37533. * $ DEBUG_FD=3 node script.js 3>debug.log
  37534. */
  37535. var fd = parseInt(process.env.DEBUG_FD, 10) || 2;
  37536. if (1 !== fd && 2 !== fd) {
  37537. 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)')();
  37538. }
  37539. var stream = 1 === fd ? process.stdout :
  37540. 2 === fd ? process.stderr :
  37541. createWritableStdioStream(fd);
  37542. /**
  37543. * Is stdout a TTY? Colored output is enabled when `true`.
  37544. */
  37545. function useColors() {
  37546. return 'colors' in exports.inspectOpts
  37547. ? Boolean(exports.inspectOpts.colors)
  37548. : tty.isatty(fd);
  37549. }
  37550. /**
  37551. * Map %o to `util.inspect()`, all on a single line.
  37552. */
  37553. exports.formatters.o = function(v) {
  37554. this.inspectOpts.colors = this.useColors;
  37555. return util.inspect(v, this.inspectOpts)
  37556. .split('\n').map(function(str) {
  37557. return str.trim()
  37558. }).join(' ');
  37559. };
  37560. /**
  37561. * Map %o to `util.inspect()`, allowing multiple lines if needed.
  37562. */
  37563. exports.formatters.O = function(v) {
  37564. this.inspectOpts.colors = this.useColors;
  37565. return util.inspect(v, this.inspectOpts);
  37566. };
  37567. /**
  37568. * Adds ANSI color escape codes if enabled.
  37569. *
  37570. * @api public
  37571. */
  37572. function formatArgs(args) {
  37573. var name = this.namespace;
  37574. var useColors = this.useColors;
  37575. if (useColors) {
  37576. var c = this.color;
  37577. var prefix = ' \u001b[3' + c + ';1m' + name + ' ' + '\u001b[0m';
  37578. args[0] = prefix + args[0].split('\n').join('\n' + prefix);
  37579. args.push('\u001b[3' + c + 'm+' + exports.humanize(this.diff) + '\u001b[0m');
  37580. } else {
  37581. args[0] = new Date().toUTCString()
  37582. + ' ' + name + ' ' + args[0];
  37583. }
  37584. }
  37585. /**
  37586. * Invokes `util.format()` with the specified arguments and writes to `stream`.
  37587. */
  37588. function log() {
  37589. return stream.write(util.format.apply(util, arguments) + '\n');
  37590. }
  37591. /**
  37592. * Save `namespaces`.
  37593. *
  37594. * @param {String} namespaces
  37595. * @api private
  37596. */
  37597. function save(namespaces) {
  37598. if (null == namespaces) {
  37599. // If you set a process.env field to null or undefined, it gets cast to the
  37600. // string 'null' or 'undefined'. Just delete instead.
  37601. delete process.env.DEBUG;
  37602. } else {
  37603. process.env.DEBUG = namespaces;
  37604. }
  37605. }
  37606. /**
  37607. * Load `namespaces`.
  37608. *
  37609. * @return {String} returns the previously persisted debug modes
  37610. * @api private
  37611. */
  37612. function load() {
  37613. return process.env.DEBUG;
  37614. }
  37615. /**
  37616. * Copied from `node/src/node.js`.
  37617. *
  37618. * XXX: It's lame that node doesn't expose this API out-of-the-box. It also
  37619. * relies on the undocumented `tty_wrap.guessHandleType()` which is also lame.
  37620. */
  37621. function createWritableStdioStream (fd) {
  37622. var stream;
  37623. var tty_wrap = process.binding('tty_wrap');
  37624. // Note stream._type is used for test-module-load-list.js
  37625. switch (tty_wrap.guessHandleType(fd)) {
  37626. case 'TTY':
  37627. stream = new tty.WriteStream(fd);
  37628. stream._type = 'tty';
  37629. // Hack to have stream not keep the event loop alive.
  37630. // See https://github.com/joyent/node/issues/1726
  37631. if (stream._handle && stream._handle.unref) {
  37632. stream._handle.unref();
  37633. }
  37634. break;
  37635. case 'FILE':
  37636. var fs = require$$0__default;
  37637. stream = new fs.SyncWriteStream(fd, { autoClose: false });
  37638. stream._type = 'fs';
  37639. break;
  37640. case 'PIPE':
  37641. case 'TCP':
  37642. var net = require$$4$1;
  37643. stream = new net.Socket({
  37644. fd: fd,
  37645. readable: false,
  37646. writable: true
  37647. });
  37648. // FIXME Should probably have an option in net.Socket to create a
  37649. // stream from an existing fd which is writable only. But for now
  37650. // we'll just add this hack and set the `readable` member to false.
  37651. // Test: ./node test/fixtures/echo.js < /etc/passwd
  37652. stream.readable = false;
  37653. stream.read = null;
  37654. stream._type = 'pipe';
  37655. // FIXME Hack to have stream not keep the event loop alive.
  37656. // See https://github.com/joyent/node/issues/1726
  37657. if (stream._handle && stream._handle.unref) {
  37658. stream._handle.unref();
  37659. }
  37660. break;
  37661. default:
  37662. // Probably an error on in uv_guess_handle()
  37663. throw new Error('Implement me. Unknown stream file type!');
  37664. }
  37665. // For supporting legacy API we put the FD here.
  37666. stream.fd = fd;
  37667. stream._isStdio = true;
  37668. return stream;
  37669. }
  37670. /**
  37671. * Init logic for `debug` instances.
  37672. *
  37673. * Create a new `inspectOpts` object in case `useColors` is set
  37674. * differently for a particular `debug` instance.
  37675. */
  37676. function init (debug) {
  37677. debug.inspectOpts = {};
  37678. var keys = Object.keys(exports.inspectOpts);
  37679. for (var i = 0; i < keys.length; i++) {
  37680. debug.inspectOpts[keys[i]] = exports.inspectOpts[keys[i]];
  37681. }
  37682. }
  37683. /**
  37684. * Enable namespaces listed in `process.env.DEBUG` initially.
  37685. */
  37686. exports.enable(load());
  37687. } (node, node.exports));
  37688. return node.exports;
  37689. }
  37690. /**
  37691. * Detect Electron renderer process, which is node, but we should
  37692. * treat as a browser.
  37693. */
  37694. if (typeof process !== 'undefined' && process.type === 'renderer') {
  37695. src.exports = requireBrowser();
  37696. } else {
  37697. src.exports = requireNode();
  37698. }
  37699. var srcExports = src.exports;
  37700. /*!
  37701. * encodeurl
  37702. * Copyright(c) 2016 Douglas Christopher Wilson
  37703. * MIT Licensed
  37704. */
  37705. /**
  37706. * Module exports.
  37707. * @public
  37708. */
  37709. var encodeurl = encodeUrl$1;
  37710. /**
  37711. * RegExp to match non-URL code points, *after* encoding (i.e. not including "%")
  37712. * and including invalid escape sequences.
  37713. * @private
  37714. */
  37715. 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;
  37716. /**
  37717. * RegExp to match unmatched surrogate pair.
  37718. * @private
  37719. */
  37720. var UNMATCHED_SURROGATE_PAIR_REGEXP = /(^|[^\uD800-\uDBFF])[\uDC00-\uDFFF]|[\uD800-\uDBFF]([^\uDC00-\uDFFF]|$)/g;
  37721. /**
  37722. * String to replace unmatched surrogate pair with.
  37723. * @private
  37724. */
  37725. var UNMATCHED_SURROGATE_PAIR_REPLACE = '$1\uFFFD$2';
  37726. /**
  37727. * Encode a URL to a percent-encoded form, excluding already-encoded sequences.
  37728. *
  37729. * This function will take an already-encoded URL and encode all the non-URL
  37730. * code points. This function will not encode the "%" character unless it is
  37731. * not part of a valid sequence (`%20` will be left as-is, but `%foo` will
  37732. * be encoded as `%25foo`).
  37733. *
  37734. * This encode is meant to be "safe" and does not throw errors. It will try as
  37735. * hard as it can to properly encode the given URL, including replacing any raw,
  37736. * unpaired surrogate pairs with the Unicode replacement character prior to
  37737. * encoding.
  37738. *
  37739. * @param {string} url
  37740. * @return {string}
  37741. * @public
  37742. */
  37743. function encodeUrl$1 (url) {
  37744. return String(url)
  37745. .replace(UNMATCHED_SURROGATE_PAIR_REGEXP, UNMATCHED_SURROGATE_PAIR_REPLACE)
  37746. .replace(ENCODE_CHARS_REGEXP, encodeURI)
  37747. }
  37748. /*!
  37749. * escape-html
  37750. * Copyright(c) 2012-2013 TJ Holowaychuk
  37751. * Copyright(c) 2015 Andreas Lubbe
  37752. * Copyright(c) 2015 Tiancheng "Timothy" Gu
  37753. * MIT Licensed
  37754. */
  37755. /**
  37756. * Module variables.
  37757. * @private
  37758. */
  37759. var matchHtmlRegExp = /["'&<>]/;
  37760. /**
  37761. * Module exports.
  37762. * @public
  37763. */
  37764. var escapeHtml_1 = escapeHtml$1;
  37765. /**
  37766. * Escape special characters in the given string of html.
  37767. *
  37768. * @param {string} string The string to escape for inserting into HTML
  37769. * @return {string}
  37770. * @public
  37771. */
  37772. function escapeHtml$1(string) {
  37773. var str = '' + string;
  37774. var match = matchHtmlRegExp.exec(str);
  37775. if (!match) {
  37776. return str;
  37777. }
  37778. var escape;
  37779. var html = '';
  37780. var index = 0;
  37781. var lastIndex = 0;
  37782. for (index = match.index; index < str.length; index++) {
  37783. switch (str.charCodeAt(index)) {
  37784. case 34: // "
  37785. escape = '&quot;';
  37786. break;
  37787. case 38: // &
  37788. escape = '&amp;';
  37789. break;
  37790. case 39: // '
  37791. escape = '&#39;';
  37792. break;
  37793. case 60: // <
  37794. escape = '&lt;';
  37795. break;
  37796. case 62: // >
  37797. escape = '&gt;';
  37798. break;
  37799. default:
  37800. continue;
  37801. }
  37802. if (lastIndex !== index) {
  37803. html += str.substring(lastIndex, index);
  37804. }
  37805. lastIndex = index + 1;
  37806. html += escape;
  37807. }
  37808. return lastIndex !== index
  37809. ? html + str.substring(lastIndex, index)
  37810. : html;
  37811. }
  37812. var escapeHtml$2 = /*@__PURE__*/getDefaultExportFromCjs(escapeHtml_1);
  37813. var onFinished$2 = {exports: {}};
  37814. /*!
  37815. * ee-first
  37816. * Copyright(c) 2014 Jonathan Ong
  37817. * MIT Licensed
  37818. */
  37819. /**
  37820. * Module exports.
  37821. * @public
  37822. */
  37823. var eeFirst = first$1;
  37824. /**
  37825. * Get the first event in a set of event emitters and event pairs.
  37826. *
  37827. * @param {array} stuff
  37828. * @param {function} done
  37829. * @public
  37830. */
  37831. function first$1(stuff, done) {
  37832. if (!Array.isArray(stuff))
  37833. throw new TypeError('arg must be an array of [ee, events...] arrays')
  37834. var cleanups = [];
  37835. for (var i = 0; i < stuff.length; i++) {
  37836. var arr = stuff[i];
  37837. if (!Array.isArray(arr) || arr.length < 2)
  37838. throw new TypeError('each array member must be [ee, events...]')
  37839. var ee = arr[0];
  37840. for (var j = 1; j < arr.length; j++) {
  37841. var event = arr[j];
  37842. var fn = listener(event, callback);
  37843. // listen to the event
  37844. ee.on(event, fn);
  37845. // push this listener to the list of cleanups
  37846. cleanups.push({
  37847. ee: ee,
  37848. event: event,
  37849. fn: fn,
  37850. });
  37851. }
  37852. }
  37853. function callback() {
  37854. cleanup();
  37855. done.apply(null, arguments);
  37856. }
  37857. function cleanup() {
  37858. var x;
  37859. for (var i = 0; i < cleanups.length; i++) {
  37860. x = cleanups[i];
  37861. x.ee.removeListener(x.event, x.fn);
  37862. }
  37863. }
  37864. function thunk(fn) {
  37865. done = fn;
  37866. }
  37867. thunk.cancel = cleanup;
  37868. return thunk
  37869. }
  37870. /**
  37871. * Create the event listener.
  37872. * @private
  37873. */
  37874. function listener(event, done) {
  37875. return function onevent(arg1) {
  37876. var args = new Array(arguments.length);
  37877. var ee = this;
  37878. var err = event === 'error'
  37879. ? arg1
  37880. : null;
  37881. // copy args to prevent arguments escaping scope
  37882. for (var i = 0; i < args.length; i++) {
  37883. args[i] = arguments[i];
  37884. }
  37885. done(err, ee, event, args);
  37886. }
  37887. }
  37888. /*!
  37889. * on-finished
  37890. * Copyright(c) 2013 Jonathan Ong
  37891. * Copyright(c) 2014 Douglas Christopher Wilson
  37892. * MIT Licensed
  37893. */
  37894. /**
  37895. * Module exports.
  37896. * @public
  37897. */
  37898. onFinished$2.exports = onFinished$1;
  37899. onFinished$2.exports.isFinished = isFinished$1;
  37900. /**
  37901. * Module dependencies.
  37902. * @private
  37903. */
  37904. var first = eeFirst;
  37905. /**
  37906. * Variables.
  37907. * @private
  37908. */
  37909. /* istanbul ignore next */
  37910. var defer$2 = typeof setImmediate === 'function'
  37911. ? setImmediate
  37912. : function(fn){ process.nextTick(fn.bind.apply(fn, arguments)); };
  37913. /**
  37914. * Invoke callback when the response has finished, useful for
  37915. * cleaning up resources afterwards.
  37916. *
  37917. * @param {object} msg
  37918. * @param {function} listener
  37919. * @return {object}
  37920. * @public
  37921. */
  37922. function onFinished$1(msg, listener) {
  37923. if (isFinished$1(msg) !== false) {
  37924. defer$2(listener, null, msg);
  37925. return msg
  37926. }
  37927. // attach the listener to the message
  37928. attachListener(msg, listener);
  37929. return msg
  37930. }
  37931. /**
  37932. * Determine if message is already finished.
  37933. *
  37934. * @param {object} msg
  37935. * @return {boolean}
  37936. * @public
  37937. */
  37938. function isFinished$1(msg) {
  37939. var socket = msg.socket;
  37940. if (typeof msg.finished === 'boolean') {
  37941. // OutgoingMessage
  37942. return Boolean(msg.finished || (socket && !socket.writable))
  37943. }
  37944. if (typeof msg.complete === 'boolean') {
  37945. // IncomingMessage
  37946. return Boolean(msg.upgrade || !socket || !socket.readable || (msg.complete && !msg.readable))
  37947. }
  37948. // don't know
  37949. return undefined
  37950. }
  37951. /**
  37952. * Attach a finished listener to the message.
  37953. *
  37954. * @param {object} msg
  37955. * @param {function} callback
  37956. * @private
  37957. */
  37958. function attachFinishedListener(msg, callback) {
  37959. var eeMsg;
  37960. var eeSocket;
  37961. var finished = false;
  37962. function onFinish(error) {
  37963. eeMsg.cancel();
  37964. eeSocket.cancel();
  37965. finished = true;
  37966. callback(error);
  37967. }
  37968. // finished on first message event
  37969. eeMsg = eeSocket = first([[msg, 'end', 'finish']], onFinish);
  37970. function onSocket(socket) {
  37971. // remove listener
  37972. msg.removeListener('socket', onSocket);
  37973. if (finished) return
  37974. if (eeMsg !== eeSocket) return
  37975. // finished on first socket event
  37976. eeSocket = first([[socket, 'error', 'close']], onFinish);
  37977. }
  37978. if (msg.socket) {
  37979. // socket already assigned
  37980. onSocket(msg.socket);
  37981. return
  37982. }
  37983. // wait for socket to be assigned
  37984. msg.on('socket', onSocket);
  37985. if (msg.socket === undefined) {
  37986. // node.js 0.8 patch
  37987. patchAssignSocket(msg, onSocket);
  37988. }
  37989. }
  37990. /**
  37991. * Attach the listener to the message.
  37992. *
  37993. * @param {object} msg
  37994. * @return {function}
  37995. * @private
  37996. */
  37997. function attachListener(msg, listener) {
  37998. var attached = msg.__onFinished;
  37999. // create a private single listener with queue
  38000. if (!attached || !attached.queue) {
  38001. attached = msg.__onFinished = createListener(msg);
  38002. attachFinishedListener(msg, attached);
  38003. }
  38004. attached.queue.push(listener);
  38005. }
  38006. /**
  38007. * Create listener on message.
  38008. *
  38009. * @param {object} msg
  38010. * @return {function}
  38011. * @private
  38012. */
  38013. function createListener(msg) {
  38014. function listener(err) {
  38015. if (msg.__onFinished === listener) msg.__onFinished = null;
  38016. if (!listener.queue) return
  38017. var queue = listener.queue;
  38018. listener.queue = null;
  38019. for (var i = 0; i < queue.length; i++) {
  38020. queue[i](err, msg);
  38021. }
  38022. }
  38023. listener.queue = [];
  38024. return listener
  38025. }
  38026. /**
  38027. * Patch ServerResponse.prototype.assignSocket for node.js 0.8.
  38028. *
  38029. * @param {ServerResponse} res
  38030. * @param {function} callback
  38031. * @private
  38032. */
  38033. function patchAssignSocket(res, callback) {
  38034. var assignSocket = res.assignSocket;
  38035. if (typeof assignSocket !== 'function') return
  38036. // res.on('socket', callback) is broken in 0.8
  38037. res.assignSocket = function _assignSocket(socket) {
  38038. assignSocket.call(this, socket);
  38039. callback(socket);
  38040. };
  38041. }
  38042. var onFinishedExports = onFinished$2.exports;
  38043. var parseurl$1 = {exports: {}};
  38044. /*!
  38045. * parseurl
  38046. * Copyright(c) 2014 Jonathan Ong
  38047. * Copyright(c) 2014-2017 Douglas Christopher Wilson
  38048. * MIT Licensed
  38049. */
  38050. /**
  38051. * Module dependencies.
  38052. * @private
  38053. */
  38054. var url$3 = require$$0$9;
  38055. var parse$8 = url$3.parse;
  38056. var Url = url$3.Url;
  38057. /**
  38058. * Module exports.
  38059. * @public
  38060. */
  38061. parseurl$1.exports = parseurl;
  38062. parseurl$1.exports.original = originalurl;
  38063. /**
  38064. * Parse the `req` url with memoization.
  38065. *
  38066. * @param {ServerRequest} req
  38067. * @return {Object}
  38068. * @public
  38069. */
  38070. function parseurl (req) {
  38071. var url = req.url;
  38072. if (url === undefined) {
  38073. // URL is undefined
  38074. return undefined
  38075. }
  38076. var parsed = req._parsedUrl;
  38077. if (fresh(url, parsed)) {
  38078. // Return cached URL parse
  38079. return parsed
  38080. }
  38081. // Parse the URL
  38082. parsed = fastparse(url);
  38083. parsed._raw = url;
  38084. return (req._parsedUrl = parsed)
  38085. }
  38086. /**
  38087. * Parse the `req` original url with fallback and memoization.
  38088. *
  38089. * @param {ServerRequest} req
  38090. * @return {Object}
  38091. * @public
  38092. */
  38093. function originalurl (req) {
  38094. var url = req.originalUrl;
  38095. if (typeof url !== 'string') {
  38096. // Fallback
  38097. return parseurl(req)
  38098. }
  38099. var parsed = req._parsedOriginalUrl;
  38100. if (fresh(url, parsed)) {
  38101. // Return cached URL parse
  38102. return parsed
  38103. }
  38104. // Parse the URL
  38105. parsed = fastparse(url);
  38106. parsed._raw = url;
  38107. return (req._parsedOriginalUrl = parsed)
  38108. }
  38109. /**
  38110. * Parse the `str` url with fast-path short-cut.
  38111. *
  38112. * @param {string} str
  38113. * @return {Object}
  38114. * @private
  38115. */
  38116. function fastparse (str) {
  38117. if (typeof str !== 'string' || str.charCodeAt(0) !== 0x2f /* / */) {
  38118. return parse$8(str)
  38119. }
  38120. var pathname = str;
  38121. var query = null;
  38122. var search = null;
  38123. // This takes the regexp from https://github.com/joyent/node/pull/7878
  38124. // Which is /^(\/[^?#\s]*)(\?[^#\s]*)?$/
  38125. // And unrolls it into a for loop
  38126. for (var i = 1; i < str.length; i++) {
  38127. switch (str.charCodeAt(i)) {
  38128. case 0x3f: /* ? */
  38129. if (search === null) {
  38130. pathname = str.substring(0, i);
  38131. query = str.substring(i + 1);
  38132. search = str.substring(i);
  38133. }
  38134. break
  38135. case 0x09: /* \t */
  38136. case 0x0a: /* \n */
  38137. case 0x0c: /* \f */
  38138. case 0x0d: /* \r */
  38139. case 0x20: /* */
  38140. case 0x23: /* # */
  38141. case 0xa0:
  38142. case 0xfeff:
  38143. return parse$8(str)
  38144. }
  38145. }
  38146. var url = Url !== undefined
  38147. ? new Url()
  38148. : {};
  38149. url.path = str;
  38150. url.href = str;
  38151. url.pathname = pathname;
  38152. if (search !== null) {
  38153. url.query = query;
  38154. url.search = search;
  38155. }
  38156. return url
  38157. }
  38158. /**
  38159. * Determine if parsed is still fresh for url.
  38160. *
  38161. * @param {string} url
  38162. * @param {object} parsedUrl
  38163. * @return {boolean}
  38164. * @private
  38165. */
  38166. function fresh (url, parsedUrl) {
  38167. return typeof parsedUrl === 'object' &&
  38168. parsedUrl !== null &&
  38169. (Url === undefined || parsedUrl instanceof Url) &&
  38170. parsedUrl._raw === url
  38171. }
  38172. var parseurlExports = parseurl$1.exports;
  38173. var require$$0$1 = {
  38174. "100": "Continue",
  38175. "101": "Switching Protocols",
  38176. "102": "Processing",
  38177. "103": "Early Hints",
  38178. "200": "OK",
  38179. "201": "Created",
  38180. "202": "Accepted",
  38181. "203": "Non-Authoritative Information",
  38182. "204": "No Content",
  38183. "205": "Reset Content",
  38184. "206": "Partial Content",
  38185. "207": "Multi-Status",
  38186. "208": "Already Reported",
  38187. "226": "IM Used",
  38188. "300": "Multiple Choices",
  38189. "301": "Moved Permanently",
  38190. "302": "Found",
  38191. "303": "See Other",
  38192. "304": "Not Modified",
  38193. "305": "Use Proxy",
  38194. "306": "(Unused)",
  38195. "307": "Temporary Redirect",
  38196. "308": "Permanent Redirect",
  38197. "400": "Bad Request",
  38198. "401": "Unauthorized",
  38199. "402": "Payment Required",
  38200. "403": "Forbidden",
  38201. "404": "Not Found",
  38202. "405": "Method Not Allowed",
  38203. "406": "Not Acceptable",
  38204. "407": "Proxy Authentication Required",
  38205. "408": "Request Timeout",
  38206. "409": "Conflict",
  38207. "410": "Gone",
  38208. "411": "Length Required",
  38209. "412": "Precondition Failed",
  38210. "413": "Payload Too Large",
  38211. "414": "URI Too Long",
  38212. "415": "Unsupported Media Type",
  38213. "416": "Range Not Satisfiable",
  38214. "417": "Expectation Failed",
  38215. "418": "I'm a teapot",
  38216. "421": "Misdirected Request",
  38217. "422": "Unprocessable Entity",
  38218. "423": "Locked",
  38219. "424": "Failed Dependency",
  38220. "425": "Unordered Collection",
  38221. "426": "Upgrade Required",
  38222. "428": "Precondition Required",
  38223. "429": "Too Many Requests",
  38224. "431": "Request Header Fields Too Large",
  38225. "451": "Unavailable For Legal Reasons",
  38226. "500": "Internal Server Error",
  38227. "501": "Not Implemented",
  38228. "502": "Bad Gateway",
  38229. "503": "Service Unavailable",
  38230. "504": "Gateway Timeout",
  38231. "505": "HTTP Version Not Supported",
  38232. "506": "Variant Also Negotiates",
  38233. "507": "Insufficient Storage",
  38234. "508": "Loop Detected",
  38235. "509": "Bandwidth Limit Exceeded",
  38236. "510": "Not Extended",
  38237. "511": "Network Authentication Required"
  38238. };
  38239. /*!
  38240. * statuses
  38241. * Copyright(c) 2014 Jonathan Ong
  38242. * Copyright(c) 2016 Douglas Christopher Wilson
  38243. * MIT Licensed
  38244. */
  38245. /**
  38246. * Module dependencies.
  38247. * @private
  38248. */
  38249. var codes = require$$0$1;
  38250. /**
  38251. * Module exports.
  38252. * @public
  38253. */
  38254. var statuses$1 = status;
  38255. // status code to message map
  38256. status.STATUS_CODES = codes;
  38257. // array of status codes
  38258. status.codes = populateStatusesMap(status, codes);
  38259. // status codes for redirects
  38260. status.redirect = {
  38261. 300: true,
  38262. 301: true,
  38263. 302: true,
  38264. 303: true,
  38265. 305: true,
  38266. 307: true,
  38267. 308: true
  38268. };
  38269. // status codes for empty bodies
  38270. status.empty = {
  38271. 204: true,
  38272. 205: true,
  38273. 304: true
  38274. };
  38275. // status codes for when you should retry the request
  38276. status.retry = {
  38277. 502: true,
  38278. 503: true,
  38279. 504: true
  38280. };
  38281. /**
  38282. * Populate the statuses map for given codes.
  38283. * @private
  38284. */
  38285. function populateStatusesMap (statuses, codes) {
  38286. var arr = [];
  38287. Object.keys(codes).forEach(function forEachCode (code) {
  38288. var message = codes[code];
  38289. var status = Number(code);
  38290. // Populate properties
  38291. statuses[status] = message;
  38292. statuses[message] = status;
  38293. statuses[message.toLowerCase()] = status;
  38294. // Add to array
  38295. arr.push(status);
  38296. });
  38297. return arr
  38298. }
  38299. /**
  38300. * Get the status code.
  38301. *
  38302. * Given a number, this will throw if it is not a known status
  38303. * code, otherwise the code will be returned. Given a string,
  38304. * the string will be parsed for a number and return the code
  38305. * if valid, otherwise will lookup the code assuming this is
  38306. * the status message.
  38307. *
  38308. * @param {string|number} code
  38309. * @returns {number}
  38310. * @public
  38311. */
  38312. function status (code) {
  38313. if (typeof code === 'number') {
  38314. if (!status[code]) throw new Error('invalid status code: ' + code)
  38315. return code
  38316. }
  38317. if (typeof code !== 'string') {
  38318. throw new TypeError('code must be a number or string')
  38319. }
  38320. // '403'
  38321. var n = parseInt(code, 10);
  38322. if (!isNaN(n)) {
  38323. if (!status[n]) throw new Error('invalid status code: ' + n)
  38324. return n
  38325. }
  38326. n = status[code.toLowerCase()];
  38327. if (!n) throw new Error('invalid status message: "' + code + '"')
  38328. return n
  38329. }
  38330. /*!
  38331. * unpipe
  38332. * Copyright(c) 2015 Douglas Christopher Wilson
  38333. * MIT Licensed
  38334. */
  38335. /**
  38336. * Module exports.
  38337. * @public
  38338. */
  38339. var unpipe_1 = unpipe$1;
  38340. /**
  38341. * Determine if there are Node.js pipe-like data listeners.
  38342. * @private
  38343. */
  38344. function hasPipeDataListeners(stream) {
  38345. var listeners = stream.listeners('data');
  38346. for (var i = 0; i < listeners.length; i++) {
  38347. if (listeners[i].name === 'ondata') {
  38348. return true
  38349. }
  38350. }
  38351. return false
  38352. }
  38353. /**
  38354. * Unpipe a stream from all destinations.
  38355. *
  38356. * @param {object} stream
  38357. * @public
  38358. */
  38359. function unpipe$1(stream) {
  38360. if (!stream) {
  38361. throw new TypeError('argument stream is required')
  38362. }
  38363. if (typeof stream.unpipe === 'function') {
  38364. // new-style
  38365. stream.unpipe();
  38366. return
  38367. }
  38368. // Node.js 0.8 hack
  38369. if (!hasPipeDataListeners(stream)) {
  38370. return
  38371. }
  38372. var listener;
  38373. var listeners = stream.listeners('close');
  38374. for (var i = 0; i < listeners.length; i++) {
  38375. listener = listeners[i];
  38376. if (listener.name !== 'cleanup' && listener.name !== 'onclose') {
  38377. continue
  38378. }
  38379. // invoke the listener
  38380. listener.call(stream);
  38381. }
  38382. }
  38383. /*!
  38384. * finalhandler
  38385. * Copyright(c) 2014-2017 Douglas Christopher Wilson
  38386. * MIT Licensed
  38387. */
  38388. /**
  38389. * Module dependencies.
  38390. * @private
  38391. */
  38392. var debug$e = srcExports('finalhandler');
  38393. var encodeUrl = encodeurl;
  38394. var escapeHtml = escapeHtml_1;
  38395. var onFinished = onFinishedExports;
  38396. var parseUrl$2 = parseurlExports;
  38397. var statuses = statuses$1;
  38398. var unpipe = unpipe_1;
  38399. /**
  38400. * Module variables.
  38401. * @private
  38402. */
  38403. var DOUBLE_SPACE_REGEXP = /\x20{2}/g;
  38404. var NEWLINE_REGEXP = /\n/g;
  38405. /* istanbul ignore next */
  38406. var defer$1 = typeof setImmediate === 'function'
  38407. ? setImmediate
  38408. : function (fn) { process.nextTick(fn.bind.apply(fn, arguments)); };
  38409. var isFinished = onFinished.isFinished;
  38410. /**
  38411. * Create a minimal HTML document.
  38412. *
  38413. * @param {string} message
  38414. * @private
  38415. */
  38416. function createHtmlDocument (message) {
  38417. var body = escapeHtml(message)
  38418. .replace(NEWLINE_REGEXP, '<br>')
  38419. .replace(DOUBLE_SPACE_REGEXP, ' &nbsp;');
  38420. return '<!DOCTYPE html>\n' +
  38421. '<html lang="en">\n' +
  38422. '<head>\n' +
  38423. '<meta charset="utf-8">\n' +
  38424. '<title>Error</title>\n' +
  38425. '</head>\n' +
  38426. '<body>\n' +
  38427. '<pre>' + body + '</pre>\n' +
  38428. '</body>\n' +
  38429. '</html>\n'
  38430. }
  38431. /**
  38432. * Module exports.
  38433. * @public
  38434. */
  38435. var finalhandler_1 = finalhandler$1;
  38436. /**
  38437. * Create a function to handle the final response.
  38438. *
  38439. * @param {Request} req
  38440. * @param {Response} res
  38441. * @param {Object} [options]
  38442. * @return {Function}
  38443. * @public
  38444. */
  38445. function finalhandler$1 (req, res, options) {
  38446. var opts = options || {};
  38447. // get environment
  38448. var env = opts.env || process.env.NODE_ENV || 'development';
  38449. // get error callback
  38450. var onerror = opts.onerror;
  38451. return function (err) {
  38452. var headers;
  38453. var msg;
  38454. var status;
  38455. // ignore 404 on in-flight response
  38456. if (!err && headersSent(res)) {
  38457. debug$e('cannot 404 after headers sent');
  38458. return
  38459. }
  38460. // unhandled error
  38461. if (err) {
  38462. // respect status code from error
  38463. status = getErrorStatusCode(err);
  38464. if (status === undefined) {
  38465. // fallback to status code on response
  38466. status = getResponseStatusCode(res);
  38467. } else {
  38468. // respect headers from error
  38469. headers = getErrorHeaders(err);
  38470. }
  38471. // get error message
  38472. msg = getErrorMessage(err, status, env);
  38473. } else {
  38474. // not found
  38475. status = 404;
  38476. msg = 'Cannot ' + req.method + ' ' + encodeUrl(getResourceName(req));
  38477. }
  38478. debug$e('default %s', status);
  38479. // schedule onerror callback
  38480. if (err && onerror) {
  38481. defer$1(onerror, err, req, res);
  38482. }
  38483. // cannot actually respond
  38484. if (headersSent(res)) {
  38485. debug$e('cannot %d after headers sent', status);
  38486. req.socket.destroy();
  38487. return
  38488. }
  38489. // send response
  38490. send$2(req, res, status, headers, msg);
  38491. }
  38492. }
  38493. /**
  38494. * Get headers from Error object.
  38495. *
  38496. * @param {Error} err
  38497. * @return {object}
  38498. * @private
  38499. */
  38500. function getErrorHeaders (err) {
  38501. if (!err.headers || typeof err.headers !== 'object') {
  38502. return undefined
  38503. }
  38504. var headers = Object.create(null);
  38505. var keys = Object.keys(err.headers);
  38506. for (var i = 0; i < keys.length; i++) {
  38507. var key = keys[i];
  38508. headers[key] = err.headers[key];
  38509. }
  38510. return headers
  38511. }
  38512. /**
  38513. * Get message from Error object, fallback to status message.
  38514. *
  38515. * @param {Error} err
  38516. * @param {number} status
  38517. * @param {string} env
  38518. * @return {string}
  38519. * @private
  38520. */
  38521. function getErrorMessage (err, status, env) {
  38522. var msg;
  38523. if (env !== 'production') {
  38524. // use err.stack, which typically includes err.message
  38525. msg = err.stack;
  38526. // fallback to err.toString() when possible
  38527. if (!msg && typeof err.toString === 'function') {
  38528. msg = err.toString();
  38529. }
  38530. }
  38531. return msg || statuses[status]
  38532. }
  38533. /**
  38534. * Get status code from Error object.
  38535. *
  38536. * @param {Error} err
  38537. * @return {number}
  38538. * @private
  38539. */
  38540. function getErrorStatusCode (err) {
  38541. // check err.status
  38542. if (typeof err.status === 'number' && err.status >= 400 && err.status < 600) {
  38543. return err.status
  38544. }
  38545. // check err.statusCode
  38546. if (typeof err.statusCode === 'number' && err.statusCode >= 400 && err.statusCode < 600) {
  38547. return err.statusCode
  38548. }
  38549. return undefined
  38550. }
  38551. /**
  38552. * Get resource name for the request.
  38553. *
  38554. * This is typically just the original pathname of the request
  38555. * but will fallback to "resource" is that cannot be determined.
  38556. *
  38557. * @param {IncomingMessage} req
  38558. * @return {string}
  38559. * @private
  38560. */
  38561. function getResourceName (req) {
  38562. try {
  38563. return parseUrl$2.original(req).pathname
  38564. } catch (e) {
  38565. return 'resource'
  38566. }
  38567. }
  38568. /**
  38569. * Get status code from response.
  38570. *
  38571. * @param {OutgoingMessage} res
  38572. * @return {number}
  38573. * @private
  38574. */
  38575. function getResponseStatusCode (res) {
  38576. var status = res.statusCode;
  38577. // default status code to 500 if outside valid range
  38578. if (typeof status !== 'number' || status < 400 || status > 599) {
  38579. status = 500;
  38580. }
  38581. return status
  38582. }
  38583. /**
  38584. * Determine if the response headers have been sent.
  38585. *
  38586. * @param {object} res
  38587. * @returns {boolean}
  38588. * @private
  38589. */
  38590. function headersSent (res) {
  38591. return typeof res.headersSent !== 'boolean'
  38592. ? Boolean(res._header)
  38593. : res.headersSent
  38594. }
  38595. /**
  38596. * Send response.
  38597. *
  38598. * @param {IncomingMessage} req
  38599. * @param {OutgoingMessage} res
  38600. * @param {number} status
  38601. * @param {object} headers
  38602. * @param {string} message
  38603. * @private
  38604. */
  38605. function send$2 (req, res, status, headers, message) {
  38606. function write () {
  38607. // response body
  38608. var body = createHtmlDocument(message);
  38609. // response status
  38610. res.statusCode = status;
  38611. res.statusMessage = statuses[status];
  38612. // response headers
  38613. setHeaders(res, headers);
  38614. // security headers
  38615. res.setHeader('Content-Security-Policy', "default-src 'none'");
  38616. res.setHeader('X-Content-Type-Options', 'nosniff');
  38617. // standard headers
  38618. res.setHeader('Content-Type', 'text/html; charset=utf-8');
  38619. res.setHeader('Content-Length', Buffer.byteLength(body, 'utf8'));
  38620. if (req.method === 'HEAD') {
  38621. res.end();
  38622. return
  38623. }
  38624. res.end(body, 'utf8');
  38625. }
  38626. if (isFinished(req)) {
  38627. write();
  38628. return
  38629. }
  38630. // unpipe everything from the request
  38631. unpipe(req);
  38632. // flush the request
  38633. onFinished(req, write);
  38634. req.resume();
  38635. }
  38636. /**
  38637. * Set response headers from an object.
  38638. *
  38639. * @param {OutgoingMessage} res
  38640. * @param {object} headers
  38641. * @private
  38642. */
  38643. function setHeaders (res, headers) {
  38644. if (!headers) {
  38645. return
  38646. }
  38647. var keys = Object.keys(headers);
  38648. for (var i = 0; i < keys.length; i++) {
  38649. var key = keys[i];
  38650. res.setHeader(key, headers[key]);
  38651. }
  38652. }
  38653. var utilsMerge = {exports: {}};
  38654. /**
  38655. * Merge object b with object a.
  38656. *
  38657. * var a = { foo: 'bar' }
  38658. * , b = { bar: 'baz' };
  38659. *
  38660. * merge(a, b);
  38661. * // => { foo: 'bar', bar: 'baz' }
  38662. *
  38663. * @param {Object} a
  38664. * @param {Object} b
  38665. * @return {Object}
  38666. * @api public
  38667. */
  38668. (function (module, exports) {
  38669. module.exports = function(a, b){
  38670. if (a && b) {
  38671. for (var key in b) {
  38672. a[key] = b[key];
  38673. }
  38674. }
  38675. return a;
  38676. };
  38677. } (utilsMerge));
  38678. var utilsMergeExports = utilsMerge.exports;
  38679. /*!
  38680. * connect
  38681. * Copyright(c) 2010 Sencha Inc.
  38682. * Copyright(c) 2011 TJ Holowaychuk
  38683. * Copyright(c) 2015 Douglas Christopher Wilson
  38684. * MIT Licensed
  38685. */
  38686. /**
  38687. * Module dependencies.
  38688. * @private
  38689. */
  38690. var debug$d = srcExports('connect:dispatcher');
  38691. var EventEmitter$3 = require$$0$7.EventEmitter;
  38692. var finalhandler = finalhandler_1;
  38693. var http$4 = require$$1;
  38694. var merge = utilsMergeExports;
  38695. var parseUrl$1 = parseurlExports;
  38696. /**
  38697. * Module exports.
  38698. * @public
  38699. */
  38700. var connect = createServer$1;
  38701. /**
  38702. * Module variables.
  38703. * @private
  38704. */
  38705. var env = process.env.NODE_ENV || 'development';
  38706. var proto = {};
  38707. /* istanbul ignore next */
  38708. var defer = typeof setImmediate === 'function'
  38709. ? setImmediate
  38710. : function(fn){ process.nextTick(fn.bind.apply(fn, arguments)); };
  38711. /**
  38712. * Create a new connect server.
  38713. *
  38714. * @return {function}
  38715. * @public
  38716. */
  38717. function createServer$1() {
  38718. function app(req, res, next){ app.handle(req, res, next); }
  38719. merge(app, proto);
  38720. merge(app, EventEmitter$3.prototype);
  38721. app.route = '/';
  38722. app.stack = [];
  38723. return app;
  38724. }
  38725. /**
  38726. * Utilize the given middleware `handle` to the given `route`,
  38727. * defaulting to _/_. This "route" is the mount-point for the
  38728. * middleware, when given a value other than _/_ the middleware
  38729. * is only effective when that segment is present in the request's
  38730. * pathname.
  38731. *
  38732. * For example if we were to mount a function at _/admin_, it would
  38733. * be invoked on _/admin_, and _/admin/settings_, however it would
  38734. * not be invoked for _/_, or _/posts_.
  38735. *
  38736. * @param {String|Function|Server} route, callback or server
  38737. * @param {Function|Server} callback or server
  38738. * @return {Server} for chaining
  38739. * @public
  38740. */
  38741. proto.use = function use(route, fn) {
  38742. var handle = fn;
  38743. var path = route;
  38744. // default route to '/'
  38745. if (typeof route !== 'string') {
  38746. handle = route;
  38747. path = '/';
  38748. }
  38749. // wrap sub-apps
  38750. if (typeof handle.handle === 'function') {
  38751. var server = handle;
  38752. server.route = path;
  38753. handle = function (req, res, next) {
  38754. server.handle(req, res, next);
  38755. };
  38756. }
  38757. // wrap vanilla http.Servers
  38758. if (handle instanceof http$4.Server) {
  38759. handle = handle.listeners('request')[0];
  38760. }
  38761. // strip trailing slash
  38762. if (path[path.length - 1] === '/') {
  38763. path = path.slice(0, -1);
  38764. }
  38765. // add the middleware
  38766. debug$d('use %s %s', path || '/', handle.name || 'anonymous');
  38767. this.stack.push({ route: path, handle: handle });
  38768. return this;
  38769. };
  38770. /**
  38771. * Handle server requests, punting them down
  38772. * the middleware stack.
  38773. *
  38774. * @private
  38775. */
  38776. proto.handle = function handle(req, res, out) {
  38777. var index = 0;
  38778. var protohost = getProtohost(req.url) || '';
  38779. var removed = '';
  38780. var slashAdded = false;
  38781. var stack = this.stack;
  38782. // final function handler
  38783. var done = out || finalhandler(req, res, {
  38784. env: env,
  38785. onerror: logerror
  38786. });
  38787. // store the original URL
  38788. req.originalUrl = req.originalUrl || req.url;
  38789. function next(err) {
  38790. if (slashAdded) {
  38791. req.url = req.url.substr(1);
  38792. slashAdded = false;
  38793. }
  38794. if (removed.length !== 0) {
  38795. req.url = protohost + removed + req.url.substr(protohost.length);
  38796. removed = '';
  38797. }
  38798. // next callback
  38799. var layer = stack[index++];
  38800. // all done
  38801. if (!layer) {
  38802. defer(done, err);
  38803. return;
  38804. }
  38805. // route data
  38806. var path = parseUrl$1(req).pathname || '/';
  38807. var route = layer.route;
  38808. // skip this layer if the route doesn't match
  38809. if (path.toLowerCase().substr(0, route.length) !== route.toLowerCase()) {
  38810. return next(err);
  38811. }
  38812. // skip if route match does not border "/", ".", or end
  38813. var c = path.length > route.length && path[route.length];
  38814. if (c && c !== '/' && c !== '.') {
  38815. return next(err);
  38816. }
  38817. // trim off the part of the url that matches the route
  38818. if (route.length !== 0 && route !== '/') {
  38819. removed = route;
  38820. req.url = protohost + req.url.substr(protohost.length + removed.length);
  38821. // ensure leading slash
  38822. if (!protohost && req.url[0] !== '/') {
  38823. req.url = '/' + req.url;
  38824. slashAdded = true;
  38825. }
  38826. }
  38827. // call the layer handle
  38828. call(layer.handle, route, err, req, res, next);
  38829. }
  38830. next();
  38831. };
  38832. /**
  38833. * Listen for connections.
  38834. *
  38835. * This method takes the same arguments
  38836. * as node's `http.Server#listen()`.
  38837. *
  38838. * HTTP and HTTPS:
  38839. *
  38840. * If you run your application both as HTTP
  38841. * and HTTPS you may wrap them individually,
  38842. * since your Connect "server" is really just
  38843. * a JavaScript `Function`.
  38844. *
  38845. * var connect = require('connect')
  38846. * , http = require('http')
  38847. * , https = require('https');
  38848. *
  38849. * var app = connect();
  38850. *
  38851. * http.createServer(app).listen(80);
  38852. * https.createServer(options, app).listen(443);
  38853. *
  38854. * @return {http.Server}
  38855. * @api public
  38856. */
  38857. proto.listen = function listen() {
  38858. var server = http$4.createServer(this);
  38859. return server.listen.apply(server, arguments);
  38860. };
  38861. /**
  38862. * Invoke a route handle.
  38863. * @private
  38864. */
  38865. function call(handle, route, err, req, res, next) {
  38866. var arity = handle.length;
  38867. var error = err;
  38868. var hasError = Boolean(err);
  38869. debug$d('%s %s : %s', handle.name || '<anonymous>', route, req.originalUrl);
  38870. try {
  38871. if (hasError && arity === 4) {
  38872. // error-handling middleware
  38873. handle(err, req, res, next);
  38874. return;
  38875. } else if (!hasError && arity < 4) {
  38876. // request-handling middleware
  38877. handle(req, res, next);
  38878. return;
  38879. }
  38880. } catch (e) {
  38881. // replace the error
  38882. error = e;
  38883. }
  38884. // continue
  38885. next(error);
  38886. }
  38887. /**
  38888. * Log error using console.error.
  38889. *
  38890. * @param {Error} err
  38891. * @private
  38892. */
  38893. function logerror(err) {
  38894. if (env !== 'test') console.error(err.stack || err.toString());
  38895. }
  38896. /**
  38897. * Get get protocol + host for a URL.
  38898. *
  38899. * @param {string} url
  38900. * @private
  38901. */
  38902. function getProtohost(url) {
  38903. if (url.length === 0 || url[0] === '/') {
  38904. return undefined;
  38905. }
  38906. var fqdnIndex = url.indexOf('://');
  38907. return fqdnIndex !== -1 && url.lastIndexOf('?', fqdnIndex) === -1
  38908. ? url.substr(0, url.indexOf('/', 3 + fqdnIndex))
  38909. : undefined;
  38910. }
  38911. var connect$1 = /*@__PURE__*/getDefaultExportFromCjs(connect);
  38912. var lib = {exports: {}};
  38913. /*
  38914. object-assign
  38915. (c) Sindre Sorhus
  38916. @license MIT
  38917. */
  38918. /* eslint-disable no-unused-vars */
  38919. var getOwnPropertySymbols = Object.getOwnPropertySymbols;
  38920. var hasOwnProperty = Object.prototype.hasOwnProperty;
  38921. var propIsEnumerable = Object.prototype.propertyIsEnumerable;
  38922. function toObject(val) {
  38923. if (val === null || val === undefined) {
  38924. throw new TypeError('Object.assign cannot be called with null or undefined');
  38925. }
  38926. return Object(val);
  38927. }
  38928. function shouldUseNative() {
  38929. try {
  38930. if (!Object.assign) {
  38931. return false;
  38932. }
  38933. // Detect buggy property enumeration order in older V8 versions.
  38934. // https://bugs.chromium.org/p/v8/issues/detail?id=4118
  38935. var test1 = new String('abc'); // eslint-disable-line no-new-wrappers
  38936. test1[5] = 'de';
  38937. if (Object.getOwnPropertyNames(test1)[0] === '5') {
  38938. return false;
  38939. }
  38940. // https://bugs.chromium.org/p/v8/issues/detail?id=3056
  38941. var test2 = {};
  38942. for (var i = 0; i < 10; i++) {
  38943. test2['_' + String.fromCharCode(i)] = i;
  38944. }
  38945. var order2 = Object.getOwnPropertyNames(test2).map(function (n) {
  38946. return test2[n];
  38947. });
  38948. if (order2.join('') !== '0123456789') {
  38949. return false;
  38950. }
  38951. // https://bugs.chromium.org/p/v8/issues/detail?id=3056
  38952. var test3 = {};
  38953. 'abcdefghijklmnopqrst'.split('').forEach(function (letter) {
  38954. test3[letter] = letter;
  38955. });
  38956. if (Object.keys(Object.assign({}, test3)).join('') !==
  38957. 'abcdefghijklmnopqrst') {
  38958. return false;
  38959. }
  38960. return true;
  38961. } catch (err) {
  38962. // We don't expect any of the above to throw, but better to be safe.
  38963. return false;
  38964. }
  38965. }
  38966. var objectAssign = shouldUseNative() ? Object.assign : function (target, source) {
  38967. var from;
  38968. var to = toObject(target);
  38969. var symbols;
  38970. for (var s = 1; s < arguments.length; s++) {
  38971. from = Object(arguments[s]);
  38972. for (var key in from) {
  38973. if (hasOwnProperty.call(from, key)) {
  38974. to[key] = from[key];
  38975. }
  38976. }
  38977. if (getOwnPropertySymbols) {
  38978. symbols = getOwnPropertySymbols(from);
  38979. for (var i = 0; i < symbols.length; i++) {
  38980. if (propIsEnumerable.call(from, symbols[i])) {
  38981. to[symbols[i]] = from[symbols[i]];
  38982. }
  38983. }
  38984. }
  38985. }
  38986. return to;
  38987. };
  38988. var vary$1 = {exports: {}};
  38989. /*!
  38990. * vary
  38991. * Copyright(c) 2014-2017 Douglas Christopher Wilson
  38992. * MIT Licensed
  38993. */
  38994. /**
  38995. * Module exports.
  38996. */
  38997. vary$1.exports = vary;
  38998. vary$1.exports.append = append;
  38999. /**
  39000. * RegExp to match field-name in RFC 7230 sec 3.2
  39001. *
  39002. * field-name = token
  39003. * token = 1*tchar
  39004. * tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*"
  39005. * / "+" / "-" / "." / "^" / "_" / "`" / "|" / "~"
  39006. * / DIGIT / ALPHA
  39007. * ; any VCHAR, except delimiters
  39008. */
  39009. var FIELD_NAME_REGEXP = /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/;
  39010. /**
  39011. * Append a field to a vary header.
  39012. *
  39013. * @param {String} header
  39014. * @param {String|Array} field
  39015. * @return {String}
  39016. * @public
  39017. */
  39018. function append (header, field) {
  39019. if (typeof header !== 'string') {
  39020. throw new TypeError('header argument is required')
  39021. }
  39022. if (!field) {
  39023. throw new TypeError('field argument is required')
  39024. }
  39025. // get fields array
  39026. var fields = !Array.isArray(field)
  39027. ? parse$7(String(field))
  39028. : field;
  39029. // assert on invalid field names
  39030. for (var j = 0; j < fields.length; j++) {
  39031. if (!FIELD_NAME_REGEXP.test(fields[j])) {
  39032. throw new TypeError('field argument contains an invalid header name')
  39033. }
  39034. }
  39035. // existing, unspecified vary
  39036. if (header === '*') {
  39037. return header
  39038. }
  39039. // enumerate current values
  39040. var val = header;
  39041. var vals = parse$7(header.toLowerCase());
  39042. // unspecified vary
  39043. if (fields.indexOf('*') !== -1 || vals.indexOf('*') !== -1) {
  39044. return '*'
  39045. }
  39046. for (var i = 0; i < fields.length; i++) {
  39047. var fld = fields[i].toLowerCase();
  39048. // append value (case-preserving)
  39049. if (vals.indexOf(fld) === -1) {
  39050. vals.push(fld);
  39051. val = val
  39052. ? val + ', ' + fields[i]
  39053. : fields[i];
  39054. }
  39055. }
  39056. return val
  39057. }
  39058. /**
  39059. * Parse a vary header into an array.
  39060. *
  39061. * @param {String} header
  39062. * @return {Array}
  39063. * @private
  39064. */
  39065. function parse$7 (header) {
  39066. var end = 0;
  39067. var list = [];
  39068. var start = 0;
  39069. // gather tokens
  39070. for (var i = 0, len = header.length; i < len; i++) {
  39071. switch (header.charCodeAt(i)) {
  39072. case 0x20: /* */
  39073. if (start === end) {
  39074. start = end = i + 1;
  39075. }
  39076. break
  39077. case 0x2c: /* , */
  39078. list.push(header.substring(start, end));
  39079. start = end = i + 1;
  39080. break
  39081. default:
  39082. end = i + 1;
  39083. break
  39084. }
  39085. }
  39086. // final token
  39087. list.push(header.substring(start, end));
  39088. return list
  39089. }
  39090. /**
  39091. * Mark that a request is varied on a header field.
  39092. *
  39093. * @param {Object} res
  39094. * @param {String|Array} field
  39095. * @public
  39096. */
  39097. function vary (res, field) {
  39098. if (!res || !res.getHeader || !res.setHeader) {
  39099. // quack quack
  39100. throw new TypeError('res argument is required')
  39101. }
  39102. // get existing header
  39103. var val = res.getHeader('Vary') || '';
  39104. var header = Array.isArray(val)
  39105. ? val.join(', ')
  39106. : String(val);
  39107. // set new header
  39108. if ((val = append(header, field))) {
  39109. res.setHeader('Vary', val);
  39110. }
  39111. }
  39112. var varyExports = vary$1.exports;
  39113. (function () {
  39114. var assign = objectAssign;
  39115. var vary = varyExports;
  39116. var defaults = {
  39117. origin: '*',
  39118. methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
  39119. preflightContinue: false,
  39120. optionsSuccessStatus: 204
  39121. };
  39122. function isString(s) {
  39123. return typeof s === 'string' || s instanceof String;
  39124. }
  39125. function isOriginAllowed(origin, allowedOrigin) {
  39126. if (Array.isArray(allowedOrigin)) {
  39127. for (var i = 0; i < allowedOrigin.length; ++i) {
  39128. if (isOriginAllowed(origin, allowedOrigin[i])) {
  39129. return true;
  39130. }
  39131. }
  39132. return false;
  39133. } else if (isString(allowedOrigin)) {
  39134. return origin === allowedOrigin;
  39135. } else if (allowedOrigin instanceof RegExp) {
  39136. return allowedOrigin.test(origin);
  39137. } else {
  39138. return !!allowedOrigin;
  39139. }
  39140. }
  39141. function configureOrigin(options, req) {
  39142. var requestOrigin = req.headers.origin,
  39143. headers = [],
  39144. isAllowed;
  39145. if (!options.origin || options.origin === '*') {
  39146. // allow any origin
  39147. headers.push([{
  39148. key: 'Access-Control-Allow-Origin',
  39149. value: '*'
  39150. }]);
  39151. } else if (isString(options.origin)) {
  39152. // fixed origin
  39153. headers.push([{
  39154. key: 'Access-Control-Allow-Origin',
  39155. value: options.origin
  39156. }]);
  39157. headers.push([{
  39158. key: 'Vary',
  39159. value: 'Origin'
  39160. }]);
  39161. } else {
  39162. isAllowed = isOriginAllowed(requestOrigin, options.origin);
  39163. // reflect origin
  39164. headers.push([{
  39165. key: 'Access-Control-Allow-Origin',
  39166. value: isAllowed ? requestOrigin : false
  39167. }]);
  39168. headers.push([{
  39169. key: 'Vary',
  39170. value: 'Origin'
  39171. }]);
  39172. }
  39173. return headers;
  39174. }
  39175. function configureMethods(options) {
  39176. var methods = options.methods;
  39177. if (methods.join) {
  39178. methods = options.methods.join(','); // .methods is an array, so turn it into a string
  39179. }
  39180. return {
  39181. key: 'Access-Control-Allow-Methods',
  39182. value: methods
  39183. };
  39184. }
  39185. function configureCredentials(options) {
  39186. if (options.credentials === true) {
  39187. return {
  39188. key: 'Access-Control-Allow-Credentials',
  39189. value: 'true'
  39190. };
  39191. }
  39192. return null;
  39193. }
  39194. function configureAllowedHeaders(options, req) {
  39195. var allowedHeaders = options.allowedHeaders || options.headers;
  39196. var headers = [];
  39197. if (!allowedHeaders) {
  39198. allowedHeaders = req.headers['access-control-request-headers']; // .headers wasn't specified, so reflect the request headers
  39199. headers.push([{
  39200. key: 'Vary',
  39201. value: 'Access-Control-Request-Headers'
  39202. }]);
  39203. } else if (allowedHeaders.join) {
  39204. allowedHeaders = allowedHeaders.join(','); // .headers is an array, so turn it into a string
  39205. }
  39206. if (allowedHeaders && allowedHeaders.length) {
  39207. headers.push([{
  39208. key: 'Access-Control-Allow-Headers',
  39209. value: allowedHeaders
  39210. }]);
  39211. }
  39212. return headers;
  39213. }
  39214. function configureExposedHeaders(options) {
  39215. var headers = options.exposedHeaders;
  39216. if (!headers) {
  39217. return null;
  39218. } else if (headers.join) {
  39219. headers = headers.join(','); // .headers is an array, so turn it into a string
  39220. }
  39221. if (headers && headers.length) {
  39222. return {
  39223. key: 'Access-Control-Expose-Headers',
  39224. value: headers
  39225. };
  39226. }
  39227. return null;
  39228. }
  39229. function configureMaxAge(options) {
  39230. var maxAge = (typeof options.maxAge === 'number' || options.maxAge) && options.maxAge.toString();
  39231. if (maxAge && maxAge.length) {
  39232. return {
  39233. key: 'Access-Control-Max-Age',
  39234. value: maxAge
  39235. };
  39236. }
  39237. return null;
  39238. }
  39239. function applyHeaders(headers, res) {
  39240. for (var i = 0, n = headers.length; i < n; i++) {
  39241. var header = headers[i];
  39242. if (header) {
  39243. if (Array.isArray(header)) {
  39244. applyHeaders(header, res);
  39245. } else if (header.key === 'Vary' && header.value) {
  39246. vary(res, header.value);
  39247. } else if (header.value) {
  39248. res.setHeader(header.key, header.value);
  39249. }
  39250. }
  39251. }
  39252. }
  39253. function cors(options, req, res, next) {
  39254. var headers = [],
  39255. method = req.method && req.method.toUpperCase && req.method.toUpperCase();
  39256. if (method === 'OPTIONS') {
  39257. // preflight
  39258. headers.push(configureOrigin(options, req));
  39259. headers.push(configureCredentials(options));
  39260. headers.push(configureMethods(options));
  39261. headers.push(configureAllowedHeaders(options, req));
  39262. headers.push(configureMaxAge(options));
  39263. headers.push(configureExposedHeaders(options));
  39264. applyHeaders(headers, res);
  39265. if (options.preflightContinue) {
  39266. next();
  39267. } else {
  39268. // Safari (and potentially other browsers) need content-length 0,
  39269. // for 204 or they just hang waiting for a body
  39270. res.statusCode = options.optionsSuccessStatus;
  39271. res.setHeader('Content-Length', '0');
  39272. res.end();
  39273. }
  39274. } else {
  39275. // actual response
  39276. headers.push(configureOrigin(options, req));
  39277. headers.push(configureCredentials(options));
  39278. headers.push(configureExposedHeaders(options));
  39279. applyHeaders(headers, res);
  39280. next();
  39281. }
  39282. }
  39283. function middlewareWrapper(o) {
  39284. // if options are static (either via defaults or custom options passed in), wrap in a function
  39285. var optionsCallback = null;
  39286. if (typeof o === 'function') {
  39287. optionsCallback = o;
  39288. } else {
  39289. optionsCallback = function (req, cb) {
  39290. cb(null, o);
  39291. };
  39292. }
  39293. return function corsMiddleware(req, res, next) {
  39294. optionsCallback(req, function (err, options) {
  39295. if (err) {
  39296. next(err);
  39297. } else {
  39298. var corsOptions = assign({}, defaults, options);
  39299. var originCallback = null;
  39300. if (corsOptions.origin && typeof corsOptions.origin === 'function') {
  39301. originCallback = corsOptions.origin;
  39302. } else if (corsOptions.origin) {
  39303. originCallback = function (origin, cb) {
  39304. cb(null, corsOptions.origin);
  39305. };
  39306. }
  39307. if (originCallback) {
  39308. originCallback(req.headers.origin, function (err2, origin) {
  39309. if (err2 || !origin) {
  39310. next(err2);
  39311. } else {
  39312. corsOptions.origin = origin;
  39313. cors(corsOptions, req, res, next);
  39314. }
  39315. });
  39316. } else {
  39317. next();
  39318. }
  39319. }
  39320. });
  39321. };
  39322. }
  39323. // can pass either an options hash, an options delegate, or nothing
  39324. lib.exports = middlewareWrapper;
  39325. }());
  39326. var libExports = lib.exports;
  39327. var corsMiddleware = /*@__PURE__*/getDefaultExportFromCjs(libExports);
  39328. var chokidar = {};
  39329. const fs$8 = require$$0__default;
  39330. const { Readable } = require$$0$6;
  39331. const sysPath$3 = require$$0$4;
  39332. const { promisify: promisify$3 } = require$$0$5;
  39333. const picomatch$1 = picomatch$3;
  39334. const readdir$1 = promisify$3(fs$8.readdir);
  39335. const stat$3 = promisify$3(fs$8.stat);
  39336. const lstat$2 = promisify$3(fs$8.lstat);
  39337. const realpath$1 = promisify$3(fs$8.realpath);
  39338. /**
  39339. * @typedef {Object} EntryInfo
  39340. * @property {String} path
  39341. * @property {String} fullPath
  39342. * @property {fs.Stats=} stats
  39343. * @property {fs.Dirent=} dirent
  39344. * @property {String} basename
  39345. */
  39346. const BANG$2 = '!';
  39347. const RECURSIVE_ERROR_CODE = 'READDIRP_RECURSIVE_ERROR';
  39348. const NORMAL_FLOW_ERRORS = new Set(['ENOENT', 'EPERM', 'EACCES', 'ELOOP', RECURSIVE_ERROR_CODE]);
  39349. const FILE_TYPE = 'files';
  39350. const DIR_TYPE = 'directories';
  39351. const FILE_DIR_TYPE = 'files_directories';
  39352. const EVERYTHING_TYPE = 'all';
  39353. const ALL_TYPES = [FILE_TYPE, DIR_TYPE, FILE_DIR_TYPE, EVERYTHING_TYPE];
  39354. const isNormalFlowError = error => NORMAL_FLOW_ERRORS.has(error.code);
  39355. const [maj, min] = process.versions.node.split('.').slice(0, 2).map(n => Number.parseInt(n, 10));
  39356. const wantBigintFsStats = process.platform === 'win32' && (maj > 10 || (maj === 10 && min >= 5));
  39357. const normalizeFilter = filter => {
  39358. if (filter === undefined) return;
  39359. if (typeof filter === 'function') return filter;
  39360. if (typeof filter === 'string') {
  39361. const glob = picomatch$1(filter.trim());
  39362. return entry => glob(entry.basename);
  39363. }
  39364. if (Array.isArray(filter)) {
  39365. const positive = [];
  39366. const negative = [];
  39367. for (const item of filter) {
  39368. const trimmed = item.trim();
  39369. if (trimmed.charAt(0) === BANG$2) {
  39370. negative.push(picomatch$1(trimmed.slice(1)));
  39371. } else {
  39372. positive.push(picomatch$1(trimmed));
  39373. }
  39374. }
  39375. if (negative.length > 0) {
  39376. if (positive.length > 0) {
  39377. return entry =>
  39378. positive.some(f => f(entry.basename)) && !negative.some(f => f(entry.basename));
  39379. }
  39380. return entry => !negative.some(f => f(entry.basename));
  39381. }
  39382. return entry => positive.some(f => f(entry.basename));
  39383. }
  39384. };
  39385. class ReaddirpStream extends Readable {
  39386. static get defaultOptions() {
  39387. return {
  39388. root: '.',
  39389. /* eslint-disable no-unused-vars */
  39390. fileFilter: (path) => true,
  39391. directoryFilter: (path) => true,
  39392. /* eslint-enable no-unused-vars */
  39393. type: FILE_TYPE,
  39394. lstat: false,
  39395. depth: 2147483648,
  39396. alwaysStat: false
  39397. };
  39398. }
  39399. constructor(options = {}) {
  39400. super({
  39401. objectMode: true,
  39402. autoDestroy: true,
  39403. highWaterMark: options.highWaterMark || 4096
  39404. });
  39405. const opts = { ...ReaddirpStream.defaultOptions, ...options };
  39406. const { root, type } = opts;
  39407. this._fileFilter = normalizeFilter(opts.fileFilter);
  39408. this._directoryFilter = normalizeFilter(opts.directoryFilter);
  39409. const statMethod = opts.lstat ? lstat$2 : stat$3;
  39410. // Use bigint stats if it's windows and stat() supports options (node 10+).
  39411. if (wantBigintFsStats) {
  39412. this._stat = path => statMethod(path, { bigint: true });
  39413. } else {
  39414. this._stat = statMethod;
  39415. }
  39416. this._maxDepth = opts.depth;
  39417. this._wantsDir = [DIR_TYPE, FILE_DIR_TYPE, EVERYTHING_TYPE].includes(type);
  39418. this._wantsFile = [FILE_TYPE, FILE_DIR_TYPE, EVERYTHING_TYPE].includes(type);
  39419. this._wantsEverything = type === EVERYTHING_TYPE;
  39420. this._root = sysPath$3.resolve(root);
  39421. this._isDirent = ('Dirent' in fs$8) && !opts.alwaysStat;
  39422. this._statsProp = this._isDirent ? 'dirent' : 'stats';
  39423. this._rdOptions = { encoding: 'utf8', withFileTypes: this._isDirent };
  39424. // Launch stream with one parent, the root dir.
  39425. this.parents = [this._exploreDir(root, 1)];
  39426. this.reading = false;
  39427. this.parent = undefined;
  39428. }
  39429. async _read(batch) {
  39430. if (this.reading) return;
  39431. this.reading = true;
  39432. try {
  39433. while (!this.destroyed && batch > 0) {
  39434. const { path, depth, files = [] } = this.parent || {};
  39435. if (files.length > 0) {
  39436. const slice = files.splice(0, batch).map(dirent => this._formatEntry(dirent, path));
  39437. for (const entry of await Promise.all(slice)) {
  39438. if (this.destroyed) return;
  39439. const entryType = await this._getEntryType(entry);
  39440. if (entryType === 'directory' && this._directoryFilter(entry)) {
  39441. if (depth <= this._maxDepth) {
  39442. this.parents.push(this._exploreDir(entry.fullPath, depth + 1));
  39443. }
  39444. if (this._wantsDir) {
  39445. this.push(entry);
  39446. batch--;
  39447. }
  39448. } else if ((entryType === 'file' || this._includeAsFile(entry)) && this._fileFilter(entry)) {
  39449. if (this._wantsFile) {
  39450. this.push(entry);
  39451. batch--;
  39452. }
  39453. }
  39454. }
  39455. } else {
  39456. const parent = this.parents.pop();
  39457. if (!parent) {
  39458. this.push(null);
  39459. break;
  39460. }
  39461. this.parent = await parent;
  39462. if (this.destroyed) return;
  39463. }
  39464. }
  39465. } catch (error) {
  39466. this.destroy(error);
  39467. } finally {
  39468. this.reading = false;
  39469. }
  39470. }
  39471. async _exploreDir(path, depth) {
  39472. let files;
  39473. try {
  39474. files = await readdir$1(path, this._rdOptions);
  39475. } catch (error) {
  39476. this._onError(error);
  39477. }
  39478. return { files, depth, path };
  39479. }
  39480. async _formatEntry(dirent, path) {
  39481. let entry;
  39482. try {
  39483. const basename = this._isDirent ? dirent.name : dirent;
  39484. const fullPath = sysPath$3.resolve(sysPath$3.join(path, basename));
  39485. entry = { path: sysPath$3.relative(this._root, fullPath), fullPath, basename };
  39486. entry[this._statsProp] = this._isDirent ? dirent : await this._stat(fullPath);
  39487. } catch (err) {
  39488. this._onError(err);
  39489. }
  39490. return entry;
  39491. }
  39492. _onError(err) {
  39493. if (isNormalFlowError(err) && !this.destroyed) {
  39494. this.emit('warn', err);
  39495. } else {
  39496. this.destroy(err);
  39497. }
  39498. }
  39499. async _getEntryType(entry) {
  39500. // entry may be undefined, because a warning or an error were emitted
  39501. // and the statsProp is undefined
  39502. const stats = entry && entry[this._statsProp];
  39503. if (!stats) {
  39504. return;
  39505. }
  39506. if (stats.isFile()) {
  39507. return 'file';
  39508. }
  39509. if (stats.isDirectory()) {
  39510. return 'directory';
  39511. }
  39512. if (stats && stats.isSymbolicLink()) {
  39513. const full = entry.fullPath;
  39514. try {
  39515. const entryRealPath = await realpath$1(full);
  39516. const entryRealPathStats = await lstat$2(entryRealPath);
  39517. if (entryRealPathStats.isFile()) {
  39518. return 'file';
  39519. }
  39520. if (entryRealPathStats.isDirectory()) {
  39521. const len = entryRealPath.length;
  39522. if (full.startsWith(entryRealPath) && full.substr(len, 1) === sysPath$3.sep) {
  39523. const recursiveError = new Error(
  39524. `Circular symlink detected: "${full}" points to "${entryRealPath}"`
  39525. );
  39526. recursiveError.code = RECURSIVE_ERROR_CODE;
  39527. return this._onError(recursiveError);
  39528. }
  39529. return 'directory';
  39530. }
  39531. } catch (error) {
  39532. this._onError(error);
  39533. }
  39534. }
  39535. }
  39536. _includeAsFile(entry) {
  39537. const stats = entry && entry[this._statsProp];
  39538. return stats && this._wantsEverything && !stats.isDirectory();
  39539. }
  39540. }
  39541. /**
  39542. * @typedef {Object} ReaddirpArguments
  39543. * @property {Function=} fileFilter
  39544. * @property {Function=} directoryFilter
  39545. * @property {String=} type
  39546. * @property {Number=} depth
  39547. * @property {String=} root
  39548. * @property {Boolean=} lstat
  39549. * @property {Boolean=} bigint
  39550. */
  39551. /**
  39552. * Main function which ends up calling readdirRec and reads all files and directories in given root recursively.
  39553. * @param {String} root Root directory
  39554. * @param {ReaddirpArguments=} options Options to specify root (start directory), filters and recursion depth
  39555. */
  39556. const readdirp$1 = (root, options = {}) => {
  39557. let type = options.entryType || options.type;
  39558. if (type === 'both') type = FILE_DIR_TYPE; // backwards-compatibility
  39559. if (type) options.type = type;
  39560. if (!root) {
  39561. throw new Error('readdirp: root argument is required. Usage: readdirp(root, options)');
  39562. } else if (typeof root !== 'string') {
  39563. throw new TypeError('readdirp: root argument must be a string. Usage: readdirp(root, options)');
  39564. } else if (type && !ALL_TYPES.includes(type)) {
  39565. throw new Error(`readdirp: Invalid type passed. Use one of ${ALL_TYPES.join(', ')}`);
  39566. }
  39567. options.root = root;
  39568. return new ReaddirpStream(options);
  39569. };
  39570. const readdirpPromise = (root, options = {}) => {
  39571. return new Promise((resolve, reject) => {
  39572. const files = [];
  39573. readdirp$1(root, options)
  39574. .on('data', entry => files.push(entry))
  39575. .on('end', () => resolve(files))
  39576. .on('error', error => reject(error));
  39577. });
  39578. };
  39579. readdirp$1.promise = readdirpPromise;
  39580. readdirp$1.ReaddirpStream = ReaddirpStream;
  39581. readdirp$1.default = readdirp$1;
  39582. var readdirp_1 = readdirp$1;
  39583. var anymatch$2 = {exports: {}};
  39584. /*!
  39585. * normalize-path <https://github.com/jonschlinkert/normalize-path>
  39586. *
  39587. * Copyright (c) 2014-2018, Jon Schlinkert.
  39588. * Released under the MIT License.
  39589. */
  39590. var normalizePath$2 = function(path, stripTrailing) {
  39591. if (typeof path !== 'string') {
  39592. throw new TypeError('expected path to be a string');
  39593. }
  39594. if (path === '\\' || path === '/') return '/';
  39595. var len = path.length;
  39596. if (len <= 1) return path;
  39597. // ensure that win32 namespaces has two leading slashes, so that the path is
  39598. // handled properly by the win32 version of path.parse() after being normalized
  39599. // https://msdn.microsoft.com/library/windows/desktop/aa365247(v=vs.85).aspx#namespaces
  39600. var prefix = '';
  39601. if (len > 4 && path[3] === '\\') {
  39602. var ch = path[2];
  39603. if ((ch === '?' || ch === '.') && path.slice(0, 2) === '\\\\') {
  39604. path = path.slice(2);
  39605. prefix = '//';
  39606. }
  39607. }
  39608. var segs = path.split(/[/\\]+/);
  39609. if (stripTrailing !== false && segs[segs.length - 1] === '') {
  39610. segs.pop();
  39611. }
  39612. return prefix + segs.join('/');
  39613. };
  39614. var anymatch_1 = anymatch$2.exports;
  39615. Object.defineProperty(anymatch_1, "__esModule", { value: true });
  39616. const picomatch = picomatch$3;
  39617. const normalizePath$1 = normalizePath$2;
  39618. /**
  39619. * @typedef {(testString: string) => boolean} AnymatchFn
  39620. * @typedef {string|RegExp|AnymatchFn} AnymatchPattern
  39621. * @typedef {AnymatchPattern|AnymatchPattern[]} AnymatchMatcher
  39622. */
  39623. const BANG$1 = '!';
  39624. const DEFAULT_OPTIONS = {returnIndex: false};
  39625. const arrify$1 = (item) => Array.isArray(item) ? item : [item];
  39626. /**
  39627. * @param {AnymatchPattern} matcher
  39628. * @param {object} options
  39629. * @returns {AnymatchFn}
  39630. */
  39631. const createPattern = (matcher, options) => {
  39632. if (typeof matcher === 'function') {
  39633. return matcher;
  39634. }
  39635. if (typeof matcher === 'string') {
  39636. const glob = picomatch(matcher, options);
  39637. return (string) => matcher === string || glob(string);
  39638. }
  39639. if (matcher instanceof RegExp) {
  39640. return (string) => matcher.test(string);
  39641. }
  39642. return (string) => false;
  39643. };
  39644. /**
  39645. * @param {Array<Function>} patterns
  39646. * @param {Array<Function>} negPatterns
  39647. * @param {String|Array} args
  39648. * @param {Boolean} returnIndex
  39649. * @returns {boolean|number}
  39650. */
  39651. const matchPatterns = (patterns, negPatterns, args, returnIndex) => {
  39652. const isList = Array.isArray(args);
  39653. const _path = isList ? args[0] : args;
  39654. if (!isList && typeof _path !== 'string') {
  39655. throw new TypeError('anymatch: second argument must be a string: got ' +
  39656. Object.prototype.toString.call(_path))
  39657. }
  39658. const path = normalizePath$1(_path);
  39659. for (let index = 0; index < negPatterns.length; index++) {
  39660. const nglob = negPatterns[index];
  39661. if (nglob(path)) {
  39662. return returnIndex ? -1 : false;
  39663. }
  39664. }
  39665. const applied = isList && [path].concat(args.slice(1));
  39666. for (let index = 0; index < patterns.length; index++) {
  39667. const pattern = patterns[index];
  39668. if (isList ? pattern(...applied) : pattern(path)) {
  39669. return returnIndex ? index : true;
  39670. }
  39671. }
  39672. return returnIndex ? -1 : false;
  39673. };
  39674. /**
  39675. * @param {AnymatchMatcher} matchers
  39676. * @param {Array|string} testString
  39677. * @param {object} options
  39678. * @returns {boolean|number|Function}
  39679. */
  39680. const anymatch$1 = (matchers, testString, options = DEFAULT_OPTIONS) => {
  39681. if (matchers == null) {
  39682. throw new TypeError('anymatch: specify first argument');
  39683. }
  39684. const opts = typeof options === 'boolean' ? {returnIndex: options} : options;
  39685. const returnIndex = opts.returnIndex || false;
  39686. // Early cache for matchers.
  39687. const mtchers = arrify$1(matchers);
  39688. const negatedGlobs = mtchers
  39689. .filter(item => typeof item === 'string' && item.charAt(0) === BANG$1)
  39690. .map(item => item.slice(1))
  39691. .map(item => picomatch(item, opts));
  39692. const patterns = mtchers
  39693. .filter(item => typeof item !== 'string' || (typeof item === 'string' && item.charAt(0) !== BANG$1))
  39694. .map(matcher => createPattern(matcher, opts));
  39695. if (testString == null) {
  39696. return (testString, ri = false) => {
  39697. const returnIndex = typeof ri === 'boolean' ? ri : false;
  39698. return matchPatterns(patterns, negatedGlobs, testString, returnIndex);
  39699. }
  39700. }
  39701. return matchPatterns(patterns, negatedGlobs, testString, returnIndex);
  39702. };
  39703. anymatch$1.default = anymatch$1;
  39704. anymatch$2.exports = anymatch$1;
  39705. var anymatchExports = anymatch$2.exports;
  39706. var require$$0 = [
  39707. "3dm",
  39708. "3ds",
  39709. "3g2",
  39710. "3gp",
  39711. "7z",
  39712. "a",
  39713. "aac",
  39714. "adp",
  39715. "ai",
  39716. "aif",
  39717. "aiff",
  39718. "alz",
  39719. "ape",
  39720. "apk",
  39721. "appimage",
  39722. "ar",
  39723. "arj",
  39724. "asf",
  39725. "au",
  39726. "avi",
  39727. "bak",
  39728. "baml",
  39729. "bh",
  39730. "bin",
  39731. "bk",
  39732. "bmp",
  39733. "btif",
  39734. "bz2",
  39735. "bzip2",
  39736. "cab",
  39737. "caf",
  39738. "cgm",
  39739. "class",
  39740. "cmx",
  39741. "cpio",
  39742. "cr2",
  39743. "cur",
  39744. "dat",
  39745. "dcm",
  39746. "deb",
  39747. "dex",
  39748. "djvu",
  39749. "dll",
  39750. "dmg",
  39751. "dng",
  39752. "doc",
  39753. "docm",
  39754. "docx",
  39755. "dot",
  39756. "dotm",
  39757. "dra",
  39758. "DS_Store",
  39759. "dsk",
  39760. "dts",
  39761. "dtshd",
  39762. "dvb",
  39763. "dwg",
  39764. "dxf",
  39765. "ecelp4800",
  39766. "ecelp7470",
  39767. "ecelp9600",
  39768. "egg",
  39769. "eol",
  39770. "eot",
  39771. "epub",
  39772. "exe",
  39773. "f4v",
  39774. "fbs",
  39775. "fh",
  39776. "fla",
  39777. "flac",
  39778. "flatpak",
  39779. "fli",
  39780. "flv",
  39781. "fpx",
  39782. "fst",
  39783. "fvt",
  39784. "g3",
  39785. "gh",
  39786. "gif",
  39787. "graffle",
  39788. "gz",
  39789. "gzip",
  39790. "h261",
  39791. "h263",
  39792. "h264",
  39793. "icns",
  39794. "ico",
  39795. "ief",
  39796. "img",
  39797. "ipa",
  39798. "iso",
  39799. "jar",
  39800. "jpeg",
  39801. "jpg",
  39802. "jpgv",
  39803. "jpm",
  39804. "jxr",
  39805. "key",
  39806. "ktx",
  39807. "lha",
  39808. "lib",
  39809. "lvp",
  39810. "lz",
  39811. "lzh",
  39812. "lzma",
  39813. "lzo",
  39814. "m3u",
  39815. "m4a",
  39816. "m4v",
  39817. "mar",
  39818. "mdi",
  39819. "mht",
  39820. "mid",
  39821. "midi",
  39822. "mj2",
  39823. "mka",
  39824. "mkv",
  39825. "mmr",
  39826. "mng",
  39827. "mobi",
  39828. "mov",
  39829. "movie",
  39830. "mp3",
  39831. "mp4",
  39832. "mp4a",
  39833. "mpeg",
  39834. "mpg",
  39835. "mpga",
  39836. "mxu",
  39837. "nef",
  39838. "npx",
  39839. "numbers",
  39840. "nupkg",
  39841. "o",
  39842. "odp",
  39843. "ods",
  39844. "odt",
  39845. "oga",
  39846. "ogg",
  39847. "ogv",
  39848. "otf",
  39849. "ott",
  39850. "pages",
  39851. "pbm",
  39852. "pcx",
  39853. "pdb",
  39854. "pdf",
  39855. "pea",
  39856. "pgm",
  39857. "pic",
  39858. "png",
  39859. "pnm",
  39860. "pot",
  39861. "potm",
  39862. "potx",
  39863. "ppa",
  39864. "ppam",
  39865. "ppm",
  39866. "pps",
  39867. "ppsm",
  39868. "ppsx",
  39869. "ppt",
  39870. "pptm",
  39871. "pptx",
  39872. "psd",
  39873. "pya",
  39874. "pyc",
  39875. "pyo",
  39876. "pyv",
  39877. "qt",
  39878. "rar",
  39879. "ras",
  39880. "raw",
  39881. "resources",
  39882. "rgb",
  39883. "rip",
  39884. "rlc",
  39885. "rmf",
  39886. "rmvb",
  39887. "rpm",
  39888. "rtf",
  39889. "rz",
  39890. "s3m",
  39891. "s7z",
  39892. "scpt",
  39893. "sgi",
  39894. "shar",
  39895. "snap",
  39896. "sil",
  39897. "sketch",
  39898. "slk",
  39899. "smv",
  39900. "snk",
  39901. "so",
  39902. "stl",
  39903. "suo",
  39904. "sub",
  39905. "swf",
  39906. "tar",
  39907. "tbz",
  39908. "tbz2",
  39909. "tga",
  39910. "tgz",
  39911. "thmx",
  39912. "tif",
  39913. "tiff",
  39914. "tlz",
  39915. "ttc",
  39916. "ttf",
  39917. "txz",
  39918. "udf",
  39919. "uvh",
  39920. "uvi",
  39921. "uvm",
  39922. "uvp",
  39923. "uvs",
  39924. "uvu",
  39925. "viv",
  39926. "vob",
  39927. "war",
  39928. "wav",
  39929. "wax",
  39930. "wbmp",
  39931. "wdp",
  39932. "weba",
  39933. "webm",
  39934. "webp",
  39935. "whl",
  39936. "wim",
  39937. "wm",
  39938. "wma",
  39939. "wmv",
  39940. "wmx",
  39941. "woff",
  39942. "woff2",
  39943. "wrm",
  39944. "wvx",
  39945. "xbm",
  39946. "xif",
  39947. "xla",
  39948. "xlam",
  39949. "xls",
  39950. "xlsb",
  39951. "xlsm",
  39952. "xlsx",
  39953. "xlt",
  39954. "xltm",
  39955. "xltx",
  39956. "xm",
  39957. "xmind",
  39958. "xpi",
  39959. "xpm",
  39960. "xwd",
  39961. "xz",
  39962. "z",
  39963. "zip",
  39964. "zipx"
  39965. ];
  39966. var binaryExtensions$1 = require$$0;
  39967. const path$8 = require$$0$4;
  39968. const binaryExtensions = binaryExtensions$1;
  39969. const extensions = new Set(binaryExtensions);
  39970. var isBinaryPath$1 = filePath => extensions.has(path$8.extname(filePath).slice(1).toLowerCase());
  39971. var constants$1 = {};
  39972. (function (exports) {
  39973. const {sep} = require$$0$4;
  39974. const {platform} = process;
  39975. const os = require$$2;
  39976. exports.EV_ALL = 'all';
  39977. exports.EV_READY = 'ready';
  39978. exports.EV_ADD = 'add';
  39979. exports.EV_CHANGE = 'change';
  39980. exports.EV_ADD_DIR = 'addDir';
  39981. exports.EV_UNLINK = 'unlink';
  39982. exports.EV_UNLINK_DIR = 'unlinkDir';
  39983. exports.EV_RAW = 'raw';
  39984. exports.EV_ERROR = 'error';
  39985. exports.STR_DATA = 'data';
  39986. exports.STR_END = 'end';
  39987. exports.STR_CLOSE = 'close';
  39988. exports.FSEVENT_CREATED = 'created';
  39989. exports.FSEVENT_MODIFIED = 'modified';
  39990. exports.FSEVENT_DELETED = 'deleted';
  39991. exports.FSEVENT_MOVED = 'moved';
  39992. exports.FSEVENT_CLONED = 'cloned';
  39993. exports.FSEVENT_UNKNOWN = 'unknown';
  39994. exports.FSEVENT_FLAG_MUST_SCAN_SUBDIRS = 1;
  39995. exports.FSEVENT_TYPE_FILE = 'file';
  39996. exports.FSEVENT_TYPE_DIRECTORY = 'directory';
  39997. exports.FSEVENT_TYPE_SYMLINK = 'symlink';
  39998. exports.KEY_LISTENERS = 'listeners';
  39999. exports.KEY_ERR = 'errHandlers';
  40000. exports.KEY_RAW = 'rawEmitters';
  40001. exports.HANDLER_KEYS = [exports.KEY_LISTENERS, exports.KEY_ERR, exports.KEY_RAW];
  40002. exports.DOT_SLASH = `.${sep}`;
  40003. exports.BACK_SLASH_RE = /\\/g;
  40004. exports.DOUBLE_SLASH_RE = /\/\//;
  40005. exports.SLASH_OR_BACK_SLASH_RE = /[/\\]/;
  40006. exports.DOT_RE = /\..*\.(sw[px])$|~$|\.subl.*\.tmp/;
  40007. exports.REPLACER_RE = /^\.[/\\]/;
  40008. exports.SLASH = '/';
  40009. exports.SLASH_SLASH = '//';
  40010. exports.BRACE_START = '{';
  40011. exports.BANG = '!';
  40012. exports.ONE_DOT = '.';
  40013. exports.TWO_DOTS = '..';
  40014. exports.STAR = '*';
  40015. exports.GLOBSTAR = '**';
  40016. exports.ROOT_GLOBSTAR = '/**/*';
  40017. exports.SLASH_GLOBSTAR = '/**';
  40018. exports.DIR_SUFFIX = 'Dir';
  40019. exports.ANYMATCH_OPTS = {dot: true};
  40020. exports.STRING_TYPE = 'string';
  40021. exports.FUNCTION_TYPE = 'function';
  40022. exports.EMPTY_STR = '';
  40023. exports.EMPTY_FN = () => {};
  40024. exports.IDENTITY_FN = val => val;
  40025. exports.isWindows = platform === 'win32';
  40026. exports.isMacos = platform === 'darwin';
  40027. exports.isLinux = platform === 'linux';
  40028. exports.isIBMi = os.type() === 'OS400';
  40029. } (constants$1));
  40030. const fs$7 = require$$0__default;
  40031. const sysPath$2 = require$$0$4;
  40032. const { promisify: promisify$2 } = require$$0$5;
  40033. const isBinaryPath = isBinaryPath$1;
  40034. const {
  40035. isWindows: isWindows$2,
  40036. isLinux,
  40037. EMPTY_FN: EMPTY_FN$2,
  40038. EMPTY_STR: EMPTY_STR$1,
  40039. KEY_LISTENERS,
  40040. KEY_ERR,
  40041. KEY_RAW,
  40042. HANDLER_KEYS,
  40043. EV_CHANGE: EV_CHANGE$2,
  40044. EV_ADD: EV_ADD$2,
  40045. EV_ADD_DIR: EV_ADD_DIR$2,
  40046. EV_ERROR: EV_ERROR$2,
  40047. STR_DATA: STR_DATA$1,
  40048. STR_END: STR_END$2,
  40049. BRACE_START: BRACE_START$1,
  40050. STAR
  40051. } = constants$1;
  40052. const THROTTLE_MODE_WATCH = 'watch';
  40053. const open$2 = promisify$2(fs$7.open);
  40054. const stat$2 = promisify$2(fs$7.stat);
  40055. const lstat$1 = promisify$2(fs$7.lstat);
  40056. const close = promisify$2(fs$7.close);
  40057. const fsrealpath = promisify$2(fs$7.realpath);
  40058. const statMethods$1 = { lstat: lstat$1, stat: stat$2 };
  40059. // TODO: emit errors properly. Example: EMFILE on Macos.
  40060. const foreach = (val, fn) => {
  40061. if (val instanceof Set) {
  40062. val.forEach(fn);
  40063. } else {
  40064. fn(val);
  40065. }
  40066. };
  40067. const addAndConvert = (main, prop, item) => {
  40068. let container = main[prop];
  40069. if (!(container instanceof Set)) {
  40070. main[prop] = container = new Set([container]);
  40071. }
  40072. container.add(item);
  40073. };
  40074. const clearItem = cont => key => {
  40075. const set = cont[key];
  40076. if (set instanceof Set) {
  40077. set.clear();
  40078. } else {
  40079. delete cont[key];
  40080. }
  40081. };
  40082. const delFromSet = (main, prop, item) => {
  40083. const container = main[prop];
  40084. if (container instanceof Set) {
  40085. container.delete(item);
  40086. } else if (container === item) {
  40087. delete main[prop];
  40088. }
  40089. };
  40090. const isEmptySet = (val) => val instanceof Set ? val.size === 0 : !val;
  40091. /**
  40092. * @typedef {String} Path
  40093. */
  40094. // fs_watch helpers
  40095. // object to hold per-process fs_watch instances
  40096. // (may be shared across chokidar FSWatcher instances)
  40097. /**
  40098. * @typedef {Object} FsWatchContainer
  40099. * @property {Set} listeners
  40100. * @property {Set} errHandlers
  40101. * @property {Set} rawEmitters
  40102. * @property {fs.FSWatcher=} watcher
  40103. * @property {Boolean=} watcherUnusable
  40104. */
  40105. /**
  40106. * @type {Map<String,FsWatchContainer>}
  40107. */
  40108. const FsWatchInstances = new Map();
  40109. /**
  40110. * Instantiates the fs_watch interface
  40111. * @param {String} path to be watched
  40112. * @param {Object} options to be passed to fs_watch
  40113. * @param {Function} listener main event handler
  40114. * @param {Function} errHandler emits info about errors
  40115. * @param {Function} emitRaw emits raw event data
  40116. * @returns {fs.FSWatcher} new fsevents instance
  40117. */
  40118. function createFsWatchInstance(path, options, listener, errHandler, emitRaw) {
  40119. const handleEvent = (rawEvent, evPath) => {
  40120. listener(path);
  40121. emitRaw(rawEvent, evPath, {watchedPath: path});
  40122. // emit based on events occurring for files from a directory's watcher in
  40123. // case the file's watcher misses it (and rely on throttling to de-dupe)
  40124. if (evPath && path !== evPath) {
  40125. fsWatchBroadcast(
  40126. sysPath$2.resolve(path, evPath), KEY_LISTENERS, sysPath$2.join(path, evPath)
  40127. );
  40128. }
  40129. };
  40130. try {
  40131. return fs$7.watch(path, options, handleEvent);
  40132. } catch (error) {
  40133. errHandler(error);
  40134. }
  40135. }
  40136. /**
  40137. * Helper for passing fs_watch event data to a collection of listeners
  40138. * @param {Path} fullPath absolute path bound to fs_watch instance
  40139. * @param {String} type listener type
  40140. * @param {*=} val1 arguments to be passed to listeners
  40141. * @param {*=} val2
  40142. * @param {*=} val3
  40143. */
  40144. const fsWatchBroadcast = (fullPath, type, val1, val2, val3) => {
  40145. const cont = FsWatchInstances.get(fullPath);
  40146. if (!cont) return;
  40147. foreach(cont[type], (listener) => {
  40148. listener(val1, val2, val3);
  40149. });
  40150. };
  40151. /**
  40152. * Instantiates the fs_watch interface or binds listeners
  40153. * to an existing one covering the same file system entry
  40154. * @param {String} path
  40155. * @param {String} fullPath absolute path
  40156. * @param {Object} options to be passed to fs_watch
  40157. * @param {Object} handlers container for event listener functions
  40158. */
  40159. const setFsWatchListener = (path, fullPath, options, handlers) => {
  40160. const {listener, errHandler, rawEmitter} = handlers;
  40161. let cont = FsWatchInstances.get(fullPath);
  40162. /** @type {fs.FSWatcher=} */
  40163. let watcher;
  40164. if (!options.persistent) {
  40165. watcher = createFsWatchInstance(
  40166. path, options, listener, errHandler, rawEmitter
  40167. );
  40168. return watcher.close.bind(watcher);
  40169. }
  40170. if (cont) {
  40171. addAndConvert(cont, KEY_LISTENERS, listener);
  40172. addAndConvert(cont, KEY_ERR, errHandler);
  40173. addAndConvert(cont, KEY_RAW, rawEmitter);
  40174. } else {
  40175. watcher = createFsWatchInstance(
  40176. path,
  40177. options,
  40178. fsWatchBroadcast.bind(null, fullPath, KEY_LISTENERS),
  40179. errHandler, // no need to use broadcast here
  40180. fsWatchBroadcast.bind(null, fullPath, KEY_RAW)
  40181. );
  40182. if (!watcher) return;
  40183. watcher.on(EV_ERROR$2, async (error) => {
  40184. const broadcastErr = fsWatchBroadcast.bind(null, fullPath, KEY_ERR);
  40185. cont.watcherUnusable = true; // documented since Node 10.4.1
  40186. // Workaround for https://github.com/joyent/node/issues/4337
  40187. if (isWindows$2 && error.code === 'EPERM') {
  40188. try {
  40189. const fd = await open$2(path, 'r');
  40190. await close(fd);
  40191. broadcastErr(error);
  40192. } catch (err) {}
  40193. } else {
  40194. broadcastErr(error);
  40195. }
  40196. });
  40197. cont = {
  40198. listeners: listener,
  40199. errHandlers: errHandler,
  40200. rawEmitters: rawEmitter,
  40201. watcher
  40202. };
  40203. FsWatchInstances.set(fullPath, cont);
  40204. }
  40205. // const index = cont.listeners.indexOf(listener);
  40206. // removes this instance's listeners and closes the underlying fs_watch
  40207. // instance if there are no more listeners left
  40208. return () => {
  40209. delFromSet(cont, KEY_LISTENERS, listener);
  40210. delFromSet(cont, KEY_ERR, errHandler);
  40211. delFromSet(cont, KEY_RAW, rawEmitter);
  40212. if (isEmptySet(cont.listeners)) {
  40213. // Check to protect against issue gh-730.
  40214. // if (cont.watcherUnusable) {
  40215. cont.watcher.close();
  40216. // }
  40217. FsWatchInstances.delete(fullPath);
  40218. HANDLER_KEYS.forEach(clearItem(cont));
  40219. cont.watcher = undefined;
  40220. Object.freeze(cont);
  40221. }
  40222. };
  40223. };
  40224. // fs_watchFile helpers
  40225. // object to hold per-process fs_watchFile instances
  40226. // (may be shared across chokidar FSWatcher instances)
  40227. const FsWatchFileInstances = new Map();
  40228. /**
  40229. * Instantiates the fs_watchFile interface or binds listeners
  40230. * to an existing one covering the same file system entry
  40231. * @param {String} path to be watched
  40232. * @param {String} fullPath absolute path
  40233. * @param {Object} options options to be passed to fs_watchFile
  40234. * @param {Object} handlers container for event listener functions
  40235. * @returns {Function} closer
  40236. */
  40237. const setFsWatchFileListener = (path, fullPath, options, handlers) => {
  40238. const {listener, rawEmitter} = handlers;
  40239. let cont = FsWatchFileInstances.get(fullPath);
  40240. const copts = cont && cont.options;
  40241. if (copts && (copts.persistent < options.persistent || copts.interval > options.interval)) {
  40242. fs$7.unwatchFile(fullPath);
  40243. cont = undefined;
  40244. }
  40245. /* eslint-enable no-unused-vars, prefer-destructuring */
  40246. if (cont) {
  40247. addAndConvert(cont, KEY_LISTENERS, listener);
  40248. addAndConvert(cont, KEY_RAW, rawEmitter);
  40249. } else {
  40250. // TODO
  40251. // listeners.add(listener);
  40252. // rawEmitters.add(rawEmitter);
  40253. cont = {
  40254. listeners: listener,
  40255. rawEmitters: rawEmitter,
  40256. options,
  40257. watcher: fs$7.watchFile(fullPath, options, (curr, prev) => {
  40258. foreach(cont.rawEmitters, (rawEmitter) => {
  40259. rawEmitter(EV_CHANGE$2, fullPath, {curr, prev});
  40260. });
  40261. const currmtime = curr.mtimeMs;
  40262. if (curr.size !== prev.size || currmtime > prev.mtimeMs || currmtime === 0) {
  40263. foreach(cont.listeners, (listener) => listener(path, curr));
  40264. }
  40265. })
  40266. };
  40267. FsWatchFileInstances.set(fullPath, cont);
  40268. }
  40269. // const index = cont.listeners.indexOf(listener);
  40270. // Removes this instance's listeners and closes the underlying fs_watchFile
  40271. // instance if there are no more listeners left.
  40272. return () => {
  40273. delFromSet(cont, KEY_LISTENERS, listener);
  40274. delFromSet(cont, KEY_RAW, rawEmitter);
  40275. if (isEmptySet(cont.listeners)) {
  40276. FsWatchFileInstances.delete(fullPath);
  40277. fs$7.unwatchFile(fullPath);
  40278. cont.options = cont.watcher = undefined;
  40279. Object.freeze(cont);
  40280. }
  40281. };
  40282. };
  40283. /**
  40284. * @mixin
  40285. */
  40286. let NodeFsHandler$1 = class NodeFsHandler {
  40287. /**
  40288. * @param {import("../index").FSWatcher} fsW
  40289. */
  40290. constructor(fsW) {
  40291. this.fsw = fsW;
  40292. this._boundHandleError = (error) => fsW._handleError(error);
  40293. }
  40294. /**
  40295. * Watch file for changes with fs_watchFile or fs_watch.
  40296. * @param {String} path to file or dir
  40297. * @param {Function} listener on fs change
  40298. * @returns {Function} closer for the watcher instance
  40299. */
  40300. _watchWithNodeFs(path, listener) {
  40301. const opts = this.fsw.options;
  40302. const directory = sysPath$2.dirname(path);
  40303. const basename = sysPath$2.basename(path);
  40304. const parent = this.fsw._getWatchedDir(directory);
  40305. parent.add(basename);
  40306. const absolutePath = sysPath$2.resolve(path);
  40307. const options = {persistent: opts.persistent};
  40308. if (!listener) listener = EMPTY_FN$2;
  40309. let closer;
  40310. if (opts.usePolling) {
  40311. options.interval = opts.enableBinaryInterval && isBinaryPath(basename) ?
  40312. opts.binaryInterval : opts.interval;
  40313. closer = setFsWatchFileListener(path, absolutePath, options, {
  40314. listener,
  40315. rawEmitter: this.fsw._emitRaw
  40316. });
  40317. } else {
  40318. closer = setFsWatchListener(path, absolutePath, options, {
  40319. listener,
  40320. errHandler: this._boundHandleError,
  40321. rawEmitter: this.fsw._emitRaw
  40322. });
  40323. }
  40324. return closer;
  40325. }
  40326. /**
  40327. * Watch a file and emit add event if warranted.
  40328. * @param {Path} file Path
  40329. * @param {fs.Stats} stats result of fs_stat
  40330. * @param {Boolean} initialAdd was the file added at watch instantiation?
  40331. * @returns {Function} closer for the watcher instance
  40332. */
  40333. _handleFile(file, stats, initialAdd) {
  40334. if (this.fsw.closed) {
  40335. return;
  40336. }
  40337. const dirname = sysPath$2.dirname(file);
  40338. const basename = sysPath$2.basename(file);
  40339. const parent = this.fsw._getWatchedDir(dirname);
  40340. // stats is always present
  40341. let prevStats = stats;
  40342. // if the file is already being watched, do nothing
  40343. if (parent.has(basename)) return;
  40344. const listener = async (path, newStats) => {
  40345. if (!this.fsw._throttle(THROTTLE_MODE_WATCH, file, 5)) return;
  40346. if (!newStats || newStats.mtimeMs === 0) {
  40347. try {
  40348. const newStats = await stat$2(file);
  40349. if (this.fsw.closed) return;
  40350. // Check that change event was not fired because of changed only accessTime.
  40351. const at = newStats.atimeMs;
  40352. const mt = newStats.mtimeMs;
  40353. if (!at || at <= mt || mt !== prevStats.mtimeMs) {
  40354. this.fsw._emit(EV_CHANGE$2, file, newStats);
  40355. }
  40356. if (isLinux && prevStats.ino !== newStats.ino) {
  40357. this.fsw._closeFile(path);
  40358. prevStats = newStats;
  40359. this.fsw._addPathCloser(path, this._watchWithNodeFs(file, listener));
  40360. } else {
  40361. prevStats = newStats;
  40362. }
  40363. } catch (error) {
  40364. // Fix issues where mtime is null but file is still present
  40365. this.fsw._remove(dirname, basename);
  40366. }
  40367. // add is about to be emitted if file not already tracked in parent
  40368. } else if (parent.has(basename)) {
  40369. // Check that change event was not fired because of changed only accessTime.
  40370. const at = newStats.atimeMs;
  40371. const mt = newStats.mtimeMs;
  40372. if (!at || at <= mt || mt !== prevStats.mtimeMs) {
  40373. this.fsw._emit(EV_CHANGE$2, file, newStats);
  40374. }
  40375. prevStats = newStats;
  40376. }
  40377. };
  40378. // kick off the watcher
  40379. const closer = this._watchWithNodeFs(file, listener);
  40380. // emit an add event if we're supposed to
  40381. if (!(initialAdd && this.fsw.options.ignoreInitial) && this.fsw._isntIgnored(file)) {
  40382. if (!this.fsw._throttle(EV_ADD$2, file, 0)) return;
  40383. this.fsw._emit(EV_ADD$2, file, stats);
  40384. }
  40385. return closer;
  40386. }
  40387. /**
  40388. * Handle symlinks encountered while reading a dir.
  40389. * @param {Object} entry returned by readdirp
  40390. * @param {String} directory path of dir being read
  40391. * @param {String} path of this item
  40392. * @param {String} item basename of this item
  40393. * @returns {Promise<Boolean>} true if no more processing is needed for this entry.
  40394. */
  40395. async _handleSymlink(entry, directory, path, item) {
  40396. if (this.fsw.closed) {
  40397. return;
  40398. }
  40399. const full = entry.fullPath;
  40400. const dir = this.fsw._getWatchedDir(directory);
  40401. if (!this.fsw.options.followSymlinks) {
  40402. // watch symlink directly (don't follow) and detect changes
  40403. this.fsw._incrReadyCount();
  40404. let linkPath;
  40405. try {
  40406. linkPath = await fsrealpath(path);
  40407. } catch (e) {
  40408. this.fsw._emitReady();
  40409. return true;
  40410. }
  40411. if (this.fsw.closed) return;
  40412. if (dir.has(item)) {
  40413. if (this.fsw._symlinkPaths.get(full) !== linkPath) {
  40414. this.fsw._symlinkPaths.set(full, linkPath);
  40415. this.fsw._emit(EV_CHANGE$2, path, entry.stats);
  40416. }
  40417. } else {
  40418. dir.add(item);
  40419. this.fsw._symlinkPaths.set(full, linkPath);
  40420. this.fsw._emit(EV_ADD$2, path, entry.stats);
  40421. }
  40422. this.fsw._emitReady();
  40423. return true;
  40424. }
  40425. // don't follow the same symlink more than once
  40426. if (this.fsw._symlinkPaths.has(full)) {
  40427. return true;
  40428. }
  40429. this.fsw._symlinkPaths.set(full, true);
  40430. }
  40431. _handleRead(directory, initialAdd, wh, target, dir, depth, throttler) {
  40432. // Normalize the directory name on Windows
  40433. directory = sysPath$2.join(directory, EMPTY_STR$1);
  40434. if (!wh.hasGlob) {
  40435. throttler = this.fsw._throttle('readdir', directory, 1000);
  40436. if (!throttler) return;
  40437. }
  40438. const previous = this.fsw._getWatchedDir(wh.path);
  40439. const current = new Set();
  40440. let stream = this.fsw._readdirp(directory, {
  40441. fileFilter: entry => wh.filterPath(entry),
  40442. directoryFilter: entry => wh.filterDir(entry),
  40443. depth: 0
  40444. }).on(STR_DATA$1, async (entry) => {
  40445. if (this.fsw.closed) {
  40446. stream = undefined;
  40447. return;
  40448. }
  40449. const item = entry.path;
  40450. let path = sysPath$2.join(directory, item);
  40451. current.add(item);
  40452. if (entry.stats.isSymbolicLink() && await this._handleSymlink(entry, directory, path, item)) {
  40453. return;
  40454. }
  40455. if (this.fsw.closed) {
  40456. stream = undefined;
  40457. return;
  40458. }
  40459. // Files that present in current directory snapshot
  40460. // but absent in previous are added to watch list and
  40461. // emit `add` event.
  40462. if (item === target || !target && !previous.has(item)) {
  40463. this.fsw._incrReadyCount();
  40464. // ensure relativeness of path is preserved in case of watcher reuse
  40465. path = sysPath$2.join(dir, sysPath$2.relative(dir, path));
  40466. this._addToNodeFs(path, initialAdd, wh, depth + 1);
  40467. }
  40468. }).on(EV_ERROR$2, this._boundHandleError);
  40469. return new Promise(resolve =>
  40470. stream.once(STR_END$2, () => {
  40471. if (this.fsw.closed) {
  40472. stream = undefined;
  40473. return;
  40474. }
  40475. const wasThrottled = throttler ? throttler.clear() : false;
  40476. resolve();
  40477. // Files that absent in current directory snapshot
  40478. // but present in previous emit `remove` event
  40479. // and are removed from @watched[directory].
  40480. previous.getChildren().filter((item) => {
  40481. return item !== directory &&
  40482. !current.has(item) &&
  40483. // in case of intersecting globs;
  40484. // a path may have been filtered out of this readdir, but
  40485. // shouldn't be removed because it matches a different glob
  40486. (!wh.hasGlob || wh.filterPath({
  40487. fullPath: sysPath$2.resolve(directory, item)
  40488. }));
  40489. }).forEach((item) => {
  40490. this.fsw._remove(directory, item);
  40491. });
  40492. stream = undefined;
  40493. // one more time for any missed in case changes came in extremely quickly
  40494. if (wasThrottled) this._handleRead(directory, false, wh, target, dir, depth, throttler);
  40495. })
  40496. );
  40497. }
  40498. /**
  40499. * Read directory to add / remove files from `@watched` list and re-read it on change.
  40500. * @param {String} dir fs path
  40501. * @param {fs.Stats} stats
  40502. * @param {Boolean} initialAdd
  40503. * @param {Number} depth relative to user-supplied path
  40504. * @param {String} target child path targeted for watch
  40505. * @param {Object} wh Common watch helpers for this path
  40506. * @param {String} realpath
  40507. * @returns {Promise<Function>} closer for the watcher instance.
  40508. */
  40509. async _handleDir(dir, stats, initialAdd, depth, target, wh, realpath) {
  40510. const parentDir = this.fsw._getWatchedDir(sysPath$2.dirname(dir));
  40511. const tracked = parentDir.has(sysPath$2.basename(dir));
  40512. if (!(initialAdd && this.fsw.options.ignoreInitial) && !target && !tracked) {
  40513. if (!wh.hasGlob || wh.globFilter(dir)) this.fsw._emit(EV_ADD_DIR$2, dir, stats);
  40514. }
  40515. // ensure dir is tracked (harmless if redundant)
  40516. parentDir.add(sysPath$2.basename(dir));
  40517. this.fsw._getWatchedDir(dir);
  40518. let throttler;
  40519. let closer;
  40520. const oDepth = this.fsw.options.depth;
  40521. if ((oDepth == null || depth <= oDepth) && !this.fsw._symlinkPaths.has(realpath)) {
  40522. if (!target) {
  40523. await this._handleRead(dir, initialAdd, wh, target, dir, depth, throttler);
  40524. if (this.fsw.closed) return;
  40525. }
  40526. closer = this._watchWithNodeFs(dir, (dirPath, stats) => {
  40527. // if current directory is removed, do nothing
  40528. if (stats && stats.mtimeMs === 0) return;
  40529. this._handleRead(dirPath, false, wh, target, dir, depth, throttler);
  40530. });
  40531. }
  40532. return closer;
  40533. }
  40534. /**
  40535. * Handle added file, directory, or glob pattern.
  40536. * Delegates call to _handleFile / _handleDir after checks.
  40537. * @param {String} path to file or ir
  40538. * @param {Boolean} initialAdd was the file added at watch instantiation?
  40539. * @param {Object} priorWh depth relative to user-supplied path
  40540. * @param {Number} depth Child path actually targeted for watch
  40541. * @param {String=} target Child path actually targeted for watch
  40542. * @returns {Promise}
  40543. */
  40544. async _addToNodeFs(path, initialAdd, priorWh, depth, target) {
  40545. const ready = this.fsw._emitReady;
  40546. if (this.fsw._isIgnored(path) || this.fsw.closed) {
  40547. ready();
  40548. return false;
  40549. }
  40550. const wh = this.fsw._getWatchHelpers(path, depth);
  40551. if (!wh.hasGlob && priorWh) {
  40552. wh.hasGlob = priorWh.hasGlob;
  40553. wh.globFilter = priorWh.globFilter;
  40554. wh.filterPath = entry => priorWh.filterPath(entry);
  40555. wh.filterDir = entry => priorWh.filterDir(entry);
  40556. }
  40557. // evaluate what is at the path we're being asked to watch
  40558. try {
  40559. const stats = await statMethods$1[wh.statMethod](wh.watchPath);
  40560. if (this.fsw.closed) return;
  40561. if (this.fsw._isIgnored(wh.watchPath, stats)) {
  40562. ready();
  40563. return false;
  40564. }
  40565. const follow = this.fsw.options.followSymlinks && !path.includes(STAR) && !path.includes(BRACE_START$1);
  40566. let closer;
  40567. if (stats.isDirectory()) {
  40568. const absPath = sysPath$2.resolve(path);
  40569. const targetPath = follow ? await fsrealpath(path) : path;
  40570. if (this.fsw.closed) return;
  40571. closer = await this._handleDir(wh.watchPath, stats, initialAdd, depth, target, wh, targetPath);
  40572. if (this.fsw.closed) return;
  40573. // preserve this symlink's target path
  40574. if (absPath !== targetPath && targetPath !== undefined) {
  40575. this.fsw._symlinkPaths.set(absPath, targetPath);
  40576. }
  40577. } else if (stats.isSymbolicLink()) {
  40578. const targetPath = follow ? await fsrealpath(path) : path;
  40579. if (this.fsw.closed) return;
  40580. const parent = sysPath$2.dirname(wh.watchPath);
  40581. this.fsw._getWatchedDir(parent).add(wh.watchPath);
  40582. this.fsw._emit(EV_ADD$2, wh.watchPath, stats);
  40583. closer = await this._handleDir(parent, stats, initialAdd, depth, path, wh, targetPath);
  40584. if (this.fsw.closed) return;
  40585. // preserve this symlink's target path
  40586. if (targetPath !== undefined) {
  40587. this.fsw._symlinkPaths.set(sysPath$2.resolve(path), targetPath);
  40588. }
  40589. } else {
  40590. closer = this._handleFile(wh.watchPath, stats, initialAdd);
  40591. }
  40592. ready();
  40593. this.fsw._addPathCloser(path, closer);
  40594. return false;
  40595. } catch (error) {
  40596. if (this.fsw._handleError(error)) {
  40597. ready();
  40598. return path;
  40599. }
  40600. }
  40601. }
  40602. };
  40603. var nodefsHandler = NodeFsHandler$1;
  40604. var fseventsHandler = {exports: {}};
  40605. const fs$6 = require$$0__default;
  40606. const sysPath$1 = require$$0$4;
  40607. const { promisify: promisify$1 } = require$$0$5;
  40608. let fsevents;
  40609. try {
  40610. fsevents = __require('fsevents');
  40611. } catch (error) {
  40612. if (process.env.CHOKIDAR_PRINT_FSEVENTS_REQUIRE_ERROR) console.error(error);
  40613. }
  40614. if (fsevents) {
  40615. // TODO: real check
  40616. const mtch = process.version.match(/v(\d+)\.(\d+)/);
  40617. if (mtch && mtch[1] && mtch[2]) {
  40618. const maj = Number.parseInt(mtch[1], 10);
  40619. const min = Number.parseInt(mtch[2], 10);
  40620. if (maj === 8 && min < 16) {
  40621. fsevents = undefined;
  40622. }
  40623. }
  40624. }
  40625. const {
  40626. EV_ADD: EV_ADD$1,
  40627. EV_CHANGE: EV_CHANGE$1,
  40628. EV_ADD_DIR: EV_ADD_DIR$1,
  40629. EV_UNLINK: EV_UNLINK$1,
  40630. EV_ERROR: EV_ERROR$1,
  40631. STR_DATA,
  40632. STR_END: STR_END$1,
  40633. FSEVENT_CREATED,
  40634. FSEVENT_MODIFIED,
  40635. FSEVENT_DELETED,
  40636. FSEVENT_MOVED,
  40637. // FSEVENT_CLONED,
  40638. FSEVENT_UNKNOWN,
  40639. FSEVENT_FLAG_MUST_SCAN_SUBDIRS,
  40640. FSEVENT_TYPE_FILE,
  40641. FSEVENT_TYPE_DIRECTORY,
  40642. FSEVENT_TYPE_SYMLINK,
  40643. ROOT_GLOBSTAR,
  40644. DIR_SUFFIX,
  40645. DOT_SLASH,
  40646. FUNCTION_TYPE: FUNCTION_TYPE$1,
  40647. EMPTY_FN: EMPTY_FN$1,
  40648. IDENTITY_FN
  40649. } = constants$1;
  40650. const Depth = (value) => isNaN(value) ? {} : {depth: value};
  40651. const stat$1 = promisify$1(fs$6.stat);
  40652. const lstat = promisify$1(fs$6.lstat);
  40653. const realpath = promisify$1(fs$6.realpath);
  40654. const statMethods = { stat: stat$1, lstat };
  40655. /**
  40656. * @typedef {String} Path
  40657. */
  40658. /**
  40659. * @typedef {Object} FsEventsWatchContainer
  40660. * @property {Set<Function>} listeners
  40661. * @property {Function} rawEmitter
  40662. * @property {{stop: Function}} watcher
  40663. */
  40664. // fsevents instance helper functions
  40665. /**
  40666. * Object to hold per-process fsevents instances (may be shared across chokidar FSWatcher instances)
  40667. * @type {Map<Path,FsEventsWatchContainer>}
  40668. */
  40669. const FSEventsWatchers = new Map();
  40670. // Threshold of duplicate path prefixes at which to start
  40671. // consolidating going forward
  40672. const consolidateThreshhold = 10;
  40673. const wrongEventFlags = new Set([
  40674. 69888, 70400, 71424, 72704, 73472, 131328, 131840, 262912
  40675. ]);
  40676. /**
  40677. * Instantiates the fsevents interface
  40678. * @param {Path} path path to be watched
  40679. * @param {Function} callback called when fsevents is bound and ready
  40680. * @returns {{stop: Function}} new fsevents instance
  40681. */
  40682. const createFSEventsInstance = (path, callback) => {
  40683. const stop = fsevents.watch(path, callback);
  40684. return {stop};
  40685. };
  40686. /**
  40687. * Instantiates the fsevents interface or binds listeners to an existing one covering
  40688. * the same file tree.
  40689. * @param {Path} path - to be watched
  40690. * @param {Path} realPath - real path for symlinks
  40691. * @param {Function} listener - called when fsevents emits events
  40692. * @param {Function} rawEmitter - passes data to listeners of the 'raw' event
  40693. * @returns {Function} closer
  40694. */
  40695. function setFSEventsListener(path, realPath, listener, rawEmitter) {
  40696. let watchPath = sysPath$1.extname(realPath) ? sysPath$1.dirname(realPath) : realPath;
  40697. const parentPath = sysPath$1.dirname(watchPath);
  40698. let cont = FSEventsWatchers.get(watchPath);
  40699. // If we've accumulated a substantial number of paths that
  40700. // could have been consolidated by watching one directory
  40701. // above the current one, create a watcher on the parent
  40702. // path instead, so that we do consolidate going forward.
  40703. if (couldConsolidate(parentPath)) {
  40704. watchPath = parentPath;
  40705. }
  40706. const resolvedPath = sysPath$1.resolve(path);
  40707. const hasSymlink = resolvedPath !== realPath;
  40708. const filteredListener = (fullPath, flags, info) => {
  40709. if (hasSymlink) fullPath = fullPath.replace(realPath, resolvedPath);
  40710. if (
  40711. fullPath === resolvedPath ||
  40712. !fullPath.indexOf(resolvedPath + sysPath$1.sep)
  40713. ) listener(fullPath, flags, info);
  40714. };
  40715. // check if there is already a watcher on a parent path
  40716. // modifies `watchPath` to the parent path when it finds a match
  40717. let watchedParent = false;
  40718. for (const watchedPath of FSEventsWatchers.keys()) {
  40719. if (realPath.indexOf(sysPath$1.resolve(watchedPath) + sysPath$1.sep) === 0) {
  40720. watchPath = watchedPath;
  40721. cont = FSEventsWatchers.get(watchPath);
  40722. watchedParent = true;
  40723. break;
  40724. }
  40725. }
  40726. if (cont || watchedParent) {
  40727. cont.listeners.add(filteredListener);
  40728. } else {
  40729. cont = {
  40730. listeners: new Set([filteredListener]),
  40731. rawEmitter,
  40732. watcher: createFSEventsInstance(watchPath, (fullPath, flags) => {
  40733. if (!cont.listeners.size) return;
  40734. if (flags & FSEVENT_FLAG_MUST_SCAN_SUBDIRS) return;
  40735. const info = fsevents.getInfo(fullPath, flags);
  40736. cont.listeners.forEach(list => {
  40737. list(fullPath, flags, info);
  40738. });
  40739. cont.rawEmitter(info.event, fullPath, info);
  40740. })
  40741. };
  40742. FSEventsWatchers.set(watchPath, cont);
  40743. }
  40744. // removes this instance's listeners and closes the underlying fsevents
  40745. // instance if there are no more listeners left
  40746. return () => {
  40747. const lst = cont.listeners;
  40748. lst.delete(filteredListener);
  40749. if (!lst.size) {
  40750. FSEventsWatchers.delete(watchPath);
  40751. if (cont.watcher) return cont.watcher.stop().then(() => {
  40752. cont.rawEmitter = cont.watcher = undefined;
  40753. Object.freeze(cont);
  40754. });
  40755. }
  40756. };
  40757. }
  40758. // Decide whether or not we should start a new higher-level
  40759. // parent watcher
  40760. const couldConsolidate = (path) => {
  40761. let count = 0;
  40762. for (const watchPath of FSEventsWatchers.keys()) {
  40763. if (watchPath.indexOf(path) === 0) {
  40764. count++;
  40765. if (count >= consolidateThreshhold) {
  40766. return true;
  40767. }
  40768. }
  40769. }
  40770. return false;
  40771. };
  40772. // returns boolean indicating whether fsevents can be used
  40773. const canUse = () => fsevents && FSEventsWatchers.size < 128;
  40774. // determines subdirectory traversal levels from root to path
  40775. const calcDepth = (path, root) => {
  40776. let i = 0;
  40777. while (!path.indexOf(root) && (path = sysPath$1.dirname(path)) !== root) i++;
  40778. return i;
  40779. };
  40780. // returns boolean indicating whether the fsevents' event info has the same type
  40781. // as the one returned by fs.stat
  40782. const sameTypes = (info, stats) => (
  40783. info.type === FSEVENT_TYPE_DIRECTORY && stats.isDirectory() ||
  40784. info.type === FSEVENT_TYPE_SYMLINK && stats.isSymbolicLink() ||
  40785. info.type === FSEVENT_TYPE_FILE && stats.isFile()
  40786. );
  40787. /**
  40788. * @mixin
  40789. */
  40790. let FsEventsHandler$1 = class FsEventsHandler {
  40791. /**
  40792. * @param {import('../index').FSWatcher} fsw
  40793. */
  40794. constructor(fsw) {
  40795. this.fsw = fsw;
  40796. }
  40797. checkIgnored(path, stats) {
  40798. const ipaths = this.fsw._ignoredPaths;
  40799. if (this.fsw._isIgnored(path, stats)) {
  40800. ipaths.add(path);
  40801. if (stats && stats.isDirectory()) {
  40802. ipaths.add(path + ROOT_GLOBSTAR);
  40803. }
  40804. return true;
  40805. }
  40806. ipaths.delete(path);
  40807. ipaths.delete(path + ROOT_GLOBSTAR);
  40808. }
  40809. addOrChange(path, fullPath, realPath, parent, watchedDir, item, info, opts) {
  40810. const event = watchedDir.has(item) ? EV_CHANGE$1 : EV_ADD$1;
  40811. this.handleEvent(event, path, fullPath, realPath, parent, watchedDir, item, info, opts);
  40812. }
  40813. async checkExists(path, fullPath, realPath, parent, watchedDir, item, info, opts) {
  40814. try {
  40815. const stats = await stat$1(path);
  40816. if (this.fsw.closed) return;
  40817. if (sameTypes(info, stats)) {
  40818. this.addOrChange(path, fullPath, realPath, parent, watchedDir, item, info, opts);
  40819. } else {
  40820. this.handleEvent(EV_UNLINK$1, path, fullPath, realPath, parent, watchedDir, item, info, opts);
  40821. }
  40822. } catch (error) {
  40823. if (error.code === 'EACCES') {
  40824. this.addOrChange(path, fullPath, realPath, parent, watchedDir, item, info, opts);
  40825. } else {
  40826. this.handleEvent(EV_UNLINK$1, path, fullPath, realPath, parent, watchedDir, item, info, opts);
  40827. }
  40828. }
  40829. }
  40830. handleEvent(event, path, fullPath, realPath, parent, watchedDir, item, info, opts) {
  40831. if (this.fsw.closed || this.checkIgnored(path)) return;
  40832. if (event === EV_UNLINK$1) {
  40833. const isDirectory = info.type === FSEVENT_TYPE_DIRECTORY;
  40834. // suppress unlink events on never before seen files
  40835. if (isDirectory || watchedDir.has(item)) {
  40836. this.fsw._remove(parent, item, isDirectory);
  40837. }
  40838. } else {
  40839. if (event === EV_ADD$1) {
  40840. // track new directories
  40841. if (info.type === FSEVENT_TYPE_DIRECTORY) this.fsw._getWatchedDir(path);
  40842. if (info.type === FSEVENT_TYPE_SYMLINK && opts.followSymlinks) {
  40843. // push symlinks back to the top of the stack to get handled
  40844. const curDepth = opts.depth === undefined ?
  40845. undefined : calcDepth(fullPath, realPath) + 1;
  40846. return this._addToFsEvents(path, false, true, curDepth);
  40847. }
  40848. // track new paths
  40849. // (other than symlinks being followed, which will be tracked soon)
  40850. this.fsw._getWatchedDir(parent).add(item);
  40851. }
  40852. /**
  40853. * @type {'add'|'addDir'|'unlink'|'unlinkDir'}
  40854. */
  40855. const eventName = info.type === FSEVENT_TYPE_DIRECTORY ? event + DIR_SUFFIX : event;
  40856. this.fsw._emit(eventName, path);
  40857. if (eventName === EV_ADD_DIR$1) this._addToFsEvents(path, false, true);
  40858. }
  40859. }
  40860. /**
  40861. * Handle symlinks encountered during directory scan
  40862. * @param {String} watchPath - file/dir path to be watched with fsevents
  40863. * @param {String} realPath - real path (in case of symlinks)
  40864. * @param {Function} transform - path transformer
  40865. * @param {Function} globFilter - path filter in case a glob pattern was provided
  40866. * @returns {Function} closer for the watcher instance
  40867. */
  40868. _watchWithFsEvents(watchPath, realPath, transform, globFilter) {
  40869. if (this.fsw.closed || this.fsw._isIgnored(watchPath)) return;
  40870. const opts = this.fsw.options;
  40871. const watchCallback = async (fullPath, flags, info) => {
  40872. // PATCH: bypass the callback for better perf when fullPath hit the ignored file list
  40873. if (this.fsw.closed || this.fsw._isIgnored(fullPath)) return;
  40874. if (
  40875. opts.depth !== undefined &&
  40876. calcDepth(fullPath, realPath) > opts.depth
  40877. ) return;
  40878. const path = transform(sysPath$1.join(
  40879. watchPath, sysPath$1.relative(watchPath, fullPath)
  40880. ));
  40881. if (globFilter && !globFilter(path)) return;
  40882. // ensure directories are tracked
  40883. const parent = sysPath$1.dirname(path);
  40884. const item = sysPath$1.basename(path);
  40885. const watchedDir = this.fsw._getWatchedDir(
  40886. info.type === FSEVENT_TYPE_DIRECTORY ? path : parent
  40887. );
  40888. // correct for wrong events emitted
  40889. if (wrongEventFlags.has(flags) || info.event === FSEVENT_UNKNOWN) {
  40890. if (typeof opts.ignored === FUNCTION_TYPE$1) {
  40891. let stats;
  40892. try {
  40893. stats = await stat$1(path);
  40894. } catch (error) {}
  40895. if (this.fsw.closed) return;
  40896. if (this.checkIgnored(path, stats)) return;
  40897. if (sameTypes(info, stats)) {
  40898. this.addOrChange(path, fullPath, realPath, parent, watchedDir, item, info, opts);
  40899. } else {
  40900. this.handleEvent(EV_UNLINK$1, path, fullPath, realPath, parent, watchedDir, item, info, opts);
  40901. }
  40902. } else {
  40903. this.checkExists(path, fullPath, realPath, parent, watchedDir, item, info, opts);
  40904. }
  40905. } else {
  40906. switch (info.event) {
  40907. case FSEVENT_CREATED:
  40908. case FSEVENT_MODIFIED:
  40909. return this.addOrChange(path, fullPath, realPath, parent, watchedDir, item, info, opts);
  40910. case FSEVENT_DELETED:
  40911. case FSEVENT_MOVED:
  40912. return this.checkExists(path, fullPath, realPath, parent, watchedDir, item, info, opts);
  40913. }
  40914. }
  40915. };
  40916. const closer = setFSEventsListener(
  40917. watchPath,
  40918. realPath,
  40919. watchCallback,
  40920. this.fsw._emitRaw
  40921. );
  40922. this.fsw._emitReady();
  40923. return closer;
  40924. }
  40925. /**
  40926. * Handle symlinks encountered during directory scan
  40927. * @param {String} linkPath path to symlink
  40928. * @param {String} fullPath absolute path to the symlink
  40929. * @param {Function} transform pre-existing path transformer
  40930. * @param {Number} curDepth level of subdirectories traversed to where symlink is
  40931. * @returns {Promise<void>}
  40932. */
  40933. async _handleFsEventsSymlink(linkPath, fullPath, transform, curDepth) {
  40934. // don't follow the same symlink more than once
  40935. if (this.fsw.closed || this.fsw._symlinkPaths.has(fullPath)) return;
  40936. this.fsw._symlinkPaths.set(fullPath, true);
  40937. this.fsw._incrReadyCount();
  40938. try {
  40939. const linkTarget = await realpath(linkPath);
  40940. if (this.fsw.closed) return;
  40941. if (this.fsw._isIgnored(linkTarget)) {
  40942. return this.fsw._emitReady();
  40943. }
  40944. this.fsw._incrReadyCount();
  40945. // add the linkTarget for watching with a wrapper for transform
  40946. // that causes emitted paths to incorporate the link's path
  40947. this._addToFsEvents(linkTarget || linkPath, (path) => {
  40948. let aliasedPath = linkPath;
  40949. if (linkTarget && linkTarget !== DOT_SLASH) {
  40950. aliasedPath = path.replace(linkTarget, linkPath);
  40951. } else if (path !== DOT_SLASH) {
  40952. aliasedPath = sysPath$1.join(linkPath, path);
  40953. }
  40954. return transform(aliasedPath);
  40955. }, false, curDepth);
  40956. } catch(error) {
  40957. if (this.fsw._handleError(error)) {
  40958. return this.fsw._emitReady();
  40959. }
  40960. }
  40961. }
  40962. /**
  40963. *
  40964. * @param {Path} newPath
  40965. * @param {fs.Stats} stats
  40966. */
  40967. emitAdd(newPath, stats, processPath, opts, forceAdd) {
  40968. const pp = processPath(newPath);
  40969. const isDir = stats.isDirectory();
  40970. const dirObj = this.fsw._getWatchedDir(sysPath$1.dirname(pp));
  40971. const base = sysPath$1.basename(pp);
  40972. // ensure empty dirs get tracked
  40973. if (isDir) this.fsw._getWatchedDir(pp);
  40974. if (dirObj.has(base)) return;
  40975. dirObj.add(base);
  40976. if (!opts.ignoreInitial || forceAdd === true) {
  40977. this.fsw._emit(isDir ? EV_ADD_DIR$1 : EV_ADD$1, pp, stats);
  40978. }
  40979. }
  40980. initWatch(realPath, path, wh, processPath) {
  40981. if (this.fsw.closed) return;
  40982. const closer = this._watchWithFsEvents(
  40983. wh.watchPath,
  40984. sysPath$1.resolve(realPath || wh.watchPath),
  40985. processPath,
  40986. wh.globFilter
  40987. );
  40988. this.fsw._addPathCloser(path, closer);
  40989. }
  40990. /**
  40991. * Handle added path with fsevents
  40992. * @param {String} path file/dir path or glob pattern
  40993. * @param {Function|Boolean=} transform converts working path to what the user expects
  40994. * @param {Boolean=} forceAdd ensure add is emitted
  40995. * @param {Number=} priorDepth Level of subdirectories already traversed.
  40996. * @returns {Promise<void>}
  40997. */
  40998. async _addToFsEvents(path, transform, forceAdd, priorDepth) {
  40999. if (this.fsw.closed) {
  41000. return;
  41001. }
  41002. const opts = this.fsw.options;
  41003. const processPath = typeof transform === FUNCTION_TYPE$1 ? transform : IDENTITY_FN;
  41004. const wh = this.fsw._getWatchHelpers(path);
  41005. // evaluate what is at the path we're being asked to watch
  41006. try {
  41007. const stats = await statMethods[wh.statMethod](wh.watchPath);
  41008. if (this.fsw.closed) return;
  41009. if (this.fsw._isIgnored(wh.watchPath, stats)) {
  41010. throw null;
  41011. }
  41012. if (stats.isDirectory()) {
  41013. // emit addDir unless this is a glob parent
  41014. if (!wh.globFilter) this.emitAdd(processPath(path), stats, processPath, opts, forceAdd);
  41015. // don't recurse further if it would exceed depth setting
  41016. if (priorDepth && priorDepth > opts.depth) return;
  41017. // scan the contents of the dir
  41018. this.fsw._readdirp(wh.watchPath, {
  41019. fileFilter: entry => wh.filterPath(entry),
  41020. directoryFilter: entry => wh.filterDir(entry),
  41021. ...Depth(opts.depth - (priorDepth || 0))
  41022. }).on(STR_DATA, (entry) => {
  41023. // need to check filterPath on dirs b/c filterDir is less restrictive
  41024. if (this.fsw.closed) {
  41025. return;
  41026. }
  41027. if (entry.stats.isDirectory() && !wh.filterPath(entry)) return;
  41028. const joinedPath = sysPath$1.join(wh.watchPath, entry.path);
  41029. const {fullPath} = entry;
  41030. if (wh.followSymlinks && entry.stats.isSymbolicLink()) {
  41031. // preserve the current depth here since it can't be derived from
  41032. // real paths past the symlink
  41033. const curDepth = opts.depth === undefined ?
  41034. undefined : calcDepth(joinedPath, sysPath$1.resolve(wh.watchPath)) + 1;
  41035. this._handleFsEventsSymlink(joinedPath, fullPath, processPath, curDepth);
  41036. } else {
  41037. this.emitAdd(joinedPath, entry.stats, processPath, opts, forceAdd);
  41038. }
  41039. }).on(EV_ERROR$1, EMPTY_FN$1).on(STR_END$1, () => {
  41040. this.fsw._emitReady();
  41041. });
  41042. } else {
  41043. this.emitAdd(wh.watchPath, stats, processPath, opts, forceAdd);
  41044. this.fsw._emitReady();
  41045. }
  41046. } catch (error) {
  41047. if (!error || this.fsw._handleError(error)) {
  41048. // TODO: Strange thing: "should not choke on an ignored watch path" will be failed without 2 ready calls -__-
  41049. this.fsw._emitReady();
  41050. this.fsw._emitReady();
  41051. }
  41052. }
  41053. if (opts.persistent && forceAdd !== true) {
  41054. if (typeof transform === FUNCTION_TYPE$1) {
  41055. // realpath has already been resolved
  41056. this.initWatch(undefined, path, wh, processPath);
  41057. } else {
  41058. let realPath;
  41059. try {
  41060. realPath = await realpath(wh.watchPath);
  41061. } catch (e) {}
  41062. this.initWatch(realPath, path, wh, processPath);
  41063. }
  41064. }
  41065. }
  41066. };
  41067. fseventsHandler.exports = FsEventsHandler$1;
  41068. fseventsHandler.exports.canUse = canUse;
  41069. var fseventsHandlerExports = fseventsHandler.exports;
  41070. const { EventEmitter: EventEmitter$2 } = require$$0$7;
  41071. const fs$5 = require$$0__default;
  41072. const sysPath = require$$0$4;
  41073. const { promisify } = require$$0$5;
  41074. const readdirp = readdirp_1;
  41075. const anymatch = anymatchExports.default;
  41076. const globParent = globParent$2;
  41077. const isGlob = isGlob$2;
  41078. const braces = braces_1;
  41079. const normalizePath = normalizePath$2;
  41080. const NodeFsHandler = nodefsHandler;
  41081. const FsEventsHandler = fseventsHandlerExports;
  41082. const {
  41083. EV_ALL,
  41084. EV_READY,
  41085. EV_ADD,
  41086. EV_CHANGE,
  41087. EV_UNLINK,
  41088. EV_ADD_DIR,
  41089. EV_UNLINK_DIR,
  41090. EV_RAW,
  41091. EV_ERROR,
  41092. STR_CLOSE,
  41093. STR_END,
  41094. BACK_SLASH_RE,
  41095. DOUBLE_SLASH_RE,
  41096. SLASH_OR_BACK_SLASH_RE,
  41097. DOT_RE,
  41098. REPLACER_RE,
  41099. SLASH,
  41100. SLASH_SLASH,
  41101. BRACE_START,
  41102. BANG,
  41103. ONE_DOT,
  41104. TWO_DOTS,
  41105. GLOBSTAR,
  41106. SLASH_GLOBSTAR,
  41107. ANYMATCH_OPTS,
  41108. STRING_TYPE,
  41109. FUNCTION_TYPE,
  41110. EMPTY_STR,
  41111. EMPTY_FN,
  41112. isWindows: isWindows$1,
  41113. isMacos,
  41114. isIBMi
  41115. } = constants$1;
  41116. const stat = promisify(fs$5.stat);
  41117. const readdir = promisify(fs$5.readdir);
  41118. /**
  41119. * @typedef {String} Path
  41120. * @typedef {'all'|'add'|'addDir'|'change'|'unlink'|'unlinkDir'|'raw'|'error'|'ready'} EventName
  41121. * @typedef {'readdir'|'watch'|'add'|'remove'|'change'} ThrottleType
  41122. */
  41123. /**
  41124. *
  41125. * @typedef {Object} WatchHelpers
  41126. * @property {Boolean} followSymlinks
  41127. * @property {'stat'|'lstat'} statMethod
  41128. * @property {Path} path
  41129. * @property {Path} watchPath
  41130. * @property {Function} entryPath
  41131. * @property {Boolean} hasGlob
  41132. * @property {Object} globFilter
  41133. * @property {Function} filterPath
  41134. * @property {Function} filterDir
  41135. */
  41136. const arrify = (value = []) => Array.isArray(value) ? value : [value];
  41137. const flatten = (list, result = []) => {
  41138. list.forEach(item => {
  41139. if (Array.isArray(item)) {
  41140. flatten(item, result);
  41141. } else {
  41142. result.push(item);
  41143. }
  41144. });
  41145. return result;
  41146. };
  41147. const unifyPaths = (paths_) => {
  41148. /**
  41149. * @type {Array<String>}
  41150. */
  41151. const paths = flatten(arrify(paths_));
  41152. if (!paths.every(p => typeof p === STRING_TYPE)) {
  41153. throw new TypeError(`Non-string provided as watch path: ${paths}`);
  41154. }
  41155. return paths.map(normalizePathToUnix);
  41156. };
  41157. // If SLASH_SLASH occurs at the beginning of path, it is not replaced
  41158. // because "//StoragePC/DrivePool/Movies" is a valid network path
  41159. const toUnix = (string) => {
  41160. let str = string.replace(BACK_SLASH_RE, SLASH);
  41161. let prepend = false;
  41162. if (str.startsWith(SLASH_SLASH)) {
  41163. prepend = true;
  41164. }
  41165. while (str.match(DOUBLE_SLASH_RE)) {
  41166. str = str.replace(DOUBLE_SLASH_RE, SLASH);
  41167. }
  41168. if (prepend) {
  41169. str = SLASH + str;
  41170. }
  41171. return str;
  41172. };
  41173. // Our version of upath.normalize
  41174. // TODO: this is not equal to path-normalize module - investigate why
  41175. const normalizePathToUnix = (path) => toUnix(sysPath.normalize(toUnix(path)));
  41176. const normalizeIgnored = (cwd = EMPTY_STR) => (path) => {
  41177. if (typeof path !== STRING_TYPE) return path;
  41178. return normalizePathToUnix(sysPath.isAbsolute(path) ? path : sysPath.join(cwd, path));
  41179. };
  41180. const getAbsolutePath = (path, cwd) => {
  41181. if (sysPath.isAbsolute(path)) {
  41182. return path;
  41183. }
  41184. if (path.startsWith(BANG)) {
  41185. return BANG + sysPath.join(cwd, path.slice(1));
  41186. }
  41187. return sysPath.join(cwd, path);
  41188. };
  41189. const undef = (opts, key) => opts[key] === undefined;
  41190. /**
  41191. * Directory entry.
  41192. * @property {Path} path
  41193. * @property {Set<Path>} items
  41194. */
  41195. class DirEntry {
  41196. /**
  41197. * @param {Path} dir
  41198. * @param {Function} removeWatcher
  41199. */
  41200. constructor(dir, removeWatcher) {
  41201. this.path = dir;
  41202. this._removeWatcher = removeWatcher;
  41203. /** @type {Set<Path>} */
  41204. this.items = new Set();
  41205. }
  41206. add(item) {
  41207. const {items} = this;
  41208. if (!items) return;
  41209. if (item !== ONE_DOT && item !== TWO_DOTS) items.add(item);
  41210. }
  41211. async remove(item) {
  41212. const {items} = this;
  41213. if (!items) return;
  41214. items.delete(item);
  41215. if (items.size > 0) return;
  41216. const dir = this.path;
  41217. try {
  41218. await readdir(dir);
  41219. } catch (err) {
  41220. if (this._removeWatcher) {
  41221. this._removeWatcher(sysPath.dirname(dir), sysPath.basename(dir));
  41222. }
  41223. }
  41224. }
  41225. has(item) {
  41226. const {items} = this;
  41227. if (!items) return;
  41228. return items.has(item);
  41229. }
  41230. /**
  41231. * @returns {Array<String>}
  41232. */
  41233. getChildren() {
  41234. const {items} = this;
  41235. if (!items) return;
  41236. return [...items.values()];
  41237. }
  41238. dispose() {
  41239. this.items.clear();
  41240. delete this.path;
  41241. delete this._removeWatcher;
  41242. delete this.items;
  41243. Object.freeze(this);
  41244. }
  41245. }
  41246. const STAT_METHOD_F = 'stat';
  41247. const STAT_METHOD_L = 'lstat';
  41248. class WatchHelper {
  41249. constructor(path, watchPath, follow, fsw) {
  41250. this.fsw = fsw;
  41251. this.path = path = path.replace(REPLACER_RE, EMPTY_STR);
  41252. this.watchPath = watchPath;
  41253. this.fullWatchPath = sysPath.resolve(watchPath);
  41254. this.hasGlob = watchPath !== path;
  41255. /** @type {object|boolean} */
  41256. if (path === EMPTY_STR) this.hasGlob = false;
  41257. this.globSymlink = this.hasGlob && follow ? undefined : false;
  41258. this.globFilter = this.hasGlob ? anymatch(path, undefined, ANYMATCH_OPTS) : false;
  41259. this.dirParts = this.getDirParts(path);
  41260. this.dirParts.forEach((parts) => {
  41261. if (parts.length > 1) parts.pop();
  41262. });
  41263. this.followSymlinks = follow;
  41264. this.statMethod = follow ? STAT_METHOD_F : STAT_METHOD_L;
  41265. }
  41266. checkGlobSymlink(entry) {
  41267. // only need to resolve once
  41268. // first entry should always have entry.parentDir === EMPTY_STR
  41269. if (this.globSymlink === undefined) {
  41270. this.globSymlink = entry.fullParentDir === this.fullWatchPath ?
  41271. false : {realPath: entry.fullParentDir, linkPath: this.fullWatchPath};
  41272. }
  41273. if (this.globSymlink) {
  41274. return entry.fullPath.replace(this.globSymlink.realPath, this.globSymlink.linkPath);
  41275. }
  41276. return entry.fullPath;
  41277. }
  41278. entryPath(entry) {
  41279. return sysPath.join(this.watchPath,
  41280. sysPath.relative(this.watchPath, this.checkGlobSymlink(entry))
  41281. );
  41282. }
  41283. filterPath(entry) {
  41284. const {stats} = entry;
  41285. if (stats && stats.isSymbolicLink()) return this.filterDir(entry);
  41286. const resolvedPath = this.entryPath(entry);
  41287. const matchesGlob = this.hasGlob && typeof this.globFilter === FUNCTION_TYPE ?
  41288. this.globFilter(resolvedPath) : true;
  41289. return matchesGlob &&
  41290. this.fsw._isntIgnored(resolvedPath, stats) &&
  41291. this.fsw._hasReadPermissions(stats);
  41292. }
  41293. getDirParts(path) {
  41294. if (!this.hasGlob) return [];
  41295. const parts = [];
  41296. const expandedPath = path.includes(BRACE_START) ? braces.expand(path) : [path];
  41297. expandedPath.forEach((path) => {
  41298. parts.push(sysPath.relative(this.watchPath, path).split(SLASH_OR_BACK_SLASH_RE));
  41299. });
  41300. return parts;
  41301. }
  41302. filterDir(entry) {
  41303. if (this.hasGlob) {
  41304. const entryParts = this.getDirParts(this.checkGlobSymlink(entry));
  41305. let globstar = false;
  41306. this.unmatchedGlob = !this.dirParts.some((parts) => {
  41307. return parts.every((part, i) => {
  41308. if (part === GLOBSTAR) globstar = true;
  41309. return globstar || !entryParts[0][i] || anymatch(part, entryParts[0][i], ANYMATCH_OPTS);
  41310. });
  41311. });
  41312. }
  41313. return !this.unmatchedGlob && this.fsw._isntIgnored(this.entryPath(entry), entry.stats);
  41314. }
  41315. }
  41316. /**
  41317. * Watches files & directories for changes. Emitted events:
  41318. * `add`, `addDir`, `change`, `unlink`, `unlinkDir`, `all`, `error`
  41319. *
  41320. * new FSWatcher()
  41321. * .add(directories)
  41322. * .on('add', path => log('File', path, 'was added'))
  41323. */
  41324. class FSWatcher extends EventEmitter$2 {
  41325. // Not indenting methods for history sake; for now.
  41326. constructor(_opts) {
  41327. super();
  41328. const opts = {};
  41329. if (_opts) Object.assign(opts, _opts); // for frozen objects
  41330. /** @type {Map<String, DirEntry>} */
  41331. this._watched = new Map();
  41332. /** @type {Map<String, Array>} */
  41333. this._closers = new Map();
  41334. /** @type {Set<String>} */
  41335. this._ignoredPaths = new Set();
  41336. /** @type {Map<ThrottleType, Map>} */
  41337. this._throttled = new Map();
  41338. /** @type {Map<Path, String|Boolean>} */
  41339. this._symlinkPaths = new Map();
  41340. this._streams = new Set();
  41341. this.closed = false;
  41342. // Set up default options.
  41343. if (undef(opts, 'persistent')) opts.persistent = true;
  41344. if (undef(opts, 'ignoreInitial')) opts.ignoreInitial = false;
  41345. if (undef(opts, 'ignorePermissionErrors')) opts.ignorePermissionErrors = false;
  41346. if (undef(opts, 'interval')) opts.interval = 100;
  41347. if (undef(opts, 'binaryInterval')) opts.binaryInterval = 300;
  41348. if (undef(opts, 'disableGlobbing')) opts.disableGlobbing = false;
  41349. opts.enableBinaryInterval = opts.binaryInterval !== opts.interval;
  41350. // Enable fsevents on OS X when polling isn't explicitly enabled.
  41351. if (undef(opts, 'useFsEvents')) opts.useFsEvents = !opts.usePolling;
  41352. // If we can't use fsevents, ensure the options reflect it's disabled.
  41353. const canUseFsEvents = FsEventsHandler.canUse();
  41354. if (!canUseFsEvents) opts.useFsEvents = false;
  41355. // Use polling on Mac if not using fsevents.
  41356. // Other platforms use non-polling fs_watch.
  41357. if (undef(opts, 'usePolling') && !opts.useFsEvents) {
  41358. opts.usePolling = isMacos;
  41359. }
  41360. // Always default to polling on IBM i because fs.watch() is not available on IBM i.
  41361. if(isIBMi) {
  41362. opts.usePolling = true;
  41363. }
  41364. // Global override (useful for end-developers that need to force polling for all
  41365. // instances of chokidar, regardless of usage/dependency depth)
  41366. const envPoll = process.env.CHOKIDAR_USEPOLLING;
  41367. if (envPoll !== undefined) {
  41368. const envLower = envPoll.toLowerCase();
  41369. if (envLower === 'false' || envLower === '0') {
  41370. opts.usePolling = false;
  41371. } else if (envLower === 'true' || envLower === '1') {
  41372. opts.usePolling = true;
  41373. } else {
  41374. opts.usePolling = !!envLower;
  41375. }
  41376. }
  41377. const envInterval = process.env.CHOKIDAR_INTERVAL;
  41378. if (envInterval) {
  41379. opts.interval = Number.parseInt(envInterval, 10);
  41380. }
  41381. // Editor atomic write normalization enabled by default with fs.watch
  41382. if (undef(opts, 'atomic')) opts.atomic = !opts.usePolling && !opts.useFsEvents;
  41383. if (opts.atomic) this._pendingUnlinks = new Map();
  41384. if (undef(opts, 'followSymlinks')) opts.followSymlinks = true;
  41385. if (undef(opts, 'awaitWriteFinish')) opts.awaitWriteFinish = false;
  41386. if (opts.awaitWriteFinish === true) opts.awaitWriteFinish = {};
  41387. const awf = opts.awaitWriteFinish;
  41388. if (awf) {
  41389. if (!awf.stabilityThreshold) awf.stabilityThreshold = 2000;
  41390. if (!awf.pollInterval) awf.pollInterval = 100;
  41391. this._pendingWrites = new Map();
  41392. }
  41393. if (opts.ignored) opts.ignored = arrify(opts.ignored);
  41394. let readyCalls = 0;
  41395. this._emitReady = () => {
  41396. readyCalls++;
  41397. if (readyCalls >= this._readyCount) {
  41398. this._emitReady = EMPTY_FN;
  41399. this._readyEmitted = true;
  41400. // use process.nextTick to allow time for listener to be bound
  41401. process.nextTick(() => this.emit(EV_READY));
  41402. }
  41403. };
  41404. this._emitRaw = (...args) => this.emit(EV_RAW, ...args);
  41405. this._readyEmitted = false;
  41406. this.options = opts;
  41407. // Initialize with proper watcher.
  41408. if (opts.useFsEvents) {
  41409. this._fsEventsHandler = new FsEventsHandler(this);
  41410. } else {
  41411. this._nodeFsHandler = new NodeFsHandler(this);
  41412. }
  41413. // You’re frozen when your heart’s not open.
  41414. Object.freeze(opts);
  41415. }
  41416. // Public methods
  41417. /**
  41418. * Adds paths to be watched on an existing FSWatcher instance
  41419. * @param {Path|Array<Path>} paths_
  41420. * @param {String=} _origAdd private; for handling non-existent paths to be watched
  41421. * @param {Boolean=} _internal private; indicates a non-user add
  41422. * @returns {FSWatcher} for chaining
  41423. */
  41424. add(paths_, _origAdd, _internal) {
  41425. const {cwd, disableGlobbing} = this.options;
  41426. this.closed = false;
  41427. let paths = unifyPaths(paths_);
  41428. if (cwd) {
  41429. paths = paths.map((path) => {
  41430. const absPath = getAbsolutePath(path, cwd);
  41431. // Check `path` instead of `absPath` because the cwd portion can't be a glob
  41432. if (disableGlobbing || !isGlob(path)) {
  41433. return absPath;
  41434. }
  41435. return normalizePath(absPath);
  41436. });
  41437. }
  41438. // set aside negated glob strings
  41439. paths = paths.filter((path) => {
  41440. if (path.startsWith(BANG)) {
  41441. this._ignoredPaths.add(path.slice(1));
  41442. return false;
  41443. }
  41444. // if a path is being added that was previously ignored, stop ignoring it
  41445. this._ignoredPaths.delete(path);
  41446. this._ignoredPaths.delete(path + SLASH_GLOBSTAR);
  41447. // reset the cached userIgnored anymatch fn
  41448. // to make ignoredPaths changes effective
  41449. this._userIgnored = undefined;
  41450. return true;
  41451. });
  41452. if (this.options.useFsEvents && this._fsEventsHandler) {
  41453. if (!this._readyCount) this._readyCount = paths.length;
  41454. if (this.options.persistent) this._readyCount += paths.length;
  41455. paths.forEach((path) => this._fsEventsHandler._addToFsEvents(path));
  41456. } else {
  41457. if (!this._readyCount) this._readyCount = 0;
  41458. this._readyCount += paths.length;
  41459. Promise.all(
  41460. paths.map(async path => {
  41461. const res = await this._nodeFsHandler._addToNodeFs(path, !_internal, 0, 0, _origAdd);
  41462. if (res) this._emitReady();
  41463. return res;
  41464. })
  41465. ).then(results => {
  41466. if (this.closed) return;
  41467. results.filter(item => item).forEach(item => {
  41468. this.add(sysPath.dirname(item), sysPath.basename(_origAdd || item));
  41469. });
  41470. });
  41471. }
  41472. return this;
  41473. }
  41474. /**
  41475. * Close watchers or start ignoring events from specified paths.
  41476. * @param {Path|Array<Path>} paths_ - string or array of strings, file/directory paths and/or globs
  41477. * @returns {FSWatcher} for chaining
  41478. */
  41479. unwatch(paths_) {
  41480. if (this.closed) return this;
  41481. const paths = unifyPaths(paths_);
  41482. const {cwd} = this.options;
  41483. paths.forEach((path) => {
  41484. // convert to absolute path unless relative path already matches
  41485. if (!sysPath.isAbsolute(path) && !this._closers.has(path)) {
  41486. if (cwd) path = sysPath.join(cwd, path);
  41487. path = sysPath.resolve(path);
  41488. }
  41489. this._closePath(path);
  41490. this._ignoredPaths.add(path);
  41491. if (this._watched.has(path)) {
  41492. this._ignoredPaths.add(path + SLASH_GLOBSTAR);
  41493. }
  41494. // reset the cached userIgnored anymatch fn
  41495. // to make ignoredPaths changes effective
  41496. this._userIgnored = undefined;
  41497. });
  41498. return this;
  41499. }
  41500. /**
  41501. * Close watchers and remove all listeners from watched paths.
  41502. * @returns {Promise<void>}.
  41503. */
  41504. close() {
  41505. if (this.closed) return this._closePromise;
  41506. this.closed = true;
  41507. // Memory management.
  41508. this.removeAllListeners();
  41509. const closers = [];
  41510. this._closers.forEach(closerList => closerList.forEach(closer => {
  41511. const promise = closer();
  41512. if (promise instanceof Promise) closers.push(promise);
  41513. }));
  41514. this._streams.forEach(stream => stream.destroy());
  41515. this._userIgnored = undefined;
  41516. this._readyCount = 0;
  41517. this._readyEmitted = false;
  41518. this._watched.forEach(dirent => dirent.dispose());
  41519. ['closers', 'watched', 'streams', 'symlinkPaths', 'throttled'].forEach(key => {
  41520. this[`_${key}`].clear();
  41521. });
  41522. this._closePromise = closers.length ? Promise.all(closers).then(() => undefined) : Promise.resolve();
  41523. return this._closePromise;
  41524. }
  41525. /**
  41526. * Expose list of watched paths
  41527. * @returns {Object} for chaining
  41528. */
  41529. getWatched() {
  41530. const watchList = {};
  41531. this._watched.forEach((entry, dir) => {
  41532. const key = this.options.cwd ? sysPath.relative(this.options.cwd, dir) : dir;
  41533. watchList[key || ONE_DOT] = entry.getChildren().sort();
  41534. });
  41535. return watchList;
  41536. }
  41537. emitWithAll(event, args) {
  41538. this.emit(...args);
  41539. if (event !== EV_ERROR) this.emit(EV_ALL, ...args);
  41540. }
  41541. // Common helpers
  41542. // --------------
  41543. /**
  41544. * Normalize and emit events.
  41545. * Calling _emit DOES NOT MEAN emit() would be called!
  41546. * @param {EventName} event Type of event
  41547. * @param {Path} path File or directory path
  41548. * @param {*=} val1 arguments to be passed with event
  41549. * @param {*=} val2
  41550. * @param {*=} val3
  41551. * @returns the error if defined, otherwise the value of the FSWatcher instance's `closed` flag
  41552. */
  41553. async _emit(event, path, val1, val2, val3) {
  41554. if (this.closed) return;
  41555. const opts = this.options;
  41556. if (isWindows$1) path = sysPath.normalize(path);
  41557. if (opts.cwd) path = sysPath.relative(opts.cwd, path);
  41558. /** @type Array<any> */
  41559. const args = [event, path];
  41560. if (val3 !== undefined) args.push(val1, val2, val3);
  41561. else if (val2 !== undefined) args.push(val1, val2);
  41562. else if (val1 !== undefined) args.push(val1);
  41563. const awf = opts.awaitWriteFinish;
  41564. let pw;
  41565. if (awf && (pw = this._pendingWrites.get(path))) {
  41566. pw.lastChange = new Date();
  41567. return this;
  41568. }
  41569. if (opts.atomic) {
  41570. if (event === EV_UNLINK) {
  41571. this._pendingUnlinks.set(path, args);
  41572. setTimeout(() => {
  41573. this._pendingUnlinks.forEach((entry, path) => {
  41574. this.emit(...entry);
  41575. this.emit(EV_ALL, ...entry);
  41576. this._pendingUnlinks.delete(path);
  41577. });
  41578. }, typeof opts.atomic === 'number' ? opts.atomic : 100);
  41579. return this;
  41580. }
  41581. if (event === EV_ADD && this._pendingUnlinks.has(path)) {
  41582. event = args[0] = EV_CHANGE;
  41583. this._pendingUnlinks.delete(path);
  41584. }
  41585. }
  41586. if (awf && (event === EV_ADD || event === EV_CHANGE) && this._readyEmitted) {
  41587. const awfEmit = (err, stats) => {
  41588. if (err) {
  41589. event = args[0] = EV_ERROR;
  41590. args[1] = err;
  41591. this.emitWithAll(event, args);
  41592. } else if (stats) {
  41593. // if stats doesn't exist the file must have been deleted
  41594. if (args.length > 2) {
  41595. args[2] = stats;
  41596. } else {
  41597. args.push(stats);
  41598. }
  41599. this.emitWithAll(event, args);
  41600. }
  41601. };
  41602. this._awaitWriteFinish(path, awf.stabilityThreshold, event, awfEmit);
  41603. return this;
  41604. }
  41605. if (event === EV_CHANGE) {
  41606. const isThrottled = !this._throttle(EV_CHANGE, path, 50);
  41607. if (isThrottled) return this;
  41608. }
  41609. if (opts.alwaysStat && val1 === undefined &&
  41610. (event === EV_ADD || event === EV_ADD_DIR || event === EV_CHANGE)
  41611. ) {
  41612. const fullPath = opts.cwd ? sysPath.join(opts.cwd, path) : path;
  41613. let stats;
  41614. try {
  41615. stats = await stat(fullPath);
  41616. } catch (err) {}
  41617. // Suppress event when fs_stat fails, to avoid sending undefined 'stat'
  41618. if (!stats || this.closed) return;
  41619. args.push(stats);
  41620. }
  41621. this.emitWithAll(event, args);
  41622. return this;
  41623. }
  41624. /**
  41625. * Common handler for errors
  41626. * @param {Error} error
  41627. * @returns {Error|Boolean} The error if defined, otherwise the value of the FSWatcher instance's `closed` flag
  41628. */
  41629. _handleError(error) {
  41630. const code = error && error.code;
  41631. if (error && code !== 'ENOENT' && code !== 'ENOTDIR' &&
  41632. (!this.options.ignorePermissionErrors || (code !== 'EPERM' && code !== 'EACCES'))
  41633. ) {
  41634. this.emit(EV_ERROR, error);
  41635. }
  41636. return error || this.closed;
  41637. }
  41638. /**
  41639. * Helper utility for throttling
  41640. * @param {ThrottleType} actionType type being throttled
  41641. * @param {Path} path being acted upon
  41642. * @param {Number} timeout duration of time to suppress duplicate actions
  41643. * @returns {Object|false} tracking object or false if action should be suppressed
  41644. */
  41645. _throttle(actionType, path, timeout) {
  41646. if (!this._throttled.has(actionType)) {
  41647. this._throttled.set(actionType, new Map());
  41648. }
  41649. /** @type {Map<Path, Object>} */
  41650. const action = this._throttled.get(actionType);
  41651. /** @type {Object} */
  41652. const actionPath = action.get(path);
  41653. if (actionPath) {
  41654. actionPath.count++;
  41655. return false;
  41656. }
  41657. let timeoutObject;
  41658. const clear = () => {
  41659. const item = action.get(path);
  41660. const count = item ? item.count : 0;
  41661. action.delete(path);
  41662. clearTimeout(timeoutObject);
  41663. if (item) clearTimeout(item.timeoutObject);
  41664. return count;
  41665. };
  41666. timeoutObject = setTimeout(clear, timeout);
  41667. const thr = {timeoutObject, clear, count: 0};
  41668. action.set(path, thr);
  41669. return thr;
  41670. }
  41671. _incrReadyCount() {
  41672. return this._readyCount++;
  41673. }
  41674. /**
  41675. * Awaits write operation to finish.
  41676. * Polls a newly created file for size variations. When files size does not change for 'threshold' milliseconds calls callback.
  41677. * @param {Path} path being acted upon
  41678. * @param {Number} threshold Time in milliseconds a file size must be fixed before acknowledging write OP is finished
  41679. * @param {EventName} event
  41680. * @param {Function} awfEmit Callback to be called when ready for event to be emitted.
  41681. */
  41682. _awaitWriteFinish(path, threshold, event, awfEmit) {
  41683. let timeoutHandler;
  41684. let fullPath = path;
  41685. if (this.options.cwd && !sysPath.isAbsolute(path)) {
  41686. fullPath = sysPath.join(this.options.cwd, path);
  41687. }
  41688. const now = new Date();
  41689. const awaitWriteFinish = (prevStat) => {
  41690. fs$5.stat(fullPath, (err, curStat) => {
  41691. if (err || !this._pendingWrites.has(path)) {
  41692. if (err && err.code !== 'ENOENT') awfEmit(err);
  41693. return;
  41694. }
  41695. const now = Number(new Date());
  41696. if (prevStat && curStat.size !== prevStat.size) {
  41697. this._pendingWrites.get(path).lastChange = now;
  41698. }
  41699. const pw = this._pendingWrites.get(path);
  41700. const df = now - pw.lastChange;
  41701. if (df >= threshold) {
  41702. this._pendingWrites.delete(path);
  41703. awfEmit(undefined, curStat);
  41704. } else {
  41705. timeoutHandler = setTimeout(
  41706. awaitWriteFinish,
  41707. this.options.awaitWriteFinish.pollInterval,
  41708. curStat
  41709. );
  41710. }
  41711. });
  41712. };
  41713. if (!this._pendingWrites.has(path)) {
  41714. this._pendingWrites.set(path, {
  41715. lastChange: now,
  41716. cancelWait: () => {
  41717. this._pendingWrites.delete(path);
  41718. clearTimeout(timeoutHandler);
  41719. return event;
  41720. }
  41721. });
  41722. timeoutHandler = setTimeout(
  41723. awaitWriteFinish,
  41724. this.options.awaitWriteFinish.pollInterval
  41725. );
  41726. }
  41727. }
  41728. _getGlobIgnored() {
  41729. return [...this._ignoredPaths.values()];
  41730. }
  41731. /**
  41732. * Determines whether user has asked to ignore this path.
  41733. * @param {Path} path filepath or dir
  41734. * @param {fs.Stats=} stats result of fs.stat
  41735. * @returns {Boolean}
  41736. */
  41737. _isIgnored(path, stats) {
  41738. if (this.options.atomic && DOT_RE.test(path)) return true;
  41739. if (!this._userIgnored) {
  41740. const {cwd} = this.options;
  41741. const ign = this.options.ignored;
  41742. const ignored = ign && ign.map(normalizeIgnored(cwd));
  41743. const paths = arrify(ignored)
  41744. .filter((path) => typeof path === STRING_TYPE && !isGlob(path))
  41745. .map((path) => path + SLASH_GLOBSTAR);
  41746. const list = this._getGlobIgnored().map(normalizeIgnored(cwd)).concat(ignored, paths);
  41747. this._userIgnored = anymatch(list, undefined, ANYMATCH_OPTS);
  41748. }
  41749. return this._userIgnored([path, stats]);
  41750. }
  41751. _isntIgnored(path, stat) {
  41752. return !this._isIgnored(path, stat);
  41753. }
  41754. /**
  41755. * Provides a set of common helpers and properties relating to symlink and glob handling.
  41756. * @param {Path} path file, directory, or glob pattern being watched
  41757. * @param {Number=} depth at any depth > 0, this isn't a glob
  41758. * @returns {WatchHelper} object containing helpers for this path
  41759. */
  41760. _getWatchHelpers(path, depth) {
  41761. const watchPath = depth || this.options.disableGlobbing || !isGlob(path) ? path : globParent(path);
  41762. const follow = this.options.followSymlinks;
  41763. return new WatchHelper(path, watchPath, follow, this);
  41764. }
  41765. // Directory helpers
  41766. // -----------------
  41767. /**
  41768. * Provides directory tracking objects
  41769. * @param {String} directory path of the directory
  41770. * @returns {DirEntry} the directory's tracking object
  41771. */
  41772. _getWatchedDir(directory) {
  41773. if (!this._boundRemove) this._boundRemove = this._remove.bind(this);
  41774. const dir = sysPath.resolve(directory);
  41775. if (!this._watched.has(dir)) this._watched.set(dir, new DirEntry(dir, this._boundRemove));
  41776. return this._watched.get(dir);
  41777. }
  41778. // File helpers
  41779. // ------------
  41780. /**
  41781. * Check for read permissions.
  41782. * Based on this answer on SO: https://stackoverflow.com/a/11781404/1358405
  41783. * @param {fs.Stats} stats - object, result of fs_stat
  41784. * @returns {Boolean} indicates whether the file can be read
  41785. */
  41786. _hasReadPermissions(stats) {
  41787. if (this.options.ignorePermissionErrors) return true;
  41788. // stats.mode may be bigint
  41789. const md = stats && Number.parseInt(stats.mode, 10);
  41790. const st = md & 0o777;
  41791. const it = Number.parseInt(st.toString(8)[0], 10);
  41792. return Boolean(4 & it);
  41793. }
  41794. /**
  41795. * Handles emitting unlink events for
  41796. * files and directories, and via recursion, for
  41797. * files and directories within directories that are unlinked
  41798. * @param {String} directory within which the following item is located
  41799. * @param {String} item base path of item/directory
  41800. * @returns {void}
  41801. */
  41802. _remove(directory, item, isDirectory) {
  41803. // if what is being deleted is a directory, get that directory's paths
  41804. // for recursive deleting and cleaning of watched object
  41805. // if it is not a directory, nestedDirectoryChildren will be empty array
  41806. const path = sysPath.join(directory, item);
  41807. const fullPath = sysPath.resolve(path);
  41808. isDirectory = isDirectory != null
  41809. ? isDirectory
  41810. : this._watched.has(path) || this._watched.has(fullPath);
  41811. // prevent duplicate handling in case of arriving here nearly simultaneously
  41812. // via multiple paths (such as _handleFile and _handleDir)
  41813. if (!this._throttle('remove', path, 100)) return;
  41814. // if the only watched file is removed, watch for its return
  41815. if (!isDirectory && !this.options.useFsEvents && this._watched.size === 1) {
  41816. this.add(directory, item, true);
  41817. }
  41818. // This will create a new entry in the watched object in either case
  41819. // so we got to do the directory check beforehand
  41820. const wp = this._getWatchedDir(path);
  41821. const nestedDirectoryChildren = wp.getChildren();
  41822. // Recursively remove children directories / files.
  41823. nestedDirectoryChildren.forEach(nested => this._remove(path, nested));
  41824. // Check if item was on the watched list and remove it
  41825. const parent = this._getWatchedDir(directory);
  41826. const wasTracked = parent.has(item);
  41827. parent.remove(item);
  41828. // Fixes issue #1042 -> Relative paths were detected and added as symlinks
  41829. // (https://github.com/paulmillr/chokidar/blob/e1753ddbc9571bdc33b4a4af172d52cb6e611c10/lib/nodefs-handler.js#L612),
  41830. // but never removed from the map in case the path was deleted.
  41831. // This leads to an incorrect state if the path was recreated:
  41832. // https://github.com/paulmillr/chokidar/blob/e1753ddbc9571bdc33b4a4af172d52cb6e611c10/lib/nodefs-handler.js#L553
  41833. if (this._symlinkPaths.has(fullPath)) {
  41834. this._symlinkPaths.delete(fullPath);
  41835. }
  41836. // If we wait for this file to be fully written, cancel the wait.
  41837. let relPath = path;
  41838. if (this.options.cwd) relPath = sysPath.relative(this.options.cwd, path);
  41839. if (this.options.awaitWriteFinish && this._pendingWrites.has(relPath)) {
  41840. const event = this._pendingWrites.get(relPath).cancelWait();
  41841. if (event === EV_ADD) return;
  41842. }
  41843. // The Entry will either be a directory that just got removed
  41844. // or a bogus entry to a file, in either case we have to remove it
  41845. this._watched.delete(path);
  41846. this._watched.delete(fullPath);
  41847. const eventName = isDirectory ? EV_UNLINK_DIR : EV_UNLINK;
  41848. if (wasTracked && !this._isIgnored(path)) this._emit(eventName, path);
  41849. // Avoid conflicts if we later create another file with the same name
  41850. if (!this.options.useFsEvents) {
  41851. this._closePath(path);
  41852. }
  41853. }
  41854. /**
  41855. * Closes all watchers for a path
  41856. * @param {Path} path
  41857. */
  41858. _closePath(path) {
  41859. this._closeFile(path);
  41860. const dir = sysPath.dirname(path);
  41861. this._getWatchedDir(dir).remove(sysPath.basename(path));
  41862. }
  41863. /**
  41864. * Closes only file-specific watchers
  41865. * @param {Path} path
  41866. */
  41867. _closeFile(path) {
  41868. const closers = this._closers.get(path);
  41869. if (!closers) return;
  41870. closers.forEach(closer => closer());
  41871. this._closers.delete(path);
  41872. }
  41873. /**
  41874. *
  41875. * @param {Path} path
  41876. * @param {Function} closer
  41877. */
  41878. _addPathCloser(path, closer) {
  41879. if (!closer) return;
  41880. let list = this._closers.get(path);
  41881. if (!list) {
  41882. list = [];
  41883. this._closers.set(path, list);
  41884. }
  41885. list.push(closer);
  41886. }
  41887. _readdirp(root, opts) {
  41888. if (this.closed) return;
  41889. const options = {type: EV_ALL, alwaysStat: true, lstat: true, ...opts};
  41890. let stream = readdirp(root, options);
  41891. this._streams.add(stream);
  41892. stream.once(STR_CLOSE, () => {
  41893. stream = undefined;
  41894. });
  41895. stream.once(STR_END, () => {
  41896. if (stream) {
  41897. this._streams.delete(stream);
  41898. stream = undefined;
  41899. }
  41900. });
  41901. return stream;
  41902. }
  41903. }
  41904. // Export FSWatcher class
  41905. chokidar.FSWatcher = FSWatcher;
  41906. /**
  41907. * Instantiates watcher with paths to be tracked.
  41908. * @param {String|Array<String>} paths file/directory paths and/or globs
  41909. * @param {Object=} options chokidar opts
  41910. * @returns an instance of FSWatcher for chaining.
  41911. */
  41912. const watch = (paths, options) => {
  41913. const watcher = new FSWatcher(options);
  41914. watcher.add(paths);
  41915. return watcher;
  41916. };
  41917. chokidar.watch = watch;
  41918. var shellQuote$1 = {};
  41919. var quote = function quote(xs) {
  41920. return xs.map(function (s) {
  41921. if (s && typeof s === 'object') {
  41922. return s.op.replace(/(.)/g, '\\$1');
  41923. }
  41924. if ((/["\s]/).test(s) && !(/'/).test(s)) {
  41925. return "'" + s.replace(/(['\\])/g, '\\$1') + "'";
  41926. }
  41927. if ((/["'\s]/).test(s)) {
  41928. return '"' + s.replace(/(["\\$`!])/g, '\\$1') + '"';
  41929. }
  41930. return String(s).replace(/([A-Za-z]:)?([#!"$&'()*,:;<=>?@[\\\]^`{|}])/g, '$1\\$2');
  41931. }).join(' ');
  41932. };
  41933. // '<(' is process substitution operator and
  41934. // can be parsed the same as control operator
  41935. var CONTROL = '(?:' + [
  41936. '\\|\\|',
  41937. '\\&\\&',
  41938. ';;',
  41939. '\\|\\&',
  41940. '\\<\\(',
  41941. '\\<\\<\\<',
  41942. '>>',
  41943. '>\\&',
  41944. '<\\&',
  41945. '[&;()|<>]'
  41946. ].join('|') + ')';
  41947. var controlRE = new RegExp('^' + CONTROL + '$');
  41948. var META = '|&;()<> \\t';
  41949. var SINGLE_QUOTE = '"((\\\\"|[^"])*?)"';
  41950. var DOUBLE_QUOTE = '\'((\\\\\'|[^\'])*?)\'';
  41951. var hash = /^#$/;
  41952. var SQ = "'";
  41953. var DQ = '"';
  41954. var DS = '$';
  41955. var TOKEN = '';
  41956. var mult = 0x100000000; // Math.pow(16, 8);
  41957. for (var i = 0; i < 4; i++) {
  41958. TOKEN += (mult * Math.random()).toString(16);
  41959. }
  41960. var startsWithToken = new RegExp('^' + TOKEN);
  41961. function matchAll(s, r) {
  41962. var origIndex = r.lastIndex;
  41963. var matches = [];
  41964. var matchObj;
  41965. while ((matchObj = r.exec(s))) {
  41966. matches.push(matchObj);
  41967. if (r.lastIndex === matchObj.index) {
  41968. r.lastIndex += 1;
  41969. }
  41970. }
  41971. r.lastIndex = origIndex;
  41972. return matches;
  41973. }
  41974. function getVar(env, pre, key) {
  41975. var r = typeof env === 'function' ? env(key) : env[key];
  41976. if (typeof r === 'undefined' && key != '') {
  41977. r = '';
  41978. } else if (typeof r === 'undefined') {
  41979. r = '$';
  41980. }
  41981. if (typeof r === 'object') {
  41982. return pre + TOKEN + JSON.stringify(r) + TOKEN;
  41983. }
  41984. return pre + r;
  41985. }
  41986. function parseInternal(string, env, opts) {
  41987. if (!opts) {
  41988. opts = {};
  41989. }
  41990. var BS = opts.escape || '\\';
  41991. var BAREWORD = '(\\' + BS + '[\'"' + META + ']|[^\\s\'"' + META + '])+';
  41992. var chunker = new RegExp([
  41993. '(' + CONTROL + ')', // control chars
  41994. '(' + BAREWORD + '|' + SINGLE_QUOTE + '|' + DOUBLE_QUOTE + ')+'
  41995. ].join('|'), 'g');
  41996. var matches = matchAll(string, chunker);
  41997. if (matches.length === 0) {
  41998. return [];
  41999. }
  42000. if (!env) {
  42001. env = {};
  42002. }
  42003. var commented = false;
  42004. return matches.map(function (match) {
  42005. var s = match[0];
  42006. if (!s || commented) {
  42007. return void undefined;
  42008. }
  42009. if (controlRE.test(s)) {
  42010. return { op: s };
  42011. }
  42012. // Hand-written scanner/parser for Bash quoting rules:
  42013. //
  42014. // 1. inside single quotes, all characters are printed literally.
  42015. // 2. inside double quotes, all characters are printed literally
  42016. // except variables prefixed by '$' and backslashes followed by
  42017. // either a double quote or another backslash.
  42018. // 3. outside of any quotes, backslashes are treated as escape
  42019. // characters and not printed (unless they are themselves escaped)
  42020. // 4. quote context can switch mid-token if there is no whitespace
  42021. // between the two quote contexts (e.g. all'one'"token" parses as
  42022. // "allonetoken")
  42023. var quote = false;
  42024. var esc = false;
  42025. var out = '';
  42026. var isGlob = false;
  42027. var i;
  42028. function parseEnvVar() {
  42029. i += 1;
  42030. var varend;
  42031. var varname;
  42032. var char = s.charAt(i);
  42033. if (char === '{') {
  42034. i += 1;
  42035. if (s.charAt(i) === '}') {
  42036. throw new Error('Bad substitution: ' + s.slice(i - 2, i + 1));
  42037. }
  42038. varend = s.indexOf('}', i);
  42039. if (varend < 0) {
  42040. throw new Error('Bad substitution: ' + s.slice(i));
  42041. }
  42042. varname = s.slice(i, varend);
  42043. i = varend;
  42044. } else if ((/[*@#?$!_-]/).test(char)) {
  42045. varname = char;
  42046. i += 1;
  42047. } else {
  42048. var slicedFromI = s.slice(i);
  42049. varend = slicedFromI.match(/[^\w\d_]/);
  42050. if (!varend) {
  42051. varname = slicedFromI;
  42052. i = s.length;
  42053. } else {
  42054. varname = slicedFromI.slice(0, varend.index);
  42055. i += varend.index - 1;
  42056. }
  42057. }
  42058. return getVar(env, '', varname);
  42059. }
  42060. for (i = 0; i < s.length; i++) {
  42061. var c = s.charAt(i);
  42062. isGlob = isGlob || (!quote && (c === '*' || c === '?'));
  42063. if (esc) {
  42064. out += c;
  42065. esc = false;
  42066. } else if (quote) {
  42067. if (c === quote) {
  42068. quote = false;
  42069. } else if (quote == SQ) {
  42070. out += c;
  42071. } else { // Double quote
  42072. if (c === BS) {
  42073. i += 1;
  42074. c = s.charAt(i);
  42075. if (c === DQ || c === BS || c === DS) {
  42076. out += c;
  42077. } else {
  42078. out += BS + c;
  42079. }
  42080. } else if (c === DS) {
  42081. out += parseEnvVar();
  42082. } else {
  42083. out += c;
  42084. }
  42085. }
  42086. } else if (c === DQ || c === SQ) {
  42087. quote = c;
  42088. } else if (controlRE.test(c)) {
  42089. return { op: s };
  42090. } else if (hash.test(c)) {
  42091. commented = true;
  42092. var commentObj = { comment: string.slice(match.index + i + 1) };
  42093. if (out.length) {
  42094. return [out, commentObj];
  42095. }
  42096. return [commentObj];
  42097. } else if (c === BS) {
  42098. esc = true;
  42099. } else if (c === DS) {
  42100. out += parseEnvVar();
  42101. } else {
  42102. out += c;
  42103. }
  42104. }
  42105. if (isGlob) {
  42106. return { op: 'glob', pattern: out };
  42107. }
  42108. return out;
  42109. }).reduce(function (prev, arg) { // finalize parsed arguments
  42110. // TODO: replace this whole reduce with a concat
  42111. return typeof arg === 'undefined' ? prev : prev.concat(arg);
  42112. }, []);
  42113. }
  42114. var parse$6 = function parse(s, env, opts) {
  42115. var mapped = parseInternal(s, env, opts);
  42116. if (typeof env !== 'function') {
  42117. return mapped;
  42118. }
  42119. return mapped.reduce(function (acc, s) {
  42120. if (typeof s === 'object') {
  42121. return acc.concat(s);
  42122. }
  42123. var xs = s.split(RegExp('(' + TOKEN + '.*?' + TOKEN + ')', 'g'));
  42124. if (xs.length === 1) {
  42125. return acc.concat(xs[0]);
  42126. }
  42127. return acc.concat(xs.filter(Boolean).map(function (x) {
  42128. if (startsWithToken.test(x)) {
  42129. return JSON.parse(x.split(TOKEN)[1]);
  42130. }
  42131. return x;
  42132. }));
  42133. }, []);
  42134. };
  42135. shellQuote$1.quote = quote;
  42136. shellQuote$1.parse = parse$6;
  42137. var macos = {
  42138. '/Applications/Atom.app/Contents/MacOS/Atom': 'atom',
  42139. '/Applications/Atom Beta.app/Contents/MacOS/Atom Beta':
  42140. '/Applications/Atom Beta.app/Contents/MacOS/Atom Beta',
  42141. '/Applications/Brackets.app/Contents/MacOS/Brackets': 'brackets',
  42142. '/Applications/Sublime Text.app/Contents/MacOS/Sublime Text':
  42143. '/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl',
  42144. '/Applications/Sublime Text.app/Contents/MacOS/sublime_text':
  42145. '/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl',
  42146. '/Applications/Sublime Text 2.app/Contents/MacOS/Sublime Text 2':
  42147. '/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl',
  42148. '/Applications/Sublime Text Dev.app/Contents/MacOS/Sublime Text':
  42149. '/Applications/Sublime Text Dev.app/Contents/SharedSupport/bin/subl',
  42150. '/Applications/Visual Studio Code.app/Contents/MacOS/Electron': 'code',
  42151. '/Applications/Visual Studio Code - Insiders.app/Contents/MacOS/Electron':
  42152. 'code-insiders',
  42153. '/Applications/VSCodium.app/Contents/MacOS/Electron': 'codium',
  42154. '/Applications/Cursor.app/Contents/MacOS/Cursor': 'cursor',
  42155. '/Applications/AppCode.app/Contents/MacOS/appcode':
  42156. '/Applications/AppCode.app/Contents/MacOS/appcode',
  42157. '/Applications/CLion.app/Contents/MacOS/clion':
  42158. '/Applications/CLion.app/Contents/MacOS/clion',
  42159. '/Applications/IntelliJ IDEA.app/Contents/MacOS/idea':
  42160. '/Applications/IntelliJ IDEA.app/Contents/MacOS/idea',
  42161. '/Applications/IntelliJ IDEA Ultimate.app/Contents/MacOS/idea':
  42162. '/Applications/IntelliJ IDEA Ultimate.app/Contents/MacOS/idea',
  42163. '/Applications/IntelliJ IDEA Community Edition.app/Contents/MacOS/idea':
  42164. '/Applications/IntelliJ IDEA Community Edition.app/Contents/MacOS/idea',
  42165. '/Applications/PhpStorm.app/Contents/MacOS/phpstorm':
  42166. '/Applications/PhpStorm.app/Contents/MacOS/phpstorm',
  42167. '/Applications/PyCharm.app/Contents/MacOS/pycharm':
  42168. '/Applications/PyCharm.app/Contents/MacOS/pycharm',
  42169. '/Applications/PyCharm CE.app/Contents/MacOS/pycharm':
  42170. '/Applications/PyCharm CE.app/Contents/MacOS/pycharm',
  42171. '/Applications/RubyMine.app/Contents/MacOS/rubymine':
  42172. '/Applications/RubyMine.app/Contents/MacOS/rubymine',
  42173. '/Applications/WebStorm.app/Contents/MacOS/webstorm':
  42174. '/Applications/WebStorm.app/Contents/MacOS/webstorm',
  42175. '/Applications/MacVim.app/Contents/MacOS/MacVim': 'mvim',
  42176. '/Applications/GoLand.app/Contents/MacOS/goland':
  42177. '/Applications/GoLand.app/Contents/MacOS/goland',
  42178. '/Applications/Rider.app/Contents/MacOS/rider':
  42179. '/Applications/Rider.app/Contents/MacOS/rider',
  42180. '/Applications/Zed.app/Contents/MacOS/zed': 'zed'
  42181. };
  42182. var linux = {
  42183. atom: 'atom',
  42184. Brackets: 'brackets',
  42185. 'code-insiders': 'code-insiders',
  42186. code: 'code',
  42187. vscodium: 'vscodium',
  42188. codium: 'codium',
  42189. emacs: 'emacs',
  42190. gvim: 'gvim',
  42191. idea: 'idea',
  42192. 'idea.sh': 'idea',
  42193. phpstorm: 'phpstorm',
  42194. 'phpstorm.sh': 'phpstorm',
  42195. pycharm: 'pycharm',
  42196. 'pycharm.sh': 'pycharm',
  42197. rubymine: 'rubymine',
  42198. 'rubymine.sh': 'rubymine',
  42199. sublime_text: 'subl',
  42200. vim: 'vim',
  42201. webstorm: 'webstorm',
  42202. 'webstorm.sh': 'webstorm',
  42203. goland: 'goland',
  42204. 'goland.sh': 'goland',
  42205. rider: 'rider',
  42206. 'rider.sh': 'rider'
  42207. };
  42208. var windows$1 = [
  42209. 'Brackets.exe',
  42210. 'Code.exe',
  42211. 'Code - Insiders.exe',
  42212. 'VSCodium.exe',
  42213. 'atom.exe',
  42214. 'sublime_text.exe',
  42215. 'notepad++.exe',
  42216. 'clion.exe',
  42217. 'clion64.exe',
  42218. 'idea.exe',
  42219. 'idea64.exe',
  42220. 'phpstorm.exe',
  42221. 'phpstorm64.exe',
  42222. 'pycharm.exe',
  42223. 'pycharm64.exe',
  42224. 'rubymine.exe',
  42225. 'rubymine64.exe',
  42226. 'webstorm.exe',
  42227. 'webstorm64.exe',
  42228. 'goland.exe',
  42229. 'goland64.exe',
  42230. 'rider.exe',
  42231. 'rider64.exe'
  42232. ];
  42233. const path$7 = require$$0$4;
  42234. const shellQuote = shellQuote$1;
  42235. const childProcess$2 = require$$2$1;
  42236. // Map from full process name to binary that starts the process
  42237. // We can't just re-use full process name, because it will spawn a new instance
  42238. // of the app every time
  42239. const COMMON_EDITORS_MACOS = macos;
  42240. const COMMON_EDITORS_LINUX = linux;
  42241. const COMMON_EDITORS_WIN = windows$1;
  42242. var guess = function guessEditor (specifiedEditor) {
  42243. if (specifiedEditor) {
  42244. return shellQuote.parse(specifiedEditor)
  42245. }
  42246. if (process.env.LAUNCH_EDITOR) {
  42247. return [process.env.LAUNCH_EDITOR]
  42248. }
  42249. if (process.versions.webcontainer) {
  42250. return [process.env.EDITOR || 'code']
  42251. }
  42252. // We can find out which editor is currently running by:
  42253. // `ps x` on macOS and Linux
  42254. // `Get-Process` on Windows
  42255. try {
  42256. if (process.platform === 'darwin') {
  42257. const output = childProcess$2
  42258. .execSync('ps x -o comm=', {
  42259. stdio: ['pipe', 'pipe', 'ignore']
  42260. })
  42261. .toString();
  42262. const processNames = Object.keys(COMMON_EDITORS_MACOS);
  42263. const processList = output.split('\n');
  42264. for (let i = 0; i < processNames.length; i++) {
  42265. const processName = processNames[i];
  42266. // Find editor by exact match.
  42267. if (processList.includes(processName)) {
  42268. return [COMMON_EDITORS_MACOS[processName]]
  42269. }
  42270. const processNameWithoutApplications = processName.replace('/Applications', '');
  42271. // Find editor installation not in /Applications.
  42272. if (output.indexOf(processNameWithoutApplications) !== -1) {
  42273. // Use the CLI command if one is specified
  42274. if (processName !== COMMON_EDITORS_MACOS[processName]) {
  42275. return [COMMON_EDITORS_MACOS[processName]]
  42276. }
  42277. // Use a partial match to find the running process path. If one is found, use the
  42278. // existing path since it can be running from anywhere.
  42279. const runningProcess = processList.find((procName) => procName.endsWith(processNameWithoutApplications));
  42280. if (runningProcess !== undefined) {
  42281. return [runningProcess]
  42282. }
  42283. }
  42284. }
  42285. } else if (process.platform === 'win32') {
  42286. const output = childProcess$2
  42287. .execSync(
  42288. 'powershell -NoProfile -Command "Get-CimInstance -Query \\"select executablepath from win32_process where executablepath is not null\\" | % { $_.ExecutablePath }"',
  42289. {
  42290. stdio: ['pipe', 'pipe', 'ignore']
  42291. }
  42292. )
  42293. .toString();
  42294. const runningProcesses = output.split('\r\n');
  42295. for (let i = 0; i < runningProcesses.length; i++) {
  42296. const fullProcessPath = runningProcesses[i].trim();
  42297. const shortProcessName = path$7.basename(fullProcessPath);
  42298. if (COMMON_EDITORS_WIN.indexOf(shortProcessName) !== -1) {
  42299. return [fullProcessPath]
  42300. }
  42301. }
  42302. } else if (process.platform === 'linux') {
  42303. // --no-heading No header line
  42304. // x List all processes owned by you
  42305. // -o comm Need only names column
  42306. const output = childProcess$2
  42307. .execSync('ps x --no-heading -o comm --sort=comm', {
  42308. stdio: ['pipe', 'pipe', 'ignore']
  42309. })
  42310. .toString();
  42311. const processNames = Object.keys(COMMON_EDITORS_LINUX);
  42312. for (let i = 0; i < processNames.length; i++) {
  42313. const processName = processNames[i];
  42314. if (output.indexOf(processName) !== -1) {
  42315. return [COMMON_EDITORS_LINUX[processName]]
  42316. }
  42317. }
  42318. }
  42319. } catch (ignoreError) {
  42320. // Ignore...
  42321. }
  42322. // Last resort, use old skool env vars
  42323. if (process.env.VISUAL) {
  42324. return [process.env.VISUAL]
  42325. } else if (process.env.EDITOR) {
  42326. return [process.env.EDITOR]
  42327. }
  42328. return [null]
  42329. };
  42330. const path$6 = require$$0$4;
  42331. // normalize file/line numbers into command line args for specific editors
  42332. var getArgs = function getArgumentsForPosition (
  42333. editor,
  42334. fileName,
  42335. lineNumber,
  42336. columnNumber = 1
  42337. ) {
  42338. const editorBasename = path$6.basename(editor).replace(/\.(exe|cmd|bat)$/i, '');
  42339. switch (editorBasename) {
  42340. case 'atom':
  42341. case 'Atom':
  42342. case 'Atom Beta':
  42343. case 'subl':
  42344. case 'sublime':
  42345. case 'sublime_text':
  42346. case 'wstorm':
  42347. case 'charm':
  42348. case 'zed':
  42349. return [`${fileName}:${lineNumber}:${columnNumber}`]
  42350. case 'notepad++':
  42351. return ['-n' + lineNumber, '-c' + columnNumber, fileName]
  42352. case 'vim':
  42353. case 'mvim':
  42354. return [`+call cursor(${lineNumber}, ${columnNumber})`, fileName]
  42355. case 'joe':
  42356. case 'gvim':
  42357. return ['+' + `${lineNumber}`, fileName]
  42358. case 'emacs':
  42359. case 'emacsclient':
  42360. return [`+${lineNumber}:${columnNumber}`, fileName]
  42361. case 'rmate':
  42362. case 'mate':
  42363. case 'mine':
  42364. return ['--line', lineNumber, fileName]
  42365. case 'code':
  42366. case 'Code':
  42367. case 'code-insiders':
  42368. case 'Code - Insiders':
  42369. case 'codium':
  42370. case 'cursor':
  42371. case 'vscodium':
  42372. case 'VSCodium':
  42373. return ['-r', '-g', `${fileName}:${lineNumber}:${columnNumber}`]
  42374. case 'appcode':
  42375. case 'clion':
  42376. case 'clion64':
  42377. case 'idea':
  42378. case 'idea64':
  42379. case 'phpstorm':
  42380. case 'phpstorm64':
  42381. case 'pycharm':
  42382. case 'pycharm64':
  42383. case 'rubymine':
  42384. case 'rubymine64':
  42385. case 'webstorm':
  42386. case 'webstorm64':
  42387. case 'goland':
  42388. case 'goland64':
  42389. case 'rider':
  42390. case 'rider64':
  42391. return ['--line', lineNumber, '--column', columnNumber, fileName]
  42392. }
  42393. if (process.env.LAUNCH_EDITOR) {
  42394. return [fileName, lineNumber, columnNumber]
  42395. }
  42396. // For all others, drop the lineNumber until we have
  42397. // a mapping above, since providing the lineNumber incorrectly
  42398. // can result in errors or confusing behavior.
  42399. return [fileName]
  42400. };
  42401. /**
  42402. * Copyright (c) 2015-present, Facebook, Inc.
  42403. *
  42404. * This source code is licensed under the MIT license found in the
  42405. * LICENSE file at
  42406. * https://github.com/facebookincubator/create-react-app/blob/master/LICENSE
  42407. *
  42408. * Modified by Yuxi Evan You
  42409. */
  42410. const fs$4 = require$$0__default;
  42411. const os$1 = require$$2;
  42412. const path$5 = require$$0$4;
  42413. const colors = picocolorsExports;
  42414. const childProcess$1 = require$$2$1;
  42415. const guessEditor = guess;
  42416. const getArgumentsForPosition = getArgs;
  42417. function wrapErrorCallback (cb) {
  42418. return (fileName, errorMessage) => {
  42419. console.log();
  42420. console.log(
  42421. colors.red('Could not open ' + path$5.basename(fileName) + ' in the editor.')
  42422. );
  42423. if (errorMessage) {
  42424. if (errorMessage[errorMessage.length - 1] !== '.') {
  42425. errorMessage += '.';
  42426. }
  42427. console.log(
  42428. colors.red('The editor process exited with an error: ' + errorMessage)
  42429. );
  42430. }
  42431. console.log();
  42432. if (cb) cb(fileName, errorMessage);
  42433. }
  42434. }
  42435. function isTerminalEditor (editor) {
  42436. switch (editor) {
  42437. case 'vim':
  42438. case 'emacs':
  42439. case 'nano':
  42440. return true
  42441. }
  42442. return false
  42443. }
  42444. const positionRE = /:(\d+)(:(\d+))?$/;
  42445. function parseFile (file) {
  42446. const fileName = file.replace(positionRE, '');
  42447. const match = file.match(positionRE);
  42448. const lineNumber = match && match[1];
  42449. const columnNumber = match && match[3];
  42450. return {
  42451. fileName,
  42452. lineNumber,
  42453. columnNumber
  42454. }
  42455. }
  42456. let _childProcess = null;
  42457. function launchEditor (file, specifiedEditor, onErrorCallback) {
  42458. const parsed = parseFile(file);
  42459. let { fileName } = parsed;
  42460. const { lineNumber, columnNumber } = parsed;
  42461. if (!fs$4.existsSync(fileName)) {
  42462. return
  42463. }
  42464. if (typeof specifiedEditor === 'function') {
  42465. onErrorCallback = specifiedEditor;
  42466. specifiedEditor = undefined;
  42467. }
  42468. onErrorCallback = wrapErrorCallback(onErrorCallback);
  42469. const [editor, ...args] = guessEditor(specifiedEditor);
  42470. if (!editor) {
  42471. onErrorCallback(fileName, null);
  42472. return
  42473. }
  42474. if (
  42475. process.platform === 'linux' &&
  42476. fileName.startsWith('/mnt/') &&
  42477. /Microsoft/i.test(os$1.release())
  42478. ) {
  42479. // Assume WSL / "Bash on Ubuntu on Windows" is being used, and
  42480. // that the file exists on the Windows file system.
  42481. // `os.release()` is "4.4.0-43-Microsoft" in the current release
  42482. // build of WSL, see: https://github.com/Microsoft/BashOnWindows/issues/423#issuecomment-221627364
  42483. // When a Windows editor is specified, interop functionality can
  42484. // handle the path translation, but only if a relative path is used.
  42485. fileName = path$5.relative('', fileName);
  42486. }
  42487. if (lineNumber) {
  42488. const extraArgs = getArgumentsForPosition(editor, fileName, lineNumber, columnNumber);
  42489. args.push.apply(args, extraArgs);
  42490. } else {
  42491. args.push(fileName);
  42492. }
  42493. if (_childProcess && isTerminalEditor(editor)) {
  42494. // There's an existing editor process already and it's attached
  42495. // to the terminal, so go kill it. Otherwise two separate editor
  42496. // instances attach to the stdin/stdout which gets confusing.
  42497. _childProcess.kill('SIGKILL');
  42498. }
  42499. if (process.platform === 'win32') {
  42500. // On Windows, we need to use `exec` with the `shell: true` option,
  42501. // and some more sanitization is required.
  42502. // However, CMD.exe on Windows is vulnerable to RCE attacks given a file name of the
  42503. // form "C:\Users\myusername\Downloads\& curl 172.21.93.52".
  42504. // `create-react-app` used a safe file name pattern to validate user-provided file names:
  42505. // - https://github.com/facebook/create-react-app/pull/4866
  42506. // - https://github.com/facebook/create-react-app/pull/5431
  42507. // But that's not a viable solution for this package because
  42508. // it's depended on by so many meta frameworks that heavily rely on
  42509. // special characters in file names for filesystem-based routing.
  42510. // We need to at least:
  42511. // - Support `+` because it's used in SvelteKit and Vike
  42512. // - Support `$` because it's used in Remix
  42513. // - Support `(` and `)` because they are used in Analog, SolidStart, and Vike
  42514. // - Support `@` because it's used in Vike
  42515. // - Support `[` and `]` because they are widely used for [slug]
  42516. // So here we choose to use `^` to escape special characters instead.
  42517. // According to https://ss64.com/nt/syntax-esc.html,
  42518. // we can use `^` to escape `&`, `<`, `>`, `|`, `%`, and `^`
  42519. // I'm not sure if we have to escape all of these, but let's do it anyway
  42520. function escapeCmdArgs (cmdArgs) {
  42521. return cmdArgs.replace(/([&|<>,;=^])/g, '^$1')
  42522. }
  42523. // Need to double quote the editor path in case it contains spaces;
  42524. // If the fileName contains spaces, we also need to double quote it in the arguments
  42525. // However, there's a case that it's concatenated with line number and column number
  42526. // which is separated by `:`. We need to double quote the whole string in this case.
  42527. // Also, if the string contains the escape character `^`, it needs to be quoted, too.
  42528. function doubleQuoteIfNeeded(str) {
  42529. if (str.includes('^')) {
  42530. // If a string includes an escaped character, not only does it need to be quoted,
  42531. // but the quotes need to be escaped too.
  42532. return `^"${str}^"`
  42533. } else if (str.includes(' ')) {
  42534. return `"${str}"`
  42535. }
  42536. return str
  42537. }
  42538. const launchCommand = [editor, ...args.map(escapeCmdArgs)]
  42539. .map(doubleQuoteIfNeeded)
  42540. .join(' ');
  42541. _childProcess = childProcess$1.exec(launchCommand, {
  42542. stdio: 'inherit',
  42543. shell: true
  42544. });
  42545. } else {
  42546. _childProcess = childProcess$1.spawn(editor, args, { stdio: 'inherit' });
  42547. }
  42548. _childProcess.on('exit', function (errorCode) {
  42549. _childProcess = null;
  42550. if (errorCode) {
  42551. onErrorCallback(fileName, '(code ' + errorCode + ')');
  42552. }
  42553. });
  42554. _childProcess.on('error', function (error) {
  42555. let { code, message } = error;
  42556. if ('ENOENT' === code) {
  42557. message = `${message} ('${editor}' command does not exist in 'PATH')`;
  42558. }
  42559. onErrorCallback(fileName, message);
  42560. });
  42561. }
  42562. var launchEditor_1 = launchEditor;
  42563. const url$2 = require$$0$9;
  42564. const path$4 = require$$0$4;
  42565. const launch = launchEditor_1;
  42566. var launchEditorMiddleware = (specifiedEditor, srcRoot, onErrorCallback) => {
  42567. if (typeof specifiedEditor === 'function') {
  42568. onErrorCallback = specifiedEditor;
  42569. specifiedEditor = undefined;
  42570. }
  42571. if (typeof srcRoot === 'function') {
  42572. onErrorCallback = srcRoot;
  42573. srcRoot = undefined;
  42574. }
  42575. srcRoot = srcRoot || process.cwd();
  42576. return function launchEditorMiddleware (req, res) {
  42577. const { file } = url$2.parse(req.url, true).query || {};
  42578. if (!file) {
  42579. res.statusCode = 500;
  42580. res.end(`launch-editor-middleware: required query param "file" is missing.`);
  42581. } else {
  42582. launch(path$4.resolve(srcRoot, file), specifiedEditor, onErrorCallback);
  42583. res.end();
  42584. }
  42585. }
  42586. };
  42587. var launchEditorMiddleware$1 = /*@__PURE__*/getDefaultExportFromCjs(launchEditorMiddleware);
  42588. async function resolveHttpServer({ proxy }, app, httpsOptions) {
  42589. if (!httpsOptions) {
  42590. const { createServer } = await import('node:http');
  42591. return createServer(app);
  42592. }
  42593. if (proxy) {
  42594. const { createServer } = await import('node:https');
  42595. return createServer(httpsOptions, app);
  42596. } else {
  42597. const { createSecureServer } = await import('node:http2');
  42598. return createSecureServer(
  42599. {
  42600. // Manually increase the session memory to prevent 502 ENHANCE_YOUR_CALM
  42601. // errors on large numbers of requests
  42602. maxSessionMemory: 1e3,
  42603. ...httpsOptions,
  42604. allowHTTP1: true
  42605. },
  42606. // @ts-expect-error TODO: is this correct?
  42607. app
  42608. );
  42609. }
  42610. }
  42611. async function resolveHttpsConfig(https) {
  42612. if (!https) return void 0;
  42613. const [ca, cert, key, pfx] = await Promise.all([
  42614. readFileIfExists(https.ca),
  42615. readFileIfExists(https.cert),
  42616. readFileIfExists(https.key),
  42617. readFileIfExists(https.pfx)
  42618. ]);
  42619. return { ...https, ca, cert, key, pfx };
  42620. }
  42621. async function readFileIfExists(value) {
  42622. if (typeof value === "string") {
  42623. return fsp.readFile(path$n.resolve(value)).catch(() => value);
  42624. }
  42625. return value;
  42626. }
  42627. async function httpServerStart(httpServer, serverOptions) {
  42628. let { port, strictPort, host, logger } = serverOptions;
  42629. return new Promise((resolve, reject) => {
  42630. const onError = (e) => {
  42631. if (e.code === "EADDRINUSE") {
  42632. if (strictPort) {
  42633. httpServer.removeListener("error", onError);
  42634. reject(new Error(`Port ${port} is already in use`));
  42635. } else {
  42636. logger.info(`Port ${port} is in use, trying another one...`);
  42637. httpServer.listen(++port, host);
  42638. }
  42639. } else {
  42640. httpServer.removeListener("error", onError);
  42641. reject(e);
  42642. }
  42643. };
  42644. httpServer.on("error", onError);
  42645. httpServer.listen(port, host, () => {
  42646. httpServer.removeListener("error", onError);
  42647. resolve(port);
  42648. });
  42649. });
  42650. }
  42651. function setClientErrorHandler(server, logger) {
  42652. server.on("clientError", (err, socket) => {
  42653. let msg = "400 Bad Request";
  42654. if (err.code === "HPE_HEADER_OVERFLOW") {
  42655. msg = "431 Request Header Fields Too Large";
  42656. logger.warn(
  42657. colors$1.yellow(
  42658. "Server responded with status code 431. See https://vite.dev/guide/troubleshooting.html#_431-request-header-fields-too-large."
  42659. )
  42660. );
  42661. }
  42662. if (err.code === "ECONNRESET" || !socket.writable) {
  42663. return;
  42664. }
  42665. socket.end(`HTTP/1.1 ${msg}\r
  42666. \r
  42667. `);
  42668. });
  42669. }
  42670. const commonFsUtils = {
  42671. existsSync: fs__default.existsSync,
  42672. isDirectory,
  42673. tryResolveRealFile,
  42674. tryResolveRealFileWithExtensions,
  42675. tryResolveRealFileOrType
  42676. };
  42677. const cachedFsUtilsMap = /* @__PURE__ */ new WeakMap();
  42678. function getFsUtils(config) {
  42679. let fsUtils = cachedFsUtilsMap.get(config);
  42680. if (!fsUtils) {
  42681. if (config.command !== "serve" || config.server.fs.cachedChecks !== true || config.server.watch?.ignored || process.versions.pnp) {
  42682. fsUtils = commonFsUtils;
  42683. } else if (!config.resolve.preserveSymlinks && config.root !== getRealPath(config.root)) {
  42684. fsUtils = commonFsUtils;
  42685. } else {
  42686. fsUtils = createCachedFsUtils(config);
  42687. }
  42688. cachedFsUtilsMap.set(config, fsUtils);
  42689. }
  42690. return fsUtils;
  42691. }
  42692. function readDirCacheSync(file) {
  42693. let dirents;
  42694. try {
  42695. dirents = fs__default.readdirSync(file, { withFileTypes: true });
  42696. } catch {
  42697. return;
  42698. }
  42699. return direntsToDirentMap(dirents);
  42700. }
  42701. function direntsToDirentMap(fsDirents) {
  42702. const dirents = /* @__PURE__ */ new Map();
  42703. for (const dirent of fsDirents) {
  42704. const type = dirent.isDirectory() ? "directory" : dirent.isSymbolicLink() ? "symlink" : dirent.isFile() ? "file" : void 0;
  42705. if (type) {
  42706. dirents.set(dirent.name, { type });
  42707. }
  42708. }
  42709. return dirents;
  42710. }
  42711. function ensureFileMaybeSymlinkIsResolved(direntCache, filePath) {
  42712. if (direntCache.type !== "file_maybe_symlink") return;
  42713. const isSymlink = fs__default.lstatSync(filePath, { throwIfNoEntry: false })?.isSymbolicLink();
  42714. direntCache.type = isSymlink === void 0 ? "error" : isSymlink ? "symlink" : "file";
  42715. }
  42716. function pathUntilPart(root, parts, i) {
  42717. let p = root;
  42718. for (let k = 0; k < i; k++) p += "/" + parts[k];
  42719. return p;
  42720. }
  42721. function createCachedFsUtils(config) {
  42722. const root = config.root;
  42723. const rootDirPath = `${root}/`;
  42724. const rootCache = { type: "directory" };
  42725. const getDirentCacheSync = (parts) => {
  42726. let direntCache = rootCache;
  42727. for (let i = 0; i < parts.length; i++) {
  42728. if (direntCache.type === "directory") {
  42729. let dirPath;
  42730. if (!direntCache.dirents) {
  42731. dirPath = pathUntilPart(root, parts, i);
  42732. const dirents = readDirCacheSync(dirPath);
  42733. if (!dirents) {
  42734. direntCache.type = "error";
  42735. return;
  42736. }
  42737. direntCache.dirents = dirents;
  42738. }
  42739. const nextDirentCache = direntCache.dirents.get(parts[i]);
  42740. if (!nextDirentCache) {
  42741. return;
  42742. }
  42743. if (nextDirentCache.type === "directory_maybe_symlink") {
  42744. dirPath ??= pathUntilPart(root, parts, i + 1);
  42745. const isSymlink = fs__default.lstatSync(dirPath, { throwIfNoEntry: false })?.isSymbolicLink();
  42746. nextDirentCache.type = isSymlink ? "symlink" : "directory";
  42747. }
  42748. direntCache = nextDirentCache;
  42749. } else if (direntCache.type === "symlink") {
  42750. return direntCache;
  42751. } else if (direntCache.type === "error") {
  42752. return direntCache;
  42753. } else {
  42754. if (i !== parts.length - 1) {
  42755. return;
  42756. }
  42757. if (direntCache.type === "file_maybe_symlink") {
  42758. ensureFileMaybeSymlinkIsResolved(
  42759. direntCache,
  42760. pathUntilPart(root, parts, i)
  42761. );
  42762. return direntCache;
  42763. } else if (direntCache.type === "file") {
  42764. return direntCache;
  42765. } else {
  42766. return;
  42767. }
  42768. }
  42769. }
  42770. return direntCache;
  42771. };
  42772. function getDirentCacheFromPath(normalizedFile) {
  42773. if (normalizedFile[normalizedFile.length - 1] === "/") {
  42774. normalizedFile = normalizedFile.slice(0, -1);
  42775. }
  42776. if (normalizedFile === root) {
  42777. return rootCache;
  42778. }
  42779. if (!normalizedFile.startsWith(rootDirPath)) {
  42780. return void 0;
  42781. }
  42782. const pathFromRoot = normalizedFile.slice(rootDirPath.length);
  42783. const parts = pathFromRoot.split("/");
  42784. const direntCache = getDirentCacheSync(parts);
  42785. if (!direntCache || direntCache.type === "error") {
  42786. return false;
  42787. }
  42788. return direntCache;
  42789. }
  42790. function onPathAdd(file, type) {
  42791. const direntCache = getDirentCacheFromPath(
  42792. normalizePath$3(path$n.dirname(file))
  42793. );
  42794. if (direntCache && direntCache.type === "directory" && direntCache.dirents) {
  42795. direntCache.dirents.set(path$n.basename(file), { type });
  42796. }
  42797. }
  42798. function onPathUnlink(file) {
  42799. const direntCache = getDirentCacheFromPath(
  42800. normalizePath$3(path$n.dirname(file))
  42801. );
  42802. if (direntCache && direntCache.type === "directory" && direntCache.dirents) {
  42803. direntCache.dirents.delete(path$n.basename(file));
  42804. }
  42805. }
  42806. return {
  42807. existsSync(file) {
  42808. if (isInNodeModules$1(file)) {
  42809. return fs__default.existsSync(file);
  42810. }
  42811. const normalizedFile = normalizePath$3(file);
  42812. const direntCache = getDirentCacheFromPath(normalizedFile);
  42813. if (direntCache === void 0 || direntCache && direntCache.type === "symlink") {
  42814. return fs__default.existsSync(file);
  42815. }
  42816. return !!direntCache;
  42817. },
  42818. tryResolveRealFile(file, preserveSymlinks) {
  42819. if (isInNodeModules$1(file)) {
  42820. return tryResolveRealFile(file, preserveSymlinks);
  42821. }
  42822. const normalizedFile = normalizePath$3(file);
  42823. const direntCache = getDirentCacheFromPath(normalizedFile);
  42824. if (direntCache === void 0 || direntCache && direntCache.type === "symlink") {
  42825. return tryResolveRealFile(file, preserveSymlinks);
  42826. }
  42827. if (!direntCache || direntCache.type === "directory") {
  42828. return;
  42829. }
  42830. return normalizedFile;
  42831. },
  42832. tryResolveRealFileWithExtensions(file, extensions, preserveSymlinks) {
  42833. if (isInNodeModules$1(file)) {
  42834. return tryResolveRealFileWithExtensions(
  42835. file,
  42836. extensions,
  42837. preserveSymlinks
  42838. );
  42839. }
  42840. const normalizedFile = normalizePath$3(file);
  42841. const dirPath = path$n.posix.dirname(normalizedFile);
  42842. const direntCache = getDirentCacheFromPath(dirPath);
  42843. if (direntCache === void 0 || direntCache && direntCache.type === "symlink") {
  42844. return tryResolveRealFileWithExtensions(
  42845. file,
  42846. extensions,
  42847. preserveSymlinks
  42848. );
  42849. }
  42850. if (!direntCache || direntCache.type !== "directory") {
  42851. return;
  42852. }
  42853. if (!direntCache.dirents) {
  42854. const dirents = readDirCacheSync(dirPath);
  42855. if (!dirents) {
  42856. direntCache.type = "error";
  42857. return;
  42858. }
  42859. direntCache.dirents = dirents;
  42860. }
  42861. const base = path$n.posix.basename(normalizedFile);
  42862. for (const ext of extensions) {
  42863. const fileName = base + ext;
  42864. const fileDirentCache = direntCache.dirents.get(fileName);
  42865. if (fileDirentCache) {
  42866. const filePath = dirPath + "/" + fileName;
  42867. ensureFileMaybeSymlinkIsResolved(fileDirentCache, filePath);
  42868. if (fileDirentCache.type === "symlink") {
  42869. return tryResolveRealFile(filePath, preserveSymlinks);
  42870. }
  42871. if (fileDirentCache.type === "file") {
  42872. return filePath;
  42873. }
  42874. }
  42875. }
  42876. },
  42877. tryResolveRealFileOrType(file, preserveSymlinks) {
  42878. if (isInNodeModules$1(file)) {
  42879. return tryResolveRealFileOrType(file, preserveSymlinks);
  42880. }
  42881. const normalizedFile = normalizePath$3(file);
  42882. const direntCache = getDirentCacheFromPath(normalizedFile);
  42883. if (direntCache === void 0 || direntCache && direntCache.type === "symlink") {
  42884. return tryResolveRealFileOrType(file, preserveSymlinks);
  42885. }
  42886. if (!direntCache) {
  42887. return;
  42888. }
  42889. if (direntCache.type === "directory") {
  42890. return { type: "directory" };
  42891. }
  42892. return { path: normalizedFile, type: "file" };
  42893. },
  42894. isDirectory(dirPath) {
  42895. if (isInNodeModules$1(dirPath)) {
  42896. return isDirectory(dirPath);
  42897. }
  42898. const direntCache = getDirentCacheFromPath(normalizePath$3(dirPath));
  42899. if (direntCache === void 0 || direntCache && direntCache.type === "symlink") {
  42900. return isDirectory(dirPath);
  42901. }
  42902. return direntCache && direntCache.type === "directory";
  42903. },
  42904. initWatcher(watcher) {
  42905. watcher.on("add", (file) => {
  42906. onPathAdd(file, "file_maybe_symlink");
  42907. });
  42908. watcher.on("addDir", (dir) => {
  42909. onPathAdd(dir, "directory_maybe_symlink");
  42910. });
  42911. watcher.on("unlink", onPathUnlink);
  42912. watcher.on("unlinkDir", onPathUnlink);
  42913. }
  42914. };
  42915. }
  42916. function tryResolveRealFile(file, preserveSymlinks) {
  42917. const stat = tryStatSync(file);
  42918. if (stat?.isFile()) return getRealPath(file, preserveSymlinks);
  42919. }
  42920. function tryResolveRealFileWithExtensions(filePath, extensions, preserveSymlinks) {
  42921. for (const ext of extensions) {
  42922. const res = tryResolveRealFile(filePath + ext, preserveSymlinks);
  42923. if (res) return res;
  42924. }
  42925. }
  42926. function tryResolveRealFileOrType(file, preserveSymlinks) {
  42927. const fileStat = tryStatSync(file);
  42928. if (fileStat?.isFile()) {
  42929. return { path: getRealPath(file, preserveSymlinks), type: "file" };
  42930. }
  42931. if (fileStat?.isDirectory()) {
  42932. return { type: "directory" };
  42933. }
  42934. return;
  42935. }
  42936. function getRealPath(resolved, preserveSymlinks) {
  42937. if (!preserveSymlinks) {
  42938. resolved = safeRealpathSync(resolved);
  42939. }
  42940. return normalizePath$3(resolved);
  42941. }
  42942. function isDirectory(path2) {
  42943. const stat = tryStatSync(path2);
  42944. return stat?.isDirectory() ?? false;
  42945. }
  42946. /*!
  42947. * etag
  42948. * Copyright(c) 2014-2016 Douglas Christopher Wilson
  42949. * MIT Licensed
  42950. */
  42951. /**
  42952. * Module exports.
  42953. * @public
  42954. */
  42955. var etag_1 = etag;
  42956. /**
  42957. * Module dependencies.
  42958. * @private
  42959. */
  42960. var crypto = require$$3$1;
  42961. var Stats = require$$0__default.Stats;
  42962. /**
  42963. * Module variables.
  42964. * @private
  42965. */
  42966. var toString = Object.prototype.toString;
  42967. /**
  42968. * Generate an entity tag.
  42969. *
  42970. * @param {Buffer|string} entity
  42971. * @return {string}
  42972. * @private
  42973. */
  42974. function entitytag (entity) {
  42975. if (entity.length === 0) {
  42976. // fast-path empty
  42977. return '"0-2jmj7l5rSw0yVb/vlWAYkK/YBwk"'
  42978. }
  42979. // compute hash of entity
  42980. var hash = crypto
  42981. .createHash('sha1')
  42982. .update(entity, 'utf8')
  42983. .digest('base64')
  42984. .substring(0, 27);
  42985. // compute length of entity
  42986. var len = typeof entity === 'string'
  42987. ? Buffer.byteLength(entity, 'utf8')
  42988. : entity.length;
  42989. return '"' + len.toString(16) + '-' + hash + '"'
  42990. }
  42991. /**
  42992. * Create a simple ETag.
  42993. *
  42994. * @param {string|Buffer|Stats} entity
  42995. * @param {object} [options]
  42996. * @param {boolean} [options.weak]
  42997. * @return {String}
  42998. * @public
  42999. */
  43000. function etag (entity, options) {
  43001. if (entity == null) {
  43002. throw new TypeError('argument entity is required')
  43003. }
  43004. // support fs.Stats object
  43005. var isStats = isstats(entity);
  43006. var weak = options && typeof options.weak === 'boolean'
  43007. ? options.weak
  43008. : isStats;
  43009. // validate argument
  43010. if (!isStats && typeof entity !== 'string' && !Buffer.isBuffer(entity)) {
  43011. throw new TypeError('argument entity must be string, Buffer, or fs.Stats')
  43012. }
  43013. // generate entity tag
  43014. var tag = isStats
  43015. ? stattag(entity)
  43016. : entitytag(entity);
  43017. return weak
  43018. ? 'W/' + tag
  43019. : tag
  43020. }
  43021. /**
  43022. * Determine if object is a Stats object.
  43023. *
  43024. * @param {object} obj
  43025. * @return {boolean}
  43026. * @api private
  43027. */
  43028. function isstats (obj) {
  43029. // genuine fs.Stats
  43030. if (typeof Stats === 'function' && obj instanceof Stats) {
  43031. return true
  43032. }
  43033. // quack quack
  43034. return obj && typeof obj === 'object' &&
  43035. 'ctime' in obj && toString.call(obj.ctime) === '[object Date]' &&
  43036. 'mtime' in obj && toString.call(obj.mtime) === '[object Date]' &&
  43037. 'ino' in obj && typeof obj.ino === 'number' &&
  43038. 'size' in obj && typeof obj.size === 'number'
  43039. }
  43040. /**
  43041. * Generate a tag for a stat.
  43042. *
  43043. * @param {object} stat
  43044. * @return {string}
  43045. * @private
  43046. */
  43047. function stattag (stat) {
  43048. var mtime = stat.mtime.getTime().toString(16);
  43049. var size = stat.size.toString(16);
  43050. return '"' + size + '-' + mtime + '"'
  43051. }
  43052. var getEtag = /*@__PURE__*/getDefaultExportFromCjs(etag_1);
  43053. 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)}
  43054. const normalizedClientEntry$1 = normalizePath$3(CLIENT_ENTRY);
  43055. const normalizedEnvEntry$1 = normalizePath$3(ENV_ENTRY);
  43056. const ERR_RESOLVE_PACKAGE_ENTRY_FAIL = "ERR_RESOLVE_PACKAGE_ENTRY_FAIL";
  43057. const browserExternalId = "__vite-browser-external";
  43058. const optionalPeerDepId = "__vite-optional-peer-dep";
  43059. const subpathImportsPrefix = "#";
  43060. const startsWithWordCharRE = /^\w/;
  43061. const debug$c = createDebugger("vite:resolve-details", {
  43062. onlyWhenFocused: true
  43063. });
  43064. function resolvePlugin(resolveOptions) {
  43065. const {
  43066. root,
  43067. isProduction,
  43068. asSrc,
  43069. ssrConfig,
  43070. preferRelative = false
  43071. } = resolveOptions;
  43072. const {
  43073. target: ssrTarget,
  43074. noExternal: ssrNoExternal,
  43075. external: ssrExternal
  43076. } = ssrConfig ?? {};
  43077. const rootInRoot = tryStatSync(path$n.join(root, root))?.isDirectory() ?? false;
  43078. return {
  43079. name: "vite:resolve",
  43080. async resolveId(id, importer, resolveOpts) {
  43081. if (id[0] === "\0" || id.startsWith("virtual:") || // When injected directly in html/client code
  43082. id.startsWith("/virtual:")) {
  43083. return;
  43084. }
  43085. const ssr = resolveOpts?.ssr === true;
  43086. const depsOptimizer = resolveOptions.getDepsOptimizer?.(ssr);
  43087. if (id.startsWith(browserExternalId)) {
  43088. return id;
  43089. }
  43090. const targetWeb = !ssr || ssrTarget === "webworker";
  43091. const isRequire = resolveOpts?.custom?.["node-resolve"]?.isRequire ?? false;
  43092. const ssrConditions = resolveOptions.ssrConfig?.resolve?.conditions || resolveOptions.conditions;
  43093. const options = {
  43094. isRequire,
  43095. ...resolveOptions,
  43096. scan: resolveOpts?.scan ?? resolveOptions.scan,
  43097. conditions: ssr ? ssrConditions : resolveOptions.conditions
  43098. };
  43099. const resolvedImports = resolveSubpathImports(
  43100. id,
  43101. importer,
  43102. options,
  43103. targetWeb
  43104. );
  43105. if (resolvedImports) {
  43106. id = resolvedImports;
  43107. if (resolveOpts.custom?.["vite:import-glob"]?.isSubImportsPattern) {
  43108. return normalizePath$3(path$n.join(root, id));
  43109. }
  43110. }
  43111. if (importer) {
  43112. if (isTsRequest(importer) || resolveOpts.custom?.depScan?.loader?.startsWith("ts")) {
  43113. options.isFromTsImporter = true;
  43114. } else {
  43115. const moduleLang = this.getModuleInfo(importer)?.meta?.vite?.lang;
  43116. options.isFromTsImporter = moduleLang && isTsRequest(`.${moduleLang}`);
  43117. }
  43118. }
  43119. let res;
  43120. if (asSrc && depsOptimizer?.isOptimizedDepUrl(id)) {
  43121. const optimizedPath = id.startsWith(FS_PREFIX) ? fsPathFromId(id) : normalizePath$3(path$n.resolve(root, id.slice(1)));
  43122. return optimizedPath;
  43123. }
  43124. if (asSrc && id.startsWith(FS_PREFIX)) {
  43125. res = fsPathFromId(id);
  43126. debug$c?.(`[@fs] ${colors$1.cyan(id)} -> ${colors$1.dim(res)}`);
  43127. return ensureVersionQuery(res, id, options, depsOptimizer);
  43128. }
  43129. if (asSrc && id[0] === "/" && (rootInRoot || !id.startsWith(withTrailingSlash(root)))) {
  43130. const fsPath = path$n.resolve(root, id.slice(1));
  43131. if (res = tryFsResolve(fsPath, options)) {
  43132. debug$c?.(`[url] ${colors$1.cyan(id)} -> ${colors$1.dim(res)}`);
  43133. return ensureVersionQuery(res, id, options, depsOptimizer);
  43134. }
  43135. }
  43136. if (id[0] === "." || (preferRelative || importer?.endsWith(".html")) && startsWithWordCharRE.test(id)) {
  43137. const basedir = importer ? path$n.dirname(importer) : process.cwd();
  43138. const fsPath = path$n.resolve(basedir, id);
  43139. const normalizedFsPath = normalizePath$3(fsPath);
  43140. if (depsOptimizer?.isOptimizedDepFile(normalizedFsPath)) {
  43141. if (!resolveOptions.isBuild && !DEP_VERSION_RE.test(normalizedFsPath)) {
  43142. const browserHash = optimizedDepInfoFromFile(
  43143. depsOptimizer.metadata,
  43144. normalizedFsPath
  43145. )?.browserHash;
  43146. if (browserHash) {
  43147. return injectQuery(normalizedFsPath, `v=${browserHash}`);
  43148. }
  43149. }
  43150. return normalizedFsPath;
  43151. }
  43152. if (targetWeb && options.mainFields.includes("browser") && (res = tryResolveBrowserMapping(fsPath, importer, options, true))) {
  43153. return res;
  43154. }
  43155. if (res = tryFsResolve(fsPath, options)) {
  43156. res = ensureVersionQuery(res, id, options, depsOptimizer);
  43157. debug$c?.(`[relative] ${colors$1.cyan(id)} -> ${colors$1.dim(res)}`);
  43158. if (!options.idOnly && !options.scan && options.isBuild && !importer?.endsWith(".html")) {
  43159. const resPkg = findNearestPackageData(
  43160. path$n.dirname(res),
  43161. options.packageCache
  43162. );
  43163. if (resPkg) {
  43164. return {
  43165. id: res,
  43166. moduleSideEffects: resPkg.hasSideEffects(res)
  43167. };
  43168. }
  43169. }
  43170. return res;
  43171. }
  43172. }
  43173. if (isWindows$3 && id[0] === "/") {
  43174. const basedir = importer ? path$n.dirname(importer) : process.cwd();
  43175. const fsPath = path$n.resolve(basedir, id);
  43176. if (res = tryFsResolve(fsPath, options)) {
  43177. debug$c?.(`[drive-relative] ${colors$1.cyan(id)} -> ${colors$1.dim(res)}`);
  43178. return ensureVersionQuery(res, id, options, depsOptimizer);
  43179. }
  43180. }
  43181. if (isNonDriveRelativeAbsolutePath(id) && (res = tryFsResolve(id, options))) {
  43182. debug$c?.(`[fs] ${colors$1.cyan(id)} -> ${colors$1.dim(res)}`);
  43183. return ensureVersionQuery(res, id, options, depsOptimizer);
  43184. }
  43185. if (isExternalUrl(id)) {
  43186. return options.idOnly ? id : { id, external: true };
  43187. }
  43188. if (isDataUrl(id)) {
  43189. return null;
  43190. }
  43191. if (bareImportRE.test(id)) {
  43192. const external = options.shouldExternalize?.(id, importer);
  43193. if (!external && asSrc && depsOptimizer && !options.scan && (res = await tryOptimizedResolve(
  43194. depsOptimizer,
  43195. id,
  43196. importer,
  43197. options.preserveSymlinks,
  43198. options.packageCache
  43199. ))) {
  43200. return res;
  43201. }
  43202. if (targetWeb && options.mainFields.includes("browser") && (res = tryResolveBrowserMapping(
  43203. id,
  43204. importer,
  43205. options,
  43206. false,
  43207. external
  43208. ))) {
  43209. return res;
  43210. }
  43211. if (res = tryNodeResolve(
  43212. id,
  43213. importer,
  43214. options,
  43215. targetWeb,
  43216. depsOptimizer,
  43217. ssr,
  43218. external
  43219. )) {
  43220. return res;
  43221. }
  43222. if (isBuiltin(id)) {
  43223. if (ssr) {
  43224. if (targetWeb && ssrNoExternal === true && // if both noExternal and external are true, noExternal will take the higher priority and bundle it.
  43225. // only if the id is explicitly listed in external, we will externalize it and skip this error.
  43226. (ssrExternal === true || !ssrExternal?.includes(id))) {
  43227. let message = `Cannot bundle Node.js built-in "${id}"`;
  43228. if (importer) {
  43229. message += ` imported from "${path$n.relative(
  43230. process.cwd(),
  43231. importer
  43232. )}"`;
  43233. }
  43234. message += `. Consider disabling ssr.noExternal or remove the built-in dependency.`;
  43235. this.error(message);
  43236. }
  43237. return options.idOnly ? id : { id, external: true, moduleSideEffects: false };
  43238. } else {
  43239. if (!asSrc) {
  43240. debug$c?.(
  43241. `externalized node built-in "${id}" to empty module. (imported by: ${colors$1.white(colors$1.dim(importer))})`
  43242. );
  43243. } else if (isProduction) {
  43244. this.warn(
  43245. `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.`
  43246. );
  43247. }
  43248. return isProduction ? browserExternalId : `${browserExternalId}:${id}`;
  43249. }
  43250. }
  43251. }
  43252. debug$c?.(`[fallthrough] ${colors$1.dim(id)}`);
  43253. },
  43254. load(id) {
  43255. if (id.startsWith(browserExternalId)) {
  43256. if (isProduction) {
  43257. return `export default {}`;
  43258. } else {
  43259. id = id.slice(browserExternalId.length + 1);
  43260. return `export default new Proxy({}, {
  43261. get(_, key) {
  43262. 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.\`)
  43263. }
  43264. })`;
  43265. }
  43266. }
  43267. if (id.startsWith(optionalPeerDepId)) {
  43268. if (isProduction) {
  43269. return `export default {}`;
  43270. } else {
  43271. const [, peerDep, parentDep] = id.split(":");
  43272. return `throw new Error(\`Could not resolve "${peerDep}" imported by "${parentDep}". Is it installed?\`)`;
  43273. }
  43274. }
  43275. }
  43276. };
  43277. }
  43278. function resolveSubpathImports(id, importer, options, targetWeb) {
  43279. if (!importer || !id.startsWith(subpathImportsPrefix)) return;
  43280. const basedir = path$n.dirname(importer);
  43281. const pkgData = findNearestPackageData(basedir, options.packageCache);
  43282. if (!pkgData) return;
  43283. let { file: idWithoutPostfix, postfix } = splitFileAndPostfix(id.slice(1));
  43284. idWithoutPostfix = "#" + idWithoutPostfix;
  43285. let importsPath = resolveExportsOrImports(
  43286. pkgData.data,
  43287. idWithoutPostfix,
  43288. options,
  43289. targetWeb,
  43290. "imports"
  43291. );
  43292. if (importsPath?.[0] === ".") {
  43293. importsPath = path$n.relative(basedir, path$n.join(pkgData.dir, importsPath));
  43294. if (importsPath[0] !== ".") {
  43295. importsPath = `./${importsPath}`;
  43296. }
  43297. }
  43298. return importsPath + postfix;
  43299. }
  43300. function ensureVersionQuery(resolved, id, options, depsOptimizer) {
  43301. if (!options.isBuild && !options.scan && depsOptimizer && !(resolved === normalizedClientEntry$1 || resolved === normalizedEnvEntry$1)) {
  43302. const isNodeModule = isInNodeModules$1(id) || isInNodeModules$1(resolved);
  43303. if (isNodeModule && !DEP_VERSION_RE.test(resolved)) {
  43304. const versionHash = depsOptimizer.metadata.browserHash;
  43305. if (versionHash && isOptimizable(resolved, depsOptimizer.options)) {
  43306. resolved = injectQuery(resolved, `v=${versionHash}`);
  43307. }
  43308. }
  43309. }
  43310. return resolved;
  43311. }
  43312. function splitFileAndPostfix(path2) {
  43313. const file = cleanUrl(path2);
  43314. return { file, postfix: path2.slice(file.length) };
  43315. }
  43316. function tryFsResolve(fsPath, options, tryIndex = true, targetWeb = true, skipPackageJson = false) {
  43317. const hashIndex = fsPath.indexOf("#");
  43318. if (hashIndex >= 0 && isInNodeModules$1(fsPath)) {
  43319. const queryIndex = fsPath.indexOf("?");
  43320. if (queryIndex < 0 || queryIndex > hashIndex) {
  43321. const file2 = queryIndex > hashIndex ? fsPath.slice(0, queryIndex) : fsPath;
  43322. const res2 = tryCleanFsResolve(
  43323. file2,
  43324. options,
  43325. tryIndex,
  43326. targetWeb,
  43327. skipPackageJson
  43328. );
  43329. if (res2) return res2 + fsPath.slice(file2.length);
  43330. }
  43331. }
  43332. const { file, postfix } = splitFileAndPostfix(fsPath);
  43333. const res = tryCleanFsResolve(
  43334. file,
  43335. options,
  43336. tryIndex,
  43337. targetWeb,
  43338. skipPackageJson
  43339. );
  43340. if (res) return res + postfix;
  43341. }
  43342. const knownTsOutputRE = /\.(?:js|mjs|cjs|jsx)$/;
  43343. const isPossibleTsOutput = (url) => knownTsOutputRE.test(url);
  43344. function tryCleanFsResolve(file, options, tryIndex = true, targetWeb = true, skipPackageJson = false) {
  43345. const { tryPrefix, extensions, preserveSymlinks } = options;
  43346. const fsUtils = options.fsUtils ?? commonFsUtils;
  43347. const fileResult = fsUtils.tryResolveRealFileOrType(
  43348. file,
  43349. options.preserveSymlinks
  43350. );
  43351. if (fileResult?.path) return fileResult.path;
  43352. let res;
  43353. const possibleJsToTs = options.isFromTsImporter && isPossibleTsOutput(file);
  43354. if (possibleJsToTs || options.extensions.length || tryPrefix) {
  43355. const dirPath = path$n.dirname(file);
  43356. if (fsUtils.isDirectory(dirPath)) {
  43357. if (possibleJsToTs) {
  43358. const fileExt = path$n.extname(file);
  43359. const fileName = file.slice(0, -fileExt.length);
  43360. if (res = fsUtils.tryResolveRealFile(
  43361. fileName + fileExt.replace("js", "ts"),
  43362. preserveSymlinks
  43363. ))
  43364. return res;
  43365. if (fileExt === ".js" && (res = fsUtils.tryResolveRealFile(
  43366. fileName + ".tsx",
  43367. preserveSymlinks
  43368. )))
  43369. return res;
  43370. }
  43371. if (res = fsUtils.tryResolveRealFileWithExtensions(
  43372. file,
  43373. extensions,
  43374. preserveSymlinks
  43375. ))
  43376. return res;
  43377. if (tryPrefix) {
  43378. const prefixed = `${dirPath}/${options.tryPrefix}${path$n.basename(file)}`;
  43379. if (res = fsUtils.tryResolveRealFile(prefixed, preserveSymlinks))
  43380. return res;
  43381. if (res = fsUtils.tryResolveRealFileWithExtensions(
  43382. prefixed,
  43383. extensions,
  43384. preserveSymlinks
  43385. ))
  43386. return res;
  43387. }
  43388. }
  43389. }
  43390. if (tryIndex && fileResult?.type === "directory") {
  43391. const dirPath = file;
  43392. if (!skipPackageJson) {
  43393. let pkgPath = `${dirPath}/package.json`;
  43394. try {
  43395. if (fsUtils.existsSync(pkgPath)) {
  43396. if (!options.preserveSymlinks) {
  43397. pkgPath = safeRealpathSync(pkgPath);
  43398. }
  43399. const pkg = loadPackageData(pkgPath);
  43400. return resolvePackageEntry(dirPath, pkg, targetWeb, options);
  43401. }
  43402. } catch (e) {
  43403. if (e.code !== ERR_RESOLVE_PACKAGE_ENTRY_FAIL && e.code !== "ENOENT")
  43404. throw e;
  43405. }
  43406. }
  43407. if (res = fsUtils.tryResolveRealFileWithExtensions(
  43408. `${dirPath}/index`,
  43409. extensions,
  43410. preserveSymlinks
  43411. ))
  43412. return res;
  43413. if (tryPrefix) {
  43414. if (res = fsUtils.tryResolveRealFileWithExtensions(
  43415. `${dirPath}/${options.tryPrefix}index`,
  43416. extensions,
  43417. preserveSymlinks
  43418. ))
  43419. return res;
  43420. }
  43421. }
  43422. }
  43423. function tryNodeResolve(id, importer, options, targetWeb, depsOptimizer, ssr = false, externalize, allowLinkedExternal = true) {
  43424. const { root, dedupe, isBuild, preserveSymlinks, packageCache } = options;
  43425. const deepMatch = deepImportRE.exec(id);
  43426. const pkgId = deepMatch ? deepMatch[1] || deepMatch[2] : cleanUrl(id);
  43427. let basedir;
  43428. if (dedupe?.includes(pkgId)) {
  43429. basedir = root;
  43430. } else if (importer && path$n.isAbsolute(importer) && // css processing appends `*` for importer
  43431. (importer[importer.length - 1] === "*" || fs__default.existsSync(cleanUrl(importer)))) {
  43432. basedir = path$n.dirname(importer);
  43433. } else {
  43434. basedir = root;
  43435. }
  43436. let selfPkg = null;
  43437. if (!isBuiltin(id) && !id.includes("\0") && bareImportRE.test(id)) {
  43438. const selfPackageData = findNearestPackageData(basedir, packageCache);
  43439. selfPkg = selfPackageData?.data.exports && selfPackageData?.data.name === pkgId ? selfPackageData : null;
  43440. }
  43441. const pkg = selfPkg || resolvePackageData(pkgId, basedir, preserveSymlinks, packageCache);
  43442. if (!pkg) {
  43443. if (basedir !== root && // root has no peer dep
  43444. !isBuiltin(id) && !id.includes("\0") && bareImportRE.test(id)) {
  43445. const mainPkg = findNearestMainPackageData(basedir, packageCache)?.data;
  43446. if (mainPkg) {
  43447. const pkgName = getNpmPackageName(id);
  43448. if (pkgName != null && mainPkg.peerDependencies?.[pkgName] && mainPkg.peerDependenciesMeta?.[pkgName]?.optional) {
  43449. return {
  43450. id: `${optionalPeerDepId}:${id}:${mainPkg.name}`
  43451. };
  43452. }
  43453. }
  43454. }
  43455. return;
  43456. }
  43457. const resolveId = deepMatch ? resolveDeepImport : resolvePackageEntry;
  43458. const unresolvedId = deepMatch ? "." + id.slice(pkgId.length) : id;
  43459. let resolved;
  43460. try {
  43461. resolved = resolveId(unresolvedId, pkg, targetWeb, options);
  43462. } catch (err) {
  43463. if (!options.tryEsmOnly) {
  43464. throw err;
  43465. }
  43466. }
  43467. if (!resolved && options.tryEsmOnly) {
  43468. resolved = resolveId(unresolvedId, pkg, targetWeb, {
  43469. ...options,
  43470. isRequire: false,
  43471. mainFields: DEFAULT_MAIN_FIELDS,
  43472. extensions: DEFAULT_EXTENSIONS
  43473. });
  43474. }
  43475. if (!resolved) {
  43476. return;
  43477. }
  43478. const processResult = (resolved2) => {
  43479. if (!externalize) {
  43480. return resolved2;
  43481. }
  43482. if (!allowLinkedExternal && !isInNodeModules$1(resolved2.id)) {
  43483. return resolved2;
  43484. }
  43485. const resolvedExt = path$n.extname(resolved2.id);
  43486. if (resolvedExt && resolvedExt !== ".js" && resolvedExt !== ".mjs" && resolvedExt !== ".cjs") {
  43487. return resolved2;
  43488. }
  43489. let resolvedId = id;
  43490. if (deepMatch && !pkg?.data.exports && path$n.extname(id) !== resolvedExt) {
  43491. const index = resolved2.id.indexOf(id);
  43492. if (index > -1) {
  43493. resolvedId = resolved2.id.slice(index);
  43494. debug$c?.(
  43495. `[processResult] ${colors$1.cyan(id)} -> ${colors$1.dim(resolvedId)}`
  43496. );
  43497. }
  43498. }
  43499. return { ...resolved2, id: resolvedId, external: true };
  43500. };
  43501. if (!options.idOnly && (!options.scan && isBuild && !depsOptimizer || externalize)) {
  43502. return processResult({
  43503. id: resolved,
  43504. moduleSideEffects: pkg.hasSideEffects(resolved)
  43505. });
  43506. }
  43507. if (!options.ssrOptimizeCheck && (!isInNodeModules$1(resolved) || // linked
  43508. !depsOptimizer || // resolving before listening to the server
  43509. options.scan)) {
  43510. return { id: resolved };
  43511. }
  43512. const isJsType = depsOptimizer ? isOptimizable(resolved, depsOptimizer.options) : OPTIMIZABLE_ENTRY_RE.test(resolved);
  43513. let exclude = depsOptimizer?.options.exclude;
  43514. let include = depsOptimizer?.options.include;
  43515. if (options.ssrOptimizeCheck) {
  43516. exclude = options.ssrConfig?.optimizeDeps?.exclude;
  43517. include = options.ssrConfig?.optimizeDeps?.include;
  43518. }
  43519. 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
  43520. // a non-optimized dep is found. So we need to skip optimization here.
  43521. // The only optimized deps are the ones explicitly listed in the config.
  43522. !options.ssrOptimizeCheck && !isBuild && ssr || // Only optimize non-external CJS deps during SSR by default
  43523. ssr && isFilePathESM(resolved, options.packageCache) && !(include?.includes(pkgId) || include?.includes(id));
  43524. if (options.ssrOptimizeCheck) {
  43525. return {
  43526. id: skipOptimization ? injectQuery(resolved, `__vite_skip_optimization`) : resolved
  43527. };
  43528. }
  43529. if (skipOptimization) {
  43530. if (!isBuild) {
  43531. const versionHash = depsOptimizer.metadata.browserHash;
  43532. if (versionHash && isJsType) {
  43533. resolved = injectQuery(resolved, `v=${versionHash}`);
  43534. }
  43535. }
  43536. } else {
  43537. const optimizedInfo = depsOptimizer.registerMissingImport(id, resolved);
  43538. resolved = depsOptimizer.getOptimizedDepId(optimizedInfo);
  43539. }
  43540. if (!options.idOnly && !options.scan && isBuild) {
  43541. return {
  43542. id: resolved,
  43543. moduleSideEffects: pkg.hasSideEffects(resolved)
  43544. };
  43545. } else {
  43546. return { id: resolved };
  43547. }
  43548. }
  43549. async function tryOptimizedResolve(depsOptimizer, id, importer, preserveSymlinks, packageCache) {
  43550. await depsOptimizer.scanProcessing;
  43551. const metadata = depsOptimizer.metadata;
  43552. const depInfo = optimizedDepInfoFromId(metadata, id);
  43553. if (depInfo) {
  43554. return depsOptimizer.getOptimizedDepId(depInfo);
  43555. }
  43556. if (!importer) return;
  43557. let idPkgDir;
  43558. const nestedIdMatch = `> ${id}`;
  43559. for (const optimizedData of metadata.depInfoList) {
  43560. if (!optimizedData.src) continue;
  43561. if (!optimizedData.id.endsWith(nestedIdMatch)) continue;
  43562. if (idPkgDir == null) {
  43563. const pkgName = getNpmPackageName(id);
  43564. if (!pkgName) break;
  43565. idPkgDir = resolvePackageData(
  43566. pkgName,
  43567. importer,
  43568. preserveSymlinks,
  43569. packageCache
  43570. )?.dir;
  43571. if (idPkgDir == null) break;
  43572. idPkgDir = normalizePath$3(idPkgDir);
  43573. }
  43574. if (optimizedData.src.startsWith(withTrailingSlash(idPkgDir))) {
  43575. return depsOptimizer.getOptimizedDepId(optimizedData);
  43576. }
  43577. }
  43578. }
  43579. function resolvePackageEntry(id, { dir, data, setResolvedCache, getResolvedCache }, targetWeb, options) {
  43580. const { file: idWithoutPostfix, postfix } = splitFileAndPostfix(id);
  43581. const cached = getResolvedCache(".", targetWeb);
  43582. if (cached) {
  43583. return cached + postfix;
  43584. }
  43585. try {
  43586. let entryPoint;
  43587. if (data.exports) {
  43588. entryPoint = resolveExportsOrImports(
  43589. data,
  43590. ".",
  43591. options,
  43592. targetWeb,
  43593. "exports"
  43594. );
  43595. }
  43596. if (!entryPoint) {
  43597. for (const field of options.mainFields) {
  43598. if (field === "browser") {
  43599. if (targetWeb) {
  43600. entryPoint = tryResolveBrowserEntry(dir, data, options);
  43601. if (entryPoint) {
  43602. break;
  43603. }
  43604. }
  43605. } else if (typeof data[field] === "string") {
  43606. entryPoint = data[field];
  43607. break;
  43608. }
  43609. }
  43610. }
  43611. entryPoint ||= data.main;
  43612. const entryPoints = entryPoint ? [entryPoint] : ["index.js", "index.json", "index.node"];
  43613. for (let entry of entryPoints) {
  43614. let skipPackageJson = false;
  43615. if (options.mainFields[0] === "sass" && !options.extensions.includes(path$n.extname(entry))) {
  43616. entry = "";
  43617. skipPackageJson = true;
  43618. } else {
  43619. const { browser: browserField } = data;
  43620. if (targetWeb && options.mainFields.includes("browser") && isObject$1(browserField)) {
  43621. entry = mapWithBrowserField(entry, browserField) || entry;
  43622. }
  43623. }
  43624. const entryPointPath = path$n.join(dir, entry);
  43625. const resolvedEntryPoint = tryFsResolve(
  43626. entryPointPath,
  43627. options,
  43628. true,
  43629. true,
  43630. skipPackageJson
  43631. );
  43632. if (resolvedEntryPoint) {
  43633. debug$c?.(
  43634. `[package entry] ${colors$1.cyan(idWithoutPostfix)} -> ${colors$1.dim(
  43635. resolvedEntryPoint
  43636. )}${postfix !== "" ? ` (postfix: ${postfix})` : ""}`
  43637. );
  43638. setResolvedCache(".", resolvedEntryPoint, targetWeb);
  43639. return resolvedEntryPoint + postfix;
  43640. }
  43641. }
  43642. } catch (e) {
  43643. packageEntryFailure(id, e.message);
  43644. }
  43645. packageEntryFailure(id);
  43646. }
  43647. function packageEntryFailure(id, details) {
  43648. const err = new Error(
  43649. `Failed to resolve entry for package "${id}". The package may have incorrect main/module/exports specified in its package.json` + (details ? ": " + details : ".")
  43650. );
  43651. err.code = ERR_RESOLVE_PACKAGE_ENTRY_FAIL;
  43652. throw err;
  43653. }
  43654. function resolveExportsOrImports(pkg, key, options, targetWeb, type) {
  43655. const additionalConditions = new Set(
  43656. options.overrideConditions || [
  43657. "production",
  43658. "development",
  43659. "module",
  43660. ...options.conditions
  43661. ]
  43662. );
  43663. const conditions = [...additionalConditions].filter((condition) => {
  43664. switch (condition) {
  43665. case "production":
  43666. return options.isProduction;
  43667. case "development":
  43668. return !options.isProduction;
  43669. }
  43670. return true;
  43671. });
  43672. const fn = type === "imports" ? f : o;
  43673. const result = fn(pkg, key, {
  43674. browser: targetWeb && !additionalConditions.has("node"),
  43675. require: options.isRequire && !additionalConditions.has("import"),
  43676. conditions
  43677. });
  43678. return result ? result[0] : void 0;
  43679. }
  43680. function resolveDeepImport(id, {
  43681. webResolvedImports,
  43682. setResolvedCache,
  43683. getResolvedCache,
  43684. dir,
  43685. data
  43686. }, targetWeb, options) {
  43687. const cache = getResolvedCache(id, targetWeb);
  43688. if (cache) {
  43689. return cache;
  43690. }
  43691. let relativeId = id;
  43692. const { exports: exportsField, browser: browserField } = data;
  43693. if (exportsField) {
  43694. if (isObject$1(exportsField) && !Array.isArray(exportsField)) {
  43695. const { file, postfix } = splitFileAndPostfix(relativeId);
  43696. const exportsId = resolveExportsOrImports(
  43697. data,
  43698. file,
  43699. options,
  43700. targetWeb,
  43701. "exports"
  43702. );
  43703. if (exportsId !== void 0) {
  43704. relativeId = exportsId + postfix;
  43705. } else {
  43706. relativeId = void 0;
  43707. }
  43708. } else {
  43709. relativeId = void 0;
  43710. }
  43711. if (!relativeId) {
  43712. throw new Error(
  43713. `Package subpath '${relativeId}' is not defined by "exports" in ${path$n.join(dir, "package.json")}.`
  43714. );
  43715. }
  43716. } else if (targetWeb && options.mainFields.includes("browser") && isObject$1(browserField)) {
  43717. const { file, postfix } = splitFileAndPostfix(relativeId);
  43718. const mapped = mapWithBrowserField(file, browserField);
  43719. if (mapped) {
  43720. relativeId = mapped + postfix;
  43721. } else if (mapped === false) {
  43722. return webResolvedImports[id] = browserExternalId;
  43723. }
  43724. }
  43725. if (relativeId) {
  43726. const resolved = tryFsResolve(
  43727. path$n.join(dir, relativeId),
  43728. options,
  43729. !exportsField,
  43730. // try index only if no exports field
  43731. targetWeb
  43732. );
  43733. if (resolved) {
  43734. debug$c?.(
  43735. `[node/deep-import] ${colors$1.cyan(id)} -> ${colors$1.dim(resolved)}`
  43736. );
  43737. setResolvedCache(id, resolved, targetWeb);
  43738. return resolved;
  43739. }
  43740. }
  43741. }
  43742. function tryResolveBrowserMapping(id, importer, options, isFilePath, externalize) {
  43743. let res;
  43744. const pkg = importer && findNearestPackageData(path$n.dirname(importer), options.packageCache);
  43745. if (pkg && isObject$1(pkg.data.browser)) {
  43746. const mapId = isFilePath ? "./" + slash$1(path$n.relative(pkg.dir, id)) : id;
  43747. const browserMappedPath = mapWithBrowserField(mapId, pkg.data.browser);
  43748. if (browserMappedPath) {
  43749. if (res = bareImportRE.test(browserMappedPath) ? tryNodeResolve(browserMappedPath, importer, options, true)?.id : tryFsResolve(path$n.join(pkg.dir, browserMappedPath), options)) {
  43750. debug$c?.(`[browser mapped] ${colors$1.cyan(id)} -> ${colors$1.dim(res)}`);
  43751. let result = { id: res };
  43752. if (options.idOnly) {
  43753. return result;
  43754. }
  43755. if (!options.scan && options.isBuild) {
  43756. const resPkg = findNearestPackageData(
  43757. path$n.dirname(res),
  43758. options.packageCache
  43759. );
  43760. if (resPkg) {
  43761. result = {
  43762. id: res,
  43763. moduleSideEffects: resPkg.hasSideEffects(res)
  43764. };
  43765. }
  43766. }
  43767. return externalize ? { ...result, external: true } : result;
  43768. }
  43769. } else if (browserMappedPath === false) {
  43770. return browserExternalId;
  43771. }
  43772. }
  43773. }
  43774. function tryResolveBrowserEntry(dir, data, options) {
  43775. const browserEntry = typeof data.browser === "string" ? data.browser : isObject$1(data.browser) && data.browser["."];
  43776. if (browserEntry) {
  43777. if (!options.isRequire && options.mainFields.includes("module") && typeof data.module === "string" && data.module !== browserEntry) {
  43778. const resolvedBrowserEntry = tryFsResolve(
  43779. path$n.join(dir, browserEntry),
  43780. options
  43781. );
  43782. if (resolvedBrowserEntry) {
  43783. const content = fs__default.readFileSync(resolvedBrowserEntry, "utf-8");
  43784. if (hasESMSyntax(content)) {
  43785. return browserEntry;
  43786. } else {
  43787. return data.module;
  43788. }
  43789. }
  43790. } else {
  43791. return browserEntry;
  43792. }
  43793. }
  43794. }
  43795. function mapWithBrowserField(relativePathInPkgDir, map) {
  43796. const normalizedPath = path$n.posix.normalize(relativePathInPkgDir);
  43797. for (const key in map) {
  43798. const normalizedKey = path$n.posix.normalize(key);
  43799. if (normalizedPath === normalizedKey || equalWithoutSuffix(normalizedPath, normalizedKey, ".js") || equalWithoutSuffix(normalizedPath, normalizedKey, "/index.js")) {
  43800. return map[key];
  43801. }
  43802. }
  43803. }
  43804. function equalWithoutSuffix(path2, key, suffix) {
  43805. return key.endsWith(suffix) && key.slice(0, -suffix.length) === path2;
  43806. }
  43807. const externalWithConversionNamespace = "vite:dep-pre-bundle:external-conversion";
  43808. const convertedExternalPrefix = "vite-dep-pre-bundle-external:";
  43809. const cjsExternalFacadeNamespace = "vite:cjs-external-facade";
  43810. const nonFacadePrefix = "vite-cjs-external-facade:";
  43811. const externalTypes = [
  43812. "css",
  43813. // supported pre-processor types
  43814. "less",
  43815. "sass",
  43816. "scss",
  43817. "styl",
  43818. "stylus",
  43819. "pcss",
  43820. "postcss",
  43821. // wasm
  43822. "wasm",
  43823. // known SFC types
  43824. "vue",
  43825. "svelte",
  43826. "marko",
  43827. "astro",
  43828. "imba",
  43829. // JSX/TSX may be configured to be compiled differently from how esbuild
  43830. // handles it by default, so exclude them as well
  43831. "jsx",
  43832. "tsx",
  43833. ...KNOWN_ASSET_TYPES
  43834. ];
  43835. function esbuildDepPlugin(qualified, external, config, ssr) {
  43836. const { extensions } = getDepOptimizationConfig(config, ssr);
  43837. const allExternalTypes = extensions ? externalTypes.filter((type) => !extensions?.includes("." + type)) : externalTypes;
  43838. const esmPackageCache = /* @__PURE__ */ new Map();
  43839. const cjsPackageCache = /* @__PURE__ */ new Map();
  43840. const _resolve = config.createResolver({
  43841. asSrc: false,
  43842. scan: true,
  43843. packageCache: esmPackageCache
  43844. });
  43845. const _resolveRequire = config.createResolver({
  43846. asSrc: false,
  43847. isRequire: true,
  43848. scan: true,
  43849. packageCache: cjsPackageCache
  43850. });
  43851. const resolve = (id, importer, kind, resolveDir) => {
  43852. let _importer;
  43853. {
  43854. _importer = importer in qualified ? qualified[importer] : importer;
  43855. }
  43856. const resolver = kind.startsWith("require") ? _resolveRequire : _resolve;
  43857. return resolver(id, _importer, void 0, ssr);
  43858. };
  43859. const resolveResult = (id, resolved) => {
  43860. if (resolved.startsWith(browserExternalId)) {
  43861. return {
  43862. path: id,
  43863. namespace: "browser-external"
  43864. };
  43865. }
  43866. if (resolved.startsWith(optionalPeerDepId)) {
  43867. return {
  43868. path: resolved,
  43869. namespace: "optional-peer-dep"
  43870. };
  43871. }
  43872. if (ssr && isBuiltin(resolved)) {
  43873. return;
  43874. }
  43875. if (isExternalUrl(resolved)) {
  43876. return {
  43877. path: resolved,
  43878. external: true
  43879. };
  43880. }
  43881. return {
  43882. path: path$n.resolve(resolved)
  43883. };
  43884. };
  43885. return {
  43886. name: "vite:dep-pre-bundle",
  43887. setup(build) {
  43888. build.onEnd(() => {
  43889. esmPackageCache.clear();
  43890. cjsPackageCache.clear();
  43891. });
  43892. build.onResolve(
  43893. {
  43894. filter: new RegExp(
  43895. `\\.(` + allExternalTypes.join("|") + `)(\\?.*)?$`
  43896. )
  43897. },
  43898. async ({ path: id, importer, kind }) => {
  43899. if (id.startsWith(convertedExternalPrefix)) {
  43900. return {
  43901. path: id.slice(convertedExternalPrefix.length),
  43902. external: true
  43903. };
  43904. }
  43905. const resolved = await resolve(id, importer, kind);
  43906. if (resolved) {
  43907. if (kind === "require-call") {
  43908. if (resolved.endsWith(".js")) {
  43909. return {
  43910. path: resolved,
  43911. external: false
  43912. };
  43913. }
  43914. return {
  43915. path: resolved,
  43916. namespace: externalWithConversionNamespace
  43917. };
  43918. }
  43919. return {
  43920. path: resolved,
  43921. external: true
  43922. };
  43923. }
  43924. }
  43925. );
  43926. build.onLoad(
  43927. { filter: /./, namespace: externalWithConversionNamespace },
  43928. (args) => {
  43929. const modulePath = `"${convertedExternalPrefix}${args.path}"`;
  43930. return {
  43931. contents: isCSSRequest(args.path) && !isModuleCSSRequest(args.path) ? `import ${modulePath};` : `export { default } from ${modulePath};export * from ${modulePath};`,
  43932. loader: "js"
  43933. };
  43934. }
  43935. );
  43936. function resolveEntry(id) {
  43937. const flatId = flattenId(id);
  43938. if (flatId in qualified) {
  43939. return {
  43940. path: qualified[flatId]
  43941. };
  43942. }
  43943. }
  43944. build.onResolve(
  43945. { filter: /^[\w@][^:]/ },
  43946. async ({ path: id, importer, kind }) => {
  43947. if (moduleListContains(external, id)) {
  43948. return {
  43949. path: id,
  43950. external: true
  43951. };
  43952. }
  43953. let entry;
  43954. if (!importer) {
  43955. if (entry = resolveEntry(id)) return entry;
  43956. const aliased = await _resolve(id, void 0, true);
  43957. if (aliased && (entry = resolveEntry(aliased))) {
  43958. return entry;
  43959. }
  43960. }
  43961. const resolved = await resolve(id, importer, kind);
  43962. if (resolved) {
  43963. return resolveResult(id, resolved);
  43964. }
  43965. }
  43966. );
  43967. build.onLoad(
  43968. { filter: /.*/, namespace: "browser-external" },
  43969. ({ path: path2 }) => {
  43970. if (config.isProduction) {
  43971. return {
  43972. contents: "module.exports = {}"
  43973. };
  43974. } else {
  43975. return {
  43976. // Return in CJS to intercept named imports. Use `Object.create` to
  43977. // create the Proxy in the prototype to workaround esbuild issue. Why?
  43978. //
  43979. // In short, esbuild cjs->esm flow:
  43980. // 1. Create empty object using `Object.create(Object.getPrototypeOf(module.exports))`.
  43981. // 2. Assign props of `module.exports` to the object.
  43982. // 3. Return object for ESM use.
  43983. //
  43984. // If we do `module.exports = new Proxy({}, {})`, step 1 returns empty object,
  43985. // step 2 does nothing as there's no props for `module.exports`. The final object
  43986. // is just an empty object.
  43987. //
  43988. // Creating the Proxy in the prototype satisfies step 1 immediately, which means
  43989. // the returned object is a Proxy that we can intercept.
  43990. //
  43991. // Note: Skip keys that are accessed by esbuild and browser devtools.
  43992. contents: `module.exports = Object.create(new Proxy({}, {
  43993. get(_, key) {
  43994. if (
  43995. key !== '__esModule' &&
  43996. key !== '__proto__' &&
  43997. key !== 'constructor' &&
  43998. key !== 'splice'
  43999. ) {
  44000. 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.\`)
  44001. }
  44002. }
  44003. }))`
  44004. };
  44005. }
  44006. }
  44007. );
  44008. build.onLoad(
  44009. { filter: /.*/, namespace: "optional-peer-dep" },
  44010. ({ path: path2 }) => {
  44011. if (config.isProduction) {
  44012. return {
  44013. contents: "module.exports = {}"
  44014. };
  44015. } else {
  44016. const [, peerDep, parentDep] = path2.split(":");
  44017. return {
  44018. contents: `throw new Error(\`Could not resolve "${peerDep}" imported by "${parentDep}". Is it installed?\`)`
  44019. };
  44020. }
  44021. }
  44022. );
  44023. }
  44024. };
  44025. }
  44026. const matchesEntireLine = (text) => `^${escapeRegex(text)}$`;
  44027. function esbuildCjsExternalPlugin(externals, platform) {
  44028. return {
  44029. name: "cjs-external",
  44030. setup(build) {
  44031. const filter = new RegExp(externals.map(matchesEntireLine).join("|"));
  44032. build.onResolve({ filter: new RegExp(`^${nonFacadePrefix}`) }, (args) => {
  44033. return {
  44034. path: args.path.slice(nonFacadePrefix.length),
  44035. external: true
  44036. };
  44037. });
  44038. build.onResolve({ filter }, (args) => {
  44039. if (args.kind === "require-call" && platform !== "node") {
  44040. return {
  44041. path: args.path,
  44042. namespace: cjsExternalFacadeNamespace
  44043. };
  44044. }
  44045. return {
  44046. path: args.path,
  44047. external: true
  44048. };
  44049. });
  44050. build.onLoad(
  44051. { filter: /.*/, namespace: cjsExternalFacadeNamespace },
  44052. (args) => ({
  44053. contents: `import * as m from ${JSON.stringify(
  44054. nonFacadePrefix + args.path
  44055. )};module.exports = m;`
  44056. })
  44057. );
  44058. }
  44059. };
  44060. }
  44061. const debug$b = createDebugger("vite:ssr-external");
  44062. const isSsrExternalCache = /* @__PURE__ */ new WeakMap();
  44063. function shouldExternalizeForSSR(id, importer, config) {
  44064. let isSsrExternal = isSsrExternalCache.get(config);
  44065. if (!isSsrExternal) {
  44066. isSsrExternal = createIsSsrExternal(config);
  44067. isSsrExternalCache.set(config, isSsrExternal);
  44068. }
  44069. return isSsrExternal(id, importer);
  44070. }
  44071. function createIsConfiguredAsSsrExternal(config) {
  44072. const { ssr, root } = config;
  44073. const noExternal = ssr?.noExternal;
  44074. const noExternalFilter = noExternal !== "undefined" && typeof noExternal !== "boolean" && createFilter(void 0, noExternal, { resolve: false });
  44075. const targetConditions = config.ssr.resolve?.externalConditions || [];
  44076. const resolveOptions = {
  44077. ...config.resolve,
  44078. root,
  44079. isProduction: false,
  44080. isBuild: true,
  44081. conditions: targetConditions
  44082. };
  44083. const isExternalizable = (id, importer, configuredAsExternal) => {
  44084. if (!bareImportRE.test(id) || id.includes("\0")) {
  44085. return false;
  44086. }
  44087. try {
  44088. return !!tryNodeResolve(
  44089. id,
  44090. // Skip passing importer in build to avoid externalizing non-hoisted dependencies
  44091. // unresolvable from root (which would be unresolvable from output bundles also)
  44092. config.command === "build" ? void 0 : importer,
  44093. resolveOptions,
  44094. ssr?.target === "webworker",
  44095. void 0,
  44096. true,
  44097. // try to externalize, will return undefined or an object without
  44098. // a external flag if it isn't externalizable
  44099. true,
  44100. // Allow linked packages to be externalized if they are explicitly
  44101. // configured as external
  44102. !!configuredAsExternal
  44103. )?.external;
  44104. } catch (e) {
  44105. debug$b?.(
  44106. `Failed to node resolve "${id}". Skipping externalizing it by default.`
  44107. );
  44108. return false;
  44109. }
  44110. };
  44111. return (id, importer) => {
  44112. if (
  44113. // If this id is defined as external, force it as external
  44114. // Note that individual package entries are allowed in ssr.external
  44115. ssr.external !== true && ssr.external?.includes(id)
  44116. ) {
  44117. return true;
  44118. }
  44119. const pkgName = getNpmPackageName(id);
  44120. if (!pkgName) {
  44121. return isExternalizable(id, importer);
  44122. }
  44123. if (
  44124. // A package name in ssr.external externalizes every
  44125. // externalizable package entry
  44126. ssr.external !== true && ssr.external?.includes(pkgName)
  44127. ) {
  44128. return isExternalizable(id, importer, true);
  44129. }
  44130. if (typeof noExternal === "boolean") {
  44131. return !noExternal;
  44132. }
  44133. if (noExternalFilter && !noExternalFilter(pkgName)) {
  44134. return false;
  44135. }
  44136. return isExternalizable(id, importer, ssr.external === true);
  44137. };
  44138. }
  44139. function createIsSsrExternal(config) {
  44140. const processedIds = /* @__PURE__ */ new Map();
  44141. const isConfiguredAsExternal = createIsConfiguredAsSsrExternal(config);
  44142. return (id, importer) => {
  44143. if (processedIds.has(id)) {
  44144. return processedIds.get(id);
  44145. }
  44146. let external = false;
  44147. if (id[0] !== "." && !path$n.isAbsolute(id)) {
  44148. external = isBuiltin(id) || isConfiguredAsExternal(id, importer);
  44149. }
  44150. processedIds.set(id, external);
  44151. return external;
  44152. };
  44153. }
  44154. const jsonExtRE = /\.json(?:$|\?)(?!commonjs-(?:proxy|external))/;
  44155. const jsonLangs = `\\.(?:json|json5)(?:$|\\?)`;
  44156. const jsonLangRE = new RegExp(jsonLangs);
  44157. const isJSONRequest = (request) => jsonLangRE.test(request);
  44158. function jsonPlugin(options = {}, isBuild) {
  44159. return {
  44160. name: "vite:json",
  44161. transform(json, id) {
  44162. if (!jsonExtRE.test(id)) return null;
  44163. if (SPECIAL_QUERY_RE.test(id)) return null;
  44164. json = stripBomTag(json);
  44165. try {
  44166. if (options.stringify) {
  44167. if (isBuild) {
  44168. return {
  44169. // during build, parse then double-stringify to remove all
  44170. // unnecessary whitespaces to reduce bundle size.
  44171. code: `export default JSON.parse(${JSON.stringify(
  44172. JSON.stringify(JSON.parse(json))
  44173. )})`,
  44174. map: { mappings: "" }
  44175. };
  44176. } else {
  44177. return `export default JSON.parse(${JSON.stringify(json)})`;
  44178. }
  44179. }
  44180. const parsed = JSON.parse(json);
  44181. return {
  44182. code: dataToEsm(parsed, {
  44183. preferConst: true,
  44184. namedExports: options.namedExports
  44185. }),
  44186. map: { mappings: "" }
  44187. };
  44188. } catch (e) {
  44189. const position = extractJsonErrorPosition(e.message, json.length);
  44190. const msg = position ? `, invalid JSON syntax found at position ${position}` : `.`;
  44191. this.error(`Failed to parse JSON file` + msg, position);
  44192. }
  44193. }
  44194. };
  44195. }
  44196. function extractJsonErrorPosition(errorMessage, inputLength) {
  44197. if (errorMessage.startsWith("Unexpected end of JSON input")) {
  44198. return inputLength - 1;
  44199. }
  44200. const errorMessageList = /at position (\d+)/.exec(errorMessage);
  44201. return errorMessageList ? Math.max(parseInt(errorMessageList[1], 10) - 1, 0) : void 0;
  44202. }
  44203. const ERR_OPTIMIZE_DEPS_PROCESSING_ERROR = "ERR_OPTIMIZE_DEPS_PROCESSING_ERROR";
  44204. const ERR_OUTDATED_OPTIMIZED_DEP = "ERR_OUTDATED_OPTIMIZED_DEP";
  44205. const ERR_FILE_NOT_FOUND_IN_OPTIMIZED_DEP_DIR = "ERR_FILE_NOT_FOUND_IN_OPTIMIZED_DEP_DIR";
  44206. const debug$a = createDebugger("vite:optimize-deps");
  44207. function optimizedDepsPlugin(config) {
  44208. return {
  44209. name: "vite:optimized-deps",
  44210. resolveId(id, source, { ssr }) {
  44211. if (getDepsOptimizer(config, ssr)?.isOptimizedDepFile(id)) {
  44212. return id;
  44213. }
  44214. },
  44215. // this.load({ id }) isn't implemented in PluginContainer
  44216. // The logic to register an id to wait until it is processed
  44217. // is in importAnalysis, see call to delayDepsOptimizerUntil
  44218. async load(id, options) {
  44219. const ssr = options?.ssr === true;
  44220. const depsOptimizer = getDepsOptimizer(config, ssr);
  44221. if (depsOptimizer?.isOptimizedDepFile(id)) {
  44222. const metadata = depsOptimizer.metadata;
  44223. const file = cleanUrl(id);
  44224. const versionMatch = DEP_VERSION_RE.exec(file);
  44225. const browserHash = versionMatch ? versionMatch[1].split("=")[1] : void 0;
  44226. const info = optimizedDepInfoFromFile(metadata, file);
  44227. if (info) {
  44228. if (browserHash && info.browserHash !== browserHash) {
  44229. throwOutdatedRequest(id);
  44230. }
  44231. try {
  44232. await info.processing;
  44233. } catch {
  44234. throwProcessingError(id);
  44235. }
  44236. const newMetadata = depsOptimizer.metadata;
  44237. if (metadata !== newMetadata) {
  44238. const currentInfo = optimizedDepInfoFromFile(newMetadata, file);
  44239. if (info.browserHash !== currentInfo?.browserHash) {
  44240. throwOutdatedRequest(id);
  44241. }
  44242. }
  44243. }
  44244. debug$a?.(`load ${colors$1.cyan(file)}`);
  44245. try {
  44246. return await fsp.readFile(file, "utf-8");
  44247. } catch (e) {
  44248. const newMetadata = depsOptimizer.metadata;
  44249. if (optimizedDepInfoFromFile(newMetadata, file)) {
  44250. throwOutdatedRequest(id);
  44251. }
  44252. throwFileNotFoundInOptimizedDep(id);
  44253. }
  44254. }
  44255. }
  44256. };
  44257. }
  44258. function throwProcessingError(id) {
  44259. const err = new Error(
  44260. `Something unexpected happened while optimizing "${id}". The current page should have reloaded by now`
  44261. );
  44262. err.code = ERR_OPTIMIZE_DEPS_PROCESSING_ERROR;
  44263. throw err;
  44264. }
  44265. function throwOutdatedRequest(id) {
  44266. const err = new Error(
  44267. `There is a new version of the pre-bundle for "${id}", a page reload is going to ask for it.`
  44268. );
  44269. err.code = ERR_OUTDATED_OPTIMIZED_DEP;
  44270. throw err;
  44271. }
  44272. function throwFileNotFoundInOptimizedDep(id) {
  44273. const err = new Error(
  44274. `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\`.`
  44275. );
  44276. err.code = ERR_FILE_NOT_FOUND_IN_OPTIMIZED_DEP_DIR;
  44277. throw err;
  44278. }
  44279. const nonJsRe = /\.json(?:$|\?)/;
  44280. const isNonJsRequest = (request) => nonJsRe.test(request);
  44281. const importMetaEnvMarker = "__vite_import_meta_env__";
  44282. const importMetaEnvKeyReCache = /* @__PURE__ */ new Map();
  44283. function definePlugin(config) {
  44284. const isBuild = config.command === "build";
  44285. const isBuildLib = isBuild && config.build.lib;
  44286. const processEnv = {};
  44287. if (!isBuildLib) {
  44288. const nodeEnv = process.env.NODE_ENV || config.mode;
  44289. Object.assign(processEnv, {
  44290. "process.env": `{}`,
  44291. "global.process.env": `{}`,
  44292. "globalThis.process.env": `{}`,
  44293. "process.env.NODE_ENV": JSON.stringify(nodeEnv),
  44294. "global.process.env.NODE_ENV": JSON.stringify(nodeEnv),
  44295. "globalThis.process.env.NODE_ENV": JSON.stringify(nodeEnv)
  44296. });
  44297. }
  44298. const importMetaKeys = {};
  44299. const importMetaEnvKeys = {};
  44300. const importMetaFallbackKeys = {};
  44301. if (isBuild) {
  44302. importMetaKeys["import.meta.hot"] = `undefined`;
  44303. for (const key in config.env) {
  44304. const val = JSON.stringify(config.env[key]);
  44305. importMetaKeys[`import.meta.env.${key}`] = val;
  44306. importMetaEnvKeys[key] = val;
  44307. }
  44308. importMetaKeys["import.meta.env.SSR"] = `undefined`;
  44309. importMetaFallbackKeys["import.meta.env"] = `undefined`;
  44310. }
  44311. const userDefine = {};
  44312. const userDefineEnv = {};
  44313. for (const key in config.define) {
  44314. userDefine[key] = handleDefineValue(config.define[key]);
  44315. if (isBuild && key.startsWith("import.meta.env.")) {
  44316. userDefineEnv[key.slice(16)] = config.define[key];
  44317. }
  44318. }
  44319. function generatePattern(ssr) {
  44320. const replaceProcessEnv = !ssr || config.ssr?.target === "webworker";
  44321. const define = {
  44322. ...replaceProcessEnv ? processEnv : {},
  44323. ...importMetaKeys,
  44324. ...userDefine,
  44325. ...importMetaFallbackKeys
  44326. };
  44327. if ("import.meta.env.SSR" in define) {
  44328. define["import.meta.env.SSR"] = ssr + "";
  44329. }
  44330. if ("import.meta.env" in define) {
  44331. define["import.meta.env"] = importMetaEnvMarker;
  44332. }
  44333. const importMetaEnvVal = serializeDefine({
  44334. ...importMetaEnvKeys,
  44335. SSR: ssr + "",
  44336. ...userDefineEnv
  44337. });
  44338. const patternKeys = Object.keys(userDefine);
  44339. if (replaceProcessEnv && Object.keys(processEnv).length) {
  44340. patternKeys.push("process.env");
  44341. }
  44342. if (Object.keys(importMetaKeys).length) {
  44343. patternKeys.push("import.meta.env", "import.meta.hot");
  44344. }
  44345. const pattern = patternKeys.length ? new RegExp(patternKeys.map(escapeRegex).join("|")) : null;
  44346. return [define, pattern, importMetaEnvVal];
  44347. }
  44348. const defaultPattern = generatePattern(false);
  44349. const ssrPattern = generatePattern(true);
  44350. return {
  44351. name: "vite:define",
  44352. async transform(code, id, options) {
  44353. const ssr = options?.ssr === true;
  44354. if (!ssr && !isBuild) {
  44355. return;
  44356. }
  44357. if (
  44358. // exclude html, css and static assets for performance
  44359. isHTMLRequest(id) || isCSSRequest(id) || isNonJsRequest(id) || config.assetsInclude(id)
  44360. ) {
  44361. return;
  44362. }
  44363. let [define, pattern, importMetaEnvVal] = ssr ? ssrPattern : defaultPattern;
  44364. if (!pattern) return;
  44365. pattern.lastIndex = 0;
  44366. if (!pattern.test(code)) return;
  44367. const hasDefineImportMetaEnv = "import.meta.env" in define;
  44368. let marker = importMetaEnvMarker;
  44369. if (hasDefineImportMetaEnv && code.includes(marker)) {
  44370. let i = 1;
  44371. do {
  44372. marker = importMetaEnvMarker + i++;
  44373. } while (code.includes(marker));
  44374. if (marker !== importMetaEnvMarker) {
  44375. define = { ...define, "import.meta.env": marker };
  44376. }
  44377. }
  44378. const result = await replaceDefine(code, id, define, config);
  44379. if (hasDefineImportMetaEnv) {
  44380. result.code = result.code.replaceAll(
  44381. getImportMetaEnvKeyRe(marker),
  44382. (m) => "undefined".padEnd(m.length)
  44383. );
  44384. if (result.code.includes(marker)) {
  44385. result.code = `const ${marker} = ${importMetaEnvVal};
  44386. ` + result.code;
  44387. if (result.map) {
  44388. const map = JSON.parse(result.map);
  44389. map.mappings = ";" + map.mappings;
  44390. result.map = map;
  44391. }
  44392. }
  44393. }
  44394. return result;
  44395. }
  44396. };
  44397. }
  44398. async function replaceDefine(code, id, define, config) {
  44399. const esbuildOptions = config.esbuild || {};
  44400. const result = await transform$1(code, {
  44401. loader: "js",
  44402. charset: esbuildOptions.charset ?? "utf8",
  44403. platform: "neutral",
  44404. define,
  44405. sourcefile: id,
  44406. sourcemap: config.command === "build" ? !!config.build.sourcemap : true
  44407. });
  44408. if (result.map.includes("<define:")) {
  44409. const originalMap = new TraceMap(result.map);
  44410. if (originalMap.sources.length >= 2) {
  44411. const sourceIndex = originalMap.sources.indexOf(id);
  44412. const decoded = decodedMap(originalMap);
  44413. decoded.sources = [id];
  44414. decoded.mappings = decoded.mappings.map(
  44415. (segments) => segments.filter((segment) => {
  44416. const index = segment[1];
  44417. segment[1] = 0;
  44418. return index === sourceIndex;
  44419. })
  44420. );
  44421. result.map = JSON.stringify(encodedMap(new TraceMap(decoded)));
  44422. }
  44423. }
  44424. return {
  44425. code: result.code,
  44426. map: result.map || null
  44427. };
  44428. }
  44429. function serializeDefine(define) {
  44430. let res = `{`;
  44431. const keys = Object.keys(define).sort();
  44432. for (let i = 0; i < keys.length; i++) {
  44433. const key = keys[i];
  44434. const val = define[key];
  44435. res += `${JSON.stringify(key)}: ${handleDefineValue(val)}`;
  44436. if (i !== keys.length - 1) {
  44437. res += `, `;
  44438. }
  44439. }
  44440. return res + `}`;
  44441. }
  44442. function handleDefineValue(value) {
  44443. if (typeof value === "undefined") return "undefined";
  44444. if (typeof value === "string") return value;
  44445. return JSON.stringify(value);
  44446. }
  44447. function getImportMetaEnvKeyRe(marker) {
  44448. let re = importMetaEnvKeyReCache.get(marker);
  44449. if (!re) {
  44450. re = new RegExp(`${marker}\\..+?\\b`, "g");
  44451. importMetaEnvKeyReCache.set(marker, re);
  44452. }
  44453. return re;
  44454. }
  44455. const normalizedClientEntry = normalizePath$3(CLIENT_ENTRY);
  44456. const normalizedEnvEntry = normalizePath$3(ENV_ENTRY);
  44457. function clientInjectionsPlugin(config) {
  44458. let injectConfigValues;
  44459. return {
  44460. name: "vite:client-inject",
  44461. async buildStart() {
  44462. const resolvedServerHostname = (await resolveHostname(config.server.host)).name;
  44463. const resolvedServerPort = config.server.port;
  44464. const devBase = config.base;
  44465. const serverHost = `${resolvedServerHostname}:${resolvedServerPort}${devBase}`;
  44466. let hmrConfig = config.server.hmr;
  44467. hmrConfig = isObject$1(hmrConfig) ? hmrConfig : void 0;
  44468. const host = hmrConfig?.host || null;
  44469. const protocol = hmrConfig?.protocol || null;
  44470. const timeout = hmrConfig?.timeout || 3e4;
  44471. const overlay = hmrConfig?.overlay !== false;
  44472. const isHmrServerSpecified = !!hmrConfig?.server;
  44473. const hmrConfigName = path$n.basename(config.configFile || "vite.config.js");
  44474. let port = hmrConfig?.clientPort || hmrConfig?.port || null;
  44475. if (config.server.middlewareMode && !isHmrServerSpecified) {
  44476. port ||= 24678;
  44477. }
  44478. let directTarget = hmrConfig?.host || resolvedServerHostname;
  44479. directTarget += `:${hmrConfig?.port || resolvedServerPort}`;
  44480. directTarget += devBase;
  44481. let hmrBase = devBase;
  44482. if (hmrConfig?.path) {
  44483. hmrBase = path$n.posix.join(hmrBase, hmrConfig.path);
  44484. }
  44485. const userDefine = {};
  44486. for (const key in config.define) {
  44487. if (!key.startsWith("import.meta.env.")) {
  44488. userDefine[key] = config.define[key];
  44489. }
  44490. }
  44491. const serializedDefines = serializeDefine(userDefine);
  44492. const modeReplacement = escapeReplacement(config.mode);
  44493. const baseReplacement = escapeReplacement(devBase);
  44494. const definesReplacement = () => serializedDefines;
  44495. const serverHostReplacement = escapeReplacement(serverHost);
  44496. const hmrProtocolReplacement = escapeReplacement(protocol);
  44497. const hmrHostnameReplacement = escapeReplacement(host);
  44498. const hmrPortReplacement = escapeReplacement(port);
  44499. const hmrDirectTargetReplacement = escapeReplacement(directTarget);
  44500. const hmrBaseReplacement = escapeReplacement(hmrBase);
  44501. const hmrTimeoutReplacement = escapeReplacement(timeout);
  44502. const hmrEnableOverlayReplacement = escapeReplacement(overlay);
  44503. const hmrConfigNameReplacement = escapeReplacement(hmrConfigName);
  44504. const wsTokenReplacement = escapeReplacement(config.webSocketToken);
  44505. injectConfigValues = (code) => {
  44506. 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).replace(`__WS_TOKEN__`, wsTokenReplacement);
  44507. };
  44508. },
  44509. async transform(code, id, options) {
  44510. if (id === normalizedClientEntry || id === normalizedEnvEntry) {
  44511. return injectConfigValues(code);
  44512. } else if (!options?.ssr && code.includes("process.env.NODE_ENV")) {
  44513. const nodeEnv = config.define?.["process.env.NODE_ENV"] || JSON.stringify(process.env.NODE_ENV || config.mode);
  44514. return await replaceDefine(
  44515. code,
  44516. id,
  44517. {
  44518. "process.env.NODE_ENV": nodeEnv,
  44519. "global.process.env.NODE_ENV": nodeEnv,
  44520. "globalThis.process.env.NODE_ENV": nodeEnv
  44521. },
  44522. config
  44523. );
  44524. }
  44525. }
  44526. };
  44527. }
  44528. function escapeReplacement(value) {
  44529. const jsonValue = JSON.stringify(value);
  44530. return () => jsonValue;
  44531. }
  44532. const wasmHelperId = "\0vite/wasm-helper.js";
  44533. const wasmInitRE = /(?<![?#].*)\.wasm\?init/;
  44534. const wasmHelper = async (opts = {}, url) => {
  44535. let result;
  44536. if (url.startsWith("data:")) {
  44537. const urlContent = url.replace(/^data:.*?base64,/, "");
  44538. let bytes;
  44539. if (typeof Buffer === "function" && typeof Buffer.from === "function") {
  44540. bytes = Buffer.from(urlContent, "base64");
  44541. } else if (typeof atob === "function") {
  44542. const binaryString = atob(urlContent);
  44543. bytes = new Uint8Array(binaryString.length);
  44544. for (let i = 0; i < binaryString.length; i++) {
  44545. bytes[i] = binaryString.charCodeAt(i);
  44546. }
  44547. } else {
  44548. throw new Error(
  44549. "Failed to decode base64-encoded data URL, Buffer and atob are not supported"
  44550. );
  44551. }
  44552. result = await WebAssembly.instantiate(bytes, opts);
  44553. } else {
  44554. const response = await fetch(url);
  44555. const contentType = response.headers.get("Content-Type") || "";
  44556. if ("instantiateStreaming" in WebAssembly && contentType.startsWith("application/wasm")) {
  44557. result = await WebAssembly.instantiateStreaming(response, opts);
  44558. } else {
  44559. const buffer = await response.arrayBuffer();
  44560. result = await WebAssembly.instantiate(buffer, opts);
  44561. }
  44562. }
  44563. return result.instance;
  44564. };
  44565. const wasmHelperCode = wasmHelper.toString();
  44566. const wasmHelperPlugin = (config) => {
  44567. return {
  44568. name: "vite:wasm-helper",
  44569. resolveId(id) {
  44570. if (id === wasmHelperId) {
  44571. return id;
  44572. }
  44573. },
  44574. async load(id) {
  44575. if (id === wasmHelperId) {
  44576. return `export default ${wasmHelperCode}`;
  44577. }
  44578. if (!wasmInitRE.test(id)) {
  44579. return;
  44580. }
  44581. const url = await fileToUrl$1(id, config, this);
  44582. return `
  44583. import initWasm from "${wasmHelperId}"
  44584. export default opts => initWasm(opts, ${JSON.stringify(url)})
  44585. `;
  44586. }
  44587. };
  44588. };
  44589. const wasmFallbackPlugin = () => {
  44590. return {
  44591. name: "vite:wasm-fallback",
  44592. async load(id) {
  44593. if (!id.endsWith(".wasm")) {
  44594. return;
  44595. }
  44596. throw new Error(
  44597. '"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.'
  44598. );
  44599. }
  44600. };
  44601. };
  44602. const workerOrSharedWorkerRE = /(?:\?|&)(worker|sharedworker)(?:&|$)/;
  44603. const workerFileRE = /(?:\?|&)worker_file&type=(\w+)(?:&|$)/;
  44604. const inlineRE$1 = /[?&]inline\b/;
  44605. const WORKER_FILE_ID = "worker_file";
  44606. const workerCache = /* @__PURE__ */ new WeakMap();
  44607. function saveEmitWorkerAsset(config, asset) {
  44608. const workerMap = workerCache.get(config.mainConfig || config);
  44609. workerMap.assets.set(asset.fileName, asset);
  44610. }
  44611. async function bundleWorkerEntry(config, id) {
  44612. const input = cleanUrl(id);
  44613. const newBundleChain = [...config.bundleChain, input];
  44614. if (config.bundleChain.includes(input)) {
  44615. throw new Error(
  44616. `Circular worker imports detected. Vite does not support it. Import chain: ${newBundleChain.map((id2) => prettifyUrl(id2, config.root)).join(" -> ")}`
  44617. );
  44618. }
  44619. const { rollup } = await import('rollup');
  44620. const { plugins, rollupOptions, format } = config.worker;
  44621. const bundle = await rollup({
  44622. ...rollupOptions,
  44623. input,
  44624. plugins: await plugins(newBundleChain),
  44625. onwarn(warning, warn) {
  44626. onRollupWarning(warning, warn, config);
  44627. },
  44628. preserveEntrySignatures: false
  44629. });
  44630. let chunk;
  44631. try {
  44632. const workerOutputConfig = config.worker.rollupOptions.output;
  44633. const workerConfig = workerOutputConfig ? Array.isArray(workerOutputConfig) ? workerOutputConfig[0] || {} : workerOutputConfig : {};
  44634. const {
  44635. output: [outputChunk, ...outputChunks]
  44636. } = await bundle.generate({
  44637. entryFileNames: path$n.posix.join(
  44638. config.build.assetsDir,
  44639. "[name]-[hash].js"
  44640. ),
  44641. chunkFileNames: path$n.posix.join(
  44642. config.build.assetsDir,
  44643. "[name]-[hash].js"
  44644. ),
  44645. assetFileNames: path$n.posix.join(
  44646. config.build.assetsDir,
  44647. "[name]-[hash].[ext]"
  44648. ),
  44649. ...workerConfig,
  44650. format,
  44651. sourcemap: config.build.sourcemap
  44652. });
  44653. chunk = outputChunk;
  44654. outputChunks.forEach((outputChunk2) => {
  44655. if (outputChunk2.type === "asset") {
  44656. saveEmitWorkerAsset(config, outputChunk2);
  44657. } else if (outputChunk2.type === "chunk") {
  44658. saveEmitWorkerAsset(config, {
  44659. fileName: outputChunk2.fileName,
  44660. originalFileName: null,
  44661. source: outputChunk2.code
  44662. });
  44663. }
  44664. });
  44665. } finally {
  44666. await bundle.close();
  44667. }
  44668. return emitSourcemapForWorkerEntry(config, chunk);
  44669. }
  44670. function emitSourcemapForWorkerEntry(config, chunk) {
  44671. const { map: sourcemap } = chunk;
  44672. if (sourcemap) {
  44673. if (config.build.sourcemap === "hidden" || config.build.sourcemap === true) {
  44674. const data = sourcemap.toString();
  44675. const mapFileName = chunk.fileName + ".map";
  44676. saveEmitWorkerAsset(config, {
  44677. fileName: mapFileName,
  44678. originalFileName: null,
  44679. source: data
  44680. });
  44681. }
  44682. }
  44683. return chunk;
  44684. }
  44685. const workerAssetUrlRE = /__VITE_WORKER_ASSET__([a-z\d]{8})__/g;
  44686. function encodeWorkerAssetFileName(fileName, workerCache2) {
  44687. const { fileNameHash } = workerCache2;
  44688. const hash = getHash(fileName);
  44689. if (!fileNameHash.get(hash)) {
  44690. fileNameHash.set(hash, fileName);
  44691. }
  44692. return `__VITE_WORKER_ASSET__${hash}__`;
  44693. }
  44694. async function workerFileToUrl(config, id) {
  44695. const workerMap = workerCache.get(config.mainConfig || config);
  44696. let fileName = workerMap.bundle.get(id);
  44697. if (!fileName) {
  44698. const outputChunk = await bundleWorkerEntry(config, id);
  44699. fileName = outputChunk.fileName;
  44700. saveEmitWorkerAsset(config, {
  44701. fileName,
  44702. originalFileName: null,
  44703. source: outputChunk.code
  44704. });
  44705. workerMap.bundle.set(id, fileName);
  44706. }
  44707. return encodeWorkerAssetFileName(fileName, workerMap);
  44708. }
  44709. function webWorkerPostPlugin() {
  44710. return {
  44711. name: "vite:worker-post",
  44712. resolveImportMeta(property, { format }) {
  44713. if (format === "iife") {
  44714. if (!property) {
  44715. return `{
  44716. url: self.location.href
  44717. }`;
  44718. }
  44719. if (property === "url") {
  44720. return "self.location.href";
  44721. }
  44722. }
  44723. return null;
  44724. }
  44725. };
  44726. }
  44727. function webWorkerPlugin(config) {
  44728. const isBuild = config.command === "build";
  44729. let server;
  44730. const isWorker = config.isWorker;
  44731. return {
  44732. name: "vite:worker",
  44733. configureServer(_server) {
  44734. server = _server;
  44735. },
  44736. buildStart() {
  44737. if (isWorker) {
  44738. return;
  44739. }
  44740. workerCache.set(config, {
  44741. assets: /* @__PURE__ */ new Map(),
  44742. bundle: /* @__PURE__ */ new Map(),
  44743. fileNameHash: /* @__PURE__ */ new Map()
  44744. });
  44745. },
  44746. load(id) {
  44747. if (isBuild && workerOrSharedWorkerRE.test(id)) {
  44748. return "";
  44749. }
  44750. },
  44751. shouldTransformCachedModule({ id }) {
  44752. if (isBuild && config.build.watch && workerOrSharedWorkerRE.test(id)) {
  44753. return true;
  44754. }
  44755. },
  44756. async transform(raw, id) {
  44757. const workerFileMatch = workerFileRE.exec(id);
  44758. if (workerFileMatch) {
  44759. const workerType2 = workerFileMatch[1];
  44760. let injectEnv = "";
  44761. const scriptPath = JSON.stringify(
  44762. path$n.posix.join(config.base, ENV_PUBLIC_PATH)
  44763. );
  44764. if (workerType2 === "classic") {
  44765. injectEnv = `importScripts(${scriptPath})
  44766. `;
  44767. } else if (workerType2 === "module") {
  44768. injectEnv = `import ${scriptPath}
  44769. `;
  44770. } else if (workerType2 === "ignore") {
  44771. if (isBuild) {
  44772. injectEnv = "";
  44773. } else if (server) {
  44774. const { moduleGraph } = server;
  44775. const module = moduleGraph.getModuleById(ENV_ENTRY);
  44776. injectEnv = module?.transformResult?.code || "";
  44777. }
  44778. }
  44779. if (injectEnv) {
  44780. const s = new MagicString(raw);
  44781. s.prepend(injectEnv + ";\n");
  44782. return {
  44783. code: s.toString(),
  44784. map: s.generateMap({ hires: "boundary" })
  44785. };
  44786. }
  44787. return;
  44788. }
  44789. const workerMatch = workerOrSharedWorkerRE.exec(id);
  44790. if (!workerMatch) return;
  44791. const { format } = config.worker;
  44792. const workerConstructor = workerMatch[1] === "sharedworker" ? "SharedWorker" : "Worker";
  44793. const workerType = isBuild ? format === "es" ? "module" : "classic" : "module";
  44794. const workerTypeOption = `{
  44795. ${workerType === "module" ? `type: "module",` : ""}
  44796. name: options?.name
  44797. }`;
  44798. let urlCode;
  44799. if (isBuild) {
  44800. if (isWorker && config.bundleChain.at(-1) === cleanUrl(id)) {
  44801. urlCode = "self.location.href";
  44802. } else if (inlineRE$1.test(id)) {
  44803. const chunk = await bundleWorkerEntry(config, id);
  44804. const encodedJs = `const encodedJs = "${Buffer.from(
  44805. chunk.code
  44806. ).toString("base64")}";`;
  44807. const code = (
  44808. // Using blob URL for SharedWorker results in multiple instances of a same worker
  44809. workerConstructor === "Worker" ? `${encodedJs}
  44810. const decodeBase64 = (base64) => Uint8Array.from(atob(base64), c => c.charCodeAt(0));
  44811. const blob = typeof self !== "undefined" && self.Blob && new Blob([${workerType === "classic" ? "" : (
  44812. // `URL` is always available, in `Worker[type="module"]`
  44813. `'URL.revokeObjectURL(import.meta.url);',`
  44814. )}decodeBase64(encodedJs)], { type: "text/javascript;charset=utf-8" });
  44815. export default function WorkerWrapper(options) {
  44816. let objURL;
  44817. try {
  44818. objURL = blob && (self.URL || self.webkitURL).createObjectURL(blob);
  44819. if (!objURL) throw ''
  44820. const worker = new ${workerConstructor}(objURL, ${workerTypeOption});
  44821. worker.addEventListener("error", () => {
  44822. (self.URL || self.webkitURL).revokeObjectURL(objURL);
  44823. });
  44824. return worker;
  44825. } catch(e) {
  44826. return new ${workerConstructor}(
  44827. "data:text/javascript;base64," + encodedJs,
  44828. ${workerTypeOption}
  44829. );
  44830. }${// For module workers, we should not revoke the URL until the worker runs,
  44831. // otherwise the worker fails to run
  44832. workerType === "classic" ? ` finally {
  44833. objURL && (self.URL || self.webkitURL).revokeObjectURL(objURL);
  44834. }` : ""}
  44835. }` : `${encodedJs}
  44836. export default function WorkerWrapper(options) {
  44837. return new ${workerConstructor}(
  44838. "data:text/javascript;base64," + encodedJs,
  44839. ${workerTypeOption}
  44840. );
  44841. }
  44842. `
  44843. );
  44844. return {
  44845. code,
  44846. // Empty sourcemap to suppress Rollup warning
  44847. map: { mappings: "" }
  44848. };
  44849. } else {
  44850. urlCode = JSON.stringify(await workerFileToUrl(config, id));
  44851. }
  44852. } else {
  44853. let url = await fileToUrl$1(cleanUrl(id), config, this);
  44854. url = injectQuery(url, `${WORKER_FILE_ID}&type=${workerType}`);
  44855. urlCode = JSON.stringify(url);
  44856. }
  44857. if (urlRE$1.test(id)) {
  44858. return {
  44859. code: `export default ${urlCode}`,
  44860. map: { mappings: "" }
  44861. // Empty sourcemap to suppress Rollup warning
  44862. };
  44863. }
  44864. return {
  44865. code: `export default function WorkerWrapper(options) {
  44866. return new ${workerConstructor}(
  44867. ${urlCode},
  44868. ${workerTypeOption}
  44869. );
  44870. }`,
  44871. map: { mappings: "" }
  44872. // Empty sourcemap to suppress Rollup warning
  44873. };
  44874. },
  44875. renderChunk(code, chunk, outputOptions) {
  44876. let s;
  44877. const result = () => {
  44878. return s && {
  44879. code: s.toString(),
  44880. map: config.build.sourcemap ? s.generateMap({ hires: "boundary" }) : null
  44881. };
  44882. };
  44883. workerAssetUrlRE.lastIndex = 0;
  44884. if (workerAssetUrlRE.test(code)) {
  44885. const toRelativeRuntime = createToImportMetaURLBasedRelativeRuntime(
  44886. outputOptions.format,
  44887. config.isWorker
  44888. );
  44889. let match;
  44890. s = new MagicString(code);
  44891. workerAssetUrlRE.lastIndex = 0;
  44892. const workerMap = workerCache.get(config.mainConfig || config);
  44893. const { fileNameHash } = workerMap;
  44894. while (match = workerAssetUrlRE.exec(code)) {
  44895. const [full, hash] = match;
  44896. const filename = fileNameHash.get(hash);
  44897. const replacement = toOutputFilePathInJS(
  44898. filename,
  44899. "asset",
  44900. chunk.fileName,
  44901. "js",
  44902. config,
  44903. toRelativeRuntime
  44904. );
  44905. const replacementString = typeof replacement === "string" ? JSON.stringify(encodeURIPath(replacement)).slice(1, -1) : `"+${replacement.runtime}+"`;
  44906. s.update(match.index, match.index + full.length, replacementString);
  44907. }
  44908. }
  44909. return result();
  44910. },
  44911. generateBundle(opts, bundle) {
  44912. if (opts.__vite_skip_asset_emit__ || isWorker) {
  44913. return;
  44914. }
  44915. const workerMap = workerCache.get(config);
  44916. workerMap.assets.forEach((asset) => {
  44917. const duplicateAsset = bundle[asset.fileName];
  44918. if (duplicateAsset) {
  44919. const content = duplicateAsset.type === "asset" ? duplicateAsset.source : duplicateAsset.code;
  44920. if (isSameContent(content, asset.source)) {
  44921. return;
  44922. }
  44923. }
  44924. this.emitFile({
  44925. type: "asset",
  44926. fileName: asset.fileName,
  44927. originalFileName: asset.originalFileName,
  44928. source: asset.source
  44929. });
  44930. });
  44931. workerMap.assets.clear();
  44932. }
  44933. };
  44934. }
  44935. function isSameContent(a, b) {
  44936. if (typeof a === "string") {
  44937. if (typeof b === "string") {
  44938. return a === b;
  44939. }
  44940. return Buffer.from(a).equals(b);
  44941. }
  44942. return Buffer.from(b).equals(a);
  44943. }
  44944. function preAliasPlugin(config) {
  44945. const findPatterns = getAliasPatterns(config.resolve.alias);
  44946. const isConfiguredAsExternal = createIsConfiguredAsSsrExternal(config);
  44947. const isBuild = config.command === "build";
  44948. const fsUtils = getFsUtils(config);
  44949. return {
  44950. name: "vite:pre-alias",
  44951. async resolveId(id, importer, options) {
  44952. const ssr = options?.ssr === true;
  44953. const depsOptimizer = !isBuild && getDepsOptimizer(config, ssr);
  44954. if (importer && depsOptimizer && bareImportRE.test(id) && !options?.scan && id !== "@vite/client" && id !== "@vite/env") {
  44955. if (findPatterns.find((pattern) => matches(pattern, id))) {
  44956. const optimizedId = await tryOptimizedResolve(
  44957. depsOptimizer,
  44958. id,
  44959. importer,
  44960. config.resolve.preserveSymlinks,
  44961. config.packageCache
  44962. );
  44963. if (optimizedId) {
  44964. return optimizedId;
  44965. }
  44966. if (depsOptimizer.options.noDiscovery) {
  44967. return;
  44968. }
  44969. const resolved = await this.resolve(id, importer, {
  44970. ...options,
  44971. custom: { ...options.custom, "vite:pre-alias": true }
  44972. });
  44973. if (resolved && !depsOptimizer.isOptimizedDepFile(resolved.id)) {
  44974. const optimizeDeps = depsOptimizer.options;
  44975. const resolvedId = cleanUrl(resolved.id);
  44976. const isVirtual = resolvedId === id || resolvedId.includes("\0");
  44977. 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))) {
  44978. const optimizedInfo = depsOptimizer.registerMissingImport(
  44979. id,
  44980. resolvedId
  44981. );
  44982. return { id: depsOptimizer.getOptimizedDepId(optimizedInfo) };
  44983. }
  44984. }
  44985. return resolved;
  44986. }
  44987. }
  44988. }
  44989. };
  44990. }
  44991. function optimizeAliasReplacementForSSR(id, optimizeDeps) {
  44992. if (optimizeDeps.include?.includes(id)) {
  44993. return true;
  44994. }
  44995. return false;
  44996. }
  44997. function matches(pattern, importee) {
  44998. if (pattern instanceof RegExp) {
  44999. return pattern.test(importee);
  45000. }
  45001. if (importee.length < pattern.length) {
  45002. return false;
  45003. }
  45004. if (importee === pattern) {
  45005. return true;
  45006. }
  45007. return importee.startsWith(withTrailingSlash(pattern));
  45008. }
  45009. function getAliasPatterns(entries) {
  45010. if (!entries) {
  45011. return [];
  45012. }
  45013. if (Array.isArray(entries)) {
  45014. return entries.map((entry) => entry.find);
  45015. }
  45016. return Object.entries(entries).map(([find]) => find);
  45017. }
  45018. function getAliasPatternMatcher(entries) {
  45019. const patterns = getAliasPatterns(entries);
  45020. return (importee) => patterns.some((pattern) => matches(pattern, importee));
  45021. }
  45022. function err(e, pos) {
  45023. const error = new Error(e);
  45024. error.pos = pos;
  45025. return error;
  45026. }
  45027. function parseWorkerOptions(rawOpts, optsStartIndex) {
  45028. let opts = {};
  45029. try {
  45030. opts = evalValue(rawOpts);
  45031. } catch {
  45032. throw err(
  45033. "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.",
  45034. optsStartIndex
  45035. );
  45036. }
  45037. if (opts == null) {
  45038. return {};
  45039. }
  45040. if (typeof opts !== "object") {
  45041. throw err(
  45042. `Expected worker options to be an object, got ${typeof opts}`,
  45043. optsStartIndex
  45044. );
  45045. }
  45046. return opts;
  45047. }
  45048. function getWorkerType(raw, clean, i) {
  45049. const commaIndex = clean.indexOf(",", i);
  45050. if (commaIndex === -1) {
  45051. return "classic";
  45052. }
  45053. const endIndex = clean.indexOf(")", i);
  45054. if (commaIndex > endIndex) {
  45055. return "classic";
  45056. }
  45057. const workerOptString = raw.substring(commaIndex + 1, endIndex).replace(/\}[\s\S]*,/g, "}");
  45058. const hasViteIgnore = hasViteIgnoreRE.test(workerOptString);
  45059. if (hasViteIgnore) {
  45060. return "ignore";
  45061. }
  45062. const cleanWorkerOptString = clean.substring(commaIndex + 1, endIndex).trim();
  45063. if (!cleanWorkerOptString.length) {
  45064. return "classic";
  45065. }
  45066. const workerOpts = parseWorkerOptions(workerOptString, commaIndex + 1);
  45067. if (workerOpts.type && (workerOpts.type === "module" || workerOpts.type === "classic")) {
  45068. return workerOpts.type;
  45069. }
  45070. return "classic";
  45071. }
  45072. function isIncludeWorkerImportMetaUrl(code) {
  45073. if ((code.includes("new Worker") || code.includes("new SharedWorker")) && code.includes("new URL") && code.includes(`import.meta.url`)) {
  45074. return true;
  45075. }
  45076. return false;
  45077. }
  45078. function workerImportMetaUrlPlugin(config) {
  45079. const isBuild = config.command === "build";
  45080. let workerResolver;
  45081. const fsResolveOptions = {
  45082. ...config.resolve,
  45083. root: config.root,
  45084. isProduction: config.isProduction,
  45085. isBuild: config.command === "build",
  45086. packageCache: config.packageCache,
  45087. ssrConfig: config.ssr,
  45088. asSrc: true
  45089. };
  45090. return {
  45091. name: "vite:worker-import-meta-url",
  45092. shouldTransformCachedModule({ code }) {
  45093. if (isBuild && config.build.watch && isIncludeWorkerImportMetaUrl(code)) {
  45094. return true;
  45095. }
  45096. },
  45097. async transform(code, id, options) {
  45098. if (!options?.ssr && isIncludeWorkerImportMetaUrl(code)) {
  45099. let s;
  45100. const cleanString = stripLiteral(code);
  45101. const workerImportMetaUrlRE = /\bnew\s+(?:Worker|SharedWorker)\s*\(\s*(new\s+URL\s*\(\s*('[^']+'|"[^"]+"|`[^`]+`)\s*,\s*import\.meta\.url\s*\))/dg;
  45102. let match;
  45103. while (match = workerImportMetaUrlRE.exec(cleanString)) {
  45104. const [[, endIndex], [expStart, expEnd], [urlStart, urlEnd]] = match.indices;
  45105. const rawUrl = code.slice(urlStart, urlEnd);
  45106. if (rawUrl[0] === "`" && rawUrl.includes("${")) {
  45107. this.error(
  45108. `\`new URL(url, import.meta.url)\` is not supported in dynamic template string.`,
  45109. expStart
  45110. );
  45111. }
  45112. s ||= new MagicString(code);
  45113. const workerType = getWorkerType(code, cleanString, endIndex);
  45114. const url = rawUrl.slice(1, -1);
  45115. let file;
  45116. if (url[0] === ".") {
  45117. file = path$n.resolve(path$n.dirname(id), url);
  45118. file = tryFsResolve(file, fsResolveOptions) ?? file;
  45119. } else {
  45120. workerResolver ??= config.createResolver({
  45121. extensions: [],
  45122. tryIndex: false,
  45123. preferRelative: true
  45124. });
  45125. file = await workerResolver(url, id);
  45126. file ??= url[0] === "/" ? slash$1(path$n.join(config.publicDir, url)) : slash$1(path$n.resolve(path$n.dirname(id), url));
  45127. }
  45128. if (isBuild && config.isWorker && config.bundleChain.at(-1) === cleanUrl(file)) {
  45129. s.update(expStart, expEnd, "self.location.href");
  45130. } else {
  45131. let builtUrl;
  45132. if (isBuild) {
  45133. builtUrl = await workerFileToUrl(config, file);
  45134. } else {
  45135. builtUrl = await fileToUrl$1(cleanUrl(file), config, this);
  45136. builtUrl = injectQuery(
  45137. builtUrl,
  45138. `${WORKER_FILE_ID}&type=${workerType}`
  45139. );
  45140. }
  45141. s.update(
  45142. expStart,
  45143. expEnd,
  45144. `new URL(/* @vite-ignore */ ${JSON.stringify(builtUrl)}, import.meta.url)`
  45145. );
  45146. }
  45147. }
  45148. if (s) {
  45149. return transformStableResult(s, id, config);
  45150. }
  45151. return null;
  45152. }
  45153. }
  45154. };
  45155. }
  45156. function assetImportMetaUrlPlugin(config) {
  45157. const { publicDir } = config;
  45158. let assetResolver;
  45159. const fsResolveOptions = {
  45160. ...config.resolve,
  45161. root: config.root,
  45162. isProduction: config.isProduction,
  45163. isBuild: config.command === "build",
  45164. packageCache: config.packageCache,
  45165. ssrConfig: config.ssr,
  45166. asSrc: true
  45167. };
  45168. return {
  45169. name: "vite:asset-import-meta-url",
  45170. async transform(code, id, options) {
  45171. if (!options?.ssr && id !== preloadHelperId && id !== CLIENT_ENTRY && code.includes("new URL") && code.includes(`import.meta.url`)) {
  45172. let s;
  45173. const assetImportMetaUrlRE = /\bnew\s+URL\s*\(\s*('[^']+'|"[^"]+"|`[^`]+`)\s*,\s*import\.meta\.url\s*(?:,\s*)?\)/dg;
  45174. const cleanString = stripLiteral(code);
  45175. let match;
  45176. while (match = assetImportMetaUrlRE.exec(cleanString)) {
  45177. const [[startIndex, endIndex], [urlStart, urlEnd]] = match.indices;
  45178. if (hasViteIgnoreRE.test(code.slice(startIndex, urlStart))) continue;
  45179. const rawUrl = code.slice(urlStart, urlEnd);
  45180. if (!s) s = new MagicString(code);
  45181. if (rawUrl[0] === "`" && rawUrl.includes("${")) {
  45182. const queryDelimiterIndex = getQueryDelimiterIndex(rawUrl);
  45183. const hasQueryDelimiter = queryDelimiterIndex !== -1;
  45184. const pureUrl = hasQueryDelimiter ? rawUrl.slice(0, queryDelimiterIndex) + "`" : rawUrl;
  45185. const queryString = hasQueryDelimiter ? rawUrl.slice(queryDelimiterIndex, -1) : "";
  45186. const ast = this.parse(pureUrl);
  45187. const templateLiteral = ast.body[0].expression;
  45188. if (templateLiteral.expressions.length) {
  45189. const pattern = buildGlobPattern(templateLiteral);
  45190. if (pattern.startsWith("**")) {
  45191. continue;
  45192. }
  45193. const globOptions = {
  45194. eager: true,
  45195. import: "default",
  45196. // A hack to allow 'as' & 'query' exist at the same time
  45197. query: injectQuery(queryString, "url")
  45198. };
  45199. s.update(
  45200. startIndex,
  45201. endIndex,
  45202. `new URL((import.meta.glob(${JSON.stringify(
  45203. pattern
  45204. )}, ${JSON.stringify(
  45205. globOptions
  45206. )}))[${pureUrl}], import.meta.url)`
  45207. );
  45208. continue;
  45209. }
  45210. }
  45211. const url = rawUrl.slice(1, -1);
  45212. let file;
  45213. if (url[0] === ".") {
  45214. file = slash$1(path$n.resolve(path$n.dirname(id), url));
  45215. file = tryFsResolve(file, fsResolveOptions) ?? file;
  45216. } else {
  45217. assetResolver ??= config.createResolver({
  45218. extensions: [],
  45219. mainFields: [],
  45220. tryIndex: false,
  45221. preferRelative: true
  45222. });
  45223. file = await assetResolver(url, id);
  45224. file ??= url[0] === "/" ? slash$1(path$n.join(publicDir, url)) : slash$1(path$n.resolve(path$n.dirname(id), url));
  45225. }
  45226. let builtUrl;
  45227. if (file) {
  45228. try {
  45229. if (publicDir && isParentDirectory(publicDir, file)) {
  45230. const publicPath = "/" + path$n.posix.relative(publicDir, file);
  45231. builtUrl = await fileToUrl$1(publicPath, config, this);
  45232. } else {
  45233. builtUrl = await fileToUrl$1(file, config, this);
  45234. }
  45235. } catch {
  45236. }
  45237. }
  45238. if (!builtUrl) {
  45239. const rawExp = code.slice(startIndex, endIndex);
  45240. config.logger.warnOnce(
  45241. `
  45242. ${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.`
  45243. );
  45244. builtUrl = url;
  45245. }
  45246. s.update(
  45247. startIndex,
  45248. endIndex,
  45249. `new URL(${JSON.stringify(builtUrl)}, import.meta.url)`
  45250. );
  45251. }
  45252. if (s) {
  45253. return transformStableResult(s, id, config);
  45254. }
  45255. }
  45256. return null;
  45257. }
  45258. };
  45259. }
  45260. function buildGlobPattern(ast) {
  45261. let pattern = "";
  45262. let lastElementIndex = -1;
  45263. for (const exp of ast.expressions) {
  45264. for (let i = lastElementIndex + 1; i < ast.quasis.length; i++) {
  45265. const el = ast.quasis[i];
  45266. if (el.end < exp.start) {
  45267. pattern += el.value.raw;
  45268. lastElementIndex = i;
  45269. }
  45270. }
  45271. pattern += "**";
  45272. }
  45273. for (let i = lastElementIndex + 1; i < ast.quasis.length; i++) {
  45274. pattern += ast.quasis[i].value.raw;
  45275. }
  45276. return pattern;
  45277. }
  45278. function getQueryDelimiterIndex(rawUrl) {
  45279. let bracketsStack = 0;
  45280. for (let i = 0; i < rawUrl.length; i++) {
  45281. if (rawUrl[i] === "{") {
  45282. bracketsStack++;
  45283. } else if (rawUrl[i] === "}") {
  45284. bracketsStack--;
  45285. } else if (rawUrl[i] === "?" && bracketsStack === 0) {
  45286. return i;
  45287. }
  45288. }
  45289. return -1;
  45290. }
  45291. function metadataPlugin() {
  45292. return {
  45293. name: "vite:build-metadata",
  45294. async renderChunk(_code, chunk) {
  45295. chunk.viteMetadata = {
  45296. importedAssets: /* @__PURE__ */ new Set(),
  45297. importedCss: /* @__PURE__ */ new Set()
  45298. };
  45299. return null;
  45300. }
  45301. };
  45302. }
  45303. // Astring is a tiny and fast JavaScript code generator from an ESTree-compliant AST.
  45304. //
  45305. // Astring was written by David Bonnet and released under an MIT license.
  45306. //
  45307. // The Git repository for Astring is available at:
  45308. // https://github.com/davidbonnet/astring.git
  45309. //
  45310. // Please use the GitHub bug tracker to report issues:
  45311. // https://github.com/davidbonnet/astring/issues
  45312. /* c8 ignore if */
  45313. if (!String.prototype.repeat) {
  45314. /* c8 ignore next */
  45315. throw new Error(
  45316. 'String.prototype.repeat is undefined, see https://github.com/davidbonnet/astring#installation',
  45317. )
  45318. }
  45319. /* c8 ignore if */
  45320. if (!String.prototype.endsWith) {
  45321. /* c8 ignore next */
  45322. throw new Error(
  45323. 'String.prototype.endsWith is undefined, see https://github.com/davidbonnet/astring#installation',
  45324. )
  45325. }
  45326. class VariableDynamicImportError extends Error {}
  45327. /* eslint-disable-next-line no-template-curly-in-string */
  45328. const example = 'For example: import(`./foo/${bar}.js`).';
  45329. function sanitizeString(str) {
  45330. if (str === '') return str;
  45331. if (str.includes('*')) {
  45332. throw new VariableDynamicImportError('A dynamic import cannot contain * characters.');
  45333. }
  45334. return glob.escapePath(str);
  45335. }
  45336. function templateLiteralToGlob(node) {
  45337. let glob = '';
  45338. for (let i = 0; i < node.quasis.length; i += 1) {
  45339. glob += sanitizeString(node.quasis[i].value.raw);
  45340. if (node.expressions[i]) {
  45341. glob += expressionToGlob(node.expressions[i]);
  45342. }
  45343. }
  45344. return glob;
  45345. }
  45346. function callExpressionToGlob(node) {
  45347. const { callee } = node;
  45348. if (
  45349. callee.type === 'MemberExpression' &&
  45350. callee.property.type === 'Identifier' &&
  45351. callee.property.name === 'concat'
  45352. ) {
  45353. return `${expressionToGlob(callee.object)}${node.arguments.map(expressionToGlob).join('')}`;
  45354. }
  45355. return '*';
  45356. }
  45357. function binaryExpressionToGlob(node) {
  45358. if (node.operator !== '+') {
  45359. throw new VariableDynamicImportError(`${node.operator} operator is not supported.`);
  45360. }
  45361. return `${expressionToGlob(node.left)}${expressionToGlob(node.right)}`;
  45362. }
  45363. function expressionToGlob(node) {
  45364. switch (node.type) {
  45365. case 'TemplateLiteral':
  45366. return templateLiteralToGlob(node);
  45367. case 'CallExpression':
  45368. return callExpressionToGlob(node);
  45369. case 'BinaryExpression':
  45370. return binaryExpressionToGlob(node);
  45371. case 'Literal': {
  45372. return sanitizeString(node.value);
  45373. }
  45374. default:
  45375. return '*';
  45376. }
  45377. }
  45378. const defaultProtocol = 'file:';
  45379. const ignoredProtocols = ['data:', 'http:', 'https:'];
  45380. function shouldIgnore(glob) {
  45381. const containsAsterisk = glob.includes('*');
  45382. const globURL = new URL(glob, defaultProtocol);
  45383. const containsIgnoredProtocol = ignoredProtocols.some(
  45384. (ignoredProtocol) => ignoredProtocol === globURL.protocol
  45385. );
  45386. return !containsAsterisk || containsIgnoredProtocol;
  45387. }
  45388. function dynamicImportToGlob(node, sourceString) {
  45389. let glob = expressionToGlob(node);
  45390. if (shouldIgnore(glob)) {
  45391. return null;
  45392. }
  45393. glob = glob.replace(/\*\*/g, '*');
  45394. if (glob.startsWith('*')) {
  45395. throw new VariableDynamicImportError(
  45396. `invalid import "${sourceString}". It cannot be statically analyzed. Variable dynamic imports must start with ./ and be limited to a specific directory. ${example}`
  45397. );
  45398. }
  45399. if (glob.startsWith('/')) {
  45400. throw new VariableDynamicImportError(
  45401. `invalid import "${sourceString}". Variable absolute imports are not supported, imports must start with ./ in the static part of the import. ${example}`
  45402. );
  45403. }
  45404. if (!glob.startsWith('./') && !glob.startsWith('../')) {
  45405. throw new VariableDynamicImportError(
  45406. `invalid import "${sourceString}". Variable bare imports are not supported, imports must start with ./ in the static part of the import. ${example}`
  45407. );
  45408. }
  45409. // Disallow ./*.ext
  45410. const ownDirectoryStarExtension = /^\.\/\*\.[\w]+$/;
  45411. if (ownDirectoryStarExtension.test(glob)) {
  45412. throw new VariableDynamicImportError(
  45413. `${
  45414. `invalid import "${sourceString}". Variable imports cannot import their own directory, ` +
  45415. 'place imports in a separate directory or make the import filename more specific. '
  45416. }${example}`
  45417. );
  45418. }
  45419. if (require$$0$4.extname(glob) === '') {
  45420. throw new VariableDynamicImportError(
  45421. `invalid import "${sourceString}". A file extension must be included in the static part of the import. ${example}`
  45422. );
  45423. }
  45424. return glob;
  45425. }
  45426. const dynamicImportHelperId = "\0vite/dynamic-import-helper.js";
  45427. const relativePathRE = /^\.{1,2}\//;
  45428. const hasDynamicImportRE = /\bimport\s*[(/]/;
  45429. const dynamicImportHelper = (glob, path, segs) => {
  45430. const v = glob[path];
  45431. if (v) {
  45432. return typeof v === "function" ? v() : Promise.resolve(v);
  45433. }
  45434. return new Promise((_, reject) => {
  45435. (typeof queueMicrotask === "function" ? queueMicrotask : setTimeout)(
  45436. reject.bind(
  45437. null,
  45438. new Error(
  45439. "Unknown variable dynamic import: " + path + (path.split("/").length !== segs ? ". Note that variables only represent file names one level deep." : "")
  45440. )
  45441. )
  45442. );
  45443. });
  45444. };
  45445. function parseDynamicImportPattern(strings) {
  45446. const filename = strings.slice(1, -1);
  45447. const ast = parseAst(strings).body[0].expression;
  45448. const userPatternQuery = dynamicImportToGlob(ast, filename);
  45449. if (!userPatternQuery) {
  45450. return null;
  45451. }
  45452. const [userPattern] = userPatternQuery.split(
  45453. // ? is escaped on posix OS
  45454. requestQueryMaybeEscapedSplitRE,
  45455. 2
  45456. );
  45457. let [rawPattern, search] = filename.split(requestQuerySplitRE, 2);
  45458. let globParams = null;
  45459. if (search) {
  45460. search = "?" + search;
  45461. if (workerOrSharedWorkerRE.test(search) || urlRE$1.test(search) || rawRE$1.test(search)) {
  45462. globParams = {
  45463. query: search,
  45464. import: "*"
  45465. };
  45466. } else {
  45467. globParams = {
  45468. query: search
  45469. };
  45470. }
  45471. }
  45472. return {
  45473. globParams,
  45474. userPattern,
  45475. rawPattern
  45476. };
  45477. }
  45478. async function transformDynamicImport(importSource, importer, resolve, root) {
  45479. if (importSource[1] !== "." && importSource[1] !== "/") {
  45480. const resolvedFileName = await resolve(importSource.slice(1, -1), importer);
  45481. if (!resolvedFileName) {
  45482. return null;
  45483. }
  45484. const relativeFileName = normalizePath$3(
  45485. posix$1.relative(
  45486. posix$1.dirname(normalizePath$3(importer)),
  45487. normalizePath$3(resolvedFileName)
  45488. )
  45489. );
  45490. importSource = "`" + (relativeFileName[0] === "." ? "" : "./") + relativeFileName + "`";
  45491. }
  45492. const dynamicImportPattern = parseDynamicImportPattern(importSource);
  45493. if (!dynamicImportPattern) {
  45494. return null;
  45495. }
  45496. const { globParams, rawPattern, userPattern } = dynamicImportPattern;
  45497. const params = globParams ? `, ${JSON.stringify(globParams)}` : "";
  45498. let newRawPattern = posix$1.relative(
  45499. posix$1.dirname(importer),
  45500. await toAbsoluteGlob(rawPattern, root, importer, resolve)
  45501. );
  45502. if (!relativePathRE.test(newRawPattern)) {
  45503. newRawPattern = `./${newRawPattern}`;
  45504. }
  45505. const exp = `(import.meta.glob(${JSON.stringify(userPattern)}${params}))`;
  45506. return {
  45507. rawPattern: newRawPattern,
  45508. pattern: userPattern,
  45509. glob: exp
  45510. };
  45511. }
  45512. function dynamicImportVarsPlugin(config) {
  45513. const resolve = config.createResolver({
  45514. preferRelative: true,
  45515. tryIndex: false,
  45516. extensions: []
  45517. });
  45518. const { include, exclude, warnOnError } = config.build.dynamicImportVarsOptions;
  45519. const filter = createFilter(include, exclude);
  45520. return {
  45521. name: "vite:dynamic-import-vars",
  45522. resolveId(id) {
  45523. if (id === dynamicImportHelperId) {
  45524. return id;
  45525. }
  45526. },
  45527. load(id) {
  45528. if (id === dynamicImportHelperId) {
  45529. return "export default " + dynamicImportHelper.toString();
  45530. }
  45531. },
  45532. async transform(source, importer) {
  45533. if (!filter(importer) || importer === CLIENT_ENTRY || !hasDynamicImportRE.test(source)) {
  45534. return;
  45535. }
  45536. await init;
  45537. let imports = [];
  45538. try {
  45539. imports = parse$d(source)[0];
  45540. } catch (e) {
  45541. return null;
  45542. }
  45543. if (!imports.length) {
  45544. return null;
  45545. }
  45546. let s;
  45547. let needDynamicImportHelper = false;
  45548. for (let index = 0; index < imports.length; index++) {
  45549. const {
  45550. s: start,
  45551. e: end,
  45552. ss: expStart,
  45553. se: expEnd,
  45554. d: dynamicIndex
  45555. } = imports[index];
  45556. if (dynamicIndex === -1 || source[start] !== "`") {
  45557. continue;
  45558. }
  45559. if (hasViteIgnoreRE.test(source.slice(expStart, expEnd))) {
  45560. continue;
  45561. }
  45562. s ||= new MagicString(source);
  45563. let result;
  45564. try {
  45565. result = await transformDynamicImport(
  45566. source.slice(start, end),
  45567. importer,
  45568. resolve,
  45569. config.root
  45570. );
  45571. } catch (error) {
  45572. if (warnOnError) {
  45573. this.warn(error);
  45574. } else {
  45575. this.error(error);
  45576. }
  45577. }
  45578. if (!result) {
  45579. continue;
  45580. }
  45581. const { rawPattern, glob } = result;
  45582. needDynamicImportHelper = true;
  45583. s.overwrite(
  45584. expStart,
  45585. expEnd,
  45586. `__variableDynamicImportRuntimeHelper(${glob}, \`${rawPattern}\`, ${rawPattern.split("/").length})`
  45587. );
  45588. }
  45589. if (s) {
  45590. if (needDynamicImportHelper) {
  45591. s.prepend(
  45592. `import __variableDynamicImportRuntimeHelper from "${dynamicImportHelperId}";`
  45593. );
  45594. }
  45595. return transformStableResult(s, importer, config);
  45596. }
  45597. }
  45598. };
  45599. }
  45600. async function resolvePlugins(config, prePlugins, normalPlugins, postPlugins) {
  45601. const isBuild = config.command === "build";
  45602. const isWorker = config.isWorker;
  45603. const buildPlugins = isBuild ? await (await Promise.resolve().then(function () { return build$1; })).resolveBuildPlugins(config) : { pre: [], post: [] };
  45604. const { modulePreload } = config.build;
  45605. const depsOptimizerEnabled = !isBuild && (isDepsOptimizerEnabled(config, false) || isDepsOptimizerEnabled(config, true));
  45606. return [
  45607. depsOptimizerEnabled ? optimizedDepsPlugin(config) : null,
  45608. isBuild ? metadataPlugin() : null,
  45609. !isWorker ? watchPackageDataPlugin(config.packageCache) : null,
  45610. preAliasPlugin(config),
  45611. alias$1({
  45612. entries: config.resolve.alias,
  45613. customResolver: viteAliasCustomResolver
  45614. }),
  45615. ...prePlugins,
  45616. modulePreload !== false && modulePreload.polyfill ? modulePreloadPolyfillPlugin(config) : null,
  45617. resolvePlugin({
  45618. ...config.resolve,
  45619. root: config.root,
  45620. isProduction: config.isProduction,
  45621. isBuild,
  45622. packageCache: config.packageCache,
  45623. ssrConfig: config.ssr,
  45624. asSrc: true,
  45625. fsUtils: getFsUtils(config),
  45626. getDepsOptimizer: isBuild ? void 0 : (ssr) => getDepsOptimizer(config, ssr),
  45627. shouldExternalize: isBuild && config.build.ssr ? (id, importer) => shouldExternalizeForSSR(id, importer, config) : void 0
  45628. }),
  45629. htmlInlineProxyPlugin(config),
  45630. cssPlugin(config),
  45631. config.esbuild !== false ? esbuildPlugin(config) : null,
  45632. jsonPlugin(
  45633. {
  45634. namedExports: true,
  45635. ...config.json
  45636. },
  45637. isBuild
  45638. ),
  45639. wasmHelperPlugin(config),
  45640. webWorkerPlugin(config),
  45641. assetPlugin(config),
  45642. ...normalPlugins,
  45643. wasmFallbackPlugin(),
  45644. definePlugin(config),
  45645. cssPostPlugin(config),
  45646. isBuild && buildHtmlPlugin(config),
  45647. workerImportMetaUrlPlugin(config),
  45648. assetImportMetaUrlPlugin(config),
  45649. ...buildPlugins.pre,
  45650. dynamicImportVarsPlugin(config),
  45651. importGlobPlugin(config),
  45652. ...postPlugins,
  45653. ...buildPlugins.post,
  45654. // internal server-only plugins are always applied after everything else
  45655. ...isBuild ? [] : [
  45656. clientInjectionsPlugin(config),
  45657. cssAnalysisPlugin(config),
  45658. importAnalysisPlugin(config)
  45659. ]
  45660. ].filter(Boolean);
  45661. }
  45662. function createPluginHookUtils(plugins) {
  45663. const sortedPluginsCache = /* @__PURE__ */ new Map();
  45664. function getSortedPlugins(hookName) {
  45665. if (sortedPluginsCache.has(hookName))
  45666. return sortedPluginsCache.get(hookName);
  45667. const sorted = getSortedPluginsByHook(hookName, plugins);
  45668. sortedPluginsCache.set(hookName, sorted);
  45669. return sorted;
  45670. }
  45671. function getSortedPluginHooks(hookName) {
  45672. const plugins2 = getSortedPlugins(hookName);
  45673. return plugins2.map((p) => getHookHandler(p[hookName])).filter(Boolean);
  45674. }
  45675. return {
  45676. getSortedPlugins,
  45677. getSortedPluginHooks
  45678. };
  45679. }
  45680. function getSortedPluginsByHook(hookName, plugins) {
  45681. const sortedPlugins = [];
  45682. let pre = 0, normal = 0, post = 0;
  45683. for (const plugin of plugins) {
  45684. const hook = plugin[hookName];
  45685. if (hook) {
  45686. if (typeof hook === "object") {
  45687. if (hook.order === "pre") {
  45688. sortedPlugins.splice(pre++, 0, plugin);
  45689. continue;
  45690. }
  45691. if (hook.order === "post") {
  45692. sortedPlugins.splice(pre + normal + post++, 0, plugin);
  45693. continue;
  45694. }
  45695. }
  45696. sortedPlugins.splice(pre + normal++, 0, plugin);
  45697. }
  45698. }
  45699. return sortedPlugins;
  45700. }
  45701. function getHookHandler(hook) {
  45702. return typeof hook === "object" ? hook.handler : hook;
  45703. }
  45704. const viteAliasCustomResolver = async function(id, importer, options) {
  45705. const resolved = await this.resolve(id, importer, options);
  45706. return resolved || { id, meta: { "vite:alias": { noResolved: true } } };
  45707. };
  45708. function ansiRegex({onlyFirst = false} = {}) {
  45709. const pattern = [
  45710. '[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)',
  45711. '(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~]))'
  45712. ].join('|');
  45713. return new RegExp(pattern, onlyFirst ? undefined : 'g');
  45714. }
  45715. const regex = ansiRegex();
  45716. function stripAnsi(string) {
  45717. if (typeof string !== 'string') {
  45718. throw new TypeError(`Expected a \`string\`, got \`${typeof string}\``);
  45719. }
  45720. // Even though the regex is global, we don't need to reset the `.lastIndex`
  45721. // because unlike `.exec()` and `.test()`, `.replace()` does it automatically
  45722. // and doing it manually has a performance penalty.
  45723. return string.replace(regex, '');
  45724. }
  45725. function prepareError(err) {
  45726. return {
  45727. message: stripAnsi(err.message),
  45728. stack: stripAnsi(cleanStack(err.stack || "")),
  45729. id: err.id,
  45730. frame: stripAnsi(err.frame || ""),
  45731. plugin: err.plugin,
  45732. pluginCode: err.pluginCode?.toString(),
  45733. loc: err.loc
  45734. };
  45735. }
  45736. function buildErrorMessage(err, args = [], includeStack = true) {
  45737. if (err.plugin) args.push(` Plugin: ${colors$1.magenta(err.plugin)}`);
  45738. const loc = err.loc ? `:${err.loc.line}:${err.loc.column}` : "";
  45739. if (err.id) args.push(` File: ${colors$1.cyan(err.id)}${loc}`);
  45740. if (err.frame) args.push(colors$1.yellow(pad$1(err.frame)));
  45741. if (includeStack && err.stack) args.push(pad$1(cleanStack(err.stack)));
  45742. return args.join("\n");
  45743. }
  45744. function cleanStack(stack) {
  45745. return stack.split(/\n/).filter((l) => /^\s*at/.test(l)).join("\n");
  45746. }
  45747. function logError(server, err) {
  45748. const msg = buildErrorMessage(err, [
  45749. colors$1.red(`Internal server error: ${err.message}`)
  45750. ]);
  45751. server.config.logger.error(msg, {
  45752. clear: true,
  45753. timestamp: true,
  45754. error: err
  45755. });
  45756. server.hot.send({
  45757. type: "error",
  45758. err: prepareError(err)
  45759. });
  45760. }
  45761. function errorMiddleware(server, allowNext = false) {
  45762. return function viteErrorMiddleware(err, _req, res, next) {
  45763. logError(server, err);
  45764. if (allowNext) {
  45765. next();
  45766. } else {
  45767. res.statusCode = 500;
  45768. res.end(`
  45769. <!DOCTYPE html>
  45770. <html lang="en">
  45771. <head>
  45772. <meta charset="UTF-8" />
  45773. <title>Error</title>
  45774. <script type="module">
  45775. const error = ${JSON.stringify(prepareError(err)).replace(
  45776. /</g,
  45777. "\\u003c"
  45778. )}
  45779. try {
  45780. const { ErrorOverlay } = await import(${JSON.stringify(path$n.posix.join(server.config.base, CLIENT_PUBLIC_PATH))})
  45781. document.body.appendChild(new ErrorOverlay(error))
  45782. } catch {
  45783. const h = (tag, text) => {
  45784. const el = document.createElement(tag)
  45785. el.textContent = text
  45786. return el
  45787. }
  45788. document.body.appendChild(h('h1', 'Internal Server Error'))
  45789. document.body.appendChild(h('h2', error.message))
  45790. document.body.appendChild(h('pre', error.stack))
  45791. document.body.appendChild(h('p', '(Error overlay failed to load)'))
  45792. }
  45793. <\/script>
  45794. </head>
  45795. <body>
  45796. </body>
  45797. </html>
  45798. `);
  45799. }
  45800. };
  45801. }
  45802. const noop$3 = () => {
  45803. };
  45804. const EMPTY_OBJECT = Object.freeze({});
  45805. const debugSourcemapCombineFilter = process.env.DEBUG_VITE_SOURCEMAP_COMBINE_FILTER;
  45806. const debugSourcemapCombine = createDebugger("vite:sourcemap-combine", {
  45807. onlyWhenFocused: true
  45808. });
  45809. const debugResolve = createDebugger("vite:resolve");
  45810. const debugPluginResolve = createDebugger("vite:plugin-resolve", {
  45811. onlyWhenFocused: "vite:plugin"
  45812. });
  45813. const debugPluginTransform = createDebugger("vite:plugin-transform", {
  45814. onlyWhenFocused: "vite:plugin"
  45815. });
  45816. const ERR_CLOSED_SERVER = "ERR_CLOSED_SERVER";
  45817. function throwClosedServerError() {
  45818. const err = new Error(
  45819. "The server is being restarted or closed. Request is outdated"
  45820. );
  45821. err.code = ERR_CLOSED_SERVER;
  45822. throw err;
  45823. }
  45824. async function createPluginContainer(config, moduleGraph, watcher) {
  45825. const container = new PluginContainer(config, moduleGraph, watcher);
  45826. await container.resolveRollupOptions();
  45827. return container;
  45828. }
  45829. class PluginContainer {
  45830. /**
  45831. * @internal use `createPluginContainer` instead
  45832. */
  45833. constructor(config, moduleGraph, watcher, plugins = config.plugins) {
  45834. this.config = config;
  45835. this.moduleGraph = moduleGraph;
  45836. this.watcher = watcher;
  45837. this.plugins = plugins;
  45838. this.minimalContext = {
  45839. meta: {
  45840. rollupVersion,
  45841. watchMode: true
  45842. },
  45843. debug: noop$3,
  45844. info: noop$3,
  45845. warn: noop$3,
  45846. // @ts-expect-error noop
  45847. error: noop$3
  45848. };
  45849. const utils = createPluginHookUtils(plugins);
  45850. this.getSortedPlugins = utils.getSortedPlugins;
  45851. this.getSortedPluginHooks = utils.getSortedPluginHooks;
  45852. }
  45853. _pluginContextMap = /* @__PURE__ */ new Map();
  45854. _pluginContextMapSsr = /* @__PURE__ */ new Map();
  45855. _resolvedRollupOptions;
  45856. _processesing = /* @__PURE__ */ new Set();
  45857. _seenResolves = {};
  45858. _closed = false;
  45859. // _addedFiles from the `load()` hook gets saved here so it can be reused in the `transform()` hook
  45860. _moduleNodeToLoadAddedImports = /* @__PURE__ */ new WeakMap();
  45861. getSortedPluginHooks;
  45862. getSortedPlugins;
  45863. watchFiles = /* @__PURE__ */ new Set();
  45864. minimalContext;
  45865. _updateModuleLoadAddedImports(id, addedImports) {
  45866. const module = this.moduleGraph?.getModuleById(id);
  45867. if (module) {
  45868. this._moduleNodeToLoadAddedImports.set(module, addedImports);
  45869. }
  45870. }
  45871. _getAddedImports(id) {
  45872. const module = this.moduleGraph?.getModuleById(id);
  45873. return module ? this._moduleNodeToLoadAddedImports.get(module) || null : null;
  45874. }
  45875. getModuleInfo(id) {
  45876. const module = this.moduleGraph?.getModuleById(id);
  45877. if (!module) {
  45878. return null;
  45879. }
  45880. if (!module.info) {
  45881. module.info = new Proxy(
  45882. { id, meta: module.meta || EMPTY_OBJECT },
  45883. // throw when an unsupported ModuleInfo property is accessed,
  45884. // so that incompatible plugins fail in a non-cryptic way.
  45885. {
  45886. get(info, key) {
  45887. if (key in info) {
  45888. return info[key];
  45889. }
  45890. if (key === "then") {
  45891. return void 0;
  45892. }
  45893. throw Error(
  45894. `[vite] The "${key}" property of ModuleInfo is not supported.`
  45895. );
  45896. }
  45897. }
  45898. );
  45899. }
  45900. return module.info ?? null;
  45901. }
  45902. // keeps track of hook promises so that we can wait for them all to finish upon closing the server
  45903. handleHookPromise(maybePromise) {
  45904. if (!maybePromise?.then) {
  45905. return maybePromise;
  45906. }
  45907. const promise = maybePromise;
  45908. this._processesing.add(promise);
  45909. return promise.finally(() => this._processesing.delete(promise));
  45910. }
  45911. get options() {
  45912. return this._resolvedRollupOptions;
  45913. }
  45914. async resolveRollupOptions() {
  45915. if (!this._resolvedRollupOptions) {
  45916. let options = this.config.build.rollupOptions;
  45917. for (const optionsHook of this.getSortedPluginHooks("options")) {
  45918. if (this._closed) {
  45919. throwClosedServerError();
  45920. }
  45921. options = await this.handleHookPromise(
  45922. optionsHook.call(this.minimalContext, options)
  45923. ) || options;
  45924. }
  45925. this._resolvedRollupOptions = options;
  45926. }
  45927. return this._resolvedRollupOptions;
  45928. }
  45929. _getPluginContext(plugin, ssr) {
  45930. const map = ssr ? this._pluginContextMapSsr : this._pluginContextMap;
  45931. if (!map.has(plugin)) {
  45932. const ctx = new PluginContext(plugin, this, ssr);
  45933. map.set(plugin, ctx);
  45934. }
  45935. return map.get(plugin);
  45936. }
  45937. // parallel, ignores returns
  45938. async hookParallel(hookName, context, args) {
  45939. const parallelPromises = [];
  45940. for (const plugin of this.getSortedPlugins(hookName)) {
  45941. const hook = plugin[hookName];
  45942. if (!hook) continue;
  45943. const handler = getHookHandler(hook);
  45944. if (hook.sequential) {
  45945. await Promise.all(parallelPromises);
  45946. parallelPromises.length = 0;
  45947. await handler.apply(context(plugin), args(plugin));
  45948. } else {
  45949. parallelPromises.push(handler.apply(context(plugin), args(plugin)));
  45950. }
  45951. }
  45952. await Promise.all(parallelPromises);
  45953. }
  45954. async buildStart(_options) {
  45955. await this.handleHookPromise(
  45956. this.hookParallel(
  45957. "buildStart",
  45958. (plugin) => this._getPluginContext(plugin, false),
  45959. () => [this.options]
  45960. )
  45961. );
  45962. }
  45963. async resolveId(rawId, importer = join$2(this.config.root, "index.html"), options) {
  45964. const skip = options?.skip;
  45965. const ssr = options?.ssr;
  45966. const scan = !!options?.scan;
  45967. const ctx = new ResolveIdContext(this, !!ssr, skip, scan);
  45968. const resolveStart = debugResolve ? performance$1.now() : 0;
  45969. let id = null;
  45970. const partial = {};
  45971. for (const plugin of this.getSortedPlugins("resolveId")) {
  45972. if (this._closed && !ssr) throwClosedServerError();
  45973. if (!plugin.resolveId) continue;
  45974. if (skip?.has(plugin)) continue;
  45975. ctx._plugin = plugin;
  45976. const pluginResolveStart = debugPluginResolve ? performance$1.now() : 0;
  45977. const handler = getHookHandler(plugin.resolveId);
  45978. const result = await this.handleHookPromise(
  45979. handler.call(ctx, rawId, importer, {
  45980. attributes: options?.attributes ?? {},
  45981. custom: options?.custom,
  45982. isEntry: !!options?.isEntry,
  45983. ssr,
  45984. scan
  45985. })
  45986. );
  45987. if (!result) continue;
  45988. if (typeof result === "string") {
  45989. id = result;
  45990. } else {
  45991. id = result.id;
  45992. Object.assign(partial, result);
  45993. }
  45994. debugPluginResolve?.(
  45995. timeFrom(pluginResolveStart),
  45996. plugin.name,
  45997. prettifyUrl(id, this.config.root)
  45998. );
  45999. break;
  46000. }
  46001. if (debugResolve && rawId !== id && !rawId.startsWith(FS_PREFIX)) {
  46002. const key = rawId + id;
  46003. if (!this._seenResolves[key]) {
  46004. this._seenResolves[key] = true;
  46005. debugResolve(
  46006. `${timeFrom(resolveStart)} ${colors$1.cyan(rawId)} -> ${colors$1.dim(
  46007. id
  46008. )}`
  46009. );
  46010. }
  46011. }
  46012. if (id) {
  46013. partial.id = isExternalUrl(id) ? id : normalizePath$3(id);
  46014. return partial;
  46015. } else {
  46016. return null;
  46017. }
  46018. }
  46019. async load(id, options) {
  46020. const ssr = options?.ssr;
  46021. const ctx = new LoadPluginContext(this, !!ssr);
  46022. for (const plugin of this.getSortedPlugins("load")) {
  46023. if (this._closed && !ssr) throwClosedServerError();
  46024. if (!plugin.load) continue;
  46025. ctx._plugin = plugin;
  46026. const handler = getHookHandler(plugin.load);
  46027. const result = await this.handleHookPromise(
  46028. handler.call(ctx, id, { ssr })
  46029. );
  46030. if (result != null) {
  46031. if (isObject$1(result)) {
  46032. ctx._updateModuleInfo(id, result);
  46033. }
  46034. this._updateModuleLoadAddedImports(id, ctx._addedImports);
  46035. return result;
  46036. }
  46037. }
  46038. this._updateModuleLoadAddedImports(id, ctx._addedImports);
  46039. return null;
  46040. }
  46041. async transform(code, id, options) {
  46042. const inMap = options?.inMap;
  46043. const ssr = options?.ssr;
  46044. const ctx = new TransformPluginContext(
  46045. this,
  46046. id,
  46047. code,
  46048. inMap,
  46049. !!ssr
  46050. );
  46051. ctx._addedImports = this._getAddedImports(id);
  46052. for (const plugin of this.getSortedPlugins("transform")) {
  46053. if (this._closed && !ssr) throwClosedServerError();
  46054. if (!plugin.transform) continue;
  46055. ctx._updateActiveInfo(plugin, id, code);
  46056. const start = debugPluginTransform ? performance$1.now() : 0;
  46057. let result;
  46058. const handler = getHookHandler(plugin.transform);
  46059. try {
  46060. result = await this.handleHookPromise(
  46061. handler.call(ctx, code, id, { ssr })
  46062. );
  46063. } catch (e) {
  46064. ctx.error(e);
  46065. }
  46066. if (!result) continue;
  46067. debugPluginTransform?.(
  46068. timeFrom(start),
  46069. plugin.name,
  46070. prettifyUrl(id, this.config.root)
  46071. );
  46072. if (isObject$1(result)) {
  46073. if (result.code !== void 0) {
  46074. code = result.code;
  46075. if (result.map) {
  46076. if (debugSourcemapCombine) {
  46077. result.map.name = plugin.name;
  46078. }
  46079. ctx.sourcemapChain.push(result.map);
  46080. }
  46081. }
  46082. ctx._updateModuleInfo(id, result);
  46083. } else {
  46084. code = result;
  46085. }
  46086. }
  46087. return {
  46088. code,
  46089. map: ctx._getCombinedSourcemap()
  46090. };
  46091. }
  46092. async watchChange(id, change) {
  46093. await this.hookParallel(
  46094. "watchChange",
  46095. (plugin) => this._getPluginContext(plugin, false),
  46096. () => [id, change]
  46097. );
  46098. }
  46099. async close() {
  46100. if (this._closed) return;
  46101. this._closed = true;
  46102. await Promise.allSettled(Array.from(this._processesing));
  46103. await this.hookParallel(
  46104. "buildEnd",
  46105. (plugin) => this._getPluginContext(plugin, false),
  46106. () => []
  46107. );
  46108. await this.hookParallel(
  46109. "closeBundle",
  46110. (plugin) => this._getPluginContext(plugin, false),
  46111. () => []
  46112. );
  46113. }
  46114. }
  46115. class PluginContext {
  46116. constructor(_plugin, _container, ssr) {
  46117. this._plugin = _plugin;
  46118. this._container = _container;
  46119. this.ssr = ssr;
  46120. this.meta = this._container.minimalContext.meta;
  46121. }
  46122. _scan = false;
  46123. _resolveSkips;
  46124. _activeId = null;
  46125. _activeCode = null;
  46126. meta;
  46127. parse(code, opts) {
  46128. return parseAst(code, opts);
  46129. }
  46130. getModuleInfo(id) {
  46131. return this._container.getModuleInfo(id);
  46132. }
  46133. async resolve(id, importer, options) {
  46134. let skip;
  46135. if (options?.skipSelf !== false && this._plugin) {
  46136. skip = new Set(this._resolveSkips);
  46137. skip.add(this._plugin);
  46138. }
  46139. let out = await this._container.resolveId(id, importer, {
  46140. attributes: options?.attributes,
  46141. custom: options?.custom,
  46142. isEntry: !!options?.isEntry,
  46143. skip,
  46144. ssr: this.ssr,
  46145. scan: this._scan
  46146. });
  46147. if (typeof out === "string") out = { id: out };
  46148. return out;
  46149. }
  46150. async load(options) {
  46151. await this._container.moduleGraph?.ensureEntryFromUrl(
  46152. unwrapId$1(options.id),
  46153. this.ssr
  46154. );
  46155. this._updateModuleInfo(options.id, options);
  46156. const loadResult = await this._container.load(options.id, {
  46157. ssr: this.ssr
  46158. });
  46159. const code = typeof loadResult === "object" ? loadResult?.code : loadResult;
  46160. if (code != null) {
  46161. await this._container.transform(code, options.id, { ssr: this.ssr });
  46162. }
  46163. const moduleInfo = this.getModuleInfo(options.id);
  46164. if (!moduleInfo) throw Error(`Failed to load module with id ${options.id}`);
  46165. return moduleInfo;
  46166. }
  46167. _updateModuleInfo(id, { meta }) {
  46168. if (meta) {
  46169. const moduleInfo = this.getModuleInfo(id);
  46170. if (moduleInfo) {
  46171. moduleInfo.meta = { ...moduleInfo.meta, ...meta };
  46172. }
  46173. }
  46174. }
  46175. getModuleIds() {
  46176. return this._container.moduleGraph ? this._container.moduleGraph.idToModuleMap.keys() : Array.prototype[Symbol.iterator]();
  46177. }
  46178. addWatchFile(id) {
  46179. this._container.watchFiles.add(id);
  46180. if (this._container.watcher)
  46181. ensureWatchedFile(
  46182. this._container.watcher,
  46183. id,
  46184. this._container.config.root
  46185. );
  46186. }
  46187. getWatchFiles() {
  46188. return [...this._container.watchFiles];
  46189. }
  46190. emitFile(assetOrFile) {
  46191. this._warnIncompatibleMethod(`emitFile`);
  46192. return "";
  46193. }
  46194. setAssetSource() {
  46195. this._warnIncompatibleMethod(`setAssetSource`);
  46196. }
  46197. getFileName() {
  46198. this._warnIncompatibleMethod(`getFileName`);
  46199. return "";
  46200. }
  46201. warn(e, position) {
  46202. const err = this._formatError(typeof e === "function" ? e() : e, position);
  46203. const msg = buildErrorMessage(
  46204. err,
  46205. [colors$1.yellow(`warning: ${err.message}`)],
  46206. false
  46207. );
  46208. this._container.config.logger.warn(msg, {
  46209. clear: true,
  46210. timestamp: true
  46211. });
  46212. }
  46213. error(e, position) {
  46214. throw this._formatError(e, position);
  46215. }
  46216. debug = noop$3;
  46217. info = noop$3;
  46218. _formatError(e, position) {
  46219. const err = typeof e === "string" ? new Error(e) : e;
  46220. if (err.pluginCode) {
  46221. return err;
  46222. }
  46223. if (this._plugin) err.plugin = this._plugin.name;
  46224. if (this._activeId && !err.id) err.id = this._activeId;
  46225. if (this._activeCode) {
  46226. err.pluginCode = this._activeCode;
  46227. const pos = position ?? err.pos ?? err.position;
  46228. if (pos != null) {
  46229. let errLocation;
  46230. try {
  46231. errLocation = numberToPos(this._activeCode, pos);
  46232. } catch (err2) {
  46233. this._container.config.logger.error(
  46234. colors$1.red(
  46235. `Error in error handler:
  46236. ${err2.stack || err2.message}
  46237. `
  46238. ),
  46239. // print extra newline to separate the two errors
  46240. { error: err2 }
  46241. );
  46242. throw err;
  46243. }
  46244. err.loc = err.loc || {
  46245. file: err.id,
  46246. ...errLocation
  46247. };
  46248. err.frame = err.frame || generateCodeFrame(this._activeCode, pos);
  46249. } else if (err.loc) {
  46250. if (!err.frame) {
  46251. let code = this._activeCode;
  46252. if (err.loc.file) {
  46253. err.id = normalizePath$3(err.loc.file);
  46254. try {
  46255. code = fs__default.readFileSync(err.loc.file, "utf-8");
  46256. } catch {
  46257. }
  46258. }
  46259. err.frame = generateCodeFrame(code, err.loc);
  46260. }
  46261. } else if (err.line && err.column) {
  46262. err.loc = {
  46263. file: err.id,
  46264. line: err.line,
  46265. column: err.column
  46266. };
  46267. err.frame = err.frame || generateCodeFrame(this._activeCode, err.loc);
  46268. }
  46269. if (this instanceof TransformPluginContext && typeof err.loc?.line === "number" && typeof err.loc?.column === "number") {
  46270. const rawSourceMap = this._getCombinedSourcemap();
  46271. if (rawSourceMap && "version" in rawSourceMap) {
  46272. const traced = new TraceMap(rawSourceMap);
  46273. const { source, line, column } = originalPositionFor$1(traced, {
  46274. line: Number(err.loc.line),
  46275. column: Number(err.loc.column)
  46276. });
  46277. if (source && line != null && column != null) {
  46278. err.loc = { file: source, line, column };
  46279. }
  46280. }
  46281. }
  46282. } else if (err.loc) {
  46283. if (!err.frame) {
  46284. let code = err.pluginCode;
  46285. if (err.loc.file) {
  46286. err.id = normalizePath$3(err.loc.file);
  46287. if (!code) {
  46288. try {
  46289. code = fs__default.readFileSync(err.loc.file, "utf-8");
  46290. } catch {
  46291. }
  46292. }
  46293. }
  46294. if (code) {
  46295. err.frame = generateCodeFrame(`${code}`, err.loc);
  46296. }
  46297. }
  46298. }
  46299. if (typeof err.loc?.column !== "number" && typeof err.loc?.line !== "number" && !err.loc?.file) {
  46300. delete err.loc;
  46301. }
  46302. return err;
  46303. }
  46304. _warnIncompatibleMethod(method) {
  46305. this._container.config.logger.warn(
  46306. colors$1.cyan(`[plugin:${this._plugin.name}] `) + colors$1.yellow(
  46307. `context method ${colors$1.bold(
  46308. `${method}()`
  46309. )} is not supported in serve mode. This plugin is likely not vite-compatible.`
  46310. )
  46311. );
  46312. }
  46313. }
  46314. class ResolveIdContext extends PluginContext {
  46315. constructor(container, ssr, skip, scan) {
  46316. super(null, container, ssr);
  46317. this._resolveSkips = skip;
  46318. this._scan = scan;
  46319. }
  46320. }
  46321. class LoadPluginContext extends PluginContext {
  46322. _addedImports = null;
  46323. constructor(container, ssr) {
  46324. super(null, container, ssr);
  46325. }
  46326. addWatchFile(id) {
  46327. if (!this._addedImports) {
  46328. this._addedImports = /* @__PURE__ */ new Set();
  46329. }
  46330. this._addedImports.add(id);
  46331. super.addWatchFile(id);
  46332. }
  46333. }
  46334. class TransformPluginContext extends LoadPluginContext {
  46335. filename;
  46336. originalCode;
  46337. originalSourcemap = null;
  46338. sourcemapChain = [];
  46339. combinedMap = null;
  46340. constructor(container, id, code, inMap, ssr) {
  46341. super(container, ssr);
  46342. this.filename = id;
  46343. this.originalCode = code;
  46344. if (inMap) {
  46345. if (debugSourcemapCombine) {
  46346. inMap.name = "$inMap";
  46347. }
  46348. this.sourcemapChain.push(inMap);
  46349. }
  46350. }
  46351. _getCombinedSourcemap() {
  46352. if (debugSourcemapCombine && debugSourcemapCombineFilter && this.filename.includes(debugSourcemapCombineFilter)) {
  46353. debugSourcemapCombine("----------", this.filename);
  46354. debugSourcemapCombine(this.combinedMap);
  46355. debugSourcemapCombine(this.sourcemapChain);
  46356. debugSourcemapCombine("----------");
  46357. }
  46358. let combinedMap = this.combinedMap;
  46359. if (combinedMap && !("version" in combinedMap) && combinedMap.mappings === "") {
  46360. this.sourcemapChain.length = 0;
  46361. return combinedMap;
  46362. }
  46363. for (let m of this.sourcemapChain) {
  46364. if (typeof m === "string") m = JSON.parse(m);
  46365. if (!("version" in m)) {
  46366. if (m.mappings === "") {
  46367. combinedMap = { mappings: "" };
  46368. break;
  46369. }
  46370. combinedMap = null;
  46371. break;
  46372. }
  46373. if (!combinedMap) {
  46374. const sm = m;
  46375. if (sm.sources.length === 1 && !sm.sources[0]) {
  46376. combinedMap = {
  46377. ...sm,
  46378. sources: [this.filename],
  46379. sourcesContent: [this.originalCode]
  46380. };
  46381. } else {
  46382. combinedMap = sm;
  46383. }
  46384. } else {
  46385. combinedMap = combineSourcemaps(cleanUrl(this.filename), [
  46386. m,
  46387. combinedMap
  46388. ]);
  46389. }
  46390. }
  46391. if (combinedMap !== this.combinedMap) {
  46392. this.combinedMap = combinedMap;
  46393. this.sourcemapChain.length = 0;
  46394. }
  46395. return this.combinedMap;
  46396. }
  46397. getCombinedSourcemap() {
  46398. const map = this._getCombinedSourcemap();
  46399. if (!map || !("version" in map) && map.mappings === "") {
  46400. return new MagicString(this.originalCode).generateMap({
  46401. includeContent: true,
  46402. hires: "boundary",
  46403. source: cleanUrl(this.filename)
  46404. });
  46405. }
  46406. return map;
  46407. }
  46408. _updateActiveInfo(plugin, id, code) {
  46409. this._plugin = plugin;
  46410. this._activeId = id;
  46411. this._activeCode = code;
  46412. }
  46413. }
  46414. const debug$9 = createDebugger("vite:deps");
  46415. const htmlTypesRE = /\.(html|vue|svelte|astro|imba)$/;
  46416. const importsRE = /(?<!\/\/.*)(?<=^|;|\*\/)\s*import(?!\s+type)(?:[\w*{}\n\r\t, ]+from)?\s*("[^"]+"|'[^']+')\s*(?=$|;|\/\/|\/\*)/gm;
  46417. function scanImports(config) {
  46418. const start = performance$1.now();
  46419. const deps = {};
  46420. const missing = {};
  46421. let entries;
  46422. const scanContext = { cancelled: false };
  46423. const esbuildContext = computeEntries(
  46424. config
  46425. ).then((computedEntries) => {
  46426. entries = computedEntries;
  46427. if (!entries.length) {
  46428. if (!config.optimizeDeps.entries && !config.optimizeDeps.include) {
  46429. config.logger.warn(
  46430. colors$1.yellow(
  46431. "(!) Could not auto-determine entry point from rollupOptions or html files and there are no explicit optimizeDeps.include patterns. Skipping dependency pre-bundling."
  46432. )
  46433. );
  46434. }
  46435. return;
  46436. }
  46437. if (scanContext.cancelled) return;
  46438. debug$9?.(
  46439. `Crawling dependencies using entries: ${entries.map((entry) => `
  46440. ${colors$1.dim(entry)}`).join("")}`
  46441. );
  46442. return prepareEsbuildScanner(config, entries, deps, missing, scanContext);
  46443. });
  46444. const result = esbuildContext.then((context) => {
  46445. function disposeContext() {
  46446. return context?.dispose().catch((e) => {
  46447. config.logger.error("Failed to dispose esbuild context", { error: e });
  46448. });
  46449. }
  46450. if (!context || scanContext?.cancelled) {
  46451. disposeContext();
  46452. return { deps: {}, missing: {} };
  46453. }
  46454. return context.rebuild().then(() => {
  46455. return {
  46456. // Ensure a fixed order so hashes are stable and improve logs
  46457. deps: orderedDependencies(deps),
  46458. missing
  46459. };
  46460. }).finally(() => {
  46461. return disposeContext();
  46462. });
  46463. }).catch(async (e) => {
  46464. if (e.errors && e.message.includes("The build was canceled")) {
  46465. return { deps: {}, missing: {} };
  46466. }
  46467. const prependMessage = colors$1.red(` Failed to scan for dependencies from entries:
  46468. ${entries.join("\n")}
  46469. `);
  46470. if (e.errors) {
  46471. const msgs = await formatMessages(e.errors, {
  46472. kind: "error",
  46473. color: true
  46474. });
  46475. e.message = prependMessage + msgs.join("\n");
  46476. } else {
  46477. e.message = prependMessage + e.message;
  46478. }
  46479. throw e;
  46480. }).finally(() => {
  46481. if (debug$9) {
  46482. const duration = (performance$1.now() - start).toFixed(2);
  46483. const depsStr = Object.keys(orderedDependencies(deps)).sort().map((id) => `
  46484. ${colors$1.cyan(id)} -> ${colors$1.dim(deps[id])}`).join("") || colors$1.dim("no dependencies found");
  46485. debug$9(`Scan completed in ${duration}ms: ${depsStr}`);
  46486. }
  46487. });
  46488. return {
  46489. cancel: async () => {
  46490. scanContext.cancelled = true;
  46491. return esbuildContext.then((context) => context?.cancel());
  46492. },
  46493. result
  46494. };
  46495. }
  46496. async function computeEntries(config) {
  46497. let entries = [];
  46498. const explicitEntryPatterns = config.optimizeDeps.entries;
  46499. const buildInput = config.build.rollupOptions?.input;
  46500. if (explicitEntryPatterns) {
  46501. entries = await globEntries(explicitEntryPatterns, config);
  46502. } else if (buildInput) {
  46503. const resolvePath = (p) => path$n.resolve(config.root, p);
  46504. if (typeof buildInput === "string") {
  46505. entries = [resolvePath(buildInput)];
  46506. } else if (Array.isArray(buildInput)) {
  46507. entries = buildInput.map(resolvePath);
  46508. } else if (isObject$1(buildInput)) {
  46509. entries = Object.values(buildInput).map(resolvePath);
  46510. } else {
  46511. throw new Error("invalid rollupOptions.input value.");
  46512. }
  46513. } else {
  46514. entries = await globEntries("**/*.html", config);
  46515. }
  46516. entries = entries.filter(
  46517. (entry) => isScannable(entry, config.optimizeDeps.extensions) && fs__default.existsSync(entry)
  46518. );
  46519. return entries;
  46520. }
  46521. async function prepareEsbuildScanner(config, entries, deps, missing, scanContext) {
  46522. const container = await createPluginContainer(config);
  46523. if (scanContext?.cancelled) return;
  46524. const plugin = esbuildScanPlugin(config, container, deps, missing, entries);
  46525. const { plugins = [], ...esbuildOptions } = config.optimizeDeps?.esbuildOptions ?? {};
  46526. let tsconfigRaw = esbuildOptions.tsconfigRaw;
  46527. if (!tsconfigRaw && !esbuildOptions.tsconfig) {
  46528. const tsconfigResult = await loadTsconfigJsonForFile(
  46529. path$n.join(config.root, "_dummy.js")
  46530. );
  46531. if (tsconfigResult.compilerOptions?.experimentalDecorators) {
  46532. tsconfigRaw = { compilerOptions: { experimentalDecorators: true } };
  46533. }
  46534. }
  46535. return await esbuild.context({
  46536. absWorkingDir: process.cwd(),
  46537. write: false,
  46538. stdin: {
  46539. contents: entries.map((e) => `import ${JSON.stringify(e)}`).join("\n"),
  46540. loader: "js"
  46541. },
  46542. bundle: true,
  46543. format: "esm",
  46544. logLevel: "silent",
  46545. plugins: [...plugins, plugin],
  46546. ...esbuildOptions,
  46547. tsconfigRaw
  46548. });
  46549. }
  46550. function orderedDependencies(deps) {
  46551. const depsList = Object.entries(deps);
  46552. depsList.sort((a, b) => a[0].localeCompare(b[0]));
  46553. return Object.fromEntries(depsList);
  46554. }
  46555. function globEntries(pattern, config) {
  46556. const resolvedPatterns = arraify(pattern);
  46557. if (resolvedPatterns.every((str) => !glob.isDynamicPattern(str))) {
  46558. return resolvedPatterns.map(
  46559. (p) => normalizePath$3(path$n.resolve(config.root, p))
  46560. );
  46561. }
  46562. return glob(pattern, {
  46563. cwd: config.root,
  46564. ignore: [
  46565. "**/node_modules/**",
  46566. `**/${config.build.outDir}/**`,
  46567. // if there aren't explicit entries, also ignore other common folders
  46568. ...config.optimizeDeps.entries ? [] : [`**/__tests__/**`, `**/coverage/**`]
  46569. ],
  46570. absolute: true,
  46571. suppressErrors: true
  46572. // suppress EACCES errors
  46573. });
  46574. }
  46575. const scriptRE = /(<script(?:\s+[a-z_:][-\w:]*(?:\s*=\s*(?:"[^"]*"|'[^']*'|[^"'<>=\s]+))?)*\s*>)(.*?)<\/script>/gis;
  46576. const commentRE = /<!--.*?-->/gs;
  46577. const srcRE = /\bsrc\s*=\s*(?:"([^"]+)"|'([^']+)'|([^\s'">]+))/i;
  46578. const typeRE = /\btype\s*=\s*(?:"([^"]+)"|'([^']+)'|([^\s'">]+))/i;
  46579. const langRE = /\blang\s*=\s*(?:"([^"]+)"|'([^']+)'|([^\s'">]+))/i;
  46580. const svelteScriptModuleRE = /\bcontext\s*=\s*(?:"([^"]+)"|'([^']+)'|([^\s'">]+))/i;
  46581. const svelteModuleRE = /\smodule\b/i;
  46582. function esbuildScanPlugin(config, container, depImports, missing, entries) {
  46583. const seen = /* @__PURE__ */ new Map();
  46584. const resolve = async (id, importer, options) => {
  46585. const key = id + (importer && path$n.dirname(importer));
  46586. if (seen.has(key)) {
  46587. return seen.get(key);
  46588. }
  46589. const resolved = await container.resolveId(
  46590. id,
  46591. importer && normalizePath$3(importer),
  46592. {
  46593. ...options,
  46594. scan: true
  46595. }
  46596. );
  46597. const res = resolved?.id;
  46598. seen.set(key, res);
  46599. return res;
  46600. };
  46601. const include = config.optimizeDeps?.include;
  46602. const exclude = [
  46603. ...config.optimizeDeps?.exclude || [],
  46604. "@vite/client",
  46605. "@vite/env"
  46606. ];
  46607. const isUnlessEntry = (path2) => !entries.includes(path2);
  46608. const externalUnlessEntry = ({ path: path2 }) => ({
  46609. path: path2,
  46610. external: isUnlessEntry(path2)
  46611. });
  46612. const doTransformGlobImport = async (contents, id, loader) => {
  46613. let transpiledContents;
  46614. if (loader !== "js") {
  46615. transpiledContents = (await transform$1(contents, { loader })).code;
  46616. } else {
  46617. transpiledContents = contents;
  46618. }
  46619. const result = await transformGlobImport(
  46620. transpiledContents,
  46621. id,
  46622. config.root,
  46623. resolve
  46624. );
  46625. return result?.s.toString() || transpiledContents;
  46626. };
  46627. return {
  46628. name: "vite:dep-scan",
  46629. setup(build) {
  46630. const scripts = {};
  46631. build.onResolve({ filter: externalRE }, ({ path: path2 }) => ({
  46632. path: path2,
  46633. external: true
  46634. }));
  46635. build.onResolve({ filter: dataUrlRE }, ({ path: path2 }) => ({
  46636. path: path2,
  46637. external: true
  46638. }));
  46639. build.onResolve({ filter: virtualModuleRE }, ({ path: path2 }) => {
  46640. return {
  46641. // strip prefix to get valid filesystem path so esbuild can resolve imports in the file
  46642. path: path2.replace(virtualModulePrefix, ""),
  46643. namespace: "script"
  46644. };
  46645. });
  46646. build.onLoad({ filter: /.*/, namespace: "script" }, ({ path: path2 }) => {
  46647. return scripts[path2];
  46648. });
  46649. build.onResolve({ filter: htmlTypesRE }, async ({ path: path2, importer }) => {
  46650. const resolved = await resolve(path2, importer);
  46651. if (!resolved) return;
  46652. if (isInNodeModules$1(resolved) && isOptimizable(resolved, config.optimizeDeps))
  46653. return;
  46654. return {
  46655. path: resolved,
  46656. namespace: "html"
  46657. };
  46658. });
  46659. const htmlTypeOnLoadCallback = async ({ path: p }) => {
  46660. let raw = await fsp.readFile(p, "utf-8");
  46661. raw = raw.replace(commentRE, "<!---->");
  46662. const isHtml = p.endsWith(".html");
  46663. let js = "";
  46664. let scriptId = 0;
  46665. const matches = raw.matchAll(scriptRE);
  46666. for (const [, openTag, content] of matches) {
  46667. const typeMatch = typeRE.exec(openTag);
  46668. const type = typeMatch && (typeMatch[1] || typeMatch[2] || typeMatch[3]);
  46669. const langMatch = langRE.exec(openTag);
  46670. const lang = langMatch && (langMatch[1] || langMatch[2] || langMatch[3]);
  46671. if (isHtml && type !== "module") {
  46672. continue;
  46673. }
  46674. if (type && !(type.includes("javascript") || type.includes("ecmascript") || type === "module")) {
  46675. continue;
  46676. }
  46677. let loader = "js";
  46678. if (lang === "ts" || lang === "tsx" || lang === "jsx") {
  46679. loader = lang;
  46680. } else if (p.endsWith(".astro")) {
  46681. loader = "ts";
  46682. }
  46683. const srcMatch = srcRE.exec(openTag);
  46684. if (srcMatch) {
  46685. const src = srcMatch[1] || srcMatch[2] || srcMatch[3];
  46686. js += `import ${JSON.stringify(src)}
  46687. `;
  46688. } else if (content.trim()) {
  46689. const contents = content + (loader.startsWith("ts") ? extractImportPaths(content) : "");
  46690. const key = `${p}?id=${scriptId++}`;
  46691. if (contents.includes("import.meta.glob")) {
  46692. scripts[key] = {
  46693. loader: "js",
  46694. // since it is transpiled
  46695. contents: await doTransformGlobImport(contents, p, loader),
  46696. resolveDir: normalizePath$3(path$n.dirname(p)),
  46697. pluginData: {
  46698. htmlType: { loader }
  46699. }
  46700. };
  46701. } else {
  46702. scripts[key] = {
  46703. loader,
  46704. contents,
  46705. resolveDir: normalizePath$3(path$n.dirname(p)),
  46706. pluginData: {
  46707. htmlType: { loader }
  46708. }
  46709. };
  46710. }
  46711. const virtualModulePath = JSON.stringify(virtualModulePrefix + key);
  46712. let addedImport = false;
  46713. if (p.endsWith(".svelte")) {
  46714. let isModule = svelteModuleRE.test(openTag);
  46715. if (!isModule) {
  46716. const contextMatch = svelteScriptModuleRE.exec(openTag);
  46717. const context = contextMatch && (contextMatch[1] || contextMatch[2] || contextMatch[3]);
  46718. isModule = context === "module";
  46719. }
  46720. if (!isModule) {
  46721. addedImport = true;
  46722. js += `import ${virtualModulePath}
  46723. `;
  46724. }
  46725. }
  46726. if (!addedImport) {
  46727. js += `export * from ${virtualModulePath}
  46728. `;
  46729. }
  46730. }
  46731. }
  46732. if (!p.endsWith(".vue") || !js.includes("export default")) {
  46733. js += "\nexport default {}";
  46734. }
  46735. return {
  46736. loader: "js",
  46737. contents: js
  46738. };
  46739. };
  46740. build.onLoad(
  46741. { filter: htmlTypesRE, namespace: "html" },
  46742. htmlTypeOnLoadCallback
  46743. );
  46744. build.onLoad(
  46745. { filter: htmlTypesRE, namespace: "file" },
  46746. htmlTypeOnLoadCallback
  46747. );
  46748. build.onResolve(
  46749. {
  46750. // avoid matching windows volume
  46751. filter: /^[\w@][^:]/
  46752. },
  46753. async ({ path: id, importer, pluginData }) => {
  46754. if (moduleListContains(exclude, id)) {
  46755. return externalUnlessEntry({ path: id });
  46756. }
  46757. if (depImports[id]) {
  46758. return externalUnlessEntry({ path: id });
  46759. }
  46760. const resolved = await resolve(id, importer, {
  46761. custom: {
  46762. depScan: { loader: pluginData?.htmlType?.loader }
  46763. }
  46764. });
  46765. if (resolved) {
  46766. if (shouldExternalizeDep(resolved, id)) {
  46767. return externalUnlessEntry({ path: id });
  46768. }
  46769. if (isInNodeModules$1(resolved) || include?.includes(id)) {
  46770. if (isOptimizable(resolved, config.optimizeDeps)) {
  46771. depImports[id] = resolved;
  46772. }
  46773. return externalUnlessEntry({ path: id });
  46774. } else if (isScannable(resolved, config.optimizeDeps.extensions)) {
  46775. const namespace = htmlTypesRE.test(resolved) ? "html" : void 0;
  46776. return {
  46777. path: path$n.resolve(resolved),
  46778. namespace
  46779. };
  46780. } else {
  46781. return externalUnlessEntry({ path: id });
  46782. }
  46783. } else {
  46784. missing[id] = normalizePath$3(importer);
  46785. }
  46786. }
  46787. );
  46788. const setupExternalize = (filter, doExternalize) => {
  46789. build.onResolve({ filter }, ({ path: path2 }) => {
  46790. return {
  46791. path: path2,
  46792. external: doExternalize(path2)
  46793. };
  46794. });
  46795. };
  46796. setupExternalize(CSS_LANGS_RE, isUnlessEntry);
  46797. setupExternalize(/\.(json|json5|wasm)$/, isUnlessEntry);
  46798. setupExternalize(
  46799. new RegExp(`\\.(${KNOWN_ASSET_TYPES.join("|")})$`),
  46800. isUnlessEntry
  46801. );
  46802. setupExternalize(SPECIAL_QUERY_RE, () => true);
  46803. build.onResolve(
  46804. {
  46805. filter: /.*/
  46806. },
  46807. async ({ path: id, importer, pluginData }) => {
  46808. const resolved = await resolve(id, importer, {
  46809. custom: {
  46810. depScan: { loader: pluginData?.htmlType?.loader }
  46811. }
  46812. });
  46813. if (resolved) {
  46814. if (shouldExternalizeDep(resolved, id) || !isScannable(resolved, config.optimizeDeps.extensions)) {
  46815. return externalUnlessEntry({ path: id });
  46816. }
  46817. const namespace = htmlTypesRE.test(resolved) ? "html" : void 0;
  46818. return {
  46819. path: path$n.resolve(cleanUrl(resolved)),
  46820. namespace
  46821. };
  46822. } else {
  46823. return externalUnlessEntry({ path: id });
  46824. }
  46825. }
  46826. );
  46827. build.onLoad({ filter: JS_TYPES_RE }, async ({ path: id }) => {
  46828. let ext = path$n.extname(id).slice(1);
  46829. if (ext === "mjs") ext = "js";
  46830. let contents = await fsp.readFile(id, "utf-8");
  46831. if (ext.endsWith("x") && config.esbuild && config.esbuild.jsxInject) {
  46832. contents = config.esbuild.jsxInject + `
  46833. ` + contents;
  46834. }
  46835. const loader = config.optimizeDeps?.esbuildOptions?.loader?.[`.${ext}`] || ext;
  46836. if (contents.includes("import.meta.glob")) {
  46837. return {
  46838. loader: "js",
  46839. // since it is transpiled,
  46840. contents: await doTransformGlobImport(contents, id, loader)
  46841. };
  46842. }
  46843. return {
  46844. loader,
  46845. contents
  46846. };
  46847. });
  46848. build.onLoad({ filter: /.*/, namespace: "file" }, () => {
  46849. return {
  46850. loader: "js",
  46851. contents: "export default {}"
  46852. };
  46853. });
  46854. }
  46855. };
  46856. }
  46857. function extractImportPaths(code) {
  46858. code = code.replace(multilineCommentsRE, "/* */").replace(singlelineCommentsRE, "");
  46859. let js = "";
  46860. let m;
  46861. importsRE.lastIndex = 0;
  46862. while ((m = importsRE.exec(code)) != null) {
  46863. js += `
  46864. import ${m[1]}`;
  46865. }
  46866. return js;
  46867. }
  46868. function shouldExternalizeDep(resolvedId, rawId) {
  46869. if (!path$n.isAbsolute(resolvedId)) {
  46870. return true;
  46871. }
  46872. if (resolvedId === rawId || resolvedId.includes("\0")) {
  46873. return true;
  46874. }
  46875. return false;
  46876. }
  46877. function isScannable(id, extensions) {
  46878. return JS_TYPES_RE.test(id) || htmlTypesRE.test(id) || extensions?.includes(path$n.extname(id)) || false;
  46879. }
  46880. function createOptimizeDepsIncludeResolver(config, ssr) {
  46881. const resolve = config.createResolver({
  46882. asSrc: false,
  46883. scan: true,
  46884. ssrOptimizeCheck: ssr,
  46885. ssrConfig: config.ssr,
  46886. packageCache: /* @__PURE__ */ new Map()
  46887. });
  46888. return async (id) => {
  46889. const lastArrowIndex = id.lastIndexOf(">");
  46890. if (lastArrowIndex === -1) {
  46891. return await resolve(id, void 0, void 0, ssr);
  46892. }
  46893. const nestedRoot = id.substring(0, lastArrowIndex).trim();
  46894. const nestedPath = id.substring(lastArrowIndex + 1).trim();
  46895. const basedir = nestedResolveBasedir(
  46896. nestedRoot,
  46897. config.root,
  46898. config.resolve.preserveSymlinks
  46899. );
  46900. return await resolve(
  46901. nestedPath,
  46902. path$n.resolve(basedir, "package.json"),
  46903. void 0,
  46904. ssr
  46905. );
  46906. };
  46907. }
  46908. function expandGlobIds(id, config) {
  46909. const pkgName = getNpmPackageName(id);
  46910. if (!pkgName) return [];
  46911. const pkgData = resolvePackageData(
  46912. pkgName,
  46913. config.root,
  46914. config.resolve.preserveSymlinks,
  46915. config.packageCache
  46916. );
  46917. if (!pkgData) return [];
  46918. const pattern = "." + id.slice(pkgName.length);
  46919. const exports = pkgData.data.exports;
  46920. if (exports) {
  46921. if (typeof exports === "string" || Array.isArray(exports)) {
  46922. return [pkgName];
  46923. }
  46924. const possibleExportPaths = [];
  46925. for (const key in exports) {
  46926. if (key[0] === ".") {
  46927. if (key.includes("*")) {
  46928. const exportsValue = getFirstExportStringValue(exports[key]);
  46929. if (!exportsValue) continue;
  46930. const exportValuePattern = exportsValue.replace(/\*/g, "**/*");
  46931. const exportsValueGlobRe = new RegExp(
  46932. exportsValue.split("*").map(escapeRegex).join("(.*)")
  46933. );
  46934. possibleExportPaths.push(
  46935. ...glob.sync(exportValuePattern, {
  46936. cwd: pkgData.dir,
  46937. ignore: ["node_modules"]
  46938. }).map((filePath) => {
  46939. if (exportsValue.startsWith("./") && !filePath.startsWith("./")) {
  46940. filePath = "./" + filePath;
  46941. }
  46942. const matched2 = exportsValueGlobRe.exec(slash$1(filePath));
  46943. if (matched2) {
  46944. let allGlobSame = matched2.length === 2;
  46945. if (!allGlobSame) {
  46946. allGlobSame = true;
  46947. for (let i = 2; i < matched2.length; i++) {
  46948. if (matched2[i] !== matched2[i - 1]) {
  46949. allGlobSame = false;
  46950. break;
  46951. }
  46952. }
  46953. }
  46954. if (allGlobSame) {
  46955. return key.replace("*", matched2[1]).slice(2);
  46956. }
  46957. }
  46958. return "";
  46959. }).filter(Boolean)
  46960. );
  46961. } else {
  46962. possibleExportPaths.push(key.slice(2));
  46963. }
  46964. }
  46965. }
  46966. const matched = micromatch$2(possibleExportPaths, pattern).map(
  46967. (match) => path$n.posix.join(pkgName, match)
  46968. );
  46969. matched.unshift(pkgName);
  46970. return matched;
  46971. } else {
  46972. const matched = glob.sync(pattern, { cwd: pkgData.dir, ignore: ["node_modules"] }).map((match) => path$n.posix.join(pkgName, slash$1(match)));
  46973. matched.unshift(pkgName);
  46974. return matched;
  46975. }
  46976. }
  46977. function getFirstExportStringValue(obj) {
  46978. if (typeof obj === "string") {
  46979. return obj;
  46980. } else if (Array.isArray(obj)) {
  46981. return obj[0];
  46982. } else {
  46983. for (const key in obj) {
  46984. return getFirstExportStringValue(obj[key]);
  46985. }
  46986. }
  46987. }
  46988. function nestedResolveBasedir(id, basedir, preserveSymlinks = false) {
  46989. const pkgs = id.split(">").map((pkg) => pkg.trim());
  46990. for (const pkg of pkgs) {
  46991. basedir = resolvePackageData(pkg, basedir, preserveSymlinks)?.dir || basedir;
  46992. }
  46993. return basedir;
  46994. }
  46995. const debug$8 = createDebugger("vite:deps");
  46996. const debounceMs = 100;
  46997. const depsOptimizerMap = /* @__PURE__ */ new WeakMap();
  46998. const devSsrDepsOptimizerMap = /* @__PURE__ */ new WeakMap();
  46999. function getDepsOptimizer(config, ssr) {
  47000. return (ssr ? devSsrDepsOptimizerMap : depsOptimizerMap).get(config);
  47001. }
  47002. async function initDepsOptimizer(config, server) {
  47003. if (!getDepsOptimizer(config, false)) {
  47004. await createDepsOptimizer(config, server);
  47005. }
  47006. }
  47007. let creatingDevSsrOptimizer;
  47008. async function initDevSsrDepsOptimizer(config, server) {
  47009. if (getDepsOptimizer(config, true)) {
  47010. return;
  47011. }
  47012. if (creatingDevSsrOptimizer) {
  47013. return creatingDevSsrOptimizer;
  47014. }
  47015. creatingDevSsrOptimizer = async function() {
  47016. const ssr = false;
  47017. if (!getDepsOptimizer(config, ssr)) {
  47018. await initDepsOptimizer(config, server);
  47019. }
  47020. await getDepsOptimizer(config, ssr).scanProcessing;
  47021. await createDevSsrDepsOptimizer(config);
  47022. creatingDevSsrOptimizer = void 0;
  47023. }();
  47024. return await creatingDevSsrOptimizer;
  47025. }
  47026. async function createDepsOptimizer(config, server) {
  47027. const { logger } = config;
  47028. const ssr = false;
  47029. const sessionTimestamp = Date.now().toString();
  47030. const cachedMetadata = await loadCachedDepOptimizationMetadata(config, ssr);
  47031. let debounceProcessingHandle;
  47032. let closed = false;
  47033. let metadata = cachedMetadata || initDepsOptimizerMetadata(config, ssr, sessionTimestamp);
  47034. const options = getDepOptimizationConfig(config, ssr);
  47035. const { noDiscovery, holdUntilCrawlEnd } = options;
  47036. const depsOptimizer = {
  47037. metadata,
  47038. registerMissingImport,
  47039. run: () => debouncedProcessing(0),
  47040. isOptimizedDepFile: createIsOptimizedDepFile(config),
  47041. isOptimizedDepUrl: createIsOptimizedDepUrl(config),
  47042. getOptimizedDepId: (depInfo) => `${depInfo.file}?v=${depInfo.browserHash}`,
  47043. close,
  47044. options
  47045. };
  47046. depsOptimizerMap.set(config, depsOptimizer);
  47047. let newDepsDiscovered = false;
  47048. let newDepsToLog = [];
  47049. let newDepsToLogHandle;
  47050. const logNewlyDiscoveredDeps = () => {
  47051. if (newDepsToLog.length) {
  47052. logger.info(
  47053. colors$1.green(
  47054. `\u2728 new dependencies optimized: ${depsLogString(newDepsToLog)}`
  47055. ),
  47056. {
  47057. timestamp: true
  47058. }
  47059. );
  47060. newDepsToLog = [];
  47061. }
  47062. };
  47063. let discoveredDepsWhileScanning = [];
  47064. const logDiscoveredDepsWhileScanning = () => {
  47065. if (discoveredDepsWhileScanning.length) {
  47066. logger.info(
  47067. colors$1.green(
  47068. `\u2728 discovered while scanning: ${depsLogString(
  47069. discoveredDepsWhileScanning
  47070. )}`
  47071. ),
  47072. {
  47073. timestamp: true
  47074. }
  47075. );
  47076. discoveredDepsWhileScanning = [];
  47077. }
  47078. };
  47079. let depOptimizationProcessing = promiseWithResolvers();
  47080. let depOptimizationProcessingQueue = [];
  47081. const resolveEnqueuedProcessingPromises = () => {
  47082. for (const processing of depOptimizationProcessingQueue) {
  47083. processing.resolve();
  47084. }
  47085. depOptimizationProcessingQueue = [];
  47086. };
  47087. let enqueuedRerun;
  47088. let currentlyProcessing = false;
  47089. let firstRunCalled = !!cachedMetadata;
  47090. let warnAboutMissedDependencies = false;
  47091. let waitingForCrawlEnd = false;
  47092. if (!cachedMetadata) {
  47093. server._onCrawlEnd(onCrawlEnd);
  47094. waitingForCrawlEnd = true;
  47095. }
  47096. let optimizationResult;
  47097. let discover;
  47098. async function close() {
  47099. closed = true;
  47100. await Promise.allSettled([
  47101. discover?.cancel(),
  47102. depsOptimizer.scanProcessing,
  47103. optimizationResult?.cancel()
  47104. ]);
  47105. }
  47106. if (!cachedMetadata) {
  47107. currentlyProcessing = true;
  47108. const manuallyIncludedDeps = {};
  47109. await addManuallyIncludedOptimizeDeps(manuallyIncludedDeps, config, ssr);
  47110. const manuallyIncludedDepsInfo = toDiscoveredDependencies(
  47111. config,
  47112. manuallyIncludedDeps,
  47113. ssr,
  47114. sessionTimestamp
  47115. );
  47116. for (const depInfo of Object.values(manuallyIncludedDepsInfo)) {
  47117. addOptimizedDepInfo(metadata, "discovered", {
  47118. ...depInfo,
  47119. processing: depOptimizationProcessing.promise
  47120. });
  47121. newDepsDiscovered = true;
  47122. }
  47123. if (noDiscovery) {
  47124. runOptimizer();
  47125. } else {
  47126. depsOptimizer.scanProcessing = new Promise((resolve) => {
  47127. (async () => {
  47128. try {
  47129. debug$8?.(colors$1.green(`scanning for dependencies...`));
  47130. discover = discoverProjectDependencies(config);
  47131. const deps = await discover.result;
  47132. discover = void 0;
  47133. const manuallyIncluded = Object.keys(manuallyIncludedDepsInfo);
  47134. discoveredDepsWhileScanning.push(
  47135. ...Object.keys(metadata.discovered).filter(
  47136. (dep) => !deps[dep] && !manuallyIncluded.includes(dep)
  47137. )
  47138. );
  47139. for (const id of Object.keys(deps)) {
  47140. if (!metadata.discovered[id]) {
  47141. addMissingDep(id, deps[id]);
  47142. }
  47143. }
  47144. const knownDeps = prepareKnownDeps();
  47145. startNextDiscoveredBatch();
  47146. optimizationResult = runOptimizeDeps(config, knownDeps, ssr);
  47147. if (!holdUntilCrawlEnd) {
  47148. optimizationResult.result.then((result) => {
  47149. if (!waitingForCrawlEnd) return;
  47150. optimizationResult = void 0;
  47151. runOptimizer(result);
  47152. });
  47153. }
  47154. } catch (e) {
  47155. logger.error(e.stack || e.message);
  47156. } finally {
  47157. resolve();
  47158. depsOptimizer.scanProcessing = void 0;
  47159. }
  47160. })();
  47161. });
  47162. }
  47163. }
  47164. function startNextDiscoveredBatch() {
  47165. newDepsDiscovered = false;
  47166. depOptimizationProcessingQueue.push(depOptimizationProcessing);
  47167. depOptimizationProcessing = promiseWithResolvers();
  47168. }
  47169. function prepareKnownDeps() {
  47170. const knownDeps = {};
  47171. for (const dep of Object.keys(metadata.optimized)) {
  47172. knownDeps[dep] = { ...metadata.optimized[dep] };
  47173. }
  47174. for (const dep of Object.keys(metadata.discovered)) {
  47175. const { processing, ...info } = metadata.discovered[dep];
  47176. knownDeps[dep] = info;
  47177. }
  47178. return knownDeps;
  47179. }
  47180. async function runOptimizer(preRunResult) {
  47181. const isRerun = firstRunCalled;
  47182. firstRunCalled = true;
  47183. enqueuedRerun = void 0;
  47184. if (debounceProcessingHandle) clearTimeout(debounceProcessingHandle);
  47185. if (closed) {
  47186. currentlyProcessing = false;
  47187. return;
  47188. }
  47189. currentlyProcessing = true;
  47190. try {
  47191. let processingResult;
  47192. if (preRunResult) {
  47193. processingResult = preRunResult;
  47194. } else {
  47195. const knownDeps = prepareKnownDeps();
  47196. startNextDiscoveredBatch();
  47197. optimizationResult = runOptimizeDeps(config, knownDeps, ssr);
  47198. processingResult = await optimizationResult.result;
  47199. optimizationResult = void 0;
  47200. }
  47201. if (closed) {
  47202. currentlyProcessing = false;
  47203. processingResult.cancel();
  47204. resolveEnqueuedProcessingPromises();
  47205. return;
  47206. }
  47207. const newData = processingResult.metadata;
  47208. const needsInteropMismatch = findInteropMismatches(
  47209. metadata.discovered,
  47210. newData.optimized
  47211. );
  47212. const needsReload = needsInteropMismatch.length > 0 || metadata.hash !== newData.hash || Object.keys(metadata.optimized).some((dep) => {
  47213. return metadata.optimized[dep].fileHash !== newData.optimized[dep].fileHash;
  47214. });
  47215. const commitProcessing = async () => {
  47216. await processingResult.commit();
  47217. for (const id in metadata.discovered) {
  47218. if (!newData.optimized[id]) {
  47219. addOptimizedDepInfo(newData, "discovered", metadata.discovered[id]);
  47220. }
  47221. }
  47222. if (!needsReload) {
  47223. newData.browserHash = metadata.browserHash;
  47224. for (const dep in newData.chunks) {
  47225. newData.chunks[dep].browserHash = metadata.browserHash;
  47226. }
  47227. for (const dep in newData.optimized) {
  47228. newData.optimized[dep].browserHash = (metadata.optimized[dep] || metadata.discovered[dep]).browserHash;
  47229. }
  47230. }
  47231. for (const o in newData.optimized) {
  47232. const discovered = metadata.discovered[o];
  47233. if (discovered) {
  47234. const optimized = newData.optimized[o];
  47235. discovered.browserHash = optimized.browserHash;
  47236. discovered.fileHash = optimized.fileHash;
  47237. discovered.needsInterop = optimized.needsInterop;
  47238. discovered.processing = void 0;
  47239. }
  47240. }
  47241. if (isRerun) {
  47242. newDepsToLog.push(
  47243. ...Object.keys(newData.optimized).filter(
  47244. (dep) => !metadata.optimized[dep]
  47245. )
  47246. );
  47247. }
  47248. metadata = depsOptimizer.metadata = newData;
  47249. resolveEnqueuedProcessingPromises();
  47250. };
  47251. if (!needsReload) {
  47252. await commitProcessing();
  47253. if (!debug$8) {
  47254. if (newDepsToLogHandle) clearTimeout(newDepsToLogHandle);
  47255. newDepsToLogHandle = setTimeout(() => {
  47256. newDepsToLogHandle = void 0;
  47257. logNewlyDiscoveredDeps();
  47258. if (warnAboutMissedDependencies) {
  47259. logDiscoveredDepsWhileScanning();
  47260. logger.info(
  47261. colors$1.magenta(
  47262. `\u2757 add these dependencies to optimizeDeps.include to speed up cold start`
  47263. ),
  47264. { timestamp: true }
  47265. );
  47266. warnAboutMissedDependencies = false;
  47267. }
  47268. }, 2 * debounceMs);
  47269. } else {
  47270. debug$8(
  47271. colors$1.green(
  47272. `\u2728 ${!isRerun ? `dependencies optimized` : `optimized dependencies unchanged`}`
  47273. )
  47274. );
  47275. }
  47276. } else {
  47277. if (newDepsDiscovered) {
  47278. processingResult.cancel();
  47279. debug$8?.(
  47280. colors$1.green(
  47281. `\u2728 delaying reload as new dependencies have been found...`
  47282. )
  47283. );
  47284. } else {
  47285. await commitProcessing();
  47286. if (!debug$8) {
  47287. if (newDepsToLogHandle) clearTimeout(newDepsToLogHandle);
  47288. newDepsToLogHandle = void 0;
  47289. logNewlyDiscoveredDeps();
  47290. if (warnAboutMissedDependencies) {
  47291. logDiscoveredDepsWhileScanning();
  47292. logger.info(
  47293. colors$1.magenta(
  47294. `\u2757 add these dependencies to optimizeDeps.include to avoid a full page reload during cold start`
  47295. ),
  47296. { timestamp: true }
  47297. );
  47298. warnAboutMissedDependencies = false;
  47299. }
  47300. }
  47301. logger.info(
  47302. colors$1.green(`\u2728 optimized dependencies changed. reloading`),
  47303. {
  47304. timestamp: true
  47305. }
  47306. );
  47307. if (needsInteropMismatch.length > 0) {
  47308. logger.warn(
  47309. `Mixed ESM and CJS detected in ${colors$1.yellow(
  47310. needsInteropMismatch.join(", ")
  47311. )}, add ${needsInteropMismatch.length === 1 ? "it" : "them"} to optimizeDeps.needsInterop to speed up cold start`,
  47312. {
  47313. timestamp: true
  47314. }
  47315. );
  47316. }
  47317. fullReload();
  47318. }
  47319. }
  47320. } catch (e) {
  47321. logger.error(
  47322. colors$1.red(`error while updating dependencies:
  47323. ${e.stack}`),
  47324. { timestamp: true, error: e }
  47325. );
  47326. resolveEnqueuedProcessingPromises();
  47327. metadata.discovered = {};
  47328. }
  47329. currentlyProcessing = false;
  47330. enqueuedRerun?.();
  47331. }
  47332. function fullReload() {
  47333. server.moduleGraph.invalidateAll();
  47334. server.hot.send({
  47335. type: "full-reload",
  47336. path: "*"
  47337. });
  47338. }
  47339. async function rerun() {
  47340. const deps = Object.keys(metadata.discovered);
  47341. const depsString = depsLogString(deps);
  47342. debug$8?.(colors$1.green(`new dependencies found: ${depsString}`));
  47343. runOptimizer();
  47344. }
  47345. function getDiscoveredBrowserHash(hash, deps, missing) {
  47346. return getHash(
  47347. hash + JSON.stringify(deps) + JSON.stringify(missing) + sessionTimestamp
  47348. );
  47349. }
  47350. function registerMissingImport(id, resolved) {
  47351. const optimized = metadata.optimized[id];
  47352. if (optimized) {
  47353. return optimized;
  47354. }
  47355. const chunk = metadata.chunks[id];
  47356. if (chunk) {
  47357. return chunk;
  47358. }
  47359. let missing = metadata.discovered[id];
  47360. if (missing) {
  47361. return missing;
  47362. }
  47363. missing = addMissingDep(id, resolved);
  47364. if (!waitingForCrawlEnd) {
  47365. debouncedProcessing();
  47366. }
  47367. return missing;
  47368. }
  47369. function addMissingDep(id, resolved) {
  47370. newDepsDiscovered = true;
  47371. return addOptimizedDepInfo(metadata, "discovered", {
  47372. id,
  47373. file: getOptimizedDepPath(id, config, ssr),
  47374. src: resolved,
  47375. // Adding a browserHash to this missing dependency that is unique to
  47376. // the current state of known + missing deps. If its optimizeDeps run
  47377. // doesn't alter the bundled files of previous known dependencies,
  47378. // we don't need a full reload and this browserHash will be kept
  47379. browserHash: getDiscoveredBrowserHash(
  47380. metadata.hash,
  47381. depsFromOptimizedDepInfo(metadata.optimized),
  47382. depsFromOptimizedDepInfo(metadata.discovered)
  47383. ),
  47384. // loading of this pre-bundled dep needs to await for its processing
  47385. // promise to be resolved
  47386. processing: depOptimizationProcessing.promise,
  47387. exportsData: extractExportsData(resolved, config, ssr)
  47388. });
  47389. }
  47390. function debouncedProcessing(timeout = debounceMs) {
  47391. enqueuedRerun = void 0;
  47392. if (debounceProcessingHandle) clearTimeout(debounceProcessingHandle);
  47393. if (newDepsToLogHandle) clearTimeout(newDepsToLogHandle);
  47394. newDepsToLogHandle = void 0;
  47395. debounceProcessingHandle = setTimeout(() => {
  47396. debounceProcessingHandle = void 0;
  47397. enqueuedRerun = rerun;
  47398. if (!currentlyProcessing) {
  47399. enqueuedRerun();
  47400. }
  47401. }, timeout);
  47402. }
  47403. async function onCrawlEnd() {
  47404. waitingForCrawlEnd = false;
  47405. debug$8?.(colors$1.green(`\u2728 static imports crawl ended`));
  47406. if (closed) {
  47407. return;
  47408. }
  47409. await depsOptimizer.scanProcessing;
  47410. if (optimizationResult && !config.optimizeDeps.noDiscovery) {
  47411. const afterScanResult = optimizationResult.result;
  47412. optimizationResult = void 0;
  47413. const result = await afterScanResult;
  47414. currentlyProcessing = false;
  47415. const crawlDeps = Object.keys(metadata.discovered);
  47416. const scanDeps = Object.keys(result.metadata.optimized);
  47417. if (scanDeps.length === 0 && crawlDeps.length === 0) {
  47418. debug$8?.(
  47419. colors$1.green(
  47420. `\u2728 no dependencies found by the scanner or crawling static imports`
  47421. )
  47422. );
  47423. startNextDiscoveredBatch();
  47424. runOptimizer(result);
  47425. return;
  47426. }
  47427. const needsInteropMismatch = findInteropMismatches(
  47428. metadata.discovered,
  47429. result.metadata.optimized
  47430. );
  47431. const scannerMissedDeps = crawlDeps.some((dep) => !scanDeps.includes(dep));
  47432. const outdatedResult = needsInteropMismatch.length > 0 || scannerMissedDeps;
  47433. if (outdatedResult) {
  47434. result.cancel();
  47435. for (const dep of scanDeps) {
  47436. if (!crawlDeps.includes(dep)) {
  47437. addMissingDep(dep, result.metadata.optimized[dep].src);
  47438. }
  47439. }
  47440. if (scannerMissedDeps) {
  47441. debug$8?.(
  47442. colors$1.yellow(
  47443. `\u2728 new dependencies were found while crawling that weren't detected by the scanner`
  47444. )
  47445. );
  47446. }
  47447. debug$8?.(colors$1.green(`\u2728 re-running optimizer`));
  47448. debouncedProcessing(0);
  47449. } else {
  47450. debug$8?.(
  47451. colors$1.green(
  47452. `\u2728 using post-scan optimizer result, the scanner found every used dependency`
  47453. )
  47454. );
  47455. startNextDiscoveredBatch();
  47456. runOptimizer(result);
  47457. }
  47458. } else if (!holdUntilCrawlEnd) {
  47459. if (newDepsDiscovered) {
  47460. debug$8?.(
  47461. colors$1.green(
  47462. `\u2728 new dependencies were found while crawling static imports, re-running optimizer`
  47463. )
  47464. );
  47465. warnAboutMissedDependencies = true;
  47466. debouncedProcessing(0);
  47467. }
  47468. } else {
  47469. const crawlDeps = Object.keys(metadata.discovered);
  47470. currentlyProcessing = false;
  47471. if (crawlDeps.length === 0) {
  47472. debug$8?.(
  47473. colors$1.green(
  47474. `\u2728 no dependencies found while crawling the static imports`
  47475. )
  47476. );
  47477. firstRunCalled = true;
  47478. }
  47479. debouncedProcessing(0);
  47480. }
  47481. }
  47482. }
  47483. async function createDevSsrDepsOptimizer(config) {
  47484. const metadata = await optimizeServerSsrDeps(config);
  47485. const depsOptimizer = {
  47486. metadata,
  47487. isOptimizedDepFile: createIsOptimizedDepFile(config),
  47488. isOptimizedDepUrl: createIsOptimizedDepUrl(config),
  47489. getOptimizedDepId: (depInfo) => `${depInfo.file}?v=${depInfo.browserHash}`,
  47490. registerMissingImport: () => {
  47491. throw new Error(
  47492. "Vite Internal Error: registerMissingImport is not supported in dev SSR"
  47493. );
  47494. },
  47495. // noop, there is no scanning during dev SSR
  47496. // the optimizer blocks the server start
  47497. run: () => {
  47498. },
  47499. close: async () => {
  47500. },
  47501. options: config.ssr.optimizeDeps
  47502. };
  47503. devSsrDepsOptimizerMap.set(config, depsOptimizer);
  47504. }
  47505. function findInteropMismatches(discovered, optimized) {
  47506. const needsInteropMismatch = [];
  47507. for (const dep in discovered) {
  47508. const discoveredDepInfo = discovered[dep];
  47509. if (discoveredDepInfo.needsInterop === void 0) continue;
  47510. const depInfo = optimized[dep];
  47511. if (!depInfo) continue;
  47512. if (depInfo.needsInterop !== discoveredDepInfo.needsInterop) {
  47513. needsInteropMismatch.push(dep);
  47514. debug$8?.(colors$1.cyan(`\u2728 needsInterop mismatch detected for ${dep}`));
  47515. }
  47516. }
  47517. return needsInteropMismatch;
  47518. }
  47519. const debug$7 = createDebugger("vite:deps");
  47520. const jsExtensionRE = /\.js$/i;
  47521. const jsMapExtensionRE = /\.js\.map$/i;
  47522. async function optimizeDeps(config, force = config.optimizeDeps.force, asCommand = false) {
  47523. const log = asCommand ? config.logger.info : debug$7;
  47524. const ssr = false;
  47525. const cachedMetadata = await loadCachedDepOptimizationMetadata(
  47526. config,
  47527. ssr,
  47528. force,
  47529. asCommand
  47530. );
  47531. if (cachedMetadata) {
  47532. return cachedMetadata;
  47533. }
  47534. const deps = await discoverProjectDependencies(config).result;
  47535. await addManuallyIncludedOptimizeDeps(deps, config, ssr);
  47536. const depsString = depsLogString(Object.keys(deps));
  47537. log?.(colors$1.green(`Optimizing dependencies:
  47538. ${depsString}`));
  47539. const depsInfo = toDiscoveredDependencies(config, deps, ssr);
  47540. const result = await runOptimizeDeps(config, depsInfo, ssr).result;
  47541. await result.commit();
  47542. return result.metadata;
  47543. }
  47544. async function optimizeServerSsrDeps(config) {
  47545. const ssr = true;
  47546. const cachedMetadata = await loadCachedDepOptimizationMetadata(
  47547. config,
  47548. ssr,
  47549. config.optimizeDeps.force,
  47550. false
  47551. );
  47552. if (cachedMetadata) {
  47553. return cachedMetadata;
  47554. }
  47555. const deps = {};
  47556. await addManuallyIncludedOptimizeDeps(deps, config, ssr);
  47557. const depsInfo = toDiscoveredDependencies(config, deps, ssr);
  47558. const result = await runOptimizeDeps(config, depsInfo, ssr).result;
  47559. await result.commit();
  47560. return result.metadata;
  47561. }
  47562. function initDepsOptimizerMetadata(config, ssr, timestamp) {
  47563. const { lockfileHash, configHash, hash } = getDepHash(config, ssr);
  47564. return {
  47565. hash,
  47566. lockfileHash,
  47567. configHash,
  47568. browserHash: getOptimizedBrowserHash(hash, {}, timestamp),
  47569. optimized: {},
  47570. chunks: {},
  47571. discovered: {},
  47572. depInfoList: []
  47573. };
  47574. }
  47575. function addOptimizedDepInfo(metadata, type, depInfo) {
  47576. metadata[type][depInfo.id] = depInfo;
  47577. metadata.depInfoList.push(depInfo);
  47578. return depInfo;
  47579. }
  47580. let firstLoadCachedDepOptimizationMetadata = true;
  47581. async function loadCachedDepOptimizationMetadata(config, ssr, force = config.optimizeDeps.force, asCommand = false) {
  47582. const log = asCommand ? config.logger.info : debug$7;
  47583. if (firstLoadCachedDepOptimizationMetadata) {
  47584. firstLoadCachedDepOptimizationMetadata = false;
  47585. setTimeout(() => cleanupDepsCacheStaleDirs(config), 0);
  47586. }
  47587. const depsCacheDir = getDepsCacheDir(config, ssr);
  47588. if (!force) {
  47589. let cachedMetadata;
  47590. try {
  47591. const cachedMetadataPath = path$n.join(depsCacheDir, METADATA_FILENAME);
  47592. cachedMetadata = parseDepsOptimizerMetadata(
  47593. await fsp.readFile(cachedMetadataPath, "utf-8"),
  47594. depsCacheDir
  47595. );
  47596. } catch (e) {
  47597. }
  47598. if (cachedMetadata) {
  47599. if (cachedMetadata.lockfileHash !== getLockfileHash(config)) {
  47600. config.logger.info(
  47601. "Re-optimizing dependencies because lockfile has changed"
  47602. );
  47603. } else if (cachedMetadata.configHash !== getConfigHash(config, ssr)) {
  47604. config.logger.info(
  47605. "Re-optimizing dependencies because vite config has changed"
  47606. );
  47607. } else {
  47608. log?.("Hash is consistent. Skipping. Use --force to override.");
  47609. return cachedMetadata;
  47610. }
  47611. }
  47612. } else {
  47613. config.logger.info("Forced re-optimization of dependencies");
  47614. }
  47615. debug$7?.(colors$1.green(`removing old cache dir ${depsCacheDir}`));
  47616. await fsp.rm(depsCacheDir, { recursive: true, force: true });
  47617. }
  47618. function discoverProjectDependencies(config) {
  47619. const { cancel, result } = scanImports(config);
  47620. return {
  47621. cancel,
  47622. result: result.then(({ deps, missing }) => {
  47623. const missingIds = Object.keys(missing);
  47624. if (missingIds.length) {
  47625. throw new Error(
  47626. `The following dependencies are imported but could not be resolved:
  47627. ${missingIds.map(
  47628. (id) => `${colors$1.cyan(id)} ${colors$1.white(
  47629. colors$1.dim(`(imported by ${missing[id]})`)
  47630. )}`
  47631. ).join(`
  47632. `)}
  47633. Are they installed?`
  47634. );
  47635. }
  47636. return deps;
  47637. })
  47638. };
  47639. }
  47640. function toDiscoveredDependencies(config, deps, ssr, timestamp) {
  47641. const browserHash = getOptimizedBrowserHash(
  47642. getDepHash(config, ssr).hash,
  47643. deps,
  47644. timestamp
  47645. );
  47646. const discovered = {};
  47647. for (const id in deps) {
  47648. const src = deps[id];
  47649. discovered[id] = {
  47650. id,
  47651. file: getOptimizedDepPath(id, config, ssr),
  47652. src,
  47653. browserHash,
  47654. exportsData: extractExportsData(src, config, ssr)
  47655. };
  47656. }
  47657. return discovered;
  47658. }
  47659. function depsLogString(qualifiedIds) {
  47660. return colors$1.yellow(qualifiedIds.join(`, `));
  47661. }
  47662. function runOptimizeDeps(resolvedConfig, depsInfo, ssr) {
  47663. const optimizerContext = { cancelled: false };
  47664. const config = {
  47665. ...resolvedConfig,
  47666. command: "build"
  47667. };
  47668. const depsCacheDir = getDepsCacheDir(resolvedConfig, ssr);
  47669. const processingCacheDir = getProcessingDepsCacheDir(resolvedConfig, ssr);
  47670. fs__default.mkdirSync(processingCacheDir, { recursive: true });
  47671. debug$7?.(colors$1.green(`creating package.json in ${processingCacheDir}`));
  47672. fs__default.writeFileSync(
  47673. path$n.resolve(processingCacheDir, "package.json"),
  47674. `{
  47675. "type": "module"
  47676. }
  47677. `
  47678. );
  47679. const metadata = initDepsOptimizerMetadata(config, ssr);
  47680. metadata.browserHash = getOptimizedBrowserHash(
  47681. metadata.hash,
  47682. depsFromOptimizedDepInfo(depsInfo)
  47683. );
  47684. const qualifiedIds = Object.keys(depsInfo);
  47685. let cleaned = false;
  47686. let committed = false;
  47687. const cleanUp = () => {
  47688. if (!cleaned && !committed) {
  47689. cleaned = true;
  47690. debug$7?.(colors$1.green(`removing cache dir ${processingCacheDir}`));
  47691. try {
  47692. fs__default.rmSync(processingCacheDir, { recursive: true, force: true });
  47693. } catch (error) {
  47694. }
  47695. }
  47696. };
  47697. const successfulResult = {
  47698. metadata,
  47699. cancel: cleanUp,
  47700. commit: async () => {
  47701. if (cleaned) {
  47702. throw new Error(
  47703. "Can not commit a Deps Optimization run as it was cancelled"
  47704. );
  47705. }
  47706. committed = true;
  47707. const dataPath = path$n.join(processingCacheDir, METADATA_FILENAME);
  47708. debug$7?.(
  47709. colors$1.green(`creating ${METADATA_FILENAME} in ${processingCacheDir}`)
  47710. );
  47711. fs__default.writeFileSync(
  47712. dataPath,
  47713. stringifyDepsOptimizerMetadata(metadata, depsCacheDir)
  47714. );
  47715. const temporaryPath = depsCacheDir + getTempSuffix();
  47716. const depsCacheDirPresent = fs__default.existsSync(depsCacheDir);
  47717. if (isWindows$3) {
  47718. if (depsCacheDirPresent) {
  47719. debug$7?.(colors$1.green(`renaming ${depsCacheDir} to ${temporaryPath}`));
  47720. await safeRename(depsCacheDir, temporaryPath);
  47721. }
  47722. debug$7?.(
  47723. colors$1.green(`renaming ${processingCacheDir} to ${depsCacheDir}`)
  47724. );
  47725. await safeRename(processingCacheDir, depsCacheDir);
  47726. } else {
  47727. if (depsCacheDirPresent) {
  47728. debug$7?.(colors$1.green(`renaming ${depsCacheDir} to ${temporaryPath}`));
  47729. fs__default.renameSync(depsCacheDir, temporaryPath);
  47730. }
  47731. debug$7?.(
  47732. colors$1.green(`renaming ${processingCacheDir} to ${depsCacheDir}`)
  47733. );
  47734. fs__default.renameSync(processingCacheDir, depsCacheDir);
  47735. }
  47736. if (depsCacheDirPresent) {
  47737. debug$7?.(colors$1.green(`removing cache temp dir ${temporaryPath}`));
  47738. fsp.rm(temporaryPath, { recursive: true, force: true });
  47739. }
  47740. }
  47741. };
  47742. if (!qualifiedIds.length) {
  47743. return {
  47744. cancel: async () => cleanUp(),
  47745. result: Promise.resolve(successfulResult)
  47746. };
  47747. }
  47748. const cancelledResult = {
  47749. metadata,
  47750. commit: async () => cleanUp(),
  47751. cancel: cleanUp
  47752. };
  47753. const start = performance$1.now();
  47754. const preparedRun = prepareEsbuildOptimizerRun(
  47755. resolvedConfig,
  47756. depsInfo,
  47757. ssr,
  47758. processingCacheDir,
  47759. optimizerContext
  47760. );
  47761. const runResult = preparedRun.then(({ context, idToExports }) => {
  47762. function disposeContext() {
  47763. return context?.dispose().catch((e) => {
  47764. config.logger.error("Failed to dispose esbuild context", { error: e });
  47765. });
  47766. }
  47767. if (!context || optimizerContext.cancelled) {
  47768. disposeContext();
  47769. return cancelledResult;
  47770. }
  47771. return context.rebuild().then((result) => {
  47772. const meta = result.metafile;
  47773. const processingCacheDirOutputPath = path$n.relative(
  47774. process.cwd(),
  47775. processingCacheDir
  47776. );
  47777. for (const id in depsInfo) {
  47778. const output = esbuildOutputFromId(
  47779. meta.outputs,
  47780. id,
  47781. processingCacheDir
  47782. );
  47783. const { exportsData, ...info } = depsInfo[id];
  47784. addOptimizedDepInfo(metadata, "optimized", {
  47785. ...info,
  47786. // We only need to hash the output.imports in to check for stability, but adding the hash
  47787. // and file path gives us a unique hash that may be useful for other things in the future
  47788. fileHash: getHash(
  47789. metadata.hash + depsInfo[id].file + JSON.stringify(output.imports)
  47790. ),
  47791. browserHash: metadata.browserHash,
  47792. // After bundling we have more information and can warn the user about legacy packages
  47793. // that require manual configuration
  47794. needsInterop: needsInterop(
  47795. config,
  47796. ssr,
  47797. id,
  47798. idToExports[id],
  47799. output
  47800. )
  47801. });
  47802. }
  47803. for (const o of Object.keys(meta.outputs)) {
  47804. if (!jsMapExtensionRE.test(o)) {
  47805. const id = path$n.relative(processingCacheDirOutputPath, o).replace(jsExtensionRE, "");
  47806. const file = getOptimizedDepPath(id, resolvedConfig, ssr);
  47807. if (!findOptimizedDepInfoInRecord(
  47808. metadata.optimized,
  47809. (depInfo) => depInfo.file === file
  47810. )) {
  47811. addOptimizedDepInfo(metadata, "chunks", {
  47812. id,
  47813. file,
  47814. needsInterop: false,
  47815. browserHash: metadata.browserHash
  47816. });
  47817. }
  47818. }
  47819. }
  47820. debug$7?.(
  47821. `Dependencies bundled in ${(performance$1.now() - start).toFixed(2)}ms`
  47822. );
  47823. return successfulResult;
  47824. }).catch((e) => {
  47825. if (e.errors && e.message.includes("The build was canceled")) {
  47826. return cancelledResult;
  47827. }
  47828. throw e;
  47829. }).finally(() => {
  47830. return disposeContext();
  47831. });
  47832. });
  47833. runResult.catch(() => {
  47834. cleanUp();
  47835. });
  47836. return {
  47837. async cancel() {
  47838. optimizerContext.cancelled = true;
  47839. const { context } = await preparedRun;
  47840. await context?.cancel();
  47841. cleanUp();
  47842. },
  47843. result: runResult
  47844. };
  47845. }
  47846. async function prepareEsbuildOptimizerRun(resolvedConfig, depsInfo, ssr, processingCacheDir, optimizerContext) {
  47847. const config = {
  47848. ...resolvedConfig,
  47849. command: "build"
  47850. };
  47851. const flatIdDeps = {};
  47852. const idToExports = {};
  47853. const optimizeDeps2 = getDepOptimizationConfig(config, ssr);
  47854. const { plugins: pluginsFromConfig = [], ...esbuildOptions } = optimizeDeps2?.esbuildOptions ?? {};
  47855. await Promise.all(
  47856. Object.keys(depsInfo).map(async (id) => {
  47857. const src = depsInfo[id].src;
  47858. const exportsData = await (depsInfo[id].exportsData ?? extractExportsData(src, config, ssr));
  47859. if (exportsData.jsxLoader && !esbuildOptions.loader?.[".js"]) {
  47860. esbuildOptions.loader = {
  47861. ".js": "jsx",
  47862. ...esbuildOptions.loader
  47863. };
  47864. }
  47865. const flatId = flattenId(id);
  47866. flatIdDeps[flatId] = src;
  47867. idToExports[id] = exportsData;
  47868. })
  47869. );
  47870. if (optimizerContext.cancelled) return { context: void 0, idToExports };
  47871. const define = {
  47872. "process.env.NODE_ENV": JSON.stringify(process.env.NODE_ENV || config.mode)
  47873. };
  47874. const platform = ssr && config.ssr?.target !== "webworker" ? "node" : "browser";
  47875. const external = [...optimizeDeps2?.exclude ?? []];
  47876. const plugins = [...pluginsFromConfig];
  47877. if (external.length) {
  47878. plugins.push(esbuildCjsExternalPlugin(external, platform));
  47879. }
  47880. plugins.push(esbuildDepPlugin(flatIdDeps, external, config, ssr));
  47881. const context = await esbuild.context({
  47882. absWorkingDir: process.cwd(),
  47883. entryPoints: Object.keys(flatIdDeps),
  47884. bundle: true,
  47885. // We can't use platform 'neutral', as esbuild has custom handling
  47886. // when the platform is 'node' or 'browser' that can't be emulated
  47887. // by using mainFields and conditions
  47888. platform,
  47889. define,
  47890. format: "esm",
  47891. // See https://github.com/evanw/esbuild/issues/1921#issuecomment-1152991694
  47892. banner: platform === "node" ? {
  47893. js: `import { createRequire } from 'module';const require = createRequire(import.meta.url);`
  47894. } : void 0,
  47895. target: ESBUILD_MODULES_TARGET,
  47896. external,
  47897. logLevel: "error",
  47898. splitting: true,
  47899. sourcemap: true,
  47900. outdir: processingCacheDir,
  47901. ignoreAnnotations: true,
  47902. metafile: true,
  47903. plugins,
  47904. charset: "utf8",
  47905. ...esbuildOptions,
  47906. supported: {
  47907. ...defaultEsbuildSupported,
  47908. ...esbuildOptions.supported
  47909. }
  47910. });
  47911. return { context, idToExports };
  47912. }
  47913. async function addManuallyIncludedOptimizeDeps(deps, config, ssr) {
  47914. const { logger } = config;
  47915. const optimizeDeps2 = getDepOptimizationConfig(config, ssr);
  47916. const optimizeDepsInclude = optimizeDeps2?.include ?? [];
  47917. if (optimizeDepsInclude.length) {
  47918. const unableToOptimize = (id, msg) => {
  47919. if (optimizeDepsInclude.includes(id)) {
  47920. logger.warn(
  47921. `${msg}: ${colors$1.cyan(id)}, present in '${ssr ? "ssr." : ""}optimizeDeps.include'`
  47922. );
  47923. }
  47924. };
  47925. const includes = [...optimizeDepsInclude];
  47926. for (let i = 0; i < includes.length; i++) {
  47927. const id = includes[i];
  47928. if (glob.isDynamicPattern(id)) {
  47929. const globIds = expandGlobIds(id, config);
  47930. includes.splice(i, 1, ...globIds);
  47931. i += globIds.length - 1;
  47932. }
  47933. }
  47934. const resolve = createOptimizeDepsIncludeResolver(config, ssr);
  47935. for (const id of includes) {
  47936. const normalizedId = normalizeId(id);
  47937. if (!deps[normalizedId]) {
  47938. const entry = await resolve(id);
  47939. if (entry) {
  47940. if (isOptimizable(entry, optimizeDeps2)) {
  47941. if (!entry.endsWith("?__vite_skip_optimization")) {
  47942. deps[normalizedId] = entry;
  47943. }
  47944. } else {
  47945. unableToOptimize(id, "Cannot optimize dependency");
  47946. }
  47947. } else {
  47948. unableToOptimize(id, "Failed to resolve dependency");
  47949. }
  47950. }
  47951. }
  47952. }
  47953. }
  47954. function depsFromOptimizedDepInfo(depsInfo) {
  47955. const obj = {};
  47956. for (const key in depsInfo) {
  47957. obj[key] = depsInfo[key].src;
  47958. }
  47959. return obj;
  47960. }
  47961. function getOptimizedDepPath(id, config, ssr) {
  47962. return normalizePath$3(
  47963. path$n.resolve(getDepsCacheDir(config, ssr), flattenId(id) + ".js")
  47964. );
  47965. }
  47966. function getDepsCacheSuffix(ssr) {
  47967. return ssr ? "_ssr" : "";
  47968. }
  47969. function getDepsCacheDir(config, ssr) {
  47970. return getDepsCacheDirPrefix(config) + getDepsCacheSuffix(ssr);
  47971. }
  47972. function getProcessingDepsCacheDir(config, ssr) {
  47973. return getDepsCacheDirPrefix(config) + getDepsCacheSuffix(ssr) + getTempSuffix();
  47974. }
  47975. function getTempSuffix() {
  47976. return "_temp_" + getHash(
  47977. `${process.pid}:${Date.now().toString()}:${Math.random().toString(16).slice(2)}`
  47978. );
  47979. }
  47980. function getDepsCacheDirPrefix(config) {
  47981. return normalizePath$3(path$n.resolve(config.cacheDir, "deps"));
  47982. }
  47983. function createIsOptimizedDepFile(config) {
  47984. const depsCacheDirPrefix = getDepsCacheDirPrefix(config);
  47985. return (id) => id.startsWith(depsCacheDirPrefix);
  47986. }
  47987. function createIsOptimizedDepUrl(config) {
  47988. const { root } = config;
  47989. const depsCacheDir = getDepsCacheDirPrefix(config);
  47990. const depsCacheDirRelative = normalizePath$3(path$n.relative(root, depsCacheDir));
  47991. const depsCacheDirPrefix = depsCacheDirRelative.startsWith("../") ? (
  47992. // if the cache directory is outside root, the url prefix would be something
  47993. // like '/@fs/absolute/path/to/node_modules/.vite'
  47994. `/@fs/${removeLeadingSlash(normalizePath$3(depsCacheDir))}`
  47995. ) : (
  47996. // if the cache directory is inside root, the url prefix would be something
  47997. // like '/node_modules/.vite'
  47998. `/${depsCacheDirRelative}`
  47999. );
  48000. return function isOptimizedDepUrl(url) {
  48001. return url.startsWith(depsCacheDirPrefix);
  48002. };
  48003. }
  48004. function parseDepsOptimizerMetadata(jsonMetadata, depsCacheDir) {
  48005. const { hash, lockfileHash, configHash, browserHash, optimized, chunks } = JSON.parse(jsonMetadata, (key, value) => {
  48006. if (key === "file" || key === "src") {
  48007. return normalizePath$3(path$n.resolve(depsCacheDir, value));
  48008. }
  48009. return value;
  48010. });
  48011. if (!chunks || Object.values(optimized).some((depInfo) => !depInfo.fileHash)) {
  48012. return;
  48013. }
  48014. const metadata = {
  48015. hash,
  48016. lockfileHash,
  48017. configHash,
  48018. browserHash,
  48019. optimized: {},
  48020. discovered: {},
  48021. chunks: {},
  48022. depInfoList: []
  48023. };
  48024. for (const id of Object.keys(optimized)) {
  48025. addOptimizedDepInfo(metadata, "optimized", {
  48026. ...optimized[id],
  48027. id,
  48028. browserHash
  48029. });
  48030. }
  48031. for (const id of Object.keys(chunks)) {
  48032. addOptimizedDepInfo(metadata, "chunks", {
  48033. ...chunks[id],
  48034. id,
  48035. browserHash,
  48036. needsInterop: false
  48037. });
  48038. }
  48039. return metadata;
  48040. }
  48041. function stringifyDepsOptimizerMetadata(metadata, depsCacheDir) {
  48042. const { hash, configHash, lockfileHash, browserHash, optimized, chunks } = metadata;
  48043. return JSON.stringify(
  48044. {
  48045. hash,
  48046. configHash,
  48047. lockfileHash,
  48048. browserHash,
  48049. optimized: Object.fromEntries(
  48050. Object.values(optimized).map(
  48051. ({ id, src, file, fileHash, needsInterop: needsInterop2 }) => [
  48052. id,
  48053. {
  48054. src,
  48055. file,
  48056. fileHash,
  48057. needsInterop: needsInterop2
  48058. }
  48059. ]
  48060. )
  48061. ),
  48062. chunks: Object.fromEntries(
  48063. Object.values(chunks).map(({ id, file }) => [id, { file }])
  48064. )
  48065. },
  48066. (key, value) => {
  48067. if (key === "file" || key === "src") {
  48068. return normalizePath$3(path$n.relative(depsCacheDir, value));
  48069. }
  48070. return value;
  48071. },
  48072. 2
  48073. );
  48074. }
  48075. function esbuildOutputFromId(outputs, id, cacheDirOutputPath) {
  48076. const cwd = process.cwd();
  48077. const flatId = flattenId(id) + ".js";
  48078. const normalizedOutputPath = normalizePath$3(
  48079. path$n.relative(cwd, path$n.join(cacheDirOutputPath, flatId))
  48080. );
  48081. const output = outputs[normalizedOutputPath];
  48082. if (output) {
  48083. return output;
  48084. }
  48085. for (const [key, value] of Object.entries(outputs)) {
  48086. if (normalizePath$3(path$n.relative(cwd, key)) === normalizedOutputPath) {
  48087. return value;
  48088. }
  48089. }
  48090. }
  48091. async function extractExportsData(filePath, config, ssr) {
  48092. await init;
  48093. const optimizeDeps2 = getDepOptimizationConfig(config, ssr);
  48094. const esbuildOptions = optimizeDeps2?.esbuildOptions ?? {};
  48095. if (optimizeDeps2.extensions?.some((ext) => filePath.endsWith(ext))) {
  48096. const result = await build$3({
  48097. ...esbuildOptions,
  48098. entryPoints: [filePath],
  48099. write: false,
  48100. format: "esm"
  48101. });
  48102. const [, exports2, , hasModuleSyntax2] = parse$d(result.outputFiles[0].text);
  48103. return {
  48104. hasModuleSyntax: hasModuleSyntax2,
  48105. exports: exports2.map((e) => e.n)
  48106. };
  48107. }
  48108. let parseResult;
  48109. let usedJsxLoader = false;
  48110. const entryContent = await fsp.readFile(filePath, "utf-8");
  48111. try {
  48112. parseResult = parse$d(entryContent);
  48113. } catch {
  48114. const loader = esbuildOptions.loader?.[path$n.extname(filePath)] || "jsx";
  48115. debug$7?.(
  48116. `Unable to parse: ${filePath}.
  48117. Trying again with a ${loader} transform.`
  48118. );
  48119. const transformed = await transformWithEsbuild(entryContent, filePath, {
  48120. loader
  48121. });
  48122. parseResult = parse$d(transformed.code);
  48123. usedJsxLoader = true;
  48124. }
  48125. const [, exports, , hasModuleSyntax] = parseResult;
  48126. const exportsData = {
  48127. hasModuleSyntax,
  48128. exports: exports.map((e) => e.n),
  48129. jsxLoader: usedJsxLoader
  48130. };
  48131. return exportsData;
  48132. }
  48133. function needsInterop(config, ssr, id, exportsData, output) {
  48134. if (getDepOptimizationConfig(config, ssr)?.needsInterop?.includes(id)) {
  48135. return true;
  48136. }
  48137. const { hasModuleSyntax, exports } = exportsData;
  48138. if (!hasModuleSyntax) {
  48139. return true;
  48140. }
  48141. if (output) {
  48142. const generatedExports = output.exports;
  48143. if (!generatedExports || isSingleDefaultExport(generatedExports) && !isSingleDefaultExport(exports)) {
  48144. return true;
  48145. }
  48146. }
  48147. return false;
  48148. }
  48149. function isSingleDefaultExport(exports) {
  48150. return exports.length === 1 && exports[0] === "default";
  48151. }
  48152. const lockfileFormats = [
  48153. { name: "package-lock.json", checkPatches: true, manager: "npm" },
  48154. { name: "yarn.lock", checkPatches: true, manager: "yarn" },
  48155. // Included in lockfile for v2+
  48156. { name: "pnpm-lock.yaml", checkPatches: false, manager: "pnpm" },
  48157. // Included in lockfile
  48158. { name: "bun.lockb", checkPatches: true, manager: "bun" }
  48159. ].sort((_, { manager }) => {
  48160. return process.env.npm_config_user_agent?.startsWith(manager) ? 1 : -1;
  48161. });
  48162. const lockfileNames = lockfileFormats.map((l) => l.name);
  48163. function getConfigHash(config, ssr) {
  48164. const optimizeDeps2 = getDepOptimizationConfig(config, ssr);
  48165. const content = JSON.stringify(
  48166. {
  48167. mode: process.env.NODE_ENV || config.mode,
  48168. root: config.root,
  48169. resolve: config.resolve,
  48170. assetsInclude: config.assetsInclude,
  48171. plugins: config.plugins.map((p) => p.name),
  48172. optimizeDeps: {
  48173. include: optimizeDeps2?.include ? unique(optimizeDeps2.include).sort() : void 0,
  48174. exclude: optimizeDeps2?.exclude ? unique(optimizeDeps2.exclude).sort() : void 0,
  48175. esbuildOptions: {
  48176. ...optimizeDeps2?.esbuildOptions,
  48177. plugins: optimizeDeps2?.esbuildOptions?.plugins?.map((p) => p.name)
  48178. }
  48179. }
  48180. },
  48181. (_, value) => {
  48182. if (typeof value === "function" || value instanceof RegExp) {
  48183. return value.toString();
  48184. }
  48185. return value;
  48186. }
  48187. );
  48188. return getHash(content);
  48189. }
  48190. function getLockfileHash(config, ssr) {
  48191. const lockfilePath = lookupFile(config.root, lockfileNames);
  48192. let content = lockfilePath ? fs__default.readFileSync(lockfilePath, "utf-8") : "";
  48193. if (lockfilePath) {
  48194. const lockfileName = path$n.basename(lockfilePath);
  48195. const { checkPatches } = lockfileFormats.find(
  48196. (f) => f.name === lockfileName
  48197. );
  48198. if (checkPatches) {
  48199. const fullPath = path$n.join(path$n.dirname(lockfilePath), "patches");
  48200. const stat = tryStatSync(fullPath);
  48201. if (stat?.isDirectory()) {
  48202. content += stat.mtimeMs.toString();
  48203. }
  48204. }
  48205. }
  48206. return getHash(content);
  48207. }
  48208. function getDepHash(config, ssr) {
  48209. const lockfileHash = getLockfileHash(config);
  48210. const configHash = getConfigHash(config, ssr);
  48211. const hash = getHash(lockfileHash + configHash);
  48212. return {
  48213. hash,
  48214. lockfileHash,
  48215. configHash
  48216. };
  48217. }
  48218. function getOptimizedBrowserHash(hash, deps, timestamp = "") {
  48219. return getHash(hash + JSON.stringify(deps) + timestamp);
  48220. }
  48221. function optimizedDepInfoFromId(metadata, id) {
  48222. return metadata.optimized[id] || metadata.discovered[id] || metadata.chunks[id];
  48223. }
  48224. function optimizedDepInfoFromFile(metadata, file) {
  48225. return metadata.depInfoList.find((depInfo) => depInfo.file === file);
  48226. }
  48227. function findOptimizedDepInfoInRecord(dependenciesInfo, callbackFn) {
  48228. for (const o of Object.keys(dependenciesInfo)) {
  48229. const info = dependenciesInfo[o];
  48230. if (callbackFn(info, o)) {
  48231. return info;
  48232. }
  48233. }
  48234. }
  48235. async function optimizedDepNeedsInterop(metadata, file, config, ssr) {
  48236. const depInfo = optimizedDepInfoFromFile(metadata, file);
  48237. if (depInfo?.src && depInfo.needsInterop === void 0) {
  48238. depInfo.exportsData ??= extractExportsData(depInfo.src, config, ssr);
  48239. depInfo.needsInterop = needsInterop(
  48240. config,
  48241. ssr,
  48242. depInfo.id,
  48243. await depInfo.exportsData
  48244. );
  48245. }
  48246. return depInfo?.needsInterop;
  48247. }
  48248. const MAX_TEMP_DIR_AGE_MS = 24 * 60 * 60 * 1e3;
  48249. async function cleanupDepsCacheStaleDirs(config) {
  48250. try {
  48251. const cacheDir = path$n.resolve(config.cacheDir);
  48252. if (fs__default.existsSync(cacheDir)) {
  48253. const dirents = await fsp.readdir(cacheDir, { withFileTypes: true });
  48254. for (const dirent of dirents) {
  48255. if (dirent.isDirectory() && dirent.name.includes("_temp_")) {
  48256. const tempDirPath = path$n.resolve(config.cacheDir, dirent.name);
  48257. const stats = await fsp.stat(tempDirPath).catch((_) => null);
  48258. if (stats?.mtime && Date.now() - stats.mtime.getTime() > MAX_TEMP_DIR_AGE_MS) {
  48259. debug$7?.(`removing stale cache temp dir ${tempDirPath}`);
  48260. await fsp.rm(tempDirPath, { recursive: true, force: true });
  48261. }
  48262. }
  48263. }
  48264. }
  48265. } catch (err) {
  48266. config.logger.error(err);
  48267. }
  48268. }
  48269. const GRACEFUL_RENAME_TIMEOUT = 5e3;
  48270. const safeRename = promisify$4(function gracefulRename(from, to, cb) {
  48271. const start = Date.now();
  48272. let backoff = 0;
  48273. fs__default.rename(from, to, function CB(er) {
  48274. if (er && (er.code === "EACCES" || er.code === "EPERM") && Date.now() - start < GRACEFUL_RENAME_TIMEOUT) {
  48275. setTimeout(function() {
  48276. fs__default.stat(to, function(stater, st) {
  48277. if (stater && stater.code === "ENOENT") fs__default.rename(from, to, CB);
  48278. else CB(er);
  48279. });
  48280. }, backoff);
  48281. if (backoff < 100) backoff += 10;
  48282. return;
  48283. }
  48284. if (cb) cb(er);
  48285. });
  48286. });
  48287. var index$1 = {
  48288. __proto__: null,
  48289. addManuallyIncludedOptimizeDeps: addManuallyIncludedOptimizeDeps,
  48290. addOptimizedDepInfo: addOptimizedDepInfo,
  48291. cleanupDepsCacheStaleDirs: cleanupDepsCacheStaleDirs,
  48292. createIsOptimizedDepFile: createIsOptimizedDepFile,
  48293. createIsOptimizedDepUrl: createIsOptimizedDepUrl,
  48294. depsFromOptimizedDepInfo: depsFromOptimizedDepInfo,
  48295. depsLogString: depsLogString,
  48296. discoverProjectDependencies: discoverProjectDependencies,
  48297. extractExportsData: extractExportsData,
  48298. getDepsCacheDir: getDepsCacheDir,
  48299. getDepsOptimizer: getDepsOptimizer,
  48300. getOptimizedDepPath: getOptimizedDepPath,
  48301. initDepsOptimizer: initDepsOptimizer,
  48302. initDepsOptimizerMetadata: initDepsOptimizerMetadata,
  48303. initDevSsrDepsOptimizer: initDevSsrDepsOptimizer,
  48304. loadCachedDepOptimizationMetadata: loadCachedDepOptimizationMetadata,
  48305. optimizeDeps: optimizeDeps,
  48306. optimizeServerSsrDeps: optimizeServerSsrDeps,
  48307. optimizedDepInfoFromFile: optimizedDepInfoFromFile,
  48308. optimizedDepInfoFromId: optimizedDepInfoFromId,
  48309. optimizedDepNeedsInterop: optimizedDepNeedsInterop,
  48310. runOptimizeDeps: runOptimizeDeps,
  48311. toDiscoveredDependencies: toDiscoveredDependencies
  48312. };
  48313. function totalist(dir, callback, pre='') {
  48314. dir = resolve$3('.', dir);
  48315. let arr = readdirSync(dir);
  48316. let i=0, abs, stats;
  48317. for (; i < arr.length; i++) {
  48318. abs = join$1(dir, arr[i]);
  48319. stats = statSync$1(abs);
  48320. stats.isDirectory()
  48321. ? totalist(abs, callback, join$1(pre, arr[i]))
  48322. : callback(join$1(pre, arr[i]), abs, stats);
  48323. }
  48324. }
  48325. /**
  48326. * @typedef ParsedURL
  48327. * @type {import('.').ParsedURL}
  48328. */
  48329. /**
  48330. * @typedef Request
  48331. * @property {string} url
  48332. * @property {ParsedURL} _parsedUrl
  48333. */
  48334. /**
  48335. * @param {Request} req
  48336. * @returns {ParsedURL|void}
  48337. */
  48338. function parse$5(req) {
  48339. let raw = req.url;
  48340. if (raw == null) return;
  48341. let prev = req._parsedUrl;
  48342. if (prev && prev.raw === raw) return prev;
  48343. let pathname=raw, search='', query;
  48344. if (raw.length > 1) {
  48345. let idx = raw.indexOf('?', 1);
  48346. if (idx !== -1) {
  48347. search = raw.substring(idx);
  48348. pathname = raw.substring(0, idx);
  48349. if (search.length > 1) {
  48350. query = qs.parse(search.substring(1));
  48351. }
  48352. }
  48353. }
  48354. return req._parsedUrl = { pathname, search, query, raw };
  48355. }
  48356. const noop$2 = () => {};
  48357. function isMatch(uri, arr) {
  48358. for (let i=0; i < arr.length; i++) {
  48359. if (arr[i].test(uri)) return true;
  48360. }
  48361. }
  48362. function toAssume(uri, extns) {
  48363. let i=0, x, len=uri.length - 1;
  48364. if (uri.charCodeAt(len) === 47) {
  48365. uri = uri.substring(0, len);
  48366. }
  48367. let arr=[], tmp=`${uri}/index`;
  48368. for (; i < extns.length; i++) {
  48369. x = extns[i] ? `.${extns[i]}` : '';
  48370. if (uri) arr.push(uri + x);
  48371. arr.push(tmp + x);
  48372. }
  48373. return arr;
  48374. }
  48375. function viaCache(cache, uri, extns) {
  48376. let i=0, data, arr=toAssume(uri, extns);
  48377. for (; i < arr.length; i++) {
  48378. if (data = cache[arr[i]]) return data;
  48379. }
  48380. }
  48381. function viaLocal(dir, isEtag, uri, extns, shouldServe) {
  48382. let i=0, arr=toAssume(uri, extns);
  48383. let abs, stats, name, headers;
  48384. for (; i < arr.length; i++) {
  48385. abs = normalize$1(join$1(dir, name=arr[i]));
  48386. if (abs.startsWith(dir) && require$$0$2.existsSync(abs)) {
  48387. stats = require$$0$2.statSync(abs);
  48388. if (stats.isDirectory()) continue;
  48389. if (shouldServe && !shouldServe(abs)) continue;
  48390. headers = toHeaders(name, stats, isEtag);
  48391. headers['Cache-Control'] = isEtag ? 'no-cache' : 'no-store';
  48392. return { abs, stats, headers };
  48393. }
  48394. }
  48395. }
  48396. function is404(req, res) {
  48397. return (res.statusCode=404,res.end());
  48398. }
  48399. function send$1(req, res, file, stats, headers) {
  48400. let code=200, tmp, opts={};
  48401. headers = { ...headers };
  48402. for (let key in headers) {
  48403. tmp = res.getHeader(key);
  48404. if (tmp) headers[key] = tmp;
  48405. }
  48406. if (tmp = res.getHeader('content-type')) {
  48407. headers['Content-Type'] = tmp;
  48408. }
  48409. if (req.headers.range) {
  48410. code = 206;
  48411. let [x, y] = req.headers.range.replace('bytes=', '').split('-');
  48412. let end = opts.end = parseInt(y, 10) || stats.size - 1;
  48413. let start = opts.start = parseInt(x, 10) || 0;
  48414. if (end >= stats.size) {
  48415. end = stats.size - 1;
  48416. }
  48417. if (start >= stats.size) {
  48418. res.setHeader('Content-Range', `bytes */${stats.size}`);
  48419. res.statusCode = 416;
  48420. return res.end();
  48421. }
  48422. headers['Content-Range'] = `bytes ${start}-${end}/${stats.size}`;
  48423. headers['Content-Length'] = (end - start + 1);
  48424. headers['Accept-Ranges'] = 'bytes';
  48425. }
  48426. res.writeHead(code, headers);
  48427. require$$0$2.createReadStream(file, opts).pipe(res);
  48428. }
  48429. const ENCODING = {
  48430. '.br': 'br',
  48431. '.gz': 'gzip',
  48432. };
  48433. function toHeaders(name, stats, isEtag) {
  48434. let enc = ENCODING[name.slice(-3)];
  48435. let ctype = lookup(name.slice(0, enc && -3)) || '';
  48436. if (ctype === 'text/html') ctype += ';charset=utf-8';
  48437. let headers = {
  48438. 'Content-Length': stats.size,
  48439. 'Content-Type': ctype,
  48440. 'Last-Modified': stats.mtime.toUTCString(),
  48441. };
  48442. if (enc) headers['Content-Encoding'] = enc;
  48443. if (isEtag) headers['ETag'] = `W/"${stats.size}-${stats.mtime.getTime()}"`;
  48444. return headers;
  48445. }
  48446. function sirv (dir, opts={}) {
  48447. dir = resolve$3(dir || '.');
  48448. let isNotFound = opts.onNoMatch || is404;
  48449. let setHeaders = opts.setHeaders || noop$2;
  48450. let extensions = opts.extensions || ['html', 'htm'];
  48451. let gzips = opts.gzip && extensions.map(x => `${x}.gz`).concat('gz');
  48452. let brots = opts.brotli && extensions.map(x => `${x}.br`).concat('br');
  48453. const FILES = {};
  48454. let fallback = '/';
  48455. let isEtag = !!opts.etag;
  48456. let isSPA = !!opts.single;
  48457. if (typeof opts.single === 'string') {
  48458. let idx = opts.single.lastIndexOf('.');
  48459. fallback += !!~idx ? opts.single.substring(0, idx) : opts.single;
  48460. }
  48461. let ignores = [];
  48462. if (opts.ignores !== false) {
  48463. ignores.push(/[/]([A-Za-z\s\d~$._-]+\.\w+){1,}$/); // any extn
  48464. if (opts.dotfiles) ignores.push(/\/\.\w/);
  48465. else ignores.push(/\/\.well-known/);
  48466. [].concat(opts.ignores || []).forEach(x => {
  48467. ignores.push(new RegExp(x, 'i'));
  48468. });
  48469. }
  48470. let cc = opts.maxAge != null && `public,max-age=${opts.maxAge}`;
  48471. if (cc && opts.immutable) cc += ',immutable';
  48472. else if (cc && opts.maxAge === 0) cc += ',must-revalidate';
  48473. if (!opts.dev) {
  48474. totalist(dir, (name, abs, stats) => {
  48475. if (/\.well-known[\\+\/]/.test(name)) ; // keep
  48476. else if (!opts.dotfiles && /(^\.|[\\+|\/+]\.)/.test(name)) return;
  48477. let headers = toHeaders(name, stats, isEtag);
  48478. if (cc) headers['Cache-Control'] = cc;
  48479. FILES['/' + name.normalize().replace(/\\+/g, '/')] = { abs, stats, headers };
  48480. });
  48481. }
  48482. let lookup = opts.dev ? viaLocal.bind(0, dir.endsWith(sep$1) ? dir : dir + sep$1, isEtag) : viaCache.bind(0, FILES);
  48483. return function (req, res, next) {
  48484. let extns = [''];
  48485. let pathname = parse$5(req).pathname;
  48486. let val = req.headers['accept-encoding'] || '';
  48487. if (gzips && val.includes('gzip')) extns.unshift(...gzips);
  48488. if (brots && /(br|brotli)/i.test(val)) extns.unshift(...brots);
  48489. extns.push(...extensions); // [...br, ...gz, orig, ...exts]
  48490. if (pathname.indexOf('%') !== -1) {
  48491. try { pathname = decodeURI(pathname); }
  48492. catch (err) { /* malform uri */ }
  48493. }
  48494. let data = lookup(pathname, extns, opts.shouldServe) || isSPA && !isMatch(pathname, ignores) && lookup(fallback, extns, opts.shouldServe);
  48495. if (!data) return next ? next() : isNotFound(req, res);
  48496. if (isEtag && req.headers['if-none-match'] === data.headers['ETag']) {
  48497. res.writeHead(304);
  48498. return res.end();
  48499. }
  48500. if (gzips || brots) {
  48501. res.setHeader('Vary', 'Accept-Encoding');
  48502. }
  48503. setHeaders(res, pathname, data.stats);
  48504. send$1(req, res, data.abs, data.stats, data.headers);
  48505. };
  48506. }
  48507. const knownJavascriptExtensionRE = /\.[tj]sx?$/;
  48508. const ERR_DENIED_FILE = "ERR_DENIED_FILE";
  48509. const sirvOptions = ({
  48510. server,
  48511. getHeaders,
  48512. disableFsServeCheck
  48513. }) => {
  48514. return {
  48515. dev: true,
  48516. etag: true,
  48517. extensions: [],
  48518. setHeaders(res, pathname) {
  48519. if (knownJavascriptExtensionRE.test(pathname)) {
  48520. res.setHeader("Content-Type", "text/javascript");
  48521. }
  48522. const headers = getHeaders();
  48523. if (headers) {
  48524. for (const name in headers) {
  48525. res.setHeader(name, headers[name]);
  48526. }
  48527. }
  48528. },
  48529. shouldServe: disableFsServeCheck ? void 0 : (filePath) => {
  48530. const servingAccessResult = checkLoadingAccess(server, filePath);
  48531. if (servingAccessResult === "denied") {
  48532. const error = new Error("denied access");
  48533. error.code = ERR_DENIED_FILE;
  48534. error.path = filePath;
  48535. throw error;
  48536. }
  48537. if (servingAccessResult === "fallback") {
  48538. return false;
  48539. }
  48540. return true;
  48541. }
  48542. };
  48543. };
  48544. function servePublicMiddleware(server, publicFiles) {
  48545. const dir = server.config.publicDir;
  48546. const serve = sirv(
  48547. dir,
  48548. sirvOptions({
  48549. server,
  48550. getHeaders: () => server.config.server.headers,
  48551. disableFsServeCheck: true
  48552. })
  48553. );
  48554. const toFilePath = (url) => {
  48555. let filePath = cleanUrl(url);
  48556. if (filePath.indexOf("%") !== -1) {
  48557. try {
  48558. filePath = decodeURI(filePath);
  48559. } catch (err) {
  48560. }
  48561. }
  48562. return normalizePath$3(filePath);
  48563. };
  48564. return function viteServePublicMiddleware(req, res, next) {
  48565. if (publicFiles && !publicFiles.has(toFilePath(req.url)) || isImportRequest(req.url) || isInternalRequest(req.url) || // for `/public-file.js?url` to be transformed
  48566. urlRE$1.test(req.url)) {
  48567. return next();
  48568. }
  48569. serve(req, res, next);
  48570. };
  48571. }
  48572. function serveStaticMiddleware(server) {
  48573. const dir = server.config.root;
  48574. const serve = sirv(
  48575. dir,
  48576. sirvOptions({
  48577. server,
  48578. getHeaders: () => server.config.server.headers
  48579. })
  48580. );
  48581. return function viteServeStaticMiddleware(req, res, next) {
  48582. const cleanedUrl = cleanUrl(req.url);
  48583. if (cleanedUrl[cleanedUrl.length - 1] === "/" || path$n.extname(cleanedUrl) === ".html" || isInternalRequest(req.url)) {
  48584. return next();
  48585. }
  48586. const url = new URL(req.url.replace(/^\/{2,}/, "/"), "http://example.com");
  48587. const pathname = decodeURI(url.pathname);
  48588. let redirectedPathname;
  48589. for (const { find, replacement } of server.config.resolve.alias) {
  48590. const matches = typeof find === "string" ? pathname.startsWith(find) : find.test(pathname);
  48591. if (matches) {
  48592. redirectedPathname = pathname.replace(find, replacement);
  48593. break;
  48594. }
  48595. }
  48596. if (redirectedPathname) {
  48597. if (redirectedPathname.startsWith(withTrailingSlash(dir))) {
  48598. redirectedPathname = redirectedPathname.slice(dir.length);
  48599. }
  48600. }
  48601. const resolvedPathname = redirectedPathname || pathname;
  48602. path$n.resolve(dir, removeLeadingSlash(resolvedPathname));
  48603. if (redirectedPathname) {
  48604. url.pathname = encodeURI(redirectedPathname);
  48605. req.url = url.href.slice(url.origin.length);
  48606. }
  48607. try {
  48608. serve(req, res, next);
  48609. } catch (e) {
  48610. if (e && "code" in e && e.code === ERR_DENIED_FILE) {
  48611. respondWithAccessDenied(e.path, server, res);
  48612. return;
  48613. }
  48614. throw e;
  48615. }
  48616. };
  48617. }
  48618. function serveRawFsMiddleware(server) {
  48619. const serveFromRoot = sirv(
  48620. "/",
  48621. sirvOptions({
  48622. server,
  48623. getHeaders: () => server.config.server.headers
  48624. })
  48625. );
  48626. return function viteServeRawFsMiddleware(req, res, next) {
  48627. const url = new URL(req.url.replace(/^\/{2,}/, "/"), "http://example.com");
  48628. if (url.pathname.startsWith(FS_PREFIX)) {
  48629. const pathname = decodeURI(url.pathname);
  48630. let newPathname = pathname.slice(FS_PREFIX.length);
  48631. if (isWindows$3) newPathname = newPathname.replace(/^[A-Z]:/i, "");
  48632. url.pathname = encodeURI(newPathname);
  48633. req.url = url.href.slice(url.origin.length);
  48634. try {
  48635. serveFromRoot(req, res, next);
  48636. } catch (e) {
  48637. if (e && "code" in e && e.code === ERR_DENIED_FILE) {
  48638. respondWithAccessDenied(e.path, server, res);
  48639. return;
  48640. }
  48641. throw e;
  48642. }
  48643. } else {
  48644. next();
  48645. }
  48646. };
  48647. }
  48648. function isFileServingAllowed(url, server) {
  48649. if (!server.config.server.fs.strict) return true;
  48650. const filePath = fsPathFromUrl(url);
  48651. return isFileLoadingAllowed(server, filePath);
  48652. }
  48653. function isUriInFilePath(uri, filePath) {
  48654. return isSameFileUri(uri, filePath) || isParentDirectory(uri, filePath);
  48655. }
  48656. function isFileLoadingAllowed(server, filePath) {
  48657. const { fs } = server.config.server;
  48658. if (!fs.strict) return true;
  48659. const filePathWithoutTrailingSlash = filePath.endsWith("/") ? filePath.slice(0, -1) : filePath;
  48660. if (server._fsDenyGlob(filePathWithoutTrailingSlash)) return false;
  48661. if (server.moduleGraph.safeModulesPath.has(filePath)) return true;
  48662. if (fs.allow.some((uri) => isUriInFilePath(uri, filePath))) return true;
  48663. return false;
  48664. }
  48665. function checkLoadingAccess(server, path2) {
  48666. if (isFileLoadingAllowed(server, slash$1(path2))) {
  48667. return "allowed";
  48668. }
  48669. if (isFileReadable(path2)) {
  48670. return "denied";
  48671. }
  48672. return "fallback";
  48673. }
  48674. function checkServingAccess(url, server) {
  48675. if (isFileServingAllowed(url, server)) {
  48676. return "allowed";
  48677. }
  48678. if (isFileReadable(cleanUrl(url))) {
  48679. return "denied";
  48680. }
  48681. return "fallback";
  48682. }
  48683. function respondWithAccessDenied(url, server, res) {
  48684. const urlMessage = `The request url "${url}" is outside of Vite serving allow list.`;
  48685. const hintMessage = `
  48686. ${server.config.server.fs.allow.map((i) => `- ${i}`).join("\n")}
  48687. Refer to docs https://vite.dev/config/server-options.html#server-fs-allow for configurations and more details.`;
  48688. server.config.logger.error(urlMessage);
  48689. server.config.logger.warnOnce(hintMessage + "\n");
  48690. res.statusCode = 403;
  48691. res.write(renderRestrictedErrorHTML(urlMessage + "\n" + hintMessage));
  48692. res.end();
  48693. }
  48694. function renderRestrictedErrorHTML(msg) {
  48695. const html = String.raw;
  48696. return html`
  48697. <body>
  48698. <h1>403 Restricted</h1>
  48699. <p>${escapeHtml$2(msg).replace(/\n/g, "<br/>")}</p>
  48700. <style>
  48701. body {
  48702. padding: 1em 2em;
  48703. }
  48704. </style>
  48705. </body>
  48706. `;
  48707. }
  48708. const ERR_LOAD_URL = "ERR_LOAD_URL";
  48709. const ERR_LOAD_PUBLIC_URL = "ERR_LOAD_PUBLIC_URL";
  48710. const ERR_DENIED_ID = "ERR_DENIED_ID";
  48711. const debugLoad = createDebugger("vite:load");
  48712. const debugTransform = createDebugger("vite:transform");
  48713. const debugCache$1 = createDebugger("vite:cache");
  48714. function transformRequest(url, server, options = {}) {
  48715. if (server._restartPromise && !options.ssr) throwClosedServerError();
  48716. const cacheKey = (options.ssr ? "ssr:" : options.html ? "html:" : "") + url;
  48717. const timestamp = Date.now();
  48718. const pending = server._pendingRequests.get(cacheKey);
  48719. if (pending) {
  48720. return server.moduleGraph.getModuleByUrl(removeTimestampQuery(url), options.ssr).then((module) => {
  48721. if (!module || pending.timestamp > module.lastInvalidationTimestamp) {
  48722. return pending.request;
  48723. } else {
  48724. pending.abort();
  48725. return transformRequest(url, server, options);
  48726. }
  48727. });
  48728. }
  48729. const request = doTransform(url, server, options, timestamp);
  48730. let cleared = false;
  48731. const clearCache = () => {
  48732. if (!cleared) {
  48733. server._pendingRequests.delete(cacheKey);
  48734. cleared = true;
  48735. }
  48736. };
  48737. server._pendingRequests.set(cacheKey, {
  48738. request,
  48739. timestamp,
  48740. abort: clearCache
  48741. });
  48742. return request.finally(clearCache);
  48743. }
  48744. async function doTransform(url, server, options, timestamp) {
  48745. url = removeTimestampQuery(url);
  48746. const { config, pluginContainer } = server;
  48747. const ssr = !!options.ssr;
  48748. if (ssr && isDepsOptimizerEnabled(config, true)) {
  48749. await initDevSsrDepsOptimizer(config, server);
  48750. }
  48751. let module = await server.moduleGraph.getModuleByUrl(url, ssr);
  48752. if (module) {
  48753. const cached = await getCachedTransformResult(
  48754. url,
  48755. module,
  48756. server,
  48757. ssr,
  48758. timestamp
  48759. );
  48760. if (cached) return cached;
  48761. }
  48762. const resolved = module ? void 0 : await pluginContainer.resolveId(url, void 0, { ssr }) ?? void 0;
  48763. const id = module?.id ?? resolved?.id ?? url;
  48764. module ??= server.moduleGraph.getModuleById(id);
  48765. if (module) {
  48766. await server.moduleGraph._ensureEntryFromUrl(url, ssr, void 0, resolved);
  48767. const cached = await getCachedTransformResult(
  48768. url,
  48769. module,
  48770. server,
  48771. ssr,
  48772. timestamp
  48773. );
  48774. if (cached) return cached;
  48775. }
  48776. const result = loadAndTransform(
  48777. id,
  48778. url,
  48779. server,
  48780. options,
  48781. timestamp,
  48782. module,
  48783. resolved
  48784. );
  48785. if (!ssr) {
  48786. const depsOptimizer = getDepsOptimizer(config, ssr);
  48787. if (!depsOptimizer?.isOptimizedDepFile(id)) {
  48788. server._registerRequestProcessing(id, () => result);
  48789. }
  48790. }
  48791. return result;
  48792. }
  48793. async function getCachedTransformResult(url, module, server, ssr, timestamp) {
  48794. const prettyUrl = debugCache$1 ? prettifyUrl(url, server.config.root) : "";
  48795. const softInvalidatedTransformResult = module && await handleModuleSoftInvalidation(module, ssr, timestamp, server);
  48796. if (softInvalidatedTransformResult) {
  48797. debugCache$1?.(`[memory-hmr] ${prettyUrl}`);
  48798. return softInvalidatedTransformResult;
  48799. }
  48800. const cached = module && (ssr ? module.ssrTransformResult : module.transformResult);
  48801. if (cached) {
  48802. debugCache$1?.(`[memory] ${prettyUrl}`);
  48803. return cached;
  48804. }
  48805. }
  48806. async function loadAndTransform(id, url, server, options, timestamp, mod, resolved) {
  48807. const { config, pluginContainer, moduleGraph } = server;
  48808. const { logger } = config;
  48809. const prettyUrl = debugLoad || debugTransform ? prettifyUrl(url, config.root) : "";
  48810. const ssr = !!options.ssr;
  48811. const file = cleanUrl(id);
  48812. if (options.allowId && !options.allowId(id)) {
  48813. const err = new Error(`Denied ID ${id}`);
  48814. err.code = ERR_DENIED_ID;
  48815. throw err;
  48816. }
  48817. let code = null;
  48818. let map = null;
  48819. const loadStart = debugLoad ? performance$1.now() : 0;
  48820. const loadResult = await pluginContainer.load(id, { ssr });
  48821. if (loadResult == null) {
  48822. if (options.html && !id.endsWith(".html")) {
  48823. return null;
  48824. }
  48825. if (options.ssr || isFileServingAllowed(file, server)) {
  48826. try {
  48827. code = await fsp.readFile(file, "utf-8");
  48828. debugLoad?.(`${timeFrom(loadStart)} [fs] ${prettyUrl}`);
  48829. } catch (e) {
  48830. if (e.code !== "ENOENT") {
  48831. if (e.code === "EISDIR") {
  48832. e.message = `${e.message} ${file}`;
  48833. }
  48834. throw e;
  48835. }
  48836. }
  48837. if (code != null) {
  48838. ensureWatchedFile(server.watcher, file, config.root);
  48839. }
  48840. }
  48841. if (code) {
  48842. try {
  48843. const extracted = await extractSourcemapFromFile(code, file);
  48844. if (extracted) {
  48845. code = extracted.code;
  48846. map = extracted.map;
  48847. }
  48848. } catch (e) {
  48849. logger.warn(`Failed to load source map for ${file}.
  48850. ${e}`, {
  48851. timestamp: true
  48852. });
  48853. }
  48854. }
  48855. } else {
  48856. debugLoad?.(`${timeFrom(loadStart)} [plugin] ${prettyUrl}`);
  48857. if (isObject$1(loadResult)) {
  48858. code = loadResult.code;
  48859. map = loadResult.map;
  48860. } else {
  48861. code = loadResult;
  48862. }
  48863. }
  48864. if (code == null) {
  48865. const isPublicFile = checkPublicFile(url, config);
  48866. let publicDirName = path$n.relative(config.root, config.publicDir);
  48867. if (publicDirName[0] !== ".") publicDirName = "/" + publicDirName;
  48868. 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?`;
  48869. const importerMod = server.moduleGraph.idToModuleMap.get(id)?.importers.values().next().value;
  48870. const importer = importerMod?.file || importerMod?.url;
  48871. const err = new Error(
  48872. `Failed to load url ${url} (resolved id: ${id})${importer ? ` in ${importer}` : ""}. ${msg}`
  48873. );
  48874. err.code = isPublicFile ? ERR_LOAD_PUBLIC_URL : ERR_LOAD_URL;
  48875. throw err;
  48876. }
  48877. if (server._restartPromise && !ssr) throwClosedServerError();
  48878. mod ??= await moduleGraph._ensureEntryFromUrl(url, ssr, void 0, resolved);
  48879. const transformStart = debugTransform ? performance$1.now() : 0;
  48880. const transformResult = await pluginContainer.transform(code, id, {
  48881. inMap: map,
  48882. ssr
  48883. });
  48884. const originalCode = code;
  48885. if (transformResult == null || isObject$1(transformResult) && transformResult.code == null) {
  48886. debugTransform?.(
  48887. timeFrom(transformStart) + colors$1.dim(` [skipped] ${prettyUrl}`)
  48888. );
  48889. } else {
  48890. debugTransform?.(`${timeFrom(transformStart)} ${prettyUrl}`);
  48891. code = transformResult.code;
  48892. map = transformResult.map;
  48893. }
  48894. let normalizedMap;
  48895. if (typeof map === "string") {
  48896. normalizedMap = JSON.parse(map);
  48897. } else if (map) {
  48898. normalizedMap = map;
  48899. } else {
  48900. normalizedMap = null;
  48901. }
  48902. if (normalizedMap && "version" in normalizedMap && mod.file) {
  48903. if (normalizedMap.mappings) {
  48904. await injectSourcesContent(normalizedMap, mod.file, logger);
  48905. }
  48906. const sourcemapPath = `${mod.file}.map`;
  48907. applySourcemapIgnoreList(
  48908. normalizedMap,
  48909. sourcemapPath,
  48910. config.server.sourcemapIgnoreList,
  48911. logger
  48912. );
  48913. if (path$n.isAbsolute(mod.file)) {
  48914. let modDirname;
  48915. for (let sourcesIndex = 0; sourcesIndex < normalizedMap.sources.length; ++sourcesIndex) {
  48916. const sourcePath = normalizedMap.sources[sourcesIndex];
  48917. if (sourcePath) {
  48918. if (path$n.isAbsolute(sourcePath)) {
  48919. modDirname ??= path$n.dirname(mod.file);
  48920. normalizedMap.sources[sourcesIndex] = path$n.relative(
  48921. modDirname,
  48922. sourcePath
  48923. );
  48924. }
  48925. }
  48926. }
  48927. }
  48928. }
  48929. if (server._restartPromise && !ssr) throwClosedServerError();
  48930. const result = ssr && !server.config.experimental.skipSsrTransform ? await server.ssrTransform(code, normalizedMap, url, originalCode) : {
  48931. code,
  48932. map: normalizedMap,
  48933. etag: getEtag(code, { weak: true })
  48934. };
  48935. if (timestamp > mod.lastInvalidationTimestamp)
  48936. moduleGraph.updateModuleTransformResult(mod, result, ssr);
  48937. return result;
  48938. }
  48939. async function handleModuleSoftInvalidation(mod, ssr, timestamp, server) {
  48940. const transformResult = ssr ? mod.ssrInvalidationState : mod.invalidationState;
  48941. if (ssr) mod.ssrInvalidationState = void 0;
  48942. else mod.invalidationState = void 0;
  48943. if (!transformResult || transformResult === "HARD_INVALIDATED") return;
  48944. if (ssr ? mod.ssrTransformResult : mod.transformResult) {
  48945. throw new Error(
  48946. `Internal server error: Soft-invalidated module "${mod.url}" should not have existing transform result`
  48947. );
  48948. }
  48949. let result;
  48950. if (ssr) {
  48951. result = transformResult;
  48952. } else {
  48953. await init;
  48954. const source = transformResult.code;
  48955. const s = new MagicString(source);
  48956. const [imports] = parse$d(source, mod.id || void 0);
  48957. for (const imp of imports) {
  48958. let rawUrl = source.slice(imp.s, imp.e);
  48959. if (rawUrl === "import.meta") continue;
  48960. const hasQuotes = rawUrl[0] === '"' || rawUrl[0] === "'";
  48961. if (hasQuotes) {
  48962. rawUrl = rawUrl.slice(1, -1);
  48963. }
  48964. const urlWithoutTimestamp = removeTimestampQuery(rawUrl);
  48965. const hmrUrl = unwrapId$1(
  48966. stripBase(removeImportQuery(urlWithoutTimestamp), server.config.base)
  48967. );
  48968. for (const importedMod of mod.clientImportedModules) {
  48969. if (importedMod.url !== hmrUrl) continue;
  48970. if (importedMod.lastHMRTimestamp > 0) {
  48971. const replacedUrl = injectQuery(
  48972. urlWithoutTimestamp,
  48973. `t=${importedMod.lastHMRTimestamp}`
  48974. );
  48975. const start = hasQuotes ? imp.s + 1 : imp.s;
  48976. const end = hasQuotes ? imp.e - 1 : imp.e;
  48977. s.overwrite(start, end, replacedUrl);
  48978. }
  48979. if (imp.d === -1 && server.config.server.preTransformRequests) {
  48980. server.warmupRequest(hmrUrl, { ssr });
  48981. }
  48982. break;
  48983. }
  48984. }
  48985. const code = s.toString();
  48986. result = {
  48987. ...transformResult,
  48988. code,
  48989. etag: getEtag(code, { weak: true })
  48990. };
  48991. }
  48992. if (timestamp > mod.lastInvalidationTimestamp)
  48993. server.moduleGraph.updateModuleTransformResult(mod, result, ssr);
  48994. return result;
  48995. }
  48996. function analyzeImportedModDifference(mod, rawId, moduleType, metadata) {
  48997. if (metadata?.isDynamicImport) return;
  48998. if (metadata?.importedNames?.length) {
  48999. const missingBindings = metadata.importedNames.filter((s) => !(s in mod));
  49000. if (missingBindings.length) {
  49001. const lastBinding = missingBindings[missingBindings.length - 1];
  49002. if (moduleType === "module") {
  49003. throw new SyntaxError(
  49004. `[vite] The requested module '${rawId}' does not provide an export named '${lastBinding}'`
  49005. );
  49006. } else {
  49007. 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.
  49008. CommonJS modules can always be imported via the default export, for example using:
  49009. import pkg from '${rawId}';
  49010. const {${missingBindings.join(", ")}} = pkg;
  49011. `);
  49012. }
  49013. }
  49014. }
  49015. }
  49016. /**
  49017. * @param {import('estree').Node} param
  49018. * @returns {string[]}
  49019. */
  49020. function extract_names(param) {
  49021. return extract_identifiers(param).map((node) => node.name);
  49022. }
  49023. /**
  49024. * @param {import('estree').Node} param
  49025. * @param {import('estree').Identifier[]} nodes
  49026. * @returns {import('estree').Identifier[]}
  49027. */
  49028. function extract_identifiers(param, nodes = []) {
  49029. switch (param.type) {
  49030. case 'Identifier':
  49031. nodes.push(param);
  49032. break;
  49033. case 'MemberExpression':
  49034. let object = param;
  49035. while (object.type === 'MemberExpression') {
  49036. object = /** @type {any} */ (object.object);
  49037. }
  49038. nodes.push(/** @type {any} */ (object));
  49039. break;
  49040. case 'ObjectPattern':
  49041. for (const prop of param.properties) {
  49042. if (prop.type === 'RestElement') {
  49043. extract_identifiers(prop.argument, nodes);
  49044. } else {
  49045. extract_identifiers(prop.value, nodes);
  49046. }
  49047. }
  49048. break;
  49049. case 'ArrayPattern':
  49050. for (const element of param.elements) {
  49051. if (element) extract_identifiers(element, nodes);
  49052. }
  49053. break;
  49054. case 'RestElement':
  49055. extract_identifiers(param.argument, nodes);
  49056. break;
  49057. case 'AssignmentPattern':
  49058. extract_identifiers(param.left, nodes);
  49059. break;
  49060. }
  49061. return nodes;
  49062. }
  49063. /**
  49064. * @typedef { import('estree').Node} Node
  49065. * @typedef {{
  49066. * skip: () => void;
  49067. * remove: () => void;
  49068. * replace: (node: Node) => void;
  49069. * }} WalkerContext
  49070. */
  49071. class WalkerBase {
  49072. constructor() {
  49073. /** @type {boolean} */
  49074. this.should_skip = false;
  49075. /** @type {boolean} */
  49076. this.should_remove = false;
  49077. /** @type {Node | null} */
  49078. this.replacement = null;
  49079. /** @type {WalkerContext} */
  49080. this.context = {
  49081. skip: () => (this.should_skip = true),
  49082. remove: () => (this.should_remove = true),
  49083. replace: (node) => (this.replacement = node)
  49084. };
  49085. }
  49086. /**
  49087. * @template {Node} Parent
  49088. * @param {Parent | null | undefined} parent
  49089. * @param {keyof Parent | null | undefined} prop
  49090. * @param {number | null | undefined} index
  49091. * @param {Node} node
  49092. */
  49093. replace(parent, prop, index, node) {
  49094. if (parent && prop) {
  49095. if (index != null) {
  49096. /** @type {Array<Node>} */ (parent[prop])[index] = node;
  49097. } else {
  49098. /** @type {Node} */ (parent[prop]) = node;
  49099. }
  49100. }
  49101. }
  49102. /**
  49103. * @template {Node} Parent
  49104. * @param {Parent | null | undefined} parent
  49105. * @param {keyof Parent | null | undefined} prop
  49106. * @param {number | null | undefined} index
  49107. */
  49108. remove(parent, prop, index) {
  49109. if (parent && prop) {
  49110. if (index !== null && index !== undefined) {
  49111. /** @type {Array<Node>} */ (parent[prop]).splice(index, 1);
  49112. } else {
  49113. delete parent[prop];
  49114. }
  49115. }
  49116. }
  49117. }
  49118. /**
  49119. * @typedef { import('estree').Node} Node
  49120. * @typedef { import('./walker.js').WalkerContext} WalkerContext
  49121. * @typedef {(
  49122. * this: WalkerContext,
  49123. * node: Node,
  49124. * parent: Node | null,
  49125. * key: string | number | symbol | null | undefined,
  49126. * index: number | null | undefined
  49127. * ) => void} SyncHandler
  49128. */
  49129. class SyncWalker extends WalkerBase {
  49130. /**
  49131. *
  49132. * @param {SyncHandler} [enter]
  49133. * @param {SyncHandler} [leave]
  49134. */
  49135. constructor(enter, leave) {
  49136. super();
  49137. /** @type {boolean} */
  49138. this.should_skip = false;
  49139. /** @type {boolean} */
  49140. this.should_remove = false;
  49141. /** @type {Node | null} */
  49142. this.replacement = null;
  49143. /** @type {WalkerContext} */
  49144. this.context = {
  49145. skip: () => (this.should_skip = true),
  49146. remove: () => (this.should_remove = true),
  49147. replace: (node) => (this.replacement = node)
  49148. };
  49149. /** @type {SyncHandler | undefined} */
  49150. this.enter = enter;
  49151. /** @type {SyncHandler | undefined} */
  49152. this.leave = leave;
  49153. }
  49154. /**
  49155. * @template {Node} Parent
  49156. * @param {Node} node
  49157. * @param {Parent | null} parent
  49158. * @param {keyof Parent} [prop]
  49159. * @param {number | null} [index]
  49160. * @returns {Node | null}
  49161. */
  49162. visit(node, parent, prop, index) {
  49163. if (node) {
  49164. if (this.enter) {
  49165. const _should_skip = this.should_skip;
  49166. const _should_remove = this.should_remove;
  49167. const _replacement = this.replacement;
  49168. this.should_skip = false;
  49169. this.should_remove = false;
  49170. this.replacement = null;
  49171. this.enter.call(this.context, node, parent, prop, index);
  49172. if (this.replacement) {
  49173. node = this.replacement;
  49174. this.replace(parent, prop, index, node);
  49175. }
  49176. if (this.should_remove) {
  49177. this.remove(parent, prop, index);
  49178. }
  49179. const skipped = this.should_skip;
  49180. const removed = this.should_remove;
  49181. this.should_skip = _should_skip;
  49182. this.should_remove = _should_remove;
  49183. this.replacement = _replacement;
  49184. if (skipped) return node;
  49185. if (removed) return null;
  49186. }
  49187. /** @type {keyof Node} */
  49188. let key;
  49189. for (key in node) {
  49190. /** @type {unknown} */
  49191. const value = node[key];
  49192. if (value && typeof value === 'object') {
  49193. if (Array.isArray(value)) {
  49194. const nodes = /** @type {Array<unknown>} */ (value);
  49195. for (let i = 0; i < nodes.length; i += 1) {
  49196. const item = nodes[i];
  49197. if (isNode(item)) {
  49198. if (!this.visit(item, node, key, i)) {
  49199. // removed
  49200. i--;
  49201. }
  49202. }
  49203. }
  49204. } else if (isNode(value)) {
  49205. this.visit(value, node, key, null);
  49206. }
  49207. }
  49208. }
  49209. if (this.leave) {
  49210. const _replacement = this.replacement;
  49211. const _should_remove = this.should_remove;
  49212. this.replacement = null;
  49213. this.should_remove = false;
  49214. this.leave.call(this.context, node, parent, prop, index);
  49215. if (this.replacement) {
  49216. node = this.replacement;
  49217. this.replace(parent, prop, index, node);
  49218. }
  49219. if (this.should_remove) {
  49220. this.remove(parent, prop, index);
  49221. }
  49222. const removed = this.should_remove;
  49223. this.replacement = _replacement;
  49224. this.should_remove = _should_remove;
  49225. if (removed) return null;
  49226. }
  49227. }
  49228. return node;
  49229. }
  49230. }
  49231. /**
  49232. * Ducktype a node.
  49233. *
  49234. * @param {unknown} value
  49235. * @returns {value is Node}
  49236. */
  49237. function isNode(value) {
  49238. return (
  49239. value !== null && typeof value === 'object' && 'type' in value && typeof value.type === 'string'
  49240. );
  49241. }
  49242. /**
  49243. * @typedef {import('estree').Node} Node
  49244. * @typedef {import('./sync.js').SyncHandler} SyncHandler
  49245. * @typedef {import('./async.js').AsyncHandler} AsyncHandler
  49246. */
  49247. /**
  49248. * @param {Node} ast
  49249. * @param {{
  49250. * enter?: SyncHandler
  49251. * leave?: SyncHandler
  49252. * }} walker
  49253. * @returns {Node | null}
  49254. */
  49255. function walk$1(ast, { enter, leave }) {
  49256. const instance = new SyncWalker(enter, leave);
  49257. return instance.visit(ast, null);
  49258. }
  49259. const ssrModuleExportsKey = `__vite_ssr_exports__`;
  49260. const ssrImportKey = `__vite_ssr_import__`;
  49261. const ssrDynamicImportKey = `__vite_ssr_dynamic_import__`;
  49262. const ssrExportAllKey = `__vite_ssr_exportAll__`;
  49263. const ssrImportMetaKey = `__vite_ssr_import_meta__`;
  49264. const hashbangRE = /^#!.*\n/;
  49265. async function ssrTransform(code, inMap, url, originalCode, options) {
  49266. if (options?.json?.stringify && isJSONRequest(url)) {
  49267. return ssrTransformJSON(code, inMap);
  49268. }
  49269. return ssrTransformScript(code, inMap, url, originalCode);
  49270. }
  49271. async function ssrTransformJSON(code, inMap) {
  49272. return {
  49273. code: code.replace("export default", `${ssrModuleExportsKey}.default =`),
  49274. map: inMap,
  49275. deps: [],
  49276. dynamicDeps: []
  49277. };
  49278. }
  49279. async function ssrTransformScript(code, inMap, url, originalCode) {
  49280. const s = new MagicString(code);
  49281. let ast;
  49282. try {
  49283. ast = await parseAstAsync(code);
  49284. } catch (err) {
  49285. if (!err.loc || !err.loc.line) throw err;
  49286. const line = err.loc.line;
  49287. throw new Error(
  49288. `Parse failure: ${err.message}
  49289. At file: ${url}
  49290. Contents of line ${line}: ${code.split("\n")[line - 1]}`
  49291. );
  49292. }
  49293. let uid = 0;
  49294. const deps = /* @__PURE__ */ new Set();
  49295. const dynamicDeps = /* @__PURE__ */ new Set();
  49296. const idToImportMap = /* @__PURE__ */ new Map();
  49297. const declaredConst = /* @__PURE__ */ new Set();
  49298. const hoistIndex = hashbangRE.exec(code)?.[0].length ?? 0;
  49299. function defineImport(index, source, metadata) {
  49300. deps.add(source);
  49301. const importId = `__vite_ssr_import_${uid++}__`;
  49302. if (metadata && (metadata.importedNames == null || metadata.importedNames.length === 0)) {
  49303. metadata = void 0;
  49304. }
  49305. const metadataStr = metadata ? `, ${JSON.stringify(metadata)}` : "";
  49306. s.appendLeft(
  49307. index,
  49308. `const ${importId} = await ${ssrImportKey}(${JSON.stringify(
  49309. source
  49310. )}${metadataStr});
  49311. `
  49312. );
  49313. return importId;
  49314. }
  49315. function defineExport(position, name, local = name) {
  49316. s.appendLeft(
  49317. position,
  49318. `
  49319. Object.defineProperty(${ssrModuleExportsKey}, "${name}", { enumerable: true, configurable: true, get(){ return ${local} }});`
  49320. );
  49321. }
  49322. const imports = [];
  49323. const exports = [];
  49324. for (const node of ast.body) {
  49325. if (node.type === "ImportDeclaration") {
  49326. imports.push(node);
  49327. } else if (node.type === "ExportNamedDeclaration" || node.type === "ExportDefaultDeclaration" || node.type === "ExportAllDeclaration") {
  49328. exports.push(node);
  49329. }
  49330. }
  49331. for (const node of imports) {
  49332. const importId = defineImport(hoistIndex, node.source.value, {
  49333. importedNames: node.specifiers.map((s2) => {
  49334. if (s2.type === "ImportSpecifier")
  49335. return s2.imported.type === "Identifier" ? s2.imported.name : (
  49336. // @ts-expect-error TODO: Estree types don't consider arbitrary module namespace specifiers yet
  49337. s2.imported.value
  49338. );
  49339. else if (s2.type === "ImportDefaultSpecifier") return "default";
  49340. }).filter(isDefined)
  49341. });
  49342. s.remove(node.start, node.end);
  49343. for (const spec of node.specifiers) {
  49344. if (spec.type === "ImportSpecifier") {
  49345. if (spec.imported.type === "Identifier") {
  49346. idToImportMap.set(
  49347. spec.local.name,
  49348. `${importId}.${spec.imported.name}`
  49349. );
  49350. } else {
  49351. idToImportMap.set(
  49352. spec.local.name,
  49353. `${importId}[${// @ts-expect-error TODO: Estree types don't consider arbitrary module namespace specifiers yet
  49354. JSON.stringify(spec.imported.value)}]`
  49355. );
  49356. }
  49357. } else if (spec.type === "ImportDefaultSpecifier") {
  49358. idToImportMap.set(spec.local.name, `${importId}.default`);
  49359. } else {
  49360. idToImportMap.set(spec.local.name, importId);
  49361. }
  49362. }
  49363. }
  49364. for (const node of exports) {
  49365. if (node.type === "ExportNamedDeclaration") {
  49366. if (node.declaration) {
  49367. if (node.declaration.type === "FunctionDeclaration" || node.declaration.type === "ClassDeclaration") {
  49368. defineExport(node.end, node.declaration.id.name);
  49369. } else {
  49370. for (const declaration of node.declaration.declarations) {
  49371. const names = extract_names(declaration.id);
  49372. for (const name of names) {
  49373. defineExport(node.end, name);
  49374. }
  49375. }
  49376. }
  49377. s.remove(node.start, node.declaration.start);
  49378. } else {
  49379. s.remove(node.start, node.end);
  49380. if (node.source) {
  49381. const importId = defineImport(
  49382. node.start,
  49383. node.source.value,
  49384. {
  49385. importedNames: node.specifiers.map((s2) => s2.local.name)
  49386. }
  49387. );
  49388. for (const spec of node.specifiers) {
  49389. const exportedAs = spec.exported.type === "Identifier" ? spec.exported.name : (
  49390. // @ts-expect-error TODO: Estree types don't consider arbitrary module namespace specifiers yet
  49391. spec.exported.value
  49392. );
  49393. defineExport(
  49394. node.start,
  49395. exportedAs,
  49396. `${importId}.${spec.local.name}`
  49397. );
  49398. }
  49399. } else {
  49400. for (const spec of node.specifiers) {
  49401. const local = spec.local.name;
  49402. const binding = idToImportMap.get(local);
  49403. const exportedAs = spec.exported.type === "Identifier" ? spec.exported.name : (
  49404. // @ts-expect-error TODO: Estree types don't consider arbitrary module namespace specifiers yet
  49405. spec.exported.value
  49406. );
  49407. defineExport(node.end, exportedAs, binding || local);
  49408. }
  49409. }
  49410. }
  49411. }
  49412. if (node.type === "ExportDefaultDeclaration") {
  49413. const expressionTypes = ["FunctionExpression", "ClassExpression"];
  49414. if ("id" in node.declaration && node.declaration.id && !expressionTypes.includes(node.declaration.type)) {
  49415. const { name } = node.declaration.id;
  49416. s.remove(
  49417. node.start,
  49418. node.start + 15
  49419. /* 'export default '.length */
  49420. );
  49421. s.append(
  49422. `
  49423. Object.defineProperty(${ssrModuleExportsKey}, "default", { enumerable: true, configurable: true, value: ${name} });`
  49424. );
  49425. } else {
  49426. s.update(
  49427. node.start,
  49428. node.start + 14,
  49429. `${ssrModuleExportsKey}.default =`
  49430. );
  49431. }
  49432. }
  49433. if (node.type === "ExportAllDeclaration") {
  49434. s.remove(node.start, node.end);
  49435. const importId = defineImport(node.start, node.source.value);
  49436. if (node.exported) {
  49437. defineExport(node.start, node.exported.name, `${importId}`);
  49438. } else {
  49439. s.appendLeft(node.start, `${ssrExportAllKey}(${importId});
  49440. `);
  49441. }
  49442. }
  49443. }
  49444. walk(ast, {
  49445. onIdentifier(id, parent, parentStack) {
  49446. const grandparent = parentStack[1];
  49447. const binding = idToImportMap.get(id.name);
  49448. if (!binding) {
  49449. return;
  49450. }
  49451. if (isStaticProperty(parent) && parent.shorthand) {
  49452. if (!isNodeInPattern(parent) || isInDestructuringAssignment(parent, parentStack)) {
  49453. s.appendLeft(id.end, `: ${binding}`);
  49454. }
  49455. } else if (parent.type === "PropertyDefinition" && grandparent?.type === "ClassBody" || parent.type === "ClassDeclaration" && id === parent.superClass) {
  49456. if (!declaredConst.has(id.name)) {
  49457. declaredConst.add(id.name);
  49458. const topNode = parentStack[parentStack.length - 2];
  49459. s.prependRight(topNode.start, `const ${id.name} = ${binding};
  49460. `);
  49461. }
  49462. } else if (
  49463. // don't transform class name identifier
  49464. !(parent.type === "ClassExpression" && id === parent.id)
  49465. ) {
  49466. s.update(id.start, id.end, binding);
  49467. }
  49468. },
  49469. onImportMeta(node) {
  49470. s.update(node.start, node.end, ssrImportMetaKey);
  49471. },
  49472. onDynamicImport(node) {
  49473. s.update(node.start, node.start + 6, ssrDynamicImportKey);
  49474. if (node.type === "ImportExpression" && node.source.type === "Literal") {
  49475. dynamicDeps.add(node.source.value);
  49476. }
  49477. }
  49478. });
  49479. let map = s.generateMap({ hires: "boundary" });
  49480. map.sources = [path$n.basename(url)];
  49481. map.sourcesContent = [originalCode];
  49482. if (inMap && inMap.mappings && "sources" in inMap && inMap.sources.length > 0) {
  49483. map = combineSourcemaps(url, [
  49484. map,
  49485. inMap
  49486. ]);
  49487. }
  49488. return {
  49489. code: s.toString(),
  49490. map,
  49491. deps: [...deps],
  49492. dynamicDeps: [...dynamicDeps]
  49493. };
  49494. }
  49495. const isNodeInPatternWeakSet = /* @__PURE__ */ new WeakSet();
  49496. const setIsNodeInPattern = (node) => isNodeInPatternWeakSet.add(node);
  49497. const isNodeInPattern = (node) => isNodeInPatternWeakSet.has(node);
  49498. function walk(root, { onIdentifier, onImportMeta, onDynamicImport }) {
  49499. const parentStack = [];
  49500. const varKindStack = [];
  49501. const scopeMap = /* @__PURE__ */ new WeakMap();
  49502. const identifiers = [];
  49503. const setScope = (node, name) => {
  49504. let scopeIds = scopeMap.get(node);
  49505. if (scopeIds && scopeIds.has(name)) {
  49506. return;
  49507. }
  49508. if (!scopeIds) {
  49509. scopeIds = /* @__PURE__ */ new Set();
  49510. scopeMap.set(node, scopeIds);
  49511. }
  49512. scopeIds.add(name);
  49513. };
  49514. function isInScope(name, parents) {
  49515. return parents.some((node) => node && scopeMap.get(node)?.has(name));
  49516. }
  49517. function handlePattern(p, parentScope) {
  49518. if (p.type === "Identifier") {
  49519. setScope(parentScope, p.name);
  49520. } else if (p.type === "RestElement") {
  49521. handlePattern(p.argument, parentScope);
  49522. } else if (p.type === "ObjectPattern") {
  49523. p.properties.forEach((property) => {
  49524. if (property.type === "RestElement") {
  49525. setScope(parentScope, property.argument.name);
  49526. } else {
  49527. handlePattern(property.value, parentScope);
  49528. }
  49529. });
  49530. } else if (p.type === "ArrayPattern") {
  49531. p.elements.forEach((element) => {
  49532. if (element) {
  49533. handlePattern(element, parentScope);
  49534. }
  49535. });
  49536. } else if (p.type === "AssignmentPattern") {
  49537. handlePattern(p.left, parentScope);
  49538. } else {
  49539. setScope(parentScope, p.name);
  49540. }
  49541. }
  49542. walk$1(root, {
  49543. enter(node, parent) {
  49544. if (node.type === "ImportDeclaration") {
  49545. return this.skip();
  49546. }
  49547. if (parent && !(parent.type === "IfStatement" && node === parent.alternate)) {
  49548. parentStack.unshift(parent);
  49549. }
  49550. if (node.type === "VariableDeclaration") {
  49551. varKindStack.unshift(node.kind);
  49552. }
  49553. if (node.type === "MetaProperty" && node.meta.name === "import") {
  49554. onImportMeta(node);
  49555. } else if (node.type === "ImportExpression") {
  49556. onDynamicImport(node);
  49557. }
  49558. if (node.type === "Identifier") {
  49559. if (!isInScope(node.name, parentStack) && isRefIdentifier(node, parent, parentStack)) {
  49560. identifiers.push([node, parentStack.slice(0)]);
  49561. }
  49562. } else if (isFunction$1(node)) {
  49563. if (node.type === "FunctionDeclaration") {
  49564. const parentScope = findParentScope(parentStack);
  49565. if (parentScope) {
  49566. setScope(parentScope, node.id.name);
  49567. }
  49568. }
  49569. if (node.type === "FunctionExpression" && node.id) {
  49570. setScope(node, node.id.name);
  49571. }
  49572. node.params.forEach((p) => {
  49573. if (p.type === "ObjectPattern" || p.type === "ArrayPattern") {
  49574. handlePattern(p, node);
  49575. return;
  49576. }
  49577. walk$1(p.type === "AssignmentPattern" ? p.left : p, {
  49578. enter(child, parent2) {
  49579. if (parent2?.type === "AssignmentPattern" && parent2?.right === child) {
  49580. return this.skip();
  49581. }
  49582. if (child.type !== "Identifier") return;
  49583. if (isStaticPropertyKey(child, parent2)) return;
  49584. if (parent2?.type === "TemplateLiteral" && parent2?.expressions.includes(child) || parent2?.type === "CallExpression" && parent2?.callee === child) {
  49585. return;
  49586. }
  49587. setScope(node, child.name);
  49588. }
  49589. });
  49590. });
  49591. } else if (node.type === "ClassDeclaration") {
  49592. const parentScope = findParentScope(parentStack);
  49593. if (parentScope) {
  49594. setScope(parentScope, node.id.name);
  49595. }
  49596. } else if (node.type === "ClassExpression" && node.id) {
  49597. setScope(node, node.id.name);
  49598. } else if (node.type === "Property" && parent.type === "ObjectPattern") {
  49599. setIsNodeInPattern(node);
  49600. } else if (node.type === "VariableDeclarator") {
  49601. const parentFunction = findParentScope(
  49602. parentStack,
  49603. varKindStack[0] === "var"
  49604. );
  49605. if (parentFunction) {
  49606. handlePattern(node.id, parentFunction);
  49607. }
  49608. } else if (node.type === "CatchClause" && node.param) {
  49609. handlePattern(node.param, node);
  49610. }
  49611. },
  49612. leave(node, parent) {
  49613. if (parent && !(parent.type === "IfStatement" && node === parent.alternate)) {
  49614. parentStack.shift();
  49615. }
  49616. if (node.type === "VariableDeclaration") {
  49617. varKindStack.shift();
  49618. }
  49619. }
  49620. });
  49621. identifiers.forEach(([node, stack]) => {
  49622. if (!isInScope(node.name, stack)) onIdentifier(node, stack[0], stack);
  49623. });
  49624. }
  49625. function isRefIdentifier(id, parent, parentStack) {
  49626. if (parent.type === "CatchClause" || (parent.type === "VariableDeclarator" || parent.type === "ClassDeclaration") && parent.id === id) {
  49627. return false;
  49628. }
  49629. if (isFunction$1(parent)) {
  49630. if (parent.id === id) {
  49631. return false;
  49632. }
  49633. if (parent.params.includes(id)) {
  49634. return false;
  49635. }
  49636. }
  49637. if (parent.type === "MethodDefinition" && !parent.computed) {
  49638. return false;
  49639. }
  49640. if (isStaticPropertyKey(id, parent)) {
  49641. return false;
  49642. }
  49643. if (isNodeInPattern(parent) && parent.value === id) {
  49644. return false;
  49645. }
  49646. if (parent.type === "ArrayPattern" && !isInDestructuringAssignment(parent, parentStack)) {
  49647. return false;
  49648. }
  49649. if (parent.type === "MemberExpression" && parent.property === id && !parent.computed) {
  49650. return false;
  49651. }
  49652. if (parent.type === "ExportSpecifier") {
  49653. return false;
  49654. }
  49655. if (id.name === "arguments") {
  49656. return false;
  49657. }
  49658. return true;
  49659. }
  49660. const isStaticProperty = (node) => node && node.type === "Property" && !node.computed;
  49661. const isStaticPropertyKey = (node, parent) => isStaticProperty(parent) && parent.key === node;
  49662. const functionNodeTypeRE = /Function(?:Expression|Declaration)$|Method$/;
  49663. function isFunction$1(node) {
  49664. return functionNodeTypeRE.test(node.type);
  49665. }
  49666. const blockNodeTypeRE = /^BlockStatement$|^For(?:In|Of)?Statement$/;
  49667. function isBlock(node) {
  49668. return blockNodeTypeRE.test(node.type);
  49669. }
  49670. function findParentScope(parentStack, isVar = false) {
  49671. return parentStack.find(isVar ? isFunction$1 : isBlock);
  49672. }
  49673. function isInDestructuringAssignment(parent, parentStack) {
  49674. if (parent && (parent.type === "Property" || parent.type === "ArrayPattern")) {
  49675. return parentStack.some((i) => i.type === "AssignmentExpression");
  49676. }
  49677. return false;
  49678. }
  49679. let offset;
  49680. function calculateOffsetOnce() {
  49681. if (offset !== void 0) {
  49682. return;
  49683. }
  49684. try {
  49685. new Function("throw new Error(1)")();
  49686. } catch (e) {
  49687. const match = /:(\d+):\d+\)$/.exec(e.stack.split("\n")[1]);
  49688. offset = match ? +match[1] - 1 : 0;
  49689. }
  49690. }
  49691. function ssrRewriteStacktrace(stack, moduleGraph) {
  49692. calculateOffsetOnce();
  49693. return stack.split("\n").map((line) => {
  49694. return line.replace(
  49695. /^ {4}at (?:(\S.*?)\s\()?(.+?):(\d+)(?::(\d+))?\)?/,
  49696. (input, varName, id, line2, column) => {
  49697. if (!id) return input;
  49698. const mod = moduleGraph.idToModuleMap.get(id);
  49699. const rawSourceMap = mod?.ssrTransformResult?.map;
  49700. if (!rawSourceMap) {
  49701. return input;
  49702. }
  49703. const traced = new TraceMap(rawSourceMap);
  49704. const pos = originalPositionFor$1(traced, {
  49705. line: Number(line2) - offset,
  49706. // stacktrace's column is 1-indexed, but sourcemap's one is 0-indexed
  49707. column: Number(column) - 1
  49708. });
  49709. if (!pos.source || pos.line == null || pos.column == null) {
  49710. return input;
  49711. }
  49712. const trimmedVarName = varName.trim();
  49713. const sourceFile = path$n.resolve(path$n.dirname(id), pos.source);
  49714. const source = `${sourceFile}:${pos.line}:${pos.column + 1}`;
  49715. if (!trimmedVarName || trimmedVarName === "eval") {
  49716. return ` at ${source}`;
  49717. } else {
  49718. return ` at ${trimmedVarName} (${source})`;
  49719. }
  49720. }
  49721. );
  49722. }).join("\n");
  49723. }
  49724. function rebindErrorStacktrace(e, stacktrace) {
  49725. const { configurable, writable } = Object.getOwnPropertyDescriptor(
  49726. e,
  49727. "stack"
  49728. );
  49729. if (configurable) {
  49730. Object.defineProperty(e, "stack", {
  49731. value: stacktrace,
  49732. enumerable: true,
  49733. configurable: true,
  49734. writable: true
  49735. });
  49736. } else if (writable) {
  49737. e.stack = stacktrace;
  49738. }
  49739. }
  49740. const rewroteStacktraces = /* @__PURE__ */ new WeakSet();
  49741. function ssrFixStacktrace(e, moduleGraph) {
  49742. if (!e.stack) return;
  49743. if (rewroteStacktraces.has(e)) return;
  49744. const stacktrace = ssrRewriteStacktrace(e.stack, moduleGraph);
  49745. rebindErrorStacktrace(e, stacktrace);
  49746. rewroteStacktraces.add(e);
  49747. }
  49748. const pendingModules = /* @__PURE__ */ new Map();
  49749. const pendingModuleDependencyGraph = /* @__PURE__ */ new Map();
  49750. const importErrors = /* @__PURE__ */ new WeakMap();
  49751. async function ssrLoadModule(url, server, fixStacktrace) {
  49752. url = unwrapId$1(url);
  49753. const pending = pendingModules.get(url);
  49754. if (pending) {
  49755. return pending;
  49756. }
  49757. const modulePromise = instantiateModule(url, server, fixStacktrace);
  49758. pendingModules.set(url, modulePromise);
  49759. modulePromise.catch(() => {
  49760. }).finally(() => {
  49761. pendingModules.delete(url);
  49762. });
  49763. return modulePromise;
  49764. }
  49765. async function instantiateModule(url, server, fixStacktrace) {
  49766. const { moduleGraph } = server;
  49767. const mod = await moduleGraph.ensureEntryFromUrl(url, true);
  49768. if (mod.ssrError) {
  49769. throw mod.ssrError;
  49770. }
  49771. if (mod.ssrModule) {
  49772. return mod.ssrModule;
  49773. }
  49774. const result = mod.ssrTransformResult || await transformRequest(url, server, { ssr: true });
  49775. if (!result) {
  49776. throw new Error(`failed to load module for ssr: ${url}`);
  49777. }
  49778. const ssrModule = {
  49779. [Symbol.toStringTag]: "Module"
  49780. };
  49781. Object.defineProperty(ssrModule, "__esModule", { value: true });
  49782. mod.ssrModule = ssrModule;
  49783. const osNormalizedFilename = isWindows$3 ? path$n.resolve(mod.file) : mod.file;
  49784. const ssrImportMeta = {
  49785. dirname: path$n.dirname(osNormalizedFilename),
  49786. filename: osNormalizedFilename,
  49787. // The filesystem URL, matching native Node.js modules
  49788. url: pathToFileURL(mod.file).toString()
  49789. };
  49790. const {
  49791. isProduction,
  49792. resolve: { dedupe, preserveSymlinks },
  49793. root,
  49794. ssr
  49795. } = server.config;
  49796. const overrideConditions = ssr.resolve?.externalConditions || [];
  49797. const resolveOptions = {
  49798. mainFields: ["main"],
  49799. conditions: [],
  49800. overrideConditions: [...overrideConditions, "production", "development"],
  49801. extensions: [".js", ".cjs", ".json"],
  49802. dedupe,
  49803. preserveSymlinks,
  49804. isBuild: false,
  49805. isProduction,
  49806. root,
  49807. ssrConfig: ssr,
  49808. legacyProxySsrExternalModules: server.config.legacy?.proxySsrExternalModules,
  49809. packageCache: server.config.packageCache
  49810. };
  49811. const ssrImport = async (dep, metadata) => {
  49812. try {
  49813. if (dep[0] !== "." && dep[0] !== "/") {
  49814. return await nodeImport(dep, mod.file, resolveOptions, metadata);
  49815. }
  49816. dep = unwrapId$1(dep);
  49817. if (!metadata?.isDynamicImport) {
  49818. addPendingModuleDependency(url, dep);
  49819. if (checkModuleDependencyExists(dep, url)) {
  49820. const depSsrModule = moduleGraph.urlToModuleMap.get(dep)?.ssrModule;
  49821. if (!depSsrModule) {
  49822. throw new Error(
  49823. "[vite] The dependency module is not yet fully initialized due to circular dependency. This is a bug in Vite SSR"
  49824. );
  49825. }
  49826. return depSsrModule;
  49827. }
  49828. }
  49829. return ssrLoadModule(dep, server, fixStacktrace);
  49830. } catch (err) {
  49831. importErrors.set(err, { importee: dep });
  49832. throw err;
  49833. }
  49834. };
  49835. const ssrDynamicImport = (dep) => {
  49836. if (dep[0] === ".") {
  49837. dep = path$n.posix.resolve(path$n.dirname(url), dep);
  49838. }
  49839. return ssrImport(dep, { isDynamicImport: true });
  49840. };
  49841. function ssrExportAll(sourceModule) {
  49842. for (const key in sourceModule) {
  49843. if (key !== "default" && key !== "__esModule") {
  49844. Object.defineProperty(ssrModule, key, {
  49845. enumerable: true,
  49846. configurable: true,
  49847. get() {
  49848. return sourceModule[key];
  49849. }
  49850. });
  49851. }
  49852. }
  49853. }
  49854. let sourceMapSuffix = "";
  49855. if (result.map && "version" in result.map) {
  49856. const moduleSourceMap = Object.assign({}, result.map, {
  49857. mappings: ";".repeat(asyncFunctionDeclarationPaddingLineCount) + result.map.mappings
  49858. });
  49859. sourceMapSuffix = `
  49860. //# ${SOURCEMAPPING_URL}=${genSourceMapUrl(moduleSourceMap)}`;
  49861. }
  49862. try {
  49863. const initModule = new AsyncFunction(
  49864. ssrModuleExportsKey,
  49865. ssrImportMetaKey,
  49866. ssrImportKey,
  49867. ssrDynamicImportKey,
  49868. ssrExportAllKey,
  49869. '"use strict";' + result.code + `
  49870. //# sourceURL=${mod.id}${sourceMapSuffix}`
  49871. );
  49872. await initModule(
  49873. ssrModule,
  49874. ssrImportMeta,
  49875. ssrImport,
  49876. ssrDynamicImport,
  49877. ssrExportAll
  49878. );
  49879. } catch (e) {
  49880. mod.ssrError = e;
  49881. const errorData = importErrors.get(e);
  49882. if (e.stack && fixStacktrace) {
  49883. ssrFixStacktrace(e, moduleGraph);
  49884. }
  49885. server.config.logger.error(
  49886. colors$1.red(
  49887. `Error when evaluating SSR module ${url}:` + (errorData?.importee ? ` failed to import "${errorData.importee}"` : "") + `
  49888. |- ${e.stack}
  49889. `
  49890. ),
  49891. {
  49892. timestamp: true,
  49893. clear: server.config.clearScreen,
  49894. error: e
  49895. }
  49896. );
  49897. throw e;
  49898. } finally {
  49899. pendingModuleDependencyGraph.delete(url);
  49900. }
  49901. return Object.freeze(ssrModule);
  49902. }
  49903. function addPendingModuleDependency(originUrl, depUrl) {
  49904. if (pendingModuleDependencyGraph.has(originUrl)) {
  49905. pendingModuleDependencyGraph.get(originUrl).add(depUrl);
  49906. } else {
  49907. pendingModuleDependencyGraph.set(originUrl, /* @__PURE__ */ new Set([depUrl]));
  49908. }
  49909. }
  49910. function checkModuleDependencyExists(originUrl, targetUrl) {
  49911. const visited = /* @__PURE__ */ new Set();
  49912. const stack = [originUrl];
  49913. while (stack.length) {
  49914. const currentUrl = stack.pop();
  49915. if (currentUrl === targetUrl) {
  49916. return true;
  49917. }
  49918. if (!visited.has(currentUrl)) {
  49919. visited.add(currentUrl);
  49920. const dependencies = pendingModuleDependencyGraph.get(currentUrl);
  49921. if (dependencies) {
  49922. for (const depUrl of dependencies) {
  49923. if (!visited.has(depUrl)) {
  49924. stack.push(depUrl);
  49925. }
  49926. }
  49927. }
  49928. }
  49929. }
  49930. return false;
  49931. }
  49932. async function nodeImport(id, importer, resolveOptions, metadata) {
  49933. let url;
  49934. let filePath;
  49935. if (id.startsWith("data:") || isExternalUrl(id) || isBuiltin(id)) {
  49936. url = id;
  49937. } else {
  49938. const resolved = tryNodeResolve(
  49939. id,
  49940. importer,
  49941. { ...resolveOptions, tryEsmOnly: true },
  49942. false,
  49943. void 0,
  49944. true
  49945. );
  49946. if (!resolved) {
  49947. const err = new Error(
  49948. `Cannot find module '${id}' imported from '${importer}'`
  49949. );
  49950. err.code = "ERR_MODULE_NOT_FOUND";
  49951. throw err;
  49952. }
  49953. filePath = resolved.id;
  49954. url = pathToFileURL(resolved.id).toString();
  49955. }
  49956. const mod = await import(url);
  49957. if (resolveOptions.legacyProxySsrExternalModules) {
  49958. return proxyESM(mod);
  49959. } else if (filePath) {
  49960. analyzeImportedModDifference(
  49961. mod,
  49962. id,
  49963. isFilePathESM(filePath, resolveOptions.packageCache) ? "module" : void 0,
  49964. metadata
  49965. );
  49966. return mod;
  49967. } else {
  49968. return mod;
  49969. }
  49970. }
  49971. function proxyESM(mod) {
  49972. if (isPrimitive(mod)) return { default: mod };
  49973. let defaultExport = "default" in mod ? mod.default : mod;
  49974. if (!isPrimitive(defaultExport) && "__esModule" in defaultExport) {
  49975. mod = defaultExport;
  49976. if ("default" in defaultExport) {
  49977. defaultExport = defaultExport.default;
  49978. }
  49979. }
  49980. return new Proxy(mod, {
  49981. get(mod2, prop) {
  49982. if (prop === "default") return defaultExport;
  49983. return mod2[prop] ?? defaultExport?.[prop];
  49984. }
  49985. });
  49986. }
  49987. function isPrimitive(value) {
  49988. return !value || typeof value !== "object" && typeof value !== "function";
  49989. }
  49990. var isWsl$2 = {exports: {}};
  49991. const fs$3 = require$$0__default;
  49992. let isDocker$2;
  49993. function hasDockerEnv() {
  49994. try {
  49995. fs$3.statSync('/.dockerenv');
  49996. return true;
  49997. } catch (_) {
  49998. return false;
  49999. }
  50000. }
  50001. function hasDockerCGroup() {
  50002. try {
  50003. return fs$3.readFileSync('/proc/self/cgroup', 'utf8').includes('docker');
  50004. } catch (_) {
  50005. return false;
  50006. }
  50007. }
  50008. var isDocker_1 = () => {
  50009. if (isDocker$2 === undefined) {
  50010. isDocker$2 = hasDockerEnv() || hasDockerCGroup();
  50011. }
  50012. return isDocker$2;
  50013. };
  50014. const os = require$$2;
  50015. const fs$2 = require$$0__default;
  50016. const isDocker$1 = isDocker_1;
  50017. const isWsl$1 = () => {
  50018. if (process.platform !== 'linux') {
  50019. return false;
  50020. }
  50021. if (os.release().toLowerCase().includes('microsoft')) {
  50022. if (isDocker$1()) {
  50023. return false;
  50024. }
  50025. return true;
  50026. }
  50027. try {
  50028. return fs$2.readFileSync('/proc/version', 'utf8').toLowerCase().includes('microsoft') ?
  50029. !isDocker$1() : false;
  50030. } catch (_) {
  50031. return false;
  50032. }
  50033. };
  50034. if (process.env.__IS_WSL_TEST__) {
  50035. isWsl$2.exports = isWsl$1;
  50036. } else {
  50037. isWsl$2.exports = isWsl$1();
  50038. }
  50039. var isWslExports = isWsl$2.exports;
  50040. var defineLazyProp = (object, propertyName, fn) => {
  50041. const define = value => Object.defineProperty(object, propertyName, {value, enumerable: true, writable: true});
  50042. Object.defineProperty(object, propertyName, {
  50043. configurable: true,
  50044. enumerable: true,
  50045. get() {
  50046. const result = fn();
  50047. define(result);
  50048. return result;
  50049. },
  50050. set(value) {
  50051. define(value);
  50052. }
  50053. });
  50054. return object;
  50055. };
  50056. const path$3 = require$$0$4;
  50057. const childProcess = require$$2$1;
  50058. const {promises: fs$1, constants: fsConstants} = require$$0__default;
  50059. const isWsl = isWslExports;
  50060. const isDocker = isDocker_1;
  50061. const defineLazyProperty = defineLazyProp;
  50062. // Path to included `xdg-open`.
  50063. const localXdgOpenPath = path$3.join(__dirname, 'xdg-open');
  50064. const {platform, arch} = process;
  50065. // Podman detection
  50066. const hasContainerEnv = () => {
  50067. try {
  50068. fs$1.statSync('/run/.containerenv');
  50069. return true;
  50070. } catch {
  50071. return false;
  50072. }
  50073. };
  50074. let cachedResult;
  50075. function isInsideContainer() {
  50076. if (cachedResult === undefined) {
  50077. cachedResult = hasContainerEnv() || isDocker();
  50078. }
  50079. return cachedResult;
  50080. }
  50081. /**
  50082. Get the mount point for fixed drives in WSL.
  50083. @inner
  50084. @returns {string} The mount point.
  50085. */
  50086. const getWslDrivesMountPoint = (() => {
  50087. // Default value for "root" param
  50088. // according to https://docs.microsoft.com/en-us/windows/wsl/wsl-config
  50089. const defaultMountPoint = '/mnt/';
  50090. let mountPoint;
  50091. return async function () {
  50092. if (mountPoint) {
  50093. // Return memoized mount point value
  50094. return mountPoint;
  50095. }
  50096. const configFilePath = '/etc/wsl.conf';
  50097. let isConfigFileExists = false;
  50098. try {
  50099. await fs$1.access(configFilePath, fsConstants.F_OK);
  50100. isConfigFileExists = true;
  50101. } catch {}
  50102. if (!isConfigFileExists) {
  50103. return defaultMountPoint;
  50104. }
  50105. const configContent = await fs$1.readFile(configFilePath, {encoding: 'utf8'});
  50106. const configMountPoint = /(?<!#.*)root\s*=\s*(?<mountPoint>.*)/g.exec(configContent);
  50107. if (!configMountPoint) {
  50108. return defaultMountPoint;
  50109. }
  50110. mountPoint = configMountPoint.groups.mountPoint.trim();
  50111. mountPoint = mountPoint.endsWith('/') ? mountPoint : `${mountPoint}/`;
  50112. return mountPoint;
  50113. };
  50114. })();
  50115. const pTryEach = async (array, mapper) => {
  50116. let latestError;
  50117. for (const item of array) {
  50118. try {
  50119. return await mapper(item); // eslint-disable-line no-await-in-loop
  50120. } catch (error) {
  50121. latestError = error;
  50122. }
  50123. }
  50124. throw latestError;
  50125. };
  50126. const baseOpen = async options => {
  50127. options = {
  50128. wait: false,
  50129. background: false,
  50130. newInstance: false,
  50131. allowNonzeroExitCode: false,
  50132. ...options
  50133. };
  50134. if (Array.isArray(options.app)) {
  50135. return pTryEach(options.app, singleApp => baseOpen({
  50136. ...options,
  50137. app: singleApp
  50138. }));
  50139. }
  50140. let {name: app, arguments: appArguments = []} = options.app || {};
  50141. appArguments = [...appArguments];
  50142. if (Array.isArray(app)) {
  50143. return pTryEach(app, appName => baseOpen({
  50144. ...options,
  50145. app: {
  50146. name: appName,
  50147. arguments: appArguments
  50148. }
  50149. }));
  50150. }
  50151. let command;
  50152. const cliArguments = [];
  50153. const childProcessOptions = {};
  50154. if (platform === 'darwin') {
  50155. command = 'open';
  50156. if (options.wait) {
  50157. cliArguments.push('--wait-apps');
  50158. }
  50159. if (options.background) {
  50160. cliArguments.push('--background');
  50161. }
  50162. if (options.newInstance) {
  50163. cliArguments.push('--new');
  50164. }
  50165. if (app) {
  50166. cliArguments.push('-a', app);
  50167. }
  50168. } else if (platform === 'win32' || (isWsl && !isInsideContainer() && !app)) {
  50169. const mountPoint = await getWslDrivesMountPoint();
  50170. command = isWsl ?
  50171. `${mountPoint}c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe` :
  50172. `${process.env.SYSTEMROOT}\\System32\\WindowsPowerShell\\v1.0\\powershell`;
  50173. cliArguments.push(
  50174. '-NoProfile',
  50175. '-NonInteractive',
  50176. '–ExecutionPolicy',
  50177. 'Bypass',
  50178. '-EncodedCommand'
  50179. );
  50180. if (!isWsl) {
  50181. childProcessOptions.windowsVerbatimArguments = true;
  50182. }
  50183. const encodedArguments = ['Start'];
  50184. if (options.wait) {
  50185. encodedArguments.push('-Wait');
  50186. }
  50187. if (app) {
  50188. // Double quote with double quotes to ensure the inner quotes are passed through.
  50189. // Inner quotes are delimited for PowerShell interpretation with backticks.
  50190. encodedArguments.push(`"\`"${app}\`""`, '-ArgumentList');
  50191. if (options.target) {
  50192. appArguments.unshift(options.target);
  50193. }
  50194. } else if (options.target) {
  50195. encodedArguments.push(`"${options.target}"`);
  50196. }
  50197. if (appArguments.length > 0) {
  50198. appArguments = appArguments.map(arg => `"\`"${arg}\`""`);
  50199. encodedArguments.push(appArguments.join(','));
  50200. }
  50201. // Using Base64-encoded command, accepted by PowerShell, to allow special characters.
  50202. options.target = Buffer.from(encodedArguments.join(' '), 'utf16le').toString('base64');
  50203. } else {
  50204. if (app) {
  50205. command = app;
  50206. } else {
  50207. // When bundled by Webpack, there's no actual package file path and no local `xdg-open`.
  50208. const isBundled = !__dirname || __dirname === '/';
  50209. // Check if local `xdg-open` exists and is executable.
  50210. let exeLocalXdgOpen = false;
  50211. try {
  50212. await fs$1.access(localXdgOpenPath, fsConstants.X_OK);
  50213. exeLocalXdgOpen = true;
  50214. } catch {}
  50215. const useSystemXdgOpen = process.versions.electron ||
  50216. platform === 'android' || isBundled || !exeLocalXdgOpen;
  50217. command = useSystemXdgOpen ? 'xdg-open' : localXdgOpenPath;
  50218. }
  50219. if (appArguments.length > 0) {
  50220. cliArguments.push(...appArguments);
  50221. }
  50222. if (!options.wait) {
  50223. // `xdg-open` will block the process unless stdio is ignored
  50224. // and it's detached from the parent even if it's unref'd.
  50225. childProcessOptions.stdio = 'ignore';
  50226. childProcessOptions.detached = true;
  50227. }
  50228. }
  50229. if (options.target) {
  50230. cliArguments.push(options.target);
  50231. }
  50232. if (platform === 'darwin' && appArguments.length > 0) {
  50233. cliArguments.push('--args', ...appArguments);
  50234. }
  50235. const subprocess = childProcess.spawn(command, cliArguments, childProcessOptions);
  50236. if (options.wait) {
  50237. return new Promise((resolve, reject) => {
  50238. subprocess.once('error', reject);
  50239. subprocess.once('close', exitCode => {
  50240. if (!options.allowNonzeroExitCode && exitCode > 0) {
  50241. reject(new Error(`Exited with code ${exitCode}`));
  50242. return;
  50243. }
  50244. resolve(subprocess);
  50245. });
  50246. });
  50247. }
  50248. subprocess.unref();
  50249. return subprocess;
  50250. };
  50251. const open = (target, options) => {
  50252. if (typeof target !== 'string') {
  50253. throw new TypeError('Expected a `target`');
  50254. }
  50255. return baseOpen({
  50256. ...options,
  50257. target
  50258. });
  50259. };
  50260. const openApp = (name, options) => {
  50261. if (typeof name !== 'string') {
  50262. throw new TypeError('Expected a `name`');
  50263. }
  50264. const {arguments: appArguments = []} = options || {};
  50265. if (appArguments !== undefined && appArguments !== null && !Array.isArray(appArguments)) {
  50266. throw new TypeError('Expected `appArguments` as Array type');
  50267. }
  50268. return baseOpen({
  50269. ...options,
  50270. app: {
  50271. name,
  50272. arguments: appArguments
  50273. }
  50274. });
  50275. };
  50276. function detectArchBinary(binary) {
  50277. if (typeof binary === 'string' || Array.isArray(binary)) {
  50278. return binary;
  50279. }
  50280. const {[arch]: archBinary} = binary;
  50281. if (!archBinary) {
  50282. throw new Error(`${arch} is not supported`);
  50283. }
  50284. return archBinary;
  50285. }
  50286. function detectPlatformBinary({[platform]: platformBinary}, {wsl}) {
  50287. if (wsl && isWsl) {
  50288. return detectArchBinary(wsl);
  50289. }
  50290. if (!platformBinary) {
  50291. throw new Error(`${platform} is not supported`);
  50292. }
  50293. return detectArchBinary(platformBinary);
  50294. }
  50295. const apps = {};
  50296. defineLazyProperty(apps, 'chrome', () => detectPlatformBinary({
  50297. darwin: 'google chrome',
  50298. win32: 'chrome',
  50299. linux: ['google-chrome', 'google-chrome-stable', 'chromium']
  50300. }, {
  50301. wsl: {
  50302. ia32: '/mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe',
  50303. x64: ['/mnt/c/Program Files/Google/Chrome/Application/chrome.exe', '/mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe']
  50304. }
  50305. }));
  50306. defineLazyProperty(apps, 'firefox', () => detectPlatformBinary({
  50307. darwin: 'firefox',
  50308. win32: 'C:\\Program Files\\Mozilla Firefox\\firefox.exe',
  50309. linux: 'firefox'
  50310. }, {
  50311. wsl: '/mnt/c/Program Files/Mozilla Firefox/firefox.exe'
  50312. }));
  50313. defineLazyProperty(apps, 'edge', () => detectPlatformBinary({
  50314. darwin: 'microsoft edge',
  50315. win32: 'msedge',
  50316. linux: ['microsoft-edge', 'microsoft-edge-dev']
  50317. }, {
  50318. wsl: '/mnt/c/Program Files (x86)/Microsoft/Edge/Application/msedge.exe'
  50319. }));
  50320. open.apps = apps;
  50321. open.openApp = openApp;
  50322. var open_1 = open;
  50323. var open$1 = /*@__PURE__*/getDefaultExportFromCjs(open_1);
  50324. var crossSpawn = {exports: {}};
  50325. var windows;
  50326. var hasRequiredWindows;
  50327. function requireWindows () {
  50328. if (hasRequiredWindows) return windows;
  50329. hasRequiredWindows = 1;
  50330. windows = isexe;
  50331. isexe.sync = sync;
  50332. var fs = require$$0__default;
  50333. function checkPathExt (path, options) {
  50334. var pathext = options.pathExt !== undefined ?
  50335. options.pathExt : process.env.PATHEXT;
  50336. if (!pathext) {
  50337. return true
  50338. }
  50339. pathext = pathext.split(';');
  50340. if (pathext.indexOf('') !== -1) {
  50341. return true
  50342. }
  50343. for (var i = 0; i < pathext.length; i++) {
  50344. var p = pathext[i].toLowerCase();
  50345. if (p && path.substr(-p.length).toLowerCase() === p) {
  50346. return true
  50347. }
  50348. }
  50349. return false
  50350. }
  50351. function checkStat (stat, path, options) {
  50352. if (!stat.isSymbolicLink() && !stat.isFile()) {
  50353. return false
  50354. }
  50355. return checkPathExt(path, options)
  50356. }
  50357. function isexe (path, options, cb) {
  50358. fs.stat(path, function (er, stat) {
  50359. cb(er, er ? false : checkStat(stat, path, options));
  50360. });
  50361. }
  50362. function sync (path, options) {
  50363. return checkStat(fs.statSync(path), path, options)
  50364. }
  50365. return windows;
  50366. }
  50367. var mode;
  50368. var hasRequiredMode;
  50369. function requireMode () {
  50370. if (hasRequiredMode) return mode;
  50371. hasRequiredMode = 1;
  50372. mode = isexe;
  50373. isexe.sync = sync;
  50374. var fs = require$$0__default;
  50375. function isexe (path, options, cb) {
  50376. fs.stat(path, function (er, stat) {
  50377. cb(er, er ? false : checkStat(stat, options));
  50378. });
  50379. }
  50380. function sync (path, options) {
  50381. return checkStat(fs.statSync(path), options)
  50382. }
  50383. function checkStat (stat, options) {
  50384. return stat.isFile() && checkMode(stat, options)
  50385. }
  50386. function checkMode (stat, options) {
  50387. var mod = stat.mode;
  50388. var uid = stat.uid;
  50389. var gid = stat.gid;
  50390. var myUid = options.uid !== undefined ?
  50391. options.uid : process.getuid && process.getuid();
  50392. var myGid = options.gid !== undefined ?
  50393. options.gid : process.getgid && process.getgid();
  50394. var u = parseInt('100', 8);
  50395. var g = parseInt('010', 8);
  50396. var o = parseInt('001', 8);
  50397. var ug = u | g;
  50398. var ret = (mod & o) ||
  50399. (mod & g) && gid === myGid ||
  50400. (mod & u) && uid === myUid ||
  50401. (mod & ug) && myUid === 0;
  50402. return ret
  50403. }
  50404. return mode;
  50405. }
  50406. var core;
  50407. if (process.platform === 'win32' || commonjsGlobal.TESTING_WINDOWS) {
  50408. core = requireWindows();
  50409. } else {
  50410. core = requireMode();
  50411. }
  50412. var isexe_1 = isexe$1;
  50413. isexe$1.sync = sync;
  50414. function isexe$1 (path, options, cb) {
  50415. if (typeof options === 'function') {
  50416. cb = options;
  50417. options = {};
  50418. }
  50419. if (!cb) {
  50420. if (typeof Promise !== 'function') {
  50421. throw new TypeError('callback not provided')
  50422. }
  50423. return new Promise(function (resolve, reject) {
  50424. isexe$1(path, options || {}, function (er, is) {
  50425. if (er) {
  50426. reject(er);
  50427. } else {
  50428. resolve(is);
  50429. }
  50430. });
  50431. })
  50432. }
  50433. core(path, options || {}, function (er, is) {
  50434. // ignore EACCES because that just means we aren't allowed to run it
  50435. if (er) {
  50436. if (er.code === 'EACCES' || options && options.ignoreErrors) {
  50437. er = null;
  50438. is = false;
  50439. }
  50440. }
  50441. cb(er, is);
  50442. });
  50443. }
  50444. function sync (path, options) {
  50445. // my kingdom for a filtered catch
  50446. try {
  50447. return core.sync(path, options || {})
  50448. } catch (er) {
  50449. if (options && options.ignoreErrors || er.code === 'EACCES') {
  50450. return false
  50451. } else {
  50452. throw er
  50453. }
  50454. }
  50455. }
  50456. const isWindows = process.platform === 'win32' ||
  50457. process.env.OSTYPE === 'cygwin' ||
  50458. process.env.OSTYPE === 'msys';
  50459. const path$2 = require$$0$4;
  50460. const COLON = isWindows ? ';' : ':';
  50461. const isexe = isexe_1;
  50462. const getNotFoundError = (cmd) =>
  50463. Object.assign(new Error(`not found: ${cmd}`), { code: 'ENOENT' });
  50464. const getPathInfo = (cmd, opt) => {
  50465. const colon = opt.colon || COLON;
  50466. // If it has a slash, then we don't bother searching the pathenv.
  50467. // just check the file itself, and that's it.
  50468. const pathEnv = cmd.match(/\//) || isWindows && cmd.match(/\\/) ? ['']
  50469. : (
  50470. [
  50471. // windows always checks the cwd first
  50472. ...(isWindows ? [process.cwd()] : []),
  50473. ...(opt.path || process.env.PATH ||
  50474. /* istanbul ignore next: very unusual */ '').split(colon),
  50475. ]
  50476. );
  50477. const pathExtExe = isWindows
  50478. ? opt.pathExt || process.env.PATHEXT || '.EXE;.CMD;.BAT;.COM'
  50479. : '';
  50480. const pathExt = isWindows ? pathExtExe.split(colon) : [''];
  50481. if (isWindows) {
  50482. if (cmd.indexOf('.') !== -1 && pathExt[0] !== '')
  50483. pathExt.unshift('');
  50484. }
  50485. return {
  50486. pathEnv,
  50487. pathExt,
  50488. pathExtExe,
  50489. }
  50490. };
  50491. const which$1 = (cmd, opt, cb) => {
  50492. if (typeof opt === 'function') {
  50493. cb = opt;
  50494. opt = {};
  50495. }
  50496. if (!opt)
  50497. opt = {};
  50498. const { pathEnv, pathExt, pathExtExe } = getPathInfo(cmd, opt);
  50499. const found = [];
  50500. const step = i => new Promise((resolve, reject) => {
  50501. if (i === pathEnv.length)
  50502. return opt.all && found.length ? resolve(found)
  50503. : reject(getNotFoundError(cmd))
  50504. const ppRaw = pathEnv[i];
  50505. const pathPart = /^".*"$/.test(ppRaw) ? ppRaw.slice(1, -1) : ppRaw;
  50506. const pCmd = path$2.join(pathPart, cmd);
  50507. const p = !pathPart && /^\.[\\\/]/.test(cmd) ? cmd.slice(0, 2) + pCmd
  50508. : pCmd;
  50509. resolve(subStep(p, i, 0));
  50510. });
  50511. const subStep = (p, i, ii) => new Promise((resolve, reject) => {
  50512. if (ii === pathExt.length)
  50513. return resolve(step(i + 1))
  50514. const ext = pathExt[ii];
  50515. isexe(p + ext, { pathExt: pathExtExe }, (er, is) => {
  50516. if (!er && is) {
  50517. if (opt.all)
  50518. found.push(p + ext);
  50519. else
  50520. return resolve(p + ext)
  50521. }
  50522. return resolve(subStep(p, i, ii + 1))
  50523. });
  50524. });
  50525. return cb ? step(0).then(res => cb(null, res), cb) : step(0)
  50526. };
  50527. const whichSync = (cmd, opt) => {
  50528. opt = opt || {};
  50529. const { pathEnv, pathExt, pathExtExe } = getPathInfo(cmd, opt);
  50530. const found = [];
  50531. for (let i = 0; i < pathEnv.length; i ++) {
  50532. const ppRaw = pathEnv[i];
  50533. const pathPart = /^".*"$/.test(ppRaw) ? ppRaw.slice(1, -1) : ppRaw;
  50534. const pCmd = path$2.join(pathPart, cmd);
  50535. const p = !pathPart && /^\.[\\\/]/.test(cmd) ? cmd.slice(0, 2) + pCmd
  50536. : pCmd;
  50537. for (let j = 0; j < pathExt.length; j ++) {
  50538. const cur = p + pathExt[j];
  50539. try {
  50540. const is = isexe.sync(cur, { pathExt: pathExtExe });
  50541. if (is) {
  50542. if (opt.all)
  50543. found.push(cur);
  50544. else
  50545. return cur
  50546. }
  50547. } catch (ex) {}
  50548. }
  50549. }
  50550. if (opt.all && found.length)
  50551. return found
  50552. if (opt.nothrow)
  50553. return null
  50554. throw getNotFoundError(cmd)
  50555. };
  50556. var which_1 = which$1;
  50557. which$1.sync = whichSync;
  50558. var pathKey$1 = {exports: {}};
  50559. const pathKey = (options = {}) => {
  50560. const environment = options.env || process.env;
  50561. const platform = options.platform || process.platform;
  50562. if (platform !== 'win32') {
  50563. return 'PATH';
  50564. }
  50565. return Object.keys(environment).reverse().find(key => key.toUpperCase() === 'PATH') || 'Path';
  50566. };
  50567. pathKey$1.exports = pathKey;
  50568. // TODO: Remove this for the next major release
  50569. pathKey$1.exports.default = pathKey;
  50570. var pathKeyExports = pathKey$1.exports;
  50571. const path$1 = require$$0$4;
  50572. const which = which_1;
  50573. const getPathKey = pathKeyExports;
  50574. function resolveCommandAttempt(parsed, withoutPathExt) {
  50575. const env = parsed.options.env || process.env;
  50576. const cwd = process.cwd();
  50577. const hasCustomCwd = parsed.options.cwd != null;
  50578. // Worker threads do not have process.chdir()
  50579. const shouldSwitchCwd = hasCustomCwd && process.chdir !== undefined && !process.chdir.disabled;
  50580. // If a custom `cwd` was specified, we need to change the process cwd
  50581. // because `which` will do stat calls but does not support a custom cwd
  50582. if (shouldSwitchCwd) {
  50583. try {
  50584. process.chdir(parsed.options.cwd);
  50585. } catch (err) {
  50586. /* Empty */
  50587. }
  50588. }
  50589. let resolved;
  50590. try {
  50591. resolved = which.sync(parsed.command, {
  50592. path: env[getPathKey({ env })],
  50593. pathExt: withoutPathExt ? path$1.delimiter : undefined,
  50594. });
  50595. } catch (e) {
  50596. /* Empty */
  50597. } finally {
  50598. if (shouldSwitchCwd) {
  50599. process.chdir(cwd);
  50600. }
  50601. }
  50602. // If we successfully resolved, ensure that an absolute path is returned
  50603. // Note that when a custom `cwd` was used, we need to resolve to an absolute path based on it
  50604. if (resolved) {
  50605. resolved = path$1.resolve(hasCustomCwd ? parsed.options.cwd : '', resolved);
  50606. }
  50607. return resolved;
  50608. }
  50609. function resolveCommand$1(parsed) {
  50610. return resolveCommandAttempt(parsed) || resolveCommandAttempt(parsed, true);
  50611. }
  50612. var resolveCommand_1 = resolveCommand$1;
  50613. var _escape = {};
  50614. // See http://www.robvanderwoude.com/escapechars.php
  50615. const metaCharsRegExp = /([()\][%!^"`<>&|;, *?])/g;
  50616. function escapeCommand(arg) {
  50617. // Escape meta chars
  50618. arg = arg.replace(metaCharsRegExp, '^$1');
  50619. return arg;
  50620. }
  50621. function escapeArgument(arg, doubleEscapeMetaChars) {
  50622. // Convert to string
  50623. arg = `${arg}`;
  50624. // Algorithm below is based on https://qntm.org/cmd
  50625. // Sequence of backslashes followed by a double quote:
  50626. // double up all the backslashes and escape the double quote
  50627. arg = arg.replace(/(\\*)"/g, '$1$1\\"');
  50628. // Sequence of backslashes followed by the end of the string
  50629. // (which will become a double quote later):
  50630. // double up all the backslashes
  50631. arg = arg.replace(/(\\*)$/, '$1$1');
  50632. // All other backslashes occur literally
  50633. // Quote the whole thing:
  50634. arg = `"${arg}"`;
  50635. // Escape meta chars
  50636. arg = arg.replace(metaCharsRegExp, '^$1');
  50637. // Double escape meta chars if necessary
  50638. if (doubleEscapeMetaChars) {
  50639. arg = arg.replace(metaCharsRegExp, '^$1');
  50640. }
  50641. return arg;
  50642. }
  50643. _escape.command = escapeCommand;
  50644. _escape.argument = escapeArgument;
  50645. var shebangRegex$1 = /^#!(.*)/;
  50646. const shebangRegex = shebangRegex$1;
  50647. var shebangCommand$1 = (string = '') => {
  50648. const match = string.match(shebangRegex);
  50649. if (!match) {
  50650. return null;
  50651. }
  50652. const [path, argument] = match[0].replace(/#! ?/, '').split(' ');
  50653. const binary = path.split('/').pop();
  50654. if (binary === 'env') {
  50655. return argument;
  50656. }
  50657. return argument ? `${binary} ${argument}` : binary;
  50658. };
  50659. const fs = require$$0__default;
  50660. const shebangCommand = shebangCommand$1;
  50661. function readShebang$1(command) {
  50662. // Read the first 150 bytes from the file
  50663. const size = 150;
  50664. const buffer = Buffer.alloc(size);
  50665. let fd;
  50666. try {
  50667. fd = fs.openSync(command, 'r');
  50668. fs.readSync(fd, buffer, 0, size, 0);
  50669. fs.closeSync(fd);
  50670. } catch (e) { /* Empty */ }
  50671. // Attempt to extract shebang (null is returned if not a shebang)
  50672. return shebangCommand(buffer.toString());
  50673. }
  50674. var readShebang_1 = readShebang$1;
  50675. const path = require$$0$4;
  50676. const resolveCommand = resolveCommand_1;
  50677. const escape$1 = _escape;
  50678. const readShebang = readShebang_1;
  50679. const isWin$1 = process.platform === 'win32';
  50680. const isExecutableRegExp = /\.(?:com|exe)$/i;
  50681. const isCmdShimRegExp = /node_modules[\\/].bin[\\/][^\\/]+\.cmd$/i;
  50682. function detectShebang(parsed) {
  50683. parsed.file = resolveCommand(parsed);
  50684. const shebang = parsed.file && readShebang(parsed.file);
  50685. if (shebang) {
  50686. parsed.args.unshift(parsed.file);
  50687. parsed.command = shebang;
  50688. return resolveCommand(parsed);
  50689. }
  50690. return parsed.file;
  50691. }
  50692. function parseNonShell(parsed) {
  50693. if (!isWin$1) {
  50694. return parsed;
  50695. }
  50696. // Detect & add support for shebangs
  50697. const commandFile = detectShebang(parsed);
  50698. // We don't need a shell if the command filename is an executable
  50699. const needsShell = !isExecutableRegExp.test(commandFile);
  50700. // If a shell is required, use cmd.exe and take care of escaping everything correctly
  50701. // Note that `forceShell` is an hidden option used only in tests
  50702. if (parsed.options.forceShell || needsShell) {
  50703. // Need to double escape meta chars if the command is a cmd-shim located in `node_modules/.bin/`
  50704. // The cmd-shim simply calls execute the package bin file with NodeJS, proxying any argument
  50705. // Because the escape of metachars with ^ gets interpreted when the cmd.exe is first called,
  50706. // we need to double escape them
  50707. const needsDoubleEscapeMetaChars = isCmdShimRegExp.test(commandFile);
  50708. // Normalize posix paths into OS compatible paths (e.g.: foo/bar -> foo\bar)
  50709. // This is necessary otherwise it will always fail with ENOENT in those cases
  50710. parsed.command = path.normalize(parsed.command);
  50711. // Escape command & arguments
  50712. parsed.command = escape$1.command(parsed.command);
  50713. parsed.args = parsed.args.map((arg) => escape$1.argument(arg, needsDoubleEscapeMetaChars));
  50714. const shellCommand = [parsed.command].concat(parsed.args).join(' ');
  50715. parsed.args = ['/d', '/s', '/c', `"${shellCommand}"`];
  50716. parsed.command = process.env.comspec || 'cmd.exe';
  50717. parsed.options.windowsVerbatimArguments = true; // Tell node's spawn that the arguments are already escaped
  50718. }
  50719. return parsed;
  50720. }
  50721. function parse$4(command, args, options) {
  50722. // Normalize arguments, similar to nodejs
  50723. if (args && !Array.isArray(args)) {
  50724. options = args;
  50725. args = null;
  50726. }
  50727. args = args ? args.slice(0) : []; // Clone array to avoid changing the original
  50728. options = Object.assign({}, options); // Clone object to avoid changing the original
  50729. // Build our parsed object
  50730. const parsed = {
  50731. command,
  50732. args,
  50733. options,
  50734. file: undefined,
  50735. original: {
  50736. command,
  50737. args,
  50738. },
  50739. };
  50740. // Delegate further parsing to shell or non-shell
  50741. return options.shell ? parsed : parseNonShell(parsed);
  50742. }
  50743. var parse_1 = parse$4;
  50744. const isWin = process.platform === 'win32';
  50745. function notFoundError(original, syscall) {
  50746. return Object.assign(new Error(`${syscall} ${original.command} ENOENT`), {
  50747. code: 'ENOENT',
  50748. errno: 'ENOENT',
  50749. syscall: `${syscall} ${original.command}`,
  50750. path: original.command,
  50751. spawnargs: original.args,
  50752. });
  50753. }
  50754. function hookChildProcess(cp, parsed) {
  50755. if (!isWin) {
  50756. return;
  50757. }
  50758. const originalEmit = cp.emit;
  50759. cp.emit = function (name, arg1) {
  50760. // If emitting "exit" event and exit code is 1, we need to check if
  50761. // the command exists and emit an "error" instead
  50762. // See https://github.com/IndigoUnited/node-cross-spawn/issues/16
  50763. if (name === 'exit') {
  50764. const err = verifyENOENT(arg1, parsed);
  50765. if (err) {
  50766. return originalEmit.call(cp, 'error', err);
  50767. }
  50768. }
  50769. return originalEmit.apply(cp, arguments); // eslint-disable-line prefer-rest-params
  50770. };
  50771. }
  50772. function verifyENOENT(status, parsed) {
  50773. if (isWin && status === 1 && !parsed.file) {
  50774. return notFoundError(parsed.original, 'spawn');
  50775. }
  50776. return null;
  50777. }
  50778. function verifyENOENTSync(status, parsed) {
  50779. if (isWin && status === 1 && !parsed.file) {
  50780. return notFoundError(parsed.original, 'spawnSync');
  50781. }
  50782. return null;
  50783. }
  50784. var enoent$1 = {
  50785. hookChildProcess,
  50786. verifyENOENT,
  50787. verifyENOENTSync,
  50788. notFoundError,
  50789. };
  50790. const cp = require$$2$1;
  50791. const parse$3 = parse_1;
  50792. const enoent = enoent$1;
  50793. function spawn(command, args, options) {
  50794. // Parse the arguments
  50795. const parsed = parse$3(command, args, options);
  50796. // Spawn the child process
  50797. const spawned = cp.spawn(parsed.command, parsed.args, parsed.options);
  50798. // Hook into child process "exit" event to emit an error if the command
  50799. // does not exists, see: https://github.com/IndigoUnited/node-cross-spawn/issues/16
  50800. enoent.hookChildProcess(spawned, parsed);
  50801. return spawned;
  50802. }
  50803. function spawnSync(command, args, options) {
  50804. // Parse the arguments
  50805. const parsed = parse$3(command, args, options);
  50806. // Spawn the child process
  50807. const result = cp.spawnSync(parsed.command, parsed.args, parsed.options);
  50808. // Analyze if the command does not exist, see: https://github.com/IndigoUnited/node-cross-spawn/issues/16
  50809. result.error = result.error || enoent.verifyENOENTSync(result.status, parsed);
  50810. return result;
  50811. }
  50812. crossSpawn.exports = spawn;
  50813. crossSpawn.exports.spawn = spawn;
  50814. crossSpawn.exports.sync = spawnSync;
  50815. crossSpawn.exports._parse = parse$3;
  50816. crossSpawn.exports._enoent = enoent;
  50817. var crossSpawnExports = crossSpawn.exports;
  50818. var spawn$1 = /*@__PURE__*/getDefaultExportFromCjs(crossSpawnExports);
  50819. function openBrowser(url, opt, logger) {
  50820. const browser = process.env.BROWSER || "";
  50821. if (browser.toLowerCase().endsWith(".js")) {
  50822. executeNodeScript(browser, url, logger);
  50823. } else if (browser.toLowerCase() !== "none") {
  50824. const browserArgs = process.env.BROWSER_ARGS ? process.env.BROWSER_ARGS.split(" ") : [];
  50825. startBrowserProcess(browser, browserArgs, url, logger);
  50826. }
  50827. }
  50828. function executeNodeScript(scriptPath, url, logger) {
  50829. const extraArgs = process.argv.slice(2);
  50830. const child = spawn$1(process.execPath, [scriptPath, ...extraArgs, url], {
  50831. stdio: "inherit"
  50832. });
  50833. child.on("close", (code) => {
  50834. if (code !== 0) {
  50835. logger.error(
  50836. colors$1.red(
  50837. `
  50838. The script specified as BROWSER environment variable failed.
  50839. ${colors$1.cyan(
  50840. scriptPath
  50841. )} exited with code ${code}.`
  50842. ),
  50843. { error: null }
  50844. );
  50845. }
  50846. });
  50847. }
  50848. const supportedChromiumBrowsers = [
  50849. "Google Chrome Canary",
  50850. "Google Chrome Dev",
  50851. "Google Chrome Beta",
  50852. "Google Chrome",
  50853. "Microsoft Edge",
  50854. "Brave Browser",
  50855. "Vivaldi",
  50856. "Chromium"
  50857. ];
  50858. async function startBrowserProcess(browser, browserArgs, url, logger) {
  50859. const preferredOSXBrowser = browser === "google chrome" ? "Google Chrome" : browser;
  50860. const shouldTryOpenChromeWithAppleScript = process.platform === "darwin" && (!preferredOSXBrowser || supportedChromiumBrowsers.includes(preferredOSXBrowser));
  50861. if (shouldTryOpenChromeWithAppleScript) {
  50862. try {
  50863. const ps = await execAsync("ps cax");
  50864. const openedBrowser = preferredOSXBrowser && ps.includes(preferredOSXBrowser) ? preferredOSXBrowser : supportedChromiumBrowsers.find((b) => ps.includes(b));
  50865. if (openedBrowser) {
  50866. await execAsync(
  50867. `osascript openChrome.applescript "${encodeURI(
  50868. url
  50869. )}" "${openedBrowser}"`,
  50870. {
  50871. cwd: join$2(VITE_PACKAGE_DIR, "bin")
  50872. }
  50873. );
  50874. return true;
  50875. }
  50876. } catch (err) {
  50877. }
  50878. }
  50879. if (process.platform === "darwin" && browser === "open") {
  50880. browser = void 0;
  50881. }
  50882. try {
  50883. const options = browser ? { app: { name: browser, arguments: browserArgs } } : {};
  50884. new Promise((_, reject) => {
  50885. open$1(url, options).then((subprocess) => {
  50886. subprocess.on("error", reject);
  50887. }).catch(reject);
  50888. }).catch((err) => {
  50889. logger.error(err.stack || err.message);
  50890. });
  50891. return true;
  50892. } catch (err) {
  50893. return false;
  50894. }
  50895. }
  50896. function execAsync(command, options) {
  50897. return new Promise((resolve, reject) => {
  50898. exec(command, options, (error, stdout) => {
  50899. if (error) {
  50900. reject(error);
  50901. } else {
  50902. resolve(stdout.toString());
  50903. }
  50904. });
  50905. });
  50906. }
  50907. function bindCLIShortcuts(server, opts) {
  50908. if (!server.httpServer || !process.stdin.isTTY || process.env.CI) {
  50909. return;
  50910. }
  50911. const isDev = isDevServer(server);
  50912. if (isDev) {
  50913. server._shortcutsOptions = opts;
  50914. }
  50915. if (opts?.print) {
  50916. server.config.logger.info(
  50917. colors$1.dim(colors$1.green(" \u279C")) + colors$1.dim(" press ") + colors$1.bold("h + enter") + colors$1.dim(" to show help")
  50918. );
  50919. }
  50920. const shortcuts = (opts?.customShortcuts ?? []).concat(
  50921. isDev ? BASE_DEV_SHORTCUTS : BASE_PREVIEW_SHORTCUTS
  50922. );
  50923. let actionRunning = false;
  50924. const onInput = async (input) => {
  50925. if (actionRunning) return;
  50926. if (input === "h") {
  50927. const loggedKeys = /* @__PURE__ */ new Set();
  50928. server.config.logger.info("\n Shortcuts");
  50929. for (const shortcut2 of shortcuts) {
  50930. if (loggedKeys.has(shortcut2.key)) continue;
  50931. loggedKeys.add(shortcut2.key);
  50932. if (shortcut2.action == null) continue;
  50933. server.config.logger.info(
  50934. colors$1.dim(" press ") + colors$1.bold(`${shortcut2.key} + enter`) + colors$1.dim(` to ${shortcut2.description}`)
  50935. );
  50936. }
  50937. return;
  50938. }
  50939. const shortcut = shortcuts.find((shortcut2) => shortcut2.key === input);
  50940. if (!shortcut || shortcut.action == null) return;
  50941. actionRunning = true;
  50942. await shortcut.action(server);
  50943. actionRunning = false;
  50944. };
  50945. const rl = readline.createInterface({ input: process.stdin });
  50946. rl.on("line", onInput);
  50947. server.httpServer.on("close", () => rl.close());
  50948. }
  50949. const BASE_DEV_SHORTCUTS = [
  50950. {
  50951. key: "r",
  50952. description: "restart the server",
  50953. async action(server) {
  50954. await restartServerWithUrls(server);
  50955. }
  50956. },
  50957. {
  50958. key: "u",
  50959. description: "show server url",
  50960. action(server) {
  50961. server.config.logger.info("");
  50962. server.printUrls();
  50963. }
  50964. },
  50965. {
  50966. key: "o",
  50967. description: "open in browser",
  50968. action(server) {
  50969. server.openBrowser();
  50970. }
  50971. },
  50972. {
  50973. key: "c",
  50974. description: "clear console",
  50975. action(server) {
  50976. server.config.logger.clearScreen("error");
  50977. }
  50978. },
  50979. {
  50980. key: "q",
  50981. description: "quit",
  50982. async action(server) {
  50983. try {
  50984. await server.close();
  50985. } finally {
  50986. process.exit();
  50987. }
  50988. }
  50989. }
  50990. ];
  50991. const BASE_PREVIEW_SHORTCUTS = [
  50992. {
  50993. key: "o",
  50994. description: "open in browser",
  50995. action(server) {
  50996. const url = server.resolvedUrls?.local[0] ?? server.resolvedUrls?.network[0];
  50997. if (url) {
  50998. openBrowser(url, true, server.config.logger);
  50999. } else {
  51000. server.config.logger.warn("No URL available to open in browser");
  51001. }
  51002. }
  51003. },
  51004. {
  51005. key: "q",
  51006. description: "quit",
  51007. async action(server) {
  51008. try {
  51009. await server.close();
  51010. } finally {
  51011. process.exit();
  51012. }
  51013. }
  51014. }
  51015. ];
  51016. function getResolvedOutDirs(root, outDir, outputOptions) {
  51017. const resolvedOutDir = path$n.resolve(root, outDir);
  51018. if (!outputOptions) return /* @__PURE__ */ new Set([resolvedOutDir]);
  51019. return new Set(
  51020. arraify(outputOptions).map(
  51021. ({ dir }) => dir ? path$n.resolve(root, dir) : resolvedOutDir
  51022. )
  51023. );
  51024. }
  51025. function resolveEmptyOutDir(emptyOutDir, root, outDirs, logger) {
  51026. if (emptyOutDir != null) return emptyOutDir;
  51027. for (const outDir of outDirs) {
  51028. if (!normalizePath$3(outDir).startsWith(withTrailingSlash(root))) {
  51029. logger?.warn(
  51030. colors$1.yellow(
  51031. `
  51032. ${colors$1.bold(`(!)`)} outDir ${colors$1.white(
  51033. colors$1.dim(outDir)
  51034. )} is not inside project root and will not be emptied.
  51035. Use --emptyOutDir to override.
  51036. `
  51037. )
  51038. );
  51039. return false;
  51040. }
  51041. }
  51042. return true;
  51043. }
  51044. function resolveChokidarOptions(options, resolvedOutDirs, emptyOutDir, cacheDir) {
  51045. const { ignored: ignoredList, ...otherOptions } = options ?? {};
  51046. const ignored = [
  51047. "**/.git/**",
  51048. "**/node_modules/**",
  51049. "**/test-results/**",
  51050. // Playwright
  51051. glob.escapePath(cacheDir) + "/**",
  51052. ...arraify(ignoredList || [])
  51053. ];
  51054. if (emptyOutDir) {
  51055. ignored.push(
  51056. ...[...resolvedOutDirs].map((outDir) => glob.escapePath(outDir) + "/**")
  51057. );
  51058. }
  51059. const resolvedWatchOptions = {
  51060. ignored,
  51061. ignoreInitial: true,
  51062. ignorePermissionErrors: true,
  51063. ...otherOptions
  51064. };
  51065. return resolvedWatchOptions;
  51066. }
  51067. class NoopWatcher extends EventEmitter$4 {
  51068. constructor(options) {
  51069. super();
  51070. this.options = options;
  51071. }
  51072. add() {
  51073. return this;
  51074. }
  51075. unwatch() {
  51076. return this;
  51077. }
  51078. getWatched() {
  51079. return {};
  51080. }
  51081. ref() {
  51082. return this;
  51083. }
  51084. unref() {
  51085. return this;
  51086. }
  51087. async close() {
  51088. }
  51089. }
  51090. function createNoopWatcher(options) {
  51091. return new NoopWatcher(options);
  51092. }
  51093. async function fetchModule(server, url, importer, options = {}) {
  51094. if (url.startsWith("data:") || isBuiltin(url)) {
  51095. return { externalize: url, type: "builtin" };
  51096. }
  51097. if (isExternalUrl(url)) {
  51098. return { externalize: url, type: "network" };
  51099. }
  51100. if (url[0] !== "." && url[0] !== "/") {
  51101. const {
  51102. isProduction,
  51103. resolve: { dedupe, preserveSymlinks },
  51104. root,
  51105. ssr
  51106. } = server.config;
  51107. const overrideConditions = ssr.resolve?.externalConditions || [];
  51108. const resolveOptions = {
  51109. mainFields: ["main"],
  51110. conditions: [],
  51111. overrideConditions: [...overrideConditions, "production", "development"],
  51112. extensions: [".js", ".cjs", ".json"],
  51113. dedupe,
  51114. preserveSymlinks,
  51115. isBuild: false,
  51116. isProduction,
  51117. root,
  51118. ssrConfig: ssr,
  51119. packageCache: server.config.packageCache
  51120. };
  51121. const resolved = tryNodeResolve(
  51122. url,
  51123. importer,
  51124. { ...resolveOptions, tryEsmOnly: true },
  51125. false,
  51126. void 0,
  51127. true
  51128. );
  51129. if (!resolved) {
  51130. const err = new Error(
  51131. `Cannot find module '${url}' imported from '${importer}'`
  51132. );
  51133. err.code = "ERR_MODULE_NOT_FOUND";
  51134. throw err;
  51135. }
  51136. const file = pathToFileURL(resolved.id).toString();
  51137. const type = isFilePathESM(resolved.id, server.config.packageCache) ? "module" : "commonjs";
  51138. return { externalize: file, type };
  51139. }
  51140. url = unwrapId$1(url);
  51141. let result = await server.transformRequest(url, { ssr: true });
  51142. if (!result) {
  51143. throw new Error(
  51144. `[vite] transform failed for module '${url}'${importer ? ` imported from '${importer}'` : ""}.`
  51145. );
  51146. }
  51147. const mod = await server.moduleGraph.getModuleByUrl(url, true);
  51148. if (!mod) {
  51149. throw new Error(
  51150. `[vite] cannot find module '${url}' ${importer ? ` imported from '${importer}'` : ""}.`
  51151. );
  51152. }
  51153. if (options.inlineSourceMap !== false) {
  51154. result = inlineSourceMap(mod, result, options.processSourceMap);
  51155. }
  51156. if (result.code[0] === "#")
  51157. result.code = result.code.replace(/^#!.*/, (s) => " ".repeat(s.length));
  51158. return { code: result.code, file: mod.file };
  51159. }
  51160. const OTHER_SOURCE_MAP_REGEXP = new RegExp(
  51161. `//# ${SOURCEMAPPING_URL}=data:application/json[^,]+base64,([A-Za-z0-9+/=]+)$`,
  51162. "gm"
  51163. );
  51164. function inlineSourceMap(mod, result, processSourceMap) {
  51165. const map = result.map;
  51166. let code = result.code;
  51167. if (!map || !("version" in map) || code.includes(VITE_RUNTIME_SOURCEMAPPING_SOURCE))
  51168. return result;
  51169. OTHER_SOURCE_MAP_REGEXP.lastIndex = 0;
  51170. if (OTHER_SOURCE_MAP_REGEXP.test(code))
  51171. code = code.replace(OTHER_SOURCE_MAP_REGEXP, "");
  51172. const sourceMap = processSourceMap?.(map) || map;
  51173. result.code = `${code.trimEnd()}
  51174. //# sourceURL=${mod.id}
  51175. ${VITE_RUNTIME_SOURCEMAPPING_SOURCE}
  51176. //# ${SOURCEMAPPING_URL}=${genSourceMapUrl(sourceMap)}
  51177. `;
  51178. return result;
  51179. }
  51180. function ssrFetchModule(server, id, importer) {
  51181. return fetchModule(server, id, importer, {
  51182. processSourceMap(map) {
  51183. return Object.assign({}, map, {
  51184. mappings: ";".repeat(asyncFunctionDeclarationPaddingLineCount) + map.mappings
  51185. });
  51186. }
  51187. });
  51188. }
  51189. var bufferUtil$1 = {exports: {}};
  51190. const BINARY_TYPES$2 = ['nodebuffer', 'arraybuffer', 'fragments'];
  51191. const hasBlob$1 = typeof Blob !== 'undefined';
  51192. if (hasBlob$1) BINARY_TYPES$2.push('blob');
  51193. var constants = {
  51194. BINARY_TYPES: BINARY_TYPES$2,
  51195. EMPTY_BUFFER: Buffer.alloc(0),
  51196. GUID: '258EAFA5-E914-47DA-95CA-C5AB0DC85B11',
  51197. hasBlob: hasBlob$1,
  51198. kForOnEventAttribute: Symbol('kIsForOnEventAttribute'),
  51199. kListener: Symbol('kListener'),
  51200. kStatusCode: Symbol('status-code'),
  51201. kWebSocket: Symbol('websocket'),
  51202. NOOP: () => {}
  51203. };
  51204. const { EMPTY_BUFFER: EMPTY_BUFFER$3 } = constants;
  51205. const FastBuffer$2 = Buffer[Symbol.species];
  51206. /**
  51207. * Merges an array of buffers into a new buffer.
  51208. *
  51209. * @param {Buffer[]} list The array of buffers to concat
  51210. * @param {Number} totalLength The total length of buffers in the list
  51211. * @return {Buffer} The resulting buffer
  51212. * @public
  51213. */
  51214. function concat$1(list, totalLength) {
  51215. if (list.length === 0) return EMPTY_BUFFER$3;
  51216. if (list.length === 1) return list[0];
  51217. const target = Buffer.allocUnsafe(totalLength);
  51218. let offset = 0;
  51219. for (let i = 0; i < list.length; i++) {
  51220. const buf = list[i];
  51221. target.set(buf, offset);
  51222. offset += buf.length;
  51223. }
  51224. if (offset < totalLength) {
  51225. return new FastBuffer$2(target.buffer, target.byteOffset, offset);
  51226. }
  51227. return target;
  51228. }
  51229. /**
  51230. * Masks a buffer using the given mask.
  51231. *
  51232. * @param {Buffer} source The buffer to mask
  51233. * @param {Buffer} mask The mask to use
  51234. * @param {Buffer} output The buffer where to store the result
  51235. * @param {Number} offset The offset at which to start writing
  51236. * @param {Number} length The number of bytes to mask.
  51237. * @public
  51238. */
  51239. function _mask(source, mask, output, offset, length) {
  51240. for (let i = 0; i < length; i++) {
  51241. output[offset + i] = source[i] ^ mask[i & 3];
  51242. }
  51243. }
  51244. /**
  51245. * Unmasks a buffer using the given mask.
  51246. *
  51247. * @param {Buffer} buffer The buffer to unmask
  51248. * @param {Buffer} mask The mask to use
  51249. * @public
  51250. */
  51251. function _unmask(buffer, mask) {
  51252. for (let i = 0; i < buffer.length; i++) {
  51253. buffer[i] ^= mask[i & 3];
  51254. }
  51255. }
  51256. /**
  51257. * Converts a buffer to an `ArrayBuffer`.
  51258. *
  51259. * @param {Buffer} buf The buffer to convert
  51260. * @return {ArrayBuffer} Converted buffer
  51261. * @public
  51262. */
  51263. function toArrayBuffer$1(buf) {
  51264. if (buf.length === buf.buffer.byteLength) {
  51265. return buf.buffer;
  51266. }
  51267. return buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.length);
  51268. }
  51269. /**
  51270. * Converts `data` to a `Buffer`.
  51271. *
  51272. * @param {*} data The data to convert
  51273. * @return {Buffer} The buffer
  51274. * @throws {TypeError}
  51275. * @public
  51276. */
  51277. function toBuffer$2(data) {
  51278. toBuffer$2.readOnly = true;
  51279. if (Buffer.isBuffer(data)) return data;
  51280. let buf;
  51281. if (data instanceof ArrayBuffer) {
  51282. buf = new FastBuffer$2(data);
  51283. } else if (ArrayBuffer.isView(data)) {
  51284. buf = new FastBuffer$2(data.buffer, data.byteOffset, data.byteLength);
  51285. } else {
  51286. buf = Buffer.from(data);
  51287. toBuffer$2.readOnly = false;
  51288. }
  51289. return buf;
  51290. }
  51291. bufferUtil$1.exports = {
  51292. concat: concat$1,
  51293. mask: _mask,
  51294. toArrayBuffer: toArrayBuffer$1,
  51295. toBuffer: toBuffer$2,
  51296. unmask: _unmask
  51297. };
  51298. /* istanbul ignore else */
  51299. if (!process.env.WS_NO_BUFFER_UTIL) {
  51300. try {
  51301. const bufferUtil = require('bufferutil');
  51302. bufferUtil$1.exports.mask = function (source, mask, output, offset, length) {
  51303. if (length < 48) _mask(source, mask, output, offset, length);
  51304. else bufferUtil.mask(source, mask, output, offset, length);
  51305. };
  51306. bufferUtil$1.exports.unmask = function (buffer, mask) {
  51307. if (buffer.length < 32) _unmask(buffer, mask);
  51308. else bufferUtil.unmask(buffer, mask);
  51309. };
  51310. } catch (e) {
  51311. // Continue regardless of the error.
  51312. }
  51313. }
  51314. var bufferUtilExports = bufferUtil$1.exports;
  51315. const kDone = Symbol('kDone');
  51316. const kRun = Symbol('kRun');
  51317. /**
  51318. * A very simple job queue with adjustable concurrency. Adapted from
  51319. * https://github.com/STRML/async-limiter
  51320. */
  51321. let Limiter$1 = class Limiter {
  51322. /**
  51323. * Creates a new `Limiter`.
  51324. *
  51325. * @param {Number} [concurrency=Infinity] The maximum number of jobs allowed
  51326. * to run concurrently
  51327. */
  51328. constructor(concurrency) {
  51329. this[kDone] = () => {
  51330. this.pending--;
  51331. this[kRun]();
  51332. };
  51333. this.concurrency = concurrency || Infinity;
  51334. this.jobs = [];
  51335. this.pending = 0;
  51336. }
  51337. /**
  51338. * Adds a job to the queue.
  51339. *
  51340. * @param {Function} job The job to run
  51341. * @public
  51342. */
  51343. add(job) {
  51344. this.jobs.push(job);
  51345. this[kRun]();
  51346. }
  51347. /**
  51348. * Removes a job from the queue and runs it if possible.
  51349. *
  51350. * @private
  51351. */
  51352. [kRun]() {
  51353. if (this.pending === this.concurrency) return;
  51354. if (this.jobs.length) {
  51355. const job = this.jobs.shift();
  51356. this.pending++;
  51357. job(this[kDone]);
  51358. }
  51359. }
  51360. };
  51361. var limiter = Limiter$1;
  51362. const zlib = zlib$1;
  51363. const bufferUtil = bufferUtilExports;
  51364. const Limiter = limiter;
  51365. const { kStatusCode: kStatusCode$2 } = constants;
  51366. const FastBuffer$1 = Buffer[Symbol.species];
  51367. const TRAILER = Buffer.from([0x00, 0x00, 0xff, 0xff]);
  51368. const kPerMessageDeflate = Symbol('permessage-deflate');
  51369. const kTotalLength = Symbol('total-length');
  51370. const kCallback = Symbol('callback');
  51371. const kBuffers = Symbol('buffers');
  51372. const kError$1 = Symbol('error');
  51373. //
  51374. // We limit zlib concurrency, which prevents severe memory fragmentation
  51375. // as documented in https://github.com/nodejs/node/issues/8871#issuecomment-250915913
  51376. // and https://github.com/websockets/ws/issues/1202
  51377. //
  51378. // Intentionally global; it's the global thread pool that's an issue.
  51379. //
  51380. let zlibLimiter;
  51381. /**
  51382. * permessage-deflate implementation.
  51383. */
  51384. let PerMessageDeflate$4 = class PerMessageDeflate {
  51385. /**
  51386. * Creates a PerMessageDeflate instance.
  51387. *
  51388. * @param {Object} [options] Configuration options
  51389. * @param {(Boolean|Number)} [options.clientMaxWindowBits] Advertise support
  51390. * for, or request, a custom client window size
  51391. * @param {Boolean} [options.clientNoContextTakeover=false] Advertise/
  51392. * acknowledge disabling of client context takeover
  51393. * @param {Number} [options.concurrencyLimit=10] The number of concurrent
  51394. * calls to zlib
  51395. * @param {(Boolean|Number)} [options.serverMaxWindowBits] Request/confirm the
  51396. * use of a custom server window size
  51397. * @param {Boolean} [options.serverNoContextTakeover=false] Request/accept
  51398. * disabling of server context takeover
  51399. * @param {Number} [options.threshold=1024] Size (in bytes) below which
  51400. * messages should not be compressed if context takeover is disabled
  51401. * @param {Object} [options.zlibDeflateOptions] Options to pass to zlib on
  51402. * deflate
  51403. * @param {Object} [options.zlibInflateOptions] Options to pass to zlib on
  51404. * inflate
  51405. * @param {Boolean} [isServer=false] Create the instance in either server or
  51406. * client mode
  51407. * @param {Number} [maxPayload=0] The maximum allowed message length
  51408. */
  51409. constructor(options, isServer, maxPayload) {
  51410. this._maxPayload = maxPayload | 0;
  51411. this._options = options || {};
  51412. this._threshold =
  51413. this._options.threshold !== undefined ? this._options.threshold : 1024;
  51414. this._isServer = !!isServer;
  51415. this._deflate = null;
  51416. this._inflate = null;
  51417. this.params = null;
  51418. if (!zlibLimiter) {
  51419. const concurrency =
  51420. this._options.concurrencyLimit !== undefined
  51421. ? this._options.concurrencyLimit
  51422. : 10;
  51423. zlibLimiter = new Limiter(concurrency);
  51424. }
  51425. }
  51426. /**
  51427. * @type {String}
  51428. */
  51429. static get extensionName() {
  51430. return 'permessage-deflate';
  51431. }
  51432. /**
  51433. * Create an extension negotiation offer.
  51434. *
  51435. * @return {Object} Extension parameters
  51436. * @public
  51437. */
  51438. offer() {
  51439. const params = {};
  51440. if (this._options.serverNoContextTakeover) {
  51441. params.server_no_context_takeover = true;
  51442. }
  51443. if (this._options.clientNoContextTakeover) {
  51444. params.client_no_context_takeover = true;
  51445. }
  51446. if (this._options.serverMaxWindowBits) {
  51447. params.server_max_window_bits = this._options.serverMaxWindowBits;
  51448. }
  51449. if (this._options.clientMaxWindowBits) {
  51450. params.client_max_window_bits = this._options.clientMaxWindowBits;
  51451. } else if (this._options.clientMaxWindowBits == null) {
  51452. params.client_max_window_bits = true;
  51453. }
  51454. return params;
  51455. }
  51456. /**
  51457. * Accept an extension negotiation offer/response.
  51458. *
  51459. * @param {Array} configurations The extension negotiation offers/reponse
  51460. * @return {Object} Accepted configuration
  51461. * @public
  51462. */
  51463. accept(configurations) {
  51464. configurations = this.normalizeParams(configurations);
  51465. this.params = this._isServer
  51466. ? this.acceptAsServer(configurations)
  51467. : this.acceptAsClient(configurations);
  51468. return this.params;
  51469. }
  51470. /**
  51471. * Releases all resources used by the extension.
  51472. *
  51473. * @public
  51474. */
  51475. cleanup() {
  51476. if (this._inflate) {
  51477. this._inflate.close();
  51478. this._inflate = null;
  51479. }
  51480. if (this._deflate) {
  51481. const callback = this._deflate[kCallback];
  51482. this._deflate.close();
  51483. this._deflate = null;
  51484. if (callback) {
  51485. callback(
  51486. new Error(
  51487. 'The deflate stream was closed while data was being processed'
  51488. )
  51489. );
  51490. }
  51491. }
  51492. }
  51493. /**
  51494. * Accept an extension negotiation offer.
  51495. *
  51496. * @param {Array} offers The extension negotiation offers
  51497. * @return {Object} Accepted configuration
  51498. * @private
  51499. */
  51500. acceptAsServer(offers) {
  51501. const opts = this._options;
  51502. const accepted = offers.find((params) => {
  51503. if (
  51504. (opts.serverNoContextTakeover === false &&
  51505. params.server_no_context_takeover) ||
  51506. (params.server_max_window_bits &&
  51507. (opts.serverMaxWindowBits === false ||
  51508. (typeof opts.serverMaxWindowBits === 'number' &&
  51509. opts.serverMaxWindowBits > params.server_max_window_bits))) ||
  51510. (typeof opts.clientMaxWindowBits === 'number' &&
  51511. !params.client_max_window_bits)
  51512. ) {
  51513. return false;
  51514. }
  51515. return true;
  51516. });
  51517. if (!accepted) {
  51518. throw new Error('None of the extension offers can be accepted');
  51519. }
  51520. if (opts.serverNoContextTakeover) {
  51521. accepted.server_no_context_takeover = true;
  51522. }
  51523. if (opts.clientNoContextTakeover) {
  51524. accepted.client_no_context_takeover = true;
  51525. }
  51526. if (typeof opts.serverMaxWindowBits === 'number') {
  51527. accepted.server_max_window_bits = opts.serverMaxWindowBits;
  51528. }
  51529. if (typeof opts.clientMaxWindowBits === 'number') {
  51530. accepted.client_max_window_bits = opts.clientMaxWindowBits;
  51531. } else if (
  51532. accepted.client_max_window_bits === true ||
  51533. opts.clientMaxWindowBits === false
  51534. ) {
  51535. delete accepted.client_max_window_bits;
  51536. }
  51537. return accepted;
  51538. }
  51539. /**
  51540. * Accept the extension negotiation response.
  51541. *
  51542. * @param {Array} response The extension negotiation response
  51543. * @return {Object} Accepted configuration
  51544. * @private
  51545. */
  51546. acceptAsClient(response) {
  51547. const params = response[0];
  51548. if (
  51549. this._options.clientNoContextTakeover === false &&
  51550. params.client_no_context_takeover
  51551. ) {
  51552. throw new Error('Unexpected parameter "client_no_context_takeover"');
  51553. }
  51554. if (!params.client_max_window_bits) {
  51555. if (typeof this._options.clientMaxWindowBits === 'number') {
  51556. params.client_max_window_bits = this._options.clientMaxWindowBits;
  51557. }
  51558. } else if (
  51559. this._options.clientMaxWindowBits === false ||
  51560. (typeof this._options.clientMaxWindowBits === 'number' &&
  51561. params.client_max_window_bits > this._options.clientMaxWindowBits)
  51562. ) {
  51563. throw new Error(
  51564. 'Unexpected or invalid parameter "client_max_window_bits"'
  51565. );
  51566. }
  51567. return params;
  51568. }
  51569. /**
  51570. * Normalize parameters.
  51571. *
  51572. * @param {Array} configurations The extension negotiation offers/reponse
  51573. * @return {Array} The offers/response with normalized parameters
  51574. * @private
  51575. */
  51576. normalizeParams(configurations) {
  51577. configurations.forEach((params) => {
  51578. Object.keys(params).forEach((key) => {
  51579. let value = params[key];
  51580. if (value.length > 1) {
  51581. throw new Error(`Parameter "${key}" must have only a single value`);
  51582. }
  51583. value = value[0];
  51584. if (key === 'client_max_window_bits') {
  51585. if (value !== true) {
  51586. const num = +value;
  51587. if (!Number.isInteger(num) || num < 8 || num > 15) {
  51588. throw new TypeError(
  51589. `Invalid value for parameter "${key}": ${value}`
  51590. );
  51591. }
  51592. value = num;
  51593. } else if (!this._isServer) {
  51594. throw new TypeError(
  51595. `Invalid value for parameter "${key}": ${value}`
  51596. );
  51597. }
  51598. } else if (key === 'server_max_window_bits') {
  51599. const num = +value;
  51600. if (!Number.isInteger(num) || num < 8 || num > 15) {
  51601. throw new TypeError(
  51602. `Invalid value for parameter "${key}": ${value}`
  51603. );
  51604. }
  51605. value = num;
  51606. } else if (
  51607. key === 'client_no_context_takeover' ||
  51608. key === 'server_no_context_takeover'
  51609. ) {
  51610. if (value !== true) {
  51611. throw new TypeError(
  51612. `Invalid value for parameter "${key}": ${value}`
  51613. );
  51614. }
  51615. } else {
  51616. throw new Error(`Unknown parameter "${key}"`);
  51617. }
  51618. params[key] = value;
  51619. });
  51620. });
  51621. return configurations;
  51622. }
  51623. /**
  51624. * Decompress data. Concurrency limited.
  51625. *
  51626. * @param {Buffer} data Compressed data
  51627. * @param {Boolean} fin Specifies whether or not this is the last fragment
  51628. * @param {Function} callback Callback
  51629. * @public
  51630. */
  51631. decompress(data, fin, callback) {
  51632. zlibLimiter.add((done) => {
  51633. this._decompress(data, fin, (err, result) => {
  51634. done();
  51635. callback(err, result);
  51636. });
  51637. });
  51638. }
  51639. /**
  51640. * Compress data. Concurrency limited.
  51641. *
  51642. * @param {(Buffer|String)} data Data to compress
  51643. * @param {Boolean} fin Specifies whether or not this is the last fragment
  51644. * @param {Function} callback Callback
  51645. * @public
  51646. */
  51647. compress(data, fin, callback) {
  51648. zlibLimiter.add((done) => {
  51649. this._compress(data, fin, (err, result) => {
  51650. done();
  51651. callback(err, result);
  51652. });
  51653. });
  51654. }
  51655. /**
  51656. * Decompress data.
  51657. *
  51658. * @param {Buffer} data Compressed data
  51659. * @param {Boolean} fin Specifies whether or not this is the last fragment
  51660. * @param {Function} callback Callback
  51661. * @private
  51662. */
  51663. _decompress(data, fin, callback) {
  51664. const endpoint = this._isServer ? 'client' : 'server';
  51665. if (!this._inflate) {
  51666. const key = `${endpoint}_max_window_bits`;
  51667. const windowBits =
  51668. typeof this.params[key] !== 'number'
  51669. ? zlib.Z_DEFAULT_WINDOWBITS
  51670. : this.params[key];
  51671. this._inflate = zlib.createInflateRaw({
  51672. ...this._options.zlibInflateOptions,
  51673. windowBits
  51674. });
  51675. this._inflate[kPerMessageDeflate] = this;
  51676. this._inflate[kTotalLength] = 0;
  51677. this._inflate[kBuffers] = [];
  51678. this._inflate.on('error', inflateOnError);
  51679. this._inflate.on('data', inflateOnData);
  51680. }
  51681. this._inflate[kCallback] = callback;
  51682. this._inflate.write(data);
  51683. if (fin) this._inflate.write(TRAILER);
  51684. this._inflate.flush(() => {
  51685. const err = this._inflate[kError$1];
  51686. if (err) {
  51687. this._inflate.close();
  51688. this._inflate = null;
  51689. callback(err);
  51690. return;
  51691. }
  51692. const data = bufferUtil.concat(
  51693. this._inflate[kBuffers],
  51694. this._inflate[kTotalLength]
  51695. );
  51696. if (this._inflate._readableState.endEmitted) {
  51697. this._inflate.close();
  51698. this._inflate = null;
  51699. } else {
  51700. this._inflate[kTotalLength] = 0;
  51701. this._inflate[kBuffers] = [];
  51702. if (fin && this.params[`${endpoint}_no_context_takeover`]) {
  51703. this._inflate.reset();
  51704. }
  51705. }
  51706. callback(null, data);
  51707. });
  51708. }
  51709. /**
  51710. * Compress data.
  51711. *
  51712. * @param {(Buffer|String)} data Data to compress
  51713. * @param {Boolean} fin Specifies whether or not this is the last fragment
  51714. * @param {Function} callback Callback
  51715. * @private
  51716. */
  51717. _compress(data, fin, callback) {
  51718. const endpoint = this._isServer ? 'server' : 'client';
  51719. if (!this._deflate) {
  51720. const key = `${endpoint}_max_window_bits`;
  51721. const windowBits =
  51722. typeof this.params[key] !== 'number'
  51723. ? zlib.Z_DEFAULT_WINDOWBITS
  51724. : this.params[key];
  51725. this._deflate = zlib.createDeflateRaw({
  51726. ...this._options.zlibDeflateOptions,
  51727. windowBits
  51728. });
  51729. this._deflate[kTotalLength] = 0;
  51730. this._deflate[kBuffers] = [];
  51731. this._deflate.on('data', deflateOnData);
  51732. }
  51733. this._deflate[kCallback] = callback;
  51734. this._deflate.write(data);
  51735. this._deflate.flush(zlib.Z_SYNC_FLUSH, () => {
  51736. if (!this._deflate) {
  51737. //
  51738. // The deflate stream was closed while data was being processed.
  51739. //
  51740. return;
  51741. }
  51742. let data = bufferUtil.concat(
  51743. this._deflate[kBuffers],
  51744. this._deflate[kTotalLength]
  51745. );
  51746. if (fin) {
  51747. data = new FastBuffer$1(data.buffer, data.byteOffset, data.length - 4);
  51748. }
  51749. //
  51750. // Ensure that the callback will not be called again in
  51751. // `PerMessageDeflate#cleanup()`.
  51752. //
  51753. this._deflate[kCallback] = null;
  51754. this._deflate[kTotalLength] = 0;
  51755. this._deflate[kBuffers] = [];
  51756. if (fin && this.params[`${endpoint}_no_context_takeover`]) {
  51757. this._deflate.reset();
  51758. }
  51759. callback(null, data);
  51760. });
  51761. }
  51762. };
  51763. var permessageDeflate = PerMessageDeflate$4;
  51764. /**
  51765. * The listener of the `zlib.DeflateRaw` stream `'data'` event.
  51766. *
  51767. * @param {Buffer} chunk A chunk of data
  51768. * @private
  51769. */
  51770. function deflateOnData(chunk) {
  51771. this[kBuffers].push(chunk);
  51772. this[kTotalLength] += chunk.length;
  51773. }
  51774. /**
  51775. * The listener of the `zlib.InflateRaw` stream `'data'` event.
  51776. *
  51777. * @param {Buffer} chunk A chunk of data
  51778. * @private
  51779. */
  51780. function inflateOnData(chunk) {
  51781. this[kTotalLength] += chunk.length;
  51782. if (
  51783. this[kPerMessageDeflate]._maxPayload < 1 ||
  51784. this[kTotalLength] <= this[kPerMessageDeflate]._maxPayload
  51785. ) {
  51786. this[kBuffers].push(chunk);
  51787. return;
  51788. }
  51789. this[kError$1] = new RangeError('Max payload size exceeded');
  51790. this[kError$1].code = 'WS_ERR_UNSUPPORTED_MESSAGE_LENGTH';
  51791. this[kError$1][kStatusCode$2] = 1009;
  51792. this.removeListener('data', inflateOnData);
  51793. this.reset();
  51794. }
  51795. /**
  51796. * The listener of the `zlib.InflateRaw` stream `'error'` event.
  51797. *
  51798. * @param {Error} err The emitted error
  51799. * @private
  51800. */
  51801. function inflateOnError(err) {
  51802. //
  51803. // There is no need to call `Zlib#close()` as the handle is automatically
  51804. // closed when an error is emitted.
  51805. //
  51806. this[kPerMessageDeflate]._inflate = null;
  51807. err[kStatusCode$2] = 1007;
  51808. this[kCallback](err);
  51809. }
  51810. var validation = {exports: {}};
  51811. const { isUtf8 } = require$$0$a;
  51812. const { hasBlob } = constants;
  51813. //
  51814. // Allowed token characters:
  51815. //
  51816. // '!', '#', '$', '%', '&', ''', '*', '+', '-',
  51817. // '.', 0-9, A-Z, '^', '_', '`', a-z, '|', '~'
  51818. //
  51819. // tokenChars[32] === 0 // ' '
  51820. // tokenChars[33] === 1 // '!'
  51821. // tokenChars[34] === 0 // '"'
  51822. // ...
  51823. //
  51824. // prettier-ignore
  51825. const tokenChars$2 = [
  51826. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0 - 15
  51827. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 16 - 31
  51828. 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, // 32 - 47
  51829. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, // 48 - 63
  51830. 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 64 - 79
  51831. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, // 80 - 95
  51832. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 96 - 111
  51833. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0 // 112 - 127
  51834. ];
  51835. /**
  51836. * Checks if a status code is allowed in a close frame.
  51837. *
  51838. * @param {Number} code The status code
  51839. * @return {Boolean} `true` if the status code is valid, else `false`
  51840. * @public
  51841. */
  51842. function isValidStatusCode$2(code) {
  51843. return (
  51844. (code >= 1000 &&
  51845. code <= 1014 &&
  51846. code !== 1004 &&
  51847. code !== 1005 &&
  51848. code !== 1006) ||
  51849. (code >= 3000 && code <= 4999)
  51850. );
  51851. }
  51852. /**
  51853. * Checks if a given buffer contains only correct UTF-8.
  51854. * Ported from https://www.cl.cam.ac.uk/%7Emgk25/ucs/utf8_check.c by
  51855. * Markus Kuhn.
  51856. *
  51857. * @param {Buffer} buf The buffer to check
  51858. * @return {Boolean} `true` if `buf` contains only correct UTF-8, else `false`
  51859. * @public
  51860. */
  51861. function _isValidUTF8(buf) {
  51862. const len = buf.length;
  51863. let i = 0;
  51864. while (i < len) {
  51865. if ((buf[i] & 0x80) === 0) {
  51866. // 0xxxxxxx
  51867. i++;
  51868. } else if ((buf[i] & 0xe0) === 0xc0) {
  51869. // 110xxxxx 10xxxxxx
  51870. if (
  51871. i + 1 === len ||
  51872. (buf[i + 1] & 0xc0) !== 0x80 ||
  51873. (buf[i] & 0xfe) === 0xc0 // Overlong
  51874. ) {
  51875. return false;
  51876. }
  51877. i += 2;
  51878. } else if ((buf[i] & 0xf0) === 0xe0) {
  51879. // 1110xxxx 10xxxxxx 10xxxxxx
  51880. if (
  51881. i + 2 >= len ||
  51882. (buf[i + 1] & 0xc0) !== 0x80 ||
  51883. (buf[i + 2] & 0xc0) !== 0x80 ||
  51884. (buf[i] === 0xe0 && (buf[i + 1] & 0xe0) === 0x80) || // Overlong
  51885. (buf[i] === 0xed && (buf[i + 1] & 0xe0) === 0xa0) // Surrogate (U+D800 - U+DFFF)
  51886. ) {
  51887. return false;
  51888. }
  51889. i += 3;
  51890. } else if ((buf[i] & 0xf8) === 0xf0) {
  51891. // 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
  51892. if (
  51893. i + 3 >= len ||
  51894. (buf[i + 1] & 0xc0) !== 0x80 ||
  51895. (buf[i + 2] & 0xc0) !== 0x80 ||
  51896. (buf[i + 3] & 0xc0) !== 0x80 ||
  51897. (buf[i] === 0xf0 && (buf[i + 1] & 0xf0) === 0x80) || // Overlong
  51898. (buf[i] === 0xf4 && buf[i + 1] > 0x8f) ||
  51899. buf[i] > 0xf4 // > U+10FFFF
  51900. ) {
  51901. return false;
  51902. }
  51903. i += 4;
  51904. } else {
  51905. return false;
  51906. }
  51907. }
  51908. return true;
  51909. }
  51910. /**
  51911. * Determines whether a value is a `Blob`.
  51912. *
  51913. * @param {*} value The value to be tested
  51914. * @return {Boolean} `true` if `value` is a `Blob`, else `false`
  51915. * @private
  51916. */
  51917. function isBlob$2(value) {
  51918. return (
  51919. hasBlob &&
  51920. typeof value === 'object' &&
  51921. typeof value.arrayBuffer === 'function' &&
  51922. typeof value.type === 'string' &&
  51923. typeof value.stream === 'function' &&
  51924. (value[Symbol.toStringTag] === 'Blob' ||
  51925. value[Symbol.toStringTag] === 'File')
  51926. );
  51927. }
  51928. validation.exports = {
  51929. isBlob: isBlob$2,
  51930. isValidStatusCode: isValidStatusCode$2,
  51931. isValidUTF8: _isValidUTF8,
  51932. tokenChars: tokenChars$2
  51933. };
  51934. if (isUtf8) {
  51935. validation.exports.isValidUTF8 = function (buf) {
  51936. return buf.length < 24 ? _isValidUTF8(buf) : isUtf8(buf);
  51937. };
  51938. } /* istanbul ignore else */ else if (!process.env.WS_NO_UTF_8_VALIDATE) {
  51939. try {
  51940. const isValidUTF8 = require('utf-8-validate');
  51941. validation.exports.isValidUTF8 = function (buf) {
  51942. return buf.length < 32 ? _isValidUTF8(buf) : isValidUTF8(buf);
  51943. };
  51944. } catch (e) {
  51945. // Continue regardless of the error.
  51946. }
  51947. }
  51948. var validationExports = validation.exports;
  51949. const { Writable: Writable$1 } = require$$0$6;
  51950. const PerMessageDeflate$3 = permessageDeflate;
  51951. const {
  51952. BINARY_TYPES: BINARY_TYPES$1,
  51953. EMPTY_BUFFER: EMPTY_BUFFER$2,
  51954. kStatusCode: kStatusCode$1,
  51955. kWebSocket: kWebSocket$3
  51956. } = constants;
  51957. const { concat, toArrayBuffer, unmask } = bufferUtilExports;
  51958. const { isValidStatusCode: isValidStatusCode$1, isValidUTF8 } = validationExports;
  51959. const FastBuffer = Buffer[Symbol.species];
  51960. const GET_INFO = 0;
  51961. const GET_PAYLOAD_LENGTH_16 = 1;
  51962. const GET_PAYLOAD_LENGTH_64 = 2;
  51963. const GET_MASK = 3;
  51964. const GET_DATA = 4;
  51965. const INFLATING = 5;
  51966. const DEFER_EVENT = 6;
  51967. /**
  51968. * HyBi Receiver implementation.
  51969. *
  51970. * @extends Writable
  51971. */
  51972. let Receiver$1 = class Receiver extends Writable$1 {
  51973. /**
  51974. * Creates a Receiver instance.
  51975. *
  51976. * @param {Object} [options] Options object
  51977. * @param {Boolean} [options.allowSynchronousEvents=true] Specifies whether
  51978. * any of the `'message'`, `'ping'`, and `'pong'` events can be emitted
  51979. * multiple times in the same tick
  51980. * @param {String} [options.binaryType=nodebuffer] The type for binary data
  51981. * @param {Object} [options.extensions] An object containing the negotiated
  51982. * extensions
  51983. * @param {Boolean} [options.isServer=false] Specifies whether to operate in
  51984. * client or server mode
  51985. * @param {Number} [options.maxPayload=0] The maximum allowed message length
  51986. * @param {Boolean} [options.skipUTF8Validation=false] Specifies whether or
  51987. * not to skip UTF-8 validation for text and close messages
  51988. */
  51989. constructor(options = {}) {
  51990. super();
  51991. this._allowSynchronousEvents =
  51992. options.allowSynchronousEvents !== undefined
  51993. ? options.allowSynchronousEvents
  51994. : true;
  51995. this._binaryType = options.binaryType || BINARY_TYPES$1[0];
  51996. this._extensions = options.extensions || {};
  51997. this._isServer = !!options.isServer;
  51998. this._maxPayload = options.maxPayload | 0;
  51999. this._skipUTF8Validation = !!options.skipUTF8Validation;
  52000. this[kWebSocket$3] = undefined;
  52001. this._bufferedBytes = 0;
  52002. this._buffers = [];
  52003. this._compressed = false;
  52004. this._payloadLength = 0;
  52005. this._mask = undefined;
  52006. this._fragmented = 0;
  52007. this._masked = false;
  52008. this._fin = false;
  52009. this._opcode = 0;
  52010. this._totalPayloadLength = 0;
  52011. this._messageLength = 0;
  52012. this._fragments = [];
  52013. this._errored = false;
  52014. this._loop = false;
  52015. this._state = GET_INFO;
  52016. }
  52017. /**
  52018. * Implements `Writable.prototype._write()`.
  52019. *
  52020. * @param {Buffer} chunk The chunk of data to write
  52021. * @param {String} encoding The character encoding of `chunk`
  52022. * @param {Function} cb Callback
  52023. * @private
  52024. */
  52025. _write(chunk, encoding, cb) {
  52026. if (this._opcode === 0x08 && this._state == GET_INFO) return cb();
  52027. this._bufferedBytes += chunk.length;
  52028. this._buffers.push(chunk);
  52029. this.startLoop(cb);
  52030. }
  52031. /**
  52032. * Consumes `n` bytes from the buffered data.
  52033. *
  52034. * @param {Number} n The number of bytes to consume
  52035. * @return {Buffer} The consumed bytes
  52036. * @private
  52037. */
  52038. consume(n) {
  52039. this._bufferedBytes -= n;
  52040. if (n === this._buffers[0].length) return this._buffers.shift();
  52041. if (n < this._buffers[0].length) {
  52042. const buf = this._buffers[0];
  52043. this._buffers[0] = new FastBuffer(
  52044. buf.buffer,
  52045. buf.byteOffset + n,
  52046. buf.length - n
  52047. );
  52048. return new FastBuffer(buf.buffer, buf.byteOffset, n);
  52049. }
  52050. const dst = Buffer.allocUnsafe(n);
  52051. do {
  52052. const buf = this._buffers[0];
  52053. const offset = dst.length - n;
  52054. if (n >= buf.length) {
  52055. dst.set(this._buffers.shift(), offset);
  52056. } else {
  52057. dst.set(new Uint8Array(buf.buffer, buf.byteOffset, n), offset);
  52058. this._buffers[0] = new FastBuffer(
  52059. buf.buffer,
  52060. buf.byteOffset + n,
  52061. buf.length - n
  52062. );
  52063. }
  52064. n -= buf.length;
  52065. } while (n > 0);
  52066. return dst;
  52067. }
  52068. /**
  52069. * Starts the parsing loop.
  52070. *
  52071. * @param {Function} cb Callback
  52072. * @private
  52073. */
  52074. startLoop(cb) {
  52075. this._loop = true;
  52076. do {
  52077. switch (this._state) {
  52078. case GET_INFO:
  52079. this.getInfo(cb);
  52080. break;
  52081. case GET_PAYLOAD_LENGTH_16:
  52082. this.getPayloadLength16(cb);
  52083. break;
  52084. case GET_PAYLOAD_LENGTH_64:
  52085. this.getPayloadLength64(cb);
  52086. break;
  52087. case GET_MASK:
  52088. this.getMask();
  52089. break;
  52090. case GET_DATA:
  52091. this.getData(cb);
  52092. break;
  52093. case INFLATING:
  52094. case DEFER_EVENT:
  52095. this._loop = false;
  52096. return;
  52097. }
  52098. } while (this._loop);
  52099. if (!this._errored) cb();
  52100. }
  52101. /**
  52102. * Reads the first two bytes of a frame.
  52103. *
  52104. * @param {Function} cb Callback
  52105. * @private
  52106. */
  52107. getInfo(cb) {
  52108. if (this._bufferedBytes < 2) {
  52109. this._loop = false;
  52110. return;
  52111. }
  52112. const buf = this.consume(2);
  52113. if ((buf[0] & 0x30) !== 0x00) {
  52114. const error = this.createError(
  52115. RangeError,
  52116. 'RSV2 and RSV3 must be clear',
  52117. true,
  52118. 1002,
  52119. 'WS_ERR_UNEXPECTED_RSV_2_3'
  52120. );
  52121. cb(error);
  52122. return;
  52123. }
  52124. const compressed = (buf[0] & 0x40) === 0x40;
  52125. if (compressed && !this._extensions[PerMessageDeflate$3.extensionName]) {
  52126. const error = this.createError(
  52127. RangeError,
  52128. 'RSV1 must be clear',
  52129. true,
  52130. 1002,
  52131. 'WS_ERR_UNEXPECTED_RSV_1'
  52132. );
  52133. cb(error);
  52134. return;
  52135. }
  52136. this._fin = (buf[0] & 0x80) === 0x80;
  52137. this._opcode = buf[0] & 0x0f;
  52138. this._payloadLength = buf[1] & 0x7f;
  52139. if (this._opcode === 0x00) {
  52140. if (compressed) {
  52141. const error = this.createError(
  52142. RangeError,
  52143. 'RSV1 must be clear',
  52144. true,
  52145. 1002,
  52146. 'WS_ERR_UNEXPECTED_RSV_1'
  52147. );
  52148. cb(error);
  52149. return;
  52150. }
  52151. if (!this._fragmented) {
  52152. const error = this.createError(
  52153. RangeError,
  52154. 'invalid opcode 0',
  52155. true,
  52156. 1002,
  52157. 'WS_ERR_INVALID_OPCODE'
  52158. );
  52159. cb(error);
  52160. return;
  52161. }
  52162. this._opcode = this._fragmented;
  52163. } else if (this._opcode === 0x01 || this._opcode === 0x02) {
  52164. if (this._fragmented) {
  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. this._compressed = compressed;
  52176. } else if (this._opcode > 0x07 && this._opcode < 0x0b) {
  52177. if (!this._fin) {
  52178. const error = this.createError(
  52179. RangeError,
  52180. 'FIN must be set',
  52181. true,
  52182. 1002,
  52183. 'WS_ERR_EXPECTED_FIN'
  52184. );
  52185. cb(error);
  52186. return;
  52187. }
  52188. if (compressed) {
  52189. const error = this.createError(
  52190. RangeError,
  52191. 'RSV1 must be clear',
  52192. true,
  52193. 1002,
  52194. 'WS_ERR_UNEXPECTED_RSV_1'
  52195. );
  52196. cb(error);
  52197. return;
  52198. }
  52199. if (
  52200. this._payloadLength > 0x7d ||
  52201. (this._opcode === 0x08 && this._payloadLength === 1)
  52202. ) {
  52203. const error = this.createError(
  52204. RangeError,
  52205. `invalid payload length ${this._payloadLength}`,
  52206. true,
  52207. 1002,
  52208. 'WS_ERR_INVALID_CONTROL_PAYLOAD_LENGTH'
  52209. );
  52210. cb(error);
  52211. return;
  52212. }
  52213. } else {
  52214. const error = this.createError(
  52215. RangeError,
  52216. `invalid opcode ${this._opcode}`,
  52217. true,
  52218. 1002,
  52219. 'WS_ERR_INVALID_OPCODE'
  52220. );
  52221. cb(error);
  52222. return;
  52223. }
  52224. if (!this._fin && !this._fragmented) this._fragmented = this._opcode;
  52225. this._masked = (buf[1] & 0x80) === 0x80;
  52226. if (this._isServer) {
  52227. if (!this._masked) {
  52228. const error = this.createError(
  52229. RangeError,
  52230. 'MASK must be set',
  52231. true,
  52232. 1002,
  52233. 'WS_ERR_EXPECTED_MASK'
  52234. );
  52235. cb(error);
  52236. return;
  52237. }
  52238. } else if (this._masked) {
  52239. const error = this.createError(
  52240. RangeError,
  52241. 'MASK must be clear',
  52242. true,
  52243. 1002,
  52244. 'WS_ERR_UNEXPECTED_MASK'
  52245. );
  52246. cb(error);
  52247. return;
  52248. }
  52249. if (this._payloadLength === 126) this._state = GET_PAYLOAD_LENGTH_16;
  52250. else if (this._payloadLength === 127) this._state = GET_PAYLOAD_LENGTH_64;
  52251. else this.haveLength(cb);
  52252. }
  52253. /**
  52254. * Gets extended payload length (7+16).
  52255. *
  52256. * @param {Function} cb Callback
  52257. * @private
  52258. */
  52259. getPayloadLength16(cb) {
  52260. if (this._bufferedBytes < 2) {
  52261. this._loop = false;
  52262. return;
  52263. }
  52264. this._payloadLength = this.consume(2).readUInt16BE(0);
  52265. this.haveLength(cb);
  52266. }
  52267. /**
  52268. * Gets extended payload length (7+64).
  52269. *
  52270. * @param {Function} cb Callback
  52271. * @private
  52272. */
  52273. getPayloadLength64(cb) {
  52274. if (this._bufferedBytes < 8) {
  52275. this._loop = false;
  52276. return;
  52277. }
  52278. const buf = this.consume(8);
  52279. const num = buf.readUInt32BE(0);
  52280. //
  52281. // The maximum safe integer in JavaScript is 2^53 - 1. An error is returned
  52282. // if payload length is greater than this number.
  52283. //
  52284. if (num > Math.pow(2, 53 - 32) - 1) {
  52285. const error = this.createError(
  52286. RangeError,
  52287. 'Unsupported WebSocket frame: payload length > 2^53 - 1',
  52288. false,
  52289. 1009,
  52290. 'WS_ERR_UNSUPPORTED_DATA_PAYLOAD_LENGTH'
  52291. );
  52292. cb(error);
  52293. return;
  52294. }
  52295. this._payloadLength = num * Math.pow(2, 32) + buf.readUInt32BE(4);
  52296. this.haveLength(cb);
  52297. }
  52298. /**
  52299. * Payload length has been read.
  52300. *
  52301. * @param {Function} cb Callback
  52302. * @private
  52303. */
  52304. haveLength(cb) {
  52305. if (this._payloadLength && this._opcode < 0x08) {
  52306. this._totalPayloadLength += this._payloadLength;
  52307. if (this._totalPayloadLength > this._maxPayload && this._maxPayload > 0) {
  52308. const error = this.createError(
  52309. RangeError,
  52310. 'Max payload size exceeded',
  52311. false,
  52312. 1009,
  52313. 'WS_ERR_UNSUPPORTED_MESSAGE_LENGTH'
  52314. );
  52315. cb(error);
  52316. return;
  52317. }
  52318. }
  52319. if (this._masked) this._state = GET_MASK;
  52320. else this._state = GET_DATA;
  52321. }
  52322. /**
  52323. * Reads mask bytes.
  52324. *
  52325. * @private
  52326. */
  52327. getMask() {
  52328. if (this._bufferedBytes < 4) {
  52329. this._loop = false;
  52330. return;
  52331. }
  52332. this._mask = this.consume(4);
  52333. this._state = GET_DATA;
  52334. }
  52335. /**
  52336. * Reads data bytes.
  52337. *
  52338. * @param {Function} cb Callback
  52339. * @private
  52340. */
  52341. getData(cb) {
  52342. let data = EMPTY_BUFFER$2;
  52343. if (this._payloadLength) {
  52344. if (this._bufferedBytes < this._payloadLength) {
  52345. this._loop = false;
  52346. return;
  52347. }
  52348. data = this.consume(this._payloadLength);
  52349. if (
  52350. this._masked &&
  52351. (this._mask[0] | this._mask[1] | this._mask[2] | this._mask[3]) !== 0
  52352. ) {
  52353. unmask(data, this._mask);
  52354. }
  52355. }
  52356. if (this._opcode > 0x07) {
  52357. this.controlMessage(data, cb);
  52358. return;
  52359. }
  52360. if (this._compressed) {
  52361. this._state = INFLATING;
  52362. this.decompress(data, cb);
  52363. return;
  52364. }
  52365. if (data.length) {
  52366. //
  52367. // This message is not compressed so its length is the sum of the payload
  52368. // length of all fragments.
  52369. //
  52370. this._messageLength = this._totalPayloadLength;
  52371. this._fragments.push(data);
  52372. }
  52373. this.dataMessage(cb);
  52374. }
  52375. /**
  52376. * Decompresses data.
  52377. *
  52378. * @param {Buffer} data Compressed data
  52379. * @param {Function} cb Callback
  52380. * @private
  52381. */
  52382. decompress(data, cb) {
  52383. const perMessageDeflate = this._extensions[PerMessageDeflate$3.extensionName];
  52384. perMessageDeflate.decompress(data, this._fin, (err, buf) => {
  52385. if (err) return cb(err);
  52386. if (buf.length) {
  52387. this._messageLength += buf.length;
  52388. if (this._messageLength > this._maxPayload && this._maxPayload > 0) {
  52389. const error = this.createError(
  52390. RangeError,
  52391. 'Max payload size exceeded',
  52392. false,
  52393. 1009,
  52394. 'WS_ERR_UNSUPPORTED_MESSAGE_LENGTH'
  52395. );
  52396. cb(error);
  52397. return;
  52398. }
  52399. this._fragments.push(buf);
  52400. }
  52401. this.dataMessage(cb);
  52402. if (this._state === GET_INFO) this.startLoop(cb);
  52403. });
  52404. }
  52405. /**
  52406. * Handles a data message.
  52407. *
  52408. * @param {Function} cb Callback
  52409. * @private
  52410. */
  52411. dataMessage(cb) {
  52412. if (!this._fin) {
  52413. this._state = GET_INFO;
  52414. return;
  52415. }
  52416. const messageLength = this._messageLength;
  52417. const fragments = this._fragments;
  52418. this._totalPayloadLength = 0;
  52419. this._messageLength = 0;
  52420. this._fragmented = 0;
  52421. this._fragments = [];
  52422. if (this._opcode === 2) {
  52423. let data;
  52424. if (this._binaryType === 'nodebuffer') {
  52425. data = concat(fragments, messageLength);
  52426. } else if (this._binaryType === 'arraybuffer') {
  52427. data = toArrayBuffer(concat(fragments, messageLength));
  52428. } else if (this._binaryType === 'blob') {
  52429. data = new Blob(fragments);
  52430. } else {
  52431. data = fragments;
  52432. }
  52433. if (this._allowSynchronousEvents) {
  52434. this.emit('message', data, true);
  52435. this._state = GET_INFO;
  52436. } else {
  52437. this._state = DEFER_EVENT;
  52438. setImmediate(() => {
  52439. this.emit('message', data, true);
  52440. this._state = GET_INFO;
  52441. this.startLoop(cb);
  52442. });
  52443. }
  52444. } else {
  52445. const buf = concat(fragments, messageLength);
  52446. if (!this._skipUTF8Validation && !isValidUTF8(buf)) {
  52447. const error = this.createError(
  52448. Error,
  52449. 'invalid UTF-8 sequence',
  52450. true,
  52451. 1007,
  52452. 'WS_ERR_INVALID_UTF8'
  52453. );
  52454. cb(error);
  52455. return;
  52456. }
  52457. if (this._state === INFLATING || this._allowSynchronousEvents) {
  52458. this.emit('message', buf, false);
  52459. this._state = GET_INFO;
  52460. } else {
  52461. this._state = DEFER_EVENT;
  52462. setImmediate(() => {
  52463. this.emit('message', buf, false);
  52464. this._state = GET_INFO;
  52465. this.startLoop(cb);
  52466. });
  52467. }
  52468. }
  52469. }
  52470. /**
  52471. * Handles a control message.
  52472. *
  52473. * @param {Buffer} data Data to handle
  52474. * @return {(Error|RangeError|undefined)} A possible error
  52475. * @private
  52476. */
  52477. controlMessage(data, cb) {
  52478. if (this._opcode === 0x08) {
  52479. if (data.length === 0) {
  52480. this._loop = false;
  52481. this.emit('conclude', 1005, EMPTY_BUFFER$2);
  52482. this.end();
  52483. } else {
  52484. const code = data.readUInt16BE(0);
  52485. if (!isValidStatusCode$1(code)) {
  52486. const error = this.createError(
  52487. RangeError,
  52488. `invalid status code ${code}`,
  52489. true,
  52490. 1002,
  52491. 'WS_ERR_INVALID_CLOSE_CODE'
  52492. );
  52493. cb(error);
  52494. return;
  52495. }
  52496. const buf = new FastBuffer(
  52497. data.buffer,
  52498. data.byteOffset + 2,
  52499. data.length - 2
  52500. );
  52501. if (!this._skipUTF8Validation && !isValidUTF8(buf)) {
  52502. const error = this.createError(
  52503. Error,
  52504. 'invalid UTF-8 sequence',
  52505. true,
  52506. 1007,
  52507. 'WS_ERR_INVALID_UTF8'
  52508. );
  52509. cb(error);
  52510. return;
  52511. }
  52512. this._loop = false;
  52513. this.emit('conclude', code, buf);
  52514. this.end();
  52515. }
  52516. this._state = GET_INFO;
  52517. return;
  52518. }
  52519. if (this._allowSynchronousEvents) {
  52520. this.emit(this._opcode === 0x09 ? 'ping' : 'pong', data);
  52521. this._state = GET_INFO;
  52522. } else {
  52523. this._state = DEFER_EVENT;
  52524. setImmediate(() => {
  52525. this.emit(this._opcode === 0x09 ? 'ping' : 'pong', data);
  52526. this._state = GET_INFO;
  52527. this.startLoop(cb);
  52528. });
  52529. }
  52530. }
  52531. /**
  52532. * Builds an error object.
  52533. *
  52534. * @param {function(new:Error|RangeError)} ErrorCtor The error constructor
  52535. * @param {String} message The error message
  52536. * @param {Boolean} prefix Specifies whether or not to add a default prefix to
  52537. * `message`
  52538. * @param {Number} statusCode The status code
  52539. * @param {String} errorCode The exposed error code
  52540. * @return {(Error|RangeError)} The error
  52541. * @private
  52542. */
  52543. createError(ErrorCtor, message, prefix, statusCode, errorCode) {
  52544. this._loop = false;
  52545. this._errored = true;
  52546. const err = new ErrorCtor(
  52547. prefix ? `Invalid WebSocket frame: ${message}` : message
  52548. );
  52549. Error.captureStackTrace(err, this.createError);
  52550. err.code = errorCode;
  52551. err[kStatusCode$1] = statusCode;
  52552. return err;
  52553. }
  52554. };
  52555. var receiver = Receiver$1;
  52556. /* eslint no-unused-vars: ["error", { "varsIgnorePattern": "^Duplex" }] */
  52557. const { randomFillSync } = require$$3$1;
  52558. const PerMessageDeflate$2 = permessageDeflate;
  52559. const { EMPTY_BUFFER: EMPTY_BUFFER$1, kWebSocket: kWebSocket$2, NOOP: NOOP$2 } = constants;
  52560. const { isBlob: isBlob$1, isValidStatusCode } = validationExports;
  52561. const { mask: applyMask, toBuffer: toBuffer$1 } = bufferUtilExports;
  52562. const kByteLength = Symbol('kByteLength');
  52563. const maskBuffer = Buffer.alloc(4);
  52564. const RANDOM_POOL_SIZE = 8 * 1024;
  52565. let randomPool;
  52566. let randomPoolPointer = RANDOM_POOL_SIZE;
  52567. const DEFAULT = 0;
  52568. const DEFLATING = 1;
  52569. const GET_BLOB_DATA = 2;
  52570. /**
  52571. * HyBi Sender implementation.
  52572. */
  52573. let Sender$1 = class Sender {
  52574. /**
  52575. * Creates a Sender instance.
  52576. *
  52577. * @param {Duplex} socket The connection socket
  52578. * @param {Object} [extensions] An object containing the negotiated extensions
  52579. * @param {Function} [generateMask] The function used to generate the masking
  52580. * key
  52581. */
  52582. constructor(socket, extensions, generateMask) {
  52583. this._extensions = extensions || {};
  52584. if (generateMask) {
  52585. this._generateMask = generateMask;
  52586. this._maskBuffer = Buffer.alloc(4);
  52587. }
  52588. this._socket = socket;
  52589. this._firstFragment = true;
  52590. this._compress = false;
  52591. this._bufferedBytes = 0;
  52592. this._queue = [];
  52593. this._state = DEFAULT;
  52594. this.onerror = NOOP$2;
  52595. this[kWebSocket$2] = undefined;
  52596. }
  52597. /**
  52598. * Frames a piece of data according to the HyBi WebSocket protocol.
  52599. *
  52600. * @param {(Buffer|String)} data The data to frame
  52601. * @param {Object} options Options object
  52602. * @param {Boolean} [options.fin=false] Specifies whether or not to set the
  52603. * FIN bit
  52604. * @param {Function} [options.generateMask] The function used to generate the
  52605. * masking key
  52606. * @param {Boolean} [options.mask=false] Specifies whether or not to mask
  52607. * `data`
  52608. * @param {Buffer} [options.maskBuffer] The buffer used to store the masking
  52609. * key
  52610. * @param {Number} options.opcode The opcode
  52611. * @param {Boolean} [options.readOnly=false] Specifies whether `data` can be
  52612. * modified
  52613. * @param {Boolean} [options.rsv1=false] Specifies whether or not to set the
  52614. * RSV1 bit
  52615. * @return {(Buffer|String)[]} The framed data
  52616. * @public
  52617. */
  52618. static frame(data, options) {
  52619. let mask;
  52620. let merge = false;
  52621. let offset = 2;
  52622. let skipMasking = false;
  52623. if (options.mask) {
  52624. mask = options.maskBuffer || maskBuffer;
  52625. if (options.generateMask) {
  52626. options.generateMask(mask);
  52627. } else {
  52628. if (randomPoolPointer === RANDOM_POOL_SIZE) {
  52629. /* istanbul ignore else */
  52630. if (randomPool === undefined) {
  52631. //
  52632. // This is lazily initialized because server-sent frames must not
  52633. // be masked so it may never be used.
  52634. //
  52635. randomPool = Buffer.alloc(RANDOM_POOL_SIZE);
  52636. }
  52637. randomFillSync(randomPool, 0, RANDOM_POOL_SIZE);
  52638. randomPoolPointer = 0;
  52639. }
  52640. mask[0] = randomPool[randomPoolPointer++];
  52641. mask[1] = randomPool[randomPoolPointer++];
  52642. mask[2] = randomPool[randomPoolPointer++];
  52643. mask[3] = randomPool[randomPoolPointer++];
  52644. }
  52645. skipMasking = (mask[0] | mask[1] | mask[2] | mask[3]) === 0;
  52646. offset = 6;
  52647. }
  52648. let dataLength;
  52649. if (typeof data === 'string') {
  52650. if (
  52651. (!options.mask || skipMasking) &&
  52652. options[kByteLength] !== undefined
  52653. ) {
  52654. dataLength = options[kByteLength];
  52655. } else {
  52656. data = Buffer.from(data);
  52657. dataLength = data.length;
  52658. }
  52659. } else {
  52660. dataLength = data.length;
  52661. merge = options.mask && options.readOnly && !skipMasking;
  52662. }
  52663. let payloadLength = dataLength;
  52664. if (dataLength >= 65536) {
  52665. offset += 8;
  52666. payloadLength = 127;
  52667. } else if (dataLength > 125) {
  52668. offset += 2;
  52669. payloadLength = 126;
  52670. }
  52671. const target = Buffer.allocUnsafe(merge ? dataLength + offset : offset);
  52672. target[0] = options.fin ? options.opcode | 0x80 : options.opcode;
  52673. if (options.rsv1) target[0] |= 0x40;
  52674. target[1] = payloadLength;
  52675. if (payloadLength === 126) {
  52676. target.writeUInt16BE(dataLength, 2);
  52677. } else if (payloadLength === 127) {
  52678. target[2] = target[3] = 0;
  52679. target.writeUIntBE(dataLength, 4, 6);
  52680. }
  52681. if (!options.mask) return [target, data];
  52682. target[1] |= 0x80;
  52683. target[offset - 4] = mask[0];
  52684. target[offset - 3] = mask[1];
  52685. target[offset - 2] = mask[2];
  52686. target[offset - 1] = mask[3];
  52687. if (skipMasking) return [target, data];
  52688. if (merge) {
  52689. applyMask(data, mask, target, offset, dataLength);
  52690. return [target];
  52691. }
  52692. applyMask(data, mask, data, 0, dataLength);
  52693. return [target, data];
  52694. }
  52695. /**
  52696. * Sends a close message to the other peer.
  52697. *
  52698. * @param {Number} [code] The status code component of the body
  52699. * @param {(String|Buffer)} [data] The message component of the body
  52700. * @param {Boolean} [mask=false] Specifies whether or not to mask the message
  52701. * @param {Function} [cb] Callback
  52702. * @public
  52703. */
  52704. close(code, data, mask, cb) {
  52705. let buf;
  52706. if (code === undefined) {
  52707. buf = EMPTY_BUFFER$1;
  52708. } else if (typeof code !== 'number' || !isValidStatusCode(code)) {
  52709. throw new TypeError('First argument must be a valid error code number');
  52710. } else if (data === undefined || !data.length) {
  52711. buf = Buffer.allocUnsafe(2);
  52712. buf.writeUInt16BE(code, 0);
  52713. } else {
  52714. const length = Buffer.byteLength(data);
  52715. if (length > 123) {
  52716. throw new RangeError('The message must not be greater than 123 bytes');
  52717. }
  52718. buf = Buffer.allocUnsafe(2 + length);
  52719. buf.writeUInt16BE(code, 0);
  52720. if (typeof data === 'string') {
  52721. buf.write(data, 2);
  52722. } else {
  52723. buf.set(data, 2);
  52724. }
  52725. }
  52726. const options = {
  52727. [kByteLength]: buf.length,
  52728. fin: true,
  52729. generateMask: this._generateMask,
  52730. mask,
  52731. maskBuffer: this._maskBuffer,
  52732. opcode: 0x08,
  52733. readOnly: false,
  52734. rsv1: false
  52735. };
  52736. if (this._state !== DEFAULT) {
  52737. this.enqueue([this.dispatch, buf, false, options, cb]);
  52738. } else {
  52739. this.sendFrame(Sender.frame(buf, options), cb);
  52740. }
  52741. }
  52742. /**
  52743. * Sends a ping message to the other peer.
  52744. *
  52745. * @param {*} data The message to send
  52746. * @param {Boolean} [mask=false] Specifies whether or not to mask `data`
  52747. * @param {Function} [cb] Callback
  52748. * @public
  52749. */
  52750. ping(data, mask, cb) {
  52751. let byteLength;
  52752. let readOnly;
  52753. if (typeof data === 'string') {
  52754. byteLength = Buffer.byteLength(data);
  52755. readOnly = false;
  52756. } else if (isBlob$1(data)) {
  52757. byteLength = data.size;
  52758. readOnly = false;
  52759. } else {
  52760. data = toBuffer$1(data);
  52761. byteLength = data.length;
  52762. readOnly = toBuffer$1.readOnly;
  52763. }
  52764. if (byteLength > 125) {
  52765. throw new RangeError('The data size must not be greater than 125 bytes');
  52766. }
  52767. const options = {
  52768. [kByteLength]: byteLength,
  52769. fin: true,
  52770. generateMask: this._generateMask,
  52771. mask,
  52772. maskBuffer: this._maskBuffer,
  52773. opcode: 0x09,
  52774. readOnly,
  52775. rsv1: false
  52776. };
  52777. if (isBlob$1(data)) {
  52778. if (this._state !== DEFAULT) {
  52779. this.enqueue([this.getBlobData, data, false, options, cb]);
  52780. } else {
  52781. this.getBlobData(data, false, options, cb);
  52782. }
  52783. } else if (this._state !== DEFAULT) {
  52784. this.enqueue([this.dispatch, data, false, options, cb]);
  52785. } else {
  52786. this.sendFrame(Sender.frame(data, options), cb);
  52787. }
  52788. }
  52789. /**
  52790. * Sends a pong message to the other peer.
  52791. *
  52792. * @param {*} data The message to send
  52793. * @param {Boolean} [mask=false] Specifies whether or not to mask `data`
  52794. * @param {Function} [cb] Callback
  52795. * @public
  52796. */
  52797. pong(data, mask, cb) {
  52798. let byteLength;
  52799. let readOnly;
  52800. if (typeof data === 'string') {
  52801. byteLength = Buffer.byteLength(data);
  52802. readOnly = false;
  52803. } else if (isBlob$1(data)) {
  52804. byteLength = data.size;
  52805. readOnly = false;
  52806. } else {
  52807. data = toBuffer$1(data);
  52808. byteLength = data.length;
  52809. readOnly = toBuffer$1.readOnly;
  52810. }
  52811. if (byteLength > 125) {
  52812. throw new RangeError('The data size must not be greater than 125 bytes');
  52813. }
  52814. const options = {
  52815. [kByteLength]: byteLength,
  52816. fin: true,
  52817. generateMask: this._generateMask,
  52818. mask,
  52819. maskBuffer: this._maskBuffer,
  52820. opcode: 0x0a,
  52821. readOnly,
  52822. rsv1: false
  52823. };
  52824. if (isBlob$1(data)) {
  52825. if (this._state !== DEFAULT) {
  52826. this.enqueue([this.getBlobData, data, false, options, cb]);
  52827. } else {
  52828. this.getBlobData(data, false, options, cb);
  52829. }
  52830. } else if (this._state !== DEFAULT) {
  52831. this.enqueue([this.dispatch, data, false, options, cb]);
  52832. } else {
  52833. this.sendFrame(Sender.frame(data, options), cb);
  52834. }
  52835. }
  52836. /**
  52837. * Sends a data message to the other peer.
  52838. *
  52839. * @param {*} data The message to send
  52840. * @param {Object} options Options object
  52841. * @param {Boolean} [options.binary=false] Specifies whether `data` is binary
  52842. * or text
  52843. * @param {Boolean} [options.compress=false] Specifies whether or not to
  52844. * compress `data`
  52845. * @param {Boolean} [options.fin=false] Specifies whether the fragment is the
  52846. * last one
  52847. * @param {Boolean} [options.mask=false] Specifies whether or not to mask
  52848. * `data`
  52849. * @param {Function} [cb] Callback
  52850. * @public
  52851. */
  52852. send(data, options, cb) {
  52853. const perMessageDeflate = this._extensions[PerMessageDeflate$2.extensionName];
  52854. let opcode = options.binary ? 2 : 1;
  52855. let rsv1 = options.compress;
  52856. let byteLength;
  52857. let readOnly;
  52858. if (typeof data === 'string') {
  52859. byteLength = Buffer.byteLength(data);
  52860. readOnly = false;
  52861. } else if (isBlob$1(data)) {
  52862. byteLength = data.size;
  52863. readOnly = false;
  52864. } else {
  52865. data = toBuffer$1(data);
  52866. byteLength = data.length;
  52867. readOnly = toBuffer$1.readOnly;
  52868. }
  52869. if (this._firstFragment) {
  52870. this._firstFragment = false;
  52871. if (
  52872. rsv1 &&
  52873. perMessageDeflate &&
  52874. perMessageDeflate.params[
  52875. perMessageDeflate._isServer
  52876. ? 'server_no_context_takeover'
  52877. : 'client_no_context_takeover'
  52878. ]
  52879. ) {
  52880. rsv1 = byteLength >= perMessageDeflate._threshold;
  52881. }
  52882. this._compress = rsv1;
  52883. } else {
  52884. rsv1 = false;
  52885. opcode = 0;
  52886. }
  52887. if (options.fin) this._firstFragment = true;
  52888. const opts = {
  52889. [kByteLength]: byteLength,
  52890. fin: options.fin,
  52891. generateMask: this._generateMask,
  52892. mask: options.mask,
  52893. maskBuffer: this._maskBuffer,
  52894. opcode,
  52895. readOnly,
  52896. rsv1
  52897. };
  52898. if (isBlob$1(data)) {
  52899. if (this._state !== DEFAULT) {
  52900. this.enqueue([this.getBlobData, data, this._compress, opts, cb]);
  52901. } else {
  52902. this.getBlobData(data, this._compress, opts, cb);
  52903. }
  52904. } else if (this._state !== DEFAULT) {
  52905. this.enqueue([this.dispatch, data, this._compress, opts, cb]);
  52906. } else {
  52907. this.dispatch(data, this._compress, opts, cb);
  52908. }
  52909. }
  52910. /**
  52911. * Gets the contents of a blob as binary data.
  52912. *
  52913. * @param {Blob} blob The blob
  52914. * @param {Boolean} [compress=false] Specifies whether or not to compress
  52915. * the data
  52916. * @param {Object} options Options object
  52917. * @param {Boolean} [options.fin=false] Specifies whether or not to set the
  52918. * FIN bit
  52919. * @param {Function} [options.generateMask] The function used to generate the
  52920. * masking key
  52921. * @param {Boolean} [options.mask=false] Specifies whether or not to mask
  52922. * `data`
  52923. * @param {Buffer} [options.maskBuffer] The buffer used to store the masking
  52924. * key
  52925. * @param {Number} options.opcode The opcode
  52926. * @param {Boolean} [options.readOnly=false] Specifies whether `data` can be
  52927. * modified
  52928. * @param {Boolean} [options.rsv1=false] Specifies whether or not to set the
  52929. * RSV1 bit
  52930. * @param {Function} [cb] Callback
  52931. * @private
  52932. */
  52933. getBlobData(blob, compress, options, cb) {
  52934. this._bufferedBytes += options[kByteLength];
  52935. this._state = GET_BLOB_DATA;
  52936. blob
  52937. .arrayBuffer()
  52938. .then((arrayBuffer) => {
  52939. if (this._socket.destroyed) {
  52940. const err = new Error(
  52941. 'The socket was closed while the blob was being read'
  52942. );
  52943. //
  52944. // `callCallbacks` is called in the next tick to ensure that errors
  52945. // that might be thrown in the callbacks behave like errors thrown
  52946. // outside the promise chain.
  52947. //
  52948. process.nextTick(callCallbacks, this, err, cb);
  52949. return;
  52950. }
  52951. this._bufferedBytes -= options[kByteLength];
  52952. const data = toBuffer$1(arrayBuffer);
  52953. if (!compress) {
  52954. this._state = DEFAULT;
  52955. this.sendFrame(Sender.frame(data, options), cb);
  52956. this.dequeue();
  52957. } else {
  52958. this.dispatch(data, compress, options, cb);
  52959. }
  52960. })
  52961. .catch((err) => {
  52962. //
  52963. // `onError` is called in the next tick for the same reason that
  52964. // `callCallbacks` above is.
  52965. //
  52966. process.nextTick(onError, this, err, cb);
  52967. });
  52968. }
  52969. /**
  52970. * Dispatches a message.
  52971. *
  52972. * @param {(Buffer|String)} data The message to send
  52973. * @param {Boolean} [compress=false] Specifies whether or not to compress
  52974. * `data`
  52975. * @param {Object} options Options object
  52976. * @param {Boolean} [options.fin=false] Specifies whether or not to set the
  52977. * FIN bit
  52978. * @param {Function} [options.generateMask] The function used to generate the
  52979. * masking key
  52980. * @param {Boolean} [options.mask=false] Specifies whether or not to mask
  52981. * `data`
  52982. * @param {Buffer} [options.maskBuffer] The buffer used to store the masking
  52983. * key
  52984. * @param {Number} options.opcode The opcode
  52985. * @param {Boolean} [options.readOnly=false] Specifies whether `data` can be
  52986. * modified
  52987. * @param {Boolean} [options.rsv1=false] Specifies whether or not to set the
  52988. * RSV1 bit
  52989. * @param {Function} [cb] Callback
  52990. * @private
  52991. */
  52992. dispatch(data, compress, options, cb) {
  52993. if (!compress) {
  52994. this.sendFrame(Sender.frame(data, options), cb);
  52995. return;
  52996. }
  52997. const perMessageDeflate = this._extensions[PerMessageDeflate$2.extensionName];
  52998. this._bufferedBytes += options[kByteLength];
  52999. this._state = DEFLATING;
  53000. perMessageDeflate.compress(data, options.fin, (_, buf) => {
  53001. if (this._socket.destroyed) {
  53002. const err = new Error(
  53003. 'The socket was closed while data was being compressed'
  53004. );
  53005. callCallbacks(this, err, cb);
  53006. return;
  53007. }
  53008. this._bufferedBytes -= options[kByteLength];
  53009. this._state = DEFAULT;
  53010. options.readOnly = false;
  53011. this.sendFrame(Sender.frame(buf, options), cb);
  53012. this.dequeue();
  53013. });
  53014. }
  53015. /**
  53016. * Executes queued send operations.
  53017. *
  53018. * @private
  53019. */
  53020. dequeue() {
  53021. while (this._state === DEFAULT && this._queue.length) {
  53022. const params = this._queue.shift();
  53023. this._bufferedBytes -= params[3][kByteLength];
  53024. Reflect.apply(params[0], this, params.slice(1));
  53025. }
  53026. }
  53027. /**
  53028. * Enqueues a send operation.
  53029. *
  53030. * @param {Array} params Send operation parameters.
  53031. * @private
  53032. */
  53033. enqueue(params) {
  53034. this._bufferedBytes += params[3][kByteLength];
  53035. this._queue.push(params);
  53036. }
  53037. /**
  53038. * Sends a frame.
  53039. *
  53040. * @param {Buffer[]} list The frame to send
  53041. * @param {Function} [cb] Callback
  53042. * @private
  53043. */
  53044. sendFrame(list, cb) {
  53045. if (list.length === 2) {
  53046. this._socket.cork();
  53047. this._socket.write(list[0]);
  53048. this._socket.write(list[1], cb);
  53049. this._socket.uncork();
  53050. } else {
  53051. this._socket.write(list[0], cb);
  53052. }
  53053. }
  53054. };
  53055. var sender = Sender$1;
  53056. /**
  53057. * Calls queued callbacks with an error.
  53058. *
  53059. * @param {Sender} sender The `Sender` instance
  53060. * @param {Error} err The error to call the callbacks with
  53061. * @param {Function} [cb] The first callback
  53062. * @private
  53063. */
  53064. function callCallbacks(sender, err, cb) {
  53065. if (typeof cb === 'function') cb(err);
  53066. for (let i = 0; i < sender._queue.length; i++) {
  53067. const params = sender._queue[i];
  53068. const callback = params[params.length - 1];
  53069. if (typeof callback === 'function') callback(err);
  53070. }
  53071. }
  53072. /**
  53073. * Handles a `Sender` error.
  53074. *
  53075. * @param {Sender} sender The `Sender` instance
  53076. * @param {Error} err The error
  53077. * @param {Function} [cb] The first pending callback
  53078. * @private
  53079. */
  53080. function onError(sender, err, cb) {
  53081. callCallbacks(sender, err, cb);
  53082. sender.onerror(err);
  53083. }
  53084. const { kForOnEventAttribute: kForOnEventAttribute$1, kListener: kListener$1 } = constants;
  53085. const kCode = Symbol('kCode');
  53086. const kData = Symbol('kData');
  53087. const kError = Symbol('kError');
  53088. const kMessage = Symbol('kMessage');
  53089. const kReason = Symbol('kReason');
  53090. const kTarget = Symbol('kTarget');
  53091. const kType = Symbol('kType');
  53092. const kWasClean = Symbol('kWasClean');
  53093. /**
  53094. * Class representing an event.
  53095. */
  53096. let Event$1 = class Event {
  53097. /**
  53098. * Create a new `Event`.
  53099. *
  53100. * @param {String} type The name of the event
  53101. * @throws {TypeError} If the `type` argument is not specified
  53102. */
  53103. constructor(type) {
  53104. this[kTarget] = null;
  53105. this[kType] = type;
  53106. }
  53107. /**
  53108. * @type {*}
  53109. */
  53110. get target() {
  53111. return this[kTarget];
  53112. }
  53113. /**
  53114. * @type {String}
  53115. */
  53116. get type() {
  53117. return this[kType];
  53118. }
  53119. };
  53120. Object.defineProperty(Event$1.prototype, 'target', { enumerable: true });
  53121. Object.defineProperty(Event$1.prototype, 'type', { enumerable: true });
  53122. /**
  53123. * Class representing a close event.
  53124. *
  53125. * @extends Event
  53126. */
  53127. class CloseEvent extends Event$1 {
  53128. /**
  53129. * Create a new `CloseEvent`.
  53130. *
  53131. * @param {String} type The name of the event
  53132. * @param {Object} [options] A dictionary object that allows for setting
  53133. * attributes via object members of the same name
  53134. * @param {Number} [options.code=0] The status code explaining why the
  53135. * connection was closed
  53136. * @param {String} [options.reason=''] A human-readable string explaining why
  53137. * the connection was closed
  53138. * @param {Boolean} [options.wasClean=false] Indicates whether or not the
  53139. * connection was cleanly closed
  53140. */
  53141. constructor(type, options = {}) {
  53142. super(type);
  53143. this[kCode] = options.code === undefined ? 0 : options.code;
  53144. this[kReason] = options.reason === undefined ? '' : options.reason;
  53145. this[kWasClean] = options.wasClean === undefined ? false : options.wasClean;
  53146. }
  53147. /**
  53148. * @type {Number}
  53149. */
  53150. get code() {
  53151. return this[kCode];
  53152. }
  53153. /**
  53154. * @type {String}
  53155. */
  53156. get reason() {
  53157. return this[kReason];
  53158. }
  53159. /**
  53160. * @type {Boolean}
  53161. */
  53162. get wasClean() {
  53163. return this[kWasClean];
  53164. }
  53165. }
  53166. Object.defineProperty(CloseEvent.prototype, 'code', { enumerable: true });
  53167. Object.defineProperty(CloseEvent.prototype, 'reason', { enumerable: true });
  53168. Object.defineProperty(CloseEvent.prototype, 'wasClean', { enumerable: true });
  53169. /**
  53170. * Class representing an error event.
  53171. *
  53172. * @extends Event
  53173. */
  53174. class ErrorEvent extends Event$1 {
  53175. /**
  53176. * Create a new `ErrorEvent`.
  53177. *
  53178. * @param {String} type The name of the event
  53179. * @param {Object} [options] A dictionary object that allows for setting
  53180. * attributes via object members of the same name
  53181. * @param {*} [options.error=null] The error that generated this event
  53182. * @param {String} [options.message=''] The error message
  53183. */
  53184. constructor(type, options = {}) {
  53185. super(type);
  53186. this[kError] = options.error === undefined ? null : options.error;
  53187. this[kMessage] = options.message === undefined ? '' : options.message;
  53188. }
  53189. /**
  53190. * @type {*}
  53191. */
  53192. get error() {
  53193. return this[kError];
  53194. }
  53195. /**
  53196. * @type {String}
  53197. */
  53198. get message() {
  53199. return this[kMessage];
  53200. }
  53201. }
  53202. Object.defineProperty(ErrorEvent.prototype, 'error', { enumerable: true });
  53203. Object.defineProperty(ErrorEvent.prototype, 'message', { enumerable: true });
  53204. /**
  53205. * Class representing a message event.
  53206. *
  53207. * @extends Event
  53208. */
  53209. class MessageEvent extends Event$1 {
  53210. /**
  53211. * Create a new `MessageEvent`.
  53212. *
  53213. * @param {String} type The name of the event
  53214. * @param {Object} [options] A dictionary object that allows for setting
  53215. * attributes via object members of the same name
  53216. * @param {*} [options.data=null] The message content
  53217. */
  53218. constructor(type, options = {}) {
  53219. super(type);
  53220. this[kData] = options.data === undefined ? null : options.data;
  53221. }
  53222. /**
  53223. * @type {*}
  53224. */
  53225. get data() {
  53226. return this[kData];
  53227. }
  53228. }
  53229. Object.defineProperty(MessageEvent.prototype, 'data', { enumerable: true });
  53230. /**
  53231. * This provides methods for emulating the `EventTarget` interface. It's not
  53232. * meant to be used directly.
  53233. *
  53234. * @mixin
  53235. */
  53236. const EventTarget = {
  53237. /**
  53238. * Register an event listener.
  53239. *
  53240. * @param {String} type A string representing the event type to listen for
  53241. * @param {(Function|Object)} handler The listener to add
  53242. * @param {Object} [options] An options object specifies characteristics about
  53243. * the event listener
  53244. * @param {Boolean} [options.once=false] A `Boolean` indicating that the
  53245. * listener should be invoked at most once after being added. If `true`,
  53246. * the listener would be automatically removed when invoked.
  53247. * @public
  53248. */
  53249. addEventListener(type, handler, options = {}) {
  53250. for (const listener of this.listeners(type)) {
  53251. if (
  53252. !options[kForOnEventAttribute$1] &&
  53253. listener[kListener$1] === handler &&
  53254. !listener[kForOnEventAttribute$1]
  53255. ) {
  53256. return;
  53257. }
  53258. }
  53259. let wrapper;
  53260. if (type === 'message') {
  53261. wrapper = function onMessage(data, isBinary) {
  53262. const event = new MessageEvent('message', {
  53263. data: isBinary ? data : data.toString()
  53264. });
  53265. event[kTarget] = this;
  53266. callListener(handler, this, event);
  53267. };
  53268. } else if (type === 'close') {
  53269. wrapper = function onClose(code, message) {
  53270. const event = new CloseEvent('close', {
  53271. code,
  53272. reason: message.toString(),
  53273. wasClean: this._closeFrameReceived && this._closeFrameSent
  53274. });
  53275. event[kTarget] = this;
  53276. callListener(handler, this, event);
  53277. };
  53278. } else if (type === 'error') {
  53279. wrapper = function onError(error) {
  53280. const event = new ErrorEvent('error', {
  53281. error,
  53282. message: error.message
  53283. });
  53284. event[kTarget] = this;
  53285. callListener(handler, this, event);
  53286. };
  53287. } else if (type === 'open') {
  53288. wrapper = function onOpen() {
  53289. const event = new Event$1('open');
  53290. event[kTarget] = this;
  53291. callListener(handler, this, event);
  53292. };
  53293. } else {
  53294. return;
  53295. }
  53296. wrapper[kForOnEventAttribute$1] = !!options[kForOnEventAttribute$1];
  53297. wrapper[kListener$1] = handler;
  53298. if (options.once) {
  53299. this.once(type, wrapper);
  53300. } else {
  53301. this.on(type, wrapper);
  53302. }
  53303. },
  53304. /**
  53305. * Remove an event listener.
  53306. *
  53307. * @param {String} type A string representing the event type to remove
  53308. * @param {(Function|Object)} handler The listener to remove
  53309. * @public
  53310. */
  53311. removeEventListener(type, handler) {
  53312. for (const listener of this.listeners(type)) {
  53313. if (listener[kListener$1] === handler && !listener[kForOnEventAttribute$1]) {
  53314. this.removeListener(type, listener);
  53315. break;
  53316. }
  53317. }
  53318. }
  53319. };
  53320. var eventTarget = {
  53321. CloseEvent,
  53322. ErrorEvent,
  53323. Event: Event$1,
  53324. EventTarget,
  53325. MessageEvent
  53326. };
  53327. /**
  53328. * Call an event listener
  53329. *
  53330. * @param {(Function|Object)} listener The listener to call
  53331. * @param {*} thisArg The value to use as `this`` when calling the listener
  53332. * @param {Event} event The event to pass to the listener
  53333. * @private
  53334. */
  53335. function callListener(listener, thisArg, event) {
  53336. if (typeof listener === 'object' && listener.handleEvent) {
  53337. listener.handleEvent.call(listener, event);
  53338. } else {
  53339. listener.call(thisArg, event);
  53340. }
  53341. }
  53342. const { tokenChars: tokenChars$1 } = validationExports;
  53343. /**
  53344. * Adds an offer to the map of extension offers or a parameter to the map of
  53345. * parameters.
  53346. *
  53347. * @param {Object} dest The map of extension offers or parameters
  53348. * @param {String} name The extension or parameter name
  53349. * @param {(Object|Boolean|String)} elem The extension parameters or the
  53350. * parameter value
  53351. * @private
  53352. */
  53353. function push(dest, name, elem) {
  53354. if (dest[name] === undefined) dest[name] = [elem];
  53355. else dest[name].push(elem);
  53356. }
  53357. /**
  53358. * Parses the `Sec-WebSocket-Extensions` header into an object.
  53359. *
  53360. * @param {String} header The field value of the header
  53361. * @return {Object} The parsed object
  53362. * @public
  53363. */
  53364. function parse$2(header) {
  53365. const offers = Object.create(null);
  53366. let params = Object.create(null);
  53367. let mustUnescape = false;
  53368. let isEscaping = false;
  53369. let inQuotes = false;
  53370. let extensionName;
  53371. let paramName;
  53372. let start = -1;
  53373. let code = -1;
  53374. let end = -1;
  53375. let i = 0;
  53376. for (; i < header.length; i++) {
  53377. code = header.charCodeAt(i);
  53378. if (extensionName === undefined) {
  53379. if (end === -1 && tokenChars$1[code] === 1) {
  53380. if (start === -1) start = i;
  53381. } else if (
  53382. i !== 0 &&
  53383. (code === 0x20 /* ' ' */ || code === 0x09) /* '\t' */
  53384. ) {
  53385. if (end === -1 && start !== -1) end = i;
  53386. } else if (code === 0x3b /* ';' */ || code === 0x2c /* ',' */) {
  53387. if (start === -1) {
  53388. throw new SyntaxError(`Unexpected character at index ${i}`);
  53389. }
  53390. if (end === -1) end = i;
  53391. const name = header.slice(start, end);
  53392. if (code === 0x2c) {
  53393. push(offers, name, params);
  53394. params = Object.create(null);
  53395. } else {
  53396. extensionName = name;
  53397. }
  53398. start = end = -1;
  53399. } else {
  53400. throw new SyntaxError(`Unexpected character at index ${i}`);
  53401. }
  53402. } else if (paramName === undefined) {
  53403. if (end === -1 && tokenChars$1[code] === 1) {
  53404. if (start === -1) start = i;
  53405. } else if (code === 0x20 || code === 0x09) {
  53406. if (end === -1 && start !== -1) end = i;
  53407. } else if (code === 0x3b || code === 0x2c) {
  53408. if (start === -1) {
  53409. throw new SyntaxError(`Unexpected character at index ${i}`);
  53410. }
  53411. if (end === -1) end = i;
  53412. push(params, header.slice(start, end), true);
  53413. if (code === 0x2c) {
  53414. push(offers, extensionName, params);
  53415. params = Object.create(null);
  53416. extensionName = undefined;
  53417. }
  53418. start = end = -1;
  53419. } else if (code === 0x3d /* '=' */ && start !== -1 && end === -1) {
  53420. paramName = header.slice(start, i);
  53421. start = end = -1;
  53422. } else {
  53423. throw new SyntaxError(`Unexpected character at index ${i}`);
  53424. }
  53425. } else {
  53426. //
  53427. // The value of a quoted-string after unescaping must conform to the
  53428. // token ABNF, so only token characters are valid.
  53429. // Ref: https://tools.ietf.org/html/rfc6455#section-9.1
  53430. //
  53431. if (isEscaping) {
  53432. if (tokenChars$1[code] !== 1) {
  53433. throw new SyntaxError(`Unexpected character at index ${i}`);
  53434. }
  53435. if (start === -1) start = i;
  53436. else if (!mustUnescape) mustUnescape = true;
  53437. isEscaping = false;
  53438. } else if (inQuotes) {
  53439. if (tokenChars$1[code] === 1) {
  53440. if (start === -1) start = i;
  53441. } else if (code === 0x22 /* '"' */ && start !== -1) {
  53442. inQuotes = false;
  53443. end = i;
  53444. } else if (code === 0x5c /* '\' */) {
  53445. isEscaping = true;
  53446. } else {
  53447. throw new SyntaxError(`Unexpected character at index ${i}`);
  53448. }
  53449. } else if (code === 0x22 && header.charCodeAt(i - 1) === 0x3d) {
  53450. inQuotes = true;
  53451. } else if (end === -1 && tokenChars$1[code] === 1) {
  53452. if (start === -1) start = i;
  53453. } else if (start !== -1 && (code === 0x20 || code === 0x09)) {
  53454. if (end === -1) end = i;
  53455. } else if (code === 0x3b || code === 0x2c) {
  53456. if (start === -1) {
  53457. throw new SyntaxError(`Unexpected character at index ${i}`);
  53458. }
  53459. if (end === -1) end = i;
  53460. let value = header.slice(start, end);
  53461. if (mustUnescape) {
  53462. value = value.replace(/\\/g, '');
  53463. mustUnescape = false;
  53464. }
  53465. push(params, paramName, value);
  53466. if (code === 0x2c) {
  53467. push(offers, extensionName, params);
  53468. params = Object.create(null);
  53469. extensionName = undefined;
  53470. }
  53471. paramName = undefined;
  53472. start = end = -1;
  53473. } else {
  53474. throw new SyntaxError(`Unexpected character at index ${i}`);
  53475. }
  53476. }
  53477. }
  53478. if (start === -1 || inQuotes || code === 0x20 || code === 0x09) {
  53479. throw new SyntaxError('Unexpected end of input');
  53480. }
  53481. if (end === -1) end = i;
  53482. const token = header.slice(start, end);
  53483. if (extensionName === undefined) {
  53484. push(offers, token, params);
  53485. } else {
  53486. if (paramName === undefined) {
  53487. push(params, token, true);
  53488. } else if (mustUnescape) {
  53489. push(params, paramName, token.replace(/\\/g, ''));
  53490. } else {
  53491. push(params, paramName, token);
  53492. }
  53493. push(offers, extensionName, params);
  53494. }
  53495. return offers;
  53496. }
  53497. /**
  53498. * Builds the `Sec-WebSocket-Extensions` header field value.
  53499. *
  53500. * @param {Object} extensions The map of extensions and parameters to format
  53501. * @return {String} A string representing the given object
  53502. * @public
  53503. */
  53504. function format$1(extensions) {
  53505. return Object.keys(extensions)
  53506. .map((extension) => {
  53507. let configurations = extensions[extension];
  53508. if (!Array.isArray(configurations)) configurations = [configurations];
  53509. return configurations
  53510. .map((params) => {
  53511. return [extension]
  53512. .concat(
  53513. Object.keys(params).map((k) => {
  53514. let values = params[k];
  53515. if (!Array.isArray(values)) values = [values];
  53516. return values
  53517. .map((v) => (v === true ? k : `${k}=${v}`))
  53518. .join('; ');
  53519. })
  53520. )
  53521. .join('; ');
  53522. })
  53523. .join(', ');
  53524. })
  53525. .join(', ');
  53526. }
  53527. var extension$1 = { format: format$1, parse: parse$2 };
  53528. /* eslint no-unused-vars: ["error", { "varsIgnorePattern": "^Duplex|Readable$", "caughtErrors": "none" }] */
  53529. const EventEmitter$1 = require$$0$7;
  53530. const https$2 = require$$1$1;
  53531. const http$3 = require$$1;
  53532. const net = require$$4$1;
  53533. const tls = require$$4$2;
  53534. const { randomBytes, createHash: createHash$1 } = require$$3$1;
  53535. const { URL: URL$2 } = require$$0$9;
  53536. const PerMessageDeflate$1 = permessageDeflate;
  53537. const Receiver = receiver;
  53538. const Sender = sender;
  53539. const { isBlob } = validationExports;
  53540. const {
  53541. BINARY_TYPES,
  53542. EMPTY_BUFFER,
  53543. GUID: GUID$1,
  53544. kForOnEventAttribute,
  53545. kListener,
  53546. kStatusCode,
  53547. kWebSocket: kWebSocket$1,
  53548. NOOP: NOOP$1
  53549. } = constants;
  53550. const {
  53551. EventTarget: { addEventListener, removeEventListener }
  53552. } = eventTarget;
  53553. const { format, parse: parse$1 } = extension$1;
  53554. const { toBuffer } = bufferUtilExports;
  53555. const closeTimeout = 30 * 1000;
  53556. const kAborted = Symbol('kAborted');
  53557. const protocolVersions = [8, 13];
  53558. const readyStates = ['CONNECTING', 'OPEN', 'CLOSING', 'CLOSED'];
  53559. const subprotocolRegex = /^[!#$%&'*+\-.0-9A-Z^_`|a-z~]+$/;
  53560. /**
  53561. * Class representing a WebSocket.
  53562. *
  53563. * @extends EventEmitter
  53564. */
  53565. let WebSocket$1 = class WebSocket extends EventEmitter$1 {
  53566. /**
  53567. * Create a new `WebSocket`.
  53568. *
  53569. * @param {(String|URL)} address The URL to which to connect
  53570. * @param {(String|String[])} [protocols] The subprotocols
  53571. * @param {Object} [options] Connection options
  53572. */
  53573. constructor(address, protocols, options) {
  53574. super();
  53575. this._binaryType = BINARY_TYPES[0];
  53576. this._closeCode = 1006;
  53577. this._closeFrameReceived = false;
  53578. this._closeFrameSent = false;
  53579. this._closeMessage = EMPTY_BUFFER;
  53580. this._closeTimer = null;
  53581. this._errorEmitted = false;
  53582. this._extensions = {};
  53583. this._paused = false;
  53584. this._protocol = '';
  53585. this._readyState = WebSocket.CONNECTING;
  53586. this._receiver = null;
  53587. this._sender = null;
  53588. this._socket = null;
  53589. if (address !== null) {
  53590. this._bufferedAmount = 0;
  53591. this._isServer = false;
  53592. this._redirects = 0;
  53593. if (protocols === undefined) {
  53594. protocols = [];
  53595. } else if (!Array.isArray(protocols)) {
  53596. if (typeof protocols === 'object' && protocols !== null) {
  53597. options = protocols;
  53598. protocols = [];
  53599. } else {
  53600. protocols = [protocols];
  53601. }
  53602. }
  53603. initAsClient(this, address, protocols, options);
  53604. } else {
  53605. this._autoPong = options.autoPong;
  53606. this._isServer = true;
  53607. }
  53608. }
  53609. /**
  53610. * For historical reasons, the custom "nodebuffer" type is used by the default
  53611. * instead of "blob".
  53612. *
  53613. * @type {String}
  53614. */
  53615. get binaryType() {
  53616. return this._binaryType;
  53617. }
  53618. set binaryType(type) {
  53619. if (!BINARY_TYPES.includes(type)) return;
  53620. this._binaryType = type;
  53621. //
  53622. // Allow to change `binaryType` on the fly.
  53623. //
  53624. if (this._receiver) this._receiver._binaryType = type;
  53625. }
  53626. /**
  53627. * @type {Number}
  53628. */
  53629. get bufferedAmount() {
  53630. if (!this._socket) return this._bufferedAmount;
  53631. return this._socket._writableState.length + this._sender._bufferedBytes;
  53632. }
  53633. /**
  53634. * @type {String}
  53635. */
  53636. get extensions() {
  53637. return Object.keys(this._extensions).join();
  53638. }
  53639. /**
  53640. * @type {Boolean}
  53641. */
  53642. get isPaused() {
  53643. return this._paused;
  53644. }
  53645. /**
  53646. * @type {Function}
  53647. */
  53648. /* istanbul ignore next */
  53649. get onclose() {
  53650. return null;
  53651. }
  53652. /**
  53653. * @type {Function}
  53654. */
  53655. /* istanbul ignore next */
  53656. get onerror() {
  53657. return null;
  53658. }
  53659. /**
  53660. * @type {Function}
  53661. */
  53662. /* istanbul ignore next */
  53663. get onopen() {
  53664. return null;
  53665. }
  53666. /**
  53667. * @type {Function}
  53668. */
  53669. /* istanbul ignore next */
  53670. get onmessage() {
  53671. return null;
  53672. }
  53673. /**
  53674. * @type {String}
  53675. */
  53676. get protocol() {
  53677. return this._protocol;
  53678. }
  53679. /**
  53680. * @type {Number}
  53681. */
  53682. get readyState() {
  53683. return this._readyState;
  53684. }
  53685. /**
  53686. * @type {String}
  53687. */
  53688. get url() {
  53689. return this._url;
  53690. }
  53691. /**
  53692. * Set up the socket and the internal resources.
  53693. *
  53694. * @param {Duplex} socket The network socket between the server and client
  53695. * @param {Buffer} head The first packet of the upgraded stream
  53696. * @param {Object} options Options object
  53697. * @param {Boolean} [options.allowSynchronousEvents=false] Specifies whether
  53698. * any of the `'message'`, `'ping'`, and `'pong'` events can be emitted
  53699. * multiple times in the same tick
  53700. * @param {Function} [options.generateMask] The function used to generate the
  53701. * masking key
  53702. * @param {Number} [options.maxPayload=0] The maximum allowed message size
  53703. * @param {Boolean} [options.skipUTF8Validation=false] Specifies whether or
  53704. * not to skip UTF-8 validation for text and close messages
  53705. * @private
  53706. */
  53707. setSocket(socket, head, options) {
  53708. const receiver = new Receiver({
  53709. allowSynchronousEvents: options.allowSynchronousEvents,
  53710. binaryType: this.binaryType,
  53711. extensions: this._extensions,
  53712. isServer: this._isServer,
  53713. maxPayload: options.maxPayload,
  53714. skipUTF8Validation: options.skipUTF8Validation
  53715. });
  53716. const sender = new Sender(socket, this._extensions, options.generateMask);
  53717. this._receiver = receiver;
  53718. this._sender = sender;
  53719. this._socket = socket;
  53720. receiver[kWebSocket$1] = this;
  53721. sender[kWebSocket$1] = this;
  53722. socket[kWebSocket$1] = this;
  53723. receiver.on('conclude', receiverOnConclude);
  53724. receiver.on('drain', receiverOnDrain);
  53725. receiver.on('error', receiverOnError);
  53726. receiver.on('message', receiverOnMessage);
  53727. receiver.on('ping', receiverOnPing);
  53728. receiver.on('pong', receiverOnPong);
  53729. sender.onerror = senderOnError;
  53730. //
  53731. // These methods may not be available if `socket` is just a `Duplex`.
  53732. //
  53733. if (socket.setTimeout) socket.setTimeout(0);
  53734. if (socket.setNoDelay) socket.setNoDelay();
  53735. if (head.length > 0) socket.unshift(head);
  53736. socket.on('close', socketOnClose);
  53737. socket.on('data', socketOnData);
  53738. socket.on('end', socketOnEnd);
  53739. socket.on('error', socketOnError$1);
  53740. this._readyState = WebSocket.OPEN;
  53741. this.emit('open');
  53742. }
  53743. /**
  53744. * Emit the `'close'` event.
  53745. *
  53746. * @private
  53747. */
  53748. emitClose() {
  53749. if (!this._socket) {
  53750. this._readyState = WebSocket.CLOSED;
  53751. this.emit('close', this._closeCode, this._closeMessage);
  53752. return;
  53753. }
  53754. if (this._extensions[PerMessageDeflate$1.extensionName]) {
  53755. this._extensions[PerMessageDeflate$1.extensionName].cleanup();
  53756. }
  53757. this._receiver.removeAllListeners();
  53758. this._readyState = WebSocket.CLOSED;
  53759. this.emit('close', this._closeCode, this._closeMessage);
  53760. }
  53761. /**
  53762. * Start a closing handshake.
  53763. *
  53764. * +----------+ +-----------+ +----------+
  53765. * - - -|ws.close()|-->|close frame|-->|ws.close()|- - -
  53766. * | +----------+ +-----------+ +----------+ |
  53767. * +----------+ +-----------+ |
  53768. * CLOSING |ws.close()|<--|close frame|<--+-----+ CLOSING
  53769. * +----------+ +-----------+ |
  53770. * | | | +---+ |
  53771. * +------------------------+-->|fin| - - - -
  53772. * | +---+ | +---+
  53773. * - - - - -|fin|<---------------------+
  53774. * +---+
  53775. *
  53776. * @param {Number} [code] Status code explaining why the connection is closing
  53777. * @param {(String|Buffer)} [data] The reason why the connection is
  53778. * closing
  53779. * @public
  53780. */
  53781. close(code, data) {
  53782. if (this.readyState === WebSocket.CLOSED) return;
  53783. if (this.readyState === WebSocket.CONNECTING) {
  53784. const msg = 'WebSocket was closed before the connection was established';
  53785. abortHandshake$1(this, this._req, msg);
  53786. return;
  53787. }
  53788. if (this.readyState === WebSocket.CLOSING) {
  53789. if (
  53790. this._closeFrameSent &&
  53791. (this._closeFrameReceived || this._receiver._writableState.errorEmitted)
  53792. ) {
  53793. this._socket.end();
  53794. }
  53795. return;
  53796. }
  53797. this._readyState = WebSocket.CLOSING;
  53798. this._sender.close(code, data, !this._isServer, (err) => {
  53799. //
  53800. // This error is handled by the `'error'` listener on the socket. We only
  53801. // want to know if the close frame has been sent here.
  53802. //
  53803. if (err) return;
  53804. this._closeFrameSent = true;
  53805. if (
  53806. this._closeFrameReceived ||
  53807. this._receiver._writableState.errorEmitted
  53808. ) {
  53809. this._socket.end();
  53810. }
  53811. });
  53812. setCloseTimer(this);
  53813. }
  53814. /**
  53815. * Pause the socket.
  53816. *
  53817. * @public
  53818. */
  53819. pause() {
  53820. if (
  53821. this.readyState === WebSocket.CONNECTING ||
  53822. this.readyState === WebSocket.CLOSED
  53823. ) {
  53824. return;
  53825. }
  53826. this._paused = true;
  53827. this._socket.pause();
  53828. }
  53829. /**
  53830. * Send a ping.
  53831. *
  53832. * @param {*} [data] The data to send
  53833. * @param {Boolean} [mask] Indicates whether or not to mask `data`
  53834. * @param {Function} [cb] Callback which is executed when the ping is sent
  53835. * @public
  53836. */
  53837. ping(data, mask, cb) {
  53838. if (this.readyState === WebSocket.CONNECTING) {
  53839. throw new Error('WebSocket is not open: readyState 0 (CONNECTING)');
  53840. }
  53841. if (typeof data === 'function') {
  53842. cb = data;
  53843. data = mask = undefined;
  53844. } else if (typeof mask === 'function') {
  53845. cb = mask;
  53846. mask = undefined;
  53847. }
  53848. if (typeof data === 'number') data = data.toString();
  53849. if (this.readyState !== WebSocket.OPEN) {
  53850. sendAfterClose(this, data, cb);
  53851. return;
  53852. }
  53853. if (mask === undefined) mask = !this._isServer;
  53854. this._sender.ping(data || EMPTY_BUFFER, mask, cb);
  53855. }
  53856. /**
  53857. * Send a pong.
  53858. *
  53859. * @param {*} [data] The data to send
  53860. * @param {Boolean} [mask] Indicates whether or not to mask `data`
  53861. * @param {Function} [cb] Callback which is executed when the pong is sent
  53862. * @public
  53863. */
  53864. pong(data, mask, cb) {
  53865. if (this.readyState === WebSocket.CONNECTING) {
  53866. throw new Error('WebSocket is not open: readyState 0 (CONNECTING)');
  53867. }
  53868. if (typeof data === 'function') {
  53869. cb = data;
  53870. data = mask = undefined;
  53871. } else if (typeof mask === 'function') {
  53872. cb = mask;
  53873. mask = undefined;
  53874. }
  53875. if (typeof data === 'number') data = data.toString();
  53876. if (this.readyState !== WebSocket.OPEN) {
  53877. sendAfterClose(this, data, cb);
  53878. return;
  53879. }
  53880. if (mask === undefined) mask = !this._isServer;
  53881. this._sender.pong(data || EMPTY_BUFFER, mask, cb);
  53882. }
  53883. /**
  53884. * Resume the socket.
  53885. *
  53886. * @public
  53887. */
  53888. resume() {
  53889. if (
  53890. this.readyState === WebSocket.CONNECTING ||
  53891. this.readyState === WebSocket.CLOSED
  53892. ) {
  53893. return;
  53894. }
  53895. this._paused = false;
  53896. if (!this._receiver._writableState.needDrain) this._socket.resume();
  53897. }
  53898. /**
  53899. * Send a data message.
  53900. *
  53901. * @param {*} data The message to send
  53902. * @param {Object} [options] Options object
  53903. * @param {Boolean} [options.binary] Specifies whether `data` is binary or
  53904. * text
  53905. * @param {Boolean} [options.compress] Specifies whether or not to compress
  53906. * `data`
  53907. * @param {Boolean} [options.fin=true] Specifies whether the fragment is the
  53908. * last one
  53909. * @param {Boolean} [options.mask] Specifies whether or not to mask `data`
  53910. * @param {Function} [cb] Callback which is executed when data is written out
  53911. * @public
  53912. */
  53913. send(data, options, cb) {
  53914. if (this.readyState === WebSocket.CONNECTING) {
  53915. throw new Error('WebSocket is not open: readyState 0 (CONNECTING)');
  53916. }
  53917. if (typeof options === 'function') {
  53918. cb = options;
  53919. options = {};
  53920. }
  53921. if (typeof data === 'number') data = data.toString();
  53922. if (this.readyState !== WebSocket.OPEN) {
  53923. sendAfterClose(this, data, cb);
  53924. return;
  53925. }
  53926. const opts = {
  53927. binary: typeof data !== 'string',
  53928. mask: !this._isServer,
  53929. compress: true,
  53930. fin: true,
  53931. ...options
  53932. };
  53933. if (!this._extensions[PerMessageDeflate$1.extensionName]) {
  53934. opts.compress = false;
  53935. }
  53936. this._sender.send(data || EMPTY_BUFFER, opts, cb);
  53937. }
  53938. /**
  53939. * Forcibly close the connection.
  53940. *
  53941. * @public
  53942. */
  53943. terminate() {
  53944. if (this.readyState === WebSocket.CLOSED) return;
  53945. if (this.readyState === WebSocket.CONNECTING) {
  53946. const msg = 'WebSocket was closed before the connection was established';
  53947. abortHandshake$1(this, this._req, msg);
  53948. return;
  53949. }
  53950. if (this._socket) {
  53951. this._readyState = WebSocket.CLOSING;
  53952. this._socket.destroy();
  53953. }
  53954. }
  53955. };
  53956. /**
  53957. * @constant {Number} CONNECTING
  53958. * @memberof WebSocket
  53959. */
  53960. Object.defineProperty(WebSocket$1, 'CONNECTING', {
  53961. enumerable: true,
  53962. value: readyStates.indexOf('CONNECTING')
  53963. });
  53964. /**
  53965. * @constant {Number} CONNECTING
  53966. * @memberof WebSocket.prototype
  53967. */
  53968. Object.defineProperty(WebSocket$1.prototype, 'CONNECTING', {
  53969. enumerable: true,
  53970. value: readyStates.indexOf('CONNECTING')
  53971. });
  53972. /**
  53973. * @constant {Number} OPEN
  53974. * @memberof WebSocket
  53975. */
  53976. Object.defineProperty(WebSocket$1, 'OPEN', {
  53977. enumerable: true,
  53978. value: readyStates.indexOf('OPEN')
  53979. });
  53980. /**
  53981. * @constant {Number} OPEN
  53982. * @memberof WebSocket.prototype
  53983. */
  53984. Object.defineProperty(WebSocket$1.prototype, 'OPEN', {
  53985. enumerable: true,
  53986. value: readyStates.indexOf('OPEN')
  53987. });
  53988. /**
  53989. * @constant {Number} CLOSING
  53990. * @memberof WebSocket
  53991. */
  53992. Object.defineProperty(WebSocket$1, 'CLOSING', {
  53993. enumerable: true,
  53994. value: readyStates.indexOf('CLOSING')
  53995. });
  53996. /**
  53997. * @constant {Number} CLOSING
  53998. * @memberof WebSocket.prototype
  53999. */
  54000. Object.defineProperty(WebSocket$1.prototype, 'CLOSING', {
  54001. enumerable: true,
  54002. value: readyStates.indexOf('CLOSING')
  54003. });
  54004. /**
  54005. * @constant {Number} CLOSED
  54006. * @memberof WebSocket
  54007. */
  54008. Object.defineProperty(WebSocket$1, 'CLOSED', {
  54009. enumerable: true,
  54010. value: readyStates.indexOf('CLOSED')
  54011. });
  54012. /**
  54013. * @constant {Number} CLOSED
  54014. * @memberof WebSocket.prototype
  54015. */
  54016. Object.defineProperty(WebSocket$1.prototype, 'CLOSED', {
  54017. enumerable: true,
  54018. value: readyStates.indexOf('CLOSED')
  54019. });
  54020. [
  54021. 'binaryType',
  54022. 'bufferedAmount',
  54023. 'extensions',
  54024. 'isPaused',
  54025. 'protocol',
  54026. 'readyState',
  54027. 'url'
  54028. ].forEach((property) => {
  54029. Object.defineProperty(WebSocket$1.prototype, property, { enumerable: true });
  54030. });
  54031. //
  54032. // Add the `onopen`, `onerror`, `onclose`, and `onmessage` attributes.
  54033. // See https://html.spec.whatwg.org/multipage/comms.html#the-websocket-interface
  54034. //
  54035. ['open', 'error', 'close', 'message'].forEach((method) => {
  54036. Object.defineProperty(WebSocket$1.prototype, `on${method}`, {
  54037. enumerable: true,
  54038. get() {
  54039. for (const listener of this.listeners(method)) {
  54040. if (listener[kForOnEventAttribute]) return listener[kListener];
  54041. }
  54042. return null;
  54043. },
  54044. set(handler) {
  54045. for (const listener of this.listeners(method)) {
  54046. if (listener[kForOnEventAttribute]) {
  54047. this.removeListener(method, listener);
  54048. break;
  54049. }
  54050. }
  54051. if (typeof handler !== 'function') return;
  54052. this.addEventListener(method, handler, {
  54053. [kForOnEventAttribute]: true
  54054. });
  54055. }
  54056. });
  54057. });
  54058. WebSocket$1.prototype.addEventListener = addEventListener;
  54059. WebSocket$1.prototype.removeEventListener = removeEventListener;
  54060. var websocket = WebSocket$1;
  54061. /**
  54062. * Initialize a WebSocket client.
  54063. *
  54064. * @param {WebSocket} websocket The client to initialize
  54065. * @param {(String|URL)} address The URL to which to connect
  54066. * @param {Array} protocols The subprotocols
  54067. * @param {Object} [options] Connection options
  54068. * @param {Boolean} [options.allowSynchronousEvents=true] Specifies whether any
  54069. * of the `'message'`, `'ping'`, and `'pong'` events can be emitted multiple
  54070. * times in the same tick
  54071. * @param {Boolean} [options.autoPong=true] Specifies whether or not to
  54072. * automatically send a pong in response to a ping
  54073. * @param {Function} [options.finishRequest] A function which can be used to
  54074. * customize the headers of each http request before it is sent
  54075. * @param {Boolean} [options.followRedirects=false] Whether or not to follow
  54076. * redirects
  54077. * @param {Function} [options.generateMask] The function used to generate the
  54078. * masking key
  54079. * @param {Number} [options.handshakeTimeout] Timeout in milliseconds for the
  54080. * handshake request
  54081. * @param {Number} [options.maxPayload=104857600] The maximum allowed message
  54082. * size
  54083. * @param {Number} [options.maxRedirects=10] The maximum number of redirects
  54084. * allowed
  54085. * @param {String} [options.origin] Value of the `Origin` or
  54086. * `Sec-WebSocket-Origin` header
  54087. * @param {(Boolean|Object)} [options.perMessageDeflate=true] Enable/disable
  54088. * permessage-deflate
  54089. * @param {Number} [options.protocolVersion=13] Value of the
  54090. * `Sec-WebSocket-Version` header
  54091. * @param {Boolean} [options.skipUTF8Validation=false] Specifies whether or
  54092. * not to skip UTF-8 validation for text and close messages
  54093. * @private
  54094. */
  54095. function initAsClient(websocket, address, protocols, options) {
  54096. const opts = {
  54097. allowSynchronousEvents: true,
  54098. autoPong: true,
  54099. protocolVersion: protocolVersions[1],
  54100. maxPayload: 100 * 1024 * 1024,
  54101. skipUTF8Validation: false,
  54102. perMessageDeflate: true,
  54103. followRedirects: false,
  54104. maxRedirects: 10,
  54105. ...options,
  54106. socketPath: undefined,
  54107. hostname: undefined,
  54108. protocol: undefined,
  54109. timeout: undefined,
  54110. method: 'GET',
  54111. host: undefined,
  54112. path: undefined,
  54113. port: undefined
  54114. };
  54115. websocket._autoPong = opts.autoPong;
  54116. if (!protocolVersions.includes(opts.protocolVersion)) {
  54117. throw new RangeError(
  54118. `Unsupported protocol version: ${opts.protocolVersion} ` +
  54119. `(supported versions: ${protocolVersions.join(', ')})`
  54120. );
  54121. }
  54122. let parsedUrl;
  54123. if (address instanceof URL$2) {
  54124. parsedUrl = address;
  54125. } else {
  54126. try {
  54127. parsedUrl = new URL$2(address);
  54128. } catch (e) {
  54129. throw new SyntaxError(`Invalid URL: ${address}`);
  54130. }
  54131. }
  54132. if (parsedUrl.protocol === 'http:') {
  54133. parsedUrl.protocol = 'ws:';
  54134. } else if (parsedUrl.protocol === 'https:') {
  54135. parsedUrl.protocol = 'wss:';
  54136. }
  54137. websocket._url = parsedUrl.href;
  54138. const isSecure = parsedUrl.protocol === 'wss:';
  54139. const isIpcUrl = parsedUrl.protocol === 'ws+unix:';
  54140. let invalidUrlMessage;
  54141. if (parsedUrl.protocol !== 'ws:' && !isSecure && !isIpcUrl) {
  54142. invalidUrlMessage =
  54143. 'The URL\'s protocol must be one of "ws:", "wss:", ' +
  54144. '"http:", "https", or "ws+unix:"';
  54145. } else if (isIpcUrl && !parsedUrl.pathname) {
  54146. invalidUrlMessage = "The URL's pathname is empty";
  54147. } else if (parsedUrl.hash) {
  54148. invalidUrlMessage = 'The URL contains a fragment identifier';
  54149. }
  54150. if (invalidUrlMessage) {
  54151. const err = new SyntaxError(invalidUrlMessage);
  54152. if (websocket._redirects === 0) {
  54153. throw err;
  54154. } else {
  54155. emitErrorAndClose(websocket, err);
  54156. return;
  54157. }
  54158. }
  54159. const defaultPort = isSecure ? 443 : 80;
  54160. const key = randomBytes(16).toString('base64');
  54161. const request = isSecure ? https$2.request : http$3.request;
  54162. const protocolSet = new Set();
  54163. let perMessageDeflate;
  54164. opts.createConnection =
  54165. opts.createConnection || (isSecure ? tlsConnect : netConnect);
  54166. opts.defaultPort = opts.defaultPort || defaultPort;
  54167. opts.port = parsedUrl.port || defaultPort;
  54168. opts.host = parsedUrl.hostname.startsWith('[')
  54169. ? parsedUrl.hostname.slice(1, -1)
  54170. : parsedUrl.hostname;
  54171. opts.headers = {
  54172. ...opts.headers,
  54173. 'Sec-WebSocket-Version': opts.protocolVersion,
  54174. 'Sec-WebSocket-Key': key,
  54175. Connection: 'Upgrade',
  54176. Upgrade: 'websocket'
  54177. };
  54178. opts.path = parsedUrl.pathname + parsedUrl.search;
  54179. opts.timeout = opts.handshakeTimeout;
  54180. if (opts.perMessageDeflate) {
  54181. perMessageDeflate = new PerMessageDeflate$1(
  54182. opts.perMessageDeflate !== true ? opts.perMessageDeflate : {},
  54183. false,
  54184. opts.maxPayload
  54185. );
  54186. opts.headers['Sec-WebSocket-Extensions'] = format({
  54187. [PerMessageDeflate$1.extensionName]: perMessageDeflate.offer()
  54188. });
  54189. }
  54190. if (protocols.length) {
  54191. for (const protocol of protocols) {
  54192. if (
  54193. typeof protocol !== 'string' ||
  54194. !subprotocolRegex.test(protocol) ||
  54195. protocolSet.has(protocol)
  54196. ) {
  54197. throw new SyntaxError(
  54198. 'An invalid or duplicated subprotocol was specified'
  54199. );
  54200. }
  54201. protocolSet.add(protocol);
  54202. }
  54203. opts.headers['Sec-WebSocket-Protocol'] = protocols.join(',');
  54204. }
  54205. if (opts.origin) {
  54206. if (opts.protocolVersion < 13) {
  54207. opts.headers['Sec-WebSocket-Origin'] = opts.origin;
  54208. } else {
  54209. opts.headers.Origin = opts.origin;
  54210. }
  54211. }
  54212. if (parsedUrl.username || parsedUrl.password) {
  54213. opts.auth = `${parsedUrl.username}:${parsedUrl.password}`;
  54214. }
  54215. if (isIpcUrl) {
  54216. const parts = opts.path.split(':');
  54217. opts.socketPath = parts[0];
  54218. opts.path = parts[1];
  54219. }
  54220. let req;
  54221. if (opts.followRedirects) {
  54222. if (websocket._redirects === 0) {
  54223. websocket._originalIpc = isIpcUrl;
  54224. websocket._originalSecure = isSecure;
  54225. websocket._originalHostOrSocketPath = isIpcUrl
  54226. ? opts.socketPath
  54227. : parsedUrl.host;
  54228. const headers = options && options.headers;
  54229. //
  54230. // Shallow copy the user provided options so that headers can be changed
  54231. // without mutating the original object.
  54232. //
  54233. options = { ...options, headers: {} };
  54234. if (headers) {
  54235. for (const [key, value] of Object.entries(headers)) {
  54236. options.headers[key.toLowerCase()] = value;
  54237. }
  54238. }
  54239. } else if (websocket.listenerCount('redirect') === 0) {
  54240. const isSameHost = isIpcUrl
  54241. ? websocket._originalIpc
  54242. ? opts.socketPath === websocket._originalHostOrSocketPath
  54243. : false
  54244. : websocket._originalIpc
  54245. ? false
  54246. : parsedUrl.host === websocket._originalHostOrSocketPath;
  54247. if (!isSameHost || (websocket._originalSecure && !isSecure)) {
  54248. //
  54249. // Match curl 7.77.0 behavior and drop the following headers. These
  54250. // headers are also dropped when following a redirect to a subdomain.
  54251. //
  54252. delete opts.headers.authorization;
  54253. delete opts.headers.cookie;
  54254. if (!isSameHost) delete opts.headers.host;
  54255. opts.auth = undefined;
  54256. }
  54257. }
  54258. //
  54259. // Match curl 7.77.0 behavior and make the first `Authorization` header win.
  54260. // If the `Authorization` header is set, then there is nothing to do as it
  54261. // will take precedence.
  54262. //
  54263. if (opts.auth && !options.headers.authorization) {
  54264. options.headers.authorization =
  54265. 'Basic ' + Buffer.from(opts.auth).toString('base64');
  54266. }
  54267. req = websocket._req = request(opts);
  54268. if (websocket._redirects) {
  54269. //
  54270. // Unlike what is done for the `'upgrade'` event, no early exit is
  54271. // triggered here if the user calls `websocket.close()` or
  54272. // `websocket.terminate()` from a listener of the `'redirect'` event. This
  54273. // is because the user can also call `request.destroy()` with an error
  54274. // before calling `websocket.close()` or `websocket.terminate()` and this
  54275. // would result in an error being emitted on the `request` object with no
  54276. // `'error'` event listeners attached.
  54277. //
  54278. websocket.emit('redirect', websocket.url, req);
  54279. }
  54280. } else {
  54281. req = websocket._req = request(opts);
  54282. }
  54283. if (opts.timeout) {
  54284. req.on('timeout', () => {
  54285. abortHandshake$1(websocket, req, 'Opening handshake has timed out');
  54286. });
  54287. }
  54288. req.on('error', (err) => {
  54289. if (req === null || req[kAborted]) return;
  54290. req = websocket._req = null;
  54291. emitErrorAndClose(websocket, err);
  54292. });
  54293. req.on('response', (res) => {
  54294. const location = res.headers.location;
  54295. const statusCode = res.statusCode;
  54296. if (
  54297. location &&
  54298. opts.followRedirects &&
  54299. statusCode >= 300 &&
  54300. statusCode < 400
  54301. ) {
  54302. if (++websocket._redirects > opts.maxRedirects) {
  54303. abortHandshake$1(websocket, req, 'Maximum redirects exceeded');
  54304. return;
  54305. }
  54306. req.abort();
  54307. let addr;
  54308. try {
  54309. addr = new URL$2(location, address);
  54310. } catch (e) {
  54311. const err = new SyntaxError(`Invalid URL: ${location}`);
  54312. emitErrorAndClose(websocket, err);
  54313. return;
  54314. }
  54315. initAsClient(websocket, addr, protocols, options);
  54316. } else if (!websocket.emit('unexpected-response', req, res)) {
  54317. abortHandshake$1(
  54318. websocket,
  54319. req,
  54320. `Unexpected server response: ${res.statusCode}`
  54321. );
  54322. }
  54323. });
  54324. req.on('upgrade', (res, socket, head) => {
  54325. websocket.emit('upgrade', res);
  54326. //
  54327. // The user may have closed the connection from a listener of the
  54328. // `'upgrade'` event.
  54329. //
  54330. if (websocket.readyState !== WebSocket$1.CONNECTING) return;
  54331. req = websocket._req = null;
  54332. const upgrade = res.headers.upgrade;
  54333. if (upgrade === undefined || upgrade.toLowerCase() !== 'websocket') {
  54334. abortHandshake$1(websocket, socket, 'Invalid Upgrade header');
  54335. return;
  54336. }
  54337. const digest = createHash$1('sha1')
  54338. .update(key + GUID$1)
  54339. .digest('base64');
  54340. if (res.headers['sec-websocket-accept'] !== digest) {
  54341. abortHandshake$1(websocket, socket, 'Invalid Sec-WebSocket-Accept header');
  54342. return;
  54343. }
  54344. const serverProt = res.headers['sec-websocket-protocol'];
  54345. let protError;
  54346. if (serverProt !== undefined) {
  54347. if (!protocolSet.size) {
  54348. protError = 'Server sent a subprotocol but none was requested';
  54349. } else if (!protocolSet.has(serverProt)) {
  54350. protError = 'Server sent an invalid subprotocol';
  54351. }
  54352. } else if (protocolSet.size) {
  54353. protError = 'Server sent no subprotocol';
  54354. }
  54355. if (protError) {
  54356. abortHandshake$1(websocket, socket, protError);
  54357. return;
  54358. }
  54359. if (serverProt) websocket._protocol = serverProt;
  54360. const secWebSocketExtensions = res.headers['sec-websocket-extensions'];
  54361. if (secWebSocketExtensions !== undefined) {
  54362. if (!perMessageDeflate) {
  54363. const message =
  54364. 'Server sent a Sec-WebSocket-Extensions header but no extension ' +
  54365. 'was requested';
  54366. abortHandshake$1(websocket, socket, message);
  54367. return;
  54368. }
  54369. let extensions;
  54370. try {
  54371. extensions = parse$1(secWebSocketExtensions);
  54372. } catch (err) {
  54373. const message = 'Invalid Sec-WebSocket-Extensions header';
  54374. abortHandshake$1(websocket, socket, message);
  54375. return;
  54376. }
  54377. const extensionNames = Object.keys(extensions);
  54378. if (
  54379. extensionNames.length !== 1 ||
  54380. extensionNames[0] !== PerMessageDeflate$1.extensionName
  54381. ) {
  54382. const message = 'Server indicated an extension that was not requested';
  54383. abortHandshake$1(websocket, socket, message);
  54384. return;
  54385. }
  54386. try {
  54387. perMessageDeflate.accept(extensions[PerMessageDeflate$1.extensionName]);
  54388. } catch (err) {
  54389. const message = 'Invalid Sec-WebSocket-Extensions header';
  54390. abortHandshake$1(websocket, socket, message);
  54391. return;
  54392. }
  54393. websocket._extensions[PerMessageDeflate$1.extensionName] =
  54394. perMessageDeflate;
  54395. }
  54396. websocket.setSocket(socket, head, {
  54397. allowSynchronousEvents: opts.allowSynchronousEvents,
  54398. generateMask: opts.generateMask,
  54399. maxPayload: opts.maxPayload,
  54400. skipUTF8Validation: opts.skipUTF8Validation
  54401. });
  54402. });
  54403. if (opts.finishRequest) {
  54404. opts.finishRequest(req, websocket);
  54405. } else {
  54406. req.end();
  54407. }
  54408. }
  54409. /**
  54410. * Emit the `'error'` and `'close'` events.
  54411. *
  54412. * @param {WebSocket} websocket The WebSocket instance
  54413. * @param {Error} The error to emit
  54414. * @private
  54415. */
  54416. function emitErrorAndClose(websocket, err) {
  54417. websocket._readyState = WebSocket$1.CLOSING;
  54418. //
  54419. // The following assignment is practically useless and is done only for
  54420. // consistency.
  54421. //
  54422. websocket._errorEmitted = true;
  54423. websocket.emit('error', err);
  54424. websocket.emitClose();
  54425. }
  54426. /**
  54427. * Create a `net.Socket` and initiate a connection.
  54428. *
  54429. * @param {Object} options Connection options
  54430. * @return {net.Socket} The newly created socket used to start the connection
  54431. * @private
  54432. */
  54433. function netConnect(options) {
  54434. options.path = options.socketPath;
  54435. return net.connect(options);
  54436. }
  54437. /**
  54438. * Create a `tls.TLSSocket` and initiate a connection.
  54439. *
  54440. * @param {Object} options Connection options
  54441. * @return {tls.TLSSocket} The newly created socket used to start the connection
  54442. * @private
  54443. */
  54444. function tlsConnect(options) {
  54445. options.path = undefined;
  54446. if (!options.servername && options.servername !== '') {
  54447. options.servername = net.isIP(options.host) ? '' : options.host;
  54448. }
  54449. return tls.connect(options);
  54450. }
  54451. /**
  54452. * Abort the handshake and emit an error.
  54453. *
  54454. * @param {WebSocket} websocket The WebSocket instance
  54455. * @param {(http.ClientRequest|net.Socket|tls.Socket)} stream The request to
  54456. * abort or the socket to destroy
  54457. * @param {String} message The error message
  54458. * @private
  54459. */
  54460. function abortHandshake$1(websocket, stream, message) {
  54461. websocket._readyState = WebSocket$1.CLOSING;
  54462. const err = new Error(message);
  54463. Error.captureStackTrace(err, abortHandshake$1);
  54464. if (stream.setHeader) {
  54465. stream[kAborted] = true;
  54466. stream.abort();
  54467. if (stream.socket && !stream.socket.destroyed) {
  54468. //
  54469. // On Node.js >= 14.3.0 `request.abort()` does not destroy the socket if
  54470. // called after the request completed. See
  54471. // https://github.com/websockets/ws/issues/1869.
  54472. //
  54473. stream.socket.destroy();
  54474. }
  54475. process.nextTick(emitErrorAndClose, websocket, err);
  54476. } else {
  54477. stream.destroy(err);
  54478. stream.once('error', websocket.emit.bind(websocket, 'error'));
  54479. stream.once('close', websocket.emitClose.bind(websocket));
  54480. }
  54481. }
  54482. /**
  54483. * Handle cases where the `ping()`, `pong()`, or `send()` methods are called
  54484. * when the `readyState` attribute is `CLOSING` or `CLOSED`.
  54485. *
  54486. * @param {WebSocket} websocket The WebSocket instance
  54487. * @param {*} [data] The data to send
  54488. * @param {Function} [cb] Callback
  54489. * @private
  54490. */
  54491. function sendAfterClose(websocket, data, cb) {
  54492. if (data) {
  54493. const length = isBlob(data) ? data.size : toBuffer(data).length;
  54494. //
  54495. // The `_bufferedAmount` property is used only when the peer is a client and
  54496. // the opening handshake fails. Under these circumstances, in fact, the
  54497. // `setSocket()` method is not called, so the `_socket` and `_sender`
  54498. // properties are set to `null`.
  54499. //
  54500. if (websocket._socket) websocket._sender._bufferedBytes += length;
  54501. else websocket._bufferedAmount += length;
  54502. }
  54503. if (cb) {
  54504. const err = new Error(
  54505. `WebSocket is not open: readyState ${websocket.readyState} ` +
  54506. `(${readyStates[websocket.readyState]})`
  54507. );
  54508. process.nextTick(cb, err);
  54509. }
  54510. }
  54511. /**
  54512. * The listener of the `Receiver` `'conclude'` event.
  54513. *
  54514. * @param {Number} code The status code
  54515. * @param {Buffer} reason The reason for closing
  54516. * @private
  54517. */
  54518. function receiverOnConclude(code, reason) {
  54519. const websocket = this[kWebSocket$1];
  54520. websocket._closeFrameReceived = true;
  54521. websocket._closeMessage = reason;
  54522. websocket._closeCode = code;
  54523. if (websocket._socket[kWebSocket$1] === undefined) return;
  54524. websocket._socket.removeListener('data', socketOnData);
  54525. process.nextTick(resume, websocket._socket);
  54526. if (code === 1005) websocket.close();
  54527. else websocket.close(code, reason);
  54528. }
  54529. /**
  54530. * The listener of the `Receiver` `'drain'` event.
  54531. *
  54532. * @private
  54533. */
  54534. function receiverOnDrain() {
  54535. const websocket = this[kWebSocket$1];
  54536. if (!websocket.isPaused) websocket._socket.resume();
  54537. }
  54538. /**
  54539. * The listener of the `Receiver` `'error'` event.
  54540. *
  54541. * @param {(RangeError|Error)} err The emitted error
  54542. * @private
  54543. */
  54544. function receiverOnError(err) {
  54545. const websocket = this[kWebSocket$1];
  54546. if (websocket._socket[kWebSocket$1] !== undefined) {
  54547. websocket._socket.removeListener('data', socketOnData);
  54548. //
  54549. // On Node.js < 14.0.0 the `'error'` event is emitted synchronously. See
  54550. // https://github.com/websockets/ws/issues/1940.
  54551. //
  54552. process.nextTick(resume, websocket._socket);
  54553. websocket.close(err[kStatusCode]);
  54554. }
  54555. if (!websocket._errorEmitted) {
  54556. websocket._errorEmitted = true;
  54557. websocket.emit('error', err);
  54558. }
  54559. }
  54560. /**
  54561. * The listener of the `Receiver` `'finish'` event.
  54562. *
  54563. * @private
  54564. */
  54565. function receiverOnFinish() {
  54566. this[kWebSocket$1].emitClose();
  54567. }
  54568. /**
  54569. * The listener of the `Receiver` `'message'` event.
  54570. *
  54571. * @param {Buffer|ArrayBuffer|Buffer[])} data The message
  54572. * @param {Boolean} isBinary Specifies whether the message is binary or not
  54573. * @private
  54574. */
  54575. function receiverOnMessage(data, isBinary) {
  54576. this[kWebSocket$1].emit('message', data, isBinary);
  54577. }
  54578. /**
  54579. * The listener of the `Receiver` `'ping'` event.
  54580. *
  54581. * @param {Buffer} data The data included in the ping frame
  54582. * @private
  54583. */
  54584. function receiverOnPing(data) {
  54585. const websocket = this[kWebSocket$1];
  54586. if (websocket._autoPong) websocket.pong(data, !this._isServer, NOOP$1);
  54587. websocket.emit('ping', data);
  54588. }
  54589. /**
  54590. * The listener of the `Receiver` `'pong'` event.
  54591. *
  54592. * @param {Buffer} data The data included in the pong frame
  54593. * @private
  54594. */
  54595. function receiverOnPong(data) {
  54596. this[kWebSocket$1].emit('pong', data);
  54597. }
  54598. /**
  54599. * Resume a readable stream
  54600. *
  54601. * @param {Readable} stream The readable stream
  54602. * @private
  54603. */
  54604. function resume(stream) {
  54605. stream.resume();
  54606. }
  54607. /**
  54608. * The `Sender` error event handler.
  54609. *
  54610. * @param {Error} The error
  54611. * @private
  54612. */
  54613. function senderOnError(err) {
  54614. const websocket = this[kWebSocket$1];
  54615. if (websocket.readyState === WebSocket$1.CLOSED) return;
  54616. if (websocket.readyState === WebSocket$1.OPEN) {
  54617. websocket._readyState = WebSocket$1.CLOSING;
  54618. setCloseTimer(websocket);
  54619. }
  54620. //
  54621. // `socket.end()` is used instead of `socket.destroy()` to allow the other
  54622. // peer to finish sending queued data. There is no need to set a timer here
  54623. // because `CLOSING` means that it is already set or not needed.
  54624. //
  54625. this._socket.end();
  54626. if (!websocket._errorEmitted) {
  54627. websocket._errorEmitted = true;
  54628. websocket.emit('error', err);
  54629. }
  54630. }
  54631. /**
  54632. * Set a timer to destroy the underlying raw socket of a WebSocket.
  54633. *
  54634. * @param {WebSocket} websocket The WebSocket instance
  54635. * @private
  54636. */
  54637. function setCloseTimer(websocket) {
  54638. websocket._closeTimer = setTimeout(
  54639. websocket._socket.destroy.bind(websocket._socket),
  54640. closeTimeout
  54641. );
  54642. }
  54643. /**
  54644. * The listener of the socket `'close'` event.
  54645. *
  54646. * @private
  54647. */
  54648. function socketOnClose() {
  54649. const websocket = this[kWebSocket$1];
  54650. this.removeListener('close', socketOnClose);
  54651. this.removeListener('data', socketOnData);
  54652. this.removeListener('end', socketOnEnd);
  54653. websocket._readyState = WebSocket$1.CLOSING;
  54654. let chunk;
  54655. //
  54656. // The close frame might not have been received or the `'end'` event emitted,
  54657. // for example, if the socket was destroyed due to an error. Ensure that the
  54658. // `receiver` stream is closed after writing any remaining buffered data to
  54659. // it. If the readable side of the socket is in flowing mode then there is no
  54660. // buffered data as everything has been already written and `readable.read()`
  54661. // will return `null`. If instead, the socket is paused, any possible buffered
  54662. // data will be read as a single chunk.
  54663. //
  54664. if (
  54665. !this._readableState.endEmitted &&
  54666. !websocket._closeFrameReceived &&
  54667. !websocket._receiver._writableState.errorEmitted &&
  54668. (chunk = websocket._socket.read()) !== null
  54669. ) {
  54670. websocket._receiver.write(chunk);
  54671. }
  54672. websocket._receiver.end();
  54673. this[kWebSocket$1] = undefined;
  54674. clearTimeout(websocket._closeTimer);
  54675. if (
  54676. websocket._receiver._writableState.finished ||
  54677. websocket._receiver._writableState.errorEmitted
  54678. ) {
  54679. websocket.emitClose();
  54680. } else {
  54681. websocket._receiver.on('error', receiverOnFinish);
  54682. websocket._receiver.on('finish', receiverOnFinish);
  54683. }
  54684. }
  54685. /**
  54686. * The listener of the socket `'data'` event.
  54687. *
  54688. * @param {Buffer} chunk A chunk of data
  54689. * @private
  54690. */
  54691. function socketOnData(chunk) {
  54692. if (!this[kWebSocket$1]._receiver.write(chunk)) {
  54693. this.pause();
  54694. }
  54695. }
  54696. /**
  54697. * The listener of the socket `'end'` event.
  54698. *
  54699. * @private
  54700. */
  54701. function socketOnEnd() {
  54702. const websocket = this[kWebSocket$1];
  54703. websocket._readyState = WebSocket$1.CLOSING;
  54704. websocket._receiver.end();
  54705. this.end();
  54706. }
  54707. /**
  54708. * The listener of the socket `'error'` event.
  54709. *
  54710. * @private
  54711. */
  54712. function socketOnError$1() {
  54713. const websocket = this[kWebSocket$1];
  54714. this.removeListener('error', socketOnError$1);
  54715. this.on('error', NOOP$1);
  54716. if (websocket) {
  54717. websocket._readyState = WebSocket$1.CLOSING;
  54718. this.destroy();
  54719. }
  54720. }
  54721. const { tokenChars } = validationExports;
  54722. /**
  54723. * Parses the `Sec-WebSocket-Protocol` header into a set of subprotocol names.
  54724. *
  54725. * @param {String} header The field value of the header
  54726. * @return {Set} The subprotocol names
  54727. * @public
  54728. */
  54729. function parse(header) {
  54730. const protocols = new Set();
  54731. let start = -1;
  54732. let end = -1;
  54733. let i = 0;
  54734. for (i; i < header.length; i++) {
  54735. const code = header.charCodeAt(i);
  54736. if (end === -1 && tokenChars[code] === 1) {
  54737. if (start === -1) start = i;
  54738. } else if (
  54739. i !== 0 &&
  54740. (code === 0x20 /* ' ' */ || code === 0x09) /* '\t' */
  54741. ) {
  54742. if (end === -1 && start !== -1) end = i;
  54743. } else if (code === 0x2c /* ',' */) {
  54744. if (start === -1) {
  54745. throw new SyntaxError(`Unexpected character at index ${i}`);
  54746. }
  54747. if (end === -1) end = i;
  54748. const protocol = header.slice(start, end);
  54749. if (protocols.has(protocol)) {
  54750. throw new SyntaxError(`The "${protocol}" subprotocol is duplicated`);
  54751. }
  54752. protocols.add(protocol);
  54753. start = end = -1;
  54754. } else {
  54755. throw new SyntaxError(`Unexpected character at index ${i}`);
  54756. }
  54757. }
  54758. if (start === -1 || end !== -1) {
  54759. throw new SyntaxError('Unexpected end of input');
  54760. }
  54761. const protocol = header.slice(start, i);
  54762. if (protocols.has(protocol)) {
  54763. throw new SyntaxError(`The "${protocol}" subprotocol is duplicated`);
  54764. }
  54765. protocols.add(protocol);
  54766. return protocols;
  54767. }
  54768. var subprotocol$1 = { parse };
  54769. /* eslint no-unused-vars: ["error", { "varsIgnorePattern": "^Duplex$", "caughtErrors": "none" }] */
  54770. const EventEmitter = require$$0$7;
  54771. const http$2 = require$$1;
  54772. const { createHash } = require$$3$1;
  54773. const extension = extension$1;
  54774. const PerMessageDeflate = permessageDeflate;
  54775. const subprotocol = subprotocol$1;
  54776. const WebSocket = websocket;
  54777. const { GUID, kWebSocket } = constants;
  54778. const keyRegex = /^[+/0-9A-Za-z]{22}==$/;
  54779. const RUNNING = 0;
  54780. const CLOSING = 1;
  54781. const CLOSED = 2;
  54782. /**
  54783. * Class representing a WebSocket server.
  54784. *
  54785. * @extends EventEmitter
  54786. */
  54787. class WebSocketServer extends EventEmitter {
  54788. /**
  54789. * Create a `WebSocketServer` instance.
  54790. *
  54791. * @param {Object} options Configuration options
  54792. * @param {Boolean} [options.allowSynchronousEvents=true] Specifies whether
  54793. * any of the `'message'`, `'ping'`, and `'pong'` events can be emitted
  54794. * multiple times in the same tick
  54795. * @param {Boolean} [options.autoPong=true] Specifies whether or not to
  54796. * automatically send a pong in response to a ping
  54797. * @param {Number} [options.backlog=511] The maximum length of the queue of
  54798. * pending connections
  54799. * @param {Boolean} [options.clientTracking=true] Specifies whether or not to
  54800. * track clients
  54801. * @param {Function} [options.handleProtocols] A hook to handle protocols
  54802. * @param {String} [options.host] The hostname where to bind the server
  54803. * @param {Number} [options.maxPayload=104857600] The maximum allowed message
  54804. * size
  54805. * @param {Boolean} [options.noServer=false] Enable no server mode
  54806. * @param {String} [options.path] Accept only connections matching this path
  54807. * @param {(Boolean|Object)} [options.perMessageDeflate=false] Enable/disable
  54808. * permessage-deflate
  54809. * @param {Number} [options.port] The port where to bind the server
  54810. * @param {(http.Server|https.Server)} [options.server] A pre-created HTTP/S
  54811. * server to use
  54812. * @param {Boolean} [options.skipUTF8Validation=false] Specifies whether or
  54813. * not to skip UTF-8 validation for text and close messages
  54814. * @param {Function} [options.verifyClient] A hook to reject connections
  54815. * @param {Function} [options.WebSocket=WebSocket] Specifies the `WebSocket`
  54816. * class to use. It must be the `WebSocket` class or class that extends it
  54817. * @param {Function} [callback] A listener for the `listening` event
  54818. */
  54819. constructor(options, callback) {
  54820. super();
  54821. options = {
  54822. allowSynchronousEvents: true,
  54823. autoPong: true,
  54824. maxPayload: 100 * 1024 * 1024,
  54825. skipUTF8Validation: false,
  54826. perMessageDeflate: false,
  54827. handleProtocols: null,
  54828. clientTracking: true,
  54829. verifyClient: null,
  54830. noServer: false,
  54831. backlog: null, // use default (511 as implemented in net.js)
  54832. server: null,
  54833. host: null,
  54834. path: null,
  54835. port: null,
  54836. WebSocket,
  54837. ...options
  54838. };
  54839. if (
  54840. (options.port == null && !options.server && !options.noServer) ||
  54841. (options.port != null && (options.server || options.noServer)) ||
  54842. (options.server && options.noServer)
  54843. ) {
  54844. throw new TypeError(
  54845. 'One and only one of the "port", "server", or "noServer" options ' +
  54846. 'must be specified'
  54847. );
  54848. }
  54849. if (options.port != null) {
  54850. this._server = http$2.createServer((req, res) => {
  54851. const body = http$2.STATUS_CODES[426];
  54852. res.writeHead(426, {
  54853. 'Content-Length': body.length,
  54854. 'Content-Type': 'text/plain'
  54855. });
  54856. res.end(body);
  54857. });
  54858. this._server.listen(
  54859. options.port,
  54860. options.host,
  54861. options.backlog,
  54862. callback
  54863. );
  54864. } else if (options.server) {
  54865. this._server = options.server;
  54866. }
  54867. if (this._server) {
  54868. const emitConnection = this.emit.bind(this, 'connection');
  54869. this._removeListeners = addListeners(this._server, {
  54870. listening: this.emit.bind(this, 'listening'),
  54871. error: this.emit.bind(this, 'error'),
  54872. upgrade: (req, socket, head) => {
  54873. this.handleUpgrade(req, socket, head, emitConnection);
  54874. }
  54875. });
  54876. }
  54877. if (options.perMessageDeflate === true) options.perMessageDeflate = {};
  54878. if (options.clientTracking) {
  54879. this.clients = new Set();
  54880. this._shouldEmitClose = false;
  54881. }
  54882. this.options = options;
  54883. this._state = RUNNING;
  54884. }
  54885. /**
  54886. * Returns the bound address, the address family name, and port of the server
  54887. * as reported by the operating system if listening on an IP socket.
  54888. * If the server is listening on a pipe or UNIX domain socket, the name is
  54889. * returned as a string.
  54890. *
  54891. * @return {(Object|String|null)} The address of the server
  54892. * @public
  54893. */
  54894. address() {
  54895. if (this.options.noServer) {
  54896. throw new Error('The server is operating in "noServer" mode');
  54897. }
  54898. if (!this._server) return null;
  54899. return this._server.address();
  54900. }
  54901. /**
  54902. * Stop the server from accepting new connections and emit the `'close'` event
  54903. * when all existing connections are closed.
  54904. *
  54905. * @param {Function} [cb] A one-time listener for the `'close'` event
  54906. * @public
  54907. */
  54908. close(cb) {
  54909. if (this._state === CLOSED) {
  54910. if (cb) {
  54911. this.once('close', () => {
  54912. cb(new Error('The server is not running'));
  54913. });
  54914. }
  54915. process.nextTick(emitClose, this);
  54916. return;
  54917. }
  54918. if (cb) this.once('close', cb);
  54919. if (this._state === CLOSING) return;
  54920. this._state = CLOSING;
  54921. if (this.options.noServer || this.options.server) {
  54922. if (this._server) {
  54923. this._removeListeners();
  54924. this._removeListeners = this._server = null;
  54925. }
  54926. if (this.clients) {
  54927. if (!this.clients.size) {
  54928. process.nextTick(emitClose, this);
  54929. } else {
  54930. this._shouldEmitClose = true;
  54931. }
  54932. } else {
  54933. process.nextTick(emitClose, this);
  54934. }
  54935. } else {
  54936. const server = this._server;
  54937. this._removeListeners();
  54938. this._removeListeners = this._server = null;
  54939. //
  54940. // The HTTP/S server was created internally. Close it, and rely on its
  54941. // `'close'` event.
  54942. //
  54943. server.close(() => {
  54944. emitClose(this);
  54945. });
  54946. }
  54947. }
  54948. /**
  54949. * See if a given request should be handled by this server instance.
  54950. *
  54951. * @param {http.IncomingMessage} req Request object to inspect
  54952. * @return {Boolean} `true` if the request is valid, else `false`
  54953. * @public
  54954. */
  54955. shouldHandle(req) {
  54956. if (this.options.path) {
  54957. const index = req.url.indexOf('?');
  54958. const pathname = index !== -1 ? req.url.slice(0, index) : req.url;
  54959. if (pathname !== this.options.path) return false;
  54960. }
  54961. return true;
  54962. }
  54963. /**
  54964. * Handle a HTTP Upgrade request.
  54965. *
  54966. * @param {http.IncomingMessage} req The request object
  54967. * @param {Duplex} socket The network socket between the server and client
  54968. * @param {Buffer} head The first packet of the upgraded stream
  54969. * @param {Function} cb Callback
  54970. * @public
  54971. */
  54972. handleUpgrade(req, socket, head, cb) {
  54973. socket.on('error', socketOnError);
  54974. const key = req.headers['sec-websocket-key'];
  54975. const upgrade = req.headers.upgrade;
  54976. const version = +req.headers['sec-websocket-version'];
  54977. if (req.method !== 'GET') {
  54978. const message = 'Invalid HTTP method';
  54979. abortHandshakeOrEmitwsClientError(this, req, socket, 405, message);
  54980. return;
  54981. }
  54982. if (upgrade === undefined || upgrade.toLowerCase() !== 'websocket') {
  54983. const message = 'Invalid Upgrade header';
  54984. abortHandshakeOrEmitwsClientError(this, req, socket, 400, message);
  54985. return;
  54986. }
  54987. if (key === undefined || !keyRegex.test(key)) {
  54988. const message = 'Missing or invalid Sec-WebSocket-Key header';
  54989. abortHandshakeOrEmitwsClientError(this, req, socket, 400, message);
  54990. return;
  54991. }
  54992. if (version !== 8 && version !== 13) {
  54993. const message = 'Missing or invalid Sec-WebSocket-Version header';
  54994. abortHandshakeOrEmitwsClientError(this, req, socket, 400, message);
  54995. return;
  54996. }
  54997. if (!this.shouldHandle(req)) {
  54998. abortHandshake(socket, 400);
  54999. return;
  55000. }
  55001. const secWebSocketProtocol = req.headers['sec-websocket-protocol'];
  55002. let protocols = new Set();
  55003. if (secWebSocketProtocol !== undefined) {
  55004. try {
  55005. protocols = subprotocol.parse(secWebSocketProtocol);
  55006. } catch (err) {
  55007. const message = 'Invalid Sec-WebSocket-Protocol header';
  55008. abortHandshakeOrEmitwsClientError(this, req, socket, 400, message);
  55009. return;
  55010. }
  55011. }
  55012. const secWebSocketExtensions = req.headers['sec-websocket-extensions'];
  55013. const extensions = {};
  55014. if (
  55015. this.options.perMessageDeflate &&
  55016. secWebSocketExtensions !== undefined
  55017. ) {
  55018. const perMessageDeflate = new PerMessageDeflate(
  55019. this.options.perMessageDeflate,
  55020. true,
  55021. this.options.maxPayload
  55022. );
  55023. try {
  55024. const offers = extension.parse(secWebSocketExtensions);
  55025. if (offers[PerMessageDeflate.extensionName]) {
  55026. perMessageDeflate.accept(offers[PerMessageDeflate.extensionName]);
  55027. extensions[PerMessageDeflate.extensionName] = perMessageDeflate;
  55028. }
  55029. } catch (err) {
  55030. const message =
  55031. 'Invalid or unacceptable Sec-WebSocket-Extensions header';
  55032. abortHandshakeOrEmitwsClientError(this, req, socket, 400, message);
  55033. return;
  55034. }
  55035. }
  55036. //
  55037. // Optionally call external client verification handler.
  55038. //
  55039. if (this.options.verifyClient) {
  55040. const info = {
  55041. origin:
  55042. req.headers[`${version === 8 ? 'sec-websocket-origin' : 'origin'}`],
  55043. secure: !!(req.socket.authorized || req.socket.encrypted),
  55044. req
  55045. };
  55046. if (this.options.verifyClient.length === 2) {
  55047. this.options.verifyClient(info, (verified, code, message, headers) => {
  55048. if (!verified) {
  55049. return abortHandshake(socket, code || 401, message, headers);
  55050. }
  55051. this.completeUpgrade(
  55052. extensions,
  55053. key,
  55054. protocols,
  55055. req,
  55056. socket,
  55057. head,
  55058. cb
  55059. );
  55060. });
  55061. return;
  55062. }
  55063. if (!this.options.verifyClient(info)) return abortHandshake(socket, 401);
  55064. }
  55065. this.completeUpgrade(extensions, key, protocols, req, socket, head, cb);
  55066. }
  55067. /**
  55068. * Upgrade the connection to WebSocket.
  55069. *
  55070. * @param {Object} extensions The accepted extensions
  55071. * @param {String} key The value of the `Sec-WebSocket-Key` header
  55072. * @param {Set} protocols The subprotocols
  55073. * @param {http.IncomingMessage} req The request object
  55074. * @param {Duplex} socket The network socket between the server and client
  55075. * @param {Buffer} head The first packet of the upgraded stream
  55076. * @param {Function} cb Callback
  55077. * @throws {Error} If called more than once with the same socket
  55078. * @private
  55079. */
  55080. completeUpgrade(extensions, key, protocols, req, socket, head, cb) {
  55081. //
  55082. // Destroy the socket if the client has already sent a FIN packet.
  55083. //
  55084. if (!socket.readable || !socket.writable) return socket.destroy();
  55085. if (socket[kWebSocket]) {
  55086. throw new Error(
  55087. 'server.handleUpgrade() was called more than once with the same ' +
  55088. 'socket, possibly due to a misconfiguration'
  55089. );
  55090. }
  55091. if (this._state > RUNNING) return abortHandshake(socket, 503);
  55092. const digest = createHash('sha1')
  55093. .update(key + GUID)
  55094. .digest('base64');
  55095. const headers = [
  55096. 'HTTP/1.1 101 Switching Protocols',
  55097. 'Upgrade: websocket',
  55098. 'Connection: Upgrade',
  55099. `Sec-WebSocket-Accept: ${digest}`
  55100. ];
  55101. const ws = new this.options.WebSocket(null, undefined, this.options);
  55102. if (protocols.size) {
  55103. //
  55104. // Optionally call external protocol selection handler.
  55105. //
  55106. const protocol = this.options.handleProtocols
  55107. ? this.options.handleProtocols(protocols, req)
  55108. : protocols.values().next().value;
  55109. if (protocol) {
  55110. headers.push(`Sec-WebSocket-Protocol: ${protocol}`);
  55111. ws._protocol = protocol;
  55112. }
  55113. }
  55114. if (extensions[PerMessageDeflate.extensionName]) {
  55115. const params = extensions[PerMessageDeflate.extensionName].params;
  55116. const value = extension.format({
  55117. [PerMessageDeflate.extensionName]: [params]
  55118. });
  55119. headers.push(`Sec-WebSocket-Extensions: ${value}`);
  55120. ws._extensions = extensions;
  55121. }
  55122. //
  55123. // Allow external modification/inspection of handshake headers.
  55124. //
  55125. this.emit('headers', headers, req);
  55126. socket.write(headers.concat('\r\n').join('\r\n'));
  55127. socket.removeListener('error', socketOnError);
  55128. ws.setSocket(socket, head, {
  55129. allowSynchronousEvents: this.options.allowSynchronousEvents,
  55130. maxPayload: this.options.maxPayload,
  55131. skipUTF8Validation: this.options.skipUTF8Validation
  55132. });
  55133. if (this.clients) {
  55134. this.clients.add(ws);
  55135. ws.on('close', () => {
  55136. this.clients.delete(ws);
  55137. if (this._shouldEmitClose && !this.clients.size) {
  55138. process.nextTick(emitClose, this);
  55139. }
  55140. });
  55141. }
  55142. cb(ws, req);
  55143. }
  55144. }
  55145. var websocketServer = WebSocketServer;
  55146. /**
  55147. * Add event listeners on an `EventEmitter` using a map of <event, listener>
  55148. * pairs.
  55149. *
  55150. * @param {EventEmitter} server The event emitter
  55151. * @param {Object.<String, Function>} map The listeners to add
  55152. * @return {Function} A function that will remove the added listeners when
  55153. * called
  55154. * @private
  55155. */
  55156. function addListeners(server, map) {
  55157. for (const event of Object.keys(map)) server.on(event, map[event]);
  55158. return function removeListeners() {
  55159. for (const event of Object.keys(map)) {
  55160. server.removeListener(event, map[event]);
  55161. }
  55162. };
  55163. }
  55164. /**
  55165. * Emit a `'close'` event on an `EventEmitter`.
  55166. *
  55167. * @param {EventEmitter} server The event emitter
  55168. * @private
  55169. */
  55170. function emitClose(server) {
  55171. server._state = CLOSED;
  55172. server.emit('close');
  55173. }
  55174. /**
  55175. * Handle socket errors.
  55176. *
  55177. * @private
  55178. */
  55179. function socketOnError() {
  55180. this.destroy();
  55181. }
  55182. /**
  55183. * Close the connection when preconditions are not fulfilled.
  55184. *
  55185. * @param {Duplex} socket The socket of the upgrade request
  55186. * @param {Number} code The HTTP response status code
  55187. * @param {String} [message] The HTTP response body
  55188. * @param {Object} [headers] Additional HTTP response headers
  55189. * @private
  55190. */
  55191. function abortHandshake(socket, code, message, headers) {
  55192. //
  55193. // The socket is writable unless the user destroyed or ended it before calling
  55194. // `server.handleUpgrade()` or in the `verifyClient` function, which is a user
  55195. // error. Handling this does not make much sense as the worst that can happen
  55196. // is that some of the data written by the user might be discarded due to the
  55197. // call to `socket.end()` below, which triggers an `'error'` event that in
  55198. // turn causes the socket to be destroyed.
  55199. //
  55200. message = message || http$2.STATUS_CODES[code];
  55201. headers = {
  55202. Connection: 'close',
  55203. 'Content-Type': 'text/html',
  55204. 'Content-Length': Buffer.byteLength(message),
  55205. ...headers
  55206. };
  55207. socket.once('finish', socket.destroy);
  55208. socket.end(
  55209. `HTTP/1.1 ${code} ${http$2.STATUS_CODES[code]}\r\n` +
  55210. Object.keys(headers)
  55211. .map((h) => `${h}: ${headers[h]}`)
  55212. .join('\r\n') +
  55213. '\r\n\r\n' +
  55214. message
  55215. );
  55216. }
  55217. /**
  55218. * Emit a `'wsClientError'` event on a `WebSocketServer` if there is at least
  55219. * one listener for it, otherwise call `abortHandshake()`.
  55220. *
  55221. * @param {WebSocketServer} server The WebSocket server
  55222. * @param {http.IncomingMessage} req The request object
  55223. * @param {Duplex} socket The socket of the upgrade request
  55224. * @param {Number} code The HTTP response status code
  55225. * @param {String} message The HTTP response body
  55226. * @private
  55227. */
  55228. function abortHandshakeOrEmitwsClientError(server, req, socket, code, message) {
  55229. if (server.listenerCount('wsClientError')) {
  55230. const err = new Error(message);
  55231. Error.captureStackTrace(err, abortHandshakeOrEmitwsClientError);
  55232. server.emit('wsClientError', err, socket, req);
  55233. } else {
  55234. abortHandshake(socket, code, message);
  55235. }
  55236. }
  55237. var WebSocketServerRaw_ = /*@__PURE__*/getDefaultExportFromCjs(websocketServer);
  55238. const allowedHostsServerCache = /* @__PURE__ */ new WeakMap();
  55239. const allowedHostsPreviewCache = /* @__PURE__ */ new WeakMap();
  55240. const isFileOrExtensionProtocolRE = /^(?:file|.+-extension):/i;
  55241. function getAdditionalAllowedHosts(resolvedServerOptions, resolvedPreviewOptions) {
  55242. const list = [];
  55243. if (typeof resolvedServerOptions.host === "string" && resolvedServerOptions.host) {
  55244. list.push(resolvedServerOptions.host);
  55245. }
  55246. if (typeof resolvedServerOptions.hmr === "object" && resolvedServerOptions.hmr.host) {
  55247. list.push(resolvedServerOptions.hmr.host);
  55248. }
  55249. if (typeof resolvedPreviewOptions.host === "string" && resolvedPreviewOptions.host) {
  55250. list.push(resolvedPreviewOptions.host);
  55251. }
  55252. if (resolvedServerOptions.origin) {
  55253. try {
  55254. const serverOriginUrl = new URL(resolvedServerOptions.origin);
  55255. list.push(serverOriginUrl.hostname);
  55256. } catch {
  55257. }
  55258. }
  55259. return list;
  55260. }
  55261. function isHostAllowedWithoutCache(allowedHosts, additionalAllowedHosts, host) {
  55262. if (isFileOrExtensionProtocolRE.test(host)) {
  55263. return true;
  55264. }
  55265. const trimmedHost = host.trim();
  55266. if (trimmedHost[0] === "[") {
  55267. const endIpv6 = trimmedHost.indexOf("]");
  55268. if (endIpv6 < 0) {
  55269. return false;
  55270. }
  55271. return net$1.isIP(trimmedHost.slice(1, endIpv6)) === 6;
  55272. }
  55273. const colonPos = trimmedHost.indexOf(":");
  55274. const hostname = colonPos === -1 ? trimmedHost : trimmedHost.slice(0, colonPos);
  55275. if (net$1.isIP(hostname) === 4) {
  55276. return true;
  55277. }
  55278. if (hostname === "localhost" || hostname.endsWith(".localhost")) {
  55279. return true;
  55280. }
  55281. for (const additionalAllowedHost of additionalAllowedHosts) {
  55282. if (additionalAllowedHost === hostname) {
  55283. return true;
  55284. }
  55285. }
  55286. for (const allowedHost of allowedHosts) {
  55287. if (allowedHost === hostname) {
  55288. return true;
  55289. }
  55290. if (allowedHost[0] === "." && (allowedHost.slice(1) === hostname || hostname.endsWith(allowedHost))) {
  55291. return true;
  55292. }
  55293. }
  55294. return false;
  55295. }
  55296. function isHostAllowed(config, isPreview, host) {
  55297. const allowedHosts = isPreview ? config.preview.allowedHosts : config.server.allowedHosts;
  55298. if (allowedHosts === true) {
  55299. return true;
  55300. }
  55301. const cache = isPreview ? allowedHostsPreviewCache : allowedHostsServerCache;
  55302. if (!cache.has(config)) {
  55303. cache.set(config, /* @__PURE__ */ new Set());
  55304. }
  55305. const cachedAllowedHosts = cache.get(config);
  55306. if (cachedAllowedHosts.has(host)) {
  55307. return true;
  55308. }
  55309. const result = isHostAllowedWithoutCache(
  55310. allowedHosts ?? [],
  55311. config.additionalAllowedHosts,
  55312. host
  55313. );
  55314. if (result) {
  55315. cachedAllowedHosts.add(host);
  55316. }
  55317. return result;
  55318. }
  55319. function hostCheckMiddleware(config, isPreview) {
  55320. return function viteHostCheckMiddleware(req, res, next) {
  55321. const hostHeader = req.headers.host;
  55322. if (!hostHeader || !isHostAllowed(config, isPreview, hostHeader)) {
  55323. const hostname = hostHeader?.replace(/:\d+$/, "");
  55324. const hostnameWithQuotes = JSON.stringify(hostname);
  55325. const optionName = `${isPreview ? "preview" : "server"}.allowedHosts`;
  55326. res.writeHead(403, {
  55327. "Content-Type": "text/plain"
  55328. });
  55329. res.end(
  55330. `Blocked request. This host (${hostnameWithQuotes}) is not allowed.
  55331. To allow this host, add ${hostnameWithQuotes} to \`${optionName}\` in vite.config.js.`
  55332. );
  55333. return;
  55334. }
  55335. return next();
  55336. };
  55337. }
  55338. const WebSocketServerRaw = process.versions.bun ? (
  55339. // @ts-expect-error: Bun defines `import.meta.require`
  55340. import.meta.require("ws").WebSocketServer
  55341. ) : WebSocketServerRaw_;
  55342. const HMR_HEADER = "vite-hmr";
  55343. const wsServerEvents = [
  55344. "connection",
  55345. "error",
  55346. "headers",
  55347. "listening",
  55348. "message"
  55349. ];
  55350. function noop$1() {
  55351. }
  55352. function hasValidToken(config, url) {
  55353. const token = url.searchParams.get("token");
  55354. if (!token) return false;
  55355. try {
  55356. const isValidToken = crypto$2.timingSafeEqual(
  55357. Buffer.from(token),
  55358. Buffer.from(config.webSocketToken)
  55359. );
  55360. return isValidToken;
  55361. } catch {
  55362. }
  55363. return false;
  55364. }
  55365. function createWebSocketServer(server, config, httpsOptions) {
  55366. if (config.server.ws === false) {
  55367. return {
  55368. name: "ws",
  55369. get clients() {
  55370. return /* @__PURE__ */ new Set();
  55371. },
  55372. async close() {
  55373. },
  55374. on: noop$1,
  55375. off: noop$1,
  55376. listen: noop$1,
  55377. send: noop$1
  55378. };
  55379. }
  55380. let wsHttpServer = void 0;
  55381. const hmr = isObject$1(config.server.hmr) && config.server.hmr;
  55382. const hmrServer = hmr && hmr.server;
  55383. const hmrPort = hmr && hmr.port;
  55384. const portsAreCompatible = !hmrPort || hmrPort === config.server.port;
  55385. const wsServer = hmrServer || portsAreCompatible && server;
  55386. let hmrServerWsListener;
  55387. const customListeners = /* @__PURE__ */ new Map();
  55388. const clientsMap = /* @__PURE__ */ new WeakMap();
  55389. const port = hmrPort || 24678;
  55390. const host = hmr && hmr.host || void 0;
  55391. const shouldHandle = (req) => {
  55392. const hostHeader = req.headers.host;
  55393. if (!hostHeader || !isHostAllowed(config, false, hostHeader)) {
  55394. return false;
  55395. }
  55396. if (config.legacy?.skipWebSocketTokenCheck) {
  55397. return true;
  55398. }
  55399. if (req.headers.origin) {
  55400. const parsedUrl = new URL(`http://example.com${req.url}`);
  55401. return hasValidToken(config, parsedUrl);
  55402. }
  55403. return true;
  55404. };
  55405. const handleUpgrade = (req, socket, head, _isPing) => {
  55406. wss.handleUpgrade(req, socket, head, (ws) => {
  55407. wss.emit("connection", ws, req);
  55408. });
  55409. };
  55410. const wss = new WebSocketServerRaw({ noServer: true });
  55411. wss.shouldHandle = shouldHandle;
  55412. if (wsServer) {
  55413. let hmrBase = config.base;
  55414. const hmrPath = hmr ? hmr.path : void 0;
  55415. if (hmrPath) {
  55416. hmrBase = path$n.posix.join(hmrBase, hmrPath);
  55417. }
  55418. hmrServerWsListener = (req, socket, head) => {
  55419. const parsedUrl = new URL(`http://example.com${req.url}`);
  55420. if (req.headers["sec-websocket-protocol"] === HMR_HEADER && parsedUrl.pathname === hmrBase) {
  55421. handleUpgrade(req, socket, head);
  55422. }
  55423. };
  55424. wsServer.on("upgrade", hmrServerWsListener);
  55425. } else {
  55426. const route = (_, res) => {
  55427. const statusCode = 426;
  55428. const body = STATUS_CODES[statusCode];
  55429. if (!body)
  55430. throw new Error(`No body text found for the ${statusCode} status code`);
  55431. res.writeHead(statusCode, {
  55432. "Content-Length": body.length,
  55433. "Content-Type": "text/plain"
  55434. });
  55435. res.end(body);
  55436. };
  55437. if (httpsOptions) {
  55438. wsHttpServer = createServer$2(httpsOptions, route);
  55439. } else {
  55440. wsHttpServer = createServer$3(route);
  55441. }
  55442. wsHttpServer.on("upgrade", (req, socket, head) => {
  55443. handleUpgrade(req, socket, head);
  55444. });
  55445. wsHttpServer.on("error", (e) => {
  55446. if (e.code === "EADDRINUSE") {
  55447. config.logger.error(
  55448. colors$1.red(`WebSocket server error: Port is already in use`),
  55449. { error: e }
  55450. );
  55451. } else {
  55452. config.logger.error(
  55453. colors$1.red(`WebSocket server error:
  55454. ${e.stack || e.message}`),
  55455. { error: e }
  55456. );
  55457. }
  55458. });
  55459. }
  55460. wss.on("connection", (socket) => {
  55461. socket.on("message", (raw) => {
  55462. if (!customListeners.size) return;
  55463. let parsed;
  55464. try {
  55465. parsed = JSON.parse(String(raw));
  55466. } catch {
  55467. }
  55468. if (!parsed || parsed.type !== "custom" || !parsed.event) return;
  55469. const listeners = customListeners.get(parsed.event);
  55470. if (!listeners?.size) return;
  55471. const client = getSocketClient(socket);
  55472. listeners.forEach((listener) => listener(parsed.data, client));
  55473. });
  55474. socket.on("error", (err) => {
  55475. config.logger.error(`${colors$1.red(`ws error:`)}
  55476. ${err.stack}`, {
  55477. timestamp: true,
  55478. error: err
  55479. });
  55480. });
  55481. socket.send(JSON.stringify({ type: "connected" }));
  55482. if (bufferedError) {
  55483. socket.send(JSON.stringify(bufferedError));
  55484. bufferedError = null;
  55485. }
  55486. });
  55487. wss.on("error", (e) => {
  55488. if (e.code === "EADDRINUSE") {
  55489. config.logger.error(
  55490. colors$1.red(`WebSocket server error: Port is already in use`),
  55491. { error: e }
  55492. );
  55493. } else {
  55494. config.logger.error(
  55495. colors$1.red(`WebSocket server error:
  55496. ${e.stack || e.message}`),
  55497. { error: e }
  55498. );
  55499. }
  55500. });
  55501. function getSocketClient(socket) {
  55502. if (!clientsMap.has(socket)) {
  55503. clientsMap.set(socket, {
  55504. send: (...args) => {
  55505. let payload;
  55506. if (typeof args[0] === "string") {
  55507. payload = {
  55508. type: "custom",
  55509. event: args[0],
  55510. data: args[1]
  55511. };
  55512. } else {
  55513. payload = args[0];
  55514. }
  55515. socket.send(JSON.stringify(payload));
  55516. },
  55517. socket
  55518. });
  55519. }
  55520. return clientsMap.get(socket);
  55521. }
  55522. let bufferedError = null;
  55523. return {
  55524. name: "ws",
  55525. listen: () => {
  55526. wsHttpServer?.listen(port, host);
  55527. },
  55528. on: (event, fn) => {
  55529. if (wsServerEvents.includes(event)) wss.on(event, fn);
  55530. else {
  55531. if (!customListeners.has(event)) {
  55532. customListeners.set(event, /* @__PURE__ */ new Set());
  55533. }
  55534. customListeners.get(event).add(fn);
  55535. }
  55536. },
  55537. off: (event, fn) => {
  55538. if (wsServerEvents.includes(event)) {
  55539. wss.off(event, fn);
  55540. } else {
  55541. customListeners.get(event)?.delete(fn);
  55542. }
  55543. },
  55544. get clients() {
  55545. return new Set(Array.from(wss.clients).map(getSocketClient));
  55546. },
  55547. send(...args) {
  55548. let payload;
  55549. if (typeof args[0] === "string") {
  55550. payload = {
  55551. type: "custom",
  55552. event: args[0],
  55553. data: args[1]
  55554. };
  55555. } else {
  55556. payload = args[0];
  55557. }
  55558. if (payload.type === "error" && !wss.clients.size) {
  55559. bufferedError = payload;
  55560. return;
  55561. }
  55562. const stringified = JSON.stringify(payload);
  55563. wss.clients.forEach((client) => {
  55564. if (client.readyState === 1) {
  55565. client.send(stringified);
  55566. }
  55567. });
  55568. },
  55569. close() {
  55570. if (hmrServerWsListener && wsServer) {
  55571. wsServer.off("upgrade", hmrServerWsListener);
  55572. }
  55573. return new Promise((resolve, reject) => {
  55574. wss.clients.forEach((client) => {
  55575. client.terminate();
  55576. });
  55577. wss.close((err) => {
  55578. if (err) {
  55579. reject(err);
  55580. } else {
  55581. if (wsHttpServer) {
  55582. wsHttpServer.close((err2) => {
  55583. if (err2) {
  55584. reject(err2);
  55585. } else {
  55586. resolve();
  55587. }
  55588. });
  55589. } else {
  55590. resolve();
  55591. }
  55592. }
  55593. });
  55594. });
  55595. }
  55596. };
  55597. }
  55598. function baseMiddleware(rawBase, middlewareMode) {
  55599. return function viteBaseMiddleware(req, res, next) {
  55600. const url = req.url;
  55601. const pathname = cleanUrl(url);
  55602. const base = rawBase;
  55603. if (pathname.startsWith(base)) {
  55604. req.url = stripBase(url, base);
  55605. return next();
  55606. }
  55607. if (middlewareMode) {
  55608. return next();
  55609. }
  55610. if (pathname === "/" || pathname === "/index.html") {
  55611. res.writeHead(302, {
  55612. Location: base + url.slice(pathname.length)
  55613. });
  55614. res.end();
  55615. return;
  55616. }
  55617. const redirectPath = withTrailingSlash(url) !== base ? joinUrlSegments(base, url) : base;
  55618. if (req.headers.accept?.includes("text/html")) {
  55619. res.writeHead(404, {
  55620. "Content-Type": "text/html"
  55621. });
  55622. res.end(
  55623. `The server is configured with a public base URL of ${base} - did you mean to visit <a href="${redirectPath}">${redirectPath}</a> instead?`
  55624. );
  55625. return;
  55626. } else {
  55627. res.writeHead(404, {
  55628. "Content-Type": "text/plain"
  55629. });
  55630. res.end(
  55631. `The server is configured with a public base URL of ${base} - did you mean to visit ${redirectPath} instead?`
  55632. );
  55633. return;
  55634. }
  55635. };
  55636. }
  55637. var httpProxy$3 = {exports: {}};
  55638. var eventemitter3 = {exports: {}};
  55639. (function (module) {
  55640. var has = Object.prototype.hasOwnProperty
  55641. , prefix = '~';
  55642. /**
  55643. * Constructor to create a storage for our `EE` objects.
  55644. * An `Events` instance is a plain object whose properties are event names.
  55645. *
  55646. * @constructor
  55647. * @private
  55648. */
  55649. function Events() {}
  55650. //
  55651. // We try to not inherit from `Object.prototype`. In some engines creating an
  55652. // instance in this way is faster than calling `Object.create(null)` directly.
  55653. // If `Object.create(null)` is not supported we prefix the event names with a
  55654. // character to make sure that the built-in object properties are not
  55655. // overridden or used as an attack vector.
  55656. //
  55657. if (Object.create) {
  55658. Events.prototype = Object.create(null);
  55659. //
  55660. // This hack is needed because the `__proto__` property is still inherited in
  55661. // some old browsers like Android 4, iPhone 5.1, Opera 11 and Safari 5.
  55662. //
  55663. if (!new Events().__proto__) prefix = false;
  55664. }
  55665. /**
  55666. * Representation of a single event listener.
  55667. *
  55668. * @param {Function} fn The listener function.
  55669. * @param {*} context The context to invoke the listener with.
  55670. * @param {Boolean} [once=false] Specify if the listener is a one-time listener.
  55671. * @constructor
  55672. * @private
  55673. */
  55674. function EE(fn, context, once) {
  55675. this.fn = fn;
  55676. this.context = context;
  55677. this.once = once || false;
  55678. }
  55679. /**
  55680. * Add a listener for a given event.
  55681. *
  55682. * @param {EventEmitter} emitter Reference to the `EventEmitter` instance.
  55683. * @param {(String|Symbol)} event The event name.
  55684. * @param {Function} fn The listener function.
  55685. * @param {*} context The context to invoke the listener with.
  55686. * @param {Boolean} once Specify if the listener is a one-time listener.
  55687. * @returns {EventEmitter}
  55688. * @private
  55689. */
  55690. function addListener(emitter, event, fn, context, once) {
  55691. if (typeof fn !== 'function') {
  55692. throw new TypeError('The listener must be a function');
  55693. }
  55694. var listener = new EE(fn, context || emitter, once)
  55695. , evt = prefix ? prefix + event : event;
  55696. if (!emitter._events[evt]) emitter._events[evt] = listener, emitter._eventsCount++;
  55697. else if (!emitter._events[evt].fn) emitter._events[evt].push(listener);
  55698. else emitter._events[evt] = [emitter._events[evt], listener];
  55699. return emitter;
  55700. }
  55701. /**
  55702. * Clear event by name.
  55703. *
  55704. * @param {EventEmitter} emitter Reference to the `EventEmitter` instance.
  55705. * @param {(String|Symbol)} evt The Event name.
  55706. * @private
  55707. */
  55708. function clearEvent(emitter, evt) {
  55709. if (--emitter._eventsCount === 0) emitter._events = new Events();
  55710. else delete emitter._events[evt];
  55711. }
  55712. /**
  55713. * Minimal `EventEmitter` interface that is molded against the Node.js
  55714. * `EventEmitter` interface.
  55715. *
  55716. * @constructor
  55717. * @public
  55718. */
  55719. function EventEmitter() {
  55720. this._events = new Events();
  55721. this._eventsCount = 0;
  55722. }
  55723. /**
  55724. * Return an array listing the events for which the emitter has registered
  55725. * listeners.
  55726. *
  55727. * @returns {Array}
  55728. * @public
  55729. */
  55730. EventEmitter.prototype.eventNames = function eventNames() {
  55731. var names = []
  55732. , events
  55733. , name;
  55734. if (this._eventsCount === 0) return names;
  55735. for (name in (events = this._events)) {
  55736. if (has.call(events, name)) names.push(prefix ? name.slice(1) : name);
  55737. }
  55738. if (Object.getOwnPropertySymbols) {
  55739. return names.concat(Object.getOwnPropertySymbols(events));
  55740. }
  55741. return names;
  55742. };
  55743. /**
  55744. * Return the listeners registered for a given event.
  55745. *
  55746. * @param {(String|Symbol)} event The event name.
  55747. * @returns {Array} The registered listeners.
  55748. * @public
  55749. */
  55750. EventEmitter.prototype.listeners = function listeners(event) {
  55751. var evt = prefix ? prefix + event : event
  55752. , handlers = this._events[evt];
  55753. if (!handlers) return [];
  55754. if (handlers.fn) return [handlers.fn];
  55755. for (var i = 0, l = handlers.length, ee = new Array(l); i < l; i++) {
  55756. ee[i] = handlers[i].fn;
  55757. }
  55758. return ee;
  55759. };
  55760. /**
  55761. * Return the number of listeners listening to a given event.
  55762. *
  55763. * @param {(String|Symbol)} event The event name.
  55764. * @returns {Number} The number of listeners.
  55765. * @public
  55766. */
  55767. EventEmitter.prototype.listenerCount = function listenerCount(event) {
  55768. var evt = prefix ? prefix + event : event
  55769. , listeners = this._events[evt];
  55770. if (!listeners) return 0;
  55771. if (listeners.fn) return 1;
  55772. return listeners.length;
  55773. };
  55774. /**
  55775. * Calls each of the listeners registered for a given event.
  55776. *
  55777. * @param {(String|Symbol)} event The event name.
  55778. * @returns {Boolean} `true` if the event had listeners, else `false`.
  55779. * @public
  55780. */
  55781. EventEmitter.prototype.emit = function emit(event, a1, a2, a3, a4, a5) {
  55782. var evt = prefix ? prefix + event : event;
  55783. if (!this._events[evt]) return false;
  55784. var listeners = this._events[evt]
  55785. , len = arguments.length
  55786. , args
  55787. , i;
  55788. if (listeners.fn) {
  55789. if (listeners.once) this.removeListener(event, listeners.fn, undefined, true);
  55790. switch (len) {
  55791. case 1: return listeners.fn.call(listeners.context), true;
  55792. case 2: return listeners.fn.call(listeners.context, a1), true;
  55793. case 3: return listeners.fn.call(listeners.context, a1, a2), true;
  55794. case 4: return listeners.fn.call(listeners.context, a1, a2, a3), true;
  55795. case 5: return listeners.fn.call(listeners.context, a1, a2, a3, a4), true;
  55796. case 6: return listeners.fn.call(listeners.context, a1, a2, a3, a4, a5), true;
  55797. }
  55798. for (i = 1, args = new Array(len -1); i < len; i++) {
  55799. args[i - 1] = arguments[i];
  55800. }
  55801. listeners.fn.apply(listeners.context, args);
  55802. } else {
  55803. var length = listeners.length
  55804. , j;
  55805. for (i = 0; i < length; i++) {
  55806. if (listeners[i].once) this.removeListener(event, listeners[i].fn, undefined, true);
  55807. switch (len) {
  55808. case 1: listeners[i].fn.call(listeners[i].context); break;
  55809. case 2: listeners[i].fn.call(listeners[i].context, a1); break;
  55810. case 3: listeners[i].fn.call(listeners[i].context, a1, a2); break;
  55811. case 4: listeners[i].fn.call(listeners[i].context, a1, a2, a3); break;
  55812. default:
  55813. if (!args) for (j = 1, args = new Array(len -1); j < len; j++) {
  55814. args[j - 1] = arguments[j];
  55815. }
  55816. listeners[i].fn.apply(listeners[i].context, args);
  55817. }
  55818. }
  55819. }
  55820. return true;
  55821. };
  55822. /**
  55823. * Add a listener for a given event.
  55824. *
  55825. * @param {(String|Symbol)} event The event name.
  55826. * @param {Function} fn The listener function.
  55827. * @param {*} [context=this] The context to invoke the listener with.
  55828. * @returns {EventEmitter} `this`.
  55829. * @public
  55830. */
  55831. EventEmitter.prototype.on = function on(event, fn, context) {
  55832. return addListener(this, event, fn, context, false);
  55833. };
  55834. /**
  55835. * Add a one-time listener for a given event.
  55836. *
  55837. * @param {(String|Symbol)} event The event name.
  55838. * @param {Function} fn The listener function.
  55839. * @param {*} [context=this] The context to invoke the listener with.
  55840. * @returns {EventEmitter} `this`.
  55841. * @public
  55842. */
  55843. EventEmitter.prototype.once = function once(event, fn, context) {
  55844. return addListener(this, event, fn, context, true);
  55845. };
  55846. /**
  55847. * Remove the listeners of a given event.
  55848. *
  55849. * @param {(String|Symbol)} event The event name.
  55850. * @param {Function} fn Only remove the listeners that match this function.
  55851. * @param {*} context Only remove the listeners that have this context.
  55852. * @param {Boolean} once Only remove one-time listeners.
  55853. * @returns {EventEmitter} `this`.
  55854. * @public
  55855. */
  55856. EventEmitter.prototype.removeListener = function removeListener(event, fn, context, once) {
  55857. var evt = prefix ? prefix + event : event;
  55858. if (!this._events[evt]) return this;
  55859. if (!fn) {
  55860. clearEvent(this, evt);
  55861. return this;
  55862. }
  55863. var listeners = this._events[evt];
  55864. if (listeners.fn) {
  55865. if (
  55866. listeners.fn === fn &&
  55867. (!once || listeners.once) &&
  55868. (!context || listeners.context === context)
  55869. ) {
  55870. clearEvent(this, evt);
  55871. }
  55872. } else {
  55873. for (var i = 0, events = [], length = listeners.length; i < length; i++) {
  55874. if (
  55875. listeners[i].fn !== fn ||
  55876. (once && !listeners[i].once) ||
  55877. (context && listeners[i].context !== context)
  55878. ) {
  55879. events.push(listeners[i]);
  55880. }
  55881. }
  55882. //
  55883. // Reset the array, or remove it completely if we have no more listeners.
  55884. //
  55885. if (events.length) this._events[evt] = events.length === 1 ? events[0] : events;
  55886. else clearEvent(this, evt);
  55887. }
  55888. return this;
  55889. };
  55890. /**
  55891. * Remove all listeners, or those of the specified event.
  55892. *
  55893. * @param {(String|Symbol)} [event] The event name.
  55894. * @returns {EventEmitter} `this`.
  55895. * @public
  55896. */
  55897. EventEmitter.prototype.removeAllListeners = function removeAllListeners(event) {
  55898. var evt;
  55899. if (event) {
  55900. evt = prefix ? prefix + event : event;
  55901. if (this._events[evt]) clearEvent(this, evt);
  55902. } else {
  55903. this._events = new Events();
  55904. this._eventsCount = 0;
  55905. }
  55906. return this;
  55907. };
  55908. //
  55909. // Alias methods names because people roll like that.
  55910. //
  55911. EventEmitter.prototype.off = EventEmitter.prototype.removeListener;
  55912. EventEmitter.prototype.addListener = EventEmitter.prototype.on;
  55913. //
  55914. // Expose the prefix.
  55915. //
  55916. EventEmitter.prefixed = prefix;
  55917. //
  55918. // Allow `EventEmitter` to be imported as module namespace.
  55919. //
  55920. EventEmitter.EventEmitter = EventEmitter;
  55921. //
  55922. // Expose the module.
  55923. //
  55924. {
  55925. module.exports = EventEmitter;
  55926. }
  55927. } (eventemitter3));
  55928. var eventemitter3Exports = eventemitter3.exports;
  55929. var common$3 = {};
  55930. /**
  55931. * Check if we're required to add a port number.
  55932. *
  55933. * @see https://url.spec.whatwg.org/#default-port
  55934. * @param {Number|String} port Port number we need to check
  55935. * @param {String} protocol Protocol we need to check against.
  55936. * @returns {Boolean} Is it a default port for the given protocol
  55937. * @api private
  55938. */
  55939. var requiresPort = function required(port, protocol) {
  55940. protocol = protocol.split(':')[0];
  55941. port = +port;
  55942. if (!port) return false;
  55943. switch (protocol) {
  55944. case 'http':
  55945. case 'ws':
  55946. return port !== 80;
  55947. case 'https':
  55948. case 'wss':
  55949. return port !== 443;
  55950. case 'ftp':
  55951. return port !== 21;
  55952. case 'gopher':
  55953. return port !== 70;
  55954. case 'file':
  55955. return false;
  55956. }
  55957. return port !== 0;
  55958. };
  55959. (function (exports) {
  55960. var common = exports,
  55961. url = require$$0$9,
  55962. required = requiresPort;
  55963. var upgradeHeader = /(^|,)\s*upgrade\s*($|,)/i,
  55964. isSSL = /^https|wss/;
  55965. /**
  55966. * Simple Regex for testing if protocol is https
  55967. */
  55968. common.isSSL = isSSL;
  55969. /**
  55970. * Copies the right headers from `options` and `req` to
  55971. * `outgoing` which is then used to fire the proxied
  55972. * request.
  55973. *
  55974. * Examples:
  55975. *
  55976. * common.setupOutgoing(outgoing, options, req)
  55977. * // => { host: ..., hostname: ...}
  55978. *
  55979. * @param {Object} Outgoing Base object to be filled with required properties
  55980. * @param {Object} Options Config object passed to the proxy
  55981. * @param {ClientRequest} Req Request Object
  55982. * @param {String} Forward String to select forward or target
  55983. * @return {Object} Outgoing Object with all required properties set
  55984. *
  55985. * @api private
  55986. */
  55987. common.setupOutgoing = function(outgoing, options, req, forward) {
  55988. outgoing.port = options[forward || 'target'].port ||
  55989. (isSSL.test(options[forward || 'target'].protocol) ? 443 : 80);
  55990. ['host', 'hostname', 'socketPath', 'pfx', 'key',
  55991. 'passphrase', 'cert', 'ca', 'ciphers', 'secureProtocol'].forEach(
  55992. function(e) { outgoing[e] = options[forward || 'target'][e]; }
  55993. );
  55994. outgoing.method = options.method || req.method;
  55995. outgoing.headers = Object.assign({}, req.headers);
  55996. if (options.headers){
  55997. Object.assign(outgoing.headers, options.headers);
  55998. }
  55999. if (options.auth) {
  56000. outgoing.auth = options.auth;
  56001. }
  56002. if (options.ca) {
  56003. outgoing.ca = options.ca;
  56004. }
  56005. if (isSSL.test(options[forward || 'target'].protocol)) {
  56006. outgoing.rejectUnauthorized = (typeof options.secure === "undefined") ? true : options.secure;
  56007. }
  56008. outgoing.agent = options.agent || false;
  56009. outgoing.localAddress = options.localAddress;
  56010. //
  56011. // Remark: If we are false and not upgrading, set the connection: close. This is the right thing to do
  56012. // as node core doesn't handle this COMPLETELY properly yet.
  56013. //
  56014. if (!outgoing.agent) {
  56015. outgoing.headers = outgoing.headers || {};
  56016. if (typeof outgoing.headers.connection !== 'string'
  56017. || !upgradeHeader.test(outgoing.headers.connection)
  56018. ) { outgoing.headers.connection = 'close'; }
  56019. }
  56020. // the final path is target path + relative path requested by user:
  56021. var target = options[forward || 'target'];
  56022. var targetPath = target && options.prependPath !== false
  56023. ? (target.path || '')
  56024. : '';
  56025. //
  56026. // Remark: Can we somehow not use url.parse as a perf optimization?
  56027. //
  56028. var outgoingPath = !options.toProxy
  56029. ? (url.parse(req.url).path || '')
  56030. : req.url;
  56031. //
  56032. // Remark: ignorePath will just straight up ignore whatever the request's
  56033. // path is. This can be labeled as FOOT-GUN material if you do not know what
  56034. // you are doing and are using conflicting options.
  56035. //
  56036. outgoingPath = !options.ignorePath ? outgoingPath : '';
  56037. outgoing.path = common.urlJoin(targetPath, outgoingPath);
  56038. if (options.changeOrigin) {
  56039. outgoing.headers.host =
  56040. required(outgoing.port, options[forward || 'target'].protocol) && !hasPort(outgoing.host)
  56041. ? outgoing.host + ':' + outgoing.port
  56042. : outgoing.host;
  56043. }
  56044. return outgoing;
  56045. };
  56046. /**
  56047. * Set the proper configuration for sockets,
  56048. * set no delay and set keep alive, also set
  56049. * the timeout to 0.
  56050. *
  56051. * Examples:
  56052. *
  56053. * common.setupSocket(socket)
  56054. * // => Socket
  56055. *
  56056. * @param {Socket} Socket instance to setup
  56057. * @return {Socket} Return the configured socket.
  56058. *
  56059. * @api private
  56060. */
  56061. common.setupSocket = function(socket) {
  56062. socket.setTimeout(0);
  56063. socket.setNoDelay(true);
  56064. socket.setKeepAlive(true, 0);
  56065. return socket;
  56066. };
  56067. /**
  56068. * Get the port number from the host. Or guess it based on the connection type.
  56069. *
  56070. * @param {Request} req Incoming HTTP request.
  56071. *
  56072. * @return {String} The port number.
  56073. *
  56074. * @api private
  56075. */
  56076. common.getPort = function(req) {
  56077. var res = req.headers.host ? req.headers.host.match(/:(\d+)/) : '';
  56078. return res ?
  56079. res[1] :
  56080. common.hasEncryptedConnection(req) ? '443' : '80';
  56081. };
  56082. /**
  56083. * Check if the request has an encrypted connection.
  56084. *
  56085. * @param {Request} req Incoming HTTP request.
  56086. *
  56087. * @return {Boolean} Whether the connection is encrypted or not.
  56088. *
  56089. * @api private
  56090. */
  56091. common.hasEncryptedConnection = function(req) {
  56092. return Boolean(req.connection.encrypted || req.connection.pair);
  56093. };
  56094. /**
  56095. * OS-agnostic join (doesn't break on URLs like path.join does on Windows)>
  56096. *
  56097. * @return {String} The generated path.
  56098. *
  56099. * @api private
  56100. */
  56101. common.urlJoin = function() {
  56102. //
  56103. // We do not want to mess with the query string. All we want to touch is the path.
  56104. //
  56105. var args = Array.prototype.slice.call(arguments),
  56106. lastIndex = args.length - 1,
  56107. last = args[lastIndex],
  56108. lastSegs = last.split('?'),
  56109. retSegs;
  56110. args[lastIndex] = lastSegs.shift();
  56111. //
  56112. // Join all strings, but remove empty strings so we don't get extra slashes from
  56113. // joining e.g. ['', 'am']
  56114. //
  56115. retSegs = [
  56116. args.filter(Boolean).join('/')
  56117. .replace(/\/+/g, '/')
  56118. .replace('http:/', 'http://')
  56119. .replace('https:/', 'https://')
  56120. ];
  56121. // Only join the query string if it exists so we don't have trailing a '?'
  56122. // on every request
  56123. // Handle case where there could be multiple ? in the URL.
  56124. retSegs.push.apply(retSegs, lastSegs);
  56125. return retSegs.join('?')
  56126. };
  56127. /**
  56128. * Rewrites or removes the domain of a cookie header
  56129. *
  56130. * @param {String|Array} Header
  56131. * @param {Object} Config, mapping of domain to rewritten domain.
  56132. * '*' key to match any domain, null value to remove the domain.
  56133. *
  56134. * @api private
  56135. */
  56136. common.rewriteCookieProperty = function rewriteCookieProperty(header, config, property) {
  56137. if (Array.isArray(header)) {
  56138. return header.map(function (headerElement) {
  56139. return rewriteCookieProperty(headerElement, config, property);
  56140. });
  56141. }
  56142. return header.replace(new RegExp("(;\\s*" + property + "=)([^;]+)", 'i'), function(match, prefix, previousValue) {
  56143. var newValue;
  56144. if (previousValue in config) {
  56145. newValue = config[previousValue];
  56146. } else if ('*' in config) {
  56147. newValue = config['*'];
  56148. } else {
  56149. //no match, return previous value
  56150. return match;
  56151. }
  56152. if (newValue) {
  56153. //replace value
  56154. return prefix + newValue;
  56155. } else {
  56156. //remove value
  56157. return '';
  56158. }
  56159. });
  56160. };
  56161. /**
  56162. * Check the host and see if it potentially has a port in it (keep it simple)
  56163. *
  56164. * @returns {Boolean} Whether we have one or not
  56165. *
  56166. * @api private
  56167. */
  56168. function hasPort(host) {
  56169. return !!~host.indexOf(':');
  56170. }} (common$3));
  56171. var url$1 = require$$0$9,
  56172. common$2 = common$3;
  56173. var redirectRegex = /^201|30(1|2|7|8)$/;
  56174. /*!
  56175. * Array of passes.
  56176. *
  56177. * A `pass` is just a function that is executed on `req, res, options`
  56178. * so that you can easily add new checks while still keeping the base
  56179. * flexible.
  56180. */
  56181. var webOutgoing = { // <--
  56182. /**
  56183. * If is a HTTP 1.0 request, remove chunk headers
  56184. *
  56185. * @param {ClientRequest} Req Request object
  56186. * @param {IncomingMessage} Res Response object
  56187. * @param {proxyResponse} Res Response object from the proxy request
  56188. *
  56189. * @api private
  56190. */
  56191. removeChunked: function removeChunked(req, res, proxyRes) {
  56192. if (req.httpVersion === '1.0') {
  56193. delete proxyRes.headers['transfer-encoding'];
  56194. }
  56195. },
  56196. /**
  56197. * If is a HTTP 1.0 request, set the correct connection header
  56198. * or if connection header not present, then use `keep-alive`
  56199. *
  56200. * @param {ClientRequest} Req Request object
  56201. * @param {IncomingMessage} Res Response object
  56202. * @param {proxyResponse} Res Response object from the proxy request
  56203. *
  56204. * @api private
  56205. */
  56206. setConnection: function setConnection(req, res, proxyRes) {
  56207. if (req.httpVersion === '1.0') {
  56208. proxyRes.headers.connection = req.headers.connection || 'close';
  56209. } else if (req.httpVersion !== '2.0' && !proxyRes.headers.connection) {
  56210. proxyRes.headers.connection = req.headers.connection || 'keep-alive';
  56211. }
  56212. },
  56213. setRedirectHostRewrite: function setRedirectHostRewrite(req, res, proxyRes, options) {
  56214. if ((options.hostRewrite || options.autoRewrite || options.protocolRewrite)
  56215. && proxyRes.headers['location']
  56216. && redirectRegex.test(proxyRes.statusCode)) {
  56217. var target = url$1.parse(options.target);
  56218. var u = url$1.parse(proxyRes.headers['location']);
  56219. // make sure the redirected host matches the target host before rewriting
  56220. if (target.host != u.host) {
  56221. return;
  56222. }
  56223. if (options.hostRewrite) {
  56224. u.host = options.hostRewrite;
  56225. } else if (options.autoRewrite) {
  56226. u.host = req.headers['host'];
  56227. }
  56228. if (options.protocolRewrite) {
  56229. u.protocol = options.protocolRewrite;
  56230. }
  56231. proxyRes.headers['location'] = u.format();
  56232. }
  56233. },
  56234. /**
  56235. * Copy headers from proxyResponse to response
  56236. * set each header in response object.
  56237. *
  56238. * @param {ClientRequest} Req Request object
  56239. * @param {IncomingMessage} Res Response object
  56240. * @param {proxyResponse} Res Response object from the proxy request
  56241. * @param {Object} Options options.cookieDomainRewrite: Config to rewrite cookie domain
  56242. *
  56243. * @api private
  56244. */
  56245. writeHeaders: function writeHeaders(req, res, proxyRes, options) {
  56246. var rewriteCookieDomainConfig = options.cookieDomainRewrite,
  56247. rewriteCookiePathConfig = options.cookiePathRewrite,
  56248. preserveHeaderKeyCase = options.preserveHeaderKeyCase,
  56249. rawHeaderKeyMap,
  56250. setHeader = function(key, header) {
  56251. if (header == undefined) return;
  56252. if (rewriteCookieDomainConfig && key.toLowerCase() === 'set-cookie') {
  56253. header = common$2.rewriteCookieProperty(header, rewriteCookieDomainConfig, 'domain');
  56254. }
  56255. if (rewriteCookiePathConfig && key.toLowerCase() === 'set-cookie') {
  56256. header = common$2.rewriteCookieProperty(header, rewriteCookiePathConfig, 'path');
  56257. }
  56258. res.setHeader(String(key).trim(), header);
  56259. };
  56260. if (typeof rewriteCookieDomainConfig === 'string') { //also test for ''
  56261. rewriteCookieDomainConfig = { '*': rewriteCookieDomainConfig };
  56262. }
  56263. if (typeof rewriteCookiePathConfig === 'string') { //also test for ''
  56264. rewriteCookiePathConfig = { '*': rewriteCookiePathConfig };
  56265. }
  56266. // message.rawHeaders is added in: v0.11.6
  56267. // https://nodejs.org/api/http.html#http_message_rawheaders
  56268. if (preserveHeaderKeyCase && proxyRes.rawHeaders != undefined) {
  56269. rawHeaderKeyMap = {};
  56270. for (var i = 0; i < proxyRes.rawHeaders.length; i += 2) {
  56271. var key = proxyRes.rawHeaders[i];
  56272. rawHeaderKeyMap[key.toLowerCase()] = key;
  56273. }
  56274. }
  56275. Object.keys(proxyRes.headers).forEach(function(key) {
  56276. var header = proxyRes.headers[key];
  56277. if (preserveHeaderKeyCase && rawHeaderKeyMap) {
  56278. key = rawHeaderKeyMap[key] || key;
  56279. }
  56280. setHeader(key, header);
  56281. });
  56282. },
  56283. /**
  56284. * Set the statusCode from the proxyResponse
  56285. *
  56286. * @param {ClientRequest} Req Request object
  56287. * @param {IncomingMessage} Res Response object
  56288. * @param {proxyResponse} Res Response object from the proxy request
  56289. *
  56290. * @api private
  56291. */
  56292. writeStatusCode: function writeStatusCode(req, res, proxyRes) {
  56293. // From Node.js docs: response.writeHead(statusCode[, statusMessage][, headers])
  56294. if(proxyRes.statusMessage) {
  56295. res.statusCode = proxyRes.statusCode;
  56296. res.statusMessage = proxyRes.statusMessage;
  56297. } else {
  56298. res.statusCode = proxyRes.statusCode;
  56299. }
  56300. }
  56301. };
  56302. var followRedirects$1 = {exports: {}};
  56303. var debug$6;
  56304. var debug_1 = function () {
  56305. if (!debug$6) {
  56306. try {
  56307. /* eslint global-require: off */
  56308. debug$6 = srcExports$1("follow-redirects");
  56309. }
  56310. catch (error) { /* */ }
  56311. if (typeof debug$6 !== "function") {
  56312. debug$6 = function () { /* */ };
  56313. }
  56314. }
  56315. debug$6.apply(null, arguments);
  56316. };
  56317. var url = require$$0$9;
  56318. var URL$1 = url.URL;
  56319. var http$1 = require$$1;
  56320. var https$1 = require$$1$1;
  56321. var Writable = require$$0$6.Writable;
  56322. var assert = require$$4$3;
  56323. var debug$5 = debug_1;
  56324. // Whether to use the native URL object or the legacy url module
  56325. var useNativeURL = false;
  56326. try {
  56327. assert(new URL$1());
  56328. }
  56329. catch (error) {
  56330. useNativeURL = error.code === "ERR_INVALID_URL";
  56331. }
  56332. // URL fields to preserve in copy operations
  56333. var preservedUrlFields = [
  56334. "auth",
  56335. "host",
  56336. "hostname",
  56337. "href",
  56338. "path",
  56339. "pathname",
  56340. "port",
  56341. "protocol",
  56342. "query",
  56343. "search",
  56344. "hash",
  56345. ];
  56346. // Create handlers that pass events from native requests
  56347. var events = ["abort", "aborted", "connect", "error", "socket", "timeout"];
  56348. var eventHandlers = Object.create(null);
  56349. events.forEach(function (event) {
  56350. eventHandlers[event] = function (arg1, arg2, arg3) {
  56351. this._redirectable.emit(event, arg1, arg2, arg3);
  56352. };
  56353. });
  56354. // Error types with codes
  56355. var InvalidUrlError = createErrorType(
  56356. "ERR_INVALID_URL",
  56357. "Invalid URL",
  56358. TypeError
  56359. );
  56360. var RedirectionError = createErrorType(
  56361. "ERR_FR_REDIRECTION_FAILURE",
  56362. "Redirected request failed"
  56363. );
  56364. var TooManyRedirectsError = createErrorType(
  56365. "ERR_FR_TOO_MANY_REDIRECTS",
  56366. "Maximum number of redirects exceeded",
  56367. RedirectionError
  56368. );
  56369. var MaxBodyLengthExceededError = createErrorType(
  56370. "ERR_FR_MAX_BODY_LENGTH_EXCEEDED",
  56371. "Request body larger than maxBodyLength limit"
  56372. );
  56373. var WriteAfterEndError = createErrorType(
  56374. "ERR_STREAM_WRITE_AFTER_END",
  56375. "write after end"
  56376. );
  56377. // istanbul ignore next
  56378. var destroy = Writable.prototype.destroy || noop;
  56379. // An HTTP(S) request that can be redirected
  56380. function RedirectableRequest(options, responseCallback) {
  56381. // Initialize the request
  56382. Writable.call(this);
  56383. this._sanitizeOptions(options);
  56384. this._options = options;
  56385. this._ended = false;
  56386. this._ending = false;
  56387. this._redirectCount = 0;
  56388. this._redirects = [];
  56389. this._requestBodyLength = 0;
  56390. this._requestBodyBuffers = [];
  56391. // Attach a callback if passed
  56392. if (responseCallback) {
  56393. this.on("response", responseCallback);
  56394. }
  56395. // React to responses of native requests
  56396. var self = this;
  56397. this._onNativeResponse = function (response) {
  56398. try {
  56399. self._processResponse(response);
  56400. }
  56401. catch (cause) {
  56402. self.emit("error", cause instanceof RedirectionError ?
  56403. cause : new RedirectionError({ cause: cause }));
  56404. }
  56405. };
  56406. // Perform the first request
  56407. this._performRequest();
  56408. }
  56409. RedirectableRequest.prototype = Object.create(Writable.prototype);
  56410. RedirectableRequest.prototype.abort = function () {
  56411. destroyRequest(this._currentRequest);
  56412. this._currentRequest.abort();
  56413. this.emit("abort");
  56414. };
  56415. RedirectableRequest.prototype.destroy = function (error) {
  56416. destroyRequest(this._currentRequest, error);
  56417. destroy.call(this, error);
  56418. return this;
  56419. };
  56420. // Writes buffered data to the current native request
  56421. RedirectableRequest.prototype.write = function (data, encoding, callback) {
  56422. // Writing is not allowed if end has been called
  56423. if (this._ending) {
  56424. throw new WriteAfterEndError();
  56425. }
  56426. // Validate input and shift parameters if necessary
  56427. if (!isString(data) && !isBuffer(data)) {
  56428. throw new TypeError("data should be a string, Buffer or Uint8Array");
  56429. }
  56430. if (isFunction(encoding)) {
  56431. callback = encoding;
  56432. encoding = null;
  56433. }
  56434. // Ignore empty buffers, since writing them doesn't invoke the callback
  56435. // https://github.com/nodejs/node/issues/22066
  56436. if (data.length === 0) {
  56437. if (callback) {
  56438. callback();
  56439. }
  56440. return;
  56441. }
  56442. // Only write when we don't exceed the maximum body length
  56443. if (this._requestBodyLength + data.length <= this._options.maxBodyLength) {
  56444. this._requestBodyLength += data.length;
  56445. this._requestBodyBuffers.push({ data: data, encoding: encoding });
  56446. this._currentRequest.write(data, encoding, callback);
  56447. }
  56448. // Error when we exceed the maximum body length
  56449. else {
  56450. this.emit("error", new MaxBodyLengthExceededError());
  56451. this.abort();
  56452. }
  56453. };
  56454. // Ends the current native request
  56455. RedirectableRequest.prototype.end = function (data, encoding, callback) {
  56456. // Shift parameters if necessary
  56457. if (isFunction(data)) {
  56458. callback = data;
  56459. data = encoding = null;
  56460. }
  56461. else if (isFunction(encoding)) {
  56462. callback = encoding;
  56463. encoding = null;
  56464. }
  56465. // Write data if needed and end
  56466. if (!data) {
  56467. this._ended = this._ending = true;
  56468. this._currentRequest.end(null, null, callback);
  56469. }
  56470. else {
  56471. var self = this;
  56472. var currentRequest = this._currentRequest;
  56473. this.write(data, encoding, function () {
  56474. self._ended = true;
  56475. currentRequest.end(null, null, callback);
  56476. });
  56477. this._ending = true;
  56478. }
  56479. };
  56480. // Sets a header value on the current native request
  56481. RedirectableRequest.prototype.setHeader = function (name, value) {
  56482. this._options.headers[name] = value;
  56483. this._currentRequest.setHeader(name, value);
  56484. };
  56485. // Clears a header value on the current native request
  56486. RedirectableRequest.prototype.removeHeader = function (name) {
  56487. delete this._options.headers[name];
  56488. this._currentRequest.removeHeader(name);
  56489. };
  56490. // Global timeout for all underlying requests
  56491. RedirectableRequest.prototype.setTimeout = function (msecs, callback) {
  56492. var self = this;
  56493. // Destroys the socket on timeout
  56494. function destroyOnTimeout(socket) {
  56495. socket.setTimeout(msecs);
  56496. socket.removeListener("timeout", socket.destroy);
  56497. socket.addListener("timeout", socket.destroy);
  56498. }
  56499. // Sets up a timer to trigger a timeout event
  56500. function startTimer(socket) {
  56501. if (self._timeout) {
  56502. clearTimeout(self._timeout);
  56503. }
  56504. self._timeout = setTimeout(function () {
  56505. self.emit("timeout");
  56506. clearTimer();
  56507. }, msecs);
  56508. destroyOnTimeout(socket);
  56509. }
  56510. // Stops a timeout from triggering
  56511. function clearTimer() {
  56512. // Clear the timeout
  56513. if (self._timeout) {
  56514. clearTimeout(self._timeout);
  56515. self._timeout = null;
  56516. }
  56517. // Clean up all attached listeners
  56518. self.removeListener("abort", clearTimer);
  56519. self.removeListener("error", clearTimer);
  56520. self.removeListener("response", clearTimer);
  56521. self.removeListener("close", clearTimer);
  56522. if (callback) {
  56523. self.removeListener("timeout", callback);
  56524. }
  56525. if (!self.socket) {
  56526. self._currentRequest.removeListener("socket", startTimer);
  56527. }
  56528. }
  56529. // Attach callback if passed
  56530. if (callback) {
  56531. this.on("timeout", callback);
  56532. }
  56533. // Start the timer if or when the socket is opened
  56534. if (this.socket) {
  56535. startTimer(this.socket);
  56536. }
  56537. else {
  56538. this._currentRequest.once("socket", startTimer);
  56539. }
  56540. // Clean up on events
  56541. this.on("socket", destroyOnTimeout);
  56542. this.on("abort", clearTimer);
  56543. this.on("error", clearTimer);
  56544. this.on("response", clearTimer);
  56545. this.on("close", clearTimer);
  56546. return this;
  56547. };
  56548. // Proxy all other public ClientRequest methods
  56549. [
  56550. "flushHeaders", "getHeader",
  56551. "setNoDelay", "setSocketKeepAlive",
  56552. ].forEach(function (method) {
  56553. RedirectableRequest.prototype[method] = function (a, b) {
  56554. return this._currentRequest[method](a, b);
  56555. };
  56556. });
  56557. // Proxy all public ClientRequest properties
  56558. ["aborted", "connection", "socket"].forEach(function (property) {
  56559. Object.defineProperty(RedirectableRequest.prototype, property, {
  56560. get: function () { return this._currentRequest[property]; },
  56561. });
  56562. });
  56563. RedirectableRequest.prototype._sanitizeOptions = function (options) {
  56564. // Ensure headers are always present
  56565. if (!options.headers) {
  56566. options.headers = {};
  56567. }
  56568. // Since http.request treats host as an alias of hostname,
  56569. // but the url module interprets host as hostname plus port,
  56570. // eliminate the host property to avoid confusion.
  56571. if (options.host) {
  56572. // Use hostname if set, because it has precedence
  56573. if (!options.hostname) {
  56574. options.hostname = options.host;
  56575. }
  56576. delete options.host;
  56577. }
  56578. // Complete the URL object when necessary
  56579. if (!options.pathname && options.path) {
  56580. var searchPos = options.path.indexOf("?");
  56581. if (searchPos < 0) {
  56582. options.pathname = options.path;
  56583. }
  56584. else {
  56585. options.pathname = options.path.substring(0, searchPos);
  56586. options.search = options.path.substring(searchPos);
  56587. }
  56588. }
  56589. };
  56590. // Executes the next native request (initial or redirect)
  56591. RedirectableRequest.prototype._performRequest = function () {
  56592. // Load the native protocol
  56593. var protocol = this._options.protocol;
  56594. var nativeProtocol = this._options.nativeProtocols[protocol];
  56595. if (!nativeProtocol) {
  56596. throw new TypeError("Unsupported protocol " + protocol);
  56597. }
  56598. // If specified, use the agent corresponding to the protocol
  56599. // (HTTP and HTTPS use different types of agents)
  56600. if (this._options.agents) {
  56601. var scheme = protocol.slice(0, -1);
  56602. this._options.agent = this._options.agents[scheme];
  56603. }
  56604. // Create the native request and set up its event handlers
  56605. var request = this._currentRequest =
  56606. nativeProtocol.request(this._options, this._onNativeResponse);
  56607. request._redirectable = this;
  56608. for (var event of events) {
  56609. request.on(event, eventHandlers[event]);
  56610. }
  56611. // RFC7230§5.3.1: When making a request directly to an origin server, […]
  56612. // a client MUST send only the absolute path […] as the request-target.
  56613. this._currentUrl = /^\//.test(this._options.path) ?
  56614. url.format(this._options) :
  56615. // When making a request to a proxy, […]
  56616. // a client MUST send the target URI in absolute-form […].
  56617. this._options.path;
  56618. // End a redirected request
  56619. // (The first request must be ended explicitly with RedirectableRequest#end)
  56620. if (this._isRedirect) {
  56621. // Write the request entity and end
  56622. var i = 0;
  56623. var self = this;
  56624. var buffers = this._requestBodyBuffers;
  56625. (function writeNext(error) {
  56626. // Only write if this request has not been redirected yet
  56627. /* istanbul ignore else */
  56628. if (request === self._currentRequest) {
  56629. // Report any write errors
  56630. /* istanbul ignore if */
  56631. if (error) {
  56632. self.emit("error", error);
  56633. }
  56634. // Write the next buffer if there are still left
  56635. else if (i < buffers.length) {
  56636. var buffer = buffers[i++];
  56637. /* istanbul ignore else */
  56638. if (!request.finished) {
  56639. request.write(buffer.data, buffer.encoding, writeNext);
  56640. }
  56641. }
  56642. // End the request if `end` has been called on us
  56643. else if (self._ended) {
  56644. request.end();
  56645. }
  56646. }
  56647. }());
  56648. }
  56649. };
  56650. // Processes a response from the current native request
  56651. RedirectableRequest.prototype._processResponse = function (response) {
  56652. // Store the redirected response
  56653. var statusCode = response.statusCode;
  56654. if (this._options.trackRedirects) {
  56655. this._redirects.push({
  56656. url: this._currentUrl,
  56657. headers: response.headers,
  56658. statusCode: statusCode,
  56659. });
  56660. }
  56661. // RFC7231§6.4: The 3xx (Redirection) class of status code indicates
  56662. // that further action needs to be taken by the user agent in order to
  56663. // fulfill the request. If a Location header field is provided,
  56664. // the user agent MAY automatically redirect its request to the URI
  56665. // referenced by the Location field value,
  56666. // even if the specific status code is not understood.
  56667. // If the response is not a redirect; return it as-is
  56668. var location = response.headers.location;
  56669. if (!location || this._options.followRedirects === false ||
  56670. statusCode < 300 || statusCode >= 400) {
  56671. response.responseUrl = this._currentUrl;
  56672. response.redirects = this._redirects;
  56673. this.emit("response", response);
  56674. // Clean up
  56675. this._requestBodyBuffers = [];
  56676. return;
  56677. }
  56678. // The response is a redirect, so abort the current request
  56679. destroyRequest(this._currentRequest);
  56680. // Discard the remainder of the response to avoid waiting for data
  56681. response.destroy();
  56682. // RFC7231§6.4: A client SHOULD detect and intervene
  56683. // in cyclical redirections (i.e., "infinite" redirection loops).
  56684. if (++this._redirectCount > this._options.maxRedirects) {
  56685. throw new TooManyRedirectsError();
  56686. }
  56687. // Store the request headers if applicable
  56688. var requestHeaders;
  56689. var beforeRedirect = this._options.beforeRedirect;
  56690. if (beforeRedirect) {
  56691. requestHeaders = Object.assign({
  56692. // The Host header was set by nativeProtocol.request
  56693. Host: response.req.getHeader("host"),
  56694. }, this._options.headers);
  56695. }
  56696. // RFC7231§6.4: Automatic redirection needs to done with
  56697. // care for methods not known to be safe, […]
  56698. // RFC7231§6.4.2–3: For historical reasons, a user agent MAY change
  56699. // the request method from POST to GET for the subsequent request.
  56700. var method = this._options.method;
  56701. if ((statusCode === 301 || statusCode === 302) && this._options.method === "POST" ||
  56702. // RFC7231§6.4.4: The 303 (See Other) status code indicates that
  56703. // the server is redirecting the user agent to a different resource […]
  56704. // A user agent can perform a retrieval request targeting that URI
  56705. // (a GET or HEAD request if using HTTP) […]
  56706. (statusCode === 303) && !/^(?:GET|HEAD)$/.test(this._options.method)) {
  56707. this._options.method = "GET";
  56708. // Drop a possible entity and headers related to it
  56709. this._requestBodyBuffers = [];
  56710. removeMatchingHeaders(/^content-/i, this._options.headers);
  56711. }
  56712. // Drop the Host header, as the redirect might lead to a different host
  56713. var currentHostHeader = removeMatchingHeaders(/^host$/i, this._options.headers);
  56714. // If the redirect is relative, carry over the host of the last request
  56715. var currentUrlParts = parseUrl(this._currentUrl);
  56716. var currentHost = currentHostHeader || currentUrlParts.host;
  56717. var currentUrl = /^\w+:/.test(location) ? this._currentUrl :
  56718. url.format(Object.assign(currentUrlParts, { host: currentHost }));
  56719. // Create the redirected request
  56720. var redirectUrl = resolveUrl(location, currentUrl);
  56721. debug$5("redirecting to", redirectUrl.href);
  56722. this._isRedirect = true;
  56723. spreadUrlObject(redirectUrl, this._options);
  56724. // Drop confidential headers when redirecting to a less secure protocol
  56725. // or to a different domain that is not a superdomain
  56726. if (redirectUrl.protocol !== currentUrlParts.protocol &&
  56727. redirectUrl.protocol !== "https:" ||
  56728. redirectUrl.host !== currentHost &&
  56729. !isSubdomain(redirectUrl.host, currentHost)) {
  56730. removeMatchingHeaders(/^(?:(?:proxy-)?authorization|cookie)$/i, this._options.headers);
  56731. }
  56732. // Evaluate the beforeRedirect callback
  56733. if (isFunction(beforeRedirect)) {
  56734. var responseDetails = {
  56735. headers: response.headers,
  56736. statusCode: statusCode,
  56737. };
  56738. var requestDetails = {
  56739. url: currentUrl,
  56740. method: method,
  56741. headers: requestHeaders,
  56742. };
  56743. beforeRedirect(this._options, responseDetails, requestDetails);
  56744. this._sanitizeOptions(this._options);
  56745. }
  56746. // Perform the redirected request
  56747. this._performRequest();
  56748. };
  56749. // Wraps the key/value object of protocols with redirect functionality
  56750. function wrap(protocols) {
  56751. // Default settings
  56752. var exports = {
  56753. maxRedirects: 21,
  56754. maxBodyLength: 10 * 1024 * 1024,
  56755. };
  56756. // Wrap each protocol
  56757. var nativeProtocols = {};
  56758. Object.keys(protocols).forEach(function (scheme) {
  56759. var protocol = scheme + ":";
  56760. var nativeProtocol = nativeProtocols[protocol] = protocols[scheme];
  56761. var wrappedProtocol = exports[scheme] = Object.create(nativeProtocol);
  56762. // Executes a request, following redirects
  56763. function request(input, options, callback) {
  56764. // Parse parameters, ensuring that input is an object
  56765. if (isURL(input)) {
  56766. input = spreadUrlObject(input);
  56767. }
  56768. else if (isString(input)) {
  56769. input = spreadUrlObject(parseUrl(input));
  56770. }
  56771. else {
  56772. callback = options;
  56773. options = validateUrl(input);
  56774. input = { protocol: protocol };
  56775. }
  56776. if (isFunction(options)) {
  56777. callback = options;
  56778. options = null;
  56779. }
  56780. // Set defaults
  56781. options = Object.assign({
  56782. maxRedirects: exports.maxRedirects,
  56783. maxBodyLength: exports.maxBodyLength,
  56784. }, input, options);
  56785. options.nativeProtocols = nativeProtocols;
  56786. if (!isString(options.host) && !isString(options.hostname)) {
  56787. options.hostname = "::1";
  56788. }
  56789. assert.equal(options.protocol, protocol, "protocol mismatch");
  56790. debug$5("options", options);
  56791. return new RedirectableRequest(options, callback);
  56792. }
  56793. // Executes a GET request, following redirects
  56794. function get(input, options, callback) {
  56795. var wrappedRequest = wrappedProtocol.request(input, options, callback);
  56796. wrappedRequest.end();
  56797. return wrappedRequest;
  56798. }
  56799. // Expose the properties on the wrapped protocol
  56800. Object.defineProperties(wrappedProtocol, {
  56801. request: { value: request, configurable: true, enumerable: true, writable: true },
  56802. get: { value: get, configurable: true, enumerable: true, writable: true },
  56803. });
  56804. });
  56805. return exports;
  56806. }
  56807. function noop() { /* empty */ }
  56808. function parseUrl(input) {
  56809. var parsed;
  56810. /* istanbul ignore else */
  56811. if (useNativeURL) {
  56812. parsed = new URL$1(input);
  56813. }
  56814. else {
  56815. // Ensure the URL is valid and absolute
  56816. parsed = validateUrl(url.parse(input));
  56817. if (!isString(parsed.protocol)) {
  56818. throw new InvalidUrlError({ input });
  56819. }
  56820. }
  56821. return parsed;
  56822. }
  56823. function resolveUrl(relative, base) {
  56824. /* istanbul ignore next */
  56825. return useNativeURL ? new URL$1(relative, base) : parseUrl(url.resolve(base, relative));
  56826. }
  56827. function validateUrl(input) {
  56828. if (/^\[/.test(input.hostname) && !/^\[[:0-9a-f]+\]$/i.test(input.hostname)) {
  56829. throw new InvalidUrlError({ input: input.href || input });
  56830. }
  56831. if (/^\[/.test(input.host) && !/^\[[:0-9a-f]+\](:\d+)?$/i.test(input.host)) {
  56832. throw new InvalidUrlError({ input: input.href || input });
  56833. }
  56834. return input;
  56835. }
  56836. function spreadUrlObject(urlObject, target) {
  56837. var spread = target || {};
  56838. for (var key of preservedUrlFields) {
  56839. spread[key] = urlObject[key];
  56840. }
  56841. // Fix IPv6 hostname
  56842. if (spread.hostname.startsWith("[")) {
  56843. spread.hostname = spread.hostname.slice(1, -1);
  56844. }
  56845. // Ensure port is a number
  56846. if (spread.port !== "") {
  56847. spread.port = Number(spread.port);
  56848. }
  56849. // Concatenate path
  56850. spread.path = spread.search ? spread.pathname + spread.search : spread.pathname;
  56851. return spread;
  56852. }
  56853. function removeMatchingHeaders(regex, headers) {
  56854. var lastValue;
  56855. for (var header in headers) {
  56856. if (regex.test(header)) {
  56857. lastValue = headers[header];
  56858. delete headers[header];
  56859. }
  56860. }
  56861. return (lastValue === null || typeof lastValue === "undefined") ?
  56862. undefined : String(lastValue).trim();
  56863. }
  56864. function createErrorType(code, message, baseClass) {
  56865. // Create constructor
  56866. function CustomError(properties) {
  56867. Error.captureStackTrace(this, this.constructor);
  56868. Object.assign(this, properties || {});
  56869. this.code = code;
  56870. this.message = this.cause ? message + ": " + this.cause.message : message;
  56871. }
  56872. // Attach constructor and set default properties
  56873. CustomError.prototype = new (baseClass || Error)();
  56874. Object.defineProperties(CustomError.prototype, {
  56875. constructor: {
  56876. value: CustomError,
  56877. enumerable: false,
  56878. },
  56879. name: {
  56880. value: "Error [" + code + "]",
  56881. enumerable: false,
  56882. },
  56883. });
  56884. return CustomError;
  56885. }
  56886. function destroyRequest(request, error) {
  56887. for (var event of events) {
  56888. request.removeListener(event, eventHandlers[event]);
  56889. }
  56890. request.on("error", noop);
  56891. request.destroy(error);
  56892. }
  56893. function isSubdomain(subdomain, domain) {
  56894. assert(isString(subdomain) && isString(domain));
  56895. var dot = subdomain.length - domain.length - 1;
  56896. return dot > 0 && subdomain[dot] === "." && subdomain.endsWith(domain);
  56897. }
  56898. function isString(value) {
  56899. return typeof value === "string" || value instanceof String;
  56900. }
  56901. function isFunction(value) {
  56902. return typeof value === "function";
  56903. }
  56904. function isBuffer(value) {
  56905. return typeof value === "object" && ("length" in value);
  56906. }
  56907. function isURL(value) {
  56908. return URL$1 && value instanceof URL$1;
  56909. }
  56910. // Exports
  56911. followRedirects$1.exports = wrap({ http: http$1, https: https$1 });
  56912. followRedirects$1.exports.wrap = wrap;
  56913. var followRedirectsExports = followRedirects$1.exports;
  56914. var httpNative = require$$1,
  56915. httpsNative = require$$1$1,
  56916. web_o = webOutgoing,
  56917. common$1 = common$3,
  56918. followRedirects = followRedirectsExports;
  56919. web_o = Object.keys(web_o).map(function(pass) {
  56920. return web_o[pass];
  56921. });
  56922. var nativeAgents = { http: httpNative, https: httpsNative };
  56923. /*!
  56924. * Array of passes.
  56925. *
  56926. * A `pass` is just a function that is executed on `req, res, options`
  56927. * so that you can easily add new checks while still keeping the base
  56928. * flexible.
  56929. */
  56930. var webIncoming = {
  56931. /**
  56932. * Sets `content-length` to '0' if request is of DELETE type.
  56933. *
  56934. * @param {ClientRequest} Req Request object
  56935. * @param {IncomingMessage} Res Response object
  56936. * @param {Object} Options Config object passed to the proxy
  56937. *
  56938. * @api private
  56939. */
  56940. deleteLength: function deleteLength(req, res, options) {
  56941. if((req.method === 'DELETE' || req.method === 'OPTIONS')
  56942. && !req.headers['content-length']) {
  56943. req.headers['content-length'] = '0';
  56944. delete req.headers['transfer-encoding'];
  56945. }
  56946. },
  56947. /**
  56948. * Sets timeout in request socket if it was specified in options.
  56949. *
  56950. * @param {ClientRequest} Req Request object
  56951. * @param {IncomingMessage} Res Response object
  56952. * @param {Object} Options Config object passed to the proxy
  56953. *
  56954. * @api private
  56955. */
  56956. timeout: function timeout(req, res, options) {
  56957. if(options.timeout) {
  56958. req.socket.setTimeout(options.timeout);
  56959. }
  56960. },
  56961. /**
  56962. * Sets `x-forwarded-*` headers if specified in config.
  56963. *
  56964. * @param {ClientRequest} Req Request object
  56965. * @param {IncomingMessage} Res Response object
  56966. * @param {Object} Options Config object passed to the proxy
  56967. *
  56968. * @api private
  56969. */
  56970. XHeaders: function XHeaders(req, res, options) {
  56971. if(!options.xfwd) return;
  56972. var encrypted = req.isSpdy || common$1.hasEncryptedConnection(req);
  56973. var values = {
  56974. for : req.connection.remoteAddress || req.socket.remoteAddress,
  56975. port : common$1.getPort(req),
  56976. proto: encrypted ? 'https' : 'http'
  56977. };
  56978. ['for', 'port', 'proto'].forEach(function(header) {
  56979. req.headers['x-forwarded-' + header] =
  56980. (req.headers['x-forwarded-' + header] || '') +
  56981. (req.headers['x-forwarded-' + header] ? ',' : '') +
  56982. values[header];
  56983. });
  56984. req.headers['x-forwarded-host'] = req.headers['x-forwarded-host'] || req.headers['host'] || '';
  56985. },
  56986. /**
  56987. * Does the actual proxying. If `forward` is enabled fires up
  56988. * a ForwardStream, same happens for ProxyStream. The request
  56989. * just dies otherwise.
  56990. *
  56991. * @param {ClientRequest} Req Request object
  56992. * @param {IncomingMessage} Res Response object
  56993. * @param {Object} Options Config object passed to the proxy
  56994. *
  56995. * @api private
  56996. */
  56997. stream: function stream(req, res, options, _, server, clb) {
  56998. // And we begin!
  56999. server.emit('start', req, res, options.target || options.forward);
  57000. var agents = options.followRedirects ? followRedirects : nativeAgents;
  57001. var http = agents.http;
  57002. var https = agents.https;
  57003. if(options.forward) {
  57004. // If forward enable, so just pipe the request
  57005. var forwardReq = (options.forward.protocol === 'https:' ? https : http).request(
  57006. common$1.setupOutgoing(options.ssl || {}, options, req, 'forward')
  57007. );
  57008. // error handler (e.g. ECONNRESET, ECONNREFUSED)
  57009. // Handle errors on incoming request as well as it makes sense to
  57010. var forwardError = createErrorHandler(forwardReq, options.forward);
  57011. req.on('error', forwardError);
  57012. forwardReq.on('error', forwardError);
  57013. (options.buffer || req).pipe(forwardReq);
  57014. if(!options.target) { return res.end(); }
  57015. }
  57016. // Request initalization
  57017. var proxyReq = (options.target.protocol === 'https:' ? https : http).request(
  57018. common$1.setupOutgoing(options.ssl || {}, options, req)
  57019. );
  57020. // Enable developers to modify the proxyReq before headers are sent
  57021. proxyReq.on('socket', function(socket) {
  57022. if(server && !proxyReq.getHeader('expect')) {
  57023. server.emit('proxyReq', proxyReq, req, res, options);
  57024. }
  57025. });
  57026. // allow outgoing socket to timeout so that we could
  57027. // show an error page at the initial request
  57028. if(options.proxyTimeout) {
  57029. proxyReq.setTimeout(options.proxyTimeout, function() {
  57030. proxyReq.abort();
  57031. });
  57032. }
  57033. // Ensure we abort proxy if request is aborted
  57034. req.on('aborted', function () {
  57035. proxyReq.abort();
  57036. });
  57037. // handle errors in proxy and incoming request, just like for forward proxy
  57038. var proxyError = createErrorHandler(proxyReq, options.target);
  57039. req.on('error', proxyError);
  57040. proxyReq.on('error', proxyError);
  57041. function createErrorHandler(proxyReq, url) {
  57042. return function proxyError(err) {
  57043. if (req.socket.destroyed && err.code === 'ECONNRESET') {
  57044. server.emit('econnreset', err, req, res, url);
  57045. return proxyReq.abort();
  57046. }
  57047. if (clb) {
  57048. clb(err, req, res, url);
  57049. } else {
  57050. server.emit('error', err, req, res, url);
  57051. }
  57052. }
  57053. }
  57054. (options.buffer || req).pipe(proxyReq);
  57055. proxyReq.on('response', function(proxyRes) {
  57056. if(server) { server.emit('proxyRes', proxyRes, req, res); }
  57057. if(!res.headersSent && !options.selfHandleResponse) {
  57058. for(var i=0; i < web_o.length; i++) {
  57059. if(web_o[i](req, res, proxyRes, options)) { break; }
  57060. }
  57061. }
  57062. if (!res.finished) {
  57063. // Allow us to listen when the proxy has completed
  57064. proxyRes.on('end', function () {
  57065. if (server) server.emit('end', req, res, proxyRes);
  57066. });
  57067. // We pipe to the response unless its expected to be handled by the user
  57068. if (!options.selfHandleResponse) proxyRes.pipe(res);
  57069. } else {
  57070. if (server) server.emit('end', req, res, proxyRes);
  57071. }
  57072. });
  57073. }
  57074. };
  57075. var http = require$$1,
  57076. https = require$$1$1,
  57077. common = common$3;
  57078. /*!
  57079. * Array of passes.
  57080. *
  57081. * A `pass` is just a function that is executed on `req, socket, options`
  57082. * so that you can easily add new checks while still keeping the base
  57083. * flexible.
  57084. */
  57085. /*
  57086. * Websockets Passes
  57087. *
  57088. */
  57089. var wsIncoming = {
  57090. /**
  57091. * WebSocket requests must have the `GET` method and
  57092. * the `upgrade:websocket` header
  57093. *
  57094. * @param {ClientRequest} Req Request object
  57095. * @param {Socket} Websocket
  57096. *
  57097. * @api private
  57098. */
  57099. checkMethodAndHeader : function checkMethodAndHeader(req, socket) {
  57100. if (req.method !== 'GET' || !req.headers.upgrade) {
  57101. socket.destroy();
  57102. return true;
  57103. }
  57104. if (req.headers.upgrade.toLowerCase() !== 'websocket') {
  57105. socket.destroy();
  57106. return true;
  57107. }
  57108. },
  57109. /**
  57110. * Sets `x-forwarded-*` headers if specified in config.
  57111. *
  57112. * @param {ClientRequest} Req Request object
  57113. * @param {Socket} Websocket
  57114. * @param {Object} Options Config object passed to the proxy
  57115. *
  57116. * @api private
  57117. */
  57118. XHeaders : function XHeaders(req, socket, options) {
  57119. if(!options.xfwd) return;
  57120. var values = {
  57121. for : req.connection.remoteAddress || req.socket.remoteAddress,
  57122. port : common.getPort(req),
  57123. proto: common.hasEncryptedConnection(req) ? 'wss' : 'ws'
  57124. };
  57125. ['for', 'port', 'proto'].forEach(function(header) {
  57126. req.headers['x-forwarded-' + header] =
  57127. (req.headers['x-forwarded-' + header] || '') +
  57128. (req.headers['x-forwarded-' + header] ? ',' : '') +
  57129. values[header];
  57130. });
  57131. },
  57132. /**
  57133. * Does the actual proxying. Make the request and upgrade it
  57134. * send the Switching Protocols request and pipe the sockets.
  57135. *
  57136. * @param {ClientRequest} Req Request object
  57137. * @param {Socket} Websocket
  57138. * @param {Object} Options Config object passed to the proxy
  57139. *
  57140. * @api private
  57141. */
  57142. stream : function stream(req, socket, options, head, server, clb) {
  57143. var createHttpHeader = function(line, headers) {
  57144. return Object.keys(headers).reduce(function (head, key) {
  57145. var value = headers[key];
  57146. if (!Array.isArray(value)) {
  57147. head.push(key + ': ' + value);
  57148. return head;
  57149. }
  57150. for (var i = 0; i < value.length; i++) {
  57151. head.push(key + ': ' + value[i]);
  57152. }
  57153. return head;
  57154. }, [line])
  57155. .join('\r\n') + '\r\n\r\n';
  57156. };
  57157. common.setupSocket(socket);
  57158. if (head && head.length) socket.unshift(head);
  57159. var proxyReq = (common.isSSL.test(options.target.protocol) ? https : http).request(
  57160. common.setupOutgoing(options.ssl || {}, options, req)
  57161. );
  57162. // Enable developers to modify the proxyReq before headers are sent
  57163. if (server) { server.emit('proxyReqWs', proxyReq, req, socket, options, head); }
  57164. // Error Handler
  57165. proxyReq.on('error', onOutgoingError);
  57166. proxyReq.on('response', function (res) {
  57167. // if upgrade event isn't going to happen, close the socket
  57168. if (!res.upgrade) {
  57169. socket.write(createHttpHeader('HTTP/' + res.httpVersion + ' ' + res.statusCode + ' ' + res.statusMessage, res.headers));
  57170. res.pipe(socket);
  57171. }
  57172. });
  57173. proxyReq.on('upgrade', function(proxyRes, proxySocket, proxyHead) {
  57174. proxySocket.on('error', onOutgoingError);
  57175. // Allow us to listen when the websocket has completed
  57176. proxySocket.on('end', function () {
  57177. server.emit('close', proxyRes, proxySocket, proxyHead);
  57178. });
  57179. // The pipe below will end proxySocket if socket closes cleanly, but not
  57180. // if it errors (eg, vanishes from the net and starts returning
  57181. // EHOSTUNREACH). We need to do that explicitly.
  57182. socket.on('error', function () {
  57183. proxySocket.end();
  57184. });
  57185. common.setupSocket(proxySocket);
  57186. if (proxyHead && proxyHead.length) proxySocket.unshift(proxyHead);
  57187. //
  57188. // Remark: Handle writing the headers to the socket when switching protocols
  57189. // Also handles when a header is an array
  57190. //
  57191. socket.write(createHttpHeader('HTTP/1.1 101 Switching Protocols', proxyRes.headers));
  57192. proxySocket.pipe(socket).pipe(proxySocket);
  57193. server.emit('open', proxySocket);
  57194. server.emit('proxySocket', proxySocket); //DEPRECATED.
  57195. });
  57196. return proxyReq.end(); // XXX: CHECK IF THIS IS THIS CORRECT
  57197. function onOutgoingError(err) {
  57198. if (clb) {
  57199. clb(err, req, socket);
  57200. } else {
  57201. server.emit('error', err, req, socket);
  57202. }
  57203. socket.end();
  57204. }
  57205. }
  57206. };
  57207. (function (module) {
  57208. var httpProxy = module.exports,
  57209. parse_url = require$$0$9.parse,
  57210. EE3 = eventemitter3Exports,
  57211. http = require$$1,
  57212. https = require$$1$1,
  57213. web = webIncoming,
  57214. ws = wsIncoming;
  57215. httpProxy.Server = ProxyServer;
  57216. /**
  57217. * Returns a function that creates the loader for
  57218. * either `ws` or `web`'s passes.
  57219. *
  57220. * Examples:
  57221. *
  57222. * httpProxy.createRightProxy('ws')
  57223. * // => [Function]
  57224. *
  57225. * @param {String} Type Either 'ws' or 'web'
  57226. * @return {Function} Loader Function that when called returns an iterator for the right passes
  57227. *
  57228. * @api private
  57229. */
  57230. function createRightProxy(type) {
  57231. return function(options) {
  57232. return function(req, res /*, [head], [opts] */) {
  57233. var passes = (type === 'ws') ? this.wsPasses : this.webPasses,
  57234. args = [].slice.call(arguments),
  57235. cntr = args.length - 1,
  57236. head, cbl;
  57237. /* optional args parse begin */
  57238. if(typeof args[cntr] === 'function') {
  57239. cbl = args[cntr];
  57240. cntr--;
  57241. }
  57242. var requestOptions = options;
  57243. if(
  57244. !(args[cntr] instanceof Buffer) &&
  57245. args[cntr] !== res
  57246. ) {
  57247. //Copy global options
  57248. requestOptions = Object.assign({}, options);
  57249. //Overwrite with request options
  57250. Object.assign(requestOptions, args[cntr]);
  57251. cntr--;
  57252. }
  57253. if(args[cntr] instanceof Buffer) {
  57254. head = args[cntr];
  57255. }
  57256. /* optional args parse end */
  57257. ['target', 'forward'].forEach(function(e) {
  57258. if (typeof requestOptions[e] === 'string')
  57259. requestOptions[e] = parse_url(requestOptions[e]);
  57260. });
  57261. if (!requestOptions.target && !requestOptions.forward) {
  57262. return this.emit('error', new Error('Must provide a proper URL as target'));
  57263. }
  57264. for(var i=0; i < passes.length; i++) {
  57265. /**
  57266. * Call of passes functions
  57267. * pass(req, res, options, head)
  57268. *
  57269. * In WebSockets case the `res` variable
  57270. * refer to the connection socket
  57271. * pass(req, socket, options, head)
  57272. */
  57273. if(passes[i](req, res, requestOptions, head, this, cbl)) { // passes can return a truthy value to halt the loop
  57274. break;
  57275. }
  57276. }
  57277. };
  57278. };
  57279. }
  57280. httpProxy.createRightProxy = createRightProxy;
  57281. function ProxyServer(options) {
  57282. EE3.call(this);
  57283. options = options || {};
  57284. options.prependPath = options.prependPath === false ? false : true;
  57285. this.web = this.proxyRequest = createRightProxy('web')(options);
  57286. this.ws = this.proxyWebsocketRequest = createRightProxy('ws')(options);
  57287. this.options = options;
  57288. this.webPasses = Object.keys(web).map(function(pass) {
  57289. return web[pass];
  57290. });
  57291. this.wsPasses = Object.keys(ws).map(function(pass) {
  57292. return ws[pass];
  57293. });
  57294. this.on('error', this.onError, this);
  57295. }
  57296. require$$0$5.inherits(ProxyServer, EE3);
  57297. ProxyServer.prototype.onError = function (err) {
  57298. //
  57299. // Remark: Replicate node core behavior using EE3
  57300. // so we force people to handle their own errors
  57301. //
  57302. if(this.listeners('error').length === 1) {
  57303. throw err;
  57304. }
  57305. };
  57306. ProxyServer.prototype.listen = function(port, hostname) {
  57307. var self = this,
  57308. closure = function(req, res) { self.web(req, res); };
  57309. this._server = this.options.ssl ?
  57310. https.createServer(this.options.ssl, closure) :
  57311. http.createServer(closure);
  57312. if(this.options.ws) {
  57313. this._server.on('upgrade', function(req, socket, head) { self.ws(req, socket, head); });
  57314. }
  57315. this._server.listen(port, hostname);
  57316. return this;
  57317. };
  57318. ProxyServer.prototype.close = function(callback) {
  57319. var self = this;
  57320. if (this._server) {
  57321. this._server.close(done);
  57322. }
  57323. // Wrap callback to nullify server after all open connections are closed.
  57324. function done() {
  57325. self._server = null;
  57326. if (callback) {
  57327. callback.apply(null, arguments);
  57328. }
  57329. } };
  57330. ProxyServer.prototype.before = function(type, passName, callback) {
  57331. if (type !== 'ws' && type !== 'web') {
  57332. throw new Error('type must be `web` or `ws`');
  57333. }
  57334. var passes = (type === 'ws') ? this.wsPasses : this.webPasses,
  57335. i = false;
  57336. passes.forEach(function(v, idx) {
  57337. if(v.name === passName) i = idx;
  57338. });
  57339. if(i === false) throw new Error('No such pass');
  57340. passes.splice(i, 0, callback);
  57341. };
  57342. ProxyServer.prototype.after = function(type, passName, callback) {
  57343. if (type !== 'ws' && type !== 'web') {
  57344. throw new Error('type must be `web` or `ws`');
  57345. }
  57346. var passes = (type === 'ws') ? this.wsPasses : this.webPasses,
  57347. i = false;
  57348. passes.forEach(function(v, idx) {
  57349. if(v.name === passName) i = idx;
  57350. });
  57351. if(i === false) throw new Error('No such pass');
  57352. passes.splice(i++, 0, callback);
  57353. };
  57354. } (httpProxy$3));
  57355. var httpProxyExports = httpProxy$3.exports;
  57356. // Use explicit /index.js to help browserify negociation in require '/lib/http-proxy' (!)
  57357. var ProxyServer = httpProxyExports.Server;
  57358. /**
  57359. * Creates the proxy server.
  57360. *
  57361. * Examples:
  57362. *
  57363. * httpProxy.createProxyServer({ .. }, 8000)
  57364. * // => '{ web: [Function], ws: [Function] ... }'
  57365. *
  57366. * @param {Object} Options Config object passed to the proxy
  57367. *
  57368. * @return {Object} Proxy Proxy object with handlers for `ws` and `web` requests
  57369. *
  57370. * @api public
  57371. */
  57372. function createProxyServer(options) {
  57373. /*
  57374. * `options` is needed and it must have the following layout:
  57375. *
  57376. * {
  57377. * target : <url string to be parsed with the url module>
  57378. * forward: <url string to be parsed with the url module>
  57379. * agent : <object to be passed to http(s).request>
  57380. * ssl : <object to be passed to https.createServer()>
  57381. * ws : <true/false, if you want to proxy websockets>
  57382. * xfwd : <true/false, adds x-forward headers>
  57383. * secure : <true/false, verify SSL certificate>
  57384. * toProxy: <true/false, explicitly specify if we are proxying to another proxy>
  57385. * prependPath: <true/false, Default: true - specify whether you want to prepend the target's path to the proxy path>
  57386. * ignorePath: <true/false, Default: false - specify whether you want to ignore the proxy path of the incoming request>
  57387. * localAddress : <Local interface string to bind for outgoing connections>
  57388. * changeOrigin: <true/false, Default: false - changes the origin of the host header to the target URL>
  57389. * preserveHeaderKeyCase: <true/false, Default: false - specify whether you want to keep letter case of response header key >
  57390. * auth : Basic authentication i.e. 'user:password' to compute an Authorization header.
  57391. * hostRewrite: rewrites the location hostname on (201/301/302/307/308) redirects, Default: null.
  57392. * autoRewrite: rewrites the location host/port on (201/301/302/307/308) redirects based on requested host/port. Default: false.
  57393. * protocolRewrite: rewrites the location protocol on (201/301/302/307/308) redirects to 'http' or 'https'. Default: null.
  57394. * }
  57395. *
  57396. * NOTE: `options.ws` and `options.ssl` are optional.
  57397. * `options.target and `options.forward` cannot be
  57398. * both missing
  57399. * }
  57400. */
  57401. return new ProxyServer(options);
  57402. }
  57403. ProxyServer.createProxyServer = createProxyServer;
  57404. ProxyServer.createServer = createProxyServer;
  57405. ProxyServer.createProxy = createProxyServer;
  57406. /**
  57407. * Export the proxy "Server" as the main export.
  57408. */
  57409. var httpProxy$2 = ProxyServer;
  57410. /*!
  57411. * Caron dimonio, con occhi di bragia
  57412. * loro accennando, tutte le raccoglie;
  57413. * batte col remo qualunque s’adagia
  57414. *
  57415. * Charon the demon, with the eyes of glede,
  57416. * Beckoning to them, collects them all together,
  57417. * Beats with his oar whoever lags behind
  57418. *
  57419. * Dante - The Divine Comedy (Canto III)
  57420. */
  57421. var httpProxy = httpProxy$2;
  57422. var httpProxy$1 = /*@__PURE__*/getDefaultExportFromCjs(httpProxy);
  57423. const debug$4 = createDebugger("vite:proxy");
  57424. const rewriteOriginHeader = (proxyReq, options, config) => {
  57425. if (options.rewriteWsOrigin) {
  57426. const { target } = options;
  57427. if (proxyReq.headersSent) {
  57428. config.logger.warn(
  57429. colors$1.yellow(
  57430. `Unable to rewrite Origin header as headers are already sent.`
  57431. )
  57432. );
  57433. return;
  57434. }
  57435. if (proxyReq.getHeader("origin") && target) {
  57436. const changedOrigin = typeof target === "object" ? `${target.protocol}//${target.host}` : target;
  57437. proxyReq.setHeader("origin", changedOrigin);
  57438. }
  57439. }
  57440. };
  57441. function proxyMiddleware(httpServer, options, config) {
  57442. const proxies = {};
  57443. Object.keys(options).forEach((context) => {
  57444. let opts = options[context];
  57445. if (!opts) {
  57446. return;
  57447. }
  57448. if (typeof opts === "string") {
  57449. opts = { target: opts, changeOrigin: true };
  57450. }
  57451. const proxy = httpProxy$1.createProxyServer(opts);
  57452. if (opts.configure) {
  57453. opts.configure(proxy, opts);
  57454. }
  57455. proxy.on("error", (err, req, originalRes) => {
  57456. const res = originalRes;
  57457. if (!res) {
  57458. config.logger.error(
  57459. `${colors$1.red(`http proxy error: ${err.message}`)}
  57460. ${err.stack}`,
  57461. {
  57462. timestamp: true,
  57463. error: err
  57464. }
  57465. );
  57466. } else if ("req" in res) {
  57467. config.logger.error(
  57468. `${colors$1.red(`http proxy error: ${originalRes.req.url}`)}
  57469. ${err.stack}`,
  57470. {
  57471. timestamp: true,
  57472. error: err
  57473. }
  57474. );
  57475. if (!res.headersSent && !res.writableEnded) {
  57476. res.writeHead(500, {
  57477. "Content-Type": "text/plain"
  57478. }).end();
  57479. }
  57480. } else {
  57481. config.logger.error(`${colors$1.red(`ws proxy error:`)}
  57482. ${err.stack}`, {
  57483. timestamp: true,
  57484. error: err
  57485. });
  57486. res.end();
  57487. }
  57488. });
  57489. proxy.on("proxyReqWs", (proxyReq, req, socket, options2, head) => {
  57490. rewriteOriginHeader(proxyReq, options2, config);
  57491. socket.on("error", (err) => {
  57492. config.logger.error(
  57493. `${colors$1.red(`ws proxy socket error:`)}
  57494. ${err.stack}`,
  57495. {
  57496. timestamp: true,
  57497. error: err
  57498. }
  57499. );
  57500. });
  57501. });
  57502. proxy.on("proxyRes", (proxyRes, req, res) => {
  57503. res.on("close", () => {
  57504. if (!res.writableEnded) {
  57505. debug$4?.("destroying proxyRes in proxyRes close event");
  57506. proxyRes.destroy();
  57507. }
  57508. });
  57509. });
  57510. proxies[context] = [proxy, { ...opts }];
  57511. });
  57512. if (httpServer) {
  57513. httpServer.on("upgrade", (req, socket, head) => {
  57514. const url = req.url;
  57515. for (const context in proxies) {
  57516. if (doesProxyContextMatchUrl(context, url)) {
  57517. const [proxy, opts] = proxies[context];
  57518. if (opts.ws || opts.target?.toString().startsWith("ws:") || opts.target?.toString().startsWith("wss:")) {
  57519. if (opts.rewrite) {
  57520. req.url = opts.rewrite(url);
  57521. }
  57522. debug$4?.(`${req.url} -> ws ${opts.target}`);
  57523. proxy.ws(req, socket, head);
  57524. return;
  57525. }
  57526. }
  57527. }
  57528. });
  57529. }
  57530. return function viteProxyMiddleware(req, res, next) {
  57531. const url = req.url;
  57532. for (const context in proxies) {
  57533. if (doesProxyContextMatchUrl(context, url)) {
  57534. const [proxy, opts] = proxies[context];
  57535. const options2 = {};
  57536. if (opts.bypass) {
  57537. const bypassResult = opts.bypass(req, res, opts);
  57538. if (typeof bypassResult === "string") {
  57539. req.url = bypassResult;
  57540. debug$4?.(`bypass: ${req.url} -> ${bypassResult}`);
  57541. return next();
  57542. } else if (bypassResult === false) {
  57543. debug$4?.(`bypass: ${req.url} -> 404`);
  57544. res.statusCode = 404;
  57545. return res.end();
  57546. }
  57547. }
  57548. debug$4?.(`${req.url} -> ${opts.target || opts.forward}`);
  57549. if (opts.rewrite) {
  57550. req.url = opts.rewrite(req.url);
  57551. }
  57552. proxy.web(req, res, options2);
  57553. return;
  57554. }
  57555. }
  57556. next();
  57557. };
  57558. }
  57559. function doesProxyContextMatchUrl(context, url) {
  57560. return context[0] === "^" && new RegExp(context).test(url) || url.startsWith(context);
  57561. }
  57562. const debug$3 = createDebugger("vite:html-fallback");
  57563. function htmlFallbackMiddleware(root, spaFallback, fsUtils = commonFsUtils) {
  57564. return function viteHtmlFallbackMiddleware(req, res, next) {
  57565. if (
  57566. // Only accept GET or HEAD
  57567. req.method !== "GET" && req.method !== "HEAD" || // Exclude default favicon requests
  57568. req.url === "/favicon.ico" || // Require Accept: text/html or */*
  57569. !(req.headers.accept === void 0 || // equivalent to `Accept: */*`
  57570. req.headers.accept === "" || // equivalent to `Accept: */*`
  57571. req.headers.accept.includes("text/html") || req.headers.accept.includes("*/*"))
  57572. ) {
  57573. return next();
  57574. }
  57575. const url = cleanUrl(req.url);
  57576. const pathname = decodeURIComponent(url);
  57577. if (pathname.endsWith(".html")) {
  57578. const filePath = path$n.join(root, pathname);
  57579. if (fsUtils.existsSync(filePath)) {
  57580. debug$3?.(`Rewriting ${req.method} ${req.url} to ${url}`);
  57581. req.url = url;
  57582. return next();
  57583. }
  57584. } else if (pathname[pathname.length - 1] === "/") {
  57585. const filePath = path$n.join(root, pathname, "index.html");
  57586. if (fsUtils.existsSync(filePath)) {
  57587. const newUrl = url + "index.html";
  57588. debug$3?.(`Rewriting ${req.method} ${req.url} to ${newUrl}`);
  57589. req.url = newUrl;
  57590. return next();
  57591. }
  57592. } else {
  57593. const filePath = path$n.join(root, pathname + ".html");
  57594. if (fsUtils.existsSync(filePath)) {
  57595. const newUrl = url + ".html";
  57596. debug$3?.(`Rewriting ${req.method} ${req.url} to ${newUrl}`);
  57597. req.url = newUrl;
  57598. return next();
  57599. }
  57600. }
  57601. if (spaFallback) {
  57602. debug$3?.(`Rewriting ${req.method} ${req.url} to /index.html`);
  57603. req.url = "/index.html";
  57604. }
  57605. next();
  57606. };
  57607. }
  57608. const debug$2 = createDebugger("vite:send", {
  57609. onlyWhenFocused: true
  57610. });
  57611. const alias = {
  57612. js: "text/javascript",
  57613. css: "text/css",
  57614. html: "text/html",
  57615. json: "application/json"
  57616. };
  57617. function send(req, res, content, type, options) {
  57618. const {
  57619. etag = getEtag(content, { weak: true }),
  57620. cacheControl = "no-cache",
  57621. headers,
  57622. map
  57623. } = options;
  57624. if (res.writableEnded) {
  57625. return;
  57626. }
  57627. if (req.headers["if-none-match"] === etag) {
  57628. res.statusCode = 304;
  57629. res.end();
  57630. return;
  57631. }
  57632. res.setHeader("Content-Type", alias[type] || type);
  57633. res.setHeader("Cache-Control", cacheControl);
  57634. res.setHeader("Etag", etag);
  57635. if (headers) {
  57636. for (const name in headers) {
  57637. res.setHeader(name, headers[name]);
  57638. }
  57639. }
  57640. if (map && "version" in map && map.mappings) {
  57641. if (type === "js" || type === "css") {
  57642. content = getCodeWithSourcemap(type, content.toString(), map);
  57643. }
  57644. } else if (type === "js" && (!map || map.mappings !== "")) {
  57645. const code = content.toString();
  57646. if (convertSourceMap.mapFileCommentRegex.test(code)) {
  57647. debug$2?.(`Skipped injecting fallback sourcemap for ${req.url}`);
  57648. } else {
  57649. const urlWithoutTimestamp = removeTimestampQuery(req.url);
  57650. const ms = new MagicString(code);
  57651. content = getCodeWithSourcemap(
  57652. type,
  57653. code,
  57654. ms.generateMap({
  57655. source: path$n.basename(urlWithoutTimestamp),
  57656. hires: "boundary",
  57657. includeContent: true
  57658. })
  57659. );
  57660. }
  57661. }
  57662. res.statusCode = 200;
  57663. res.end(content);
  57664. return;
  57665. }
  57666. const debugCache = createDebugger("vite:cache");
  57667. const knownIgnoreList = /* @__PURE__ */ new Set(["/", "/favicon.ico"]);
  57668. const trailingQuerySeparatorsRE = /[?&]+$/;
  57669. const urlRE = /[?&]url\b/;
  57670. const rawRE = /[?&]raw\b/;
  57671. const inlineRE = /[?&]inline\b/;
  57672. const svgRE = /\.svg\b/;
  57673. function deniedServingAccessForTransform(url, server, res, next) {
  57674. if (rawRE.test(url) || urlRE.test(url) || inlineRE.test(url) || svgRE.test(url)) {
  57675. const servingAccessResult = checkServingAccess(url, server);
  57676. if (servingAccessResult === "denied") {
  57677. respondWithAccessDenied(url, server, res);
  57678. return true;
  57679. }
  57680. if (servingAccessResult === "fallback") {
  57681. next();
  57682. return true;
  57683. }
  57684. }
  57685. return false;
  57686. }
  57687. function cachedTransformMiddleware(server) {
  57688. return function viteCachedTransformMiddleware(req, res, next) {
  57689. const ifNoneMatch = req.headers["if-none-match"];
  57690. if (ifNoneMatch) {
  57691. const moduleByEtag = server.moduleGraph.getModuleByEtag(ifNoneMatch);
  57692. if (moduleByEtag?.transformResult?.etag === ifNoneMatch && moduleByEtag?.url === req.url) {
  57693. const maybeMixedEtag = isCSSRequest(req.url);
  57694. if (!maybeMixedEtag) {
  57695. debugCache?.(`[304] ${prettifyUrl(req.url, server.config.root)}`);
  57696. res.statusCode = 304;
  57697. return res.end();
  57698. }
  57699. }
  57700. }
  57701. next();
  57702. };
  57703. }
  57704. function transformMiddleware(server) {
  57705. const { root, publicDir } = server.config;
  57706. const publicDirInRoot = publicDir.startsWith(withTrailingSlash(root));
  57707. const publicPath = `${publicDir.slice(root.length)}/`;
  57708. return async function viteTransformMiddleware(req, res, next) {
  57709. if (req.method !== "GET" || knownIgnoreList.has(req.url)) {
  57710. return next();
  57711. }
  57712. let url;
  57713. try {
  57714. url = decodeURI(removeTimestampQuery(req.url)).replace(
  57715. NULL_BYTE_PLACEHOLDER,
  57716. "\0"
  57717. );
  57718. } catch (e) {
  57719. return next(e);
  57720. }
  57721. const withoutQuery = cleanUrl(url);
  57722. try {
  57723. const isSourceMap = withoutQuery.endsWith(".map");
  57724. if (isSourceMap) {
  57725. const depsOptimizer = getDepsOptimizer(server.config, false);
  57726. if (depsOptimizer?.isOptimizedDepUrl(url)) {
  57727. const sourcemapPath = url.startsWith(FS_PREFIX) ? fsPathFromId(url) : normalizePath$3(path$n.resolve(server.config.root, url.slice(1)));
  57728. try {
  57729. const map = JSON.parse(
  57730. await fsp.readFile(sourcemapPath, "utf-8")
  57731. );
  57732. applySourcemapIgnoreList(
  57733. map,
  57734. sourcemapPath,
  57735. server.config.server.sourcemapIgnoreList,
  57736. server.config.logger
  57737. );
  57738. return send(req, res, JSON.stringify(map), "json", {
  57739. headers: server.config.server.headers
  57740. });
  57741. } catch (e) {
  57742. const dummySourceMap = {
  57743. version: 3,
  57744. file: sourcemapPath.replace(/\.map$/, ""),
  57745. sources: [],
  57746. sourcesContent: [],
  57747. names: [],
  57748. mappings: ";;;;;;;;;"
  57749. };
  57750. return send(req, res, JSON.stringify(dummySourceMap), "json", {
  57751. cacheControl: "no-cache",
  57752. headers: server.config.server.headers
  57753. });
  57754. }
  57755. } else {
  57756. const originalUrl = url.replace(/\.map($|\?)/, "$1");
  57757. const map = (await server.moduleGraph.getModuleByUrl(originalUrl, false))?.transformResult?.map;
  57758. if (map) {
  57759. return send(req, res, JSON.stringify(map), "json", {
  57760. headers: server.config.server.headers
  57761. });
  57762. } else {
  57763. return next();
  57764. }
  57765. }
  57766. }
  57767. if (publicDirInRoot && url.startsWith(publicPath)) {
  57768. warnAboutExplicitPublicPathInUrl(url);
  57769. }
  57770. const urlWithoutTrailingQuerySeparators = url.replace(
  57771. trailingQuerySeparatorsRE,
  57772. ""
  57773. );
  57774. if (deniedServingAccessForTransform(
  57775. urlWithoutTrailingQuerySeparators,
  57776. server,
  57777. res,
  57778. next
  57779. )) {
  57780. return;
  57781. }
  57782. if (isJSRequest(url) || isImportRequest(url) || isCSSRequest(url) || isHTMLProxy(url)) {
  57783. url = removeImportQuery(url);
  57784. url = unwrapId$1(url);
  57785. if (isCSSRequest(url)) {
  57786. if (req.headers.accept?.includes("text/css") && !isDirectRequest(url)) {
  57787. url = injectQuery(url, "direct");
  57788. }
  57789. const ifNoneMatch = req.headers["if-none-match"];
  57790. if (ifNoneMatch && (await server.moduleGraph.getModuleByUrl(url, false))?.transformResult?.etag === ifNoneMatch) {
  57791. debugCache?.(`[304] ${prettifyUrl(url, server.config.root)}`);
  57792. res.statusCode = 304;
  57793. return res.end();
  57794. }
  57795. }
  57796. const result = await transformRequest(url, server, {
  57797. html: req.headers.accept?.includes("text/html"),
  57798. allowId(id) {
  57799. return !deniedServingAccessForTransform(id, server, res, next);
  57800. }
  57801. });
  57802. if (result) {
  57803. const depsOptimizer = getDepsOptimizer(server.config, false);
  57804. const type = isDirectCSSRequest(url) ? "css" : "js";
  57805. const isDep = DEP_VERSION_RE.test(url) || depsOptimizer?.isOptimizedDepUrl(url);
  57806. return send(req, res, result.code, type, {
  57807. etag: result.etag,
  57808. // allow browser to cache npm deps!
  57809. cacheControl: isDep ? "max-age=31536000,immutable" : "no-cache",
  57810. headers: server.config.server.headers,
  57811. map: result.map
  57812. });
  57813. }
  57814. }
  57815. } catch (e) {
  57816. if (e?.code === ERR_OPTIMIZE_DEPS_PROCESSING_ERROR) {
  57817. if (!res.writableEnded) {
  57818. res.statusCode = 504;
  57819. res.statusMessage = "Optimize Deps Processing Error";
  57820. res.end();
  57821. }
  57822. server.config.logger.error(e.message);
  57823. return;
  57824. }
  57825. if (e?.code === ERR_OUTDATED_OPTIMIZED_DEP) {
  57826. if (!res.writableEnded) {
  57827. res.statusCode = 504;
  57828. res.statusMessage = "Outdated Optimize Dep";
  57829. res.end();
  57830. }
  57831. return;
  57832. }
  57833. if (e?.code === ERR_CLOSED_SERVER) {
  57834. if (!res.writableEnded) {
  57835. res.statusCode = 504;
  57836. res.statusMessage = "Outdated Request";
  57837. res.end();
  57838. }
  57839. return;
  57840. }
  57841. if (e?.code === ERR_FILE_NOT_FOUND_IN_OPTIMIZED_DEP_DIR) {
  57842. if (!res.writableEnded) {
  57843. res.statusCode = 404;
  57844. res.end();
  57845. }
  57846. server.config.logger.warn(colors$1.yellow(e.message));
  57847. return;
  57848. }
  57849. if (e?.code === ERR_LOAD_URL) {
  57850. return next();
  57851. }
  57852. if (e?.code === ERR_DENIED_ID) {
  57853. return;
  57854. }
  57855. return next(e);
  57856. }
  57857. next();
  57858. };
  57859. function warnAboutExplicitPublicPathInUrl(url) {
  57860. let warning;
  57861. if (isImportRequest(url)) {
  57862. const rawUrl = removeImportQuery(url);
  57863. if (urlRE.test(url)) {
  57864. warning = `Assets in the public directory are served at the root path.
  57865. Instead of ${colors$1.cyan(rawUrl)}, use ${colors$1.cyan(
  57866. rawUrl.replace(publicPath, "/")
  57867. )}.`;
  57868. } else {
  57869. warning = `Assets in public directory cannot be imported from JavaScript.
  57870. If you intend to import that asset, put the file in the src directory, and use ${colors$1.cyan(
  57871. rawUrl.replace(publicPath, "/src/")
  57872. )} instead of ${colors$1.cyan(rawUrl)}.
  57873. If you intend to use the URL of that asset, use ${colors$1.cyan(
  57874. injectQuery(rawUrl.replace(publicPath, "/"), "url")
  57875. )}.`;
  57876. }
  57877. } else {
  57878. warning = `Files in the public directory are served at the root path.
  57879. Instead of ${colors$1.cyan(url)}, use ${colors$1.cyan(
  57880. url.replace(publicPath, "/")
  57881. )}.`;
  57882. }
  57883. server.config.logger.warn(colors$1.yellow(warning));
  57884. }
  57885. }
  57886. function createDevHtmlTransformFn(config) {
  57887. const [preHooks, normalHooks, postHooks] = resolveHtmlTransforms(
  57888. config.plugins,
  57889. config.logger
  57890. );
  57891. const transformHooks = [
  57892. preImportMapHook(config),
  57893. injectCspNonceMetaTagHook(config),
  57894. ...preHooks,
  57895. htmlEnvHook(config),
  57896. devHtmlHook,
  57897. ...normalHooks,
  57898. ...postHooks,
  57899. injectNonceAttributeTagHook(config),
  57900. postImportMapHook()
  57901. ];
  57902. return (server, url, html, originalUrl) => {
  57903. return applyHtmlTransforms(html, transformHooks, {
  57904. path: url,
  57905. filename: getHtmlFilename(url, server),
  57906. server,
  57907. originalUrl
  57908. });
  57909. };
  57910. }
  57911. function getHtmlFilename(url, server) {
  57912. if (url.startsWith(FS_PREFIX)) {
  57913. return decodeURIComponent(fsPathFromId(url));
  57914. } else {
  57915. return decodeURIComponent(
  57916. normalizePath$3(path$n.join(server.config.root, url.slice(1)))
  57917. );
  57918. }
  57919. }
  57920. function shouldPreTransform(url, config) {
  57921. return !checkPublicFile(url, config) && (isJSRequest(url) || isCSSRequest(url));
  57922. }
  57923. const wordCharRE = /\w/;
  57924. function isBareRelative(url) {
  57925. return wordCharRE.test(url[0]) && !url.includes(":");
  57926. }
  57927. const isSrcSet = (attr) => attr.name === "srcset" && attr.prefix === void 0;
  57928. const processNodeUrl = (url, useSrcSetReplacer, config, htmlPath, originalUrl, server, isClassicScriptLink) => {
  57929. const replacer = (url2) => {
  57930. if (server?.moduleGraph) {
  57931. const mod = server.moduleGraph.urlToModuleMap.get(url2);
  57932. if (mod && mod.lastHMRTimestamp > 0) {
  57933. url2 = injectQuery(url2, `t=${mod.lastHMRTimestamp}`);
  57934. }
  57935. }
  57936. if (url2[0] === "/" && url2[1] !== "/" || // #3230 if some request url (localhost:3000/a/b) return to fallback html, the relative assets
  57937. // path will add `/a/` prefix, it will caused 404.
  57938. //
  57939. // skip if url contains `:` as it implies a url protocol or Windows path that we don't want to replace.
  57940. //
  57941. // rewrite `./index.js` -> `localhost:5173/a/index.js`.
  57942. // rewrite `../index.js` -> `localhost:5173/index.js`.
  57943. // rewrite `relative/index.js` -> `localhost:5173/a/relative/index.js`.
  57944. (url2[0] === "." || isBareRelative(url2)) && originalUrl && originalUrl !== "/" && htmlPath === "/index.html") {
  57945. url2 = path$n.posix.join(config.base, url2);
  57946. }
  57947. if (server && !isClassicScriptLink && shouldPreTransform(url2, config)) {
  57948. let preTransformUrl;
  57949. if (url2[0] === "/" && url2[1] !== "/") {
  57950. preTransformUrl = url2;
  57951. } else if (url2[0] === "." || isBareRelative(url2)) {
  57952. preTransformUrl = path$n.posix.join(
  57953. config.base,
  57954. path$n.posix.dirname(htmlPath),
  57955. url2
  57956. );
  57957. }
  57958. if (preTransformUrl) {
  57959. try {
  57960. preTransformUrl = decodeURI(preTransformUrl);
  57961. } catch (err) {
  57962. return url2;
  57963. }
  57964. preTransformRequest(server, preTransformUrl, config.decodedBase);
  57965. }
  57966. }
  57967. return url2;
  57968. };
  57969. const processedUrl = useSrcSetReplacer ? processSrcSetSync(url, ({ url: url2 }) => replacer(url2)) : replacer(url);
  57970. return processedUrl;
  57971. };
  57972. const devHtmlHook = async (html, { path: htmlPath, filename, server, originalUrl }) => {
  57973. const { config, moduleGraph, watcher } = server;
  57974. const base = config.base || "/";
  57975. const decodedBase = config.decodedBase || "/";
  57976. let proxyModulePath;
  57977. let proxyModuleUrl;
  57978. const trailingSlash = htmlPath.endsWith("/");
  57979. if (!trailingSlash && getFsUtils(config).existsSync(filename)) {
  57980. proxyModulePath = htmlPath;
  57981. proxyModuleUrl = proxyModulePath;
  57982. } else {
  57983. const validPath = `${htmlPath}${trailingSlash ? "index.html" : ""}`;
  57984. proxyModulePath = `\0${validPath}`;
  57985. proxyModuleUrl = wrapId$1(proxyModulePath);
  57986. }
  57987. proxyModuleUrl = joinUrlSegments(decodedBase, proxyModuleUrl);
  57988. const s = new MagicString(html);
  57989. let inlineModuleIndex = -1;
  57990. const proxyCacheUrl = decodeURI(
  57991. cleanUrl(proxyModulePath).replace(normalizePath$3(config.root), "")
  57992. );
  57993. const styleUrl = [];
  57994. const inlineStyles = [];
  57995. const addInlineModule = (node, ext) => {
  57996. inlineModuleIndex++;
  57997. const contentNode = node.childNodes[0];
  57998. const code = contentNode.value;
  57999. let map;
  58000. if (proxyModulePath[0] !== "\0") {
  58001. map = new MagicString(html).snip(
  58002. contentNode.sourceCodeLocation.startOffset,
  58003. contentNode.sourceCodeLocation.endOffset
  58004. ).generateMap({ hires: "boundary" });
  58005. map.sources = [filename];
  58006. map.file = filename;
  58007. }
  58008. addToHTMLProxyCache(config, proxyCacheUrl, inlineModuleIndex, { code, map });
  58009. const modulePath = `${proxyModuleUrl}?html-proxy&index=${inlineModuleIndex}.${ext}`;
  58010. const module = server?.moduleGraph.getModuleById(modulePath);
  58011. if (module) {
  58012. server?.moduleGraph.invalidateModule(module);
  58013. }
  58014. s.update(
  58015. node.sourceCodeLocation.startOffset,
  58016. node.sourceCodeLocation.endOffset,
  58017. `<script type="module" src="${modulePath}"><\/script>`
  58018. );
  58019. preTransformRequest(server, modulePath, decodedBase);
  58020. };
  58021. await traverseHtml(html, filename, (node) => {
  58022. if (!nodeIsElement(node)) {
  58023. return;
  58024. }
  58025. if (node.nodeName === "script") {
  58026. const { src, sourceCodeLocation, isModule } = getScriptInfo(node);
  58027. if (src) {
  58028. const processedUrl = processNodeUrl(
  58029. src.value,
  58030. isSrcSet(src),
  58031. config,
  58032. htmlPath,
  58033. originalUrl,
  58034. server,
  58035. !isModule
  58036. );
  58037. if (processedUrl !== src.value) {
  58038. overwriteAttrValue(s, sourceCodeLocation, processedUrl);
  58039. }
  58040. } else if (isModule && node.childNodes.length) {
  58041. addInlineModule(node, "js");
  58042. } else if (node.childNodes.length) {
  58043. const scriptNode = node.childNodes[node.childNodes.length - 1];
  58044. for (const {
  58045. url,
  58046. start,
  58047. end
  58048. } of extractImportExpressionFromClassicScript(scriptNode)) {
  58049. const processedUrl = processNodeUrl(
  58050. url,
  58051. false,
  58052. config,
  58053. htmlPath,
  58054. originalUrl
  58055. );
  58056. if (processedUrl !== url) {
  58057. s.update(start, end, processedUrl);
  58058. }
  58059. }
  58060. }
  58061. }
  58062. const inlineStyle = findNeedTransformStyleAttribute(node);
  58063. if (inlineStyle) {
  58064. inlineModuleIndex++;
  58065. inlineStyles.push({
  58066. index: inlineModuleIndex,
  58067. location: inlineStyle.location,
  58068. code: inlineStyle.attr.value
  58069. });
  58070. }
  58071. if (node.nodeName === "style" && node.childNodes.length) {
  58072. const children = node.childNodes[0];
  58073. styleUrl.push({
  58074. start: children.sourceCodeLocation.startOffset,
  58075. end: children.sourceCodeLocation.endOffset,
  58076. code: children.value
  58077. });
  58078. }
  58079. const assetAttrs = assetAttrsConfig[node.nodeName];
  58080. if (assetAttrs) {
  58081. for (const p of node.attrs) {
  58082. const attrKey = getAttrKey(p);
  58083. if (p.value && assetAttrs.includes(attrKey)) {
  58084. const processedUrl = processNodeUrl(
  58085. p.value,
  58086. isSrcSet(p),
  58087. config,
  58088. htmlPath,
  58089. originalUrl
  58090. );
  58091. if (processedUrl !== p.value) {
  58092. overwriteAttrValue(
  58093. s,
  58094. node.sourceCodeLocation.attrs[attrKey],
  58095. processedUrl
  58096. );
  58097. }
  58098. }
  58099. }
  58100. }
  58101. });
  58102. await Promise.all([
  58103. ...styleUrl.map(async ({ start, end, code }, index) => {
  58104. const url = `${proxyModulePath}?html-proxy&direct&index=${index}.css`;
  58105. const mod = await moduleGraph.ensureEntryFromUrl(url, false);
  58106. ensureWatchedFile(watcher, mod.file, config.root);
  58107. const result = await server.pluginContainer.transform(code, mod.id);
  58108. let content = "";
  58109. if (result) {
  58110. if (result.map && "version" in result.map) {
  58111. if (result.map.mappings) {
  58112. await injectSourcesContent(
  58113. result.map,
  58114. proxyModulePath,
  58115. config.logger
  58116. );
  58117. }
  58118. content = getCodeWithSourcemap("css", result.code, result.map);
  58119. } else {
  58120. content = result.code;
  58121. }
  58122. }
  58123. s.overwrite(start, end, content);
  58124. }),
  58125. ...inlineStyles.map(async ({ index, location, code }) => {
  58126. const url = `${proxyModulePath}?html-proxy&inline-css&style-attr&index=${index}.css`;
  58127. const mod = await moduleGraph.ensureEntryFromUrl(url, false);
  58128. ensureWatchedFile(watcher, mod.file, config.root);
  58129. await server?.pluginContainer.transform(code, mod.id);
  58130. const hash = getHash(cleanUrl(mod.id));
  58131. const result = htmlProxyResult.get(`${hash}_${index}`);
  58132. overwriteAttrValue(s, location, result ?? "");
  58133. })
  58134. ]);
  58135. html = s.toString();
  58136. return {
  58137. html,
  58138. tags: [
  58139. {
  58140. tag: "script",
  58141. attrs: {
  58142. type: "module",
  58143. src: path$n.posix.join(base, CLIENT_PUBLIC_PATH)
  58144. },
  58145. injectTo: "head-prepend"
  58146. }
  58147. ]
  58148. };
  58149. };
  58150. function indexHtmlMiddleware(root, server) {
  58151. const isDev = isDevServer(server);
  58152. const fsUtils = getFsUtils(server.config);
  58153. return async function viteIndexHtmlMiddleware(req, res, next) {
  58154. if (res.writableEnded) {
  58155. return next();
  58156. }
  58157. const url = req.url && cleanUrl(req.url);
  58158. if (url?.endsWith(".html") && req.headers["sec-fetch-dest"] !== "script") {
  58159. let filePath;
  58160. if (isDev && url.startsWith(FS_PREFIX)) {
  58161. filePath = decodeURIComponent(fsPathFromId(url));
  58162. } else {
  58163. filePath = normalizePath$3(
  58164. path$n.resolve(path$n.join(root, decodeURIComponent(url)))
  58165. );
  58166. }
  58167. if (isDev) {
  58168. const servingAccessResult = checkLoadingAccess(server, filePath);
  58169. if (servingAccessResult === "denied") {
  58170. return respondWithAccessDenied(filePath, server, res);
  58171. }
  58172. if (servingAccessResult === "fallback") {
  58173. return next();
  58174. }
  58175. } else {
  58176. if (!isParentDirectory(root, filePath)) {
  58177. return next();
  58178. }
  58179. }
  58180. if (fsUtils.existsSync(filePath)) {
  58181. const headers = isDev ? server.config.server.headers : server.config.preview.headers;
  58182. try {
  58183. let html = await fsp.readFile(filePath, "utf-8");
  58184. if (isDev) {
  58185. html = await server.transformIndexHtml(url, html, req.originalUrl);
  58186. }
  58187. return send(req, res, html, "html", { headers });
  58188. } catch (e) {
  58189. return next(e);
  58190. }
  58191. }
  58192. }
  58193. next();
  58194. };
  58195. }
  58196. function preTransformRequest(server, decodedUrl, decodedBase) {
  58197. if (!server.config.server.preTransformRequests) return;
  58198. decodedUrl = unwrapId$1(stripBase(decodedUrl, decodedBase));
  58199. server.warmupRequest(decodedUrl);
  58200. }
  58201. const logTime = createDebugger("vite:time");
  58202. function timeMiddleware(root) {
  58203. return function viteTimeMiddleware(req, res, next) {
  58204. const start = performance$1.now();
  58205. const end = res.end;
  58206. res.end = (...args) => {
  58207. logTime?.(`${timeFrom(start)} ${prettifyUrl(req.url, root)}`);
  58208. return end.call(res, ...args);
  58209. };
  58210. next();
  58211. };
  58212. }
  58213. class ModuleNode {
  58214. /**
  58215. * Public served url path, starts with /
  58216. */
  58217. url;
  58218. /**
  58219. * Resolved file system path + query
  58220. */
  58221. id = null;
  58222. file = null;
  58223. type;
  58224. info;
  58225. meta;
  58226. importers = /* @__PURE__ */ new Set();
  58227. clientImportedModules = /* @__PURE__ */ new Set();
  58228. ssrImportedModules = /* @__PURE__ */ new Set();
  58229. acceptedHmrDeps = /* @__PURE__ */ new Set();
  58230. acceptedHmrExports = null;
  58231. importedBindings = null;
  58232. isSelfAccepting;
  58233. transformResult = null;
  58234. ssrTransformResult = null;
  58235. ssrModule = null;
  58236. ssrError = null;
  58237. lastHMRTimestamp = 0;
  58238. /**
  58239. * `import.meta.hot.invalidate` is called by the client.
  58240. * If there's multiple clients, multiple `invalidate` request is received.
  58241. * This property is used to dedupe those request to avoid multiple updates happening.
  58242. * @internal
  58243. */
  58244. lastHMRInvalidationReceived = false;
  58245. lastInvalidationTimestamp = 0;
  58246. /**
  58247. * If the module only needs to update its imports timestamp (e.g. within an HMR chain),
  58248. * it is considered soft-invalidated. In this state, its `transformResult` should exist,
  58249. * and the next `transformRequest` for this module will replace the timestamps.
  58250. *
  58251. * By default the value is `undefined` if it's not soft/hard-invalidated. If it gets
  58252. * soft-invalidated, this will contain the previous `transformResult` value. If it gets
  58253. * hard-invalidated, this will be set to `'HARD_INVALIDATED'`.
  58254. * @internal
  58255. */
  58256. invalidationState;
  58257. /**
  58258. * @internal
  58259. */
  58260. ssrInvalidationState;
  58261. /**
  58262. * The module urls that are statically imported in the code. This information is separated
  58263. * out from `importedModules` as only importers that statically import the module can be
  58264. * soft invalidated. Other imports (e.g. watched files) needs the importer to be hard invalidated.
  58265. * @internal
  58266. */
  58267. staticImportedUrls;
  58268. /**
  58269. * @param setIsSelfAccepting - set `false` to set `isSelfAccepting` later. e.g. #7870
  58270. */
  58271. constructor(url, setIsSelfAccepting = true) {
  58272. this.url = url;
  58273. this.type = isDirectCSSRequest(url) ? "css" : "js";
  58274. if (setIsSelfAccepting) {
  58275. this.isSelfAccepting = false;
  58276. }
  58277. }
  58278. get importedModules() {
  58279. const importedModules = new Set(this.clientImportedModules);
  58280. for (const module of this.ssrImportedModules) {
  58281. importedModules.add(module);
  58282. }
  58283. return importedModules;
  58284. }
  58285. }
  58286. class ModuleGraph {
  58287. constructor(resolveId) {
  58288. this.resolveId = resolveId;
  58289. }
  58290. urlToModuleMap = /* @__PURE__ */ new Map();
  58291. idToModuleMap = /* @__PURE__ */ new Map();
  58292. etagToModuleMap = /* @__PURE__ */ new Map();
  58293. // a single file may corresponds to multiple modules with different queries
  58294. fileToModulesMap = /* @__PURE__ */ new Map();
  58295. safeModulesPath = /* @__PURE__ */ new Set();
  58296. /**
  58297. * @internal
  58298. */
  58299. _unresolvedUrlToModuleMap = /* @__PURE__ */ new Map();
  58300. /**
  58301. * @internal
  58302. */
  58303. _ssrUnresolvedUrlToModuleMap = /* @__PURE__ */ new Map();
  58304. /** @internal */
  58305. _hasResolveFailedErrorModules = /* @__PURE__ */ new Set();
  58306. async getModuleByUrl(rawUrl, ssr) {
  58307. rawUrl = removeImportQuery(removeTimestampQuery(rawUrl));
  58308. const mod = this._getUnresolvedUrlToModule(rawUrl, ssr);
  58309. if (mod) {
  58310. return mod;
  58311. }
  58312. const [url] = await this._resolveUrl(rawUrl, ssr);
  58313. return this.urlToModuleMap.get(url);
  58314. }
  58315. getModuleById(id) {
  58316. return this.idToModuleMap.get(removeTimestampQuery(id));
  58317. }
  58318. getModulesByFile(file) {
  58319. return this.fileToModulesMap.get(file);
  58320. }
  58321. onFileChange(file) {
  58322. const mods = this.getModulesByFile(file);
  58323. if (mods) {
  58324. const seen = /* @__PURE__ */ new Set();
  58325. mods.forEach((mod) => {
  58326. this.invalidateModule(mod, seen);
  58327. });
  58328. }
  58329. }
  58330. onFileDelete(file) {
  58331. const mods = this.getModulesByFile(file);
  58332. if (mods) {
  58333. mods.forEach((mod) => {
  58334. mod.importedModules.forEach((importedMod) => {
  58335. importedMod.importers.delete(mod);
  58336. });
  58337. });
  58338. }
  58339. }
  58340. invalidateModule(mod, seen = /* @__PURE__ */ new Set(), timestamp = Date.now(), isHmr = false, softInvalidate = false) {
  58341. const prevInvalidationState = mod.invalidationState;
  58342. const prevSsrInvalidationState = mod.ssrInvalidationState;
  58343. if (softInvalidate) {
  58344. mod.invalidationState ??= mod.transformResult ?? "HARD_INVALIDATED";
  58345. mod.ssrInvalidationState ??= mod.ssrTransformResult ?? "HARD_INVALIDATED";
  58346. } else {
  58347. mod.invalidationState = "HARD_INVALIDATED";
  58348. mod.ssrInvalidationState = "HARD_INVALIDATED";
  58349. }
  58350. if (seen.has(mod) && prevInvalidationState === mod.invalidationState && prevSsrInvalidationState === mod.ssrInvalidationState) {
  58351. return;
  58352. }
  58353. seen.add(mod);
  58354. if (isHmr) {
  58355. mod.lastHMRTimestamp = timestamp;
  58356. mod.lastHMRInvalidationReceived = false;
  58357. } else {
  58358. mod.lastInvalidationTimestamp = timestamp;
  58359. }
  58360. const etag = mod.transformResult?.etag;
  58361. if (etag) this.etagToModuleMap.delete(etag);
  58362. mod.transformResult = null;
  58363. mod.ssrTransformResult = null;
  58364. mod.ssrModule = null;
  58365. mod.ssrError = null;
  58366. mod.importers.forEach((importer) => {
  58367. if (!importer.acceptedHmrDeps.has(mod)) {
  58368. const shouldSoftInvalidateImporter = (importer.staticImportedUrls?.has(mod.url) || softInvalidate) && importer.type !== "css";
  58369. this.invalidateModule(
  58370. importer,
  58371. seen,
  58372. timestamp,
  58373. isHmr,
  58374. shouldSoftInvalidateImporter
  58375. );
  58376. }
  58377. });
  58378. this._hasResolveFailedErrorModules.delete(mod);
  58379. }
  58380. invalidateAll() {
  58381. const timestamp = Date.now();
  58382. const seen = /* @__PURE__ */ new Set();
  58383. this.idToModuleMap.forEach((mod) => {
  58384. this.invalidateModule(mod, seen, timestamp);
  58385. });
  58386. }
  58387. /**
  58388. * Update the module graph based on a module's updated imports information
  58389. * If there are dependencies that no longer have any importers, they are
  58390. * returned as a Set.
  58391. *
  58392. * @param staticImportedUrls Subset of `importedModules` where they're statically imported in code.
  58393. * This is only used for soft invalidations so `undefined` is fine but may cause more runtime processing.
  58394. */
  58395. async updateModuleInfo(mod, importedModules, importedBindings, acceptedModules, acceptedExports, isSelfAccepting, ssr, staticImportedUrls) {
  58396. mod.isSelfAccepting = isSelfAccepting;
  58397. const prevImports = ssr ? mod.ssrImportedModules : mod.clientImportedModules;
  58398. let noLongerImported;
  58399. let resolvePromises = [];
  58400. let resolveResults = new Array(importedModules.size);
  58401. let index = 0;
  58402. for (const imported of importedModules) {
  58403. const nextIndex = index++;
  58404. if (typeof imported === "string") {
  58405. resolvePromises.push(
  58406. this.ensureEntryFromUrl(imported, ssr).then((dep) => {
  58407. dep.importers.add(mod);
  58408. resolveResults[nextIndex] = dep;
  58409. })
  58410. );
  58411. } else {
  58412. imported.importers.add(mod);
  58413. resolveResults[nextIndex] = imported;
  58414. }
  58415. }
  58416. if (resolvePromises.length) {
  58417. await Promise.all(resolvePromises);
  58418. }
  58419. const nextImports = new Set(resolveResults);
  58420. if (ssr) {
  58421. mod.ssrImportedModules = nextImports;
  58422. } else {
  58423. mod.clientImportedModules = nextImports;
  58424. }
  58425. prevImports.forEach((dep) => {
  58426. if (!mod.clientImportedModules.has(dep) && !mod.ssrImportedModules.has(dep)) {
  58427. dep.importers.delete(mod);
  58428. if (!dep.importers.size) {
  58429. (noLongerImported || (noLongerImported = /* @__PURE__ */ new Set())).add(dep);
  58430. }
  58431. }
  58432. });
  58433. resolvePromises = [];
  58434. resolveResults = new Array(acceptedModules.size);
  58435. index = 0;
  58436. for (const accepted of acceptedModules) {
  58437. const nextIndex = index++;
  58438. if (typeof accepted === "string") {
  58439. resolvePromises.push(
  58440. this.ensureEntryFromUrl(accepted, ssr).then((dep) => {
  58441. resolveResults[nextIndex] = dep;
  58442. })
  58443. );
  58444. } else {
  58445. resolveResults[nextIndex] = accepted;
  58446. }
  58447. }
  58448. if (resolvePromises.length) {
  58449. await Promise.all(resolvePromises);
  58450. }
  58451. mod.acceptedHmrDeps = new Set(resolveResults);
  58452. mod.staticImportedUrls = staticImportedUrls;
  58453. mod.acceptedHmrExports = acceptedExports;
  58454. mod.importedBindings = importedBindings;
  58455. return noLongerImported;
  58456. }
  58457. async ensureEntryFromUrl(rawUrl, ssr, setIsSelfAccepting = true) {
  58458. return this._ensureEntryFromUrl(rawUrl, ssr, setIsSelfAccepting);
  58459. }
  58460. /**
  58461. * @internal
  58462. */
  58463. async _ensureEntryFromUrl(rawUrl, ssr, setIsSelfAccepting = true, resolved) {
  58464. rawUrl = removeImportQuery(removeTimestampQuery(rawUrl));
  58465. let mod = this._getUnresolvedUrlToModule(rawUrl, ssr);
  58466. if (mod) {
  58467. return mod;
  58468. }
  58469. const modPromise = (async () => {
  58470. const [url, resolvedId, meta] = await this._resolveUrl(
  58471. rawUrl,
  58472. ssr,
  58473. resolved
  58474. );
  58475. mod = this.idToModuleMap.get(resolvedId);
  58476. if (!mod) {
  58477. mod = new ModuleNode(url, setIsSelfAccepting);
  58478. if (meta) mod.meta = meta;
  58479. this.urlToModuleMap.set(url, mod);
  58480. mod.id = resolvedId;
  58481. this.idToModuleMap.set(resolvedId, mod);
  58482. const file = mod.file = cleanUrl(resolvedId);
  58483. let fileMappedModules = this.fileToModulesMap.get(file);
  58484. if (!fileMappedModules) {
  58485. fileMappedModules = /* @__PURE__ */ new Set();
  58486. this.fileToModulesMap.set(file, fileMappedModules);
  58487. }
  58488. fileMappedModules.add(mod);
  58489. } else if (!this.urlToModuleMap.has(url)) {
  58490. this.urlToModuleMap.set(url, mod);
  58491. }
  58492. this._setUnresolvedUrlToModule(rawUrl, mod, ssr);
  58493. return mod;
  58494. })();
  58495. this._setUnresolvedUrlToModule(rawUrl, modPromise, ssr);
  58496. return modPromise;
  58497. }
  58498. // some deps, like a css file referenced via @import, don't have its own
  58499. // url because they are inlined into the main css import. But they still
  58500. // need to be represented in the module graph so that they can trigger
  58501. // hmr in the importing css file.
  58502. createFileOnlyEntry(file) {
  58503. file = normalizePath$3(file);
  58504. let fileMappedModules = this.fileToModulesMap.get(file);
  58505. if (!fileMappedModules) {
  58506. fileMappedModules = /* @__PURE__ */ new Set();
  58507. this.fileToModulesMap.set(file, fileMappedModules);
  58508. }
  58509. const url = `${FS_PREFIX}${file}`;
  58510. for (const m of fileMappedModules) {
  58511. if (m.url === url || m.id === file) {
  58512. return m;
  58513. }
  58514. }
  58515. const mod = new ModuleNode(url);
  58516. mod.file = file;
  58517. fileMappedModules.add(mod);
  58518. return mod;
  58519. }
  58520. // for incoming urls, it is important to:
  58521. // 1. remove the HMR timestamp query (?t=xxxx) and the ?import query
  58522. // 2. resolve its extension so that urls with or without extension all map to
  58523. // the same module
  58524. async resolveUrl(url, ssr) {
  58525. url = removeImportQuery(removeTimestampQuery(url));
  58526. const mod = await this._getUnresolvedUrlToModule(url, ssr);
  58527. if (mod?.id) {
  58528. return [mod.url, mod.id, mod.meta];
  58529. }
  58530. return this._resolveUrl(url, ssr);
  58531. }
  58532. updateModuleTransformResult(mod, result, ssr) {
  58533. if (ssr) {
  58534. mod.ssrTransformResult = result;
  58535. } else {
  58536. const prevEtag = mod.transformResult?.etag;
  58537. if (prevEtag) this.etagToModuleMap.delete(prevEtag);
  58538. mod.transformResult = result;
  58539. if (result?.etag) this.etagToModuleMap.set(result.etag, mod);
  58540. }
  58541. }
  58542. getModuleByEtag(etag) {
  58543. return this.etagToModuleMap.get(etag);
  58544. }
  58545. /**
  58546. * @internal
  58547. */
  58548. _getUnresolvedUrlToModule(url, ssr) {
  58549. return (ssr ? this._ssrUnresolvedUrlToModuleMap : this._unresolvedUrlToModuleMap).get(url);
  58550. }
  58551. /**
  58552. * @internal
  58553. */
  58554. _setUnresolvedUrlToModule(url, mod, ssr) {
  58555. (ssr ? this._ssrUnresolvedUrlToModuleMap : this._unresolvedUrlToModuleMap).set(url, mod);
  58556. }
  58557. /**
  58558. * @internal
  58559. */
  58560. async _resolveUrl(url, ssr, alreadyResolved) {
  58561. const resolved = alreadyResolved ?? await this.resolveId(url, !!ssr);
  58562. const resolvedId = resolved?.id || url;
  58563. if (url !== resolvedId && !url.includes("\0") && !url.startsWith(`virtual:`)) {
  58564. const ext = extname$1(cleanUrl(resolvedId));
  58565. if (ext) {
  58566. const pathname = cleanUrl(url);
  58567. if (!pathname.endsWith(ext)) {
  58568. url = pathname + ext + url.slice(pathname.length);
  58569. }
  58570. }
  58571. }
  58572. return [url, resolvedId, resolved?.meta];
  58573. }
  58574. }
  58575. function notFoundMiddleware() {
  58576. return function vite404Middleware(_, res) {
  58577. res.statusCode = 404;
  58578. res.end();
  58579. };
  58580. }
  58581. const ROOT_FILES = [
  58582. // '.git',
  58583. // https://pnpm.io/workspaces/
  58584. "pnpm-workspace.yaml",
  58585. // https://rushjs.io/pages/advanced/config_files/
  58586. // 'rush.json',
  58587. // https://nx.dev/latest/react/getting-started/nx-setup
  58588. // 'workspace.json',
  58589. // 'nx.json',
  58590. // https://github.com/lerna/lerna#lernajson
  58591. "lerna.json"
  58592. ];
  58593. function hasWorkspacePackageJSON(root) {
  58594. const path = join$2(root, "package.json");
  58595. if (!isFileReadable(path)) {
  58596. return false;
  58597. }
  58598. try {
  58599. const content = JSON.parse(fs__default.readFileSync(path, "utf-8")) || {};
  58600. return !!content.workspaces;
  58601. } catch {
  58602. return false;
  58603. }
  58604. }
  58605. function hasRootFile(root) {
  58606. return ROOT_FILES.some((file) => fs__default.existsSync(join$2(root, file)));
  58607. }
  58608. function hasPackageJSON(root) {
  58609. const path = join$2(root, "package.json");
  58610. return fs__default.existsSync(path);
  58611. }
  58612. function searchForPackageRoot(current, root = current) {
  58613. if (hasPackageJSON(current)) return current;
  58614. const dir = dirname$2(current);
  58615. if (!dir || dir === current) return root;
  58616. return searchForPackageRoot(dir, root);
  58617. }
  58618. function searchForWorkspaceRoot(current, root = searchForPackageRoot(current)) {
  58619. if (hasRootFile(current)) return current;
  58620. if (hasWorkspacePackageJSON(current)) return current;
  58621. const dir = dirname$2(current);
  58622. if (!dir || dir === current) return root;
  58623. return searchForWorkspaceRoot(dir, root);
  58624. }
  58625. function warmupFiles(server) {
  58626. const options = server.config.server.warmup;
  58627. const root = server.config.root;
  58628. if (options?.clientFiles?.length) {
  58629. mapFiles(options.clientFiles, root).then((files) => {
  58630. for (const file of files) {
  58631. warmupFile(server, file, false);
  58632. }
  58633. });
  58634. }
  58635. if (options?.ssrFiles?.length) {
  58636. mapFiles(options.ssrFiles, root).then((files) => {
  58637. for (const file of files) {
  58638. warmupFile(server, file, true);
  58639. }
  58640. });
  58641. }
  58642. }
  58643. async function warmupFile(server, file, ssr) {
  58644. if (file.endsWith(".html")) {
  58645. const url = htmlFileToUrl(file, server.config.root);
  58646. if (url) {
  58647. try {
  58648. const html = await fsp.readFile(file, "utf-8");
  58649. await server.transformIndexHtml(url, html);
  58650. } catch (e) {
  58651. server.config.logger.error(
  58652. `Pre-transform error (${colors$1.cyan(file)}): ${e.message}`,
  58653. {
  58654. error: e,
  58655. timestamp: true
  58656. }
  58657. );
  58658. }
  58659. }
  58660. } else {
  58661. const url = fileToUrl(file, server.config.root);
  58662. await server.warmupRequest(url, { ssr });
  58663. }
  58664. }
  58665. function htmlFileToUrl(file, root) {
  58666. const url = path$n.relative(root, file);
  58667. if (url[0] === ".") return;
  58668. return "/" + normalizePath$3(url);
  58669. }
  58670. function fileToUrl(file, root) {
  58671. const url = path$n.relative(root, file);
  58672. if (url[0] === ".") {
  58673. return path$n.posix.join(FS_PREFIX, normalizePath$3(file));
  58674. }
  58675. return "/" + normalizePath$3(url);
  58676. }
  58677. function mapFiles(files, root) {
  58678. return glob(files, {
  58679. cwd: root,
  58680. absolute: true
  58681. });
  58682. }
  58683. function rejectInvalidRequestMiddleware() {
  58684. return function viteRejectInvalidRequestMiddleware(req, res, next) {
  58685. if (req.url?.includes("#")) {
  58686. res.writeHead(400);
  58687. res.end();
  58688. return;
  58689. }
  58690. return next();
  58691. };
  58692. }
  58693. function createServer(inlineConfig = {}) {
  58694. return _createServer(inlineConfig, { hotListen: true });
  58695. }
  58696. async function _createServer(inlineConfig = {}, options) {
  58697. const config = await resolveConfig(inlineConfig, "serve");
  58698. const initPublicFilesPromise = initPublicFiles(config);
  58699. const { root, server: serverConfig } = config;
  58700. const httpsOptions = await resolveHttpsConfig(config.server.https);
  58701. const { middlewareMode } = serverConfig;
  58702. const resolvedOutDirs = getResolvedOutDirs(
  58703. config.root,
  58704. config.build.outDir,
  58705. config.build.rollupOptions?.output
  58706. );
  58707. const emptyOutDir = resolveEmptyOutDir(
  58708. config.build.emptyOutDir,
  58709. config.root,
  58710. resolvedOutDirs
  58711. );
  58712. const resolvedWatchOptions = resolveChokidarOptions(
  58713. {
  58714. disableGlobbing: true,
  58715. ...serverConfig.watch
  58716. },
  58717. resolvedOutDirs,
  58718. emptyOutDir,
  58719. config.cacheDir
  58720. );
  58721. const middlewares = connect$1();
  58722. const httpServer = middlewareMode ? null : await resolveHttpServer(serverConfig, middlewares, httpsOptions);
  58723. const ws = createWebSocketServer(httpServer, config, httpsOptions);
  58724. const hot = createHMRBroadcaster().addChannel(ws).addChannel(createServerHMRChannel());
  58725. if (typeof config.server.hmr === "object" && config.server.hmr.channels) {
  58726. config.server.hmr.channels.forEach((channel) => hot.addChannel(channel));
  58727. }
  58728. const publicFiles = await initPublicFilesPromise;
  58729. const { publicDir } = config;
  58730. if (httpServer) {
  58731. setClientErrorHandler(httpServer, config.logger);
  58732. }
  58733. const watchEnabled = serverConfig.watch !== null;
  58734. const watcher = watchEnabled ? chokidar.watch(
  58735. // config file dependencies and env file might be outside of root
  58736. [
  58737. root,
  58738. ...config.configFileDependencies,
  58739. ...getEnvFilesForMode(config.mode, config.envDir),
  58740. // Watch the public directory explicitly because it might be outside
  58741. // of the root directory.
  58742. ...publicDir && publicFiles ? [publicDir] : []
  58743. ],
  58744. resolvedWatchOptions
  58745. ) : createNoopWatcher(resolvedWatchOptions);
  58746. const moduleGraph = new ModuleGraph(
  58747. (url, ssr) => container.resolveId(url, void 0, { ssr })
  58748. );
  58749. const container = await createPluginContainer(config, moduleGraph, watcher);
  58750. const closeHttpServer = createServerCloseFn(httpServer);
  58751. const devHtmlTransformFn = createDevHtmlTransformFn(config);
  58752. const onCrawlEndCallbacks = [];
  58753. const crawlEndFinder = setupOnCrawlEnd(() => {
  58754. onCrawlEndCallbacks.forEach((cb) => cb());
  58755. });
  58756. function waitForRequestsIdle(ignoredId) {
  58757. return crawlEndFinder.waitForRequestsIdle(ignoredId);
  58758. }
  58759. function _registerRequestProcessing(id, done) {
  58760. crawlEndFinder.registerRequestProcessing(id, done);
  58761. }
  58762. function _onCrawlEnd(cb) {
  58763. onCrawlEndCallbacks.push(cb);
  58764. }
  58765. let server = {
  58766. config,
  58767. middlewares,
  58768. httpServer,
  58769. watcher,
  58770. pluginContainer: container,
  58771. ws,
  58772. hot,
  58773. moduleGraph,
  58774. resolvedUrls: null,
  58775. // will be set on listen
  58776. ssrTransform(code, inMap, url, originalCode = code) {
  58777. return ssrTransform(code, inMap, url, originalCode, server.config);
  58778. },
  58779. transformRequest(url, options2) {
  58780. return transformRequest(url, server, options2);
  58781. },
  58782. async warmupRequest(url, options2) {
  58783. try {
  58784. await transformRequest(url, server, options2);
  58785. } catch (e) {
  58786. if (e?.code === ERR_OUTDATED_OPTIMIZED_DEP || e?.code === ERR_CLOSED_SERVER) {
  58787. return;
  58788. }
  58789. server.config.logger.error(`Pre-transform error: ${e.message}`, {
  58790. error: e,
  58791. timestamp: true
  58792. });
  58793. }
  58794. },
  58795. transformIndexHtml(url, html, originalUrl) {
  58796. return devHtmlTransformFn(server, url, html, originalUrl);
  58797. },
  58798. async ssrLoadModule(url, opts) {
  58799. return ssrLoadModule(url, server, opts?.fixStacktrace);
  58800. },
  58801. async ssrFetchModule(url, importer) {
  58802. return ssrFetchModule(server, url, importer);
  58803. },
  58804. ssrFixStacktrace(e) {
  58805. ssrFixStacktrace(e, moduleGraph);
  58806. },
  58807. ssrRewriteStacktrace(stack) {
  58808. return ssrRewriteStacktrace(stack, moduleGraph);
  58809. },
  58810. async reloadModule(module) {
  58811. if (serverConfig.hmr !== false && module.file) {
  58812. updateModules(module.file, [module], Date.now(), server);
  58813. }
  58814. },
  58815. async listen(port, isRestart) {
  58816. await startServer(server, port);
  58817. if (httpServer) {
  58818. server.resolvedUrls = await resolveServerUrls(
  58819. httpServer,
  58820. config.server,
  58821. config
  58822. );
  58823. if (!isRestart && config.server.open) server.openBrowser();
  58824. }
  58825. return server;
  58826. },
  58827. openBrowser() {
  58828. const options2 = server.config.server;
  58829. const url = server.resolvedUrls?.local[0] ?? server.resolvedUrls?.network[0];
  58830. if (url) {
  58831. const path2 = typeof options2.open === "string" ? new URL(options2.open, url).href : url;
  58832. if (server.config.server.preTransformRequests) {
  58833. setTimeout(() => {
  58834. const getMethod = path2.startsWith("https:") ? get$1 : get$2;
  58835. getMethod(
  58836. path2,
  58837. {
  58838. headers: {
  58839. // Allow the history middleware to redirect to /index.html
  58840. Accept: "text/html"
  58841. }
  58842. },
  58843. (res) => {
  58844. res.on("end", () => {
  58845. });
  58846. }
  58847. ).on("error", () => {
  58848. }).end();
  58849. }, 0);
  58850. }
  58851. openBrowser(path2, true, server.config.logger);
  58852. } else {
  58853. server.config.logger.warn("No URL available to open in browser");
  58854. }
  58855. },
  58856. async close() {
  58857. if (!middlewareMode) {
  58858. teardownSIGTERMListener(closeServerAndExit);
  58859. }
  58860. await Promise.allSettled([
  58861. watcher.close(),
  58862. hot.close(),
  58863. container.close(),
  58864. crawlEndFinder?.cancel(),
  58865. getDepsOptimizer(server.config)?.close(),
  58866. getDepsOptimizer(server.config, true)?.close(),
  58867. closeHttpServer()
  58868. ]);
  58869. while (server._pendingRequests.size > 0) {
  58870. await Promise.allSettled(
  58871. [...server._pendingRequests.values()].map(
  58872. (pending) => pending.request
  58873. )
  58874. );
  58875. }
  58876. server.resolvedUrls = null;
  58877. },
  58878. printUrls() {
  58879. if (server.resolvedUrls) {
  58880. printServerUrls(
  58881. server.resolvedUrls,
  58882. serverConfig.host,
  58883. config.logger.info
  58884. );
  58885. } else if (middlewareMode) {
  58886. throw new Error("cannot print server URLs in middleware mode.");
  58887. } else {
  58888. throw new Error(
  58889. "cannot print server URLs before server.listen is called."
  58890. );
  58891. }
  58892. },
  58893. bindCLIShortcuts(options2) {
  58894. bindCLIShortcuts(server, options2);
  58895. },
  58896. async restart(forceOptimize) {
  58897. if (!server._restartPromise) {
  58898. server._forceOptimizeOnRestart = !!forceOptimize;
  58899. server._restartPromise = restartServer(server).finally(() => {
  58900. server._restartPromise = null;
  58901. server._forceOptimizeOnRestart = false;
  58902. });
  58903. }
  58904. return server._restartPromise;
  58905. },
  58906. waitForRequestsIdle,
  58907. _registerRequestProcessing,
  58908. _onCrawlEnd,
  58909. _setInternalServer(_server) {
  58910. server = _server;
  58911. },
  58912. _restartPromise: null,
  58913. _importGlobMap: /* @__PURE__ */ new Map(),
  58914. _forceOptimizeOnRestart: false,
  58915. _pendingRequests: /* @__PURE__ */ new Map(),
  58916. _fsDenyGlob: picomatch$4(
  58917. // matchBase: true does not work as it's documented
  58918. // https://github.com/micromatch/picomatch/issues/89
  58919. // convert patterns without `/` on our side for now
  58920. config.server.fs.deny.map(
  58921. (pattern) => pattern.includes("/") ? pattern : `**/${pattern}`
  58922. ),
  58923. {
  58924. matchBase: false,
  58925. nocase: true,
  58926. dot: true
  58927. }
  58928. ),
  58929. _shortcutsOptions: void 0
  58930. };
  58931. const reflexServer = new Proxy(server, {
  58932. get: (_, property) => {
  58933. return server[property];
  58934. },
  58935. set: (_, property, value) => {
  58936. server[property] = value;
  58937. return true;
  58938. }
  58939. });
  58940. const closeServerAndExit = async () => {
  58941. try {
  58942. await server.close();
  58943. } finally {
  58944. process.exit();
  58945. }
  58946. };
  58947. if (!middlewareMode) {
  58948. setupSIGTERMListener(closeServerAndExit);
  58949. }
  58950. const onHMRUpdate = async (type, file) => {
  58951. if (serverConfig.hmr !== false) {
  58952. try {
  58953. await handleHMRUpdate(type, file, server);
  58954. } catch (err) {
  58955. hot.send({
  58956. type: "error",
  58957. err: prepareError(err)
  58958. });
  58959. }
  58960. }
  58961. };
  58962. const onFileAddUnlink = async (file, isUnlink) => {
  58963. file = normalizePath$3(file);
  58964. await container.watchChange(file, { event: isUnlink ? "delete" : "create" });
  58965. if (publicDir && publicFiles) {
  58966. if (file.startsWith(publicDir)) {
  58967. const path2 = file.slice(publicDir.length);
  58968. publicFiles[isUnlink ? "delete" : "add"](path2);
  58969. if (!isUnlink) {
  58970. const moduleWithSamePath = await moduleGraph.getModuleByUrl(path2);
  58971. const etag = moduleWithSamePath?.transformResult?.etag;
  58972. if (etag) {
  58973. moduleGraph.etagToModuleMap.delete(etag);
  58974. }
  58975. }
  58976. }
  58977. }
  58978. if (isUnlink) moduleGraph.onFileDelete(file);
  58979. await onHMRUpdate(isUnlink ? "delete" : "create", file);
  58980. };
  58981. watcher.on("change", async (file) => {
  58982. file = normalizePath$3(file);
  58983. await container.watchChange(file, { event: "update" });
  58984. moduleGraph.onFileChange(file);
  58985. await onHMRUpdate("update", file);
  58986. });
  58987. getFsUtils(config).initWatcher?.(watcher);
  58988. watcher.on("add", (file) => {
  58989. onFileAddUnlink(file, false);
  58990. });
  58991. watcher.on("unlink", (file) => {
  58992. onFileAddUnlink(file, true);
  58993. });
  58994. hot.on("vite:invalidate", async ({ path: path2, message }) => {
  58995. const mod = moduleGraph.urlToModuleMap.get(path2);
  58996. if (mod && mod.isSelfAccepting && mod.lastHMRTimestamp > 0 && !mod.lastHMRInvalidationReceived) {
  58997. mod.lastHMRInvalidationReceived = true;
  58998. config.logger.info(
  58999. colors$1.yellow(`hmr invalidate `) + colors$1.dim(path2) + (message ? ` ${message}` : ""),
  59000. { timestamp: true }
  59001. );
  59002. const file = getShortName(mod.file, config.root);
  59003. updateModules(
  59004. file,
  59005. [...mod.importers],
  59006. mod.lastHMRTimestamp,
  59007. server,
  59008. true
  59009. );
  59010. }
  59011. });
  59012. if (!middlewareMode && httpServer) {
  59013. httpServer.once("listening", () => {
  59014. serverConfig.port = httpServer.address().port;
  59015. });
  59016. }
  59017. const postHooks = [];
  59018. for (const hook of config.getSortedPluginHooks("configureServer")) {
  59019. postHooks.push(await hook(reflexServer));
  59020. }
  59021. if (process.env.DEBUG) {
  59022. middlewares.use(timeMiddleware(root));
  59023. }
  59024. middlewares.use(rejectInvalidRequestMiddleware());
  59025. const { cors } = serverConfig;
  59026. if (cors !== false) {
  59027. middlewares.use(
  59028. corsMiddleware(
  59029. typeof cors === "boolean" ? {} : cors ?? { origin: defaultAllowedOrigins }
  59030. )
  59031. );
  59032. }
  59033. const { allowedHosts } = serverConfig;
  59034. if (allowedHosts !== true && !serverConfig.https) {
  59035. middlewares.use(hostCheckMiddleware(config, false));
  59036. }
  59037. middlewares.use(cachedTransformMiddleware(server));
  59038. const { proxy } = serverConfig;
  59039. if (proxy) {
  59040. const middlewareServer = (isObject$1(middlewareMode) ? middlewareMode.server : null) || httpServer;
  59041. middlewares.use(proxyMiddleware(middlewareServer, proxy, config));
  59042. }
  59043. if (config.base !== "/") {
  59044. middlewares.use(baseMiddleware(config.rawBase, !!middlewareMode));
  59045. }
  59046. middlewares.use("/__open-in-editor", launchEditorMiddleware$1());
  59047. middlewares.use(function viteHMRPingMiddleware(req, res, next) {
  59048. if (req.headers["accept"] === "text/x-vite-ping") {
  59049. res.writeHead(204).end();
  59050. } else {
  59051. next();
  59052. }
  59053. });
  59054. if (publicDir) {
  59055. middlewares.use(servePublicMiddleware(server, publicFiles));
  59056. }
  59057. middlewares.use(transformMiddleware(server));
  59058. middlewares.use(serveRawFsMiddleware(server));
  59059. middlewares.use(serveStaticMiddleware(server));
  59060. if (config.appType === "spa" || config.appType === "mpa") {
  59061. middlewares.use(
  59062. htmlFallbackMiddleware(
  59063. root,
  59064. config.appType === "spa",
  59065. getFsUtils(config)
  59066. )
  59067. );
  59068. }
  59069. postHooks.forEach((fn) => fn && fn());
  59070. if (config.appType === "spa" || config.appType === "mpa") {
  59071. middlewares.use(indexHtmlMiddleware(root, server));
  59072. middlewares.use(notFoundMiddleware());
  59073. }
  59074. middlewares.use(errorMiddleware(server, !!middlewareMode));
  59075. let initingServer;
  59076. let serverInited = false;
  59077. const initServer = async () => {
  59078. if (serverInited) return;
  59079. if (initingServer) return initingServer;
  59080. initingServer = async function() {
  59081. await container.buildStart({});
  59082. if (isDepsOptimizerEnabled(config, false)) {
  59083. await initDepsOptimizer(config, server);
  59084. }
  59085. warmupFiles(server);
  59086. initingServer = void 0;
  59087. serverInited = true;
  59088. }();
  59089. return initingServer;
  59090. };
  59091. if (!middlewareMode && httpServer) {
  59092. const listen = httpServer.listen.bind(httpServer);
  59093. httpServer.listen = async (port, ...args) => {
  59094. try {
  59095. hot.listen();
  59096. await initServer();
  59097. } catch (e) {
  59098. httpServer.emit("error", e);
  59099. return;
  59100. }
  59101. return listen(port, ...args);
  59102. };
  59103. } else {
  59104. if (options.hotListen) {
  59105. hot.listen();
  59106. }
  59107. await initServer();
  59108. }
  59109. return server;
  59110. }
  59111. async function startServer(server, inlinePort) {
  59112. const httpServer = server.httpServer;
  59113. if (!httpServer) {
  59114. throw new Error("Cannot call server.listen in middleware mode.");
  59115. }
  59116. const options = server.config.server;
  59117. const hostname = await resolveHostname(options.host);
  59118. const configPort = inlinePort ?? options.port;
  59119. const port = (!configPort || configPort === server._configServerPort ? server._currentServerPort : configPort) ?? DEFAULT_DEV_PORT;
  59120. server._configServerPort = configPort;
  59121. const serverPort = await httpServerStart(httpServer, {
  59122. port,
  59123. strictPort: options.strictPort,
  59124. host: hostname.host,
  59125. logger: server.config.logger
  59126. });
  59127. server._currentServerPort = serverPort;
  59128. }
  59129. function createServerCloseFn(server) {
  59130. if (!server) {
  59131. return () => Promise.resolve();
  59132. }
  59133. let hasListened = false;
  59134. const openSockets = /* @__PURE__ */ new Set();
  59135. server.on("connection", (socket) => {
  59136. openSockets.add(socket);
  59137. socket.on("close", () => {
  59138. openSockets.delete(socket);
  59139. });
  59140. });
  59141. server.once("listening", () => {
  59142. hasListened = true;
  59143. });
  59144. return () => new Promise((resolve, reject) => {
  59145. openSockets.forEach((s) => s.destroy());
  59146. if (hasListened) {
  59147. server.close((err) => {
  59148. if (err) {
  59149. reject(err);
  59150. } else {
  59151. resolve();
  59152. }
  59153. });
  59154. } else {
  59155. resolve();
  59156. }
  59157. });
  59158. }
  59159. function resolvedAllowDir(root, dir) {
  59160. return normalizePath$3(path$n.resolve(root, dir));
  59161. }
  59162. function resolveServerOptions(root, raw, logger) {
  59163. const server = {
  59164. preTransformRequests: true,
  59165. ...raw,
  59166. sourcemapIgnoreList: raw?.sourcemapIgnoreList === false ? () => false : raw?.sourcemapIgnoreList || isInNodeModules$1,
  59167. middlewareMode: raw?.middlewareMode || false
  59168. };
  59169. let allowDirs = server.fs?.allow;
  59170. const deny = server.fs?.deny || [".env", ".env.*", "*.{crt,pem}"];
  59171. if (!allowDirs) {
  59172. allowDirs = [searchForWorkspaceRoot(root)];
  59173. }
  59174. if (process.versions.pnp) {
  59175. try {
  59176. const enableGlobalCache = execSync("yarn config get enableGlobalCache", { cwd: root }).toString().trim() === "true";
  59177. const yarnCacheDir = execSync(
  59178. `yarn config get ${enableGlobalCache ? "globalFolder" : "cacheFolder"}`,
  59179. { cwd: root }
  59180. ).toString().trim();
  59181. allowDirs.push(yarnCacheDir);
  59182. } catch (e) {
  59183. logger.warn(`Get yarn cache dir error: ${e.message}`, {
  59184. timestamp: true
  59185. });
  59186. }
  59187. }
  59188. allowDirs = allowDirs.map((i) => resolvedAllowDir(root, i));
  59189. const resolvedClientDir = resolvedAllowDir(root, CLIENT_DIR);
  59190. if (!allowDirs.some((dir) => isParentDirectory(dir, resolvedClientDir))) {
  59191. allowDirs.push(resolvedClientDir);
  59192. }
  59193. server.fs = {
  59194. strict: server.fs?.strict ?? true,
  59195. allow: allowDirs,
  59196. deny,
  59197. cachedChecks: server.fs?.cachedChecks
  59198. };
  59199. if (server.origin?.endsWith("/")) {
  59200. server.origin = server.origin.slice(0, -1);
  59201. logger.warn(
  59202. colors$1.yellow(
  59203. `${colors$1.bold("(!)")} server.origin should not end with "/". Using "${server.origin}" instead.`
  59204. )
  59205. );
  59206. }
  59207. return server;
  59208. }
  59209. async function restartServer(server) {
  59210. global.__vite_start_time = performance$1.now();
  59211. const shortcutsOptions = server._shortcutsOptions;
  59212. let inlineConfig = server.config.inlineConfig;
  59213. if (server._forceOptimizeOnRestart) {
  59214. inlineConfig = mergeConfig(inlineConfig, {
  59215. optimizeDeps: {
  59216. force: true
  59217. }
  59218. });
  59219. }
  59220. {
  59221. let newServer = null;
  59222. try {
  59223. newServer = await _createServer(inlineConfig, { hotListen: false });
  59224. } catch (err) {
  59225. server.config.logger.error(err.message, {
  59226. timestamp: true
  59227. });
  59228. server.config.logger.error("server restart failed", { timestamp: true });
  59229. return;
  59230. }
  59231. await server.close();
  59232. const middlewares = server.middlewares;
  59233. newServer._configServerPort = server._configServerPort;
  59234. newServer._currentServerPort = server._currentServerPort;
  59235. Object.assign(server, newServer);
  59236. middlewares.stack = newServer.middlewares.stack;
  59237. server.middlewares = middlewares;
  59238. newServer._setInternalServer(server);
  59239. }
  59240. const {
  59241. logger,
  59242. server: { port, middlewareMode }
  59243. } = server.config;
  59244. if (!middlewareMode) {
  59245. await server.listen(port, true);
  59246. } else {
  59247. server.hot.listen();
  59248. }
  59249. logger.info("server restarted.", { timestamp: true });
  59250. if (shortcutsOptions) {
  59251. shortcutsOptions.print = false;
  59252. bindCLIShortcuts(server, shortcutsOptions);
  59253. }
  59254. }
  59255. async function restartServerWithUrls(server) {
  59256. if (server.config.server.middlewareMode) {
  59257. await server.restart();
  59258. return;
  59259. }
  59260. const { port: prevPort, host: prevHost } = server.config.server;
  59261. const prevUrls = server.resolvedUrls;
  59262. await server.restart();
  59263. const {
  59264. logger,
  59265. server: { port, host }
  59266. } = server.config;
  59267. if ((port ?? DEFAULT_DEV_PORT) !== (prevPort ?? DEFAULT_DEV_PORT) || host !== prevHost || diffDnsOrderChange(prevUrls, server.resolvedUrls)) {
  59268. logger.info("");
  59269. server.printUrls();
  59270. }
  59271. }
  59272. const callCrawlEndIfIdleAfterMs = 50;
  59273. function setupOnCrawlEnd(onCrawlEnd) {
  59274. const registeredIds = /* @__PURE__ */ new Set();
  59275. const seenIds = /* @__PURE__ */ new Set();
  59276. const onCrawlEndPromiseWithResolvers = promiseWithResolvers();
  59277. let timeoutHandle;
  59278. let cancelled = false;
  59279. function cancel() {
  59280. cancelled = true;
  59281. }
  59282. let crawlEndCalled = false;
  59283. function callOnCrawlEnd() {
  59284. if (!cancelled && !crawlEndCalled) {
  59285. crawlEndCalled = true;
  59286. onCrawlEnd();
  59287. }
  59288. onCrawlEndPromiseWithResolvers.resolve();
  59289. }
  59290. function registerRequestProcessing(id, done) {
  59291. if (!seenIds.has(id)) {
  59292. seenIds.add(id);
  59293. registeredIds.add(id);
  59294. done().catch(() => {
  59295. }).finally(() => markIdAsDone(id));
  59296. }
  59297. }
  59298. function waitForRequestsIdle(ignoredId) {
  59299. if (ignoredId) {
  59300. seenIds.add(ignoredId);
  59301. markIdAsDone(ignoredId);
  59302. } else {
  59303. checkIfCrawlEndAfterTimeout();
  59304. }
  59305. return onCrawlEndPromiseWithResolvers.promise;
  59306. }
  59307. function markIdAsDone(id) {
  59308. registeredIds.delete(id);
  59309. checkIfCrawlEndAfterTimeout();
  59310. }
  59311. function checkIfCrawlEndAfterTimeout() {
  59312. if (cancelled || registeredIds.size > 0) return;
  59313. if (timeoutHandle) clearTimeout(timeoutHandle);
  59314. timeoutHandle = setTimeout(
  59315. callOnCrawlEndWhenIdle,
  59316. callCrawlEndIfIdleAfterMs
  59317. );
  59318. }
  59319. async function callOnCrawlEndWhenIdle() {
  59320. if (cancelled || registeredIds.size > 0) return;
  59321. callOnCrawlEnd();
  59322. }
  59323. return {
  59324. registerRequestProcessing,
  59325. waitForRequestsIdle,
  59326. cancel
  59327. };
  59328. }
  59329. var index = {
  59330. __proto__: null,
  59331. _createServer: _createServer,
  59332. createServer: createServer,
  59333. createServerCloseFn: createServerCloseFn,
  59334. resolveServerOptions: resolveServerOptions,
  59335. restartServerWithUrls: restartServerWithUrls
  59336. };
  59337. const debugHmr = createDebugger("vite:hmr");
  59338. const whitespaceRE = /\s/;
  59339. const normalizedClientDir = normalizePath$3(CLIENT_DIR);
  59340. function getShortName(file, root) {
  59341. return file.startsWith(withTrailingSlash(root)) ? path$n.posix.relative(root, file) : file;
  59342. }
  59343. async function handleHMRUpdate(type, file, server) {
  59344. const { hot, config, moduleGraph } = server;
  59345. const shortFile = getShortName(file, config.root);
  59346. const isConfig = file === config.configFile;
  59347. const isConfigDependency = config.configFileDependencies.some(
  59348. (name) => file === name
  59349. );
  59350. const isEnv = config.inlineConfig.envFile !== false && getEnvFilesForMode(config.mode, config.envDir).includes(file);
  59351. if (isConfig || isConfigDependency || isEnv) {
  59352. debugHmr?.(`[config change] ${colors$1.dim(shortFile)}`);
  59353. config.logger.info(
  59354. colors$1.green(
  59355. `${normalizePath$3(
  59356. path$n.relative(process.cwd(), file)
  59357. )} changed, restarting server...`
  59358. ),
  59359. { clear: true, timestamp: true }
  59360. );
  59361. try {
  59362. await restartServerWithUrls(server);
  59363. } catch (e) {
  59364. config.logger.error(colors$1.red(e));
  59365. }
  59366. return;
  59367. }
  59368. debugHmr?.(`[file change] ${colors$1.dim(shortFile)}`);
  59369. if (file.startsWith(withTrailingSlash(normalizedClientDir))) {
  59370. hot.send({
  59371. type: "full-reload",
  59372. path: "*",
  59373. triggeredBy: path$n.resolve(config.root, file)
  59374. });
  59375. return;
  59376. }
  59377. const mods = new Set(moduleGraph.getModulesByFile(file));
  59378. if (type === "create") {
  59379. for (const mod of moduleGraph._hasResolveFailedErrorModules) {
  59380. mods.add(mod);
  59381. }
  59382. }
  59383. if (type === "create" || type === "delete") {
  59384. for (const mod of getAffectedGlobModules(file, server)) {
  59385. mods.add(mod);
  59386. }
  59387. }
  59388. const timestamp = Date.now();
  59389. const hmrContext = {
  59390. file,
  59391. timestamp,
  59392. modules: [...mods],
  59393. read: () => readModifiedFile(file),
  59394. server
  59395. };
  59396. if (type === "update") {
  59397. for (const hook of config.getSortedPluginHooks("handleHotUpdate")) {
  59398. const filteredModules = await hook(hmrContext);
  59399. if (filteredModules) {
  59400. hmrContext.modules = filteredModules;
  59401. }
  59402. }
  59403. }
  59404. if (!hmrContext.modules.length) {
  59405. if (file.endsWith(".html")) {
  59406. config.logger.info(colors$1.green(`page reload `) + colors$1.dim(shortFile), {
  59407. clear: true,
  59408. timestamp: true
  59409. });
  59410. hot.send({
  59411. type: "full-reload",
  59412. path: config.server.middlewareMode ? "*" : "/" + normalizePath$3(path$n.relative(config.root, file))
  59413. });
  59414. } else {
  59415. debugHmr?.(`[no modules matched] ${colors$1.dim(shortFile)}`);
  59416. }
  59417. return;
  59418. }
  59419. updateModules(shortFile, hmrContext.modules, timestamp, server);
  59420. }
  59421. function updateModules(file, modules, timestamp, { config, hot, moduleGraph }, afterInvalidation) {
  59422. const updates = [];
  59423. const invalidatedModules = /* @__PURE__ */ new Set();
  59424. const traversedModules = /* @__PURE__ */ new Set();
  59425. let needFullReload = modules.length === 0;
  59426. for (const mod of modules) {
  59427. const boundaries = [];
  59428. const hasDeadEnd = propagateUpdate(mod, traversedModules, boundaries);
  59429. moduleGraph.invalidateModule(mod, invalidatedModules, timestamp, true);
  59430. if (needFullReload) {
  59431. continue;
  59432. }
  59433. if (hasDeadEnd) {
  59434. needFullReload = hasDeadEnd;
  59435. continue;
  59436. }
  59437. updates.push(
  59438. ...boundaries.map(
  59439. ({ boundary, acceptedVia, isWithinCircularImport }) => ({
  59440. type: `${boundary.type}-update`,
  59441. timestamp,
  59442. path: normalizeHmrUrl(boundary.url),
  59443. acceptedPath: normalizeHmrUrl(acceptedVia.url),
  59444. explicitImportRequired: boundary.type === "js" ? isExplicitImportRequired(acceptedVia.url) : false,
  59445. isWithinCircularImport,
  59446. // browser modules are invalidated by changing ?t= query,
  59447. // but in ssr we control the module system, so we can directly remove them form cache
  59448. ssrInvalidates: getSSRInvalidatedImporters(acceptedVia)
  59449. })
  59450. )
  59451. );
  59452. }
  59453. if (needFullReload) {
  59454. const reason = typeof needFullReload === "string" ? colors$1.dim(` (${needFullReload})`) : "";
  59455. config.logger.info(
  59456. colors$1.green(`page reload `) + colors$1.dim(file) + reason,
  59457. { clear: !afterInvalidation, timestamp: true }
  59458. );
  59459. hot.send({
  59460. type: "full-reload",
  59461. triggeredBy: path$n.resolve(config.root, file)
  59462. });
  59463. return;
  59464. }
  59465. if (updates.length === 0) {
  59466. debugHmr?.(colors$1.yellow(`no update happened `) + colors$1.dim(file));
  59467. return;
  59468. }
  59469. config.logger.info(
  59470. colors$1.green(`hmr update `) + colors$1.dim([...new Set(updates.map((u) => u.path))].join(", ")),
  59471. { clear: !afterInvalidation, timestamp: true }
  59472. );
  59473. hot.send({
  59474. type: "update",
  59475. updates
  59476. });
  59477. }
  59478. function populateSSRImporters(module, timestamp, seen = /* @__PURE__ */ new Set()) {
  59479. module.ssrImportedModules.forEach((importer) => {
  59480. if (seen.has(importer)) {
  59481. return;
  59482. }
  59483. if (importer.lastHMRTimestamp === timestamp || importer.lastInvalidationTimestamp === timestamp) {
  59484. seen.add(importer);
  59485. populateSSRImporters(importer, timestamp, seen);
  59486. }
  59487. });
  59488. return seen;
  59489. }
  59490. function getSSRInvalidatedImporters(module) {
  59491. return [...populateSSRImporters(module, module.lastHMRTimestamp)].map(
  59492. (m) => m.file
  59493. );
  59494. }
  59495. function areAllImportsAccepted(importedBindings, acceptedExports) {
  59496. for (const binding of importedBindings) {
  59497. if (!acceptedExports.has(binding)) {
  59498. return false;
  59499. }
  59500. }
  59501. return true;
  59502. }
  59503. function propagateUpdate(node, traversedModules, boundaries, currentChain = [node]) {
  59504. if (traversedModules.has(node)) {
  59505. return false;
  59506. }
  59507. traversedModules.add(node);
  59508. if (node.id && node.isSelfAccepting === void 0) {
  59509. debugHmr?.(
  59510. `[propagate update] stop propagation because not analyzed: ${colors$1.dim(
  59511. node.id
  59512. )}`
  59513. );
  59514. return false;
  59515. }
  59516. if (node.isSelfAccepting) {
  59517. boundaries.push({
  59518. boundary: node,
  59519. acceptedVia: node,
  59520. isWithinCircularImport: isNodeWithinCircularImports(node, currentChain)
  59521. });
  59522. for (const importer of node.importers) {
  59523. if (isCSSRequest(importer.url) && !currentChain.includes(importer)) {
  59524. propagateUpdate(
  59525. importer,
  59526. traversedModules,
  59527. boundaries,
  59528. currentChain.concat(importer)
  59529. );
  59530. }
  59531. }
  59532. return false;
  59533. }
  59534. if (node.acceptedHmrExports) {
  59535. boundaries.push({
  59536. boundary: node,
  59537. acceptedVia: node,
  59538. isWithinCircularImport: isNodeWithinCircularImports(node, currentChain)
  59539. });
  59540. } else {
  59541. if (!node.importers.size) {
  59542. return true;
  59543. }
  59544. if (!isCSSRequest(node.url) && [...node.importers].every((i) => isCSSRequest(i.url))) {
  59545. return true;
  59546. }
  59547. }
  59548. for (const importer of node.importers) {
  59549. const subChain = currentChain.concat(importer);
  59550. if (importer.acceptedHmrDeps.has(node)) {
  59551. boundaries.push({
  59552. boundary: importer,
  59553. acceptedVia: node,
  59554. isWithinCircularImport: isNodeWithinCircularImports(importer, subChain)
  59555. });
  59556. continue;
  59557. }
  59558. if (node.id && node.acceptedHmrExports && importer.importedBindings) {
  59559. const importedBindingsFromNode = importer.importedBindings.get(node.id);
  59560. if (importedBindingsFromNode && areAllImportsAccepted(importedBindingsFromNode, node.acceptedHmrExports)) {
  59561. continue;
  59562. }
  59563. }
  59564. if (!currentChain.includes(importer) && propagateUpdate(importer, traversedModules, boundaries, subChain)) {
  59565. return true;
  59566. }
  59567. }
  59568. return false;
  59569. }
  59570. function isNodeWithinCircularImports(node, nodeChain, currentChain = [node], traversedModules = /* @__PURE__ */ new Set()) {
  59571. if (traversedModules.has(node)) {
  59572. return false;
  59573. }
  59574. traversedModules.add(node);
  59575. for (const importer of node.importers) {
  59576. if (importer === node) continue;
  59577. if (isCSSRequest(importer.url)) continue;
  59578. const importerIndex = nodeChain.indexOf(importer);
  59579. if (importerIndex > -1) {
  59580. if (debugHmr) {
  59581. const importChain = [
  59582. importer,
  59583. ...[...currentChain].reverse(),
  59584. ...nodeChain.slice(importerIndex, -1).reverse()
  59585. ];
  59586. debugHmr(
  59587. colors$1.yellow(`circular imports detected: `) + importChain.map((m) => colors$1.dim(m.url)).join(" -> ")
  59588. );
  59589. }
  59590. return true;
  59591. }
  59592. if (!currentChain.includes(importer)) {
  59593. const result = isNodeWithinCircularImports(
  59594. importer,
  59595. nodeChain,
  59596. currentChain.concat(importer),
  59597. traversedModules
  59598. );
  59599. if (result) return result;
  59600. }
  59601. }
  59602. return false;
  59603. }
  59604. function handlePrunedModules(mods, { hot }) {
  59605. const t = Date.now();
  59606. mods.forEach((mod) => {
  59607. mod.lastHMRTimestamp = t;
  59608. mod.lastHMRInvalidationReceived = false;
  59609. debugHmr?.(`[dispose] ${colors$1.dim(mod.file)}`);
  59610. });
  59611. hot.send({
  59612. type: "prune",
  59613. paths: [...mods].map((m) => m.url)
  59614. });
  59615. }
  59616. function lexAcceptedHmrDeps(code, start, urls) {
  59617. let state = 0 /* inCall */;
  59618. let prevState = 0 /* inCall */;
  59619. let currentDep = "";
  59620. function addDep(index) {
  59621. urls.add({
  59622. url: currentDep,
  59623. start: index - currentDep.length - 1,
  59624. end: index + 1
  59625. });
  59626. currentDep = "";
  59627. }
  59628. for (let i = start; i < code.length; i++) {
  59629. const char = code.charAt(i);
  59630. switch (state) {
  59631. case 0 /* inCall */:
  59632. case 4 /* inArray */:
  59633. if (char === `'`) {
  59634. prevState = state;
  59635. state = 1 /* inSingleQuoteString */;
  59636. } else if (char === `"`) {
  59637. prevState = state;
  59638. state = 2 /* inDoubleQuoteString */;
  59639. } else if (char === "`") {
  59640. prevState = state;
  59641. state = 3 /* inTemplateString */;
  59642. } else if (whitespaceRE.test(char)) {
  59643. continue;
  59644. } else {
  59645. if (state === 0 /* inCall */) {
  59646. if (char === `[`) {
  59647. state = 4 /* inArray */;
  59648. } else {
  59649. return true;
  59650. }
  59651. } else if (state === 4 /* inArray */) {
  59652. if (char === `]`) {
  59653. return false;
  59654. } else if (char === ",") {
  59655. continue;
  59656. } else {
  59657. error(i);
  59658. }
  59659. }
  59660. }
  59661. break;
  59662. case 1 /* inSingleQuoteString */:
  59663. if (char === `'`) {
  59664. addDep(i);
  59665. if (prevState === 0 /* inCall */) {
  59666. return false;
  59667. } else {
  59668. state = prevState;
  59669. }
  59670. } else {
  59671. currentDep += char;
  59672. }
  59673. break;
  59674. case 2 /* inDoubleQuoteString */:
  59675. if (char === `"`) {
  59676. addDep(i);
  59677. if (prevState === 0 /* inCall */) {
  59678. return false;
  59679. } else {
  59680. state = prevState;
  59681. }
  59682. } else {
  59683. currentDep += char;
  59684. }
  59685. break;
  59686. case 3 /* inTemplateString */:
  59687. if (char === "`") {
  59688. addDep(i);
  59689. if (prevState === 0 /* inCall */) {
  59690. return false;
  59691. } else {
  59692. state = prevState;
  59693. }
  59694. } else if (char === "$" && code.charAt(i + 1) === "{") {
  59695. error(i);
  59696. } else {
  59697. currentDep += char;
  59698. }
  59699. break;
  59700. default:
  59701. throw new Error("unknown import.meta.hot lexer state");
  59702. }
  59703. }
  59704. return false;
  59705. }
  59706. function lexAcceptedHmrExports(code, start, exportNames) {
  59707. const urls = /* @__PURE__ */ new Set();
  59708. lexAcceptedHmrDeps(code, start, urls);
  59709. for (const { url } of urls) {
  59710. exportNames.add(url);
  59711. }
  59712. return urls.size > 0;
  59713. }
  59714. function normalizeHmrUrl(url) {
  59715. if (url[0] !== "." && url[0] !== "/") {
  59716. url = wrapId$1(url);
  59717. }
  59718. return url;
  59719. }
  59720. function error(pos) {
  59721. const err = new Error(
  59722. `import.meta.hot.accept() can only accept string literals or an Array of string literals.`
  59723. );
  59724. err.pos = pos;
  59725. throw err;
  59726. }
  59727. async function readModifiedFile(file) {
  59728. const content = await fsp.readFile(file, "utf-8");
  59729. if (!content) {
  59730. const mtime = (await fsp.stat(file)).mtimeMs;
  59731. for (let n = 0; n < 10; n++) {
  59732. await new Promise((r) => setTimeout(r, 10));
  59733. const newMtime = (await fsp.stat(file)).mtimeMs;
  59734. if (newMtime !== mtime) {
  59735. break;
  59736. }
  59737. }
  59738. return await fsp.readFile(file, "utf-8");
  59739. } else {
  59740. return content;
  59741. }
  59742. }
  59743. function createHMRBroadcaster() {
  59744. const channels = [];
  59745. const readyChannels = /* @__PURE__ */ new WeakSet();
  59746. const broadcaster = {
  59747. get channels() {
  59748. return [...channels];
  59749. },
  59750. addChannel(channel) {
  59751. if (channels.some((c) => c.name === channel.name)) {
  59752. throw new Error(`HMR channel "${channel.name}" is already defined.`);
  59753. }
  59754. channels.push(channel);
  59755. return broadcaster;
  59756. },
  59757. on(event, listener) {
  59758. if (event === "connection") {
  59759. const channels2 = this.channels;
  59760. channels2.forEach(
  59761. (channel) => channel.on("connection", () => {
  59762. readyChannels.add(channel);
  59763. if (channels2.every((c) => readyChannels.has(c))) {
  59764. listener();
  59765. }
  59766. })
  59767. );
  59768. return;
  59769. }
  59770. channels.forEach((channel) => channel.on(event, listener));
  59771. return;
  59772. },
  59773. off(event, listener) {
  59774. channels.forEach((channel) => channel.off(event, listener));
  59775. return;
  59776. },
  59777. send(...args) {
  59778. channels.forEach((channel) => channel.send(...args));
  59779. },
  59780. listen() {
  59781. channels.forEach((channel) => channel.listen());
  59782. },
  59783. close() {
  59784. return Promise.all(channels.map((channel) => channel.close()));
  59785. }
  59786. };
  59787. return broadcaster;
  59788. }
  59789. function createServerHMRChannel() {
  59790. const innerEmitter = new EventEmitter$4();
  59791. const outsideEmitter = new EventEmitter$4();
  59792. return {
  59793. name: "ssr",
  59794. send(...args) {
  59795. let payload;
  59796. if (typeof args[0] === "string") {
  59797. payload = {
  59798. type: "custom",
  59799. event: args[0],
  59800. data: args[1]
  59801. };
  59802. } else {
  59803. payload = args[0];
  59804. }
  59805. outsideEmitter.emit("send", payload);
  59806. },
  59807. off(event, listener) {
  59808. innerEmitter.off(event, listener);
  59809. },
  59810. on: (event, listener) => {
  59811. innerEmitter.on(event, listener);
  59812. },
  59813. close() {
  59814. innerEmitter.removeAllListeners();
  59815. outsideEmitter.removeAllListeners();
  59816. },
  59817. listen() {
  59818. innerEmitter.emit("connection");
  59819. },
  59820. api: {
  59821. innerEmitter,
  59822. outsideEmitter
  59823. }
  59824. };
  59825. }
  59826. const debug$1 = createDebugger("vite:import-analysis");
  59827. const clientDir = normalizePath$3(CLIENT_DIR);
  59828. const skipRE = /\.(?:map|json)(?:$|\?)/;
  59829. const canSkipImportAnalysis = (id) => skipRE.test(id) || isDirectCSSRequest(id);
  59830. const optimizedDepChunkRE = /\/chunk-[A-Z\d]{8}\.js/;
  59831. const optimizedDepDynamicRE = /-[A-Z\d]{8}\.js/;
  59832. const hasViteIgnoreRE = /\/\*\s*@vite-ignore\s*\*\//;
  59833. const urlIsStringRE = /^(?:'.*'|".*"|`.*`)$/;
  59834. const templateLiteralRE = /^\s*`(.*)`\s*$/;
  59835. function isExplicitImportRequired(url) {
  59836. return !isJSRequest(url) && !isCSSRequest(url);
  59837. }
  59838. function extractImportedBindings(id, source, importSpec, importedBindings) {
  59839. let bindings = importedBindings.get(id);
  59840. if (!bindings) {
  59841. bindings = /* @__PURE__ */ new Set();
  59842. importedBindings.set(id, bindings);
  59843. }
  59844. const isDynamic = importSpec.d > -1;
  59845. const isMeta = importSpec.d === -2;
  59846. if (isDynamic || isMeta) {
  59847. bindings.add("*");
  59848. return;
  59849. }
  59850. const exp = source.slice(importSpec.ss, importSpec.se);
  59851. ESM_STATIC_IMPORT_RE.lastIndex = 0;
  59852. const match = ESM_STATIC_IMPORT_RE.exec(exp);
  59853. if (!match) {
  59854. return;
  59855. }
  59856. const staticImport = {
  59857. type: "static",
  59858. code: match[0],
  59859. start: match.index,
  59860. end: match.index + match[0].length,
  59861. imports: match.groups.imports,
  59862. specifier: match.groups.specifier
  59863. };
  59864. const parsed = parseStaticImport(staticImport);
  59865. if (!parsed) {
  59866. return;
  59867. }
  59868. if (parsed.namespacedImport) {
  59869. bindings.add("*");
  59870. }
  59871. if (parsed.defaultImport) {
  59872. bindings.add("default");
  59873. }
  59874. if (parsed.namedImports) {
  59875. for (const name of Object.keys(parsed.namedImports)) {
  59876. bindings.add(name);
  59877. }
  59878. }
  59879. }
  59880. function importAnalysisPlugin(config) {
  59881. const { root, base } = config;
  59882. const fsUtils = getFsUtils(config);
  59883. const clientPublicPath = path$n.posix.join(base, CLIENT_PUBLIC_PATH);
  59884. const enablePartialAccept = config.experimental?.hmrPartialAccept;
  59885. const matchAlias = getAliasPatternMatcher(config.resolve.alias);
  59886. let server;
  59887. let _env;
  59888. let _ssrEnv;
  59889. function getEnv(ssr) {
  59890. if (!_ssrEnv || !_env) {
  59891. const importMetaEnvKeys = {};
  59892. const userDefineEnv = {};
  59893. for (const key in config.env) {
  59894. importMetaEnvKeys[key] = JSON.stringify(config.env[key]);
  59895. }
  59896. for (const key in config.define) {
  59897. if (key.startsWith("import.meta.env.")) {
  59898. userDefineEnv[key.slice(16)] = config.define[key];
  59899. }
  59900. }
  59901. const env = `import.meta.env = ${serializeDefine({
  59902. ...importMetaEnvKeys,
  59903. SSR: "__vite_ssr__",
  59904. ...userDefineEnv
  59905. })};`;
  59906. _ssrEnv = env.replace("__vite_ssr__", "true");
  59907. _env = env.replace("__vite_ssr__", "false");
  59908. }
  59909. return ssr ? _ssrEnv : _env;
  59910. }
  59911. return {
  59912. name: "vite:import-analysis",
  59913. configureServer(_server) {
  59914. server = _server;
  59915. },
  59916. async transform(source, importer, options) {
  59917. if (!server) {
  59918. return null;
  59919. }
  59920. const ssr = options?.ssr === true;
  59921. if (canSkipImportAnalysis(importer)) {
  59922. debug$1?.(colors$1.dim(`[skipped] ${prettifyUrl(importer, root)}`));
  59923. return null;
  59924. }
  59925. const msAtStart = debug$1 ? performance$1.now() : 0;
  59926. await init;
  59927. let imports;
  59928. let exports;
  59929. source = stripBomTag(source);
  59930. try {
  59931. [imports, exports] = parse$d(source);
  59932. } catch (_e) {
  59933. const e = _e;
  59934. const { message, showCodeFrame } = createParseErrorInfo(
  59935. importer,
  59936. source
  59937. );
  59938. this.error(message, showCodeFrame ? e.idx : void 0);
  59939. }
  59940. const depsOptimizer = getDepsOptimizer(config, ssr);
  59941. const { moduleGraph } = server;
  59942. const importerModule = moduleGraph.getModuleById(importer);
  59943. if (!importerModule) {
  59944. throwOutdatedRequest(importer);
  59945. }
  59946. if (!imports.length && !this._addedImports) {
  59947. importerModule.isSelfAccepting = false;
  59948. debug$1?.(
  59949. `${timeFrom(msAtStart)} ${colors$1.dim(
  59950. `[no imports] ${prettifyUrl(importer, root)}`
  59951. )}`
  59952. );
  59953. return source;
  59954. }
  59955. let hasHMR = false;
  59956. let isSelfAccepting = false;
  59957. let hasEnv = false;
  59958. let needQueryInjectHelper = false;
  59959. let s;
  59960. const str = () => s || (s = new MagicString(source));
  59961. let isPartiallySelfAccepting = false;
  59962. const importedBindings = enablePartialAccept ? /* @__PURE__ */ new Map() : null;
  59963. const toAbsoluteUrl = (url) => path$n.posix.resolve(path$n.posix.dirname(importerModule.url), url);
  59964. const normalizeUrl = async (url, pos, forceSkipImportAnalysis = false) => {
  59965. url = stripBase(url, base);
  59966. let importerFile = importer;
  59967. const optimizeDeps = getDepOptimizationConfig(config, ssr);
  59968. if (moduleListContains(optimizeDeps?.exclude, url)) {
  59969. if (depsOptimizer) {
  59970. await depsOptimizer.scanProcessing;
  59971. for (const optimizedModule of depsOptimizer.metadata.depInfoList) {
  59972. if (!optimizedModule.src) continue;
  59973. if (optimizedModule.file === importerModule.file) {
  59974. importerFile = optimizedModule.src;
  59975. }
  59976. }
  59977. }
  59978. }
  59979. const resolved = await this.resolve(url, importerFile);
  59980. if (!resolved || resolved.meta?.["vite:alias"]?.noResolved) {
  59981. if (ssr) {
  59982. return [url, url];
  59983. }
  59984. importerModule.isSelfAccepting = false;
  59985. moduleGraph._hasResolveFailedErrorModules.add(importerModule);
  59986. return this.error(
  59987. `Failed to resolve import "${url}" from "${normalizePath$3(
  59988. path$n.relative(process.cwd(), importerFile)
  59989. )}". Does the file exist?`,
  59990. pos
  59991. );
  59992. }
  59993. if (isExternalUrl(resolved.id)) {
  59994. return [resolved.id, resolved.id];
  59995. }
  59996. const isRelative = url[0] === ".";
  59997. const isSelfImport = !isRelative && cleanUrl(url) === cleanUrl(importer);
  59998. if (resolved.id.startsWith(withTrailingSlash(root))) {
  59999. url = resolved.id.slice(root.length);
  60000. } else if (depsOptimizer?.isOptimizedDepFile(resolved.id) || // vite-plugin-react isn't following the leading \0 virtual module convention.
  60001. // This is a temporary hack to avoid expensive fs checks for React apps.
  60002. // We'll remove this as soon we're able to fix the react plugins.
  60003. resolved.id !== "/@react-refresh" && path$n.isAbsolute(resolved.id) && fsUtils.existsSync(cleanUrl(resolved.id))) {
  60004. url = path$n.posix.join(FS_PREFIX, resolved.id);
  60005. } else {
  60006. url = resolved.id;
  60007. }
  60008. if (url[0] !== "." && url[0] !== "/") {
  60009. url = wrapId$1(resolved.id);
  60010. }
  60011. if (!ssr) {
  60012. if (isExplicitImportRequired(url)) {
  60013. url = injectQuery(url, "import");
  60014. } else if ((isRelative || isSelfImport) && !DEP_VERSION_RE.test(url)) {
  60015. const versionMatch = DEP_VERSION_RE.exec(importer);
  60016. if (versionMatch) {
  60017. url = injectQuery(url, versionMatch[1]);
  60018. }
  60019. }
  60020. try {
  60021. const depModule = await moduleGraph._ensureEntryFromUrl(
  60022. unwrapId$1(url),
  60023. ssr,
  60024. canSkipImportAnalysis(url) || forceSkipImportAnalysis,
  60025. resolved
  60026. );
  60027. if (depModule.lastHMRTimestamp > 0) {
  60028. url = injectQuery(url, `t=${depModule.lastHMRTimestamp}`);
  60029. }
  60030. } catch (e) {
  60031. e.pos = pos;
  60032. throw e;
  60033. }
  60034. url = joinUrlSegments(base, url);
  60035. }
  60036. return [url, resolved.id];
  60037. };
  60038. const orderedImportedUrls = new Array(imports.length);
  60039. const orderedAcceptedUrls = new Array(
  60040. imports.length
  60041. );
  60042. const orderedAcceptedExports = new Array(
  60043. imports.length
  60044. );
  60045. await Promise.all(
  60046. imports.map(async (importSpecifier, index) => {
  60047. const {
  60048. s: start,
  60049. e: end,
  60050. ss: expStart,
  60051. se: expEnd,
  60052. d: dynamicIndex,
  60053. a: attributeIndex
  60054. } = importSpecifier;
  60055. let specifier = importSpecifier.n;
  60056. const rawUrl = source.slice(start, end);
  60057. if (rawUrl === "import.meta") {
  60058. const prop = source.slice(end, end + 4);
  60059. if (prop === ".hot") {
  60060. hasHMR = true;
  60061. const endHot = end + 4 + (source[end + 4] === "?" ? 1 : 0);
  60062. if (source.slice(endHot, endHot + 7) === ".accept") {
  60063. if (source.slice(endHot, endHot + 14) === ".acceptExports") {
  60064. const importAcceptedExports = orderedAcceptedExports[index] = /* @__PURE__ */ new Set();
  60065. lexAcceptedHmrExports(
  60066. source,
  60067. source.indexOf("(", endHot + 14) + 1,
  60068. importAcceptedExports
  60069. );
  60070. isPartiallySelfAccepting = true;
  60071. } else {
  60072. const importAcceptedUrls = orderedAcceptedUrls[index] = /* @__PURE__ */ new Set();
  60073. if (lexAcceptedHmrDeps(
  60074. source,
  60075. source.indexOf("(", endHot + 7) + 1,
  60076. importAcceptedUrls
  60077. )) {
  60078. isSelfAccepting = true;
  60079. }
  60080. }
  60081. }
  60082. } else if (prop === ".env") {
  60083. hasEnv = true;
  60084. }
  60085. return;
  60086. } else if (templateLiteralRE.test(rawUrl)) {
  60087. if (!(rawUrl.includes("${") && rawUrl.includes("}"))) {
  60088. specifier = rawUrl.replace(templateLiteralRE, "$1");
  60089. }
  60090. }
  60091. const isDynamicImport = dynamicIndex > -1;
  60092. if (!isDynamicImport && attributeIndex > -1) {
  60093. str().remove(end + 1, expEnd);
  60094. }
  60095. if (specifier !== void 0) {
  60096. if (isExternalUrl(specifier) || isDataUrl(specifier)) {
  60097. return;
  60098. }
  60099. if (ssr && !matchAlias(specifier)) {
  60100. if (shouldExternalizeForSSR(specifier, importer, config)) {
  60101. return;
  60102. }
  60103. if (isBuiltin(specifier)) {
  60104. return;
  60105. }
  60106. }
  60107. if (specifier === clientPublicPath) {
  60108. return;
  60109. }
  60110. if (specifier[0] === "/" && !(config.assetsInclude(cleanUrl(specifier)) || urlRE$1.test(specifier)) && checkPublicFile(specifier, config)) {
  60111. throw new Error(
  60112. `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(
  60113. specifier,
  60114. "url"
  60115. )} instead.`
  60116. );
  60117. }
  60118. const [url, resolvedId] = await normalizeUrl(specifier, start);
  60119. server?.moduleGraph.safeModulesPath.add(
  60120. fsPathFromUrl(stripBase(url, base))
  60121. );
  60122. if (url !== specifier) {
  60123. let rewriteDone = false;
  60124. if (depsOptimizer?.isOptimizedDepFile(resolvedId) && !optimizedDepChunkRE.test(resolvedId)) {
  60125. const file = cleanUrl(resolvedId);
  60126. const needsInterop = await optimizedDepNeedsInterop(
  60127. depsOptimizer.metadata,
  60128. file,
  60129. config,
  60130. ssr
  60131. );
  60132. if (needsInterop === void 0) {
  60133. if (!optimizedDepDynamicRE.test(file)) {
  60134. config.logger.error(
  60135. colors$1.red(
  60136. `Vite Error, ${url} optimized info should be defined`
  60137. )
  60138. );
  60139. }
  60140. } else if (needsInterop) {
  60141. debug$1?.(`${url} needs interop`);
  60142. interopNamedImports(
  60143. str(),
  60144. importSpecifier,
  60145. url,
  60146. index,
  60147. importer,
  60148. config
  60149. );
  60150. rewriteDone = true;
  60151. }
  60152. } else if (url.includes(browserExternalId) && source.slice(expStart, start).includes("{")) {
  60153. interopNamedImports(
  60154. str(),
  60155. importSpecifier,
  60156. url,
  60157. index,
  60158. importer,
  60159. config
  60160. );
  60161. rewriteDone = true;
  60162. }
  60163. if (!rewriteDone) {
  60164. const rewrittenUrl = JSON.stringify(url);
  60165. const s2 = isDynamicImport ? start : start - 1;
  60166. const e = isDynamicImport ? end : end + 1;
  60167. str().overwrite(s2, e, rewrittenUrl, {
  60168. contentOnly: true
  60169. });
  60170. }
  60171. }
  60172. const hmrUrl = unwrapId$1(stripBase(url, base));
  60173. const isLocalImport = !isExternalUrl(hmrUrl) && !isDataUrl(hmrUrl);
  60174. if (isLocalImport) {
  60175. orderedImportedUrls[index] = hmrUrl;
  60176. }
  60177. if (enablePartialAccept && importedBindings) {
  60178. extractImportedBindings(
  60179. resolvedId,
  60180. source,
  60181. importSpecifier,
  60182. importedBindings
  60183. );
  60184. }
  60185. if (!isDynamicImport && isLocalImport && config.server.preTransformRequests) {
  60186. const url2 = removeImportQuery(hmrUrl);
  60187. server.warmupRequest(url2, { ssr });
  60188. }
  60189. } else if (!importer.startsWith(withTrailingSlash(clientDir))) {
  60190. if (!isInNodeModules$1(importer)) {
  60191. const hasViteIgnore = hasViteIgnoreRE.test(
  60192. // complete expression inside parens
  60193. source.slice(dynamicIndex + 1, end)
  60194. );
  60195. if (!hasViteIgnore) {
  60196. this.warn(
  60197. `
  60198. ` + colors$1.cyan(importerModule.file) + `
  60199. ` + colors$1.reset(generateCodeFrame(source, start, end)) + colors$1.yellow(
  60200. `
  60201. The above dynamic import cannot be analyzed by Vite.
  60202. See ${colors$1.blue(
  60203. `https://github.com/rollup/plugins/tree/master/packages/dynamic-import-vars#limitations`
  60204. )} 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.
  60205. `
  60206. )
  60207. );
  60208. }
  60209. }
  60210. if (!ssr) {
  60211. if (!urlIsStringRE.test(rawUrl) || isExplicitImportRequired(rawUrl.slice(1, -1))) {
  60212. needQueryInjectHelper = true;
  60213. str().overwrite(
  60214. start,
  60215. end,
  60216. `__vite__injectQuery(${rawUrl}, 'import')`,
  60217. { contentOnly: true }
  60218. );
  60219. }
  60220. }
  60221. }
  60222. })
  60223. );
  60224. const _orderedImportedUrls = orderedImportedUrls.filter(isDefined);
  60225. const importedUrls = new Set(_orderedImportedUrls);
  60226. const staticImportedUrls = new Set(
  60227. _orderedImportedUrls.map((url) => removeTimestampQuery(url))
  60228. );
  60229. const acceptedUrls = mergeAcceptedUrls(orderedAcceptedUrls);
  60230. const acceptedExports = mergeAcceptedUrls(orderedAcceptedExports);
  60231. const isClassicWorker = importer.includes(WORKER_FILE_ID) && importer.includes("type=classic");
  60232. if (hasEnv && !isClassicWorker) {
  60233. str().prepend(getEnv(ssr));
  60234. }
  60235. if (hasHMR && !ssr && !isClassicWorker) {
  60236. debugHmr?.(
  60237. `${isSelfAccepting ? `[self-accepts]` : isPartiallySelfAccepting ? `[accepts-exports]` : acceptedUrls.size ? `[accepts-deps]` : `[detected api usage]`} ${prettifyUrl(importer, root)}`
  60238. );
  60239. str().prepend(
  60240. `import { createHotContext as __vite__createHotContext } from "${clientPublicPath}";import.meta.hot = __vite__createHotContext(${JSON.stringify(
  60241. normalizeHmrUrl(importerModule.url)
  60242. )});`
  60243. );
  60244. }
  60245. if (needQueryInjectHelper) {
  60246. if (isClassicWorker) {
  60247. str().append("\n" + __vite__injectQuery.toString());
  60248. } else {
  60249. str().prepend(
  60250. `import { injectQuery as __vite__injectQuery } from "${clientPublicPath}";`
  60251. );
  60252. }
  60253. }
  60254. const normalizedAcceptedUrls = /* @__PURE__ */ new Set();
  60255. for (const { url, start, end } of acceptedUrls) {
  60256. const [normalized] = await moduleGraph.resolveUrl(
  60257. toAbsoluteUrl(url),
  60258. ssr
  60259. );
  60260. normalizedAcceptedUrls.add(normalized);
  60261. str().overwrite(start, end, JSON.stringify(normalized), {
  60262. contentOnly: true
  60263. });
  60264. }
  60265. if (!isCSSRequest(importer) || SPECIAL_QUERY_RE.test(importer)) {
  60266. const pluginImports = this._addedImports;
  60267. if (pluginImports) {
  60268. (await Promise.all(
  60269. [...pluginImports].map((id) => normalizeUrl(id, 0, true))
  60270. )).forEach(([url]) => importedUrls.add(url));
  60271. }
  60272. if (ssr && importerModule.isSelfAccepting) {
  60273. isSelfAccepting = true;
  60274. }
  60275. if (!isSelfAccepting && isPartiallySelfAccepting && acceptedExports.size >= exports.length && exports.every((e) => acceptedExports.has(e.n))) {
  60276. isSelfAccepting = true;
  60277. }
  60278. const prunedImports = await moduleGraph.updateModuleInfo(
  60279. importerModule,
  60280. importedUrls,
  60281. importedBindings,
  60282. normalizedAcceptedUrls,
  60283. isPartiallySelfAccepting ? acceptedExports : null,
  60284. isSelfAccepting,
  60285. ssr,
  60286. staticImportedUrls
  60287. );
  60288. if (hasHMR && prunedImports) {
  60289. handlePrunedModules(prunedImports, server);
  60290. }
  60291. }
  60292. debug$1?.(
  60293. `${timeFrom(msAtStart)} ${colors$1.dim(
  60294. `[${importedUrls.size} imports rewritten] ${prettifyUrl(
  60295. importer,
  60296. root
  60297. )}`
  60298. )}`
  60299. );
  60300. if (s) {
  60301. return transformStableResult(s, importer, config);
  60302. } else {
  60303. return source;
  60304. }
  60305. }
  60306. };
  60307. }
  60308. function mergeAcceptedUrls(orderedUrls) {
  60309. const acceptedUrls = /* @__PURE__ */ new Set();
  60310. for (const urls of orderedUrls) {
  60311. if (!urls) continue;
  60312. for (const url of urls) acceptedUrls.add(url);
  60313. }
  60314. return acceptedUrls;
  60315. }
  60316. function createParseErrorInfo(importer, source) {
  60317. const isVue = importer.endsWith(".vue");
  60318. const isJsx = importer.endsWith(".jsx") || importer.endsWith(".tsx");
  60319. const maybeJSX = !isVue && isJSRequest(importer);
  60320. const probablyBinary = source.includes(
  60321. "\uFFFD"
  60322. );
  60323. 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(
  60324. importer
  60325. )} file format, or if it's an asset, add "**/*${path$n.extname(
  60326. importer
  60327. )}" to \`assetsInclude\` in your configuration.`;
  60328. return {
  60329. message: `Failed to parse source for import analysis because the content contains invalid JS syntax. ` + msg,
  60330. showCodeFrame: !probablyBinary
  60331. };
  60332. }
  60333. const interopHelper = (m) => m?.__esModule ? m : { ...typeof m === "object" && !Array.isArray(m) || typeof m === "function" ? m : {}, default: m };
  60334. function interopNamedImports(str, importSpecifier, rewrittenUrl, importIndex, importer, config) {
  60335. const source = str.original;
  60336. const {
  60337. s: start,
  60338. e: end,
  60339. ss: expStart,
  60340. se: expEnd,
  60341. d: dynamicIndex
  60342. } = importSpecifier;
  60343. const exp = source.slice(expStart, expEnd);
  60344. if (dynamicIndex > -1) {
  60345. str.overwrite(
  60346. expStart,
  60347. expEnd,
  60348. `import('${rewrittenUrl}').then(m => (${interopHelper.toString()})(m.default))` + getLineBreaks(exp),
  60349. { contentOnly: true }
  60350. );
  60351. } else {
  60352. const rawUrl = source.slice(start, end);
  60353. const rewritten = transformCjsImport(
  60354. exp,
  60355. rewrittenUrl,
  60356. rawUrl,
  60357. importIndex,
  60358. importer,
  60359. config
  60360. );
  60361. if (rewritten) {
  60362. str.overwrite(expStart, expEnd, rewritten + getLineBreaks(exp), {
  60363. contentOnly: true
  60364. });
  60365. } else {
  60366. str.overwrite(
  60367. start,
  60368. end,
  60369. rewrittenUrl + getLineBreaks(source.slice(start, end)),
  60370. {
  60371. contentOnly: true
  60372. }
  60373. );
  60374. }
  60375. }
  60376. }
  60377. function getLineBreaks(str) {
  60378. return str.includes("\n") ? "\n".repeat(str.split("\n").length - 1) : "";
  60379. }
  60380. function transformCjsImport(importExp, url, rawUrl, importIndex, importer, config) {
  60381. const node = parseAst(importExp).body[0];
  60382. if (config.command === "serve" && node.type === "ExportAllDeclaration" && !node.exported) {
  60383. config.logger.warn(
  60384. colors$1.yellow(
  60385. `
  60386. 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}"\``
  60387. )
  60388. );
  60389. } else if (node.type === "ImportDeclaration" || node.type === "ExportNamedDeclaration") {
  60390. if (!node.specifiers.length) {
  60391. return `import "${url}"`;
  60392. }
  60393. const importNames = [];
  60394. const exportNames = [];
  60395. let defaultExports = "";
  60396. for (const spec of node.specifiers) {
  60397. if (spec.type === "ImportSpecifier" && spec.imported.type === "Identifier") {
  60398. const importedName = spec.imported.name;
  60399. const localName = spec.local.name;
  60400. importNames.push({ importedName, localName });
  60401. } else if (spec.type === "ImportDefaultSpecifier") {
  60402. importNames.push({
  60403. importedName: "default",
  60404. localName: spec.local.name
  60405. });
  60406. } else if (spec.type === "ImportNamespaceSpecifier") {
  60407. importNames.push({ importedName: "*", localName: spec.local.name });
  60408. } else if (spec.type === "ExportSpecifier" && spec.exported.type === "Identifier") {
  60409. const importedName = spec.local.name;
  60410. const exportedName = spec.exported.name;
  60411. if (exportedName === "default") {
  60412. defaultExports = makeLegalIdentifier(
  60413. `__vite__cjsExportDefault_${importIndex}`
  60414. );
  60415. importNames.push({ importedName, localName: defaultExports });
  60416. } else {
  60417. const localName = makeLegalIdentifier(
  60418. `__vite__cjsExport_${exportedName}`
  60419. );
  60420. importNames.push({ importedName, localName });
  60421. exportNames.push(`${localName} as ${exportedName}`);
  60422. }
  60423. }
  60424. }
  60425. const cjsModuleName = makeLegalIdentifier(
  60426. `__vite__cjsImport${importIndex}_${rawUrl}`
  60427. );
  60428. const lines = [`import ${cjsModuleName} from "${url}"`];
  60429. importNames.forEach(({ importedName, localName }) => {
  60430. if (importedName === "*") {
  60431. lines.push(
  60432. `const ${localName} = (${interopHelper.toString()})(${cjsModuleName})`
  60433. );
  60434. } else if (importedName === "default") {
  60435. lines.push(
  60436. `const ${localName} = ${cjsModuleName}.__esModule ? ${cjsModuleName}.default : ${cjsModuleName}`
  60437. );
  60438. } else {
  60439. lines.push(`const ${localName} = ${cjsModuleName}["${importedName}"]`);
  60440. }
  60441. });
  60442. if (defaultExports) {
  60443. lines.push(`export default ${defaultExports}`);
  60444. }
  60445. if (exportNames.length) {
  60446. lines.push(`export { ${exportNames.join(", ")} }`);
  60447. }
  60448. return lines.join("; ");
  60449. }
  60450. }
  60451. function __vite__injectQuery(url, queryToInject) {
  60452. if (url[0] !== "." && url[0] !== "/") {
  60453. return url;
  60454. }
  60455. const pathname = url.replace(/[?#].*$/, "");
  60456. const { search, hash } = new URL(url, "http://vite.dev");
  60457. return `${pathname}?${queryToInject}${search ? `&` + search.slice(1) : ""}${hash || ""}`;
  60458. }
  60459. const isModernFlag = `__VITE_IS_MODERN__`;
  60460. const preloadMethod = `__vitePreload`;
  60461. const preloadMarker = `__VITE_PRELOAD__`;
  60462. const preloadHelperId = "\0vite/preload-helper.js";
  60463. const preloadMarkerRE = new RegExp(preloadMarker, "g");
  60464. const dynamicImportPrefixRE = /import\s*\(/;
  60465. 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;
  60466. function toRelativePath(filename, importer) {
  60467. const relPath = path$n.posix.relative(path$n.posix.dirname(importer), filename);
  60468. return relPath[0] === "." ? relPath : `./${relPath}`;
  60469. }
  60470. function indexOfMatchInSlice(str, reg, pos = 0) {
  60471. reg.lastIndex = pos;
  60472. const result = reg.exec(str);
  60473. return result?.index ?? -1;
  60474. }
  60475. function detectScriptRel() {
  60476. const relList = typeof document !== "undefined" && document.createElement("link").relList;
  60477. return relList && relList.supports && relList.supports("modulepreload") ? "modulepreload" : "preload";
  60478. }
  60479. function preload(baseModule, deps, importerUrl) {
  60480. let promise = Promise.resolve();
  60481. if (__VITE_IS_MODERN__ && deps && deps.length > 0) {
  60482. const links = document.getElementsByTagName("link");
  60483. const cspNonceMeta = document.querySelector(
  60484. "meta[property=csp-nonce]"
  60485. );
  60486. const cspNonce = cspNonceMeta?.nonce || cspNonceMeta?.getAttribute("nonce");
  60487. promise = Promise.allSettled(
  60488. deps.map((dep) => {
  60489. dep = assetsURL(dep, importerUrl);
  60490. if (dep in seen) return;
  60491. seen[dep] = true;
  60492. const isCss = dep.endsWith(".css");
  60493. const cssSelector = isCss ? '[rel="stylesheet"]' : "";
  60494. const isBaseRelative = !!importerUrl;
  60495. if (isBaseRelative) {
  60496. for (let i = links.length - 1; i >= 0; i--) {
  60497. const link2 = links[i];
  60498. if (link2.href === dep && (!isCss || link2.rel === "stylesheet")) {
  60499. return;
  60500. }
  60501. }
  60502. } else if (document.querySelector(`link[href="${dep}"]${cssSelector}`)) {
  60503. return;
  60504. }
  60505. const link = document.createElement("link");
  60506. link.rel = isCss ? "stylesheet" : scriptRel;
  60507. if (!isCss) {
  60508. link.as = "script";
  60509. }
  60510. link.crossOrigin = "";
  60511. link.href = dep;
  60512. if (cspNonce) {
  60513. link.setAttribute("nonce", cspNonce);
  60514. }
  60515. document.head.appendChild(link);
  60516. if (isCss) {
  60517. return new Promise((res, rej) => {
  60518. link.addEventListener("load", res);
  60519. link.addEventListener(
  60520. "error",
  60521. () => rej(new Error(`Unable to preload CSS for ${dep}`))
  60522. );
  60523. });
  60524. }
  60525. })
  60526. );
  60527. }
  60528. function handlePreloadError(err) {
  60529. const e = new Event("vite:preloadError", {
  60530. cancelable: true
  60531. });
  60532. e.payload = err;
  60533. window.dispatchEvent(e);
  60534. if (!e.defaultPrevented) {
  60535. throw err;
  60536. }
  60537. }
  60538. return promise.then((res) => {
  60539. for (const item of res || []) {
  60540. if (item.status !== "rejected") continue;
  60541. handlePreloadError(item.reason);
  60542. }
  60543. return baseModule().catch(handlePreloadError);
  60544. });
  60545. }
  60546. function buildImportAnalysisPlugin(config) {
  60547. const ssr = !!config.build.ssr;
  60548. const isWorker = config.isWorker;
  60549. const insertPreload = !(ssr || !!config.build.lib || isWorker);
  60550. const renderBuiltUrl = config.experimental.renderBuiltUrl;
  60551. const isRelativeBase = config.base === "./" || config.base === "";
  60552. const { modulePreload } = config.build;
  60553. const scriptRel2 = modulePreload && modulePreload.polyfill ? `'modulepreload'` : `(${detectScriptRel.toString()})()`;
  60554. const assetsURL2 = renderBuiltUrl || isRelativeBase ? (
  60555. // If `experimental.renderBuiltUrl` is used, the dependencies might be relative to the current chunk.
  60556. // If relative base is used, the dependencies are relative to the current chunk.
  60557. // The importerUrl is passed as third parameter to __vitePreload in this case
  60558. `function(dep, importerUrl) { return new URL(dep, importerUrl).href }`
  60559. ) : (
  60560. // If the base isn't relative, then the deps are relative to the projects `outDir` and the base
  60561. // is appended inside __vitePreload too.
  60562. `function(dep) { return ${JSON.stringify(config.base)}+dep }`
  60563. );
  60564. const preloadCode = `const scriptRel = ${scriptRel2};const assetsURL = ${assetsURL2};const seen = {};export const ${preloadMethod} = ${preload.toString()}`;
  60565. return {
  60566. name: "vite:build-import-analysis",
  60567. resolveId(id) {
  60568. if (id === preloadHelperId) {
  60569. return id;
  60570. }
  60571. },
  60572. load(id) {
  60573. if (id === preloadHelperId) {
  60574. return preloadCode;
  60575. }
  60576. },
  60577. async transform(source, importer) {
  60578. if (isInNodeModules$1(importer) && !dynamicImportPrefixRE.test(source)) {
  60579. return;
  60580. }
  60581. await init;
  60582. let imports = [];
  60583. try {
  60584. imports = parse$d(source)[0];
  60585. } catch (_e) {
  60586. const e = _e;
  60587. const { message, showCodeFrame } = createParseErrorInfo(
  60588. importer,
  60589. source
  60590. );
  60591. this.error(message, showCodeFrame ? e.idx : void 0);
  60592. }
  60593. if (!imports.length) {
  60594. return null;
  60595. }
  60596. const dynamicImports = {};
  60597. if (insertPreload) {
  60598. let match;
  60599. while (match = dynamicImportTreeshakenRE.exec(source)) {
  60600. if (match[1]) {
  60601. dynamicImports[dynamicImportTreeshakenRE.lastIndex] = {
  60602. declaration: `const ${match[2]}`,
  60603. names: match[2]?.trim()
  60604. };
  60605. continue;
  60606. }
  60607. if (match[3]) {
  60608. let names2 = /\.([^.?]+)/.exec(match[4])?.[1] || "";
  60609. if (names2 === "default") {
  60610. names2 = "default: __vite_default__";
  60611. }
  60612. dynamicImports[dynamicImportTreeshakenRE.lastIndex - match[4]?.length - 1] = { declaration: `const {${names2}}`, names: `{ ${names2} }` };
  60613. continue;
  60614. }
  60615. const names = match[6]?.trim();
  60616. dynamicImports[dynamicImportTreeshakenRE.lastIndex - match[5]?.length] = { declaration: `const {${names}}`, names: `{ ${names} }` };
  60617. }
  60618. }
  60619. let s;
  60620. const str = () => s || (s = new MagicString(source));
  60621. let needPreloadHelper = false;
  60622. for (let index = 0; index < imports.length; index++) {
  60623. const {
  60624. s: start,
  60625. e: end,
  60626. ss: expStart,
  60627. se: expEnd,
  60628. d: dynamicIndex,
  60629. a: attributeIndex
  60630. } = imports[index];
  60631. const isDynamicImport = dynamicIndex > -1;
  60632. if (!isDynamicImport && attributeIndex > -1) {
  60633. str().remove(end + 1, expEnd);
  60634. }
  60635. if (isDynamicImport && insertPreload && // Only preload static urls
  60636. (source[start] === '"' || source[start] === "'" || source[start] === "`")) {
  60637. needPreloadHelper = true;
  60638. const { declaration, names } = dynamicImports[expEnd] || {};
  60639. if (names) {
  60640. str().prependLeft(
  60641. expStart,
  60642. `${preloadMethod}(async () => { ${declaration} = await `
  60643. );
  60644. str().appendRight(expEnd, `;return ${names}}`);
  60645. } else {
  60646. str().prependLeft(expStart, `${preloadMethod}(() => `);
  60647. }
  60648. str().appendRight(
  60649. expEnd,
  60650. `,${isModernFlag}?${preloadMarker}:void 0${renderBuiltUrl || isRelativeBase ? ",import.meta.url" : ""})`
  60651. );
  60652. }
  60653. }
  60654. if (needPreloadHelper && insertPreload && !source.includes(`const ${preloadMethod} =`)) {
  60655. str().prepend(`import { ${preloadMethod} } from "${preloadHelperId}";`);
  60656. }
  60657. if (s) {
  60658. return {
  60659. code: s.toString(),
  60660. map: config.build.sourcemap ? s.generateMap({ hires: "boundary" }) : null
  60661. };
  60662. }
  60663. },
  60664. renderChunk(code, _, { format }) {
  60665. if (code.indexOf(isModernFlag) > -1) {
  60666. const re = new RegExp(isModernFlag, "g");
  60667. const isModern = String(format === "es");
  60668. if (config.build.sourcemap) {
  60669. const s = new MagicString(code);
  60670. let match;
  60671. while (match = re.exec(code)) {
  60672. s.update(match.index, match.index + isModernFlag.length, isModern);
  60673. }
  60674. return {
  60675. code: s.toString(),
  60676. map: s.generateMap({ hires: "boundary" })
  60677. };
  60678. } else {
  60679. return code.replace(re, isModern);
  60680. }
  60681. }
  60682. return null;
  60683. },
  60684. generateBundle({ format }, bundle) {
  60685. if (format !== "es") {
  60686. return;
  60687. }
  60688. if (!insertPreload) {
  60689. const removedPureCssFiles = removedPureCssFilesCache.get(config);
  60690. if (removedPureCssFiles && removedPureCssFiles.size > 0) {
  60691. for (const file in bundle) {
  60692. const chunk = bundle[file];
  60693. if (chunk.type === "chunk" && chunk.code.includes("import")) {
  60694. const code = chunk.code;
  60695. let imports;
  60696. try {
  60697. imports = parse$d(code)[0].filter((i) => i.d > -1);
  60698. } catch (e) {
  60699. const loc = numberToPos(code, e.idx);
  60700. this.error({
  60701. name: e.name,
  60702. message: e.message,
  60703. stack: e.stack,
  60704. cause: e.cause,
  60705. pos: e.idx,
  60706. loc: { ...loc, file: chunk.fileName },
  60707. frame: generateCodeFrame(code, loc)
  60708. });
  60709. }
  60710. for (const imp of imports) {
  60711. const {
  60712. n: name,
  60713. s: start,
  60714. e: end,
  60715. ss: expStart,
  60716. se: expEnd
  60717. } = imp;
  60718. let url = name;
  60719. if (!url) {
  60720. const rawUrl = code.slice(start, end);
  60721. if (rawUrl[0] === `"` && rawUrl[rawUrl.length - 1] === `"`)
  60722. url = rawUrl.slice(1, -1);
  60723. }
  60724. if (!url) continue;
  60725. const normalizedFile = path$n.posix.join(
  60726. path$n.posix.dirname(chunk.fileName),
  60727. url
  60728. );
  60729. if (removedPureCssFiles.has(normalizedFile)) {
  60730. chunk.code = chunk.code.slice(0, expStart) + `Promise.resolve({${"".padEnd(expEnd - expStart - 19, " ")}})` + chunk.code.slice(expEnd);
  60731. }
  60732. }
  60733. }
  60734. }
  60735. }
  60736. return;
  60737. }
  60738. for (const file in bundle) {
  60739. const chunk = bundle[file];
  60740. if (chunk.type === "chunk" && chunk.code.indexOf(preloadMarker) > -1) {
  60741. const code = chunk.code;
  60742. let imports;
  60743. try {
  60744. imports = parse$d(code)[0].filter((i) => i.d > -1);
  60745. } catch (e) {
  60746. const loc = numberToPos(code, e.idx);
  60747. this.error({
  60748. name: e.name,
  60749. message: e.message,
  60750. stack: e.stack,
  60751. cause: e.cause,
  60752. pos: e.idx,
  60753. loc: { ...loc, file: chunk.fileName },
  60754. frame: generateCodeFrame(code, loc)
  60755. });
  60756. }
  60757. const s = new MagicString(code);
  60758. const rewroteMarkerStartPos = /* @__PURE__ */ new Set();
  60759. const fileDeps = [];
  60760. const addFileDep = (url, runtime = false) => {
  60761. const index = fileDeps.findIndex((dep) => dep.url === url);
  60762. if (index === -1) {
  60763. return fileDeps.push({ url, runtime }) - 1;
  60764. } else {
  60765. return index;
  60766. }
  60767. };
  60768. if (imports.length) {
  60769. for (let index = 0; index < imports.length; index++) {
  60770. const {
  60771. n: name,
  60772. s: start,
  60773. e: end,
  60774. ss: expStart,
  60775. se: expEnd
  60776. } = imports[index];
  60777. let url = name;
  60778. if (!url) {
  60779. const rawUrl = code.slice(start, end);
  60780. if (rawUrl[0] === `"` && rawUrl[rawUrl.length - 1] === `"`)
  60781. url = rawUrl.slice(1, -1);
  60782. }
  60783. const deps = /* @__PURE__ */ new Set();
  60784. let hasRemovedPureCssChunk = false;
  60785. let normalizedFile = void 0;
  60786. if (url) {
  60787. normalizedFile = path$n.posix.join(
  60788. path$n.posix.dirname(chunk.fileName),
  60789. url
  60790. );
  60791. const ownerFilename = chunk.fileName;
  60792. const analyzed = /* @__PURE__ */ new Set();
  60793. const addDeps = (filename) => {
  60794. if (filename === ownerFilename) return;
  60795. if (analyzed.has(filename)) return;
  60796. analyzed.add(filename);
  60797. const chunk2 = bundle[filename];
  60798. if (chunk2) {
  60799. deps.add(chunk2.fileName);
  60800. if (chunk2.type === "chunk") {
  60801. chunk2.imports.forEach(addDeps);
  60802. chunk2.viteMetadata.importedCss.forEach((file2) => {
  60803. deps.add(file2);
  60804. });
  60805. }
  60806. } else {
  60807. const removedPureCssFiles = removedPureCssFilesCache.get(config);
  60808. const chunk3 = removedPureCssFiles.get(filename);
  60809. if (chunk3) {
  60810. if (chunk3.viteMetadata.importedCss.size) {
  60811. chunk3.viteMetadata.importedCss.forEach((file2) => {
  60812. deps.add(file2);
  60813. });
  60814. hasRemovedPureCssChunk = true;
  60815. }
  60816. s.update(expStart, expEnd, "Promise.resolve({})");
  60817. }
  60818. }
  60819. };
  60820. addDeps(normalizedFile);
  60821. }
  60822. let markerStartPos2 = indexOfMatchInSlice(
  60823. code,
  60824. preloadMarkerRE,
  60825. end
  60826. );
  60827. if (markerStartPos2 === -1 && imports.length === 1) {
  60828. markerStartPos2 = indexOfMatchInSlice(code, preloadMarkerRE);
  60829. }
  60830. if (markerStartPos2 > 0) {
  60831. let depsArray = deps.size > 1 || // main chunk is removed
  60832. hasRemovedPureCssChunk && deps.size > 0 ? modulePreload === false ? (
  60833. // CSS deps use the same mechanism as module preloads, so even if disabled,
  60834. // we still need to pass these deps to the preload helper in dynamic imports.
  60835. [...deps].filter((d) => d.endsWith(".css"))
  60836. ) : [...deps] : [];
  60837. const resolveDependencies = modulePreload ? modulePreload.resolveDependencies : void 0;
  60838. if (resolveDependencies && normalizedFile) {
  60839. const cssDeps = [];
  60840. const otherDeps = [];
  60841. for (const dep of depsArray) {
  60842. (dep.endsWith(".css") ? cssDeps : otherDeps).push(dep);
  60843. }
  60844. depsArray = [
  60845. ...resolveDependencies(normalizedFile, otherDeps, {
  60846. hostId: file,
  60847. hostType: "js"
  60848. }),
  60849. ...cssDeps
  60850. ];
  60851. }
  60852. let renderedDeps;
  60853. if (renderBuiltUrl) {
  60854. renderedDeps = depsArray.map((dep) => {
  60855. const replacement = toOutputFilePathInJS(
  60856. dep,
  60857. "asset",
  60858. chunk.fileName,
  60859. "js",
  60860. config,
  60861. toRelativePath
  60862. );
  60863. if (typeof replacement === "string") {
  60864. return addFileDep(replacement);
  60865. }
  60866. return addFileDep(replacement.runtime, true);
  60867. });
  60868. } else {
  60869. renderedDeps = depsArray.map(
  60870. (d) => (
  60871. // Don't include the assets dir if the default asset file names
  60872. // are used, the path will be reconstructed by the import preload helper
  60873. isRelativeBase ? addFileDep(toRelativePath(d, file)) : addFileDep(d)
  60874. )
  60875. );
  60876. }
  60877. s.update(
  60878. markerStartPos2,
  60879. markerStartPos2 + preloadMarker.length,
  60880. renderedDeps.length > 0 ? `__vite__mapDeps([${renderedDeps.join(",")}])` : `[]`
  60881. );
  60882. rewroteMarkerStartPos.add(markerStartPos2);
  60883. }
  60884. }
  60885. }
  60886. if (fileDeps.length > 0) {
  60887. const fileDepsCode = `[${fileDeps.map(
  60888. (fileDep) => fileDep.runtime ? fileDep.url : JSON.stringify(fileDep.url)
  60889. ).join(",")}]`;
  60890. const mapDepsCode = `const __vite__mapDeps=(i,m=__vite__mapDeps,d=(m.f||(m.f=${fileDepsCode})))=>i.map(i=>d[i]);
  60891. `;
  60892. if (code.startsWith("#!")) {
  60893. s.prependLeft(code.indexOf("\n") + 1, mapDepsCode);
  60894. } else {
  60895. s.prepend(mapDepsCode);
  60896. }
  60897. }
  60898. let markerStartPos = indexOfMatchInSlice(code, preloadMarkerRE);
  60899. while (markerStartPos >= 0) {
  60900. if (!rewroteMarkerStartPos.has(markerStartPos)) {
  60901. s.update(
  60902. markerStartPos,
  60903. markerStartPos + preloadMarker.length,
  60904. "void 0"
  60905. );
  60906. }
  60907. markerStartPos = indexOfMatchInSlice(
  60908. code,
  60909. preloadMarkerRE,
  60910. markerStartPos + preloadMarker.length
  60911. );
  60912. }
  60913. if (s.hasChanged()) {
  60914. chunk.code = s.toString();
  60915. if (config.build.sourcemap && chunk.map) {
  60916. const nextMap = s.generateMap({
  60917. source: chunk.fileName,
  60918. hires: "boundary"
  60919. });
  60920. const map = combineSourcemaps(chunk.fileName, [
  60921. nextMap,
  60922. chunk.map
  60923. ]);
  60924. map.toUrl = () => genSourceMapUrl(map);
  60925. chunk.map = map;
  60926. if (config.build.sourcemap === "inline") {
  60927. chunk.code = chunk.code.replace(
  60928. convertSourceMap.mapFileCommentRegex,
  60929. ""
  60930. );
  60931. chunk.code += `
  60932. //# sourceMappingURL=${genSourceMapUrl(map)}`;
  60933. } else if (config.build.sourcemap) {
  60934. const mapAsset = bundle[chunk.fileName + ".map"];
  60935. if (mapAsset && mapAsset.type === "asset") {
  60936. mapAsset.source = map.toString();
  60937. }
  60938. }
  60939. }
  60940. }
  60941. }
  60942. }
  60943. }
  60944. };
  60945. }
  60946. function ssrManifestPlugin(config) {
  60947. const ssrManifest = {};
  60948. const base = config.base;
  60949. return {
  60950. name: "vite:ssr-manifest",
  60951. generateBundle(_options, bundle) {
  60952. for (const file in bundle) {
  60953. const chunk = bundle[file];
  60954. if (chunk.type === "chunk") {
  60955. for (const id in chunk.modules) {
  60956. const normalizedId = normalizePath$3(relative$2(config.root, id));
  60957. const mappedChunks = ssrManifest[normalizedId] ?? (ssrManifest[normalizedId] = []);
  60958. if (!chunk.isEntry) {
  60959. mappedChunks.push(joinUrlSegments(base, chunk.fileName));
  60960. chunk.viteMetadata.importedCss.forEach((file2) => {
  60961. mappedChunks.push(joinUrlSegments(base, file2));
  60962. });
  60963. }
  60964. chunk.viteMetadata.importedAssets.forEach((file2) => {
  60965. mappedChunks.push(joinUrlSegments(base, file2));
  60966. });
  60967. }
  60968. if (chunk.code.includes(preloadMethod)) {
  60969. const code = chunk.code;
  60970. let imports = [];
  60971. try {
  60972. imports = parse$d(code)[0].filter((i) => i.n && i.d > -1);
  60973. } catch (_e) {
  60974. const e = _e;
  60975. const loc = numberToPos(code, e.idx);
  60976. this.error({
  60977. name: e.name,
  60978. message: e.message,
  60979. stack: e.stack,
  60980. cause: e.cause,
  60981. pos: e.idx,
  60982. loc: { ...loc, file: chunk.fileName },
  60983. frame: generateCodeFrame(code, loc)
  60984. });
  60985. }
  60986. if (imports.length) {
  60987. for (let index = 0; index < imports.length; index++) {
  60988. const { s: start, e: end, n: name } = imports[index];
  60989. const url = code.slice(start, end);
  60990. const deps = [];
  60991. const ownerFilename = chunk.fileName;
  60992. const analyzed = /* @__PURE__ */ new Set();
  60993. const addDeps = (filename) => {
  60994. if (filename === ownerFilename) return;
  60995. if (analyzed.has(filename)) return;
  60996. analyzed.add(filename);
  60997. const chunk2 = bundle[filename];
  60998. if (chunk2) {
  60999. chunk2.viteMetadata.importedCss.forEach((file2) => {
  61000. deps.push(joinUrlSegments(base, file2));
  61001. });
  61002. chunk2.imports.forEach(addDeps);
  61003. }
  61004. };
  61005. const normalizedFile = normalizePath$3(
  61006. join$2(dirname$2(chunk.fileName), url.slice(1, -1))
  61007. );
  61008. addDeps(normalizedFile);
  61009. ssrManifest[basename$2(name)] = deps;
  61010. }
  61011. }
  61012. }
  61013. }
  61014. }
  61015. this.emitFile({
  61016. fileName: typeof config.build.ssrManifest === "string" ? config.build.ssrManifest : ".vite/ssr-manifest.json",
  61017. type: "asset",
  61018. source: JSON.stringify(sortObjectKeys(ssrManifest), void 0, 2)
  61019. });
  61020. }
  61021. };
  61022. }
  61023. function loadFallbackPlugin() {
  61024. return {
  61025. name: "vite:load-fallback",
  61026. async load(id) {
  61027. try {
  61028. const cleanedId = cleanUrl(id);
  61029. const content = await fsp.readFile(cleanedId, "utf-8");
  61030. this.addWatchFile(cleanedId);
  61031. return content;
  61032. } catch (e) {
  61033. const content = await fsp.readFile(id, "utf-8");
  61034. this.addWatchFile(id);
  61035. return content;
  61036. }
  61037. }
  61038. };
  61039. }
  61040. function completeSystemWrapPlugin() {
  61041. const SystemJSWrapRE = /System.register\(.*?(\(exports\)|\(\))/g;
  61042. return {
  61043. name: "vite:force-systemjs-wrap-complete",
  61044. renderChunk(code, chunk, opts) {
  61045. if (opts.format === "system") {
  61046. return {
  61047. code: code.replace(
  61048. SystemJSWrapRE,
  61049. (s, s1) => s.replace(s1, "(exports, module)")
  61050. ),
  61051. map: null
  61052. };
  61053. }
  61054. }
  61055. };
  61056. }
  61057. function resolveBuildOptions(raw, logger, root) {
  61058. const deprecatedPolyfillModulePreload = raw?.polyfillModulePreload;
  61059. if (raw) {
  61060. const { polyfillModulePreload, ...rest } = raw;
  61061. raw = rest;
  61062. if (deprecatedPolyfillModulePreload !== void 0) {
  61063. logger.warn(
  61064. "polyfillModulePreload is deprecated. Use modulePreload.polyfill instead."
  61065. );
  61066. }
  61067. if (deprecatedPolyfillModulePreload === false && raw.modulePreload === void 0) {
  61068. raw.modulePreload = { polyfill: false };
  61069. }
  61070. }
  61071. const modulePreload = raw?.modulePreload;
  61072. const defaultModulePreload = {
  61073. polyfill: true
  61074. };
  61075. const defaultBuildOptions = {
  61076. outDir: "dist",
  61077. assetsDir: "assets",
  61078. assetsInlineLimit: DEFAULT_ASSETS_INLINE_LIMIT,
  61079. cssCodeSplit: !raw?.lib,
  61080. sourcemap: false,
  61081. rollupOptions: {},
  61082. minify: raw?.ssr ? false : "esbuild",
  61083. terserOptions: {},
  61084. write: true,
  61085. emptyOutDir: null,
  61086. copyPublicDir: true,
  61087. manifest: false,
  61088. lib: false,
  61089. ssr: false,
  61090. ssrManifest: false,
  61091. ssrEmitAssets: false,
  61092. reportCompressedSize: true,
  61093. chunkSizeWarningLimit: 500,
  61094. watch: null
  61095. };
  61096. const userBuildOptions = raw ? mergeConfig(defaultBuildOptions, raw) : defaultBuildOptions;
  61097. const resolved = {
  61098. target: "modules",
  61099. cssTarget: false,
  61100. ...userBuildOptions,
  61101. commonjsOptions: {
  61102. include: [/node_modules/],
  61103. extensions: [".js", ".cjs"],
  61104. ...userBuildOptions.commonjsOptions
  61105. },
  61106. dynamicImportVarsOptions: {
  61107. warnOnError: true,
  61108. exclude: [/node_modules/],
  61109. ...userBuildOptions.dynamicImportVarsOptions
  61110. },
  61111. // Resolve to false | object
  61112. modulePreload: modulePreload === false ? false : typeof modulePreload === "object" ? {
  61113. ...defaultModulePreload,
  61114. ...modulePreload
  61115. } : defaultModulePreload
  61116. };
  61117. if (resolved.target === "modules") {
  61118. resolved.target = ESBUILD_MODULES_TARGET;
  61119. } else if (resolved.target === "esnext" && resolved.minify === "terser") {
  61120. try {
  61121. const terserPackageJsonPath = requireResolveFromRootWithFallback(
  61122. root,
  61123. "terser/package.json"
  61124. );
  61125. const terserPackageJson = JSON.parse(
  61126. fs__default.readFileSync(terserPackageJsonPath, "utf-8")
  61127. );
  61128. const v = terserPackageJson.version.split(".");
  61129. if (v[0] === "5" && v[1] < 16) {
  61130. resolved.target = "es2021";
  61131. }
  61132. } catch {
  61133. }
  61134. }
  61135. if (!resolved.cssTarget) {
  61136. resolved.cssTarget = resolved.target;
  61137. }
  61138. if (resolved.minify === "false") {
  61139. resolved.minify = false;
  61140. } else if (resolved.minify === true) {
  61141. resolved.minify = "esbuild";
  61142. }
  61143. if (resolved.cssMinify == null) {
  61144. resolved.cssMinify = !!resolved.minify;
  61145. }
  61146. return resolved;
  61147. }
  61148. async function resolveBuildPlugins(config) {
  61149. const options = config.build;
  61150. const { commonjsOptions } = options;
  61151. const usePluginCommonjs = !Array.isArray(commonjsOptions?.include) || commonjsOptions?.include.length !== 0;
  61152. const rollupOptionsPlugins = options.rollupOptions.plugins;
  61153. return {
  61154. pre: [
  61155. completeSystemWrapPlugin(),
  61156. ...usePluginCommonjs ? [commonjs(options.commonjsOptions)] : [],
  61157. dataURIPlugin(),
  61158. ...(await asyncFlatten(arraify(rollupOptionsPlugins))).filter(
  61159. Boolean
  61160. ),
  61161. ...config.isWorker ? [webWorkerPostPlugin()] : []
  61162. ],
  61163. post: [
  61164. buildImportAnalysisPlugin(config),
  61165. ...config.esbuild !== false ? [buildEsbuildPlugin(config)] : [],
  61166. ...options.minify ? [terserPlugin(config)] : [],
  61167. ...!config.isWorker ? [
  61168. ...options.manifest ? [manifestPlugin(config)] : [],
  61169. ...options.ssrManifest ? [ssrManifestPlugin(config)] : [],
  61170. buildReporterPlugin(config)
  61171. ] : [],
  61172. loadFallbackPlugin()
  61173. ]
  61174. };
  61175. }
  61176. async function build(inlineConfig = {}) {
  61177. const config = await resolveConfig(
  61178. inlineConfig,
  61179. "build",
  61180. "production",
  61181. "production"
  61182. );
  61183. const options = config.build;
  61184. const { root, logger, packageCache } = config;
  61185. const ssr = !!options.ssr;
  61186. const libOptions = options.lib;
  61187. logger.info(
  61188. colors$1.cyan(
  61189. `vite v${VERSION} ${colors$1.green(
  61190. `building ${ssr ? `SSR bundle ` : ``}for ${config.mode}...`
  61191. )}`
  61192. )
  61193. );
  61194. const resolve = (p) => path$n.resolve(root, p);
  61195. const input = libOptions ? options.rollupOptions?.input || (typeof libOptions.entry === "string" ? resolve(libOptions.entry) : Array.isArray(libOptions.entry) ? libOptions.entry.map(resolve) : Object.fromEntries(
  61196. Object.entries(libOptions.entry).map(([alias, file]) => [
  61197. alias,
  61198. resolve(file)
  61199. ])
  61200. )) : typeof options.ssr === "string" ? resolve(options.ssr) : options.rollupOptions?.input || resolve("index.html");
  61201. if (ssr && typeof input === "string" && input.endsWith(".html")) {
  61202. throw new Error(
  61203. `rollupOptions.input should not be an html file when building for SSR. Please specify a dedicated SSR entry.`
  61204. );
  61205. }
  61206. if (config.build.cssCodeSplit === false) {
  61207. const inputs = typeof input === "string" ? [input] : Array.isArray(input) ? input : Object.values(input);
  61208. if (inputs.some((input2) => input2.endsWith(".css"))) {
  61209. throw new Error(
  61210. `When "build.cssCodeSplit: false" is set, "rollupOptions.input" should not include CSS files.`
  61211. );
  61212. }
  61213. }
  61214. const outDir = resolve(options.outDir);
  61215. const plugins = ssr ? config.plugins.map((p) => injectSsrFlagToHooks(p)) : config.plugins;
  61216. const rollupOptions = {
  61217. preserveEntrySignatures: ssr ? "allow-extension" : libOptions ? "strict" : false,
  61218. cache: config.build.watch ? void 0 : false,
  61219. ...options.rollupOptions,
  61220. input,
  61221. plugins,
  61222. external: options.rollupOptions?.external,
  61223. onwarn(warning, warn) {
  61224. onRollupWarning(warning, warn, config);
  61225. }
  61226. };
  61227. function extractStack(e) {
  61228. const { stack, name = "Error", message } = e;
  61229. if (!stack) {
  61230. return stack;
  61231. }
  61232. const expectedPrefix = `${name}: ${message}
  61233. `;
  61234. if (stack.startsWith(expectedPrefix)) {
  61235. return stack.slice(expectedPrefix.length);
  61236. }
  61237. return stack;
  61238. }
  61239. const normalizeCodeFrame = (frame) => {
  61240. const trimmedPadding = frame.replace(/^\n|\n$/g, "");
  61241. return `
  61242. ${trimmedPadding}
  61243. `;
  61244. };
  61245. const enhanceRollupError = (e) => {
  61246. const stackOnly = extractStack(e);
  61247. let msg = colors$1.red((e.plugin ? `[${e.plugin}] ` : "") + e.message);
  61248. if (e.id) {
  61249. msg += `
  61250. file: ${colors$1.cyan(
  61251. e.id + (e.loc ? `:${e.loc.line}:${e.loc.column}` : "")
  61252. )}`;
  61253. }
  61254. if (e.frame) {
  61255. msg += `
  61256. ` + colors$1.yellow(normalizeCodeFrame(e.frame));
  61257. }
  61258. e.message = msg;
  61259. if (stackOnly !== void 0) {
  61260. e.stack = `${e.message}
  61261. ${stackOnly}`;
  61262. }
  61263. };
  61264. const outputBuildError = (e) => {
  61265. enhanceRollupError(e);
  61266. clearLine();
  61267. logger.error(e.message, { error: e });
  61268. };
  61269. let bundle;
  61270. let startTime;
  61271. try {
  61272. const buildOutputOptions = (output = {}) => {
  61273. if (output.output) {
  61274. logger.warn(
  61275. `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.`
  61276. );
  61277. }
  61278. if (output.file) {
  61279. throw new Error(
  61280. `Vite does not support "rollupOptions.output.file". Please use "rollupOptions.output.dir" and "rollupOptions.output.entryFileNames" instead.`
  61281. );
  61282. }
  61283. if (output.sourcemap) {
  61284. logger.warnOnce(
  61285. colors$1.yellow(
  61286. `Vite does not support "rollupOptions.output.sourcemap". Please use "build.sourcemap" instead.`
  61287. )
  61288. );
  61289. }
  61290. const ssrNodeBuild = ssr && config.ssr.target === "node";
  61291. const ssrWorkerBuild = ssr && config.ssr.target === "webworker";
  61292. const format = output.format || "es";
  61293. const jsExt = ssrNodeBuild || libOptions ? resolveOutputJsExtension(
  61294. format,
  61295. findNearestPackageData(root, packageCache)?.data.type
  61296. ) : "js";
  61297. return {
  61298. dir: outDir,
  61299. // Default format is 'es' for regular and for SSR builds
  61300. format,
  61301. exports: "auto",
  61302. sourcemap: options.sourcemap,
  61303. name: libOptions ? libOptions.name : void 0,
  61304. hoistTransitiveImports: libOptions ? false : void 0,
  61305. // es2015 enables `generatedCode.symbols`
  61306. // - #764 add `Symbol.toStringTag` when build es module into cjs chunk
  61307. // - #1048 add `Symbol.toStringTag` for module default export
  61308. generatedCode: "es2015",
  61309. entryFileNames: ssr ? `[name].${jsExt}` : libOptions ? ({ name }) => resolveLibFilename(
  61310. libOptions,
  61311. format,
  61312. name,
  61313. root,
  61314. jsExt,
  61315. packageCache
  61316. ) : path$n.posix.join(options.assetsDir, `[name]-[hash].${jsExt}`),
  61317. chunkFileNames: libOptions ? `[name]-[hash].${jsExt}` : path$n.posix.join(options.assetsDir, `[name]-[hash].${jsExt}`),
  61318. assetFileNames: libOptions ? `[name].[ext]` : path$n.posix.join(options.assetsDir, `[name]-[hash].[ext]`),
  61319. inlineDynamicImports: output.format === "umd" || output.format === "iife" || ssrWorkerBuild && (typeof input === "string" || Object.keys(input).length === 1),
  61320. ...output
  61321. };
  61322. };
  61323. const outputs = resolveBuildOutputs(
  61324. options.rollupOptions?.output,
  61325. libOptions,
  61326. logger
  61327. );
  61328. const normalizedOutputs = [];
  61329. if (Array.isArray(outputs)) {
  61330. for (const resolvedOutput of outputs) {
  61331. normalizedOutputs.push(buildOutputOptions(resolvedOutput));
  61332. }
  61333. } else {
  61334. normalizedOutputs.push(buildOutputOptions(outputs));
  61335. }
  61336. const resolvedOutDirs = getResolvedOutDirs(
  61337. root,
  61338. options.outDir,
  61339. options.rollupOptions?.output
  61340. );
  61341. const emptyOutDir = resolveEmptyOutDir(
  61342. options.emptyOutDir,
  61343. root,
  61344. resolvedOutDirs,
  61345. logger
  61346. );
  61347. if (config.build.watch) {
  61348. logger.info(colors$1.cyan(`
  61349. watching for file changes...`));
  61350. const resolvedChokidarOptions = resolveChokidarOptions(
  61351. config.build.watch.chokidar,
  61352. resolvedOutDirs,
  61353. emptyOutDir,
  61354. config.cacheDir
  61355. );
  61356. const { watch } = await import('rollup');
  61357. const watcher = watch({
  61358. ...rollupOptions,
  61359. output: normalizedOutputs,
  61360. watch: {
  61361. ...config.build.watch,
  61362. chokidar: resolvedChokidarOptions
  61363. }
  61364. });
  61365. watcher.on("event", (event) => {
  61366. if (event.code === "BUNDLE_START") {
  61367. logger.info(colors$1.cyan(`
  61368. build started...`));
  61369. if (options.write) {
  61370. prepareOutDir(resolvedOutDirs, emptyOutDir, config);
  61371. }
  61372. } else if (event.code === "BUNDLE_END") {
  61373. event.result.close();
  61374. logger.info(colors$1.cyan(`built in ${event.duration}ms.`));
  61375. } else if (event.code === "ERROR") {
  61376. outputBuildError(event.error);
  61377. }
  61378. });
  61379. return watcher;
  61380. }
  61381. const { rollup } = await import('rollup');
  61382. startTime = Date.now();
  61383. bundle = await rollup(rollupOptions);
  61384. if (options.write) {
  61385. prepareOutDir(resolvedOutDirs, emptyOutDir, config);
  61386. }
  61387. const res = [];
  61388. for (const output of normalizedOutputs) {
  61389. res.push(await bundle[options.write ? "write" : "generate"](output));
  61390. }
  61391. logger.info(
  61392. `${colors$1.green(`\u2713 built in ${displayTime(Date.now() - startTime)}`)}`
  61393. );
  61394. return Array.isArray(outputs) ? res : res[0];
  61395. } catch (e) {
  61396. enhanceRollupError(e);
  61397. clearLine();
  61398. if (startTime) {
  61399. logger.error(
  61400. `${colors$1.red("x")} Build failed in ${displayTime(Date.now() - startTime)}`
  61401. );
  61402. startTime = void 0;
  61403. }
  61404. throw e;
  61405. } finally {
  61406. if (bundle) await bundle.close();
  61407. }
  61408. }
  61409. function prepareOutDir(outDirs, emptyOutDir, config) {
  61410. const outDirsArray = [...outDirs];
  61411. for (const outDir of outDirs) {
  61412. if (emptyOutDir !== false && fs__default.existsSync(outDir)) {
  61413. const skipDirs = outDirsArray.map((dir) => {
  61414. const relative = path$n.relative(outDir, dir);
  61415. if (relative && !relative.startsWith("..") && !path$n.isAbsolute(relative)) {
  61416. return relative;
  61417. }
  61418. return "";
  61419. }).filter(Boolean);
  61420. emptyDir(outDir, [...skipDirs, ".git"]);
  61421. }
  61422. if (config.build.copyPublicDir && config.publicDir && fs__default.existsSync(config.publicDir)) {
  61423. if (!areSeparateFolders(outDir, config.publicDir)) {
  61424. config.logger.warn(
  61425. colors$1.yellow(
  61426. `
  61427. ${colors$1.bold(
  61428. `(!)`
  61429. )} The public directory feature may not work correctly. outDir ${colors$1.white(
  61430. colors$1.dim(outDir)
  61431. )} and publicDir ${colors$1.white(
  61432. colors$1.dim(config.publicDir)
  61433. )} are not separate folders.
  61434. `
  61435. )
  61436. );
  61437. }
  61438. copyDir(config.publicDir, outDir);
  61439. }
  61440. }
  61441. }
  61442. function getPkgName(name) {
  61443. return name?.[0] === "@" ? name.split("/")[1] : name;
  61444. }
  61445. function resolveOutputJsExtension(format, type = "commonjs") {
  61446. if (type === "module") {
  61447. return format === "cjs" || format === "umd" ? "cjs" : "js";
  61448. } else {
  61449. return format === "es" ? "mjs" : "js";
  61450. }
  61451. }
  61452. function resolveLibFilename(libOptions, format, entryName, root, extension, packageCache) {
  61453. if (typeof libOptions.fileName === "function") {
  61454. return libOptions.fileName(format, entryName);
  61455. }
  61456. const packageJson = findNearestPackageData(root, packageCache)?.data;
  61457. const name = libOptions.fileName || (packageJson && typeof libOptions.entry === "string" ? getPkgName(packageJson.name) : entryName);
  61458. if (!name)
  61459. throw new Error(
  61460. 'Name in package.json is required if option "build.lib.fileName" is not provided.'
  61461. );
  61462. extension ??= resolveOutputJsExtension(format, packageJson?.type);
  61463. if (format === "cjs" || format === "es") {
  61464. return `${name}.${extension}`;
  61465. }
  61466. return `${name}.${format}.${extension}`;
  61467. }
  61468. function resolveBuildOutputs(outputs, libOptions, logger) {
  61469. if (libOptions) {
  61470. const libHasMultipleEntries = typeof libOptions.entry !== "string" && Object.values(libOptions.entry).length > 1;
  61471. const libFormats = libOptions.formats || (libHasMultipleEntries ? ["es", "cjs"] : ["es", "umd"]);
  61472. if (!Array.isArray(outputs)) {
  61473. if (libFormats.includes("umd") || libFormats.includes("iife")) {
  61474. if (libHasMultipleEntries) {
  61475. throw new Error(
  61476. 'Multiple entry points are not supported when output formats include "umd" or "iife".'
  61477. );
  61478. }
  61479. if (!libOptions.name) {
  61480. throw new Error(
  61481. 'Option "build.lib.name" is required when output formats include "umd" or "iife".'
  61482. );
  61483. }
  61484. }
  61485. return libFormats.map((format) => ({ ...outputs, format }));
  61486. }
  61487. if (libOptions.formats) {
  61488. logger.warn(
  61489. colors$1.yellow(
  61490. '"build.lib.formats" will be ignored because "build.rollupOptions.output" is already an array format.'
  61491. )
  61492. );
  61493. }
  61494. outputs.forEach((output) => {
  61495. if ((output.format === "umd" || output.format === "iife") && !output.name) {
  61496. throw new Error(
  61497. 'Entries in "build.rollupOptions.output" must specify "name" when the format is "umd" or "iife".'
  61498. );
  61499. }
  61500. });
  61501. }
  61502. return outputs;
  61503. }
  61504. const warningIgnoreList = [`CIRCULAR_DEPENDENCY`, `THIS_IS_UNDEFINED`];
  61505. const dynamicImportWarningIgnoreList = [
  61506. `Unsupported expression`,
  61507. `statically analyzed`
  61508. ];
  61509. function clearLine() {
  61510. const tty = process.stdout.isTTY && !process.env.CI;
  61511. if (tty) {
  61512. process.stdout.clearLine(0);
  61513. process.stdout.cursorTo(0);
  61514. }
  61515. }
  61516. function onRollupWarning(warning, warn, config) {
  61517. const viteWarn = (warnLog) => {
  61518. let warning2;
  61519. if (typeof warnLog === "function") {
  61520. warning2 = warnLog();
  61521. } else {
  61522. warning2 = warnLog;
  61523. }
  61524. if (typeof warning2 === "object") {
  61525. if (warning2.code === "UNRESOLVED_IMPORT") {
  61526. const id = warning2.id;
  61527. const exporter = warning2.exporter;
  61528. if (!id || !id.endsWith("?commonjs-external")) {
  61529. throw new Error(
  61530. `[vite]: Rollup failed to resolve import "${exporter}" from "${id}".
  61531. This is most likely unintended because it can break your application at runtime.
  61532. If you do want to externalize this module explicitly add it to
  61533. \`build.rollupOptions.external\``
  61534. );
  61535. }
  61536. }
  61537. if (warning2.plugin === "rollup-plugin-dynamic-import-variables" && dynamicImportWarningIgnoreList.some(
  61538. (msg) => warning2.message.includes(msg)
  61539. )) {
  61540. return;
  61541. }
  61542. if (warningIgnoreList.includes(warning2.code)) {
  61543. return;
  61544. }
  61545. if (warning2.code === "PLUGIN_WARNING") {
  61546. config.logger.warn(
  61547. `${colors$1.bold(
  61548. colors$1.yellow(`[plugin:${warning2.plugin}]`)
  61549. )} ${colors$1.yellow(warning2.message)}`
  61550. );
  61551. return;
  61552. }
  61553. }
  61554. warn(warnLog);
  61555. };
  61556. clearLine();
  61557. const userOnWarn = config.build.rollupOptions?.onwarn;
  61558. if (userOnWarn) {
  61559. userOnWarn(warning, viteWarn);
  61560. } else {
  61561. viteWarn(warning);
  61562. }
  61563. }
  61564. function resolveUserExternal(user, id, parentId, isResolved) {
  61565. if (typeof user === "function") {
  61566. return user(id, parentId, isResolved);
  61567. } else if (Array.isArray(user)) {
  61568. return user.some((test) => isExternal(id, test));
  61569. } else {
  61570. return isExternal(id, user);
  61571. }
  61572. }
  61573. function isExternal(id, test) {
  61574. if (typeof test === "string") {
  61575. return id === test;
  61576. } else {
  61577. return test.test(id);
  61578. }
  61579. }
  61580. function injectSsrFlagToHooks(plugin) {
  61581. const { resolveId, load, transform } = plugin;
  61582. return {
  61583. ...plugin,
  61584. resolveId: wrapSsrResolveId(resolveId),
  61585. load: wrapSsrLoad(load),
  61586. transform: wrapSsrTransform(transform)
  61587. };
  61588. }
  61589. function wrapSsrResolveId(hook) {
  61590. if (!hook) return;
  61591. const fn = getHookHandler(hook);
  61592. const handler = function(id, importer, options) {
  61593. return fn.call(this, id, importer, injectSsrFlag(options));
  61594. };
  61595. if ("handler" in hook) {
  61596. return {
  61597. ...hook,
  61598. handler
  61599. };
  61600. } else {
  61601. return handler;
  61602. }
  61603. }
  61604. function wrapSsrLoad(hook) {
  61605. if (!hook) return;
  61606. const fn = getHookHandler(hook);
  61607. const handler = function(id, ...args) {
  61608. return fn.call(this, id, injectSsrFlag(args[0]));
  61609. };
  61610. if ("handler" in hook) {
  61611. return {
  61612. ...hook,
  61613. handler
  61614. };
  61615. } else {
  61616. return handler;
  61617. }
  61618. }
  61619. function wrapSsrTransform(hook) {
  61620. if (!hook) return;
  61621. const fn = getHookHandler(hook);
  61622. const handler = function(code, importer, ...args) {
  61623. return fn.call(this, code, importer, injectSsrFlag(args[0]));
  61624. };
  61625. if ("handler" in hook) {
  61626. return {
  61627. ...hook,
  61628. handler
  61629. };
  61630. } else {
  61631. return handler;
  61632. }
  61633. }
  61634. function injectSsrFlag(options) {
  61635. return { ...options ?? {}, ssr: true };
  61636. }
  61637. const needsEscapeRegEx = /[\n\r'\\\u2028\u2029]/;
  61638. const quoteNewlineRegEx = /([\n\r'\u2028\u2029])/g;
  61639. const backSlashRegEx = /\\/g;
  61640. function escapeId(id) {
  61641. if (!needsEscapeRegEx.test(id)) return id;
  61642. return id.replace(backSlashRegEx, "\\\\").replace(quoteNewlineRegEx, "\\$1");
  61643. }
  61644. const getResolveUrl = (path2, URL = "URL") => `new ${URL}(${path2}).href`;
  61645. const getRelativeUrlFromDocument = (relativePath, umd = false) => getResolveUrl(
  61646. `'${escapeId(partialEncodeURIPath(relativePath))}', ${umd ? `typeof document === 'undefined' ? location.href : ` : ""}document.currentScript && document.currentScript.tagName.toUpperCase() === 'SCRIPT' && document.currentScript.src || document.baseURI`
  61647. );
  61648. const getFileUrlFromFullPath = (path2) => `require('u' + 'rl').pathToFileURL(${path2}).href`;
  61649. const getFileUrlFromRelativePath = (path2) => getFileUrlFromFullPath(`__dirname + '/${escapeId(path2)}'`);
  61650. const relativeUrlMechanisms = {
  61651. amd: (relativePath) => {
  61652. if (relativePath[0] !== ".") relativePath = "./" + relativePath;
  61653. return getResolveUrl(
  61654. `require.toUrl('${escapeId(relativePath)}'), document.baseURI`
  61655. );
  61656. },
  61657. cjs: (relativePath) => `(typeof document === 'undefined' ? ${getFileUrlFromRelativePath(
  61658. relativePath
  61659. )} : ${getRelativeUrlFromDocument(relativePath)})`,
  61660. es: (relativePath) => getResolveUrl(
  61661. `'${escapeId(partialEncodeURIPath(relativePath))}', import.meta.url`
  61662. ),
  61663. iife: (relativePath) => getRelativeUrlFromDocument(relativePath),
  61664. // NOTE: make sure rollup generate `module` params
  61665. system: (relativePath) => getResolveUrl(
  61666. `'${escapeId(partialEncodeURIPath(relativePath))}', module.meta.url`
  61667. ),
  61668. umd: (relativePath) => `(typeof document === 'undefined' && typeof location === 'undefined' ? ${getFileUrlFromRelativePath(
  61669. relativePath
  61670. )} : ${getRelativeUrlFromDocument(relativePath, true)})`
  61671. };
  61672. const customRelativeUrlMechanisms = {
  61673. ...relativeUrlMechanisms,
  61674. "worker-iife": (relativePath) => getResolveUrl(
  61675. `'${escapeId(partialEncodeURIPath(relativePath))}', self.location.href`
  61676. )
  61677. };
  61678. function toOutputFilePathInJS(filename, type, hostId, hostType, config, toRelative) {
  61679. const { renderBuiltUrl } = config.experimental;
  61680. let relative = config.base === "" || config.base === "./";
  61681. if (renderBuiltUrl) {
  61682. const result = renderBuiltUrl(filename, {
  61683. hostId,
  61684. hostType,
  61685. type,
  61686. ssr: !!config.build.ssr
  61687. });
  61688. if (typeof result === "object") {
  61689. if (result.runtime) {
  61690. return { runtime: result.runtime };
  61691. }
  61692. if (typeof result.relative === "boolean") {
  61693. relative = result.relative;
  61694. }
  61695. } else if (result) {
  61696. return result;
  61697. }
  61698. }
  61699. if (relative && !config.build.ssr) {
  61700. return toRelative(filename, hostId);
  61701. }
  61702. return joinUrlSegments(config.decodedBase, filename);
  61703. }
  61704. function createToImportMetaURLBasedRelativeRuntime(format, isWorker) {
  61705. const formatLong = isWorker && format === "iife" ? "worker-iife" : format;
  61706. const toRelativePath = customRelativeUrlMechanisms[formatLong];
  61707. return (filename, importer) => ({
  61708. runtime: toRelativePath(
  61709. path$n.posix.relative(path$n.dirname(importer), filename)
  61710. )
  61711. });
  61712. }
  61713. function toOutputFilePathWithoutRuntime(filename, type, hostId, hostType, config, toRelative) {
  61714. const { renderBuiltUrl } = config.experimental;
  61715. let relative = config.base === "" || config.base === "./";
  61716. if (renderBuiltUrl) {
  61717. const result = renderBuiltUrl(filename, {
  61718. hostId,
  61719. hostType,
  61720. type,
  61721. ssr: !!config.build.ssr
  61722. });
  61723. if (typeof result === "object") {
  61724. if (result.runtime) {
  61725. throw new Error(
  61726. `{ runtime: "${result.runtime}" } is not supported for assets in ${hostType} files: ${filename}`
  61727. );
  61728. }
  61729. if (typeof result.relative === "boolean") {
  61730. relative = result.relative;
  61731. }
  61732. } else if (result) {
  61733. return result;
  61734. }
  61735. }
  61736. if (relative && !config.build.ssr) {
  61737. return toRelative(filename, hostId);
  61738. } else {
  61739. return joinUrlSegments(config.decodedBase, filename);
  61740. }
  61741. }
  61742. const toOutputFilePathInCss = toOutputFilePathWithoutRuntime;
  61743. const toOutputFilePathInHtml = toOutputFilePathWithoutRuntime;
  61744. function areSeparateFolders(a, b) {
  61745. const na = normalizePath$3(a);
  61746. const nb = normalizePath$3(b);
  61747. return na !== nb && !na.startsWith(withTrailingSlash(nb)) && !nb.startsWith(withTrailingSlash(na));
  61748. }
  61749. var build$1 = {
  61750. __proto__: null,
  61751. build: build,
  61752. createToImportMetaURLBasedRelativeRuntime: createToImportMetaURLBasedRelativeRuntime,
  61753. onRollupWarning: onRollupWarning,
  61754. resolveBuildOptions: resolveBuildOptions,
  61755. resolveBuildOutputs: resolveBuildOutputs,
  61756. resolveBuildPlugins: resolveBuildPlugins,
  61757. resolveLibFilename: resolveLibFilename,
  61758. resolveUserExternal: resolveUserExternal,
  61759. toOutputFilePathInCss: toOutputFilePathInCss,
  61760. toOutputFilePathInHtml: toOutputFilePathInHtml,
  61761. toOutputFilePathInJS: toOutputFilePathInJS,
  61762. toOutputFilePathWithoutRuntime: toOutputFilePathWithoutRuntime
  61763. };
  61764. // NOTE: supports Node 6.x
  61765. const NOOP = () => {};
  61766. const MIMES = /text|javascript|\/json|xml/i;
  61767. /**
  61768. * @param {any} chunk
  61769. * @param {BufferEncoding} enc
  61770. * @returns {number}
  61771. */
  61772. function getChunkSize(chunk, enc) {
  61773. return chunk ? Buffer.byteLength(chunk, enc) : 0;
  61774. }
  61775. /**
  61776. * @param {import('./index.d.mts').Options} [options]
  61777. * @returns {import('./index.d.mts').Middleware}
  61778. */
  61779. function compression ({ threshold = 1024, level = -1, brotli = false, gzip = true, mimes = MIMES } = {}) {
  61780. const brotliOpts = (typeof brotli === 'object' && brotli) || {};
  61781. const gzipOpts = (typeof gzip === 'object' && gzip) || {};
  61782. // disable Brotli on Node<12.7 where it is unsupported:
  61783. if (!zlib$1.createBrotliCompress) brotli = false;
  61784. return (req, res, next = NOOP) => {
  61785. const accept = req.headers['accept-encoding'] + '';
  61786. const encoding = ((brotli && accept.match(/\bbr\b/)) || (gzip && accept.match(/\bgzip\b/)) || [])[0];
  61787. // skip if no response body or no supported encoding:
  61788. if (req.method === 'HEAD' || !encoding) return next();
  61789. /** @type {zlib.Gzip | zlib.BrotliCompress} */
  61790. let compress;
  61791. /** @type {Array<[string, function]>?} */
  61792. let pendingListeners = [];
  61793. let pendingStatus = 0;
  61794. let started = false;
  61795. let size = 0;
  61796. function start() {
  61797. started = true;
  61798. // @ts-ignore
  61799. size = res.getHeader('Content-Length') | 0 || size;
  61800. const compressible = mimes.test(
  61801. String(res.getHeader('Content-Type') || 'text/plain')
  61802. );
  61803. const cleartext = !res.getHeader('Content-Encoding');
  61804. const listeners = pendingListeners || [];
  61805. if (compressible && cleartext && size >= threshold) {
  61806. res.setHeader('Content-Encoding', encoding);
  61807. res.removeHeader('Content-Length');
  61808. if (encoding === 'br') {
  61809. compress = zlib$1.createBrotliCompress({
  61810. params: Object.assign({
  61811. [zlib$1.constants.BROTLI_PARAM_QUALITY]: level,
  61812. [zlib$1.constants.BROTLI_PARAM_SIZE_HINT]: size,
  61813. }, brotliOpts)
  61814. });
  61815. } else {
  61816. compress = zlib$1.createGzip(
  61817. Object.assign({ level }, gzipOpts)
  61818. );
  61819. }
  61820. // backpressure
  61821. compress.on('data', chunk => write.call(res, chunk) || compress.pause());
  61822. on.call(res, 'drain', () => compress.resume());
  61823. compress.on('end', () => end.call(res));
  61824. listeners.forEach(p => compress.on.apply(compress, p));
  61825. } else {
  61826. pendingListeners = null;
  61827. listeners.forEach(p => on.apply(res, p));
  61828. }
  61829. writeHead.call(res, pendingStatus || res.statusCode);
  61830. }
  61831. const { end, write, on, writeHead } = res;
  61832. res.writeHead = function (status, reason, headers) {
  61833. if (typeof reason !== 'string') [headers, reason] = [reason, headers];
  61834. if (headers) for (let k in headers) res.setHeader(k, headers[k]);
  61835. pendingStatus = status;
  61836. return this;
  61837. };
  61838. res.write = function (chunk, enc) {
  61839. size += getChunkSize(chunk, enc);
  61840. if (!started) start();
  61841. if (!compress) return write.apply(this, arguments);
  61842. return compress.write.apply(compress, arguments);
  61843. };
  61844. res.end = function (chunk, enc) {
  61845. if (arguments.length > 0 && typeof chunk !== 'function') {
  61846. size += getChunkSize(chunk, enc);
  61847. }
  61848. if (!started) start();
  61849. if (!compress) return end.apply(this, arguments);
  61850. return compress.end.apply(compress, arguments);
  61851. };
  61852. res.on = function (type, listener) {
  61853. if (!pendingListeners) on.call(this, type, listener);
  61854. else if (compress) compress.on(type, listener);
  61855. else pendingListeners.push([type, listener]);
  61856. return this;
  61857. };
  61858. next();
  61859. };
  61860. }
  61861. function resolvePreviewOptions(preview2, server) {
  61862. return {
  61863. port: preview2?.port,
  61864. strictPort: preview2?.strictPort ?? server.strictPort,
  61865. host: preview2?.host ?? server.host,
  61866. allowedHosts: preview2?.allowedHosts ?? server.allowedHosts,
  61867. https: preview2?.https ?? server.https,
  61868. open: preview2?.open ?? server.open,
  61869. proxy: preview2?.proxy ?? server.proxy,
  61870. cors: preview2?.cors ?? server.cors,
  61871. headers: preview2?.headers ?? server.headers
  61872. };
  61873. }
  61874. async function preview(inlineConfig = {}) {
  61875. const config = await resolveConfig(
  61876. inlineConfig,
  61877. "serve",
  61878. "production",
  61879. "production",
  61880. true
  61881. );
  61882. const distDir = path$n.resolve(config.root, config.build.outDir);
  61883. if (!fs__default.existsSync(distDir) && // error if no plugins implement `configurePreviewServer`
  61884. config.plugins.every((plugin) => !plugin.configurePreviewServer) && // error if called in CLI only. programmatic usage could access `httpServer`
  61885. // and affect file serving
  61886. process.argv[1]?.endsWith(path$n.normalize("bin/vite.js")) && process.argv[2] === "preview") {
  61887. throw new Error(
  61888. `The directory "${config.build.outDir}" does not exist. Did you build your project?`
  61889. );
  61890. }
  61891. const app = connect$1();
  61892. const httpServer = await resolveHttpServer(
  61893. config.preview,
  61894. app,
  61895. await resolveHttpsConfig(config.preview?.https)
  61896. );
  61897. setClientErrorHandler(httpServer, config.logger);
  61898. const options = config.preview;
  61899. const logger = config.logger;
  61900. const closeHttpServer = createServerCloseFn(httpServer);
  61901. const server = {
  61902. config,
  61903. middlewares: app,
  61904. httpServer,
  61905. async close() {
  61906. teardownSIGTERMListener(closeServerAndExit);
  61907. await closeHttpServer();
  61908. },
  61909. resolvedUrls: null,
  61910. printUrls() {
  61911. if (server.resolvedUrls) {
  61912. printServerUrls(server.resolvedUrls, options.host, logger.info);
  61913. } else {
  61914. throw new Error("cannot print server URLs before server is listening.");
  61915. }
  61916. },
  61917. bindCLIShortcuts(options2) {
  61918. bindCLIShortcuts(server, options2);
  61919. }
  61920. };
  61921. const closeServerAndExit = async () => {
  61922. try {
  61923. await server.close();
  61924. } finally {
  61925. process.exit();
  61926. }
  61927. };
  61928. setupSIGTERMListener(closeServerAndExit);
  61929. const postHooks = [];
  61930. for (const hook of config.getSortedPluginHooks("configurePreviewServer")) {
  61931. postHooks.push(await hook(server));
  61932. }
  61933. const { cors } = config.preview;
  61934. if (cors !== false) {
  61935. app.use(
  61936. corsMiddleware(
  61937. typeof cors === "boolean" ? {} : cors ?? { origin: defaultAllowedOrigins }
  61938. )
  61939. );
  61940. }
  61941. const { allowedHosts } = config.preview;
  61942. if (allowedHosts !== true && !config.preview.https) {
  61943. app.use(hostCheckMiddleware(config, true));
  61944. }
  61945. const { proxy } = config.preview;
  61946. if (proxy) {
  61947. app.use(proxyMiddleware(httpServer, proxy, config));
  61948. }
  61949. app.use(compression());
  61950. if (config.base !== "/") {
  61951. app.use(baseMiddleware(config.rawBase, false));
  61952. }
  61953. const headers = config.preview.headers;
  61954. const viteAssetMiddleware = (...args) => sirv(distDir, {
  61955. etag: true,
  61956. dev: true,
  61957. extensions: [],
  61958. ignores: false,
  61959. setHeaders(res) {
  61960. if (headers) {
  61961. for (const name in headers) {
  61962. res.setHeader(name, headers[name]);
  61963. }
  61964. }
  61965. },
  61966. shouldServe(filePath) {
  61967. return shouldServeFile(filePath, distDir);
  61968. }
  61969. })(...args);
  61970. app.use(viteAssetMiddleware);
  61971. if (config.appType === "spa" || config.appType === "mpa") {
  61972. app.use(htmlFallbackMiddleware(distDir, config.appType === "spa"));
  61973. }
  61974. postHooks.forEach((fn) => fn && fn());
  61975. if (config.appType === "spa" || config.appType === "mpa") {
  61976. const normalizedDistDir = normalizePath$3(distDir);
  61977. app.use(indexHtmlMiddleware(normalizedDistDir, server));
  61978. app.use(notFoundMiddleware());
  61979. }
  61980. const hostname = await resolveHostname(options.host);
  61981. const port = options.port ?? DEFAULT_PREVIEW_PORT;
  61982. await httpServerStart(httpServer, {
  61983. port,
  61984. strictPort: options.strictPort,
  61985. host: hostname.host,
  61986. logger
  61987. });
  61988. server.resolvedUrls = await resolveServerUrls(
  61989. httpServer,
  61990. config.preview,
  61991. config
  61992. );
  61993. if (options.open) {
  61994. const url = server.resolvedUrls?.local[0] ?? server.resolvedUrls?.network[0];
  61995. if (url) {
  61996. const path2 = typeof options.open === "string" ? new URL(options.open, url).href : url;
  61997. openBrowser(path2, true, logger);
  61998. }
  61999. }
  62000. return server;
  62001. }
  62002. var preview$1 = {
  62003. __proto__: null,
  62004. preview: preview,
  62005. resolvePreviewOptions: resolvePreviewOptions
  62006. };
  62007. function resolveSSROptions(ssr, preserveSymlinks) {
  62008. ssr ??= {};
  62009. const optimizeDeps = ssr.optimizeDeps ?? {};
  62010. const target = "node";
  62011. return {
  62012. target,
  62013. ...ssr,
  62014. optimizeDeps: {
  62015. ...optimizeDeps,
  62016. noDiscovery: true,
  62017. // always true for ssr
  62018. esbuildOptions: {
  62019. preserveSymlinks,
  62020. ...optimizeDeps.esbuildOptions
  62021. }
  62022. }
  62023. };
  62024. }
  62025. const debug = createDebugger("vite:config");
  62026. const promisifiedRealpath = promisify$4(fs__default.realpath);
  62027. function defineConfig(config) {
  62028. return config;
  62029. }
  62030. function checkBadCharactersInPath(path2, logger) {
  62031. const badChars = [];
  62032. if (path2.includes("#")) {
  62033. badChars.push("#");
  62034. }
  62035. if (path2.includes("?")) {
  62036. badChars.push("?");
  62037. }
  62038. if (badChars.length > 0) {
  62039. const charString = badChars.map((c) => `"${c}"`).join(" and ");
  62040. const inflectedChars = badChars.length > 1 ? "characters" : "character";
  62041. logger.warn(
  62042. colors$1.yellow(
  62043. `The project root contains the ${charString} ${inflectedChars} (${colors$1.cyan(
  62044. path2
  62045. )}), which may not work when running Vite. Consider renaming the directory to remove the characters.`
  62046. )
  62047. );
  62048. }
  62049. }
  62050. async function resolveConfig(inlineConfig, command, defaultMode = "development", defaultNodeEnv = "development", isPreview = false) {
  62051. let config = inlineConfig;
  62052. let configFileDependencies = [];
  62053. let mode = inlineConfig.mode || defaultMode;
  62054. const isNodeEnvSet = !!process.env.NODE_ENV;
  62055. const packageCache = /* @__PURE__ */ new Map();
  62056. if (!isNodeEnvSet) {
  62057. process.env.NODE_ENV = defaultNodeEnv;
  62058. }
  62059. const configEnv = {
  62060. mode,
  62061. command,
  62062. isSsrBuild: command === "build" && !!config.build?.ssr,
  62063. isPreview
  62064. };
  62065. let { configFile } = config;
  62066. if (configFile !== false) {
  62067. const loadResult = await loadConfigFromFile(
  62068. configEnv,
  62069. configFile,
  62070. config.root,
  62071. config.logLevel,
  62072. config.customLogger
  62073. );
  62074. if (loadResult) {
  62075. config = mergeConfig(loadResult.config, config);
  62076. configFile = loadResult.path;
  62077. configFileDependencies = loadResult.dependencies;
  62078. }
  62079. }
  62080. mode = inlineConfig.mode || config.mode || mode;
  62081. configEnv.mode = mode;
  62082. const filterPlugin = (p) => {
  62083. if (!p) {
  62084. return false;
  62085. } else if (!p.apply) {
  62086. return true;
  62087. } else if (typeof p.apply === "function") {
  62088. return p.apply({ ...config, mode }, configEnv);
  62089. } else {
  62090. return p.apply === command;
  62091. }
  62092. };
  62093. const rawUserPlugins = (await asyncFlatten(config.plugins || [])).filter(filterPlugin);
  62094. const [prePlugins, normalPlugins, postPlugins] = sortUserPlugins(rawUserPlugins);
  62095. const userPlugins = [...prePlugins, ...normalPlugins, ...postPlugins];
  62096. config = await runConfigHook(config, userPlugins, configEnv);
  62097. const logger = createLogger(config.logLevel, {
  62098. allowClearScreen: config.clearScreen,
  62099. customLogger: config.customLogger
  62100. });
  62101. const resolvedRoot = normalizePath$3(
  62102. config.root ? path$n.resolve(config.root) : process.cwd()
  62103. );
  62104. checkBadCharactersInPath(resolvedRoot, logger);
  62105. const clientAlias = [
  62106. {
  62107. find: /^\/?@vite\/env/,
  62108. replacement: path$n.posix.join(FS_PREFIX, normalizePath$3(ENV_ENTRY))
  62109. },
  62110. {
  62111. find: /^\/?@vite\/client/,
  62112. replacement: path$n.posix.join(FS_PREFIX, normalizePath$3(CLIENT_ENTRY))
  62113. }
  62114. ];
  62115. const resolvedAlias = normalizeAlias(
  62116. mergeAlias(clientAlias, config.resolve?.alias || [])
  62117. );
  62118. const resolveOptions = {
  62119. mainFields: config.resolve?.mainFields ?? DEFAULT_MAIN_FIELDS,
  62120. conditions: config.resolve?.conditions ?? [],
  62121. extensions: config.resolve?.extensions ?? DEFAULT_EXTENSIONS,
  62122. dedupe: config.resolve?.dedupe ?? [],
  62123. preserveSymlinks: config.resolve?.preserveSymlinks ?? false,
  62124. alias: resolvedAlias
  62125. };
  62126. if (
  62127. // @ts-expect-error removed field
  62128. config.resolve?.browserField === false && resolveOptions.mainFields.includes("browser")
  62129. ) {
  62130. logger.warn(
  62131. colors$1.yellow(
  62132. `\`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.`
  62133. )
  62134. );
  62135. }
  62136. const envDir = config.envDir ? normalizePath$3(path$n.resolve(resolvedRoot, config.envDir)) : resolvedRoot;
  62137. const userEnv = inlineConfig.envFile !== false && loadEnv(mode, envDir, resolveEnvPrefix(config));
  62138. const userNodeEnv = process.env.VITE_USER_NODE_ENV;
  62139. if (!isNodeEnvSet && userNodeEnv) {
  62140. if (userNodeEnv === "development") {
  62141. process.env.NODE_ENV = "development";
  62142. } else {
  62143. logger.warn(
  62144. `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.`
  62145. );
  62146. }
  62147. }
  62148. const isProduction = process.env.NODE_ENV === "production";
  62149. const isBuild = command === "build";
  62150. const relativeBaseShortcut = config.base === "" || config.base === "./";
  62151. const resolvedBase = relativeBaseShortcut ? !isBuild || config.build?.ssr ? "/" : "./" : resolveBaseUrl(config.base, isBuild, logger) ?? "/";
  62152. const resolvedBuildOptions = resolveBuildOptions(
  62153. config.build,
  62154. logger,
  62155. resolvedRoot
  62156. );
  62157. const pkgDir = findNearestPackageData(resolvedRoot, packageCache)?.dir;
  62158. const cacheDir = normalizePath$3(
  62159. config.cacheDir ? path$n.resolve(resolvedRoot, config.cacheDir) : pkgDir ? path$n.join(pkgDir, `node_modules/.vite`) : path$n.join(resolvedRoot, `.vite`)
  62160. );
  62161. const assetsFilter = config.assetsInclude && (!Array.isArray(config.assetsInclude) || config.assetsInclude.length) ? createFilter(config.assetsInclude) : () => false;
  62162. const createResolver = (options) => {
  62163. let aliasContainer;
  62164. let resolverContainer;
  62165. return async (id, importer, aliasOnly, ssr2) => {
  62166. let container;
  62167. if (aliasOnly) {
  62168. container = aliasContainer || (aliasContainer = await createPluginContainer({
  62169. ...resolved,
  62170. plugins: [alias$1({ entries: resolved.resolve.alias })]
  62171. }));
  62172. } else {
  62173. container = resolverContainer || (resolverContainer = await createPluginContainer({
  62174. ...resolved,
  62175. plugins: [
  62176. alias$1({ entries: resolved.resolve.alias }),
  62177. resolvePlugin({
  62178. ...resolved.resolve,
  62179. root: resolvedRoot,
  62180. isProduction,
  62181. isBuild: command === "build",
  62182. ssrConfig: resolved.ssr,
  62183. asSrc: true,
  62184. preferRelative: false,
  62185. tryIndex: true,
  62186. ...options,
  62187. idOnly: true,
  62188. fsUtils: getFsUtils(resolved)
  62189. })
  62190. ]
  62191. }));
  62192. }
  62193. return (await container.resolveId(id, importer, {
  62194. ssr: ssr2,
  62195. scan: options?.scan
  62196. }))?.id;
  62197. };
  62198. };
  62199. const { publicDir } = config;
  62200. const resolvedPublicDir = publicDir !== false && publicDir !== "" ? normalizePath$3(
  62201. path$n.resolve(
  62202. resolvedRoot,
  62203. typeof publicDir === "string" ? publicDir : "public"
  62204. )
  62205. ) : "";
  62206. const server = resolveServerOptions(resolvedRoot, config.server, logger);
  62207. const ssr = resolveSSROptions(config.ssr, resolveOptions.preserveSymlinks);
  62208. const optimizeDeps = config.optimizeDeps || {};
  62209. const BASE_URL = resolvedBase;
  62210. let resolved;
  62211. let createUserWorkerPlugins = config.worker?.plugins;
  62212. if (Array.isArray(createUserWorkerPlugins)) {
  62213. createUserWorkerPlugins = () => config.worker?.plugins;
  62214. logger.warn(
  62215. colors$1.yellow(
  62216. `worker.plugins is now a function that returns an array of plugins. Please update your Vite config accordingly.
  62217. `
  62218. )
  62219. );
  62220. }
  62221. const createWorkerPlugins = async function(bundleChain) {
  62222. const rawWorkerUserPlugins = (await asyncFlatten(createUserWorkerPlugins?.() || [])).filter(filterPlugin);
  62223. let workerConfig = mergeConfig({}, config);
  62224. const [workerPrePlugins, workerNormalPlugins, workerPostPlugins] = sortUserPlugins(rawWorkerUserPlugins);
  62225. const workerUserPlugins = [
  62226. ...workerPrePlugins,
  62227. ...workerNormalPlugins,
  62228. ...workerPostPlugins
  62229. ];
  62230. workerConfig = await runConfigHook(
  62231. workerConfig,
  62232. workerUserPlugins,
  62233. configEnv
  62234. );
  62235. const workerResolved = {
  62236. ...workerConfig,
  62237. ...resolved,
  62238. isWorker: true,
  62239. mainConfig: resolved,
  62240. bundleChain
  62241. };
  62242. const resolvedWorkerPlugins = await resolvePlugins(
  62243. workerResolved,
  62244. workerPrePlugins,
  62245. workerNormalPlugins,
  62246. workerPostPlugins
  62247. );
  62248. await Promise.all(
  62249. createPluginHookUtils(resolvedWorkerPlugins).getSortedPluginHooks("configResolved").map((hook) => hook(workerResolved))
  62250. );
  62251. return resolvedWorkerPlugins;
  62252. };
  62253. const resolvedWorkerOptions = {
  62254. format: config.worker?.format || "iife",
  62255. plugins: createWorkerPlugins,
  62256. rollupOptions: config.worker?.rollupOptions || {}
  62257. };
  62258. const base = withTrailingSlash(resolvedBase);
  62259. const preview = resolvePreviewOptions(config.preview, server);
  62260. resolved = {
  62261. configFile: configFile ? normalizePath$3(configFile) : void 0,
  62262. configFileDependencies: configFileDependencies.map(
  62263. (name) => normalizePath$3(path$n.resolve(name))
  62264. ),
  62265. inlineConfig,
  62266. root: resolvedRoot,
  62267. base,
  62268. decodedBase: decodeURI(base),
  62269. rawBase: resolvedBase,
  62270. resolve: resolveOptions,
  62271. publicDir: resolvedPublicDir,
  62272. cacheDir,
  62273. command,
  62274. mode,
  62275. ssr,
  62276. isWorker: false,
  62277. mainConfig: null,
  62278. bundleChain: [],
  62279. isProduction,
  62280. plugins: userPlugins,
  62281. css: resolveCSSOptions(config.css),
  62282. esbuild: config.esbuild === false ? false : {
  62283. jsxDev: !isProduction,
  62284. ...config.esbuild
  62285. },
  62286. server,
  62287. build: resolvedBuildOptions,
  62288. preview,
  62289. envDir,
  62290. env: {
  62291. ...userEnv,
  62292. BASE_URL,
  62293. MODE: mode,
  62294. DEV: !isProduction,
  62295. PROD: isProduction
  62296. },
  62297. assetsInclude(file) {
  62298. return DEFAULT_ASSETS_RE.test(file) || assetsFilter(file);
  62299. },
  62300. logger,
  62301. packageCache,
  62302. createResolver,
  62303. optimizeDeps: {
  62304. holdUntilCrawlEnd: true,
  62305. ...optimizeDeps,
  62306. esbuildOptions: {
  62307. preserveSymlinks: resolveOptions.preserveSymlinks,
  62308. ...optimizeDeps.esbuildOptions
  62309. }
  62310. },
  62311. worker: resolvedWorkerOptions,
  62312. appType: config.appType ?? "spa",
  62313. experimental: {
  62314. importGlobRestoreExtension: false,
  62315. hmrPartialAccept: false,
  62316. ...config.experimental
  62317. },
  62318. // random 72 bits (12 base64 chars)
  62319. // at least 64bits is recommended
  62320. // https://owasp.org/www-community/vulnerabilities/Insufficient_Session-ID_Length
  62321. webSocketToken: Buffer.from(
  62322. crypto$2.getRandomValues(new Uint8Array(9))
  62323. ).toString("base64url"),
  62324. additionalAllowedHosts: getAdditionalAllowedHosts(server, preview),
  62325. getSortedPlugins: void 0,
  62326. getSortedPluginHooks: void 0
  62327. };
  62328. resolved = {
  62329. ...config,
  62330. ...resolved
  62331. };
  62332. resolved.plugins = await resolvePlugins(
  62333. resolved,
  62334. prePlugins,
  62335. normalPlugins,
  62336. postPlugins
  62337. );
  62338. Object.assign(resolved, createPluginHookUtils(resolved.plugins));
  62339. await Promise.all(
  62340. resolved.getSortedPluginHooks("configResolved").map((hook) => hook(resolved))
  62341. );
  62342. optimizeDepsDisabledBackwardCompatibility(resolved, resolved.optimizeDeps);
  62343. optimizeDepsDisabledBackwardCompatibility(
  62344. resolved,
  62345. resolved.ssr.optimizeDeps,
  62346. "ssr."
  62347. );
  62348. debug?.(`using resolved config: %O`, {
  62349. ...resolved,
  62350. plugins: resolved.plugins.map((p) => p.name),
  62351. worker: {
  62352. ...resolved.worker,
  62353. plugins: `() => plugins`
  62354. }
  62355. });
  62356. if (config.build?.terserOptions && config.build.minify && config.build.minify !== "terser") {
  62357. logger.warn(
  62358. colors$1.yellow(
  62359. `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".`
  62360. )
  62361. );
  62362. }
  62363. const outputOption = config.build?.rollupOptions?.output ?? [];
  62364. if (Array.isArray(outputOption)) {
  62365. const assetFileNamesList = outputOption.map(
  62366. (output) => output.assetFileNames
  62367. );
  62368. if (assetFileNamesList.length > 1) {
  62369. const firstAssetFileNames = assetFileNamesList[0];
  62370. const hasDifferentReference = assetFileNamesList.some(
  62371. (assetFileNames) => assetFileNames !== firstAssetFileNames
  62372. );
  62373. if (hasDifferentReference) {
  62374. resolved.logger.warn(
  62375. colors$1.yellow(`
  62376. assetFileNames isn't equal for every build.rollupOptions.output. A single pattern across all outputs is supported by Vite.
  62377. `)
  62378. );
  62379. }
  62380. }
  62381. }
  62382. if (
  62383. // @ts-expect-error Option removed
  62384. config.legacy?.buildSsrCjsExternalHeuristics || // @ts-expect-error Option removed
  62385. config.ssr?.format === "cjs"
  62386. ) {
  62387. resolved.logger.warn(
  62388. colors$1.yellow(`
  62389. (!) Experimental legacy.buildSsrCjsExternalHeuristics and ssr.format were be removed in Vite 5.
  62390. The only SSR Output format is ESM. Find more information at https://github.com/vitejs/vite/discussions/13816.
  62391. `)
  62392. );
  62393. }
  62394. const resolvedBuildOutDir = normalizePath$3(
  62395. path$n.resolve(resolved.root, resolved.build.outDir)
  62396. );
  62397. if (isParentDirectory(resolvedBuildOutDir, resolved.root) || resolvedBuildOutDir === resolved.root) {
  62398. resolved.logger.warn(
  62399. colors$1.yellow(`
  62400. (!) 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.
  62401. `)
  62402. );
  62403. }
  62404. return resolved;
  62405. }
  62406. function resolveBaseUrl(base = "/", isBuild, logger) {
  62407. if (base[0] === ".") {
  62408. logger.warn(
  62409. colors$1.yellow(
  62410. colors$1.bold(
  62411. `(!) invalid "base" option: "${base}". The value can only be an absolute URL, "./", or an empty string.`
  62412. )
  62413. )
  62414. );
  62415. return "/";
  62416. }
  62417. const isExternal = isExternalUrl(base);
  62418. if (!isExternal && base[0] !== "/") {
  62419. logger.warn(
  62420. colors$1.yellow(
  62421. colors$1.bold(`(!) "base" option should start with a slash.`)
  62422. )
  62423. );
  62424. }
  62425. if (!isBuild || !isExternal) {
  62426. base = new URL(base, "http://vite.dev").pathname;
  62427. if (base[0] !== "/") {
  62428. base = "/" + base;
  62429. }
  62430. }
  62431. return base;
  62432. }
  62433. function sortUserPlugins(plugins) {
  62434. const prePlugins = [];
  62435. const postPlugins = [];
  62436. const normalPlugins = [];
  62437. if (plugins) {
  62438. plugins.flat().forEach((p) => {
  62439. if (p.enforce === "pre") prePlugins.push(p);
  62440. else if (p.enforce === "post") postPlugins.push(p);
  62441. else normalPlugins.push(p);
  62442. });
  62443. }
  62444. return [prePlugins, normalPlugins, postPlugins];
  62445. }
  62446. async function loadConfigFromFile(configEnv, configFile, configRoot = process.cwd(), logLevel, customLogger) {
  62447. const start = performance$1.now();
  62448. const getTime = () => `${(performance$1.now() - start).toFixed(2)}ms`;
  62449. let resolvedPath;
  62450. if (configFile) {
  62451. resolvedPath = path$n.resolve(configFile);
  62452. } else {
  62453. for (const filename of DEFAULT_CONFIG_FILES) {
  62454. const filePath = path$n.resolve(configRoot, filename);
  62455. if (!fs__default.existsSync(filePath)) continue;
  62456. resolvedPath = filePath;
  62457. break;
  62458. }
  62459. }
  62460. if (!resolvedPath) {
  62461. debug?.("no config file found.");
  62462. return null;
  62463. }
  62464. const isESM = typeof process.versions.deno === "string" || isFilePathESM(resolvedPath);
  62465. try {
  62466. const bundled = await bundleConfigFile(resolvedPath, isESM);
  62467. const userConfig = await loadConfigFromBundledFile(
  62468. resolvedPath,
  62469. bundled.code,
  62470. isESM
  62471. );
  62472. debug?.(`bundled config file loaded in ${getTime()}`);
  62473. const config = await (typeof userConfig === "function" ? userConfig(configEnv) : userConfig);
  62474. if (!isObject$1(config)) {
  62475. throw new Error(`config must export or return an object.`);
  62476. }
  62477. return {
  62478. path: normalizePath$3(resolvedPath),
  62479. config,
  62480. dependencies: bundled.dependencies
  62481. };
  62482. } catch (e) {
  62483. createLogger(logLevel, { customLogger }).error(
  62484. colors$1.red(`failed to load config from ${resolvedPath}`),
  62485. {
  62486. error: e
  62487. }
  62488. );
  62489. throw e;
  62490. }
  62491. }
  62492. async function bundleConfigFile(fileName, isESM) {
  62493. const dirnameVarName = "__vite_injected_original_dirname";
  62494. const filenameVarName = "__vite_injected_original_filename";
  62495. const importMetaUrlVarName = "__vite_injected_original_import_meta_url";
  62496. const result = await build$3({
  62497. absWorkingDir: process.cwd(),
  62498. entryPoints: [fileName],
  62499. write: false,
  62500. target: [`node${process.versions.node}`],
  62501. platform: "node",
  62502. bundle: true,
  62503. format: isESM ? "esm" : "cjs",
  62504. mainFields: ["main"],
  62505. sourcemap: "inline",
  62506. metafile: true,
  62507. define: {
  62508. __dirname: dirnameVarName,
  62509. __filename: filenameVarName,
  62510. "import.meta.url": importMetaUrlVarName,
  62511. "import.meta.dirname": dirnameVarName,
  62512. "import.meta.filename": filenameVarName
  62513. },
  62514. plugins: [
  62515. {
  62516. name: "externalize-deps",
  62517. setup(build2) {
  62518. const packageCache = /* @__PURE__ */ new Map();
  62519. const resolveByViteResolver = (id, importer, isRequire) => {
  62520. return tryNodeResolve(
  62521. id,
  62522. importer,
  62523. {
  62524. root: path$n.dirname(fileName),
  62525. isBuild: true,
  62526. isProduction: true,
  62527. preferRelative: false,
  62528. tryIndex: true,
  62529. mainFields: [],
  62530. conditions: [],
  62531. overrideConditions: ["node"],
  62532. dedupe: [],
  62533. extensions: DEFAULT_EXTENSIONS,
  62534. preserveSymlinks: false,
  62535. packageCache,
  62536. isRequire
  62537. },
  62538. false
  62539. )?.id;
  62540. };
  62541. build2.onResolve(
  62542. { filter: /^[^.].*/ },
  62543. async ({ path: id, importer, kind }) => {
  62544. if (kind === "entry-point" || path$n.isAbsolute(id) || isNodeBuiltin(id)) {
  62545. return;
  62546. }
  62547. if (isBuiltin(id)) {
  62548. return { external: true };
  62549. }
  62550. const isImport = isESM || kind === "dynamic-import";
  62551. let idFsPath;
  62552. try {
  62553. idFsPath = resolveByViteResolver(id, importer, !isImport);
  62554. } catch (e) {
  62555. if (!isImport) {
  62556. let canResolveWithImport = false;
  62557. try {
  62558. canResolveWithImport = !!resolveByViteResolver(
  62559. id,
  62560. importer,
  62561. false
  62562. );
  62563. } catch {
  62564. }
  62565. if (canResolveWithImport) {
  62566. throw new Error(
  62567. `Failed to resolve ${JSON.stringify(
  62568. id
  62569. )}. 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.`
  62570. );
  62571. }
  62572. }
  62573. throw e;
  62574. }
  62575. if (idFsPath && isImport) {
  62576. idFsPath = pathToFileURL(idFsPath).href;
  62577. }
  62578. if (idFsPath && !isImport && isFilePathESM(idFsPath, packageCache)) {
  62579. throw new Error(
  62580. `${JSON.stringify(
  62581. id
  62582. )} 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.`
  62583. );
  62584. }
  62585. return {
  62586. path: idFsPath,
  62587. external: true
  62588. };
  62589. }
  62590. );
  62591. }
  62592. },
  62593. {
  62594. name: "inject-file-scope-variables",
  62595. setup(build2) {
  62596. build2.onLoad({ filter: /\.[cm]?[jt]s$/ }, async (args) => {
  62597. const contents = await fsp.readFile(args.path, "utf-8");
  62598. const injectValues = `const ${dirnameVarName} = ${JSON.stringify(
  62599. path$n.dirname(args.path)
  62600. )};const ${filenameVarName} = ${JSON.stringify(args.path)};const ${importMetaUrlVarName} = ${JSON.stringify(
  62601. pathToFileURL(args.path).href
  62602. )};`;
  62603. return {
  62604. loader: args.path.endsWith("ts") ? "ts" : "js",
  62605. contents: injectValues + contents
  62606. };
  62607. });
  62608. }
  62609. }
  62610. ]
  62611. });
  62612. const { text } = result.outputFiles[0];
  62613. return {
  62614. code: text,
  62615. dependencies: result.metafile ? Object.keys(result.metafile.inputs) : []
  62616. };
  62617. }
  62618. const _require = createRequire$1(import.meta.url);
  62619. async function loadConfigFromBundledFile(fileName, bundledCode, isESM) {
  62620. if (isESM) {
  62621. const fileBase = `${fileName}.timestamp-${Date.now()}-${Math.random().toString(16).slice(2)}`;
  62622. const fileNameTmp = `${fileBase}.mjs`;
  62623. const fileUrl = `${pathToFileURL(fileBase)}.mjs`;
  62624. await fsp.writeFile(fileNameTmp, bundledCode);
  62625. try {
  62626. return (await import(fileUrl)).default;
  62627. } finally {
  62628. fs__default.unlink(fileNameTmp, () => {
  62629. });
  62630. }
  62631. } else {
  62632. const extension = path$n.extname(fileName);
  62633. const realFileName = await promisifiedRealpath(fileName);
  62634. const loaderExt = extension in _require.extensions ? extension : ".js";
  62635. const defaultLoader = _require.extensions[loaderExt];
  62636. _require.extensions[loaderExt] = (module, filename) => {
  62637. if (filename === realFileName) {
  62638. module._compile(bundledCode, filename);
  62639. } else {
  62640. defaultLoader(module, filename);
  62641. }
  62642. };
  62643. delete _require.cache[_require.resolve(fileName)];
  62644. const raw = _require(fileName);
  62645. _require.extensions[loaderExt] = defaultLoader;
  62646. return raw.__esModule ? raw.default : raw;
  62647. }
  62648. }
  62649. async function runConfigHook(config, plugins, configEnv) {
  62650. let conf = config;
  62651. for (const p of getSortedPluginsByHook("config", plugins)) {
  62652. const hook = p.config;
  62653. const handler = getHookHandler(hook);
  62654. if (handler) {
  62655. const res = await handler(conf, configEnv);
  62656. if (res) {
  62657. conf = mergeConfig(conf, res);
  62658. }
  62659. }
  62660. }
  62661. return conf;
  62662. }
  62663. function getDepOptimizationConfig(config, ssr) {
  62664. return ssr ? config.ssr.optimizeDeps : config.optimizeDeps;
  62665. }
  62666. function isDepsOptimizerEnabled(config, ssr) {
  62667. const optimizeDeps = getDepOptimizationConfig(config, ssr);
  62668. return !(optimizeDeps.noDiscovery && !optimizeDeps.include?.length);
  62669. }
  62670. function optimizeDepsDisabledBackwardCompatibility(resolved, optimizeDeps, optimizeDepsPath = "") {
  62671. const optimizeDepsDisabled = optimizeDeps.disabled;
  62672. if (optimizeDepsDisabled !== void 0) {
  62673. if (optimizeDepsDisabled === true || optimizeDepsDisabled === "dev") {
  62674. const commonjsOptionsInclude = resolved.build?.commonjsOptions?.include;
  62675. const commonjsPluginDisabled = Array.isArray(commonjsOptionsInclude) && commonjsOptionsInclude.length === 0;
  62676. optimizeDeps.noDiscovery = true;
  62677. optimizeDeps.include = void 0;
  62678. if (commonjsPluginDisabled) {
  62679. resolved.build.commonjsOptions.include = void 0;
  62680. }
  62681. resolved.logger.warn(
  62682. colors$1.yellow(`(!) Experimental ${optimizeDepsPath}optimizeDeps.disabled and deps pre-bundling during build were removed in Vite 5.1.
  62683. To disable the deps optimizer, set ${optimizeDepsPath}optimizeDeps.noDiscovery to true and ${optimizeDepsPath}optimizeDeps.include as undefined or empty.
  62684. Please remove ${optimizeDepsPath}optimizeDeps.disabled from your config.
  62685. ${commonjsPluginDisabled ? "Empty config.build.commonjsOptions.include will be ignored to support CJS during build. This config should also be removed." : ""}
  62686. `)
  62687. );
  62688. } else if (optimizeDepsDisabled === false || optimizeDepsDisabled === "build") {
  62689. resolved.logger.warn(
  62690. colors$1.yellow(`(!) Experimental ${optimizeDepsPath}optimizeDeps.disabled and deps pre-bundling during build were removed in Vite 5.1.
  62691. Setting it to ${optimizeDepsDisabled} now has no effect.
  62692. Please remove ${optimizeDepsPath}optimizeDeps.disabled from your config.
  62693. `)
  62694. );
  62695. }
  62696. }
  62697. }
  62698. export { resolveEnvPrefix as A, colors$1 as B, getDefaultExportFromCjs as C, commonjsGlobal as D, index$1 as E, index as F, build$1 as G, preview$1 as H, 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, isFileLoadingAllowed as y, loadEnv as z };