foliojs / brotli.js

A JavaScript port of the Brotli compression algorithm, as used in WOFF2
500 stars 51 forks source link

Compress / decompress output should return Buffer object #13

Closed miherlosev closed 7 years ago

miherlosev commented 7 years ago

I have a code snippet:

var data = new Buffer('This is a string');
var compressed;
var decompressed;

 var brotli = require("brotli");
 compressed = brotli.compress(data);
 decompressed = brotli.decompress(compressed);

 console.log('brotli:');
 console.log(JSON.stringify(decompressed));
 console.log(decompressed.toString());

 var iltorb = require('iltorb');
 compressed = iltorb.compressSync(data);
 decompressed = iltorb.decompressSync(compressed);
 console.log('iltorb:');
 console.log(JSON.stringify(decompressed));
 console.log(decompressed.toString());

Then I run this code the I get output:

brotli:
{"0":84,"1":104,"2":105,"3":115,"4":32,"5":105,"6":115,"7":32,"8":97,"9":32,"10":115,"11":116,"12":114,"13":105,"14":110,"15":103}
84,104,105,115,32,105,115,32,97,32,115,116,114,105,110,103
iltorb:
{"type":"Buffer","data":[84,104,105,115,32,105,115,32,97,32,115,116,114,105,110,103]}
This is a string

If I change code to:

var brotli = require("brotli");
 compressed = brotli.compress(data);
 decompressed = brotli.decompress(compressed);

 decompressed = new Buffer(decompressed);
 console.log('brotli:');
 console.log(JSON.stringify(decompressed));
 console.log(decompressed.toString());

>brotli:
>{"type":"Buffer","data":[84,104,105,115,32,105,115,32,97,32,115,116,114,105,110,103]}
>This is a string

then all works right

ohroy commented 7 years ago

Yes, I think so.

devongovett commented 7 years ago

Yes, brotli returns Uint8Array instead of Buffer because the browser buffer library is quite large. If you want a Buffer, you can wrap it yourself as in your second example.