bitboxer / jimson

JSON-RPC 2.0 client and server for Ruby

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Feature] Support for namespace

josevalim opened this issue · comments

I would like to discuss the possibility to built in jimson some kind of namespace support as a convenience on top of JSON-RPC. This proposal could be implemented in two ways:

Using methods

When the a request comes and the method is "foo.bar", we would invoke "foo" on the handler object that would return another object which "bar" would be called on. However, this approach could lead to security vulnerabilities (for example, given a function returns an array, one could call methods on the Array like pack/unpack that could be CPU or SPACE intensive).

Explicit exposure

A better approach is to whitelist which methods allow nesting, this could be in the Handler API. A proposal is the jimson_nesting method:

extend Jimson::Handler
jimson_namespace :service1

My case

My case is that I want to expose several services via JSON-RPC without actually having to create many ports. For example:

module Handle
  extend Jimson::Handler
  def service1; Service1; end
  def service2; Service2; end
end

And then I would like to invoke it as: "service1.foo".

Client

Today, the client uses method missing and this approach doesn't work with namespace. One option is to allow namespace in the client:

service1 = client.namespace(:service1)
service1.foo

This would be a very thin wrapper around the client that would basically convert the method to "service1.foo".

What do you think? In case you are interested in something like this, please let me know and I will gladly implement it. :)

I was thinking of implementing this using a sort of router, similar to what you'd see in Rails. So we could do something like:

Jimson::Server.start do |jimson|
  jimson.map 'service1.*', service1_handler
  jimson.map 'service2.*', service2_handler
  jimson.map '*', main_handler
end

Thoughts?

As for the client side, it is currently possible to call methods with a . in them like so:

client['system.listMethods']

For using namespaces, I'd prefer to similarly use [] to as to not pollute the client's blank slate, so something like:

client[:service1].foo

Server side namespacing is in 0.7.0. I'm going to work on some nicer client side handling of it next. Right now you'd have to call it like client['ns.method']