@rayselfs/cf-rule-engine 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +276 -0
- package/dist/adapters/cf-function.cjs +10 -0
- package/dist/adapters/cf-function.d.cts +2 -0
- package/dist/adapters/cf-function.d.ts +2 -0
- package/dist/adapters/cf-function.js +10 -0
- package/dist/adapters/lambda-edge.cjs +10 -0
- package/dist/adapters/lambda-edge.d.cts +2 -0
- package/dist/adapters/lambda-edge.d.ts +2 -0
- package/dist/adapters/lambda-edge.js +10 -0
- package/dist/behaviors/construct-response.cjs +7 -0
- package/dist/behaviors/construct-response.d.cts +62 -0
- package/dist/behaviors/construct-response.d.ts +62 -0
- package/dist/behaviors/construct-response.js +7 -0
- package/dist/behaviors/copy-header.cjs +7 -0
- package/dist/behaviors/copy-header.d.cts +27 -0
- package/dist/behaviors/copy-header.d.ts +27 -0
- package/dist/behaviors/copy-header.js +7 -0
- package/dist/behaviors/directory-index.cjs +7 -0
- package/dist/behaviors/directory-index.d.cts +33 -0
- package/dist/behaviors/directory-index.d.ts +33 -0
- package/dist/behaviors/directory-index.js +7 -0
- package/dist/behaviors/image-optimize.cjs +9 -0
- package/dist/behaviors/image-optimize.d.cts +103 -0
- package/dist/behaviors/image-optimize.d.ts +103 -0
- package/dist/behaviors/image-optimize.js +9 -0
- package/dist/behaviors/index.cjs +126 -0
- package/dist/behaviors/index.d.cts +46 -0
- package/dist/behaviors/index.d.ts +46 -0
- package/dist/behaviors/index.js +126 -0
- package/dist/behaviors/redirect.cjs +7 -0
- package/dist/behaviors/redirect.d.cts +44 -0
- package/dist/behaviors/redirect.d.ts +44 -0
- package/dist/behaviors/redirect.js +7 -0
- package/dist/behaviors/remove-response-headers.cjs +7 -0
- package/dist/behaviors/remove-response-headers.d.cts +27 -0
- package/dist/behaviors/remove-response-headers.d.ts +27 -0
- package/dist/behaviors/remove-response-headers.js +7 -0
- package/dist/behaviors/rewrite-uri.cjs +7 -0
- package/dist/behaviors/rewrite-uri.d.cts +43 -0
- package/dist/behaviors/rewrite-uri.d.ts +43 -0
- package/dist/behaviors/rewrite-uri.js +7 -0
- package/dist/behaviors/set-cache-control.cjs +7 -0
- package/dist/behaviors/set-cache-control.d.cts +29 -0
- package/dist/behaviors/set-cache-control.d.ts +29 -0
- package/dist/behaviors/set-cache-control.js +7 -0
- package/dist/behaviors/set-cors-headers.cjs +7 -0
- package/dist/behaviors/set-cors-headers.d.cts +76 -0
- package/dist/behaviors/set-cors-headers.d.ts +76 -0
- package/dist/behaviors/set-cors-headers.js +7 -0
- package/dist/behaviors/set-csp.cjs +7 -0
- package/dist/behaviors/set-csp.d.cts +48 -0
- package/dist/behaviors/set-csp.d.ts +48 -0
- package/dist/behaviors/set-csp.js +7 -0
- package/dist/behaviors/set-request-header.cjs +7 -0
- package/dist/behaviors/set-request-header.d.cts +26 -0
- package/dist/behaviors/set-request-header.d.ts +26 -0
- package/dist/behaviors/set-request-header.js +7 -0
- package/dist/behaviors/set-response-header.cjs +7 -0
- package/dist/behaviors/set-response-header.d.cts +28 -0
- package/dist/behaviors/set-response-header.d.ts +28 -0
- package/dist/behaviors/set-response-header.js +7 -0
- package/dist/behaviors/set-security-headers.cjs +7 -0
- package/dist/behaviors/set-security-headers.d.cts +55 -0
- package/dist/behaviors/set-security-headers.d.ts +55 -0
- package/dist/behaviors/set-security-headers.js +7 -0
- package/dist/behaviors/strip-query-params.cjs +7 -0
- package/dist/behaviors/strip-query-params.d.cts +25 -0
- package/dist/behaviors/strip-query-params.d.ts +25 -0
- package/dist/behaviors/strip-query-params.js +7 -0
- package/dist/cf-function-D27hUVtN.d.cts +70 -0
- package/dist/cf-function-u0PvbW_s.d.ts +70 -0
- package/dist/chunk-2DE6WPPL.js +21 -0
- package/dist/chunk-32SMWYAF.cjs +13 -0
- package/dist/chunk-3BBLG4IX.cjs +132 -0
- package/dist/chunk-3PVDUC5M.js +12 -0
- package/dist/chunk-5CPBXZ4X.js +12 -0
- package/dist/chunk-5PT5X62W.js +98 -0
- package/dist/chunk-63WIEBQB.cjs +50 -0
- package/dist/chunk-6DBZBV2M.js +42 -0
- package/dist/chunk-75ZPJI57.cjs +9 -0
- package/dist/chunk-B4WEJSEZ.cjs +14 -0
- package/dist/chunk-BDNPQ7AU.js +21 -0
- package/dist/chunk-BUAIBB3N.js +14 -0
- package/dist/chunk-BZQJYOU2.js +12 -0
- package/dist/chunk-C32DL3EP.js +13 -0
- package/dist/chunk-CF5PWWTF.cjs +15 -0
- package/dist/chunk-CV234DQT.cjs +14 -0
- package/dist/chunk-D47P7HVZ.cjs +12 -0
- package/dist/chunk-DSSFFJWL.js +38 -0
- package/dist/chunk-FTP7NLKX.js +29 -0
- package/dist/chunk-G7JGTBTT.cjs +8 -0
- package/dist/chunk-GK5JX7OM.cjs +39 -0
- package/dist/chunk-H2LO6MQG.js +50 -0
- package/dist/chunk-IBXAK2A4.cjs +21 -0
- package/dist/chunk-JGJW7D2N.cjs +12 -0
- package/dist/chunk-JU5WX5RU.cjs +21 -0
- package/dist/chunk-KW5YBTSD.js +12 -0
- package/dist/chunk-KZ72PI2A.js +0 -0
- package/dist/chunk-L7NBJ4JA.cjs +12 -0
- package/dist/chunk-LBJUCJF2.js +14 -0
- package/dist/chunk-LNQPYKGG.js +20 -0
- package/dist/chunk-LO2BO3RU.js +15 -0
- package/dist/chunk-LSCC62CZ.cjs +13 -0
- package/dist/chunk-LTLBEBKL.cjs +42 -0
- package/dist/chunk-M5KUQBDW.js +17 -0
- package/dist/chunk-MEHWLQLR.cjs +1 -0
- package/dist/chunk-MLKGABMK.js +9 -0
- package/dist/chunk-MRPTC74I.cjs +29 -0
- package/dist/chunk-MSES76XK.cjs +14 -0
- package/dist/chunk-MVGYPBYB.cjs +16 -0
- package/dist/chunk-NPMGSPNL.js +88 -0
- package/dist/chunk-OSGZTNTS.cjs +42 -0
- package/dist/chunk-OSZWDCTS.cjs +12 -0
- package/dist/chunk-OTFDML3K.cjs +11 -0
- package/dist/chunk-PPUHEL4H.cjs +17 -0
- package/dist/chunk-PY3JMRDG.js +11 -0
- package/dist/chunk-Q4NP4C3B.js +47 -0
- package/dist/chunk-R7WXS7BI.js +42 -0
- package/dist/chunk-RBBKFG5J.js +14 -0
- package/dist/chunk-S2AAATFN.js +16 -0
- package/dist/chunk-SAXDN5NS.cjs +88 -0
- package/dist/chunk-SGEBNQR2.cjs +14 -0
- package/dist/chunk-SOBTD7AD.js +39 -0
- package/dist/chunk-SRQF5UEJ.js +13 -0
- package/dist/chunk-TQLJIT4H.cjs +98 -0
- package/dist/chunk-U54FZCOH.cjs +14 -0
- package/dist/chunk-UD456E4I.js +8 -0
- package/dist/chunk-UI6LKDJI.cjs +19 -0
- package/dist/chunk-UKB5JAX4.js +32 -0
- package/dist/chunk-VEEOQ7TS.cjs +8 -0
- package/dist/chunk-VQCRSBWL.js +19 -0
- package/dist/chunk-WEBU4R5C.js +132 -0
- package/dist/chunk-WKYMSRCD.cjs +47 -0
- package/dist/chunk-WW7YVRAI.cjs +32 -0
- package/dist/chunk-WWSRNCUP.cjs +38 -0
- package/dist/chunk-XLSZ5RB7.js +8 -0
- package/dist/chunk-XPQG5IML.js +14 -0
- package/dist/chunk-XUI4Y22M.js +20 -0
- package/dist/chunk-YVUR35RN.cjs +20 -0
- package/dist/chunk-ZTMSH34E.js +14 -0
- package/dist/chunk-ZXS23HXA.cjs +20 -0
- package/dist/core/rule.cjs +17 -0
- package/dist/core/rule.d.cts +101 -0
- package/dist/core/rule.d.ts +101 -0
- package/dist/core/rule.js +17 -0
- package/dist/core/types.cjs +1 -0
- package/dist/core/types.d.cts +53 -0
- package/dist/core/types.d.ts +53 -0
- package/dist/core/types.js +1 -0
- package/dist/criteria/country-is.cjs +7 -0
- package/dist/criteria/country-is.d.cts +35 -0
- package/dist/criteria/country-is.d.ts +35 -0
- package/dist/criteria/country-is.js +7 -0
- package/dist/criteria/file-extension.cjs +7 -0
- package/dist/criteria/file-extension.d.cts +32 -0
- package/dist/criteria/file-extension.d.ts +32 -0
- package/dist/criteria/file-extension.js +7 -0
- package/dist/criteria/header-contains.cjs +7 -0
- package/dist/criteria/header-contains.d.cts +33 -0
- package/dist/criteria/header-contains.d.ts +33 -0
- package/dist/criteria/header-contains.js +7 -0
- package/dist/criteria/header-equals.cjs +7 -0
- package/dist/criteria/header-equals.d.cts +31 -0
- package/dist/criteria/header-equals.d.ts +31 -0
- package/dist/criteria/header-equals.js +7 -0
- package/dist/criteria/hostname-is.cjs +7 -0
- package/dist/criteria/hostname-is.d.cts +22 -0
- package/dist/criteria/hostname-is.d.ts +22 -0
- package/dist/criteria/hostname-is.js +7 -0
- package/dist/criteria/index.cjs +49 -0
- package/dist/criteria/index.d.cts +12 -0
- package/dist/criteria/index.d.ts +12 -0
- package/dist/criteria/index.js +49 -0
- package/dist/criteria/ip-cidr.cjs +8 -0
- package/dist/criteria/ip-cidr.d.cts +31 -0
- package/dist/criteria/ip-cidr.d.ts +31 -0
- package/dist/criteria/ip-cidr.js +8 -0
- package/dist/criteria/method-is.cjs +7 -0
- package/dist/criteria/method-is.d.cts +29 -0
- package/dist/criteria/method-is.d.ts +29 -0
- package/dist/criteria/method-is.js +7 -0
- package/dist/criteria/path-equals.cjs +7 -0
- package/dist/criteria/path-equals.d.cts +22 -0
- package/dist/criteria/path-equals.d.ts +22 -0
- package/dist/criteria/path-equals.js +7 -0
- package/dist/criteria/path-matches.cjs +8 -0
- package/dist/criteria/path-matches.d.cts +22 -0
- package/dist/criteria/path-matches.d.ts +22 -0
- package/dist/criteria/path-matches.js +8 -0
- package/dist/criteria/path-prefix.cjs +7 -0
- package/dist/criteria/path-prefix.d.cts +21 -0
- package/dist/criteria/path-prefix.d.ts +21 -0
- package/dist/criteria/path-prefix.js +7 -0
- package/dist/criteria/user-agent-matches.cjs +8 -0
- package/dist/criteria/user-agent-matches.d.cts +32 -0
- package/dist/criteria/user-agent-matches.d.ts +32 -0
- package/dist/criteria/user-agent-matches.js +8 -0
- package/dist/helpers/index.cjs +42 -0
- package/dist/helpers/index.d.cts +34 -0
- package/dist/helpers/index.d.ts +34 -0
- package/dist/helpers/index.js +42 -0
- package/dist/helpers/preflight-request.cjs +8 -0
- package/dist/helpers/preflight-request.d.cts +38 -0
- package/dist/helpers/preflight-request.d.ts +38 -0
- package/dist/helpers/preflight-request.js +8 -0
- package/dist/helpers/send-country-code.cjs +8 -0
- package/dist/helpers/send-country-code.d.cts +35 -0
- package/dist/helpers/send-country-code.d.ts +35 -0
- package/dist/helpers/send-country-code.js +8 -0
- package/dist/helpers/staging-whitelist.cjs +14 -0
- package/dist/helpers/staging-whitelist.d.cts +73 -0
- package/dist/helpers/staging-whitelist.d.ts +73 -0
- package/dist/helpers/staging-whitelist.js +14 -0
- package/dist/index.cjs +26 -0
- package/dist/index.d.cts +4 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +26 -0
- package/dist/lambda-edge-D15Nf__n.d.ts +66 -0
- package/dist/lambda-edge-DxTOa2cg.d.cts +66 -0
- package/dist/shared/cidr.cjs +11 -0
- package/dist/shared/cidr.d.cts +8 -0
- package/dist/shared/cidr.d.ts +8 -0
- package/dist/shared/cidr.js +11 -0
- package/dist/shared/wildcard.cjs +11 -0
- package/dist/shared/wildcard.d.cts +8 -0
- package/dist/shared/wildcard.d.ts +8 -0
- package/dist/shared/wildcard.js +11 -0
- package/package.json +65 -0
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { ResponseBehaviorFn } from '../core/types.cjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Removes one or more headers from the outgoing response.
|
|
5
|
+
*
|
|
6
|
+
* Useful for stripping headers that reveal implementation details
|
|
7
|
+
* (e.g. `server`, `x-powered-by`) or headers added by the origin that
|
|
8
|
+
* should not be forwarded to the client.
|
|
9
|
+
*
|
|
10
|
+
* Header names are matched case-insensitively.
|
|
11
|
+
*
|
|
12
|
+
* @param headerNames - Array of response header names to remove.
|
|
13
|
+
* @returns A `ResponseBehaviorFn` to use directly in `defineViewerResponse` or wrapped in a `ResponseRule`.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* import { removeResponseHeaders } from '@viverse/cf-engine/behaviors'
|
|
18
|
+
* import { defineViewerResponse } from '@viverse/cf-engine/adapters/cf-function'
|
|
19
|
+
*
|
|
20
|
+
* export default defineViewerResponse([
|
|
21
|
+
* removeResponseHeaders(['server', 'x-powered-by', 'x-amzn-requestid']),
|
|
22
|
+
* ])
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
declare function removeResponseHeaders(headerNames: string[]): ResponseBehaviorFn;
|
|
26
|
+
|
|
27
|
+
export { removeResponseHeaders };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { ResponseBehaviorFn } from '../core/types.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Removes one or more headers from the outgoing response.
|
|
5
|
+
*
|
|
6
|
+
* Useful for stripping headers that reveal implementation details
|
|
7
|
+
* (e.g. `server`, `x-powered-by`) or headers added by the origin that
|
|
8
|
+
* should not be forwarded to the client.
|
|
9
|
+
*
|
|
10
|
+
* Header names are matched case-insensitively.
|
|
11
|
+
*
|
|
12
|
+
* @param headerNames - Array of response header names to remove.
|
|
13
|
+
* @returns A `ResponseBehaviorFn` to use directly in `defineViewerResponse` or wrapped in a `ResponseRule`.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* import { removeResponseHeaders } from '@viverse/cf-engine/behaviors'
|
|
18
|
+
* import { defineViewerResponse } from '@viverse/cf-engine/adapters/cf-function'
|
|
19
|
+
*
|
|
20
|
+
* export default defineViewerResponse([
|
|
21
|
+
* removeResponseHeaders(['server', 'x-powered-by', 'x-amzn-requestid']),
|
|
22
|
+
* ])
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
declare function removeResponseHeaders(headerNames: string[]): ResponseBehaviorFn;
|
|
26
|
+
|
|
27
|
+
export { removeResponseHeaders };
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { BehaviorFn } from '../core/types.cjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Rewrite mode that controls how the URI is transformed:
|
|
5
|
+
* - `'set'` — Replace the entire URI with the target value.
|
|
6
|
+
* - `'replace'` — Replace all occurrences of `match` in the URI with `target`.
|
|
7
|
+
* - `'prepend'` — Prepend `target` to the beginning of the URI.
|
|
8
|
+
* - `'regex-replace'` — Replace all matches of the `match` regex pattern with `target`.
|
|
9
|
+
*/
|
|
10
|
+
type RewriteMode = 'set' | 'replace' | 'prepend' | 'regex-replace';
|
|
11
|
+
/**
|
|
12
|
+
* Rewrites the request URI before it is forwarded to the origin.
|
|
13
|
+
*
|
|
14
|
+
* Akamai equivalent: `rewriteUrl` behavior.
|
|
15
|
+
*
|
|
16
|
+
* @param mode - The rewrite strategy to apply (see `RewriteMode`).
|
|
17
|
+
* @param target - The target value for the rewrite (new URI, replacement string, or prefix).
|
|
18
|
+
* @param match - The substring or regex pattern to match against (required for `'replace'`
|
|
19
|
+
* and `'regex-replace'` modes; ignored for `'set'` and `'prepend'`).
|
|
20
|
+
* @returns A `BehaviorFn` to use as the second argument to `rule()`.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```ts
|
|
24
|
+
* import { rewriteUri } from '@viverse/cf-engine/behaviors'
|
|
25
|
+
* import { pathPrefix } from '@viverse/cf-engine/criteria'
|
|
26
|
+
* import { rule } from '@viverse/cf-engine'
|
|
27
|
+
*
|
|
28
|
+
* // Set: replace entire URI
|
|
29
|
+
* rule(pathEquals(['/index.php']), rewriteUri('set', '/index.html'))
|
|
30
|
+
*
|
|
31
|
+
* // Prepend: add a path prefix
|
|
32
|
+
* rule(pathPrefix(['/images/']), rewriteUri('prepend', '/v2'))
|
|
33
|
+
*
|
|
34
|
+
* // Replace: substitute a path segment
|
|
35
|
+
* rule(pathPrefix(['/legacy/']), rewriteUri('replace', '/current/', '/legacy/'))
|
|
36
|
+
*
|
|
37
|
+
* // Regex-replace: pattern-based substitution
|
|
38
|
+
* rule(pathMatches(['/posts/[0-9]*']), rewriteUri('regex-replace', '/articles/$1', '/posts/([0-9]+)'))
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
declare function rewriteUri(mode: RewriteMode, target: string, match?: string): BehaviorFn;
|
|
42
|
+
|
|
43
|
+
export { type RewriteMode, rewriteUri };
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { BehaviorFn } from '../core/types.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Rewrite mode that controls how the URI is transformed:
|
|
5
|
+
* - `'set'` — Replace the entire URI with the target value.
|
|
6
|
+
* - `'replace'` — Replace all occurrences of `match` in the URI with `target`.
|
|
7
|
+
* - `'prepend'` — Prepend `target` to the beginning of the URI.
|
|
8
|
+
* - `'regex-replace'` — Replace all matches of the `match` regex pattern with `target`.
|
|
9
|
+
*/
|
|
10
|
+
type RewriteMode = 'set' | 'replace' | 'prepend' | 'regex-replace';
|
|
11
|
+
/**
|
|
12
|
+
* Rewrites the request URI before it is forwarded to the origin.
|
|
13
|
+
*
|
|
14
|
+
* Akamai equivalent: `rewriteUrl` behavior.
|
|
15
|
+
*
|
|
16
|
+
* @param mode - The rewrite strategy to apply (see `RewriteMode`).
|
|
17
|
+
* @param target - The target value for the rewrite (new URI, replacement string, or prefix).
|
|
18
|
+
* @param match - The substring or regex pattern to match against (required for `'replace'`
|
|
19
|
+
* and `'regex-replace'` modes; ignored for `'set'` and `'prepend'`).
|
|
20
|
+
* @returns A `BehaviorFn` to use as the second argument to `rule()`.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```ts
|
|
24
|
+
* import { rewriteUri } from '@viverse/cf-engine/behaviors'
|
|
25
|
+
* import { pathPrefix } from '@viverse/cf-engine/criteria'
|
|
26
|
+
* import { rule } from '@viverse/cf-engine'
|
|
27
|
+
*
|
|
28
|
+
* // Set: replace entire URI
|
|
29
|
+
* rule(pathEquals(['/index.php']), rewriteUri('set', '/index.html'))
|
|
30
|
+
*
|
|
31
|
+
* // Prepend: add a path prefix
|
|
32
|
+
* rule(pathPrefix(['/images/']), rewriteUri('prepend', '/v2'))
|
|
33
|
+
*
|
|
34
|
+
* // Replace: substitute a path segment
|
|
35
|
+
* rule(pathPrefix(['/legacy/']), rewriteUri('replace', '/current/', '/legacy/'))
|
|
36
|
+
*
|
|
37
|
+
* // Regex-replace: pattern-based substitution
|
|
38
|
+
* rule(pathMatches(['/posts/[0-9]*']), rewriteUri('regex-replace', '/articles/$1', '/posts/([0-9]+)'))
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
declare function rewriteUri(mode: RewriteMode, target: string, match?: string): BehaviorFn;
|
|
42
|
+
|
|
43
|
+
export { type RewriteMode, rewriteUri };
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { ResponseBehaviorFn } from '../core/types.cjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Sets the `Cache-Control` response header to the specified value.
|
|
5
|
+
*
|
|
6
|
+
* Overwrites any existing `Cache-Control` header set by the origin.
|
|
7
|
+
* Use this to enforce caching policies at the edge regardless of origin behavior.
|
|
8
|
+
*
|
|
9
|
+
* @param value - The `Cache-Control` directive string (e.g. `'public, max-age=31536000'`).
|
|
10
|
+
* @returns A `ResponseBehaviorFn` to use directly in `defineViewerResponse` or wrapped in a `ResponseRule`.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* import { setCacheControl } from '@viverse/cf-engine/behaviors'
|
|
15
|
+
* import { pathPrefix } from '@viverse/cf-engine/criteria'
|
|
16
|
+
* import { rule } from '@viverse/cf-engine'
|
|
17
|
+
* import { defineViewerResponse } from '@viverse/cf-engine/adapters/cf-function'
|
|
18
|
+
*
|
|
19
|
+
* export default defineViewerResponse([
|
|
20
|
+
* // Cache static assets for 1 year
|
|
21
|
+
* { criteria: pathPrefix(['/static/']), behavior: setCacheControl('public, max-age=31536000, immutable') },
|
|
22
|
+
* // Never cache API responses
|
|
23
|
+
* { criteria: pathPrefix(['/api/']), behavior: setCacheControl('no-store') },
|
|
24
|
+
* ])
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
declare function setCacheControl(value: string): ResponseBehaviorFn;
|
|
28
|
+
|
|
29
|
+
export { setCacheControl };
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { ResponseBehaviorFn } from '../core/types.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Sets the `Cache-Control` response header to the specified value.
|
|
5
|
+
*
|
|
6
|
+
* Overwrites any existing `Cache-Control` header set by the origin.
|
|
7
|
+
* Use this to enforce caching policies at the edge regardless of origin behavior.
|
|
8
|
+
*
|
|
9
|
+
* @param value - The `Cache-Control` directive string (e.g. `'public, max-age=31536000'`).
|
|
10
|
+
* @returns A `ResponseBehaviorFn` to use directly in `defineViewerResponse` or wrapped in a `ResponseRule`.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* import { setCacheControl } from '@viverse/cf-engine/behaviors'
|
|
15
|
+
* import { pathPrefix } from '@viverse/cf-engine/criteria'
|
|
16
|
+
* import { rule } from '@viverse/cf-engine'
|
|
17
|
+
* import { defineViewerResponse } from '@viverse/cf-engine/adapters/cf-function'
|
|
18
|
+
*
|
|
19
|
+
* export default defineViewerResponse([
|
|
20
|
+
* // Cache static assets for 1 year
|
|
21
|
+
* { criteria: pathPrefix(['/static/']), behavior: setCacheControl('public, max-age=31536000, immutable') },
|
|
22
|
+
* // Never cache API responses
|
|
23
|
+
* { criteria: pathPrefix(['/api/']), behavior: setCacheControl('no-store') },
|
|
24
|
+
* ])
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
declare function setCacheControl(value: string): ResponseBehaviorFn;
|
|
28
|
+
|
|
29
|
+
export { setCacheControl };
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { ResponseBehaviorFn } from '../core/types.cjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* CORS configuration options for the `setCorsHeaders` behavior.
|
|
5
|
+
*/
|
|
6
|
+
interface CorsOptions {
|
|
7
|
+
/**
|
|
8
|
+
* List of allowed origin patterns. Supports exact strings and wildcard `*` patterns
|
|
9
|
+
* (e.g. `'https://*.viverse.com'`). Use `['*']` to allow all origins.
|
|
10
|
+
* Default: `['*']`
|
|
11
|
+
*/
|
|
12
|
+
allowedOrigins?: string[];
|
|
13
|
+
/**
|
|
14
|
+
* When `true`, reflects the incoming `Origin` request header as the
|
|
15
|
+
* `Access-Control-Allow-Origin` response value, provided it matches one of
|
|
16
|
+
* `allowedOrigins`. Required when `allowCredentials` is `true` (browsers reject
|
|
17
|
+
* `Access-Control-Allow-Origin: *` with credentials).
|
|
18
|
+
* Default: `false`
|
|
19
|
+
*/
|
|
20
|
+
allowOriginEcho?: boolean;
|
|
21
|
+
/**
|
|
22
|
+
* Value for the `Access-Control-Allow-Methods` header.
|
|
23
|
+
* Default: `'GET, POST, OPTIONS'`
|
|
24
|
+
*/
|
|
25
|
+
allowedMethods?: string;
|
|
26
|
+
/**
|
|
27
|
+
* Value for the `Access-Control-Allow-Headers` header.
|
|
28
|
+
* Default: `'Content-Type, Cache-Control, Pragma, Range'`
|
|
29
|
+
*/
|
|
30
|
+
allowedHeaders?: string;
|
|
31
|
+
/**
|
|
32
|
+
* When `true`, sets `Access-Control-Allow-Credentials: true`.
|
|
33
|
+
* Must be used together with `allowOriginEcho: true`; browsers reject
|
|
34
|
+
* wildcard origins when credentials are present.
|
|
35
|
+
* Default: `false`
|
|
36
|
+
*/
|
|
37
|
+
allowCredentials?: boolean;
|
|
38
|
+
/**
|
|
39
|
+
* Preflight cache duration in seconds. Sets `Access-Control-Max-Age` when specified.
|
|
40
|
+
* Omit to exclude the header.
|
|
41
|
+
*/
|
|
42
|
+
maxAge?: number;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Sets CORS response headers with configurable origin matching, methods, headers,
|
|
46
|
+
* credentials, and preflight cache duration.
|
|
47
|
+
*
|
|
48
|
+
* Akamai equivalent: typically implemented via `modifyOutgoingResponseHeader` rules
|
|
49
|
+
* for each CORS header individually.
|
|
50
|
+
*
|
|
51
|
+
* @param options - CORS configuration. All fields are optional with safe defaults.
|
|
52
|
+
* @returns A `ResponseBehaviorFn` to use directly in `defineViewerResponse` or wrapped in a `ResponseRule`.
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```ts
|
|
56
|
+
* import { setCorsHeaders } from '@viverse/cf-engine/behaviors'
|
|
57
|
+
* import { defineViewerResponse } from '@viverse/cf-engine/adapters/cf-function'
|
|
58
|
+
*
|
|
59
|
+
* // Allow all origins (default)
|
|
60
|
+
* export default defineViewerResponse([setCorsHeaders()])
|
|
61
|
+
*
|
|
62
|
+
* // Echo origin with credentials (e.g. for authenticated API endpoints)
|
|
63
|
+
* export default defineViewerResponse([
|
|
64
|
+
* setCorsHeaders({
|
|
65
|
+
* allowedOrigins: ['https://www.viverse.com', 'https://*.htc.com'],
|
|
66
|
+
* allowOriginEcho: true,
|
|
67
|
+
* allowCredentials: true,
|
|
68
|
+
* allowedMethods: 'GET, POST, PUT, DELETE, OPTIONS',
|
|
69
|
+
* maxAge: 86400,
|
|
70
|
+
* }),
|
|
71
|
+
* ])
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
declare function setCorsHeaders(options?: CorsOptions): ResponseBehaviorFn;
|
|
75
|
+
|
|
76
|
+
export { type CorsOptions, setCorsHeaders };
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { ResponseBehaviorFn } from '../core/types.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* CORS configuration options for the `setCorsHeaders` behavior.
|
|
5
|
+
*/
|
|
6
|
+
interface CorsOptions {
|
|
7
|
+
/**
|
|
8
|
+
* List of allowed origin patterns. Supports exact strings and wildcard `*` patterns
|
|
9
|
+
* (e.g. `'https://*.viverse.com'`). Use `['*']` to allow all origins.
|
|
10
|
+
* Default: `['*']`
|
|
11
|
+
*/
|
|
12
|
+
allowedOrigins?: string[];
|
|
13
|
+
/**
|
|
14
|
+
* When `true`, reflects the incoming `Origin` request header as the
|
|
15
|
+
* `Access-Control-Allow-Origin` response value, provided it matches one of
|
|
16
|
+
* `allowedOrigins`. Required when `allowCredentials` is `true` (browsers reject
|
|
17
|
+
* `Access-Control-Allow-Origin: *` with credentials).
|
|
18
|
+
* Default: `false`
|
|
19
|
+
*/
|
|
20
|
+
allowOriginEcho?: boolean;
|
|
21
|
+
/**
|
|
22
|
+
* Value for the `Access-Control-Allow-Methods` header.
|
|
23
|
+
* Default: `'GET, POST, OPTIONS'`
|
|
24
|
+
*/
|
|
25
|
+
allowedMethods?: string;
|
|
26
|
+
/**
|
|
27
|
+
* Value for the `Access-Control-Allow-Headers` header.
|
|
28
|
+
* Default: `'Content-Type, Cache-Control, Pragma, Range'`
|
|
29
|
+
*/
|
|
30
|
+
allowedHeaders?: string;
|
|
31
|
+
/**
|
|
32
|
+
* When `true`, sets `Access-Control-Allow-Credentials: true`.
|
|
33
|
+
* Must be used together with `allowOriginEcho: true`; browsers reject
|
|
34
|
+
* wildcard origins when credentials are present.
|
|
35
|
+
* Default: `false`
|
|
36
|
+
*/
|
|
37
|
+
allowCredentials?: boolean;
|
|
38
|
+
/**
|
|
39
|
+
* Preflight cache duration in seconds. Sets `Access-Control-Max-Age` when specified.
|
|
40
|
+
* Omit to exclude the header.
|
|
41
|
+
*/
|
|
42
|
+
maxAge?: number;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Sets CORS response headers with configurable origin matching, methods, headers,
|
|
46
|
+
* credentials, and preflight cache duration.
|
|
47
|
+
*
|
|
48
|
+
* Akamai equivalent: typically implemented via `modifyOutgoingResponseHeader` rules
|
|
49
|
+
* for each CORS header individually.
|
|
50
|
+
*
|
|
51
|
+
* @param options - CORS configuration. All fields are optional with safe defaults.
|
|
52
|
+
* @returns A `ResponseBehaviorFn` to use directly in `defineViewerResponse` or wrapped in a `ResponseRule`.
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```ts
|
|
56
|
+
* import { setCorsHeaders } from '@viverse/cf-engine/behaviors'
|
|
57
|
+
* import { defineViewerResponse } from '@viverse/cf-engine/adapters/cf-function'
|
|
58
|
+
*
|
|
59
|
+
* // Allow all origins (default)
|
|
60
|
+
* export default defineViewerResponse([setCorsHeaders()])
|
|
61
|
+
*
|
|
62
|
+
* // Echo origin with credentials (e.g. for authenticated API endpoints)
|
|
63
|
+
* export default defineViewerResponse([
|
|
64
|
+
* setCorsHeaders({
|
|
65
|
+
* allowedOrigins: ['https://www.viverse.com', 'https://*.htc.com'],
|
|
66
|
+
* allowOriginEcho: true,
|
|
67
|
+
* allowCredentials: true,
|
|
68
|
+
* allowedMethods: 'GET, POST, PUT, DELETE, OPTIONS',
|
|
69
|
+
* maxAge: 86400,
|
|
70
|
+
* }),
|
|
71
|
+
* ])
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
declare function setCorsHeaders(options?: CorsOptions): ResponseBehaviorFn;
|
|
75
|
+
|
|
76
|
+
export { type CorsOptions, setCorsHeaders };
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { ResponseBehaviorFn } from '../core/types.cjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Configuration for the `Content-Security-Policy` header.
|
|
5
|
+
*/
|
|
6
|
+
interface CspOptions {
|
|
7
|
+
/**
|
|
8
|
+
* Map of CSP directive names to their values.
|
|
9
|
+
* Each entry becomes one `<directive> <value>` segment in the header,
|
|
10
|
+
* joined with `'; '`.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* { 'default-src': "'self'", 'img-src': "'self' data: https:", 'script-src': "'self' 'nonce-abc123'" }
|
|
15
|
+
* // → "default-src 'self'; img-src 'self' data: https:; script-src 'self' 'nonce-abc123'"
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
directives: Record<string, string>;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Sets the `Content-Security-Policy` response header from a directives map.
|
|
22
|
+
*
|
|
23
|
+
* Directive entries are joined with `'; '` to form the final header value.
|
|
24
|
+
* Overwrites any existing CSP header from the origin.
|
|
25
|
+
*
|
|
26
|
+
* @param options - CSP configuration object containing the `directives` map.
|
|
27
|
+
* @returns A `ResponseBehaviorFn` to use directly in `defineViewerResponse` or wrapped in a `ResponseRule`.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```ts
|
|
31
|
+
* import { setCsp } from '@viverse/cf-engine/behaviors'
|
|
32
|
+
* import { defineViewerResponse } from '@viverse/cf-engine/adapters/cf-function'
|
|
33
|
+
*
|
|
34
|
+
* export default defineViewerResponse([
|
|
35
|
+
* setCsp({
|
|
36
|
+
* directives: {
|
|
37
|
+
* 'default-src': "'self'",
|
|
38
|
+
* 'script-src': "'self' https://cdn.example.com",
|
|
39
|
+
* 'img-src': "'self' data: https:",
|
|
40
|
+
* 'frame-ancestors': "'none'",
|
|
41
|
+
* },
|
|
42
|
+
* }),
|
|
43
|
+
* ])
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
declare function setCsp(options: CspOptions): ResponseBehaviorFn;
|
|
47
|
+
|
|
48
|
+
export { type CspOptions, setCsp };
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { ResponseBehaviorFn } from '../core/types.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Configuration for the `Content-Security-Policy` header.
|
|
5
|
+
*/
|
|
6
|
+
interface CspOptions {
|
|
7
|
+
/**
|
|
8
|
+
* Map of CSP directive names to their values.
|
|
9
|
+
* Each entry becomes one `<directive> <value>` segment in the header,
|
|
10
|
+
* joined with `'; '`.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* { 'default-src': "'self'", 'img-src': "'self' data: https:", 'script-src': "'self' 'nonce-abc123'" }
|
|
15
|
+
* // → "default-src 'self'; img-src 'self' data: https:; script-src 'self' 'nonce-abc123'"
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
directives: Record<string, string>;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Sets the `Content-Security-Policy` response header from a directives map.
|
|
22
|
+
*
|
|
23
|
+
* Directive entries are joined with `'; '` to form the final header value.
|
|
24
|
+
* Overwrites any existing CSP header from the origin.
|
|
25
|
+
*
|
|
26
|
+
* @param options - CSP configuration object containing the `directives` map.
|
|
27
|
+
* @returns A `ResponseBehaviorFn` to use directly in `defineViewerResponse` or wrapped in a `ResponseRule`.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```ts
|
|
31
|
+
* import { setCsp } from '@viverse/cf-engine/behaviors'
|
|
32
|
+
* import { defineViewerResponse } from '@viverse/cf-engine/adapters/cf-function'
|
|
33
|
+
*
|
|
34
|
+
* export default defineViewerResponse([
|
|
35
|
+
* setCsp({
|
|
36
|
+
* directives: {
|
|
37
|
+
* 'default-src': "'self'",
|
|
38
|
+
* 'script-src': "'self' https://cdn.example.com",
|
|
39
|
+
* 'img-src': "'self' data: https:",
|
|
40
|
+
* 'frame-ancestors': "'none'",
|
|
41
|
+
* },
|
|
42
|
+
* }),
|
|
43
|
+
* ])
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
declare function setCsp(options: CspOptions): ResponseBehaviorFn;
|
|
47
|
+
|
|
48
|
+
export { type CspOptions, setCsp };
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { BehaviorFn } from '../core/types.cjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Sets a request header to the specified value before the request is forwarded to the origin.
|
|
5
|
+
*
|
|
6
|
+
* If the header already exists, it is overwritten. Header names are automatically lowercased
|
|
7
|
+
* to match CloudFront's normalized internal format.
|
|
8
|
+
*
|
|
9
|
+
* Akamai equivalent: `modifyOutgoingRequestHeader` behavior with `action: 'SET'`.
|
|
10
|
+
*
|
|
11
|
+
* @param headerName - The request header name to set (e.g. `'x-forwarded-for'`). Case-insensitive.
|
|
12
|
+
* @param value - The value to assign to the header.
|
|
13
|
+
* @returns A `BehaviorFn` to use as the second argument to `rule()`.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* import { setRequestHeader } from '@viverse/cf-engine/behaviors'
|
|
18
|
+
* import { pathPrefix } from '@viverse/cf-engine/criteria'
|
|
19
|
+
* import { rule } from '@viverse/cf-engine'
|
|
20
|
+
*
|
|
21
|
+
* rule(pathPrefix(['/api/']), setRequestHeader('x-internal-service', 'cf-edge'))
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
declare function setRequestHeader(headerName: string, value: string): BehaviorFn;
|
|
25
|
+
|
|
26
|
+
export { setRequestHeader };
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { BehaviorFn } from '../core/types.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Sets a request header to the specified value before the request is forwarded to the origin.
|
|
5
|
+
*
|
|
6
|
+
* If the header already exists, it is overwritten. Header names are automatically lowercased
|
|
7
|
+
* to match CloudFront's normalized internal format.
|
|
8
|
+
*
|
|
9
|
+
* Akamai equivalent: `modifyOutgoingRequestHeader` behavior with `action: 'SET'`.
|
|
10
|
+
*
|
|
11
|
+
* @param headerName - The request header name to set (e.g. `'x-forwarded-for'`). Case-insensitive.
|
|
12
|
+
* @param value - The value to assign to the header.
|
|
13
|
+
* @returns A `BehaviorFn` to use as the second argument to `rule()`.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* import { setRequestHeader } from '@viverse/cf-engine/behaviors'
|
|
18
|
+
* import { pathPrefix } from '@viverse/cf-engine/criteria'
|
|
19
|
+
* import { rule } from '@viverse/cf-engine'
|
|
20
|
+
*
|
|
21
|
+
* rule(pathPrefix(['/api/']), setRequestHeader('x-internal-service', 'cf-edge'))
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
declare function setRequestHeader(headerName: string, value: string): BehaviorFn;
|
|
25
|
+
|
|
26
|
+
export { setRequestHeader };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { ResponseBehaviorFn } from '../core/types.cjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Sets a response header to the specified value.
|
|
5
|
+
*
|
|
6
|
+
* Overwrites the header if it already exists. Header names are automatically
|
|
7
|
+
* lowercased to match CloudFront's normalized format.
|
|
8
|
+
*
|
|
9
|
+
* Use inside `defineViewerResponse` to add or override outgoing response headers.
|
|
10
|
+
*
|
|
11
|
+
* @param headerName - The response header name to set (e.g. `'x-custom-header'`). Case-insensitive.
|
|
12
|
+
* @param value - The value to assign to the header.
|
|
13
|
+
* @returns A `ResponseBehaviorFn` to use directly in `defineViewerResponse` or wrapped in a `ResponseRule`.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* import { setResponseHeader } from '@viverse/cf-engine/behaviors'
|
|
18
|
+
* import { defineViewerResponse } from '@viverse/cf-engine/adapters/cf-function'
|
|
19
|
+
*
|
|
20
|
+
* export default defineViewerResponse([
|
|
21
|
+
* setResponseHeader('x-powered-by', 'CloudFront'),
|
|
22
|
+
* setResponseHeader('x-custom-env', 'production'),
|
|
23
|
+
* ])
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
declare function setResponseHeader(headerName: string, value: string): ResponseBehaviorFn;
|
|
27
|
+
|
|
28
|
+
export { setResponseHeader };
|