@candlerip/shared 0.0.85 → 0.0.87

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 (29) hide show
  1. package/cache/common/index.d.ts +0 -1
  2. package/cache/common/index.js +0 -1
  3. package/cache/redis/workers/index.js +8 -12
  4. package/cache/redis/workers/type.d.ts +0 -1
  5. package/message-broker/index.d.ts +2 -0
  6. package/message-broker/index.js +2 -0
  7. package/message-broker/message-broker/domains.d.ts +9 -0
  8. package/message-broker/message-broker/index.d.ts +2 -0
  9. package/message-broker/message-broker/index.js +2 -0
  10. package/message-broker/message-broker/singletons/base/index.d.ts +12 -0
  11. package/message-broker/message-broker/singletons/base/index.js +54 -0
  12. package/message-broker/message-broker/singletons/base/type.d.ts +8 -0
  13. package/message-broker/message-broker/singletons/base/type.js +1 -0
  14. package/message-broker/message-broker/singletons/index.d.ts +6 -0
  15. package/message-broker/message-broker/singletons/index.js +42 -0
  16. package/message-broker/message-broker/singletons/type.d.ts +4 -0
  17. package/message-broker/message-broker/singletons/type.js +1 -0
  18. package/message-broker/message-broker-queue-name/domains.d.ts +2 -0
  19. package/package.json +7 -3
  20. package/cache/common/cache-service/index.d.ts +0 -2
  21. package/cache/common/cache-service/index.js +0 -2
  22. package/cache/common/cache-service/refresh-cache/domains.d.ts +0 -4
  23. package/cache/common/cache-service/workers/index.d.ts +0 -2
  24. package/cache/common/cache-service/workers/index.js +0 -39
  25. package/cache/common/cache-service/workers/type.d.ts +0 -14
  26. /package/{cache/common/cache-service/workers/type.js → message-broker/message-broker/domains.js} +0 -0
  27. /package/{cache/common/cache-service/refresh-cache → message-broker/message-broker-queue-name}/domains.js +0 -0
  28. /package/{cache/common/cache-service/refresh-cache → message-broker/message-broker-queue-name}/index.d.ts +0 -0
  29. /package/{cache/common/cache-service/refresh-cache → message-broker/message-broker-queue-name}/index.js +0 -0
@@ -1,2 +1 @@
1
1
  export * from './cache-key/index.js';
2
- export * from './cache-service/index.js';
@@ -1,2 +1 @@
1
1
  export * from './cache-key/index.js';
2
- export * from './cache-service/index.js';
@@ -18,7 +18,6 @@ export const CacheWorker = (environmentMode, url, password, options) => {
18
18
  resetCustomError();
19
19
  });
20
20
  _client.connect();
21
- const _composeKey = (key) => `${environmentMode}:${key}`;
22
21
  const _getClient = () => {
23
22
  const resp = getCustomError();
24
23
  if (resp) {
@@ -39,15 +38,14 @@ export const CacheWorker = (environmentMode, url, password, options) => {
39
38
  };
40
39
  };
41
40
  const getCache = async (key) => {
42
- const redisKey = _composeKey(key);
43
- const { composeCustomError } = CustomErrorWorker({ info: { redisKey } });
41
+ const { composeCustomError } = CustomErrorWorker({ info: { key } });
44
42
  const { data: client, customError } = _getClient();
45
43
  if (customError) {
46
44
  return { customError };
47
45
  }
48
46
  let data;
49
47
  try {
50
- const cache = await client.get(redisKey);
48
+ const cache = await client.get(key);
51
49
  if (cache) {
52
50
  data = JSON.parse(cache);
53
51
  }
@@ -67,8 +65,7 @@ export const CacheWorker = (environmentMode, url, password, options) => {
67
65
  };
68
66
  };
69
67
  const mGetCache = async (keys) => {
70
- const redisKeys = keys.map((it) => _composeKey(it));
71
- const { composeCustomError } = CustomErrorWorker({ info: { redisKeys } });
68
+ const { composeCustomError } = CustomErrorWorker({ info: { keys } });
72
69
  const { data: client, customError } = _getClient();
73
70
  if (customError) {
74
71
  return { customError };
@@ -76,14 +73,14 @@ export const CacheWorker = (environmentMode, url, password, options) => {
76
73
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
77
74
  const data = {};
78
75
  try {
79
- const cache = await client.mGet(redisKeys);
76
+ const cache = await client.mGet(keys);
80
77
  if (!isArray(cache, isString)) {
81
78
  return {
82
79
  customError: composeCustomError('Error get data from redis', { cache }),
83
80
  };
84
81
  }
85
82
  cache.forEach((it, i) => {
86
- data[redisKeys[i]] = JSON.parse(it);
83
+ data[keys[i]] = JSON.parse(it);
87
84
  });
88
85
  }
89
86
  catch (err) {
@@ -91,7 +88,7 @@ export const CacheWorker = (environmentMode, url, password, options) => {
91
88
  customError: composeCustomError('Error get data from redis', { err }),
92
89
  };
93
90
  }
94
- if (Object.keys(data).length !== redisKeys.length) {
91
+ if (Object.keys(data).length !== keys.length) {
95
92
  return {
96
93
  customError: composeCustomError('Missing data from redis', { data }),
97
94
  };
@@ -101,14 +98,13 @@ export const CacheWorker = (environmentMode, url, password, options) => {
101
98
  };
102
99
  };
103
100
  const setCache = async (key, data) => {
104
- const redisKey = _composeKey(key);
105
- const { composeCustomError } = CustomErrorWorker({ info: { redisKey } });
101
+ const { composeCustomError } = CustomErrorWorker({ info: { key } });
106
102
  const { data: client, customError } = _getClient();
107
103
  if (customError) {
108
104
  return { customError };
109
105
  }
110
106
  try {
111
- await client.set(redisKey, JSON.stringify(data));
107
+ await client.set(key, JSON.stringify(data));
112
108
  }
113
109
  catch (err) {
114
110
  return {
@@ -9,7 +9,6 @@ export type TCacheWorker = (environmentMode: EnvironmentMode, url: string, passw
9
9
  mGetCache: MGetCache;
10
10
  setCache: SetCache;
11
11
  };
12
- export type ComposeKey = (key: CacheKey) => string;
13
12
  export type GetCache = <T extends CacheKey = never, D extends Cache[T] = Cache[T]>(key: T) => Promise<TCustomError<D>>;
14
13
  export type MGetCache = <T extends CacheKey = never, D extends Pick<Cache, T> = Pick<Cache, T>>(keys: T[]) => Promise<TCustomError<D>>;
15
14
  export type SetCache = <T extends CacheKey = never>(key: T, data: Cache[T]) => Promise<TCustomError<null>>;
@@ -0,0 +1,2 @@
1
+ export * from './message-broker/index.js';
2
+ export * from './message-broker-queue-name/index.js';
@@ -0,0 +1,2 @@
1
+ export * from './message-broker/index.js';
2
+ export * from './message-broker-queue-name/index.js';
@@ -0,0 +1,9 @@
1
+ import { CacheKey } from '../../cache/common/index.js';
2
+ import { Page } from '../../common/index.js';
3
+ export type MessageBroker = {
4
+ 'cache-service:refresh-cache': {
5
+ cacheKeys?: CacheKey[];
6
+ pageDictionaries?: Page[];
7
+ pageServerData?: Array<'candles-page' | 'home-page' | 'persons-page' | 'persons-map-page'>;
8
+ } | 'full';
9
+ };
@@ -0,0 +1,2 @@
1
+ export * from './domains.js';
2
+ export * from './singletons/index.js';
@@ -0,0 +1,2 @@
1
+ export * from './domains.js';
2
+ export * from './singletons/index.js';
@@ -0,0 +1,12 @@
1
+ import { GetChannel, Init } from './type.js';
2
+ export declare const BaseMessageBrokerSingleton: {
3
+ errorWorker: {
4
+ addCustomErrorInfo: import("../../../../common/custom-error/workers/type.js").AddCustomErrorInfo;
5
+ addCustomErrorMessage: import("../../../../common/custom-error/workers/type.js").AddCustomErrorMessage;
6
+ composeCustomError: import("../../../../common/custom-error/workers/type.js").ComposeCustomError;
7
+ getCustomError: import("../../../../common/custom-error/workers/type.js").GetCustomError;
8
+ resetCustomError: import("../../../../common/custom-error/workers/type.js").ResetCustomError;
9
+ };
10
+ getChannel: GetChannel;
11
+ init: Init;
12
+ };
@@ -0,0 +1,54 @@
1
+ import amqp from 'amqplib';
2
+ import { CustomErrorWorker } from '../../../../common/index.js';
3
+ export const BaseMessageBrokerSingleton = (() => {
4
+ let _channel;
5
+ let _connection;
6
+ let _url;
7
+ const errorWorker = CustomErrorWorker();
8
+ const _handleError = (err) => {
9
+ errorWorker.addCustomErrorMessage('Error connect RabbitMQ');
10
+ errorWorker.addCustomErrorInfo({ err });
11
+ setTimeout(() => _connect(), 5000);
12
+ };
13
+ const _connect = async () => {
14
+ if (!_url) {
15
+ return;
16
+ }
17
+ try {
18
+ _connection = await amqp.connect(_url);
19
+ _connection.on('error', (err) => {
20
+ _handleError(err);
21
+ });
22
+ _channel = await _connection.createChannel();
23
+ console.info('Successfully connected to RabbitMQ!');
24
+ errorWorker.resetCustomError();
25
+ }
26
+ catch (err) {
27
+ _handleError(err);
28
+ }
29
+ };
30
+ const getChannel = () => {
31
+ if (!_channel) {
32
+ return {
33
+ customError: errorWorker.composeCustomError('MessageBrokerSingleton not initialized'),
34
+ };
35
+ }
36
+ return {
37
+ data: _channel,
38
+ };
39
+ };
40
+ const init = (url) => {
41
+ const error = errorWorker.getCustomError();
42
+ if (error) {
43
+ return error;
44
+ }
45
+ errorWorker.addCustomErrorMessage('MessageBroker initializing...');
46
+ _url = url;
47
+ _connect();
48
+ };
49
+ return {
50
+ errorWorker,
51
+ getChannel,
52
+ init,
53
+ };
54
+ })();
@@ -0,0 +1,8 @@
1
+ import amqp from 'amqplib';
2
+ import { CustomError, TCustomError } from '../../../../common/index.js';
3
+ export type _Connect = () => Promise<void>;
4
+ export type _HandleError = (err: unknown) => void;
5
+ export type GetChannel = () => TCustomError<amqp.Channel>;
6
+ export type Init = (url: string) => {
7
+ customError: CustomError;
8
+ } | undefined;
@@ -0,0 +1,6 @@
1
+ import { ListenQueue, SendMessage } from './type.js';
2
+ export declare const MessageBrokerSingleton: {
3
+ init: import("./base/type.js").Init;
4
+ listenQueue: ListenQueue;
5
+ sendMessage: SendMessage;
6
+ };
@@ -0,0 +1,42 @@
1
+ import { composeCustomError, consoleCustomError, stringify } from '../../../common/index.js';
2
+ import { BaseMessageBrokerSingleton } from './base/index.js';
3
+ export const MessageBrokerSingleton = (() => {
4
+ const { errorWorker, getChannel, init } = BaseMessageBrokerSingleton;
5
+ const listenQueue = async (queueName, onMessage) => {
6
+ errorWorker.addCustomErrorInfo({ queueName, onMessage });
7
+ const { data: channel, customError } = getChannel();
8
+ if (customError) {
9
+ setTimeout(() => listenQueue(queueName, onMessage), 5000);
10
+ return;
11
+ }
12
+ await channel.assertQueue(queueName, { durable: true });
13
+ channel.consume(queueName, (msg) => {
14
+ if (msg) {
15
+ let parsedMsg;
16
+ try {
17
+ parsedMsg = JSON.parse(msg.content.toString());
18
+ onMessage(parsedMsg);
19
+ }
20
+ catch (err) {
21
+ const error = composeCustomError('Error parsing message', { err, msg });
22
+ consoleCustomError(error);
23
+ }
24
+ channel.ack(msg);
25
+ }
26
+ });
27
+ };
28
+ const sendMessage = async (queueName, message) => {
29
+ errorWorker.addCustomErrorInfo({ message, queueName });
30
+ const { data: channel, customError } = getChannel();
31
+ if (customError) {
32
+ return;
33
+ }
34
+ await channel.assertQueue(queueName, { durable: true });
35
+ channel.sendToQueue(queueName, Buffer.from(stringify(message)));
36
+ };
37
+ return {
38
+ init,
39
+ listenQueue,
40
+ sendMessage,
41
+ };
42
+ })();
@@ -0,0 +1,4 @@
1
+ import { MessageBrokerQueueName } from '../../message-broker-queue-name/index.js';
2
+ import { MessageBroker } from '../domains.js';
3
+ export type ListenQueue = <T extends MessageBrokerQueueName>(queueName: T, onMessage: (message: MessageBroker[T]) => void) => Promise<void>;
4
+ export type SendMessage = <T extends MessageBrokerQueueName>(queueName: T, message: MessageBroker[T]) => Promise<void>;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,2 @@
1
+ import { MessageBroker } from '../message-broker/domains.js';
2
+ export type MessageBrokerQueueName = keyof MessageBroker;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@candlerip/shared",
3
- "version": "0.0.85",
3
+ "version": "0.0.87",
4
4
  "type": "module",
5
5
  "engines": {
6
6
  "node": "=22.19.0"
@@ -12,17 +12,21 @@
12
12
  "./aws/sdk": "./aws/sdk/index.js",
13
13
  "./cache/common": "./cache/common/index.js",
14
14
  "./cache/redis": "./cache/redis/index.js",
15
- "./database": "./database/index.js"
15
+ "./database": "./database/index.js",
16
+ "./message-broker": "./message-broker/index.js"
16
17
  },
17
18
  "scripts": {
18
- "dev": "crs-tsc-watch"
19
+ "dev": "crs-tsc-watch",
20
+ "publish-package": "bash ./_devops/publish-package.sh"
19
21
  },
20
22
  "devDependencies": {
21
23
  "@aws-sdk/client-ec2": "^3.938.0",
22
24
  "@aws-sdk/client-route-53": "^3.936.0",
23
25
  "@aws-sdk/client-sqs": "^3.936.0",
24
26
  "@candlerip/shared-devops": "^0.0.50",
27
+ "@types/amqplib": "^0.10.8",
25
28
  "@types/node": "^24.9.1",
29
+ "amqplib": "^0.10.9",
26
30
  "aws-cdk-lib": "^2.227.0",
27
31
  "eslint": "^9.39.0",
28
32
  "globals": "^16.5.0",
@@ -1,2 +0,0 @@
1
- export * from './refresh-cache/index.js';
2
- export * from './workers/index.js';
@@ -1,2 +0,0 @@
1
- export * from './refresh-cache/index.js';
2
- export * from './workers/index.js';
@@ -1,4 +0,0 @@
1
- import { CacheKey } from '../../cache-key/index.js';
2
- export type CacheServiceRefreshCache = {
3
- cacheKeys: CacheKey[];
4
- } | 'full';
@@ -1,2 +0,0 @@
1
- import { TCacheServiceSqsWorker } from './type.js';
2
- export declare const CacheServiceSqsWorker: TCacheServiceSqsWorker;
@@ -1,39 +0,0 @@
1
- import { SqsWorker } from '../../../../aws/sdk/index.js';
2
- import { LANGUAGES } from '../../../../common/index.js';
3
- export const CacheServiceSqsWorker = ({ awsRegion, environmentMode }) => {
4
- const _sqsWorker = SqsWorker({ awsRegion });
5
- const refreshCache = async (props) => {
6
- let refreshCache;
7
- if (!props) {
8
- refreshCache = 'full';
9
- }
10
- else {
11
- const { cacheKeys, pageDictionaries, pageServerData } = props;
12
- const updatableCacheKeys = [];
13
- if (cacheKeys) {
14
- updatableCacheKeys.forEach((it) => updatableCacheKeys.push(it));
15
- }
16
- if (pageDictionaries) {
17
- pageDictionaries.forEach((page) => {
18
- LANGUAGES.forEach((lang) => {
19
- updatableCacheKeys.push(`${page}-dictionary-${lang}`);
20
- });
21
- });
22
- }
23
- if (pageServerData) {
24
- pageServerData.forEach((page) => {
25
- LANGUAGES.forEach((lang) => {
26
- updatableCacheKeys.push(`${page}-server-data-${lang}`);
27
- });
28
- });
29
- }
30
- refreshCache = {
31
- cacheKeys: updatableCacheKeys,
32
- };
33
- }
34
- await _sqsWorker.sendSqsMessage(`cache-service-${environmentMode}`, { refreshCache });
35
- };
36
- return {
37
- refreshCache,
38
- };
39
- };
@@ -1,14 +0,0 @@
1
- import { EnvironmentMode, Page } from '../../../../common/index.js';
2
- import { AwsRegion } from '../../../../aws/common/index.js';
3
- import { CacheKey } from '../../cache-key/index.js';
4
- export type TCacheServiceSqsWorker = (props: {
5
- awsRegion: AwsRegion;
6
- environmentMode: EnvironmentMode;
7
- }) => {
8
- refreshCache: RefreshCache;
9
- };
10
- export type RefreshCache = (props?: {
11
- cacheKeys?: CacheKey[];
12
- pageDictionaries?: Page[];
13
- pageServerData?: Array<'candles-page' | 'home-page' | 'persons-page' | 'persons-map-page'>;
14
- }) => Promise<void>;