http/json_text.ts

JSON text read as the Rust spine's serde_json reads it.

JSON.parse accepts three things serde_json refuses: an escaped lone surrogate ("\ud800", in a string or a key), a number out of f64 range (1e400, which JSON.parse reads as Infinity), and nesting 128 deep or more. Accepted, they don't survive storage: a lone surrogate is written to a text column as U+FFFD and refused by a jsonb one, and Infinity serializes as null. parse_json_text refuses all three, so a request body or frame gets the same verdict on both spines (invalid_json_body for a REST body, parse_error for a JSON-RPC body or frame, invalid_params for GET /rpc's params).

The check is one linear pass over the text after JSON.parse accepts it (is_serde_json_text): a few milliseconds a MiB, whatever the text holds.

"\u0000" is not refused here: both parsers accept it. Postgres refuses it, so the input steps do, as an invalid value rather than invalid JSON (scan_json_value in http/schema_helpers.ts).

view source

Declarations
#

5 declarations

JSON_DEPTH_LIMIT
#

http/json_text.ts view source

128 import {JSON_DEPTH_LIMIT} from '@fuzdev/fuz_app/http/json_text.js';

The nesting depth serde_json refuses: a value nested this deep or deeper fails.

JsonBodyRequest
#

http/json_text.ts view source

JsonBodyRequest import type {JsonBodyRequest} from '@fuzdev/fuz_app/http/json_text.js';

The request a body is read from: Hono's c.req, structurally, so this module stays hono-free.

raw

type Request

text

type (): Promise<string>

returns Promise<string>

JsonBodyResult
#

http/json_text.ts view source

JsonBodyResult

type { ok: true; value: unknown; } | { ok: false; }

import type {JsonBodyResult} from '@fuzdev/fuz_app/http/json_text.js';

A request body read as JSON by read_json_body: the value, or a refusal.

parse_json_text
#

http/json_text.ts view source

(text: string): unknown import {parse_json_text} from '@fuzdev/fuz_app/http/json_text.js';

Parses JSON text as JSON.parse does, refusing what serde_json refuses: an escaped lone surrogate, a number out of f64 range, and nesting JSON_DEPTH_LIMIT deep or more.

text

the JSON text

type string

returns

unknown

the parsed value

throws

  • SyntaxError - when `JSON.parse` throws or the text has one of the three

read_json_body
#

http/json_text.ts view source

(req: JsonBodyRequest): Promise<JsonBodyResult> import {read_json_body} from '@fuzdev/fuz_app/http/json_text.js';

Reads a request body as JSON text through parse_json_text, once per request: the authorization phase's acting read and input validation share the one parse. Reads the body with c.req.text(), as parse_json_text reads the text and not a parsed value.

req

the request, Hono's c.req

returns

Promise<JsonBodyResult>

the parsed body, or {ok: false} when it isn't JSON text parse_json_text accepts

Imported by
#