Skip to content

Commit

Permalink
transaction: update Buffer syntax (sync with Haraka) (#56)
Browse files Browse the repository at this point in the history
- Added test for add_data() when parse_body is true.
  • Loading branch information
lnedry authored Dec 3, 2023
1 parent c34fb41 commit df7c768
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
10 changes: 5 additions & 5 deletions lib/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class Transaction {

add_data (line) {
if (typeof line === 'string') { // This shouldn't ever happen...
line = new Buffer(line, 'binary');
line = Buffer.from(line, 'binary');
}
// check if this is the end of headers line
if (this.header_pos === 0 &&
Expand All @@ -86,7 +86,7 @@ class Transaction {
line.toString('binary').replace(/\r\n$/, '\n'));
}
}
else if (this.header_pos && this.parse_body) {
else if (this.parse_body) {
if (line[0] === 0x2E) line = line.slice(1); // Strip leading "."
let new_line = this.body.parse_more(
line.toString('binary').replace(/\r\n$/, '\n'));
Expand All @@ -95,8 +95,8 @@ class Transaction {
return; // buffering for banners
}

new_line = new_line.replace(/^\./gm, '..').replace(/\r?\n/gm, '\r\n');
line = new Buffer(new_line,'binary');
new_line = new_line.toString('binary').replace(/^\./gm, '..').replace(/\r?\n/gm, '\r\n');
line = Buffer.from(new_line,'binary');
}

if (!this.discard_data) this.message_stream.add_line(line);
Expand Down Expand Up @@ -131,7 +131,7 @@ class Transaction {
data = data.toString('binary')
.replace(/^\./gm, '..')
.replace(/\r?\n/gm, '\r\n');
const line = new Buffer(data, 'binary');
const line = Buffer.from(data, 'binary');

if (!this.discard_data) this.message_stream.add_line(line);
}
Expand Down
12 changes: 11 additions & 1 deletion test/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ describe('transaction', function () {
done();
})

it('can add_data with parse_body = true', (done) => {
const newTrans = transaction.createTransaction('', {"headers":{"max_lines":1000}});
newTrans.parse_body = true;
newTrans.add_data(`From: test@example.com\r\n`);
newTrans.add_data(`\r\n`);
newTrans.add_data(`abcde\r\n`);
newTrans.add_data(`zyxwvu\r\n`);
done();
})

describe('Header', function () {

beforeEach((done) => {
Expand All @@ -60,4 +70,4 @@ describe('transaction', function () {
done();
})
})
})
})

0 comments on commit df7c768

Please sign in to comment.