[]previous and next page

readJsonLimited

Reads and parses a JSON request body safely (see parseJsonSafe), never more than maxBytes.

Reads and parses a JSON request body safely (see parseJsonSafe), never more than maxBytes. The body is read as a stream and the read stops at the bound, so a route that accepts large uploads on purpose can still keep each JSON field small. Use it where readJson would buffer up to the handler's limits.maxBodyBytes.

Import#

ts
import { readJsonLimited } from "@shadow-js/dusk/server";

Signature#

ts
async function readJsonLimited(request: Request, maxBytes: number): Promise<unknown>

Parameters#

Name Description
request The request.
maxBytes The limit in bytes; a smaller limit than the handler's, for this route.

Returns#

The parsed body.

Throws#

  • BodyTooLargeError (413) when the body is larger, before reading anything when Content-Length says so.
  • HttpError 415 when the content type is not JSON or names a charset other than UTF-8, 400 for bytes that are not valid UTF-8, invalid JSON or a forbidden key.
  • RangeError when maxBytes is NaN or negative.

Examples#

ts
export const POST = async ({ request }) => json(await readJsonLimited(request, 64 * 1024));