js-proxy
Advanced tools
Comparing version 0.1.0 to 0.1.1
119
cjs/index.js
'use strict'; | ||
Object.defineProperty(exports, '__esModule', {value: true}).default = () => {}; | ||
const { ARRAY, FUNCTION, NULL, OBJECT, UNDEFINED } = require('proxy-target/types'); | ||
const handlerTraps = require('proxy-target/traps'); | ||
const { bound } = require('proxy-target'); | ||
const { create, drop } = require('gc-hook'); | ||
const { Object, Proxy, Reflect } = globalThis; | ||
const { isArray } = Array; | ||
const { ownKeys } = Reflect; | ||
const { create: extend, hasOwn, values } = Object; | ||
const traps = new Set([...values(handlerTraps)]); | ||
const typesOf = new WeakMap; | ||
const direct = Symbol(); | ||
const extendHandler = (handler, type, value) => { | ||
const descriptors = { type: { value: type } }; | ||
const hasValueOf = hasOwn(handler, 'valueOf'); | ||
for(const trap of traps) { | ||
let descriptor = value(handler[trap] || Reflect[trap]); | ||
if (hasValueOf && trap === handlerTraps.GET) { | ||
const { valueOf } = handler; | ||
const { value } = descriptor; | ||
descriptor = { | ||
value($, s, ..._) { | ||
return s === direct ? | ||
valueOf.call(this, $) : | ||
value.call(this, $, s, ..._); | ||
} | ||
}; | ||
} | ||
descriptors[trap] = descriptor; | ||
} | ||
return extend(handler, descriptors); | ||
}; | ||
const proxy = ($, target, handler, token = $) => { | ||
if (token === $) { | ||
switch (typeof $) { | ||
case OBJECT: | ||
case FUNCTION: | ||
case UNDEFINED: break; | ||
default: { | ||
token = false; | ||
if (target === $) target = Object($); | ||
} | ||
} | ||
} | ||
const p = new Proxy(target, handler); | ||
const { destruct } = handler; | ||
return destruct ? create($, destruct, { token, return: p }) : p; | ||
}; | ||
const proxyOf = namespace => { | ||
const proxies = { free: token => drop(token) }; | ||
for (const type of ownKeys(namespace)) { | ||
const traps = namespace[type]; | ||
switch (type) { | ||
case ARRAY: { | ||
const handler = extendHandler(traps, type, method => ({ | ||
value([ $ ], ..._) { | ||
return method.call(this, $, ..._); | ||
} | ||
})); | ||
proxies[type] = ($, ..._) => proxy($, [ $ ], handler, ..._); | ||
break; | ||
} | ||
case FUNCTION: { | ||
const handler = extendHandler(traps, type, method => ({ | ||
value($, ..._) { | ||
return method.call(this, $(), ..._); | ||
} | ||
})); | ||
proxies[type] = ($, ..._) => proxy($, bound($), handler, ..._); | ||
break; | ||
} | ||
case OBJECT: { | ||
const handler = extendHandler(traps, type, method => ({ | ||
value({ $ }, ..._) { | ||
return method.call(this, $, ..._); | ||
} | ||
})); | ||
proxies[type] = ($, ..._) => proxy($, { $ }, handler, ..._); | ||
break; | ||
} | ||
default: { | ||
const handler = extendHandler(traps, type, value => ({ value })); | ||
proxies[type] = ($, ..._) => { | ||
const p = proxy($, $, handler, ..._); | ||
typesOf.set(p, type); | ||
return p; | ||
}; | ||
break; | ||
} | ||
} | ||
} | ||
return proxies; | ||
}; | ||
exports.proxyOf = proxyOf; | ||
const typeOf = value => { | ||
const type = typeof value; | ||
return type === OBJECT ? | ||
(value ? | ||
(typesOf.get(value) || (isArray(value) ? ARRAY : OBJECT)) : | ||
NULL | ||
) : | ||
type; | ||
}; | ||
exports.typeOf = typeOf; | ||
/** | ||
* @template T the value held by the proxy | ||
* @param {T} value a proxied value or a regular | ||
* @returns {T} | ||
*/ | ||
const valueOf = value => (value[direct] || value.valueOf()); | ||
exports.valueOf = valueOf; |
116
esm/index.js
@@ -1,1 +0,115 @@ | ||
export default () => {}; | ||
import { ARRAY, FUNCTION, NULL, OBJECT, UNDEFINED } from 'proxy-target/types'; | ||
import * as handlerTraps from 'proxy-target/traps'; | ||
import { bound } from 'proxy-target'; | ||
import { create, drop } from 'gc-hook'; | ||
const { Object, Proxy, Reflect } = globalThis; | ||
const { isArray } = Array; | ||
const { ownKeys } = Reflect; | ||
const { create: extend, hasOwn, values } = Object; | ||
const traps = new Set([...values(handlerTraps)]); | ||
const typesOf = new WeakMap; | ||
const direct = Symbol(); | ||
const extendHandler = (handler, type, value) => { | ||
const descriptors = { type: { value: type } }; | ||
const hasValueOf = hasOwn(handler, 'valueOf'); | ||
for(const trap of traps) { | ||
let descriptor = value(handler[trap] || Reflect[trap]); | ||
if (hasValueOf && trap === handlerTraps.GET) { | ||
const { valueOf } = handler; | ||
const { value } = descriptor; | ||
descriptor = { | ||
value($, s, ..._) { | ||
return s === direct ? | ||
valueOf.call(this, $) : | ||
value.call(this, $, s, ..._); | ||
} | ||
}; | ||
} | ||
descriptors[trap] = descriptor; | ||
} | ||
return extend(handler, descriptors); | ||
}; | ||
const proxy = ($, target, handler, token = $) => { | ||
if (token === $) { | ||
switch (typeof $) { | ||
case OBJECT: | ||
case FUNCTION: | ||
case UNDEFINED: break; | ||
default: { | ||
token = false; | ||
if (target === $) target = Object($); | ||
} | ||
} | ||
} | ||
const p = new Proxy(target, handler); | ||
const { destruct } = handler; | ||
return destruct ? create($, destruct, { token, return: p }) : p; | ||
}; | ||
export const proxyOf = namespace => { | ||
const proxies = { free: token => drop(token) }; | ||
for (const type of ownKeys(namespace)) { | ||
const traps = namespace[type]; | ||
switch (type) { | ||
case ARRAY: { | ||
const handler = extendHandler(traps, type, method => ({ | ||
value([ $ ], ..._) { | ||
return method.call(this, $, ..._); | ||
} | ||
})); | ||
proxies[type] = ($, ..._) => proxy($, [ $ ], handler, ..._); | ||
break; | ||
} | ||
case FUNCTION: { | ||
const handler = extendHandler(traps, type, method => ({ | ||
value($, ..._) { | ||
return method.call(this, $(), ..._); | ||
} | ||
})); | ||
proxies[type] = ($, ..._) => proxy($, bound($), handler, ..._); | ||
break; | ||
} | ||
case OBJECT: { | ||
const handler = extendHandler(traps, type, method => ({ | ||
value({ $ }, ..._) { | ||
return method.call(this, $, ..._); | ||
} | ||
})); | ||
proxies[type] = ($, ..._) => proxy($, { $ }, handler, ..._); | ||
break; | ||
} | ||
default: { | ||
const handler = extendHandler(traps, type, value => ({ value })); | ||
proxies[type] = ($, ..._) => { | ||
const p = proxy($, $, handler, ..._); | ||
typesOf.set(p, type); | ||
return p; | ||
}; | ||
break; | ||
} | ||
} | ||
} | ||
return proxies; | ||
}; | ||
export const typeOf = value => { | ||
const type = typeof value; | ||
return type === OBJECT ? | ||
(value ? | ||
(typesOf.get(value) || (isArray(value) ? ARRAY : OBJECT)) : | ||
NULL | ||
) : | ||
type; | ||
}; | ||
/** | ||
* @template T the value held by the proxy | ||
* @param {T} value a proxied value or a regular | ||
* @returns {T} | ||
*/ | ||
export const valueOf = value => (value[direct] || value.valueOf()); |
{ | ||
"name": "js-proxy", | ||
"version": "0.1.0", | ||
"description": "", | ||
"version": "0.1.1", | ||
"description": "The one-stop shop solution for JS Proxies and FFI APIs", | ||
"main": "./cjs/index.js", | ||
@@ -9,6 +9,11 @@ "scripts": { | ||
"cjs": "ascjs esm cjs", | ||
"coveralls": "c8 report --reporter=text-lcov | coveralls", | ||
"test": "c8 node test/index.js" | ||
"test": "c8 node --expose-gc test/index.js", | ||
"coverage": "mkdir -p ./coverage; c8 report --reporter=text-lcov > ./coverage/lcov.info" | ||
}, | ||
"keywords": [], | ||
"keywords": [ | ||
"proxy", | ||
"ffi", | ||
"gc", | ||
"destruct" | ||
], | ||
"author": "Andrea Giammarchi", | ||
@@ -18,4 +23,3 @@ "license": "MIT", | ||
"ascjs": "^6.0.3", | ||
"c8": "^9.1.0", | ||
"coveralls": "^3.1.1" | ||
"c8": "^9.1.0" | ||
}, | ||
@@ -29,2 +33,14 @@ "module": "./esm/index.js", | ||
}, | ||
"./heap": { | ||
"import": "./esm/heap.js", | ||
"default": "./cjs/heap.js" | ||
}, | ||
"./traps": { | ||
"import": "./esm/traps.js", | ||
"default": "./cjs/traps.js" | ||
}, | ||
"./types": { | ||
"import": "./esm/types.js", | ||
"default": "./cjs/types.js" | ||
}, | ||
"./package.json": "./package.json" | ||
@@ -34,4 +50,4 @@ }, | ||
"gc-hook": "^0.3.1", | ||
"proxy-target": "^3.0.1" | ||
"proxy-target": "^3.0.2" | ||
} | ||
} |
@@ -1,3 +0,9 @@ | ||
# js-proxy | ||
# JS Proxy | ||
Coming soon ... | ||
[![build status](https://github.com/WebReflection/js-proxy/actions/workflows/node.js.yml/badge.svg)](https://github.com/WebReflection/js-proxy/actions) [![Coverage Status](https://coveralls.io/repos/github/WebReflection/js-proxy/badge.svg?branch=main)](https://coveralls.io/github/WebReflection/js-proxy?branch=main) | ||
<sup>**Social Media Photo by [Vinu T](https://unsplash.com/@happy_pixel?utm_content=creditCopyText&utm_medium=referral&utm_source=unsplash) on [Unsplash](https://unsplash.com/photos/a-small-waterfall-in-the-middle-of-a-forest-DHo1nNUI0y4?utm_content=creditCopyText&utm_medium=referral&utm_source=unsplash)**</sup> | ||
The one-stop shop solution for JS Proxies and FFI APIs. | ||
## ... coming soon ... |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Trivial Package
Supply chain riskPackages less than 10 lines of code are easily copied into your own project and may not warrant the additional supply chain risk of an external dependency.
Found 1 instance in 1 package
12780
2
12
318
10
1
Updatedproxy-target@^3.0.2