express-rate-limit 8.4.0 → 8.4.1

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.cjs CHANGED
@@ -201,6 +201,16 @@ var MemoryStore = class {
201
201
  // source/rate-limit.ts
202
202
  var import_node_net3 = require("node:net");
203
203
 
204
+ // source/console-logger.ts
205
+ var ConsoleLogger = {
206
+ warn(...args) {
207
+ console.warn(...args.reverse());
208
+ },
209
+ error(...args) {
210
+ console.error(...args.reverse());
211
+ }
212
+ };
213
+
204
214
  // source/headers.ts
205
215
  var import_node_buffer = require("node:buffer");
206
216
  var import_node_crypto = require("node:crypto");
@@ -548,7 +558,8 @@ var validations = {
548
558
  validate: true,
549
559
  headers: true,
550
560
  max: true,
551
- passOnStoreError: true
561
+ passOnStoreError: true,
562
+ logger: true
552
563
  };
553
564
  const validOptions = Object.keys(optionsMap).concat(
554
565
  "draft_polli_ratelimit_headers",
@@ -661,7 +672,15 @@ var validations = {
661
672
  }
662
673
  }
663
674
  };
664
- var getValidations = (_enabled) => {
675
+ function validateLogger(logger) {
676
+ if (typeof logger !== "object" || typeof logger.error !== "function" || typeof logger.warn !== "function") {
677
+ throw new TypeError(
678
+ "Provided logger does not implement the Logger interface"
679
+ );
680
+ }
681
+ }
682
+ var getValidations = (_enabled, logger) => {
683
+ validateLogger(logger);
665
684
  let enabled;
666
685
  if (typeof _enabled === "boolean") {
667
686
  enabled = {
@@ -687,8 +706,8 @@ var getValidations = (_enabled) => {
687
706
  args
688
707
  );
689
708
  } catch (error) {
690
- if (error instanceof ChangeWarning) console.warn(error);
691
- else console.error(error);
709
+ if (error instanceof ChangeWarning) logger.warn(error);
710
+ else logger.error(error);
692
711
  }
693
712
  };
694
713
  }
@@ -741,7 +760,11 @@ var getOptionsFromConfig = (config) => {
741
760
  };
742
761
  var parseOptions = (passedOptions) => {
743
762
  const notUndefinedOptions = omitUndefinedProperties(passedOptions);
744
- const validations2 = getValidations(notUndefinedOptions?.validate ?? true);
763
+ const logger = passedOptions.logger ?? ConsoleLogger;
764
+ const validations2 = getValidations(
765
+ notUndefinedOptions?.validate ?? true,
766
+ logger
767
+ );
745
768
  validations2.validationsConfig();
746
769
  validations2.knownOptions(passedOptions);
747
770
  validations2.draftPolliHeaders(
@@ -816,7 +839,8 @@ var parseOptions = (passedOptions) => {
816
839
  notUndefinedOptions.store ?? new MemoryStore(validations2)
817
840
  ),
818
841
  // Print an error to the console if a few known misconfigurations are detected.
819
- validations: validations2
842
+ validations: validations2,
843
+ logger
820
844
  };
821
845
  if (typeof config.store.increment !== "function" || typeof config.store.decrement !== "function" || typeof config.store.resetKey !== "function" || config.store.resetAll !== void 0 && typeof config.store.resetAll !== "function" || config.store.init !== void 0 && typeof config.store.init !== "function") {
822
846
  throw new TypeError(
@@ -858,9 +882,9 @@ var rateLimit = (passedOptions) => {
858
882
  resetTime = incrementResult.resetTime;
859
883
  } catch (error) {
860
884
  if (config.passOnStoreError) {
861
- console.error(
862
- "express-rate-limit: error from store, allowing request without rate-limiting.",
863
- error
885
+ config.logger.error(
886
+ error,
887
+ "express-rate-limit: error from store, allowing request without rate-limiting."
864
888
  );
865
889
  next();
866
890
  return;
package/dist/index.d.cts CHANGED
@@ -159,6 +159,26 @@ declare const validations: {
159
159
  windowMs(windowMs: number): void;
160
160
  };
161
161
  export type Validations = typeof validations;
162
+ /**
163
+ * Basic logging function
164
+ *
165
+ * @param error {unknown} - The error to log
166
+ * @param message {string | undefined} - Additional details about the error
167
+ */
168
+ export type LoggerFn = (error: unknown, message?: string) => void;
169
+ /**
170
+ * Minimal interface for logging warnings and errors
171
+ */
172
+ export type Logger = {
173
+ /**
174
+ * Function to log an error
175
+ */
176
+ error: LoggerFn;
177
+ /**
178
+ * Function to log a warning
179
+ */
180
+ warn: LoggerFn;
181
+ };
162
182
  /**
163
183
  * Callback that fires when a client's hit counter is incremented.
164
184
  *
@@ -477,6 +497,10 @@ export type Options = {
477
497
  * If the Store generates an error, allow the request to pass.
478
498
  */
479
499
  passOnStoreError: boolean;
500
+ /**
501
+ * The logger to use to log errors. If absent, logs to the console.
502
+ */
503
+ logger: Logger;
480
504
  };
481
505
  /**
482
506
  * The extended request object that includes information about the client's
package/dist/index.d.mts CHANGED
@@ -159,6 +159,26 @@ declare const validations: {
159
159
  windowMs(windowMs: number): void;
160
160
  };
161
161
  export type Validations = typeof validations;
162
+ /**
163
+ * Basic logging function
164
+ *
165
+ * @param error {unknown} - The error to log
166
+ * @param message {string | undefined} - Additional details about the error
167
+ */
168
+ export type LoggerFn = (error: unknown, message?: string) => void;
169
+ /**
170
+ * Minimal interface for logging warnings and errors
171
+ */
172
+ export type Logger = {
173
+ /**
174
+ * Function to log an error
175
+ */
176
+ error: LoggerFn;
177
+ /**
178
+ * Function to log a warning
179
+ */
180
+ warn: LoggerFn;
181
+ };
162
182
  /**
163
183
  * Callback that fires when a client's hit counter is incremented.
164
184
  *
@@ -477,6 +497,10 @@ export type Options = {
477
497
  * If the Store generates an error, allow the request to pass.
478
498
  */
479
499
  passOnStoreError: boolean;
500
+ /**
501
+ * The logger to use to log errors. If absent, logs to the console.
502
+ */
503
+ logger: Logger;
480
504
  };
481
505
  /**
482
506
  * The extended request object that includes information about the client's
package/dist/index.d.ts CHANGED
@@ -159,6 +159,26 @@ declare const validations: {
159
159
  windowMs(windowMs: number): void;
160
160
  };
161
161
  export type Validations = typeof validations;
162
+ /**
163
+ * Basic logging function
164
+ *
165
+ * @param error {unknown} - The error to log
166
+ * @param message {string | undefined} - Additional details about the error
167
+ */
168
+ export type LoggerFn = (error: unknown, message?: string) => void;
169
+ /**
170
+ * Minimal interface for logging warnings and errors
171
+ */
172
+ export type Logger = {
173
+ /**
174
+ * Function to log an error
175
+ */
176
+ error: LoggerFn;
177
+ /**
178
+ * Function to log a warning
179
+ */
180
+ warn: LoggerFn;
181
+ };
162
182
  /**
163
183
  * Callback that fires when a client's hit counter is incremented.
164
184
  *
@@ -477,6 +497,10 @@ export type Options = {
477
497
  * If the Store generates an error, allow the request to pass.
478
498
  */
479
499
  passOnStoreError: boolean;
500
+ /**
501
+ * The logger to use to log errors. If absent, logs to the console.
502
+ */
503
+ logger: Logger;
480
504
  };
481
505
  /**
482
506
  * The extended request object that includes information about the client's
package/dist/index.mjs CHANGED
@@ -172,6 +172,16 @@ var MemoryStore = class {
172
172
  // source/rate-limit.ts
173
173
  import { isIPv6 as isIPv62 } from "node:net";
174
174
 
175
+ // source/console-logger.ts
176
+ var ConsoleLogger = {
177
+ warn(...args) {
178
+ console.warn(...args.reverse());
179
+ },
180
+ error(...args) {
181
+ console.error(...args.reverse());
182
+ }
183
+ };
184
+
175
185
  // source/headers.ts
176
186
  import { Buffer } from "node:buffer";
177
187
  import { createHash } from "node:crypto";
@@ -519,7 +529,8 @@ var validations = {
519
529
  validate: true,
520
530
  headers: true,
521
531
  max: true,
522
- passOnStoreError: true
532
+ passOnStoreError: true,
533
+ logger: true
523
534
  };
524
535
  const validOptions = Object.keys(optionsMap).concat(
525
536
  "draft_polli_ratelimit_headers",
@@ -632,7 +643,15 @@ var validations = {
632
643
  }
633
644
  }
634
645
  };
635
- var getValidations = (_enabled) => {
646
+ function validateLogger(logger) {
647
+ if (typeof logger !== "object" || typeof logger.error !== "function" || typeof logger.warn !== "function") {
648
+ throw new TypeError(
649
+ "Provided logger does not implement the Logger interface"
650
+ );
651
+ }
652
+ }
653
+ var getValidations = (_enabled, logger) => {
654
+ validateLogger(logger);
636
655
  let enabled;
637
656
  if (typeof _enabled === "boolean") {
638
657
  enabled = {
@@ -658,8 +677,8 @@ var getValidations = (_enabled) => {
658
677
  args
659
678
  );
660
679
  } catch (error) {
661
- if (error instanceof ChangeWarning) console.warn(error);
662
- else console.error(error);
680
+ if (error instanceof ChangeWarning) logger.warn(error);
681
+ else logger.error(error);
663
682
  }
664
683
  };
665
684
  }
@@ -712,7 +731,11 @@ var getOptionsFromConfig = (config) => {
712
731
  };
713
732
  var parseOptions = (passedOptions) => {
714
733
  const notUndefinedOptions = omitUndefinedProperties(passedOptions);
715
- const validations2 = getValidations(notUndefinedOptions?.validate ?? true);
734
+ const logger = passedOptions.logger ?? ConsoleLogger;
735
+ const validations2 = getValidations(
736
+ notUndefinedOptions?.validate ?? true,
737
+ logger
738
+ );
716
739
  validations2.validationsConfig();
717
740
  validations2.knownOptions(passedOptions);
718
741
  validations2.draftPolliHeaders(
@@ -787,7 +810,8 @@ var parseOptions = (passedOptions) => {
787
810
  notUndefinedOptions.store ?? new MemoryStore(validations2)
788
811
  ),
789
812
  // Print an error to the console if a few known misconfigurations are detected.
790
- validations: validations2
813
+ validations: validations2,
814
+ logger
791
815
  };
792
816
  if (typeof config.store.increment !== "function" || typeof config.store.decrement !== "function" || typeof config.store.resetKey !== "function" || config.store.resetAll !== void 0 && typeof config.store.resetAll !== "function" || config.store.init !== void 0 && typeof config.store.init !== "function") {
793
817
  throw new TypeError(
@@ -829,9 +853,9 @@ var rateLimit = (passedOptions) => {
829
853
  resetTime = incrementResult.resetTime;
830
854
  } catch (error) {
831
855
  if (config.passOnStoreError) {
832
- console.error(
833
- "express-rate-limit: error from store, allowing request without rate-limiting.",
834
- error
856
+ config.logger.error(
857
+ error,
858
+ "express-rate-limit: error from store, allowing request without rate-limiting."
835
859
  );
836
860
  next();
837
861
  return;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "express-rate-limit",
3
- "version": "8.4.0",
3
+ "version": "8.4.1",
4
4
  "description": "Basic IP rate-limiting middleware for Express. Use to limit repeated requests to public APIs and/or endpoints such as password reset.",
5
5
  "author": {
6
6
  "name": "Nathan Friedly",