hapi-terminator 0.0.4 → 0.0.6
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.d.ts +7 -2
- package/dist/index.js +17 -26
- package/dist/package.json +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
import type { Server, Request } from '@hapi/hapi';
|
|
2
|
+
type LimitOption = number | ((request: Request, size: number) => boolean);
|
|
3
|
+
type LimitOptionName = (typeof LIMIT_OPTION_NAMES)[keyof typeof LIMIT_OPTION_NAMES];
|
|
2
4
|
export type TerminatorOptions = {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
+
[Name in LimitOptionName]?: LimitOption;
|
|
6
|
+
};
|
|
7
|
+
declare const LIMIT_OPTION_NAMES: {
|
|
8
|
+
readonly REGISTERED: "registeredLimit";
|
|
9
|
+
readonly UNREGISTERED: "unregisteredLimit";
|
|
5
10
|
};
|
|
6
11
|
export declare const plugin: {
|
|
7
12
|
pkg: {
|
package/dist/index.js
CHANGED
|
@@ -1,13 +1,16 @@
|
|
|
1
1
|
import Boom from '@hapi/boom';
|
|
2
2
|
import pkg from './package.json';
|
|
3
|
+
const LIMIT_OPTION_NAMES = {
|
|
4
|
+
REGISTERED: 'registeredLimit',
|
|
5
|
+
UNREGISTERED: 'unregisteredLimit',
|
|
6
|
+
};
|
|
3
7
|
export const plugin = { pkg, register };
|
|
4
8
|
async function register(server, options) {
|
|
5
9
|
server.ext('onRequest', onRequest(options));
|
|
6
10
|
}
|
|
7
11
|
function onRequest(options) {
|
|
8
12
|
return (request, h) => {
|
|
9
|
-
const
|
|
10
|
-
const registeredRouteHandler = handleRegisteredRoute(request, h, options);
|
|
13
|
+
const handler = validateRoute(request, h, options);
|
|
11
14
|
const rawContentLength = request.headers['content-length'];
|
|
12
15
|
if (!rawContentLength) {
|
|
13
16
|
return h.continue;
|
|
@@ -18,37 +21,25 @@ function onRequest(options) {
|
|
|
18
21
|
}
|
|
19
22
|
const matchedRoute = request.server.match(request.method, request.path);
|
|
20
23
|
if (matchedRoute != null) {
|
|
21
|
-
return
|
|
24
|
+
return handler(contentLength, LIMIT_OPTION_NAMES.REGISTERED, option => {
|
|
25
|
+
throw Boom.entityTooLarge(typeof option === 'number' ? `Payload content length greater than maximum allowed: ${option}` : undefined);
|
|
26
|
+
});
|
|
22
27
|
}
|
|
23
|
-
return
|
|
24
|
-
};
|
|
25
|
-
}
|
|
26
|
-
function handleUnregisteredRoute(request, h, options) {
|
|
27
|
-
return (contentLength) => {
|
|
28
|
-
if (options.unregisteredLimit == null ||
|
|
29
|
-
(typeof options.unregisteredLimit === 'number' && options.unregisteredLimit < 0)) {
|
|
30
|
-
return h.continue;
|
|
31
|
-
}
|
|
32
|
-
if ((typeof options.unregisteredLimit === 'number' && contentLength > options.unregisteredLimit) ||
|
|
33
|
-
(typeof options.unregisteredLimit === 'function' && options.unregisteredLimit(request, contentLength))) {
|
|
34
|
-
request.raw.req.socket?.destroy();
|
|
28
|
+
return handler(contentLength, LIMIT_OPTION_NAMES.UNREGISTERED, () => {
|
|
35
29
|
throw Boom.notFound();
|
|
36
|
-
}
|
|
37
|
-
return h.continue;
|
|
30
|
+
});
|
|
38
31
|
};
|
|
39
32
|
}
|
|
40
|
-
function
|
|
41
|
-
return (contentLength) => {
|
|
42
|
-
|
|
43
|
-
|
|
33
|
+
function validateRoute(request, h, options) {
|
|
34
|
+
return (contentLength, optionName, throwError) => {
|
|
35
|
+
const option = options[optionName];
|
|
36
|
+
if (option == null || (typeof option === 'number' && option < 0)) {
|
|
44
37
|
return h.continue;
|
|
45
38
|
}
|
|
46
|
-
if ((typeof
|
|
47
|
-
(typeof
|
|
39
|
+
if ((typeof option === 'number' && contentLength > option) ||
|
|
40
|
+
(typeof option === 'function' && option(request, contentLength))) {
|
|
48
41
|
request.raw.req.socket?.destroy();
|
|
49
|
-
|
|
50
|
-
? `Payload content length greater than maximum allowed: ${options.registeredLimit}`
|
|
51
|
-
: undefined);
|
|
42
|
+
throwError(option);
|
|
52
43
|
}
|
|
53
44
|
return h.continue;
|
|
54
45
|
};
|
package/dist/package.json
CHANGED