Skip to content

Commit

Permalink
update deprecated calls to Buffer()` -m fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
msimerson committed Dec 6, 2018
1 parent 7e36c17 commit d14c6d1
Show file tree
Hide file tree
Showing 18 changed files with 65 additions and 54 deletions.
16 changes: 13 additions & 3 deletions Changes.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@

## 2.8.23 - Mmm DD, 201Y
## 2.8.24 - Mmm DD, 201Y

### Changes

* Implement SIGTERM graceful shutdown if pid is 1 #2547
* early_talker: skip if sender has good karma
* early_talker: skip if sender has good karma #2551
* dockerfile: update to node 10 #2552
* Update deprecated usages of Buffer #2553

### New Features

* Implement SIGTERM graceful shutdown if pid is 1 #2547

### Fixes

* mf.resolvable: reduce timeout by one second (so < plugin.timeout) #2544

## 2.8.23 - Nov 18, 2018


### Changes

* tighten Haraka pattern in .gitignore #2542
Expand All @@ -26,7 +36,7 @@

### Changes

* clamd: add check.authenticated, check.private_ip, check.local_ip option
* clamd: add check.authenticated, check.private_ip, check.local_ip option
* use get_decoded on headers that may be encoded #2537
* connection: move max_mime_part config load to connection init #2528
* outbound: init TLS when we send email, not when old queue file is loaded #2503
Expand Down
6 changes: 3 additions & 3 deletions chunkemitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class ChunkEmitter extends EventEmitter {

fill (input) {
if (typeof input === 'string') {
input = new Buffer(input);
input = Buffer.from(input);
}

// Optimization: don't allocate a new buffer until
Expand All @@ -25,7 +25,7 @@ class ChunkEmitter extends EventEmitter {
this.bufs.push(input);
this.bufs_size += input.length;
if ((input.length + this.bufs_size) > this.buffer_size) {
this.buf = new Buffer(this.buffer_size);
this.buf = Buffer.alloc(this.buffer_size);
const in_new = Buffer.concat(this.bufs, this.bufs_size);
input = in_new;
// Reset
Expand All @@ -41,7 +41,7 @@ class ChunkEmitter extends EventEmitter {
let remaining = this.buffer_size - this.pos;
if (remaining === 0) {
this.emit('data', this.buf); //.slice(0));
this.buf = new Buffer(this.buffer_size);
this.buf = Buffer.alloc(this.buffer_size);
this.pos = 0;
remaining = this.buffer_size;
}
Expand Down
13 changes: 5 additions & 8 deletions connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,8 @@ class Connection {
const has_host = self.remote.host ? `${self.remote.host} ` : '';
const rhost = `client ${has_host}[${self.remote.ip}]`

if (!self.client.on) {
return;
}
if (!self.client.on) return;

self.client.on('end', () => {
if (self.state >= states.DISCONNECTING) return;
self.remote.closed = true;
Expand Down Expand Up @@ -202,9 +201,7 @@ class Connection {
self.process_data(data);
});

const ha_list = net.isIPv6(self.remote.ip) ?
haproxy_hosts_ipv6
: haproxy_hosts_ipv4;
const ha_list = net.isIPv6(self.remote.ip) ? haproxy_hosts_ipv6 : haproxy_hosts_ipv4;

if (ha_list.some((element, index, array) => {
return ipaddr.parse(self.remote.ip).match(element[0], element[1]);
Expand Down Expand Up @@ -333,7 +330,7 @@ class Connection {
/* eslint no-control-regex: 0 */
if (/[^\x00-\x7F]/.test(this.current_line)) {
// See if this is a TLS handshake
const buf = new Buffer(this.current_line.substr(0,3), 'binary');
const buf = Buffer.from(this.current_line.substr(0,3), 'binary');
if (buf[0] === 0x16 && buf[1] === 0x03 &&
(buf[2] === 0x00 || buf[2] === 0x01)) // SSLv3/TLS1.x format
{
Expand Down Expand Up @@ -521,7 +518,7 @@ class Connection {
this.transaction.notes.data_line_length_exceeded = true;
const b = Buffer.concat([
this.current_data.slice(0, maxlength - 2),
new Buffer("\r\n ", 'utf8'),
Buffer.from("\r\n ", 'utf8'),
this.current_data.slice(maxlength - 2)
], this.current_data.length + 3);
this.current_data = b;
Expand Down
6 changes: 3 additions & 3 deletions dkim.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Buf {

pop (buf) {
if (!this.bar.length) {
if (!buf) buf = new Buffer('');
if (!buf) buf = Buffer.from('');
return buf;
}
if (buf && buf.length) {
Expand Down Expand Up @@ -215,7 +215,7 @@ class DKIMObject {
l = this.line_buffer.pop(line).toString('utf-8');
l = l.replace(/[\t ]+(\r?\n)$/,"$1");
l = l.replace(/[\t ]+/g,' ');
l = this.line_buffer.pop(new Buffer(l));
l = this.line_buffer.pop(Buffer.from(l));
this.bh.update(l);
}
}
Expand Down Expand Up @@ -488,7 +488,7 @@ class DKIMVerifyStream extends Stream {

// Check for LF line endings and convert to CRLF if necessary
if (line[line.length-2] !== 0x0d) {
line = Buffer.concat([ line.slice(0, line.length-1), new Buffer("\r\n") ], line.length+1);
line = Buffer.concat([ line.slice(0, line.length-1), Buffer.from("\r\n") ], line.length+1);
}

// Look for CRLF
Expand Down
11 changes: 5 additions & 6 deletions docs/tutorials/Migrating_from_v1_to_v2.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ to an application or over a network, your code will become significantly
simpler (and a lot faster).

In v1.x Haraka populated the `transaction.data_lines` array for each line of
data received. If you were writing the data to a socket then you had to handle
backpressure manually by checking the return of `write()` and adding
data received. If you were writing the data to a socket then you had to handle backpressure manually by checking the return of `write()` and adding
`on('drain')` handlers like so:

var data_marker = 0;
Expand All @@ -53,7 +52,7 @@ backpressure manually by checking the return of `write()` and adding
while (wrote_all && (data_marker < connection.transaction.data_lines.length)) {
var line = connection.transaction.data_lines[data_marker];
data_marker++;
wrote_all = socket.write(new Buffer(line.replace(/^\./, '..').replace(/\r?\n/g, '\r\n')), 'binary');
wrote_all = socket.write(Buffer.from(line.replace(/^\./, '..').replace(/\r?\n/g, '\r\n')), 'binary');
if (!wrote_all) return;
}
// we get here if wrote_all still true, and we got to end of data_lines
Expand All @@ -72,14 +71,14 @@ backpressure manually by checking the return of `write()` and adding
In v2.x this now becomes:

connection.transaction.message_stream.pipe(socket, {dot_stuffing: true, ending_dot: true});

This automatically chunks the data, handles backpressure and will apply any
necessary format changes. See `docs/Transaction.md` for the full details.

If you need to handle the input data by line, then you will need to create
your own writable stream and then pipe the message to the stream and then
extract the lines from the stream of data. See `plugins/dkim_sign.js` for
an example.
an example.

Fixing attachment\_hooks plugins
-------------------------------
Expand All @@ -106,7 +105,7 @@ for example if you are sending attachments to a remote service. In order
for this backpressure to apply to the connection itself (so that we don't
have to buffer up data in memory), we need to provide the connection object
to the stream:

var transaction = connection.transaction;
transaction.attachment_hooks(
function (ctype, filename, body, stream) {
Expand Down
20 changes: 10 additions & 10 deletions mailbody.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Body extends events.EventEmitter {
this.decode_function = null;
this.children = []; // if multipart
this.state = 'start';
this.buf = new Buffer(buf_siz);
this.buf = Buffer.alloc(buf_siz);
this.buf_fill = 0;
this.decode_accumulator = '';
this.decode_qp = utils.decode_qp;
Expand Down Expand Up @@ -137,7 +137,7 @@ class Body extends events.EventEmitter {
}

_empty_filter (ct, enc) {
let new_buf = new Buffer('');
let new_buf = Buffer.from('');
this.filters.forEach(function (filter) {
new_buf = filter(ct, enc, new_buf) || new_buf;
});
Expand All @@ -149,7 +149,7 @@ class Body extends events.EventEmitter {
if (this.state === 'attachment') {
if (this.buf_fill > 0) {
// see below for why we create a new buffer here.
const to_emit = new Buffer(this.buf_fill);
const to_emit = Buffer.alloc(this.buf_fill);
this.buf.copy(to_emit, 0, 0, this.buf_fill);
this.attachment_stream.emit_data(to_emit);
}
Expand All @@ -165,7 +165,7 @@ class Body extends events.EventEmitter {
if (this.state === 'attachment') {
if (this.buf_fill > 0) {
// see below for why we create a new buffer here.
const to_emit = new Buffer(this.buf_fill);
const to_emit = Buffer.alloc(this.buf_fill);
this.buf.copy(to_emit, 0, 0, this.buf_fill);
this.attachment_stream.emit_data(to_emit);
}
Expand Down Expand Up @@ -305,7 +305,7 @@ class Body extends events.EventEmitter {
// using async code, it will get overwritten under us. Creating a new
// buffer eliminates that problem (at the expense of a malloc and a
// memcpy())
const to_emit = new Buffer(this.buf_fill);
const to_emit = Buffer.alloc(this.buf_fill);
this.buf.copy(to_emit, 0, 0, this.buf_fill);
this.attachment_stream.emit_data(to_emit);
if (buf.length > buf_siz) {
Expand Down Expand Up @@ -347,7 +347,7 @@ class Body extends events.EventEmitter {
if (emit_length > 0) {
const emit_now = to_process.substring(0, emit_length);
this.decode_accumulator = to_process.substring(emit_length);
return new Buffer(emit_now, 'base64');
return Buffer.from(emit_now, 'base64');
} else {
this.decode_accumulator = '';
// This is the end of the base64 data, we don't really have enough bits
Expand All @@ -360,12 +360,12 @@ class Body extends events.EventEmitter {
while (to_process.length > 0 && to_process.length < 4) {
to_process += '=';
}
return new Buffer(to_process, 'base64');
return Buffer.from(to_process, 'base64');
}
}

decode_8bit (line) {
return new Buffer(line, 'binary');
return Buffer.from(line, 'binary');
}
}

Expand Down Expand Up @@ -426,11 +426,11 @@ function insert_banner (ct, enc, buf, banners) {
}

if (!banner_buf) {
banner_buf = new Buffer(banner_str);
banner_buf = Buffer.from(banner_str);
}

// Allocate a new buffer: (7 or 2 is <P>...</P> vs \n...\n - correct that if you change those!)
const new_buf = new Buffer(buf.length + banner_buf.length + (is_html ? 7 : 2));
const new_buf = Buffer.alloc(buf.length + banner_buf.length + (is_html ? 7 : 2));

// Now we find where to insert it and combine it with the original buf:
if (is_html) {
Expand Down
4 changes: 2 additions & 2 deletions mailheader.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class Header {
const matches = /\bcharset\s*=\s*["']?([\w_-]*)/.exec(this.get('content-type'));
if (matches && !/UTF-?8/i.test(matches[1])) {
const encoding = matches[1];
const source = new Buffer(val, 'binary');
const source = Buffer.from(val, 'binary');
val = try_convert(source, encoding).toString();
}
}
Expand Down Expand Up @@ -214,7 +214,7 @@ function _decode_header (matched, encoding, lang, cte, data) {
data = utils.decode_qp(data.replace(/_/g, ' '));
break;
case 'B':
data = new Buffer(data, "base64");
data = Buffer.from(data, "base64");
break;
default:
logger.logerror("Invalid header encoding type: " + cte);
Expand Down
8 changes: 4 additions & 4 deletions messagestream.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class MessageStream extends Stream {
const self = this;

if (typeof line === 'string') {
line = new Buffer(line);
line = Buffer.from(line);
}

// create a ChunkEmitter
Expand Down Expand Up @@ -286,7 +286,7 @@ class MessageStream extends Stream {
line[line.length-1] === 0x0a && line[line.length-2] === 0x0d)
{
// We copy the line to a new buffer before modifying the copy
line = new Buffer(line);
line = Buffer.from(line);
line[line.length-2] = 0x0a;
line = line.slice(0, line.length-1);
}
Expand All @@ -309,7 +309,7 @@ class MessageStream extends Stream {
this.read_ce.end(function () {
if (self.clamd_style) {
// Add 0 length to notify end
const buf = new Buffer(4);
const buf = Buffer.alloc(4);
buf.writeUInt32BE(0, 0);
self.emit('data', buf);
}
Expand Down Expand Up @@ -342,7 +342,7 @@ class MessageStream extends Stream {
this.read_ce.on('data', function (chunk) {
if (self.clamd_style) {
// Prefix data length to the beginning of line
const buf = new Buffer(chunk.length+4);
const buf = Buffer.alloc(chunk.length+4);
buf.writeUInt32BE(chunk.length, 0);
chunk.copy(buf, 4);
self.emit('data', buf);
Expand Down
2 changes: 1 addition & 1 deletion plugins/dkim_sign.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class DKIMSignStream extends Stream {

// Add trailing CRLF if we have data left over
if (this.buffer.ar.length) {
this.buffer.ar.push(new Buffer("\r\n"));
this.buffer.ar.push(Buffer.from("\r\n"));
this.buffer.len += 2;
const le = Buffer.concat(this.buffer.ar, this.buffer.len);
this.hash.update(le);
Expand Down
2 changes: 1 addition & 1 deletion plugins/queue/qmail-queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ exports.hook_queue = function (next, connection) {
plugin.loginfo("Message Stream sent to qmail. Now sending envelope");
// now send envelope
// Hope this will be big enough...
const buf = new Buffer(4096);
const buf = Buffer.alloc(4096);
let p = 0;
buf[p++] = 70;
const mail_from = connection.transaction.mail_from.address();
Expand Down
2 changes: 1 addition & 1 deletion plugins/queue/smtp_forward.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ exports.auth = function (cfg, connection, smtp_client) {
}

const base64 = function (str) {
const buffer = new Buffer(str, 'UTF-8');
const buffer = Buffer.from(str, 'UTF-8');
return buffer.toString('base64');
};

Expand Down
4 changes: 4 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,10 @@ Server.get_smtp_server = function (host, port, inactivity_timeout, done) {
verifyError: client.authorizationError,
peerCertificate: client.getPeerCertificate(),
});

client.on('error', function (err) {
connection.fail(err);
});
}

if (port === Server.cfg.main.smtps_port) {
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/util_hmailitem.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ exports.createHMailItem = function (outbound_context, options, callback) {
while ((match = re.exec(contents))) {
let line = match[1];
line = line.replace(/\r?\n?$/, '\r\n'); // make sure it ends in \r\n
conn.transaction.add_data(new Buffer(line));
conn.transaction.add_data(Buffer.from(line));
contents = contents.substr(match[1].length);
if (contents.length === 0) {
break;
Expand Down
10 changes: 5 additions & 5 deletions tests/mailbody.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,14 @@ exports.banners = {
const insert_banners_fn = body.filters[0];

content_type = 'text/html';
buf = new Buffer("winter </html>");
buf = Buffer.from("winter </html>");
new_buf = insert_banners_fn (content_type, enc, buf);
test.equal(new_buf.toString(), "winter <P>htmlbanner</P></html>",
"html banner looks ok");


content_type = 'text/plain';
buf = new Buffer("winter");
buf = Buffer.from("winter");
new_buf = insert_banners_fn (content_type, enc, buf);
test.equal(new_buf.toString(), "winter\ntextbanner\n",
"text banner looks ok");
Expand All @@ -141,7 +141,7 @@ exports.banners = {


content_type = 'text/html';
const empty_buf = new Buffer('');
const empty_buf = Buffer.from('');
new_buf = insert_banners_fn (content_type, enc, empty_buf);
test.equal(new_buf.toString(), "<P>htmlbanner</P>",
"empty html part gets a banner" );
Expand Down Expand Up @@ -189,10 +189,10 @@ exports.filters = {
const body = new Body();
body.add_filter(function (ct, enc, buf) {
if (/^text\/plain/.test(ct)) {
return new Buffer("TEXT FILTERED");
return Buffer.from("TEXT FILTERED");
}
else if (/text\/html/.test(ct)) {
return new Buffer("<p>HTML FILTERED</p>");
return Buffer.from("<p>HTML FILTERED</p>");
}
});
const parts = _fill_body(body);
Expand Down
Loading

0 comments on commit d14c6d1

Please sign in to comment.