Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
dkackman committed Aug 30, 2023
1 parent 321fc1f commit d0287f3
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions packages/chia-daemon/rpc_proxy.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,50 @@
import { makePayload, getPayloadDescriptor } from './payload_generator.js';
import ChiaHttps from "./chia_https.js";
import { makePayload, getPayloadDescriptor } from "./payload_generator.js";
// This might be evil and generally abusive of the javascript type system but...
// This here will allow us to call an arbitray method-like thing on any object
// This here will allow us to call an arbitrary method-like thing on any object
// and transform it into an rpc invocation though we don't know the endpoint name until runtime.
// If the invoked method is an endpoint on the rpc server, it will be invoked as such.
//
// so we can do something like the below even though get_blockhain_state is not declared or implemented anywhere:
// const full_node = createRpcProxy(chiaDeamon, 'chia_full_node');
// const state = await full_node.get_blockhain_state();
// so we can do something like the below even though get_blockchain_state is not declared or implemented anywhere:
// const full_node = createRpcProxy(chiaDaemon, 'chia_full_node');
// const state = await full_node.get_blockchain_state();
//
/**
* Returns a proxy object that transforms any method into an RPC invocation.
* @param {ChiaDaemon} chia - The chia daemon service that will execute the RPC.
* @param {ChiaDaemon|ChiaHttps} chia - The chia daemon service that will execute the RPC.
* @param {string} service - The name of the chia endpoint service.
* @returns {Proxy} The proxy that will route methods calls.
*/
export default function createRpcProxy(chia, service) {
// create a proxy around a new object that will intrecept
// create a proxy around a new object that will intercept
// any method call that doesn't exist and turn it into an RPC invocation
//
// add a couple helper functions to our synthetic object
const o = {
makePayload: (endpoint, requiredOnly = true) => makePayload(service, endpoint, requiredOnly),
getPayloadDescriptor: (endpoint) => getPayloadDescriptor(service, endpoint),
makePayload: (endpoint, requiredOnly = true) =>
makePayload(service, endpoint, requiredOnly),
getPayloadDescriptor: (endpoint) =>
getPayloadDescriptor(service, endpoint),
};

return new Proxy(o, {
get(target, functionName) {
if (typeof target[functionName] === 'undefined') {
if (typeof target[functionName] === "undefined") {
// here, since 'functionName' does not exist on the object, we are
// going to assume it is an rpc function name on the endpoint
// here we call back into the chia server to do the RPC
return async (data) => await chia.sendCommand(service, functionName, data);
} else if (typeof target[functionName] === 'function') {
return async (data) =>
await chia.sendCommand(service, functionName, data);
} else if (typeof target[functionName] === "function") {
// here the function exists so just reflect apply to invoke
return new Proxy(target[functionName], {
apply: (target, thisArg, argumentsList) => Reflect.apply(target, thisArg, argumentsList)
apply: (target, thisArg, argumentsList) =>
Reflect.apply(target, thisArg, argumentsList),
});
} else {
// here the property is a value - reflect return it
return Reflect.get(target, functionName);
}
}
},
});
}

0 comments on commit d0287f3

Please sign in to comment.