@deepkit/http 1.0.15 → 1.0.19
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/CHANGELOG.md +22 -0
- package/dist/cjs/src/controllers.js +1 -1
- package/dist/cjs/src/controllers.js.map +1 -1
- package/dist/cjs/src/decorator.js +8 -24
- package/dist/cjs/src/decorator.js.map +1 -1
- package/dist/cjs/src/filter.js +1 -1
- package/dist/cjs/src/filter.js.map +1 -1
- package/dist/cjs/src/http.d.ts +3 -2
- package/dist/cjs/src/http.js +20 -17
- package/dist/cjs/src/http.js.map +1 -1
- package/dist/cjs/src/kernel.d.ts +2 -2
- package/dist/cjs/src/kernel.js +11 -18
- package/dist/cjs/src/kernel.js.map +1 -1
- package/dist/cjs/src/logger.js +1 -1
- package/dist/cjs/src/logger.js.map +1 -1
- package/dist/cjs/src/middleware.js +1 -1
- package/dist/cjs/src/middleware.js.map +1 -1
- package/dist/cjs/src/model.js +2 -2
- package/dist/cjs/src/model.js.map +1 -1
- package/dist/cjs/src/request-parser.js +4 -4
- package/dist/cjs/src/request-parser.js.map +1 -1
- package/dist/cjs/src/router.d.ts +1 -0
- package/dist/cjs/src/router.js +23 -21
- package/dist/cjs/src/router.js.map +1 -1
- package/dist/cjs/src/static-serving.js +3 -6
- package/dist/cjs/src/static-serving.js.map +1 -1
- package/dist/cjs/tsconfig.tsbuildinfo +1 -1
- package/dist/esm/src/decorator.js +8 -24
- package/dist/esm/src/decorator.js.map +1 -1
- package/dist/esm/src/http.d.ts +3 -2
- package/dist/esm/src/http.js +21 -18
- package/dist/esm/src/http.js.map +1 -1
- package/dist/esm/src/kernel.d.ts +2 -2
- package/dist/esm/src/kernel.js +12 -19
- package/dist/esm/src/kernel.js.map +1 -1
- package/dist/esm/src/model.js +2 -2
- package/dist/esm/src/model.js.map +1 -1
- package/dist/esm/src/request-parser.js +4 -4
- package/dist/esm/src/request-parser.js.map +1 -1
- package/dist/esm/src/router.d.ts +1 -0
- package/dist/esm/src/router.js +25 -23
- package/dist/esm/src/router.js.map +1 -1
- package/dist/esm/src/static-serving.js +3 -6
- package/dist/esm/src/static-serving.js.map +1 -1
- package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
- package/package.json +11 -11
- package/src/http.ts +14 -17
- package/src/kernel.ts +8 -21
- package/src/router.ts +12 -16
- package/src/static-serving.ts +1 -7
- package/tsconfig.json +1 -0
package/src/kernel.ts
CHANGED
|
@@ -2,14 +2,7 @@ import { InjectorContext, Setter } from '@deepkit/injector';
|
|
|
2
2
|
import { HttpRouter } from './router.js';
|
|
3
3
|
import { EventDispatcher } from '@deepkit/event';
|
|
4
4
|
import { LoggerInterface } from '@deepkit/logger';
|
|
5
|
-
import {
|
|
6
|
-
HttpRequest,
|
|
7
|
-
HttpResponse,
|
|
8
|
-
incomingMessageToHttpRequest,
|
|
9
|
-
MemoryHttpResponse,
|
|
10
|
-
RequestBuilder,
|
|
11
|
-
serverResponseToHttpResponse,
|
|
12
|
-
} from './model.js';
|
|
5
|
+
import { HttpRequest, HttpResponse, incomingMessageToHttpRequest, MemoryHttpResponse, RequestBuilder, serverResponseToHttpResponse } from './model.js';
|
|
13
6
|
import { HttpError, HttpRequestEvent, HttpResultFormatter, httpWorkflow, JSONResponse } from './http.js';
|
|
14
7
|
import { FrameCategory, Stopwatch } from '@deepkit/stopwatch';
|
|
15
8
|
import { unlink } from 'fs';
|
|
@@ -43,7 +36,7 @@ export class HttpKernel {
|
|
|
43
36
|
protected eventDispatcher: EventDispatcher,
|
|
44
37
|
protected injectorContext: InjectorContext,
|
|
45
38
|
protected logger: LoggerInterface,
|
|
46
|
-
protected stopwatch
|
|
39
|
+
protected stopwatch: Stopwatch,
|
|
47
40
|
) {
|
|
48
41
|
this.setHttpRequest = injectorContext.setter(undefined, HttpRequest);
|
|
49
42
|
this.setHttpResponse = injectorContext.setter(undefined, HttpResponse);
|
|
@@ -112,16 +105,12 @@ export class HttpKernel {
|
|
|
112
105
|
|
|
113
106
|
req.throwErrorOnNotFound = options.throwOnNotFound || false;
|
|
114
107
|
|
|
115
|
-
const frame = this.stopwatch
|
|
116
|
-
const workflow = httpWorkflow.create('start', this.eventDispatcher,
|
|
108
|
+
const frame = this.stopwatch.start(req.method + ' ' + req.getUrl(), FrameCategory.http, true);
|
|
109
|
+
const workflow = httpWorkflow.create('start', this.eventDispatcher, this.stopwatch, httpInjectorContext);
|
|
117
110
|
|
|
118
111
|
try {
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
await frame.run(() => workflow.apply('request', new HttpRequestEvent(httpInjectorContext, req, res)));
|
|
122
|
-
} else {
|
|
123
|
-
await workflow.apply('request', new HttpRequestEvent(httpInjectorContext, req, res));
|
|
124
|
-
}
|
|
112
|
+
frame.data({ url: req.getUrl(), method: req.getMethod(), clientIp: req.getRemoteAddress() });
|
|
113
|
+
await frame.run(() => workflow.apply('request', new HttpRequestEvent(httpInjectorContext, req, res)));
|
|
125
114
|
} catch (error: any) {
|
|
126
115
|
if (!res.headersSent) {
|
|
127
116
|
const resultFormatter = httpInjectorContext.get(HttpResultFormatter);
|
|
@@ -151,10 +140,8 @@ export class HttpKernel {
|
|
|
151
140
|
});
|
|
152
141
|
}
|
|
153
142
|
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
frame.end();
|
|
157
|
-
}
|
|
143
|
+
frame.data({ responseStatus: res.statusCode });
|
|
144
|
+
frame.end();
|
|
158
145
|
}
|
|
159
146
|
}
|
|
160
147
|
}
|
package/src/router.ts
CHANGED
|
@@ -8,18 +8,7 @@
|
|
|
8
8
|
* You should have received a copy of the MIT License along with this program.
|
|
9
9
|
*/
|
|
10
10
|
import { ClassType, CompilerContext, getClassName, isArray, isClass, urlJoin } from '@deepkit/core';
|
|
11
|
-
import {
|
|
12
|
-
entity,
|
|
13
|
-
ReflectionClass,
|
|
14
|
-
ReflectionFunction,
|
|
15
|
-
ReflectionKind,
|
|
16
|
-
ReflectionParameter,
|
|
17
|
-
SerializationOptions,
|
|
18
|
-
serializer,
|
|
19
|
-
Serializer,
|
|
20
|
-
Type,
|
|
21
|
-
ValidationError,
|
|
22
|
-
} from '@deepkit/type';
|
|
11
|
+
import { entity, ReflectionClass, ReflectionFunction, ReflectionKind, ReflectionParameter, SerializationOptions, serializer, Serializer, Type, ValidationError } from '@deepkit/type';
|
|
23
12
|
import { getActions, HttpAction, httpClass, HttpController, HttpDecorator } from './decorator.js';
|
|
24
13
|
import { HttpRequest, HttpRequestPositionedParameters, HttpRequestQuery, HttpRequestResolvedParameters } from './model.js';
|
|
25
14
|
import { InjectorContext, InjectorModule } from '@deepkit/injector';
|
|
@@ -30,7 +19,7 @@ import { HttpMiddlewareConfig, HttpMiddlewareFn } from './middleware.js';
|
|
|
30
19
|
|
|
31
20
|
//@ts-ignore
|
|
32
21
|
import qs from 'qs';
|
|
33
|
-
import { HtmlResponse, JSONResponse, Response } from './http.js';
|
|
22
|
+
import { HtmlResponse, JSONResponse, Redirect, Response } from './http.js';
|
|
34
23
|
import { getRequestParserCodeForParameters, ParameterForRequestParser, parseRoutePathToRegex } from './request-parser.js';
|
|
35
24
|
import { HttpConfig } from './module.config.js';
|
|
36
25
|
|
|
@@ -444,6 +433,13 @@ export abstract class HttpRouterRegistryFunctionRegistrar {
|
|
|
444
433
|
this.register(convertOptions(['HEAD'], pathOrOptions, this.defaultOptions), callback);
|
|
445
434
|
}
|
|
446
435
|
|
|
436
|
+
public redirectCallback(pathOrOptions: string | HttpRouterFunctionOptions, callback: (request: HttpRequest) => string, redirectCode: number = 302) {
|
|
437
|
+
const options = convertOptions(['GET'], pathOrOptions, this.defaultOptions);
|
|
438
|
+
this.register(options, (request: HttpRequest) => {
|
|
439
|
+
return Redirect.toUrl(callback(request), redirectCode);
|
|
440
|
+
});
|
|
441
|
+
}
|
|
442
|
+
|
|
447
443
|
private register(options: HttpRouterFunctionOptions, callback: (...args: any[]) => any, module?: InjectorModule<any>) {
|
|
448
444
|
const fn = ReflectionFunction.from(callback);
|
|
449
445
|
|
|
@@ -536,7 +532,7 @@ export class HttpRouterRegistry extends HttpRouterRegistryFunctionRegistrar {
|
|
|
536
532
|
type: 'controller',
|
|
537
533
|
controller,
|
|
538
534
|
module,
|
|
539
|
-
methodName: action.methodName
|
|
535
|
+
methodName: action.methodName,
|
|
540
536
|
};
|
|
541
537
|
const routeConfig = createRouteConfigFromHttpAction(routeAction, action, module, controllerData);
|
|
542
538
|
|
|
@@ -578,7 +574,7 @@ export class HttpRouter {
|
|
|
578
574
|
controllers: (ClassType | { module: InjectorModule<any>, controller: ClassType })[],
|
|
579
575
|
middlewareRegistry: MiddlewareRegistry = new MiddlewareRegistry(),
|
|
580
576
|
module: InjectorModule<any> = new InjectorModule(),
|
|
581
|
-
config: HttpConfig = new HttpConfig()
|
|
577
|
+
config: HttpConfig = new HttpConfig(),
|
|
582
578
|
): HttpRouter {
|
|
583
579
|
return new this(new HttpControllers(controllers.map(v => {
|
|
584
580
|
return isClass(v) ? { controller: v, module } : v;
|
|
@@ -636,7 +632,7 @@ export class HttpRouter {
|
|
|
636
632
|
resolverForToken: routeConfig.resolverForToken,
|
|
637
633
|
resolverForParameterName: routeConfig.resolverForParameterName,
|
|
638
634
|
pathParameterNames: parsedRoute.pathParameterNames,
|
|
639
|
-
routeConfig
|
|
635
|
+
routeConfig,
|
|
640
636
|
});
|
|
641
637
|
|
|
642
638
|
let methodCheck = '';
|
package/src/static-serving.ts
CHANGED
|
@@ -49,13 +49,7 @@ export async function staticOnRoute(
|
|
|
49
49
|
return new Promise(resolve => {
|
|
50
50
|
stat(finalLocalPath, (err, stat) => {
|
|
51
51
|
if (stat && stat.isFile()) {
|
|
52
|
-
event.
|
|
53
|
-
new RouteConfig('static', ['GET'], event.url, {
|
|
54
|
-
type: 'function',
|
|
55
|
-
fn: staticServe,
|
|
56
|
-
}),
|
|
57
|
-
() => ({ arguments: [localPath, relativePath, event.request, event.response], parameters: {} }),
|
|
58
|
-
);
|
|
52
|
+
event.routeFoundCallback(staticServe, [localPath, relativePath, event.request, event.response]);
|
|
59
53
|
}
|
|
60
54
|
resolve(undefined);
|
|
61
55
|
});
|