@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
50 lines (49 loc) • 2.9 kB
TypeScript
/**
* Raw request bodies and raw responses for the server adapters.
*
* The adapters parse JSON (and url-encoded forms) into `ctx.body`. Routes that
* take uploads — `POST /v1/audio/transcriptions` takes `multipart/form-data`
* the way OpenAI's API does — need the bytes instead, so for the content types
* below every adapter hands the route the unparsed body as a `Buffer`, bounded
* by `bodyParser.maxSize`. A route that must answer in a wire format other than
* the adapters' `{ data, metadata }` envelope (an OpenAI-compatible route, a
* plain-text response) returns a web `Response`, which every adapter sends as is.
*
* @module server/utils/rawBody
*/
/** `true` for a content type whose body the adapters pass through unparsed. */
export declare function isRawBodyContentType(contentType: unknown): boolean;
/** "10mb" / "512kb" / "1gb" / a byte count → bytes, or `undefined` when it is none of those. */
export declare function tryParseByteLimit(limit: string | number | undefined): number | undefined;
/** "10mb" / "512kb" / "1gb" / a byte count → bytes. Falls back to 10 MB. */
export declare function parseByteLimit(limit: string | number | undefined): number;
/** Thrown when a raw body grows past the adapter's `maxSize`. */
export declare class RawBodyTooLargeError extends Error {
readonly status = 413;
constructor(limit: number);
}
/** Read a Node request stream into one `Buffer`, refusing more than `maxBytes`. */
export declare function readRawBody(stream: AsyncIterable<Buffer | string>, maxBytes: number): Promise<Buffer>;
/** A web `ReadableStream` of bytes as an async iterable of Buffers, releasing the reader on exit. */
export declare function webStreamChunks(stream: ReadableStream<Uint8Array>): AsyncIterable<Buffer>;
/**
* Read a web `Request` body, refusing more than `maxBytes`.
*
* A declared `Content-Length` over the limit is refused without reading
* anything. Otherwise the stream is read chunk by chunk and stopped at the
* limit, so a chunked body without a `Content-Length` cannot be buffered whole.
* Over the limit the result is the error itself rather than a throw: a throw
* from a middleware lands in the framework's error handler, which answers 500.
*
* The outcome is kept per request, because a web stream can only be read once
* and the body may be asked for more than once (middleware, then route).
*/
export declare function readBoundedRequestBody(request: Request, maxBytes: number): Promise<Buffer | RawBodyTooLargeError>;
/** A web `Response` returned by a route handler. */
export declare function isWebResponse(value: unknown): value is Response;
/** Status, headers and body bytes of a web `Response`, for adapters that write to a Node response. */
export declare function readWebResponse(response: Response): Promise<{
status: number;
headers: Record<string, string>;
body: Buffer;
}>;