virtual-proxy
Advanced tools
+58
-25
@@ -7,3 +7,3 @@ const { | ||
| Object: { hasOwn }, | ||
| TypeError, | ||
| Reflect, | ||
| Reflect: { | ||
@@ -64,7 +64,14 @@ isExtensible, | ||
| * $target: T, | ||
| * target: V, | ||
| * handler: import(".").VirtualHandler<V>, | ||
| * context: { | ||
| * target: V, | ||
| * handler: import(".").VirtualHandler<V>, | ||
| * TypeError: new (message: string) => unknown, | ||
| * }, | ||
| * ) => boolean} | ||
| */ | ||
| const preventExtensionHelper = (origin, $target, target, handler) => { | ||
| const preventExtensionHelper = ( | ||
| origin, | ||
| $target, | ||
| { target, handler, TypeError }, | ||
| ) => { | ||
| const keys = handler.ownKeys(target); | ||
@@ -119,3 +126,3 @@ for (let index = 0, length = keys.length; index < length; index++) { | ||
| isExtensible($target) { | ||
| const { handler, target } = this; | ||
| const { handler, target, TypeError } = this; | ||
| const extensible = handler.isExtensible(target); | ||
@@ -128,3 +135,3 @@ // a) | ||
| /* c8 ignore start */ | ||
| if (!preventExtensionHelper("isExtensible", $target, target, handler)) { | ||
| if (!preventExtensionHelper("isExtensible", $target, this)) { | ||
| throw new TypeError(format("isExtensible", "preventExtensions")); | ||
@@ -138,3 +145,3 @@ } | ||
| preventExtensions($target) { | ||
| const { handler, target } = this; | ||
| const { handler, target, TypeError } = this; | ||
| const success = handler.preventExtensions(target); | ||
@@ -144,5 +151,3 @@ // a) | ||
| /* c8 ignore start */ | ||
| if ( | ||
| !preventExtensionHelper("preventExtensions", $target, target, handler) | ||
| ) { | ||
| if (!preventExtensionHelper("preventExtensions", $target, this)) { | ||
| throw new TypeError(format("preventExtensions", "preventExtensions")); | ||
@@ -176,3 +181,3 @@ } | ||
| defineProperty($target, key, descriptor) { | ||
| const { handler, target } = this; | ||
| const { handler, target, TypeError } = this; | ||
| // b) + c) + d) | ||
@@ -210,3 +215,3 @@ const success = handler.defineProperty(target, key, descriptor); | ||
| getOwnPropertyDescriptor($target, key) { | ||
| const { handler, target } = this; | ||
| const { handler, target, TypeError } = this; | ||
| const descriptor = handler.getOwnPropertyDescriptor(target, key); | ||
@@ -234,3 +239,3 @@ // c) | ||
| deleteProperty($target, key) { | ||
| const { handler, target } = this; | ||
| const { handler, target, TypeError } = this; | ||
| const success = handler.deleteProperty(target, key); | ||
@@ -254,3 +259,3 @@ // b) | ||
| ownKeys($target) { | ||
| const { handler, target } = this; | ||
| const { handler, target, TypeError } = this; | ||
| const keys = handler.ownKeys(target); | ||
@@ -280,3 +285,3 @@ // f) | ||
| has($target, key) { | ||
| const { handler, target } = this; | ||
| const { handler, target, TypeError } = this; | ||
| const existent = handler.has(target, key); | ||
@@ -339,5 +344,8 @@ // b) | ||
| * handler: ProxyHandler<V>, | ||
| * options?: { | ||
| * TypeError?: new (message: string) => unknown, | ||
| * }, | ||
| * ) => import(".").ActualHandler<V, T>} | ||
| */ | ||
| export const setupVirtualHandler = (target, handler) => | ||
| export const setupVirtualHandler = (target, handler, options) => | ||
| /** @type {any} */ ({ | ||
@@ -347,2 +355,6 @@ __proto__: actual_handler_prototype, | ||
| handler: toVirtualHandler(handler), | ||
| TypeError: | ||
| options && hasOwn(options, "TypeError") | ||
| ? options.TypeError | ||
| : globalThis.TypeError, | ||
| }); | ||
@@ -355,6 +367,9 @@ | ||
| * handler: ProxyHandler<V>, | ||
| * options?: { | ||
| * TypeError?: new (message: string) => unknown, | ||
| * }, | ||
| * ) => T} | ||
| */ | ||
| export const VirtualProxy = function (integrity, target, handler) { | ||
| return new Proxy(integrity, setupVirtualHandler(target, handler)); | ||
| export const VirtualProxy = function (integrity, target, handler, options) { | ||
| return new Proxy(integrity, setupVirtualHandler(target, handler, options)); | ||
| }; | ||
@@ -367,6 +382,14 @@ | ||
| * handler: ProxyHandler<V>, | ||
| * options?: { | ||
| * TypeError?: new (message: string) => unknown, | ||
| * } | ||
| * ) => { proxy: T, revoke: () => void }} | ||
| */ | ||
| export const RevocableVirtualProxy = function (integrity, target, handler) { | ||
| return revocable(integrity, setupVirtualHandler(target, handler)); | ||
| export const RevocableVirtualProxy = function ( | ||
| integrity, | ||
| target, | ||
| handler, | ||
| options, | ||
| ) { | ||
| return revocable(integrity, setupVirtualHandler(target, handler, options)); | ||
| }; | ||
@@ -378,6 +401,9 @@ | ||
| * handler: ProxyHandler<V>, | ||
| * options?: { | ||
| * TypeError?: new (message: string) => unknown, | ||
| * }, | ||
| * ) => object} | ||
| */ | ||
| export const VirtualObject = function (virtual, handler) { | ||
| return VirtualProxy({ __proto__: null }, virtual, handler); | ||
| export const VirtualObject = function (virtual, handler, options) { | ||
| return VirtualProxy({ __proto__: null }, virtual, handler, options); | ||
| }; | ||
@@ -389,6 +415,9 @@ | ||
| * handler: ProxyHandler<V>, | ||
| * options?: { | ||
| * TypeError?: new (message: string) => unknown, | ||
| * }, | ||
| * ) => any[]} | ||
| */ | ||
| export const VirtualArray = function (virtual, handler) { | ||
| return VirtualProxy([], virtual, handler); | ||
| export const VirtualArray = function (virtual, handler, options) { | ||
| return VirtualProxy([], virtual, handler, options); | ||
| }; | ||
@@ -400,5 +429,8 @@ | ||
| * handler: ProxyHandler<V>, | ||
| * options?: { | ||
| * TypeError?: new (message: string) => unknown, | ||
| * }, | ||
| * ) => Function} | ||
| */ | ||
| export const VirtualFunction = function (virtual, handler) { | ||
| export const VirtualFunction = function (virtual, handler, options) { | ||
| return VirtualProxy( | ||
@@ -412,3 +444,4 @@ /* c8 ignore start */ | ||
| handler, | ||
| options, | ||
| ); | ||
| }; |
+5
-2
| { | ||
| "name": "virtual-proxy", | ||
| "description": "More permisive proxy objects by separating invariant bookkeeping from the target object.", | ||
| "version": "0.1.5", | ||
| "version": "0.2.0", | ||
| "author": { | ||
@@ -19,2 +19,3 @@ "name": "Laurent Christophe", | ||
| "scripts": { | ||
| "lint": "npx tsc && npx eslint && npx prettier --check .", | ||
| "test": "node test/test.mjs", | ||
@@ -32,5 +33,7 @@ "test-cov": "npx c8 -- node test/test.mjs" | ||
| "c8": "^10.1.3", | ||
| "eslint": "^9.20.0", | ||
| "prettier": "^3.4.2", | ||
| "typescript": "^5.7.2" | ||
| "typescript": "^5.7.2", | ||
| "typescript-eslint": "^8.23.0" | ||
| } | ||
| } |
+20
-0
@@ -0,1 +1,9 @@ | ||
| export type Options = { | ||
| /** | ||
| * The constructor to use to create a TypeError. Useful when dealing with | ||
| * multiple realms to ensure errors are created in the correct realm. | ||
| */ | ||
| TypeError?: new (message: string) => unknown; | ||
| }; | ||
| /** | ||
@@ -13,2 +21,3 @@ * Main export of the library, it creates an handler object that should be | ||
| * a regular proxy handler object. | ||
| * @param options Optional object to configure the actual handler object. | ||
| * @returns An (actual) handler object that should be passed to the proxy | ||
@@ -20,2 +29,3 @@ * constructor. | ||
| handler: ProxyHandler<V>, | ||
| options?: Options, | ||
| ) => ProxyHandler<T>; | ||
@@ -32,2 +42,3 @@ | ||
| * a regular proxy handler object. | ||
| * @param options Optional object to configure the actual handler object. | ||
| * @returns A virtual proxy object. | ||
@@ -39,2 +50,3 @@ */ | ||
| handler: ProxyHandler<V>, | ||
| options?: Options, | ||
| ) => T; | ||
@@ -48,2 +60,3 @@ | ||
| * @param handler The virtual handler object. | ||
| * @param options Optional object to configure the actual handler object. | ||
| * @returns Both a virtual proxy object and a function to revoke the proxy. | ||
@@ -55,2 +68,3 @@ */ | ||
| handler: ProxyHandler<V>, | ||
| options?: Options, | ||
| ) => { proxy: T; revoke: () => void }; | ||
@@ -62,2 +76,3 @@ | ||
| * @param handler The virtual handler object. | ||
| * @param options Optional object to configure the actual handler object. | ||
| * @returns A plain virtual proxy object. | ||
@@ -68,2 +83,3 @@ */ | ||
| handler: ProxyHandler<V>, | ||
| options?: Options, | ||
| ) => T; | ||
@@ -75,2 +91,3 @@ | ||
| * @param handler The virtual handler object. | ||
| * @param options Optional object to configure the actual handler object. | ||
| * @returns A virtual proxy array. | ||
@@ -81,2 +98,3 @@ */ | ||
| handler: ProxyHandler<V>, | ||
| options?: Options, | ||
| ) => T; | ||
@@ -88,2 +106,3 @@ | ||
| * @param handler The virtual handler object. | ||
| * @param options Optional object to configure the actual handler object. | ||
| * @returns A virtual proxy function. | ||
@@ -94,2 +113,3 @@ */ | ||
| handler: ProxyHandler<V>, | ||
| options?: Options, | ||
| ) => T; |
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
21612
8.94%509
11.38%6
50%