This repository has been archived by the owner on May 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathutils.js
197 lines (166 loc) · 3.59 KB
/
utils.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
'use strict';
var uglifyjs = require('uglify-js');
var merge = require('util')._extend;
/**
* Create an `AST` from the given `js`, invoking `cb(err, ast)`
*
* @api private
* @param {String} js
* @param {Function} cb
*/
exports.ast = function (js, cb) {
try {
var ast = uglifyjs.parse(js);
cb(null, ast);
} catch (err) {
var e = new Error(err.message);
// cheap hack to use actual errors
// rather than the indecipherable JS_Parse_Error garbage
merge(e, err);
// expose the bad js
e.source = js;
cb(e);
}
};
/**
* Compress the given `ast`, conditionally using `opts`
*
* @api private
* @param {Object} [opts]
* @return {AST}
*/
exports.compress = function (ast, opts) {
opts = opts || exports.compress.defaults;
var compressor = uglifyjs.Compressor(opts);
// for some stupid reason, this is the
// only non-modifier method...
return ast.transform(compressor);
};
/**
* Default compression options
*
* @api private
* @type {Object}
*/
exports.compress.defaults = {
sequences: true,
properties: true,
dead_code: true,
drop_debugger: true,
unsafe: true,
conditionals: true,
comparisons: true,
evaluate: true,
booleans: true,
loops: true,
unused: true,
hoist_funs: true,
hoist_vars: true,
if_return: true,
join_vars: true,
cascade: true,
warnings: false
};
/**
* Uglify the given `js` with `opts`
*
* @api private
* @param {String} js
* @param {Object} [opts]
* @param {Function} cb
*/
exports.uglify = function (js, opts, cb) {
/**
* Handle mangling and compression of the generated `AST`
*
* @api private
* @param {Error} err
* @param {AST} ast
*/
function handleAST(err, ast) {
if (err) return cb(err);
var stream = new uglifyjs.OutputStream;
ast.figure_out_scope();
ast.mangle_names();
ast = exports.compress(ast, opts.compressor);
if (opts.strings) {
ast = mangleStrings(ast);
// disable uglify's string escaping to prevent
// double escaping our hex
stream.print_string = function (str) {
return this.print('"' + str + '"');
};
}
ast.print(stream);
return cb(null, stream.toString());
}
if (typeof opts === 'function') {
cb = opts;
opts = {};
}
// build the AST
exports.ast(js, handleAST);
};
/**
* Escape map.
*/
var map = {
'\b': '\\b',
'\f': '\\f',
'\n': '\\n',
'\r': '\\r',
'\t': '\\t',
'\\': '\\\\'
};
/**
* Convert (or _obfuscate_) a string to its escaped
* hexidecimal representation. For example,
* `hex('a')` will return `'\x63'`.
*
* @api public
* @name obfuscator.utils.hex
* @param {String} str
* @return {String}
*/
exports.hex = function (str) {
var result = '';
for (var i = 0, l = str.length; i < l; i++) {
var char = str[i];
if (map[char]) {
result += map[char];
} else if ('\\' == char) {
result += '\\' + str[++i];
} else {
result += '\\x' + str.charCodeAt(i).toString(16);
}
}
return result;
};
/**
* Mangle strings contained in the given `ast`.
*
* @api private
* @param {AST} ast
* @return {AST} mangled ast
*/
function mangleStrings(ast) {
var transformer = new uglifyjs.TreeTransformer(null, mangleString);
return ast.transform(transformer);
}
/**
* Mangle the given `node`, assuming it's an `AST_String`.
*
* @api private
* @param {AST_Node} node
* @return {AST_Node}
*/
function mangleString(node) {
if (!(node instanceof uglifyjs.AST_String)) {
return;
}
var str = node.getValue();
var hex = exports.hex(str);
var obj = merge({}, node);
obj.value = hex;
return new uglifyjs.AST_String(obj);
}