@ooneex/middleware 1.0.0 → 1.1.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.
- package/dist/index.d.ts +3 -1
- package/dist/index.js +2 -2
- package/dist/index.js.map +3 -3
- package/package.json +7 -6
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { AppEnv } from "@ooneex/app-env";
|
|
1
2
|
import { ContextConfigType as ContextConfigType2, ContextType as ContextType2 } from "@ooneex/controller";
|
|
2
3
|
import { ContextConfigType, ContextType } from "@ooneex/controller";
|
|
3
4
|
import { ContextConfigType as SocketContextConfigType, ContextType as SocketContextType } from "@ooneex/socket";
|
|
@@ -10,13 +11,14 @@ interface ISocketMiddleware<T extends SocketContextConfigType = SocketContextCon
|
|
|
10
11
|
handler: (context: SocketContextType<T>) => Promise<SocketContextType<T>> | SocketContextType<T>;
|
|
11
12
|
}
|
|
12
13
|
declare class CorsMiddleware<T extends ContextConfigType2 = ContextConfigType2> implements IMiddleware<T> {
|
|
14
|
+
private readonly env;
|
|
13
15
|
private readonly origins;
|
|
14
16
|
private readonly methods;
|
|
15
17
|
private readonly headers;
|
|
16
18
|
private readonly exposedHeaders;
|
|
17
19
|
private readonly credentials;
|
|
18
20
|
private readonly maxAge;
|
|
19
|
-
constructor();
|
|
21
|
+
constructor(env: AppEnv);
|
|
20
22
|
handler: (context: ContextType2<T>) => Promise<ContextType2<T>>;
|
|
21
23
|
private isOriginAllowed;
|
|
22
24
|
}
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
// @bun
|
|
2
|
-
var d=function(e,t,
|
|
2
|
+
var d=function(e,t,r,o){var i=arguments.length,s=i<3?t:o===null?o=Object.getOwnPropertyDescriptor(t,r):o,n;if(typeof Reflect==="object"&&typeof Reflect.decorate==="function")s=Reflect.decorate(e,t,r,o);else for(var p=e.length-1;p>=0;p--)if(n=e[p])s=(i<3?n(s):i>3?n(t,r,s):n(t,r))||s;return i>3&&s&&Object.defineProperty(t,r,s),s},l=(e,t)=>(r,o)=>t(r,o,e),h=(e,t)=>{if(typeof Reflect==="object"&&typeof Reflect.metadata==="function")return Reflect.metadata(e,t)};import{AppEnv as m}from"@ooneex/app-env";import{inject as y,injectable as C}from"@ooneex/container";var c=["GET","HEAD","PUT","PATCH","POST","DELETE"],g=["Content-Type","Authorization"];class a{env;origins;methods;headers;exposedHeaders;credentials;maxAge;constructor(e){this.env=e;let t=this.env.CORS_ORIGINS??"*";this.origins=t==="*"?"*":t.split(",").map((r)=>r.trim()),this.methods=this.env.CORS_METHODS?.split(",").map((r)=>r.trim())??c,this.headers=this.env.CORS_HEADERS?.split(",").map((r)=>r.trim())??g,this.exposedHeaders=this.env.CORS_EXPOSED_HEADERS?.split(",").map((r)=>r.trim())??[],this.credentials=this.env.CORS_CREDENTIALS==="true",this.maxAge=Number(this.env.CORS_MAX_AGE??86400)}handler=async(e)=>{let t=e.header.get("Origin");if(!t)return e;if(!this.isOriginAllowed(t))return e;let r=this.origins==="*"?"*":t;if(e.response.header.setAccessControlAllowOrigin(r).setAccessControlAllowMethods(this.methods).setAccessControlAllowHeaders(this.headers).setAccessControlAllowCredentials(this.credentials),this.exposedHeaders.length>0)e.response.header.set("Access-Control-Expose-Headers",this.exposedHeaders.join(", "));if(e.method==="OPTIONS")e.response.header.set("Access-Control-Max-Age",String(this.maxAge)),e.response.json({},204);return e};isOriginAllowed(e){if(this.origins==="*")return!0;return this.origins.includes(e)}}a=d([C(),l(0,y(m)),h("design:paramtypes",[typeof m==="undefined"?Object:m])],a);import{container as T,EContainerScope as A}from"@ooneex/container";var f={middleware:(e=A.Singleton)=>{return(t)=>{T.add(t,e)}}};export{f as decorator,a as CorsMiddleware};
|
|
3
3
|
|
|
4
|
-
//# debugId=
|
|
4
|
+
//# debugId=E6271FBABA94044B64756E2164756E21
|
package/dist/index.js.map
CHANGED
|
@@ -2,10 +2,10 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["src/CorsMiddleware.ts", "src/decorators.ts"],
|
|
4
4
|
"sourcesContent": [
|
|
5
|
-
"import { injectable } from \"@ooneex/container\";\nimport type { ContextConfigType, ContextType } from \"@ooneex/controller\";\nimport type { HttpMethodType } from \"@ooneex/types\";\nimport type { IMiddleware } from \"./types\";\n\nconst defaultMethods: HttpMethodType[] = [\"GET\", \"HEAD\", \"PUT\", \"PATCH\", \"POST\", \"DELETE\"];\nconst defaultHeaders: string[] = [\"Content-Type\", \"Authorization\"];\n\n@injectable()\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() {\n const origins =
|
|
5
|
+
"import { AppEnv } from \"@ooneex/app-env\";\nimport { inject, injectable } from \"@ooneex/container\";\nimport type { ContextConfigType, ContextType } from \"@ooneex/controller\";\nimport type { HttpMethodType } from \"@ooneex/types\";\nimport type { IMiddleware } from \"./types\";\n\nconst defaultMethods: HttpMethodType[] = [\"GET\", \"HEAD\", \"PUT\", \"PATCH\", \"POST\", \"DELETE\"];\nconst defaultHeaders: string[] = [\"Content-Type\", \"Authorization\"];\n\n@injectable()\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
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": ";8cAAA,iBAAS,wBACT,iBAAS,gBAAQ,0BAKjB,IAAM,EAAmC,CAAC,MAAO,OAAQ,MAAO,QAAS,OAAQ,QAAQ,EACnF,EAA2B,CAAC,eAAgB,eAAe,EAG1D,MAAM,CAA0F,CAQxD,IAP5B,QACA,QACA,QACA,eACA,YACA,OAEjB,WAAW,CAAkC,EAAa,CAAb,WAC3C,IAA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,0DAAM,GCVb,oBAAS,qBAAW,0BAGb,IAAM,EAAY,CACvB,WAAY,CAAC,EAAyB,EAAgB,YAAc,CAClE,MAAO,CAAC,IAAkE,CACxE,EAAU,IAAI,EAAQ,CAAK,GAGjC",
|
|
9
|
+
"debugId": "E6271FBABA94044B64756E2164756E21",
|
|
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.
|
|
4
|
+
"version": "1.1.1",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"files": [
|
|
7
7
|
"dist",
|
|
@@ -25,15 +25,16 @@
|
|
|
25
25
|
"test": "bun test tests",
|
|
26
26
|
"build": "bunup",
|
|
27
27
|
"lint": "tsgo --noEmit && bunx biome lint",
|
|
28
|
-
"npm:publish": "bun publish --tolerate-republish --access public"
|
|
28
|
+
"npm:publish": "bun publish --tolerate-republish --force --production --access public"
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@ooneex/
|
|
31
|
+
"@ooneex/app-env": "1.0.2",
|
|
32
|
+
"@ooneex/container": "1.0.1"
|
|
32
33
|
},
|
|
33
34
|
"devDependencies": {
|
|
34
|
-
"@ooneex/controller": "
|
|
35
|
-
"@ooneex/types": "
|
|
36
|
-
"@ooneex/socket": "
|
|
35
|
+
"@ooneex/controller": "1.1.1",
|
|
36
|
+
"@ooneex/types": "1.0.1",
|
|
37
|
+
"@ooneex/socket": "1.1.1"
|
|
37
38
|
},
|
|
38
39
|
"keywords": [
|
|
39
40
|
"bun",
|