Skip to content

Commit 9bad6bb

Browse files
authored
chore(docs): add execution flow of CLI REPL eval (#1024)
1 parent 4f888fd commit 9bad6bb

File tree

4 files changed

+55
-0
lines changed

4 files changed

+55
-0
lines changed

docs/cli-code-evaluation.md

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# CLI REPL Code Evaluation
2+
3+
This document describes the evaluation of user code in the [CLI REPL](../packages/cli-repl),
4+
i.e. what happens when the user types `db.coll.find({})` in the CLI REPL and hits enter.
5+
6+
## Involved Components
7+
8+
### Async REPL
9+
The [`async-repl`](../packages/cli-repl/src/async-repl.ts) is responsible for setting up the Node.js
10+
`REPLServer` that `mongosh` is using for the CLI. Most notabily we customize the `REPLServer.eval` function
11+
to disable certain default REPL behavior and set up customized interrupt and error handling.
12+
13+
### Mongosh Node REPL
14+
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
15+
on its own but relies on an external I/O provider. It initializes and manages the Shell API abstraction
16+
as well as handles things like access to the history or startup messages.
17+
18+
### Shell Evaluator
19+
The [`ShellEvaluator`](../packages/shell-evaluator) is an intermediate package also used by non-CLI uses of the shell and is responsible
20+
for correctly transforming user input before evaluation. It relies on the `async-rewriter2` for input
21+
transformation.
22+
The Shell Evaluator also detects linux-commands like `show dbs` and triggers their execution without further code transformation or evaluation.
23+
24+
### Async Rewriter
25+
The [`async-rewriter2`](../packages/async-rewriter2) transforms code written in a synchronous way into
26+
code with automatically inserted `await` statements to resolve promises returned by the Shell API.
27+
28+
## Execution
29+
30+
1. User enters `db.coll.find({})` and hits enter.
31+
2. The `REPLServer.eval` function is called which is customized in [`async-repl`](../packages/cli-repl/src/async-repl.ts).
32+
3. The customized `eval` function triggers [`MongoshNodeRepl.eval`](../packages/cli-repl/src/mongosh-repl.ts).
33+
4. `MongoshNodeRepl` forwards the user input to [`ShellEvaluator.customEval`](../packages/shell-evaluator/src/shell-evaluator.ts).
34+
5. `ShellEvaluator.customEval` calls `ShellEvaluator.innerEval`:
35+
1. For linux-style commands, e.g. `show dbs`, processing is immediately triggered and the result is returned up the chain.
36+
2. For JavaScript user input:
37+
1. `ShellEvaluator` calls the `async-rewriter` to rewrite user input.
38+
2. Rewritten user input is now evaluated in the current Shell API context using the _original, non-modified_ `REPLServer.eval`
39+
function that has been passed down.
40+
3. The evaluation result is returned up the chain.
41+
42+
See also the following diagram which additionally includes important events emitted during the execution.
43+
44+
![](cli-code-evaluation.png)

docs/cli-code-evaluation.png

193 KB
Loading

docs/diagrams.drawio

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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+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x1/xRXPi+OWNxPzb3IHoFLO9uJEnJa6yIAg+4c1+6IvJd8DI/URAiy42UGddm5lR34IjbL5KoZBDgNLNxJPFhv9CIeauSvdJBQKM3l5iurRibsYCN5Gx4kNjYmmpsxPhNkqoxSGzGhmpsxC0PyaHwILFxLNXYiFGzJB10GNg0N/Mli+hesSnuX8HGGSg2hjyPQR00YrzqDhSaxqtEjiTjqF9oxMhT9nbwILDZ7cqei7uxxMhT9vrBIMExxqrBEWNPfagBThMc5cGnJQafsrcmBwmO8ujTEqPP4h2LwYOjPPy0xPBT9hrHIMCxzPpCWvbmd7/giPGnPtQNT6t5CivJiugVHFsMQGfgnyVIqPYNLVnGL5LUMfELCo3j7L3fRcAggz+9l21HDC6efUd7ta9H9i3raUlQVjmhk36aYR/IHSB0oQufuRCN2+54p5/vXIjv8gmQfJ6RtjqWs9tNt9NBKW4qTGNICDVkhgZeQSKefg9rskkAmvQKkGRrYaC+qrmRfcLglRbLr8vlSdHlN/rM6b8=</diagram></mxfile>

docs/index.md

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Development Documentation
2+
3+
This folder contains development-specific documentation on the inner workings of `mongosh` and its packages.
4+
5+
> If you are looking for end-user documentation, please refer to: https://docs.mongodb.com/mongodb-shell/.
6+
7+
## Topics
8+
Go to any of the linked topics for details:
9+
10+
* [CLI REPL Code Evaluation](./cli-code-evaluation.md)

0 commit comments

Comments
 (0)