From 4963729c678e0519b2ab0ae2ca275c117dff7287 Mon Sep 17 00:00:00 2001 From: titanism <101466223+titanism@users.noreply.github.com> Date: Thu, 14 Sep 2023 11:12:38 -0500 Subject: [PATCH] fix: optional compression for bounce reports > 8MB --- jobs/bounce-report.js | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/jobs/bounce-report.js b/jobs/bounce-report.js index 7eb3e9b30c..34a4704988 100644 --- a/jobs/bounce-report.js +++ b/jobs/bounce-report.js @@ -3,12 +3,14 @@ require('#config/env'); const process = require('node:process'); const zlib = require('node:zlib'); +const { Buffer } = require('node:buffer'); const { parentPort } = require('node:worker_threads'); // eslint-disable-next-line import/no-unassigned-import require('#config/mongoose'); const Graceful = require('@ladjs/graceful'); +const bytes = require('bytes'); const dayjs = require('dayjs-with-plugins'); const humanize = require('humanize-string'); const mongoose = require('mongoose'); @@ -21,6 +23,8 @@ const emailHelper = require('#helpers/email'); const logger = require('#helpers/logger'); const setupMongoose = require('#helpers/setup-mongoose'); +const BYTES_8MB = bytes('8MB'); + const graceful = new Graceful({ mongooses: [mongoose], logger @@ -227,6 +231,20 @@ function makeDelimitedString(arr) { .join('
  • ')}
  • ` ].join('\n'); + let filename = `email-deliverability-logs-${dayjs(now).format( + 'YYYY-MM-DD-h-mm-A-z' + )}.csv`.toLowerCase(); + + const buffer = Buffer.from(csv.join('\n', 'utf8')); + + let content; + if (Buffer.byteLength(buffer) > BYTES_8MB) { + content = zlib.gzipSync(buffer, { level: 9 }); + filename += '.gz'; + } else { + content = buffer; + } + // email the spreadsheet to admins await emailHelper({ template: 'alert', @@ -237,10 +255,8 @@ function makeDelimitedString(arr) { ).format('M/D/YY h:mm A z')} (${set.size} trusted hosts blocked)`, attachments: [ { - filename: `email-deliverability-logs-${dayjs(now).format( - 'YYYY-MM-DD-h-mm-A-z' - )}.csv.gz`.toLowerCase(), - content: zlib.gzipSync(csv.join('\n'), { level: 9 }) + filename, + content } ] },