mielk-api 1.5.2 → 1.5.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.
@@ -12,6 +12,7 @@ import { failure, HttpResponseStatus } from '../../http/index.js';
12
12
  import { RateLimitConfigFactory } from './rateLimitConfigFactory.js';
13
13
  import { generateRedisLockKey } from '../redis/redisLockChecker.js';
14
14
  import { Msg } from '../../internal/messaging/messageTags.js';
15
+ import { generateRedisKey } from '../redis/redisKeysFactory.js';
15
16
  export const createGlobalRateLimiter = (config) => {
16
17
  return createRateLimiter(RateLimitConfigFactory.globalIp(config));
17
18
  };
@@ -23,8 +24,8 @@ export const createRateLimiter = (config) => {
23
24
  res.setHeader('X-RateLimit-Limit', limit);
24
25
  res.setHeader('X-RateLimit-Remaining', Math.max(0, limit - currentCounter));
25
26
  res.setHeader('X-RateLimit-Reset', ttl);
26
- console.log('IP:', req.ip);
27
- console.log('Forwarded:', req.headers['x-forwarded-for']);
27
+ // console.log('IP:', req.ip);
28
+ // console.log('Forwarded:', req.headers['x-forwarded-for']);
28
29
  if (!allowed) {
29
30
  return failure(res, HttpResponseStatus.TOO_MANY_REQUESTS, message || Msg.apiStatus.tooManyRequests);
30
31
  }
@@ -32,8 +33,8 @@ export const createRateLimiter = (config) => {
32
33
  });
33
34
  };
34
35
  const createRateLimitFromConfig = (config, req) => {
35
- const { limit, windowSec, message, redisLockerConfig } = config;
36
- const key = generateKeyFromConfig(config, req);
36
+ const { keyPrefix, keySuffixes, limit, windowSec, message, redisLockerConfig } = config;
37
+ const key = generateRedisKey(keyPrefix, keySuffixes, req);
37
38
  const rateLimit = { key, limit, windowSec, message };
38
39
  if (redisLockerConfig) {
39
40
  const { keyPrefix, ttlSeconds } = redisLockerConfig;
@@ -44,7 +45,3 @@ const createRateLimitFromConfig = (config, req) => {
44
45
  }
45
46
  return rateLimit;
46
47
  };
47
- const generateKeyFromConfig = (config, req) => {
48
- const { keyPrefix, keySuffixes } = config;
49
- return `${keyPrefix}:${keySuffixes(req).join(':')}`;
50
- };
@@ -1,7 +1,7 @@
1
1
  import { RedisKeyFactory } from "../redis/redisKeysFactory.js";
2
2
  import { Msg } from "../../internal/messaging/messageTags.js";
3
3
  const defaultRateLimitConfig = {
4
- keyPrefix: RedisKeyFactory.prefixes.login,
4
+ keyPrefix: RedisKeyFactory.prefixes.authLogin,
5
5
  keySuffixes: RedisKeyFactory.suffixes.ip,
6
6
  windowSec: 15 * 60,
7
7
  limit: 5,
@@ -11,7 +11,7 @@ const createRateLimitConfig = (params) => {
11
11
  return Object.assign(Object.assign({}, defaultRateLimitConfig), params);
12
12
  };
13
13
  const globalIp = (params) => {
14
- return createRateLimitConfig(Object.assign({ keyPrefix: RedisKeyFactory.prefixes.global, windowSec: 60, limit: 2 }, (params || {})));
14
+ return createRateLimitConfig(Object.assign({ keyPrefix: RedisKeyFactory.prefixes.authGlobal, windowSec: 60, limit: 2 }, (params || {})));
15
15
  };
16
16
  const loginIp = (params) => {
17
17
  return createRateLimitConfig(Object.assign({}, (params || {})));
@@ -20,7 +20,7 @@ const loginIpMail = (params) => {
20
20
  return createRateLimitConfig(Object.assign({ keySuffixes: RedisKeyFactory.suffixes.ipMail }, (params || {})));
21
21
  };
22
22
  const loginMail = (params) => {
23
- return createRateLimitConfig(Object.assign({ keySuffixes: RedisKeyFactory.suffixes.mail, redisLockerConfig: { keyPrefix: RedisKeyFactory.prefixes.lockEmail, ttlSeconds: 15 * 60 } }, (params || {})));
23
+ return createRateLimitConfig(Object.assign({ keySuffixes: RedisKeyFactory.suffixes.mail, redisLockerConfig: { keyPrefix: RedisKeyFactory.prefixes.authLockEmail, ttlSeconds: 15 * 60 } }, (params || {})));
24
24
  };
25
25
  export const RateLimitConfigFactory = {
26
26
  globalIp,
@@ -1,10 +1,10 @@
1
1
  import { Request } from 'express';
2
2
  export declare const RedisKeyFactory: {
3
3
  prefixes: {
4
- readonly global: "global";
5
- readonly login: "login";
6
- readonly lockEmail: "lock:email";
7
- readonly refresh: "refresh";
4
+ readonly authGlobal: "auth.global";
5
+ readonly authLogin: "auth.login";
6
+ readonly authLockEmail: "auth.lock:email";
7
+ readonly authRefresh: "auth.refresh";
8
8
  };
9
9
  suffixes: {
10
10
  readonly ip: (req: Request) => string[];
@@ -12,3 +12,4 @@ export declare const RedisKeyFactory: {
12
12
  readonly mail: (req: Request) => string[];
13
13
  };
14
14
  };
15
+ export declare const generateRedisKey: (prefix: string, suffixCallback: (req: Request) => string[], req: Request) => string;
@@ -1,8 +1,8 @@
1
1
  const prefixes = {
2
- global: 'global',
3
- login: 'login',
4
- lockEmail: 'lock:email',
5
- refresh: 'refresh',
2
+ authGlobal: 'auth.global',
3
+ authLogin: 'auth.login',
4
+ authLockEmail: 'auth.lock:email',
5
+ authRefresh: 'auth.refresh',
6
6
  };
7
7
  const getIpSuffixCallback = (req) => {
8
8
  const ip = req.ip || 'undefined';
@@ -28,3 +28,6 @@ export const RedisKeyFactory = {
28
28
  prefixes: prefixes,
29
29
  suffixes: suffixCallbacks
30
30
  };
31
+ export const generateRedisKey = (prefix, suffixCallback, req) => {
32
+ return `${prefix}:${suffixCallback(req).join(':')}`;
33
+ };
@@ -1,7 +1,7 @@
1
1
  import { RedisKeyFactory } from "./redisKeysFactory.js";
2
2
  import { Msg } from "../../internal/messaging/messageTags.js";
3
3
  const defaultRedisLockCheckerConfig = {
4
- keyPrefix: RedisKeyFactory.prefixes.lockEmail,
4
+ keyPrefix: RedisKeyFactory.prefixes.authLockEmail,
5
5
  keySuffixes: RedisKeyFactory.suffixes.mail,
6
6
  message: Msg.apiStatus.locked,
7
7
  };
@@ -1,6 +1,6 @@
1
1
  import { RedisKeyFactory } from "./redisKeysFactory.js";
2
2
  const defaultRedisLockerConfig = {
3
- keyPrefix: RedisKeyFactory.prefixes.lockEmail,
3
+ keyPrefix: RedisKeyFactory.prefixes.authLockEmail,
4
4
  ttlSeconds: 30 * 60,
5
5
  };
6
6
  const createRedisLockerConfig = (params) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mielk-api",
3
- "version": "1.5.2",
3
+ "version": "1.5.4",
4
4
  "keywords": [],
5
5
  "author": "mielk",
6
6
  "description": "Wrapper for API operations",