fix(mail/delivery): Centralize runtime/plugin imports and switch modules to use plugins exports; unify EventEmitter usage; update Deno dependencies and small path/server refactors

This commit is contained in:
2025-10-24 10:00:25 +00:00
parent d4778d15fc
commit 27b6bb779e
17 changed files with 87 additions and 62 deletions

View File

@@ -1,5 +1,16 @@
# Changelog # Changelog
## 2025-10-24 - 1.2.1 - fix(mail/delivery)
Centralize runtime/plugin imports and switch modules to use plugins exports; unify EventEmitter usage; update Deno dependencies and small path/server refactors
- Centralized Node and third-party imports in ts/plugins.ts and re-exported commonly used utilities (net, tls, dns, fs, smartfile, smartdns, smartmail, mailauth, uuid, ip, LRUCache, etc).
- Replaced direct EventEmitter / Node built-in imports with plugins.EventEmitter across delivery, smtpclient, routing and the unified email server to standardize runtime integration.
- Updated deno.json dependency map: added @push.rocks/smartfile, @push.rocks/smartdns, @tsclass/tsclass and ip; reordered lru-cache entry.
- Stopped exporting ./dns/index.ts from ts/index.ts (DNS is available via mail/routing) to avoid duplicate exports.
- Added keysDir alias and dnsRecordsDir in ts/paths.ts and small path-related fixes.
- Added a placeholder SmtpServer and other minor delivery/smtpserver refactors and sanitizations.
- Added a local .claude/settings.local.json for development permissions (local-only configuration).
## 2025-10-24 - 1.2.0 - feat(plugins) ## 2025-10-24 - 1.2.0 - feat(plugins)
Add smartmail, mailauth and uuid to Deno dependencies and export them from plugins; include local dev permissions file Add smartmail, mailauth and uuid to Deno dependencies and export them from plugins; include local dev permissions file

View File

@@ -41,9 +41,13 @@
"@std/crypto": "jsr:@std/crypto@^1.0.0", "@std/crypto": "jsr:@std/crypto@^1.0.0",
"@std/assert": "jsr:@std/assert@^1.0.0", "@std/assert": "jsr:@std/assert@^1.0.0",
"@apiclient.xyz/cloudflare": "npm:@apiclient.xyz/cloudflare@latest", "@apiclient.xyz/cloudflare": "npm:@apiclient.xyz/cloudflare@latest",
"lru-cache": "npm:lru-cache@^11.0.0", "@push.rocks/smartfile": "npm:@push.rocks/smartfile@latest",
"@push.rocks/smartdns": "npm:@push.rocks/smartdns@latest",
"@push.rocks/smartmail": "npm:@push.rocks/smartmail@^2.0.0", "@push.rocks/smartmail": "npm:@push.rocks/smartmail@^2.0.0",
"@tsclass/tsclass": "npm:@tsclass/tsclass@latest",
"lru-cache": "npm:lru-cache@^11.0.0",
"mailauth": "npm:mailauth@^4.0.0", "mailauth": "npm:mailauth@^4.0.0",
"uuid": "npm:uuid@^9.0.0" "uuid": "npm:uuid@^9.0.0",
"ip": "npm:ip@^2.0.0"
} }
} }

View File

@@ -3,6 +3,6 @@
*/ */
export const commitinfo = { export const commitinfo = {
name: '@serve.zone/mailer', name: '@serve.zone/mailer',
version: '1.2.0', version: '1.2.1',
description: 'Enterprise mail server with SMTP, HTTP API, and DNS management - built for serve.zone infrastructure' description: 'Enterprise mail server with SMTP, HTTP API, and DNS management - built for serve.zone infrastructure'
} }

View File

@@ -8,5 +8,6 @@ export * from './mail/core/index.ts';
export * from './mail/delivery/index.ts'; export * from './mail/delivery/index.ts';
export * from './mail/routing/index.ts'; export * from './mail/routing/index.ts';
export * from './api/index.ts'; export * from './api/index.ts';
export * from './dns/index.ts';
export * from './config/index.ts'; export * from './config/index.ts';
// DNS exports are included in mail/routing, so we skip './dns/index.ts' to avoid duplication

View File

@@ -1,7 +1,4 @@
import * as plugins from '../../plugins.ts'; import * as plugins from '../../plugins.ts';
import { EventEmitter } from 'node:events';
import * as fs from 'node:fs';
import * as path from 'node:path';
import { logger } from '../../logger.ts'; import { logger } from '../../logger.ts';
import { type EmailProcessingMode } from '../routing/classes.email.config.ts'; import { type EmailProcessingMode } from '../routing/classes.email.config.ts';
import type { IEmailRoute } from '../routing/interfaces.ts'; import type { IEmailRoute } from '../routing/interfaces.ts';
@@ -74,7 +71,7 @@ export interface IQueueStats {
/** /**
* A unified queue for all email modes * A unified queue for all email modes
*/ */
export class UnifiedDeliveryQueue extends EventEmitter { export class UnifiedDeliveryQueue extends plugins.EventEmitter {
private options: Required<IQueueOptions>; private options: Required<IQueueOptions>;
private queue: Map<string, IQueueItem> = new Map(); private queue: Map<string, IQueueItem> = new Map();
private checkTimer?: NodeJS.Timeout; private checkTimer?: NodeJS.Timeout;

View File

@@ -1,7 +1,4 @@
import * as plugins from '../../plugins.ts'; import * as plugins from '../../plugins.ts';
import { EventEmitter } from 'node:events';
import * as net from 'node:net';
import * as tls from 'node:tls';
import { logger } from '../../logger.ts'; import { logger } from '../../logger.ts';
import { import {
SecurityLogger, SecurityLogger,
@@ -100,7 +97,7 @@ export interface IDeliveryStats {
/** /**
* Handles delivery for all email processing modes * Handles delivery for all email processing modes
*/ */
export class MultiModeDeliverySystem extends EventEmitter { export class MultiModeDeliverySystem extends plugins.EventEmitter {
private queue: UnifiedDeliveryQueue; private queue: UnifiedDeliveryQueue;
private options: Required<IMultiModeDeliveryOptions>; private options: Required<IMultiModeDeliveryOptions>;
private stats: IDeliveryStats; private stats: IDeliveryStats;

View File

@@ -1,5 +1,4 @@
import * as plugins from '../../plugins.ts'; import * as plugins from '../../plugins.ts';
import { EventEmitter } from 'node:events';
import { logger } from '../../logger.ts'; import { logger } from '../../logger.ts';
import { SecurityLogger, SecurityLogLevel, SecurityEventType } from '../../security/index.ts'; import { SecurityLogger, SecurityLogLevel, SecurityEventType } from '../../security/index.ts';
@@ -84,7 +83,7 @@ export interface IRateLimitResult {
/** /**
* Unified rate limiter for all email processing modes * Unified rate limiter for all email processing modes
*/ */
export class UnifiedRateLimiter extends EventEmitter { export class UnifiedRateLimiter extends plugins.EventEmitter {
private config: IHierarchicalRateLimits; private config: IHierarchicalRateLimits;
private counters: Map<string, ILimitCounter> = new Map(); private counters: Map<string, ILimitCounter> = new Map();
private patternCounters: Map<string, ILimitCounter> = new Map(); private patternCounters: Map<string, ILimitCounter> = new Map();

View File

@@ -6,3 +6,9 @@
export class DeliveryPlaceholder { export class DeliveryPlaceholder {
// Placeholder for delivery functionality // Placeholder for delivery functionality
} }
export class SmtpServer {
// Placeholder SMTP server
async start() {}
async stop() {}
}

View File

@@ -3,7 +3,7 @@
* SMTP command sending and response parsing * SMTP command sending and response parsing
*/ */
import { EventEmitter } from 'node:events'; import * as plugins from '../../../plugins.ts';
import { SMTP_COMMANDS, SMTP_CODES, LINE_ENDINGS } from './constants.ts'; import { SMTP_COMMANDS, SMTP_CODES, LINE_ENDINGS } from './constants.ts';
import type { import type {
ISmtpConnection, ISmtpConnection,
@@ -19,7 +19,7 @@ import {
} from './utils/helpers.ts'; } from './utils/helpers.ts';
import { logCommand, logDebug } from './utils/logging.ts'; import { logCommand, logDebug } from './utils/logging.ts';
export class CommandHandler extends EventEmitter { export class CommandHandler extends plugins.EventEmitter {
private options: ISmtpClientOptions; private options: ISmtpClientOptions;
private responseBuffer: string = ''; private responseBuffer: string = '';
private pendingCommand: { resolve: Function; reject: Function; command: string } | null = null; private pendingCommand: { resolve: Function; reject: Function; command: string } | null = null;

View File

@@ -3,20 +3,18 @@
* Connection pooling and lifecycle management * Connection pooling and lifecycle management
*/ */
import * as net from 'node:net'; import * as plugins from '../../../plugins.ts';
import * as tls from 'node:tls';
import { EventEmitter } from 'node:events';
import { DEFAULTS, CONNECTION_STATES } from './constants.ts'; import { DEFAULTS, CONNECTION_STATES } from './constants.ts';
import type { import type {
ISmtpClientOptions, ISmtpClientOptions,
ISmtpConnection, ISmtpConnection,
IConnectionPoolStatus, IConnectionPoolStatus,
ConnectionState ConnectionState
} from './interfaces.ts'; } from './interfaces.ts';
import { logConnection, logDebug } from './utils/logging.ts'; import { logConnection, logDebug } from './utils/logging.ts';
import { generateConnectionId } from './utils/helpers.ts'; import { generateConnectionId } from './utils/helpers.ts';
export class ConnectionManager extends EventEmitter { export class ConnectionManager extends plugins.EventEmitter {
private options: ISmtpClientOptions; private options: ISmtpClientOptions;
private connections: Map<string, ISmtpConnection> = new Map(); private connections: Map<string, ISmtpConnection> = new Map();
private pendingConnections: Set<string> = new Set(); private pendingConnections: Set<string> = new Set();

View File

@@ -3,7 +3,7 @@
* Main client class with delegation to handlers * Main client class with delegation to handlers
*/ */
import { EventEmitter } from 'node:events'; import * as plugins from '../../../plugins.ts';
import type { Email } from '../../core/classes.email.ts'; import type { Email } from '../../core/classes.email.ts';
import type { import type {
ISmtpClientOptions, ISmtpClientOptions,
@@ -30,7 +30,7 @@ interface ISmtpClientDependencies {
errorHandler: SmtpErrorHandler; errorHandler: SmtpErrorHandler;
} }
export class SmtpClient extends EventEmitter { export class SmtpClient extends plugins.EventEmitter {
private options: ISmtpClientOptions; private options: ISmtpClientOptions;
private connectionManager: ConnectionManager; private connectionManager: ConnectionManager;
private commandHandler: CommandHandler; private commandHandler: CommandHandler;

View File

@@ -3,17 +3,16 @@
* Provides utilities for managing TLS certificates * Provides utilities for managing TLS certificates
*/ */
import * as fs from 'fs'; import * as plugins from '../../../plugins.ts';
import * as tls from 'tls';
import { SmtpLogger } from './utils/logging.ts'; import { SmtpLogger } from './utils/logging.ts';
/** /**
* Certificate data * Certificate data
*/ */
export interface ICertificateData { export interface ICertificateData {
key: Buffer; key: plugins.Buffer;
cert: Buffer; cert: plugins.Buffer;
ca?: Buffer; ca?: plugins.Buffer;
} }
/** /**
@@ -155,7 +154,7 @@ export function loadCertificatesFromString(options: {
const caBuffer = caStr ? Buffer.from(caStr, 'utf8') : undefined; const caBuffer = caStr ? Buffer.from(caStr, 'utf8') : undefined;
// Test the certificates first // Test the certificates first
const secureContext = tls.createSecureContext({ const secureContext = plugins.tls.createSecureContext({
key: keyBuffer, key: keyBuffer,
cert: certBuffer, cert: certBuffer,
ca: caBuffer ca: caBuffer
@@ -206,7 +205,7 @@ export function loadCertificatesFromString(options: {
// Validate the certificates by attempting to create a secure context // Validate the certificates by attempting to create a secure context
try { try {
const secureContext = tls.createSecureContext({ const secureContext = plugins.tls.createSecureContext({
key: keyBuffer, key: keyBuffer,
cert: certBuffer, cert: certBuffer,
ca: caBuffer ca: caBuffer
@@ -253,9 +252,9 @@ export function loadCertificatesFromFiles(options: {
}): ICertificateData { }): ICertificateData {
try { try {
// Read files directly as Buffers // Read files directly as Buffers
const key = fs.readFileSync(options.keyPath); const key = plugins.fs.readFileSync(options.keyPath);
const cert = fs.readFileSync(options.certPath); const cert = plugins.fs.readFileSync(options.certPath);
const ca = options.caPath ? fs.readFileSync(options.caPath) : undefined; const ca = options.caPath ? plugins.fs.readFileSync(options.caPath) : undefined;
// Log for debugging // Log for debugging
SmtpLogger.debug('Certificate file properties', { SmtpLogger.debug('Certificate file properties', {
@@ -266,7 +265,7 @@ export function loadCertificatesFromFiles(options: {
// Validate the certificates by attempting to create a secure context // Validate the certificates by attempting to create a secure context
try { try {
const secureContext = tls.createSecureContext({ const secureContext = plugins.tls.createSecureContext({
key, key,
cert, cert,
ca ca
@@ -364,8 +363,8 @@ ORWZbz+8rBL0JIeA7eFxEA==
export function createTlsOptions( export function createTlsOptions(
certificates: ICertificateData, certificates: ICertificateData,
isServer: boolean = true isServer: boolean = true
): tls.TlsOptions { ): plugins.tls.TlsOptions {
const options: tls.TlsOptions = { const options: plugins.tls.TlsOptions = {
key: certificates.key, key: certificates.key,
cert: certificates.cert, cert: certificates.cert,
ca: certificates.ca, ca: certificates.ca,

View File

@@ -4,8 +4,6 @@
*/ */
import * as plugins from '../../../plugins.ts'; import * as plugins from '../../../plugins.ts';
import * as fs from 'fs';
import * as path from 'path';
import { SmtpState } from './interfaces.ts'; import { SmtpState } from './interfaces.ts';
import type { ISmtpSession, ISmtpTransactionResult } from './interfaces.ts'; import type { ISmtpSession, ISmtpTransactionResult } from './interfaces.ts';
import type { IDataHandler, ISmtpServer } from './interfaces.ts'; import type { IDataHandler, ISmtpServer } from './interfaces.ts';

View File

@@ -1,12 +1,11 @@
import * as plugins from '../../plugins.ts'; import * as plugins from '../../plugins.ts';
import { EventEmitter } from 'node:events';
import type { IEmailRoute, IEmailMatch, IEmailAction, IEmailContext } from './interfaces.ts'; import type { IEmailRoute, IEmailMatch, IEmailAction, IEmailContext } from './interfaces.ts';
import type { Email } from '../core/classes.email.ts'; import type { Email } from '../core/classes.email.ts';
/** /**
* Email router that evaluates routes and determines actions * Email router that evaluates routes and determines actions
*/ */
export class EmailRouter extends EventEmitter { export class EmailRouter extends plugins.EventEmitter {
private routes: IEmailRoute[]; private routes: IEmailRoute[];
private patternCache: Map<string, boolean> = new Map(); private patternCache: Map<string, boolean> = new Map();
private storageManager?: any; // StorageManager instance private storageManager?: any; // StorageManager instance

View File

@@ -1,6 +1,5 @@
import * as plugins from '../../plugins.ts'; import * as plugins from '../../plugins.ts';
import * as paths from '../../paths.ts'; import * as paths from '../../paths.ts';
import { EventEmitter } from 'events';
import { logger } from '../../logger.ts'; import { logger } from '../../logger.ts';
import { import {
SecurityLogger, SecurityLogger,
@@ -154,7 +153,7 @@ export interface IServerStats {
/** /**
* Unified email server that handles all email traffic with pattern-based routing * Unified email server that handles all email traffic with pattern-based routing
*/ */
export class UnifiedEmailServer extends EventEmitter { export class UnifiedEmailServer extends plugins.EventEmitter {
private dcRouter: DcRouter; private dcRouter: DcRouter;
private options: IUnifiedEmailServerOptions; private options: IUnifiedEmailServerOptions;
private emailRouter: EmailRouter; private emailRouter: EmailRouter;

View File

@@ -19,3 +19,9 @@ export const logsDir = plugins.path.join(configDir, 'logs');
// DKIM keys directory // DKIM keys directory
export const dkimKeysDir = plugins.path.join(configDir, 'dkim-keys'); export const dkimKeysDir = plugins.path.join(configDir, 'dkim-keys');
// Keys directory (alias for compatibility)
export const keysDir = dkimKeysDir;
// DNS records directory
export const dnsRecordsDir = plugins.path.join(configDir, 'dns-records');

View File

@@ -10,19 +10,7 @@ export * as cli from '@std/cli';
export { serveDir } from '@std/http/file-server'; export { serveDir } from '@std/http/file-server';
export * as crypto from '@std/crypto'; export * as crypto from '@std/crypto';
// Cloudflare API client // Node.js built-in modules (needed for SMTP and email processing)
import * as cloudflareImport from '@apiclient.xyz/cloudflare';
export const cloudflare = cloudflareImport;
// Email libraries
import * as smartmail from '@push.rocks/smartmail';
import * as mailauth from 'mailauth';
import * as uuid from 'uuid';
export { smartmail, mailauth, uuid };
// Node.js compatibility - needed for SMTP and email processing
// We import these as npm: specifiers for Node.js modules that don't have Deno equivalents
import { EventEmitter } from 'node:events'; import { EventEmitter } from 'node:events';
import * as net from 'node:net'; import * as net from 'node:net';
import * as tls from 'node:tls'; import * as tls from 'node:tls';
@@ -31,9 +19,32 @@ import * as fs from 'node:fs';
import * as os from 'node:os'; import * as os from 'node:os';
import * as process from 'node:process'; import * as process from 'node:process';
import * as buffer from 'node:buffer'; import * as buffer from 'node:buffer';
import * as util from 'node:util';
export { EventEmitter }; export { EventEmitter, net, tls, dns, fs, os, process, buffer, util };
export { net, tls, dns, fs, os, process, buffer };
// Re-export Buffer for convenience
export const Buffer = buffer.Buffer; export const Buffer = buffer.Buffer;
// Cloudflare API client
import * as cloudflareImport from '@apiclient.xyz/cloudflare';
export const cloudflare = cloudflareImport;
// @push.rocks packages
import * as smartfile from '@push.rocks/smartfile';
import * as smartdns from '@push.rocks/smartdns';
import * as smartmail from '@push.rocks/smartmail';
export { smartfile, smartdns, smartmail };
// @tsclass packages
import * as tsclass from '@tsclass/tsclass';
export { tsclass };
// Third-party libraries
import * as mailauth from 'mailauth';
import { dkimSign } from 'mailauth/lib/dkim/sign.js';
import * as uuid from 'uuid';
import * as ip from 'ip';
import { LRUCache } from 'lru-cache';
export { mailauth, dkimSign, uuid, ip, LRUCache };