forked from streamergy/natsFS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync.js
173 lines (144 loc) · 4.71 KB
/
sync.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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import nats from 'nats';
import fs, { ReadStream } from 'node:fs';
import process from 'node:process';
import crypto from 'crypto';
import { ArgumentParser } from 'argparse';
const parser = new ArgumentParser({
// name: 'NATS Filesystem Sync',
description: 'Sync objects from a NATS object store to your filesystem'
});
parser.add_argument('-u', '--host', { help: 'NATS broker host. Default "localhost:4222"'})
parser.add_argument('-t', '--token', { help: 'NATS broker token' })
parser.add_argument('-b', '--bucket', { help: 'NATS object bucket name' })
parser.add_argument('-m', '--mount', { help: 'Folder to sync objects to' })
parser.add_argument('-c', '--config', { help: 'Path to config file. CLI options override options from the file' })
parser.add_argument('-1', '--once', { help: 'Only run sync once, don\'t listen for NATS updates' })
let args = parser.parse_args();
if(args.config){
const config = JSON.parse(fs.readFileSync(args.config));
// undefined vars from args override
for(const key in config){
if(args[key] === undefined){
args[key] = config[key];
}
}
}
if((!args.bucket) || (!args.mount)) {
console.log('sync.js: error: the following arguments are required: -b/--bucket, -m/--mount');
process.exit(1);
}
if(!args.host){
args.host = 'localhost:4222';
}
process.chdir(args.mount);
const connection = await nats.connect({ servers: args.host, token: args.token });
const jetStream = connection.jetstream();
const objectBucket = await jetStream.views.os(args.bucket);
const slashReplace = /^\/*/g;
async function pipeStream(readStream, writableStream) {
const reader = readStream.getReader();
return await new Promise((resolve, reject) => {
let totalSize = 0;
reader.read().then(function processText({ done, value }) {
if(done){
writableStream.end();
resolve(totalSize);
return;
}
writableStream.write(value);
totalSize += value.length;
reader.read().then(processText);
});
});
}
async function callFsFunction(fnct, ...args) {
await new Promise((resolve, reject) => {
fnct(...args, (err, data) => {
if(err) reject(err);
resolve(data);
});
});
}
async function downloadFile(path){
path = path.replace(slashReplace, '');
const parts = path.split('/');
let currentPath = '';
for(const part of parts.slice(0, -1)) {
currentPath += `${part}/`;
try{
const stats = fs.statSync(currentPath);
if(stats.isDirectory()) {
continue;
}
} catch(e){
if(e.code !== 'ENOENT'){
throw e;
}
}
fs.mkdirSync(currentPath);
}
const natsHandle = await objectBucket.get(path);
return await pipeStream(natsHandle.data, fs.createWriteStream(path));
}
async function getHash(path, algorithm) {
return new Promise((resolve, reject) => {
const hash = crypto.createHash(algorithm.replace('-', ''));
const rs = fs.createReadStream(path);
rs.on('error', reject);
rs.on('data', chunk => hash.update(chunk));
rs.on('end', () => {
resolve(hash.digest('base64'))
});
})
}
async function syncFile(path, data){
if(data === undefined){
data = await objectBucket.info(path);
}
if (data.deleted) {
process.stdout.write(`${path}: deleting file...`);
await callFsFunction(fs.unlink, path);
console.log('done');
return;
}
try{
// const stats = await callFsFunction(fs.stat, path);
let natsDigest = data.digest;
const algorithm = natsDigest.split('=')[0];
natsDigest = data
.digest
.substring(algorithm.length + 1)
.replaceAll('_', '/')
.replaceAll('-', '+');
const diskDigest = await getHash(path, algorithm);
if (natsDigest === diskDigest) {
// file is newest version
console.log(`${path}: already newest version`);
return;
}
}catch(e){
if(e.code !== 'ENOENT'){
throw e;
}
}
process.stdout.write(`${path}: downloading file...`);
const size = await downloadFile(path);
console.log(`done (${size}B)`)
}
async function syncAllFiles() {
const files = await objectBucket.list();
for(const object of files){
await syncFile(object.name, object);
}
}
await syncAllFiles();
if(args.once){
process.exit(0);
}
const watch = await objectBucket.watch();
for await(const update of watch){
if(!update) {
continue;
}
await syncFile(update.name, update);
}