forked from noInfoPath/noinfopath-mailparser
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpipe.js
More file actions
72 lines (61 loc) · 1.92 KB
/
pipe.js
File metadata and controls
72 lines (61 loc) · 1.92 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/* eslint no-console:0 */
'use strict';
const util = require('util');
const fs = require('fs');
const MailParser = require('../lib/mail-parser.js');
let parser = new MailParser();
let input = fs.createReadStream(__dirname + '/nodemailer.eml');
let attachments = [];
input.pipe(parser);
parser.on('headers', headers => {
console.log(util.inspect(headers, false, 22));
});
parser.on('data', data => {
if (data.type === 'text') {
Object.keys(data).forEach(key => {
console.log(key);
console.log('----');
console.log(data[key]);
});
}
if (data.type === 'attachment') {
attachments.push(data);
data.chunks = [];
data.chunklen = 0;
let size = 0;
Object.keys(data).forEach(key => {
if (typeof data[key] !== 'object' && typeof data[key] !== 'function') {
console.log('%s: %s', key, JSON.stringify(data[key]));
}
});
data.content.on('readable', () => {
let chunk;
while ((chunk = data.content.read()) !== null) {
size += chunk.length;
data.chunks.push(chunk);
data.chunklen += chunk.length;
}
});
data.content.on('end', () => {
data.buf = Buffer.concat(data.chunks, data.chunklen);
console.log('%s: %s B', 'size', size);
// attachment needs to be released before next chunk of
// message data can be processed
data.release();
});
}
});
parser.on('end', () => {
console.log('READY');
parser.updateImageLinks(
(attachment, done) => done(false, 'data:' + attachment.contentType + ';base64,' + attachment.buf.toString('base64')),
(err, html) => {
if (err) {
console.log(err);
}
if (html) {
console.log(html);
}
}
);
});