-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
97 lines (66 loc) · 1.8 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
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
'use strict';
const fs = require('fs');
// const sourceFile = './input.js';
const sourceFile = process.argv[2];
const UglifyJS = require('uglify-js');
const walk = require('esprima-walk')
const crypto = require('crypto');
const escodegen = require('escodegen');
let source;
try {
source = fs.readFileSync(sourceFile, 'utf8');
} catch (error) {
}
const esprima = require("esprima");
const { exit } = require('process');
let tree;
try {
tree = esprima.parseScript(source);
} catch (error) {
}
// console.log(JSON.stringify(tree, null, 2))
if (!tree) {
process.exit(1);
}
walk(tree, (node) => {
if (node && (node.type === 'ArrowFunctionExpression' || node.type === 'FunctionExpression')) {
const z = {
"type": "VariableDeclaration",
"declarations": [
{
"type": "VariableDeclarator",
"id": {
"type": "Identifier",
"name": "z"
},
"init": node
}
],
"kind": "const"
};
printFn(z);
};
if (node && node.type === 'FunctionDeclaration') {
let name;
if (node && node.id && node.id.name) {
name = node.id.name;
node.id.name = 'MORK';
}
printFn(node, name);
};
});
function printFn(a, name = null) {
const b = escodegen.generate({
"type": "Program",
"body": [a],
"sourceType": "script"
})
const code = UglifyJS.minify(b);
const hash = crypto.createHash('md5').update(code.code).digest('hex');
const id = crypto.randomBytes(16).toString("hex");
fs.writeFile(`./functions/${hash}.js`, code.code, () => {});
if(name) {
fs.appendFileSync(`./functions/${hash}-names.txt`, `\n// ${name}`);
}
console.log(`${id},${sourceFile},${hash}`);
}