@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,181 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
6
|
+
if (mod && mod.__esModule) return mod;
|
|
7
|
+
var result = {};
|
|
8
|
+
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
|
|
9
|
+
result["default"] = mod;
|
|
10
|
+
return result;
|
|
11
|
+
};
|
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
+
/* istanbul ignore file */
|
|
14
|
+
const constants_1 = require("../constants");
|
|
15
|
+
const fs_extra_1 = require("fs-extra");
|
|
16
|
+
const path_1 = require("path");
|
|
17
|
+
const paths_1 = require("../deploy/paths");
|
|
18
|
+
const timing_1 = require("../timing");
|
|
19
|
+
const loadRouter_1 = __importDefault(require("../router/loadRouter"));
|
|
20
|
+
const nonWebpackRequire_1 = __importDefault(require("../utils/nonWebpackRequire"));
|
|
21
|
+
const reqResMapper_1 = __importDefault(require("./reqResMapper"));
|
|
22
|
+
const stdStreamsWrapper_1 = __importStar(require("./stdStreamsWrapper"));
|
|
23
|
+
const getBodyLoggingData_1 = __importDefault(require("./getBodyLoggingData"));
|
|
24
|
+
const lambdaInstance_1 = require("./lambdaInstance");
|
|
25
|
+
const config_1 = __importDefault(require("../config"));
|
|
26
|
+
const router = loadRouter_1.default('./' + path_1.basename(config_1.default.get('routes')));
|
|
27
|
+
/**
|
|
28
|
+
* This is the user lambda entry point
|
|
29
|
+
* @param event A lambda event
|
|
30
|
+
* @param instance The lambda instance
|
|
31
|
+
*/
|
|
32
|
+
async function handler(event, instance) {
|
|
33
|
+
// We always create a new lambda invocations, for all jobs so that we can accurately
|
|
34
|
+
// identify cold starts.
|
|
35
|
+
const invocation = new lambdaInstance_1.LambdaInvocation(instance);
|
|
36
|
+
invocation.start();
|
|
37
|
+
const driLogger = stdStreamsWrapper_1.deepRequestInspectionLogger(instance);
|
|
38
|
+
// Deployment actions: only for internal usage and is not exposed
|
|
39
|
+
switch (event.action) {
|
|
40
|
+
case 'getEdgeConfig':
|
|
41
|
+
return router.createEdgeConfig();
|
|
42
|
+
case 'getPreloadConfig':
|
|
43
|
+
return withLogging(() => router.createPreloadConfig());
|
|
44
|
+
}
|
|
45
|
+
let req, res, responsePromise;
|
|
46
|
+
try {
|
|
47
|
+
const reqResResult = reqResMapper_1.default(event, invocation, {
|
|
48
|
+
allowReqQueryDuplicates: router.allowReqQueryDuplicates,
|
|
49
|
+
});
|
|
50
|
+
req = reqResResult.req;
|
|
51
|
+
res = reqResResult.res;
|
|
52
|
+
responsePromise = reqResResult.responsePromise;
|
|
53
|
+
}
|
|
54
|
+
catch (e) {
|
|
55
|
+
return withLogging(() => {
|
|
56
|
+
console.log('Failed to parse the event to get request and response data:', e.message, e.stack);
|
|
57
|
+
return {
|
|
58
|
+
body: (Buffer.from('{"error":"Failed to parse the event to get request and response data"}')),
|
|
59
|
+
isBase64Encoded: true,
|
|
60
|
+
statusCode: 530,
|
|
61
|
+
statusMessage: 'Internal Layer0 Error',
|
|
62
|
+
multiValueHeaders: {},
|
|
63
|
+
};
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
// Handle web requests.
|
|
67
|
+
return await withLogging(async () => {
|
|
68
|
+
const headers = req.headers || {};
|
|
69
|
+
driLogger.logDownstreamRequestInfo({
|
|
70
|
+
method: req.method,
|
|
71
|
+
path: req.url,
|
|
72
|
+
host: headers['host'],
|
|
73
|
+
headers: headers,
|
|
74
|
+
// We keep the protocol format same as the internal Node format on protocol fields.
|
|
75
|
+
protocol: `${headers['x-0-protocol'] || 'https'}:`,
|
|
76
|
+
...getBodyLoggingData_1.default(req._rawBodyBase64, req.headers),
|
|
77
|
+
});
|
|
78
|
+
try {
|
|
79
|
+
await ensureAppStarted(3001);
|
|
80
|
+
}
|
|
81
|
+
catch (error) {
|
|
82
|
+
console.error(error);
|
|
83
|
+
throw error;
|
|
84
|
+
}
|
|
85
|
+
timing_1.withTimings(() => router.run(req, res))();
|
|
86
|
+
return responsePromise.then((result) => {
|
|
87
|
+
const headers = res.headers || {};
|
|
88
|
+
driLogger.logDownstreamResponseInfo({
|
|
89
|
+
statusCode: res.statusCode,
|
|
90
|
+
statusMessage: res.statusMessage,
|
|
91
|
+
headers,
|
|
92
|
+
...getBodyLoggingData_1.default(result.body, res.headers),
|
|
93
|
+
});
|
|
94
|
+
return result;
|
|
95
|
+
});
|
|
96
|
+
}, req, instance);
|
|
97
|
+
}
|
|
98
|
+
exports.handler = handler;
|
|
99
|
+
async function startApp(port) {
|
|
100
|
+
const modulePath = path_1.join(process.cwd(), paths_1.pathForBackend(constants_1.BACKENDS.js));
|
|
101
|
+
if (fs_extra_1.existsSync(modulePath)) {
|
|
102
|
+
let prod = nonWebpackRequire_1.default(modulePath);
|
|
103
|
+
if (prod.default) {
|
|
104
|
+
prod = prod.default;
|
|
105
|
+
}
|
|
106
|
+
// load the prod entry point from the framework integration package
|
|
107
|
+
await prod(port);
|
|
108
|
+
// let the router know where to proxy SSR requests
|
|
109
|
+
router.setBackend(constants_1.BACKENDS.js, {
|
|
110
|
+
domainOrIp: constants_1.JS_BACKEND_HOSTNAME,
|
|
111
|
+
port,
|
|
112
|
+
});
|
|
113
|
+
// use default JS backend port when no port is provided
|
|
114
|
+
const imageOptimizerPort = process.env.LAYER0_IMAGE_OPTIMIZER_PORT;
|
|
115
|
+
// check if provided env variable port can be parsed
|
|
116
|
+
if (imageOptimizerPort && !/^\d+$/.test(imageOptimizerPort)) {
|
|
117
|
+
console.error('Error: LAYER0_IMAGE_OPTIMIZER_PORT is not valid number');
|
|
118
|
+
process.exit(1);
|
|
119
|
+
}
|
|
120
|
+
// let the router know where to proxy image optimizer requests
|
|
121
|
+
router.setBackend(constants_1.BACKENDS.imageOptimizer, {
|
|
122
|
+
domainOrIp: process.env.LAYER0_IMAGE_OPTIMIZER_HOST,
|
|
123
|
+
port: imageOptimizerPort ? parseInt(imageOptimizerPort) : undefined,
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Starts the user's SSR app if it hasn't already been started
|
|
129
|
+
*/
|
|
130
|
+
let appStartedPromise;
|
|
131
|
+
async function ensureAppStarted(port) {
|
|
132
|
+
if (!appStartedPromise) {
|
|
133
|
+
appStartedPromise = startApp(port);
|
|
134
|
+
}
|
|
135
|
+
return appStartedPromise;
|
|
136
|
+
}
|
|
137
|
+
// TODO: We need to implement a way to clearly split logs between:
|
|
138
|
+
// 1) user code logs that needs to be JSON, flagged with { awsTag: 'userLogs' }
|
|
139
|
+
// and sent to STDOUT to be picked up by Cloudwatch and our Log Streamer
|
|
140
|
+
// 2) our internal wrapper exception logs that needs to be forwarded and handled
|
|
141
|
+
// by XBP
|
|
142
|
+
//
|
|
143
|
+
// 1) is correctly implemented by stdStreamsWrapper, *but* we need to make sure that user code errors
|
|
144
|
+
// cannot be logged to stdoud past `stdStreamsWrapper.disable()` otherwise they would not appear in Log Streamer
|
|
145
|
+
//
|
|
146
|
+
// 2) https://moovweb.atlassian.net/browse/PC-1766, exception logging and shipping in XBP needs
|
|
147
|
+
// to be reviewed:
|
|
148
|
+
// - some exceptions are escaping (some unhandled promise rejection for example)
|
|
149
|
+
// - XBP is logging all access logs, but should only log exception traces (which should trigger alert)
|
|
150
|
+
/**
|
|
151
|
+
* Starts collecting stdout
|
|
152
|
+
* @param req The web request, if we're serving one
|
|
153
|
+
*/
|
|
154
|
+
function startLogging(req, lambdaInstance) {
|
|
155
|
+
if (process.env.LAYER0_LOCAL !== 'true' || process.env.LAYER0_LOCAL_CONSOLE_WRAP === 'true') {
|
|
156
|
+
// don't enable logging when running in local production mode because it makes debug output harder to read
|
|
157
|
+
stdStreamsWrapper_1.default.enable({
|
|
158
|
+
clientIp: req && req.headers[constants_1.HTTP_HEADERS.x0ClientIp],
|
|
159
|
+
requestId: req && req.headers[constants_1.HTTP_HEADERS.xRequestId],
|
|
160
|
+
wi: lambdaInstance === null || lambdaInstance === void 0 ? void 0 : lambdaInstance.id,
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Stops collecting stdout
|
|
166
|
+
*/
|
|
167
|
+
function stopLogging() {
|
|
168
|
+
stdStreamsWrapper_1.default.disable();
|
|
169
|
+
}
|
|
170
|
+
// this allows to call deploy action with streaming enabled. This is meant for internal functions such as getPreloadConfig
|
|
171
|
+
// that is called after a deployment being successful. We don't need to wrap the edgeConfig call since it will never be available for the user
|
|
172
|
+
// since it runs before the deployment has finished and user has no way to access those logs.
|
|
173
|
+
async function withLogging(action, req, lambdaInstance) {
|
|
174
|
+
try {
|
|
175
|
+
startLogging(req, lambdaInstance);
|
|
176
|
+
return await action();
|
|
177
|
+
}
|
|
178
|
+
finally {
|
|
179
|
+
stopLogging();
|
|
180
|
+
}
|
|
181
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Objects of this class represent a single instantiation of an AWS Lambda function.
|
|
3
|
+
*/
|
|
4
|
+
export default class LambdaInstance {
|
|
5
|
+
private readonly _id;
|
|
6
|
+
private readonly _spawnedAt;
|
|
7
|
+
private _invocationCounter;
|
|
8
|
+
private _lifetimeDuration;
|
|
9
|
+
get id(): string;
|
|
10
|
+
get age(): number;
|
|
11
|
+
get invocationCounter(): number;
|
|
12
|
+
get lifetimeDuration(): number;
|
|
13
|
+
startInvocation(): void;
|
|
14
|
+
stopInvocation(duration: number): void;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Objects of this class represent a single invocation of an AWS Lambda function.
|
|
18
|
+
*/
|
|
19
|
+
export declare class LambdaInvocation {
|
|
20
|
+
private readonly _startTime;
|
|
21
|
+
private _stopTime;
|
|
22
|
+
private readonly _lambda;
|
|
23
|
+
private _isRunning;
|
|
24
|
+
get lambda(): LambdaInstance;
|
|
25
|
+
get duration(): number;
|
|
26
|
+
constructor(lambda: LambdaInstance);
|
|
27
|
+
start(): void;
|
|
28
|
+
stop(): void;
|
|
29
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
const uuid = __importStar(require("uuid"));
|
|
11
|
+
/**
|
|
12
|
+
* Objects of this class represent a single instantiation of an AWS Lambda function.
|
|
13
|
+
*/
|
|
14
|
+
class LambdaInstance {
|
|
15
|
+
constructor() {
|
|
16
|
+
this._id = uuid.v4().toString();
|
|
17
|
+
this._spawnedAt = Date.now();
|
|
18
|
+
this._invocationCounter = 0;
|
|
19
|
+
this._lifetimeDuration = 0;
|
|
20
|
+
}
|
|
21
|
+
get id() {
|
|
22
|
+
return this._id;
|
|
23
|
+
}
|
|
24
|
+
get age() {
|
|
25
|
+
return Date.now() - this._spawnedAt;
|
|
26
|
+
}
|
|
27
|
+
get invocationCounter() {
|
|
28
|
+
return this._invocationCounter;
|
|
29
|
+
}
|
|
30
|
+
get lifetimeDuration() {
|
|
31
|
+
return this._lifetimeDuration;
|
|
32
|
+
}
|
|
33
|
+
startInvocation() {
|
|
34
|
+
++this._invocationCounter;
|
|
35
|
+
}
|
|
36
|
+
stopInvocation(duration) {
|
|
37
|
+
this._lifetimeDuration += duration;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
exports.default = LambdaInstance;
|
|
41
|
+
/**
|
|
42
|
+
* Objects of this class represent a single invocation of an AWS Lambda function.
|
|
43
|
+
*/
|
|
44
|
+
class LambdaInvocation {
|
|
45
|
+
constructor(lambda) {
|
|
46
|
+
this._startTime = Date.now();
|
|
47
|
+
this._isRunning = false;
|
|
48
|
+
this._lambda = lambda;
|
|
49
|
+
this._stopTime = this._startTime;
|
|
50
|
+
}
|
|
51
|
+
get lambda() {
|
|
52
|
+
return this._lambda;
|
|
53
|
+
}
|
|
54
|
+
get duration() {
|
|
55
|
+
return this._stopTime - this._startTime;
|
|
56
|
+
}
|
|
57
|
+
start() {
|
|
58
|
+
if (!this._isRunning) {
|
|
59
|
+
this._isRunning = true;
|
|
60
|
+
this._lambda.startInvocation();
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
stop() {
|
|
64
|
+
if (this._isRunning) {
|
|
65
|
+
this._isRunning = false;
|
|
66
|
+
this._stopTime = Date.now();
|
|
67
|
+
this._lambda.stopInvocation(this.duration);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
exports.LambdaInvocation = LambdaInvocation;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file is modified version of: https://github.com/danielcondemarin/serverless-next.js/blob/master/packages/next-aws-lambda/lib/compatLayer.js
|
|
3
|
+
*
|
|
4
|
+
* Mods:
|
|
5
|
+
* - getHeader were only able to read lower case headers, but it was able to set upper case headers as well with writeHead,
|
|
6
|
+
* so all headers are converted to lowercase now.
|
|
7
|
+
*
|
|
8
|
+
* - Gzippes the response on response end if matches the content-type
|
|
9
|
+
*
|
|
10
|
+
* - Removed variable for base64 support since we always return base64 from the lambdas.
|
|
11
|
+
*/
|
|
12
|
+
import { LambdaInvocation } from './lambdaInstance';
|
|
13
|
+
export interface ReqResMapperOptions {
|
|
14
|
+
/**
|
|
15
|
+
* If `true`, duplicate queries in request will not be transformed to array
|
|
16
|
+
*/
|
|
17
|
+
allowReqQueryDuplicates?: boolean;
|
|
18
|
+
}
|
|
19
|
+
declare const _default: (event: any, invocation: LambdaInvocation, options?: ReqResMapperOptions) => {
|
|
20
|
+
req: any;
|
|
21
|
+
res: any;
|
|
22
|
+
responsePromise: Promise<unknown>;
|
|
23
|
+
};
|
|
24
|
+
export default _default;
|
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* This file is modified version of: https://github.com/danielcondemarin/serverless-next.js/blob/master/packages/next-aws-lambda/lib/compatLayer.js
|
|
4
|
+
*
|
|
5
|
+
* Mods:
|
|
6
|
+
* - getHeader were only able to read lower case headers, but it was able to set upper case headers as well with writeHead,
|
|
7
|
+
* so all headers are converted to lowercase now.
|
|
8
|
+
*
|
|
9
|
+
* - Gzippes the response on response end if matches the content-type
|
|
10
|
+
*
|
|
11
|
+
* - Removed variable for base64 support since we always return base64 from the lambdas.
|
|
12
|
+
*/
|
|
13
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
14
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
const constants_1 = require("../constants");
|
|
18
|
+
const qs_1 = require("qs");
|
|
19
|
+
const uuid_1 = require("uuid");
|
|
20
|
+
const adaptResponseEncodingToDownstream_1 = __importDefault(require("./adaptResponseEncodingToDownstream"));
|
|
21
|
+
const stream_1 = __importDefault(require("stream"));
|
|
22
|
+
const environment_1 = require("../environment");
|
|
23
|
+
/**
|
|
24
|
+
* Gets the version of @layer0/core. For this to work in testing an entry needed
|
|
25
|
+
* to be added to moduleNameMapper in the jest config specifically for package.json
|
|
26
|
+
*
|
|
27
|
+
* Notes:
|
|
28
|
+
* - In this module there are multiple places that need to read Layer0 version.
|
|
29
|
+
* But elegantly and correctly offering a single function to read Layer0 version is hard
|
|
30
|
+
* because these different places are transpiled, bundled and invoked in different
|
|
31
|
+
* contexts. For example, if we try to replace this with reading version from
|
|
32
|
+
* DeploymentBuilder then bundling fails.
|
|
33
|
+
*
|
|
34
|
+
* @return {String}
|
|
35
|
+
*/
|
|
36
|
+
function getLayer0Version() {
|
|
37
|
+
return process.env[constants_1.LAYER0_ENV_VARIABLES.versionOverride] || require('../../package.json').version;
|
|
38
|
+
}
|
|
39
|
+
exports.default = (event, invocation, options = {
|
|
40
|
+
allowReqQueryDuplicates: false,
|
|
41
|
+
}) => {
|
|
42
|
+
const response = {
|
|
43
|
+
body: Buffer.from(''),
|
|
44
|
+
isBase64Encoded: true,
|
|
45
|
+
statusCode: 200,
|
|
46
|
+
statusMessage: 'OK',
|
|
47
|
+
multiValueHeaders: {},
|
|
48
|
+
};
|
|
49
|
+
let responsePromise;
|
|
50
|
+
const req = new stream_1.default.Readable();
|
|
51
|
+
// we provide url, path, and query (decoded), to match how express works so that development and production environment behave the same
|
|
52
|
+
req.url = event.path;
|
|
53
|
+
req.path = event.path;
|
|
54
|
+
req.query = {};
|
|
55
|
+
req.method = event.httpMethod;
|
|
56
|
+
req.rawHeaders = [];
|
|
57
|
+
req.headers = {};
|
|
58
|
+
if (event.multiValueQueryStringParameters &&
|
|
59
|
+
Object.keys(event.multiValueQueryStringParameters).length) {
|
|
60
|
+
Object.keys(event.multiValueQueryStringParameters).forEach(key => {
|
|
61
|
+
// keep backwards compatibility so that arrays with a single value are
|
|
62
|
+
// converted to just be that value:
|
|
63
|
+
const curVal = event.multiValueQueryStringParameters[key];
|
|
64
|
+
if (Array.isArray(curVal) && curVal.length === 1) {
|
|
65
|
+
event.multiValueQueryStringParameters[key] = curVal[0];
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
const qs = qs_1.stringify(event.multiValueQueryStringParameters, {
|
|
69
|
+
indices: !options.allowReqQueryDuplicates,
|
|
70
|
+
});
|
|
71
|
+
req.url += `?${qs}`;
|
|
72
|
+
req.query = qs_1.parse(qs); // we stringify and reparse to ensure that query parameters with a single value are not provided as an array
|
|
73
|
+
}
|
|
74
|
+
const headers = event.multiValueHeaders || {};
|
|
75
|
+
for (const key of Object.keys(headers)) {
|
|
76
|
+
// Compatibility with https://nodejs.org/api/all.html#http_message_rawheaders
|
|
77
|
+
// Header names are not lower-cased to maintain Node compatibility.
|
|
78
|
+
for (const value of headers[key]) {
|
|
79
|
+
req.rawHeaders.push(key);
|
|
80
|
+
req.rawHeaders.push(value);
|
|
81
|
+
}
|
|
82
|
+
// Compatibility with https://nodejs.org/api/all.html#http_message_headers
|
|
83
|
+
// Header names *are* lower-cased to maintain Node compatibility.
|
|
84
|
+
req.headers[key.toLowerCase()] = headers[key].toString();
|
|
85
|
+
}
|
|
86
|
+
req.getHeader = (name) => req.headers[name.toLowerCase()];
|
|
87
|
+
req.getHeaders = () => req.headers;
|
|
88
|
+
req.setHeader = (name, value) => {
|
|
89
|
+
req.headers[name.toLowerCase()] = value;
|
|
90
|
+
};
|
|
91
|
+
req.removeHeader = (name) => {
|
|
92
|
+
delete req.headers[name.toLowerCase()];
|
|
93
|
+
};
|
|
94
|
+
req.connection = {};
|
|
95
|
+
// Capture the downstream `accept-encoding` because compute can modify the `req` object
|
|
96
|
+
// and we need the accept encoding post-user code to compress the response.
|
|
97
|
+
const downstreamAcceptEncoding = req.getHeader(constants_1.HTTP_HEADERS.acceptEncoding);
|
|
98
|
+
// req.secure is ExpressJS extension, set to false only when the app is running locally or the protocol is HTTP
|
|
99
|
+
// (so the default remains HTTPS which has historically been the only protocol we allowed)
|
|
100
|
+
req.secure = !environment_1.isLocal() && req.getHeader(constants_1.HTTP_HEADERS.x0Protocol) !== 'http';
|
|
101
|
+
// Depending on the version req.socket.encrypted is set to true by NodeJS when the protocol is HTTPS.
|
|
102
|
+
req.socket = {
|
|
103
|
+
encrypted: req.secure,
|
|
104
|
+
};
|
|
105
|
+
// Depending on the version req.connection.encrypted is set to true by NodeJS when the protocol is HTTPS.
|
|
106
|
+
req.connection = {
|
|
107
|
+
encrypted: req.secure,
|
|
108
|
+
};
|
|
109
|
+
req.protocol = req.secure ? 'https' : 'http';
|
|
110
|
+
// If the downstream has sent us request ID we use it, otherwise we generate a new one.
|
|
111
|
+
// We have to repeat this code here *and* in Router *and* in XBP *and* in edge because
|
|
112
|
+
// each one of these components may be the first to receive the request:
|
|
113
|
+
// - Router when using `layer0 run`
|
|
114
|
+
// - Handler when using `layer0 run --production`
|
|
115
|
+
// - XBP when making request to cloud
|
|
116
|
+
// - Edge when making request to edge
|
|
117
|
+
let newRequestId;
|
|
118
|
+
if (!req.headers[constants_1.HTTP_HEADERS.xRequestId]) {
|
|
119
|
+
newRequestId = uuid_1.v4();
|
|
120
|
+
req.headers[constants_1.HTTP_HEADERS.xRequestId] = newRequestId;
|
|
121
|
+
}
|
|
122
|
+
const res = new stream_1.default();
|
|
123
|
+
Object.defineProperty(res, 'statusCode', {
|
|
124
|
+
get() {
|
|
125
|
+
return response.statusCode;
|
|
126
|
+
},
|
|
127
|
+
set(statusCode) {
|
|
128
|
+
response.statusCode = statusCode;
|
|
129
|
+
},
|
|
130
|
+
});
|
|
131
|
+
Object.defineProperty(res, 'statusMessage', {
|
|
132
|
+
get() {
|
|
133
|
+
return response.statusMessage;
|
|
134
|
+
},
|
|
135
|
+
set(statusMessage) {
|
|
136
|
+
response.statusMessage = statusMessage;
|
|
137
|
+
},
|
|
138
|
+
});
|
|
139
|
+
res.headers = {};
|
|
140
|
+
res.writeHead = (status, statusMessage, headers) => {
|
|
141
|
+
response.statusCode = status;
|
|
142
|
+
if (typeof statusMessage === 'string') {
|
|
143
|
+
response.statusMessage = statusMessage;
|
|
144
|
+
}
|
|
145
|
+
else {
|
|
146
|
+
if (typeof statusMessage === 'object') {
|
|
147
|
+
headers = statusMessage;
|
|
148
|
+
statusMessage = undefined;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
if (typeof headers === 'object') {
|
|
152
|
+
Object.entries(headers).forEach(([name, value]) => {
|
|
153
|
+
res.setHeader(name, value);
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
// If this component has generated the new request ID then
|
|
157
|
+
// it is also responsible for sending it downstream.
|
|
158
|
+
if (newRequestId) {
|
|
159
|
+
res.setHeader(constants_1.HTTP_HEADERS.xRequestId, newRequestId);
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
res.write = (chunk) => {
|
|
163
|
+
response.body = Buffer.concat([
|
|
164
|
+
response.body,
|
|
165
|
+
Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk),
|
|
166
|
+
]);
|
|
167
|
+
};
|
|
168
|
+
res.setHeader = (name, value) => {
|
|
169
|
+
res.headers[name.toLowerCase()] = value;
|
|
170
|
+
};
|
|
171
|
+
res.removeHeader = (name) => {
|
|
172
|
+
delete res.headers[name.toLowerCase()];
|
|
173
|
+
};
|
|
174
|
+
res.getHeader = (name) => {
|
|
175
|
+
return res.headers[name.toLowerCase()];
|
|
176
|
+
};
|
|
177
|
+
res.getHeaders = () => {
|
|
178
|
+
return res.headers;
|
|
179
|
+
};
|
|
180
|
+
const onResEnd = (resolve) => (text) => {
|
|
181
|
+
if (text) {
|
|
182
|
+
res.write(text);
|
|
183
|
+
}
|
|
184
|
+
// Compress the response if needed.
|
|
185
|
+
adaptResponseEncodingToDownstream_1.default(downstreamAcceptEncoding, res, response);
|
|
186
|
+
// We always respond with base64 so that we can serve non-text assets like images.
|
|
187
|
+
response.body = response.body.toString('base64');
|
|
188
|
+
response.multiValueHeaders = res.headers;
|
|
189
|
+
invocation.stop();
|
|
190
|
+
logStatusCode(response);
|
|
191
|
+
logExecutionTime(response, invocation);
|
|
192
|
+
logComponentVersion(response, getLayer0Version(), invocation.lambda.id);
|
|
193
|
+
fixApiGatewayMultipleHeaders();
|
|
194
|
+
resolve(response);
|
|
195
|
+
};
|
|
196
|
+
responsePromise = new Promise(resolve => {
|
|
197
|
+
res.end = onResEnd(resolve);
|
|
198
|
+
});
|
|
199
|
+
if (event.body) {
|
|
200
|
+
// We allow two ways to read the body. One is through stream events and
|
|
201
|
+
// the other is directly on the request object. The 2nd one is used
|
|
202
|
+
// internally by logging. This does not consume additional memory as
|
|
203
|
+
// event.body is alive during the whole lambda lifetime anyways.
|
|
204
|
+
req.push(event.body, event.isBase64Encoded ? 'base64' : undefined);
|
|
205
|
+
req._rawBodyBase64 = event.body;
|
|
206
|
+
}
|
|
207
|
+
req.push(null);
|
|
208
|
+
function fixApiGatewayMultipleHeaders() {
|
|
209
|
+
for (const key of Object.keys(response.multiValueHeaders)) {
|
|
210
|
+
if (!Array.isArray(response.multiValueHeaders[key])) {
|
|
211
|
+
response.multiValueHeaders[key] = [response.multiValueHeaders[key]];
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
return { req, res, responsePromise };
|
|
216
|
+
};
|
|
217
|
+
/**
|
|
218
|
+
* Adds an `x-0-status` header to the response based on the status code.
|
|
219
|
+
*
|
|
220
|
+
* Notes:
|
|
221
|
+
* - If the `x-0-status` already exists then the status is prefixed to upstream value.
|
|
222
|
+
*
|
|
223
|
+
* @param response The response event
|
|
224
|
+
* @private
|
|
225
|
+
*/
|
|
226
|
+
function logStatusCode(response) {
|
|
227
|
+
prefixResponseHeader(response, constants_1.HTTP_HEADERS.x0Status, `w=${response.statusCode}`);
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* Adds an `x-0-t` header to the response based on the current time.
|
|
231
|
+
*
|
|
232
|
+
* Notes:
|
|
233
|
+
* - If the `x-0-t` already exists then the execution time is prefixed to upstream value.
|
|
234
|
+
* - `wt` stands for "worker time", the duration of the current execution
|
|
235
|
+
* - `wc` stands for "worker count", the number of times the lambda has been invoked,
|
|
236
|
+
* with the first invoication having the count of *1* (same as `pc` counter in XBP)
|
|
237
|
+
* - `wg` stands for "worker age", the number of milliseconds since the lambda instances was spawned
|
|
238
|
+
* (`wa` already stands for request transformation time)
|
|
239
|
+
* - `wl` stands for "worker lifetime", the total lifetime duration
|
|
240
|
+
*
|
|
241
|
+
* @param response The response event
|
|
242
|
+
* @param startTime The start time of the request
|
|
243
|
+
* @private
|
|
244
|
+
*/
|
|
245
|
+
function logExecutionTime(response, invocation) {
|
|
246
|
+
const lambda = invocation.lambda;
|
|
247
|
+
prefixResponseHeader(response, constants_1.HTTP_HEADERS.x0T, `wt=${invocation.duration},wc=${lambda.invocationCounter},wg=${lambda.age},wl=${lambda.lifetimeDuration}`);
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* Adds an `x-0-components` header to the response based on the current time.
|
|
251
|
+
*
|
|
252
|
+
* Notes:
|
|
253
|
+
* - If the `x-0-components` already exists then the version is prefixed to upstream value.
|
|
254
|
+
* - `w` stands for "worker" and has the Layer0 version of the worker
|
|
255
|
+
* - `wi` stands for "worker id"
|
|
256
|
+
*
|
|
257
|
+
* @param response The response event
|
|
258
|
+
* @param version The Layer0 version of the package
|
|
259
|
+
* @private
|
|
260
|
+
*/
|
|
261
|
+
function logComponentVersion(response, version, lambdaId) {
|
|
262
|
+
prefixResponseHeader(response, constants_1.HTTP_HEADERS.x0Components, `w=${version},wi=${lambdaId}`);
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* Sets the response header to a new value by prefixing it with the given value or,
|
|
266
|
+
* if it doesn't exist, by setting it to the given value,
|
|
267
|
+
*
|
|
268
|
+
* @param response The response event
|
|
269
|
+
* @param headerName The name of the header to prefix or set
|
|
270
|
+
* @param prefixValue The value of the prefix without trailing comma
|
|
271
|
+
* @private
|
|
272
|
+
*/
|
|
273
|
+
function prefixResponseHeader(response, headerName, prefixValue) {
|
|
274
|
+
const headerValue = response.multiValueHeaders[headerName];
|
|
275
|
+
if (headerValue) {
|
|
276
|
+
if (Array.isArray(headerValue)) {
|
|
277
|
+
headerValue[0] = `${prefixValue},${headerValue[0]}`;
|
|
278
|
+
}
|
|
279
|
+
else {
|
|
280
|
+
response.multiValueHeaders[headerName] = `${prefixValue},${headerValue}`;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
else {
|
|
284
|
+
response.multiValueHeaders[headerName] = `${prefixValue}`;
|
|
285
|
+
}
|
|
286
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import LambdaInstance from './lambdaInstance';
|
|
2
|
+
export declare function deepRequestInspectionLogger(instance: LambdaInstance): {
|
|
3
|
+
logDownstreamRequestInfo: (data: any) => void;
|
|
4
|
+
logDownstreamResponseInfo: (data: any) => void;
|
|
5
|
+
logUpstreamRequestInfo: (data: any) => void;
|
|
6
|
+
logUpstreamResponseInfo: (data: any) => void;
|
|
7
|
+
logUpstreamResponseBodyInfo: (data: any) => void;
|
|
8
|
+
};
|
|
9
|
+
declare const stdStreamsWrapper: {
|
|
10
|
+
/**
|
|
11
|
+
* Wraps console.xxx methods AND standard streams into a JSON payload that will
|
|
12
|
+
* be sent back to the final user in the log streaming interface.
|
|
13
|
+
*
|
|
14
|
+
* Notes:
|
|
15
|
+
* - We have to wrap standard streams because some logging libraries (like 'consola')
|
|
16
|
+
* writes to those directly without going through console.xxx
|
|
17
|
+
* - We have to wrap console.xxx methods too, as they don't seem to be bound to
|
|
18
|
+
* standard streams in AWS Lambda Environment. That wrapper (using pino) streams
|
|
19
|
+
* to process.stdout, leading to the point below.
|
|
20
|
+
* - Both notes above would result in a double json encapsulation when logging via
|
|
21
|
+
* console.xxx methods. We prevent that in our standard stream wrapper by detecting
|
|
22
|
+
* a json string that coming from pino logger.
|
|
23
|
+
*/
|
|
24
|
+
enable: ({ clientIp, requestId, wi, }?: {
|
|
25
|
+
clientIp?: string | undefined;
|
|
26
|
+
requestId?: string | undefined;
|
|
27
|
+
wi?: string | undefined;
|
|
28
|
+
}) => void;
|
|
29
|
+
disable: () => void;
|
|
30
|
+
};
|
|
31
|
+
export default stdStreamsWrapper;
|