-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelper.js
More file actions
130 lines (112 loc) · 4.03 KB
/
helper.js
File metadata and controls
130 lines (112 loc) · 4.03 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import Crypto from 'node:crypto';
import fs from 'node:fs';
import { mkdirp } from 'mkdirp';
import {rimraf} from 'rimraf';
const secret = process.env.SECRET;
const sigLength = parseInt(process.env.SIG_LENGTH);
const expiry = parseInt(process.env.EXPIRY);
const dbRoot = process.env.DBROOT;
const dir = {
manyToOne: dbRoot + "/manyToOne/",
oneToMany: dbRoot + "/oneToMany/",
oneToOne: dbRoot + "/oneToOne/",
tmp: dbRoot + "/tmp/"
}
function hash(str){
return Crypto.hash('sha256', str, 'base64url'); // For small size str this is faster than fs.createHash()
}
function sign(str){
// Note: https://nodejs.org/api/crypto.html#using-strings-as-inputs-to-cryptographic-apis
return Crypto.createHmac('sha256', secret).update(str).digest('base64url').substr(0,sigLength);
}
export function validate(key){
const sig = key.substr(0, sigLength);
const hash = key.substr(sigLength,);
if (sig === sign(hash + 'public')){
return 'public';
} else if (sig === sign(hash + 'private')){
return 'private';
} else {
return false;
}
}
export function genPublicKey(privateKey){
const privateHash = privateKey.substr(sigLength,);
const publicHash = hash(privateHash);
const publicKey = sign(publicHash + 'public') + publicHash;
return publicKey
}
export function genKeyPair(seed = Crypto.randomUUID()){
const privateHash = hash(seed);
const privateKey = sign(privateHash + 'private') + privateHash;
const publicKey = genPublicKey(privateKey);
return {private: privateKey, public: publicKey};
}
export function setupDB(){
for (const key in dir) {
mkdirp.sync(dir[key]);
}
}
export function publicProduce(publicKey, data){
const destDir = dir.manyToOne + publicKey + '/';
const uuid = Crypto.randomUUID();
const tmpfile = dir.tmp + uuid;
fs.writeFileSync(tmpfile, data, {flush: true});
mkdirp.sync(destDir);
fs.renameSync(tmpfile, destDir + uuid);
}
export function privateConsume(privateKey){
const publicKey = genPublicKey(privateKey);
const srcDir = dir.manyToOne + publicKey + '/';
if (!fs.existsSync(srcDir)) return [];
let aggregatedDataAsArray = [];
for (const file of fs.readdirSync(srcDir)) {
const data = fs.readFileSync(srcDir + file, 'utf8');
aggregatedDataAsArray.push(data);
fs.unlinkSync(srcDir + file);
}
return aggregatedDataAsArray;
}
export function privateProduce(privateKey, data){
const publicKey = genPublicKey(privateKey);
const tmpfile = dir.tmp + Crypto.randomUUID();
fs.writeFileSync(tmpfile, data, {flush: true});
fs.renameSync(tmpfile, dir.oneToMany + publicKey);
}
export function publicConsume(publicKey){
const srcFile = dir.oneToMany + publicKey;
if (!fs.existsSync(srcFile)) return;
return fs.readFileSync(srcFile, 'utf8');
}
export function oneToOneProduce(privateKey, key, data){
const publicKey = genPublicKey(privateKey);
const destDir = dir.oneToOne + publicKey + '/';
mkdirp.sync(destDir);
const tmpfile = dir.tmp + Crypto.randomUUID();
fs.writeFileSync(tmpfile, data, {flush: true});
fs.renameSync(tmpfile, destDir + hash(key));
}
export function oneToOneConsume(publicKey, key){
const srcFile = dir.oneToOne + publicKey + '/' + hash(key)
if (!fs.existsSync(srcFile)) return;
const data = fs.readFileSync(srcFile, 'utf8');
fs.unlinkSync(srcFile);
return data;
}
export function oneToOneIsConsumed(privateKey, key){
const publicKey = genPublicKey(privateKey);
const srcFile = dir.oneToOne + publicKey + '/' + hash(key);
return !fs.existsSync(srcFile);
}
function isExpired(path){
const age = (new Date().getTime() - fs.statSync(path).mtime) / 1000;
return age > expiry;
}
function gcIn(dir){
rimraf(dir + '/**/*', {glob: true, filter: isExpired}).then((val) => {console.log(`Garbage cleaned in ${dir}`);}, (err) => {console.log(err.data);});
}
export function gc(){
for (const key in dir) {
gcIn(dir[key]);
}
}