@deepkit/http 1.0.1-alpha.16 → 1.0.1-alpha.160
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 +236 -0
- package/dist/cjs/index.d.ts +14 -10
- package/dist/cjs/index.js +19 -11
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/src/controllers.d.ts +10 -3
- package/dist/cjs/src/controllers.js +7 -3
- package/dist/cjs/src/controllers.js.map +1 -1
- package/dist/cjs/src/decorator.d.ts +144 -70
- package/dist/cjs/src/decorator.js +277 -102
- package/dist/cjs/src/decorator.js.map +1 -1
- package/dist/cjs/src/filter.d.ts +49 -0
- package/dist/cjs/src/filter.js +215 -0
- package/dist/cjs/src/filter.js.map +1 -0
- package/dist/cjs/src/http-app.d.ts +8 -0
- package/dist/cjs/src/http-app.js +16 -0
- package/dist/cjs/src/http-app.js.map +1 -0
- package/dist/cjs/src/http.d.ts +131 -32
- package/dist/cjs/src/http.js +523 -122
- package/dist/cjs/src/http.js.map +1 -1
- package/dist/cjs/src/kernel.d.ts +50 -9
- package/dist/cjs/src/kernel.js +109 -98
- package/dist/cjs/src/kernel.js.map +1 -1
- package/dist/cjs/src/logger.d.ts +3 -3
- package/dist/cjs/src/logger.js +9 -12
- package/dist/cjs/src/logger.js.map +1 -1
- package/dist/cjs/src/middleware.d.ts +61 -0
- package/dist/cjs/src/middleware.js +95 -0
- package/dist/cjs/src/middleware.js.map +1 -0
- package/dist/cjs/src/model.d.ts +270 -4
- package/dist/cjs/src/model.js +401 -3
- package/dist/cjs/src/model.js.map +1 -1
- package/dist/cjs/src/module.config.d.ts +149 -4
- package/dist/cjs/src/module.config.js +11 -6
- package/dist/cjs/src/module.config.js.map +1 -1
- package/dist/cjs/src/module.d.ts +14 -32
- package/dist/cjs/src/module.js +122 -7
- package/dist/cjs/src/module.js.map +1 -1
- package/dist/cjs/src/request-parser.d.ts +37 -0
- package/dist/cjs/src/request-parser.js +382 -0
- package/dist/cjs/src/request-parser.js.map +1 -0
- package/dist/cjs/src/router.d.ts +174 -75
- package/dist/cjs/src/router.js +469 -288
- package/dist/cjs/src/router.js.map +1 -1
- package/dist/cjs/src/static-serving.d.ts +32 -4
- package/dist/cjs/src/static-serving.js +115 -71
- package/dist/cjs/src/static-serving.js.map +1 -1
- package/dist/cjs/src/utils.js +2 -2
- package/dist/cjs/src/utils.js.map +1 -1
- package/dist/cjs/tsconfig.tsbuildinfo +1 -2916
- package/dist/esm/index.d.ts +14 -10
- package/dist/esm/index.js +14 -10
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/src/controllers.d.ts +10 -3
- package/dist/esm/src/controllers.js +7 -3
- package/dist/esm/src/controllers.js.map +1 -1
- package/dist/esm/src/decorator.d.ts +144 -70
- package/dist/esm/src/decorator.js +272 -99
- package/dist/esm/src/decorator.js.map +1 -1
- package/dist/esm/src/filter.d.ts +49 -0
- package/dist/esm/src/filter.js +209 -0
- package/dist/esm/src/filter.js.map +1 -0
- package/dist/esm/src/http-app.d.ts +8 -0
- package/dist/esm/src/http-app.js +12 -0
- package/dist/esm/src/http-app.js.map +1 -0
- package/dist/esm/src/http.d.ts +131 -32
- package/dist/esm/src/http.js +504 -121
- package/dist/esm/src/http.js.map +1 -1
- package/dist/esm/src/kernel.d.ts +50 -9
- package/dist/esm/src/kernel.js +110 -100
- package/dist/esm/src/kernel.js.map +1 -1
- package/dist/esm/src/logger.d.ts +3 -3
- package/dist/esm/src/logger.js +7 -11
- package/dist/esm/src/logger.js.map +1 -1
- package/dist/esm/src/middleware.d.ts +61 -0
- package/dist/esm/src/middleware.js +90 -0
- package/dist/esm/src/middleware.js.map +1 -0
- package/dist/esm/src/model.d.ts +270 -4
- package/dist/esm/src/model.js +357 -1
- package/dist/esm/src/model.js.map +1 -1
- package/dist/esm/src/module.config.d.ts +149 -4
- package/dist/esm/src/module.config.js +9 -5
- package/dist/esm/src/module.config.js.map +1 -1
- package/dist/esm/src/module.d.ts +14 -32
- package/dist/esm/src/module.js +121 -7
- package/dist/esm/src/module.js.map +1 -1
- package/dist/esm/src/request-parser.d.ts +37 -0
- package/dist/esm/src/request-parser.js +372 -0
- package/dist/esm/src/request-parser.js.map +1 -0
- package/dist/esm/src/router.d.ts +174 -75
- package/dist/esm/src/router.js +463 -285
- package/dist/esm/src/router.js.map +1 -1
- package/dist/esm/src/static-serving.d.ts +32 -4
- package/dist/esm/src/static-serving.js +107 -65
- package/dist/esm/src/static-serving.js.map +1 -1
- package/dist/esm/src/utils.js +1 -0
- package/dist/esm/src/utils.js.map +1 -1
- package/dist/esm/tsconfig.esm.tsbuildinfo +1 -2916
- package/index.ts +14 -10
- package/package.json +30 -19
- package/src/controllers.ts +4 -3
- package/src/decorator.ts +242 -104
- package/src/filter.ts +224 -0
- package/src/http-app.ts +11 -0
- package/src/http.ts +501 -100
- package/src/kernel.ts +128 -80
- package/src/logger.ts +4 -6
- package/src/middleware.ts +116 -0
- package/src/model.ts +516 -5
- package/src/module.config.ts +168 -5
- package/src/module.ts +120 -7
- package/src/request-parser.ts +430 -0
- package/src/router.ts +546 -317
- package/src/static-serving.ts +120 -50
- package/src/utils.ts +0 -2
- package/tsconfig.esm.json +7 -7
- package/tsconfig.json +18 -11
- package/tsconfig.spec.json +8 -0
- package/dist/cjs/tests/router.spec.d.ts +0 -1
- package/dist/cjs/tests/router.spec.js +0 -481
- package/dist/cjs/tests/router.spec.js.map +0 -1
- package/dist/esm/tests/router.spec.d.ts +0 -1
- package/dist/esm/tests/router.spec.js +0 -479
- package/dist/esm/tests/router.spec.js.map +0 -1
- package/tests/router.spec.ts +0 -347
package/src/static-serving.ts
CHANGED
|
@@ -8,54 +8,65 @@
|
|
|
8
8
|
* You should have received a copy of the MIT License along with this program.
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
|
-
import { http } from './decorator';
|
|
11
|
+
import { http } from './decorator.js';
|
|
12
12
|
import { join } from 'path';
|
|
13
13
|
import { readFileSync, stat } from 'fs';
|
|
14
|
-
import { HtmlResponse, httpWorkflow } from './http';
|
|
14
|
+
import { HtmlResponse, httpWorkflow } from './http.js';
|
|
15
15
|
import { AppModule } from '@deepkit/app';
|
|
16
|
-
import { normalizeDirectory } from './utils';
|
|
17
|
-
import { ClassType } from '@deepkit/core';
|
|
18
|
-
import {
|
|
19
|
-
import { HttpRequest, HttpResponse } from './model';
|
|
16
|
+
import { normalizeDirectory } from './utils.js';
|
|
17
|
+
import { ClassType, urlJoin } from '@deepkit/core';
|
|
18
|
+
import { HttpRequest, HttpResponse } from './model.js';
|
|
20
19
|
import send from 'send';
|
|
21
20
|
import { eventDispatcher } from '@deepkit/event';
|
|
22
|
-
import { RouteConfig } from './router';
|
|
21
|
+
import { HttpRouter, RouteConfig } from './router.js';
|
|
23
22
|
|
|
24
|
-
export function
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
23
|
+
export function staticServe(
|
|
24
|
+
localPath: string,
|
|
25
|
+
path: string,
|
|
26
|
+
request: HttpRequest,
|
|
27
|
+
response: HttpResponse,
|
|
28
|
+
) {
|
|
29
|
+
return new Promise((resolve, reject) => {
|
|
30
|
+
const res = send(request, path, { root: localPath });
|
|
31
|
+
res.pipe(response);
|
|
32
|
+
res.on('end', resolve);
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export async function staticOnRoute(
|
|
37
|
+
event: typeof httpWorkflow.onRoute.event,
|
|
38
|
+
path: string,
|
|
39
|
+
localPath: string,
|
|
40
|
+
) {
|
|
41
|
+
if (event.sent) return;
|
|
42
|
+
if (event.route) return;
|
|
34
43
|
|
|
44
|
+
if (!event.request.url?.startsWith(path)) return;
|
|
45
|
+
|
|
46
|
+
const relativePath = urlJoin('/', event.url.substr(path.length));
|
|
47
|
+
const finalLocalPath = join(localPath, relativePath);
|
|
48
|
+
|
|
49
|
+
return new Promise(resolve => {
|
|
50
|
+
stat(finalLocalPath, (err, stat) => {
|
|
51
|
+
if (stat && stat.isFile()) {
|
|
52
|
+
event.routeFound(
|
|
53
|
+
new RouteConfig('static', ['GET'], event.url, {
|
|
54
|
+
type: 'function',
|
|
55
|
+
fn: staticServe,
|
|
56
|
+
}),
|
|
57
|
+
() => ({ arguments: [localPath, relativePath, event.request, event.response], parameters: {} }),
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
resolve(undefined);
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function serveStaticListener(module: AppModule<any>, path: string, localPath: string = path): ClassType {
|
|
66
|
+
class HttpRequestStaticServingListener {
|
|
35
67
|
@eventDispatcher.listen(httpWorkflow.onRoute, 101) //after default route listener at 100
|
|
36
68
|
onRoute(event: typeof httpWorkflow.onRoute.event) {
|
|
37
|
-
|
|
38
|
-
if (event.route) return;
|
|
39
|
-
|
|
40
|
-
if (!event.request.url?.startsWith(path)) return;
|
|
41
|
-
|
|
42
|
-
const relativePath = join('/', event.url.substr(path.length));
|
|
43
|
-
const finalLocalPath = join(localPath, relativePath);
|
|
44
|
-
|
|
45
|
-
return new Promise(resolve => {
|
|
46
|
-
stat(finalLocalPath, (err, stat) => {
|
|
47
|
-
if (stat && stat.isFile()) {
|
|
48
|
-
event.routeFound(
|
|
49
|
-
new RouteConfig('static', 'GET', event.url, {
|
|
50
|
-
controller: HttpRequestStaticServingListener,
|
|
51
|
-
methodName: 'serve'
|
|
52
|
-
}),
|
|
53
|
-
() => [relativePath, event.request, event.response]
|
|
54
|
-
);
|
|
55
|
-
}
|
|
56
|
-
resolve(undefined);
|
|
57
|
-
});
|
|
58
|
-
});
|
|
69
|
+
return staticOnRoute(event, path, localPath);
|
|
59
70
|
}
|
|
60
71
|
}
|
|
61
72
|
|
|
@@ -72,23 +83,82 @@ function loadHtml(localPath: string, path: string): string {
|
|
|
72
83
|
}
|
|
73
84
|
}
|
|
74
85
|
|
|
86
|
+
export interface StaticHttpOptions {
|
|
87
|
+
/**
|
|
88
|
+
* The public URL path.
|
|
89
|
+
*/
|
|
90
|
+
path: string;
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* The local path from the file system. Either relative or absolute.
|
|
94
|
+
*/
|
|
95
|
+
localPath: string;
|
|
96
|
+
|
|
97
|
+
groups?: string[];
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* The controller name of the registered controller class. Is per default `StaticController`.
|
|
101
|
+
*/
|
|
102
|
+
controllerName?: string;
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Replaces strings in the served index.html file.
|
|
106
|
+
*/
|
|
107
|
+
indexReplace?: { [name: string]: string };
|
|
108
|
+
}
|
|
109
|
+
|
|
75
110
|
/**
|
|
76
|
-
* Serves
|
|
77
|
-
*
|
|
111
|
+
* Serves an index file and allows to load asset files from the same folder. Can be used to serve an angular application
|
|
112
|
+
*
|
|
113
|
+
* All paths like <path>/*.* that don't match a file are redirected to ${localPath}/index.html.
|
|
114
|
+
* All paths like <path>/*.* that match a file resolve to the file.
|
|
78
115
|
*/
|
|
79
|
-
export function registerStaticHttpController(module: AppModule<any
|
|
80
|
-
path = normalizeDirectory(path);
|
|
116
|
+
export function registerStaticHttpController(module: AppModule<any>, options: StaticHttpOptions): void {
|
|
81
117
|
let indexHtml = '';
|
|
82
118
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
119
|
+
const groups = options.groups || [];
|
|
120
|
+
|
|
121
|
+
class StaticController {
|
|
122
|
+
@(http.GET().group(...groups))
|
|
123
|
+
serveIndex(request: HttpRequest, response: HttpResponse) {
|
|
124
|
+
if (!indexHtml) {
|
|
125
|
+
indexHtml = loadHtml(options.localPath, normalizeDirectory(options.path));
|
|
126
|
+
if (options.indexReplace) {
|
|
127
|
+
for (const [k, v] of Object.entries(options.indexReplace)) {
|
|
128
|
+
indexHtml = indexHtml.replace(k, v);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
88
132
|
return indexHtml ? new HtmlResponse(indexHtml) : new HtmlResponse('Index not found', 404);
|
|
89
133
|
}
|
|
90
134
|
}
|
|
91
135
|
|
|
92
|
-
|
|
93
|
-
|
|
136
|
+
if (options.controllerName) {
|
|
137
|
+
Object.defineProperty(StaticController, 'name', { value: options.controllerName, writable: true });
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const path = normalizeDirectory(options.path);
|
|
141
|
+
|
|
142
|
+
const route1 = new RouteConfig('static', ['GET'], path, {
|
|
143
|
+
type: 'controller',
|
|
144
|
+
controller: StaticController,
|
|
145
|
+
module,
|
|
146
|
+
methodName: 'serveIndex',
|
|
147
|
+
});
|
|
148
|
+
route1.groups = groups;
|
|
149
|
+
module.configureProvider<HttpRouter>(router => router.addRoute(route1), { global: true });
|
|
150
|
+
|
|
151
|
+
if (path !== '/') {
|
|
152
|
+
const route2 = new RouteConfig('static', ['GET'], path.slice(0, -1), {
|
|
153
|
+
type: 'controller',
|
|
154
|
+
controller: StaticController,
|
|
155
|
+
module,
|
|
156
|
+
methodName: 'serveIndex',
|
|
157
|
+
});
|
|
158
|
+
route2.groups = groups;
|
|
159
|
+
module.configureProvider<HttpRouter>(router => router.addRoute(route2), { global: true });
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
module.addProvider(StaticController);
|
|
163
|
+
module.addListener(serveStaticListener(module, normalizeDirectory(options.path), options.localPath));
|
|
94
164
|
}
|
package/src/utils.ts
CHANGED
package/tsconfig.esm.json
CHANGED
|
@@ -6,31 +6,31 @@
|
|
|
6
6
|
},
|
|
7
7
|
"references": [
|
|
8
8
|
{
|
|
9
|
-
"path": "../
|
|
9
|
+
"path": "../app/tsconfig.esm.json"
|
|
10
10
|
},
|
|
11
11
|
{
|
|
12
|
-
"path": "../
|
|
12
|
+
"path": "../core/tsconfig.esm.json"
|
|
13
13
|
},
|
|
14
14
|
{
|
|
15
|
-
"path": "../
|
|
15
|
+
"path": "../event/tsconfig.esm.json"
|
|
16
16
|
},
|
|
17
17
|
{
|
|
18
18
|
"path": "../injector/tsconfig.esm.json"
|
|
19
19
|
},
|
|
20
20
|
{
|
|
21
|
-
"path": "../
|
|
21
|
+
"path": "../logger/tsconfig.esm.json"
|
|
22
22
|
},
|
|
23
23
|
{
|
|
24
|
-
"path": "../
|
|
24
|
+
"path": "../stopwatch/tsconfig.esm.json"
|
|
25
25
|
},
|
|
26
26
|
{
|
|
27
27
|
"path": "../template/tsconfig.esm.json"
|
|
28
28
|
},
|
|
29
29
|
{
|
|
30
|
-
"path": "../
|
|
30
|
+
"path": "../type/tsconfig.esm.json"
|
|
31
31
|
},
|
|
32
32
|
{
|
|
33
|
-
"path": "../
|
|
33
|
+
"path": "../workflow/tsconfig.esm.json"
|
|
34
34
|
}
|
|
35
35
|
]
|
|
36
36
|
}
|
package/tsconfig.json
CHANGED
|
@@ -8,45 +8,52 @@
|
|
|
8
8
|
"experimentalDecorators": true,
|
|
9
9
|
"emitDecoratorMetadata": true,
|
|
10
10
|
"moduleResolution": "node",
|
|
11
|
-
"target": "
|
|
11
|
+
"target": "es2020",
|
|
12
12
|
"module": "CommonJS",
|
|
13
13
|
"esModuleInterop": true,
|
|
14
14
|
"outDir": "./dist/cjs",
|
|
15
15
|
"declaration": true,
|
|
16
|
-
"composite": true
|
|
16
|
+
"composite": true,
|
|
17
|
+
"types": [
|
|
18
|
+
"node",
|
|
19
|
+
"send"
|
|
20
|
+
]
|
|
17
21
|
},
|
|
22
|
+
"reflection": true,
|
|
18
23
|
"include": [
|
|
19
24
|
"src",
|
|
20
|
-
"tests",
|
|
21
25
|
"index.ts"
|
|
22
26
|
],
|
|
27
|
+
"exclude": [
|
|
28
|
+
"tests"
|
|
29
|
+
],
|
|
23
30
|
"references": [
|
|
24
31
|
{
|
|
25
|
-
"path": "../
|
|
32
|
+
"path": "../app/tsconfig.json"
|
|
26
33
|
},
|
|
27
34
|
{
|
|
28
|
-
"path": "../
|
|
35
|
+
"path": "../core/tsconfig.json"
|
|
29
36
|
},
|
|
30
37
|
{
|
|
31
|
-
"path": "../
|
|
38
|
+
"path": "../event/tsconfig.json"
|
|
32
39
|
},
|
|
33
40
|
{
|
|
34
41
|
"path": "../injector/tsconfig.json"
|
|
35
42
|
},
|
|
36
43
|
{
|
|
37
|
-
"path": "../
|
|
44
|
+
"path": "../logger/tsconfig.json"
|
|
38
45
|
},
|
|
39
46
|
{
|
|
40
|
-
"path": "../
|
|
47
|
+
"path": "../stopwatch/tsconfig.json"
|
|
41
48
|
},
|
|
42
49
|
{
|
|
43
50
|
"path": "../template/tsconfig.json"
|
|
44
51
|
},
|
|
45
52
|
{
|
|
46
|
-
"path": "../
|
|
53
|
+
"path": "../type/tsconfig.json"
|
|
47
54
|
},
|
|
48
55
|
{
|
|
49
|
-
"path": "../
|
|
56
|
+
"path": "../workflow/tsconfig.json"
|
|
50
57
|
}
|
|
51
58
|
]
|
|
52
|
-
}
|
|
59
|
+
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import 'reflect-metadata';
|