All Projects → TremayneChrist → Protectjs

TremayneChrist / Protectjs

Licence: mit
Private methods & properties in JavaScript

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Protectjs

Grunt Javascript Obfuscator
Obfuscates JavaScript files using amazing javascript-obfuscator.
Stars: ✭ 34 (-63.83%)
Mutual labels:  protection
Sidekiq Lock
Simple redis-based lock mechanism for your sidekiq workers
Stars: ✭ 60 (-36.17%)
Mutual labels:  lock
Griefprevention
GriefDefender has replaced GP. See github link for latest information.
Stars: ✭ 76 (-19.15%)
Mutual labels:  protection
Lock
Auth0's signin solution
Stars: ✭ 997 (+960.64%)
Mutual labels:  lock
Repo Lockdown
GitHub Action that immediately closes and locks issues and pull requests
Stars: ✭ 56 (-40.43%)
Mutual labels:  lock
Corenavigation
📱📲 Navigate between view controllers with ease. 💫 🔜 More stable version (written in Swift 5) coming soon.
Stars: ✭ 69 (-26.6%)
Mutual labels:  protection
Dyfauthidandgesturelock
手势密码解锁和 TouchID (指纹) / FaceID(面容) 解锁,代码简洁高效。(Gesture passcode unlocking and TouchID (fingerprint) / FaceID (facial features) unlocking, its code is concise and efficient.) https://github.com/dgynfi/DYFAuthIDAndGestureLock
Stars: ✭ 20 (-78.72%)
Mutual labels:  lock
Cidram
CIDRAM: Classless Inter-Domain Routing Access Manager.
Stars: ✭ 86 (-8.51%)
Mutual labels:  protection
Dnscrypt Menu
Manage DNSCrypt from the macOS menu bar (BitBar plugin)
Stars: ✭ 59 (-37.23%)
Mutual labels:  protection
Hookmeifyoucan
Protection against cycript/runtime
Stars: ✭ 69 (-26.6%)
Mutual labels:  protection
Kioskmode Android
Screen Pinning Android Lollipop with enable Device Administrator without Root needed
Stars: ✭ 40 (-57.45%)
Mutual labels:  lock
Alfred Lock
Alfred 3 workflow to lock your Mac
Stars: ✭ 54 (-42.55%)
Mutual labels:  lock
Lock System
Lock your system
Stars: ✭ 69 (-26.6%)
Mutual labels:  lock
Ideas
Ideas for protecting C/C++
Stars: ✭ 37 (-60.64%)
Mutual labels:  protection
Yubikeylockd
Simple daemon for locking and unlocking macOS with Yubikey
Stars: ✭ 78 (-17.02%)
Mutual labels:  lock
Tic Tac
Client not paid ? This is the solution of your problem
Stars: ✭ 29 (-69.15%)
Mutual labels:  protection
Skater .net Obfuscator
Skater .NET Obfuscator is an obfuscation tool for .NET code protection. It implements all known software protection techniques and obfuscation algorithms.
Stars: ✭ 64 (-31.91%)
Mutual labels:  protection
Limiter
一个注解使你的SpringBoot项目获得分布式锁和限流器能力
Stars: ✭ 93 (-1.06%)
Mutual labels:  lock
Pause On Lock
Pause/Resume your music player when locking/unlocking your Linux desktop.
Stars: ✭ 79 (-15.96%)
Mutual labels:  lock
Laravel Botscout
Block malicious scripts using botscout.com protection for your laravel app
Stars: ✭ 69 (-26.6%)
Mutual labels:  protection

ProtectJS / Protect JS

Build Status Coverage Status

Methods/Functions

Adding private methods to an object in JavaScript has always been an awkward thing to do, as JavaScript doesn't exactly support it. Instead, we either place enclosed functions in the constructor, which consumes more memory per object, or, we enclose both the object definition and our private methods inside of a closure.

The latter is a good way to accomplish private methods in JavaScript as it creates truly private methods and doesn't add to the memory consumption. The problem with it is, is that your private methods are separate from the object and your coding style is different compared to public definitions on the prototype.

ProtectJS is a library that extends objects you define, to allow you to add all your private methods to the prototype chain. It does this by adding protection checks to all of your methods, any marked with a prepending underscore (_) is treated as a private method.

Other properties

As well as methods, JavaScript doesn't exactly support any type of private property. This becomes a problem when dangerous modifications to these properties occur.

Think of complex algorithms - If these could be modified by external sources, their output would be completely invalidated, potentially causing huge implications.

Examples

In many languages, it is custom to prepend private variables with an underscore (_), so we use this in JavaScript to show that an object should be considered as a private. Obviously this has absolutely no affect to whether it is actually private or not, it's more of a visual reference for a developer.

Let's create a basic object using this approach...

// Create the object
function MyObject() {}

// This is our public method
MyObject.prototype.public = function () {
  console.log('PUBLIC method has been called');
};

// This is our private method, using (_)
MyObject.prototype._private = function () {
  console.log('PRIVATE method has been called');
};

// Create an instance of the object
var mo = new MyObject();

// Call its methods
mo.public(); // Pass
mo._private(); // Pass

As I stated previously, there is no protection here, so I could easily call the _private() method and get a message saying "PRIVATE method has been called".

Now let's protect the object with ProtectJS!..

Add ProtectJS into your source, making sure it loads before the code you want to protect.

<head>
    <title>PrivateJS</title>
    <script type="text/javascript" src="ProtectJS/protect.min.js"></script>
    <script type="text/javascript" src="app.js"></script>
</head>

Now that ProtectJS is available, we need to use it on the object

// Create the object
function MyObject() {}

// This is our public method
MyObject.prototype.public = function () {
  console.log('PUBLIC method has been called');
};

// This is our private method, using (_)
MyObject.prototype._private = function () {
  console.log('PRIVATE method has been called');
};

protect(MyObject); // Protect the object prototype

// Create an instance of the object
var mo = new MyObject();

// Call its methods
mo.public(); // Pass
mo._private(); // Fail

ProtectJS assumes that all methods starting with underscores in the prototype are private, and will add protection checks to prevent them from being called outside of the object.

Once protected, the only way to call a private method is by calling it through another method inside the same object.


Pros

  1. Enables private methods to be added onto the prototype, keeping object memory to a minimum.

  2. Protects your other properties from being modified by external sources.

  3. Keeps code creation clean and easy to read.

  4. Allows you to define objects in a natural way.

  5. Doesn't change your coding style.

  6. Protects properties you don't want to be used.

Cons

  1. You have to include the ProtectJS library in your project.
  2. Marginal overheads to be considered (Performance tests to come)

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].