forked from kesla/node-snappy
-
Notifications
You must be signed in to change notification settings - Fork 3
/
snappy.js
54 lines (42 loc) · 1.5 KB
/
snappy.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
var binding = require('bindings')('binding');
var assert = require('assert');
/**
* Compress asyncronous.
* If input isn't a string or buffer, automatically convert to buffer by using
* JSON.stringify.
*/
exports.compress = function (input, callback) {
if (!(typeof (input) === 'string' || Buffer.isBuffer(input))) {
return callback(new Error('input must be a String or a Buffer'));
}
binding.compress(input, callback);
};
exports.compressSync = function (input) {
assert(typeof (input) === 'string' || Buffer.isBuffer(input), 'input must be a String or a Buffer');
return binding.compressSync(input);
};
/**
* Asyncronous decide if a buffer is compressed in a correct way.
*/
exports.isValidCompressed = binding.isValidCompressed;
exports.isValidCompressedSync = binding.isValidCompressedSync;
/**
* Asyncronous uncompress previously compressed data.
* A parser can be attached. If no parser is attached, return buffer.
*/
exports.uncompress = function (compressed, opts, callback) {
if (!callback) {
callback = opts;
}
if (!Buffer.isBuffer(compressed)) {
return callback(new Error('input must be a Buffer'));
}
binding.uncompress(compressed, uncompressOpts(opts), callback);
};
exports.uncompressSync = function (compressed, opts) {
assert(Buffer.isBuffer(compressed), 'input must be a Buffer');
return binding.uncompressSync(compressed, uncompressOpts(opts));
};
function uncompressOpts (opts) {
return (opts && typeof opts.asBuffer === 'boolean') ? opts : {asBuffer: true};
}