Files
SnowLuma/.pnpm-store/v11/files/64/d4a3056f56aa48451229ce7c7786bddfe57531d8428f5e2cbd9522a3d045d1a1cdc4f0a1429d8040376ecc921c1e68f5138966cf22be21fdcd6cc49a9a0162
T
TanTan 2a69b23b47
Dev Build / cleanup (push) Waiting to run
Dev Build / typecheck (push) Waiting to run
docs: 仓库链接指向本镜像仓库
2026-11-13 09:27:07 +08:00

64 lines
2.5 KiB
Plaintext

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MAX_BATCH_SIZE = exports.DEFAULT_MAX_REQUEST_BODY_SIZE = void 0;
exports.requestBodyTooLargeMessage = requestBodyTooLargeMessage;
exports.resolveMaxRequestBodySize = resolveMaxRequestBodySize;
exports.readRequestBody = readRequestBody;
/** Default upper bound, in bytes, on a request body read by the HTTP entry points (4 MiB). */
exports.DEFAULT_MAX_REQUEST_BODY_SIZE = 4 * 1024 * 1024;
/** Upper bound on the number of messages accepted in one JSON-RPC batch array. */
exports.MAX_BATCH_SIZE = 100;
/** The message answered with 413 for a request body over `maxBytes`. */
function requestBodyTooLargeMessage(maxBytes) {
return `Payload Too Large: Request body must not exceed ${maxBytes} bytes`;
}
/**
* Resolves a `maxRequestBodySize` option to the bound to apply: the default when
* omitted, otherwise the value itself, which must be a positive finite number of
* bytes (a `RangeError` is thrown at configuration time for anything else).
*/
function resolveMaxRequestBodySize(value) {
if (value === undefined) {
return exports.DEFAULT_MAX_REQUEST_BODY_SIZE;
}
if (typeof value !== 'number' || !Number.isFinite(value) || value <= 0) {
throw new RangeError(`maxRequestBodySize must be a positive number of bytes, got ${String(value)}`);
}
return value;
}
/**
* Reads a request body as text, up to `maxBytes` (default
* {@linkcode DEFAULT_MAX_REQUEST_BODY_SIZE}). A declared `Content-Length` over the
* limit is refused without reading anything; otherwise the read stops as soon as
* more than the limit has arrived. Stream failures propagate.
*/
async function readRequestBody(request, maxBytes = exports.DEFAULT_MAX_REQUEST_BODY_SIZE) {
if (Number(request.headers.get('content-length')) > maxBytes) {
return { tooLarge: true };
}
if (request.body === null) {
return { tooLarge: false, text: '' };
}
const reader = request.body.getReader();
const decoder = new TextDecoder();
let received = 0;
let text = '';
try {
for (;;) {
const { done, value } = await reader.read();
if (done) {
break;
}
received += value.byteLength;
if (received > maxBytes) {
return { tooLarge: true };
}
text += decoder.decode(value, { stream: true });
}
}
finally {
reader.releaseLock();
}
return { tooLarge: false, text: text + decoder.decode() };
}
//# sourceMappingURL=requestBody.js.map