@adonisjs/http-server 5.11.0 → 6.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.
Files changed (169) hide show
  1. package/README.md +15 -38
  2. package/build/index.d.ts +16 -0
  3. package/build/index.js +16 -0
  4. package/build/src/cookies/client.d.ts +12 -0
  5. package/build/src/cookies/client.js +42 -0
  6. package/build/src/cookies/drivers/encrypted.d.ts +4 -0
  7. package/build/src/cookies/drivers/encrypted.js +16 -0
  8. package/build/src/cookies/drivers/plain.d.ts +3 -0
  9. package/build/src/cookies/drivers/plain.js +13 -0
  10. package/build/src/cookies/drivers/signed.d.ts +4 -0
  11. package/build/src/cookies/drivers/signed.js +16 -0
  12. package/build/src/cookies/parser.d.ts +9 -0
  13. package/build/src/cookies/parser.js +69 -0
  14. package/build/src/cookies/serializer.d.ts +11 -0
  15. package/build/src/cookies/serializer.js +39 -0
  16. package/build/src/debug.d.ts +3 -0
  17. package/build/src/debug.js +2 -0
  18. package/build/src/define_config.d.ts +2 -0
  19. package/build/src/define_config.js +40 -0
  20. package/build/src/define_middleware.d.ts +4 -0
  21. package/build/src/define_middleware.js +6 -0
  22. package/build/src/exceptions/abort_exception.d.ts +5 -0
  23. package/build/src/exceptions/abort_exception.js +6 -0
  24. package/build/src/exceptions/cannot_lookup_route.d.ts +5 -0
  25. package/build/src/exceptions/cannot_lookup_route.js +5 -0
  26. package/build/src/exceptions/http_exception.d.ts +6 -0
  27. package/build/src/exceptions/http_exception.js +20 -0
  28. package/build/src/exceptions/route_not_found.d.ts +5 -0
  29. package/build/src/exceptions/route_not_found.js +5 -0
  30. package/build/src/helpers.d.ts +6 -67
  31. package/build/src/helpers.js +13 -168
  32. package/build/src/http_context/local_storage.d.ts +9 -0
  33. package/build/src/http_context/local_storage.js +14 -0
  34. package/build/src/http_context/main.d.ts +20 -0
  35. package/build/src/http_context/main.js +49 -0
  36. package/build/src/middleware/store.d.ts +11 -0
  37. package/build/src/middleware/store.js +33 -0
  38. package/build/src/qs.d.ts +7 -0
  39. package/build/src/qs.js +13 -0
  40. package/build/src/redirect.d.ts +18 -0
  41. package/build/src/redirect.js +80 -0
  42. package/build/src/request.d.ts +81 -0
  43. package/build/src/request.js +315 -0
  44. package/build/src/response.d.ts +108 -0
  45. package/build/src/response.js +587 -0
  46. package/build/src/router/brisk.d.ts +18 -0
  47. package/build/src/router/brisk.js +35 -0
  48. package/build/src/router/executor.d.ts +4 -0
  49. package/build/src/router/executor.js +16 -0
  50. package/build/src/router/group.d.ts +19 -0
  51. package/build/src/router/group.js +117 -0
  52. package/build/src/router/lookup_store/main.d.ts +17 -0
  53. package/build/src/router/lookup_store/main.js +37 -0
  54. package/build/src/router/lookup_store/route_finder.d.ts +8 -0
  55. package/build/src/router/lookup_store/route_finder.js +28 -0
  56. package/build/src/router/lookup_store/url_builder.d.ts +16 -0
  57. package/build/src/router/lookup_store/url_builder.js +111 -0
  58. package/build/src/router/main.d.ts +36 -0
  59. package/build/src/router/main.js +161 -0
  60. package/build/src/router/matchers.d.ts +14 -0
  61. package/build/src/router/matchers.js +15 -0
  62. package/build/src/router/resource.d.ts +27 -0
  63. package/build/src/router/resource.js +124 -0
  64. package/build/src/router/route.d.ts +28 -0
  65. package/build/src/router/route.js +135 -0
  66. package/build/src/router/store.d.ts +12 -0
  67. package/build/src/router/store.js +87 -0
  68. package/build/src/server/factories/final_handler.d.ts +4 -0
  69. package/build/src/server/factories/final_handler.js +17 -0
  70. package/build/src/server/factories/middleware_handler.d.ts +5 -0
  71. package/build/src/server/factories/middleware_handler.js +5 -0
  72. package/build/src/server/factories/use_return_value.d.ts +2 -0
  73. package/build/src/server/factories/use_return_value.js +9 -0
  74. package/build/src/server/factories/write_response.d.ts +2 -0
  75. package/build/src/server/factories/write_response.js +12 -0
  76. package/build/src/server/main.d.ts +23 -0
  77. package/build/src/server/main.js +124 -0
  78. package/build/src/types/base.d.ts +5 -0
  79. package/build/src/types/base.js +1 -0
  80. package/build/src/types/middleware.d.ts +14 -0
  81. package/build/src/types/middleware.js +1 -0
  82. package/build/src/types/qs.d.ts +15 -0
  83. package/build/src/types/qs.js +1 -0
  84. package/build/src/types/request.d.ts +7 -0
  85. package/build/src/types/request.js +1 -0
  86. package/build/src/types/response.d.ts +18 -0
  87. package/build/src/types/response.js +1 -0
  88. package/build/src/types/route.d.ts +74 -0
  89. package/build/src/types/route.js +1 -0
  90. package/build/src/types/server.d.ts +13 -0
  91. package/build/src/types/server.js +1 -0
  92. package/package.json +98 -96
  93. package/build/adonis-typings/container.d.ts +0 -24
  94. package/build/adonis-typings/container.js +0 -8
  95. package/build/adonis-typings/context.d.ts +0 -73
  96. package/build/adonis-typings/context.js +0 -8
  97. package/build/adonis-typings/cookie-client.d.ts +0 -34
  98. package/build/adonis-typings/cookie-client.js +0 -8
  99. package/build/adonis-typings/http-server.d.ts +0 -98
  100. package/build/adonis-typings/http-server.js +0 -8
  101. package/build/adonis-typings/index.d.ts +0 -16
  102. package/build/adonis-typings/index.js +0 -16
  103. package/build/adonis-typings/middleware.d.ts +0 -95
  104. package/build/adonis-typings/middleware.js +0 -8
  105. package/build/adonis-typings/request.d.ts +0 -556
  106. package/build/adonis-typings/request.js +0 -8
  107. package/build/adonis-typings/response.d.ts +0 -438
  108. package/build/adonis-typings/response.js +0 -8
  109. package/build/adonis-typings/route.d.ts +0 -559
  110. package/build/adonis-typings/route.js +0 -8
  111. package/build/exceptions.json +0 -110
  112. package/build/providers/HttpServerProvider.d.ts +0 -46
  113. package/build/providers/HttpServerProvider.js +0 -100
  114. package/build/src/Cookie/Client/index.d.ts +0 -39
  115. package/build/src/Cookie/Client/index.js +0 -108
  116. package/build/src/Cookie/Drivers/Encrypted.d.ts +0 -24
  117. package/build/src/Cookie/Drivers/Encrypted.js +0 -42
  118. package/build/src/Cookie/Drivers/Plain.d.ts +0 -23
  119. package/build/src/Cookie/Drivers/Plain.js +0 -40
  120. package/build/src/Cookie/Drivers/Signed.d.ts +0 -24
  121. package/build/src/Cookie/Drivers/Signed.js +0 -42
  122. package/build/src/Cookie/Parser/index.d.ts +0 -61
  123. package/build/src/Cookie/Parser/index.js +0 -174
  124. package/build/src/Cookie/Serializer/index.d.ts +0 -44
  125. package/build/src/Cookie/Serializer/index.js +0 -88
  126. package/build/src/Exceptions/HttpException.d.ts +0 -20
  127. package/build/src/Exceptions/HttpException.js +0 -36
  128. package/build/src/Exceptions/RouterException.d.ts +0 -36
  129. package/build/src/Exceptions/RouterException.js +0 -76
  130. package/build/src/HttpContext/LocalStorage/index.d.ts +0 -24
  131. package/build/src/HttpContext/LocalStorage/index.js +0 -28
  132. package/build/src/HttpContext/index.d.ts +0 -90
  133. package/build/src/HttpContext/index.js +0 -181
  134. package/build/src/MiddlewareStore/index.d.ts +0 -92
  135. package/build/src/MiddlewareStore/index.js +0 -133
  136. package/build/src/Redirect/index.d.ts +0 -71
  137. package/build/src/Redirect/index.js +0 -139
  138. package/build/src/Request/index.d.ts +0 -619
  139. package/build/src/Request/index.js +0 -862
  140. package/build/src/Response/index.d.ts +0 -412
  141. package/build/src/Response/index.js +0 -1010
  142. package/build/src/Router/BriskRoute.d.ts +0 -53
  143. package/build/src/Router/BriskRoute.js +0 -74
  144. package/build/src/Router/Group.d.ts +0 -101
  145. package/build/src/Router/Group.js +0 -165
  146. package/build/src/Router/LookupStore.d.ts +0 -122
  147. package/build/src/Router/LookupStore.js +0 -264
  148. package/build/src/Router/Matchers.d.ts +0 -31
  149. package/build/src/Router/Matchers.js +0 -43
  150. package/build/src/Router/Resource.d.ts +0 -95
  151. package/build/src/Router/Resource.js +0 -182
  152. package/build/src/Router/Route.d.ts +0 -138
  153. package/build/src/Router/Route.js +0 -204
  154. package/build/src/Router/Store.d.ts +0 -93
  155. package/build/src/Router/Store.js +0 -211
  156. package/build/src/Router/index.d.ts +0 -142
  157. package/build/src/Router/index.js +0 -333
  158. package/build/src/Server/ExceptionManager/index.d.ts +0 -49
  159. package/build/src/Server/ExceptionManager/index.js +0 -96
  160. package/build/src/Server/Hooks/index.d.ts +0 -43
  161. package/build/src/Server/Hooks/index.js +0 -77
  162. package/build/src/Server/PreCompiler/index.d.ts +0 -60
  163. package/build/src/Server/PreCompiler/index.js +0 -141
  164. package/build/src/Server/RequestHandler/index.d.ts +0 -39
  165. package/build/src/Server/RequestHandler/index.js +0 -87
  166. package/build/src/Server/index.d.ts +0 -90
  167. package/build/src/Server/index.js +0 -175
  168. package/build/standalone.d.ts +0 -14
  169. package/build/standalone.js +0 -23
@@ -1,862 +0,0 @@
1
- "use strict";
2
- /**
3
- * @adonisjs/http-server
4
- *
5
- * (c) Harminder Virk <virk@adonisjs.com>
6
- *
7
- * For the full copyright and license information, please view the LICENSE
8
- * file that was distributed with this source code.
9
- */
10
- var __importDefault = (this && this.__importDefault) || function (mod) {
11
- return (mod && mod.__esModule) ? mod : { "default": mod };
12
- };
13
- Object.defineProperty(exports, "__esModule", { value: true });
14
- exports.Request = void 0;
15
- /// <reference path="../../adonis-typings/index.ts" />
16
- const qs_1 = __importDefault(require("qs"));
17
- const fresh_1 = __importDefault(require("fresh"));
18
- const net_1 = require("net");
19
- const type_is_1 = __importDefault(require("type-is"));
20
- const accepts_1 = __importDefault(require("accepts"));
21
- const proxy_addr_1 = __importDefault(require("proxy-addr"));
22
- const macroable_1 = require("macroable");
23
- const utils_1 = require("@poppinss/utils");
24
- const url_1 = require("url");
25
- const helpers_1 = require("@poppinss/utils/build/helpers");
26
- const helpers_2 = require("../helpers");
27
- const Parser_1 = require("../Cookie/Parser");
28
- /**
29
- * HTTP Request class exposes the interface to consistently read values
30
- * related to a given HTTP request. The class is wrapper over
31
- * [IncomingMessage](https://nodejs.org/api/http.html#http_class_http_incomingmessage)
32
- * and has extended API.
33
- *
34
- * You can access the original [IncomingMessage](https://nodejs.org/api/http.html#http_class_http_incomingmessage)
35
- * using `request.request` property.
36
- */
37
- class Request extends macroable_1.Macroable {
38
- constructor(request, response, encryption, config) {
39
- super();
40
- this.request = request;
41
- this.response = response;
42
- this.encryption = encryption;
43
- this.config = config;
44
- /**
45
- * Request body set using `setBody` method
46
- */
47
- this.requestBody = {};
48
- /**
49
- * Route params
50
- */
51
- this.routeParams = {};
52
- /**
53
- * A merged copy of `request body` and `querystring`
54
- */
55
- this.requestData = {};
56
- /**
57
- * Original merged copy of `request body` and `querystring`.
58
- * Further mutation to this object are not allowed
59
- */
60
- this.originalRequestData = {};
61
- /**
62
- * Parsed query string
63
- */
64
- this.requestQs = {};
65
- /**
66
- * Raw request body as text
67
- */
68
- this.rawRequestBody = null;
69
- /**
70
- * Cached copy of `accepts` fn to do content
71
- * negotiation.
72
- */
73
- this.lazyAccepts = null;
74
- /**
75
- * Parses copy of the URL with query string as a string and not
76
- * object. This is done to build URL's with query string without
77
- * stringifying the object
78
- */
79
- this.parsedUrl = (0, url_1.parse)(this.request.url, false);
80
- this.parseQueryString();
81
- }
82
- /**
83
- * Parses the query string
84
- */
85
- parseQueryString() {
86
- if (this.parsedUrl.query) {
87
- this.updateQs(qs_1.default.parse(this.parsedUrl.query));
88
- this.originalRequestData = { ...this.requestData };
89
- }
90
- }
91
- /**
92
- * Initiates the cookie parser lazily
93
- */
94
- initiateCookieParser() {
95
- if (!this.cookieParser) {
96
- this.cookieParser = new Parser_1.CookieParser(this.header('cookie'), this.encryption);
97
- }
98
- }
99
- /**
100
- * Lazily initiates the `accepts` module to make sure to parse
101
- * the request headers only when one of the content-negotiation
102
- * methods are used.
103
- */
104
- initiateAccepts() {
105
- this.lazyAccepts = this.lazyAccepts || (0, accepts_1.default)(this.request);
106
- }
107
- /**
108
- * Returns the request id from the `x-request-id` header. The
109
- * header is untoched, if it already exists.
110
- */
111
- id() {
112
- let requestId = this.header('x-request-id');
113
- if (!requestId && this.config.generateRequestId) {
114
- requestId = (0, helpers_1.cuid)();
115
- this.request.headers['x-request-id'] = requestId;
116
- }
117
- return requestId;
118
- }
119
- /**
120
- * Set initial request body. A copy of the input will be maintained as the original
121
- * request body. Since the request body and query string is subject to mutations, we
122
- * keep one original reference to flash old data (whenever required).
123
- *
124
- * This method is supposed to be invoked by the body parser and must be called only
125
- * once. For further mutations make use of `updateBody` method.
126
- */
127
- setInitialBody(body) {
128
- if (this.originalRequestData && Object.isFrozen(this.originalRequestData)) {
129
- throw new Error('Cannot re-set initial body. Use "request.updateBody" instead');
130
- }
131
- this.updateBody(body);
132
- /*
133
- * Freeze the original object
134
- */
135
- this.originalRequestData = Object.freeze({ ...this.requestData });
136
- }
137
- /**
138
- * Update the request body with new data object. The `all` property
139
- * will be re-computed by merging the query string and request
140
- * body.
141
- */
142
- updateBody(body) {
143
- this.requestBody = body;
144
- this.requestData = { ...this.requestBody, ...this.requestQs };
145
- }
146
- /**
147
- * Update the request raw body. Bodyparser sets this when unable to parse
148
- * the request body or when request is multipart/form-data.
149
- */
150
- updateRawBody(rawBody) {
151
- this.rawRequestBody = rawBody;
152
- }
153
- /**
154
- * Update the query string with the new data object. The `all` property
155
- * will be re-computed by merging the query and the request body.
156
- */
157
- updateQs(data) {
158
- this.requestQs = data;
159
- this.requestData = { ...this.requestBody, ...this.requestQs };
160
- }
161
- /**
162
- * Update route params
163
- */
164
- updateParams(data) {
165
- this.routeParams = data;
166
- }
167
- /**
168
- * Returns route params
169
- */
170
- params() {
171
- return this.routeParams;
172
- }
173
- /**
174
- * Returns reference to the query string object
175
- */
176
- get() {
177
- process.emitWarning('DeprecationWarning', 'request.get() is deprecated. Use request.qs() instead');
178
- return this.qs();
179
- }
180
- /**
181
- * Returns the query string object by reference
182
- */
183
- qs() {
184
- return this.requestQs;
185
- }
186
- /**
187
- * Returns reference to the request body
188
- */
189
- post() {
190
- process.emitWarning('DeprecationWarning', 'request.post() is deprecated. Use request.body() instead');
191
- return this.body();
192
- }
193
- /**
194
- * Returns reference to the request body
195
- */
196
- body() {
197
- return this.requestBody;
198
- }
199
- /**
200
- * Returns reference to the merged copy of request body
201
- * and query string
202
- */
203
- all() {
204
- return this.requestData;
205
- }
206
- /**
207
- * Returns reference to the merged copy of original request
208
- * query string and body
209
- */
210
- original() {
211
- return this.originalRequestData;
212
- }
213
- /**
214
- * Returns the request raw body (if exists), or returns `null`.
215
- *
216
- * Ideally you must be dealing with the parsed body accessed using [[input]], [[all]] or
217
- * [[post]] methods. The `raw` body is always a string.
218
- */
219
- raw() {
220
- return this.rawRequestBody;
221
- }
222
- /**
223
- * Returns value for a given key from the request body or query string.
224
- * The `defaultValue` is used when original value is `undefined`.
225
- *
226
- * @example
227
- * ```js
228
- * request.input('username')
229
- *
230
- * // with default value
231
- * request.input('username', 'virk')
232
- * ```
233
- */
234
- input(key, defaultValue) {
235
- return utils_1.lodash.get(this.requestData, key, defaultValue);
236
- }
237
- /**
238
- * Returns value for a given key from route params
239
- *
240
- * @example
241
- * ```js
242
- * request.param('id')
243
- *
244
- * // with default value
245
- * request.param('id', 1)
246
- * ```
247
- */
248
- param(key, defaultValue) {
249
- return utils_1.lodash.get(this.routeParams, key, defaultValue);
250
- }
251
- /**
252
- * Get everything from the request body except the given keys.
253
- *
254
- * @example
255
- * ```js
256
- * request.except(['_csrf'])
257
- * ```
258
- */
259
- except(keys) {
260
- return utils_1.lodash.omit(this.requestData, keys);
261
- }
262
- /**
263
- * Get value for specified keys.
264
- *
265
- * @example
266
- * ```js
267
- * request.only(['username', 'age'])
268
- * ```
269
- */
270
- only(keys) {
271
- return utils_1.lodash.pick(this.requestData, keys);
272
- }
273
- /**
274
- * Returns the HTTP request method. This is the original
275
- * request method. For spoofed request method, make
276
- * use of [[method]].
277
- *
278
- * @example
279
- * ```js
280
- * request.intended()
281
- * ```
282
- */
283
- intended() {
284
- return this.request.method;
285
- }
286
- /**
287
- * Returns the request HTTP method by taking method spoofing into account.
288
- *
289
- * Method spoofing works when all of the following are true.
290
- *
291
- * 1. `app.http.allowMethodSpoofing` config value is true.
292
- * 2. request query string has `_method`.
293
- * 3. The [[intended]] request method is `POST`.
294
- *
295
- * @example
296
- * ```js
297
- * request.method()
298
- * ```
299
- */
300
- method() {
301
- if (this.config.allowMethodSpoofing && this.intended() === 'POST') {
302
- return this.input('_method', this.intended()).toUpperCase();
303
- }
304
- return this.intended();
305
- }
306
- /**
307
- * Returns a copy of headers as an object
308
- */
309
- headers() {
310
- return this.request.headers;
311
- }
312
- /**
313
- * Returns value for a given header key. The default value is
314
- * used when original value is `undefined`.
315
- */
316
- header(key, defaultValue) {
317
- key = key.toLowerCase();
318
- const headers = this.headers();
319
- switch (key) {
320
- case 'referer':
321
- case 'referrer':
322
- return headers.referrer || headers.referer || defaultValue;
323
- default:
324
- return headers[key] || defaultValue;
325
- }
326
- }
327
- /**
328
- * Returns the ip address of the user. This method is optimize to fetch
329
- * ip address even when running your AdonisJs app behind a proxy.
330
- *
331
- * You can also define your own custom function to compute the ip address by
332
- * defining `app.http.getIp` as a function inside the config file.
333
- *
334
- * ```js
335
- * {
336
- * http: {
337
- * getIp (request) {
338
- * // I am using nginx as a proxy server and want to trust 'x-real-ip'
339
- * return request.header('x-real-ip')
340
- * }
341
- * }
342
- * }
343
- * ```
344
- *
345
- * You can control the behavior of trusting the proxy values by defining it
346
- * inside the `config/app.js` file.
347
- *
348
- * ```js
349
- * {
350
- * http: {
351
- * trustProxy: '127.0.0.1'
352
- * }
353
- * }
354
- * ```
355
- *
356
- * The value of trustProxy is passed directly to [proxy-addr](https://www.npmjs.com/package/proxy-addr)
357
- */
358
- ip() {
359
- const ipFn = this.config.getIp;
360
- if (typeof ipFn === 'function') {
361
- return ipFn(this);
362
- }
363
- return (0, proxy_addr_1.default)(this.request, this.config.trustProxy);
364
- }
365
- /**
366
- * Returns an array of ip addresses from most to least trusted one.
367
- * This method is optimize to fetch ip address even when running
368
- * your AdonisJs app behind a proxy.
369
- *
370
- * You can control the behavior of trusting the proxy values by defining it
371
- * inside the `config/app.js` file.
372
- *
373
- * ```js
374
- * {
375
- * http: {
376
- * trustProxy: '127.0.0.1'
377
- * }
378
- * }
379
- * ```
380
- *
381
- * The value of trustProxy is passed directly to [proxy-addr](https://www.npmjs.com/package/proxy-addr)
382
- */
383
- ips() {
384
- return proxy_addr_1.default.all(this.request, this.config.trustProxy);
385
- }
386
- /**
387
- * Returns the request protocol by checking for the URL protocol or
388
- * `X-Forwarded-Proto` header.
389
- *
390
- * If the `trust` is evaluated to `false`, then URL protocol is returned,
391
- * otherwise `X-Forwarded-Proto` header is used (if exists).
392
- *
393
- * You can control the behavior of trusting the proxy values by defining it
394
- * inside the `config/app.js` file.
395
- *
396
- * ```js
397
- * {
398
- * http: {
399
- * trustProxy: '127.0.0.1'
400
- * }
401
- * }
402
- * ```
403
- *
404
- * The value of trustProxy is passed directly to [proxy-addr](https://www.npmjs.com/package/proxy-addr)
405
- */
406
- protocol() {
407
- if (this.request.connection['encrypted']) {
408
- return 'https';
409
- }
410
- if (!(0, helpers_2.trustProxy)(this.request.connection.remoteAddress, this.config.trustProxy)) {
411
- return this.parsedUrl.protocol || 'http';
412
- }
413
- const forwardedProtocol = this.header('X-Forwarded-Proto');
414
- return forwardedProtocol ? forwardedProtocol.split(/\s*,\s*/)[0] : 'http';
415
- }
416
- /**
417
- * Returns a boolean telling if request is served over `https`
418
- * or not. Check [[protocol]] method to know how protocol is
419
- * fetched.
420
- */
421
- secure() {
422
- return this.protocol() === 'https';
423
- }
424
- /**
425
- * Returns the request host. If proxy headers are trusted, then
426
- * `X-Forwarded-Host` is given priority over the `Host` header.
427
- *
428
- * You can control the behavior of trusting the proxy values by defining it
429
- * inside the `config/app.js` file.
430
- *
431
- * ```js
432
- * {
433
- * http: {
434
- * trustProxy: '127.0.0.1'
435
- * }
436
- * }
437
- * ```
438
- *
439
- * The value of trustProxy is passed directly to [proxy-addr](https://www.npmjs.com/package/proxy-addr)
440
- */
441
- host() {
442
- let host = this.header('host');
443
- /*
444
- * Use X-Fowarded-Host when we trust the proxy header and it
445
- * exists
446
- */
447
- if (!(0, helpers_2.trustProxy)(this.request.connection.remoteAddress, this.config.trustProxy)) {
448
- host = this.header('X-Forwarded-Host') || host;
449
- }
450
- if (!host) {
451
- return null;
452
- }
453
- return host;
454
- }
455
- /**
456
- * Returns the request hostname. If proxy headers are trusted, then
457
- * `X-Forwarded-Host` is given priority over the `Host` header.
458
- *
459
- * You can control the behavior of trusting the proxy values by defining it
460
- * inside the `config/app.js` file.
461
- *
462
- * ```js
463
- * {
464
- * http: {
465
- * trustProxy: '127.0.0.1'
466
- * }
467
- * }
468
- * ```
469
- *
470
- * The value of trustProxy is passed directly to [proxy-addr](https://www.npmjs.com/package/proxy-addr)
471
- */
472
- hostname() {
473
- const host = this.host();
474
- if (!host) {
475
- return null;
476
- }
477
- /*
478
- * Support for IPv6
479
- * https://datatracker.ietf.org/doc/html/rfc3986#section-3.2.2
480
- * https://github.com/nodejs/node/pull/5314
481
- */
482
- const offset = host[0] === '[' ? host.indexOf(']') + 1 : 0;
483
- const index = host.indexOf(':', offset);
484
- return index !== -1 ? host.substring(0, index) : host;
485
- }
486
- /**
487
- * Returns an array of subdomains for the given host. An empty array is
488
- * returned if [[hostname]] is `null` or is an IP address.
489
- *
490
- * Also `www` is not considered as a subdomain
491
- */
492
- subdomains() {
493
- const hostname = this.hostname();
494
- /*
495
- * Return empty array when hostname is missing or it's
496
- * an IP address
497
- */
498
- if (!hostname || (0, net_1.isIP)(hostname)) {
499
- return [];
500
- }
501
- const offset = this.config.subdomainOffset;
502
- const subdomains = hostname.split('.').reverse().slice(offset);
503
- /*
504
- * Remove www from the subdomains list
505
- */
506
- if (subdomains[subdomains.length - 1] === 'www') {
507
- subdomains.splice(subdomains.length - 1, 1);
508
- }
509
- return subdomains;
510
- }
511
- /**
512
- * Returns a boolean telling, if request `X-Requested-With === 'xmlhttprequest'`
513
- * or not.
514
- */
515
- ajax() {
516
- const xRequestedWith = this.header('X-Requested-With', '');
517
- return xRequestedWith.toLowerCase() === 'xmlhttprequest';
518
- }
519
- /**
520
- * Returns a boolean telling, if request has `X-Pjax` header
521
- * set or not
522
- */
523
- pjax() {
524
- return !!this.header('X-Pjax');
525
- }
526
- /**
527
- * Returns the request relative URL.
528
- *
529
- * @example
530
- * ```js
531
- * request.url()
532
- *
533
- * // include query string
534
- * request.url(true)
535
- * ```
536
- */
537
- url(includeQueryString) {
538
- const pathname = this.parsedUrl.pathname;
539
- return includeQueryString && this.parsedUrl.query
540
- ? `${pathname}?${this.parsedUrl.query}`
541
- : pathname;
542
- }
543
- /**
544
- * Returns the complete HTTP url by combining
545
- * [[protocol]]://[[hostname]]/[[url]]
546
- *
547
- * @example
548
- * ```js
549
- * request.completeUrl()
550
- *
551
- * // include query string
552
- * request.completeUrl(true)
553
- * ```
554
- */
555
- completeUrl(includeQueryString) {
556
- const protocol = this.protocol();
557
- const hostname = this.host();
558
- return `${protocol}://${hostname}${this.url(includeQueryString)}`;
559
- }
560
- /**
561
- * Find if the current HTTP request is for the given route or the routes
562
- */
563
- matchesRoute(routeIdentifier) {
564
- /**
565
- * The context is missing inside the HTTP server hooks.
566
- */
567
- if (!this.ctx || !this.ctx.route) {
568
- return false;
569
- }
570
- const route = this.ctx.route;
571
- /**
572
- * Search the identifier(s) against the route "pattern", "name" and the route handler
573
- */
574
- return !!(Array.isArray(routeIdentifier) ? routeIdentifier : [routeIdentifier]).find((identifier) => {
575
- return (route.pattern === identifier || route.name === identifier || route.handler === identifier);
576
- });
577
- }
578
- /**
579
- * Returns the best matching content type of the request by
580
- * matching against the given types.
581
- *
582
- * The content type is picked from the `content-type` header and request
583
- * must have body.
584
- *
585
- * The method response highly depends upon the types array values. Described below:
586
- *
587
- * | Type(s) | Return value |
588
- * |----------|---------------|
589
- * | ['json'] | json |
590
- * | ['application/*'] | application/json |
591
- * | ['vnd+json'] | application/json |
592
- *
593
- * @example
594
- * ```js
595
- * const bodyType = request.is(['json', 'xml'])
596
- *
597
- * if (bodyType === 'json') {
598
- * // process JSON
599
- * }
600
- *
601
- * if (bodyType === 'xml') {
602
- * // process XML
603
- * }
604
- * ```
605
- */
606
- is(types) {
607
- return (0, type_is_1.default)(this.request, types) || null;
608
- }
609
- /**
610
- * Returns the best type using `Accept` header and
611
- * by matching it against the given types.
612
- *
613
- * If nothing is matched, then `null` will be returned
614
- *
615
- * Make sure to check [accepts](https://www.npmjs.com/package/accepts) package
616
- * docs too.
617
- *
618
- * @example
619
- * ```js
620
- * switch (request.accepts(['json', 'html'])) {
621
- * case 'json':
622
- * return response.json(user)
623
- * case 'html':
624
- * return view.render('user', { user })
625
- * default:
626
- * // decide yourself
627
- * }
628
- * ```
629
- */
630
- accepts(types) {
631
- this.initiateAccepts();
632
- return this.lazyAccepts.type(types) || null;
633
- }
634
- /**
635
- * Return the types that the request accepts, in the order of the
636
- * client's preference (most preferred first).
637
- *
638
- * Make sure to check [accepts](https://www.npmjs.com/package/accepts) package
639
- * docs too.
640
- */
641
- types() {
642
- this.initiateAccepts();
643
- return this.lazyAccepts.types();
644
- }
645
- /**
646
- * Returns the best language using `Accept-language` header
647
- * and by matching it against the given languages.
648
- *
649
- * If nothing is matched, then `null` will be returned
650
- *
651
- * Make sure to check [accepts](https://www.npmjs.com/package/accepts) package
652
- * docs too.
653
- *
654
- * @example
655
- * ```js
656
- * switch (request.language(['fr', 'de'])) {
657
- * case 'fr':
658
- * return view.render('about', { lang: 'fr' })
659
- * case 'de':
660
- * return view.render('about', { lang: 'de' })
661
- * default:
662
- * return view.render('about', { lang: 'en' })
663
- * }
664
- * ```
665
- */
666
- language(languages) {
667
- this.initiateAccepts();
668
- return this.lazyAccepts.language(languages) || null;
669
- }
670
- /**
671
- * Return the languages that the request accepts, in the order of the
672
- * client's preference (most preferred first).
673
- *
674
- * Make sure to check [accepts](https://www.npmjs.com/package/accepts) package
675
- * docs too.
676
- */
677
- languages() {
678
- this.initiateAccepts();
679
- return this.lazyAccepts.languages();
680
- }
681
- /**
682
- * Returns the best charset using `Accept-charset` header
683
- * and by matching it against the given charsets.
684
- *
685
- * If nothing is matched, then `null` will be returned
686
- *
687
- * Make sure to check [accepts](https://www.npmjs.com/package/accepts) package
688
- * docs too.
689
- *
690
- * @example
691
- * ```js
692
- * switch (request.charset(['utf-8', 'ISO-8859-1'])) {
693
- * case 'utf-8':
694
- * // make utf-8 friendly response
695
- * case 'ISO-8859-1':
696
- * // make ISO-8859-1 friendly response
697
- * }
698
- * ```
699
- */
700
- charset(charsets) {
701
- this.initiateAccepts();
702
- return this.lazyAccepts.charset(charsets) || null;
703
- }
704
- /**
705
- * Return the charsets that the request accepts, in the order of the
706
- * client's preference (most preferred first).
707
- *
708
- * Make sure to check [accepts](https://www.npmjs.com/package/accepts) package
709
- * docs too.
710
- */
711
- charsets() {
712
- this.initiateAccepts();
713
- return this.lazyAccepts.charsets();
714
- }
715
- /**
716
- * Returns the best encoding using `Accept-encoding` header
717
- * and by matching it against the given encodings.
718
- *
719
- * If nothing is matched, then `null` will be returned
720
- *
721
- * Make sure to check [accepts](https://www.npmjs.com/package/accepts) package
722
- * docs too.
723
- */
724
- encoding(encodings) {
725
- this.initiateAccepts();
726
- return this.lazyAccepts.encoding(encodings) || null;
727
- }
728
- /**
729
- * Return the charsets that the request accepts, in the order of the
730
- * client's preference (most preferred first).
731
- *
732
- * Make sure to check [accepts](https://www.npmjs.com/package/accepts) package
733
- * docs too.
734
- */
735
- encodings() {
736
- this.initiateAccepts();
737
- return this.lazyAccepts.encodings();
738
- }
739
- /**
740
- * Returns a boolean telling if request has body
741
- */
742
- hasBody() {
743
- return type_is_1.default.hasBody(this.request);
744
- }
745
- /**
746
- * Returns a boolean telling if the new response etag evaluates same
747
- * as the request header `if-none-match`. In case of `true`, the
748
- * server must return `304` response, telling the browser to
749
- * use the client cache.
750
- *
751
- * You won't have to deal with this method directly, since AdonisJs will
752
- * handle this for you when `http.etag = true` inside `config/app.js` file.
753
- *
754
- * However, this is how you can use it manually.
755
- *
756
- * @example
757
- * ```js
758
- * const responseBody = view.render('some-view')
759
- *
760
- * // sets the HTTP etag header for response
761
- * response.setEtag(responseBody)
762
- *
763
- * if (request.fresh()) {
764
- * response.sendStatus(304)
765
- * } else {
766
- * response.send(responseBody)
767
- * }
768
- * ```
769
- */
770
- fresh() {
771
- if (['GET', 'HEAD'].indexOf(this.intended()) === -1) {
772
- return false;
773
- }
774
- const status = this.response.statusCode;
775
- if ((status >= 200 && status < 300) || status === 304) {
776
- return (0, fresh_1.default)(this.headers(), this.response.getHeaders());
777
- }
778
- return false;
779
- }
780
- /**
781
- * Opposite of [[fresh]]
782
- */
783
- stale() {
784
- return !this.fresh();
785
- }
786
- /**
787
- * Returns all parsed and signed cookies. Signed cookies ensures
788
- * that their value isn't tampered.
789
- */
790
- cookiesList() {
791
- this.initiateCookieParser();
792
- return this.cookieParser.list();
793
- }
794
- /**
795
- * Returns value for a given key from signed cookies. Optional
796
- * defaultValue is returned when actual value is undefined.
797
- */
798
- cookie(key, defaultValue) {
799
- this.initiateCookieParser();
800
- return this.cookieParser.unsign(key) || defaultValue;
801
- }
802
- /**
803
- * Returns value for a given key from signed cookies. Optional
804
- * defaultValue is returned when actual value is undefined.
805
- */
806
- encryptedCookie(key, defaultValue) {
807
- this.initiateCookieParser();
808
- return this.cookieParser.decrypt(key) || defaultValue;
809
- }
810
- /**
811
- * Returns value for a given key from unsigned cookies. Optional
812
- * defaultValue is returned when actual value is undefined.
813
- */
814
- plainCookie(key, defaultValue) {
815
- this.initiateCookieParser();
816
- return this.cookieParser.decode(key) || defaultValue;
817
- }
818
- /**
819
- * Returns a boolean telling if a signed url as a valid signature
820
- * or not.
821
- */
822
- hasValidSignature(purpose) {
823
- const { signature, ...rest } = this.qs();
824
- if (!signature) {
825
- return false;
826
- }
827
- /*
828
- * Return false when signature fails
829
- */
830
- const signedUrl = this.encryption.verifier.unsign(signature, purpose);
831
- if (!signedUrl) {
832
- return false;
833
- }
834
- const queryString = qs_1.default.stringify(rest);
835
- return queryString ? `${this.url()}?${queryString}` === signedUrl : this.url() === signedUrl;
836
- }
837
- /**
838
- * toJSON copy of the request
839
- */
840
- toJSON() {
841
- return {
842
- id: this.id(),
843
- url: this.url(),
844
- query: this.parsedUrl.query,
845
- body: this.all(),
846
- params: this.params(),
847
- headers: this.headers(),
848
- method: this.method(),
849
- protocol: this.protocol(),
850
- cookies: this.cookiesList(),
851
- hostname: this.hostname(),
852
- ip: this.ip(),
853
- subdomains: this.ctx.subdomains,
854
- };
855
- }
856
- }
857
- exports.Request = Request;
858
- /**
859
- * Required by Macroable
860
- */
861
- Request.macros = {};
862
- Request.getters = {};