1

I'm trying to test if text is compressed via Snappy. I'm using the Node.JS https://github.com/kesla/node-snappy library.

credit to this answer for help on reading from a file.

$cat Decompress.js 
var snappy = require('snappy');
var fs     = require('fs');

var filename = './snappy_compressed_docs/snappy_compressed_file'

fs.readFile(filename, 'utf8', function(err, data) {
    if (err) throw err;    
    console.log("done printing data. 'typeof data':", typeof data);

    console.log("snappy: ", snappy);

        snappy.isValidCompressed(data, function(e, result) {
        if(e) { console.log("error!", e); throw e; }
            console.log("snappy.isValidCompressed:", result);
    });
});

Here's what I'm getting:

$node Decompress.js 
done printing data. 'typeof data': string
snappy:  { compress: [Function],
  isValidCompressed: [Function: isValidCompressed],
  uncompress: [Function] }
Assertion failed: (obj->HasIndexedPropertiesInExternalArrayData()),
 function Length, file ../src/node_buffer.cc, line 115.
Abort trap: 6

What's going on in the Assertion failed... part?

Community
  • 1
  • 1
Kevin Meredith
  • 41,036
  • 63
  • 209
  • 384

1 Answers1

3

The issue here is that isValidCompressed expects a buffer, rather than a string.

Based on the assertion that failed, we can see that we're looking for weird properties in “external” array data, which means something somewhere is expecting something more raw than a JavaScript String. If we look at node-snappy's isValidCompressed bindings, we see it's treating its input data as a node::Buffer. Since the typeof data is giving us string, we probably don't have the right type of data.

The solution here is to drop the 'utf8' argument to readFile, which gives us the data as a raw buffer instead of trying to convert it into a string.