Skip to content

Commit

Permalink
Make Message-ID first header
Browse files Browse the repository at this point in the history
(means that its position doesn't have to be tracked anymore)
  • Loading branch information
animetosho committed May 8, 2016
1 parent 838a3dc commit e66f4ed
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions lib/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,11 @@ function Post(headers, pool, date) {
this.pool = pool;
this.genTime = date ? date.getTime() : Date.now();

// TODO: consider writing Message-ID as the first header - this simplifies some logic
this.messageId = randString() + '-' + this.genTime + '@nyuu';
if(pool) {
this.buf = pool.get();
while(1) {
this.postPos = this.buf.write('Message-ID: <' + this.messageId + '>\r\n', 0, ENCODING);
for(var h in headers) {
var v = headers[h];

Expand All @@ -191,6 +191,7 @@ function Post(headers, pool, date) {
this.headers[h.toLowerCase()] = v;
this.postPos += this.buf.write(h + ': ' + v.replace(RE_BADCHAR, '') + '\r\n', this.postPos, ENCODING);
}
this.postPos += this.buf.write('\r\n', this.postPos, ENCODING);
if(this.postPos >= this.buf.length) {
// likely overflowed, try again
this.buf = new Buffer(this.buf.length + Math.max(this.buf.length, BUFFER_ENLARGE_SPACE));
Expand All @@ -199,17 +200,14 @@ function Post(headers, pool, date) {
}
break;
}

// write in Message-ID
this.mIdPos = this.postPos + 13 /* 'Message-ID: <'.length */;
this.postPos += this.buf.write('Message-ID: <' + this.messageId + '>\r\n\r\n', this.postPos, ENCODING);
} else {
this.bufs = [];
var pushBuf = function(data) {
var d = new Buffer(data + '\r\n', ENCODING);
this.postPos += d.length;
this.bufs.push(d);
}.bind(this);
pushBuf('Message-ID: <' + this.messageId + '>');
for(var h in headers) {
var v = headers[h];

Expand All @@ -219,9 +217,7 @@ function Post(headers, pool, date) {
this.headers[h.toLowerCase()] = v;
pushBuf(h + ': ' + v.replace(RE_BADCHAR, ''));
}
// write in Message-ID
this.mIdPos = this.postPos + 13 /* 'Message-ID: <'.length */;
pushBuf('Message-ID: <' + this.messageId + '>\r\n');
pushBuf('');
}
}
Post.prototype = {
Expand All @@ -234,7 +230,7 @@ Post.prototype = {

randomizeMessageID: function() {
var rnd = randString();
this.data.write(rnd, this.mIdPos, ENCODING);
this.data.write(rnd, 13 /* 'Message-ID: <'.length */, ENCODING);
return this.messageId = rnd + this.messageId.substr(24);
},

Expand Down

0 comments on commit e66f4ed

Please sign in to comment.