New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

readline-browser

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

readline-browser

This library is designed with same APIs as the node.js core module `readline`.

latest
npmnpm
Version
0.0.3
Version published
Maintainers
1
Created
Source

readline-browser

This library is designed with same APIs as the node.js core module readline.

Which supports you start a repl in a browser environment.

Example

with xterm.js

const { Terminal } = require("xterm");
const rl = require("readline-browser");
const getStringWidth = require("string-width");


const term = new Terminal();
term.open(document.getElementById("terminal"));


function getMappedKeyName(rawName){
    if(rawName == "Backspace"){
        return "backspace";
    }else if(rawName == "ArrowLeft"){
        return 'left';
    }else if(rawName == "ArrowRight"){
        return 'right';
    }else if(rawName == "ArrowUp"){
        return 'up';
    }else if(rawName == "ArrowDown"){
        return 'down';
    }else if(rawName == "Delete"){
        return 'delete';
    }else if(rawName == "Enter"){
        return 'enter';
    }else if(rawName == "Tab"){
        return 'tab';
    }else if(rawName == "Home"){
        return 'home';
    }else if(rawName == "End"){
        return 'end';
    }else if(rawName == "Return"){
        return 'return';
    }
    return rawName;
}
const input = {
    on(event,listener){
        if(event == "data"){

        }else if(event == "keypress"){
            term.onData((char)=>{
                if(getStringWidth(char)>=2){
                    listener(char,{
                        sequence:char,
                        name:undefined,
                        ctrl:false,
                        meta:false,
                        shift:false,
                    })
                }
            });

            term.onKey(({key,domEvent})=>{

                const name = getMappedKeyName(domEvent.key);
                const ctrl = domEvent.ctrlKey;
                const shift = domEvent.shiftKey;

                listener(key,{
                    sequence:name,
                    name,
                    ctrl,
                    shift
                });
            });
        }        
    },
    resume(){
        console.log("resume");
    },
    pause(){
        console.log("pause");

    }
}

const output = {
    isTTY:true,
    get columns(){
        return term.cols;
    },
    on(event,listener){
        if(event == "resize"){
            term.onResize(listener);
        }
    },
    write(data){
        term.write(data);
    }
}

const intf = rl.createInterface(input,output);

async function startRepl(){
    while(true){
        console.log(await new Promise((resolve)=>intf.question("$ > ",resolve)));
    }
}

startRepl();

replace builtin module

//const rl = require("readline");
const rl = require("readline-browser");

//some other things

FAQs

Package last updated on 25 May 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts