stdin read issues

260 views
Skip to first unread message

Mikeal Rogers

unread,
Dec 14, 2009, 7:40:56 PM12/14/09
to nodejs
I'm writing an alternate view server for CouchDB using node.js and I
ran in to an issue with stdin reads.

CouchDB defines a line protocol for view servers to be used over stdin/
stdout. The lines are seperated by \n, which in all other languages is
easy to handle using a basic readline() call.

After some testing I realized that the data event on stdin in node.js
only fires for \r\n and since node.js doesn't support any method of
raw read() calls on stdin I'm at a loss as to how I should proceed.

-Mikeal

Ryan Dahl

unread,
Dec 15, 2009, 2:29:43 AM12/15/09
to nod...@googlegroups.com
stdin, when associated with a terminal device, is line buffered by
default. Node doesn't change that. What is needed is bindings to the
termios, in particular tcsetattr() (I think). This would be super cool
because we could also implement libreadline-like functionality in
javascript (command history, etc).

For your specific problem, a simple hack to get ti working would be to
change couchdb to separate messages by \r\n.

Ryan Dahl

unread,
Dec 15, 2009, 2:31:17 AM12/15/09
to nod...@googlegroups.com
> stdin, when associated with a terminal device, is line buffered by
> default. Node doesn't change that. What is needed is bindings to the
> termios, in particular tcsetattr() (I think). This would be super cool
> because we could also implement libreadline-like functionality in
> javascript (command history, etc).

Note, "when associated with a terminal device", is the key part here.
Shouldn't couch launch node as a subprocess? In that case stdin in
shouldn't be buffered.

Mikeal Rogers

unread,
Dec 15, 2009, 3:13:45 AM12/15/09
to nod...@googlegroups.com
I've spent some more time debugging this and it's not actually an issue with \r\n differentials.

Even using \r it's not being read. I'm pretty sure there is an event or signal that is fired when hitting return in the terminal that is not being fired when I just write to stdin from another program. In fact I can write lines and flush all day and the data event will never fire in node.

This *may* be an issue with libev and Mac OS X, I've read about some bugs in kqueue on non-socket objects which could be an issue.

And yes, this is all being launched as a subprocess.

-Mikeal




--

You received this message because you are subscribed to the Google Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com.
To unsubscribe from this group, send email to nodejs+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nodejs?hl=en.



Ryan Dahl

unread,
Dec 15, 2009, 4:47:54 AM12/15/09
to nod...@googlegroups.com
On Tue, Dec 15, 2009 at 9:13 AM, Mikeal Rogers <mikeal...@gmail.com> wrote:
> I've spent some more time debugging this and it's not actually an issue with
> \r\n differentials.
>
> Even using \r it's not being read. I'm pretty sure there is an event or
> signal that is fired when hitting return in the terminal that is not being
> fired when I just write to stdin from another program. In fact I can write
> lines and flush all day and the data event will never fire in node.
>
> This *may* be an issue with libev and Mac OS X, I've read about some bugs in
> kqueue on non-socket objects which could be an issue.
>
> And yes, this is all being launched as a subprocess.
>

It would be nice if you could extract a little node script which
demonstrated this so that I can debug it.

Mikeal Rogers

unread,
Dec 15, 2009, 1:21:09 PM12/15/09
to nod...@googlegroups.com
Here you go:

file test.js
========================
var sys = require("sys");

var child = process.createChildProcess('/Users/mikeal/tmp/test/sub.js')

child.addListener("output", function (data){ sys.puts("success") });
child.write('test\r')
========================

file sub.js
========================
#!/usr/bin/env node
var sys = require("sys");

process.stdio.open()
process.stdio.addListener("data", function (data) {sys.puts(data)})
========================

Don't forget to chmod 755 sub.js

I've only verified that this bug exists on Mac OS 10.6.2

-Mikeal


Tim Caswell

unread,
Dec 15, 2009, 1:36:05 PM12/15/09
to nod...@googlegroups.com
I found the same behavior while implementing my sqlite driver that wraps the sqlite3 program.  I can't stream database results because all the data gets returned at once.

Ryan Dahl

unread,
Feb 5, 2010, 8:37:46 PM2/5/10
to nod...@googlegroups.com
On Tue, Dec 15, 2009 at 10:36 AM, Tim Caswell <t...@creationix.com> wrote:
> I found the same behavior while implementing my sqlite driver that wraps the
> sqlite3 program.  I can't stream database results because all the data gets
> returned at once.

Fixed in fe85062046ac36c2207aa0bbd2f496663eaca996 (?)

Reply all
Reply to author
Forward
0 new messages