-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecode_entry.js
More file actions
122 lines (111 loc) · 3.83 KB
/
decode_entry.js
File metadata and controls
122 lines (111 loc) · 3.83 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
const { Worker } = require('worker_threads');
const fs = require('fs');
const util = require('util');
const readdir = util.promisify(fs.readdir);
const lstat = util.promisify(fs.lstat);
function runWorkerWithTimeout(workerPath, workerData, timeoutDuration) {
return new Promise((resolve, reject) => {
const worker = new Worker(workerPath, { workerData });
// Handle worker messages
worker.on('message', resolve);
// Handle worker errors
worker.on('error', reject);
// Handle worker exit
worker.on('exit', (code) => {
if (code !== 0) {
reject(new Error(`Worker stopped with exit code ${code}`));
}
});
// Set up timeout
const timeoutId = setTimeout(() => {
console.log('Worker timed out, terminating...');
worker.terminate();
reject(new Error('Worker operation timed out'));
}, timeoutDuration);
// Clear timeout if worker finishes in time
worker.on('exit', () => {
clearTimeout(timeoutId);
});
});
}
async function decodeFileOrDir(inputPath) {
console.log(`start decoding inputPath: ${inputPath}`);
const stats = await lstat(inputPath);
if (stats.isDirectory()) {
const paths = await readdir(inputPath);
const promises = paths.sort().map(async (path) => {
const fileFullPath = `${inputPath}/${path}`;
const fileStats = await lstat(fileFullPath);
if (path.endsWith('.xlog')) {
console.log(`handling file: ${fileFullPath}`);
return Promise.race([
runWorkerWithTimeout('./lib/decode_xlog_nocrypt.js', fileFullPath, 10000),
]);
} else if (fileStats.isDirectory()) {
return decodeFileOrDir(fileFullPath);
} else {
console.log(`ignore unsupported file : ${path}`);
}
});
await Promise.all(promises);
} else {
// single file
await Promise.race([
runWorkerWithTimeout('./lib/decode_xlog_nocrypt.js', inputPath, 10000),
]);
}
}
async function decodeEncryptedFileOrDir(inputPath, privateKey) {
console.log(`start decoding inputPath: ${inputPath}`);
console.log(`private key = ${privateKey}`);
const stats = await lstat(inputPath);
if (stats.isDirectory()) {
const paths = await readdir(inputPath);
const promises = paths.sort().map(async (path) => {
const fileFullPath = `${inputPath}/${path}`;
const fileStats = await lstat(fileFullPath);
if (path.endsWith('.xlog')) {
console.log(`handling file: ${fileFullPath}`);
return Promise.race([
runWorkerWithTimeout('./lib/decode_xlog_crypt.js', { inputPath: fileFullPath, privateKey }, 30000),
]);
} else if (fileStats.isDirectory()) {
return decodeEncryptedFileOrDir(fileFullPath, privateKey);
} else {
console.log(`ignore unsupported file : ${path}`);
}
});
await Promise.all(promises);
} else {
// single file
await Promise.race([
runWorkerWithTimeout('./lib/decode_xlog_crypt.js', { inputPath, privateKey }, 30000),
]);
}
}
module.exports = { decodeFileOrDir, decodeEncryptedFileOrDir };
// // test code
// const { Signals } = require('node:process');
//
// const args = process.argv.slice(2);
// if (args.length < 1) {
// console.error('Error: path param not found!');
// process.exit(-1);
// }
// process.addListener(Signals, (signal) => {
// console.log(`signal - ${signal}`);
// });
// process.addListener('uncaughtException', (e) => {
// console.log(`uncaughtException - ${e.message}`);
// });
// process.addListener('beforeExit', (code) => {
// console.log(`beforeExit - ${code}`);
// });
//
// console.log('Path list is:', args);
// // decodeFileOrDir(args[0]).then(() => console.log('process end!!!!')).catch((e) => {
// // console.log(e);
// // });
// decodeEncryptedFileOrDir(args[0], args[1]).then(() => console.log('process end!!!!')).catch((e) => {
// console.log(e);
// });