@adonisjs/http-server 7.0.0-1 → 7.0.0-2
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-Z63E3STR.js +4436 -0
- package/build/chunk-Z63E3STR.js.map +1 -0
- package/build/factories/main.js +332 -14
- package/build/factories/main.js.map +1 -0
- package/build/index.js +309 -22
- package/build/index.js.map +1 -0
- package/build/src/router/lookup_store/main.d.ts +1 -3
- package/build/src/router/lookup_store/route_finder.d.ts +5 -1
- package/build/src/router/main.d.ts +2 -2
- package/build/src/router/resource.d.ts +19 -7
- package/build/src/types/main.js +1 -15
- package/build/src/types/main.js.map +1 -0
- package/package.json +52 -51
- package/build/factories/http_context.js +0 -51
- package/build/factories/http_server.js +0 -26
- package/build/factories/qs_parser_factory.js +0 -44
- package/build/factories/request.js +0 -73
- package/build/factories/response.js +0 -77
- package/build/factories/router.js +0 -45
- package/build/factories/server_factory.js +0 -65
- package/build/src/cookies/client.js +0 -84
- package/build/src/cookies/drivers/encrypted.js +0 -36
- package/build/src/cookies/drivers/plain.js +0 -33
- package/build/src/cookies/drivers/signed.js +0 -36
- package/build/src/cookies/parser.js +0 -167
- package/build/src/cookies/serializer.js +0 -79
- package/build/src/debug.js +0 -10
- package/build/src/define_config.js +0 -68
- package/build/src/define_middleware.js +0 -35
- package/build/src/exception_handler.js +0 -306
- package/build/src/exceptions.js +0 -38
- package/build/src/helpers.js +0 -105
- package/build/src/http_context/local_storage.js +0 -39
- package/build/src/http_context/main.js +0 -105
- package/build/src/qs.js +0 -25
- package/build/src/redirect.js +0 -140
- package/build/src/request.js +0 -865
- package/build/src/response.js +0 -1208
- package/build/src/router/brisk.js +0 -85
- package/build/src/router/executor.js +0 -30
- package/build/src/router/factories/use_return_value.js +0 -22
- package/build/src/router/group.js +0 -207
- package/build/src/router/lookup_store/main.js +0 -86
- package/build/src/router/lookup_store/route_finder.js +0 -49
- package/build/src/router/lookup_store/url_builder.js +0 -209
- package/build/src/router/main.js +0 -316
- package/build/src/router/matchers.js +0 -36
- package/build/src/router/parser.js +0 -17
- package/build/src/router/resource.js +0 -216
- package/build/src/router/route.js +0 -293
- package/build/src/router/store.js +0 -195
- package/build/src/server/factories/final_handler.js +0 -30
- package/build/src/server/factories/middleware_handler.js +0 -16
- package/build/src/server/factories/write_response.js +0 -24
- package/build/src/server/main.js +0 -292
- package/build/src/types/base.js +0 -9
- package/build/src/types/middleware.js +0 -9
- package/build/src/types/qs.js +0 -9
- package/build/src/types/request.js +0 -9
- package/build/src/types/response.js +0 -9
- package/build/src/types/route.js +0 -9
- package/build/src/types/server.js +0 -9
|
@@ -1,167 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,79 +0,0 @@
|
|
|
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
|
-
}
|
package/build/src/debug.js
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
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');
|
|
@@ -1,68 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,35 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,306 +0,0 @@
|
|
|
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 is from '@sindresorhus/is';
|
|
10
|
-
import Macroable from '@poppinss/macroable';
|
|
11
|
-
import { parseRange } from './helpers.js';
|
|
12
|
-
import * as errors from './exceptions.js';
|
|
13
|
-
/**
|
|
14
|
-
* The base HTTP exception handler one can inherit from to handle
|
|
15
|
-
* HTTP exceptions.
|
|
16
|
-
*
|
|
17
|
-
* The HTTP exception handler has support for
|
|
18
|
-
*
|
|
19
|
-
* - Ability to render exceptions by calling the render method on the exception.
|
|
20
|
-
* - Rendering status pages
|
|
21
|
-
* - Pretty printing errors during development
|
|
22
|
-
* - Transforming errors to JSON or HTML using content negotiation
|
|
23
|
-
* - Reporting errors
|
|
24
|
-
*/
|
|
25
|
-
export class ExceptionHandler extends Macroable {
|
|
26
|
-
/**
|
|
27
|
-
* Computed from the status pages property
|
|
28
|
-
*/
|
|
29
|
-
#expandedStatusPages;
|
|
30
|
-
/**
|
|
31
|
-
* Whether or not to render debug info. When set to true, the errors
|
|
32
|
-
* will have the complete error stack.
|
|
33
|
-
*/
|
|
34
|
-
debug = process.env.NODE_ENV !== 'production';
|
|
35
|
-
/**
|
|
36
|
-
* Whether or not to render status pages. When set to true, the unhandled
|
|
37
|
-
* errors with matching status codes will be rendered using a status
|
|
38
|
-
* page.
|
|
39
|
-
*/
|
|
40
|
-
renderStatusPages = process.env.NODE_ENV === 'production';
|
|
41
|
-
/**
|
|
42
|
-
* A collection of error status code range and the view to render.
|
|
43
|
-
*/
|
|
44
|
-
statusPages = {};
|
|
45
|
-
/**
|
|
46
|
-
* Enable/disable errors reporting
|
|
47
|
-
*/
|
|
48
|
-
reportErrors = true;
|
|
49
|
-
/**
|
|
50
|
-
* An array of exception classes to ignore when
|
|
51
|
-
* reporting an error
|
|
52
|
-
*/
|
|
53
|
-
ignoreExceptions = [
|
|
54
|
-
errors.E_HTTP_EXCEPTION,
|
|
55
|
-
errors.E_ROUTE_NOT_FOUND,
|
|
56
|
-
errors.E_CANNOT_LOOKUP_ROUTE,
|
|
57
|
-
errors.E_HTTP_REQUEST_ABORTED,
|
|
58
|
-
];
|
|
59
|
-
/**
|
|
60
|
-
* An array of HTTP status codes to ignore when reporting
|
|
61
|
-
* an error
|
|
62
|
-
*/
|
|
63
|
-
ignoreStatuses = [400, 422, 401];
|
|
64
|
-
/**
|
|
65
|
-
* An array of error codes to ignore when reporting
|
|
66
|
-
* an error
|
|
67
|
-
*/
|
|
68
|
-
ignoreCodes = [];
|
|
69
|
-
/**
|
|
70
|
-
* Expands status pages
|
|
71
|
-
*/
|
|
72
|
-
#expandStatusPages() {
|
|
73
|
-
if (!this.#expandedStatusPages) {
|
|
74
|
-
this.#expandedStatusPages = Object.keys(this.statusPages).reduce((result, range) => {
|
|
75
|
-
const renderer = this.statusPages[range];
|
|
76
|
-
result = Object.assign(result, parseRange(range, renderer));
|
|
77
|
-
return result;
|
|
78
|
-
}, {});
|
|
79
|
-
}
|
|
80
|
-
return this.#expandedStatusPages;
|
|
81
|
-
}
|
|
82
|
-
/**
|
|
83
|
-
* Forcefully tweaking the type of the error object to
|
|
84
|
-
* have known properties.
|
|
85
|
-
*/
|
|
86
|
-
#toHttpError(error) {
|
|
87
|
-
const httpError = is.object(error) ? error : new Error(String(error));
|
|
88
|
-
httpError.message = httpError.message || 'Internal server error';
|
|
89
|
-
httpError.status = httpError.status || 500;
|
|
90
|
-
return httpError;
|
|
91
|
-
}
|
|
92
|
-
/**
|
|
93
|
-
* Error reporting context
|
|
94
|
-
*/
|
|
95
|
-
context(ctx) {
|
|
96
|
-
const requestId = ctx.request.id();
|
|
97
|
-
return requestId
|
|
98
|
-
? {
|
|
99
|
-
'x-request-id': requestId,
|
|
100
|
-
}
|
|
101
|
-
: {};
|
|
102
|
-
}
|
|
103
|
-
/**
|
|
104
|
-
* Returns the log level for an error based upon the error
|
|
105
|
-
* status code.
|
|
106
|
-
*/
|
|
107
|
-
getErrorLogLevel(error) {
|
|
108
|
-
if (error.status >= 500) {
|
|
109
|
-
return 'error';
|
|
110
|
-
}
|
|
111
|
-
if (error.status >= 400) {
|
|
112
|
-
return 'warn';
|
|
113
|
-
}
|
|
114
|
-
return 'info';
|
|
115
|
-
}
|
|
116
|
-
/**
|
|
117
|
-
* A boolean to control if errors should be rendered with
|
|
118
|
-
* all the available debugging info.
|
|
119
|
-
*/
|
|
120
|
-
isDebuggingEnabled(_) {
|
|
121
|
-
return this.debug;
|
|
122
|
-
}
|
|
123
|
-
/**
|
|
124
|
-
* Returns a boolean by checking if an error should be reported.
|
|
125
|
-
*/
|
|
126
|
-
shouldReport(error) {
|
|
127
|
-
if (this.reportErrors === false) {
|
|
128
|
-
return false;
|
|
129
|
-
}
|
|
130
|
-
if (this.ignoreStatuses.includes(error.status)) {
|
|
131
|
-
return false;
|
|
132
|
-
}
|
|
133
|
-
if (error.code && this.ignoreCodes.includes(error.code)) {
|
|
134
|
-
return false;
|
|
135
|
-
}
|
|
136
|
-
if (this.ignoreExceptions.find((exception) => error instanceof exception)) {
|
|
137
|
-
return false;
|
|
138
|
-
}
|
|
139
|
-
return true;
|
|
140
|
-
}
|
|
141
|
-
/**
|
|
142
|
-
* Renders an error to JSON response
|
|
143
|
-
*/
|
|
144
|
-
async renderErrorAsJSON(error, ctx) {
|
|
145
|
-
if (this.isDebuggingEnabled(ctx)) {
|
|
146
|
-
const { default: Youch } = await import('youch');
|
|
147
|
-
const json = await new Youch(error, ctx.request.request).toJSON();
|
|
148
|
-
ctx.response.status(error.status).send(json.error);
|
|
149
|
-
return;
|
|
150
|
-
}
|
|
151
|
-
ctx.response.status(error.status).send({ message: error.message });
|
|
152
|
-
}
|
|
153
|
-
/**
|
|
154
|
-
* Renders an error to JSON API response
|
|
155
|
-
*/
|
|
156
|
-
async renderErrorAsJSONAPI(error, ctx) {
|
|
157
|
-
if (this.isDebuggingEnabled(ctx)) {
|
|
158
|
-
const { default: Youch } = await import('youch');
|
|
159
|
-
const json = await new Youch(error, ctx.request.request).toJSON();
|
|
160
|
-
ctx.response.status(error.status).send(json.error);
|
|
161
|
-
return;
|
|
162
|
-
}
|
|
163
|
-
ctx.response.status(error.status).send({
|
|
164
|
-
errors: [
|
|
165
|
-
{
|
|
166
|
-
title: error.message,
|
|
167
|
-
code: error.code,
|
|
168
|
-
status: error.status,
|
|
169
|
-
},
|
|
170
|
-
],
|
|
171
|
-
});
|
|
172
|
-
}
|
|
173
|
-
/**
|
|
174
|
-
* Renders an error to HTML response
|
|
175
|
-
*/
|
|
176
|
-
async renderErrorAsHTML(error, ctx) {
|
|
177
|
-
if (this.isDebuggingEnabled(ctx)) {
|
|
178
|
-
const { default: Youch } = await import('youch');
|
|
179
|
-
const html = await new Youch(error, ctx.request.request).toHTML({
|
|
180
|
-
cspNonce: 'nonce' in ctx.response ? ctx.response.nonce : undefined,
|
|
181
|
-
});
|
|
182
|
-
ctx.response.status(error.status).send(html);
|
|
183
|
-
return;
|
|
184
|
-
}
|
|
185
|
-
ctx.response.status(error.status).send(`<p> ${error.message} </p>`);
|
|
186
|
-
}
|
|
187
|
-
/**
|
|
188
|
-
* Renders the validation error message to a JSON
|
|
189
|
-
* response
|
|
190
|
-
*/
|
|
191
|
-
async renderValidationErrorAsJSON(error, ctx) {
|
|
192
|
-
ctx.response.status(error.status).send({
|
|
193
|
-
errors: error.messages,
|
|
194
|
-
});
|
|
195
|
-
}
|
|
196
|
-
/**
|
|
197
|
-
* Renders the validation error message as per JSON API
|
|
198
|
-
* spec
|
|
199
|
-
*/
|
|
200
|
-
async renderValidationErrorAsJSONAPI(error, ctx) {
|
|
201
|
-
ctx.response.status(error.status).send({
|
|
202
|
-
errors: error.messages.map((message) => {
|
|
203
|
-
return {
|
|
204
|
-
title: message.message,
|
|
205
|
-
code: message.rule,
|
|
206
|
-
source: {
|
|
207
|
-
pointer: message.field,
|
|
208
|
-
},
|
|
209
|
-
meta: message.meta,
|
|
210
|
-
};
|
|
211
|
-
}),
|
|
212
|
-
});
|
|
213
|
-
}
|
|
214
|
-
/**
|
|
215
|
-
* Renders the validation error as an HTML string
|
|
216
|
-
*/
|
|
217
|
-
async renderValidationErrorAsHTML(error, ctx) {
|
|
218
|
-
ctx.response
|
|
219
|
-
.status(error.status)
|
|
220
|
-
.type('html')
|
|
221
|
-
.send(error.messages
|
|
222
|
-
.map((message) => {
|
|
223
|
-
return `${message.field} - ${message.message}`;
|
|
224
|
-
})
|
|
225
|
-
.join('<br />'));
|
|
226
|
-
}
|
|
227
|
-
/**
|
|
228
|
-
* Renders the error to response
|
|
229
|
-
*/
|
|
230
|
-
renderError(error, ctx) {
|
|
231
|
-
switch (ctx.request.accepts(['html', 'application/vnd.api+json', 'json'])) {
|
|
232
|
-
case 'application/vnd.api+json':
|
|
233
|
-
return this.renderErrorAsJSONAPI(error, ctx);
|
|
234
|
-
case 'json':
|
|
235
|
-
return this.renderErrorAsJSON(error, ctx);
|
|
236
|
-
case 'html':
|
|
237
|
-
default:
|
|
238
|
-
return this.renderErrorAsHTML(error, ctx);
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
/**
|
|
242
|
-
* Renders the validation error to response
|
|
243
|
-
*/
|
|
244
|
-
renderValidationError(error, ctx) {
|
|
245
|
-
switch (ctx.request.accepts(['html', 'application/vnd.api+json', 'json'])) {
|
|
246
|
-
case 'application/vnd.api+json':
|
|
247
|
-
return this.renderValidationErrorAsJSONAPI(error, ctx);
|
|
248
|
-
case 'json':
|
|
249
|
-
return this.renderValidationErrorAsJSON(error, ctx);
|
|
250
|
-
case 'html':
|
|
251
|
-
default:
|
|
252
|
-
return this.renderValidationErrorAsHTML(error, ctx);
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
/**
|
|
256
|
-
* Reports an error during an HTTP request
|
|
257
|
-
*/
|
|
258
|
-
async report(error, ctx) {
|
|
259
|
-
const httpError = this.#toHttpError(error);
|
|
260
|
-
if (!this.shouldReport(httpError)) {
|
|
261
|
-
return;
|
|
262
|
-
}
|
|
263
|
-
if (typeof httpError.report === 'function') {
|
|
264
|
-
httpError.report(httpError, ctx);
|
|
265
|
-
return;
|
|
266
|
-
}
|
|
267
|
-
/**
|
|
268
|
-
* Log the error using the logger
|
|
269
|
-
*/
|
|
270
|
-
const level = this.getErrorLogLevel(httpError);
|
|
271
|
-
ctx.logger.log(level, {
|
|
272
|
-
...(level === 'error' || level === 'fatal' ? { err: httpError } : {}),
|
|
273
|
-
...this.context(ctx),
|
|
274
|
-
}, httpError.message);
|
|
275
|
-
}
|
|
276
|
-
/**
|
|
277
|
-
* Handles the error during the HTTP request.
|
|
278
|
-
*/
|
|
279
|
-
async handle(error, ctx) {
|
|
280
|
-
const httpError = this.#toHttpError(error);
|
|
281
|
-
/**
|
|
282
|
-
* Self handle exception
|
|
283
|
-
*/
|
|
284
|
-
if (typeof httpError.handle === 'function') {
|
|
285
|
-
return httpError.handle(httpError, ctx);
|
|
286
|
-
}
|
|
287
|
-
/**
|
|
288
|
-
* Handle validation error using the validation error
|
|
289
|
-
* renderers
|
|
290
|
-
*/
|
|
291
|
-
if (httpError.code === 'E_VALIDATION_ERROR' && 'messages' in httpError) {
|
|
292
|
-
return this.renderValidationError(httpError, ctx);
|
|
293
|
-
}
|
|
294
|
-
/**
|
|
295
|
-
* Render status page
|
|
296
|
-
*/
|
|
297
|
-
const statusPages = this.#expandStatusPages();
|
|
298
|
-
if (this.renderStatusPages && statusPages[httpError.status]) {
|
|
299
|
-
return statusPages[httpError.status](httpError, ctx);
|
|
300
|
-
}
|
|
301
|
-
/**
|
|
302
|
-
* Use the format renderers.
|
|
303
|
-
*/
|
|
304
|
-
return this.renderError(httpError, ctx);
|
|
305
|
-
}
|
|
306
|
-
}
|
package/build/src/exceptions.js
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
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 { createError, Exception } from '@poppinss/utils';
|
|
10
|
-
export const E_ROUTE_NOT_FOUND = createError('Cannot %s:%s', 'E_ROUTE_NOT_FOUND', 404);
|
|
11
|
-
export const E_CANNOT_LOOKUP_ROUTE = createError('Cannot lookup route "%s"', 'E_CANNOT_LOOKUP_ROUTE', 500);
|
|
12
|
-
export const E_HTTP_EXCEPTION = class HttpException extends Exception {
|
|
13
|
-
body;
|
|
14
|
-
static code = 'E_HTTP_EXCEPTION';
|
|
15
|
-
/**
|
|
16
|
-
* This method returns an instance of the exception class
|
|
17
|
-
*/
|
|
18
|
-
static invoke(body, status, code = 'E_HTTP_EXCEPTION') {
|
|
19
|
-
if (body === null || body === undefined) {
|
|
20
|
-
const error = new this('HTTP Exception', { status, code });
|
|
21
|
-
error.body = 'Internal server error';
|
|
22
|
-
return error;
|
|
23
|
-
}
|
|
24
|
-
if (typeof body === 'object') {
|
|
25
|
-
const error = new this(body.message || 'HTTP Exception', { status, code });
|
|
26
|
-
error.body = body;
|
|
27
|
-
return error;
|
|
28
|
-
}
|
|
29
|
-
const error = new this(body, { status, code });
|
|
30
|
-
error.body = body;
|
|
31
|
-
return error;
|
|
32
|
-
}
|
|
33
|
-
};
|
|
34
|
-
export const E_HTTP_REQUEST_ABORTED = class AbortException extends E_HTTP_EXCEPTION {
|
|
35
|
-
handle(error, ctx) {
|
|
36
|
-
ctx.response.status(error.status).send(error.body);
|
|
37
|
-
}
|
|
38
|
-
};
|