-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
62 lines (56 loc) · 1.62 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
52
53
54
55
56
57
58
59
60
61
62
const fs = require('fs');
const Stream = require('stream');
const silk = require('bindings')('silk.node');
function getStreamTransform(api, options) {
let buffer = Buffer.alloc(0);
return new Stream.Transform({
transform(chunk, encoding, callback) {
buffer = Buffer.concat([buffer, chunk]);
callback();
},
flush(callback) {
try {
const output = api(buffer, options);
callback(null, output);
} catch (e) {
callback(e);
}
},
});
}
function getBuffer(input) {
if (typeof input === 'string') return fs.readFileSync(input);
if (input instanceof Buffer) return input;
throw new Error('should be either string of filename or buffer of file content');
}
function getWrappedApi(api) {
return function (input, output, options) {
if (arguments.length === 0) {
return getStreamTransform(api, { });
} else if (arguments.length === 1) {
if (typeof input === 'object' && !(input instanceof Buffer)) {
return getStreamTransform(api, input || { });
}
return api(getBuffer(input));
}
const inBuffer = getBuffer(input);
if (typeof output === 'string') {
const outBuffer = api(inBuffer, options || { });
fs.writeFileSync(output, outBuffer);
} else {
return api(inBuffer, output || { });
}
};
}
const decode = getWrappedApi(silk.decode);
const encode = getWrappedApi(silk.encode);
function compare(ref, test, options = { }) {
const refBuffer = getBuffer(ref);
const testBuffer = getBuffer(test);
return silk.compare(refBuffer, testBuffer, options);
}
module.exports = {
decode,
encode,
compare,
};