-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
51 lines (45 loc) · 1.41 KB
/
index.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
'use strict';
const { promisify } = require('util');
const { isUint8Array } = require('util/types');
function load() {
try {
return require('../build/Release/zstd.node');
} catch {
// Webpack will fail when just returning the require, so we need to wrap
// in a try/catch and rethrow.
/* eslint no-useless-catch: 0 */
try {
return require('../build/Debug/zstd.node');
} catch (error) {
throw error;
}
}
}
const zstd = load();
const _compress = promisify(zstd.compress);
const _decompress = promisify(zstd.decompress);
// Error objects created via napi don't have JS stacks; wrap them so .stack is present
// https://github.com/nodejs/node/issues/25318#issuecomment-451068073
exports.compress = async function compress(data, compressionLevel) {
if (!isUint8Array(data)) {
throw new TypeError(`parameter 'data' must be a Uint8Array.`);
}
if (compressionLevel != null && typeof compressionLevel !== 'number') {
throw new TypeError(`parameter 'compressionLevel' must be a number.`);
}
try {
return await _compress(data, compressionLevel ?? 3);
} catch (e) {
throw new Error(`zstd: ${e.message}`);
}
};
exports.decompress = async function decompress(data) {
if (!isUint8Array(data)) {
throw new TypeError(`parameter 'data' must be a Uint8Array.`);
}
try {
return await _decompress(data);
} catch (e) {
throw new Error(`zstd: ${e.message}`);
}
};