@adonisjs/http-server 6.4.3-0 → 6.5.0-0
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/LICENSE.md +1 -1
- package/build/{test_factories → factories}/http_context.d.ts +0 -0
- package/build/{test_factories → factories}/http_context.js +0 -0
- package/build/factories/main.d.ts +6 -0
- package/build/factories/main.js +6 -0
- package/build/{test_factories → factories}/qs_parser_factory.d.ts +0 -0
- package/build/{test_factories → factories}/qs_parser_factory.js +0 -0
- package/build/{test_factories → factories}/request.d.ts +0 -0
- package/build/{test_factories → factories}/request.js +1 -1
- package/build/{test_factories → factories}/response.d.ts +0 -0
- package/build/{test_factories → factories}/response.js +0 -0
- package/build/{test_factories → factories}/router.d.ts +0 -0
- package/build/{test_factories → factories}/router.js +0 -0
- package/build/{test_factories → factories}/server_factory.d.ts +0 -0
- package/build/{test_factories → factories}/server_factory.js +0 -0
- package/build/index.d.ts +2 -1
- package/build/index.js +2 -1
- package/build/src/exception_handler.d.ts +28 -0
- package/build/src/exception_handler.js +148 -0
- package/build/src/{exceptions/main.d.ts → exceptions.d.ts} +1 -1
- package/build/src/{exceptions/main.js → exceptions.js} +0 -0
- package/build/src/helpers.d.ts +1 -0
- package/build/src/helpers.js +21 -0
- package/build/src/response.js +1 -1
- package/build/src/router/lookup_store/route_finder.js +1 -1
- package/build/src/server/factories/final_handler.js +1 -1
- package/build/src/server/main.js +5 -1
- package/build/src/types/server.d.ts +14 -0
- package/package.json +8 -7
package/LICENSE.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# The MIT License
|
|
2
2
|
|
|
3
|
-
Copyright
|
|
3
|
+
Copyright (c) 2023 AdonisJS Framework
|
|
4
4
|
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
6
|
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { HttpContextFactory } from './http_context.js';
|
|
2
|
+
export { QsParserFactory } from './qs_parser_factory.js';
|
|
3
|
+
export { RequestFactory } from './request.js';
|
|
4
|
+
export { ResponseFactory } from './response.js';
|
|
5
|
+
export { RouterFactory } from './router.js';
|
|
6
|
+
export { ServerFactory } from './server_factory.js';
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { HttpContextFactory } from './http_context.js';
|
|
2
|
+
export { QsParserFactory } from './qs_parser_factory.js';
|
|
3
|
+
export { RequestFactory } from './request.js';
|
|
4
|
+
export { ResponseFactory } from './response.js';
|
|
5
|
+
export { RouterFactory } from './router.js';
|
|
6
|
+
export { ServerFactory } from './server_factory.js';
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
package/build/index.d.ts
CHANGED
|
@@ -9,4 +9,5 @@ export { defineConfig } from './src/define_config.js';
|
|
|
9
9
|
export { RouteResource } from './src/router/resource.js';
|
|
10
10
|
export { BriskRoute } from './src/router/brisk.js';
|
|
11
11
|
export { HttpContext } from './src/http_context/main.js';
|
|
12
|
-
export * as errors from './src/exceptions
|
|
12
|
+
export * as errors from './src/exceptions.js';
|
|
13
|
+
export { HttpExceptionHandler } from './src/exception_handler.js';
|
package/build/index.js
CHANGED
|
@@ -9,4 +9,5 @@ export { defineConfig } from './src/define_config.js';
|
|
|
9
9
|
export { RouteResource } from './src/router/resource.js';
|
|
10
10
|
export { BriskRoute } from './src/router/brisk.js';
|
|
11
11
|
export { HttpContext } from './src/http_context/main.js';
|
|
12
|
-
export * as errors from './src/exceptions
|
|
12
|
+
export * as errors from './src/exceptions.js';
|
|
13
|
+
export { HttpExceptionHandler } from './src/exception_handler.js';
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { Logger } from '@adonisjs/logger';
|
|
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
|
+
export declare class HttpExceptionHandler {
|
|
6
|
+
#private;
|
|
7
|
+
protected logger: Logger;
|
|
8
|
+
protected debug: boolean;
|
|
9
|
+
protected renderStatusPages: boolean;
|
|
10
|
+
protected statusPages: Record<StatusPageRange, StatusPageRenderer>;
|
|
11
|
+
protected renderers: {
|
|
12
|
+
html: (error: HttpError, ctx: HttpContext) => Promise<void>;
|
|
13
|
+
json: (error: HttpError, ctx: HttpContext) => Promise<void>;
|
|
14
|
+
json_api: (error: HttpError, ctx: HttpContext) => Promise<void>;
|
|
15
|
+
};
|
|
16
|
+
protected reportErrors: boolean;
|
|
17
|
+
protected ignoreExceptions: any[];
|
|
18
|
+
protected ignoreStatuses: number[];
|
|
19
|
+
protected ignoreCodes: string[];
|
|
20
|
+
constructor(logger: Logger);
|
|
21
|
+
protected context(ctx: HttpContext): any;
|
|
22
|
+
protected getResponseFormat(ctx: HttpContext): 'html' | 'json' | 'json_api';
|
|
23
|
+
protected getErrorLogLevel(error: HttpError): Level;
|
|
24
|
+
protected isDebuggingEnabled(_: HttpContext): boolean;
|
|
25
|
+
protected shouldReport(error: HttpError): boolean;
|
|
26
|
+
report(error: unknown, ctx: HttpContext): Promise<void>;
|
|
27
|
+
handle(error: unknown, ctx: HttpContext): Promise<any>;
|
|
28
|
+
}
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import is from '@sindresorhus/is';
|
|
2
|
+
import { parseRange } from './helpers.js';
|
|
3
|
+
import * as errors from './exceptions.js';
|
|
4
|
+
export class HttpExceptionHandler {
|
|
5
|
+
logger;
|
|
6
|
+
debug = process.env.NODE_ENV !== 'production';
|
|
7
|
+
renderStatusPages = process.env.NODE_ENV === 'production';
|
|
8
|
+
statusPages = {};
|
|
9
|
+
#expandedStatusPages;
|
|
10
|
+
renderers = {
|
|
11
|
+
html: async (error, ctx) => {
|
|
12
|
+
if (this.isDebuggingEnabled(ctx)) {
|
|
13
|
+
const { default: Youch } = await import('youch');
|
|
14
|
+
const html = await new Youch(error, ctx.request.request).toHTML();
|
|
15
|
+
ctx.response.status(error.status).send(html);
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
ctx.response.status(error.status).send(`<p> ${error.message} </p>`);
|
|
19
|
+
},
|
|
20
|
+
json: async (error, ctx) => {
|
|
21
|
+
if (this.isDebuggingEnabled(ctx)) {
|
|
22
|
+
const { default: Youch } = await import('youch');
|
|
23
|
+
const json = await new Youch(error, ctx.request.request).toJSON();
|
|
24
|
+
ctx.response.status(error.status).send(json.error);
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
ctx.response.status(error.status).send({ message: error.message });
|
|
28
|
+
},
|
|
29
|
+
json_api: async (error, ctx) => {
|
|
30
|
+
if (this.isDebuggingEnabled(ctx)) {
|
|
31
|
+
const { default: Youch } = await import('youch');
|
|
32
|
+
const json = await new Youch(error, ctx.request.request).toJSON();
|
|
33
|
+
ctx.response.status(error.status).send(json.error);
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
ctx.response.status(error.status).send({
|
|
37
|
+
errors: [
|
|
38
|
+
{
|
|
39
|
+
title: error.message,
|
|
40
|
+
code: error.code,
|
|
41
|
+
status: error.status,
|
|
42
|
+
},
|
|
43
|
+
],
|
|
44
|
+
});
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
reportErrors = true;
|
|
48
|
+
ignoreExceptions = [
|
|
49
|
+
errors.E_HTTP_EXCEPTION,
|
|
50
|
+
errors.E_ROUTE_NOT_FOUND,
|
|
51
|
+
errors.E_CANNOT_LOOKUP_ROUTE,
|
|
52
|
+
errors.E_HTTP_REQUEST_ABORTED,
|
|
53
|
+
];
|
|
54
|
+
ignoreStatuses = [400, 422, 401];
|
|
55
|
+
ignoreCodes = [];
|
|
56
|
+
constructor(logger) {
|
|
57
|
+
this.logger = logger;
|
|
58
|
+
}
|
|
59
|
+
#expandStatusPages() {
|
|
60
|
+
if (!this.#expandedStatusPages) {
|
|
61
|
+
this.#expandedStatusPages = Object.keys(this.statusPages).reduce((result, range) => {
|
|
62
|
+
const renderer = this.statusPages[range];
|
|
63
|
+
result = Object.assign(result, parseRange(range, renderer));
|
|
64
|
+
return result;
|
|
65
|
+
}, {});
|
|
66
|
+
}
|
|
67
|
+
return this.#expandedStatusPages;
|
|
68
|
+
}
|
|
69
|
+
#toHttpError(error) {
|
|
70
|
+
const httpError = is.object(error) ? error : new Error(String(error));
|
|
71
|
+
httpError.message = httpError.message || 'Internal server error';
|
|
72
|
+
httpError.status = httpError.status || 500;
|
|
73
|
+
return httpError;
|
|
74
|
+
}
|
|
75
|
+
context(ctx) {
|
|
76
|
+
const requestId = ctx.request.id();
|
|
77
|
+
return requestId
|
|
78
|
+
? {
|
|
79
|
+
'x-request-id': requestId,
|
|
80
|
+
}
|
|
81
|
+
: {};
|
|
82
|
+
}
|
|
83
|
+
getResponseFormat(ctx) {
|
|
84
|
+
switch (ctx.request.accepts(['html', 'application/vnd.api+json', 'json'])) {
|
|
85
|
+
case 'application/vnd.api+json':
|
|
86
|
+
return 'json_api';
|
|
87
|
+
case 'json':
|
|
88
|
+
return 'json';
|
|
89
|
+
case 'html':
|
|
90
|
+
default:
|
|
91
|
+
return 'html';
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
getErrorLogLevel(error) {
|
|
95
|
+
if (error.status >= 500) {
|
|
96
|
+
return 'error';
|
|
97
|
+
}
|
|
98
|
+
if (error.status >= 400) {
|
|
99
|
+
return 'warn';
|
|
100
|
+
}
|
|
101
|
+
return 'info';
|
|
102
|
+
}
|
|
103
|
+
isDebuggingEnabled(_) {
|
|
104
|
+
return this.debug;
|
|
105
|
+
}
|
|
106
|
+
shouldReport(error) {
|
|
107
|
+
if (this.reportErrors === false) {
|
|
108
|
+
return false;
|
|
109
|
+
}
|
|
110
|
+
if (this.ignoreStatuses.includes(error.status)) {
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
if (error.code && this.ignoreCodes.includes(error.code)) {
|
|
114
|
+
return false;
|
|
115
|
+
}
|
|
116
|
+
if (this.ignoreExceptions.find((exception) => error instanceof exception)) {
|
|
117
|
+
return false;
|
|
118
|
+
}
|
|
119
|
+
return true;
|
|
120
|
+
}
|
|
121
|
+
async report(error, ctx) {
|
|
122
|
+
const httpError = this.#toHttpError(error);
|
|
123
|
+
if (!this.shouldReport(httpError)) {
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
if (typeof httpError.report === 'function') {
|
|
127
|
+
httpError.report(httpError, ctx);
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
const level = this.getErrorLogLevel(httpError);
|
|
131
|
+
this.logger.log(level, {
|
|
132
|
+
...(level === 'error' || level === 'fatal' ? { err: httpError } : {}),
|
|
133
|
+
...this.context(ctx),
|
|
134
|
+
}, httpError.message);
|
|
135
|
+
}
|
|
136
|
+
async handle(error, ctx) {
|
|
137
|
+
const httpError = this.#toHttpError(error);
|
|
138
|
+
if (typeof httpError.handle === 'function') {
|
|
139
|
+
return httpError.handle(httpError, ctx);
|
|
140
|
+
}
|
|
141
|
+
const statusPages = this.#expandStatusPages();
|
|
142
|
+
if (this.renderStatusPages && statusPages[httpError.status]) {
|
|
143
|
+
return statusPages[httpError.status](httpError, ctx);
|
|
144
|
+
}
|
|
145
|
+
const responseFormat = this.getResponseFormat(ctx);
|
|
146
|
+
return this.renderers[responseFormat](httpError, ctx);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/// <reference types="node" resolution-mode="require"/>
|
|
2
2
|
import { Exception } from '@poppinss/utils';
|
|
3
|
-
import type { HttpContext } from '
|
|
3
|
+
import type { HttpContext } from './http_context/main.js';
|
|
4
4
|
export declare const E_ROUTE_NOT_FOUND: new (args: [method: string, url: string], options?: ErrorOptions | undefined) => Exception;
|
|
5
5
|
export declare const E_CANNOT_LOOKUP_ROUTE: new (args: [routeIdentifier: string], options?: ErrorOptions | undefined) => Exception;
|
|
6
6
|
export declare const E_HTTP_EXCEPTION: {
|
|
File without changes
|
package/build/src/helpers.d.ts
CHANGED
|
@@ -6,3 +6,4 @@ import { RouteResource } from './router/resource.js';
|
|
|
6
6
|
export declare function dropSlash(input: string): string;
|
|
7
7
|
export declare function toRoutesJSON(routes: (RouteGroup | Route | RouteResource | BriskRoute)[]): RouteJSON[];
|
|
8
8
|
export declare function trustProxy(remoteAddress: string, proxyFn: (addr: string, distance: number) => boolean): boolean;
|
|
9
|
+
export declare function parseRange<T>(range: string, value: T): Record<number, T>;
|
package/build/src/helpers.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import Cache from 'tmp-cache';
|
|
2
|
+
import { InvalidArgumentsException } from '@poppinss/utils';
|
|
2
3
|
import { BriskRoute } from './router/brisk.js';
|
|
3
4
|
import { RouteGroup } from './router/group.js';
|
|
4
5
|
import { RouteResource } from './router/resource.js';
|
|
@@ -39,3 +40,23 @@ export function trustProxy(remoteAddress, proxyFn) {
|
|
|
39
40
|
proxyCache.set(remoteAddress, result);
|
|
40
41
|
return result;
|
|
41
42
|
}
|
|
43
|
+
export function parseRange(range, value) {
|
|
44
|
+
const parts = range.split('..');
|
|
45
|
+
const min = Number(parts[0]);
|
|
46
|
+
const max = Number(parts[1]);
|
|
47
|
+
if (Number.isNaN(min) || Number.isNaN(max)) {
|
|
48
|
+
return {};
|
|
49
|
+
}
|
|
50
|
+
if (min === max) {
|
|
51
|
+
return {
|
|
52
|
+
[min]: value,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
if (max < min) {
|
|
56
|
+
throw new InvalidArgumentsException(`Invalid range "${range}"`);
|
|
57
|
+
}
|
|
58
|
+
return [...Array(max - min + 1).keys()].reduce((result, step) => {
|
|
59
|
+
result[min + step] = value;
|
|
60
|
+
return result;
|
|
61
|
+
}, {});
|
|
62
|
+
}
|
package/build/src/response.js
CHANGED
|
@@ -13,7 +13,7 @@ import { RuntimeException } from '@poppinss/utils';
|
|
|
13
13
|
import contentDisposition from 'content-disposition';
|
|
14
14
|
import { Redirect } from './redirect.js';
|
|
15
15
|
import { CookieSerializer } from './cookies/serializer.js';
|
|
16
|
-
import { E_HTTP_REQUEST_ABORTED } from './exceptions
|
|
16
|
+
import { E_HTTP_REQUEST_ABORTED } from './exceptions.js';
|
|
17
17
|
const statFn = promisify(stat);
|
|
18
18
|
const CACHEABLE_HTTP_METHODS = ['GET', 'HEAD'];
|
|
19
19
|
export class Response extends Macroable {
|
package/build/src/server/main.js
CHANGED
|
@@ -13,6 +13,7 @@ import { asyncLocalStorage } from '../http_context/local_storage.js';
|
|
|
13
13
|
import { middlewareHandler } from './factories/middleware_handler.js';
|
|
14
14
|
export class Server {
|
|
15
15
|
#defaultErrorHandler = {
|
|
16
|
+
report() { },
|
|
16
17
|
handle(error, ctx) {
|
|
17
18
|
ctx.response.status(error.status || 500).send(error.message || 'Internal server error');
|
|
18
19
|
},
|
|
@@ -60,7 +61,10 @@ export class Server {
|
|
|
60
61
|
}
|
|
61
62
|
#handleRequest(ctx, resolver) {
|
|
62
63
|
return this.#serverMiddlewareStack.runner()
|
|
63
|
-
.errorHandler((error) =>
|
|
64
|
+
.errorHandler((error) => {
|
|
65
|
+
this.#resolvedErrorHandler.report(error, ctx);
|
|
66
|
+
return this.#resolvedErrorHandler.handle(error, ctx);
|
|
67
|
+
})
|
|
64
68
|
.finalHandler(finalHandler(this.#router, resolver, ctx))
|
|
65
69
|
.run(middlewareHandler(resolver, ctx))
|
|
66
70
|
.catch((error) => {
|
|
@@ -3,11 +3,25 @@ import type { QSParserConfig } from './qs.js';
|
|
|
3
3
|
import type { RequestConfig } from './request.js';
|
|
4
4
|
import type { ResponseConfig } from './response.js';
|
|
5
5
|
import type { HttpContext } from '../http_context/main.js';
|
|
6
|
+
export type HttpError = {
|
|
7
|
+
message: string;
|
|
8
|
+
status: number;
|
|
9
|
+
code?: string;
|
|
10
|
+
stack?: string;
|
|
11
|
+
cause?: any;
|
|
12
|
+
messages?: any;
|
|
13
|
+
errors?: any;
|
|
14
|
+
handle?: (...args: any[]) => any;
|
|
15
|
+
report?: (...args: any[]) => any;
|
|
16
|
+
};
|
|
17
|
+
export type StatusPageRange = `${number}..${number}`;
|
|
18
|
+
export type StatusPageRenderer = (error: HttpError, ctx: HttpContext) => any | Promise<any>;
|
|
6
19
|
export type HttpRequestFinishedPayload = {
|
|
7
20
|
ctx: HttpContext;
|
|
8
21
|
duration: [number, number];
|
|
9
22
|
};
|
|
10
23
|
export type ServerErrorHandler = {
|
|
24
|
+
report: (error: any, ctx: HttpContext) => any;
|
|
11
25
|
handle: (error: any, ctx: HttpContext) => any;
|
|
12
26
|
};
|
|
13
27
|
export type ErrorHandlerAsAClass = Constructor<ServerErrorHandler>;
|
package/package.json
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adonisjs/http-server",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.5.0-0",
|
|
4
4
|
"description": "AdonisJS HTTP server with support packed with Routing and Cookies",
|
|
5
5
|
"main": "build/index.js",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"files": [
|
|
8
8
|
"build/src",
|
|
9
|
-
"build/
|
|
9
|
+
"build/factories",
|
|
10
10
|
"build/index.d.ts",
|
|
11
11
|
"build/index.js"
|
|
12
12
|
],
|
|
13
13
|
"exports": {
|
|
14
14
|
".": "./build/index.js",
|
|
15
15
|
"./types": "./build/src/types/main.js",
|
|
16
|
-
"./
|
|
16
|
+
"./factories": "./build/factories/main.js"
|
|
17
17
|
},
|
|
18
18
|
"scripts": {
|
|
19
19
|
"pretest": "npm run lint",
|
|
@@ -44,7 +44,7 @@
|
|
|
44
44
|
"@adonisjs/encryption": "^5.1.0-0",
|
|
45
45
|
"@adonisjs/events": "^8.4.3-0",
|
|
46
46
|
"@adonisjs/fold": "^9.9.0-0",
|
|
47
|
-
"@adonisjs/logger": "^5.
|
|
47
|
+
"@adonisjs/logger": "^5.3.0-0",
|
|
48
48
|
"@commitlint/cli": "^17.4.2",
|
|
49
49
|
"@commitlint/config-conventional": "^17.4.2",
|
|
50
50
|
"@fastify/middie": "^8.1.0",
|
|
@@ -113,14 +113,15 @@
|
|
|
113
113
|
"qs": "^6.11.0",
|
|
114
114
|
"tmp-cache": "^1.1.0",
|
|
115
115
|
"type-is": "^1.6.18",
|
|
116
|
-
"vary": "^1.1.2"
|
|
116
|
+
"vary": "^1.1.2",
|
|
117
|
+
"youch": "^3.2.2"
|
|
117
118
|
},
|
|
118
119
|
"peerDependencies": {
|
|
119
120
|
"@adonisjs/application": "^6.8.0-0",
|
|
120
121
|
"@adonisjs/encryption": "^5.1.0-0",
|
|
121
122
|
"@adonisjs/events": "^8.4.3-0",
|
|
122
123
|
"@adonisjs/fold": "^9.9.0-0",
|
|
123
|
-
"@adonisjs/logger": "^5.
|
|
124
|
+
"@adonisjs/logger": "^5.3.0-0"
|
|
124
125
|
},
|
|
125
126
|
"repository": {
|
|
126
127
|
"type": "git",
|
|
@@ -183,7 +184,7 @@
|
|
|
183
184
|
],
|
|
184
185
|
"exclude": [
|
|
185
186
|
"tests/**",
|
|
186
|
-
"
|
|
187
|
+
"factories/**",
|
|
187
188
|
".yalc/**"
|
|
188
189
|
]
|
|
189
190
|
}
|