@@ -37,6 +37,12 @@ export type ServerConfig = {
3737 * @default DEFAULT_PORT
3838 */
3939 port ?: number ;
40+
41+ /**
42+ * Whether or prevent `smtpsaurus` from logging info to the console.
43+ * @default false
44+ */
45+ quiet ?: boolean ;
4046} ;
4147
4248/**
@@ -60,8 +66,12 @@ export type ServerConfig = {
6066 * // Setting `findPortOnConflict` to `true` may be useful when running
6167 * // tests in parallel, as it allows `smtpsaurus` to find an open
6268 * // automatically if the one specified is in use.
63- * server = new SmtpServer({ port: 42024, findPortOnConflict: true });
64- * });
69+ * server = new SmtpServer({
70+ * port: 42024,
71+ * findPortOnConflict: true,
72+ * quiet: true,
73+ * });
74+ * });
6575 *
6676 * afterEach(async () => {
6777 * await server.stop();
@@ -98,26 +108,28 @@ export type ServerConfig = {
98108 * transporter.close();
99109 * });
100110 * });
101- ```
102- *
111+ * ```
103112 */
104113export class SmtpServer {
105114 /**
106115 * Domain name associated with the server; for example, smtpsaurus.email.
107116 * This is the name that the server uses to identify itself, such as in
108117 * the initial greeting, to the sender during an SMTP transaction
118+ * @default DEFAULT_DOMAIN;
109119 */
110120 domain : string ;
111121
112122 /**
113123 * Hostname where the server is running; for example 127.0.0.1.
124+ * @default DEFAULT_HOSTNAME;
114125 */
115126 hostname : string ;
116127
117128 /**
118129 * Port number the server is listening on.
130+ * @default DEFAULT_PORT;
119131 */
120- port : number ;
132+ port : number = DEFAULT_PORT ;
121133
122134 /**
123135 * Provides access to mailbox operations, such as retrieving emails.
@@ -151,6 +163,24 @@ export class SmtpServer {
151163 */
152164 private mainLoopExitSignal : Promise < void > ;
153165
166+ /**
167+ * @private
168+ * Whether or not `smtpsaurus` should log info to the console.
169+ * @default false
170+ */
171+ private quiet : boolean = false ;
172+
173+ /**
174+ * @private
175+ * A wrapper around `console.log` that only logs to the console when
176+ * `smtpsaurus` is not running under quiet mode.
177+ */
178+ private log = ( ...data : Parameters < typeof console . log > ) => {
179+ if ( this . quiet ) return ;
180+
181+ console . log ( ...data ) ;
182+ } ;
183+
154184 /**
155185 * Constructor for the SmtpServer class. Initializes server configuration
156186 * and starts listening for incoming connections.
@@ -161,6 +191,7 @@ export class SmtpServer {
161191 this . domain = config ?. domain ?? DEFAULT_DOMAIN ;
162192 this . port = config ?. port ?? DEFAULT_PORT ;
163193 this . hostname = DEFAULT_HOSTNAME ;
194+ this . quiet = ! ! config ?. quiet ;
164195
165196 if ( ! config ?. findPortOnConflict ) {
166197 this . listener = Deno . listen ( {
@@ -192,7 +223,7 @@ export class SmtpServer {
192223 ) ;
193224 }
194225
195- console . log (
226+ this . log (
196227 `🦕 smtpsaurus listening at ${ this . listener . addr . hostname } on port ${ this . port } .` ,
197228 ) ;
198229
@@ -212,7 +243,7 @@ export class SmtpServer {
212243 const clientHostname = connection . remoteAddr . hostname ;
213244 const clientPort = connection . remoteAddr . port ;
214245
215- console . log ( `🛜 New connection from ${ clientHostname } :${ clientPort } .` ) ;
246+ this . log ( `🛜 New connection from ${ clientHostname } :${ clientPort } .` ) ;
216247
217248 // Initial greeting
218249 await writeMessage (
0 commit comments