@farm.js/core 0.1.16 → 0.1.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/after.cjs +12 -225
- package/dist/agent-runtime.cjs +83 -2800
- package/dist/agent-runtime.d.mts +3 -3
- package/dist/agent-runtime.d.ts +3 -3
- package/dist/api/client.cjs +17 -4026
- package/dist/api/client.d.mts +3 -3
- package/dist/api/client.d.ts +3 -3
- package/dist/api/client.mjs +3 -3
- package/dist/api/route-manager.cjs +34 -4317
- package/dist/api/route-manager.d.mts +3 -3
- package/dist/api/route-manager.d.ts +3 -3
- package/dist/api/route-manager.mjs +5 -5
- package/dist/api/runtime.cjs +29 -2660
- package/dist/api/runtime.d.mts +3 -3
- package/dist/api/runtime.d.ts +3 -3
- package/dist/api/runtime.mjs +6 -3
- package/dist/{api-client-B-n3pm_Q.d.mts → api-client-Cx54IR5g.d.mts} +2 -2
- package/dist/{api-client-C0_NZEBl.d.ts → api-client-DykpaKcY.d.ts} +2 -2
- package/dist/api.cjs +88 -8361
- package/dist/api.d.mts +5 -5
- package/dist/api.d.ts +5 -5
- package/dist/api.mjs +8 -8
- package/dist/{app-AyjEsCBq.d.ts → app-BHY_10Lm.d.ts} +3 -3
- package/dist/{app-CYPHeOo_.d.mts → app-BMk0do23.d.mts} +3 -3
- package/dist/app-markdown.cjs +39 -280
- package/dist/app-markdown.d.mts +4 -4
- package/dist/app-markdown.d.ts +4 -4
- package/dist/app-markdown.mjs +3 -3
- package/dist/cache.cjs +38 -1526
- package/dist/cache.d.mts +11 -1
- package/dist/cache.d.ts +11 -1
- package/dist/cache.mjs +1 -1
- package/dist/chunk-22GUSVNJ.cjs +351 -0
- package/dist/chunk-2CQ74SP6.cjs +13 -0
- package/dist/chunk-2SJ4RQ6P.cjs +57 -0
- package/dist/chunk-3376ZTRC.cjs +1 -0
- package/dist/{chunk-IL5RWYK7.mjs → chunk-3XG26Z3J.mjs} +1 -1
- package/dist/{chunk-7HDZYYS4.mjs → chunk-4OOBRCJX.mjs} +27 -5
- package/dist/chunk-4PNUXIVU.cjs +472 -0
- package/dist/chunk-4PPEPOZZ.cjs +21 -0
- package/dist/chunk-4ZFL5GFN.cjs +520 -0
- package/dist/{chunk-B76DT5Y6.mjs → chunk-57J3Y7Y3.mjs} +1 -1
- package/dist/chunk-5OVTOKGH.cjs +65 -0
- package/dist/chunk-5QIKR7RR.cjs +224 -0
- package/dist/{chunk-MOQNHLTS.mjs → chunk-5QOEPUYE.mjs} +5 -2
- package/dist/{chunk-HKLRP3BN.mjs → chunk-5SPPCN5J.mjs} +1 -1
- package/dist/chunk-5ZSWG4UX.cjs +127 -0
- package/dist/chunk-62YJ2IEB.cjs +238 -0
- package/dist/chunk-6EODJYCX.cjs +183 -0
- package/dist/chunk-6EXBPJ24.cjs +434 -0
- package/dist/chunk-6QQDAAAZ.cjs +38 -0
- package/dist/chunk-6V4TDWEC.cjs +455 -0
- package/dist/{chunk-LUC2FAGW.mjs → chunk-6VQIKZZO.mjs} +1 -1
- package/dist/chunk-72ADZ6GQ.cjs +19 -0
- package/dist/chunk-75BRBIFP.cjs +172 -0
- package/dist/{chunk-7ZWL7XOU.mjs → chunk-76DG3LF3.mjs} +1 -1
- package/dist/chunk-7W3CPVXS.cjs +24 -0
- package/dist/chunk-A6WIQH5Z.cjs +31 -0
- package/dist/{chunk-EWDK36MM.mjs → chunk-A72YEUZ3.mjs} +1 -1
- package/dist/chunk-AFOD3LYZ.cjs +377 -0
- package/dist/chunk-AMDMVWMI.cjs +39 -0
- package/dist/chunk-AORPIPNB.cjs +552 -0
- package/dist/chunk-ARJWXNQL.cjs +256 -0
- package/dist/{chunk-ZHOIQC6U.mjs → chunk-ATERBVHQ.mjs} +2 -2
- package/dist/chunk-AWRISO2H.cjs +2199 -0
- package/dist/chunk-AYJR42WX.cjs +711 -0
- package/dist/chunk-B4CH33IG.cjs +644 -0
- package/dist/chunk-BIDYX3SL.cjs +91 -0
- package/dist/chunk-BSTRERPP.cjs +40 -0
- package/dist/chunk-BYODCJD6.cjs +663 -0
- package/dist/chunk-C3VRMB6H.cjs +85 -0
- package/dist/chunk-C5GUTBZK.cjs +173 -0
- package/dist/chunk-CIAGU2X4.cjs +337 -0
- package/dist/chunk-CMGMGHUZ.cjs +27 -0
- package/dist/chunk-CXP5U3RW.cjs +54 -0
- package/dist/chunk-D2ULAULS.cjs +166 -0
- package/dist/chunk-D7JTOLFK.cjs +285 -0
- package/dist/chunk-DVM4FKZL.cjs +169 -0
- package/dist/chunk-DYEJQTDN.cjs +49 -0
- package/dist/chunk-E2BMSZ2W.cjs +151 -0
- package/dist/chunk-E3UK5IIB.cjs +593 -0
- package/dist/chunk-EM6SBQKM.cjs +470 -0
- package/dist/{chunk-27JNCYEK.mjs → chunk-EN7WTEAY.mjs} +2 -2
- package/dist/{chunk-IGO2HEV7.mjs → chunk-F6SJAKFJ.mjs} +62 -28
- package/dist/{chunk-GSDIZACQ.mjs → chunk-FG5H2VCJ.mjs} +2 -2
- package/dist/chunk-FJSWYIR7.cjs +482 -0
- package/dist/chunk-FK6GO24V.cjs +54 -0
- package/dist/chunk-FXUUWYAX.cjs +654 -0
- package/dist/chunk-G3HRIVEY.cjs +395 -0
- package/dist/chunk-G643DQCH.cjs +13 -0
- package/dist/chunk-G7HXGLEC.cjs +175 -0
- package/dist/chunk-GDSHDTJ4.cjs +152 -0
- package/dist/chunk-GNEO2XOF.cjs +2040 -0
- package/dist/{chunk-NHHKW2T6.mjs → chunk-GYKJCFTS.mjs} +170 -52
- package/dist/chunk-H6G5INWM.cjs +1094 -0
- package/dist/chunk-HBTUGXHS.cjs +180 -0
- package/dist/chunk-HREOKQWT.cjs +45 -0
- package/dist/chunk-I4J452B7.cjs +435 -0
- package/dist/chunk-I57LW5VZ.cjs +94 -0
- package/dist/chunk-I7PUUDGY.cjs +389 -0
- package/dist/chunk-IAMRBF3Z.cjs +86 -0
- package/dist/chunk-IKNRN4VS.cjs +14214 -0
- package/dist/chunk-IQXFRNY2.cjs +169 -0
- package/dist/chunk-IXTFU4PL.cjs +2355 -0
- package/dist/chunk-J2OP5GG3.cjs +725 -0
- package/dist/chunk-J3LOGE4T.cjs +42 -0
- package/dist/chunk-J7FSKED3.cjs +93 -0
- package/dist/chunk-JDNHKPTD.cjs +22 -0
- package/dist/chunk-JSROZYYO.cjs +164 -0
- package/dist/chunk-K6FNHVDR.cjs +696 -0
- package/dist/chunk-KGZIORNL.cjs +43 -0
- package/dist/{chunk-LZK5CXDW.mjs → chunk-KLSSO6YD.mjs} +2 -2
- package/dist/{chunk-TYVVIQ5M.mjs → chunk-KPNVGRBL.mjs} +32 -25
- package/dist/chunk-KR6CKSXT.cjs +15 -0
- package/dist/chunk-KROWKCUH.cjs +185 -0
- package/dist/chunk-KVFPGF2M.cjs +46 -0
- package/dist/{chunk-KUO3GBPK.mjs → chunk-L6V3P73N.mjs} +1 -1
- package/dist/chunk-LCCBO7PJ.cjs +46 -0
- package/dist/chunk-LIUGYUX3.cjs +22 -0
- package/dist/chunk-LTNGXMQ6.cjs +55 -0
- package/dist/chunk-M2L4MYDQ.cjs +14 -0
- package/dist/chunk-MCJLA63J.cjs +1 -0
- package/dist/chunk-MKB2EGIR.cjs +148 -0
- package/dist/chunk-MQ3H6LT5.cjs +202 -0
- package/dist/chunk-MT763TDW.cjs +43 -0
- package/dist/chunk-MWSK6C4J.cjs +18 -0
- package/dist/chunk-MWWVKDW3.cjs +169 -0
- package/dist/{chunk-KTGFMHET.mjs → chunk-N6UROPT7.mjs} +1 -1
- package/dist/chunk-NO3WWW3N.cjs +192 -0
- package/dist/{chunk-NDD2YVGU.mjs → chunk-NRVGEI5I.mjs} +10 -3
- package/dist/chunk-NV3A5DXM.cjs +862 -0
- package/dist/chunk-NWOD6NIT.cjs +9 -0
- package/dist/chunk-NZKJGRB3.cjs +230 -0
- package/dist/chunk-OAVTUMJP.cjs +1496 -0
- package/dist/chunk-OHY2ZWUG.cjs +25 -0
- package/dist/chunk-OI6CMLWX.cjs +47 -0
- package/dist/chunk-OIEOCXKG.cjs +453 -0
- package/dist/chunk-OWOUTKIJ.cjs +558 -0
- package/dist/chunk-OX6O6WVA.cjs +74 -0
- package/dist/chunk-OXSIH4D6.cjs +476 -0
- package/dist/chunk-OYEUREB6.cjs +35 -0
- package/dist/chunk-PCOTGFSM.cjs +139 -0
- package/dist/chunk-PZYCPSCC.cjs +35 -0
- package/dist/chunk-QBK2AA4U.cjs +445 -0
- package/dist/chunk-QBPXNZGV.cjs +487 -0
- package/dist/{chunk-IXTEENRS.mjs → chunk-QGVJVSNX.mjs} +5 -5
- package/dist/chunk-QNPEUFHQ.cjs +27 -0
- package/dist/chunk-QZRVZSPN.cjs +236 -0
- package/dist/chunk-R5A5KNVX.cjs +20 -0
- package/dist/chunk-R5JIFR6B.cjs +42 -0
- package/dist/chunk-R6AIILII.cjs +193 -0
- package/dist/chunk-RG3ZOTG5.cjs +34 -0
- package/dist/chunk-ROUTX2E5.cjs +390 -0
- package/dist/chunk-RTHIVJ2G.cjs +54 -0
- package/dist/chunk-S3A45T7F.cjs +166 -0
- package/dist/chunk-S3JO3N4H.cjs +17 -0
- package/dist/chunk-SK35NQKK.cjs +27 -0
- package/dist/chunk-SU7MH53P.cjs +180 -0
- package/dist/chunk-SVRTJOUF.cjs +113 -0
- package/dist/chunk-SWN467MP.cjs +18 -0
- package/dist/{chunk-6SCB2MXB.mjs → chunk-SXHV42UQ.mjs} +16 -1
- package/dist/{chunk-OWTIOHON.mjs → chunk-SYGGPS4A.mjs} +2 -2
- package/dist/chunk-SYQ3MOWW.cjs +73 -0
- package/dist/chunk-T5CERMEH.cjs +417 -0
- package/dist/chunk-TFAXUHWX.cjs +206 -0
- package/dist/chunk-TIWHTW3Q.cjs +24 -0
- package/dist/chunk-TKJZFIML.cjs +20 -0
- package/dist/chunk-TKNSDBBN.cjs +193 -0
- package/dist/{chunk-NC333MFA.mjs → chunk-TSWW5BVH.mjs} +7 -4
- package/dist/chunk-TWGMSEHX.cjs +525 -0
- package/dist/chunk-TYQUSXUG.cjs +1357 -0
- package/dist/chunk-U4KT7QP3.cjs +23 -0
- package/dist/{chunk-LK6FGAQR.mjs → chunk-UDBJCUPA.mjs} +7 -4
- package/dist/chunk-UJ2C4ICS.cjs +277 -0
- package/dist/chunk-UN5GHC5V.cjs +10244 -0
- package/dist/chunk-UQZ4AYTH.cjs +168 -0
- package/dist/chunk-US2QVW3E.cjs +36 -0
- package/dist/{chunk-NUD6XBMO.mjs → chunk-UTBVHYS3.mjs} +2 -2
- package/dist/{chunk-76MNRHTS.mjs → chunk-UUYF3CHN.mjs} +2 -2
- package/dist/chunk-VB6UCX3V.cjs +421 -0
- package/dist/chunk-VEKFLAQM.cjs +300 -0
- package/dist/chunk-VQDBNVOO.cjs +14 -0
- package/dist/chunk-W4E7WMXE.cjs +71 -0
- package/dist/{chunk-VVTREAG5.mjs → chunk-WD4YXVLV.mjs} +11 -3
- package/dist/{chunk-UQZPTIML.mjs → chunk-WD5PNSOB.mjs} +2 -2
- package/dist/chunk-WJ3HK3LJ.cjs +1102 -0
- package/dist/chunk-WMAPPDMM.cjs +28 -0
- package/dist/chunk-WMOGFOUJ.cjs +223 -0
- package/dist/chunk-WPYFSQKK.cjs +37 -0
- package/dist/chunk-WZMNW2IW.cjs +1415 -0
- package/dist/chunk-XGC343EY.cjs +443 -0
- package/dist/{chunk-DPZUDPVJ.mjs → chunk-XP777LRD.mjs} +1 -1
- package/dist/chunk-XSKZORAS.cjs +727 -0
- package/dist/chunk-XWAWFQFS.cjs +82 -0
- package/dist/{chunk-KATQ6I56.mjs → chunk-XWAYMDLB.mjs} +1 -1
- package/dist/chunk-Y4YMF2NQ.cjs +460 -0
- package/dist/chunk-Y4YWHNKU.cjs +250 -0
- package/dist/chunk-Y53ZSFN6.cjs +504 -0
- package/dist/chunk-YA6LLTIJ.cjs +411 -0
- package/dist/chunk-YFPGHF5F.cjs +202 -0
- package/dist/chunk-YJRZR7VU.cjs +28 -0
- package/dist/chunk-YTAABQEV.cjs +1 -0
- package/dist/chunk-YX3DBN4M.cjs +18 -0
- package/dist/chunk-YZTASLPQ.cjs +64 -0
- package/dist/chunk-Z5HIWVDP.cjs +2173 -0
- package/dist/{chunk-Q3MDL7TM.mjs → chunk-ZCNJKLA2.mjs} +30 -11
- package/dist/chunk-ZEG3OPIB.cjs +93 -0
- package/dist/chunk-ZMN3H73H.cjs +155 -0
- package/dist/client/lifecycle.cjs +6 -35
- package/dist/{client-plugins-CqVtZi5X.d.ts → client-plugins-C089bQhN.d.ts} +2 -2
- package/dist/{client-plugins-CIPRtSsW.d.mts → client-plugins-JXXmZliS.d.mts} +2 -2
- package/dist/client-plugins.cjs +7 -448
- package/dist/client-plugins.d.mts +3 -3
- package/dist/client-plugins.d.ts +3 -3
- package/dist/client.cjs +171 -9774
- package/dist/client.d.mts +8 -8
- package/dist/client.d.ts +8 -8
- package/dist/client.mjs +6 -6
- package/dist/config.cjs +8 -36
- package/dist/config.d.mts +3 -3
- package/dist/config.d.ts +3 -3
- package/dist/cron.cjs +24 -491
- package/dist/cron.mjs +2 -2
- package/dist/deferred.cjs +26 -475
- package/dist/deployment.cjs +39 -210
- package/dist/devtools-OUIPHVV6.cjs +388 -0
- package/dist/{devtools-IU7LKZOH.mjs → devtools-UFVTMQX5.mjs} +7 -7
- package/dist/devtools-client-4WAPJY5L.cjs +254 -0
- package/dist/devtools-ui-5GBDIGMK.cjs +1296 -0
- package/dist/docs.cjs +46 -5649
- package/dist/docs.mjs +9 -9
- package/dist/{endpoint-kSAxE52R.d.ts → endpoint-1zA994xL.d.ts} +1 -1
- package/dist/{endpoint-Bu3XgeMK.d.mts → endpoint-DAc96i93.d.mts} +1 -1
- package/dist/env-types.cjs +5 -226
- package/dist/env.cjs +23 -306
- package/dist/env.d.mts +5 -3
- package/dist/env.d.ts +5 -3
- package/dist/env.mjs +2 -1
- package/dist/environment/vite.cjs +8 -372
- package/dist/environment.cjs +12 -94
- package/dist/font.cjs +13 -63
- package/dist/headers.cjs +7 -123
- package/dist/i18n/client.cjs +55 -294
- package/dist/i18n/index.cjs +22 -209
- package/dist/i18n/server.cjs +22 -745
- package/dist/image/server.cjs +13 -729
- package/dist/image/sharp.cjs +33 -171
- package/dist/image.cjs +41 -85
- package/dist/index.cjs +1151 -67862
- package/dist/index.d.mts +14 -14
- package/dist/index.d.ts +14 -14
- package/dist/index.mjs +34 -34
- package/dist/instrumentation.cjs +12 -138
- package/dist/integration-orm-55F7CII5.cjs +13 -0
- package/dist/integrations.cjs +55 -4090
- package/dist/integrations.d.mts +3 -3
- package/dist/integrations.d.ts +3 -3
- package/dist/internal/app-markdown-runtime.cjs +27 -224
- package/dist/internal/app-markdown-runtime.d.mts +3 -3
- package/dist/internal/app-markdown-runtime.d.ts +3 -3
- package/dist/internal/app-markdown-runtime.mjs +2 -2
- package/dist/internal/build-runtime.cjs +120 -52038
- package/dist/internal/build-runtime.d.mts +5 -5
- package/dist/internal/build-runtime.d.ts +5 -5
- package/dist/internal/build-runtime.mjs +23 -23
- package/dist/internal/cli-runtime.cjs +67 -13765
- package/dist/internal/cli-runtime.mjs +13 -13
- package/dist/internal/client-error-overlay.cjs +80 -733
- package/dist/internal/client-error-overlay.mjs +1 -1
- package/dist/internal/client-runtime.cjs +81 -913
- package/dist/internal/compression-runtime.cjs +8 -214
- package/dist/internal/compression-runtime.d.mts +3 -3
- package/dist/internal/compression-runtime.d.ts +3 -3
- package/dist/internal/config-runtime.cjs +42 -6535
- package/dist/internal/config-runtime.d.mts +4 -4
- package/dist/internal/config-runtime.d.ts +4 -4
- package/dist/internal/config-runtime.mjs +7 -7
- package/dist/internal/docs-precompiled-runtime.cjs +26 -46
- package/dist/internal/infra-analytics-runtime.cjs +10 -536
- package/dist/internal/isolated-boundary.cjs +14 -414
- package/dist/internal/metadata-image-runtime.cjs +7 -285
- package/dist/internal/product-telemetry-runtime.cjs +42 -97
- package/dist/internal/product-telemetry-runtime.mjs +1 -1
- package/dist/internal/production-node-env.cjs +6 -63
- package/dist/internal/production-runtime.cjs +418 -7073
- package/dist/internal/production-runtime.d.mts +9 -9
- package/dist/internal/production-runtime.d.ts +9 -9
- package/dist/internal/production-runtime.mjs +6 -6
- package/dist/internal/request-cookies.cjs +6 -54
- package/dist/json-ld.cjs +24 -50
- package/dist/layers-E4WPJNY5.cjs +17 -0
- package/dist/manager-G4B2IPYE.cjs +14 -0
- package/dist/markdown.cjs +21 -532
- package/dist/markdown.mjs +1 -1
- package/dist/{metadata-8g__AMPM.d.ts → metadata-BcNJP4fq.d.ts} +1 -1
- package/dist/{metadata-OMX6wFNf.d.mts → metadata-C1Z9vLbb.d.mts} +1 -1
- package/dist/middleware.cjs +36 -2988
- package/dist/middleware.d.mts +5 -5
- package/dist/middleware.d.ts +5 -5
- package/dist/middleware.mjs +4 -4
- package/dist/navigation.cjs +22 -2588
- package/dist/nitro-4I2MYLY2.cjs +652 -0
- package/dist/{nitro-3N4ZXRYM.mjs → nitro-CVZ36JM5.mjs} +4 -4
- package/dist/observability.cjs +22 -614
- package/dist/og.cjs +5 -28
- package/dist/plugin.cjs +106 -37291
- package/dist/plugin.d.mts +3 -3
- package/dist/plugin.d.ts +3 -3
- package/dist/plugin.mjs +19 -19
- package/dist/{production-runtime-W3M0eNDV.d.mts → production-runtime-jcrTlRTb.d.mts} +1 -1
- package/dist/{production-runtime-BJh7o6Cr.d.ts → production-runtime-oKmC0WU-.d.ts} +1 -1
- package/dist/query/client.cjs +113 -449
- package/dist/query/index.cjs +26 -288
- package/dist/query/parsers.cjs +23 -247
- package/dist/query/server.cjs +36 -298
- package/dist/{render-plan-io7pvDNc.d.mts → render-plan-BLIfc4jt.d.mts} +1 -1
- package/dist/{render-plan-Be4ZGTJP.d.ts → render-plan-DOB9cV-h.d.ts} +1 -1
- package/dist/renderer/react/client.cjs +9 -43
- package/dist/renderer/react/server.cjs +40 -103
- package/dist/renderer/react/vite.cjs +7 -29
- package/dist/renderer-client.cjs +126 -4261
- package/dist/renderer-client.d.mts +5 -5
- package/dist/renderer-client.d.ts +5 -5
- package/dist/renderer-client.mjs +3 -3
- package/dist/renderer.cjs +31 -249
- package/dist/{request-DZkHYQ89.d.mts → request-Cr53NRHr.d.mts} +1 -1
- package/dist/{request-G8JhKr4F.d.ts → request-Dt2gNuQD.d.ts} +1 -1
- package/dist/request.cjs +5 -68
- package/dist/request.d.mts +4 -4
- package/dist/request.d.ts +4 -4
- package/dist/{route-C_eLaFTZ.d.mts → route-B0XrsrFk.d.mts} +1 -1
- package/dist/{route-BxNJT447.d.ts → route-DOEpbyWZ.d.ts} +1 -1
- package/dist/{route-rendering-BcLLPJgN.d.ts → route-rendering-CWYd8Mv1.d.ts} +1 -1
- package/dist/{route-rendering-CbAKr1TY.d.mts → route-rendering-CeFBEAoZ.d.mts} +1 -1
- package/dist/router.cjs +11 -474
- package/dist/routes.cjs +42 -2424
- package/dist/routes.d.mts +4 -4
- package/dist/routes.d.ts +4 -4
- package/dist/routes.mjs +2 -2
- package/dist/schema-dev-notice-MXSZMVDG.cjs +56 -0
- package/dist/schema.cjs +6 -33
- package/dist/search-client-G3RICOFU.cjs +13 -0
- package/dist/server-action-security.cjs +25 -541
- package/dist/server-fn-client.cjs +6 -337
- package/dist/server-fn-client.d.mts +3 -3
- package/dist/server-fn-client.d.ts +3 -3
- package/dist/server-fn.cjs +32 -1799
- package/dist/server-fn.d.mts +1 -1
- package/dist/server-fn.d.ts +1 -1
- package/dist/server-fn.mjs +2 -2
- package/dist/server-plugins.cjs +15 -884
- package/dist/server-plugins.d.mts +4 -4
- package/dist/server-plugins.d.ts +4 -4
- package/dist/server-query-client.cjs +15 -1849
- package/dist/server-query-client.d.mts +1 -1
- package/dist/server-query-client.d.ts +1 -1
- package/dist/server-query-client.mjs +4 -4
- package/dist/server-query.cjs +16 -1796
- package/dist/server-query.d.mts +1 -1
- package/dist/server-query.d.ts +1 -1
- package/dist/server-query.mjs +3 -3
- package/dist/server.cjs +296 -44522
- package/dist/server.d.mts +9 -9
- package/dist/server.d.ts +9 -9
- package/dist/server.mjs +22 -22
- package/dist/{spa-router-BcS_4nqm.d.mts → spa-router-CQWiYGxS.d.mts} +2 -2
- package/dist/{spa-router-BzFBUJkn.d.ts → spa-router-Wjiw-8hB.d.ts} +2 -2
- package/dist/storage.cjs +57 -576
- package/dist/testing.cjs +92 -3148
- package/dist/testing.d.mts +3 -3
- package/dist/testing.d.ts +3 -3
- package/dist/testing.mjs +11 -7
- package/dist/theme/client.cjs +13 -108
- package/dist/theme/index.cjs +6 -68
- package/dist/theme/runtime.cjs +13 -78
- package/dist/theme/server.cjs +10 -179
- package/dist/{utils-C4yrAhNc.d.ts → utils-B0XJmG_b.d.ts} +1 -1
- package/dist/{utils-5kQP1mFR.d.mts → utils-uUxF0K0P.d.mts} +1 -1
- package/dist/version.cjs +4 -32
- package/dist/version.mjs +1 -1
- package/dist/vite.cjs +119 -37622
- package/dist/vite.d.mts +3 -3
- package/dist/vite.d.ts +3 -3
- package/dist/vite.mjs +19 -19
- package/dist/workflows.cjs +19 -1040
- package/dist/workflows.mjs +2 -2
- package/package.json +2 -2
- package/dist/{chunk-VO5SCWF5.mjs → chunk-BNGS6KJ6.mjs} +0 -0
- package/dist/{chunk-MUUZJLCI.mjs → chunk-EGJDUF4Q.mjs} +3 -3
- package/dist/{chunk-4L7SZOHZ.mjs → chunk-YHOCRI4H.mjs} +3 -3
package/dist/middleware.cjs
CHANGED
|
@@ -1,3017 +1,65 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __create = Object.create;
|
|
3
|
-
var __defProp = Object.defineProperty;
|
|
4
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
-
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
9
|
-
var __export = (target, all) => {
|
|
10
|
-
for (var name in all)
|
|
11
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
12
|
-
};
|
|
13
|
-
var __copyProps = (to, from, except, desc) => {
|
|
14
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
15
|
-
for (let key of __getOwnPropNames(from))
|
|
16
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
17
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
18
|
-
}
|
|
19
|
-
return to;
|
|
20
|
-
};
|
|
21
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
22
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
23
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
24
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
25
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
26
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
27
|
-
mod
|
|
28
|
-
));
|
|
29
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
30
2
|
|
|
31
|
-
// src/middleware/index.ts
|
|
32
|
-
var middleware_exports = {};
|
|
33
|
-
__export(middleware_exports, {
|
|
34
|
-
MiddlewareManager: () => MiddlewareManager,
|
|
35
|
-
UnsupportedRateLimitStorageError: () => UnsupportedRateLimitStorageError,
|
|
36
|
-
_runWithMiddlewareContext: () => _runWithMiddlewareContext,
|
|
37
|
-
_runWithMiddlewareData: () => _runWithMiddlewareData,
|
|
38
|
-
applyProductionMiddlewareHeaders: () => applyProductionMiddlewareHeaders,
|
|
39
|
-
canonicalizeRequestPathSegments: () => canonicalizeRequestPathSegments,
|
|
40
|
-
createContext: () => createContext,
|
|
41
|
-
createProductionMiddlewareRunner: () => createProductionMiddlewareRunner,
|
|
42
|
-
farmMiddlewarePlugin: () => farmMiddlewarePlugin,
|
|
43
|
-
getFromMiddleware: () => getFromMiddleware,
|
|
44
|
-
getMiddlewareContext: () => getMiddlewareContext,
|
|
45
|
-
getMiddlewareData: () => getMiddlewareData,
|
|
46
|
-
getMiddlewareValue: () => getMiddlewareValue,
|
|
47
|
-
getRateLimitStatus: () => getRateLimitStatus,
|
|
48
|
-
hasMiddlewareData: () => hasMiddlewareData,
|
|
49
|
-
matchesMiddlewareConfig: () => matchesMiddlewareConfig2,
|
|
50
|
-
memoryRateLimitStorage: () => memoryRateLimitStorage,
|
|
51
|
-
middleware: () => middleware,
|
|
52
|
-
unwrapMiddleware: () => unwrapMiddleware
|
|
53
|
-
});
|
|
54
|
-
module.exports = __toCommonJS(middleware_exports);
|
|
55
3
|
|
|
56
|
-
// src/middleware/chain.ts
|
|
57
|
-
var DEFAULT_MEMORY_RATE_LIMIT_ENTRIES = 1e5;
|
|
58
|
-
var _UnsupportedRateLimitStorageError = class _UnsupportedRateLimitStorageError extends TypeError {
|
|
59
|
-
constructor() {
|
|
60
|
-
super(
|
|
61
|
-
"RateLimitStorage must implement atomic increment(key, windowMs). Legacy get/set adapters can lose increments under concurrency. Use memoryRateLimitStorage() for one process or redisRateLimitStorage() from @farm.js/cache-redis for production."
|
|
62
|
-
);
|
|
63
|
-
this.name = "UnsupportedRateLimitStorageError";
|
|
64
|
-
}
|
|
65
|
-
};
|
|
66
|
-
__name(_UnsupportedRateLimitStorageError, "UnsupportedRateLimitStorageError");
|
|
67
|
-
var UnsupportedRateLimitStorageError = _UnsupportedRateLimitStorageError;
|
|
68
|
-
function memoryRateLimitStorage(options = {}) {
|
|
69
|
-
const maxEntries = options.maxEntries ?? DEFAULT_MEMORY_RATE_LIMIT_ENTRIES;
|
|
70
|
-
if (!Number.isSafeInteger(maxEntries) || maxEntries <= 0) {
|
|
71
|
-
throw new TypeError("memoryRateLimitStorage maxEntries must be a positive safe integer.");
|
|
72
|
-
}
|
|
73
|
-
const records = /* @__PURE__ */ new Map();
|
|
74
|
-
let earliestExpiry = Infinity;
|
|
75
|
-
function read(key, now) {
|
|
76
|
-
const record = records.get(key);
|
|
77
|
-
if (!record) return null;
|
|
78
|
-
if (record.resetAt <= now) {
|
|
79
|
-
records.delete(key);
|
|
80
|
-
return null;
|
|
81
|
-
}
|
|
82
|
-
return record;
|
|
83
|
-
}
|
|
84
|
-
__name(read, "read");
|
|
85
|
-
function pruneExpired(now) {
|
|
86
|
-
if (now < earliestExpiry) return;
|
|
87
|
-
earliestExpiry = Infinity;
|
|
88
|
-
for (const [key, record] of records) {
|
|
89
|
-
if (record.resetAt <= now) records.delete(key);
|
|
90
|
-
else earliestExpiry = Math.min(earliestExpiry, record.resetAt);
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
__name(pruneExpired, "pruneExpired");
|
|
94
|
-
return {
|
|
95
|
-
increment(key, windowMs) {
|
|
96
|
-
if (!Number.isSafeInteger(windowMs) || windowMs <= 0) {
|
|
97
|
-
throw new TypeError("Rate-limit windowMs must be a positive safe integer.");
|
|
98
|
-
}
|
|
99
|
-
const now = Date.now();
|
|
100
|
-
const current = read(key, now);
|
|
101
|
-
if (current) {
|
|
102
|
-
current.count++;
|
|
103
|
-
return { ...current };
|
|
104
|
-
}
|
|
105
|
-
if (records.size >= maxEntries) pruneExpired(now);
|
|
106
|
-
if (records.size >= maxEntries) {
|
|
107
|
-
throw new Error(
|
|
108
|
-
`Process-local rate-limit storage reached its ${maxEntries} active-key capacity. Configure a shared production adapter or increase maxEntries.`
|
|
109
|
-
);
|
|
110
|
-
}
|
|
111
|
-
const next = { count: 1, resetAt: now + windowMs };
|
|
112
|
-
records.set(key, next);
|
|
113
|
-
earliestExpiry = Math.min(earliestExpiry, next.resetAt);
|
|
114
|
-
return { ...next };
|
|
115
|
-
},
|
|
116
|
-
get(key) {
|
|
117
|
-
const record = read(key, Date.now());
|
|
118
|
-
return record ? { ...record } : null;
|
|
119
|
-
}
|
|
120
|
-
};
|
|
121
|
-
}
|
|
122
|
-
__name(memoryRateLimitStorage, "memoryRateLimitStorage");
|
|
123
|
-
var defaultRateLimitStorage = memoryRateLimitStorage();
|
|
124
|
-
function parseTimeWindow(window) {
|
|
125
|
-
const match = window.match(/^(\d+)(ms|s|m|h|d)$/);
|
|
126
|
-
if (!match) throw new Error(`Invalid time window: ${window}`);
|
|
127
|
-
const [, amount, unit] = match;
|
|
128
|
-
const value = Number.parseInt(amount, 10);
|
|
129
|
-
const multipliers = {
|
|
130
|
-
ms: 1,
|
|
131
|
-
s: 1e3,
|
|
132
|
-
m: 60 * 1e3,
|
|
133
|
-
h: 60 * 60 * 1e3,
|
|
134
|
-
d: 24 * 60 * 60 * 1e3
|
|
135
|
-
};
|
|
136
|
-
const windowMs = value * multipliers[unit];
|
|
137
|
-
if (!Number.isSafeInteger(windowMs) || windowMs <= 0) {
|
|
138
|
-
throw new Error(`Invalid time window: ${window}`);
|
|
139
|
-
}
|
|
140
|
-
return windowMs;
|
|
141
|
-
}
|
|
142
|
-
__name(parseTimeWindow, "parseTimeWindow");
|
|
143
|
-
function createRateLimitMiddleware(config) {
|
|
144
|
-
if (!Number.isSafeInteger(config.requests) || config.requests <= 0) {
|
|
145
|
-
throw new TypeError("Rate limit requests must be a positive safe integer.");
|
|
146
|
-
}
|
|
147
|
-
const windowMs = parseTimeWindow(config.window);
|
|
148
|
-
const storage = config.storage || defaultRateLimitStorage;
|
|
149
|
-
assertAtomicRateLimitStorage(storage);
|
|
150
|
-
return async (ctx, next) => {
|
|
151
|
-
const key = config.keyGenerator ? config.keyGenerator(ctx) : `${requireRateLimitClientAddress(ctx)}:${ctx.pathname}`;
|
|
152
|
-
const current = await storage.increment(key, windowMs);
|
|
153
|
-
assertRateLimitIncrementResult(current);
|
|
154
|
-
const now = Date.now();
|
|
155
|
-
const resetIn = Math.max(1, Math.ceil((current.resetAt - now) / 1e3));
|
|
156
|
-
const remaining = Math.max(0, config.requests - current.count);
|
|
157
|
-
setRateLimitHeaders(ctx, config.requests, remaining, resetIn, Math.ceil(windowMs / 1e3));
|
|
158
|
-
if (current.count > config.requests) {
|
|
159
|
-
setMiddlewareResponseHeader(ctx, "Retry-After", String(resetIn));
|
|
160
|
-
if (config.onLimit) {
|
|
161
|
-
const result = await config.onLimit(ctx);
|
|
162
|
-
if (result) return result;
|
|
163
|
-
}
|
|
164
|
-
ctx.json(
|
|
165
|
-
{
|
|
166
|
-
error: "Rate limit exceeded",
|
|
167
|
-
retryAfter: resetIn
|
|
168
|
-
},
|
|
169
|
-
429
|
|
170
|
-
);
|
|
171
|
-
return;
|
|
172
|
-
}
|
|
173
|
-
return next();
|
|
174
|
-
};
|
|
175
|
-
}
|
|
176
|
-
__name(createRateLimitMiddleware, "createRateLimitMiddleware");
|
|
177
|
-
function requireRateLimitClientAddress(ctx) {
|
|
178
|
-
const address = ctx.request.socket?.remoteAddress;
|
|
179
|
-
if (address) return address;
|
|
180
|
-
throw new Error(
|
|
181
|
-
"Rate limiting could not identify the caller: this runtime reports no client address. Set `server.trustProxy` so a proxy's x-forwarded-for is trusted, or pass a `keyGenerator` that derives the bucket from something you do have, such as a session or an API key."
|
|
182
|
-
);
|
|
183
|
-
}
|
|
184
|
-
__name(requireRateLimitClientAddress, "requireRateLimitClientAddress");
|
|
185
|
-
function assertAtomicRateLimitStorage(storage) {
|
|
186
|
-
if (!storage || typeof storage.increment !== "function") {
|
|
187
|
-
throw new UnsupportedRateLimitStorageError();
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
__name(assertAtomicRateLimitStorage, "assertAtomicRateLimitStorage");
|
|
191
|
-
function assertRateLimitIncrementResult(result) {
|
|
192
|
-
if (!result || !Number.isSafeInteger(result.count) || result.count <= 0 || !Number.isFinite(result.resetAt) || result.resetAt <= 0) {
|
|
193
|
-
throw new TypeError(
|
|
194
|
-
"RateLimitStorage.increment() must return a positive integer count and a resetAt timestamp."
|
|
195
|
-
);
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
__name(assertRateLimitIncrementResult, "assertRateLimitIncrementResult");
|
|
199
|
-
function setRateLimitHeaders(ctx, limit, remaining, resetIn, windowSeconds) {
|
|
200
|
-
setMiddlewareResponseHeader(ctx, "RateLimit-Policy", `"farm";q=${limit};w=${windowSeconds}`);
|
|
201
|
-
setMiddlewareResponseHeader(ctx, "RateLimit", `"farm";r=${remaining};t=${resetIn}`);
|
|
202
|
-
}
|
|
203
|
-
__name(setRateLimitHeaders, "setRateLimitHeaders");
|
|
204
|
-
function setMiddlewareResponseHeader(ctx, name, value) {
|
|
205
|
-
ctx.headers.set(name, value);
|
|
206
|
-
if (!ctx.response.headersSent && !ctx.response.writableEnded) {
|
|
207
|
-
ctx.response.setHeader(name, value);
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
__name(setMiddlewareResponseHeader, "setMiddlewareResponseHeader");
|
|
211
|
-
function matchPattern(pattern, pathname) {
|
|
212
|
-
if (pattern instanceof RegExp) {
|
|
213
|
-
return pattern.test(pathname);
|
|
214
|
-
}
|
|
215
|
-
const regexPattern = pattern.replace(/\*\*/g, "__DOUBLE_STAR__").replace(/\*/g, "__SINGLE_STAR__").replace(/[.+?^${}()|[\]\\/]/g, "\\$&").replace(/__DOUBLE_STAR__/g, ".*").replace(/__SINGLE_STAR__/g, "[^/]+");
|
|
216
|
-
const regex = new RegExp(`^${regexPattern}$`);
|
|
217
|
-
return regex.test(pathname);
|
|
218
|
-
}
|
|
219
|
-
__name(matchPattern, "matchPattern");
|
|
220
|
-
var _MiddlewareChainImpl = class _MiddlewareChainImpl {
|
|
221
|
-
constructor(basePath = "/") {
|
|
222
|
-
this.handlers = [];
|
|
223
|
-
this.basePath = basePath === "/" ? "/" : basePath.endsWith("/") ? basePath.slice(0, -1) : basePath;
|
|
224
|
-
}
|
|
225
|
-
/**
|
|
226
|
-
* Set the base path for this middleware chain (called by manager when loading)
|
|
227
|
-
*/
|
|
228
|
-
setBasePath(path4) {
|
|
229
|
-
this.basePath = path4 === "/" ? "/" : path4.endsWith("/") ? path4.slice(0, -1) : path4;
|
|
230
|
-
}
|
|
231
|
-
use(fn) {
|
|
232
|
-
this.handlers.push(fn);
|
|
233
|
-
return this;
|
|
234
|
-
}
|
|
235
|
-
/**
|
|
236
|
-
* Normalize a path pattern - auto-scope to middleware route
|
|
237
|
-
* If middleware is in /contact, then "/api" becomes "/contact/api"
|
|
238
|
-
*/
|
|
239
|
-
normalizePattern(pattern) {
|
|
240
|
-
if (this.basePath === "/") {
|
|
241
|
-
return pattern.startsWith("/") ? pattern : `/${pattern}`;
|
|
242
|
-
}
|
|
243
|
-
const cleanPattern = pattern.startsWith("/") ? pattern.slice(1) : pattern;
|
|
244
|
-
return `${this.basePath}/${cleanPattern}`;
|
|
245
|
-
}
|
|
246
|
-
/**
|
|
247
|
-
* Conditionally run middleware based on a condition.
|
|
248
|
-
* Only supports boolean values or functions that evaluate to boolean.
|
|
249
|
-
* Route-based string matching has been removed - use function conditions instead.
|
|
250
|
-
*
|
|
251
|
-
* @example
|
|
252
|
-
* .when(true, (ctx, next) => { ... }) // Always run
|
|
253
|
-
* .when((ctx) => ctx.data.get('flag'), (ctx, next) => { ... }) // Conditional
|
|
254
|
-
* .when((ctx) => ctx.pathname === '/contact', (ctx, next) => { ... }) // Path-based condition
|
|
255
|
-
*/
|
|
256
|
-
when(condition, fn) {
|
|
257
|
-
const conditionalMiddleware = /* @__PURE__ */ __name(async (ctx, next) => {
|
|
258
|
-
const shouldRun = typeof condition === "function" ? condition(ctx) : condition;
|
|
259
|
-
if (!shouldRun) {
|
|
260
|
-
return next();
|
|
261
|
-
}
|
|
262
|
-
if (typeof fn === "function" && fn.length === 2) {
|
|
263
|
-
return fn(ctx, next);
|
|
264
|
-
} else {
|
|
265
|
-
const subChain = middleware(this.basePath);
|
|
266
|
-
fn(subChain);
|
|
267
|
-
const { handlers } = subChain.build();
|
|
268
|
-
let index = 0;
|
|
269
|
-
let returnedResponse;
|
|
270
|
-
const executeNext = /* @__PURE__ */ __name(async () => {
|
|
271
|
-
if (index < handlers.length) {
|
|
272
|
-
const handler = handlers[index++];
|
|
273
|
-
const result = await handler(ctx, executeNext);
|
|
274
|
-
if (result instanceof Response) {
|
|
275
|
-
returnedResponse = result;
|
|
276
|
-
return result;
|
|
277
|
-
}
|
|
278
|
-
} else {
|
|
279
|
-
const result = await next();
|
|
280
|
-
if (result instanceof Response) {
|
|
281
|
-
returnedResponse = result;
|
|
282
|
-
return result;
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
return returnedResponse;
|
|
286
|
-
}, "executeNext");
|
|
287
|
-
return executeNext();
|
|
288
|
-
}
|
|
289
|
-
}, "conditionalMiddleware");
|
|
290
|
-
this.handlers.push(conditionalMiddleware);
|
|
291
|
-
return this;
|
|
292
|
-
}
|
|
293
|
-
rateLimit(config) {
|
|
294
|
-
this.handlers.push(createRateLimitMiddleware(config));
|
|
295
|
-
return this;
|
|
296
|
-
}
|
|
297
|
-
redirect(source, destination, permanent = false) {
|
|
298
|
-
const redirectMiddleware = /* @__PURE__ */ __name(async (ctx, next) => {
|
|
299
|
-
const normalizedSource = this.normalizePattern(source);
|
|
300
|
-
if (matchPattern(normalizedSource, ctx.pathname)) {
|
|
301
|
-
ctx.redirect(destination, permanent ? 308 : 307);
|
|
302
|
-
return;
|
|
303
|
-
}
|
|
304
|
-
return next();
|
|
305
|
-
}, "redirectMiddleware");
|
|
306
|
-
this.handlers.push(redirectMiddleware);
|
|
307
|
-
return this;
|
|
308
|
-
}
|
|
309
|
-
/**
|
|
310
|
-
* Rewrite the current middleware route to a new destination.
|
|
311
|
-
* Since each middleware file is route-specific, the source is automatically
|
|
312
|
-
* the middleware's route, so you only need to specify the destination.
|
|
313
|
-
*
|
|
314
|
-
* @param destination - The destination path to rewrite to
|
|
315
|
-
* @param condition - Optional boolean or function that evaluates to boolean.
|
|
316
|
-
* If provided and evaluates to false, rewrite is skipped.
|
|
317
|
-
* If not provided, rewrite always happens.
|
|
318
|
-
*
|
|
319
|
-
* @example
|
|
320
|
-
* // In /contact/middleware.ts
|
|
321
|
-
* .rewrite('/about') // Always rewrites /contact to /about
|
|
322
|
-
* .rewrite('/about', true) // Always rewrites
|
|
323
|
-
* .rewrite('/about', false) // Never rewrites
|
|
324
|
-
* .rewrite('/about', (ctx) => ctx.data.get('shouldRewrite')) // Conditional rewrite
|
|
325
|
-
*/
|
|
326
|
-
rewrite(destination, condition) {
|
|
327
|
-
const rewriteMiddleware = /* @__PURE__ */ __name(async (ctx, next) => {
|
|
328
|
-
if (condition !== void 0) {
|
|
329
|
-
const shouldRewrite = typeof condition === "function" ? condition(ctx) : condition;
|
|
330
|
-
if (!shouldRewrite) {
|
|
331
|
-
return next();
|
|
332
|
-
}
|
|
333
|
-
}
|
|
334
|
-
if (this.basePath !== "/") {
|
|
335
|
-
if (ctx.pathname === this.basePath) {
|
|
336
|
-
ctx.rewrite(destination);
|
|
337
|
-
} else if (ctx.pathname.startsWith(this.basePath + "/")) {
|
|
338
|
-
const subPath = ctx.pathname.slice(this.basePath.length);
|
|
339
|
-
const newPath = destination + subPath;
|
|
340
|
-
ctx.rewrite(newPath);
|
|
341
|
-
}
|
|
342
|
-
} else {
|
|
343
|
-
ctx.rewrite(destination);
|
|
344
|
-
}
|
|
345
|
-
return next();
|
|
346
|
-
}, "rewriteMiddleware");
|
|
347
|
-
this.handlers.push(rewriteMiddleware);
|
|
348
|
-
return this;
|
|
349
|
-
}
|
|
350
|
-
build() {
|
|
351
|
-
return {
|
|
352
|
-
handlers: this.handlers,
|
|
353
|
-
config: this.config
|
|
354
|
-
};
|
|
355
|
-
}
|
|
356
|
-
};
|
|
357
|
-
__name(_MiddlewareChainImpl, "MiddlewareChainImpl");
|
|
358
|
-
var MiddlewareChainImpl = _MiddlewareChainImpl;
|
|
359
|
-
async function getRateLimitStatus(key, limit, storage = defaultRateLimitStorage) {
|
|
360
|
-
if (!Number.isSafeInteger(limit) || limit <= 0) {
|
|
361
|
-
throw new TypeError("Rate limit must be a positive safe integer.");
|
|
362
|
-
}
|
|
363
|
-
assertAtomicRateLimitStorage(storage);
|
|
364
|
-
if (typeof storage.get !== "function") {
|
|
365
|
-
throw new TypeError(
|
|
366
|
-
"getRateLimitStatus() requires a RateLimitStorage adapter that implements get(key)."
|
|
367
|
-
);
|
|
368
|
-
}
|
|
369
|
-
const record = await storage.get(key);
|
|
370
|
-
if (!record) {
|
|
371
|
-
return {
|
|
372
|
-
requests: 0,
|
|
373
|
-
limit,
|
|
374
|
-
remaining: limit,
|
|
375
|
-
resetIn: null,
|
|
376
|
-
resetAt: null,
|
|
377
|
-
isLimited: false
|
|
378
|
-
};
|
|
379
|
-
}
|
|
380
|
-
assertRateLimitIncrementResult(record);
|
|
381
|
-
const now = Date.now();
|
|
382
|
-
if (record.resetAt && now > record.resetAt) {
|
|
383
|
-
return {
|
|
384
|
-
requests: 0,
|
|
385
|
-
limit,
|
|
386
|
-
remaining: limit,
|
|
387
|
-
resetIn: null,
|
|
388
|
-
resetAt: null,
|
|
389
|
-
isLimited: false
|
|
390
|
-
};
|
|
391
|
-
}
|
|
392
|
-
const requests = record.count || 0;
|
|
393
|
-
const remaining = Math.max(0, limit - requests);
|
|
394
|
-
const isLimited = requests >= limit;
|
|
395
|
-
let resetIn = null;
|
|
396
|
-
let resetAt = null;
|
|
397
|
-
if (record.resetAt) {
|
|
398
|
-
const remainingMs = record.resetAt - now;
|
|
399
|
-
resetIn = remainingMs > 0 ? Math.ceil(remainingMs / 1e3) : null;
|
|
400
|
-
resetAt = new Date(record.resetAt);
|
|
401
|
-
}
|
|
402
|
-
return {
|
|
403
|
-
requests,
|
|
404
|
-
limit,
|
|
405
|
-
remaining,
|
|
406
|
-
resetIn,
|
|
407
|
-
resetAt,
|
|
408
|
-
isLimited
|
|
409
|
-
};
|
|
410
|
-
}
|
|
411
|
-
__name(getRateLimitStatus, "getRateLimitStatus");
|
|
412
|
-
function middleware(basePath = "/") {
|
|
413
|
-
return new MiddlewareChainImpl(basePath);
|
|
414
|
-
}
|
|
415
|
-
__name(middleware, "middleware");
|
|
416
4
|
|
|
417
|
-
// src/server/request.ts
|
|
418
|
-
var import_node_async_hooks = require("async_hooks");
|
|
419
|
-
var import_node_stream = require("stream");
|
|
420
5
|
|
|
421
|
-
// src/server/request-bridge.ts
|
|
422
|
-
var CURRENT_REQUEST_RESOLVER_KEY = /* @__PURE__ */ Symbol.for("farm.currentRequestResolver");
|
|
423
|
-
function getGlobalState() {
|
|
424
|
-
return globalThis;
|
|
425
|
-
}
|
|
426
|
-
__name(getGlobalState, "getGlobalState");
|
|
427
|
-
function _setCurrentRequestResolver(resolver) {
|
|
428
|
-
getGlobalState()[CURRENT_REQUEST_RESOLVER_KEY] = resolver;
|
|
429
|
-
}
|
|
430
|
-
__name(_setCurrentRequestResolver, "_setCurrentRequestResolver");
|
|
431
6
|
|
|
432
|
-
// src/server/request.ts
|
|
433
|
-
var REQUEST_STORAGE_KEY = /* @__PURE__ */ Symbol.for("@farm.js/core/request-storage");
|
|
434
|
-
function getRequestStore() {
|
|
435
|
-
const runtime3 = globalThis;
|
|
436
|
-
const existing = runtime3[REQUEST_STORAGE_KEY];
|
|
437
|
-
if (existing instanceof import_node_async_hooks.AsyncLocalStorage) {
|
|
438
|
-
return existing;
|
|
439
|
-
}
|
|
440
|
-
const storage = new import_node_async_hooks.AsyncLocalStorage();
|
|
441
|
-
runtime3[REQUEST_STORAGE_KEY] = storage;
|
|
442
|
-
return storage;
|
|
443
|
-
}
|
|
444
|
-
__name(getRequestStore, "getRequestStore");
|
|
445
|
-
var requestStore = getRequestStore();
|
|
446
|
-
_setCurrentRequestResolver(() => requestStore.getStore());
|
|
447
|
-
function resolveFarmRequestURL(req, options = {}) {
|
|
448
|
-
if (options.origin) {
|
|
449
|
-
return new URL(req.url || "/", options.origin);
|
|
450
|
-
}
|
|
451
|
-
const forwardedHost = options.trustProxy ? firstForwardedHeaderValue(req.headers["x-forwarded-host"]) : void 0;
|
|
452
|
-
const fallbackHost = firstForwardedHeaderValue(req.headers.host) || "localhost";
|
|
453
|
-
const forwardedProto = options.trustProxy ? firstForwardedHeaderValue(req.headers["x-forwarded-proto"]) : void 0;
|
|
454
|
-
const normalizedProto = forwardedProto?.toLowerCase();
|
|
455
|
-
const proto = normalizedProto === "https" || normalizedProto === "http" ? normalizedProto : isEncryptedFarmRequest(req) ? "https" : "http";
|
|
456
|
-
return new URL(req.url || "/", resolveRequestOrigin(proto, forwardedHost, fallbackHost));
|
|
457
|
-
}
|
|
458
|
-
__name(resolveFarmRequestURL, "resolveFarmRequestURL");
|
|
459
|
-
function createWebRequestFromFarmRequest(req, options = {}) {
|
|
460
|
-
const fullUrl = resolveFarmRequestURL(req, options).toString();
|
|
461
|
-
const headers = new Headers();
|
|
462
|
-
for (const [key, value] of Object.entries(req.headers)) {
|
|
463
|
-
if (value == null) {
|
|
464
|
-
continue;
|
|
465
|
-
}
|
|
466
|
-
if (Array.isArray(value)) {
|
|
467
|
-
for (const item of value) {
|
|
468
|
-
headers.append(key, item);
|
|
469
|
-
}
|
|
470
|
-
continue;
|
|
471
|
-
}
|
|
472
|
-
headers.set(key, value);
|
|
473
|
-
}
|
|
474
|
-
const method = (req.method || "GET").toUpperCase();
|
|
475
|
-
const init = {
|
|
476
|
-
method: req.method,
|
|
477
|
-
headers
|
|
478
|
-
};
|
|
479
|
-
if (method !== "GET" && method !== "HEAD") {
|
|
480
|
-
init.body = import_node_stream.Readable.toWeb(req);
|
|
481
|
-
init.duplex = "half";
|
|
482
|
-
}
|
|
483
|
-
return new Request(fullUrl, init);
|
|
484
|
-
}
|
|
485
|
-
__name(createWebRequestFromFarmRequest, "createWebRequestFromFarmRequest");
|
|
486
|
-
function isEncryptedFarmRequest(req) {
|
|
487
|
-
return Boolean(req.socket?.encrypted);
|
|
488
|
-
}
|
|
489
|
-
__name(isEncryptedFarmRequest, "isEncryptedFarmRequest");
|
|
490
|
-
function firstForwardedHeaderValue(value) {
|
|
491
|
-
const first = Array.isArray(value) ? value[0] : value;
|
|
492
|
-
const token = first?.split(",", 1)[0]?.trim();
|
|
493
|
-
return token || void 0;
|
|
494
|
-
}
|
|
495
|
-
__name(firstForwardedHeaderValue, "firstForwardedHeaderValue");
|
|
496
|
-
function resolveRequestOrigin(proto, host, fallback) {
|
|
497
|
-
for (const candidate of [host, fallback, "localhost"]) {
|
|
498
|
-
if (!candidate) continue;
|
|
499
|
-
if (/[\s/?#@\\]/u.test(candidate)) continue;
|
|
500
|
-
try {
|
|
501
|
-
const url = new URL(`${proto}://${candidate}`);
|
|
502
|
-
if (url.username || url.password || url.pathname !== "/" || url.search || url.hash) continue;
|
|
503
|
-
return url.origin;
|
|
504
|
-
} catch {
|
|
505
|
-
}
|
|
506
|
-
}
|
|
507
|
-
return `${proto}://localhost`;
|
|
508
|
-
}
|
|
509
|
-
__name(resolveRequestOrigin, "resolveRequestOrigin");
|
|
510
7
|
|
|
511
|
-
// src/request-cookies.ts
|
|
512
|
-
function parseRequestCookieHeader(header) {
|
|
513
|
-
if (!header?.trim()) return [];
|
|
514
|
-
return header.split(";").map((part) => part.trim()).filter(Boolean).map((part) => {
|
|
515
|
-
const separatorIndex = part.indexOf("=");
|
|
516
|
-
if (separatorIndex === -1) {
|
|
517
|
-
return {
|
|
518
|
-
name: decodeCookiePart(part),
|
|
519
|
-
value: ""
|
|
520
|
-
};
|
|
521
|
-
}
|
|
522
|
-
return {
|
|
523
|
-
name: decodeCookiePart(part.slice(0, separatorIndex).trim()),
|
|
524
|
-
value: decodeCookiePart(part.slice(separatorIndex + 1).trim())
|
|
525
|
-
};
|
|
526
|
-
}).filter((cookie) => cookie.name.length > 0);
|
|
527
|
-
}
|
|
528
|
-
__name(parseRequestCookieHeader, "parseRequestCookieHeader");
|
|
529
|
-
function decodeCookiePart(value) {
|
|
530
|
-
try {
|
|
531
|
-
return decodeURIComponent(value);
|
|
532
|
-
} catch {
|
|
533
|
-
return value;
|
|
534
|
-
}
|
|
535
|
-
}
|
|
536
|
-
__name(decodeCookiePart, "decodeCookiePart");
|
|
537
8
|
|
|
538
|
-
// src/middleware/cookie-header.ts
|
|
539
|
-
function parseMiddlewareCookieHeader(cookieHeader) {
|
|
540
|
-
const cookies = /* @__PURE__ */ Object.create(null);
|
|
541
|
-
for (const cookie of parseRequestCookieHeader(cookieHeader)) {
|
|
542
|
-
if (!Object.prototype.hasOwnProperty.call(cookies, cookie.name)) {
|
|
543
|
-
cookies[cookie.name] = cookie.value;
|
|
544
|
-
}
|
|
545
|
-
}
|
|
546
|
-
return cookies;
|
|
547
|
-
}
|
|
548
|
-
__name(parseMiddlewareCookieHeader, "parseMiddlewareCookieHeader");
|
|
549
|
-
function applyCookieNamePrefixRequirements(name, options) {
|
|
550
|
-
if (name.startsWith("__Host-")) {
|
|
551
|
-
return { ...options, secure: true, path: "/", domain: void 0 };
|
|
552
|
-
}
|
|
553
|
-
if (name.startsWith("__Secure-")) {
|
|
554
|
-
return { ...options, secure: true };
|
|
555
|
-
}
|
|
556
|
-
return options;
|
|
557
|
-
}
|
|
558
|
-
__name(applyCookieNamePrefixRequirements, "applyCookieNamePrefixRequirements");
|
|
559
|
-
function serializeMiddlewareCookie(name, value, options = {}) {
|
|
560
|
-
const resolved = applyCookieNamePrefixRequirements(name, options);
|
|
561
|
-
let cookie = `${encodeURIComponent(name)}=${encodeURIComponent(value)}`;
|
|
562
|
-
if (resolved.maxAge != null) cookie += `; Max-Age=${resolved.maxAge}`;
|
|
563
|
-
if (resolved.expires) cookie += `; Expires=${resolved.expires.toUTCString()}`;
|
|
564
|
-
cookie += `; Path=${resolved.path || "/"}`;
|
|
565
|
-
if (resolved.domain) cookie += `; Domain=${resolved.domain}`;
|
|
566
|
-
if (resolved.secure) cookie += "; Secure";
|
|
567
|
-
if (resolved.httpOnly) cookie += "; HttpOnly";
|
|
568
|
-
if (resolved.sameSite) {
|
|
569
|
-
cookie += `; SameSite=${resolved.sameSite.charAt(0).toUpperCase()}${resolved.sameSite.slice(1)}`;
|
|
570
|
-
}
|
|
571
|
-
return cookie;
|
|
572
|
-
}
|
|
573
|
-
__name(serializeMiddlewareCookie, "serializeMiddlewareCookie");
|
|
574
|
-
function serializeMiddlewareCookieDeletion(name, options = {}) {
|
|
575
|
-
return serializeMiddlewareCookie(name, "", { ...options, maxAge: 0, expires: /* @__PURE__ */ new Date(0) });
|
|
576
|
-
}
|
|
577
|
-
__name(serializeMiddlewareCookieDeletion, "serializeMiddlewareCookieDeletion");
|
|
578
9
|
|
|
579
|
-
|
|
580
|
-
var _CookieJarImpl = class _CookieJarImpl {
|
|
581
|
-
constructor(req, res) {
|
|
582
|
-
this.req = req;
|
|
583
|
-
this.res = res;
|
|
584
|
-
this.cookies = parseMiddlewareCookieHeader(req.headers.cookie);
|
|
585
|
-
const existing = res.getHeader("Set-Cookie");
|
|
586
|
-
this.setCookies = Array.isArray(existing) ? existing.map(String) : existing === void 0 ? [] : [String(existing)];
|
|
587
|
-
}
|
|
588
|
-
get(name) {
|
|
589
|
-
return this.cookies[name];
|
|
590
|
-
}
|
|
591
|
-
set(name, value, options = {}) {
|
|
592
|
-
this.cookies[name] = value;
|
|
593
|
-
const cookieString = serializeMiddlewareCookie(name, value, options);
|
|
594
|
-
this.setCookies.push(cookieString);
|
|
595
|
-
this.res.setHeader("Set-Cookie", this.setCookies);
|
|
596
|
-
}
|
|
597
|
-
delete(name, options = {}) {
|
|
598
|
-
delete this.cookies[name];
|
|
599
|
-
this.setCookies.push(serializeMiddlewareCookieDeletion(name, options));
|
|
600
|
-
this.res.setHeader("Set-Cookie", this.setCookies);
|
|
601
|
-
}
|
|
602
|
-
getAll() {
|
|
603
|
-
return { ...this.cookies };
|
|
604
|
-
}
|
|
605
|
-
};
|
|
606
|
-
__name(_CookieJarImpl, "CookieJarImpl");
|
|
607
|
-
var CookieJarImpl = _CookieJarImpl;
|
|
608
|
-
function createContext(req, res, viteServer, parent, server) {
|
|
609
|
-
const url = resolveFarmRequestURL(req, { trustProxy: server?.trustProxy });
|
|
610
|
-
const headers = /* @__PURE__ */ new Map();
|
|
611
|
-
const data = parent?.data ? new Map(parent.data) : /* @__PURE__ */ new Map();
|
|
612
|
-
const locals = parent?.locals ? new Map(parent.locals) : /* @__PURE__ */ new Map();
|
|
613
|
-
const cookies = new CookieJarImpl(req, res);
|
|
614
|
-
if (parent?.headers) {
|
|
615
|
-
for (const [key, value] of Object.entries(parent.headers)) {
|
|
616
|
-
headers.set(key, value);
|
|
617
|
-
}
|
|
618
|
-
}
|
|
619
|
-
let handled = false;
|
|
620
|
-
const applyResponseHeaders = /* @__PURE__ */ __name(() => {
|
|
621
|
-
for (const [key, value] of headers) {
|
|
622
|
-
try {
|
|
623
|
-
res.setHeader(key, value);
|
|
624
|
-
} catch {
|
|
625
|
-
}
|
|
626
|
-
}
|
|
627
|
-
}, "applyResponseHeaders");
|
|
628
|
-
const ctx = {
|
|
629
|
-
request: req,
|
|
630
|
-
response: res,
|
|
631
|
-
url,
|
|
632
|
-
pathname: url.pathname,
|
|
633
|
-
searchParams: url.searchParams,
|
|
634
|
-
method: req.method || "GET",
|
|
635
|
-
params: {},
|
|
636
|
-
route: url.pathname,
|
|
637
|
-
parent,
|
|
638
|
-
vite: {
|
|
639
|
-
isDev: process.env.NODE_ENV !== "production",
|
|
640
|
-
hmr: !!viteServer?.hot,
|
|
641
|
-
server: viteServer
|
|
642
|
-
},
|
|
643
|
-
data,
|
|
644
|
-
locals,
|
|
645
|
-
headers,
|
|
646
|
-
cookies,
|
|
647
|
-
_handled: false,
|
|
648
|
-
redirect(redirectUrl, status = 307) {
|
|
649
|
-
if (handled) {
|
|
650
|
-
console.warn("Response already sent, cannot redirect");
|
|
651
|
-
return;
|
|
652
|
-
}
|
|
653
|
-
ctx._redirectUrl = redirectUrl;
|
|
654
|
-
ctx._handled = true;
|
|
655
|
-
handled = true;
|
|
656
|
-
applyResponseHeaders();
|
|
657
|
-
res.writeHead(status, {
|
|
658
|
-
Location: redirectUrl,
|
|
659
|
-
"Content-Type": "text/plain"
|
|
660
|
-
});
|
|
661
|
-
res.end(`Redirecting to ${redirectUrl}`);
|
|
662
|
-
},
|
|
663
|
-
rewrite(rewriteUrl) {
|
|
664
|
-
ctx._rewriteUrl = rewriteUrl;
|
|
665
|
-
const newUrl = new URL(rewriteUrl, ctx.url);
|
|
666
|
-
ctx.url = newUrl;
|
|
667
|
-
ctx.pathname = newUrl.pathname;
|
|
668
|
-
ctx.searchParams = newUrl.searchParams;
|
|
669
|
-
ctx.route = newUrl.pathname;
|
|
670
|
-
req.url = rewriteUrl;
|
|
671
|
-
},
|
|
672
|
-
json(jsonData, status = 200) {
|
|
673
|
-
if (handled) {
|
|
674
|
-
console.warn("Response already sent, cannot send JSON");
|
|
675
|
-
return;
|
|
676
|
-
}
|
|
677
|
-
ctx._handled = true;
|
|
678
|
-
handled = true;
|
|
679
|
-
applyResponseHeaders();
|
|
680
|
-
res.writeHead(status, {
|
|
681
|
-
"Content-Type": "application/json"
|
|
682
|
-
});
|
|
683
|
-
res.end(JSON.stringify(jsonData));
|
|
684
|
-
},
|
|
685
|
-
text(content, status = 200) {
|
|
686
|
-
if (handled) {
|
|
687
|
-
console.warn("Response already sent, cannot send text");
|
|
688
|
-
return;
|
|
689
|
-
}
|
|
690
|
-
ctx._handled = true;
|
|
691
|
-
handled = true;
|
|
692
|
-
applyResponseHeaders();
|
|
693
|
-
res.writeHead(status, {
|
|
694
|
-
"Content-Type": "text/plain"
|
|
695
|
-
});
|
|
696
|
-
res.end(content);
|
|
697
|
-
},
|
|
698
|
-
html(content, status = 200) {
|
|
699
|
-
if (handled) {
|
|
700
|
-
console.warn("Response already sent, cannot send HTML");
|
|
701
|
-
return;
|
|
702
|
-
}
|
|
703
|
-
ctx._handled = true;
|
|
704
|
-
handled = true;
|
|
705
|
-
applyResponseHeaders();
|
|
706
|
-
res.writeHead(status, {
|
|
707
|
-
"Content-Type": "text/html"
|
|
708
|
-
});
|
|
709
|
-
res.end(content);
|
|
710
|
-
}
|
|
711
|
-
};
|
|
712
|
-
return ctx;
|
|
713
|
-
}
|
|
714
|
-
__name(createContext, "createContext");
|
|
10
|
+
var _chunkFJSWYIR7cjs = require('./chunk-FJSWYIR7.cjs');
|
|
715
11
|
|
|
716
|
-
// src/middleware/manager.ts
|
|
717
|
-
var fs = __toESM(require("fs"));
|
|
718
|
-
var path = __toESM(require("path"));
|
|
719
12
|
|
|
720
|
-
// src/utils/file-module.ts
|
|
721
|
-
var import_node_url = require("url");
|
|
722
|
-
function toFileModuleUrl(filePath) {
|
|
723
|
-
return (0, import_node_url.pathToFileURL)(filePath).href;
|
|
724
|
-
}
|
|
725
|
-
__name(toFileModuleUrl, "toFileModuleUrl");
|
|
726
13
|
|
|
727
|
-
// src/middleware/module.ts
|
|
728
|
-
function isWebRequest(request) {
|
|
729
|
-
return typeof Request !== "undefined" && request instanceof Request;
|
|
730
|
-
}
|
|
731
|
-
__name(isWebRequest, "isWebRequest");
|
|
732
|
-
function toWebRequest(ctx) {
|
|
733
|
-
if (isWebRequest(ctx.request)) {
|
|
734
|
-
return ctx.request;
|
|
735
|
-
}
|
|
736
|
-
return createWebRequestFromFarmRequest(ctx.request, { origin: ctx.url.origin });
|
|
737
|
-
}
|
|
738
|
-
__name(toWebRequest, "toWebRequest");
|
|
739
|
-
function createRequestMiddlewareContext(ctx) {
|
|
740
|
-
const context2 = {
|
|
741
|
-
get url() {
|
|
742
|
-
return ctx.url;
|
|
743
|
-
},
|
|
744
|
-
get pathname() {
|
|
745
|
-
return ctx.pathname;
|
|
746
|
-
},
|
|
747
|
-
get searchParams() {
|
|
748
|
-
return ctx.searchParams;
|
|
749
|
-
},
|
|
750
|
-
get method() {
|
|
751
|
-
return ctx.method;
|
|
752
|
-
},
|
|
753
|
-
get params() {
|
|
754
|
-
return ctx.params;
|
|
755
|
-
},
|
|
756
|
-
get route() {
|
|
757
|
-
return ctx.route;
|
|
758
|
-
},
|
|
759
|
-
get locals() {
|
|
760
|
-
return ctx.locals;
|
|
761
|
-
},
|
|
762
|
-
get data() {
|
|
763
|
-
return ctx.data;
|
|
764
|
-
},
|
|
765
|
-
get headers() {
|
|
766
|
-
return ctx.headers;
|
|
767
|
-
},
|
|
768
|
-
get cookies() {
|
|
769
|
-
return ctx.cookies;
|
|
770
|
-
},
|
|
771
|
-
get(key) {
|
|
772
|
-
return ctx.locals.get(key);
|
|
773
|
-
},
|
|
774
|
-
has(key) {
|
|
775
|
-
return ctx.locals.has(key);
|
|
776
|
-
},
|
|
777
|
-
set(key, value) {
|
|
778
|
-
ctx.locals.set(key, value);
|
|
779
|
-
},
|
|
780
|
-
delete(key) {
|
|
781
|
-
return ctx.locals.delete(key);
|
|
782
|
-
},
|
|
783
|
-
redirect(url, status) {
|
|
784
|
-
ctx.redirect(url, status);
|
|
785
|
-
},
|
|
786
|
-
rewrite(url) {
|
|
787
|
-
ctx.rewrite(url);
|
|
788
|
-
},
|
|
789
|
-
json(data, status) {
|
|
790
|
-
ctx.json(data, status);
|
|
791
|
-
},
|
|
792
|
-
text(content, status) {
|
|
793
|
-
ctx.text(content, status);
|
|
794
|
-
},
|
|
795
|
-
html(content, status) {
|
|
796
|
-
ctx.html(content, status);
|
|
797
|
-
}
|
|
798
|
-
};
|
|
799
|
-
return context2;
|
|
800
|
-
}
|
|
801
|
-
__name(createRequestMiddlewareContext, "createRequestMiddlewareContext");
|
|
802
|
-
function normalizeMiddlewareModule(middlewareModule, routePath) {
|
|
803
|
-
const module2 = middlewareModule;
|
|
804
|
-
const defaultExport = module2.default;
|
|
805
|
-
const namedExport = module2.middleware;
|
|
806
|
-
const hasNamedExport = typeof namedExport === "function";
|
|
807
|
-
if (namedExport !== void 0 && !hasNamedExport) {
|
|
808
|
-
throw new TypeError("The named middleware export must be a function");
|
|
809
|
-
}
|
|
810
|
-
if (defaultExport && hasNamedExport) {
|
|
811
|
-
throw new Error(
|
|
812
|
-
"A middleware file cannot export both a default handler and a named middleware handler"
|
|
813
|
-
);
|
|
814
|
-
}
|
|
815
|
-
if (hasNamedExport) {
|
|
816
|
-
return {
|
|
817
|
-
handlers: [
|
|
818
|
-
async (ctx) => {
|
|
819
|
-
return namedExport(toWebRequest(ctx), createRequestMiddlewareContext(ctx));
|
|
820
|
-
}
|
|
821
|
-
],
|
|
822
|
-
config: module2.config
|
|
823
|
-
};
|
|
824
|
-
}
|
|
825
|
-
if (defaultExport && typeof defaultExport === "object" && "build" in defaultExport) {
|
|
826
|
-
if (typeof defaultExport.setBasePath === "function") {
|
|
827
|
-
defaultExport.setBasePath(routePath);
|
|
828
|
-
}
|
|
829
|
-
const built = defaultExport.build();
|
|
830
|
-
const handlers = Array.isArray(built?.handlers) ? built.handlers.filter((handler) => typeof handler === "function") : [];
|
|
831
|
-
return handlers.length > 0 ? {
|
|
832
|
-
handlers,
|
|
833
|
-
config: module2.config || built?.config
|
|
834
|
-
} : null;
|
|
835
|
-
}
|
|
836
|
-
if (typeof defaultExport === "function") {
|
|
837
|
-
return {
|
|
838
|
-
handlers: [defaultExport],
|
|
839
|
-
config: module2.config
|
|
840
|
-
};
|
|
841
|
-
}
|
|
842
|
-
return null;
|
|
843
|
-
}
|
|
844
|
-
__name(normalizeMiddlewareModule, "normalizeMiddlewareModule");
|
|
845
14
|
|
|
846
|
-
|
|
847
|
-
function decodeRouteSegment(segment) {
|
|
848
|
-
try {
|
|
849
|
-
return decodeURIComponent(segment);
|
|
850
|
-
} catch {
|
|
851
|
-
return segment;
|
|
852
|
-
}
|
|
853
|
-
}
|
|
854
|
-
__name(decodeRouteSegment, "decodeRouteSegment");
|
|
855
|
-
function canonicalizeRequestPathSegments(pathname) {
|
|
856
|
-
const segments = pathname.split("/").filter(Boolean);
|
|
857
|
-
if (!pathname.includes("%")) return segments;
|
|
858
|
-
return segments.map(decodeRouteSegment);
|
|
859
|
-
}
|
|
860
|
-
__name(canonicalizeRequestPathSegments, "canonicalizeRequestPathSegments");
|
|
861
|
-
function canonicalizeRequestPathname(pathname) {
|
|
862
|
-
const encoded = pathname.includes("%");
|
|
863
|
-
if (!encoded && !pathname.includes("//")) return pathname;
|
|
864
|
-
const collapsed = pathname.replace(/\/{2,}/g, "/");
|
|
865
|
-
if (!encoded) return collapsed;
|
|
866
|
-
return collapsed.split("/").map((segment) => {
|
|
867
|
-
const decoded = decodeRouteSegment(segment);
|
|
868
|
-
return decoded === segment ? segment : decoded.replace(/\//g, "%2F");
|
|
869
|
-
}).join("/");
|
|
870
|
-
}
|
|
871
|
-
__name(canonicalizeRequestPathname, "canonicalizeRequestPathname");
|
|
15
|
+
var _chunkBYODCJD6cjs = require('./chunk-BYODCJD6.cjs');
|
|
872
16
|
|
|
873
|
-
// src/utils.ts
|
|
874
|
-
var logger = {
|
|
875
|
-
info: /* @__PURE__ */ __name((message) => console.log(`[info] ${message}`), "info"),
|
|
876
|
-
success: /* @__PURE__ */ __name((message) => console.log(`[success] ${message}`), "success"),
|
|
877
|
-
warn: /* @__PURE__ */ __name((message) => console.warn(`\u26A0\uFE0F ${message}`), "warn"),
|
|
878
|
-
error: /* @__PURE__ */ __name((message) => console.error(`\u274C ${message}`), "error"),
|
|
879
|
-
ready: /* @__PURE__ */ __name((message) => console.log(`${message}`), "ready"),
|
|
880
|
-
event: /* @__PURE__ */ __name((message) => console.log(` ${message}`), "event")
|
|
881
|
-
};
|
|
882
17
|
|
|
883
|
-
// src/server/response.ts
|
|
884
|
-
function watchResponseEvent(res, events) {
|
|
885
|
-
if (typeof res.once !== "function" || typeof res.removeListener !== "function") {
|
|
886
|
-
return null;
|
|
887
|
-
}
|
|
888
|
-
let settled = false;
|
|
889
|
-
const listeners = events.map(([event, value]) => {
|
|
890
|
-
const listener = /* @__PURE__ */ __name(() => {
|
|
891
|
-
if (settled) return;
|
|
892
|
-
settled = true;
|
|
893
|
-
dispose();
|
|
894
|
-
resolvePromise(value);
|
|
895
|
-
}, "listener");
|
|
896
|
-
return { event, listener };
|
|
897
|
-
});
|
|
898
|
-
let resolvePromise;
|
|
899
|
-
const promise = new Promise((resolve) => {
|
|
900
|
-
resolvePromise = resolve;
|
|
901
|
-
for (const { event, listener } of listeners) {
|
|
902
|
-
res.once(event, listener);
|
|
903
|
-
}
|
|
904
|
-
});
|
|
905
|
-
const dispose = /* @__PURE__ */ __name(() => {
|
|
906
|
-
for (const { event, listener } of listeners) {
|
|
907
|
-
res.removeListener(event, listener);
|
|
908
|
-
}
|
|
909
|
-
}, "dispose");
|
|
910
|
-
return { promise, dispose };
|
|
911
|
-
}
|
|
912
|
-
__name(watchResponseEvent, "watchResponseEvent");
|
|
913
|
-
async function waitForWritable(res) {
|
|
914
|
-
if (res.writableEnded || res.destroyed) {
|
|
915
|
-
return false;
|
|
916
|
-
}
|
|
917
|
-
const watcher = watchResponseEvent(res, [
|
|
918
|
-
["drain", true],
|
|
919
|
-
["close", false],
|
|
920
|
-
["error", false]
|
|
921
|
-
]);
|
|
922
|
-
if (!watcher) {
|
|
923
|
-
return false;
|
|
924
|
-
}
|
|
925
|
-
try {
|
|
926
|
-
return await watcher.promise;
|
|
927
|
-
} finally {
|
|
928
|
-
watcher.dispose();
|
|
929
|
-
}
|
|
930
|
-
}
|
|
931
|
-
__name(waitForWritable, "waitForWritable");
|
|
932
|
-
function splitSetCookieHeader(value) {
|
|
933
|
-
const cookies = [];
|
|
934
|
-
let start = 0;
|
|
935
|
-
for (let index = 0; index < value.length; index += 1) {
|
|
936
|
-
if (value[index] !== ",") continue;
|
|
937
|
-
let next = index + 1;
|
|
938
|
-
while (value[next] === " " || value[next] === " ") next += 1;
|
|
939
|
-
const equals = value.indexOf("=", next);
|
|
940
|
-
if (equals === -1) continue;
|
|
941
|
-
const separator = value.slice(next, equals);
|
|
942
|
-
if (separator.length === 0 || /[;,\s]/.test(separator)) continue;
|
|
943
|
-
cookies.push(value.slice(start, index).trim());
|
|
944
|
-
start = next;
|
|
945
|
-
index = next - 1;
|
|
946
|
-
}
|
|
947
|
-
cookies.push(value.slice(start).trim());
|
|
948
|
-
return cookies.filter(Boolean);
|
|
949
|
-
}
|
|
950
|
-
__name(splitSetCookieHeader, "splitSetCookieHeader");
|
|
951
|
-
function applyWebResponseHeaders(res, headers, options = {}) {
|
|
952
|
-
const responseHeaders = headers;
|
|
953
|
-
const rawSetCookies = responseHeaders.raw?.()["set-cookie"];
|
|
954
|
-
const setCookies = responseHeaders.getSetCookie?.() || rawSetCookies || [];
|
|
955
|
-
const existing = options.appendSetCookie && typeof res.getHeader === "function" ? res.getHeader("Set-Cookie") : void 0;
|
|
956
|
-
const existingCookies = Array.isArray(existing) ? existing.map(String) : existing === void 0 ? [] : [String(existing)];
|
|
957
|
-
let fallbackSetCookie = "";
|
|
958
|
-
headers.forEach((value, key) => {
|
|
959
|
-
if (key.toLowerCase() === "set-cookie") {
|
|
960
|
-
fallbackSetCookie = value;
|
|
961
|
-
return;
|
|
962
|
-
}
|
|
963
|
-
res.setHeader(key, value);
|
|
964
|
-
});
|
|
965
|
-
const cookies = setCookies.length > 0 ? setCookies : fallbackSetCookie ? splitSetCookieHeader(fallbackSetCookie) : [];
|
|
966
|
-
if (cookies.length > 0) {
|
|
967
|
-
res.setHeader("Set-Cookie", [...existingCookies, ...cookies]);
|
|
968
|
-
}
|
|
969
|
-
}
|
|
970
|
-
__name(applyWebResponseHeaders, "applyWebResponseHeaders");
|
|
971
|
-
async function sendWebResponse(res, response) {
|
|
972
|
-
res.statusCode = response.status;
|
|
973
|
-
applyWebResponseHeaders(res, response.headers, { appendSetCookie: true });
|
|
974
|
-
if (!response.body) {
|
|
975
|
-
res.end();
|
|
976
|
-
return;
|
|
977
|
-
}
|
|
978
|
-
if (typeof res.write !== "function") {
|
|
979
|
-
const body = await response.arrayBuffer();
|
|
980
|
-
res.end(Buffer.from(body));
|
|
981
|
-
return;
|
|
982
|
-
}
|
|
983
|
-
const reader = response.body.getReader();
|
|
984
|
-
const disconnectWatcher = watchResponseEvent(res, [
|
|
985
|
-
["close", true],
|
|
986
|
-
["error", true]
|
|
987
|
-
]);
|
|
988
|
-
try {
|
|
989
|
-
while (true) {
|
|
990
|
-
if (res.destroyed) {
|
|
991
|
-
void reader.cancel().catch(() => {
|
|
992
|
-
});
|
|
993
|
-
return;
|
|
994
|
-
}
|
|
995
|
-
const read = reader.read().then((result) => ({ type: "read", result }));
|
|
996
|
-
const next = disconnectWatcher ? await Promise.race([
|
|
997
|
-
read,
|
|
998
|
-
disconnectWatcher.promise.then(() => ({ type: "disconnect" }))
|
|
999
|
-
]) : await read;
|
|
1000
|
-
if (next.type === "disconnect") {
|
|
1001
|
-
void reader.cancel().catch(() => {
|
|
1002
|
-
});
|
|
1003
|
-
return;
|
|
1004
|
-
}
|
|
1005
|
-
const { done, value } = next.result;
|
|
1006
|
-
if (done) {
|
|
1007
|
-
break;
|
|
1008
|
-
}
|
|
1009
|
-
if (!value || value.byteLength === 0) {
|
|
1010
|
-
continue;
|
|
1011
|
-
}
|
|
1012
|
-
if (!res.write(value)) {
|
|
1013
|
-
if (!await waitForWritable(res)) {
|
|
1014
|
-
void reader.cancel().catch(() => {
|
|
1015
|
-
});
|
|
1016
|
-
return;
|
|
1017
|
-
}
|
|
1018
|
-
}
|
|
1019
|
-
}
|
|
1020
|
-
res.end();
|
|
1021
|
-
} catch (error) {
|
|
1022
|
-
void reader.cancel(error).catch(() => {
|
|
1023
|
-
});
|
|
1024
|
-
if (!res.writableEnded) {
|
|
1025
|
-
const responseError = error instanceof Error ? error : new Error(String(error));
|
|
1026
|
-
if (typeof res.destroy === "function") {
|
|
1027
|
-
res.destroy(responseError);
|
|
1028
|
-
} else {
|
|
1029
|
-
res.end();
|
|
1030
|
-
}
|
|
1031
|
-
}
|
|
1032
|
-
throw error;
|
|
1033
|
-
} finally {
|
|
1034
|
-
disconnectWatcher?.dispose();
|
|
1035
|
-
try {
|
|
1036
|
-
reader.releaseLock();
|
|
1037
|
-
} catch {
|
|
1038
|
-
}
|
|
1039
|
-
}
|
|
1040
|
-
}
|
|
1041
|
-
__name(sendWebResponse, "sendWebResponse");
|
|
1042
18
|
|
|
1043
|
-
|
|
1044
|
-
var import_api = require("@opentelemetry/api");
|
|
1045
|
-
var FARM_TRACER_NAME = "@farm.js/core";
|
|
1046
|
-
var ALL_SPAN_KINDS = [
|
|
1047
|
-
"request",
|
|
1048
|
-
"render",
|
|
1049
|
-
"middleware",
|
|
1050
|
-
"api",
|
|
1051
|
-
"integration",
|
|
1052
|
-
"storage",
|
|
1053
|
-
"ppr",
|
|
1054
|
-
"build",
|
|
1055
|
-
"plugin"
|
|
1056
|
-
];
|
|
1057
|
-
var DEFAULT_IGNORED_PATHS = [
|
|
1058
|
-
"/@vite/",
|
|
1059
|
-
"/@fs/",
|
|
1060
|
-
"/@id/",
|
|
1061
|
-
"/@react-refresh",
|
|
1062
|
-
"/node_modules/",
|
|
1063
|
-
"/__vite",
|
|
1064
|
-
"/.well-known/appspecific/"
|
|
1065
|
-
];
|
|
1066
|
-
var FARM_REQUEST_METHOD_CONTEXT_KEY = (0, import_api.createContextKey)("@farm.js/core/request-method");
|
|
1067
|
-
var TRACING_RUNTIME = /* @__PURE__ */ Symbol.for("@farm.js/core/tracing-runtime/v1");
|
|
1068
|
-
var runtime = globalThis;
|
|
1069
|
-
var tracingState = runtime[TRACING_RUNTIME] ?? (runtime[TRACING_RUNTIME] = normalizeFarmTracingConfig(false));
|
|
1070
|
-
function normalizeFarmTracingConfig(config) {
|
|
1071
|
-
if (!config) {
|
|
1072
|
-
return {
|
|
1073
|
-
enabled: false,
|
|
1074
|
-
spans: new Set(ALL_SPAN_KINDS),
|
|
1075
|
-
recordEvents: true,
|
|
1076
|
-
attributes: {},
|
|
1077
|
-
ignorePaths: DEFAULT_IGNORED_PATHS
|
|
1078
|
-
};
|
|
1079
|
-
}
|
|
1080
|
-
if (config === true) {
|
|
1081
|
-
return {
|
|
1082
|
-
enabled: true,
|
|
1083
|
-
spans: new Set(ALL_SPAN_KINDS),
|
|
1084
|
-
recordEvents: true,
|
|
1085
|
-
attributes: {},
|
|
1086
|
-
ignorePaths: DEFAULT_IGNORED_PATHS
|
|
1087
|
-
};
|
|
1088
|
-
}
|
|
1089
|
-
return {
|
|
1090
|
-
enabled: config.enabled ?? true,
|
|
1091
|
-
spans: new Set(config.spans ?? ALL_SPAN_KINDS),
|
|
1092
|
-
recordEvents: config.recordEvents ?? true,
|
|
1093
|
-
attributes: { ...config.attributes },
|
|
1094
|
-
ignorePaths: [...DEFAULT_IGNORED_PATHS, ...config.ignorePaths ?? []]
|
|
1095
|
-
};
|
|
1096
|
-
}
|
|
1097
|
-
__name(normalizeFarmTracingConfig, "normalizeFarmTracingConfig");
|
|
1098
|
-
function recordFarmEventTrace(event) {
|
|
1099
|
-
if (!tracingState.enabled) return void 0;
|
|
1100
|
-
const activeContext = import_api.context.active();
|
|
1101
|
-
const activeSpan = import_api.trace.getSpan(activeContext);
|
|
1102
|
-
const traceContext = getSpanTraceContext(activeSpan);
|
|
1103
|
-
if (activeSpan && traceContext) {
|
|
1104
|
-
if (event.type === "route.matched") {
|
|
1105
|
-
const method = activeContext.getValue(FARM_REQUEST_METHOD_CONTEXT_KEY) ?? "HTTP";
|
|
1106
|
-
activeSpan.updateName(`${method} ${event.route}`);
|
|
1107
|
-
activeSpan.setAttribute("http.route", event.route);
|
|
1108
|
-
activeSpan.setAttribute("farm.route", event.route);
|
|
1109
|
-
}
|
|
1110
|
-
if (tracingState.recordEvents) {
|
|
1111
|
-
activeSpan.addEvent(event.type, toEventAttributes(event), event.timestamp);
|
|
1112
|
-
}
|
|
1113
|
-
const error = event.type === "request.error" ? void 0 : getEventError(event);
|
|
1114
|
-
if (error !== void 0) {
|
|
1115
|
-
if (event.type === "render.error") {
|
|
1116
|
-
recordSpanException(activeSpan, error);
|
|
1117
|
-
} else {
|
|
1118
|
-
recordSpanError(activeSpan, error);
|
|
1119
|
-
}
|
|
1120
|
-
}
|
|
1121
|
-
}
|
|
1122
|
-
const completedTraceContext = recordCompletedEventSpan(event, activeContext);
|
|
1123
|
-
return traceContext ?? completedTraceContext;
|
|
1124
|
-
}
|
|
1125
|
-
__name(recordFarmEventTrace, "recordFarmEventTrace");
|
|
1126
|
-
function recordCompletedEventSpan(event, parentContext) {
|
|
1127
|
-
const descriptor = getCompletedSpanDescriptor(event);
|
|
1128
|
-
if (!descriptor || !tracingState.spans.has(descriptor.kind)) return void 0;
|
|
1129
|
-
const tracer = import_api.trace.getTracer(FARM_TRACER_NAME);
|
|
1130
|
-
const span = tracer.startSpan(
|
|
1131
|
-
descriptor.name,
|
|
1132
|
-
{
|
|
1133
|
-
kind: import_api.SpanKind.INTERNAL,
|
|
1134
|
-
startTime: event.timestamp - descriptor.durationMs,
|
|
1135
|
-
attributes: {
|
|
1136
|
-
...tracingState.attributes,
|
|
1137
|
-
...toEventAttributes(event),
|
|
1138
|
-
"farm.event.type": event.type
|
|
1139
|
-
}
|
|
1140
|
-
},
|
|
1141
|
-
parentContext
|
|
1142
|
-
);
|
|
1143
|
-
const status = "status" in event && typeof event.status === "number" ? event.status : void 0;
|
|
1144
|
-
if (status !== void 0) setResponseStatus(span, status);
|
|
1145
|
-
const error = getEventError(event);
|
|
1146
|
-
if (error !== void 0) recordSpanError(span, error);
|
|
1147
|
-
const traceContext = getSpanTraceContext(span);
|
|
1148
|
-
span.end(event.timestamp);
|
|
1149
|
-
return traceContext;
|
|
1150
|
-
}
|
|
1151
|
-
__name(recordCompletedEventSpan, "recordCompletedEventSpan");
|
|
1152
|
-
function getCompletedSpanDescriptor(event) {
|
|
1153
|
-
if (!("durationMs" in event) || typeof event.durationMs !== "number") return void 0;
|
|
1154
|
-
switch (event.type) {
|
|
1155
|
-
case "render.complete":
|
|
1156
|
-
return { kind: "render", name: `farm.render ${event.route}`, durationMs: event.durationMs };
|
|
1157
|
-
case "render.stream.shellReady":
|
|
1158
|
-
return {
|
|
1159
|
-
kind: "render",
|
|
1160
|
-
name: `farm.render.shell ${event.route}`,
|
|
1161
|
-
durationMs: event.durationMs
|
|
1162
|
-
};
|
|
1163
|
-
case "render.stream.complete":
|
|
1164
|
-
return {
|
|
1165
|
-
kind: "render",
|
|
1166
|
-
name: `farm.render.stream ${event.route}`,
|
|
1167
|
-
durationMs: event.durationMs
|
|
1168
|
-
};
|
|
1169
|
-
case "middleware.complete":
|
|
1170
|
-
return {
|
|
1171
|
-
kind: "middleware",
|
|
1172
|
-
name: `farm.middleware ${event.name ?? event.route ?? "anonymous"}`,
|
|
1173
|
-
durationMs: event.durationMs
|
|
1174
|
-
};
|
|
1175
|
-
case "api.request.complete":
|
|
1176
|
-
case "api.error":
|
|
1177
|
-
return {
|
|
1178
|
-
kind: "api",
|
|
1179
|
-
name: `farm.api ${event.method} ${event.route}`,
|
|
1180
|
-
durationMs: event.durationMs
|
|
1181
|
-
};
|
|
1182
|
-
case "integration.api.call.complete":
|
|
1183
|
-
return {
|
|
1184
|
-
kind: "integration",
|
|
1185
|
-
name: `farm.integration ${event.integration}.${event.operation}`,
|
|
1186
|
-
durationMs: event.durationMs
|
|
1187
|
-
};
|
|
1188
|
-
case "storage.query.complete":
|
|
1189
|
-
return {
|
|
1190
|
-
kind: "storage",
|
|
1191
|
-
name: `farm.storage ${event.operation}`,
|
|
1192
|
-
durationMs: event.durationMs
|
|
1193
|
-
};
|
|
1194
|
-
case "ppr.refresh.complete":
|
|
1195
|
-
return {
|
|
1196
|
-
kind: "ppr",
|
|
1197
|
-
name: `farm.ppr.refresh ${event.route}`,
|
|
1198
|
-
durationMs: event.durationMs
|
|
1199
|
-
};
|
|
1200
|
-
case "build.complete":
|
|
1201
|
-
return {
|
|
1202
|
-
kind: "build",
|
|
1203
|
-
name: `farm.build${event.target ? ` ${event.target}` : ""}`,
|
|
1204
|
-
durationMs: event.durationMs
|
|
1205
|
-
};
|
|
1206
|
-
case "plugin.hook.complete":
|
|
1207
|
-
return {
|
|
1208
|
-
kind: "plugin",
|
|
1209
|
-
name: `farm.plugin ${event.plugin}.${event.hook}`,
|
|
1210
|
-
durationMs: event.durationMs
|
|
1211
|
-
};
|
|
1212
|
-
default:
|
|
1213
|
-
return void 0;
|
|
1214
|
-
}
|
|
1215
|
-
}
|
|
1216
|
-
__name(getCompletedSpanDescriptor, "getCompletedSpanDescriptor");
|
|
1217
|
-
function hashFarmCacheKey(value) {
|
|
1218
|
-
let hash = 2166136261;
|
|
1219
|
-
for (let index = 0; index < value.length; index++) {
|
|
1220
|
-
hash ^= value.charCodeAt(index);
|
|
1221
|
-
hash = Math.imul(hash, 16777619);
|
|
1222
|
-
}
|
|
1223
|
-
return (hash >>> 0).toString(16).padStart(8, "0");
|
|
1224
|
-
}
|
|
1225
|
-
__name(hashFarmCacheKey, "hashFarmCacheKey");
|
|
1226
|
-
function toEventAttributes(event) {
|
|
1227
|
-
const attributes = {};
|
|
1228
|
-
const redactKey = typeof event.type === "string" && event.type.startsWith("cache.");
|
|
1229
|
-
for (const [key, value] of Object.entries(event)) {
|
|
1230
|
-
if (key === "timestamp" || key === "level" || key === "error" || key === "traceId" || key === "spanId" || key === "traceSampled" || value === void 0) {
|
|
1231
|
-
continue;
|
|
1232
|
-
}
|
|
1233
|
-
if (redactKey && key === "key" && typeof value === "string") {
|
|
1234
|
-
attributes["farm.key_hash"] = hashFarmCacheKey(value);
|
|
1235
|
-
continue;
|
|
1236
|
-
}
|
|
1237
|
-
if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
|
|
1238
|
-
attributes[`farm.${key}`] = value;
|
|
1239
|
-
} else if (Array.isArray(value)) {
|
|
1240
|
-
if (value.every((entry) => typeof entry === "string")) {
|
|
1241
|
-
attributes[`farm.${key}`] = value;
|
|
1242
|
-
} else if (value.every((entry) => typeof entry === "number")) {
|
|
1243
|
-
attributes[`farm.${key}`] = value;
|
|
1244
|
-
} else if (value.every((entry) => typeof entry === "boolean")) {
|
|
1245
|
-
attributes[`farm.${key}`] = value;
|
|
1246
|
-
}
|
|
1247
|
-
}
|
|
1248
|
-
}
|
|
1249
|
-
return attributes;
|
|
1250
|
-
}
|
|
1251
|
-
__name(toEventAttributes, "toEventAttributes");
|
|
1252
|
-
function getEventError(event) {
|
|
1253
|
-
return "error" in event ? event.error : void 0;
|
|
1254
|
-
}
|
|
1255
|
-
__name(getEventError, "getEventError");
|
|
1256
|
-
function recordSpanException(span, error) {
|
|
1257
|
-
const normalized = error instanceof Error ? error : new Error(String(error));
|
|
1258
|
-
span.recordException(normalized);
|
|
1259
|
-
return normalized;
|
|
1260
|
-
}
|
|
1261
|
-
__name(recordSpanException, "recordSpanException");
|
|
1262
|
-
function recordSpanError(span, error) {
|
|
1263
|
-
const normalized = recordSpanException(span, error);
|
|
1264
|
-
span.setStatus({ code: import_api.SpanStatusCode.ERROR, message: normalized.message });
|
|
1265
|
-
}
|
|
1266
|
-
__name(recordSpanError, "recordSpanError");
|
|
1267
|
-
function setResponseStatus(span, status) {
|
|
1268
|
-
span.setAttribute("http.response.status_code", status);
|
|
1269
|
-
if (status >= 500) {
|
|
1270
|
-
span.setStatus({ code: import_api.SpanStatusCode.ERROR });
|
|
1271
|
-
}
|
|
1272
|
-
}
|
|
1273
|
-
__name(setResponseStatus, "setResponseStatus");
|
|
1274
|
-
function getSpanTraceContext(span) {
|
|
1275
|
-
if (!span) return void 0;
|
|
1276
|
-
const spanContext = span.spanContext();
|
|
1277
|
-
if (!(0, import_api.isSpanContextValid)(spanContext)) return void 0;
|
|
1278
|
-
return {
|
|
1279
|
-
traceId: spanContext.traceId,
|
|
1280
|
-
spanId: spanContext.spanId,
|
|
1281
|
-
traceSampled: (spanContext.traceFlags & 1) === 1
|
|
1282
|
-
};
|
|
1283
|
-
}
|
|
1284
|
-
__name(getSpanTraceContext, "getSpanTraceContext");
|
|
19
|
+
var _chunkY53ZSFN6cjs = require('./chunk-Y53ZSFN6.cjs');
|
|
1285
20
|
|
|
1286
|
-
// src/observability.ts
|
|
1287
|
-
var OBSERVABILITY_RUNTIME = /* @__PURE__ */ Symbol.for("@farm.js/core/observability-runtime/v1");
|
|
1288
|
-
var runtime2 = globalThis;
|
|
1289
|
-
var shared = runtime2[OBSERVABILITY_RUNTIME] ?? (runtime2[OBSERVABILITY_RUNTIME] = {
|
|
1290
|
-
handlers: /* @__PURE__ */ new Set(),
|
|
1291
|
-
unfilteredHandlers: /* @__PURE__ */ new Set(),
|
|
1292
|
-
config: { logs: false, handlers: [], tracing: normalizeFarmTracingConfig(false) }
|
|
1293
|
-
});
|
|
1294
|
-
var runtimeHandlers = shared.handlers;
|
|
1295
|
-
var unfilteredRuntimeHandlers = shared.unfilteredHandlers;
|
|
1296
|
-
var observabilityState = shared.config;
|
|
1297
|
-
function emitFarmEvent(input) {
|
|
1298
|
-
const event = {
|
|
1299
|
-
timestamp: Date.now(),
|
|
1300
|
-
level: inferFarmEventLevel(input.type),
|
|
1301
|
-
...input
|
|
1302
|
-
};
|
|
1303
|
-
const traceContext = recordFarmEventTrace(event);
|
|
1304
|
-
if (traceContext) {
|
|
1305
|
-
event.traceId = traceContext.traceId;
|
|
1306
|
-
event.spanId = traceContext.spanId;
|
|
1307
|
-
event.traceSampled = traceContext.traceSampled;
|
|
1308
|
-
}
|
|
1309
|
-
notifyFarmEventHandlers(event, unfilteredRuntimeHandlers);
|
|
1310
|
-
if (!shouldEmitFarmEvent(event)) return event;
|
|
1311
|
-
if (observabilityState.logs) {
|
|
1312
|
-
logFarmEvent(event);
|
|
1313
|
-
}
|
|
1314
|
-
notifyFarmEventHandlers(event, [...observabilityState.handlers, ...runtimeHandlers]);
|
|
1315
|
-
return event;
|
|
1316
|
-
}
|
|
1317
|
-
__name(emitFarmEvent, "emitFarmEvent");
|
|
1318
|
-
function notifyFarmEventHandlers(event, handlers) {
|
|
1319
|
-
for (const handler of handlers) {
|
|
1320
|
-
try {
|
|
1321
|
-
Promise.resolve(handler(event)).catch((error) => {
|
|
1322
|
-
console.warn(`[farm:observability] event handler failed: ${formatError(error)}`);
|
|
1323
|
-
});
|
|
1324
|
-
} catch (error) {
|
|
1325
|
-
console.warn(`[farm:observability] event handler failed: ${formatError(error)}`);
|
|
1326
|
-
}
|
|
1327
|
-
}
|
|
1328
|
-
}
|
|
1329
|
-
__name(notifyFarmEventHandlers, "notifyFarmEventHandlers");
|
|
1330
|
-
function shouldEmitFarmEvent(event) {
|
|
1331
|
-
if (!observabilityState.logs && observabilityState.handlers.length === 0 && runtimeHandlers.size === 0) {
|
|
1332
|
-
return false;
|
|
1333
|
-
}
|
|
1334
|
-
if (observabilityState.events && !observabilityState.events.has(event.type)) {
|
|
1335
|
-
return false;
|
|
1336
|
-
}
|
|
1337
|
-
return true;
|
|
1338
|
-
}
|
|
1339
|
-
__name(shouldEmitFarmEvent, "shouldEmitFarmEvent");
|
|
1340
|
-
function inferFarmEventLevel(type) {
|
|
1341
|
-
if (type === "error" || type.endsWith(".error") || type.endsWith(".failed")) {
|
|
1342
|
-
return "error";
|
|
1343
|
-
}
|
|
1344
|
-
if (type.endsWith(".bypass") || type.endsWith(".invalidated") || type.endsWith(".stale") || type.endsWith(".notFound")) {
|
|
1345
|
-
return "warn";
|
|
1346
|
-
}
|
|
1347
|
-
if (type.endsWith(".hit") || type.endsWith(".miss") || type.endsWith(".start") || type.endsWith(".shellReady")) {
|
|
1348
|
-
return "debug";
|
|
1349
|
-
}
|
|
1350
|
-
return "info";
|
|
1351
|
-
}
|
|
1352
|
-
__name(inferFarmEventLevel, "inferFarmEventLevel");
|
|
1353
|
-
function logFarmEvent(event) {
|
|
1354
|
-
const message = `[farm:${event.level}] ${event.type}${formatFarmEventDetails(event)}`;
|
|
1355
|
-
switch (event.level) {
|
|
1356
|
-
case "error":
|
|
1357
|
-
console.error(message);
|
|
1358
|
-
break;
|
|
1359
|
-
case "warn":
|
|
1360
|
-
console.warn(message);
|
|
1361
|
-
break;
|
|
1362
|
-
default:
|
|
1363
|
-
console.log(message);
|
|
1364
|
-
break;
|
|
1365
|
-
}
|
|
1366
|
-
}
|
|
1367
|
-
__name(logFarmEvent, "logFarmEvent");
|
|
1368
|
-
function formatFarmEventDetails(event) {
|
|
1369
|
-
const details = [];
|
|
1370
|
-
const record = event;
|
|
1371
|
-
for (const key of [
|
|
1372
|
-
"route",
|
|
1373
|
-
"pathname",
|
|
1374
|
-
"method",
|
|
1375
|
-
"status",
|
|
1376
|
-
"key",
|
|
1377
|
-
"tag",
|
|
1378
|
-
"path",
|
|
1379
|
-
"reason",
|
|
1380
|
-
"durationMs",
|
|
1381
|
-
"integration",
|
|
1382
|
-
"server",
|
|
1383
|
-
"tool",
|
|
1384
|
-
"outcome",
|
|
1385
|
-
"operation",
|
|
1386
|
-
"plugin",
|
|
1387
|
-
"hook",
|
|
1388
|
-
"target",
|
|
1389
|
-
"count"
|
|
1390
|
-
]) {
|
|
1391
|
-
const value = record[key];
|
|
1392
|
-
if (value !== void 0) {
|
|
1393
|
-
details.push(`${key}=${formatDetailValue(value)}`);
|
|
1394
|
-
}
|
|
1395
|
-
}
|
|
1396
|
-
return details.length > 0 ? ` ${details.join(" ")}` : "";
|
|
1397
|
-
}
|
|
1398
|
-
__name(formatFarmEventDetails, "formatFarmEventDetails");
|
|
1399
|
-
function formatDetailValue(value) {
|
|
1400
|
-
if (typeof value === "string") {
|
|
1401
|
-
return value;
|
|
1402
|
-
}
|
|
1403
|
-
if (typeof value === "number" || typeof value === "boolean") {
|
|
1404
|
-
return String(value);
|
|
1405
|
-
}
|
|
1406
|
-
try {
|
|
1407
|
-
return JSON.stringify(value);
|
|
1408
|
-
} catch {
|
|
1409
|
-
return String(value);
|
|
1410
|
-
}
|
|
1411
|
-
}
|
|
1412
|
-
__name(formatDetailValue, "formatDetailValue");
|
|
1413
|
-
function formatError(error) {
|
|
1414
|
-
return error instanceof Error ? error.message : String(error);
|
|
1415
|
-
}
|
|
1416
|
-
__name(formatError, "formatError");
|
|
1417
21
|
|
|
1418
|
-
// src/navigation-errors.ts
|
|
1419
|
-
var REDIRECT_ERROR_CODE = "FARM_REDIRECT";
|
|
1420
|
-
var NOT_FOUND_ERROR_CODE = "FARM_NOT_FOUND";
|
|
1421
|
-
var REDIRECT_ERROR_SYMBOL = /* @__PURE__ */ Symbol.for("farm.navigation.redirect");
|
|
1422
|
-
var NOT_FOUND_ERROR_SYMBOL = /* @__PURE__ */ Symbol.for("farm.navigation.notFound");
|
|
1423
|
-
function isFarmRedirectError(error) {
|
|
1424
|
-
return Boolean(getFarmRedirectError(error));
|
|
1425
|
-
}
|
|
1426
|
-
__name(isFarmRedirectError, "isFarmRedirectError");
|
|
1427
|
-
function getFarmRedirectError(error) {
|
|
1428
|
-
if (!error || typeof error !== "object") return null;
|
|
1429
|
-
const candidate = error;
|
|
1430
|
-
if (candidate[REDIRECT_ERROR_SYMBOL]) {
|
|
1431
|
-
return candidate[REDIRECT_ERROR_SYMBOL];
|
|
1432
|
-
}
|
|
1433
|
-
if (typeof candidate.digest === "string" && candidate.digest.startsWith(`${REDIRECT_ERROR_CODE};`)) {
|
|
1434
|
-
const [, status, ...urlParts] = candidate.digest.split(";");
|
|
1435
|
-
const parsedStatus = Number(status);
|
|
1436
|
-
if (isFarmRedirectStatus(parsedStatus)) {
|
|
1437
|
-
return {
|
|
1438
|
-
status: parsedStatus,
|
|
1439
|
-
url: urlParts.join(";")
|
|
1440
|
-
};
|
|
1441
|
-
}
|
|
1442
|
-
}
|
|
1443
|
-
return null;
|
|
1444
|
-
}
|
|
1445
|
-
__name(getFarmRedirectError, "getFarmRedirectError");
|
|
1446
|
-
function isFarmNotFoundError(error) {
|
|
1447
|
-
if (!error || typeof error !== "object") return false;
|
|
1448
|
-
const candidate = error;
|
|
1449
|
-
return Boolean(candidate[NOT_FOUND_ERROR_SYMBOL] || candidate.digest === NOT_FOUND_ERROR_CODE);
|
|
1450
|
-
}
|
|
1451
|
-
__name(isFarmNotFoundError, "isFarmNotFoundError");
|
|
1452
|
-
function isFarmRedirectStatus(status) {
|
|
1453
|
-
return status === 301 || status === 302 || status === 303 || status === 307 || status === 308;
|
|
1454
|
-
}
|
|
1455
|
-
__name(isFarmRedirectStatus, "isFarmRedirectStatus");
|
|
1456
22
|
|
|
1457
|
-
// src/base-path.ts
|
|
1458
|
-
var FARM_BASE_PATH = /* @__PURE__ */ Symbol.for("farm.basePath");
|
|
1459
|
-
function getFarmGlobalState() {
|
|
1460
|
-
return globalThis;
|
|
1461
|
-
}
|
|
1462
|
-
__name(getFarmGlobalState, "getFarmGlobalState");
|
|
1463
|
-
function getFarmBasePath() {
|
|
1464
|
-
return getFarmGlobalState()[FARM_BASE_PATH] ?? "";
|
|
1465
|
-
}
|
|
1466
|
-
__name(getFarmBasePath, "getFarmBasePath");
|
|
1467
|
-
function stripFarmBasePath(pathname, basePath = getFarmBasePath()) {
|
|
1468
|
-
const normalizedBasePath = normalizeFarmBasePath(basePath);
|
|
1469
|
-
if (!normalizedBasePath) return pathname || "/";
|
|
1470
|
-
if (pathname === normalizedBasePath) return "/";
|
|
1471
|
-
if (!pathname.startsWith(`${normalizedBasePath}/`)) return pathname || "/";
|
|
1472
|
-
return pathname.slice(normalizedBasePath.length) || "/";
|
|
1473
|
-
}
|
|
1474
|
-
__name(stripFarmBasePath, "stripFarmBasePath");
|
|
1475
|
-
function normalizeFarmBasePath(basePath) {
|
|
1476
|
-
if (!basePath || basePath === "/") return "";
|
|
1477
|
-
const hasUnstableCharacters = /* @__PURE__ */ __name((candidate) => candidate.includes("\\") || Array.from(candidate).some((character) => {
|
|
1478
|
-
const code = character.charCodeAt(0);
|
|
1479
|
-
return code <= 31 || code >= 127 && code <= 159;
|
|
1480
|
-
}), "hasUnstableCharacters");
|
|
1481
|
-
if (hasUnstableCharacters(basePath)) {
|
|
1482
|
-
throw new Error("Farm basePath cannot contain backslashes or control characters.");
|
|
1483
|
-
}
|
|
1484
|
-
const pathname = basePath.trim();
|
|
1485
|
-
if (!pathname || pathname === "/") return "";
|
|
1486
|
-
if (pathname.includes("?") || pathname.includes("#")) {
|
|
1487
|
-
throw new Error("Farm basePath cannot contain a query string or hash.");
|
|
1488
|
-
}
|
|
1489
|
-
if (pathname.startsWith("//") || /^[a-z][a-z\d+.-]*:\/\//i.test(pathname)) {
|
|
1490
|
-
throw new Error('Farm basePath must be a pathname such as "/docs", not a URL.');
|
|
1491
|
-
}
|
|
1492
|
-
for (const segment of pathname.split("/")) {
|
|
1493
|
-
let decoded = segment;
|
|
1494
|
-
try {
|
|
1495
|
-
decoded = decodeURIComponent(segment);
|
|
1496
|
-
} catch {
|
|
1497
|
-
}
|
|
1498
|
-
if (hasUnstableCharacters(decoded)) {
|
|
1499
|
-
throw new Error("Farm basePath cannot contain backslashes or control characters.");
|
|
1500
|
-
}
|
|
1501
|
-
if (decoded.includes("/")) {
|
|
1502
|
-
throw new Error("Farm basePath cannot contain percent-encoded path separators.");
|
|
1503
|
-
}
|
|
1504
|
-
if (decoded === "." || decoded === "..") {
|
|
1505
|
-
throw new Error('Farm basePath cannot contain "." or ".." path segments.');
|
|
1506
|
-
}
|
|
1507
|
-
}
|
|
1508
|
-
return `/${pathname}`.replace(/\/{2,}/g, "/").replace(/\/+$/, "");
|
|
1509
|
-
}
|
|
1510
|
-
__name(normalizeFarmBasePath, "normalizeFarmBasePath");
|
|
1511
23
|
|
|
1512
|
-
// src/i18n/routing.ts
|
|
1513
|
-
function resolveFarmLocalePath(pathname, config) {
|
|
1514
|
-
const normalized = normalizePathname(stripFarmBasePath(pathname, config.basePath));
|
|
1515
|
-
if (config.routing === "none") {
|
|
1516
|
-
return { pathname: normalized, explicit: false };
|
|
1517
|
-
}
|
|
1518
|
-
const segments = normalized.split("/").filter(Boolean);
|
|
1519
|
-
const firstSegment = segments[0];
|
|
1520
|
-
const locale = config.locales.find(
|
|
1521
|
-
(candidate) => candidate.toLowerCase() === firstSegment?.toLowerCase()
|
|
1522
|
-
);
|
|
1523
|
-
if (!locale) {
|
|
1524
|
-
return { pathname: normalized, explicit: false };
|
|
1525
|
-
}
|
|
1526
|
-
const remaining = segments.slice(1);
|
|
1527
|
-
return {
|
|
1528
|
-
locale,
|
|
1529
|
-
pathname: remaining.length > 0 ? `/${remaining.join("/")}` : "/",
|
|
1530
|
-
explicit: true
|
|
1531
|
-
};
|
|
1532
|
-
}
|
|
1533
|
-
__name(resolveFarmLocalePath, "resolveFarmLocalePath");
|
|
1534
|
-
function stripFarmLocaleFromPathname(pathname, config) {
|
|
1535
|
-
return resolveFarmLocalePath(pathname, config).pathname;
|
|
1536
|
-
}
|
|
1537
|
-
__name(stripFarmLocaleFromPathname, "stripFarmLocaleFromPathname");
|
|
1538
|
-
function normalizePathname(pathname) {
|
|
1539
|
-
const withLeadingSlash = pathname.startsWith("/") ? pathname : `/${pathname}`;
|
|
1540
|
-
if (withLeadingSlash === "/") return "/";
|
|
1541
|
-
return withLeadingSlash.replace(/\/{2,}/g, "/").replace(/\/$/, "") || "/";
|
|
1542
|
-
}
|
|
1543
|
-
__name(normalizePathname, "normalizePathname");
|
|
1544
24
|
|
|
1545
|
-
// src/cli-colors.ts
|
|
1546
|
-
var import_picocolors = __toESM(require("picocolors"));
|
|
1547
|
-
function createCliColors(interactive = process.stdout.isTTY === true) {
|
|
1548
|
-
return import_picocolors.default.createColors(interactive);
|
|
1549
|
-
}
|
|
1550
|
-
__name(createCliColors, "createCliColors");
|
|
1551
25
|
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
|
|
26
|
+
var _chunkXGC343EYcjs = require('./chunk-XGC343EY.cjs');
|
|
27
|
+
require('./chunk-RG3ZOTG5.cjs');
|
|
28
|
+
require('./chunk-M2L4MYDQ.cjs');
|
|
29
|
+
require('./chunk-VQDBNVOO.cjs');
|
|
30
|
+
require('./chunk-HBTUGXHS.cjs');
|
|
31
|
+
require('./chunk-5QIKR7RR.cjs');
|
|
32
|
+
require('./chunk-QNPEUFHQ.cjs');
|
|
33
|
+
require('./chunk-UQZ4AYTH.cjs');
|
|
34
|
+
require('./chunk-E3UK5IIB.cjs');
|
|
35
|
+
require('./chunk-OX6O6WVA.cjs');
|
|
36
|
+
require('./chunk-5ZSWG4UX.cjs');
|
|
1560
37
|
|
|
1561
|
-
// src/middleware/matcher.ts
|
|
1562
|
-
var compiledConfigCache = /* @__PURE__ */ new WeakMap();
|
|
1563
|
-
var compiledRouteCache = /* @__PURE__ */ new WeakMap();
|
|
1564
|
-
function compileMiddlewareRoute(entry) {
|
|
1565
|
-
const path4 = entry.path;
|
|
1566
|
-
if (path4 === "/") return void 0;
|
|
1567
|
-
const cached = compiledRouteCache.get(entry);
|
|
1568
|
-
if (cached?.path === path4) return cached;
|
|
1569
|
-
const compiled = {
|
|
1570
|
-
path: path4,
|
|
1571
|
-
exact: compilePattern(path4),
|
|
1572
|
-
nested: compilePattern(`${path4}/:__farmRest*`)
|
|
1573
|
-
};
|
|
1574
|
-
compiledRouteCache.set(entry, compiled);
|
|
1575
|
-
return compiled;
|
|
1576
|
-
}
|
|
1577
|
-
__name(compileMiddlewareRoute, "compileMiddlewareRoute");
|
|
1578
|
-
function matchesMiddlewareRoute(pathname, entry) {
|
|
1579
|
-
const compiled = compileMiddlewareRoute(entry);
|
|
1580
|
-
if (!compiled) return { matched: true };
|
|
1581
|
-
const canonicalPathname = canonicalizeRequestPathname(pathname);
|
|
1582
|
-
const exactMatch = matchPattern2(compiled.exact, canonicalPathname);
|
|
1583
|
-
if (exactMatch.matched) return exactMatch;
|
|
1584
|
-
const nestedMatch = matchPattern2(compiled.nested, canonicalPathname);
|
|
1585
|
-
if (!nestedMatch.matched) return { matched: false };
|
|
1586
|
-
const params = { ...nestedMatch.params };
|
|
1587
|
-
delete params.__farmRest;
|
|
1588
|
-
return { matched: true, params: Object.keys(params).length > 0 ? params : void 0 };
|
|
1589
|
-
}
|
|
1590
|
-
__name(matchesMiddlewareRoute, "matchesMiddlewareRoute");
|
|
1591
|
-
function sameEntries(current, previous) {
|
|
1592
|
-
return current === void 0 || previous === void 0 ? current === previous : current.length === previous.length && current.every((entry, index) => entry === previous[index]);
|
|
1593
|
-
}
|
|
1594
|
-
__name(sameEntries, "sameEntries");
|
|
1595
|
-
function toMatcherList(matcher) {
|
|
1596
|
-
if (!matcher) return [];
|
|
1597
|
-
return Array.isArray(matcher) ? matcher : [matcher];
|
|
1598
|
-
}
|
|
1599
|
-
__name(toMatcherList, "toMatcherList");
|
|
1600
|
-
function escapeRegex(value) {
|
|
1601
|
-
return value.replace(/[|\\{}()[\]^$+?.]/g, "\\$&");
|
|
1602
|
-
}
|
|
1603
|
-
__name(escapeRegex, "escapeRegex");
|
|
1604
|
-
function parseColonParam(segment) {
|
|
1605
|
-
const raw = segment.slice(1);
|
|
1606
|
-
const last = raw[raw.length - 1];
|
|
1607
|
-
const modifier = last === "*" || last === "+" || last === "?" ? last : void 0;
|
|
1608
|
-
return {
|
|
1609
|
-
name: modifier ? raw.slice(0, -1) : raw,
|
|
1610
|
-
modifier
|
|
1611
|
-
};
|
|
1612
|
-
}
|
|
1613
|
-
__name(parseColonParam, "parseColonParam");
|
|
1614
|
-
function compilePathPattern(pattern) {
|
|
1615
|
-
const params = [];
|
|
1616
|
-
const segments = pattern.split("/").filter(Boolean);
|
|
1617
|
-
if (segments.length === 0) {
|
|
1618
|
-
return { kind: "path", regex: /^\/$/, params };
|
|
1619
|
-
}
|
|
1620
|
-
const parts = segments.map((segment) => {
|
|
1621
|
-
if (segment === "**") {
|
|
1622
|
-
return "(?:/.*)?";
|
|
1623
|
-
}
|
|
1624
|
-
if (segment === "*") {
|
|
1625
|
-
return "/[^/]+";
|
|
1626
|
-
}
|
|
1627
|
-
if (segment.startsWith(":")) {
|
|
1628
|
-
const { name, modifier } = parseColonParam(segment);
|
|
1629
|
-
params.push(name);
|
|
1630
|
-
if (modifier === "*") {
|
|
1631
|
-
return "(?:/(.*))?";
|
|
1632
|
-
}
|
|
1633
|
-
if (modifier === "+") {
|
|
1634
|
-
return "/(.+)";
|
|
1635
|
-
}
|
|
1636
|
-
return "/([^/]+)";
|
|
1637
|
-
}
|
|
1638
|
-
if (segment.startsWith("[...") && segment.endsWith("]")) {
|
|
1639
|
-
params.push(segment.slice(4, -1));
|
|
1640
|
-
return "(?:/(.*))?";
|
|
1641
|
-
}
|
|
1642
|
-
if (segment.startsWith("[") && segment.endsWith("]")) {
|
|
1643
|
-
params.push(segment.slice(1, -1));
|
|
1644
|
-
return "/([^/]+)";
|
|
1645
|
-
}
|
|
1646
|
-
return `/${escapeRegex(segment).replace(/\\\*/g, "[^/]*")}`;
|
|
1647
|
-
});
|
|
1648
|
-
return {
|
|
1649
|
-
kind: "path",
|
|
1650
|
-
regex: new RegExp(`^${parts.join("")}$`),
|
|
1651
|
-
params
|
|
1652
|
-
};
|
|
1653
|
-
}
|
|
1654
|
-
__name(compilePathPattern, "compilePathPattern");
|
|
1655
|
-
function compilePattern(pattern) {
|
|
1656
|
-
if (pattern instanceof RegExp) {
|
|
1657
|
-
return { kind: "regexp", regex: pattern };
|
|
1658
|
-
}
|
|
1659
|
-
if (pattern === "*" || pattern === "/(.*)") {
|
|
1660
|
-
return { kind: "all" };
|
|
1661
|
-
}
|
|
1662
|
-
if (pattern.endsWith("(.*)")) {
|
|
1663
|
-
return {
|
|
1664
|
-
kind: "subtree",
|
|
1665
|
-
prefix: pattern.slice(0, -4).replace(/\/$/, "")
|
|
1666
|
-
};
|
|
1667
|
-
}
|
|
1668
|
-
return compilePathPattern(pattern);
|
|
1669
|
-
}
|
|
1670
|
-
__name(compilePattern, "compilePattern");
|
|
1671
|
-
function compileMiddlewareConfig(config) {
|
|
1672
|
-
const compiled = {
|
|
1673
|
-
matcher: config.matcher ? toMatcherList(config.matcher).map(
|
|
1674
|
-
(matcher) => typeof matcher === "function" ? matcher : compilePattern(matcher)
|
|
1675
|
-
) : void 0,
|
|
1676
|
-
exclude: config.exclude?.map(compilePattern)
|
|
1677
|
-
};
|
|
1678
|
-
compiledConfigCache.set(config, {
|
|
1679
|
-
compiled,
|
|
1680
|
-
matcher: config.matcher,
|
|
1681
|
-
matcherEntries: Array.isArray(config.matcher) ? config.matcher.slice() : void 0,
|
|
1682
|
-
excludeEntries: config.exclude?.slice()
|
|
1683
|
-
});
|
|
1684
|
-
return compiled;
|
|
1685
|
-
}
|
|
1686
|
-
__name(compileMiddlewareConfig, "compileMiddlewareConfig");
|
|
1687
|
-
function matchPattern2(pattern, pathname) {
|
|
1688
|
-
if (pattern.kind === "all") {
|
|
1689
|
-
return { matched: true };
|
|
1690
|
-
}
|
|
1691
|
-
if (pattern.kind === "subtree") {
|
|
1692
|
-
return {
|
|
1693
|
-
matched: pathname === pattern.prefix || pathname.startsWith(`${pattern.prefix}/`)
|
|
1694
|
-
};
|
|
1695
|
-
}
|
|
1696
|
-
pattern.regex.lastIndex = 0;
|
|
1697
|
-
const match = pattern.regex.exec(pathname);
|
|
1698
|
-
if (!match) {
|
|
1699
|
-
return { matched: false };
|
|
1700
|
-
}
|
|
1701
|
-
if (pattern.kind === "regexp") {
|
|
1702
|
-
return {
|
|
1703
|
-
matched: true,
|
|
1704
|
-
params: match.groups ? { ...match.groups } : void 0
|
|
1705
|
-
};
|
|
1706
|
-
}
|
|
1707
|
-
const values = {};
|
|
1708
|
-
pattern.params.forEach((param, index) => {
|
|
1709
|
-
values[param] = match[index + 1] || "";
|
|
1710
|
-
});
|
|
1711
|
-
return {
|
|
1712
|
-
matched: true,
|
|
1713
|
-
params: Object.keys(values).length > 0 ? values : void 0
|
|
1714
|
-
};
|
|
1715
|
-
}
|
|
1716
|
-
__name(matchPattern2, "matchPattern");
|
|
1717
|
-
function matchesCompiledMiddlewareConfig(pathname, config, ctx) {
|
|
1718
|
-
let canonicalPathname;
|
|
1719
|
-
const getCanonicalPathname = /* @__PURE__ */ __name(() => canonicalPathname ?? (canonicalPathname = canonicalizeRequestPathname(pathname)), "getCanonicalPathname");
|
|
1720
|
-
if (config.exclude) {
|
|
1721
|
-
for (const pattern of config.exclude) {
|
|
1722
|
-
if (matchPattern2(pattern, getCanonicalPathname()).matched) {
|
|
1723
|
-
return { matched: false };
|
|
1724
|
-
}
|
|
1725
|
-
}
|
|
1726
|
-
}
|
|
1727
|
-
if (config.matcher) {
|
|
1728
|
-
for (const matcher of config.matcher) {
|
|
1729
|
-
if (typeof matcher === "function") {
|
|
1730
|
-
if (matcher(ctx)) {
|
|
1731
|
-
return { matched: true };
|
|
1732
|
-
}
|
|
1733
|
-
continue;
|
|
1734
|
-
}
|
|
1735
|
-
const result = matchPattern2(matcher, getCanonicalPathname());
|
|
1736
|
-
if (result.matched) {
|
|
1737
|
-
return result;
|
|
1738
|
-
}
|
|
1739
|
-
}
|
|
1740
|
-
return { matched: false };
|
|
1741
|
-
}
|
|
1742
|
-
return { matched: true };
|
|
1743
|
-
}
|
|
1744
|
-
__name(matchesCompiledMiddlewareConfig, "matchesCompiledMiddlewareConfig");
|
|
1745
|
-
function matchesMiddlewareConfig(pathname, config, ctx) {
|
|
1746
|
-
const cached = compiledConfigCache.get(config);
|
|
1747
|
-
const unchanged = cached && (Array.isArray(config.matcher) ? sameEntries(config.matcher, cached.matcherEntries) : config.matcher === cached.matcher) && sameEntries(config.exclude, cached.excludeEntries);
|
|
1748
|
-
const compiled = unchanged ? cached.compiled : compileMiddlewareConfig(config);
|
|
1749
|
-
return matchesCompiledMiddlewareConfig(pathname, compiled, ctx);
|
|
1750
|
-
}
|
|
1751
|
-
__name(matchesMiddlewareConfig, "matchesMiddlewareConfig");
|
|
1752
38
|
|
|
1753
|
-
|
|
1754
|
-
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
|
|
1759
|
-
constructor(appDir, viteServer, config, i18n, server) {
|
|
1760
|
-
this.middleware = [];
|
|
1761
|
-
this.configMiddleware = [];
|
|
1762
|
-
this.appDirs = Array.isArray(appDir) ? [...appDir] : [appDir];
|
|
1763
|
-
this.viteServer = viteServer;
|
|
1764
|
-
this.i18n = i18n;
|
|
1765
|
-
this.server = server;
|
|
1766
|
-
this.configure(config);
|
|
1767
|
-
}
|
|
1768
|
-
configure(config) {
|
|
1769
|
-
this.configMiddleware = [];
|
|
1770
|
-
this.globalConfig = void 0;
|
|
1771
|
-
if (!config) return;
|
|
1772
|
-
const entries = Array.isArray(config) ? config : [config];
|
|
1773
|
-
for (const [index, entry] of entries.entries()) {
|
|
1774
|
-
const handlers = this.getConfigHandlers(entry);
|
|
1775
|
-
const middlewareConfig = this.toMiddlewareConfig(entry);
|
|
1776
|
-
compileMiddlewareConfig(middlewareConfig);
|
|
1777
|
-
if (handlers.length === 0) {
|
|
1778
|
-
this.globalConfig = middlewareConfig;
|
|
1779
|
-
continue;
|
|
1780
|
-
}
|
|
1781
|
-
this.configMiddleware.push({
|
|
1782
|
-
path: "/",
|
|
1783
|
-
filePath: `farm.config.ts#middleware-${index}`,
|
|
1784
|
-
handlers,
|
|
1785
|
-
config: middlewareConfig,
|
|
1786
|
-
source: "config"
|
|
1787
|
-
});
|
|
1788
|
-
}
|
|
1789
|
-
}
|
|
1790
|
-
/**
|
|
1791
|
-
* Discover all middleware.ts files
|
|
1792
|
-
*/
|
|
1793
|
-
async discover() {
|
|
1794
|
-
const middlewareByPath = /* @__PURE__ */ new Map();
|
|
1795
|
-
for (const appDir of this.appDirs) {
|
|
1796
|
-
const discovered = [];
|
|
1797
|
-
await this.discoverInDirectory(appDir, "/", discovered);
|
|
1798
|
-
for (const middleware2 of discovered) {
|
|
1799
|
-
compileMiddlewareRoute(middleware2);
|
|
1800
|
-
middlewareByPath.set(middleware2.path, middleware2);
|
|
1801
|
-
}
|
|
1802
|
-
}
|
|
1803
|
-
this.middleware = Array.from(middlewareByPath.values());
|
|
1804
|
-
this.middleware.sort((a, b) => {
|
|
1805
|
-
const depthA = a.path.split("/").filter(Boolean).length;
|
|
1806
|
-
const depthB = b.path.split("/").filter(Boolean).length;
|
|
1807
|
-
return depthA - depthB;
|
|
1808
|
-
});
|
|
1809
|
-
if (process.env.FARM_VERBOSE && this.middleware.length > 0) {
|
|
1810
|
-
logger.success(`Discovered ${this.middleware.length} middleware files`);
|
|
1811
|
-
for (const mw of this.middleware) {
|
|
1812
|
-
logger.info(` ${mw.path} (${mw.handlers.length} handlers)`);
|
|
1813
|
-
}
|
|
1814
|
-
}
|
|
1815
|
-
}
|
|
1816
|
-
/**
|
|
1817
|
-
* Recursively discover middleware files
|
|
1818
|
-
*/
|
|
1819
|
-
async discoverInDirectory(dir, routePath, discovered) {
|
|
1820
|
-
if (!fs.existsSync(dir)) {
|
|
1821
|
-
return;
|
|
1822
|
-
}
|
|
1823
|
-
const middlewareFile = this.findMiddlewareFile(dir);
|
|
1824
|
-
if (middlewareFile) {
|
|
1825
|
-
const middleware2 = await this.loadMiddleware(middlewareFile, routePath);
|
|
1826
|
-
if (middleware2) {
|
|
1827
|
-
discovered.push(middleware2);
|
|
1828
|
-
}
|
|
1829
|
-
}
|
|
1830
|
-
const entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
1831
|
-
for (const entry of entries) {
|
|
1832
|
-
if (entry.isDirectory() && !entry.name.startsWith(".") && !entry.name.startsWith("_")) {
|
|
1833
|
-
const subPath = path.join(dir, entry.name);
|
|
1834
|
-
const subRoutePath = appendMiddlewareRoutePath(routePath, entry.name);
|
|
1835
|
-
await this.discoverInDirectory(subPath, subRoutePath, discovered);
|
|
1836
|
-
}
|
|
1837
|
-
}
|
|
1838
|
-
}
|
|
1839
|
-
/**
|
|
1840
|
-
* Find middleware file in directory
|
|
1841
|
-
*/
|
|
1842
|
-
findMiddlewareFile(dir) {
|
|
1843
|
-
const extensions = [".ts", ".tsx", ".js", ".jsx"];
|
|
1844
|
-
for (const ext of extensions) {
|
|
1845
|
-
const filePath = path.join(dir, `middleware${ext}`);
|
|
1846
|
-
if (fs.existsSync(filePath)) {
|
|
1847
|
-
return filePath;
|
|
1848
|
-
}
|
|
1849
|
-
}
|
|
1850
|
-
return null;
|
|
1851
|
-
}
|
|
1852
|
-
/**
|
|
1853
|
-
* Load a middleware file
|
|
1854
|
-
*/
|
|
1855
|
-
async loadMiddleware(filePath, routePath) {
|
|
1856
|
-
try {
|
|
1857
|
-
let module2;
|
|
1858
|
-
if (this.viteServer) {
|
|
1859
|
-
module2 = await this.viteServer.ssrLoadModule(filePath);
|
|
1860
|
-
} else {
|
|
1861
|
-
module2 = await import(
|
|
1862
|
-
/* @vite-ignore */
|
|
1863
|
-
toFileModuleUrl(filePath)
|
|
1864
|
-
);
|
|
1865
|
-
}
|
|
1866
|
-
const normalized = normalizeMiddlewareModule(module2, routePath);
|
|
1867
|
-
if (!normalized) {
|
|
1868
|
-
throw new Error("must export a default handler or a named middleware handler");
|
|
1869
|
-
}
|
|
1870
|
-
if (normalized.config) {
|
|
1871
|
-
compileMiddlewareConfig(normalized.config);
|
|
1872
|
-
}
|
|
1873
|
-
return {
|
|
1874
|
-
path: routePath,
|
|
1875
|
-
filePath,
|
|
1876
|
-
handlers: normalized.handlers,
|
|
1877
|
-
config: normalized.config,
|
|
1878
|
-
source: "file"
|
|
1879
|
-
};
|
|
1880
|
-
} catch (error) {
|
|
1881
|
-
throw new Error(`Failed to load middleware ${filePath}: ${error}`);
|
|
1882
|
-
}
|
|
1883
|
-
}
|
|
1884
|
-
/**
|
|
1885
|
-
* Execute middleware for a request
|
|
1886
|
-
*/
|
|
1887
|
-
async execute(req, res) {
|
|
1888
|
-
const url = resolveFarmRequestURL(req, { trustProxy: this.server?.trustProxy });
|
|
1889
|
-
const pathname = url.pathname;
|
|
1890
|
-
const routePathname = this.i18n?.enabled ? stripFarmLocaleFromPathname(pathname, this.i18n) : stripFarmBasePath(pathname, this.i18n?.basePath ?? "/");
|
|
1891
|
-
const method = req.method || "GET";
|
|
1892
|
-
const startTime = Date.now();
|
|
1893
|
-
let parentData;
|
|
1894
|
-
let ctx = createContext(req, res, this.viteServer, void 0, this.server);
|
|
1895
|
-
if (this.globalConfig) {
|
|
1896
|
-
const globalMatch = this.matchesConfig(routePathname, this.globalConfig, ctx);
|
|
1897
|
-
if (!globalMatch.matched) {
|
|
1898
|
-
return false;
|
|
1899
|
-
}
|
|
1900
|
-
if (globalMatch.params) {
|
|
1901
|
-
ctx.params = { ...ctx.params, ...globalMatch.params };
|
|
1902
|
-
}
|
|
1903
|
-
}
|
|
1904
|
-
const applicable = [
|
|
1905
|
-
...this.configMiddleware.map((mw) => ({ mw, routeMatch: { matched: true } })),
|
|
1906
|
-
...this.middleware.map((mw) => ({ mw, routeMatch: matchesMiddlewareRoute(routePathname, mw) })).filter((entry) => entry.routeMatch.matched)
|
|
1907
|
-
];
|
|
1908
|
-
if (applicable.length === 0) {
|
|
1909
|
-
return false;
|
|
1910
|
-
}
|
|
1911
|
-
const pc = createCliColors();
|
|
1912
|
-
const log = [
|
|
1913
|
-
pc.dim("[") + pc.bold(pc.blue("FARM")) + pc.dim("]"),
|
|
1914
|
-
pc.dim("[") + pc.bold(pc.magenta("MIDDLEWARE")) + pc.dim("]"),
|
|
1915
|
-
pc.dim("[") + pc.bold(pc.white(method.padEnd(3))) + pc.dim("]"),
|
|
1916
|
-
pc.gray("Executing middleware: "),
|
|
1917
|
-
pc.gray(pathname),
|
|
1918
|
-
pc.dim(` (${(Date.now() - startTime).toFixed(2)}ms)`)
|
|
1919
|
-
].join(" ");
|
|
1920
|
-
console.log(log);
|
|
1921
|
-
for (const entry of applicable) {
|
|
1922
|
-
const { mw, routeMatch } = entry;
|
|
1923
|
-
const configMatch = mw.config ? this.matchesConfig(routePathname, mw.config, ctx) : { matched: true };
|
|
1924
|
-
if (!configMatch.matched) {
|
|
1925
|
-
continue;
|
|
1926
|
-
}
|
|
1927
|
-
if (routeMatch.params) {
|
|
1928
|
-
ctx.params = { ...ctx.params, ...routeMatch.params };
|
|
1929
|
-
}
|
|
1930
|
-
if (configMatch.params) {
|
|
1931
|
-
ctx.params = { ...ctx.params, ...configMatch.params };
|
|
1932
|
-
}
|
|
1933
|
-
if (parentData) {
|
|
1934
|
-
ctx = createContext(req, res, this.viteServer, parentData, this.server);
|
|
1935
|
-
if (routeMatch.params) {
|
|
1936
|
-
ctx.params = { ...ctx.params, ...routeMatch.params };
|
|
1937
|
-
}
|
|
1938
|
-
if (configMatch.params) {
|
|
1939
|
-
ctx.params = { ...ctx.params, ...configMatch.params };
|
|
1940
|
-
}
|
|
1941
|
-
}
|
|
1942
|
-
const middlewareStartTime = Date.now();
|
|
1943
|
-
const middlewareEvent = {
|
|
1944
|
-
route: mw.path,
|
|
1945
|
-
pathname,
|
|
1946
|
-
name: mw.filePath
|
|
1947
|
-
};
|
|
1948
|
-
emitFarmEvent({ type: "middleware.start", ...middlewareEvent });
|
|
1949
|
-
try {
|
|
1950
|
-
let handlerIndex = 0;
|
|
1951
|
-
let returnedResponse;
|
|
1952
|
-
const executeNext = /* @__PURE__ */ __name(async () => {
|
|
1953
|
-
if (handlerIndex < mw.handlers.length) {
|
|
1954
|
-
const handler = mw.handlers[handlerIndex++];
|
|
1955
|
-
const result2 = await handler(ctx, executeNext);
|
|
1956
|
-
if (isMiddlewareResponse(result2)) {
|
|
1957
|
-
returnedResponse = result2;
|
|
1958
|
-
return result2;
|
|
1959
|
-
}
|
|
1960
|
-
}
|
|
1961
|
-
return returnedResponse;
|
|
1962
|
-
}, "executeNext");
|
|
1963
|
-
const result = await executeNext();
|
|
1964
|
-
const response = isMiddlewareResponse(result) ? result : returnedResponse;
|
|
1965
|
-
if (response) {
|
|
1966
|
-
if (!res.headersSent && !res.writableEnded) {
|
|
1967
|
-
for (const [key, value] of ctx.headers) {
|
|
1968
|
-
try {
|
|
1969
|
-
res.setHeader(key, value);
|
|
1970
|
-
} catch (error) {
|
|
1971
|
-
}
|
|
1972
|
-
}
|
|
1973
|
-
}
|
|
1974
|
-
emitFarmEvent({
|
|
1975
|
-
type: "middleware.shortCircuit",
|
|
1976
|
-
...middlewareEvent,
|
|
1977
|
-
status: response.status
|
|
1978
|
-
});
|
|
1979
|
-
await sendWebResponse(res, response);
|
|
1980
|
-
return true;
|
|
1981
|
-
}
|
|
1982
|
-
if (ctx._handled || res.headersSent || res.writableEnded) {
|
|
1983
|
-
emitFarmEvent({
|
|
1984
|
-
type: "middleware.shortCircuit",
|
|
1985
|
-
...middlewareEvent,
|
|
1986
|
-
status: res.statusCode
|
|
1987
|
-
});
|
|
1988
|
-
return true;
|
|
1989
|
-
}
|
|
1990
|
-
emitFarmEvent({
|
|
1991
|
-
type: "middleware.complete",
|
|
1992
|
-
...middlewareEvent,
|
|
1993
|
-
durationMs: Date.now() - middlewareStartTime
|
|
1994
|
-
});
|
|
1995
|
-
} catch (error) {
|
|
1996
|
-
emitFarmEvent(
|
|
1997
|
-
isFarmRedirectError(error) || isFarmNotFoundError(error) ? {
|
|
1998
|
-
type: "middleware.shortCircuit",
|
|
1999
|
-
...middlewareEvent,
|
|
2000
|
-
status: isFarmRedirectError(error) ? getFarmRedirectError(error).status : 404
|
|
2001
|
-
} : { type: "middleware.error", ...middlewareEvent, error }
|
|
2002
|
-
);
|
|
2003
|
-
throw error;
|
|
2004
|
-
}
|
|
2005
|
-
parentData = {
|
|
2006
|
-
data: new Map(ctx.data),
|
|
2007
|
-
locals: new Map(ctx.locals),
|
|
2008
|
-
headers: Object.fromEntries(ctx.headers)
|
|
2009
|
-
};
|
|
2010
|
-
}
|
|
2011
|
-
if (!res.headersSent && !res.writableEnded) {
|
|
2012
|
-
for (const [key, value] of ctx.headers) {
|
|
2013
|
-
try {
|
|
2014
|
-
res.setHeader(key, value);
|
|
2015
|
-
} catch (error) {
|
|
2016
|
-
}
|
|
2017
|
-
}
|
|
2018
|
-
}
|
|
2019
|
-
req.__FARM_MIDDLEWARE_DATA__ = new Map(ctx.data);
|
|
2020
|
-
req.__FARM_MIDDLEWARE_CONTEXT__ = new Map(ctx.locals);
|
|
2021
|
-
return false;
|
|
2022
|
-
}
|
|
2023
|
-
/**
|
|
2024
|
-
* Check if pathname matches middleware config
|
|
2025
|
-
*/
|
|
2026
|
-
matchesConfig(pathname, config, ctx) {
|
|
2027
|
-
return matchesMiddlewareConfig(pathname, config, ctx);
|
|
2028
|
-
}
|
|
2029
|
-
/**
|
|
2030
|
-
* Reload middleware (for HMR)
|
|
2031
|
-
*/
|
|
2032
|
-
async reload() {
|
|
2033
|
-
await this.discover();
|
|
2034
|
-
}
|
|
2035
|
-
getMiddlewares() {
|
|
2036
|
-
return [...this.configMiddleware, ...this.middleware];
|
|
2037
|
-
}
|
|
2038
|
-
hasMiddleware() {
|
|
2039
|
-
return this.configMiddleware.length > 0 || this.middleware.length > 0;
|
|
2040
|
-
}
|
|
2041
|
-
getConfigHandlers(entry) {
|
|
2042
|
-
const handlers = [];
|
|
2043
|
-
if ("handler" in entry && typeof entry.handler === "function") {
|
|
2044
|
-
handlers.push(entry.handler);
|
|
2045
|
-
}
|
|
2046
|
-
if ("handlers" in entry && Array.isArray(entry.handlers)) {
|
|
2047
|
-
handlers.push(...entry.handlers.filter((handler) => typeof handler === "function"));
|
|
2048
|
-
}
|
|
2049
|
-
return handlers;
|
|
2050
|
-
}
|
|
2051
|
-
toMiddlewareConfig(entry) {
|
|
2052
|
-
const { matcher, exclude, runtime: runtime3 } = entry;
|
|
2053
|
-
return { matcher, exclude, runtime: runtime3 };
|
|
2054
|
-
}
|
|
2055
|
-
};
|
|
2056
|
-
__name(_MiddlewareManager, "MiddlewareManager");
|
|
2057
|
-
var MiddlewareManager = _MiddlewareManager;
|
|
39
|
+
var _chunkUS2QVW3Ecjs = require('./chunk-US2QVW3E.cjs');
|
|
40
|
+
require('./chunk-U4KT7QP3.cjs');
|
|
41
|
+
require('./chunk-BIDYX3SL.cjs');
|
|
42
|
+
require('./chunk-I57LW5VZ.cjs');
|
|
43
|
+
require('./chunk-NO3WWW3N.cjs');
|
|
44
|
+
require('./chunk-S3JO3N4H.cjs');
|
|
2058
45
|
|
|
2059
|
-
// src/middleware/server.ts
|
|
2060
|
-
var import_async_hooks = require("async_hooks");
|
|
2061
|
-
var MIDDLEWARE_DATA_STORE_KEY = /* @__PURE__ */ Symbol.for("farm.middlewareDataStore");
|
|
2062
|
-
var MIDDLEWARE_CONTEXT_STORE_KEY = /* @__PURE__ */ Symbol.for("farm.middlewareContextStore");
|
|
2063
|
-
function getGlobalMiddlewareStore(key) {
|
|
2064
|
-
const state = globalThis;
|
|
2065
|
-
const existing = state[key];
|
|
2066
|
-
if (existing instanceof import_async_hooks.AsyncLocalStorage) {
|
|
2067
|
-
return existing;
|
|
2068
|
-
}
|
|
2069
|
-
const store = new import_async_hooks.AsyncLocalStorage();
|
|
2070
|
-
state[key] = store;
|
|
2071
|
-
return store;
|
|
2072
|
-
}
|
|
2073
|
-
__name(getGlobalMiddlewareStore, "getGlobalMiddlewareStore");
|
|
2074
|
-
var middlewareStore = getGlobalMiddlewareStore(MIDDLEWARE_DATA_STORE_KEY);
|
|
2075
|
-
var middlewareContextStore = getGlobalMiddlewareStore(MIDDLEWARE_CONTEXT_STORE_KEY);
|
|
2076
|
-
function toMiddlewareMap(data) {
|
|
2077
|
-
if (data instanceof Map) {
|
|
2078
|
-
return new Map(data);
|
|
2079
|
-
}
|
|
2080
|
-
return new Map(Object.entries(data));
|
|
2081
|
-
}
|
|
2082
|
-
__name(toMiddlewareMap, "toMiddlewareMap");
|
|
2083
|
-
async function _runWithMiddlewareData(data, fn) {
|
|
2084
|
-
return middlewareStore.run(toMiddlewareMap(data), fn);
|
|
2085
|
-
}
|
|
2086
|
-
__name(_runWithMiddlewareData, "_runWithMiddlewareData");
|
|
2087
|
-
async function _runWithMiddlewareContext(context2, fn) {
|
|
2088
|
-
return middlewareContextStore.run(toMiddlewareMap(context2), fn);
|
|
2089
|
-
}
|
|
2090
|
-
__name(_runWithMiddlewareContext, "_runWithMiddlewareContext");
|
|
2091
|
-
function getMiddlewareData() {
|
|
2092
|
-
return middlewareStore.getStore() || /* @__PURE__ */ new Map();
|
|
2093
|
-
}
|
|
2094
|
-
__name(getMiddlewareData, "getMiddlewareData");
|
|
2095
|
-
function getMiddlewareValue(key) {
|
|
2096
|
-
const data = getMiddlewareData();
|
|
2097
|
-
return data.get(key);
|
|
2098
|
-
}
|
|
2099
|
-
__name(getMiddlewareValue, "getMiddlewareValue");
|
|
2100
|
-
function getMiddlewareContext() {
|
|
2101
|
-
return middlewareContextStore.getStore() || /* @__PURE__ */ new Map();
|
|
2102
|
-
}
|
|
2103
|
-
__name(getMiddlewareContext, "getMiddlewareContext");
|
|
2104
46
|
|
|
2105
|
-
// src/middleware/helpers.ts
|
|
2106
|
-
function unwrapMiddleware(middleware2, defaults) {
|
|
2107
|
-
if (!middleware2) {
|
|
2108
|
-
return defaults || {};
|
|
2109
|
-
}
|
|
2110
|
-
const result = { ...defaults };
|
|
2111
|
-
for (const [key, value] of middleware2.data) {
|
|
2112
|
-
result[key] = value;
|
|
2113
|
-
}
|
|
2114
|
-
return result;
|
|
2115
|
-
}
|
|
2116
|
-
__name(unwrapMiddleware, "unwrapMiddleware");
|
|
2117
|
-
function getFromMiddleware(middleware2, key, defaultValue) {
|
|
2118
|
-
if (!middleware2) {
|
|
2119
|
-
return defaultValue;
|
|
2120
|
-
}
|
|
2121
|
-
const value = middleware2.data.get(key);
|
|
2122
|
-
return value !== void 0 ? value : defaultValue;
|
|
2123
|
-
}
|
|
2124
|
-
__name(getFromMiddleware, "getFromMiddleware");
|
|
2125
|
-
function hasMiddlewareData(middleware2) {
|
|
2126
|
-
if (!middleware2) return false;
|
|
2127
|
-
return middleware2.data.size > 0;
|
|
2128
|
-
}
|
|
2129
|
-
__name(hasMiddlewareData, "hasMiddlewareData");
|
|
2130
47
|
|
|
2131
|
-
// src/middleware/production-runtime.ts
|
|
2132
|
-
var FARM_SET_COOKIE_HEADERS = /* @__PURE__ */ Symbol("farm.setCookieHeaders");
|
|
2133
|
-
var _WebResponseHeaderMap = class _WebResponseHeaderMap extends Map {
|
|
2134
|
-
constructor() {
|
|
2135
|
-
super(...arguments);
|
|
2136
|
-
this.setCookies = [];
|
|
2137
|
-
}
|
|
2138
|
-
set(key, value) {
|
|
2139
|
-
if (key.toLowerCase() === "set-cookie") {
|
|
2140
|
-
this.setCookies = [value];
|
|
2141
|
-
super.set("Set-Cookie", value);
|
|
2142
|
-
return this;
|
|
2143
|
-
}
|
|
2144
|
-
return super.set(key, value);
|
|
2145
|
-
}
|
|
2146
|
-
delete(key) {
|
|
2147
|
-
if (key.toLowerCase() === "set-cookie") {
|
|
2148
|
-
this.setCookies = [];
|
|
2149
|
-
return super.delete("Set-Cookie") || super.delete("set-cookie");
|
|
2150
|
-
}
|
|
2151
|
-
return super.delete(key);
|
|
2152
|
-
}
|
|
2153
|
-
clear() {
|
|
2154
|
-
this.setCookies = [];
|
|
2155
|
-
super.clear();
|
|
2156
|
-
}
|
|
2157
|
-
appendSetCookie(value) {
|
|
2158
|
-
this.setCookies.push(value);
|
|
2159
|
-
super.set("Set-Cookie", value);
|
|
2160
|
-
}
|
|
2161
|
-
replaceSetCookies(values) {
|
|
2162
|
-
this.setCookies = [...values];
|
|
2163
|
-
super.delete("Set-Cookie");
|
|
2164
|
-
super.delete("set-cookie");
|
|
2165
|
-
if (this.setCookies.length > 0) {
|
|
2166
|
-
super.set("Set-Cookie", this.setCookies[this.setCookies.length - 1]);
|
|
2167
|
-
}
|
|
2168
|
-
}
|
|
2169
|
-
getSetCookies() {
|
|
2170
|
-
return this.setCookies;
|
|
2171
|
-
}
|
|
2172
|
-
};
|
|
2173
|
-
__name(_WebResponseHeaderMap, "WebResponseHeaderMap");
|
|
2174
|
-
var WebResponseHeaderMap = _WebResponseHeaderMap;
|
|
2175
|
-
var _WebCookieJar = class _WebCookieJar {
|
|
2176
|
-
constructor(request, headers) {
|
|
2177
|
-
this.headers = headers;
|
|
2178
|
-
this.cookies = parseMiddlewareCookieHeader(request.headers.get("cookie"));
|
|
2179
|
-
}
|
|
2180
|
-
get(name) {
|
|
2181
|
-
return this.cookies[name];
|
|
2182
|
-
}
|
|
2183
|
-
set(name, value, options = {}) {
|
|
2184
|
-
this.cookies[name] = value;
|
|
2185
|
-
const cookieString = serializeMiddlewareCookie(name, value, options);
|
|
2186
|
-
this.headers.appendSetCookie(cookieString);
|
|
2187
|
-
}
|
|
2188
|
-
delete(name, options = {}) {
|
|
2189
|
-
delete this.cookies[name];
|
|
2190
|
-
this.headers.appendSetCookie(serializeMiddlewareCookieDeletion(name, options));
|
|
2191
|
-
}
|
|
2192
|
-
getAll() {
|
|
2193
|
-
return { ...this.cookies };
|
|
2194
|
-
}
|
|
2195
|
-
};
|
|
2196
|
-
__name(_WebCookieJar, "WebCookieJar");
|
|
2197
|
-
var WebCookieJar = _WebCookieJar;
|
|
2198
|
-
function mapToHeaders(map) {
|
|
2199
|
-
const headers = new Headers();
|
|
2200
|
-
for (const [key, value] of map) {
|
|
2201
|
-
if (key.toLowerCase() === "set-cookie") continue;
|
|
2202
|
-
headers.set(key, value);
|
|
2203
|
-
}
|
|
2204
|
-
for (const cookie of map.getSetCookies()) {
|
|
2205
|
-
headers.append("Set-Cookie", cookie);
|
|
2206
|
-
}
|
|
2207
|
-
return headers;
|
|
2208
|
-
}
|
|
2209
|
-
__name(mapToHeaders, "mapToHeaders");
|
|
2210
|
-
function headersToRecord(headers) {
|
|
2211
|
-
return Object.fromEntries(headers);
|
|
2212
|
-
}
|
|
2213
|
-
__name(headersToRecord, "headersToRecord");
|
|
2214
|
-
function isMiddlewareResponse2(value) {
|
|
2215
|
-
return value instanceof Response;
|
|
2216
|
-
}
|
|
2217
|
-
__name(isMiddlewareResponse2, "isMiddlewareResponse");
|
|
2218
|
-
function findHeaderName(headers, name) {
|
|
2219
|
-
const normalizedName = name.toLowerCase();
|
|
2220
|
-
return [...headers.keys()].find((key) => key.toLowerCase() === normalizedName);
|
|
2221
|
-
}
|
|
2222
|
-
__name(findHeaderName, "findHeaderName");
|
|
2223
|
-
function createResponseShim(headers) {
|
|
2224
|
-
const bodyChunks = [];
|
|
2225
|
-
const encoder = new TextEncoder();
|
|
2226
|
-
const response = {
|
|
2227
|
-
headersSent: false,
|
|
2228
|
-
writableEnded: false,
|
|
2229
|
-
statusCode: 200,
|
|
2230
|
-
statusMessage: "",
|
|
2231
|
-
setHeader(name, value) {
|
|
2232
|
-
const existingName = findHeaderName(headers, name);
|
|
2233
|
-
if (existingName) headers.delete(existingName);
|
|
2234
|
-
if (name.toLowerCase() === "set-cookie") {
|
|
2235
|
-
headers.replaceSetCookies(Array.isArray(value) ? value.map(String) : [String(value)]);
|
|
2236
|
-
} else {
|
|
2237
|
-
headers.set(name, Array.isArray(value) ? value.join(", ") : String(value));
|
|
2238
|
-
}
|
|
2239
|
-
return this;
|
|
2240
|
-
},
|
|
2241
|
-
getHeader(name) {
|
|
2242
|
-
if (name.toLowerCase() === "set-cookie") {
|
|
2243
|
-
const cookies = headers.getSetCookies();
|
|
2244
|
-
return cookies.length > 0 ? [...cookies] : void 0;
|
|
2245
|
-
}
|
|
2246
|
-
const existingName = findHeaderName(headers, name);
|
|
2247
|
-
return existingName ? headers.get(existingName) : void 0;
|
|
2248
|
-
},
|
|
2249
|
-
hasHeader(name) {
|
|
2250
|
-
return name.toLowerCase() === "set-cookie" ? headers.getSetCookies().length > 0 : findHeaderName(headers, name) !== void 0;
|
|
2251
|
-
},
|
|
2252
|
-
getHeaderNames() {
|
|
2253
|
-
return [...headers.keys()].map((name) => name.toLowerCase());
|
|
2254
|
-
},
|
|
2255
|
-
getHeaders() {
|
|
2256
|
-
return Object.fromEntries(
|
|
2257
|
-
[...headers.keys()].map((name) => [name.toLowerCase(), this.getHeader(name)])
|
|
2258
|
-
);
|
|
2259
|
-
},
|
|
2260
|
-
removeHeader(name) {
|
|
2261
|
-
const existingName = findHeaderName(headers, name);
|
|
2262
|
-
if (existingName) headers.delete(existingName);
|
|
2263
|
-
},
|
|
2264
|
-
appendHeader(name, value) {
|
|
2265
|
-
const values = Array.isArray(value) ? value.map(String) : [String(value)];
|
|
2266
|
-
if (name.toLowerCase() === "set-cookie") {
|
|
2267
|
-
for (const item of values) headers.appendSetCookie(item);
|
|
2268
|
-
return this;
|
|
2269
|
-
}
|
|
2270
|
-
const existing = this.getHeader(name);
|
|
2271
|
-
return this.setHeader(name, existing ? `${String(existing)}, ${values.join(", ")}` : values);
|
|
2272
|
-
},
|
|
2273
|
-
writeHead(status, statusMessageOrHeaders, responseHeaders) {
|
|
2274
|
-
this.statusCode = status;
|
|
2275
|
-
const nextHeaders = typeof statusMessageOrHeaders === "string" ? responseHeaders : statusMessageOrHeaders;
|
|
2276
|
-
if (typeof statusMessageOrHeaders === "string") {
|
|
2277
|
-
this.statusMessage = statusMessageOrHeaders;
|
|
2278
|
-
}
|
|
2279
|
-
if (nextHeaders) {
|
|
2280
|
-
for (const [key, value] of Object.entries(nextHeaders)) {
|
|
2281
|
-
this.setHeader(key, value);
|
|
2282
|
-
}
|
|
2283
|
-
}
|
|
2284
|
-
this.headersSent = true;
|
|
2285
|
-
return this;
|
|
2286
|
-
},
|
|
2287
|
-
flushHeaders() {
|
|
2288
|
-
this.headersSent = true;
|
|
2289
|
-
},
|
|
2290
|
-
write(chunk, encodingOrCallback, callback) {
|
|
2291
|
-
bodyChunks.push(typeof chunk === "string" ? encoder.encode(chunk) : new Uint8Array(chunk));
|
|
2292
|
-
this.headersSent = true;
|
|
2293
|
-
const done = typeof encodingOrCallback === "function" ? encodingOrCallback : callback;
|
|
2294
|
-
done?.();
|
|
2295
|
-
return true;
|
|
2296
|
-
},
|
|
2297
|
-
end(chunk, encodingOrCallback, callback) {
|
|
2298
|
-
if (typeof chunk === "string" || chunk instanceof Uint8Array) {
|
|
2299
|
-
bodyChunks.push(typeof chunk === "string" ? encoder.encode(chunk) : new Uint8Array(chunk));
|
|
2300
|
-
}
|
|
2301
|
-
this.headersSent = true;
|
|
2302
|
-
this.writableEnded = true;
|
|
2303
|
-
const done = typeof chunk === "function" ? chunk : typeof encodingOrCallback === "function" ? encodingOrCallback : callback;
|
|
2304
|
-
done?.();
|
|
2305
|
-
return this;
|
|
2306
|
-
}
|
|
2307
|
-
};
|
|
2308
|
-
return {
|
|
2309
|
-
response,
|
|
2310
|
-
getResponse(method) {
|
|
2311
|
-
if (!response.headersSent && !response.writableEnded) return null;
|
|
2312
|
-
const bodyLength = bodyChunks.reduce((total, chunk) => total + chunk.byteLength, 0);
|
|
2313
|
-
const body = new Uint8Array(bodyLength);
|
|
2314
|
-
let offset = 0;
|
|
2315
|
-
for (const chunk of bodyChunks) {
|
|
2316
|
-
body.set(chunk, offset);
|
|
2317
|
-
offset += chunk.byteLength;
|
|
2318
|
-
}
|
|
2319
|
-
const bodyAllowed = method.toUpperCase() !== "HEAD" && response.statusCode !== 204 && response.statusCode !== 205 && response.statusCode !== 304;
|
|
2320
|
-
return new Response(bodyAllowed && bodyLength > 0 ? body : null, {
|
|
2321
|
-
status: response.statusCode,
|
|
2322
|
-
statusText: response.statusMessage || void 0,
|
|
2323
|
-
headers: mapToHeaders(headers)
|
|
2324
|
-
});
|
|
2325
|
-
}
|
|
2326
|
-
};
|
|
2327
|
-
}
|
|
2328
|
-
__name(createResponseShim, "createResponseShim");
|
|
2329
|
-
function withNodeRequestShape(request, trustProxy, clientAddress) {
|
|
2330
|
-
const requestWithShape = request;
|
|
2331
|
-
if (!requestWithShape.socket) {
|
|
2332
|
-
const forwardedFor = trustProxy ? request.headers.get("x-forwarded-for")?.split(",")[0]?.trim() : void 0;
|
|
2333
|
-
try {
|
|
2334
|
-
requestWithShape.socket = { remoteAddress: forwardedFor || clientAddress || void 0 };
|
|
2335
|
-
} catch {
|
|
2336
|
-
}
|
|
2337
|
-
}
|
|
2338
|
-
return requestWithShape;
|
|
2339
|
-
}
|
|
2340
|
-
__name(withNodeRequestShape, "withNodeRequestShape");
|
|
2341
|
-
function createHandledResponse(body, init, headers) {
|
|
2342
|
-
const responseHeaders = new Headers();
|
|
2343
|
-
for (const [key, value] of headers) {
|
|
2344
|
-
if (key.toLowerCase() === "set-cookie") continue;
|
|
2345
|
-
responseHeaders.set(key, value);
|
|
2346
|
-
}
|
|
2347
|
-
for (const [key, value] of new Headers(init.headers)) {
|
|
2348
|
-
responseHeaders.set(key, value);
|
|
2349
|
-
}
|
|
2350
|
-
for (const cookie of headers.getSetCookies()) {
|
|
2351
|
-
responseHeaders.append("Set-Cookie", cookie);
|
|
2352
|
-
}
|
|
2353
|
-
return new Response(body, { ...init, headers: responseHeaders });
|
|
2354
|
-
}
|
|
2355
|
-
__name(createHandledResponse, "createHandledResponse");
|
|
2356
|
-
function createWebMiddlewareContext(request, parent, trustProxy = false, clientAddress) {
|
|
2357
|
-
let currentRequest = withNodeRequestShape(request, trustProxy, clientAddress);
|
|
2358
|
-
let handledResponse = null;
|
|
2359
|
-
const url = new URL(currentRequest.url);
|
|
2360
|
-
const data = parent?.data ? new Map(parent.data) : /* @__PURE__ */ new Map();
|
|
2361
|
-
const locals = parent?.locals ? new Map(parent.locals) : /* @__PURE__ */ new Map();
|
|
2362
|
-
const headers = new WebResponseHeaderMap();
|
|
2363
|
-
if (parent?.headers) {
|
|
2364
|
-
for (const [key, value] of Object.entries(parent.headers)) {
|
|
2365
|
-
if (key.toLowerCase() === "set-cookie") continue;
|
|
2366
|
-
headers.set(key, value);
|
|
2367
|
-
}
|
|
2368
|
-
}
|
|
2369
|
-
const parentCookies = parent?.[FARM_SET_COOKIE_HEADERS];
|
|
2370
|
-
if (parentCookies) headers.replaceSetCookies(parentCookies);
|
|
2371
|
-
const responseShim = createResponseShim(headers);
|
|
2372
|
-
const ctx = {
|
|
2373
|
-
request: currentRequest,
|
|
2374
|
-
response: responseShim.response,
|
|
2375
|
-
url,
|
|
2376
|
-
pathname: url.pathname,
|
|
2377
|
-
searchParams: url.searchParams,
|
|
2378
|
-
method: currentRequest.method || "GET",
|
|
2379
|
-
params: {},
|
|
2380
|
-
route: url.pathname,
|
|
2381
|
-
parent,
|
|
2382
|
-
vite: {
|
|
2383
|
-
isDev: false,
|
|
2384
|
-
hmr: false
|
|
2385
|
-
},
|
|
2386
|
-
data,
|
|
2387
|
-
locals,
|
|
2388
|
-
headers,
|
|
2389
|
-
cookies: new WebCookieJar(currentRequest, headers),
|
|
2390
|
-
_handled: false,
|
|
2391
|
-
_redirectUrl: void 0,
|
|
2392
|
-
_rewriteUrl: void 0,
|
|
2393
|
-
redirect(redirectUrl, status = 307) {
|
|
2394
|
-
ctx._redirectUrl = redirectUrl;
|
|
2395
|
-
ctx._handled = true;
|
|
2396
|
-
handledResponse = createHandledResponse(
|
|
2397
|
-
`Redirecting to ${redirectUrl}`,
|
|
2398
|
-
{
|
|
2399
|
-
status,
|
|
2400
|
-
headers: {
|
|
2401
|
-
Location: redirectUrl,
|
|
2402
|
-
"Content-Type": "text/plain"
|
|
2403
|
-
}
|
|
2404
|
-
},
|
|
2405
|
-
headers
|
|
2406
|
-
);
|
|
2407
|
-
},
|
|
2408
|
-
rewrite(rewriteUrl) {
|
|
2409
|
-
ctx._rewriteUrl = rewriteUrl;
|
|
2410
|
-
const nextUrl = new URL(rewriteUrl, currentRequest.url);
|
|
2411
|
-
currentRequest = withNodeRequestShape(
|
|
2412
|
-
new Request(nextUrl, currentRequest),
|
|
2413
|
-
trustProxy,
|
|
2414
|
-
clientAddress
|
|
2415
|
-
);
|
|
2416
|
-
ctx.request = currentRequest;
|
|
2417
|
-
ctx.url = nextUrl;
|
|
2418
|
-
ctx.pathname = nextUrl.pathname;
|
|
2419
|
-
ctx.searchParams = nextUrl.searchParams;
|
|
2420
|
-
ctx.route = nextUrl.pathname;
|
|
2421
|
-
},
|
|
2422
|
-
json(jsonData, status = 200) {
|
|
2423
|
-
ctx._handled = true;
|
|
2424
|
-
handledResponse = createHandledResponse(
|
|
2425
|
-
JSON.stringify(jsonData),
|
|
2426
|
-
{
|
|
2427
|
-
status,
|
|
2428
|
-
headers: {
|
|
2429
|
-
"Content-Type": "application/json"
|
|
2430
|
-
}
|
|
2431
|
-
},
|
|
2432
|
-
headers
|
|
2433
|
-
);
|
|
2434
|
-
},
|
|
2435
|
-
text(content, status = 200) {
|
|
2436
|
-
ctx._handled = true;
|
|
2437
|
-
handledResponse = createHandledResponse(
|
|
2438
|
-
content,
|
|
2439
|
-
{
|
|
2440
|
-
status,
|
|
2441
|
-
headers: {
|
|
2442
|
-
"Content-Type": "text/plain"
|
|
2443
|
-
}
|
|
2444
|
-
},
|
|
2445
|
-
headers
|
|
2446
|
-
);
|
|
2447
|
-
},
|
|
2448
|
-
html(content, status = 200) {
|
|
2449
|
-
ctx._handled = true;
|
|
2450
|
-
handledResponse = createHandledResponse(
|
|
2451
|
-
content,
|
|
2452
|
-
{
|
|
2453
|
-
status,
|
|
2454
|
-
headers: {
|
|
2455
|
-
"Content-Type": "text/html"
|
|
2456
|
-
}
|
|
2457
|
-
},
|
|
2458
|
-
headers
|
|
2459
|
-
);
|
|
2460
|
-
}
|
|
2461
|
-
};
|
|
2462
|
-
return {
|
|
2463
|
-
ctx,
|
|
2464
|
-
headers,
|
|
2465
|
-
getRequest: /* @__PURE__ */ __name(() => currentRequest, "getRequest"),
|
|
2466
|
-
getResponse: /* @__PURE__ */ __name(() => handledResponse || responseShim.getResponse(currentRequest.method), "getResponse")
|
|
2467
|
-
};
|
|
2468
|
-
}
|
|
2469
|
-
__name(createWebMiddlewareContext, "createWebMiddlewareContext");
|
|
2470
|
-
function matchesMiddlewareConfig2(pathname, config, ctx) {
|
|
2471
|
-
return matchesMiddlewareConfig(pathname, config, ctx);
|
|
2472
|
-
}
|
|
2473
|
-
__name(matchesMiddlewareConfig2, "matchesMiddlewareConfig");
|
|
2474
|
-
function getConfigHandlers(entry) {
|
|
2475
|
-
const handlers = [];
|
|
2476
|
-
if ("handler" in entry && typeof entry.handler === "function") {
|
|
2477
|
-
handlers.push(entry.handler);
|
|
2478
|
-
}
|
|
2479
|
-
if ("handlers" in entry && Array.isArray(entry.handlers)) {
|
|
2480
|
-
handlers.push(...entry.handlers.filter((handler) => typeof handler === "function"));
|
|
2481
|
-
}
|
|
2482
|
-
return handlers;
|
|
2483
|
-
}
|
|
2484
|
-
__name(getConfigHandlers, "getConfigHandlers");
|
|
2485
|
-
function toMiddlewareConfig(entry) {
|
|
2486
|
-
const { matcher, exclude, runtime: runtime3 } = entry;
|
|
2487
|
-
return { matcher, exclude, runtime: runtime3 };
|
|
2488
|
-
}
|
|
2489
|
-
__name(toMiddlewareConfig, "toMiddlewareConfig");
|
|
2490
|
-
function normalizeConfigMiddleware(config) {
|
|
2491
|
-
const entries = [];
|
|
2492
|
-
let globalConfig;
|
|
2493
|
-
if (!config) {
|
|
2494
|
-
return { entries, globalConfig };
|
|
2495
|
-
}
|
|
2496
|
-
const configEntries = Array.isArray(config) ? config : [config];
|
|
2497
|
-
for (const [index, entry] of configEntries.entries()) {
|
|
2498
|
-
const handlers = getConfigHandlers(entry);
|
|
2499
|
-
const middlewareConfig = toMiddlewareConfig(entry);
|
|
2500
|
-
if (handlers.length === 0) {
|
|
2501
|
-
globalConfig = middlewareConfig;
|
|
2502
|
-
continue;
|
|
2503
|
-
}
|
|
2504
|
-
compileMiddlewareConfig(middlewareConfig);
|
|
2505
|
-
entries.push({
|
|
2506
|
-
path: "/",
|
|
2507
|
-
filePath: `farm.config.ts#middleware-${index}`,
|
|
2508
|
-
handlers,
|
|
2509
|
-
config: middlewareConfig,
|
|
2510
|
-
source: "config"
|
|
2511
|
-
});
|
|
2512
|
-
}
|
|
2513
|
-
return { entries, globalConfig };
|
|
2514
|
-
}
|
|
2515
|
-
__name(normalizeConfigMiddleware, "normalizeConfigMiddleware");
|
|
2516
|
-
function normalizeFileMiddleware(modules = []) {
|
|
2517
|
-
const entries = [];
|
|
2518
|
-
for (const moduleEntry of modules) {
|
|
2519
|
-
const normalized = normalizeMiddlewareModule(moduleEntry.module, moduleEntry.path);
|
|
2520
|
-
if (!normalized) {
|
|
2521
|
-
continue;
|
|
2522
|
-
}
|
|
2523
|
-
if (normalized.config) compileMiddlewareConfig(normalized.config);
|
|
2524
|
-
entries.push({
|
|
2525
|
-
path: moduleEntry.path,
|
|
2526
|
-
filePath: moduleEntry.filePath || moduleEntry.path,
|
|
2527
|
-
handlers: normalized.handlers,
|
|
2528
|
-
config: normalized.config,
|
|
2529
|
-
source: "file"
|
|
2530
|
-
});
|
|
2531
|
-
}
|
|
2532
|
-
return entries;
|
|
2533
|
-
}
|
|
2534
|
-
__name(normalizeFileMiddleware, "normalizeFileMiddleware");
|
|
2535
|
-
async function executeHandlers(handlers, ctx) {
|
|
2536
|
-
let handlerIndex = 0;
|
|
2537
|
-
let returnedResponse;
|
|
2538
|
-
const executeNext = /* @__PURE__ */ __name(async () => {
|
|
2539
|
-
if (handlerIndex < handlers.length) {
|
|
2540
|
-
const handler = handlers[handlerIndex++];
|
|
2541
|
-
const result2 = await handler(ctx, executeNext);
|
|
2542
|
-
if (isMiddlewareResponse2(result2)) {
|
|
2543
|
-
returnedResponse = result2;
|
|
2544
|
-
return result2;
|
|
2545
|
-
}
|
|
2546
|
-
}
|
|
2547
|
-
return returnedResponse;
|
|
2548
|
-
}, "executeNext");
|
|
2549
|
-
const result = await executeNext();
|
|
2550
|
-
return isMiddlewareResponse2(result) ? result : returnedResponse;
|
|
2551
|
-
}
|
|
2552
|
-
__name(executeHandlers, "executeHandlers");
|
|
2553
|
-
function emptyResult(request) {
|
|
2554
|
-
return {
|
|
2555
|
-
request,
|
|
2556
|
-
response: null,
|
|
2557
|
-
data: /* @__PURE__ */ new Map(),
|
|
2558
|
-
context: /* @__PURE__ */ new Map(),
|
|
2559
|
-
headers: new Headers(),
|
|
2560
|
-
handled: false
|
|
2561
|
-
};
|
|
2562
|
-
}
|
|
2563
|
-
__name(emptyResult, "emptyResult");
|
|
2564
|
-
function createProductionMiddlewareRunner(options = {}) {
|
|
2565
|
-
const configMiddleware = normalizeConfigMiddleware(options.config);
|
|
2566
|
-
const fileMiddleware = normalizeFileMiddleware(options.modules);
|
|
2567
|
-
for (const entry of fileMiddleware) compileMiddlewareRoute(entry);
|
|
2568
|
-
const entries = [...configMiddleware.entries, ...fileMiddleware];
|
|
2569
|
-
const globalConfig = configMiddleware.globalConfig;
|
|
2570
|
-
if (globalConfig) compileMiddlewareConfig(globalConfig);
|
|
2571
|
-
return /* @__PURE__ */ __name(async function runProductionMiddleware(request, requestOptions = {}) {
|
|
2572
|
-
if (!globalConfig && entries.length === 0) {
|
|
2573
|
-
return emptyResult(request);
|
|
2574
|
-
}
|
|
2575
|
-
let contextState = createWebMiddlewareContext(
|
|
2576
|
-
request,
|
|
2577
|
-
void 0,
|
|
2578
|
-
options.server?.trustProxy === true,
|
|
2579
|
-
requestOptions.clientAddress
|
|
2580
|
-
);
|
|
2581
|
-
let ctx = contextState.ctx;
|
|
2582
|
-
let currentRequest = contextState.getRequest();
|
|
2583
|
-
const initialPathname = options.i18n?.enabled ? stripFarmLocaleFromPathname(ctx.pathname, options.i18n) : stripFarmBasePath(ctx.pathname, options.i18n?.basePath ?? "/");
|
|
2584
|
-
let parentData;
|
|
2585
|
-
if (globalConfig) {
|
|
2586
|
-
const globalMatch = matchesMiddlewareConfig(initialPathname, globalConfig, ctx);
|
|
2587
|
-
if (!globalMatch.matched) {
|
|
2588
|
-
return emptyResult(request);
|
|
2589
|
-
}
|
|
2590
|
-
if (globalMatch.params) {
|
|
2591
|
-
ctx.params = { ...ctx.params, ...globalMatch.params };
|
|
2592
|
-
}
|
|
2593
|
-
}
|
|
2594
|
-
const applicable = entries.map((entry) => ({
|
|
2595
|
-
entry,
|
|
2596
|
-
routeMatch: entry.source === "config" ? { matched: true } : matchesMiddlewareRoute(initialPathname, entry)
|
|
2597
|
-
})).filter((candidate) => candidate.routeMatch.matched);
|
|
2598
|
-
if (applicable.length === 0) {
|
|
2599
|
-
return {
|
|
2600
|
-
request: currentRequest,
|
|
2601
|
-
response: null,
|
|
2602
|
-
data: new Map(ctx.data),
|
|
2603
|
-
context: new Map(ctx.locals),
|
|
2604
|
-
headers: mapToHeaders(contextState.headers),
|
|
2605
|
-
handled: false
|
|
2606
|
-
};
|
|
2607
|
-
}
|
|
2608
|
-
for (const candidate of applicable) {
|
|
2609
|
-
const { entry, routeMatch } = candidate;
|
|
2610
|
-
const configMatch = entry.config ? matchesMiddlewareConfig(initialPathname, entry.config, ctx) : { matched: true };
|
|
2611
|
-
if (!configMatch.matched) {
|
|
2612
|
-
continue;
|
|
2613
|
-
}
|
|
2614
|
-
if (parentData) {
|
|
2615
|
-
contextState = createWebMiddlewareContext(
|
|
2616
|
-
currentRequest,
|
|
2617
|
-
parentData,
|
|
2618
|
-
options.server?.trustProxy === true,
|
|
2619
|
-
requestOptions.clientAddress
|
|
2620
|
-
);
|
|
2621
|
-
ctx = contextState.ctx;
|
|
2622
|
-
}
|
|
2623
|
-
if (routeMatch.params) {
|
|
2624
|
-
ctx.params = { ...ctx.params, ...routeMatch.params };
|
|
2625
|
-
}
|
|
2626
|
-
if (configMatch.params) {
|
|
2627
|
-
ctx.params = { ...ctx.params, ...configMatch.params };
|
|
2628
|
-
}
|
|
2629
|
-
const middlewareStartTime = Date.now();
|
|
2630
|
-
const middlewareEvent = {
|
|
2631
|
-
route: entry.path,
|
|
2632
|
-
pathname: initialPathname,
|
|
2633
|
-
name: entry.filePath
|
|
2634
|
-
};
|
|
2635
|
-
emitFarmEvent({ type: "middleware.start", ...middlewareEvent });
|
|
2636
|
-
try {
|
|
2637
|
-
const returnedResponse = await executeHandlers(entry.handlers, ctx);
|
|
2638
|
-
currentRequest = contextState.getRequest();
|
|
2639
|
-
if (returnedResponse) {
|
|
2640
|
-
const response = applyProductionMiddlewareHeaders(
|
|
2641
|
-
returnedResponse,
|
|
2642
|
-
mapToHeaders(contextState.headers)
|
|
2643
|
-
);
|
|
2644
|
-
emitFarmEvent({
|
|
2645
|
-
type: "middleware.shortCircuit",
|
|
2646
|
-
...middlewareEvent,
|
|
2647
|
-
status: response.status
|
|
2648
|
-
});
|
|
2649
|
-
return {
|
|
2650
|
-
request: currentRequest,
|
|
2651
|
-
response,
|
|
2652
|
-
data: new Map(ctx.data),
|
|
2653
|
-
context: new Map(ctx.locals),
|
|
2654
|
-
headers: mapToHeaders(contextState.headers),
|
|
2655
|
-
handled: true
|
|
2656
|
-
};
|
|
2657
|
-
}
|
|
2658
|
-
if (ctx._handled || ctx.response.headersSent || ctx.response.writableEnded) {
|
|
2659
|
-
const response = contextState.getResponse() || new Response(null);
|
|
2660
|
-
emitFarmEvent({
|
|
2661
|
-
type: "middleware.shortCircuit",
|
|
2662
|
-
...middlewareEvent,
|
|
2663
|
-
status: response.status
|
|
2664
|
-
});
|
|
2665
|
-
return {
|
|
2666
|
-
request: currentRequest,
|
|
2667
|
-
response,
|
|
2668
|
-
data: new Map(ctx.data),
|
|
2669
|
-
context: new Map(ctx.locals),
|
|
2670
|
-
headers: mapToHeaders(contextState.headers),
|
|
2671
|
-
handled: true
|
|
2672
|
-
};
|
|
2673
|
-
}
|
|
2674
|
-
emitFarmEvent({
|
|
2675
|
-
type: "middleware.complete",
|
|
2676
|
-
...middlewareEvent,
|
|
2677
|
-
durationMs: Date.now() - middlewareStartTime
|
|
2678
|
-
});
|
|
2679
|
-
} catch (error) {
|
|
2680
|
-
currentRequest = contextState.getRequest();
|
|
2681
|
-
if (isFarmRedirectError(error)) {
|
|
2682
|
-
const redirect = getFarmRedirectError(error);
|
|
2683
|
-
const response = applyProductionMiddlewareHeaders(
|
|
2684
|
-
new Response(null, { status: redirect.status, headers: { Location: redirect.url } }),
|
|
2685
|
-
mapToHeaders(contextState.headers)
|
|
2686
|
-
);
|
|
2687
|
-
emitFarmEvent({
|
|
2688
|
-
type: "middleware.shortCircuit",
|
|
2689
|
-
...middlewareEvent,
|
|
2690
|
-
status: response.status
|
|
2691
|
-
});
|
|
2692
|
-
return {
|
|
2693
|
-
request: currentRequest,
|
|
2694
|
-
response,
|
|
2695
|
-
data: new Map(ctx.data),
|
|
2696
|
-
context: new Map(ctx.locals),
|
|
2697
|
-
headers: mapToHeaders(contextState.headers),
|
|
2698
|
-
handled: true
|
|
2699
|
-
};
|
|
2700
|
-
}
|
|
2701
|
-
if (isFarmNotFoundError(error)) {
|
|
2702
|
-
emitFarmEvent({ type: "middleware.shortCircuit", ...middlewareEvent, status: 404 });
|
|
2703
|
-
return {
|
|
2704
|
-
request: currentRequest,
|
|
2705
|
-
response: null,
|
|
2706
|
-
data: new Map(ctx.data),
|
|
2707
|
-
context: new Map(ctx.locals),
|
|
2708
|
-
headers: mapToHeaders(contextState.headers),
|
|
2709
|
-
handled: true,
|
|
2710
|
-
notFound: true
|
|
2711
|
-
};
|
|
2712
|
-
}
|
|
2713
|
-
emitFarmEvent({
|
|
2714
|
-
type: "middleware.error",
|
|
2715
|
-
...middlewareEvent,
|
|
2716
|
-
error
|
|
2717
|
-
});
|
|
2718
|
-
throw error;
|
|
2719
|
-
}
|
|
2720
|
-
parentData = {
|
|
2721
|
-
data: new Map(ctx.data),
|
|
2722
|
-
locals: new Map(ctx.locals),
|
|
2723
|
-
headers: headersToRecord(contextState.headers),
|
|
2724
|
-
[FARM_SET_COOKIE_HEADERS]: [...contextState.headers.getSetCookies()]
|
|
2725
|
-
};
|
|
2726
|
-
}
|
|
2727
|
-
return {
|
|
2728
|
-
request: currentRequest,
|
|
2729
|
-
response: null,
|
|
2730
|
-
data: new Map(ctx.data),
|
|
2731
|
-
context: new Map(ctx.locals),
|
|
2732
|
-
headers: mapToHeaders(contextState.headers),
|
|
2733
|
-
handled: false
|
|
2734
|
-
};
|
|
2735
|
-
}, "runProductionMiddleware");
|
|
2736
|
-
}
|
|
2737
|
-
__name(createProductionMiddlewareRunner, "createProductionMiddlewareRunner");
|
|
2738
|
-
function applyProductionMiddlewareHeaders(response, middlewareHeaders) {
|
|
2739
|
-
if (!middlewareHeaders || Array.from(middlewareHeaders.keys()).length === 0) {
|
|
2740
|
-
return response;
|
|
2741
|
-
}
|
|
2742
|
-
const headers = new Headers(response.headers);
|
|
2743
|
-
const getSetCookie = middlewareHeaders.getSetCookie;
|
|
2744
|
-
const setCookies = getSetCookie ? getSetCookie.call(middlewareHeaders) : middlewareHeaders.get("set-cookie") ? [middlewareHeaders.get("set-cookie")] : [];
|
|
2745
|
-
for (const [key, value] of middlewareHeaders) {
|
|
2746
|
-
if (key.toLowerCase() === "set-cookie") continue;
|
|
2747
|
-
if (headers.has(key)) continue;
|
|
2748
|
-
headers.set(key, value);
|
|
2749
|
-
}
|
|
2750
|
-
for (const cookie of setCookies) {
|
|
2751
|
-
headers.append("Set-Cookie", cookie);
|
|
2752
|
-
}
|
|
2753
|
-
return new Response(response.body, {
|
|
2754
|
-
status: response.status,
|
|
2755
|
-
statusText: response.statusText,
|
|
2756
|
-
headers
|
|
2757
|
-
});
|
|
2758
|
-
}
|
|
2759
|
-
__name(applyProductionMiddlewareHeaders, "applyProductionMiddlewareHeaders");
|
|
2760
48
|
|
|
2761
|
-
// src/middleware/vite-plugin.ts
|
|
2762
|
-
var import_node_fs = __toESM(require("fs"));
|
|
2763
|
-
var import_node_path2 = __toESM(require("path"));
|
|
2764
49
|
|
|
2765
|
-
// src/i18n/config.ts
|
|
2766
|
-
var import_node_path = __toESM(require("path"));
|
|
2767
|
-
var DEFAULT_FARM_I18N_COOKIE = "farm_locale";
|
|
2768
|
-
var DEFAULT_FARM_I18N_COOKIE_MAX_AGE = 60 * 60 * 24 * 365;
|
|
2769
|
-
var DEFAULT_DETECTION = ["url", "cookie", "accept-language"];
|
|
2770
|
-
function resolveFarmI18nConfig(input, options = {}) {
|
|
2771
|
-
const root = options.root || process.cwd();
|
|
2772
|
-
const basePath = options.basePath || "/";
|
|
2773
|
-
const strictByDefault = options.mode === "production";
|
|
2774
|
-
if (!input) {
|
|
2775
|
-
return {
|
|
2776
|
-
enabled: false,
|
|
2777
|
-
basePath,
|
|
2778
|
-
locales: ["en"],
|
|
2779
|
-
defaultLocale: "en",
|
|
2780
|
-
messages: import_node_path.default.join(root, "src/messages"),
|
|
2781
|
-
routing: "none",
|
|
2782
|
-
detection: [],
|
|
2783
|
-
fallbackLocale: "en",
|
|
2784
|
-
strict: strictByDefault,
|
|
2785
|
-
cookie: {
|
|
2786
|
-
name: DEFAULT_FARM_I18N_COOKIE,
|
|
2787
|
-
maxAge: DEFAULT_FARM_I18N_COOKIE_MAX_AGE,
|
|
2788
|
-
path: "/",
|
|
2789
|
-
sameSite: "lax",
|
|
2790
|
-
secure: options.mode === "production"
|
|
2791
|
-
},
|
|
2792
|
-
direction: {}
|
|
2793
|
-
};
|
|
2794
|
-
}
|
|
2795
|
-
if (!Array.isArray(input.locales) || input.locales.length === 0) {
|
|
2796
|
-
throw new Error("i18n.locales must contain at least one locale.");
|
|
2797
|
-
}
|
|
2798
|
-
const locales = input.locales.map(canonicalizeLocale);
|
|
2799
|
-
if (new Set(locales).size !== locales.length) {
|
|
2800
|
-
throw new Error("i18n.locales must not contain duplicate locales.");
|
|
2801
|
-
}
|
|
2802
|
-
const defaultLocale = canonicalizeLocale(input.defaultLocale);
|
|
2803
|
-
if (!locales.includes(defaultLocale)) {
|
|
2804
|
-
throw new Error(`i18n.defaultLocale "${defaultLocale}" must be included in i18n.locales.`);
|
|
2805
|
-
}
|
|
2806
|
-
const fallbackLocale = canonicalizeLocale(input.fallbackLocale || defaultLocale);
|
|
2807
|
-
if (!locales.includes(fallbackLocale)) {
|
|
2808
|
-
throw new Error(`i18n.fallbackLocale "${fallbackLocale}" must be included in i18n.locales.`);
|
|
2809
|
-
}
|
|
2810
|
-
const detection = resolveDetection(input);
|
|
2811
|
-
const sameSite = input.cookie?.sameSite ?? "lax";
|
|
2812
|
-
if (sameSite !== "lax" && sameSite !== "strict" && sameSite !== "none") {
|
|
2813
|
-
throw new Error('i18n.cookie.sameSite must be "lax", "strict", or "none".');
|
|
2814
|
-
}
|
|
2815
|
-
const direction = {};
|
|
2816
|
-
for (const [rawLocale, value] of Object.entries(input.direction || {})) {
|
|
2817
|
-
const locale = canonicalizeLocale(rawLocale);
|
|
2818
|
-
if (!locales.includes(locale)) {
|
|
2819
|
-
throw new Error(`i18n.direction contains unknown locale "${rawLocale}".`);
|
|
2820
|
-
}
|
|
2821
|
-
if (value !== "ltr" && value !== "rtl") {
|
|
2822
|
-
throw new Error(`i18n.direction.${rawLocale} must be "ltr" or "rtl".`);
|
|
2823
|
-
}
|
|
2824
|
-
direction[locale] = value;
|
|
2825
|
-
}
|
|
2826
|
-
return {
|
|
2827
|
-
enabled: true,
|
|
2828
|
-
basePath,
|
|
2829
|
-
locales,
|
|
2830
|
-
defaultLocale,
|
|
2831
|
-
messages: import_node_path.default.resolve(root, input.messages || "src/messages"),
|
|
2832
|
-
routing: input.routing || "prefix-except-default",
|
|
2833
|
-
detection,
|
|
2834
|
-
fallbackLocale,
|
|
2835
|
-
strict: input.strict ?? strictByDefault,
|
|
2836
|
-
cookie: {
|
|
2837
|
-
name: input.cookie?.name?.trim() || DEFAULT_FARM_I18N_COOKIE,
|
|
2838
|
-
maxAge: normalizePositiveInteger(
|
|
2839
|
-
input.cookie?.maxAge,
|
|
2840
|
-
DEFAULT_FARM_I18N_COOKIE_MAX_AGE,
|
|
2841
|
-
"i18n.cookie.maxAge"
|
|
2842
|
-
),
|
|
2843
|
-
path: normalizeCookiePath(input.cookie?.path),
|
|
2844
|
-
sameSite,
|
|
2845
|
-
secure: input.cookie?.secure ?? options.mode === "production"
|
|
2846
|
-
},
|
|
2847
|
-
direction
|
|
2848
|
-
};
|
|
2849
|
-
}
|
|
2850
|
-
__name(resolveFarmI18nConfig, "resolveFarmI18nConfig");
|
|
2851
|
-
function canonicalizeLocale(locale) {
|
|
2852
|
-
if (!locale || typeof locale !== "string") {
|
|
2853
|
-
throw new Error("Farm i18n locales must be non-empty strings.");
|
|
2854
|
-
}
|
|
2855
|
-
try {
|
|
2856
|
-
return Intl.getCanonicalLocales(locale)[0];
|
|
2857
|
-
} catch {
|
|
2858
|
-
throw new Error(`Invalid i18n locale "${locale}".`);
|
|
2859
|
-
}
|
|
2860
|
-
}
|
|
2861
|
-
__name(canonicalizeLocale, "canonicalizeLocale");
|
|
2862
|
-
function resolveDetection(input) {
|
|
2863
|
-
if (input.detection === false || input.localeDetection === false) {
|
|
2864
|
-
return ["url"];
|
|
2865
|
-
}
|
|
2866
|
-
const detection = input.detection || DEFAULT_DETECTION;
|
|
2867
|
-
const allowed = /* @__PURE__ */ new Set(["url", "cookie", "accept-language"]);
|
|
2868
|
-
const unique = [];
|
|
2869
|
-
for (const signal of detection) {
|
|
2870
|
-
if (!allowed.has(signal)) {
|
|
2871
|
-
throw new Error(`Unsupported i18n detection signal "${signal}".`);
|
|
2872
|
-
}
|
|
2873
|
-
if (!unique.includes(signal)) unique.push(signal);
|
|
2874
|
-
}
|
|
2875
|
-
return unique;
|
|
2876
|
-
}
|
|
2877
|
-
__name(resolveDetection, "resolveDetection");
|
|
2878
|
-
function normalizePositiveInteger(value, fallback, name) {
|
|
2879
|
-
if (value === void 0) return fallback;
|
|
2880
|
-
if (!Number.isInteger(value) || value < 0) {
|
|
2881
|
-
throw new Error(`${name} must be a positive integer.`);
|
|
2882
|
-
}
|
|
2883
|
-
return value;
|
|
2884
|
-
}
|
|
2885
|
-
__name(normalizePositiveInteger, "normalizePositiveInteger");
|
|
2886
|
-
function normalizeCookiePath(value) {
|
|
2887
|
-
if (value === void 0) return "/";
|
|
2888
|
-
if (typeof value !== "string") {
|
|
2889
|
-
throw new Error("i18n.cookie.path must be a root-relative pathname.");
|
|
2890
|
-
}
|
|
2891
|
-
const hasUnstableCharacters = /* @__PURE__ */ __name((candidate) => candidate.includes("\\") || Array.from(candidate).some((character) => {
|
|
2892
|
-
const code = character.charCodeAt(0);
|
|
2893
|
-
return code <= 31 || code >= 127 && code <= 159;
|
|
2894
|
-
}), "hasUnstableCharacters");
|
|
2895
|
-
if (hasUnstableCharacters(value)) {
|
|
2896
|
-
throw new Error("i18n.cookie.path cannot contain backslashes or control characters.");
|
|
2897
|
-
}
|
|
2898
|
-
const pathname = value.trim();
|
|
2899
|
-
if (!pathname || !pathname.startsWith("/") || pathname.startsWith("//") || pathname.includes(";") || pathname.includes("?") || pathname.includes("#")) {
|
|
2900
|
-
throw new Error(
|
|
2901
|
-
"i18n.cookie.path must be a root-relative pathname without attributes, a query, or a hash."
|
|
2902
|
-
);
|
|
2903
|
-
}
|
|
2904
|
-
for (const segment of pathname.split("/")) {
|
|
2905
|
-
let decoded = segment;
|
|
2906
|
-
try {
|
|
2907
|
-
decoded = decodeURIComponent(segment);
|
|
2908
|
-
} catch {
|
|
2909
|
-
}
|
|
2910
|
-
if (hasUnstableCharacters(decoded)) {
|
|
2911
|
-
throw new Error("i18n.cookie.path cannot contain backslashes or control characters.");
|
|
2912
|
-
}
|
|
2913
|
-
if (decoded.includes("/")) {
|
|
2914
|
-
throw new Error("i18n.cookie.path cannot contain percent-encoded path separators.");
|
|
2915
|
-
}
|
|
2916
|
-
if (decoded === "." || decoded === "..") {
|
|
2917
|
-
throw new Error('i18n.cookie.path cannot contain "." or ".." path segments.');
|
|
2918
|
-
}
|
|
2919
|
-
}
|
|
2920
|
-
return pathname;
|
|
2921
|
-
}
|
|
2922
|
-
__name(normalizeCookiePath, "normalizeCookiePath");
|
|
2923
50
|
|
|
2924
|
-
|
|
2925
|
-
|
|
2926
|
-
|
|
2927
|
-
|
|
2928
|
-
|
|
2929
|
-
|
|
2930
|
-
|
|
2931
|
-
|
|
2932
|
-
|
|
2933
|
-
|
|
2934
|
-
|
|
2935
|
-
|
|
2936
|
-
|
|
2937
|
-
|
|
2938
|
-
|
|
2939
|
-
mode: server.config.mode === "production" ? "production" : "development",
|
|
2940
|
-
basePath: options.basePath ?? "/"
|
|
2941
|
-
});
|
|
2942
|
-
manager = new MiddlewareManager(
|
|
2943
|
-
getMiddlewareAppDirectory(server.config.root, srcDir),
|
|
2944
|
-
server,
|
|
2945
|
-
void 0,
|
|
2946
|
-
i18n
|
|
2947
|
-
);
|
|
2948
|
-
await manager.discover();
|
|
2949
|
-
discoveryComplete = true;
|
|
2950
|
-
}, "discover");
|
|
2951
|
-
return {
|
|
2952
|
-
name: "@farm.js/core:middleware",
|
|
2953
|
-
enforce: "pre",
|
|
2954
|
-
configureServer(server) {
|
|
2955
|
-
discoveryPromise = discover(server);
|
|
2956
|
-
void discoveryPromise.catch((error) => {
|
|
2957
|
-
console.error("[FARM] Middleware discovery error:", error);
|
|
2958
|
-
});
|
|
2959
|
-
const execute = /* @__PURE__ */ __name(async (req, res, _pathname, sharedData) => {
|
|
2960
|
-
if (discoveryPromise) {
|
|
2961
|
-
await discoveryPromise;
|
|
2962
|
-
}
|
|
2963
|
-
if (!manager) {
|
|
2964
|
-
return false;
|
|
2965
|
-
}
|
|
2966
|
-
const handled = await manager.execute(req, res);
|
|
2967
|
-
const middlewareData = req.__FARM_MIDDLEWARE_DATA__;
|
|
2968
|
-
if (sharedData && middlewareData instanceof Map) {
|
|
2969
|
-
for (const [key, value] of middlewareData) {
|
|
2970
|
-
sharedData.set(key, value);
|
|
2971
|
-
}
|
|
2972
|
-
}
|
|
2973
|
-
return handled;
|
|
2974
|
-
}, "execute");
|
|
2975
|
-
server.__farmMiddleware__ = {
|
|
2976
|
-
execute,
|
|
2977
|
-
getMiddlewares: /* @__PURE__ */ __name(() => manager?.getMiddlewares() ?? [], "getMiddlewares"),
|
|
2978
|
-
isReady: /* @__PURE__ */ __name(() => discoveryComplete, "isReady"),
|
|
2979
|
-
waitForDiscovery: /* @__PURE__ */ __name(() => discoveryPromise, "waitForDiscovery")
|
|
2980
|
-
};
|
|
2981
|
-
},
|
|
2982
|
-
async handleHotUpdate({ file, server, modules }) {
|
|
2983
|
-
if (!/^middleware\.[tj]sx?$/.test(import_node_path2.default.basename(file))) {
|
|
2984
|
-
return;
|
|
2985
|
-
}
|
|
2986
|
-
for (const module2 of modules) {
|
|
2987
|
-
server.moduleGraph.invalidateModule(module2);
|
|
2988
|
-
}
|
|
2989
|
-
await manager?.reload();
|
|
2990
|
-
server.ws.send({ type: "full-reload", path: "*" });
|
|
2991
|
-
return [];
|
|
2992
|
-
}
|
|
2993
|
-
};
|
|
2994
|
-
}
|
|
2995
|
-
__name(farmMiddlewarePlugin, "farmMiddlewarePlugin");
|
|
2996
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
2997
|
-
0 && (module.exports = {
|
|
2998
|
-
MiddlewareManager,
|
|
2999
|
-
UnsupportedRateLimitStorageError,
|
|
3000
|
-
_runWithMiddlewareContext,
|
|
3001
|
-
_runWithMiddlewareData,
|
|
3002
|
-
applyProductionMiddlewareHeaders,
|
|
3003
|
-
canonicalizeRequestPathSegments,
|
|
3004
|
-
createContext,
|
|
3005
|
-
createProductionMiddlewareRunner,
|
|
3006
|
-
farmMiddlewarePlugin,
|
|
3007
|
-
getFromMiddleware,
|
|
3008
|
-
getMiddlewareContext,
|
|
3009
|
-
getMiddlewareData,
|
|
3010
|
-
getMiddlewareValue,
|
|
3011
|
-
getRateLimitStatus,
|
|
3012
|
-
hasMiddlewareData,
|
|
3013
|
-
matchesMiddlewareConfig,
|
|
3014
|
-
memoryRateLimitStorage,
|
|
3015
|
-
middleware,
|
|
3016
|
-
unwrapMiddleware
|
|
3017
|
-
});
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
exports.MiddlewareManager = _chunkY53ZSFN6cjs.MiddlewareManager; exports.UnsupportedRateLimitStorageError = _chunkFJSWYIR7cjs.UnsupportedRateLimitStorageError; exports._runWithMiddlewareContext = _chunkXGC343EYcjs._runWithMiddlewareContext; exports._runWithMiddlewareData = _chunkXGC343EYcjs._runWithMiddlewareData; exports.applyProductionMiddlewareHeaders = _chunkBYODCJD6cjs.applyProductionMiddlewareHeaders; exports.canonicalizeRequestPathSegments = _chunkUS2QVW3Ecjs.canonicalizeRequestPathSegments; exports.createContext = _chunkY53ZSFN6cjs.createContext; exports.createProductionMiddlewareRunner = _chunkBYODCJD6cjs.createProductionMiddlewareRunner; exports.farmMiddlewarePlugin = _chunkFJSWYIR7cjs.farmMiddlewarePlugin; exports.getFromMiddleware = _chunkFJSWYIR7cjs.getFromMiddleware; exports.getMiddlewareContext = _chunkXGC343EYcjs.getMiddlewareContext; exports.getMiddlewareData = _chunkXGC343EYcjs.getMiddlewareData; exports.getMiddlewareValue = _chunkXGC343EYcjs.getMiddlewareValue; exports.getRateLimitStatus = _chunkFJSWYIR7cjs.getRateLimitStatus; exports.hasMiddlewareData = _chunkFJSWYIR7cjs.hasMiddlewareData; exports.matchesMiddlewareConfig = _chunkBYODCJD6cjs.matchesMiddlewareConfig; exports.memoryRateLimitStorage = _chunkFJSWYIR7cjs.memoryRateLimitStorage; exports.middleware = _chunkFJSWYIR7cjs.middleware; exports.unwrapMiddleware = _chunkFJSWYIR7cjs.unwrapMiddleware;
|