Sannis / node-ubjson

Universal Binary JSON packer/unpacker for Node.js
http://sannis.github.com/node-ubjson
Other
35 stars 5 forks source link

`packToBuffer` without predetermined size #32

Open jbenet opened 9 years ago

jbenet commented 9 years ago

sometimes one needs to encode documents without having a pre-allocated buffer, or without knowing how big the object will be. currently, ubjson throws if the buffer is too small:

> buf1 = new Buffer(512)
<Buffer 00 00 04 00 ff ff ff ff 80 eb 09 01 01 00 00 00 a0 8a 9c 00 01 00 00 00 56 04 00 00 ff ff ff ff 01 00 00 00 00 00 00 00 ff ff ff ff 00 00 00 00 00 00 ... >
> buf2 = new Buffer(32)
<Buffer 00 00 00 00 00 00 00 00 60 76 9c 00 01 00 00 00 82 04 00 00 ff ff ff ff 01 00 00 00 00 00 00 00>
> ubjson.packToBufferSync(doc, buf1)
483
> ubjson.packToBufferSync(doc, buf2)
TypeError: flush is not a function
    at maybeFlush (/Users/jbenet/git/node-ubjson-bin/node_modules/ubjson/lib/strtok-extend.js:18:5)
    at self.put (/Users/jbenet/git/node-ubjson-bin/node_modules/ubjson/lib/strtok-extend.js:125:16)
    at ubjsonPack (/Users/jbenet/git/node-ubjson-bin/node_modules/ubjson/lib/ubjson-pack.js:103:25)
    at /Users/jbenet/git/node-ubjson-bin/node_modules/ubjson/lib/ubjson-pack.js:139:18
    at Array.forEach (native)
    at ubjsonPack (/Users/jbenet/git/node-ubjson-bin/node_modules/ubjson/lib/ubjson-pack.js:138:12)
    at packToBufferWithOffsetSync (/Users/jbenet/git/node-ubjson-bin/node_modules/ubjson/lib/ubjson.js:94:10)
    at Object.packToBufferSync (/Users/jbenet/git/node-ubjson-bin/node_modules/ubjson/lib/ubjson.js:102:10)
    at repl:1:8
    at REPLServer.defaultEval (repl.js:155:27)

should have equivalents to JSON.stringify and JSON.parse for simple usage

vphantom commented 8 years ago

Indeed, how are we supposed to know the size of the buffer to allocate? (Since NodeJS apparently refuses to implement an automatic realloc() on appends.) The safest way I can think of would be to discover the JSON version's size, which would always be larger (unnecessarily so):

var buf = new Buffer(JSON.stringify(object).length);

...but that incurs a considerable performance penalty of course. The cleaner solution would be for ubjson to create whatever buffer it needs, like most other modules do for this kind of thing.