chore: bump version to 1.2.0

refactor: update SMTP session handling to process one email per session
- Removed maxEmailsPerSession configuration and related logic
- Simplified processSession to handle a single email
- Adjusted addEmailToSession to create a new session for each email
- Updated types to reflect changes in session management
This commit is contained in:
Siwat Sirichai 2025-08-19 18:24:17 +07:00
parent 069540e310
commit b035b27da7
5 changed files with 131 additions and 665 deletions

View file

@ -3,20 +3,20 @@
[![TypeScript](https://img.shields.io/badge/%3C%2F%3E-TypeScript-%230074c1.svg)](http://www.typescriptlang.org/)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
A production-ready TypeScript client library for SMTP over WebSocket protocol with **session-based queue management**, automatic reconnection, and comprehensive error handling. Features proper SMTP session protocol implementation with mutex-based session locking.
A production-ready TypeScript client library for SMTP over WebSocket protocol with **dedicated SMTP sessions**, automatic reconnection, and comprehensive error handling. Each email gets its own complete SMTP session for maximum protocol compliance and isolation.
## 🚀 Key Features
### **Session-Based SMTP Protocol**
- **Proper SMTP Sessions**: One session = EHLO → AUTH → Multiple Emails → QUIT
### **Dedicated SMTP Sessions**
- **Isolated SMTP Sessions**: One email = One complete SMTP session (EHLO → AUTH → Email → QUIT)
- **Session Mutex**: Only one SMTP session active at a time (protocol compliant)
- **Email Batching**: Send multiple emails efficiently within a single session
- **Maximum Isolation**: Each email in its own session for better error handling
- **Resource Optimization**: Connect only when needed, disconnect when idle
### **Intelligent Queue Management**
- **Session Queuing**: Queues complete email sessions, not individual commands
- **Session Queuing**: Each email queued as its own dedicated session
- **Priority-Based Processing**: CRITICAL > HIGH > NORMAL > LOW priority levels
- **Configurable Batching**: Control emails per session with `maxEmailsPerSession`
- **One-to-One Mapping**: One email per SMTP session for maximum isolation
- **Smart Resource Usage**: Auto-connect/disconnect based on queue state
### **Production-Ready Reliability**
@ -107,21 +107,19 @@ sequenceDiagram
Client->>Server: AUTH PLAIN <credentials>
Server-->>Client: 235 AUTH OK
loop For each email in session
Client->>Server: MAIL FROM:<sender>
Server-->>Client: 250 Sender OK
Client->>Server: RCPT TO:<recipient>
Server-->>Client: 250 Recipient OK
Client->>Server: DATA
Server-->>Client: 354 Start data
Client->>Server: <email content>\r\n.
Server-->>Client: 250 Message accepted
Client->>App: Email resolved
end
Client->>Server: MAIL FROM:<sender>
Server-->>Client: 250 Sender OK
Client->>Server: RCPT TO:<recipient>
Server-->>Client: 250 Recipient OK
Client->>Server: DATA
Server-->>Client: 354 Start data
Client->>Server: <email content>\r\n.
Server-->>Client: 250 Message accepted
Client->>App: Email resolved
Client->>Server: QUIT
Server-->>Client: 221 Goodbye
@ -151,13 +149,12 @@ const client = new SMTPOverWSClient({
url: 'wss://api.siwatsystem.com/smtp',
apiKey: 'your-api-key',
debug: true,
maxEmailsPerSession: 5, // Batch up to 5 emails per session
sessionTimeout: 300000 // 5 minute session timeout
});
// Queue individual emails - they'll be batched into sessions automatically
// Queue individual emails - each gets its own dedicated SMTP session
try {
// These emails will be sent in the same SMTP session
// Each email will be sent in its own complete SMTP session
const emailId1 = await client.queueEmail(
'sender@example.com',
['recipient1@example.com'],
@ -184,14 +181,13 @@ try {
import nodemailer from 'nodemailer';
import { createTransport } from '@siwats/mxrelay-consumer';
// Create transport with session batching
// Create transport with dedicated sessions
const transport = createTransport('your-api-key', {
host: 'api.siwatsystem.com',
port: 443,
secure: true,
debug: true,
maxEmailsPerSession: 10, // Batch up to 10 emails per session
sessionBatchTimeout: 2000 // Wait 2 seconds to batch emails
sessionBatchTimeout: 2000 // Processing delay between sessions
});
const transporter = nodemailer.createTransporter(transport);
@ -213,7 +209,7 @@ const emails = [
}
];
// Send all emails - they'll be efficiently batched into SMTP sessions
// Send all emails - each gets its own dedicated SMTP session
for (const email of emails) {
try {
const info = await transporter.sendMail(email);
@ -237,8 +233,7 @@ interface SMTPClientConfig {
// Session Management
sessionTimeout?: number; // Session timeout (default: 300000ms)
maxEmailsPerSession?: number; // Emails per session (default: 10)
sessionBatchTimeout?: number; // Batch wait time (default: 1000ms)
sessionBatchTimeout?: number; // Processing delay between sessions (default: 1000ms)
// Connection Management
debug?: boolean; // Enable debug logging (default: false)
@ -289,7 +284,7 @@ await client.queueEmail(
'Subject: SECURITY BREACH\r\n\r\nImmediate action required!',
{
priority: MessagePriority.CRITICAL,
immediate: true // Skip batching, send immediately
immediate: true // Send with highest priority
}
);
```
@ -422,9 +417,10 @@ flowchart TD
subgraph Queue ["📋 Session Creation"]
SQ[Session Queue]
S1[Session 1 - CRITICAL]
S2[Session 2 - HIGH]
S3[Session 3 - NORMAL]
S1[Session 1 - CRITICAL - Email 4]
S2[Session 2 - HIGH - Email 1]
S3[Session 3 - HIGH - Email 3]
S4[Session 4 - NORMAL - Email 2]
end
subgraph Processing ["⚡ Session Processing"]
@ -441,10 +437,12 @@ flowchart TD
SQ --> S1
SQ --> S2
SQ --> S3
SQ --> S4
S1 --> SP
S2 --> SP
S3 --> SP
S4 --> SP
SP --> SC
SC --> SM
@ -467,8 +465,7 @@ const client = new SMTPOverWSClient({
// Production optimizations
debug: false,
maxEmailsPerSession: 20, // Batch more emails per session
sessionBatchTimeout: 5000, // Wait longer to accumulate emails
sessionBatchTimeout: 5000, // Longer delay between sessions
sessionTimeout: 600000, // 10 minute session timeout
maxQueueSize: 1000, // Higher queue capacity
reconnectInterval: 15000, // Longer reconnect delay
@ -514,7 +511,7 @@ process.on('SIGTERM', async () => {
### createTransport(apiKey, options?)
Creates a Nodemailer-compatible transport with session batching.
Creates a Nodemailer-compatible transport with dedicated SMTP sessions.
- `apiKey` - Your API key for authentication
- `options` - Optional transport and session configuration
@ -531,7 +528,7 @@ const client = new SMTPOverWSClient({
});
// Debug output includes:
// - Session creation and batching
// - Session creation (one per email)
// - SMTP protocol commands and responses
// - Connection state transitions
// - Queue processing details