@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.
Files changed (228) hide show
  1. package/README.md +276 -0
  2. package/dist/adapters/cf-function.cjs +10 -0
  3. package/dist/adapters/cf-function.d.cts +2 -0
  4. package/dist/adapters/cf-function.d.ts +2 -0
  5. package/dist/adapters/cf-function.js +10 -0
  6. package/dist/adapters/lambda-edge.cjs +10 -0
  7. package/dist/adapters/lambda-edge.d.cts +2 -0
  8. package/dist/adapters/lambda-edge.d.ts +2 -0
  9. package/dist/adapters/lambda-edge.js +10 -0
  10. package/dist/behaviors/construct-response.cjs +7 -0
  11. package/dist/behaviors/construct-response.d.cts +62 -0
  12. package/dist/behaviors/construct-response.d.ts +62 -0
  13. package/dist/behaviors/construct-response.js +7 -0
  14. package/dist/behaviors/copy-header.cjs +7 -0
  15. package/dist/behaviors/copy-header.d.cts +27 -0
  16. package/dist/behaviors/copy-header.d.ts +27 -0
  17. package/dist/behaviors/copy-header.js +7 -0
  18. package/dist/behaviors/directory-index.cjs +7 -0
  19. package/dist/behaviors/directory-index.d.cts +33 -0
  20. package/dist/behaviors/directory-index.d.ts +33 -0
  21. package/dist/behaviors/directory-index.js +7 -0
  22. package/dist/behaviors/image-optimize.cjs +9 -0
  23. package/dist/behaviors/image-optimize.d.cts +103 -0
  24. package/dist/behaviors/image-optimize.d.ts +103 -0
  25. package/dist/behaviors/image-optimize.js +9 -0
  26. package/dist/behaviors/index.cjs +126 -0
  27. package/dist/behaviors/index.d.cts +46 -0
  28. package/dist/behaviors/index.d.ts +46 -0
  29. package/dist/behaviors/index.js +126 -0
  30. package/dist/behaviors/redirect.cjs +7 -0
  31. package/dist/behaviors/redirect.d.cts +44 -0
  32. package/dist/behaviors/redirect.d.ts +44 -0
  33. package/dist/behaviors/redirect.js +7 -0
  34. package/dist/behaviors/remove-response-headers.cjs +7 -0
  35. package/dist/behaviors/remove-response-headers.d.cts +27 -0
  36. package/dist/behaviors/remove-response-headers.d.ts +27 -0
  37. package/dist/behaviors/remove-response-headers.js +7 -0
  38. package/dist/behaviors/rewrite-uri.cjs +7 -0
  39. package/dist/behaviors/rewrite-uri.d.cts +43 -0
  40. package/dist/behaviors/rewrite-uri.d.ts +43 -0
  41. package/dist/behaviors/rewrite-uri.js +7 -0
  42. package/dist/behaviors/set-cache-control.cjs +7 -0
  43. package/dist/behaviors/set-cache-control.d.cts +29 -0
  44. package/dist/behaviors/set-cache-control.d.ts +29 -0
  45. package/dist/behaviors/set-cache-control.js +7 -0
  46. package/dist/behaviors/set-cors-headers.cjs +7 -0
  47. package/dist/behaviors/set-cors-headers.d.cts +76 -0
  48. package/dist/behaviors/set-cors-headers.d.ts +76 -0
  49. package/dist/behaviors/set-cors-headers.js +7 -0
  50. package/dist/behaviors/set-csp.cjs +7 -0
  51. package/dist/behaviors/set-csp.d.cts +48 -0
  52. package/dist/behaviors/set-csp.d.ts +48 -0
  53. package/dist/behaviors/set-csp.js +7 -0
  54. package/dist/behaviors/set-request-header.cjs +7 -0
  55. package/dist/behaviors/set-request-header.d.cts +26 -0
  56. package/dist/behaviors/set-request-header.d.ts +26 -0
  57. package/dist/behaviors/set-request-header.js +7 -0
  58. package/dist/behaviors/set-response-header.cjs +7 -0
  59. package/dist/behaviors/set-response-header.d.cts +28 -0
  60. package/dist/behaviors/set-response-header.d.ts +28 -0
  61. package/dist/behaviors/set-response-header.js +7 -0
  62. package/dist/behaviors/set-security-headers.cjs +7 -0
  63. package/dist/behaviors/set-security-headers.d.cts +55 -0
  64. package/dist/behaviors/set-security-headers.d.ts +55 -0
  65. package/dist/behaviors/set-security-headers.js +7 -0
  66. package/dist/behaviors/strip-query-params.cjs +7 -0
  67. package/dist/behaviors/strip-query-params.d.cts +25 -0
  68. package/dist/behaviors/strip-query-params.d.ts +25 -0
  69. package/dist/behaviors/strip-query-params.js +7 -0
  70. package/dist/cf-function-D27hUVtN.d.cts +70 -0
  71. package/dist/cf-function-u0PvbW_s.d.ts +70 -0
  72. package/dist/chunk-2DE6WPPL.js +21 -0
  73. package/dist/chunk-32SMWYAF.cjs +13 -0
  74. package/dist/chunk-3BBLG4IX.cjs +132 -0
  75. package/dist/chunk-3PVDUC5M.js +12 -0
  76. package/dist/chunk-5CPBXZ4X.js +12 -0
  77. package/dist/chunk-5PT5X62W.js +98 -0
  78. package/dist/chunk-63WIEBQB.cjs +50 -0
  79. package/dist/chunk-6DBZBV2M.js +42 -0
  80. package/dist/chunk-75ZPJI57.cjs +9 -0
  81. package/dist/chunk-B4WEJSEZ.cjs +14 -0
  82. package/dist/chunk-BDNPQ7AU.js +21 -0
  83. package/dist/chunk-BUAIBB3N.js +14 -0
  84. package/dist/chunk-BZQJYOU2.js +12 -0
  85. package/dist/chunk-C32DL3EP.js +13 -0
  86. package/dist/chunk-CF5PWWTF.cjs +15 -0
  87. package/dist/chunk-CV234DQT.cjs +14 -0
  88. package/dist/chunk-D47P7HVZ.cjs +12 -0
  89. package/dist/chunk-DSSFFJWL.js +38 -0
  90. package/dist/chunk-FTP7NLKX.js +29 -0
  91. package/dist/chunk-G7JGTBTT.cjs +8 -0
  92. package/dist/chunk-GK5JX7OM.cjs +39 -0
  93. package/dist/chunk-H2LO6MQG.js +50 -0
  94. package/dist/chunk-IBXAK2A4.cjs +21 -0
  95. package/dist/chunk-JGJW7D2N.cjs +12 -0
  96. package/dist/chunk-JU5WX5RU.cjs +21 -0
  97. package/dist/chunk-KW5YBTSD.js +12 -0
  98. package/dist/chunk-KZ72PI2A.js +0 -0
  99. package/dist/chunk-L7NBJ4JA.cjs +12 -0
  100. package/dist/chunk-LBJUCJF2.js +14 -0
  101. package/dist/chunk-LNQPYKGG.js +20 -0
  102. package/dist/chunk-LO2BO3RU.js +15 -0
  103. package/dist/chunk-LSCC62CZ.cjs +13 -0
  104. package/dist/chunk-LTLBEBKL.cjs +42 -0
  105. package/dist/chunk-M5KUQBDW.js +17 -0
  106. package/dist/chunk-MEHWLQLR.cjs +1 -0
  107. package/dist/chunk-MLKGABMK.js +9 -0
  108. package/dist/chunk-MRPTC74I.cjs +29 -0
  109. package/dist/chunk-MSES76XK.cjs +14 -0
  110. package/dist/chunk-MVGYPBYB.cjs +16 -0
  111. package/dist/chunk-NPMGSPNL.js +88 -0
  112. package/dist/chunk-OSGZTNTS.cjs +42 -0
  113. package/dist/chunk-OSZWDCTS.cjs +12 -0
  114. package/dist/chunk-OTFDML3K.cjs +11 -0
  115. package/dist/chunk-PPUHEL4H.cjs +17 -0
  116. package/dist/chunk-PY3JMRDG.js +11 -0
  117. package/dist/chunk-Q4NP4C3B.js +47 -0
  118. package/dist/chunk-R7WXS7BI.js +42 -0
  119. package/dist/chunk-RBBKFG5J.js +14 -0
  120. package/dist/chunk-S2AAATFN.js +16 -0
  121. package/dist/chunk-SAXDN5NS.cjs +88 -0
  122. package/dist/chunk-SGEBNQR2.cjs +14 -0
  123. package/dist/chunk-SOBTD7AD.js +39 -0
  124. package/dist/chunk-SRQF5UEJ.js +13 -0
  125. package/dist/chunk-TQLJIT4H.cjs +98 -0
  126. package/dist/chunk-U54FZCOH.cjs +14 -0
  127. package/dist/chunk-UD456E4I.js +8 -0
  128. package/dist/chunk-UI6LKDJI.cjs +19 -0
  129. package/dist/chunk-UKB5JAX4.js +32 -0
  130. package/dist/chunk-VEEOQ7TS.cjs +8 -0
  131. package/dist/chunk-VQCRSBWL.js +19 -0
  132. package/dist/chunk-WEBU4R5C.js +132 -0
  133. package/dist/chunk-WKYMSRCD.cjs +47 -0
  134. package/dist/chunk-WW7YVRAI.cjs +32 -0
  135. package/dist/chunk-WWSRNCUP.cjs +38 -0
  136. package/dist/chunk-XLSZ5RB7.js +8 -0
  137. package/dist/chunk-XPQG5IML.js +14 -0
  138. package/dist/chunk-XUI4Y22M.js +20 -0
  139. package/dist/chunk-YVUR35RN.cjs +20 -0
  140. package/dist/chunk-ZTMSH34E.js +14 -0
  141. package/dist/chunk-ZXS23HXA.cjs +20 -0
  142. package/dist/core/rule.cjs +17 -0
  143. package/dist/core/rule.d.cts +101 -0
  144. package/dist/core/rule.d.ts +101 -0
  145. package/dist/core/rule.js +17 -0
  146. package/dist/core/types.cjs +1 -0
  147. package/dist/core/types.d.cts +53 -0
  148. package/dist/core/types.d.ts +53 -0
  149. package/dist/core/types.js +1 -0
  150. package/dist/criteria/country-is.cjs +7 -0
  151. package/dist/criteria/country-is.d.cts +35 -0
  152. package/dist/criteria/country-is.d.ts +35 -0
  153. package/dist/criteria/country-is.js +7 -0
  154. package/dist/criteria/file-extension.cjs +7 -0
  155. package/dist/criteria/file-extension.d.cts +32 -0
  156. package/dist/criteria/file-extension.d.ts +32 -0
  157. package/dist/criteria/file-extension.js +7 -0
  158. package/dist/criteria/header-contains.cjs +7 -0
  159. package/dist/criteria/header-contains.d.cts +33 -0
  160. package/dist/criteria/header-contains.d.ts +33 -0
  161. package/dist/criteria/header-contains.js +7 -0
  162. package/dist/criteria/header-equals.cjs +7 -0
  163. package/dist/criteria/header-equals.d.cts +31 -0
  164. package/dist/criteria/header-equals.d.ts +31 -0
  165. package/dist/criteria/header-equals.js +7 -0
  166. package/dist/criteria/hostname-is.cjs +7 -0
  167. package/dist/criteria/hostname-is.d.cts +22 -0
  168. package/dist/criteria/hostname-is.d.ts +22 -0
  169. package/dist/criteria/hostname-is.js +7 -0
  170. package/dist/criteria/index.cjs +49 -0
  171. package/dist/criteria/index.d.cts +12 -0
  172. package/dist/criteria/index.d.ts +12 -0
  173. package/dist/criteria/index.js +49 -0
  174. package/dist/criteria/ip-cidr.cjs +8 -0
  175. package/dist/criteria/ip-cidr.d.cts +31 -0
  176. package/dist/criteria/ip-cidr.d.ts +31 -0
  177. package/dist/criteria/ip-cidr.js +8 -0
  178. package/dist/criteria/method-is.cjs +7 -0
  179. package/dist/criteria/method-is.d.cts +29 -0
  180. package/dist/criteria/method-is.d.ts +29 -0
  181. package/dist/criteria/method-is.js +7 -0
  182. package/dist/criteria/path-equals.cjs +7 -0
  183. package/dist/criteria/path-equals.d.cts +22 -0
  184. package/dist/criteria/path-equals.d.ts +22 -0
  185. package/dist/criteria/path-equals.js +7 -0
  186. package/dist/criteria/path-matches.cjs +8 -0
  187. package/dist/criteria/path-matches.d.cts +22 -0
  188. package/dist/criteria/path-matches.d.ts +22 -0
  189. package/dist/criteria/path-matches.js +8 -0
  190. package/dist/criteria/path-prefix.cjs +7 -0
  191. package/dist/criteria/path-prefix.d.cts +21 -0
  192. package/dist/criteria/path-prefix.d.ts +21 -0
  193. package/dist/criteria/path-prefix.js +7 -0
  194. package/dist/criteria/user-agent-matches.cjs +8 -0
  195. package/dist/criteria/user-agent-matches.d.cts +32 -0
  196. package/dist/criteria/user-agent-matches.d.ts +32 -0
  197. package/dist/criteria/user-agent-matches.js +8 -0
  198. package/dist/helpers/index.cjs +42 -0
  199. package/dist/helpers/index.d.cts +34 -0
  200. package/dist/helpers/index.d.ts +34 -0
  201. package/dist/helpers/index.js +42 -0
  202. package/dist/helpers/preflight-request.cjs +8 -0
  203. package/dist/helpers/preflight-request.d.cts +38 -0
  204. package/dist/helpers/preflight-request.d.ts +38 -0
  205. package/dist/helpers/preflight-request.js +8 -0
  206. package/dist/helpers/send-country-code.cjs +8 -0
  207. package/dist/helpers/send-country-code.d.cts +35 -0
  208. package/dist/helpers/send-country-code.d.ts +35 -0
  209. package/dist/helpers/send-country-code.js +8 -0
  210. package/dist/helpers/staging-whitelist.cjs +14 -0
  211. package/dist/helpers/staging-whitelist.d.cts +73 -0
  212. package/dist/helpers/staging-whitelist.d.ts +73 -0
  213. package/dist/helpers/staging-whitelist.js +14 -0
  214. package/dist/index.cjs +26 -0
  215. package/dist/index.d.cts +4 -0
  216. package/dist/index.d.ts +4 -0
  217. package/dist/index.js +26 -0
  218. package/dist/lambda-edge-D15Nf__n.d.ts +66 -0
  219. package/dist/lambda-edge-DxTOa2cg.d.cts +66 -0
  220. package/dist/shared/cidr.cjs +11 -0
  221. package/dist/shared/cidr.d.cts +8 -0
  222. package/dist/shared/cidr.d.ts +8 -0
  223. package/dist/shared/cidr.js +11 -0
  224. package/dist/shared/wildcard.cjs +11 -0
  225. package/dist/shared/wildcard.d.cts +8 -0
  226. package/dist/shared/wildcard.d.ts +8 -0
  227. package/dist/shared/wildcard.js +11 -0
  228. package/package.json +65 -0
@@ -0,0 +1,29 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});// src/behaviors/rewrite-uri.ts
2
+ function rewriteUri(mode, target, match) {
3
+ return (request) => {
4
+ let uri = request.uri;
5
+ switch (mode) {
6
+ case "set":
7
+ uri = target;
8
+ break;
9
+ case "prepend":
10
+ uri = target + uri;
11
+ break;
12
+ case "replace":
13
+ if (match !== void 0) {
14
+ uri = uri.split(match).join(target);
15
+ }
16
+ break;
17
+ case "regex-replace":
18
+ if (match !== void 0) {
19
+ uri = uri.replace(new RegExp(match, "g"), target);
20
+ }
21
+ break;
22
+ }
23
+ return { action: "continue", request: Object.assign({}, request, { uri }) };
24
+ };
25
+ }
26
+
27
+
28
+
29
+ exports.rewriteUri = rewriteUri;
@@ -0,0 +1,14 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});// src/behaviors/strip-query-params.ts
2
+ function stripQueryParams(params) {
3
+ return (request) => {
4
+ const querystring = Object.assign({}, request.querystring);
5
+ for (let i = 0; i < params.length; i++) {
6
+ delete querystring[params[i]];
7
+ }
8
+ return { action: "continue", request: Object.assign({}, request, { querystring }) };
9
+ };
10
+ }
11
+
12
+
13
+
14
+ exports.stripQueryParams = stripQueryParams;
@@ -0,0 +1,16 @@
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 _chunkIBXAK2A4cjs = require('./chunk-IBXAK2A4.cjs');
4
+
5
+ // src/criteria/user-agent-matches.ts
6
+ function userAgentMatches(patterns) {
7
+ return (req) => {
8
+ const ua = _optionalChain([req, 'access', _ => _.headers, 'access', _2 => _2["user-agent"], 'optionalAccess', _3 => _3.value]);
9
+ if (!ua) return false;
10
+ return _chunkIBXAK2A4cjs.matchesAnyWildcard.call(void 0, ua, patterns);
11
+ };
12
+ }
13
+
14
+
15
+
16
+ exports.userAgentMatches = userAgentMatches;
@@ -0,0 +1,88 @@
1
+ import {
2
+ runRules
3
+ } from "./chunk-Q4NP4C3B.js";
4
+ import {
5
+ __export
6
+ } from "./chunk-MLKGABMK.js";
7
+
8
+ // src/adapters/cf-function.ts
9
+ var cf_function_exports = {};
10
+ __export(cf_function_exports, {
11
+ defineViewerRequest: () => defineViewerRequest,
12
+ defineViewerResponse: () => defineViewerResponse
13
+ });
14
+ function normalizeRequest(event) {
15
+ const ev = event;
16
+ const req = ev.request;
17
+ const viewer = ev.viewer;
18
+ const headers = req.headers ?? {};
19
+ return {
20
+ uri: req.uri,
21
+ method: req.method,
22
+ protocol: "https",
23
+ querystring: req.querystring ?? {},
24
+ headers,
25
+ clientIp: viewer?.ip ?? "",
26
+ country: headers["cloudfront-viewer-country"]?.value
27
+ };
28
+ }
29
+ function denormalizeRequest(req, cookies) {
30
+ return {
31
+ method: req.method,
32
+ uri: req.uri,
33
+ querystring: req.querystring,
34
+ headers: req.headers,
35
+ cookies
36
+ };
37
+ }
38
+ function denormalizeResponse(res) {
39
+ return {
40
+ statusCode: res.statusCode,
41
+ statusDescription: res.statusDescription,
42
+ headers: res.headers,
43
+ body: res.body ?? ""
44
+ };
45
+ }
46
+ function defineViewerRequest(rules) {
47
+ return (event) => {
48
+ const ev = event;
49
+ const evReq = ev.request;
50
+ const originalCookies = evReq.cookies ?? {};
51
+ const req = normalizeRequest(event);
52
+ const result = runRules(rules, req);
53
+ if (result.action === "respond") return denormalizeResponse(result.response);
54
+ return denormalizeRequest(result.request, originalCookies);
55
+ };
56
+ }
57
+ function defineViewerResponse(responseBehaviors) {
58
+ return (event) => {
59
+ const ev = event;
60
+ const evRes = ev.response;
61
+ const req = normalizeRequest(event);
62
+ let response = {
63
+ statusCode: evRes?.statusCode ?? 200,
64
+ statusDescription: evRes?.statusDescription,
65
+ headers: evRes?.headers ?? {},
66
+ body: evRes?.body
67
+ };
68
+ for (let i = 0; i < responseBehaviors.length; i++) {
69
+ const entry = responseBehaviors[i];
70
+ if (typeof entry === "function") {
71
+ response = entry(req, response);
72
+ } else if (!entry.criteria || entry.criteria(req)) {
73
+ response = entry.behavior(req, response);
74
+ }
75
+ }
76
+ const normalized = denormalizeResponse(response);
77
+ if (!response.body) {
78
+ delete normalized.body;
79
+ }
80
+ return Object.assign({}, evRes, normalized);
81
+ };
82
+ }
83
+
84
+ export {
85
+ defineViewerRequest,
86
+ defineViewerResponse,
87
+ cf_function_exports
88
+ };
@@ -0,0 +1,42 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});// src/behaviors/construct-response.ts
2
+ var statusDescriptions = {
3
+ 200: "OK",
4
+ 204: "No Content",
5
+ 400: "Bad Request",
6
+ 401: "Unauthorized",
7
+ 403: "Forbidden",
8
+ 404: "Not Found",
9
+ 405: "Method Not Allowed",
10
+ 429: "Too Many Requests",
11
+ 500: "Internal Server Error"
12
+ };
13
+ function constructResponse(options) {
14
+ return () => {
15
+ const headers = {
16
+ "cache-control": { value: "no-store" }
17
+ };
18
+ if (options.contentType) {
19
+ headers["content-type"] = { value: options.contentType };
20
+ }
21
+ if (options.headers) {
22
+ const headerEntries = Object.entries(options.headers);
23
+ for (let i = 0; i < headerEntries.length; i++) {
24
+ const entry = headerEntries[i];
25
+ headers[entry[0].toLowerCase()] = { value: entry[1] };
26
+ }
27
+ }
28
+ return {
29
+ action: "respond",
30
+ response: {
31
+ statusCode: options.statusCode,
32
+ statusDescription: statusDescriptions[options.statusCode],
33
+ headers,
34
+ body: options.body
35
+ }
36
+ };
37
+ };
38
+ }
39
+
40
+
41
+
42
+ exports.constructResponse = constructResponse;
@@ -0,0 +1,12 @@
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/criteria/country-is.ts
2
+ function countryIs(codes) {
3
+ return (req) => {
4
+ const country = _optionalChain([req, 'access', _ => _.headers, 'access', _2 => _2["cloudfront-viewer-country"], 'optionalAccess', _3 => _3.value, 'optionalAccess', _4 => _4.toUpperCase, 'call', _5 => _5()]);
5
+ if (!country) return false;
6
+ return codes.some((c) => c.toUpperCase() === country);
7
+ };
8
+ }
9
+
10
+
11
+
12
+ exports.countryIs = countryIs;
@@ -0,0 +1,11 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});// src/criteria/method-is.ts
2
+ function methodIs(methods) {
3
+ return (req) => {
4
+ const method = req.method.toUpperCase();
5
+ return methods.some((m) => m.toUpperCase() === method);
6
+ };
7
+ }
8
+
9
+
10
+
11
+ exports.methodIs = methodIs;
@@ -0,0 +1,17 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});// src/behaviors/set-request-header.ts
2
+ function setRequestHeader(headerName, value) {
3
+ return (request) => {
4
+ return {
5
+ action: "continue",
6
+ request: Object.assign({}, request, {
7
+ headers: Object.assign({}, request.headers, {
8
+ [headerName.toLowerCase()]: { value }
9
+ })
10
+ })
11
+ };
12
+ };
13
+ }
14
+
15
+
16
+
17
+ exports.setRequestHeader = setRequestHeader;
@@ -0,0 +1,11 @@
1
+ // src/criteria/method-is.ts
2
+ function methodIs(methods) {
3
+ return (req) => {
4
+ const method = req.method.toUpperCase();
5
+ return methods.some((m) => m.toUpperCase() === method);
6
+ };
7
+ }
8
+
9
+ export {
10
+ methodIs
11
+ };
@@ -0,0 +1,47 @@
1
+ // src/core/rule.ts
2
+ function rule(criteriaOrBehavior, behavior) {
3
+ if (behavior === void 0) {
4
+ return { behavior: criteriaOrBehavior };
5
+ }
6
+ return { criteria: criteriaOrBehavior, behavior };
7
+ }
8
+ function all(fns) {
9
+ return (req) => fns.every((fn) => fn(req));
10
+ }
11
+ function any(fns) {
12
+ return (req) => fns.some((fn) => fn(req));
13
+ }
14
+ function not(fn) {
15
+ return (req) => !fn(req);
16
+ }
17
+ function chain(behaviors) {
18
+ return (request) => {
19
+ let current = request;
20
+ for (let i = 0; i < behaviors.length; i++) {
21
+ const result = behaviors[i](current);
22
+ if (result.action === "respond") return result;
23
+ current = result.request;
24
+ }
25
+ return { action: "continue", request: current };
26
+ };
27
+ }
28
+ function runRules(rules, request) {
29
+ for (let i = 0; i < rules.length; i++) {
30
+ const r = rules[i];
31
+ if (!r.criteria || r.criteria(request)) {
32
+ const result = r.behavior(request);
33
+ if (result.action === "respond") return result;
34
+ request = result.request;
35
+ }
36
+ }
37
+ return { action: "continue", request };
38
+ }
39
+
40
+ export {
41
+ rule,
42
+ all,
43
+ any,
44
+ not,
45
+ chain,
46
+ runRules
47
+ };
@@ -0,0 +1,42 @@
1
+ // src/behaviors/directory-index.ts
2
+ function directoryIndex(indexFile) {
3
+ const file = indexFile || "index.html";
4
+ return (request) => {
5
+ const uri = request.uri;
6
+ if (uri.endsWith("/")) {
7
+ return { action: "continue", request: Object.assign({}, request, { uri: uri + file }) };
8
+ }
9
+ if (uri.endsWith("/" + file)) {
10
+ const dirUri = uri.slice(0, uri.length - file.length);
11
+ return {
12
+ action: "respond",
13
+ response: {
14
+ statusCode: 301,
15
+ statusDescription: "Moved Permanently",
16
+ headers: {
17
+ location: { value: dirUri },
18
+ "cache-control": { value: "no-store" }
19
+ }
20
+ }
21
+ };
22
+ }
23
+ if (/\/[^/.]+$/.test(uri)) {
24
+ return {
25
+ action: "respond",
26
+ response: {
27
+ statusCode: 301,
28
+ statusDescription: "Moved Permanently",
29
+ headers: {
30
+ location: { value: uri + "/" },
31
+ "cache-control": { value: "no-store" }
32
+ }
33
+ }
34
+ };
35
+ }
36
+ return { action: "continue", request };
37
+ };
38
+ }
39
+
40
+ export {
41
+ directoryIndex
42
+ };
@@ -0,0 +1,14 @@
1
+ // src/behaviors/set-response-header.ts
2
+ function setResponseHeader(headerName, value) {
3
+ return (_request, response) => {
4
+ return Object.assign({}, response, {
5
+ headers: Object.assign({}, response.headers, {
6
+ [headerName.toLowerCase()]: { value }
7
+ })
8
+ });
9
+ };
10
+ }
11
+
12
+ export {
13
+ setResponseHeader
14
+ };
@@ -0,0 +1,16 @@
1
+ import {
2
+ matchesAnyWildcard
3
+ } from "./chunk-2DE6WPPL.js";
4
+
5
+ // src/criteria/user-agent-matches.ts
6
+ function userAgentMatches(patterns) {
7
+ return (req) => {
8
+ const ua = req.headers["user-agent"]?.value;
9
+ if (!ua) return false;
10
+ return matchesAnyWildcard(ua, patterns);
11
+ };
12
+ }
13
+
14
+ export {
15
+ userAgentMatches
16
+ };
@@ -0,0 +1,88 @@
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
+
3
+ var _chunkWKYMSRCDcjs = require('./chunk-WKYMSRCD.cjs');
4
+
5
+
6
+ var _chunk75ZPJI57cjs = require('./chunk-75ZPJI57.cjs');
7
+
8
+ // src/adapters/cf-function.ts
9
+ var cf_function_exports = {};
10
+ _chunk75ZPJI57cjs.__export.call(void 0, cf_function_exports, {
11
+ defineViewerRequest: () => defineViewerRequest,
12
+ defineViewerResponse: () => defineViewerResponse
13
+ });
14
+ function normalizeRequest(event) {
15
+ const ev = event;
16
+ const req = ev.request;
17
+ const viewer = ev.viewer;
18
+ const headers = _nullishCoalesce(req.headers, () => ( {}));
19
+ return {
20
+ uri: req.uri,
21
+ method: req.method,
22
+ protocol: "https",
23
+ querystring: _nullishCoalesce(req.querystring, () => ( {})),
24
+ headers,
25
+ clientIp: _nullishCoalesce(_optionalChain([viewer, 'optionalAccess', _ => _.ip]), () => ( "")),
26
+ country: _optionalChain([headers, 'access', _2 => _2["cloudfront-viewer-country"], 'optionalAccess', _3 => _3.value])
27
+ };
28
+ }
29
+ function denormalizeRequest(req, cookies) {
30
+ return {
31
+ method: req.method,
32
+ uri: req.uri,
33
+ querystring: req.querystring,
34
+ headers: req.headers,
35
+ cookies
36
+ };
37
+ }
38
+ function denormalizeResponse(res) {
39
+ return {
40
+ statusCode: res.statusCode,
41
+ statusDescription: res.statusDescription,
42
+ headers: res.headers,
43
+ body: _nullishCoalesce(res.body, () => ( ""))
44
+ };
45
+ }
46
+ function defineViewerRequest(rules) {
47
+ return (event) => {
48
+ const ev = event;
49
+ const evReq = ev.request;
50
+ const originalCookies = _nullishCoalesce(evReq.cookies, () => ( {}));
51
+ const req = normalizeRequest(event);
52
+ const result = _chunkWKYMSRCDcjs.runRules.call(void 0, rules, req);
53
+ if (result.action === "respond") return denormalizeResponse(result.response);
54
+ return denormalizeRequest(result.request, originalCookies);
55
+ };
56
+ }
57
+ function defineViewerResponse(responseBehaviors) {
58
+ return (event) => {
59
+ const ev = event;
60
+ const evRes = ev.response;
61
+ const req = normalizeRequest(event);
62
+ let response = {
63
+ statusCode: _nullishCoalesce(_optionalChain([evRes, 'optionalAccess', _4 => _4.statusCode]), () => ( 200)),
64
+ statusDescription: _optionalChain([evRes, 'optionalAccess', _5 => _5.statusDescription]),
65
+ headers: _nullishCoalesce(_optionalChain([evRes, 'optionalAccess', _6 => _6.headers]), () => ( {})),
66
+ body: _optionalChain([evRes, 'optionalAccess', _7 => _7.body])
67
+ };
68
+ for (let i = 0; i < responseBehaviors.length; i++) {
69
+ const entry = responseBehaviors[i];
70
+ if (typeof entry === "function") {
71
+ response = entry(req, response);
72
+ } else if (!entry.criteria || entry.criteria(req)) {
73
+ response = entry.behavior(req, response);
74
+ }
75
+ }
76
+ const normalized = denormalizeResponse(response);
77
+ if (!response.body) {
78
+ delete normalized.body;
79
+ }
80
+ return Object.assign({}, evRes, normalized);
81
+ };
82
+ }
83
+
84
+
85
+
86
+
87
+
88
+ exports.defineViewerRequest = defineViewerRequest; exports.defineViewerResponse = defineViewerResponse; exports.cf_function_exports = cf_function_exports;
@@ -0,0 +1,14 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});// src/behaviors/remove-response-headers.ts
2
+ function removeResponseHeaders(headerNames) {
3
+ return (_request, response) => {
4
+ const headers = Object.assign({}, response.headers);
5
+ for (let i = 0; i < headerNames.length; i++) {
6
+ delete headers[headerNames[i].toLowerCase()];
7
+ }
8
+ return Object.assign({}, response, { headers });
9
+ };
10
+ }
11
+
12
+
13
+
14
+ exports.removeResponseHeaders = removeResponseHeaders;
@@ -0,0 +1,39 @@
1
+ // 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 = options?.allowedOrigins ?? ["*"];
10
+ const allowedMethods = options?.allowedMethods ?? "GET, POST, OPTIONS";
11
+ const allowedHeaders = options?.allowedHeaders ?? "Content-Type, Cache-Control, Pragma, Range";
12
+ return (request, response) => {
13
+ let allowOrigin = allowedOrigins[0] ?? "*";
14
+ if (options?.allowOriginEcho) {
15
+ const originHeader = request.headers["origin"]?.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 (options?.allowCredentials) {
26
+ corsHeaders["access-control-allow-credentials"] = { value: "true" };
27
+ }
28
+ if (options?.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
+ export {
38
+ setCorsHeaders
39
+ };
@@ -0,0 +1,13 @@
1
+ // src/criteria/header-contains.ts
2
+ function headerContains(headerName, substrings) {
3
+ return (req) => {
4
+ const val = req.headers[headerName.toLowerCase()]?.value;
5
+ if (val === void 0) return false;
6
+ const lower = val.toLowerCase();
7
+ return substrings.some((s) => lower.includes(s.toLowerCase()));
8
+ };
9
+ }
10
+
11
+ export {
12
+ headerContains
13
+ };
@@ -0,0 +1,98 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});// src/behaviors/image-optimize.ts
2
+ function selectBreakpoint(width, breakpoints) {
3
+ const sorted = breakpoints.slice().sort(function(a, b) {
4
+ return a - b;
5
+ });
6
+ for (var i = 0; i < sorted.length; i++) {
7
+ if (sorted[i] >= width) return sorted[i];
8
+ }
9
+ return sorted[sorted.length - 1];
10
+ }
11
+ function mapAkamaiFormat(akamaiFormat) {
12
+ switch (akamaiFormat) {
13
+ case "chrome":
14
+ case "webp":
15
+ return "webp";
16
+ case "avif":
17
+ return "avif";
18
+ default:
19
+ return "jpeg";
20
+ }
21
+ }
22
+ function selectFormat(acceptHeader, formats) {
23
+ if (acceptHeader) {
24
+ for (var i = 0; i < formats.length; i++) {
25
+ if (formats[i] === "avif" && acceptHeader.indexOf("image/avif") !== -1) return "avif";
26
+ if (formats[i] === "webp" && acceptHeader.indexOf("image/webp") !== -1) return "webp";
27
+ }
28
+ }
29
+ return formats[formats.length - 1] !== void 0 ? formats[formats.length - 1] : "jpeg";
30
+ }
31
+ function resolveImageParams(request, options) {
32
+ var formats = options.formats !== void 0 ? options.formats : ["avif", "webp", "jpeg"];
33
+ var quality = options.quality !== void 0 ? options.quality : 75;
34
+ var sortedBreakpoints = options.breakpoints.slice().sort(function(a, b) {
35
+ return a - b;
36
+ });
37
+ var imwidthParamName = options.imwidthParam !== void 0 ? options.imwidthParam : "imwidth";
38
+ var imformatParamName = options.imformatParam !== void 0 ? options.imformatParam : "imformat";
39
+ var width = NaN;
40
+ var imwidthVal = request.querystring[imwidthParamName] !== void 0 ? request.querystring[imwidthParamName].value : void 0;
41
+ if (imwidthVal !== void 0) {
42
+ var parsed = parseInt(imwidthVal, 10);
43
+ if (isFinite(parsed)) width = parsed;
44
+ }
45
+ if (!isFinite(width)) {
46
+ var widthHeader = request.headers["cloudfront-viewer-width"] !== void 0 ? request.headers["cloudfront-viewer-width"].value : void 0;
47
+ if (widthHeader !== void 0) {
48
+ var parsedHeader = parseInt(widthHeader, 10);
49
+ if (isFinite(parsedHeader)) width = parsedHeader;
50
+ }
51
+ }
52
+ var breakpoint = isFinite(width) ? selectBreakpoint(width, sortedBreakpoints) : sortedBreakpoints[sortedBreakpoints.length - 1];
53
+ var format;
54
+ var imformatVal = request.querystring[imformatParamName] !== void 0 ? request.querystring[imformatParamName].value : void 0;
55
+ if (imformatVal !== void 0) {
56
+ format = mapAkamaiFormat(imformatVal);
57
+ } else {
58
+ var acceptVal = request.headers["accept"] !== void 0 ? request.headers["accept"].value : void 0;
59
+ format = selectFormat(acceptVal, formats);
60
+ }
61
+ return { breakpoint, format, quality };
62
+ }
63
+ function imageOptimize(options) {
64
+ var imformatParamName = options.imformatParam !== void 0 ? options.imformatParam : "imformat";
65
+ return function(request) {
66
+ var resolved = resolveImageParams(request, options);
67
+ var qs = Object.assign({}, request.querystring);
68
+ qs["imwidth"] = { value: String(resolved.breakpoint) };
69
+ qs["f"] = { value: resolved.format };
70
+ if (qs["q"] === void 0) {
71
+ var legacyQuality = qs["quality"] !== void 0 ? qs["quality"].value : void 0;
72
+ qs["q"] = { value: legacyQuality !== void 0 ? legacyQuality : String(resolved.quality) };
73
+ }
74
+ delete qs["quality"];
75
+ delete qs[imformatParamName];
76
+ if (imformatParamName !== "imformat") {
77
+ delete qs["imformat"];
78
+ }
79
+ var headers = Object.assign({}, request.headers);
80
+ if (options.origin !== void 0) {
81
+ headers["x-img-source-type"] = { value: options.origin.type };
82
+ if (options.origin.type === "gateway") {
83
+ headers["x-img-upstream-gateway"] = { value: options.origin.upstreamGateway };
84
+ } else {
85
+ headers["x-img-source-bucket"] = { value: options.origin.sourceBucket };
86
+ }
87
+ }
88
+ if (options.originSecret !== void 0) {
89
+ headers["x-origin-verify"] = { value: options.originSecret };
90
+ }
91
+ return { action: "continue", request: Object.assign({}, request, { querystring: qs, headers }) };
92
+ };
93
+ }
94
+
95
+
96
+
97
+
98
+ exports.resolveImageParams = resolveImageParams; exports.imageOptimize = imageOptimize;
@@ -0,0 +1,14 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});// src/criteria/file-extension.ts
2
+ function fileExtension(extensions) {
3
+ return (req) => {
4
+ const path = req.uri.split("?")[0];
5
+ const dot = path.lastIndexOf(".");
6
+ if (dot === -1) return false;
7
+ const ext = path.slice(dot + 1).toLowerCase();
8
+ return extensions.some((e) => e.toLowerCase() === ext);
9
+ };
10
+ }
11
+
12
+
13
+
14
+ exports.fileExtension = fileExtension;
@@ -0,0 +1,8 @@
1
+ // src/criteria/path-equals.ts
2
+ function pathEquals(paths) {
3
+ return (req) => paths.some((p) => req.uri === p);
4
+ }
5
+
6
+ export {
7
+ pathEquals
8
+ };
@@ -0,0 +1,19 @@
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-security-headers.ts
2
+ function setSecurityHeaders(options) {
3
+ const hsts = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _ => _.hsts]), () => ( "max-age=31536000; includeSubDomains"));
4
+ const xFrameOptions = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _2 => _2.xFrameOptions]), () => ( "SAMEORIGIN"));
5
+ const xContentTypeOptions = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _3 => _3.xContentTypeOptions]), () => ( "nosniff"));
6
+ return (_request, response) => {
7
+ return Object.assign({}, response, {
8
+ headers: Object.assign({}, response.headers, {
9
+ "strict-transport-security": { value: hsts },
10
+ "x-frame-options": { value: xFrameOptions },
11
+ "x-content-type-options": { value: xContentTypeOptions }
12
+ })
13
+ });
14
+ };
15
+ }
16
+
17
+
18
+
19
+ exports.setSecurityHeaders = setSecurityHeaders;