@kaito-http/core 3.0.0-beta.8 → 3.0.2

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.
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/cors/cors.ts
21
+ var cors_exports = {};
22
+ __export(cors_exports, {
23
+ experimental_createCORSTransform: () => experimental_createCORSTransform,
24
+ experimental_createOriginMatcher: () => experimental_createOriginMatcher
25
+ });
26
+ module.exports = __toCommonJS(cors_exports);
27
+ function experimental_createOriginMatcher(origins) {
28
+ if (origins.length === 0) {
29
+ return () => false;
30
+ }
31
+ const source = origins.map((origin) => {
32
+ if (origin.startsWith("*.")) {
33
+ const escapedDomain = origin.slice(2).replace(/[.+?^${}()|[\]\\]/g, "\\$&");
34
+ return `^(?:https?://)[^.]+\\.${escapedDomain}$`;
35
+ } else {
36
+ const escapedOrigin = origin.replace(/[.+?^${}()|[\]\\]/g, "\\$&");
37
+ return `^${escapedOrigin}$`;
38
+ }
39
+ }).join("|");
40
+ const regex = new RegExp(source);
41
+ return (origin) => regex.test(origin);
42
+ }
43
+ function experimental_createCORSTransform(origins) {
44
+ const matcher = experimental_createOriginMatcher(origins);
45
+ return (request, response) => {
46
+ const origin = request.headers.get("Origin");
47
+ if (origin && matcher(origin)) {
48
+ response.headers.set("Access-Control-Allow-Origin", origin);
49
+ response.headers.set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
50
+ response.headers.set("Access-Control-Allow-Headers", "Content-Type, Authorization");
51
+ response.headers.set("Access-Control-Max-Age", "86400");
52
+ response.headers.set("Access-Control-Allow-Credentials", "true");
53
+ }
54
+ };
55
+ }
56
+ // Annotate the CommonJS export names for ESM import in node:
57
+ 0 && (module.exports = {
58
+ experimental_createCORSTransform,
59
+ experimental_createOriginMatcher
60
+ });
@@ -0,0 +1,55 @@
1
+ /**
2
+ * Creates a function that matches origins against a predefined set of patterns, supporting wildcards.
3
+ * The matcher handles both exact matches and wildcard subdomain patterns (e.g., '*.example.com').
4
+ *
5
+ * **⚠️ This API is experimental and may change or even be removed in the future. ⚠️**
6
+ *
7
+ * @param origins Array of origin patterns to match against.
8
+ * Patterns can be exact origins (e.g., 'https://example.com') or wildcard patterns (e.g., '*.example.com') that match subdomains.
9
+ * @returns A function that tests if an origin matches any of the patterns
10
+ *
11
+ * @example
12
+ * ```typescript
13
+ * const allowedOrigins = [
14
+ * 'https://example.com',
15
+ * '*.trusted-domain.com' // Won't match https://evil-domain.com, only subdomains
16
+ * ];
17
+ *
18
+ * const matcher = createOriginMatcher(allowedOrigins);
19
+ *
20
+ * // Exact match
21
+ * console.log(matcher('https://example.com')); // true
22
+ * console.log(matcher('http://example.com')); // false
23
+ *
24
+ * // Wildcard subdomain matches
25
+ * console.log(matcher('https://app.trusted-domain.com')); // true
26
+ * console.log(matcher('https://staging.trusted-domain.com')); // true
27
+ * console.log(matcher('https://trusted-domain.com')); // false, because it's not a subdomain
28
+ * console.log(matcher('https://evil-domain.com')); // false
29
+ * ```
30
+ */
31
+ declare function experimental_createOriginMatcher(origins: string[]): (origin: string) => boolean;
32
+ /**
33
+ * Create a function to apply CORS headers with sane defaults for most apps.
34
+ *
35
+ * **⚠️ This API is experimental and may change or even be removed in the future. ⚠️**
36
+ *
37
+ * @param options Options object
38
+ * @returns A function that will mutate the Response object by applying the CORS headers
39
+ * @example
40
+ * ```ts
41
+ * const cors = createCORSHandler({
42
+ * origins: ['https://example.com', "*.allows-subdomains.com", "http://localhost:3000"],
43
+ * });
44
+ *
45
+ * const handler = createKaitoHandler({
46
+ * // ...
47
+ * transform: async (request, response) => {
48
+ * cors(request, response);
49
+ * }
50
+ * });
51
+ * ```
52
+ */
53
+ declare function experimental_createCORSTransform(origins: string[]): (request: Request, response: Response) => void;
54
+
55
+ export { experimental_createCORSTransform, experimental_createOriginMatcher };
@@ -0,0 +1,55 @@
1
+ /**
2
+ * Creates a function that matches origins against a predefined set of patterns, supporting wildcards.
3
+ * The matcher handles both exact matches and wildcard subdomain patterns (e.g., '*.example.com').
4
+ *
5
+ * **⚠️ This API is experimental and may change or even be removed in the future. ⚠️**
6
+ *
7
+ * @param origins Array of origin patterns to match against.
8
+ * Patterns can be exact origins (e.g., 'https://example.com') or wildcard patterns (e.g., '*.example.com') that match subdomains.
9
+ * @returns A function that tests if an origin matches any of the patterns
10
+ *
11
+ * @example
12
+ * ```typescript
13
+ * const allowedOrigins = [
14
+ * 'https://example.com',
15
+ * '*.trusted-domain.com' // Won't match https://evil-domain.com, only subdomains
16
+ * ];
17
+ *
18
+ * const matcher = createOriginMatcher(allowedOrigins);
19
+ *
20
+ * // Exact match
21
+ * console.log(matcher('https://example.com')); // true
22
+ * console.log(matcher('http://example.com')); // false
23
+ *
24
+ * // Wildcard subdomain matches
25
+ * console.log(matcher('https://app.trusted-domain.com')); // true
26
+ * console.log(matcher('https://staging.trusted-domain.com')); // true
27
+ * console.log(matcher('https://trusted-domain.com')); // false, because it's not a subdomain
28
+ * console.log(matcher('https://evil-domain.com')); // false
29
+ * ```
30
+ */
31
+ declare function experimental_createOriginMatcher(origins: string[]): (origin: string) => boolean;
32
+ /**
33
+ * Create a function to apply CORS headers with sane defaults for most apps.
34
+ *
35
+ * **⚠️ This API is experimental and may change or even be removed in the future. ⚠️**
36
+ *
37
+ * @param options Options object
38
+ * @returns A function that will mutate the Response object by applying the CORS headers
39
+ * @example
40
+ * ```ts
41
+ * const cors = createCORSHandler({
42
+ * origins: ['https://example.com', "*.allows-subdomains.com", "http://localhost:3000"],
43
+ * });
44
+ *
45
+ * const handler = createKaitoHandler({
46
+ * // ...
47
+ * transform: async (request, response) => {
48
+ * cors(request, response);
49
+ * }
50
+ * });
51
+ * ```
52
+ */
53
+ declare function experimental_createCORSTransform(origins: string[]): (request: Request, response: Response) => void;
54
+
55
+ export { experimental_createCORSTransform, experimental_createOriginMatcher };
@@ -0,0 +1,34 @@
1
+ // src/cors/cors.ts
2
+ function experimental_createOriginMatcher(origins) {
3
+ if (origins.length === 0) {
4
+ return () => false;
5
+ }
6
+ const source = origins.map((origin) => {
7
+ if (origin.startsWith("*.")) {
8
+ const escapedDomain = origin.slice(2).replace(/[.+?^${}()|[\]\\]/g, "\\$&");
9
+ return `^(?:https?://)[^.]+\\.${escapedDomain}$`;
10
+ } else {
11
+ const escapedOrigin = origin.replace(/[.+?^${}()|[\]\\]/g, "\\$&");
12
+ return `^${escapedOrigin}$`;
13
+ }
14
+ }).join("|");
15
+ const regex = new RegExp(source);
16
+ return (origin) => regex.test(origin);
17
+ }
18
+ function experimental_createCORSTransform(origins) {
19
+ const matcher = experimental_createOriginMatcher(origins);
20
+ return (request, response) => {
21
+ const origin = request.headers.get("Origin");
22
+ if (origin && matcher(origin)) {
23
+ response.headers.set("Access-Control-Allow-Origin", origin);
24
+ response.headers.set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
25
+ response.headers.set("Access-Control-Allow-Headers", "Content-Type, Authorization");
26
+ response.headers.set("Access-Control-Max-Age", "86400");
27
+ response.headers.set("Access-Control-Allow-Credentials", "true");
28
+ }
29
+ };
30
+ }
31
+ export {
32
+ experimental_createCORSTransform,
33
+ experimental_createOriginMatcher
34
+ };