@adonisjs/http-server 7.0.0-0 → 7.0.0-1
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/factories/http_context.d.ts +25 -0
- package/build/factories/http_context.js +51 -0
- package/build/factories/http_server.d.ts +8 -0
- package/build/factories/http_server.js +26 -0
- package/build/factories/main.d.ts +6 -149
- package/build/factories/main.js +14 -331
- package/build/factories/qs_parser_factory.d.ts +20 -0
- package/build/factories/qs_parser_factory.js +44 -0
- package/build/factories/request.d.ts +29 -0
- package/build/factories/request.js +73 -0
- package/build/factories/response.d.ts +29 -0
- package/build/factories/response.js +77 -0
- package/build/factories/router.d.ts +23 -0
- package/build/factories/router.js +45 -0
- package/build/factories/server_factory.d.ts +29 -0
- package/build/factories/server_factory.js +65 -0
- package/build/index.d.ts +14 -272
- package/build/index.js +22 -308
- package/build/src/cookies/client.d.ts +37 -0
- package/build/src/cookies/client.js +84 -0
- package/build/src/cookies/drivers/encrypted.d.ts +16 -0
- package/build/src/cookies/drivers/encrypted.js +36 -0
- package/build/src/cookies/drivers/plain.d.ts +15 -0
- package/build/src/cookies/drivers/plain.js +33 -0
- package/build/src/cookies/drivers/signed.d.ts +16 -0
- package/build/src/cookies/drivers/signed.js +36 -0
- package/build/src/cookies/parser.d.ts +37 -0
- package/build/src/cookies/parser.js +167 -0
- package/build/src/cookies/serializer.d.ts +33 -0
- package/build/src/cookies/serializer.js +79 -0
- package/build/src/debug.d.ts +3 -0
- package/build/src/debug.js +10 -0
- package/build/src/define_config.d.ts +9 -0
- package/build/src/define_config.js +68 -0
- package/build/src/define_middleware.d.ts +11 -0
- package/build/src/define_middleware.js +35 -0
- package/build/src/exception_handler.d.ts +113 -0
- package/build/src/exception_handler.js +306 -0
- package/build/src/exceptions.d.ts +84 -0
- package/build/src/exceptions.js +38 -0
- package/build/src/helpers.d.ts +23 -0
- package/build/src/helpers.js +105 -0
- package/build/src/http_context/local_storage.d.ts +12 -0
- package/build/src/http_context/local_storage.js +39 -0
- package/build/src/http_context/main.d.ts +58 -0
- package/build/src/http_context/main.js +105 -0
- package/build/src/qs.d.ts +11 -0
- package/build/src/qs.js +25 -0
- package/build/src/redirect.d.ts +42 -0
- package/build/src/redirect.js +140 -0
- package/build/src/request.d.ts +565 -0
- package/build/src/request.js +865 -0
- package/build/src/response.d.ts +620 -0
- package/build/src/response.js +1208 -0
- package/build/src/router/brisk.d.ts +42 -0
- package/build/src/router/brisk.js +85 -0
- package/build/src/router/executor.d.ts +9 -0
- package/build/src/router/executor.js +30 -0
- package/build/src/router/factories/use_return_value.d.ts +6 -0
- package/build/src/router/factories/use_return_value.js +22 -0
- package/build/src/router/group.d.ts +65 -0
- package/build/src/router/group.js +207 -0
- package/build/src/router/lookup_store/main.d.ts +49 -0
- package/build/src/router/lookup_store/main.js +86 -0
- package/build/src/router/lookup_store/route_finder.d.ts +21 -0
- package/build/src/router/lookup_store/route_finder.js +49 -0
- package/build/src/router/lookup_store/url_builder.d.ts +52 -0
- package/build/src/router/lookup_store/url_builder.js +209 -0
- package/build/src/router/main.d.ts +128 -0
- package/build/src/router/main.js +316 -0
- package/build/src/router/matchers.d.ts +27 -0
- package/build/src/router/matchers.js +36 -0
- package/build/src/router/parser.d.ts +5 -0
- package/build/src/router/parser.js +17 -0
- package/build/src/router/resource.d.ts +54 -0
- package/build/src/router/resource.js +216 -0
- package/build/src/router/route.d.ts +92 -0
- package/build/src/router/route.js +293 -0
- package/build/src/router/store.d.ts +66 -0
- package/build/src/router/store.js +195 -0
- package/build/src/server/factories/final_handler.d.ts +10 -0
- package/build/src/server/factories/final_handler.js +30 -0
- package/build/src/server/factories/middleware_handler.d.ts +8 -0
- package/build/src/server/factories/middleware_handler.js +16 -0
- package/build/src/server/factories/write_response.d.ts +6 -0
- package/build/src/server/factories/write_response.js +24 -0
- package/build/{main-29eaaee4.d.ts → src/server/main.d.ts} +18 -13
- package/build/src/server/main.js +292 -0
- package/build/src/types/base.d.ts +19 -0
- package/build/src/types/base.js +9 -0
- package/build/src/types/main.d.ts +7 -14
- package/build/src/types/main.js +15 -0
- package/build/src/types/middleware.d.ts +35 -0
- package/build/src/types/middleware.js +9 -0
- package/build/src/types/qs.d.ts +68 -0
- package/build/src/types/qs.js +9 -0
- package/build/src/types/request.d.ts +39 -0
- package/build/src/types/request.js +9 -0
- package/build/src/types/response.d.ts +45 -0
- package/build/src/types/response.js +9 -0
- package/build/src/types/route.d.ts +166 -0
- package/build/src/types/route.js +9 -0
- package/build/src/types/server.d.ts +72 -0
- package/build/src/types/server.js +9 -0
- package/package.json +6 -3
- package/build/chunk-XX72ATFY.js +0 -4388
- package/build/main-e5b46c83.d.ts +0 -2210
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @adonisjs/http-server
|
|
3
|
+
*
|
|
4
|
+
* (c) AdonisJS
|
|
5
|
+
*
|
|
6
|
+
* For the full copyright and license information, please view the LICENSE
|
|
7
|
+
* file that was distributed with this source code.
|
|
8
|
+
*/
|
|
9
|
+
import cookie from 'cookie';
|
|
10
|
+
import { CookieClient } from './client.js';
|
|
11
|
+
/**
|
|
12
|
+
* Cookie parser parses the HTTP `cookie` header and collects all cookies
|
|
13
|
+
* inside an object of `key-value` pair, but doesn't attempt to decrypt
|
|
14
|
+
* or unsign or decode the individual values.
|
|
15
|
+
*
|
|
16
|
+
* The cookie values are lazily decrypted, or unsigned to avoid unncessary
|
|
17
|
+
* processing, which infact can be used as a means to burden the server
|
|
18
|
+
* by sending too many cookies which even doesn't belongs to the
|
|
19
|
+
* server.
|
|
20
|
+
*/
|
|
21
|
+
export class CookieParser {
|
|
22
|
+
#client;
|
|
23
|
+
/**
|
|
24
|
+
* A copy of cached cookies, they are cached during a request after
|
|
25
|
+
* initial decoding, unsigning or decrypting.
|
|
26
|
+
*/
|
|
27
|
+
#cachedCookies = {
|
|
28
|
+
signedCookies: {},
|
|
29
|
+
plainCookies: {},
|
|
30
|
+
encryptedCookies: {},
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* An object of key-value pair collected by parsing
|
|
34
|
+
* the request cookie header.
|
|
35
|
+
*/
|
|
36
|
+
#cookies;
|
|
37
|
+
constructor(cookieHeader, encryption) {
|
|
38
|
+
this.#client = new CookieClient(encryption);
|
|
39
|
+
this.#cookies = this.#parse(cookieHeader);
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Parses the request `cookie` header
|
|
43
|
+
*/
|
|
44
|
+
#parse(cookieHeader) {
|
|
45
|
+
/*
|
|
46
|
+
* Set to empty object when cookie header is empty string
|
|
47
|
+
*/
|
|
48
|
+
if (!cookieHeader) {
|
|
49
|
+
return {};
|
|
50
|
+
}
|
|
51
|
+
/*
|
|
52
|
+
* Parse and store reference
|
|
53
|
+
*/
|
|
54
|
+
return cookie.parse(cookieHeader);
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Attempts to decode a cookie by the name. When calling this method,
|
|
58
|
+
* you are assuming that the cookie was just encoded in the first
|
|
59
|
+
* place and not signed or encrypted.
|
|
60
|
+
*/
|
|
61
|
+
decode(key, encoded = true) {
|
|
62
|
+
/*
|
|
63
|
+
* Ignore when initial value is not defined or null
|
|
64
|
+
*/
|
|
65
|
+
const value = this.#cookies[key];
|
|
66
|
+
if (value === null || value === undefined) {
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
/*
|
|
70
|
+
* Reference to the cache object. Mainly done to avoid typos,
|
|
71
|
+
* since this object is referenced a handful of times inside
|
|
72
|
+
* this method.
|
|
73
|
+
*/
|
|
74
|
+
const cache = this.#cachedCookies.plainCookies;
|
|
75
|
+
/*
|
|
76
|
+
* Return from cache, when already parsed
|
|
77
|
+
*/
|
|
78
|
+
if (cache[key] !== undefined) {
|
|
79
|
+
return cache[key];
|
|
80
|
+
}
|
|
81
|
+
/*
|
|
82
|
+
* Attempt to unpack and cache it for future. The value is only
|
|
83
|
+
* when value it is not null.
|
|
84
|
+
*/
|
|
85
|
+
const parsed = encoded ? this.#client.decode(key, value) : value;
|
|
86
|
+
if (parsed !== null) {
|
|
87
|
+
cache[key] = parsed;
|
|
88
|
+
}
|
|
89
|
+
return parsed;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Attempts to unsign a cookie by the name. When calling this method,
|
|
93
|
+
* you are assuming that the cookie was signed in the first place.
|
|
94
|
+
*/
|
|
95
|
+
unsign(key) {
|
|
96
|
+
/*
|
|
97
|
+
* Ignore when initial value is not defined or null
|
|
98
|
+
*/
|
|
99
|
+
const value = this.#cookies[key];
|
|
100
|
+
if (value === null || value === undefined) {
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
103
|
+
/*
|
|
104
|
+
* Reference to the cache object. Mainly done to avoid typos,
|
|
105
|
+
* since this object is referenced a handful of times inside
|
|
106
|
+
* this method.
|
|
107
|
+
*/
|
|
108
|
+
const cache = this.#cachedCookies.signedCookies;
|
|
109
|
+
/*
|
|
110
|
+
* Return from cache, when already parsed
|
|
111
|
+
*/
|
|
112
|
+
if (cache[key] !== undefined) {
|
|
113
|
+
return cache[key];
|
|
114
|
+
}
|
|
115
|
+
/*
|
|
116
|
+
* Attempt to unpack and cache it for future. The value is only
|
|
117
|
+
* when value it is not null.
|
|
118
|
+
*/
|
|
119
|
+
const parsed = this.#client.unsign(key, value);
|
|
120
|
+
if (parsed !== null) {
|
|
121
|
+
cache[key] = parsed;
|
|
122
|
+
}
|
|
123
|
+
return parsed;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Attempts to decrypt a cookie by the name. When calling this method,
|
|
127
|
+
* you are assuming that the cookie was encrypted in the first place.
|
|
128
|
+
*/
|
|
129
|
+
decrypt(key) {
|
|
130
|
+
/*
|
|
131
|
+
* Ignore when initial value is not defined or null
|
|
132
|
+
*/
|
|
133
|
+
const value = this.#cookies[key];
|
|
134
|
+
if (value === null || value === undefined) {
|
|
135
|
+
return null;
|
|
136
|
+
}
|
|
137
|
+
/*
|
|
138
|
+
* Reference to the cache object. Mainly done to avoid typos,
|
|
139
|
+
* since this object is referenced a handful of times inside
|
|
140
|
+
* this method.
|
|
141
|
+
*/
|
|
142
|
+
const cache = this.#cachedCookies.encryptedCookies;
|
|
143
|
+
/*
|
|
144
|
+
* Return from cache, when already parsed
|
|
145
|
+
*/
|
|
146
|
+
if (cache[key] !== undefined) {
|
|
147
|
+
return cache[key];
|
|
148
|
+
}
|
|
149
|
+
/*
|
|
150
|
+
* Attempt to unpack and cache it for future. The value is only
|
|
151
|
+
* when value it is not null.
|
|
152
|
+
*/
|
|
153
|
+
const parsed = this.#client.decrypt(key, value);
|
|
154
|
+
if (parsed !== null) {
|
|
155
|
+
cache[key] = parsed;
|
|
156
|
+
}
|
|
157
|
+
return parsed;
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Returns an object of cookies key-value pair. Do note, the
|
|
161
|
+
* cookies are not decoded, unsigned or decrypted inside this
|
|
162
|
+
* list.
|
|
163
|
+
*/
|
|
164
|
+
list() {
|
|
165
|
+
return this.#cookies;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { Encryption } from '@adonisjs/encryption';
|
|
2
|
+
import type { CookieOptions } from '../types/response.js';
|
|
3
|
+
/**
|
|
4
|
+
* Cookies serializer is used to serialize a value to be set on the `Set-Cookie`
|
|
5
|
+
* header. You can `encode`, `sign` on `encrypt` cookies using the serializer
|
|
6
|
+
* and then set them individually using the `set-cookie` header.
|
|
7
|
+
*/
|
|
8
|
+
export declare class CookieSerializer {
|
|
9
|
+
#private;
|
|
10
|
+
constructor(encryption: Encryption);
|
|
11
|
+
/**
|
|
12
|
+
* Encodes value as a plain cookie. By default, the plain value will be converted
|
|
13
|
+
* to a string using "JSON.stringify" method and then encoded as a base64 string.
|
|
14
|
+
*
|
|
15
|
+
* You can disable encoding of the cookie by setting `options.encoded = false`.
|
|
16
|
+
*
|
|
17
|
+
* ```ts
|
|
18
|
+
* serializer.encode('name', 'virk')
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
encode(key: string, value: any, options?: Partial<CookieOptions & {
|
|
22
|
+
encode: boolean;
|
|
23
|
+
}>): string | null;
|
|
24
|
+
/**
|
|
25
|
+
* Sign a key-value pair to a signed cookie. The signed value has a
|
|
26
|
+
* verification hash attached to it to detect data tampering.
|
|
27
|
+
*/
|
|
28
|
+
sign(key: string, value: any, options?: Partial<CookieOptions>): string | null;
|
|
29
|
+
/**
|
|
30
|
+
* Encrypts a key-value pair to an encrypted cookie.
|
|
31
|
+
*/
|
|
32
|
+
encrypt(key: string, value: any, options?: Partial<CookieOptions>): string | null;
|
|
33
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @adonisjs/http-server
|
|
3
|
+
*
|
|
4
|
+
* (c) AdonisJS
|
|
5
|
+
*
|
|
6
|
+
* For the full copyright and license information, please view the LICENSE
|
|
7
|
+
* file that was distributed with this source code.
|
|
8
|
+
*/
|
|
9
|
+
import cookie from 'cookie';
|
|
10
|
+
import string from '@poppinss/utils/string';
|
|
11
|
+
import { CookieClient } from './client.js';
|
|
12
|
+
/**
|
|
13
|
+
* Cookies serializer is used to serialize a value to be set on the `Set-Cookie`
|
|
14
|
+
* header. You can `encode`, `sign` on `encrypt` cookies using the serializer
|
|
15
|
+
* and then set them individually using the `set-cookie` header.
|
|
16
|
+
*/
|
|
17
|
+
export class CookieSerializer {
|
|
18
|
+
#client;
|
|
19
|
+
constructor(encryption) {
|
|
20
|
+
this.#client = new CookieClient(encryption);
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Serializes the key-value pair to a string, that can be set on the
|
|
24
|
+
* `Set-Cookie` header.
|
|
25
|
+
*/
|
|
26
|
+
#serializeAsCookie(key, value, options) {
|
|
27
|
+
/**
|
|
28
|
+
* Invoked expires method to get the date
|
|
29
|
+
*/
|
|
30
|
+
let expires = options?.expires;
|
|
31
|
+
if (typeof expires === 'function') {
|
|
32
|
+
expires = expires();
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Parse string based max age to seconds
|
|
36
|
+
*/
|
|
37
|
+
let maxAge = options?.maxAge ? string.seconds.parse(options?.maxAge) : undefined;
|
|
38
|
+
const parsedOptions = Object.assign({}, options, { maxAge, expires });
|
|
39
|
+
return cookie.serialize(key, value, parsedOptions);
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Encodes value as a plain cookie. By default, the plain value will be converted
|
|
43
|
+
* to a string using "JSON.stringify" method and then encoded as a base64 string.
|
|
44
|
+
*
|
|
45
|
+
* You can disable encoding of the cookie by setting `options.encoded = false`.
|
|
46
|
+
*
|
|
47
|
+
* ```ts
|
|
48
|
+
* serializer.encode('name', 'virk')
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
encode(key, value, options) {
|
|
52
|
+
const packedValue = options?.encode === false ? value : this.#client.encode(key, value);
|
|
53
|
+
if (packedValue === null || packedValue === undefined) {
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
return this.#serializeAsCookie(key, packedValue, options);
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Sign a key-value pair to a signed cookie. The signed value has a
|
|
60
|
+
* verification hash attached to it to detect data tampering.
|
|
61
|
+
*/
|
|
62
|
+
sign(key, value, options) {
|
|
63
|
+
const packedValue = this.#client.sign(key, value);
|
|
64
|
+
if (packedValue === null) {
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
return this.#serializeAsCookie(key, packedValue, options);
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Encrypts a key-value pair to an encrypted cookie.
|
|
71
|
+
*/
|
|
72
|
+
encrypt(key, value, options) {
|
|
73
|
+
const packedValue = this.#client.encrypt(key, value);
|
|
74
|
+
if (packedValue === null) {
|
|
75
|
+
return null;
|
|
76
|
+
}
|
|
77
|
+
return this.#serializeAsCookie(key, packedValue, options);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @adonisjs/http-server
|
|
3
|
+
*
|
|
4
|
+
* (c) AdonisJS
|
|
5
|
+
*
|
|
6
|
+
* For the full copyright and license information, please view the LICENSE
|
|
7
|
+
* file that was distributed with this source code.
|
|
8
|
+
*/
|
|
9
|
+
import { debuglog } from 'node:util';
|
|
10
|
+
export default debuglog('adonisjs:http');
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { ServerConfig } from './types/server.js';
|
|
2
|
+
type UserDefinedServerConfig = Partial<Omit<ServerConfig, 'trustProxy'> & {
|
|
3
|
+
trustProxy: ((address: string, distance: number) => boolean) | boolean | string;
|
|
4
|
+
}>;
|
|
5
|
+
/**
|
|
6
|
+
* Define configuration for the HTTP server
|
|
7
|
+
*/
|
|
8
|
+
export declare function defineConfig(config: UserDefinedServerConfig): ServerConfig;
|
|
9
|
+
export {};
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @adonisjs/http-server
|
|
3
|
+
*
|
|
4
|
+
* (c) AdonisJS
|
|
5
|
+
*
|
|
6
|
+
* For the full copyright and license information, please view the LICENSE
|
|
7
|
+
* file that was distributed with this source code.
|
|
8
|
+
*/
|
|
9
|
+
import proxyAddr from 'proxy-addr';
|
|
10
|
+
import string from '@poppinss/utils/string';
|
|
11
|
+
/**
|
|
12
|
+
* Define configuration for the HTTP server
|
|
13
|
+
*/
|
|
14
|
+
export function defineConfig(config) {
|
|
15
|
+
const { trustProxy, ...rest } = config;
|
|
16
|
+
const normalizedConfig = {
|
|
17
|
+
allowMethodSpoofing: false,
|
|
18
|
+
trustProxy: proxyAddr.compile('loopback'),
|
|
19
|
+
subdomainOffset: 2,
|
|
20
|
+
generateRequestId: false,
|
|
21
|
+
useAsyncLocalStorage: false,
|
|
22
|
+
etag: false,
|
|
23
|
+
jsonpCallbackName: 'callback',
|
|
24
|
+
cookie: {
|
|
25
|
+
maxAge: '2h',
|
|
26
|
+
path: '/',
|
|
27
|
+
httpOnly: true,
|
|
28
|
+
secure: false,
|
|
29
|
+
sameSite: false,
|
|
30
|
+
},
|
|
31
|
+
qs: {
|
|
32
|
+
parse: {
|
|
33
|
+
depth: 5,
|
|
34
|
+
parameterLimit: 1000,
|
|
35
|
+
allowSparse: false,
|
|
36
|
+
arrayLimit: 20,
|
|
37
|
+
comma: true,
|
|
38
|
+
},
|
|
39
|
+
stringify: {
|
|
40
|
+
encode: true,
|
|
41
|
+
encodeValuesOnly: false,
|
|
42
|
+
arrayFormat: 'indices',
|
|
43
|
+
skipNulls: false,
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
...rest,
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* Normalizing maxAge property on cookies to be a number in
|
|
50
|
+
* seconds
|
|
51
|
+
*/
|
|
52
|
+
if (normalizedConfig.cookie.maxAge) {
|
|
53
|
+
normalizedConfig.cookie.maxAge = string.seconds.parse(normalizedConfig.cookie.maxAge);
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Normalizing trust proxy setting to allow boolean and
|
|
57
|
+
* string values
|
|
58
|
+
*/
|
|
59
|
+
if (typeof trustProxy === 'boolean') {
|
|
60
|
+
const tpValue = trustProxy;
|
|
61
|
+
normalizedConfig.trustProxy = (_, __) => tpValue;
|
|
62
|
+
}
|
|
63
|
+
else if (typeof trustProxy === 'string') {
|
|
64
|
+
const tpValue = trustProxy;
|
|
65
|
+
normalizedConfig.trustProxy = proxyAddr.compile(tpValue);
|
|
66
|
+
}
|
|
67
|
+
return normalizedConfig;
|
|
68
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { LazyImport, UnWrapLazyImport } from './types/base.js';
|
|
2
|
+
import type { GetMiddlewareArgs, MiddlewareAsClass, ParsedGlobalMiddleware } from './types/middleware.js';
|
|
3
|
+
/**
|
|
4
|
+
* Define an collection of named middleware. The collection gets converted
|
|
5
|
+
* into a collection of factory functions. Calling the function returns
|
|
6
|
+
* a reference to the executable middleware.
|
|
7
|
+
*/
|
|
8
|
+
export declare function defineNamedMiddleware<NamedMiddleware extends Record<string | number | symbol, LazyImport<MiddlewareAsClass>>>(collection: NamedMiddleware): { [K in keyof NamedMiddleware]: <Args extends GetMiddlewareArgs<UnWrapLazyImport<NamedMiddleware[K]>>>(...args: Args) => {
|
|
9
|
+
name: K;
|
|
10
|
+
args: Args[0];
|
|
11
|
+
} & ParsedGlobalMiddleware; };
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @adonisjs/http-server
|
|
3
|
+
*
|
|
4
|
+
* (c) AdonisJS
|
|
5
|
+
*
|
|
6
|
+
* For the full copyright and license information, please view the LICENSE
|
|
7
|
+
* file that was distributed with this source code.
|
|
8
|
+
*/
|
|
9
|
+
import { moduleImporter } from '@adonisjs/fold';
|
|
10
|
+
/**
|
|
11
|
+
* Converts a middleware name and its lazy import to a factory function. The function
|
|
12
|
+
* can than later be used to reference the middleware with different arguments
|
|
13
|
+
* every time.
|
|
14
|
+
*/
|
|
15
|
+
function middlewareReferenceBuilder(name, middleware) {
|
|
16
|
+
const handler = moduleImporter(middleware, 'handle').toHandleMethod();
|
|
17
|
+
return function (...args) {
|
|
18
|
+
return {
|
|
19
|
+
name,
|
|
20
|
+
args: args[0],
|
|
21
|
+
...handler,
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Define an collection of named middleware. The collection gets converted
|
|
27
|
+
* into a collection of factory functions. Calling the function returns
|
|
28
|
+
* a reference to the executable middleware.
|
|
29
|
+
*/
|
|
30
|
+
export function defineNamedMiddleware(collection) {
|
|
31
|
+
return Object.keys(collection).reduce((result, key) => {
|
|
32
|
+
result[key] = middlewareReferenceBuilder(key, collection[key]);
|
|
33
|
+
return result;
|
|
34
|
+
}, {});
|
|
35
|
+
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import Macroable from '@poppinss/macroable';
|
|
2
|
+
import type { Level } from '@adonisjs/logger/types';
|
|
3
|
+
import type { HttpContext } from './http_context/main.js';
|
|
4
|
+
import type { HttpError, StatusPageRange, StatusPageRenderer } from './types/server.js';
|
|
5
|
+
/**
|
|
6
|
+
* The base HTTP exception handler one can inherit from to handle
|
|
7
|
+
* HTTP exceptions.
|
|
8
|
+
*
|
|
9
|
+
* The HTTP exception handler has support for
|
|
10
|
+
*
|
|
11
|
+
* - Ability to render exceptions by calling the render method on the exception.
|
|
12
|
+
* - Rendering status pages
|
|
13
|
+
* - Pretty printing errors during development
|
|
14
|
+
* - Transforming errors to JSON or HTML using content negotiation
|
|
15
|
+
* - Reporting errors
|
|
16
|
+
*/
|
|
17
|
+
export declare class ExceptionHandler extends Macroable {
|
|
18
|
+
#private;
|
|
19
|
+
/**
|
|
20
|
+
* Whether or not to render debug info. When set to true, the errors
|
|
21
|
+
* will have the complete error stack.
|
|
22
|
+
*/
|
|
23
|
+
protected debug: boolean;
|
|
24
|
+
/**
|
|
25
|
+
* Whether or not to render status pages. When set to true, the unhandled
|
|
26
|
+
* errors with matching status codes will be rendered using a status
|
|
27
|
+
* page.
|
|
28
|
+
*/
|
|
29
|
+
protected renderStatusPages: boolean;
|
|
30
|
+
/**
|
|
31
|
+
* A collection of error status code range and the view to render.
|
|
32
|
+
*/
|
|
33
|
+
protected statusPages: Record<StatusPageRange, StatusPageRenderer>;
|
|
34
|
+
/**
|
|
35
|
+
* Enable/disable errors reporting
|
|
36
|
+
*/
|
|
37
|
+
protected reportErrors: boolean;
|
|
38
|
+
/**
|
|
39
|
+
* An array of exception classes to ignore when
|
|
40
|
+
* reporting an error
|
|
41
|
+
*/
|
|
42
|
+
protected ignoreExceptions: any[];
|
|
43
|
+
/**
|
|
44
|
+
* An array of HTTP status codes to ignore when reporting
|
|
45
|
+
* an error
|
|
46
|
+
*/
|
|
47
|
+
protected ignoreStatuses: number[];
|
|
48
|
+
/**
|
|
49
|
+
* An array of error codes to ignore when reporting
|
|
50
|
+
* an error
|
|
51
|
+
*/
|
|
52
|
+
protected ignoreCodes: string[];
|
|
53
|
+
/**
|
|
54
|
+
* Error reporting context
|
|
55
|
+
*/
|
|
56
|
+
protected context(ctx: HttpContext): any;
|
|
57
|
+
/**
|
|
58
|
+
* Returns the log level for an error based upon the error
|
|
59
|
+
* status code.
|
|
60
|
+
*/
|
|
61
|
+
protected getErrorLogLevel(error: HttpError): Level;
|
|
62
|
+
/**
|
|
63
|
+
* A boolean to control if errors should be rendered with
|
|
64
|
+
* all the available debugging info.
|
|
65
|
+
*/
|
|
66
|
+
protected isDebuggingEnabled(_: HttpContext): boolean;
|
|
67
|
+
/**
|
|
68
|
+
* Returns a boolean by checking if an error should be reported.
|
|
69
|
+
*/
|
|
70
|
+
protected shouldReport(error: HttpError): boolean;
|
|
71
|
+
/**
|
|
72
|
+
* Renders an error to JSON response
|
|
73
|
+
*/
|
|
74
|
+
renderErrorAsJSON(error: HttpError, ctx: HttpContext): Promise<void>;
|
|
75
|
+
/**
|
|
76
|
+
* Renders an error to JSON API response
|
|
77
|
+
*/
|
|
78
|
+
renderErrorAsJSONAPI(error: HttpError, ctx: HttpContext): Promise<void>;
|
|
79
|
+
/**
|
|
80
|
+
* Renders an error to HTML response
|
|
81
|
+
*/
|
|
82
|
+
renderErrorAsHTML(error: HttpError, ctx: HttpContext): Promise<void>;
|
|
83
|
+
/**
|
|
84
|
+
* Renders the validation error message to a JSON
|
|
85
|
+
* response
|
|
86
|
+
*/
|
|
87
|
+
renderValidationErrorAsJSON(error: HttpError, ctx: HttpContext): Promise<void>;
|
|
88
|
+
/**
|
|
89
|
+
* Renders the validation error message as per JSON API
|
|
90
|
+
* spec
|
|
91
|
+
*/
|
|
92
|
+
renderValidationErrorAsJSONAPI(error: HttpError, ctx: HttpContext): Promise<void>;
|
|
93
|
+
/**
|
|
94
|
+
* Renders the validation error as an HTML string
|
|
95
|
+
*/
|
|
96
|
+
renderValidationErrorAsHTML(error: HttpError, ctx: HttpContext): Promise<void>;
|
|
97
|
+
/**
|
|
98
|
+
* Renders the error to response
|
|
99
|
+
*/
|
|
100
|
+
renderError(error: HttpError, ctx: HttpContext): Promise<void>;
|
|
101
|
+
/**
|
|
102
|
+
* Renders the validation error to response
|
|
103
|
+
*/
|
|
104
|
+
renderValidationError(error: HttpError, ctx: HttpContext): Promise<void>;
|
|
105
|
+
/**
|
|
106
|
+
* Reports an error during an HTTP request
|
|
107
|
+
*/
|
|
108
|
+
report(error: unknown, ctx: HttpContext): Promise<void>;
|
|
109
|
+
/**
|
|
110
|
+
* Handles the error during the HTTP request.
|
|
111
|
+
*/
|
|
112
|
+
handle(error: unknown, ctx: HttpContext): Promise<any>;
|
|
113
|
+
}
|