11import { WorkerEntrypoint } from "cloudflare:workers" ;
22import { blue } from "kleur/colors" ;
3- import { LogLevel , SharedHeaders } from "miniflare:shared" ;
43import PostalMime from "postal-mime" ;
5- import { CoreBindings } from "../core/constants" ;
64import { RAW_EMAIL } from "./constants" ;
75import { type MiniflareEmailMessage as EmailMessage } from "./email.worker" ;
86import type { EmailAddress , MessageBuilder } from "./types" ;
@@ -66,31 +64,33 @@ function formatMessageBuilder(builder: MessageBuilder): string {
6664 return lines . join ( "\n" ) ;
6765}
6866
67+ /**
68+ * Appends path segments to a base path using the separator already implied by
69+ * the base path string. This trims trailing `/` and `\` from the base before
70+ * joining, but does not otherwise normalize the full path.
71+ */
72+ function joinPath ( base : string , ...segments : string [ ] ) : string {
73+ const separator = base . includes ( "\\" ) ? "\\" : "/" ;
74+ return [ base . replace ( / [ \\ / ] + $ / , "" ) , ...segments ] . join ( separator ) ;
75+ }
76+
6977interface SendEmailEnv {
70- [ CoreBindings . SERVICE_LOOPBACK ] : Fetcher ;
78+ MINIFLARE_EMAIL_DISK : Fetcher ;
79+ email_directory : string ;
7180 destination_address : string | undefined ;
7281 allowed_destination_addresses : string [ ] | undefined ;
7382 allowed_sender_addresses : string [ ] | undefined ;
7483}
7584
7685export class SendEmailBinding extends WorkerEntrypoint < SendEmailEnv > {
7786 /**
78- * Logs a message via the loopback service
87+ * Logs a message via the runtime console.
7988 */
80- private log ( message : string , level : LogLevel = LogLevel . INFO ) : void {
81- this . ctx . waitUntil (
82- this . env [ CoreBindings . SERVICE_LOOPBACK ] . fetch (
83- "http://localhost/core/log" ,
84- {
85- method : "POST" ,
86- headers : { [ SharedHeaders . LOG_LEVEL ] : level . toString ( ) } ,
87- body : message ,
88- }
89- )
90- ) ;
89+ private log ( message : string ) : void {
90+ console . log ( message ) ;
9191 }
9292 /**
93- * Stores content to a temporary file via the loopback service
93+ * Stores content to a temporary file via the disk service.
9494 */
9595 private async storeTempFile (
9696 content : string | ArrayBuffer | ArrayBufferView ,
@@ -111,14 +111,14 @@ export class SendEmailBinding extends WorkerEntrypoint<SendEmailEnv> {
111111 ) ;
112112 }
113113
114- const resp = await this . env [ CoreBindings . SERVICE_LOOPBACK ] . fetch (
115- `http://localhost/core/store-temp-file?extension= ${ extension } & prefix= ${ prefix } `,
116- {
117- method : "POST " ,
118- body,
119- }
120- ) ;
121- return await resp . text ( ) ;
114+ const fileName = ` ${ crypto . randomUUID ( ) } . ${ extension } ` ;
115+ const url = new URL ( ` ${ prefix } / ${ fileName } `, "http://placeholder/" ) ;
116+ await this . env . MINIFLARE_EMAIL_DISK . fetch ( url , {
117+ method : "PUT " ,
118+ body,
119+ } ) ;
120+
121+ return joinPath ( this . env . email_directory , prefix , fileName ) ;
122122 }
123123
124124 private checkDestinationAllowed ( to : string ) {
0 commit comments