Skip to content

Commit 1943e4a

Browse files
authored
Merge pull request #3 from honmanyau/add-quiet-mode
Add a quiet mode
2 parents ce4093d + d2c795f commit 1943e4a

4 files changed

Lines changed: 49 additions & 9 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## v0.1.3
4+
5+
- Add the ability to run `smtpsaurus` under quiet mode, which stops `smtpsaurus`
6+
from logging to STDOUT.
7+
38
## v0.1.2
49

510
- Implement the ability for `smtpsaurus` to optionally find an open port to

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,11 @@ describe("SmtpServer", () => {
8181
// Setting `findPortOnConflict` to `true` may be useful when running
8282
// tests in parallel, as it allows `smtpsaurus` to find an open
8383
// automatically if the one specified is in use.
84-
server = new SmtpServer({ port: 42024, findPortOnConflict: true });
84+
server = new SmtpServer({
85+
port: 42024,
86+
findPortOnConflict: true
87+
quiet: true
88+
});
8589
});
8690

8791
afterEach(async () => {

deno.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"lock": true,
33
"name": "@smtpsaurus/smtpsaurus",
4-
"version": "0.1.2",
4+
"version": "0.1.3",
55
"license": "MIT",
66
"exports": "./src/mod.ts",
77
"publish": {

src/smtpsaurus.ts

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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
*/
104113
export 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

Comments
 (0)