@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.
- package/LICENSE +21 -0
- package/cordis.patch.yml +14 -0
- package/lib/index.js +1334 -0
- package/lib/index.js.map +1 -0
- package/lib/types/bot.d.ts +49 -0
- package/lib/types/bot.d.ts.map +1 -0
- package/lib/types/bot.js +189 -0
- package/lib/types/bot.js.map +1 -0
- package/lib/types/config.d.ts +51 -0
- package/lib/types/config.d.ts.map +1 -0
- package/lib/types/config.js +55 -0
- package/lib/types/config.js.map +1 -0
- package/lib/types/decode.d.ts +60 -0
- package/lib/types/decode.d.ts.map +1 -0
- package/lib/types/decode.js +195 -0
- package/lib/types/decode.js.map +1 -0
- package/lib/types/errors.d.ts +33 -0
- package/lib/types/errors.d.ts.map +1 -0
- package/lib/types/errors.js +156 -0
- package/lib/types/errors.js.map +1 -0
- package/lib/types/html.d.ts +30 -0
- package/lib/types/html.d.ts.map +1 -0
- package/lib/types/html.js +121 -0
- package/lib/types/html.js.map +1 -0
- package/lib/types/index.d.ts +11 -0
- package/lib/types/index.d.ts.map +1 -0
- package/lib/types/index.js +10 -0
- package/lib/types/index.js.map +1 -0
- package/lib/types/mail.d.ts +24 -0
- package/lib/types/mail.d.ts.map +1 -0
- package/lib/types/mail.js +26 -0
- package/lib/types/mail.js.map +1 -0
- package/lib/types/media.d.ts +52 -0
- package/lib/types/media.d.ts.map +1 -0
- package/lib/types/media.js +81 -0
- package/lib/types/media.js.map +1 -0
- package/lib/types/modes/html.d.ts +13 -0
- package/lib/types/modes/html.d.ts.map +1 -0
- package/lib/types/modes/html.js +70 -0
- package/lib/types/modes/html.js.map +1 -0
- package/lib/types/modes/shared.d.ts +114 -0
- package/lib/types/modes/shared.d.ts.map +1 -0
- package/lib/types/modes/shared.js +185 -0
- package/lib/types/modes/shared.js.map +1 -0
- package/lib/types/send.d.ts +26 -0
- package/lib/types/send.d.ts.map +1 -0
- package/lib/types/send.js +58 -0
- package/lib/types/send.js.map +1 -0
- package/lib/types/transport/imap.d.ts +95 -0
- package/lib/types/transport/imap.d.ts.map +1 -0
- package/lib/types/transport/imap.js +354 -0
- package/lib/types/transport/imap.js.map +1 -0
- package/package.json +70 -3
- package/src/bot.ts +264 -0
- package/src/config.ts +98 -0
- package/src/decode.ts +275 -0
- package/src/errors.ts +199 -0
- package/src/html.ts +137 -0
- package/src/index.ts +32 -0
- package/src/mail.ts +46 -0
- package/src/media.ts +102 -0
- package/src/modes/html.ts +105 -0
- package/src/modes/shared.ts +285 -0
- package/src/send.ts +87 -0
- package/src/transport/imap.ts +418 -0
- package/README.md +0 -3
package/src/errors.ts
ADDED
|
@@ -0,0 +1,199 @@
|
|
|
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
|
+
|
|
11
|
+
import { PlatformError } from '@deepseek-ai/cordis-plugin-im-core'
|
|
12
|
+
import type { PlatformErrorKind } from '@deepseek-ai/cordis-plugin-im-core'
|
|
13
|
+
|
|
14
|
+
/** The fields a `nodemailer` SMTP error carries, all optional in practice. */
|
|
15
|
+
export interface SmtpFailure {
|
|
16
|
+
name?: string
|
|
17
|
+
message?: string
|
|
18
|
+
code?: string
|
|
19
|
+
command?: string
|
|
20
|
+
response?: string
|
|
21
|
+
responseCode?: number
|
|
22
|
+
cause?: unknown
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Read the classification fields off a caught value (a non-`Error` carries none of them). */
|
|
26
|
+
function failureOf(error: unknown): SmtpFailure {
|
|
27
|
+
if (error instanceof Error) {
|
|
28
|
+
const value = error as Error & SmtpFailure
|
|
29
|
+
return {
|
|
30
|
+
name: value.name,
|
|
31
|
+
message: value.message,
|
|
32
|
+
...(typeof value.code === 'string' ? { code: value.code } : {}),
|
|
33
|
+
...(typeof value.command === 'string' ? { command: value.command } : {}),
|
|
34
|
+
...(typeof value.response === 'string'
|
|
35
|
+
? { response: value.response }
|
|
36
|
+
: {}),
|
|
37
|
+
...(typeof value.responseCode === 'number'
|
|
38
|
+
? { responseCode: value.responseCode }
|
|
39
|
+
: {}),
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return { message: String(error) }
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Whether a caught value is the abort error of a cancelled operation. */
|
|
46
|
+
function isCancellation(error: unknown): boolean {
|
|
47
|
+
return (
|
|
48
|
+
(error instanceof Error || error instanceof DOMException) &&
|
|
49
|
+
error.name === 'AbortError'
|
|
50
|
+
)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Whether a response code is a temporary refusal (RFC 5321 §4.2). */
|
|
54
|
+
function isTemporary(code: number): boolean {
|
|
55
|
+
return code >= 400 && code < 500
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Whether a response code is a permanent refusal (RFC 5321 §4.2). */
|
|
59
|
+
function isPermanent(code: number): boolean {
|
|
60
|
+
return code >= 500 && code < 600
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Refine a stage's default kind with the meaning of the server's response code.
|
|
65
|
+
* @param code - the numeric SMTP response, when the error carried one.
|
|
66
|
+
* @param fallback - the kind the stage implies on its own.
|
|
67
|
+
* @returns the refined kind.
|
|
68
|
+
*/
|
|
69
|
+
function refineKind(
|
|
70
|
+
code: number,
|
|
71
|
+
fallback: PlatformErrorKind,
|
|
72
|
+
): PlatformErrorKind {
|
|
73
|
+
switch (code) {
|
|
74
|
+
case 421:
|
|
75
|
+
case 450:
|
|
76
|
+
case 451:
|
|
77
|
+
return 'retryable'
|
|
78
|
+
case 452:
|
|
79
|
+
return 'rate-limit'
|
|
80
|
+
case 534:
|
|
81
|
+
case 535:
|
|
82
|
+
return 'permission'
|
|
83
|
+
case 550:
|
|
84
|
+
return 'not-found'
|
|
85
|
+
case 552:
|
|
86
|
+
return 'limit'
|
|
87
|
+
case 553:
|
|
88
|
+
case 554:
|
|
89
|
+
return 'invalid'
|
|
90
|
+
}
|
|
91
|
+
if (isTemporary(code)) return 'retryable'
|
|
92
|
+
if (isPermanent(code)) return 'invalid'
|
|
93
|
+
return fallback
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** The `RCPT TO` kind of one rejection (550 names a missing mailbox, other 5xx codes deny the recipient). */
|
|
97
|
+
function recipientKind(code: number): PlatformErrorKind {
|
|
98
|
+
if (code === 550) return 'not-found'
|
|
99
|
+
if (isPermanent(code)) return 'permission'
|
|
100
|
+
return refineKind(code, 'retryable')
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** Whether the error says the message grew past the server's size limit before `DATA` began. */
|
|
104
|
+
function isOversized(failure: SmtpFailure): boolean {
|
|
105
|
+
return /larger than allowed|message size|maximum message size/i.test(
|
|
106
|
+
failure.message ?? failure.response ?? '',
|
|
107
|
+
)
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Map any failure of a publication onto the core taxonomy.
|
|
112
|
+
*
|
|
113
|
+
* `published: 'no'` is claimed only when the transaction provably had not handed the body over when it failed: an
|
|
114
|
+
* authentication or envelope refusal, a `DATA` command refusal, or a completed `DATA` the server rejected. A transport
|
|
115
|
+
* failure without a server answer leaves the outcome unknown, because `nodemailer` reports a connection that died
|
|
116
|
+
* while the body was in flight exactly like a refused connection; a `354` answer says the body had started.
|
|
117
|
+
*
|
|
118
|
+
* @param error - the caught failure.
|
|
119
|
+
* @returns the classified platform error.
|
|
120
|
+
*/
|
|
121
|
+
export function classifyMailError(error: unknown): PlatformError {
|
|
122
|
+
if (error instanceof PlatformError) return error
|
|
123
|
+
const failure = failureOf(error)
|
|
124
|
+
const message =
|
|
125
|
+
failure.message === undefined
|
|
126
|
+
? 'mail: send failed'
|
|
127
|
+
: `mail: ${failure.message}`
|
|
128
|
+
const cause = error
|
|
129
|
+
const code = failure.responseCode
|
|
130
|
+
const codeInit = code === undefined ? {} : { code }
|
|
131
|
+
const make = (kind: PlatformErrorKind, published: 'no' | 'unknown') =>
|
|
132
|
+
new PlatformError(kind, message, { ...codeInit, published, cause })
|
|
133
|
+
|
|
134
|
+
if (isCancellation(error))
|
|
135
|
+
return new PlatformError('fatal', message, { published: 'unknown', cause })
|
|
136
|
+
|
|
137
|
+
switch (failure.code) {
|
|
138
|
+
case 'EAUTH':
|
|
139
|
+
return make('permission', 'no')
|
|
140
|
+
case 'EENVELOPE':
|
|
141
|
+
switch (failure.command) {
|
|
142
|
+
case 'MAIL FROM':
|
|
143
|
+
// A 550 here rejects the sender identity, not a missing mailbox; every other code keeps its RFC meaning.
|
|
144
|
+
return make(
|
|
145
|
+
code === 550 || code === undefined
|
|
146
|
+
? 'permission'
|
|
147
|
+
: refineKind(code, 'permission'),
|
|
148
|
+
'no',
|
|
149
|
+
)
|
|
150
|
+
case 'RCPT TO':
|
|
151
|
+
// The last RCPT answer is the one recorded; all-recipients-rejected keeps it.
|
|
152
|
+
return make(
|
|
153
|
+
code === undefined ? 'invalid' : recipientKind(code),
|
|
154
|
+
'no',
|
|
155
|
+
)
|
|
156
|
+
case 'DATA':
|
|
157
|
+
return make(
|
|
158
|
+
code === undefined ? 'invalid' : refineKind(code, 'invalid'),
|
|
159
|
+
'no',
|
|
160
|
+
)
|
|
161
|
+
default:
|
|
162
|
+
// "No recipients defined" and other local envelope checks: nothing was sent.
|
|
163
|
+
return make(
|
|
164
|
+
code === undefined ? 'invalid' : refineKind(code, 'invalid'),
|
|
165
|
+
'no',
|
|
166
|
+
)
|
|
167
|
+
}
|
|
168
|
+
case 'EMESSAGE':
|
|
169
|
+
if (failure.command === 'MAIL FROM' && isOversized(failure))
|
|
170
|
+
return make('limit', 'no')
|
|
171
|
+
if (failure.command === 'DATA')
|
|
172
|
+
return make(
|
|
173
|
+
code === undefined ? 'invalid' : refineKind(code, 'invalid'),
|
|
174
|
+
'no',
|
|
175
|
+
)
|
|
176
|
+
return make(
|
|
177
|
+
code === undefined ? 'invalid' : refineKind(code, 'invalid'),
|
|
178
|
+
'no',
|
|
179
|
+
)
|
|
180
|
+
case 'ECONNECTION':
|
|
181
|
+
case 'ETIMEDOUT': {
|
|
182
|
+
// `nodemailer` reports a socket that died while the body was in flight exactly like a refused connection: no
|
|
183
|
+
// server answer is attached, so whether the message left this host cannot be told. Claiming `no` there would
|
|
184
|
+
// invite a duplicate delivery, so an answerless transport failure stays unknown. A server answer can be told:
|
|
185
|
+
// `354` means the body had started, any other code means the transaction had not reached that point.
|
|
186
|
+
if (code === undefined) return make('retryable', 'unknown')
|
|
187
|
+
const published = code === 354 ? 'unknown' : 'no'
|
|
188
|
+
return make(refineKind(code, 'retryable'), published)
|
|
189
|
+
}
|
|
190
|
+
case 'EDNS':
|
|
191
|
+
return make('retryable', 'no')
|
|
192
|
+
case 'EPROTOCOL':
|
|
193
|
+
return make('retryable', 'unknown')
|
|
194
|
+
case 'ESTREAM':
|
|
195
|
+
return make('invalid', 'no')
|
|
196
|
+
default:
|
|
197
|
+
return make('retryable', 'unknown')
|
|
198
|
+
}
|
|
199
|
+
}
|
package/src/html.ts
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
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
|
+
|
|
10
|
+
import { h } from '@deepseek-ai/satori-element'
|
|
11
|
+
import type { Element } from '@deepseek-ai/satori-element'
|
|
12
|
+
import { parse, SyntaxKind } from 'html5parser'
|
|
13
|
+
import type { INode, ITag } from 'html5parser'
|
|
14
|
+
|
|
15
|
+
/** The named character references the traversal decodes (upstream's table). */
|
|
16
|
+
const entities: Record<string, string> = {
|
|
17
|
+
nbsp: ' ',
|
|
18
|
+
cent: '¢',
|
|
19
|
+
pound: '£',
|
|
20
|
+
yen: '¥',
|
|
21
|
+
euro: '€',
|
|
22
|
+
copy: '©',
|
|
23
|
+
reg: '®',
|
|
24
|
+
lt: '<',
|
|
25
|
+
gt: '>',
|
|
26
|
+
quot: '"',
|
|
27
|
+
amp: '&',
|
|
28
|
+
apos: "'",
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Decode named, decimal and hexadecimal character references of one text run.
|
|
33
|
+
*
|
|
34
|
+
* A named reference outside {@link entities} is not an error: it stays as written. (Upstream returned the whole text
|
|
35
|
+
* node in that case, which duplicated everything around the unknown reference.)
|
|
36
|
+
*
|
|
37
|
+
* @param text - raw character data of one HTML text node.
|
|
38
|
+
* @returns the text with the known references decoded.
|
|
39
|
+
*/
|
|
40
|
+
export function decodeHE(text: string): string {
|
|
41
|
+
const regex = /&(([a-z0-9]+)|(#[0-9]{1,6})|(#x[0-9a-fA-F]{1,6}));/gi
|
|
42
|
+
return text.replace(
|
|
43
|
+
regex,
|
|
44
|
+
(
|
|
45
|
+
raw: string,
|
|
46
|
+
_whole: string,
|
|
47
|
+
name?: string,
|
|
48
|
+
dec?: string,
|
|
49
|
+
hex?: string,
|
|
50
|
+
) => {
|
|
51
|
+
if (name) return entities[name] ?? raw
|
|
52
|
+
if (dec) return String.fromCharCode(Number(dec.substring(1)))
|
|
53
|
+
if (hex) return String.fromCharCode(parseInt(hex.substring(2), 16))
|
|
54
|
+
return raw
|
|
55
|
+
},
|
|
56
|
+
)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** One attribute of a parsed tag, as written in the source. */
|
|
60
|
+
function attribute(node: ITag, name: string): string | undefined {
|
|
61
|
+
return node.attributeMap?.[name]?.value?.value
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Tags whose whole subtree is not part of the message body. */
|
|
65
|
+
const IGNORED = new Set(['head', 'script', 'style', 'meta'])
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Convert an HTML body into elements.
|
|
69
|
+
*
|
|
70
|
+
* @param html - the `text/html` part `mailparser` decoded.
|
|
71
|
+
* @param options - `onSubscript` reports a dropped `<sub>`/`<sup>` (the vocabulary has neither).
|
|
72
|
+
* @returns the elements of the body, in document order.
|
|
73
|
+
*/
|
|
74
|
+
export function htmlToElements(
|
|
75
|
+
html: string,
|
|
76
|
+
options: { onSubscript?: () => void } = {},
|
|
77
|
+
): Element[] {
|
|
78
|
+
const visit = (nodes: readonly INode[]): Element[] => {
|
|
79
|
+
const out: Element[] = []
|
|
80
|
+
for (const node of nodes) {
|
|
81
|
+
if (node.type === SyntaxKind.Text) {
|
|
82
|
+
out.push(h.text(decodeHE(node.value).trim() || ' '))
|
|
83
|
+
continue
|
|
84
|
+
}
|
|
85
|
+
const body = node.body ?? []
|
|
86
|
+
switch (node.name) {
|
|
87
|
+
case 'a':
|
|
88
|
+
out.push(
|
|
89
|
+
h('a', { href: attribute(node, 'href') || '#' }, ...visit(body)),
|
|
90
|
+
)
|
|
91
|
+
break
|
|
92
|
+
case 'strong':
|
|
93
|
+
case 'b':
|
|
94
|
+
out.push(h('b', ...visit(body)))
|
|
95
|
+
break
|
|
96
|
+
case 'br':
|
|
97
|
+
out.push(h('br'))
|
|
98
|
+
break
|
|
99
|
+
case 'code':
|
|
100
|
+
out.push(h('code', ...visit(body)))
|
|
101
|
+
break
|
|
102
|
+
case 's':
|
|
103
|
+
case 'del':
|
|
104
|
+
out.push(h('s', ...visit(body)))
|
|
105
|
+
break
|
|
106
|
+
case 'i':
|
|
107
|
+
case 'em':
|
|
108
|
+
out.push(h('i', ...visit(body)))
|
|
109
|
+
break
|
|
110
|
+
case 'p':
|
|
111
|
+
out.push(h('p', ...visit(body)))
|
|
112
|
+
break
|
|
113
|
+
case 'sub':
|
|
114
|
+
case 'sup':
|
|
115
|
+
// The vocabulary has neither: the text stays, the position is lost.
|
|
116
|
+
options.onSubscript?.()
|
|
117
|
+
out.push(...visit(body))
|
|
118
|
+
break
|
|
119
|
+
case 'u':
|
|
120
|
+
out.push(h('u', ...visit(body)))
|
|
121
|
+
break
|
|
122
|
+
case 'img': {
|
|
123
|
+
const src = attribute(node, 'src')
|
|
124
|
+
if (src === undefined || !/^(data|https?):/i.test(src)) break
|
|
125
|
+
const alt = attribute(node, 'alt')
|
|
126
|
+
out.push(h('img', { src, ...(alt === undefined ? {} : { alt }) }))
|
|
127
|
+
break
|
|
128
|
+
}
|
|
129
|
+
default:
|
|
130
|
+
if (IGNORED.has(node.name)) break
|
|
131
|
+
out.push(...visit(body))
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return out
|
|
135
|
+
}
|
|
136
|
+
return visit(parse(html, { setAttributeMap: true }))
|
|
137
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
export { MailBot, MailBot as default, type MailModes } from './bot.ts'
|
|
2
|
+
export { addressOf, Config, type Config as MailConfig } from './config.ts'
|
|
3
|
+
export {
|
|
4
|
+
attachmentKind,
|
|
5
|
+
decodeMessage,
|
|
6
|
+
type AttachmentKind,
|
|
7
|
+
type MailDecoder,
|
|
8
|
+
type MailReferrer,
|
|
9
|
+
type MailSource,
|
|
10
|
+
} from './decode.ts'
|
|
11
|
+
export { classifyMailError, type SmtpFailure } from './errors.ts'
|
|
12
|
+
export {
|
|
13
|
+
MediaStaging,
|
|
14
|
+
type StagedMedia,
|
|
15
|
+
type StagedMediaExtra,
|
|
16
|
+
} from './media.ts'
|
|
17
|
+
export { createHtmlMode } from './modes/html.ts'
|
|
18
|
+
export type {
|
|
19
|
+
MailDraft,
|
|
20
|
+
MailModeTypes,
|
|
21
|
+
MailNode,
|
|
22
|
+
MailPayload,
|
|
23
|
+
MailPayloadAttachment,
|
|
24
|
+
} from './modes/shared.ts'
|
|
25
|
+
export { sendPayload, type MailSendDeps } from './send.ts'
|
|
26
|
+
export {
|
|
27
|
+
ImapTransport,
|
|
28
|
+
type ImapError,
|
|
29
|
+
type ImapTransportOptions,
|
|
30
|
+
type MailLogger,
|
|
31
|
+
} from './transport/imap.ts'
|
|
32
|
+
export { SMTP, type Attachment, type SendOptions } from './mail.ts'
|
package/src/mail.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { createTransport, Transporter } from 'nodemailer'
|
|
2
|
+
import type { Config } from './config.ts'
|
|
3
|
+
|
|
4
|
+
export interface Attachment {
|
|
5
|
+
filename?: string
|
|
6
|
+
content: Buffer
|
|
7
|
+
contentType: string
|
|
8
|
+
cid?: string
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface SendOptions {
|
|
12
|
+
to: string
|
|
13
|
+
html: string
|
|
14
|
+
text?: string
|
|
15
|
+
attachments?: Attachment[]
|
|
16
|
+
subject?: string
|
|
17
|
+
inReplyTo?: string
|
|
18
|
+
references?: string[]
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export class SMTP {
|
|
22
|
+
transporter: Transporter
|
|
23
|
+
from: string
|
|
24
|
+
|
|
25
|
+
constructor(config: Config) {
|
|
26
|
+
this.transporter = createTransport({
|
|
27
|
+
host: config.smtp.host,
|
|
28
|
+
port: config.smtp.port,
|
|
29
|
+
secure: config.smtp.tls,
|
|
30
|
+
auth: {
|
|
31
|
+
user: config.username,
|
|
32
|
+
pass: config.password,
|
|
33
|
+
},
|
|
34
|
+
})
|
|
35
|
+
const address = config.selfId || config.username
|
|
36
|
+
this.from = config.name ? `${config.name} <${address}>` : address
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async send(options: SendOptions): Promise<string> {
|
|
40
|
+
const info = await this.transporter.sendMail({
|
|
41
|
+
...options,
|
|
42
|
+
from: this.from,
|
|
43
|
+
})
|
|
44
|
+
return info.messageId
|
|
45
|
+
}
|
|
46
|
+
}
|
package/src/media.ts
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import type { ResolvedResource } from '@deepseek-ai/cordis-plugin-im-core'
|
|
2
|
+
import { randomUUID } from 'node:crypto'
|
|
3
|
+
|
|
4
|
+
/** Bytes staged for one outbound attachment. */
|
|
5
|
+
export interface StagedMedia {
|
|
6
|
+
data: Uint8Array
|
|
7
|
+
/** Name the source suggested, if any; the payload decides the final file name. */
|
|
8
|
+
name: string
|
|
9
|
+
mime?: string | undefined
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** What a staged resource carries beyond its key. */
|
|
13
|
+
export interface StagedMediaExtra {
|
|
14
|
+
name?: string | undefined
|
|
15
|
+
mime?: string | undefined
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
interface Entry {
|
|
19
|
+
media: StagedMedia
|
|
20
|
+
bytes: number
|
|
21
|
+
expiresAt: number
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Bounded in-memory staging for outbound attachments.
|
|
26
|
+
*
|
|
27
|
+
* SMTP has no upload step: the bytes must ride the MIME body of the message that references them, and `serialize` is
|
|
28
|
+
* pure, so `upload` fetches and stages the bytes and `sendPayload` reads them back by key. Entries expire and the
|
|
29
|
+
* whole store is bounded. A key is the Content-ID of its attachment (and the `cid:` target of the HTML part), which
|
|
30
|
+
* makes it safe inside a `Message-ID`-style bracket.
|
|
31
|
+
*
|
|
32
|
+
* A key is single-use: the resolved resource carries `expiresAt: 0`, so the pipeline cache never reuses a key whose
|
|
33
|
+
* bytes a successful send released.
|
|
34
|
+
*/
|
|
35
|
+
export class MediaStaging {
|
|
36
|
+
private readonly entries = new Map<string, Entry>()
|
|
37
|
+
private bytes = 0
|
|
38
|
+
|
|
39
|
+
constructor(
|
|
40
|
+
private readonly ttlMs: number,
|
|
41
|
+
private readonly maxBytes: number,
|
|
42
|
+
) {}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Stage bytes under a fresh Content-ID.
|
|
46
|
+
* @param media - the bytes, their suggested name and MIME type.
|
|
47
|
+
* @returns the resource key `prepare` hands to `serialize`; it is also the `cid`.
|
|
48
|
+
*/
|
|
49
|
+
stage(media: StagedMedia): ResolvedResource {
|
|
50
|
+
const now = Date.now()
|
|
51
|
+
this.prune(now)
|
|
52
|
+
const key = randomUUID().replaceAll('-', '')
|
|
53
|
+
const bytes = media.data.byteLength
|
|
54
|
+
this.entries.set(key, { media, bytes, expiresAt: now + this.ttlMs })
|
|
55
|
+
this.bytes += bytes
|
|
56
|
+
this.evict()
|
|
57
|
+
const extra: StagedMediaExtra = { name: media.name, mime: media.mime }
|
|
58
|
+
return { key, expiresAt: 0, extra }
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Read staged bytes without releasing them.
|
|
63
|
+
* @param key - a key returned by {@link stage}.
|
|
64
|
+
* @returns the staged media, or undefined once expired, evicted or released.
|
|
65
|
+
*/
|
|
66
|
+
get(key: string): StagedMedia | undefined {
|
|
67
|
+
this.prune(Date.now())
|
|
68
|
+
return this.entries.get(key)?.media
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Read staged bytes and release them (a published message needs them no more).
|
|
73
|
+
* @param key - a key returned by {@link stage}.
|
|
74
|
+
* @returns the staged media, or undefined once expired, evicted or released.
|
|
75
|
+
*/
|
|
76
|
+
take(key: string): StagedMedia | undefined {
|
|
77
|
+
this.prune(Date.now())
|
|
78
|
+
const entry = this.entries.get(key)
|
|
79
|
+
if (entry === undefined) return undefined
|
|
80
|
+
this.entries.delete(key)
|
|
81
|
+
this.bytes -= entry.bytes
|
|
82
|
+
return entry.media
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
private prune(now: number): void {
|
|
86
|
+
for (const [key, entry] of this.entries) {
|
|
87
|
+
if (entry.expiresAt > now) continue
|
|
88
|
+
this.entries.delete(key)
|
|
89
|
+
this.bytes -= entry.bytes
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
private evict(): void {
|
|
94
|
+
while (this.bytes > this.maxBytes) {
|
|
95
|
+
const oldest = this.entries.keys().next()
|
|
96
|
+
if (oldest.done === true) return
|
|
97
|
+
const entry = this.entries.get(oldest.value)
|
|
98
|
+
this.entries.delete(oldest.value)
|
|
99
|
+
if (entry !== undefined) this.bytes -= entry.bytes
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `html` mode: the only mode, a single MIME message per logical message.
|
|
3
|
+
*
|
|
4
|
+
* Native tags are the inline subset a mail body can express (`b`/`i`/`u`/`s`/`code`/`pre`/`a`/`p`/`blockquote`/
|
|
5
|
+
* lists and media). Everything else takes the core fallback and is reported as degraded. `assemble` always returns
|
|
6
|
+
* one draft — mail has no message splitting — and `serialize` writes the `<pre>`-wrapped HTML, the plain-text
|
|
7
|
+
* alternative and the attachments.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { ElementRegistry } from '@deepseek-ai/cordis-plugin-im-core'
|
|
11
|
+
import type { Mode, RenderContext } from '@deepseek-ai/cordis-plugin-im-core'
|
|
12
|
+
import type { Element } from '@deepseek-ai/satori-element'
|
|
13
|
+
import {
|
|
14
|
+
assembleDraft,
|
|
15
|
+
escapeAttribute,
|
|
16
|
+
escapeHtml,
|
|
17
|
+
inlineNode,
|
|
18
|
+
inlineRun,
|
|
19
|
+
inlineWrapper,
|
|
20
|
+
mediaNode,
|
|
21
|
+
serializeDraft,
|
|
22
|
+
} from './shared.ts'
|
|
23
|
+
import type { MailModeTypes, MailNode } from './shared.ts'
|
|
24
|
+
|
|
25
|
+
/** An address a `mailto:` link can carry. */
|
|
26
|
+
const MAIL_ADDRESS = /^[^\s@<>]+@[^\s@<>]+$/
|
|
27
|
+
|
|
28
|
+
/** Wrap the inline text of `children` in one tag, letting media of the subtree follow it. */
|
|
29
|
+
function wrap(
|
|
30
|
+
tag: string,
|
|
31
|
+
ctx: RenderContext<MailModeTypes>,
|
|
32
|
+
children: readonly Element[],
|
|
33
|
+
textSuffix = '',
|
|
34
|
+
): MailNode[] {
|
|
35
|
+
return inlineWrapper(tag, ctx.render(children), textSuffix)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Build the `html` mode. */
|
|
39
|
+
export function createHtmlMode(): Mode<MailModeTypes> {
|
|
40
|
+
const elements = new ElementRegistry<MailModeTypes>()
|
|
41
|
+
.register('text', (el) =>
|
|
42
|
+
inlineNode(escapeHtml(el.attrs.content), el.attrs.content),
|
|
43
|
+
)
|
|
44
|
+
.register('br', () => inlineNode('<br>', '\n'))
|
|
45
|
+
.register('p', (el, ctx) => wrap('p', ctx, el.children, '\n\n'))
|
|
46
|
+
.register('b', (el, ctx) => wrap('b', ctx, el.children))
|
|
47
|
+
.register('i', (el, ctx) => wrap('i', ctx, el.children))
|
|
48
|
+
.register('u', (el, ctx) => wrap('u', ctx, el.children))
|
|
49
|
+
.register('s', (el, ctx) => wrap('s', ctx, el.children))
|
|
50
|
+
.register('code', (el, ctx) => {
|
|
51
|
+
const content = ctx.text(el.children)
|
|
52
|
+
return inlineNode(`<code>${escapeHtml(content)}</code>`, content)
|
|
53
|
+
})
|
|
54
|
+
.register('pre', (el, ctx) => {
|
|
55
|
+
const content = ctx.text(el.children)
|
|
56
|
+
return inlineNode(`<pre>${escapeHtml(content)}</pre>`, `${content}\n\n`)
|
|
57
|
+
})
|
|
58
|
+
.register('blockquote', (el, ctx) =>
|
|
59
|
+
wrap('blockquote', ctx, el.children, '\n\n'),
|
|
60
|
+
)
|
|
61
|
+
.register('ul', (el, ctx) => wrap('ul', ctx, el.children))
|
|
62
|
+
.register('ol', (el, ctx) => wrap('ol', ctx, el.children))
|
|
63
|
+
.register('li', (el, ctx) => wrap('li', ctx, el.children, '\n'))
|
|
64
|
+
.register('a', (el, ctx) => {
|
|
65
|
+
const { html, text, rest } = inlineRun(ctx.render(el.children))
|
|
66
|
+
const href = el.attrs.href
|
|
67
|
+
const label = html === '' ? escapeHtml(href) : html
|
|
68
|
+
const plain = text === '' ? href : text
|
|
69
|
+
const plainText = plain === href ? href : `${plain} (${href})`
|
|
70
|
+
return [
|
|
71
|
+
inlineNode(
|
|
72
|
+
`<a href="${escapeAttribute(href)}">${label}</a>`,
|
|
73
|
+
plainText,
|
|
74
|
+
),
|
|
75
|
+
...rest,
|
|
76
|
+
]
|
|
77
|
+
})
|
|
78
|
+
.register(
|
|
79
|
+
'at',
|
|
80
|
+
(el) => {
|
|
81
|
+
const id = el.attrs.id
|
|
82
|
+
const label = el.attrs.name ?? id ?? el.attrs.type
|
|
83
|
+
if (id !== undefined && MAIL_ADDRESS.test(id)) {
|
|
84
|
+
return inlineNode(
|
|
85
|
+
`<a href="mailto:${escapeAttribute(id)}">@${escapeHtml(id)}</a>`,
|
|
86
|
+
`@${id}`,
|
|
87
|
+
)
|
|
88
|
+
}
|
|
89
|
+
return inlineNode(escapeHtml(`@${label}`), `@${label}`)
|
|
90
|
+
},
|
|
91
|
+
{ variants: ['user'] },
|
|
92
|
+
)
|
|
93
|
+
.register('img', (el, ctx) => mediaNode(el, ctx), { variants: ['inline'] })
|
|
94
|
+
.register('audio', (el, ctx) => mediaNode(el, ctx))
|
|
95
|
+
.register('video', (el, ctx) => mediaNode(el, ctx))
|
|
96
|
+
.register('file', (el, ctx) => mediaNode(el, ctx))
|
|
97
|
+
|
|
98
|
+
return {
|
|
99
|
+
id: 'html',
|
|
100
|
+
elements,
|
|
101
|
+
limits: {},
|
|
102
|
+
assemble: (nodes, envelope) => assembleDraft(nodes, envelope.quote),
|
|
103
|
+
serialize: (prepared, ctx) => serializeDraft(prepared, ctx),
|
|
104
|
+
}
|
|
105
|
+
}
|