@ooneex/middleware 1.2.4 → 1.2.6
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.
- package/dist/index.js +90 -2
- package/dist/index.js.map +3 -3
- package/package.json +6 -6
package/dist/index.js
CHANGED
|
@@ -1,4 +1,92 @@
|
|
|
1
1
|
// @bun
|
|
2
|
-
var
|
|
2
|
+
var __legacyDecorateClassTS = function(decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
|
|
5
|
+
r = Reflect.decorate(decorators, target, key, desc);
|
|
6
|
+
else
|
|
7
|
+
for (var i = decorators.length - 1;i >= 0; i--)
|
|
8
|
+
if (d = decorators[i])
|
|
9
|
+
r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
10
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
11
|
+
};
|
|
12
|
+
var __legacyDecorateParamTS = (index, decorator) => (target, key) => decorator(target, key, index);
|
|
13
|
+
var __legacyMetadataTS = (k, v) => {
|
|
14
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function")
|
|
15
|
+
return Reflect.metadata(k, v);
|
|
16
|
+
};
|
|
3
17
|
|
|
4
|
-
|
|
18
|
+
// src/CorsMiddleware.ts
|
|
19
|
+
import { AppEnv } from "@ooneex/app-env";
|
|
20
|
+
import { inject } from "@ooneex/container";
|
|
21
|
+
|
|
22
|
+
// src/decorators.ts
|
|
23
|
+
import { container, EContainerScope } from "@ooneex/container";
|
|
24
|
+
var decorator = {
|
|
25
|
+
middleware: (scope = EContainerScope.Singleton) => {
|
|
26
|
+
return (target) => {
|
|
27
|
+
container.add(target, scope);
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
// src/CorsMiddleware.ts
|
|
33
|
+
var defaultMethods = ["GET", "HEAD", "PUT", "PATCH", "POST", "DELETE"];
|
|
34
|
+
var defaultHeaders = ["Content-Type", "Authorization"];
|
|
35
|
+
|
|
36
|
+
class CorsMiddleware {
|
|
37
|
+
env;
|
|
38
|
+
origins;
|
|
39
|
+
methods;
|
|
40
|
+
headers;
|
|
41
|
+
exposedHeaders;
|
|
42
|
+
credentials;
|
|
43
|
+
maxAge;
|
|
44
|
+
constructor(env) {
|
|
45
|
+
this.env = env;
|
|
46
|
+
const origins = this.env.CORS_ORIGINS ?? "*";
|
|
47
|
+
this.origins = origins === "*" ? "*" : origins.split(",").map((o) => o.trim());
|
|
48
|
+
this.methods = this.env.CORS_METHODS?.split(",").map((m) => m.trim()) ?? defaultMethods;
|
|
49
|
+
this.headers = this.env.CORS_HEADERS?.split(",").map((h) => h.trim()) ?? defaultHeaders;
|
|
50
|
+
this.exposedHeaders = this.env.CORS_EXPOSED_HEADERS?.split(",").map((h) => h.trim()) ?? [];
|
|
51
|
+
this.credentials = this.env.CORS_CREDENTIALS === "true";
|
|
52
|
+
this.maxAge = Number(this.env.CORS_MAX_AGE ?? 86400);
|
|
53
|
+
}
|
|
54
|
+
handler = async (context) => {
|
|
55
|
+
const origin = context.header.get("Origin");
|
|
56
|
+
if (!origin) {
|
|
57
|
+
return context;
|
|
58
|
+
}
|
|
59
|
+
if (!this.isOriginAllowed(origin)) {
|
|
60
|
+
return context;
|
|
61
|
+
}
|
|
62
|
+
const allowedOrigin = this.origins === "*" ? "*" : origin;
|
|
63
|
+
context.response.header.setAccessControlAllowOrigin(allowedOrigin).setAccessControlAllowMethods(this.methods).setAccessControlAllowHeaders(this.headers).setAccessControlAllowCredentials(this.credentials);
|
|
64
|
+
if (this.exposedHeaders.length > 0) {
|
|
65
|
+
context.response.header.set("Access-Control-Expose-Headers", this.exposedHeaders.join(", "));
|
|
66
|
+
}
|
|
67
|
+
if (context.method === "OPTIONS") {
|
|
68
|
+
context.response.header.set("Access-Control-Max-Age", String(this.maxAge));
|
|
69
|
+
context.response.json({}, 204);
|
|
70
|
+
}
|
|
71
|
+
return context;
|
|
72
|
+
};
|
|
73
|
+
isOriginAllowed(origin) {
|
|
74
|
+
if (this.origins === "*") {
|
|
75
|
+
return true;
|
|
76
|
+
}
|
|
77
|
+
return this.origins.includes(origin);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
CorsMiddleware = __legacyDecorateClassTS([
|
|
81
|
+
decorator.middleware(),
|
|
82
|
+
__legacyDecorateParamTS(0, inject(AppEnv)),
|
|
83
|
+
__legacyMetadataTS("design:paramtypes", [
|
|
84
|
+
typeof AppEnv === "undefined" ? Object : AppEnv
|
|
85
|
+
])
|
|
86
|
+
], CorsMiddleware);
|
|
87
|
+
export {
|
|
88
|
+
decorator,
|
|
89
|
+
CorsMiddleware
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
//# debugId=F51CF696A3A231AA64756E2164756E21
|
package/dist/index.js.map
CHANGED
|
@@ -3,9 +3,9 @@
|
|
|
3
3
|
"sources": ["src/CorsMiddleware.ts", "src/decorators.ts"],
|
|
4
4
|
"sourcesContent": [
|
|
5
5
|
"import { AppEnv } from \"@ooneex/app-env\";\nimport { inject } from \"@ooneex/container\";\nimport type { ContextConfigType, ContextType } from \"@ooneex/controller\";\nimport type { HttpMethodType } from \"@ooneex/types\";\nimport { decorator } from \"./decorators\";\nimport type { IMiddleware } from \"./types\";\n\nconst defaultMethods: HttpMethodType[] = [\"GET\", \"HEAD\", \"PUT\", \"PATCH\", \"POST\", \"DELETE\"];\nconst defaultHeaders: string[] = [\"Content-Type\", \"Authorization\"];\n\n@decorator.middleware()\nexport class CorsMiddleware<T extends ContextConfigType = ContextConfigType> implements IMiddleware<T> {\n private readonly origins: string[] | \"*\";\n private readonly methods: HttpMethodType[];\n private readonly headers: string[];\n private readonly exposedHeaders: string[];\n private readonly credentials: boolean;\n private readonly maxAge: number;\n\n constructor(@inject(AppEnv) private readonly env: AppEnv) {\n const origins = this.env.CORS_ORIGINS ?? \"*\";\n this.origins = origins === \"*\" ? \"*\" : origins.split(\",\").map((o) => o.trim());\n this.methods = (this.env.CORS_METHODS?.split(\",\").map((m) => m.trim()) as HttpMethodType[]) ?? defaultMethods;\n this.headers = this.env.CORS_HEADERS?.split(\",\").map((h) => h.trim()) ?? defaultHeaders;\n this.exposedHeaders = this.env.CORS_EXPOSED_HEADERS?.split(\",\").map((h) => h.trim()) ?? [];\n this.credentials = this.env.CORS_CREDENTIALS === \"true\";\n this.maxAge = Number(this.env.CORS_MAX_AGE ?? 86400);\n }\n\n public handler = async (context: ContextType<T>): Promise<ContextType<T>> => {\n const origin = context.header.get(\"Origin\");\n\n if (!origin) {\n return context;\n }\n\n if (!this.isOriginAllowed(origin)) {\n return context;\n }\n\n const allowedOrigin = this.origins === \"*\" ? \"*\" : origin;\n\n context.response.header\n .setAccessControlAllowOrigin(allowedOrigin)\n .setAccessControlAllowMethods(this.methods)\n .setAccessControlAllowHeaders(this.headers)\n .setAccessControlAllowCredentials(this.credentials);\n\n if (this.exposedHeaders.length > 0) {\n context.response.header.set(\"Access-Control-Expose-Headers\", this.exposedHeaders.join(\", \"));\n }\n\n if (context.method === \"OPTIONS\") {\n context.response.header.set(\"Access-Control-Max-Age\", String(this.maxAge));\n context.response.json({}, 204);\n }\n\n return context;\n };\n\n private isOriginAllowed(origin: string): boolean {\n if (this.origins === \"*\") {\n return true;\n }\n\n return this.origins.includes(origin);\n }\n}\n",
|
|
6
|
-
"import { container, EContainerScope
|
|
6
|
+
"import { container, EContainerScope } from \"@ooneex/container\";\nimport type { MiddlewareClassType, SocketMiddlewareClassType } from \"./types\";\n\nexport const decorator = {\n middleware: (scope: EContainerScope = EContainerScope.Singleton) => {\n return (target: MiddlewareClassType | SocketMiddlewareClassType): void => {\n container.add(target, scope);\n };\n },\n};\n"
|
|
7
7
|
],
|
|
8
|
-
"mappings": ";
|
|
9
|
-
"debugId": "
|
|
8
|
+
"mappings": ";;;;;;;;;;;;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AA,IACT;AAAA,IAEA,MAAM,gBAAgB,KAAK,YAAY,MAAM,MAAM;AAAA,IAEnD,QAAQ,SAAS,OACd,4BAA4B,aAAa,EACzC,6BAA6B,KAAK,OAAO,EACzC,6BAA6B,KAAK,OAAO,EACzC,iCAAiC,KAAK,WAAW;AAAA,IAEpD,IAAI,KAAK,eAAe,SAAS,GAAG;AAAA,MAClC,QAAQ,SAAS,OAAO,IAAI,iCAAiC,KAAK,eAAe,KAAK,IAAI,CAAC;AAAA,IAC7F;AAAA,IAEA,IAAI,QAAQ,WAAW,WAAW;AAAA,MAChC,QAAQ,SAAS,OAAO,IAAI,0BAA0B,OAAO,KAAK,MAAM,CAAC;AAAA,MACzE,QAAQ,SAAS,KAAK,CAAC,GAAG,GAAG;AAAA,IAC/B;AAAA,IAEA,OAAO;AAAA;AAAA,EAGD,eAAe,CAAC,QAAyB;AAAA,IAC/C,IAAI,KAAK,YAAY,KAAK;AAAA,MACxB,OAAO;AAAA,IACT;AAAA,IAEA,OAAO,KAAK,QAAQ,SAAS,MAAM;AAAA;AAEvC;AAxDa,iBAAN;AAAA,EADN,UAAU,WAAW;AAAA,EASP,kCAAO,MAAM;AAAA,EARrB;AAAA;AAAA;AAAA,GAAM;",
|
|
9
|
+
"debugId": "F51CF696A3A231AA64756E2164756E21",
|
|
10
10
|
"names": []
|
|
11
11
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ooneex/middleware",
|
|
3
3
|
"description": "Middleware pipeline framework with decorator-based registration for processing HTTP requests, responses, and WebSocket events in sequence",
|
|
4
|
-
"version": "1.2.
|
|
4
|
+
"version": "1.2.6",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"files": [
|
|
7
7
|
"dist",
|
|
@@ -28,13 +28,13 @@
|
|
|
28
28
|
"npm:publish": "bun publish --tolerate-republish --force --production --access public"
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@ooneex/app-env": "1.
|
|
32
|
-
"@ooneex/container": "1.
|
|
31
|
+
"@ooneex/app-env": "1.3.0",
|
|
32
|
+
"@ooneex/container": "1.2.2"
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
|
-
"@ooneex/controller": "1.3.
|
|
36
|
-
"@ooneex/types": "1.1.
|
|
37
|
-
"@ooneex/socket": "1.2.
|
|
35
|
+
"@ooneex/controller": "1.3.3",
|
|
36
|
+
"@ooneex/types": "1.1.3",
|
|
37
|
+
"@ooneex/socket": "1.2.4"
|
|
38
38
|
},
|
|
39
39
|
"keywords": [
|
|
40
40
|
"bun",
|