npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

protect-class

v0.2.1

Published

A small utility that allows you to protect a js class from changes (adding, removing fields) and traversing the public fields of the class, including getters.

Downloads

6

Readme

Protect class js

Creates a protected proxy class that restricts access to certain properties and allows viewing only of the class's public fields, including getters.

Install

npm install protect-class

Usage

Javascript

import { protect } from 'protect-class';

class Foo {
  bar = 'bar';
}

const cls = protect(new Foo());
...

Typescript

import { Protect, protect } from 'protect-class';

@Protect()
class Foo {
  public bar: string = 'bar';
}

const cls = new Foo();
/*
  OR
  const cls = protect(new Foo());
*/
...

Examples

Demonstration of the utility. In the following example, we will create a custom class:

class Test {
  test1;
  test2 = {
    test4: undefined
  };
  #test3;
  
  get test3() {
      return this.#test3;
  }
  
  set test3(value) {
      this.#test3 = value;
  }
}

Let's see how an instance of the class behaves without protection:

const cls = new Test();

console.log(Object.keys(cls));
// [ 'test1', 'test2' ]

console.log({...cls}); 
// { test1: undefined, test2: { test4: undefined } }

console.log({...Object.assign(cls, { test1: 'test1', test3: 'test3', test5: 'Hello' })}); 
// { test1: 'test1', test2: { test4: undefined }, test5: 'Hello' }

console.log(cls.test5); 
// Hello

console.log(JSON.stringify(cls)) 
// {"test1":"test1","test2":{},"test5":"Hello"}

As a result, we do not have the test3 field in the output, but a new test5 field has appeared :confused:

And with protection:

const protect_cls = protect(new Test());

console.log(Object.keys(protect_cls));
// [ 'test1', 'test2', 'test3' ]

console.log({...protect_cls});
// { test1: undefined, test2: { test4: undefined }, test3: undefined }

console.log({...Object.assign(protect_cls, { test1: 'test1', test3: 'test3', test5: 'Hello' })});
// { test1: 'test1', test2: { test4: undefined }, test3: 'test3' }

console.log(protect_cls.test5);
// undefined

console.log(JSON.stringify(protect_cls))
// {"test1":"test1","test2":{},"test3":"test3"}

The protected instance outputs the getter key test3 and when executing Object.assign did not add the new field test5 :sunglasses:

Options

protect(cls: Object, options?: IOptionsProtectedObject)

Creates a protected class proxy that restricts access to certain properties.

const cls = protect(new Foo(), {
  allowProtectedField: true,
  allowReadError: true,
  allowWriteError: true,
  disableDeleteError: true,
  disableCache: true
});

Protect(options?: IOptionsProtectedObject)

Decorator function that creates a protected class proxy.

@Protect({
  allowProtectedField: true,
  allowReadError: true,
  allowWriteError: true,
  disableDeleteError: true,
  disableCache: true
})
class Foo {
  public bar: string = 'bar';
}

options: IOptionsProtectedObject

  • allowProtectedField: boolean - (default: false) Allow access to protected fields (fields starting with "_").
  • allowReadError: boolean - (default: false) Allow throwing an error when trying to access an unknown property.
  • allowWriteError: boolean - (default: false) Allow throwing an error when trying to write to an unknown property.
  • disableDeleteError: boolean - (default: false) Disable throwing an error when trying to delete a property.
  • disableCache: boolean - (default: false) Disable caching of the enumerable getters.