@vertz/core 0.2.15 → 0.2.17

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/dist/index.js CHANGED
@@ -18,7 +18,7 @@ import {
18
18
  parseBody,
19
19
  parseRequest,
20
20
  runMiddlewareChain
21
- } from "./shared/chunk-m3w3ytn5.js";
21
+ } from "./shared/chunk-wg4kz2h2.js";
22
22
 
23
23
  // src/result.ts
24
24
  var RESULT_BRAND = Symbol.for("vertz.result");
package/dist/internals.js CHANGED
@@ -6,7 +6,7 @@ import {
6
6
  parseBody,
7
7
  parseRequest,
8
8
  runMiddlewareChain
9
- } from "./shared/chunk-m3w3ytn5.js";
9
+ } from "./shared/chunk-wg4kz2h2.js";
10
10
  export {
11
11
  runMiddlewareChain,
12
12
  parseRequest,
@@ -313,25 +313,64 @@ function parseRequest(request) {
313
313
  };
314
314
  }
315
315
  var DEFAULT_MAX_BODY_SIZE = 10 * 1024 * 1024;
316
- async function parseBody(request, maxBodySize = DEFAULT_MAX_BODY_SIZE) {
317
- const contentLength = parseInt(request.headers.get("content-length") ?? "0", 10);
318
- if (maxBodySize && contentLength > maxBodySize) {
316
+ async function readBodyBytes(request, maxBodySize) {
317
+ const contentLengthHeader = request.headers.get("content-length");
318
+ const contentLength = contentLengthHeader ? Number.parseInt(contentLengthHeader, 10) : 0;
319
+ if (maxBodySize > 0 && Number.isFinite(contentLength) && contentLength > maxBodySize) {
319
320
  throw new BadRequestException("Request body too large");
320
321
  }
322
+ if (!request.body) {
323
+ return null;
324
+ }
325
+ const reader = request.body.getReader();
326
+ const chunks = [];
327
+ let totalBytes = 0;
328
+ while (true) {
329
+ const { done, value } = await reader.read();
330
+ if (done)
331
+ break;
332
+ if (!value)
333
+ continue;
334
+ totalBytes += value.byteLength;
335
+ if (maxBodySize > 0 && totalBytes > maxBodySize) {
336
+ await reader.cancel();
337
+ throw new BadRequestException("Request body too large");
338
+ }
339
+ chunks.push(value);
340
+ }
341
+ const body = new Uint8Array(totalBytes);
342
+ let offset = 0;
343
+ for (const chunk of chunks) {
344
+ body.set(chunk, offset);
345
+ offset += chunk.byteLength;
346
+ }
347
+ return body;
348
+ }
349
+ async function parseBody(request, maxBodySize = DEFAULT_MAX_BODY_SIZE) {
321
350
  const contentType = request.headers.get("content-type") ?? "";
322
351
  if (contentType.includes("application/json")) {
352
+ const rawBody = await readBodyBytes(request, maxBodySize);
353
+ const decodedBody = rawBody ? new TextDecoder().decode(rawBody) : "";
323
354
  try {
324
- return await request.json();
355
+ return JSON.parse(decodedBody);
325
356
  } catch {
326
357
  throw new BadRequestException("Invalid JSON body");
327
358
  }
328
359
  }
360
+ if (contentType.includes("application/xml")) {
361
+ const rawBody = await readBodyBytes(request, maxBodySize);
362
+ const decodedBody = rawBody ? new TextDecoder().decode(rawBody) : "";
363
+ return decodedBody;
364
+ }
329
365
  if (contentType.startsWith("text/")) {
330
- return request.text();
366
+ const rawBody = await readBodyBytes(request, maxBodySize);
367
+ const decodedBody = rawBody ? new TextDecoder().decode(rawBody) : "";
368
+ return decodedBody;
331
369
  }
332
370
  if (contentType.includes("application/x-www-form-urlencoded")) {
333
- const text = await request.text();
334
- return Object.fromEntries(new URLSearchParams(text));
371
+ const rawBody = await readBodyBytes(request, maxBodySize);
372
+ const decodedBody = rawBody ? new TextDecoder().decode(rawBody) : "";
373
+ return Object.fromEntries(new URLSearchParams(decodedBody));
335
374
  }
336
375
  return;
337
376
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vertz/core",
3
- "version": "0.2.15",
3
+ "version": "0.2.17",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "description": "Vertz core framework primitives",
@@ -35,7 +35,7 @@
35
35
  "typecheck": "tsc --noEmit -p tsconfig.typecheck.json"
36
36
  },
37
37
  "dependencies": {
38
- "@vertz/schema": "^0.2.14"
38
+ "@vertz/schema": "^0.2.16"
39
39
  },
40
40
  "devDependencies": {
41
41
  "@types/node": "^25.3.1",