gulp-obfuscate Documentation

repository·master·Indexed 18 days ago

https://github.com/mikrofusion/gulp-obfuscate

A Gulp plugin for obfuscating JavaScript code by replacing variable, function, and object property names with visually confusing characters. It supports multiple styles, including LOOK_OF_DISAPPROVAL and ZALGO, and allows for the exclusion of specific patterns or reserved words to maintain interface compatibility.

Tokens
1.3K
Snippets
5
Records
7
Agent score
13%

What's inside gulp-obfuscate

  1. Exclude specific patterns from obfuscation

    master

    Use the exclude option to prevent certain strings or regular expressions from being obfuscated. This is useful for maintaining names for interfaces or external code dependencies.

    Note: The default list of JavaScript reserved words and special characters is automatically appended to any values you provide in options.exclude.

  2. Use gulp-obfuscate in a Gulp task

    master

    To use the plugin, require gulp-obfuscate and pipe your source files through the obfuscate() function within a Gulp task.

    var gulp = require('gulp');
    var obfuscate = require('gulp-obfuscate');
    
    gulp.task('default', function () {
    	return gulp.src('test.js')
    		.pipe(obfuscate());
    });
  3. Configure obfuscation method with obfuscate(options)

    master

    The obfuscate(options) function accepts an options object to customize the obfuscation style. You can choose between LOOK_OF_DISAPPROVAL (default) and ZALGO styles. Both methods produce valid ECMAScript 5.1 variables, but ZALGO may not be supported by older browsers.

    obfuscate = require('gulp-obfuscate');
    ...
    .pipe(obfuscate({ replaceMethod: obfuscate.ZALGO }))
    ...
  4. Reference: obfuscate(options) configuration keys

    master

    The following configuration keys are available for the obfuscate(options) method:

    // options.replaceMethod
    // Type: ZALGO | LOOK_OF_DISAPPROVAL
    // Default: 'LOOK_OF_DISAPPROVAL'
    
    // options.exclude
    // Type: String | Array of Strings
    // Default: 'break', 'case', 'catch', 'continue', 'debugger', 'default', 'delete', 'do', 'else', 'finally', 'for', 'function', 'if', 'in', 'instanceof', 'new', 'return', 'switch', 'this', 'throw', 'try', 'typeof', 'var', 'void', 'while', 'with', 'prototype', 'null', 'true', 'false', 'NaN', 'undefined', 'Infinity', 'ಠ_ಠ', 'H͇̬͔̳̖̅̒ͥͧẸ̖͇͈͍̱̭̌͂͆͊_C͈OM̱̈́͛̈ͩ͐͊ͦEͨ̓̐S̬̘͍͕͔͊̆̑̈́̅'
  5. Use gulp-obfuscate as a Gulp plugin

    master

    The gulpObfuscate(options) function is the primary entrypoint for the plugin. It obfuscates variable names, function names, and object properties by replacing them with a specific string pattern.

    Options

    OptionTypeDefaultDescription
    excludeArraygulpObfuscate.defaultExcludeA list of strings (regex patterns or literal names) to exclude from obfuscation.
    replaceMethodNumbergulpObfuscate.LOOK_OF_DISAPPROVALDetermines the character set used for the obfuscated names.
    debugBooleanfalseIf true, logs the conversion mapping (e.g., original <-> obfuscated) to the console using gulp-util.

    Replace Methods

    • gulpObfuscate.LOOK_OF_DISAPPROVAL (Value: 1): Uses the string ಠ_ಠ.
    • gulpObfuscate.ZALGO (Value: 0): Uses a Zalgo-style string: H͇̬͔̳̖̅̒ͥͧẸ̖͇͈͍̱̭̌͂͆͊_C͈OM̱̈́͛̈ͩ͐͊ͦEͨ̓̐S̬̘͍͕͔͊̆̑̈́̅.
    const gulpObfuscate = require('gulp-obfuscate');
    
    // Basic usage
    gulp.task('obfuscate', () => {
      return gulp.src('src/**/*.js')
        .pipe(gulpObfuscate({
          exclude: ['importantVar', 'apiEndpoint'],
          replaceMethod: gulpObfuscate.ZALGO,
          debug: true
        }))
        .pipe(gulp.dest('dist'));
    });
  6. Initialize and configure gulp-obfuscate

    master

    The gulpObfuscate.init() method resets the plugin's internal state. While init() is called automatically upon requiring the module, you can call it manually to reset seeds or configuration constants.

    Internal State and Constants

    When init() is called, the following properties are set:

    • gulpObfuscate.nameHash: An object used to track the mapping between original names and obfuscated names to ensure consistency within a single run.
    • gulpObfuscate.seed: A random integer between minSeed and maxSeed used to generate unique obfuscated names.
    • gulpObfuscate.minSeed: The minimum value for the random seed (default: 1).
    • gulpObfuscate.maxSeed: The maximum value for the random seed (default: 999).
    • gulpObfuscate.defaultExclude: An array of reserved keywords and strings that are automatically excluded from obfuscation (e.g., var, function, this, null, true, false, undefined, NaN, Infinity, and the obfuscation strings themselves).
    • gulpObfuscate.ZALGO: Constant 0.
    • gulpObfuscate.LOOK_OF_DISAPPROVAL: Constant 1.