Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Change `EventEmitter` to `Stream` for ".pipe()" and other Stream goodies #154

Closed
wants to merge 1 commit into from

2 participants

Cristian Douce Brian White
Cristian Douce

As explained in here it becomes handy to have a Stream inherited message. The pipe method can really come handy in much ways.

For example I used it in combination with mailparser module like this:

//...
imap.fetch(results, {
  headers: {
    parse: false
  },
  body: true,
  cb: function(fetch) {
    fetch.on("message", function(message) {
      var parser = new mailparser.MailParser;

      parser.on("headers", function(headers) {
        return console.log("Message Headers:", headers.received);
      });

      parser.on("end", function(mail) {
        return console.log("Mail:", mail);
      });

      message.pipe(parser);
    });
  }
}, function(err) {
  imap.logout();
});

//...
Brian White mscdex commented on the diff
lib/imap.js
@@ -105,7 +105,7 @@ function ImapConnection(options) {
this.authenticated = false;
}
-inherits(ImapConnection, EventEmitter);
+inherits(ImapConnection, Stream);
Brian White Owner
mscdex added a note

This doesn't make much sense since ImapConnection never emits 'data' and doesn't have a write() method.

would you like me to revert this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Brian White
Owner

Well, there's a bit more that's needed for this to be a real ReadableStream than just changing what object it inherits from (e.g. a 'readable' property that is set appropriately, appropriate events being sent at the end of the stream, etc).

Brian White mscdex commented on the diff
lib/imap.js
((6 lines not shown))
function ImapFetch() {
this._parse = false;
}
-inherits(ImapFetch, EventEmitter);
+inherits(ImapFetch, Stream);
Brian White Owner
mscdex added a note

Same as with ImapConnection, there are no 'data' events emitted by this object and there is no write() method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Cristian Douce

Would you prefer that only ImapMessage extends Stream?

Brian White
Owner

Yeah, ImapMessage extending Stream is fine. However as previously mentioned, more plumbing needs to be done for it to a be real stream since inheriting from Stream only gives you .pipe() for free.

Brian White
Owner

As mentioned in #203, (streams2) streams are now passed after the rewrite.

Brian White mscdex closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 17, 2013
  1. Cristian Douce
This page is out of date. Refresh to see the latest.
Showing with 5 additions and 5 deletions.
  1. +5 −5 lib/imap.js
10 lib/imap.js
View
@@ -3,7 +3,7 @@ var assert = require('assert'),
inspect = require('util').inspect,
inherits = require('util').inherits,
Socket = require('net').Socket,
- EventEmitter = require('events').EventEmitter,
+ Stream = require('stream').Stream,
utf7 = require('utf7').imap,
// customized copy of XRegExp to deal with multiple variables of the same
// name
@@ -38,7 +38,7 @@ var IDLE_NONE = 1,
function ImapConnection(options) {
if (!(this instanceof ImapConnection))
return new ImapConnection(options);
- EventEmitter.call(this);
+ Stream.call(this);
this._options = {
username: options.username || options.user || '',
@@ -105,7 +105,7 @@ function ImapConnection(options) {
this.authenticated = false;
}
-inherits(ImapConnection, EventEmitter);
+inherits(ImapConnection, Stream);
Brian White Owner
mscdex added a note

This doesn't make much sense since ImapConnection never emits 'data' and doesn't have a write() method.

would you like me to revert this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
module.exports = ImapConnection;
module.exports.ImapConnection = ImapConnection;
@@ -1498,9 +1498,9 @@ function ImapMessage() {
this.structure = undefined;
this.size = undefined;
}
-inherits(ImapMessage, EventEmitter);
+inherits(ImapMessage, Stream);
function ImapFetch() {
this._parse = false;
}
-inherits(ImapFetch, EventEmitter);
+inherits(ImapFetch, Stream);
Brian White Owner
mscdex added a note

Same as with ImapConnection, there are no 'data' events emitted by this object and there is no write() method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.