creationix / nstore

nStore is a simple, in-process key/value database for node.js
MIT License
392 stars 31 forks source link

Broken with express+connect+nstore-session #8

Open pkrumins opened 14 years ago

pkrumins commented 14 years ago

Here is a terrible bug in nStore. When used together with express, and nstore-session it somehow manages to create a wrong index for the database, which leads to invalid JSON exceptions being thrown.

Here is an example. This database was created by nstore-session:

{"lastAccess":1284315882938}    {"key":"df4e2e9f03e00ae7657823ff2c1a.6c069e863cf2262ec95d8cfe5dac2d47"}
{"lastAccess":1284315883048}    {"key":"df4e2e9f03e00ae7657823ff2c1a.6c069e863cf2262ec95d8cfe5dac2d47"}
    {}
{"lastAccess":1284315889592,"name":"pkrumins"}  {"key":"dccae3e52ec5ef3f584aea9df1bb.6f58e178ddc813ae35b84c665a58ec8b"}

And when used, nStore would throw the following error:

SyntaxError: Unexpected token ILLEGAL
    at Object.parse (native)
    at /usr/local/pkg/nodejs-0.2.0/lib/node/.npm/nStore/0.2.1/package/lib/nstore.js:103:27
    at /usr/local/pkg/nodejs-0.2.0/lib/node/.npm/nStore/0.2.1/package/lib/nstore.js:33:7
    at node.js:764:9

So I debugged it and located that when it requests the last key 'dccae3e52ec5ef3f584aea9df1bb.6f58e178ddc813ae35b84c665a58ec8b', the index it uses to read the record from the db file is messed up:

{ position: 279
, length: 46
, meta: { key: 'dccae3e52ec5ef3f584aea9df1bb.6f58e178ddc813ae35b84c665a58ec8b' }
}

The contents in the db at position 279 is:

4aea9df1bb.6f58e178ddc813ae35b84c665a58ec8b"}

At line 101 nStore tries to call JSON.parse(buffer.toString()); and this buffer.toString() is the data above. So it throws an exception that I listed.

Here is how to reproduce. Clone StackVM at this revision: http://github.com/substack/stackvm/commit/fa702d45234532b7bacc572e08676f4b74627cd0

Next run node ./stackvm/bin/populate.js to populate users database.

Then run node ./stackvm/server.js to start StackVM locally.

Then connect to localhost:9000 and use pkrumins as login and moo as password. That will log you into StackVM.

Now in the console you should see SyntaxError: Unexpected token ILLEGAL. Also if you now press logout, express will catch this exception and display it in the browser. But you'll also see it in the console.

I hope you can locate this bug, I spent a lot of time debugging it but could not determine what caused it. If you just simply took the database I listed there above (and replace 4 spaces between records with tabs), it would work. Doesn't work just in the web environment.

creationix commented 14 years ago

Sorry for being slow, but have you tried the new code since I merged in the rewrite?

milani commented 13 years ago

I have same problem. cloned repository at March 31. It is still buggy.