-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathyyds-mail-utils.js
More file actions
276 lines (250 loc) · 9.06 KB
/
Copy pathyyds-mail-utils.js
File metadata and controls
276 lines (250 loc) · 9.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
(function yydsMailUtilsModule(root, factory) {
if (typeof module !== 'undefined' && module.exports) {
module.exports = factory();
return;
}
root.YydsMailUtils = factory();
})(typeof self !== 'undefined' ? self : globalThis, function createYydsMailUtils() {
const DEFAULT_YYDS_MAIL_BASE_URL = 'https://maliapi.215.im/v1';
const YYDS_MAIL_PROVIDER = 'yyds-mail';
function firstNonEmptyString(values) {
for (const value of values) {
if (value === undefined || value === null) continue;
const normalized = String(value).trim();
if (normalized) return normalized;
}
return '';
}
function normalizeYydsMailBaseUrl(rawValue = '') {
const value = String(rawValue || '').trim();
if (!value) return DEFAULT_YYDS_MAIL_BASE_URL;
const candidate = /^[a-zA-Z][a-zA-Z\d+\-.]*:\/\//.test(value)
? value
: `https://${value}`;
try {
const parsed = new URL(candidate);
if (!['http:', 'https:'].includes(parsed.protocol)) {
return DEFAULT_YYDS_MAIL_BASE_URL;
}
parsed.hash = '';
parsed.search = '';
const pathname = parsed.pathname === '/' ? '' : parsed.pathname.replace(/\/+$/, '');
return `${parsed.origin}${pathname}` || DEFAULT_YYDS_MAIL_BASE_URL;
} catch {
return DEFAULT_YYDS_MAIL_BASE_URL;
}
}
function normalizeYydsMailApiKey(value = '') {
return String(value || '').trim();
}
function normalizeYydsMailAddress(value = '') {
return String(value || '').trim().toLowerCase();
}
function buildYydsMailHeaders(config = {}, options = {}) {
const headers = {};
const apiKey = firstNonEmptyString([
options.apiKey,
options.includeConfigApiKey === false ? '' : config.apiKey,
options.includeConfigApiKey === false ? '' : config.yydsMailApiKey,
]);
const tempToken = firstNonEmptyString([
options.tempToken,
config.tempToken,
config.token,
]);
if (apiKey) {
headers['X-API-Key'] = apiKey;
}
if (tempToken) {
headers.Authorization = /^bearer\s+/i.test(tempToken)
? tempToken
: `Bearer ${tempToken}`;
}
if (options.json) {
headers['Content-Type'] = 'application/json';
}
if (options.acceptJson !== false) {
headers.Accept = 'application/json';
}
return headers;
}
function joinYydsMailUrl(baseUrl, path, params = {}) {
const normalizedBase = normalizeYydsMailBaseUrl(baseUrl);
const normalizedPath = String(path || '').trim();
const url = new URL(`${normalizedBase}${normalizedPath.startsWith('/') ? '' : '/'}${normalizedPath}`);
for (const [key, value] of Object.entries(params || {})) {
if (value === undefined || value === null || value === '') continue;
url.searchParams.set(key, String(value));
}
return url.toString();
}
function normalizeYydsMailInbox(payload = {}) {
const safePayload = payload && typeof payload === 'object' ? payload : {};
return {
id: firstNonEmptyString([safePayload.id, safePayload.inbox_id, safePayload.inboxId]),
address: normalizeYydsMailAddress(firstNonEmptyString([safePayload.address, safePayload.email])),
token: firstNonEmptyString([safePayload.token, safePayload.tempToken, safePayload.accessToken]),
expiresAt: firstNonEmptyString([safePayload.expiresAt, safePayload.expires_at]) || null,
isActive: safePayload.isActive !== undefined ? Boolean(safePayload.isActive) : true,
createdAt: firstNonEmptyString([safePayload.createdAt, safePayload.created_at]) || null,
raw: safePayload,
};
}
function normalizeYydsMailCurrentInbox(value = null) {
if (!value || typeof value !== 'object') return null;
const inbox = normalizeYydsMailInbox(value);
return inbox.address && inbox.token ? inbox : null;
}
function getYydsMailRows(payload) {
if (Array.isArray(payload)) return payload;
if (!payload || typeof payload !== 'object') return [];
const candidates = [
payload.messages,
payload.items,
payload.list,
payload.records,
payload.data?.messages,
payload.data?.items,
payload.data?.list,
payload.data?.records,
];
for (const candidate of candidates) {
if (Array.isArray(candidate)) {
return candidate;
}
}
return [];
}
function stripHtmlTags(value = '') {
return String(value || '')
.replace(/<style[\s\S]*?<\/style>/gi, ' ')
.replace(/<script[\s\S]*?<\/script>/gi, ' ')
.replace(/<[^>]+>/g, ' ')
.replace(/ /gi, ' ')
.replace(/&/gi, '&')
.replace(/</gi, '<')
.replace(/>/gi, '>')
.replace(/\s+/g, ' ')
.trim();
}
function normalizeYydsMailSender(value = {}) {
if (typeof value === 'string') {
return { name: '', address: value };
}
const safeValue = value && typeof value === 'object' ? value : {};
return {
name: firstNonEmptyString([safeValue.name]),
address: firstNonEmptyString([safeValue.address, safeValue.email]),
};
}
function normalizeYydsMailRecipients(value) {
const list = Array.isArray(value) ? value : (value ? [value] : []);
return list.map((item) => {
if (typeof item === 'string') return { name: '', address: normalizeYydsMailAddress(item) };
const safeItem = item && typeof item === 'object' ? item : {};
return {
name: firstNonEmptyString([safeItem.name]),
address: normalizeYydsMailAddress(firstNonEmptyString([safeItem.address, safeItem.email])),
};
}).filter((item) => item.address);
}
function normalizeYydsMailCreatedAt(value = '') {
const source = firstNonEmptyString([value]);
if (!source) return '';
const parsed = Date.parse(source);
return Number.isFinite(parsed) ? new Date(parsed).toISOString() : source;
}
function normalizeYydsMailMessage(row = {}) {
if (!row || typeof row !== 'object') return null;
const from = normalizeYydsMailSender(row.from || row.sender);
const to = normalizeYydsMailRecipients(row.to || row.recipients || row.recipient);
const htmlValue = Array.isArray(row.html)
? row.html.join('\n')
: firstNonEmptyString([row.html, row.bodyHtml, row.body_html]);
const textValue = firstNonEmptyString([row.text, row.bodyText, row.body_text, row.bodyPreview, row.preview]);
const raw = firstNonEmptyString([row.raw, row.source]);
const bodyPreview = (textValue || stripHtmlTags(htmlValue) || stripHtmlTags(raw))
.replace(/\s+/g, ' ')
.trim();
return {
id: firstNonEmptyString([row.id, row.message_id, row.messageId]),
address: normalizeYydsMailAddress(firstNonEmptyString([
row.address,
row.mailbox,
to[0]?.address,
])),
inboxId: firstNonEmptyString([row.inboxId, row.inbox_id]),
subject: firstNonEmptyString([row.subject, row.title]),
from: {
name: from.name,
emailAddress: {
address: from.address,
},
},
to,
seen: Boolean(row.seen),
bodyPreview,
raw: raw || htmlValue || textValue || '',
text: textValue,
html: htmlValue,
receivedDateTime: normalizeYydsMailCreatedAt(firstNonEmptyString([
row.createdAt,
row.created_at,
row.receivedDateTime,
row.date,
])),
};
}
function normalizeYydsMailMessages(payload) {
return getYydsMailRows(payload)
.map((row) => normalizeYydsMailMessage(row))
.filter(Boolean);
}
function extractYydsMailVerificationCode(text) {
const source = String(text || '');
const matchCn = source.match(/(?:代码为|验证码[^0-9]*?)[\s::]*(\d{6})/i);
if (matchCn) return matchCn[1];
const matchOpenAiLogin = source.match(/(?:chatgpt\s+log-?in\s+code|enter\s+this\s+code)[^0-9]{0,24}(\d{6})/i);
if (matchOpenAiLogin) return matchOpenAiLogin[1];
const matchChatGPT = source.match(/your\s+chatgpt\s+code\s+is\s+(\d{6})/i);
if (matchChatGPT) return matchChatGPT[1];
const matchEn = source.match(/code(?:\s+is|[\s:])+(\d{6})/i);
if (matchEn) return matchEn[1];
const matchStandalone = source.match(/\b(\d{6})\b/);
return matchStandalone ? matchStandalone[1] : null;
}
function normalizeYydsMailMessageDetail(payload = {}) {
const message = normalizeYydsMailMessage(payload);
if (!message) return null;
const combined = [
message.subject,
message.from?.emailAddress?.address,
message.bodyPreview,
message.text,
message.html,
message.raw,
].filter(Boolean).join(' ');
return {
...message,
bodyPreview: message.bodyPreview || stripHtmlTags(combined),
verification_code: extractYydsMailVerificationCode(combined) || '',
};
}
return {
DEFAULT_YYDS_MAIL_BASE_URL,
YYDS_MAIL_PROVIDER,
buildYydsMailHeaders,
extractYydsMailVerificationCode,
firstNonEmptyString,
joinYydsMailUrl,
normalizeYydsMailAddress,
normalizeYydsMailApiKey,
normalizeYydsMailBaseUrl,
normalizeYydsMailCurrentInbox,
normalizeYydsMailInbox,
normalizeYydsMailMessage,
normalizeYydsMailMessageDetail,
normalizeYydsMailMessages,
stripHtmlTags,
};
});