smtp: reduce usage of nullish coalescing operator

This commit is contained in:
Zack Schuster 2022-10-11 09:58:07 -07:00
parent 19fd8338f5
commit 5533dce964
5 changed files with 20 additions and 23 deletions

View File

@ -147,7 +147,7 @@ function convertAddressTokens(tokens: AddressToken[]) {
if (addresses.length === 0) {
for (let i = texts.length - 1; i >= 0; i--) {
const text = texts[i];
if (text?.length > 0) {
if (text != null && text.length > 0) {
texts[i] = text
.replace(/\s*\b[^@\s]+@[^@\s]+\b\s*/, (address: string) => {
if (addresses.length === 0) {

View File

@ -148,8 +148,8 @@ export class SMTPClient {
if (typeof returnPath === 'string' && returnPath.length > 0) {
const parsedAddresses = addressparser(returnPath);
if (parsedAddresses.length > 0) {
const [firstParsedAddress] = parsedAddresses;
stack.returnPath = firstParsedAddress?.address ?? '';
const [{ address = '' } = {}] = parsedAddresses;
stack.returnPath = address;
}
}

View File

@ -8,19 +8,13 @@ export function getRFC2822Date(date = new Date(), useUtc = false) {
return getRFC2822DateUTC(date);
}
const dates = date
const [zero, one, two, ...rest] = date
.toString()
.replace('GMT', '')
.replace(/\s\(.*\)$/, '')
.split(' ');
dates[0] = dates[0] + ',';
const day = dates[1] ?? '';
dates[1] = dates[2] ?? '';
dates[2] = day;
return dates.join(' ');
return [zero + ',', two, one, ...rest].join(' ');
}
/**

View File

@ -163,7 +163,7 @@ export class Message {
headers[header] as string | string[]
);
} else {
// allow any headers the user wants to set??
// allow any headers the user wants to set
this.header[header.toLowerCase()] = headers[header];
}
}
@ -589,10 +589,9 @@ class MessageStream extends Stream {
attachment: MessageAttachment,
callback: () => void
) => {
const { data = '' } = attachment;
outputBase64(
attachment.encoded
? attachment.data ?? ''
: Buffer.from(attachment.data ?? '').toString('base64'),
attachment.encoded ? data : Buffer.from(data).toString('base64'),
callback
);
};

View File

@ -21,8 +21,9 @@ const MAX_B64_MIME_WORD_BYTE_LENGTH = 39;
function tripletToBase64(num: number) {
return (
(LOOKUP[(num >> 18) & 0x3f] ?? '') +
(LOOKUP[(num >> 12) & 0x3f] ?? '') +
'' +
LOOKUP[(num >> 18) & 0x3f] +
LOOKUP[(num >> 12) & 0x3f] +
LOOKUP[(num >> 6) & 0x3f] +
LOOKUP[num & 0x3f]
);
@ -32,7 +33,9 @@ function encodeChunk(uint8: Uint8Array, start: number, end: number) {
let output = '';
for (let i = start; i < end; i += 3) {
output += tripletToBase64(
((uint8[i] ?? 0) << 16) + ((uint8[i + 1] ?? 0) << 8) + (uint8[i + 2] ?? 0)
(Number(uint8[i]) << 16) +
(Number(uint8[i + 1]) << 8) +
Number(uint8[i + 2])
);
}
return output;
@ -54,12 +57,12 @@ function encodeBase64(data: Uint8Array) {
// pad the end with zeros, but make sure to not forget the extra bytes
if (extraBytes === 1) {
const tmp = data[len - 1] ?? 0;
const tmp = Number(data[len - 1]);
output += LOOKUP[tmp >> 2];
output += LOOKUP[(tmp << 4) & 0x3f];
output += '==';
} else if (extraBytes === 2) {
const tmp = ((data[len - 2] ?? 0) << 8) + (data[len - 1] ?? 0);
const tmp = (Number(data[len - 2]) << 8) + Number(data[len - 1]);
output += LOOKUP[tmp >> 10];
output += LOOKUP[(tmp >> 4) & 0x3f];
output += LOOKUP[(tmp << 2) & 0x3f];
@ -93,9 +96,10 @@ function splitMimeEncodedString(str: string, maxlen = 12) {
while (!done) {
let chr;
done = true;
const match = str.substring(curLine.length).match(/^=([0-9A-F]{2})/i); // check if not middle of a unicode char sequence
if (match) {
chr = parseInt(match[1] ?? '', 16);
const matchset = str.substring(curLine.length).match(/^=([0-9A-F]{2})/i); // check if not middle of a unicode char sequence
if (matchset != null) {
const [, secondMatch = ''] = matchset;
chr = parseInt(secondMatch, 16);
// invalid sequence, move one char back anc recheck
if (chr < 0xc2 && chr > 0x7f) {
curLine = curLine.substring(0, curLine.length - 3);