@abejarano/ts-express-server 1.5.3 → 1.7.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.
- package/README.md +14 -2
- package/dist/BootstrapServer.d.ts +13 -6
- package/dist/BootstrapServer.js +30 -23
- package/dist/BootstrapStandardServer.d.ts +2 -1
- package/dist/BootstrapStandardServer.js +6 -4
- package/dist/abstract/ServerModule.d.ts +2 -2
- package/dist/abstract/ServerService.d.ts +2 -2
- package/dist/abstract/ServerTypes.d.ts +64 -0
- package/dist/abstract/ServerTypes.js +8 -0
- package/dist/abstract/index.d.ts +1 -0
- package/dist/abstract/index.js +1 -0
- package/dist/adapters/BunAdapter.d.ts +18 -0
- package/dist/adapters/BunAdapter.js +741 -0
- package/dist/adapters/ExpressAdapter.d.ts +8 -0
- package/dist/adapters/ExpressAdapter.js +40 -0
- package/dist/adapters/index.d.ts +2 -0
- package/dist/adapters/index.js +18 -0
- package/dist/createRouter.d.ts +2 -0
- package/dist/createRouter.js +12 -0
- package/dist/decorators/Controller.d.ts +1 -1
- package/dist/decorators/Controller.js +4 -1
- package/dist/decorators/DecoratorGuards.d.ts +1 -0
- package/dist/decorators/DecoratorGuards.js +14 -0
- package/dist/decorators/Handlers.d.ts +5 -5
- package/dist/decorators/Handlers.js +5 -1
- package/dist/decorators/Use.d.ts +2 -2
- package/dist/decorators/Use.js +5 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.js +2 -0
- package/dist/modules/ControllersModule.d.ts +2 -2
- package/dist/modules/ControllersModule.js +14 -8
- package/dist/modules/CorsModule.d.ts +2 -2
- package/dist/modules/CorsModule.js +66 -2
- package/dist/modules/FileUploadModule.d.ts +4 -4
- package/dist/modules/FileUploadModule.js +43 -6
- package/dist/modules/RateLimitModule.d.ts +4 -4
- package/dist/modules/RateLimitModule.js +48 -7
- package/dist/modules/RequestContextModule.d.ts +2 -9
- package/dist/modules/RequestContextModule.js +53 -4
- package/dist/modules/RoutesModule.d.ts +4 -4
- package/dist/modules/RoutesModule.js +12 -3
- package/dist/modules/SecurityModule.d.ts +4 -4
- package/dist/modules/SecurityModule.js +43 -6
- package/dist/testing/createDecoratedTestApp.d.ts +3 -3
- package/dist/testing/createDecoratedTestApp.js +8 -4
- package/package.json +1 -1
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { Express, RequestHandler, Router } from "express";
|
|
2
1
|
import { BaseServerModule } from "../abstract";
|
|
2
|
+
import { ServerApp, ServerContext, ServerHandler, ServerRouter } from "../abstract";
|
|
3
3
|
export interface RouteConfig {
|
|
4
4
|
path: string;
|
|
5
|
-
router:
|
|
6
|
-
middleware?:
|
|
5
|
+
router: ServerRouter;
|
|
6
|
+
middleware?: ServerHandler[] | ServerHandler;
|
|
7
7
|
}
|
|
8
8
|
export declare class RoutesModule extends BaseServerModule {
|
|
9
9
|
name: string;
|
|
@@ -13,5 +13,5 @@ export declare class RoutesModule extends BaseServerModule {
|
|
|
13
13
|
getModuleName(): string;
|
|
14
14
|
addRoute(route: RouteConfig): void;
|
|
15
15
|
addRoutes(routes: RouteConfig[]): void;
|
|
16
|
-
init(app:
|
|
16
|
+
init(app: ServerApp, context?: ServerContext): void;
|
|
17
17
|
}
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.RoutesModule = void 0;
|
|
4
4
|
const abstract_1 = require("../abstract");
|
|
5
|
+
const abstract_2 = require("../abstract");
|
|
5
6
|
class RoutesModule extends abstract_1.BaseServerModule {
|
|
6
7
|
constructor(routes = []) {
|
|
7
8
|
super();
|
|
@@ -20,10 +21,18 @@ class RoutesModule extends abstract_1.BaseServerModule {
|
|
|
20
21
|
addRoutes(routes) {
|
|
21
22
|
this.routes.push(...routes);
|
|
22
23
|
}
|
|
23
|
-
init(app) {
|
|
24
|
+
init(app, context) {
|
|
25
|
+
if (context?.runtime === abstract_2.ServerRuntime.Bun) {
|
|
26
|
+
console.warn("[RoutesModule] Express routers are not supported on Bun. Migrate to decorated controllers for Bun runtime.");
|
|
27
|
+
}
|
|
24
28
|
this.routes.forEach(({ path, router, middleware }) => {
|
|
25
|
-
|
|
26
|
-
|
|
29
|
+
const middlewareList = Array.isArray(middleware)
|
|
30
|
+
? middleware
|
|
31
|
+
: middleware
|
|
32
|
+
? [middleware]
|
|
33
|
+
: [];
|
|
34
|
+
if (middlewareList.length > 0) {
|
|
35
|
+
app.use(path, ...middlewareList, router);
|
|
27
36
|
}
|
|
28
37
|
else {
|
|
29
38
|
app.use(path, router);
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { Express } from "express";
|
|
2
|
-
import helmet from "helmet";
|
|
3
1
|
import { BaseServerModule } from "../abstract";
|
|
2
|
+
import { ServerApp, ServerContext } from "../abstract";
|
|
3
|
+
import type { HelmetOptions } from "helmet";
|
|
4
4
|
export declare class SecurityModule extends BaseServerModule {
|
|
5
5
|
name: string;
|
|
6
6
|
priority: number;
|
|
7
7
|
private readonly helmetOptions;
|
|
8
|
-
constructor(helmetOptions?:
|
|
8
|
+
constructor(helmetOptions?: HelmetOptions);
|
|
9
9
|
getModuleName(): string;
|
|
10
|
-
init(app:
|
|
10
|
+
init(app: ServerApp, context?: ServerContext): void;
|
|
11
11
|
}
|
|
@@ -1,11 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
3
|
exports.SecurityModule = void 0;
|
|
7
|
-
const helmet_1 = __importDefault(require("helmet"));
|
|
8
4
|
const abstract_1 = require("../abstract");
|
|
5
|
+
const abstract_2 = require("../abstract");
|
|
9
6
|
class SecurityModule extends abstract_1.BaseServerModule {
|
|
10
7
|
constructor(helmetOptions) {
|
|
11
8
|
super();
|
|
@@ -29,8 +26,48 @@ class SecurityModule extends abstract_1.BaseServerModule {
|
|
|
29
26
|
getModuleName() {
|
|
30
27
|
return this.name;
|
|
31
28
|
}
|
|
32
|
-
init(app) {
|
|
33
|
-
|
|
29
|
+
init(app, context) {
|
|
30
|
+
const runtime = context?.runtime ?? abstract_2.ServerRuntime.Express;
|
|
31
|
+
if (runtime === abstract_2.ServerRuntime.Express) {
|
|
32
|
+
const helmetModule = require("helmet");
|
|
33
|
+
const helmet = helmetModule.default ?? helmetModule;
|
|
34
|
+
app.use(helmet(this.helmetOptions));
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
app.use(createSecurityMiddleware(this.helmetOptions));
|
|
34
38
|
}
|
|
35
39
|
}
|
|
36
40
|
exports.SecurityModule = SecurityModule;
|
|
41
|
+
const createSecurityMiddleware = (options) => {
|
|
42
|
+
return (_req, res, next) => {
|
|
43
|
+
const frameguard = options?.frameguard;
|
|
44
|
+
if (frameguard && typeof frameguard === "object" && "action" in frameguard) {
|
|
45
|
+
const action = frameguard.action;
|
|
46
|
+
if (action) {
|
|
47
|
+
res.set("x-frame-options", action.toUpperCase());
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
if (options?.ieNoOpen !== false) {
|
|
51
|
+
res.set("x-download-options", "noopen");
|
|
52
|
+
}
|
|
53
|
+
if (options?.noSniff !== false) {
|
|
54
|
+
res.set("x-content-type-options", "nosniff");
|
|
55
|
+
}
|
|
56
|
+
if (options?.originAgentCluster) {
|
|
57
|
+
res.set("origin-agent-cluster", "?1");
|
|
58
|
+
}
|
|
59
|
+
if (options?.permittedCrossDomainPolicies === false) {
|
|
60
|
+
res.set("x-permitted-cross-domain-policies", "none");
|
|
61
|
+
}
|
|
62
|
+
const referrerPolicy = options?.referrerPolicy;
|
|
63
|
+
if (referrerPolicy && typeof referrerPolicy === "object" && "policy" in referrerPolicy) {
|
|
64
|
+
res.set("referrer-policy", Array.isArray(referrerPolicy.policy)
|
|
65
|
+
? referrerPolicy.policy.join(",")
|
|
66
|
+
: referrerPolicy.policy);
|
|
67
|
+
}
|
|
68
|
+
if (options?.dnsPrefetchControl) {
|
|
69
|
+
res.set("x-dns-prefetch-control", "on");
|
|
70
|
+
}
|
|
71
|
+
next();
|
|
72
|
+
};
|
|
73
|
+
};
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import { Express } from "express";
|
|
2
1
|
import type { BootstrapServer } from "../BootstrapServer";
|
|
3
2
|
import { ControllersModule } from "../modules";
|
|
4
|
-
import { BaseServerService } from "../abstract";
|
|
3
|
+
import { BaseServerService, ServerApp, ServerRuntime } from "../abstract";
|
|
5
4
|
import { BootstrapStandardServerOptions } from "../BootstrapStandardServer";
|
|
6
5
|
type ControllerClass<T = any> = new (...args: any[]) => T;
|
|
7
6
|
export interface DecoratedTestAppOptions {
|
|
@@ -10,9 +9,10 @@ export interface DecoratedTestAppOptions {
|
|
|
10
9
|
port?: number;
|
|
11
10
|
services?: BaseServerService[];
|
|
12
11
|
standardOptions?: BootstrapStandardServerOptions;
|
|
12
|
+
runtime?: ServerRuntime;
|
|
13
13
|
}
|
|
14
14
|
export interface DecoratedTestAppResult {
|
|
15
|
-
app:
|
|
15
|
+
app: ServerApp;
|
|
16
16
|
server: BootstrapServer;
|
|
17
17
|
stop: () => Promise<void>;
|
|
18
18
|
}
|
|
@@ -12,6 +12,7 @@ const mergeOptions = (base, override) => {
|
|
|
12
12
|
...(override.services ?? []),
|
|
13
13
|
];
|
|
14
14
|
return {
|
|
15
|
+
runtime: override.runtime ?? base.runtime,
|
|
15
16
|
services: mergedServices.length ? mergedServices : undefined,
|
|
16
17
|
modules: {
|
|
17
18
|
...(base.modules ?? {}),
|
|
@@ -31,15 +32,18 @@ async function createDecoratedTestApp(options) {
|
|
|
31
32
|
},
|
|
32
33
|
};
|
|
33
34
|
const mergedOptions = mergeOptions(baseOptions, standardOptions);
|
|
34
|
-
const server = (0, BootstrapStandardServer_1.BootstrapStandardServer)(port, moduleInstance,
|
|
35
|
+
const server = (0, BootstrapStandardServer_1.BootstrapStandardServer)(port, moduleInstance, {
|
|
36
|
+
...mergedOptions,
|
|
37
|
+
runtime: options.runtime ?? mergedOptions.runtime,
|
|
38
|
+
});
|
|
35
39
|
await server.initialize();
|
|
36
40
|
return {
|
|
37
41
|
app: server.getApp(),
|
|
38
42
|
server,
|
|
39
43
|
stop: async () => {
|
|
40
|
-
const
|
|
41
|
-
if (
|
|
42
|
-
await new Promise((resolve) =>
|
|
44
|
+
const serverInstance = server.getServer();
|
|
45
|
+
if (serverInstance) {
|
|
46
|
+
await new Promise((resolve) => serverInstance.close(() => resolve()));
|
|
43
47
|
}
|
|
44
48
|
},
|
|
45
49
|
};
|