@adonisjs/http-server 8.0.0-next.5 → 8.0.0-next.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/{chunk-BFGF3A5X.js → chunk-3XEJZ4S4.js} +1105 -566
- package/build/{chunk-ASX56VAK.js → chunk-NQNHMINZ.js} +59 -0
- package/build/factories/http_context.d.ts +2 -1
- package/build/factories/http_server.d.ts +7 -0
- package/build/factories/main.js +31 -5
- package/build/factories/qs_parser_factory.d.ts +3 -2
- package/build/factories/request.d.ts +1 -0
- package/build/factories/response.d.ts +1 -0
- package/build/factories/router.d.ts +1 -0
- package/build/factories/server_factory.d.ts +1 -0
- package/build/factories/url_builder_factory.d.ts +1 -0
- package/build/index.d.ts +3 -1
- package/build/index.js +87 -37
- package/build/src/cookies/client.d.ts +35 -0
- package/build/src/cookies/drivers/encrypted.d.ts +13 -0
- package/build/src/cookies/drivers/plain.d.ts +9 -0
- package/build/src/cookies/drivers/signed.d.ts +13 -0
- package/build/src/cookies/parser.d.ts +18 -0
- package/build/src/cookies/serializer.d.ts +21 -2
- package/build/src/define_config.d.ts +1 -3
- package/build/src/define_middleware.d.ts +1 -1
- package/build/src/exception_handler.d.ts +72 -31
- package/build/src/helpers.d.ts +50 -0
- package/build/src/helpers.js +5 -1
- package/build/src/http_context/local_storage.d.ts +17 -0
- package/build/src/http_context/main.d.ts +8 -0
- package/build/src/qs.d.ts +14 -0
- package/build/src/redirect.d.ts +62 -9
- package/build/src/request.d.ts +109 -10
- package/build/src/response.d.ts +416 -203
- package/build/src/router/brisk.d.ts +13 -0
- package/build/src/router/executor.d.ts +4 -0
- package/build/src/router/factories/use_return_value.d.ts +5 -0
- package/build/src/router/group.d.ts +18 -1
- package/build/src/router/legacy/url_builder.d.ts +14 -14
- package/build/src/router/main.d.ts +73 -4
- package/build/src/router/matchers.d.ts +3 -0
- package/build/src/router/resource.d.ts +34 -1
- package/build/src/router/route.d.ts +9 -1
- package/build/src/router/signed_url_builder.d.ts +4 -8
- package/build/src/router/store.d.ts +9 -0
- package/build/src/router/url_builder.d.ts +4 -9
- package/build/src/server/factories/middleware_handler.d.ts +10 -1
- package/build/src/server/factories/route_finder.d.ts +13 -3
- package/build/src/server/factories/write_response.d.ts +9 -2
- package/build/src/server/main.d.ts +77 -23
- package/build/src/tracing_channels.d.ts +4 -4
- package/build/src/types/middleware.d.ts +34 -9
- package/build/src/types/qs.d.ts +5 -0
- package/build/src/types/request.d.ts +1 -1
- package/build/src/types/response.d.ts +14 -6
- package/build/src/types/route.d.ts +40 -19
- package/build/src/types/server.d.ts +26 -11
- package/build/src/types/tracing_channels.d.ts +21 -3
- package/build/src/types/url_builder.d.ts +24 -11
- package/package.json +17 -15
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import {
|
|
2
2
|
__export,
|
|
3
|
+
createSignedURL,
|
|
4
|
+
createURL,
|
|
3
5
|
default as default2,
|
|
4
6
|
default2 as default3,
|
|
5
7
|
parseRoute,
|
|
6
8
|
serializeCookie
|
|
7
|
-
} from "./chunk-
|
|
9
|
+
} from "./chunk-NQNHMINZ.js";
|
|
8
10
|
|
|
9
11
|
// src/errors.ts
|
|
10
12
|
var errors_exports = {};
|
|
@@ -107,42 +109,76 @@ function unpack3(key, encryptedValue, encryption) {
|
|
|
107
109
|
|
|
108
110
|
// src/cookies/client.ts
|
|
109
111
|
var CookieClient = class {
|
|
112
|
+
/**
|
|
113
|
+
* Private encryption instance used for signing and encrypting cookies
|
|
114
|
+
*/
|
|
110
115
|
#encryption;
|
|
116
|
+
/**
|
|
117
|
+
* Create a new instance of CookieClient
|
|
118
|
+
*
|
|
119
|
+
* @param encryption - The encryption instance for cookie operations
|
|
120
|
+
*/
|
|
111
121
|
constructor(encryption) {
|
|
112
122
|
this.#encryption = encryption;
|
|
113
123
|
}
|
|
114
124
|
/**
|
|
115
125
|
* Encrypt a key value pair to be sent in the cookie header
|
|
126
|
+
*
|
|
127
|
+
* @param key - The cookie key
|
|
128
|
+
* @param value - The value to encrypt
|
|
129
|
+
* @returns The encrypted cookie string or null if encryption fails
|
|
116
130
|
*/
|
|
117
131
|
encrypt(key, value) {
|
|
118
132
|
return pack3(key, value, this.#encryption);
|
|
119
133
|
}
|
|
120
134
|
/**
|
|
121
135
|
* Sign a key value pair to be sent in the cookie header
|
|
136
|
+
*
|
|
137
|
+
* @param key - The cookie key
|
|
138
|
+
* @param value - The value to sign
|
|
139
|
+
* @returns The signed cookie string or null if signing fails
|
|
122
140
|
*/
|
|
123
141
|
sign(key, value) {
|
|
124
142
|
return pack2(key, value, this.#encryption);
|
|
125
143
|
}
|
|
126
144
|
/**
|
|
127
145
|
* Encode a key value pair to be sent in the cookie header
|
|
146
|
+
*
|
|
147
|
+
* @param _ - Unused key parameter
|
|
148
|
+
* @param value - The value to encode
|
|
149
|
+
* @param stringify - Whether to stringify the value before encoding
|
|
150
|
+
* @returns The encoded cookie string or null if encoding fails
|
|
128
151
|
*/
|
|
129
152
|
encode(_, value, stringify2 = true) {
|
|
130
153
|
return stringify2 ? pack(value) : value;
|
|
131
154
|
}
|
|
132
155
|
/**
|
|
133
156
|
* Unsign a signed cookie value
|
|
157
|
+
*
|
|
158
|
+
* @param key - The cookie key
|
|
159
|
+
* @param value - The signed cookie value to unsign
|
|
160
|
+
* @returns The original value if valid signature, null otherwise
|
|
134
161
|
*/
|
|
135
162
|
unsign(key, value) {
|
|
136
163
|
return canUnpack2(value) ? unpack2(key, value, this.#encryption) : null;
|
|
137
164
|
}
|
|
138
165
|
/**
|
|
139
166
|
* Decrypt an encrypted cookie value
|
|
167
|
+
*
|
|
168
|
+
* @param key - The cookie key
|
|
169
|
+
* @param value - The encrypted cookie value to decrypt
|
|
170
|
+
* @returns The decrypted value or null if decryption fails
|
|
140
171
|
*/
|
|
141
172
|
decrypt(key, value) {
|
|
142
173
|
return canUnpack3(value) ? unpack3(key, value, this.#encryption) : null;
|
|
143
174
|
}
|
|
144
175
|
/**
|
|
145
176
|
* Decode an encoded cookie value
|
|
177
|
+
*
|
|
178
|
+
* @param _ - Unused key parameter
|
|
179
|
+
* @param value - The encoded cookie value to decode
|
|
180
|
+
* @param stringified - Whether the value was stringified during encoding
|
|
181
|
+
* @returns The decoded value or null if decoding fails
|
|
146
182
|
*/
|
|
147
183
|
decode(_, value, stringified = true) {
|
|
148
184
|
if (!stringified) {
|
|
@@ -152,6 +188,10 @@ var CookieClient = class {
|
|
|
152
188
|
}
|
|
153
189
|
/**
|
|
154
190
|
* Parse response cookie
|
|
191
|
+
*
|
|
192
|
+
* @param key - The cookie key
|
|
193
|
+
* @param value - The cookie value to parse
|
|
194
|
+
* @returns The parsed value or undefined if parsing fails
|
|
155
195
|
*/
|
|
156
196
|
parse(key, value) {
|
|
157
197
|
if (canUnpack2(value)) {
|
|
@@ -166,6 +206,129 @@ var CookieClient = class {
|
|
|
166
206
|
}
|
|
167
207
|
};
|
|
168
208
|
|
|
209
|
+
// src/cookies/parser.ts
|
|
210
|
+
import cookie from "cookie";
|
|
211
|
+
var CookieParser = class {
|
|
212
|
+
/**
|
|
213
|
+
* Cookie client instance for handling cookie operations
|
|
214
|
+
*/
|
|
215
|
+
#client;
|
|
216
|
+
/**
|
|
217
|
+
* A copy of cached cookies, they are cached during a request after
|
|
218
|
+
* initial decoding, unsigning or decrypting.
|
|
219
|
+
*/
|
|
220
|
+
#cachedCookies = {
|
|
221
|
+
signedCookies: {},
|
|
222
|
+
plainCookies: {},
|
|
223
|
+
encryptedCookies: {}
|
|
224
|
+
};
|
|
225
|
+
/**
|
|
226
|
+
* An object of key-value pair collected by parsing
|
|
227
|
+
* the request cookie header.
|
|
228
|
+
*/
|
|
229
|
+
#cookies;
|
|
230
|
+
/**
|
|
231
|
+
* Create a new instance of CookieParser
|
|
232
|
+
*
|
|
233
|
+
* @param cookieHeader - The raw cookie header string from the request
|
|
234
|
+
* @param encryption - The encryption instance for cookie operations
|
|
235
|
+
*/
|
|
236
|
+
constructor(cookieHeader, encryption) {
|
|
237
|
+
this.#client = new CookieClient(encryption);
|
|
238
|
+
this.#cookies = this.#parse(cookieHeader);
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* Parses the request `cookie` header
|
|
242
|
+
*
|
|
243
|
+
* @param cookieHeader - The cookie header string to parse
|
|
244
|
+
* @returns Parsed cookies as key-value pairs
|
|
245
|
+
*/
|
|
246
|
+
#parse(cookieHeader) {
|
|
247
|
+
if (!cookieHeader) {
|
|
248
|
+
return {};
|
|
249
|
+
}
|
|
250
|
+
return cookie.parse(cookieHeader);
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* Attempts to decode a cookie by the name. When calling this method,
|
|
254
|
+
* you are assuming that the cookie was just stringified in the first
|
|
255
|
+
* place and not signed or encrypted.
|
|
256
|
+
*
|
|
257
|
+
* @param key - The cookie key to decode
|
|
258
|
+
* @param stringified - Whether the cookie value was stringified
|
|
259
|
+
* @returns The decoded cookie value or null if decoding fails
|
|
260
|
+
*/
|
|
261
|
+
decode(key, stringified = true) {
|
|
262
|
+
const value = this.#cookies[key];
|
|
263
|
+
if (value === null || value === void 0) {
|
|
264
|
+
return null;
|
|
265
|
+
}
|
|
266
|
+
const cache = this.#cachedCookies.plainCookies;
|
|
267
|
+
if (cache[key] !== void 0) {
|
|
268
|
+
return cache[key];
|
|
269
|
+
}
|
|
270
|
+
const parsed = this.#client.decode(key, value, stringified);
|
|
271
|
+
if (parsed !== null) {
|
|
272
|
+
cache[key] = parsed;
|
|
273
|
+
}
|
|
274
|
+
return parsed;
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* Attempts to unsign a cookie by the name. When calling this method,
|
|
278
|
+
* you are assuming that the cookie was signed in the first place.
|
|
279
|
+
*
|
|
280
|
+
* @param key - The cookie key to unsign
|
|
281
|
+
* @returns The original cookie value or null if unsigning fails
|
|
282
|
+
*/
|
|
283
|
+
unsign(key) {
|
|
284
|
+
const value = this.#cookies[key];
|
|
285
|
+
if (value === null || value === void 0) {
|
|
286
|
+
return null;
|
|
287
|
+
}
|
|
288
|
+
const cache = this.#cachedCookies.signedCookies;
|
|
289
|
+
if (cache[key] !== void 0) {
|
|
290
|
+
return cache[key];
|
|
291
|
+
}
|
|
292
|
+
const parsed = this.#client.unsign(key, value);
|
|
293
|
+
if (parsed !== null) {
|
|
294
|
+
cache[key] = parsed;
|
|
295
|
+
}
|
|
296
|
+
return parsed;
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* Attempts to decrypt a cookie by the name. When calling this method,
|
|
300
|
+
* you are assuming that the cookie was encrypted in the first place.
|
|
301
|
+
*
|
|
302
|
+
* @param key - The cookie key to decrypt
|
|
303
|
+
* @returns The decrypted cookie value or null if decryption fails
|
|
304
|
+
*/
|
|
305
|
+
decrypt(key) {
|
|
306
|
+
const value = this.#cookies[key];
|
|
307
|
+
if (value === null || value === void 0) {
|
|
308
|
+
return null;
|
|
309
|
+
}
|
|
310
|
+
const cache = this.#cachedCookies.encryptedCookies;
|
|
311
|
+
if (cache[key] !== void 0) {
|
|
312
|
+
return cache[key];
|
|
313
|
+
}
|
|
314
|
+
const parsed = this.#client.decrypt(key, value);
|
|
315
|
+
if (parsed !== null) {
|
|
316
|
+
cache[key] = parsed;
|
|
317
|
+
}
|
|
318
|
+
return parsed;
|
|
319
|
+
}
|
|
320
|
+
/**
|
|
321
|
+
* Returns an object of cookies key-value pair. Do note, the
|
|
322
|
+
* cookies are not decoded, unsigned or decrypted inside this
|
|
323
|
+
* list.
|
|
324
|
+
*
|
|
325
|
+
* @returns Raw cookies as key-value pairs
|
|
326
|
+
*/
|
|
327
|
+
list() {
|
|
328
|
+
return this.#cookies;
|
|
329
|
+
}
|
|
330
|
+
};
|
|
331
|
+
|
|
169
332
|
// src/tracing_channels.ts
|
|
170
333
|
var tracing_channels_exports = {};
|
|
171
334
|
__export(tracing_channels_exports, {
|
|
@@ -176,14 +339,14 @@ __export(tracing_channels_exports, {
|
|
|
176
339
|
httpRouteHandler: () => httpRouteHandler
|
|
177
340
|
});
|
|
178
341
|
import diagnostics_channel from "diagnostics_channel";
|
|
179
|
-
var httpRequest = diagnostics_channel.tracingChannel("adonisjs
|
|
180
|
-
var httpMiddleware = diagnostics_channel.tracingChannel("adonisjs
|
|
342
|
+
var httpRequest = diagnostics_channel.tracingChannel("adonisjs.http.request");
|
|
343
|
+
var httpMiddleware = diagnostics_channel.tracingChannel("adonisjs.http.middleware");
|
|
181
344
|
var httpExceptionHandler = diagnostics_channel.tracingChannel(
|
|
182
|
-
"adonisjs
|
|
345
|
+
"adonisjs.http.exception.handler"
|
|
183
346
|
);
|
|
184
|
-
var httpRouteHandler = diagnostics_channel.tracingChannel("adonisjs
|
|
347
|
+
var httpRouteHandler = diagnostics_channel.tracingChannel("adonisjs.http.route.handler");
|
|
185
348
|
var httpResponseSerializer = diagnostics_channel.tracingChannel(
|
|
186
|
-
"adonisjs
|
|
349
|
+
"adonisjs.http.response.serializer"
|
|
187
350
|
);
|
|
188
351
|
|
|
189
352
|
// src/router/route.ts
|
|
@@ -217,14 +380,14 @@ function execute(route, resolver, ctx, errorResponder) {
|
|
|
217
380
|
if (typeof route.handler === "function") {
|
|
218
381
|
return httpRouteHandler.tracePromise(
|
|
219
382
|
($ctx) => Promise.resolve(route.handler($ctx)),
|
|
220
|
-
route,
|
|
383
|
+
httpRouteHandler.hasSubscribers ? { route } : void 0,
|
|
221
384
|
void 0,
|
|
222
385
|
ctx
|
|
223
386
|
).then(useReturnValue(ctx));
|
|
224
387
|
}
|
|
225
388
|
return httpRouteHandler.tracePromise(
|
|
226
389
|
route.handler.handle,
|
|
227
|
-
route,
|
|
390
|
+
httpRouteHandler.hasSubscribers ? { route } : void 0,
|
|
228
391
|
void 0,
|
|
229
392
|
resolver,
|
|
230
393
|
ctx
|
|
@@ -233,7 +396,7 @@ function execute(route, resolver, ctx, errorResponder) {
|
|
|
233
396
|
if (typeof middleware === "function") {
|
|
234
397
|
return httpMiddleware.tracePromise(
|
|
235
398
|
middleware,
|
|
236
|
-
middleware,
|
|
399
|
+
httpMiddleware.hasSubscribers ? { middleware } : void 0,
|
|
237
400
|
void 0,
|
|
238
401
|
ctx,
|
|
239
402
|
next
|
|
@@ -241,7 +404,7 @@ function execute(route, resolver, ctx, errorResponder) {
|
|
|
241
404
|
}
|
|
242
405
|
return httpMiddleware.tracePromise(
|
|
243
406
|
middleware.handle,
|
|
244
|
-
middleware,
|
|
407
|
+
httpMiddleware.hasSubscribers ? { middleware } : void 0,
|
|
245
408
|
void 0,
|
|
246
409
|
resolver,
|
|
247
410
|
ctx,
|
|
@@ -281,6 +444,12 @@ var BriskRoute = class extends Macroable {
|
|
|
281
444
|
* Reference to route instance. Set after `setHandler` is called
|
|
282
445
|
*/
|
|
283
446
|
route = null;
|
|
447
|
+
/**
|
|
448
|
+
* Creates a new BriskRoute instance
|
|
449
|
+
* @param app - The AdonisJS application instance
|
|
450
|
+
* @param routerMiddleware - Array of global middleware registered on the router
|
|
451
|
+
* @param options - Configuration options for the brisk route
|
|
452
|
+
*/
|
|
284
453
|
constructor(app, routerMiddleware, options) {
|
|
285
454
|
super();
|
|
286
455
|
this.#app = app;
|
|
@@ -290,6 +459,8 @@ var BriskRoute = class extends Macroable {
|
|
|
290
459
|
}
|
|
291
460
|
/**
|
|
292
461
|
* Set handler for the brisk route
|
|
462
|
+
* @param handler - The route handler function
|
|
463
|
+
* @returns The created route instance
|
|
293
464
|
*/
|
|
294
465
|
setHandler(handler) {
|
|
295
466
|
this.route = new Route(this.#app, this.#routerMiddleware, {
|
|
@@ -303,6 +474,8 @@ var BriskRoute = class extends Macroable {
|
|
|
303
474
|
/**
|
|
304
475
|
* Redirects to a given route. Params from the original request will
|
|
305
476
|
* be used when no custom params are defined.
|
|
477
|
+
* @param args - Route identifier, parameters, and options for building the redirect URL
|
|
478
|
+
* @returns The created route instance
|
|
306
479
|
*/
|
|
307
480
|
redirect(...args) {
|
|
308
481
|
const [identifier, params, options] = args;
|
|
@@ -318,6 +491,9 @@ var BriskRoute = class extends Macroable {
|
|
|
318
491
|
}
|
|
319
492
|
/**
|
|
320
493
|
* Redirect request to a fixed URL
|
|
494
|
+
* @param url - The URL to redirect to
|
|
495
|
+
* @param options - Optional redirect options including HTTP status code
|
|
496
|
+
* @returns The created route instance
|
|
321
497
|
*/
|
|
322
498
|
redirectToPath(url, options) {
|
|
323
499
|
function redirectsToPath(ctx) {
|
|
@@ -378,6 +554,12 @@ var RouteResource = class extends Macroable2 {
|
|
|
378
554
|
* A collection of routes instances that belongs to this resource
|
|
379
555
|
*/
|
|
380
556
|
routes = [];
|
|
557
|
+
/**
|
|
558
|
+
* Creates a new RouteResource instance
|
|
559
|
+
* @param app - The AdonisJS application instance
|
|
560
|
+
* @param routerMiddleware - Array of global middleware registered on the router
|
|
561
|
+
* @param options - Configuration options for the route resource
|
|
562
|
+
*/
|
|
381
563
|
constructor(app, routerMiddleware, options) {
|
|
382
564
|
super();
|
|
383
565
|
this.#validateResourceName(options.resource);
|
|
@@ -468,6 +650,8 @@ var RouteResource = class extends Macroable2 {
|
|
|
468
650
|
}
|
|
469
651
|
/**
|
|
470
652
|
* Register only given routes and remove others
|
|
653
|
+
* @param names - Array of action names to keep
|
|
654
|
+
* @returns Current RouteResource instance with filtered actions
|
|
471
655
|
*/
|
|
472
656
|
only(names) {
|
|
473
657
|
this.#filter(names, true).forEach((route) => route.markAsDeleted());
|
|
@@ -475,6 +659,8 @@ var RouteResource = class extends Macroable2 {
|
|
|
475
659
|
}
|
|
476
660
|
/**
|
|
477
661
|
* Register all routes, except the one's defined
|
|
662
|
+
* @param names - Array of action names to exclude
|
|
663
|
+
* @returns Current RouteResource instance with filtered actions
|
|
478
664
|
*/
|
|
479
665
|
except(names) {
|
|
480
666
|
this.#filter(names, false).forEach((route) => route.markAsDeleted());
|
|
@@ -483,12 +669,16 @@ var RouteResource = class extends Macroable2 {
|
|
|
483
669
|
/**
|
|
484
670
|
* Register api only routes. The `create` and `edit` routes, which
|
|
485
671
|
* are meant to show forms will not be registered
|
|
672
|
+
* @returns Current RouteResource instance without create and edit actions
|
|
486
673
|
*/
|
|
487
674
|
apiOnly() {
|
|
488
675
|
return this.except(["create", "edit"]);
|
|
489
676
|
}
|
|
490
677
|
/**
|
|
491
678
|
* Define matcher for params inside the resource
|
|
679
|
+
* @param key - The parameter name to match
|
|
680
|
+
* @param matcher - The matcher pattern (RegExp, string, or RouteMatcher)
|
|
681
|
+
* @returns Current RouteResource instance for method chaining
|
|
492
682
|
*/
|
|
493
683
|
where(key, matcher) {
|
|
494
684
|
this.routes.forEach((route) => {
|
|
@@ -514,6 +704,8 @@ var RouteResource = class extends Macroable2 {
|
|
|
514
704
|
}
|
|
515
705
|
/**
|
|
516
706
|
* Set the param name for a given resource
|
|
707
|
+
* @param resources - Object mapping resource names to parameter names
|
|
708
|
+
* @returns Current RouteResource instance for method chaining
|
|
517
709
|
*/
|
|
518
710
|
params(resources) {
|
|
519
711
|
Object.keys(resources).forEach((resource) => {
|
|
@@ -534,6 +726,9 @@ var RouteResource = class extends Macroable2 {
|
|
|
534
726
|
*
|
|
535
727
|
* Calling this method multiple times will append middleware
|
|
536
728
|
* to existing list.
|
|
729
|
+
* @param actions - Action name(s) or '*' for all actions
|
|
730
|
+
* @param middleware - Middleware function(s) to apply
|
|
731
|
+
* @returns Current RouteResource instance for method chaining
|
|
537
732
|
*/
|
|
538
733
|
use(actions, middleware) {
|
|
539
734
|
if (actions === "*") {
|
|
@@ -544,13 +739,19 @@ var RouteResource = class extends Macroable2 {
|
|
|
544
739
|
return this;
|
|
545
740
|
}
|
|
546
741
|
/**
|
|
547
|
-
* @
|
|
742
|
+
* Alias for {@link RouteResource.use}
|
|
743
|
+
* @param actions - Action name(s) or '*' for all actions
|
|
744
|
+
* @param middleware - Middleware function(s) to apply
|
|
745
|
+
* @returns Current RouteResource instance for method chaining
|
|
548
746
|
*/
|
|
549
747
|
middleware(actions, middleware) {
|
|
550
748
|
return this.use(actions, middleware);
|
|
551
749
|
}
|
|
552
750
|
/**
|
|
553
751
|
* Prepend name to all the routes
|
|
752
|
+
* @param name - The name to prepend to all route names
|
|
753
|
+
* @param normalizeName - Whether to normalize the name to snake_case
|
|
754
|
+
* @returns Current RouteResource instance for method chaining
|
|
554
755
|
*/
|
|
555
756
|
as(name, normalizeName = true) {
|
|
556
757
|
name = normalizeName ? string.snakeCase(name) : name;
|
|
@@ -564,6 +765,10 @@ var RouteResource = class extends Macroable2 {
|
|
|
564
765
|
|
|
565
766
|
// src/router/group.ts
|
|
566
767
|
var RouteGroup = class _RouteGroup extends Macroable3 {
|
|
768
|
+
/**
|
|
769
|
+
* Creates a new RouteGroup instance
|
|
770
|
+
* @param routes - Array of routes that belong to this group
|
|
771
|
+
*/
|
|
567
772
|
constructor(routes) {
|
|
568
773
|
super();
|
|
569
774
|
this.routes = routes;
|
|
@@ -679,6 +884,9 @@ var RouteGroup = class _RouteGroup extends Macroable3 {
|
|
|
679
884
|
* Route.group(() => {
|
|
680
885
|
* }).where('id', /^[0-9]+/)
|
|
681
886
|
* ```
|
|
887
|
+
* @param param - The parameter name to match
|
|
888
|
+
* @param matcher - The matcher pattern (RegExp, string, or RouteMatcher)
|
|
889
|
+
* @returns Current RouteGroup instance for method chaining
|
|
682
890
|
*/
|
|
683
891
|
where(param, matcher) {
|
|
684
892
|
this.routes.forEach((route) => this.#updateRouteMatchers(route, param, matcher));
|
|
@@ -691,6 +899,8 @@ var RouteGroup = class _RouteGroup extends Macroable3 {
|
|
|
691
899
|
* Route.group(() => {
|
|
692
900
|
* }).prefix('v1')
|
|
693
901
|
* ```
|
|
902
|
+
* @param prefix - The prefix to add to all routes in the group
|
|
903
|
+
* @returns Current RouteGroup instance for method chaining
|
|
694
904
|
*/
|
|
695
905
|
prefix(prefix) {
|
|
696
906
|
this.routes.forEach((route) => this.#setRoutePrefix(route, prefix));
|
|
@@ -703,6 +913,8 @@ var RouteGroup = class _RouteGroup extends Macroable3 {
|
|
|
703
913
|
* Route.group(() => {
|
|
704
914
|
* }).domain(':name.adonisjs.com')
|
|
705
915
|
* ```
|
|
916
|
+
* @param domain - The domain pattern for all routes in the group
|
|
917
|
+
* @returns Current RouteGroup instance for method chaining
|
|
706
918
|
*/
|
|
707
919
|
domain(domain) {
|
|
708
920
|
this.routes.forEach((route) => this.#updateRouteDomain(route, domain));
|
|
@@ -715,6 +927,8 @@ var RouteGroup = class _RouteGroup extends Macroable3 {
|
|
|
715
927
|
* Route.group(() => {
|
|
716
928
|
* }).as('version1')
|
|
717
929
|
* ```
|
|
930
|
+
* @param name - The name to prepend to all route names in the group
|
|
931
|
+
* @returns Current RouteGroup instance for method chaining
|
|
718
932
|
*/
|
|
719
933
|
as(name) {
|
|
720
934
|
this.routes.forEach((route) => this.#updateRouteName(route, name));
|
|
@@ -727,6 +941,8 @@ var RouteGroup = class _RouteGroup extends Macroable3 {
|
|
|
727
941
|
* Route.group(() => {
|
|
728
942
|
* }).use(middleware.auth())
|
|
729
943
|
* ```
|
|
944
|
+
* @param middleware - Middleware function(s) to apply to all routes in the group
|
|
945
|
+
* @returns Current RouteGroup instance for method chaining
|
|
730
946
|
*/
|
|
731
947
|
use(middleware) {
|
|
732
948
|
if (!this.#middleware.length) {
|
|
@@ -742,7 +958,9 @@ var RouteGroup = class _RouteGroup extends Macroable3 {
|
|
|
742
958
|
return this;
|
|
743
959
|
}
|
|
744
960
|
/**
|
|
745
|
-
* @
|
|
961
|
+
* Alias for {@link RouteGroup.use}
|
|
962
|
+
* @param middleware - Middleware function(s) to apply to all routes in the group
|
|
963
|
+
* @returns Current RouteGroup instance for method chaining
|
|
746
964
|
*/
|
|
747
965
|
middleware(middleware) {
|
|
748
966
|
return this.use(middleware);
|
|
@@ -934,6 +1152,12 @@ var Route = class extends Macroable4 {
|
|
|
934
1152
|
* routes. We mantain an array for each layer of the stack
|
|
935
1153
|
*/
|
|
936
1154
|
#middleware = [];
|
|
1155
|
+
/**
|
|
1156
|
+
* Creates a new Route instance
|
|
1157
|
+
* @param app - The AdonisJS application instance
|
|
1158
|
+
* @param routerMiddleware - Array of global middleware registered on the router
|
|
1159
|
+
* @param options - Configuration options for the route
|
|
1160
|
+
*/
|
|
937
1161
|
constructor(app, routerMiddleware, options) {
|
|
938
1162
|
super();
|
|
939
1163
|
this.#app = app;
|
|
@@ -1037,6 +1261,8 @@ var Route = class extends Macroable4 {
|
|
|
1037
1261
|
/**
|
|
1038
1262
|
* Define prefix for the route. Calling this method multiple times
|
|
1039
1263
|
* applies multiple prefixes in the reverse order.
|
|
1264
|
+
* @param prefix - The prefix to add to the route
|
|
1265
|
+
* @returns Current Route instance for method chaining
|
|
1040
1266
|
*/
|
|
1041
1267
|
prefix(prefix) {
|
|
1042
1268
|
this.#prefixes.push(prefix);
|
|
@@ -1064,7 +1290,7 @@ var Route = class extends Macroable4 {
|
|
|
1064
1290
|
return this;
|
|
1065
1291
|
}
|
|
1066
1292
|
/**
|
|
1067
|
-
* @
|
|
1293
|
+
* Alias for {@link Route.use}
|
|
1068
1294
|
*/
|
|
1069
1295
|
middleware(middleware) {
|
|
1070
1296
|
return this.use(middleware);
|
|
@@ -1175,108 +1401,15 @@ import proxyaddr from "proxy-addr";
|
|
|
1175
1401
|
import { safeEqual } from "@poppinss/utils";
|
|
1176
1402
|
import Macroable5 from "@poppinss/macroable";
|
|
1177
1403
|
import lodash from "@poppinss/utils/lodash";
|
|
1178
|
-
|
|
1179
|
-
// src/cookies/parser.ts
|
|
1180
|
-
import cookie from "cookie";
|
|
1181
|
-
var CookieParser = class {
|
|
1182
|
-
#client;
|
|
1183
|
-
/**
|
|
1184
|
-
* A copy of cached cookies, they are cached during a request after
|
|
1185
|
-
* initial decoding, unsigning or decrypting.
|
|
1186
|
-
*/
|
|
1187
|
-
#cachedCookies = {
|
|
1188
|
-
signedCookies: {},
|
|
1189
|
-
plainCookies: {},
|
|
1190
|
-
encryptedCookies: {}
|
|
1191
|
-
};
|
|
1192
|
-
/**
|
|
1193
|
-
* An object of key-value pair collected by parsing
|
|
1194
|
-
* the request cookie header.
|
|
1195
|
-
*/
|
|
1196
|
-
#cookies;
|
|
1197
|
-
constructor(cookieHeader, encryption) {
|
|
1198
|
-
this.#client = new CookieClient(encryption);
|
|
1199
|
-
this.#cookies = this.#parse(cookieHeader);
|
|
1200
|
-
}
|
|
1201
|
-
/**
|
|
1202
|
-
* Parses the request `cookie` header
|
|
1203
|
-
*/
|
|
1204
|
-
#parse(cookieHeader) {
|
|
1205
|
-
if (!cookieHeader) {
|
|
1206
|
-
return {};
|
|
1207
|
-
}
|
|
1208
|
-
return cookie.parse(cookieHeader);
|
|
1209
|
-
}
|
|
1210
|
-
/**
|
|
1211
|
-
* Attempts to decode a cookie by the name. When calling this method,
|
|
1212
|
-
* you are assuming that the cookie was just stringified in the first
|
|
1213
|
-
* place and not signed or encrypted.
|
|
1214
|
-
*/
|
|
1215
|
-
decode(key, stringified = true) {
|
|
1216
|
-
const value = this.#cookies[key];
|
|
1217
|
-
if (value === null || value === void 0) {
|
|
1218
|
-
return null;
|
|
1219
|
-
}
|
|
1220
|
-
const cache = this.#cachedCookies.plainCookies;
|
|
1221
|
-
if (cache[key] !== void 0) {
|
|
1222
|
-
return cache[key];
|
|
1223
|
-
}
|
|
1224
|
-
const parsed = this.#client.decode(key, value, stringified);
|
|
1225
|
-
if (parsed !== null) {
|
|
1226
|
-
cache[key] = parsed;
|
|
1227
|
-
}
|
|
1228
|
-
return parsed;
|
|
1229
|
-
}
|
|
1230
|
-
/**
|
|
1231
|
-
* Attempts to unsign a cookie by the name. When calling this method,
|
|
1232
|
-
* you are assuming that the cookie was signed in the first place.
|
|
1233
|
-
*/
|
|
1234
|
-
unsign(key) {
|
|
1235
|
-
const value = this.#cookies[key];
|
|
1236
|
-
if (value === null || value === void 0) {
|
|
1237
|
-
return null;
|
|
1238
|
-
}
|
|
1239
|
-
const cache = this.#cachedCookies.signedCookies;
|
|
1240
|
-
if (cache[key] !== void 0) {
|
|
1241
|
-
return cache[key];
|
|
1242
|
-
}
|
|
1243
|
-
const parsed = this.#client.unsign(key, value);
|
|
1244
|
-
if (parsed !== null) {
|
|
1245
|
-
cache[key] = parsed;
|
|
1246
|
-
}
|
|
1247
|
-
return parsed;
|
|
1248
|
-
}
|
|
1249
|
-
/**
|
|
1250
|
-
* Attempts to decrypt a cookie by the name. When calling this method,
|
|
1251
|
-
* you are assuming that the cookie was encrypted in the first place.
|
|
1252
|
-
*/
|
|
1253
|
-
decrypt(key) {
|
|
1254
|
-
const value = this.#cookies[key];
|
|
1255
|
-
if (value === null || value === void 0) {
|
|
1256
|
-
return null;
|
|
1257
|
-
}
|
|
1258
|
-
const cache = this.#cachedCookies.encryptedCookies;
|
|
1259
|
-
if (cache[key] !== void 0) {
|
|
1260
|
-
return cache[key];
|
|
1261
|
-
}
|
|
1262
|
-
const parsed = this.#client.decrypt(key, value);
|
|
1263
|
-
if (parsed !== null) {
|
|
1264
|
-
cache[key] = parsed;
|
|
1265
|
-
}
|
|
1266
|
-
return parsed;
|
|
1267
|
-
}
|
|
1404
|
+
var Request = class extends Macroable5 {
|
|
1268
1405
|
/**
|
|
1269
|
-
*
|
|
1270
|
-
*
|
|
1271
|
-
*
|
|
1406
|
+
* Creates a new Request instance wrapping the native Node.js HTTP request
|
|
1407
|
+
* @param request - Native Node.js incoming message instance
|
|
1408
|
+
* @param response - Native Node.js server response instance
|
|
1409
|
+
* @param encryption - Encryption module for cookie and URL signing
|
|
1410
|
+
* @param config - Request configuration options
|
|
1411
|
+
* @param qsParser - Query string parser instance
|
|
1272
1412
|
*/
|
|
1273
|
-
list() {
|
|
1274
|
-
return this.#cookies;
|
|
1275
|
-
}
|
|
1276
|
-
};
|
|
1277
|
-
|
|
1278
|
-
// src/request.ts
|
|
1279
|
-
var Request = class extends Macroable5 {
|
|
1280
1413
|
constructor(request, response, encryption, config, qsParser) {
|
|
1281
1414
|
super();
|
|
1282
1415
|
this.request = request;
|
|
@@ -1331,16 +1464,15 @@ var Request = class extends Macroable5 {
|
|
|
1331
1464
|
*/
|
|
1332
1465
|
#cookieParser;
|
|
1333
1466
|
/**
|
|
1334
|
-
* Parsed URL with query string stored as a string
|
|
1467
|
+
* Parsed URL with query string stored as a string and decode flag
|
|
1335
1468
|
*/
|
|
1336
1469
|
parsedUrl;
|
|
1337
1470
|
/**
|
|
1338
|
-
*
|
|
1339
|
-
* reference
|
|
1471
|
+
* HTTP context reference - creates a circular reference when set by the context
|
|
1340
1472
|
*/
|
|
1341
1473
|
ctx;
|
|
1342
1474
|
/**
|
|
1343
|
-
* Parses the query string
|
|
1475
|
+
* Parses the query string from the parsed URL and updates internal state
|
|
1344
1476
|
*/
|
|
1345
1477
|
#parseQueryString() {
|
|
1346
1478
|
if (this.parsedUrl.query) {
|
|
@@ -1349,7 +1481,7 @@ var Request = class extends Macroable5 {
|
|
|
1349
1481
|
}
|
|
1350
1482
|
}
|
|
1351
1483
|
/**
|
|
1352
|
-
* Initiates the cookie parser lazily
|
|
1484
|
+
* Initiates the cookie parser lazily when first needed
|
|
1353
1485
|
*/
|
|
1354
1486
|
#initiateCookieParser() {
|
|
1355
1487
|
if (!this.#cookieParser) {
|
|
@@ -1367,6 +1499,7 @@ var Request = class extends Macroable5 {
|
|
|
1367
1499
|
/**
|
|
1368
1500
|
* Returns the request id from the `x-request-id` header. The
|
|
1369
1501
|
* header is untouched, if it already exists.
|
|
1502
|
+
* @returns The request ID or undefined if not found/generated
|
|
1370
1503
|
*/
|
|
1371
1504
|
id() {
|
|
1372
1505
|
let requestId = this.header("x-request-id");
|
|
@@ -1383,6 +1516,8 @@ var Request = class extends Macroable5 {
|
|
|
1383
1516
|
*
|
|
1384
1517
|
* This method is supposed to be invoked by the body parser and must be called only
|
|
1385
1518
|
* once. For further mutations make use of `updateBody` method.
|
|
1519
|
+
* @param body - Parsed request body data
|
|
1520
|
+
* @returns {void}
|
|
1386
1521
|
*/
|
|
1387
1522
|
setInitialBody(body) {
|
|
1388
1523
|
if (this.#originalRequestData && Object.isFrozen(this.#originalRequestData)) {
|
|
@@ -1395,6 +1530,8 @@ var Request = class extends Macroable5 {
|
|
|
1395
1530
|
* Update the request body with new data object. The `all` property
|
|
1396
1531
|
* will be re-computed by merging the query string and request
|
|
1397
1532
|
* body.
|
|
1533
|
+
* @param body - New request body data to set
|
|
1534
|
+
* @returns {void}
|
|
1398
1535
|
*/
|
|
1399
1536
|
updateBody(body) {
|
|
1400
1537
|
this.#requestBody = body;
|
|
@@ -1403,6 +1540,8 @@ var Request = class extends Macroable5 {
|
|
|
1403
1540
|
/**
|
|
1404
1541
|
* Update the request raw body. Bodyparser sets this when unable to parse
|
|
1405
1542
|
* the request body or when request is multipart/form-data.
|
|
1543
|
+
* @param rawBody - Raw request body as string
|
|
1544
|
+
* @returns {void}
|
|
1406
1545
|
*/
|
|
1407
1546
|
updateRawBody(rawBody) {
|
|
1408
1547
|
this.#rawRequestBody = rawBody;
|
|
@@ -1410,6 +1549,8 @@ var Request = class extends Macroable5 {
|
|
|
1410
1549
|
/**
|
|
1411
1550
|
* Update the query string with the new data object. The `all` property
|
|
1412
1551
|
* will be re-computed by merging the query and the request body.
|
|
1552
|
+
* @param data - New query string data to set
|
|
1553
|
+
* @returns {void}
|
|
1413
1554
|
*/
|
|
1414
1555
|
updateQs(data) {
|
|
1415
1556
|
this.#requestQs = data;
|
|
@@ -1417,18 +1558,21 @@ var Request = class extends Macroable5 {
|
|
|
1417
1558
|
}
|
|
1418
1559
|
/**
|
|
1419
1560
|
* Returns route params
|
|
1561
|
+
* @returns {Record<string, any>} Object containing route parameters
|
|
1420
1562
|
*/
|
|
1421
1563
|
params() {
|
|
1422
1564
|
return this.ctx?.params || {};
|
|
1423
1565
|
}
|
|
1424
1566
|
/**
|
|
1425
1567
|
* Returns the query string object by reference
|
|
1568
|
+
* @returns {Record<string, any>} Object containing parsed query string parameters
|
|
1426
1569
|
*/
|
|
1427
1570
|
qs() {
|
|
1428
1571
|
return this.#requestQs;
|
|
1429
1572
|
}
|
|
1430
1573
|
/**
|
|
1431
1574
|
* Returns reference to the request body
|
|
1575
|
+
* @returns {Record<string, any>} Object containing parsed request body
|
|
1432
1576
|
*/
|
|
1433
1577
|
body() {
|
|
1434
1578
|
return this.#requestBody;
|
|
@@ -1436,6 +1580,7 @@ var Request = class extends Macroable5 {
|
|
|
1436
1580
|
/**
|
|
1437
1581
|
* Returns reference to the merged copy of request body
|
|
1438
1582
|
* and query string
|
|
1583
|
+
* @returns {Record<string, any>} Object containing merged request body and query parameters
|
|
1439
1584
|
*/
|
|
1440
1585
|
all() {
|
|
1441
1586
|
return this.#requestData;
|
|
@@ -1443,6 +1588,7 @@ var Request = class extends Macroable5 {
|
|
|
1443
1588
|
/**
|
|
1444
1589
|
* Returns reference to the merged copy of original request
|
|
1445
1590
|
* query string and body
|
|
1591
|
+
* @returns {Record<string, any>} Object containing original merged request data
|
|
1446
1592
|
*/
|
|
1447
1593
|
original() {
|
|
1448
1594
|
return this.#originalRequestData;
|
|
@@ -1452,6 +1598,7 @@ var Request = class extends Macroable5 {
|
|
|
1452
1598
|
*
|
|
1453
1599
|
* Ideally you must be dealing with the parsed body accessed using [[input]], [[all]] or
|
|
1454
1600
|
* [[post]] methods. The `raw` body is always a string.
|
|
1601
|
+
* @returns {string | null} Raw request body as string or null if not set
|
|
1455
1602
|
*/
|
|
1456
1603
|
raw() {
|
|
1457
1604
|
return this.#rawRequestBody || null;
|
|
@@ -1467,6 +1614,9 @@ var Request = class extends Macroable5 {
|
|
|
1467
1614
|
* // with default value
|
|
1468
1615
|
* request.input('username', 'virk')
|
|
1469
1616
|
* ```
|
|
1617
|
+
* @param key - Key to lookup in request data
|
|
1618
|
+
* @param defaultValue - Default value when key is not found
|
|
1619
|
+
* @returns Value from request data or default value
|
|
1470
1620
|
*/
|
|
1471
1621
|
input(key, defaultValue) {
|
|
1472
1622
|
return lodash.get(this.#requestData, key, defaultValue);
|
|
@@ -1481,6 +1631,9 @@ var Request = class extends Macroable5 {
|
|
|
1481
1631
|
* // with default value
|
|
1482
1632
|
* request.param('id', 1)
|
|
1483
1633
|
* ```
|
|
1634
|
+
* @param key - Parameter key to lookup
|
|
1635
|
+
* @param defaultValue - Default value when parameter is not found
|
|
1636
|
+
* @returns Value from route parameters or default value
|
|
1484
1637
|
*/
|
|
1485
1638
|
param(key, defaultValue) {
|
|
1486
1639
|
return lodash.get(this.params(), key, defaultValue);
|
|
@@ -1492,6 +1645,8 @@ var Request = class extends Macroable5 {
|
|
|
1492
1645
|
* ```js
|
|
1493
1646
|
* request.except(['_csrf'])
|
|
1494
1647
|
* ```
|
|
1648
|
+
* @param keys - Array of keys to exclude from the result
|
|
1649
|
+
* @returns {Record<string, any>} Object with all request data except specified keys
|
|
1495
1650
|
*/
|
|
1496
1651
|
except(keys) {
|
|
1497
1652
|
return lodash.omit(this.#requestData, keys);
|
|
@@ -1503,6 +1658,8 @@ var Request = class extends Macroable5 {
|
|
|
1503
1658
|
* ```js
|
|
1504
1659
|
* request.only(['username', 'age'])
|
|
1505
1660
|
* ```
|
|
1661
|
+
* @param keys - Array of keys to include in the result
|
|
1662
|
+
* @returns {{ [K in T]: any }} Object with only the specified keys from request data
|
|
1506
1663
|
*/
|
|
1507
1664
|
only(keys) {
|
|
1508
1665
|
return lodash.pick(this.#requestData, keys);
|
|
@@ -1516,6 +1673,7 @@ var Request = class extends Macroable5 {
|
|
|
1516
1673
|
* ```js
|
|
1517
1674
|
* request.intended()
|
|
1518
1675
|
* ```
|
|
1676
|
+
* @returns {string} Original HTTP method from the request
|
|
1519
1677
|
*/
|
|
1520
1678
|
intended() {
|
|
1521
1679
|
return this.request.method;
|
|
@@ -1533,6 +1691,7 @@ var Request = class extends Macroable5 {
|
|
|
1533
1691
|
* ```js
|
|
1534
1692
|
* request.method()
|
|
1535
1693
|
* ```
|
|
1694
|
+
* @returns {string} HTTP method (potentially spoofed)
|
|
1536
1695
|
*/
|
|
1537
1696
|
method() {
|
|
1538
1697
|
if (this.#config.allowMethodSpoofing && this.intended() === "POST") {
|
|
@@ -1542,6 +1701,7 @@ var Request = class extends Macroable5 {
|
|
|
1542
1701
|
}
|
|
1543
1702
|
/**
|
|
1544
1703
|
* Returns a copy of headers as an object
|
|
1704
|
+
* @returns {IncomingHttpHeaders} Object containing all HTTP headers
|
|
1545
1705
|
*/
|
|
1546
1706
|
headers() {
|
|
1547
1707
|
return this.request.headers;
|
|
@@ -1549,6 +1709,9 @@ var Request = class extends Macroable5 {
|
|
|
1549
1709
|
/**
|
|
1550
1710
|
* Returns value for a given header key. The default value is
|
|
1551
1711
|
* used when original value is `undefined`.
|
|
1712
|
+
* @param key - Header name to lookup
|
|
1713
|
+
* @param defaultValue - Default value when header is not found
|
|
1714
|
+
* @returns {string | undefined} Header value or default value if not found
|
|
1552
1715
|
*/
|
|
1553
1716
|
header(key, defaultValue) {
|
|
1554
1717
|
key = key.toLowerCase();
|
|
@@ -1591,6 +1754,7 @@ var Request = class extends Macroable5 {
|
|
|
1591
1754
|
* ```
|
|
1592
1755
|
*
|
|
1593
1756
|
* The value of trustProxy is passed directly to [proxy-addr](https://www.npmjs.com/package/proxy-addr)
|
|
1757
|
+
* @returns {string} Client IP address
|
|
1594
1758
|
*/
|
|
1595
1759
|
ip() {
|
|
1596
1760
|
const ipFn = this.#config.getIp;
|
|
@@ -1616,6 +1780,7 @@ var Request = class extends Macroable5 {
|
|
|
1616
1780
|
* ```
|
|
1617
1781
|
*
|
|
1618
1782
|
* The value of trustProxy is passed directly to [proxy-addr](https://www.npmjs.com/package/proxy-addr)
|
|
1783
|
+
* @returns {string[]} Array of IP addresses from most to least trusted
|
|
1619
1784
|
*/
|
|
1620
1785
|
ips() {
|
|
1621
1786
|
return proxyaddr.all(this.request, this.#config.trustProxy);
|
|
@@ -1639,6 +1804,7 @@ var Request = class extends Macroable5 {
|
|
|
1639
1804
|
* ```
|
|
1640
1805
|
*
|
|
1641
1806
|
* The value of trustProxy is passed directly to [proxy-addr](https://www.npmjs.com/package/proxy-addr)
|
|
1807
|
+
* @returns {string} Request protocol ('http' or 'https')
|
|
1642
1808
|
*/
|
|
1643
1809
|
protocol() {
|
|
1644
1810
|
if ("encrypted" in this.request.socket) {
|
|
@@ -1654,6 +1820,7 @@ var Request = class extends Macroable5 {
|
|
|
1654
1820
|
* Returns a boolean telling if request is served over `https`
|
|
1655
1821
|
* or not. Check [[protocol]] method to know how protocol is
|
|
1656
1822
|
* fetched.
|
|
1823
|
+
* @returns {boolean} True if request is served over HTTPS
|
|
1657
1824
|
*/
|
|
1658
1825
|
secure() {
|
|
1659
1826
|
return this.protocol() === "https";
|
|
@@ -1674,6 +1841,7 @@ var Request = class extends Macroable5 {
|
|
|
1674
1841
|
* ```
|
|
1675
1842
|
*
|
|
1676
1843
|
* The value of trustProxy is passed directly to [proxy-addr](https://www.npmjs.com/package/proxy-addr)
|
|
1844
|
+
* @returns {string | null} Request host or null if not found
|
|
1677
1845
|
*/
|
|
1678
1846
|
host() {
|
|
1679
1847
|
let host = this.header("host");
|
|
@@ -1701,6 +1869,7 @@ var Request = class extends Macroable5 {
|
|
|
1701
1869
|
* ```
|
|
1702
1870
|
*
|
|
1703
1871
|
* The value of trustProxy is passed directly to [proxy-addr](https://www.npmjs.com/package/proxy-addr)
|
|
1872
|
+
* @returns {string | null} Request hostname (without port) or null if not found
|
|
1704
1873
|
*/
|
|
1705
1874
|
hostname() {
|
|
1706
1875
|
const host = this.host();
|
|
@@ -1716,6 +1885,7 @@ var Request = class extends Macroable5 {
|
|
|
1716
1885
|
* returned if [[hostname]] is `null` or is an IP address.
|
|
1717
1886
|
*
|
|
1718
1887
|
* Also `www` is not considered as a subdomain
|
|
1888
|
+
* @returns {string[]} Array of subdomains (excluding www)
|
|
1719
1889
|
*/
|
|
1720
1890
|
subdomains() {
|
|
1721
1891
|
const hostname = this.hostname();
|
|
@@ -1732,6 +1902,7 @@ var Request = class extends Macroable5 {
|
|
|
1732
1902
|
/**
|
|
1733
1903
|
* Returns a boolean telling, if request `X-Requested-With === 'xmlhttprequest'`
|
|
1734
1904
|
* or not.
|
|
1905
|
+
* @returns {boolean} True if request is an AJAX request
|
|
1735
1906
|
*/
|
|
1736
1907
|
ajax() {
|
|
1737
1908
|
const xRequestedWith = this.header("X-Requested-With", "");
|
|
@@ -1740,6 +1911,7 @@ var Request = class extends Macroable5 {
|
|
|
1740
1911
|
/**
|
|
1741
1912
|
* Returns a boolean telling, if request has `X-Pjax` header
|
|
1742
1913
|
* set or not
|
|
1914
|
+
* @returns {boolean} True if request is a PJAX request
|
|
1743
1915
|
*/
|
|
1744
1916
|
pjax() {
|
|
1745
1917
|
return !!this.header("X-Pjax");
|
|
@@ -1754,6 +1926,8 @@ var Request = class extends Macroable5 {
|
|
|
1754
1926
|
* // include query string
|
|
1755
1927
|
* request.url(true)
|
|
1756
1928
|
* ```
|
|
1929
|
+
* @param includeQueryString - Whether to include query string in the URL
|
|
1930
|
+
* @returns {string} Request pathname, optionally with query string
|
|
1757
1931
|
*/
|
|
1758
1932
|
url(includeQueryString) {
|
|
1759
1933
|
const pathname = this.parsedUrl.pathname;
|
|
@@ -1770,6 +1944,8 @@ var Request = class extends Macroable5 {
|
|
|
1770
1944
|
* // include query string
|
|
1771
1945
|
* request.completeUrl(true)
|
|
1772
1946
|
* ```
|
|
1947
|
+
* @param includeQueryString - Whether to include query string in the URL
|
|
1948
|
+
* @returns {string} Complete URL including protocol and host
|
|
1773
1949
|
*/
|
|
1774
1950
|
completeUrl(includeQueryString) {
|
|
1775
1951
|
const protocol = this.protocol();
|
|
@@ -1778,6 +1954,8 @@ var Request = class extends Macroable5 {
|
|
|
1778
1954
|
}
|
|
1779
1955
|
/**
|
|
1780
1956
|
* Find if the current HTTP request is for the given route or the routes
|
|
1957
|
+
* @param routeIdentifier - Route name, pattern, or handler reference to match
|
|
1958
|
+
* @returns {boolean} True if the request matches any of the given route identifiers
|
|
1781
1959
|
*/
|
|
1782
1960
|
matchesRoute(routeIdentifier) {
|
|
1783
1961
|
if (!this.ctx || !this.ctx.route) {
|
|
@@ -1823,6 +2001,8 @@ var Request = class extends Macroable5 {
|
|
|
1823
2001
|
* // process XML
|
|
1824
2002
|
* }
|
|
1825
2003
|
* ```
|
|
2004
|
+
* @param types - Array of content types to match against
|
|
2005
|
+
* @returns {string | null} Best matching content type or null if no match
|
|
1826
2006
|
*/
|
|
1827
2007
|
is(types) {
|
|
1828
2008
|
return typeIs(this.request, types) || null;
|
|
@@ -1847,6 +2027,8 @@ var Request = class extends Macroable5 {
|
|
|
1847
2027
|
* // decide yourself
|
|
1848
2028
|
* }
|
|
1849
2029
|
* ```
|
|
2030
|
+
* @param types - Array of types to match against Accept header
|
|
2031
|
+
* @returns {T | null} Best matching accept type or null if no match
|
|
1850
2032
|
*/
|
|
1851
2033
|
accepts(types) {
|
|
1852
2034
|
this.#initiateAccepts();
|
|
@@ -1858,6 +2040,7 @@ var Request = class extends Macroable5 {
|
|
|
1858
2040
|
*
|
|
1859
2041
|
* Make sure to check [accepts](https://www.npmjs.com/package/accepts) package
|
|
1860
2042
|
* docs too.
|
|
2043
|
+
* @returns {string[]} Array of accepted types in preference order
|
|
1861
2044
|
*/
|
|
1862
2045
|
types() {
|
|
1863
2046
|
this.#initiateAccepts();
|
|
@@ -1883,6 +2066,8 @@ var Request = class extends Macroable5 {
|
|
|
1883
2066
|
* return view.render('about', { lang: 'en' })
|
|
1884
2067
|
* }
|
|
1885
2068
|
* ```
|
|
2069
|
+
* @param languages - Array of languages to match against Accept-Language header
|
|
2070
|
+
* @returns {T | null} Best matching language or null if no match
|
|
1886
2071
|
*/
|
|
1887
2072
|
language(languages) {
|
|
1888
2073
|
this.#initiateAccepts();
|
|
@@ -1894,6 +2079,7 @@ var Request = class extends Macroable5 {
|
|
|
1894
2079
|
*
|
|
1895
2080
|
* Make sure to check [accepts](https://www.npmjs.com/package/accepts) package
|
|
1896
2081
|
* docs too.
|
|
2082
|
+
* @returns {string[]} Array of accepted languages in preference order
|
|
1897
2083
|
*/
|
|
1898
2084
|
languages() {
|
|
1899
2085
|
this.#initiateAccepts();
|
|
@@ -1917,6 +2103,8 @@ var Request = class extends Macroable5 {
|
|
|
1917
2103
|
* // make ISO-8859-1 friendly response
|
|
1918
2104
|
* }
|
|
1919
2105
|
* ```
|
|
2106
|
+
* @param charsets - Array of charsets to match against Accept-Charset header
|
|
2107
|
+
* @returns {T | null} Best matching charset or null if no match
|
|
1920
2108
|
*/
|
|
1921
2109
|
charset(charsets) {
|
|
1922
2110
|
this.#initiateAccepts();
|
|
@@ -1928,6 +2116,7 @@ var Request = class extends Macroable5 {
|
|
|
1928
2116
|
*
|
|
1929
2117
|
* Make sure to check [accepts](https://www.npmjs.com/package/accepts) package
|
|
1930
2118
|
* docs too.
|
|
2119
|
+
* @returns {string[]} Array of accepted charsets in preference order
|
|
1931
2120
|
*/
|
|
1932
2121
|
charsets() {
|
|
1933
2122
|
this.#initiateAccepts();
|
|
@@ -1941,17 +2130,20 @@ var Request = class extends Macroable5 {
|
|
|
1941
2130
|
*
|
|
1942
2131
|
* Make sure to check [accepts](https://www.npmjs.com/package/accepts) package
|
|
1943
2132
|
* docs too.
|
|
2133
|
+
* @param encodings - Array of encodings to match against Accept-Encoding header
|
|
2134
|
+
* @returns {T | null} Best matching encoding or null if no match
|
|
1944
2135
|
*/
|
|
1945
2136
|
encoding(encodings) {
|
|
1946
2137
|
this.#initiateAccepts();
|
|
1947
2138
|
return this.#lazyAccepts.encoding(encodings) || null;
|
|
1948
2139
|
}
|
|
1949
2140
|
/**
|
|
1950
|
-
* Return the
|
|
2141
|
+
* Return the encodings that the request accepts, in the order of the
|
|
1951
2142
|
* client's preference (most preferred first).
|
|
1952
2143
|
*
|
|
1953
2144
|
* Make sure to check [accepts](https://www.npmjs.com/package/accepts) package
|
|
1954
2145
|
* docs too.
|
|
2146
|
+
* @returns {string[]} Array of accepted encodings in preference order
|
|
1955
2147
|
*/
|
|
1956
2148
|
encodings() {
|
|
1957
2149
|
this.#initiateAccepts();
|
|
@@ -1959,6 +2151,7 @@ var Request = class extends Macroable5 {
|
|
|
1959
2151
|
}
|
|
1960
2152
|
/**
|
|
1961
2153
|
* Returns a boolean telling if request has body
|
|
2154
|
+
* @returns {boolean} True if request contains a body
|
|
1962
2155
|
*/
|
|
1963
2156
|
hasBody() {
|
|
1964
2157
|
return typeIs.hasBody(this.request);
|
|
@@ -1986,6 +2179,7 @@ var Request = class extends Macroable5 {
|
|
|
1986
2179
|
* response.send(responseBody)
|
|
1987
2180
|
* }
|
|
1988
2181
|
* ```
|
|
2182
|
+
* @returns {boolean} True if client cache is fresh (should return 304)
|
|
1989
2183
|
*/
|
|
1990
2184
|
fresh() {
|
|
1991
2185
|
if (["GET", "HEAD"].indexOf(this.intended()) === -1) {
|
|
@@ -1999,6 +2193,7 @@ var Request = class extends Macroable5 {
|
|
|
1999
2193
|
}
|
|
2000
2194
|
/**
|
|
2001
2195
|
* Opposite of [[fresh]]
|
|
2196
|
+
* @returns {boolean} True if client cache is stale (should send new response)
|
|
2002
2197
|
*/
|
|
2003
2198
|
stale() {
|
|
2004
2199
|
return !this.fresh();
|
|
@@ -2006,6 +2201,7 @@ var Request = class extends Macroable5 {
|
|
|
2006
2201
|
/**
|
|
2007
2202
|
* Returns all parsed and signed cookies. Signed cookies ensures
|
|
2008
2203
|
* that their value isn't tampered.
|
|
2204
|
+
* @returns {{ [key: string]: any }} Object containing all parsed cookies
|
|
2009
2205
|
*/
|
|
2010
2206
|
cookiesList() {
|
|
2011
2207
|
this.#initiateCookieParser();
|
|
@@ -2014,14 +2210,20 @@ var Request = class extends Macroable5 {
|
|
|
2014
2210
|
/**
|
|
2015
2211
|
* Returns value for a given key from signed cookies. Optional
|
|
2016
2212
|
* defaultValue is returned when actual value is undefined.
|
|
2213
|
+
* @param key - Cookie name to lookup
|
|
2214
|
+
* @param defaultValue - Default value when cookie is not found
|
|
2215
|
+
* @returns Cookie value or default value if not found
|
|
2017
2216
|
*/
|
|
2018
2217
|
cookie(key, defaultValue) {
|
|
2019
2218
|
this.#initiateCookieParser();
|
|
2020
2219
|
return this.#cookieParser.unsign(key) || defaultValue;
|
|
2021
2220
|
}
|
|
2022
2221
|
/**
|
|
2023
|
-
* Returns value for a given key from
|
|
2222
|
+
* Returns value for a given key from encrypted cookies. Optional
|
|
2024
2223
|
* defaultValue is returned when actual value is undefined.
|
|
2224
|
+
* @param key - Cookie name to lookup
|
|
2225
|
+
* @param defaultValue - Default value when cookie is not found
|
|
2226
|
+
* @returns Decrypted cookie value or default value if not found
|
|
2025
2227
|
*/
|
|
2026
2228
|
encryptedCookie(key, defaultValue) {
|
|
2027
2229
|
this.#initiateCookieParser();
|
|
@@ -2035,8 +2237,10 @@ var Request = class extends Macroable5 {
|
|
|
2035
2237
|
return this.#cookieParser.decode(key, encoded) || defaultValueOrOptions;
|
|
2036
2238
|
}
|
|
2037
2239
|
/**
|
|
2038
|
-
* Returns a boolean telling if a signed url
|
|
2240
|
+
* Returns a boolean telling if a signed url has a valid signature
|
|
2039
2241
|
* or not.
|
|
2242
|
+
* @param purpose - Optional purpose for signature verification
|
|
2243
|
+
* @returns {boolean} True if the signed URL has a valid signature
|
|
2040
2244
|
*/
|
|
2041
2245
|
hasValidSignature(purpose) {
|
|
2042
2246
|
const { signature, ...rest } = this.qs();
|
|
@@ -2052,6 +2256,7 @@ var Request = class extends Macroable5 {
|
|
|
2052
2256
|
}
|
|
2053
2257
|
/**
|
|
2054
2258
|
* Serializes request to JSON format
|
|
2259
|
+
* @returns Object representation of the request
|
|
2055
2260
|
*/
|
|
2056
2261
|
serialize() {
|
|
2057
2262
|
return {
|
|
@@ -2071,6 +2276,7 @@ var Request = class extends Macroable5 {
|
|
|
2071
2276
|
}
|
|
2072
2277
|
/**
|
|
2073
2278
|
* toJSON copy of the request
|
|
2279
|
+
* @returns JSON representation of the request
|
|
2074
2280
|
*/
|
|
2075
2281
|
toJSON() {
|
|
2076
2282
|
return this.serialize();
|
|
@@ -2081,21 +2287,40 @@ var Request = class extends Macroable5 {
|
|
|
2081
2287
|
import { parse } from "url";
|
|
2082
2288
|
var Redirect = class {
|
|
2083
2289
|
/**
|
|
2084
|
-
*
|
|
2290
|
+
* Flag indicating whether to forward the existing query string from the current request
|
|
2085
2291
|
*/
|
|
2086
2292
|
#forwardQueryString = false;
|
|
2087
2293
|
/**
|
|
2088
|
-
*
|
|
2294
|
+
* HTTP status code to use for the redirect response (defaults to 302)
|
|
2089
2295
|
*/
|
|
2090
2296
|
#statusCode = 302;
|
|
2091
2297
|
/**
|
|
2092
|
-
*
|
|
2298
|
+
* Custom query string parameters to include in the redirect URL
|
|
2093
2299
|
*/
|
|
2094
2300
|
#queryString = {};
|
|
2301
|
+
/**
|
|
2302
|
+
* Reference to the Node.js incoming HTTP request
|
|
2303
|
+
*/
|
|
2095
2304
|
#request;
|
|
2305
|
+
/**
|
|
2306
|
+
* Reference to the AdonisJS response instance
|
|
2307
|
+
*/
|
|
2096
2308
|
#response;
|
|
2309
|
+
/**
|
|
2310
|
+
* Reference to the AdonisJS router instance for URL building
|
|
2311
|
+
*/
|
|
2097
2312
|
#router;
|
|
2313
|
+
/**
|
|
2314
|
+
* Query string parser instance
|
|
2315
|
+
*/
|
|
2098
2316
|
#qs;
|
|
2317
|
+
/**
|
|
2318
|
+
* Creates a new Redirect instance for handling HTTP redirects
|
|
2319
|
+
* @param request - Node.js incoming HTTP request
|
|
2320
|
+
* @param response - AdonisJS response instance
|
|
2321
|
+
* @param router - AdonisJS router instance
|
|
2322
|
+
* @param qs - Query string parser instance
|
|
2323
|
+
*/
|
|
2099
2324
|
constructor(request, response, router, qs) {
|
|
2100
2325
|
this.#request = request;
|
|
2101
2326
|
this.#response = response;
|
|
@@ -2103,7 +2328,9 @@ var Redirect = class {
|
|
|
2103
2328
|
this.#qs = qs;
|
|
2104
2329
|
}
|
|
2105
2330
|
/**
|
|
2106
|
-
* Sends response by setting
|
|
2331
|
+
* Sends the redirect response by setting required headers and status code
|
|
2332
|
+
* @param url - Target URL for redirection
|
|
2333
|
+
* @param query - Query string parameters to append
|
|
2107
2334
|
*/
|
|
2108
2335
|
#sendResponse(url, query) {
|
|
2109
2336
|
const stringified = this.#qs.stringify(query);
|
|
@@ -2115,22 +2342,25 @@ var Redirect = class {
|
|
|
2115
2342
|
this.#response.send(`Redirecting to ${url}`);
|
|
2116
2343
|
}
|
|
2117
2344
|
/**
|
|
2118
|
-
*
|
|
2345
|
+
* Extracts and returns the referrer URL from request headers
|
|
2346
|
+
* @returns {string} The referrer URL or '/' if not found
|
|
2119
2347
|
*/
|
|
2120
2348
|
#getReferrerUrl() {
|
|
2121
2349
|
let url = this.#request.headers["referer"] || this.#request.headers["referrer"] || "/";
|
|
2122
2350
|
return Array.isArray(url) ? url[0] : url;
|
|
2123
2351
|
}
|
|
2124
2352
|
/**
|
|
2125
|
-
*
|
|
2353
|
+
* Sets a custom HTTP status code for the redirect response
|
|
2354
|
+
* @param statusCode - HTTP status code to use (e.g., 301, 302, 307)
|
|
2355
|
+
* @returns {this} The Redirect instance for method chaining
|
|
2126
2356
|
*/
|
|
2127
2357
|
status(statusCode) {
|
|
2128
2358
|
this.#statusCode = statusCode;
|
|
2129
2359
|
return this;
|
|
2130
2360
|
}
|
|
2131
2361
|
/**
|
|
2132
|
-
*
|
|
2133
|
-
*
|
|
2362
|
+
* Clears any query string values previously added using the withQs method
|
|
2363
|
+
* @returns {this} The Redirect instance for method chaining
|
|
2134
2364
|
*/
|
|
2135
2365
|
clearQs() {
|
|
2136
2366
|
this.#forwardQueryString = false;
|
|
@@ -2150,7 +2380,8 @@ var Redirect = class {
|
|
|
2150
2380
|
return this;
|
|
2151
2381
|
}
|
|
2152
2382
|
/**
|
|
2153
|
-
*
|
|
2383
|
+
* Redirects to the previous path using the Referer header
|
|
2384
|
+
* Falls back to '/' if no referrer is found
|
|
2154
2385
|
*/
|
|
2155
2386
|
back() {
|
|
2156
2387
|
let query = {};
|
|
@@ -2165,7 +2396,8 @@ var Redirect = class {
|
|
|
2165
2396
|
this.#sendResponse(url.pathname || "", query);
|
|
2166
2397
|
}
|
|
2167
2398
|
/**
|
|
2168
|
-
*
|
|
2399
|
+
* Redirects to a route using its identifier (name, pattern, or handler reference)
|
|
2400
|
+
* @param args - Route identifier, parameters, and options for URL building
|
|
2169
2401
|
*/
|
|
2170
2402
|
toRoute(...args) {
|
|
2171
2403
|
const [identifier, params, options] = args;
|
|
@@ -2177,7 +2409,8 @@ var Redirect = class {
|
|
|
2177
2409
|
return this.toPath(url);
|
|
2178
2410
|
}
|
|
2179
2411
|
/**
|
|
2180
|
-
*
|
|
2412
|
+
* Redirects to a specific URL path
|
|
2413
|
+
* @param url - Target URL path for redirection
|
|
2181
2414
|
*/
|
|
2182
2415
|
toPath(url) {
|
|
2183
2416
|
let query = {};
|
|
@@ -2255,24 +2488,17 @@ var ResponseStatus = {
|
|
|
2255
2488
|
NetworkAuthenticationRequired: 511
|
|
2256
2489
|
};
|
|
2257
2490
|
|
|
2258
|
-
// src/response.ts
|
|
2259
|
-
import etag from "etag";
|
|
2260
|
-
import vary from "vary";
|
|
2261
|
-
import fresh2 from "fresh";
|
|
2262
|
-
import destroy from "destroy";
|
|
2263
|
-
import { extname } from "path";
|
|
2264
|
-
import { Buffer } from "buffer";
|
|
2265
|
-
import onFinished from "on-finished";
|
|
2266
|
-
import { stat } from "fs/promises";
|
|
2267
|
-
import Macroable6 from "@poppinss/macroable";
|
|
2268
|
-
import { createReadStream } from "fs";
|
|
2269
|
-
import contentDisposition from "content-disposition";
|
|
2270
|
-
import { safeStringify } from "@poppinss/utils/json";
|
|
2271
|
-
import { RuntimeException as RuntimeException3 } from "@poppinss/utils/exception";
|
|
2272
|
-
|
|
2273
2491
|
// src/cookies/serializer.ts
|
|
2274
2492
|
var CookieSerializer = class {
|
|
2493
|
+
/**
|
|
2494
|
+
* Cookie client instance for handling cookie operations
|
|
2495
|
+
*/
|
|
2275
2496
|
#client;
|
|
2497
|
+
/**
|
|
2498
|
+
* Create a new instance of CookieSerializer
|
|
2499
|
+
*
|
|
2500
|
+
* @param encryption - The encryption instance for cookie operations
|
|
2501
|
+
*/
|
|
2276
2502
|
constructor(encryption) {
|
|
2277
2503
|
this.#client = new CookieClient(encryption);
|
|
2278
2504
|
}
|
|
@@ -2286,6 +2512,11 @@ var CookieSerializer = class {
|
|
|
2286
2512
|
* serializer.encode('name', 'virk')
|
|
2287
2513
|
* serializer.encode('name', 'virk', { stringify: false })
|
|
2288
2514
|
* ```
|
|
2515
|
+
*
|
|
2516
|
+
* @param key - The cookie key
|
|
2517
|
+
* @param value - The value to encode
|
|
2518
|
+
* @param options - Cookie encoding options
|
|
2519
|
+
* @returns The serialized cookie string or null if encoding fails
|
|
2289
2520
|
*/
|
|
2290
2521
|
encode(key, value, options) {
|
|
2291
2522
|
const stringify2 = options?.stringify ?? options?.encode;
|
|
@@ -2298,6 +2529,11 @@ var CookieSerializer = class {
|
|
|
2298
2529
|
/**
|
|
2299
2530
|
* Sign a key-value pair to a signed cookie. The signed value has a
|
|
2300
2531
|
* verification hash attached to it to detect data tampering.
|
|
2532
|
+
*
|
|
2533
|
+
* @param key - The cookie key
|
|
2534
|
+
* @param value - The value to sign
|
|
2535
|
+
* @param options - Cookie options
|
|
2536
|
+
* @returns The serialized signed cookie string or null if signing fails
|
|
2301
2537
|
*/
|
|
2302
2538
|
sign(key, value, options) {
|
|
2303
2539
|
const packedValue = this.#client.sign(key, value);
|
|
@@ -2308,6 +2544,11 @@ var CookieSerializer = class {
|
|
|
2308
2544
|
}
|
|
2309
2545
|
/**
|
|
2310
2546
|
* Encrypts a key-value pair to an encrypted cookie.
|
|
2547
|
+
*
|
|
2548
|
+
* @param key - The cookie key
|
|
2549
|
+
* @param value - The value to encrypt
|
|
2550
|
+
* @param options - Cookie options
|
|
2551
|
+
* @returns The serialized encrypted cookie string or null if encryption fails
|
|
2311
2552
|
*/
|
|
2312
2553
|
encrypt(key, value, options) {
|
|
2313
2554
|
const packedValue = this.#client.encrypt(key, value);
|
|
@@ -2319,8 +2560,31 @@ var CookieSerializer = class {
|
|
|
2319
2560
|
};
|
|
2320
2561
|
|
|
2321
2562
|
// src/response.ts
|
|
2563
|
+
import etag from "etag";
|
|
2564
|
+
import vary from "vary";
|
|
2565
|
+
import fresh2 from "fresh";
|
|
2566
|
+
import destroy from "destroy";
|
|
2567
|
+
import { extname } from "path";
|
|
2568
|
+
import { Buffer } from "buffer";
|
|
2569
|
+
import onFinished from "on-finished";
|
|
2570
|
+
import { stat } from "fs/promises";
|
|
2571
|
+
import Macroable6 from "@poppinss/macroable";
|
|
2572
|
+
import { createReadStream } from "fs";
|
|
2573
|
+
import contentDisposition from "content-disposition";
|
|
2574
|
+
import { safeStringify } from "@poppinss/utils/json";
|
|
2575
|
+
import { RuntimeException as RuntimeException3 } from "@poppinss/utils/exception";
|
|
2322
2576
|
var CACHEABLE_HTTP_METHODS = ["GET", "HEAD"];
|
|
2323
2577
|
var Response = class extends Macroable6 {
|
|
2578
|
+
/**
|
|
2579
|
+
* Creates a new Response instance
|
|
2580
|
+
*
|
|
2581
|
+
* @param request - Node.js IncomingMessage instance
|
|
2582
|
+
* @param response - Node.js ServerResponse instance
|
|
2583
|
+
* @param encryption - Encryption service for cookie handling
|
|
2584
|
+
* @param config - Response configuration settings
|
|
2585
|
+
* @param router - Router instance for URL generation
|
|
2586
|
+
* @param qs - Query string parser
|
|
2587
|
+
*/
|
|
2324
2588
|
constructor(request, response, encryption, config, router, qs) {
|
|
2325
2589
|
super();
|
|
2326
2590
|
this.request = request;
|
|
@@ -2331,73 +2595,67 @@ var Response = class extends Macroable6 {
|
|
|
2331
2595
|
this.#cookieSerializer = new CookieSerializer(encryption);
|
|
2332
2596
|
}
|
|
2333
2597
|
/**
|
|
2334
|
-
* Query string parser
|
|
2598
|
+
* Query string parser instance used for URL manipulation
|
|
2335
2599
|
*/
|
|
2336
2600
|
#qs;
|
|
2337
2601
|
/**
|
|
2338
|
-
*
|
|
2602
|
+
* Collection of outgoing HTTP headers to be sent with the response
|
|
2339
2603
|
*/
|
|
2340
2604
|
#headers = {};
|
|
2341
2605
|
/**
|
|
2342
|
-
*
|
|
2606
|
+
* Flag indicating whether an explicit status code has been set
|
|
2343
2607
|
*/
|
|
2344
2608
|
#hasExplicitStatus = false;
|
|
2345
2609
|
/**
|
|
2346
|
-
*
|
|
2610
|
+
* Cookie serializer instance for handling cookie encryption, signing, and encoding
|
|
2347
2611
|
*/
|
|
2348
2612
|
#cookieSerializer;
|
|
2349
2613
|
/**
|
|
2350
|
-
* Router
|
|
2614
|
+
* Router instance used for generating redirect URLs from route definitions
|
|
2351
2615
|
*/
|
|
2352
2616
|
#router;
|
|
2353
2617
|
/**
|
|
2354
|
-
*
|
|
2618
|
+
* Configuration object containing response-related settings
|
|
2355
2619
|
*/
|
|
2356
2620
|
#config;
|
|
2357
2621
|
/**
|
|
2358
|
-
*
|
|
2359
|
-
* response socket at the end of the request
|
|
2622
|
+
* Indicates whether the response has any content (body, stream, or file) ready to be sent
|
|
2360
2623
|
*/
|
|
2361
2624
|
get hasLazyBody() {
|
|
2362
2625
|
return !!(this.lazyBody.content || this.lazyBody.fileToStream || this.lazyBody.stream);
|
|
2363
2626
|
}
|
|
2364
2627
|
/**
|
|
2365
|
-
*
|
|
2628
|
+
* Indicates whether the response has non-stream content set
|
|
2366
2629
|
*/
|
|
2367
2630
|
get hasContent() {
|
|
2368
2631
|
return !!this.lazyBody.content;
|
|
2369
2632
|
}
|
|
2370
2633
|
/**
|
|
2371
|
-
*
|
|
2372
|
-
* method
|
|
2634
|
+
* Indicates whether the response body is set as a readable stream
|
|
2373
2635
|
*/
|
|
2374
2636
|
get hasStream() {
|
|
2375
2637
|
return !!this.lazyBody.stream;
|
|
2376
2638
|
}
|
|
2377
2639
|
/**
|
|
2378
|
-
*
|
|
2379
|
-
* or "response.attachment" methods
|
|
2640
|
+
* Indicates whether the response is configured to stream a file
|
|
2380
2641
|
*/
|
|
2381
2642
|
get hasFileToStream() {
|
|
2382
2643
|
return !!this.lazyBody.fileToStream;
|
|
2383
2644
|
}
|
|
2384
2645
|
/**
|
|
2385
|
-
*
|
|
2386
|
-
* has content using the "hasContent" method
|
|
2646
|
+
* The response content data
|
|
2387
2647
|
*/
|
|
2388
2648
|
get content() {
|
|
2389
2649
|
return this.lazyBody.content;
|
|
2390
2650
|
}
|
|
2391
2651
|
/**
|
|
2392
|
-
*
|
|
2393
|
-
* method
|
|
2652
|
+
* The readable stream instance configured for the response
|
|
2394
2653
|
*/
|
|
2395
2654
|
get outgoingStream() {
|
|
2396
2655
|
return this.lazyBody.stream?.[0];
|
|
2397
2656
|
}
|
|
2398
2657
|
/**
|
|
2399
|
-
*
|
|
2400
|
-
* method.
|
|
2658
|
+
* Configuration for file streaming including path and etag generation flag
|
|
2401
2659
|
*/
|
|
2402
2660
|
get fileToStream() {
|
|
2403
2661
|
return this.lazyBody.fileToStream ? {
|
|
@@ -2406,48 +2664,46 @@ var Response = class extends Macroable6 {
|
|
|
2406
2664
|
} : void 0;
|
|
2407
2665
|
}
|
|
2408
2666
|
/**
|
|
2409
|
-
* Lazy body
|
|
2410
|
-
*
|
|
2411
|
-
* is called.
|
|
2667
|
+
* Lazy body container that holds response content until ready to send.
|
|
2668
|
+
* Contains different types of response data: content, stream, or fileToStream.
|
|
2412
2669
|
*/
|
|
2413
2670
|
lazyBody = {};
|
|
2414
2671
|
/**
|
|
2415
|
-
*
|
|
2416
|
-
* reference
|
|
2672
|
+
* HTTP context reference (creates circular dependency with HttpContext)
|
|
2417
2673
|
*/
|
|
2418
2674
|
ctx;
|
|
2419
2675
|
/**
|
|
2420
|
-
*
|
|
2421
|
-
* Any more attempts to update headers or body will result
|
|
2422
|
-
* in raised exceptions.
|
|
2676
|
+
* Indicates whether the response has been completely sent
|
|
2423
2677
|
*/
|
|
2424
2678
|
get finished() {
|
|
2425
2679
|
return this.response.writableFinished;
|
|
2426
2680
|
}
|
|
2427
2681
|
/**
|
|
2428
|
-
*
|
|
2429
|
-
* Any more attempts to update headers will result in raised
|
|
2430
|
-
* exceptions.
|
|
2682
|
+
* Indicates whether response headers have been sent to the client
|
|
2431
2683
|
*/
|
|
2432
2684
|
get headersSent() {
|
|
2433
2685
|
return this.response.headersSent;
|
|
2434
2686
|
}
|
|
2435
2687
|
/**
|
|
2436
|
-
*
|
|
2437
|
-
* or not. When value is `true`, you can feel free to write headers
|
|
2438
|
-
* and body.
|
|
2688
|
+
* Indicates whether the response is still pending (headers and body can still be modified)
|
|
2439
2689
|
*/
|
|
2440
2690
|
get isPending() {
|
|
2441
2691
|
return !this.headersSent && !this.finished;
|
|
2442
2692
|
}
|
|
2443
2693
|
/**
|
|
2444
|
-
* Normalizes header value to a string or an array of
|
|
2694
|
+
* Normalizes header value to a string or an array of strings
|
|
2695
|
+
*
|
|
2696
|
+
* @param value - The header value to normalize
|
|
2697
|
+
* @returns Normalized header value
|
|
2445
2698
|
*/
|
|
2446
2699
|
#castHeaderValue(value) {
|
|
2447
2700
|
return Array.isArray(value) ? value.map(String) : String(value);
|
|
2448
2701
|
}
|
|
2449
2702
|
/**
|
|
2450
2703
|
* Ends the response by flushing headers and writing body
|
|
2704
|
+
*
|
|
2705
|
+
* @param body - Optional response body
|
|
2706
|
+
* @param statusCode - Optional status code
|
|
2451
2707
|
*/
|
|
2452
2708
|
#endResponse(body, statusCode) {
|
|
2453
2709
|
this.writeHead(statusCode);
|
|
@@ -2455,14 +2711,18 @@ var Response = class extends Macroable6 {
|
|
|
2455
2711
|
res.end(body, null, null);
|
|
2456
2712
|
}
|
|
2457
2713
|
/**
|
|
2458
|
-
*
|
|
2714
|
+
* Determines the data type of the content for serialization
|
|
2459
2715
|
*
|
|
2716
|
+
* Supported types:
|
|
2460
2717
|
* - Dates
|
|
2461
2718
|
* - Arrays
|
|
2462
2719
|
* - Booleans
|
|
2463
2720
|
* - Objects
|
|
2464
2721
|
* - Strings
|
|
2465
2722
|
* - Buffer
|
|
2723
|
+
*
|
|
2724
|
+
* @param content - The content to analyze
|
|
2725
|
+
* @returns The determined data type as string
|
|
2466
2726
|
*/
|
|
2467
2727
|
#getDataType(content) {
|
|
2468
2728
|
const dataType = typeof content;
|
|
@@ -2484,10 +2744,17 @@ var Response = class extends Macroable6 {
|
|
|
2484
2744
|
throw new RuntimeException3(`Cannot serialize "${dataType}" to HTTP response`);
|
|
2485
2745
|
}
|
|
2486
2746
|
/**
|
|
2487
|
-
* Writes the body with appropriate
|
|
2488
|
-
*
|
|
2747
|
+
* Writes the response body with appropriate headers and content type detection
|
|
2748
|
+
*
|
|
2749
|
+
* Automatically sets:
|
|
2750
|
+
* - Content-Type based on content analysis
|
|
2751
|
+
* - Content-Length header
|
|
2752
|
+
* - ETag header (if enabled)
|
|
2753
|
+
* - Status code 204 for empty bodies
|
|
2489
2754
|
*
|
|
2490
|
-
*
|
|
2755
|
+
* @param content - The response content
|
|
2756
|
+
* @param generateEtag - Whether to generate ETag header
|
|
2757
|
+
* @param jsonpCallbackName - Optional JSONP callback name
|
|
2491
2758
|
*/
|
|
2492
2759
|
writeBody(content, generateEtag, jsonpCallbackName) {
|
|
2493
2760
|
const hasEmptyBody = content === null || content === void 0 || content === "";
|
|
@@ -2557,7 +2824,16 @@ var Response = class extends Macroable6 {
|
|
|
2557
2824
|
this.#endResponse(content);
|
|
2558
2825
|
}
|
|
2559
2826
|
/**
|
|
2560
|
-
*
|
|
2827
|
+
* Streams the response body and handles error cleanup
|
|
2828
|
+
*
|
|
2829
|
+
* Manages stream lifecycle including:
|
|
2830
|
+
* - Error handling with custom callbacks
|
|
2831
|
+
* - Proper stream cleanup to prevent memory leaks
|
|
2832
|
+
* - Response finalization
|
|
2833
|
+
*
|
|
2834
|
+
* @param body - The readable stream to pipe
|
|
2835
|
+
* @param errorCallback - Optional custom error handler
|
|
2836
|
+
* @returns Promise that resolves when streaming is complete
|
|
2561
2837
|
*/
|
|
2562
2838
|
streamBody(body, errorCallback) {
|
|
2563
2839
|
return new Promise((resolve) => {
|
|
@@ -2598,7 +2874,19 @@ var Response = class extends Macroable6 {
|
|
|
2598
2874
|
});
|
|
2599
2875
|
}
|
|
2600
2876
|
/**
|
|
2601
|
-
*
|
|
2877
|
+
* Streams a file for download with proper headers and caching support
|
|
2878
|
+
*
|
|
2879
|
+
* Sets appropriate headers:
|
|
2880
|
+
* - Last-Modified based on file stats
|
|
2881
|
+
* - Content-Type based on file extension
|
|
2882
|
+
* - Content-Length from file size
|
|
2883
|
+
* - ETag (if enabled)
|
|
2884
|
+
*
|
|
2885
|
+
* Handles HEAD requests and cache validation (304 responses).
|
|
2886
|
+
*
|
|
2887
|
+
* @param filePath - Path to the file to stream
|
|
2888
|
+
* @param generateEtag - Whether to generate ETag header
|
|
2889
|
+
* @param errorCallback - Optional custom error handler
|
|
2602
2890
|
*/
|
|
2603
2891
|
async streamFileForDownload(filePath, generateEtag, errorCallback) {
|
|
2604
2892
|
try {
|
|
@@ -2638,18 +2926,18 @@ var Response = class extends Macroable6 {
|
|
|
2638
2926
|
}
|
|
2639
2927
|
}
|
|
2640
2928
|
/**
|
|
2641
|
-
*
|
|
2642
|
-
* to the TCP socket.
|
|
2929
|
+
* Registers a callback to be called when the response is finished
|
|
2643
2930
|
*
|
|
2644
|
-
*
|
|
2645
|
-
* the "
|
|
2931
|
+
* The callback is executed when the response has been completely sent.
|
|
2932
|
+
* Uses the "on-finished" package internally.
|
|
2933
|
+
*
|
|
2934
|
+
* @param callback - Function to call when response is finished
|
|
2646
2935
|
*/
|
|
2647
2936
|
onFinish(callback) {
|
|
2648
2937
|
onFinished(this.response, callback);
|
|
2649
2938
|
}
|
|
2650
2939
|
/**
|
|
2651
|
-
*
|
|
2652
|
-
* response.setHeader method
|
|
2940
|
+
* Transfers all buffered headers to the underlying Node.js response object
|
|
2653
2941
|
*/
|
|
2654
2942
|
relayHeaders() {
|
|
2655
2943
|
if (!this.headersSent) {
|
|
@@ -2662,22 +2950,29 @@ var Response = class extends Macroable6 {
|
|
|
2662
2950
|
}
|
|
2663
2951
|
}
|
|
2664
2952
|
/**
|
|
2665
|
-
*
|
|
2953
|
+
* Writes the response status code and headers
|
|
2954
|
+
*
|
|
2955
|
+
* @param statusCode - Optional status code to set
|
|
2956
|
+
* @returns The Response instance for chaining
|
|
2666
2957
|
*/
|
|
2667
2958
|
writeHead(statusCode) {
|
|
2668
2959
|
this.response.writeHead(statusCode || this.response.statusCode, this.#headers);
|
|
2669
2960
|
return this;
|
|
2670
2961
|
}
|
|
2671
2962
|
/**
|
|
2672
|
-
*
|
|
2673
|
-
*
|
|
2963
|
+
* Gets the value of a response header
|
|
2964
|
+
*
|
|
2965
|
+
* @param key - Header name
|
|
2966
|
+
* @returns The header value
|
|
2674
2967
|
*/
|
|
2675
2968
|
getHeader(key) {
|
|
2676
2969
|
const value = this.#headers[key.toLowerCase()];
|
|
2677
2970
|
return value === void 0 ? this.response.getHeader(key) : value;
|
|
2678
2971
|
}
|
|
2679
2972
|
/**
|
|
2680
|
-
*
|
|
2973
|
+
* Gets all response headers as an object
|
|
2974
|
+
*
|
|
2975
|
+
* @returns Object containing all headers
|
|
2681
2976
|
*/
|
|
2682
2977
|
getHeaders() {
|
|
2683
2978
|
return {
|
|
@@ -2686,14 +2981,15 @@ var Response = class extends Macroable6 {
|
|
|
2686
2981
|
};
|
|
2687
2982
|
}
|
|
2688
2983
|
/**
|
|
2689
|
-
*
|
|
2690
|
-
* using [[append]] method.
|
|
2984
|
+
* Sets a response header (replaces existing value)
|
|
2691
2985
|
*
|
|
2692
|
-
*
|
|
2986
|
+
* @param key - Header name
|
|
2987
|
+
* @param value - Header value (ignored if null/undefined)
|
|
2988
|
+
* @returns The Response instance for chaining
|
|
2693
2989
|
*
|
|
2694
2990
|
* @example
|
|
2695
|
-
* ```
|
|
2696
|
-
* response.header('
|
|
2991
|
+
* ```ts
|
|
2992
|
+
* response.header('Content-Type', 'application/json')
|
|
2697
2993
|
* ```
|
|
2698
2994
|
*/
|
|
2699
2995
|
header(key, value) {
|
|
@@ -2704,14 +3000,15 @@ var Response = class extends Macroable6 {
|
|
|
2704
3000
|
return this;
|
|
2705
3001
|
}
|
|
2706
3002
|
/**
|
|
2707
|
-
*
|
|
2708
|
-
* [[header]] method.
|
|
3003
|
+
* Appends a value to an existing response header
|
|
2709
3004
|
*
|
|
2710
|
-
*
|
|
3005
|
+
* @param key - Header name
|
|
3006
|
+
* @param value - Header value to append (ignored if null/undefined)
|
|
3007
|
+
* @returns The Response instance for chaining
|
|
2711
3008
|
*
|
|
2712
3009
|
* @example
|
|
2713
|
-
* ```
|
|
2714
|
-
* response.append('
|
|
3010
|
+
* ```ts
|
|
3011
|
+
* response.append('Set-Cookie', 'session=abc123')
|
|
2715
3012
|
* ```
|
|
2716
3013
|
*/
|
|
2717
3014
|
append(key, value) {
|
|
@@ -2731,7 +3028,11 @@ var Response = class extends Macroable6 {
|
|
|
2731
3028
|
return this;
|
|
2732
3029
|
}
|
|
2733
3030
|
/**
|
|
2734
|
-
*
|
|
3031
|
+
* Sets a header only if it doesn't already exist
|
|
3032
|
+
*
|
|
3033
|
+
* @param key - Header name
|
|
3034
|
+
* @param value - Header value
|
|
3035
|
+
* @returns The Response instance for chaining
|
|
2735
3036
|
*/
|
|
2736
3037
|
safeHeader(key, value) {
|
|
2737
3038
|
if (!this.getHeader(key)) {
|
|
@@ -2740,7 +3041,10 @@ var Response = class extends Macroable6 {
|
|
|
2740
3041
|
return this;
|
|
2741
3042
|
}
|
|
2742
3043
|
/**
|
|
2743
|
-
* Removes
|
|
3044
|
+
* Removes a response header
|
|
3045
|
+
*
|
|
3046
|
+
* @param key - Header name to remove
|
|
3047
|
+
* @returns The Response instance for chaining
|
|
2744
3048
|
*/
|
|
2745
3049
|
removeHeader(key) {
|
|
2746
3050
|
key = key.toLowerCase();
|
|
@@ -2751,13 +3055,18 @@ var Response = class extends Macroable6 {
|
|
|
2751
3055
|
return this;
|
|
2752
3056
|
}
|
|
2753
3057
|
/**
|
|
2754
|
-
*
|
|
3058
|
+
* Gets the current response status code
|
|
3059
|
+
*
|
|
3060
|
+
* @returns The HTTP status code
|
|
2755
3061
|
*/
|
|
2756
3062
|
getStatus() {
|
|
2757
3063
|
return this.response.statusCode;
|
|
2758
3064
|
}
|
|
2759
3065
|
/**
|
|
2760
|
-
*
|
|
3066
|
+
* Sets the response status code
|
|
3067
|
+
*
|
|
3068
|
+
* @param code - HTTP status code
|
|
3069
|
+
* @returns The Response instance for chaining
|
|
2761
3070
|
*/
|
|
2762
3071
|
status(code) {
|
|
2763
3072
|
this.#hasExplicitStatus = true;
|
|
@@ -2765,8 +3074,10 @@ var Response = class extends Macroable6 {
|
|
|
2765
3074
|
return this;
|
|
2766
3075
|
}
|
|
2767
3076
|
/**
|
|
2768
|
-
*
|
|
2769
|
-
*
|
|
3077
|
+
* Sets the status code only if not explicitly set already
|
|
3078
|
+
*
|
|
3079
|
+
* @param code - HTTP status code
|
|
3080
|
+
* @returns The Response instance for chaining
|
|
2770
3081
|
*/
|
|
2771
3082
|
safeStatus(code) {
|
|
2772
3083
|
if (this.#hasExplicitStatus) {
|
|
@@ -2776,15 +3087,16 @@ var Response = class extends Macroable6 {
|
|
|
2776
3087
|
return this;
|
|
2777
3088
|
}
|
|
2778
3089
|
/**
|
|
2779
|
-
*
|
|
2780
|
-
* partial types like file extensions.
|
|
3090
|
+
* Sets the Content-Type header based on mime type lookup
|
|
2781
3091
|
*
|
|
2782
|
-
*
|
|
2783
|
-
*
|
|
3092
|
+
* @param type - File extension or mime type
|
|
3093
|
+
* @param charset - Optional character encoding
|
|
3094
|
+
* @returns The Response instance for chaining
|
|
2784
3095
|
*
|
|
2785
3096
|
* @example
|
|
2786
|
-
* ```
|
|
2787
|
-
* response.type('.json') // Content-
|
|
3097
|
+
* ```ts
|
|
3098
|
+
* response.type('.json') // Content-Type: application/json
|
|
3099
|
+
* response.type('html', 'utf-8') // Content-Type: text/html; charset=utf-8
|
|
2788
3100
|
* ```
|
|
2789
3101
|
*/
|
|
2790
3102
|
type(type, charset) {
|
|
@@ -2793,25 +3105,30 @@ var Response = class extends Macroable6 {
|
|
|
2793
3105
|
return this;
|
|
2794
3106
|
}
|
|
2795
3107
|
/**
|
|
2796
|
-
*
|
|
3108
|
+
* Sets the Vary HTTP header for cache control
|
|
3109
|
+
*
|
|
3110
|
+
* @param field - Header field name(s) to vary on
|
|
3111
|
+
* @returns The Response instance for chaining
|
|
2797
3112
|
*/
|
|
2798
3113
|
vary(field) {
|
|
2799
3114
|
vary(this.response, field);
|
|
2800
3115
|
return this;
|
|
2801
3116
|
}
|
|
2802
3117
|
/**
|
|
2803
|
-
*
|
|
2804
|
-
* when `etag = true` in the defined config object.
|
|
3118
|
+
* Sets the ETag header by computing a hash from the response body
|
|
2805
3119
|
*
|
|
2806
|
-
*
|
|
3120
|
+
* @param body - The response body to hash
|
|
3121
|
+
* @param weak - Whether to generate a weak ETag
|
|
3122
|
+
* @returns The Response instance for chaining
|
|
2807
3123
|
*/
|
|
2808
3124
|
setEtag(body, weak = false) {
|
|
2809
3125
|
this.header("Etag", etag(body, { weak }));
|
|
2810
3126
|
return this;
|
|
2811
3127
|
}
|
|
2812
3128
|
/**
|
|
2813
|
-
*
|
|
3129
|
+
* Sets the X-Request-Id header by copying from the incoming request
|
|
2814
3130
|
*
|
|
3131
|
+
* @returns The Response instance for chaining
|
|
2815
3132
|
*/
|
|
2816
3133
|
setRequestId() {
|
|
2817
3134
|
const requestId = this.request.headers["x-request-id"];
|
|
@@ -2821,27 +3138,20 @@ var Response = class extends Macroable6 {
|
|
|
2821
3138
|
return this;
|
|
2822
3139
|
}
|
|
2823
3140
|
/**
|
|
2824
|
-
*
|
|
2825
|
-
* as the request header `if-none-match`. In case of `true`, the
|
|
2826
|
-
* server must return `304` response, telling the browser to
|
|
2827
|
-
* use the client cache.
|
|
3141
|
+
* Checks if the response is fresh (client cache is valid)
|
|
2828
3142
|
*
|
|
2829
|
-
*
|
|
2830
|
-
*
|
|
3143
|
+
* Compares ETags and modified dates between request and response
|
|
3144
|
+
* to determine if a 304 Not Modified response should be sent.
|
|
2831
3145
|
*
|
|
2832
|
-
*
|
|
3146
|
+
* @returns True if client cache is fresh, false otherwise
|
|
2833
3147
|
*
|
|
2834
3148
|
* @example
|
|
2835
|
-
* ```
|
|
2836
|
-
*
|
|
2837
|
-
*
|
|
2838
|
-
* // sets the HTTP etag header for response
|
|
2839
|
-
* response.setEtag(responseBody)
|
|
2840
|
-
*
|
|
3149
|
+
* ```ts
|
|
3150
|
+
* response.setEtag(content)
|
|
2841
3151
|
* if (response.fresh()) {
|
|
2842
|
-
* response.
|
|
3152
|
+
* response.status(304).send(null)
|
|
2843
3153
|
* } else {
|
|
2844
|
-
* response.send(
|
|
3154
|
+
* response.send(content)
|
|
2845
3155
|
* }
|
|
2846
3156
|
* ```
|
|
2847
3157
|
*/
|
|
@@ -2856,8 +3166,9 @@ var Response = class extends Macroable6 {
|
|
|
2856
3166
|
return false;
|
|
2857
3167
|
}
|
|
2858
3168
|
/**
|
|
2859
|
-
*
|
|
2860
|
-
*
|
|
3169
|
+
* Gets the response body content
|
|
3170
|
+
*
|
|
3171
|
+
* @returns The response body or null if not set or is a stream
|
|
2861
3172
|
*/
|
|
2862
3173
|
getBody() {
|
|
2863
3174
|
if (this.lazyBody.content) {
|
|
@@ -2866,58 +3177,54 @@ var Response = class extends Macroable6 {
|
|
|
2866
3177
|
return null;
|
|
2867
3178
|
}
|
|
2868
3179
|
/**
|
|
2869
|
-
*
|
|
2870
|
-
* is read from `config/app.js` file, using `http.etag` property.
|
|
3180
|
+
* Sends the response body with optional ETag generation
|
|
2871
3181
|
*
|
|
2872
|
-
*
|
|
2873
|
-
*
|
|
3182
|
+
* @param body - The response body
|
|
3183
|
+
* @param generateEtag - Whether to generate ETag header (defaults to config)
|
|
2874
3184
|
*/
|
|
2875
3185
|
send(body, generateEtag = this.#config.etag) {
|
|
2876
3186
|
this.lazyBody.content = [body, generateEtag];
|
|
2877
3187
|
}
|
|
2878
3188
|
/**
|
|
2879
|
-
*
|
|
3189
|
+
* Sends a JSON response (alias for send)
|
|
3190
|
+
*
|
|
3191
|
+
* @param body - The response body to serialize as JSON
|
|
3192
|
+
* @param generateEtag - Whether to generate ETag header
|
|
2880
3193
|
*/
|
|
2881
3194
|
json(body, generateEtag = this.#config.etag) {
|
|
2882
3195
|
return this.send(body, generateEtag);
|
|
2883
3196
|
}
|
|
2884
3197
|
/**
|
|
2885
|
-
*
|
|
2886
|
-
* from top to bottom.
|
|
3198
|
+
* Sends a JSONP response with callback wrapping
|
|
2887
3199
|
*
|
|
2888
|
-
*
|
|
2889
|
-
*
|
|
2890
|
-
*
|
|
2891
|
-
*
|
|
3200
|
+
* Callback name resolution priority:
|
|
3201
|
+
* 1. Explicit callbackName parameter
|
|
3202
|
+
* 2. Query string parameter
|
|
3203
|
+
* 3. Config value
|
|
3204
|
+
* 4. Default "callback"
|
|
2892
3205
|
*
|
|
2893
|
-
*
|
|
2894
|
-
*
|
|
3206
|
+
* @param body - The response body
|
|
3207
|
+
* @param callbackName - JSONP callback function name
|
|
3208
|
+
* @param generateEtag - Whether to generate ETag header
|
|
2895
3209
|
*/
|
|
2896
3210
|
jsonp(body, callbackName = this.#config.jsonpCallbackName, generateEtag = this.#config.etag) {
|
|
2897
3211
|
this.lazyBody.content = [body, generateEtag, callbackName];
|
|
2898
3212
|
}
|
|
2899
3213
|
/**
|
|
2900
|
-
*
|
|
2901
|
-
* the stream, avoiding memory leaks.
|
|
2902
|
-
*
|
|
2903
|
-
* If `raiseErrors=false`, then this method will self handle all the exceptions by
|
|
2904
|
-
* writing a generic HTTP response. To have more control over the error, it is
|
|
2905
|
-
* recommended to set `raiseErrors=true` and wrap this function inside a
|
|
2906
|
-
* `try/catch` statement.
|
|
3214
|
+
* Pipes a readable stream to the response with graceful error handling
|
|
2907
3215
|
*
|
|
2908
|
-
*
|
|
3216
|
+
* @param body - The readable stream to pipe
|
|
3217
|
+
* @param errorCallback - Optional custom error handler
|
|
2909
3218
|
*
|
|
2910
3219
|
* @example
|
|
2911
|
-
* ```
|
|
2912
|
-
* //
|
|
3220
|
+
* ```ts
|
|
3221
|
+
* // Auto error handling
|
|
2913
3222
|
* response.stream(fs.createReadStream('file.txt'))
|
|
2914
3223
|
*
|
|
2915
|
-
* //
|
|
2916
|
-
*
|
|
2917
|
-
*
|
|
2918
|
-
* }
|
|
2919
|
-
* response.status(404).send('File not found')
|
|
2920
|
-
* }
|
|
3224
|
+
* // Custom error handling
|
|
3225
|
+
* response.stream(stream, (error) => {
|
|
3226
|
+
* return error.code === 'ENOENT' ? ['Not found', 404] : ['Error', 500]
|
|
3227
|
+
* })
|
|
2921
3228
|
* ```
|
|
2922
3229
|
*/
|
|
2923
3230
|
stream(body, errorCallback) {
|
|
@@ -2927,38 +3234,35 @@ var Response = class extends Macroable6 {
|
|
|
2927
3234
|
this.lazyBody.stream = [body, errorCallback];
|
|
2928
3235
|
}
|
|
2929
3236
|
/**
|
|
2930
|
-
*
|
|
2931
|
-
* appropriate `Content-type`, `Content-type` and `Last-modified` headers.
|
|
3237
|
+
* Downloads a file by streaming it with appropriate headers
|
|
2932
3238
|
*
|
|
2933
|
-
*
|
|
3239
|
+
* Automatically sets:
|
|
3240
|
+
* - Content-Type from file extension
|
|
3241
|
+
* - Content-Length from file size
|
|
3242
|
+
* - Last-Modified from file stats
|
|
3243
|
+
* - ETag (if enabled)
|
|
2934
3244
|
*
|
|
2935
|
-
*
|
|
2936
|
-
*
|
|
2937
|
-
*
|
|
2938
|
-
* `try/catch` statement.
|
|
3245
|
+
* @param filePath - Path to the file to download
|
|
3246
|
+
* @param generateEtag - Whether to generate ETag header
|
|
3247
|
+
* @param errorCallback - Optional custom error handler
|
|
2939
3248
|
*
|
|
2940
3249
|
* @example
|
|
2941
|
-
* ```
|
|
2942
|
-
*
|
|
2943
|
-
* response.download('
|
|
2944
|
-
*
|
|
2945
|
-
* // Manually handle (note the await call)
|
|
2946
|
-
* try {
|
|
2947
|
-
* await response.download('somefile.jpg')
|
|
2948
|
-
* } catch (error) {
|
|
2949
|
-
* response.status(error.code === 'ENOENT' ? 404 : 500)
|
|
2950
|
-
* response.send('Cannot process file')
|
|
2951
|
-
* }
|
|
3250
|
+
* ```ts
|
|
3251
|
+
* response.download('/path/to/file.pdf')
|
|
3252
|
+
* response.download('/images/photo.jpg', true, (err) => ['Custom error', 500])
|
|
2952
3253
|
* ```
|
|
2953
3254
|
*/
|
|
2954
3255
|
download(filePath, generateEtag = this.#config.etag, errorCallback) {
|
|
2955
3256
|
this.lazyBody.fileToStream = [filePath, generateEtag, errorCallback];
|
|
2956
3257
|
}
|
|
2957
3258
|
/**
|
|
2958
|
-
*
|
|
2959
|
-
* within the browser.
|
|
3259
|
+
* Forces file download by setting Content-Disposition header
|
|
2960
3260
|
*
|
|
2961
|
-
*
|
|
3261
|
+
* @param filePath - Path to the file to download
|
|
3262
|
+
* @param name - Optional filename for download (defaults to original filename)
|
|
3263
|
+
* @param disposition - Content-Disposition type (defaults to 'attachment')
|
|
3264
|
+
* @param generateEtag - Whether to generate ETag header
|
|
3265
|
+
* @param errorCallback - Optional custom error handler
|
|
2962
3266
|
*/
|
|
2963
3267
|
attachment(filePath, name, disposition, generateEtag, errorCallback) {
|
|
2964
3268
|
name = name || filePath;
|
|
@@ -2966,11 +3270,14 @@ var Response = class extends Macroable6 {
|
|
|
2966
3270
|
return this.download(filePath, generateEtag, errorCallback);
|
|
2967
3271
|
}
|
|
2968
3272
|
/**
|
|
2969
|
-
*
|
|
3273
|
+
* Sets the Location header for redirects
|
|
3274
|
+
*
|
|
3275
|
+
* @param url - The URL to redirect to
|
|
3276
|
+
* @returns The Response instance for chaining
|
|
2970
3277
|
*
|
|
2971
3278
|
* @example
|
|
2972
|
-
* ```
|
|
2973
|
-
* response.location('/
|
|
3279
|
+
* ```ts
|
|
3280
|
+
* response.location('/dashboard')
|
|
2974
3281
|
* ```
|
|
2975
3282
|
*/
|
|
2976
3283
|
location(url) {
|
|
@@ -2991,15 +3298,21 @@ var Response = class extends Macroable6 {
|
|
|
2991
3298
|
return handler;
|
|
2992
3299
|
}
|
|
2993
3300
|
/**
|
|
2994
|
-
*
|
|
2995
|
-
*
|
|
3301
|
+
* Aborts the request with a custom response body and status code
|
|
3302
|
+
*
|
|
3303
|
+
* @param body - Response body for the aborted request
|
|
3304
|
+
* @param status - HTTP status code (defaults to 400)
|
|
3305
|
+
* @throws Always throws an HTTP exception
|
|
2996
3306
|
*/
|
|
2997
3307
|
abort(body, status) {
|
|
2998
3308
|
throw E_HTTP_REQUEST_ABORTED.invoke(body, status || ResponseStatus.BadRequest);
|
|
2999
3309
|
}
|
|
3000
3310
|
/**
|
|
3001
|
-
*
|
|
3002
|
-
*
|
|
3311
|
+
* Conditionally aborts the request if the condition is truthy
|
|
3312
|
+
*
|
|
3313
|
+
* @param condition - Condition to evaluate
|
|
3314
|
+
* @param body - Response body for the aborted request
|
|
3315
|
+
* @param status - HTTP status code (defaults to 400)
|
|
3003
3316
|
*/
|
|
3004
3317
|
abortIf(condition, body, status) {
|
|
3005
3318
|
if (condition) {
|
|
@@ -3007,8 +3320,11 @@ var Response = class extends Macroable6 {
|
|
|
3007
3320
|
}
|
|
3008
3321
|
}
|
|
3009
3322
|
/**
|
|
3010
|
-
*
|
|
3011
|
-
*
|
|
3323
|
+
* Conditionally aborts the request if the condition is falsy
|
|
3324
|
+
*
|
|
3325
|
+
* @param condition - Condition to evaluate
|
|
3326
|
+
* @param body - Response body for the aborted request
|
|
3327
|
+
* @param status - HTTP status code (defaults to 400)
|
|
3012
3328
|
*/
|
|
3013
3329
|
abortUnless(condition, body, status) {
|
|
3014
3330
|
if (!condition) {
|
|
@@ -3016,8 +3332,12 @@ var Response = class extends Macroable6 {
|
|
|
3016
3332
|
}
|
|
3017
3333
|
}
|
|
3018
3334
|
/**
|
|
3019
|
-
*
|
|
3020
|
-
*
|
|
3335
|
+
* Sets a signed cookie in the response
|
|
3336
|
+
*
|
|
3337
|
+
* @param key - Cookie name
|
|
3338
|
+
* @param value - Cookie value
|
|
3339
|
+
* @param options - Cookie options (overrides config defaults)
|
|
3340
|
+
* @returns The Response instance for chaining
|
|
3021
3341
|
*/
|
|
3022
3342
|
cookie(key, value, options) {
|
|
3023
3343
|
options = Object.assign({}, this.#config.cookie, options);
|
|
@@ -3029,8 +3349,12 @@ var Response = class extends Macroable6 {
|
|
|
3029
3349
|
return this;
|
|
3030
3350
|
}
|
|
3031
3351
|
/**
|
|
3032
|
-
*
|
|
3033
|
-
*
|
|
3352
|
+
* Sets an encrypted cookie in the response
|
|
3353
|
+
*
|
|
3354
|
+
* @param key - Cookie name
|
|
3355
|
+
* @param value - Cookie value
|
|
3356
|
+
* @param options - Cookie options (overrides config defaults)
|
|
3357
|
+
* @returns The Response instance for chaining
|
|
3034
3358
|
*/
|
|
3035
3359
|
encryptedCookie(key, value, options) {
|
|
3036
3360
|
options = Object.assign({}, this.#config.cookie, options);
|
|
@@ -3042,8 +3366,12 @@ var Response = class extends Macroable6 {
|
|
|
3042
3366
|
return this;
|
|
3043
3367
|
}
|
|
3044
3368
|
/**
|
|
3045
|
-
*
|
|
3046
|
-
*
|
|
3369
|
+
* Sets a plain (unsigned/unencrypted) cookie in the response
|
|
3370
|
+
*
|
|
3371
|
+
* @param key - Cookie name
|
|
3372
|
+
* @param value - Cookie value
|
|
3373
|
+
* @param options - Cookie options including encode flag
|
|
3374
|
+
* @returns The Response instance for chaining
|
|
3047
3375
|
*/
|
|
3048
3376
|
plainCookie(key, value, options) {
|
|
3049
3377
|
options = Object.assign({}, this.#config.cookie, options);
|
|
@@ -3055,7 +3383,11 @@ var Response = class extends Macroable6 {
|
|
|
3055
3383
|
return this;
|
|
3056
3384
|
}
|
|
3057
3385
|
/**
|
|
3058
|
-
*
|
|
3386
|
+
* Clears an existing cookie by setting it to expire
|
|
3387
|
+
*
|
|
3388
|
+
* @param key - Cookie name to clear
|
|
3389
|
+
* @param options - Cookie options (should match original cookie options)
|
|
3390
|
+
* @returns The Response instance for chaining
|
|
3059
3391
|
*/
|
|
3060
3392
|
clearCookie(key, options) {
|
|
3061
3393
|
options = Object.assign({}, this.#config.cookie, options);
|
|
@@ -3066,10 +3398,10 @@ var Response = class extends Macroable6 {
|
|
|
3066
3398
|
return this;
|
|
3067
3399
|
}
|
|
3068
3400
|
/**
|
|
3069
|
-
*
|
|
3070
|
-
* and response is already pending.
|
|
3401
|
+
* Finalizes and sends the response
|
|
3071
3402
|
*
|
|
3072
|
-
*
|
|
3403
|
+
* Writes the buffered body (content, stream, or file) to the client.
|
|
3404
|
+
* This method is idempotent - calling it multiple times has no effect.
|
|
3073
3405
|
*/
|
|
3074
3406
|
finish() {
|
|
3075
3407
|
if (!this.isPending) {
|
|
@@ -3090,294 +3422,408 @@ var Response = class extends Macroable6 {
|
|
|
3090
3422
|
this.#endResponse();
|
|
3091
3423
|
}
|
|
3092
3424
|
/**
|
|
3093
|
-
*
|
|
3425
|
+
* Sends a 100 Continue response
|
|
3094
3426
|
*/
|
|
3095
3427
|
continue() {
|
|
3096
3428
|
this.status(ResponseStatus.Continue);
|
|
3097
3429
|
return this.send(null, false);
|
|
3098
3430
|
}
|
|
3099
3431
|
/**
|
|
3100
|
-
*
|
|
3432
|
+
* Sends a 101 Switching Protocols response
|
|
3101
3433
|
*/
|
|
3102
3434
|
switchingProtocols() {
|
|
3103
3435
|
this.status(ResponseStatus.SwitchingProtocols);
|
|
3104
3436
|
return this.send(null, false);
|
|
3105
3437
|
}
|
|
3106
3438
|
/**
|
|
3107
|
-
*
|
|
3439
|
+
* Sends a 200 OK response
|
|
3440
|
+
*
|
|
3441
|
+
* @param body - Response body
|
|
3442
|
+
* @param generateEtag - Whether to generate ETag header
|
|
3108
3443
|
*/
|
|
3109
3444
|
ok(body, generateEtag) {
|
|
3110
3445
|
this.status(ResponseStatus.Ok);
|
|
3111
3446
|
return this.send(body, generateEtag);
|
|
3112
3447
|
}
|
|
3113
3448
|
/**
|
|
3114
|
-
*
|
|
3449
|
+
* Sends a 201 Created response
|
|
3450
|
+
*
|
|
3451
|
+
* @param body - Response body
|
|
3452
|
+
* @param generateEtag - Whether to generate ETag header
|
|
3115
3453
|
*/
|
|
3116
3454
|
created(body, generateEtag) {
|
|
3117
3455
|
this.status(ResponseStatus.Created);
|
|
3118
3456
|
return this.send(body, generateEtag);
|
|
3119
3457
|
}
|
|
3120
3458
|
/**
|
|
3121
|
-
*
|
|
3459
|
+
* Sends a 202 Accepted response
|
|
3460
|
+
*
|
|
3461
|
+
* @param body - Response body
|
|
3462
|
+
* @param generateEtag - Whether to generate ETag header
|
|
3122
3463
|
*/
|
|
3123
3464
|
accepted(body, generateEtag) {
|
|
3124
3465
|
this.status(ResponseStatus.Accepted);
|
|
3125
3466
|
return this.send(body, generateEtag);
|
|
3126
3467
|
}
|
|
3127
3468
|
/**
|
|
3128
|
-
*
|
|
3469
|
+
* Sends a 203 Non-Authoritative Information response
|
|
3470
|
+
*
|
|
3471
|
+
* @param body - Response body
|
|
3472
|
+
* @param generateEtag - Whether to generate ETag header
|
|
3129
3473
|
*/
|
|
3130
3474
|
nonAuthoritativeInformation(body, generateEtag) {
|
|
3131
3475
|
this.status(ResponseStatus.NonAuthoritativeInformation);
|
|
3132
3476
|
return this.send(body, generateEtag);
|
|
3133
3477
|
}
|
|
3134
3478
|
/**
|
|
3135
|
-
*
|
|
3479
|
+
* Sends a 204 No Content response
|
|
3136
3480
|
*/
|
|
3137
3481
|
noContent() {
|
|
3138
3482
|
this.status(ResponseStatus.NoContent);
|
|
3139
3483
|
return this.send(null, false);
|
|
3140
3484
|
}
|
|
3141
3485
|
/**
|
|
3142
|
-
*
|
|
3486
|
+
* Sends a 205 Reset Content response
|
|
3143
3487
|
*/
|
|
3144
3488
|
resetContent() {
|
|
3145
3489
|
this.status(ResponseStatus.ResetContent);
|
|
3146
3490
|
return this.send(null, false);
|
|
3147
3491
|
}
|
|
3148
3492
|
/**
|
|
3149
|
-
*
|
|
3493
|
+
* Sends a 206 Partial Content response
|
|
3494
|
+
*
|
|
3495
|
+
* @param body - Response body
|
|
3496
|
+
* @param generateEtag - Whether to generate ETag header
|
|
3150
3497
|
*/
|
|
3151
3498
|
partialContent(body, generateEtag) {
|
|
3152
3499
|
this.status(ResponseStatus.PartialContent);
|
|
3153
3500
|
return this.send(body, generateEtag);
|
|
3154
3501
|
}
|
|
3155
3502
|
/**
|
|
3156
|
-
*
|
|
3503
|
+
* Sends a 300 Multiple Choices response
|
|
3504
|
+
*
|
|
3505
|
+
* @param body - Response body
|
|
3506
|
+
* @param generateEtag - Whether to generate ETag header
|
|
3157
3507
|
*/
|
|
3158
3508
|
multipleChoices(body, generateEtag) {
|
|
3159
3509
|
this.status(ResponseStatus.MultipleChoices);
|
|
3160
3510
|
return this.send(body, generateEtag);
|
|
3161
3511
|
}
|
|
3162
3512
|
/**
|
|
3163
|
-
*
|
|
3513
|
+
* Sends a 301 Moved Permanently response
|
|
3514
|
+
*
|
|
3515
|
+
* @param body - Response body
|
|
3516
|
+
* @param generateEtag - Whether to generate ETag header
|
|
3164
3517
|
*/
|
|
3165
3518
|
movedPermanently(body, generateEtag) {
|
|
3166
3519
|
this.status(ResponseStatus.MovedPermanently);
|
|
3167
3520
|
return this.send(body, generateEtag);
|
|
3168
3521
|
}
|
|
3169
3522
|
/**
|
|
3170
|
-
*
|
|
3523
|
+
* Sends a 302 Found (Moved Temporarily) response
|
|
3524
|
+
*
|
|
3525
|
+
* @param body - Response body
|
|
3526
|
+
* @param generateEtag - Whether to generate ETag header
|
|
3171
3527
|
*/
|
|
3172
3528
|
movedTemporarily(body, generateEtag) {
|
|
3173
3529
|
this.status(ResponseStatus.Found);
|
|
3174
3530
|
return this.send(body, generateEtag);
|
|
3175
3531
|
}
|
|
3176
3532
|
/**
|
|
3177
|
-
*
|
|
3533
|
+
* Sends a 303 See Other response
|
|
3534
|
+
*
|
|
3535
|
+
* @param body - Response body
|
|
3536
|
+
* @param generateEtag - Whether to generate ETag header
|
|
3178
3537
|
*/
|
|
3179
3538
|
seeOther(body, generateEtag) {
|
|
3180
3539
|
this.status(ResponseStatus.SeeOther);
|
|
3181
3540
|
return this.send(body, generateEtag);
|
|
3182
3541
|
}
|
|
3183
3542
|
/**
|
|
3184
|
-
*
|
|
3543
|
+
* Sends a 304 Not Modified response
|
|
3544
|
+
*
|
|
3545
|
+
* @param body - Response body
|
|
3546
|
+
* @param generateEtag - Whether to generate ETag header
|
|
3185
3547
|
*/
|
|
3186
3548
|
notModified(body, generateEtag) {
|
|
3187
3549
|
this.status(ResponseStatus.NotModified);
|
|
3188
3550
|
return this.send(body, generateEtag);
|
|
3189
3551
|
}
|
|
3190
3552
|
/**
|
|
3191
|
-
*
|
|
3553
|
+
* Sends a 305 Use Proxy response
|
|
3554
|
+
*
|
|
3555
|
+
* @param body - Response body
|
|
3556
|
+
* @param generateEtag - Whether to generate ETag header
|
|
3192
3557
|
*/
|
|
3193
3558
|
useProxy(body, generateEtag) {
|
|
3194
3559
|
this.status(ResponseStatus.UseProxy);
|
|
3195
3560
|
return this.send(body, generateEtag);
|
|
3196
3561
|
}
|
|
3197
3562
|
/**
|
|
3198
|
-
*
|
|
3563
|
+
* Sends a 307 Temporary Redirect response
|
|
3564
|
+
*
|
|
3565
|
+
* @param body - Response body
|
|
3566
|
+
* @param generateEtag - Whether to generate ETag header
|
|
3199
3567
|
*/
|
|
3200
3568
|
temporaryRedirect(body, generateEtag) {
|
|
3201
3569
|
this.status(ResponseStatus.TemporaryRedirect);
|
|
3202
3570
|
return this.send(body, generateEtag);
|
|
3203
3571
|
}
|
|
3204
3572
|
/**
|
|
3205
|
-
*
|
|
3573
|
+
* Sends a 400 Bad Request response
|
|
3574
|
+
*
|
|
3575
|
+
* @param body - Response body
|
|
3576
|
+
* @param generateEtag - Whether to generate ETag header
|
|
3206
3577
|
*/
|
|
3207
3578
|
badRequest(body, generateEtag) {
|
|
3208
3579
|
this.status(ResponseStatus.BadRequest);
|
|
3209
3580
|
return this.send(body, generateEtag);
|
|
3210
3581
|
}
|
|
3211
3582
|
/**
|
|
3212
|
-
*
|
|
3583
|
+
* Sends a 401 Unauthorized response
|
|
3584
|
+
*
|
|
3585
|
+
* @param body - Response body
|
|
3586
|
+
* @param generateEtag - Whether to generate ETag header
|
|
3213
3587
|
*/
|
|
3214
3588
|
unauthorized(body, generateEtag) {
|
|
3215
3589
|
this.status(ResponseStatus.Unauthorized);
|
|
3216
3590
|
return this.send(body, generateEtag);
|
|
3217
3591
|
}
|
|
3218
3592
|
/**
|
|
3219
|
-
*
|
|
3593
|
+
* Sends a 402 Payment Required response
|
|
3594
|
+
*
|
|
3595
|
+
* @param body - Response body
|
|
3596
|
+
* @param generateEtag - Whether to generate ETag header
|
|
3220
3597
|
*/
|
|
3221
3598
|
paymentRequired(body, generateEtag) {
|
|
3222
3599
|
this.status(ResponseStatus.PaymentRequired);
|
|
3223
3600
|
return this.send(body, generateEtag);
|
|
3224
3601
|
}
|
|
3225
3602
|
/**
|
|
3226
|
-
*
|
|
3603
|
+
* Sends a 403 Forbidden response
|
|
3604
|
+
*
|
|
3605
|
+
* @param body - Response body
|
|
3606
|
+
* @param generateEtag - Whether to generate ETag header
|
|
3227
3607
|
*/
|
|
3228
3608
|
forbidden(body, generateEtag) {
|
|
3229
3609
|
this.status(ResponseStatus.Forbidden);
|
|
3230
3610
|
return this.send(body, generateEtag);
|
|
3231
3611
|
}
|
|
3232
3612
|
/**
|
|
3233
|
-
*
|
|
3613
|
+
* Sends a 404 Not Found response
|
|
3614
|
+
*
|
|
3615
|
+
* @param body - Response body
|
|
3616
|
+
* @param generateEtag - Whether to generate ETag header
|
|
3234
3617
|
*/
|
|
3235
3618
|
notFound(body, generateEtag) {
|
|
3236
3619
|
this.status(ResponseStatus.NotFound);
|
|
3237
3620
|
return this.send(body, generateEtag);
|
|
3238
3621
|
}
|
|
3239
3622
|
/**
|
|
3240
|
-
*
|
|
3623
|
+
* Sends a 405 Method Not Allowed response
|
|
3624
|
+
*
|
|
3625
|
+
* @param body - Response body
|
|
3626
|
+
* @param generateEtag - Whether to generate ETag header
|
|
3241
3627
|
*/
|
|
3242
3628
|
methodNotAllowed(body, generateEtag) {
|
|
3243
3629
|
this.status(ResponseStatus.MethodNotAllowed);
|
|
3244
3630
|
return this.send(body, generateEtag);
|
|
3245
3631
|
}
|
|
3246
3632
|
/**
|
|
3247
|
-
*
|
|
3633
|
+
* Sends a 406 Not Acceptable response
|
|
3634
|
+
*
|
|
3635
|
+
* @param body - Response body
|
|
3636
|
+
* @param generateEtag - Whether to generate ETag header
|
|
3248
3637
|
*/
|
|
3249
3638
|
notAcceptable(body, generateEtag) {
|
|
3250
3639
|
this.status(ResponseStatus.NotAcceptable);
|
|
3251
3640
|
return this.send(body, generateEtag);
|
|
3252
3641
|
}
|
|
3253
3642
|
/**
|
|
3254
|
-
*
|
|
3643
|
+
* Sends a 407 Proxy Authentication Required response
|
|
3644
|
+
*
|
|
3645
|
+
* @param body - Response body
|
|
3646
|
+
* @param generateEtag - Whether to generate ETag header
|
|
3255
3647
|
*/
|
|
3256
3648
|
proxyAuthenticationRequired(body, generateEtag) {
|
|
3257
3649
|
this.status(ResponseStatus.ProxyAuthenticationRequired);
|
|
3258
3650
|
return this.send(body, generateEtag);
|
|
3259
3651
|
}
|
|
3260
3652
|
/**
|
|
3261
|
-
*
|
|
3653
|
+
* Sends a 408 Request Timeout response
|
|
3654
|
+
*
|
|
3655
|
+
* @param body - Response body
|
|
3656
|
+
* @param generateEtag - Whether to generate ETag header
|
|
3262
3657
|
*/
|
|
3263
3658
|
requestTimeout(body, generateEtag) {
|
|
3264
3659
|
this.status(ResponseStatus.RequestTimeout);
|
|
3265
3660
|
return this.send(body, generateEtag);
|
|
3266
3661
|
}
|
|
3267
3662
|
/**
|
|
3268
|
-
*
|
|
3663
|
+
* Sends a 409 Conflict response
|
|
3664
|
+
*
|
|
3665
|
+
* @param body - Response body
|
|
3666
|
+
* @param generateEtag - Whether to generate ETag header
|
|
3269
3667
|
*/
|
|
3270
3668
|
conflict(body, generateEtag) {
|
|
3271
3669
|
this.status(ResponseStatus.Conflict);
|
|
3272
3670
|
return this.send(body, generateEtag);
|
|
3273
3671
|
}
|
|
3274
3672
|
/**
|
|
3275
|
-
*
|
|
3673
|
+
* Sends a 410 Gone response
|
|
3674
|
+
*
|
|
3675
|
+
* @param body - Response body
|
|
3676
|
+
* @param generateEtag - Whether to generate ETag header
|
|
3276
3677
|
*/
|
|
3277
3678
|
gone(body, generateEtag) {
|
|
3278
3679
|
this.status(ResponseStatus.Gone);
|
|
3279
3680
|
return this.send(body, generateEtag);
|
|
3280
3681
|
}
|
|
3281
3682
|
/**
|
|
3282
|
-
*
|
|
3683
|
+
* Sends a 411 Length Required response
|
|
3684
|
+
*
|
|
3685
|
+
* @param body - Response body
|
|
3686
|
+
* @param generateEtag - Whether to generate ETag header
|
|
3283
3687
|
*/
|
|
3284
3688
|
lengthRequired(body, generateEtag) {
|
|
3285
3689
|
this.status(ResponseStatus.LengthRequired);
|
|
3286
3690
|
return this.send(body, generateEtag);
|
|
3287
3691
|
}
|
|
3288
3692
|
/**
|
|
3289
|
-
*
|
|
3693
|
+
* Sends a 412 Precondition Failed response
|
|
3694
|
+
*
|
|
3695
|
+
* @param body - Response body
|
|
3696
|
+
* @param generateEtag - Whether to generate ETag header
|
|
3290
3697
|
*/
|
|
3291
3698
|
preconditionFailed(body, generateEtag) {
|
|
3292
3699
|
this.status(ResponseStatus.PreconditionFailed);
|
|
3293
3700
|
return this.send(body, generateEtag);
|
|
3294
3701
|
}
|
|
3295
3702
|
/**
|
|
3296
|
-
*
|
|
3703
|
+
* Sends a 413 Payload Too Large response
|
|
3704
|
+
*
|
|
3705
|
+
* @param body - Response body
|
|
3706
|
+
* @param generateEtag - Whether to generate ETag header
|
|
3297
3707
|
*/
|
|
3298
3708
|
requestEntityTooLarge(body, generateEtag) {
|
|
3299
3709
|
this.status(ResponseStatus.PayloadTooLarge);
|
|
3300
3710
|
return this.send(body, generateEtag);
|
|
3301
3711
|
}
|
|
3302
3712
|
/**
|
|
3303
|
-
*
|
|
3713
|
+
* Sends a 414 URI Too Long response
|
|
3714
|
+
*
|
|
3715
|
+
* @param body - Response body
|
|
3716
|
+
* @param generateEtag - Whether to generate ETag header
|
|
3304
3717
|
*/
|
|
3305
3718
|
requestUriTooLong(body, generateEtag) {
|
|
3306
3719
|
this.status(ResponseStatus.URITooLong);
|
|
3307
3720
|
return this.send(body, generateEtag);
|
|
3308
3721
|
}
|
|
3309
3722
|
/**
|
|
3310
|
-
*
|
|
3723
|
+
* Sends a 415 Unsupported Media Type response
|
|
3724
|
+
*
|
|
3725
|
+
* @param body - Response body
|
|
3726
|
+
* @param generateEtag - Whether to generate ETag header
|
|
3311
3727
|
*/
|
|
3312
3728
|
unsupportedMediaType(body, generateEtag) {
|
|
3313
3729
|
this.status(ResponseStatus.UnsupportedMediaType);
|
|
3314
3730
|
return this.send(body, generateEtag);
|
|
3315
3731
|
}
|
|
3316
3732
|
/**
|
|
3317
|
-
*
|
|
3733
|
+
* Sends a 416 Range Not Satisfiable response
|
|
3734
|
+
*
|
|
3735
|
+
* @param body - Response body
|
|
3736
|
+
* @param generateEtag - Whether to generate ETag header
|
|
3318
3737
|
*/
|
|
3319
3738
|
requestedRangeNotSatisfiable(body, generateEtag) {
|
|
3320
3739
|
this.status(ResponseStatus.RangeNotSatisfiable);
|
|
3321
3740
|
return this.send(body, generateEtag);
|
|
3322
3741
|
}
|
|
3323
3742
|
/**
|
|
3324
|
-
*
|
|
3743
|
+
* Sends a 417 Expectation Failed response
|
|
3744
|
+
*
|
|
3745
|
+
* @param body - Response body
|
|
3746
|
+
* @param generateEtag - Whether to generate ETag header
|
|
3325
3747
|
*/
|
|
3326
3748
|
expectationFailed(body, generateEtag) {
|
|
3327
3749
|
this.status(ResponseStatus.ExpectationFailed);
|
|
3328
3750
|
return this.send(body, generateEtag);
|
|
3329
3751
|
}
|
|
3330
3752
|
/**
|
|
3331
|
-
*
|
|
3753
|
+
* Sends a 422 Unprocessable Entity response
|
|
3754
|
+
*
|
|
3755
|
+
* @param body - Response body
|
|
3756
|
+
* @param generateEtag - Whether to generate ETag header
|
|
3332
3757
|
*/
|
|
3333
3758
|
unprocessableEntity(body, generateEtag) {
|
|
3334
3759
|
this.status(ResponseStatus.UnprocessableEntity);
|
|
3335
3760
|
return this.send(body, generateEtag);
|
|
3336
3761
|
}
|
|
3337
3762
|
/**
|
|
3338
|
-
*
|
|
3763
|
+
* Sends a 429 Too Many Requests response
|
|
3764
|
+
*
|
|
3765
|
+
* @param body - Response body
|
|
3766
|
+
* @param generateEtag - Whether to generate ETag header
|
|
3339
3767
|
*/
|
|
3340
3768
|
tooManyRequests(body, generateEtag) {
|
|
3341
3769
|
this.status(ResponseStatus.TooManyRequests);
|
|
3342
3770
|
return this.send(body, generateEtag);
|
|
3343
3771
|
}
|
|
3344
3772
|
/**
|
|
3345
|
-
*
|
|
3773
|
+
* Sends a 500 Internal Server Error response
|
|
3774
|
+
*
|
|
3775
|
+
* @param body - Response body
|
|
3776
|
+
* @param generateEtag - Whether to generate ETag header
|
|
3346
3777
|
*/
|
|
3347
3778
|
internalServerError(body, generateEtag) {
|
|
3348
3779
|
this.status(ResponseStatus.InternalServerError);
|
|
3349
3780
|
return this.send(body, generateEtag);
|
|
3350
3781
|
}
|
|
3351
3782
|
/**
|
|
3352
|
-
*
|
|
3783
|
+
* Sends a 501 Not Implemented response
|
|
3784
|
+
*
|
|
3785
|
+
* @param body - Response body
|
|
3786
|
+
* @param generateEtag - Whether to generate ETag header
|
|
3353
3787
|
*/
|
|
3354
3788
|
notImplemented(body, generateEtag) {
|
|
3355
3789
|
this.status(ResponseStatus.NotImplemented);
|
|
3356
3790
|
return this.send(body, generateEtag);
|
|
3357
3791
|
}
|
|
3358
3792
|
/**
|
|
3359
|
-
*
|
|
3793
|
+
* Sends a 502 Bad Gateway response
|
|
3794
|
+
*
|
|
3795
|
+
* @param body - Response body
|
|
3796
|
+
* @param generateEtag - Whether to generate ETag header
|
|
3360
3797
|
*/
|
|
3361
3798
|
badGateway(body, generateEtag) {
|
|
3362
3799
|
this.status(ResponseStatus.BadGateway);
|
|
3363
3800
|
return this.send(body, generateEtag);
|
|
3364
3801
|
}
|
|
3365
3802
|
/**
|
|
3366
|
-
*
|
|
3803
|
+
* Sends a 503 Service Unavailable response
|
|
3804
|
+
*
|
|
3805
|
+
* @param body - Response body
|
|
3806
|
+
* @param generateEtag - Whether to generate ETag header
|
|
3367
3807
|
*/
|
|
3368
3808
|
serviceUnavailable(body, generateEtag) {
|
|
3369
3809
|
this.status(ResponseStatus.ServiceUnavailable);
|
|
3370
3810
|
return this.send(body, generateEtag);
|
|
3371
3811
|
}
|
|
3372
3812
|
/**
|
|
3373
|
-
*
|
|
3813
|
+
* Sends a 504 Gateway Timeout response
|
|
3814
|
+
*
|
|
3815
|
+
* @param body - Response body
|
|
3816
|
+
* @param generateEtag - Whether to generate ETag header
|
|
3374
3817
|
*/
|
|
3375
3818
|
gatewayTimeout(body, generateEtag) {
|
|
3376
3819
|
this.status(ResponseStatus.GatewayTimeout);
|
|
3377
3820
|
return this.send(body, generateEtag);
|
|
3378
3821
|
}
|
|
3379
3822
|
/**
|
|
3380
|
-
*
|
|
3823
|
+
* Sends a 505 HTTP Version Not Supported response
|
|
3824
|
+
*
|
|
3825
|
+
* @param body - Response body
|
|
3826
|
+
* @param generateEtag - Whether to generate ETag header
|
|
3381
3827
|
*/
|
|
3382
3828
|
httpVersionNotSupported(body, generateEtag) {
|
|
3383
3829
|
this.status(ResponseStatus.HTTPVersionNotSupported);
|
|
@@ -3472,6 +3918,8 @@ var RoutesStore = class {
|
|
|
3472
3918
|
* }
|
|
3473
3919
|
* })
|
|
3474
3920
|
* ```
|
|
3921
|
+
* @param route - The route to add to the store
|
|
3922
|
+
* @returns Current RoutesStore instance for method chaining
|
|
3475
3923
|
*/
|
|
3476
3924
|
add(route) {
|
|
3477
3925
|
if (route.domain !== "root") {
|
|
@@ -3492,6 +3940,11 @@ var RoutesStore = class {
|
|
|
3492
3940
|
* The domain parameter has to be a registered pattern and not the fully
|
|
3493
3941
|
* qualified runtime domain. You must call `matchDomain` first to fetch
|
|
3494
3942
|
* the pattern for qualified domain
|
|
3943
|
+
* @param url - The URL to match
|
|
3944
|
+
* @param method - HTTP method
|
|
3945
|
+
* @param shouldDecodeParam - Whether to decode parameters
|
|
3946
|
+
* @param domain - Optional domain tokens and hostname
|
|
3947
|
+
* @returns Matched route or null if no match found
|
|
3495
3948
|
*/
|
|
3496
3949
|
match(url, method, shouldDecodeParam, domain) {
|
|
3497
3950
|
const domainName = domain?.tokens[0]?.old || "root";
|
|
@@ -3517,6 +3970,8 @@ var RoutesStore = class {
|
|
|
3517
3970
|
}
|
|
3518
3971
|
/**
|
|
3519
3972
|
* Match hostname against registered domains.
|
|
3973
|
+
* @param hostname - The hostname to match
|
|
3974
|
+
* @returns Array of matched domain tokens
|
|
3520
3975
|
*/
|
|
3521
3976
|
matchDomain(hostname) {
|
|
3522
3977
|
if (!hostname || !this.usingDomains) {
|
|
@@ -3526,6 +3981,84 @@ var RoutesStore = class {
|
|
|
3526
3981
|
}
|
|
3527
3982
|
};
|
|
3528
3983
|
|
|
3984
|
+
// src/router/url_builder.ts
|
|
3985
|
+
function createUrlBuilder(router, searchParamsStringifier) {
|
|
3986
|
+
let domainsList;
|
|
3987
|
+
function createUrlForRoute(identifier, params, options, method) {
|
|
3988
|
+
if (!domainsList) {
|
|
3989
|
+
domainsList = Object.keys(router.toJSON()).filter((domain2) => domain2 !== "root");
|
|
3990
|
+
}
|
|
3991
|
+
const domain = domainsList.find((name) => identifier.startsWith(`${name}@`));
|
|
3992
|
+
const routeIdentifier = domain ? identifier.replace(new RegExp(`^${domain}@`), "") : identifier;
|
|
3993
|
+
const route = router.findOrFail(routeIdentifier, domain, method, true);
|
|
3994
|
+
return createURL(
|
|
3995
|
+
route.name ?? route.pattern,
|
|
3996
|
+
route.tokens,
|
|
3997
|
+
searchParamsStringifier,
|
|
3998
|
+
params,
|
|
3999
|
+
options
|
|
4000
|
+
);
|
|
4001
|
+
}
|
|
4002
|
+
const urlFor = function route(...[identifier, params, options]) {
|
|
4003
|
+
return createUrlForRoute(identifier, params, options);
|
|
4004
|
+
};
|
|
4005
|
+
urlFor.get = function urlForMethodGet(...[identifier, params, options]) {
|
|
4006
|
+
return {
|
|
4007
|
+
url: createUrlForRoute(identifier, params, options, "GET"),
|
|
4008
|
+
method: "get",
|
|
4009
|
+
toString() {
|
|
4010
|
+
return this.url;
|
|
4011
|
+
}
|
|
4012
|
+
};
|
|
4013
|
+
};
|
|
4014
|
+
urlFor.post = function urlForMethodPost(...[identifier, params, options]) {
|
|
4015
|
+
return {
|
|
4016
|
+
url: createUrlForRoute(identifier, params, options, "POST"),
|
|
4017
|
+
method: "post",
|
|
4018
|
+
toString() {
|
|
4019
|
+
return this.url;
|
|
4020
|
+
}
|
|
4021
|
+
};
|
|
4022
|
+
};
|
|
4023
|
+
urlFor.put = function urlForMethodPut(...[identifier, params, options]) {
|
|
4024
|
+
return {
|
|
4025
|
+
url: createUrlForRoute(identifier, params, options, "PUT"),
|
|
4026
|
+
method: "put",
|
|
4027
|
+
toString() {
|
|
4028
|
+
return this.url;
|
|
4029
|
+
}
|
|
4030
|
+
};
|
|
4031
|
+
};
|
|
4032
|
+
urlFor.patch = function urlForMethodPatch(...[identifier, params, options]) {
|
|
4033
|
+
return {
|
|
4034
|
+
url: createUrlForRoute(identifier, params, options, "PATCH"),
|
|
4035
|
+
method: "patch",
|
|
4036
|
+
toString() {
|
|
4037
|
+
return this.url;
|
|
4038
|
+
}
|
|
4039
|
+
};
|
|
4040
|
+
};
|
|
4041
|
+
urlFor.delete = function urlForMethodDelete(...[identifier, params, options]) {
|
|
4042
|
+
return {
|
|
4043
|
+
url: createUrlForRoute(identifier, params, options, "DELETE"),
|
|
4044
|
+
method: "delete",
|
|
4045
|
+
toString() {
|
|
4046
|
+
return this.url;
|
|
4047
|
+
}
|
|
4048
|
+
};
|
|
4049
|
+
};
|
|
4050
|
+
urlFor.method = function urlForCustomMethod(method, ...[identifier, params, options]) {
|
|
4051
|
+
return {
|
|
4052
|
+
url: createUrlForRoute(identifier, params, options, method),
|
|
4053
|
+
method,
|
|
4054
|
+
toString() {
|
|
4055
|
+
return this.url;
|
|
4056
|
+
}
|
|
4057
|
+
};
|
|
4058
|
+
};
|
|
4059
|
+
return urlFor;
|
|
4060
|
+
}
|
|
4061
|
+
|
|
3529
4062
|
// src/router/legacy/url_builder.ts
|
|
3530
4063
|
var UrlBuilder = class {
|
|
3531
4064
|
/**
|
|
@@ -3549,15 +4082,22 @@ var UrlBuilder = class {
|
|
|
3549
4082
|
* Route finder for finding route pattern
|
|
3550
4083
|
*/
|
|
3551
4084
|
#router;
|
|
4085
|
+
/**
|
|
4086
|
+
* Domain to use for URL generation
|
|
4087
|
+
*/
|
|
3552
4088
|
#domain;
|
|
4089
|
+
/**
|
|
4090
|
+
* Creates a new UrlBuilder instance
|
|
4091
|
+
* @param router - The router instance
|
|
4092
|
+
* @param domain - Optional domain for URL generation
|
|
4093
|
+
*/
|
|
3553
4094
|
constructor(router, domain) {
|
|
3554
4095
|
this.#router = router;
|
|
3555
4096
|
this.#domain = domain;
|
|
3556
4097
|
}
|
|
3557
4098
|
/**
|
|
3558
4099
|
* Prefix a custom base URL to the final URI
|
|
3559
|
-
* @deprecated
|
|
3560
|
-
* Instead use "@adonisjs/core/services/url_builder" instead
|
|
4100
|
+
* @deprecated Instead use "@adonisjs/core/services/url_builder" instead
|
|
3561
4101
|
*/
|
|
3562
4102
|
prefixUrl(url) {
|
|
3563
4103
|
this.#baseUrl = url;
|
|
@@ -3566,8 +4106,7 @@ var UrlBuilder = class {
|
|
|
3566
4106
|
/**
|
|
3567
4107
|
* Disable route lookup. Calling this method considers
|
|
3568
4108
|
* the "identifier" as the route pattern
|
|
3569
|
-
* @deprecated
|
|
3570
|
-
* Instead use "@adonisjs/core/services/url_builder" instead
|
|
4109
|
+
* @deprecated Instead use "@adonisjs/core/services/url_builder" instead
|
|
3571
4110
|
*/
|
|
3572
4111
|
disableRouteLookup() {
|
|
3573
4112
|
this.#shouldPerformLookup = false;
|
|
@@ -3575,8 +4114,7 @@ var UrlBuilder = class {
|
|
|
3575
4114
|
}
|
|
3576
4115
|
/**
|
|
3577
4116
|
* Append query string to the final URI
|
|
3578
|
-
* @deprecated
|
|
3579
|
-
* Instead use "@adonisjs/core/services/url_builder" instead
|
|
4117
|
+
* @deprecated Instead use "@adonisjs/core/services/url_builder" instead
|
|
3580
4118
|
*/
|
|
3581
4119
|
qs(queryString) {
|
|
3582
4120
|
if (!queryString) {
|
|
@@ -3587,8 +4125,7 @@ var UrlBuilder = class {
|
|
|
3587
4125
|
}
|
|
3588
4126
|
/**
|
|
3589
4127
|
* Specify params to apply to the route pattern
|
|
3590
|
-
* @deprecated
|
|
3591
|
-
* Instead use "@adonisjs/core/services/url_builder" instead
|
|
4128
|
+
* @deprecated Instead use "@adonisjs/core/services/url_builder" instead
|
|
3592
4129
|
*/
|
|
3593
4130
|
params(params) {
|
|
3594
4131
|
if (!params) {
|
|
@@ -3602,8 +4139,9 @@ var UrlBuilder = class {
|
|
|
3602
4139
|
* route name, controller.method name or the route pattern
|
|
3603
4140
|
* itself.
|
|
3604
4141
|
*
|
|
3605
|
-
* @deprecated
|
|
3606
|
-
*
|
|
4142
|
+
* @deprecated Instead use "@adonisjs/core/services/url_builder" instead
|
|
4143
|
+
* @param identifier - Route identifier to generate URL for
|
|
4144
|
+
* @returns Generated URL string
|
|
3607
4145
|
*/
|
|
3608
4146
|
make(identifier) {
|
|
3609
4147
|
return this.#router.makeUrl(identifier, this.#params, {
|
|
@@ -3618,8 +4156,7 @@ var UrlBuilder = class {
|
|
|
3618
4156
|
* route name, controller.method name or the route pattern
|
|
3619
4157
|
* itself.
|
|
3620
4158
|
*
|
|
3621
|
-
* @deprecated
|
|
3622
|
-
* Instead use "@adonisjs/core/services/url_builder" instead
|
|
4159
|
+
* @deprecated Instead use "@adonisjs/core/services/url_builder" instead
|
|
3623
4160
|
*
|
|
3624
4161
|
*/
|
|
3625
4162
|
makeSigned(identifier, options) {
|
|
@@ -3639,12 +4176,14 @@ var RouteMatchers = class extends Macroable7 {
|
|
|
3639
4176
|
/**
|
|
3640
4177
|
* Enforce value to be a number and also casts it to number data
|
|
3641
4178
|
* type
|
|
4179
|
+
* @returns Route matcher configuration for numeric values
|
|
3642
4180
|
*/
|
|
3643
4181
|
number() {
|
|
3644
4182
|
return { match: /^[0-9]+$/, cast: (value) => Number(value) };
|
|
3645
4183
|
}
|
|
3646
4184
|
/**
|
|
3647
4185
|
* Enforce value to be formatted as uuid
|
|
4186
|
+
* @returns Route matcher configuration for UUID values
|
|
3648
4187
|
*/
|
|
3649
4188
|
uuid() {
|
|
3650
4189
|
return {
|
|
@@ -3654,133 +4193,13 @@ var RouteMatchers = class extends Macroable7 {
|
|
|
3654
4193
|
}
|
|
3655
4194
|
/**
|
|
3656
4195
|
* Enforce value to be formatted as slug
|
|
4196
|
+
* @returns Route matcher configuration for slug values
|
|
3657
4197
|
*/
|
|
3658
4198
|
slug() {
|
|
3659
4199
|
return { match: /^[^\s-_](?!.*?[-_]{2,})([a-z0-9-\\]{1,})[^\s]*[^-_\s]$/ };
|
|
3660
4200
|
}
|
|
3661
4201
|
};
|
|
3662
4202
|
|
|
3663
|
-
// src/router/url_builder.ts
|
|
3664
|
-
function createURL(identifier, tokens, searchParamsStringifier, params, options) {
|
|
3665
|
-
const uriSegments = [];
|
|
3666
|
-
const paramsArray = Array.isArray(params) ? params : null;
|
|
3667
|
-
const paramsObject = !Array.isArray(params) ? params ?? {} : {};
|
|
3668
|
-
let paramsIndex = 0;
|
|
3669
|
-
for (const token of tokens) {
|
|
3670
|
-
if (token.type === 0) {
|
|
3671
|
-
uriSegments.push(token.val === "/" ? "" : `${token.val}${token.end}`);
|
|
3672
|
-
continue;
|
|
3673
|
-
}
|
|
3674
|
-
if (token.type === 2) {
|
|
3675
|
-
const values = paramsArray ? paramsArray.slice(paramsIndex) : paramsObject["*"];
|
|
3676
|
-
if (!Array.isArray(values) || !values.length) {
|
|
3677
|
-
throw new Error(
|
|
3678
|
-
`Cannot make URL for "${identifier}". Invalid value provided for the wildcard param`
|
|
3679
|
-
);
|
|
3680
|
-
}
|
|
3681
|
-
uriSegments.push(`${values.join("/")}${token.end}`);
|
|
3682
|
-
break;
|
|
3683
|
-
}
|
|
3684
|
-
const paramName = token.val;
|
|
3685
|
-
const value = paramsArray ? paramsArray[paramsIndex] : paramsObject[paramName];
|
|
3686
|
-
const isDefined = value !== void 0 && value !== null;
|
|
3687
|
-
if (token.type === 1 && !isDefined) {
|
|
3688
|
-
throw new Error(
|
|
3689
|
-
`Cannot make URL for "${identifier}". Missing value for the "${paramName}" param`
|
|
3690
|
-
);
|
|
3691
|
-
}
|
|
3692
|
-
if (isDefined) {
|
|
3693
|
-
uriSegments.push(`${value}${token.end}`);
|
|
3694
|
-
}
|
|
3695
|
-
paramsIndex++;
|
|
3696
|
-
}
|
|
3697
|
-
let URI = `/${uriSegments.join("/")}`;
|
|
3698
|
-
if (options?.prefixUrl) {
|
|
3699
|
-
URI = `${options?.prefixUrl.replace(/\/$/, "")}${URI}`;
|
|
3700
|
-
}
|
|
3701
|
-
if (options?.qs) {
|
|
3702
|
-
const queryString = searchParamsStringifier(options?.qs);
|
|
3703
|
-
URI = queryString ? `${URI}?${queryString}` : URI;
|
|
3704
|
-
}
|
|
3705
|
-
return URI;
|
|
3706
|
-
}
|
|
3707
|
-
function createUrlBuilder(router, searchParamsStringifier) {
|
|
3708
|
-
let domainsList;
|
|
3709
|
-
function createUrlForRoute(identifier, params, options, method) {
|
|
3710
|
-
if (!domainsList) {
|
|
3711
|
-
domainsList = Object.keys(router.toJSON()).filter((domain2) => domain2 !== "root");
|
|
3712
|
-
}
|
|
3713
|
-
const domain = domainsList.find((name) => identifier.startsWith(`${name}@`));
|
|
3714
|
-
const routeIdentifier = domain ? identifier.replace(new RegExp(`^${domain}@`), "") : identifier;
|
|
3715
|
-
const route = router.findOrFail(routeIdentifier, domain, method, true);
|
|
3716
|
-
return createURL(
|
|
3717
|
-
route.name ?? route.pattern,
|
|
3718
|
-
route.tokens,
|
|
3719
|
-
searchParamsStringifier,
|
|
3720
|
-
params,
|
|
3721
|
-
options
|
|
3722
|
-
);
|
|
3723
|
-
}
|
|
3724
|
-
const urlFor = function route(...[identifier, params, options]) {
|
|
3725
|
-
return createUrlForRoute(identifier, params, options);
|
|
3726
|
-
};
|
|
3727
|
-
urlFor.get = function urlForMethodGet(...[identifier, params, options]) {
|
|
3728
|
-
return {
|
|
3729
|
-
url: createUrlForRoute(identifier, params, options, "GET"),
|
|
3730
|
-
method: "get",
|
|
3731
|
-
toString() {
|
|
3732
|
-
return this.url;
|
|
3733
|
-
}
|
|
3734
|
-
};
|
|
3735
|
-
};
|
|
3736
|
-
urlFor.post = function urlForMethodPost(...[identifier, params, options]) {
|
|
3737
|
-
return {
|
|
3738
|
-
url: createUrlForRoute(identifier, params, options, "POST"),
|
|
3739
|
-
method: "post",
|
|
3740
|
-
toString() {
|
|
3741
|
-
return this.url;
|
|
3742
|
-
}
|
|
3743
|
-
};
|
|
3744
|
-
};
|
|
3745
|
-
urlFor.put = function urlForMethodPut(...[identifier, params, options]) {
|
|
3746
|
-
return {
|
|
3747
|
-
url: createUrlForRoute(identifier, params, options, "PUT"),
|
|
3748
|
-
method: "put",
|
|
3749
|
-
toString() {
|
|
3750
|
-
return this.url;
|
|
3751
|
-
}
|
|
3752
|
-
};
|
|
3753
|
-
};
|
|
3754
|
-
urlFor.patch = function urlForMethodPatch(...[identifier, params, options]) {
|
|
3755
|
-
return {
|
|
3756
|
-
url: createUrlForRoute(identifier, params, options, "PATCH"),
|
|
3757
|
-
method: "patch",
|
|
3758
|
-
toString() {
|
|
3759
|
-
return this.url;
|
|
3760
|
-
}
|
|
3761
|
-
};
|
|
3762
|
-
};
|
|
3763
|
-
urlFor.delete = function urlForMethodDelete(...[identifier, params, options]) {
|
|
3764
|
-
return {
|
|
3765
|
-
url: createUrlForRoute(identifier, params, options, "DELETE"),
|
|
3766
|
-
method: "delete",
|
|
3767
|
-
toString() {
|
|
3768
|
-
return this.url;
|
|
3769
|
-
}
|
|
3770
|
-
};
|
|
3771
|
-
};
|
|
3772
|
-
urlFor.method = function urlForCustomMethod(method, ...[identifier, params, options]) {
|
|
3773
|
-
return {
|
|
3774
|
-
url: createUrlForRoute(identifier, params, options, method),
|
|
3775
|
-
method,
|
|
3776
|
-
toString() {
|
|
3777
|
-
return this.url;
|
|
3778
|
-
}
|
|
3779
|
-
};
|
|
3780
|
-
};
|
|
3781
|
-
return urlFor;
|
|
3782
|
-
}
|
|
3783
|
-
|
|
3784
4203
|
// src/define_middleware.ts
|
|
3785
4204
|
import { moduleImporter as moduleImporter2 } from "@adonisjs/fold";
|
|
3786
4205
|
function middlewareReferenceBuilder(name, middleware) {
|
|
@@ -3805,20 +4224,6 @@ function defineNamedMiddleware(collection) {
|
|
|
3805
4224
|
}
|
|
3806
4225
|
|
|
3807
4226
|
// src/router/signed_url_builder.ts
|
|
3808
|
-
function createSignedURL(identifier, tokens, searchParamsStringifier, encryption, params, options) {
|
|
3809
|
-
const signature = encryption.verifier.sign(
|
|
3810
|
-
createURL(identifier, tokens, searchParamsStringifier, params, {
|
|
3811
|
-
...options,
|
|
3812
|
-
prefixUrl: void 0
|
|
3813
|
-
}),
|
|
3814
|
-
options?.expiresIn,
|
|
3815
|
-
options?.purpose
|
|
3816
|
-
);
|
|
3817
|
-
return createURL(identifier, tokens, searchParamsStringifier, params, {
|
|
3818
|
-
...options,
|
|
3819
|
-
qs: { ...options?.qs, signature }
|
|
3820
|
-
});
|
|
3821
|
-
}
|
|
3822
4227
|
function createSignedUrlBuilder(router, encryption, searchParamsStringifier) {
|
|
3823
4228
|
let domainsList;
|
|
3824
4229
|
function createSignedUrlForRoute(identifier, params, options, method) {
|
|
@@ -3966,6 +4371,12 @@ var Router = class {
|
|
|
3966
4371
|
* List of route references kept for lookup.
|
|
3967
4372
|
*/
|
|
3968
4373
|
routes = {};
|
|
4374
|
+
/**
|
|
4375
|
+
* Creates a new Router instance
|
|
4376
|
+
* @param app - The AdonisJS application instance
|
|
4377
|
+
* @param encryption - Encryption service for signed URLs
|
|
4378
|
+
* @param qsParser - Query string parser for URL generation
|
|
4379
|
+
*/
|
|
3969
4380
|
constructor(app, encryption, qsParser) {
|
|
3970
4381
|
this.#app = app;
|
|
3971
4382
|
this.#encryption = encryption;
|
|
@@ -3996,6 +4407,8 @@ var Router = class {
|
|
|
3996
4407
|
}
|
|
3997
4408
|
/**
|
|
3998
4409
|
* Parses the route pattern
|
|
4410
|
+
* @param pattern - The route pattern to parse
|
|
4411
|
+
* @param matchers - Optional route matchers
|
|
3999
4412
|
*/
|
|
4000
4413
|
parsePattern(pattern, matchers) {
|
|
4001
4414
|
return parseRoute(pattern, matchers);
|
|
@@ -4004,6 +4417,8 @@ var Router = class {
|
|
|
4004
4417
|
* Define an array of middleware to use on all the routes.
|
|
4005
4418
|
* Calling this method multiple times pushes to the
|
|
4006
4419
|
* existing list of middleware
|
|
4420
|
+
* @param middleware - Array of middleware classes to apply globally
|
|
4421
|
+
* @returns Current Router instance for method chaining
|
|
4007
4422
|
*/
|
|
4008
4423
|
use(middleware) {
|
|
4009
4424
|
middleware.forEach(
|
|
@@ -4018,12 +4433,18 @@ var Router = class {
|
|
|
4018
4433
|
* Define a collection of named middleware. The defined collection is
|
|
4019
4434
|
* not registered anywhere, but instead converted in a new collection
|
|
4020
4435
|
* of functions you can apply on the routes, or router groups.
|
|
4436
|
+
* @param collection - Object mapping middleware names to middleware classes
|
|
4437
|
+
* @returns Named middleware functions
|
|
4021
4438
|
*/
|
|
4022
4439
|
named(collection) {
|
|
4023
4440
|
return defineNamedMiddleware(collection);
|
|
4024
4441
|
}
|
|
4025
4442
|
/**
|
|
4026
4443
|
* Add route for a given pattern and methods
|
|
4444
|
+
* @param pattern - The route pattern
|
|
4445
|
+
* @param methods - Array of HTTP methods
|
|
4446
|
+
* @param handler - Route handler (function, string, or controller tuple)
|
|
4447
|
+
* @returns The created route instance
|
|
4027
4448
|
*/
|
|
4028
4449
|
route(pattern, methods, handler) {
|
|
4029
4450
|
const route = new Route(this.#app, this.#middleware, {
|
|
@@ -4037,6 +4458,9 @@ var Router = class {
|
|
|
4037
4458
|
}
|
|
4038
4459
|
/**
|
|
4039
4460
|
* Define a route that handles all common HTTP methods
|
|
4461
|
+
* @param pattern - The route pattern
|
|
4462
|
+
* @param handler - Route handler (function, string, or controller tuple)
|
|
4463
|
+
* @returns The created route instance
|
|
4040
4464
|
*/
|
|
4041
4465
|
any(pattern, handler) {
|
|
4042
4466
|
return this.route(
|
|
@@ -4047,30 +4471,45 @@ var Router = class {
|
|
|
4047
4471
|
}
|
|
4048
4472
|
/**
|
|
4049
4473
|
* Define `GET` route
|
|
4474
|
+
* @param pattern - The route pattern
|
|
4475
|
+
* @param handler - Route handler (function, string, or controller tuple)
|
|
4476
|
+
* @returns The created route instance
|
|
4050
4477
|
*/
|
|
4051
4478
|
get(pattern, handler) {
|
|
4052
4479
|
return this.route(pattern, ["GET", "HEAD"], handler);
|
|
4053
4480
|
}
|
|
4054
4481
|
/**
|
|
4055
4482
|
* Define `POST` route
|
|
4483
|
+
* @param pattern - The route pattern
|
|
4484
|
+
* @param handler - Route handler (function, string, or controller tuple)
|
|
4485
|
+
* @returns The created route instance
|
|
4056
4486
|
*/
|
|
4057
4487
|
post(pattern, handler) {
|
|
4058
4488
|
return this.route(pattern, ["POST"], handler);
|
|
4059
4489
|
}
|
|
4060
4490
|
/**
|
|
4061
4491
|
* Define `PUT` route
|
|
4492
|
+
* @param pattern - The route pattern
|
|
4493
|
+
* @param handler - Route handler (function, string, or controller tuple)
|
|
4494
|
+
* @returns The created route instance
|
|
4062
4495
|
*/
|
|
4063
4496
|
put(pattern, handler) {
|
|
4064
4497
|
return this.route(pattern, ["PUT"], handler);
|
|
4065
4498
|
}
|
|
4066
4499
|
/**
|
|
4067
4500
|
* Define `PATCH` route
|
|
4501
|
+
* @param pattern - The route pattern
|
|
4502
|
+
* @param handler - Route handler (function, string, or controller tuple)
|
|
4503
|
+
* @returns The created route instance
|
|
4068
4504
|
*/
|
|
4069
4505
|
patch(pattern, handler) {
|
|
4070
4506
|
return this.route(pattern, ["PATCH"], handler);
|
|
4071
4507
|
}
|
|
4072
4508
|
/**
|
|
4073
4509
|
* Define `DELETE` route
|
|
4510
|
+
* @param pattern - The route pattern
|
|
4511
|
+
* @param handler - Route handler (function, string, or controller tuple)
|
|
4512
|
+
* @returns The created route instance
|
|
4074
4513
|
*/
|
|
4075
4514
|
delete(pattern, handler) {
|
|
4076
4515
|
return this.route(pattern, ["DELETE"], handler);
|
|
@@ -4078,6 +4517,8 @@ var Router = class {
|
|
|
4078
4517
|
/**
|
|
4079
4518
|
* Creates a group of routes. A route group can apply transforms
|
|
4080
4519
|
* to routes in bulk
|
|
4520
|
+
* @param callback - Function that defines routes within the group
|
|
4521
|
+
* @returns The created route group instance
|
|
4081
4522
|
*/
|
|
4082
4523
|
group(callback) {
|
|
4083
4524
|
const group = new RouteGroup([]);
|
|
@@ -4089,6 +4530,9 @@ var Router = class {
|
|
|
4089
4530
|
}
|
|
4090
4531
|
/**
|
|
4091
4532
|
* Registers a route resource with conventional set of routes
|
|
4533
|
+
* @param resource - The resource name
|
|
4534
|
+
* @param controller - Controller to handle the resource
|
|
4535
|
+
* @returns The created route resource instance
|
|
4092
4536
|
*/
|
|
4093
4537
|
resource(resource, controller) {
|
|
4094
4538
|
const resourceInstance = new RouteResource(this.#app, this.#middleware, {
|
|
@@ -4102,6 +4546,9 @@ var Router = class {
|
|
|
4102
4546
|
}
|
|
4103
4547
|
/**
|
|
4104
4548
|
* Register a route resource with shallow nested routes.
|
|
4549
|
+
* @param resource - The resource name
|
|
4550
|
+
* @param controller - Controller to handle the resource
|
|
4551
|
+
* @returns The created route resource instance
|
|
4105
4552
|
*/
|
|
4106
4553
|
shallowResource(resource, controller) {
|
|
4107
4554
|
const resourceInstance = new RouteResource(this.#app, this.#middleware, {
|
|
@@ -4115,6 +4562,8 @@ var Router = class {
|
|
|
4115
4562
|
}
|
|
4116
4563
|
/**
|
|
4117
4564
|
* Returns a brisk route instance for a given URL pattern
|
|
4565
|
+
* @param pattern - The route pattern
|
|
4566
|
+
* @returns The created brisk route instance
|
|
4118
4567
|
*/
|
|
4119
4568
|
on(pattern) {
|
|
4120
4569
|
const briskRoute = new BriskRoute(this.#app, this.#middleware, {
|
|
@@ -4127,6 +4576,9 @@ var Router = class {
|
|
|
4127
4576
|
/**
|
|
4128
4577
|
* Define matcher for a given param. The global params are applied
|
|
4129
4578
|
* on all the routes (unless overridden at the route level).
|
|
4579
|
+
* @param param - The parameter name to match
|
|
4580
|
+
* @param matcher - The matcher pattern (RegExp, string, or RouteMatcher)
|
|
4581
|
+
* @returns Current Router instance for method chaining
|
|
4130
4582
|
*/
|
|
4131
4583
|
where(param, matcher) {
|
|
4132
4584
|
if (typeof matcher === "string") {
|
|
@@ -4179,6 +4631,11 @@ var Router = class {
|
|
|
4179
4631
|
*
|
|
4180
4632
|
* When "disableLegacyLookup" is set, the lookup will be performed
|
|
4181
4633
|
* only using the route name
|
|
4634
|
+
* @param routeIdentifier - Route name, pattern, or controller reference
|
|
4635
|
+
* @param domain - Optional domain to search within
|
|
4636
|
+
* @param method - Optional HTTP method to filter by
|
|
4637
|
+
* @param disableLegacyLookup - Whether to disable legacy lookup strategies
|
|
4638
|
+
* @returns Found route or null if not found
|
|
4182
4639
|
*/
|
|
4183
4640
|
find(routeIdentifier, domain, method, disableLegacyLookup) {
|
|
4184
4641
|
if (!domain) {
|
|
@@ -4220,6 +4677,12 @@ var Router = class {
|
|
|
4220
4677
|
*
|
|
4221
4678
|
* When "disableLegacyLookup" is set, the lookup will be performed
|
|
4222
4679
|
* only using the route name
|
|
4680
|
+
* @param routeIdentifier - Route name, pattern, or controller reference
|
|
4681
|
+
* @param domain - Optional domain to search within
|
|
4682
|
+
* @param method - Optional HTTP method to filter by
|
|
4683
|
+
* @param disableLegacyLookup - Whether to disable legacy lookup strategies
|
|
4684
|
+
* @returns Found route
|
|
4685
|
+
* @throws Error when route is not found
|
|
4223
4686
|
*/
|
|
4224
4687
|
findOrFail(routeIdentifier, domain, method, disableLegacyLookup) {
|
|
4225
4688
|
const route = this.find(routeIdentifier, domain, method, disableLegacyLookup);
|
|
@@ -4236,12 +4699,18 @@ var Router = class {
|
|
|
4236
4699
|
* When "followLookupStrategy" is enabled, the lookup will be performed
|
|
4237
4700
|
* on the basis of the lookup strategy enabled via the "lookupStrategies"
|
|
4238
4701
|
* method. The default lookupStrategy is "name" and "pattern".
|
|
4702
|
+
* @param routeIdentifier - Route name, pattern, or controller reference
|
|
4703
|
+
* @param domain - Optional domain to search within
|
|
4704
|
+
* @param method - Optional HTTP method to filter by
|
|
4705
|
+
* @param followLookupStrategy - Whether to follow the configured lookup strategy
|
|
4706
|
+
* @returns True if route exists, false otherwise
|
|
4239
4707
|
*/
|
|
4240
4708
|
has(routeIdentifier, domain, method, followLookupStrategy) {
|
|
4241
4709
|
return !!this.find(routeIdentifier, domain, method, followLookupStrategy);
|
|
4242
4710
|
}
|
|
4243
4711
|
/**
|
|
4244
4712
|
* Returns a list of routes grouped by their domain names
|
|
4713
|
+
* @returns Object mapping domain names to route arrays
|
|
4245
4714
|
*/
|
|
4246
4715
|
toJSON() {
|
|
4247
4716
|
return this.routes;
|
|
@@ -4250,6 +4719,8 @@ var Router = class {
|
|
|
4250
4719
|
* Generates types for the URL builder. These types must
|
|
4251
4720
|
* be written inside a file for the URL builder to
|
|
4252
4721
|
* pick them up.
|
|
4722
|
+
* @param indentation - Indentation level for generated types
|
|
4723
|
+
* @returns Generated TypeScript types as string
|
|
4253
4724
|
*/
|
|
4254
4725
|
generateTypes(indentation = 0) {
|
|
4255
4726
|
const routesList = {};
|
|
@@ -4318,6 +4789,11 @@ var Router = class {
|
|
|
4318
4789
|
}
|
|
4319
4790
|
/**
|
|
4320
4791
|
* Find route for a given URL, method and optionally domain
|
|
4792
|
+
* @param uri - The URI to match
|
|
4793
|
+
* @param method - HTTP method
|
|
4794
|
+
* @param shouldDecodeParam - Whether to decode parameters
|
|
4795
|
+
* @param hostname - Optional hostname for domain matching
|
|
4796
|
+
* @returns Matched route or null if no match found
|
|
4321
4797
|
*/
|
|
4322
4798
|
match(uri, method, shouldDecodeParam, hostname) {
|
|
4323
4799
|
const matchingDomain = this.#store.matchDomain(hostname);
|
|
@@ -4328,9 +4804,7 @@ var Router = class {
|
|
|
4328
4804
|
}
|
|
4329
4805
|
/**
|
|
4330
4806
|
* Create URL builder instance.
|
|
4331
|
-
* @deprecated
|
|
4332
|
-
*
|
|
4333
|
-
* Instead use "@adonisjs/core/services/url_builder" instead
|
|
4807
|
+
* @deprecated Instead use "@adonisjs/core/services/url_builder" instead
|
|
4334
4808
|
*/
|
|
4335
4809
|
builder() {
|
|
4336
4810
|
return new UrlBuilder(this);
|
|
@@ -4402,27 +4876,13 @@ import { RuntimeException as RuntimeException6 } from "@poppinss/utils/exception
|
|
|
4402
4876
|
// src/http_context/local_storage.ts
|
|
4403
4877
|
import { AsyncLocalStorage } from "async_hooks";
|
|
4404
4878
|
var asyncLocalStorage = {
|
|
4405
|
-
/**
|
|
4406
|
-
* Check if the async local storage for the HTTP
|
|
4407
|
-
* context is enabled or not
|
|
4408
|
-
*/
|
|
4409
4879
|
isEnabled: false,
|
|
4410
|
-
/**
|
|
4411
|
-
* HTTP context storage instance for the current scope
|
|
4412
|
-
*/
|
|
4413
4880
|
storage: null,
|
|
4414
|
-
/**
|
|
4415
|
-
* Create the storage instance. This method must be called only
|
|
4416
|
-
* once.
|
|
4417
|
-
*/
|
|
4418
4881
|
create() {
|
|
4419
4882
|
this.isEnabled = true;
|
|
4420
4883
|
this.storage = new AsyncLocalStorage();
|
|
4421
4884
|
return this.storage;
|
|
4422
4885
|
},
|
|
4423
|
-
/**
|
|
4424
|
-
* Destroy the create storage instance
|
|
4425
|
-
*/
|
|
4426
4886
|
destroy() {
|
|
4427
4887
|
this.isEnabled = false;
|
|
4428
4888
|
this.storage = null;
|
|
@@ -4431,6 +4891,14 @@ var asyncLocalStorage = {
|
|
|
4431
4891
|
|
|
4432
4892
|
// src/http_context/main.ts
|
|
4433
4893
|
var HttpContext = class extends Macroable8 {
|
|
4894
|
+
/**
|
|
4895
|
+
* Creates a new HttpContext instance
|
|
4896
|
+
*
|
|
4897
|
+
* @param {Request} request - The HTTP request instance
|
|
4898
|
+
* @param {Response} response - The HTTP response instance
|
|
4899
|
+
* @param {Logger} logger - The logger instance
|
|
4900
|
+
* @param {ContainerResolver<any>} containerResolver - The IoC container resolver
|
|
4901
|
+
*/
|
|
4434
4902
|
constructor(request, response, logger, containerResolver) {
|
|
4435
4903
|
super();
|
|
4436
4904
|
this.request = request;
|
|
@@ -4517,13 +4985,30 @@ import { moduleCaller as moduleCaller2, moduleImporter as moduleImporter4 } from
|
|
|
4517
4985
|
// src/qs.ts
|
|
4518
4986
|
import { parse as parse2, stringify } from "qs";
|
|
4519
4987
|
var Qs = class {
|
|
4988
|
+
/**
|
|
4989
|
+
* Configuration object containing parse and stringify options for query strings
|
|
4990
|
+
*/
|
|
4520
4991
|
#config;
|
|
4992
|
+
/**
|
|
4993
|
+
* Creates a new query string parser instance with the provided configuration
|
|
4994
|
+
* @param config - Configuration object with parse and stringify options
|
|
4995
|
+
*/
|
|
4521
4996
|
constructor(config) {
|
|
4522
4997
|
this.#config = config;
|
|
4523
4998
|
}
|
|
4999
|
+
/**
|
|
5000
|
+
* Parses a query string into a JavaScript object using the configured options
|
|
5001
|
+
* @param value - Query string to parse (e.g., "foo=bar&baz=qux")
|
|
5002
|
+
* @returns Parsed object representation of the query string
|
|
5003
|
+
*/
|
|
4524
5004
|
parse = (value) => {
|
|
4525
5005
|
return parse2(value, this.#config.parse);
|
|
4526
5006
|
};
|
|
5007
|
+
/**
|
|
5008
|
+
* Converts a JavaScript object into a query string using the configured options
|
|
5009
|
+
* @param value - Object to convert to query string
|
|
5010
|
+
* @returns Stringified query string representation of the object
|
|
5011
|
+
*/
|
|
4527
5012
|
stringify = (value) => {
|
|
4528
5013
|
return stringify(value, this.#config.stringify);
|
|
4529
5014
|
};
|
|
@@ -4566,7 +5051,7 @@ function middlewareHandler(resolver, ctx) {
|
|
|
4566
5051
|
debug_default("executing middleware %s", fn.name);
|
|
4567
5052
|
return httpMiddleware.tracePromise(
|
|
4568
5053
|
fn.handle,
|
|
4569
|
-
fn,
|
|
5054
|
+
httpMiddleware.hasSubscribers ? { middleware: fn } : void 0,
|
|
4570
5055
|
void 0,
|
|
4571
5056
|
resolver,
|
|
4572
5057
|
ctx,
|
|
@@ -4577,9 +5062,12 @@ function middlewareHandler(resolver, ctx) {
|
|
|
4577
5062
|
|
|
4578
5063
|
// src/server/main.ts
|
|
4579
5064
|
var Server = class {
|
|
5065
|
+
/**
|
|
5066
|
+
* Flag indicating whether the server has been booted and initialized
|
|
5067
|
+
*/
|
|
4580
5068
|
#booted = false;
|
|
4581
5069
|
/**
|
|
4582
|
-
*
|
|
5070
|
+
* Built-in fallback error handler used when no custom handler is registered
|
|
4583
5071
|
*/
|
|
4584
5072
|
#defaultErrorHandler = {
|
|
4585
5073
|
report() {
|
|
@@ -4589,86 +5077,88 @@ var Server = class {
|
|
|
4589
5077
|
}
|
|
4590
5078
|
};
|
|
4591
5079
|
/**
|
|
4592
|
-
* Logger instance
|
|
4593
|
-
* to the context to have request specific
|
|
4594
|
-
* logging capabilities.
|
|
5080
|
+
* Logger instance for server-level logging (child loggers are created per request)
|
|
4595
5081
|
*/
|
|
4596
5082
|
#logger;
|
|
4597
5083
|
/**
|
|
4598
|
-
*
|
|
5084
|
+
* Lazy import reference to the custom error handler class
|
|
4599
5085
|
*/
|
|
4600
5086
|
#errorHandler;
|
|
4601
5087
|
/**
|
|
4602
|
-
*
|
|
4603
|
-
* handler class.
|
|
5088
|
+
* Active error handler instance (either custom or default)
|
|
4604
5089
|
*/
|
|
4605
5090
|
#resolvedErrorHandler = this.#defaultErrorHandler;
|
|
4606
5091
|
/**
|
|
4607
|
-
*
|
|
5092
|
+
* Event emitter for HTTP server lifecycle events
|
|
4608
5093
|
*/
|
|
4609
5094
|
#emitter;
|
|
4610
5095
|
/**
|
|
4611
|
-
*
|
|
5096
|
+
* AdonisJS application instance providing IoC container and configuration
|
|
4612
5097
|
*/
|
|
4613
5098
|
#app;
|
|
4614
5099
|
/**
|
|
4615
|
-
*
|
|
5100
|
+
* Encryption service for secure cookie handling and data encryption
|
|
4616
5101
|
*/
|
|
4617
5102
|
#encryption;
|
|
4618
5103
|
/**
|
|
4619
|
-
* Server
|
|
5104
|
+
* Server configuration settings including timeouts, middleware options, etc.
|
|
4620
5105
|
*/
|
|
4621
5106
|
#config;
|
|
4622
5107
|
/**
|
|
4623
|
-
* Query string parser
|
|
5108
|
+
* Query string parser instance for URL parameter processing
|
|
4624
5109
|
*/
|
|
4625
5110
|
#qsParser;
|
|
4626
5111
|
/**
|
|
4627
|
-
*
|
|
5112
|
+
* Compiled middleware stack that executes on every incoming HTTP request
|
|
4628
5113
|
*/
|
|
4629
5114
|
#serverMiddlewareStack;
|
|
4630
5115
|
/**
|
|
4631
|
-
*
|
|
5116
|
+
* Router instance responsible for route registration and matching
|
|
4632
5117
|
*/
|
|
4633
5118
|
#router;
|
|
4634
5119
|
/**
|
|
4635
|
-
* Reference to the underlying Node HTTP server
|
|
5120
|
+
* Reference to the underlying Node.js HTTP or HTTPS server instance
|
|
4636
5121
|
*/
|
|
4637
5122
|
#nodeHttpServer;
|
|
4638
5123
|
/**
|
|
4639
|
-
*
|
|
5124
|
+
* Collection of registered global middleware before compilation
|
|
4640
5125
|
*/
|
|
4641
5126
|
#middleware = [];
|
|
4642
5127
|
/**
|
|
4643
|
-
*
|
|
4644
|
-
*
|
|
4645
|
-
* registered error handler.
|
|
4646
|
-
*
|
|
4647
|
-
* We share this with the route middleware pipeline as well,
|
|
4648
|
-
* so that it does not throw any exceptions
|
|
5128
|
+
* Error responder function that handles exceptions in middleware and routes.
|
|
5129
|
+
* Reports errors and delegates handling to the configured error handler.
|
|
4649
5130
|
*/
|
|
4650
5131
|
#requestErrorResponder = (error, ctx) => {
|
|
4651
5132
|
this.#resolvedErrorHandler.report(error, ctx);
|
|
4652
5133
|
return httpExceptionHandler.tracePromise(
|
|
4653
5134
|
this.#resolvedErrorHandler.handle,
|
|
4654
5135
|
void 0,
|
|
4655
|
-
|
|
5136
|
+
this.#resolvedErrorHandler,
|
|
4656
5137
|
error,
|
|
4657
5138
|
ctx
|
|
4658
5139
|
);
|
|
4659
5140
|
};
|
|
4660
5141
|
/**
|
|
4661
|
-
*
|
|
5142
|
+
* Indicates whether the server has completed its boot process
|
|
4662
5143
|
*/
|
|
4663
5144
|
get booted() {
|
|
4664
5145
|
return this.#booted;
|
|
4665
5146
|
}
|
|
4666
5147
|
/**
|
|
4667
|
-
*
|
|
5148
|
+
* Indicates whether async local storage is enabled for request context
|
|
4668
5149
|
*/
|
|
4669
5150
|
get usingAsyncLocalStorage() {
|
|
4670
5151
|
return asyncLocalStorage.isEnabled;
|
|
4671
5152
|
}
|
|
5153
|
+
/**
|
|
5154
|
+
* Creates a new Server instance
|
|
5155
|
+
*
|
|
5156
|
+
* @param app - AdonisJS application instance
|
|
5157
|
+
* @param encryption - Encryption service for secure operations
|
|
5158
|
+
* @param emitter - Event emitter for server lifecycle events
|
|
5159
|
+
* @param logger - Logger instance for server operations
|
|
5160
|
+
* @param config - Server configuration settings
|
|
5161
|
+
*/
|
|
4672
5162
|
constructor(app, encryption, emitter, logger, config) {
|
|
4673
5163
|
this.#app = app;
|
|
4674
5164
|
this.#emitter = emitter;
|
|
@@ -4681,7 +5171,7 @@ var Server = class {
|
|
|
4681
5171
|
debug_default("server config: %O", this.#config);
|
|
4682
5172
|
}
|
|
4683
5173
|
/**
|
|
4684
|
-
*
|
|
5174
|
+
* Initializes or destroys async local storage based on configuration
|
|
4685
5175
|
*/
|
|
4686
5176
|
#createAsyncLocalStore() {
|
|
4687
5177
|
if (this.#config.useAsyncLocalStorage) {
|
|
@@ -4692,7 +5182,7 @@ var Server = class {
|
|
|
4692
5182
|
}
|
|
4693
5183
|
}
|
|
4694
5184
|
/**
|
|
4695
|
-
*
|
|
5185
|
+
* Compiles registered middleware into a frozen middleware stack for execution
|
|
4696
5186
|
*/
|
|
4697
5187
|
#createServerMiddlewareStack() {
|
|
4698
5188
|
this.#serverMiddlewareStack = new Middleware2();
|
|
@@ -4701,7 +5191,11 @@ var Server = class {
|
|
|
4701
5191
|
this.#middleware = [];
|
|
4702
5192
|
}
|
|
4703
5193
|
/**
|
|
4704
|
-
*
|
|
5194
|
+
* Processes an HTTP request through the middleware pipeline and routing
|
|
5195
|
+
*
|
|
5196
|
+
* @param ctx - HTTP context containing request/response objects
|
|
5197
|
+
* @param resolver - Container resolver for dependency injection
|
|
5198
|
+
* @returns Promise that resolves when request processing is complete
|
|
4705
5199
|
*/
|
|
4706
5200
|
#handleRequest(ctx, resolver) {
|
|
4707
5201
|
return this.#serverMiddlewareStack.runner().errorHandler((error) => this.#requestErrorResponder(error, ctx)).finalHandler(routeFinder(this.#router, resolver, ctx, this.#requestErrorResponder)).run(middlewareHandler(resolver, ctx)).catch((error) => {
|
|
@@ -4710,7 +5204,10 @@ var Server = class {
|
|
|
4710
5204
|
}).finally(writeResponse(ctx));
|
|
4711
5205
|
}
|
|
4712
5206
|
/**
|
|
4713
|
-
* Creates a pipeline
|
|
5207
|
+
* Creates a testing middleware pipeline for unit/integration testing
|
|
5208
|
+
*
|
|
5209
|
+
* @param middleware - Array of middleware classes to include in pipeline
|
|
5210
|
+
* @returns TestingMiddlewarePipeline instance for test execution
|
|
4714
5211
|
*/
|
|
4715
5212
|
pipeline(middleware) {
|
|
4716
5213
|
const middlewareStack = new Middleware2();
|
|
@@ -4739,9 +5236,10 @@ var Server = class {
|
|
|
4739
5236
|
};
|
|
4740
5237
|
}
|
|
4741
5238
|
/**
|
|
4742
|
-
*
|
|
4743
|
-
*
|
|
4744
|
-
* of middleware
|
|
5239
|
+
* Registers global middleware to run on all incoming HTTP requests
|
|
5240
|
+
*
|
|
5241
|
+
* @param middleware - Array of lazy-imported middleware classes
|
|
5242
|
+
* @returns The Server instance for method chaining
|
|
4745
5243
|
*/
|
|
4746
5244
|
use(middleware) {
|
|
4747
5245
|
middleware.forEach(
|
|
@@ -4753,18 +5251,22 @@ var Server = class {
|
|
|
4753
5251
|
return this;
|
|
4754
5252
|
}
|
|
4755
5253
|
/**
|
|
4756
|
-
*
|
|
4757
|
-
*
|
|
5254
|
+
* Registers a custom error handler for HTTP request processing
|
|
5255
|
+
*
|
|
5256
|
+
* @param handler - Lazy import of the error handler class
|
|
5257
|
+
* @returns The Server instance for method chaining
|
|
4758
5258
|
*/
|
|
4759
5259
|
errorHandler(handler) {
|
|
4760
5260
|
this.#errorHandler = handler;
|
|
4761
5261
|
return this;
|
|
4762
5262
|
}
|
|
4763
5263
|
/**
|
|
4764
|
-
*
|
|
5264
|
+
* Initializes the server by compiling middleware, committing routes, and resolving handlers
|
|
4765
5265
|
*
|
|
4766
|
-
*
|
|
4767
|
-
* -
|
|
5266
|
+
* Performs the following operations:
|
|
5267
|
+
* - Compiles the middleware stack
|
|
5268
|
+
* - Commits registered routes to the router
|
|
5269
|
+
* - Resolves and instantiates the custom error handler
|
|
4768
5270
|
*/
|
|
4769
5271
|
async boot() {
|
|
4770
5272
|
if (this.#booted) {
|
|
@@ -4783,7 +5285,9 @@ var Server = class {
|
|
|
4783
5285
|
this.#booted = true;
|
|
4784
5286
|
}
|
|
4785
5287
|
/**
|
|
4786
|
-
*
|
|
5288
|
+
* Configures the underlying Node.js HTTP/HTTPS server with timeout settings
|
|
5289
|
+
*
|
|
5290
|
+
* @param server - Node.js HTTP or HTTPS server instance
|
|
4787
5291
|
*/
|
|
4788
5292
|
setNodeServer(server) {
|
|
4789
5293
|
server.timeout = this.#config.timeout ?? server.timeout;
|
|
@@ -4793,33 +5297,48 @@ var Server = class {
|
|
|
4793
5297
|
this.#nodeHttpServer = server;
|
|
4794
5298
|
}
|
|
4795
5299
|
/**
|
|
4796
|
-
*
|
|
4797
|
-
*
|
|
5300
|
+
* Gets the underlying Node.js HTTP/HTTPS server instance
|
|
5301
|
+
*
|
|
5302
|
+
* @returns The configured server instance or undefined if not set
|
|
4798
5303
|
*/
|
|
4799
5304
|
getNodeServer() {
|
|
4800
5305
|
return this.#nodeHttpServer;
|
|
4801
5306
|
}
|
|
4802
5307
|
/**
|
|
4803
|
-
*
|
|
4804
|
-
*
|
|
5308
|
+
* Gets the router instance used for route registration and matching
|
|
5309
|
+
*
|
|
5310
|
+
* @returns The Router instance
|
|
4805
5311
|
*/
|
|
4806
5312
|
getRouter() {
|
|
4807
5313
|
return this.#router;
|
|
4808
5314
|
}
|
|
4809
5315
|
/**
|
|
4810
|
-
* Creates
|
|
5316
|
+
* Creates a Request instance from Node.js request/response objects
|
|
5317
|
+
*
|
|
5318
|
+
* @param req - Node.js IncomingMessage
|
|
5319
|
+
* @param res - Node.js ServerResponse
|
|
5320
|
+
* @returns New Request instance
|
|
4811
5321
|
*/
|
|
4812
5322
|
createRequest(req, res) {
|
|
4813
5323
|
return new Request(req, res, this.#encryption, this.#config, this.#qsParser);
|
|
4814
5324
|
}
|
|
4815
5325
|
/**
|
|
4816
|
-
* Creates
|
|
5326
|
+
* Creates a Response instance from Node.js request/response objects
|
|
5327
|
+
*
|
|
5328
|
+
* @param req - Node.js IncomingMessage
|
|
5329
|
+
* @param res - Node.js ServerResponse
|
|
5330
|
+
* @returns New Response instance
|
|
4817
5331
|
*/
|
|
4818
5332
|
createResponse(req, res) {
|
|
4819
5333
|
return new Response(req, res, this.#encryption, this.#config, this.#router, this.#qsParser);
|
|
4820
5334
|
}
|
|
4821
5335
|
/**
|
|
4822
|
-
* Creates an instance
|
|
5336
|
+
* Creates an HttpContext instance with request-specific logger
|
|
5337
|
+
*
|
|
5338
|
+
* @param request - Request instance
|
|
5339
|
+
* @param response - Response instance
|
|
5340
|
+
* @param resolver - Container resolver for dependency injection
|
|
5341
|
+
* @returns New HttpContext instance
|
|
4823
5342
|
*/
|
|
4824
5343
|
createHttpContext(request, response, resolver) {
|
|
4825
5344
|
return new HttpContext(
|
|
@@ -4830,13 +5349,19 @@ var Server = class {
|
|
|
4830
5349
|
);
|
|
4831
5350
|
}
|
|
4832
5351
|
/**
|
|
4833
|
-
*
|
|
5352
|
+
* Gets the list of registered global middleware
|
|
5353
|
+
*
|
|
5354
|
+
* @returns Array of parsed global middleware
|
|
4834
5355
|
*/
|
|
4835
5356
|
getMiddlewareList() {
|
|
4836
5357
|
return this.#serverMiddlewareStack ? Array.from(this.#serverMiddlewareStack.all()) : [...this.#middleware];
|
|
4837
5358
|
}
|
|
4838
5359
|
/**
|
|
4839
|
-
*
|
|
5360
|
+
* Handles an incoming HTTP request by creating context and processing through pipeline
|
|
5361
|
+
*
|
|
5362
|
+
* @param req - Node.js IncomingMessage
|
|
5363
|
+
* @param res - Node.js ServerResponse
|
|
5364
|
+
* @returns Promise that resolves when request processing is complete
|
|
4840
5365
|
*/
|
|
4841
5366
|
handle(req, res) {
|
|
4842
5367
|
const hasRequestListener = this.#emitter.hasListeners("http:request_completed");
|
|
@@ -4858,10 +5383,22 @@ var Server = class {
|
|
|
4858
5383
|
if (this.usingAsyncLocalStorage) {
|
|
4859
5384
|
return asyncLocalStorage.storage.run(
|
|
4860
5385
|
ctx,
|
|
4861
|
-
() => httpRequest.tracePromise(
|
|
5386
|
+
() => httpRequest.tracePromise(
|
|
5387
|
+
this.#handleRequest,
|
|
5388
|
+
httpRequest.hasSubscribers ? { ctx } : void 0,
|
|
5389
|
+
this,
|
|
5390
|
+
ctx,
|
|
5391
|
+
resolver
|
|
5392
|
+
)
|
|
4862
5393
|
);
|
|
4863
5394
|
}
|
|
4864
|
-
return httpRequest.tracePromise(
|
|
5395
|
+
return httpRequest.tracePromise(
|
|
5396
|
+
this.#handleRequest,
|
|
5397
|
+
httpRequest.hasSubscribers ? { ctx } : void 0,
|
|
5398
|
+
this,
|
|
5399
|
+
ctx,
|
|
5400
|
+
resolver
|
|
5401
|
+
);
|
|
4865
5402
|
}
|
|
4866
5403
|
};
|
|
4867
5404
|
|
|
@@ -4928,6 +5465,7 @@ export {
|
|
|
4928
5465
|
E_HTTP_REQUEST_ABORTED,
|
|
4929
5466
|
errors_exports,
|
|
4930
5467
|
CookieClient,
|
|
5468
|
+
CookieParser,
|
|
4931
5469
|
canWriteResponseBody,
|
|
4932
5470
|
tracing_channels_exports,
|
|
4933
5471
|
Route,
|
|
@@ -4938,6 +5476,7 @@ export {
|
|
|
4938
5476
|
Request,
|
|
4939
5477
|
Redirect,
|
|
4940
5478
|
ResponseStatus,
|
|
5479
|
+
CookieSerializer,
|
|
4941
5480
|
Response,
|
|
4942
5481
|
Qs,
|
|
4943
5482
|
Router,
|