diff options
| author | Yuqian Yang <crupest@crupest.life> | 2025-06-14 01:21:11 +0800 | 
|---|---|---|
| committer | Yuqian Yang <crupest@crupest.life> | 2025-06-14 02:35:01 +0800 | 
| commit | 8fe85fccf3881114202301ac986073564d5abd3f (patch) | |
| tree | fc58511731fd77296e3f1df54478bb0208e5fcc5 /deno/mail-relay/aws | |
| parent | 42526fed6f86a3a3a352313078668fcc8b473a3b (diff) | |
| download | crupest-8fe85fccf3881114202301ac986073564d5abd3f.tar.gz crupest-8fe85fccf3881114202301ac986073564d5abd3f.tar.bz2 crupest-8fe85fccf3881114202301ac986073564d5abd3f.zip  | |
deno(mail-server): drop custom logger, use builtin console.
Diffstat (limited to 'deno/mail-relay/aws')
| -rw-r--r-- | deno/mail-relay/aws/app.ts | 27 | ||||
| -rw-r--r-- | deno/mail-relay/aws/deliver.ts | 15 | ||||
| -rw-r--r-- | deno/mail-relay/aws/fetch.ts | 35 | ||||
| -rw-r--r-- | deno/mail-relay/aws/mail.ts | 22 | 
4 files changed, 42 insertions, 57 deletions
diff --git a/deno/mail-relay/aws/app.ts b/deno/mail-relay/aws/app.ts index 05d93cd..b7d0154 100644 --- a/deno/mail-relay/aws/app.ts +++ b/deno/mail-relay/aws/app.ts @@ -94,11 +94,10 @@ function createAwsOptions({  }  function createOutbound( -  logger: Logger,    awsOptions: ReturnType<typeof createAwsOptions>,    db: DbService,  ) { -  const deliverer = new AwsMailDeliverer(logger, awsOptions); +  const deliverer = new AwsMailDeliverer(awsOptions);    deliverer.preHooks.push(      new AwsMailMessageIdRewriteHook(db.messageIdToAws.bind(db)),    ); @@ -168,11 +167,7 @@ function createAwsFetchOnlyServices() {      password: config.get("awsPassword"),      region: config.get("awsRegion"),    }); -  const fetcher = new AwsMailFetcher( -    logger, -    awsOptions, -    config.get("awsMailBucket"), -  ); +  const fetcher = new AwsMailFetcher(awsOptions, config.get("awsMailBucket"));    return { config, logger, awsOptions, fetcher };  } @@ -195,7 +190,7 @@ function createAwsServices() {    const { config, logger, inbound, awsOptions, fetcher, recycler } =      createAwsRecycleOnlyServices();    const dbService = new DbService(join(config.get("dataPath"), "db.sqlite")); -  const outbound = createOutbound(logger, awsOptions, dbService); +  const outbound = createOutbound(awsOptions, dbService);    return {      config, @@ -211,10 +206,10 @@ function createAwsServices() {  function createServerServices() {    const services = createAwsServices(); -  const { logger, config, outbound, inbound, fetcher } = services; -  const smtp = createSmtp(logger, outbound); +  const { config, outbound, inbound, fetcher } = services; +  const smtp = createSmtp(outbound); -  const hono = createHono(logger, outbound, inbound); +  const hono = createHono(outbound, inbound);    setupAwsHono(hono, {      path: config.get("awsInboundPath"),      auth: config.get("awsInboundKey"), @@ -252,11 +247,11 @@ function serve(cron: boolean = false) {  }  async function listLives() { -  const { logger, fetcher } = createAwsFetchOnlyServices(); +  const { fetcher } = createAwsFetchOnlyServices();    const liveMails = await fetcher.listLiveMails(); -  logger.info(`Total ${liveMails.length}:`); +  console.info(`Total ${liveMails.length}:`);    if (liveMails.length !== 0) { -    logger.info(liveMails.join("\n")); +    console.info(liveMails.join("\n"));    }  } @@ -272,8 +267,8 @@ if (import.meta.main) {        command: "sendmail",        describe: "send mail via this server's endpoint",        handler: async (_argv) => { -        const { logger, config } = createBaseServices(); -        await sendMail(logger, config.getInt("httpPort")); +        const { config } = createBaseServices(); +        await sendMail(config.getInt("httpPort"));        },      })      .command({ diff --git a/deno/mail-relay/aws/deliver.ts b/deno/mail-relay/aws/deliver.ts index 117e87a..a002eda 100644 --- a/deno/mail-relay/aws/deliver.ts +++ b/deno/mail-relay/aws/deliver.ts @@ -4,7 +4,6 @@ import {    SESv2ClientConfig,  } from "@aws-sdk/client-sesv2"; -import { Logger } from "@crupest/base/log";  import { Mail, MailDeliverContext, SyncMailDeliverer } from "../mail.ts";  declare module "../mail.ts" { @@ -15,13 +14,11 @@ declare module "../mail.ts" {  export class AwsMailDeliverer extends SyncMailDeliverer {    readonly name = "aws"; -  readonly #logger;    readonly #aws;    readonly #ses; -  constructor(logger: Logger, aws: SESv2ClientConfig) { -    super(logger); -    this.#logger = logger; +  constructor(aws: SESv2ClientConfig) { +    super();      this.#aws = aws;      this.#ses = new SESv2Client(aws);    } @@ -30,7 +27,7 @@ export class AwsMailDeliverer extends SyncMailDeliverer {      mail: Mail,      context: MailDeliverContext,    ): Promise<void> { -    this.#logger.info("Begin to call aws send-email api..."); +    console.info("Begin to call aws send-email api...");      try {        const sendCommand = new SendEmailCommand({ @@ -41,9 +38,11 @@ export class AwsMailDeliverer extends SyncMailDeliverer {        const res = await this.#ses.send(sendCommand);        if (res.MessageId == null) { -        this.#logger.warn("Aws send-email returns no message id."); +        console.warn("Aws send-email returns no message id.");        } else { -        context.result.awsMessageId = `${res.MessageId}@${this.#aws.region}.amazonses.com`; +        context.result.awsMessageId = `${res.MessageId}@${ +          this.#aws.region +        }.amazonses.com`;        }        context.result.recipients.set("*", { diff --git a/deno/mail-relay/aws/fetch.ts b/deno/mail-relay/aws/fetch.ts index ef1ba5f..bbb671a 100644 --- a/deno/mail-relay/aws/fetch.ts +++ b/deno/mail-relay/aws/fetch.ts @@ -8,7 +8,6 @@ import {  } from "@aws-sdk/client-s3";  import { toFileNameString } from "@crupest/base/date"; -import { Logger } from "@crupest/base/log";  import { Mail } from "../mail.ts"; @@ -42,18 +41,16 @@ export type AwsS3MailConsumer = (  export class AwsMailFetcher {    readonly #livePrefix = "mail/live/";    readonly #archivePrefix = "mail/archive/"; -  readonly #logger;    readonly #s3;    readonly #bucket; -  constructor(logger: Logger, aws: S3ClientConfig, bucket: string) { -    this.#logger = logger; +  constructor(aws: S3ClientConfig, bucket: string) {      this.#s3 = new S3Client(aws);      this.#bucket = bucket;    }    async listLiveMails(): Promise<string[]> { -    this.#logger.info("Begin to retrieve live mails."); +    console.info("Begin to retrieve live mails.");      const listCommand = new ListObjectsV2Command({        Bucket: this.#bucket, @@ -62,16 +59,14 @@ export class AwsMailFetcher {      const res = await this.#s3.send(listCommand);      if (res.Contents == null) { -      this.#logger.warn("Listing live mails in S3 returns null Content."); +      console.warn("Listing live mails in S3 returns null Content.");        return [];      }      const result: string[] = [];      for (const object of res.Contents) {        if (object.Key == null) { -        this.#logger.warn( -          "Listing live mails in S3 returns an object with no Key.", -        ); +        console.warn("Listing live mails in S3 returns an object with no Key.");          continue;        } @@ -83,9 +78,9 @@ export class AwsMailFetcher {    }    async consumeS3Mail(s3Key: string, consumer: AwsS3MailConsumer) { -    this.#logger.info(`Begin to consume s3 mail ${s3Key} ...`); +    console.info(`Begin to consume s3 mail ${s3Key} ...`); -    this.#logger.info(`Fetching s3 mail ${s3Key}...`); +    console.info(`Fetching s3 mail ${s3Key}...`);      const mailPath = `${this.#livePrefix}${s3Key}`;      const command = new GetObjectCommand({        Bucket: this.#bucket, @@ -98,14 +93,14 @@ export class AwsMailFetcher {      }      const rawMail = await res.Body.transformToString(); -    this.#logger.info(`Done fetching s3 mail ${s3Key}.`); +    console.info(`Done fetching s3 mail ${s3Key}.`); -    this.#logger.info(`Calling consumer...`); +    console.info(`Calling consumer...`);      await consumer(rawMail, s3Key); -    this.#logger.info(`Done consuming s3 mail ${s3Key}.`); +    console.info(`Done consuming s3 mail ${s3Key}.`);      const date = new Mail(rawMail) -      .startSimpleParse(this.#logger) +      .startSimpleParse()        .sections()        .headers()        .date(); @@ -113,17 +108,17 @@ export class AwsMailFetcher {        date != null ? toFileNameString(date, true) : "invalid-date";      const newPath = `${this.#archivePrefix}${dateString}/${s3Key}`; -    this.#logger.info(`Archiving s3 mail ${s3Key} to ${newPath}...`); +    console.info(`Archiving s3 mail ${s3Key} to ${newPath}...`);      await s3MoveObject(this.#s3, this.#bucket, mailPath, newPath); -    this.#logger.info(`Done archiving s3 mail ${s3Key}.`); +    console.info(`Done archiving s3 mail ${s3Key}.`); -    this.#logger.info(`Done consuming s3 mail ${s3Key}.`); +    console.info(`Done consuming s3 mail ${s3Key}.`);    }    async recycleLiveMails(consumer: AwsS3MailConsumer) { -    this.#logger.info("Begin to recycle live mails..."); +    console.info("Begin to recycle live mails...");      const mails = await this.listLiveMails(); -    this.#logger.info(`Found ${mails.length} live mails`); +    console.info(`Found ${mails.length} live mails`);      for (const s3Key of mails) {        await this.consumeS3Mail(s3Key, consumer);      } diff --git a/deno/mail-relay/aws/mail.ts b/deno/mail-relay/aws/mail.ts index d2cfad1..cc05d23 100644 --- a/deno/mail-relay/aws/mail.ts +++ b/deno/mail-relay/aws/mail.ts @@ -8,17 +8,17 @@ export class AwsMailMessageIdRewriteHook implements MailDeliverHook {    }    async callback(context: MailDeliverContext): Promise<void> { -    context.logger.info("Rewrite message ids..."); +    console.info("Rewrite message ids...");      const addresses = context.mail.simpleFindAllAddresses(); -    context.logger.info(`Addresses found in mail: ${addresses.join(", ")}.`); +    console.info(`Addresses found in mail: ${addresses.join(", ")}.`);      for (const address of addresses) {        const awsMessageId = await this.#lookup(address);        if (awsMessageId != null && awsMessageId.length !== 0) { -        context.logger.info(`Rewrite ${address} to ${awsMessageId}.`); +        console.info(`Rewrite ${address} to ${awsMessageId}.`);          context.mail.raw = context.mail.raw.replaceAll(address, awsMessageId);        }      } -    context.logger.info("Done rewrite message ids."); +    console.info("Done rewrite message ids.");    }  } @@ -30,24 +30,20 @@ export class AwsMailMessageIdSaveHook implements MailDeliverHook {    }    async callback(context: MailDeliverContext): Promise<void> { -    context.logger.info("Save aws message ids..."); +    console.info("Save aws message ids...");      const messageId = context.mail -      .startSimpleParse(context.logger) +      .startSimpleParse()        .sections()        .headers()        .messageId();      if (messageId == null) { -      context.logger.info( -        "Original mail does not have message id. Skip saving.", -      ); +      console.info("Original mail does not have message id. Skip saving.");        return;      }      if (context.result.awsMessageId != null) { -      context.logger.info( -        `Saving ${messageId} => ${context.result.awsMessageId}.`, -      ); +      console.info(`Saving ${messageId} => ${context.result.awsMessageId}.`);        await this.#record(messageId, context.result.awsMessageId);      } -    context.logger.info("Done save message ids."); +    console.info("Done save message ids.");    }  }  | 
