Skip to content

node: cursors should be readable object streams #3600

Open

Description

@jonathanong

http://rethinkdb.com/api/javascript/event_emitter-cursor/

right now the docs say they inherit from event emitter. a readable object stream should be returned instead, which would be backwards compatible but would also properly buffer as well as .pipe().

please use https://www.npmjs.com/package/readable-stream

Activity

  1. danielmewes commented on Jan 19, 2015

    @danielmewes
    Member

    Pinging @deontologician
    (can't really comment myself since I don't know much JS)

  2. added this to the subsequent milestone on Jan 19, 2015
  3. deontologician commented on Jan 23, 2015

    @deontologician
    Contributor

    I'm not up to date on this stuff, I'd need to read the docs related to it

  4. deontologician commented on Feb 10, 2015

    @deontologician
    Contributor

    @jonathanong Would it be reasonable to just provide the .pipe method instead? In terms of "proper" buffering, what are you looking for specifically that the current driver doesn't provide?

  5. jonathanong commented on Feb 10, 2015

    @jonathanong
    ContributorAuthor

    yes, but don't implement your own. use require('stream').Readable's pipe implementation.

  6. deontologician commented on Feb 10, 2015

    @deontologician
    Contributor

    I guess I'm asking what the benefit is to reimplementing it as a readable stream, vs. just adding a pipe method to our existing implementation.

  7. jonathanong commented on Feb 10, 2015

    @jonathanong
    ContributorAuthor

    i'm not sure how to answer that. the way i read this is, "what is the benefit of being standards compliant?".

  8. deontologician commented on Feb 10, 2015

    @deontologician
    Contributor

    Sorry! I wasn't expressing myself very well. My question is about whether it's enough to implement streams in a duck-typed manner (which we currently provide all the same methods except for .pipe and .then) or whether we should actually subclass ReadableStream, because it's common to do typeof comparisons.

    I'm trying to understand the issue a bit better than I currently do. Thanks for taking the time to bring this issue up

  9. jonathanong commented on Feb 10, 2015

    @jonathanong
    ContributorAuthor

    because you're going to introduce bugs? i don't see this as "benefits". i see "costs" of not doing it. it's also easier to properly implement readable streams.

    maybe you're asking if you can just do:

    Cursor.prototype.pipe = function (dest, options) {
      return this.stream().pipe(dest, options);
    }

    which is fine, but it should be a readable stream's pipe()

  10. deontologician commented on Feb 10, 2015

    @deontologician
    Contributor

    Ok cool, thanks for clarifying. I think we'll do this, I just wanted to make sure I understood the reasoning behind it

  11. coffeemug commented on Feb 10, 2015

    @coffeemug
    Contributor

    Ping @neumino. Would you mind giving a bit of feedback on this, since you understand the ecosystem really well?

  12. modified the milestones: 2.1, subsequent on Feb 10, 2015
  13. neumino commented on Feb 10, 2015

    @neumino
    Member

    Node.js streams are basically an attempt to provide a standard interface for streams, but it is still tagged as unstable even with Node 0.12 -- https://nodejs.org/api/stream.html#stream_stream
    One interesting thing with providing a Node.js stream is that because it is a standard, you can pipe it to a Transform streams or a Writable streams - that other libraries can implement, like the fs module, process.stdout etc..

    One important caveat with streams is that it was implemented for buffers. You can set it to ouput objects/values, but in this case, null is a forbidden value (sending null means that the stream has ended).

    So while it's nice to have, it cannot completely replace the current interface with cursors (except if you pass null values as a ReQL pseudo type or undefined in the streams).
    It's also relatively little work to add a Stream interface. I did it in rethinkdbdash, and it's about 150 lines -- https://github.com/neumino/rethinkdbdash/blob/master/lib/stream.js.

    Note: In rethinkdbdash, streams are currently implemented with the current API:

    var stream = query.toStream();
    stream.on('error', handleError);
    stream.pipe(process.stdout);
    

    The stream is not returned by the promise of run because it would require to cache errors in multiple places:

    query.run(connection, {stream: true}).then(function(stream) {
        stream.on('error', handleError);
        stream.pipe(process.stdout);
    }).error(handleError);
    
  14. danielmewes commented on Feb 10, 2015

    @danielmewes
    Member

    Thanks for the info @neumino !

  15. internalfx commented on Feb 10, 2015

    @internalfx

    I like the idea of using streams. 👍

  16. deontologician commented on Feb 10, 2015

    @deontologician
    Contributor

    So, given the null problem, I think we should probably do like RethinkDBdash and provide a .toStream method. Then the user can decide if nulls are a risk

  17. marshall007 commented on Feb 13, 2015

    @marshall007
    Contributor

    @deontologician you're talking about implementing .toStream() on queries, not the cursor object right?

    It's also worth noting that currently rethinkdbdash simply drops null values from the stream but as @neumino mentioned there are other options. I would lean towards emitting undefined in place of null but keep in mind that's only supported as of v0.12.0.

  18. deontologician commented on Feb 13, 2015

    @deontologician
    Contributor

    Queries can't support toStream since they have to be executed by the server. Either the cursor or the run method would have to have an option to turn a cursor into a stream.

  19. danielmewes commented on Feb 13, 2015

    @danielmewes
    Member

    I think @marshall007 was asking because rethinkdbdash seems to have the toStream() method directly on the query.
    That only works because it has a connection pool though, so the run is implicit in that case.

  20. modified the milestones: 2.1, on Mar 26, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions