Minor bug when using dnode synchronously #108

Closed
ashtuchkin opened this Issue Sep 8, 2012 · 1 comment

Comments

Projects
None yet
2 participants
Contributor

ashtuchkin commented Sep 8, 2012

When both client and server are in one process (scenario: unit tests), there could be several callbacks back-and-fourth synchronously through the dnode. After 2-3 callbacks, the dnode stops responding.

I tracked down the error (see https://github.com/substack/dnode/blob/master/lib/dnode.js#L126) -
the self._line variable should be cleaned before calling self.handle(row), because inside that fn we could eventually call dnode.write again and this would append new json to the uncleaned self._line, which will break JSON.parse. Also, it would be great to emit the json.parse error, because now the stream just ends without any indication of error.

TL;DR - lines 126 and 127 should be swapped.

Current code:

dnode.prototype.write = function (buf) {
 ..
        for (var i = 0; i < buf.length; i++) {
            if (buf.charCodeAt(i) === 0x0a) {
                try { row = json.parse(self._line) }
                catch (err) { return self.end() }

                self.handle(row);
                self._line = '';
            }
            else self._line += buf.charAt(i)
        }
}
Owner

substack commented Mar 1, 2013

Thanks for tracking this down and fixing it! Sorry for the delay.

substack closed this Mar 1, 2013

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