+39
-1
| "use strict"; | ||
| const { EventEmitter } = require("node:events"); | ||
| const huffman = require("./lib/huffman"); | ||
@@ -14,4 +16,6 @@ const { getNormalizedParams } = require("./lib/formatters"); | ||
| static Wrapper = class { | ||
| static Wrapper = class extends EventEmitter { | ||
| constructor() { | ||
| super(); | ||
| return new Proxy(this, { | ||
@@ -22,2 +26,4 @@ map: new Map(), | ||
| const isEventSubscription = name === "on"; | ||
| if (name === "toJSON") { | ||
@@ -45,5 +51,21 @@ return target.constructor.toString(); | ||
| !isEventSubscription && | ||
| target.emit("call", { | ||
| key, | ||
| before: output, | ||
| after: response, | ||
| didHash: true, | ||
| }); | ||
| return response; | ||
| } | ||
| !isEventSubscription && | ||
| target.emit("call", { | ||
| key, | ||
| before: output, | ||
| after: output, | ||
| didHash: false, | ||
| }); | ||
| return output; | ||
@@ -60,5 +82,21 @@ }; | ||
| !isEventSubscription && | ||
| target.emit("access", { | ||
| key, | ||
| before: value, | ||
| after: response, | ||
| didHash: true, | ||
| }); | ||
| return response; | ||
| } | ||
| !isEventSubscription && | ||
| target.emit("access", { | ||
| key, | ||
| before: value, | ||
| after: value, | ||
| didHash: false, | ||
| }); | ||
| return value; | ||
@@ -65,0 +103,0 @@ }, |
+1
-1
| { | ||
| "name": "cipherio", | ||
| "version": "1.3.3", | ||
| "version": "1.4.3", | ||
| "description": "Make secure parts in shared code", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
+32
-0
@@ -60,2 +60,34 @@ # cipherio | ||
| Subscribe to events | ||
| ```js | ||
| const instance = new (class extends cipherio.Wrapper { | ||
| x = "abc"; | ||
| foo() { | ||
| return true; | ||
| } | ||
| })(); | ||
| instance.on("access", ({ before, after }) => { | ||
| console.log(before !== after); // true | ||
| }); | ||
| instance.x; | ||
| ``` | ||
| ```js | ||
| const instance = new (class extends cipherio.Wrapper { | ||
| x = "abc"; | ||
| foo() { | ||
| return true; | ||
| } | ||
| })(); | ||
| instance.on("call", ({ before, after }) => { | ||
| // ... | ||
| }); | ||
| instance.foo(); | ||
| ``` | ||
| ## Test | ||
@@ -62,0 +94,0 @@ |
10239
16.3%282
12.35%97
49.23%