3 Node.js gotchas that I’ve found in the past 3 days

I’ve been writing a NoSQL database server in Node.js. Now before anybody lynch me for mentioning 2 buzzwords or for the fact that I’m writing a database server, please know that I have perfectly valid and informed reasons for doing this.

Ignoring the oddity that is Javascript, one of the earliest issues that I faced was the fact that there seemed to be no way to be notified of when a fs.createWriteStream(...).write(...) completes. WriteStream has a ‘drain’ event but I’m not interested in when the entire write queue is drained, I’m only interested in when a specific write has been drained. After reading the Node.js code it turned out that WriteStream.write() does accept a callback for completion notification, it just isn’t documented.

Another issue was that EPIPE errors on the socket would cause Node.js to throw an exception. I was initially confused because I thought such errors would be emitted through the ‘error’ event, which I haven’t bound on yet. In a mailing list post Ryan Dahl said that errors are thrown as exceptions except when the ‘error’ event is bound, which he admits is a rather dubious behavior. Again this isn’t documented.

I also found out that Node.js would close the connection whenever it encounters a socket error. Any socket error. This is also rather dubious behavior because it would make it impossible to handle certain edge cases involving EPIPE. Suppose that the server is sending some data to the client. The client hasn’t acknowledged that yet, but sends a bunch of data to the server and closes the connection. The server will now encounter an EPIPE error upon writing, making it impossible to read the remaining data that the client has sent. Granted, such a protocol is rather dubious by itself but I do think Node.js should make it at least possible to handle this situation.

Those who want to develop in Node.js have been warned.

6 Comments »

  1. DTrejo said,

    April 6, 2011 @ 2:44 pm

    Please remember that node.js is not even at version 0.5.0! It is nowhere near 1.0!

    I’ve done some work to improve the node docs, and I’ve taken down you concerns, for later addition. Also, node is open source, we welcome your contributions :)

  2. DTrejo said,

    April 6, 2011 @ 2:48 pm

    Here’s information on how to contribute: https://github.com/joyent/node/wiki/Contributing

    I won’t be in the channel much, but feel free to stop into IRC and say hello: http://webchat.freenode.net/?channels=node.js&uio=d4

  3. Hongli said,

    April 6, 2011 @ 2:49 pm

    Hey DTrejo, thanks for the response. I know that it’s open source, in fact I have a documentation patch ready in my local repository. However I’m reluctant to submit it because I need to hand over my copyright and deal with all kinds of legal issues thanks to the contributor agreement.

  4. DTrejo said,

    April 6, 2011 @ 2:53 pm

    Hey Hongli, not sure if saw this, but there’s an easy to fill out electronic version at the very bottom of http://nodejs.org/cla.html. Or are your concerns more about the handing over of copyright?

  5. Hongli said,

    April 6, 2011 @ 2:57 pm

    Yes, I’m concerned about the very fact of handing over copyright as well as all the associated legal consequences, not the ease of submitting the form.

  6. 3D Drawing Pad said,

    May 18, 2011 @ 2:16 am

    Hello Hongli Lai, i was wondering if you knew how to program web apps? Please contact me so we can talk
    thanks

RSS feed for comments on this post · TrackBack URI

Leave a Comment