drapcode-utility 2.7.3 → 2.7.4-sandbox.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 (148) hide show
  1. package/build/aws/aws-util.d.ts +6 -0
  2. package/build/aws/aws-util.d.ts.map +1 -0
  3. package/build/aws/aws-util.js +34 -0
  4. package/build/aws/aws-util.js.map +1 -0
  5. package/build/aws/encryption/KMS.d.ts +9 -0
  6. package/build/aws/encryption/KMS.d.ts.map +1 -0
  7. package/build/aws/encryption/KMS.js +65 -0
  8. package/build/aws/encryption/KMS.js.map +1 -0
  9. package/build/{encryption → aws/encryption}/crypt.d.ts +1 -0
  10. package/build/aws/encryption/crypt.d.ts.map +1 -0
  11. package/build/{encryption → aws/encryption}/crypt.js +25 -36
  12. package/build/aws/encryption/crypt.js.map +1 -0
  13. package/build/{encryption → aws/encryption}/file.d.ts +2 -1
  14. package/build/aws/encryption/file.d.ts.map +1 -0
  15. package/build/{encryption → aws/encryption}/file.js +2 -3
  16. package/build/aws/encryption/file.js.map +1 -0
  17. package/build/{encryption → aws/encryption}/index.d.ts +4 -3
  18. package/build/aws/encryption/index.d.ts.map +1 -0
  19. package/build/{encryption → aws/encryption}/index.js +41 -41
  20. package/build/aws/encryption/index.js.map +1 -0
  21. package/build/{encryption → aws/encryption}/utility.d.ts +1 -0
  22. package/build/aws/encryption/utility.d.ts.map +1 -0
  23. package/build/{encryption → aws/encryption}/utility.js +3 -2
  24. package/build/aws/encryption/utility.js.map +1 -0
  25. package/build/aws/model.d.ts +30 -0
  26. package/build/aws/model.d.ts.map +1 -0
  27. package/build/{encryption → aws}/model.js +1 -0
  28. package/build/aws/model.js.map +1 -0
  29. package/build/aws/s3-util.d.ts +36 -0
  30. package/build/aws/s3-util.d.ts.map +1 -0
  31. package/build/aws/s3-util.js +198 -0
  32. package/build/aws/s3-util.js.map +1 -0
  33. package/build/errors/app-error.d.ts +17 -4
  34. package/build/errors/app-error.d.ts.map +1 -0
  35. package/build/errors/app-error.js +184 -6
  36. package/build/errors/app-error.js.map +1 -0
  37. package/build/errors/async-handler.d.ts +3 -0
  38. package/build/errors/async-handler.d.ts.map +1 -0
  39. package/build/errors/async-handler.js +9 -0
  40. package/build/errors/async-handler.js.map +1 -0
  41. package/build/errors/axios-error.d.ts +2 -1
  42. package/build/errors/axios-error.d.ts.map +1 -0
  43. package/build/errors/axios-error.js +12 -8
  44. package/build/errors/axios-error.js.map +1 -0
  45. package/build/errors/bad-request-error.d.ts +2 -1
  46. package/build/errors/bad-request-error.d.ts.map +1 -0
  47. package/build/errors/bad-request-error.js +1 -0
  48. package/build/errors/bad-request-error.js.map +1 -0
  49. package/build/errors/custom-error.d.ts +1 -0
  50. package/build/errors/custom-error.d.ts.map +1 -0
  51. package/build/errors/custom-error.js +1 -0
  52. package/build/errors/custom-error.js.map +1 -0
  53. package/build/errors/error-classes.d.ts +23 -0
  54. package/build/errors/error-classes.d.ts.map +1 -0
  55. package/build/errors/error-classes.js +87 -0
  56. package/build/errors/error-classes.js.map +1 -0
  57. package/build/errors/error-codes.d.ts +47 -0
  58. package/build/errors/error-codes.d.ts.map +1 -0
  59. package/build/errors/error-codes.js +78 -0
  60. package/build/errors/error-codes.js.map +1 -0
  61. package/build/errors/error-obj.d.ts +10 -0
  62. package/build/errors/error-obj.d.ts.map +1 -0
  63. package/build/errors/error-obj.js +55 -0
  64. package/build/errors/error-obj.js.map +1 -0
  65. package/build/errors/get-error-details.d.ts +11 -0
  66. package/build/errors/get-error-details.d.ts.map +1 -0
  67. package/build/errors/get-error-details.js +28 -0
  68. package/build/errors/get-error-details.js.map +1 -0
  69. package/build/errors/not-found.d.ts +2 -1
  70. package/build/errors/not-found.d.ts.map +1 -0
  71. package/build/errors/not-found.js +3 -2
  72. package/build/errors/not-found.js.map +1 -0
  73. package/build/errors/wrap-error.d.ts +9 -0
  74. package/build/errors/wrap-error.d.ts.map +1 -0
  75. package/build/errors/wrap-error.js +17 -0
  76. package/build/errors/wrap-error.js.map +1 -0
  77. package/build/format-fields/index.d.ts +1 -0
  78. package/build/format-fields/index.d.ts.map +1 -0
  79. package/build/format-fields/index.js +13 -13
  80. package/build/format-fields/index.js.map +1 -0
  81. package/build/index.d.ts +25 -21
  82. package/build/index.d.ts.map +1 -0
  83. package/build/index.js +13 -7
  84. package/build/index.js.map +1 -0
  85. package/build/middlewares/redis/request-log.d.ts +1 -0
  86. package/build/middlewares/redis/request-log.d.ts.map +1 -0
  87. package/build/middlewares/redis/request-log.js +3 -2
  88. package/build/middlewares/redis/request-log.js.map +1 -0
  89. package/build/query/queryBuilder.d.ts +1 -0
  90. package/build/query/queryBuilder.d.ts.map +1 -0
  91. package/build/query/queryBuilder.js +66 -129
  92. package/build/query/queryBuilder.js.map +1 -0
  93. package/build/utils/check-error.d.ts +1 -0
  94. package/build/utils/check-error.d.ts.map +1 -0
  95. package/build/utils/check-error.js +14 -15
  96. package/build/utils/check-error.js.map +1 -0
  97. package/build/utils/common-util.d.ts +2 -1
  98. package/build/utils/common-util.d.ts.map +1 -0
  99. package/build/utils/common-util.js +46 -47
  100. package/build/utils/common-util.js.map +1 -0
  101. package/build/utils/date-util.d.ts +1 -0
  102. package/build/utils/date-util.d.ts.map +1 -0
  103. package/build/utils/date-util.js +59 -60
  104. package/build/utils/date-util.js.map +1 -0
  105. package/build/utils/file-util.d.ts +9 -0
  106. package/build/utils/file-util.d.ts.map +1 -0
  107. package/build/utils/file-util.js +120 -9
  108. package/build/utils/file-util.js.map +1 -0
  109. package/build/utils/prepare-query.d.ts +1 -0
  110. package/build/utils/prepare-query.d.ts.map +1 -0
  111. package/build/utils/prepare-query.js +15 -17
  112. package/build/utils/prepare-query.js.map +1 -0
  113. package/build/utils/project-util.d.ts +2 -1
  114. package/build/utils/project-util.d.ts.map +1 -0
  115. package/build/utils/project-util.js +45 -50
  116. package/build/utils/project-util.js.map +1 -0
  117. package/build/utils/query-parser.d.ts +1 -1
  118. package/build/utils/query-parser.d.ts.map +1 -0
  119. package/build/utils/query-parser.js +6 -568
  120. package/build/utils/query-parser.js.map +1 -0
  121. package/build/utils/query-utils.d.ts +1 -0
  122. package/build/utils/query-utils.d.ts.map +1 -0
  123. package/build/utils/query-utils.js +46 -50
  124. package/build/utils/query-utils.js.map +1 -0
  125. package/build/utils/rest-client.d.ts +1 -0
  126. package/build/utils/rest-client.d.ts.map +1 -0
  127. package/build/utils/rest-client.js +88 -122
  128. package/build/utils/rest-client.js.map +1 -0
  129. package/build/utils/token.d.ts +1 -0
  130. package/build/utils/token.d.ts.map +1 -0
  131. package/build/utils/token.js +4 -3
  132. package/build/utils/token.js.map +1 -0
  133. package/build/utils/util.d.ts +2 -1
  134. package/build/utils/util.d.ts.map +1 -0
  135. package/build/utils/util.js +340 -392
  136. package/build/utils/util.js.map +1 -0
  137. package/build/utils/uuid-generator.d.ts +1 -0
  138. package/build/utils/uuid-generator.d.ts.map +1 -0
  139. package/build/utils/uuid-generator.js +9 -8
  140. package/build/utils/uuid-generator.js.map +1 -0
  141. package/package.json +7 -6
  142. package/build/encryption/KMS.d.ts +0 -8
  143. package/build/encryption/KMS.js +0 -67
  144. package/build/encryption/model.d.ts +0 -17
  145. package/build/middlewares/error-logger.d.ts +0 -3
  146. package/build/middlewares/error-logger.js +0 -74
  147. package/build/utils/s3-util.d.ts +0 -36
  148. package/build/utils/s3-util.js +0 -275
@@ -1,275 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.processAndRemoveScriptFromSVG = exports.processAndRemoveExifDataFromImg = exports.fileUploadToS3 = exports.createS3Client = void 0;
7
- const drapcode_constant_1 = require("drapcode-constant");
8
- const client_s3_1 = require("@aws-sdk/client-s3");
9
- const fs_1 = __importDefault(require("fs"));
10
- const mime_types_1 = __importDefault(require("mime-types"));
11
- const lib_storage_1 = require("@aws-sdk/lib-storage");
12
- const encryption_1 = require("../encryption");
13
- const uuid_1 = require("uuid");
14
- const gm_1 = __importDefault(require("gm"));
15
- const svgo_1 = require("svgo");
16
- const promises_1 = __importDefault(require("fs/promises"));
17
- const exiftool_vendored_1 = require("exiftool-vendored");
18
- const exiftool = new exiftool_vendored_1.ExifTool({});
19
- const createS3Client = (awsConfig) => {
20
- if (!awsConfig) {
21
- return null;
22
- }
23
- const { region, accessKey, accessSecret } = awsConfig;
24
- const s3client = new client_s3_1.S3Client({
25
- region,
26
- credentials: {
27
- accessKeyId: accessKey,
28
- secretAccessKey: accessSecret,
29
- },
30
- });
31
- return s3client;
32
- };
33
- exports.createS3Client = createS3Client;
34
- const fileUploadToS3 = async (files, s3Client, s3Config, publicS3Client, publicS3Config, isGenerateIcons, encryption, options = {}, encrypted = false) => {
35
- if (Array.isArray(files)) {
36
- console.log("Multiple file");
37
- const fileOptions = [];
38
- const s3Key = s3Config.key;
39
- for (const file of files) {
40
- const fileSlug = slugifyFileName(file.originalname);
41
- if (s3Config.append) {
42
- s3Config.key = `${s3Key}/${(0, uuid_1.v4)()}/${fileSlug}`;
43
- }
44
- const fileObj = await processFileUploadToS3(file, s3Client, s3Config, publicS3Client, publicS3Config, isGenerateIcons, encryption, options, encrypted);
45
- if (fileObj) {
46
- fileOptions.push(fileObj);
47
- }
48
- }
49
- return fileOptions;
50
- }
51
- else {
52
- console.log("Single file");
53
- const fileSlug = slugifyFileName(files.originalname);
54
- if (s3Config.append) {
55
- s3Config.key = `${s3Config.key}/${(0, uuid_1.v4)()}/${fileSlug}`;
56
- }
57
- console.log("encryption :>> ", encryption);
58
- return await processFileUploadToS3(files, s3Client, s3Config, publicS3Client, publicS3Config, isGenerateIcons, encryption, options, encrypted);
59
- }
60
- };
61
- exports.fileUploadToS3 = fileUploadToS3;
62
- const processFileUploadToS3 = async (file, s3Client, s3Config, publicS3Client, publicS3Config, isGenerateIcons, encryption, options = {}, encrypt = false) => {
63
- try {
64
- console.log("encrypt :>> ", encrypt);
65
- const contentType = mime_types_1.default.lookup(file.originalname) || "";
66
- console.log("s3Config :>> ", s3Config);
67
- const { acl, key, bucket } = s3Config;
68
- let params = null;
69
- let encryptedFilePath = null;
70
- if (encrypt) {
71
- encryptedFilePath = await (0, encryption_1.cryptFile)(file.path, encryption, false);
72
- params = {
73
- Bucket: bucket,
74
- ACL: acl,
75
- Key: key,
76
- Body: fs_1.default.createReadStream(encryptedFilePath),
77
- ContentType: contentType,
78
- ...options,
79
- };
80
- }
81
- else {
82
- params = {
83
- Bucket: bucket,
84
- ACL: acl,
85
- Key: key,
86
- Body: fs_1.default.createReadStream(file.path),
87
- ContentType: contentType,
88
- ...options,
89
- };
90
- }
91
- console.log("encryptedFilePath :>> ", encryptedFilePath);
92
- console.log("params :>> ", params);
93
- const parallelUploads3 = new lib_storage_1.Upload({ client: s3Client, params });
94
- const data = await parallelUploads3.done();
95
- const { smallIcon, mediumIcon, largeIcon } = await handleIconUpload(file, contentType, publicS3Client, publicS3Config, isGenerateIcons, key, options);
96
- if (data) {
97
- if (encryptedFilePath) {
98
- fs_1.default.unlinkSync(encryptedFilePath);
99
- }
100
- fs_1.default.unlinkSync(file.path);
101
- }
102
- const { originalname, mimetype, size } = file;
103
- return {
104
- originalname,
105
- mimetype,
106
- size,
107
- key: data.Key,
108
- contentType,
109
- smallIcon,
110
- mediumIcon,
111
- largeIcon,
112
- };
113
- }
114
- catch (error) {
115
- console.error("Error uploading file to S3:", error);
116
- return null;
117
- }
118
- };
119
- const imageTypeFiles = [...drapcode_constant_1.imageMimeTypes, "image/jpg"];
120
- const processAndRemoveExifDataFromImg = async (files) => {
121
- try {
122
- files = Array.isArray(files) ? files : [files];
123
- files = files.filter((file) => imageTypeFiles.includes(file.mimetype));
124
- let promises = [];
125
- if (files.length) {
126
- promises = files.map(async (file) => {
127
- // Remove EXIF data using exiftool
128
- return await exiftool
129
- .write(file.path, {}, { writeArgs: ["-all=", "-overwrite_original"] })
130
- .catch((err) => {
131
- console.error("Error processing image:", err);
132
- throw err;
133
- });
134
- });
135
- await Promise.all(promises);
136
- }
137
- return promises;
138
- }
139
- catch (error) {
140
- console.error("\n error :>> ", error);
141
- }
142
- };
143
- exports.processAndRemoveExifDataFromImg = processAndRemoveExifDataFromImg;
144
- const processAndRemoveScriptFromSVG = async (files) => {
145
- if (!files || !files.length) {
146
- return;
147
- }
148
- try {
149
- files = Array.isArray(files) ? files : [files];
150
- files = files.filter((file) => file.mimetype === "image/svg+xml");
151
- if (!files || !files.length) {
152
- return;
153
- }
154
- let promises = files.map(async (file) => {
155
- const fileContent = await promises_1.default.readFile(file.path, "utf-8");
156
- const sanitizedContent = (0, svgo_1.optimize)(fileContent, {
157
- plugins: [
158
- "removeScriptElement", // Remove <script>,
159
- "removeDesc",
160
- "removeTitle",
161
- "removeMetadata",
162
- "removeComments",
163
- "removeXMLProcInst",
164
- "removeDoctype",
165
- "cleanupAttrs",
166
- {
167
- name: "removeAttrs", // Plugin to remove event handlers (e.g., onclick)
168
- params: {
169
- attrs: "(on.*)", // Matches attributes starting with "on" (e.g., onclick, onmouseover)
170
- },
171
- },
172
- {
173
- name: "removeForeignObject", // Custom plugin to remove <foreignObject>
174
- fn: (item) => {
175
- if (item.name === "foreignObject") {
176
- return null; // Remove this element
177
- }
178
- else
179
- return item;
180
- },
181
- },
182
- ],
183
- });
184
- await promises_1.default.writeFile(file.path, sanitizedContent.data, "utf-8");
185
- });
186
- await Promise.all(promises);
187
- }
188
- catch (error) {
189
- console.error("\n error :>> ", error);
190
- }
191
- };
192
- exports.processAndRemoveScriptFromSVG = processAndRemoveScriptFromSVG;
193
- const handleIconUpload = async (file, contentType, publicS3Client, publicS3Config, isGenerateIcons, key, options = {}) => {
194
- const { path: filePath, size: fileSize } = file;
195
- const iconSizes = [200, 300, 500];
196
- const iconLabels = ["small", "medium", "large"];
197
- try {
198
- if (isGenerateIcons &&
199
- fileSize < 157286400 && //150mb
200
- [...drapcode_constant_1.imageMimeTypes, "application/pdf", "image/gif"].includes(contentType)) {
201
- const iconFiles = await generateDynamicIcons(filePath, iconSizes, contentType);
202
- const { bucket, acl } = publicS3Config;
203
- const iconUploads = iconFiles.map(async (iconFilePath, index) => {
204
- const iconLabel = iconLabels[index];
205
- const iconKey = `${key}/icons/${(0, uuid_1.v4)()}_${iconLabel}.png`;
206
- const iconParams = {
207
- Bucket: bucket,
208
- ACL: acl,
209
- Key: iconKey,
210
- Body: fs_1.default.createReadStream(iconFilePath),
211
- ContentType: "image/png",
212
- ...options,
213
- };
214
- const iconUpload = new lib_storage_1.Upload({
215
- client: publicS3Client,
216
- params: iconParams,
217
- });
218
- const iconData = await iconUpload.done();
219
- fs_1.default.unlinkSync(iconFilePath);
220
- return iconData.Key;
221
- });
222
- const [smallKey, mediumKey, largeKey] = await Promise.all(iconUploads);
223
- return {
224
- smallIcon: generateS3IconUrl(bucket, smallKey),
225
- mediumIcon: generateS3IconUrl(bucket, mediumKey),
226
- largeIcon: generateS3IconUrl(bucket, largeKey),
227
- };
228
- }
229
- else {
230
- return generateStaticIcons(contentType);
231
- }
232
- }
233
- catch (iconError) {
234
- console.error("Error generating or uploading icons:", iconError);
235
- return generateStaticIcons(contentType);
236
- }
237
- };
238
- const generateS3IconUrl = (bucket, key) => {
239
- if (!key)
240
- return null;
241
- return `https://${bucket}.s3.amazonaws.com/${key}`;
242
- };
243
- const generateDynamicIcons = (filePath, sizes, contentType) => {
244
- return Promise.all(sizes.map((size) => {
245
- const outputFilePath = `${filePath}_${size}.png`;
246
- return new Promise((resolve, reject) => {
247
- let gmInstance = (0, gm_1.default)(filePath);
248
- if (contentType === "application/pdf") {
249
- gmInstance = gmInstance.selectFrame(0);
250
- }
251
- gmInstance.resize(size, size).write(outputFilePath, (err) => {
252
- if (err) {
253
- reject(err);
254
- }
255
- else {
256
- resolve(outputFilePath);
257
- }
258
- });
259
- });
260
- }));
261
- };
262
- const generateStaticIcons = (type) => {
263
- const icon = drapcode_constant_1.iconMaps[type] || drapcode_constant_1.defaultIcon;
264
- return {
265
- smallIcon: icon,
266
- mediumIcon: icon,
267
- largeIcon: icon,
268
- };
269
- };
270
- const slugifyFileName = (fileName) => {
271
- return fileName
272
- .replace(/\s+/g, "_") // Replace spaces with underscores
273
- .replace(/[^a-zA-Z0-9._]/g, "") // Remove anything that's not a letter, number, dot, or underscore
274
- .toLowerCase(); // Convert to lowercase
275
- };