-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
41 lines (38 loc) · 1.08 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
const bindings = require("./build/Release/iou.node");
const fs = require("fs");
function readFile(path, cb) {
if (typeof cb !== "function") {
throw new TypeError("Callback must be a function");
}
fs.open(path, "r", 0o666, (err, fd) => {
if (err) return cb(err);
const onerr = err => fs.close(fd, () => cb(err));
fs.fstat(fd, (err, stats) => {
if (err) return onerr(err);
const dst = Buffer.allocUnsafe(stats.size);
bindings.read(fd, dst, 0, stats.size, 0, (err, bytesRead, data) => {
if (err) return onerr(err);
fs.close(fd, err => cb(err, data));
});
});
});
}
function writeFile(path, data, cb) {
if (typeof cb !== "function") {
throw new TypeError("Callback must be a function");
}
fs.open(path, "w", 0o666, (err, fd) => {
if (err) return cb(err);
const onerr = err => fs.close(fd, () => cb(err));
bindings.writeBuffer(fd, data, 0, data.byteLength, 0, (err, bytesWritten, data) => {
if (err) return onerr(err);
fs.close(fd, err => cb(err));
});
});
}
module.exports = {
readFile,
writeFile,
read: bindings.read,
write: bindings.writeBuffer
};