@farm.js/core 0.1.0-beta.6 → 0.1.0-beta.8
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 +227 -0
- package/dist/after.cjs.map +1 -0
- package/dist/after.d.mts +25 -0
- package/dist/after.d.ts +25 -0
- package/dist/after.mjs +14 -0
- package/dist/after.mjs.map +1 -0
- package/dist/agent-runtime.cjs +2379 -0
- package/dist/agent-runtime.cjs.map +1 -0
- package/dist/agent-runtime.d.mts +94 -0
- package/dist/agent-runtime.d.ts +94 -0
- package/dist/agent-runtime.mjs +479 -0
- package/dist/agent-runtime.mjs.map +1 -0
- package/dist/api/route-manager.d.mts +66 -0
- package/dist/api/route-manager.d.ts +66 -0
- package/dist/api/runtime.d.mts +3 -0
- package/dist/api/runtime.d.ts +3 -0
- package/dist/api.cjs +4100 -0
- package/dist/api.cjs.map +1 -0
- package/dist/api.d.mts +48 -0
- package/dist/api.d.ts +48 -0
- package/dist/api.mjs +93 -0
- package/dist/api.mjs.map +1 -0
- package/dist/app-BWtqQu3R.d.mts +302 -0
- package/dist/app-markdown.cjs +244 -0
- package/dist/app-markdown.cjs.map +1 -0
- package/dist/app-markdown.d.mts +22 -0
- package/dist/app-markdown.d.ts +22 -0
- package/dist/app-markdown.mjs +27 -0
- package/dist/app-markdown.mjs.map +1 -0
- package/dist/app-xltejvLY.d.ts +302 -0
- package/dist/cache.cjs +945 -0
- package/dist/cache.cjs.map +1 -0
- package/dist/cache.d.mts +192 -0
- package/dist/cache.d.ts +192 -0
- package/dist/cache.mjs +46 -0
- package/dist/cache.mjs.map +1 -0
- package/dist/chunk-2ED5Q2GH.mjs +422 -0
- package/dist/chunk-2ED5Q2GH.mjs.map +1 -0
- package/dist/chunk-32W4QEJP.mjs +158 -0
- package/dist/chunk-32W4QEJP.mjs.map +1 -0
- package/dist/chunk-3XNTL3FQ.mjs +452 -0
- package/dist/chunk-3XNTL3FQ.mjs.map +1 -0
- package/dist/chunk-44ZCFSKE.mjs +84 -0
- package/dist/chunk-44ZCFSKE.mjs.map +1 -0
- package/dist/chunk-4MGVSBRP.mjs +7787 -0
- package/dist/chunk-4MGVSBRP.mjs.map +1 -0
- package/dist/chunk-4S3ORGFA.mjs +44 -0
- package/dist/chunk-4S3ORGFA.mjs.map +1 -0
- package/dist/chunk-5BY76U2M.mjs +1 -0
- package/dist/chunk-5BY76U2M.mjs.map +1 -0
- package/dist/chunk-5ZGPRGWV.mjs +136 -0
- package/dist/chunk-5ZGPRGWV.mjs.map +1 -0
- package/dist/chunk-65A7QIKI.mjs +142 -0
- package/dist/chunk-65A7QIKI.mjs.map +1 -0
- package/dist/chunk-74YTBPTY.mjs +450 -0
- package/dist/chunk-74YTBPTY.mjs.map +1 -0
- package/dist/chunk-7FBJLMQH.mjs +338 -0
- package/dist/chunk-7FBJLMQH.mjs.map +1 -0
- package/dist/chunk-7HO6AEOP.mjs +74 -0
- package/dist/chunk-7HO6AEOP.mjs.map +1 -0
- package/dist/chunk-7MABZJIM.mjs +29 -0
- package/dist/chunk-7MABZJIM.mjs.map +1 -0
- package/dist/chunk-7N62DNSO.mjs +41 -0
- package/dist/chunk-7N62DNSO.mjs.map +1 -0
- package/dist/chunk-7ZY5AIJO.mjs +161 -0
- package/dist/chunk-7ZY5AIJO.mjs.map +1 -0
- package/dist/chunk-ALCLXXTY.mjs +187 -0
- package/dist/chunk-ALCLXXTY.mjs.map +1 -0
- package/dist/chunk-AZ7YUVNR.mjs +65 -0
- package/dist/chunk-AZ7YUVNR.mjs.map +1 -0
- package/dist/chunk-B232I2GP.mjs +163 -0
- package/dist/chunk-B232I2GP.mjs.map +1 -0
- package/dist/chunk-B4W3BNTL.mjs +483 -0
- package/dist/chunk-B4W3BNTL.mjs.map +1 -0
- package/dist/chunk-C2ROET2V.mjs +718 -0
- package/dist/chunk-C2ROET2V.mjs.map +1 -0
- package/dist/chunk-CGILA3WO.mjs +14 -0
- package/dist/chunk-CGILA3WO.mjs.map +1 -0
- package/dist/chunk-CGIXMLSY.mjs +184 -0
- package/dist/chunk-CGIXMLSY.mjs.map +1 -0
- package/dist/chunk-DJSGLHCN.mjs +597 -0
- package/dist/chunk-DJSGLHCN.mjs.map +1 -0
- package/dist/chunk-E2MPIBRE.mjs +149 -0
- package/dist/chunk-E2MPIBRE.mjs.map +1 -0
- package/dist/chunk-E6EPN5BT.mjs +203 -0
- package/dist/chunk-E6EPN5BT.mjs.map +1 -0
- package/dist/chunk-EI43C4RA.mjs +6277 -0
- package/dist/chunk-EI43C4RA.mjs.map +1 -0
- package/dist/chunk-EJ7T4TPB.mjs +622 -0
- package/dist/chunk-EJ7T4TPB.mjs.map +1 -0
- package/dist/chunk-FQYDHKKU.mjs +128 -0
- package/dist/chunk-FQYDHKKU.mjs.map +1 -0
- package/dist/chunk-FRPWCMMG.mjs +299 -0
- package/dist/chunk-FRPWCMMG.mjs.map +1 -0
- package/dist/chunk-GN72QAIH.mjs +146 -0
- package/dist/chunk-GN72QAIH.mjs.map +1 -0
- package/dist/chunk-GQVGIUSN.mjs +149 -0
- package/dist/chunk-GQVGIUSN.mjs.map +1 -0
- package/dist/chunk-GS6K5OCM.mjs +407 -0
- package/dist/chunk-GS6K5OCM.mjs.map +1 -0
- package/dist/chunk-GXASWWJU.mjs +364 -0
- package/dist/chunk-GXASWWJU.mjs.map +1 -0
- package/dist/chunk-GY2STC2S.mjs +180 -0
- package/dist/chunk-GY2STC2S.mjs.map +1 -0
- package/dist/chunk-H2FA2SBS.mjs +51 -0
- package/dist/chunk-H2FA2SBS.mjs.map +1 -0
- package/dist/chunk-I4CUBGVF.mjs +93 -0
- package/dist/chunk-I4CUBGVF.mjs.map +1 -0
- package/dist/chunk-ITN462YM.mjs +98 -0
- package/dist/chunk-ITN462YM.mjs.map +1 -0
- package/dist/chunk-J23HNLFY.mjs +72 -0
- package/dist/chunk-J23HNLFY.mjs.map +1 -0
- package/dist/chunk-J4FNJ5IU.mjs +179 -0
- package/dist/chunk-J4FNJ5IU.mjs.map +1 -0
- package/dist/chunk-J5YWQVI5.mjs +211 -0
- package/dist/chunk-J5YWQVI5.mjs.map +1 -0
- package/dist/chunk-JDEOUK5V.mjs +427 -0
- package/dist/chunk-JDEOUK5V.mjs.map +1 -0
- package/dist/chunk-JK25YEN6.mjs +219 -0
- package/dist/chunk-JK25YEN6.mjs.map +1 -0
- package/dist/chunk-K4667E2Q.mjs +1916 -0
- package/dist/chunk-K4667E2Q.mjs.map +1 -0
- package/dist/chunk-LGHEOHBU.mjs +218 -0
- package/dist/chunk-LGHEOHBU.mjs.map +1 -0
- package/dist/chunk-LM2YNO4U.mjs +24 -0
- package/dist/chunk-LM2YNO4U.mjs.map +1 -0
- package/dist/chunk-ME4MO2RJ.mjs +3540 -0
- package/dist/chunk-ME4MO2RJ.mjs.map +1 -0
- package/dist/chunk-MIUW47JX.mjs +639 -0
- package/dist/chunk-MIUW47JX.mjs.map +1 -0
- package/dist/chunk-NNLCAVB6.mjs +391 -0
- package/dist/chunk-NNLCAVB6.mjs.map +1 -0
- package/dist/chunk-P7QDS5AT.mjs +191 -0
- package/dist/chunk-P7QDS5AT.mjs.map +1 -0
- package/dist/chunk-PFE6GMNE.mjs +603 -0
- package/dist/chunk-PFE6GMNE.mjs.map +1 -0
- package/dist/chunk-QGOHVJ6J.mjs +63 -0
- package/dist/chunk-QGOHVJ6J.mjs.map +1 -0
- package/dist/chunk-QKVUNGAH.mjs +39 -0
- package/dist/chunk-QKVUNGAH.mjs.map +1 -0
- package/dist/chunk-QM746KUO.mjs +56 -0
- package/dist/chunk-QM746KUO.mjs.map +1 -0
- package/dist/chunk-QTZZ2ISQ.mjs +753 -0
- package/dist/chunk-QTZZ2ISQ.mjs.map +1 -0
- package/dist/chunk-RBRLLZL5.mjs +81 -0
- package/dist/chunk-RBRLLZL5.mjs.map +1 -0
- package/dist/chunk-RHIFZZUI.mjs +69 -0
- package/dist/chunk-RHIFZZUI.mjs.map +1 -0
- package/dist/chunk-RQ633A5I.mjs +187 -0
- package/dist/chunk-RQ633A5I.mjs.map +1 -0
- package/dist/chunk-SCFX4UTD.mjs +28 -0
- package/dist/chunk-SCFX4UTD.mjs.map +1 -0
- package/dist/chunk-SL6HTIOK.mjs +66 -0
- package/dist/chunk-SL6HTIOK.mjs.map +1 -0
- package/dist/chunk-SPBBQLXI.mjs +207 -0
- package/dist/chunk-SPBBQLXI.mjs.map +1 -0
- package/dist/chunk-SSA7THPX.mjs +10 -0
- package/dist/chunk-SSA7THPX.mjs.map +1 -0
- package/dist/chunk-T4QJQK76.mjs +459 -0
- package/dist/chunk-T4QJQK76.mjs.map +1 -0
- package/dist/chunk-TQAEEJB4.mjs +157 -0
- package/dist/chunk-TQAEEJB4.mjs.map +1 -0
- package/dist/chunk-UB5UOSHB.mjs +458 -0
- package/dist/chunk-UB5UOSHB.mjs.map +1 -0
- package/dist/chunk-USWZZUEH.mjs +25 -0
- package/dist/chunk-USWZZUEH.mjs.map +1 -0
- package/dist/chunk-UVCIEUJC.mjs +16 -0
- package/dist/chunk-UVCIEUJC.mjs.map +1 -0
- package/dist/chunk-UXUAWNO5.mjs +396 -0
- package/dist/chunk-UXUAWNO5.mjs.map +1 -0
- package/dist/chunk-V3XURYJO.mjs +756 -0
- package/dist/chunk-V3XURYJO.mjs.map +1 -0
- package/dist/chunk-V6WE6JSZ.mjs +352 -0
- package/dist/chunk-V6WE6JSZ.mjs.map +1 -0
- package/dist/chunk-V7RBCSDJ.mjs +170 -0
- package/dist/chunk-V7RBCSDJ.mjs.map +1 -0
- package/dist/chunk-VCANOYFI.mjs +588 -0
- package/dist/chunk-VCANOYFI.mjs.map +1 -0
- package/dist/chunk-VY2UYN5R.mjs +224 -0
- package/dist/chunk-VY2UYN5R.mjs.map +1 -0
- package/dist/chunk-W2CRQFF6.mjs +1352 -0
- package/dist/chunk-W2CRQFF6.mjs.map +1 -0
- package/dist/chunk-WDU6QXNK.mjs +872 -0
- package/dist/chunk-WDU6QXNK.mjs.map +1 -0
- package/dist/chunk-WZ5QFROO.mjs +23 -0
- package/dist/chunk-WZ5QFROO.mjs.map +1 -0
- package/dist/chunk-XIVV5IWN.mjs +541 -0
- package/dist/chunk-XIVV5IWN.mjs.map +1 -0
- package/dist/chunk-YW7OBE6Q.mjs +119 -0
- package/dist/chunk-YW7OBE6Q.mjs.map +1 -0
- package/dist/chunk-Z5CQMP54.mjs +365 -0
- package/dist/chunk-Z5CQMP54.mjs.map +1 -0
- package/dist/chunk-ZBE6MLHL.mjs +230 -0
- package/dist/chunk-ZBE6MLHL.mjs.map +1 -0
- package/dist/chunk-ZVWA4ZQ3.mjs +189 -0
- package/dist/chunk-ZVWA4ZQ3.mjs.map +1 -0
- package/dist/chunk-ZZ57EH5Z.mjs +173 -0
- package/dist/chunk-ZZ57EH5Z.mjs.map +1 -0
- package/dist/client-BT1mFQas.d.mts +447 -0
- package/dist/client-Nn0q4eO4.d.ts +447 -0
- package/dist/client-cache-CIlyBlmx.d.ts +6 -0
- package/dist/client-cache-D-EZ7XVW.d.mts +6 -0
- package/dist/client-plugins-BI2R6Eae.d.mts +1926 -0
- package/dist/client-plugins-DU9oMspX.d.ts +1926 -0
- package/dist/client-plugins.cjs +415 -0
- package/dist/client-plugins.cjs.map +1 -0
- package/dist/client-plugins.d.mts +22 -0
- package/dist/client-plugins.d.ts +22 -0
- package/dist/client-plugins.mjs +10 -0
- package/dist/client-plugins.mjs.map +1 -0
- package/dist/client.cjs +5402 -0
- package/dist/client.cjs.map +1 -0
- package/dist/client.d.mts +447 -0
- package/dist/client.d.ts +447 -0
- package/dist/client.mjs +110 -0
- package/dist/client.mjs.map +1 -0
- package/dist/config.cjs +38 -0
- package/dist/config.cjs.map +1 -0
- package/dist/config.d.mts +29 -0
- package/dist/config.d.ts +29 -0
- package/dist/config.mjs +10 -0
- package/dist/config.mjs.map +1 -0
- package/dist/cron.cjs +406 -0
- package/dist/cron.cjs.map +1 -0
- package/dist/cron.d.mts +78 -0
- package/dist/cron.d.ts +78 -0
- package/dist/cron.mjs +30 -0
- package/dist/cron.mjs.map +1 -0
- package/dist/deferred.cjs +438 -0
- package/dist/deferred.cjs.map +1 -0
- package/dist/deferred.d.mts +42 -0
- package/dist/deferred.d.ts +42 -0
- package/dist/deferred.mjs +28 -0
- package/dist/deferred.mjs.map +1 -0
- package/dist/deployment.cjs +199 -0
- package/dist/deployment.cjs.map +1 -0
- package/dist/deployment.d.mts +34 -0
- package/dist/deployment.d.ts +34 -0
- package/dist/deployment.mjs +38 -0
- package/dist/deployment.mjs.map +1 -0
- package/dist/devtools-REQTS3I7.mjs +371 -0
- package/dist/devtools-REQTS3I7.mjs.map +1 -0
- package/dist/devtools-client-3XRTBND5.mjs +216 -0
- package/dist/devtools-client-3XRTBND5.mjs.map +1 -0
- package/dist/devtools-ui-NJKFORTG.mjs +1349 -0
- package/dist/devtools-ui-NJKFORTG.mjs.map +1 -0
- package/dist/docs.cjs +5548 -0
- package/dist/docs.cjs.map +1 -0
- package/dist/docs.d.mts +69 -0
- package/dist/docs.d.ts +69 -0
- package/dist/docs.mjs +47 -0
- package/dist/docs.mjs.map +1 -0
- package/dist/env-types.cjs +232 -0
- package/dist/env-types.cjs.map +1 -0
- package/dist/env-types.d.mts +12 -0
- package/dist/env-types.d.ts +12 -0
- package/dist/env-types.mjs +11 -0
- package/dist/env-types.mjs.map +1 -0
- package/dist/env.cjs +215 -0
- package/dist/env.cjs.map +1 -0
- package/dist/env.d.mts +45 -0
- package/dist/env.d.ts +45 -0
- package/dist/env.mjs +22 -0
- package/dist/env.mjs.map +1 -0
- package/dist/environment/vite.d.mts +22 -0
- package/dist/environment/vite.d.ts +22 -0
- package/dist/environment.cjs +96 -0
- package/dist/environment.cjs.map +1 -0
- package/dist/environment.d.mts +18 -0
- package/dist/environment.d.ts +18 -0
- package/dist/environment.mjs +14 -0
- package/dist/environment.mjs.map +1 -0
- package/dist/headers.cjs +126 -0
- package/dist/headers.cjs.map +1 -0
- package/dist/headers.d.mts +22 -0
- package/dist/headers.d.ts +22 -0
- package/dist/headers.mjs +12 -0
- package/dist/headers.mjs.map +1 -0
- package/dist/i18n/client.d.mts +32 -0
- package/dist/i18n/client.d.ts +32 -0
- package/dist/i18n/index.d.mts +20 -0
- package/dist/i18n/index.d.ts +20 -0
- package/dist/i18n/server.d.mts +30 -0
- package/dist/i18n/server.d.ts +30 -0
- package/dist/image/server.d.mts +37 -0
- package/dist/image/server.d.ts +37 -0
- package/dist/image/sharp.d.mts +7 -0
- package/dist/image/sharp.d.ts +7 -0
- package/dist/image-config-Duob7wER.d.mts +50 -0
- package/dist/image-config-Duob7wER.d.ts +50 -0
- package/dist/image.cjs +209 -0
- package/dist/image.cjs.map +1 -0
- package/dist/image.d.mts +39 -0
- package/dist/image.d.ts +39 -0
- package/dist/image.mjs +165 -0
- package/dist/image.mjs.map +1 -0
- package/dist/index.cjs +42186 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.mts +454 -0
- package/dist/index.d.ts +454 -0
- package/dist/index.mjs +980 -0
- package/dist/index.mjs.map +1 -0
- package/dist/integration-api-CHT2OKAf.d.mts +93 -0
- package/dist/integration-api-CHT2OKAf.d.ts +93 -0
- package/dist/integration-orm-GQLGU66X.mjs +13 -0
- package/dist/integration-orm-GQLGU66X.mjs.map +1 -0
- package/dist/integrations.cjs +2820 -0
- package/dist/integrations.cjs.map +1 -0
- package/dist/integrations.d.mts +22 -0
- package/dist/integrations.d.ts +22 -0
- package/dist/integrations.mjs +51 -0
- package/dist/integrations.mjs.map +1 -0
- package/dist/internal/build-runtime.cjs +643 -322
- package/dist/internal/build-runtime.cjs.map +1 -1
- package/dist/internal/build-runtime.d.mts +39 -0
- package/dist/internal/build-runtime.d.ts +39 -0
- package/dist/internal/build-runtime.mjs +11 -10
- package/dist/internal/client-runtime.d.mts +2 -0
- package/dist/internal/client-runtime.d.ts +2 -0
- package/dist/internal/config-runtime.cjs +135 -39
- package/dist/internal/config-runtime.cjs.map +1 -1
- package/dist/internal/config-runtime.d.mts +39 -0
- package/dist/internal/config-runtime.d.ts +39 -0
- package/dist/internal/config-runtime.mjs +5 -4
- package/dist/internal/production-node-env.d.mts +12 -0
- package/dist/internal/production-node-env.d.ts +12 -0
- package/dist/internal/production-runtime.d.mts +39 -0
- package/dist/internal/production-runtime.d.ts +39 -0
- package/dist/layers-VWVDTPUZ.mjs +16 -0
- package/dist/layers-VWVDTPUZ.mjs.map +1 -0
- package/dist/manager-THKFTOFE.mjs +10 -0
- package/dist/manager-THKFTOFE.mjs.map +1 -0
- package/dist/markdown.cjs +364 -0
- package/dist/markdown.cjs.map +1 -0
- package/dist/markdown.d.mts +60 -0
- package/dist/markdown.d.ts +60 -0
- package/dist/markdown.mjs +18 -0
- package/dist/markdown.mjs.map +1 -0
- package/dist/metadata-Cm9ujH4I.d.ts +22 -0
- package/dist/metadata-DacXbnLA.d.mts +22 -0
- package/dist/middleware.cjs +2148 -0
- package/dist/middleware.cjs.map +1 -0
- package/dist/middleware.d.mts +164 -0
- package/dist/middleware.d.ts +164 -0
- package/dist/middleware.mjs +49 -0
- package/dist/middleware.mjs.map +1 -0
- package/dist/navigation-errors-Dvwpzaer.d.mts +13 -0
- package/dist/navigation-errors-Dvwpzaer.d.ts +13 -0
- package/dist/navigation.cjs +1329 -0
- package/dist/navigation.cjs.map +1 -0
- package/dist/navigation.d.mts +14 -0
- package/dist/navigation.d.ts +14 -0
- package/dist/navigation.mjs +33 -0
- package/dist/navigation.mjs.map +1 -0
- package/dist/nitro-QO5LV6QK.mjs +689 -0
- package/dist/nitro-QO5LV6QK.mjs.map +1 -0
- package/dist/observability.cjs +186 -0
- package/dist/observability.cjs.map +1 -0
- package/dist/observability.d.mts +335 -0
- package/dist/observability.d.ts +335 -0
- package/dist/observability.mjs +16 -0
- package/dist/observability.mjs.map +1 -0
- package/dist/params-BALtrRim.d.ts +15 -0
- package/dist/params-ry-lxqmy.d.mts +15 -0
- package/dist/plugin-BJDf43dc.d.mts +165 -0
- package/dist/plugin-BJDf43dc.d.ts +165 -0
- package/dist/plugin.cjs +23470 -0
- package/dist/plugin.cjs.map +1 -1
- package/dist/plugin.d.mts +22 -0
- package/dist/plugin.d.ts +22 -0
- package/dist/plugin.mjs +8 -7
- package/dist/production-runtime-4KKq1AVC.d.mts +79 -0
- package/dist/production-runtime-C_B79w2P.d.mts +28 -0
- package/dist/production-runtime-DDEM1D2q.d.ts +28 -0
- package/dist/production-runtime-DRsWDN2x.d.ts +79 -0
- package/dist/query/client.d.mts +61 -0
- package/dist/query/client.d.ts +61 -0
- package/dist/query/index.d.mts +90 -0
- package/dist/query/index.d.ts +90 -0
- package/dist/query/parsers.d.mts +25 -0
- package/dist/query/parsers.d.ts +25 -0
- package/dist/query/server.d.mts +55 -0
- package/dist/query/server.d.ts +55 -0
- package/dist/request-Dsq4kGW0.d.mts +6 -0
- package/dist/request-udtobOLv.d.ts +6 -0
- package/dist/request.d.mts +2 -0
- package/dist/request.d.ts +2 -0
- package/dist/resolver-DJqjLMg_.d.mts +14 -0
- package/dist/resolver-IH4RmPEl.d.ts +14 -0
- package/dist/route-runtime-DoOHzCAD.d.mts +38 -0
- package/dist/route-runtime-DoOHzCAD.d.ts +38 -0
- package/dist/router.d.mts +36 -0
- package/dist/router.d.ts +36 -0
- package/dist/routes.d.mts +224 -0
- package/dist/routes.d.ts +224 -0
- package/dist/runtime-9nQjVM7X.d.mts +206 -0
- package/dist/runtime-Cxpq6xZN.d.mts +27 -0
- package/dist/runtime-DgeS2skB.d.ts +206 -0
- package/dist/runtime-bv63eUiO.d.ts +27 -0
- package/dist/server-action-security.d.mts +43 -0
- package/dist/server-action-security.d.ts +43 -0
- package/dist/server-fn-client.d.mts +32 -0
- package/dist/server-fn-client.d.ts +32 -0
- package/dist/server-fn.d.mts +93 -0
- package/dist/server-fn.d.ts +93 -0
- package/dist/server-plugins.d.mts +55 -0
- package/dist/server-plugins.d.ts +55 -0
- package/dist/server-query-client.d.mts +50 -0
- package/dist/server-query-client.d.ts +50 -0
- package/dist/server-query.d.mts +36 -0
- package/dist/server-query.d.ts +36 -0
- package/dist/server.cjs +731 -447
- package/dist/server.cjs.map +1 -1
- package/dist/server.d.mts +48 -0
- package/dist/server.d.ts +48 -0
- package/dist/server.mjs +9 -8
- package/dist/server.mjs.map +1 -1
- package/dist/storage.d.mts +52 -0
- package/dist/storage.d.ts +52 -0
- package/dist/testing.d.mts +101 -0
- package/dist/testing.d.ts +101 -0
- package/dist/transport-BmbGTXA5.d.mts +49 -0
- package/dist/transport-BmbGTXA5.d.ts +49 -0
- package/dist/types-BMxWr97v.d.mts +81 -0
- package/dist/types-BMxWr97v.d.ts +81 -0
- package/dist/types-C9N-nk5R.d.mts +53 -0
- package/dist/types-C9N-nk5R.d.ts +53 -0
- package/dist/types-D1A6-XvH.d.mts +73 -0
- package/dist/types-D1A6-XvH.d.ts +73 -0
- package/dist/utils-B-cUHwoK.d.ts +23 -0
- package/dist/utils-Cm41AefV.d.mts +23 -0
- package/dist/version.cjs +1 -1
- package/dist/version.cjs.map +1 -1
- package/dist/version.d.mts +3 -0
- package/dist/version.d.ts +3 -0
- package/dist/version.mjs +1 -1
- package/dist/vite.cjs +682 -437
- package/dist/vite.cjs.map +1 -1
- package/dist/vite.d.mts +22 -0
- package/dist/vite.d.ts +22 -0
- package/dist/vite.mjs +6 -5
- package/dist/workflows.d.mts +110 -0
- package/dist/workflows.d.ts +110 -0
- package/package.json +3 -2
- package/types/client.d.ts +1 -1
- package/types/image.d.ts +4 -0
- package/types/images.d.ts +59 -0
|
@@ -0,0 +1,2148 @@
|
|
|
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);
|
|
30
|
+
|
|
31
|
+
// src/middleware/index.ts
|
|
32
|
+
var middleware_exports = {};
|
|
33
|
+
__export(middleware_exports, {
|
|
34
|
+
MiddlewareManager: () => MiddlewareManager,
|
|
35
|
+
_runWithMiddlewareContext: () => _runWithMiddlewareContext,
|
|
36
|
+
_runWithMiddlewareData: () => _runWithMiddlewareData,
|
|
37
|
+
applyProductionMiddlewareHeaders: () => applyProductionMiddlewareHeaders,
|
|
38
|
+
createContext: () => createContext,
|
|
39
|
+
createProductionMiddlewareRunner: () => createProductionMiddlewareRunner,
|
|
40
|
+
farmMiddlewarePlugin: () => farmMiddlewarePlugin,
|
|
41
|
+
getFromMiddleware: () => getFromMiddleware,
|
|
42
|
+
getMiddlewareContext: () => getMiddlewareContext,
|
|
43
|
+
getMiddlewareData: () => getMiddlewareData,
|
|
44
|
+
getMiddlewareValue: () => getMiddlewareValue,
|
|
45
|
+
getRateLimitStatus: () => getRateLimitStatus,
|
|
46
|
+
hasMiddlewareData: () => hasMiddlewareData,
|
|
47
|
+
middleware: () => middleware,
|
|
48
|
+
unwrapMiddleware: () => unwrapMiddleware
|
|
49
|
+
});
|
|
50
|
+
module.exports = __toCommonJS(middleware_exports);
|
|
51
|
+
|
|
52
|
+
// src/storage/index.ts
|
|
53
|
+
var import_unstorage = require("unstorage");
|
|
54
|
+
var import_memory = __toESM(require("unstorage/drivers/memory"));
|
|
55
|
+
function createMemoryStorage() {
|
|
56
|
+
return (0, import_unstorage.createStorage)({
|
|
57
|
+
driver: (0, import_memory.default)()
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
__name(createMemoryStorage, "createMemoryStorage");
|
|
61
|
+
var GLOBAL_STORAGE_KEY = /* @__PURE__ */ Symbol.for("farmjs.storage.global");
|
|
62
|
+
function readGlobalStorage() {
|
|
63
|
+
return globalThis[GLOBAL_STORAGE_KEY];
|
|
64
|
+
}
|
|
65
|
+
__name(readGlobalStorage, "readGlobalStorage");
|
|
66
|
+
function writeGlobalStorage(storage) {
|
|
67
|
+
globalThis[GLOBAL_STORAGE_KEY] = storage;
|
|
68
|
+
return storage;
|
|
69
|
+
}
|
|
70
|
+
__name(writeGlobalStorage, "writeGlobalStorage");
|
|
71
|
+
function getActiveGlobalStorage() {
|
|
72
|
+
const existing = readGlobalStorage();
|
|
73
|
+
if (existing) {
|
|
74
|
+
return existing;
|
|
75
|
+
}
|
|
76
|
+
return writeGlobalStorage(createMemoryStorage());
|
|
77
|
+
}
|
|
78
|
+
__name(getActiveGlobalStorage, "getActiveGlobalStorage");
|
|
79
|
+
var globalStorage = getActiveGlobalStorage();
|
|
80
|
+
function normalizeMountKey(base) {
|
|
81
|
+
return (base || "").trim().replace(/^[:/\\]+|[:/\\]+$/g, "").replace(/[\\/]+/g, ":");
|
|
82
|
+
}
|
|
83
|
+
__name(normalizeMountKey, "normalizeMountKey");
|
|
84
|
+
function getStorage(namespace) {
|
|
85
|
+
globalStorage = getActiveGlobalStorage();
|
|
86
|
+
const base = normalizeMountKey(namespace);
|
|
87
|
+
if (!base) {
|
|
88
|
+
return globalStorage;
|
|
89
|
+
}
|
|
90
|
+
const namespaced = (0, import_unstorage.prefixStorage)(globalStorage, base);
|
|
91
|
+
return {
|
|
92
|
+
...namespaced,
|
|
93
|
+
async clear() {
|
|
94
|
+
const keys = await namespaced.getKeys();
|
|
95
|
+
await Promise.all(keys.map((key) => namespaced.removeItem(key)));
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
__name(getStorage, "getStorage");
|
|
100
|
+
|
|
101
|
+
// src/middleware/chain.ts
|
|
102
|
+
function getRateLimitStorage() {
|
|
103
|
+
return getStorage("ratelimit");
|
|
104
|
+
}
|
|
105
|
+
__name(getRateLimitStorage, "getRateLimitStorage");
|
|
106
|
+
var defaultRateLimitStorage = {
|
|
107
|
+
async get(key) {
|
|
108
|
+
const storage = getRateLimitStorage();
|
|
109
|
+
const value = await storage.getItem(key);
|
|
110
|
+
return value ?? null;
|
|
111
|
+
},
|
|
112
|
+
async set(key, value, ttl) {
|
|
113
|
+
const storage = getRateLimitStorage();
|
|
114
|
+
await storage.setItem(key, value, ttl ? { ttl } : void 0);
|
|
115
|
+
},
|
|
116
|
+
async delete(key) {
|
|
117
|
+
const storage = getRateLimitStorage();
|
|
118
|
+
await storage.removeItem(key);
|
|
119
|
+
},
|
|
120
|
+
async ttl(key) {
|
|
121
|
+
const storage = getRateLimitStorage();
|
|
122
|
+
const record = await storage.getItem(key);
|
|
123
|
+
if (!record || !record.resetAt) {
|
|
124
|
+
return null;
|
|
125
|
+
}
|
|
126
|
+
const remainingMs = record.resetAt - Date.now();
|
|
127
|
+
return remainingMs > 0 ? Math.ceil(remainingMs / 1e3) : null;
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
function parseTimeWindow(window) {
|
|
131
|
+
const match = window.match(/^(\d+)(ms|s|m|h|d)$/);
|
|
132
|
+
if (!match) throw new Error(`Invalid time window: ${window}`);
|
|
133
|
+
const [, amount, unit] = match;
|
|
134
|
+
const value = Number.parseInt(amount, 10);
|
|
135
|
+
const multipliers = {
|
|
136
|
+
ms: 1,
|
|
137
|
+
s: 1e3,
|
|
138
|
+
m: 60 * 1e3,
|
|
139
|
+
h: 60 * 60 * 1e3,
|
|
140
|
+
d: 24 * 60 * 60 * 1e3
|
|
141
|
+
};
|
|
142
|
+
return value * multipliers[unit];
|
|
143
|
+
}
|
|
144
|
+
__name(parseTimeWindow, "parseTimeWindow");
|
|
145
|
+
function createRateLimitMiddleware(config) {
|
|
146
|
+
const windowMs = parseTimeWindow(config.window);
|
|
147
|
+
const storage = config.storage || defaultRateLimitStorage;
|
|
148
|
+
return async (ctx, next) => {
|
|
149
|
+
const key = config.keyGenerator ? config.keyGenerator(ctx) : `${ctx.request.socket.remoteAddress}:${ctx.pathname}`;
|
|
150
|
+
const now = Date.now();
|
|
151
|
+
const ttlSeconds = Math.ceil(windowMs / 1e3);
|
|
152
|
+
const record = await storage.get(key);
|
|
153
|
+
if (record && now > record.resetAt) {
|
|
154
|
+
await storage.delete(key);
|
|
155
|
+
}
|
|
156
|
+
const current = await storage.get(key);
|
|
157
|
+
if (!current) {
|
|
158
|
+
await storage.set(
|
|
159
|
+
key,
|
|
160
|
+
{
|
|
161
|
+
count: 1,
|
|
162
|
+
resetAt: now + windowMs
|
|
163
|
+
},
|
|
164
|
+
ttlSeconds
|
|
165
|
+
);
|
|
166
|
+
return next();
|
|
167
|
+
}
|
|
168
|
+
if (current.count >= config.requests) {
|
|
169
|
+
if (config.onLimit) {
|
|
170
|
+
const result = await config.onLimit(ctx);
|
|
171
|
+
if (result) return;
|
|
172
|
+
}
|
|
173
|
+
ctx.json(
|
|
174
|
+
{
|
|
175
|
+
error: "Rate limit exceeded",
|
|
176
|
+
retryAfter: Math.ceil((current.resetAt - now) / 1e3)
|
|
177
|
+
},
|
|
178
|
+
429
|
|
179
|
+
);
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
current.count++;
|
|
183
|
+
await storage.set(key, current, ttlSeconds);
|
|
184
|
+
return next();
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
__name(createRateLimitMiddleware, "createRateLimitMiddleware");
|
|
188
|
+
function matchPattern(pattern, pathname) {
|
|
189
|
+
if (pattern instanceof RegExp) {
|
|
190
|
+
return pattern.test(pathname);
|
|
191
|
+
}
|
|
192
|
+
const regexPattern = pattern.replace(/\*\*/g, "__DOUBLE_STAR__").replace(/\*/g, "[^/]+").replace(/__DOUBLE_STAR__/g, ".*").replace(/\//g, "\\/");
|
|
193
|
+
const regex = new RegExp(`^${regexPattern}$`);
|
|
194
|
+
return regex.test(pathname);
|
|
195
|
+
}
|
|
196
|
+
__name(matchPattern, "matchPattern");
|
|
197
|
+
var _MiddlewareChainImpl = class _MiddlewareChainImpl {
|
|
198
|
+
constructor(basePath = "/") {
|
|
199
|
+
this.handlers = [];
|
|
200
|
+
this.basePath = basePath === "/" ? "/" : basePath.endsWith("/") ? basePath.slice(0, -1) : basePath;
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Set the base path for this middleware chain (called by manager when loading)
|
|
204
|
+
*/
|
|
205
|
+
setBasePath(path3) {
|
|
206
|
+
this.basePath = path3 === "/" ? "/" : path3.endsWith("/") ? path3.slice(0, -1) : path3;
|
|
207
|
+
}
|
|
208
|
+
use(fn) {
|
|
209
|
+
this.handlers.push(fn);
|
|
210
|
+
return this;
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Normalize a path pattern - auto-scope to middleware route
|
|
214
|
+
* If middleware is in /contact, then "/api" becomes "/contact/api"
|
|
215
|
+
*/
|
|
216
|
+
normalizePattern(pattern) {
|
|
217
|
+
if (this.basePath === "/") {
|
|
218
|
+
return pattern.startsWith("/") ? pattern : `/${pattern}`;
|
|
219
|
+
}
|
|
220
|
+
const cleanPattern = pattern.startsWith("/") ? pattern.slice(1) : pattern;
|
|
221
|
+
return `${this.basePath}/${cleanPattern}`;
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Conditionally run middleware based on a condition.
|
|
225
|
+
* Only supports boolean values or functions that evaluate to boolean.
|
|
226
|
+
* Route-based string matching has been removed - use function conditions instead.
|
|
227
|
+
*
|
|
228
|
+
* @example
|
|
229
|
+
* .when(true, (ctx, next) => { ... }) // Always run
|
|
230
|
+
* .when((ctx) => ctx.data.get('flag'), (ctx, next) => { ... }) // Conditional
|
|
231
|
+
* .when((ctx) => ctx.pathname === '/contact', (ctx, next) => { ... }) // Path-based condition
|
|
232
|
+
*/
|
|
233
|
+
when(condition, fn) {
|
|
234
|
+
const conditionalMiddleware = /* @__PURE__ */ __name(async (ctx, next) => {
|
|
235
|
+
const shouldRun = typeof condition === "function" ? condition(ctx) : condition;
|
|
236
|
+
if (!shouldRun) {
|
|
237
|
+
return next();
|
|
238
|
+
}
|
|
239
|
+
if (typeof fn === "function" && fn.length === 2) {
|
|
240
|
+
return fn(ctx, next);
|
|
241
|
+
} else {
|
|
242
|
+
const subChain = middleware(this.basePath);
|
|
243
|
+
fn(subChain);
|
|
244
|
+
const { handlers } = subChain.build();
|
|
245
|
+
let index = 0;
|
|
246
|
+
let returnedResponse;
|
|
247
|
+
const executeNext = /* @__PURE__ */ __name(async () => {
|
|
248
|
+
if (index < handlers.length) {
|
|
249
|
+
const handler = handlers[index++];
|
|
250
|
+
const result = await handler(ctx, executeNext);
|
|
251
|
+
if (result instanceof Response) {
|
|
252
|
+
returnedResponse = result;
|
|
253
|
+
return result;
|
|
254
|
+
}
|
|
255
|
+
} else {
|
|
256
|
+
const result = await next();
|
|
257
|
+
if (result instanceof Response) {
|
|
258
|
+
returnedResponse = result;
|
|
259
|
+
return result;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
return returnedResponse;
|
|
263
|
+
}, "executeNext");
|
|
264
|
+
return executeNext();
|
|
265
|
+
}
|
|
266
|
+
}, "conditionalMiddleware");
|
|
267
|
+
this.handlers.push(conditionalMiddleware);
|
|
268
|
+
return this;
|
|
269
|
+
}
|
|
270
|
+
rateLimit(config) {
|
|
271
|
+
this.handlers.push(createRateLimitMiddleware(config));
|
|
272
|
+
return this;
|
|
273
|
+
}
|
|
274
|
+
redirect(source, destination, permanent = false) {
|
|
275
|
+
const redirectMiddleware = /* @__PURE__ */ __name(async (ctx, next) => {
|
|
276
|
+
const normalizedSource = this.normalizePattern(source);
|
|
277
|
+
if (matchPattern(normalizedSource, ctx.pathname)) {
|
|
278
|
+
ctx.redirect(destination, permanent ? 308 : 307);
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
281
|
+
return next();
|
|
282
|
+
}, "redirectMiddleware");
|
|
283
|
+
this.handlers.push(redirectMiddleware);
|
|
284
|
+
return this;
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* Rewrite the current middleware route to a new destination.
|
|
288
|
+
* Since each middleware file is route-specific, the source is automatically
|
|
289
|
+
* the middleware's route, so you only need to specify the destination.
|
|
290
|
+
*
|
|
291
|
+
* @param destination - The destination path to rewrite to
|
|
292
|
+
* @param condition - Optional boolean or function that evaluates to boolean.
|
|
293
|
+
* If provided and evaluates to false, rewrite is skipped.
|
|
294
|
+
* If not provided, rewrite always happens.
|
|
295
|
+
*
|
|
296
|
+
* @example
|
|
297
|
+
* // In /contact/middleware.ts
|
|
298
|
+
* .rewrite('/about') // Always rewrites /contact to /about
|
|
299
|
+
* .rewrite('/about', true) // Always rewrites
|
|
300
|
+
* .rewrite('/about', false) // Never rewrites
|
|
301
|
+
* .rewrite('/about', (ctx) => ctx.data.get('shouldRewrite')) // Conditional rewrite
|
|
302
|
+
*/
|
|
303
|
+
rewrite(destination, condition) {
|
|
304
|
+
const rewriteMiddleware = /* @__PURE__ */ __name(async (ctx, next) => {
|
|
305
|
+
if (condition !== void 0) {
|
|
306
|
+
const shouldRewrite = typeof condition === "function" ? condition(ctx) : condition;
|
|
307
|
+
if (!shouldRewrite) {
|
|
308
|
+
return next();
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
if (this.basePath !== "/") {
|
|
312
|
+
if (ctx.pathname === this.basePath) {
|
|
313
|
+
ctx.rewrite(destination);
|
|
314
|
+
} else if (ctx.pathname.startsWith(this.basePath + "/")) {
|
|
315
|
+
const subPath = ctx.pathname.slice(this.basePath.length);
|
|
316
|
+
const newPath = destination + subPath;
|
|
317
|
+
ctx.rewrite(newPath);
|
|
318
|
+
}
|
|
319
|
+
} else {
|
|
320
|
+
ctx.rewrite(destination);
|
|
321
|
+
}
|
|
322
|
+
return next();
|
|
323
|
+
}, "rewriteMiddleware");
|
|
324
|
+
this.handlers.push(rewriteMiddleware);
|
|
325
|
+
return this;
|
|
326
|
+
}
|
|
327
|
+
build() {
|
|
328
|
+
return {
|
|
329
|
+
handlers: this.handlers,
|
|
330
|
+
config: this.config
|
|
331
|
+
};
|
|
332
|
+
}
|
|
333
|
+
};
|
|
334
|
+
__name(_MiddlewareChainImpl, "MiddlewareChainImpl");
|
|
335
|
+
var MiddlewareChainImpl = _MiddlewareChainImpl;
|
|
336
|
+
async function getRateLimitStatus(key, limit, storage = defaultRateLimitStorage) {
|
|
337
|
+
const record = await storage.get(key);
|
|
338
|
+
if (!record) {
|
|
339
|
+
return {
|
|
340
|
+
requests: 0,
|
|
341
|
+
limit,
|
|
342
|
+
remaining: limit,
|
|
343
|
+
resetIn: null,
|
|
344
|
+
resetAt: null,
|
|
345
|
+
isLimited: false
|
|
346
|
+
};
|
|
347
|
+
}
|
|
348
|
+
const now = Date.now();
|
|
349
|
+
if (record.resetAt && now > record.resetAt) {
|
|
350
|
+
return {
|
|
351
|
+
requests: 0,
|
|
352
|
+
limit,
|
|
353
|
+
remaining: limit,
|
|
354
|
+
resetIn: null,
|
|
355
|
+
resetAt: null,
|
|
356
|
+
isLimited: false
|
|
357
|
+
};
|
|
358
|
+
}
|
|
359
|
+
const requests = record.count || 0;
|
|
360
|
+
const remaining = Math.max(0, limit - requests);
|
|
361
|
+
const isLimited = requests >= limit;
|
|
362
|
+
let resetIn = null;
|
|
363
|
+
let resetAt = null;
|
|
364
|
+
if (record.resetAt) {
|
|
365
|
+
const remainingMs = record.resetAt - now;
|
|
366
|
+
resetIn = remainingMs > 0 ? Math.ceil(remainingMs / 1e3) : null;
|
|
367
|
+
resetAt = new Date(record.resetAt);
|
|
368
|
+
} else if (storage.ttl) {
|
|
369
|
+
resetIn = await storage.ttl(key);
|
|
370
|
+
resetAt = resetIn ? new Date(now + resetIn * 1e3) : null;
|
|
371
|
+
}
|
|
372
|
+
return {
|
|
373
|
+
requests,
|
|
374
|
+
limit,
|
|
375
|
+
remaining,
|
|
376
|
+
resetIn,
|
|
377
|
+
resetAt,
|
|
378
|
+
isLimited
|
|
379
|
+
};
|
|
380
|
+
}
|
|
381
|
+
__name(getRateLimitStatus, "getRateLimitStatus");
|
|
382
|
+
function middleware(basePath = "/") {
|
|
383
|
+
return new MiddlewareChainImpl(basePath);
|
|
384
|
+
}
|
|
385
|
+
__name(middleware, "middleware");
|
|
386
|
+
|
|
387
|
+
// src/middleware/context.ts
|
|
388
|
+
function parseCookies(cookieHeader) {
|
|
389
|
+
if (!cookieHeader) return {};
|
|
390
|
+
return cookieHeader.split(";").reduce(
|
|
391
|
+
(cookies, cookie) => {
|
|
392
|
+
const [name, ...rest] = cookie.split("=");
|
|
393
|
+
const value = rest.join("=").trim();
|
|
394
|
+
if (name && value) {
|
|
395
|
+
cookies[name.trim()] = decodeURIComponent(value);
|
|
396
|
+
}
|
|
397
|
+
return cookies;
|
|
398
|
+
},
|
|
399
|
+
{}
|
|
400
|
+
);
|
|
401
|
+
}
|
|
402
|
+
__name(parseCookies, "parseCookies");
|
|
403
|
+
function serializeCookie(name, value, options = {}) {
|
|
404
|
+
let cookie = `${encodeURIComponent(name)}=${encodeURIComponent(value)}`;
|
|
405
|
+
if (options.maxAge) {
|
|
406
|
+
cookie += `; Max-Age=${options.maxAge}`;
|
|
407
|
+
}
|
|
408
|
+
if (options.expires) {
|
|
409
|
+
cookie += `; Expires=${options.expires.toUTCString()}`;
|
|
410
|
+
}
|
|
411
|
+
if (options.path) {
|
|
412
|
+
cookie += `; Path=${options.path}`;
|
|
413
|
+
} else {
|
|
414
|
+
cookie += "; Path=/";
|
|
415
|
+
}
|
|
416
|
+
if (options.domain) {
|
|
417
|
+
cookie += `; Domain=${options.domain}`;
|
|
418
|
+
}
|
|
419
|
+
if (options.secure) {
|
|
420
|
+
cookie += "; Secure";
|
|
421
|
+
}
|
|
422
|
+
if (options.httpOnly) {
|
|
423
|
+
cookie += "; HttpOnly";
|
|
424
|
+
}
|
|
425
|
+
if (options.sameSite) {
|
|
426
|
+
cookie += `; SameSite=${options.sameSite.charAt(0).toUpperCase() + options.sameSite.slice(1)}`;
|
|
427
|
+
}
|
|
428
|
+
return cookie;
|
|
429
|
+
}
|
|
430
|
+
__name(serializeCookie, "serializeCookie");
|
|
431
|
+
var _CookieJarImpl = class _CookieJarImpl {
|
|
432
|
+
constructor(req, res) {
|
|
433
|
+
this.req = req;
|
|
434
|
+
this.res = res;
|
|
435
|
+
this.setCookies = [];
|
|
436
|
+
this.cookies = parseCookies(req.headers.cookie);
|
|
437
|
+
}
|
|
438
|
+
get(name) {
|
|
439
|
+
return this.cookies[name];
|
|
440
|
+
}
|
|
441
|
+
set(name, value, options = {}) {
|
|
442
|
+
this.cookies[name] = value;
|
|
443
|
+
const cookieString = serializeCookie(name, value, options);
|
|
444
|
+
this.setCookies.push(cookieString);
|
|
445
|
+
this.res.setHeader("Set-Cookie", this.setCookies);
|
|
446
|
+
}
|
|
447
|
+
delete(name) {
|
|
448
|
+
delete this.cookies[name];
|
|
449
|
+
const cookieString = serializeCookie(name, "", {
|
|
450
|
+
maxAge: 0,
|
|
451
|
+
expires: /* @__PURE__ */ new Date(0)
|
|
452
|
+
});
|
|
453
|
+
this.setCookies.push(cookieString);
|
|
454
|
+
this.res.setHeader("Set-Cookie", this.setCookies);
|
|
455
|
+
}
|
|
456
|
+
getAll() {
|
|
457
|
+
return { ...this.cookies };
|
|
458
|
+
}
|
|
459
|
+
};
|
|
460
|
+
__name(_CookieJarImpl, "CookieJarImpl");
|
|
461
|
+
var CookieJarImpl = _CookieJarImpl;
|
|
462
|
+
function createContext(req, res, viteServer, parent) {
|
|
463
|
+
const url = new URL(req.url || "/", `http://${req.headers.host || "localhost"}`);
|
|
464
|
+
const headers = /* @__PURE__ */ new Map();
|
|
465
|
+
const data = parent?.data ? new Map(parent.data) : /* @__PURE__ */ new Map();
|
|
466
|
+
const locals = parent?.locals ? new Map(parent.locals) : /* @__PURE__ */ new Map();
|
|
467
|
+
const cookies = new CookieJarImpl(req, res);
|
|
468
|
+
Object.entries(req.headers).forEach(([key, value]) => {
|
|
469
|
+
if (typeof value === "string") {
|
|
470
|
+
headers.set(key, value);
|
|
471
|
+
} else if (Array.isArray(value)) {
|
|
472
|
+
headers.set(key, value.join(", "));
|
|
473
|
+
}
|
|
474
|
+
});
|
|
475
|
+
if (parent?.headers) {
|
|
476
|
+
for (const [key, value] of Object.entries(parent.headers)) {
|
|
477
|
+
headers.set(key, value);
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
let handled = false;
|
|
481
|
+
const ctx = {
|
|
482
|
+
request: req,
|
|
483
|
+
response: res,
|
|
484
|
+
url,
|
|
485
|
+
pathname: url.pathname,
|
|
486
|
+
searchParams: url.searchParams,
|
|
487
|
+
method: req.method || "GET",
|
|
488
|
+
params: {},
|
|
489
|
+
route: url.pathname,
|
|
490
|
+
parent,
|
|
491
|
+
vite: {
|
|
492
|
+
isDev: process.env.NODE_ENV !== "production",
|
|
493
|
+
hmr: !!viteServer?.hot,
|
|
494
|
+
server: viteServer
|
|
495
|
+
},
|
|
496
|
+
data,
|
|
497
|
+
locals,
|
|
498
|
+
headers,
|
|
499
|
+
cookies,
|
|
500
|
+
_handled: false,
|
|
501
|
+
redirect(redirectUrl, status = 307) {
|
|
502
|
+
if (handled) {
|
|
503
|
+
console.warn("Response already sent, cannot redirect");
|
|
504
|
+
return;
|
|
505
|
+
}
|
|
506
|
+
ctx._redirectUrl = redirectUrl;
|
|
507
|
+
ctx._handled = true;
|
|
508
|
+
handled = true;
|
|
509
|
+
res.writeHead(status, {
|
|
510
|
+
Location: redirectUrl,
|
|
511
|
+
"Content-Type": "text/plain"
|
|
512
|
+
});
|
|
513
|
+
res.end(`Redirecting to ${redirectUrl}`);
|
|
514
|
+
},
|
|
515
|
+
rewrite(rewriteUrl) {
|
|
516
|
+
ctx._rewriteUrl = rewriteUrl;
|
|
517
|
+
const newUrl = new URL(rewriteUrl, `http://${req.headers.host || "localhost"}`);
|
|
518
|
+
ctx.url = newUrl;
|
|
519
|
+
ctx.pathname = newUrl.pathname;
|
|
520
|
+
ctx.searchParams = newUrl.searchParams;
|
|
521
|
+
req.url = rewriteUrl;
|
|
522
|
+
},
|
|
523
|
+
json(jsonData, status = 200) {
|
|
524
|
+
if (handled) {
|
|
525
|
+
console.warn("Response already sent, cannot send JSON");
|
|
526
|
+
return;
|
|
527
|
+
}
|
|
528
|
+
ctx._handled = true;
|
|
529
|
+
handled = true;
|
|
530
|
+
res.writeHead(status, {
|
|
531
|
+
"Content-Type": "application/json"
|
|
532
|
+
});
|
|
533
|
+
res.end(JSON.stringify(jsonData));
|
|
534
|
+
},
|
|
535
|
+
text(content, status = 200) {
|
|
536
|
+
if (handled) {
|
|
537
|
+
console.warn("Response already sent, cannot send text");
|
|
538
|
+
return;
|
|
539
|
+
}
|
|
540
|
+
ctx._handled = true;
|
|
541
|
+
handled = true;
|
|
542
|
+
res.writeHead(status, {
|
|
543
|
+
"Content-Type": "text/plain"
|
|
544
|
+
});
|
|
545
|
+
res.end(content);
|
|
546
|
+
},
|
|
547
|
+
html(content, status = 200) {
|
|
548
|
+
if (handled) {
|
|
549
|
+
console.warn("Response already sent, cannot send HTML");
|
|
550
|
+
return;
|
|
551
|
+
}
|
|
552
|
+
ctx._handled = true;
|
|
553
|
+
handled = true;
|
|
554
|
+
res.writeHead(status, {
|
|
555
|
+
"Content-Type": "text/html"
|
|
556
|
+
});
|
|
557
|
+
res.end(content);
|
|
558
|
+
}
|
|
559
|
+
};
|
|
560
|
+
return ctx;
|
|
561
|
+
}
|
|
562
|
+
__name(createContext, "createContext");
|
|
563
|
+
|
|
564
|
+
// src/middleware/manager.ts
|
|
565
|
+
var fs = __toESM(require("fs"));
|
|
566
|
+
var path = __toESM(require("path"));
|
|
567
|
+
|
|
568
|
+
// src/server/request.ts
|
|
569
|
+
var import_node_async_hooks = require("async_hooks");
|
|
570
|
+
|
|
571
|
+
// src/server/request-bridge.ts
|
|
572
|
+
var CURRENT_REQUEST_RESOLVER_KEY = /* @__PURE__ */ Symbol.for("farm.currentRequestResolver");
|
|
573
|
+
function getGlobalState() {
|
|
574
|
+
return globalThis;
|
|
575
|
+
}
|
|
576
|
+
__name(getGlobalState, "getGlobalState");
|
|
577
|
+
function _setCurrentRequestResolver(resolver) {
|
|
578
|
+
getGlobalState()[CURRENT_REQUEST_RESOLVER_KEY] = resolver;
|
|
579
|
+
}
|
|
580
|
+
__name(_setCurrentRequestResolver, "_setCurrentRequestResolver");
|
|
581
|
+
|
|
582
|
+
// src/server/request.ts
|
|
583
|
+
var REQUEST_STORAGE_KEY = /* @__PURE__ */ Symbol.for("@farm.js/core/request-storage");
|
|
584
|
+
function getRequestStore() {
|
|
585
|
+
const runtime = globalThis;
|
|
586
|
+
const existing = runtime[REQUEST_STORAGE_KEY];
|
|
587
|
+
if (existing instanceof import_node_async_hooks.AsyncLocalStorage) {
|
|
588
|
+
return existing;
|
|
589
|
+
}
|
|
590
|
+
const storage = new import_node_async_hooks.AsyncLocalStorage();
|
|
591
|
+
runtime[REQUEST_STORAGE_KEY] = storage;
|
|
592
|
+
return storage;
|
|
593
|
+
}
|
|
594
|
+
__name(getRequestStore, "getRequestStore");
|
|
595
|
+
var requestStore = getRequestStore();
|
|
596
|
+
_setCurrentRequestResolver(() => requestStore.getStore());
|
|
597
|
+
function createWebRequestFromFarmRequest(req) {
|
|
598
|
+
const forwardedHost = req.headers["x-forwarded-host"];
|
|
599
|
+
const host = Array.isArray(forwardedHost) ? forwardedHost[0] : forwardedHost || req.headers.host || "localhost";
|
|
600
|
+
const forwardedProto = req.headers["x-forwarded-proto"];
|
|
601
|
+
const proto = Array.isArray(forwardedProto) ? forwardedProto[0] : forwardedProto || "http";
|
|
602
|
+
const fullUrl = new URL(req.url || "/", `${proto}://${host}`).toString();
|
|
603
|
+
const headers = new Headers();
|
|
604
|
+
for (const [key, value] of Object.entries(req.headers)) {
|
|
605
|
+
if (value == null) {
|
|
606
|
+
continue;
|
|
607
|
+
}
|
|
608
|
+
if (Array.isArray(value)) {
|
|
609
|
+
for (const item of value) {
|
|
610
|
+
headers.append(key, item);
|
|
611
|
+
}
|
|
612
|
+
continue;
|
|
613
|
+
}
|
|
614
|
+
headers.set(key, value);
|
|
615
|
+
}
|
|
616
|
+
return new Request(fullUrl, {
|
|
617
|
+
method: req.method,
|
|
618
|
+
headers
|
|
619
|
+
});
|
|
620
|
+
}
|
|
621
|
+
__name(createWebRequestFromFarmRequest, "createWebRequestFromFarmRequest");
|
|
622
|
+
|
|
623
|
+
// src/middleware/module.ts
|
|
624
|
+
function isWebRequest(request) {
|
|
625
|
+
return typeof Request !== "undefined" && request instanceof Request;
|
|
626
|
+
}
|
|
627
|
+
__name(isWebRequest, "isWebRequest");
|
|
628
|
+
function toWebRequest(ctx) {
|
|
629
|
+
if (isWebRequest(ctx.request)) {
|
|
630
|
+
return ctx.request;
|
|
631
|
+
}
|
|
632
|
+
return createWebRequestFromFarmRequest(ctx.request);
|
|
633
|
+
}
|
|
634
|
+
__name(toWebRequest, "toWebRequest");
|
|
635
|
+
function createRequestMiddlewareContext(ctx) {
|
|
636
|
+
const context = {
|
|
637
|
+
get url() {
|
|
638
|
+
return ctx.url;
|
|
639
|
+
},
|
|
640
|
+
get pathname() {
|
|
641
|
+
return ctx.pathname;
|
|
642
|
+
},
|
|
643
|
+
get searchParams() {
|
|
644
|
+
return ctx.searchParams;
|
|
645
|
+
},
|
|
646
|
+
get method() {
|
|
647
|
+
return ctx.method;
|
|
648
|
+
},
|
|
649
|
+
get params() {
|
|
650
|
+
return ctx.params;
|
|
651
|
+
},
|
|
652
|
+
get route() {
|
|
653
|
+
return ctx.route;
|
|
654
|
+
},
|
|
655
|
+
get locals() {
|
|
656
|
+
return ctx.locals;
|
|
657
|
+
},
|
|
658
|
+
get data() {
|
|
659
|
+
return ctx.data;
|
|
660
|
+
},
|
|
661
|
+
get headers() {
|
|
662
|
+
return ctx.headers;
|
|
663
|
+
},
|
|
664
|
+
get cookies() {
|
|
665
|
+
return ctx.cookies;
|
|
666
|
+
},
|
|
667
|
+
get(key) {
|
|
668
|
+
return ctx.locals.get(key);
|
|
669
|
+
},
|
|
670
|
+
has(key) {
|
|
671
|
+
return ctx.locals.has(key);
|
|
672
|
+
},
|
|
673
|
+
set(key, value) {
|
|
674
|
+
ctx.locals.set(key, value);
|
|
675
|
+
},
|
|
676
|
+
delete(key) {
|
|
677
|
+
return ctx.locals.delete(key);
|
|
678
|
+
},
|
|
679
|
+
redirect(url, status) {
|
|
680
|
+
ctx.redirect(url, status);
|
|
681
|
+
},
|
|
682
|
+
rewrite(url) {
|
|
683
|
+
ctx.rewrite(url);
|
|
684
|
+
},
|
|
685
|
+
json(data, status) {
|
|
686
|
+
ctx.json(data, status);
|
|
687
|
+
},
|
|
688
|
+
text(content, status) {
|
|
689
|
+
ctx.text(content, status);
|
|
690
|
+
},
|
|
691
|
+
html(content, status) {
|
|
692
|
+
ctx.html(content, status);
|
|
693
|
+
}
|
|
694
|
+
};
|
|
695
|
+
return context;
|
|
696
|
+
}
|
|
697
|
+
__name(createRequestMiddlewareContext, "createRequestMiddlewareContext");
|
|
698
|
+
function normalizeMiddlewareModule(middlewareModule, routePath) {
|
|
699
|
+
const module2 = middlewareModule;
|
|
700
|
+
const defaultExport = module2.default;
|
|
701
|
+
const namedExport = module2.middleware;
|
|
702
|
+
const hasNamedExport = typeof namedExport === "function";
|
|
703
|
+
if (namedExport !== void 0 && !hasNamedExport) {
|
|
704
|
+
throw new TypeError("The named middleware export must be a function");
|
|
705
|
+
}
|
|
706
|
+
if (defaultExport && hasNamedExport) {
|
|
707
|
+
throw new Error(
|
|
708
|
+
"A middleware file cannot export both a default handler and a named middleware handler"
|
|
709
|
+
);
|
|
710
|
+
}
|
|
711
|
+
if (hasNamedExport) {
|
|
712
|
+
return {
|
|
713
|
+
handlers: [
|
|
714
|
+
async (ctx) => {
|
|
715
|
+
return namedExport(toWebRequest(ctx), createRequestMiddlewareContext(ctx));
|
|
716
|
+
}
|
|
717
|
+
],
|
|
718
|
+
config: module2.config
|
|
719
|
+
};
|
|
720
|
+
}
|
|
721
|
+
if (defaultExport && typeof defaultExport === "object" && "build" in defaultExport) {
|
|
722
|
+
if (typeof defaultExport.setBasePath === "function") {
|
|
723
|
+
defaultExport.setBasePath(routePath);
|
|
724
|
+
}
|
|
725
|
+
const built = defaultExport.build();
|
|
726
|
+
const handlers = Array.isArray(built?.handlers) ? built.handlers.filter((handler) => typeof handler === "function") : [];
|
|
727
|
+
return handlers.length > 0 ? {
|
|
728
|
+
handlers,
|
|
729
|
+
config: module2.config || built?.config
|
|
730
|
+
} : null;
|
|
731
|
+
}
|
|
732
|
+
if (typeof defaultExport === "function") {
|
|
733
|
+
return {
|
|
734
|
+
handlers: [defaultExport],
|
|
735
|
+
config: module2.config
|
|
736
|
+
};
|
|
737
|
+
}
|
|
738
|
+
return null;
|
|
739
|
+
}
|
|
740
|
+
__name(normalizeMiddlewareModule, "normalizeMiddlewareModule");
|
|
741
|
+
|
|
742
|
+
// src/utils.ts
|
|
743
|
+
var logger = {
|
|
744
|
+
info: /* @__PURE__ */ __name((message) => console.log(`[info] ${message}`), "info"),
|
|
745
|
+
success: /* @__PURE__ */ __name((message) => console.log(`[success] ${message}`), "success"),
|
|
746
|
+
warn: /* @__PURE__ */ __name((message) => console.warn(`\u26A0\uFE0F ${message}`), "warn"),
|
|
747
|
+
error: /* @__PURE__ */ __name((message) => console.error(`\u274C ${message}`), "error"),
|
|
748
|
+
ready: /* @__PURE__ */ __name((message) => console.log(`${message}`), "ready"),
|
|
749
|
+
event: /* @__PURE__ */ __name((message) => console.log(` ${message}`), "event")
|
|
750
|
+
};
|
|
751
|
+
|
|
752
|
+
// src/server/response.ts
|
|
753
|
+
var import_node_events = require("events");
|
|
754
|
+
async function sendWebResponse(res, response) {
|
|
755
|
+
res.statusCode = response.status;
|
|
756
|
+
const responseHeaders = response.headers;
|
|
757
|
+
const setCookies = responseHeaders.getSetCookie?.() || [];
|
|
758
|
+
if (setCookies.length > 0) {
|
|
759
|
+
res.setHeader("Set-Cookie", setCookies);
|
|
760
|
+
}
|
|
761
|
+
response.headers.forEach((value, key) => {
|
|
762
|
+
if (key.toLowerCase() === "set-cookie" && setCookies.length > 0) {
|
|
763
|
+
return;
|
|
764
|
+
}
|
|
765
|
+
res.setHeader(key, value);
|
|
766
|
+
});
|
|
767
|
+
if (!response.body) {
|
|
768
|
+
res.end();
|
|
769
|
+
return;
|
|
770
|
+
}
|
|
771
|
+
if (typeof res.write !== "function") {
|
|
772
|
+
const body = await response.arrayBuffer();
|
|
773
|
+
res.end(Buffer.from(body));
|
|
774
|
+
return;
|
|
775
|
+
}
|
|
776
|
+
const reader = response.body.getReader();
|
|
777
|
+
try {
|
|
778
|
+
while (true) {
|
|
779
|
+
const { done, value } = await reader.read();
|
|
780
|
+
if (done) {
|
|
781
|
+
break;
|
|
782
|
+
}
|
|
783
|
+
if (!value || value.byteLength === 0) {
|
|
784
|
+
continue;
|
|
785
|
+
}
|
|
786
|
+
if (!res.write(value)) {
|
|
787
|
+
await (0, import_node_events.once)(res, "drain");
|
|
788
|
+
}
|
|
789
|
+
}
|
|
790
|
+
res.end();
|
|
791
|
+
} catch (error) {
|
|
792
|
+
if (!res.writableEnded) {
|
|
793
|
+
const responseError = error instanceof Error ? error : new Error(String(error));
|
|
794
|
+
if (typeof res.destroy === "function") {
|
|
795
|
+
res.destroy(responseError);
|
|
796
|
+
} else {
|
|
797
|
+
res.end();
|
|
798
|
+
}
|
|
799
|
+
}
|
|
800
|
+
throw error;
|
|
801
|
+
} finally {
|
|
802
|
+
reader.releaseLock();
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
__name(sendWebResponse, "sendWebResponse");
|
|
806
|
+
|
|
807
|
+
// src/observability.ts
|
|
808
|
+
var runtimeHandlers = /* @__PURE__ */ new Set();
|
|
809
|
+
var observabilityState = {
|
|
810
|
+
logs: false,
|
|
811
|
+
handlers: []
|
|
812
|
+
};
|
|
813
|
+
function emitFarmEvent(input) {
|
|
814
|
+
const event = {
|
|
815
|
+
timestamp: Date.now(),
|
|
816
|
+
level: inferFarmEventLevel(input.type),
|
|
817
|
+
...input
|
|
818
|
+
};
|
|
819
|
+
if (!shouldEmitFarmEvent(event)) {
|
|
820
|
+
return event;
|
|
821
|
+
}
|
|
822
|
+
if (observabilityState.logs) {
|
|
823
|
+
logFarmEvent(event);
|
|
824
|
+
}
|
|
825
|
+
for (const handler of [...observabilityState.handlers, ...runtimeHandlers]) {
|
|
826
|
+
try {
|
|
827
|
+
Promise.resolve(handler(event)).catch((error) => {
|
|
828
|
+
console.warn(`[farm:observability] event handler failed: ${formatError(error)}`);
|
|
829
|
+
});
|
|
830
|
+
} catch (error) {
|
|
831
|
+
console.warn(`[farm:observability] event handler failed: ${formatError(error)}`);
|
|
832
|
+
}
|
|
833
|
+
}
|
|
834
|
+
return event;
|
|
835
|
+
}
|
|
836
|
+
__name(emitFarmEvent, "emitFarmEvent");
|
|
837
|
+
function shouldEmitFarmEvent(event) {
|
|
838
|
+
if (!observabilityState.logs && observabilityState.handlers.length === 0 && runtimeHandlers.size === 0) {
|
|
839
|
+
return false;
|
|
840
|
+
}
|
|
841
|
+
if (observabilityState.events && !observabilityState.events.has(event.type)) {
|
|
842
|
+
return false;
|
|
843
|
+
}
|
|
844
|
+
return true;
|
|
845
|
+
}
|
|
846
|
+
__name(shouldEmitFarmEvent, "shouldEmitFarmEvent");
|
|
847
|
+
function inferFarmEventLevel(type) {
|
|
848
|
+
if (type === "error" || type.endsWith(".error") || type.endsWith(".failed")) {
|
|
849
|
+
return "error";
|
|
850
|
+
}
|
|
851
|
+
if (type.endsWith(".bypass") || type.endsWith(".invalidated") || type.endsWith(".stale") || type.endsWith(".notFound")) {
|
|
852
|
+
return "warn";
|
|
853
|
+
}
|
|
854
|
+
if (type.endsWith(".hit") || type.endsWith(".miss") || type.endsWith(".start") || type.endsWith(".shellReady")) {
|
|
855
|
+
return "debug";
|
|
856
|
+
}
|
|
857
|
+
return "info";
|
|
858
|
+
}
|
|
859
|
+
__name(inferFarmEventLevel, "inferFarmEventLevel");
|
|
860
|
+
function logFarmEvent(event) {
|
|
861
|
+
const message = `[farm:${event.level}] ${event.type}${formatFarmEventDetails(event)}`;
|
|
862
|
+
switch (event.level) {
|
|
863
|
+
case "error":
|
|
864
|
+
console.error(message);
|
|
865
|
+
break;
|
|
866
|
+
case "warn":
|
|
867
|
+
console.warn(message);
|
|
868
|
+
break;
|
|
869
|
+
default:
|
|
870
|
+
console.log(message);
|
|
871
|
+
break;
|
|
872
|
+
}
|
|
873
|
+
}
|
|
874
|
+
__name(logFarmEvent, "logFarmEvent");
|
|
875
|
+
function formatFarmEventDetails(event) {
|
|
876
|
+
const details = [];
|
|
877
|
+
const record = event;
|
|
878
|
+
for (const key of [
|
|
879
|
+
"route",
|
|
880
|
+
"pathname",
|
|
881
|
+
"method",
|
|
882
|
+
"status",
|
|
883
|
+
"key",
|
|
884
|
+
"tag",
|
|
885
|
+
"path",
|
|
886
|
+
"reason",
|
|
887
|
+
"durationMs",
|
|
888
|
+
"integration",
|
|
889
|
+
"operation",
|
|
890
|
+
"plugin",
|
|
891
|
+
"hook",
|
|
892
|
+
"target",
|
|
893
|
+
"count"
|
|
894
|
+
]) {
|
|
895
|
+
const value = record[key];
|
|
896
|
+
if (value !== void 0) {
|
|
897
|
+
details.push(`${key}=${formatDetailValue(value)}`);
|
|
898
|
+
}
|
|
899
|
+
}
|
|
900
|
+
return details.length > 0 ? ` ${details.join(" ")}` : "";
|
|
901
|
+
}
|
|
902
|
+
__name(formatFarmEventDetails, "formatFarmEventDetails");
|
|
903
|
+
function formatDetailValue(value) {
|
|
904
|
+
if (typeof value === "string") {
|
|
905
|
+
return value;
|
|
906
|
+
}
|
|
907
|
+
if (typeof value === "number" || typeof value === "boolean") {
|
|
908
|
+
return String(value);
|
|
909
|
+
}
|
|
910
|
+
try {
|
|
911
|
+
return JSON.stringify(value);
|
|
912
|
+
} catch {
|
|
913
|
+
return String(value);
|
|
914
|
+
}
|
|
915
|
+
}
|
|
916
|
+
__name(formatDetailValue, "formatDetailValue");
|
|
917
|
+
function formatError(error) {
|
|
918
|
+
return error instanceof Error ? error.message : String(error);
|
|
919
|
+
}
|
|
920
|
+
__name(formatError, "formatError");
|
|
921
|
+
|
|
922
|
+
// src/i18n/routing.ts
|
|
923
|
+
function resolveFarmLocalePath(pathname, config) {
|
|
924
|
+
const normalized = normalizePathname(pathname);
|
|
925
|
+
if (config.routing === "none") {
|
|
926
|
+
return { pathname: normalized, explicit: false };
|
|
927
|
+
}
|
|
928
|
+
const segments = normalized.split("/").filter(Boolean);
|
|
929
|
+
const firstSegment = segments[0];
|
|
930
|
+
const locale = config.locales.find(
|
|
931
|
+
(candidate) => candidate.toLowerCase() === firstSegment?.toLowerCase()
|
|
932
|
+
);
|
|
933
|
+
if (!locale) {
|
|
934
|
+
return { pathname: normalized, explicit: false };
|
|
935
|
+
}
|
|
936
|
+
const remaining = segments.slice(1);
|
|
937
|
+
return {
|
|
938
|
+
locale,
|
|
939
|
+
pathname: remaining.length > 0 ? `/${remaining.join("/")}` : "/",
|
|
940
|
+
explicit: true
|
|
941
|
+
};
|
|
942
|
+
}
|
|
943
|
+
__name(resolveFarmLocalePath, "resolveFarmLocalePath");
|
|
944
|
+
function stripFarmLocaleFromPathname(pathname, config) {
|
|
945
|
+
return resolveFarmLocalePath(pathname, config).pathname;
|
|
946
|
+
}
|
|
947
|
+
__name(stripFarmLocaleFromPathname, "stripFarmLocaleFromPathname");
|
|
948
|
+
function normalizePathname(pathname) {
|
|
949
|
+
const withLeadingSlash = pathname.startsWith("/") ? pathname : `/${pathname}`;
|
|
950
|
+
if (withLeadingSlash === "/") return "/";
|
|
951
|
+
return withLeadingSlash.replace(/\/{2,}/g, "/").replace(/\/$/, "") || "/";
|
|
952
|
+
}
|
|
953
|
+
__name(normalizePathname, "normalizePathname");
|
|
954
|
+
|
|
955
|
+
// src/middleware/manager.ts
|
|
956
|
+
function isMiddlewareResponse(value) {
|
|
957
|
+
return value instanceof Response;
|
|
958
|
+
}
|
|
959
|
+
__name(isMiddlewareResponse, "isMiddlewareResponse");
|
|
960
|
+
var _MiddlewareManager = class _MiddlewareManager {
|
|
961
|
+
constructor(appDir, viteServer, config, i18n) {
|
|
962
|
+
this.middleware = [];
|
|
963
|
+
this.configMiddleware = [];
|
|
964
|
+
this.appDirs = Array.isArray(appDir) ? [...appDir] : [appDir];
|
|
965
|
+
this.viteServer = viteServer;
|
|
966
|
+
this.i18n = i18n;
|
|
967
|
+
this.configure(config);
|
|
968
|
+
}
|
|
969
|
+
configure(config) {
|
|
970
|
+
this.configMiddleware = [];
|
|
971
|
+
this.globalConfig = void 0;
|
|
972
|
+
if (!config) return;
|
|
973
|
+
const entries = Array.isArray(config) ? config : [config];
|
|
974
|
+
for (const [index, entry] of entries.entries()) {
|
|
975
|
+
const handlers = this.getConfigHandlers(entry);
|
|
976
|
+
const middlewareConfig = this.toMiddlewareConfig(entry);
|
|
977
|
+
if (handlers.length === 0) {
|
|
978
|
+
this.globalConfig = middlewareConfig;
|
|
979
|
+
continue;
|
|
980
|
+
}
|
|
981
|
+
this.configMiddleware.push({
|
|
982
|
+
path: "/",
|
|
983
|
+
filePath: `farm.config.ts#middleware-${index}`,
|
|
984
|
+
handlers,
|
|
985
|
+
config: middlewareConfig,
|
|
986
|
+
source: "config"
|
|
987
|
+
});
|
|
988
|
+
}
|
|
989
|
+
}
|
|
990
|
+
/**
|
|
991
|
+
* Discover all middleware.ts files
|
|
992
|
+
*/
|
|
993
|
+
async discover() {
|
|
994
|
+
const middlewareByPath = /* @__PURE__ */ new Map();
|
|
995
|
+
for (const appDir of this.appDirs) {
|
|
996
|
+
const discovered = [];
|
|
997
|
+
await this.discoverInDirectory(appDir, "/", discovered);
|
|
998
|
+
for (const middleware2 of discovered) {
|
|
999
|
+
middlewareByPath.set(middleware2.path, middleware2);
|
|
1000
|
+
}
|
|
1001
|
+
}
|
|
1002
|
+
this.middleware = Array.from(middlewareByPath.values());
|
|
1003
|
+
this.middleware.sort((a, b) => {
|
|
1004
|
+
const depthA = a.path.split("/").length;
|
|
1005
|
+
const depthB = b.path.split("/").length;
|
|
1006
|
+
return depthA - depthB;
|
|
1007
|
+
});
|
|
1008
|
+
if (process.env.FARM_VERBOSE && this.middleware.length > 0) {
|
|
1009
|
+
logger.success(`Discovered ${this.middleware.length} middleware files`);
|
|
1010
|
+
for (const mw of this.middleware) {
|
|
1011
|
+
logger.info(` ${mw.path} (${mw.handlers.length} handlers)`);
|
|
1012
|
+
}
|
|
1013
|
+
}
|
|
1014
|
+
}
|
|
1015
|
+
/**
|
|
1016
|
+
* Recursively discover middleware files
|
|
1017
|
+
*/
|
|
1018
|
+
async discoverInDirectory(dir, routePath, discovered) {
|
|
1019
|
+
if (!fs.existsSync(dir)) {
|
|
1020
|
+
return;
|
|
1021
|
+
}
|
|
1022
|
+
const middlewareFile = this.findMiddlewareFile(dir);
|
|
1023
|
+
if (middlewareFile) {
|
|
1024
|
+
const middleware2 = await this.loadMiddleware(middlewareFile, routePath);
|
|
1025
|
+
if (middleware2) {
|
|
1026
|
+
discovered.push(middleware2);
|
|
1027
|
+
}
|
|
1028
|
+
}
|
|
1029
|
+
const entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
1030
|
+
for (const entry of entries) {
|
|
1031
|
+
if (entry.isDirectory() && !entry.name.startsWith(".") && !entry.name.startsWith("_")) {
|
|
1032
|
+
const subPath = path.join(dir, entry.name);
|
|
1033
|
+
const subRoutePath = path.posix.join(routePath, entry.name);
|
|
1034
|
+
await this.discoverInDirectory(subPath, subRoutePath, discovered);
|
|
1035
|
+
}
|
|
1036
|
+
}
|
|
1037
|
+
}
|
|
1038
|
+
/**
|
|
1039
|
+
* Find middleware file in directory
|
|
1040
|
+
*/
|
|
1041
|
+
findMiddlewareFile(dir) {
|
|
1042
|
+
const extensions = [".ts", ".tsx", ".js", ".jsx"];
|
|
1043
|
+
for (const ext of extensions) {
|
|
1044
|
+
const filePath = path.join(dir, `middleware${ext}`);
|
|
1045
|
+
if (fs.existsSync(filePath)) {
|
|
1046
|
+
return filePath;
|
|
1047
|
+
}
|
|
1048
|
+
}
|
|
1049
|
+
return null;
|
|
1050
|
+
}
|
|
1051
|
+
/**
|
|
1052
|
+
* Load a middleware file
|
|
1053
|
+
*/
|
|
1054
|
+
async loadMiddleware(filePath, routePath) {
|
|
1055
|
+
try {
|
|
1056
|
+
let module2;
|
|
1057
|
+
if (this.viteServer) {
|
|
1058
|
+
module2 = await this.viteServer.ssrLoadModule(filePath);
|
|
1059
|
+
} else {
|
|
1060
|
+
const fileUrl = `file://${filePath}`;
|
|
1061
|
+
module2 = await import(
|
|
1062
|
+
/* @vite-ignore */
|
|
1063
|
+
fileUrl
|
|
1064
|
+
);
|
|
1065
|
+
}
|
|
1066
|
+
const normalized = normalizeMiddlewareModule(module2, routePath);
|
|
1067
|
+
if (!normalized) {
|
|
1068
|
+
logger.warn(
|
|
1069
|
+
`Middleware file ${filePath} must export a default handler or a named middleware handler`
|
|
1070
|
+
);
|
|
1071
|
+
return null;
|
|
1072
|
+
}
|
|
1073
|
+
return {
|
|
1074
|
+
path: routePath,
|
|
1075
|
+
filePath,
|
|
1076
|
+
handlers: normalized.handlers,
|
|
1077
|
+
config: normalized.config,
|
|
1078
|
+
source: "file"
|
|
1079
|
+
};
|
|
1080
|
+
} catch (error) {
|
|
1081
|
+
logger.error(`Failed to load middleware ${filePath}: ${error}`);
|
|
1082
|
+
return null;
|
|
1083
|
+
}
|
|
1084
|
+
}
|
|
1085
|
+
/**
|
|
1086
|
+
* Execute middleware for a request
|
|
1087
|
+
*/
|
|
1088
|
+
async execute(req, res) {
|
|
1089
|
+
const url = new URL(req.url || "/", `http://${req.headers.host || "localhost"}`);
|
|
1090
|
+
const pathname = url.pathname;
|
|
1091
|
+
const routePathname = this.i18n?.enabled ? stripFarmLocaleFromPathname(pathname, this.i18n) : pathname;
|
|
1092
|
+
const method = req.method || "GET";
|
|
1093
|
+
const startTime = Date.now();
|
|
1094
|
+
let parentData;
|
|
1095
|
+
let ctx = createContext(req, res, this.viteServer);
|
|
1096
|
+
if (this.globalConfig) {
|
|
1097
|
+
const globalMatch = this.matchesConfig(routePathname, this.globalConfig, ctx);
|
|
1098
|
+
if (!globalMatch.matched) {
|
|
1099
|
+
return false;
|
|
1100
|
+
}
|
|
1101
|
+
if (globalMatch.params) {
|
|
1102
|
+
ctx.params = { ...ctx.params, ...globalMatch.params };
|
|
1103
|
+
}
|
|
1104
|
+
}
|
|
1105
|
+
const applicable = [
|
|
1106
|
+
...this.configMiddleware.map((mw) => ({ mw, routeMatch: { matched: true } })),
|
|
1107
|
+
...this.middleware.map((mw) => ({ mw, routeMatch: this.matchRoutePath(routePathname, mw.path) })).filter((entry) => entry.routeMatch.matched)
|
|
1108
|
+
];
|
|
1109
|
+
if (applicable.length === 0) {
|
|
1110
|
+
return false;
|
|
1111
|
+
}
|
|
1112
|
+
try {
|
|
1113
|
+
const pc = require("picocolors");
|
|
1114
|
+
const middlewarePaths = applicable.map(({ mw }) => mw.path).join(", ");
|
|
1115
|
+
const log = [
|
|
1116
|
+
pc.dim("[") + pc.bold(pc.blue("FARM")) + pc.dim("]"),
|
|
1117
|
+
pc.dim("[") + pc.bold(pc.magenta("MIDDLEWARE")) + pc.dim("]"),
|
|
1118
|
+
pc.dim("[") + pc.bold(pc.white(method.padEnd(3))) + pc.dim("]"),
|
|
1119
|
+
pc.gray("Executing middleware: "),
|
|
1120
|
+
pc.gray(pathname),
|
|
1121
|
+
pc.dim(` (${(Date.now() - startTime).toFixed(2)}ms)`)
|
|
1122
|
+
].join(" ");
|
|
1123
|
+
console.log(log);
|
|
1124
|
+
} catch {
|
|
1125
|
+
console.log(
|
|
1126
|
+
`[FARM] [MIDDLEWARE] [${method}] Executing ${pathname} (${applicable.length} middleware)`
|
|
1127
|
+
);
|
|
1128
|
+
}
|
|
1129
|
+
for (const entry of applicable) {
|
|
1130
|
+
const { mw, routeMatch } = entry;
|
|
1131
|
+
const configMatch = mw.config ? this.matchesConfig(routePathname, mw.config, ctx) : { matched: true };
|
|
1132
|
+
if (!configMatch.matched) {
|
|
1133
|
+
continue;
|
|
1134
|
+
}
|
|
1135
|
+
if (routeMatch.params) {
|
|
1136
|
+
ctx.params = { ...ctx.params, ...routeMatch.params };
|
|
1137
|
+
}
|
|
1138
|
+
if (configMatch.params) {
|
|
1139
|
+
ctx.params = { ...ctx.params, ...configMatch.params };
|
|
1140
|
+
}
|
|
1141
|
+
if (parentData) {
|
|
1142
|
+
ctx = createContext(req, res, this.viteServer, parentData);
|
|
1143
|
+
if (routeMatch.params) {
|
|
1144
|
+
ctx.params = { ...ctx.params, ...routeMatch.params };
|
|
1145
|
+
}
|
|
1146
|
+
if (configMatch.params) {
|
|
1147
|
+
ctx.params = { ...ctx.params, ...configMatch.params };
|
|
1148
|
+
}
|
|
1149
|
+
}
|
|
1150
|
+
const middlewareStartTime = Date.now();
|
|
1151
|
+
const middlewareEvent = {
|
|
1152
|
+
route: mw.path,
|
|
1153
|
+
pathname,
|
|
1154
|
+
name: mw.filePath
|
|
1155
|
+
};
|
|
1156
|
+
emitFarmEvent({ type: "middleware.start", ...middlewareEvent });
|
|
1157
|
+
try {
|
|
1158
|
+
let handlerIndex = 0;
|
|
1159
|
+
let returnedResponse;
|
|
1160
|
+
const executeNext = /* @__PURE__ */ __name(async () => {
|
|
1161
|
+
if (handlerIndex < mw.handlers.length) {
|
|
1162
|
+
const handler = mw.handlers[handlerIndex++];
|
|
1163
|
+
const result2 = await handler(ctx, executeNext);
|
|
1164
|
+
if (isMiddlewareResponse(result2)) {
|
|
1165
|
+
returnedResponse = result2;
|
|
1166
|
+
return result2;
|
|
1167
|
+
}
|
|
1168
|
+
}
|
|
1169
|
+
return returnedResponse;
|
|
1170
|
+
}, "executeNext");
|
|
1171
|
+
const result = await executeNext();
|
|
1172
|
+
const response = isMiddlewareResponse(result) ? result : returnedResponse;
|
|
1173
|
+
if (response) {
|
|
1174
|
+
if (!res.headersSent && !res.writableEnded) {
|
|
1175
|
+
for (const [key, value] of ctx.headers) {
|
|
1176
|
+
try {
|
|
1177
|
+
res.setHeader(key, value);
|
|
1178
|
+
} catch (error) {
|
|
1179
|
+
}
|
|
1180
|
+
}
|
|
1181
|
+
}
|
|
1182
|
+
emitFarmEvent({
|
|
1183
|
+
type: "middleware.shortCircuit",
|
|
1184
|
+
...middlewareEvent,
|
|
1185
|
+
status: response.status
|
|
1186
|
+
});
|
|
1187
|
+
await sendWebResponse(res, response);
|
|
1188
|
+
return true;
|
|
1189
|
+
}
|
|
1190
|
+
if (ctx._handled || res.headersSent || res.writableEnded) {
|
|
1191
|
+
emitFarmEvent({
|
|
1192
|
+
type: "middleware.shortCircuit",
|
|
1193
|
+
...middlewareEvent,
|
|
1194
|
+
status: res.statusCode
|
|
1195
|
+
});
|
|
1196
|
+
return true;
|
|
1197
|
+
}
|
|
1198
|
+
emitFarmEvent({
|
|
1199
|
+
type: "middleware.complete",
|
|
1200
|
+
...middlewareEvent,
|
|
1201
|
+
durationMs: Date.now() - middlewareStartTime
|
|
1202
|
+
});
|
|
1203
|
+
} catch (error) {
|
|
1204
|
+
emitFarmEvent({
|
|
1205
|
+
type: "middleware.error",
|
|
1206
|
+
...middlewareEvent,
|
|
1207
|
+
error
|
|
1208
|
+
});
|
|
1209
|
+
throw error;
|
|
1210
|
+
}
|
|
1211
|
+
parentData = {
|
|
1212
|
+
data: new Map(ctx.data),
|
|
1213
|
+
locals: new Map(ctx.locals),
|
|
1214
|
+
headers: Object.fromEntries(ctx.headers)
|
|
1215
|
+
};
|
|
1216
|
+
}
|
|
1217
|
+
if (!res.headersSent && !res.writableEnded) {
|
|
1218
|
+
for (const [key, value] of ctx.headers) {
|
|
1219
|
+
try {
|
|
1220
|
+
res.setHeader(key, value);
|
|
1221
|
+
} catch (error) {
|
|
1222
|
+
}
|
|
1223
|
+
}
|
|
1224
|
+
}
|
|
1225
|
+
req.__FARM_MIDDLEWARE_DATA__ = new Map(ctx.data);
|
|
1226
|
+
req.__FARM_MIDDLEWARE_CONTEXT__ = new Map(ctx.locals);
|
|
1227
|
+
return false;
|
|
1228
|
+
}
|
|
1229
|
+
/**
|
|
1230
|
+
* Check if pathname matches middleware config
|
|
1231
|
+
*/
|
|
1232
|
+
matchesConfig(pathname, config, ctx) {
|
|
1233
|
+
if (config.exclude) {
|
|
1234
|
+
for (const pattern of config.exclude) {
|
|
1235
|
+
if (this.matchPattern(pattern, pathname).matched) {
|
|
1236
|
+
return { matched: false };
|
|
1237
|
+
}
|
|
1238
|
+
}
|
|
1239
|
+
}
|
|
1240
|
+
if (config.matcher) {
|
|
1241
|
+
for (const matcher of this.toMatcherList(config.matcher)) {
|
|
1242
|
+
if (typeof matcher === "string" || matcher instanceof RegExp) {
|
|
1243
|
+
const result = this.matchPattern(matcher, pathname);
|
|
1244
|
+
if (result.matched) {
|
|
1245
|
+
return result;
|
|
1246
|
+
}
|
|
1247
|
+
} else if (typeof matcher === "function") {
|
|
1248
|
+
return { matched: matcher(ctx) };
|
|
1249
|
+
}
|
|
1250
|
+
}
|
|
1251
|
+
return { matched: false };
|
|
1252
|
+
}
|
|
1253
|
+
return { matched: true };
|
|
1254
|
+
}
|
|
1255
|
+
/**
|
|
1256
|
+
* Match a pattern against pathname
|
|
1257
|
+
*/
|
|
1258
|
+
matchPattern(pattern, pathname) {
|
|
1259
|
+
if (pattern instanceof RegExp) {
|
|
1260
|
+
pattern.lastIndex = 0;
|
|
1261
|
+
const match2 = pattern.exec(pathname);
|
|
1262
|
+
return {
|
|
1263
|
+
matched: !!match2,
|
|
1264
|
+
params: match2?.groups ? { ...match2.groups } : void 0
|
|
1265
|
+
};
|
|
1266
|
+
}
|
|
1267
|
+
if (pattern === "*" || pattern === "/(.*)") {
|
|
1268
|
+
return { matched: true };
|
|
1269
|
+
}
|
|
1270
|
+
if (pattern.endsWith("(.*)")) {
|
|
1271
|
+
const prefix = pattern.slice(0, -4);
|
|
1272
|
+
return { matched: pathname === prefix || pathname.startsWith(`${prefix}/`) };
|
|
1273
|
+
}
|
|
1274
|
+
const { regex, params } = this.compilePathPattern(pattern);
|
|
1275
|
+
const match = regex.exec(pathname);
|
|
1276
|
+
if (!match) {
|
|
1277
|
+
return { matched: false };
|
|
1278
|
+
}
|
|
1279
|
+
const values = {};
|
|
1280
|
+
params.forEach((param, index) => {
|
|
1281
|
+
values[param] = decodeURIComponent(match[index + 1] || "");
|
|
1282
|
+
});
|
|
1283
|
+
return {
|
|
1284
|
+
matched: true,
|
|
1285
|
+
params: Object.keys(values).length > 0 ? values : void 0
|
|
1286
|
+
};
|
|
1287
|
+
}
|
|
1288
|
+
/**
|
|
1289
|
+
* Reload middleware (for HMR)
|
|
1290
|
+
*/
|
|
1291
|
+
async reload() {
|
|
1292
|
+
await this.discover();
|
|
1293
|
+
}
|
|
1294
|
+
getMiddlewares() {
|
|
1295
|
+
return [...this.configMiddleware, ...this.middleware];
|
|
1296
|
+
}
|
|
1297
|
+
hasMiddleware() {
|
|
1298
|
+
return this.configMiddleware.length > 0 || this.middleware.length > 0;
|
|
1299
|
+
}
|
|
1300
|
+
matchRoutePath(pathname, middlewarePath) {
|
|
1301
|
+
if (middlewarePath === "/") return { matched: true };
|
|
1302
|
+
const exactMatch = this.matchPattern(middlewarePath, pathname);
|
|
1303
|
+
if (exactMatch.matched) {
|
|
1304
|
+
return exactMatch;
|
|
1305
|
+
}
|
|
1306
|
+
const nestedMatch = this.matchPattern(`${middlewarePath}/:__farmRest*`, pathname);
|
|
1307
|
+
if (!nestedMatch.matched) {
|
|
1308
|
+
return { matched: false };
|
|
1309
|
+
}
|
|
1310
|
+
const params = { ...nestedMatch.params };
|
|
1311
|
+
delete params.__farmRest;
|
|
1312
|
+
return {
|
|
1313
|
+
matched: true,
|
|
1314
|
+
params: Object.keys(params).length > 0 ? params : void 0
|
|
1315
|
+
};
|
|
1316
|
+
}
|
|
1317
|
+
toMatcherList(matcher) {
|
|
1318
|
+
if (!matcher) return [];
|
|
1319
|
+
return Array.isArray(matcher) ? matcher : [matcher];
|
|
1320
|
+
}
|
|
1321
|
+
getConfigHandlers(entry) {
|
|
1322
|
+
const handlers = [];
|
|
1323
|
+
if ("handler" in entry && typeof entry.handler === "function") {
|
|
1324
|
+
handlers.push(entry.handler);
|
|
1325
|
+
}
|
|
1326
|
+
if ("handlers" in entry && Array.isArray(entry.handlers)) {
|
|
1327
|
+
handlers.push(...entry.handlers.filter((handler) => typeof handler === "function"));
|
|
1328
|
+
}
|
|
1329
|
+
return handlers;
|
|
1330
|
+
}
|
|
1331
|
+
toMiddlewareConfig(entry) {
|
|
1332
|
+
const { matcher, exclude, runtime } = entry;
|
|
1333
|
+
return { matcher, exclude, runtime };
|
|
1334
|
+
}
|
|
1335
|
+
compilePathPattern(pattern) {
|
|
1336
|
+
const params = [];
|
|
1337
|
+
const segments = pattern.split("/").filter(Boolean);
|
|
1338
|
+
if (segments.length === 0) {
|
|
1339
|
+
return { regex: /^\/$/, params };
|
|
1340
|
+
}
|
|
1341
|
+
const parts = segments.map((segment) => {
|
|
1342
|
+
if (segment === "**") {
|
|
1343
|
+
return "(?:/.*)?";
|
|
1344
|
+
}
|
|
1345
|
+
if (segment === "*") {
|
|
1346
|
+
return "/[^/]+";
|
|
1347
|
+
}
|
|
1348
|
+
if (segment.startsWith(":")) {
|
|
1349
|
+
const { name, modifier } = this.parseColonParam(segment);
|
|
1350
|
+
params.push(name);
|
|
1351
|
+
if (modifier === "*") {
|
|
1352
|
+
return "(?:/(.*))?";
|
|
1353
|
+
}
|
|
1354
|
+
if (modifier === "+") {
|
|
1355
|
+
return "/(.+)";
|
|
1356
|
+
}
|
|
1357
|
+
return "/([^/]+)";
|
|
1358
|
+
}
|
|
1359
|
+
if (segment.startsWith("[...") && segment.endsWith("]")) {
|
|
1360
|
+
params.push(segment.slice(4, -1));
|
|
1361
|
+
return "(?:/(.*))?";
|
|
1362
|
+
}
|
|
1363
|
+
if (segment.startsWith("[") && segment.endsWith("]")) {
|
|
1364
|
+
params.push(segment.slice(1, -1));
|
|
1365
|
+
return "/([^/]+)";
|
|
1366
|
+
}
|
|
1367
|
+
return `/${this.escapeRegex(segment).replace(/\\\*/g, "[^/]*")}`;
|
|
1368
|
+
});
|
|
1369
|
+
return {
|
|
1370
|
+
regex: new RegExp(`^${parts.join("")}$`),
|
|
1371
|
+
params
|
|
1372
|
+
};
|
|
1373
|
+
}
|
|
1374
|
+
parseColonParam(segment) {
|
|
1375
|
+
const raw = segment.slice(1);
|
|
1376
|
+
const last = raw[raw.length - 1];
|
|
1377
|
+
const modifier = last === "*" || last === "+" || last === "?" ? last : void 0;
|
|
1378
|
+
return {
|
|
1379
|
+
name: modifier ? raw.slice(0, -1) : raw,
|
|
1380
|
+
modifier
|
|
1381
|
+
};
|
|
1382
|
+
}
|
|
1383
|
+
escapeRegex(value) {
|
|
1384
|
+
return value.replace(/[|\\{}()[\]^$+?.]/g, "\\$&");
|
|
1385
|
+
}
|
|
1386
|
+
};
|
|
1387
|
+
__name(_MiddlewareManager, "MiddlewareManager");
|
|
1388
|
+
var MiddlewareManager = _MiddlewareManager;
|
|
1389
|
+
|
|
1390
|
+
// src/middleware/server.ts
|
|
1391
|
+
var import_async_hooks = require("async_hooks");
|
|
1392
|
+
var MIDDLEWARE_DATA_STORE_KEY = /* @__PURE__ */ Symbol.for("farm.middlewareDataStore");
|
|
1393
|
+
var MIDDLEWARE_CONTEXT_STORE_KEY = /* @__PURE__ */ Symbol.for("farm.middlewareContextStore");
|
|
1394
|
+
function getGlobalMiddlewareStore(key) {
|
|
1395
|
+
const state = globalThis;
|
|
1396
|
+
const existing = state[key];
|
|
1397
|
+
if (existing instanceof import_async_hooks.AsyncLocalStorage) {
|
|
1398
|
+
return existing;
|
|
1399
|
+
}
|
|
1400
|
+
const store = new import_async_hooks.AsyncLocalStorage();
|
|
1401
|
+
state[key] = store;
|
|
1402
|
+
return store;
|
|
1403
|
+
}
|
|
1404
|
+
__name(getGlobalMiddlewareStore, "getGlobalMiddlewareStore");
|
|
1405
|
+
var middlewareStore = getGlobalMiddlewareStore(MIDDLEWARE_DATA_STORE_KEY);
|
|
1406
|
+
var middlewareContextStore = getGlobalMiddlewareStore(MIDDLEWARE_CONTEXT_STORE_KEY);
|
|
1407
|
+
function toMiddlewareMap(data) {
|
|
1408
|
+
if (data instanceof Map) {
|
|
1409
|
+
return new Map(data);
|
|
1410
|
+
}
|
|
1411
|
+
return new Map(Object.entries(data));
|
|
1412
|
+
}
|
|
1413
|
+
__name(toMiddlewareMap, "toMiddlewareMap");
|
|
1414
|
+
async function _runWithMiddlewareData(data, fn) {
|
|
1415
|
+
return middlewareStore.run(toMiddlewareMap(data), fn);
|
|
1416
|
+
}
|
|
1417
|
+
__name(_runWithMiddlewareData, "_runWithMiddlewareData");
|
|
1418
|
+
async function _runWithMiddlewareContext(context, fn) {
|
|
1419
|
+
return middlewareContextStore.run(toMiddlewareMap(context), fn);
|
|
1420
|
+
}
|
|
1421
|
+
__name(_runWithMiddlewareContext, "_runWithMiddlewareContext");
|
|
1422
|
+
function getMiddlewareData() {
|
|
1423
|
+
return middlewareStore.getStore() || /* @__PURE__ */ new Map();
|
|
1424
|
+
}
|
|
1425
|
+
__name(getMiddlewareData, "getMiddlewareData");
|
|
1426
|
+
function getMiddlewareValue(key) {
|
|
1427
|
+
const data = getMiddlewareData();
|
|
1428
|
+
return data.get(key);
|
|
1429
|
+
}
|
|
1430
|
+
__name(getMiddlewareValue, "getMiddlewareValue");
|
|
1431
|
+
function getMiddlewareContext() {
|
|
1432
|
+
return middlewareContextStore.getStore() || /* @__PURE__ */ new Map();
|
|
1433
|
+
}
|
|
1434
|
+
__name(getMiddlewareContext, "getMiddlewareContext");
|
|
1435
|
+
|
|
1436
|
+
// src/middleware/helpers.ts
|
|
1437
|
+
function unwrapMiddleware(middleware2, defaults) {
|
|
1438
|
+
if (!middleware2) {
|
|
1439
|
+
return defaults || {};
|
|
1440
|
+
}
|
|
1441
|
+
const result = { ...defaults };
|
|
1442
|
+
for (const [key, value] of middleware2.data) {
|
|
1443
|
+
result[key] = value;
|
|
1444
|
+
}
|
|
1445
|
+
return result;
|
|
1446
|
+
}
|
|
1447
|
+
__name(unwrapMiddleware, "unwrapMiddleware");
|
|
1448
|
+
function getFromMiddleware(middleware2, key, defaultValue) {
|
|
1449
|
+
if (!middleware2) {
|
|
1450
|
+
return defaultValue;
|
|
1451
|
+
}
|
|
1452
|
+
const value = middleware2.data.get(key);
|
|
1453
|
+
return value !== void 0 ? value : defaultValue;
|
|
1454
|
+
}
|
|
1455
|
+
__name(getFromMiddleware, "getFromMiddleware");
|
|
1456
|
+
function hasMiddlewareData(middleware2) {
|
|
1457
|
+
if (!middleware2) return false;
|
|
1458
|
+
return middleware2.data.size > 0;
|
|
1459
|
+
}
|
|
1460
|
+
__name(hasMiddlewareData, "hasMiddlewareData");
|
|
1461
|
+
|
|
1462
|
+
// src/middleware/production-runtime.ts
|
|
1463
|
+
function parseCookies2(cookieHeader) {
|
|
1464
|
+
if (!cookieHeader) return {};
|
|
1465
|
+
return cookieHeader.split(";").reduce(
|
|
1466
|
+
(cookies, cookie) => {
|
|
1467
|
+
const [name, ...rest] = cookie.split("=");
|
|
1468
|
+
const value = rest.join("=").trim();
|
|
1469
|
+
if (name && value) {
|
|
1470
|
+
cookies[name.trim()] = decodeURIComponent(value);
|
|
1471
|
+
}
|
|
1472
|
+
return cookies;
|
|
1473
|
+
},
|
|
1474
|
+
{}
|
|
1475
|
+
);
|
|
1476
|
+
}
|
|
1477
|
+
__name(parseCookies2, "parseCookies");
|
|
1478
|
+
function serializeCookie2(name, value, options = {}) {
|
|
1479
|
+
let cookie = `${encodeURIComponent(name)}=${encodeURIComponent(value)}`;
|
|
1480
|
+
if (options.maxAge) cookie += `; Max-Age=${options.maxAge}`;
|
|
1481
|
+
if (options.expires) cookie += `; Expires=${options.expires.toUTCString()}`;
|
|
1482
|
+
cookie += `; Path=${options.path || "/"}`;
|
|
1483
|
+
if (options.domain) cookie += `; Domain=${options.domain}`;
|
|
1484
|
+
if (options.secure) cookie += "; Secure";
|
|
1485
|
+
if (options.httpOnly) cookie += "; HttpOnly";
|
|
1486
|
+
if (options.sameSite) {
|
|
1487
|
+
cookie += `; SameSite=${options.sameSite.charAt(0).toUpperCase()}${options.sameSite.slice(1)}`;
|
|
1488
|
+
}
|
|
1489
|
+
return cookie;
|
|
1490
|
+
}
|
|
1491
|
+
__name(serializeCookie2, "serializeCookie");
|
|
1492
|
+
var _WebCookieJar = class _WebCookieJar {
|
|
1493
|
+
constructor(request, headers) {
|
|
1494
|
+
this.headers = headers;
|
|
1495
|
+
this.setCookies = [];
|
|
1496
|
+
this.cookies = parseCookies2(request.headers.get("cookie"));
|
|
1497
|
+
}
|
|
1498
|
+
get(name) {
|
|
1499
|
+
return this.cookies[name];
|
|
1500
|
+
}
|
|
1501
|
+
set(name, value, options = {}) {
|
|
1502
|
+
this.cookies[name] = value;
|
|
1503
|
+
const cookieString = serializeCookie2(name, value, options);
|
|
1504
|
+
this.setCookies.push(cookieString);
|
|
1505
|
+
this.headers.set("Set-Cookie", this.setCookies.join(", "));
|
|
1506
|
+
}
|
|
1507
|
+
delete(name) {
|
|
1508
|
+
delete this.cookies[name];
|
|
1509
|
+
const cookieString = serializeCookie2(name, "", {
|
|
1510
|
+
maxAge: 0,
|
|
1511
|
+
expires: /* @__PURE__ */ new Date(0)
|
|
1512
|
+
});
|
|
1513
|
+
this.setCookies.push(cookieString);
|
|
1514
|
+
this.headers.set("Set-Cookie", this.setCookies.join(", "));
|
|
1515
|
+
}
|
|
1516
|
+
getAll() {
|
|
1517
|
+
return { ...this.cookies };
|
|
1518
|
+
}
|
|
1519
|
+
};
|
|
1520
|
+
__name(_WebCookieJar, "WebCookieJar");
|
|
1521
|
+
var WebCookieJar = _WebCookieJar;
|
|
1522
|
+
function mapToHeaders(map) {
|
|
1523
|
+
const headers = new Headers();
|
|
1524
|
+
for (const [key, value] of map) {
|
|
1525
|
+
if (key.toLowerCase() === "set-cookie") {
|
|
1526
|
+
headers.append(key, value);
|
|
1527
|
+
} else {
|
|
1528
|
+
headers.set(key, value);
|
|
1529
|
+
}
|
|
1530
|
+
}
|
|
1531
|
+
return headers;
|
|
1532
|
+
}
|
|
1533
|
+
__name(mapToHeaders, "mapToHeaders");
|
|
1534
|
+
function headersToRecord(headers) {
|
|
1535
|
+
return Object.fromEntries(headers);
|
|
1536
|
+
}
|
|
1537
|
+
__name(headersToRecord, "headersToRecord");
|
|
1538
|
+
function isMiddlewareResponse2(value) {
|
|
1539
|
+
return value instanceof Response;
|
|
1540
|
+
}
|
|
1541
|
+
__name(isMiddlewareResponse2, "isMiddlewareResponse");
|
|
1542
|
+
function createResponseShim(headers) {
|
|
1543
|
+
return {
|
|
1544
|
+
headersSent: false,
|
|
1545
|
+
writableEnded: false,
|
|
1546
|
+
setHeader(name, value) {
|
|
1547
|
+
headers.set(name, Array.isArray(value) ? value.join(", ") : String(value));
|
|
1548
|
+
},
|
|
1549
|
+
getHeader(name) {
|
|
1550
|
+
return headers.get(name);
|
|
1551
|
+
},
|
|
1552
|
+
writeHead(status, responseHeaders) {
|
|
1553
|
+
this.statusCode = status;
|
|
1554
|
+
if (responseHeaders) {
|
|
1555
|
+
for (const [key, value] of Object.entries(responseHeaders)) {
|
|
1556
|
+
headers.set(key, value);
|
|
1557
|
+
}
|
|
1558
|
+
}
|
|
1559
|
+
this.headersSent = true;
|
|
1560
|
+
},
|
|
1561
|
+
end() {
|
|
1562
|
+
this.writableEnded = true;
|
|
1563
|
+
}
|
|
1564
|
+
};
|
|
1565
|
+
}
|
|
1566
|
+
__name(createResponseShim, "createResponseShim");
|
|
1567
|
+
function withNodeRequestShape(request) {
|
|
1568
|
+
const requestWithShape = request;
|
|
1569
|
+
if (!requestWithShape.socket) {
|
|
1570
|
+
const forwardedFor = request.headers.get("x-forwarded-for")?.split(",")[0]?.trim();
|
|
1571
|
+
try {
|
|
1572
|
+
requestWithShape.socket = {
|
|
1573
|
+
remoteAddress: forwardedFor || "127.0.0.1"
|
|
1574
|
+
};
|
|
1575
|
+
} catch {
|
|
1576
|
+
}
|
|
1577
|
+
}
|
|
1578
|
+
return requestWithShape;
|
|
1579
|
+
}
|
|
1580
|
+
__name(withNodeRequestShape, "withNodeRequestShape");
|
|
1581
|
+
function createHandledResponse(body, init, headers) {
|
|
1582
|
+
const responseHeaders = new Headers(init.headers);
|
|
1583
|
+
for (const [key, value] of headers) {
|
|
1584
|
+
if (key.toLowerCase() === "set-cookie") {
|
|
1585
|
+
responseHeaders.append(key, value);
|
|
1586
|
+
} else {
|
|
1587
|
+
responseHeaders.set(key, value);
|
|
1588
|
+
}
|
|
1589
|
+
}
|
|
1590
|
+
return new Response(body, { ...init, headers: responseHeaders });
|
|
1591
|
+
}
|
|
1592
|
+
__name(createHandledResponse, "createHandledResponse");
|
|
1593
|
+
function createWebMiddlewareContext(request, parent) {
|
|
1594
|
+
let currentRequest = withNodeRequestShape(request);
|
|
1595
|
+
let handledResponse = null;
|
|
1596
|
+
const url = new URL(currentRequest.url);
|
|
1597
|
+
const data = parent?.data ? new Map(parent.data) : /* @__PURE__ */ new Map();
|
|
1598
|
+
const locals = parent?.locals ? new Map(parent.locals) : /* @__PURE__ */ new Map();
|
|
1599
|
+
const headers = /* @__PURE__ */ new Map();
|
|
1600
|
+
if (parent?.headers) {
|
|
1601
|
+
for (const [key, value] of Object.entries(parent.headers)) {
|
|
1602
|
+
headers.set(key, value);
|
|
1603
|
+
}
|
|
1604
|
+
}
|
|
1605
|
+
const ctx = {
|
|
1606
|
+
request: currentRequest,
|
|
1607
|
+
response: createResponseShim(headers),
|
|
1608
|
+
url,
|
|
1609
|
+
pathname: url.pathname,
|
|
1610
|
+
searchParams: url.searchParams,
|
|
1611
|
+
method: currentRequest.method || "GET",
|
|
1612
|
+
params: {},
|
|
1613
|
+
route: url.pathname,
|
|
1614
|
+
parent,
|
|
1615
|
+
vite: {
|
|
1616
|
+
isDev: false,
|
|
1617
|
+
hmr: false
|
|
1618
|
+
},
|
|
1619
|
+
data,
|
|
1620
|
+
locals,
|
|
1621
|
+
headers,
|
|
1622
|
+
cookies: new WebCookieJar(currentRequest, headers),
|
|
1623
|
+
_handled: false,
|
|
1624
|
+
_redirectUrl: void 0,
|
|
1625
|
+
_rewriteUrl: void 0,
|
|
1626
|
+
redirect(redirectUrl, status = 307) {
|
|
1627
|
+
ctx._redirectUrl = redirectUrl;
|
|
1628
|
+
ctx._handled = true;
|
|
1629
|
+
handledResponse = createHandledResponse(
|
|
1630
|
+
`Redirecting to ${redirectUrl}`,
|
|
1631
|
+
{
|
|
1632
|
+
status,
|
|
1633
|
+
headers: {
|
|
1634
|
+
Location: redirectUrl,
|
|
1635
|
+
"Content-Type": "text/plain"
|
|
1636
|
+
}
|
|
1637
|
+
},
|
|
1638
|
+
headers
|
|
1639
|
+
);
|
|
1640
|
+
},
|
|
1641
|
+
rewrite(rewriteUrl) {
|
|
1642
|
+
ctx._rewriteUrl = rewriteUrl;
|
|
1643
|
+
const nextUrl = new URL(rewriteUrl, currentRequest.url);
|
|
1644
|
+
currentRequest = withNodeRequestShape(new Request(nextUrl, currentRequest));
|
|
1645
|
+
ctx.request = currentRequest;
|
|
1646
|
+
ctx.url = nextUrl;
|
|
1647
|
+
ctx.pathname = nextUrl.pathname;
|
|
1648
|
+
ctx.searchParams = nextUrl.searchParams;
|
|
1649
|
+
ctx.route = nextUrl.pathname;
|
|
1650
|
+
},
|
|
1651
|
+
json(jsonData, status = 200) {
|
|
1652
|
+
ctx._handled = true;
|
|
1653
|
+
handledResponse = createHandledResponse(
|
|
1654
|
+
JSON.stringify(jsonData),
|
|
1655
|
+
{
|
|
1656
|
+
status,
|
|
1657
|
+
headers: {
|
|
1658
|
+
"Content-Type": "application/json"
|
|
1659
|
+
}
|
|
1660
|
+
},
|
|
1661
|
+
headers
|
|
1662
|
+
);
|
|
1663
|
+
},
|
|
1664
|
+
text(content, status = 200) {
|
|
1665
|
+
ctx._handled = true;
|
|
1666
|
+
handledResponse = createHandledResponse(
|
|
1667
|
+
content,
|
|
1668
|
+
{
|
|
1669
|
+
status,
|
|
1670
|
+
headers: {
|
|
1671
|
+
"Content-Type": "text/plain"
|
|
1672
|
+
}
|
|
1673
|
+
},
|
|
1674
|
+
headers
|
|
1675
|
+
);
|
|
1676
|
+
},
|
|
1677
|
+
html(content, status = 200) {
|
|
1678
|
+
ctx._handled = true;
|
|
1679
|
+
handledResponse = createHandledResponse(
|
|
1680
|
+
content,
|
|
1681
|
+
{
|
|
1682
|
+
status,
|
|
1683
|
+
headers: {
|
|
1684
|
+
"Content-Type": "text/html"
|
|
1685
|
+
}
|
|
1686
|
+
},
|
|
1687
|
+
headers
|
|
1688
|
+
);
|
|
1689
|
+
}
|
|
1690
|
+
};
|
|
1691
|
+
return {
|
|
1692
|
+
ctx,
|
|
1693
|
+
getRequest: /* @__PURE__ */ __name(() => currentRequest, "getRequest"),
|
|
1694
|
+
getResponse: /* @__PURE__ */ __name(() => handledResponse, "getResponse")
|
|
1695
|
+
};
|
|
1696
|
+
}
|
|
1697
|
+
__name(createWebMiddlewareContext, "createWebMiddlewareContext");
|
|
1698
|
+
function toMatcherList(matcher) {
|
|
1699
|
+
if (!matcher) return [];
|
|
1700
|
+
return Array.isArray(matcher) ? matcher : [matcher];
|
|
1701
|
+
}
|
|
1702
|
+
__name(toMatcherList, "toMatcherList");
|
|
1703
|
+
function escapeRegex(value) {
|
|
1704
|
+
return value.replace(/[|\\{}()[\]^$+?.]/g, "\\$&");
|
|
1705
|
+
}
|
|
1706
|
+
__name(escapeRegex, "escapeRegex");
|
|
1707
|
+
function parseColonParam(segment) {
|
|
1708
|
+
const raw = segment.slice(1);
|
|
1709
|
+
const last = raw[raw.length - 1];
|
|
1710
|
+
const modifier = last === "*" || last === "+" || last === "?" ? last : void 0;
|
|
1711
|
+
return {
|
|
1712
|
+
name: modifier ? raw.slice(0, -1) : raw,
|
|
1713
|
+
modifier
|
|
1714
|
+
};
|
|
1715
|
+
}
|
|
1716
|
+
__name(parseColonParam, "parseColonParam");
|
|
1717
|
+
function compilePathPattern(pattern) {
|
|
1718
|
+
const params = [];
|
|
1719
|
+
const segments = pattern.split("/").filter(Boolean);
|
|
1720
|
+
if (segments.length === 0) {
|
|
1721
|
+
return { regex: /^\/$/, params };
|
|
1722
|
+
}
|
|
1723
|
+
const parts = segments.map((segment) => {
|
|
1724
|
+
if (segment === "**") {
|
|
1725
|
+
return "(?:/.*)?";
|
|
1726
|
+
}
|
|
1727
|
+
if (segment === "*") {
|
|
1728
|
+
return "/[^/]+";
|
|
1729
|
+
}
|
|
1730
|
+
if (segment.startsWith(":")) {
|
|
1731
|
+
const { name, modifier } = parseColonParam(segment);
|
|
1732
|
+
params.push(name);
|
|
1733
|
+
if (modifier === "*") {
|
|
1734
|
+
return "(?:/(.*))?";
|
|
1735
|
+
}
|
|
1736
|
+
if (modifier === "+") {
|
|
1737
|
+
return "/(.+)";
|
|
1738
|
+
}
|
|
1739
|
+
return "/([^/]+)";
|
|
1740
|
+
}
|
|
1741
|
+
if (segment.startsWith("[...") && segment.endsWith("]")) {
|
|
1742
|
+
params.push(segment.slice(4, -1));
|
|
1743
|
+
return "(?:/(.*))?";
|
|
1744
|
+
}
|
|
1745
|
+
if (segment.startsWith("[") && segment.endsWith("]")) {
|
|
1746
|
+
params.push(segment.slice(1, -1));
|
|
1747
|
+
return "/([^/]+)";
|
|
1748
|
+
}
|
|
1749
|
+
return `/${escapeRegex(segment).replace(/\\\*/g, "[^/]*")}`;
|
|
1750
|
+
});
|
|
1751
|
+
return {
|
|
1752
|
+
regex: new RegExp(`^${parts.join("")}$`),
|
|
1753
|
+
params
|
|
1754
|
+
};
|
|
1755
|
+
}
|
|
1756
|
+
__name(compilePathPattern, "compilePathPattern");
|
|
1757
|
+
function matchPattern2(pattern, pathname) {
|
|
1758
|
+
if (pattern instanceof RegExp) {
|
|
1759
|
+
pattern.lastIndex = 0;
|
|
1760
|
+
const match2 = pattern.exec(pathname);
|
|
1761
|
+
return {
|
|
1762
|
+
matched: !!match2,
|
|
1763
|
+
params: match2?.groups ? { ...match2.groups } : void 0
|
|
1764
|
+
};
|
|
1765
|
+
}
|
|
1766
|
+
if (pattern === "*" || pattern === "/(.*)") {
|
|
1767
|
+
return { matched: true };
|
|
1768
|
+
}
|
|
1769
|
+
if (pattern.endsWith("(.*)")) {
|
|
1770
|
+
const prefix = pattern.slice(0, -4);
|
|
1771
|
+
return { matched: pathname === prefix || pathname.startsWith(`${prefix}/`) };
|
|
1772
|
+
}
|
|
1773
|
+
const { regex, params } = compilePathPattern(pattern);
|
|
1774
|
+
const match = regex.exec(pathname);
|
|
1775
|
+
if (!match) {
|
|
1776
|
+
return { matched: false };
|
|
1777
|
+
}
|
|
1778
|
+
const values = {};
|
|
1779
|
+
params.forEach((param, index) => {
|
|
1780
|
+
values[param] = decodeURIComponent(match[index + 1] || "");
|
|
1781
|
+
});
|
|
1782
|
+
return {
|
|
1783
|
+
matched: true,
|
|
1784
|
+
params: Object.keys(values).length > 0 ? values : void 0
|
|
1785
|
+
};
|
|
1786
|
+
}
|
|
1787
|
+
__name(matchPattern2, "matchPattern");
|
|
1788
|
+
function matchesConfig(pathname, config, ctx) {
|
|
1789
|
+
if (config.exclude) {
|
|
1790
|
+
for (const pattern of config.exclude) {
|
|
1791
|
+
if (matchPattern2(pattern, pathname).matched) {
|
|
1792
|
+
return { matched: false };
|
|
1793
|
+
}
|
|
1794
|
+
}
|
|
1795
|
+
}
|
|
1796
|
+
if (config.matcher) {
|
|
1797
|
+
for (const matcher of toMatcherList(config.matcher)) {
|
|
1798
|
+
if (typeof matcher === "string" || matcher instanceof RegExp) {
|
|
1799
|
+
const result = matchPattern2(matcher, pathname);
|
|
1800
|
+
if (result.matched) {
|
|
1801
|
+
return result;
|
|
1802
|
+
}
|
|
1803
|
+
} else if (typeof matcher === "function") {
|
|
1804
|
+
return { matched: matcher(ctx) };
|
|
1805
|
+
}
|
|
1806
|
+
}
|
|
1807
|
+
return { matched: false };
|
|
1808
|
+
}
|
|
1809
|
+
return { matched: true };
|
|
1810
|
+
}
|
|
1811
|
+
__name(matchesConfig, "matchesConfig");
|
|
1812
|
+
function matchRoutePath(pathname, middlewarePath) {
|
|
1813
|
+
if (middlewarePath === "/") return { matched: true };
|
|
1814
|
+
const exactMatch = matchPattern2(middlewarePath, pathname);
|
|
1815
|
+
if (exactMatch.matched) {
|
|
1816
|
+
return exactMatch;
|
|
1817
|
+
}
|
|
1818
|
+
const nestedMatch = matchPattern2(`${middlewarePath}/:__farmRest*`, pathname);
|
|
1819
|
+
if (!nestedMatch.matched) {
|
|
1820
|
+
return { matched: false };
|
|
1821
|
+
}
|
|
1822
|
+
const params = { ...nestedMatch.params };
|
|
1823
|
+
delete params.__farmRest;
|
|
1824
|
+
return {
|
|
1825
|
+
matched: true,
|
|
1826
|
+
params: Object.keys(params).length > 0 ? params : void 0
|
|
1827
|
+
};
|
|
1828
|
+
}
|
|
1829
|
+
__name(matchRoutePath, "matchRoutePath");
|
|
1830
|
+
function getConfigHandlers(entry) {
|
|
1831
|
+
const handlers = [];
|
|
1832
|
+
if ("handler" in entry && typeof entry.handler === "function") {
|
|
1833
|
+
handlers.push(entry.handler);
|
|
1834
|
+
}
|
|
1835
|
+
if ("handlers" in entry && Array.isArray(entry.handlers)) {
|
|
1836
|
+
handlers.push(...entry.handlers.filter((handler) => typeof handler === "function"));
|
|
1837
|
+
}
|
|
1838
|
+
return handlers;
|
|
1839
|
+
}
|
|
1840
|
+
__name(getConfigHandlers, "getConfigHandlers");
|
|
1841
|
+
function toMiddlewareConfig(entry) {
|
|
1842
|
+
const { matcher, exclude, runtime } = entry;
|
|
1843
|
+
return { matcher, exclude, runtime };
|
|
1844
|
+
}
|
|
1845
|
+
__name(toMiddlewareConfig, "toMiddlewareConfig");
|
|
1846
|
+
function normalizeConfigMiddleware(config) {
|
|
1847
|
+
const entries = [];
|
|
1848
|
+
let globalConfig;
|
|
1849
|
+
if (!config) {
|
|
1850
|
+
return { entries, globalConfig };
|
|
1851
|
+
}
|
|
1852
|
+
const configEntries = Array.isArray(config) ? config : [config];
|
|
1853
|
+
for (const [index, entry] of configEntries.entries()) {
|
|
1854
|
+
const handlers = getConfigHandlers(entry);
|
|
1855
|
+
const middlewareConfig = toMiddlewareConfig(entry);
|
|
1856
|
+
if (handlers.length === 0) {
|
|
1857
|
+
globalConfig = middlewareConfig;
|
|
1858
|
+
continue;
|
|
1859
|
+
}
|
|
1860
|
+
entries.push({
|
|
1861
|
+
path: "/",
|
|
1862
|
+
filePath: `farm.config.ts#middleware-${index}`,
|
|
1863
|
+
handlers,
|
|
1864
|
+
config: middlewareConfig,
|
|
1865
|
+
source: "config"
|
|
1866
|
+
});
|
|
1867
|
+
}
|
|
1868
|
+
return { entries, globalConfig };
|
|
1869
|
+
}
|
|
1870
|
+
__name(normalizeConfigMiddleware, "normalizeConfigMiddleware");
|
|
1871
|
+
function normalizeFileMiddleware(modules = []) {
|
|
1872
|
+
const entries = [];
|
|
1873
|
+
for (const moduleEntry of modules) {
|
|
1874
|
+
const normalized = normalizeMiddlewareModule(moduleEntry.module, moduleEntry.path);
|
|
1875
|
+
if (!normalized) {
|
|
1876
|
+
continue;
|
|
1877
|
+
}
|
|
1878
|
+
entries.push({
|
|
1879
|
+
path: moduleEntry.path,
|
|
1880
|
+
filePath: moduleEntry.filePath || moduleEntry.path,
|
|
1881
|
+
handlers: normalized.handlers,
|
|
1882
|
+
config: normalized.config,
|
|
1883
|
+
source: "file"
|
|
1884
|
+
});
|
|
1885
|
+
}
|
|
1886
|
+
return entries;
|
|
1887
|
+
}
|
|
1888
|
+
__name(normalizeFileMiddleware, "normalizeFileMiddleware");
|
|
1889
|
+
async function executeHandlers(handlers, ctx) {
|
|
1890
|
+
let handlerIndex = 0;
|
|
1891
|
+
let returnedResponse;
|
|
1892
|
+
const executeNext = /* @__PURE__ */ __name(async () => {
|
|
1893
|
+
if (handlerIndex < handlers.length) {
|
|
1894
|
+
const handler = handlers[handlerIndex++];
|
|
1895
|
+
const result2 = await handler(ctx, executeNext);
|
|
1896
|
+
if (isMiddlewareResponse2(result2)) {
|
|
1897
|
+
returnedResponse = result2;
|
|
1898
|
+
return result2;
|
|
1899
|
+
}
|
|
1900
|
+
}
|
|
1901
|
+
return returnedResponse;
|
|
1902
|
+
}, "executeNext");
|
|
1903
|
+
const result = await executeNext();
|
|
1904
|
+
return isMiddlewareResponse2(result) ? result : returnedResponse;
|
|
1905
|
+
}
|
|
1906
|
+
__name(executeHandlers, "executeHandlers");
|
|
1907
|
+
function emptyResult(request) {
|
|
1908
|
+
return {
|
|
1909
|
+
request,
|
|
1910
|
+
response: null,
|
|
1911
|
+
data: /* @__PURE__ */ new Map(),
|
|
1912
|
+
context: /* @__PURE__ */ new Map(),
|
|
1913
|
+
headers: new Headers(),
|
|
1914
|
+
handled: false
|
|
1915
|
+
};
|
|
1916
|
+
}
|
|
1917
|
+
__name(emptyResult, "emptyResult");
|
|
1918
|
+
function createProductionMiddlewareRunner(options = {}) {
|
|
1919
|
+
const configMiddleware = normalizeConfigMiddleware(options.config);
|
|
1920
|
+
const fileMiddleware = normalizeFileMiddleware(options.modules);
|
|
1921
|
+
const entries = [...configMiddleware.entries, ...fileMiddleware];
|
|
1922
|
+
const globalConfig = configMiddleware.globalConfig;
|
|
1923
|
+
return /* @__PURE__ */ __name(async function runProductionMiddleware(request) {
|
|
1924
|
+
if (!globalConfig && entries.length === 0) {
|
|
1925
|
+
return emptyResult(request);
|
|
1926
|
+
}
|
|
1927
|
+
let contextState = createWebMiddlewareContext(request);
|
|
1928
|
+
let ctx = contextState.ctx;
|
|
1929
|
+
let currentRequest = contextState.getRequest();
|
|
1930
|
+
const initialPathname = options.i18n?.enabled ? stripFarmLocaleFromPathname(ctx.pathname, options.i18n) : ctx.pathname;
|
|
1931
|
+
let parentData;
|
|
1932
|
+
if (globalConfig) {
|
|
1933
|
+
const globalMatch = matchesConfig(initialPathname, globalConfig, ctx);
|
|
1934
|
+
if (!globalMatch.matched) {
|
|
1935
|
+
return emptyResult(request);
|
|
1936
|
+
}
|
|
1937
|
+
if (globalMatch.params) {
|
|
1938
|
+
ctx.params = { ...ctx.params, ...globalMatch.params };
|
|
1939
|
+
}
|
|
1940
|
+
}
|
|
1941
|
+
const applicable = entries.map((entry) => ({
|
|
1942
|
+
entry,
|
|
1943
|
+
routeMatch: entry.source === "config" ? { matched: true } : matchRoutePath(initialPathname, entry.path)
|
|
1944
|
+
})).filter((candidate) => candidate.routeMatch.matched);
|
|
1945
|
+
if (applicable.length === 0) {
|
|
1946
|
+
return {
|
|
1947
|
+
request: currentRequest,
|
|
1948
|
+
response: null,
|
|
1949
|
+
data: new Map(ctx.data),
|
|
1950
|
+
context: new Map(ctx.locals),
|
|
1951
|
+
headers: mapToHeaders(ctx.headers),
|
|
1952
|
+
handled: false
|
|
1953
|
+
};
|
|
1954
|
+
}
|
|
1955
|
+
for (const candidate of applicable) {
|
|
1956
|
+
const { entry, routeMatch } = candidate;
|
|
1957
|
+
const configMatch = entry.config ? matchesConfig(initialPathname, entry.config, ctx) : { matched: true };
|
|
1958
|
+
if (!configMatch.matched) {
|
|
1959
|
+
continue;
|
|
1960
|
+
}
|
|
1961
|
+
if (parentData) {
|
|
1962
|
+
contextState = createWebMiddlewareContext(currentRequest, parentData);
|
|
1963
|
+
ctx = contextState.ctx;
|
|
1964
|
+
}
|
|
1965
|
+
if (routeMatch.params) {
|
|
1966
|
+
ctx.params = { ...ctx.params, ...routeMatch.params };
|
|
1967
|
+
}
|
|
1968
|
+
if (configMatch.params) {
|
|
1969
|
+
ctx.params = { ...ctx.params, ...configMatch.params };
|
|
1970
|
+
}
|
|
1971
|
+
const middlewareStartTime = Date.now();
|
|
1972
|
+
const middlewareEvent = {
|
|
1973
|
+
route: entry.path,
|
|
1974
|
+
pathname: initialPathname,
|
|
1975
|
+
name: entry.filePath
|
|
1976
|
+
};
|
|
1977
|
+
emitFarmEvent({ type: "middleware.start", ...middlewareEvent });
|
|
1978
|
+
try {
|
|
1979
|
+
const returnedResponse = await executeHandlers(entry.handlers, ctx);
|
|
1980
|
+
currentRequest = contextState.getRequest();
|
|
1981
|
+
if (returnedResponse) {
|
|
1982
|
+
const response = applyProductionMiddlewareHeaders(
|
|
1983
|
+
returnedResponse,
|
|
1984
|
+
mapToHeaders(ctx.headers)
|
|
1985
|
+
);
|
|
1986
|
+
emitFarmEvent({
|
|
1987
|
+
type: "middleware.shortCircuit",
|
|
1988
|
+
...middlewareEvent,
|
|
1989
|
+
status: response.status
|
|
1990
|
+
});
|
|
1991
|
+
return {
|
|
1992
|
+
request: currentRequest,
|
|
1993
|
+
response,
|
|
1994
|
+
data: new Map(ctx.data),
|
|
1995
|
+
context: new Map(ctx.locals),
|
|
1996
|
+
headers: mapToHeaders(ctx.headers),
|
|
1997
|
+
handled: true
|
|
1998
|
+
};
|
|
1999
|
+
}
|
|
2000
|
+
if (ctx._handled) {
|
|
2001
|
+
const response = contextState.getResponse() || new Response(null);
|
|
2002
|
+
emitFarmEvent({
|
|
2003
|
+
type: "middleware.shortCircuit",
|
|
2004
|
+
...middlewareEvent,
|
|
2005
|
+
status: response.status
|
|
2006
|
+
});
|
|
2007
|
+
return {
|
|
2008
|
+
request: currentRequest,
|
|
2009
|
+
response,
|
|
2010
|
+
data: new Map(ctx.data),
|
|
2011
|
+
context: new Map(ctx.locals),
|
|
2012
|
+
headers: mapToHeaders(ctx.headers),
|
|
2013
|
+
handled: true
|
|
2014
|
+
};
|
|
2015
|
+
}
|
|
2016
|
+
emitFarmEvent({
|
|
2017
|
+
type: "middleware.complete",
|
|
2018
|
+
...middlewareEvent,
|
|
2019
|
+
durationMs: Date.now() - middlewareStartTime
|
|
2020
|
+
});
|
|
2021
|
+
} catch (error) {
|
|
2022
|
+
emitFarmEvent({
|
|
2023
|
+
type: "middleware.error",
|
|
2024
|
+
...middlewareEvent,
|
|
2025
|
+
error
|
|
2026
|
+
});
|
|
2027
|
+
throw error;
|
|
2028
|
+
}
|
|
2029
|
+
parentData = {
|
|
2030
|
+
data: new Map(ctx.data),
|
|
2031
|
+
locals: new Map(ctx.locals),
|
|
2032
|
+
headers: headersToRecord(ctx.headers)
|
|
2033
|
+
};
|
|
2034
|
+
}
|
|
2035
|
+
return {
|
|
2036
|
+
request: currentRequest,
|
|
2037
|
+
response: null,
|
|
2038
|
+
data: new Map(ctx.data),
|
|
2039
|
+
context: new Map(ctx.locals),
|
|
2040
|
+
headers: mapToHeaders(ctx.headers),
|
|
2041
|
+
handled: false
|
|
2042
|
+
};
|
|
2043
|
+
}, "runProductionMiddleware");
|
|
2044
|
+
}
|
|
2045
|
+
__name(createProductionMiddlewareRunner, "createProductionMiddlewareRunner");
|
|
2046
|
+
function applyProductionMiddlewareHeaders(response, middlewareHeaders) {
|
|
2047
|
+
if (!middlewareHeaders || Array.from(middlewareHeaders.keys()).length === 0) {
|
|
2048
|
+
return response;
|
|
2049
|
+
}
|
|
2050
|
+
const headers = new Headers(response.headers);
|
|
2051
|
+
for (const [key, value] of middlewareHeaders) {
|
|
2052
|
+
if (key.toLowerCase() === "set-cookie") {
|
|
2053
|
+
headers.append(key, value);
|
|
2054
|
+
} else {
|
|
2055
|
+
headers.set(key, value);
|
|
2056
|
+
}
|
|
2057
|
+
}
|
|
2058
|
+
return new Response(response.body, {
|
|
2059
|
+
status: response.status,
|
|
2060
|
+
statusText: response.statusText,
|
|
2061
|
+
headers
|
|
2062
|
+
});
|
|
2063
|
+
}
|
|
2064
|
+
__name(applyProductionMiddlewareHeaders, "applyProductionMiddlewareHeaders");
|
|
2065
|
+
|
|
2066
|
+
// src/middleware/vite-plugin.ts
|
|
2067
|
+
var import_node_fs = __toESM(require("fs"));
|
|
2068
|
+
var import_node_path = __toESM(require("path"));
|
|
2069
|
+
function getMiddlewareAppDirectory(root, srcDir) {
|
|
2070
|
+
const sourceDirectory = import_node_path.default.join(root, srcDir);
|
|
2071
|
+
const appDirectory = import_node_path.default.join(sourceDirectory, "app");
|
|
2072
|
+
return import_node_fs.default.existsSync(appDirectory) ? appDirectory : sourceDirectory;
|
|
2073
|
+
}
|
|
2074
|
+
__name(getMiddlewareAppDirectory, "getMiddlewareAppDirectory");
|
|
2075
|
+
function farmMiddlewarePlugin(options = {}) {
|
|
2076
|
+
const srcDir = options.srcDir ?? "src";
|
|
2077
|
+
let manager = null;
|
|
2078
|
+
let discoveryComplete = false;
|
|
2079
|
+
let discoveryPromise = null;
|
|
2080
|
+
const discover = /* @__PURE__ */ __name(async (server) => {
|
|
2081
|
+
manager = new MiddlewareManager(getMiddlewareAppDirectory(server.config.root, srcDir), server);
|
|
2082
|
+
await manager.discover();
|
|
2083
|
+
discoveryComplete = true;
|
|
2084
|
+
}, "discover");
|
|
2085
|
+
return {
|
|
2086
|
+
name: "@farm.js/core:middleware",
|
|
2087
|
+
enforce: "pre",
|
|
2088
|
+
configureServer(server) {
|
|
2089
|
+
discoveryPromise = discover(server).catch((error) => {
|
|
2090
|
+
discoveryComplete = true;
|
|
2091
|
+
console.error("[FARM] Middleware discovery error:", error);
|
|
2092
|
+
});
|
|
2093
|
+
const execute = /* @__PURE__ */ __name(async (req, res, _pathname, sharedData) => {
|
|
2094
|
+
if (discoveryPromise && !discoveryComplete) {
|
|
2095
|
+
await discoveryPromise;
|
|
2096
|
+
}
|
|
2097
|
+
if (!manager) {
|
|
2098
|
+
return false;
|
|
2099
|
+
}
|
|
2100
|
+
const handled = await manager.execute(req, res);
|
|
2101
|
+
const middlewareData = req.__FARM_MIDDLEWARE_DATA__;
|
|
2102
|
+
if (sharedData && middlewareData instanceof Map) {
|
|
2103
|
+
for (const [key, value] of middlewareData) {
|
|
2104
|
+
sharedData.set(key, value);
|
|
2105
|
+
}
|
|
2106
|
+
}
|
|
2107
|
+
return handled;
|
|
2108
|
+
}, "execute");
|
|
2109
|
+
server.__farmMiddleware__ = {
|
|
2110
|
+
execute,
|
|
2111
|
+
getMiddlewares: /* @__PURE__ */ __name(() => manager?.getMiddlewares() ?? [], "getMiddlewares"),
|
|
2112
|
+
isReady: /* @__PURE__ */ __name(() => discoveryComplete, "isReady"),
|
|
2113
|
+
waitForDiscovery: /* @__PURE__ */ __name(() => discoveryPromise, "waitForDiscovery")
|
|
2114
|
+
};
|
|
2115
|
+
},
|
|
2116
|
+
async handleHotUpdate({ file, server, modules }) {
|
|
2117
|
+
if (!/^middleware\.[tj]sx?$/.test(import_node_path.default.basename(file))) {
|
|
2118
|
+
return;
|
|
2119
|
+
}
|
|
2120
|
+
for (const module2 of modules) {
|
|
2121
|
+
server.moduleGraph.invalidateModule(module2);
|
|
2122
|
+
}
|
|
2123
|
+
await manager?.reload();
|
|
2124
|
+
server.ws.send({ type: "full-reload", path: "*" });
|
|
2125
|
+
return [];
|
|
2126
|
+
}
|
|
2127
|
+
};
|
|
2128
|
+
}
|
|
2129
|
+
__name(farmMiddlewarePlugin, "farmMiddlewarePlugin");
|
|
2130
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
2131
|
+
0 && (module.exports = {
|
|
2132
|
+
MiddlewareManager,
|
|
2133
|
+
_runWithMiddlewareContext,
|
|
2134
|
+
_runWithMiddlewareData,
|
|
2135
|
+
applyProductionMiddlewareHeaders,
|
|
2136
|
+
createContext,
|
|
2137
|
+
createProductionMiddlewareRunner,
|
|
2138
|
+
farmMiddlewarePlugin,
|
|
2139
|
+
getFromMiddleware,
|
|
2140
|
+
getMiddlewareContext,
|
|
2141
|
+
getMiddlewareData,
|
|
2142
|
+
getMiddlewareValue,
|
|
2143
|
+
getRateLimitStatus,
|
|
2144
|
+
hasMiddlewareData,
|
|
2145
|
+
middleware,
|
|
2146
|
+
unwrapMiddleware
|
|
2147
|
+
});
|
|
2148
|
+
//# sourceMappingURL=middleware.cjs.map
|