fix: rpc headers covert & typo (#559)

Co-authored-by: liuzhicong <liuzhicong@dhgate.com>
This commit is contained in:
刘志聪
2025-01-16 00:20:02 +08:00
committed by GitHub
parent 2bb033964c
commit 3870727a08
4 changed files with 19 additions and 13 deletions

View File

@@ -292,13 +292,14 @@ export const commonParseMail = async (parsedEmailContext: ParsedEmailContext): P
try {
const { default: PostalMime } = await import('postal-mime');
const parsedEmail = await PostalMime.parse(raw_mail);
return {
parsedEmailContext.parsedEmail = {
sender: parsedEmail.from ? `${parsedEmail.from.name} <${parsedEmail.from.address}>` : "",
subject: parsedEmail.subject || "",
text: parsedEmail.text || "",
html: parsedEmail.html || "",
headers: parsedEmail.headers || [],
};
return parsedEmailContext.parsedEmail;
}
catch (e) {
console.error("Failed use PostalMime to parse email", e);
@@ -451,9 +452,17 @@ export async function triggerAnotherWorker(
console.log(`worker.binding = ${bindingName} not match keywords, parsedText = ${parsedText}`);
continue;
}
try {
const requestBody = JSON.stringify(rpcEmailMessage);
const bodyObj = { ...rpcEmailMessage } as any;
if (bodyObj.headers && typeof bodyObj.headers.forEach === "function") {
const headerObj: any = {}
bodyObj.headers.forEach((value: string, key: string) => {
headerObj[key] = value;
});
bodyObj.headers = headerObj
}
const requestBody = JSON.stringify(bodyObj);
console.log(`exec worker , binding = ${bindingName} , requestBody = ${requestBody}`);
await method(requestBody);
} catch (e1) {
console.error(`execute method = ${methodName} error`, e1);

View File

@@ -80,16 +80,13 @@ async function email(message: ForwardableEmailMessage, env: Bindings, ctx: Execu
// trigger another worker
try {
const headersMap = new Map<string, string>();
if (message.headers) {
message.headers.forEach((value, key) => { headersMap.set(key, value); });
}
const parsedText = (await commonParseMail(parsedEmailContext))?.text ?? ""
const parsedEmail = (await commonParseMail(parsedEmailContext));
const parsedText = parsedEmail?.text ?? ""
const rpcEmail: RPCEmailMessage = {
from: message.from,
to: message.to,
rawEmail: rawEmail,
headers: headersMap
headers: message.headers
}
await triggerAnotherWorker({ env: env } as Context<HonoCustomType>, rpcEmail, parsedText);
} catch (error) {

View File

@@ -338,8 +338,8 @@ export async function sendMailToTelegram(
return;
}
const settings = await c.env.KV.get<TelegramSettings>(CONSTANTS.TG_KV_SETTINGS_KEY, "json");
const golbalPush = settings?.enableGlobalMailPush && settings?.globalMailPushList;
if (!userId && !golbalPush) {
const globalPush = settings?.enableGlobalMailPush && settings?.globalMailPushList;
if (!userId && !globalPush) {
return;
}
const mailId = await c.env.DB.prepare(
@@ -353,7 +353,7 @@ export async function sendMailToTelegram(
url.searchParams.set("mail_id", mailId);
miniAppButtons.push(Markup.button.webApp("查看邮件", url.toString()));
}
if (golbalPush) {
if (globalPush) {
for (const pushId of settings.globalMailPushList) {
await bot.telegram.sendMessage(pushId, mail, {
...Markup.inlineKeyboard([

View File

@@ -108,7 +108,7 @@ type RPCEmailMessage = {
from: string | undefined | null,
to: string | undefined | null,
rawEmail: string | undefined | null,
headers: Map<string, string>,
headers: object | undefined | null,
}
type ParsedEmailContext = {