Skip to content

Instantly share code, notes, and snippets.

@peernohell
Created October 6, 2018 08:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peernohell/b76099d8566702176f7e3e95857cdbfe to your computer and use it in GitHub Desktop.
Save peernohell/b76099d8566702176f7e3e95857cdbfe to your computer and use it in GitHub Desktop.
mailsplit issue rfc822 header only
'use strict';
const Splitter = require('./lib/message-splitter');
const Transform = require('stream').Transform;
class MailParser extends Transform {
constructor(config) {
let options = {
readableObjectMode: true,
writableObjectMode: false
};
super(options);
this.options = config || {};
this.splitter = new Splitter();
this.splitter.on('readable', () => {
if (this.reading) {
return false;
}
this.readData();
});
}
_transform(chunk, encoding, done) {
if (!chunk || !chunk.length) {
return done();
}
if (this.splitter.write(chunk) === false) {
return this.splitter.once('drain', () => {
done();
});
} else {
return done();
}
}
readData() {
let data = this.splitter.read();
if (data === null) { console.log('readData is null'); }
else { console.log(`readData data[type=${data.type}]${data.contentType && ' ' + data.contentType}${data.value && ` value="${data.value.toString()}"` || ''}`); }
if (data === null) {
this.reading = false;
if (this.endReceived) {
console.log('call endStream');
this.endStream();
}
return;
}
setImmediate(() => this.readData());
}
}
const main = text => {
let encodedText = `Content-Type: multipart/mixed; boundary="ABC"
--ABC
Content-Type: message/rfc822
Content-Type: text/plain; charset=utf-8
Subject: OK
${text}--ABC--`;
// encodedText = 'Content-type: text/plain; charset=utf-8\r\nSubject: ÕÄÖÜ\r\n\r\nok';
// encodedText = 'Content-type: text/plain; charset=utf-8\r\nSubject: ÕÄÖÜ';
const mail = Buffer.from(encodedText, 'utf-8');
let mailParser = new MailParser();
mailParser.end(mail);
mailParser.on('data', data => console.log('data', data));
mailParser.on('end', () => {
console.log('end!!!!!');
});
};
main('');
setTimeout(() => {
console.log('\n\n\n\n*** *** *** *** *** *** *** *** *** *** ***');
main('\n\nok');
}, 1000);
console.log('test end');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment