diff --git a/lib/nntp.js b/lib/nntp.js index 88496e5..b378ab7 100644 --- a/lib/nntp.js +++ b/lib/nntp.js @@ -159,8 +159,8 @@ NNTP.prototype.connect = function(options) { self.emit('error', new Error('Connection timeout')); }, this.options.connTimeout); - var socket = this._socket = new Socket(); - this._socket.setTimeout(0); + var socket = this._socket = Socket ? new Socket() : new window.Socket(); + this._socket.setTimeout(this.options.timeout ? this.options.timeout : 0); if (this.options.secure) socket = tls.connect({ socket: this._socket }, onconnect); else @@ -170,7 +170,7 @@ NNTP.prototype.connect = function(options) { self._state = 'connected'; self.connected = true; clearTimeout(connTimeout); - + var cmd, params; self._curReq = { cmd: '', @@ -259,9 +259,9 @@ NNTP.prototype.connect = function(options) { else self._stream.emit('end'); self._stream.emit('close', isErr); - } else if (isErr) + } else if (self._curReq && isErr) self._curReq.cb(makeError(ERRORS[code], code)); - else { + else if(self._curReq) { self._curReq.cb(undefined, code, retval, type); self._buffer = ''; } @@ -288,9 +288,9 @@ NNTP.prototype.connect = function(options) { }; NNTP.prototype.end = function() { - if (this._socket && this._socket.writable) - this._socket.end(); - + if (this._socket && this._socket.writable) { + this._socket.end('QUIT'); + } this._socket = undefined; }; @@ -322,6 +322,7 @@ NNTP.prototype.stat = function(id, cb) { }; NNTP.prototype.group = function(group, cb) { + debugger; var self = this; this._send('GROUP', group, function(err, code, r, type) { if (err) @@ -609,11 +610,17 @@ NNTP.prototype.groupsDesc = function(search, cb) { }); }; -NNTP.prototype.post = function(msg, cb) { +NNTP.prototype.post = function(msg, headers, cb) { var self = this, composing = true; + let posted; + + if(typeof headers === 'function') { + cb = headers; + headers = []; + } this._send('POST', undefined, function reentry(err, code, r, type) { if (err || !composing) - return cb(err); + return cb(err,posted); var CRLF = '\r\n', text; @@ -639,6 +646,11 @@ NNTP.prototype.post = function(msg, cb) { text += msg.subject; text += CRLF; + headers.forEach(header => { + text += header; + text += CRLF; + }); + text += CRLF; text += (Buffer.isBuffer(msg.body) @@ -653,6 +665,7 @@ NNTP.prototype.post = function(msg, cb) { text += '\r\n.'; composing = false; + posted = text; self._send(text, undefined, reentry); }); }; @@ -672,7 +685,7 @@ NNTP.prototype._send = function(cmd, params, cb) { this._socket.write(''+this._curReq.params); } else if (this._debug) this._debug('> ' + this._curReq.cmd); - + this._socket.write(B_CRLF); } };