@rayselfs/cf-rule-engine 1.7.0 → 1.8.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.
Files changed (43) hide show
  1. package/README.md +31 -3
  2. package/dist/adapters/cf-function.cjs +4 -4
  3. package/dist/adapters/cf-function.js +3 -3
  4. package/dist/adapters/lambda-edge.d.cts +1 -1
  5. package/dist/adapters/lambda-edge.d.ts +1 -1
  6. package/dist/adapters/viewer-request.cjs +3 -3
  7. package/dist/adapters/viewer-request.js +2 -2
  8. package/dist/behaviors/index.cjs +13 -13
  9. package/dist/behaviors/index.js +12 -12
  10. package/dist/behaviors/set-cors-headers.cjs +6 -2
  11. package/dist/behaviors/set-cors-headers.d.cts +53 -42
  12. package/dist/behaviors/set-cors-headers.d.ts +53 -42
  13. package/dist/behaviors/set-cors-headers.js +5 -1
  14. package/dist/{chunk-H2LO6MQG.js → chunk-CLDF6FM5.js} +22 -12
  15. package/dist/{chunk-PR5UQJCC.js → chunk-MTZ35AIR.js} +1 -1
  16. package/dist/{chunk-SOBTD7AD.js → chunk-PI7RKMGS.js} +21 -10
  17. package/dist/{chunk-63WIEBQB.cjs → chunk-PW4FKMJ2.cjs} +22 -12
  18. package/dist/{chunk-5ZIB3AJ7.cjs → chunk-SYPOHDLN.cjs} +2 -2
  19. package/dist/chunk-XWJEVLMZ.cjs +50 -0
  20. package/dist/criteria/header-equals.d.cts +2 -2
  21. package/dist/criteria/header-equals.d.ts +2 -2
  22. package/dist/criteria/index.cjs +12 -12
  23. package/dist/criteria/index.js +12 -12
  24. package/dist/helpers/index.cjs +9 -8
  25. package/dist/helpers/index.js +8 -7
  26. package/dist/helpers/preflight-request.cjs +3 -2
  27. package/dist/helpers/preflight-request.d.cts +7 -6
  28. package/dist/helpers/preflight-request.d.ts +7 -6
  29. package/dist/helpers/preflight-request.js +2 -1
  30. package/dist/helpers/whitelist.cjs +3 -3
  31. package/dist/helpers/whitelist.js +2 -2
  32. package/dist/index.cjs +7 -7
  33. package/dist/index.d.cts +1 -1
  34. package/dist/index.d.ts +1 -1
  35. package/dist/index.js +5 -5
  36. package/dist/{lambda-edge-9xiONGmR.d.cts → lambda-edge-D1NHYwvA.d.cts} +1 -1
  37. package/dist/{lambda-edge-BK3-bFx8.d.ts → lambda-edge-DnIvWFGe.d.ts} +1 -1
  38. package/package.json +1 -1
  39. package/dist/chunk-GK5JX7OM.cjs +0 -39
  40. package/dist/{chunk-SGN2N3WI.cjs → chunk-FK3B4SP5.cjs} +2 -2
  41. package/dist/{chunk-BJZPAQHW.js → chunk-I2GJR6LY.js} +3 -3
  42. package/dist/{chunk-ER2YEZZO.js → chunk-RL7ZETZR.js} +3 -3
  43. package/dist/{chunk-MO7HW25R.cjs → chunk-T5EXFHVA.cjs} +3 -3
@@ -2,6 +2,10 @@
2
2
 
3
3
  var _chunkOTFDML3Kcjs = require('./chunk-OTFDML3K.cjs');
4
4
 
5
+
6
+
7
+ var _chunkXWJEVLMZcjs = require('./chunk-XWJEVLMZ.cjs');
8
+
5
9
  // src/helpers/preflight-request.ts
6
10
  function matchesOriginPattern(origin, pattern) {
7
11
  if (pattern === "*") return true;
@@ -10,27 +14,33 @@ function matchesOriginPattern(origin, pattern) {
10
14
  return new RegExp(`^${escaped}$`).test(origin);
11
15
  }
12
16
  function preflightRequest(options) {
13
- const allowedOrigins = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _ => _.allowedOrigins]), () => ( ["*"]));
14
- const allowedMethods = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _2 => _2.allowedMethods]), () => ( "GET, POST, OPTIONS"));
15
- const allowedHeaders = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _3 => _3.allowedHeaders]), () => ( "Content-Type, Cache-Control, Pragma, Range"));
16
- const allowCredentials = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _4 => _4.allowCredentials]), () => ( false));
17
- const maxAge = _optionalChain([options, 'optionalAccess', _5 => _5.maxAge]);
17
+ const { allowedOrigins } = options;
18
+ const allowedMethods = _nullishCoalesce(options.allowedMethods, () => ( ["GET", "POST", "OPTIONS"]));
19
+ const allowedHeaders = _nullishCoalesce(options.allowedHeaders, () => ( ["Content-Type", "Cache-Control", "Pragma", "Range"]));
20
+ const allowCredentials = _nullishCoalesce(options.allowCredentials, () => ( false));
21
+ const maxAge = options.maxAge;
18
22
  return {
19
23
  criteria: _chunkOTFDML3Kcjs.methodIs.call(void 0, ["OPTIONS"]),
20
24
  behavior: (request) => {
21
25
  let allowOrigin;
22
- if (_optionalChain([options, 'optionalAccess', _6 => _6.allowOriginEcho])) {
23
- const originHeader = _optionalChain([request, 'access', _7 => _7.headers, 'access', _8 => _8["origin"], 'optionalAccess', _9 => _9.value]);
24
- allowOrigin = originHeader && allowedOrigins.some((p) => matchesOriginPattern(originHeader, p)) ? originHeader : _nullishCoalesce(allowedOrigins[0], () => ( "*"));
26
+ if (allowedOrigins === _chunkXWJEVLMZcjs.ORIGIN_WILDCARD) {
27
+ allowOrigin = "*";
28
+ } else if (allowedOrigins === _chunkXWJEVLMZcjs.ORIGIN_ECHO) {
29
+ allowOrigin = _optionalChain([request, 'access', _ => _.headers, 'access', _2 => _2["origin"], 'optionalAccess', _3 => _3.value]);
25
30
  } else {
26
- allowOrigin = allowedOrigins.includes("*") ? "*" : _nullishCoalesce(allowedOrigins[0], () => ( "*"));
31
+ const originHeader = _optionalChain([request, 'access', _4 => _4.headers, 'access', _5 => _5["origin"], 'optionalAccess', _6 => _6.value]);
32
+ if (originHeader && allowedOrigins.some((p) => matchesOriginPattern(originHeader, p))) {
33
+ allowOrigin = originHeader;
34
+ }
27
35
  }
28
36
  const headers = {
29
37
  "cache-control": { value: "no-store" },
30
- "access-control-allow-origin": { value: allowOrigin },
31
- "access-control-allow-methods": { value: allowedMethods },
32
- "access-control-allow-headers": { value: allowedHeaders }
38
+ "access-control-allow-methods": { value: allowedMethods.join(", ") },
39
+ "access-control-allow-headers": { value: allowedHeaders.join(", ") }
33
40
  };
41
+ if (allowOrigin !== void 0) {
42
+ headers["access-control-allow-origin"] = { value: allowOrigin };
43
+ }
34
44
  if (allowCredentials) {
35
45
  headers["access-control-allow-credentials"] = { value: "true" };
36
46
  }
@@ -1,6 +1,6 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
2
 
3
- var _chunkSGN2N3WIcjs = require('./chunk-SGN2N3WI.cjs');
3
+ var _chunkFK3B4SP5cjs = require('./chunk-FK3B4SP5.cjs');
4
4
 
5
5
 
6
6
  var _chunkN6QAQALUcjs = require('./chunk-N6QAQALU.cjs');
@@ -11,7 +11,7 @@ var _chunk75ZPJI57cjs = require('./chunk-75ZPJI57.cjs');
11
11
  // src/adapters/cf-function.ts
12
12
  var cf_function_exports = {};
13
13
  _chunk75ZPJI57cjs.__export.call(void 0, cf_function_exports, {
14
- defineViewerRequest: () => _chunkSGN2N3WIcjs.defineViewerRequest,
14
+ defineViewerRequest: () => _chunkFK3B4SP5cjs.defineViewerRequest,
15
15
  defineViewerResponse: () => _chunkN6QAQALUcjs.defineViewerResponse
16
16
  });
17
17
 
@@ -0,0 +1,50 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }// src/behaviors/set-cors-headers.ts
2
+ var ORIGIN_WILDCARD = "*";
3
+ var ORIGIN_ECHO = "echo";
4
+ function matchesOriginPattern(origin, pattern) {
5
+ if (pattern === "*") return true;
6
+ if (!pattern.includes("*")) return origin === pattern;
7
+ const escaped = pattern.replace(/[.+^${}()|[\]\\]/g, "\\$&").replace(/\*/g, ".*");
8
+ return new RegExp(`^${escaped}$`).test(origin);
9
+ }
10
+ function setCorsHeaders(options) {
11
+ const { allowedOrigins } = options;
12
+ return (request, response) => {
13
+ let allowOrigin;
14
+ if (allowedOrigins === ORIGIN_WILDCARD) {
15
+ allowOrigin = "*";
16
+ } else if (allowedOrigins === ORIGIN_ECHO) {
17
+ allowOrigin = _optionalChain([request, 'access', _ => _.headers, 'access', _2 => _2["origin"], 'optionalAccess', _3 => _3.value]);
18
+ } else {
19
+ const originHeader = _optionalChain([request, 'access', _4 => _4.headers, 'access', _5 => _5["origin"], 'optionalAccess', _6 => _6.value]);
20
+ if (originHeader && allowedOrigins.some((p) => matchesOriginPattern(originHeader, p))) {
21
+ allowOrigin = originHeader;
22
+ }
23
+ }
24
+ if (allowOrigin === void 0) return response;
25
+ const corsHeaders = {
26
+ "access-control-allow-origin": { value: allowOrigin }
27
+ };
28
+ if (options.allowedMethods !== void 0) {
29
+ corsHeaders["access-control-allow-methods"] = { value: options.allowedMethods.join(", ") };
30
+ }
31
+ if (options.allowedHeaders !== void 0) {
32
+ corsHeaders["access-control-allow-headers"] = { value: options.allowedHeaders.join(", ") };
33
+ }
34
+ if (options.allowCredentials) {
35
+ corsHeaders["access-control-allow-credentials"] = { value: "true" };
36
+ }
37
+ if (options.maxAge !== void 0) {
38
+ corsHeaders["access-control-max-age"] = { value: String(options.maxAge) };
39
+ }
40
+ return Object.assign({}, response, {
41
+ headers: Object.assign({}, response.headers, corsHeaders)
42
+ });
43
+ };
44
+ }
45
+
46
+
47
+
48
+
49
+
50
+ exports.ORIGIN_WILDCARD = ORIGIN_WILDCARD; exports.ORIGIN_ECHO = ORIGIN_ECHO; exports.setCorsHeaders = setCorsHeaders;
@@ -17,12 +17,12 @@ import { CriteriaFn } from '../core/types.cjs';
17
17
  * ```typescript
18
18
  * import { rule } from '@rayselfs/cf-rule-engine'
19
19
  * import { headerEquals } from '@rayselfs/cf-rule-engine/criteria'
20
- * import { setCorsHeaders } from '@rayselfs/cf-rule-engine/behaviors'
20
+ * import { setCorsHeaders, ORIGIN_ECHO } from '@rayselfs/cf-rule-engine/behaviors'
21
21
  *
22
22
  * // Apply CORS headers only for requests from known origins
23
23
  * rule(
24
24
  * headerEquals('origin', ['https://www.example.com', 'https://store.example.com']),
25
- * setCorsHeaders({ allowOriginEcho: true, allowCredentials: true }),
25
+ * setCorsHeaders({ allowedOrigins: ORIGIN_ECHO, allowCredentials: true }),
26
26
  * )
27
27
  * ```
28
28
  */
@@ -17,12 +17,12 @@ import { CriteriaFn } from '../core/types.js';
17
17
  * ```typescript
18
18
  * import { rule } from '@rayselfs/cf-rule-engine'
19
19
  * import { headerEquals } from '@rayselfs/cf-rule-engine/criteria'
20
- * import { setCorsHeaders } from '@rayselfs/cf-rule-engine/behaviors'
20
+ * import { setCorsHeaders, ORIGIN_ECHO } from '@rayselfs/cf-rule-engine/behaviors'
21
21
  *
22
22
  * // Apply CORS headers only for requests from known origins
23
23
  * rule(
24
24
  * headerEquals('origin', ['https://www.example.com', 'https://store.example.com']),
25
- * setCorsHeaders({ allowOriginEcho: true, allowCredentials: true }),
25
+ * setCorsHeaders({ allowedOrigins: ORIGIN_ECHO, allowCredentials: true }),
26
26
  * )
27
27
  * ```
28
28
  */
@@ -1,5 +1,17 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
2
 
3
+ var _chunkG7JGTBTTcjs = require('../chunk-G7JGTBTT.cjs');
4
+
5
+
6
+ var _chunkMVGYPBYBcjs = require('../chunk-MVGYPBYB.cjs');
7
+
8
+
9
+ var _chunk32SMWYAFcjs = require('../chunk-32SMWYAF.cjs');
10
+
11
+
12
+ var _chunkL7NBJ4JAcjs = require('../chunk-L7NBJ4JA.cjs');
13
+
14
+
3
15
  var _chunkJGJW7D2Ncjs = require('../chunk-JGJW7D2N.cjs');
4
16
 
5
17
 
@@ -14,12 +26,6 @@ var _chunkVEEOQ7TScjs = require('../chunk-VEEOQ7TS.cjs');
14
26
 
15
27
 
16
28
  var _chunkCF5PWWTFcjs = require('../chunk-CF5PWWTF.cjs');
17
-
18
-
19
- var _chunkG7JGTBTTcjs = require('../chunk-G7JGTBTT.cjs');
20
-
21
-
22
- var _chunkMVGYPBYBcjs = require('../chunk-MVGYPBYB.cjs');
23
29
  require('../chunk-IBXAK2A4.cjs');
24
30
 
25
31
 
@@ -27,12 +33,6 @@ var _chunkOSZWDCTScjs = require('../chunk-OSZWDCTS.cjs');
27
33
 
28
34
 
29
35
  var _chunkU54FZCOHcjs = require('../chunk-U54FZCOH.cjs');
30
-
31
-
32
- var _chunk32SMWYAFcjs = require('../chunk-32SMWYAF.cjs');
33
-
34
-
35
- var _chunkL7NBJ4JAcjs = require('../chunk-L7NBJ4JA.cjs');
36
36
  require('../chunk-75ZPJI57.cjs');
37
37
 
38
38
 
@@ -1,3 +1,15 @@
1
+ import {
2
+ pathPrefix
3
+ } from "../chunk-XLSZ5RB7.js";
4
+ import {
5
+ userAgentMatches
6
+ } from "../chunk-S2AAATFN.js";
7
+ import {
8
+ headerContains
9
+ } from "../chunk-SRQF5UEJ.js";
10
+ import {
11
+ headerEquals
12
+ } from "../chunk-BZQJYOU2.js";
1
13
  import {
2
14
  hostnameIs
3
15
  } from "../chunk-3PVDUC5M.js";
@@ -14,12 +26,6 @@ import {
14
26
  import {
15
27
  pathMatches
16
28
  } from "../chunk-LO2BO3RU.js";
17
- import {
18
- pathPrefix
19
- } from "../chunk-XLSZ5RB7.js";
20
- import {
21
- userAgentMatches
22
- } from "../chunk-S2AAATFN.js";
23
29
  import "../chunk-2DE6WPPL.js";
24
30
  import {
25
31
  countryIs
@@ -27,12 +33,6 @@ import {
27
33
  import {
28
34
  fileExtension
29
35
  } from "../chunk-LBJUCJF2.js";
30
- import {
31
- headerContains
32
- } from "../chunk-SRQF5UEJ.js";
33
- import {
34
- headerEquals
35
- } from "../chunk-BZQJYOU2.js";
36
36
  import "../chunk-MLKGABMK.js";
37
37
  export {
38
38
  countryIs,
@@ -1,27 +1,28 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
2
 
3
- var _chunk63WIEBQBcjs = require('../chunk-63WIEBQB.cjs');
3
+ var _chunkPW4FKMJ2cjs = require('../chunk-PW4FKMJ2.cjs');
4
4
 
5
5
 
6
6
  var _chunkLSCC62CZcjs = require('../chunk-LSCC62CZ.cjs');
7
7
 
8
8
 
9
- var _chunkMO7HW25Rcjs = require('../chunk-MO7HW25R.cjs');
9
+ var _chunkT5EXFHVAcjs = require('../chunk-T5EXFHVA.cjs');
10
+ require('../chunk-MVGYPBYB.cjs');
11
+
12
+
13
+ var _chunkL7NBJ4JAcjs = require('../chunk-L7NBJ4JA.cjs');
10
14
  require('../chunk-D47P7HVZ.cjs');
11
15
  require('../chunk-YVUR35RN.cjs');
12
16
  require('../chunk-OTFDML3K.cjs');
13
17
  require('../chunk-CF5PWWTF.cjs');
14
- require('../chunk-MVGYPBYB.cjs');
15
18
  require('../chunk-IBXAK2A4.cjs');
16
-
17
-
18
- var _chunkL7NBJ4JAcjs = require('../chunk-L7NBJ4JA.cjs');
19
+ require('../chunk-XWJEVLMZ.cjs');
19
20
 
20
21
 
21
22
  var _chunkB4WEJSEZcjs = require('../chunk-B4WEJSEZ.cjs');
23
+ require('../chunk-JU5WX5RU.cjs');
22
24
  require('../chunk-WWSRNCUP.cjs');
23
25
  require('../chunk-WKYMSRCD.cjs');
24
- require('../chunk-JU5WX5RU.cjs');
25
26
  require('../chunk-75ZPJI57.cjs');
26
27
 
27
28
  // src/helpers/staging-indicator.ts
@@ -39,4 +40,4 @@ function stagingIndicator() {
39
40
 
40
41
 
41
42
 
42
- exports.preflightRequest = _chunk63WIEBQBcjs.preflightRequest; exports.sendCountryCode = _chunkLSCC62CZcjs.sendCountryCode; exports.stagingIndicator = stagingIndicator; exports.whitelist = _chunkMO7HW25Rcjs.whitelist;
43
+ exports.preflightRequest = _chunkPW4FKMJ2cjs.preflightRequest; exports.sendCountryCode = _chunkLSCC62CZcjs.sendCountryCode; exports.stagingIndicator = stagingIndicator; exports.whitelist = _chunkT5EXFHVAcjs.whitelist;
@@ -1,27 +1,28 @@
1
1
  import {
2
2
  preflightRequest
3
- } from "../chunk-H2LO6MQG.js";
3
+ } from "../chunk-CLDF6FM5.js";
4
4
  import {
5
5
  sendCountryCode
6
6
  } from "../chunk-C32DL3EP.js";
7
7
  import {
8
8
  whitelist
9
- } from "../chunk-ER2YEZZO.js";
9
+ } from "../chunk-RL7ZETZR.js";
10
+ import "../chunk-S2AAATFN.js";
11
+ import {
12
+ headerEquals
13
+ } from "../chunk-BZQJYOU2.js";
10
14
  import "../chunk-KW5YBTSD.js";
11
15
  import "../chunk-LNQPYKGG.js";
12
16
  import "../chunk-PY3JMRDG.js";
13
17
  import "../chunk-LO2BO3RU.js";
14
- import "../chunk-S2AAATFN.js";
15
18
  import "../chunk-2DE6WPPL.js";
16
- import {
17
- headerEquals
18
- } from "../chunk-BZQJYOU2.js";
19
+ import "../chunk-PI7RKMGS.js";
19
20
  import {
20
21
  setResponseHeader
21
22
  } from "../chunk-RBBKFG5J.js";
23
+ import "../chunk-BDNPQ7AU.js";
22
24
  import "../chunk-DSSFFJWL.js";
23
25
  import "../chunk-Q4NP4C3B.js";
24
- import "../chunk-BDNPQ7AU.js";
25
26
  import "../chunk-MLKGABMK.js";
26
27
 
27
28
  // src/helpers/staging-indicator.ts
@@ -1,8 +1,9 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
2
 
3
- var _chunk63WIEBQBcjs = require('../chunk-63WIEBQB.cjs');
3
+ var _chunkPW4FKMJ2cjs = require('../chunk-PW4FKMJ2.cjs');
4
4
  require('../chunk-OTFDML3K.cjs');
5
+ require('../chunk-XWJEVLMZ.cjs');
5
6
  require('../chunk-75ZPJI57.cjs');
6
7
 
7
8
 
8
- exports.preflightRequest = _chunk63WIEBQBcjs.preflightRequest;
9
+ exports.preflightRequest = _chunkPW4FKMJ2cjs.preflightRequest;
@@ -5,19 +5,20 @@ import { CorsOptions } from '../behaviors/set-cors-headers.cjs';
5
5
  * Returns a `Rule` that responds 204 to OPTIONS preflight requests with CORS headers.
6
6
  *
7
7
  * Accepts the same `CorsOptions` as `setCorsHeaders` — pass the same object to both
8
- * to define CORS config in one place.
8
+ * to define CORS config in one place. `allowedMethods` and `allowedHeaders` default to
9
+ * permissive values if omitted.
9
10
  *
10
11
  * @example
11
12
  * ```ts
12
13
  * import { preflightRequest } from '@rayselfs/cf-rule-engine/helpers'
13
- * import { setCorsHeaders } from '@rayselfs/cf-rule-engine/behaviors'
14
+ * import { setCorsHeaders, ORIGIN_WILDCARD } from '@rayselfs/cf-rule-engine/behaviors'
14
15
  * import { defineViewerRequest, defineViewerResponse } from '@rayselfs/cf-rule-engine/adapters/cf-function'
15
16
  * import type { CorsOptions } from '@rayselfs/cf-rule-engine/behaviors'
16
17
  *
17
18
  * const CORS: CorsOptions = {
18
- * allowedOrigins: ['*'],
19
- * allowedMethods: 'GET, POST, OPTIONS',
20
- * allowedHeaders: 'Content-Type, Cache-Control, Pragma, Range',
19
+ * allowedOrigins: ORIGIN_WILDCARD,
20
+ * allowedMethods: ['GET', 'POST', 'OPTIONS'],
21
+ * allowedHeaders: ['Content-Type', 'Cache-Control', 'Pragma', 'Range'],
21
22
  * }
22
23
  *
23
24
  * // viewer-request.ts
@@ -33,6 +34,6 @@ import { CorsOptions } from '../behaviors/set-cors-headers.cjs';
33
34
  *
34
35
  * @returns A `Rule` ready to pass into `defineViewerRequest`.
35
36
  */
36
- declare function preflightRequest(options?: CorsOptions): Rule;
37
+ declare function preflightRequest(options: CorsOptions): Rule;
37
38
 
38
39
  export { preflightRequest };
@@ -5,19 +5,20 @@ import { CorsOptions } from '../behaviors/set-cors-headers.js';
5
5
  * Returns a `Rule` that responds 204 to OPTIONS preflight requests with CORS headers.
6
6
  *
7
7
  * Accepts the same `CorsOptions` as `setCorsHeaders` — pass the same object to both
8
- * to define CORS config in one place.
8
+ * to define CORS config in one place. `allowedMethods` and `allowedHeaders` default to
9
+ * permissive values if omitted.
9
10
  *
10
11
  * @example
11
12
  * ```ts
12
13
  * import { preflightRequest } from '@rayselfs/cf-rule-engine/helpers'
13
- * import { setCorsHeaders } from '@rayselfs/cf-rule-engine/behaviors'
14
+ * import { setCorsHeaders, ORIGIN_WILDCARD } from '@rayselfs/cf-rule-engine/behaviors'
14
15
  * import { defineViewerRequest, defineViewerResponse } from '@rayselfs/cf-rule-engine/adapters/cf-function'
15
16
  * import type { CorsOptions } from '@rayselfs/cf-rule-engine/behaviors'
16
17
  *
17
18
  * const CORS: CorsOptions = {
18
- * allowedOrigins: ['*'],
19
- * allowedMethods: 'GET, POST, OPTIONS',
20
- * allowedHeaders: 'Content-Type, Cache-Control, Pragma, Range',
19
+ * allowedOrigins: ORIGIN_WILDCARD,
20
+ * allowedMethods: ['GET', 'POST', 'OPTIONS'],
21
+ * allowedHeaders: ['Content-Type', 'Cache-Control', 'Pragma', 'Range'],
21
22
  * }
22
23
  *
23
24
  * // viewer-request.ts
@@ -33,6 +34,6 @@ import { CorsOptions } from '../behaviors/set-cors-headers.js';
33
34
  *
34
35
  * @returns A `Rule` ready to pass into `defineViewerRequest`.
35
36
  */
36
- declare function preflightRequest(options?: CorsOptions): Rule;
37
+ declare function preflightRequest(options: CorsOptions): Rule;
37
38
 
38
39
  export { preflightRequest };
@@ -1,7 +1,8 @@
1
1
  import {
2
2
  preflightRequest
3
- } from "../chunk-H2LO6MQG.js";
3
+ } from "../chunk-CLDF6FM5.js";
4
4
  import "../chunk-PY3JMRDG.js";
5
+ import "../chunk-PI7RKMGS.js";
5
6
  import "../chunk-MLKGABMK.js";
6
7
  export {
7
8
  preflightRequest
@@ -1,14 +1,14 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
2
 
3
- var _chunkMO7HW25Rcjs = require('../chunk-MO7HW25R.cjs');
3
+ var _chunkT5EXFHVAcjs = require('../chunk-T5EXFHVA.cjs');
4
+ require('../chunk-MVGYPBYB.cjs');
4
5
  require('../chunk-D47P7HVZ.cjs');
5
6
  require('../chunk-YVUR35RN.cjs');
6
7
  require('../chunk-CF5PWWTF.cjs');
7
- require('../chunk-MVGYPBYB.cjs');
8
8
  require('../chunk-IBXAK2A4.cjs');
9
9
  require('../chunk-WWSRNCUP.cjs');
10
10
  require('../chunk-WKYMSRCD.cjs');
11
11
  require('../chunk-75ZPJI57.cjs');
12
12
 
13
13
 
14
- exports.whitelist = _chunkMO7HW25Rcjs.whitelist;
14
+ exports.whitelist = _chunkT5EXFHVAcjs.whitelist;
@@ -1,10 +1,10 @@
1
1
  import {
2
2
  whitelist
3
- } from "../chunk-ER2YEZZO.js";
3
+ } from "../chunk-RL7ZETZR.js";
4
+ import "../chunk-S2AAATFN.js";
4
5
  import "../chunk-KW5YBTSD.js";
5
6
  import "../chunk-LNQPYKGG.js";
6
7
  import "../chunk-LO2BO3RU.js";
7
- import "../chunk-S2AAATFN.js";
8
8
  import "../chunk-2DE6WPPL.js";
9
9
  import "../chunk-DSSFFJWL.js";
10
10
  import "../chunk-Q4NP4C3B.js";
package/dist/index.cjs CHANGED
@@ -1,11 +1,12 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true});require('./chunk-MEHWLQLR.cjs');
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
2
 
3
-
4
- var _chunk5ZIB3AJ7cjs = require('./chunk-5ZIB3AJ7.cjs');
3
+ var _chunkSYPOHDLNcjs = require('./chunk-SYPOHDLN.cjs');
5
4
 
6
5
 
7
6
  var _chunk3BBLG4IXcjs = require('./chunk-3BBLG4IX.cjs');
8
- require('./chunk-SGN2N3WI.cjs');
7
+ require('./chunk-FK3B4SP5.cjs');
8
+ require('./chunk-N6QAQALU.cjs');
9
+ require('./chunk-6NFAPLQ7.cjs');
9
10
 
10
11
 
11
12
 
@@ -14,8 +15,7 @@ require('./chunk-SGN2N3WI.cjs');
14
15
 
15
16
 
16
17
  var _chunkWKYMSRCDcjs = require('./chunk-WKYMSRCD.cjs');
17
- require('./chunk-N6QAQALU.cjs');
18
- require('./chunk-6NFAPLQ7.cjs');
18
+ require('./chunk-MEHWLQLR.cjs');
19
19
  require('./chunk-75ZPJI57.cjs');
20
20
 
21
21
 
@@ -26,4 +26,4 @@ require('./chunk-75ZPJI57.cjs');
26
26
 
27
27
 
28
28
 
29
- exports.all = _chunkWKYMSRCDcjs.all; exports.any = _chunkWKYMSRCDcjs.any; exports.cfFunction = _chunk5ZIB3AJ7cjs.cf_function_exports; exports.chain = _chunkWKYMSRCDcjs.chain; exports.lambdaEdge = _chunk3BBLG4IXcjs.lambda_edge_exports; exports.not = _chunkWKYMSRCDcjs.not; exports.rule = _chunkWKYMSRCDcjs.rule; exports.runRules = _chunkWKYMSRCDcjs.runRules;
29
+ exports.all = _chunkWKYMSRCDcjs.all; exports.any = _chunkWKYMSRCDcjs.any; exports.cfFunction = _chunkSYPOHDLNcjs.cf_function_exports; exports.chain = _chunkWKYMSRCDcjs.chain; exports.lambdaEdge = _chunk3BBLG4IXcjs.lambda_edge_exports; exports.not = _chunkWKYMSRCDcjs.not; exports.rule = _chunkWKYMSRCDcjs.rule; exports.runRules = _chunkWKYMSRCDcjs.runRules;
package/dist/index.d.cts CHANGED
@@ -1,6 +1,6 @@
1
1
  export { BehaviorFn, BehaviorResult, CriteriaFn, HttpRequest, HttpResponse, ResponseBehaviorFn, ResponseRule, Rule, ViewerRequestHandler, ViewerResponseHandler } from './core/types.cjs';
2
2
  export { all, any, chain, not, rule, runRules } from './core/rule.cjs';
3
3
  export { c as cfFunction } from './cf-function-BkfWpTfl.cjs';
4
- export { l as lambdaEdge } from './lambda-edge-9xiONGmR.cjs';
4
+ export { l as lambdaEdge } from './lambda-edge-D1NHYwvA.cjs';
5
5
  import './adapters/viewer-request.cjs';
6
6
  import './adapters/viewer-response.cjs';
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  export { BehaviorFn, BehaviorResult, CriteriaFn, HttpRequest, HttpResponse, ResponseBehaviorFn, ResponseRule, Rule, ViewerRequestHandler, ViewerResponseHandler } from './core/types.js';
2
2
  export { all, any, chain, not, rule, runRules } from './core/rule.js';
3
3
  export { c as cfFunction } from './cf-function-CZwCWch-.js';
4
- export { l as lambdaEdge } from './lambda-edge-BK3-bFx8.js';
4
+ export { l as lambdaEdge } from './lambda-edge-DnIvWFGe.js';
5
5
  import './adapters/viewer-request.js';
6
6
  import './adapters/viewer-response.js';
package/dist/index.js CHANGED
@@ -1,11 +1,12 @@
1
- import "./chunk-KZ72PI2A.js";
2
1
  import {
3
2
  cf_function_exports
4
- } from "./chunk-PR5UQJCC.js";
3
+ } from "./chunk-MTZ35AIR.js";
5
4
  import {
6
5
  lambda_edge_exports
7
6
  } from "./chunk-WEBU4R5C.js";
8
- import "./chunk-BJZPAQHW.js";
7
+ import "./chunk-I2GJR6LY.js";
8
+ import "./chunk-TURH5IFN.js";
9
+ import "./chunk-CQ7YZ3AR.js";
9
10
  import {
10
11
  all,
11
12
  any,
@@ -14,8 +15,7 @@ import {
14
15
  rule,
15
16
  runRules
16
17
  } from "./chunk-Q4NP4C3B.js";
17
- import "./chunk-TURH5IFN.js";
18
- import "./chunk-CQ7YZ3AR.js";
18
+ import "./chunk-KZ72PI2A.js";
19
19
  import "./chunk-MLKGABMK.js";
20
20
  export {
21
21
  all,
@@ -51,7 +51,7 @@ declare function defineViewerRequest(rules: Rule[]): (event: unknown) => unknown
51
51
  *
52
52
  * export const handler = defineViewerResponse([
53
53
  * setSecurityHeaders(),
54
- * setCorsHeaders({ allowedOrigins: ['https://www.example.com'], allowOriginEcho: true }),
54
+ * setCorsHeaders({ allowedOrigins: ['https://www.example.com'] }),
55
55
  * ])
56
56
  * ```
57
57
  */
@@ -51,7 +51,7 @@ declare function defineViewerRequest(rules: Rule[]): (event: unknown) => unknown
51
51
  *
52
52
  * export const handler = defineViewerResponse([
53
53
  * setSecurityHeaders(),
54
- * setCorsHeaders({ allowedOrigins: ['https://www.example.com'], allowOriginEcho: true }),
54
+ * setCorsHeaders({ allowedOrigins: ['https://www.example.com'] }),
55
55
  * ])
56
56
  * ```
57
57
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rayselfs/cf-rule-engine",
3
- "version": "1.7.0",
3
+ "version": "1.8.1",
4
4
  "description": "Composable, tree-shakeable CloudFront Function rules",
5
5
  "license": "MIT",
6
6
  "sideEffects": false,
@@ -1,39 +0,0 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }// src/behaviors/set-cors-headers.ts
2
- function matchesOriginPattern(origin, pattern) {
3
- if (pattern === "*") return true;
4
- if (!pattern.includes("*")) return origin === pattern;
5
- const escaped = pattern.replace(/[.+^${}()|[\]\\]/g, "\\$&").replace(/\*/g, ".*");
6
- return new RegExp(`^${escaped}$`).test(origin);
7
- }
8
- function setCorsHeaders(options) {
9
- const allowedOrigins = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _ => _.allowedOrigins]), () => ( ["*"]));
10
- const allowedMethods = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _2 => _2.allowedMethods]), () => ( "GET, POST, OPTIONS"));
11
- const allowedHeaders = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _3 => _3.allowedHeaders]), () => ( "Content-Type, Cache-Control, Pragma, Range"));
12
- return (request, response) => {
13
- let allowOrigin = _nullishCoalesce(allowedOrigins[0], () => ( "*"));
14
- if (_optionalChain([options, 'optionalAccess', _4 => _4.allowOriginEcho])) {
15
- const originHeader = _optionalChain([request, 'access', _5 => _5.headers, 'access', _6 => _6["origin"], 'optionalAccess', _7 => _7.value]);
16
- if (originHeader && allowedOrigins.some((p) => matchesOriginPattern(originHeader, p))) {
17
- allowOrigin = originHeader;
18
- }
19
- }
20
- const corsHeaders = {
21
- "access-control-allow-origin": { value: allowOrigin },
22
- "access-control-allow-methods": { value: allowedMethods },
23
- "access-control-allow-headers": { value: allowedHeaders }
24
- };
25
- if (_optionalChain([options, 'optionalAccess', _8 => _8.allowCredentials])) {
26
- corsHeaders["access-control-allow-credentials"] = { value: "true" };
27
- }
28
- if (_optionalChain([options, 'optionalAccess', _9 => _9.maxAge]) !== void 0) {
29
- corsHeaders["access-control-max-age"] = { value: String(options.maxAge) };
30
- }
31
- return Object.assign({}, response, {
32
- headers: Object.assign({}, response.headers, corsHeaders)
33
- });
34
- };
35
- }
36
-
37
-
38
-
39
- exports.setCorsHeaders = setCorsHeaders;
@@ -1,11 +1,11 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }
2
2
 
3
- var _chunkWKYMSRCDcjs = require('./chunk-WKYMSRCD.cjs');
4
3
 
5
4
 
5
+ var _chunk6NFAPLQ7cjs = require('./chunk-6NFAPLQ7.cjs');
6
6
 
7
7
 
8
- var _chunk6NFAPLQ7cjs = require('./chunk-6NFAPLQ7.cjs');
8
+ var _chunkWKYMSRCDcjs = require('./chunk-WKYMSRCD.cjs');
9
9
 
10
10
  // src/adapters/viewer-request.ts
11
11
  function defineViewerRequest(rules) {
@@ -1,11 +1,11 @@
1
- import {
2
- runRules
3
- } from "./chunk-Q4NP4C3B.js";
4
1
  import {
5
2
  denormalizeRequest,
6
3
  denormalizeResponse,
7
4
  normalizeRequest
8
5
  } from "./chunk-CQ7YZ3AR.js";
6
+ import {
7
+ runRules
8
+ } from "./chunk-Q4NP4C3B.js";
9
9
 
10
10
  // src/adapters/viewer-request.ts
11
11
  function defineViewerRequest(rules) {
@@ -1,12 +1,12 @@
1
+ import {
2
+ userAgentMatches
3
+ } from "./chunk-S2AAATFN.js";
1
4
  import {
2
5
  ipCidr
3
6
  } from "./chunk-KW5YBTSD.js";
4
7
  import {
5
8
  pathMatches
6
9
  } from "./chunk-LO2BO3RU.js";
7
- import {
8
- userAgentMatches
9
- } from "./chunk-S2AAATFN.js";
10
10
  import {
11
11
  redirect
12
12
  } from "./chunk-DSSFFJWL.js";