Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions docs/cli-code-evaluation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# CLI REPL Code Evaluation

This document describes the evaluation of user code in the [CLI REPL](../packages/cli-repl),
i.e. what happens when the user types `db.coll.find({})` in the CLI REPL and hits enter.

## Involved Components

### Async REPL
The [`async-repl`](../packages/cli-repl/src/async-repl.ts) is responsible for setting up the Node.js
`REPLServer` that `mongosh` is using for the CLI. Most notabily we customize the `REPLServer.eval` function
to disable certain default REPL behavior and set up customized interrupt and error handling.

### Mongosh Node REPL
The [`MongoshNodeRepl`](../packages/cli-repl/src/mongosh-repl.ts) is our main interface and orchestrator for the CLI. However it does not do any I/O
on its own but relies on an external I/O provider. It initializes and manages the Shell API abstraction
as well as handles things like access to the history or startup messages.

### Shell Evaluator
The [`ShellEvaluator`](../packages/shell-evaluator) is an intermediate package also used by non-CLI uses of the shell and is responsible
for correctly transforming user input before evaluation. It relies on the `async-rewriter2` for input
transformation.
The Shell Evaluator also detects linux-commands like `show dbs` and triggers their execution without further code transformation or evaluation.

### Async Rewriter
The [`async-rewriter2`](../packages/async-rewriter2) transforms code written in a synchronous way into
code with automatically inserted `await` statements to resolve promises returned by the Shell API.

## Execution

1. User enters `db.coll.find({})` and hits enter.
2. The `REPLServer.eval` function is called which is customized in [`async-repl`](../packages/cli-repl/src/async-repl.ts).
3. The customized `eval` function triggers [`MongoshNodeRepl.eval`](../packages/cli-repl/src/mongosh-repl.ts).
4. `MongoshNodeRepl` forwards the user input to [`ShellEvaluator.customEval`](../packages/shell-evaluator/src/shell-evaluator.ts).
5. `ShellEvaluator.customEval` calls `ShellEvaluator.innerEval`:
1. For linux-style commands, e.g. `show dbs`, processing is immediately triggered and the result is returned up the chain.
2. For JavaScript user input:
1. `ShellEvaluator` calls the `async-rewriter` to rewrite user input.
2. Rewritten user input is now evaluated in the current Shell API context using the _original, non-modified_ `REPLServer.eval`
function that has been passed down.
3. The evaluation result is returned up the chain.

See also the following diagram which additionally includes important events emitted during the execution.

![](cli-code-evaluation.png)
Binary file added docs/cli-code-evaluation.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions docs/diagrams.drawio
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<mxfile host="Electron" modified="2021-07-20T09:31:16.378Z" agent="5.0 (Macintosh; Intel Mac OS X 11_4_0) AppleWebKit/537.36 (KHTML, like Gecko) draw.io/14.6.13 Chrome/89.0.4389.128 Electron/12.0.7 Safari/537.36" etag="m0ryZ81Uoev6-Ma22SGn" version="14.6.13" type="device"><diagram id="Ff37bpA9ezkQJEPIHSPT" name="CLI REPL Evaluation">7Vxbc6M2FP41nmkfkuFq8GMuTtOZbLoTz06zTx0CslEXEBVybO+vr2SEuUi2aYKRG5J9WOsgCXG+T+foSAdG5k28/g17afgFBSAaGVqwHpm3I8PQxxOX/sckm1ziToxcsMAw4JVKwQz+BFyocekSBiCrVSQIRQSmdaGPkgT4pCbzMEarerU5iup3Tb0Fv6NWCma+FwGh2p8wICF/CsMp5fcALsLizvSJ8yuxV1TmHWehF6BVRWROR+YNRojkv+L1DYiY8gq95O3u9lzdDQyDhLRp8Nf0HjyQ52+beBFPn7Hm/x6sLgo1Z2RTPDEIqAJ4EWESogVKvGhaSq8xWiYBYN1qtFTWeUAopUKdCv8GhGw4mt6SICoKSRzxq3OUkDsvhhEjxQ1aYggwHcQjoOq5BmtInlnfl4bNi995O/b7ds3vuy1sikJC8KbaipW/Vy+W7balomH++OyZ96qVizI6Th8c0iWnp4cXgByoN9mBT2cNQDGg46HtMIg8Al/r4/A4fRe7eiXC9AcH+b8Anvf76kVLfqen6deHGcCvAF8CKhb4UKLNIFiFkIBZ6m01saJzvoEsjKIbFCG8bWvObfa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L5KoZBDgNLNxJPFhv9CIeauSvdJBQKM3l5iurRibsYCN5Gx4kNjYmmpsxPhNkqoxSGzGhmpsxC0PyaHwILFxLNXYiFGzJB10GNg0N/Mli+hesSnuX8HGGSg2hjyPQR00YrzqDhSaxqtEjiTjqF9oxMhT9nbwILDZ7cqei7uxxMhT9vrBIMExxqrBEWNPfagBThMc5cGnJQafsrcmBwmO8ujTEqPP4h2LwYOjPPy0xPBT9hrHIMCxzPpCWvbmd7/giPGnPtQNT6t5CivJiugVHFsMQGfgnyVIqPYNLVnGL5LUMfELCo3j7L3fRcAggz+9l21HDC6efUd7ta9H9i3raUlQVjmhk36aYR/IHSB0oQufuRCN2+54p5/vXIjv8gmQfJ6RtjqWs9tNt9NBKW4qTGNICDVkhgZeQSKefg9rskkAmvQKkGRrYaC+qrmRfcLglRbLr8vlSdHlN/rM6b8=</diagram></mxfile>
10 changes: 10 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Development Documentation

This folder contains development-specific documentation on the inner workings of `mongosh` and its packages.

> If you are looking for end-user documentation, please refer to: https://docs.mongodb.com/mongodb-shell/.

## Topics
Go to any of the linked topics for details:

* [CLI REPL Code Evaluation](./cli-code-evaluation.md)