Skip to content

build: fix type errors with newer versions of typescript #323

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
186 changes: 99 additions & 87 deletions email.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion email.js.map

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@
"type": "module",
"devDependencies": {
"@ledge/configs": "23.3.23322",
"@rollup/plugin-typescript": "8.3.2",
"@rollup/plugin-typescript": "8.5.0",
"@types/mailparser": "3.4.0",
"@types/node": "12.12.6",
"@types/smtp-server": "3.5.7",
"@typescript-eslint/eslint-plugin": "5.21.0",
"@typescript-eslint/parser": "5.21.0",
"ava": "4.2.0",
"eslint": "8.14.0",
"@typescript-eslint/eslint-plugin": "5.40.0",
"@typescript-eslint/parser": "5.40.0",
"ava": "4.3.3",
"eslint": "8.25.0",
"eslint-config-prettier": "8.5.0",
"eslint-plugin-prettier": "4.0.0",
"eslint-plugin-prettier": "4.2.1",
"mailparser": "3.5.0",
"prettier": "2.6.2",
"rollup": "2.70.2",
"prettier": "2.7.1",
"rollup": "2.79.0",
"smtp-server": "3.11.0",
"ts-node": "10.7.0",
"ts-node": "10.9.1",
"tslib": "2.4.0",
"typescript": "4.3.5"
},
Expand All @@ -44,7 +44,7 @@
}
},
"resolutions": {
"nodemailer": "6.7.4"
"nodemailer": "6.8.0"
},
"engines": {
"node": ">=12"
Expand Down
38 changes: 24 additions & 14 deletions smtp/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ interface AddressToken {

export interface AddressObject {
address?: string;
name?: string;
name?: string | undefined;
group?: AddressObject[];
}

Expand Down Expand Up @@ -40,11 +40,12 @@ function tokenizeAddress(address: string | string[] = '') {
let operator: string | undefined = undefined;

for (const character of address.toString()) {
if ((operator?.length ?? 0) > 0 && character === operator) {
const operatorHasLength = operator != null && operator.length > 0;
if (operatorHasLength === true && character === operator) {
tokens.push({ type: 'operator', value: character });
token = undefined;
operator = undefined;
} else if ((operator?.length ?? 0) === 0 && OPERATORS.has(character)) {
} else if (operatorHasLength === false && OPERATORS.has(character)) {
tokens.push({ type: 'operator', value: character });
token = undefined;
operator = OPERATORS.get(character);
Expand Down Expand Up @@ -137,7 +138,11 @@ function convertAddressTokens(tokens: AddressToken[]) {
// If no address was found, try to detect one from regular text
if (addresses.length === 0 && texts.length > 0) {
for (let i = texts.length - 1; i >= 0; i--) {
if (texts[i].match(/^[^@\s]+@[^@\s]+$/)) {
const text = texts[i];
if (text == null) {
continue;
}
if (/^[^@\s]+@[^@\s]+$/.test(text)) {
addresses = texts.splice(i, 1);
break;
}
Expand All @@ -146,16 +151,21 @@ function convertAddressTokens(tokens: AddressToken[]) {
// still no address
if (addresses.length === 0) {
for (let i = texts.length - 1; i >= 0; i--) {
texts[i] = texts[i]
.replace(/\s*\b[^@\s]+@[^@\s]+\b\s*/, (address: string) => {
if (addresses.length === 0) {
addresses = [address.trim()];
return ' ';
} else {
return address;
}
})
.trim();
const text = texts[i];
if (text != null && text.length > 0) {
texts[i] = text
.replace(/\s*\b[^@\s]+@[^@\s]+\b\s*/, (address: string) => {
if (addresses.length === 0) {
addresses = [address.trim()];
return ' ';
} else {
return address;
}
})
.trim();
} else {
texts[i] = '';
}

if (addresses.length > 0) {
break;
Expand Down
26 changes: 14 additions & 12 deletions smtp/client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { addressparser } from './address.js';
import { AddressObject, addressparser } from './address.js';
import type { MessageAttachment, MessageHeaders } from './message.js';
import { Message } from './message.js';
import type { SMTPConnectionOptions } from './connection.js';
Expand Down Expand Up @@ -113,10 +113,10 @@ export class SMTPClient {
/* ø */
}
) {
const [{ address: from }] = addressparser(message.header.from);
const [{ address: from = '' } = {}] = addressparser(message.header.from);
const stack = {
message,
to: [] as ReturnType<typeof addressparser>,
to: new Array<AddressObject>(),
from,
callback: callback.bind(this),
} as MessageStack;
Expand Down Expand Up @@ -146,10 +146,10 @@ export class SMTPClient {
}

if (typeof returnPath === 'string' && returnPath.length > 0) {
const parsedReturnPath = addressparser(returnPath);
if (parsedReturnPath.length > 0) {
const [{ address: returnPathAddress }] = parsedReturnPath;
stack.returnPath = returnPathAddress as string;
const parsedAddresses = addressparser(returnPath);
if (parsedAddresses.length > 0) {
const [{ address = '' } = {}] = parsedAddresses;
stack.returnPath = address;
}
}

Expand All @@ -165,15 +165,17 @@ export class SMTPClient {
clearTimeout(this.timer);
}

if (this.queue.length) {
const queueItem = this.queue[0];
if (queueItem) {
if (this.smtp.state() == SMTPState.NOTCONNECTED) {
this._connect(this.queue[0]);
this._connect(queueItem);
} else if (
this.smtp.state() == SMTPState.CONNECTED &&
!this.sending &&
this.ready
) {
this._sendmail(this.queue.shift() as MessageStack);
this.queue.shift();
this._sendmail(queueItem);
}
}
// wait around 1 seconds in case something does come in,
Expand Down Expand Up @@ -312,10 +314,10 @@ export class SMTPClient {
throw new TypeError('stack.to must be array');
}

const to = stack.to.shift()?.address;
const to = stack.to.shift();
this.smtp.rcpt(
this._sendsmtp(stack, stack.to.length ? this._sendrcpt : this._senddata),
`<${to}>`
`<${to == null ? '' : to.address || ''}>`
);
}

Expand Down
31 changes: 19 additions & 12 deletions smtp/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export type SMTPSocketOptions = Omit<
>;

export interface SMTPConnectionOptions {
timeout: number | null;
timeout: number | null | undefined;
user: string;
password: string;
domain: string;
Expand Down Expand Up @@ -174,7 +174,7 @@ export class SMTPConnection extends EventEmitter {
this.port = port || (ssl ? SMTP_SSL_PORT : tls ? SMTP_TLS_PORT : SMTP_PORT);
this.loggedin = user && password ? false : true;

if (!user && (password?.length ?? 0) > 0) {
if (user == null && password != null && password.length > 0) {
throw new Error('`password` cannot be set without `user`');
}

Expand Down Expand Up @@ -520,7 +520,8 @@ export class SMTPConnection extends EventEmitter {
// It's actually stricter, in that only spaces are allowed between
// parameters, but were not going to check for that here. Note
// that the space isn't present if there are no parameters.
this.features[parse[1].toLowerCase()] = parse[2] || true;
const [, one = '', two = true] = parse;
this.features[one.toLowerCase()] = two;
}
});
}
Expand Down Expand Up @@ -554,7 +555,7 @@ export class SMTPConnection extends EventEmitter {
* @returns {boolean} whether the extension exists
*/
public has_extn(opt: string) {
return (this.features ?? {})[opt.toLowerCase()] === undefined;
return this.features != null && this.features[opt.toLowerCase()] != null;
Comment on lines -557 to +558
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eleith this behavior has been in place since before i even showed up (see 50b9ec1) but it just seems wrong

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it took a bit of digging because this file has been renamed more than once.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the only change i did to that method in that commit was to add nullish coalescing. i'm referring specifically to the comparison to undefined.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed. it's wrong. i also can't find any use of this public method anywhere.

}

/**
Expand Down Expand Up @@ -631,7 +632,11 @@ export class SMTPConnection extends EventEmitter {
*/
public message(data: string) {
this.log(data);
this.sock?.write(data) ?? this.log('no socket to write to');
if (this.sock != null) {
this.sock.write(data);
} else {
this.log('no socket to write to');
}
}

/**
Expand Down Expand Up @@ -710,10 +715,10 @@ export class SMTPConnection extends EventEmitter {
const login = {
user: user ? () => user : this.user,
password: password ? () => password : this.password,
method: options?.method?.toUpperCase() ?? '',
method: options.method != null ? options.method.toUpperCase() : '',
};

const domain = options?.domain || this.domain;
const domain = options.domain || this.domain;

const initiate = (err: Error | null | undefined, data: unknown) => {
if (err) {
Expand Down Expand Up @@ -755,16 +760,18 @@ export class SMTPConnection extends EventEmitter {
// List of authentication methods we support: from preferred to
// less preferred methods.
if (!method) {
const preferred = this.authentication;
let auth = '';

if (typeof this.features?.['auth'] === 'string') {
if (
this.features != null &&
typeof this.features['auth'] === 'string'
) {
auth = this.features['auth'];
}

for (let i = 0; i < preferred.length; i++) {
if (auth.includes(preferred[i])) {
method = preferred[i];
for (const authMethod of this.authentication) {
if (auth.includes(authMethod)) {
method = authMethod;
break;
}
}
Expand Down
10 changes: 2 additions & 8 deletions smtp/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(' ');
}

/**
Expand Down
4 changes: 3 additions & 1 deletion smtp/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ export class SMTPError extends Error {
error?: Error | null,
smtp?: unknown
) {
const msg = error?.message ? `${message} (${error.message})` : message;
const shouldUseError =
error != null && error.message != null && error.message.length > 0;
const msg = shouldUseError ? `${message} (${error.message})` : message;
const err = new SMTPError(msg);

err.code = code;
Expand Down
43 changes: 24 additions & 19 deletions smtp/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ export class Message {
) {
const attachment = headers[header];
if (Array.isArray(attachment)) {
for (let i = 0; i < attachment.length; i++) {
this.attach(attachment[i]);
for (const attachmentItem of attachment) {
this.attach(attachmentItem);
}
} else if (attachment != null) {
this.attach(attachment);
Expand All @@ -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];
}
}
Expand Down Expand Up @@ -419,9 +419,9 @@ class MessageStream extends Stream {
) => {
const chunk = MIME64CHUNK * 16;
const buffer = Buffer.alloc(chunk);
const { headers = {} } = attachment;

const inputEncoding =
attachment?.headers?.['content-transfer-encoding'] || 'base64';
const inputEncoding = headers['content-transfer-encoding'] || 'base64';
const encoding =
inputEncoding === '7bit'
? 'ascii'
Expand Down Expand Up @@ -479,7 +479,7 @@ class MessageStream extends Stream {
callback: () => void
) => {
const { stream } = attachment;
if (stream?.readable) {
if (stream != null && stream.readable) {
let previous = Buffer.alloc(0);

stream.resume();
Expand Down Expand Up @@ -546,14 +546,17 @@ class MessageStream extends Stream {
) => {
if (index < list.length) {
output(`--${boundary}${CRLF}`);
if (list[index].related) {
outputRelated(list[index], () =>
outputMessage(boundary, list, index + 1, callback)
);
} else {
outputAttachment(list[index], () =>
outputMessage(boundary, list, index + 1, callback)
);
const item = list[index];
if (item != null) {
if (item.related) {
outputRelated(item, () =>
outputMessage(boundary, list, index + 1, callback)
);
} else {
outputAttachment(item, () =>
outputMessage(boundary, list, index + 1, callback)
);
}
}
} else {
output(`${CRLF}--${boundary}--${CRLF}${CRLF}`);
Expand Down Expand Up @@ -588,10 +591,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
);
};
Expand Down Expand Up @@ -630,7 +632,8 @@ class MessageStream extends Stream {
`Content-Type: multipart/related; boundary="${boundary}"${CRLF}${CRLF}--${boundary}${CRLF}`
);
outputAttachment(message, () => {
outputMessage(boundary, message.related ?? [], 0, () => {
const { related = [] } = message;
outputMessage(boundary, related, 0, () => {
output(`${CRLF}--${boundary}--${CRLF}${CRLF}`);
callback();
});
Expand Down Expand Up @@ -674,7 +677,9 @@ class MessageStream extends Stream {
} else {
this.emit(
'data',
this.buffer?.toString('utf-8', 0, this.bufferIndex) ?? ''
this.buffer != null
? this.buffer.toString('utf-8', 0, this.bufferIndex)
: ''
);
this.emit('end');
}
Expand Down
Loading