@adonisjs/http-server 6.8.2-9 → 7.0.0-0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/chunk-XX72ATFY.js +4388 -0
- package/build/factories/main.d.ts +149 -6
- package/build/factories/main.js +331 -14
- package/build/index.d.ts +272 -14
- package/build/index.js +308 -22
- package/build/{src/server/main.d.ts → main-29eaaee4.d.ts} +12 -17
- package/build/main-e5b46c83.d.ts +2210 -0
- package/build/src/types/main.d.ts +14 -7
- package/build/src/types/main.js +0 -15
- package/package.json +62 -53
- package/build/factories/http_context.d.ts +0 -25
- package/build/factories/http_context.js +0 -51
- package/build/factories/http_server.d.ts +0 -8
- package/build/factories/http_server.js +0 -26
- package/build/factories/qs_parser_factory.d.ts +0 -20
- package/build/factories/qs_parser_factory.js +0 -44
- package/build/factories/request.d.ts +0 -29
- package/build/factories/request.js +0 -73
- package/build/factories/response.d.ts +0 -29
- package/build/factories/response.js +0 -77
- package/build/factories/router.d.ts +0 -23
- package/build/factories/router.js +0 -45
- package/build/factories/server_factory.d.ts +0 -29
- package/build/factories/server_factory.js +0 -65
- package/build/src/cookies/client.d.ts +0 -37
- package/build/src/cookies/client.js +0 -84
- package/build/src/cookies/drivers/encrypted.d.ts +0 -16
- package/build/src/cookies/drivers/encrypted.js +0 -36
- package/build/src/cookies/drivers/plain.d.ts +0 -15
- package/build/src/cookies/drivers/plain.js +0 -33
- package/build/src/cookies/drivers/signed.d.ts +0 -16
- package/build/src/cookies/drivers/signed.js +0 -36
- package/build/src/cookies/parser.d.ts +0 -37
- package/build/src/cookies/parser.js +0 -167
- package/build/src/cookies/serializer.d.ts +0 -33
- package/build/src/cookies/serializer.js +0 -79
- package/build/src/debug.d.ts +0 -3
- package/build/src/debug.js +0 -10
- package/build/src/define_config.d.ts +0 -5
- package/build/src/define_config.js +0 -51
- package/build/src/define_middleware.d.ts +0 -11
- package/build/src/define_middleware.js +0 -35
- package/build/src/exception_handler.d.ts +0 -112
- package/build/src/exception_handler.js +0 -303
- package/build/src/exceptions.d.ts +0 -84
- package/build/src/exceptions.js +0 -38
- package/build/src/helpers.d.ts +0 -23
- package/build/src/helpers.js +0 -84
- package/build/src/http_context/local_storage.d.ts +0 -12
- package/build/src/http_context/local_storage.js +0 -39
- package/build/src/http_context/main.d.ts +0 -58
- package/build/src/http_context/main.js +0 -105
- package/build/src/qs.d.ts +0 -11
- package/build/src/qs.js +0 -25
- package/build/src/redirect.d.ts +0 -42
- package/build/src/redirect.js +0 -140
- package/build/src/request.d.ts +0 -565
- package/build/src/request.js +0 -865
- package/build/src/response.d.ts +0 -540
- package/build/src/response.js +0 -1208
- package/build/src/router/brisk.d.ts +0 -42
- package/build/src/router/brisk.js +0 -85
- package/build/src/router/executor.d.ts +0 -8
- package/build/src/router/executor.js +0 -29
- package/build/src/router/factories/use_return_value.d.ts +0 -6
- package/build/src/router/factories/use_return_value.js +0 -22
- package/build/src/router/group.d.ts +0 -65
- package/build/src/router/group.js +0 -207
- package/build/src/router/lookup_store/main.d.ts +0 -49
- package/build/src/router/lookup_store/main.js +0 -86
- package/build/src/router/lookup_store/route_finder.d.ts +0 -21
- package/build/src/router/lookup_store/route_finder.js +0 -49
- package/build/src/router/lookup_store/url_builder.d.ts +0 -52
- package/build/src/router/lookup_store/url_builder.js +0 -209
- package/build/src/router/main.d.ts +0 -128
- package/build/src/router/main.js +0 -316
- package/build/src/router/matchers.d.ts +0 -27
- package/build/src/router/matchers.js +0 -36
- package/build/src/router/parser.d.ts +0 -5
- package/build/src/router/parser.js +0 -17
- package/build/src/router/resource.d.ts +0 -54
- package/build/src/router/resource.js +0 -216
- package/build/src/router/route.d.ts +0 -92
- package/build/src/router/route.js +0 -293
- package/build/src/router/store.d.ts +0 -66
- package/build/src/router/store.js +0 -195
- package/build/src/server/factories/final_handler.d.ts +0 -9
- package/build/src/server/factories/final_handler.js +0 -30
- package/build/src/server/factories/middleware_handler.d.ts +0 -8
- package/build/src/server/factories/middleware_handler.js +0 -16
- package/build/src/server/factories/write_response.d.ts +0 -6
- package/build/src/server/factories/write_response.js +0 -24
- package/build/src/server/main.js +0 -283
- package/build/src/types/base.d.ts +0 -19
- package/build/src/types/base.js +0 -9
- package/build/src/types/middleware.d.ts +0 -35
- package/build/src/types/middleware.js +0 -9
- package/build/src/types/qs.d.ts +0 -68
- package/build/src/types/qs.js +0 -9
- package/build/src/types/request.d.ts +0 -39
- package/build/src/types/request.js +0 -9
- package/build/src/types/response.d.ts +0 -45
- package/build/src/types/response.js +0 -9
- package/build/src/types/route.d.ts +0 -165
- package/build/src/types/route.js +0 -9
- package/build/src/types/server.d.ts +0 -72
- package/build/src/types/server.js +0 -9
|
@@ -1,293 +0,0 @@
|
|
|
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
|
-
import is from '@sindresorhus/is';
|
|
10
|
-
import Macroable from '@poppinss/macroable';
|
|
11
|
-
import Middleware from '@poppinss/middleware';
|
|
12
|
-
import { RuntimeException } from '@poppinss/utils';
|
|
13
|
-
import { moduleCaller, moduleExpression, moduleImporter } from '@adonisjs/fold';
|
|
14
|
-
import { execute } from './executor.js';
|
|
15
|
-
import { dropSlash } from '../helpers.js';
|
|
16
|
-
import debug from '../debug.js';
|
|
17
|
-
/**
|
|
18
|
-
* The route class exposes the APIs for constructing a route using the
|
|
19
|
-
* fluent API.
|
|
20
|
-
*/
|
|
21
|
-
export class Route extends Macroable {
|
|
22
|
-
/**
|
|
23
|
-
* Route pattern
|
|
24
|
-
*/
|
|
25
|
-
#pattern;
|
|
26
|
-
/**
|
|
27
|
-
* HTTP Methods for the route
|
|
28
|
-
*/
|
|
29
|
-
#methods;
|
|
30
|
-
/**
|
|
31
|
-
* A unique name for the route
|
|
32
|
-
*/
|
|
33
|
-
#name;
|
|
34
|
-
/**
|
|
35
|
-
* A boolean to prevent route from getting registered within
|
|
36
|
-
* the store.
|
|
37
|
-
*
|
|
38
|
-
* This flag must be set before "Router.commit" method
|
|
39
|
-
*/
|
|
40
|
-
#isDeleted = false;
|
|
41
|
-
/**
|
|
42
|
-
* Route handler
|
|
43
|
-
*/
|
|
44
|
-
#handler;
|
|
45
|
-
/**
|
|
46
|
-
* Matchers inherited from the router
|
|
47
|
-
*/
|
|
48
|
-
#globalMatchers;
|
|
49
|
-
/**
|
|
50
|
-
* Reference to the AdonisJS application
|
|
51
|
-
*/
|
|
52
|
-
#app;
|
|
53
|
-
/**
|
|
54
|
-
* Middleware registered on the router
|
|
55
|
-
*/
|
|
56
|
-
#routerMiddleware;
|
|
57
|
-
/**
|
|
58
|
-
* By default the route is part of the `root` domain. Root domain is used
|
|
59
|
-
* when no domain is defined
|
|
60
|
-
*/
|
|
61
|
-
#routeDomain = 'root';
|
|
62
|
-
/**
|
|
63
|
-
* An object of matchers to be forwarded to the store. The matchers
|
|
64
|
-
* list is populated by calling `where` method
|
|
65
|
-
*/
|
|
66
|
-
#matchers = {};
|
|
67
|
-
/**
|
|
68
|
-
* Custom prefixes defined on the route or the route parent
|
|
69
|
-
* groups
|
|
70
|
-
*/
|
|
71
|
-
#prefixes = [];
|
|
72
|
-
/**
|
|
73
|
-
* Middleware defined directly on the route or the route parent
|
|
74
|
-
* routes. We mantain an array for each layer of the stack
|
|
75
|
-
*/
|
|
76
|
-
#middleware = [];
|
|
77
|
-
constructor(app, routerMiddleware, options) {
|
|
78
|
-
super();
|
|
79
|
-
this.#app = app;
|
|
80
|
-
this.#routerMiddleware = routerMiddleware;
|
|
81
|
-
this.#pattern = options.pattern;
|
|
82
|
-
this.#methods = options.methods;
|
|
83
|
-
this.#handler = this.#resolveRouteHandle(options.handler);
|
|
84
|
-
this.#globalMatchers = options.globalMatchers;
|
|
85
|
-
}
|
|
86
|
-
/**
|
|
87
|
-
* Resolves the route handler string expression to a
|
|
88
|
-
* handler method object
|
|
89
|
-
*/
|
|
90
|
-
#resolveRouteHandle(handler) {
|
|
91
|
-
if (typeof handler === 'string') {
|
|
92
|
-
return {
|
|
93
|
-
reference: handler,
|
|
94
|
-
...moduleExpression(handler, this.#app.appRoot).toHandleMethod(),
|
|
95
|
-
};
|
|
96
|
-
}
|
|
97
|
-
/**
|
|
98
|
-
* Using a lazily imported controller
|
|
99
|
-
*/
|
|
100
|
-
if (Array.isArray(handler)) {
|
|
101
|
-
/**
|
|
102
|
-
* The first item of the tuple is a class constructor
|
|
103
|
-
*/
|
|
104
|
-
if (is.class_(handler[0])) {
|
|
105
|
-
return {
|
|
106
|
-
reference: handler,
|
|
107
|
-
...moduleCaller(handler[0], (handler[1] || 'handle')).toHandleMethod(),
|
|
108
|
-
};
|
|
109
|
-
}
|
|
110
|
-
/**
|
|
111
|
-
* The first item of the tuple is a function that lazily
|
|
112
|
-
* loads the controller
|
|
113
|
-
*/
|
|
114
|
-
return {
|
|
115
|
-
reference: handler,
|
|
116
|
-
...moduleImporter(handler[0], (handler[1] || 'handle')).toHandleMethod(),
|
|
117
|
-
};
|
|
118
|
-
}
|
|
119
|
-
return handler;
|
|
120
|
-
}
|
|
121
|
-
/**
|
|
122
|
-
* Returns an object of param matchers by merging global and local
|
|
123
|
-
* matchers. The local copy is given preference over the global
|
|
124
|
-
* one's
|
|
125
|
-
*/
|
|
126
|
-
#getMatchers() {
|
|
127
|
-
return { ...this.#globalMatchers, ...this.#matchers };
|
|
128
|
-
}
|
|
129
|
-
/**
|
|
130
|
-
* Returns a normalized pattern string by prefixing the `prefix` (if defined).
|
|
131
|
-
*/
|
|
132
|
-
#computePattern() {
|
|
133
|
-
const pattern = dropSlash(this.#pattern);
|
|
134
|
-
const prefix = this.#prefixes
|
|
135
|
-
.slice()
|
|
136
|
-
.reverse()
|
|
137
|
-
.map((one) => dropSlash(one))
|
|
138
|
-
.join('');
|
|
139
|
-
return prefix ? `${prefix}${pattern === '/' ? '' : pattern}` : pattern;
|
|
140
|
-
}
|
|
141
|
-
/**
|
|
142
|
-
* Define matcher for a given param. If a matcher exists, then we do not
|
|
143
|
-
* override that, since the routes inside a group will set matchers
|
|
144
|
-
* before the group, so they should have priority over the group
|
|
145
|
-
* matchers.
|
|
146
|
-
*
|
|
147
|
-
* ```ts
|
|
148
|
-
* Route.group(() => {
|
|
149
|
-
* Route.get('/:id', 'handler').where('id', /^[0-9]$/)
|
|
150
|
-
* }).where('id', /[^a-z$]/)
|
|
151
|
-
* ```
|
|
152
|
-
*
|
|
153
|
-
* The `/^[0-9]$/` will win over the matcher defined by the group
|
|
154
|
-
*/
|
|
155
|
-
where(param, matcher) {
|
|
156
|
-
if (this.#matchers[param]) {
|
|
157
|
-
return this;
|
|
158
|
-
}
|
|
159
|
-
if (typeof matcher === 'string') {
|
|
160
|
-
this.#matchers[param] = { match: new RegExp(matcher) };
|
|
161
|
-
}
|
|
162
|
-
else if (is.regExp(matcher)) {
|
|
163
|
-
this.#matchers[param] = { match: matcher };
|
|
164
|
-
}
|
|
165
|
-
else {
|
|
166
|
-
this.#matchers[param] = matcher;
|
|
167
|
-
}
|
|
168
|
-
return this;
|
|
169
|
-
}
|
|
170
|
-
/**
|
|
171
|
-
* Define prefix for the route. Calling this method multiple times
|
|
172
|
-
* applies multiple prefixes in the reverse order.
|
|
173
|
-
*/
|
|
174
|
-
prefix(prefix) {
|
|
175
|
-
this.#prefixes.push(prefix);
|
|
176
|
-
return this;
|
|
177
|
-
}
|
|
178
|
-
/**
|
|
179
|
-
* Define a custom domain for the route. We do not overwrite the domain
|
|
180
|
-
* unless `overwrite` flag is set to true.
|
|
181
|
-
*/
|
|
182
|
-
domain(domain, overwrite = false) {
|
|
183
|
-
if (this.#routeDomain === 'root' || overwrite) {
|
|
184
|
-
this.#routeDomain = domain;
|
|
185
|
-
}
|
|
186
|
-
return this;
|
|
187
|
-
}
|
|
188
|
-
/**
|
|
189
|
-
* Define one or more middleware to be executed before the route
|
|
190
|
-
* handler.
|
|
191
|
-
*
|
|
192
|
-
* Named middleware can be referenced using the name registered with
|
|
193
|
-
* the router middleware store.
|
|
194
|
-
*/
|
|
195
|
-
use(middleware) {
|
|
196
|
-
this.#middleware.push(Array.isArray(middleware) ? middleware : [middleware]);
|
|
197
|
-
return this;
|
|
198
|
-
}
|
|
199
|
-
/**
|
|
200
|
-
* @alias use
|
|
201
|
-
*/
|
|
202
|
-
middleware(middleware) {
|
|
203
|
-
return this.use(middleware);
|
|
204
|
-
}
|
|
205
|
-
/**
|
|
206
|
-
* Give a unique name to the route. Assinging a new unique removes the
|
|
207
|
-
* existing name of the route.
|
|
208
|
-
*
|
|
209
|
-
* Setting prepends to true prefixes the name to the existing name.
|
|
210
|
-
*/
|
|
211
|
-
as(name, prepend = false) {
|
|
212
|
-
if (prepend) {
|
|
213
|
-
if (!this.#name) {
|
|
214
|
-
throw new RuntimeException(`Routes inside a group must have names before calling "router.group.as"`);
|
|
215
|
-
}
|
|
216
|
-
this.#name = `${name}.${this.#name}`;
|
|
217
|
-
return this;
|
|
218
|
-
}
|
|
219
|
-
this.#name = name;
|
|
220
|
-
return this;
|
|
221
|
-
}
|
|
222
|
-
/**
|
|
223
|
-
* Check if the route was marked to be deleted
|
|
224
|
-
*/
|
|
225
|
-
isDeleted() {
|
|
226
|
-
return this.#isDeleted;
|
|
227
|
-
}
|
|
228
|
-
/**
|
|
229
|
-
* Mark route as deleted. Deleted routes are not registered
|
|
230
|
-
* with the route store
|
|
231
|
-
*/
|
|
232
|
-
markAsDeleted() {
|
|
233
|
-
this.#isDeleted = true;
|
|
234
|
-
}
|
|
235
|
-
/**
|
|
236
|
-
* Get the route name
|
|
237
|
-
*/
|
|
238
|
-
getName() {
|
|
239
|
-
return this.#name;
|
|
240
|
-
}
|
|
241
|
-
/**
|
|
242
|
-
* Get the route pattern
|
|
243
|
-
*/
|
|
244
|
-
getPattern() {
|
|
245
|
-
return this.#pattern;
|
|
246
|
-
}
|
|
247
|
-
/**
|
|
248
|
-
* Set the route pattern
|
|
249
|
-
*/
|
|
250
|
-
setPattern(pattern) {
|
|
251
|
-
this.#pattern = pattern;
|
|
252
|
-
return this;
|
|
253
|
-
}
|
|
254
|
-
/**
|
|
255
|
-
* Returns the stack of middleware registered on the route.
|
|
256
|
-
* The value is shared by reference.
|
|
257
|
-
*/
|
|
258
|
-
getMiddleware() {
|
|
259
|
-
return this.#middleware;
|
|
260
|
-
}
|
|
261
|
-
/**
|
|
262
|
-
* Returns the middleware instance for persistence inside the
|
|
263
|
-
* store
|
|
264
|
-
*/
|
|
265
|
-
#getMiddlewareForStore() {
|
|
266
|
-
const middleware = new Middleware();
|
|
267
|
-
this.#routerMiddleware.forEach((one) => {
|
|
268
|
-
debug('adding global middleware to route %s, %O', this.#pattern, one);
|
|
269
|
-
middleware.add(one);
|
|
270
|
-
});
|
|
271
|
-
this.#middleware.flat().forEach((one) => {
|
|
272
|
-
debug('adding named middleware to route %s, %O', this.#pattern, one);
|
|
273
|
-
middleware.add(one);
|
|
274
|
-
});
|
|
275
|
-
return middleware;
|
|
276
|
-
}
|
|
277
|
-
/**
|
|
278
|
-
* Returns JSON representation of the route
|
|
279
|
-
*/
|
|
280
|
-
toJSON() {
|
|
281
|
-
return {
|
|
282
|
-
domain: this.#routeDomain,
|
|
283
|
-
pattern: this.#computePattern(),
|
|
284
|
-
matchers: this.#getMatchers(),
|
|
285
|
-
meta: {},
|
|
286
|
-
name: this.#name,
|
|
287
|
-
handler: this.#handler,
|
|
288
|
-
methods: this.#methods,
|
|
289
|
-
middleware: this.#getMiddlewareForStore(),
|
|
290
|
-
execute: execute,
|
|
291
|
-
};
|
|
292
|
-
}
|
|
293
|
-
}
|
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
import type { RouteJSON, MatchedRoute, StoreRoutesTree, MatchItRouteToken } from '../types/route.js';
|
|
2
|
-
/**
|
|
3
|
-
* Store class is used to store a list of routes, along side with their tokens
|
|
4
|
-
* to match the URLs.
|
|
5
|
-
*
|
|
6
|
-
* ```ts
|
|
7
|
-
* const store = new Store()
|
|
8
|
-
*
|
|
9
|
-
* store.add({
|
|
10
|
-
* pattern: 'posts/:id',
|
|
11
|
-
* handler: function onRoute () {},
|
|
12
|
-
* middleware: [],
|
|
13
|
-
* matchers: {
|
|
14
|
-
* id: '^[0-9]$+'
|
|
15
|
-
* },
|
|
16
|
-
* meta: {},
|
|
17
|
-
* methods: ['GET']
|
|
18
|
-
* })
|
|
19
|
-
*
|
|
20
|
-
* store.match('posts/1', 'GET')
|
|
21
|
-
* ```
|
|
22
|
-
*/
|
|
23
|
-
export declare class RoutesStore {
|
|
24
|
-
#private;
|
|
25
|
-
/**
|
|
26
|
-
* A flag to know if routes for explicit domains
|
|
27
|
-
* have been registered
|
|
28
|
-
*/
|
|
29
|
-
usingDomains: boolean;
|
|
30
|
-
/**
|
|
31
|
-
* Tree of registered routes and their matchit tokens
|
|
32
|
-
*/
|
|
33
|
-
tree: StoreRoutesTree;
|
|
34
|
-
/**
|
|
35
|
-
* Add a route to the store
|
|
36
|
-
*
|
|
37
|
-
* ```ts
|
|
38
|
-
* store.add({
|
|
39
|
-
* pattern: 'post/:id',
|
|
40
|
-
* methods: ['GET'],
|
|
41
|
-
* matchers: {},
|
|
42
|
-
* meta: {},
|
|
43
|
-
* handler: function handler () {
|
|
44
|
-
* }
|
|
45
|
-
* })
|
|
46
|
-
* ```
|
|
47
|
-
*/
|
|
48
|
-
add(route: RouteJSON): this;
|
|
49
|
-
/**
|
|
50
|
-
* Matches the url, method and optionally domain to pull the matching
|
|
51
|
-
* route. `null` is returned when unable to match the URL against
|
|
52
|
-
* registered routes.
|
|
53
|
-
*
|
|
54
|
-
* The domain parameter has to be a registered pattern and not the fully
|
|
55
|
-
* qualified runtime domain. You must call `matchDomain` first to fetch
|
|
56
|
-
* the pattern for qualified domain
|
|
57
|
-
*/
|
|
58
|
-
match(url: string, method: string, domain?: {
|
|
59
|
-
tokens: MatchItRouteToken[];
|
|
60
|
-
hostname: string;
|
|
61
|
-
}): null | MatchedRoute;
|
|
62
|
-
/**
|
|
63
|
-
* Match hostname against registered domains.
|
|
64
|
-
*/
|
|
65
|
-
matchDomain(hostname?: string | null): MatchItRouteToken[];
|
|
66
|
-
}
|
|
@@ -1,195 +0,0 @@
|
|
|
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
|
|
10
|
-
import matchit from '@poppinss/matchit';
|
|
11
|
-
import lodash from '@poppinss/utils/lodash';
|
|
12
|
-
import { RuntimeException } from '@poppinss/utils';
|
|
13
|
-
import { parseRoutePattern } from './parser.js';
|
|
14
|
-
import debug from '../debug.js';
|
|
15
|
-
/**
|
|
16
|
-
* Store class is used to store a list of routes, along side with their tokens
|
|
17
|
-
* to match the URLs.
|
|
18
|
-
*
|
|
19
|
-
* ```ts
|
|
20
|
-
* const store = new Store()
|
|
21
|
-
*
|
|
22
|
-
* store.add({
|
|
23
|
-
* pattern: 'posts/:id',
|
|
24
|
-
* handler: function onRoute () {},
|
|
25
|
-
* middleware: [],
|
|
26
|
-
* matchers: {
|
|
27
|
-
* id: '^[0-9]$+'
|
|
28
|
-
* },
|
|
29
|
-
* meta: {},
|
|
30
|
-
* methods: ['GET']
|
|
31
|
-
* })
|
|
32
|
-
*
|
|
33
|
-
* store.match('posts/1', 'GET')
|
|
34
|
-
* ```
|
|
35
|
-
*/
|
|
36
|
-
export class RoutesStore {
|
|
37
|
-
/**
|
|
38
|
-
* A flag to know if routes for explicit domains
|
|
39
|
-
* have been registered
|
|
40
|
-
*/
|
|
41
|
-
usingDomains = false;
|
|
42
|
-
/**
|
|
43
|
-
* Tree of registered routes and their matchit tokens
|
|
44
|
-
*/
|
|
45
|
-
tree = { tokens: [], domains: {} };
|
|
46
|
-
/**
|
|
47
|
-
* Returns the domain node for a given domain.
|
|
48
|
-
*/
|
|
49
|
-
#getDomainNode(domain) {
|
|
50
|
-
if (!this.tree.domains[domain]) {
|
|
51
|
-
this.tree.tokens.push(parseRoutePattern(domain));
|
|
52
|
-
this.tree.domains[domain] = {};
|
|
53
|
-
}
|
|
54
|
-
return this.tree.domains[domain];
|
|
55
|
-
}
|
|
56
|
-
/**
|
|
57
|
-
* Returns the method node for a given domain and method.
|
|
58
|
-
*/
|
|
59
|
-
#getMethodNode(domain, method) {
|
|
60
|
-
const domainNode = this.#getDomainNode(domain);
|
|
61
|
-
if (!domainNode[method]) {
|
|
62
|
-
domainNode[method] = { tokens: [], routes: {}, routeKeys: {} };
|
|
63
|
-
}
|
|
64
|
-
return domainNode[method];
|
|
65
|
-
}
|
|
66
|
-
/**
|
|
67
|
-
* Collects route params
|
|
68
|
-
*/
|
|
69
|
-
#collectRouteParams(route, tokens) {
|
|
70
|
-
const collectedParams = new Set();
|
|
71
|
-
for (let token of tokens) {
|
|
72
|
-
if ([1, 3].includes(token.type)) {
|
|
73
|
-
if (collectedParams.has(token.val)) {
|
|
74
|
-
throw new RuntimeException(`Duplicate param "${token.val}" found in "${route.pattern}"`);
|
|
75
|
-
}
|
|
76
|
-
else {
|
|
77
|
-
collectedParams.add(token.val);
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
const params = [...collectedParams];
|
|
82
|
-
collectedParams.clear();
|
|
83
|
-
return params;
|
|
84
|
-
}
|
|
85
|
-
/**
|
|
86
|
-
* Register route for a given domain and method
|
|
87
|
-
*/
|
|
88
|
-
#registerRoute(domain, method, tokens, route) {
|
|
89
|
-
const methodRoutes = this.#getMethodNode(domain, method);
|
|
90
|
-
/*
|
|
91
|
-
* Check for duplicate route for the same domain and method
|
|
92
|
-
*/
|
|
93
|
-
if (methodRoutes.routes[route.pattern]) {
|
|
94
|
-
throw new RuntimeException(`Duplicate route found. "${method}: ${route.pattern}" route already exists`);
|
|
95
|
-
}
|
|
96
|
-
if (debug.enabled) {
|
|
97
|
-
debug('registering route to the store %O', route);
|
|
98
|
-
debug('route middleware %O', route.middleware.all().entries());
|
|
99
|
-
}
|
|
100
|
-
methodRoutes.tokens.push(tokens);
|
|
101
|
-
methodRoutes.routes[route.pattern] = route;
|
|
102
|
-
methodRoutes.routeKeys[route.pattern] =
|
|
103
|
-
domain !== 'root' ? `${domain}-${method}-${route.pattern}` : `${method}-${route.pattern}`;
|
|
104
|
-
}
|
|
105
|
-
/**
|
|
106
|
-
* Add a route to the store
|
|
107
|
-
*
|
|
108
|
-
* ```ts
|
|
109
|
-
* store.add({
|
|
110
|
-
* pattern: 'post/:id',
|
|
111
|
-
* methods: ['GET'],
|
|
112
|
-
* matchers: {},
|
|
113
|
-
* meta: {},
|
|
114
|
-
* handler: function handler () {
|
|
115
|
-
* }
|
|
116
|
-
* })
|
|
117
|
-
* ```
|
|
118
|
-
*/
|
|
119
|
-
add(route) {
|
|
120
|
-
/**
|
|
121
|
-
* Set flag when a custom domain is used
|
|
122
|
-
*/
|
|
123
|
-
if (route.domain !== 'root') {
|
|
124
|
-
this.usingDomains = true;
|
|
125
|
-
}
|
|
126
|
-
/**
|
|
127
|
-
* Generate tokens for the route
|
|
128
|
-
*/
|
|
129
|
-
const tokens = parseRoutePattern(route.pattern, route.matchers);
|
|
130
|
-
/**
|
|
131
|
-
* Create route node object for persistence
|
|
132
|
-
*/
|
|
133
|
-
const routeNode = lodash.merge({ meta: {} }, lodash.pick(route, ['pattern', 'handler', 'meta', 'middleware', 'name', 'execute']));
|
|
134
|
-
/**
|
|
135
|
-
* Set route params
|
|
136
|
-
*/
|
|
137
|
-
routeNode.meta.params = this.#collectRouteParams(routeNode, tokens);
|
|
138
|
-
/**
|
|
139
|
-
* Register route for every method
|
|
140
|
-
*/
|
|
141
|
-
route.methods.forEach((method) => {
|
|
142
|
-
this.#registerRoute(route.domain, method, tokens, routeNode);
|
|
143
|
-
});
|
|
144
|
-
return this;
|
|
145
|
-
}
|
|
146
|
-
/**
|
|
147
|
-
* Matches the url, method and optionally domain to pull the matching
|
|
148
|
-
* route. `null` is returned when unable to match the URL against
|
|
149
|
-
* registered routes.
|
|
150
|
-
*
|
|
151
|
-
* The domain parameter has to be a registered pattern and not the fully
|
|
152
|
-
* qualified runtime domain. You must call `matchDomain` first to fetch
|
|
153
|
-
* the pattern for qualified domain
|
|
154
|
-
*/
|
|
155
|
-
match(url, method, domain) {
|
|
156
|
-
const domainName = domain?.tokens[0]?.old || 'root';
|
|
157
|
-
const matchedDomain = this.tree.domains[domainName];
|
|
158
|
-
if (!matchedDomain) {
|
|
159
|
-
return null;
|
|
160
|
-
}
|
|
161
|
-
/*
|
|
162
|
-
* Next get the method node for the given method inside the domain. If
|
|
163
|
-
* method node is missing, means no routes ever got registered for that
|
|
164
|
-
* method
|
|
165
|
-
*/
|
|
166
|
-
const matchedMethod = this.tree.domains[domainName][method];
|
|
167
|
-
if (!matchedMethod) {
|
|
168
|
-
return null;
|
|
169
|
-
}
|
|
170
|
-
/*
|
|
171
|
-
* Next, match route for the given url inside the tokens list for the
|
|
172
|
-
* matchedMethod
|
|
173
|
-
*/
|
|
174
|
-
const matchedRoute = matchit.match(url, matchedMethod.tokens);
|
|
175
|
-
if (!matchedRoute.length) {
|
|
176
|
-
return null;
|
|
177
|
-
}
|
|
178
|
-
const route = matchedMethod.routes[matchedRoute[0].old];
|
|
179
|
-
return {
|
|
180
|
-
route: route,
|
|
181
|
-
routeKey: matchedMethod.routeKeys[route.pattern],
|
|
182
|
-
params: matchit.exec(url, matchedRoute),
|
|
183
|
-
subdomains: domain?.hostname ? matchit.exec(domain.hostname, domain.tokens) : {},
|
|
184
|
-
};
|
|
185
|
-
}
|
|
186
|
-
/**
|
|
187
|
-
* Match hostname against registered domains.
|
|
188
|
-
*/
|
|
189
|
-
matchDomain(hostname) {
|
|
190
|
-
if (!hostname || !this.usingDomains) {
|
|
191
|
-
return [];
|
|
192
|
-
}
|
|
193
|
-
return matchit.match(hostname, this.tree.tokens);
|
|
194
|
-
}
|
|
195
|
-
}
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import type { ContainerResolver } from '@adonisjs/fold';
|
|
2
|
-
import type { Router } from '../../router/main.js';
|
|
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
|
-
*/
|
|
9
|
-
export declare function finalHandler(router: Router, resolver: ContainerResolver<any>, ctx: HttpContext): () => any;
|
|
@@ -1,30 +0,0 @@
|
|
|
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
|
-
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
|
-
*/
|
|
15
|
-
export function finalHandler(router, resolver, ctx) {
|
|
16
|
-
return function () {
|
|
17
|
-
const url = ctx.request.url();
|
|
18
|
-
const method = ctx.request.method();
|
|
19
|
-
const hostname = router.usingDomains ? ctx.request.hostname() : undefined;
|
|
20
|
-
const route = router.match(url, method, hostname);
|
|
21
|
-
if (route) {
|
|
22
|
-
ctx.params = route.params;
|
|
23
|
-
ctx.subdomains = route.subdomains;
|
|
24
|
-
ctx.route = route.route;
|
|
25
|
-
ctx.routeKey = route.routeKey;
|
|
26
|
-
return route.route.execute(route.route, resolver, ctx);
|
|
27
|
-
}
|
|
28
|
-
return Promise.reject(new errors.E_ROUTE_NOT_FOUND([method, url]));
|
|
29
|
-
};
|
|
30
|
-
}
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import type { NextFn } from '@poppinss/middleware/types';
|
|
2
|
-
import type { ContainerResolver } from '@adonisjs/fold';
|
|
3
|
-
import type { HttpContext } from '../../http_context/main.js';
|
|
4
|
-
import { ParsedGlobalMiddleware } from '../../types/middleware.js';
|
|
5
|
-
/**
|
|
6
|
-
* The middleware handler invokes the middleware functions.
|
|
7
|
-
*/
|
|
8
|
-
export declare function middlewareHandler(resolver: ContainerResolver<any>, ctx: HttpContext): (fn: ParsedGlobalMiddleware, next: NextFn) => any;
|
|
@@ -1,16 +0,0 @@
|
|
|
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
|
-
*/
|
|
12
|
-
export function middlewareHandler(resolver, ctx) {
|
|
13
|
-
return function (fn, next) {
|
|
14
|
-
return fn.handle(resolver, ctx, next);
|
|
15
|
-
};
|
|
16
|
-
}
|
|
@@ -1,24 +0,0 @@
|
|
|
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
|
-
*/
|
|
13
|
-
export function writeResponse(ctx) {
|
|
14
|
-
return function () {
|
|
15
|
-
try {
|
|
16
|
-
ctx.response.finish();
|
|
17
|
-
}
|
|
18
|
-
catch (error) {
|
|
19
|
-
ctx.logger.fatal({ err: error }, 'Response serialization failed');
|
|
20
|
-
ctx.response.internalServerError(error.message);
|
|
21
|
-
ctx.response.finish();
|
|
22
|
-
}
|
|
23
|
-
};
|
|
24
|
-
}
|