@rayselfs/cf-rule-engine 1.8.2 → 1.9.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 (91) hide show
  1. package/dist/adapters/lambda-edge.cjs +2 -2
  2. package/dist/adapters/lambda-edge.js +1 -1
  3. package/dist/adapters/viewer-request-async.cjs +26 -0
  4. package/dist/adapters/viewer-request-async.d.cts +28 -0
  5. package/dist/adapters/viewer-request-async.d.ts +28 -0
  6. package/dist/adapters/viewer-request-async.js +26 -0
  7. package/dist/behaviors/construct-response.d.cts +2 -2
  8. package/dist/behaviors/construct-response.d.ts +2 -2
  9. package/dist/behaviors/image-optimize.d.cts +24 -13
  10. package/dist/behaviors/image-optimize.d.ts +24 -13
  11. package/dist/behaviors/index.cjs +12 -11
  12. package/dist/behaviors/index.d.cts +2 -2
  13. package/dist/behaviors/index.d.ts +2 -2
  14. package/dist/behaviors/index.js +11 -10
  15. package/dist/behaviors/kvs.cjs +24 -0
  16. package/dist/behaviors/kvs.d.cts +34 -0
  17. package/dist/behaviors/kvs.d.ts +34 -0
  18. package/dist/behaviors/kvs.js +24 -0
  19. package/dist/behaviors/redirect.d.cts +2 -2
  20. package/dist/behaviors/redirect.d.ts +2 -2
  21. package/dist/behaviors/rewrite-uri.cjs +2 -2
  22. package/dist/behaviors/rewrite-uri.js +1 -1
  23. package/dist/behaviors/set-cors-headers.cjs +3 -2
  24. package/dist/behaviors/set-cors-headers.d.cts +5 -22
  25. package/dist/behaviors/set-cors-headers.d.ts +5 -22
  26. package/dist/behaviors/set-cors-headers.js +2 -1
  27. package/dist/behaviors/set-csp.d.cts +2 -2
  28. package/dist/behaviors/set-csp.d.ts +2 -2
  29. package/dist/behaviors/set-security-headers.d.cts +2 -2
  30. package/dist/behaviors/set-security-headers.d.ts +2 -2
  31. package/dist/{chunk-ORW3KDO5.js → chunk-7EA7GFWX.js} +4 -7
  32. package/dist/{chunk-MRPTC74I.cjs → chunk-BSH5JZBL.cjs} +4 -2
  33. package/dist/{chunk-2DE6WPPL.js → chunk-EEZ7NUJG.js} +12 -1
  34. package/dist/{chunk-PBR6AREG.cjs → chunk-EMDI676G.cjs} +7 -10
  35. package/dist/{chunk-3BBLG4IX.cjs → chunk-G4JEAL6L.cjs} +11 -8
  36. package/dist/{chunk-CQA2DCVF.js → chunk-H3RK4USR.js} +4 -6
  37. package/dist/{chunk-RL7ZETZR.js → chunk-IHDSTTO2.js} +5 -5
  38. package/dist/{chunk-AEZDDJEW.cjs → chunk-IHVOAORH.cjs} +6 -8
  39. package/dist/{chunk-T5EXFHVA.cjs → chunk-ISXKMJCN.cjs} +5 -5
  40. package/dist/{chunk-MVGYPBYB.cjs → chunk-LVOM5GJ6.cjs} +2 -2
  41. package/dist/{chunk-D47P7HVZ.cjs → chunk-MK4QBCD5.cjs} +2 -2
  42. package/dist/chunk-NWRGD3AH.js +71 -0
  43. package/dist/{chunk-FTP7NLKX.js → chunk-QVY6REMD.js} +4 -2
  44. package/dist/{chunk-IBXAK2A4.cjs → chunk-ULICUDDH.cjs} +12 -1
  45. package/dist/{chunk-WEBU4R5C.js → chunk-ULR7EP5D.js} +11 -8
  46. package/dist/{chunk-S2AAATFN.js → chunk-VQGBRWJK.js} +1 -1
  47. package/dist/chunk-WZKRNMF2.cjs +71 -0
  48. package/dist/{chunk-LO2BO3RU.js → chunk-Y7TIDVVC.js} +1 -1
  49. package/dist/{chunk-KW5YBTSD.js → chunk-YHTUV2SA.js} +1 -1
  50. package/dist/{chunk-CF5PWWTF.cjs → chunk-ZEFLAOTL.cjs} +2 -2
  51. package/dist/core/types.d.cts +8 -8
  52. package/dist/core/types.d.ts +8 -8
  53. package/dist/criteria/file-extension.d.cts +3 -3
  54. package/dist/criteria/file-extension.d.ts +3 -3
  55. package/dist/criteria/index.cjs +8 -8
  56. package/dist/criteria/index.js +7 -7
  57. package/dist/criteria/ip-cidr.cjs +3 -3
  58. package/dist/criteria/ip-cidr.js +2 -2
  59. package/dist/criteria/kvs.cjs +14 -0
  60. package/dist/criteria/kvs.d.cts +34 -0
  61. package/dist/criteria/kvs.d.ts +34 -0
  62. package/dist/criteria/kvs.js +14 -0
  63. package/dist/criteria/path-matches.cjs +3 -3
  64. package/dist/criteria/path-matches.js +2 -2
  65. package/dist/criteria/user-agent-matches.cjs +3 -3
  66. package/dist/criteria/user-agent-matches.js +2 -2
  67. package/dist/helpers/index.cjs +10 -10
  68. package/dist/helpers/index.js +10 -10
  69. package/dist/helpers/preflight-request.cjs +4 -3
  70. package/dist/helpers/preflight-request.js +3 -2
  71. package/dist/helpers/whitelist.cjs +7 -7
  72. package/dist/helpers/whitelist.d.cts +2 -2
  73. package/dist/helpers/whitelist.d.ts +2 -2
  74. package/dist/helpers/whitelist.js +6 -6
  75. package/dist/index.cjs +2 -2
  76. package/dist/index.js +1 -1
  77. package/dist/shared/cidr.cjs +2 -2
  78. package/dist/shared/cidr.d.cts +2 -2
  79. package/dist/shared/cidr.d.ts +2 -2
  80. package/dist/shared/cidr.js +1 -1
  81. package/dist/shared/kvs.cjs +1 -0
  82. package/dist/shared/kvs.d.cts +10 -0
  83. package/dist/shared/kvs.d.ts +10 -0
  84. package/dist/shared/kvs.js +0 -0
  85. package/dist/shared/wildcard.cjs +4 -2
  86. package/dist/shared/wildcard.d.cts +10 -1
  87. package/dist/shared/wildcard.d.ts +10 -1
  88. package/dist/shared/wildcard.js +3 -1
  89. package/package.json +1 -1
  90. package/dist/chunk-LNQPYKGG.js +0 -20
  91. package/dist/chunk-YVUR35RN.cjs +0 -20
@@ -16,14 +16,11 @@ type Origin = `https://${string}` | `http://${string}`;
16
16
  * - `ORIGIN_ECHO` (`'echo'`) — echo any request `Origin` if present, skip if none
17
17
  */
18
18
  type OriginPolicy = OriginWildcard | Origin[] | OriginEcho;
19
- /**
20
- * Standard HTTP methods allowed in `Access-Control-Allow-Methods`.
21
- */
22
19
  type Methods = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' | 'OPTIONS' | 'TRACE' | 'CONNECT';
23
20
  /**
24
21
  * CORS configuration options for `setCorsHeaders` and `preflightRequest`.
25
22
  */
26
- interface CorsOptions {
23
+ type CorsOptions = {
27
24
  /**
28
25
  * Origin policy. See `OriginPolicy` for details.
29
26
  */
@@ -54,7 +51,7 @@ interface CorsOptions {
54
51
  * Omit to exclude the header.
55
52
  */
56
53
  maxAge?: number;
57
- }
54
+ };
58
55
  /**
59
56
  * Sets CORS response headers with configurable origin policy.
60
57
  *
@@ -63,23 +60,9 @@ interface CorsOptions {
63
60
  *
64
61
  * @example
65
62
  * ```ts
66
- * import { setCorsHeaders, ORIGIN_WILDCARD, ORIGIN_ECHO } from '@rayselfs/cf-rule-engine/behaviors/set-cors-headers'
67
- * import { defineViewerResponse } from '@rayselfs/cf-rule-engine/adapters/viewer-response'
68
- *
69
- * // Public API — static Access-Control-Allow-Origin: *
70
- * export default defineViewerResponse([
71
- * setCorsHeaders({ allowedOrigins: ORIGIN_WILDCARD }),
72
- * ])
73
- *
74
- * // Restricted — echo only listed origins (supports wildcard subdomains)
75
- * export default defineViewerResponse([
76
- * setCorsHeaders({ allowedOrigins: ['https://*.viverse.com', 'https://sdk-api.viverse.com'] }),
77
- * ])
78
- *
79
- * // Echo any origin (e.g. for credentialed requests)
80
- * export default defineViewerResponse([
81
- * setCorsHeaders({ allowedOrigins: ORIGIN_ECHO, allowCredentials: true }),
82
- * ])
63
+ * setCorsHeaders({ allowedOrigins: ORIGIN_WILDCARD })
64
+ * setCorsHeaders({ allowedOrigins: ['https://*.viverse.com'] })
65
+ * setCorsHeaders({ allowedOrigins: ORIGIN_ECHO, allowCredentials: true })
83
66
  * ```
84
67
  */
85
68
  declare function setCorsHeaders(options: CorsOptions): ResponseBehaviorFn;
@@ -16,14 +16,11 @@ type Origin = `https://${string}` | `http://${string}`;
16
16
  * - `ORIGIN_ECHO` (`'echo'`) — echo any request `Origin` if present, skip if none
17
17
  */
18
18
  type OriginPolicy = OriginWildcard | Origin[] | OriginEcho;
19
- /**
20
- * Standard HTTP methods allowed in `Access-Control-Allow-Methods`.
21
- */
22
19
  type Methods = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' | 'OPTIONS' | 'TRACE' | 'CONNECT';
23
20
  /**
24
21
  * CORS configuration options for `setCorsHeaders` and `preflightRequest`.
25
22
  */
26
- interface CorsOptions {
23
+ type CorsOptions = {
27
24
  /**
28
25
  * Origin policy. See `OriginPolicy` for details.
29
26
  */
@@ -54,7 +51,7 @@ interface CorsOptions {
54
51
  * Omit to exclude the header.
55
52
  */
56
53
  maxAge?: number;
57
- }
54
+ };
58
55
  /**
59
56
  * Sets CORS response headers with configurable origin policy.
60
57
  *
@@ -63,23 +60,9 @@ interface CorsOptions {
63
60
  *
64
61
  * @example
65
62
  * ```ts
66
- * import { setCorsHeaders, ORIGIN_WILDCARD, ORIGIN_ECHO } from '@rayselfs/cf-rule-engine/behaviors/set-cors-headers'
67
- * import { defineViewerResponse } from '@rayselfs/cf-rule-engine/adapters/viewer-response'
68
- *
69
- * // Public API — static Access-Control-Allow-Origin: *
70
- * export default defineViewerResponse([
71
- * setCorsHeaders({ allowedOrigins: ORIGIN_WILDCARD }),
72
- * ])
73
- *
74
- * // Restricted — echo only listed origins (supports wildcard subdomains)
75
- * export default defineViewerResponse([
76
- * setCorsHeaders({ allowedOrigins: ['https://*.viverse.com', 'https://sdk-api.viverse.com'] }),
77
- * ])
78
- *
79
- * // Echo any origin (e.g. for credentialed requests)
80
- * export default defineViewerResponse([
81
- * setCorsHeaders({ allowedOrigins: ORIGIN_ECHO, allowCredentials: true }),
82
- * ])
63
+ * setCorsHeaders({ allowedOrigins: ORIGIN_WILDCARD })
64
+ * setCorsHeaders({ allowedOrigins: ['https://*.viverse.com'] })
65
+ * setCorsHeaders({ allowedOrigins: ORIGIN_ECHO, allowCredentials: true })
83
66
  * ```
84
67
  */
85
68
  declare function setCorsHeaders(options: CorsOptions): ResponseBehaviorFn;
@@ -2,7 +2,8 @@ import {
2
2
  ORIGIN_ECHO,
3
3
  ORIGIN_WILDCARD,
4
4
  setCorsHeaders
5
- } from "../chunk-CQA2DCVF.js";
5
+ } from "../chunk-H3RK4USR.js";
6
+ import "../chunk-EEZ7NUJG.js";
6
7
  import "../chunk-MLKGABMK.js";
7
8
  export {
8
9
  ORIGIN_ECHO,
@@ -3,7 +3,7 @@ import { ResponseBehaviorFn } from '../core/types.cjs';
3
3
  /**
4
4
  * Configuration for the `Content-Security-Policy` header.
5
5
  */
6
- interface CspOptions {
6
+ type CspOptions = {
7
7
  /**
8
8
  * Map of CSP directive names to their values.
9
9
  * Each entry becomes one `<directive> <value>` segment in the header,
@@ -16,7 +16,7 @@ interface CspOptions {
16
16
  * ```
17
17
  */
18
18
  directives: Record<string, string>;
19
- }
19
+ };
20
20
  /**
21
21
  * Sets the `Content-Security-Policy` response header from a directives map.
22
22
  *
@@ -3,7 +3,7 @@ import { ResponseBehaviorFn } from '../core/types.js';
3
3
  /**
4
4
  * Configuration for the `Content-Security-Policy` header.
5
5
  */
6
- interface CspOptions {
6
+ type CspOptions = {
7
7
  /**
8
8
  * Map of CSP directive names to their values.
9
9
  * Each entry becomes one `<directive> <value>` segment in the header,
@@ -16,7 +16,7 @@ interface CspOptions {
16
16
  * ```
17
17
  */
18
18
  directives: Record<string, string>;
19
- }
19
+ };
20
20
  /**
21
21
  * Sets the `Content-Security-Policy` response header from a directives map.
22
22
  *
@@ -8,7 +8,7 @@ import { ResponseBehaviorFn } from '../core/types.cjs';
8
8
  *
9
9
  * Pass at least one field.
10
10
  */
11
- interface SecurityHeadersOptions {
11
+ type SecurityHeadersOptions = {
12
12
  /**
13
13
  * Value for the `Strict-Transport-Security` header.
14
14
  * Example: `'max-age=31536000; includeSubDomains'`
@@ -31,7 +31,7 @@ interface SecurityHeadersOptions {
31
31
  * Note: deprecated in modern browsers but still used for legacy compatibility.
32
32
  */
33
33
  xXssProtection?: string;
34
- }
34
+ };
35
35
  /**
36
36
  * Sets security headers on the outgoing response.
37
37
  *
@@ -8,7 +8,7 @@ import { ResponseBehaviorFn } from '../core/types.js';
8
8
  *
9
9
  * Pass at least one field.
10
10
  */
11
- interface SecurityHeadersOptions {
11
+ type SecurityHeadersOptions = {
12
12
  /**
13
13
  * Value for the `Strict-Transport-Security` header.
14
14
  * Example: `'max-age=31536000; includeSubDomains'`
@@ -31,7 +31,7 @@ interface SecurityHeadersOptions {
31
31
  * Note: deprecated in modern browsers but still used for legacy compatibility.
32
32
  */
33
33
  xXssProtection?: string;
34
- }
34
+ };
35
35
  /**
36
36
  * Sets security headers on the outgoing response.
37
37
  *
@@ -4,15 +4,12 @@ import {
4
4
  import {
5
5
  ORIGIN_ECHO,
6
6
  ORIGIN_WILDCARD
7
- } from "./chunk-CQA2DCVF.js";
7
+ } from "./chunk-H3RK4USR.js";
8
+ import {
9
+ matchesOriginPattern
10
+ } from "./chunk-EEZ7NUJG.js";
8
11
 
9
12
  // src/helpers/preflight-request.ts
10
- function matchesOriginPattern(origin, pattern) {
11
- if (pattern === "*") return true;
12
- if (!pattern.includes("*")) return origin === pattern;
13
- const escaped = pattern.replace(/[.+^${}()|[\]\\]/g, "\\$&").replace(/\*/g, ".*");
14
- return new RegExp(`^${escaped}$`).test(origin);
15
- }
16
13
  function preflightRequest(options) {
17
14
  const allowedOrigins = options.allowedOrigins;
18
15
  const allowedMethods = options.allowedMethods ?? ["GET", "POST", "OPTIONS"];
@@ -1,5 +1,6 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true});// src/behaviors/rewrite-uri.ts
2
2
  function rewriteUri(mode, target, match) {
3
+ const re = mode === "regex-replace" && match !== void 0 ? new RegExp(match, "g") : null;
3
4
  return (request) => {
4
5
  let uri = request.uri;
5
6
  switch (mode) {
@@ -15,8 +16,9 @@ function rewriteUri(mode, target, match) {
15
16
  }
16
17
  break;
17
18
  case "regex-replace":
18
- if (match !== void 0) {
19
- uri = uri.replace(new RegExp(match, "g"), target);
19
+ if (re) {
20
+ re.lastIndex = 0;
21
+ uri = uri.replace(re, target);
20
22
  }
21
23
  break;
22
24
  }
@@ -1,5 +1,6 @@
1
1
  // src/shared/wildcard.ts
2
2
  var regexCache = /* @__PURE__ */ Object.create(null);
3
+ var originPatternCache = /* @__PURE__ */ Object.create(null);
3
4
  function wildcardToRegex(pattern) {
4
5
  if (!(pattern in regexCache)) {
5
6
  const escaped = pattern.replace(/[.+^${}()|[\]\\]/g, "\\$&").replace(/\*/g, ".*").replace(/\?/g, ".");
@@ -13,9 +14,19 @@ function matchesWildcard(str, pattern) {
13
14
  function matchesAnyWildcard(str, patterns) {
14
15
  return patterns.some((p) => matchesWildcard(str, p));
15
16
  }
17
+ function matchesOriginPattern(origin, pattern) {
18
+ if (pattern === "*") return true;
19
+ if (!pattern.includes("*")) return origin === pattern;
20
+ if (!(pattern in originPatternCache)) {
21
+ const escaped = pattern.replace(/[.+^${}()|[\]\\]/g, "\\$&").replace(/\*/g, ".*");
22
+ originPatternCache[pattern] = new RegExp(`^${escaped}$`);
23
+ }
24
+ return originPatternCache[pattern].test(origin);
25
+ }
16
26
 
17
27
  export {
18
28
  wildcardToRegex,
19
29
  matchesWildcard,
20
- matchesAnyWildcard
30
+ matchesAnyWildcard,
31
+ matchesOriginPattern
21
32
  };
@@ -4,15 +4,12 @@ var _chunkOTFDML3Kcjs = require('./chunk-OTFDML3K.cjs');
4
4
 
5
5
 
6
6
 
7
- var _chunkAEZDDJEWcjs = require('./chunk-AEZDDJEW.cjs');
7
+ var _chunkIHVOAORHcjs = require('./chunk-IHVOAORH.cjs');
8
+
9
+
10
+ var _chunkULICUDDHcjs = require('./chunk-ULICUDDH.cjs');
8
11
 
9
12
  // src/helpers/preflight-request.ts
10
- function matchesOriginPattern(origin, pattern) {
11
- if (pattern === "*") return true;
12
- if (!pattern.includes("*")) return origin === pattern;
13
- const escaped = pattern.replace(/[.+^${}()|[\]\\]/g, "\\$&").replace(/\*/g, ".*");
14
- return new RegExp(`^${escaped}$`).test(origin);
15
- }
16
13
  function preflightRequest(options) {
17
14
  const allowedOrigins = options.allowedOrigins;
18
15
  const allowedMethods = _nullishCoalesce(options.allowedMethods, () => ( ["GET", "POST", "OPTIONS"]));
@@ -23,13 +20,13 @@ function preflightRequest(options) {
23
20
  criteria: _chunkOTFDML3Kcjs.methodIs.call(void 0, ["OPTIONS"]),
24
21
  behavior: (request) => {
25
22
  let allowOrigin;
26
- if (allowedOrigins === _chunkAEZDDJEWcjs.ORIGIN_WILDCARD) {
23
+ if (allowedOrigins === _chunkIHVOAORHcjs.ORIGIN_WILDCARD) {
27
24
  allowOrigin = "*";
28
- } else if (allowedOrigins === _chunkAEZDDJEWcjs.ORIGIN_ECHO) {
25
+ } else if (allowedOrigins === _chunkIHVOAORHcjs.ORIGIN_ECHO) {
29
26
  allowOrigin = _optionalChain([request, 'access', _ => _.headers, 'access', _2 => _2["origin"], 'optionalAccess', _3 => _3.value]);
30
27
  } else {
31
28
  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))) {
29
+ if (originHeader && allowedOrigins.some((p) => _chunkULICUDDHcjs.matchesOriginPattern.call(void 0, originHeader, p))) {
33
30
  allowOrigin = originHeader;
34
31
  }
35
32
  }
@@ -35,14 +35,17 @@ function denormalizeHeaders(headers) {
35
35
  }
36
36
  function parseQuerystring(qs) {
37
37
  if (!qs) return {};
38
- return Object.fromEntries(
39
- qs.split("&").map((p) => {
40
- const parts = p.split("=");
41
- const k = parts[0];
42
- const v = parts[1] || "";
43
- return [k, { value: v }];
44
- })
45
- );
38
+ const result = {};
39
+ const pairs = qs.split("&");
40
+ for (let i = 0; i < pairs.length; i++) {
41
+ const eq = pairs[i].indexOf("=");
42
+ if (eq === -1) {
43
+ result[pairs[i]] = { value: "" };
44
+ } else {
45
+ result[pairs[i].slice(0, eq)] = { value: pairs[i].slice(eq + 1) };
46
+ }
47
+ }
48
+ return result;
46
49
  }
47
50
  function serializeQuerystring(qs) {
48
51
  const qsEntries = Object.entries(qs);
@@ -1,12 +1,10 @@
1
+ import {
2
+ matchesOriginPattern
3
+ } from "./chunk-EEZ7NUJG.js";
4
+
1
5
  // src/behaviors/set-cors-headers.ts
2
6
  var ORIGIN_WILDCARD = "*";
3
7
  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
8
  function setCorsHeaders(options) {
11
9
  const allowedOrigins = options.allowedOrigins;
12
10
  return (request, response) => {
@@ -1,12 +1,12 @@
1
+ import {
2
+ pathMatches
3
+ } from "./chunk-Y7TIDVVC.js";
1
4
  import {
2
5
  userAgentMatches
3
- } from "./chunk-S2AAATFN.js";
6
+ } from "./chunk-VQGBRWJK.js";
4
7
  import {
5
8
  ipCidr
6
- } from "./chunk-KW5YBTSD.js";
7
- import {
8
- pathMatches
9
- } from "./chunk-LO2BO3RU.js";
9
+ } from "./chunk-YHTUV2SA.js";
10
10
  import {
11
11
  redirect
12
12
  } from "./chunk-DSSFFJWL.js";
@@ -1,12 +1,10 @@
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
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; }
2
+
3
+ var _chunkULICUDDHcjs = require('./chunk-ULICUDDH.cjs');
4
+
5
+ // src/behaviors/set-cors-headers.ts
2
6
  var ORIGIN_WILDCARD = "*";
3
7
  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
8
  function setCorsHeaders(options) {
11
9
  const allowedOrigins = options.allowedOrigins;
12
10
  return (request, response) => {
@@ -17,7 +15,7 @@ function setCorsHeaders(options) {
17
15
  allowOrigin = _optionalChain([request, 'access', _ => _.headers, 'access', _2 => _2["origin"], 'optionalAccess', _3 => _3.value]);
18
16
  } else {
19
17
  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))) {
18
+ if (originHeader && allowedOrigins.some((p) => _chunkULICUDDHcjs.matchesOriginPattern.call(void 0, originHeader, p))) {
21
19
  allowOrigin = originHeader;
22
20
  }
23
21
  }
@@ -1,12 +1,12 @@
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 _chunkMVGYPBYBcjs = require('./chunk-MVGYPBYB.cjs');
3
+ var _chunkZEFLAOTLcjs = require('./chunk-ZEFLAOTL.cjs');
4
4
 
5
5
 
6
- var _chunkD47P7HVZcjs = require('./chunk-D47P7HVZ.cjs');
6
+ var _chunkLVOM5GJ6cjs = require('./chunk-LVOM5GJ6.cjs');
7
7
 
8
8
 
9
- var _chunkCF5PWWTFcjs = require('./chunk-CF5PWWTF.cjs');
9
+ var _chunkMK4QBCD5cjs = require('./chunk-MK4QBCD5.cjs');
10
10
 
11
11
 
12
12
  var _chunkWWSRNCUPcjs = require('./chunk-WWSRNCUP.cjs');
@@ -20,9 +20,9 @@ var _chunkWKYMSRCDcjs = require('./chunk-WKYMSRCD.cjs');
20
20
  function whitelist(options) {
21
21
  const userAgents = _nullishCoalesce(options.userAgents, () => ( []));
22
22
  const bypassPaths = _nullishCoalesce(options.bypassPaths, () => ( []));
23
- const criteria = [_chunkWKYMSRCDcjs.not.call(void 0, _chunkD47P7HVZcjs.ipCidr.call(void 0, options.cidrs)), _chunkWKYMSRCDcjs.not.call(void 0, _chunkMVGYPBYBcjs.userAgentMatches.call(void 0, userAgents))];
23
+ const criteria = [_chunkWKYMSRCDcjs.not.call(void 0, _chunkMK4QBCD5cjs.ipCidr.call(void 0, options.cidrs)), _chunkWKYMSRCDcjs.not.call(void 0, _chunkLVOM5GJ6cjs.userAgentMatches.call(void 0, userAgents))];
24
24
  if (bypassPaths.length > 0) {
25
- criteria.push(_chunkWKYMSRCDcjs.not.call(void 0, _chunkCF5PWWTFcjs.pathMatches.call(void 0, bypassPaths)));
25
+ criteria.push(_chunkWKYMSRCDcjs.not.call(void 0, _chunkZEFLAOTLcjs.pathMatches.call(void 0, bypassPaths)));
26
26
  }
27
27
  return _chunkWKYMSRCDcjs.rule.call(void 0, _chunkWKYMSRCDcjs.all.call(void 0, criteria), _chunkWWSRNCUPcjs.redirect.call(void 0, 302, options.redirectUrl));
28
28
  }
@@ -1,13 +1,13 @@
1
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; }
2
2
 
3
- var _chunkIBXAK2A4cjs = require('./chunk-IBXAK2A4.cjs');
3
+ var _chunkULICUDDHcjs = require('./chunk-ULICUDDH.cjs');
4
4
 
5
5
  // src/criteria/user-agent-matches.ts
6
6
  function userAgentMatches(patterns) {
7
7
  return (req) => {
8
8
  const ua = _optionalChain([req, 'access', _ => _.headers, 'access', _2 => _2["user-agent"], 'optionalAccess', _3 => _3.value]);
9
9
  if (!ua) return false;
10
- return _chunkIBXAK2A4cjs.matchesAnyWildcard.call(void 0, ua, patterns);
10
+ return _chunkULICUDDHcjs.matchesAnyWildcard.call(void 0, ua, patterns);
11
11
  };
12
12
  }
13
13
 
@@ -1,10 +1,10 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
2
 
3
- var _chunkYVUR35RNcjs = require('./chunk-YVUR35RN.cjs');
3
+ var _chunkWZKRNMF2cjs = require('./chunk-WZKRNMF2.cjs');
4
4
 
5
5
  // src/criteria/ip-cidr.ts
6
6
  function ipCidr(cidrs) {
7
- return (req) => _chunkYVUR35RNcjs.matchesAnyCidr.call(void 0, req.clientIp, cidrs);
7
+ return (req) => _chunkWZKRNMF2cjs.matchesAnyCidr.call(void 0, req.clientIp, cidrs);
8
8
  }
9
9
 
10
10
 
@@ -0,0 +1,71 @@
1
+ // src/shared/cidr.ts
2
+ function ipToInt(ip) {
3
+ return ip.split(".").reduce((acc, octet) => (acc << 8) + parseInt(octet, 10) >>> 0, 0);
4
+ }
5
+ function isIPv6(ip) {
6
+ return ip.indexOf(":") !== -1;
7
+ }
8
+ function expandIPv6Groups(ip) {
9
+ if (ip.indexOf(".") !== -1) {
10
+ const lastColon = ip.lastIndexOf(":");
11
+ const ipv4Part = ip.slice(lastColon + 1);
12
+ const octs = ipv4Part.split(".");
13
+ if (octs.length !== 4) return null;
14
+ const hi = (parseInt(octs[0], 10) << 8 | parseInt(octs[1], 10)) & 65535;
15
+ const lo = (parseInt(octs[2], 10) << 8 | parseInt(octs[3], 10)) & 65535;
16
+ ip = ip.slice(0, lastColon + 1) + hi.toString(16) + ":" + lo.toString(16);
17
+ }
18
+ const halves = ip.split("::");
19
+ if (halves.length > 2) return null;
20
+ const left = halves[0] ? halves[0].split(":") : [];
21
+ const right = halves.length === 2 && halves[1] ? halves[1].split(":") : [];
22
+ if (halves.length === 1 && left.length !== 8) return null;
23
+ const fill = 8 - left.length - right.length;
24
+ if (fill < 0) return null;
25
+ const groups = [];
26
+ for (let i = 0; i < left.length; i++) {
27
+ groups.push(parseInt(left[i] || "0", 16) & 65535);
28
+ }
29
+ for (let j = 0; j < fill; j++) {
30
+ groups.push(0);
31
+ }
32
+ for (let k = 0; k < right.length; k++) {
33
+ groups.push(parseInt(right[k] || "0", 16) & 65535);
34
+ }
35
+ return groups.length === 8 ? groups : null;
36
+ }
37
+ function inCidrIPv6(ip, cidr) {
38
+ const slashIdx = cidr.indexOf("/");
39
+ const range = slashIdx === -1 ? cidr : cidr.slice(0, slashIdx);
40
+ const prefixLen = slashIdx === -1 ? 128 : parseInt(cidr.slice(slashIdx + 1), 10);
41
+ const ipGroups = expandIPv6Groups(ip);
42
+ const rangeGroups = expandIPv6Groups(range);
43
+ if (!ipGroups || !rangeGroups) return false;
44
+ const fullGroups = Math.floor(prefixLen / 16);
45
+ const remainBits = prefixLen % 16;
46
+ for (let i = 0; i < fullGroups; i++) {
47
+ if (ipGroups[i] !== rangeGroups[i]) return false;
48
+ }
49
+ if (remainBits > 0 && fullGroups < 8) {
50
+ const mask = ~0 << 16 - remainBits & 65535;
51
+ if ((ipGroups[fullGroups] & mask) !== (rangeGroups[fullGroups] & mask)) return false;
52
+ }
53
+ return true;
54
+ }
55
+ function inCidr(ip, cidr) {
56
+ if (isIPv6(ip) || isIPv6(cidr)) return inCidrIPv6(ip, cidr);
57
+ const parts = cidr.split("/");
58
+ const range = parts[0];
59
+ const bits = parts[1] || "32";
60
+ const mask = bits === "0" ? 0 : ~0 << 32 - parseInt(bits, 10) >>> 0;
61
+ return (ipToInt(ip) & mask) === (ipToInt(range) & mask);
62
+ }
63
+ function matchesAnyCidr(ip, cidrs) {
64
+ return cidrs.some((cidr) => inCidr(ip, cidr));
65
+ }
66
+
67
+ export {
68
+ ipToInt,
69
+ inCidr,
70
+ matchesAnyCidr
71
+ };
@@ -1,5 +1,6 @@
1
1
  // src/behaviors/rewrite-uri.ts
2
2
  function rewriteUri(mode, target, match) {
3
+ const re = mode === "regex-replace" && match !== void 0 ? new RegExp(match, "g") : null;
3
4
  return (request) => {
4
5
  let uri = request.uri;
5
6
  switch (mode) {
@@ -15,8 +16,9 @@ function rewriteUri(mode, target, match) {
15
16
  }
16
17
  break;
17
18
  case "regex-replace":
18
- if (match !== void 0) {
19
- uri = uri.replace(new RegExp(match, "g"), target);
19
+ if (re) {
20
+ re.lastIndex = 0;
21
+ uri = uri.replace(re, target);
20
22
  }
21
23
  break;
22
24
  }
@@ -1,5 +1,6 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true});// src/shared/wildcard.ts
2
2
  var regexCache = /* @__PURE__ */ Object.create(null);
3
+ var originPatternCache = /* @__PURE__ */ Object.create(null);
3
4
  function wildcardToRegex(pattern) {
4
5
  if (!(pattern in regexCache)) {
5
6
  const escaped = pattern.replace(/[.+^${}()|[\]\\]/g, "\\$&").replace(/\*/g, ".*").replace(/\?/g, ".");
@@ -13,9 +14,19 @@ function matchesWildcard(str, pattern) {
13
14
  function matchesAnyWildcard(str, patterns) {
14
15
  return patterns.some((p) => matchesWildcard(str, p));
15
16
  }
17
+ function matchesOriginPattern(origin, pattern) {
18
+ if (pattern === "*") return true;
19
+ if (!pattern.includes("*")) return origin === pattern;
20
+ if (!(pattern in originPatternCache)) {
21
+ const escaped = pattern.replace(/[.+^${}()|[\]\\]/g, "\\$&").replace(/\*/g, ".*");
22
+ originPatternCache[pattern] = new RegExp(`^${escaped}$`);
23
+ }
24
+ return originPatternCache[pattern].test(origin);
25
+ }
26
+
16
27
 
17
28
 
18
29
 
19
30
 
20
31
 
21
- exports.wildcardToRegex = wildcardToRegex; exports.matchesWildcard = matchesWildcard; exports.matchesAnyWildcard = matchesAnyWildcard;
32
+ exports.wildcardToRegex = wildcardToRegex; exports.matchesWildcard = matchesWildcard; exports.matchesAnyWildcard = matchesAnyWildcard; exports.matchesOriginPattern = matchesOriginPattern;
@@ -35,14 +35,17 @@ function denormalizeHeaders(headers) {
35
35
  }
36
36
  function parseQuerystring(qs) {
37
37
  if (!qs) return {};
38
- return Object.fromEntries(
39
- qs.split("&").map((p) => {
40
- const parts = p.split("=");
41
- const k = parts[0];
42
- const v = parts[1] || "";
43
- return [k, { value: v }];
44
- })
45
- );
38
+ const result = {};
39
+ const pairs = qs.split("&");
40
+ for (let i = 0; i < pairs.length; i++) {
41
+ const eq = pairs[i].indexOf("=");
42
+ if (eq === -1) {
43
+ result[pairs[i]] = { value: "" };
44
+ } else {
45
+ result[pairs[i].slice(0, eq)] = { value: pairs[i].slice(eq + 1) };
46
+ }
47
+ }
48
+ return result;
46
49
  }
47
50
  function serializeQuerystring(qs) {
48
51
  const qsEntries = Object.entries(qs);
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  matchesAnyWildcard
3
- } from "./chunk-2DE6WPPL.js";
3
+ } from "./chunk-EEZ7NUJG.js";
4
4
 
5
5
  // src/criteria/user-agent-matches.ts
6
6
  function userAgentMatches(patterns) {