@adonisjs/http-server 8.0.0-next.7 → 8.0.0-next.9
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-CVZAIRWJ.js +1222 -0
- package/build/{chunk-3XEJZ4S4.js → chunk-JD6QW4NQ.js} +252 -1310
- package/build/factories/main.js +2 -2
- package/build/index.d.ts +1 -0
- package/build/index.js +10 -7
- package/build/src/helpers.d.ts +19 -0
- package/build/src/helpers.js +3 -1
- package/package.json +11 -11
- package/build/chunk-NQNHMINZ.js +0 -135
|
@@ -1,12 +1,56 @@
|
|
|
1
1
|
import {
|
|
2
|
+
BriskRoute,
|
|
3
|
+
Route,
|
|
4
|
+
RouteGroup,
|
|
5
|
+
RouteResource,
|
|
2
6
|
__export,
|
|
3
7
|
createSignedURL,
|
|
4
8
|
createURL,
|
|
9
|
+
debug_default,
|
|
5
10
|
default as default2,
|
|
6
11
|
default2 as default3,
|
|
12
|
+
httpExceptionHandler,
|
|
13
|
+
httpMiddleware,
|
|
14
|
+
httpRequest,
|
|
15
|
+
httpResponseSerializer,
|
|
7
16
|
parseRoute,
|
|
8
|
-
|
|
9
|
-
|
|
17
|
+
safeDecodeURI,
|
|
18
|
+
serializeCookie,
|
|
19
|
+
toRoutesJSON,
|
|
20
|
+
trustProxy
|
|
21
|
+
} from "./chunk-CVZAIRWJ.js";
|
|
22
|
+
|
|
23
|
+
// src/qs.ts
|
|
24
|
+
import { parse, stringify } from "qs";
|
|
25
|
+
var Qs = class {
|
|
26
|
+
/**
|
|
27
|
+
* Configuration object containing parse and stringify options for query strings
|
|
28
|
+
*/
|
|
29
|
+
#config;
|
|
30
|
+
/**
|
|
31
|
+
* Creates a new query string parser instance with the provided configuration
|
|
32
|
+
* @param config - Configuration object with parse and stringify options
|
|
33
|
+
*/
|
|
34
|
+
constructor(config) {
|
|
35
|
+
this.#config = config;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Parses a query string into a JavaScript object using the configured options
|
|
39
|
+
* @param value - Query string to parse (e.g., "foo=bar&baz=qux")
|
|
40
|
+
* @returns Parsed object representation of the query string
|
|
41
|
+
*/
|
|
42
|
+
parse = (value) => {
|
|
43
|
+
return parse(value, this.#config.parse);
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* Converts a JavaScript object into a query string using the configured options
|
|
47
|
+
* @param value - Object to convert to query string
|
|
48
|
+
* @returns Stringified query string representation of the object
|
|
49
|
+
*/
|
|
50
|
+
stringify = (value) => {
|
|
51
|
+
return stringify(value, this.#config.stringify);
|
|
52
|
+
};
|
|
53
|
+
};
|
|
10
54
|
|
|
11
55
|
// src/errors.ts
|
|
12
56
|
var errors_exports = {};
|
|
@@ -112,1282 +156,220 @@ var CookieClient = class {
|
|
|
112
156
|
/**
|
|
113
157
|
* Private encryption instance used for signing and encrypting cookies
|
|
114
158
|
*/
|
|
115
|
-
#encryption;
|
|
116
|
-
/**
|
|
117
|
-
* Create a new instance of CookieClient
|
|
118
|
-
*
|
|
119
|
-
* @param encryption - The encryption instance for cookie operations
|
|
120
|
-
*/
|
|
121
|
-
constructor(encryption) {
|
|
122
|
-
this.#encryption = encryption;
|
|
123
|
-
}
|
|
124
|
-
/**
|
|
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
|
|
130
|
-
*/
|
|
131
|
-
encrypt(key, value) {
|
|
132
|
-
return pack3(key, value, this.#encryption);
|
|
133
|
-
}
|
|
134
|
-
/**
|
|
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
|
|
140
|
-
*/
|
|
141
|
-
sign(key, value) {
|
|
142
|
-
return pack2(key, value, this.#encryption);
|
|
143
|
-
}
|
|
144
|
-
/**
|
|
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
|
|
151
|
-
*/
|
|
152
|
-
encode(_, value, stringify2 = true) {
|
|
153
|
-
return stringify2 ? pack(value) : value;
|
|
154
|
-
}
|
|
155
|
-
/**
|
|
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
|
|
161
|
-
*/
|
|
162
|
-
unsign(key, value) {
|
|
163
|
-
return canUnpack2(value) ? unpack2(key, value, this.#encryption) : null;
|
|
164
|
-
}
|
|
165
|
-
/**
|
|
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
|
|
171
|
-
*/
|
|
172
|
-
decrypt(key, value) {
|
|
173
|
-
return canUnpack3(value) ? unpack3(key, value, this.#encryption) : null;
|
|
174
|
-
}
|
|
175
|
-
/**
|
|
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
|
|
182
|
-
*/
|
|
183
|
-
decode(_, value, stringified = true) {
|
|
184
|
-
if (!stringified) {
|
|
185
|
-
return value;
|
|
186
|
-
}
|
|
187
|
-
return canUnpack(value) ? unpack(value) : null;
|
|
188
|
-
}
|
|
189
|
-
/**
|
|
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
|
|
195
|
-
*/
|
|
196
|
-
parse(key, value) {
|
|
197
|
-
if (canUnpack2(value)) {
|
|
198
|
-
return unpack2(key, value, this.#encryption);
|
|
199
|
-
}
|
|
200
|
-
if (canUnpack3(value)) {
|
|
201
|
-
return unpack3(key, value, this.#encryption);
|
|
202
|
-
}
|
|
203
|
-
if (canUnpack(value)) {
|
|
204
|
-
return unpack(value);
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
};
|
|
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
|
-
|
|
332
|
-
// src/tracing_channels.ts
|
|
333
|
-
var tracing_channels_exports = {};
|
|
334
|
-
__export(tracing_channels_exports, {
|
|
335
|
-
httpExceptionHandler: () => httpExceptionHandler,
|
|
336
|
-
httpMiddleware: () => httpMiddleware,
|
|
337
|
-
httpRequest: () => httpRequest,
|
|
338
|
-
httpResponseSerializer: () => httpResponseSerializer,
|
|
339
|
-
httpRouteHandler: () => httpRouteHandler
|
|
340
|
-
});
|
|
341
|
-
import diagnostics_channel from "diagnostics_channel";
|
|
342
|
-
var httpRequest = diagnostics_channel.tracingChannel("adonisjs.http.request");
|
|
343
|
-
var httpMiddleware = diagnostics_channel.tracingChannel("adonisjs.http.middleware");
|
|
344
|
-
var httpExceptionHandler = diagnostics_channel.tracingChannel(
|
|
345
|
-
"adonisjs.http.exception.handler"
|
|
346
|
-
);
|
|
347
|
-
var httpRouteHandler = diagnostics_channel.tracingChannel("adonisjs.http.route.handler");
|
|
348
|
-
var httpResponseSerializer = diagnostics_channel.tracingChannel(
|
|
349
|
-
"adonisjs.http.response.serializer"
|
|
350
|
-
);
|
|
351
|
-
|
|
352
|
-
// src/router/route.ts
|
|
353
|
-
import is from "@sindresorhus/is";
|
|
354
|
-
import Macroable4 from "@poppinss/macroable";
|
|
355
|
-
import Middleware from "@poppinss/middleware";
|
|
356
|
-
import { RuntimeException as RuntimeException2 } from "@poppinss/utils/exception";
|
|
357
|
-
import { moduleCaller, moduleImporter } from "@adonisjs/fold";
|
|
358
|
-
|
|
359
|
-
// src/debug.ts
|
|
360
|
-
import { debuglog } from "util";
|
|
361
|
-
var debug_default = debuglog("adonisjs:http");
|
|
362
|
-
|
|
363
|
-
// src/router/factories/use_return_value.ts
|
|
364
|
-
function canWriteResponseBody(value, ctx) {
|
|
365
|
-
return value !== void 0 && // Return value is explicitly defined
|
|
366
|
-
!ctx.response.hasLazyBody && // Lazy body is not set
|
|
367
|
-
value !== ctx.response;
|
|
368
|
-
}
|
|
369
|
-
function useReturnValue(ctx) {
|
|
370
|
-
return function(value) {
|
|
371
|
-
if (canWriteResponseBody(value, ctx)) {
|
|
372
|
-
ctx.response.send(value);
|
|
373
|
-
}
|
|
374
|
-
};
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
// src/router/executor.ts
|
|
378
|
-
function execute(route, resolver, ctx, errorResponder) {
|
|
379
|
-
return route.middleware.runner().errorHandler((error) => errorResponder(error, ctx)).finalHandler(() => {
|
|
380
|
-
if (typeof route.handler === "function") {
|
|
381
|
-
return httpRouteHandler.tracePromise(
|
|
382
|
-
($ctx) => Promise.resolve(route.handler($ctx)),
|
|
383
|
-
httpRouteHandler.hasSubscribers ? { route } : void 0,
|
|
384
|
-
void 0,
|
|
385
|
-
ctx
|
|
386
|
-
).then(useReturnValue(ctx));
|
|
387
|
-
}
|
|
388
|
-
return httpRouteHandler.tracePromise(
|
|
389
|
-
route.handler.handle,
|
|
390
|
-
httpRouteHandler.hasSubscribers ? { route } : void 0,
|
|
391
|
-
void 0,
|
|
392
|
-
resolver,
|
|
393
|
-
ctx
|
|
394
|
-
).then(useReturnValue(ctx));
|
|
395
|
-
}).run(async (middleware, next) => {
|
|
396
|
-
if (typeof middleware === "function") {
|
|
397
|
-
return httpMiddleware.tracePromise(
|
|
398
|
-
middleware,
|
|
399
|
-
httpMiddleware.hasSubscribers ? { middleware } : void 0,
|
|
400
|
-
void 0,
|
|
401
|
-
ctx,
|
|
402
|
-
next
|
|
403
|
-
);
|
|
404
|
-
}
|
|
405
|
-
return httpMiddleware.tracePromise(
|
|
406
|
-
middleware.handle,
|
|
407
|
-
httpMiddleware.hasSubscribers ? { middleware } : void 0,
|
|
408
|
-
void 0,
|
|
409
|
-
resolver,
|
|
410
|
-
ctx,
|
|
411
|
-
next,
|
|
412
|
-
middleware.args
|
|
413
|
-
);
|
|
414
|
-
});
|
|
415
|
-
}
|
|
416
|
-
|
|
417
|
-
// src/utils.ts
|
|
418
|
-
import Cache from "tmp-cache";
|
|
419
|
-
import { InvalidArgumentsException } from "@poppinss/utils/exception";
|
|
420
|
-
|
|
421
|
-
// src/router/group.ts
|
|
422
|
-
import Macroable3 from "@poppinss/macroable";
|
|
423
|
-
|
|
424
|
-
// src/router/brisk.ts
|
|
425
|
-
import Macroable from "@poppinss/macroable";
|
|
426
|
-
var BriskRoute = class extends Macroable {
|
|
427
|
-
/**
|
|
428
|
-
* Route pattern
|
|
429
|
-
*/
|
|
430
|
-
#pattern;
|
|
431
|
-
/**
|
|
432
|
-
* Matchers inherited from the router
|
|
433
|
-
*/
|
|
434
|
-
#globalMatchers;
|
|
435
|
-
/**
|
|
436
|
-
* Reference to the AdonisJS application
|
|
437
|
-
*/
|
|
438
|
-
#app;
|
|
439
|
-
/**
|
|
440
|
-
* Middleware registered on the router
|
|
441
|
-
*/
|
|
442
|
-
#routerMiddleware;
|
|
443
|
-
/**
|
|
444
|
-
* Reference to route instance. Set after `setHandler` is called
|
|
445
|
-
*/
|
|
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
|
-
*/
|
|
453
|
-
constructor(app, routerMiddleware, options) {
|
|
454
|
-
super();
|
|
455
|
-
this.#app = app;
|
|
456
|
-
this.#routerMiddleware = routerMiddleware;
|
|
457
|
-
this.#pattern = options.pattern;
|
|
458
|
-
this.#globalMatchers = options.globalMatchers;
|
|
459
|
-
}
|
|
460
|
-
/**
|
|
461
|
-
* Set handler for the brisk route
|
|
462
|
-
* @param handler - The route handler function
|
|
463
|
-
* @returns The created route instance
|
|
464
|
-
*/
|
|
465
|
-
setHandler(handler) {
|
|
466
|
-
this.route = new Route(this.#app, this.#routerMiddleware, {
|
|
467
|
-
pattern: this.#pattern,
|
|
468
|
-
globalMatchers: this.#globalMatchers,
|
|
469
|
-
methods: ["GET", "HEAD"],
|
|
470
|
-
handler
|
|
471
|
-
});
|
|
472
|
-
return this.route;
|
|
473
|
-
}
|
|
474
|
-
/**
|
|
475
|
-
* Redirects to a given route. Params from the original request will
|
|
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
|
|
479
|
-
*/
|
|
480
|
-
redirect(...args) {
|
|
481
|
-
const [identifier, params, options] = args;
|
|
482
|
-
function redirectsToRoute(ctx) {
|
|
483
|
-
const redirector = ctx.response.redirect();
|
|
484
|
-
if (options?.status) {
|
|
485
|
-
redirector.status(options.status);
|
|
486
|
-
}
|
|
487
|
-
return redirector.toRoute(identifier, params || ctx.params, options);
|
|
488
|
-
}
|
|
489
|
-
Object.defineProperty(redirectsToRoute, "listArgs", { value: identifier, writable: false });
|
|
490
|
-
return this.setHandler(redirectsToRoute);
|
|
491
|
-
}
|
|
492
|
-
/**
|
|
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
|
|
497
|
-
*/
|
|
498
|
-
redirectToPath(url, options) {
|
|
499
|
-
function redirectsToPath(ctx) {
|
|
500
|
-
const redirector = ctx.response.redirect();
|
|
501
|
-
if (options?.status) {
|
|
502
|
-
redirector.status(options.status);
|
|
503
|
-
}
|
|
504
|
-
return redirector.toPath(url);
|
|
505
|
-
}
|
|
506
|
-
Object.defineProperty(redirectsToPath, "listArgs", { value: url, writable: false });
|
|
507
|
-
return this.setHandler(redirectsToPath);
|
|
508
|
-
}
|
|
509
|
-
};
|
|
510
|
-
|
|
511
|
-
// src/router/resource.ts
|
|
512
|
-
import string from "@poppinss/utils/string";
|
|
513
|
-
import Macroable2 from "@poppinss/macroable";
|
|
514
|
-
import { RuntimeException } from "@poppinss/utils/exception";
|
|
515
|
-
var RouteResource = class extends Macroable2 {
|
|
516
|
-
/**
|
|
517
|
-
* Resource identifier. Nested resources are separated
|
|
518
|
-
* with a dot notation
|
|
519
|
-
*/
|
|
520
|
-
#resource;
|
|
521
|
-
/**
|
|
522
|
-
* The controller to handle resource routing requests
|
|
523
|
-
*/
|
|
524
|
-
#controller;
|
|
525
|
-
/**
|
|
526
|
-
* Is it a shallow resource? Shallow resources URLs do not have parent
|
|
527
|
-
* resource name and id once they can be identified with the id.
|
|
528
|
-
*/
|
|
529
|
-
#shallow = false;
|
|
530
|
-
/**
|
|
531
|
-
* Matchers inherited from the router
|
|
532
|
-
*/
|
|
533
|
-
#globalMatchers;
|
|
534
|
-
/**
|
|
535
|
-
* Reference to the AdonisJS application
|
|
536
|
-
*/
|
|
537
|
-
#app;
|
|
538
|
-
/**
|
|
539
|
-
* Middleware registered on the router
|
|
540
|
-
*/
|
|
541
|
-
#routerMiddleware;
|
|
542
|
-
/**
|
|
543
|
-
* Parameter names for the resources. Defaults to `id` for
|
|
544
|
-
* a singular resource and `resource_id` for nested
|
|
545
|
-
* resources.
|
|
546
|
-
*/
|
|
547
|
-
#params = {};
|
|
548
|
-
/**
|
|
549
|
-
* Base name for the routes. We suffix action names
|
|
550
|
-
* on top of the base name
|
|
551
|
-
*/
|
|
552
|
-
#routesBaseName;
|
|
553
|
-
/**
|
|
554
|
-
* A collection of routes instances that belongs to this resource
|
|
555
|
-
*/
|
|
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
|
-
*/
|
|
563
|
-
constructor(app, routerMiddleware, options) {
|
|
564
|
-
super();
|
|
565
|
-
this.#validateResourceName(options.resource);
|
|
566
|
-
this.#app = app;
|
|
567
|
-
this.#shallow = options.shallow;
|
|
568
|
-
this.#routerMiddleware = routerMiddleware;
|
|
569
|
-
this.#controller = options.controller;
|
|
570
|
-
this.#globalMatchers = options.globalMatchers;
|
|
571
|
-
this.#resource = this.#normalizeResourceName(options.resource);
|
|
572
|
-
this.#routesBaseName = this.#getRoutesBaseName();
|
|
573
|
-
this.#buildRoutes();
|
|
574
|
-
}
|
|
575
|
-
/**
|
|
576
|
-
* Normalizes the resource name to dropping leading and trailing
|
|
577
|
-
* slashes.
|
|
578
|
-
*/
|
|
579
|
-
#normalizeResourceName(resource) {
|
|
580
|
-
return resource.replace(/^\//, "").replace(/\/$/, "");
|
|
581
|
-
}
|
|
582
|
-
/**
|
|
583
|
-
* Ensure resource name is not an empty string
|
|
584
|
-
*/
|
|
585
|
-
#validateResourceName(resource) {
|
|
586
|
-
if (!resource || resource === "/") {
|
|
587
|
-
throw new RuntimeException(`Invalid resource name "${resource}"`);
|
|
588
|
-
}
|
|
589
|
-
}
|
|
590
|
-
/**
|
|
591
|
-
* Converting segments of a resource to snake case to
|
|
592
|
-
* make the route name.
|
|
593
|
-
*/
|
|
594
|
-
#getRoutesBaseName() {
|
|
595
|
-
return this.#resource.split(".").map((token) => string.snakeCase(token)).join(".");
|
|
596
|
-
}
|
|
597
|
-
/**
|
|
598
|
-
* Create a new route for the given pattern, methods and controller action
|
|
599
|
-
*/
|
|
600
|
-
#createRoute(pattern, methods, action) {
|
|
601
|
-
const route = new Route(this.#app, this.#routerMiddleware, {
|
|
602
|
-
pattern,
|
|
603
|
-
methods,
|
|
604
|
-
handler: typeof this.#controller === "string" ? `${this.#controller}.${action}` : [this.#controller, action],
|
|
605
|
-
globalMatchers: this.#globalMatchers
|
|
606
|
-
});
|
|
607
|
-
route.as(`${this.#routesBaseName}.${action}`);
|
|
608
|
-
this.routes.push(route);
|
|
609
|
-
}
|
|
610
|
-
/**
|
|
611
|
-
* Returns the `resource_id` name for a given resource. The
|
|
612
|
-
* resource name is converted to singular form and
|
|
613
|
-
* transformed to snake case.
|
|
614
|
-
*
|
|
615
|
-
* photos becomes photo_id
|
|
616
|
-
* users becomes user_id
|
|
617
|
-
*/
|
|
618
|
-
#getResourceId(resource) {
|
|
619
|
-
return `${string.snakeCase(string.singular(resource))}_id`;
|
|
620
|
-
}
|
|
621
|
-
/**
|
|
622
|
-
* Build routes for the given resource
|
|
623
|
-
*/
|
|
624
|
-
#buildRoutes() {
|
|
625
|
-
const resources = this.#resource.split(".");
|
|
626
|
-
const mainResource = resources.pop();
|
|
627
|
-
this.#params[mainResource] = ":id";
|
|
628
|
-
const baseURI = `${resources.map((resource) => {
|
|
629
|
-
const paramName = `:${this.#getResourceId(resource)}`;
|
|
630
|
-
this.#params[resource] = paramName;
|
|
631
|
-
return `${resource}/${paramName}`;
|
|
632
|
-
}).join("/")}/${mainResource}`;
|
|
633
|
-
this.#createRoute(baseURI, ["GET", "HEAD"], "index");
|
|
634
|
-
this.#createRoute(`${baseURI}/create`, ["GET", "HEAD"], "create");
|
|
635
|
-
this.#createRoute(baseURI, ["POST"], "store");
|
|
636
|
-
this.#createRoute(`${this.#shallow ? mainResource : baseURI}/:id`, ["GET", "HEAD"], "show");
|
|
637
|
-
this.#createRoute(`${this.#shallow ? mainResource : baseURI}/:id/edit`, ["GET", "HEAD"], "edit");
|
|
638
|
-
this.#createRoute(`${this.#shallow ? mainResource : baseURI}/:id`, ["PUT", "PATCH"], "update");
|
|
639
|
-
this.#createRoute(`${this.#shallow ? mainResource : baseURI}/:id`, ["DELETE"], "destroy");
|
|
640
|
-
}
|
|
641
|
-
/**
|
|
642
|
-
* Filter the routes based on their partial names
|
|
643
|
-
*/
|
|
644
|
-
#filter(names, inverse) {
|
|
645
|
-
const actions = Array.isArray(names) ? names : [names];
|
|
646
|
-
return this.routes.filter((route) => {
|
|
647
|
-
const match = actions.find((name) => route.getName().endsWith(name));
|
|
648
|
-
return inverse ? !match : match;
|
|
649
|
-
});
|
|
650
|
-
}
|
|
651
|
-
/**
|
|
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
|
|
655
|
-
*/
|
|
656
|
-
only(names) {
|
|
657
|
-
this.#filter(names, true).forEach((route) => route.markAsDeleted());
|
|
658
|
-
return this;
|
|
659
|
-
}
|
|
660
|
-
/**
|
|
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
|
|
664
|
-
*/
|
|
665
|
-
except(names) {
|
|
666
|
-
this.#filter(names, false).forEach((route) => route.markAsDeleted());
|
|
667
|
-
return this;
|
|
668
|
-
}
|
|
669
|
-
/**
|
|
670
|
-
* Register api only routes. The `create` and `edit` routes, which
|
|
671
|
-
* are meant to show forms will not be registered
|
|
672
|
-
* @returns Current RouteResource instance without create and edit actions
|
|
673
|
-
*/
|
|
674
|
-
apiOnly() {
|
|
675
|
-
return this.except(["create", "edit"]);
|
|
676
|
-
}
|
|
677
|
-
/**
|
|
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
|
|
682
|
-
*/
|
|
683
|
-
where(key, matcher) {
|
|
684
|
-
this.routes.forEach((route) => {
|
|
685
|
-
route.where(key, matcher);
|
|
686
|
-
});
|
|
687
|
-
return this;
|
|
688
|
-
}
|
|
689
|
-
tap(actions, callback) {
|
|
690
|
-
if (typeof actions === "function") {
|
|
691
|
-
this.routes.forEach((route) => {
|
|
692
|
-
if (!route.isDeleted()) {
|
|
693
|
-
actions(route);
|
|
694
|
-
}
|
|
695
|
-
});
|
|
696
|
-
return this;
|
|
697
|
-
}
|
|
698
|
-
this.#filter(actions, false).forEach((route) => {
|
|
699
|
-
if (!route.isDeleted()) {
|
|
700
|
-
callback(route);
|
|
701
|
-
}
|
|
702
|
-
});
|
|
703
|
-
return this;
|
|
704
|
-
}
|
|
705
|
-
/**
|
|
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
|
|
709
|
-
*/
|
|
710
|
-
params(resources) {
|
|
711
|
-
Object.keys(resources).forEach((resource) => {
|
|
712
|
-
const param = resources[resource];
|
|
713
|
-
const existingParam = this.#params[resource];
|
|
714
|
-
this.#params[resource] = `:${param}`;
|
|
715
|
-
this.routes.forEach((route) => {
|
|
716
|
-
route.setPattern(
|
|
717
|
-
route.getPattern().replace(`${resource}/${existingParam}`, `${resource}/:${param}`)
|
|
718
|
-
);
|
|
719
|
-
});
|
|
720
|
-
});
|
|
721
|
-
return this;
|
|
722
|
-
}
|
|
723
|
-
/**
|
|
724
|
-
* Define one or more middleware on the routes created by
|
|
725
|
-
* the resource.
|
|
726
|
-
*
|
|
727
|
-
* Calling this method multiple times will append middleware
|
|
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
|
|
732
|
-
*/
|
|
733
|
-
use(actions, middleware) {
|
|
734
|
-
if (actions === "*") {
|
|
735
|
-
this.tap((route) => route.use(middleware));
|
|
736
|
-
} else {
|
|
737
|
-
this.tap(actions, (route) => route.use(middleware));
|
|
738
|
-
}
|
|
739
|
-
return this;
|
|
740
|
-
}
|
|
741
|
-
/**
|
|
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
|
|
746
|
-
*/
|
|
747
|
-
middleware(actions, middleware) {
|
|
748
|
-
return this.use(actions, middleware);
|
|
749
|
-
}
|
|
750
|
-
/**
|
|
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
|
|
755
|
-
*/
|
|
756
|
-
as(name, normalizeName = true) {
|
|
757
|
-
name = normalizeName ? string.snakeCase(name) : name;
|
|
758
|
-
this.routes.forEach((route) => {
|
|
759
|
-
route.as(route.getName().replace(this.#routesBaseName, name), false);
|
|
760
|
-
});
|
|
761
|
-
this.#routesBaseName = name;
|
|
762
|
-
return this;
|
|
763
|
-
}
|
|
764
|
-
};
|
|
765
|
-
|
|
766
|
-
// src/router/group.ts
|
|
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
|
-
*/
|
|
772
|
-
constructor(routes) {
|
|
773
|
-
super();
|
|
774
|
-
this.routes = routes;
|
|
775
|
-
}
|
|
776
|
-
/**
|
|
777
|
-
* Array of middleware registered on the group.
|
|
778
|
-
*/
|
|
779
|
-
#middleware = [];
|
|
780
|
-
/**
|
|
781
|
-
* Shares midldeware stack with the routes. The method is invoked recursively
|
|
782
|
-
* to only register middleware with the route class and not with the
|
|
783
|
-
* resource or the child group
|
|
784
|
-
*/
|
|
785
|
-
#shareMiddlewareStackWithRoutes(route) {
|
|
786
|
-
if (route instanceof _RouteGroup) {
|
|
787
|
-
route.routes.forEach((child) => this.#shareMiddlewareStackWithRoutes(child));
|
|
788
|
-
return;
|
|
789
|
-
}
|
|
790
|
-
if (route instanceof RouteResource) {
|
|
791
|
-
route.routes.forEach((child) => child.getMiddleware().unshift(this.#middleware));
|
|
792
|
-
return;
|
|
793
|
-
}
|
|
794
|
-
if (route instanceof BriskRoute) {
|
|
795
|
-
route.route.getMiddleware().unshift(this.#middleware);
|
|
796
|
-
return;
|
|
797
|
-
}
|
|
798
|
-
route.getMiddleware().unshift(this.#middleware);
|
|
799
|
-
}
|
|
800
|
-
/**
|
|
801
|
-
* Updates the route name. The method is invoked recursively to only update
|
|
802
|
-
* the name with the route class and not with the resource or the child
|
|
803
|
-
* group.
|
|
804
|
-
*/
|
|
805
|
-
#updateRouteName(route, name) {
|
|
806
|
-
if (route instanceof _RouteGroup) {
|
|
807
|
-
route.routes.forEach((child) => this.#updateRouteName(child, name));
|
|
808
|
-
return;
|
|
809
|
-
}
|
|
810
|
-
if (route instanceof RouteResource) {
|
|
811
|
-
route.routes.forEach((child) => child.as(name, true));
|
|
812
|
-
return;
|
|
813
|
-
}
|
|
814
|
-
if (route instanceof BriskRoute) {
|
|
815
|
-
route.route.as(name, true);
|
|
816
|
-
return;
|
|
817
|
-
}
|
|
818
|
-
route.as(name, true);
|
|
819
|
-
}
|
|
820
|
-
/**
|
|
821
|
-
* Sets prefix on the route. The method is invoked recursively to only set
|
|
822
|
-
* the prefix with the route class and not with the resource or the
|
|
823
|
-
* child group.
|
|
824
|
-
*/
|
|
825
|
-
#setRoutePrefix(route, prefix) {
|
|
826
|
-
if (route instanceof _RouteGroup) {
|
|
827
|
-
route.routes.forEach((child) => this.#setRoutePrefix(child, prefix));
|
|
828
|
-
return;
|
|
829
|
-
}
|
|
830
|
-
if (route instanceof RouteResource) {
|
|
831
|
-
route.routes.forEach((child) => child.prefix(prefix));
|
|
832
|
-
return;
|
|
833
|
-
}
|
|
834
|
-
if (route instanceof BriskRoute) {
|
|
835
|
-
route.route.prefix(prefix);
|
|
836
|
-
return;
|
|
837
|
-
}
|
|
838
|
-
route.prefix(prefix);
|
|
839
|
-
}
|
|
840
|
-
/**
|
|
841
|
-
* Updates domain on the route. The method is invoked recursively to only update
|
|
842
|
-
* the domain with the route class and not with the resource or the child
|
|
843
|
-
* group.
|
|
844
|
-
*/
|
|
845
|
-
#updateRouteDomain(route, domain) {
|
|
846
|
-
if (route instanceof _RouteGroup) {
|
|
847
|
-
route.routes.forEach((child) => this.#updateRouteDomain(child, domain));
|
|
848
|
-
return;
|
|
849
|
-
}
|
|
850
|
-
if (route instanceof RouteResource) {
|
|
851
|
-
route.routes.forEach((child) => child.domain(domain));
|
|
852
|
-
return;
|
|
853
|
-
}
|
|
854
|
-
if (route instanceof BriskRoute) {
|
|
855
|
-
route.route.domain(domain, false);
|
|
856
|
-
return;
|
|
857
|
-
}
|
|
858
|
-
route.domain(domain, false);
|
|
859
|
-
}
|
|
860
|
-
/**
|
|
861
|
-
* Updates matchers on the route. The method is invoked recursively to only update
|
|
862
|
-
* the matchers with the route class and not with the resource or the child
|
|
863
|
-
* group.
|
|
864
|
-
*/
|
|
865
|
-
#updateRouteMatchers(route, param, matcher) {
|
|
866
|
-
if (route instanceof _RouteGroup) {
|
|
867
|
-
route.routes.forEach((child) => this.#updateRouteMatchers(child, param, matcher));
|
|
868
|
-
return;
|
|
869
|
-
}
|
|
870
|
-
if (route instanceof RouteResource) {
|
|
871
|
-
route.routes.forEach((child) => child.where(param, matcher));
|
|
872
|
-
return;
|
|
873
|
-
}
|
|
874
|
-
if (route instanceof BriskRoute) {
|
|
875
|
-
route.route.where(param, matcher);
|
|
876
|
-
return;
|
|
877
|
-
}
|
|
878
|
-
route.where(param, matcher);
|
|
879
|
-
}
|
|
880
|
-
/**
|
|
881
|
-
* Define route param matcher
|
|
882
|
-
*
|
|
883
|
-
* ```ts
|
|
884
|
-
* Route.group(() => {
|
|
885
|
-
* }).where('id', /^[0-9]+/)
|
|
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
|
|
890
|
-
*/
|
|
891
|
-
where(param, matcher) {
|
|
892
|
-
this.routes.forEach((route) => this.#updateRouteMatchers(route, param, matcher));
|
|
893
|
-
return this;
|
|
894
|
-
}
|
|
895
|
-
/**
|
|
896
|
-
* Define prefix all the routes in the group.
|
|
897
|
-
*
|
|
898
|
-
* ```ts
|
|
899
|
-
* Route.group(() => {
|
|
900
|
-
* }).prefix('v1')
|
|
901
|
-
* ```
|
|
902
|
-
* @param prefix - The prefix to add to all routes in the group
|
|
903
|
-
* @returns Current RouteGroup instance for method chaining
|
|
904
|
-
*/
|
|
905
|
-
prefix(prefix) {
|
|
906
|
-
this.routes.forEach((route) => this.#setRoutePrefix(route, prefix));
|
|
907
|
-
return this;
|
|
908
|
-
}
|
|
909
|
-
/**
|
|
910
|
-
* Define domain for all the routes.
|
|
911
|
-
*
|
|
912
|
-
* ```ts
|
|
913
|
-
* Route.group(() => {
|
|
914
|
-
* }).domain(':name.adonisjs.com')
|
|
915
|
-
* ```
|
|
916
|
-
* @param domain - The domain pattern for all routes in the group
|
|
917
|
-
* @returns Current RouteGroup instance for method chaining
|
|
918
|
-
*/
|
|
919
|
-
domain(domain) {
|
|
920
|
-
this.routes.forEach((route) => this.#updateRouteDomain(route, domain));
|
|
921
|
-
return this;
|
|
922
|
-
}
|
|
923
|
-
/**
|
|
924
|
-
* Prepend name to the routes name.
|
|
925
|
-
*
|
|
926
|
-
* ```ts
|
|
927
|
-
* Route.group(() => {
|
|
928
|
-
* }).as('version1')
|
|
929
|
-
* ```
|
|
930
|
-
* @param name - The name to prepend to all route names in the group
|
|
931
|
-
* @returns Current RouteGroup instance for method chaining
|
|
932
|
-
*/
|
|
933
|
-
as(name) {
|
|
934
|
-
this.routes.forEach((route) => this.#updateRouteName(route, name));
|
|
935
|
-
return this;
|
|
936
|
-
}
|
|
937
|
-
/**
|
|
938
|
-
* Prepend an array of middleware to all routes middleware.
|
|
939
|
-
*
|
|
940
|
-
* ```ts
|
|
941
|
-
* Route.group(() => {
|
|
942
|
-
* }).use(middleware.auth())
|
|
943
|
-
* ```
|
|
944
|
-
* @param middleware - Middleware function(s) to apply to all routes in the group
|
|
945
|
-
* @returns Current RouteGroup instance for method chaining
|
|
946
|
-
*/
|
|
947
|
-
use(middleware) {
|
|
948
|
-
if (!this.#middleware.length) {
|
|
949
|
-
this.routes.forEach((route) => this.#shareMiddlewareStackWithRoutes(route));
|
|
950
|
-
}
|
|
951
|
-
if (Array.isArray(middleware)) {
|
|
952
|
-
for (let one of middleware) {
|
|
953
|
-
this.#middleware.push(one);
|
|
954
|
-
}
|
|
955
|
-
} else {
|
|
956
|
-
this.#middleware.push(middleware);
|
|
957
|
-
}
|
|
958
|
-
return this;
|
|
959
|
-
}
|
|
960
|
-
/**
|
|
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
|
|
964
|
-
*/
|
|
965
|
-
middleware(middleware) {
|
|
966
|
-
return this.use(middleware);
|
|
967
|
-
}
|
|
968
|
-
};
|
|
969
|
-
|
|
970
|
-
// src/utils.ts
|
|
971
|
-
var proxyCache = new Cache({ max: 200 });
|
|
972
|
-
function dropSlash(input) {
|
|
973
|
-
if (input === "/") {
|
|
974
|
-
return "/";
|
|
975
|
-
}
|
|
976
|
-
return `/${input.replace(/^\//, "").replace(/\/$/, "")}`;
|
|
977
|
-
}
|
|
978
|
-
function toRoutesJSON(routes) {
|
|
979
|
-
return routes.reduce((list, route) => {
|
|
980
|
-
if (route instanceof RouteGroup) {
|
|
981
|
-
list = list.concat(toRoutesJSON(route.routes));
|
|
982
|
-
return list;
|
|
983
|
-
}
|
|
984
|
-
if (route instanceof RouteResource) {
|
|
985
|
-
list = list.concat(toRoutesJSON(route.routes));
|
|
986
|
-
return list;
|
|
987
|
-
}
|
|
988
|
-
if (route instanceof BriskRoute) {
|
|
989
|
-
if (route.route && !route.route.isDeleted()) {
|
|
990
|
-
list.push(route.route.toJSON());
|
|
991
|
-
}
|
|
992
|
-
return list;
|
|
993
|
-
}
|
|
994
|
-
if (!route.isDeleted()) {
|
|
995
|
-
list.push(route.toJSON());
|
|
996
|
-
}
|
|
997
|
-
return list;
|
|
998
|
-
}, []);
|
|
999
|
-
}
|
|
1000
|
-
function trustProxy(remoteAddress, proxyFn) {
|
|
1001
|
-
if (proxyCache.has(remoteAddress)) {
|
|
1002
|
-
return proxyCache.get(remoteAddress);
|
|
1003
|
-
}
|
|
1004
|
-
const result = proxyFn(remoteAddress, 0);
|
|
1005
|
-
proxyCache.set(remoteAddress, result);
|
|
1006
|
-
return result;
|
|
1007
|
-
}
|
|
1008
|
-
function parseRange(range, value) {
|
|
1009
|
-
const parts = range.split("..");
|
|
1010
|
-
const min = Number(parts[0]);
|
|
1011
|
-
const max = Number(parts[1]);
|
|
1012
|
-
if (parts.length === 1 && !Number.isNaN(min)) {
|
|
1013
|
-
return {
|
|
1014
|
-
[min]: value
|
|
1015
|
-
};
|
|
1016
|
-
}
|
|
1017
|
-
if (Number.isNaN(min) || Number.isNaN(max)) {
|
|
1018
|
-
return {};
|
|
1019
|
-
}
|
|
1020
|
-
if (min === max) {
|
|
1021
|
-
return {
|
|
1022
|
-
[min]: value
|
|
1023
|
-
};
|
|
1024
|
-
}
|
|
1025
|
-
if (max < min) {
|
|
1026
|
-
throw new InvalidArgumentsException(`Invalid range "${range}"`);
|
|
1027
|
-
}
|
|
1028
|
-
return [...Array(max - min + 1).keys()].reduce(
|
|
1029
|
-
(result, step) => {
|
|
1030
|
-
result[min + step] = value;
|
|
1031
|
-
return result;
|
|
1032
|
-
},
|
|
1033
|
-
{}
|
|
1034
|
-
);
|
|
1035
|
-
}
|
|
1036
|
-
function decodeComponentChar(highCharCode, lowCharCode) {
|
|
1037
|
-
if (highCharCode === 50) {
|
|
1038
|
-
if (lowCharCode === 53) return "%";
|
|
1039
|
-
if (lowCharCode === 51) return "#";
|
|
1040
|
-
if (lowCharCode === 52) return "$";
|
|
1041
|
-
if (lowCharCode === 54) return "&";
|
|
1042
|
-
if (lowCharCode === 66) return "+";
|
|
1043
|
-
if (lowCharCode === 98) return "+";
|
|
1044
|
-
if (lowCharCode === 67) return ",";
|
|
1045
|
-
if (lowCharCode === 99) return ",";
|
|
1046
|
-
if (lowCharCode === 70) return "/";
|
|
1047
|
-
if (lowCharCode === 102) return "/";
|
|
1048
|
-
return null;
|
|
1049
|
-
}
|
|
1050
|
-
if (highCharCode === 51) {
|
|
1051
|
-
if (lowCharCode === 65) return ":";
|
|
1052
|
-
if (lowCharCode === 97) return ":";
|
|
1053
|
-
if (lowCharCode === 66) return ";";
|
|
1054
|
-
if (lowCharCode === 98) return ";";
|
|
1055
|
-
if (lowCharCode === 68) return "=";
|
|
1056
|
-
if (lowCharCode === 100) return "=";
|
|
1057
|
-
if (lowCharCode === 70) return "?";
|
|
1058
|
-
if (lowCharCode === 102) return "?";
|
|
1059
|
-
return null;
|
|
1060
|
-
}
|
|
1061
|
-
if (highCharCode === 52 && lowCharCode === 48) {
|
|
1062
|
-
return "@";
|
|
1063
|
-
}
|
|
1064
|
-
return null;
|
|
1065
|
-
}
|
|
1066
|
-
function safeDecodeURI(path, useSemicolonDelimiter) {
|
|
1067
|
-
let shouldDecode = false;
|
|
1068
|
-
let shouldDecodeParam = false;
|
|
1069
|
-
let querystring = "";
|
|
1070
|
-
for (let i = 1; i < path.length; i++) {
|
|
1071
|
-
const charCode = path.charCodeAt(i);
|
|
1072
|
-
if (charCode === 37) {
|
|
1073
|
-
const highCharCode = path.charCodeAt(i + 1);
|
|
1074
|
-
const lowCharCode = path.charCodeAt(i + 2);
|
|
1075
|
-
if (decodeComponentChar(highCharCode, lowCharCode) === null) {
|
|
1076
|
-
shouldDecode = true;
|
|
1077
|
-
} else {
|
|
1078
|
-
shouldDecodeParam = true;
|
|
1079
|
-
if (highCharCode === 50 && lowCharCode === 53) {
|
|
1080
|
-
shouldDecode = true;
|
|
1081
|
-
path = path.slice(0, i + 1) + "25" + path.slice(i + 1);
|
|
1082
|
-
i += 2;
|
|
1083
|
-
}
|
|
1084
|
-
i += 2;
|
|
1085
|
-
}
|
|
1086
|
-
} else if (charCode === 63 || charCode === 35 || charCode === 59 && useSemicolonDelimiter) {
|
|
1087
|
-
querystring = path.slice(i + 1);
|
|
1088
|
-
path = path.slice(0, i);
|
|
1089
|
-
break;
|
|
1090
|
-
}
|
|
1091
|
-
}
|
|
1092
|
-
const decodedPath = shouldDecode ? decodeURI(path) : path;
|
|
1093
|
-
return { pathname: decodedPath, query: querystring, shouldDecodeParam };
|
|
1094
|
-
}
|
|
1095
|
-
|
|
1096
|
-
// src/router/route.ts
|
|
1097
|
-
import StringBuilder from "@poppinss/utils/string_builder";
|
|
1098
|
-
import string2 from "@poppinss/utils/string";
|
|
1099
|
-
var Route = class extends Macroable4 {
|
|
1100
|
-
/**
|
|
1101
|
-
* Route pattern
|
|
1102
|
-
*/
|
|
1103
|
-
#pattern;
|
|
1104
|
-
/**
|
|
1105
|
-
* HTTP Methods for the route
|
|
1106
|
-
*/
|
|
1107
|
-
#methods;
|
|
1108
|
-
/**
|
|
1109
|
-
* A unique name for the route
|
|
1110
|
-
*/
|
|
1111
|
-
#name;
|
|
1112
|
-
/**
|
|
1113
|
-
* A boolean to prevent route from getting registered within
|
|
1114
|
-
* the store.
|
|
1115
|
-
*
|
|
1116
|
-
* This flag must be set before "Router.commit" method
|
|
1117
|
-
*/
|
|
1118
|
-
#isDeleted = false;
|
|
1119
|
-
/**
|
|
1120
|
-
* Route handler
|
|
1121
|
-
*/
|
|
1122
|
-
#handler;
|
|
1123
|
-
/**
|
|
1124
|
-
* Matchers inherited from the router
|
|
1125
|
-
*/
|
|
1126
|
-
#globalMatchers;
|
|
1127
|
-
/**
|
|
1128
|
-
* Reference to the AdonisJS application
|
|
1129
|
-
*/
|
|
1130
|
-
#app;
|
|
1131
|
-
/**
|
|
1132
|
-
* Middleware registered on the router
|
|
1133
|
-
*/
|
|
1134
|
-
#routerMiddleware;
|
|
1135
|
-
/**
|
|
1136
|
-
* By default the route is part of the `root` domain. Root domain is used
|
|
1137
|
-
* when no domain is defined
|
|
1138
|
-
*/
|
|
1139
|
-
#routeDomain = "root";
|
|
1140
|
-
/**
|
|
1141
|
-
* An object of matchers to be forwarded to the store. The matchers
|
|
1142
|
-
* list is populated by calling `where` method
|
|
1143
|
-
*/
|
|
1144
|
-
#matchers = {};
|
|
1145
|
-
/**
|
|
1146
|
-
* Custom prefixes defined on the route or the route parent
|
|
1147
|
-
* groups
|
|
1148
|
-
*/
|
|
1149
|
-
#prefixes = [];
|
|
1150
|
-
/**
|
|
1151
|
-
* Middleware defined directly on the route or the route parent
|
|
1152
|
-
* routes. We mantain an array for each layer of the stack
|
|
1153
|
-
*/
|
|
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
|
-
*/
|
|
1161
|
-
constructor(app, routerMiddleware, options) {
|
|
1162
|
-
super();
|
|
1163
|
-
this.#app = app;
|
|
1164
|
-
this.#routerMiddleware = routerMiddleware;
|
|
1165
|
-
this.#pattern = options.pattern;
|
|
1166
|
-
this.#methods = options.methods;
|
|
1167
|
-
this.#globalMatchers = options.globalMatchers;
|
|
1168
|
-
const { handler, routeName } = this.#resolveRouteHandle(options.handler);
|
|
1169
|
-
this.#handler = handler;
|
|
1170
|
-
this.#name = routeName;
|
|
1171
|
-
}
|
|
1172
|
-
/**
|
|
1173
|
-
* Resolves the route handler string expression to a
|
|
1174
|
-
* handler method object
|
|
1175
|
-
*/
|
|
1176
|
-
#resolveRouteHandle(handler) {
|
|
1177
|
-
if (typeof handler === "string") {
|
|
1178
|
-
const parts = handler.split(".");
|
|
1179
|
-
const method = parts.length === 1 ? "handle" : parts.pop();
|
|
1180
|
-
const moduleRefId = parts.join(".");
|
|
1181
|
-
return {
|
|
1182
|
-
handler: {
|
|
1183
|
-
method,
|
|
1184
|
-
reference: handler,
|
|
1185
|
-
importExpression: moduleRefId,
|
|
1186
|
-
...moduleImporter(() => this.#app.import(moduleRefId), method).toHandleMethod(),
|
|
1187
|
-
name: handler
|
|
1188
|
-
},
|
|
1189
|
-
routeName: `${new StringBuilder(moduleRefId.split("/").pop()).removeSuffix("controller").snakeCase()}.${string2.snakeCase(method)}`
|
|
1190
|
-
};
|
|
1191
|
-
}
|
|
1192
|
-
if (Array.isArray(handler)) {
|
|
1193
|
-
const controller = handler[0];
|
|
1194
|
-
const method = handler[1] ?? "handle";
|
|
1195
|
-
if (is.class(controller)) {
|
|
1196
|
-
return {
|
|
1197
|
-
handler: {
|
|
1198
|
-
method,
|
|
1199
|
-
reference: handler,
|
|
1200
|
-
importExpression: null,
|
|
1201
|
-
...moduleCaller(controller, method).toHandleMethod()
|
|
1202
|
-
},
|
|
1203
|
-
routeName: `${new StringBuilder(controller.name).removeSuffix("controller").snakeCase()}.${string2.snakeCase(method)}`
|
|
1204
|
-
};
|
|
1205
|
-
}
|
|
1206
|
-
return {
|
|
1207
|
-
handler: {
|
|
1208
|
-
method,
|
|
1209
|
-
reference: handler,
|
|
1210
|
-
importExpression: String(controller),
|
|
1211
|
-
...moduleImporter(controller, method).toHandleMethod()
|
|
1212
|
-
},
|
|
1213
|
-
routeName: controller.name ? `${new StringBuilder(controller.name).removeSuffix("controller").snakeCase()}.${string2.snakeCase(method)}` : void 0
|
|
1214
|
-
};
|
|
1215
|
-
}
|
|
1216
|
-
return { handler };
|
|
1217
|
-
}
|
|
159
|
+
#encryption;
|
|
1218
160
|
/**
|
|
1219
|
-
*
|
|
1220
|
-
*
|
|
1221
|
-
*
|
|
161
|
+
* Create a new instance of CookieClient
|
|
162
|
+
*
|
|
163
|
+
* @param encryption - The encryption instance for cookie operations
|
|
1222
164
|
*/
|
|
1223
|
-
|
|
1224
|
-
|
|
165
|
+
constructor(encryption) {
|
|
166
|
+
this.#encryption = encryption;
|
|
1225
167
|
}
|
|
1226
168
|
/**
|
|
1227
|
-
*
|
|
169
|
+
* Encrypt a key value pair to be sent in the cookie header
|
|
170
|
+
*
|
|
171
|
+
* @param key - The cookie key
|
|
172
|
+
* @param value - The value to encrypt
|
|
173
|
+
* @returns The encrypted cookie string or null if encryption fails
|
|
1228
174
|
*/
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
const prefix = this.#prefixes.slice().reverse().map((one) => dropSlash(one)).join("");
|
|
1232
|
-
return prefix ? `${prefix}${pattern === "/" ? "" : pattern}` : pattern;
|
|
175
|
+
encrypt(key, value) {
|
|
176
|
+
return pack3(key, value, this.#encryption);
|
|
1233
177
|
}
|
|
1234
178
|
/**
|
|
1235
|
-
*
|
|
1236
|
-
* override that, since the routes inside a group will set matchers
|
|
1237
|
-
* before the group, so they should have priority over the group
|
|
1238
|
-
* matchers.
|
|
1239
|
-
*
|
|
1240
|
-
* ```ts
|
|
1241
|
-
* Route.group(() => {
|
|
1242
|
-
* Route.get('/:id', 'handler').where('id', /^[0-9]$/)
|
|
1243
|
-
* }).where('id', /[^a-z$]/)
|
|
1244
|
-
* ```
|
|
179
|
+
* Sign a key value pair to be sent in the cookie header
|
|
1245
180
|
*
|
|
1246
|
-
*
|
|
181
|
+
* @param key - The cookie key
|
|
182
|
+
* @param value - The value to sign
|
|
183
|
+
* @returns The signed cookie string or null if signing fails
|
|
1247
184
|
*/
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
return this;
|
|
1251
|
-
}
|
|
1252
|
-
if (typeof matcher === "string") {
|
|
1253
|
-
this.#matchers[param] = { match: new RegExp(matcher) };
|
|
1254
|
-
} else if (is.regExp(matcher)) {
|
|
1255
|
-
this.#matchers[param] = { match: matcher };
|
|
1256
|
-
} else {
|
|
1257
|
-
this.#matchers[param] = matcher;
|
|
1258
|
-
}
|
|
1259
|
-
return this;
|
|
185
|
+
sign(key, value) {
|
|
186
|
+
return pack2(key, value, this.#encryption);
|
|
1260
187
|
}
|
|
1261
188
|
/**
|
|
1262
|
-
*
|
|
1263
|
-
*
|
|
1264
|
-
* @param
|
|
1265
|
-
* @
|
|
189
|
+
* Encode a key value pair to be sent in the cookie header
|
|
190
|
+
*
|
|
191
|
+
* @param _ - Unused key parameter
|
|
192
|
+
* @param value - The value to encode
|
|
193
|
+
* @param stringify - Whether to stringify the value before encoding
|
|
194
|
+
* @returns The encoded cookie string or null if encoding fails
|
|
1266
195
|
*/
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
return this;
|
|
196
|
+
encode(_, value, stringify2 = true) {
|
|
197
|
+
return stringify2 ? pack(value) : value;
|
|
1270
198
|
}
|
|
1271
199
|
/**
|
|
1272
|
-
*
|
|
1273
|
-
*
|
|
200
|
+
* Unsign a signed cookie value
|
|
201
|
+
*
|
|
202
|
+
* @param key - The cookie key
|
|
203
|
+
* @param value - The signed cookie value to unsign
|
|
204
|
+
* @returns The original value if valid signature, null otherwise
|
|
1274
205
|
*/
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
this.#routeDomain = domain;
|
|
1278
|
-
}
|
|
1279
|
-
return this;
|
|
206
|
+
unsign(key, value) {
|
|
207
|
+
return canUnpack2(value) ? unpack2(key, value, this.#encryption) : null;
|
|
1280
208
|
}
|
|
1281
209
|
/**
|
|
1282
|
-
*
|
|
1283
|
-
* handler.
|
|
210
|
+
* Decrypt an encrypted cookie value
|
|
1284
211
|
*
|
|
1285
|
-
*
|
|
1286
|
-
*
|
|
212
|
+
* @param key - The cookie key
|
|
213
|
+
* @param value - The encrypted cookie value to decrypt
|
|
214
|
+
* @returns The decrypted value or null if decryption fails
|
|
1287
215
|
*/
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
return this;
|
|
216
|
+
decrypt(key, value) {
|
|
217
|
+
return canUnpack3(value) ? unpack3(key, value, this.#encryption) : null;
|
|
1291
218
|
}
|
|
1292
219
|
/**
|
|
1293
|
-
*
|
|
220
|
+
* Decode an encoded cookie value
|
|
221
|
+
*
|
|
222
|
+
* @param _ - Unused key parameter
|
|
223
|
+
* @param value - The encoded cookie value to decode
|
|
224
|
+
* @param stringified - Whether the value was stringified during encoding
|
|
225
|
+
* @returns The decoded value or null if decoding fails
|
|
1294
226
|
*/
|
|
1295
|
-
|
|
1296
|
-
|
|
227
|
+
decode(_, value, stringified = true) {
|
|
228
|
+
if (!stringified) {
|
|
229
|
+
return value;
|
|
230
|
+
}
|
|
231
|
+
return canUnpack(value) ? unpack(value) : null;
|
|
1297
232
|
}
|
|
1298
233
|
/**
|
|
1299
|
-
*
|
|
1300
|
-
* existing name of the route.
|
|
234
|
+
* Parse response cookie
|
|
1301
235
|
*
|
|
1302
|
-
*
|
|
236
|
+
* @param key - The cookie key
|
|
237
|
+
* @param value - The cookie value to parse
|
|
238
|
+
* @returns The parsed value or undefined if parsing fails
|
|
1303
239
|
*/
|
|
1304
|
-
|
|
1305
|
-
if (
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
return
|
|
240
|
+
parse(key, value) {
|
|
241
|
+
if (canUnpack2(value)) {
|
|
242
|
+
return unpack2(key, value, this.#encryption);
|
|
243
|
+
}
|
|
244
|
+
if (canUnpack3(value)) {
|
|
245
|
+
return unpack3(key, value, this.#encryption);
|
|
246
|
+
}
|
|
247
|
+
if (canUnpack(value)) {
|
|
248
|
+
return unpack(value);
|
|
1313
249
|
}
|
|
1314
|
-
this.#name = name;
|
|
1315
|
-
return this;
|
|
1316
250
|
}
|
|
251
|
+
};
|
|
252
|
+
|
|
253
|
+
// src/cookies/parser.ts
|
|
254
|
+
import cookie from "cookie";
|
|
255
|
+
var CookieParser = class {
|
|
1317
256
|
/**
|
|
1318
|
-
*
|
|
257
|
+
* Cookie client instance for handling cookie operations
|
|
1319
258
|
*/
|
|
1320
|
-
|
|
1321
|
-
return this.#isDeleted;
|
|
1322
|
-
}
|
|
259
|
+
#client;
|
|
1323
260
|
/**
|
|
1324
|
-
*
|
|
1325
|
-
*
|
|
261
|
+
* A copy of cached cookies, they are cached during a request after
|
|
262
|
+
* initial decoding, unsigning or decrypting.
|
|
1326
263
|
*/
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
264
|
+
#cachedCookies = {
|
|
265
|
+
signedCookies: {},
|
|
266
|
+
plainCookies: {},
|
|
267
|
+
encryptedCookies: {}
|
|
268
|
+
};
|
|
269
|
+
/**
|
|
270
|
+
* An object of key-value pair collected by parsing
|
|
271
|
+
* the request cookie header.
|
|
272
|
+
*/
|
|
273
|
+
#cookies;
|
|
1330
274
|
/**
|
|
1331
|
-
*
|
|
275
|
+
* Create a new instance of CookieParser
|
|
276
|
+
*
|
|
277
|
+
* @param cookieHeader - The raw cookie header string from the request
|
|
278
|
+
* @param encryption - The encryption instance for cookie operations
|
|
1332
279
|
*/
|
|
1333
|
-
|
|
1334
|
-
|
|
280
|
+
constructor(cookieHeader, encryption) {
|
|
281
|
+
this.#client = new CookieClient(encryption);
|
|
282
|
+
this.#cookies = this.#parse(cookieHeader);
|
|
1335
283
|
}
|
|
1336
284
|
/**
|
|
1337
|
-
*
|
|
285
|
+
* Parses the request `cookie` header
|
|
286
|
+
*
|
|
287
|
+
* @param cookieHeader - The cookie header string to parse
|
|
288
|
+
* @returns Parsed cookies as key-value pairs
|
|
1338
289
|
*/
|
|
1339
|
-
|
|
1340
|
-
|
|
290
|
+
#parse(cookieHeader) {
|
|
291
|
+
if (!cookieHeader) {
|
|
292
|
+
return {};
|
|
293
|
+
}
|
|
294
|
+
return cookie.parse(cookieHeader);
|
|
1341
295
|
}
|
|
1342
296
|
/**
|
|
1343
|
-
*
|
|
297
|
+
* Attempts to decode a cookie by the name. When calling this method,
|
|
298
|
+
* you are assuming that the cookie was just stringified in the first
|
|
299
|
+
* place and not signed or encrypted.
|
|
300
|
+
*
|
|
301
|
+
* @param key - The cookie key to decode
|
|
302
|
+
* @param stringified - Whether the cookie value was stringified
|
|
303
|
+
* @returns The decoded cookie value or null if decoding fails
|
|
1344
304
|
*/
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
305
|
+
decode(key, stringified = true) {
|
|
306
|
+
const value = this.#cookies[key];
|
|
307
|
+
if (value === null || value === void 0) {
|
|
308
|
+
return null;
|
|
309
|
+
}
|
|
310
|
+
const cache = this.#cachedCookies.plainCookies;
|
|
311
|
+
if (cache[key] !== void 0) {
|
|
312
|
+
return cache[key];
|
|
313
|
+
}
|
|
314
|
+
const parsed = this.#client.decode(key, value, stringified);
|
|
315
|
+
if (parsed !== null) {
|
|
316
|
+
cache[key] = parsed;
|
|
317
|
+
}
|
|
318
|
+
return parsed;
|
|
1348
319
|
}
|
|
1349
320
|
/**
|
|
1350
|
-
*
|
|
1351
|
-
*
|
|
321
|
+
* Attempts to unsign a cookie by the name. When calling this method,
|
|
322
|
+
* you are assuming that the cookie was signed in the first place.
|
|
323
|
+
*
|
|
324
|
+
* @param key - The cookie key to unsign
|
|
325
|
+
* @returns The original cookie value or null if unsigning fails
|
|
1352
326
|
*/
|
|
1353
|
-
|
|
1354
|
-
|
|
327
|
+
unsign(key) {
|
|
328
|
+
const value = this.#cookies[key];
|
|
329
|
+
if (value === null || value === void 0) {
|
|
330
|
+
return null;
|
|
331
|
+
}
|
|
332
|
+
const cache = this.#cachedCookies.signedCookies;
|
|
333
|
+
if (cache[key] !== void 0) {
|
|
334
|
+
return cache[key];
|
|
335
|
+
}
|
|
336
|
+
const parsed = this.#client.unsign(key, value);
|
|
337
|
+
if (parsed !== null) {
|
|
338
|
+
cache[key] = parsed;
|
|
339
|
+
}
|
|
340
|
+
return parsed;
|
|
1355
341
|
}
|
|
1356
342
|
/**
|
|
1357
|
-
*
|
|
1358
|
-
*
|
|
343
|
+
* Attempts to decrypt a cookie by the name. When calling this method,
|
|
344
|
+
* you are assuming that the cookie was encrypted in the first place.
|
|
345
|
+
*
|
|
346
|
+
* @param key - The cookie key to decrypt
|
|
347
|
+
* @returns The decrypted cookie value or null if decryption fails
|
|
1359
348
|
*/
|
|
1360
|
-
|
|
1361
|
-
const
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
349
|
+
decrypt(key) {
|
|
350
|
+
const value = this.#cookies[key];
|
|
351
|
+
if (value === null || value === void 0) {
|
|
352
|
+
return null;
|
|
353
|
+
}
|
|
354
|
+
const cache = this.#cachedCookies.encryptedCookies;
|
|
355
|
+
if (cache[key] !== void 0) {
|
|
356
|
+
return cache[key];
|
|
357
|
+
}
|
|
358
|
+
const parsed = this.#client.decrypt(key, value);
|
|
359
|
+
if (parsed !== null) {
|
|
360
|
+
cache[key] = parsed;
|
|
361
|
+
}
|
|
362
|
+
return parsed;
|
|
1372
363
|
}
|
|
1373
364
|
/**
|
|
1374
|
-
* Returns
|
|
365
|
+
* Returns an object of cookies key-value pair. Do note, the
|
|
366
|
+
* cookies are not decoded, unsigned or decrypted inside this
|
|
367
|
+
* list.
|
|
368
|
+
*
|
|
369
|
+
* @returns Raw cookies as key-value pairs
|
|
1375
370
|
*/
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
const matchers = this.#getMatchers();
|
|
1379
|
-
return {
|
|
1380
|
-
domain: this.#routeDomain,
|
|
1381
|
-
pattern,
|
|
1382
|
-
matchers,
|
|
1383
|
-
tokens: parseRoute(pattern, matchers),
|
|
1384
|
-
meta: {},
|
|
1385
|
-
name: this.#name,
|
|
1386
|
-
handler: this.#handler,
|
|
1387
|
-
methods: this.#methods,
|
|
1388
|
-
middleware: this.#getMiddlewareForStore(),
|
|
1389
|
-
execute
|
|
1390
|
-
};
|
|
371
|
+
list() {
|
|
372
|
+
return this.#cookies;
|
|
1391
373
|
}
|
|
1392
374
|
};
|
|
1393
375
|
|
|
@@ -1396,12 +378,12 @@ import fresh from "fresh";
|
|
|
1396
378
|
import typeIs from "type-is";
|
|
1397
379
|
import accepts from "accepts";
|
|
1398
380
|
import { isIP } from "net";
|
|
1399
|
-
import
|
|
381
|
+
import is from "@sindresorhus/is";
|
|
1400
382
|
import proxyaddr from "proxy-addr";
|
|
1401
383
|
import { safeEqual } from "@poppinss/utils";
|
|
1402
|
-
import
|
|
384
|
+
import Macroable from "@poppinss/macroable";
|
|
1403
385
|
import lodash from "@poppinss/utils/lodash";
|
|
1404
|
-
var Request = class extends
|
|
386
|
+
var Request = class extends Macroable {
|
|
1405
387
|
/**
|
|
1406
388
|
* Creates a new Request instance wrapping the native Node.js HTTP request
|
|
1407
389
|
* @param request - Native Node.js incoming message instance
|
|
@@ -2231,7 +1213,7 @@ var Request = class extends Macroable5 {
|
|
|
2231
1213
|
}
|
|
2232
1214
|
plainCookie(key, defaultValueOrOptions, encoded) {
|
|
2233
1215
|
this.#initiateCookieParser();
|
|
2234
|
-
if (
|
|
1216
|
+
if (is.object(defaultValueOrOptions)) {
|
|
2235
1217
|
return this.#cookieParser.decode(key, defaultValueOrOptions?.encoded) || defaultValueOrOptions.defaultValue;
|
|
2236
1218
|
}
|
|
2237
1219
|
return this.#cookieParser.decode(key, encoded) || defaultValueOrOptions;
|
|
@@ -2284,7 +1266,6 @@ var Request = class extends Macroable5 {
|
|
|
2284
1266
|
};
|
|
2285
1267
|
|
|
2286
1268
|
// src/redirect.ts
|
|
2287
|
-
import { parse } from "url";
|
|
2288
1269
|
var Redirect = class {
|
|
2289
1270
|
/**
|
|
2290
1271
|
* Flag indicating whether to forward the existing query string from the current request
|
|
@@ -2386,7 +1367,7 @@ var Redirect = class {
|
|
|
2386
1367
|
back() {
|
|
2387
1368
|
let query = {};
|
|
2388
1369
|
const referrerUrl = this.#getReferrerUrl();
|
|
2389
|
-
const url =
|
|
1370
|
+
const url = safeDecodeURI(referrerUrl, false);
|
|
2390
1371
|
debug_default('referrer url "%s"', referrerUrl);
|
|
2391
1372
|
debug_default('referrer base url "%s"', url.pathname);
|
|
2392
1373
|
if (this.#forwardQueryString) {
|
|
@@ -2415,7 +1396,7 @@ var Redirect = class {
|
|
|
2415
1396
|
toPath(url) {
|
|
2416
1397
|
let query = {};
|
|
2417
1398
|
if (this.#forwardQueryString) {
|
|
2418
|
-
query = this.#qs.parse(
|
|
1399
|
+
query = this.#qs.parse(safeDecodeURI(this.#request.url, false).query || "");
|
|
2419
1400
|
}
|
|
2420
1401
|
Object.assign(query, this.#queryString);
|
|
2421
1402
|
this.#sendResponse(url, query);
|
|
@@ -2568,13 +1549,13 @@ import { extname } from "path";
|
|
|
2568
1549
|
import { Buffer } from "buffer";
|
|
2569
1550
|
import onFinished from "on-finished";
|
|
2570
1551
|
import { stat } from "fs/promises";
|
|
2571
|
-
import
|
|
1552
|
+
import Macroable2 from "@poppinss/macroable";
|
|
2572
1553
|
import { createReadStream } from "fs";
|
|
2573
1554
|
import contentDisposition from "content-disposition";
|
|
2574
1555
|
import { safeStringify } from "@poppinss/utils/json";
|
|
2575
|
-
import { RuntimeException
|
|
1556
|
+
import { RuntimeException } from "@poppinss/utils/exception";
|
|
2576
1557
|
var CACHEABLE_HTTP_METHODS = ["GET", "HEAD"];
|
|
2577
|
-
var Response = class extends
|
|
1558
|
+
var Response = class extends Macroable2 {
|
|
2578
1559
|
/**
|
|
2579
1560
|
* Creates a new Response instance
|
|
2580
1561
|
*
|
|
@@ -2741,7 +1722,7 @@ var Response = class extends Macroable6 {
|
|
|
2741
1722
|
}
|
|
2742
1723
|
return "object";
|
|
2743
1724
|
}
|
|
2744
|
-
throw new
|
|
1725
|
+
throw new RuntimeException(`Cannot serialize "${dataType}" to HTTP response`);
|
|
2745
1726
|
}
|
|
2746
1727
|
/**
|
|
2747
1728
|
* Writes the response body with appropriate headers and content type detection
|
|
@@ -3832,13 +2813,13 @@ var Response = class extends Macroable6 {
|
|
|
3832
2813
|
};
|
|
3833
2814
|
|
|
3834
2815
|
// src/router/main.ts
|
|
3835
|
-
import
|
|
3836
|
-
import { moduleImporter as
|
|
3837
|
-
import { RuntimeException as
|
|
2816
|
+
import is2 from "@sindresorhus/is";
|
|
2817
|
+
import { moduleImporter as moduleImporter2 } from "@adonisjs/fold";
|
|
2818
|
+
import { RuntimeException as RuntimeException3 } from "@poppinss/utils/exception";
|
|
3838
2819
|
|
|
3839
2820
|
// src/router/store.ts
|
|
3840
2821
|
import matchit from "@poppinss/matchit";
|
|
3841
|
-
import { RuntimeException as
|
|
2822
|
+
import { RuntimeException as RuntimeException2 } from "@poppinss/utils/exception";
|
|
3842
2823
|
var RoutesStore = class {
|
|
3843
2824
|
/**
|
|
3844
2825
|
* A flag to know if routes for explicit domains
|
|
@@ -3877,7 +2858,7 @@ var RoutesStore = class {
|
|
|
3877
2858
|
for (let token of tokens) {
|
|
3878
2859
|
if ([1, 3].includes(token.type)) {
|
|
3879
2860
|
if (collectedParams.has(token.val)) {
|
|
3880
|
-
throw new
|
|
2861
|
+
throw new RuntimeException2(`Duplicate param "${token.val}" found in "${route.pattern}"`);
|
|
3881
2862
|
} else {
|
|
3882
2863
|
collectedParams.add(token.val);
|
|
3883
2864
|
}
|
|
@@ -3893,7 +2874,7 @@ var RoutesStore = class {
|
|
|
3893
2874
|
#registerRoute(domain, method, tokens, route) {
|
|
3894
2875
|
const methodRoutes = this.#getMethodNode(domain, method);
|
|
3895
2876
|
if (methodRoutes.routes[route.pattern]) {
|
|
3896
|
-
throw new
|
|
2877
|
+
throw new RuntimeException2(
|
|
3897
2878
|
`Duplicate route found. "${method}: ${route.pattern}" route already exists`
|
|
3898
2879
|
);
|
|
3899
2880
|
}
|
|
@@ -4171,8 +3152,8 @@ var UrlBuilder = class {
|
|
|
4171
3152
|
};
|
|
4172
3153
|
|
|
4173
3154
|
// src/router/matchers.ts
|
|
4174
|
-
import
|
|
4175
|
-
var RouteMatchers = class extends
|
|
3155
|
+
import Macroable3 from "@poppinss/macroable";
|
|
3156
|
+
var RouteMatchers = class extends Macroable3 {
|
|
4176
3157
|
/**
|
|
4177
3158
|
* Enforce value to be a number and also casts it to number data
|
|
4178
3159
|
* type
|
|
@@ -4201,9 +3182,9 @@ var RouteMatchers = class extends Macroable7 {
|
|
|
4201
3182
|
};
|
|
4202
3183
|
|
|
4203
3184
|
// src/define_middleware.ts
|
|
4204
|
-
import { moduleImporter
|
|
3185
|
+
import { moduleImporter } from "@adonisjs/fold";
|
|
4205
3186
|
function middlewareReferenceBuilder(name, middleware) {
|
|
4206
|
-
const handler =
|
|
3187
|
+
const handler = moduleImporter(middleware, "handle").toHandleMethod();
|
|
4207
3188
|
return function(...args) {
|
|
4208
3189
|
return {
|
|
4209
3190
|
...handler,
|
|
@@ -4424,7 +3405,7 @@ var Router = class {
|
|
|
4424
3405
|
middleware.forEach(
|
|
4425
3406
|
(one) => this.#middleware.push({
|
|
4426
3407
|
reference: one,
|
|
4427
|
-
...
|
|
3408
|
+
...moduleImporter2(one, "handle").toHandleMethod()
|
|
4428
3409
|
})
|
|
4429
3410
|
);
|
|
4430
3411
|
return this;
|
|
@@ -4583,7 +3564,7 @@ var Router = class {
|
|
|
4583
3564
|
where(param, matcher) {
|
|
4584
3565
|
if (typeof matcher === "string") {
|
|
4585
3566
|
this.#globalMatchers[param] = { match: new RegExp(matcher) };
|
|
4586
|
-
} else if (
|
|
3567
|
+
} else if (is2.regExp(matcher)) {
|
|
4587
3568
|
this.#globalMatchers[param] = { match: matcher };
|
|
4588
3569
|
} else {
|
|
4589
3570
|
this.#globalMatchers[param] = matcher;
|
|
@@ -4606,7 +3587,7 @@ var Router = class {
|
|
|
4606
3587
|
}
|
|
4607
3588
|
const routeNames = routeNamesByDomain.get(route.domain);
|
|
4608
3589
|
if (route.name && routeNames.has(route.name)) {
|
|
4609
|
-
throw new
|
|
3590
|
+
throw new RuntimeException3(
|
|
4610
3591
|
`A route with name "${route.name}" already exists. It may happen when two routes use the same controller, so make sure to give explicit names to these routes`
|
|
4611
3592
|
);
|
|
4612
3593
|
}
|
|
@@ -4772,7 +3753,7 @@ var Router = class {
|
|
|
4772
3753
|
(domain) => this.routes[domain].forEach((route) => trackRoute.bind(this)(route, domain))
|
|
4773
3754
|
);
|
|
4774
3755
|
return Object.keys(routesList).reduce((result, method) => {
|
|
4775
|
-
result.push(`${" ".repeat(indentation)}
|
|
3756
|
+
result.push(`${" ".repeat(indentation)}${method}: {`);
|
|
4776
3757
|
Object.keys(routesList[method]).forEach((identifier) => {
|
|
4777
3758
|
const key = `'${identifier}'`;
|
|
4778
3759
|
const { paramsTuple, hasRequiredParams, params } = routesList[method][identifier];
|
|
@@ -4780,10 +3761,10 @@ var Router = class {
|
|
|
4780
3761
|
const tupleName = hasRequiredParams ? "paramsTuple" : "paramsTuple?";
|
|
4781
3762
|
const dictValue = `{${params.join(",")}}`;
|
|
4782
3763
|
const tupleValue = `[${paramsTuple?.join(",")}]`;
|
|
4783
|
-
const value = `{ ${tupleName}: ${tupleValue}
|
|
4784
|
-
result.push(`${" ".repeat(indentation + 2)}${key}: ${value}
|
|
3764
|
+
const value = `{ ${tupleName}: ${tupleValue}; ${dictName}: ${dictValue} }`;
|
|
3765
|
+
result.push(`${" ".repeat(indentation + 2)}${key}: ${value}`);
|
|
4785
3766
|
});
|
|
4786
|
-
result.push(`${" ".repeat(indentation)}}
|
|
3767
|
+
result.push(`${" ".repeat(indentation)}}`);
|
|
4787
3768
|
return result;
|
|
4788
3769
|
}, []).join("\n");
|
|
4789
3770
|
}
|
|
@@ -4870,8 +3851,8 @@ var Router = class {
|
|
|
4870
3851
|
|
|
4871
3852
|
// src/http_context/main.ts
|
|
4872
3853
|
import { inspect } from "util";
|
|
4873
|
-
import
|
|
4874
|
-
import { RuntimeException as
|
|
3854
|
+
import Macroable4 from "@poppinss/macroable";
|
|
3855
|
+
import { RuntimeException as RuntimeException4 } from "@poppinss/utils/exception";
|
|
4875
3856
|
|
|
4876
3857
|
// src/http_context/local_storage.ts
|
|
4877
3858
|
import { AsyncLocalStorage } from "async_hooks";
|
|
@@ -4890,7 +3871,7 @@ var asyncLocalStorage = {
|
|
|
4890
3871
|
};
|
|
4891
3872
|
|
|
4892
3873
|
// src/http_context/main.ts
|
|
4893
|
-
var HttpContext = class extends
|
|
3874
|
+
var HttpContext = class extends Macroable4 {
|
|
4894
3875
|
/**
|
|
4895
3876
|
* Creates a new HttpContext instance
|
|
4896
3877
|
*
|
|
@@ -4931,13 +3912,13 @@ var HttpContext = class extends Macroable8 {
|
|
|
4931
3912
|
*/
|
|
4932
3913
|
static getOrFail() {
|
|
4933
3914
|
if (!this.usingAsyncLocalStorage || !asyncLocalStorage.storage) {
|
|
4934
|
-
throw new
|
|
3915
|
+
throw new RuntimeException4(
|
|
4935
3916
|
'HTTP context is not available. Enable "useAsyncLocalStorage" inside "config/app.ts" file'
|
|
4936
3917
|
);
|
|
4937
3918
|
}
|
|
4938
3919
|
const store = this.get();
|
|
4939
3920
|
if (!store) {
|
|
4940
|
-
throw new
|
|
3921
|
+
throw new RuntimeException4("Http context is not available outside of an HTTP request");
|
|
4941
3922
|
}
|
|
4942
3923
|
return store;
|
|
4943
3924
|
}
|
|
@@ -4979,40 +3960,8 @@ var HttpContext = class extends Macroable8 {
|
|
|
4979
3960
|
|
|
4980
3961
|
// src/server/main.ts
|
|
4981
3962
|
import onFinished2 from "on-finished";
|
|
4982
|
-
import
|
|
4983
|
-
import { moduleCaller
|
|
4984
|
-
|
|
4985
|
-
// src/qs.ts
|
|
4986
|
-
import { parse as parse2, stringify } from "qs";
|
|
4987
|
-
var Qs = class {
|
|
4988
|
-
/**
|
|
4989
|
-
* Configuration object containing parse and stringify options for query strings
|
|
4990
|
-
*/
|
|
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
|
-
*/
|
|
4996
|
-
constructor(config) {
|
|
4997
|
-
this.#config = config;
|
|
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
|
-
*/
|
|
5004
|
-
parse = (value) => {
|
|
5005
|
-
return parse2(value, this.#config.parse);
|
|
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
|
-
*/
|
|
5012
|
-
stringify = (value) => {
|
|
5013
|
-
return stringify(value, this.#config.stringify);
|
|
5014
|
-
};
|
|
5015
|
-
};
|
|
3963
|
+
import Middleware from "@poppinss/middleware";
|
|
3964
|
+
import { moduleCaller, moduleImporter as moduleImporter3 } from "@adonisjs/fold";
|
|
5016
3965
|
|
|
5017
3966
|
// src/server/factories/route_finder.ts
|
|
5018
3967
|
function routeFinder(router, resolver, ctx, errorResponder) {
|
|
@@ -5185,7 +4134,7 @@ var Server = class {
|
|
|
5185
4134
|
* Compiles registered middleware into a frozen middleware stack for execution
|
|
5186
4135
|
*/
|
|
5187
4136
|
#createServerMiddlewareStack() {
|
|
5188
|
-
this.#serverMiddlewareStack = new
|
|
4137
|
+
this.#serverMiddlewareStack = new Middleware();
|
|
5189
4138
|
this.#middleware.forEach((middleware) => this.#serverMiddlewareStack.add(middleware));
|
|
5190
4139
|
this.#serverMiddlewareStack.freeze();
|
|
5191
4140
|
this.#middleware = [];
|
|
@@ -5210,11 +4159,11 @@ var Server = class {
|
|
|
5210
4159
|
* @returns TestingMiddlewarePipeline instance for test execution
|
|
5211
4160
|
*/
|
|
5212
4161
|
pipeline(middleware) {
|
|
5213
|
-
const middlewareStack = new
|
|
4162
|
+
const middlewareStack = new Middleware();
|
|
5214
4163
|
middleware.forEach((one) => {
|
|
5215
4164
|
middlewareStack.add({
|
|
5216
4165
|
reference: one,
|
|
5217
|
-
...
|
|
4166
|
+
...moduleCaller(one, "handle").toHandleMethod()
|
|
5218
4167
|
});
|
|
5219
4168
|
});
|
|
5220
4169
|
middlewareStack.freeze();
|
|
@@ -5245,7 +4194,7 @@ var Server = class {
|
|
|
5245
4194
|
middleware.forEach(
|
|
5246
4195
|
(one) => this.#middleware.push({
|
|
5247
4196
|
reference: one,
|
|
5248
|
-
...
|
|
4197
|
+
...moduleImporter3(one, "handle").toHandleMethod()
|
|
5249
4198
|
})
|
|
5250
4199
|
);
|
|
5251
4200
|
return this;
|
|
@@ -5404,7 +4353,7 @@ var Server = class {
|
|
|
5404
4353
|
|
|
5405
4354
|
// src/define_config.ts
|
|
5406
4355
|
import proxyAddr from "proxy-addr";
|
|
5407
|
-
import
|
|
4356
|
+
import string from "@poppinss/utils/string";
|
|
5408
4357
|
import lodash2 from "@poppinss/utils/lodash";
|
|
5409
4358
|
function defineConfig(config) {
|
|
5410
4359
|
const { trustProxy: trustProxy2, ...rest } = config;
|
|
@@ -5444,7 +4393,7 @@ function defineConfig(config) {
|
|
|
5444
4393
|
};
|
|
5445
4394
|
const normalizedConfig = lodash2.merge({}, defaults, rest);
|
|
5446
4395
|
if (normalizedConfig.cookie.maxAge) {
|
|
5447
|
-
normalizedConfig.cookie.maxAge =
|
|
4396
|
+
normalizedConfig.cookie.maxAge = string.seconds.parse(normalizedConfig.cookie.maxAge);
|
|
5448
4397
|
}
|
|
5449
4398
|
if (typeof trustProxy2 === "boolean") {
|
|
5450
4399
|
const tpValue = trustProxy2;
|
|
@@ -5459,6 +4408,7 @@ function defineConfig(config) {
|
|
|
5459
4408
|
}
|
|
5460
4409
|
|
|
5461
4410
|
export {
|
|
4411
|
+
Qs,
|
|
5462
4412
|
E_ROUTE_NOT_FOUND,
|
|
5463
4413
|
E_CANNOT_LOOKUP_ROUTE,
|
|
5464
4414
|
E_HTTP_EXCEPTION,
|
|
@@ -5466,19 +4416,11 @@ export {
|
|
|
5466
4416
|
errors_exports,
|
|
5467
4417
|
CookieClient,
|
|
5468
4418
|
CookieParser,
|
|
5469
|
-
canWriteResponseBody,
|
|
5470
|
-
tracing_channels_exports,
|
|
5471
|
-
Route,
|
|
5472
|
-
BriskRoute,
|
|
5473
|
-
RouteResource,
|
|
5474
|
-
RouteGroup,
|
|
5475
|
-
parseRange,
|
|
5476
4419
|
Request,
|
|
5477
4420
|
Redirect,
|
|
5478
4421
|
ResponseStatus,
|
|
5479
4422
|
CookieSerializer,
|
|
5480
4423
|
Response,
|
|
5481
|
-
Qs,
|
|
5482
4424
|
Router,
|
|
5483
4425
|
HttpContext,
|
|
5484
4426
|
Server,
|