loopzero-gateway-email 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +6 -0
- package/dist/index.cjs +130 -33
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +22 -0
- package/dist/index.d.ts +22 -0
- package/dist/index.js +131 -34
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { channelConfigReaders, BasePlatformAdapter } from 'loopzero-gateway';
|
|
1
|
+
import { channelConfigReaders, BasePlatformAdapter, ReconnectBackoff } from 'loopzero-gateway';
|
|
2
2
|
import { ImapFlow } from 'imapflow';
|
|
3
3
|
import { simpleParser } from 'mailparser';
|
|
4
4
|
import { createTransport } from 'nodemailer';
|
|
@@ -82,16 +82,33 @@ var ImapClient = class {
|
|
|
82
82
|
config;
|
|
83
83
|
client;
|
|
84
84
|
idleAbort;
|
|
85
|
+
lostListener;
|
|
86
|
+
/** Set by `disconnect()`, so the close it causes is not reported as a loss. */
|
|
87
|
+
closing = false;
|
|
88
|
+
onConnectionLost(listener) {
|
|
89
|
+
this.lostListener = listener;
|
|
90
|
+
}
|
|
85
91
|
async connect() {
|
|
86
|
-
|
|
92
|
+
const client = new ImapFlow({
|
|
87
93
|
host: this.config.host,
|
|
88
94
|
port: this.config.port,
|
|
89
95
|
secure: this.config.secure,
|
|
90
96
|
auth: this.config.auth,
|
|
91
97
|
logger: false
|
|
92
98
|
});
|
|
93
|
-
|
|
94
|
-
|
|
99
|
+
this.client = client;
|
|
100
|
+
let lastError;
|
|
101
|
+
client.on("error", (err) => {
|
|
102
|
+
lastError = err;
|
|
103
|
+
});
|
|
104
|
+
let opened = false;
|
|
105
|
+
client.on("close", () => {
|
|
106
|
+
if (!opened || this.closing) return;
|
|
107
|
+
this.lostListener?.(lastError);
|
|
108
|
+
});
|
|
109
|
+
await client.connect();
|
|
110
|
+
await client.mailboxOpen("INBOX");
|
|
111
|
+
opened = true;
|
|
95
112
|
}
|
|
96
113
|
// eslint-disable-next-line @typescript-eslint/require-await
|
|
97
114
|
async mailboxStatus() {
|
|
@@ -159,6 +176,7 @@ var ImapClient = class {
|
|
|
159
176
|
}
|
|
160
177
|
}
|
|
161
178
|
async disconnect() {
|
|
179
|
+
this.closing = true;
|
|
162
180
|
await this.stopIdle();
|
|
163
181
|
if (this.client !== void 0) {
|
|
164
182
|
try {
|
|
@@ -359,6 +377,7 @@ var ThreadStore = class _ThreadStore {
|
|
|
359
377
|
|
|
360
378
|
// src/adapter.ts
|
|
361
379
|
var DEFAULT_POLL_INTERVAL_MS = 15e3;
|
|
380
|
+
var MAX_RECONNECT_DELAY_MS = 6e4;
|
|
362
381
|
function reportDrainFailure(err) {
|
|
363
382
|
console.error("[email] drain failed:", err instanceof Error ? err.message : err);
|
|
364
383
|
}
|
|
@@ -381,6 +400,13 @@ var EmailAdapter = class extends BasePlatformAdapter {
|
|
|
381
400
|
// EC-4: serialize concurrent _dispatchInbound calls.
|
|
382
401
|
dispatchQueue = Promise.resolve();
|
|
383
402
|
pollTimer;
|
|
403
|
+
/** Set by `disconnect()`: a lost connection is then not reopened. */
|
|
404
|
+
stopping = false;
|
|
405
|
+
reconnectTimer;
|
|
406
|
+
reconnectBackoff = new ReconnectBackoff({
|
|
407
|
+
initialDelayMs: 1e3,
|
|
408
|
+
maxDelayMs: MAX_RECONNECT_DELAY_MS
|
|
409
|
+
});
|
|
384
410
|
/** In-memory cursor (#348); mirrored to `options.cursorStore` when one is given. */
|
|
385
411
|
cursor;
|
|
386
412
|
constructor(opts) {
|
|
@@ -393,39 +419,55 @@ var EmailAdapter = class extends BasePlatformAdapter {
|
|
|
393
419
|
}
|
|
394
420
|
this.options = opts;
|
|
395
421
|
}
|
|
396
|
-
// biome-ignore lint/complexity/noExcessiveCognitiveComplexity: connect orchestrates factory-vs-real, verify(), connect(), IDLE-vs-poll choice, and rollback-on-throw — a linear single-transaction sequence; splitting hurts the disconnect-on-failure invariant.
|
|
397
422
|
async connect() {
|
|
398
423
|
if (this.connected) return true;
|
|
424
|
+
this.stopping = false;
|
|
399
425
|
try {
|
|
400
|
-
const imapFactory = this.options.__imapFactory;
|
|
401
426
|
const smtpFactory = this.options.__smtpFactory;
|
|
402
|
-
|
|
403
|
-
host: this.options.imapHost,
|
|
404
|
-
port: this.options.imapPort ?? 993,
|
|
405
|
-
secure: true,
|
|
406
|
-
auth: { user: this.options.address, pass: this.options.password }
|
|
407
|
-
}) : new ImapClient({
|
|
408
|
-
host: this.options.imapHost,
|
|
409
|
-
port: this.options.imapPort ?? 993,
|
|
410
|
-
secure: true,
|
|
411
|
-
auth: { user: this.options.address, pass: this.options.password }
|
|
412
|
-
});
|
|
413
|
-
this.smtp = smtpFactory !== void 0 ? smtpFactory({
|
|
427
|
+
const smtpConfig = {
|
|
414
428
|
host: this.options.smtpHost,
|
|
415
429
|
port: this.options.smtpPort ?? 587,
|
|
416
430
|
secure: (this.options.smtpPort ?? 587) === 465,
|
|
417
431
|
auth: { user: this.options.address, pass: this.options.password }
|
|
418
|
-
}
|
|
419
|
-
|
|
420
|
-
port: this.options.smtpPort ?? 587,
|
|
421
|
-
secure: (this.options.smtpPort ?? 587) === 465,
|
|
422
|
-
auth: { user: this.options.address, pass: this.options.password }
|
|
423
|
-
});
|
|
432
|
+
};
|
|
433
|
+
this.smtp = smtpFactory !== void 0 ? smtpFactory(smtpConfig) : new SmtpClient(smtpConfig);
|
|
424
434
|
await this.smtp.verify();
|
|
425
|
-
await this.
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
435
|
+
await this._openImap();
|
|
436
|
+
this.connected = true;
|
|
437
|
+
return true;
|
|
438
|
+
} catch (err) {
|
|
439
|
+
console.error("[email] connect failed:", err instanceof Error ? err.message : err);
|
|
440
|
+
await this.disconnect().catch(() => {
|
|
441
|
+
});
|
|
442
|
+
return false;
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
/** A fresh IMAP client, watched for loss of its connection. */
|
|
446
|
+
_newImapClient() {
|
|
447
|
+
const imapFactory = this.options.__imapFactory;
|
|
448
|
+
const config = {
|
|
449
|
+
host: this.options.imapHost,
|
|
450
|
+
port: this.options.imapPort ?? 993,
|
|
451
|
+
secure: true,
|
|
452
|
+
auth: { user: this.options.address, pass: this.options.password }
|
|
453
|
+
};
|
|
454
|
+
const client = imapFactory !== void 0 ? imapFactory(config) : new ImapClient(config);
|
|
455
|
+
client.onConnectionLost((cause) => this._imapLost(client, cause));
|
|
456
|
+
return client;
|
|
457
|
+
}
|
|
458
|
+
/**
|
|
459
|
+
* Open IMAP, keep or take the cursor, and start watching: IDLE when the server has it, a poll
|
|
460
|
+
* otherwise. Used by `connect()` and by every reconnect, so a reconnect resumes from the cursor
|
|
461
|
+
* and dispatches what arrived while the connection was down.
|
|
462
|
+
*/
|
|
463
|
+
async _openImap() {
|
|
464
|
+
const imap = this._newImapClient();
|
|
465
|
+
this.imap = imap;
|
|
466
|
+
try {
|
|
467
|
+
await imap.connect();
|
|
468
|
+
await this._establishCursor(imap);
|
|
469
|
+
if (imap.supportsIdle()) {
|
|
470
|
+
await imap.startIdle(() => {
|
|
429
471
|
void this._drainUnseen().catch(reportDrainFailure);
|
|
430
472
|
});
|
|
431
473
|
void this._drainUnseen().catch(reportDrainFailure);
|
|
@@ -435,20 +477,75 @@ var EmailAdapter = class extends BasePlatformAdapter {
|
|
|
435
477
|
void this._drainUnseen().catch(reportDrainFailure);
|
|
436
478
|
}, intervalMs);
|
|
437
479
|
}
|
|
438
|
-
this.
|
|
439
|
-
return true;
|
|
480
|
+
this.reconnectBackoff.opened();
|
|
440
481
|
} catch (err) {
|
|
441
|
-
|
|
442
|
-
await
|
|
482
|
+
if (this.imap === imap) this.imap = void 0;
|
|
483
|
+
await imap.disconnect().catch(() => {
|
|
443
484
|
});
|
|
444
|
-
|
|
485
|
+
throw err;
|
|
445
486
|
}
|
|
446
487
|
}
|
|
447
|
-
|
|
488
|
+
/**
|
|
489
|
+
* The IMAP connection dropped (a reset, a timeout, the server going away). The adapter stays
|
|
490
|
+
* connected from its caller's view and opens a new IMAP connection with growing delays until
|
|
491
|
+
* one succeeds or `disconnect()` is called. Without this a dropped link ended inbound mail for
|
|
492
|
+
* the life of the process while `connect()` had answered true.
|
|
493
|
+
*/
|
|
494
|
+
_imapLost(client, cause) {
|
|
495
|
+
if (this.stopping || this.imap !== client) return;
|
|
496
|
+
console.error(
|
|
497
|
+
`[email] IMAP connection lost (${cause?.message ?? "closed by the server"}); reconnecting`
|
|
498
|
+
);
|
|
499
|
+
this.imap = void 0;
|
|
500
|
+
this._stopPolling();
|
|
501
|
+
void client.disconnect().catch(() => {
|
|
502
|
+
});
|
|
503
|
+
this._scheduleReconnect();
|
|
504
|
+
}
|
|
505
|
+
_scheduleReconnect() {
|
|
506
|
+
if (this.stopping || this.reconnectTimer !== void 0) return;
|
|
507
|
+
const delay = this.reconnectBackoff.nextDelayMs() ?? MAX_RECONNECT_DELAY_MS;
|
|
508
|
+
this.reconnectTimer = setTimeout(() => {
|
|
509
|
+
this.reconnectTimer = void 0;
|
|
510
|
+
this._reconnectImap().catch((err) => {
|
|
511
|
+
console.error(
|
|
512
|
+
"[email] IMAP reconnect crashed (will retry):",
|
|
513
|
+
err instanceof Error ? err.message : err
|
|
514
|
+
);
|
|
515
|
+
this._scheduleReconnect();
|
|
516
|
+
});
|
|
517
|
+
}, delay);
|
|
518
|
+
}
|
|
519
|
+
async _reconnectImap() {
|
|
520
|
+
if (this.stopping) return;
|
|
521
|
+
try {
|
|
522
|
+
await this._openImap();
|
|
523
|
+
if (this.stopping) {
|
|
524
|
+
await this.imap?.disconnect().catch(() => {
|
|
525
|
+
});
|
|
526
|
+
this.imap = void 0;
|
|
527
|
+
}
|
|
528
|
+
} catch (err) {
|
|
529
|
+
console.error(
|
|
530
|
+
"[email] IMAP reconnect failed (will retry):",
|
|
531
|
+
err instanceof Error ? err.message : err
|
|
532
|
+
);
|
|
533
|
+
this._scheduleReconnect();
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
_stopPolling() {
|
|
448
537
|
if (this.pollTimer !== void 0) {
|
|
449
538
|
clearInterval(this.pollTimer);
|
|
450
539
|
this.pollTimer = void 0;
|
|
451
540
|
}
|
|
541
|
+
}
|
|
542
|
+
async disconnect() {
|
|
543
|
+
this.stopping = true;
|
|
544
|
+
if (this.reconnectTimer !== void 0) {
|
|
545
|
+
clearTimeout(this.reconnectTimer);
|
|
546
|
+
this.reconnectTimer = void 0;
|
|
547
|
+
}
|
|
548
|
+
this._stopPolling();
|
|
452
549
|
if (this.imap !== void 0) {
|
|
453
550
|
await this.imap.disconnect().catch(() => {
|
|
454
551
|
});
|