keryx 0.48.1 → 0.49.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -178,8 +178,11 @@ export type ParseRequestParamsOptions = {
178
178
  * unread — nothing is consumed off `req`, so a later reader (typically the
179
179
  * action, via `connection.rawRequest`) still sees the bytes exactly as sent.
180
180
  * @returns A plain object containing all merged parameters.
181
- * @throws {TypedError} With type {@link ErrorType.CONNECTION_ACTION_RUN} when a
182
- * JSON body cannot be parsed, or when the body exceeds
181
+ * @throws {TypedError} With type
182
+ * {@link ErrorType.CONNECTION_ACTION_PARAM_VALIDATION} when a JSON or form
183
+ * body cannot be parsed (an empty or malformed JSON body, or one that is not
184
+ * a JSON object), or with type {@link ErrorType.CONNECTION_ACTION_RUN} and a
185
+ * "Payload Too Large" message when the body exceeds
183
186
  * {@link config.server.web.maxBodySize}.
184
187
  */
185
188
  export async function parseRequestParams(
@@ -203,23 +206,33 @@ export async function parseRequestParams(
203
206
  const contentType = options?.rawBody ? null : baseMediaType(req);
204
207
 
205
208
  if (req.method !== "GET" && contentType === "application/json") {
209
+ // Use streaming reader that aborts early if the body exceeds the
210
+ // configured limit — never allocates the full oversized payload.
211
+ const text = await readBodyWithLimit(req);
212
+ let bodyContent: unknown;
206
213
  try {
207
- // Use streaming reader that aborts early if the body exceeds the
208
- // configured limit — never allocates the full oversized payload.
209
- const text = await readBodyWithLimit(req);
210
- const bodyContent = JSON.parse(text) as Record<string, unknown>;
211
- // Merge JSON body directly — preserves types (objects, arrays, booleans, numbers)
212
- for (const [key, value] of Object.entries(bodyContent)) {
213
- params[key] = value;
214
- }
214
+ bodyContent = JSON.parse(text);
215
215
  } catch (e) {
216
- if (e instanceof TypedError) throw e;
217
216
  throw new TypedError({
218
217
  message: `cannot parse request body: ${e}`,
219
- type: ErrorType.CONNECTION_ACTION_RUN,
218
+ type: ErrorType.CONNECTION_ACTION_PARAM_VALIDATION,
220
219
  cause: e,
221
220
  });
222
221
  }
222
+ if (
223
+ bodyContent === null ||
224
+ typeof bodyContent !== "object" ||
225
+ Array.isArray(bodyContent)
226
+ ) {
227
+ throw new TypedError({
228
+ message: "cannot parse request body: a JSON body must be an object",
229
+ type: ErrorType.CONNECTION_ACTION_PARAM_VALIDATION,
230
+ });
231
+ }
232
+ // Merge JSON body directly — preserves types (objects, arrays, booleans, numbers)
233
+ for (const [key, value] of Object.entries(bodyContent)) {
234
+ params[key] = value;
235
+ }
223
236
  } else if (
224
237
  req.method !== "GET" &&
225
238
  (contentType === "multipart/form-data" ||
@@ -234,7 +247,13 @@ export async function parseRequestParams(
234
247
  await readBodyWithLimit(req.clone() as Request);
235
248
  }
236
249
 
237
- const f = await req.formData();
250
+ const f = await req.formData().catch((e: unknown) => {
251
+ throw new TypedError({
252
+ message: `cannot parse request body: ${e}`,
253
+ type: ErrorType.CONNECTION_ACTION_PARAM_VALIDATION,
254
+ cause: e,
255
+ });
256
+ });
238
257
  f.forEach((value, key) => appendParam(params, key, value));
239
258
  }
240
259