10gen-labs/mongo-web-shell — pull request #168 by FuegoFro (2013-06-14 21:44:09+00:00)
Title: New sandboxing/scoping method

Shells create invisible iframes that can be used to evaluate javascript in a sandboxed environment. This allows us to remove much of the source mutation code. However, the previous method of detecting and executing database calls was not only brittle, but wouldn't work with this new model. Thus, I created a db object which we put in the sandboxed namespace. In order to match MongoDB's shell with respect to how collections are created and accessed, I needed to implement something akin to the Ruby's method missing. This is done by mutating all reads of fields on objects to use an accessor function inserted into the sandboxed namespace (e.g. 'a.b' => '__get(a, "b")').

By creating separate db and collection classes, a lot of existing code moved around and some files were removed. The Query file was essentially just call throughs to the request file and, since we refactored requests.js, many of the methods in that file were much more about the collection method being accessed, and not about the request itself. Thus I removed the Query file and moved the collection method-specific code out of requests and into the Coll (collection) class. This resulted in appropriate changes to the Cursor class, which relied on the Query object, as well as a slight refactoring of the interface that the Cursor class uses. It's still pretty confusing, with a lot of callbacks and contracts that need to be fulfilled, but the logic is slightly more separated than it was before.

All the tests have been updated and new tests have been written to cover the changed and added functionality. The keyword handling (help, show, it) is still not great and probably going to change soon, so I haven't written tests around keyword handling in either mutateSource or DB; I figured this was enough of a change as it was.