-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1-mixed.js
52 lines (44 loc) · 1.15 KB
/
1-mixed.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
'use strict';
const fs = require('node:fs');
class Message {
constructor(to, subject, text) {
this.to = to;
this.subject = subject;
this.text = text;
this.files = [];
}
async attach(filename) {
console.log('Attach file:', filename);
const readable = fs.createReadStream(filename);
const data = [];
for await (const chunk of readable) data.push(chunk);
const content = Buffer.concat(data);
this.files.push({ filename, content });
}
async send() {
console.log('Send message');
throw new Error('Nullae electronicae epistulae in Roma');
}
async warn(cause) {
const error = new Error('Message can not be sent', { cause });
console.log('Message', error);
}
}
// Usage
const main = async () => {
const subj = 'De virtutem animi';
const text = [
'Lucio Verissimo Fratri,',
'Hodie cogitavi de iis quae mihi et tibi sunt communia...',
];
const data = text.join('\n');
const message = new Message('Lucius Verus', subj, data);
await message.attach('./1-mixed.js');
console.log(message);
try {
await message.send();
} catch (error) {
await message.warn(error);
}
};
main();