@toa.io/extensions.exposition 1.0.0-alpha.152 → 1.0.0-alpha.153

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.
Files changed (114) hide show
  1. package/components/identity.bans/operations/tsconfig.tsbuildinfo +1 -1
  2. package/components/identity.basic/operations/tsconfig.tsbuildinfo +1 -1
  3. package/components/identity.federation/operations/tsconfig.tsbuildinfo +1 -1
  4. package/components/identity.keys/operations/tsconfig.tsbuildinfo +1 -1
  5. package/components/identity.otp/operations/tsconfig.tsbuildinfo +1 -1
  6. package/components/identity.passkeys/operations/tsconfig.tsbuildinfo +1 -1
  7. package/components/identity.roles/operations/tsconfig.tsbuildinfo +1 -1
  8. package/components/identity.tokens/operations/tsconfig.tsbuildinfo +1 -1
  9. package/documentation/io.md +42 -2
  10. package/documentation/notes/desync.jpg +0 -0
  11. package/documentation/notes/peers.md +59 -0
  12. package/documentation/notes/throttling.md +82 -0
  13. package/features/io.throttle.feature +40 -0
  14. package/package.json +3 -3
  15. package/schemas/io/throttle.cos.yaml +36 -0
  16. package/source/Directive.test.ts +4 -2
  17. package/source/Directive.ts +16 -1
  18. package/source/Gateway.ts +2 -0
  19. package/source/HTTP/Server.ts +47 -21
  20. package/source/HTTP/exceptions.ts +21 -15
  21. package/source/RTD/Directives.ts +4 -0
  22. package/source/RTD/Tree.ts +4 -0
  23. package/source/directives/io/Directive.ts +5 -2
  24. package/source/directives/io/IO.ts +17 -5
  25. package/source/directives/io/Input.ts +1 -1
  26. package/source/directives/io/Output.ts +1 -1
  27. package/source/directives/io/Throttle.ts +32 -0
  28. package/source/directives/io/lib/throttle/Configuration.test.ts +40 -0
  29. package/source/directives/io/lib/throttle/Configuration.ts +58 -0
  30. package/source/directives/io/lib/throttle/Interval.ts +31 -0
  31. package/source/directives/io/lib/throttle/Keys.ts +40 -0
  32. package/source/directives/io/lib/throttle/Quota.ts +22 -0
  33. package/source/directives/io/lib/throttle/Quotas.test.ts +132 -0
  34. package/source/directives/io/lib/throttle/Quotas.ts +83 -0
  35. package/source/directives/io/lib/throttle/components/Component.ts +5 -0
  36. package/source/directives/io/lib/throttle/components/IP.ts +40 -0
  37. package/source/directives/io/lib/throttle/components/Path.ts +8 -0
  38. package/source/directives/io/lib/throttle/components/index.ts +13 -0
  39. package/source/directives/io/lib/throttle/conditions/Condition.ts +5 -0
  40. package/source/directives/io/lib/throttle/conditions/Status.ts +17 -0
  41. package/source/directives/io/lib/throttle/conditions/index.ts +11 -0
  42. package/source/directives/io/lib/throttle/index.ts +2 -0
  43. package/source/directives/io/schemas.test.ts +9 -0
  44. package/source/directives/io/schemas.ts +3 -0
  45. package/transpiled/Directive.d.ts +3 -0
  46. package/transpiled/Directive.js +12 -1
  47. package/transpiled/Directive.js.map +1 -1
  48. package/transpiled/Gateway.js +1 -0
  49. package/transpiled/Gateway.js.map +1 -1
  50. package/transpiled/HTTP/Server.js +41 -16
  51. package/transpiled/HTTP/Server.js.map +1 -1
  52. package/transpiled/HTTP/exceptions.d.ts +11 -8
  53. package/transpiled/HTTP/exceptions.js +23 -17
  54. package/transpiled/HTTP/exceptions.js.map +1 -1
  55. package/transpiled/RTD/Directives.d.ts +3 -0
  56. package/transpiled/RTD/Tree.d.ts +1 -0
  57. package/transpiled/RTD/Tree.js +3 -0
  58. package/transpiled/RTD/Tree.js.map +1 -1
  59. package/transpiled/directives/io/Directive.d.ts +5 -2
  60. package/transpiled/directives/io/IO.d.ts +4 -2
  61. package/transpiled/directives/io/IO.js +13 -3
  62. package/transpiled/directives/io/IO.js.map +1 -1
  63. package/transpiled/directives/io/Input.d.ts +1 -1
  64. package/transpiled/directives/io/Input.js +1 -1
  65. package/transpiled/directives/io/Input.js.map +1 -1
  66. package/transpiled/directives/io/Output.d.ts +1 -1
  67. package/transpiled/directives/io/Output.js +1 -1
  68. package/transpiled/directives/io/Output.js.map +1 -1
  69. package/transpiled/directives/io/Throttle.d.ts +11 -0
  70. package/transpiled/directives/io/Throttle.js +51 -0
  71. package/transpiled/directives/io/Throttle.js.map +1 -0
  72. package/transpiled/directives/io/lib/throttle/Configuration.d.ts +23 -0
  73. package/transpiled/directives/io/lib/throttle/Configuration.js +27 -0
  74. package/transpiled/directives/io/lib/throttle/Configuration.js.map +1 -0
  75. package/transpiled/directives/io/lib/throttle/Interval.d.ts +9 -0
  76. package/transpiled/directives/io/lib/throttle/Interval.js +31 -0
  77. package/transpiled/directives/io/lib/throttle/Interval.js.map +1 -0
  78. package/transpiled/directives/io/lib/throttle/Keys.d.ts +12 -0
  79. package/transpiled/directives/io/lib/throttle/Keys.js +34 -0
  80. package/transpiled/directives/io/lib/throttle/Keys.js.map +1 -0
  81. package/transpiled/directives/io/lib/throttle/Quota.d.ts +8 -0
  82. package/transpiled/directives/io/lib/throttle/Quota.js +22 -0
  83. package/transpiled/directives/io/lib/throttle/Quota.js.map +1 -0
  84. package/transpiled/directives/io/lib/throttle/Quotas.d.ts +26 -0
  85. package/transpiled/directives/io/lib/throttle/Quotas.js +61 -0
  86. package/transpiled/directives/io/lib/throttle/Quotas.js.map +1 -0
  87. package/transpiled/directives/io/lib/throttle/components/Component.d.ts +4 -0
  88. package/transpiled/directives/io/lib/throttle/components/Component.js +3 -0
  89. package/transpiled/directives/io/lib/throttle/components/Component.js.map +1 -0
  90. package/transpiled/directives/io/lib/throttle/components/IP.d.ts +6 -0
  91. package/transpiled/directives/io/lib/throttle/components/IP.js +33 -0
  92. package/transpiled/directives/io/lib/throttle/components/IP.js.map +1 -0
  93. package/transpiled/directives/io/lib/throttle/components/Path.d.ts +5 -0
  94. package/transpiled/directives/io/lib/throttle/components/Path.js +10 -0
  95. package/transpiled/directives/io/lib/throttle/components/Path.js.map +1 -0
  96. package/transpiled/directives/io/lib/throttle/components/index.d.ts +5 -0
  97. package/transpiled/directives/io/lib/throttle/components/index.js +10 -0
  98. package/transpiled/directives/io/lib/throttle/components/index.js.map +1 -0
  99. package/transpiled/directives/io/lib/throttle/conditions/Condition.d.ts +4 -0
  100. package/transpiled/directives/io/lib/throttle/conditions/Condition.js +3 -0
  101. package/transpiled/directives/io/lib/throttle/conditions/Condition.js.map +1 -0
  102. package/transpiled/directives/io/lib/throttle/conditions/Status.d.ts +7 -0
  103. package/transpiled/directives/io/lib/throttle/conditions/Status.js +19 -0
  104. package/transpiled/directives/io/lib/throttle/conditions/Status.js.map +1 -0
  105. package/transpiled/directives/io/lib/throttle/conditions/index.d.ts +5 -0
  106. package/transpiled/directives/io/lib/throttle/conditions/index.js +8 -0
  107. package/transpiled/directives/io/lib/throttle/conditions/index.js.map +1 -0
  108. package/transpiled/directives/io/lib/throttle/index.d.ts +2 -0
  109. package/transpiled/directives/io/lib/throttle/index.js +8 -0
  110. package/transpiled/directives/io/lib/throttle/index.js.map +1 -0
  111. package/transpiled/directives/io/schemas.d.ts +2 -0
  112. package/transpiled/directives/io/schemas.js +2 -1
  113. package/transpiled/directives/io/schemas.js.map +1 -1
  114. package/transpiled/tsconfig.tsbuildinfo +1 -1
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.IO = void 0;
4
4
  const Output_1 = require("./Output");
5
5
  const Input_1 = require("./Input");
6
+ const Throttle_1 = require("./Throttle");
6
7
  class IO {
7
8
  name = 'io';
8
9
  mandatory = true;
@@ -17,17 +18,26 @@ class IO {
17
18
  let restricted = false;
18
19
  for (const directive of directives) {
19
20
  restricted ||= directive instanceof Output_1.Output;
20
- directive.attach(context);
21
+ directive.preflight(context);
21
22
  }
22
23
  if (!restricted)
23
- DENIAL.attach(context);
24
+ DENIAL.preflight(context);
24
25
  return null;
25
26
  }
27
+ async settle(directives, context, output) {
28
+ for (const directive of directives)
29
+ await directive.settle?.(context, output);
30
+ }
31
+ dispose(directives) {
32
+ for (const directive of directives)
33
+ directive.dispose?.();
34
+ }
26
35
  }
27
36
  exports.IO = IO;
28
37
  const constructors = {
38
+ input: Input_1.Input,
29
39
  output: Output_1.Output,
30
- input: Input_1.Input
40
+ throttle: Throttle_1.Throttle
31
41
  };
32
42
  const DENIAL = new Output_1.Output([]);
33
43
  //# sourceMappingURL=IO.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"IO.js","sourceRoot":"","sources":["../../../source/directives/io/IO.ts"],"names":[],"mappings":";;;AAAA,qCAAiC;AACjC,mCAA+B;AAK/B,MAAa,EAAE;IACG,IAAI,GAAG,IAAI,CAAA;IACX,SAAS,GAAG,IAAI,CAAA;IAEzB,MAAM,CAAE,IAAY,EAAE,KAAc;QACzC,IAAI,CAAC,CAAC,IAAI,IAAI,YAAY,CAAC;YACzB,MAAM,IAAI,KAAK,CAAC,iBAAiB,IAAI,sBAAsB,CAAC,CAAA;QAE9D,MAAM,SAAS,GAAG,YAAY,CAAC,IAAI,CAAC,CAAA;QAEpC,SAAS,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAA;QAEzB,OAAO,IAAI,SAAS,CAAC,KAAK,CAAC,CAAA;IAC7B,CAAC;IAEM,SAAS,CAAE,UAAuB,EAAE,OAAgB;QACzD,IAAI,UAAU,GAAG,KAAK,CAAA;QAEtB,KAAK,MAAM,SAAS,IAAI,UAAU,EAAE,CAAC;YACnC,UAAU,KAAK,SAAS,YAAY,eAAM,CAAA;YAE1C,SAAS,CAAC,MAAM,CAAC,OAAO,CAAC,CAAA;QAC3B,CAAC;QAED,IAAI,CAAC,UAAU;YACb,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,CAAA;QAExB,OAAO,IAAI,CAAA;IACb,CAAC;CACF;AA7BD,gBA6BC;AAED,MAAM,YAAY,GAAgC;IAChD,MAAM,EAAE,eAAM;IACd,KAAK,EAAE,aAAK;CACb,CAAA;AAED,MAAM,MAAM,GAAW,IAAI,eAAM,CAAC,EAAE,CAAC,CAAA"}
1
+ {"version":3,"file":"IO.js","sourceRoot":"","sources":["../../../source/directives/io/IO.ts"],"names":[],"mappings":";;;AAAA,qCAAiC;AACjC,mCAA+B;AAC/B,yCAAqC;AAKrC,MAAa,EAAE;IACG,IAAI,G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4
  private readonly permissions;
5
5
  constructor(permissions: Permissions);
6
6
  static validate(permissions: unknown): asserts permissions is Permissions;
7
- attach(context: Context): void;
7
+ preflight(context: Context): void;
8
8
  private check;
9
9
  private violation;
10
10
  }
@@ -34,7 +34,7 @@ class Input {
34
34
  static validate(permissions) {
35
35
  schemas.input.validate(permissions, 'Incorrect \'io:input\' format');
36
36
  }
37
- attach(context) {
37
+ preflight(context) {
38
38
  context.pipelines.body.push((body) => this.check(body));
39
39
  }
40
40
  check(body) {
@@ -1 +1 @@
1
- {"version":3,"file":"Input.js","sourceRoot":"","sources":["../../../source/directives/io/Input.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,qCAAuC;AACvC,mDAAoC;AAKpC,MAAa,KAAK;IACC,WAAW,CAAa;IAEzC,YAAoB,WAAwB;QAC1C,IAAI,CAAC,WAAW,GAAG,WAAW,CAAA;IAChC,CAAC;IAEM,MAAM,CAAC,QAAQ,CAAE,WAAoB;QAC1C,OAAO,CAAC,KAAK,CAAC,QAAQ,CAAc,WAAW,EAAE,+BAA+B,CAAC,CAAA;IACnF,CAAC;IAEM,MAAM,CAAE,OAAgB;QAC7B,OAAO,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAA;IACzD,CAAC;IAEO,KAAK,CAAE,IAAa;QAC1B,IAAI,IAAI,KAAK,SAAS;YACpB,OAAO,IAAI,CAAA;QAEb,IAAI,CAAC;YACH,OAAO,CAAC,OAAO,CAAC,QAAQ,CAAsB,IAAI,CAAC,CAAA;QACrD,CAAC;QAAC,MAAM,CAAC;YACP,MAAM,IAAI,iBAAU,CAAC,sBAAsB,CAAC,CAAA;QAC9C,CAAC;QAED,MAAM,QAAQ,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAA;QAErC,IAAI,QAAQ,KAAK,SAAS;YACxB,MAAM,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sB,IAAI,CAAC,CAAA;QACrD,CAAC;QAAC,MAAM,CAAC;YACP,MAAM,IAAI,iBAAU,CAAC,sBAAsB,CAAC,CAAA;QAC9C,CAAC;QAED,MAAM,QAAQ,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAA;QAErC,IAAI,QAAQ,KAAK,SAAS;YACxB,MAAM,IAAI,iBAAU,CAAC,qBAAqB,QAAQ,EAAE,CAAC,CAAA;QAEvD,OAAO,IAAI,CAAA;IACb,CAAC;IAEO,SAAS,CAAE,KAA0B;QAC3C,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC;YACvB,OAAO,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC,IAAI,CAAC,WAAW,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAA;QAE1E,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;YACzB,MAAM,QAAQ,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAA;YAErC,IAAI,QAAQ,KAAK,SAAS;gBACxB,OAAO,QAAQ,CAAA;QACnB,CAAC;IACH,CAAC;CACF;AA5CD,sBA4CC"}
@@ -6,7 +6,7 @@ export declare class Output implements Directive {
6
6
  private readonly permissions;
7
7
  constructor(permissions: Permissions);
8
8
  static validate(permissions: unknown): asserts permissions is Permissions;
9
- attach(context: Context): void;
9
+ preflight(context: Context): void;
10
10
  private restriction;
11
11
  private fit;
12
12
  }
@@ -43,7 +43,7 @@ class Output {
43
43
  static validate(permissions) {
44
44
  schemas.output.validate(permissions, 'Incorrect \'io:output\' format');
45
45
  }
46
- attach(context) {
46
+ preflight(context) {
47
47
  context.pipelines.response.push(this.restriction(context));
48
48
  }
49
49
  restriction(context) {
@@ -1 +1 @@
1
- {"version":3,"file":"Output.js","sourceRoot":"","sources":["../../../source/directives/io/Output.ts"],"names":[],"mappings":";;;;;;;;;;;;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AAI,CAAC;gBAC7B,OAAO,CAAC,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,MAAiB,CAAC,CAAC,CAAA;;gBAExE,OAAO,CAAC,IAAI,GAAG,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,CAAA;QACzC,CAAC,CAAA;IACH,CAAC;IAEO,GAAG,CAAE,OAAgB;QAC3B,MAAM,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC;aACpC,MAAM,CAAC,CAAC,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,WAAW,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAA;QAEpD,OAAO,MAAM,CAAC,WAAW,CAAC,OAAO,CAAC,CAAA;IACpC,CAAC;CACF;AA3DD,wBA2DC"}
@@ -0,0 +1,11 @@
1
+ import { type Declaration } from './lib/throttle';
2
+ import type * as http from '../../HTTP';
3
+ import type { Directive } from './Directive';
4
+ export declare class Throttle implements Directive {
5
+ private readonly quotas;
6
+ constructor(declaration: Declaration);
7
+ static validate(declaration: unknown): asserts declaration is Declaration;
8
+ preflight(context: http.Context): void;
9
+ settle(context: http.Context, output: http.OutgoingMessage): void;
10
+ dispose(): void;
11
+ }
@@ -0,0 +1,51 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.Throttle = void 0;
27
+ const HTTP_1 = require("../../HTTP");
28
+ const schemas = __importStar(require("./schemas"));
29
+ const throttle_1 = require("./lib/throttle");
30
+ class Throttle {
31
+ quotas;
32
+ constructor(declaration) {
33
+ const configuration = (0, throttle_1.parse)(declaration);
34
+ this.quotas = throttle_1.Quotas.create(configuration);
35
+ }
36
+ static validate(declaration) {
37
+ schemas.throttle.validate(declaration, 'Incorrect \'io:throttle\' format');
38
+ }
39
+ preflight(context) {
40
+ if (!this.quotas.ok(context))
41
+ throw new HTTP_1.TooManyRequests();
42
+ }
43
+ settle(context, output) {
44
+ this.quotas.use(context, output);
45
+ }
46
+ dispose() {
47
+ this.quotas.dispose();
48
+ }
49
+ }
50
+ exports.Throttle = Throttle;
51
+ //# sourceMappingURL=Throttle.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Throttle.js","sourceRoot":"","sources":["../../../source/directives/io/Throttle.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,qCAA4C;AAC5C,mDAAoC;AACpC,6CAAgE;AAIhE,MAAa,QAAQ;IACF,MAAM,CAAQ;IAE/B,YAAoB,WAAwB;QAC1C,MAAM,aAAa,GAAG,IAAA,gBAAK,EAAC,WAAW,CAAC,CAAA;QAExC,IAAI,CAAC,MAAM,GAAG,iBAAM,CAAC,MAAM,CAAC,aAAa,CAAC,CAAA;IAC5C,CAAC;IAEM,MAAM,CAAC,QAAQ,CAAE,WAAoB;QAC1C,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAC,WAAW,EAAE,kCAAkC,CAAC,CAAA;IAC5E,CAAC;IAEM,SAAS,CAAE,OAAqB;QACrC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC,OAAO,CAAC;YAC1B,MAAM,IAAI,sBAAe,EAAE,CAAA;IAC/B,CAAC;IAEM,MAAM,CAAE,OAAqB,EAAE,MAA4B;QAChE,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,OAAO,EAAE,MAAM,CAAC,CAAA;IAClC,CAAC;IAEM,OAAO;QACZ,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,CAAA;IACvB,CAAC;CACF;AAzBD,4BAyBC"}
@@ -0,0 +1,23 @@
1
+ export interface Configuration {
2
+ key: KeyComponent[];
3
+ condition?: KeyCondition[];
4
+ requests: number;
5
+ interval: number;
6
+ cooldown: number;
7
+ }
8
+ interface Rule<T, K = unknown> {
9
+ method: T;
10
+ options?: K;
11
+ }
12
+ export type KeyComponentMethod = 'ip' | 'path';
13
+ export type KeyConditionMethod = 'status';
14
+ export type KeyComponent = Rule<KeyComponentMethod>;
15
+ export type KeyCondition = Rule<KeyConditionMethod>;
16
+ type KeyDeclaration = KeyComponentMethod | KeyComponentMethod[];
17
+ type ConditionDeclaration = Record<KeyConditionMethod, unknown> | Array<Record<KeyConditionMethod, unknown>>;
18
+ export interface Declaration extends Omit<Configuration, 'key' | 'condition'> {
19
+ key: KeyDeclaration;
20
+ condition?: ConditionDeclaration;
21
+ }
22
+ export declare function parse(declaration: Declaration): Configuration;
23
+ export {};
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parse = void 0;
4
+ function parse(declaration) {
5
+ const { key, condition, requests, interval, cooldown } = declaration;
6
+ return {
7
+ key: mapKey(key),
8
+ condition: mapCondition(condition),
9
+ requests,
10
+ interval: interval * 1000,
11
+ cooldown: cooldown * 1000
12
+ };
13
+ }
14
+ exports.parse = parse;
15
+ function mapKey(declaration) {
16
+ const methods = Array.isArray(declaration) ? declaration : [declaration];
17
+ return methods.map((method) => ({ method }));
18
+ }
19
+ function mapCondition(declaration) {
20
+ if (declaration === undefined)
21
+ return;
22
+ // reduce to a single object, then map entries to rules
23
+ const conditions = Array.isArray(declaration) ? declaration : [declaration];
24
+ const single = conditions.reduce((acc, condition) => ({ ...acc, ...condition }), {});
25
+ return Object.entries(single).map(([method, options]) => ({ method, options }));
26
+ }
27
+ //# sourceMappingURL=Configuration.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Configuration.js","sourceRoot":"","sources":["../../../../../source/directives/io/lib/throttle/Configuration.ts"],"names":[],"mappings":";;;AA8BA,SAAgB,KAAK,CAAE,WAAwB;IAC7C,MAAM,EAAE,GAAG,EAAE,SAAS,EAAE,QAAQ,EAAE,QAAQ,EAAE,QAAQ,EAAE,GAAG,WAAW,CAAA;IAEpE,OAAO;QACL,GAAG,EAAE,MAAM,CAAC,GAAG,CAAC;QAChB,SAAS,EAAE,YAAY,CAAC,SAAS,CAAC;QAClC,QAAQ;QACR,QAAQ,EAAE,QAAQ,GAAG,IAAI;QACzB,QAAQ,EAAE,QAAQ,GAAG,IAAI;KAC1B,CAAA;AACH,CAAC;AAVD,sBAUC;AAED,SAAS,MAAM,CAAE,WAA2B;IAC1C,MAAM,OAAO,GAAG,KAAK,CAAC,OAAO,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,WAAW,CAAC,CAAA;IAExE,OAAO,OAAO,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,CAAC,CAAA;AAC9C,CAAC;AAED,SAAS,YAAY,CAAE,WAAkC;IACvD,IAAI,WAAW,KAAK,SAAS;QAC3B,OAAM;IAER,uDAAuD;IACvD,MAAM,UAAU,GAAG,KAAK,CAAC,OAAO,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,WAAW,CAAC,CAAA;IAC3E,MAAM,MAAM,GAAG,UAAU,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,SAAS,EAAE,EAAE,CAAC,CAAC,EAAE,GAAG,GAAG,EAAE,GAAG,SAAS,EAAE,CAAC,EAAE,EAAE,CAAC,CAAA;IAEpF,OAAO,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,EAAE,CAAC,CAAC,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC,CAAmB,CAAA;AACnG,CAAC"}
@@ -0,0 +1,9 @@
1
+ /// <reference types="node" />
2
+ import { EventEmitter } from 'node:events';
3
+ export declare class Interval extends EventEmitter {
4
+ number: number;
5
+ private interval;
6
+ constructor(interval: number);
7
+ start(interval: number): void;
8
+ dispose(): void;
9
+ }
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.Interval = void 0;
7
+ const node_assert_1 = __importDefault(require("node:assert"));
8
+ const node_events_1 = require("node:events");
9
+ class Interval extends node_events_1.EventEmitter {
10
+ number = 0;
11
+ interval = null;
12
+ constructor(interval) {
13
+ super();
14
+ const number = Math.ceil(Date.now() / interval);
15
+ const shift = number * interval - Date.now();
16
+ node_assert_1.default.ok(shift >= 0, 'shift must be positive');
17
+ setTimeout(() => this.start(interval), shift);
18
+ }
19
+ start(interval) {
20
+ this.interval = setInterval(() => this.emit('tick'), interval);
21
+ }
22
+ dispose() {
23
+ if (this.interval !== null) {
24
+ clearInterval(this.interval);
25
+ this.interval = null;
26
+ }
27
+ this.removeAllListeners();
28
+ }
29
+ }
30
+ exports.Interval = Interval;
31
+ //# sourceMappingURL=Interval.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Interval.js","sourceRoot":"","sources":["../../../../../source/directives/io/lib/throttle/Interval.ts"],"names":[],"mappings":";;;;;;AAAA,8DAAgC;AAChC,6CAA0C;AAE1C,MAAa,QAAS,SAAQ,0BAAY;IACjC,MAAM,GAAW,CAAC,CAAA;IACjB,QAAQ,GAA0C,IAAI,CAAA;IAE9D,YAAoB,QAAgB;QAClC,KAAK,EAAE,CAAA;QAEP,MAAM,MAAM,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,QAAQ,CAAC,CAAA;QAC/C,MAAM,KAAK,GAAG,MAAM,GAAG,QAAQ,GAAG,IAAI,CAAC,GAAG,EAAE,CAAA;QAE5C,qBAAM,CAAC,EAAE,CAAC,KAAK,IAAI,CAAC,EAAE,wBAAwB,CAAC,CAAA;QAE/C,UAAU,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,EAAE,KAAK,CAAC,CAAA;IAC/C,CAAC;IAEM,KAAK,CAAE,QAAgB;QAC5B,IAAI,CAAC,QAAQ,GAAG,WAAW,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,QAAQ,CAAC,CAAA;IAChE,CAAC;IAEM,OAAO;QACZ,IAAI,IAAI,CAAC,QAAQ,KAAK,IAAI,EAAE,CAAC;YAC3B,aAAa,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAA;YAC5B,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAA;QACtB,CAAC;QAED,IAAI,CAAC,kBAAkB,EAAE,CAAA;IAC3B,CAAC;CACF;AA3BD,4BA2BC"}
@@ -0,0 +1,12 @@
1
+ import { type Component } from './components';
2
+ import { type Condition } from './conditions';
3
+ import type { KeyComponent, KeyCondition } from './Configuration';
4
+ import type { Input as Context, Output } from '../../../../io';
5
+ export declare class Keys {
6
+ private readonly components;
7
+ private readonly conditions?;
8
+ constructor(components: Component[], conditions?: Condition[]);
9
+ static create(componentRules: KeyComponent[], conditionRules?: KeyCondition[]): Keys;
10
+ get(context: Context): string;
11
+ match(input: Context, output: Output): string | null;
12
+ }
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Keys = void 0;
4
+ const node_crypto_1 = require("node:crypto");
5
+ const components_1 = require("./components");
6
+ const conditions_1 = require("./conditions");
7
+ class Keys {
8
+ components;
9
+ conditions;
10
+ constructor(components, conditions) {
11
+ this.components = components;
12
+ this.conditions = conditions;
13
+ }
14
+ static create(componentRules, conditionRules) {
15
+ const components = componentRules.map((rule) => new components_1.Components[rule.method](rule.options));
16
+ const conditions = conditionRules?.map((rule) => new conditions_1.Conditions[rule.method](rule.options));
17
+ return new this(components, conditions);
18
+ }
19
+ get(context) {
20
+ const hash = (0, node_crypto_1.createHash)('sha256');
21
+ for (const component of this.components)
22
+ hash.update(component.get(context));
23
+ return hash.digest('hex');
24
+ }
25
+ match(input, output) {
26
+ const miss = this.conditions?.some((condition) => !condition.match(input, output));
27
+ if (miss === true)
28
+ return null;
29
+ else
30
+ return this.get(input);
31
+ }
32
+ }
33
+ exports.Keys = Keys;
34
+ //# sourceMappingURL=Keys.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Keys.js","sourceRoot":"","sources":["../../../../../source/directives/io/lib/throttle/Keys.ts"],"names":[],"mappings":";;;AAAA,6CAAwC;AACxC,6CAAyD;AACzD,6CAAyD;AAIzD,MAAa,IAAI;IACE,UAAU,CAAa;IACvB,UAAU,CAAc;IAEzC,YAAoB,UAAuB,EAAE,UAAwB;QACnE,IAAI,CAAC,UAAU,GAAG,UAAU,CAAA;QAC5B,IAAI,CAAC,UAAU,GAAG,UAAU,CAAA;IAC9B,CAAC;IAEM,MAAM,CAAC,MAAM,CAAE,cAA8B,EAAE,cAA+B;QACnF,MAAM,UAAU,GAAG,cAAc,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,uBAAU,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAA;QAC1F,MAAM,UAAU,GAAG,cAAc,EAAE,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,uBAAU,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAA;QAE3F,OAAO,IAAI,IAAI,CAAC,UAAU,EAAE,UAAU,CAAC,CAAA;IACzC,CAAC;IAEM,GAAG,CAAE,OAAgB;QAC1B,MAAM,IAAI,GAAG,IAAA,wBAAU,EAAC,QAAQ,CAAC,CAAA;QAEjC,KAAK,MAAM,SAAS,IAAI,IAAI,CAAC,UAAU;YACrC,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAA;QAErC,OAAO,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA;IAC3B,CAAC;IAEM,KAAK,CAAE,KAAc,EAAE,MAAc;QAC1C,MAAM,IAAI,GAAG,IAAI,CAAC,UAAU,EAAE,IAAI,CAAC,CAAC,SAAS,EAAE,EAAE,CAAC,CAAC,SAAS,CAAC,KAAK,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC,CAAA;QAElF,IAAI,IAAI,KAAK,IAAI;YACf,OAAO,IAAI,CAAA;;YAEX,OAAO,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,CAAA;IAC1B,CAAC;CACF;AAjCD,oBAiCC"}
@@ -0,0 +1,8 @@
1
+ export declare class Quota {
2
+ private readonly limit;
3
+ private used;
4
+ constructor(limit: number);
5
+ get idle(): boolean;
6
+ use(count?: number): boolean;
7
+ reset(): void;
8
+ }
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Quota = void 0;
4
+ class Quota {
5
+ limit = 0;
6
+ used = 0;
7
+ constructor(limit) {
8
+ this.limit = limit;
9
+ }
10
+ get idle() {
11
+ return this.used === 0;
12
+ }
13
+ use(count = 1) {
14
+ this.used += count;
15
+ return this.used < this.limit;
16
+ }
17
+ reset() {
18
+ this.used = 0;
19
+ }
20
+ }
21
+ exports.Quota = Quota;
22
+ //# sourceMappingURL=Quota.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Quota.js","sourceRoot":"","sources":["../../../../../source/directives/io/lib/throttle/Quota.ts"],"names":[],"mappings":";;;AAAA,MAAa,KAAK;IACC,KAAK,GAAW,CAAC,CAAA;IAC1B,IAAI,GAAW,CAAC,CAAA;IAExB,YAAoB,KAAa;QAC/B,IAAI,CAAC,KAAK,GAAG,KAAK,CAAA;IACpB,CAAC;IAED,IAAW,IAAI;QACb,OAAO,IAAI,CAAC,IAAI,KAAK,CAAC,CAAA;IACxB,CAAC;IAEM,GAAG,CAAE,KAAK,GAAG,CAAC;QACnB,IAAI,CAAC,IAAI,IAAI,KAAK,CAAA;QAElB,OAAO,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,KAAK,CAAA;IAC/B,CAAC;IAEM,KAAK;QACV,IAAI,CAAC,IAAI,GAAG,CAAC,CAAA;IACf,CAAC;CACF;AArBD,sBAqBC"}
@@ -0,0 +1,26 @@
1
+ import { Keys } from './Keys';
2
+ import { Interval } from './Interval';
3
+ import type { Configuration } from './Configuration';
4
+ import type { Input as Context, Output } from '../../../../io';
5
+ export declare class Quotas {
6
+ private readonly requests;
7
+ private readonly cooldown;
8
+ private readonly interval;
9
+ private readonly keys;
10
+ private readonly quotas;
11
+ private readonly blocked;
12
+ constructor(options: Options);
13
+ static create(configuration: Configuration): Quotas;
14
+ ok(context: Context): boolean;
15
+ use(input: Context, output: Output): void;
16
+ dispose(): void;
17
+ private readonly reset;
18
+ private block;
19
+ }
20
+ interface Options {
21
+ keys: Keys;
22
+ requests: number;
23
+ cooldown: number;
24
+ interval: Interval;
25
+ }
26
+ export {};
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Quotas = void 0;
4
+ const openspan_1 = require("openspan");
5
+ const Keys_1 = require("./Keys");
6
+ const Quota_1 = require("./Quota");
7
+ const Interval_1 = require("./Interval");
8
+ class Quotas {
9
+ requests;
10
+ cooldown;
11
+ interval;
12
+ keys;
13
+ quotas = {};
14
+ blocked = {};
15
+ constructor(options) {
16
+ this.requests = options.requests;
17
+ this.cooldown = options.cooldown;
18
+ this.interval = options.interval;
19
+ this.keys = options.keys;
20
+ this.interval.on('tick', this.reset);
21
+ }
22
+ static create(configuration) {
23
+ const requests = configuration.requests;
24
+ const cooldown = configuration.cooldown;
25
+ const keys = Keys_1.Keys.create(configuration.key, configuration.condition);
26
+ const interval = new Interval_1.Interval(configuration.interval);
27
+ return new this({ requests, cooldown, keys, interval });
28
+ }
29
+ ok(context) {
30
+ const key = this.keys.get(context);
31
+ return this.blocked[key] !== true;
32
+ }
33
+ use(input, output) {
34
+ const key = this.keys.match(input, output);
35
+ if (key === null)
36
+ return;
37
+ this.quotas[key] ??= new Quota_1.Quota(this.requests);
38
+ const ok = this.quotas[key].use();
39
+ if (!ok)
40
+ this.block(key);
41
+ }
42
+ dispose() {
43
+ this.interval.dispose();
44
+ }
45
+ reset = () => {
46
+ for (const key in this.quotas) {
47
+ const quota = this.quotas[key];
48
+ if (quota.idle)
49
+ delete this.quotas[key];
50
+ else
51
+ quota.reset();
52
+ }
53
+ };
54
+ block(key) {
55
+ this.blocked[key] = true;
56
+ setTimeout(() => delete this.blocked[key], this.cooldown);
57
+ openspan_1.console.info('Quota exceeded, key blocked', { key, cooldown: this.cooldown });
58
+ }
59
+ }
60
+ exports.Quotas = Quotas;
61
+ //# sourceMappingURL=Quotas.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Quotas.js","sourceRoot":"","sources":["../../../../../source/directives/io/lib/throttle/Quotas.ts"],"names":[],"mappings":";;;AAAA,uCAAkC;AAClC,iCAA6B;AAC7B,mCAA+B;AAC/B,yCAAqC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,CAAA;QAE1C,IAAI,GAAG,KAAK,IAAI;YACd,OAAM;QAER,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,KAAK,IAAI,aAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAA;QAE7C,MAAM,EAAE,GAAG,IAAI,CAAC,MAAM,CAAC,GAAG,CAAE,CAAC,GAAG,EAAE,CAAA;QAElC,IAAI,CAAC,EAAE;YACL,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;IACnB,CAAC;IAEM,OAAO;QACZ,IAAI,CAAC,QAAQ,CAAC,OAAO,EAAE,CAAA;IACzB,CAAC;IAEgB,KAAK,GAAG,GAAS,EAAE;QAClC,KAAK,MAAM,GAAG,IAAI,IAAI,CAAC,MAAM,EAAE,CAAC;YAC9B,MAAM,KAAK,GAAG,IAAI,CAAC,MAAM,CAAC,GAAG,CAAE,CAAA;YAE/B,IAAI,KAAK,CAAC,IAAI;gBACZ,OAAO,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;;gBAEvB,KAAK,CAAC,KAAK,EAAE,CAAA;QACjB,CAAC;IACH,CAAC,CAAA;IAEO,KAAK,CAAE,GAAW;QACxB,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,IAAI,CAAA;QAExB,UAAU,CAAC,GAAG,EAAE,CAAC,OAAO,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAA;QAEzD,kBAAO,CAAC,IAAI,CAAC,6BAA6B,EAAE,EAAE,GAAG,EAAE,QAAQ,EAAE,IAAI,CAAC,QAAQ,EAAE,CAAC,CAAA;IAC/E,CAAC;CACF;AApED,wBAoEC"}
@@ -0,0 +1,4 @@
1
+ import type { Input as Context } from '../../../../../io';
2
+ export interface Component {
3
+ get: (context: Context) => string;
4
+ }
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=Component.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Component.js","sourceRoot":"","sources":["../../../../../../source/directives/io/lib/throttle/components/Component.ts"],"names":[],"mappings":""}
@@ -0,0 +1,6 @@
1
+ import type { Context } from '../../../../../HTTP';
2
+ import type { Component } from './Component';
3
+ export declare class IP implements Component {
4
+ get(context: Context): string;
5
+ private xff;
6
+ }
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.IP = void 0;
4
+ class IP {
5
+ get(context) {
6
+ return this.xff(context) ?? context.request.socket.remoteAddress ?? '';
7
+ }
8
+ xff(context) {
9
+ const xff = context.request.headers['x-forwarded-for'];
10
+ if (xff === undefined || typeof xff === 'string')
11
+ return xff;
12
+ let ip;
13
+ for (const value of xff) {
14
+ ip = value.trim();
15
+ if (!local(ip))
16
+ return ip;
17
+ }
18
+ return ip; // last otherwise
19
+ }
20
+ }
21
+ exports.IP = IP;
22
+ function local(ip) {
23
+ return (ip === 'unknown' ||
24
+ ip === '' ||
25
+ ip === '127.0.0.1' ||
26
+ ip === '::1' ||
27
+ ip.startsWith('10.') ||
28
+ ip.startsWith('192.168.') ||
29
+ ip.match(/^172\.(1[6-9]|2[0-9]|3[0-1])\./) !== null ||
30
+ ip.startsWith('fd') ||
31
+ ip.startsWith('fe80:'));
32
+ }
33
+ //# sourceMappingURL=IP.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"IP.js","sourceRoot":"","sources":["../../../../../../source/directives/io/lib/throttle/components/IP.ts"],"names":[],"mappings":";;;AAGA,MAAa,EAAE;IACN,GAAG,CAAE,OAAgB;QAC1B,OAAO,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,IAAI,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC,aAAa,IAAI,EAAE,CAAA;IACxE,CAAC;IAEO,GAAG,CAAE,OAAgB;QAC3B,MAAM,GAAG,GAAG,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC,iBAAiB,CAAC,CAAA;QAEtD,IAAI,GAAG,KAAK,SAAS,IAAI,OAAO,GAAG,KAAK,QAAQ;YAC9C,OAAO,GAAG,CAAA;QAEZ,IAAI,EAAE,CAAA;QAEN,KAAK,MAAM,KAAK,IAAI,GAAG,EAAE,CAAC;YACxB,EAAE,GAAG,KAAK,CAAC,IAAI,EAAE,CAAA;YAEjB,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC;gBACZ,OAAO,EAAE,CAAA;QACb,CAAC;QAED,OAAO,EAAE,CAAA,CAAC,iBAAiB;IAC7B,CAAC;CACF;AAtBD,gBAsBC;AAED,SAAS,KAAK,CAAE,EAAU;IACxB,OAAO,CACL,EAAE,KAAK,SAAS;QAChB,EAAE,KAAK,EAAE;QACT,EAAE,KAAK,WAAW;QAClB,EAAE,KAAK,KAAK;QACZ,EAAE,CAAC,UAAU,CAAC,KAAK,CAAC;QACpB,EAAE,CAAC,UAAU,CAAC,UAAU,CAAC;QACzB,EAAE,CAAC,KAAK,CAAC,gCAAgC,CAAC,KAAK,IAAI;QACnD,EAAE,CAAC,UAAU,CAAC,IAAI,CAAC;QACnB,EAAE,CAAC,UAAU,CAAC,OAAO,CAAC,CACvB,CAAA;AACH,CAAC"}
@@ -0,0 +1,5 @@
1
+ import type { Context } from '../../../../../HTTP';
2
+ import type { Component } from './Component';
3
+ export declare class Path implements Component {
4
+ get(context: Context): string;
5
+ }
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Path = void 0;
4
+ class Path {
5
+ get(context) {
6
+ return context.url.pathname;
7
+ }
8
+ }
9
+ exports.Path = Path;
10
+ //# sourceMappingURL=Path.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Path.js","sourceRoot":"","sources":["../../../../../../source/directives/io/lib/throttle/components/Path.ts"],"names":[],"mappings":";;;AAGA,MAAa,IAAI;IACR,GAAG,CAAE,OAAgB;QAC1B,OAAO,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAA;IAC7B,CAAC;CACF;AAJD,oBAIC"}
@@ -0,0 +1,5 @@
1
+ import type { KeyComponentMethod } from '../Configuration';
2
+ import type { Component } from './Component';
3
+ type Constructor<T> = new (options: unknown) => T;
4
+ export declare const Components: Record<KeyComponentMethod, Constructor<Component>>;
5
+ export type { Component };
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Components = void 0;
4
+ const Path_1 = require("./Path");
5
+ const IP_1 = require("./IP");
6
+ exports.Components = {
7
+ ip: IP_1.IP,
8
+ path: Path_1.Path
9
+ };
10
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../../source/directives/io/lib/throttle/components/index.ts"],"names":[],"mappings":";;;AAAA,iCAA6B;AAC7B,6BAAyB;AAMZ,QAAA,UAAU,GAAuD;IAC5E,EAAE,EAAE,OAAE;IACN,IAAI,EAAE,WAAI;CACX,CAAA"}
@@ -0,0 +1,4 @@
1
+ import type { Input, Output } from '../../../../../io';
2
+ export interface Condition {
3
+ match: (input: Input, output: Output) => boolean;
4
+ }
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=Condition.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Condition.js","sourceRoot":"","sources":["../../../../../../source/directives/io/lib/throttle/conditions/Condition.ts"],"names":[],"mappings":""}
@@ -0,0 +1,7 @@
1
+ import type { Input, Output } from '../../../../../io';
2
+ import type { Condition } from './Condition';
3
+ export declare class Status implements Condition {
4
+ private readonly status;
5
+ constructor(status: unknown);
6
+ match(input: Input, output: Output): boolean;
7
+ }
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.Status = void 0;
7
+ const node_assert_1 = __importDefault(require("node:assert"));
8
+ class Status {
9
+ status;
10
+ constructor(status) {
11
+ node_assert_1.default.ok(typeof status === 'number', 'Status must be a number');
12
+ this.status = status;
13
+ }
14
+ match(input, output) {
15
+ return output?.status === this.status;
16
+ }
17
+ }
18
+ exports.Status = Status;
19
+ //# sourceMappingURL=Status.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Status.js","sourceRoot":"","sources":["../../../../../../source/directives/io/lib/throttle/conditions/Status.ts"],"names":[],"mappings":";;;;;;AAAA,8DAAgC;AAIhC,MAAa,MAAM;IACA,MAAM,CAAQ;IAE/B,YAAoB,MAAe;QACjC,qBAAM,CAAC,EAAE,CAAC,OAAO,MAAM,KAAK,QAAQ,EAAE,yBAAyB,CAAC,CAAA;QAEhE,IAAI,CAAC,MAAM,GAAG,MAAM,CAAA;IACtB,CAAC;IAEM,KAAK,CAAE,KAAY,EAAE,MAAc;QACxC,OAAO,MAAM,EAAE,MAAM,KAAK,IAAI,CAAC,MAAM,CAAA;IACvC,CAAC;CACF;AAZD,wBAYC"}
@@ -0,0 +1,5 @@
1
+ import type { KeyConditionMethod } from '../Configuration';
2
+ import type { Condition } from './Condition';
3
+ type Constructor<T> = new (options: unknown) => T;
4
+ export declare const Conditions: Record<KeyConditionMethod, Constructor<Condition>>;
5
+ export type { Condition };