@juspay/neurolink
Version:
The pipe layer of an AI nervous system: one interface connecting provider neurons to your application, across three inference types — generate, stream and decide. `decide` returns typed, calibrated judgements from a non-generative model (~400ms, ~$0.00002
34 lines (33 loc) • 1.83 kB
TypeScript
/**
* Bounded JSON and form body reads for the Express adapter.
*
* `express.json()` and `express.urlencoded()` (body-parser) refuse a body over
* their limit with a 413, but only after the whole request has been taken off
* the socket: a declared Content-Length over the limit is seen at once and the
* body is still drained, and a chunked body is read to its end. Nothing has
* authenticated the request at that point, so a client could make the server
* receive any amount of data for the price of one 413.
*
* `createBoundedBodyParser` wraps a body-parser middleware so the bytes are
* read here, up to the limit, and handed to that same middleware to parse. Which
* requests are parsed, charset handling, strict JSON, the qs options and the
* error each failure carries all stay body-parser's, because body-parser still
* makes those decisions: it is asked first, from the headers alone, whether it
* would read the request, and then parses the bytes it is given. What changes is
* who reads the socket and what happens at the limit. A body over the limit is
* refused without reading it (declared Content-Length) or at the limit (chunked,
* or a compressed body that inflates past it), and the connection is closed so
* the client stops sending.
*
* `express.raw()` (uploads) is not wrapped.
*
* @module server/utils/expressBody
*/
import type { ServerNodeMiddleware } from "../../types/index.js";
/**
* Wrap a body-parser middleware (`express.json()`, `express.urlencoded()`) so a
* body is read only as far as `limit`. `limit` is the one the middleware was
* built with. A limit that cannot be read as a byte count here is left to the
* middleware alone.
*/
export declare function createBoundedBodyParser(parser: ServerNodeMiddleware, limit: string | number | undefined): ServerNodeMiddleware;