-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdelivery.js
More file actions
47 lines (38 loc) · 1.06 KB
/
delivery.js
File metadata and controls
47 lines (38 loc) · 1.06 KB
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
'use strict';
const fs = require('node:fs');
const { console } = require('./logger.js');
class Attachment {
constructor(filename) {
this.filename = filename;
this.readable = fs.createReadStream(filename);
}
}
class Message {
constructor(to, subject, text) {
this.to = to;
this.subject = subject;
this.text = text;
this.files = [];
}
async attach({ filename, readable }) {
const data = [];
for await (const chunk of readable) data.push(chunk);
const content = Buffer.concat(data);
this.files.push({ filename, content });
}
}
const send = async (message) => {
console.log('Sending message...');
console.log(message);
throw new Error('Nullae electronicae epistulae in Roma');
};
const toBool = [() => true, () => false];
const fileExists = (name) => fs.promises.access(name).then(...toBool);
const canAttach = async (files) => {
for (const { filename } of files) {
const exists = await fileExists(filename);
if (!exists) return false;
}
return true;
};
module.exports = { Attachment, Message, send, canAttach };