javascript-obfuscator-lite 0.0.0-stage → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/utils.js ADDED
@@ -0,0 +1,452 @@
1
+ 'use strict';
2
+
3
+ const t = require('@babel/types');
4
+
5
+ const PROPERTY_CONTAINERS = new Set([
6
+ 'ObjectProperty',
7
+ 'ObjectMethod',
8
+ 'ClassMethod',
9
+ 'ClassPrivateMethod',
10
+ 'ClassProperty',
11
+ 'ClassPrivateProperty',
12
+ 'ClassAccessorProperty',
13
+ ]);
14
+
15
+ const GENERATED = Symbol('javascript-obfuscator-lite.generated');
16
+
17
+ /** Mark an AST node as generated by the obfuscator itself. */
18
+ function markGenerated(node) {
19
+ if (node && typeof node === 'object') {
20
+ node[GENERATED] = true;
21
+ }
22
+ return node;
23
+ }
24
+
25
+ /** Return true when an AST node was generated by the obfuscator itself. */
26
+ function isGenerated(node) {
27
+ return Boolean(node && node[GENERATED]);
28
+ }
29
+
30
+ const FUNCTION_LIKE = new Set([
31
+ 'FunctionExpression',
32
+ 'ArrowFunctionExpression',
33
+ 'ObjectMethod',
34
+ 'ClassMethod',
35
+ 'ClassPrivateMethod',
36
+ ]);
37
+
38
+ /**
39
+ * Walk a Babel AST node recursively without creating a traversal context.
40
+ * Returning `false` from the visitor skips the node's children.
41
+ */
42
+ function walk(node, visit) {
43
+ if (!node || typeof node.type !== 'string') {
44
+ return;
45
+ }
46
+ if (visit(node) === false) {
47
+ return;
48
+ }
49
+ const keys = t.VISITOR_KEYS[node.type] || [];
50
+ for (const key of keys) {
51
+ const child = node[key];
52
+ if (Array.isArray(child)) {
53
+ for (const item of child) {
54
+ walk(item, visit);
55
+ }
56
+ } else {
57
+ walk(child, visit);
58
+ }
59
+ }
60
+ }
61
+
62
+ /** Return true when the node is the key of an object/class member. */
63
+ function isPropertyKeyPath(path) {
64
+ const parent = path.parentPath && path.parentPath.node;
65
+ return Boolean(parent && path.key === 'key' && PROPERTY_CONTAINERS.has(parent.type));
66
+ }
67
+
68
+ /**
69
+ * Return true when replacing a StringLiteral at `path` with an arbitrary
70
+ * expression keeps the program syntactically valid.
71
+ */
72
+ function isSafeStringExpression(path) {
73
+ const parent = path.parentPath && path.parentPath.node;
74
+ if (!parent) {
75
+ return false;
76
+ }
77
+
78
+ if (isPropertyKeyPath(path)) {
79
+ return false;
80
+ }
81
+
82
+ switch (parent.type) {
83
+ case 'ImportDeclaration':
84
+ case 'ExportNamedDeclaration':
85
+ case 'ExportAllDeclaration':
86
+ // Module specifiers must remain string literals.
87
+ return path.key !== 'source';
88
+ case 'ImportSpecifier':
89
+ case 'ExportSpecifier':
90
+ case 'ImportAttribute':
91
+ return false;
92
+ case 'JSXAttribute':
93
+ return path.key !== 'value';
94
+ case 'MemberExpression':
95
+ case 'OptionalMemberExpression':
96
+ // A non-computed property must be an identifier. Computed properties
97
+ // (obj["x"]) are safe to replace with an expression.
98
+ return parent.computed || path.key !== 'property';
99
+ default:
100
+ return true;
101
+ }
102
+ }
103
+
104
+ /** Return true when replacing a NumericLiteral at `path` is syntactically safe. */
105
+ function isSafeNumericExpression(path) {
106
+ const parent = path.parentPath && path.parentPath.node;
107
+ if (!parent) {
108
+ return false;
109
+ }
110
+ if (isPropertyKeyPath(path)) {
111
+ return false;
112
+ }
113
+ if (parent.type === 'ImportAttribute') {
114
+ return false;
115
+ }
116
+ if (
117
+ (parent.type === 'MemberExpression' || parent.type === 'OptionalMemberExpression') &&
118
+ path.key === 'property' &&
119
+ !parent.computed
120
+ ) {
121
+ return false;
122
+ }
123
+ if (parent.type === 'JSXAttribute' && path.key === 'value') {
124
+ return false;
125
+ }
126
+ return true;
127
+ }
128
+
129
+ /** Return true when replacing a BooleanLiteral at `path` is syntactically safe. */
130
+ function isSafeBooleanExpression(path) {
131
+ const parent = path.parentPath && path.parentPath.node;
132
+ if (!parent) {
133
+ return false;
134
+ }
135
+ if (isPropertyKeyPath(path)) {
136
+ return false;
137
+ }
138
+ if (parent.type === 'ImportAttribute') {
139
+ return false;
140
+ }
141
+ if (parent.type === 'JSXAttribute' && path.key === 'value') {
142
+ return false;
143
+ }
144
+ return true;
145
+ }
146
+
147
+ /** Return true when a directive list contains "use strict". */
148
+ function hasUseStrict(directives) {
149
+ return (
150
+ Array.isArray(directives) &&
151
+ directives.some(
152
+ (directive) => directive.value && directive.value.value === 'use strict'
153
+ )
154
+ );
155
+ }
156
+
157
+ /**
158
+ * Return true when the expression at `path` is evaluated in strict mode,
159
+ * including modules, classes, and functions with a "use strict" directive.
160
+ */
161
+ function isStrictContext(path) {
162
+ let scope = path.scope;
163
+ while (scope) {
164
+ const scopePath = scope.path;
165
+ if (scopePath.isProgram()) {
166
+ return (
167
+ scopePath.node.sourceType === 'module' ||
168
+ hasUseStrict(scopePath.node.directives)
169
+ );
170
+ }
171
+ if (typeof scopePath.isFunction === 'function' && scopePath.isFunction()) {
172
+ if (
173
+ (typeof scopePath.isClassMethod === 'function' && scopePath.isClassMethod()) ||
174
+ (typeof scopePath.isClassPrivateMethod === 'function' &&
175
+ scopePath.isClassPrivateMethod())
176
+ ) {
177
+ return true;
178
+ }
179
+ const body = scopePath.node.body;
180
+ if (body && hasUseStrict(body.directives)) {
181
+ return true;
182
+ }
183
+ }
184
+ if (
185
+ (typeof scopePath.isClass === 'function' && scopePath.isClass()) ||
186
+ (typeof scopePath.isClassBody === 'function' && scopePath.isClassBody()) ||
187
+ (typeof scopePath.isClassProperty === 'function' &&
188
+ scopePath.isClassProperty()) ||
189
+ (typeof scopePath.isClassPrivateProperty === 'function' &&
190
+ scopePath.isClassPrivateProperty()) ||
191
+ (typeof scopePath.isStaticBlock === 'function' && scopePath.isStaticBlock())
192
+ ) {
193
+ return true;
194
+ }
195
+ scope = scope.parent;
196
+ }
197
+ return false;
198
+ }
199
+
200
+ const EVAL_FORBIDDEN_NODES = new Set([
201
+ 'Super',
202
+ 'MetaProperty',
203
+ 'Import',
204
+ 'ImportExpression',
205
+ 'AwaitExpression',
206
+ 'YieldExpression',
207
+ 'PrivateName',
208
+ 'ClassPrivateProperty',
209
+ 'ClassPrivateMethod',
210
+ ]);
211
+
212
+ /** Return true when an expression cannot be safely evaluated through eval(). */
213
+ function containsForbiddenEvalNode(node) {
214
+ let found = false;
215
+ walk(node, (child) => {
216
+ if (found) {
217
+ return false;
218
+ }
219
+ if (EVAL_FORBIDDEN_NODES.has(child.type)) {
220
+ found = true;
221
+ return false;
222
+ }
223
+ return true;
224
+ });
225
+ return found;
226
+ }
227
+
228
+ /**
229
+ * Return true when a statement contains a function/class declaration that
230
+ * would change semantics if the statement were moved into a switch case.
231
+ * Nested function bodies are intentionally skipped: moving a whole function
232
+ * expression is safe.
233
+ */
234
+ function hasUnsafeDeclaration(statement) {
235
+ let unsafe = false;
236
+ walk(statement, (node) => {
237
+ if (unsafe) {
238
+ return false;
239
+ }
240
+ if (node !== statement) {
241
+ if (node.type === 'FunctionDeclaration' || node.type === 'ClassDeclaration') {
242
+ unsafe = true;
243
+ return false;
244
+ }
245
+ if (node.type === 'ClassExpression' || FUNCTION_LIKE.has(node.type)) {
246
+ return false;
247
+ }
248
+ }
249
+ return true;
250
+ });
251
+ return unsafe;
252
+ }
253
+
254
+ /** Return true when an AST contains a `with` statement. */
255
+ function hasWithStatement(ast) {
256
+ let found = false;
257
+ t.traverseFast(ast, (node) => {
258
+ if (node.type === 'WithStatement') {
259
+ found = true;
260
+ }
261
+ });
262
+ return found;
263
+ }
264
+
265
+ /** Collect every identifier name that appears anywhere in the AST. */
266
+ function collectIdentifierNames(ast) {
267
+ const names = new Set();
268
+ t.traverseFast(ast, (node) => {
269
+ if (node.type === 'Identifier' || node.type === 'JSXIdentifier') {
270
+ names.add(node.name);
271
+ }
272
+ });
273
+ return names;
274
+ }
275
+
276
+ /**
277
+ * Create a generator of short, globally unique identifier names.
278
+ * Using file-wide uniqueness avoids variable capture when renaming a binding
279
+ * that is referenced from a nested scope.
280
+ */
281
+ function createNameGenerator(ast) {
282
+ const used = collectIdentifierNames(ast);
283
+ let counter = 0;
284
+
285
+ return {
286
+ used,
287
+ next() {
288
+ let name;
289
+ do {
290
+ name = `_${(counter++).toString(36)}`;
291
+ } while (used.has(name));
292
+ used.add(name);
293
+ return name;
294
+ },
295
+ reserve(name) {
296
+ used.add(name);
297
+ },
298
+ };
299
+ }
300
+
301
+ /** Escape a property name as a ``\xNN`` / ``\uNNNN`` string body. */
302
+ function encodePropertyName(name) {
303
+ let encoded = '';
304
+ for (const character of name) {
305
+ const codePoint = character.codePointAt(0);
306
+ if (codePoint <= 0xff) {
307
+ encoded += `\\x${codePoint.toString(16).padStart(2, '0')}`;
308
+ } else {
309
+ encoded += unicodeEscape(character);
310
+ }
311
+ }
312
+ return encoded;
313
+ }
314
+
315
+ /**
316
+ * Escape a string as a sequence of ``\uNNNN`` escapes.
317
+ * Characters outside the BMP are emitted as a valid surrogate pair.
318
+ */
319
+ function unicodeEscape(value) {
320
+ let encoded = '';
321
+ for (const character of value) {
322
+ let codePoint = character.codePointAt(0);
323
+ if (codePoint <= 0xffff) {
324
+ encoded += `\\u${codePoint.toString(16).padStart(4, '0')}`;
325
+ } else {
326
+ codePoint -= 0x10000;
327
+ const high = 0xd800 + (codePoint >> 10);
328
+ const low = 0xdc00 + (codePoint & 0x3ff);
329
+ encoded += `\\u${high.toString(16).padStart(4, '0')}`;
330
+ encoded += `\\u${low.toString(16).padStart(4, '0')}`;
331
+ }
332
+ }
333
+ return encoded;
334
+ }
335
+
336
+ /** Build a StringLiteral that Babel will print using the supplied raw text. */
337
+ function stringLiteralWithRaw(value, raw) {
338
+ const node = t.stringLiteral(value);
339
+ node.extra = {
340
+ rawValue: value,
341
+ raw,
342
+ };
343
+ return node;
344
+ }
345
+
346
+ /** Return true when a property node is an identifier or string with `name`. */
347
+ function isPropertyNamed(node, name) {
348
+ return (
349
+ (node.type === 'Identifier' && node.name === name) ||
350
+ (node.type === 'StringLiteral' && node.value === name)
351
+ );
352
+ }
353
+
354
+ /** Return true for `module.exports` or `module["exports"]`. */
355
+ function isModuleExportsMember(node) {
356
+ return (
357
+ node.type === 'MemberExpression' &&
358
+ node.object.type === 'Identifier' &&
359
+ node.object.name === 'module' &&
360
+ isPropertyNamed(node.property, 'exports')
361
+ );
362
+ }
363
+
364
+ /** Return true for `exports.x` or `module.exports.x`. */
365
+ function isExportsMember(node) {
366
+ return (
367
+ node.type === 'MemberExpression' &&
368
+ ((node.object.type === 'Identifier' && node.object.name === 'exports') ||
369
+ isModuleExportsMember(node.object))
370
+ );
371
+ }
372
+
373
+ /** Detect CommonJS export patterns, where global renaming is unsafe. */
374
+ function hasCommonJsExports(ast) {
375
+ let found = false;
376
+ t.traverseFast(ast, (node) => {
377
+ if (found) {
378
+ return;
379
+ }
380
+ if (node.type === 'AssignmentExpression') {
381
+ const left = node.left;
382
+ if (left.type === 'Identifier' && left.name === 'exports') {
383
+ found = true;
384
+ return;
385
+ }
386
+ if (isModuleExportsMember(left) || isExportsMember(left)) {
387
+ found = true;
388
+ return;
389
+ }
390
+ }
391
+ if (isExportsMember(node)) {
392
+ found = true;
393
+ }
394
+ });
395
+ return found;
396
+ }
397
+
398
+ /** Collect names that are exported and therefore must not be renamed. */
399
+ function collectExportedNames(ast) {
400
+ const names = new Set();
401
+ t.traverseFast(ast, (node) => {
402
+ if (node.type === 'ExportNamedDeclaration') {
403
+ if (node.declaration) {
404
+ const identifiers = t.getBindingIdentifiers(node.declaration);
405
+ for (const name of Object.keys(identifiers)) {
406
+ names.add(name);
407
+ }
408
+ }
409
+ for (const specifier of node.specifiers || []) {
410
+ if (specifier.local && specifier.local.type === 'Identifier') {
411
+ names.add(specifier.local.name);
412
+ }
413
+ }
414
+ } else if (node.type === 'ExportDefaultDeclaration') {
415
+ const declaration = node.declaration;
416
+ if (
417
+ declaration &&
418
+ (declaration.type === 'FunctionDeclaration' ||
419
+ declaration.type === 'ClassDeclaration') &&
420
+ declaration.id
421
+ ) {
422
+ names.add(declaration.id.name);
423
+ }
424
+ if (declaration && declaration.type === 'Identifier') {
425
+ names.add(declaration.name);
426
+ }
427
+ }
428
+ });
429
+ return names;
430
+ }
431
+
432
+ module.exports = {
433
+ markGenerated,
434
+ isGenerated,
435
+ walk,
436
+ isPropertyKeyPath,
437
+ isSafeStringExpression,
438
+ isSafeNumericExpression,
439
+ isSafeBooleanExpression,
440
+ hasUseStrict,
441
+ isStrictContext,
442
+ containsForbiddenEvalNode,
443
+ hasUnsafeDeclaration,
444
+ collectIdentifierNames,
445
+ createNameGenerator,
446
+ encodePropertyName,
447
+ unicodeEscape,
448
+ stringLiteralWithRaw,
449
+ hasCommonJsExports,
450
+ collectExportedNames,
451
+ hasWithStatement,
452
+ };
package/package.json CHANGED
@@ -1,6 +1,58 @@
1
1
  {
2
2
  "name": "javascript-obfuscator-lite",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "1.0.0",
4
+ "description": "A lightweight, AST-based JavaScript obfuscator with a CLI and a programmatic API.",
5
+ "keywords": [
6
+ "javascript",
7
+ "obfuscator",
8
+ "obfuscation",
9
+ "code-protection",
10
+ "protect",
11
+ "ast",
12
+ "babel",
13
+ "cli"
14
+ ],
15
+ "author": "Wang Yinzhen",
16
+ "license": "MIT",
17
+ "homepage": "https://js-obfuscator.github.io/",
18
+ "main": "lib/index.js",
19
+ "types": "lib/index.d.ts",
20
+ "exports": {
21
+ ".": {
22
+ "types": "./lib/index.d.ts",
23
+ "require": "./lib/index.js",
24
+ "default": "./lib/index.js"
25
+ },
26
+ "./package.json": "./package.json"
27
+ },
28
+ "bin": {
29
+ "javascript-obfuscator-lite": "bin/cli.js",
30
+ "jso-lite": "bin/cli.js"
31
+ },
32
+ "files": [
33
+ "bin",
34
+ "lib",
35
+ "README.md",
36
+ "LICENSE",
37
+ "CHANGELOG.md"
38
+ ],
39
+ "scripts": {
40
+ "test": "node --test",
41
+ "test:smoke": "node test-support/smoke.js",
42
+ "test:watch": "node --test --watch",
43
+ "prepublishOnly": "npm test && npm run test:smoke"
44
+ },
45
+ "engines": {
46
+ "node": ">=12.0.0"
47
+ },
48
+ "publishConfig": {
49
+ "access": "public"
50
+ },
51
+ "sideEffects": false,
52
+ "dependencies": {
53
+ "@babel/generator": "^7.23.6",
54
+ "@babel/parser": "^7.23.9",
55
+ "@babel/traverse": "^7.23.9",
56
+ "@babel/types": "^7.23.9"
57
+ }
58
+ }