@adonisjs/http-server 7.0.0 → 7.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/README.md CHANGED
@@ -8,7 +8,7 @@
8
8
  Implementation of HTTP server used by AdonisJS. The package ships with a powerful **Router**, **Middleware pipeline**, helpers to create plain and signed URL for registered routes and much more.
9
9
 
10
10
  ## Official Documentation
11
- The documentation is available on the [AdonisJS website](https://docs.adonisjs.com/guides/context)
11
+ The documentation is available on the [AdonisJS website](https://docs.adonisjs.com/guides/http)
12
12
 
13
13
  ## Contributing
14
14
  One of the primary goals of AdonisJS is to have a vibrant community of users and contributors who believes in the principles of the framework.
package/build/index.js CHANGED
@@ -289,7 +289,13 @@ var ExceptionHandler = class extends Macroable {
289
289
  }
290
290
  const statusPages = this.#expandStatusPages();
291
291
  if (this.renderStatusPages && statusPages[httpError.status]) {
292
- return statusPages[httpError.status](httpError, ctx);
292
+ const statusPageResponse = await statusPages[httpError.status](httpError, ctx);
293
+ if (statusPageResponse !== void 0 && // Return value is explicitly defined
294
+ !ctx.response.hasLazyBody && // Lazy body is not set
295
+ statusPageResponse !== ctx.response) {
296
+ return ctx.response.safeStatus(httpError.status).send(statusPageResponse);
297
+ }
298
+ return statusPageResponse;
293
299
  }
294
300
  return this.renderError(httpError, ctx);
295
301
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/exception_handler.ts"],"sourcesContent":["/*\n * @adonisjs/http-server\n *\n * (c) AdonisJS\n *\n * For the full copyright and license information, please view the LICENSE\n * file that was distributed with this source code.\n */\n\nimport is from '@sindresorhus/is'\nimport Macroable from '@poppinss/macroable'\nimport type { Level } from '@adonisjs/logger/types'\n\nimport { parseRange } from './helpers.js'\nimport * as errors from './exceptions.js'\nimport type { HttpContext } from './http_context/main.js'\nimport type { HttpError, StatusPageRange, StatusPageRenderer } from './types/server.js'\n\n/**\n * The base HTTP exception handler one can inherit from to handle\n * HTTP exceptions.\n *\n * The HTTP exception handler has support for\n *\n * - Ability to render exceptions by calling the render method on the exception.\n * - Rendering status pages\n * - Pretty printing errors during development\n * - Transforming errors to JSON or HTML using content negotiation\n * - Reporting errors\n */\nexport class ExceptionHandler extends Macroable {\n /**\n * Computed from the status pages property\n */\n #expandedStatusPages?: Record<number, StatusPageRenderer>\n\n /**\n * Whether or not to render debug info. When set to true, the errors\n * will have the complete error stack.\n */\n protected debug: boolean = process.env.NODE_ENV !== 'production'\n\n /**\n * Whether or not to render status pages. When set to true, the unhandled\n * errors with matching status codes will be rendered using a status\n * page.\n */\n protected renderStatusPages: boolean = process.env.NODE_ENV === 'production'\n\n /**\n * A collection of error status code range and the view to render.\n */\n protected statusPages: Record<StatusPageRange, StatusPageRenderer> = {}\n\n /**\n * Enable/disable errors reporting\n */\n protected reportErrors: boolean = true\n\n /**\n * An array of exception classes to ignore when\n * reporting an error\n */\n protected ignoreExceptions: any[] = [\n errors.E_HTTP_EXCEPTION,\n errors.E_ROUTE_NOT_FOUND,\n errors.E_CANNOT_LOOKUP_ROUTE,\n errors.E_HTTP_REQUEST_ABORTED,\n ]\n\n /**\n * An array of HTTP status codes to ignore when reporting\n * an error\n */\n protected ignoreStatuses: number[] = [400, 422, 401]\n\n /**\n * An array of error codes to ignore when reporting\n * an error\n */\n protected ignoreCodes: string[] = []\n\n /**\n * Expands status pages\n */\n #expandStatusPages() {\n if (!this.#expandedStatusPages) {\n this.#expandedStatusPages = Object.keys(this.statusPages).reduce(\n (result, range) => {\n const renderer = this.statusPages[range as StatusPageRange]\n result = Object.assign(result, parseRange(range, renderer))\n return result\n },\n {} as Record<number, StatusPageRenderer>\n )\n }\n\n return this.#expandedStatusPages\n }\n\n /**\n * Forcefully tweaking the type of the error object to\n * have known properties.\n */\n #toHttpError(error: unknown): HttpError {\n const httpError: any = is.object(error) ? error : new Error(String(error))\n if (!httpError.message) {\n httpError.message = 'Internal server error'\n }\n if (!httpError.status) {\n httpError.status = 500\n }\n return httpError\n }\n\n /**\n * Error reporting context\n */\n protected context(ctx: HttpContext): any {\n const requestId = ctx.request.id()\n return requestId\n ? {\n 'x-request-id': requestId,\n }\n : {}\n }\n\n /**\n * Returns the log level for an error based upon the error\n * status code.\n */\n protected getErrorLogLevel(error: HttpError): Level {\n if (error.status >= 500) {\n return 'error'\n }\n\n if (error.status >= 400) {\n return 'warn'\n }\n\n return 'info'\n }\n\n /**\n * A boolean to control if errors should be rendered with\n * all the available debugging info.\n */\n protected isDebuggingEnabled(_: HttpContext): boolean {\n return this.debug\n }\n\n /**\n * Returns a boolean by checking if an error should be reported.\n */\n protected shouldReport(error: HttpError): boolean {\n if (this.reportErrors === false) {\n return false\n }\n\n if (this.ignoreStatuses.includes(error.status)) {\n return false\n }\n\n if (error.code && this.ignoreCodes.includes(error.code)) {\n return false\n }\n\n if (this.ignoreExceptions.find((exception) => error instanceof exception)) {\n return false\n }\n\n return true\n }\n\n /**\n * Renders an error to JSON response\n */\n async renderErrorAsJSON(error: HttpError, ctx: HttpContext) {\n if (this.isDebuggingEnabled(ctx)) {\n const { default: Youch } = await import('youch')\n const json = await new Youch(error, ctx.request.request).toJSON()\n ctx.response.status(error.status).send(json.error)\n return\n }\n\n ctx.response.status(error.status).send({ message: error.message })\n }\n\n /**\n * Renders an error to JSON API response\n */\n async renderErrorAsJSONAPI(error: HttpError, ctx: HttpContext) {\n if (this.isDebuggingEnabled(ctx)) {\n const { default: Youch } = await import('youch')\n const json = await new Youch(error, ctx.request.request).toJSON()\n ctx.response.status(error.status).send(json.error)\n return\n }\n\n ctx.response.status(error.status).send({\n errors: [\n {\n title: error.message,\n code: error.code,\n status: error.status,\n },\n ],\n })\n }\n\n /**\n * Renders an error to HTML response\n */\n async renderErrorAsHTML(error: HttpError, ctx: HttpContext) {\n if (this.isDebuggingEnabled(ctx)) {\n const { default: Youch } = await import('youch')\n const html = await new Youch(error, ctx.request.request).toHTML({\n cspNonce: 'nonce' in ctx.response ? ctx.response.nonce : undefined,\n })\n ctx.response.status(error.status).send(html)\n return\n }\n\n ctx.response.status(error.status).send(`<p> ${error.message} </p>`)\n }\n\n /**\n * Renders the validation error message to a JSON\n * response\n */\n async renderValidationErrorAsJSON(error: HttpError, ctx: HttpContext) {\n ctx.response.status(error.status).send({\n errors: error.messages,\n })\n }\n\n /**\n * Renders the validation error message as per JSON API\n * spec\n */\n async renderValidationErrorAsJSONAPI(error: HttpError, ctx: HttpContext) {\n ctx.response.status(error.status).send({\n errors: error.messages.map((message: any) => {\n return {\n title: message.message,\n code: message.rule,\n source: {\n pointer: message.field,\n },\n meta: message.meta,\n }\n }),\n })\n }\n\n /**\n * Renders the validation error as an HTML string\n */\n async renderValidationErrorAsHTML(error: HttpError, ctx: HttpContext) {\n ctx.response\n .status(error.status)\n .type('html')\n .send(\n error.messages\n .map((message: any) => {\n return `${message.field} - ${message.message}`\n })\n .join('<br />')\n )\n }\n\n /**\n * Renders the error to response\n */\n renderError(error: HttpError, ctx: HttpContext) {\n switch (ctx.request.accepts(['html', 'application/vnd.api+json', 'json'])) {\n case 'application/vnd.api+json':\n return this.renderErrorAsJSONAPI(error, ctx)\n case 'json':\n return this.renderErrorAsJSON(error, ctx)\n case 'html':\n default:\n return this.renderErrorAsHTML(error, ctx)\n }\n }\n\n /**\n * Renders the validation error to response\n */\n renderValidationError(error: HttpError, ctx: HttpContext) {\n switch (ctx.request.accepts(['html', 'application/vnd.api+json', 'json'])) {\n case 'application/vnd.api+json':\n return this.renderValidationErrorAsJSONAPI(error, ctx)\n case 'json':\n return this.renderValidationErrorAsJSON(error, ctx)\n case 'html':\n default:\n return this.renderValidationErrorAsHTML(error, ctx)\n }\n }\n\n /**\n * Reports an error during an HTTP request\n */\n async report(error: unknown, ctx: HttpContext) {\n const httpError = this.#toHttpError(error)\n if (!this.shouldReport(httpError)) {\n return\n }\n\n if (typeof httpError.report === 'function') {\n httpError.report(httpError, ctx)\n return\n }\n\n /**\n * Log the error using the logger\n */\n const level = this.getErrorLogLevel(httpError)\n ctx.logger.log(\n level,\n {\n ...(level === 'error' || level === 'fatal' ? { err: httpError } : {}),\n ...this.context(ctx),\n },\n httpError.message\n )\n }\n\n /**\n * Handles the error during the HTTP request.\n */\n async handle(error: unknown, ctx: HttpContext) {\n const httpError = this.#toHttpError(error)\n\n /**\n * Self handle exception\n */\n if (typeof httpError.handle === 'function') {\n return httpError.handle(httpError, ctx)\n }\n\n /**\n * Handle validation error using the validation error\n * renderers\n */\n if (httpError.code === 'E_VALIDATION_ERROR' && 'messages' in httpError) {\n return this.renderValidationError(httpError, ctx)\n }\n\n /**\n * Render status page\n */\n const statusPages = this.#expandStatusPages()\n if (this.renderStatusPages && statusPages[httpError.status]) {\n return statusPages[httpError.status](httpError, ctx)\n }\n\n /**\n * Use the format renderers.\n */\n return this.renderError(httpError, ctx)\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AASA,OAAO,QAAQ;AACf,OAAO,eAAe;AAoBf,IAAM,mBAAN,cAA+B,UAAU;AAAA;AAAA;AAAA;AAAA,EAI9C;AAAA;AAAA;AAAA;AAAA;AAAA,EAMU,QAAiB,QAAQ,IAAI,aAAa;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAO1C,oBAA6B,QAAQ,IAAI,aAAa;AAAA;AAAA;AAAA;AAAA,EAKtD,cAA2D,CAAC;AAAA;AAAA;AAAA;AAAA,EAK5D,eAAwB;AAAA;AAAA;AAAA;AAAA;AAAA,EAMxB,mBAA0B;AAAA,IAC3B;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACT;AAAA;AAAA;AAAA;AAAA;AAAA,EAMU,iBAA2B,CAAC,KAAK,KAAK,GAAG;AAAA;AAAA;AAAA;AAAA;AAAA,EAMzC,cAAwB,CAAC;AAAA;AAAA;AAAA;AAAA,EAKnC,qBAAqB;AACnB,QAAI,CAAC,KAAK,sBAAsB;AAC9B,WAAK,uBAAuB,OAAO,KAAK,KAAK,WAAW,EAAE;AAAA,QACxD,CAAC,QAAQ,UAAU;AACjB,gBAAM,WAAW,KA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ACrD;AAKA,WAAO,KAAK,YAAY,WAAW,GAAG;AAAA,EACxC;AACF;","names":[]}
1
+ {"version":3,"sources":["../src/exception_handler.ts"],"sourcesContent":["/*\n * @adonisjs/http-server\n *\n * (c) AdonisJS\n *\n * For the full copyright and license information, please view the LICENSE\n * file that was distributed with this source code.\n */\n\nimport is from '@sindresorhus/is'\nimport Macroable from '@poppinss/macroable'\nimport type { Level } from '@adonisjs/logger/types'\n\nimport { parseRange } from './helpers.js'\nimport * as errors from './exceptions.js'\nimport type { HttpContext } from './http_context/main.js'\nimport type { HttpError, StatusPageRange, StatusPageRenderer } from './types/server.js'\n\n/**\n * The base HTTP exception handler one can inherit from to handle\n * HTTP exceptions.\n *\n * The HTTP exception handler has support for\n *\n * - Ability to render exceptions by calling the render method on the exception.\n * - Rendering status pages\n * - Pretty printing errors during development\n * - Transforming errors to JSON or HTML using content negotiation\n * - Reporting errors\n */\nexport class ExceptionHandler extends Macroable {\n /**\n * Computed from the status pages property\n */\n #expandedStatusPages?: Record<number, StatusPageRenderer>\n\n /**\n * Whether or not to render debug info. When set to true, the errors\n * will have the complete error stack.\n */\n protected debug: boolean = process.env.NODE_ENV !== 'production'\n\n /**\n * Whether or not to render status pages. When set to true, the unhandled\n * errors with matching status codes will be rendered using a status\n * page.\n */\n protected renderStatusPages: boolean = process.env.NODE_ENV === 'production'\n\n /**\n * A collection of error status code range and the view to render.\n */\n protected statusPages: Record<StatusPageRange, StatusPageRenderer> = {}\n\n /**\n * Enable/disable errors reporting\n */\n protected reportErrors: boolean = true\n\n /**\n * An array of exception classes to ignore when\n * reporting an error\n */\n protected ignoreExceptions: any[] = [\n errors.E_HTTP_EXCEPTION,\n errors.E_ROUTE_NOT_FOUND,\n errors.E_CANNOT_LOOKUP_ROUTE,\n errors.E_HTTP_REQUEST_ABORTED,\n ]\n\n /**\n * An array of HTTP status codes to ignore when reporting\n * an error\n */\n protected ignoreStatuses: number[] = [400, 422, 401]\n\n /**\n * An array of error codes to ignore when reporting\n * an error\n */\n protected ignoreCodes: string[] = []\n\n /**\n * Expands status pages\n */\n #expandStatusPages() {\n if (!this.#expandedStatusPages) {\n this.#expandedStatusPages = Object.keys(this.statusPages).reduce(\n (result, range) => {\n const renderer = this.statusPages[range as StatusPageRange]\n result = Object.assign(result, parseRange(range, renderer))\n return result\n },\n {} as Record<number, StatusPageRenderer>\n )\n }\n\n return this.#expandedStatusPages\n }\n\n /**\n * Forcefully tweaking the type of the error object to\n * have known properties.\n */\n #toHttpError(error: unknown): HttpError {\n const httpError: any = is.object(error) ? error : new Error(String(error))\n if (!httpError.message) {\n httpError.message = 'Internal server error'\n }\n if (!httpError.status) {\n httpError.status = 500\n }\n return httpError\n }\n\n /**\n * Error reporting context\n */\n protected context(ctx: HttpContext): any {\n const requestId = ctx.request.id()\n return requestId\n ? {\n 'x-request-id': requestId,\n }\n : {}\n }\n\n /**\n * Returns the log level for an error based upon the error\n * status code.\n */\n protected getErrorLogLevel(error: HttpError): Level {\n if (error.status >= 500) {\n return 'error'\n }\n\n if (error.status >= 400) {\n return 'warn'\n }\n\n return 'info'\n }\n\n /**\n * A boolean to control if errors should be rendered with\n * all the available debugging info.\n */\n protected isDebuggingEnabled(_: HttpContext): boolean {\n return this.debug\n }\n\n /**\n * Returns a boolean by checking if an error should be reported.\n */\n protected shouldReport(error: HttpError): boolean {\n if (this.reportErrors === false) {\n return false\n }\n\n if (this.ignoreStatuses.includes(error.status)) {\n return false\n }\n\n if (error.code && this.ignoreCodes.includes(error.code)) {\n return false\n }\n\n if (this.ignoreExceptions.find((exception) => error instanceof exception)) {\n return false\n }\n\n return true\n }\n\n /**\n * Renders an error to JSON response\n */\n async renderErrorAsJSON(error: HttpError, ctx: HttpContext) {\n if (this.isDebuggingEnabled(ctx)) {\n const { default: Youch } = await import('youch')\n const json = await new Youch(error, ctx.request.request).toJSON()\n ctx.response.status(error.status).send(json.error)\n return\n }\n\n ctx.response.status(error.status).send({ message: error.message })\n }\n\n /**\n * Renders an error to JSON API response\n */\n async renderErrorAsJSONAPI(error: HttpError, ctx: HttpContext) {\n if (this.isDebuggingEnabled(ctx)) {\n const { default: Youch } = await import('youch')\n const json = await new Youch(error, ctx.request.request).toJSON()\n ctx.response.status(error.status).send(json.error)\n return\n }\n\n ctx.response.status(error.status).send({\n errors: [\n {\n title: error.message,\n code: error.code,\n status: error.status,\n },\n ],\n })\n }\n\n /**\n * Renders an error to HTML response\n */\n async renderErrorAsHTML(error: HttpError, ctx: HttpContext) {\n if (this.isDebuggingEnabled(ctx)) {\n const { default: Youch } = await import('youch')\n const html = await new Youch(error, ctx.request.request).toHTML({\n cspNonce: 'nonce' in ctx.response ? ctx.response.nonce : undefined,\n })\n ctx.response.status(error.status).send(html)\n return\n }\n\n ctx.response.status(error.status).send(`<p> ${error.message} </p>`)\n }\n\n /**\n * Renders the validation error message to a JSON\n * response\n */\n async renderValidationErrorAsJSON(error: HttpError, ctx: HttpContext) {\n ctx.response.status(error.status).send({\n errors: error.messages,\n })\n }\n\n /**\n * Renders the validation error message as per JSON API\n * spec\n */\n async renderValidationErrorAsJSONAPI(error: HttpError, ctx: HttpContext) {\n ctx.response.status(error.status).send({\n errors: error.messages.map((message: any) => {\n return {\n title: message.message,\n code: message.rule,\n source: {\n pointer: message.field,\n },\n meta: message.meta,\n }\n }),\n })\n }\n\n /**\n * Renders the validation error as an HTML string\n */\n async renderValidationErrorAsHTML(error: HttpError, ctx: HttpContext) {\n ctx.response\n .status(error.status)\n .type('html')\n .send(\n error.messages\n .map((message: any) => {\n return `${message.field} - ${message.message}`\n })\n .join('<br />')\n )\n }\n\n /**\n * Renders the error to response\n */\n renderError(error: HttpError, ctx: HttpContext) {\n switch (ctx.request.accepts(['html', 'application/vnd.api+json', 'json'])) {\n case 'application/vnd.api+json':\n return this.renderErrorAsJSONAPI(error, ctx)\n case 'json':\n return this.renderErrorAsJSON(error, ctx)\n case 'html':\n default:\n return this.renderErrorAsHTML(error, ctx)\n }\n }\n\n /**\n * Renders the validation error to response\n */\n renderValidationError(error: HttpError, ctx: HttpContext) {\n switch (ctx.request.accepts(['html', 'application/vnd.api+json', 'json'])) {\n case 'application/vnd.api+json':\n return this.renderValidationErrorAsJSONAPI(error, ctx)\n case 'json':\n return this.renderValidationErrorAsJSON(error, ctx)\n case 'html':\n default:\n return this.renderValidationErrorAsHTML(error, ctx)\n }\n }\n\n /**\n * Reports an error during an HTTP request\n */\n async report(error: unknown, ctx: HttpContext) {\n const httpError = this.#toHttpError(error)\n if (!this.shouldReport(httpError)) {\n return\n }\n\n if (typeof httpError.report === 'function') {\n httpError.report(httpError, ctx)\n return\n }\n\n /**\n * Log the error using the logger\n */\n const level = this.getErrorLogLevel(httpError)\n ctx.logger.log(\n level,\n {\n ...(level === 'error' || level === 'fatal' ? { err: httpError } : {}),\n ...this.context(ctx),\n },\n httpError.message\n )\n }\n\n /**\n * Handles the error during the HTTP request.\n */\n async handle(error: unknown, ctx: HttpContext) {\n const httpError = this.#toHttpError(error)\n\n /**\n * Self handle exception\n */\n if (typeof httpError.handle === 'function') {\n return httpError.handle(httpError, ctx)\n }\n\n /**\n * Handle validation error using the validation error\n * renderers\n */\n if (httpError.code === 'E_VALIDATION_ERROR' && 'messages' in httpError) {\n return this.renderValidationError(httpError, ctx)\n }\n\n /**\n * Render status page\n */\n const statusPages = this.#expandStatusPages()\n if (this.renderStatusPages && statusPages[httpError.status]) {\n const statusPageResponse = await statusPages[httpError.status](httpError, ctx)\n\n /**\n * Use return value and convert it into a response\n */\n if (\n statusPageResponse !== undefined && // Return value is explicitly defined\n !ctx.response.hasLazyBody && // Lazy body is not set\n statusPageResponse !== ctx.response // Return value is not the instance of response object\n ) {\n return ctx.response.safeStatus(httpError.status).send(statusPageResponse)\n }\n return statusPageResponse\n }\n\n /**\n * Use the format renderers.\n */\n return this.renderError(httpError, ctx)\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AASA,OAAO,QAAQ;AACf,OAAO,eAAe;AAoBf,IAAM,mBAAN,cAA+B,UAA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AM,QAAQ,KAAK,iBAAiB,SAAS;AAC7C,QAAI,OAAO;AAAA,MACT;AAAA,MACA;AAAA,QACE,GAAI,UAAU,WAAW,UAAU,UAAU,EAAE,KAAK,UAAU,IAAI,CAAC;AAAA,QACnE,GAAG,KAAK,QAAQ,GAAG;AAAA,MACrB;AAAA,MACA,UAAU;AAAA,IACZ;AAAA,EACF;AAAA;AAAA;AAAA;AAAA,EAKA,MAAM,OAAO,OAAgB,KAAkB;AAC7C,UAAM,YAAY,KAAK,aAAa,KAAK;AAKzC,QAAI,OAAO,UAAU,WAAW,YAAY;AAC1C,aAAO,UAAU,OAAO,WAAW,GAAG;AAAA,IACxC;AAMA,QAAI,UAAU,SAAS,wBAAwB,cAAc,WAAW;AACtE,aAAO,KAAK,sBAAsB,WAAW,GAAG;AAAA,IAClD;AAKA,UAAM,cAAc,KAAK,mBAAmB;AAC5C,QAAI,KAAK,qBAAqB,YAAY,UAAU,MAAM,GAAG;AAC3D,YAAM,qBAAqB,MAAM,YAAY,UAAU,MAAM,EAAE,WAAW,GAAG;AAK7E,UACE,uBAAuB;AAAA,MACvB,CAAC,IAAI,SAAS;AAAA,MACd,uBAAuB,IAAI,UAC3B;AACA,eAAO,IAAI,SAAS,WAAW,UAAU,MAAM,EAAE,KAAK,kBAAkB;AAAA,MAC1E;AACA,aAAO;AAAA,IACT;AAKA,WAAO,KAAK,YAAY,WAAW,GAAG;AAAA,EACxC;AACF;","names":[]}
@@ -11,6 +11,8 @@ export type CookieOptions = {
11
11
  path: string;
12
12
  sameSite: boolean | 'lax' | 'none' | 'strict';
13
13
  secure: boolean;
14
+ partitioned?: boolean;
15
+ priority?: 'low' | 'medium' | 'high';
14
16
  };
15
17
  /**
16
18
  * Types from which response header can be casted to a
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adonisjs/http-server",
3
- "version": "7.0.0",
3
+ "version": "7.0.2",
4
4
  "description": "AdonisJS HTTP server with support packed with Routing and Cookies",
5
5
  "main": "build/index.js",
6
6
  "type": "module",
@@ -42,21 +42,21 @@
42
42
  "author": "virk,adonisjs",
43
43
  "license": "MIT",
44
44
  "devDependencies": {
45
- "@adonisjs/application": "^8.0.0",
45
+ "@adonisjs/application": "^8.0.2",
46
46
  "@adonisjs/encryption": "^6.0.0",
47
47
  "@adonisjs/eslint-config": "^1.2.1",
48
48
  "@adonisjs/events": "^9.0.0",
49
- "@adonisjs/fold": "^10.0.0",
50
- "@adonisjs/logger": "^6.0.0",
49
+ "@adonisjs/fold": "^10.0.1",
50
+ "@adonisjs/logger": "^6.0.1",
51
51
  "@adonisjs/prettier-config": "^1.2.1",
52
52
  "@adonisjs/tsconfig": "^1.2.1",
53
- "@commitlint/cli": "^18.4.4",
54
- "@commitlint/config-conventional": "^18.4.4",
53
+ "@commitlint/cli": "^18.5.0",
54
+ "@commitlint/config-conventional": "^18.5.0",
55
55
  "@fastify/middie": "^8.3.0",
56
56
  "@japa/assert": "^2.1.0",
57
57
  "@japa/expect-type": "^2.0.1",
58
58
  "@japa/runner": "^3.1.1",
59
- "@swc/core": "^1.3.102",
59
+ "@swc/core": "^1.3.105",
60
60
  "@types/accepts": "^1.3.7",
61
61
  "@types/content-disposition": "^0.5.8",
62
62
  "@types/cookie": "^0.6.0",
@@ -67,7 +67,7 @@
67
67
  "@types/fs-extra": "^11.0.4",
68
68
  "@types/http-status-codes": "^1.2.0",
69
69
  "@types/mime-types": "^2.1.4",
70
- "@types/node": "^20.10.6",
70
+ "@types/node": "^20.11.5",
71
71
  "@types/on-finished": "^2.3.4",
72
72
  "@types/pem": "^1.14.4",
73
73
  "@types/proxy-addr": "^2.0.3",
@@ -77,7 +77,7 @@
77
77
  "@types/vary": "^1.1.3",
78
78
  "@vinejs/vine": "^1.6.0",
79
79
  "autocannon": "^7.14.0",
80
- "c8": "^9.0.0",
80
+ "c8": "^9.1.0",
81
81
  "cross-env": "^7.0.3",
82
82
  "del-cli": "^5.1.0",
83
83
  "eslint": "^8.56.0",
@@ -89,9 +89,9 @@
89
89
  "husky": "^8.0.3",
90
90
  "np": "^9.2.0",
91
91
  "pem": "^1.14.8",
92
- "prettier": "^3.1.1",
92
+ "prettier": "^3.2.4",
93
93
  "reflect-metadata": "^0.2.1",
94
- "supertest": "^6.3.3",
94
+ "supertest": "^6.3.4",
95
95
  "ts-node": "^10.9.2",
96
96
  "tsup": "^8.0.1",
97
97
  "typescript": "^5.3.3"
@@ -101,7 +101,7 @@
101
101
  "@poppinss/macroable": "^1.0.1",
102
102
  "@poppinss/matchit": "^3.1.2",
103
103
  "@poppinss/middleware": "^3.2.2",
104
- "@poppinss/utils": "^6.7.0",
104
+ "@poppinss/utils": "^6.7.1",
105
105
  "@sindresorhus/is": "^6.1.0",
106
106
  "accepts": "^1.3.8",
107
107
  "content-disposition": "^0.5.4",
@@ -120,11 +120,11 @@
120
120
  "youch": "^3.3.3"
121
121
  },
122
122
  "peerDependencies": {
123
- "@adonisjs/application": "^8.0.0",
123
+ "@adonisjs/application": "^8.0.2",
124
124
  "@adonisjs/encryption": "^6.0.0",
125
125
  "@adonisjs/events": "^9.0.0",
126
- "@adonisjs/fold": "^10.0.0",
127
- "@adonisjs/logger": "^6.0.0"
126
+ "@adonisjs/fold": "^10.0.1",
127
+ "@adonisjs/logger": "^6.0.1"
128
128
  },
129
129
  "repository": {
130
130
  "type": "git",