1 line
21 KiB
JSON
1 line
21 KiB
JSON
{"_id":"private","_rev":"34-3a6b546f8abce10c95f4142ff312b3eb","name":"private","description":"Utility for associating truly private state with any JavaScript object","dist-tags":{"latest":"0.1.7"},"versions":{"0.0.1":{"author":{"name":"Ben Newman","email":"bn@cs.stanford.edu"},"name":"private","description":"Utility for associating truly private state with any JavaScript object","keywords":["private","access control","access modifiers","encapsulation","secret","state","privilege","scope","es5"],"version":"0.0.1","homepage":"http://github.com/benjamn/private","repository":{"type":"git","url":"git://github.com/benjamn/private.git"},"main":"private.js","engines":{"node":">= 0.6"},"_id":"private@0.0.1","dist":{"shasum":"536aafc205c36c801ecc27b742b94c123b85f201","tarball":"https://registry.npmjs.org/private/-/private-0.0.1.tgz"},"_from":".","_npmVersion":"1.2.10","_npmUser":{"name":"benjamn","email":"bn@cs.stanford.edu"},"maintainers":[{"name":"benjamn","email":"bn@cs.stanford.edu"}],"directories":{}},"0.0.2":{"author":{"name":"Ben Newman","email":"bn@cs.stanford.edu"},"name":"private","description":"Utility for associating truly private state with any JavaScript object","keywords":["private","access control","access modifiers","encapsulation","secret","state","privilege","scope","es5"],"version":"0.0.2","homepage":"http://github.com/benjamn/private","repository":{"type":"git","url":"git://github.com/benjamn/private.git"},"main":"private.js","engines":{"node":">= 0.6"},"_id":"private@0.0.2","dist":{"shasum":"06e84c17b9bed1ce4b9af507495c8b629c5f96f6","tarball":"https://registry.npmjs.org/private/-/private-0.0.2.tgz"},"_from":".","_npmVersion":"1.2.10","_npmUser":{"name":"benjamn","email":"bn@cs.stanford.edu"},"maintainers":[{"name":"benjamn","email":"bn@cs.stanford.edu"}],"directories":{}},"0.0.3":{"author":{"name":"Ben Newman","email":"bn@cs.stanford.edu"},"name":"private","description":"Utility for associating truly private state with any JavaScript object","keywords":["private","access control","access modifiers","encapsulation","secret","state","privilege","scope","es5"],"version":"0.0.3","homepage":"http://github.com/benjamn/private","repository":{"type":"git","url":"git://github.com/benjamn/private.git"},"main":"private.js","engines":{"node":">= 0.6"},"_id":"private@0.0.3","dist":{"shasum":"752243a6416a03534ce47aaaa5beffd49aae0d35","tarball":"https://registry.npmjs.org/private/-/private-0.0.3.tgz"},"_from":".","_npmVersion":"1.2.10","_npmUser":{"name":"benjamn","email":"bn@cs.stanford.edu"},"maintainers":[{"name":"benjamn","email":"bn@cs.stanford.edu"}],"directories":{}},"0.0.4":{"author":{"name":"Ben Newman","email":"bn@cs.stanford.edu"},"name":"private","description":"Utility for associating truly private state with any JavaScript object","keywords":["private","access control","access modifiers","encapsulation","secret","state","privilege","scope","es5"],"version":"0.0.4","homepage":"http://github.com/benjamn/private","repository":{"type":"git","url":"git://github.com/benjamn/private.git"},"main":"private.js","engines":{"node":">= 0.6"},"_id":"private@0.0.4","dist":{"shasum":"d6d2c5fbbf8e52db0183b73719d61341c73fd313","tarball":"https://registry.npmjs.org/private/-/private-0.0.4.tgz"},"_from":".","_npmVersion":"1.2.10","_npmUser":{"name":"benjamn","email":"bn@cs.stanford.edu"},"maintainers":[{"name":"benjamn","email":"bn@cs.stanford.edu"}],"directories":{}},"0.0.5":{"author":{"name":"Ben Newman","email":"bn@cs.stanford.edu"},"name":"private","description":"Utility for associating truly private state with any JavaScript object","keywords":["private","access control","access modifiers","encapsulation","secret","state","privilege","scope","es5"],"version":"0.0.5","homepage":"http://github.com/benjamn/private","repository":{"type":"git","url":"git://github.com/benjamn/private.git"},"main":"private.js","scripts":{"test":"node test/run.js"},"engines":{"node":">= 0.6"},"bugs":{"url":"https://github.com/benjamn/private/issues"},"_id":"private@0.0.5","dist":{"shasum":"a05f8c522c6bf9e49256b62c4bc37c2100b39fc2","tarball":"https://registry.npmjs.org/private/-/private-0.0.5.tgz"},"_from":".","_npmVersion":"1.2.24","_npmUser":{"name":"benjamn","email":"bn@cs.stanford.edu"},"maintainers":[{"name":"benjamn","email":"bn@cs.stanford.edu"}],"directories":{}},"0.1.0":{"author":{"name":"Ben Newman","email":"bn@cs.stanford.edu"},"name":"private","description":"Utility for associating truly private state with any JavaScript object","keywords":["private","access control","access modifiers","encapsulation","secret","state","privilege","scope","es5"],"version":"0.1.0","homepage":"http://github.com/benjamn/private","repository":{"type":"git","url":"git://github.com/benjamn/private.git"},"main":"private.js","scripts":{"test":"node test/run.js"},"engines":{"node":">= 0.6"},"bugs":{"url":"https://github.com/benjamn/private/issues"},"_id":"private@0.1.0","dist":{"shasum":"1a80de5460e930c7b074cd578a571ad36c0b49cd","tarball":"https://registry.npmjs.org/private/-/private-0.1.0.tgz"},"_from":".","_npmVersion":"1.3.13","_npmUser":{"name":"benjamn","email":"bn@cs.stanford.edu"},"maintainers":[{"name":"benjamn","email":"bn@cs.stanford.edu"}],"directories":{}},"0.1.1":{"author":{"name":"Ben Newman","email":"bn@cs.stanford.edu"},"name":"private","description":"Utility for associating truly private state with any JavaScript object","keywords":["private","access control","access modifiers","encapsulation","secret","state","privilege","scope","es5"],"version":"0.1.1","homepage":"http://github.com/benjamn/private","repository":{"type":"git","url":"git://github.com/benjamn/private.git"},"main":"private.js","scripts":{"test":"node test/run.js"},"engines":{"node":">= 0.6"},"bugs":{"url":"https://github.com/benjamn/private/issues"},"_id":"private@0.1.1","dist":{"shasum":"cb2407f851db954e1446371a3cceaaea61831727","tarball":"https://registry.npmjs.org/private/-/private-0.1.1.tgz"},"_from":".","_npmVersion":"1.3.24","_npmUser":{"name":"benjamn","email":"bn@cs.stanford.edu"},"maintainers":[{"name":"benjamn","email":"bn@cs.stanford.edu"}],"directories":{}},"0.1.2":{"author":{"name":"Ben Newman","email":"bn@cs.stanford.edu"},"name":"private","description":"Utility for associating truly private state with any JavaScript object","keywords":["private","access control","access modifiers","encapsulation","secret","state","privilege","scope","es5"],"version":"0.1.2","homepage":"http://github.com/benjamn/private","repository":{"type":"git","url":"git://github.com/benjamn/private.git"},"main":"private.js","scripts":{"test":"node test/run.js"},"engines":{"node":">= 0.6"},"bugs":{"url":"https://github.com/benjamn/private/issues"},"_id":"private@0.1.2","dist":{"shasum":"8fdaf569fc1a28ce4fe6d1d50e2295d44d4c8c2b","tarball":"https://registry.npmjs.org/private/-/private-0.1.2.tgz"},"_from":".","_npmVersion":"1.3.15","_npmUser":{"name":"benjamn","email":"bn@cs.stanford.edu"},"maintainers":[{"name":"benjamn","email":"bn@cs.stanford.edu"}],"directories":{}},"0.1.3":{"author":{"name":"Ben Newman","email":"bn@cs.stanford.edu"},"name":"private","description":"Utility for associating truly private state with any JavaScript object","keywords":["private","access control","access modifiers","encapsulation","secret","state","privilege","scope","es5"],"version":"0.1.3","homepage":"http://github.com/benjamn/private","repository":{"type":"git","url":"git://github.com/benjamn/private.git"},"main":"private.js","scripts":{"test":"node test/run.js"},"engines":{"node":">= 0.6"},"bugs":{"url":"https://github.com/benjamn/private/issues"},"_id":"private@0.1.3","dist":{"shasum":"4c4f535caf4feaef58c2c9afaca6062b0caa61ac","tarball":"https://registry.npmjs.org/private/-/private-0.1.3.tgz"},"_from":".","_npmVersion":"1.4.3","_npmUser":{"name":"benjamn","email":"bn@cs.stanford.edu"},"maintainers":[{"name":"benjamn","email":"bn@cs.stanford.edu"}],"directories":{}},"0.1.4":{"author":{"name":"Ben Newman","email":"bn@cs.stanford.edu"},"name":"private","description":"Utility for associating truly private state with any JavaScript object","keywords":["private","access control","access modifiers","encapsulation","secret","state","privilege","scope","es5"],"version":"0.1.4","homepage":"http://github.com/benjamn/private","repository":{"type":"git","url":"git://github.com/benjamn/private.git"},"license":"MIT","main":"private.js","scripts":{"test":"node test/run.js"},"engines":{"node":">= 0.6"},"bugs":{"url":"https://github.com/benjamn/private/issues"},"_id":"private@0.1.4","dist":{"shasum":"d4f9b6ac89406eae9e415c2dc3e4f97dd1a47064","tarball":"https://registry.npmjs.org/private/-/private-0.1.4.tgz"},"_from":".","_npmVersion":"1.4.3","_npmUser":{"name":"benjamn","email":"bn@cs.stanford.edu"},"maintainers":[{"name":"benjamn","email":"bn@cs.stanford.edu"}],"directories":{}},"0.1.5":{"author":{"name":"Ben Newman","email":"bn@cs.stanford.edu"},"name":"private","description":"Utility for associating truly private state with any JavaScript object","keywords":["private","access control","access modifiers","encapsulation","secret","state","privilege","scope","es5"],"version":"0.1.5","homepage":"http://github.com/benjamn/private","repository":{"type":"git","url":"git://github.com/benjamn/private.git"},"license":"MIT","main":"private.js","scripts":{"test":"node test/run.js"},"engines":{"node":">= 0.6"},"bugs":{"url":"https://github.com/benjamn/private/issues"},"_id":"private@0.1.5","dist":{"shasum":"0683611864a4ec54224fd3049766fb42bcc55c83","tarball":"https://registry.npmjs.org/private/-/private-0.1.5.tgz"},"_from":".","_npmVersion":"1.4.3","_npmUser":{"name":"benjamn","email":"bn@cs.stanford.edu"},"maintainers":[{"name":"benjamn","email":"bn@cs.stanford.edu"}],"directories":{}},"0.1.6":{"author":{"name":"Ben Newman","email":"bn@cs.stanford.edu"},"name":"private","description":"Utility for associating truly private state with any JavaScript object","keywords":["private","access control","access modifiers","encapsulation","secret","state","privilege","scope","es5"],"version":"0.1.6","homepage":"http://github.com/benjamn/private","repository":{"type":"git","url":"git://github.com/benjamn/private.git"},"license":"MIT","main":"private.js","scripts":{"test":"node test/run.js"},"engines":{"node":">= 0.6"},"gitHead":"8df9e34c8f66a5e634a1024dba3ea16704d5178d","bugs":{"url":"https://github.com/benjamn/private/issues"},"_id":"private@0.1.6","_shasum":"55c6a976d0f9bafb9924851350fe47b9b5fbb7c1","_from":".","_npmVersion":"1.4.14","_npmUser":{"name":"benjamn","email":"bn@cs.stanford.edu"},"maintainers":[{"name":"benjamn","email":"bn@cs.stanford.edu"}],"dist":{"shasum":"55c6a976d0f9bafb9924851350fe47b9b5fbb7c1","tarball":"https://registry.npmjs.org/private/-/private-0.1.6.tgz"},"directories":{}},"0.1.7":{"author":{"name":"Ben Newman","email":"bn@cs.stanford.edu"},"name":"private","description":"Utility for associating truly private state with any JavaScript object","keywords":["private","access control","access modifiers","encapsulation","secret","state","privilege","scope","es5"],"version":"0.1.7","homepage":"http://github.com/benjamn/private","repository":{"type":"git","url":"git://github.com/benjamn/private.git"},"license":"MIT","main":"private.js","files":["private.js"],"scripts":{"test":"mocha --reporter spec --full-trace test/run.js"},"engines":{"node":">= 0.6"},"devDependencies":{"mocha":"^3.2.0"},"gitHead":"cf3166de8d96dae5bae641559ea1b4e4aab1c0d3","bugs":{"url":"https://github.com/benjamn/private/issues"},"_id":"private@0.1.7","_shasum":"68ce5e8a1ef0a23bb570cc28537b5332aba63ef1","_from":".","_npmVersion":"4.1.2","_nodeVersion":"4.7.3","_npmUser":{"name":"benjamn","email":"bn@cs.stanford.edu"},"maintainers":[{"name":"benjamn","email":"bn@cs.stanford.edu"}],"dist":{"shasum":"68ce5e8a1ef0a23bb570cc28537b5332aba63ef1","tarball":"https://registry.npmjs.org/private/-/private-0.1.7.tgz"},"_npmOperationalInternal":{"host":"packages-18-east.internal.npmjs.com","tmp":"tmp/private-0.1.7.tgz_1486316339625_0.9297276348806918"},"directories":{}}},"readme":"private [](https://travis-ci.org/benjamn/private)\n===\n\nA general-purpose utility for associating truly private state with any JavaScript object.\n\nInstallation\n---\n\nFrom NPM:\n\n npm install private\n\nFrom GitHub:\n\n cd path/to/node_modules\n git clone git://github.com/benjamn/private.git\n cd private\n npm install .\n\nUsage\n---\n**Get or create a secret object associated with any (non-frozen) object:**\n```js\nvar getSecret = require(\"private\").makeAccessor();\nvar obj = Object.create(null); // any kind of object works\ngetSecret(obj).totallySafeProperty = \"p455w0rd\";\n\nconsole.log(Object.keys(obj)); // []\nconsole.log(Object.getOwnPropertyNames(obj)); // []\nconsole.log(getSecret(obj)); // { totallySafeProperty: \"p455w0rd\" }\n```\nNow, only code that has a reference to both `getSecret` and `obj` can possibly access `.totallySafeProperty`.\n\n*Importantly, no global references to the secret object are retained by the `private` package, so as soon as `obj` gets garbage collected, the secret will be reclaimed as well. In other words, you don't have to worry about memory leaks.*\n\n**Create a unique property name that cannot be enumerated or guessed:**\n```js\nvar secretKey = require(\"private\").makeUniqueKey();\nvar obj = Object.create(null); // any kind of object works\n\nObject.defineProperty(obj, secretKey, {\n value: { totallySafeProperty: \"p455w0rd\" },\n enumerable: false // optional; non-enumerability is the default\n});\n\nObject.defineProperty(obj, \"nonEnumerableProperty\", {\n value: \"anyone can guess my name\",\n enumerable: false\n});\n\nconsole.log(obj[secretKey].totallySafeProperty); // p455w0rd\nconsole.log(obj.nonEnumerableProperty); // \"anyone can guess my name\"\nconsole.log(Object.keys(obj)); // []\nconsole.log(Object.getOwnPropertyNames(obj)); // [\"nonEnumerableProperty\"]\n\nfor (var key in obj) {\n console.log(key); // never called\n}\n```\nBecause these keys are non-enumerable, you can't discover them using a `for`-`in` loop. Because `secretKey` is a long string of random characters, you would have a lot of trouble guessing it. And because the `private` module wraps `Object.getOwnPropertyNames` to exclude the keys it generates, you can't even use that interface to discover it.\n\nUnless you have access to the value of the `secretKey` property name, there is no way to access the value associated with it. So your only responsibility as secret-keeper is to avoid handing out the value of `secretKey` to untrusted code.\n\nThink of this style as a home-grown version of the first style. Note, however, that it requires a full implementation of ES5's `Object.defineProperty` method in order to make any safety guarantees, whereas the first example will provide safety even in environments that do not support `Object.defineProperty`.\n\nRationale\n---\n\nIn JavaScript, the only data that are truly private are local variables\nwhose values do not *leak* from the scope in which they were defined.\n\nThis notion of *closure privacy* is powerful, and it readily provides some\nof the benefits of traditional data privacy, a la Java or C++:\n```js\nfunction MyClass(secret) {\n this.increment = function() {\n return ++secret;\n };\n}\n\nvar mc = new MyClass(3);\nconsole.log(mc.increment()); // 4\n```\nYou can learn something about `secret` by calling `.increment()`, and you\ncan increase its value by one as many times as you like, but you can never\ndecrease its value, because it is completely inaccessible except through\nthe `.increment` method. And if the `.increment` method were not\navailable, it would be as if no `secret` variable had ever been declared,\nas far as you could tell.\n\nThis style breaks down as soon as you want to inherit methods from the\nprototype of a class:\n```js\nfunction MyClass(secret) {\n this.secret = secret;\n}\n\nMyClass.prototype.increment = function() {\n return ++this.secret;\n};\n```\nThe only way to communicate between the `MyClass` constructor and the\n`.increment` method in this example is to manipulate shared properties of\n`this`. Unfortunately `this.secret` is now exposed to unlicensed\nmodification:\n```js\nvar mc = new MyClass(6);\nconsole.log(mc.increment()); // 7\nmc.secret -= Infinity;\nconsole.log(mc.increment()); // -Infinity\nmc.secret = \"Go home JavaScript, you're drunk.\";\nmc.increment(); // NaN\n```\nAnother problem with closure privacy is that it only lends itself to\nper-instance privacy, whereas the `private` keyword in most\nobject-oriented languages indicates that the data member in question is\nvisible to all instances of the same class.\n\nSuppose you have a `Node` class with a notion of parents and children:\n```js\nfunction Node() {\n var parent;\n var children = [];\n\n this.getParent = function() {\n return parent;\n };\n\n this.appendChild = function(child) {\n children.push(child);\n child.parent = this; // Can this be made to work?\n };\n}\n```\nThe desire here is to allow other `Node` objects to manipulate the value\nreturned by `.getParent()`, but otherwise disallow any modification of the\n`parent` variable. You could expose a `.setParent` function, but then\nanyone could call it, and you might as well give up on the getter/setter\npattern.\n\nThis module solves both of these problems.\n\nUsage\n---\n\nLet's revisit the `Node` example from above:\n```js\nvar p = require(\"private\").makeAccessor();\n\nfunction Node() {\n var privates = p(this);\n var children = [];\n\n this.getParent = function() {\n return privates.parent;\n };\n\n this.appendChild = function(child) {\n children.push(child);\n var cp = p(child);\n if (cp.parent)\n cp.parent.removeChild(child);\n cp.parent = this;\n return child;\n };\n}\n```\nNow, in order to access the private data of a `Node` object, you need to\nhave access to the unique `p` function that is being used here. This is\nalready an improvement over the previous example, because it allows\nrestricted access by other `Node` instances, but can it help with the\n`Node.prototype` problem too?\n\nYes it can!\n```js\nvar p = require(\"private\").makeAccessor();\n\nfunction Node() {\n p(this).children = [];\n}\n\nvar Np = Node.prototype;\n\nNp.getParent = function() {\n return p(this).parent;\n};\n\nNp.appendChild = function(child) {\n p(this).children.push(child);\n var cp = p(child);\n if (cp.parent)\n cp.parent.removeChild(child);\n cp.parent = this;\n return child;\n};\n```\nBecause `p` is in scope not only within the `Node` constructor but also\nwithin `Node` methods, we can finally avoid redefining methods every time\nthe `Node` constructor is called.\n\nNow, you might be wondering how you can restrict access to `p` so that no\nuntrusted code is able to call it. The answer is to use your favorite\nmodule pattern, be it CommonJS, AMD `define`, or even the old\nImmediately-Invoked Function Expression:\n```js\nvar Node = (function() {\n var p = require(\"private\").makeAccessor();\n\n function Node() {\n p(this).children = [];\n }\n\n var Np = Node.prototype;\n\n Np.getParent = function() {\n return p(this).parent;\n };\n\n Np.appendChild = function(child) {\n p(this).children.push(child);\n var cp = p(child);\n if (cp.parent)\n cp.parent.removeChild(child);\n cp.parent = this;\n return child;\n };\n\n return Node;\n}());\n\nvar parent = new Node;\nvar child = new Node;\nparent.appendChild(child);\nassert.strictEqual(child.getParent(), parent);\n```\nBecause this version of `p` never leaks from the enclosing function scope,\nonly `Node` objects have access to it.\n\nSo, you see, the claim I made at the beginning of this README remains\ntrue:\n\n> In JavaScript, the only data that are truly private are local variables\n> whose values do not *leak* from the scope in which they were defined.\n\nIt just so happens that closure privacy is sufficient to implement a\nprivacy model similar to that provided by other languages.\n","maintainers":[{"name":"benjamn","email":"bn@cs.stanford.edu"}],"time":{"modified":"2017-07-23T13:49:36.820Z","created":"2013-05-01T22:36:44.743Z","0.0.1":"2013-05-01T22:36:45.595Z","0.0.2":"2013-05-02T02:55:20.057Z","0.0.3":"2013-05-02T11:35:07.019Z","0.0.4":"2013-05-02T14:09:05.282Z","0.0.5":"2013-07-19T16:27:52.648Z","0.1.0":"2013-11-05T02:24:45.832Z","0.1.1":"2014-02-19T18:48:23.700Z","0.1.2":"2014-02-19T21:43:35.761Z","0.1.3":"2014-04-01T20:25:03.244Z","0.1.4":"2014-05-23T16:07:58.451Z","0.1.5":"2014-05-23T22:33:55.945Z","0.1.6":"2014-11-12T21:21:08.108Z","0.1.7":"2017-02-05T17:39:00.387Z"},"author":{"name":"Ben Newman","email":"bn@cs.stanford.edu"},"repository":{"type":"git","url":"git://github.com/benjamn/private.git"},"readmeFilename":"README.md","homepage":"http://github.com/benjamn/private","keywords":["private","access control","access modifiers","encapsulation","secret","state","privilege","scope","es5"],"bugs":{"url":"https://github.com/benjamn/private/issues"},"users":{"benjamn":true,"ziflex":true,"scottfreecode":true,"tzq1011":true,"iddan":true,"amir-arad":true},"license":"MIT","_attachments":{},"_etag":"W/\"5974a970-53a5\"","_lastModified":"Sun, 23 Jul 2017 13:49:36 GMT","_cached":true} |