@adonisjs/http-server 6.8.2-6 → 6.8.2-8
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 +151 -2
- package/build/src/router/store.d.ts +54 -1
- package/build/src/router/store.js +107 -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 +44 -71
- 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
package/src/router/store.ts
DELETED
|
@@ -1,239 +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
|
-
// @ts-expect-error
|
|
11
|
-
import matchit from '@poppinss/matchit'
|
|
12
|
-
import lodash from '@poppinss/utils/lodash'
|
|
13
|
-
import { RuntimeException } from '@poppinss/utils'
|
|
14
|
-
import type {
|
|
15
|
-
RouteJSON,
|
|
16
|
-
MatchedRoute,
|
|
17
|
-
StoreRouteNode,
|
|
18
|
-
StoreDomainNode,
|
|
19
|
-
StoreMethodNode,
|
|
20
|
-
StoreRoutesTree,
|
|
21
|
-
MatchItRouteToken,
|
|
22
|
-
} from '../types/route.js'
|
|
23
|
-
import { parseRoutePattern } from './parser.js'
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Store class is used to store a list of routes, along side with their tokens
|
|
27
|
-
* to match the URLs.
|
|
28
|
-
*
|
|
29
|
-
* ```ts
|
|
30
|
-
* const store = new Store()
|
|
31
|
-
*
|
|
32
|
-
* store.add({
|
|
33
|
-
* pattern: 'posts/:id',
|
|
34
|
-
* handler: function onRoute () {},
|
|
35
|
-
* middleware: [],
|
|
36
|
-
* matchers: {
|
|
37
|
-
* id: '^[0-9]$+'
|
|
38
|
-
* },
|
|
39
|
-
* meta: {},
|
|
40
|
-
* methods: ['GET']
|
|
41
|
-
* })
|
|
42
|
-
*
|
|
43
|
-
* store.match('posts/1', 'GET')
|
|
44
|
-
* ```
|
|
45
|
-
*/
|
|
46
|
-
export class RoutesStore {
|
|
47
|
-
/**
|
|
48
|
-
* A flag to know if routes for explicit domains
|
|
49
|
-
* have been registered
|
|
50
|
-
*/
|
|
51
|
-
usingDomains: boolean = false
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Tree of registered routes and their matchit tokens
|
|
55
|
-
*/
|
|
56
|
-
tree: StoreRoutesTree = { tokens: [], domains: {} }
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Returns the domain node for a given domain.
|
|
60
|
-
*/
|
|
61
|
-
#getDomainNode(domain: string): StoreDomainNode {
|
|
62
|
-
if (!this.tree.domains[domain]) {
|
|
63
|
-
this.tree.tokens.push(parseRoutePattern(domain))
|
|
64
|
-
this.tree.domains[domain] = {}
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
return this.tree.domains[domain]
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Returns the method node for a given domain and method.
|
|
72
|
-
*/
|
|
73
|
-
#getMethodNode(domain: string, method: string): StoreMethodNode {
|
|
74
|
-
const domainNode = this.#getDomainNode(domain)
|
|
75
|
-
if (!domainNode[method]) {
|
|
76
|
-
domainNode[method] = { tokens: [], routes: {}, routeKeys: {} }
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
return domainNode[method]
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* Collects route params
|
|
84
|
-
*/
|
|
85
|
-
#collectRouteParams(route: StoreRouteNode, tokens: MatchItRouteToken[]) {
|
|
86
|
-
const collectedParams: Set<string> = new Set()
|
|
87
|
-
|
|
88
|
-
for (let token of tokens) {
|
|
89
|
-
if ([1, 3].includes(token.type)) {
|
|
90
|
-
if (collectedParams.has(token.val)) {
|
|
91
|
-
throw new RuntimeException(`Duplicate param "${token.val}" found in "${route.pattern}"`)
|
|
92
|
-
} else {
|
|
93
|
-
collectedParams.add(token.val)
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
const params = [...collectedParams]
|
|
99
|
-
collectedParams.clear()
|
|
100
|
-
|
|
101
|
-
return params
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
* Register route for a given domain and method
|
|
106
|
-
*/
|
|
107
|
-
#registerRoute(
|
|
108
|
-
domain: string,
|
|
109
|
-
method: string,
|
|
110
|
-
tokens: MatchItRouteToken[],
|
|
111
|
-
route: StoreRouteNode
|
|
112
|
-
) {
|
|
113
|
-
const methodRoutes = this.#getMethodNode(domain, method)
|
|
114
|
-
|
|
115
|
-
/*
|
|
116
|
-
* Check for duplicate route for the same domain and method
|
|
117
|
-
*/
|
|
118
|
-
if (methodRoutes.routes[route.pattern]) {
|
|
119
|
-
throw new RuntimeException(
|
|
120
|
-
`Duplicate route found. "${method}: ${route.pattern}" route already exists`
|
|
121
|
-
)
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
methodRoutes.tokens.push(tokens)
|
|
125
|
-
methodRoutes.routes[route.pattern] = route
|
|
126
|
-
methodRoutes.routeKeys[route.pattern] =
|
|
127
|
-
domain !== 'root' ? `${domain}-${method}-${route.pattern}` : `${method}-${route.pattern}`
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* Add a route to the store
|
|
132
|
-
*
|
|
133
|
-
* ```ts
|
|
134
|
-
* store.add({
|
|
135
|
-
* pattern: 'post/:id',
|
|
136
|
-
* methods: ['GET'],
|
|
137
|
-
* matchers: {},
|
|
138
|
-
* meta: {},
|
|
139
|
-
* handler: function handler () {
|
|
140
|
-
* }
|
|
141
|
-
* })
|
|
142
|
-
* ```
|
|
143
|
-
*/
|
|
144
|
-
add(route: RouteJSON): this {
|
|
145
|
-
/**
|
|
146
|
-
* Set flag when a custom domain is used
|
|
147
|
-
*/
|
|
148
|
-
if (route.domain !== 'root') {
|
|
149
|
-
this.usingDomains = true
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
/**
|
|
153
|
-
* Generate tokens for the route
|
|
154
|
-
*/
|
|
155
|
-
const tokens = parseRoutePattern(route.pattern, route.matchers)
|
|
156
|
-
|
|
157
|
-
/**
|
|
158
|
-
* Create route node object for persistence
|
|
159
|
-
*/
|
|
160
|
-
const routeNode: StoreRouteNode = lodash.merge(
|
|
161
|
-
{ meta: {} },
|
|
162
|
-
lodash.pick(route, ['pattern', 'handler', 'meta', 'middleware', 'name', 'execute'])
|
|
163
|
-
)
|
|
164
|
-
|
|
165
|
-
/**
|
|
166
|
-
* Set route params
|
|
167
|
-
*/
|
|
168
|
-
routeNode.meta.params = this.#collectRouteParams(routeNode, tokens)
|
|
169
|
-
|
|
170
|
-
/**
|
|
171
|
-
* Register route for every method
|
|
172
|
-
*/
|
|
173
|
-
route.methods.forEach((method) => {
|
|
174
|
-
this.#registerRoute(route.domain, method, tokens, routeNode)
|
|
175
|
-
})
|
|
176
|
-
|
|
177
|
-
return this
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
/**
|
|
181
|
-
* Matches the url, method and optionally domain to pull the matching
|
|
182
|
-
* route. `null` is returned when unable to match the URL against
|
|
183
|
-
* registered routes.
|
|
184
|
-
*
|
|
185
|
-
* The domain parameter has to be a registered pattern and not the fully
|
|
186
|
-
* qualified runtime domain. You must call `matchDomain` first to fetch
|
|
187
|
-
* the pattern for qualified domain
|
|
188
|
-
*/
|
|
189
|
-
match(
|
|
190
|
-
url: string,
|
|
191
|
-
method: string,
|
|
192
|
-
domain?: { tokens: MatchItRouteToken[]; hostname: string }
|
|
193
|
-
): null | MatchedRoute {
|
|
194
|
-
const domainName = domain?.tokens[0]?.old || 'root'
|
|
195
|
-
|
|
196
|
-
const matchedDomain = this.tree.domains[domainName]
|
|
197
|
-
if (!matchedDomain) {
|
|
198
|
-
return null
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
/*
|
|
202
|
-
* Next get the method node for the given method inside the domain. If
|
|
203
|
-
* method node is missing, means no routes ever got registered for that
|
|
204
|
-
* method
|
|
205
|
-
*/
|
|
206
|
-
const matchedMethod = this.tree.domains[domainName][method]
|
|
207
|
-
if (!matchedMethod) {
|
|
208
|
-
return null
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
/*
|
|
212
|
-
* Next, match route for the given url inside the tokens list for the
|
|
213
|
-
* matchedMethod
|
|
214
|
-
*/
|
|
215
|
-
const matchedRoute = matchit.match(url, matchedMethod.tokens)
|
|
216
|
-
if (!matchedRoute.length) {
|
|
217
|
-
return null
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
const route = matchedMethod.routes[matchedRoute[0].old]
|
|
221
|
-
return {
|
|
222
|
-
route: route,
|
|
223
|
-
routeKey: matchedMethod.routeKeys[route.pattern],
|
|
224
|
-
params: matchit.exec(url, matchedRoute),
|
|
225
|
-
subdomains: domain?.hostname ? matchit.exec(domain.hostname, domain.tokens) : {},
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
/**
|
|
230
|
-
* Match hostname against registered domains.
|
|
231
|
-
*/
|
|
232
|
-
matchDomain(hostname?: string | null): MatchItRouteToken[] {
|
|
233
|
-
if (!hostname || !this.usingDomains) {
|
|
234
|
-
return []
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
return matchit.match(hostname, this.tree.tokens)
|
|
238
|
-
}
|
|
239
|
-
}
|
|
@@ -1,38 +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
|
-
import type { ContainerResolver } from '@adonisjs/fold'
|
|
11
|
-
|
|
12
|
-
import * as errors from '../../exceptions.js'
|
|
13
|
-
import type { Router } from '../../router/main.js'
|
|
14
|
-
import type { HttpContext } from '../../http_context/main.js'
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* The final handler is executed after the server middleware stack.
|
|
18
|
-
* It looks for a matching route and executes the route middleware
|
|
19
|
-
* stack.
|
|
20
|
-
*/
|
|
21
|
-
export function finalHandler(router: Router, resolver: ContainerResolver<any>, ctx: HttpContext) {
|
|
22
|
-
return function () {
|
|
23
|
-
const url = ctx.request.url()
|
|
24
|
-
const method = ctx.request.method()
|
|
25
|
-
const hostname = router.usingDomains ? ctx.request.hostname() : undefined
|
|
26
|
-
const route = router.match(url, method, hostname)
|
|
27
|
-
|
|
28
|
-
if (route) {
|
|
29
|
-
ctx.params = route.params
|
|
30
|
-
ctx.subdomains = route.subdomains
|
|
31
|
-
ctx.route = route.route
|
|
32
|
-
ctx.routeKey = route.routeKey
|
|
33
|
-
return route.route.execute(route.route, resolver, ctx)
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
return Promise.reject(new errors.E_ROUTE_NOT_FOUND([method, url]))
|
|
37
|
-
}
|
|
38
|
-
}
|
|
@@ -1,23 +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
|
-
import type { NextFn } from '@poppinss/middleware/types'
|
|
11
|
-
import type { ContainerResolver } from '@adonisjs/fold'
|
|
12
|
-
|
|
13
|
-
import type { HttpContext } from '../../http_context/main.js'
|
|
14
|
-
import { ParsedGlobalMiddleware } from '../../types/middleware.js'
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* The middleware handler invokes the middleware functions.
|
|
18
|
-
*/
|
|
19
|
-
export function middlewareHandler(resolver: ContainerResolver<any>, ctx: HttpContext) {
|
|
20
|
-
return function (fn: ParsedGlobalMiddleware, next: NextFn) {
|
|
21
|
-
return fn.handle(resolver, ctx, next)
|
|
22
|
-
}
|
|
23
|
-
}
|
|
@@ -1,26 +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
|
-
import type { HttpContext } from '../../http_context/main.js'
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Writes the response to the socket. The "finish" method can
|
|
14
|
-
* raise error when unable to serialize the response.
|
|
15
|
-
*/
|
|
16
|
-
export function writeResponse(ctx: HttpContext) {
|
|
17
|
-
return function () {
|
|
18
|
-
try {
|
|
19
|
-
ctx.response.finish()
|
|
20
|
-
} catch (error) {
|
|
21
|
-
ctx.logger.fatal({ err: error }, 'Response serialization failed')
|
|
22
|
-
ctx.response.internalServerError(error.message)
|
|
23
|
-
ctx.response.finish()
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
}
|
package/src/server/main.ts
DELETED
|
@@ -1,356 +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
|
-
import onFinished from 'on-finished'
|
|
11
|
-
import { Emitter } from '@adonisjs/events'
|
|
12
|
-
import Middleware from '@poppinss/middleware'
|
|
13
|
-
import type { Logger } from '@adonisjs/logger'
|
|
14
|
-
import type { Encryption } from '@adonisjs/encryption'
|
|
15
|
-
import type { Server as HttpsServer } from 'node:https'
|
|
16
|
-
import type { Application } from '@adonisjs/application'
|
|
17
|
-
import { ContainerResolver, moduleCaller, moduleImporter } from '@adonisjs/fold'
|
|
18
|
-
import type { ServerResponse, IncomingMessage, Server as HttpServer } from 'node:http'
|
|
19
|
-
|
|
20
|
-
import type { LazyImport } from '../types/base.js'
|
|
21
|
-
import type { MiddlewareAsClass, ParsedGlobalMiddleware } from '../types/middleware.js'
|
|
22
|
-
import type {
|
|
23
|
-
ServerConfig,
|
|
24
|
-
ServerErrorHandler,
|
|
25
|
-
ErrorHandlerAsAClass,
|
|
26
|
-
TestingMiddlewarePipeline,
|
|
27
|
-
} from '../types/server.js'
|
|
28
|
-
|
|
29
|
-
import { Qs } from '../qs.js'
|
|
30
|
-
import debug from '../debug.js'
|
|
31
|
-
import { Request } from '../request.js'
|
|
32
|
-
import { Response } from '../response.js'
|
|
33
|
-
import { Router } from '../router/main.js'
|
|
34
|
-
import { HttpContext } from '../http_context/main.js'
|
|
35
|
-
import { finalHandler } from './factories/final_handler.js'
|
|
36
|
-
import { writeResponse } from './factories/write_response.js'
|
|
37
|
-
import { asyncLocalStorage } from '../http_context/local_storage.js'
|
|
38
|
-
import { middlewareHandler } from './factories/middleware_handler.js'
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* The HTTP server implementation to handle incoming requests and respond using the
|
|
42
|
-
* registered routes.
|
|
43
|
-
*/
|
|
44
|
-
export class Server {
|
|
45
|
-
/**
|
|
46
|
-
* The default error handler to use
|
|
47
|
-
*/
|
|
48
|
-
#defaultErrorHandler: ServerErrorHandler = {
|
|
49
|
-
report() {},
|
|
50
|
-
handle(error, ctx) {
|
|
51
|
-
ctx.response.status(error.status || 500).send(error.message || 'Internal server error')
|
|
52
|
-
},
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Logger instance, a child logger is added
|
|
57
|
-
* to the context to have request specific
|
|
58
|
-
* logging capabilities.
|
|
59
|
-
*/
|
|
60
|
-
#logger: Logger
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Registered error handler (if any)
|
|
64
|
-
*/
|
|
65
|
-
#errorHandler?: LazyImport<ErrorHandlerAsAClass>
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* Resolved error handler is an instance of the lazily imported error
|
|
69
|
-
* handler class.
|
|
70
|
-
*/
|
|
71
|
-
#resolvedErrorHandler: ServerErrorHandler = this.#defaultErrorHandler
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* Emitter is required to notify when a request finishes
|
|
75
|
-
*/
|
|
76
|
-
#emitter: Emitter<any>
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* The application instance to be shared with the router
|
|
80
|
-
*/
|
|
81
|
-
#app: Application<any>
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* The encryption instance to be shared with the router
|
|
85
|
-
*/
|
|
86
|
-
#encryption: Encryption
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Server config
|
|
90
|
-
*/
|
|
91
|
-
#config: ServerConfig
|
|
92
|
-
|
|
93
|
-
/**
|
|
94
|
-
* Query string parser used by the server
|
|
95
|
-
*/
|
|
96
|
-
#qsParser: Qs
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* Server middleware stack runs on every incoming HTTP request
|
|
100
|
-
*/
|
|
101
|
-
#serverMiddlewareStack?: Middleware<ParsedGlobalMiddleware>
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* Reference to the router used by the server
|
|
105
|
-
*/
|
|
106
|
-
#router: Router
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* Reference to the underlying Node HTTP server in use
|
|
110
|
-
*/
|
|
111
|
-
#nodeHttpServer?: HttpServer | HttpsServer
|
|
112
|
-
|
|
113
|
-
/**
|
|
114
|
-
* Middleware store to be shared with the routes
|
|
115
|
-
*/
|
|
116
|
-
#middleware: ParsedGlobalMiddleware[] = []
|
|
117
|
-
|
|
118
|
-
/**
|
|
119
|
-
* Know if async local storage is enabled or not.
|
|
120
|
-
*/
|
|
121
|
-
get usingAsyncLocalStorage() {
|
|
122
|
-
return asyncLocalStorage.isEnabled
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
constructor(
|
|
126
|
-
app: Application<any>,
|
|
127
|
-
encryption: Encryption,
|
|
128
|
-
emitter: Emitter<any>,
|
|
129
|
-
logger: Logger,
|
|
130
|
-
config: ServerConfig
|
|
131
|
-
) {
|
|
132
|
-
this.#app = app
|
|
133
|
-
this.#emitter = emitter
|
|
134
|
-
this.#config = config
|
|
135
|
-
this.#logger = logger
|
|
136
|
-
this.#encryption = encryption
|
|
137
|
-
this.#qsParser = new Qs(this.#config.qs)
|
|
138
|
-
this.#router = new Router(this.#app, this.#encryption, this.#qsParser)
|
|
139
|
-
this.#createAsyncLocalStore()
|
|
140
|
-
|
|
141
|
-
debug('server config: %O', this.#config)
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
/**
|
|
145
|
-
* Create async local storage store when enabled
|
|
146
|
-
*/
|
|
147
|
-
#createAsyncLocalStore() {
|
|
148
|
-
if (this.#config.useAsyncLocalStorage) {
|
|
149
|
-
debug('creating ALS store for HTTP context')
|
|
150
|
-
asyncLocalStorage.create()
|
|
151
|
-
} else {
|
|
152
|
-
asyncLocalStorage.destroy()
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
/**
|
|
157
|
-
* Creates an instance of the server middleware stack
|
|
158
|
-
*/
|
|
159
|
-
#createServerMiddlewareStack() {
|
|
160
|
-
this.#serverMiddlewareStack = new Middleware()
|
|
161
|
-
this.#middleware.forEach((middleware) => this.#serverMiddlewareStack!.add(middleware))
|
|
162
|
-
this.#serverMiddlewareStack.freeze()
|
|
163
|
-
this.#middleware = []
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
/**
|
|
167
|
-
* Handles the HTTP request
|
|
168
|
-
*/
|
|
169
|
-
#handleRequest(ctx: HttpContext, resolver: ContainerResolver<any>) {
|
|
170
|
-
return this.#serverMiddlewareStack!.runner()
|
|
171
|
-
.errorHandler((error) => {
|
|
172
|
-
this.#resolvedErrorHandler.report(error, ctx)
|
|
173
|
-
return this.#resolvedErrorHandler.handle(error, ctx)
|
|
174
|
-
})
|
|
175
|
-
.finalHandler(finalHandler(this.#router!, resolver, ctx))
|
|
176
|
-
.run(middlewareHandler(resolver, ctx))
|
|
177
|
-
.catch((error) => {
|
|
178
|
-
ctx.logger.fatal({ err: error }, 'Exception raised by error handler')
|
|
179
|
-
return this.#defaultErrorHandler.handle(error, ctx)
|
|
180
|
-
})
|
|
181
|
-
.finally(writeResponse(ctx))
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
/**
|
|
185
|
-
* Creates a pipeline of middleware.
|
|
186
|
-
*/
|
|
187
|
-
pipeline(middleware: MiddlewareAsClass[]): TestingMiddlewarePipeline {
|
|
188
|
-
const middlewareStack = new Middleware<ParsedGlobalMiddleware>()
|
|
189
|
-
middleware.forEach((one) => {
|
|
190
|
-
middlewareStack.add(moduleCaller(one, 'handle').toHandleMethod())
|
|
191
|
-
})
|
|
192
|
-
|
|
193
|
-
middlewareStack.freeze()
|
|
194
|
-
const stackRunner = middlewareStack.runner()
|
|
195
|
-
|
|
196
|
-
return {
|
|
197
|
-
finalHandler(handler) {
|
|
198
|
-
stackRunner.finalHandler(handler)
|
|
199
|
-
return this
|
|
200
|
-
},
|
|
201
|
-
errorHandler(handler) {
|
|
202
|
-
stackRunner.errorHandler(handler)
|
|
203
|
-
return this
|
|
204
|
-
},
|
|
205
|
-
run(ctx) {
|
|
206
|
-
return stackRunner.run((handler, next) => {
|
|
207
|
-
return handler.handle(ctx.containerResolver, ctx, next)
|
|
208
|
-
})
|
|
209
|
-
},
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
/**
|
|
214
|
-
* Define an array of middleware to use on all the incoming HTTP request.
|
|
215
|
-
* Calling this method multiple times pushes to the existing list
|
|
216
|
-
* of middleware
|
|
217
|
-
*/
|
|
218
|
-
use(middleware: LazyImport<MiddlewareAsClass>[]): this {
|
|
219
|
-
middleware.forEach((one) =>
|
|
220
|
-
this.#middleware.push(moduleImporter(one, 'handle').toHandleMethod())
|
|
221
|
-
)
|
|
222
|
-
|
|
223
|
-
return this
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
/**
|
|
227
|
-
* Register a custom error handler for HTTP requests.
|
|
228
|
-
* All errors will be reported to this method
|
|
229
|
-
*/
|
|
230
|
-
errorHandler(handler: LazyImport<ErrorHandlerAsAClass>): this {
|
|
231
|
-
this.#errorHandler = handler
|
|
232
|
-
return this
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
/**
|
|
236
|
-
* Boot the server. Calling this method performs the following actions.
|
|
237
|
-
*
|
|
238
|
-
* - Register routes with the store.
|
|
239
|
-
* - Resolve and construct the error handler.
|
|
240
|
-
*/
|
|
241
|
-
async boot() {
|
|
242
|
-
debug('booting HTTP server')
|
|
243
|
-
|
|
244
|
-
/**
|
|
245
|
-
* Creates the middleware stack for the server
|
|
246
|
-
*/
|
|
247
|
-
this.#createServerMiddlewareStack()
|
|
248
|
-
|
|
249
|
-
/**
|
|
250
|
-
* Commit routes
|
|
251
|
-
*/
|
|
252
|
-
this.#router.commit()
|
|
253
|
-
|
|
254
|
-
/**
|
|
255
|
-
* Register custom error handler
|
|
256
|
-
*/
|
|
257
|
-
if (this.#errorHandler) {
|
|
258
|
-
if (debug.enabled) {
|
|
259
|
-
debug('using custom error handler "%s"', this.#errorHandler)
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
const moduleExports = await this.#errorHandler()
|
|
263
|
-
this.#resolvedErrorHandler = await this.#app.container.make(moduleExports.default)
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
/**
|
|
268
|
-
* Set the HTTP server instance used to listen for requests.
|
|
269
|
-
*/
|
|
270
|
-
setNodeServer(server: HttpServer | HttpsServer) {
|
|
271
|
-
this.#nodeHttpServer = server
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
/**
|
|
275
|
-
* Returns reference to the underlying HTTP server
|
|
276
|
-
* in use
|
|
277
|
-
*/
|
|
278
|
-
getNodeServer() {
|
|
279
|
-
return this.#nodeHttpServer
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
/**
|
|
283
|
-
* Returns reference to the router instance used
|
|
284
|
-
* by the server.
|
|
285
|
-
*/
|
|
286
|
-
getRouter(): Router {
|
|
287
|
-
return this.#router
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
/**
|
|
291
|
-
* Creates an instance of the [[Request]] class
|
|
292
|
-
*/
|
|
293
|
-
createRequest(req: IncomingMessage, res: ServerResponse) {
|
|
294
|
-
return new Request(req, res, this.#encryption, this.#config, this.#qsParser)
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
/**
|
|
298
|
-
* Creates an instance of the [[Response]] class
|
|
299
|
-
*/
|
|
300
|
-
createResponse(req: IncomingMessage, res: ServerResponse) {
|
|
301
|
-
return new Response(req, res, this.#encryption, this.#config, this.#router, this.#qsParser)
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
/**
|
|
305
|
-
* Creates an instance of the [[HttpContext]] class
|
|
306
|
-
*/
|
|
307
|
-
createHttpContext(request: Request, response: Response, resolver: ContainerResolver<any>) {
|
|
308
|
-
return new HttpContext(
|
|
309
|
-
request,
|
|
310
|
-
response,
|
|
311
|
-
this.#logger.child({ request_id: request.id() }),
|
|
312
|
-
resolver
|
|
313
|
-
)
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
/**
|
|
317
|
-
* Handle request
|
|
318
|
-
*/
|
|
319
|
-
handle(req: IncomingMessage, res: ServerResponse) {
|
|
320
|
-
/**
|
|
321
|
-
* Setup for the "http:request_finished" event
|
|
322
|
-
*/
|
|
323
|
-
const hasRequestListener = this.#emitter.hasListeners('http:request_finished')
|
|
324
|
-
const startTime = hasRequestListener ? process.hrtime() : null
|
|
325
|
-
|
|
326
|
-
/**
|
|
327
|
-
* Creating essential instances
|
|
328
|
-
*/
|
|
329
|
-
const resolver = this.#app.container.createResolver()
|
|
330
|
-
const ctx = this.createHttpContext(
|
|
331
|
-
this.createRequest(req, res),
|
|
332
|
-
this.createResponse(req, res),
|
|
333
|
-
resolver
|
|
334
|
-
)
|
|
335
|
-
|
|
336
|
-
/**
|
|
337
|
-
* Emit event when listening for the request_finished event
|
|
338
|
-
*/
|
|
339
|
-
if (startTime) {
|
|
340
|
-
onFinished(res, () => {
|
|
341
|
-
this.#emitter.emit('http:request_finished', {
|
|
342
|
-
ctx: ctx,
|
|
343
|
-
duration: process.hrtime(startTime),
|
|
344
|
-
})
|
|
345
|
-
})
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
/**
|
|
349
|
-
* Handle request
|
|
350
|
-
*/
|
|
351
|
-
if (this.usingAsyncLocalStorage) {
|
|
352
|
-
return asyncLocalStorage.storage!.run(ctx, () => this.#handleRequest(ctx, resolver))
|
|
353
|
-
}
|
|
354
|
-
return this.#handleRequest(ctx, resolver)
|
|
355
|
-
}
|
|
356
|
-
}
|
package/src/types/base.ts
DELETED
|
@@ -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
|
-
|
|
10
|
-
export type { NextFn } from '@poppinss/middleware/types'
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Accept one or more of the mentioned type
|
|
14
|
-
*/
|
|
15
|
-
export type OneOrMore<T> = T | T[]
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Class constructor type
|
|
19
|
-
*/
|
|
20
|
-
export type Constructor<T> = new (...args: any[]) => T
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* A function that lazily imports a middleware
|
|
24
|
-
*/
|
|
25
|
-
export type LazyImport<DefaultExport> = () => Promise<{ default: DefaultExport }>
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Unwraps default export from a lazy import function
|
|
29
|
-
*/
|
|
30
|
-
export type UnWrapLazyImport<Fn extends LazyImport<any>> = Awaited<ReturnType<Fn>>['default']
|