@resolveio/server-lib 22.2.41 → 22.2.43

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.
@@ -67,6 +67,7 @@ export declare class MethodManager {
67
67
  private getTextMessageProvider;
68
68
  safeStringify(obj: any): string;
69
69
  getCircularReplacer(): (key: any, value: any) => any;
70
+ private finalizeEmailHistoryStorage;
70
71
  private tryMergeEmailOccurrence;
71
72
  sendEmail(sendTo: string, subject: string, text?: string | null, html?: string | null, attachments?: any[] | null, send_from?: string | null, reply_to?: string | null, force_ses?: boolean, local_override?: boolean, options?: SendEmailOptions): Promise<true | EmailHistoryModel>;
72
73
  getAWS(): AWS;
@@ -164,6 +164,150 @@ function formatGroupedEmailContent(correlationId, occurrences) {
164
164
  html: html
165
165
  };
166
166
  }
167
+ var EMAIL_HISTORY_TEXT_MAX_CHARS = 12000;
168
+ var EMAIL_HISTORY_HTML_MAX_CHARS = 20000;
169
+ var EMAIL_HISTORY_ERROR_MAX_CHARS = 8000;
170
+ var EMAIL_HISTORY_META_MAX_CHARS = 6000;
171
+ var EMAIL_HISTORY_ATTACHMENT_MAX_COUNT = 10;
172
+ var EMAIL_HISTORY_ATTACHMENT_FIELD_MAX_CHARS = 1024;
173
+ var EMAIL_HISTORY_OCCURRENCE_MAX_COUNT = 20;
174
+ var EMAIL_HISTORY_OCCURRENCE_TEXT_MAX_CHARS = 4000;
175
+ var EMAIL_HISTORY_OCCURRENCE_HTML_MAX_CHARS = 6000;
176
+ function truncateTextForHistory(value, maxChars) {
177
+ if (typeof value !== 'string') {
178
+ return '';
179
+ }
180
+ if (value.length <= maxChars) {
181
+ return value;
182
+ }
183
+ var suffix = '...[truncated]';
184
+ var keepLength = Math.max(0, maxChars - suffix.length);
185
+ return "".concat(value.slice(0, keepLength)).concat(suffix);
186
+ }
187
+ function sanitizeMetaForHistory(meta) {
188
+ if (!meta || typeof meta !== 'object') {
189
+ return undefined;
190
+ }
191
+ try {
192
+ var serialized = JSON.stringify(meta);
193
+ if (!serialized) {
194
+ return undefined;
195
+ }
196
+ if (serialized.length <= EMAIL_HISTORY_META_MAX_CHARS) {
197
+ return JSON.parse(serialized);
198
+ }
199
+ return {
200
+ truncated: true,
201
+ originalSize: serialized.length,
202
+ preview: truncateTextForHistory(serialized, EMAIL_HISTORY_META_MAX_CHARS)
203
+ };
204
+ }
205
+ catch (_a) {
206
+ return {
207
+ truncated: true,
208
+ preview: '[unserializable meta]'
209
+ };
210
+ }
211
+ }
212
+ function estimateAttachmentContentLength(content, encoding) {
213
+ if (Buffer.isBuffer(content)) {
214
+ return content.length;
215
+ }
216
+ if (content instanceof Uint8Array) {
217
+ return content.byteLength;
218
+ }
219
+ if (content && typeof content === 'object') {
220
+ if (content._bsontype === 'Binary' && content.buffer && typeof content.buffer.length === 'number') {
221
+ return content.buffer.length;
222
+ }
223
+ if (Array.isArray(content.data)) {
224
+ return content.data.length;
225
+ }
226
+ }
227
+ if (typeof content === 'string') {
228
+ if (encoding === 'base64') {
229
+ var normalized = content.replace(/\s/g, '');
230
+ var padding = normalized.endsWith('==') ? 2 : normalized.endsWith('=') ? 1 : 0;
231
+ return Math.max(0, Math.floor((normalized.length * 3) / 4) - padding);
232
+ }
233
+ return Buffer.byteLength(content, 'utf8');
234
+ }
235
+ return undefined;
236
+ }
237
+ function sanitizeAttachmentForHistory(attachment) {
238
+ var source = attachment && typeof attachment === 'object' ? attachment : {};
239
+ var summary = {
240
+ contentStored: false
241
+ };
242
+ if (typeof source.filename === 'string') {
243
+ summary.filename = truncateTextForHistory(source.filename, EMAIL_HISTORY_ATTACHMENT_FIELD_MAX_CHARS);
244
+ }
245
+ if (typeof source.contentType === 'string') {
246
+ summary.contentType = truncateTextForHistory(source.contentType, EMAIL_HISTORY_ATTACHMENT_FIELD_MAX_CHARS);
247
+ }
248
+ if (typeof source.contentDisposition === 'string') {
249
+ summary.contentDisposition = truncateTextForHistory(source.contentDisposition, EMAIL_HISTORY_ATTACHMENT_FIELD_MAX_CHARS);
250
+ }
251
+ if (typeof source.cid === 'string') {
252
+ summary.cid = truncateTextForHistory(source.cid, EMAIL_HISTORY_ATTACHMENT_FIELD_MAX_CHARS);
253
+ }
254
+ if (typeof source.path === 'string') {
255
+ summary.path = truncateTextForHistory(source.path, EMAIL_HISTORY_ATTACHMENT_FIELD_MAX_CHARS);
256
+ }
257
+ if (typeof source.href === 'string') {
258
+ summary.href = truncateTextForHistory(source.href, EMAIL_HISTORY_ATTACHMENT_FIELD_MAX_CHARS);
259
+ }
260
+ if (typeof source.encoding === 'string') {
261
+ summary.encoding = truncateTextForHistory(source.encoding, EMAIL_HISTORY_ATTACHMENT_FIELD_MAX_CHARS);
262
+ }
263
+ var contentLength = typeof source.size === 'number'
264
+ ? source.size
265
+ : estimateAttachmentContentLength(source.content, source.encoding);
266
+ if (typeof contentLength === 'number' && Number.isFinite(contentLength) && contentLength >= 0) {
267
+ summary.contentLength = contentLength;
268
+ }
269
+ return summary;
270
+ }
271
+ function sanitizeAttachmentsForHistory(attachments) {
272
+ if (!Array.isArray(attachments) || !attachments.length) {
273
+ return [];
274
+ }
275
+ var summarized = attachments
276
+ .slice(0, EMAIL_HISTORY_ATTACHMENT_MAX_COUNT)
277
+ .map(function (attachment) { return sanitizeAttachmentForHistory(attachment); });
278
+ if (attachments.length > EMAIL_HISTORY_ATTACHMENT_MAX_COUNT) {
279
+ summarized.push({
280
+ contentStored: false,
281
+ omittedCount: attachments.length - EMAIL_HISTORY_ATTACHMENT_MAX_COUNT
282
+ });
283
+ }
284
+ return summarized;
285
+ }
286
+ function sanitizeOccurrencesForHistory(occurrences) {
287
+ if (!Array.isArray(occurrences) || !occurrences.length) {
288
+ return undefined;
289
+ }
290
+ return occurrences.slice(-EMAIL_HISTORY_OCCURRENCE_MAX_COUNT).map(function (occurrence) {
291
+ var createdAtCandidate = occurrence.createdAt instanceof Date ? occurrence.createdAt : new Date(occurrence.createdAt);
292
+ var createdAt = Number.isNaN(createdAtCandidate.getTime()) ? new Date() : createdAtCandidate;
293
+ var sanitized = {
294
+ _id: occurrence._id || (0, common_1.objectIdHexString)(),
295
+ createdAt: createdAt,
296
+ subject: truncateTextForHistory(occurrence.subject || '', EMAIL_HISTORY_ATTACHMENT_FIELD_MAX_CHARS)
297
+ };
298
+ if (typeof occurrence.text === 'string' && occurrence.text.length > 0) {
299
+ sanitized.text = truncateTextForHistory(occurrence.text, EMAIL_HISTORY_OCCURRENCE_TEXT_MAX_CHARS);
300
+ }
301
+ if (typeof occurrence.html === 'string' && occurrence.html.length > 0) {
302
+ sanitized.html = truncateTextForHistory(occurrence.html, EMAIL_HISTORY_OCCURRENCE_HTML_MAX_CHARS);
303
+ }
304
+ var sanitizedMeta = sanitizeMetaForHistory(occurrence.meta);
305
+ if (sanitizedMeta) {
306
+ sanitized.meta = sanitizedMeta;
307
+ }
308
+ return sanitized;
309
+ });
310
+ }
167
311
  function createNodeMailerSESV2Transport(sesClient) {
168
312
  var _this = this;
169
313
  var transport = {
@@ -1347,13 +1491,7 @@ var MethodManager = /** @class */ (function () {
1347
1491
  _a.trys.push([1, 8, 10, 14]);
1348
1492
  if (!err) return [3 /*break*/, 3];
1349
1493
  console.error('Failed to send email:', err);
1350
- return [4 /*yield*/, email_history_collection_1.EmailHistories.updateOne({ _id: emailHistory._id }, {
1351
- $set: {
1352
- status: 'failed',
1353
- error: typeof err === 'string' ? err : this.safeStringify(err),
1354
- completedAt: new Date(),
1355
- },
1356
- })];
1494
+ return [4 /*yield*/, this.finalizeEmailHistoryStorage(emailHistory, 'failed', typeof err === 'string' ? err : this.safeStringify(err))];
1357
1495
  case 2:
1358
1496
  _a.sent();
1359
1497
  return [3 /*break*/, 7];
@@ -1363,12 +1501,7 @@ var MethodManager = /** @class */ (function () {
1363
1501
  case 4:
1364
1502
  _a.sent();
1365
1503
  return [3 /*break*/, 7];
1366
- case 5: return [4 /*yield*/, email_history_collection_1.EmailHistories.updateOne({ _id: emailHistory._id }, {
1367
- $set: {
1368
- status: 'completed',
1369
- completedAt: new Date(),
1370
- },
1371
- })];
1504
+ case 5: return [4 /*yield*/, this.finalizeEmailHistoryStorage(emailHistory, 'completed')];
1372
1505
  case 6:
1373
1506
  _a.sent();
1374
1507
  _a.label = 7;
@@ -1376,13 +1509,7 @@ var MethodManager = /** @class */ (function () {
1376
1509
  case 8:
1377
1510
  error_7 = _a.sent();
1378
1511
  console.error('Error in sendMail callback:', error_7);
1379
- return [4 /*yield*/, email_history_collection_1.EmailHistories.updateOne({ _id: emailHistory._id }, {
1380
- $set: {
1381
- status: 'failed',
1382
- error: typeof error_7 === 'string' ? error_7 : this.safeStringify(error_7),
1383
- completedAt: new Date(),
1384
- },
1385
- })];
1512
+ return [4 /*yield*/, this.finalizeEmailHistoryStorage(emailHistory, 'failed', typeof error_7 === 'string' ? error_7 : this.safeStringify(error_7))];
1386
1513
  case 9:
1387
1514
  _a.sent();
1388
1515
  return [3 /*break*/, 14];
@@ -1482,6 +1609,39 @@ var MethodManager = /** @class */ (function () {
1482
1609
  return value;
1483
1610
  };
1484
1611
  };
1612
+ MethodManager.prototype.finalizeEmailHistoryStorage = function (emailHistory, status, error) {
1613
+ return __awaiter(this, void 0, void 0, function () {
1614
+ var setValues, sanitizedOccurrences;
1615
+ return __generator(this, function (_a) {
1616
+ switch (_a.label) {
1617
+ case 0:
1618
+ setValues = {
1619
+ status: status,
1620
+ text: truncateTextForHistory(emailHistory.text, EMAIL_HISTORY_TEXT_MAX_CHARS),
1621
+ html: truncateTextForHistory(emailHistory.html, EMAIL_HISTORY_HTML_MAX_CHARS),
1622
+ attachments: sanitizeAttachmentsForHistory(emailHistory.attachments),
1623
+ completedAt: new Date()
1624
+ };
1625
+ sanitizedOccurrences = sanitizeOccurrencesForHistory(emailHistory.occurrences);
1626
+ if (sanitizedOccurrences) {
1627
+ setValues.occurrences = sanitizedOccurrences;
1628
+ }
1629
+ if (status === 'failed') {
1630
+ setValues.error = truncateTextForHistory(error || '', EMAIL_HISTORY_ERROR_MAX_CHARS);
1631
+ }
1632
+ else {
1633
+ setValues.error = '';
1634
+ }
1635
+ return [4 /*yield*/, email_history_collection_1.EmailHistories.updateOne({ _id: emailHistory._id }, {
1636
+ $set: setValues
1637
+ })];
1638
+ case 1:
1639
+ _a.sent();
1640
+ return [2 /*return*/];
1641
+ }
1642
+ });
1643
+ });
1644
+ };
1485
1645
  MethodManager.prototype.tryMergeEmailOccurrence = function (email, subject, correlationId, occurrence, meta) {
1486
1646
  return __awaiter(this, void 0, void 0, function () {
1487
1647
  var existing, normalizedOccurrences, _a, aggregatedText, aggregatedHtml, updated;