All Projects → Palindrom → Jsonpatcherproxy

Palindrom / Jsonpatcherproxy

ES6 proxy powered JSON Object observer that emits JSON patches when changes occur to your object tree.

Programming Languages

javascript
184084 projects - #8 most used programming language

Labels

Projects that are alternatives of or similar to Jsonpatcherproxy

Mr2
Mr.2 can help you expose local server to external network. Support both TCP/UDP, of course support HTTP. Zero-Configuration.
Stars: ✭ 1,102 (+1260.49%)
Mutual labels:  proxy
Adblock Proxy
💊 (Archived) AdBlock Proxy with support for AdBlock Plus Filter Lists and Host files
Stars: ✭ 68 (-16.05%)
Mutual labels:  proxy
Foxman
🍥 an extensible mock server
Stars: ✭ 76 (-6.17%)
Mutual labels:  proxy
Hproxy
hproxy - Asynchronous IP proxy pool, aims to make getting proxy as convenient as possible.(异步爬虫代理池)
Stars: ✭ 62 (-23.46%)
Mutual labels:  proxy
Jladder
基于Java Netty开发的翻墙程序,支持Socks协议和Http协议翻墙。
Stars: ✭ 68 (-16.05%)
Mutual labels:  proxy
Tor Router
A SOCKS, HTTP and DNS proxy for distributing traffic across multiple instances of Tor
Stars: ✭ 69 (-14.81%)
Mutual labels:  proxy
Snmpfwd
SNMP Proxy Forwarder
Stars: ✭ 58 (-28.4%)
Mutual labels:  proxy
Docker Nginx Image Proxy
on the fly image cropping with gravity, resize and compression microservice
Stars: ✭ 79 (-2.47%)
Mutual labels:  proxy
Proxify
Swiss Army knife Proxy tool for HTTP/HTTPS traffic capture, manipulation, and replay on the go.
Stars: ✭ 1,153 (+1323.46%)
Mutual labels:  proxy
Whistle
HTTP, HTTP2, HTTPS, Websocket debugging proxy
Stars: ✭ 9,683 (+11854.32%)
Mutual labels:  proxy
Proxy List
A list of free, public, forward proxy servers. UPDATED DAILY!
Stars: ✭ 1,125 (+1288.89%)
Mutual labels:  proxy
Simplify Core
Simplify 为简化重复的JAVA代码而生,基于JDK8,无其它jar包依赖,提供序列化,json parse/generator,日期处理,asm && jdkproxy 实现动态代理功能 等常见操作。
Stars: ✭ 65 (-19.75%)
Mutual labels:  proxy
Tcptunnel
将本地内网服务器映射到公网。
Stars: ✭ 72 (-11.11%)
Mutual labels:  proxy
Vkrss
Generates RSS feed of opened/closed vk.com wall or global searched opened posts. Features: post filtering (include/exclude by regexp and/or by owner type), ads skipping, automatic title generation, hash-tags extraction as RSS categories, initial author extraction, HTML formatting
Stars: ✭ 59 (-27.16%)
Mutual labels:  proxy
Bdtunnel
BoutDuTunnel is able to create virtual connections tunnelled in HTTP requests.
Stars: ✭ 78 (-3.7%)
Mutual labels:  proxy
Infrared
An ultra lightweight minecraft reverse proxy and idle placeholder
Stars: ✭ 59 (-27.16%)
Mutual labels:  proxy
Go Proxy
用Go写一个内网穿透工具
Stars: ✭ 68 (-16.05%)
Mutual labels:  proxy
Venom
Venom - A Multi-hop Proxy for Penetration Testers
Stars: ✭ 1,228 (+1416.05%)
Mutual labels:  proxy
Lightsocks Nodejs
It's a simple socks5 proxy tool which based on lightsocks
Stars: ✭ 79 (-2.47%)
Mutual labels:  proxy
Free proxy ss
分享来自互联网上免费的shadowsocks(SS)/ShadowsocksR(SSR)/V2ray(vmess)代理 每15分钟更新一次,每次各分享4个临时可用代理。 及时订阅、及时更新。
Stars: ✭ 72 (-11.11%)
Mutual labels:  proxy

JSONPatcherProxy

JSONPatcherProxy


ES6 proxy powered JSON Object observer that emits JSON patches when changes occur to your object tree.

Build Status

With JSONPatcherProxy, you don't need polling or dirty checking. Any changes to the object are synchronously emitted.

Why you should use JSONPatcherProxy

JSON-Patch (RFC6902) is a standard format that allows you to update a JSON document by sending the changes rather than the whole document. JSONPatcherProxy plays well with the HTTP PATCH verb (method) and REST style programming.

Mark Nottingham has a nice blog about it.

Footprint

  • 1.30 KB minified and gzipped (3.25 KB minified)

Features

  • Allows you to watch an object and record all patches for later usage.
  • Allows you to watch an object and handle individual patches synchronously through a callback.
  • Tested in Edge, Firefox, Chrome, Safari and Node.js.

Install

Install the current version (and save it as a dependency):

npm

$ npm install jsonpatcherproxy --save

Download as ZIP

Adding to your project

In a web browser

Load the bundled distribution script:

<script src="dist/jsonpatcherproxy.min.js"></script>

In browsers that support ECMAScript modules, the below code uses this library as a module:

<script type="module">
  import { JSONPatcherProxy } from 'jsonpatcherproxy/src/jsonpatcherproxy.mjs';
</script>

You can use rawgit.com as a CDN.

In Node.js

import { JSONPatcherProxy } from 'jsonpatcherproxy';

Usage

Generating patches:

var myObj = { firstName:"Joachim", lastName:"Wester", contactDetails: { phoneNumbers: [ { number:"555-123" }] } };
var jsonPatcherProxy = new JSONPatcherProxy( myObj );
var observedObject = jsonPatcherProxy.observe(true);
observedObject.firstName = "Albert";
observedObject.contactDetails.phoneNumbers[0].number = "123";
observedObject.contactDetails.phoneNumbers.push({number:"456"});
var patches = jsonPatcherProxy.generate();
// patches  == [
//   { op:"replace", path="/firstName", value:"Albert"},
//   { op:"replace", path="/contactDetails/phoneNumbers/0/number", value:"123"},
//   { op:"add", path="/contactDetails/phoneNumbers/1", value:{number:"456"}}];

Receiving patches in a callback:

var myObj = { firstName:"Joachim", lastName:"Wester", contactDetails: { phoneNumbers: [ { number:"555-123" }] } };
var jsonPatcherProxy = new JSONPatcherProxy( myObj );
var observedObject = jsonPatcherProxy.observe(true, function(patch) {
    // patch == { op:"replace", path="/firstName", value:"Albert"}
});
observedObject.firstName = "Albert";

API

Object observing

constructor: JSONPatcherProxy( root Object, [showDetachedWarning Boolean = true] ): JSONPatcherProxy

Creates an instance of JSONPatcherProxy around your object of interest root, for later observe, unobserve, pause, resume calls. Returns JSONPatcherProxy.

root: The object tree you want to observe

showDetachedWarning: Modifying a child object that is detached from the parent tree is not recommended, because the object will continue to be a Proxy. That's why JSONPatcherProxy warns when a detached proxy is accessed. You can set this to false to disable those warnings.

JSONPatcherProxy#observe(record Boolean, [callback Function]): Proxy

Sets up a deep proxy observer on root that listens for changes in the tree. When changes are detected, the optional callback is called with the generated single patch as the parameter.

record: if set to false, all changes are will be pass through the callback and no history will be kept. If set to true patches history will be kept until you call generate, this will return several patches and deletes them from history.

Returns a Proxy mirror of your object.

  • Note 1: you must either set record to true or pass a callback.
  • Note 2: you have to use the return value of this function as your object of interest. Changes to the original object will go unnoticed.
  • Note 3: please make sure to call JSONPatcherProxy#generate often if you choose to record. Because the patches will accumulate if you don't.

🚨 Generated patches are not immutable. See "Limitations" below.

JSONPatcherProxy#generate () : Array

It returns the changes of your object since the last time it's called. You have to be recording (by setting record to true) when calling JSONPatcherProxy#observe.

If there are no pending changes in root, returns an empty array (length 0).

🚨 Generated patches are not immutable. See "Limitations" below.

JSONPatcherProxy#pause () : void

Disables patches emitting (to both callback and patches array). However, the object will be updated if you change it.

JSONPatcherProxy#resume () : void

Enables patches emitting (to both callback and patches array). Starting from the moment you call it.

JSONPatcherProxy#revoke () : void

De-proxifies (revokes) all the proxies that were created either in #observe call or by adding sub-objects to the tree in runtime.

JSONPatcherProxy#disableTraps () : void

Turns the proxified object into a forward-proxy object; doesn't emit any patches anymore, like a normal object.

undefineds (JS to JSON projection)

As undefined type does not exist in JSON, it's also not a valid value of JSON Patch operation. Therefore JSONPatcherProxy will not generate JSON Patches that sets anything to undefined.

Whenever a value is set to undefined in JS, JSONPatcherProxy method generate and compare will treat it similarly to how JavaScript method JSON.stringify (MDN) treats them:

If undefined (...) is encountered during conversion it is either omitted (when it is found in an object) or censored to null (when it is found in an array).

See the ECMAScript spec for details.

Limitations

  1. It mutates your original object: During proxification, JSONPatcherProxy mutates the object you pass to it. Because it doesn't deep-clone for better performance. If you want your original object to remain untouched, please deep-clone before you pass it to the constructor.

  2. It does not support multi-level proxification: During proxification, it recursively traverses the object and sets each property to its new proxified version. This means you can't proxify an already proxified object because the original proxy will record proxification as a series of changes. And the emitted patches are unpredictable. Also, when you change a property from either one of the proxies, both of the proxies will emit patches in an undefined manner.

  3. Generated patches are not immutable. The patches generated by JSONPatcherProxy contain reference to the profixied object as the patch value. You should either serialize the patch to JSON immediately or deep clone the value it if you want to process it later. If you don't do that, you run the risk that reading the patch will contain changes added after the patch was generated.

  4. JSONPatcherProxy might not generate patches for changes of integers above Number.MAX_SAFE_INTEGER, or any other changes beyond the precision of JavaScript double-precision floating-point. To reduce patch generation noise, JSONPatcherProxy compares if the new value is the same (===) as the previous one. If so, then no patch is generated. Therefore, a change from Number.MAX_SAFE_INTEGER + 1 to Number.MAX_SAFE_INTEGER + 2, or from 0.02 to 0.020000000000000001 will not be effectively observed.

Specs/tests

In browser

Go to /test

In Node

Run:

npm test

Benchmarking

When you run npm run bench, few things happen:

  1. Five benchmark specs defined in proxyBenchmark.js are executed. This might take a minute.
  2. The results are appended to the benchmark.tsv file, including the following information for each spec:
    • current Git commit SHA
    • number of operations per second
    • name of the spec
  3. At the end, in your console you will see the detailed information about the current benchmark, and the comparison of all data found in benchmark.tsv relatively to the first results for each given spec name, e.g.:
Observe and generate, small object (JSONPatcherProxy)
 ec7b9bf: 136720 Ops/sec
 92da649: 136351 Ops/sec (0.3% worse)
Observe and generate (JSONPatcherProxy)
 ec7b9bf: 2762 Ops/sec
 92da649: 2793 Ops/sec (1.1% better)
Primitive mutation (JSONPatcherProxy)
 ec7b9bf: 781852 Ops/sec
 92da649: 781270 Ops/sec (0.1% worse)
Complex mutation (JSONPatcherProxy)
 ec7b9bf: 11808 Ops/sec
 92da649: 10692 Ops/sec (9.5% worse)
Serialization (JSONPatcherProxy)
 ec7b9bf: 1719 Ops/sec
 92da649: 1719 Ops/sec (no difference)

Contributing

  • Fork it.
  • Clone it locally.
  • Run npm install.
  • Run npm test to make sure tests pass before touching the code.
  • Modify, test again, push, and send a PR!

Bumping the version

Version bumping is automated. Just use npm version command and all files will be updated with the new version.

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].