@tsed/platform-http 8.11.0 → 8.11.2-rc.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/lib/esm/common/builder/PlatformBuilder.js +0 -1
- package/lib/esm/common/builder/__fixtures__/PlatformCustom.js +0 -1
- package/lib/esm/common/config/interfaces/PlatformJsonMapperSettings.js +0 -1
- package/lib/esm/common/config/interfaces/PlatformMulterSettings.js +0 -1
- package/lib/esm/common/config/interfaces/PlatformRouterSettings.js +0 -1
- package/lib/esm/common/config/interfaces/PlatformStaticsSettings.js +0 -1
- package/lib/esm/common/config/services/PlatformConfiguration.js +0 -1
- package/lib/esm/common/constants/routerOptions.js +0 -1
- package/lib/esm/common/decorators/PlatformProvider.js +0 -1
- package/lib/esm/common/decorators/multer/multerFileSize.js +0 -1
- package/lib/esm/common/decorators/multer/multerOptions.js +0 -1
- package/lib/esm/common/decorators/multer/multipartFile.js +0 -1
- package/lib/esm/common/decorators/params/error.js +0 -1
- package/lib/esm/common/decorators/params/next.js +0 -1
- package/lib/esm/common/decorators/params/request.js +0 -1
- package/lib/esm/common/decorators/params/response.js +0 -1
- package/lib/esm/common/domain/AnyToPromiseWithCtx.js +0 -1
- package/lib/esm/common/domain/EndpointMetadata.js +0 -1
- package/lib/esm/common/domain/PlatformContext.js +0 -1
- package/lib/esm/common/domain/PlatformRouteDetails.js +0 -1
- package/lib/esm/common/exports.js +0 -1
- package/lib/esm/common/fn/adapter.js +0 -1
- package/lib/esm/common/fn/application.js +0 -1
- package/lib/esm/common/index.js +0 -1
- package/lib/esm/common/interfaces/AfterInit.js +0 -1
- package/lib/esm/common/interfaces/AfterListen.js +0 -1
- package/lib/esm/common/interfaces/AfterRoutesInit.js +0 -1
- package/lib/esm/common/interfaces/BeforeInit.js +0 -1
- package/lib/esm/common/interfaces/BeforeListen.js +0 -1
- package/lib/esm/common/interfaces/BeforeRoutesInit.js +0 -1
- package/lib/esm/common/interfaces/HandlerType.js +0 -1
- package/lib/esm/common/interfaces/IncomingEvent.js +0 -1
- package/lib/esm/common/interfaces/OnReady.js +0 -1
- package/lib/esm/common/interfaces/OnRequest.js +0 -1
- package/lib/esm/common/interfaces/OnResponse.js +0 -1
- package/lib/esm/common/interfaces/OnRoutesInit.js +0 -1
- package/lib/esm/common/interfaces/PlatformRouteOptions.js +0 -1
- package/lib/esm/common/interfaces/ResponseErrorObject.js +0 -1
- package/lib/esm/common/interfaces/Route.js +0 -1
- package/lib/esm/common/middlewares/PlatformAcceptMimesMiddleware.js +0 -1
- package/lib/esm/common/middlewares/PlatformMulterMiddleware.js +0 -1
- package/lib/esm/common/services/Platform.js +0 -1
- package/lib/esm/common/services/PlatformAdapter.js +0 -1
- package/lib/esm/common/services/PlatformApplication.js +0 -1
- package/lib/esm/common/services/PlatformHandler.js +0 -1
- package/lib/esm/common/services/PlatformRequest.js +0 -1
- package/lib/esm/common/services/PlatformResponse.js +0 -1
- package/lib/esm/common/utils/closeServer.js +0 -1
- package/lib/esm/common/utils/createContext.js +0 -1
- package/lib/esm/common/utils/createHttpServer.js +0 -1
- package/lib/esm/common/utils/createHttpsServer.js +0 -1
- package/lib/esm/common/utils/createInjector.js +0 -1
- package/lib/esm/common/utils/createServer.js +0 -1
- package/lib/esm/common/utils/ensureContext.js +0 -1
- package/lib/esm/common/utils/getConfiguration.js +0 -1
- package/lib/esm/common/utils/getStaticsOptions.js +0 -1
- package/lib/esm/common/utils/listenServer.js +0 -1
- package/lib/esm/common/utils/mapReturnedResponse.js +0 -1
- package/lib/esm/common/utils/printRoutes.js +0 -1
- package/lib/esm/common/utils/resolveControllers.js +0 -1
- package/lib/esm/common/utils/setResponseHeaders.js +0 -1
- package/lib/esm/index.js +0 -1
- package/lib/esm/testing/FakeAdapter.js +0 -1
- package/lib/esm/testing/FakeResponse.js +0 -1
- package/lib/esm/testing/PlatformTest.js +0 -1
- package/lib/esm/testing/index.js +0 -1
- package/package.json +20 -17
- package/.barrelsby.json +0 -5
- package/lib/esm/common/builder/PlatformBuilder.js.map +0 -1
- package/lib/esm/common/builder/__fixtures__/PlatformCustom.js.map +0 -1
- package/lib/esm/common/config/interfaces/PlatformJsonMapperSettings.js.map +0 -1
- package/lib/esm/common/config/interfaces/PlatformMulterSettings.js.map +0 -1
- package/lib/esm/common/config/interfaces/PlatformRouterSettings.js.map +0 -1
- package/lib/esm/common/config/interfaces/PlatformStaticsSettings.js.map +0 -1
- package/lib/esm/common/config/services/PlatformConfiguration.js.map +0 -1
- package/lib/esm/common/constants/routerOptions.js.map +0 -1
- package/lib/esm/common/decorators/PlatformProvider.js.map +0 -1
- package/lib/esm/common/decorators/multer/multerFileSize.js.map +0 -1
- package/lib/esm/common/decorators/multer/multerOptions.js.map +0 -1
- package/lib/esm/common/decorators/multer/multipartFile.js.map +0 -1
- package/lib/esm/common/decorators/params/error.js.map +0 -1
- package/lib/esm/common/decorators/params/next.js.map +0 -1
- package/lib/esm/common/decorators/params/request.js.map +0 -1
- package/lib/esm/common/decorators/params/response.js.map +0 -1
- package/lib/esm/common/domain/AnyToPromiseWithCtx.js.map +0 -1
- package/lib/esm/common/domain/EndpointMetadata.js.map +0 -1
- package/lib/esm/common/domain/PlatformContext.js.map +0 -1
- package/lib/esm/common/domain/PlatformRouteDetails.js.map +0 -1
- package/lib/esm/common/exports.js.map +0 -1
- package/lib/esm/common/fn/adapter.js.map +0 -1
- package/lib/esm/common/fn/application.js.map +0 -1
- package/lib/esm/common/index.js.map +0 -1
- package/lib/esm/common/interfaces/AfterInit.js.map +0 -1
- package/lib/esm/common/interfaces/AfterListen.js.map +0 -1
- package/lib/esm/common/interfaces/AfterRoutesInit.js.map +0 -1
- package/lib/esm/common/interfaces/BeforeInit.js.map +0 -1
- package/lib/esm/common/interfaces/BeforeListen.js.map +0 -1
- package/lib/esm/common/interfaces/BeforeRoutesInit.js.map +0 -1
- package/lib/esm/common/interfaces/HandlerType.js.map +0 -1
- package/lib/esm/common/interfaces/IncomingEvent.js.map +0 -1
- package/lib/esm/common/interfaces/OnReady.js.map +0 -1
- package/lib/esm/common/interfaces/OnRequest.js.map +0 -1
- package/lib/esm/common/interfaces/OnResponse.js.map +0 -1
- package/lib/esm/common/interfaces/OnRoutesInit.js.map +0 -1
- package/lib/esm/common/interfaces/PlatformRouteOptions.js.map +0 -1
- package/lib/esm/common/interfaces/ResponseErrorObject.js.map +0 -1
- package/lib/esm/common/interfaces/Route.js.map +0 -1
- package/lib/esm/common/middlewares/PlatformAcceptMimesMiddleware.js.map +0 -1
- package/lib/esm/common/middlewares/PlatformMulterMiddleware.js.map +0 -1
- package/lib/esm/common/services/Platform.js.map +0 -1
- package/lib/esm/common/services/PlatformAdapter.js.map +0 -1
- package/lib/esm/common/services/PlatformApplication.js.map +0 -1
- package/lib/esm/common/services/PlatformHandler.js.map +0 -1
- package/lib/esm/common/services/PlatformRequest.js.map +0 -1
- package/lib/esm/common/services/PlatformResponse.js.map +0 -1
- package/lib/esm/common/utils/closeServer.js.map +0 -1
- package/lib/esm/common/utils/createContext.js.map +0 -1
- package/lib/esm/common/utils/createHttpServer.js.map +0 -1
- package/lib/esm/common/utils/createHttpsServer.js.map +0 -1
- package/lib/esm/common/utils/createInjector.js.map +0 -1
- package/lib/esm/common/utils/createServer.js.map +0 -1
- package/lib/esm/common/utils/ensureContext.js.map +0 -1
- package/lib/esm/common/utils/getConfiguration.js.map +0 -1
- package/lib/esm/common/utils/getStaticsOptions.js.map +0 -1
- package/lib/esm/common/utils/listenServer.js.map +0 -1
- package/lib/esm/common/utils/mapReturnedResponse.js.map +0 -1
- package/lib/esm/common/utils/printRoutes.js.map +0 -1
- package/lib/esm/common/utils/resolveControllers.js.map +0 -1
- package/lib/esm/common/utils/setResponseHeaders.js.map +0 -1
- package/lib/esm/index.js.map +0 -1
- package/lib/esm/testing/FakeAdapter.js.map +0 -1
- package/lib/esm/testing/FakeResponse.js.map +0 -1
- package/lib/esm/testing/PlatformTest.js.map +0 -1
- package/lib/esm/testing/index.js.map +0 -1
- package/src/common/builder/PlatformBuilder.spec.ts +0 -331
- package/src/common/builder/PlatformBuilder.ts +0 -426
- package/src/common/builder/__fixtures__/PlatformCustom.ts +0 -39
- package/src/common/config/interfaces/PlatformJsonMapperSettings.ts +0 -11
- package/src/common/config/interfaces/PlatformMulterSettings.ts +0 -22
- package/src/common/config/interfaces/PlatformRouterSettings.ts +0 -11
- package/src/common/config/interfaces/PlatformStaticsSettings.ts +0 -23
- package/src/common/config/services/PlatformConfiguration.spec.ts +0 -181
- package/src/common/config/services/PlatformConfiguration.ts +0 -158
- package/src/common/constants/routerOptions.ts +0 -1
- package/src/common/decorators/PlatformProvider.ts +0 -14
- package/src/common/decorators/multer/multerFileSize.ts +0 -31
- package/src/common/decorators/multer/multerOptions.ts +0 -45
- package/src/common/decorators/multer/multipartFile.ts +0 -51
- package/src/common/decorators/params/error.spec.ts +0 -15
- package/src/common/decorators/params/error.ts +0 -16
- package/src/common/decorators/params/next.spec.ts +0 -15
- package/src/common/decorators/params/next.ts +0 -31
- package/src/common/decorators/params/request.spec.ts +0 -50
- package/src/common/decorators/params/request.ts +0 -86
- package/src/common/decorators/params/response.spec.ts +0 -37
- package/src/common/decorators/params/response.ts +0 -74
- package/src/common/domain/AnyToPromiseWithCtx.spec.ts +0 -20
- package/src/common/domain/AnyToPromiseWithCtx.ts +0 -30
- package/src/common/domain/EndpointMetadata.ts +0 -1
- package/src/common/domain/PlatformContext.spec.ts +0 -124
- package/src/common/domain/PlatformContext.ts +0 -136
- package/src/common/domain/PlatformRouteDetails.ts +0 -7
- package/src/common/exports.ts +0 -4
- package/src/common/fn/adapter.ts +0 -42
- package/src/common/fn/application.ts +0 -11
- package/src/common/index.ts +0 -61
- package/src/common/interfaces/AfterInit.ts +0 -3
- package/src/common/interfaces/AfterListen.ts +0 -3
- package/src/common/interfaces/AfterRoutesInit.ts +0 -3
- package/src/common/interfaces/BeforeInit.ts +0 -3
- package/src/common/interfaces/BeforeListen.ts +0 -3
- package/src/common/interfaces/BeforeRoutesInit.ts +0 -3
- package/src/common/interfaces/HandlerType.ts +0 -12
- package/src/common/interfaces/IncomingEvent.ts +0 -6
- package/src/common/interfaces/OnReady.ts +0 -3
- package/src/common/interfaces/OnRequest.ts +0 -5
- package/src/common/interfaces/OnResponse.ts +0 -5
- package/src/common/interfaces/OnRoutesInit.ts +0 -3
- package/src/common/interfaces/PlatformRouteOptions.ts +0 -17
- package/src/common/interfaces/ResponseErrorObject.ts +0 -8
- package/src/common/interfaces/Route.ts +0 -12
- package/src/common/middlewares/PlatformAcceptMimesMiddleware.spec.ts +0 -215
- package/src/common/middlewares/PlatformAcceptMimesMiddleware.ts +0 -34
- package/src/common/middlewares/PlatformMulterMiddleware.ts +0 -6
- package/src/common/services/Platform.spec.ts +0 -145
- package/src/common/services/Platform.ts +0 -79
- package/src/common/services/PlatformAdapter.ts +0 -102
- package/src/common/services/PlatformApplication.spec.ts +0 -89
- package/src/common/services/PlatformApplication.ts +0 -38
- package/src/common/services/PlatformHandler.spec.ts +0 -152
- package/src/common/services/PlatformHandler.ts +0 -137
- package/src/common/services/PlatformRequest.spec.ts +0 -185
- package/src/common/services/PlatformRequest.ts +0 -182
- package/src/common/services/PlatformResponse.spec.ts +0 -364
- package/src/common/services/PlatformResponse.ts +0 -393
- package/src/common/utils/closeServer.spec.ts +0 -11
- package/src/common/utils/closeServer.ts +0 -8
- package/src/common/utils/createContext.spec.ts +0 -111
- package/src/common/utils/createContext.ts +0 -52
- package/src/common/utils/createHttpServer.spec.ts +0 -75
- package/src/common/utils/createHttpServer.ts +0 -16
- package/src/common/utils/createHttpsServer.spec.ts +0 -74
- package/src/common/utils/createHttpsServer.ts +0 -17
- package/src/common/utils/createInjector.spec.ts +0 -42
- package/src/common/utils/createInjector.ts +0 -25
- package/src/common/utils/createServer.ts +0 -52
- package/src/common/utils/ensureContext.spec.ts +0 -41
- package/src/common/utils/ensureContext.ts +0 -17
- package/src/common/utils/getConfiguration.spec.ts +0 -163
- package/src/common/utils/getConfiguration.ts +0 -34
- package/src/common/utils/getStaticsOptions.spec.ts +0 -27
- package/src/common/utils/getStaticsOptions.ts +0 -24
- package/src/common/utils/listenServer.ts +0 -18
- package/src/common/utils/mapReturnedResponse.spec.ts +0 -35
- package/src/common/utils/mapReturnedResponse.ts +0 -10
- package/src/common/utils/printRoutes.spec.ts +0 -19
- package/src/common/utils/printRoutes.ts +0 -40
- package/src/common/utils/resolveControllers.spec.ts +0 -40
- package/src/common/utils/resolveControllers.ts +0 -54
- package/src/common/utils/setResponseHeaders.spec.ts +0 -84
- package/src/common/utils/setResponseHeaders.ts +0 -31
- package/src/index.ts +0 -2
- package/src/testing/FakeAdapter.ts +0 -69
- package/src/testing/FakeResponse.ts +0 -115
- package/src/testing/PlatformTest.spec.ts +0 -53
- package/src/testing/PlatformTest.ts +0 -161
- package/src/testing/index.ts +0 -5
- package/vitest.config.mts +0 -25
|
@@ -1,393 +0,0 @@
|
|
|
1
|
-
import {OutgoingHttpHeaders, ServerResponse} from "node:http";
|
|
2
|
-
|
|
3
|
-
import {isArray, isBoolean, isNumber, isStream, isString} from "@tsed/core";
|
|
4
|
-
import {injectable, lazyInject, ProviderScope} from "@tsed/di";
|
|
5
|
-
import {getStatusMessage} from "@tsed/schema";
|
|
6
|
-
import encodeUrl from "encodeurl";
|
|
7
|
-
|
|
8
|
-
import type {PlatformContext} from "../domain/PlatformContext.js";
|
|
9
|
-
import type {PlatformRequest} from "./PlatformRequest.js";
|
|
10
|
-
|
|
11
|
-
declare global {
|
|
12
|
-
namespace TsED {
|
|
13
|
-
// @ts-ignore
|
|
14
|
-
export interface Response {}
|
|
15
|
-
|
|
16
|
-
export interface SetCookieOpts extends Record<string, unknown> {
|
|
17
|
-
maxAge?: number | undefined;
|
|
18
|
-
signed?: boolean | undefined;
|
|
19
|
-
expires?: Date | undefined;
|
|
20
|
-
httpOnly?: boolean | undefined;
|
|
21
|
-
path?: string | undefined;
|
|
22
|
-
domain?: string | undefined;
|
|
23
|
-
secure?: boolean | undefined;
|
|
24
|
-
encode?: ((val: string) => string) | undefined;
|
|
25
|
-
sameSite?: boolean | "lax" | "strict" | "none" | undefined;
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Platform Response abstraction layer.
|
|
32
|
-
* @platform
|
|
33
|
-
*/
|
|
34
|
-
export class PlatformResponse<Res extends Record<string, any> = any> {
|
|
35
|
-
data: any;
|
|
36
|
-
|
|
37
|
-
constructor(readonly $ctx: PlatformContext) {}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* The current @@PlatformRequest@@.
|
|
41
|
-
*/
|
|
42
|
-
get request(): PlatformRequest {
|
|
43
|
-
return this.$ctx.request;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
get raw(): Res {
|
|
47
|
-
return this.$ctx.event.response as any;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Get the current statusCode
|
|
52
|
-
*/
|
|
53
|
-
get statusCode() {
|
|
54
|
-
return this.raw.statusCode;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* An object that contains response local variables scoped to the request, and therefore available only to the view(s) rendered during that request / response cycle (if any). Otherwise, this property is identical to app.locals.
|
|
59
|
-
*
|
|
60
|
-
* This property is useful for exposing request-level information such as the request path name, authenticated user, user settings, and so on.
|
|
61
|
-
*/
|
|
62
|
-
get locals() {
|
|
63
|
-
return this.raw.locals;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Return the original response framework instance
|
|
68
|
-
*/
|
|
69
|
-
get response() {
|
|
70
|
-
return this.getResponse();
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* Return the original response node.js instance
|
|
75
|
-
*/
|
|
76
|
-
get res() {
|
|
77
|
-
return this.getRes();
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* Returns the HTTP response header specified by field. The match is case-insensitive.
|
|
82
|
-
*
|
|
83
|
-
* ```typescript
|
|
84
|
-
* response.get('Content-Type') // => "text/plain"
|
|
85
|
-
* ```
|
|
86
|
-
*
|
|
87
|
-
* @param name
|
|
88
|
-
*/
|
|
89
|
-
get(name: string) {
|
|
90
|
-
return this.raw.get(name);
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
getHeaders(): OutgoingHttpHeaders {
|
|
94
|
-
return this.raw.getHeaders();
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* Return the Framework response object (express, koa, etc...)
|
|
99
|
-
*/
|
|
100
|
-
getResponse<R = Res>(): R {
|
|
101
|
-
return this.raw as any;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
* Return the Node.js response object
|
|
106
|
-
*/
|
|
107
|
-
getRes(): ServerResponse {
|
|
108
|
-
return this.raw as any;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
hasStatus() {
|
|
112
|
-
return this.statusCode !== 200;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* Sets the HTTP status for the response.
|
|
117
|
-
*
|
|
118
|
-
* @param status
|
|
119
|
-
*/
|
|
120
|
-
status(status: number) {
|
|
121
|
-
this.raw.status(status);
|
|
122
|
-
|
|
123
|
-
return this;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
/**
|
|
127
|
-
* Set header `field` to `val`, or pass
|
|
128
|
-
* an object of header fields.
|
|
129
|
-
*
|
|
130
|
-
* Examples:
|
|
131
|
-
* ```typescript
|
|
132
|
-
* response.setHeaders({ Accept: 'text/plain', 'X-API-Key': 'tobi' });
|
|
133
|
-
* ```
|
|
134
|
-
*
|
|
135
|
-
* Aliased as `res.header()`.
|
|
136
|
-
*/
|
|
137
|
-
setHeaders(headers: OutgoingHttpHeaders) {
|
|
138
|
-
// apply headers
|
|
139
|
-
Object.entries(headers).forEach(([key, item]) => {
|
|
140
|
-
this.setHeader(key, item);
|
|
141
|
-
});
|
|
142
|
-
|
|
143
|
-
return this;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
setHeader(key: string, item: any) {
|
|
147
|
-
this.raw.set(key, this.formatHeader(key, item));
|
|
148
|
-
|
|
149
|
-
return this;
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
/**
|
|
153
|
-
* Set `Content-Type` response header with `type` through `mime.lookup()`
|
|
154
|
-
* when it does not contain "/", or set the Content-Type to `type` otherwise.
|
|
155
|
-
*
|
|
156
|
-
* Examples:
|
|
157
|
-
*
|
|
158
|
-
* res.type('.html');
|
|
159
|
-
* res.type('html');
|
|
160
|
-
* res.type('json');
|
|
161
|
-
* res.type('application/json');
|
|
162
|
-
* res.type('png');
|
|
163
|
-
*/
|
|
164
|
-
contentType(contentType: string) {
|
|
165
|
-
this.raw.contentType(contentType);
|
|
166
|
-
|
|
167
|
-
return this;
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
contentLength(length: number) {
|
|
171
|
-
this.setHeader("Content-Length", length);
|
|
172
|
-
return this;
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
getContentLength() {
|
|
176
|
-
if (this.get("Content-Length")) {
|
|
177
|
-
return parseInt(this.get("Content-Length"), 10) || 0;
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
getContentType() {
|
|
182
|
-
return (this.get("Content-Type") || "").split(";")[0];
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
/**
|
|
186
|
-
* Sets the HTTP response Content-Disposition header field to “attachment”.
|
|
187
|
-
* If a filename is given, then it sets the Content-Type based on the extension name via res.type(), and sets the Content-Disposition “filename=” parameter.
|
|
188
|
-
*
|
|
189
|
-
* ```typescript
|
|
190
|
-
* res.attachment()
|
|
191
|
-
* // Content-Disposition: attachment
|
|
192
|
-
*
|
|
193
|
-
* res.attachment('path/to/logo.png')
|
|
194
|
-
* // Content-Disposition: attachment; filename="logo.png"
|
|
195
|
-
* // Content-Type: image/png
|
|
196
|
-
* ```
|
|
197
|
-
*
|
|
198
|
-
* @param filename
|
|
199
|
-
*/
|
|
200
|
-
attachment(filename: string) {
|
|
201
|
-
this.raw.attachment(filename);
|
|
202
|
-
return this;
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
/**
|
|
206
|
-
* Redirects to the URL derived from the specified path, with specified status, a positive integer that corresponds to an [HTTP status code](http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html).
|
|
207
|
-
* If not specified, status defaults to `302 Found`.
|
|
208
|
-
*
|
|
209
|
-
* @param status
|
|
210
|
-
* @param url
|
|
211
|
-
*/
|
|
212
|
-
redirect(status: number, url: string): this {
|
|
213
|
-
status = status || 302;
|
|
214
|
-
|
|
215
|
-
this.location(url);
|
|
216
|
-
|
|
217
|
-
// Set location header
|
|
218
|
-
url = this.get("Location");
|
|
219
|
-
|
|
220
|
-
const txt = `${getStatusMessage(status)}. Redirecting to ${url}`;
|
|
221
|
-
|
|
222
|
-
this.status(status);
|
|
223
|
-
|
|
224
|
-
if (this.request.method === "HEAD") {
|
|
225
|
-
this.end();
|
|
226
|
-
} else {
|
|
227
|
-
this.setHeader("Content-Length", Buffer.byteLength(txt)).end(txt);
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
return this;
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
/**
|
|
234
|
-
* Sets the response Location HTTP header to the specified path parameter.
|
|
235
|
-
*
|
|
236
|
-
* @param location
|
|
237
|
-
*/
|
|
238
|
-
location(location: string): this {
|
|
239
|
-
this.setHeader("Location", location);
|
|
240
|
-
|
|
241
|
-
return this;
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
/**
|
|
245
|
-
* Stream the given data.
|
|
246
|
-
*
|
|
247
|
-
* @param data
|
|
248
|
-
*/
|
|
249
|
-
stream(data: any) {
|
|
250
|
-
data.pipe(this.raw);
|
|
251
|
-
|
|
252
|
-
return this;
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
/**
|
|
256
|
-
* Renders a view and sends the rendered HTML string to the client.
|
|
257
|
-
*
|
|
258
|
-
* @param path
|
|
259
|
-
* @param options
|
|
260
|
-
*/
|
|
261
|
-
async render(path: string, options: any = {}) {
|
|
262
|
-
const platformViews = await lazyInject(() => import("@tsed/platform-views"));
|
|
263
|
-
|
|
264
|
-
return platformViews.render(path, {
|
|
265
|
-
...this.locals,
|
|
266
|
-
...options
|
|
267
|
-
}) as unknown as string;
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
/**
|
|
271
|
-
* Send any data to your consumer.
|
|
272
|
-
*
|
|
273
|
-
* This method accept a ReadableStream, a plain object, boolean, string, number, null and undefined data.
|
|
274
|
-
* It chooses the better way to send the data.
|
|
275
|
-
*
|
|
276
|
-
* @param data
|
|
277
|
-
*/
|
|
278
|
-
body(data: any) {
|
|
279
|
-
this.data = data;
|
|
280
|
-
|
|
281
|
-
if (data === undefined) {
|
|
282
|
-
this.end();
|
|
283
|
-
|
|
284
|
-
return this;
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
if (isStream(data)) {
|
|
288
|
-
this.stream(data);
|
|
289
|
-
|
|
290
|
-
return this;
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
if (Buffer.isBuffer(data)) {
|
|
294
|
-
this.buffer(data);
|
|
295
|
-
|
|
296
|
-
return this;
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
if (isBoolean(data) || isNumber(data) || isString(data) || data === null) {
|
|
300
|
-
if (data === null) {
|
|
301
|
-
this.status(204);
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
this.end(data);
|
|
305
|
-
|
|
306
|
-
return this;
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
this.json(data);
|
|
310
|
-
|
|
311
|
-
return this;
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
getBody() {
|
|
315
|
-
return this.data;
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
/**
|
|
319
|
-
* Add a listener to handler the end of the request/response.
|
|
320
|
-
* @param cb
|
|
321
|
-
*/
|
|
322
|
-
onEnd(cb: () => Promise<void> | void): this {
|
|
323
|
-
const res: any = this.getRes();
|
|
324
|
-
|
|
325
|
-
res.on("finish", cb);
|
|
326
|
-
|
|
327
|
-
return this;
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
isDone(): boolean {
|
|
331
|
-
if (this.$ctx.isFinished()) {
|
|
332
|
-
return true;
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
const res = this.getRes();
|
|
336
|
-
|
|
337
|
-
return Boolean(this.isHeadersSent() || res.writableEnded || res.writableFinished);
|
|
338
|
-
}
|
|
339
|
-
|
|
340
|
-
isHeadersSent() {
|
|
341
|
-
return this.getRes().headersSent;
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
cookie(name: string, value: string | null, opts?: TsED.SetCookieOpts) {
|
|
345
|
-
if (value === null) {
|
|
346
|
-
this.raw.clearCookie(name, opts);
|
|
347
|
-
return this;
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
this.raw.cookie(name, value, opts);
|
|
351
|
-
|
|
352
|
-
const cookie = this.raw.get("set-cookie");
|
|
353
|
-
|
|
354
|
-
if (!isArray(value)) {
|
|
355
|
-
this.raw.set("set-cookie", [].concat(cookie));
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
return this;
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
protected formatHeader(key: string, item: any) {
|
|
362
|
-
if (key.toLowerCase() === "location") {
|
|
363
|
-
// "back" is an alias for the referrer
|
|
364
|
-
if (item === "back") {
|
|
365
|
-
item = this.request.get("Referrer") || "/";
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
item = encodeUrl(String(item));
|
|
369
|
-
}
|
|
370
|
-
return item;
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
protected json(data: any) {
|
|
374
|
-
this.raw.json(data);
|
|
375
|
-
|
|
376
|
-
return this;
|
|
377
|
-
}
|
|
378
|
-
|
|
379
|
-
protected buffer(data: any) {
|
|
380
|
-
if (!this.getContentType()) {
|
|
381
|
-
this.contentType("application/octet-stream");
|
|
382
|
-
}
|
|
383
|
-
|
|
384
|
-
this.contentLength(data.length);
|
|
385
|
-
this.end(data);
|
|
386
|
-
}
|
|
387
|
-
|
|
388
|
-
protected end(data?: string | Buffer) {
|
|
389
|
-
this.raw.send(data);
|
|
390
|
-
}
|
|
391
|
-
}
|
|
392
|
-
|
|
393
|
-
injectable(PlatformResponse).scope(ProviderScope.INSTANCE);
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import {closeServer} from "./closeServer.js";
|
|
2
|
-
|
|
3
|
-
describe("closeServer", () => {
|
|
4
|
-
it("should close the server", async () => {
|
|
5
|
-
const server: any = {
|
|
6
|
-
close: vi.fn().mockImplementation((cb) => cb())
|
|
7
|
-
};
|
|
8
|
-
expect(await closeServer(server)).toEqual(undefined);
|
|
9
|
-
expect(server.close).toHaveBeenCalledWith(expect.any(Function));
|
|
10
|
-
});
|
|
11
|
-
});
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import type Http from "node:http";
|
|
2
|
-
import type Https from "node:https";
|
|
3
|
-
|
|
4
|
-
import type Http2 from "http2";
|
|
5
|
-
|
|
6
|
-
export function closeServer(server: Http.Server | Https.Server | Http2.Http2Server) {
|
|
7
|
-
return new Promise((resolve) => server.close(() => resolve(undefined)));
|
|
8
|
-
}
|
|
@@ -1,111 +0,0 @@
|
|
|
1
|
-
import {configuration, injector} from "@tsed/di";
|
|
2
|
-
import {$asyncEmit} from "@tsed/hooks";
|
|
3
|
-
|
|
4
|
-
import {PlatformTest} from "../../testing/PlatformTest.js";
|
|
5
|
-
import {PlatformResponse} from "../services/PlatformResponse.js";
|
|
6
|
-
import {createContext} from "./createContext.js";
|
|
7
|
-
|
|
8
|
-
vi.mock("@tsed/hooks", async (importOriginal) => {
|
|
9
|
-
const mod = await importOriginal<typeof import("@tsed/hooks")>();
|
|
10
|
-
return {
|
|
11
|
-
...mod,
|
|
12
|
-
$asyncEmit: vi.fn()
|
|
13
|
-
};
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
async function createContextFixture(reqOpts?: any) {
|
|
17
|
-
const request = PlatformTest.createRequest(reqOpts);
|
|
18
|
-
const response = PlatformTest.createResponse();
|
|
19
|
-
|
|
20
|
-
configuration().logger.level = "info";
|
|
21
|
-
configuration().logger.ignoreUrlPatterns = ["/admin", /\/admin2/];
|
|
22
|
-
|
|
23
|
-
const invoke = createContext();
|
|
24
|
-
const ctx = await invoke({request, response});
|
|
25
|
-
ctx.response.getRes().on = vi.fn();
|
|
26
|
-
|
|
27
|
-
ctx.response.onEnd(() => ctx.finish());
|
|
28
|
-
|
|
29
|
-
const call = async () => {
|
|
30
|
-
await ctx.start();
|
|
31
|
-
|
|
32
|
-
ctx.logger.info({event: "test"});
|
|
33
|
-
ctx.logger.flush();
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
return {call, injector: injector(), request, response, ctx};
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
describe("createContext", () => {
|
|
40
|
-
beforeEach(() => PlatformTest.create());
|
|
41
|
-
afterEach(() => PlatformTest.reset());
|
|
42
|
-
afterEach(() => {
|
|
43
|
-
vi.resetAllMocks();
|
|
44
|
-
});
|
|
45
|
-
it("should create context and attach it to the request", async () => {
|
|
46
|
-
// GIVEN
|
|
47
|
-
const {injector, ctx, call} = await createContextFixture();
|
|
48
|
-
|
|
49
|
-
vi.mocked($asyncEmit).mockResolvedValue(undefined);
|
|
50
|
-
vi.spyOn(injector.logger, "info").mockReturnValue(undefined);
|
|
51
|
-
|
|
52
|
-
// WHEN
|
|
53
|
-
await call();
|
|
54
|
-
|
|
55
|
-
// THEN
|
|
56
|
-
expect($asyncEmit).toHaveBeenCalledWith("$onRequest", [ctx]);
|
|
57
|
-
|
|
58
|
-
await vi.mocked(ctx.response.getRes().on).mock.calls[0][1](ctx);
|
|
59
|
-
|
|
60
|
-
expect($asyncEmit).toHaveBeenCalledWith("$onResponse", [ctx]);
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
it("should ignore logs", async () => {
|
|
64
|
-
// GIVEN
|
|
65
|
-
const {injector, call} = await createContextFixture({
|
|
66
|
-
url: "/admin",
|
|
67
|
-
originalUrl: "/admin"
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
vi.mocked($asyncEmit).mockResolvedValue(undefined);
|
|
71
|
-
vi.spyOn(injector.logger, "info").mockReturnValue(undefined);
|
|
72
|
-
|
|
73
|
-
// WHEN
|
|
74
|
-
await call();
|
|
75
|
-
|
|
76
|
-
// THEN
|
|
77
|
-
expect(injector.logger.info).toHaveBeenCalledTimes(0);
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
it("should add a x-request-id header to the response", async () => {
|
|
81
|
-
// GIVEN
|
|
82
|
-
vi.spyOn(PlatformResponse.prototype, "setHeader");
|
|
83
|
-
|
|
84
|
-
const {ctx, response, request, call} = await createContextFixture();
|
|
85
|
-
response.req = request;
|
|
86
|
-
|
|
87
|
-
// WHEN
|
|
88
|
-
await call();
|
|
89
|
-
|
|
90
|
-
// THEN
|
|
91
|
-
expect(ctx.response.setHeader).toHaveBeenCalledWith("x-request-id", expect.any(String));
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
it("should use an existing x-request-id request header for the response x-request-id header", async () => {
|
|
95
|
-
// GIVEN
|
|
96
|
-
vi.spyOn(PlatformResponse.prototype, "setHeader");
|
|
97
|
-
|
|
98
|
-
const {ctx, response, request, call} = await createContextFixture({
|
|
99
|
-
headers: {
|
|
100
|
-
"x-request-id": "test-id"
|
|
101
|
-
}
|
|
102
|
-
});
|
|
103
|
-
response.req = request;
|
|
104
|
-
|
|
105
|
-
// WHEN
|
|
106
|
-
await call();
|
|
107
|
-
|
|
108
|
-
// THEN
|
|
109
|
-
expect(ctx.response.setHeader).toHaveBeenCalledWith("x-request-id", "test-id");
|
|
110
|
-
});
|
|
111
|
-
});
|
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
import {configuration, injector} from "@tsed/di";
|
|
2
|
-
import {v4} from "uuid";
|
|
3
|
-
|
|
4
|
-
import {PlatformContext} from "../domain/PlatformContext.js";
|
|
5
|
-
import {IncomingEvent} from "../interfaces/IncomingEvent.js";
|
|
6
|
-
import {PlatformRequest} from "../services/PlatformRequest.js";
|
|
7
|
-
import {PlatformResponse} from "../services/PlatformResponse.js";
|
|
8
|
-
|
|
9
|
-
function defaultReqIdBuilder(req: any) {
|
|
10
|
-
return req.get?.("x-request-id") || v4().split("-").join("");
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
function mapIgnoreUrlPatterns(ignoreUrlPatterns: any[]) {
|
|
14
|
-
return ignoreUrlPatterns.map((pattern: string | RegExp) => (typeof pattern === "string" ? new RegExp(pattern, "gi") : pattern));
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export function buildIgnoreLog(ignoreUrlPatterns: any[] | undefined) {
|
|
18
|
-
if (ignoreUrlPatterns) {
|
|
19
|
-
ignoreUrlPatterns = mapIgnoreUrlPatterns(ignoreUrlPatterns);
|
|
20
|
-
return (ignore: boolean, data: any, url: string) => ignoreUrlPatterns?.find((reg) => !!url.match(reg));
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Create the TsED context to wrap request, response, injector, etc...
|
|
26
|
-
* @ignore
|
|
27
|
-
*/
|
|
28
|
-
export function createContext(): (event: IncomingEvent) => PlatformContext {
|
|
29
|
-
const ResponseKlass = injector().getProvider(PlatformResponse)?.useClass;
|
|
30
|
-
const RequestKlass = injector().getProvider(PlatformRequest)?.useClass;
|
|
31
|
-
const {reqIdBuilder = defaultReqIdBuilder, ...loggerOptions} = configuration().logger;
|
|
32
|
-
|
|
33
|
-
const opts = {
|
|
34
|
-
...loggerOptions,
|
|
35
|
-
ResponseKlass,
|
|
36
|
-
RequestKlass
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
const ignoreLog = buildIgnoreLog(loggerOptions.ignoreUrlPatterns);
|
|
40
|
-
|
|
41
|
-
return function invokeContext(event: IncomingEvent) {
|
|
42
|
-
const $ctx = new PlatformContext({
|
|
43
|
-
...opts,
|
|
44
|
-
event,
|
|
45
|
-
id: reqIdBuilder(event.request)
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
ignoreLog && $ctx.logger.alterIgnoreLog((ignore, data) => ignoreLog(ignore, data, $ctx.url));
|
|
49
|
-
|
|
50
|
-
return $ctx;
|
|
51
|
-
};
|
|
52
|
-
}
|
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
import Http from "node:http";
|
|
2
|
-
|
|
3
|
-
import {configuration, destroyInjector, injector, InjectorService, logger} from "@tsed/di";
|
|
4
|
-
|
|
5
|
-
import {createHttpServer} from "./createHttpServer.js";
|
|
6
|
-
|
|
7
|
-
describe("createHttpServer", () => {
|
|
8
|
-
afterEach(() => {
|
|
9
|
-
vi.resetAllMocks();
|
|
10
|
-
});
|
|
11
|
-
afterEach(() => destroyInjector());
|
|
12
|
-
it("should create an instance of Http (http port true)", async () => {
|
|
13
|
-
configuration().set("httpPort", true);
|
|
14
|
-
|
|
15
|
-
const fn: any = vi.fn();
|
|
16
|
-
|
|
17
|
-
const listener: any = createHttpServer(fn);
|
|
18
|
-
|
|
19
|
-
expect(!!injector().get(Http.Server)).toEqual(true);
|
|
20
|
-
|
|
21
|
-
expect(listener).toBeInstanceOf(Function);
|
|
22
|
-
|
|
23
|
-
const server = injector().get(Http.Server)!;
|
|
24
|
-
|
|
25
|
-
vi.spyOn(logger(), "info").mockReturnValue(undefined);
|
|
26
|
-
vi.spyOn(logger(), "debug").mockReturnValue(undefined);
|
|
27
|
-
vi.spyOn(server, "listen").mockReturnValue(undefined as never);
|
|
28
|
-
vi.spyOn(server, "address").mockReturnValue({port: 8089, address: "0.0.0.0"} as never);
|
|
29
|
-
vi.spyOn(server, "on").mockImplementation(((event: string, cb: any) => {
|
|
30
|
-
if (event === "listening") {
|
|
31
|
-
cb();
|
|
32
|
-
}
|
|
33
|
-
}) as never);
|
|
34
|
-
|
|
35
|
-
await listener();
|
|
36
|
-
|
|
37
|
-
expect(server.listen).toHaveBeenCalledWith(true, "0.0.0.0");
|
|
38
|
-
expect(logger().info).toHaveBeenCalledWith("Listen server on http://0.0.0.0:8089");
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
it("should create a raw object (http port false)", () => {
|
|
42
|
-
configuration().set("httpPort", false);
|
|
43
|
-
|
|
44
|
-
const fn: any = vi.fn();
|
|
45
|
-
|
|
46
|
-
const listener = createHttpServer(fn);
|
|
47
|
-
|
|
48
|
-
expect(injector().get(Http.Server)).toEqual(null);
|
|
49
|
-
|
|
50
|
-
expect(listener).toBeUndefined();
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
it("should create an instance of Http (http port 0)", () => {
|
|
54
|
-
configuration().set("httpPort", 0);
|
|
55
|
-
|
|
56
|
-
const fn: any = vi.fn();
|
|
57
|
-
|
|
58
|
-
const listener = createHttpServer(fn);
|
|
59
|
-
|
|
60
|
-
expect(!!injector().get(Http.Server)).toEqual(true);
|
|
61
|
-
|
|
62
|
-
expect(listener).toBeInstanceOf(Function);
|
|
63
|
-
});
|
|
64
|
-
it("should create an instance of Http (http port + address)", () => {
|
|
65
|
-
configuration().set("httpPort", "0.0.0.0:8080");
|
|
66
|
-
|
|
67
|
-
const fn: any = vi.fn();
|
|
68
|
-
|
|
69
|
-
const listener = createHttpServer(fn);
|
|
70
|
-
|
|
71
|
-
expect(!!injector().get(Http.Server)).toEqual(true);
|
|
72
|
-
|
|
73
|
-
expect(listener).toBeInstanceOf(Function);
|
|
74
|
-
});
|
|
75
|
-
});
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import Http from "node:http";
|
|
2
|
-
|
|
3
|
-
import {configuration, constant} from "@tsed/di";
|
|
4
|
-
|
|
5
|
-
import {createServer} from "./createServer.js";
|
|
6
|
-
|
|
7
|
-
export function createHttpServer(requestListener: Http.RequestListener) {
|
|
8
|
-
const httpOptions = configuration().get<Http.ServerOptions>("httpOptions");
|
|
9
|
-
|
|
10
|
-
return createServer({
|
|
11
|
-
port: constant<string | false>("httpPort"),
|
|
12
|
-
type: "http",
|
|
13
|
-
token: Http.Server,
|
|
14
|
-
server: () => Http.createServer(httpOptions, requestListener)
|
|
15
|
-
});
|
|
16
|
-
}
|