Skip to content

Commit

Permalink
fix streams in on-copy and message-handler. message-handler optimizat…
Browse files Browse the repository at this point in the history
…ions, filter-handler optimizations
  • Loading branch information
NickOvt committed Dec 4, 2024
1 parent fa849f2 commit d1c67e6
Show file tree
Hide file tree
Showing 3 changed files with 453 additions and 387 deletions.
15 changes: 9 additions & 6 deletions lib/filter-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,9 @@ class FilterHandler {

let rawchunks = chunks;

let prepared;

let raw = Buffer.concat(chunks, chunklen);
let raw;

let prepared;
if (options.mimeTree) {
if (options.mimeTree && options.mimeTree.header) {
// remove old headers
Expand All @@ -159,6 +158,7 @@ class FilterHandler {
mimeTree: options.mimeTree
});
} else {
raw = Buffer.concat(chunks, chunklen);
prepared = await this.prepareMessage({
raw
});
Expand Down Expand Up @@ -661,11 +661,14 @@ class FilterHandler {

date: false,
flags,

raw,
rawchunks
rawchunks,
chunklen
};

if (raw) {
messageOpts.raw = raw;
}

if (options.verificationResults) {
messageOpts.verificationResults = options.verificationResults;
}
Expand Down
22 changes: 17 additions & 5 deletions lib/handlers/on-copy.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,15 +233,27 @@ async function copyHandler(server, messageHandler, connection, mailbox, update,
const newPrepared = await new Promise((resolve, reject) => {
if (targetMailboxEncrypted && !isMessageEncrypted && userData.pubKey) {
// encrypt message
const outputStream = messageHandler.indexer.rebuild(messageData.mimeTree).value; // get raw rebuilder stream
let raw = Buffer.from([], 'binary'); // set initial raw
// get raw from existing mimetree
let outputStream = messageHandler.indexer.rebuild(messageData.mimeTree); // get raw rebuilder response obj (.value is the stream)

if (!outputStream || outputStream.type !== 'stream' || !outputStream.value) {
return reject(new Error('Cannot fetch message'));
}
outputStream = outputStream.value; // set stream to actual stream object (.value)

let chunks = [];
let chunklen = 0;
outputStream
.on('data', data => {
raw = Buffer.concat([raw, data]);
.on('readable', () => {
let chunk;
while ((chunk = outputStream.read()) !== null) {
chunks.push(chunk);
chunklen += chunk.length;
}
})
.on('end', () => {
messageHandler.encryptMessages(userData.pubKey || '', raw, (err, res) => {
const raw = Buffer.concat(chunks, chunklen);
messageHandler.encryptMessages(userData.pubKey, raw, (err, res) => {
if (err) {
return reject(err);
}
Expand Down
Loading

0 comments on commit d1c67e6

Please sign in to comment.