@whatwg-node/server
Version:
Fetch API compliant HTTP Server adapter
37 lines (36 loc) • 1.79 kB
TypeScript
import type { FetchAPI } from '../types.js';
import type { ServerAdapterPlugin } from './types.js';
import { HTTPError } from './useErrorHandling.js';
export declare class RequestBodyTooLargeError extends HTTPError {
name: string;
constructor(message?: string);
}
export declare class InvalidContentLengthError extends HTTPError {
name: string;
constructor(message?: string);
}
export type LimitRequestBodySizeOptions = {
/**
* Maps early-reject errors (invalid `Content-Length` / body too large) to an HTTP `Response`
* used with `endResponse`. Defaults to a plain-text body with the error status and message.
*
* Frameworks such as GraphQL Yoga can supply a factory that returns a GraphQL error payload.
*/
responseFromError?: (error: HTTPError, fetchAPI: FetchAPI) => Response;
};
/**
* Limits the size of incoming HTTP request bodies.
*
* Requests whose `Content-Length` exceeds `limit` (or whose `Content-Length` is invalid) are
* rejected early via `endResponse`. Bodies are also counted while streaming (via
* `TransformStream`), including when `Content-Length` is missing, wrong, or overridden — so a
* short `Content-Length` cannot bypass the limit. `Transfer-Encoding` and `Content-Encoding`
* cases are covered by the same byte counter.
*
* When using `useContentEncoding`, register it **before** this plugin so the byte counter sees
* decoded bytes. `onRequest` hooks run in `plugins` array order; the reverse order would count
* compressed size and then allow decompression past `limit`.
*
* To disable limiting, omit this plugin from the adapter.
*/
export declare function useLimitRequestBodySize<TServerContext = {}>(limit: number, options?: LimitRequestBodySizeOptions): ServerAdapterPlugin<TServerContext>;