srvx-nightly 0.12.3 → 0.12.4
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/body-limit.d.mts +31 -3
- package/dist/body-limit.mjs +8 -6
- package/dist/cli.mjs +1 -1
- package/package.json +1 -1
package/dist/body-limit.d.mts
CHANGED
|
@@ -26,9 +26,15 @@
|
|
|
26
26
|
* the body is consumed (`request.text()` / `.json()` / `.arrayBuffer()` /
|
|
27
27
|
* `.body`), matching the streamed-limit behaviour.
|
|
28
28
|
*
|
|
29
|
+
* Pass {@link BodyLimitOptions.createError | `options.createError`} to throw your
|
|
30
|
+
* own error on overflow (e.g. a framework's native HTTP error) instead of the
|
|
31
|
+
* default {@link createBodyTooLargeError | `413`-style error}. It is used for both
|
|
32
|
+
* the `Content-Length` fast path and the streaming limit, and is forwarded to
|
|
33
|
+
* `clone()` so clones stay limited with the same error.
|
|
34
|
+
*
|
|
29
35
|
* @see https://srvx.h3.dev/guide/body-limit
|
|
30
36
|
*/
|
|
31
|
-
declare function limitRequestBody<T extends Request>(request: T, maxRequestBodySize: number): T;
|
|
37
|
+
declare function limitRequestBody<T extends Request>(request: T, maxRequestBodySize: number, options?: BodyLimitOptions): T;
|
|
32
38
|
/**
|
|
33
39
|
* Wraps a body `ReadableStream` so the total number of bytes read cannot exceed
|
|
34
40
|
* `maxRequestBodySize`.
|
|
@@ -41,9 +47,13 @@ declare function limitRequestBody<T extends Request>(request: T, maxRequestBodyS
|
|
|
41
47
|
* producing / release the socket). Cancelling the wrapped stream propagates to
|
|
42
48
|
* the upstream stream.
|
|
43
49
|
*
|
|
50
|
+
* Pass {@link BodyLimitOptions.createError | `options.createError`} to error the
|
|
51
|
+
* stream with your own error on overflow instead of the default
|
|
52
|
+
* {@link createBodyTooLargeError | `413`-style error}.
|
|
53
|
+
*
|
|
44
54
|
* @see https://srvx.h3.dev/guide/body-limit
|
|
45
55
|
*/
|
|
46
|
-
declare function limitBodyStream(stream: ReadableStream<Uint8Array>, maxRequestBodySize: number): ReadableStream<Uint8Array>;
|
|
56
|
+
declare function limitBodyStream(stream: ReadableStream<Uint8Array>, maxRequestBodySize: number, options?: BodyLimitOptions): ReadableStream<Uint8Array>;
|
|
47
57
|
/**
|
|
48
58
|
* The canonical error thrown when a request body exceeds `maxRequestBodySize`.
|
|
49
59
|
*
|
|
@@ -60,6 +70,24 @@ interface BodyTooLargeError extends Error {
|
|
|
60
70
|
/** Alias of {@link statusCode}. */
|
|
61
71
|
status: 413;
|
|
62
72
|
}
|
|
73
|
+
/**
|
|
74
|
+
* Factory for the error thrown when a body exceeds `maxRequestBodySize`, injectable
|
|
75
|
+
* via {@link BodyLimitOptions.createError} to override the default.
|
|
76
|
+
*
|
|
77
|
+
* @see https://srvx.h3.dev/guide/body-limit
|
|
78
|
+
*/
|
|
79
|
+
type BodyLimitErrorFactory = (maxRequestBodySize: number) => unknown;
|
|
80
|
+
/**
|
|
81
|
+
* Options for {@link limitRequestBody} and {@link limitBodyStream}.
|
|
82
|
+
*
|
|
83
|
+
* @see https://srvx.h3.dev/guide/body-limit
|
|
84
|
+
*/
|
|
85
|
+
interface BodyLimitOptions {
|
|
86
|
+
/**
|
|
87
|
+
* Factory for the error thrown on overflow. Defaults to {@link createBodyTooLargeError}.
|
|
88
|
+
*/
|
|
89
|
+
createError?: BodyLimitErrorFactory;
|
|
90
|
+
}
|
|
63
91
|
/**
|
|
64
92
|
* Creates the canonical {@link BodyTooLargeError | `413 Payload Too Large` error}
|
|
65
93
|
* used across srvx when a request body exceeds the configured `maxRequestBodySize`.
|
|
@@ -67,4 +95,4 @@ interface BodyTooLargeError extends Error {
|
|
|
67
95
|
* @see https://srvx.h3.dev/guide/body-limit
|
|
68
96
|
*/
|
|
69
97
|
declare function createBodyTooLargeError(maxRequestBodySize: number): BodyTooLargeError;
|
|
70
|
-
export { BodyTooLargeError, createBodyTooLargeError, limitBodyStream, limitRequestBody };
|
|
98
|
+
export { BodyLimitErrorFactory, BodyLimitOptions, BodyTooLargeError, createBodyTooLargeError, limitBodyStream, limitRequestBody };
|
package/dist/body-limit.mjs
CHANGED
|
@@ -1,22 +1,24 @@
|
|
|
1
|
-
function limitRequestBody(request, maxRequestBodySize) {
|
|
1
|
+
function limitRequestBody(request, maxRequestBodySize, options) {
|
|
2
2
|
if (!request.body) return request;
|
|
3
|
+
const createError = options?.createError ?? createBodyTooLargeError;
|
|
3
4
|
const contentLengthHeader = request.headers.get("content-length");
|
|
4
5
|
const contentLength = contentLengthHeader && /^\d+$/.test(contentLengthHeader) ? Number(contentLengthHeader) : NaN;
|
|
5
6
|
const initiallyUsed = request.bodyUsed;
|
|
6
7
|
const overLimit = contentLength > maxRequestBodySize;
|
|
7
|
-
if (overLimit) request.body.cancel(
|
|
8
|
+
if (overLimit) request.body.cancel(createError(maxRequestBodySize)).catch(() => {});
|
|
8
9
|
let limited;
|
|
9
|
-
const limitedBody = () => limited ??= new Response(overLimit ? erroredStream(
|
|
10
|
+
const limitedBody = () => limited ??= new Response(overLimit ? erroredStream(createError(maxRequestBodySize)) : limitBodyStream(request.body, maxRequestBodySize, options));
|
|
10
11
|
return new Proxy(request, { get(target, prop) {
|
|
11
12
|
if (prop === "body") return limitedBody().body;
|
|
12
13
|
if (prop === "bodyUsed") return initiallyUsed || (limited?.bodyUsed ?? false);
|
|
13
14
|
if (typeof prop === "string" && bodyReadMethods.has(prop)) return () => limitedBody()[prop]();
|
|
14
|
-
if (prop === "clone") return () => limitRequestBody(target.clone(), maxRequestBodySize);
|
|
15
|
+
if (prop === "clone") return () => limitRequestBody(target.clone(), maxRequestBodySize, options);
|
|
15
16
|
const value = Reflect.get(target, prop, target);
|
|
16
17
|
return typeof value === "function" ? value.bind(target) : value;
|
|
17
18
|
} });
|
|
18
19
|
}
|
|
19
|
-
function limitBodyStream(stream, maxRequestBodySize) {
|
|
20
|
+
function limitBodyStream(stream, maxRequestBodySize, options) {
|
|
21
|
+
const createError = options?.createError ?? createBodyTooLargeError;
|
|
20
22
|
const reader = stream.getReader();
|
|
21
23
|
let size = 0;
|
|
22
24
|
return new ReadableStream({
|
|
@@ -28,7 +30,7 @@ function limitBodyStream(stream, maxRequestBodySize) {
|
|
|
28
30
|
}
|
|
29
31
|
size += value.byteLength;
|
|
30
32
|
if (size > maxRequestBodySize) {
|
|
31
|
-
const error =
|
|
33
|
+
const error = createError(maxRequestBodySize);
|
|
32
34
|
reader.cancel(error).catch(() => {});
|
|
33
35
|
controller.error(error);
|
|
34
36
|
return;
|
package/dist/cli.mjs
CHANGED