edge-functions 4.8.1 → 5.0.0-stage.1
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/README.md +17 -0
- package/dist/chunk-ACWSA73O.js +1172 -0
- package/dist/chunk-GVUH3USH.js +29 -0
- package/dist/commands-LW4LOJNI.js +183 -0
- package/dist/env-LSTI67LB.js +19 -0
- package/dist/main.js +166 -0
- package/package.json +35 -91
- package/.commitlintrc.json +0 -40
- package/.gitmodules +0 -3
- package/.husky/commit-msg +0 -5
- package/.husky/pre-commit +0 -4
- package/.prettierignore +0 -9
- package/CHANGELOG.md +0 -1495
- package/CODEOWNERS +0 -1
- package/CODE_OF_CONDUCT.md +0 -73
- package/CONTRIBUTING.md +0 -91
- package/Dockerfile +0 -13
- package/aliases.js +0 -15
- package/docs/nextjs.md +0 -83
- package/docs/nodejs-apis.md +0 -58
- package/docs/overview.md +0 -51
- package/docs/presets.md +0 -125
- package/docs/publish-preset.md +0 -46
- package/jest.global.setup.js +0 -20
- package/jsconfig.json +0 -22
- package/jsdoc.json +0 -52
- package/lib/build/bundlers/base.bundlers.js +0 -87
- package/lib/build/bundlers/esbuild/esbuild.bundlers.js +0 -85
- package/lib/build/bundlers/esbuild/esbuild.bundlers.test.js +0 -85
- package/lib/build/bundlers/esbuild/esbuild.config.js +0 -56
- package/lib/build/bundlers/esbuild/index.js +0 -3
- package/lib/build/bundlers/esbuild/plugins/azion-polyfills/azion-polyfills.plugins.js +0 -105
- package/lib/build/bundlers/esbuild/plugins/azion-polyfills/azion-polyfills.plugins.test.js +0 -46
- package/lib/build/bundlers/esbuild/plugins/azion-polyfills/index.js +0 -3
- package/lib/build/bundlers/esbuild/plugins/node-polyfills/index.js +0 -3
- package/lib/build/bundlers/esbuild/plugins/node-polyfills/node-polyfills.plugins.js +0 -337
- package/lib/build/bundlers/esbuild/plugins/node-polyfills/node-polyfills.plugins.test.js +0 -40
- package/lib/build/bundlers/helpers/azion-libs.js +0 -25
- package/lib/build/bundlers/index.js +0 -4
- package/lib/build/bundlers/webpack/index.js +0 -3
- package/lib/build/bundlers/webpack/plugins/azion-polyfills/azion-polyfills.plugins.js +0 -96
- package/lib/build/bundlers/webpack/plugins/azion-polyfills/azion-polyfills.plugins.test.js +0 -84
- package/lib/build/bundlers/webpack/plugins/azion-polyfills/index.js +0 -3
- package/lib/build/bundlers/webpack/plugins/babel-custom/babel-custom-loader.plugins.js +0 -40
- package/lib/build/bundlers/webpack/plugins/babel-custom/index.js +0 -3
- package/lib/build/bundlers/webpack/plugins/node-polyfills/index.js +0 -3
- package/lib/build/bundlers/webpack/plugins/node-polyfills/node-polyfills.plugins.js +0 -180
- package/lib/build/bundlers/webpack/plugins/node-polyfills/node-polyfills.plugins.test.js +0 -59
- package/lib/build/bundlers/webpack/webpack.bundlers.js +0 -114
- package/lib/build/bundlers/webpack/webpack.bundlers.test.js +0 -96
- package/lib/build/bundlers/webpack/webpack.config.js +0 -125
- package/lib/build/dispatcher/dispatcher.js +0 -600
- package/lib/build/dispatcher/dispatcher.test.js +0 -93
- package/lib/build/dispatcher/helpers/helpers.js +0 -104
- package/lib/build/dispatcher/index.js +0 -3
- package/lib/commands/build.commands.js +0 -151
- package/lib/commands/dev.commands.js +0 -38
- package/lib/commands/index.js +0 -13
- package/lib/commands/init.commands.js +0 -37
- package/lib/commands/manifest.commands.js +0 -68
- package/lib/commands/presets.commands.js +0 -82
- package/lib/constants/index.js +0 -4
- package/lib/constants/messages/build.messages.js +0 -54
- package/lib/constants/messages/env.messages.js +0 -43
- package/lib/constants/messages/global.messages.js +0 -28
- package/lib/constants/messages/index.js +0 -11
- package/lib/constants/runtime-apis.constants.js +0 -118
- package/lib/env/env.test.js +0 -178
- package/lib/env/index.js +0 -5
- package/lib/env/polyfills/async-hooks/async-hooks.polyfills.js +0 -10
- package/lib/env/polyfills/async-hooks/context/async-hooks.context.js +0 -10
- package/lib/env/polyfills/async-hooks/context/index.js +0 -3
- package/lib/env/polyfills/async-hooks/index.js +0 -8
- package/lib/env/polyfills/azion/env-vars/context/env-vars.context.js +0 -67
- package/lib/env/polyfills/azion/env-vars/context/env-vars.context.test.js +0 -20
- package/lib/env/polyfills/azion/env-vars/context/index.js +0 -3
- package/lib/env/polyfills/azion/env-vars/env-vars.polyfills.js +0 -25
- package/lib/env/polyfills/azion/env-vars/env-vars.polyfills.test.js +0 -22
- package/lib/env/polyfills/azion/env-vars/index.js +0 -3
- package/lib/env/polyfills/azion/fetch/context/fetch.context.js +0 -39
- package/lib/env/polyfills/azion/fetch/context/fetch.context.test.js +0 -46
- package/lib/env/polyfills/azion/fetch/index.js +0 -3
- package/lib/env/polyfills/azion/fetch-event/context/fetch-event.context.js +0 -25
- package/lib/env/polyfills/azion/fetch-event/context/index.js +0 -3
- package/lib/env/polyfills/azion/fetch-event/index.js +0 -3
- package/lib/env/polyfills/azion/firewall-event/context/firewall-event.context.js +0 -153
- package/lib/env/polyfills/azion/firewall-event/context/firewall-event.context.test.js +0 -56
- package/lib/env/polyfills/azion/firewall-event/context/index.js +0 -3
- package/lib/env/polyfills/azion/firewall-event/index.js +0 -3
- package/lib/env/polyfills/azion/network-list/context/index.js +0 -3
- package/lib/env/polyfills/azion/network-list/context/network-list.context.js +0 -232
- package/lib/env/polyfills/azion/network-list/context/network-list.context.test.js +0 -44
- package/lib/env/polyfills/azion/network-list/index.js +0 -3
- package/lib/env/polyfills/azion/network-list/network-list.polyfills.js +0 -16
- package/lib/env/polyfills/azion/storage/context/index.js +0 -3
- package/lib/env/polyfills/azion/storage/context/storage.context.js +0 -182
- package/lib/env/polyfills/azion/storage/context/storage.context.test.js +0 -199
- package/lib/env/polyfills/azion/storage/index.js +0 -4
- package/lib/env/polyfills/azion/storage/storage.polyfills.js +0 -206
- package/lib/env/polyfills/azion/storage/storage.polyfills.test.js +0 -102
- package/lib/env/polyfills/fs/context/fs.context.js +0 -96
- package/lib/env/polyfills/fs/context/index.js +0 -3
- package/lib/env/polyfills/fs/fs.polyfills.js +0 -242
- package/lib/env/polyfills/fs/index.js +0 -8
- package/lib/env/polyfills/fs/promises/promises.polyfills.js +0 -137
- package/lib/env/polyfills/index.js +0 -17
- package/lib/env/runtime.env.js +0 -103
- package/lib/env/server.env.js +0 -213
- package/lib/env/vulcan.env.js +0 -314
- package/lib/main.js +0 -258
- package/lib/notations/namespaces.js +0 -45
- package/lib/notations/typedef.js +0 -32
- package/lib/presets/angular/azion.config.js +0 -10
- package/lib/presets/angular/config.js +0 -10
- package/lib/presets/angular/handler.js +0 -17
- package/lib/presets/angular/prebuild.js +0 -38
- package/lib/presets/astro/azion.config.js +0 -11
- package/lib/presets/astro/config.js +0 -10
- package/lib/presets/astro/handler.js +0 -17
- package/lib/presets/astro/prebuild.js +0 -32
- package/lib/presets/docusaurus/azion.config.js +0 -11
- package/lib/presets/docusaurus/config.js +0 -10
- package/lib/presets/docusaurus/handler.js +0 -17
- package/lib/presets/docusaurus/prebuild.js +0 -21
- package/lib/presets/eleventy/azion.config.js +0 -11
- package/lib/presets/eleventy/config.js +0 -10
- package/lib/presets/eleventy/handler.js +0 -17
- package/lib/presets/eleventy/prebuild.js +0 -19
- package/lib/presets/emscripten/azion.config.js +0 -11
- package/lib/presets/emscripten/config.js +0 -33
- package/lib/presets/emscripten/handler.js +0 -37
- package/lib/presets/emscripten/prebuild.js +0 -50
- package/lib/presets/gatsby/azion.config.js +0 -11
- package/lib/presets/gatsby/config.js +0 -10
- package/lib/presets/gatsby/handler.js +0 -17
- package/lib/presets/gatsby/prebuild.js +0 -21
- package/lib/presets/hexo/azion.config.js +0 -11
- package/lib/presets/hexo/config.js +0 -10
- package/lib/presets/hexo/handler.js +0 -17
- package/lib/presets/hexo/prebuild.js +0 -33
- package/lib/presets/html/azion.config.js +0 -11
- package/lib/presets/html/config.js +0 -10
- package/lib/presets/html/handler.js +0 -17
- package/lib/presets/html/prebuild.js +0 -15
- package/lib/presets/hugo/azion.config.js +0 -11
- package/lib/presets/hugo/config.js +0 -10
- package/lib/presets/hugo/handler.js +0 -17
- package/lib/presets/hugo/prebuild.js +0 -22
- package/lib/presets/javascript/azion.config.js +0 -11
- package/lib/presets/javascript/config.js +0 -10
- package/lib/presets/javascript/handler.js +0 -15
- package/lib/presets/javascript/prebuild.js +0 -8
- package/lib/presets/jekyll/azion.config.js +0 -11
- package/lib/presets/jekyll/config.js +0 -10
- package/lib/presets/jekyll/handler.js +0 -17
- package/lib/presets/jekyll/prebuild.js +0 -19
- package/lib/presets/next/azion.config.js +0 -11
- package/lib/presets/next/config.js +0 -12
- package/lib/presets/next/default/handler/async-local-storage.js +0 -3
- package/lib/presets/next/default/handler/images.js +0 -160
- package/lib/presets/next/default/handler/routing/http.js +0 -134
- package/lib/presets/next/default/handler/routing/http.test.js +0 -129
- package/lib/presets/next/default/handler/routing/index.js +0 -85
- package/lib/presets/next/default/handler/routing/libs.js +0 -4
- package/lib/presets/next/default/handler/routing/matcher.js +0 -69
- package/lib/presets/next/default/handler/routing/matcher.test.js +0 -98
- package/lib/presets/next/default/handler/routing/pcre.js +0 -43
- package/lib/presets/next/default/handler/routing/pcre.test.js +0 -139
- package/lib/presets/next/default/handler/routing/routes-matcher.js +0 -575
- package/lib/presets/next/default/handler/routing/utils.js +0 -126
- package/lib/presets/next/default/handler/routing/utils.test.js +0 -56
- package/lib/presets/next/default/prebuild/edge/index.js +0 -54
- package/lib/presets/next/default/prebuild/index.js +0 -147
- package/lib/presets/next/default/prebuild/mapping/assets.js +0 -50
- package/lib/presets/next/default/prebuild/mapping/assets.test.js +0 -26
- package/lib/presets/next/default/prebuild/mapping/index.js +0 -241
- package/lib/presets/next/default/prebuild/mapping/index.test.js +0 -77
- package/lib/presets/next/default/prebuild/mapping/process-mapping.js +0 -265
- package/lib/presets/next/default/prebuild/mapping/process-mapping.test.js +0 -356
- package/lib/presets/next/default/prebuild/prerendered/index.js +0 -222
- package/lib/presets/next/default/prebuild/validation/support.js +0 -177
- package/lib/presets/next/default/prebuild/validation/support.test.js +0 -123
- package/lib/presets/next/default/prebuild/validation/supported-versions.js +0 -19
- package/lib/presets/next/handler.js +0 -86
- package/lib/presets/next/node/custom-server/12.3.x/index.js +0 -22
- package/lib/presets/next/node/custom-server/12.3.x/server/base-http/compute-js.js +0 -22
- package/lib/presets/next/node/custom-server/12.3.x/server/compute-js.js +0 -62
- package/lib/presets/next/node/custom-server/12.3.x/server/config.js +0 -852
- package/lib/presets/next/node/custom-server/12.3.x/server/load-components.js +0 -106
- package/lib/presets/next/node/custom-server/12.3.x/server/next-compute-js-server.js +0 -1229
- package/lib/presets/next/node/custom-server/12.3.x/server/next.js +0 -103
- package/lib/presets/next/node/custom-server/12.3.x/server/require.js +0 -298
- package/lib/presets/next/node/custom-server/12.3.x/server/response-cache/index.js +0 -125
- package/lib/presets/next/node/custom-server/12.3.x/server/serve-static.js +0 -58
- package/lib/presets/next/node/custom-server/12.3.x/util/etag.js +0 -37
- package/lib/presets/next/node/handler/index.js +0 -25
- package/lib/presets/next/node/prebuild/index.js +0 -88
- package/lib/presets/next/node/prebuild/statics/index.js +0 -286
- package/lib/presets/next/postbuild.js +0 -32
- package/lib/presets/next/prebuild.js +0 -149
- package/lib/presets/next/static/handler.js +0 -17
- package/lib/presets/next/static/prebuild.js +0 -241
- package/lib/presets/next/utils/fs.js +0 -113
- package/lib/presets/next/utils/next.js +0 -40
- package/lib/presets/next/utils/routing.js +0 -62
- package/lib/presets/next/utils.next.js +0 -66
- package/lib/presets/nuxt/azion.config.js +0 -10
- package/lib/presets/nuxt/config.js +0 -10
- package/lib/presets/nuxt/handler.js +0 -17
- package/lib/presets/nuxt/prebuild.js +0 -19
- package/lib/presets/preact/azion.config.js +0 -5
- package/lib/presets/preact/config.js +0 -10
- package/lib/presets/preact/handler.js +0 -17
- package/lib/presets/preact/prebuild.js +0 -89
- package/lib/presets/qwik/azion.config.js +0 -5
- package/lib/presets/qwik/config.js +0 -10
- package/lib/presets/qwik/handler.js +0 -17
- package/lib/presets/qwik/prebuild.js +0 -52
- package/lib/presets/react/azion.config.js +0 -11
- package/lib/presets/react/config.js +0 -10
- package/lib/presets/react/handler.js +0 -17
- package/lib/presets/react/prebuild.js +0 -93
- package/lib/presets/rustwasm/azion.config.js +0 -11
- package/lib/presets/rustwasm/config.js +0 -33
- package/lib/presets/rustwasm/handler.js +0 -25
- package/lib/presets/rustwasm/prebuild.js +0 -22
- package/lib/presets/stencil/azion.config.js +0 -51
- package/lib/presets/stencil/config.js +0 -10
- package/lib/presets/stencil/handler.js +0 -17
- package/lib/presets/stencil/prebuild.js +0 -21
- package/lib/presets/svelte/azion.config.js +0 -11
- package/lib/presets/svelte/config.js +0 -10
- package/lib/presets/svelte/handler.js +0 -17
- package/lib/presets/svelte/prebuild.js +0 -21
- package/lib/presets/typescript/azion.config.js +0 -11
- package/lib/presets/typescript/config.js +0 -10
- package/lib/presets/typescript/handler.js +0 -15
- package/lib/presets/typescript/prebuild.js +0 -8
- package/lib/presets/vitepress/azion.config.js +0 -11
- package/lib/presets/vitepress/config.js +0 -10
- package/lib/presets/vitepress/handler.js +0 -17
- package/lib/presets/vitepress/prebuild.js +0 -39
- package/lib/presets/vue/azion.config.js +0 -11
- package/lib/presets/vue/config.js +0 -10
- package/lib/presets/vue/handler.js +0 -17
- package/lib/presets/vue/prebuild.js +0 -91
- package/lib/presets/vuepress/azion.config.js +0 -49
- package/lib/presets/vuepress/config.js +0 -10
- package/lib/presets/vuepress/handler.js +0 -17
- package/lib/presets/vuepress/prebuild.js +0 -39
- package/lib/providers/azion/firewall_worker.js +0 -4
- package/lib/providers/azion/worker.js +0 -4
- package/lib/utils/checkingProjectType/checkingProjectType.test.js +0 -25
- package/lib/utils/checkingProjectType/checkingProjectType.utils.js +0 -27
- package/lib/utils/checkingProjectType/index.js +0 -3
- package/lib/utils/copyDirectory/copyDirectory.utils.js +0 -83
- package/lib/utils/copyDirectory/copyDirectory.utils.test.js +0 -60
- package/lib/utils/copyDirectory/index.js +0 -3
- package/lib/utils/copyFilesToFS/copyFilesToFS.utils.js +0 -17
- package/lib/utils/copyFilesToFS/copyFilesToFS.utils.test.js +0 -62
- package/lib/utils/copyFilesToFS/index.js +0 -3
- package/lib/utils/debug/debug.utils.js +0 -37
- package/lib/utils/debug/debug.utils.test.js +0 -47
- package/lib/utils/debug/index.js +0 -3
- package/lib/utils/exec/exec.utils.js +0 -86
- package/lib/utils/exec/exec.utils.test.js +0 -85
- package/lib/utils/exec/index.js +0 -3
- package/lib/utils/feedback/feedback.utils.js +0 -129
- package/lib/utils/feedback/feedback.utils.test.js +0 -11
- package/lib/utils/feedback/index.js +0 -3
- package/lib/utils/generateManifest/generateManifest.utils.js +0 -25
- package/lib/utils/generateManifest/index.js +0 -3
- package/lib/utils/generateTimestamp/generateTimestamp.utils.js +0 -26
- package/lib/utils/generateTimestamp/generateTimestamp.utils.test.js +0 -10
- package/lib/utils/generateTimestamp/index.js +0 -3
- package/lib/utils/generateWebpackBanner/generateWebpackBanner.utils.js +0 -22
- package/lib/utils/generateWebpackBanner/generateWebpackBanner.utils.test.js +0 -22
- package/lib/utils/generateWebpackBanner/index.js +0 -3
- package/lib/utils/getAbsoluteLibDirPath/getAbsoluteLibDirPath.utils.js +0 -31
- package/lib/utils/getAbsoluteLibDirPath/getAbsoluteLibDirPath.utils.test.js +0 -13
- package/lib/utils/getAbsoluteLibDirPath/index.js +0 -3
- package/lib/utils/getExportedFunctionBody/getExportedFunctionBody.utils.js +0 -88
- package/lib/utils/getExportedFunctionBody/getExportedFunctionBody.utils.test.js +0 -81
- package/lib/utils/getExportedFunctionBody/index.js +0 -3
- package/lib/utils/getPackageManager/getPackageManager.utils.js +0 -123
- package/lib/utils/getPackageManager/getPackageManager.utils.test.js +0 -51
- package/lib/utils/getPackageManager/index.js +0 -3
- package/lib/utils/getPackageVersion/getPackageVersion.utils.js +0 -29
- package/lib/utils/getPackageVersion/getPackageVersion.utils.test.js +0 -34
- package/lib/utils/getPackageVersion/index.js +0 -3
- package/lib/utils/getProjectJsonFile/getProjectJsonFile.utils.js +0 -21
- package/lib/utils/getProjectJsonFile/getProjectJsonFile.utils.test.js +0 -42
- package/lib/utils/getProjectJsonFile/index.js +0 -3
- package/lib/utils/getUrlFromResource/getUrlFromResource.utils.js +0 -23
- package/lib/utils/getUrlFromResource/getUrlFromResource.utils.test.js +0 -43
- package/lib/utils/getUrlFromResource/index.js +0 -3
- package/lib/utils/getVulcanBuildId/getVulcanBuildId.utils.js +0 -50
- package/lib/utils/getVulcanBuildId/getVulcanBuildId.utils.test.js +0 -16
- package/lib/utils/getVulcanBuildId/index.js +0 -3
- package/lib/utils/helperHandlerCode/helperHandlerCode.js +0 -31
- package/lib/utils/helperHandlerCode/helperHandlerCode.test.js +0 -29
- package/lib/utils/helperHandlerCode/index.js +0 -3
- package/lib/utils/index.js +0 -51
- package/lib/utils/injectFilesInMem/index.js +0 -3
- package/lib/utils/injectFilesInMem/injectFilesInMem.utils.js +0 -51
- package/lib/utils/injectFilesInMem/injectFilesInMem.utils.test.js +0 -48
- package/lib/utils/overrideStaticOutputPath/index.js +0 -3
- package/lib/utils/overrideStaticOutputPath/overrideStaticOutputPath.utils.js +0 -58
- package/lib/utils/overrideStaticOutputPath/overrideStaticOutputPath.utils.test.js +0 -38
- package/lib/utils/presets/index.js +0 -3
- package/lib/utils/presets/presets.utils.js +0 -126
- package/lib/utils/presets/presets.utils.test.js +0 -76
- package/lib/utils/readWorkerFile/index.js +0 -3
- package/lib/utils/readWorkerFile/readWorkerFile.utils.js +0 -37
- package/lib/utils/readWorkerFile/readWorkerFile.utils.test.js +0 -26
- package/lib/utils/relocateImportsAndRequires/index.js +0 -3
- package/lib/utils/relocateImportsAndRequires/relocateImportsAndRequires.utils.js +0 -23
- package/lib/utils/relocateImportsAndRequires/relocateImportsAndRequires.utils.test.js +0 -43
- package/lib/utils/spinner/index.js +0 -3
- package/lib/utils/spinner/spinner.utils.js +0 -40
- package/lib/utils/vercel/index.js +0 -3
- package/lib/utils/vercel/vercel.utils.js +0 -101
- package/lib/utils/vercel/vercel.utils.test.js +0 -110
- package/tasks/process-reports-e2e.js +0 -97
- package/tasks/process-reports-nodejs-apis.js +0 -100
- package/tasks/sync-aliases.js +0 -117
- package/verdaccio/config/config.yaml +0 -50
|
@@ -0,0 +1,1172 @@
|
|
|
1
|
+
import {
|
|
2
|
+
debug,
|
|
3
|
+
generateTimestamp
|
|
4
|
+
} from "./chunk-GVUH3USH.js";
|
|
5
|
+
|
|
6
|
+
// lib/env/runtime.ts
|
|
7
|
+
import { EdgeRuntime } from "edge-runtime";
|
|
8
|
+
import {
|
|
9
|
+
fetchContext,
|
|
10
|
+
FetchEventContext,
|
|
11
|
+
AsyncHooksContext,
|
|
12
|
+
StorageContext,
|
|
13
|
+
EnvVarsContext,
|
|
14
|
+
NetworkListContext,
|
|
15
|
+
fsContext,
|
|
16
|
+
FirewallEventContext
|
|
17
|
+
} from "azion/bundler/polyfills";
|
|
18
|
+
function runtime(code, isFirewallEvent = false) {
|
|
19
|
+
const extend = (context) => {
|
|
20
|
+
context.RESERVED_FETCH = context.fetch.bind(context);
|
|
21
|
+
context.fetch = async (resource, options) => fetchContext(context, resource, options);
|
|
22
|
+
context.FetchEvent = isFirewallEvent ? FirewallEventContext : FetchEventContext;
|
|
23
|
+
context.Response = isFirewallEvent ? Response : context.Response;
|
|
24
|
+
context.eval = eval;
|
|
25
|
+
context.process = { env: process.env };
|
|
26
|
+
context.File = void 0;
|
|
27
|
+
context.WebSocket = void 0;
|
|
28
|
+
context.ASYNC_LOCAL_STORAGE = AsyncHooksContext;
|
|
29
|
+
context.STORAGE_CONTEXT = StorageContext;
|
|
30
|
+
context.ENV_VARS_CONTEXT = EnvVarsContext;
|
|
31
|
+
context.NETWORK_LIST_CONTEXT = NetworkListContext;
|
|
32
|
+
context.FS_CONTEXT = fsContext;
|
|
33
|
+
return context;
|
|
34
|
+
};
|
|
35
|
+
const edgeRuntime = new EdgeRuntime({
|
|
36
|
+
extend,
|
|
37
|
+
initialCode: code,
|
|
38
|
+
codeGeneration: {
|
|
39
|
+
strings: true
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
return edgeRuntime;
|
|
43
|
+
}
|
|
44
|
+
var runtime_default = runtime;
|
|
45
|
+
|
|
46
|
+
// lib/env/server.ts
|
|
47
|
+
import net from "net";
|
|
48
|
+
import { feedback as feedback5 } from "azion/utils/node";
|
|
49
|
+
import chokidar from "chokidar";
|
|
50
|
+
|
|
51
|
+
// lib/env/bundler.ts
|
|
52
|
+
import { feedback } from "azion/utils/node";
|
|
53
|
+
import fs from "fs";
|
|
54
|
+
import fsPromises from "fs/promises";
|
|
55
|
+
import path from "path";
|
|
56
|
+
import {
|
|
57
|
+
ModuleKind,
|
|
58
|
+
ModuleResolutionKind,
|
|
59
|
+
ScriptTarget,
|
|
60
|
+
transpileModule
|
|
61
|
+
} from "typescript";
|
|
62
|
+
import prettier from "prettier";
|
|
63
|
+
import { createRequire } from "module";
|
|
64
|
+
async function writeStore(values, scope = "global") {
|
|
65
|
+
let basePath;
|
|
66
|
+
switch (scope) {
|
|
67
|
+
case "global":
|
|
68
|
+
basePath = globalThis.bundler.tempPath;
|
|
69
|
+
break;
|
|
70
|
+
case "local":
|
|
71
|
+
basePath = path.join(process.cwd());
|
|
72
|
+
break;
|
|
73
|
+
default:
|
|
74
|
+
basePath = scope;
|
|
75
|
+
break;
|
|
76
|
+
}
|
|
77
|
+
const bundlerSesssionStorePath = path.join(basePath, ".azion-bundler.json");
|
|
78
|
+
try {
|
|
79
|
+
await fsPromises.mkdir(basePath, { recursive: true });
|
|
80
|
+
} catch (error) {
|
|
81
|
+
debug.error(error);
|
|
82
|
+
feedback.build.error(
|
|
83
|
+
`An error occurred while creating the ${bundlerSesssionStorePath} folder.`
|
|
84
|
+
);
|
|
85
|
+
throw error;
|
|
86
|
+
}
|
|
87
|
+
try {
|
|
88
|
+
await fsPromises.writeFile(
|
|
89
|
+
bundlerSesssionStorePath,
|
|
90
|
+
JSON.stringify(values, null, 2)
|
|
91
|
+
);
|
|
92
|
+
} catch (error) {
|
|
93
|
+
debug.error(error);
|
|
94
|
+
feedback.build.error(
|
|
95
|
+
`An error occurred while writing the ${bundlerSesssionStorePath} file.`
|
|
96
|
+
);
|
|
97
|
+
throw error;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
async function readStore(scope = "global") {
|
|
101
|
+
let basePath;
|
|
102
|
+
switch (scope) {
|
|
103
|
+
case "global":
|
|
104
|
+
basePath = globalThis.bundler.tempPath;
|
|
105
|
+
break;
|
|
106
|
+
case "local":
|
|
107
|
+
basePath = path.join(process.cwd());
|
|
108
|
+
break;
|
|
109
|
+
default:
|
|
110
|
+
basePath = scope;
|
|
111
|
+
break;
|
|
112
|
+
}
|
|
113
|
+
if (!basePath) return {};
|
|
114
|
+
const bundlerStoreFilePath = path.join(basePath, ".azion-bundler.json");
|
|
115
|
+
try {
|
|
116
|
+
await fsPromises.access(bundlerStoreFilePath);
|
|
117
|
+
const fileContents = await fsPromises.readFile(
|
|
118
|
+
bundlerStoreFilePath,
|
|
119
|
+
"utf8"
|
|
120
|
+
);
|
|
121
|
+
return JSON.parse(fileContents);
|
|
122
|
+
} catch (error) {
|
|
123
|
+
return {};
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
function handleDependencyError(error, configPath) {
|
|
127
|
+
if (error.message.includes("ERR_MODULE_NOT_FOUND")) {
|
|
128
|
+
const missingPackage = error.message.match(/'([^']+)'/)?.[1];
|
|
129
|
+
if (missingPackage) {
|
|
130
|
+
feedback.build.error(
|
|
131
|
+
`Missing dependency: ${missingPackage}. Please install it using 'npm install ${missingPackage}' or 'yarn add ${missingPackage}'.`
|
|
132
|
+
);
|
|
133
|
+
} else {
|
|
134
|
+
feedback.build.error(
|
|
135
|
+
`A required dependency is missing. Please ensure all dependencies are installed.`
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
debug.error(
|
|
139
|
+
`Failed to load configuration from ${configPath}. ${error.message}`
|
|
140
|
+
);
|
|
141
|
+
} else {
|
|
142
|
+
throw error;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
async function readUserConfig(configPath) {
|
|
146
|
+
const require2 = createRequire(import.meta.url);
|
|
147
|
+
const extensions = [".js", ".mjs", ".cjs", ".ts"];
|
|
148
|
+
const configName = "azion.config";
|
|
149
|
+
let resolvedConfigPath;
|
|
150
|
+
if (configPath) {
|
|
151
|
+
resolvedConfigPath = path.isAbsolute(configPath) ? configPath : path.join(process.cwd(), configPath);
|
|
152
|
+
if (!fs.existsSync(resolvedConfigPath)) {
|
|
153
|
+
return null;
|
|
154
|
+
}
|
|
155
|
+
} else {
|
|
156
|
+
for (const ext of extensions) {
|
|
157
|
+
const testPath = path.join(process.cwd(), `${configName}${ext}`);
|
|
158
|
+
if (fs.existsSync(testPath)) {
|
|
159
|
+
resolvedConfigPath = testPath;
|
|
160
|
+
break;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
if (!resolvedConfigPath) {
|
|
165
|
+
return null;
|
|
166
|
+
}
|
|
167
|
+
const extension = path.extname(resolvedConfigPath);
|
|
168
|
+
let configModule;
|
|
169
|
+
try {
|
|
170
|
+
switch (extension) {
|
|
171
|
+
case ".ts":
|
|
172
|
+
const tsContent = await fsPromises.readFile(
|
|
173
|
+
resolvedConfigPath,
|
|
174
|
+
"utf-8"
|
|
175
|
+
);
|
|
176
|
+
const jsContent = transpileModule(tsContent, {
|
|
177
|
+
compilerOptions: {
|
|
178
|
+
module: ModuleKind.CommonJS,
|
|
179
|
+
target: ScriptTarget.ES2020,
|
|
180
|
+
moduleResolution: ModuleResolutionKind.Node10
|
|
181
|
+
}
|
|
182
|
+
}).outputText;
|
|
183
|
+
const tempJsPath = resolvedConfigPath.replace(".ts", ".temp.js");
|
|
184
|
+
await fsPromises.writeFile(tempJsPath, jsContent);
|
|
185
|
+
try {
|
|
186
|
+
configModule = require2(tempJsPath);
|
|
187
|
+
} finally {
|
|
188
|
+
await fsPromises.unlink(tempJsPath);
|
|
189
|
+
}
|
|
190
|
+
break;
|
|
191
|
+
case ".mjs":
|
|
192
|
+
configModule = await import(resolvedConfigPath);
|
|
193
|
+
break;
|
|
194
|
+
case ".cjs":
|
|
195
|
+
case ".js":
|
|
196
|
+
try {
|
|
197
|
+
configModule = await import(resolvedConfigPath);
|
|
198
|
+
} catch (error) {
|
|
199
|
+
if (error.message === "ERR_REQUIRE_ESM") {
|
|
200
|
+
configModule = require2(resolvedConfigPath);
|
|
201
|
+
throw error;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
break;
|
|
205
|
+
default:
|
|
206
|
+
throw new Error(`Unsupported file extension: ${extension}`);
|
|
207
|
+
}
|
|
208
|
+
return configModule.default || configModule;
|
|
209
|
+
} catch (error) {
|
|
210
|
+
if (error.message.includes("ERR_MODULE_NOT_FOUND")) {
|
|
211
|
+
handleDependencyError(error, resolvedConfigPath);
|
|
212
|
+
return null;
|
|
213
|
+
}
|
|
214
|
+
throw error;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
function isCommonJS() {
|
|
218
|
+
const packageJsonPath = path.join(process.cwd(), "package.json");
|
|
219
|
+
if (fs.existsSync(packageJsonPath)) {
|
|
220
|
+
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf8"));
|
|
221
|
+
return packageJson.type !== "module";
|
|
222
|
+
}
|
|
223
|
+
return true;
|
|
224
|
+
}
|
|
225
|
+
async function writeUserConfig(config) {
|
|
226
|
+
const useCommonJS = isCommonJS();
|
|
227
|
+
const extension = useCommonJS ? ".cjs" : ".mjs";
|
|
228
|
+
const isTypeScript = config.build?.preset === "typescript";
|
|
229
|
+
const configExtension = isTypeScript ? ".ts" : extension;
|
|
230
|
+
const configPath = path.join(process.cwd(), `azion.config${configExtension}`);
|
|
231
|
+
const moduleExportStyle = isTypeScript ? "export default" : useCommonJS ? "module.exports =" : "export default";
|
|
232
|
+
const configComment = `/**
|
|
233
|
+
* This file was automatically generated based on your preset configuration.
|
|
234
|
+
*
|
|
235
|
+
* For better type checking and IntelliSense:
|
|
236
|
+
* 1. Install azion as dev dependency:
|
|
237
|
+
* npm install -D azion
|
|
238
|
+
*
|
|
239
|
+
* 2. Use defineConfig:
|
|
240
|
+
* import { defineConfig } from 'azion'
|
|
241
|
+
*
|
|
242
|
+
* For more configuration options, visit:
|
|
243
|
+
* https://github.com/aziontech/azion
|
|
244
|
+
*/
|
|
245
|
+
|
|
246
|
+
`;
|
|
247
|
+
const replacer = (key, value) => {
|
|
248
|
+
if (typeof value === "function") {
|
|
249
|
+
return `__FUNCTION_START__${value.toString()}__FUNCTION_END__`;
|
|
250
|
+
}
|
|
251
|
+
return value;
|
|
252
|
+
};
|
|
253
|
+
const formattedContent = await prettier.format(
|
|
254
|
+
configComment + `${moduleExportStyle} ${JSON.stringify(config, replacer, 2)};`,
|
|
255
|
+
{
|
|
256
|
+
parser: "babel",
|
|
257
|
+
semi: false,
|
|
258
|
+
singleQuote: true,
|
|
259
|
+
trailingComma: "none"
|
|
260
|
+
}
|
|
261
|
+
);
|
|
262
|
+
if (!fs.existsSync(configPath)) {
|
|
263
|
+
await fsPromises.writeFile(configPath, formattedContent);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
var bundler_default = {
|
|
267
|
+
writeStore,
|
|
268
|
+
readStore,
|
|
269
|
+
readUserConfig,
|
|
270
|
+
writeUserConfig
|
|
271
|
+
};
|
|
272
|
+
|
|
273
|
+
// lib/commands/build/build.ts
|
|
274
|
+
import { feedback as feedback4 } from "azion/utils/node";
|
|
275
|
+
import { writeFile } from "fs/promises";
|
|
276
|
+
|
|
277
|
+
// lib/commands/build/utils.ts
|
|
278
|
+
import { join } from "path";
|
|
279
|
+
import fs2 from "fs";
|
|
280
|
+
import { getPackageManager } from "azion/utils/node";
|
|
281
|
+
var PackageJsonError = class extends Error {
|
|
282
|
+
constructor(message, code) {
|
|
283
|
+
super(message);
|
|
284
|
+
this.code = code;
|
|
285
|
+
this.name = "PackageJsonError";
|
|
286
|
+
}
|
|
287
|
+
};
|
|
288
|
+
var DependenciesError = class extends Error {
|
|
289
|
+
constructor(packageManager) {
|
|
290
|
+
super(`Please install dependencies using ${packageManager}`);
|
|
291
|
+
this.name = "DependenciesError";
|
|
292
|
+
}
|
|
293
|
+
};
|
|
294
|
+
var readPackageJson = () => {
|
|
295
|
+
const packageJsonPath = join(process.cwd(), "package.json");
|
|
296
|
+
try {
|
|
297
|
+
const content = fs2.readFileSync(packageJsonPath, "utf8");
|
|
298
|
+
return JSON.parse(content);
|
|
299
|
+
} catch (error) {
|
|
300
|
+
throw new PackageJsonError(
|
|
301
|
+
"Failed to read package.json",
|
|
302
|
+
error.code
|
|
303
|
+
);
|
|
304
|
+
}
|
|
305
|
+
};
|
|
306
|
+
var hasNodeModulesDirectory = async () => {
|
|
307
|
+
const nodeModulesPath = join(process.cwd(), "node_modules");
|
|
308
|
+
try {
|
|
309
|
+
const stats = await fs2.promises.stat(nodeModulesPath);
|
|
310
|
+
return stats.isDirectory();
|
|
311
|
+
} catch (error) {
|
|
312
|
+
return false;
|
|
313
|
+
}
|
|
314
|
+
};
|
|
315
|
+
var checkDependencies = async () => {
|
|
316
|
+
let projectJson;
|
|
317
|
+
try {
|
|
318
|
+
projectJson = readPackageJson();
|
|
319
|
+
} catch (error) {
|
|
320
|
+
if (error instanceof PackageJsonError && error.code === "ENOENT") {
|
|
321
|
+
return;
|
|
322
|
+
}
|
|
323
|
+
throw error;
|
|
324
|
+
}
|
|
325
|
+
if (projectJson && (projectJson.dependencies || projectJson.devDependencies)) {
|
|
326
|
+
const pkgManager = await getPackageManager();
|
|
327
|
+
const nodeModulesExists = await hasNodeModulesDirectory();
|
|
328
|
+
if (!nodeModulesExists) {
|
|
329
|
+
throw new DependenciesError(pkgManager);
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
};
|
|
333
|
+
function resolveConfigPriority({
|
|
334
|
+
inputValue,
|
|
335
|
+
fileValue,
|
|
336
|
+
storeValue,
|
|
337
|
+
defaultValue
|
|
338
|
+
}) {
|
|
339
|
+
return inputValue ?? fileValue ?? storeValue ?? defaultValue;
|
|
340
|
+
}
|
|
341
|
+
function resolvePresetPriority({
|
|
342
|
+
inputValue,
|
|
343
|
+
fileValue,
|
|
344
|
+
storeValue,
|
|
345
|
+
defaultValue
|
|
346
|
+
}) {
|
|
347
|
+
if (typeof fileValue === "object" && fileValue.metadata?.name) {
|
|
348
|
+
return fileValue;
|
|
349
|
+
}
|
|
350
|
+
if (typeof storeValue === "object" && storeValue.metadata?.name) {
|
|
351
|
+
return storeValue;
|
|
352
|
+
}
|
|
353
|
+
return resolveConfigPriority({
|
|
354
|
+
inputValue,
|
|
355
|
+
fileValue,
|
|
356
|
+
storeValue,
|
|
357
|
+
defaultValue
|
|
358
|
+
});
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
// lib/commands/build/modules/config/config.ts
|
|
362
|
+
import { join as join2 } from "path";
|
|
363
|
+
var setupBuildConfig = (azionConfig, preset) => {
|
|
364
|
+
const fileExtension = `${preset.metadata.ext || "js"}`;
|
|
365
|
+
const tempFile = `azion-${generateTimestamp()}.temp.${fileExtension}`;
|
|
366
|
+
const buildConfigSetup = {
|
|
367
|
+
...azionConfig.build,
|
|
368
|
+
entry: join2(process.cwd(), tempFile),
|
|
369
|
+
preset,
|
|
370
|
+
setup: {
|
|
371
|
+
contentToInject: void 0,
|
|
372
|
+
defineVars: {}
|
|
373
|
+
}
|
|
374
|
+
};
|
|
375
|
+
buildConfigSetup.polyfills = Boolean(azionConfig.build?.polyfills);
|
|
376
|
+
buildConfigSetup.worker = Boolean(azionConfig.build?.worker);
|
|
377
|
+
return buildConfigSetup;
|
|
378
|
+
};
|
|
379
|
+
|
|
380
|
+
// lib/commands/build/modules/preset/preset.ts
|
|
381
|
+
import * as presets2 from "azion/presets";
|
|
382
|
+
import * as utilsNode from "azion/utils/node";
|
|
383
|
+
|
|
384
|
+
// lib/commands/build/modules/preset/infer/infer-preset.ts
|
|
385
|
+
import * as presets from "azion/presets";
|
|
386
|
+
import { join as join3 } from "path";
|
|
387
|
+
import fs3 from "fs";
|
|
388
|
+
import { extname } from "path";
|
|
389
|
+
|
|
390
|
+
// lib/commands/build/modules/preset/frameworks/frameworks-info.ts
|
|
391
|
+
import { listFrameworks as netlifyListFrameworks } from "@netlify/framework-info";
|
|
392
|
+
var listFrameworks = ({
|
|
393
|
+
projectDir = process.cwd()
|
|
394
|
+
}) => {
|
|
395
|
+
return netlifyListFrameworks({
|
|
396
|
+
projectDir
|
|
397
|
+
});
|
|
398
|
+
};
|
|
399
|
+
|
|
400
|
+
// lib/commands/build/modules/preset/frameworks/index.ts
|
|
401
|
+
var frameworks_default = { listFrameworks };
|
|
402
|
+
|
|
403
|
+
// lib/commands/build/modules/preset/infer/infer-preset.ts
|
|
404
|
+
async function inferPreset() {
|
|
405
|
+
try {
|
|
406
|
+
const detectedFramework = await frameworks_default.listFrameworks({
|
|
407
|
+
projectDir: process.cwd()
|
|
408
|
+
});
|
|
409
|
+
if (detectedFramework[0]?.id) {
|
|
410
|
+
const hasPreset = Object.values(presets).some(
|
|
411
|
+
(preset) => preset.metadata?.name === detectedFramework[0].id
|
|
412
|
+
);
|
|
413
|
+
if (hasPreset) return detectedFramework[0].id;
|
|
414
|
+
}
|
|
415
|
+
const tsConfigPath = join3(process.cwd(), "tsconfig.json");
|
|
416
|
+
const tsConfigExists = fs3.existsSync(tsConfigPath);
|
|
417
|
+
if (tsConfigExists) return "typescript";
|
|
418
|
+
const files = fs3.readdirSync(process.cwd());
|
|
419
|
+
const hasTypeScriptFiles = files.some(
|
|
420
|
+
(file) => [".ts", ".tsx"].includes(extname(file))
|
|
421
|
+
);
|
|
422
|
+
if (hasTypeScriptFiles) return "typescript";
|
|
423
|
+
return "javascript";
|
|
424
|
+
} catch (error) {
|
|
425
|
+
return "javascript";
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
var infer_preset_default = { inferPreset };
|
|
429
|
+
|
|
430
|
+
// lib/commands/build/modules/preset/preset.ts
|
|
431
|
+
var validatePreset = (preset) => {
|
|
432
|
+
if (!preset.metadata?.name || !preset.config) {
|
|
433
|
+
throw new Error("Preset must have name and config.");
|
|
434
|
+
}
|
|
435
|
+
const presetName = preset.metadata.name.toLowerCase();
|
|
436
|
+
const validPresets = Object.keys(presets2);
|
|
437
|
+
if (!validPresets.includes(presetName)) {
|
|
438
|
+
throw new Error(`Invalid build preset name: '${presetName}'`);
|
|
439
|
+
}
|
|
440
|
+
return true;
|
|
441
|
+
};
|
|
442
|
+
var loadPreset = async (presetName) => {
|
|
443
|
+
const normalizedName = presetName.toLowerCase();
|
|
444
|
+
return presets2[normalizedName];
|
|
445
|
+
};
|
|
446
|
+
var resolvePreset = async (input) => {
|
|
447
|
+
if (!input) {
|
|
448
|
+
utilsNode.feedback.build.info(
|
|
449
|
+
"No preset specified, using automatic detection..."
|
|
450
|
+
);
|
|
451
|
+
input = await infer_preset_default.inferPreset();
|
|
452
|
+
}
|
|
453
|
+
const preset = typeof input === "string" ? await loadPreset(input) : input;
|
|
454
|
+
validatePreset(preset);
|
|
455
|
+
return preset;
|
|
456
|
+
};
|
|
457
|
+
|
|
458
|
+
// lib/commands/build/modules/prebuild/utils.ts
|
|
459
|
+
import fs4 from "fs";
|
|
460
|
+
import fsPromises2 from "fs/promises";
|
|
461
|
+
import { join as join4 } from "path";
|
|
462
|
+
var injectWorkerMemoryFiles = async ({
|
|
463
|
+
namespace,
|
|
464
|
+
property,
|
|
465
|
+
dirs
|
|
466
|
+
}) => {
|
|
467
|
+
const result = {};
|
|
468
|
+
if (dirs.length === 0) {
|
|
469
|
+
return "";
|
|
470
|
+
}
|
|
471
|
+
for (const dir of dirs) {
|
|
472
|
+
const files = await fsPromises2.readdir(dir);
|
|
473
|
+
for (const file of files) {
|
|
474
|
+
const filePath = join4(dir, file);
|
|
475
|
+
const stats = await fsPromises2.stat(filePath);
|
|
476
|
+
if (stats.isDirectory()) {
|
|
477
|
+
const subDirContent = await injectWorkerMemoryFiles({
|
|
478
|
+
namespace,
|
|
479
|
+
property,
|
|
480
|
+
dirs: [filePath]
|
|
481
|
+
});
|
|
482
|
+
Object.assign(result, subDirContent);
|
|
483
|
+
} else if (stats.isFile()) {
|
|
484
|
+
const bufferContent = await fsPromises2.readFile(filePath);
|
|
485
|
+
let key = filePath;
|
|
486
|
+
if (!filePath.startsWith("/")) key = `/${filePath}`;
|
|
487
|
+
result[key] = { content: bufferContent.toString("base64") };
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
return `globalThis.${namespace}.${property}=${JSON.stringify(result)};`;
|
|
492
|
+
};
|
|
493
|
+
var copyFilesToLocalEdgeStorage = ({
|
|
494
|
+
dirs,
|
|
495
|
+
prefix,
|
|
496
|
+
outputPath
|
|
497
|
+
}) => {
|
|
498
|
+
dirs.forEach((dir) => {
|
|
499
|
+
const targetPath = prefix ? dir.replace(prefix, "") : dir;
|
|
500
|
+
const fullTargetPath = join4(outputPath, targetPath);
|
|
501
|
+
if (!fs4.existsSync(fullTargetPath)) {
|
|
502
|
+
fs4.mkdirSync(fullTargetPath, { recursive: true });
|
|
503
|
+
}
|
|
504
|
+
fs4.cpSync(dir, fullTargetPath, { recursive: true });
|
|
505
|
+
});
|
|
506
|
+
};
|
|
507
|
+
var injectWorkerGlobals = ({
|
|
508
|
+
namespace,
|
|
509
|
+
property,
|
|
510
|
+
vars
|
|
511
|
+
}) => Object.entries(vars).reduce(
|
|
512
|
+
(acc, [key, value]) => {
|
|
513
|
+
const propPath = property ? `${namespace}.${property}` : namespace;
|
|
514
|
+
return `${acc} globalThis.${propPath}.${key}=${value};`;
|
|
515
|
+
},
|
|
516
|
+
property ? `globalThis.${namespace}.${property}={};` : `globalThis.${namespace}={};`
|
|
517
|
+
);
|
|
518
|
+
var injectWorkerPathPrefix = async ({
|
|
519
|
+
namespace,
|
|
520
|
+
property,
|
|
521
|
+
prefix
|
|
522
|
+
}) => {
|
|
523
|
+
const formattedPrefix = prefix && typeof prefix === "string" && prefix !== "" ? prefix : '""';
|
|
524
|
+
return `globalThis.${namespace} = { ...globalThis.${namespace}, ${property}: '${formattedPrefix}'};`;
|
|
525
|
+
};
|
|
526
|
+
var utils_default = {
|
|
527
|
+
injectWorkerMemoryFiles,
|
|
528
|
+
copyFilesToLocalEdgeStorage,
|
|
529
|
+
injectWorkerGlobals,
|
|
530
|
+
injectWorkerPathPrefix
|
|
531
|
+
};
|
|
532
|
+
|
|
533
|
+
// lib/commands/build/modules/prebuild/prebuild.ts
|
|
534
|
+
var EDGE_STORAGE = ".edge/storage";
|
|
535
|
+
var BUNDLER_NAMESPACE = "bundler";
|
|
536
|
+
var DEFAULT_PREBUILD_RESULT = {
|
|
537
|
+
filesToInject: [],
|
|
538
|
+
injection: {
|
|
539
|
+
globals: {},
|
|
540
|
+
entry: "",
|
|
541
|
+
banner: ""
|
|
542
|
+
},
|
|
543
|
+
bundler: {
|
|
544
|
+
defineVars: {},
|
|
545
|
+
plugins: []
|
|
546
|
+
}
|
|
547
|
+
};
|
|
548
|
+
var executePrebuild = async ({
|
|
549
|
+
buildConfig,
|
|
550
|
+
ctx
|
|
551
|
+
}) => {
|
|
552
|
+
const result = await buildConfig.preset.prebuild?.(buildConfig, ctx) || DEFAULT_PREBUILD_RESULT;
|
|
553
|
+
const globalThisWithVars = utils_default.injectWorkerGlobals({
|
|
554
|
+
namespace: BUNDLER_NAMESPACE,
|
|
555
|
+
// Transform globals object:
|
|
556
|
+
// 1. Convert object to entries
|
|
557
|
+
// 2. Remove any entries with undefined values
|
|
558
|
+
// 3. Ensure remaining values are typed as string
|
|
559
|
+
vars: Object.fromEntries(
|
|
560
|
+
Object.entries(result.injection?.globals || {}).filter(([_, v]) => v !== void 0).map(([k, v]) => [k, v])
|
|
561
|
+
)
|
|
562
|
+
});
|
|
563
|
+
const memoryFiles = await utils_default.injectWorkerMemoryFiles({
|
|
564
|
+
namespace: BUNDLER_NAMESPACE,
|
|
565
|
+
property: "__FILES__",
|
|
566
|
+
dirs: buildConfig.memoryFS?.injectionDirs || []
|
|
567
|
+
});
|
|
568
|
+
if (buildConfig.memoryFS) {
|
|
569
|
+
utils_default.copyFilesToLocalEdgeStorage({
|
|
570
|
+
dirs: buildConfig.memoryFS?.injectionDirs,
|
|
571
|
+
prefix: buildConfig.memoryFS?.removePathPrefix,
|
|
572
|
+
outputPath: EDGE_STORAGE
|
|
573
|
+
});
|
|
574
|
+
}
|
|
575
|
+
const pathPrefix = await utils_default.injectWorkerPathPrefix({
|
|
576
|
+
namespace: BUNDLER_NAMESPACE,
|
|
577
|
+
property: "FS_PATH_PREFIX_TO_REMOVE",
|
|
578
|
+
prefix: buildConfig.memoryFS?.removePathPrefix || ""
|
|
579
|
+
});
|
|
580
|
+
const globalThisWithInjections = `${globalThisWithVars}${pathPrefix}
|
|
581
|
+
${memoryFiles}`;
|
|
582
|
+
return {
|
|
583
|
+
filesToInject: result.filesToInject || [],
|
|
584
|
+
injection: {
|
|
585
|
+
globals: result.injection?.globals || {},
|
|
586
|
+
entry: "",
|
|
587
|
+
banner: globalThisWithInjections
|
|
588
|
+
},
|
|
589
|
+
bundler: {
|
|
590
|
+
defineVars: result.bundler?.defineVars || {},
|
|
591
|
+
plugins: result.bundler?.plugins || []
|
|
592
|
+
}
|
|
593
|
+
};
|
|
594
|
+
};
|
|
595
|
+
|
|
596
|
+
// lib/commands/build/modules/core/core.ts
|
|
597
|
+
import fsPromises3 from "fs/promises";
|
|
598
|
+
|
|
599
|
+
// lib/commands/build/modules/core/bundler-execute/index.ts
|
|
600
|
+
import {
|
|
601
|
+
createAzionESBuildConfig,
|
|
602
|
+
executeESBuildBuild,
|
|
603
|
+
createAzionWebpackConfig,
|
|
604
|
+
executeWebpackBuild
|
|
605
|
+
} from "azion/bundler";
|
|
606
|
+
var createAzionESBuildConfigWrapper = (buildConfig, ctx) => {
|
|
607
|
+
return createAzionESBuildConfig(buildConfig, ctx);
|
|
608
|
+
};
|
|
609
|
+
var createAzionWebpackConfigWrapper = (buildConfig, ctx) => {
|
|
610
|
+
return createAzionWebpackConfig(buildConfig, ctx);
|
|
611
|
+
};
|
|
612
|
+
var executeESBuildBuildWrapper = async (esbuildConfig) => {
|
|
613
|
+
return executeESBuildBuild(esbuildConfig);
|
|
614
|
+
};
|
|
615
|
+
var executeWebpackBuildWrapper = async (webpackConfig) => {
|
|
616
|
+
return executeWebpackBuild(webpackConfig);
|
|
617
|
+
};
|
|
618
|
+
var bundler_execute_default = {
|
|
619
|
+
createAzionESBuildConfigWrapper,
|
|
620
|
+
createAzionWebpackConfigWrapper,
|
|
621
|
+
executeESBuildBuildWrapper,
|
|
622
|
+
executeWebpackBuildWrapper
|
|
623
|
+
};
|
|
624
|
+
|
|
625
|
+
// lib/commands/build/modules/core/utils.ts
|
|
626
|
+
var moveImportsToTopLevel = (code) => {
|
|
627
|
+
const importRegex = /import\s+.*?from\s*['"](.*?)['"];?/g;
|
|
628
|
+
const requireRegex = /(const\s+.*?=\s*require\(.*\).*);/g;
|
|
629
|
+
const importsList = (code.match(importRegex) || []).map(
|
|
630
|
+
(match) => match.trim()
|
|
631
|
+
);
|
|
632
|
+
const requiresList = (code.match(requireRegex) || []).map(
|
|
633
|
+
(match) => match.trim()
|
|
634
|
+
);
|
|
635
|
+
let newCode = code.replace(importRegex, "").replace(requireRegex, "").replace(/\n\s*\n\s*\n/g, "\n\n");
|
|
636
|
+
if (importsList.length || requiresList.length) {
|
|
637
|
+
newCode = `${[...importsList, ...requiresList].join("\n")}
|
|
638
|
+
|
|
639
|
+
${newCode.trim()}`;
|
|
640
|
+
}
|
|
641
|
+
return newCode;
|
|
642
|
+
};
|
|
643
|
+
|
|
644
|
+
// lib/commands/build/modules/core/core.ts
|
|
645
|
+
import fs5 from "fs";
|
|
646
|
+
var injectHybridFsPolyfill = (code, buildConfig, ctx) => {
|
|
647
|
+
if (buildConfig.polyfills && ctx.production) {
|
|
648
|
+
return `import SRC_NODE_FS from "node:fs";
|
|
649
|
+
${code}`;
|
|
650
|
+
}
|
|
651
|
+
return code;
|
|
652
|
+
};
|
|
653
|
+
var executeBuild = async ({
|
|
654
|
+
buildConfig,
|
|
655
|
+
prebuildResult,
|
|
656
|
+
ctx
|
|
657
|
+
}) => {
|
|
658
|
+
try {
|
|
659
|
+
if (prebuildResult.filesToInject.length > 0) {
|
|
660
|
+
const entryContent = await fsPromises3.readFile(
|
|
661
|
+
buildConfig.entry,
|
|
662
|
+
"utf-8"
|
|
663
|
+
);
|
|
664
|
+
const filesContent = prebuildResult.filesToInject.reduce(
|
|
665
|
+
(accumulator, filePath) => `${accumulator} ${fs5.readFileSync(filePath, "utf-8")}`,
|
|
666
|
+
" "
|
|
667
|
+
);
|
|
668
|
+
const contentWithInjection = `${filesContent} ${entryContent}`;
|
|
669
|
+
const contentWithTopLevelImports = moveImportsToTopLevel(contentWithInjection);
|
|
670
|
+
await fsPromises3.writeFile(buildConfig.entry, contentWithTopLevelImports);
|
|
671
|
+
}
|
|
672
|
+
const bundlerConfig = {
|
|
673
|
+
...buildConfig,
|
|
674
|
+
preset: buildConfig.preset,
|
|
675
|
+
setup: {
|
|
676
|
+
contentToInject: prebuildResult.injection.banner,
|
|
677
|
+
defineVars: Object.fromEntries(
|
|
678
|
+
Object.entries(prebuildResult.bundler.defineVars).filter(([_, v]) => v !== void 0).map(([k, v]) => [k, v])
|
|
679
|
+
)
|
|
680
|
+
}
|
|
681
|
+
};
|
|
682
|
+
const bundler = buildConfig.bundler;
|
|
683
|
+
switch (bundler) {
|
|
684
|
+
case "esbuild": {
|
|
685
|
+
const esbuildConfig = bundler_execute_default.createAzionESBuildConfigWrapper(
|
|
686
|
+
bundlerConfig,
|
|
687
|
+
ctx
|
|
688
|
+
);
|
|
689
|
+
await bundler_execute_default.executeESBuildBuildWrapper(esbuildConfig);
|
|
690
|
+
break;
|
|
691
|
+
}
|
|
692
|
+
case "webpack": {
|
|
693
|
+
const webpackConfig = bundler_execute_default.createAzionWebpackConfigWrapper(
|
|
694
|
+
bundlerConfig,
|
|
695
|
+
ctx
|
|
696
|
+
);
|
|
697
|
+
await bundler_execute_default.executeWebpackBuildWrapper(webpackConfig);
|
|
698
|
+
break;
|
|
699
|
+
}
|
|
700
|
+
default:
|
|
701
|
+
throw new Error(`Unsupported bundler: ${bundler}`);
|
|
702
|
+
}
|
|
703
|
+
let bundledCode = "";
|
|
704
|
+
if (ctx.production === true) {
|
|
705
|
+
bundledCode = await fsPromises3.readFile(ctx.output, "utf-8");
|
|
706
|
+
bundledCode = injectHybridFsPolyfill(bundledCode, buildConfig, ctx);
|
|
707
|
+
await fsPromises3.writeFile(ctx.output, bundledCode);
|
|
708
|
+
}
|
|
709
|
+
return bundledCode;
|
|
710
|
+
} catch (error) {
|
|
711
|
+
return Promise.reject(error);
|
|
712
|
+
}
|
|
713
|
+
};
|
|
714
|
+
|
|
715
|
+
// lib/commands/build/modules/postbuild/postbuild.ts
|
|
716
|
+
var executePostbuild = async ({
|
|
717
|
+
buildConfig,
|
|
718
|
+
ctx
|
|
719
|
+
}) => {
|
|
720
|
+
const { postbuild } = buildConfig.preset;
|
|
721
|
+
if (postbuild) {
|
|
722
|
+
const outputWorker = ctx.production === false ? ctx.output.replace(".js", ".dev.js") : ctx.output;
|
|
723
|
+
await postbuild(buildConfig, { ...ctx, output: outputWorker });
|
|
724
|
+
}
|
|
725
|
+
};
|
|
726
|
+
|
|
727
|
+
// lib/commands/build/modules/environment/utils.ts
|
|
728
|
+
import lodash from "lodash";
|
|
729
|
+
var mergeConfigWithUserOverrides = (baseConfig, userConfig) => {
|
|
730
|
+
const customizer = (objValue, srcValue) => {
|
|
731
|
+
if (objValue && !objValue.build) {
|
|
732
|
+
return srcValue;
|
|
733
|
+
}
|
|
734
|
+
return void 0;
|
|
735
|
+
};
|
|
736
|
+
return lodash.mergeWith({}, baseConfig, userConfig, customizer);
|
|
737
|
+
};
|
|
738
|
+
var utils_default2 = { mergeConfigWithUserOverrides };
|
|
739
|
+
|
|
740
|
+
// lib/commands/build/modules/environment/environment.ts
|
|
741
|
+
var setEnvironment = async ({
|
|
742
|
+
config: userConfig,
|
|
743
|
+
preset,
|
|
744
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
745
|
+
ctx
|
|
746
|
+
}) => {
|
|
747
|
+
try {
|
|
748
|
+
const { config: presetConfig } = preset;
|
|
749
|
+
const mergedConfig = utils_default2.mergeConfigWithUserOverrides(
|
|
750
|
+
presetConfig,
|
|
751
|
+
userConfig
|
|
752
|
+
);
|
|
753
|
+
if (!mergedConfig.build?.preset) {
|
|
754
|
+
mergedConfig.build = {
|
|
755
|
+
...mergedConfig.build,
|
|
756
|
+
preset: preset.metadata.name,
|
|
757
|
+
...!preset.handler && preset.config.build?.entry && {
|
|
758
|
+
entry: preset.config.build.entry
|
|
759
|
+
}
|
|
760
|
+
};
|
|
761
|
+
}
|
|
762
|
+
const hasUserConfig = await env_default.readUserConfig();
|
|
763
|
+
if (!hasUserConfig) await env_default.writeUserConfig(mergedConfig);
|
|
764
|
+
const storeConfig = {
|
|
765
|
+
preset: mergedConfig.build.preset,
|
|
766
|
+
bundler: mergedConfig.build?.bundler,
|
|
767
|
+
polyfills: mergedConfig.build?.polyfills,
|
|
768
|
+
worker: mergedConfig.build?.worker,
|
|
769
|
+
entry: mergedConfig.build.entry
|
|
770
|
+
};
|
|
771
|
+
await env_default.writeStore(storeConfig);
|
|
772
|
+
} catch (error) {
|
|
773
|
+
throw new Error(`Failed to set environment: ${error.message}`);
|
|
774
|
+
}
|
|
775
|
+
};
|
|
776
|
+
|
|
777
|
+
// lib/commands/build/modules/worker/worker.ts
|
|
778
|
+
import fsPromises4 from "fs/promises";
|
|
779
|
+
import { dirname } from "path";
|
|
780
|
+
|
|
781
|
+
// lib/commands/build/modules/worker/utils.ts
|
|
782
|
+
var createEventHandlerCode = (entrypoint) => {
|
|
783
|
+
return `
|
|
784
|
+
import moduleOrFunction from '${entrypoint}';
|
|
785
|
+
|
|
786
|
+
// Handle the case where import returns a function directly
|
|
787
|
+
const module = typeof moduleOrFunction === 'function'
|
|
788
|
+
? { default: moduleOrFunction }
|
|
789
|
+
: moduleOrFunction;
|
|
790
|
+
|
|
791
|
+
// Check standard handlers first
|
|
792
|
+
const hasFirewallHandler = module.firewall || (module.default && module.default.firewall);
|
|
793
|
+
|
|
794
|
+
// Detect if the module exports a function directly (legacy)
|
|
795
|
+
const isLegacyDefaultFunction = typeof module.default === 'function';
|
|
796
|
+
|
|
797
|
+
let eventType = 'fetch';
|
|
798
|
+
let handler;
|
|
799
|
+
|
|
800
|
+
if (hasFirewallHandler) {
|
|
801
|
+
eventType = 'firewall';
|
|
802
|
+
handler = module.firewall || module.default.firewall;
|
|
803
|
+
} else if (isLegacyDefaultFunction) {
|
|
804
|
+
// Legacy case: function exported directly as default
|
|
805
|
+
handler = module.default;
|
|
806
|
+
} else {
|
|
807
|
+
// Normal case: look for fetch handler
|
|
808
|
+
handler = module.fetch || (module.default && module.default.fetch);
|
|
809
|
+
}
|
|
810
|
+
|
|
811
|
+
if (!handler) {
|
|
812
|
+
throw new Error("Handler not found in module");
|
|
813
|
+
}
|
|
814
|
+
|
|
815
|
+
addEventListener(eventType, (event) => {
|
|
816
|
+
if (eventType === 'fetch' && !hasFirewallHandler) {
|
|
817
|
+
event.respondWith((async function() {
|
|
818
|
+
try {
|
|
819
|
+
return handler(event);
|
|
820
|
+
} catch (error) {
|
|
821
|
+
return new Response(\`Error: \${error.message}\`, { status: 500 });
|
|
822
|
+
}
|
|
823
|
+
})());
|
|
824
|
+
}
|
|
825
|
+
else {
|
|
826
|
+
(async function() {
|
|
827
|
+
try {
|
|
828
|
+
return handler(event);
|
|
829
|
+
} catch (error) {
|
|
830
|
+
return new Response(\`Error: \${error.message}\`, { status: 500 });
|
|
831
|
+
}
|
|
832
|
+
})();
|
|
833
|
+
}
|
|
834
|
+
});
|
|
835
|
+
`;
|
|
836
|
+
};
|
|
837
|
+
var utils_default3 = { createEventHandlerCode };
|
|
838
|
+
|
|
839
|
+
// lib/commands/build/modules/worker/worker.ts
|
|
840
|
+
var setupWorkerCode = async (buildConfig, ctx) => {
|
|
841
|
+
try {
|
|
842
|
+
if (buildConfig.worker) {
|
|
843
|
+
return fsPromises4.readFile(ctx.entrypoint, "utf-8");
|
|
844
|
+
}
|
|
845
|
+
const wrapperCode = utils_default3.createEventHandlerCode(ctx.entrypoint);
|
|
846
|
+
await fsPromises4.mkdir(dirname(ctx.output), { recursive: true });
|
|
847
|
+
if (ctx.production !== false) {
|
|
848
|
+
await fsPromises4.writeFile(ctx.output, wrapperCode, "utf-8");
|
|
849
|
+
}
|
|
850
|
+
return wrapperCode;
|
|
851
|
+
} catch (error) {
|
|
852
|
+
throw new Error(
|
|
853
|
+
`Failed to setup worker code: ${error instanceof Error ? error.message : String(error)}`
|
|
854
|
+
);
|
|
855
|
+
}
|
|
856
|
+
};
|
|
857
|
+
|
|
858
|
+
// lib/commands/build/modules/entrypoint/entrypoint.ts
|
|
859
|
+
import * as utilsNode2 from "azion/utils/node";
|
|
860
|
+
import path2 from "path";
|
|
861
|
+
import fsPromises5 from "fs/promises";
|
|
862
|
+
var resolveEntrypoint = async ({
|
|
863
|
+
ctx,
|
|
864
|
+
preset
|
|
865
|
+
}) => {
|
|
866
|
+
if (ctx.entrypoint && !preset.handler) {
|
|
867
|
+
const entrypointPath = path2.resolve(ctx.entrypoint);
|
|
868
|
+
try {
|
|
869
|
+
await fsPromises5.access(entrypointPath);
|
|
870
|
+
utilsNode2.feedback.build.info(`Using ${ctx.entrypoint} as entry point.`);
|
|
871
|
+
return ctx.entrypoint;
|
|
872
|
+
} catch (error) {
|
|
873
|
+
debug.error(error);
|
|
874
|
+
throw new Error(
|
|
875
|
+
`Entry point "${ctx.entrypoint}" was not found. Please verify the path and try again.`
|
|
876
|
+
);
|
|
877
|
+
}
|
|
878
|
+
}
|
|
879
|
+
if (preset.handler) {
|
|
880
|
+
const rootPathNodeModules = globalThis.bundler.root.includes("node_modules");
|
|
881
|
+
const presetPath = rootPathNodeModules ? path2.resolve(globalThis.bundler.root, "../") : path2.resolve(globalThis.bundler.root, "node_modules");
|
|
882
|
+
const handlerPath = path2.resolve(
|
|
883
|
+
presetPath,
|
|
884
|
+
"azion",
|
|
885
|
+
"packages",
|
|
886
|
+
"presets",
|
|
887
|
+
"src",
|
|
888
|
+
"presets",
|
|
889
|
+
preset.metadata.name,
|
|
890
|
+
"handler.ts"
|
|
891
|
+
);
|
|
892
|
+
utilsNode2.feedback.build.info(
|
|
893
|
+
`Using built-in handler from "${preset.metadata.name}" preset.`
|
|
894
|
+
);
|
|
895
|
+
return handlerPath;
|
|
896
|
+
}
|
|
897
|
+
if (preset.config.build?.entry) {
|
|
898
|
+
const presetEntry = preset.config.build.entry;
|
|
899
|
+
utilsNode2.feedback.build.info(`Using preset default entry: ${presetEntry}`);
|
|
900
|
+
return path2.resolve(presetEntry);
|
|
901
|
+
}
|
|
902
|
+
throw new Error(
|
|
903
|
+
`Cannot determine entry point. Please specify one using --entry or in your configuration.`
|
|
904
|
+
);
|
|
905
|
+
};
|
|
906
|
+
|
|
907
|
+
// lib/commands/build/build.ts
|
|
908
|
+
var build = async ({
|
|
909
|
+
config,
|
|
910
|
+
ctx
|
|
911
|
+
}) => {
|
|
912
|
+
try {
|
|
913
|
+
await checkDependencies();
|
|
914
|
+
const resolvedPreset = await resolvePreset(config.build?.preset);
|
|
915
|
+
const buildConfigSetup = setupBuildConfig(config, resolvedPreset);
|
|
916
|
+
ctx.entrypoint = await resolveEntrypoint({
|
|
917
|
+
ctx,
|
|
918
|
+
preset: resolvedPreset
|
|
919
|
+
});
|
|
920
|
+
const worker = await setupWorkerCode(buildConfigSetup, ctx);
|
|
921
|
+
await writeFile(buildConfigSetup.entry, worker);
|
|
922
|
+
feedback4.build.info("Starting prebuild...");
|
|
923
|
+
const prebuildResult = await executePrebuild({
|
|
924
|
+
buildConfig: buildConfigSetup,
|
|
925
|
+
ctx
|
|
926
|
+
});
|
|
927
|
+
feedback4.build.info("Prebuild completed successfully");
|
|
928
|
+
feedback4.build.info("Starting build...");
|
|
929
|
+
await executeBuild({
|
|
930
|
+
buildConfig: buildConfigSetup,
|
|
931
|
+
prebuildResult,
|
|
932
|
+
ctx
|
|
933
|
+
});
|
|
934
|
+
feedback4.build.success("Build completed successfully");
|
|
935
|
+
await executePostbuild({ buildConfig: buildConfigSetup, ctx });
|
|
936
|
+
await setEnvironment({ config, preset: resolvedPreset, ctx });
|
|
937
|
+
return {
|
|
938
|
+
config,
|
|
939
|
+
ctx
|
|
940
|
+
};
|
|
941
|
+
} catch (error) {
|
|
942
|
+
debug.error(error);
|
|
943
|
+
feedback4.build.error(
|
|
944
|
+
error instanceof Error ? error.message : String(error)
|
|
945
|
+
);
|
|
946
|
+
process.exit(1);
|
|
947
|
+
}
|
|
948
|
+
};
|
|
949
|
+
|
|
950
|
+
// lib/commands/build/command.ts
|
|
951
|
+
import { resolve } from "path";
|
|
952
|
+
async function buildCommand(options) {
|
|
953
|
+
const userConfig = await readUserConfig() || {};
|
|
954
|
+
const { build: userBuildConfig } = userConfig;
|
|
955
|
+
const bundlerStore = await readStore();
|
|
956
|
+
const presetInput = resolvePresetPriority({
|
|
957
|
+
inputValue: options.preset,
|
|
958
|
+
fileValue: userBuildConfig?.preset,
|
|
959
|
+
storeValue: bundlerStore.preset,
|
|
960
|
+
defaultValue: void 0
|
|
961
|
+
});
|
|
962
|
+
const buildConfig = {
|
|
963
|
+
preset: presetInput,
|
|
964
|
+
entry: resolveConfigPriority({
|
|
965
|
+
inputValue: options.entry,
|
|
966
|
+
fileValue: userBuildConfig?.entry,
|
|
967
|
+
storeValue: bundlerStore?.entry,
|
|
968
|
+
defaultValue: void 0
|
|
969
|
+
}),
|
|
970
|
+
bundler: resolveConfigPriority({
|
|
971
|
+
inputValue: void 0,
|
|
972
|
+
fileValue: userBuildConfig?.bundler,
|
|
973
|
+
storeValue: bundlerStore?.bundler,
|
|
974
|
+
defaultValue: "esbuild"
|
|
975
|
+
}),
|
|
976
|
+
polyfills: resolveConfigPriority({
|
|
977
|
+
inputValue: userBuildConfig?.polyfills,
|
|
978
|
+
fileValue: options.polyfills,
|
|
979
|
+
storeValue: bundlerStore?.polyfills,
|
|
980
|
+
defaultValue: true
|
|
981
|
+
}),
|
|
982
|
+
worker: resolveConfigPriority({
|
|
983
|
+
inputValue: userBuildConfig?.worker,
|
|
984
|
+
fileValue: options.worker,
|
|
985
|
+
storeValue: bundlerStore?.worker,
|
|
986
|
+
defaultValue: false
|
|
987
|
+
})
|
|
988
|
+
};
|
|
989
|
+
await writeStore(buildConfig);
|
|
990
|
+
const config = {
|
|
991
|
+
...userConfig,
|
|
992
|
+
build: {
|
|
993
|
+
...buildConfig,
|
|
994
|
+
memoryFS: userConfig?.build?.memoryFS
|
|
995
|
+
}
|
|
996
|
+
};
|
|
997
|
+
return build({
|
|
998
|
+
config,
|
|
999
|
+
ctx: {
|
|
1000
|
+
production: options.production ?? true,
|
|
1001
|
+
output: resolve(".edge", "worker.js"),
|
|
1002
|
+
entrypoint: buildConfig.entry ? resolve(buildConfig.entry) : ""
|
|
1003
|
+
}
|
|
1004
|
+
});
|
|
1005
|
+
}
|
|
1006
|
+
|
|
1007
|
+
// lib/env/server.ts
|
|
1008
|
+
import { runServer } from "edge-runtime";
|
|
1009
|
+
import fs6 from "fs/promises";
|
|
1010
|
+
var currentServer;
|
|
1011
|
+
var isChangeHandlerRunning = false;
|
|
1012
|
+
var checkAndChangeAddEventListener = (eventTarget, newEvent, code, replaceCode = true) => {
|
|
1013
|
+
let codeChanged = code;
|
|
1014
|
+
const eventRegex = new RegExp(
|
|
1015
|
+
`addEventListener\\((['"]?)${eventTarget}\\1,`,
|
|
1016
|
+
"g"
|
|
1017
|
+
);
|
|
1018
|
+
const firewallFunctionRegex = /firewall:\s*\(event\)\s*=>\s*{/g;
|
|
1019
|
+
const firewallFunction = !!code.match(firewallFunctionRegex);
|
|
1020
|
+
const firewallEventTypeRegex = /eventType\s*=\s*['"]firewall['"];/g;
|
|
1021
|
+
const matchEvent = !!code.match(eventRegex);
|
|
1022
|
+
if (replaceCode && matchEvent || firewallFunction) {
|
|
1023
|
+
codeChanged = code.replace(eventRegex, `addEventListener("${newEvent}",`);
|
|
1024
|
+
if (firewallFunction) {
|
|
1025
|
+
codeChanged = codeChanged.replace(
|
|
1026
|
+
firewallEventTypeRegex,
|
|
1027
|
+
"eventType = 'fetch';"
|
|
1028
|
+
);
|
|
1029
|
+
}
|
|
1030
|
+
}
|
|
1031
|
+
return { matchEvent: matchEvent || firewallFunction, codeChanged };
|
|
1032
|
+
};
|
|
1033
|
+
function checkPortAvailability(port) {
|
|
1034
|
+
return new Promise((resolve2) => {
|
|
1035
|
+
const client = new net.Socket();
|
|
1036
|
+
client.setTimeout(1e3);
|
|
1037
|
+
client.on("connect", () => {
|
|
1038
|
+
client.destroy();
|
|
1039
|
+
resolve2(true);
|
|
1040
|
+
});
|
|
1041
|
+
client.on("timeout", () => {
|
|
1042
|
+
client.destroy();
|
|
1043
|
+
resolve2(false);
|
|
1044
|
+
});
|
|
1045
|
+
client.on("error", (err) => {
|
|
1046
|
+
client.destroy();
|
|
1047
|
+
if (err.message.includes("ECONNREFUSED")) {
|
|
1048
|
+
resolve2(false);
|
|
1049
|
+
} else {
|
|
1050
|
+
resolve2(true);
|
|
1051
|
+
}
|
|
1052
|
+
});
|
|
1053
|
+
client.connect(port, "127.0.0.1");
|
|
1054
|
+
});
|
|
1055
|
+
}
|
|
1056
|
+
async function readWorkerFile(filePath) {
|
|
1057
|
+
try {
|
|
1058
|
+
await fs6.access(filePath);
|
|
1059
|
+
return await fs6.readFile(filePath, "utf8");
|
|
1060
|
+
} catch (error) {
|
|
1061
|
+
const errorMessage = error.message.includes("ENOENT") ? "File does not exist." : `An error occurred while reading the ${filePath} file.`;
|
|
1062
|
+
throw new Error(errorMessage);
|
|
1063
|
+
}
|
|
1064
|
+
}
|
|
1065
|
+
async function initializeServer(port, workerCode) {
|
|
1066
|
+
const { matchEvent: isFirewallEvent, codeChanged } = (
|
|
1067
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1068
|
+
checkAndChangeAddEventListener("firewall", "fetch", workerCode)
|
|
1069
|
+
);
|
|
1070
|
+
const initialCode = isFirewallEvent ? codeChanged : workerCode;
|
|
1071
|
+
const execution = runtime_default(initialCode, isFirewallEvent);
|
|
1072
|
+
return runServer({ port, host: "0.0.0.0", runtime: execution });
|
|
1073
|
+
}
|
|
1074
|
+
async function buildToLocalServer() {
|
|
1075
|
+
const vulcanEnv = await bundler_default.readStore("global");
|
|
1076
|
+
if (!vulcanEnv) {
|
|
1077
|
+
const msg = "Run the build command before running your project.";
|
|
1078
|
+
feedback5.server.error(msg);
|
|
1079
|
+
throw new Error(msg);
|
|
1080
|
+
}
|
|
1081
|
+
await buildCommand({ production: false });
|
|
1082
|
+
}
|
|
1083
|
+
async function manageServer(workerPath, port) {
|
|
1084
|
+
try {
|
|
1085
|
+
if (currentServer) {
|
|
1086
|
+
await currentServer.close();
|
|
1087
|
+
}
|
|
1088
|
+
await buildToLocalServer();
|
|
1089
|
+
const workerCode = await readWorkerFile(workerPath);
|
|
1090
|
+
try {
|
|
1091
|
+
currentServer = await initializeServer(port, workerCode);
|
|
1092
|
+
feedback5.server.success(
|
|
1093
|
+
`Function running on port 0.0.0.0:${port}, url: http://localhost:${port}`
|
|
1094
|
+
);
|
|
1095
|
+
} catch (error) {
|
|
1096
|
+
if (error.message.includes("EADDRINUSE")) {
|
|
1097
|
+
await manageServer(workerPath, port + 1);
|
|
1098
|
+
} else {
|
|
1099
|
+
throw error;
|
|
1100
|
+
}
|
|
1101
|
+
}
|
|
1102
|
+
} catch (error) {
|
|
1103
|
+
feedback5.server.error(error);
|
|
1104
|
+
console.log(error);
|
|
1105
|
+
process.exit(1);
|
|
1106
|
+
} finally {
|
|
1107
|
+
isChangeHandlerRunning = false;
|
|
1108
|
+
}
|
|
1109
|
+
}
|
|
1110
|
+
async function handleFileChange(path3, workerPath, port) {
|
|
1111
|
+
if (isChangeHandlerRunning) return;
|
|
1112
|
+
if (path3.startsWith(".azion-bundler") || path3.startsWith("azion") && path3.includes(".temp") || path3.startsWith(".edge") || path3.startsWith("node_modules") || path3.startsWith(".vercel")) {
|
|
1113
|
+
return;
|
|
1114
|
+
}
|
|
1115
|
+
isChangeHandlerRunning = true;
|
|
1116
|
+
try {
|
|
1117
|
+
feedback5.build.info("Rebuilding with the new changes...");
|
|
1118
|
+
await manageServer(workerPath, port);
|
|
1119
|
+
} catch (error) {
|
|
1120
|
+
debug.error(`Build or server restart failed: ${error}`);
|
|
1121
|
+
} finally {
|
|
1122
|
+
isChangeHandlerRunning = false;
|
|
1123
|
+
}
|
|
1124
|
+
}
|
|
1125
|
+
async function startServer(workerPath, port) {
|
|
1126
|
+
const IsPortInUse = await checkPortAvailability(port);
|
|
1127
|
+
if (IsPortInUse) {
|
|
1128
|
+
feedback5.server.error(
|
|
1129
|
+
`Port ${port} is in use. Please choose another port.`
|
|
1130
|
+
);
|
|
1131
|
+
process.exit(1);
|
|
1132
|
+
}
|
|
1133
|
+
await manageServer(workerPath, port);
|
|
1134
|
+
const watcher = chokidar.watch("./", {
|
|
1135
|
+
persistent: true,
|
|
1136
|
+
ignoreInitial: true,
|
|
1137
|
+
// Ignore the initial add events
|
|
1138
|
+
depth: 99,
|
|
1139
|
+
ignored: [".git", ".vscode", ".idea", ".sublime-text", ".history"]
|
|
1140
|
+
// Added common IDE-related folders
|
|
1141
|
+
});
|
|
1142
|
+
const handleUserFileChange = async (path3) => {
|
|
1143
|
+
await handleFileChange(path3, workerPath, port);
|
|
1144
|
+
};
|
|
1145
|
+
watcher.on("add", handleUserFileChange).on("change", handleUserFileChange).on("unlink", handleUserFileChange).on("addDir", handleUserFileChange).on("unlinkDir", handleUserFileChange).on("error", (error) => debug.error(`Watcher error: ${error}`)).on(
|
|
1146
|
+
"ready",
|
|
1147
|
+
() => feedback5.server.info("Initial scan complete. Ready for changes.")
|
|
1148
|
+
);
|
|
1149
|
+
}
|
|
1150
|
+
var server_default = startServer;
|
|
1151
|
+
|
|
1152
|
+
// lib/env/index.ts
|
|
1153
|
+
var env_default = {
|
|
1154
|
+
runtime: runtime_default,
|
|
1155
|
+
server: server_default,
|
|
1156
|
+
readStore,
|
|
1157
|
+
readUserConfig,
|
|
1158
|
+
writeStore,
|
|
1159
|
+
writeUserConfig
|
|
1160
|
+
};
|
|
1161
|
+
|
|
1162
|
+
export {
|
|
1163
|
+
runtime_default,
|
|
1164
|
+
writeStore,
|
|
1165
|
+
readStore,
|
|
1166
|
+
readUserConfig,
|
|
1167
|
+
writeUserConfig,
|
|
1168
|
+
bundler_default,
|
|
1169
|
+
server_default,
|
|
1170
|
+
env_default,
|
|
1171
|
+
buildCommand
|
|
1172
|
+
};
|