@adonisjs/http-server 8.0.0 → 8.1.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.
@@ -1,8 +1,7 @@
1
- import "../utils-BjSHKI3s.js";
2
- import { _ as Qs, c as HttpRequest, i as HttpResponse, n as Server, r as HttpContext, s as Router, t as defineConfig } from "../define_config-D-kQXU0e.js";
3
- import { t as createURL } from "../helpers-C_2HouOe.js";
4
- import { parseRoute } from "../src/helpers.js";
5
- import "../types-AUwURgIL.js";
1
+ import "../chunk-B2GA45YG.js";
2
+ import { _ as Qs, c as HttpRequest, i as HttpResponse, n as Server, r as HttpContext, s as Router, t as defineConfig } from "../define_config-t7GL92UR.js";
3
+ import { s as parseRoute } from "../helpers-aa47NQc6.js";
4
+ import { t as createURL } from "../helpers-XD4_pfkD.js";
6
5
  import { Container } from "@adonisjs/fold";
7
6
  import { Socket } from "node:net";
8
7
  import proxyAddr from "proxy-addr";
@@ -14,7 +13,15 @@ import { IncomingMessage, ServerResponse } from "node:http";
14
13
  import { Logger } from "@adonisjs/logger";
15
14
  import { Emitter } from "@adonisjs/events";
16
15
  import { LoggerFactory } from "@adonisjs/logger/factories";
16
+ //#region factories/qs_parser_factory.ts
17
+ /**
18
+ * QS Parser factory is used to generate the query string
19
+ * parser for testing
20
+ */
17
21
  var QsParserFactory = class {
22
+ /**
23
+ * Default configuration options for the QS parser
24
+ */
18
25
  #options = {
19
26
  parse: {
20
27
  depth: 5,
@@ -30,33 +37,75 @@ var QsParserFactory = class {
30
37
  skipNulls: false
31
38
  }
32
39
  };
40
+ /**
41
+ * Merge QS parser factory options
42
+ * @param options - Partial options to merge with existing configuration
43
+ */
33
44
  merge(options) {
34
45
  Object.assign(this.#options.parse, options.parse);
35
46
  Object.assign(this.#options.stringify, options.stringify);
36
47
  return this;
37
48
  }
49
+ /**
50
+ * Create instance of the QS parser class
51
+ */
38
52
  create() {
39
53
  return new Qs(this.#options);
40
54
  }
41
55
  };
56
+ //#endregion
57
+ //#region factories/router.ts
58
+ /**
59
+ * Router factory is used to generate router class instances for
60
+ * testing
61
+ */
42
62
  var RouterFactory = class {
63
+ /**
64
+ * Factory parameters for creating router instances
65
+ */
43
66
  #parameters = {};
67
+ /**
68
+ * Returns an instance of the application class
69
+ */
44
70
  #getApp() {
45
71
  return this.#parameters.app || new AppFactory().create(new URL("./app/", import.meta.url));
46
72
  }
73
+ /**
74
+ * Returns an instance of the encryptor to encrypt
75
+ * signed URLs
76
+ */
47
77
  #createEncryption() {
48
78
  return this.#parameters.encryption || new EncryptionFactory().create();
49
79
  }
80
+ /**
81
+ * Merge factory params
82
+ * @param params - Partial factory parameters to merge
83
+ */
50
84
  merge(params) {
51
85
  Object.assign(this.#parameters, params);
52
86
  return this;
53
87
  }
88
+ /**
89
+ * Create router instance
90
+ */
54
91
  create() {
55
92
  return new Router(this.#getApp(), this.#createEncryption(), new QsParserFactory().create());
56
93
  }
57
94
  };
95
+ //#endregion
96
+ //#region factories/request.ts
97
+ /**
98
+ * Request factory is used to generate request class instances for
99
+ * testing
100
+ */
58
101
  var HttpRequestFactory = class {
102
+ /**
103
+ * Factory parameters for creating request instances
104
+ */
59
105
  #parameters = {};
106
+ /**
107
+ * Returns the config for the request class
108
+ */
60
109
  #getConfig() {
61
110
  return {
62
111
  allowMethodSpoofing: false,
@@ -69,29 +118,59 @@ var HttpRequestFactory = class {
69
118
  ...this.#parameters.config
70
119
  };
71
120
  }
121
+ /**
122
+ * Returns the HTTP req object
123
+ */
72
124
  #createRequest() {
73
125
  const req = this.#parameters.req || new IncomingMessage(new Socket());
74
126
  if (this.#parameters.url) req.url = this.#parameters.url;
75
127
  if (this.#parameters.method) req.method = this.#parameters.method;
76
128
  return req;
77
129
  }
130
+ /**
131
+ * Returns the HTTP res object
132
+ * @param req - The incoming message request object
133
+ */
78
134
  #createResponse(req) {
79
135
  return this.#parameters.res || new ServerResponse(req);
80
136
  }
137
+ /**
138
+ * Returns an instance of the encryptor to encrypt
139
+ * signed URLs
140
+ */
81
141
  #createEncryption() {
82
142
  return this.#parameters.encryption || new EncryptionFactory().create();
83
143
  }
144
+ /**
145
+ * Merge factory params
146
+ * @param params - Partial factory parameters to merge
147
+ */
84
148
  merge(params) {
85
149
  Object.assign(this.#parameters, params);
86
150
  return this;
87
151
  }
152
+ /**
153
+ * Create request
154
+ */
88
155
  create() {
89
156
  const req = this.#createRequest();
90
157
  return new HttpRequest(req, this.#createResponse(req), this.#createEncryption(), this.#getConfig(), new QsParserFactory().create());
91
158
  }
92
159
  };
160
+ //#endregion
161
+ //#region factories/response.ts
162
+ /**
163
+ * Response factory is used to generate response class instances for
164
+ * testing
165
+ */
93
166
  var HttpResponseFactory = class {
167
+ /**
168
+ * Factory parameters for creating response instances
169
+ */
94
170
  #parameters = {};
171
+ /**
172
+ * Returns the config for the request class
173
+ */
95
174
  #getConfig() {
96
175
  return {
97
176
  etag: false,
@@ -107,54 +186,119 @@ var HttpResponseFactory = class {
107
186
  ...this.#parameters.config
108
187
  };
109
188
  }
189
+ /**
190
+ * Returns the HTTP req object
191
+ */
110
192
  #createRequest() {
111
193
  return this.#parameters.req || new IncomingMessage(new Socket());
112
194
  }
195
+ /**
196
+ * Returns an instance of the router
197
+ */
113
198
  #createRouter() {
114
199
  return this.#parameters.router || new RouterFactory().create();
115
200
  }
201
+ /**
202
+ * Returns the HTTP res object
203
+ * @param req - The incoming message request object
204
+ */
116
205
  #createResponse(req) {
117
206
  return this.#parameters.res || new ServerResponse(req);
118
207
  }
208
+ /**
209
+ * Returns an instance of the encryptor to encrypt
210
+ * signed URLs
211
+ */
119
212
  #createEncryption() {
120
213
  return this.#parameters.encryption || new EncryptionFactory().create();
121
214
  }
215
+ /**
216
+ * Merge factory params
217
+ * @param params - Partial factory parameters to merge
218
+ */
122
219
  merge(params) {
123
220
  Object.assign(this.#parameters, params);
124
221
  return this;
125
222
  }
223
+ /**
224
+ * Create response class instance
225
+ */
126
226
  create() {
127
227
  const req = this.#createRequest();
128
228
  return new HttpResponse(req, this.#createResponse(req), this.#createEncryption(), this.#getConfig(), this.#createRouter(), new QsParserFactory().create());
129
229
  }
130
230
  };
231
+ //#endregion
232
+ //#region factories/server_factory.ts
233
+ /**
234
+ * Server factory is used to generate server class instances for
235
+ * testing
236
+ */
131
237
  var ServerFactory = class {
238
+ /**
239
+ * Factory parameters for creating server instances
240
+ */
132
241
  #parameters = {};
242
+ /**
243
+ * Returns the emitter instance
244
+ */
133
245
  #getEmitter() {
134
246
  return this.#parameters.emitter || new Emitter(this.#getApp());
135
247
  }
248
+ /**
249
+ * Returns the logger instance
250
+ */
136
251
  #getLogger() {
137
252
  return this.#parameters.logger || new Logger({ enabled: false });
138
253
  }
254
+ /**
255
+ * Returns the config for the server class
256
+ */
139
257
  #getConfig() {
140
258
  return defineConfig(this.#parameters.config || {});
141
259
  }
260
+ /**
261
+ * Returns an instance of the application class
262
+ */
142
263
  #getApp() {
143
264
  return this.#parameters.app || new AppFactory().create(new URL("./app/", import.meta.url));
144
265
  }
266
+ /**
267
+ * Returns an instance of the encryptor to encrypt
268
+ * signed URLs
269
+ */
145
270
  #createEncryption() {
146
271
  return this.#parameters.encryption || new EncryptionFactory().create();
147
272
  }
273
+ /**
274
+ * Merge factory params
275
+ * @param params - Partial factory parameters to merge
276
+ */
148
277
  merge(params) {
149
278
  Object.assign(this.#parameters, params);
150
279
  return this;
151
280
  }
281
+ /**
282
+ * Create server instance
283
+ */
152
284
  create() {
153
285
  return new Server(this.#getApp(), this.#createEncryption(), this.#getEmitter(), this.#getLogger(), this.#getConfig());
154
286
  }
155
287
  };
288
+ //#endregion
289
+ //#region factories/http_context.ts
290
+ /**
291
+ * HttpContext factory is used to generate Http context class instances for
292
+ * testing
293
+ */
156
294
  var HttpContextFactory = class {
295
+ /**
296
+ * Factory parameters for creating HTTP context instances
297
+ */
157
298
  #parameters = {};
299
+ /**
300
+ * Returns the request class instance
301
+ */
158
302
  #createRequest() {
159
303
  if (this.#parameters.request) return this.#parameters.request;
160
304
  let { url, method, route } = this.#parameters;
@@ -164,18 +308,32 @@ var HttpContextFactory = class {
164
308
  method
165
309
  }).create();
166
310
  }
311
+ /**
312
+ * Returns the response class instance
313
+ */
167
314
  #createResponse() {
168
315
  return this.#parameters.response || new HttpResponseFactory().create();
169
316
  }
317
+ /**
318
+ * Returns an instance of the logger class
319
+ */
170
320
  #createLogger() {
171
321
  return this.#parameters.logger || new LoggerFactory().create();
172
322
  }
323
+ /**
324
+ * Merge factory params
325
+ * @param params - Partial factory parameters to merge
326
+ */
173
327
  merge(params) {
174
328
  Object.assign(this.#parameters, params);
175
329
  return this;
176
330
  }
331
+ /**
332
+ * Create HTTP context instance
333
+ */
177
334
  create() {
178
335
  return new HttpContext(this.#createRequest(), this.#createResponse(), this.#createLogger(), new Container().createResolver());
179
336
  }
180
337
  };
338
+ //#endregion
181
339
  export { HttpContextFactory, QsParserFactory, HttpRequestFactory as RequestFactory, HttpResponseFactory as ResponseFactory, RouterFactory, ServerFactory };
@@ -1,4 +1,28 @@
1
+ //#region src/client/helpers.ts
2
+ /**
3
+ * Finds a route by its identifier across domains.
4
+ *
5
+ * Searches for routes by name, pattern, or controller reference. When no domain
6
+ * is specified, searches across all domains. Supports legacy lookup strategies
7
+ * for backwards compatibility.
8
+ *
9
+ * @param domainsRoutes - Object mapping domain names to route arrays
10
+ * @param routeIdentifier - Route name, pattern, or controller reference to find
11
+ * @param domain - Optional domain to limit search scope
12
+ * @param method - Optional HTTP method to filter routes
13
+ * @param disableLegacyLookup - Whether to disable pattern and controller lookup
14
+ *
15
+ * @example
16
+ * ```ts
17
+ * const route = findRoute(routes, 'users.show', 'api', 'GET')
18
+ * const route2 = findRoute(routes, '/users/:id', undefined, 'GET')
19
+ * ```
20
+ */
1
21
  function findRoute(domainsRoutes, routeIdentifier, domain, method, disableLegacyLookup) {
22
+ /**
23
+ * Search for route in all the domains when no domain name is
24
+ * mentioned.
25
+ */
2
26
  if (!domain) {
3
27
  let route = null;
4
28
  for (const routeDomain of Object.keys(domainsRoutes)) {
@@ -9,6 +33,10 @@ function findRoute(domainsRoutes, routeIdentifier, domain, method, disableLegacy
9
33
  }
10
34
  const routes = domainsRoutes[domain];
11
35
  if (!routes) return null;
36
+ /**
37
+ * Pattern and controller are supported for legacy reasons. However
38
+ * the URL builder only works with names
39
+ */
12
40
  const lookupByPattern = !disableLegacyLookup;
13
41
  const lookupByController = !disableLegacyLookup;
14
42
  return routes.find((route) => {
@@ -18,16 +46,34 @@ function findRoute(domainsRoutes, routeIdentifier, domain, method, disableLegacy
18
46
  return false;
19
47
  }) || null;
20
48
  }
49
+ /**
50
+ * Makes URL for a given route pattern using its parsed tokens. The
51
+ * tokens could be generated using the "parseRoute" method.
52
+ *
53
+ * @param pattern - The route pattern
54
+ * @param tokens - Array of parsed route tokens
55
+ * @param searchParamsStringifier - Function to stringify query parameters
56
+ * @param params - Route parameters as array or object
57
+ * @param options - URL options
58
+ * @returns {string} The generated URL
59
+ */
21
60
  function createURL(pattern, tokens, searchParamsStringifier, params, options) {
22
61
  const uriSegments = [];
23
62
  const paramsArray = Array.isArray(params) ? params : null;
24
63
  const paramsObject = !Array.isArray(params) ? params ?? {} : {};
25
64
  let paramsIndex = 0;
26
65
  for (const token of tokens) {
66
+ /**
67
+ * Static param
68
+ */
27
69
  if (token.type === 0) {
28
70
  uriSegments.push(token.val === "/" ? "" : `${token.val}${token.end}`);
29
71
  continue;
30
72
  }
73
+ /**
74
+ * Wildcard param. It will always be the last param, hence we will provide
75
+ * it all the remaining values
76
+ */
31
77
  if (token.type === 2) {
32
78
  const values = paramsArray ? paramsArray.slice(paramsIndex) : paramsObject["*"];
33
79
  if (!Array.isArray(values) || !values.length) throw new Error(`Cannot make URL for "${pattern}". Invalid value provided for the wildcard param`);
@@ -37,16 +83,26 @@ function createURL(pattern, tokens, searchParamsStringifier, params, options) {
37
83
  const paramName = token.val;
38
84
  const value = paramsArray ? paramsArray[paramsIndex] : paramsObject[paramName];
39
85
  const isDefined = value !== void 0 && value !== null;
86
+ /**
87
+ * Required param
88
+ */
40
89
  if (token.type === 1 && !isDefined) throw new Error(`Cannot make URL for "${pattern}". Missing value for the "${paramName}" param`);
41
90
  if (isDefined) uriSegments.push(`${value}${token.end}`);
42
91
  paramsIndex++;
43
92
  }
44
93
  let URI = `/${uriSegments.join("/")}`;
94
+ /**
95
+ * Prefix base URL
96
+ */
45
97
  if (options?.prefixUrl) URI = `${options?.prefixUrl.replace(/\/$/, "")}${URI}`;
98
+ /**
99
+ * Append query string
100
+ */
46
101
  if (options?.qs) {
47
102
  const queryString = searchParamsStringifier(options?.qs);
48
103
  URI = queryString ? `${URI}?${queryString}` : URI;
49
104
  }
50
105
  return URI;
51
106
  }
107
+ //#endregion
52
108
  export { findRoute as n, createURL as t };