@adonisjs/http-server 7.0.0-1 → 7.0.0-3

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.
Files changed (65) hide show
  1. package/build/chunk-NC6OWANS.js +4437 -0
  2. package/build/chunk-NC6OWANS.js.map +1 -0
  3. package/build/factories/http_server.d.ts +1 -0
  4. package/build/factories/main.js +332 -14
  5. package/build/factories/main.js.map +1 -0
  6. package/build/index.js +309 -22
  7. package/build/index.js.map +1 -0
  8. package/build/src/define_middleware.d.ts +2 -1
  9. package/build/src/router/lookup_store/main.d.ts +1 -3
  10. package/build/src/router/lookup_store/route_finder.d.ts +5 -1
  11. package/build/src/router/main.d.ts +5 -4
  12. package/build/src/router/resource.d.ts +19 -7
  13. package/build/src/types/main.js +1 -15
  14. package/build/src/types/main.js.map +1 -0
  15. package/build/src/types/middleware.d.ts +3 -1
  16. package/package.json +60 -59
  17. package/build/factories/http_context.js +0 -51
  18. package/build/factories/http_server.js +0 -26
  19. package/build/factories/qs_parser_factory.js +0 -44
  20. package/build/factories/request.js +0 -73
  21. package/build/factories/response.js +0 -77
  22. package/build/factories/router.js +0 -45
  23. package/build/factories/server_factory.js +0 -65
  24. package/build/src/cookies/client.js +0 -84
  25. package/build/src/cookies/drivers/encrypted.js +0 -36
  26. package/build/src/cookies/drivers/plain.js +0 -33
  27. package/build/src/cookies/drivers/signed.js +0 -36
  28. package/build/src/cookies/parser.js +0 -167
  29. package/build/src/cookies/serializer.js +0 -79
  30. package/build/src/debug.js +0 -10
  31. package/build/src/define_config.js +0 -68
  32. package/build/src/define_middleware.js +0 -35
  33. package/build/src/exception_handler.js +0 -306
  34. package/build/src/exceptions.js +0 -38
  35. package/build/src/helpers.js +0 -105
  36. package/build/src/http_context/local_storage.js +0 -39
  37. package/build/src/http_context/main.js +0 -105
  38. package/build/src/qs.js +0 -25
  39. package/build/src/redirect.js +0 -140
  40. package/build/src/request.js +0 -865
  41. package/build/src/response.js +0 -1208
  42. package/build/src/router/brisk.js +0 -85
  43. package/build/src/router/executor.js +0 -30
  44. package/build/src/router/factories/use_return_value.js +0 -22
  45. package/build/src/router/group.js +0 -207
  46. package/build/src/router/lookup_store/main.js +0 -86
  47. package/build/src/router/lookup_store/route_finder.js +0 -49
  48. package/build/src/router/lookup_store/url_builder.js +0 -209
  49. package/build/src/router/main.js +0 -316
  50. package/build/src/router/matchers.js +0 -36
  51. package/build/src/router/parser.js +0 -17
  52. package/build/src/router/resource.js +0 -216
  53. package/build/src/router/route.js +0 -293
  54. package/build/src/router/store.js +0 -195
  55. package/build/src/server/factories/final_handler.js +0 -30
  56. package/build/src/server/factories/middleware_handler.js +0 -16
  57. package/build/src/server/factories/write_response.js +0 -24
  58. package/build/src/server/main.js +0 -292
  59. package/build/src/types/base.js +0 -9
  60. package/build/src/types/middleware.js +0 -9
  61. package/build/src/types/qs.js +0 -9
  62. package/build/src/types/request.js +0 -9
  63. package/build/src/types/response.js +0 -9
  64. package/build/src/types/route.js +0 -9
  65. package/build/src/types/server.js +0 -9
@@ -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
- }
@@ -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
- };
@@ -1,105 +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 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
- }
@@ -1,39 +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 { 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
- };
@@ -1,105 +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 { inspect } from 'node:util';
10
- import Macroable from '@poppinss/macroable';
11
- import { RuntimeException } from '@poppinss/utils';
12
- import { asyncLocalStorage } from './local_storage.js';
13
- /**
14
- * Http context encapsulates properties for a given HTTP request. The
15
- * context class can be extended using macros and getters.
16
- */
17
- export class HttpContext extends Macroable {
18
- request;
19
- response;
20
- logger;
21
- containerResolver;
22
- /**
23
- * Find if async localstorage is enabled for HTTP requests
24
- * or not
25
- */
26
- static get usingAsyncLocalStorage() {
27
- return asyncLocalStorage.isEnabled;
28
- }
29
- /**
30
- * Get access to the HTTP context. Available only when
31
- * "usingAsyncLocalStorage" is true
32
- */
33
- static get() {
34
- if (!this.usingAsyncLocalStorage || !asyncLocalStorage.storage) {
35
- return null;
36
- }
37
- return asyncLocalStorage.storage.getStore() || null;
38
- }
39
- /**
40
- * Get the HttpContext instance or raise an exception if not
41
- * available
42
- */
43
- static getOrFail() {
44
- /**
45
- * Localstorage is not enabled
46
- */
47
- if (!this.usingAsyncLocalStorage || !asyncLocalStorage.storage) {
48
- throw new RuntimeException('HTTP context is not available. Enable "useAsyncLocalStorage" inside "config/app.ts" file');
49
- }
50
- const store = this.get();
51
- if (!store) {
52
- throw new RuntimeException('Http context is not available outside of an HTTP request');
53
- }
54
- return store;
55
- }
56
- /**
57
- * Run a method that doesn't have access to HTTP context from
58
- * the async local storage.
59
- */
60
- static runOutsideContext(callback, ...args) {
61
- if (!asyncLocalStorage.storage) {
62
- return callback(...args);
63
- }
64
- return asyncLocalStorage.storage.exit(callback, ...args);
65
- }
66
- /**
67
- * Reference to the current route. Not available inside
68
- * server middleware
69
- */
70
- route;
71
- /**
72
- * A unique key for the current route
73
- */
74
- routeKey;
75
- /**
76
- * Route params
77
- */
78
- params = {};
79
- /**
80
- * Route subdomains
81
- */
82
- subdomains = {};
83
- constructor(request, response, logger, containerResolver) {
84
- super();
85
- this.request = request;
86
- this.response = response;
87
- this.logger = logger;
88
- this.containerResolver = containerResolver;
89
- /*
90
- * Creating the circular reference. We do this, since request and response
91
- * are meant to be extended and at times people would want to access
92
- * other ctx properties like `logger`, `profiler` inside those
93
- * extended methods.
94
- */
95
- this.request.ctx = this;
96
- this.response.ctx = this;
97
- }
98
- /**
99
- * A helper to see top level properties on the context object
100
- */
101
- /* c8 ignore next 3 */
102
- inspect() {
103
- return inspect(this, false, 1, true);
104
- }
105
- }
package/build/src/qs.js DELETED
@@ -1,25 +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 { parse, stringify } from 'qs';
10
- /**
11
- * Query string parser used to parse and stringify query
12
- * strings.
13
- */
14
- export class Qs {
15
- #config;
16
- constructor(config) {
17
- this.#config = config;
18
- }
19
- parse(value) {
20
- return parse(value, this.#config.parse);
21
- }
22
- stringify(value) {
23
- return stringify(value, this.#config.stringify);
24
- }
25
- }