-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathobfuscatingTransformer.ts
123 lines (106 loc) · 3.32 KB
/
obfuscatingTransformer.ts
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
import * as crypto from "crypto"
import * as fs from "fs"
import * as path from "path"
import * as JavaScriptObfuscator from "javascript-obfuscator"
import { path as appRootPath } from "app-root-path"
import generate from "babel-generator"
import { getCallerFile } from "./getCallerFile"
import {
MetroTransformer,
getMetroTransformer,
MetroTransformerResult,
maybeTransformMetroResult,
} from "./getMetroTransformer"
import {
obfuscateCode,
obfuscateCodePreservingSourceMap,
} from "./obfuscateCode"
import { extendFileExtension } from "./extendFileExtension"
function getOwnCacheKey(upstreamCacheKey: string, configFilename: string) {
var key = crypto.createHash("md5")
key.update(upstreamCacheKey)
key.update(fs.readFileSync(__filename))
key.update(fs.readFileSync(configFilename))
return key.digest("hex")
}
export interface ObfuscatingTransformerOptions {
filter?(filename: string, source: string): boolean
upstreamTransformer?: MetroTransformer
obfuscatorOptions?: JavaScriptObfuscator.Options
trace?: boolean
emitObfuscatedFiles?: boolean
enableInDevelopment?: boolean
}
const sourceDir = path.join(appRootPath, "src")
export function obfuscatingTransformer({
filter = filename => filename.startsWith(sourceDir),
upstreamTransformer = getMetroTransformer(),
obfuscatorOptions: _obfuscatorOptions,
...otherOptions
}: ObfuscatingTransformerOptions): MetroTransformer {
const callerFilename = getCallerFile()
const obfuscatorOptions: JavaScriptObfuscator.Options = {
..._obfuscatorOptions,
sourceMap: true,
sourceMapMode: "separate",
stringArray: false,
}
return {
transform(props) {
const result = upstreamTransformer.transform(props)
if (props.options.dev && !otherOptions.enableInDevelopment) {
return result
}
const resultCanBeObfuscated = result.code || result.ast
if (resultCanBeObfuscated && filter(props.filename, props.src)) {
if (otherOptions.trace) {
console.log("Obfuscating", props.filename)
}
const { code, map }: MetroTransformerResult = result.code
? result
: result.ast
? (generate(result.ast, {
filename: props.filename,
retainLines: true,
sourceMaps: true,
sourceFileName: props.filename,
}) as MetroTransformerResult)
: { code: "", map: "" }
if (!code) {
return result
} else if (!map) {
return {
code: obfuscateCode(code, obfuscatorOptions),
}
}
if (otherOptions.emitObfuscatedFiles) {
const emitDir = path.dirname(props.filename)
const filename = extendFileExtension(
path.basename(props.filename),
"obfuscated",
)
fs.writeFileSync(path.join(emitDir, filename), code)
}
return maybeTransformMetroResult(
result,
obfuscateCodePreservingSourceMap(
code,
map,
props.filename,
props.src,
obfuscatorOptions,
),
)
}
return result
},
getCacheKey() {
return getOwnCacheKey(
upstreamTransformer.getCacheKey
? upstreamTransformer.getCacheKey()
: "",
callerFilename,
)
},
}
}