@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,58 @@
|
|
|
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 cacheManifestFromRouter_1 = __importDefault(require("./cacheManifestFromRouter"));
|
|
7
|
+
const environment_1 = require("../environment");
|
|
8
|
+
function addBuiltInRoutes(router) {
|
|
9
|
+
addCacheManifest(router);
|
|
10
|
+
try {
|
|
11
|
+
// Add @layer0/devtools built-in routes if the module is installed
|
|
12
|
+
require('@layer0/devtools/addBuiltInRoutes')({
|
|
13
|
+
router,
|
|
14
|
+
local: environment_1.isLocal(),
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
catch (e) {
|
|
18
|
+
// istanbul ignore if
|
|
19
|
+
if (e.code !== 'MODULE_NOT_FOUND') {
|
|
20
|
+
throw e;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
exports.default = addBuiltInRoutes;
|
|
25
|
+
/**
|
|
26
|
+
* Adds /__layer0__/cache-manifest.js, which contains:
|
|
27
|
+
*
|
|
28
|
+
* ```
|
|
29
|
+
* self.__LAYER0_CACHE_MANIFEST__ = {
|
|
30
|
+
* "/some/route": {
|
|
31
|
+
* browser: {
|
|
32
|
+
* serviceWorkerSeconds: (number)
|
|
33
|
+
* },
|
|
34
|
+
* edge: {
|
|
35
|
+
* maxAgeSeconds: (number)
|
|
36
|
+
* }
|
|
37
|
+
* }
|
|
38
|
+
* }
|
|
39
|
+
* ```
|
|
40
|
+
*
|
|
41
|
+
* @private
|
|
42
|
+
* @param router
|
|
43
|
+
*/
|
|
44
|
+
function addCacheManifest(router) {
|
|
45
|
+
router.match('/__layer0__/cache-manifest.js', ({ cache, setResponseHeader, send, allowCors }) => {
|
|
46
|
+
cache({
|
|
47
|
+
edge: {
|
|
48
|
+
maxAgeSeconds: 60 * 60 * 24 * 365,
|
|
49
|
+
},
|
|
50
|
+
browser: {
|
|
51
|
+
maxAgeSeconds: 60 * 60,
|
|
52
|
+
},
|
|
53
|
+
});
|
|
54
|
+
setResponseHeader('content-type', 'text/javascript');
|
|
55
|
+
allowCors();
|
|
56
|
+
send(() => `self.__LAYER0_CACHE_MANIFEST__=${cacheManifestFromRouter_1.default(router).toJSON()}`);
|
|
57
|
+
});
|
|
58
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import Response from './Response';
|
|
2
|
+
/**
|
|
3
|
+
* Adds preload headers for all `<link rel="preload">` tags on the response.body
|
|
4
|
+
* @private
|
|
5
|
+
* @param res The response to which headers should be added.
|
|
6
|
+
*/
|
|
7
|
+
export default function addPreloadHeaders(res: Response): void;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
/**
|
|
4
|
+
* Adds preload headers for all `<link rel="preload">` tags on the response.body
|
|
5
|
+
* @private
|
|
6
|
+
* @param res The response to which headers should be added.
|
|
7
|
+
*/
|
|
8
|
+
function addPreloadHeaders(res) {
|
|
9
|
+
if (/text\/html/i.test(res.getHeader('Content-Type'))) {
|
|
10
|
+
let { body } = res;
|
|
11
|
+
const matches = [];
|
|
12
|
+
const preloadRegex = /<link rel="preload" href="([^"]*)".+?(?=>)/gis;
|
|
13
|
+
const typeRegex = /as="([^"]*)"/i;
|
|
14
|
+
let match;
|
|
15
|
+
if (Buffer.isBuffer(body)) {
|
|
16
|
+
body = body.toString('utf8');
|
|
17
|
+
}
|
|
18
|
+
if (typeof body === 'string') {
|
|
19
|
+
while ((match = preloadRegex.exec(body))) {
|
|
20
|
+
// determine possible type if defined on the tag
|
|
21
|
+
const type = typeRegex.exec(match[0]);
|
|
22
|
+
matches.push({ uri: match[1], as: type && type[1] });
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
const newLinks = matches.map(linkFile).join(', ');
|
|
26
|
+
// Add to existing links if exist
|
|
27
|
+
const existingLinks = res.getHeader('link');
|
|
28
|
+
if (existingLinks) {
|
|
29
|
+
res.setHeader('link', `${existingLinks}, ${newLinks}`);
|
|
30
|
+
}
|
|
31
|
+
else if (newLinks.length) {
|
|
32
|
+
res.setHeader('link', newLinks);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
exports.default = addPreloadHeaders;
|
|
37
|
+
/**
|
|
38
|
+
* Creates a preload header value for the specified file
|
|
39
|
+
* @param link The request uri and possible type
|
|
40
|
+
* @private
|
|
41
|
+
*/
|
|
42
|
+
function linkFile(link) {
|
|
43
|
+
let { uri, as } = link;
|
|
44
|
+
let name = uri.split('?')[0];
|
|
45
|
+
if (!as) {
|
|
46
|
+
if (name.toLowerCase().endsWith('.css')) {
|
|
47
|
+
as = 'style';
|
|
48
|
+
}
|
|
49
|
+
else if (name.toLowerCase().match(/(woff|woff2|truetype|opentype|embedded-opentype)$/)) {
|
|
50
|
+
as = 'font';
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
as = 'script';
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return `<${name}>; as=${as}; rel=preload`;
|
|
57
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
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 BaseResponseWriter_1 = require("./BaseResponseWriter");
|
|
7
|
+
const CacheManifest_1 = __importDefault(require("./CacheManifest"));
|
|
8
|
+
const mapValues_1 = __importDefault(require("../utils/mapValues"));
|
|
9
|
+
/**
|
|
10
|
+
* Generates a new `CacheManifest` object from the rules given in the provided `Router`
|
|
11
|
+
*/
|
|
12
|
+
function cacheManifestFromRouter(router) {
|
|
13
|
+
const manifest = new CacheManifest_1.default();
|
|
14
|
+
const propToString = (regexOrString) => {
|
|
15
|
+
if (regexOrString === undefined)
|
|
16
|
+
return undefined;
|
|
17
|
+
return regexOrString instanceof RegExp ? regexOrString.source : regexOrString;
|
|
18
|
+
};
|
|
19
|
+
for (let route of router.routeGroups.getRoutes()) {
|
|
20
|
+
const writer = new StubWriter();
|
|
21
|
+
// @ts-ignore Typescript doesn't like that we're passing in the base class here -
|
|
22
|
+
// but we keep ResponseWriter on the handler method for documentation purposes
|
|
23
|
+
route.handler(writer);
|
|
24
|
+
const entry = {
|
|
25
|
+
criteriaPath: route.criteria.path,
|
|
26
|
+
route: route.toRegexString(),
|
|
27
|
+
method: propToString(route.criteria.method),
|
|
28
|
+
protocol: propToString(route.criteria.protocol),
|
|
29
|
+
cacheOptions: writer.cacheOptions,
|
|
30
|
+
returnsResponse: writer.returnsResponse,
|
|
31
|
+
};
|
|
32
|
+
if (route.criteria.query) {
|
|
33
|
+
// @ts-ignore The lodash response is a Dictionary which confuses TS
|
|
34
|
+
entry.query = mapValues_1.default(route.criteria.query, (value) => value ? value.source : null);
|
|
35
|
+
}
|
|
36
|
+
// Because service worker cannot match on headers and cookies we ignore them
|
|
37
|
+
// during the creation of cache manifest. This is problematic but it's
|
|
38
|
+
// less problematic than pretending to not ignore them and creating
|
|
39
|
+
// routes that are too "greedy" (they can never match on headers/cookies
|
|
40
|
+
// so overall the condition is always weaker and thus matches more than
|
|
41
|
+
// it should)
|
|
42
|
+
//
|
|
43
|
+
// An example of the issues this causes arises when users try to block some
|
|
44
|
+
// traffic in the router (e.g. from certain countries) The blocking is
|
|
45
|
+
// based on the geo location headers that are injected into the request
|
|
46
|
+
// by the edge so aren't even theoretically available for evaluation
|
|
47
|
+
// in the browser. This leads to service worker matching *all* requests
|
|
48
|
+
// to something that has no caching options (it only has the blocking code)
|
|
49
|
+
// leading to service worker rejecting to cache anything, contrary to what
|
|
50
|
+
// the rest of the router states.
|
|
51
|
+
if (route.criteria.headers || route.criteria.cookies) {
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
manifest.entries.push(entry);
|
|
55
|
+
}
|
|
56
|
+
return manifest;
|
|
57
|
+
}
|
|
58
|
+
exports.default = cacheManifestFromRouter;
|
|
59
|
+
/**
|
|
60
|
+
* Helper class used to generate manifest entries from a Router
|
|
61
|
+
* @private
|
|
62
|
+
*/
|
|
63
|
+
class StubWriter extends BaseResponseWriter_1.BaseResponseWriter {
|
|
64
|
+
constructor() {
|
|
65
|
+
super(...arguments);
|
|
66
|
+
this.response = {};
|
|
67
|
+
this.request = {};
|
|
68
|
+
this.cacheOptions = {};
|
|
69
|
+
this.returnsResponse = false;
|
|
70
|
+
this.doesReturnResponse = () => (this.returnsResponse = true);
|
|
71
|
+
this.cache = (options) => {
|
|
72
|
+
this.cacheOptions = options;
|
|
73
|
+
};
|
|
74
|
+
this.send = this.doesReturnResponse;
|
|
75
|
+
this.compute = this.doesReturnResponse;
|
|
76
|
+
this.proxy = this.doesReturnResponse;
|
|
77
|
+
this.redirect = this.doesReturnResponse;
|
|
78
|
+
this.render = this.doesReturnResponse;
|
|
79
|
+
this.serveStatic = this.doesReturnResponse;
|
|
80
|
+
this.serviceWorker = this.doesReturnResponse;
|
|
81
|
+
this.renderWithApp = this.doesReturnResponse;
|
|
82
|
+
this.appShell = this.doesReturnResponse;
|
|
83
|
+
this.stream = this.doesReturnResponse;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import CustomCacheKey from './CustomCacheKey';
|
|
2
|
+
/**
|
|
3
|
+
* Returns a DSL for creating custom server cache keys based on cookies, query parameters, and
|
|
4
|
+
* request headers.
|
|
5
|
+
*
|
|
6
|
+
* example:
|
|
7
|
+
*
|
|
8
|
+
* ```js
|
|
9
|
+
* new Router()
|
|
10
|
+
* .get('/s/:id',
|
|
11
|
+
* cache({
|
|
12
|
+
* edge: {
|
|
13
|
+
* key: createCustomCacheKey()
|
|
14
|
+
* .addHeader('user-agent')
|
|
15
|
+
* .excludeQueryParameters(['uid'])
|
|
16
|
+
* .addCookie('location', cookie => {
|
|
17
|
+
* cookie.group('na').byPattern('us|ca')
|
|
18
|
+
* cookie.group('eur').byPattern('de|fr|ee')
|
|
19
|
+
* })
|
|
20
|
+
* }
|
|
21
|
+
* })
|
|
22
|
+
* )
|
|
23
|
+
* ```
|
|
24
|
+
* @deprecated Use `new CustomCacheKey()` instead.
|
|
25
|
+
* @return {CustomCacheKey}
|
|
26
|
+
*/
|
|
27
|
+
export default function createCustomCacheKey(): CustomCacheKey;
|
|
@@ -0,0 +1,35 @@
|
|
|
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 CustomCacheKey_1 = __importDefault(require("./CustomCacheKey"));
|
|
7
|
+
/**
|
|
8
|
+
* Returns a DSL for creating custom server cache keys based on cookies, query parameters, and
|
|
9
|
+
* request headers.
|
|
10
|
+
*
|
|
11
|
+
* example:
|
|
12
|
+
*
|
|
13
|
+
* ```js
|
|
14
|
+
* new Router()
|
|
15
|
+
* .get('/s/:id',
|
|
16
|
+
* cache({
|
|
17
|
+
* edge: {
|
|
18
|
+
* key: createCustomCacheKey()
|
|
19
|
+
* .addHeader('user-agent')
|
|
20
|
+
* .excludeQueryParameters(['uid'])
|
|
21
|
+
* .addCookie('location', cookie => {
|
|
22
|
+
* cookie.group('na').byPattern('us|ca')
|
|
23
|
+
* cookie.group('eur').byPattern('de|fr|ee')
|
|
24
|
+
* })
|
|
25
|
+
* }
|
|
26
|
+
* })
|
|
27
|
+
* )
|
|
28
|
+
* ```
|
|
29
|
+
* @deprecated Use `new CustomCacheKey()` instead.
|
|
30
|
+
* @return {CustomCacheKey}
|
|
31
|
+
*/
|
|
32
|
+
function createCustomCacheKey() {
|
|
33
|
+
return new CustomCacheKey_1.default();
|
|
34
|
+
}
|
|
35
|
+
exports.default = createCustomCacheKey;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import Response from './Response';
|
|
2
|
+
export interface DeriveSurrogateKeyOptions {
|
|
3
|
+
/**
|
|
4
|
+
* Determines the behavior when the origin returns a `x-0-surrogate-key` response header
|
|
5
|
+
* - Use "merge" to include surrogate keys returned from the origin via the `x-0-surrogate-key` response header.
|
|
6
|
+
* - Use "override" to discard surrogate keys returned from the origin via the `x-0-surrogate-key` response header.
|
|
7
|
+
*/
|
|
8
|
+
onConflict: 'merge' | 'override';
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Creates a response transformer that assigns surrogate keys for the given body parsed as JSON. Accepts a
|
|
12
|
+
* callback function that takes the response body parsed as json and returns an array of surrogate
|
|
13
|
+
* keys to assign the response when cached. This function will only be called on cache misses. When a request
|
|
14
|
+
* has no body or a body that cannot be parsed as JSON, the callback function will be passed `undefined`.
|
|
15
|
+
* Returning anything other than an array of string will result in no surrogate keys being assigned and a warning
|
|
16
|
+
* will be logged.
|
|
17
|
+
*
|
|
18
|
+
* Example:
|
|
19
|
+
*
|
|
20
|
+
* ```
|
|
21
|
+
* new Router()
|
|
22
|
+
* .graphqlOperation('GetPost', ({ cache }) => {
|
|
23
|
+
* cache({
|
|
24
|
+
* edge: {
|
|
25
|
+
* maxAgeSeconds: 60 * 60
|
|
26
|
+
* }
|
|
27
|
+
* })
|
|
28
|
+
* proxy('graphql', {
|
|
29
|
+
* transformResponse: deriveSurrogateKeysFromJson(json => [`posts.${json.id}`])
|
|
30
|
+
* })
|
|
31
|
+
* })
|
|
32
|
+
* ```
|
|
33
|
+
* @param extractKeys
|
|
34
|
+
* @returns
|
|
35
|
+
*/
|
|
36
|
+
export default function deriveSurrogateKeysFromJson(extractKeys: (bodyFromJson: any | undefined) => string[], options?: DeriveSurrogateKeyOptions): (res: Response) => Promise<void>;
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const __1 = require("..");
|
|
4
|
+
/**
|
|
5
|
+
* Creates a response transformer that assigns surrogate keys for the given body parsed as JSON. Accepts a
|
|
6
|
+
* callback function that takes the response body parsed as json and returns an array of surrogate
|
|
7
|
+
* keys to assign the response when cached. This function will only be called on cache misses. When a request
|
|
8
|
+
* has no body or a body that cannot be parsed as JSON, the callback function will be passed `undefined`.
|
|
9
|
+
* Returning anything other than an array of string will result in no surrogate keys being assigned and a warning
|
|
10
|
+
* will be logged.
|
|
11
|
+
*
|
|
12
|
+
* Example:
|
|
13
|
+
*
|
|
14
|
+
* ```
|
|
15
|
+
* new Router()
|
|
16
|
+
* .graphqlOperation('GetPost', ({ cache }) => {
|
|
17
|
+
* cache({
|
|
18
|
+
* edge: {
|
|
19
|
+
* maxAgeSeconds: 60 * 60
|
|
20
|
+
* }
|
|
21
|
+
* })
|
|
22
|
+
* proxy('graphql', {
|
|
23
|
+
* transformResponse: deriveSurrogateKeysFromJson(json => [`posts.${json.id}`])
|
|
24
|
+
* })
|
|
25
|
+
* })
|
|
26
|
+
* ```
|
|
27
|
+
* @param extractKeys
|
|
28
|
+
* @returns
|
|
29
|
+
*/
|
|
30
|
+
function deriveSurrogateKeysFromJson(extractKeys, options = { onConflict: 'merge' }) {
|
|
31
|
+
return async (res) => {
|
|
32
|
+
const result = extractKeys(parseBodyAsJson(res.body));
|
|
33
|
+
if (!Array.isArray(result)) {
|
|
34
|
+
console.warn(`The function provided to deriveSurrogateKeysFromJson should return an array, but returned a value of type "${typeof result}" instead. Surrogate keys will not be set.`);
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
const keys = new Set(result);
|
|
38
|
+
if (options.onConflict === 'merge') {
|
|
39
|
+
let surrogateKeyFromOrigin = res.getHeader(__1.HTTP_HEADERS.x0SurrogateKey);
|
|
40
|
+
if (surrogateKeyFromOrigin != null) {
|
|
41
|
+
if (!Array.isArray(surrogateKeyFromOrigin)) {
|
|
42
|
+
surrogateKeyFromOrigin = [surrogateKeyFromOrigin];
|
|
43
|
+
}
|
|
44
|
+
for (let header of surrogateKeyFromOrigin) {
|
|
45
|
+
for (let key of header.split(' ')) {
|
|
46
|
+
keys.add(key);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
else if (options.onConflict !== 'override') {
|
|
52
|
+
throw new Error(`invalid value "${options.onConflict}" for onConflict option in deriveSurrogateKeysFromJson. Use "override" or "merge".`);
|
|
53
|
+
}
|
|
54
|
+
if (keys.size > 0) {
|
|
55
|
+
res.setHeader(__1.HTTP_HEADERS.x0SurrogateKey, Array.from(keys).join(' '));
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
exports.default = deriveSurrogateKeysFromJson;
|
|
60
|
+
function parseBodyAsJson(body) {
|
|
61
|
+
if (!body) {
|
|
62
|
+
return undefined;
|
|
63
|
+
}
|
|
64
|
+
try {
|
|
65
|
+
return JSON.parse(typeof body === 'string' ? body : body.toString('utf8'));
|
|
66
|
+
}
|
|
67
|
+
catch (e) {
|
|
68
|
+
return undefined;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export { default as Router } from './Router';
|
|
2
|
+
export { RouteHandler } from './Router';
|
|
3
|
+
export { default as CustomCacheKey } from './CustomCacheKey';
|
|
4
|
+
export { default as ResponseWriter } from './ResponseWriter';
|
|
5
|
+
export { default as deriveSurrogateKeysFromJson } from './deriveSurrogateKeysFromJson';
|
|
6
|
+
/**
|
|
7
|
+
* We've deprecated this to simplify the API. The examples in the docs now use `new CustomCacheKey()` instead.
|
|
8
|
+
* @private
|
|
9
|
+
* @deprecated Use `new CustomCacheKey()` instead.
|
|
10
|
+
*/
|
|
11
|
+
export { default as createCustomCacheKey } from './createCustomCacheKey';
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
var Router_1 = require("./Router");
|
|
4
|
+
exports.Router = Router_1.default;
|
|
5
|
+
var CustomCacheKey_1 = require("./CustomCacheKey");
|
|
6
|
+
exports.CustomCacheKey = CustomCacheKey_1.default;
|
|
7
|
+
var ResponseWriter_1 = require("./ResponseWriter");
|
|
8
|
+
exports.ResponseWriter = ResponseWriter_1.default;
|
|
9
|
+
var deriveSurrogateKeysFromJson_1 = require("./deriveSurrogateKeysFromJson");
|
|
10
|
+
exports.deriveSurrogateKeysFromJson = deriveSurrogateKeysFromJson_1.default;
|
|
11
|
+
/**
|
|
12
|
+
* We've deprecated this to simplify the API. The examples in the docs now use `new CustomCacheKey()` instead.
|
|
13
|
+
* @private
|
|
14
|
+
* @deprecated Use `new CustomCacheKey()` instead.
|
|
15
|
+
*/
|
|
16
|
+
var createCustomCacheKey_1 = require("./createCustomCacheKey");
|
|
17
|
+
exports.createCustomCacheKey = createCustomCacheKey_1.default;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import Request from './Request';
|
|
2
|
+
import Response from './Response';
|
|
3
|
+
/**
|
|
4
|
+
* @private
|
|
5
|
+
* Emulates the edge by interpolating header values
|
|
6
|
+
*
|
|
7
|
+
* Note: This code must be kept in sync with the interpolateParams: values from ECL compiler
|
|
8
|
+
*
|
|
9
|
+
* Supported interpolation strings are:
|
|
10
|
+
* - ${method}: the request method GET/PUT/POST/etc...
|
|
11
|
+
* - ${path}: the return path without query parameters
|
|
12
|
+
* - ${url}: the original unmodified request url
|
|
13
|
+
* - ${req:<request header name>}: a request header value
|
|
14
|
+
* - ${req:cookie:<request cookie name>}: a request cookie value
|
|
15
|
+
* - ${res:<response header name>}: a response header value
|
|
16
|
+
*
|
|
17
|
+
* @param req The request
|
|
18
|
+
* @param res The response
|
|
19
|
+
* @param value The string or array of strings to interpolate
|
|
20
|
+
*/
|
|
21
|
+
export default function interpolateParams(req: Request, res: Response, value: any): any;
|
|
@@ -0,0 +1,150 @@
|
|
|
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 cookie_1 = __importDefault(require("cookie"));
|
|
7
|
+
const querystring_1 = require("querystring");
|
|
8
|
+
const identity_1 = __importDefault(require("../utils/identity"));
|
|
9
|
+
/**
|
|
10
|
+
* @private
|
|
11
|
+
* Emulates the edge by interpolating header values
|
|
12
|
+
*
|
|
13
|
+
* Note: This code must be kept in sync with the interpolateParams: values from ECL compiler
|
|
14
|
+
*
|
|
15
|
+
* Supported interpolation strings are:
|
|
16
|
+
* - ${method}: the request method GET/PUT/POST/etc...
|
|
17
|
+
* - ${path}: the return path without query parameters
|
|
18
|
+
* - ${url}: the original unmodified request url
|
|
19
|
+
* - ${req:<request header name>}: a request header value
|
|
20
|
+
* - ${req:cookie:<request cookie name>}: a request cookie value
|
|
21
|
+
* - ${res:<response header name>}: a response header value
|
|
22
|
+
*
|
|
23
|
+
* @param req The request
|
|
24
|
+
* @param res The response
|
|
25
|
+
* @param value The string or array of strings to interpolate
|
|
26
|
+
*/
|
|
27
|
+
function interpolateParams(req, res, value) {
|
|
28
|
+
if (typeof value === 'string') {
|
|
29
|
+
return interpolateParam(req, res, value);
|
|
30
|
+
}
|
|
31
|
+
if (!Array.isArray(value)) {
|
|
32
|
+
// Skip any non-string and non-string array values
|
|
33
|
+
return value;
|
|
34
|
+
}
|
|
35
|
+
const results = [];
|
|
36
|
+
value.forEach(v => {
|
|
37
|
+
results.push(interpolateParams(req, res, v));
|
|
38
|
+
});
|
|
39
|
+
return results;
|
|
40
|
+
}
|
|
41
|
+
exports.default = interpolateParams;
|
|
42
|
+
/**
|
|
43
|
+
* @private
|
|
44
|
+
* Interpolates a single string value
|
|
45
|
+
*
|
|
46
|
+
* NOTE: We emulate the Fastly Edge closely, but there are known difference. E.g. if you have header='hi ${req:cookie:test}' and there is no
|
|
47
|
+
* cookie name 'test' Fastly will return header='hi (null)' but if you have header='${req:cookie:test}' Fastly will drop the header.
|
|
48
|
+
* This interpolator just returns '' for not found values.
|
|
49
|
+
* @param req The request
|
|
50
|
+
* @param res The response
|
|
51
|
+
* @param value a string to interpolate
|
|
52
|
+
*/
|
|
53
|
+
function interpolateParam(req, res, value) {
|
|
54
|
+
// We can have multiple replacements per value, so we need to loop through all the possible cases
|
|
55
|
+
// Use '--backslashdollar--' so we do not interpret '\${' in the results.
|
|
56
|
+
// Use '--separator--' so we keep the '${' in the results of split().
|
|
57
|
+
const segments = value
|
|
58
|
+
.replace(/\\\${/g, '--backslashdollar--')
|
|
59
|
+
.replace(/\${/g, '--separator--${')
|
|
60
|
+
.split('--separator--');
|
|
61
|
+
// Parse cookies once at the top of the function as an optimization
|
|
62
|
+
let cookies = {};
|
|
63
|
+
if (req === null || req === void 0 ? void 0 : req.headers.cookie) {
|
|
64
|
+
// Pass in the 'do nothing' decode function to return the cookie value without decoding it
|
|
65
|
+
cookies = cookie_1.default.parse(req.headers.cookie, { decode: identity_1.default });
|
|
66
|
+
}
|
|
67
|
+
// Parse query parameters once at the top of the function as an optimization
|
|
68
|
+
let qs = {};
|
|
69
|
+
if (req === null || req === void 0 ? void 0 : req.url) {
|
|
70
|
+
const queryParameterIndex = req.url.indexOf('?');
|
|
71
|
+
if (queryParameterIndex >= 0) {
|
|
72
|
+
const queryParameters = req.url.substring(queryParameterIndex + 1);
|
|
73
|
+
qs = querystring_1.parse(queryParameters);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
const interpolatedResults = [];
|
|
77
|
+
const requestPrefixLength = '${req:'.length;
|
|
78
|
+
const queryPrefixLength = '${query:'.length;
|
|
79
|
+
segments.forEach(segment => {
|
|
80
|
+
if (req) {
|
|
81
|
+
segment = segment.replace(/\${method}/, req.method);
|
|
82
|
+
segment = segment.replace(/\${path}/, req.path);
|
|
83
|
+
segment = segment.replace(/\${url}/, req.url);
|
|
84
|
+
// ${query:paramName} returns the found query parameter
|
|
85
|
+
const startQueryParameterIndex = segment.search(/\${query:/);
|
|
86
|
+
if (startQueryParameterIndex >= 0) {
|
|
87
|
+
let queryParameterName = segment.substring(startQueryParameterIndex + queryPrefixLength);
|
|
88
|
+
const endQueryParameterIndex = queryParameterName.indexOf('}');
|
|
89
|
+
if (endQueryParameterIndex <= 0) {
|
|
90
|
+
throw new Error(`Cannot interpolate substring "${segment}" of value "${value}" (1)`);
|
|
91
|
+
}
|
|
92
|
+
queryParameterName = queryParameterName.substring(0, endQueryParameterIndex);
|
|
93
|
+
// When there are multiple matches for the query parameter's name, we emulate
|
|
94
|
+
// the edge's behavior and return the first match only.
|
|
95
|
+
let queryReplacementString = qs[queryParameterName] || '';
|
|
96
|
+
if (Array.isArray(queryReplacementString)) {
|
|
97
|
+
queryReplacementString = queryReplacementString[0];
|
|
98
|
+
}
|
|
99
|
+
segment = segment.replace(/\${query:.*?}/, queryReplacementString);
|
|
100
|
+
}
|
|
101
|
+
const startRequestIndex = segment.search(/\${req:/);
|
|
102
|
+
if (startRequestIndex >= 0) {
|
|
103
|
+
let requestHeaderName = segment.substring(startRequestIndex + requestPrefixLength);
|
|
104
|
+
const endRequestIndex = requestHeaderName.indexOf('}');
|
|
105
|
+
if (endRequestIndex <= 0) {
|
|
106
|
+
throw new Error(`Cannot interpolate substring "${segment}" of value "${value}" (2)`);
|
|
107
|
+
}
|
|
108
|
+
requestHeaderName = requestHeaderName.substring(0, endRequestIndex);
|
|
109
|
+
let replacementString = '';
|
|
110
|
+
// req:cookie:<key> injects the value of cookie named <key> or an empty string if <key> not found
|
|
111
|
+
if (requestHeaderName.indexOf('cookie:') === 0) {
|
|
112
|
+
const cookieName = requestHeaderName.split(':')[1];
|
|
113
|
+
replacementString = cookies[cookieName] || '';
|
|
114
|
+
// req:param:<name> injects the value of named parameter <name> or an empty string if <name> not found
|
|
115
|
+
}
|
|
116
|
+
else if (requestHeaderName.indexOf('param:') === 0) {
|
|
117
|
+
const paramName = requestHeaderName.split(':')[1];
|
|
118
|
+
if (req.params && req.params[paramName]) {
|
|
119
|
+
replacementString = String(req.params[paramName]);
|
|
120
|
+
}
|
|
121
|
+
// req:<header>
|
|
122
|
+
}
|
|
123
|
+
else {
|
|
124
|
+
// Use an empty string if the request header is not found
|
|
125
|
+
replacementString = req.headers[requestHeaderName] || '';
|
|
126
|
+
}
|
|
127
|
+
segment = segment.replace(/\${req:.*?}/, replacementString);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
const startResponseIndex = segment.search(/\${res:/);
|
|
131
|
+
if (startResponseIndex >= 0) {
|
|
132
|
+
if (!res) {
|
|
133
|
+
throw new Error(`Cannot interpolate response parameter "${segment}" during receive phase`);
|
|
134
|
+
}
|
|
135
|
+
let responseHeaderName = segment.substring(startResponseIndex + requestPrefixLength);
|
|
136
|
+
const endResponseIndex = responseHeaderName.indexOf('}');
|
|
137
|
+
if (endResponseIndex <= 0) {
|
|
138
|
+
throw new Error(`Cannot interpolate substring "${segment}" of value "${value}" (3)`);
|
|
139
|
+
}
|
|
140
|
+
responseHeaderName = responseHeaderName.substring(0, endResponseIndex);
|
|
141
|
+
segment = segment.replace(/\${res:.*?}/, res.getHeader(responseHeaderName) || '');
|
|
142
|
+
}
|
|
143
|
+
// If no replacement was done, we assume the <key> in ${<key>} was not valid and throw an error
|
|
144
|
+
if (segment.search(/\${/) >= 0) {
|
|
145
|
+
throw new Error(`Cannot interpolate substring "${segment}" of "${value}" (4)`);
|
|
146
|
+
}
|
|
147
|
+
interpolatedResults.push(segment);
|
|
148
|
+
});
|
|
149
|
+
return interpolatedResults.join('').replace(/--backslashdollar--/g, '${');
|
|
150
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Loads and returns a router. This function can accomodate:
|
|
3
|
+
*
|
|
4
|
+
* 1.) a routes file that uses `module.exports` or `export default`.
|
|
5
|
+
* 2.) a routes file that exports a function using `module.exports` or `export default`.
|
|
6
|
+
* 3.) a router instance
|
|
7
|
+
* 4.) An object with a default key that is a router instance
|
|
8
|
+
* 5.) An object with a default key whose value is a function that returns a router instance.
|
|
9
|
+
*
|
|
10
|
+
* @param routerPath A path to routes.js,
|
|
11
|
+
* @return {Router} The Router instance
|
|
12
|
+
*/
|
|
13
|
+
export default function loadRouter(router: any): any;
|
|
@@ -0,0 +1,29 @@
|
|
|
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 nonWebpackRequire_1 = __importDefault(require("../utils/nonWebpackRequire"));
|
|
7
|
+
/**
|
|
8
|
+
* Loads and returns a router. This function can accomodate:
|
|
9
|
+
*
|
|
10
|
+
* 1.) a routes file that uses `module.exports` or `export default`.
|
|
11
|
+
* 2.) a routes file that exports a function using `module.exports` or `export default`.
|
|
12
|
+
* 3.) a router instance
|
|
13
|
+
* 4.) An object with a default key that is a router instance
|
|
14
|
+
* 5.) An object with a default key whose value is a function that returns a router instance.
|
|
15
|
+
*
|
|
16
|
+
* @param routerPath A path to routes.js,
|
|
17
|
+
* @return {Router} The Router instance
|
|
18
|
+
*/
|
|
19
|
+
function loadRouter(router) {
|
|
20
|
+
if (typeof router === 'string') {
|
|
21
|
+
router = nonWebpackRequire_1.default(router);
|
|
22
|
+
}
|
|
23
|
+
if (router.default)
|
|
24
|
+
router = router.default;
|
|
25
|
+
if (typeof router === 'function')
|
|
26
|
+
router = router();
|
|
27
|
+
return router;
|
|
28
|
+
}
|
|
29
|
+
exports.default = loadRouter;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function mergeQueryString(requestUrl: string, targetRedirectOrPath: string): string;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
/**
|
|
4
|
+
* Used to create URL instance with just a path
|
|
5
|
+
*/
|
|
6
|
+
const PLACEHOLDER_HOSTNAME = '__';
|
|
7
|
+
function mergeQueryString(requestUrl, targetRedirectOrPath) {
|
|
8
|
+
// `https://${PLACEHOLDER_HOSTNAME}` is temporarily inserted to make the `URL` object work
|
|
9
|
+
// with relative path, but is removed from the returned value right after.
|
|
10
|
+
const requestSearchParams = new URL(requestUrl, `https://${PLACEHOLDER_HOSTNAME}`).searchParams;
|
|
11
|
+
const targetUrl = new URL(targetRedirectOrPath, `https://${PLACEHOLDER_HOSTNAME}`);
|
|
12
|
+
// Apply query string from request to the target, except if they are already set explicitly on the target
|
|
13
|
+
requestSearchParams.forEach((value, key) => {
|
|
14
|
+
if (!targetUrl.searchParams.get(key)) {
|
|
15
|
+
targetUrl.searchParams.set(key, value);
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
// Keep the target as initially intended, absolute or relative path
|
|
19
|
+
return targetUrl.hostname === PLACEHOLDER_HOSTNAME
|
|
20
|
+
? `${targetUrl.pathname}${targetUrl.search}`
|
|
21
|
+
: targetUrl.toString();
|
|
22
|
+
}
|
|
23
|
+
exports.default = mergeQueryString;
|