@go-mailer/jarvis 10.4.2 → 10.5.0

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/index.js CHANGED
@@ -10,6 +10,8 @@ const { RequestLogger, ProcessLogger } = require('./lib/middlewares/logger')
10
10
  const Utility = require('./lib/utilitiy/index')
11
11
  const { HTTPCacheManager } = require('./lib/middlewares/cache/manager')
12
12
  const { AutoHealer } = require('./lib/middlewares/healer')
13
+ const { localCache } = require('./lib/redis/cache')
14
+ const { QueueThrottler } = require('./lib/queue/throttler')
13
15
 
14
16
  module.exports = {
15
17
  APIClient,
@@ -20,8 +22,10 @@ module.exports = {
20
22
  FeatureFlag,
21
23
  HTTP,
22
24
  HTTPCacheManager,
25
+ localCache,
23
26
  ProcessLogger,
24
27
  QueryBuilder,
28
+ QueueThrottler,
25
29
  Redis,
26
30
  RequestLogger,
27
31
  Utility
@@ -120,8 +120,8 @@ const authorizeUser = ({ action, resource }) => {
120
120
  const has_authority = localCache.get_item(key)
121
121
  if (has_authority == null) {
122
122
  const is_permitted = await checkAuthority({ action, resource, tenant_id, user_id })
123
- localCache.add_item(key, is_permitted, 15 * 60)
124
- } else if (has_authority === false){
123
+ localCache.add_item(key, is_permitted, 15 * 60)
124
+ } else if (has_authority === false) {
125
125
  throw new Error('Unauthorized')
126
126
  }
127
127
 
@@ -1,104 +1,104 @@
1
- const { EntityId } = require("redis-om");
2
- const { RedisClient } = require("../../redis");
3
- const { HTTPCache } = require("./schema");
1
+ const { EntityId } = require('redis-om')
2
+ const { RedisClient } = require('../../redis')
3
+ const { HTTPCache } = require('./schema')
4
4
 
5
5
  const HTTPCacheManager = () => {
6
6
  const clearCache = async (request, response, next) => {
7
- const { tenant_id, cache_resource } = request;
8
- if (isNaN(tenant_id)) return next();
7
+ const { tenant_id, cache_resource } = request
8
+ if (isNaN(tenant_id)) return next()
9
9
 
10
- const repo = await RedisClient.register_schema(HTTPCache);
10
+ const repo = await RedisClient.register_schema(HTTPCache)
11
11
  const cached_records = await repo
12
12
  .search()
13
- .where("tenant_id")
13
+ .where('tenant_id')
14
14
  .equals(Number(tenant_id))
15
- .where("resource")
15
+ .where('resource')
16
16
  .equals(cache_resource)
17
- .returnAll();
17
+ .returnAll()
18
18
  for (const cached_record of cached_records) {
19
- await repo.remove(cached_record[EntityId]);
19
+ await repo.remove(cached_record[EntityId])
20
20
  }
21
- next();
22
- };
21
+ next()
22
+ }
23
23
 
24
24
  const clearCacheByResource = async (tenant_id, cache_resource) => {
25
- const repo = await RedisClient.register_schema(HTTPCache);
25
+ const repo = await RedisClient.register_schema(HTTPCache)
26
26
  const cached_records = await repo
27
27
  .search()
28
- .where("tenant_id")
28
+ .where('tenant_id')
29
29
  .equals(Number(tenant_id))
30
- .where("resource")
30
+ .where('resource')
31
31
  .equals(cache_resource)
32
- .returnAll();
32
+ .returnAll()
33
33
  for (const cached_record of cached_records) {
34
- await repo.remove(cached_record[EntityId]);
34
+ await repo.remove(cached_record[EntityId])
35
35
  }
36
- };
36
+ }
37
37
 
38
38
  const handleRequest = async (request, response, next) => {
39
- const { tenant_id, cache_key } = request;
40
- if (isNaN(tenant_id)) return next();
39
+ const { tenant_id, cache_key } = request
40
+ if (isNaN(tenant_id)) return next()
41
41
 
42
- const repo = await RedisClient.register_schema(HTTPCache);
43
- let cached_record = await repo
42
+ const repo = await RedisClient.register_schema(HTTPCache)
43
+ const cached_record = await repo
44
44
  .search()
45
- .where("tenant_id")
45
+ .where('tenant_id')
46
46
  .equals(Number(tenant_id))
47
- .where("key")
47
+ .where('key')
48
48
  .equals(cache_key)
49
- .returnFirst();
49
+ .returnFirst()
50
50
 
51
- let payload = {};
51
+ let payload = {}
52
52
  if (cached_record) {
53
- payload = JSON.parse(cached_record.data);
54
- return response.status(payload.status_code).json(payload);
53
+ payload = JSON.parse(cached_record.data)
54
+ return response.status(payload.status_code).json(payload)
55
55
  }
56
56
 
57
- next();
58
- };
57
+ next()
58
+ }
59
59
 
60
60
  const save = async (request, __, next) => {
61
- const { tenant_id, cache_key, cache_resource, payload } = request;
62
- if (isNaN(tenant_id)) return next();
61
+ const { tenant_id, cache_key, cache_resource, payload } = request
62
+ if (isNaN(tenant_id)) return next()
63
63
 
64
64
  if (payload.status_code >= 200 && payload.status_code < 300) {
65
- const repo = await RedisClient.register_schema(HTTPCache);
65
+ const repo = await RedisClient.register_schema(HTTPCache)
66
66
 
67
67
  let cached_record = await repo
68
68
  .search()
69
- .where("tenant_id")
69
+ .where('tenant_id')
70
70
  .equals(Number(tenant_id))
71
- .where("key")
71
+ .where('key')
72
72
  .equals(cache_key)
73
- .returnFirst();
73
+ .returnFirst()
74
74
 
75
75
  const data_to_cache = {
76
76
  tenant_id: Number(tenant_id),
77
77
  key: cache_key,
78
78
  resource: cache_resource,
79
- data: JSON.stringify(payload),
80
- };
79
+ data: JSON.stringify(payload)
80
+ }
81
81
 
82
82
  if (cached_record) {
83
- await repo.save(cached_record[EntityId], data_to_cache);
83
+ await repo.save(cached_record[EntityId], data_to_cache)
84
84
  } else {
85
- cached_record = await repo.save(data_to_cache);
86
- await repo.expire(cached_record[EntityId], 10 * 60);
85
+ cached_record = await repo.save(data_to_cache)
86
+ await repo.expire(cached_record[EntityId], 10 * 60)
87
87
  }
88
88
  }
89
89
 
90
- next();
91
- };
90
+ next()
91
+ }
92
92
 
93
93
  const setup = (request, __, next) => {
94
- const { _parsedUrl, body } = request;
95
- const resource = _parsedUrl.pathname.split("/")[1];
96
- request.cache_key = request.originalUrl + "&" + JSON.stringify(body);
97
- request.cache_resource = resource;
98
- next();
99
- };
94
+ const { _parsedUrl, body } = request
95
+ const resource = _parsedUrl.pathname.split('/')[1]
96
+ request.cache_key = request.originalUrl + '&' + JSON.stringify(body)
97
+ request.cache_resource = resource
98
+ next()
99
+ }
100
100
 
101
- return { clearCache, clearCacheByResource, handleRequest, save, setup };
102
- };
101
+ return { clearCache, clearCacheByResource, handleRequest, save, setup }
102
+ }
103
103
 
104
- module.exports = { HTTPCacheManager };
104
+ module.exports = { HTTPCacheManager }
@@ -1,12 +1,12 @@
1
- const { Schema } = require("redis-om");
1
+ const { Schema } = require('redis-om')
2
2
 
3
- const HTTPCache = new Schema("httpcache", {
4
- data: { type: "string" },
5
- key: { type: "string" },
6
- resource: { type: "string" },
7
- tenant_id: { type: "number" },
8
- });
3
+ const HTTPCache = new Schema('httpcache', {
4
+ data: { type: 'string' },
5
+ key: { type: 'string' },
6
+ resource: { type: 'string' },
7
+ tenant_id: { type: 'number' }
8
+ })
9
9
 
10
10
  module.exports = {
11
- HTTPCache,
12
- };
11
+ HTTPCache
12
+ }
@@ -1,12 +1,7 @@
1
- const cron = require("node-cron");
1
+ const cron = require('node-cron')
2
2
 
3
- function AutoHealer(timer = "******", handler = () => {}) {
4
- const execute = async (request, response, next) => {
5
- cron.schedule(timer, () => handler(...handler.arguments, request, response));
6
- next();
7
- };
8
-
9
- return execute;
3
+ function AutoHealer (timer = '* * * * * *', handler = () => {}) {
4
+ cron.schedule(timer, handler)
10
5
  }
11
6
 
12
- module.exports = { AutoHealer };
7
+ module.exports = { AutoHealer }
@@ -0,0 +1,29 @@
1
+ class QueueThrottler {
2
+ constructor (concurrency = 25) {
3
+ this.concurrency = concurrency
4
+ this.isEmpty = true
5
+ this.tasks = new Map()
6
+ }
7
+
8
+ addTask (key, value) {
9
+ if (!key || !value || this.tasks.size === this.concurrency || this.tasks.has(key)) return
10
+
11
+ this.tasks.set(key, value)
12
+ this.isEmpty = false
13
+ }
14
+
15
+ getTask (key) {
16
+ return this.tasks.get(key)
17
+ }
18
+
19
+ getTaskCount () {
20
+ return this.tasks.size
21
+ }
22
+
23
+ removeTask (key) {
24
+ this.tasks.delete(key)
25
+ this.isEmpty = this.tasks.size === 0
26
+ }
27
+ }
28
+
29
+ module.exports = { QueueThrottler }
@@ -3,13 +3,13 @@ class LocalCache {
3
3
  this.cache = new Map()
4
4
  }
5
5
 
6
- add_item (key='', value, duration) {
6
+ add_item (key = '', value, duration) {
7
7
  if (!key || !value) return
8
8
  this.cache.set(key, value)
9
9
  this.expire_item(key, duration)
10
10
  }
11
11
 
12
- expire_item(key, duration = 5) {
12
+ expire_item (key, duration = 5) {
13
13
  setTimeout(() => {
14
14
  this.cache.delete(key)
15
15
  }, duration * 1000)
@@ -20,12 +20,12 @@ class LocalCache {
20
20
  return this.cache.get(key)
21
21
  }
22
22
 
23
- remove_item(key) {
23
+ remove_item (key) {
24
24
  if (!key || !this.cache.has(key)) return
25
25
  this.cache.delete(key)
26
26
  }
27
27
  }
28
28
 
29
29
  module.exports = {
30
- localCache: new LocalCache()
31
- }
30
+ localCache: new LocalCache()
31
+ }
@@ -2,7 +2,7 @@ const { Schema } = require('redis-om')
2
2
 
3
3
  const DraftSchema = new Schema('draft', {
4
4
  id: { type: 'number' },
5
- data: { type: 'string' },
5
+ data: { type: 'string' }
6
6
  })
7
7
 
8
8
  module.exports = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@go-mailer/jarvis",
3
- "version": "10.4.2",
3
+ "version": "10.5.0",
4
4
  "main": "index.js",
5
5
  "repository": "git@github.com:go-mailer-ltd/jarvis-node.git",
6
6
  "author": "Nathan Oguntuberu <nateoguns.work@gmail.com>",
@@ -11,14 +11,14 @@
11
11
  },
12
12
  "dependencies": {
13
13
  "@logtail/node": "^0.3.3",
14
- "axios": "^1.3.4",
15
- "dotenv": "^16.0.3",
14
+ "axios": "^1.11.0",
15
+ "dotenv": "^17.2.2",
16
16
  "geoip-lite": "^1.4.10",
17
17
  "jsonwebtoken": "^9.0.0",
18
18
  "node-cron": "^4.2.1",
19
19
  "node-device-detector": "^2.1.1",
20
- "redis": "^4.6.12",
21
- "redis-om": "^0.4.3",
20
+ "redis": "^5.8.2",
21
+ "redis-om": "^0.4.7",
22
22
  "request-ip": "^3.3.0"
23
23
  },
24
24
  "devDependencies": {