node-repl-await
Standalone util function from Node.js core to process await statements in REPL.
Last updated 6 months ago by hyurl .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install node-repl-await 
SYNC missed versions from official npm registry.

Node's processTopLevelAwait

Standalone util function from Node.js core to process await statements in REPL.

Since v10.0.0, Node.js introduced a new experimental feature to support await keyword in the REPL by using the argument --experimental-repl-await, however, if a user wants to implement a custom REPL console, there would be no await-support at all, to achieve such a goal, this package clones the internal module of await-support to form a standalone version, allowing users share the benefits of await-support in their own REPL environments.

See Node.js docs for more details, and contribute to the original source.

Example

const repl = require("repl");
const vm = require("vm");
const { processTopLevelAwait } = require("node-repl-await");

function isRecoverableError(error) {
    if (error.name === 'SyntaxError') {
        return /^(Unexpected end of input|Unexpected token)/.test(error.message);
    }
    return false;
}

async function myEval(code, context, filename, callback) {
    code = processTopLevelAwait(code) || code;

    try {
        let result = await vm.runInNewContext(code, context);
        callback(null, result);
    } catch (e) {
        if (isRecoverableError(e)) {
            callback(new repl.Recoverable(e));
        } else {
            console.log(e);
        }
    }
}

repl.start({ prompt: '> ', eval: myEval });

API

function processTopLevelAwait(src: string): string | void

Tries to wrap the given source code to an immediately-invoked function if it contains any top level await statement in the form of

(async () => { /* source code */ })()

If no await presents or processing failed, the function returns null.

Current Tags

  • 0.1.1                                ...           latest (6 months ago)

2 Versions

  • 0.1.1                                ...           6 months ago
  • 0.1.0                                ...           a year ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 15
Last Day 0
Last Week 0
Last Month 18
Dev Dependencies (2)

Copyright 2014 - 2017 © taobao.org |