
Security News
Crates.io Users Targeted by Phishing Emails
The Rust Security Response WG is warning of phishing emails from rustfoundation.dev targeting crates.io users.
micro-observer
Advanced tools
Utilizes ES6 Proxies to easily observe and validate changes on nested objects.
micro-observer is a micro-library for TypeScript and/or JavaScript that allows you to easily observe and validate changes on nested objects using ES6 Proxies.
The primary goal of this library is to intercept and report the changes that occur on a nested data structure in a clear and concise manner. In many ways, it is essentially a "nested proxy", but the goal is not necessarily to emulate the Proxy API in its entirety.
Demo: tannernielsen.com/micro-observer-demo.html
Via npm:
$ npm install micro-observer
Via yarn:
$ yarn add micro-observer
Via cdn:
<script src="https://cdn.jsdelivr.net/npm/micro-observer/dist/observer.js"></script>
You can also change it to .min.js
for a minified version.
Creating a nested proxy in TypeScript:
// Importing ChangeReport is optional -- it just allows your IDE to provide useful hints
import {Observer, ChangeReport} from 'micro-observer';
let data = {list: [1, 2, 3], nested: {prop: 'value'}};
let proxy = Observer.create(data, (change: ChangeReport) => {
console.log(change);
return true;
});
Or in JavaScript:
let Observer = require('micro-observer').Observer;
let data = {list: [1, 2, 3], nested: {prop: 'value'}};
let proxy = Observer.create(data, function(change) {
console.log(change);
return true;
});
Making changes and viewing the reports:
proxy.nested.prop = 'new value';
// {type: 'set-prop', path: 'nested.prop', property: 'prop', newValue: 'new value', target: {prop: 'value'}}
proxy.list.push(4);
// {type: 'function-call', path: 'list', property: 'list', function: 'push', arguments: [4], target: [1, 2, 3]}
The micro-observer API is very simple, as only one function is exported:
Creates a "nested proxy" to observe the given data. Every modification that is attempted through the proxy is summarized as a ChangeReport (explained in greater detail below) and passed to the validator to determine if it should be accepted.
An object that describes a change made somewhere within the data. (Note that this is merely a TypeScript type definition, so it is not instantiatable.) Each one features the following properties:
let data = {
someProp: 'value',
unprotectedProp: 'value',
$protectedProp: 'protected value',
nested: {
someProp: 'value',
objects: [
{name: 'Bob', age: 40}, {name: 'Mike', age: 28}
],
nested: {
prop1: 1,
prop2: 2
}
}
};
let proxy = Observer.create(data, function(change){
console.log(change);
// Protect properties that start with '$'
if (change.property.startsWith('$')) return false;
else return true;
});
proxy.someProp = 'new val';
// {type: 'set-prop', path: 'someProp', property: 'someProp', newValue: 'new val', target: {someProp: 'value', unprotectedProp: ...}}
delete proxy.unprotectedProp;
// {type: 'delete-prop', path: 'unprotectedProp', property: 'unprotectedProp', target: {someProp: 'new val', unprotectedProp: ...}}
console.log(proxy.unprotectedProp);
// undefined
delete proxy.$protectedProp;
// {type: 'delete-prop', path: '$unprotectedProp', property: '$unprotectedProp', target: {someProp: 'new val', $protectedProp: ...}}
console.log(proxy.$protectedProp);
// 'protected value'
proxy.nested.someProp = 'NEW val';
// {type: 'set-prop', path: 'nested.someProp', property: 'someProp', newValue: 'NEW val', target: {someProp: 'value', objects: [...], ...}}
proxy.nested.objects.push({name: 'Mitch', age: 54});
// {type: 'function-call', path: 'nested.objects', property: 'objects', function: 'push', arguments: [{name: 'Mitch', age: 54}], target: [{name: 'Bob', age: 45}, ...]}
proxy.nested.nested.prop3 = 3;
// {type: 'set-prop', path: 'nested.nested.prop3', property: 'prop3', newValue: 3, target: {prop1: 1, prop2: 2}}
Contributions are always welcome! Just be sure to run yarn lint
and yarn test
before submitting a pull
request.
Tanner Nielsen © 2020
FAQs
Utilizes ES6 Proxies to easily observe and validate changes on nested objects.
We found that micro-observer demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The Rust Security Response WG is warning of phishing emails from rustfoundation.dev targeting crates.io users.
Product
Socket now lets you customize pull request alert headers, helping security teams share clear guidance right in PRs to speed reviews and reduce back-and-forth.
Product
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.