@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.
Files changed (208) hide show
  1. package/README.md +3 -6
  2. package/build/factories/http_context.d.ts +10 -1
  3. package/build/factories/http_context.js +27 -0
  4. package/build/factories/http_server.d.ts +8 -0
  5. package/build/factories/http_server.js +26 -0
  6. package/build/factories/main.d.ts +0 -1
  7. package/build/factories/main.js +8 -0
  8. package/build/factories/qs_parser_factory.d.ts +10 -1
  9. package/build/factories/qs_parser_factory.js +18 -0
  10. package/build/factories/request.d.ts +10 -1
  11. package/build/factories/request.js +31 -0
  12. package/build/factories/response.d.ts +10 -1
  13. package/build/factories/response.js +34 -0
  14. package/build/factories/router.d.ts +10 -1
  15. package/build/factories/router.js +25 -0
  16. package/build/factories/server_factory.d.ts +10 -1
  17. package/build/factories/server_factory.js +34 -0
  18. package/build/index.d.ts +0 -1
  19. package/build/index.js +8 -0
  20. package/build/src/cookies/client.d.ts +25 -1
  21. package/build/src/cookies/client.js +42 -0
  22. package/build/src/cookies/drivers/encrypted.d.ts +12 -1
  23. package/build/src/cookies/drivers/encrypted.js +20 -0
  24. package/build/src/cookies/drivers/plain.d.ts +12 -1
  25. package/build/src/cookies/drivers/plain.js +20 -0
  26. package/build/src/cookies/drivers/signed.d.ts +12 -1
  27. package/build/src/cookies/drivers/signed.js +20 -0
  28. package/build/src/cookies/parser.d.ts +28 -1
  29. package/build/src/cookies/parser.js +98 -0
  30. package/build/src/cookies/serializer.d.ts +22 -1
  31. package/build/src/cookies/serializer.js +40 -0
  32. package/build/src/debug.d.ts +0 -1
  33. package/build/src/debug.js +8 -0
  34. package/build/src/define_config.d.ts +3 -1
  35. package/build/src/define_config.js +11 -0
  36. package/build/src/define_middleware.d.ts +5 -1
  37. package/build/src/define_middleware.js +18 -0
  38. package/build/src/exception_handler.d.ts +65 -1
  39. package/build/src/exception_handler.js +95 -0
  40. package/build/src/exceptions.d.ts +6 -1
  41. package/build/src/exceptions.js +11 -0
  42. package/build/src/helpers.d.ts +14 -1
  43. package/build/src/helpers.js +22 -0
  44. package/build/src/http_context/local_storage.d.ts +3 -1
  45. package/build/src/http_context/local_storage.js +25 -0
  46. package/build/src/http_context/main.d.ts +36 -1
  47. package/build/src/http_context/main.js +54 -0
  48. package/build/src/qs.d.ts +4 -1
  49. package/build/src/qs.js +12 -0
  50. package/build/src/redirect.d.ts +24 -1
  51. package/build/src/redirect.js +60 -0
  52. package/build/src/request.d.ts +466 -1
  53. package/build/src/request.js +542 -0
  54. package/build/src/response.d.ts +425 -2
  55. package/build/src/response.js +608 -7
  56. package/build/src/router/brisk.d.ts +22 -1
  57. package/build/src/router/brisk.js +42 -0
  58. package/build/src/router/executor.d.ts +4 -1
  59. package/build/src/router/executor.js +12 -0
  60. package/build/src/router/factories/use_return_value.d.ts +4 -1
  61. package/build/src/router/factories/use_return_value.js +16 -3
  62. package/build/src/router/group.d.ts +47 -1
  63. package/build/src/router/group.js +88 -0
  64. package/build/src/router/lookup_store/main.d.ts +32 -1
  65. package/build/src/router/lookup_store/main.js +49 -0
  66. package/build/src/router/lookup_store/route_finder.d.ts +13 -1
  67. package/build/src/router/lookup_store/route_finder.js +21 -0
  68. package/build/src/router/lookup_store/url_builder.d.ts +36 -1
  69. package/build/src/router/lookup_store/url_builder.js +97 -0
  70. package/build/src/router/main.d.ts +87 -1
  71. package/build/src/router/main.js +142 -0
  72. package/build/src/router/matchers.d.ts +13 -1
  73. package/build/src/router/matchers.js +21 -0
  74. package/build/src/router/parser.d.ts +3 -1
  75. package/build/src/router/parser.js +12 -0
  76. package/build/src/router/resource.d.ts +28 -1
  77. package/build/src/router/resource.js +90 -0
  78. package/build/src/router/route.d.ts +65 -1
  79. package/build/src/router/route.js +151 -2
  80. package/build/src/router/store.d.ts +54 -1
  81. package/build/src/router/store.js +107 -0
  82. package/build/src/server/factories/final_handler.d.ts +5 -1
  83. package/build/src/server/factories/final_handler.js +13 -0
  84. package/build/src/server/factories/middleware_handler.d.ts +3 -1
  85. package/build/src/server/factories/middleware_handler.js +11 -0
  86. package/build/src/server/factories/write_response.d.ts +4 -1
  87. package/build/src/server/factories/write_response.js +12 -0
  88. package/build/src/server/main.d.ts +48 -1
  89. package/build/src/server/main.js +128 -0
  90. package/build/src/types/base.d.ts +12 -1
  91. package/build/src/types/base.js +8 -0
  92. package/build/src/types/main.d.ts +0 -1
  93. package/build/src/types/main.js +8 -0
  94. package/build/src/types/middleware.d.ts +18 -1
  95. package/build/src/types/middleware.js +8 -0
  96. package/build/src/types/qs.d.ts +53 -1
  97. package/build/src/types/qs.js +8 -0
  98. package/build/src/types/request.d.ts +32 -1
  99. package/build/src/types/request.js +8 -0
  100. package/build/src/types/response.d.ts +27 -1
  101. package/build/src/types/response.js +8 -0
  102. package/build/src/types/route.d.ts +87 -1
  103. package/build/src/types/route.js +8 -0
  104. package/build/src/types/server.d.ts +35 -1
  105. package/build/src/types/server.js +8 -0
  106. package/package.json +44 -71
  107. package/build/factories/http_context.d.ts.map +0 -1
  108. package/build/factories/main.d.ts.map +0 -1
  109. package/build/factories/qs_parser_factory.d.ts.map +0 -1
  110. package/build/factories/request.d.ts.map +0 -1
  111. package/build/factories/response.d.ts.map +0 -1
  112. package/build/factories/router.d.ts.map +0 -1
  113. package/build/factories/server_factory.d.ts.map +0 -1
  114. package/build/index.d.ts.map +0 -1
  115. package/build/src/cookies/client.d.ts.map +0 -1
  116. package/build/src/cookies/drivers/encrypted.d.ts.map +0 -1
  117. package/build/src/cookies/drivers/plain.d.ts.map +0 -1
  118. package/build/src/cookies/drivers/signed.d.ts.map +0 -1
  119. package/build/src/cookies/parser.d.ts.map +0 -1
  120. package/build/src/cookies/serializer.d.ts.map +0 -1
  121. package/build/src/debug.d.ts.map +0 -1
  122. package/build/src/define_config.d.ts.map +0 -1
  123. package/build/src/define_middleware.d.ts.map +0 -1
  124. package/build/src/exception_handler.d.ts.map +0 -1
  125. package/build/src/exceptions.d.ts.map +0 -1
  126. package/build/src/helpers.d.ts.map +0 -1
  127. package/build/src/http_context/local_storage.d.ts.map +0 -1
  128. package/build/src/http_context/main.d.ts.map +0 -1
  129. package/build/src/qs.d.ts.map +0 -1
  130. package/build/src/redirect.d.ts.map +0 -1
  131. package/build/src/request.d.ts.map +0 -1
  132. package/build/src/response.d.ts.map +0 -1
  133. package/build/src/router/brisk.d.ts.map +0 -1
  134. package/build/src/router/executor.d.ts.map +0 -1
  135. package/build/src/router/factories/use_return_value.d.ts.map +0 -1
  136. package/build/src/router/group.d.ts.map +0 -1
  137. package/build/src/router/lookup_store/main.d.ts.map +0 -1
  138. package/build/src/router/lookup_store/route_finder.d.ts.map +0 -1
  139. package/build/src/router/lookup_store/url_builder.d.ts.map +0 -1
  140. package/build/src/router/main.d.ts.map +0 -1
  141. package/build/src/router/matchers.d.ts.map +0 -1
  142. package/build/src/router/parser.d.ts.map +0 -1
  143. package/build/src/router/resource.d.ts.map +0 -1
  144. package/build/src/router/route.d.ts.map +0 -1
  145. package/build/src/router/store.d.ts.map +0 -1
  146. package/build/src/server/factories/final_handler.d.ts.map +0 -1
  147. package/build/src/server/factories/middleware_handler.d.ts.map +0 -1
  148. package/build/src/server/factories/write_response.d.ts.map +0 -1
  149. package/build/src/server/main.d.ts.map +0 -1
  150. package/build/src/types/base.d.ts.map +0 -1
  151. package/build/src/types/main.d.ts.map +0 -1
  152. package/build/src/types/middleware.d.ts.map +0 -1
  153. package/build/src/types/qs.d.ts.map +0 -1
  154. package/build/src/types/request.d.ts.map +0 -1
  155. package/build/src/types/response.d.ts.map +0 -1
  156. package/build/src/types/route.d.ts.map +0 -1
  157. package/build/src/types/server.d.ts.map +0 -1
  158. package/factories/http_context.ts +0 -73
  159. package/factories/main.ts +0 -15
  160. package/factories/qs_parser_factory.ts +0 -54
  161. package/factories/request.ts +0 -101
  162. package/factories/response.ts +0 -106
  163. package/factories/router.ts +0 -61
  164. package/factories/server_factory.ts +0 -94
  165. package/index.ts +0 -23
  166. package/src/cookies/client.ts +0 -98
  167. package/src/cookies/drivers/encrypted.ts +0 -42
  168. package/src/cookies/drivers/plain.ts +0 -37
  169. package/src/cookies/drivers/signed.ts +0 -42
  170. package/src/cookies/parser.ts +0 -196
  171. package/src/cookies/serializer.ts +0 -98
  172. package/src/debug.ts +0 -11
  173. package/src/define_config.ts +0 -56
  174. package/src/define_middleware.ts +0 -61
  175. package/src/exception_handler.ts +0 -290
  176. package/src/exceptions.ts +0 -55
  177. package/src/helpers.ts +0 -108
  178. package/src/http_context/local_storage.ts +0 -50
  179. package/src/http_context/main.ts +0 -126
  180. package/src/qs.ts +0 -31
  181. package/src/redirect.ts +0 -181
  182. package/src/request.ts +0 -982
  183. package/src/response.ts +0 -1421
  184. package/src/router/brisk.ts +0 -113
  185. package/src/router/executor.ts +0 -36
  186. package/src/router/factories/use_return_value.ts +0 -26
  187. package/src/router/group.ts +0 -243
  188. package/src/router/lookup_store/main.ts +0 -102
  189. package/src/router/lookup_store/route_finder.ts +0 -60
  190. package/src/router/lookup_store/url_builder.ts +0 -250
  191. package/src/router/main.ts +0 -431
  192. package/src/router/matchers.ts +0 -40
  193. package/src/router/parser.ts +0 -20
  194. package/src/router/resource.ts +0 -277
  195. package/src/router/route.ts +0 -363
  196. package/src/router/store.ts +0 -239
  197. package/src/server/factories/final_handler.ts +0 -38
  198. package/src/server/factories/middleware_handler.ts +0 -23
  199. package/src/server/factories/write_response.ts +0 -26
  200. package/src/server/main.ts +0 -356
  201. package/src/types/base.ts +0 -30
  202. package/src/types/main.ts +0 -16
  203. package/src/types/middleware.ts +0 -59
  204. package/src/types/qs.ts +0 -85
  205. package/src/types/request.ts +0 -52
  206. package/src/types/response.ts +0 -57
  207. package/src/types/route.ts +0 -217
  208. package/src/types/server.ts +0 -92
@@ -1,10 +1,51 @@
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
+ 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
+ */
5
36
  export class RoutesStore {
37
+ /**
38
+ * A flag to know if routes for explicit domains
39
+ * have been registered
40
+ */
6
41
  usingDomains = false;
42
+ /**
43
+ * Tree of registered routes and their matchit tokens
44
+ */
7
45
  tree = { tokens: [], domains: {} };
46
+ /**
47
+ * Returns the domain node for a given domain.
48
+ */
8
49
  #getDomainNode(domain) {
9
50
  if (!this.tree.domains[domain]) {
10
51
  this.tree.tokens.push(parseRoutePattern(domain));
@@ -12,6 +53,9 @@ export class RoutesStore {
12
53
  }
13
54
  return this.tree.domains[domain];
14
55
  }
56
+ /**
57
+ * Returns the method node for a given domain and method.
58
+ */
15
59
  #getMethodNode(domain, method) {
16
60
  const domainNode = this.#getDomainNode(domain);
17
61
  if (!domainNode[method]) {
@@ -19,6 +63,9 @@ export class RoutesStore {
19
63
  }
20
64
  return domainNode[method];
21
65
  }
66
+ /**
67
+ * Collects route params
68
+ */
22
69
  #collectRouteParams(route, tokens) {
23
70
  const collectedParams = new Set();
24
71
  for (let token of tokens) {
@@ -35,38 +82,95 @@ export class RoutesStore {
35
82
  collectedParams.clear();
36
83
  return params;
37
84
  }
85
+ /**
86
+ * Register route for a given domain and method
87
+ */
38
88
  #registerRoute(domain, method, tokens, route) {
39
89
  const methodRoutes = this.#getMethodNode(domain, method);
90
+ /*
91
+ * Check for duplicate route for the same domain and method
92
+ */
40
93
  if (methodRoutes.routes[route.pattern]) {
41
94
  throw new RuntimeException(`Duplicate route found. "${method}: ${route.pattern}" route already exists`);
42
95
  }
96
+ if (debug.enabled) {
97
+ debug('registering route to the store %O', route);
98
+ debug('route middleware %O', route.middleware.all().entries());
99
+ }
43
100
  methodRoutes.tokens.push(tokens);
44
101
  methodRoutes.routes[route.pattern] = route;
45
102
  methodRoutes.routeKeys[route.pattern] =
46
103
  domain !== 'root' ? `${domain}-${method}-${route.pattern}` : `${method}-${route.pattern}`;
47
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
+ */
48
119
  add(route) {
120
+ /**
121
+ * Set flag when a custom domain is used
122
+ */
49
123
  if (route.domain !== 'root') {
50
124
  this.usingDomains = true;
51
125
  }
126
+ /**
127
+ * Generate tokens for the route
128
+ */
52
129
  const tokens = parseRoutePattern(route.pattern, route.matchers);
130
+ /**
131
+ * Create route node object for persistence
132
+ */
53
133
  const routeNode = lodash.merge({ meta: {} }, lodash.pick(route, ['pattern', 'handler', 'meta', 'middleware', 'name', 'execute']));
134
+ /**
135
+ * Set route params
136
+ */
54
137
  routeNode.meta.params = this.#collectRouteParams(routeNode, tokens);
138
+ /**
139
+ * Register route for every method
140
+ */
55
141
  route.methods.forEach((method) => {
56
142
  this.#registerRoute(route.domain, method, tokens, routeNode);
57
143
  });
58
144
  return this;
59
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
+ */
60
155
  match(url, method, domain) {
61
156
  const domainName = domain?.tokens[0]?.old || 'root';
62
157
  const matchedDomain = this.tree.domains[domainName];
63
158
  if (!matchedDomain) {
64
159
  return null;
65
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
+ */
66
166
  const matchedMethod = this.tree.domains[domainName][method];
67
167
  if (!matchedMethod) {
68
168
  return null;
69
169
  }
170
+ /*
171
+ * Next, match route for the given url inside the tokens list for the
172
+ * matchedMethod
173
+ */
70
174
  const matchedRoute = matchit.match(url, matchedMethod.tokens);
71
175
  if (!matchedRoute.length) {
72
176
  return null;
@@ -79,6 +183,9 @@ export class RoutesStore {
79
183
  subdomains: domain?.hostname ? matchit.exec(domain.hostname, domain.tokens) : {},
80
184
  };
81
185
  }
186
+ /**
187
+ * Match hostname against registered domains.
188
+ */
82
189
  matchDomain(hostname) {
83
190
  if (!hostname || !this.usingDomains) {
84
191
  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
@@ -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
@@ -1 +1,9 @@
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 {};
@@ -5,4 +5,3 @@ export * from './request.js';
5
5
  export * from './response.js';
6
6
  export * from './route.js';
7
7
  export * from './server.js';
8
- //# sourceMappingURL=main.d.ts.map
@@ -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
@@ -1 +1,9 @@
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 {};