@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,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
/**
|
|
4
|
+
* This middleware adds getHeaders(), getHeader(name), setHeader(name, value), and removeHeader(name) to Express's
|
|
5
|
+
* request object, making it compatible with the request object we provide in serverless.
|
|
6
|
+
* See lambda/reqResMapper.ts
|
|
7
|
+
* @param request
|
|
8
|
+
* @param _response
|
|
9
|
+
* @param next
|
|
10
|
+
*/
|
|
11
|
+
const serverlessCompatibilityMiddleware = (request, _response, next) => {
|
|
12
|
+
// @ts-ignore
|
|
13
|
+
request.getHeader = (name) => {
|
|
14
|
+
return request.headers[name.toLowerCase()];
|
|
15
|
+
};
|
|
16
|
+
// @ts-ignore
|
|
17
|
+
request.getHeaders = () => {
|
|
18
|
+
return request.headers;
|
|
19
|
+
};
|
|
20
|
+
// @ts-ignore
|
|
21
|
+
request.setHeader = (name, value) => {
|
|
22
|
+
request.headers[name.toLowerCase()] = value;
|
|
23
|
+
};
|
|
24
|
+
// @ts-ignore
|
|
25
|
+
request.removeHeader = (name) => {
|
|
26
|
+
delete request.headers[name.toLowerCase()];
|
|
27
|
+
};
|
|
28
|
+
next();
|
|
29
|
+
};
|
|
30
|
+
exports.default = serverlessCompatibilityMiddleware;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { ProxyOptions } from '../router/ResponseWriter';
|
|
2
|
+
import EdgeResponseWriter from './EdgeResponseWriter';
|
|
3
|
+
export default class EdgeErrorPageResponseWriter extends EdgeResponseWriter {
|
|
4
|
+
/**
|
|
5
|
+
* Disables proxy from the edge for error pages
|
|
6
|
+
*
|
|
7
|
+
* @param backend
|
|
8
|
+
* @param config
|
|
9
|
+
* @param config.path
|
|
10
|
+
*/
|
|
11
|
+
proxy: (_backend: string, _opts?: ProxyOptions) => never;
|
|
12
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
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 EdgeResponseWriter_1 = __importDefault(require("./EdgeResponseWriter"));
|
|
7
|
+
class EdgeErrorPageResponseWriter extends EdgeResponseWriter_1.default {
|
|
8
|
+
constructor() {
|
|
9
|
+
super(...arguments);
|
|
10
|
+
/**
|
|
11
|
+
* Disables proxy from the edge for error pages
|
|
12
|
+
*
|
|
13
|
+
* @param backend
|
|
14
|
+
* @param config
|
|
15
|
+
* @param config.path
|
|
16
|
+
*/
|
|
17
|
+
this.proxy = (_backend, _opts = {}) => {
|
|
18
|
+
throw new Error('Proxy cannot be used inside router.catch()');
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
exports.default = EdgeErrorPageResponseWriter;
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { CacheOptions } from '../router/CacheOptions';
|
|
2
|
+
import { EdgeRule, EdgeRoute } from '../types';
|
|
3
|
+
import { ProxyOptions, RedirectOptions, ServeStaticOptions, VerifyJwtOptions } from '../router/ResponseWriter';
|
|
4
|
+
import ResponseWriter from '../router/ResponseWriter';
|
|
5
|
+
import Route from '../router/Route';
|
|
6
|
+
import RequestContext from '../router/RequestContext';
|
|
7
|
+
import Request from '../router/Request';
|
|
8
|
+
import Response from '../router/Response';
|
|
9
|
+
/**
|
|
10
|
+
* A substitute implementation of `ResponseWriter` that creates the contents of `layer0.json`, which
|
|
11
|
+
* is used to generate edge code.
|
|
12
|
+
*/
|
|
13
|
+
export default class EdgeResponseWriter extends ResponseWriter {
|
|
14
|
+
readonly edgeConfig: EdgeRule;
|
|
15
|
+
stream: () => void;
|
|
16
|
+
/**
|
|
17
|
+
* @param req The `HttpRequest` object
|
|
18
|
+
* @param res The `HttpResponse` object
|
|
19
|
+
* @param route The route being hit
|
|
20
|
+
* @param context The request context
|
|
21
|
+
*/
|
|
22
|
+
constructor(_req: any, _res: any, route: Route, context: RequestContext);
|
|
23
|
+
setRequestHeader: (name: string, value: string) => void;
|
|
24
|
+
updateRequestHeader: (name: string, match: RegExp, replace: string) => void;
|
|
25
|
+
removeRequestHeader: (name: string) => void;
|
|
26
|
+
setResponseHeader: (name: string, value: string) => void;
|
|
27
|
+
updateResponseHeader: (name: string, match: RegExp, replace: string) => void;
|
|
28
|
+
removeResponseHeader: (name: string) => void;
|
|
29
|
+
/**
|
|
30
|
+
* Note:
|
|
31
|
+
* * We allow x-0-surrogate-key upstream response headers to be set by the edge logic,
|
|
32
|
+
* to equalizes what can be done in the serverless vs what can be done in the edge.
|
|
33
|
+
* * We do not extend this to other response headers because there are many that are
|
|
34
|
+
* set internally in VCL (e.g. x-0-version, x-0-shield-response, x-0-isg-loading-page-served,
|
|
35
|
+
* x-0-caching-status, and so on) which if affected would interfere with the correct
|
|
36
|
+
* functioning of the service.
|
|
37
|
+
*/
|
|
38
|
+
setUpstreamResponseHeader: (name: string, value: string) => void;
|
|
39
|
+
updateUpstreamResponseHeader: (name: string, match: RegExp, replace: string) => void;
|
|
40
|
+
removeUpstreamResponseHeader: (name: string) => void;
|
|
41
|
+
addResponseCookie: (name: string, value: string) => void;
|
|
42
|
+
updateResponseCookie: (name: string, match: RegExp, replace: string) => void;
|
|
43
|
+
removeResponseCookie: (name: string) => void;
|
|
44
|
+
addUpstreamResponseCookie: (name: string, value: string) => void;
|
|
45
|
+
updateUpstreamResponseCookie: (name: string, match: RegExp, replace: string) => void;
|
|
46
|
+
removeUpstreamResponseCookie: (name: string) => void;
|
|
47
|
+
private shouldParseQueryString;
|
|
48
|
+
/**
|
|
49
|
+
* Sends a redirect from the edge
|
|
50
|
+
*
|
|
51
|
+
* In case of a complex redirect where the router needs to parse query string
|
|
52
|
+
* from the matched request, or inject query strings in the redirect, we delegate
|
|
53
|
+
* the response to Serverless compute and cache it for a very long time.
|
|
54
|
+
*
|
|
55
|
+
*
|
|
56
|
+
* @param to The destination URL
|
|
57
|
+
* @param statusCode The http response status.
|
|
58
|
+
*/
|
|
59
|
+
redirect: (to: string, options?: RedirectOptions) => void;
|
|
60
|
+
/**
|
|
61
|
+
* Serves static assets.
|
|
62
|
+
* @param path The request path
|
|
63
|
+
*/
|
|
64
|
+
serveStatic: (path: string, { permanent, exclude, onNotFound, loadingPage, expiresSeconds, statusCode, statusMessage, disableAutoPublish, }?: ServeStaticOptions) => Promise<void>;
|
|
65
|
+
verifyJwt: (options: VerifyJwtOptions) => Promise<void>;
|
|
66
|
+
/**
|
|
67
|
+
* Proxies from the edge
|
|
68
|
+
* @param backend
|
|
69
|
+
* @param config
|
|
70
|
+
* @param config.path
|
|
71
|
+
*/
|
|
72
|
+
proxy: (backend: string, { path, transformResponse, transformRequest }?: ProxyOptions) => Promise<void>;
|
|
73
|
+
/**
|
|
74
|
+
* Rewrites the request path.
|
|
75
|
+
*/
|
|
76
|
+
updatePath: (destination: string) => void;
|
|
77
|
+
/**
|
|
78
|
+
* Sets the edge route and injects the x-0-route header value.
|
|
79
|
+
*
|
|
80
|
+
* Notes:
|
|
81
|
+
* - We need `x-0-route` header for statistics so we log it in the edge.
|
|
82
|
+
* We thus set it on the request, which allows us to log it, but in the edge
|
|
83
|
+
* code we unset this header before going upstream which avoids any leaking.
|
|
84
|
+
* - route.criteria.path can be null (fallback, or different match method)
|
|
85
|
+
* so we check that the path exists or injecting the request path as a fallback.
|
|
86
|
+
* - We don't allow upstream to dicate the `vary` header - developers can use
|
|
87
|
+
* our custom cache key mechanism which is a superset anyways. The only
|
|
88
|
+
* header we allow in the `vary` is `accept-encoding` which then shards
|
|
89
|
+
* the cache depending on the encoding that the user agent is accepting.
|
|
90
|
+
* In any case we already include `accept-encoding` in the default cache
|
|
91
|
+
* key so including it in `vary` is superfluous *except* that downstream
|
|
92
|
+
* caches might use it.
|
|
93
|
+
*
|
|
94
|
+
* @private
|
|
95
|
+
*/
|
|
96
|
+
setEdgeRoute(route: EdgeRoute): void;
|
|
97
|
+
/**
|
|
98
|
+
* Creates the edge cache config
|
|
99
|
+
* @param config
|
|
100
|
+
*/
|
|
101
|
+
cache: (config: CacheOptions) => void;
|
|
102
|
+
/**
|
|
103
|
+
* Sends string content back to client. If content is a string, the response will be sent
|
|
104
|
+
* directly from the edge. If it is a function, the request will be computed by a JavaScript worker.
|
|
105
|
+
* StatusCode defaults to 200 on routes which is set by createEdgeConfig. On error routes we send the status code
|
|
106
|
+
* as null, since we need to preserve the status code of the failed request unless it is provided.
|
|
107
|
+
* @param content The content to send to the browser
|
|
108
|
+
* @param statusCode The HTTP status code.
|
|
109
|
+
* @param statusMessage The HTTP status message
|
|
110
|
+
*/
|
|
111
|
+
send: (content: string | (() => string), statusCode: number | undefined, statusMessage?: string | undefined) => void;
|
|
112
|
+
/**
|
|
113
|
+
* Computed responses are always proxied to the serverless backend.
|
|
114
|
+
* @param callback
|
|
115
|
+
*/
|
|
116
|
+
compute: (callback: (request: Request, response: Response) => void | Promise<void>) => void;
|
|
117
|
+
/**
|
|
118
|
+
* Passes request and response to the specified callback, which should
|
|
119
|
+
* handle rendering the response as a string
|
|
120
|
+
*/
|
|
121
|
+
render: () => Promise<void>;
|
|
122
|
+
}
|
|
@@ -0,0 +1,451 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
3
|
+
if (mod && mod.__esModule) return mod;
|
|
4
|
+
var result = {};
|
|
5
|
+
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
|
|
6
|
+
result["default"] = mod;
|
|
7
|
+
return result;
|
|
8
|
+
};
|
|
9
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
10
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
11
|
+
};
|
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
+
const convertParamsToBackReferences_1 = __importStar(require("../utils/convertParamsToBackReferences"));
|
|
14
|
+
const constants_1 = require("../constants");
|
|
15
|
+
const get_1 = __importDefault(require("../utils/get"));
|
|
16
|
+
const isEmpty_1 = __importDefault(require("../utils/isEmpty"));
|
|
17
|
+
const ResponseWriter_1 = __importDefault(require("../router/ResponseWriter"));
|
|
18
|
+
const Route_1 = __importDefault(require("../router/Route"));
|
|
19
|
+
const validateBackends_1 = __importDefault(require("../utils/validateBackends"));
|
|
20
|
+
const validateWriter_1 = require("../router/validateWriter");
|
|
21
|
+
const universalRouteUtils_1 = require("../router/universalRouteUtils");
|
|
22
|
+
const slash_1 = __importDefault(require("slash"));
|
|
23
|
+
const FAR_FUTURE_TTL = 60 * 60 * 24 * 365 * 10;
|
|
24
|
+
/**
|
|
25
|
+
* A substitute implementation of `ResponseWriter` that creates the contents of `layer0.json`, which
|
|
26
|
+
* is used to generate edge code.
|
|
27
|
+
*/
|
|
28
|
+
class EdgeResponseWriter extends ResponseWriter_1.default {
|
|
29
|
+
/**
|
|
30
|
+
* @param req The `HttpRequest` object
|
|
31
|
+
* @param res The `HttpResponse` object
|
|
32
|
+
* @param route The route being hit
|
|
33
|
+
* @param context The request context
|
|
34
|
+
*/
|
|
35
|
+
constructor(_req, _res, route, context) {
|
|
36
|
+
super(_req, _res, route, context); // istanbul ignore next - this is a known issue with TypeScript
|
|
37
|
+
// Many of the methods such as updateRequestHeader assume transform.request
|
|
38
|
+
// and transform.response are initialized to empty arrays by the constructor.
|
|
39
|
+
this.stream = () => { };
|
|
40
|
+
this.setRequestHeader = (name, value) => {
|
|
41
|
+
validateWriter_1.validateSetRequestHeader(name);
|
|
42
|
+
this.edgeConfig.transform.request.push({
|
|
43
|
+
action: constants_1.ACTIONS.setHeader,
|
|
44
|
+
name,
|
|
45
|
+
value,
|
|
46
|
+
});
|
|
47
|
+
};
|
|
48
|
+
this.updateRequestHeader = (name, match, replace) => {
|
|
49
|
+
validateWriter_1.validateUpdateRequestHeader(name);
|
|
50
|
+
this.edgeConfig.transform.request.push({
|
|
51
|
+
action: constants_1.ACTIONS.updateHeader,
|
|
52
|
+
name,
|
|
53
|
+
valueRegex: match.source,
|
|
54
|
+
replaceValue: replace,
|
|
55
|
+
});
|
|
56
|
+
};
|
|
57
|
+
this.removeRequestHeader = (name) => {
|
|
58
|
+
validateWriter_1.validateRemoveRequestHeader(name);
|
|
59
|
+
this.edgeConfig.transform.request.push({
|
|
60
|
+
action: constants_1.ACTIONS.removeHeader,
|
|
61
|
+
name,
|
|
62
|
+
});
|
|
63
|
+
};
|
|
64
|
+
this.setResponseHeader = (name, value) => {
|
|
65
|
+
validateWriter_1.validateSetResponseHeader(name);
|
|
66
|
+
this.edgeConfig.transform.response.push({
|
|
67
|
+
action: constants_1.ACTIONS.setHeader,
|
|
68
|
+
name,
|
|
69
|
+
value,
|
|
70
|
+
});
|
|
71
|
+
};
|
|
72
|
+
this.updateResponseHeader = (name, match, replace) => {
|
|
73
|
+
validateWriter_1.validateUpdateResponseHeader(name);
|
|
74
|
+
this.edgeConfig.transform.response.push({
|
|
75
|
+
action: constants_1.ACTIONS.updateHeader,
|
|
76
|
+
name,
|
|
77
|
+
valueRegex: match.source,
|
|
78
|
+
replaceValue: replace,
|
|
79
|
+
});
|
|
80
|
+
};
|
|
81
|
+
this.removeResponseHeader = (name) => {
|
|
82
|
+
validateWriter_1.validateRemoveResponseHeader(name);
|
|
83
|
+
this.edgeConfig.transform.response.push({
|
|
84
|
+
action: constants_1.ACTIONS.removeHeader,
|
|
85
|
+
name,
|
|
86
|
+
});
|
|
87
|
+
};
|
|
88
|
+
/**
|
|
89
|
+
* Note:
|
|
90
|
+
* * We allow x-0-surrogate-key upstream response headers to be set by the edge logic,
|
|
91
|
+
* to equalizes what can be done in the serverless vs what can be done in the edge.
|
|
92
|
+
* * We do not extend this to other response headers because there are many that are
|
|
93
|
+
* set internally in VCL (e.g. x-0-version, x-0-shield-response, x-0-isg-loading-page-served,
|
|
94
|
+
* x-0-caching-status, and so on) which if affected would interfere with the correct
|
|
95
|
+
* functioning of the service.
|
|
96
|
+
*/
|
|
97
|
+
this.setUpstreamResponseHeader = (name, value) => {
|
|
98
|
+
if (name.toLowerCase() != constants_1.HTTP_HEADERS.x0SurrogateKey) {
|
|
99
|
+
validateWriter_1.validateSetResponseHeader(name);
|
|
100
|
+
}
|
|
101
|
+
this.edgeConfig.transform.upstreamResponse.push({
|
|
102
|
+
action: constants_1.ACTIONS.setHeader,
|
|
103
|
+
name,
|
|
104
|
+
value,
|
|
105
|
+
});
|
|
106
|
+
};
|
|
107
|
+
this.updateUpstreamResponseHeader = (name, match, replace) => {
|
|
108
|
+
validateWriter_1.validateUpdateResponseHeader(name);
|
|
109
|
+
this.edgeConfig.transform.upstreamResponse.push({
|
|
110
|
+
action: constants_1.ACTIONS.updateHeader,
|
|
111
|
+
name,
|
|
112
|
+
valueRegex: match.source,
|
|
113
|
+
replaceValue: replace,
|
|
114
|
+
});
|
|
115
|
+
};
|
|
116
|
+
this.removeUpstreamResponseHeader = (name) => {
|
|
117
|
+
validateWriter_1.validateRemoveResponseHeader(name);
|
|
118
|
+
this.edgeConfig.transform.upstreamResponse.push({
|
|
119
|
+
action: constants_1.ACTIONS.removeHeader,
|
|
120
|
+
name,
|
|
121
|
+
});
|
|
122
|
+
};
|
|
123
|
+
this.addResponseCookie = (name, value) => {
|
|
124
|
+
this.edgeConfig.transform.response.push({
|
|
125
|
+
action: constants_1.ACTIONS.addCookie,
|
|
126
|
+
name,
|
|
127
|
+
value,
|
|
128
|
+
});
|
|
129
|
+
};
|
|
130
|
+
this.updateResponseCookie = (name, match, replace) => {
|
|
131
|
+
this.edgeConfig.transform.response.push({
|
|
132
|
+
action: constants_1.ACTIONS.updateCookie,
|
|
133
|
+
name,
|
|
134
|
+
valueRegex: match.source,
|
|
135
|
+
replaceValue: replace,
|
|
136
|
+
});
|
|
137
|
+
};
|
|
138
|
+
this.removeResponseCookie = (name) => {
|
|
139
|
+
this.edgeConfig.transform.response.push({
|
|
140
|
+
action: constants_1.ACTIONS.removeCookie,
|
|
141
|
+
name,
|
|
142
|
+
});
|
|
143
|
+
};
|
|
144
|
+
this.addUpstreamResponseCookie = (name, value) => {
|
|
145
|
+
this.edgeConfig.transform.upstreamResponse.push({
|
|
146
|
+
action: constants_1.ACTIONS.addCookie,
|
|
147
|
+
name,
|
|
148
|
+
value,
|
|
149
|
+
});
|
|
150
|
+
};
|
|
151
|
+
this.updateUpstreamResponseCookie = (name, match, replace) => {
|
|
152
|
+
this.edgeConfig.transform.upstreamResponse.push({
|
|
153
|
+
action: constants_1.ACTIONS.updateCookie,
|
|
154
|
+
name,
|
|
155
|
+
valueRegex: match.source,
|
|
156
|
+
replaceValue: replace,
|
|
157
|
+
});
|
|
158
|
+
};
|
|
159
|
+
this.removeUpstreamResponseCookie = (name) => {
|
|
160
|
+
this.edgeConfig.transform.upstreamResponse.push({
|
|
161
|
+
action: constants_1.ACTIONS.removeCookie,
|
|
162
|
+
name,
|
|
163
|
+
});
|
|
164
|
+
};
|
|
165
|
+
/**
|
|
166
|
+
* Sends a redirect from the edge
|
|
167
|
+
*
|
|
168
|
+
* In case of a complex redirect where the router needs to parse query string
|
|
169
|
+
* from the matched request, or inject query strings in the redirect, we delegate
|
|
170
|
+
* the response to Serverless compute and cache it for a very long time.
|
|
171
|
+
*
|
|
172
|
+
*
|
|
173
|
+
* @param to The destination URL
|
|
174
|
+
* @param statusCode The http response status.
|
|
175
|
+
*/
|
|
176
|
+
this.redirect = (to, options = {}) => {
|
|
177
|
+
const { to: toPath, statusCode, query } = this._readRedirectParams(to, options);
|
|
178
|
+
const addQueryString = !isEmpty_1.default(query);
|
|
179
|
+
const writerParam = convertParamsToBackReferences_1.getBackReferences(Route_1.default.pathWithQueryString(toPath, query));
|
|
180
|
+
const parseQueryString = this.shouldParseQueryString(writerParam);
|
|
181
|
+
// Complex redirects manipulating query string are delegated to compute and cached "for ever".
|
|
182
|
+
// Cache key includes query string by default so every path / query string
|
|
183
|
+
// combination is cached independently, which is what we want as redirects
|
|
184
|
+
// location header includes original request's query string
|
|
185
|
+
if (parseQueryString || addQueryString) {
|
|
186
|
+
this.cache({
|
|
187
|
+
edge: {
|
|
188
|
+
maxAgeSeconds: FAR_FUTURE_TTL,
|
|
189
|
+
},
|
|
190
|
+
});
|
|
191
|
+
this.proxy(constants_1.BACKENDS.js);
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
this.setEdgeRoute({ action: constants_1.ACTIONS.syntheticRes, statusCode });
|
|
195
|
+
this.edgeConfig.transform.response.push({
|
|
196
|
+
action: constants_1.ACTIONS.setHeader,
|
|
197
|
+
name: constants_1.HTTP_HEADERS.location,
|
|
198
|
+
value: '${url}',
|
|
199
|
+
});
|
|
200
|
+
if (this.route.criteria.path) {
|
|
201
|
+
// This applies the 'to' argument, interpolate path params from the original request
|
|
202
|
+
// and appends original request's query strings (hence the added path-to-regexp matcher and added
|
|
203
|
+
// back reference)
|
|
204
|
+
const valueRegex = Route_1.default.toEdgeRegexString(this.route.criteria.path + '{(\\?[^#]+)}?');
|
|
205
|
+
const matchParams = convertParamsToBackReferences_1.getBackReferences(this.route.criteria.path);
|
|
206
|
+
const replaceValue = convertParamsToBackReferences_1.default(this.route.criteria.path, toPath) +
|
|
207
|
+
`\\${Object.keys(matchParams).length + 1}`;
|
|
208
|
+
this.edgeConfig.transform.response.push({
|
|
209
|
+
action: constants_1.ACTIONS.updateHeader,
|
|
210
|
+
name: constants_1.HTTP_HEADERS.location,
|
|
211
|
+
valueRegex,
|
|
212
|
+
replaceValue,
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
else {
|
|
216
|
+
// This is a very special case where there is no path matcher, which is generally
|
|
217
|
+
// the case for fallback routes.
|
|
218
|
+
// We only extract the query string from the request path and
|
|
219
|
+
// apply them to the redirect 'to'.
|
|
220
|
+
this.edgeConfig.transform.response.push({
|
|
221
|
+
action: constants_1.ACTIONS.updateHeader,
|
|
222
|
+
name: constants_1.HTTP_HEADERS.location,
|
|
223
|
+
valueRegex: Route_1.default.toEdgeRegexString('([^?]*){(\\?[^#]+)}?'),
|
|
224
|
+
replaceValue: `${toPath}\\2`,
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
/**
|
|
229
|
+
* Serves static assets.
|
|
230
|
+
* @param path The request path
|
|
231
|
+
*/
|
|
232
|
+
this.serveStatic = (path, { permanent, exclude, onNotFound, loadingPage, expiresSeconds, statusCode, statusMessage, disableAutoPublish, } = {}) => {
|
|
233
|
+
if (path && !path.startsWith('/'))
|
|
234
|
+
path = `/${path}`;
|
|
235
|
+
const fsPath = [];
|
|
236
|
+
let loadingPageTransform;
|
|
237
|
+
if (loadingPage) {
|
|
238
|
+
fsPath.push(loadingPage);
|
|
239
|
+
loadingPageTransform = {
|
|
240
|
+
valueRegex: this.route.toEdgeRegexString(),
|
|
241
|
+
replaceValue: convertParamsToBackReferences_1.default(this.route.criteria.path, loadingPage),
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
if (path) {
|
|
245
|
+
this.updatePath(slash_1.default(path));
|
|
246
|
+
}
|
|
247
|
+
if (typeof path === 'string') {
|
|
248
|
+
fsPath.push(path);
|
|
249
|
+
}
|
|
250
|
+
const isPermanent = permanent === true || get_1.default(permanent, 'enabled') || false;
|
|
251
|
+
this.setEdgeRoute({
|
|
252
|
+
action: constants_1.ACTIONS.proxy,
|
|
253
|
+
backend: isPermanent ? constants_1.BACKENDS.permanentStatic : constants_1.BACKENDS.static,
|
|
254
|
+
fsPath,
|
|
255
|
+
fsExclude: exclude,
|
|
256
|
+
fallbackToCompute: onNotFound != null,
|
|
257
|
+
loadingPage: loadingPageTransform,
|
|
258
|
+
expiresSeconds,
|
|
259
|
+
statusMessage,
|
|
260
|
+
statusCode,
|
|
261
|
+
disableAutoPublish,
|
|
262
|
+
});
|
|
263
|
+
return Promise.resolve();
|
|
264
|
+
};
|
|
265
|
+
this.verifyJwt = (options) => {
|
|
266
|
+
this.edgeConfig.jwtVerification = options;
|
|
267
|
+
return Promise.resolve();
|
|
268
|
+
};
|
|
269
|
+
/**
|
|
270
|
+
* Proxies from the edge
|
|
271
|
+
* @param backend
|
|
272
|
+
* @param config
|
|
273
|
+
* @param config.path
|
|
274
|
+
*/
|
|
275
|
+
this.proxy = (backend, { path, transformResponse, transformRequest } = {}) => {
|
|
276
|
+
validateBackends_1.default(backend);
|
|
277
|
+
if (transformResponse != null || transformRequest != null || typeof path === 'function') {
|
|
278
|
+
backend = constants_1.BACKENDS.js;
|
|
279
|
+
}
|
|
280
|
+
else if (typeof path === 'string') {
|
|
281
|
+
// Only rewrite the request path if we're not going through compute, otherwise the router
|
|
282
|
+
// in compute would match the wrong route
|
|
283
|
+
this.updatePath(path);
|
|
284
|
+
}
|
|
285
|
+
this.setEdgeRoute({
|
|
286
|
+
action: constants_1.ACTIONS.proxy,
|
|
287
|
+
backend,
|
|
288
|
+
});
|
|
289
|
+
return Promise.resolve();
|
|
290
|
+
};
|
|
291
|
+
/**
|
|
292
|
+
* Rewrites the request path.
|
|
293
|
+
*/
|
|
294
|
+
this.updatePath = (destination) => {
|
|
295
|
+
let replacePathValue = convertParamsToBackReferences_1.default(this.route.criteria.path, destination);
|
|
296
|
+
// Makes path definition more flexible, allowing path with no / prefix (ex: { path: ':path' })
|
|
297
|
+
if (replacePathValue[0] !== '/') {
|
|
298
|
+
replacePathValue = `/${replacePathValue}`;
|
|
299
|
+
}
|
|
300
|
+
this.edgeConfig.transform.request.push({
|
|
301
|
+
action: constants_1.ACTIONS.updatePath,
|
|
302
|
+
valueRegex: this.route.toEdgeRegexString(),
|
|
303
|
+
replaceValue: replacePathValue,
|
|
304
|
+
});
|
|
305
|
+
};
|
|
306
|
+
/**
|
|
307
|
+
* Creates the edge cache config
|
|
308
|
+
* @param config
|
|
309
|
+
*/
|
|
310
|
+
this.cache = (config) => {
|
|
311
|
+
const { edge, browser, key } = config;
|
|
312
|
+
if (edge) {
|
|
313
|
+
const { maxAgeSeconds, staleWhileRevalidateSeconds, forcePrivateCaching } = edge;
|
|
314
|
+
this.edgeConfig.cache = {
|
|
315
|
+
maxAgeSeconds,
|
|
316
|
+
staleWhileRevalidateSeconds,
|
|
317
|
+
forcePrivateCaching,
|
|
318
|
+
};
|
|
319
|
+
}
|
|
320
|
+
else if (edge === false) {
|
|
321
|
+
this.edgeConfig.cache = {
|
|
322
|
+
disabled: true,
|
|
323
|
+
};
|
|
324
|
+
}
|
|
325
|
+
if (key) {
|
|
326
|
+
if (!this.edgeConfig.cache) {
|
|
327
|
+
this.edgeConfig.cache = {};
|
|
328
|
+
}
|
|
329
|
+
this.edgeConfig.cache.customKey = key.toJSON();
|
|
330
|
+
}
|
|
331
|
+
if (browser || browser === false) {
|
|
332
|
+
const { maxAgeSeconds, serviceWorkerSeconds } = browser || { maxAgeSeconds: 0 };
|
|
333
|
+
if (serviceWorkerSeconds && maxAgeSeconds && maxAgeSeconds > 0) {
|
|
334
|
+
throw new Error('When service worker caching is set, the browser max age must be set to zero. Refer to https://docs.layer0.co/guides/caching#section_caching_a_response');
|
|
335
|
+
}
|
|
336
|
+
if (maxAgeSeconds === 0) {
|
|
337
|
+
this.edgeConfig.transform.response.push({
|
|
338
|
+
action: constants_1.ACTIONS.setHeader,
|
|
339
|
+
name: constants_1.HTTP_HEADERS.cacheControl,
|
|
340
|
+
value: 'private, no-store, no-cache',
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
else if (maxAgeSeconds) {
|
|
344
|
+
// Here we use "private" instead of "public" to prevent downstream CDNs from caching the asset
|
|
345
|
+
// Since we already have an explicit way to cache at edge, we choose to keep browser and edge
|
|
346
|
+
// caching totally separate.
|
|
347
|
+
this.edgeConfig.transform.response.push({
|
|
348
|
+
action: constants_1.ACTIONS.setHeader,
|
|
349
|
+
name: constants_1.HTTP_HEADERS.cacheControl,
|
|
350
|
+
value: `private, max-age=${maxAgeSeconds}`,
|
|
351
|
+
});
|
|
352
|
+
}
|
|
353
|
+
if (serviceWorkerSeconds) {
|
|
354
|
+
this.edgeConfig.transform.response.push({
|
|
355
|
+
action: constants_1.ACTIONS.setHeader,
|
|
356
|
+
name: constants_1.HTTP_HEADERS.xSwCacheControl,
|
|
357
|
+
value: `max-age=${serviceWorkerSeconds}`,
|
|
358
|
+
});
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
};
|
|
362
|
+
/**
|
|
363
|
+
* Sends string content back to client. If content is a string, the response will be sent
|
|
364
|
+
* directly from the edge. If it is a function, the request will be computed by a JavaScript worker.
|
|
365
|
+
* StatusCode defaults to 200 on routes which is set by createEdgeConfig. On error routes we send the status code
|
|
366
|
+
* as null, since we need to preserve the status code of the failed request unless it is provided.
|
|
367
|
+
* @param content The content to send to the browser
|
|
368
|
+
* @param statusCode The HTTP status code.
|
|
369
|
+
* @param statusMessage The HTTP status message
|
|
370
|
+
*/
|
|
371
|
+
this.send = (content, statusCode, statusMessage) => {
|
|
372
|
+
if (typeof content === 'function') {
|
|
373
|
+
this.render();
|
|
374
|
+
}
|
|
375
|
+
else {
|
|
376
|
+
this.setEdgeRoute({
|
|
377
|
+
action: constants_1.ACTIONS.syntheticRes,
|
|
378
|
+
statusCode,
|
|
379
|
+
statusMessage,
|
|
380
|
+
content,
|
|
381
|
+
});
|
|
382
|
+
}
|
|
383
|
+
};
|
|
384
|
+
/**
|
|
385
|
+
* Computed responses are always proxied to the serverless backend.
|
|
386
|
+
* @param callback
|
|
387
|
+
*/
|
|
388
|
+
this.compute = (callback) => {
|
|
389
|
+
this.setEdgeRoute({
|
|
390
|
+
action: constants_1.ACTIONS.proxy,
|
|
391
|
+
backend: constants_1.BACKENDS.js,
|
|
392
|
+
});
|
|
393
|
+
};
|
|
394
|
+
/**
|
|
395
|
+
* Passes request and response to the specified callback, which should
|
|
396
|
+
* handle rendering the response as a string
|
|
397
|
+
*/
|
|
398
|
+
this.render = () => {
|
|
399
|
+
this.setEdgeRoute({
|
|
400
|
+
action: constants_1.ACTIONS.proxy,
|
|
401
|
+
backend: constants_1.BACKENDS.js,
|
|
402
|
+
});
|
|
403
|
+
return Promise.resolve();
|
|
404
|
+
};
|
|
405
|
+
this.edgeConfig = {
|
|
406
|
+
transform: {
|
|
407
|
+
request: [],
|
|
408
|
+
upstreamResponse: [],
|
|
409
|
+
response: [],
|
|
410
|
+
},
|
|
411
|
+
};
|
|
412
|
+
}
|
|
413
|
+
shouldParseQueryString(writerParam) {
|
|
414
|
+
const query = this.route.criteria.query;
|
|
415
|
+
if (query === undefined) {
|
|
416
|
+
return false;
|
|
417
|
+
}
|
|
418
|
+
return Object.keys(query).some(key => {
|
|
419
|
+
const value = query[key];
|
|
420
|
+
return universalRouteUtils_1.isParamMatcher(value) && !!writerParam[key];
|
|
421
|
+
});
|
|
422
|
+
}
|
|
423
|
+
/**
|
|
424
|
+
* Sets the edge route and injects the x-0-route header value.
|
|
425
|
+
*
|
|
426
|
+
* Notes:
|
|
427
|
+
* - We need `x-0-route` header for statistics so we log it in the edge.
|
|
428
|
+
* We thus set it on the request, which allows us to log it, but in the edge
|
|
429
|
+
* code we unset this header before going upstream which avoids any leaking.
|
|
430
|
+
* - route.criteria.path can be null (fallback, or different match method)
|
|
431
|
+
* so we check that the path exists or injecting the request path as a fallback.
|
|
432
|
+
* - We don't allow upstream to dicate the `vary` header - developers can use
|
|
433
|
+
* our custom cache key mechanism which is a superset anyways. The only
|
|
434
|
+
* header we allow in the `vary` is `accept-encoding` which then shards
|
|
435
|
+
* the cache depending on the encoding that the user agent is accepting.
|
|
436
|
+
* In any case we already include `accept-encoding` in the default cache
|
|
437
|
+
* key so including it in `vary` is superfluous *except* that downstream
|
|
438
|
+
* caches might use it.
|
|
439
|
+
*
|
|
440
|
+
* @private
|
|
441
|
+
*/
|
|
442
|
+
setEdgeRoute(route) {
|
|
443
|
+
this.edgeConfig.route = route;
|
|
444
|
+
this.edgeConfig.transform.upstreamResponse.push({
|
|
445
|
+
action: constants_1.ACTIONS.setHeader,
|
|
446
|
+
name: constants_1.HTTP_HEADERS.vary,
|
|
447
|
+
value: constants_1.HTTP_HEADERS.acceptEncoding,
|
|
448
|
+
});
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
exports.default = EdgeResponseWriter;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Router } from '../router';
|
|
2
|
+
import { EdgeBodyMatcher, EdgeRegexMatcher, EdgeConfig } from '../types';
|
|
3
|
+
export interface BuildMatcherFromRouteResult {
|
|
4
|
+
match: (EdgeRegexMatcher | EdgeBodyMatcher)[];
|
|
5
|
+
matchResp: EdgeRegexMatcher[];
|
|
6
|
+
matchExplicitMethod?: string;
|
|
7
|
+
matchProtocolJavaScriptRegExpSource?: string;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Creates an outer edge manager config for the specified router by iterating through
|
|
11
|
+
* each route and calling it with a mock request
|
|
12
|
+
* and response.
|
|
13
|
+
* @param router
|
|
14
|
+
* @return An object containing the Edge config
|
|
15
|
+
*/
|
|
16
|
+
export default function createEdgeConfig(router: Router): Promise<EdgeConfig>;
|