File size: 2,609 Bytes
edaa6b9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"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