-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
51 lines (46 loc) · 1.29 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
//@ts-check
const binding = require('./binding');
/**
* Throw a reflink error.
* @param {binding.ReflinkError} reflinkError Error properties.
* @returns {Error & binding.ReflinkError}
*/
function createReflinkError(reflinkError) {
const error = new Error(reflinkError.message)
return Object.assign(error, {
path: reflinkError.path,
dest: reflinkError.dest,
code: reflinkError.code,
errno: reflinkError.errno,
});
}
/**
* Either throw the reflink error or return the number.
* @param {number | binding.ReflinkError} result Result of the binding function.
* @returns {number}
*/
function handleReflinkResult(result) {
if (typeof result === 'number') {
return result;
} else {
throw createReflinkError(result);
}
}
/**
* Create a reflink asynchronously.
* @param {String} src Source of the reflink.
* @param {String} dst Target of the reflink.
* @returns {Promise.<number>}
*/
const reflinkFile = (src, dst) => binding.reflinkFile(src, dst).then(handleReflinkResult);
/**
* Create a reflink asynchronously.
* @param {String} src Source of the reflink.
* @param {String} dst Target of the reflink.
* @returns {number}
*/
const reflinkFileSync = (src, dst) => handleReflinkResult(binding.reflinkFileSync(src, dst));
module.exports = {
reflinkFile,
reflinkFileSync,
};