Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

stdin-discarder

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

stdin-discarder - npm Package Compare versions

Comparing version 0.1.0 to 0.2.0

80

index.js
import process from 'node:process';
import readline from 'node:readline';
import {BufferListStream} from 'bl';

@@ -8,34 +6,9 @@ const ASCII_ETX_CODE = 0x03; // Ctrl+C emits this code

class StdinDiscarder {
#requests = 0;
#mutedStream = new BufferListStream();
#ourEmit;
#rl;
#activeCount = 0;
constructor() {
this.#mutedStream.pipe(process.stdout);
const self = this; // eslint-disable-line unicorn/no-this-assignment
this.#ourEmit = function (event, data, ...arguments_) {
const {stdin} = process;
if (self.#requests > 0 || stdin.emit === self.#ourEmit) {
if (event === 'keypress') { // Fixes readline behavior
return;
}
if (event === 'data' && data.includes(ASCII_ETX_CODE)) {
process.emit('SIGINT');
}
Reflect.apply(self.#ourEmit, this, [event, data, ...arguments_]);
} else {
Reflect.apply(process.stdin.emit, this, [event, data, ...arguments_]);
}
};
}
start() {
this.#requests++;
this.#activeCount++;
if (this.#requests === 1) {
this._realStart();
if (this.#activeCount === 1) {
this.#realStart();
}

@@ -45,43 +18,38 @@ }

stop() {
if (this.#requests <= 0) {
if (this.#activeCount <= 0) {
throw new Error('`stop` called more times than `start`');
}
this.#requests--;
this.#activeCount--;
if (this.#requests === 0) {
this._realStop();
if (this.#activeCount === 0) {
this.#realStop();
}
}
// TODO: Use private methods when targeting Node.js 14.
_realStart() {
// No known way to make it work reliably on Windows
if (process.platform === 'win32') {
#realStart() {
// No known way to make it work reliably on Windows.
if (process.platform === 'win32' || !process.stdin.isTTY) {
return;
}
this.#rl = readline.createInterface({
input: process.stdin,
output: this.#mutedStream,
});
this.#rl.on('SIGINT', () => {
if (process.listenerCount('SIGINT') === 0) {
process.emit('SIGINT');
} else {
this.#rl.close();
process.kill(process.pid, 'SIGINT');
}
});
process.stdin.setRawMode(true);
process.stdin.on('data', this.#handleInput);
}
_realStop() {
if (process.platform === 'win32') {
#realStop() {
if (!process.stdin.isTTY) {
return;
}
this.#rl.close();
this.#rl = undefined;
process.stdin.setRawMode(false);
process.stdin.off('data', this.#handleInput);
}
#handleInput(chunk) {
// Allow Ctrl+C to gracefully exit.
if (chunk[0] === ASCII_ETX_CODE) {
process.emit('SIGINT');
}
}
}

@@ -88,0 +56,0 @@

{
"name": "stdin-discarder",
"version": "0.1.0",
"version": "0.2.0",
"description": "Discard stdin input except for Ctrl+C",

@@ -14,6 +14,9 @@ "license": "MIT",

"type": "module",
"exports": "./index.js",
"types": "./index.d.ts",
"exports": {
"types": "./index.d.ts",
"default": "./index.js"
},
"sideEffects": false,
"engines": {
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
"node": ">=18"
},

@@ -35,11 +38,7 @@ "scripts": {

],
"dependencies": {
"bl": "^5.0.0"
},
"devDependencies": {
"ava": "^4.3.0",
"hook-std": "^3.0.0",
"tsd": "^0.21.0",
"xo": "^0.50.0"
"ava": "^6.0.1",
"tsd": "^0.29.0",
"xo": "^0.56.0"
}
}

@@ -11,4 +11,2 @@ # stdin-discarder

**Work in progress**
## Install

@@ -15,0 +13,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc