Skip to content

Commit

Permalink
fix: optional compression for bounce reports > 8MB
Browse files Browse the repository at this point in the history
  • Loading branch information
titanism committed Sep 14, 2023
1 parent 03ca6ee commit 4963729
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions jobs/bounce-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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
Expand Down Expand Up @@ -227,6 +231,20 @@ function makeDelimitedString(arr) {
.join('</li><li>')}</li></ul>`
].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',
Expand All @@ -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
}
]
},
Expand Down

0 comments on commit 4963729

Please sign in to comment.