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

minimal-password-prompt

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

minimal-password-prompt

Minimal hidden password prompt with no dependencies for Node.js.

latest
Source
npmnpm
Version
1.1.0
Version published
Weekly downloads
684
20.42%
Maintainers
1
Weekly downloads
 
Created
Source

minimal-password-prompt

npm bundle size NPM GitHub top language GitHub issues

Minimal hidden password prompt for Node.js! [npm] [github]

prompt-password example

Simple, no extra hassles

packagedependenciesdependentstotal lines of production code
minimal-password-prompt0048
password-prompt2432147
  • No dependencies
  • Multiplatform: tested on Windows, Linux and macOS
  • Minimal: Less than 50 lines of code

Seriously, you can also just copy and paste to your project:

const stdin = process.stdin;
const stdout = process.stdout;

const captureStdin = (onData) => {
  stdin.setEncoding('utf-8');
  stdin.setRawMode(true);
  stdin.on('data', onData);
  stdin.resume();
};

const releaseStdin = (onData) => {
  stdin.pause();
  stdin.removeListener('data', onData);
  stdin.setRawMode(false);
  stdout.write('\n');
};

const prompt = (question, ctrlcExits = true) => (
  new Promise((resolve, reject) => {
    let input = '';
    const onData = (data) => {
      switch (data) {
        case '\u000A': // \n
        case '\u000D': // \r
        case '\u0004': // Ctrl+D
          releaseStdin(onData);
          resolve(input);
          break;
        case '\u0003': // Ctrl+C
          releaseStdin(onData);
          ctrlcExits // exit or raise error
            ? process.exit()
            : reject(new Error('Ctrl+C'));
          break;
        case '\u0008': // Backspace
        case '\u007F': // Delete
          input = input.slice(0, -1);
          break;
        default: // any other
          input += data;
      };
    };
    captureStdin(onData);
    stdout.write(question);
  })
);

module.exports = prompt;

(please sill do remember to include MIT license/copyright)

Usage

const prompt = require('minimal-password-prompt');

prompt('enter password: ').then(pw => {
  console.log(`Password: ${pw}`);
});

or with async/await:

const prompt = require('minimal-password-prompt');

(async () => {
  const pw = await prompt('enter password: ');
  console.log(`password: ${pw}`);
})();

More examples here

prompt reference

Parameters:

  • question (String) prompt output before user input
  • ctrlcExits (Boolean), optional
    • true: Ctrl+C exits program (default)
    • false: Ctrl+C throws error

Returns:

  • Promise<String> user input during prompt

Installation

Install with npm:

npm install --save minimal-password-prompt

Keywords

password

FAQs

Package last updated on 27 Feb 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