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

privy

v0.4.1

Published

Private Members for Objects in JavaScript

Downloads

115

Readme

Privy, Private Members for Objects in JavaScript

The thing that makes programming so difficult is it has to be perfect. A program has to be absolutely perfect in all detail, for all possible inputs and all possible uses even including uses that were not anticipated in the design of the program. If any flaw is found in the program the computer has licence to do the worst possible thing at the worst possible time and it is not the computer's fault.

-- Douglas Crockford

What is the goal?

Privy aims to create truly private members for JavaScript objects that are accessible through prototype methods. It is a real solution, made possible by the power of ~~Greyskull~~ closures, that associates private members with an object for the duration of its lifetime.

Members are private and should not be confused with protected.

What would typically usage look like?

var Person = (function () {
  var p = Privy.createAccessor();

  function Person(name, age) {
    var privates = Privy.createPrivates(this, p);
    
    privates.name = name;
    privates.age = age;
  }

  Person.prototype.name = function () {
    return p(this).name;
  };

  Person.prototype.sameAge = function (other) {
    return p(this).age === p(other).age;
  };

  return Person;
}).call();

var thomas = new Person("Thomas", 22),
    sarah  = new Person("Sarah", 22);

thomas.name();           // "Thomas"
sarah.sameAge(thomas);   // true
thomas.name.call(sarah); // "Sarah"

How does this differ from X?

I am unable to comment on all implementations of private members in JavaScript, but generally most are susceptible to immortal objects, incorrect design of private in Object Orientation or can be exploited by changing the this argument. More information can be found further down in the trade off section.

Privy differs due to the nature of its so called accessor function, a handshake takes place between the accessors and the privy functions via a sealer object preventing a man-in-the-middle attack.

Correct use of Privy ensures that only prototype members declared in scope of the sealer will have access to the private members, this is achieved through closures.

What are the trade offs?

Costs:

  • One single property used per object.
  • One function per constructor to access private members
  • Additional operational objects such as secrets and sealers for each constructor.
  • Gaining access to the private member object takes 7 additional function calls.
  • No support for prototypal inheritance (currently).

Benefits:

  • Prototype methods can access the private members, not just privileged ones.
  • Privates are created in the constructor and will be eligible for garbage collection when the object goes out of scope.
  • Access to members does not require cryptography but rather a fundamental language feature, closure.
  • Other objects of the same constructor can see other privates as long as they have a reference to the object (similar to other Object Oriented languages).

Privy API

Privy.createAccessor([property = "_"])

Creates an accessor that is intended for use with a single constructor. Passing a value for property will be used as a string for the private property store. New accessor is returned.

Privy.createPrivates(object, accessor)

Creates a private store on the object based on the accessor, which by default will be _.

Returns the newly created private member object.

Compatibility

Requires functionally of Object.defineProperty and Object.defineProperties introduced in ECMAScript 5.

Disclaimer

This was a weekend project and is by no means bullet proof. Privy is an attempt to materialize some thoughts I had on JavaScript.

License

Copyright (C) 2013 Thomas Nadin

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.