@adonisjs/http-server 6.8.2-5 → 6.8.2-7
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 +3 -6
- package/build/factories/http_context.d.ts +10 -1
- package/build/factories/http_context.js +27 -0
- package/build/factories/http_server.d.ts +8 -0
- package/build/factories/http_server.js +26 -0
- package/build/factories/main.d.ts +0 -1
- package/build/factories/main.js +8 -0
- package/build/factories/qs_parser_factory.d.ts +10 -1
- package/build/factories/qs_parser_factory.js +18 -0
- package/build/factories/request.d.ts +10 -1
- package/build/factories/request.js +31 -0
- package/build/factories/response.d.ts +10 -1
- package/build/factories/response.js +34 -0
- package/build/factories/router.d.ts +10 -1
- package/build/factories/router.js +25 -0
- package/build/factories/server_factory.d.ts +10 -1
- package/build/factories/server_factory.js +34 -0
- package/build/index.d.ts +0 -1
- package/build/index.js +8 -0
- package/build/src/cookies/client.d.ts +25 -1
- package/build/src/cookies/client.js +42 -0
- package/build/src/cookies/drivers/encrypted.d.ts +12 -1
- package/build/src/cookies/drivers/encrypted.js +20 -0
- package/build/src/cookies/drivers/plain.d.ts +12 -1
- package/build/src/cookies/drivers/plain.js +20 -0
- package/build/src/cookies/drivers/signed.d.ts +12 -1
- package/build/src/cookies/drivers/signed.js +20 -0
- package/build/src/cookies/parser.d.ts +28 -1
- package/build/src/cookies/parser.js +98 -0
- package/build/src/cookies/serializer.d.ts +22 -1
- package/build/src/cookies/serializer.js +40 -0
- package/build/src/debug.d.ts +0 -1
- package/build/src/debug.js +8 -0
- package/build/src/define_config.d.ts +3 -1
- package/build/src/define_config.js +11 -0
- package/build/src/define_middleware.d.ts +5 -1
- package/build/src/define_middleware.js +18 -0
- package/build/src/exception_handler.d.ts +65 -1
- package/build/src/exception_handler.js +95 -0
- package/build/src/exceptions.d.ts +6 -1
- package/build/src/exceptions.js +11 -0
- package/build/src/helpers.d.ts +14 -1
- package/build/src/helpers.js +22 -0
- package/build/src/http_context/local_storage.d.ts +3 -1
- package/build/src/http_context/local_storage.js +25 -0
- package/build/src/http_context/main.d.ts +36 -1
- package/build/src/http_context/main.js +54 -0
- package/build/src/qs.d.ts +4 -1
- package/build/src/qs.js +12 -0
- package/build/src/redirect.d.ts +24 -1
- package/build/src/redirect.js +60 -0
- package/build/src/request.d.ts +466 -1
- package/build/src/request.js +542 -0
- package/build/src/response.d.ts +425 -2
- package/build/src/response.js +608 -7
- package/build/src/router/brisk.d.ts +22 -1
- package/build/src/router/brisk.js +42 -0
- package/build/src/router/executor.d.ts +4 -1
- package/build/src/router/executor.js +12 -0
- package/build/src/router/factories/use_return_value.d.ts +4 -1
- package/build/src/router/factories/use_return_value.js +16 -3
- package/build/src/router/group.d.ts +47 -1
- package/build/src/router/group.js +88 -0
- package/build/src/router/lookup_store/main.d.ts +32 -1
- package/build/src/router/lookup_store/main.js +49 -0
- package/build/src/router/lookup_store/route_finder.d.ts +13 -1
- package/build/src/router/lookup_store/route_finder.js +21 -0
- package/build/src/router/lookup_store/url_builder.d.ts +36 -1
- package/build/src/router/lookup_store/url_builder.js +97 -0
- package/build/src/router/main.d.ts +87 -1
- package/build/src/router/main.js +142 -0
- package/build/src/router/matchers.d.ts +13 -1
- package/build/src/router/matchers.js +21 -0
- package/build/src/router/parser.d.ts +3 -1
- package/build/src/router/parser.js +12 -0
- package/build/src/router/resource.d.ts +28 -1
- package/build/src/router/resource.js +90 -0
- package/build/src/router/route.d.ts +65 -1
- package/build/src/router/route.js +142 -0
- package/build/src/router/store.d.ts +54 -1
- package/build/src/router/store.js +102 -0
- package/build/src/server/factories/final_handler.d.ts +5 -1
- package/build/src/server/factories/final_handler.js +13 -0
- package/build/src/server/factories/middleware_handler.d.ts +3 -1
- package/build/src/server/factories/middleware_handler.js +11 -0
- package/build/src/server/factories/write_response.d.ts +4 -1
- package/build/src/server/factories/write_response.js +12 -0
- package/build/src/server/main.d.ts +48 -1
- package/build/src/server/main.js +128 -0
- package/build/src/types/base.d.ts +12 -1
- package/build/src/types/base.js +8 -0
- package/build/src/types/main.d.ts +0 -1
- package/build/src/types/main.js +8 -0
- package/build/src/types/middleware.d.ts +18 -1
- package/build/src/types/middleware.js +8 -0
- package/build/src/types/qs.d.ts +53 -1
- package/build/src/types/qs.js +8 -0
- package/build/src/types/request.d.ts +32 -1
- package/build/src/types/request.js +8 -0
- package/build/src/types/response.d.ts +27 -1
- package/build/src/types/response.js +8 -0
- package/build/src/types/route.d.ts +87 -1
- package/build/src/types/route.js +8 -0
- package/build/src/types/server.d.ts +35 -1
- package/build/src/types/server.js +8 -0
- package/package.json +43 -70
- package/build/factories/http_context.d.ts.map +0 -1
- package/build/factories/main.d.ts.map +0 -1
- package/build/factories/qs_parser_factory.d.ts.map +0 -1
- package/build/factories/request.d.ts.map +0 -1
- package/build/factories/response.d.ts.map +0 -1
- package/build/factories/router.d.ts.map +0 -1
- package/build/factories/server_factory.d.ts.map +0 -1
- package/build/index.d.ts.map +0 -1
- package/build/src/cookies/client.d.ts.map +0 -1
- package/build/src/cookies/drivers/encrypted.d.ts.map +0 -1
- package/build/src/cookies/drivers/plain.d.ts.map +0 -1
- package/build/src/cookies/drivers/signed.d.ts.map +0 -1
- package/build/src/cookies/parser.d.ts.map +0 -1
- package/build/src/cookies/serializer.d.ts.map +0 -1
- package/build/src/debug.d.ts.map +0 -1
- package/build/src/define_config.d.ts.map +0 -1
- package/build/src/define_middleware.d.ts.map +0 -1
- package/build/src/exception_handler.d.ts.map +0 -1
- package/build/src/exceptions.d.ts.map +0 -1
- package/build/src/helpers.d.ts.map +0 -1
- package/build/src/http_context/local_storage.d.ts.map +0 -1
- package/build/src/http_context/main.d.ts.map +0 -1
- package/build/src/qs.d.ts.map +0 -1
- package/build/src/redirect.d.ts.map +0 -1
- package/build/src/request.d.ts.map +0 -1
- package/build/src/response.d.ts.map +0 -1
- package/build/src/router/brisk.d.ts.map +0 -1
- package/build/src/router/executor.d.ts.map +0 -1
- package/build/src/router/factories/use_return_value.d.ts.map +0 -1
- package/build/src/router/group.d.ts.map +0 -1
- package/build/src/router/lookup_store/main.d.ts.map +0 -1
- package/build/src/router/lookup_store/route_finder.d.ts.map +0 -1
- package/build/src/router/lookup_store/url_builder.d.ts.map +0 -1
- package/build/src/router/main.d.ts.map +0 -1
- package/build/src/router/matchers.d.ts.map +0 -1
- package/build/src/router/parser.d.ts.map +0 -1
- package/build/src/router/resource.d.ts.map +0 -1
- package/build/src/router/route.d.ts.map +0 -1
- package/build/src/router/store.d.ts.map +0 -1
- package/build/src/server/factories/final_handler.d.ts.map +0 -1
- package/build/src/server/factories/middleware_handler.d.ts.map +0 -1
- package/build/src/server/factories/write_response.d.ts.map +0 -1
- package/build/src/server/main.d.ts.map +0 -1
- package/build/src/types/base.d.ts.map +0 -1
- package/build/src/types/main.d.ts.map +0 -1
- package/build/src/types/middleware.d.ts.map +0 -1
- package/build/src/types/qs.d.ts.map +0 -1
- package/build/src/types/request.d.ts.map +0 -1
- package/build/src/types/response.d.ts.map +0 -1
- package/build/src/types/route.d.ts.map +0 -1
- package/build/src/types/server.d.ts.map +0 -1
- package/factories/http_context.ts +0 -73
- package/factories/main.ts +0 -15
- package/factories/qs_parser_factory.ts +0 -54
- package/factories/request.ts +0 -101
- package/factories/response.ts +0 -106
- package/factories/router.ts +0 -61
- package/factories/server_factory.ts +0 -94
- package/index.ts +0 -23
- package/src/cookies/client.ts +0 -98
- package/src/cookies/drivers/encrypted.ts +0 -42
- package/src/cookies/drivers/plain.ts +0 -37
- package/src/cookies/drivers/signed.ts +0 -42
- package/src/cookies/parser.ts +0 -196
- package/src/cookies/serializer.ts +0 -98
- package/src/debug.ts +0 -11
- package/src/define_config.ts +0 -56
- package/src/define_middleware.ts +0 -61
- package/src/exception_handler.ts +0 -290
- package/src/exceptions.ts +0 -55
- package/src/helpers.ts +0 -108
- package/src/http_context/local_storage.ts +0 -50
- package/src/http_context/main.ts +0 -126
- package/src/qs.ts +0 -31
- package/src/redirect.ts +0 -181
- package/src/request.ts +0 -982
- package/src/response.ts +0 -1421
- package/src/router/brisk.ts +0 -113
- package/src/router/executor.ts +0 -36
- package/src/router/factories/use_return_value.ts +0 -26
- package/src/router/group.ts +0 -243
- package/src/router/lookup_store/main.ts +0 -102
- package/src/router/lookup_store/route_finder.ts +0 -60
- package/src/router/lookup_store/url_builder.ts +0 -250
- package/src/router/main.ts +0 -431
- package/src/router/matchers.ts +0 -40
- package/src/router/parser.ts +0 -20
- package/src/router/resource.ts +0 -277
- package/src/router/route.ts +0 -363
- package/src/router/store.ts +0 -239
- package/src/server/factories/final_handler.ts +0 -38
- package/src/server/factories/middleware_handler.ts +0 -23
- package/src/server/factories/write_response.ts +0 -26
- package/src/server/main.ts +0 -356
- package/src/types/base.ts +0 -30
- package/src/types/main.ts +0 -16
- package/src/types/middleware.ts +0 -59
- package/src/types/qs.ts +0 -85
- package/src/types/request.ts +0 -52
- package/src/types/response.ts +0 -57
- package/src/types/route.ts +0 -217
- package/src/types/server.ts +0 -92
|
@@ -1,10 +1,50 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @adonisjs/http-server
|
|
3
|
+
*
|
|
4
|
+
* (c) AdonisJS
|
|
5
|
+
*
|
|
6
|
+
* For the full copyright and license information, please view the LICENSE
|
|
7
|
+
* file that was distributed with this source code.
|
|
8
|
+
*/
|
|
9
|
+
// @ts-expect-error
|
|
1
10
|
import matchit from '@poppinss/matchit';
|
|
2
11
|
import lodash from '@poppinss/utils/lodash';
|
|
3
12
|
import { RuntimeException } from '@poppinss/utils';
|
|
4
13
|
import { parseRoutePattern } from './parser.js';
|
|
14
|
+
/**
|
|
15
|
+
* Store class is used to store a list of routes, along side with their tokens
|
|
16
|
+
* to match the URLs.
|
|
17
|
+
*
|
|
18
|
+
* ```ts
|
|
19
|
+
* const store = new Store()
|
|
20
|
+
*
|
|
21
|
+
* store.add({
|
|
22
|
+
* pattern: 'posts/:id',
|
|
23
|
+
* handler: function onRoute () {},
|
|
24
|
+
* middleware: [],
|
|
25
|
+
* matchers: {
|
|
26
|
+
* id: '^[0-9]$+'
|
|
27
|
+
* },
|
|
28
|
+
* meta: {},
|
|
29
|
+
* methods: ['GET']
|
|
30
|
+
* })
|
|
31
|
+
*
|
|
32
|
+
* store.match('posts/1', 'GET')
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
5
35
|
export class RoutesStore {
|
|
36
|
+
/**
|
|
37
|
+
* A flag to know if routes for explicit domains
|
|
38
|
+
* have been registered
|
|
39
|
+
*/
|
|
6
40
|
usingDomains = false;
|
|
41
|
+
/**
|
|
42
|
+
* Tree of registered routes and their matchit tokens
|
|
43
|
+
*/
|
|
7
44
|
tree = { tokens: [], domains: {} };
|
|
45
|
+
/**
|
|
46
|
+
* Returns the domain node for a given domain.
|
|
47
|
+
*/
|
|
8
48
|
#getDomainNode(domain) {
|
|
9
49
|
if (!this.tree.domains[domain]) {
|
|
10
50
|
this.tree.tokens.push(parseRoutePattern(domain));
|
|
@@ -12,6 +52,9 @@ export class RoutesStore {
|
|
|
12
52
|
}
|
|
13
53
|
return this.tree.domains[domain];
|
|
14
54
|
}
|
|
55
|
+
/**
|
|
56
|
+
* Returns the method node for a given domain and method.
|
|
57
|
+
*/
|
|
15
58
|
#getMethodNode(domain, method) {
|
|
16
59
|
const domainNode = this.#getDomainNode(domain);
|
|
17
60
|
if (!domainNode[method]) {
|
|
@@ -19,6 +62,9 @@ export class RoutesStore {
|
|
|
19
62
|
}
|
|
20
63
|
return domainNode[method];
|
|
21
64
|
}
|
|
65
|
+
/**
|
|
66
|
+
* Collects route params
|
|
67
|
+
*/
|
|
22
68
|
#collectRouteParams(route, tokens) {
|
|
23
69
|
const collectedParams = new Set();
|
|
24
70
|
for (let token of tokens) {
|
|
@@ -35,8 +81,14 @@ export class RoutesStore {
|
|
|
35
81
|
collectedParams.clear();
|
|
36
82
|
return params;
|
|
37
83
|
}
|
|
84
|
+
/**
|
|
85
|
+
* Register route for a given domain and method
|
|
86
|
+
*/
|
|
38
87
|
#registerRoute(domain, method, tokens, route) {
|
|
39
88
|
const methodRoutes = this.#getMethodNode(domain, method);
|
|
89
|
+
/*
|
|
90
|
+
* Check for duplicate route for the same domain and method
|
|
91
|
+
*/
|
|
40
92
|
if (methodRoutes.routes[route.pattern]) {
|
|
41
93
|
throw new RuntimeException(`Duplicate route found. "${method}: ${route.pattern}" route already exists`);
|
|
42
94
|
}
|
|
@@ -45,28 +97,75 @@ export class RoutesStore {
|
|
|
45
97
|
methodRoutes.routeKeys[route.pattern] =
|
|
46
98
|
domain !== 'root' ? `${domain}-${method}-${route.pattern}` : `${method}-${route.pattern}`;
|
|
47
99
|
}
|
|
100
|
+
/**
|
|
101
|
+
* Add a route to the store
|
|
102
|
+
*
|
|
103
|
+
* ```ts
|
|
104
|
+
* store.add({
|
|
105
|
+
* pattern: 'post/:id',
|
|
106
|
+
* methods: ['GET'],
|
|
107
|
+
* matchers: {},
|
|
108
|
+
* meta: {},
|
|
109
|
+
* handler: function handler () {
|
|
110
|
+
* }
|
|
111
|
+
* })
|
|
112
|
+
* ```
|
|
113
|
+
*/
|
|
48
114
|
add(route) {
|
|
115
|
+
/**
|
|
116
|
+
* Set flag when a custom domain is used
|
|
117
|
+
*/
|
|
49
118
|
if (route.domain !== 'root') {
|
|
50
119
|
this.usingDomains = true;
|
|
51
120
|
}
|
|
121
|
+
/**
|
|
122
|
+
* Generate tokens for the route
|
|
123
|
+
*/
|
|
52
124
|
const tokens = parseRoutePattern(route.pattern, route.matchers);
|
|
125
|
+
/**
|
|
126
|
+
* Create route node object for persistence
|
|
127
|
+
*/
|
|
53
128
|
const routeNode = lodash.merge({ meta: {} }, lodash.pick(route, ['pattern', 'handler', 'meta', 'middleware', 'name', 'execute']));
|
|
129
|
+
/**
|
|
130
|
+
* Set route params
|
|
131
|
+
*/
|
|
54
132
|
routeNode.meta.params = this.#collectRouteParams(routeNode, tokens);
|
|
133
|
+
/**
|
|
134
|
+
* Register route for every method
|
|
135
|
+
*/
|
|
55
136
|
route.methods.forEach((method) => {
|
|
56
137
|
this.#registerRoute(route.domain, method, tokens, routeNode);
|
|
57
138
|
});
|
|
58
139
|
return this;
|
|
59
140
|
}
|
|
141
|
+
/**
|
|
142
|
+
* Matches the url, method and optionally domain to pull the matching
|
|
143
|
+
* route. `null` is returned when unable to match the URL against
|
|
144
|
+
* registered routes.
|
|
145
|
+
*
|
|
146
|
+
* The domain parameter has to be a registered pattern and not the fully
|
|
147
|
+
* qualified runtime domain. You must call `matchDomain` first to fetch
|
|
148
|
+
* the pattern for qualified domain
|
|
149
|
+
*/
|
|
60
150
|
match(url, method, domain) {
|
|
61
151
|
const domainName = domain?.tokens[0]?.old || 'root';
|
|
62
152
|
const matchedDomain = this.tree.domains[domainName];
|
|
63
153
|
if (!matchedDomain) {
|
|
64
154
|
return null;
|
|
65
155
|
}
|
|
156
|
+
/*
|
|
157
|
+
* Next get the method node for the given method inside the domain. If
|
|
158
|
+
* method node is missing, means no routes ever got registered for that
|
|
159
|
+
* method
|
|
160
|
+
*/
|
|
66
161
|
const matchedMethod = this.tree.domains[domainName][method];
|
|
67
162
|
if (!matchedMethod) {
|
|
68
163
|
return null;
|
|
69
164
|
}
|
|
165
|
+
/*
|
|
166
|
+
* Next, match route for the given url inside the tokens list for the
|
|
167
|
+
* matchedMethod
|
|
168
|
+
*/
|
|
70
169
|
const matchedRoute = matchit.match(url, matchedMethod.tokens);
|
|
71
170
|
if (!matchedRoute.length) {
|
|
72
171
|
return null;
|
|
@@ -79,6 +178,9 @@ export class RoutesStore {
|
|
|
79
178
|
subdomains: domain?.hostname ? matchit.exec(domain.hostname, domain.tokens) : {},
|
|
80
179
|
};
|
|
81
180
|
}
|
|
181
|
+
/**
|
|
182
|
+
* Match hostname against registered domains.
|
|
183
|
+
*/
|
|
82
184
|
matchDomain(hostname) {
|
|
83
185
|
if (!hostname || !this.usingDomains) {
|
|
84
186
|
return [];
|
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import type { ContainerResolver } from '@adonisjs/fold';
|
|
2
2
|
import type { Router } from '../../router/main.js';
|
|
3
3
|
import type { HttpContext } from '../../http_context/main.js';
|
|
4
|
+
/**
|
|
5
|
+
* The final handler is executed after the server middleware stack.
|
|
6
|
+
* It looks for a matching route and executes the route middleware
|
|
7
|
+
* stack.
|
|
8
|
+
*/
|
|
4
9
|
export declare function finalHandler(router: Router, resolver: ContainerResolver<any>, ctx: HttpContext): () => any;
|
|
5
|
-
//# sourceMappingURL=final_handler.d.ts.map
|
|
@@ -1,4 +1,17 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @adonisjs/http-server
|
|
3
|
+
*
|
|
4
|
+
* (c) AdonisJS
|
|
5
|
+
*
|
|
6
|
+
* For the full copyright and license information, please view the LICENSE
|
|
7
|
+
* file that was distributed with this source code.
|
|
8
|
+
*/
|
|
1
9
|
import * as errors from '../../exceptions.js';
|
|
10
|
+
/**
|
|
11
|
+
* The final handler is executed after the server middleware stack.
|
|
12
|
+
* It looks for a matching route and executes the route middleware
|
|
13
|
+
* stack.
|
|
14
|
+
*/
|
|
2
15
|
export function finalHandler(router, resolver, ctx) {
|
|
3
16
|
return function () {
|
|
4
17
|
const url = ctx.request.url();
|
|
@@ -2,5 +2,7 @@ import type { NextFn } from '@poppinss/middleware/types';
|
|
|
2
2
|
import type { ContainerResolver } from '@adonisjs/fold';
|
|
3
3
|
import type { HttpContext } from '../../http_context/main.js';
|
|
4
4
|
import { ParsedGlobalMiddleware } from '../../types/middleware.js';
|
|
5
|
+
/**
|
|
6
|
+
* The middleware handler invokes the middleware functions.
|
|
7
|
+
*/
|
|
5
8
|
export declare function middlewareHandler(resolver: ContainerResolver<any>, ctx: HttpContext): (fn: ParsedGlobalMiddleware, next: NextFn) => any;
|
|
6
|
-
//# sourceMappingURL=middleware_handler.d.ts.map
|
|
@@ -1,3 +1,14 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @adonisjs/http-server
|
|
3
|
+
*
|
|
4
|
+
* (c) AdonisJS
|
|
5
|
+
*
|
|
6
|
+
* For the full copyright and license information, please view the LICENSE
|
|
7
|
+
* file that was distributed with this source code.
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* The middleware handler invokes the middleware functions.
|
|
11
|
+
*/
|
|
1
12
|
export function middlewareHandler(resolver, ctx) {
|
|
2
13
|
return function (fn, next) {
|
|
3
14
|
return fn.handle(resolver, ctx, next);
|
|
@@ -1,3 +1,6 @@
|
|
|
1
1
|
import type { HttpContext } from '../../http_context/main.js';
|
|
2
|
+
/**
|
|
3
|
+
* Writes the response to the socket. The "finish" method can
|
|
4
|
+
* raise error when unable to serialize the response.
|
|
5
|
+
*/
|
|
2
6
|
export declare function writeResponse(ctx: HttpContext): () => void;
|
|
3
|
-
//# sourceMappingURL=write_response.d.ts.map
|
|
@@ -1,3 +1,15 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @adonisjs/http-server
|
|
3
|
+
*
|
|
4
|
+
* (c) AdonisJS
|
|
5
|
+
*
|
|
6
|
+
* For the full copyright and license information, please view the LICENSE
|
|
7
|
+
* file that was distributed with this source code.
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Writes the response to the socket. The "finish" method can
|
|
11
|
+
* raise error when unable to serialize the response.
|
|
12
|
+
*/
|
|
1
13
|
export function writeResponse(ctx) {
|
|
2
14
|
return function () {
|
|
3
15
|
try {
|
|
@@ -14,20 +14,67 @@ import { Request } from '../request.js';
|
|
|
14
14
|
import { Response } from '../response.js';
|
|
15
15
|
import { Router } from '../router/main.js';
|
|
16
16
|
import { HttpContext } from '../http_context/main.js';
|
|
17
|
+
/**
|
|
18
|
+
* The HTTP server implementation to handle incoming requests and respond using the
|
|
19
|
+
* registered routes.
|
|
20
|
+
*/
|
|
17
21
|
export declare class Server {
|
|
18
22
|
#private;
|
|
23
|
+
/**
|
|
24
|
+
* Know if async local storage is enabled or not.
|
|
25
|
+
*/
|
|
19
26
|
get usingAsyncLocalStorage(): boolean;
|
|
20
27
|
constructor(app: Application<any>, encryption: Encryption, emitter: Emitter<any>, logger: Logger, config: ServerConfig);
|
|
28
|
+
/**
|
|
29
|
+
* Creates a pipeline of middleware.
|
|
30
|
+
*/
|
|
21
31
|
pipeline(middleware: MiddlewareAsClass[]): TestingMiddlewarePipeline;
|
|
32
|
+
/**
|
|
33
|
+
* Define an array of middleware to use on all the incoming HTTP request.
|
|
34
|
+
* Calling this method multiple times pushes to the existing list
|
|
35
|
+
* of middleware
|
|
36
|
+
*/
|
|
22
37
|
use(middleware: LazyImport<MiddlewareAsClass>[]): this;
|
|
38
|
+
/**
|
|
39
|
+
* Register a custom error handler for HTTP requests.
|
|
40
|
+
* All errors will be reported to this method
|
|
41
|
+
*/
|
|
23
42
|
errorHandler(handler: LazyImport<ErrorHandlerAsAClass>): this;
|
|
43
|
+
/**
|
|
44
|
+
* Boot the server. Calling this method performs the following actions.
|
|
45
|
+
*
|
|
46
|
+
* - Register routes with the store.
|
|
47
|
+
* - Resolve and construct the error handler.
|
|
48
|
+
*/
|
|
24
49
|
boot(): Promise<void>;
|
|
50
|
+
/**
|
|
51
|
+
* Set the HTTP server instance used to listen for requests.
|
|
52
|
+
*/
|
|
25
53
|
setNodeServer(server: HttpServer | HttpsServer): void;
|
|
54
|
+
/**
|
|
55
|
+
* Returns reference to the underlying HTTP server
|
|
56
|
+
* in use
|
|
57
|
+
*/
|
|
26
58
|
getNodeServer(): HttpServer<typeof IncomingMessage, typeof ServerResponse> | HttpsServer<typeof IncomingMessage, typeof ServerResponse> | undefined;
|
|
59
|
+
/**
|
|
60
|
+
* Returns reference to the router instance used
|
|
61
|
+
* by the server.
|
|
62
|
+
*/
|
|
27
63
|
getRouter(): Router;
|
|
64
|
+
/**
|
|
65
|
+
* Creates an instance of the [[Request]] class
|
|
66
|
+
*/
|
|
28
67
|
createRequest(req: IncomingMessage, res: ServerResponse): Request;
|
|
68
|
+
/**
|
|
69
|
+
* Creates an instance of the [[Response]] class
|
|
70
|
+
*/
|
|
29
71
|
createResponse(req: IncomingMessage, res: ServerResponse): Response;
|
|
72
|
+
/**
|
|
73
|
+
* Creates an instance of the [[HttpContext]] class
|
|
74
|
+
*/
|
|
30
75
|
createHttpContext(request: Request, response: Response, resolver: ContainerResolver<any>): HttpContext;
|
|
76
|
+
/**
|
|
77
|
+
* Handle request
|
|
78
|
+
*/
|
|
31
79
|
handle(req: IncomingMessage, res: ServerResponse): Promise<any>;
|
|
32
80
|
}
|
|
33
|
-
//# sourceMappingURL=main.d.ts.map
|
package/build/src/server/main.js
CHANGED
|
@@ -1,3 +1,11 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @adonisjs/http-server
|
|
3
|
+
*
|
|
4
|
+
* (c) AdonisJS
|
|
5
|
+
*
|
|
6
|
+
* For the full copyright and license information, please view the LICENSE
|
|
7
|
+
* file that was distributed with this source code.
|
|
8
|
+
*/
|
|
1
9
|
import onFinished from 'on-finished';
|
|
2
10
|
import Middleware from '@poppinss/middleware';
|
|
3
11
|
import { moduleCaller, moduleImporter } from '@adonisjs/fold';
|
|
@@ -11,25 +19,74 @@ import { finalHandler } from './factories/final_handler.js';
|
|
|
11
19
|
import { writeResponse } from './factories/write_response.js';
|
|
12
20
|
import { asyncLocalStorage } from '../http_context/local_storage.js';
|
|
13
21
|
import { middlewareHandler } from './factories/middleware_handler.js';
|
|
22
|
+
/**
|
|
23
|
+
* The HTTP server implementation to handle incoming requests and respond using the
|
|
24
|
+
* registered routes.
|
|
25
|
+
*/
|
|
14
26
|
export class Server {
|
|
27
|
+
/**
|
|
28
|
+
* The default error handler to use
|
|
29
|
+
*/
|
|
15
30
|
#defaultErrorHandler = {
|
|
16
31
|
report() { },
|
|
17
32
|
handle(error, ctx) {
|
|
18
33
|
ctx.response.status(error.status || 500).send(error.message || 'Internal server error');
|
|
19
34
|
},
|
|
20
35
|
};
|
|
36
|
+
/**
|
|
37
|
+
* Logger instance, a child logger is added
|
|
38
|
+
* to the context to have request specific
|
|
39
|
+
* logging capabilities.
|
|
40
|
+
*/
|
|
21
41
|
#logger;
|
|
42
|
+
/**
|
|
43
|
+
* Registered error handler (if any)
|
|
44
|
+
*/
|
|
22
45
|
#errorHandler;
|
|
46
|
+
/**
|
|
47
|
+
* Resolved error handler is an instance of the lazily imported error
|
|
48
|
+
* handler class.
|
|
49
|
+
*/
|
|
23
50
|
#resolvedErrorHandler = this.#defaultErrorHandler;
|
|
51
|
+
/**
|
|
52
|
+
* Emitter is required to notify when a request finishes
|
|
53
|
+
*/
|
|
24
54
|
#emitter;
|
|
55
|
+
/**
|
|
56
|
+
* The application instance to be shared with the router
|
|
57
|
+
*/
|
|
25
58
|
#app;
|
|
59
|
+
/**
|
|
60
|
+
* The encryption instance to be shared with the router
|
|
61
|
+
*/
|
|
26
62
|
#encryption;
|
|
63
|
+
/**
|
|
64
|
+
* Server config
|
|
65
|
+
*/
|
|
27
66
|
#config;
|
|
67
|
+
/**
|
|
68
|
+
* Query string parser used by the server
|
|
69
|
+
*/
|
|
28
70
|
#qsParser;
|
|
71
|
+
/**
|
|
72
|
+
* Server middleware stack runs on every incoming HTTP request
|
|
73
|
+
*/
|
|
29
74
|
#serverMiddlewareStack;
|
|
75
|
+
/**
|
|
76
|
+
* Reference to the router used by the server
|
|
77
|
+
*/
|
|
30
78
|
#router;
|
|
79
|
+
/**
|
|
80
|
+
* Reference to the underlying Node HTTP server in use
|
|
81
|
+
*/
|
|
31
82
|
#nodeHttpServer;
|
|
83
|
+
/**
|
|
84
|
+
* Middleware store to be shared with the routes
|
|
85
|
+
*/
|
|
32
86
|
#middleware = [];
|
|
87
|
+
/**
|
|
88
|
+
* Know if async local storage is enabled or not.
|
|
89
|
+
*/
|
|
33
90
|
get usingAsyncLocalStorage() {
|
|
34
91
|
return asyncLocalStorage.isEnabled;
|
|
35
92
|
}
|
|
@@ -44,6 +101,9 @@ export class Server {
|
|
|
44
101
|
this.#createAsyncLocalStore();
|
|
45
102
|
debug('server config: %O', this.#config);
|
|
46
103
|
}
|
|
104
|
+
/**
|
|
105
|
+
* Create async local storage store when enabled
|
|
106
|
+
*/
|
|
47
107
|
#createAsyncLocalStore() {
|
|
48
108
|
if (this.#config.useAsyncLocalStorage) {
|
|
49
109
|
debug('creating ALS store for HTTP context');
|
|
@@ -53,12 +113,18 @@ export class Server {
|
|
|
53
113
|
asyncLocalStorage.destroy();
|
|
54
114
|
}
|
|
55
115
|
}
|
|
116
|
+
/**
|
|
117
|
+
* Creates an instance of the server middleware stack
|
|
118
|
+
*/
|
|
56
119
|
#createServerMiddlewareStack() {
|
|
57
120
|
this.#serverMiddlewareStack = new Middleware();
|
|
58
121
|
this.#middleware.forEach((middleware) => this.#serverMiddlewareStack.add(middleware));
|
|
59
122
|
this.#serverMiddlewareStack.freeze();
|
|
60
123
|
this.#middleware = [];
|
|
61
124
|
}
|
|
125
|
+
/**
|
|
126
|
+
* Handles the HTTP request
|
|
127
|
+
*/
|
|
62
128
|
#handleRequest(ctx, resolver) {
|
|
63
129
|
return this.#serverMiddlewareStack.runner()
|
|
64
130
|
.errorHandler((error) => {
|
|
@@ -73,6 +139,9 @@ export class Server {
|
|
|
73
139
|
})
|
|
74
140
|
.finally(writeResponse(ctx));
|
|
75
141
|
}
|
|
142
|
+
/**
|
|
143
|
+
* Creates a pipeline of middleware.
|
|
144
|
+
*/
|
|
76
145
|
pipeline(middleware) {
|
|
77
146
|
const middlewareStack = new Middleware();
|
|
78
147
|
middleware.forEach((one) => {
|
|
@@ -96,18 +165,42 @@ export class Server {
|
|
|
96
165
|
},
|
|
97
166
|
};
|
|
98
167
|
}
|
|
168
|
+
/**
|
|
169
|
+
* Define an array of middleware to use on all the incoming HTTP request.
|
|
170
|
+
* Calling this method multiple times pushes to the existing list
|
|
171
|
+
* of middleware
|
|
172
|
+
*/
|
|
99
173
|
use(middleware) {
|
|
100
174
|
middleware.forEach((one) => this.#middleware.push(moduleImporter(one, 'handle').toHandleMethod()));
|
|
101
175
|
return this;
|
|
102
176
|
}
|
|
177
|
+
/**
|
|
178
|
+
* Register a custom error handler for HTTP requests.
|
|
179
|
+
* All errors will be reported to this method
|
|
180
|
+
*/
|
|
103
181
|
errorHandler(handler) {
|
|
104
182
|
this.#errorHandler = handler;
|
|
105
183
|
return this;
|
|
106
184
|
}
|
|
185
|
+
/**
|
|
186
|
+
* Boot the server. Calling this method performs the following actions.
|
|
187
|
+
*
|
|
188
|
+
* - Register routes with the store.
|
|
189
|
+
* - Resolve and construct the error handler.
|
|
190
|
+
*/
|
|
107
191
|
async boot() {
|
|
108
192
|
debug('booting HTTP server');
|
|
193
|
+
/**
|
|
194
|
+
* Creates the middleware stack for the server
|
|
195
|
+
*/
|
|
109
196
|
this.#createServerMiddlewareStack();
|
|
197
|
+
/**
|
|
198
|
+
* Commit routes
|
|
199
|
+
*/
|
|
110
200
|
this.#router.commit();
|
|
201
|
+
/**
|
|
202
|
+
* Register custom error handler
|
|
203
|
+
*/
|
|
111
204
|
if (this.#errorHandler) {
|
|
112
205
|
if (debug.enabled) {
|
|
113
206
|
debug('using custom error handler "%s"', this.#errorHandler);
|
|
@@ -116,29 +209,61 @@ export class Server {
|
|
|
116
209
|
this.#resolvedErrorHandler = await this.#app.container.make(moduleExports.default);
|
|
117
210
|
}
|
|
118
211
|
}
|
|
212
|
+
/**
|
|
213
|
+
* Set the HTTP server instance used to listen for requests.
|
|
214
|
+
*/
|
|
119
215
|
setNodeServer(server) {
|
|
120
216
|
this.#nodeHttpServer = server;
|
|
121
217
|
}
|
|
218
|
+
/**
|
|
219
|
+
* Returns reference to the underlying HTTP server
|
|
220
|
+
* in use
|
|
221
|
+
*/
|
|
122
222
|
getNodeServer() {
|
|
123
223
|
return this.#nodeHttpServer;
|
|
124
224
|
}
|
|
225
|
+
/**
|
|
226
|
+
* Returns reference to the router instance used
|
|
227
|
+
* by the server.
|
|
228
|
+
*/
|
|
125
229
|
getRouter() {
|
|
126
230
|
return this.#router;
|
|
127
231
|
}
|
|
232
|
+
/**
|
|
233
|
+
* Creates an instance of the [[Request]] class
|
|
234
|
+
*/
|
|
128
235
|
createRequest(req, res) {
|
|
129
236
|
return new Request(req, res, this.#encryption, this.#config, this.#qsParser);
|
|
130
237
|
}
|
|
238
|
+
/**
|
|
239
|
+
* Creates an instance of the [[Response]] class
|
|
240
|
+
*/
|
|
131
241
|
createResponse(req, res) {
|
|
132
242
|
return new Response(req, res, this.#encryption, this.#config, this.#router, this.#qsParser);
|
|
133
243
|
}
|
|
244
|
+
/**
|
|
245
|
+
* Creates an instance of the [[HttpContext]] class
|
|
246
|
+
*/
|
|
134
247
|
createHttpContext(request, response, resolver) {
|
|
135
248
|
return new HttpContext(request, response, this.#logger.child({ request_id: request.id() }), resolver);
|
|
136
249
|
}
|
|
250
|
+
/**
|
|
251
|
+
* Handle request
|
|
252
|
+
*/
|
|
137
253
|
handle(req, res) {
|
|
254
|
+
/**
|
|
255
|
+
* Setup for the "http:request_finished" event
|
|
256
|
+
*/
|
|
138
257
|
const hasRequestListener = this.#emitter.hasListeners('http:request_finished');
|
|
139
258
|
const startTime = hasRequestListener ? process.hrtime() : null;
|
|
259
|
+
/**
|
|
260
|
+
* Creating essential instances
|
|
261
|
+
*/
|
|
140
262
|
const resolver = this.#app.container.createResolver();
|
|
141
263
|
const ctx = this.createHttpContext(this.createRequest(req, res), this.createResponse(req, res), resolver);
|
|
264
|
+
/**
|
|
265
|
+
* Emit event when listening for the request_finished event
|
|
266
|
+
*/
|
|
142
267
|
if (startTime) {
|
|
143
268
|
onFinished(res, () => {
|
|
144
269
|
this.#emitter.emit('http:request_finished', {
|
|
@@ -147,6 +272,9 @@ export class Server {
|
|
|
147
272
|
});
|
|
148
273
|
});
|
|
149
274
|
}
|
|
275
|
+
/**
|
|
276
|
+
* Handle request
|
|
277
|
+
*/
|
|
150
278
|
if (this.usingAsyncLocalStorage) {
|
|
151
279
|
return asyncLocalStorage.storage.run(ctx, () => this.#handleRequest(ctx, resolver));
|
|
152
280
|
}
|
|
@@ -1,8 +1,19 @@
|
|
|
1
1
|
export type { NextFn } from '@poppinss/middleware/types';
|
|
2
|
+
/**
|
|
3
|
+
* Accept one or more of the mentioned type
|
|
4
|
+
*/
|
|
2
5
|
export type OneOrMore<T> = T | T[];
|
|
6
|
+
/**
|
|
7
|
+
* Class constructor type
|
|
8
|
+
*/
|
|
3
9
|
export type Constructor<T> = new (...args: any[]) => T;
|
|
10
|
+
/**
|
|
11
|
+
* A function that lazily imports a middleware
|
|
12
|
+
*/
|
|
4
13
|
export type LazyImport<DefaultExport> = () => Promise<{
|
|
5
14
|
default: DefaultExport;
|
|
6
15
|
}>;
|
|
16
|
+
/**
|
|
17
|
+
* Unwraps default export from a lazy import function
|
|
18
|
+
*/
|
|
7
19
|
export type UnWrapLazyImport<Fn extends LazyImport<any>> = Awaited<ReturnType<Fn>>['default'];
|
|
8
|
-
//# sourceMappingURL=base.d.ts.map
|
package/build/src/types/base.js
CHANGED
package/build/src/types/main.js
CHANGED
|
@@ -1,3 +1,11 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @adonisjs/http-server
|
|
3
|
+
*
|
|
4
|
+
* (c) AdonisJS
|
|
5
|
+
*
|
|
6
|
+
* For the full copyright and license information, please view the LICENSE
|
|
7
|
+
* file that was distributed with this source code.
|
|
8
|
+
*/
|
|
1
9
|
export * from './base.js';
|
|
2
10
|
export * from './middleware.js';
|
|
3
11
|
export * from './qs.js';
|
|
@@ -1,18 +1,35 @@
|
|
|
1
1
|
import type { ContainerResolver } from '@adonisjs/fold';
|
|
2
2
|
import type { Constructor, NextFn } from './base.js';
|
|
3
3
|
import type { HttpContext } from '../http_context/main.js';
|
|
4
|
+
/**
|
|
5
|
+
* Middleware represented as a class
|
|
6
|
+
*/
|
|
4
7
|
export type MiddlewareAsClass = Constructor<{
|
|
5
8
|
handle: (ctx: HttpContext, next: NextFn, args?: any) => any;
|
|
6
9
|
}>;
|
|
10
|
+
/**
|
|
11
|
+
* Check if a union has undefined or null
|
|
12
|
+
*/
|
|
7
13
|
type HasUndefined<T> = T extends NonNullable<T> ? true : false;
|
|
14
|
+
/**
|
|
15
|
+
* Returns the arguments accepted by the middleware's handle method
|
|
16
|
+
*/
|
|
8
17
|
export type GetMiddlewareArgs<Middleware extends MiddlewareAsClass> = Parameters<InstanceType<Middleware>['handle']>[2] extends undefined ? [] : HasUndefined<Parameters<InstanceType<Middleware>['handle']>[2]> extends true ? [Parameters<InstanceType<Middleware>['handle']>[2]] : [Parameters<InstanceType<Middleware>['handle']>[2]?];
|
|
18
|
+
/**
|
|
19
|
+
* The middleware defined as a function on the router or the server
|
|
20
|
+
*/
|
|
9
21
|
export type MiddlewareFn = (ctx: HttpContext, next: NextFn) => any;
|
|
22
|
+
/**
|
|
23
|
+
* Parsed global middleware
|
|
24
|
+
*/
|
|
10
25
|
export type ParsedGlobalMiddleware = {
|
|
11
26
|
handle: (resolver: ContainerResolver<any>, ...args: [ctx: HttpContext, next: NextFn, params?: any]) => any;
|
|
12
27
|
};
|
|
28
|
+
/**
|
|
29
|
+
* Parsed named middleware
|
|
30
|
+
*/
|
|
13
31
|
export type ParsedNamedMiddleware = ParsedGlobalMiddleware & {
|
|
14
32
|
name: string;
|
|
15
33
|
args: any;
|
|
16
34
|
};
|
|
17
35
|
export {};
|
|
18
|
-
//# sourceMappingURL=middleware.d.ts.map
|