Skip to content

Commit

Permalink
Adding support to process for operating system signals (#209)
Browse files Browse the repository at this point in the history
* Patching event-loop to latest version

* Adding signals binding

* Adding support for OS signal listeners

* Adding signals JS example

* Refactoring

* Linking event-loop as a separate crate

* Refactoring

* Fixing clippy warnings

* Fixing compilation issues on Windows
  • Loading branch information
aalykiot committed Apr 9, 2024
1 parent 50b05c4 commit 9cefa5d
Show file tree
Hide file tree
Showing 18 changed files with 365 additions and 1,284 deletions.
47 changes: 47 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 9 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ name = "dune"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[target.'cfg(unix)'.dependencies]
nix = { version = "0.28.0", features = ["signal"] }

[target.'cfg(windows)'.dependencies]
enable-ansi-support = "0.2.1"
[dependencies.dune_event_loop]
git = "https://github.com/aalykiot/dune-event-loop"
branch = "main"

[dependencies]
v8 = { version = "0.89.0", default-features = false }
Expand Down Expand Up @@ -66,5 +64,11 @@ tokio = { version = "1.37.0", features = ["full"] }
axum = { version = "0.7.5", features = ["ws"] }
uuid = { version = "1.8.0", features = ["v4", "fast-rng"] }

[target.'cfg(unix)'.dependencies]
nix = { version = "0.28.0", features = ["signal"] }

[target.'cfg(windows)'.dependencies]
enable-ansi-support = "0.2.1"

[dev-dependencies]
assert_fs = "1.1.1"
17 changes: 17 additions & 0 deletions examples/signals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import http from 'http';

// We need somehow to keep the event-loop alive.
http.createServer(() => {}).listen(3000);

let shouldExit = false;

// Exit on fast double CTRL+C key press.
const onSignal = () => {
if (shouldExit) process.exit(0);
shouldExit = true;
setTimeout(() => {
shouldExit = false;
}, 500);
};

process.on('SIGINT', onSignal);
2 changes: 2 additions & 0 deletions src/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::net;
use crate::perf_hooks;
use crate::process;
use crate::promise;
use crate::signals;
use crate::stdio;
use crate::timers;
use anyhow::Error;
Expand All @@ -28,6 +29,7 @@ lazy_static! {
("net", net::initialize),
("promise", promise::initialize),
("http_parser", http_parser::initialize),
("signals", signals::initialize),
];
HashMap::from_iter(bindings.into_iter())
};
Expand Down
4 changes: 2 additions & 2 deletions src/dns.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::bindings::set_exception_code;
use crate::bindings::set_function_to;
use crate::bindings::set_property_to;
use crate::event_loop::LoopHandle;
use crate::event_loop::TaskResult;
use crate::runtime::JsFuture;
use crate::runtime::JsRuntime;
use anyhow::Result;
use dns_lookup::lookup_host;
use dune_event_loop::LoopHandle;
use dune_event_loop::TaskResult;
use std::net::IpAddr;

pub fn initialize(scope: &mut v8::HandleScope) -> v8::Global<v8::Object> {
Expand Down
Loading

0 comments on commit 9cefa5d

Please sign in to comment.