Skip to content

Commit

Permalink
feat: make incoming email mock use file path for raw emails (#739)
Browse files Browse the repository at this point in the history
  • Loading branch information
BlankParticle authored Aug 24, 2024
1 parent 017ea74 commit a31b44c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
4 changes: 2 additions & 2 deletions apps/mail-bridge/queue/mail-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1031,8 +1031,8 @@ export const worker = createWorker<MailProcessorJobData>(
console.error(e);
void discord.info(
e instanceof Error
? `${e.message} \n${e.stack}`
: 'Unknown Error, Check Logs'
? e.message
: 'Unknown Error in Mail Processor, Check Logs'
);
span?.recordException(e as Error);
// Throw the error to be caught by the worker, and moving to failed jobs
Expand Down
35 changes: 28 additions & 7 deletions apps/mail-bridge/scripts/mock-incoming.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
import { cancel, group, intro, outro, text } from '@clack/prompts';
import { mailProcessorQueue } from '../queue/mail-processor';
import { typeIdValidator } from '@u22n/utils/typeid';
import { existsSync, readFileSync } from 'node:fs';
import { testEmail } from './email-templates';
import { nanoid } from 'nanoid';
import { z } from 'zod';

const fixWrappingQuotes = (str: string) => {
if (
(str.startsWith("'") && str.endsWith("'")) ||
(str.startsWith('"') && str.endsWith('"'))
) {
return str.slice(1, -1);
}
return str;
};

intro('Mock Incoming Mail');

const params = await group(
Expand Down Expand Up @@ -64,9 +75,18 @@ const message = await group(
}
}
}),
rawMessage: () =>
messagePath: () =>
text({
message: 'Enter raw email in RFC822 format',
message: 'Path to raw email file (drop file into terminal)',
validate: (value) => {
if (value.length === 0) return;
const fileExists = existsSync(fixWrappingQuotes(value))
? true
: false;
if (!fileExists) {
return `File at path ${fixWrappingQuotes(value)} does not exist`;
}
},
placeholder: '(Skip to use test email)',
initialValue: ''
})
Expand All @@ -80,11 +100,12 @@ const message = await group(
);

const encodedEmail = Buffer.from(
message.rawMessage ||
testEmail({
from: message.from,
to: message.rcpt_to
})
message.messagePath
? readFileSync(fixWrappingQuotes(message.messagePath), 'utf8')
: testEmail({
from: message.from,
to: message.rcpt_to
})
).toString('base64');

await mailProcessorQueue.add(`mock incoming mail ${nanoid(6)}`, {
Expand Down

0 comments on commit a31b44c

Please sign in to comment.