npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ctrlc-wrapper

v0.0.4

Published

Wrapper enabling to send CTRL+C signal to child process

Downloads

81

Readme

ctrlc-wrapper

Windows doesn't support sending signals to other processes as it is possible on POSIX platforms.

Using kill methods on Windows (like process.kill() with Node.js) means the target process is getting killed forcefully and abruptly (similar to SIGKILL).

However, in a console, processes can be terminated with the CTRL+C key combination.
Most programming languages have an implementation to capture this signal (usually as SIGINT), allowing applications to handle it and to terminate "gracefully".

The problem is that the CTRL+C key combination cannot be easily simulated for the following reasons:

  • In order to be able to generate a CTRL+C signal programmatically, several Console Functions needs to be called - something which can only be done in lower-level programming languages.
  • The process which should receive the CTRL+C signal needs to live in its own console since the CTRL+C signal is sent to all processes attached to a console. Spawning a process in a new console, again, is something which is only possible in lower-level programming languages.

This wrapper application does exactly the points described above.

The wrapper inherits the exit code from the child process. If there's an error with the wrapper itself, the exit code is -1.

Usage

npm install ctrlc-wrapper
import { spawnWithWrapper } from 'ctrlc-wrapper';

const child = spawnWithWrapper('node test/read-echo.js');

child.stdout.on('data', (data) => {
  console.log(`stdout: ${data}`);
  if (/READING/.test(data)) {
    child.sendCtrlC();
  }
});

child.stderr.on('data', (data) => {
  console.log(`stderr: ${data}`);
});

child.on('close', (code, signal) => {
  console.log(`close: ${code} ${signal}`);
});

Test

To start the child process node test/read-echo.js with the wrapper:

go run ./cmd/start node test/read-echo.js

To terminate:

  • Press CTRL+C
  • Write ^C to stdin
  • Exit from within the child

Build

pnpm build

Notes

Why the separate ctrlc.exe binary?

It would be possible to send the CTRL+C signal directly from within start.exe but this means an additional process must be spawned (e.g. cmd /c pause) to prevent losing the original (parent) console during the console switch (FreeConsole -> AttachConsole). Using a separate binary to send the CTRL+C signal is much safer.

CREATE_NEW_CONSOLE vs. CREATE_NEW_PROCESS_GROUP

Both methods seems to protect from receiving the CTRL+C signal in the current console.

However, spawning the child with CREATE_NEW_PROCESS_GROUP would mean that we need to start another "wrapper" in which the "normal processing of CTRL+C input" is enabled first (via SetConsoleCtrlHandler) before starting the actual child process.

CreateRemoteThread

Instead of ctrlc.exe we might be able to terminate the target process by inject a thread into it, but this seems to be overly complicated...