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

proxy-private-fields

v1.0.1

Published

This package enables you to create JS objects with private fields. You can define your own rules for private field detection, behaviour on overwrite attempts and more.

Downloads

5

Readme

proxy-private-fields

This package enables you to create JS objects with private fields. You can define your own rules for private field detection, behaviour on overwrite attempts and more.

The default behaviour is such:

import privatize, { lockpick } from "proxy-private-fields";

const object = {
  "@private a": 1,
  b: 2,
};

const privatized = privatize(object);

console.log(privatized.a); // undefined
console.log(privatized.b); // 2

As we can see, the privatized object blocks access to fields which have been annotated with @private. This behaviour can be of course customized, either via your own regexp or a handler:

const object1 = {
  "myOwnPrefix a": 1,
  b: 2,
};

const object2 = {
  c: 3,
  d: 4,
};

const privateFields = ["c"];

const privatizedWithCustomRegExp = privatize(object1, {
  regexp: /^myOwnPrefix/,
});

const privatizedWithCustomHandler = privatize(object2, {
  handler: (object) => {
    return privateFields.map((el) => [el, el]);
  },
});

console.log(privatizedWithCustomRegExp.a); // undefined
console.log(privatizedWithCustomRegExp.b); // 2

console.log(privatizedWithCustomHandler.c); // undefined
console.log(privatizedWithCustomHandler.d); // 4

Bear in mind that the handler has to return pairs of keys. In this case this might seem a bit redundant, but in cases where a regexp is used the function has to know how the fields in the original object are named and how they are supposed to be named in the target one.

As you probably noticed in the import section, the package exposes something called lockpick. This is by default a Symbol that enables you to reconstruct the whole object, together with the private fields. In the next example I will show you how it's done. I will also demontrate the fact, that methods have access to private fields.

const object = {
  "@private a": 1,
  b: 2,
  method() {
    this.a = 3;
    this.b = 4;
    console.log(this.a, this.b);
  },
};

const privatized = privatize(object);

privatized.method(); // 3 4

console.log(privatized.a); // undefined
console.log(privatized.b); // 4

const lockpicked = privatized[lockpick];

console.log(lockpicked.a); // 3
console.log(lockpicked.b); // 4

console.log(lockpicked === privatized); // false
console.log(lockpicked === object); // false

Bear in mind that using the lockpick creates a new reference. This is of course also customizable:

const object = {
  "@private a": 1,
  b: 2,
};

const privatized = privatize(object, { newReferenceOnLockpick: false });

const lockpicked = privatized[lockpick];

console.log(lockpicked === privatized); // false
console.log(lockpicked === object); // true

console.log(lockpicked.a); // 1

The last thing I want to talk about are overwrite errors:

const object = {
  "@private a": 1,
  b: 2,
};

const privatized = privatize(object);

privatized.a = 3; // Throws an error

If you want to prevent this, you can pass your own error handler:

const object = {
  "@private a": 1,
  b: 2,
};

const privatized = privatize(object, {
  overwriteHandler: (target, path, value) => {
    console.log("This field cannot be mutated");
  },
});

privatized.a = 3; // This field cannot be mutated