@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
package/dist/internals.js
CHANGED
|
@@ -313,25 +313,64 @@ function parseRequest(request) {
|
|
|
313
313
|
};
|
|
314
314
|
}
|
|
315
315
|
var DEFAULT_MAX_BODY_SIZE = 10 * 1024 * 1024;
|
|
316
|
-
async function
|
|
317
|
-
const
|
|
318
|
-
|
|
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
|
|
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
|
-
|
|
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
|
|
334
|
-
|
|
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.
|
|
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.
|
|
38
|
+
"@vertz/schema": "^0.2.16"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
41
|
"@types/node": "^25.3.1",
|