@nextage/nx-frame-be 1.0.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.
Files changed (119) hide show
  1. package/README.md +1 -0
  2. package/README.md.old +45 -0
  3. package/build/app.d.ts +0 -0
  4. package/build/app.js +1 -0
  5. package/build/aws/aws-cluster-manager.d.ts +28 -0
  6. package/build/aws/aws-cluster-manager.js +105 -0
  7. package/build/aws/aws-email-manager.d.ts +24 -0
  8. package/build/aws/aws-email-manager.js +57 -0
  9. package/build/aws/aws-queue-manager.d.ts +37 -0
  10. package/build/aws/aws-queue-manager.js +100 -0
  11. package/build/aws/aws.interfaces.d.ts +39 -0
  12. package/build/aws/aws.interfaces.js +2 -0
  13. package/build/aws/index.d.ts +4 -0
  14. package/build/aws/index.js +20 -0
  15. package/build/common/base/__test__/base.model.test.d.ts +1 -0
  16. package/build/common/base/__test__/base.model.test.js +283 -0
  17. package/build/common/base/base.controller.d.ts +124 -0
  18. package/build/common/base/base.controller.js +134 -0
  19. package/build/common/base/base.interfaces.d.ts +81 -0
  20. package/build/common/base/base.interfaces.js +2 -0
  21. package/build/common/base/base.model.d.ts +384 -0
  22. package/build/common/base/base.model.js +730 -0
  23. package/build/common/base/folded.model.d.ts +41 -0
  24. package/build/common/base/folded.model.js +215 -0
  25. package/build/common/base/mongo-utils.d.ts +122 -0
  26. package/build/common/base/mongo-utils.js +242 -0
  27. package/build/common/base/mongo.types.d.ts +20 -0
  28. package/build/common/base/mongo.types.js +2 -0
  29. package/build/common/base/sql-utils.d.ts +20 -0
  30. package/build/common/base/sql-utils.js +69 -0
  31. package/build/common/constants.d.ts +27 -0
  32. package/build/common/constants.js +70 -0
  33. package/build/common/crypto/crypto.classes.d.ts +100 -0
  34. package/build/common/crypto/crypto.classes.js +146 -0
  35. package/build/common/crypto/crypto.constants.d.ts +68 -0
  36. package/build/common/crypto/crypto.constants.js +57 -0
  37. package/build/common/crypto/crypto.error.d.ts +4 -0
  38. package/build/common/crypto/crypto.error.js +10 -0
  39. package/build/common/crypto/crypto.interfaces.d.ts +46 -0
  40. package/build/common/crypto/crypto.interfaces.js +2 -0
  41. package/build/common/crypto/crypto.utils.d.ts +139 -0
  42. package/build/common/crypto/crypto.utils.js +380 -0
  43. package/build/common/crypto/index.d.ts +3 -0
  44. package/build/common/crypto/index.js +19 -0
  45. package/build/common/enums.d.ts +61 -0
  46. package/build/common/enums.js +75 -0
  47. package/build/common/errors.d.ts +13 -0
  48. package/build/common/errors.js +24 -0
  49. package/build/common/express/express.interfaces.d.ts +20 -0
  50. package/build/common/express/express.interfaces.js +2 -0
  51. package/build/common/express/express.utils.d.ts +31 -0
  52. package/build/common/express/express.utils.js +85 -0
  53. package/build/common/express/index.d.ts +2 -0
  54. package/build/common/express/index.js +18 -0
  55. package/build/common/graphql/__test__/generator.schema.test.d.ts +1 -0
  56. package/build/common/graphql/__test__/generator.schema.test.js +391 -0
  57. package/build/common/graphql/directives.d.ts +18 -0
  58. package/build/common/graphql/directives.js +56 -0
  59. package/build/common/graphql/generator copy.d.ts +41 -0
  60. package/build/common/graphql/generator copy.js +385 -0
  61. package/build/common/graphql/generator.d.ts +42 -0
  62. package/build/common/graphql/generator.js +385 -0
  63. package/build/common/graphql/index.d.ts +4 -0
  64. package/build/common/graphql/index.js +21 -0
  65. package/build/common/graphql/interfaces.d.ts +67 -0
  66. package/build/common/graphql/interfaces.js +2 -0
  67. package/build/common/graphql/loader.d.ts +22 -0
  68. package/build/common/graphql/loader.js +186 -0
  69. package/build/common/graphql/scalars/index.d.ts +10 -0
  70. package/build/common/graphql/scalars/index.js +6 -0
  71. package/build/common/graphql/scalars/resolvers.d.ts +8 -0
  72. package/build/common/graphql/scalars/resolvers.js +71 -0
  73. package/build/common/graphql/scalars/schema.d.ts +1 -0
  74. package/build/common/graphql/scalars/schema.js +12 -0
  75. package/build/common/graphql/types/index.d.ts +3 -0
  76. package/build/common/graphql/types/index.js +6 -0
  77. package/build/common/graphql/types/schema.d.ts +1 -0
  78. package/build/common/graphql/types/schema.js +98 -0
  79. package/build/common/graphql/utils.d.ts +18 -0
  80. package/build/common/graphql/utils.js +40 -0
  81. package/build/common/grid-fs/gridfs-base.model.d.ts +53 -0
  82. package/build/common/grid-fs/gridfs-base.model.js +95 -0
  83. package/build/common/grid-fs/gridfs.interfaces.d.ts +41 -0
  84. package/build/common/grid-fs/gridfs.interfaces.js +2 -0
  85. package/build/common/index.d.ts +10 -0
  86. package/build/common/index.js +32 -0
  87. package/build/common/interfaces.d.ts +26 -0
  88. package/build/common/interfaces.js +10 -0
  89. package/build/common/manager/bulk-manager.d.ts +59 -0
  90. package/build/common/manager/bulk-manager.js +103 -0
  91. package/build/common/manager/crypto-manager.d.ts +88 -0
  92. package/build/common/manager/crypto-manager.js +200 -0
  93. package/build/common/manager/index.d.ts +5 -0
  94. package/build/common/manager/index.js +21 -0
  95. package/build/common/manager/pubsub-manager.d.ts +20 -0
  96. package/build/common/manager/pubsub-manager.js +38 -0
  97. package/build/common/manager/shutdown-manager.d.ts +27 -0
  98. package/build/common/manager/shutdown-manager.js +118 -0
  99. package/build/common/manager/socket-io-cli-manager.d.ts +53 -0
  100. package/build/common/manager/socket-io-cli-manager.js +91 -0
  101. package/build/common/manager/tunnel-manager.d.ts +28 -0
  102. package/build/common/manager/tunnel-manager.js +49 -0
  103. package/build/common/models/coded-entity.controller.d.ts +5 -0
  104. package/build/common/models/coded-entity.controller.js +9 -0
  105. package/build/common/models/coded-entity.model.d.ts +25 -0
  106. package/build/common/models/coded-entity.model.js +51 -0
  107. package/build/common/models/user.model.d.ts +56 -0
  108. package/build/common/models/user.model.js +72 -0
  109. package/build/common/types.d.ts +136 -0
  110. package/build/common/types.js +16 -0
  111. package/build/common/utils.d.ts +552 -0
  112. package/build/common/utils.js +1100 -0
  113. package/build/index.d.ts +2 -0
  114. package/build/index.js +18 -0
  115. package/build/test/env.d.ts +0 -0
  116. package/build/test/env.js +5 -0
  117. package/build/test/setup.d.ts +6 -0
  118. package/build/test/setup.js +59 -0
  119. package/package.json +90 -0
@@ -0,0 +1,103 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.BulkManager = void 0;
16
+ const async_1 = __importDefault(require("async"));
17
+ const constants_1 = require("../constants");
18
+ const utils_1 = require("../utils");
19
+ class BulkManager {
20
+ constructor(model, logClass, logger) {
21
+ this.model = model;
22
+ this.logClass = logClass || this;
23
+ this.logger = logger || constants_1.APP.logger;
24
+ this.reset();
25
+ }
26
+ /**
27
+ *
28
+ */
29
+ reset() {
30
+ if (!this.model)
31
+ return;
32
+ this.bulk = { index: 0, count: 0, value: [] };
33
+ this.bulk.value.push(this.model.initBulk());
34
+ }
35
+ /**
36
+ *
37
+ * @param selector
38
+ * @param update
39
+ * @param multi
40
+ * @param upsert
41
+ * @returns
42
+ */
43
+ add(selector, update, multi = false, upsert = false) {
44
+ var _a, _b, _c, _d, _e;
45
+ if (!this.model)
46
+ return;
47
+ if (update.$set && !Object.keys(update.$set).length)
48
+ delete update.$set;
49
+ if (update.$unset && !Object.keys(update.$unset).length)
50
+ delete update.$unset;
51
+ if (!Object.keys(update).length)
52
+ return;
53
+ if (multi)
54
+ (_a = this.bulk) === null || _a === void 0 ? void 0 : _a.value[this.bulk.index].find(selector).update(update);
55
+ else if (upsert)
56
+ (_b = this.bulk) === null || _b === void 0 ? void 0 : _b.value[this.bulk.index].find(selector).upsert().updateOne(update);
57
+ else
58
+ (_c = this.bulk) === null || _c === void 0 ? void 0 : _c.value[this.bulk.index].find(selector).updateOne(update);
59
+ this.bulk.count++;
60
+ if (((_d = this.bulk) === null || _d === void 0 ? void 0 : _d.count) === 50000) {
61
+ this.bulk.value.push(this.model.initBulk());
62
+ this.bulk.index++;
63
+ this.bulk.count = 0;
64
+ }
65
+ return (_e = this.bulk) === null || _e === void 0 ? void 0 : _e.index;
66
+ }
67
+ /**
68
+ *
69
+ * @param {*} res
70
+ */
71
+ parseRes(res) {
72
+ if (!this.model || !res)
73
+ return;
74
+ this.logger.debug(`bulk result - inserted: ${res.insertedCount || 0}, updated: ${res.modifiedCount || 0}, removed: ${res.deletedCount || 0}`, (0, utils_1.logClassCtx)(this.logClass));
75
+ }
76
+ /**
77
+ *
78
+ * @param {*} callback
79
+ */
80
+ apply(callback) {
81
+ if (!this.model)
82
+ return Promise.resolve();
83
+ return new Promise((resolve) => {
84
+ var _a;
85
+ async_1.default.eachSeries(((_a = this.bulk) === null || _a === void 0 ? void 0 : _a.value) || [], (bulk, cb) => __awaiter(this, void 0, void 0, function* () {
86
+ this.logger.debug('bulk update', (0, utils_1.logClassCtx)(this.logClass));
87
+ try {
88
+ const res = yield bulk.execute();
89
+ this.parseRes(res);
90
+ }
91
+ catch (e) {
92
+ this.logger.error(`bulk update error %o`, e, (0, utils_1.logClassCtx)(this));
93
+ }
94
+ cb();
95
+ }), (err) => {
96
+ this.reset();
97
+ callback();
98
+ resolve();
99
+ });
100
+ });
101
+ }
102
+ }
103
+ exports.BulkManager = BulkManager;
@@ -0,0 +1,88 @@
1
+ /// <reference types="node" />
2
+ import { Stream } from 'stream';
3
+ import { RSAKeyPair } from '../crypto';
4
+ import { KeyCypheredStreamPair, SymmetricKey } from '../crypto';
5
+ import { KeyOptions } from '../crypto';
6
+ export declare class CryptoManager {
7
+ private logger;
8
+ private secret;
9
+ constructor({ logger, secret }?: {
10
+ logger?: any;
11
+ secret?: string;
12
+ });
13
+ /**
14
+ * Cypher a stream using a random generated AES-256 key
15
+ *
16
+ * @param { Stream } toCypherStream the stream to be cyphered
17
+ * @returns { KeyCypheredStreamPair } the symKey generated and the cypheredStream
18
+ */
19
+ cypherStream(toCypherStream: string | Stream): Promise<KeyCypheredStreamPair>;
20
+ /**
21
+ * Decypher a stream using the keyoptions provided to get the right key from database
22
+ *
23
+ * @param { KeyOptions } keyOptions
24
+ * @param { Stream } streamToDecypher
25
+ */
26
+ decypherStream(keyOptions: {
27
+ privateKeyOptions: KeyOptions;
28
+ symmetricKeyOptions: KeyOptions;
29
+ }, streamToDecypher: Stream): Promise<string | Stream>;
30
+ /**
31
+ * Cypher symmetric key provided with publicKey and save it by keyOptions
32
+ *
33
+ * @param { SymmetricKey } symmetricKey
34
+ * @param { { symmetricKey: KeyOptions, publicKey: KeyOptions } } keysOptions
35
+ */
36
+ saveAESKey(symmetricKey: KeyOptions, { publicKeyOptions, symmetricKeyOptions }: {
37
+ publicKeyOptions: KeyOptions;
38
+ symmetricKeyOptions: KeyOptions;
39
+ }): Promise<void>;
40
+ /**
41
+ * Get the SymmetricKey by keyOptions and decypher it using privateKey
42
+ *
43
+ * @param { { symmetricKey: KeyOptions, privateKey: KeyOptions } } keysOptions
44
+ */
45
+ getAESKey({ privateKeyOptions, symmetricKeyOptions }: {
46
+ privateKeyOptions: KeyOptions;
47
+ symmetricKeyOptions: KeyOptions;
48
+ }): Promise<SymmetricKey>;
49
+ /**
50
+ * Generate Private/Public Rsa KeyPair using KeyOptions
51
+ *
52
+ * @param {KeyOptions | object} publicKeyOpt options used to locate the publicKey and it's characteristics
53
+ * @param {KeyOptions | object} privateKeyOpt options used to locate the privateKey and it's characteristics
54
+ */
55
+ getRSAKeyPair(publicKeyOptions: KeyOptions, privateKeyOptions: KeyOptions): Promise<RSAKeyPair>;
56
+ /**
57
+ * Save RSA KeyPair to the database using KeyOptions
58
+ *
59
+ * @param { RSAKeyOptions | object } publicKeyOptions options used to identify the publicKey saving characteristics
60
+ * @param { RSAKeyOptions | object } privateKeyOptions options used to identify the privateKey saving characteristics
61
+ */
62
+ saveRSAKeyPair(publicKeyOptions: KeyOptions, privateKeyOptions: KeyOptions): Promise<{
63
+ success: boolean;
64
+ message: string;
65
+ }>;
66
+ /**
67
+ * @private
68
+ * Save a generic Key using KeyOptions
69
+ *
70
+ * @param {*} key
71
+ * @param { KeyOptions | object} keyOptions
72
+ */
73
+ _saveKey(key: string, keyOptions: KeyOptions): Promise<any>;
74
+ /**
75
+ * @private
76
+ * Get a generic Key using provided options
77
+ *
78
+ * @param { KeyOptions | object } keyOptions options used to locate the key and it's characteristics
79
+ */
80
+ _getKey(keyOptions: KeyOptions): Promise<string>;
81
+ /**
82
+ * Verify if the chunk size given, it's valid based on chunks
83
+ *
84
+ * @param { String[] } chunks the chunks
85
+ * @param { number } chunkSize size of everyChunks
86
+ */
87
+ _verifyChunkSize(chunks: string[], chunkSize: number): boolean;
88
+ }
@@ -0,0 +1,200 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.CryptoManager = void 0;
13
+ const constants_1 = require("../constants");
14
+ const crypto_1 = require("../crypto");
15
+ const crypto_2 = require("../crypto");
16
+ const constants_2 = require("../constants");
17
+ const crypto_constants_1 = require("../crypto/crypto.constants");
18
+ const crypto_constants_2 = require("../crypto/crypto.constants");
19
+ const crypto_error_1 = require("../crypto/crypto.error");
20
+ const utils_1 = require("../utils");
21
+ const utils_2 = require("../utils");
22
+ const utils_3 = require("../utils");
23
+ // const KEY_TYPES = Object.freeze({
24
+ // PUBLIC : 'publicKeyOptions',
25
+ // PRIVATE : 'privateKeyOptions',
26
+ // SYMMETRIC: 'symmetricKeyOptions'
27
+ // });
28
+ class CryptoManager {
29
+ constructor({ logger, secret } = {}) {
30
+ this.logger = logger || constants_2.APP.logger;
31
+ this.secret = secret || constants_2.APP.dataScr || '';
32
+ // console.log('asd', APP.dataScr)
33
+ if (!this.secret)
34
+ throw new Error('Secret is not defined');
35
+ }
36
+ /**
37
+ * Cypher a stream using a random generated AES-256 key
38
+ *
39
+ * @param { Stream } toCypherStream the stream to be cyphered
40
+ * @returns { KeyCypheredStreamPair } the symKey generated and the cypheredStream
41
+ */
42
+ cypherStream(toCypherStream) {
43
+ return __awaiter(this, void 0, void 0, function* () {
44
+ const symKey = yield constants_1.cryptoUtils.generateRndSymKey(crypto_constants_2.CRYPTO_SYM_BITSIZE);
45
+ const cypheredStream = yield constants_1.cryptoUtils.cypherSym(symKey, toCypherStream);
46
+ return new crypto_2.KeyCypheredStreamPair(symKey, cypheredStream);
47
+ });
48
+ }
49
+ /**
50
+ * Decypher a stream using the keyoptions provided to get the right key from database
51
+ *
52
+ * @param { KeyOptions } keyOptions
53
+ * @param { Stream } streamToDecypher
54
+ */
55
+ decypherStream(keyOptions, streamToDecypher) {
56
+ return __awaiter(this, void 0, void 0, function* () {
57
+ const keyObj = yield this.getAESKey(keyOptions);
58
+ return yield constants_1.cryptoUtils.decypherSym(keyObj, streamToDecypher);
59
+ });
60
+ }
61
+ /**
62
+ * Cypher symmetric key provided with publicKey and save it by keyOptions
63
+ *
64
+ * @param { SymmetricKey } symmetricKey
65
+ * @param { { symmetricKey: KeyOptions, publicKey: KeyOptions } } keysOptions
66
+ */
67
+ saveAESKey(symmetricKey_1, _a) {
68
+ return __awaiter(this, arguments, void 0, function* (symmetricKey, { publicKeyOptions, symmetricKeyOptions }) {
69
+ const publicKey = yield this._getKey(publicKeyOptions);
70
+ const encryptedKey = yield constants_1.cryptoUtils.cypherAsym(publicKey, JSON.stringify(symmetricKey));
71
+ yield this._saveKey(encryptedKey, symmetricKeyOptions);
72
+ });
73
+ }
74
+ /**
75
+ * Get the SymmetricKey by keyOptions and decypher it using privateKey
76
+ *
77
+ * @param { { symmetricKey: KeyOptions, privateKey: KeyOptions } } keysOptions
78
+ */
79
+ getAESKey(_a) {
80
+ return __awaiter(this, arguments, void 0, function* ({ privateKeyOptions, symmetricKeyOptions }) {
81
+ const privateKey = yield this._getKey(privateKeyOptions);
82
+ if (!privateKey) {
83
+ const error = crypto_constants_2.CRYPTO_ERRORS.PRIV_KEY_NOT_FOUND;
84
+ this.logger.error(`[${error.code}] ${error.message}`, (0, utils_1.logClassCtx)(this, 'getAESKey'));
85
+ throw new crypto_error_1.CryptoError(error.message, error.code);
86
+ }
87
+ const encryptedKey = yield this._getKey(symmetricKeyOptions);
88
+ if (!encryptedKey) {
89
+ const error = crypto_constants_2.CRYPTO_ERRORS.ENC_SYM_NOT_FOUND;
90
+ this.logger.error(`[${error.code}] ${error.message}`, (0, utils_1.logClassCtx)(this, 'getAESKey'));
91
+ throw new crypto_error_1.CryptoError(error.message, error.code);
92
+ }
93
+ const decryptedKey = yield constants_1.cryptoUtils.decypherAsym(privateKey, encryptedKey);
94
+ return JSON.parse(decryptedKey);
95
+ });
96
+ }
97
+ /**
98
+ * Generate Private/Public Rsa KeyPair using KeyOptions
99
+ *
100
+ * @param {KeyOptions | object} publicKeyOpt options used to locate the publicKey and it's characteristics
101
+ * @param {KeyOptions | object} privateKeyOpt options used to locate the privateKey and it's characteristics
102
+ */
103
+ getRSAKeyPair(publicKeyOptions, privateKeyOptions) {
104
+ return __awaiter(this, void 0, void 0, function* () {
105
+ const keyPair = { publicKey: '', privateKey: '' };
106
+ const options = new crypto_1.RSAKeyPairOptions(publicKeyOptions, privateKeyOptions);
107
+ keyPair.privateKey = yield this._getKey(options.privateKeyOptions);
108
+ keyPair.publicKey = yield this._getKey(options.publicKeyOptions);
109
+ return keyPair;
110
+ });
111
+ }
112
+ /**
113
+ * Save RSA KeyPair to the database using KeyOptions
114
+ *
115
+ * @param { RSAKeyOptions | object } publicKeyOptions options used to identify the publicKey saving characteristics
116
+ * @param { RSAKeyOptions | object } privateKeyOptions options used to identify the privateKey saving characteristics
117
+ */
118
+ saveRSAKeyPair(publicKeyOptions, privateKeyOptions) {
119
+ return __awaiter(this, void 0, void 0, function* () {
120
+ const options = new crypto_1.RSAKeyPairOptions(publicKeyOptions, privateKeyOptions);
121
+ const keypair = yield constants_1.cryptoUtils.generateAsymKeys(crypto_constants_1.CRYPTO_ASYM_BITSIZE);
122
+ if (!keypair) {
123
+ const error = crypto_constants_2.CRYPTO_ERRORS.GEN_KEY_PAIR;
124
+ this.logger.error(`[${error.code}] ${error.message}`, (0, utils_1.logClassCtx)(this, 'saveRSAKeyPair'));
125
+ throw new crypto_error_1.CryptoError(error.message, error.code);
126
+ }
127
+ const keys = Object.keys(keypair);
128
+ const promises = keys.map((property) => this._saveKey(keypair[property], options[`${property}Options`]));
129
+ const res = yield Promise.all(promises);
130
+ return { success: true, message: 'RSA KeyPair saved successfully' };
131
+ });
132
+ }
133
+ /**
134
+ * @private
135
+ * Save a generic Key using KeyOptions
136
+ *
137
+ * @param {*} key
138
+ * @param { KeyOptions | object} keyOptions
139
+ */
140
+ _saveKey(key, keyOptions) {
141
+ const tempEncKey = constants_1.cryptoUtils.encryptText(key, this.secret);
142
+ const encryptedKey = keyOptions.isChunked ? (0, utils_3.chunkString)(tempEncKey, keyOptions.chunkSize) : tempEncKey;
143
+ const data = keyOptions.hasFilter ? (0, utils_1.initObjectWithProperty)(keyOptions.path, encryptedKey) : (0, utils_2.initObjectFromDotNotation)(keyOptions.path, encryptedKey);
144
+ if (keyOptions.hasAdditionalFields) {
145
+ keyOptions.additionalFields.forEach((field) => {
146
+ keyOptions.hasFilter ? data[field.path] = field.value : (0, utils_3.setAttr)(data, field.path, field.value);
147
+ });
148
+ }
149
+ return keyOptions.hasFilter ? keyOptions.model.findOneAndUpdate(keyOptions.filter, data, {}) : keyOptions.model.create(data, {});
150
+ }
151
+ /**
152
+ * @private
153
+ * Get a generic Key using provided options
154
+ *
155
+ * @param { KeyOptions | object } keyOptions options used to locate the key and it's characteristics
156
+ */
157
+ _getKey(keyOptions) {
158
+ return __awaiter(this, void 0, void 0, function* () {
159
+ const logCtx = (0, utils_1.logClassCtx)(this, '_getKey');
160
+ if (!keyOptions) {
161
+ const error = crypto_constants_2.CRYPTO_ERRORS.KEY_OPTS_ARGS;
162
+ this.logger.error(`[${error.code}] ${error.message}`, logCtx);
163
+ throw new crypto_error_1.CryptoError(error.message, error.code);
164
+ }
165
+ if (keyOptions.key)
166
+ return keyOptions.key;
167
+ if (!keyOptions.model || !keyOptions.hasFilter) {
168
+ const error = crypto_constants_2.CRYPTO_ERRORS.KEY_OPTS_PROPS;
169
+ this.logger.error(`[${error.code}] ${error.message}`, logCtx);
170
+ throw new crypto_error_1.CryptoError(error.message, error.code);
171
+ }
172
+ const data = yield keyOptions.model.findOne(keyOptions.filter);
173
+ let encryptedKey = '';
174
+ if (keyOptions.isChunked) {
175
+ const chunks = (0, utils_3.getAttr)(data, keyOptions.path);
176
+ if (!this._verifyChunkSize(chunks, keyOptions.chunkSize)) {
177
+ const error = crypto_constants_2.CRYPTO_ERRORS.CHUNK_SIZE;
178
+ this.logger.error(`[${error.code}] ${error.message}`, logCtx);
179
+ throw new crypto_error_1.CryptoError(error.message, error.code);
180
+ }
181
+ encryptedKey = chunks.join('');
182
+ }
183
+ else
184
+ encryptedKey = (0, utils_3.getAttr)(data, keyOptions.path);
185
+ return constants_1.cryptoUtils.decryptText(encryptedKey, this.secret);
186
+ });
187
+ }
188
+ /**
189
+ * Verify if the chunk size given, it's valid based on chunks
190
+ *
191
+ * @param { String[] } chunks the chunks
192
+ * @param { number } chunkSize size of everyChunks
193
+ */
194
+ _verifyChunkSize(chunks, chunkSize) {
195
+ const origChunkNumber = chunks.length;
196
+ const newChunkNumber = (0, utils_3.chunkString)(chunks.join(''), chunkSize).length;
197
+ return origChunkNumber === newChunkNumber;
198
+ }
199
+ }
200
+ exports.CryptoManager = CryptoManager;
@@ -0,0 +1,5 @@
1
+ export * from './bulk-manager';
2
+ export * from './crypto-manager';
3
+ export * from './pubsub-manager';
4
+ export * from './socket-io-cli-manager';
5
+ export * from './tunnel-manager';
@@ -0,0 +1,21 @@
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 __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./bulk-manager"), exports);
18
+ __exportStar(require("./crypto-manager"), exports);
19
+ __exportStar(require("./pubsub-manager"), exports);
20
+ __exportStar(require("./socket-io-cli-manager"), exports);
21
+ __exportStar(require("./tunnel-manager"), exports);
@@ -0,0 +1,20 @@
1
+ import { NxContext } from '../interfaces';
2
+ export declare class PubSubManager {
3
+ /**
4
+ *
5
+ * @param {*} key
6
+ * @param {*} method
7
+ */
8
+ subscribe(key: string, method: Function): string;
9
+ /**
10
+ *
11
+ * @param {*} key
12
+ * @param {*} data
13
+ */
14
+ publish(key: string, data: any, context: NxContext): void;
15
+ /**
16
+ *
17
+ * @param {*} id
18
+ */
19
+ unsubscribe(id: string): void;
20
+ }
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PubSubManager = void 0;
4
+ const events_1 = require("events");
5
+ const utils_1 = require("../utils");
6
+ const subs = {};
7
+ const eventsEmitter = new events_1.EventEmitter();
8
+ class PubSubManager {
9
+ /**
10
+ *
11
+ * @param {*} key
12
+ * @param {*} method
13
+ */
14
+ subscribe(key, method) {
15
+ if (!(0, utils_1.isFunction)(method))
16
+ return '';
17
+ const id = (0, utils_1.uuid)();
18
+ subs[id] = { key, method };
19
+ eventsEmitter.on(key, (data, context) => method(data, context));
20
+ return id;
21
+ }
22
+ /**
23
+ *
24
+ * @param {*} key
25
+ * @param {*} data
26
+ */
27
+ publish(key, data, context) {
28
+ eventsEmitter.emit(key, data, context);
29
+ }
30
+ /**
31
+ *
32
+ * @param {*} id
33
+ */
34
+ unsubscribe(id) {
35
+ eventsEmitter.off(subs[id].key, subs[id].method);
36
+ }
37
+ }
38
+ exports.PubSubManager = PubSubManager;
@@ -0,0 +1,27 @@
1
+ export declare class ShutdownManager {
2
+ private isShuttingDown;
3
+ private shutdownHandlers;
4
+ shutdownTimeout: number;
5
+ constructor();
6
+ private setupSignalHandlers;
7
+ /**
8
+ *
9
+ * @param handler
10
+ */
11
+ addShutdownHandler(handler: () => Promise<void>): void;
12
+ /**
13
+ *
14
+ * @param exitCode
15
+ * @returns
16
+ */
17
+ private shutdown;
18
+ /**
19
+ *
20
+ */
21
+ private executeShutdownHandlers;
22
+ /**
23
+ *
24
+ * @returns
25
+ */
26
+ isShutdownInProgress(): boolean;
27
+ }
@@ -0,0 +1,118 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.ShutdownManager = void 0;
13
+ const constants_1 = require("../constants");
14
+ class ShutdownManager {
15
+ constructor() {
16
+ this.isShuttingDown = false;
17
+ this.shutdownHandlers = [];
18
+ this.shutdownTimeout = 3000;
19
+ this.setupSignalHandlers();
20
+ }
21
+ setupSignalHandlers() {
22
+ // SIGTERM Management (normal termination)
23
+ process.on('SIGTERM', () => {
24
+ constants_1.APP.logger.info('\nšŸ›‘ Received SIGTERM, starting graceful shutdown...');
25
+ this.shutdown();
26
+ });
27
+ // SIGINT Management (Ctrl+C)
28
+ process.on('SIGINT', () => {
29
+ constants_1.APP.logger.info('\nšŸ›‘ Received SIGINT (Ctrl+C), starting graceful shutdown...');
30
+ this.shutdown();
31
+ });
32
+ // SIGHUP Management (closed terminal)
33
+ process.on('SIGHUP', () => {
34
+ constants_1.APP.logger.info('\nšŸ›‘ Received SIGHUP, starting graceful shutdown...');
35
+ this.shutdown();
36
+ });
37
+ // SIGUSR2 Management (nodemon)
38
+ process.on('SIGUSR2', () => {
39
+ constants_1.APP.logger.info('\nšŸ›‘ Received SIGHUP, starting graceful shutdown...');
40
+ this.shutdown();
41
+ });
42
+ // Manages other exceptions
43
+ process.on('uncaughtException', (error) => {
44
+ constants_1.APP.logger.error('šŸ’„ Uncaught Exception: %o', error);
45
+ constants_1.APP.logger.info('šŸ›‘ Starting emergency shutdown...');
46
+ this.shutdown(1);
47
+ });
48
+ // Manages promise unhandled rejection
49
+ process.on('unhandledRejection', (reason, promise) => {
50
+ constants_1.APP.logger.error('šŸ’„ Unhandled Rejection at: %o reason: %o', promise, reason);
51
+ constants_1.APP.logger.info('šŸ›‘ Starting emergency shutdown...');
52
+ this.shutdown(1);
53
+ });
54
+ }
55
+ /**
56
+ *
57
+ * @param handler
58
+ */
59
+ addShutdownHandler(handler) {
60
+ this.shutdownHandlers.push(handler);
61
+ }
62
+ /**
63
+ *
64
+ * @param exitCode
65
+ * @returns
66
+ */
67
+ shutdown() {
68
+ return __awaiter(this, arguments, void 0, function* (exitCode = 0) {
69
+ if (this.isShuttingDown) {
70
+ constants_1.APP.logger.info('āš ļø Shutdown already in progress...');
71
+ return;
72
+ }
73
+ this.isShuttingDown = true;
74
+ const shutdownPromise = this.executeShutdownHandlers();
75
+ const timeoutPromise = new Promise((_, reject) => {
76
+ setTimeout(() => {
77
+ reject(new Error(`Shutdown timeout after ${this.shutdownTimeout}ms`));
78
+ }, this.shutdownTimeout);
79
+ });
80
+ try {
81
+ yield Promise.race([shutdownPromise, timeoutPromise]);
82
+ constants_1.APP.logger.info('āœ… Graceful shutdown completed');
83
+ }
84
+ catch (error) {
85
+ constants_1.APP.logger.error('āŒ Shutdown timeout or error:', error);
86
+ exitCode = 1;
87
+ }
88
+ process.exit(exitCode);
89
+ });
90
+ }
91
+ /**
92
+ *
93
+ */
94
+ executeShutdownHandlers() {
95
+ return __awaiter(this, void 0, void 0, function* () {
96
+ constants_1.APP.logger.info(`šŸ”„ Executing ${this.shutdownHandlers.length} shutdown handlers...`);
97
+ const promises = this.shutdownHandlers.map((handler, index) => __awaiter(this, void 0, void 0, function* () {
98
+ try {
99
+ yield handler();
100
+ constants_1.APP.logger.info(`āœ… Shutdown handler ${index + 1} completed`);
101
+ }
102
+ catch (error) {
103
+ constants_1.APP.logger.error(`āŒ Shutdown handler ${index + 1} failed:`, error);
104
+ throw error;
105
+ }
106
+ }));
107
+ yield Promise.all(promises);
108
+ });
109
+ }
110
+ /**
111
+ *
112
+ * @returns
113
+ */
114
+ isShutdownInProgress() {
115
+ return this.isShuttingDown;
116
+ }
117
+ }
118
+ exports.ShutdownManager = ShutdownManager;
@@ -0,0 +1,53 @@
1
+ /// <reference types="node" />
2
+ import EventEmitter from 'events';
3
+ import { ManagerOptions } from 'socket.io-client';
4
+ export declare class SocketIoCliManager extends EventEmitter {
5
+ private params;
6
+ private host;
7
+ private port;
8
+ private protocol;
9
+ private manager?;
10
+ private socket?;
11
+ private uri?;
12
+ private logger;
13
+ private _listeners;
14
+ private _logCtx;
15
+ constructor(host: string, port: number, params?: Partial<ManagerOptions>, logger?: any);
16
+ /**
17
+ * Init Socket
18
+ *
19
+ * @param {*} encoding
20
+ */
21
+ private init;
22
+ /**
23
+ *
24
+ */
25
+ protected onConnect(): void;
26
+ /**
27
+ *
28
+ */
29
+ protected onDisconnect(): void;
30
+ /**
31
+ * Format and send message
32
+ * @param {*} event
33
+ * @param {*} data
34
+ * @param {*} callback
35
+ */
36
+ sendData(event: string, data: any, callback: Function): void;
37
+ /**
38
+ *
39
+ * @param {*} event
40
+ * @param {*} callback
41
+ */
42
+ addEventListener(event: string, callback: Function): void;
43
+ /**
44
+ *
45
+ * @param {*} event
46
+ * @param {*} callback
47
+ */
48
+ removeEventListener(event: string): void;
49
+ /**
50
+ * Close Socket
51
+ */
52
+ close(): void;
53
+ }