drapcode-utility 1.1.8 → 1.2.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.
@@ -4,11 +4,6 @@ export declare const processKMSEncryption: (config: AwsConfig, arn: string, plai
4
4
  data: string;
5
5
  message: string;
6
6
  }>;
7
- export declare const processKMSDecryption: (config: AwsConfig, cipherText: string, context: any) => Promise<{
8
- status: string;
9
- data: string;
10
- message: string;
11
- }>;
12
7
  export declare const processKMSGenerateDataKey: (config: AwsConfig, arn: string) => Promise<{
13
8
  status: string;
14
9
  dataKey: string;
@@ -36,7 +36,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
36
36
  }
37
37
  };
38
38
  Object.defineProperty(exports, "__esModule", { value: true });
39
- exports.processKMSGenerateDataKey = exports.processKMSDecryption = exports.processKMSEncryption = void 0;
39
+ exports.processKMSGenerateDataKey = exports.processKMSEncryption = void 0;
40
40
  var client_kms_1 = require("@aws-sdk/client-kms");
41
41
  var processKMSEncryption = function (config, arn, plainText, context) { return __awaiter(void 0, void 0, void 0, function () {
42
42
  var accessKeyId, secretAccessKey, region, client, input, command, response, cipherText, error_1;
@@ -72,38 +72,8 @@ var processKMSEncryption = function (config, arn, plainText, context) { return _
72
72
  });
73
73
  }); };
74
74
  exports.processKMSEncryption = processKMSEncryption;
75
- var processKMSDecryption = function (config, cipherText, context) { return __awaiter(void 0, void 0, void 0, function () {
76
- var accessKeyId, secretAccessKey, region, client, dcipherText, input, command, response, plainText, 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
- client = new client_kms_1.KMSClient({
83
- region: region,
84
- credentials: { accessKeyId: accessKeyId, secretAccessKey: secretAccessKey },
85
- });
86
- dcipherText = Buffer.from(cipherText, "base64");
87
- input = {
88
- CiphertextBlob: dcipherText,
89
- EncryptionContext: context,
90
- };
91
- command = new client_kms_1.DecryptCommand(input);
92
- return [4 /*yield*/, client.send(command)];
93
- case 1:
94
- response = _a.sent();
95
- plainText = Buffer.from(response.Plaintext).toString("base64");
96
- return [2 /*return*/, { status: "SUCCESS", data: plainText, message: "" }];
97
- case 2:
98
- error_2 = _a.sent();
99
- return [2 /*return*/, { status: "FAILED", data: "", message: error_2.message }];
100
- case 3: return [2 /*return*/];
101
- }
102
- });
103
- }); };
104
- exports.processKMSDecryption = processKMSDecryption;
105
75
  var processKMSGenerateDataKey = function (config, arn) { return __awaiter(void 0, void 0, void 0, function () {
106
- var accessKeyId, secretAccessKey, region, input, client, command, response, cipherText, error_3;
76
+ var accessKeyId, secretAccessKey, region, input, client, command, response, cipherText, error_2;
107
77
  return __generator(this, function (_a) {
108
78
  switch (_a.label) {
109
79
  case 0:
@@ -124,10 +94,10 @@ var processKMSGenerateDataKey = function (config, arn) { return __awaiter(void 0
124
94
  cipherText = Buffer.from(response.CiphertextBlob).toString("base64");
125
95
  return [2 /*return*/, { status: "SUCCESS", dataKey: cipherText, message: "" }];
126
96
  case 2:
127
- error_3 = _a.sent();
97
+ error_2 = _a.sent();
128
98
  return [2 /*return*/, {
129
99
  status: "FAILED",
130
- message: error_3.message,
100
+ message: error_2.message,
131
101
  dataKey: "",
132
102
  }];
133
103
  case 3: return [2 /*return*/];
@@ -46,7 +46,8 @@ export declare const drapcodeEncryptDecrypt: (data: string, encrypt: boolean) =>
46
46
  data: string;
47
47
  message: string;
48
48
  }>;
49
- export declare const cryptFile: (filePath: any, encryption: Encryption, decrypt: boolean) => Promise<string | {
49
+ export declare const cryptFile: (filePath: any, encryption: Encryption, decrypt: boolean) => Promise<string>;
50
+ export declare const processKMSDecryption: (config: AwsConfig, cipherText: string, context: any) => Promise<{
50
51
  status: string;
51
52
  data: string;
52
53
  message: string;
@@ -36,7 +36,8 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
36
36
  }
37
37
  };
38
38
  Object.defineProperty(exports, "__esModule", { value: true });
39
- exports.cryptFile = exports.drapcodeEncryptDecrypt = exports.decryptDataWithKMS = exports.encryptDataWithKMS = exports.createKMSDataKey = exports.processDataEncryptionDecryption = exports.processItemEncryptDecrypt = exports.crypt = void 0;
39
+ exports.processKMSDecryption = exports.cryptFile = exports.drapcodeEncryptDecrypt = exports.decryptDataWithKMS = exports.encryptDataWithKMS = exports.createKMSDataKey = exports.processDataEncryptionDecryption = exports.processItemEncryptDecrypt = exports.crypt = void 0;
40
+ var client_kms_1 = require("@aws-sdk/client-kms");
40
41
  var crypt_1 = require("./crypt");
41
42
  var file_1 = require("./file");
42
43
  var KMS_1 = require("./KMS");
@@ -52,7 +53,7 @@ var crypt = function (data, fields, encryption, decrypt) { return __awaiter(void
52
53
  accessKeyId: accessKeyId,
53
54
  secretAccessKey: secretAccessKey,
54
55
  };
55
- return [4 /*yield*/, (0, KMS_1.processKMSDecryption)(config, encryption.dataKey, {})];
56
+ return [4 /*yield*/, (0, exports.processKMSDecryption)(config, encryption.dataKey, {})];
56
57
  case 1:
57
58
  plainTextData = _b.sent();
58
59
  if (plainTextData.status === "FAILED") {
@@ -201,7 +202,7 @@ var decryptDataWithKMS = function (config, cipherText, context) { return __await
201
202
  var plainTextData;
202
203
  return __generator(this, function (_a) {
203
204
  switch (_a.label) {
204
- case 0: return [4 /*yield*/, (0, KMS_1.processKMSDecryption)(config, cipherText, context)];
205
+ case 0: return [4 /*yield*/, (0, exports.processKMSDecryption)(config, cipherText, context)];
205
206
  case 1:
206
207
  plainTextData = _a.sent();
207
208
  return [2 /*return*/, plainTextData];
@@ -240,7 +241,7 @@ var drapcodeEncryptDecrypt = function (data, encrypt) { return __awaiter(void 0,
240
241
  accessKeyId: accessKey,
241
242
  secretAccessKey: secretKey,
242
243
  };
243
- return [4 /*yield*/, (0, KMS_1.processKMSDecryption)(config, privateDataKey, {})];
244
+ return [4 /*yield*/, (0, exports.processKMSDecryption)(config, privateDataKey, {})];
244
245
  case 1:
245
246
  plainTextData = _a.sent();
246
247
  if (plainTextData.status === "FAILED") {
@@ -266,31 +267,44 @@ var drapcodeEncryptDecrypt = function (data, encrypt) { return __awaiter(void 0,
266
267
  }); };
267
268
  exports.drapcodeEncryptDecrypt = drapcodeEncryptDecrypt;
268
269
  var cryptFile = function (filePath, encryption, decrypt) { return __awaiter(void 0, void 0, void 0, function () {
269
- var awsConfig, encryptionType, dataKey, accessKeyId, secretAccessKey, region, config, plainTextData, data;
270
+ var data;
270
271
  return __generator(this, function (_a) {
271
272
  switch (_a.label) {
272
- case 0:
273
- awsConfig = encryption.awsConfig, encryptionType = encryption.encryptionType, dataKey = encryption.dataKey;
274
- if (!(encryptionType === "KMS")) return [3 /*break*/, 2];
275
- accessKeyId = awsConfig.accessKeyId, secretAccessKey = awsConfig.secretAccessKey, region = awsConfig.region;
276
- config = {
277
- region: region,
278
- accessKeyId: accessKeyId,
279
- secretAccessKey: secretAccessKey,
280
- };
281
- return [4 /*yield*/, (0, KMS_1.processKMSDecryption)(config, dataKey, {})];
273
+ case 0: return [4 /*yield*/, (0, file_1.processFileEncryptionDecryption)(filePath, encryption, decrypt)];
282
274
  case 1:
283
- plainTextData = _a.sent();
284
- if (plainTextData.status === "FAILED") {
285
- return [2 /*return*/, plainTextData];
286
- }
287
- encryption.dataKey = plainTextData.data;
288
- _a.label = 2;
289
- case 2: return [4 /*yield*/, (0, file_1.processFileEncryptionDecryption)(filePath, encryption, decrypt)];
290
- case 3:
291
275
  data = _a.sent();
292
276
  return [2 /*return*/, data];
293
277
  }
294
278
  });
295
279
  }); };
296
280
  exports.cryptFile = cryptFile;
281
+ var processKMSDecryption = function (config, cipherText, context) { return __awaiter(void 0, void 0, void 0, function () {
282
+ var accessKeyId, secretAccessKey, region, client, dcipherText, input, command, response, plainText, error_1;
283
+ return __generator(this, function (_a) {
284
+ switch (_a.label) {
285
+ case 0:
286
+ _a.trys.push([0, 2, , 3]);
287
+ accessKeyId = config.accessKeyId, secretAccessKey = config.secretAccessKey, region = config.region;
288
+ client = new client_kms_1.KMSClient({
289
+ region: region,
290
+ credentials: { accessKeyId: accessKeyId, secretAccessKey: secretAccessKey },
291
+ });
292
+ dcipherText = Buffer.from(cipherText, "base64");
293
+ input = {
294
+ CiphertextBlob: dcipherText,
295
+ EncryptionContext: context,
296
+ };
297
+ command = new client_kms_1.DecryptCommand(input);
298
+ return [4 /*yield*/, client.send(command)];
299
+ case 1:
300
+ response = _a.sent();
301
+ plainText = Buffer.from(response.Plaintext).toString("base64");
302
+ return [2 /*return*/, { status: "SUCCESS", data: plainText, message: "" }];
303
+ case 2:
304
+ error_1 = _a.sent();
305
+ return [2 /*return*/, { status: "FAILED", data: "", message: error_1.message }];
306
+ case 3: return [2 /*return*/];
307
+ }
308
+ });
309
+ }); };
310
+ exports.processKMSDecryption = processKMSDecryption;
@@ -13,6 +13,10 @@ interface S3Config {
13
13
  }
14
14
  export declare const createS3Client: (awsConfig: AWSConfig) => S3Client | null;
15
15
  export declare const fileUploadToS3: (files: any, s3Client: S3Client, s3Config: S3Config, encryption: Encryption, options?: {}) => Promise<{
16
+ status: string;
17
+ data: string;
18
+ message: string;
19
+ } | {
16
20
  originalname: any;
17
21
  mimetype: any;
18
22
  size: any;
@@ -56,6 +56,7 @@ var fs_1 = __importDefault(require("fs"));
56
56
  var mime_types_1 = __importDefault(require("mime-types"));
57
57
  var lib_storage_1 = require("@aws-sdk/lib-storage");
58
58
  var encryption_1 = require("../encryption");
59
+ var encryption_2 = require("../encryption");
59
60
  var createS3Client = function (awsConfig) {
60
61
  if (!awsConfig) {
61
62
  return null;
@@ -74,11 +75,28 @@ exports.createS3Client = createS3Client;
74
75
  var fileUploadToS3 = function (files, s3Client, s3Config, encryption, options) {
75
76
  if (options === void 0) { options = {}; }
76
77
  return __awaiter(void 0, void 0, void 0, function () {
77
- var fileOptions, s3Key, _a, _b, _c, _i, index, file, fileObj, fileObj;
78
+ var awsConfig, encryptionType, dataKey, accessKeyId, secretAccessKey, region, config, plainTextData, fileOptions, s3Key, _a, _b, _c, _i, index, file, fileObj, fileObj;
78
79
  return __generator(this, function (_d) {
79
80
  switch (_d.label) {
80
81
  case 0:
81
- if (!Array.isArray(files)) return [3 /*break*/, 5];
82
+ awsConfig = encryption.awsConfig, encryptionType = encryption.encryptionType, dataKey = encryption.dataKey;
83
+ if (!(encryptionType === "KMS")) return [3 /*break*/, 2];
84
+ accessKeyId = awsConfig.accessKeyId, secretAccessKey = awsConfig.secretAccessKey, region = awsConfig.region;
85
+ config = {
86
+ region: region,
87
+ accessKeyId: accessKeyId,
88
+ secretAccessKey: secretAccessKey,
89
+ };
90
+ return [4 /*yield*/, (0, encryption_2.processKMSDecryption)(config, dataKey, {})];
91
+ case 1:
92
+ plainTextData = _d.sent();
93
+ if (plainTextData.status === "FAILED") {
94
+ return [2 /*return*/, plainTextData];
95
+ }
96
+ encryption.dataKey = plainTextData.data;
97
+ _d.label = 2;
98
+ case 2:
99
+ if (!Array.isArray(files)) return [3 /*break*/, 7];
82
100
  console.log("I have multiple file");
83
101
  fileOptions = [];
84
102
  s3Key = s3Config.key;
@@ -87,34 +105,34 @@ var fileUploadToS3 = function (files, s3Client, s3Config, encryption, options) {
87
105
  for (_c in _a)
88
106
  _b.push(_c);
89
107
  _i = 0;
90
- _d.label = 1;
91
- case 1:
92
- if (!(_i < _b.length)) return [3 /*break*/, 4];
108
+ _d.label = 3;
109
+ case 3:
110
+ if (!(_i < _b.length)) return [3 /*break*/, 6];
93
111
  _c = _b[_i];
94
- if (!(_c in _a)) return [3 /*break*/, 3];
112
+ if (!(_c in _a)) return [3 /*break*/, 5];
95
113
  index = _c;
96
114
  file = files[index];
97
115
  if (s3Config.append) {
98
116
  s3Config.key = "".concat(s3Key, "/").concat(file.originalname);
99
117
  }
100
118
  return [4 /*yield*/, processFileUploadToS3(file, s3Client, s3Config, encryption, options, encryption ? true : false)];
101
- case 2:
119
+ case 4:
102
120
  fileObj = _d.sent();
103
121
  if (fileObj) {
104
122
  fileOptions.push(fileObj);
105
123
  }
106
- _d.label = 3;
107
- case 3:
108
- _i++;
109
- return [3 /*break*/, 1];
110
- case 4: return [2 /*return*/, fileOptions];
124
+ _d.label = 5;
111
125
  case 5:
126
+ _i++;
127
+ return [3 /*break*/, 3];
128
+ case 6: return [2 /*return*/, fileOptions];
129
+ case 7:
112
130
  console.log("I have single file");
113
131
  if (s3Config.append) {
114
132
  s3Config.key = "".concat(s3Config.key, "/").concat(files.originalname);
115
133
  }
116
134
  return [4 /*yield*/, processFileUploadToS3(files, s3Client, s3Config, encryption, options, encryption ? true : false)];
117
- case 6:
135
+ case 8:
118
136
  fileObj = _d.sent();
119
137
  return [2 /*return*/, fileObj];
120
138
  }
@@ -160,7 +160,7 @@ var stringToExpression = function (str) {
160
160
  };
161
161
  exports.stringToExpression = stringToExpression;
162
162
  var cleanCollectionAndFieldName = function (str) {
163
- str = str.replace(/[^a-zA-Z0-9 ]/, "");
163
+ str = str.replace(/[^A-Za-z0-9\s_]/, "");
164
164
  return str;
165
165
  };
166
166
  exports.cleanCollectionAndFieldName = cleanCollectionAndFieldName;
@@ -224,7 +224,7 @@ var convertItemToArray = function (itemValue) {
224
224
  };
225
225
  exports.convertItemToArray = convertItemToArray;
226
226
  var validateAlphanumericString = function (str) {
227
- if (/[^a-zA-Z0-9 ]/.test(str)) {
227
+ if (/[^A-Za-z0-9\s_]/.test(str)) {
228
228
  return "String should contain only numbers and letters";
229
229
  }
230
230
  return false;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "drapcode-utility",
3
- "version": "1.1.8",
3
+ "version": "1.2.0",
4
4
  "description": "",
5
5
  "main": "build/index.js",
6
6
  "types": "build/index.d.ts",