marcello3d/node-buffalo — pull request #15 by dptole (2014-08-21 20:36:02+00:00)
Title: Encode/decode int64 properly.

After a while I realized that int32+ were encoded and decoded as floats64, so if you `serialize` `(0)` you will get something like `(1)`:
```javascript
// (0)
buffalo.serialize({ t: +new Date });
// (1)
//
```

The type `01` is meant for double 8 bytes (64-bit IEEE 754 floating point), so the code is saying "I'm a double." when its abviously not.
Although the final result isn't wrong, it's not right also. The problem is that JavaScript just can't handle int32+ with binary operators. So, codes like this `(2)` must be translated to something like this `(3)` because of this limitation.
```javascript
// (2)
value = (buffer[offset++]) |
(buffer[offset++] << 8) |
(buffer[offset++] << 16) |
(buffer[offset++] << 24) |
(buffer[offset++] << 32) |
(buffer[offset++] << 40) |
(buffer[offset++] << 48) |
(buffer[offset++] << 56) |
(buffer[offset++] << 64)

// (3)
for(var i = 8; i; i--) {
buffer[offset++] = num & 0xFF
num /= 256
}
```

In the code below you will find the translate function at `exports.writeInt8`, so you receive a int32+ and write this value as a int64 in the buffer.
To translate back I did kinda the same trick in the function `readInt` adding the `bytes` argument, which is 4 for default. When when you call it as `readInt(8)` it reads the next 8 bytes as a int64.