This repository has been archived by the owner on Dec 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathrawData.js
95 lines (83 loc) · 2.61 KB
/
rawData.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
var crypto = require("./crypto");
var fs = require("fs");
const DATA_PATH = "encrypted";
// Make sure the directory exists
fs.mkdir(DATA_PATH, 0750);
/**
* Calculate the total number of bytes in all raw data files
*
* @param onSize: Callback that gets the number of bytes of all files
*/
exports.getTotalSize = function(onSize) {
fs.readdir(DATA_PATH, function(error, files) {
var total = 0;
// Get the next file in a closure that recursively calls itself
(function getNextFile() {
// Trigger the callback with the total size when we're done
if (files.length == 0)
return onSize(total);
// Grab the next file and update the remaining files
var file = DATA_PATH + "/" + files.shift();
fs.stat(file, function(error, stats) {
total += stats.size;
getNextFile();
});
})();
});
};
/**
* Read all raw data files
*
* @param onFile: Callback with 1 parameter with the file data
* Callback must return true to get the next file
*/
exports.readAll = function(onFile) {
fs.readdir(DATA_PATH, function(error, files) {
// Get the next file in a closure that recursively calls itself
(function getNextFile() {
// Trigger the callback with nothing if there's no files
if (files.length == 0)
return onFile();
// Grab the next file and update the remaining files
var file = DATA_PATH + "/" + files.shift();
fs.readFile(file, "utf8", function(error, blob) {
crypto.decrypt(blob, function(json) {
// Give the callback a js object; grab more files when it returns true
if (onFile(JSON.parse(json)))
getNextFile();
});
});
})();
});
};
/**
* Save a data array to disk
*
* @param data: Array of data arrays [url, timestamp, sessionid]
*/
exports.save = function(data) {
try {
// Only accept data that we're expecting
data = data.filter(function(entry) {
return entry.length == 3 &&
typeof entry[0] == "string" &&
typeof entry[1] == "number" &&
typeof entry[2] == "number";
});
// Don't bother writing nothing to disk
if (data.length == 0)
return false;
// Save the data to disk as JSON
var file = DATA_PATH + "/" + Date.now();
crypto.encrypt(JSON.stringify(data), function(blob) {
fs.writeFile(file, blob);
});
return true;
}
catch(ex) {
return false;
}
};