64 lines
2.5 KiB
Plaintext
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 |