keryx 0.48.1 → 0.48.2
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.
- package/package.json +1 -1
- package/servers/web.ts +17 -8
- package/util/webRouting.ts +32 -13
package/package.json
CHANGED
package/servers/web.ts
CHANGED
|
@@ -673,14 +673,23 @@ export class WebServer extends Server<ReturnType<typeof Bun.serve>> {
|
|
|
673
673
|
rawBody,
|
|
674
674
|
});
|
|
675
675
|
} catch (e) {
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
)
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
676
|
+
// An oversized or unparseable body is the client's fault: answer it with
|
|
677
|
+
// the JSON error envelope (and CORS headers) rather than letting it
|
|
678
|
+
// escape to Bun's default 500 handler, and never leak the connection.
|
|
679
|
+
connection.destroy();
|
|
680
|
+
const error =
|
|
681
|
+
e instanceof TypedError
|
|
682
|
+
? e
|
|
683
|
+
: new TypedError({
|
|
684
|
+
message: `cannot parse request body: ${e}`,
|
|
685
|
+
type: ErrorType.CONNECTION_ACTION_PARAM_VALIDATION,
|
|
686
|
+
cause: e,
|
|
687
|
+
});
|
|
688
|
+
const status = error.message.startsWith("Payload Too Large") ? 413 : 400;
|
|
689
|
+
return {
|
|
690
|
+
response: buildError(connection, error, status, requestOrigin),
|
|
691
|
+
actionName: actionName ?? undefined,
|
|
692
|
+
};
|
|
684
693
|
}
|
|
685
694
|
|
|
686
695
|
const { response, error } = await connection.act(
|
package/util/webRouting.ts
CHANGED
|
@@ -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
|
|
182
|
-
*
|
|
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
|
-
|
|
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.
|
|
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
|
|