@im-more/dsh-mail 0.0.0-stage → 0.1.0

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.
Files changed (66) hide show
  1. package/LICENSE +21 -0
  2. package/cordis.patch.yml +14 -0
  3. package/lib/index.js +1334 -0
  4. package/lib/index.js.map +1 -0
  5. package/lib/types/bot.d.ts +49 -0
  6. package/lib/types/bot.d.ts.map +1 -0
  7. package/lib/types/bot.js +189 -0
  8. package/lib/types/bot.js.map +1 -0
  9. package/lib/types/config.d.ts +51 -0
  10. package/lib/types/config.d.ts.map +1 -0
  11. package/lib/types/config.js +55 -0
  12. package/lib/types/config.js.map +1 -0
  13. package/lib/types/decode.d.ts +60 -0
  14. package/lib/types/decode.d.ts.map +1 -0
  15. package/lib/types/decode.js +195 -0
  16. package/lib/types/decode.js.map +1 -0
  17. package/lib/types/errors.d.ts +33 -0
  18. package/lib/types/errors.d.ts.map +1 -0
  19. package/lib/types/errors.js +156 -0
  20. package/lib/types/errors.js.map +1 -0
  21. package/lib/types/html.d.ts +30 -0
  22. package/lib/types/html.d.ts.map +1 -0
  23. package/lib/types/html.js +121 -0
  24. package/lib/types/html.js.map +1 -0
  25. package/lib/types/index.d.ts +11 -0
  26. package/lib/types/index.d.ts.map +1 -0
  27. package/lib/types/index.js +10 -0
  28. package/lib/types/index.js.map +1 -0
  29. package/lib/types/mail.d.ts +24 -0
  30. package/lib/types/mail.d.ts.map +1 -0
  31. package/lib/types/mail.js +26 -0
  32. package/lib/types/mail.js.map +1 -0
  33. package/lib/types/media.d.ts +52 -0
  34. package/lib/types/media.d.ts.map +1 -0
  35. package/lib/types/media.js +81 -0
  36. package/lib/types/media.js.map +1 -0
  37. package/lib/types/modes/html.d.ts +13 -0
  38. package/lib/types/modes/html.d.ts.map +1 -0
  39. package/lib/types/modes/html.js +70 -0
  40. package/lib/types/modes/html.js.map +1 -0
  41. package/lib/types/modes/shared.d.ts +114 -0
  42. package/lib/types/modes/shared.d.ts.map +1 -0
  43. package/lib/types/modes/shared.js +185 -0
  44. package/lib/types/modes/shared.js.map +1 -0
  45. package/lib/types/send.d.ts +26 -0
  46. package/lib/types/send.d.ts.map +1 -0
  47. package/lib/types/send.js +58 -0
  48. package/lib/types/send.js.map +1 -0
  49. package/lib/types/transport/imap.d.ts +95 -0
  50. package/lib/types/transport/imap.d.ts.map +1 -0
  51. package/lib/types/transport/imap.js +354 -0
  52. package/lib/types/transport/imap.js.map +1 -0
  53. package/package.json +70 -3
  54. package/src/bot.ts +264 -0
  55. package/src/config.ts +98 -0
  56. package/src/decode.ts +275 -0
  57. package/src/errors.ts +199 -0
  58. package/src/html.ts +137 -0
  59. package/src/index.ts +32 -0
  60. package/src/mail.ts +46 -0
  61. package/src/media.ts +102 -0
  62. package/src/modes/html.ts +105 -0
  63. package/src/modes/shared.ts +285 -0
  64. package/src/send.ts +87 -0
  65. package/src/transport/imap.ts +418 -0
  66. package/README.md +0 -3
@@ -0,0 +1,60 @@
1
+ /**
2
+ * Inbound decoding: one parsed message becomes one {@link InboundMessage}.
3
+ *
4
+ * The message is addressed to the sender's own mailbox (`chat.id = private:<address>`), the `Message-ID` is the
5
+ * message identity (and the intake dedupe key), `In-Reply-To`/`References` become `quote`/`referrer`, and attachments
6
+ * become media elements whose `src` is the bot's `satori:` attachment route. Messages sent by the bot itself are
7
+ * skipped, because a copy arriving back in the mailbox would otherwise answer itself.
8
+ */
9
+ import type { InboundMessage } from '@deepseek-ai/cordis-plugin-im-core';
10
+ import type { ParsedMail } from 'mailparser';
11
+ import type { Config } from './config.ts';
12
+ /** Where one message was read from; the attachment route re-fetches by the same coordinates. */
13
+ export interface MailSource {
14
+ uid: number;
15
+ mailbox: string;
16
+ size: number;
17
+ }
18
+ /** Adapter-private context carried through `Target.referrer`. */
19
+ export interface MailReferrer {
20
+ /** The sender's address (the `From:` mailbox), the chat this message belongs to. */
21
+ address: string;
22
+ /** The message this one is, as delivered. */
23
+ messageId: string;
24
+ subject?: string;
25
+ /** `Reply-To:`, when the message names another answer address. */
26
+ replyTo?: string;
27
+ /** `References:`, oldest first, as `<…>` values. */
28
+ references?: string[];
29
+ /** IMAP UID this message was read at (attachment route). */
30
+ uid: number;
31
+ /** Mailbox this message was read from. */
32
+ mailbox: string;
33
+ }
34
+ /** The part of the bot inbound decoding uses. */
35
+ export interface MailDecoder {
36
+ readonly selfId: string;
37
+ readonly config: Config;
38
+ getInternalUrl(path: string, init?: Record<string, string>): string;
39
+ /** Reports a dropped `<sub>`/`<sup>`; optional so decoding needs no logger. */
40
+ readonly log?: (message: string) => void;
41
+ }
42
+ /** Which media element an attachment's MIME type maps to. */
43
+ export type AttachmentKind = 'img' | 'audio' | 'video' | 'file';
44
+ /**
45
+ * The element kind of one attachment.
46
+ * @param mime - the attachment's MIME type.
47
+ * @returns the vocabulary tag the attachment is shown as.
48
+ */
49
+ export declare function attachmentKind(mime: string): AttachmentKind;
50
+ /**
51
+ * Decode one parsed message.
52
+ *
53
+ * @param bot - the adapter the message arrived for (identity, mailbox, `satori:` route base).
54
+ * @param mail - the parsed MIME message.
55
+ * @param source - the IMAP coordinates the message was read at.
56
+ * @returns the inbound message, or `undefined` when the message cannot be routed (no sender address, or the bot's
57
+ * own message).
58
+ */
59
+ export declare function decodeMessage(bot: MailDecoder, mail: ParsedMail, source: MailSource): InboundMessage | undefined;
60
+ //# sourceMappingURL=decode.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"decode.d.ts","sourceRoot":"","sources":["../../src/decode.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAEV,cAAc,EAEf,MAAM,oCAAoC,CAAA;AAG3C,OAAO,KAAK,EAA+B,UAAU,EAAE,MAAM,YAAY,CAAA;AACzE,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AAGzC,gGAAgG;AAChG,MAAM,WAAW,UAAU;IACzB,GAAG,EAAE,MAAM,CAAA;IACX,OAAO,EAAE,MAAM,CAAA;IACf,IAAI,EAAE,MAAM,CAAA;CACb;AAED,iEAAiE;AACjE,MAAM,WAAW,YAAY;IAC3B,oFAAoF;IACpF,OAAO,EAAE,MAAM,CAAA;IACf,6CAA6C;IAC7C,SAAS,EAAE,MAAM,CAAA;IACjB,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,kEAAkE;IAClE,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,oDAAoD;IACpD,UAAU,CAAC,EAAE,MAAM,EAAE,CAAA;IACrB,4DAA4D;IAC5D,GAAG,EAAE,MAAM,CAAA;IACX,0CAA0C;IAC1C,OAAO,EAAE,MAAM,CAAA;CAChB;AAED,iDAAiD;AACjD,MAAM,WAAW,WAAW;IAC1B,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;IACvB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;IACvB,cAAc,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,MAAM,CAAA;IACnE,+EAA+E;IAC/E,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI,CAAA;CACzC;AAED,6DAA6D;AAC7D,MAAM,MAAM,cAAc,GAAG,KAAK,GAAG,OAAO,GAAG,OAAO,GAAG,MAAM,CAAA;AAE/D;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,cAAc,CAM3D;AAuGD;;;;;;;;GAQG;AACH,wBAAgB,aAAa,CAC3B,GAAG,EAAE,WAAW,EAChB,IAAI,EAAE,UAAU,EAChB,MAAM,EAAE,UAAU,GACjB,cAAc,GAAG,SAAS,CA2F5B"}
@@ -0,0 +1,195 @@
1
+ /**
2
+ * Inbound decoding: one parsed message becomes one {@link InboundMessage}.
3
+ *
4
+ * The message is addressed to the sender's own mailbox (`chat.id = private:<address>`), the `Message-ID` is the
5
+ * message identity (and the intake dedupe key), `In-Reply-To`/`References` become `quote`/`referrer`, and attachments
6
+ * become media elements whose `src` is the bot's `satori:` attachment route. Messages sent by the bot itself are
7
+ * skipped, because a copy arriving back in the mailbox would otherwise answer itself.
8
+ */
9
+ import { h } from '@deepseek-ai/satori-element';
10
+ import { htmlToElements } from "./html.js";
11
+ /**
12
+ * The element kind of one attachment.
13
+ * @param mime - the attachment's MIME type.
14
+ * @returns the vocabulary tag the attachment is shown as.
15
+ */
16
+ export function attachmentKind(mime) {
17
+ const type = mime.split(';')[0]?.trim().toLowerCase() ?? '';
18
+ if (type.startsWith('image/'))
19
+ return 'img';
20
+ if (type.startsWith('audio/'))
21
+ return 'audio';
22
+ if (type.startsWith('video/'))
23
+ return 'video';
24
+ return 'file';
25
+ }
26
+ /** Every address of the given headers, groups expanded; entries without an address are dropped. */
27
+ function addressesOf(...headers) {
28
+ const out = [];
29
+ for (const header of headers) {
30
+ if (header === undefined)
31
+ continue;
32
+ for (const object of Array.isArray(header) ? header : [header]) {
33
+ for (const item of object.value) {
34
+ const list = item.group ?? [item];
35
+ for (const address of list) {
36
+ if (address.address)
37
+ out.push(address);
38
+ }
39
+ }
40
+ }
41
+ }
42
+ return out;
43
+ }
44
+ /** `References:` as a list of `<…>` values (mailparser gives a string for a single reference). */
45
+ function referencesOf(mail) {
46
+ if (mail.references === undefined)
47
+ return [];
48
+ return Array.isArray(mail.references)
49
+ ? [...mail.references]
50
+ : [mail.references];
51
+ }
52
+ /** A stable id for a message that carries no `Message-ID`. */
53
+ function synthesizedId(uid, selfId) {
54
+ const at = selfId.lastIndexOf('@');
55
+ return `<${uid}@${at < 0 ? selfId : selfId.slice(at + 1)}>`;
56
+ }
57
+ /** The display name of an address, when it has one. */
58
+ function nameOf(address) {
59
+ const name = address.name.trim();
60
+ return name === '' ? undefined : name;
61
+ }
62
+ /** Media elements of the message's attachments, in MIME order, served by the attachment route. */
63
+ function attachmentElements(bot, mail, source) {
64
+ return mail.attachments.map((attachment, index) => {
65
+ const kind = attachmentKind(attachment.contentType);
66
+ const filename = attachment.filename ?? `attachment-${index + 1}`;
67
+ const src = bot.getInternalUrl(`/messages/${source.uid}/attachments/${index}`, { box: source.mailbox });
68
+ switch (kind) {
69
+ case 'img':
70
+ return h('img', {
71
+ src,
72
+ ...(attachment.filename === undefined
73
+ ? {}
74
+ : { alt: attachment.filename }),
75
+ });
76
+ case 'audio':
77
+ return h('audio', { src, name: filename });
78
+ case 'video':
79
+ return h('video', { src, name: filename });
80
+ case 'file':
81
+ return h('file', { src, name: filename });
82
+ }
83
+ });
84
+ }
85
+ /** The body elements of one message: the HTML traversal, or the plain text part, plus the attachments. */
86
+ function bodyElements(bot, mail, source) {
87
+ const body = [];
88
+ if (!mail.html) {
89
+ const text = mail.text ?? '';
90
+ if (text !== '')
91
+ body.push(h.text(text));
92
+ }
93
+ else {
94
+ const log = bot.log;
95
+ body.push(...htmlToElements(mail.html, log === undefined
96
+ ? {}
97
+ : {
98
+ onSubscript: () => {
99
+ log('mail: dropped <sub>/<sup>, the vocabulary has no subscript');
100
+ },
101
+ }));
102
+ }
103
+ body.push(...attachmentElements(bot, mail, source));
104
+ return body;
105
+ }
106
+ /**
107
+ * Decode one parsed message.
108
+ *
109
+ * @param bot - the adapter the message arrived for (identity, mailbox, `satori:` route base).
110
+ * @param mail - the parsed MIME message.
111
+ * @param source - the IMAP coordinates the message was read at.
112
+ * @returns the inbound message, or `undefined` when the message cannot be routed (no sender address, or the bot's
113
+ * own message).
114
+ */
115
+ export function decodeMessage(bot, mail, source) {
116
+ const from = mail.from?.value[0];
117
+ if (from?.address === undefined || from.address === '') {
118
+ bot.log?.('mail: dropped a message without a sender address');
119
+ return undefined;
120
+ }
121
+ if (from.address.toLowerCase() === bot.selfId.toLowerCase()) {
122
+ bot.log?.(`mail: dropped a message from the bot's own address (${from.address})`);
123
+ return undefined;
124
+ }
125
+ const address = from.address;
126
+ const name = nameOf(from);
127
+ const messageId = mail.messageId ?? synthesizedId(source.uid, bot.selfId);
128
+ const replyTo = mail.replyTo?.value[0]?.address;
129
+ const references = referencesOf(mail);
130
+ const body = bodyElements(bot, mail, source);
131
+ // Content must never be empty; a structural-only message becomes one `<raw>` child.
132
+ const content = h('message', {}, ...(body.length === 0
133
+ ? [
134
+ h('raw', {
135
+ platform: 'mail',
136
+ type: 'message',
137
+ data: {
138
+ messageId,
139
+ contentType: mail.headers.get('content-type'),
140
+ },
141
+ }, h.text('')),
142
+ ]
143
+ : body));
144
+ const recipients = addressesOf(mail.to, mail.cc);
145
+ const seen = new Set();
146
+ const users = [];
147
+ let mentionsSelf = false;
148
+ for (const recipient of recipients) {
149
+ if (recipient.address === undefined)
150
+ continue;
151
+ const lower = recipient.address.toLowerCase();
152
+ if (lower === bot.selfId.toLowerCase()) {
153
+ mentionsSelf = true;
154
+ continue;
155
+ }
156
+ if (seen.has(lower))
157
+ continue;
158
+ seen.add(lower);
159
+ const recipientName = nameOf(recipient);
160
+ users.push({
161
+ id: recipient.address,
162
+ ...(recipientName === undefined ? {} : { name: recipientName }),
163
+ });
164
+ }
165
+ const chat = {
166
+ id: `private:${address}`,
167
+ type: 'direct',
168
+ ...(name === undefined ? {} : { name }),
169
+ };
170
+ const sender = {
171
+ id: address,
172
+ ...(name === undefined ? {} : { name }),
173
+ };
174
+ const referrer = {
175
+ address,
176
+ messageId,
177
+ ...(mail.subject === undefined ? {} : { subject: mail.subject }),
178
+ ...(replyTo === undefined ? {} : { replyTo }),
179
+ ...(references.length === 0 ? {} : { references }),
180
+ uid: source.uid,
181
+ mailbox: source.mailbox,
182
+ };
183
+ return {
184
+ id: messageId,
185
+ chat,
186
+ sender,
187
+ content,
188
+ ...(mail.inReplyTo === undefined ? {} : { quote: { id: mail.inReplyTo } }),
189
+ mentions: { self: mentionsSelf, all: false, users },
190
+ time: mail.date === undefined ? Date.now() : +mail.date,
191
+ referrer,
192
+ raw: { uid: source.uid, mailbox: source.mailbox, size: source.size },
193
+ };
194
+ }
195
+ //# sourceMappingURL=decode.js.map
@@ -0,0 +1 @@
1
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AE,KAAK,EAAE;QACnD,IAAI,EAAE,IAAI,CAAC,IAAI,KAAK,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI;QACvD,QAAQ;QACR,GAAG,EAAE,EAAE,GAAG,EAAE,MAAM,CAAC,GAAG,EAAE,OAAO,EAAE,MAAM,CAAC,OAAO,EAAE,IAAI,EAAE,MAAM,CAAC,IAAI,EAAE;KACrE,CAAA;AACH,CAAC"}
@@ -0,0 +1,33 @@
1
+ /**
2
+ * SMTP failures and their mapping onto the core platform taxonomy.
3
+ *
4
+ * `nodemailer` reports one failed mail transaction as an `Error` carrying `code` (the stage), `command` (the SMTP
5
+ * verb) and `responseCode` (the server's numeric answer). The stage alone cannot say whether the message left this
6
+ * host: a connection loss after `DATA` started says nothing, while a refusal before the body was sent means the
7
+ * server certainly did not accept the message. `published` is decided from that, the error kind from the stage and
8
+ * the refined by the response code.
9
+ */
10
+ import { PlatformError } from '@deepseek-ai/cordis-plugin-im-core';
11
+ /** The fields a `nodemailer` SMTP error carries, all optional in practice. */
12
+ export interface SmtpFailure {
13
+ name?: string;
14
+ message?: string;
15
+ code?: string;
16
+ command?: string;
17
+ response?: string;
18
+ responseCode?: number;
19
+ cause?: unknown;
20
+ }
21
+ /**
22
+ * Map any failure of a publication onto the core taxonomy.
23
+ *
24
+ * `published: 'no'` is claimed only when the transaction provably had not handed the body over when it failed: an
25
+ * authentication or envelope refusal, a `DATA` command refusal, or a completed `DATA` the server rejected. A transport
26
+ * failure without a server answer leaves the outcome unknown, because `nodemailer` reports a connection that died
27
+ * while the body was in flight exactly like a refused connection; a `354` answer says the body had started.
28
+ *
29
+ * @param error - the caught failure.
30
+ * @returns the classified platform error.
31
+ */
32
+ export declare function classifyMailError(error: unknown): PlatformError;
33
+ //# sourceMappingURL=errors.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/errors.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAE,aAAa,EAAE,MAAM,oCAAoC,CAAA;AAGlE,8EAA8E;AAC9E,MAAM,WAAW,WAAW;IAC1B,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,YAAY,CAAC,EAAE,MAAM,CAAA;IACrB,KAAK,CAAC,EAAE,OAAO,CAAA;CAChB;AAuFD;;;;;;;;;;GAUG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,OAAO,GAAG,aAAa,CA8E/D"}
@@ -0,0 +1,156 @@
1
+ /**
2
+ * SMTP failures and their mapping onto the core platform taxonomy.
3
+ *
4
+ * `nodemailer` reports one failed mail transaction as an `Error` carrying `code` (the stage), `command` (the SMTP
5
+ * verb) and `responseCode` (the server's numeric answer). The stage alone cannot say whether the message left this
6
+ * host: a connection loss after `DATA` started says nothing, while a refusal before the body was sent means the
7
+ * server certainly did not accept the message. `published` is decided from that, the error kind from the stage and
8
+ * the refined by the response code.
9
+ */
10
+ import { PlatformError } from '@deepseek-ai/cordis-plugin-im-core';
11
+ /** Read the classification fields off a caught value (a non-`Error` carries none of them). */
12
+ function failureOf(error) {
13
+ if (error instanceof Error) {
14
+ const value = error;
15
+ return {
16
+ name: value.name,
17
+ message: value.message,
18
+ ...(typeof value.code === 'string' ? { code: value.code } : {}),
19
+ ...(typeof value.command === 'string' ? { command: value.command } : {}),
20
+ ...(typeof value.response === 'string'
21
+ ? { response: value.response }
22
+ : {}),
23
+ ...(typeof value.responseCode === 'number'
24
+ ? { responseCode: value.responseCode }
25
+ : {}),
26
+ };
27
+ }
28
+ return { message: String(error) };
29
+ }
30
+ /** Whether a caught value is the abort error of a cancelled operation. */
31
+ function isCancellation(error) {
32
+ return ((error instanceof Error || error instanceof DOMException) &&
33
+ error.name === 'AbortError');
34
+ }
35
+ /** Whether a response code is a temporary refusal (RFC 5321 §4.2). */
36
+ function isTemporary(code) {
37
+ return code >= 400 && code < 500;
38
+ }
39
+ /** Whether a response code is a permanent refusal (RFC 5321 §4.2). */
40
+ function isPermanent(code) {
41
+ return code >= 500 && code < 600;
42
+ }
43
+ /**
44
+ * Refine a stage's default kind with the meaning of the server's response code.
45
+ * @param code - the numeric SMTP response, when the error carried one.
46
+ * @param fallback - the kind the stage implies on its own.
47
+ * @returns the refined kind.
48
+ */
49
+ function refineKind(code, fallback) {
50
+ switch (code) {
51
+ case 421:
52
+ case 450:
53
+ case 451:
54
+ return 'retryable';
55
+ case 452:
56
+ return 'rate-limit';
57
+ case 534:
58
+ case 535:
59
+ return 'permission';
60
+ case 550:
61
+ return 'not-found';
62
+ case 552:
63
+ return 'limit';
64
+ case 553:
65
+ case 554:
66
+ return 'invalid';
67
+ }
68
+ if (isTemporary(code))
69
+ return 'retryable';
70
+ if (isPermanent(code))
71
+ return 'invalid';
72
+ return fallback;
73
+ }
74
+ /** The `RCPT TO` kind of one rejection (550 names a missing mailbox, other 5xx codes deny the recipient). */
75
+ function recipientKind(code) {
76
+ if (code === 550)
77
+ return 'not-found';
78
+ if (isPermanent(code))
79
+ return 'permission';
80
+ return refineKind(code, 'retryable');
81
+ }
82
+ /** Whether the error says the message grew past the server's size limit before `DATA` began. */
83
+ function isOversized(failure) {
84
+ return /larger than allowed|message size|maximum message size/i.test(failure.message ?? failure.response ?? '');
85
+ }
86
+ /**
87
+ * Map any failure of a publication onto the core taxonomy.
88
+ *
89
+ * `published: 'no'` is claimed only when the transaction provably had not handed the body over when it failed: an
90
+ * authentication or envelope refusal, a `DATA` command refusal, or a completed `DATA` the server rejected. A transport
91
+ * failure without a server answer leaves the outcome unknown, because `nodemailer` reports a connection that died
92
+ * while the body was in flight exactly like a refused connection; a `354` answer says the body had started.
93
+ *
94
+ * @param error - the caught failure.
95
+ * @returns the classified platform error.
96
+ */
97
+ export function classifyMailError(error) {
98
+ if (error instanceof PlatformError)
99
+ return error;
100
+ const failure = failureOf(error);
101
+ const message = failure.message === undefined
102
+ ? 'mail: send failed'
103
+ : `mail: ${failure.message}`;
104
+ const cause = error;
105
+ const code = failure.responseCode;
106
+ const codeInit = code === undefined ? {} : { code };
107
+ const make = (kind, published) => new PlatformError(kind, message, { ...codeInit, published, cause });
108
+ if (isCancellation(error))
109
+ return new PlatformError('fatal', message, { published: 'unknown', cause });
110
+ switch (failure.code) {
111
+ case 'EAUTH':
112
+ return make('permission', 'no');
113
+ case 'EENVELOPE':
114
+ switch (failure.command) {
115
+ case 'MAIL FROM':
116
+ // A 550 here rejects the sender identity, not a missing mailbox; every other code keeps its RFC meaning.
117
+ return make(code === 550 || code === undefined
118
+ ? 'permission'
119
+ : refineKind(code, 'permission'), 'no');
120
+ case 'RCPT TO':
121
+ // The last RCPT answer is the one recorded; all-recipients-rejected keeps it.
122
+ return make(code === undefined ? 'invalid' : recipientKind(code), 'no');
123
+ case 'DATA':
124
+ return make(code === undefined ? 'invalid' : refineKind(code, 'invalid'), 'no');
125
+ default:
126
+ // "No recipients defined" and other local envelope checks: nothing was sent.
127
+ return make(code === undefined ? 'invalid' : refineKind(code, 'invalid'), 'no');
128
+ }
129
+ case 'EMESSAGE':
130
+ if (failure.command === 'MAIL FROM' && isOversized(failure))
131
+ return make('limit', 'no');
132
+ if (failure.command === 'DATA')
133
+ return make(code === undefined ? 'invalid' : refineKind(code, 'invalid'), 'no');
134
+ return make(code === undefined ? 'invalid' : refineKind(code, 'invalid'), 'no');
135
+ case 'ECONNECTION':
136
+ case 'ETIMEDOUT': {
137
+ // `nodemailer` reports a socket that died while the body was in flight exactly like a refused connection: no
138
+ // server answer is attached, so whether the message left this host cannot be told. Claiming `no` there would
139
+ // invite a duplicate delivery, so an answerless transport failure stays unknown. A server answer can be told:
140
+ // `354` means the body had started, any other code means the transaction had not reached that point.
141
+ if (code === undefined)
142
+ return make('retryable', 'unknown');
143
+ const published = code === 354 ? 'unknown' : 'no';
144
+ return make(refineKind(code, 'retryable'), published);
145
+ }
146
+ case 'EDNS':
147
+ return make('retryable', 'no');
148
+ case 'EPROTOCOL':
149
+ return make('retryable', 'unknown');
150
+ case 'ESTREAM':
151
+ return make('invalid', 'no');
152
+ default:
153
+ return make('retryable', 'unknown');
154
+ }
155
+ }
156
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.js","sourceRoot":"","sources":["../../src/errors.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAE,aAAa,EAAE,MAAM,oCAAoC,CAAA;AAclE,8FAA8F;AAC9F,SAAS,SAAS,CAAC,KAAc;IAC/B,IAAI,KAAK,YAAY,KAAK,EAAE,CAAC;QAC3B,MAAM,KAAK,GAAG,KAA4B,CAAA;QAC1C,OAAO;YACL,IAAI,EAAE,KAAK,CAAC,IAAI;YAChB,OAAO,EAAE,KAAK,CAAC,OAAO;YACtB,GAAG,CAAC,OAAO,KAAK,CAAC,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,KAAK,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;YAC/D,GAAG,CAAC,OAAO,KAAK,CAAC,OAAO,KAAK,QAAQ,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,KAAK,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;YACxE,GAAG,CAAC,OAAO,KAAK,CAAC,QAAQ,KAAK,QAAQ;gBACpC,CAAC,CAAC,EAAE,QAAQ,EAAE,KAAK,CAAC,QAAQ,EAAE;gBAC9B,CAAC,CAAC,EAAE,CAAC;YACP,GAAG,CAAC,OAAO,KAAK,CAAC,YAAY,KAAK,QAAQ;gBACxC,CAAC,CAAC,EAAE,YAAY,EAAE,KAAK,CAAC,YAAY,EAAE;gBACtC,CAAC,CAAC,EAAE,CAAC;SACR,CAA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C,CAAC,CAAC;YACjB,6GAA6G;YAC7G,6GAA6G;YAC7G,8GAA8G;YAC9G,qGAAqG;YACrG,IAAI,IAAI,KAAK,SAAS;gBAAE,OAAO,IAAI,CAAC,WAAW,EAAE,SAAS,CAAC,CAAA;YAC3D,MAAM,SAAS,GAAG,IAAI,KAAK,GAAG,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,IAAI,CAAA;YACjD,OAAO,IAAI,CAAC,UAAU,CAAC,IAAI,EAAE,WAAW,CAAC,EAAE,SAAS,CAAC,CAAA;QACvD,CAAC;QACD,KAAK,MAAM;YACT,OAAO,IAAI,CAAC,WAAW,EAAE,IAAI,CAAC,CAAA;QAChC,KAAK,WAAW;YACd,OAAO,IAAI,CAAC,WAAW,EAAE,SAAS,CAAC,CAAA;QACrC,KAAK,SAAS;YACZ,OAAO,IAAI,CAAC,SAAS,EAAE,IAAI,CAAC,CAAA;QAC9B;YACE,OAAO,IAAI,CAAC,WAAW,EAAE,SAAS,CAAC,CAAA;IACvC,CAAC;AACH,CAAC"}
@@ -0,0 +1,30 @@
1
+ /**
2
+ * HTML body decoding: an HTML string from `mailparser` becomes Satori elements, following the upstream traversal
3
+ * (`html5parser` + `visit`) instead of a text extraction.
4
+ *
5
+ * Two deliberate deviations from upstream, both one-line bug fixes (design §5.2): an unknown named character
6
+ * reference is kept literally instead of replacing the whole text node, and an image's `alt` is read from `alt`
7
+ * rather than from `src`.
8
+ */
9
+ import type { Element } from '@deepseek-ai/satori-element';
10
+ /**
11
+ * Decode named, decimal and hexadecimal character references of one text run.
12
+ *
13
+ * A named reference outside {@link entities} is not an error: it stays as written. (Upstream returned the whole text
14
+ * node in that case, which duplicated everything around the unknown reference.)
15
+ *
16
+ * @param text - raw character data of one HTML text node.
17
+ * @returns the text with the known references decoded.
18
+ */
19
+ export declare function decodeHE(text: string): string;
20
+ /**
21
+ * Convert an HTML body into elements.
22
+ *
23
+ * @param html - the `text/html` part `mailparser` decoded.
24
+ * @param options - `onSubscript` reports a dropped `<sub>`/`<sup>` (the vocabulary has neither).
25
+ * @returns the elements of the body, in document order.
26
+ */
27
+ export declare function htmlToElements(html: string, options?: {
28
+ onSubscript?: () => void;
29
+ }): Element[];
30
+ //# sourceMappingURL=html.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"html.d.ts","sourceRoot":"","sources":["../../src/html.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAGH,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,6BAA6B,CAAA;AAoB1D;;;;;;;;GAQG;AACH,wBAAgB,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAiB7C;AAUD;;;;;;GAMG;AACH,wBAAgB,cAAc,CAC5B,IAAI,EAAE,MAAM,EACZ,OAAO,GAAE;IAAE,WAAW,CAAC,EAAE,MAAM,IAAI,CAAA;CAAO,GACzC,OAAO,EAAE,CA4DX"}
@@ -0,0 +1,121 @@
1
+ /**
2
+ * HTML body decoding: an HTML string from `mailparser` becomes Satori elements, following the upstream traversal
3
+ * (`html5parser` + `visit`) instead of a text extraction.
4
+ *
5
+ * Two deliberate deviations from upstream, both one-line bug fixes (design §5.2): an unknown named character
6
+ * reference is kept literally instead of replacing the whole text node, and an image's `alt` is read from `alt`
7
+ * rather than from `src`.
8
+ */
9
+ import { h } from '@deepseek-ai/satori-element';
10
+ import { parse, SyntaxKind } from 'html5parser';
11
+ /** The named character references the traversal decodes (upstream's table). */
12
+ const entities = {
13
+ nbsp: ' ',
14
+ cent: '¢',
15
+ pound: '£',
16
+ yen: '¥',
17
+ euro: '€',
18
+ copy: '©',
19
+ reg: '®',
20
+ lt: '<',
21
+ gt: '>',
22
+ quot: '"',
23
+ amp: '&',
24
+ apos: "'",
25
+ };
26
+ /**
27
+ * Decode named, decimal and hexadecimal character references of one text run.
28
+ *
29
+ * A named reference outside {@link entities} is not an error: it stays as written. (Upstream returned the whole text
30
+ * node in that case, which duplicated everything around the unknown reference.)
31
+ *
32
+ * @param text - raw character data of one HTML text node.
33
+ * @returns the text with the known references decoded.
34
+ */
35
+ export function decodeHE(text) {
36
+ const regex = /&(([a-z0-9]+)|(#[0-9]{1,6})|(#x[0-9a-fA-F]{1,6}));/gi;
37
+ return text.replace(regex, (raw, _whole, name, dec, hex) => {
38
+ if (name)
39
+ return entities[name] ?? raw;
40
+ if (dec)
41
+ return String.fromCharCode(Number(dec.substring(1)));
42
+ if (hex)
43
+ return String.fromCharCode(parseInt(hex.substring(2), 16));
44
+ return raw;
45
+ });
46
+ }
47
+ /** One attribute of a parsed tag, as written in the source. */
48
+ function attribute(node, name) {
49
+ return node.attributeMap?.[name]?.value?.value;
50
+ }
51
+ /** Tags whose whole subtree is not part of the message body. */
52
+ const IGNORED = new Set(['head', 'script', 'style', 'meta']);
53
+ /**
54
+ * Convert an HTML body into elements.
55
+ *
56
+ * @param html - the `text/html` part `mailparser` decoded.
57
+ * @param options - `onSubscript` reports a dropped `<sub>`/`<sup>` (the vocabulary has neither).
58
+ * @returns the elements of the body, in document order.
59
+ */
60
+ export function htmlToElements(html, options = {}) {
61
+ const visit = (nodes) => {
62
+ const out = [];
63
+ for (const node of nodes) {
64
+ if (node.type === SyntaxKind.Text) {
65
+ out.push(h.text(decodeHE(node.value).trim() || ' '));
66
+ continue;
67
+ }
68
+ const body = node.body ?? [];
69
+ switch (node.name) {
70
+ case 'a':
71
+ out.push(h('a', { href: attribute(node, 'href') || '#' }, ...visit(body)));
72
+ break;
73
+ case 'strong':
74
+ case 'b':
75
+ out.push(h('b', ...visit(body)));
76
+ break;
77
+ case 'br':
78
+ out.push(h('br'));
79
+ break;
80
+ case 'code':
81
+ out.push(h('code', ...visit(body)));
82
+ break;
83
+ case 's':
84
+ case 'del':
85
+ out.push(h('s', ...visit(body)));
86
+ break;
87
+ case 'i':
88
+ case 'em':
89
+ out.push(h('i', ...visit(body)));
90
+ break;
91
+ case 'p':
92
+ out.push(h('p', ...visit(body)));
93
+ break;
94
+ case 'sub':
95
+ case 'sup':
96
+ // The vocabulary has neither: the text stays, the position is lost.
97
+ options.onSubscript?.();
98
+ out.push(...visit(body));
99
+ break;
100
+ case 'u':
101
+ out.push(h('u', ...visit(body)));
102
+ break;
103
+ case 'img': {
104
+ const src = attribute(node, 'src');
105
+ if (src === undefined || !/^(data|https?):/i.test(src))
106
+ break;
107
+ const alt = attribute(node, 'alt');
108
+ out.push(h('img', { src, ...(alt === undefined ? {} : { alt }) }));
109
+ break;
110
+ }
111
+ default:
112
+ if (IGNORED.has(node.name))
113
+ break;
114
+ out.push(...visit(body));
115
+ }
116
+ }
117
+ return out;
118
+ };
119
+ return visit(parse(html, { setAttributeMap: true }));
120
+ }
121
+ //# sourceMappingURL=html.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"html.js","sourceRoot":"","sources":["../../src/html.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,CAAC,EAAE,MAAM,6BAA6B,CAAA;AAE/C,OAAO,EAAE,KAAK,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AAG/C,+EAA+E;AAC/E,MAAM,QAAQ,GAA2B;IACvC,IAAI,EAAE,GAAG;IACT,IAAI,EAAE,GAAG;IACT,KAAK,EAAE,GAAG;IACV,GAAG,EAAE,GAAG;IACR,IAAI,EAAE,GAAG;IACT,IAAI,EAAE,GAAG;IACT,GAAG,EAAE,GAAG;IACR,EAAE,EAAE,GAAG;IACP,EAAE,EAAE,GAAG;IACP,IAAI,EAAE,GAAG;IACT,GAAG,EAAE,GAAG;IACR,IAAI,EAAE,GAAG;CACV,CAAA;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,QAAQ,CAAC,IAAY;IACnC,MAAM,KAAK,GAAG,sDAAsD,CAAA;IACpE,OAAO,IAAI,CAAC,OAAO,CACjB,KAAK,EACL,CACE,GAAW,EACX,MAAc,EACd,IAAa,EACb,GAAY,EACZ,GAAY,EACZ,EAAE;QACF,IAAI,IAAI;YAAE,OAAO,QAAQ,CAAC,IA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AAE,GAAG,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,CAAA;oBAChC,MAAK;gBACP,KAAK,KAAK,CAAC,CAAC,CAAC;oBACX,MAAM,GAAG,GAAG,SAAS,CAAC,IAAI,EAAE,KAAK,CAAC,CAAA;oBAClC,IAAI,GAAG,KAAK,SAAS,IAAI,CAAC,kBAAkB,CAAC,IAAI,CAAC,GAAG,CAAC;wBAAE,MAAK;oBAC7D,MAAM,GAAG,GAAG,SAAS,CAAC,IAAI,EAAE,KAAK,CAAC,CAAA;oBAClC,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,EAAE,EAAE,GAAG,EAAE,GAAG,CAAC,GAAG,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,CAAC,CAAC,CAAA;oBAClE,MAAK;gBACP,CAAC;gBACD;oBACE,IAAI,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC;wBAAE,MAAK;oBACjC,GAAG,CAAC,IAAI,CAAC,GAAG,KAAK,CAAC,IAAI,CAAC,CAAC,CAAA;YAC5B,CAAC;QACH,CAAC;QACD,OAAO,GAAG,CAAA;IACZ,CAAC,CAAA;IACD,OAAO,KAAK,CAAC,KAAK,CAAC,IAAI,EAAE,EAAE,eAAe,EAAE,IAAI,EAAE,CAAC,CAAC,CAAA;AACtD,CAAC"}
@@ -0,0 +1,11 @@
1
+ export { MailBot, MailBot as default, type MailModes } from './bot.ts';
2
+ export { addressOf, Config, type Config as MailConfig } from './config.ts';
3
+ export { attachmentKind, decodeMessage, type AttachmentKind, type MailDecoder, type MailReferrer, type MailSource, } from './decode.ts';
4
+ export { classifyMailError, type SmtpFailure } from './errors.ts';
5
+ export { MediaStaging, type StagedMedia, type StagedMediaExtra, } from './media.ts';
6
+ export { createHtmlMode } from './modes/html.ts';
7
+ export type { MailDraft, MailModeTypes, MailNode, MailPayload, MailPayloadAttachment, } from './modes/shared.ts';
8
+ export { sendPayload, type MailSendDeps } from './send.ts';
9
+ export { ImapTransport, type ImapError, type ImapTransportOptions, type MailLogger, } from './transport/imap.ts';
10
+ export { SMTP, type Attachment, type SendOptions } from './mail.ts';
11
+ //# sourceMappingURL=index.d.ts.map