Node.js Evaluation Loop (NEL)
NEL
is an npm module for running
Node.js REPL sessions.
NEL
is a spin-off library from
IJavascript. This fact explains some
of the design decisions in NEL
such as returning results in MIME format, and
the functionality provided for completion and inspection of Javascript
expressions. See the section on usage for more details.
Please, consider this repository as an alpha release. The API is likely to
change.
Main Features
- Run Javascript code within a
Node.js
session. The result can be formatted
as:
- Run Javascript code
asynchronously.
- Generate a list of completion
options for an
incomplete piece of Javascript code.
- Inspect a Javascript
expression and
return information such as type or even documentation (currently, only for
Javascript builtins).
Announcements
NEL
is in the process of being refactored to maximise the amount shared
amongst ijavascript,
jp-babel and
jp-coffeescript.NEL v0.3
: New API (simplify API by hiding type module:nel~Task)NEL v0.2
: API change (removed Session#executionCount)NEL v0.1.1
: New experimental $$mimer$$
and $$defaultMimer$$
NEL v0.1
: Output change (changed function output)NEL v0.0
: Initial release
Install
npm install nel
Usage
Hello, World!
var nel = require("nel");
var session = new nel.Session();
var onSuccess = function printResult(result) { console.log(result); }
var onError = function printError(error) { console.log(error); }
var code = "['Hello', 'World!'].join(', ');";
session.execute(code, onSuccess, onError);
Exceptions
code = "throw new Error('Hello, World!');";
session.execute(code, onSuccess, onError);
stdout
and stderr
code = "console.log('Hello, World!');";
session.execute(code, onSuccess, onError);
process.stdout.write(session.stdout.read());
MIME output
A session may return results in MIME formats other than 'text/plain'.
code = "$$html$$ = \"<div style='background-color:olive;width:50px;height:50px'></div>\";";
session.execute(code, onSuccess, onError);
code = "$$svg$$ = \"<svg><rect width=80 height=80 style='fill: orange;'/></svg>\";";
session.execute(code, onSuccess, onError);
code = "$$png$$ = require('fs').readFileSync('image.png').toString('base64');";
session.execute(code, onSuccess, onError);
code = "$$jpeg$$ = require('fs').readFileSync('image.jpg').toString('base64');";
session.execute(code, onSuccess, onError);
code = "$$mime$$ = {\"text/html\": \"<div style='background-color:olive;width:50px;height:50px'></div>\"};";
session.execute(code, onSuccess, onError);
Generate a completion list
NEL
can parse simple Javascript variable expressions and generate a list of
completion options:
session.complete(
"set",
3,
onSuccess,
onError
);
Note that the cursor position can be located anywhere within the Javascript
code:
session.complete(
"set",
2,
onSuccess,
onError
);
Inspect an expression
NEL
can parse simple Javascript variable expressions and inspect their value:
code = "var a = [1, 2, 3];";
session.execute(code, null, onError);
session.inspect(
code,
5,
onSuccess,
onError
);
NEL
can also provide relevant documentation (currently only available for
Javascript builtins):
session.inspect(
"parseInt",
8,
onSuccess,
onError
);
Callbacks beforeRequest
and afterRequest
var beforeRequest = function() { console.log("This callback runs first"); }
code = "'I run next'";
var afterRequest = function() { console.log("This callback runs last"); }
session.execute(code, onSuccess, onError, beforeRequest, afterRequest);
Contributions
First of all, thank you for taking the time to contribute. Please, read
CONTRIBUTING.md
and use the issue tracker for
any contributions: support requests, bug reports, enhancement requests, pull
requests, ...
TODO
- Implement
$$text$$
, $$update$$()
- Add tests for
$$async$$
and $$done()$$
- Add tests for
$$html$$
, $$png$$
, $$jpeg$$
, $$mime$$
, $$mimer$$
, ... - Session#complete and Session#inspect: make
cursorPos
argument default to
code.length
- Add
Node.js
documentation