-
Notifications
You must be signed in to change notification settings - Fork 3
fix: #50 #51 #52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: v2
Are you sure you want to change the base?
fix: #50 #51 #52
Changes from all commits
ea72f35
720a1bc
4449b72
b29c075
063d431
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,3 +4,5 @@ target | |
| **/*~ | ||
| **/node_modules | ||
| **/.DS_Store | ||
| index.d.ts | ||
|
|
||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| export function ctrlc(pid: number): void; | ||
| export function ctrlc(pid: number, processKillerPath?: string): void; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,21 +1,41 @@ | ||
| const { execFileSync } = require("child_process"); | ||
| const { platform, arch } = require("os"); | ||
| const { join } = require("path"); | ||
|
|
||
| const archDistDirName = arch() === "arm64" ? "arm64" : "x64"; | ||
| const isArm64 = arch() === "arm64"; // ? "arm64" : "x64"; | ||
| const archDistDirName = isArm64 ? "arm64" : "x64"; | ||
| const isWindows = platform() === "win32"; | ||
|
|
||
| const native = | ||
| platform() === "win32" | ||
| ? require(`../dist/${archDistDirName}/ctrlc-windows.node`) | ||
| : require("./posix"); | ||
| // this would be more friendly to bundlers | ||
| const native = isWindows | ||
| ? isArm64 | ||
| ? require("../dist/arm64/ctrlc-windows.node") | ||
| : require("../dist/x64/ctrlc-windows.node") | ||
| : null; | ||
|
|
||
| /** | ||
| * workaround for node-loader users; because node-loader only copies .node files, | ||
| * devs using them will need to manually copy process-killer.exe into their app bundle | ||
| * and provide the path to it when calling ctrlc() | ||
| */ | ||
| const defaultKillerPath = join(__dirname, "..", `dist/${archDistDirName}/process-killer.exe`); | ||
|
|
||
| module.exports = { | ||
| ctrlc(pid) { | ||
| ctrlc(pid, processKillerPath = defaultKillerPath) { | ||
| if (!isWindows) { | ||
| let error = new Error( | ||
| "tried to invoke windows-specific ctrlc on a non-windows platform", | ||
| ); | ||
| error.name = "PlatformError"; | ||
| throw error; | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. move posix.js here |
||
| } | ||
| try { | ||
| // don't even attempt if the | ||
| // process is not running | ||
| if (process.kill(pid, 0)) { | ||
| const processKillerPath = join(__dirname, "..", `dist/${archDistDirName}/process-killer.exe`); | ||
| native.ctrlc(pid, processKillerPath); | ||
| native.ignoreSignal(true); | ||
| execFileSync(processKillerPath, [pid.toString()]); | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can correctly spawn executables in asar therefore fixes #50 see https://www.electronjs.org/docs/latest/tutorial/asar-archives#executing-binaries-inside-asar-archive |
||
| native.ignoreSignal(false); | ||
| } | ||
| } catch (error) { | ||
| if (error.code !== "ESRCH") { | ||
|
|
@@ -24,3 +44,4 @@ module.exports = { | |
| } | ||
| }, | ||
| }; | ||
|
|
||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,11 @@ | ||
| #![windows_subsystem = "windows"] | ||
|
|
||
| #[cfg(windows)] | ||
| use std::error::Error; | ||
|
|
||
| #[cfg(windows)] | ||
| use windows::Win32::System::Console::{ | ||
| AttachConsole, FreeConsole, GenerateConsoleCtrlEvent, SetConsoleCtrlHandler, CTRL_C_EVENT, | ||
| AttachConsole, GenerateConsoleCtrlEvent, SetConsoleCtrlHandler, CTRL_C_EVENT, | ||
| }; | ||
|
|
||
| #[cfg(windows)] | ||
|
|
@@ -21,8 +23,6 @@ pub fn main() -> Result<(), Box<dyn Error>> { | |
| #[cfg(windows)] | ||
| fn kill_pid(pid: u32) -> Result<(), Box<dyn Error>> { | ||
| unsafe { | ||
| FreeConsole(); | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. a subsystem:windows program doesn't have a console to begin with |
||
|
|
||
| if AttachConsole(pid).as_bool() { | ||
| SetConsoleCtrlHandler(None, true); | ||
| GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,56 +3,11 @@ | |
| #[macro_use] | ||
| extern crate napi_derive; | ||
|
|
||
| use napi::{Error, JsNumber, JsString, Result, Status}; | ||
| use std::ffi::OsString; | ||
| use std::process::Command; | ||
| use windows::Win32::System::Console::SetConsoleCtrlHandler; | ||
|
|
||
| #[napi] | ||
| fn ctrlc(pid_from_js: JsNumber, killer_exe_path: JsString) -> Result<bool> { | ||
| let pid = match pid_from_js.get_int32() { | ||
| Ok(child) => child, | ||
| Err(error) => { | ||
| return Err(napi::Error { | ||
| status: Status::GenericFailure, | ||
| reason: format!("unable to parse passed pid: {}", error), | ||
| }); | ||
| } | ||
| }; | ||
| let killer_exe = match OsString::try_from(killer_exe_path.into_utf8()?.into_owned()?) { | ||
| Ok(child) => child, | ||
| Err(error) => { | ||
| return Err(napi::Error { | ||
| status: Status::GenericFailure, | ||
| reason: format!("unable to kind process killer executable: {}", error), | ||
| }); | ||
| } | ||
| }; | ||
|
|
||
| fn ignore_signal(ignore: bool) { | ||
| unsafe { | ||
| SetConsoleCtrlHandler(None, true); | ||
| }; | ||
|
|
||
| let mut killer = match Command::new(killer_exe).arg(format!("{:?}", pid)).spawn() { | ||
| Ok(child) => child, | ||
| Err(error) => { | ||
| return Err(napi::Error { | ||
| status: Status::GenericFailure, | ||
| reason: format!("unable to spawn process to kill pid: {}", error), | ||
| }); | ||
| } | ||
| }; | ||
|
|
||
| let status = killer.wait(); | ||
|
|
||
| unsafe { SetConsoleCtrlHandler(None, false) }; | ||
|
|
||
| match status { | ||
| Ok(status) if status.success() => Ok(true), | ||
| Ok(_) => Err(Error::new( | ||
| Status::GenericFailure, | ||
| format!("unable to kill process with pid '{:?}'", pid), | ||
| )), | ||
| Err(error) => Err(Error::new(Status::GenericFailure, format!("{}", error))), | ||
| SetConsoleCtrlHandler(None, ignore); | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the only thing that we can't do in nodejs |
||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bundlers like webpack or rspack won't be able to parse non literal paths in require.