@ohos-ports/layer0-prefetch 4.20.0-beta.0 → 4.20.0-beta.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/node_modules/@layer0/core/README.md +50 -0
- package/node_modules/@layer0/core/build.d.ts +5 -0
- package/node_modules/@layer0/core/build.js +17 -0
- package/node_modules/@layer0/core/cache/Cache.d.ts +56 -0
- package/node_modules/@layer0/core/cache/Cache.js +138 -0
- package/node_modules/@layer0/core/cache/CacheKey.d.ts +52 -0
- package/node_modules/@layer0/core/cache/CacheKey.js +95 -0
- package/node_modules/@layer0/core/config.d.ts +53 -0
- package/node_modules/@layer0/core/config.js +113 -0
- package/node_modules/@layer0/core/constants.d.ts +214 -0
- package/node_modules/@layer0/core/constants.js +216 -0
- package/node_modules/@layer0/core/context.d.ts +38 -0
- package/node_modules/@layer0/core/context.js +98 -0
- package/node_modules/@layer0/core/default-app/all/layer0.config.js +60 -0
- package/node_modules/@layer0/core/default-app/js/routes.js +27 -0
- package/node_modules/@layer0/core/default-app/ts/routes.ts +27 -0
- package/node_modules/@layer0/core/deploy/DeploymentBuilder.d.ts +177 -0
- package/node_modules/@layer0/core/deploy/DeploymentBuilder.js +543 -0
- package/node_modules/@layer0/core/deploy/bundle-esbuild.d.ts +11 -0
- package/node_modules/@layer0/core/deploy/bundle-esbuild.js +67 -0
- package/node_modules/@layer0/core/deploy/bundle-ncc.d.ts +8 -0
- package/node_modules/@layer0/core/deploy/bundle-ncc.js +30 -0
- package/node_modules/@layer0/core/deploy/bundle-nft.d.ts +7 -0
- package/node_modules/@layer0/core/deploy/bundle-nft.js +41 -0
- package/node_modules/@layer0/core/deploy/bundle-webpack.d.ts +11 -0
- package/node_modules/@layer0/core/deploy/bundle-webpack.js +127 -0
- package/node_modules/@layer0/core/deploy/index.d.ts +19 -0
- package/node_modules/@layer0/core/deploy/index.js +6 -0
- package/node_modules/@layer0/core/deploy/listSources.d.ts +11 -0
- package/node_modules/@layer0/core/deploy/listSources.js +18 -0
- package/node_modules/@layer0/core/deploy/paths.d.ts +27 -0
- package/node_modules/@layer0/core/deploy/paths.js +33 -0
- package/node_modules/@layer0/core/deploy/resolveInPackage.d.ts +7 -0
- package/node_modules/@layer0/core/deploy/resolveInPackage.js +21 -0
- package/node_modules/@layer0/core/deploy/routeToFiles.d.ts +14 -0
- package/node_modules/@layer0/core/deploy/routeToFiles.js +24 -0
- package/node_modules/@layer0/core/dev/DevServerOptions.d.ts +33 -0
- package/node_modules/@layer0/core/dev/DevServerOptions.js +2 -0
- package/node_modules/@layer0/core/dev/createDevServer.d.ts +8 -0
- package/node_modules/@layer0/core/dev/createDevServer.js +114 -0
- package/node_modules/@layer0/core/dev/index.d.ts +3 -0
- package/node_modules/@layer0/core/dev/index.js +6 -0
- package/node_modules/@layer0/core/dev/runLocalApp.d.ts +8 -0
- package/node_modules/@layer0/core/dev/runLocalApp.js +73 -0
- package/node_modules/@layer0/core/dev/serverlessCompatibilityMiddleware.d.ts +11 -0
- package/node_modules/@layer0/core/dev/serverlessCompatibilityMiddleware.js +30 -0
- package/node_modules/@layer0/core/edge/EdgeErrorPageResponseWriter.d.ts +12 -0
- package/node_modules/@layer0/core/edge/EdgeErrorPageResponseWriter.js +22 -0
- package/node_modules/@layer0/core/edge/EdgeResponseWriter.d.ts +122 -0
- package/node_modules/@layer0/core/edge/EdgeResponseWriter.js +451 -0
- package/node_modules/@layer0/core/edge/createEdgeConfig.d.ts +16 -0
- package/node_modules/@layer0/core/edge/createEdgeConfig.js +284 -0
- package/node_modules/@layer0/core/environment.d.ts +24 -0
- package/node_modules/@layer0/core/environment.js +42 -0
- package/node_modules/@layer0/core/errors/BackendFetchError.d.ts +11 -0
- package/node_modules/@layer0/core/errors/BackendFetchError.js +17 -0
- package/node_modules/@layer0/core/errors/FrameworkBuildError.d.ts +7 -0
- package/node_modules/@layer0/core/errors/FrameworkBuildError.js +26 -0
- package/node_modules/@layer0/core/index.d.ts +3 -0
- package/node_modules/@layer0/core/index.js +9 -0
- package/node_modules/@layer0/core/init/addToPackageJson.d.ts +7 -0
- package/node_modules/@layer0/core/init/addToPackageJson.js +22 -0
- package/node_modules/@layer0/core/init/index.d.ts +2 -0
- package/node_modules/@layer0/core/init/index.js +6 -0
- package/node_modules/@layer0/core/init/init.d.ts +5 -0
- package/node_modules/@layer0/core/init/init.js +17 -0
- package/node_modules/@layer0/core/lambda/JSBackend.d.ts +30 -0
- package/node_modules/@layer0/core/lambda/JSBackend.js +51 -0
- package/node_modules/@layer0/core/lambda/RequestListenerJSBackend.d.ts +8 -0
- package/node_modules/@layer0/core/lambda/RequestListenerJSBackend.js +19 -0
- package/node_modules/@layer0/core/lambda/adaptResponseEncodingToDownstream.d.ts +17 -0
- package/node_modules/@layer0/core/lambda/adaptResponseEncodingToDownstream.js +132 -0
- package/node_modules/@layer0/core/lambda/consoleWrapper.d.ts +16 -0
- package/node_modules/@layer0/core/lambda/consoleWrapper.js +127 -0
- package/node_modules/@layer0/core/lambda/getBodyLoggingData.d.ts +10 -0
- package/node_modules/@layer0/core/lambda/getBodyLoggingData.js +108 -0
- package/node_modules/@layer0/core/lambda/handler.d.ts +1 -0
- package/node_modules/@layer0/core/lambda/handler.js +91 -0
- package/node_modules/@layer0/core/lambda/handler.js.map +7 -0
- package/node_modules/@layer0/core/lambda/innerHandler.d.ts +7 -0
- package/node_modules/@layer0/core/lambda/innerHandler.js +181 -0
- package/node_modules/@layer0/core/lambda/lambdaInstance.d.ts +29 -0
- package/node_modules/@layer0/core/lambda/lambdaInstance.js +71 -0
- package/node_modules/@layer0/core/lambda/reqResMapper.d.ts +24 -0
- package/node_modules/@layer0/core/lambda/reqResMapper.js +286 -0
- package/node_modules/@layer0/core/lambda/stdStreamsWrapper.d.ts +31 -0
- package/node_modules/@layer0/core/lambda/stdStreamsWrapper.js +196 -0
- package/node_modules/@layer0/core/lambda/toJSBackend.d.ts +10 -0
- package/node_modules/@layer0/core/lambda/toJSBackend.js +23 -0
- package/node_modules/@layer0/core/node_modules/esbuild/LICENSE.md +21 -0
- package/node_modules/@layer0/core/node_modules/esbuild/README.md +3 -0
- package/node_modules/@layer0/core/node_modules/esbuild/bin/esbuild +223 -0
- package/node_modules/@layer0/core/node_modules/esbuild/install.js +301 -0
- package/node_modules/@layer0/core/node_modules/esbuild/lib/main.d.ts +716 -0
- package/node_modules/@layer0/core/node_modules/esbuild/lib/main.js +2532 -0
- package/node_modules/@layer0/core/node_modules/esbuild/node_modules/@ohos-ports/esbuild-openharmony-arm64/README.md +3 -0
- package/node_modules/@layer0/core/node_modules/esbuild/node_modules/@ohos-ports/esbuild-openharmony-arm64/bin/esbuild +0 -0
- package/node_modules/@layer0/core/node_modules/esbuild/node_modules/@ohos-ports/esbuild-openharmony-arm64/esbuild.wasm +0 -0
- package/node_modules/@layer0/core/node_modules/esbuild/node_modules/@ohos-ports/esbuild-openharmony-arm64/package.json +28 -0
- package/node_modules/@layer0/core/node_modules/esbuild/node_modules/@ohos-ports/esbuild-openharmony-arm64/wasm_exec.js +575 -0
- package/node_modules/@layer0/core/node_modules/esbuild/node_modules/@ohos-ports/esbuild-openharmony-arm64/wasm_exec_node.js +40 -0
- package/node_modules/@layer0/core/node_modules/esbuild/package.json +78 -0
- package/node_modules/@layer0/core/package.json +132 -0
- package/node_modules/@layer0/core/parser/parseRouter.d.ts +13 -0
- package/node_modules/@layer0/core/parser/parseRouter.js +77 -0
- package/node_modules/@layer0/core/plugins/PluginBase.d.ts +18 -0
- package/node_modules/@layer0/core/plugins/PluginBase.js +19 -0
- package/node_modules/@layer0/core/router/Backend.d.ts +69 -0
- package/node_modules/@layer0/core/router/Backend.js +176 -0
- package/node_modules/@layer0/core/router/BackendOptions.d.ts +11 -0
- package/node_modules/@layer0/core/router/BackendOptions.js +2 -0
- package/node_modules/@layer0/core/router/BaseResponseWriter.d.ts +65 -0
- package/node_modules/@layer0/core/router/BaseResponseWriter.js +81 -0
- package/node_modules/@layer0/core/router/BehindEdgeResponseWriterProxy.d.ts +4 -0
- package/node_modules/@layer0/core/router/BehindEdgeResponseWriterProxy.js +89 -0
- package/node_modules/@layer0/core/router/CacheManifest.d.ts +80 -0
- package/node_modules/@layer0/core/router/CacheManifest.js +83 -0
- package/node_modules/@layer0/core/router/CacheOptions.d.ts +171 -0
- package/node_modules/@layer0/core/router/CacheOptions.js +2 -0
- package/node_modules/@layer0/core/router/CustomCacheKey.d.ts +140 -0
- package/node_modules/@layer0/core/router/CustomCacheKey.js +272 -0
- package/node_modules/@layer0/core/router/GroupConfig.d.ts +7 -0
- package/node_modules/@layer0/core/router/GroupConfig.js +20 -0
- package/node_modules/@layer0/core/router/Groups.d.ts +7 -0
- package/node_modules/@layer0/core/router/Groups.js +21 -0
- package/node_modules/@layer0/core/router/Headers.d.ts +3 -0
- package/node_modules/@layer0/core/router/Headers.js +2 -0
- package/node_modules/@layer0/core/router/PathMatcher.d.ts +51 -0
- package/node_modules/@layer0/core/router/PathMatcher.js +73 -0
- package/node_modules/@layer0/core/router/Preload.d.ts +16 -0
- package/node_modules/@layer0/core/router/Preload.js +2 -0
- package/node_modules/@layer0/core/router/PreloadRequests.d.ts +23 -0
- package/node_modules/@layer0/core/router/PreloadRequests.js +66 -0
- package/node_modules/@layer0/core/router/Request.d.ts +74 -0
- package/node_modules/@layer0/core/router/Request.js +2 -0
- package/node_modules/@layer0/core/router/RequestContext.d.ts +12 -0
- package/node_modules/@layer0/core/router/RequestContext.js +11 -0
- package/node_modules/@layer0/core/router/Response.d.ts +55 -0
- package/node_modules/@layer0/core/router/Response.js +2 -0
- package/node_modules/@layer0/core/router/ResponseWriter.d.ts +1011 -0
- package/node_modules/@layer0/core/router/ResponseWriter.js +1341 -0
- package/node_modules/@layer0/core/router/Route.d.ts +124 -0
- package/node_modules/@layer0/core/router/Route.js +280 -0
- package/node_modules/@layer0/core/router/RouteCriteria.d.ts +68 -0
- package/node_modules/@layer0/core/router/RouteCriteria.js +2 -0
- package/node_modules/@layer0/core/router/RouteGroup.d.ts +157 -0
- package/node_modules/@layer0/core/router/RouteGroup.js +223 -0
- package/node_modules/@layer0/core/router/RouteGroupList.d.ts +45 -0
- package/node_modules/@layer0/core/router/RouteGroupList.js +73 -0
- package/node_modules/@layer0/core/router/Router.d.ts +439 -0
- package/node_modules/@layer0/core/router/Router.js +907 -0
- package/node_modules/@layer0/core/router/addBuiltInRoutes.d.ts +2 -0
- package/node_modules/@layer0/core/router/addBuiltInRoutes.js +58 -0
- package/node_modules/@layer0/core/router/addPreloadHeaders.d.ts +7 -0
- package/node_modules/@layer0/core/router/addPreloadHeaders.js +57 -0
- package/node_modules/@layer0/core/router/cacheManifestFromRouter.d.ts +6 -0
- package/node_modules/@layer0/core/router/cacheManifestFromRouter.js +85 -0
- package/node_modules/@layer0/core/router/createCustomCacheKey.d.ts +27 -0
- package/node_modules/@layer0/core/router/createCustomCacheKey.js +35 -0
- package/node_modules/@layer0/core/router/deriveSurrogateKeysFromJson.d.ts +36 -0
- package/node_modules/@layer0/core/router/deriveSurrogateKeysFromJson.js +70 -0
- package/node_modules/@layer0/core/router/index.d.ts +11 -0
- package/node_modules/@layer0/core/router/index.js +17 -0
- package/node_modules/@layer0/core/router/interpolateParams.d.ts +21 -0
- package/node_modules/@layer0/core/router/interpolateParams.js +150 -0
- package/node_modules/@layer0/core/router/loadRouter.d.ts +13 -0
- package/node_modules/@layer0/core/router/loadRouter.js +29 -0
- package/node_modules/@layer0/core/router/mergeQueryString.d.ts +1 -0
- package/node_modules/@layer0/core/router/mergeQueryString.js +23 -0
- package/node_modules/@layer0/core/router/normalizeCriteria.d.ts +6 -0
- package/node_modules/@layer0/core/router/normalizeCriteria.js +20 -0
- package/node_modules/@layer0/core/router/stream.d.ts +12 -0
- package/node_modules/@layer0/core/router/stream.js +30 -0
- package/node_modules/@layer0/core/router/universalRouteUtils.d.ts +55 -0
- package/node_modules/@layer0/core/router/universalRouteUtils.js +114 -0
- package/node_modules/@layer0/core/router/validateWriter.d.ts +41 -0
- package/node_modules/@layer0/core/router/validateWriter.js +120 -0
- package/node_modules/@layer0/core/serverless.yml +30 -0
- package/node_modules/@layer0/core/source.d.ts +9 -0
- package/node_modules/@layer0/core/source.js +17 -0
- package/node_modules/@layer0/core/test-utils/MockRequest.d.ts +15 -0
- package/node_modules/@layer0/core/test-utils/MockRequest.js +36 -0
- package/node_modules/@layer0/core/test-utils/MockResponse.d.ts +7 -0
- package/node_modules/@layer0/core/test-utils/MockResponse.js +24 -0
- package/node_modules/@layer0/core/test-utils/expect-extend.d.ts +1 -0
- package/node_modules/@layer0/core/test-utils/expect-extend.js +11 -0
- package/node_modules/@layer0/core/test-utils/index.d.ts +4 -0
- package/node_modules/@layer0/core/test-utils/index.js +11 -0
- package/node_modules/@layer0/core/test-utils/matchers/toBeCachedByTheBrowser.d.ts +19 -0
- package/node_modules/@layer0/core/test-utils/matchers/toBeCachedByTheBrowser.js +45 -0
- package/node_modules/@layer0/core/test-utils/matchers/toBeCachedByTheEdge.d.ts +19 -0
- package/node_modules/@layer0/core/test-utils/matchers/toBeCachedByTheEdge.js +38 -0
- package/node_modules/@layer0/core/test-utils/matchers/toBeCachedByTheServiceWorker.d.ts +19 -0
- package/node_modules/@layer0/core/test-utils/matchers/toBeCachedByTheServiceWorker.js +45 -0
- package/node_modules/@layer0/core/test-utils/matchers/toBeServedStale.d.ts +20 -0
- package/node_modules/@layer0/core/test-utils/matchers/toBeServedStale.js +50 -0
- package/node_modules/@layer0/core/test-utils/matchers/toHaveBody.d.ts +18 -0
- package/node_modules/@layer0/core/test-utils/matchers/toHaveBody.js +40 -0
- package/node_modules/@layer0/core/test-utils/matchers/toHaveHeader.d.ts +19 -0
- package/node_modules/@layer0/core/test-utils/matchers/toHaveHeader.js +43 -0
- package/node_modules/@layer0/core/test-utils/matchers.d.ts +6 -0
- package/node_modules/@layer0/core/test-utils/matchers.js +18 -0
- package/node_modules/@layer0/core/test-utils/utils/createRouteMock.d.ts +21 -0
- package/node_modules/@layer0/core/test-utils/utils/createRouteMock.js +27 -0
- package/node_modules/@layer0/core/test-utils/utils/getBackend.d.ts +13 -0
- package/node_modules/@layer0/core/test-utils/utils/getBackend.js +30 -0
- package/node_modules/@layer0/core/test-utils/utils/getMessage.d.ts +1 -0
- package/node_modules/@layer0/core/test-utils/utils/getMessage.js +16 -0
- package/node_modules/@layer0/core/test-utils/utils/runRoute.d.ts +10 -0
- package/node_modules/@layer0/core/test-utils/utils/runRoute.js +39 -0
- package/node_modules/@layer0/core/timing.d.ts +115 -0
- package/node_modules/@layer0/core/timing.js +200 -0
- package/node_modules/@layer0/core/transform/index.d.ts +1 -0
- package/node_modules/@layer0/core/transform/index.js +4 -0
- package/node_modules/@layer0/core/transform/transformMethod.d.ts +40 -0
- package/node_modules/@layer0/core/transform/transformMethod.js +58 -0
- package/node_modules/@layer0/core/types.d.ts +102 -0
- package/node_modules/@layer0/core/types.js +30 -0
- package/node_modules/@layer0/core/utils/Tasks.d.ts +32 -0
- package/node_modules/@layer0/core/utils/Tasks.js +80 -0
- package/node_modules/@layer0/core/utils/Validator.d.ts +18 -0
- package/node_modules/@layer0/core/utils/Validator.js +178 -0
- package/node_modules/@layer0/core/utils/bindParamsToPath.d.ts +15 -0
- package/node_modules/@layer0/core/utils/bindParamsToPath.js +49 -0
- package/node_modules/@layer0/core/utils/convertParamsToBackReferences.d.ts +16 -0
- package/node_modules/@layer0/core/utils/convertParamsToBackReferences.js +45 -0
- package/node_modules/@layer0/core/utils/convertStringMatcherToRegex.d.ts +7 -0
- package/node_modules/@layer0/core/utils/convertStringMatcherToRegex.js +26 -0
- package/node_modules/@layer0/core/utils/decode.d.ts +9 -0
- package/node_modules/@layer0/core/utils/decode.js +44 -0
- package/node_modules/@layer0/core/utils/esImport.d.ts +1 -0
- package/node_modules/@layer0/core/utils/esImport.js +9 -0
- package/node_modules/@layer0/core/utils/fs.d.ts +1 -0
- package/node_modules/@layer0/core/utils/fs.js +7 -0
- package/node_modules/@layer0/core/utils/get.d.ts +6 -0
- package/node_modules/@layer0/core/utils/get.js +19 -0
- package/node_modules/@layer0/core/utils/getPackageVersion.d.ts +7 -0
- package/node_modules/@layer0/core/utils/getPackageVersion.js +19 -0
- package/node_modules/@layer0/core/utils/has.d.ts +6 -0
- package/node_modules/@layer0/core/utils/has.js +15 -0
- package/node_modules/@layer0/core/utils/identity.d.ts +7 -0
- package/node_modules/@layer0/core/utils/identity.js +12 -0
- package/node_modules/@layer0/core/utils/index.d.ts +1 -0
- package/node_modules/@layer0/core/utils/index.js +4 -0
- package/node_modules/@layer0/core/utils/isEmpty.d.ts +6 -0
- package/node_modules/@layer0/core/utils/isEmpty.js +20 -0
- package/node_modules/@layer0/core/utils/isString.d.ts +1 -0
- package/node_modules/@layer0/core/utils/isString.js +6 -0
- package/node_modules/@layer0/core/utils/logo.d.ts +5 -0
- package/node_modules/@layer0/core/utils/logo.js +27 -0
- package/node_modules/@layer0/core/utils/mapValues.d.ts +7 -0
- package/node_modules/@layer0/core/utils/mapValues.js +16 -0
- package/node_modules/@layer0/core/utils/minBy.d.ts +8 -0
- package/node_modules/@layer0/core/utils/minBy.js +41 -0
- package/node_modules/@layer0/core/utils/nonWebpackRequire.d.ts +11 -0
- package/node_modules/@layer0/core/utils/nonWebpackRequire.js +28 -0
- package/node_modules/@layer0/core/utils/packageUtils.d.ts +12 -0
- package/node_modules/@layer0/core/utils/packageUtils.js +22 -0
- package/node_modules/@layer0/core/utils/ports.d.ts +5 -0
- package/node_modules/@layer0/core/utils/ports.js +7 -0
- package/node_modules/@layer0/core/utils/readBody.d.ts +7 -0
- package/node_modules/@layer0/core/utils/readBody.js +15 -0
- package/node_modules/@layer0/core/utils/readRawBody.d.ts +9 -0
- package/node_modules/@layer0/core/utils/readRawBody.js +21 -0
- package/node_modules/@layer0/core/utils/regExpEscape.d.ts +12 -0
- package/node_modules/@layer0/core/utils/regExpEscape.js +20 -0
- package/node_modules/@layer0/core/utils/requireInternal.d.ts +7 -0
- package/node_modules/@layer0/core/utils/requireInternal.js +24 -0
- package/node_modules/@layer0/core/utils/set.d.ts +10 -0
- package/node_modules/@layer0/core/utils/set.js +21 -0
- package/node_modules/@layer0/core/utils/toEdgeRegex.d.ts +11 -0
- package/node_modules/@layer0/core/utils/toEdgeRegex.js +23 -0
- package/node_modules/@layer0/core/utils/toHttpMethodMatchRegex.d.ts +12 -0
- package/node_modules/@layer0/core/utils/toHttpMethodMatchRegex.js +17 -0
- package/node_modules/@layer0/core/utils/toLower.d.ts +1 -0
- package/node_modules/@layer0/core/utils/toLower.js +9 -0
- package/node_modules/@layer0/core/utils/validateBackends.d.ts +5 -0
- package/node_modules/@layer0/core/utils/validateBackends.js +30 -0
- package/node_modules/@layer0/core/utils/watch.d.ts +10 -0
- package/node_modules/@layer0/core/utils/watch.js +17 -0
- package/package.json +47 -3
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
## Overview
|
|
2
|
+
|
|
3
|
+
Core library for the [Layer0](https://docs.layer0.co), infrastructure to run your big, dynamic website frontend and make it load in less than one second.
|
|
4
|
+
|
|
5
|
+
The `@layer0/core` package provides a CDN-as-JavaScript API for defining edge routing and logic on Layer0.
|
|
6
|
+
|
|
7
|
+
## Example Routes File
|
|
8
|
+
|
|
9
|
+
```js
|
|
10
|
+
// routes.js
|
|
11
|
+
|
|
12
|
+
import { Router } from '@layer0/core/router'
|
|
13
|
+
import { createNextPlugin } = from '@layer0/next'
|
|
14
|
+
import { renderNextPage, nextRoutes } from '@layer0/next'
|
|
15
|
+
|
|
16
|
+
export default new Router()
|
|
17
|
+
// redirect at edge
|
|
18
|
+
.match('/some/path/:withVar', async ({ redirect }) => {
|
|
19
|
+
redirect('/some/other/path/:withVar', { statusCode: 301 })
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
// proxy the legacy site
|
|
23
|
+
.match('/some/path/:withVar', async ({ proxy }) => {
|
|
24
|
+
proxy('legacy', {
|
|
25
|
+
path: '/some/other/path/:withVar',
|
|
26
|
+
})
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
// match based on header and proxy the legacy site
|
|
30
|
+
.match({ headers: { 'layer0-device-type': /desktop/ } }, async ({ proxy }) => {
|
|
31
|
+
proxy('legacy')
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
// vanity URL for next.js
|
|
35
|
+
.match('/some/vanity/url/:productId', res => {
|
|
36
|
+
renderNextPage('/p/[productId]', res)
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
// Send a synthetic html response
|
|
40
|
+
.match('/static-html', ({ setResponseHeader, send }) => {
|
|
41
|
+
setResponseHeader('Content-Type', 'text/html; charset=UTF-8')
|
|
42
|
+
send('<html><body><h1>Hello world!</h1></body></html>', 200)
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
// match Next.js routes based on the pages directory
|
|
46
|
+
.use(nextRoutes)
|
|
47
|
+
|
|
48
|
+
// fall back to proxying the legacy site
|
|
49
|
+
.fallback(({ proxy }) => proxy('legacy'))
|
|
50
|
+
```
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
/* istanbul ignore file */
|
|
7
|
+
const DeploymentBuilder_1 = __importDefault(require("./deploy/DeploymentBuilder"));
|
|
8
|
+
/**
|
|
9
|
+
* The default build script for Layer0. Clears the previous build and
|
|
10
|
+
* builds a .layer0 directory with lambda and s3 subdirectories
|
|
11
|
+
*/
|
|
12
|
+
async function build() {
|
|
13
|
+
const builder = new DeploymentBuilder_1.default(process.cwd());
|
|
14
|
+
builder.clearPreviousBuildOutput();
|
|
15
|
+
await builder.build();
|
|
16
|
+
}
|
|
17
|
+
exports.default = build;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import CacheKey from './CacheKey';
|
|
3
|
+
import LRU from 'lru-cache';
|
|
4
|
+
import RequestContext from '../router/RequestContext';
|
|
5
|
+
import Response from '../router/Response';
|
|
6
|
+
/**
|
|
7
|
+
* We store the responses in the cache differently from their actual
|
|
8
|
+
* representation because they are different classes in different
|
|
9
|
+
* execution environments.
|
|
10
|
+
*/
|
|
11
|
+
export interface CachedResponse {
|
|
12
|
+
/**
|
|
13
|
+
* The HTTP status code
|
|
14
|
+
*/
|
|
15
|
+
statusCode?: number;
|
|
16
|
+
/**
|
|
17
|
+
* The HTTP status message
|
|
18
|
+
*/
|
|
19
|
+
statusMessage?: string;
|
|
20
|
+
/**
|
|
21
|
+
* The response body as a string or a Buffer
|
|
22
|
+
*/
|
|
23
|
+
body?: string | Buffer;
|
|
24
|
+
/**
|
|
25
|
+
* The response HTTP headers
|
|
26
|
+
*/
|
|
27
|
+
headers: {
|
|
28
|
+
[key: string]: string | string[];
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Must have the LAYER0_CACHE environment variable set.
|
|
33
|
+
*/
|
|
34
|
+
export declare function isCacheEnabled(): boolean;
|
|
35
|
+
/**
|
|
36
|
+
* Gets the singleton instance of the development response cache
|
|
37
|
+
*/
|
|
38
|
+
export declare function getCache(): LRU<string, CachedResponse>;
|
|
39
|
+
/**
|
|
40
|
+
* Evicts all entries from the cache
|
|
41
|
+
*/
|
|
42
|
+
export declare function clearCache(): void;
|
|
43
|
+
/**
|
|
44
|
+
* Returns the cached response for the specified key
|
|
45
|
+
* @param key The cache key
|
|
46
|
+
*/
|
|
47
|
+
export declare function getCachedResponse(key: CacheKey): CachedResponse | undefined;
|
|
48
|
+
export interface AddToCacheOptions {
|
|
49
|
+
response: Response;
|
|
50
|
+
cacheKey: CacheKey;
|
|
51
|
+
context: RequestContext;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Writes the response to the cache if x-0-cache-control has a non-zero max-age value.
|
|
55
|
+
*/
|
|
56
|
+
export declare function maybeCacheResponse({ response, cacheKey, context }: AddToCacheOptions): boolean;
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const constants_1 = require("../constants");
|
|
7
|
+
const lru_cache_1 = __importDefault(require("lru-cache"));
|
|
8
|
+
const lodash_clonedeep_1 = __importDefault(require("lodash.clonedeep"));
|
|
9
|
+
let cache;
|
|
10
|
+
/**
|
|
11
|
+
* Must have the LAYER0_CACHE environment variable set.
|
|
12
|
+
*/
|
|
13
|
+
function isCacheEnabled() {
|
|
14
|
+
return process.env.LAYER0_CACHE === 'true';
|
|
15
|
+
}
|
|
16
|
+
exports.isCacheEnabled = isCacheEnabled;
|
|
17
|
+
/**
|
|
18
|
+
* Gets the singleton instance of the development response cache
|
|
19
|
+
*/
|
|
20
|
+
function getCache() {
|
|
21
|
+
if (!cache) {
|
|
22
|
+
cache = new lru_cache_1.default();
|
|
23
|
+
}
|
|
24
|
+
return cache;
|
|
25
|
+
}
|
|
26
|
+
exports.getCache = getCache;
|
|
27
|
+
/**
|
|
28
|
+
* Evicts all entries from the cache
|
|
29
|
+
*/
|
|
30
|
+
function clearCache() {
|
|
31
|
+
getCache().reset();
|
|
32
|
+
}
|
|
33
|
+
exports.clearCache = clearCache;
|
|
34
|
+
/**
|
|
35
|
+
* Returns the cached response for the specified key
|
|
36
|
+
* @param key The cache key
|
|
37
|
+
*/
|
|
38
|
+
function getCachedResponse(key) {
|
|
39
|
+
return getCache().get(key.toString());
|
|
40
|
+
}
|
|
41
|
+
exports.getCachedResponse = getCachedResponse;
|
|
42
|
+
/**
|
|
43
|
+
* Writes the response to the cache if x-0-cache-control has a non-zero max-age value.
|
|
44
|
+
*/
|
|
45
|
+
function maybeCacheResponse({ response, cacheKey, context }) {
|
|
46
|
+
var _a;
|
|
47
|
+
const method = (_a = cacheKey.method) === null || _a === void 0 ? void 0 : _a.toLowerCase();
|
|
48
|
+
if (method && !constants_1.CACHEABLE_METHODS.has(method) && context.cacheRouteMethod !== method) {
|
|
49
|
+
// POSTs should only be cached when the route explicitly defines the method criteria as POST
|
|
50
|
+
response.setHeader(constants_1.HTTP_HEADERS.x0CachingStatus, constants_1.CACHING_STATUS.method);
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
else if (cacheKey.body && Buffer.from(cacheKey.body, 'utf8').length > 8000) {
|
|
54
|
+
// here we ensure that we're counting bytes by reading the string as utf8, which is a single byte per character
|
|
55
|
+
// The 8000 character limit is the maximum size fastly can read from the request body
|
|
56
|
+
response.setHeader(constants_1.HTTP_HEADERS.x0CachingStatus, constants_1.CACHING_STATUS.bodyTooBig);
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
else if (response.statusCode && response.statusCode >= 400) {
|
|
60
|
+
// we don't cache statuses 400 and above
|
|
61
|
+
response.setHeader(constants_1.HTTP_HEADERS.x0CachingStatus, constants_1.CACHING_STATUS.code);
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
else if (response.getHeader(constants_1.HTTP_HEADERS.setCookie)) {
|
|
65
|
+
// we don't cache responses with set-cookie headers as they may contain personal data
|
|
66
|
+
response.setHeader(constants_1.HTTP_HEADERS.x0CachingStatus, constants_1.CACHING_STATUS.setCookie);
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
const cacheParams = getCacheParams(response);
|
|
70
|
+
if (cacheParams.private && !context.forcePrivateCaching) {
|
|
71
|
+
response.setHeader(constants_1.HTTP_HEADERS.x0CachingStatus, constants_1.CACHING_STATUS.private);
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
else if (cacheParams.maxAge) {
|
|
75
|
+
getCache().set(cacheKey.toString(), convertToCachedResponse(response), cacheParams.maxAge * 1000);
|
|
76
|
+
response.setHeader(constants_1.HTTP_HEADERS.x0CachingStatus, constants_1.CACHING_STATUS.ok);
|
|
77
|
+
return true;
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
response.setHeader(constants_1.HTTP_HEADERS.x0CachingStatus, constants_1.CACHING_STATUS.noMaxAge);
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
exports.maybeCacheResponse = maybeCacheResponse;
|
|
85
|
+
/**
|
|
86
|
+
* We have to deep clone parts of the
|
|
87
|
+
* @param response HTTP response that we are caching
|
|
88
|
+
*/
|
|
89
|
+
function convertToCachedResponse(response) {
|
|
90
|
+
return {
|
|
91
|
+
statusCode: response.statusCode,
|
|
92
|
+
statusMessage: response.statusMessage,
|
|
93
|
+
headers: lodash_clonedeep_1.default(response.getHeaders()),
|
|
94
|
+
body: response.body,
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Gets the effective caching settings for the response
|
|
99
|
+
* @param response The current response
|
|
100
|
+
*/
|
|
101
|
+
function getCacheParams(response) {
|
|
102
|
+
const defaults = {
|
|
103
|
+
private: false,
|
|
104
|
+
maxAge: 0,
|
|
105
|
+
};
|
|
106
|
+
return {
|
|
107
|
+
...defaults,
|
|
108
|
+
...getCacheParamsFromHeader(response.getHeader(constants_1.HTTP_HEADERS.cacheControl)),
|
|
109
|
+
...getCacheParamsFromHeader(response.getHeader(constants_1.HTTP_HEADERS.x0CacheControl)),
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Extracts private and max-age/s-maxage from a cache-control header
|
|
114
|
+
* @param cacheControl
|
|
115
|
+
*/
|
|
116
|
+
function getCacheParamsFromHeader(cacheControl) {
|
|
117
|
+
const result = {};
|
|
118
|
+
if (cacheControl) {
|
|
119
|
+
if (!Array.isArray(cacheControl)) {
|
|
120
|
+
cacheControl = [cacheControl];
|
|
121
|
+
}
|
|
122
|
+
const values = {};
|
|
123
|
+
for (let header of cacheControl) {
|
|
124
|
+
const pairs = header.split(/,\s*/).map(v => v.split(/=/));
|
|
125
|
+
for (let [name, value] of pairs) {
|
|
126
|
+
if (name && value) {
|
|
127
|
+
values[name.toLowerCase()] = value;
|
|
128
|
+
}
|
|
129
|
+
if (name.toLowerCase() === 'private') {
|
|
130
|
+
// private has no value
|
|
131
|
+
result.private = true;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
result.maxAge = parseInt(values['s-maxage'] || values['max-age'] || '0');
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
return result;
|
|
138
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { CustomCacheKey } from '../router';
|
|
2
|
+
import Request from '../router/Request';
|
|
3
|
+
/**
|
|
4
|
+
* An actual cache key derived from a request per the user's CustomCacheKey specification
|
|
5
|
+
*/
|
|
6
|
+
export default class CacheKey {
|
|
7
|
+
body?: string;
|
|
8
|
+
method?: string;
|
|
9
|
+
pathname: string;
|
|
10
|
+
query: {
|
|
11
|
+
[key: string]: string | string[] | undefined;
|
|
12
|
+
};
|
|
13
|
+
headers: {
|
|
14
|
+
[key: string]: string | string[];
|
|
15
|
+
};
|
|
16
|
+
cookies: {
|
|
17
|
+
[key: string]: string | string[];
|
|
18
|
+
};
|
|
19
|
+
static create(request: Request, customKey?: CustomCacheKey): CacheKey;
|
|
20
|
+
constructor(request: Request, customKey?: CustomCacheKey);
|
|
21
|
+
/**
|
|
22
|
+
* We always ignore layer0_* query params when computing the cache key so that things like layer0_prefetch don't cache bust
|
|
23
|
+
* @param query
|
|
24
|
+
*/
|
|
25
|
+
excludeLayer0QueryParams(query: {
|
|
26
|
+
[name: string]: string | string[] | undefined;
|
|
27
|
+
}): {
|
|
28
|
+
[name: string]: string | string[] | undefined;
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* Returns an array of values in the object sorted by the key
|
|
32
|
+
* We sort query params so that different orderings will result in a hit
|
|
33
|
+
* @param source Any object of key/value pairs, e.g. query, headers, cookies, etc...
|
|
34
|
+
*/
|
|
35
|
+
toSortedArray(source: {
|
|
36
|
+
[name: string]: any;
|
|
37
|
+
}): any[][];
|
|
38
|
+
/**
|
|
39
|
+
* Returns the values of the request headers that are by default included in the caching key
|
|
40
|
+
*
|
|
41
|
+
* Notes:
|
|
42
|
+
* - Cache hash calculation has a connascence of algorithm with VCL moov_hash subroutine so if you
|
|
43
|
+
* are modifying it in any way, you have to modify VCL code generator in the same way. When in doubt
|
|
44
|
+
* the VCL cache hash calculation takes precendence as it is designed to serve production traffic
|
|
45
|
+
* and has been battle tested to do so reliably.
|
|
46
|
+
* - VCL cache hash includes some request/service properties that do no apply in local development:
|
|
47
|
+
* * build number does not exist for local development
|
|
48
|
+
* * flush timestamps for gradual flushing do not apply locally
|
|
49
|
+
*/
|
|
50
|
+
private getDefaultHeaders;
|
|
51
|
+
toString(): string;
|
|
52
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const constants_1 = require("../constants");
|
|
7
|
+
const crypto_1 = __importDefault(require("crypto"));
|
|
8
|
+
/**
|
|
9
|
+
* An actual cache key derived from a request per the user's CustomCacheKey specification
|
|
10
|
+
*/
|
|
11
|
+
class CacheKey {
|
|
12
|
+
constructor(request, customKey) {
|
|
13
|
+
var _a;
|
|
14
|
+
this.cookies = {};
|
|
15
|
+
const { host } = request.headers;
|
|
16
|
+
this.body = request.body;
|
|
17
|
+
this.method = (_a = request.method) === null || _a === void 0 ? void 0 : _a.toLowerCase();
|
|
18
|
+
this.query = this.excludeLayer0QueryParams(request.query || {});
|
|
19
|
+
this.pathname = new URL(request.url, `http://${host}`).pathname;
|
|
20
|
+
this.headers = this.getDefaultHeaders(request.headers);
|
|
21
|
+
if (customKey) {
|
|
22
|
+
for (let action of customKey.getActions()) {
|
|
23
|
+
action(this, request);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
static create(request, customKey) {
|
|
28
|
+
return new CacheKey(request, customKey);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* We always ignore layer0_* query params when computing the cache key so that things like layer0_prefetch don't cache bust
|
|
32
|
+
* @param query
|
|
33
|
+
*/
|
|
34
|
+
excludeLayer0QueryParams(query) {
|
|
35
|
+
const result = {};
|
|
36
|
+
for (let name in query) {
|
|
37
|
+
if (!name.startsWith('layer0_')) {
|
|
38
|
+
result[name] = query[name];
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return result;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Returns an array of values in the object sorted by the key
|
|
45
|
+
* We sort query params so that different orderings will result in a hit
|
|
46
|
+
* @param source Any object of key/value pairs, e.g. query, headers, cookies, etc...
|
|
47
|
+
*/
|
|
48
|
+
toSortedArray(source) {
|
|
49
|
+
return Object.keys(source)
|
|
50
|
+
.sort()
|
|
51
|
+
.map(key => [key, source[key]]);
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Returns the values of the request headers that are by default included in the caching key
|
|
55
|
+
*
|
|
56
|
+
* Notes:
|
|
57
|
+
* - Cache hash calculation has a connascence of algorithm with VCL moov_hash subroutine so if you
|
|
58
|
+
* are modifying it in any way, you have to modify VCL code generator in the same way. When in doubt
|
|
59
|
+
* the VCL cache hash calculation takes precendence as it is designed to serve production traffic
|
|
60
|
+
* and has been battle tested to do so reliably.
|
|
61
|
+
* - VCL cache hash includes some request/service properties that do no apply in local development:
|
|
62
|
+
* * build number does not exist for local development
|
|
63
|
+
* * flush timestamps for gradual flushing do not apply locally
|
|
64
|
+
*/
|
|
65
|
+
getDefaultHeaders(headers) {
|
|
66
|
+
return {
|
|
67
|
+
[constants_1.HTTP_HEADERS.host]: headers[constants_1.HTTP_HEADERS.host],
|
|
68
|
+
[constants_1.HTTP_HEADERS.acceptEncoding]: headers[constants_1.HTTP_HEADERS.acceptEncoding],
|
|
69
|
+
[constants_1.HTTP_HEADERS.x0Destination]: headers[constants_1.HTTP_HEADERS.x0Destination],
|
|
70
|
+
[constants_1.HTTP_HEADERS.x0Protocol]: headers[constants_1.HTTP_HEADERS.x0Protocol],
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
toString() {
|
|
74
|
+
return JSON.stringify({
|
|
75
|
+
pathname: this.pathname,
|
|
76
|
+
method: this.method,
|
|
77
|
+
query: this.toSortedArray(this.query),
|
|
78
|
+
body: hash(this.body),
|
|
79
|
+
headers: this.toSortedArray(this.headers),
|
|
80
|
+
cookies: this.toSortedArray(this.cookies),
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
exports.default = CacheKey;
|
|
85
|
+
/**
|
|
86
|
+
* Returns the sha256 hash of the provided string as hex
|
|
87
|
+
* @param data The data to hash
|
|
88
|
+
*/
|
|
89
|
+
function hash(data) {
|
|
90
|
+
if (!data)
|
|
91
|
+
return null;
|
|
92
|
+
const hash = crypto_1.default.createHash('sha256');
|
|
93
|
+
hash.update(data);
|
|
94
|
+
return hash.digest('hex');
|
|
95
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An object containing the config settings. This class is internal, but an instance
|
|
3
|
+
* can be obtained using.
|
|
4
|
+
*
|
|
5
|
+
* ```js
|
|
6
|
+
* import config from '@layer0/core/config'
|
|
7
|
+
* ```
|
|
8
|
+
*
|
|
9
|
+
* @internal
|
|
10
|
+
*/
|
|
11
|
+
declare class Config {
|
|
12
|
+
private values?;
|
|
13
|
+
readonly defaults: {
|
|
14
|
+
production: boolean;
|
|
15
|
+
backends: {};
|
|
16
|
+
includeNodeModules: boolean;
|
|
17
|
+
routes: string;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Get configuration by key. Will throw an error if no value is configured
|
|
21
|
+
* @param key The key to grab from the config.
|
|
22
|
+
* @param fallback A value to return if a value is not configured
|
|
23
|
+
*/
|
|
24
|
+
get(key: string, fallback?: any): any;
|
|
25
|
+
/**
|
|
26
|
+
* Returns true if a value has been configured for a specific key, otherwise false.
|
|
27
|
+
* @param key The key to test.
|
|
28
|
+
*/
|
|
29
|
+
has(key: string): boolean;
|
|
30
|
+
/**
|
|
31
|
+
* Returns all config values
|
|
32
|
+
*/
|
|
33
|
+
get all(): any;
|
|
34
|
+
/**
|
|
35
|
+
* Loads Layer0 config and sets the current configuration.
|
|
36
|
+
*/
|
|
37
|
+
private loadLayer0Config;
|
|
38
|
+
/**
|
|
39
|
+
* Load configuration from file
|
|
40
|
+
* @private
|
|
41
|
+
*/
|
|
42
|
+
private loadFromFile;
|
|
43
|
+
/**
|
|
44
|
+
* Load configuration from env var
|
|
45
|
+
* @private
|
|
46
|
+
*/
|
|
47
|
+
private loadFromEnv;
|
|
48
|
+
}
|
|
49
|
+
declare const _default: Config;
|
|
50
|
+
/**
|
|
51
|
+
* Create a Config instance.
|
|
52
|
+
*/
|
|
53
|
+
export default _default;
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const get_1 = __importDefault(require("./utils/get"));
|
|
7
|
+
const has_1 = __importDefault(require("./utils/has"));
|
|
8
|
+
const deepmerge_1 = __importDefault(require("deepmerge"));
|
|
9
|
+
const fs_1 = require("fs");
|
|
10
|
+
const path_1 = require("path");
|
|
11
|
+
const constants_1 = require("./constants");
|
|
12
|
+
const Validator_1 = __importDefault(require("./utils/Validator"));
|
|
13
|
+
const nonWebpackRequire_1 = __importDefault(require("./utils/nonWebpackRequire"));
|
|
14
|
+
/**
|
|
15
|
+
* An object containing the config settings. This class is internal, but an instance
|
|
16
|
+
* can be obtained using.
|
|
17
|
+
*
|
|
18
|
+
* ```js
|
|
19
|
+
* import config from '@layer0/core/config'
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* @internal
|
|
23
|
+
*/
|
|
24
|
+
class Config {
|
|
25
|
+
constructor() {
|
|
26
|
+
this.defaults = {
|
|
27
|
+
production: true,
|
|
28
|
+
backends: {},
|
|
29
|
+
includeNodeModules: false,
|
|
30
|
+
routes: ['routes.js', 'routes.ts', 'routes.cjs'].find(file => fs_1.existsSync(path_1.join(process.cwd(), file))) || 'routes.js',
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Get configuration by key. Will throw an error if no value is configured
|
|
35
|
+
* @param key The key to grab from the config.
|
|
36
|
+
* @param fallback A value to return if a value is not configured
|
|
37
|
+
*/
|
|
38
|
+
get(key, fallback) {
|
|
39
|
+
if (this.has(key)) {
|
|
40
|
+
return get_1.default(this.values, key);
|
|
41
|
+
}
|
|
42
|
+
else if (typeof fallback !== 'undefined') {
|
|
43
|
+
return fallback;
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
throw new Error(`Layer0 configuration key: "${key}" is not defined`);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Returns true if a value has been configured for a specific key, otherwise false.
|
|
51
|
+
* @param key The key to test.
|
|
52
|
+
*/
|
|
53
|
+
has(key) {
|
|
54
|
+
if (!this.values)
|
|
55
|
+
this.loadLayer0Config();
|
|
56
|
+
return has_1.default(this.values, key);
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Returns all config values
|
|
60
|
+
*/
|
|
61
|
+
get all() {
|
|
62
|
+
if (!this.values)
|
|
63
|
+
this.loadLayer0Config();
|
|
64
|
+
return this.values;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Loads Layer0 config and sets the current configuration.
|
|
68
|
+
*/
|
|
69
|
+
loadLayer0Config() {
|
|
70
|
+
const file = this.loadFromFile();
|
|
71
|
+
const env = this.loadFromEnv();
|
|
72
|
+
this.values = deepmerge_1.default.all([{}, this.defaults, file, env]);
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Load configuration from file
|
|
76
|
+
* @private
|
|
77
|
+
*/
|
|
78
|
+
loadFromFile() {
|
|
79
|
+
const configAbsPath = [
|
|
80
|
+
path_1.join(process.cwd(), constants_1.LAYER0_CONFIG_FILE),
|
|
81
|
+
path_1.join(process.cwd(), constants_1.LAYER0_CONFIG_FILE.replace('.js', '.cjs')),
|
|
82
|
+
].find(fs_1.existsSync);
|
|
83
|
+
if (configAbsPath) {
|
|
84
|
+
try {
|
|
85
|
+
const fileConfig = nonWebpackRequire_1.default(configAbsPath);
|
|
86
|
+
return Validator_1.default.validateLayer0FileConfiguration(fileConfig);
|
|
87
|
+
}
|
|
88
|
+
catch (e) {
|
|
89
|
+
throw new Error(`Failed to load ${configAbsPath}: ${e.message}`);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return {};
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Load configuration from env var
|
|
96
|
+
* @private
|
|
97
|
+
*/
|
|
98
|
+
loadFromEnv() {
|
|
99
|
+
if (process.env[constants_1.LAYER0_ENV_VARIABLES.config]) {
|
|
100
|
+
try {
|
|
101
|
+
return JSON.parse(process.env[constants_1.LAYER0_ENV_VARIABLES.config]);
|
|
102
|
+
}
|
|
103
|
+
catch (e) {
|
|
104
|
+
throw new Error(`Layer0 configuration variable: ${constants_1.LAYER0_ENV_VARIABLES.config}, is not parsable as json`);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return {};
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Create a Config instance.
|
|
112
|
+
*/
|
|
113
|
+
exports.default = new Config();
|