94 lines
2.9 KiB
TypeScript
94 lines
2.9 KiB
TypeScript
/**
|
|
* SMTP Client Factory
|
|
* Factory function for client creation and dependency injection
|
|
*/
|
|
|
|
import { SmtpClient } from './smtp-client.js';
|
|
import { ConnectionManager } from './connection-manager.js';
|
|
import { CommandHandler } from './command-handler.js';
|
|
import { AuthHandler } from './auth-handler.js';
|
|
import { TlsHandler } from './tls-handler.js';
|
|
import { SmtpErrorHandler } from './error-handler.js';
|
|
import type { ISmtpClientOptions } from './interfaces.js';
|
|
import { validateClientOptions } from './utils/validation.js';
|
|
import { DEFAULTS } from './constants.js';
|
|
|
|
/**
|
|
* Create a complete SMTP client with all components
|
|
*/
|
|
export function createSmtpClient(options: ISmtpClientOptions): SmtpClient {
|
|
// Validate options
|
|
const errors = validateClientOptions(options);
|
|
if (errors.length > 0) {
|
|
throw new Error(`Invalid client options: ${errors.join(', ')}`);
|
|
}
|
|
|
|
// Apply defaults
|
|
const clientOptions: ISmtpClientOptions = {
|
|
connectionTimeout: DEFAULTS.CONNECTION_TIMEOUT,
|
|
socketTimeout: DEFAULTS.SOCKET_TIMEOUT,
|
|
maxConnections: DEFAULTS.MAX_CONNECTIONS,
|
|
maxMessages: DEFAULTS.MAX_MESSAGES,
|
|
pool: false,
|
|
secure: false,
|
|
debug: false,
|
|
...options
|
|
};
|
|
|
|
// Create handlers
|
|
const errorHandler = new SmtpErrorHandler(clientOptions);
|
|
const connectionManager = new ConnectionManager(clientOptions);
|
|
const commandHandler = new CommandHandler(clientOptions);
|
|
const authHandler = new AuthHandler(clientOptions, commandHandler);
|
|
const tlsHandler = new TlsHandler(clientOptions, commandHandler);
|
|
|
|
// Create and return SMTP client
|
|
return new SmtpClient({
|
|
options: clientOptions,
|
|
connectionManager,
|
|
commandHandler,
|
|
authHandler,
|
|
tlsHandler,
|
|
errorHandler
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Create SMTP client with connection pooling enabled
|
|
*/
|
|
export function createPooledSmtpClient(options: ISmtpClientOptions): SmtpClient {
|
|
return createSmtpClient({
|
|
...options,
|
|
pool: true,
|
|
maxConnections: options.maxConnections || DEFAULTS.MAX_CONNECTIONS,
|
|
maxMessages: options.maxMessages || DEFAULTS.MAX_MESSAGES
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Create SMTP client for high-volume sending
|
|
*/
|
|
export function createBulkSmtpClient(options: ISmtpClientOptions): SmtpClient {
|
|
return createSmtpClient({
|
|
...options,
|
|
pool: true,
|
|
maxConnections: Math.max(options.maxConnections || 10, 10),
|
|
maxMessages: Math.max(options.maxMessages || 1000, 1000),
|
|
connectionTimeout: options.connectionTimeout || 30000,
|
|
socketTimeout: options.socketTimeout || 120000
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Create SMTP client for transactional emails
|
|
*/
|
|
export function createTransactionalSmtpClient(options: ISmtpClientOptions): SmtpClient {
|
|
return createSmtpClient({
|
|
...options,
|
|
pool: false, // Use fresh connections for transactional emails
|
|
maxConnections: 1,
|
|
maxMessages: 1,
|
|
connectionTimeout: options.connectionTimeout || 10000,
|
|
socketTimeout: options.socketTimeout || 30000
|
|
});
|
|
} |