drapcode-utility 2.0.1 → 2.0.2

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 (50) hide show
  1. package/build/encryption/KMS.js +54 -102
  2. package/build/encryption/crypt.d.ts +4 -2
  3. package/build/encryption/crypt.js +76 -91
  4. package/build/encryption/file.d.ts +0 -2
  5. package/build/encryption/file.js +14 -130
  6. package/build/encryption/index.js +162 -334
  7. package/build/encryption/utility.js +7 -10
  8. package/build/errors/app-error.js +9 -27
  9. package/build/errors/axios-error.js +3 -3
  10. package/build/errors/bad-request-error.js +10 -28
  11. package/build/errors/custom-error.js +5 -23
  12. package/build/errors/not-found.js +9 -27
  13. package/build/format-fields/index.d.ts +0 -1
  14. package/build/format-fields/index.js +32 -65
  15. package/build/index.d.ts +1 -4
  16. package/build/index.js +1 -4
  17. package/build/middlewares/error-logger.d.ts +1 -1
  18. package/build/middlewares/error-logger.js +29 -29
  19. package/build/middlewares/redis/request-log.js +24 -74
  20. package/build/query/queryBuilder.d.ts +9 -0
  21. package/build/query/queryBuilder.js +567 -0
  22. package/build/utils/check-error.d.ts +15 -8
  23. package/build/utils/check-error.js +71 -160
  24. package/build/utils/common-util.d.ts +40 -39
  25. package/build/utils/common-util.js +60 -59
  26. package/build/utils/date-util.d.ts +28 -7
  27. package/build/utils/date-util.js +180 -127
  28. package/build/utils/file-util.d.ts +51 -6
  29. package/build/utils/file-util.js +36 -40
  30. package/build/utils/prepare-query.js +70 -43
  31. package/build/utils/project-util.d.ts +43 -5
  32. package/build/utils/project-util.js +176 -121
  33. package/build/utils/query-parser.d.ts +1 -1
  34. package/build/utils/query-parser.js +289 -342
  35. package/build/utils/query-utils.d.ts +2 -2
  36. package/build/utils/query-utils.js +103 -116
  37. package/build/utils/rest-client.js +236 -328
  38. package/build/utils/s3-util.js +238 -469
  39. package/build/utils/token.js +34 -81
  40. package/build/utils/util.d.ts +58 -13
  41. package/build/utils/util.js +424 -494
  42. package/build/utils/uuid-generator.d.ts +20 -1
  43. package/build/utils/uuid-generator.js +111 -47
  44. package/package.json +7 -5
  45. package/build/middlewares/interceptor-logger-new.d.ts +0 -2
  46. package/build/middlewares/interceptor-logger-new.js +0 -53
  47. package/build/middlewares/interceptor-logger.d.ts +0 -2
  48. package/build/middlewares/interceptor-logger.js +0 -52
  49. package/build/utils/query-parser-new.d.ts +0 -1
  50. package/build/utils/query-parser-new.js +0 -541
@@ -1,107 +1,59 @@
1
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 __generator = (this && this.__generator) || function (thisArg, body) {
12
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
- function verb(n) { return function (v) { return step([n, v]); }; }
15
- function step(op) {
16
- if (f) throw new TypeError("Generator is already executing.");
17
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
- if (y = 0, t) op = [op[0] & 2, t.value];
20
- switch (op[0]) {
21
- case 0: case 1: t = op; break;
22
- case 4: _.label++; return { value: op[1], done: false };
23
- case 5: _.label++; y = op[1]; op = [0]; continue;
24
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
- default:
26
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
- if (t[2]) _.ops.pop();
31
- _.trys.pop(); continue;
32
- }
33
- op = body.call(thisArg, _);
34
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
- }
37
- };
38
2
  Object.defineProperty(exports, "__esModule", { value: true });
39
3
  exports.processKMSGenerateDataKey = exports.processKMSEncryption = void 0;
40
- var client_kms_1 = require("@aws-sdk/client-kms");
41
- var processKMSEncryption = function (config, arn, plainText, context) { return __awaiter(void 0, void 0, void 0, function () {
42
- var accessKeyId, secretAccessKey, region, client, input, command, response, cipherText, error_1;
43
- return __generator(this, function (_a) {
44
- switch (_a.label) {
45
- case 0:
46
- _a.trys.push([0, 2, , 3]);
47
- accessKeyId = config.accessKeyId, secretAccessKey = config.secretAccessKey, region = config.region;
48
- client = new client_kms_1.KMSClient({
49
- region: region,
50
- credentials: { accessKeyId: accessKeyId, secretAccessKey: secretAccessKey },
51
- });
52
- input = {
53
- KeyId: arn,
54
- Plaintext: Buffer.from(plainText),
55
- EncryptionContext: context,
56
- };
57
- command = new client_kms_1.EncryptCommand(input);
58
- return [4 /*yield*/, client.send(command)];
59
- case 1:
60
- response = _a.sent();
61
- cipherText = Buffer.from(response.CiphertextBlob).toString("base64");
62
- return [2 /*return*/, { status: "SUCCESS", data: cipherText, message: "" }];
63
- case 2:
64
- error_1 = _a.sent();
65
- return [2 /*return*/, {
66
- status: "FAILED",
67
- data: "",
68
- message: error_1.message,
69
- }];
70
- case 3: return [2 /*return*/];
71
- }
72
- });
73
- }); };
4
+ const client_kms_1 = require("@aws-sdk/client-kms");
5
+ const processKMSEncryption = async (config, arn, plainText, context) => {
6
+ try {
7
+ const { accessKeyId, secretAccessKey, region } = config;
8
+ const client = new client_kms_1.KMSClient({
9
+ region,
10
+ credentials: { accessKeyId, secretAccessKey },
11
+ });
12
+ const input = {
13
+ KeyId: arn,
14
+ Plaintext: Buffer.from(plainText),
15
+ EncryptionContext: context,
16
+ };
17
+ const command = new client_kms_1.EncryptCommand(input);
18
+ const response = await client.send(command);
19
+ // Encrypted text will be in uint8array
20
+ // To save it in database, we need string representation
21
+ //@ts-ignore it is important else it will throw error
22
+ const cipherText = Buffer.from(response.CiphertextBlob).toString("base64");
23
+ return { status: "SUCCESS", data: cipherText, message: "" };
24
+ }
25
+ catch (error) {
26
+ return {
27
+ status: "FAILED",
28
+ data: "",
29
+ message: error.message,
30
+ };
31
+ }
32
+ };
74
33
  exports.processKMSEncryption = processKMSEncryption;
75
- var processKMSGenerateDataKey = function (config, arn) { return __awaiter(void 0, void 0, void 0, function () {
76
- var accessKeyId, secretAccessKey, region, input, client, command, response, cipherText, error_2;
77
- return __generator(this, function (_a) {
78
- switch (_a.label) {
79
- case 0:
80
- _a.trys.push([0, 2, , 3]);
81
- accessKeyId = config.accessKeyId, secretAccessKey = config.secretAccessKey, region = config.region;
82
- input = {
83
- KeyId: arn,
84
- KeySpec: "AES_256",
85
- };
86
- client = new client_kms_1.KMSClient({
87
- region: region,
88
- credentials: { accessKeyId: accessKeyId, secretAccessKey: secretAccessKey },
89
- });
90
- command = new client_kms_1.GenerateDataKeyCommand(input);
91
- return [4 /*yield*/, client.send(command)];
92
- case 1:
93
- response = _a.sent();
94
- cipherText = Buffer.from(response.CiphertextBlob).toString("base64");
95
- return [2 /*return*/, { status: "SUCCESS", dataKey: cipherText, message: "" }];
96
- case 2:
97
- error_2 = _a.sent();
98
- return [2 /*return*/, {
99
- status: "FAILED",
100
- message: error_2.message,
101
- dataKey: "",
102
- }];
103
- case 3: return [2 /*return*/];
104
- }
105
- });
106
- }); };
34
+ const processKMSGenerateDataKey = async (config, arn) => {
35
+ try {
36
+ const { accessKeyId, secretAccessKey, region } = config;
37
+ const input = {
38
+ KeyId: arn,
39
+ KeySpec: "AES_256",
40
+ };
41
+ const client = new client_kms_1.KMSClient({
42
+ region,
43
+ credentials: { accessKeyId, secretAccessKey },
44
+ });
45
+ const command = new client_kms_1.GenerateDataKeyCommand(input);
46
+ const response = await client.send(command);
47
+ //@ts-ignore it is important else it will throw error
48
+ const cipherText = Buffer.from(response.CiphertextBlob).toString("base64");
49
+ return { status: "SUCCESS", dataKey: cipherText, message: "" };
50
+ }
51
+ catch (error) {
52
+ return {
53
+ status: "FAILED",
54
+ message: error.message,
55
+ dataKey: "",
56
+ };
57
+ }
58
+ };
107
59
  exports.processKMSGenerateDataKey = processKMSGenerateDataKey;
@@ -1,2 +1,4 @@
1
- export declare const encryptData: (data: string, key: string) => Promise<string>;
2
- export declare const decryptData: (data: string, key: string) => Promise<string>;
1
+ export declare const encryptData: (data: string, key: string) => string;
2
+ export declare const decryptData: (data: string, key: string) => string;
3
+ export declare const encryptFile: (filePath: string, key: string) => Promise<string>;
4
+ export declare const decryptFile: (encryptedFilePath: string, key: string) => Promise<string>;
@@ -1,103 +1,88 @@
1
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 __generator = (this && this.__generator) || function (thisArg, body) {
12
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
- function verb(n) { return function (v) { return step([n, v]); }; }
15
- function step(op) {
16
- if (f) throw new TypeError("Generator is already executing.");
17
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
- if (y = 0, t) op = [op[0] & 2, t.value];
20
- switch (op[0]) {
21
- case 0: case 1: t = op; break;
22
- case 4: _.label++; return { value: op[1], done: false };
23
- case 5: _.label++; y = op[1]; op = [0]; continue;
24
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
- default:
26
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
- if (t[2]) _.ops.pop();
31
- _.trys.pop(); continue;
32
- }
33
- op = body.call(thisArg, _);
34
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
- }
37
- };
38
2
  var __importDefault = (this && this.__importDefault) || function (mod) {
39
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
40
4
  };
41
5
  Object.defineProperty(exports, "__esModule", { value: true });
42
- exports.decryptData = exports.encryptData = void 0;
43
- var crypto_1 = __importDefault(require("crypto"));
44
- var defaultAlgorithm = "aes-256-cbc";
45
- var encryptData = function (data, key) { return __awaiter(void 0, void 0, void 0, function () {
46
- var iv, keyBuffer, cipher, encryptedDataBuffer, result;
47
- return __generator(this, function (_a) {
48
- try {
49
- iv = Buffer.from("i4mboZDwaNEC38YCzi77lw==", "base64");
50
- keyBuffer = Buffer.from(key, "base64");
51
- cipher = crypto_1.default.createCipheriv(defaultAlgorithm, keyBuffer, iv);
52
- encryptedDataBuffer = cipher.update("".concat(data));
53
- encryptedDataBuffer = Buffer.concat([encryptedDataBuffer, cipher.final()]);
54
- result = encryptedDataBuffer.toString("base64");
55
- return [2 /*return*/, handleExtraString(result, true)];
56
- }
57
- catch (error) {
58
- console.error("\n Error: ", error);
59
- return [2 /*return*/, data];
60
- }
61
- return [2 /*return*/];
62
- });
63
- }); };
6
+ exports.decryptFile = exports.encryptFile = exports.decryptData = exports.encryptData = void 0;
7
+ const fs_1 = __importDefault(require("fs"));
8
+ const crypto_1 = __importDefault(require("crypto"));
9
+ const defaultAlgorithm = "aes-256-cbc";
10
+ const fixedIv = Buffer.from("i4mboZDwaNEC38YCzi77lw==", "base64"); // Must be 16 bytes
11
+ const encryptData = (data, key) => {
12
+ try {
13
+ const keyBuffer = Buffer.from(key, "base64");
14
+ const cipher = crypto_1.default.createCipheriv(defaultAlgorithm, keyBuffer, fixedIv);
15
+ const encrypted = Buffer.concat([cipher.update(`${data}`), cipher.final()]);
16
+ const base64Encrypted = encrypted.toString("base64");
17
+ return handleExtraString(base64Encrypted, true);
18
+ }
19
+ catch (error) {
20
+ console.error("\n Error: ", error);
21
+ return data;
22
+ }
23
+ };
64
24
  exports.encryptData = encryptData;
65
- var decryptData = function (data, key) { return __awaiter(void 0, void 0, void 0, function () {
66
- var iv, encryptedData, keyBuffer, decipher, decryptedBuffer;
67
- return __generator(this, function (_a) {
68
- try {
69
- data = handleExtraString(data, false);
70
- iv = Buffer.from("i4mboZDwaNEC38YCzi77lw==", "base64");
71
- encryptedData = Buffer.from(data, "base64");
72
- keyBuffer = Buffer.from(key, "base64");
73
- decipher = crypto_1.default.createDecipheriv(defaultAlgorithm, keyBuffer, iv);
74
- decryptedBuffer = decipher.update(encryptedData);
75
- decryptedBuffer = Buffer.concat([decryptedBuffer, decipher.final()]);
76
- return [2 /*return*/, decryptedBuffer.toString()];
77
- }
78
- catch (error) {
79
- console.error("\n Error: ", error);
80
- return [2 /*return*/, data];
81
- }
82
- return [2 /*return*/];
83
- });
84
- }); };
25
+ const decryptData = (data, key) => {
26
+ try {
27
+ const cleaned = handleExtraString(data, false);
28
+ const encryptedData = Buffer.from(cleaned, "base64");
29
+ const keyBuffer = Buffer.from(key, "base64");
30
+ const decipher = crypto_1.default.createDecipheriv(defaultAlgorithm, keyBuffer, fixedIv);
31
+ const decrypted = Buffer.concat([
32
+ decipher.update(encryptedData),
33
+ decipher.final(),
34
+ ]);
35
+ return decrypted.toString();
36
+ }
37
+ catch (error) {
38
+ console.error("\n Error: ", error);
39
+ return data;
40
+ }
41
+ };
85
42
  exports.decryptData = decryptData;
86
- var handleExtraString = function (key, append) {
87
- if (append === void 0) { append = true; }
88
- if (!key || key === "undefined") {
89
- return key;
43
+ const encryptFile = async (filePath, key) => {
44
+ try {
45
+ const keyBuffer = Buffer.from(key, "base64");
46
+ const fileContent = await fs_1.default.promises.readFile(filePath);
47
+ const cipher = crypto_1.default.createCipheriv(defaultAlgorithm, keyBuffer, fixedIv);
48
+ let encryptedDataBuffer = cipher.update(fileContent);
49
+ encryptedDataBuffer = Buffer.concat([encryptedDataBuffer, cipher.final()]);
50
+ const encryptedFilePath = filePath + ".enc";
51
+ await fs_1.default.promises.writeFile(encryptedFilePath, encryptedDataBuffer);
52
+ console.log("File encrypted successfully.");
53
+ return encryptedFilePath;
90
54
  }
91
- if (append) {
92
- var start = crypto_1.default.randomBytes(2).toString("hex");
93
- var end = crypto_1.default.randomBytes(2).toString("hex");
94
- key = "".concat(start).concat(key).concat(end);
95
- return key;
55
+ catch (error) {
56
+ console.error("\n Error: ", error);
57
+ throw error;
58
+ }
59
+ };
60
+ exports.encryptFile = encryptFile;
61
+ const decryptFile = async (encryptedFilePath, key) => {
62
+ try {
63
+ const keyBuffer = Buffer.from(key, "base64");
64
+ const encryptedData = await fs_1.default.promises.readFile(encryptedFilePath);
65
+ const decipher = crypto_1.default.createDecipheriv(defaultAlgorithm, keyBuffer, fixedIv);
66
+ let decryptedBuffer = decipher.update(encryptedData);
67
+ decryptedBuffer = Buffer.concat([decryptedBuffer, decipher.final()]);
68
+ const decryptedFilePath = `${encryptedFilePath.slice(0, -4)}.dec`;
69
+ await fs_1.default.promises.writeFile(decryptedFilePath, decryptedBuffer);
70
+ console.log("File decrypted successfully.");
71
+ return decryptedFilePath;
72
+ }
73
+ catch (error) {
74
+ console.error("Error:", error);
75
+ throw error;
96
76
  }
97
- else {
98
- key = key.toString();
99
- key = key.substring(4, key.length);
100
- key = key.substring(0, key.length - 4);
77
+ };
78
+ exports.decryptFile = decryptFile;
79
+ const handleExtraString = (key, append = true) => {
80
+ if (!key || key === "undefined")
101
81
  return key;
82
+ if (append) {
83
+ const prefix = crypto_1.default.randomBytes(2).toString("hex");
84
+ const suffix = crypto_1.default.randomBytes(2).toString("hex");
85
+ return `${prefix}${key}${suffix}`;
102
86
  }
87
+ return key.substring(4, key.length - 4);
103
88
  };
@@ -1,4 +1,2 @@
1
1
  import { Encryption } from "./model";
2
2
  export declare const processFileEncryptionDecryption: (data: string, encryption: Encryption, decrypt: boolean) => Promise<string>;
3
- export declare const encryptFile: (filePath: string, key: string) => Promise<string>;
4
- export declare const decryptFile: (encryptedFilePath: string, key: string) => Promise<string>;
@@ -1,134 +1,18 @@
1
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 __generator = (this && this.__generator) || function (thisArg, body) {
12
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
- function verb(n) { return function (v) { return step([n, v]); }; }
15
- function step(op) {
16
- if (f) throw new TypeError("Generator is already executing.");
17
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
- if (y = 0, t) op = [op[0] & 2, t.value];
20
- switch (op[0]) {
21
- case 0: case 1: t = op; break;
22
- case 4: _.label++; return { value: op[1], done: false };
23
- case 5: _.label++; y = op[1]; op = [0]; continue;
24
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
- default:
26
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
- if (t[2]) _.ops.pop();
31
- _.trys.pop(); continue;
32
- }
33
- op = body.call(thisArg, _);
34
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.processFileEncryptionDecryption = void 0;
4
+ const crypt_1 = require("./crypt");
5
+ const processFileEncryptionDecryption = async (data, encryption, decrypt) => {
6
+ const { encryptionType, dataKey } = encryption;
7
+ if (!data)
8
+ return data;
9
+ switch (encryptionType) {
10
+ case "KMS":
11
+ return decrypt
12
+ ? await (0, crypt_1.decryptFile)(data, dataKey)
13
+ : await (0, crypt_1.encryptFile)(data, dataKey);
14
+ default:
15
+ return data;
36
16
  }
37
17
  };
38
- var __importDefault = (this && this.__importDefault) || function (mod) {
39
- return (mod && mod.__esModule) ? mod : { "default": mod };
40
- };
41
- Object.defineProperty(exports, "__esModule", { value: true });
42
- exports.decryptFile = exports.encryptFile = exports.processFileEncryptionDecryption = void 0;
43
- var fs_1 = __importDefault(require("fs"));
44
- var crypto_1 = __importDefault(require("crypto"));
45
- var defaultAlgorithm = "aes-256-cbc";
46
- var processFileEncryptionDecryption = function (data, encryption, decrypt) { return __awaiter(void 0, void 0, void 0, function () {
47
- var encryptionType, dataKey, _a, result, _b;
48
- return __generator(this, function (_c) {
49
- switch (_c.label) {
50
- case 0:
51
- encryptionType = encryption.encryptionType, dataKey = encryption.dataKey;
52
- if (!data)
53
- return [2 /*return*/, data];
54
- _a = encryptionType;
55
- switch (_a) {
56
- case "KMS": return [3 /*break*/, 1];
57
- }
58
- return [3 /*break*/, 6];
59
- case 1:
60
- if (!decrypt) return [3 /*break*/, 3];
61
- return [4 /*yield*/, (0, exports.decryptFile)(data, dataKey)];
62
- case 2:
63
- _b = _c.sent();
64
- return [3 /*break*/, 5];
65
- case 3: return [4 /*yield*/, (0, exports.encryptFile)(data, dataKey)];
66
- case 4:
67
- _b = _c.sent();
68
- _c.label = 5;
69
- case 5:
70
- result = _b;
71
- return [2 /*return*/, result];
72
- case 6: return [2 /*return*/, data];
73
- }
74
- });
75
- }); };
76
18
  exports.processFileEncryptionDecryption = processFileEncryptionDecryption;
77
- var encryptFile = function (filePath, key) { return __awaiter(void 0, void 0, void 0, function () {
78
- var iv, keyBuffer, fileContent, cipher, encryptedDataBuffer, encryptedFilePath, error_1;
79
- return __generator(this, function (_a) {
80
- switch (_a.label) {
81
- case 0:
82
- _a.trys.push([0, 3, , 4]);
83
- iv = Buffer.from("i4mboZDwaNEC38YCzi77lw==", "base64");
84
- keyBuffer = Buffer.from(key, "base64");
85
- return [4 /*yield*/, fs_1.default.promises.readFile(filePath)];
86
- case 1:
87
- fileContent = _a.sent();
88
- cipher = crypto_1.default.createCipheriv(defaultAlgorithm, keyBuffer, iv);
89
- encryptedDataBuffer = cipher.update(fileContent);
90
- encryptedDataBuffer = Buffer.concat([encryptedDataBuffer, cipher.final()]);
91
- encryptedFilePath = filePath + ".enc";
92
- return [4 /*yield*/, fs_1.default.promises.writeFile(encryptedFilePath, encryptedDataBuffer)];
93
- case 2:
94
- _a.sent();
95
- console.log("File encrypted successfully.");
96
- return [2 /*return*/, encryptedFilePath];
97
- case 3:
98
- error_1 = _a.sent();
99
- console.error("\n Error: ", error_1);
100
- throw error_1;
101
- case 4: return [2 /*return*/];
102
- }
103
- });
104
- }); };
105
- exports.encryptFile = encryptFile;
106
- var decryptFile = function (encryptedFilePath, key) { return __awaiter(void 0, void 0, void 0, function () {
107
- var iv, keyBuffer, encryptedData, decipher, decryptedBuffer, decryptedFilePath, error_2;
108
- return __generator(this, function (_a) {
109
- switch (_a.label) {
110
- case 0:
111
- _a.trys.push([0, 3, , 4]);
112
- iv = Buffer.from("i4mboZDwaNEC38YCzi77lw==", "base64");
113
- keyBuffer = Buffer.from(key, "base64");
114
- return [4 /*yield*/, fs_1.default.promises.readFile(encryptedFilePath)];
115
- case 1:
116
- encryptedData = _a.sent();
117
- decipher = crypto_1.default.createDecipheriv(defaultAlgorithm, keyBuffer, iv);
118
- decryptedBuffer = decipher.update(encryptedData);
119
- decryptedBuffer = Buffer.concat([decryptedBuffer, decipher.final()]);
120
- decryptedFilePath = "".concat(encryptedFilePath.slice(0, -4), ".dec");
121
- return [4 /*yield*/, fs_1.default.promises.writeFile(decryptedFilePath, decryptedBuffer)];
122
- case 2:
123
- _a.sent();
124
- console.log("File decrypted successfully.");
125
- return [2 /*return*/, decryptedFilePath];
126
- case 3:
127
- error_2 = _a.sent();
128
- console.error("Error:", error_2);
129
- throw error_2;
130
- case 4: return [2 /*return*/];
131
- }
132
- });
133
- }); };
134
- exports.decryptFile = decryptFile;