@rayselfs/cf-rule-engine 1.8.2 → 1.9.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.
Files changed (73) 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 +5 -0
  5. package/dist/adapters/viewer-request-async.d.ts +5 -0
  6. package/dist/adapters/viewer-request-async.js +26 -0
  7. package/dist/behaviors/index.cjs +12 -11
  8. package/dist/behaviors/index.js +11 -10
  9. package/dist/behaviors/kvs.cjs +24 -0
  10. package/dist/behaviors/kvs.d.cts +6 -0
  11. package/dist/behaviors/kvs.d.ts +6 -0
  12. package/dist/behaviors/kvs.js +24 -0
  13. package/dist/behaviors/rewrite-uri.cjs +2 -2
  14. package/dist/behaviors/rewrite-uri.js +1 -1
  15. package/dist/behaviors/set-cors-headers.cjs +3 -2
  16. package/dist/behaviors/set-cors-headers.d.cts +0 -27
  17. package/dist/behaviors/set-cors-headers.d.ts +0 -27
  18. package/dist/behaviors/set-cors-headers.js +2 -1
  19. package/dist/{chunk-ORW3KDO5.js → chunk-7EA7GFWX.js} +4 -7
  20. package/dist/{chunk-MRPTC74I.cjs → chunk-BSH5JZBL.cjs} +4 -2
  21. package/dist/{chunk-2DE6WPPL.js → chunk-EEZ7NUJG.js} +12 -1
  22. package/dist/{chunk-PBR6AREG.cjs → chunk-EMDI676G.cjs} +7 -10
  23. package/dist/{chunk-3BBLG4IX.cjs → chunk-G4JEAL6L.cjs} +11 -8
  24. package/dist/{chunk-CQA2DCVF.js → chunk-H3RK4USR.js} +4 -6
  25. package/dist/{chunk-RL7ZETZR.js → chunk-IHDSTTO2.js} +5 -5
  26. package/dist/{chunk-AEZDDJEW.cjs → chunk-IHVOAORH.cjs} +6 -8
  27. package/dist/{chunk-T5EXFHVA.cjs → chunk-ISXKMJCN.cjs} +5 -5
  28. package/dist/{chunk-MVGYPBYB.cjs → chunk-LVOM5GJ6.cjs} +2 -2
  29. package/dist/{chunk-D47P7HVZ.cjs → chunk-MK4QBCD5.cjs} +2 -2
  30. package/dist/chunk-NWRGD3AH.js +71 -0
  31. package/dist/{chunk-FTP7NLKX.js → chunk-QVY6REMD.js} +4 -2
  32. package/dist/{chunk-IBXAK2A4.cjs → chunk-ULICUDDH.cjs} +12 -1
  33. package/dist/{chunk-WEBU4R5C.js → chunk-ULR7EP5D.js} +11 -8
  34. package/dist/{chunk-S2AAATFN.js → chunk-VQGBRWJK.js} +1 -1
  35. package/dist/chunk-WZKRNMF2.cjs +71 -0
  36. package/dist/{chunk-LO2BO3RU.js → chunk-Y7TIDVVC.js} +1 -1
  37. package/dist/{chunk-KW5YBTSD.js → chunk-YHTUV2SA.js} +1 -1
  38. package/dist/{chunk-CF5PWWTF.cjs → chunk-ZEFLAOTL.cjs} +2 -2
  39. package/dist/criteria/index.cjs +14 -14
  40. package/dist/criteria/index.js +19 -19
  41. package/dist/criteria/ip-cidr.cjs +3 -3
  42. package/dist/criteria/ip-cidr.js +2 -2
  43. package/dist/criteria/kvs.cjs +14 -0
  44. package/dist/criteria/kvs.d.cts +6 -0
  45. package/dist/criteria/kvs.d.ts +6 -0
  46. package/dist/criteria/kvs.js +14 -0
  47. package/dist/criteria/path-matches.cjs +3 -3
  48. package/dist/criteria/path-matches.js +2 -2
  49. package/dist/criteria/user-agent-matches.cjs +3 -3
  50. package/dist/criteria/user-agent-matches.js +2 -2
  51. package/dist/helpers/index.cjs +11 -11
  52. package/dist/helpers/index.js +11 -11
  53. package/dist/helpers/preflight-request.cjs +4 -3
  54. package/dist/helpers/preflight-request.js +3 -2
  55. package/dist/helpers/whitelist.cjs +7 -7
  56. package/dist/helpers/whitelist.js +6 -6
  57. package/dist/index.cjs +2 -2
  58. package/dist/index.js +1 -1
  59. package/dist/shared/cidr.cjs +2 -2
  60. package/dist/shared/cidr.d.cts +2 -2
  61. package/dist/shared/cidr.d.ts +2 -2
  62. package/dist/shared/cidr.js +1 -1
  63. package/dist/shared/kvs.cjs +1 -0
  64. package/dist/shared/kvs.d.cts +5 -0
  65. package/dist/shared/kvs.d.ts +5 -0
  66. package/dist/shared/kvs.js +0 -0
  67. package/dist/shared/wildcard.cjs +4 -2
  68. package/dist/shared/wildcard.d.cts +10 -1
  69. package/dist/shared/wildcard.d.ts +10 -1
  70. package/dist/shared/wildcard.js +3 -1
  71. package/package.json +1 -1
  72. package/dist/chunk-LNQPYKGG.js +0 -20
  73. package/dist/chunk-YVUR35RN.cjs +0 -20
@@ -1,10 +1,10 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
2
 
3
3
 
4
- var _chunk3BBLG4IXcjs = require('../chunk-3BBLG4IX.cjs');
4
+ var _chunkG4JEAL6Lcjs = require('../chunk-G4JEAL6L.cjs');
5
5
  require('../chunk-WKYMSRCD.cjs');
6
6
  require('../chunk-75ZPJI57.cjs');
7
7
 
8
8
 
9
9
 
10
- exports.defineViewerRequest = _chunk3BBLG4IXcjs.defineViewerRequest; exports.defineViewerResponse = _chunk3BBLG4IXcjs.defineViewerResponse;
10
+ exports.defineViewerRequest = _chunkG4JEAL6Lcjs.defineViewerRequest; exports.defineViewerResponse = _chunkG4JEAL6Lcjs.defineViewerResponse;
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  defineViewerRequest,
3
3
  defineViewerResponse
4
- } from "../chunk-WEBU4R5C.js";
4
+ } from "../chunk-ULR7EP5D.js";
5
5
  import "../chunk-Q4NP4C3B.js";
6
6
  import "../chunk-MLKGABMK.js";
7
7
  export {
@@ -0,0 +1,26 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }
2
+
3
+ var _chunkWKYMSRCDcjs = require('../chunk-WKYMSRCD.cjs');
4
+
5
+
6
+
7
+
8
+ var _chunk6NFAPLQ7cjs = require('../chunk-6NFAPLQ7.cjs');
9
+ require('../chunk-75ZPJI57.cjs');
10
+
11
+ // src/adapters/viewer-request-async.ts
12
+ function defineViewerRequestAsync(setup) {
13
+ return async (event) => {
14
+ const ev = event;
15
+ const evReq = ev.request;
16
+ const originalCookies = _nullishCoalesce(evReq.cookies, () => ( {}));
17
+ const req = _chunk6NFAPLQ7cjs.normalizeRequest.call(void 0, event);
18
+ const rules = await setup(event);
19
+ const result = _chunkWKYMSRCDcjs.runRules.call(void 0, rules, req);
20
+ if (result.action === "respond") return _chunk6NFAPLQ7cjs.denormalizeResponse.call(void 0, result.response);
21
+ return _chunk6NFAPLQ7cjs.denormalizeRequest.call(void 0, result.request, originalCookies);
22
+ };
23
+ }
24
+
25
+
26
+ exports.defineViewerRequestAsync = defineViewerRequestAsync;
@@ -0,0 +1,5 @@
1
+ import { Rule } from '../core/types.cjs';
2
+
3
+ declare function defineViewerRequestAsync(setup: (event: unknown) => Promise<Rule[]>): (event: unknown) => Promise<unknown>;
4
+
5
+ export { defineViewerRequestAsync };
@@ -0,0 +1,5 @@
1
+ import { Rule } from '../core/types.js';
2
+
3
+ declare function defineViewerRequestAsync(setup: (event: unknown) => Promise<Rule[]>): (event: unknown) => Promise<unknown>;
4
+
5
+ export { defineViewerRequestAsync };
@@ -0,0 +1,26 @@
1
+ import {
2
+ runRules
3
+ } from "../chunk-Q4NP4C3B.js";
4
+ import {
5
+ denormalizeRequest,
6
+ denormalizeResponse,
7
+ normalizeRequest
8
+ } from "../chunk-CQ7YZ3AR.js";
9
+ import "../chunk-MLKGABMK.js";
10
+
11
+ // src/adapters/viewer-request-async.ts
12
+ function defineViewerRequestAsync(setup) {
13
+ return async (event) => {
14
+ const ev = event;
15
+ const evReq = ev.request;
16
+ const originalCookies = evReq.cookies ?? {};
17
+ const req = normalizeRequest(event);
18
+ const rules = await setup(event);
19
+ const result = runRules(rules, req);
20
+ if (result.action === "respond") return denormalizeResponse(result.response);
21
+ return denormalizeRequest(result.request, originalCookies);
22
+ };
23
+ }
24
+ export {
25
+ defineViewerRequestAsync
26
+ };
@@ -1,5 +1,12 @@
1
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; }
2
2
 
3
+ var _chunkIHVOAORHcjs = require('../chunk-IHVOAORH.cjs');
4
+ require('../chunk-ULICUDDH.cjs');
5
+
6
+
7
+ var _chunkZXS23HXAcjs = require('../chunk-ZXS23HXA.cjs');
8
+
9
+
3
10
  var _chunkPPUHEL4Hcjs = require('../chunk-PPUHEL4H.cjs');
4
11
 
5
12
 
@@ -12,6 +19,9 @@ var _chunk3UXNXJ6Ncjs = require('../chunk-3UXNXJ6N.cjs');
12
19
  var _chunkMSES76XKcjs = require('../chunk-MSES76XK.cjs');
13
20
 
14
21
 
22
+ var _chunkLTLBEBKLcjs = require('../chunk-LTLBEBKL.cjs');
23
+
24
+
15
25
  var _chunkKXC6ES3Bcjs = require('../chunk-KXC6ES3B.cjs');
16
26
 
17
27
 
@@ -21,25 +31,16 @@ var _chunkWWSRNCUPcjs = require('../chunk-WWSRNCUP.cjs');
21
31
  var _chunkSGEBNQR2cjs = require('../chunk-SGEBNQR2.cjs');
22
32
 
23
33
 
24
- var _chunkMRPTC74Icjs = require('../chunk-MRPTC74I.cjs');
34
+ var _chunkBSH5JZBLcjs = require('../chunk-BSH5JZBL.cjs');
25
35
 
26
36
 
27
37
  var _chunkCV234DQTcjs = require('../chunk-CV234DQT.cjs');
28
38
 
29
39
 
30
- var _chunkAEZDDJEWcjs = require('../chunk-AEZDDJEW.cjs');
31
-
32
-
33
- var _chunkZXS23HXAcjs = require('../chunk-ZXS23HXA.cjs');
34
-
35
-
36
40
  var _chunkOSGZTNTScjs = require('../chunk-OSGZTNTS.cjs');
37
41
 
38
42
 
39
43
  var _chunkJU5WX5RUcjs = require('../chunk-JU5WX5RU.cjs');
40
-
41
-
42
- var _chunkLTLBEBKLcjs = require('../chunk-LTLBEBKL.cjs');
43
44
  require('../chunk-75ZPJI57.cjs');
44
45
 
45
46
  // src/behaviors/verify-token.ts
@@ -123,4 +124,4 @@ function verifyToken(options) {
123
124
 
124
125
 
125
126
 
126
- exports.constructResponse = _chunkOSGZTNTScjs.constructResponse; exports.copyHeader = _chunkJU5WX5RUcjs.copyHeader; exports.directoryIndex = _chunkLTLBEBKLcjs.directoryIndex; exports.imageOptimize = _chunkKXC6ES3Bcjs.imageOptimize; exports.redirect = _chunkWWSRNCUPcjs.redirect; exports.removeResponseHeaders = _chunkSGEBNQR2cjs.removeResponseHeaders; exports.rewriteUri = _chunkMRPTC74Icjs.rewriteUri; exports.setCacheControl = _chunkCV234DQTcjs.setCacheControl; exports.setCorsHeaders = _chunkAEZDDJEWcjs.setCorsHeaders; exports.setCsp = _chunkZXS23HXAcjs.setCsp; exports.setRequestHeader = _chunkPPUHEL4Hcjs.setRequestHeader; exports.setResponseHeader = _chunkB4WEJSEZcjs.setResponseHeader; exports.setSecurityHeaders = _chunk3UXNXJ6Ncjs.setSecurityHeaders; exports.stripQueryParams = _chunkMSES76XKcjs.stripQueryParams; exports.verifyToken = verifyToken;
127
+ exports.constructResponse = _chunkOSGZTNTScjs.constructResponse; exports.copyHeader = _chunkJU5WX5RUcjs.copyHeader; exports.directoryIndex = _chunkLTLBEBKLcjs.directoryIndex; exports.imageOptimize = _chunkKXC6ES3Bcjs.imageOptimize; exports.redirect = _chunkWWSRNCUPcjs.redirect; exports.removeResponseHeaders = _chunkSGEBNQR2cjs.removeResponseHeaders; exports.rewriteUri = _chunkBSH5JZBLcjs.rewriteUri; exports.setCacheControl = _chunkCV234DQTcjs.setCacheControl; exports.setCorsHeaders = _chunkIHVOAORHcjs.setCorsHeaders; exports.setCsp = _chunkZXS23HXAcjs.setCsp; exports.setRequestHeader = _chunkPPUHEL4Hcjs.setRequestHeader; exports.setResponseHeader = _chunkB4WEJSEZcjs.setResponseHeader; exports.setSecurityHeaders = _chunk3UXNXJ6Ncjs.setSecurityHeaders; exports.stripQueryParams = _chunkMSES76XKcjs.stripQueryParams; exports.verifyToken = verifyToken;
@@ -1,3 +1,10 @@
1
+ import {
2
+ setCorsHeaders
3
+ } from "../chunk-H3RK4USR.js";
4
+ import "../chunk-EEZ7NUJG.js";
5
+ import {
6
+ setCsp
7
+ } from "../chunk-XUI4Y22M.js";
1
8
  import {
2
9
  setRequestHeader
3
10
  } from "../chunk-M5KUQBDW.js";
@@ -10,6 +17,9 @@ import {
10
17
  import {
11
18
  stripQueryParams
12
19
  } from "../chunk-XPQG5IML.js";
20
+ import {
21
+ directoryIndex
22
+ } from "../chunk-R7WXS7BI.js";
13
23
  import {
14
24
  imageOptimize
15
25
  } from "../chunk-LQRLWDQQ.js";
@@ -21,25 +31,16 @@ import {
21
31
  } from "../chunk-BUAIBB3N.js";
22
32
  import {
23
33
  rewriteUri
24
- } from "../chunk-FTP7NLKX.js";
34
+ } from "../chunk-QVY6REMD.js";
25
35
  import {
26
36
  setCacheControl
27
37
  } from "../chunk-ZTMSH34E.js";
28
- import {
29
- setCorsHeaders
30
- } from "../chunk-CQA2DCVF.js";
31
- import {
32
- setCsp
33
- } from "../chunk-XUI4Y22M.js";
34
38
  import {
35
39
  constructResponse
36
40
  } from "../chunk-6DBZBV2M.js";
37
41
  import {
38
42
  copyHeader
39
43
  } from "../chunk-BDNPQ7AU.js";
40
- import {
41
- directoryIndex
42
- } from "../chunk-R7WXS7BI.js";
43
44
  import "../chunk-MLKGABMK.js";
44
45
 
45
46
  // src/behaviors/verify-token.ts
@@ -0,0 +1,24 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});require('../chunk-75ZPJI57.cjs');
2
+
3
+ // src/behaviors/kvs.ts
4
+ async function kvsRedirect(handle, key, statusCode) {
5
+ const raw = await handle.get(key);
6
+ const map = raw ? JSON.parse(raw) : {};
7
+ const code = statusCode !== void 0 ? statusCode : 301;
8
+ const desc = code === 302 ? "Found" : "Moved Permanently";
9
+ return (request) => {
10
+ const dest = map[request.uri];
11
+ if (!dest) return { action: "continue", request };
12
+ return {
13
+ action: "respond",
14
+ response: {
15
+ statusCode: code,
16
+ statusDescription: desc,
17
+ headers: { location: { value: dest } }
18
+ }
19
+ };
20
+ };
21
+ }
22
+
23
+
24
+ exports.kvsRedirect = kvsRedirect;
@@ -0,0 +1,6 @@
1
+ import { BehaviorFn } from '../core/types.cjs';
2
+ import { KvsHandle } from '../shared/kvs.cjs';
3
+
4
+ declare function kvsRedirect(handle: KvsHandle, key: string, statusCode?: number): Promise<BehaviorFn>;
5
+
6
+ export { kvsRedirect };
@@ -0,0 +1,6 @@
1
+ import { BehaviorFn } from '../core/types.js';
2
+ import { KvsHandle } from '../shared/kvs.js';
3
+
4
+ declare function kvsRedirect(handle: KvsHandle, key: string, statusCode?: number): Promise<BehaviorFn>;
5
+
6
+ export { kvsRedirect };
@@ -0,0 +1,24 @@
1
+ import "../chunk-MLKGABMK.js";
2
+
3
+ // src/behaviors/kvs.ts
4
+ async function kvsRedirect(handle, key, statusCode) {
5
+ const raw = await handle.get(key);
6
+ const map = raw ? JSON.parse(raw) : {};
7
+ const code = statusCode !== void 0 ? statusCode : 301;
8
+ const desc = code === 302 ? "Found" : "Moved Permanently";
9
+ return (request) => {
10
+ const dest = map[request.uri];
11
+ if (!dest) return { action: "continue", request };
12
+ return {
13
+ action: "respond",
14
+ response: {
15
+ statusCode: code,
16
+ statusDescription: desc,
17
+ headers: { location: { value: dest } }
18
+ }
19
+ };
20
+ };
21
+ }
22
+ export {
23
+ kvsRedirect
24
+ };
@@ -1,7 +1,7 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
2
 
3
- var _chunkMRPTC74Icjs = require('../chunk-MRPTC74I.cjs');
3
+ var _chunkBSH5JZBLcjs = require('../chunk-BSH5JZBL.cjs');
4
4
  require('../chunk-75ZPJI57.cjs');
5
5
 
6
6
 
7
- exports.rewriteUri = _chunkMRPTC74Icjs.rewriteUri;
7
+ exports.rewriteUri = _chunkBSH5JZBLcjs.rewriteUri;
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  rewriteUri
3
- } from "../chunk-FTP7NLKX.js";
3
+ } from "../chunk-QVY6REMD.js";
4
4
  import "../chunk-MLKGABMK.js";
5
5
  export {
6
6
  rewriteUri
@@ -2,10 +2,11 @@
2
2
 
3
3
 
4
4
 
5
- var _chunkAEZDDJEWcjs = require('../chunk-AEZDDJEW.cjs');
5
+ var _chunkIHVOAORHcjs = require('../chunk-IHVOAORH.cjs');
6
+ require('../chunk-ULICUDDH.cjs');
6
7
  require('../chunk-75ZPJI57.cjs');
7
8
 
8
9
 
9
10
 
10
11
 
11
- exports.ORIGIN_ECHO = _chunkAEZDDJEWcjs.ORIGIN_ECHO; exports.ORIGIN_WILDCARD = _chunkAEZDDJEWcjs.ORIGIN_WILDCARD; exports.setCorsHeaders = _chunkAEZDDJEWcjs.setCorsHeaders;
12
+ exports.ORIGIN_ECHO = _chunkIHVOAORHcjs.ORIGIN_ECHO; exports.ORIGIN_WILDCARD = _chunkIHVOAORHcjs.ORIGIN_WILDCARD; exports.setCorsHeaders = _chunkIHVOAORHcjs.setCorsHeaders;
@@ -55,33 +55,6 @@ interface CorsOptions {
55
55
  */
56
56
  maxAge?: number;
57
57
  }
58
- /**
59
- * Sets CORS response headers with configurable origin policy.
60
- *
61
- * @param options - CORS configuration. `allowedOrigins` is required.
62
- * @returns A `ResponseBehaviorFn` to use directly in `defineViewerResponse` or wrapped in a `ResponseRule`.
63
- *
64
- * @example
65
- * ```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
- * ])
83
- * ```
84
- */
85
58
  declare function setCorsHeaders(options: CorsOptions): ResponseBehaviorFn;
86
59
 
87
60
  export { type CorsOptions, type Methods, ORIGIN_ECHO, ORIGIN_WILDCARD, type Origin, type OriginEcho, type OriginPolicy, type OriginWildcard, setCorsHeaders };
@@ -55,33 +55,6 @@ interface CorsOptions {
55
55
  */
56
56
  maxAge?: number;
57
57
  }
58
- /**
59
- * Sets CORS response headers with configurable origin policy.
60
- *
61
- * @param options - CORS configuration. `allowedOrigins` is required.
62
- * @returns A `ResponseBehaviorFn` to use directly in `defineViewerResponse` or wrapped in a `ResponseRule`.
63
- *
64
- * @example
65
- * ```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
- * ])
83
- * ```
84
- */
85
58
  declare function setCorsHeaders(options: CorsOptions): ResponseBehaviorFn;
86
59
 
87
60
  export { type CorsOptions, type Methods, ORIGIN_ECHO, ORIGIN_WILDCARD, type Origin, type OriginEcho, type OriginPolicy, type OriginWildcard, setCorsHeaders };
@@ -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,
@@ -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