@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,306 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/// <reference types="node" resolution-mode="require"/>
|
|
2
|
+
import { Exception } from '@poppinss/utils';
|
|
3
|
+
import type { HttpContext } from './http_context/main.js';
|
|
4
|
+
export declare const E_ROUTE_NOT_FOUND: new (args: [method: string, url: string], options?: ErrorOptions | undefined) => Exception;
|
|
5
|
+
export declare const E_CANNOT_LOOKUP_ROUTE: new (args: [routeIdentifier: string], options?: ErrorOptions | undefined) => Exception;
|
|
6
|
+
export declare const E_HTTP_EXCEPTION: {
|
|
7
|
+
new (message?: string | undefined, options?: (ErrorOptions & {
|
|
8
|
+
code?: string | undefined;
|
|
9
|
+
status?: number | undefined;
|
|
10
|
+
}) | undefined): {
|
|
11
|
+
body: any;
|
|
12
|
+
name: string;
|
|
13
|
+
help?: string | undefined;
|
|
14
|
+
code?: string | undefined;
|
|
15
|
+
status: number;
|
|
16
|
+
toString(): string;
|
|
17
|
+
readonly [Symbol.toStringTag]: string;
|
|
18
|
+
message: string;
|
|
19
|
+
stack?: string | undefined;
|
|
20
|
+
cause?: unknown;
|
|
21
|
+
};
|
|
22
|
+
code: string;
|
|
23
|
+
/**
|
|
24
|
+
* This method returns an instance of the exception class
|
|
25
|
+
*/
|
|
26
|
+
invoke(body: any, status: number, code?: string): {
|
|
27
|
+
body: any;
|
|
28
|
+
name: string;
|
|
29
|
+
help?: string | undefined;
|
|
30
|
+
code?: string | undefined;
|
|
31
|
+
status: number;
|
|
32
|
+
toString(): string;
|
|
33
|
+
readonly [Symbol.toStringTag]: string;
|
|
34
|
+
message: string;
|
|
35
|
+
stack?: string | undefined;
|
|
36
|
+
cause?: unknown;
|
|
37
|
+
};
|
|
38
|
+
help?: string | undefined;
|
|
39
|
+
status?: number | undefined;
|
|
40
|
+
message?: string | undefined;
|
|
41
|
+
captureStackTrace(targetObject: object, constructorOpt?: Function | undefined): void;
|
|
42
|
+
prepareStackTrace?: ((err: Error, stackTraces: NodeJS.CallSite[]) => any) | undefined;
|
|
43
|
+
stackTraceLimit: number;
|
|
44
|
+
};
|
|
45
|
+
export declare const E_HTTP_REQUEST_ABORTED: {
|
|
46
|
+
new (message?: string | undefined, options?: (ErrorOptions & {
|
|
47
|
+
code?: string | undefined;
|
|
48
|
+
status?: number | undefined;
|
|
49
|
+
}) | undefined): {
|
|
50
|
+
handle(error: any, ctx: HttpContext): void;
|
|
51
|
+
body: any;
|
|
52
|
+
name: string;
|
|
53
|
+
help?: string | undefined;
|
|
54
|
+
code?: string | undefined;
|
|
55
|
+
status: number;
|
|
56
|
+
toString(): string;
|
|
57
|
+
readonly [Symbol.toStringTag]: string;
|
|
58
|
+
message: string;
|
|
59
|
+
stack?: string | undefined;
|
|
60
|
+
cause?: unknown;
|
|
61
|
+
};
|
|
62
|
+
code: string;
|
|
63
|
+
/**
|
|
64
|
+
* This method returns an instance of the exception class
|
|
65
|
+
*/
|
|
66
|
+
invoke(body: any, status: number, code?: string): {
|
|
67
|
+
body: any;
|
|
68
|
+
name: string;
|
|
69
|
+
help?: string | undefined;
|
|
70
|
+
code?: string | undefined;
|
|
71
|
+
status: number;
|
|
72
|
+
toString(): string;
|
|
73
|
+
readonly [Symbol.toStringTag]: string;
|
|
74
|
+
message: string;
|
|
75
|
+
stack?: string | undefined;
|
|
76
|
+
cause?: unknown;
|
|
77
|
+
};
|
|
78
|
+
help?: string | undefined;
|
|
79
|
+
status?: number | undefined;
|
|
80
|
+
message?: string | undefined;
|
|
81
|
+
captureStackTrace(targetObject: object, constructorOpt?: Function | undefined): void;
|
|
82
|
+
prepareStackTrace?: ((err: Error, stackTraces: NodeJS.CallSite[]) => any) | undefined;
|
|
83
|
+
stackTraceLimit: number;
|
|
84
|
+
};
|
|
@@ -0,0 +1,38 @@
|
|
|
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
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Route } from './router/route.js';
|
|
2
|
+
import { BriskRoute } from './router/brisk.js';
|
|
3
|
+
import { RouteGroup } from './router/group.js';
|
|
4
|
+
import type { RouteJSON } from './types/route.js';
|
|
5
|
+
import { RouteResource } from './router/resource.js';
|
|
6
|
+
/**
|
|
7
|
+
* Makes input string consistent by having only the starting
|
|
8
|
+
* slash
|
|
9
|
+
*/
|
|
10
|
+
export declare function dropSlash(input: string): string;
|
|
11
|
+
/**
|
|
12
|
+
* Returns a flat list of routes from the route groups and resources
|
|
13
|
+
*/
|
|
14
|
+
export declare function toRoutesJSON(routes: (RouteGroup | Route | RouteResource | BriskRoute)[]): RouteJSON[];
|
|
15
|
+
/**
|
|
16
|
+
* Helper to know if the remote address should
|
|
17
|
+
* be trusted.
|
|
18
|
+
*/
|
|
19
|
+
export declare function trustProxy(remoteAddress: string, proxyFn: (addr: string, distance: number) => boolean): boolean;
|
|
20
|
+
/**
|
|
21
|
+
* Parses a range expression to an object filled with the range
|
|
22
|
+
*/
|
|
23
|
+
export declare function parseRange<T>(range: string, value: T): Record<number, T>;
|
|
@@ -0,0 +1,105 @@
|
|
|
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 Cache from 'tmp-cache';
|
|
10
|
+
import { InvalidArgumentsException } from '@poppinss/utils';
|
|
11
|
+
import { BriskRoute } from './router/brisk.js';
|
|
12
|
+
import { RouteGroup } from './router/group.js';
|
|
13
|
+
import { RouteResource } from './router/resource.js';
|
|
14
|
+
const proxyCache = new Cache({ max: 200 });
|
|
15
|
+
/**
|
|
16
|
+
* Makes input string consistent by having only the starting
|
|
17
|
+
* slash
|
|
18
|
+
*/
|
|
19
|
+
export function dropSlash(input) {
|
|
20
|
+
if (input === '/') {
|
|
21
|
+
return '/';
|
|
22
|
+
}
|
|
23
|
+
return `/${input.replace(/^\//, '').replace(/\/$/, '')}`;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Returns a flat list of routes from the route groups and resources
|
|
27
|
+
*/
|
|
28
|
+
export function toRoutesJSON(routes) {
|
|
29
|
+
return routes.reduce((list, route) => {
|
|
30
|
+
if (route instanceof RouteGroup) {
|
|
31
|
+
list = list.concat(toRoutesJSON(route.routes));
|
|
32
|
+
return list;
|
|
33
|
+
}
|
|
34
|
+
if (route instanceof RouteResource) {
|
|
35
|
+
list = list.concat(toRoutesJSON(route.routes));
|
|
36
|
+
return list;
|
|
37
|
+
}
|
|
38
|
+
if (route instanceof BriskRoute) {
|
|
39
|
+
if (route.route && !route.route.isDeleted()) {
|
|
40
|
+
list.push(route.route.toJSON());
|
|
41
|
+
}
|
|
42
|
+
return list;
|
|
43
|
+
}
|
|
44
|
+
if (!route.isDeleted()) {
|
|
45
|
+
list.push(route.toJSON());
|
|
46
|
+
}
|
|
47
|
+
return list;
|
|
48
|
+
}, []);
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Helper to know if the remote address should
|
|
52
|
+
* be trusted.
|
|
53
|
+
*/
|
|
54
|
+
export function trustProxy(remoteAddress, proxyFn) {
|
|
55
|
+
if (proxyCache.has(remoteAddress)) {
|
|
56
|
+
return proxyCache.get(remoteAddress);
|
|
57
|
+
}
|
|
58
|
+
const result = proxyFn(remoteAddress, 0);
|
|
59
|
+
proxyCache.set(remoteAddress, result);
|
|
60
|
+
return result;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Parses a range expression to an object filled with the range
|
|
64
|
+
*/
|
|
65
|
+
export function parseRange(range, value) {
|
|
66
|
+
const parts = range.split('..');
|
|
67
|
+
const min = Number(parts[0]);
|
|
68
|
+
const max = Number(parts[1]);
|
|
69
|
+
/**
|
|
70
|
+
* The ending status code does not exists
|
|
71
|
+
*/
|
|
72
|
+
if (parts.length === 1 && !Number.isNaN(min)) {
|
|
73
|
+
return {
|
|
74
|
+
[min]: value,
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* The starting status code is not a number
|
|
79
|
+
*/
|
|
80
|
+
if (Number.isNaN(min) || Number.isNaN(max)) {
|
|
81
|
+
return {};
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Min and max are same
|
|
85
|
+
*/
|
|
86
|
+
if (min === max) {
|
|
87
|
+
return {
|
|
88
|
+
[min]: value,
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Max cannot be smaller than min
|
|
93
|
+
*/
|
|
94
|
+
if (max < min) {
|
|
95
|
+
throw new InvalidArgumentsException(`Invalid range "${range}"`);
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Loop over the range and create a collection
|
|
99
|
+
* of status codes
|
|
100
|
+
*/
|
|
101
|
+
return [...Array(max - min + 1).keys()].reduce((result, step) => {
|
|
102
|
+
result[min + step] = value;
|
|
103
|
+
return result;
|
|
104
|
+
}, {});
|
|
105
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/// <reference types="node" resolution-mode="require"/>
|
|
2
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
3
|
+
import type { HttpContext } from './main.js';
|
|
4
|
+
/**
|
|
5
|
+
* Async local storage for HTTP context
|
|
6
|
+
*/
|
|
7
|
+
export declare const asyncLocalStorage: {
|
|
8
|
+
isEnabled: boolean;
|
|
9
|
+
storage: null | AsyncLocalStorage<HttpContext>;
|
|
10
|
+
create(): AsyncLocalStorage<HttpContext>;
|
|
11
|
+
destroy(): void;
|
|
12
|
+
};
|
|
@@ -0,0 +1,39 @@
|
|
|
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 { AsyncLocalStorage } from 'node:async_hooks';
|
|
10
|
+
/**
|
|
11
|
+
* Async local storage for HTTP context
|
|
12
|
+
*/
|
|
13
|
+
export const asyncLocalStorage = {
|
|
14
|
+
/**
|
|
15
|
+
* Check if the async local storage for the HTTP
|
|
16
|
+
* context is enabled or not
|
|
17
|
+
*/
|
|
18
|
+
isEnabled: false,
|
|
19
|
+
/**
|
|
20
|
+
* HTTP context storage instance for the current scope
|
|
21
|
+
*/
|
|
22
|
+
storage: null,
|
|
23
|
+
/**
|
|
24
|
+
* Create the storage instance. This method must be called only
|
|
25
|
+
* once.
|
|
26
|
+
*/
|
|
27
|
+
create() {
|
|
28
|
+
this.isEnabled = true;
|
|
29
|
+
this.storage = new AsyncLocalStorage();
|
|
30
|
+
return this.storage;
|
|
31
|
+
},
|
|
32
|
+
/**
|
|
33
|
+
* Destroy the create storage instance
|
|
34
|
+
*/
|
|
35
|
+
destroy() {
|
|
36
|
+
this.isEnabled = false;
|
|
37
|
+
this.storage = null;
|
|
38
|
+
},
|
|
39
|
+
};
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import Macroable from '@poppinss/macroable';
|
|
2
|
+
import type { Logger } from '@adonisjs/logger';
|
|
3
|
+
import { ContainerResolver } from '@adonisjs/fold';
|
|
4
|
+
import type { Request } from '../request.js';
|
|
5
|
+
import type { Response } from '../response.js';
|
|
6
|
+
import type { StoreRouteNode } from '../types/route.js';
|
|
7
|
+
/**
|
|
8
|
+
* Http context encapsulates properties for a given HTTP request. The
|
|
9
|
+
* context class can be extended using macros and getters.
|
|
10
|
+
*/
|
|
11
|
+
export declare class HttpContext extends Macroable {
|
|
12
|
+
request: Request;
|
|
13
|
+
response: Response;
|
|
14
|
+
logger: Logger;
|
|
15
|
+
containerResolver: ContainerResolver<any>;
|
|
16
|
+
/**
|
|
17
|
+
* Find if async localstorage is enabled for HTTP requests
|
|
18
|
+
* or not
|
|
19
|
+
*/
|
|
20
|
+
static get usingAsyncLocalStorage(): boolean;
|
|
21
|
+
/**
|
|
22
|
+
* Get access to the HTTP context. Available only when
|
|
23
|
+
* "usingAsyncLocalStorage" is true
|
|
24
|
+
*/
|
|
25
|
+
static get(): HttpContext | null;
|
|
26
|
+
/**
|
|
27
|
+
* Get the HttpContext instance or raise an exception if not
|
|
28
|
+
* available
|
|
29
|
+
*/
|
|
30
|
+
static getOrFail(): HttpContext;
|
|
31
|
+
/**
|
|
32
|
+
* Run a method that doesn't have access to HTTP context from
|
|
33
|
+
* the async local storage.
|
|
34
|
+
*/
|
|
35
|
+
static runOutsideContext<T>(callback: (...args: any[]) => T, ...args: any[]): T;
|
|
36
|
+
/**
|
|
37
|
+
* Reference to the current route. Not available inside
|
|
38
|
+
* server middleware
|
|
39
|
+
*/
|
|
40
|
+
route?: StoreRouteNode;
|
|
41
|
+
/**
|
|
42
|
+
* A unique key for the current route
|
|
43
|
+
*/
|
|
44
|
+
routeKey?: string;
|
|
45
|
+
/**
|
|
46
|
+
* Route params
|
|
47
|
+
*/
|
|
48
|
+
params: Record<string, any>;
|
|
49
|
+
/**
|
|
50
|
+
* Route subdomains
|
|
51
|
+
*/
|
|
52
|
+
subdomains: Record<string, any>;
|
|
53
|
+
constructor(request: Request, response: Response, logger: Logger, containerResolver: ContainerResolver<any>);
|
|
54
|
+
/**
|
|
55
|
+
* A helper to see top level properties on the context object
|
|
56
|
+
*/
|
|
57
|
+
inspect(): string;
|
|
58
|
+
}
|