stdin-discarder
Advanced tools
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 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
0
3
3990
54
38
- Removedbl@^5.0.0
- Removedbase64-js@1.5.1(transitive)
- Removedbl@5.1.0(transitive)
- Removedbuffer@6.0.3(transitive)
- Removedieee754@1.2.1(transitive)
- Removedinherits@2.0.4(transitive)
- Removedreadable-stream@3.6.2(transitive)
- Removedsafe-buffer@5.2.1(transitive)
- Removedstring_decoder@1.3.0(transitive)
- Removedutil-deprecate@1.0.2(transitive)