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 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
- registeredLimit?: number | ((request: Request, size: number) => boolean);
4
- unregisteredLimit?: number | ((request: Request, size: number) => boolean);
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 unregisteredRouteHandler = handleUnregisteredRoute(request, h, options);
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 registeredRouteHandler(contentLength);
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 unregisteredRouteHandler(contentLength);
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 handleRegisteredRoute(request, h, options) {
41
- return (contentLength) => {
42
- if (options.registeredLimit == null ||
43
- (typeof options.registeredLimit === 'number' && options.registeredLimit < 0)) {
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 options.registeredLimit === 'number' && contentLength > options.registeredLimit) ||
47
- (typeof options.registeredLimit === 'function' && options.registeredLimit(request, contentLength))) {
39
+ if ((typeof option === 'number' && contentLength > option) ||
40
+ (typeof option === 'function' && option(request, contentLength))) {
48
41
  request.raw.req.socket?.destroy();
49
- throw Boom.entityTooLarge(typeof options.registeredLimit === 'number'
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
@@ -3,7 +3,7 @@
3
3
  "main": "dist/index.js",
4
4
  "typings": "dist/index.d.ts",
5
5
  "type": "module",
6
- "version": "0.0.4",
6
+ "version": "0.0.6",
7
7
  "license": "MIT",
8
8
  "author": {
9
9
  "name": "Kamaal Farah"
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "main": "dist/index.js",
4
4
  "typings": "dist/index.d.ts",
5
5
  "type": "module",
6
- "version": "0.0.4",
6
+ "version": "0.0.6",
7
7
  "license": "MIT",
8
8
  "author": {
9
9
  "name": "Kamaal Farah"