From 13f715e233de18cf2bad6fab64e8b4922fccf0a0 Mon Sep 17 00:00:00 2001 From: Matt Sergeant Date: Sat, 30 Sep 2017 18:07:33 -0400 Subject: [PATCH] This just cleans up the code, probably no fix (#2066) * clean up todo_writing --- outbound/hmail.js | 2 +- outbound/index.js | 17 ++++++----------- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/outbound/hmail.js b/outbound/hmail.js index 2ef0ba00b..bbe77b94c 100644 --- a/outbound/hmail.js +++ b/outbound/hmail.js @@ -113,7 +113,7 @@ HMailItem.prototype.read_todo = function () { // I'm making the assumption here we won't ever read less than 4 bytes // as no filesystem on the planet should be that dumb... tl_reader.destroy(); - const todo_len = (buf[0] << 24) + (buf[1] << 16) + (buf[2] << 8) + buf[3]; + const todo_len = buf.readUInt32BE(0); const td_reader = fs.createReadStream(self.path, {encoding: 'utf8', start: 4, end: todo_len + 3}); self.data_start = todo_len + 4; let todo = ''; diff --git a/outbound/index.js b/outbound/index.js index f1fd393bd..f3b30e59d 100644 --- a/outbound/index.js +++ b/outbound/index.js @@ -319,20 +319,15 @@ exports.build_todo = function (todo, ws, write_more) { return value; } } - const todo_str = new Buffer(JSON.stringify(todo, exclude_from_json)); - // since JS has no pack() we have to manually write the bytes of a long - const todo_length = new Buffer(4); - const todo_l = todo_str.length; - todo_length[3] = todo_l & 0xff; - todo_length[2] = (todo_l >> 8) & 0xff; - todo_length[1] = (todo_l >> 16) & 0xff; - todo_length[0] = (todo_l >> 24) & 0xff; - - const buf = Buffer.concat([todo_length, todo_str], todo_str.length + 4); + const todo_json = JSON.stringify(todo, exclude_from_json); + const buf = new Buffer(4 + todo_json.length); + buf.writeUInt32BE(todo_json.length, 0); + buf.write(todo_json, 4); const continue_writing = ws.write(buf); - if (continue_writing) return write_more(); + if (continue_writing) return process.nextTick(write_more); + ws.once('drain', write_more); };