forked from kesla/node-snappy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnappy.js
57 lines (43 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
55
56
57
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 (!Buffer.isBuffer(compressed))
return callback(new Error('input must be a Buffer'))
if (!callback) {
callback = opts
opts = {}
}
if (typeof(opts.asBuffer) !== 'boolean')
opts.asBuffer = true
binding.uncompress(compressed, opts, callback)
}
exports.uncompressSync = function (compressed, opts) {
assert(Buffer.isBuffer(compressed), 'input must be a Buffer');
opts = opts || {};
if (typeof(opts.asBuffer) !== 'boolean')
opts.asBuffer = true
return binding.uncompressSync(compressed, opts)
}